2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
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>
35 #include <ipxe/xenstore.h>
36 #include <ipxe/xenbus.h>
37 #include <ipxe/xengrant.h>
38 #include <ipxe/xenevent.h>
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, \
58 #define EIO_NETIF_RSP( status ) \
59 EUNIQ ( EINFO_EIO, -(status), \
60 EIO_NETIF_RSP_ERROR, EIO_NETIF_RSP_DROPPED )
62 /******************************************************************************
66 ******************************************************************************
72 * @v netfront Netfront device
73 * @ret rc Return status code
75 static int netfront_reset ( struct netfront_nic *netfront ) {
76 struct xen_device *xendev = netfront->xendev;
80 /* Get current backend state */
81 if ( ( state = xenbus_backend_state ( xendev ) ) < 0 ) {
83 DBGC ( netfront, "NETFRONT %s could not read backend state: "
84 "%s\n", xendev->key, strerror ( rc ) );
88 /* If the backend is not already in InitWait, then mark
89 * frontend as Closed to shut down the backend.
91 if ( state != XenbusStateInitWait ) {
93 /* Set state to Closed */
94 xenbus_set_state ( xendev, XenbusStateClosed );
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 ) );
105 /* Reset state to Initialising */
106 xenbus_set_state ( xendev, XenbusStateInitialising );
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 ) );
121 * @v netfront Netfront device
122 * @v hw_addr Hardware address to fill in
123 * @ret rc Return status code
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;
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;
138 DBGC2 ( netfront, "NETFRONT %s has MAC address \"%s\"\n",
141 /* Decode MAC address */
142 len = hex_decode ( ':', mac, hw_addr, ETH_ALEN );
145 DBGC ( netfront, "NETFRONT %s could not decode MAC address "
146 "\"%s\": %s\n", xendev->key, mac, strerror ( rc ) );
160 * Write XenStore numeric value
162 * @v netfront Netfront device
164 * @v num Numeric value
165 * @ret rc Return status code
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;
174 if ( ( rc = xenstore_write_num ( xen, num, xendev->key, subkey,
176 DBGC ( netfront, "NETFRONT %s could not set %s=\"%ld\": %s\n",
177 xendev->key, subkey, num, strerror ( rc ) );
185 * Write XenStore flag value
187 * @v netfront Netfront device
189 * @v num Numeric value
190 * @ret rc Return status code
192 static int netfront_write_flag ( struct netfront_nic *netfront,
193 const char *subkey ) {
195 return netfront_write_num ( netfront, subkey, 1 );
199 * Delete XenStore value
201 * @v netfront Netfront device
203 * @ret rc Return status code
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;
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 ) );
220 /******************************************************************************
224 ******************************************************************************
228 * Create event channel
230 * @v netfront Netfront device
231 * @ret rc Return status code
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;
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;
250 netfront->event.port = alloc_unbound.port;
252 /* Publish event channel */
253 if ( ( rc = netfront_write_num ( netfront, "event-channel",
254 netfront->event.port ) ) != 0 )
257 DBGC ( netfront, "NETFRONT %s event-channel=\"%d\"\n",
258 xendev->key, netfront->event.port );
261 netfront_rm ( netfront, "event-channel" );
263 close.port = netfront->event.port;
264 xenevent_close ( xen, &close );
272 * @v netfront Netfront device
273 * @ret rc Return status code
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;
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 ) );
294 * Destroy event channel
296 * @v netfront Netfront device
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;
303 /* Unpublish event channel */
304 netfront_rm ( netfront, "event-channel" );
306 /* Close event channel */
307 close.port = netfront->event.port;
308 xenevent_close ( xen, &close );
311 /******************************************************************************
315 ******************************************************************************
319 * Create descriptor ring
321 * @v netfront Netfront device
322 * @v ring Descriptor ring
323 * @ret rc Return status code
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;
332 /* Initialise buffer ID ring */
333 for ( i = 0 ; i < ring->count ; i++ ) {
335 assert ( ring->iobufs[i] == NULL );
340 /* Allocate and initialise shared ring */
341 ring->sring.raw = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
342 if ( ! ring->sring.raw ) {
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;
356 /* Publish shared ring reference */
357 if ( ( rc = netfront_write_num ( netfront, ring->ref_key,
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 ) );
367 netfront_rm ( netfront, ring->ref_key );
369 xengrant_invalidate ( xen, ring->ref );
371 free_dma ( ring->sring.raw, PAGE_SIZE );
377 * Add buffer to descriptor ring
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
386 * The caller is responsible for ensuring that there is space in the
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;
399 assert ( ! netfront_ring_is_full ( ring ) );
401 /* Allocate buffer ID */
402 next_id = ring->ids[ ring->id_prod & ( ring->count - 1 ) ];
403 next_ref = ring->refs[next_id];
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
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 ) );
417 /* Store I/O buffer */
418 assert ( ring->iobufs[next_id] == NULL );
419 ring->iobufs[next_id] = iobuf;
421 /* Consume buffer ID */
424 /* Return buffer ID and grant reference */
432 * Remove buffer from descriptor ring
434 * @v netfront Netfront device
435 * @v ring Descriptor ring
437 * @ret iobuf I/O buffer
439 static struct io_buffer * netfront_pull ( struct netfront_nic *netfront,
440 struct netfront_ring *ring,
442 struct xen_device *xendev = netfront->xendev;
443 struct xen_hypervisor *xen = xendev->xen;
444 struct io_buffer *iobuf;
447 assert ( id < ring->count );
449 /* Revoke access from I/O buffer page */
450 xengrant_invalidate ( xen, ring->refs[id] );
452 /* Retrieve I/O buffer */
453 iobuf = ring->iobufs[id];
454 assert ( iobuf != NULL );
455 ring->iobufs[id] = NULL;
458 ring->ids[ ( ring->id_cons++ ) & ( ring->count - 1 ) ] = id;
464 * Destroy descriptor ring
466 * @v netfront Netfront device
467 * @v ring Descriptor ring
468 * @v discard Method used to discard outstanding buffer, or NULL
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;
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 );
486 /* Unpublish shared ring reference */
487 netfront_rm ( netfront, ring->ref_key );
489 /* Revoke access from shared ring */
490 xengrant_invalidate ( xen, ring->ref );
493 free_dma ( ring->sring.raw, PAGE_SIZE );
494 ring->sring.raw = NULL;
497 /******************************************************************************
499 * Network device interface
501 ******************************************************************************
505 * Refill receive descriptor ring
507 * @v netdev Network device
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;
517 /* Do nothing if ring is already full */
518 if ( netfront_ring_is_full ( &netfront->rx ) )
524 /* Allocate I/O buffer */
525 iobuf = alloc_iob ( PAGE_SIZE );
527 /* Wait for next refill */
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,
536 &request->gref ) ) != 0 ) {
537 netdev_rx_err ( netdev, iobuf, rc );
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 ) );
544 /* Move to next descriptor */
545 netfront->rx_fring.req_prod_pvt++;
547 } while ( ! netfront_ring_is_full ( &netfront->rx ) );
549 /* Push new descriptors and notify backend if applicable */
550 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->rx_fring, notify );
552 netfront_send_event ( netfront );
556 * Open network device
558 * @v netdev Network device
559 * @ret rc Return status code
561 static int netfront_open ( struct net_device *netdev ) {
562 struct netfront_nic *netfront = netdev->priv;
563 struct xen_device *xendev = netfront->xendev;
566 /* Ensure device is in a suitable initial state */
567 if ( ( rc = netfront_reset ( netfront ) ) != 0 )
570 /* Create transmit descriptor ring */
571 if ( ( rc = netfront_create_ring ( netfront, &netfront->tx ) ) != 0 )
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 );
577 /* Create receive descriptor ring */
578 if ( ( rc = netfront_create_ring ( netfront, &netfront->rx ) ) != 0 )
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 );
584 /* Create event channel */
585 if ( ( rc = netfront_create_event ( netfront ) ) != 0 )
586 goto err_create_event;
588 /* "Request" the rx-copy feature. Current versions of
589 * xen_netback.ko will fail silently if this parameter is not
592 if ( ( rc = netfront_write_flag ( netfront, "request-rx-copy" ) ) != 0 )
593 goto err_request_rx_copy;
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;
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;
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 ) );
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;
619 /* Refill receive descriptor ring */
620 netfront_refill_rx ( netdev );
623 netdev_link_up ( netdev );
628 netfront_reset ( netfront );
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" );
636 netfront_destroy_event ( netfront );
638 netfront_destroy_ring ( netfront, &netfront->rx, NULL );
640 netfront_destroy_ring ( netfront, &netfront->tx, NULL );
647 * Close network device
649 * @v netdev Network device
651 static void netfront_close ( struct net_device *netdev ) {
652 struct netfront_nic *netfront = netdev->priv;
653 struct xen_device *xendev = netfront->xendev;
656 /* Reset devic, thereby ensuring that grant references are no
657 * longer in use, etc.
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.
669 netdev_link_err ( netdev, rc );
671 netdev_link_down ( netdev );
675 netfront_rm ( netfront, "feature-rx-notify" );
676 netfront_rm ( netfront, "feature-no-csum-offload" );
677 netfront_rm ( netfront, "request-rx-copy" );
679 /* Destroy event channel */
680 netfront_destroy_event ( netfront );
682 /* Destroy receive descriptor ring, freeing any outstanding
685 netfront_destroy_ring ( netfront, &netfront->rx, free_iob );
687 /* Destroy transmit descriptor ring. Leave any outstanding
688 * I/O buffers to be freed by netdev_tx_flush().
690 netfront_destroy_ring ( netfront, &netfront->tx, NULL );
696 * @v netdev Network device
697 * @v iobuf I/O buffer
698 * @ret rc Return status code
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;
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",
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 ) {
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 ) );
729 /* Consume descriptor */
730 netfront->tx_fring.req_prod_pvt++;
732 /* Push new descriptor and notify backend if applicable */
733 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->tx_fring, notify );
735 netfront_send_event ( netfront );
741 * Poll for completed packets
743 * @v netdev Network device
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;
753 /* Consume any unconsumed responses */
754 while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->tx_fring ) ) {
756 /* Get next response */
757 response = RING_GET_RESPONSE ( &netfront->tx_fring,
758 netfront->tx_fring.rsp_cons++ );
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 );
768 rc = -EIO_NETIF_RSP ( status );
769 DBGC2 ( netfront, "NETFRONT %s TX id %d error %d: %s\n",
770 xendev->key, response->id, status,
772 netdev_tx_complete_err ( netdev, iobuf, rc );
778 * Poll for received packets
780 * @v netdev Network device
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;
791 /* Consume any unconsumed responses */
792 while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->rx_fring ) ) {
794 /* Get next response */
795 response = RING_GET_RESPONSE ( &netfront->rx_fring,
796 netfront->rx_fring.rsp_cons++ );
798 /* Retrieve from descriptor ring */
799 iobuf = netfront_pull ( netfront, &netfront->rx, response->id );
800 status = response->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 );
810 rc = -EIO_NETIF_RSP ( status );
811 DBGC2 ( netfront, "NETFRONT %s RX id %d error %d: %s\n",
812 xendev->key, response->id, status,
814 netdev_rx_err ( netdev, iobuf, rc );
820 * Poll for completed and received packets
822 * @v netdev Network device
824 static void netfront_poll ( struct net_device *netdev ) {
826 /* Poll for TX completions */
827 netfront_poll_tx ( netdev );
829 /* Poll for RX completions */
830 netfront_poll_rx ( netdev );
832 /* Refill RX descriptor ring */
833 netfront_refill_rx ( netdev );
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,
844 /******************************************************************************
846 * Xen device bus interface
848 ******************************************************************************
854 * @v xendev Xen device
855 * @ret rc Return status code
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;
863 /* Allocate and initialise structure */
864 netdev = alloc_etherdev ( sizeof ( *netfront ) );
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 );
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;
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],
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],
894 /* Fetch MAC address */
895 if ( ( rc = netfront_read_mac ( netfront, netdev->hw_addr ) ) != 0 )
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.
902 netfront_reset ( netfront );
904 /* Register network device */
905 if ( ( rc = register_netdev ( netdev ) ) != 0 )
906 goto err_register_netdev;
908 /* Set initial link state */
909 netdev_link_down ( netdev );
911 xen_set_drvdata ( xendev, netdev );
914 unregister_netdev ( netdev );
917 xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
919 netdev_nullify ( netdev );
920 netdev_put ( netdev );
928 * @v xendev Xen device
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;
935 /* Unregister network device */
936 unregister_netdev ( netdev );
939 xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
941 /* Free network device */
942 netdev_nullify ( netdev );
943 netdev_put ( netdev );
946 /** Xen netfront driver */
947 struct xen_driver netfront_driver __xen_driver = {
950 .probe = netfront_probe,
951 .remove = netfront_remove,