These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / netfront.c
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <ipxe/netdevice.h>
30 #include <ipxe/ethernet.h>
31 #include <ipxe/if_ether.h>
32 #include <ipxe/malloc.h>
33 #include <ipxe/base16.h>
34 #include <ipxe/xen.h>
35 #include <ipxe/xenstore.h>
36 #include <ipxe/xenbus.h>
37 #include <ipxe/xengrant.h>
38 #include <ipxe/xenevent.h>
39 #include "netfront.h"
40
41 /** @file
42  *
43  * Xen netfront driver
44  *
45  */
46
47 /* Disambiguate the various error causes */
48 #define EIO_NETIF_RSP_ERROR                                             \
49         __einfo_error ( EINFO_EIO_NETIF_RSP_ERROR )
50 #define EINFO_EIO_NETIF_RSP_ERROR                                       \
51         __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_ERROR,                  \
52                           "Unspecified network error" )
53 #define EIO_NETIF_RSP_DROPPED                                           \
54         __einfo_error ( EINFO_EIO_NETIF_RSP_DROPPED )
55 #define EINFO_EIO_NETIF_RSP_DROPPED                                     \
56         __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_DROPPED,                \
57                           "Packet dropped" )
58 #define EIO_NETIF_RSP( status )                                         \
59         EUNIQ ( EINFO_EIO, -(status),                                   \
60                 EIO_NETIF_RSP_ERROR, EIO_NETIF_RSP_DROPPED )
61
62 /******************************************************************************
63  *
64  * XenStore interface
65  *
66  ******************************************************************************
67  */
68
69 /**
70  * Reset device
71  *
72  * @v netfront          Netfront device
73  * @ret rc              Return status code
74  */
75 static int netfront_reset ( struct netfront_nic *netfront ) {
76         struct xen_device *xendev = netfront->xendev;
77         int state;
78         int rc;
79
80         /* Get current backend state */
81         if ( ( state = xenbus_backend_state ( xendev ) ) < 0 ) {
82                 rc = state;
83                 DBGC ( netfront, "NETFRONT %s could not read backend state: "
84                        "%s\n", xendev->key, strerror ( rc ) );
85                 return rc;
86         }
87
88         /* If the backend is not already in InitWait, then mark
89          * frontend as Closed to shut down the backend.
90          */
91         if ( state != XenbusStateInitWait ) {
92
93                 /* Set state to Closed */
94                 xenbus_set_state ( xendev, XenbusStateClosed );
95
96                 /* Wait for backend to reach Closed */
97                 if ( ( rc = xenbus_backend_wait ( xendev,
98                                                   XenbusStateClosed ) ) != 0 ) {
99                         DBGC ( netfront, "NETFRONT %s backend did not reach "
100                                "Closed: %s\n", xendev->key, strerror ( rc ) );
101                         return rc;
102                 }
103         }
104
105         /* Reset state to Initialising */
106         xenbus_set_state ( xendev, XenbusStateInitialising );
107
108         /* Wait for backend to reach InitWait */
109         if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateInitWait ) ) != 0){
110                 DBGC ( netfront, "NETFRONT %s backend did not reach InitWait: "
111                        "%s\n", xendev->key, strerror ( rc ) );
112                 return rc;
113         }
114
115         return 0;
116 }
117
118 /**
119  * Fetch MAC address
120  *
121  * @v netfront          Netfront device
122  * @v hw_addr           Hardware address to fill in
123  * @ret rc              Return status code
124  */
125 static int netfront_read_mac ( struct netfront_nic *netfront, void *hw_addr ) {
126         struct xen_device *xendev = netfront->xendev;
127         struct xen_hypervisor *xen = xendev->xen;
128         char *mac;
129         int len;
130         int rc;
131
132         /* Fetch MAC address */
133         if ( ( rc = xenstore_read ( xen, &mac, xendev->key, "mac", NULL ) )!=0){
134                 DBGC ( netfront, "NETFRONT %s could not read MAC address: %s\n",
135                        xendev->key, strerror ( rc ) );
136                 goto err_xenstore_read;
137         }
138         DBGC2 ( netfront, "NETFRONT %s has MAC address \"%s\"\n",
139                 xendev->key, mac );
140
141         /* Decode MAC address */
142         len = hex_decode ( ':', mac, hw_addr, ETH_ALEN );
143         if ( len < 0 ) {
144                 rc = len;
145                 DBGC ( netfront, "NETFRONT %s could not decode MAC address "
146                        "\"%s\": %s\n", xendev->key, mac, strerror ( rc ) );
147                 goto err_decode;
148         }
149
150         /* Success */
151         rc = 0;
152
153  err_decode:
154         free ( mac );
155  err_xenstore_read:
156         return rc;
157 }
158
159 /**
160  * Write XenStore numeric value
161  *
162  * @v netfront          Netfront device
163  * @v subkey            Subkey
164  * @v num               Numeric value
165  * @ret rc              Return status code
166  */
167 static int netfront_write_num ( struct netfront_nic *netfront,
168                                 const char *subkey, unsigned long num ) {
169         struct xen_device *xendev = netfront->xendev;
170         struct xen_hypervisor *xen = xendev->xen;
171         int rc;
172
173         /* Write value */
174         if ( ( rc = xenstore_write_num ( xen, num, xendev->key, subkey,
175                                          NULL ) ) != 0 ) {
176                 DBGC ( netfront, "NETFRONT %s could not set %s=\"%ld\": %s\n",
177                        xendev->key, subkey, num, strerror ( rc ) );
178                 return rc;
179         }
180
181         return 0;
182 }
183
184 /**
185  * Write XenStore flag value
186  *
187  * @v netfront          Netfront device
188  * @v subkey            Subkey
189  * @v num               Numeric value
190  * @ret rc              Return status code
191  */
192 static int netfront_write_flag ( struct netfront_nic *netfront,
193                                  const char *subkey ) {
194
195         return netfront_write_num ( netfront, subkey, 1 );
196 }
197
198 /**
199  * Delete XenStore value
200  *
201  * @v netfront          Netfront device
202  * @v subkey            Subkey
203  * @ret rc              Return status code
204  */
205 static int netfront_rm ( struct netfront_nic *netfront, const char *subkey ) {
206         struct xen_device *xendev = netfront->xendev;
207         struct xen_hypervisor *xen = xendev->xen;
208         int rc;
209
210         /* Remove value */
211         if ( ( rc = xenstore_rm ( xen, xendev->key, subkey, NULL ) ) != 0 ) {
212                 DBGC ( netfront, "NETFRONT %s could not delete %s: %s\n",
213                        xendev->key, subkey, strerror ( rc ) );
214                 return rc;
215         }
216
217         return 0;
218 }
219
220 /******************************************************************************
221  *
222  * Events
223  *
224  ******************************************************************************
225  */
226
227 /**
228  * Create event channel
229  *
230  * @v netfront          Netfront device
231  * @ret rc              Return status code
232  */
233 static int netfront_create_event ( struct netfront_nic *netfront ) {
234         struct xen_device *xendev = netfront->xendev;
235         struct xen_hypervisor *xen = xendev->xen;
236         struct evtchn_alloc_unbound alloc_unbound;
237         struct evtchn_close close;
238         int xenrc;
239         int rc;
240
241         /* Allocate event */
242         alloc_unbound.dom = DOMID_SELF;
243         alloc_unbound.remote_dom = xendev->backend_id;
244         if ( ( xenrc = xenevent_alloc_unbound ( xen, &alloc_unbound ) ) != 0 ) {
245                 rc = -EXEN ( xenrc );
246                 DBGC ( netfront, "NETFRONT %s could not allocate event: %s\n",
247                        xendev->key, strerror ( rc ) );
248                 goto err_alloc_unbound;
249         }
250         netfront->event.port = alloc_unbound.port;
251
252         /* Publish event channel */
253         if ( ( rc = netfront_write_num ( netfront, "event-channel",
254                                          netfront->event.port ) ) != 0 )
255                 goto err_write_num;
256
257         DBGC ( netfront, "NETFRONT %s event-channel=\"%d\"\n",
258                xendev->key, netfront->event.port );
259         return 0;
260
261         netfront_rm ( netfront, "event-channel" );
262  err_write_num:
263         close.port = netfront->event.port;
264         xenevent_close ( xen, &close );
265  err_alloc_unbound:
266         return rc;
267 }
268
269 /**
270  * Send event
271  *
272  * @v netfront          Netfront device
273  * @ret rc              Return status code
274  */
275 static inline __attribute__ (( always_inline )) int
276 netfront_send_event ( struct netfront_nic *netfront ) {
277         struct xen_device *xendev = netfront->xendev;
278         struct xen_hypervisor *xen = xendev->xen;
279         int xenrc;
280         int rc;
281
282         /* Send event */
283         if ( ( xenrc = xenevent_send ( xen, &netfront->event ) ) != 0 ) {
284                 rc = -EXEN ( xenrc );
285                 DBGC ( netfront, "NETFRONT %s could not send event: %s\n",
286                        xendev->key, strerror ( rc ) );
287                 return rc;
288         }
289
290         return 0;
291 }
292
293 /**
294  * Destroy event channel
295  *
296  * @v netfront          Netfront device
297  */
298 static void netfront_destroy_event ( struct netfront_nic *netfront ) {
299         struct xen_device *xendev = netfront->xendev;
300         struct xen_hypervisor *xen = xendev->xen;
301         struct evtchn_close close;
302
303         /* Unpublish event channel */
304         netfront_rm ( netfront, "event-channel" );
305
306         /* Close event channel */
307         close.port = netfront->event.port;
308         xenevent_close ( xen, &close );
309 }
310
311 /******************************************************************************
312  *
313  * Descriptor rings
314  *
315  ******************************************************************************
316  */
317
318 /**
319  * Create descriptor ring
320  *
321  * @v netfront          Netfront device
322  * @v ring              Descriptor ring
323  * @ret rc              Return status code
324  */
325 static int netfront_create_ring ( struct netfront_nic *netfront,
326                                   struct netfront_ring *ring ) {
327         struct xen_device *xendev = netfront->xendev;
328         struct xen_hypervisor *xen = xendev->xen;
329         unsigned int i;
330         int rc;
331
332         /* Initialise buffer ID ring */
333         for ( i = 0 ; i < ring->count ; i++ ) {
334                 ring->ids[i] = i;
335                 assert ( ring->iobufs[i] == NULL );
336         }
337         ring->id_prod = 0;
338         ring->id_cons = 0;
339
340         /* Allocate and initialise shared ring */
341         ring->sring.raw = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
342         if ( ! ring->sring.raw ) {
343                 rc = -ENOMEM;
344                 goto err_alloc;
345         }
346
347         /* Grant access to shared ring */
348         if ( ( rc = xengrant_permit_access ( xen, ring->ref, xendev->backend_id,
349                                              0, ring->sring.raw ) ) != 0 ) {
350                 DBGC ( netfront, "NETFRONT %s could not permit access to "
351                        "%#08lx: %s\n", xendev->key,
352                        virt_to_phys ( ring->sring.raw ), strerror ( rc ) );
353                 goto err_permit_access;
354         }
355
356         /* Publish shared ring reference */
357         if ( ( rc = netfront_write_num ( netfront, ring->ref_key,
358                                          ring->ref ) ) != 0 )
359                 goto err_write_num;
360
361         DBGC ( netfront, "NETFRONT %s %s=\"%d\" [%08lx,%08lx)\n",
362                xendev->key, ring->ref_key, ring->ref,
363                virt_to_phys ( ring->sring.raw ),
364                ( virt_to_phys ( ring->sring.raw ) + PAGE_SIZE ) );
365         return 0;
366
367         netfront_rm ( netfront, ring->ref_key );
368  err_write_num:
369         xengrant_invalidate ( xen, ring->ref );
370  err_permit_access:
371         free_dma ( ring->sring.raw, PAGE_SIZE );
372  err_alloc:
373         return rc;
374 }
375
376 /**
377  * Add buffer to descriptor ring
378  *
379  * @v netfront          Netfront device
380  * @v ring              Descriptor ring
381  * @v iobuf             I/O buffer
382  * @v id                Buffer ID to fill in
383  * @v ref               Grant reference to fill in
384  * @ret rc              Return status code
385  *
386  * The caller is responsible for ensuring that there is space in the
387  * ring.
388  */
389 static int netfront_push ( struct netfront_nic *netfront,
390                            struct netfront_ring *ring, struct io_buffer *iobuf,
391                            uint16_t *id, grant_ref_t *ref ) {
392         struct xen_device *xendev = netfront->xendev;
393         struct xen_hypervisor *xen = xendev->xen;
394         unsigned int next_id;
395         unsigned int next_ref;
396         int rc;
397
398         /* Sanity check */
399         assert ( ! netfront_ring_is_full ( ring ) );
400
401         /* Allocate buffer ID */
402         next_id = ring->ids[ ring->id_prod & ( ring->count - 1 ) ];
403         next_ref = ring->refs[next_id];
404
405         /* Grant access to I/O buffer page.  I/O buffers are naturally
406          * aligned, so we never need to worry about crossing a page
407          * boundary.
408          */
409         if ( ( rc = xengrant_permit_access ( xen, next_ref, xendev->backend_id,
410                                              0, iobuf->data ) ) != 0 ) {
411                 DBGC ( netfront, "NETFRONT %s could not permit access to "
412                        "%#08lx: %s\n", xendev->key,
413                        virt_to_phys ( iobuf->data ), strerror ( rc ) );
414                 return rc;
415         }
416
417         /* Store I/O buffer */
418         assert ( ring->iobufs[next_id] == NULL );
419         ring->iobufs[next_id] = iobuf;
420
421         /* Consume buffer ID */
422         ring->id_prod++;
423
424         /* Return buffer ID and grant reference */
425         *id = next_id;
426         *ref = next_ref;
427
428         return 0;
429 }
430
431 /**
432  * Remove buffer from descriptor ring
433  *
434  * @v netfront          Netfront device
435  * @v ring              Descriptor ring
436  * @v id                Buffer ID
437  * @ret iobuf           I/O buffer
438  */
439 static struct io_buffer * netfront_pull ( struct netfront_nic *netfront,
440                                           struct netfront_ring *ring,
441                                           unsigned int id ) {
442         struct xen_device *xendev = netfront->xendev;
443         struct xen_hypervisor *xen = xendev->xen;
444         struct io_buffer *iobuf;
445
446         /* Sanity check */
447         assert ( id < ring->count );
448
449         /* Revoke access from I/O buffer page */
450         xengrant_invalidate ( xen, ring->refs[id] );
451
452         /* Retrieve I/O buffer */
453         iobuf = ring->iobufs[id];
454         assert ( iobuf != NULL );
455         ring->iobufs[id] = NULL;
456
457         /* Free buffer ID */
458         ring->ids[ ( ring->id_cons++ ) & ( ring->count - 1 ) ] = id;
459
460         return iobuf;
461 }
462
463 /**
464  * Destroy descriptor ring
465  *
466  * @v netfront          Netfront device
467  * @v ring              Descriptor ring
468  * @v discard           Method used to discard outstanding buffer, or NULL
469  */
470 static void netfront_destroy_ring ( struct netfront_nic *netfront,
471                                     struct netfront_ring *ring,
472                                     void ( * discard ) ( struct io_buffer * ) ){
473         struct xen_device *xendev = netfront->xendev;
474         struct xen_hypervisor *xen = xendev->xen;
475         struct io_buffer *iobuf;
476         unsigned int id;
477
478         /* Flush any outstanding buffers */
479         while ( ! netfront_ring_is_empty ( ring ) ) {
480                 id = ring->ids[ ring->id_cons & ( ring->count - 1 ) ];
481                 iobuf = netfront_pull ( netfront, ring, id );
482                 if ( discard )
483                         discard ( iobuf );
484         }
485
486         /* Unpublish shared ring reference */
487         netfront_rm ( netfront, ring->ref_key );
488
489         /* Revoke access from shared ring */
490         xengrant_invalidate ( xen, ring->ref );
491
492         /* Free page */
493         free_dma ( ring->sring.raw, PAGE_SIZE );
494         ring->sring.raw = NULL;
495 }
496
497 /******************************************************************************
498  *
499  * Network device interface
500  *
501  ******************************************************************************
502  */
503
504 /**
505  * Refill receive descriptor ring
506  *
507  * @v netdev            Network device
508  */
509 static void netfront_refill_rx ( struct net_device *netdev ) {
510         struct netfront_nic *netfront = netdev->priv;
511         struct xen_device *xendev = netfront->xendev;
512         struct io_buffer *iobuf;
513         struct netif_rx_request *request;
514         int notify;
515         int rc;
516
517         /* Do nothing if ring is already full */
518         if ( netfront_ring_is_full ( &netfront->rx ) )
519                 return;
520
521         /* Refill ring */
522         do {
523
524                 /* Allocate I/O buffer */
525                 iobuf = alloc_iob ( PAGE_SIZE );
526                 if ( ! iobuf ) {
527                         /* Wait for next refill */
528                         break;
529                 }
530
531                 /* Add to descriptor ring */
532                 request = RING_GET_REQUEST ( &netfront->rx_fring,
533                                              netfront->rx_fring.req_prod_pvt );
534                 if ( ( rc = netfront_push ( netfront, &netfront->rx,
535                                             iobuf, &request->id,
536                                             &request->gref ) ) != 0 ) {
537                         netdev_rx_err ( netdev, iobuf, rc );
538                         break;
539                 }
540                 DBGC2 ( netfront, "NETFRONT %s RX id %d ref %d is %#08lx+%zx\n",
541                         xendev->key, request->id, request->gref,
542                         virt_to_phys ( iobuf->data ), iob_tailroom ( iobuf ) );
543
544                 /* Move to next descriptor */
545                 netfront->rx_fring.req_prod_pvt++;
546
547         } while ( ! netfront_ring_is_full ( &netfront->rx ) );
548
549         /* Push new descriptors and notify backend if applicable */
550         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->rx_fring, notify );
551         if ( notify )
552                 netfront_send_event ( netfront );
553 }
554
555 /**
556  * Open network device
557  *
558  * @v netdev            Network device
559  * @ret rc              Return status code
560  */
561 static int netfront_open ( struct net_device *netdev ) {
562         struct netfront_nic *netfront = netdev->priv;
563         struct xen_device *xendev = netfront->xendev;
564         int rc;
565
566         /* Ensure device is in a suitable initial state */
567         if ( ( rc = netfront_reset ( netfront ) ) != 0 )
568                 goto err_reset;
569
570         /* Create transmit descriptor ring */
571         if ( ( rc = netfront_create_ring ( netfront, &netfront->tx ) ) != 0 )
572                 goto err_create_tx;
573         SHARED_RING_INIT ( netfront->tx_sring );
574         FRONT_RING_INIT ( &netfront->tx_fring, netfront->tx_sring, PAGE_SIZE );
575         assert ( RING_SIZE ( &netfront->tx_fring ) >= netfront->tx.count );
576
577         /* Create receive descriptor ring */
578         if ( ( rc = netfront_create_ring ( netfront, &netfront->rx ) ) != 0 )
579                 goto err_create_rx;
580         SHARED_RING_INIT ( netfront->rx_sring );
581         FRONT_RING_INIT ( &netfront->rx_fring, netfront->rx_sring, PAGE_SIZE );
582         assert ( RING_SIZE ( &netfront->rx_fring ) >= netfront->rx.count );
583
584         /* Create event channel */
585         if ( ( rc = netfront_create_event ( netfront ) ) != 0 )
586                 goto err_create_event;
587
588         /* "Request" the rx-copy feature.  Current versions of
589          * xen_netback.ko will fail silently if this parameter is not
590          * present.
591          */
592         if ( ( rc = netfront_write_flag ( netfront, "request-rx-copy" ) ) != 0 )
593                 goto err_request_rx_copy;
594
595         /* Disable checksum offload, since we will always do the work anyway */
596         if ( ( rc = netfront_write_flag ( netfront,
597                                           "feature-no-csum-offload" ) ) != 0 )
598                 goto err_feature_no_csum_offload;
599
600         /* Inform backend that we will send notifications for RX requests */
601         if ( ( rc = netfront_write_flag ( netfront,
602                                           "feature-rx-notify" ) ) != 0 )
603                 goto err_feature_rx_notify;
604
605         /* Set state to Connected */
606         if ( ( rc = xenbus_set_state ( xendev, XenbusStateConnected ) ) != 0 ) {
607                 DBGC ( netfront, "NETFRONT %s could not set state=\"%d\": %s\n",
608                        xendev->key, XenbusStateConnected, strerror ( rc ) );
609                 goto err_set_state;
610         }
611
612         /* Wait for backend to connect */
613         if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateConnected ) ) !=0){
614                 DBGC ( netfront, "NETFRONT %s could not connect to backend: "
615                        "%s\n", xendev->key, strerror ( rc ) );
616                 goto err_backend_wait;
617         }
618
619         /* Refill receive descriptor ring */
620         netfront_refill_rx ( netdev );
621
622         /* Set link up */
623         netdev_link_up ( netdev );
624
625         return 0;
626
627  err_backend_wait:
628         netfront_reset ( netfront );
629  err_set_state:
630         netfront_rm ( netfront, "feature-rx-notify" );
631  err_feature_rx_notify:
632         netfront_rm ( netfront, "feature-no-csum-offload" );
633  err_feature_no_csum_offload:
634         netfront_rm ( netfront, "request-rx-copy" );
635  err_request_rx_copy:
636         netfront_destroy_event ( netfront );
637  err_create_event:
638         netfront_destroy_ring ( netfront, &netfront->rx, NULL );
639  err_create_rx:
640         netfront_destroy_ring ( netfront, &netfront->tx, NULL );
641  err_create_tx:
642  err_reset:
643         return rc;
644 }
645
646 /**
647  * Close network device
648  *
649  * @v netdev            Network device
650  */
651 static void netfront_close ( struct net_device *netdev ) {
652         struct netfront_nic *netfront = netdev->priv;
653         struct xen_device *xendev = netfront->xendev;
654         int rc;
655
656         /* Reset devic, thereby ensuring that grant references are no
657          * longer in use, etc.
658          */
659         if ( ( rc = netfront_reset ( netfront ) ) != 0 ) {
660                 DBGC ( netfront, "NETFRONT %s could not disconnect from "
661                        "backend: %s\n", xendev->key, strerror ( rc ) );
662                 /* Things will probably go _very_ badly wrong if this
663                  * happens, since it means the backend may still write
664                  * to the outstanding RX buffers that we are about to
665                  * free.  The best we can do is report the error via
666                  * the link status, but there's a good chance the
667                  * machine will crash soon.
668                  */
669                 netdev_link_err ( netdev, rc );
670         } else {
671                 netdev_link_down ( netdev );
672         }
673
674         /* Delete flags */
675         netfront_rm ( netfront, "feature-rx-notify" );
676         netfront_rm ( netfront, "feature-no-csum-offload" );
677         netfront_rm ( netfront, "request-rx-copy" );
678
679         /* Destroy event channel */
680         netfront_destroy_event ( netfront );
681
682         /* Destroy receive descriptor ring, freeing any outstanding
683          * I/O buffers.
684          */
685         netfront_destroy_ring ( netfront, &netfront->rx, free_iob );
686
687         /* Destroy transmit descriptor ring.  Leave any outstanding
688          * I/O buffers to be freed by netdev_tx_flush().
689          */
690         netfront_destroy_ring ( netfront, &netfront->tx, NULL );
691 }
692
693 /**
694  * Transmit packet
695  *
696  * @v netdev            Network device
697  * @v iobuf             I/O buffer
698  * @ret rc              Return status code
699  */
700 static int netfront_transmit ( struct net_device *netdev,
701                                struct io_buffer *iobuf ) {
702         struct netfront_nic *netfront = netdev->priv;
703         struct xen_device *xendev = netfront->xendev;
704         struct netif_tx_request *request;
705         int notify;
706         int rc;
707
708         /* Check that we have space in the ring */
709         if ( netfront_ring_is_full ( &netfront->tx ) ) {
710                 DBGC ( netfront, "NETFRONT %s out of transmit descriptors\n",
711                        xendev->key );
712                 return -ENOBUFS;
713         }
714
715         /* Add to descriptor ring */
716         request = RING_GET_REQUEST ( &netfront->tx_fring,
717                                      netfront->tx_fring.req_prod_pvt );
718         if ( ( rc = netfront_push ( netfront, &netfront->tx, iobuf,
719                                     &request->id, &request->gref ) ) != 0 ) {
720                 return rc;
721         }
722         request->offset = ( virt_to_phys ( iobuf->data ) & ( PAGE_SIZE - 1 ) );
723         request->flags = NETTXF_data_validated;
724         request->size = iob_len ( iobuf );
725         DBGC2 ( netfront, "NETFRONT %s TX id %d ref %d is %#08lx+%zx\n",
726                 xendev->key, request->id, request->gref,
727                 virt_to_phys ( iobuf->data ), iob_len ( iobuf ) );
728
729         /* Consume descriptor */
730         netfront->tx_fring.req_prod_pvt++;
731
732         /* Push new descriptor and notify backend if applicable */
733         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->tx_fring, notify );
734         if ( notify )
735                 netfront_send_event ( netfront );
736
737         return 0;
738 }
739
740 /**
741  * Poll for completed packets
742  *
743  * @v netdev            Network device
744  */
745 static void netfront_poll_tx ( struct net_device *netdev ) {
746         struct netfront_nic *netfront = netdev->priv;
747         struct xen_device *xendev = netfront->xendev;
748         struct netif_tx_response *response;
749         struct io_buffer *iobuf;
750         unsigned int status;
751         int rc;
752
753         /* Consume any unconsumed responses */
754         while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->tx_fring ) ) {
755
756                 /* Get next response */
757                 response = RING_GET_RESPONSE ( &netfront->tx_fring,
758                                                netfront->tx_fring.rsp_cons++ );
759
760                 /* Retrieve from descriptor ring */
761                 iobuf = netfront_pull ( netfront, &netfront->tx, response->id );
762                 status = response->status;
763                 if ( status == NETIF_RSP_OKAY ) {
764                         DBGC2 ( netfront, "NETFRONT %s TX id %d complete\n",
765                                 xendev->key, response->id );
766                         netdev_tx_complete ( netdev, iobuf );
767                 } else {
768                         rc = -EIO_NETIF_RSP ( status );
769                         DBGC2 ( netfront, "NETFRONT %s TX id %d error %d: %s\n",
770                                 xendev->key, response->id, status,
771                                 strerror ( rc ) );
772                         netdev_tx_complete_err ( netdev, iobuf, rc );
773                 }
774         }
775 }
776
777 /**
778  * Poll for received packets
779  *
780  * @v netdev            Network device
781  */
782 static void netfront_poll_rx ( struct net_device *netdev ) {
783         struct netfront_nic *netfront = netdev->priv;
784         struct xen_device *xendev = netfront->xendev;
785         struct netif_rx_response *response;
786         struct io_buffer *iobuf;
787         int status;
788         size_t len;
789         int rc;
790
791         /* Consume any unconsumed responses */
792         while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->rx_fring ) ) {
793
794                 /* Get next response */
795                 response = RING_GET_RESPONSE ( &netfront->rx_fring,
796                                                netfront->rx_fring.rsp_cons++ );
797
798                 /* Retrieve from descriptor ring */
799                 iobuf = netfront_pull ( netfront, &netfront->rx, response->id );
800                 status = response->status;
801                 if ( status >= 0 ) {
802                         len = status;
803                         iob_reserve ( iobuf, response->offset );
804                         iob_put ( iobuf, len );
805                         DBGC2 ( netfront, "NETFRONT %s RX id %d complete "
806                                 "%#08lx+%zx\n", xendev->key, response->id,
807                                 virt_to_phys ( iobuf->data ), len );
808                         netdev_rx ( netdev, iobuf );
809                 } else {
810                         rc = -EIO_NETIF_RSP ( status );
811                         DBGC2 ( netfront, "NETFRONT %s RX id %d error %d: %s\n",
812                                 xendev->key, response->id, status,
813                                 strerror ( rc ) );
814                         netdev_rx_err ( netdev, iobuf, rc );
815                 }
816         }
817 }
818
819 /**
820  * Poll for completed and received packets
821  *
822  * @v netdev            Network device
823  */
824 static void netfront_poll ( struct net_device *netdev ) {
825
826         /* Poll for TX completions */
827         netfront_poll_tx ( netdev );
828
829         /* Poll for RX completions */
830         netfront_poll_rx ( netdev );
831
832         /* Refill RX descriptor ring */
833         netfront_refill_rx ( netdev );
834 }
835
836 /** Network device operations */
837 static struct net_device_operations netfront_operations = {
838         .open           = netfront_open,
839         .close          = netfront_close,
840         .transmit       = netfront_transmit,
841         .poll           = netfront_poll,
842 };
843
844 /******************************************************************************
845  *
846  * Xen device bus interface
847  *
848  ******************************************************************************
849  */
850
851 /**
852  * Probe Xen device
853  *
854  * @v xendev            Xen device
855  * @ret rc              Return status code
856  */
857 static int netfront_probe ( struct xen_device *xendev ) {
858         struct xen_hypervisor *xen = xendev->xen;
859         struct net_device *netdev;
860         struct netfront_nic *netfront;
861         int rc;
862
863         /* Allocate and initialise structure */
864         netdev = alloc_etherdev ( sizeof ( *netfront ) );
865         if ( ! netdev ) {
866                 rc = -ENOMEM;
867                 goto err_alloc;
868         }
869         netdev_init ( netdev, &netfront_operations );
870         netdev->dev = &xendev->dev;
871         netfront = netdev->priv;
872         netfront->xendev = xendev;
873         DBGC ( netfront, "NETFRONT %s backend=\"%s\" in domain %ld\n",
874                xendev->key, xendev->backend, xendev->backend_id );
875
876         /* Allocate grant references and initialise descriptor rings */
877         if ( ( rc = xengrant_alloc ( xen, netfront->refs,
878                                      NETFRONT_REF_COUNT ) ) != 0 ) {
879                 DBGC ( netfront, "NETFRONT %s could not allocate grant "
880                        "references: %s\n", xendev->key, strerror ( rc ) );
881                 goto err_grant_alloc;
882         }
883         netfront_init_ring ( &netfront->tx, "tx-ring-ref",
884                              netfront->refs[NETFRONT_REF_TX_RING],
885                              NETFRONT_NUM_TX_DESC, netfront->tx_iobufs,
886                              &netfront->refs[NETFRONT_REF_TX_BASE],
887                              netfront->tx_ids );
888         netfront_init_ring ( &netfront->rx, "rx-ring-ref",
889                              netfront->refs[NETFRONT_REF_RX_RING],
890                              NETFRONT_NUM_RX_DESC, netfront->rx_iobufs,
891                              &netfront->refs[NETFRONT_REF_RX_BASE],
892                              netfront->rx_ids );
893
894         /* Fetch MAC address */
895         if ( ( rc = netfront_read_mac ( netfront, netdev->hw_addr ) ) != 0 )
896                 goto err_read_mac;
897
898         /* Reset device.  Ignore failures; allow the device to be
899          * registered so that reset errors can be observed by the user
900          * when attempting to open the device.
901          */
902         netfront_reset ( netfront );
903
904         /* Register network device */
905         if ( ( rc = register_netdev ( netdev ) ) != 0 )
906                 goto err_register_netdev;
907
908         /* Set initial link state */
909         netdev_link_down ( netdev );
910
911         xen_set_drvdata ( xendev, netdev );
912         return 0;
913
914         unregister_netdev ( netdev );
915  err_register_netdev:
916  err_read_mac:
917         xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
918  err_grant_alloc:
919         netdev_nullify ( netdev );
920         netdev_put ( netdev );
921  err_alloc:
922         return rc;
923 }
924
925 /**
926  * Remove Xen device
927  *
928  * @v xendev            Xen device
929  */
930 static void netfront_remove ( struct xen_device *xendev ) {
931         struct net_device *netdev = xen_get_drvdata ( xendev );
932         struct netfront_nic *netfront = netdev->priv;
933         struct xen_hypervisor *xen = xendev->xen;
934
935         /* Unregister network device */
936         unregister_netdev ( netdev );
937
938         /* Free resources */
939         xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
940
941         /* Free network device */
942         netdev_nullify ( netdev );
943         netdev_put ( netdev );
944 }
945
946 /** Xen netfront driver */
947 struct xen_driver netfront_driver __xen_driver = {
948         .name = "netfront",
949         .type = "vif",
950         .probe = netfront_probe,
951         .remove = netfront_remove,
952 };