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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/netdevice.h>
26 #include <ipxe/ethernet.h>
27 #include <ipxe/if_ether.h>
28 #include <ipxe/malloc.h>
29 #include <ipxe/base16.h>
31 #include <ipxe/xenstore.h>
32 #include <ipxe/xenbus.h>
33 #include <ipxe/xengrant.h>
34 #include <ipxe/xenevent.h>
43 /* Disambiguate the various error causes */
44 #define EIO_NETIF_RSP_ERROR \
45 __einfo_error ( EINFO_EIO_NETIF_RSP_ERROR )
46 #define EINFO_EIO_NETIF_RSP_ERROR \
47 __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_ERROR, \
48 "Unspecified network error" )
49 #define EIO_NETIF_RSP_DROPPED \
50 __einfo_error ( EINFO_EIO_NETIF_RSP_DROPPED )
51 #define EINFO_EIO_NETIF_RSP_DROPPED \
52 __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_DROPPED, \
54 #define EIO_NETIF_RSP( status ) \
55 EUNIQ ( EINFO_EIO, -(status), \
56 EIO_NETIF_RSP_ERROR, EIO_NETIF_RSP_DROPPED )
58 /******************************************************************************
62 ******************************************************************************
68 * @v netfront Netfront device
69 * @ret rc Return status code
71 static int netfront_reset ( struct netfront_nic *netfront ) {
72 struct xen_device *xendev = netfront->xendev;
76 /* Get current backend state */
77 if ( ( state = xenbus_backend_state ( xendev ) ) < 0 ) {
79 DBGC ( netfront, "NETFRONT %s could not read backend state: "
80 "%s\n", xendev->key, strerror ( rc ) );
84 /* If the backend is not already in InitWait, then mark
85 * frontend as Closed to shut down the backend.
87 if ( state != XenbusStateInitWait ) {
89 /* Set state to Closed */
90 xenbus_set_state ( xendev, XenbusStateClosed );
92 /* Wait for backend to reach Closed */
93 if ( ( rc = xenbus_backend_wait ( xendev,
94 XenbusStateClosed ) ) != 0 ) {
95 DBGC ( netfront, "NETFRONT %s backend did not reach "
96 "Closed: %s\n", xendev->key, strerror ( rc ) );
101 /* Reset state to Initialising */
102 xenbus_set_state ( xendev, XenbusStateInitialising );
104 /* Wait for backend to reach InitWait */
105 if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateInitWait ) ) != 0){
106 DBGC ( netfront, "NETFRONT %s backend did not reach InitWait: "
107 "%s\n", xendev->key, strerror ( rc ) );
117 * @v netfront Netfront device
118 * @v hw_addr Hardware address to fill in
119 * @ret rc Return status code
121 static int netfront_read_mac ( struct netfront_nic *netfront, void *hw_addr ) {
122 struct xen_device *xendev = netfront->xendev;
123 struct xen_hypervisor *xen = xendev->xen;
128 /* Fetch MAC address */
129 if ( ( rc = xenstore_read ( xen, &mac, xendev->key, "mac", NULL ) )!=0){
130 DBGC ( netfront, "NETFRONT %s could not read MAC address: %s\n",
131 xendev->key, strerror ( rc ) );
132 goto err_xenstore_read;
134 DBGC2 ( netfront, "NETFRONT %s has MAC address \"%s\"\n",
137 /* Decode MAC address */
138 len = hex_decode ( mac, ':', hw_addr, ETH_ALEN );
141 DBGC ( netfront, "NETFRONT %s could not decode MAC address "
142 "\"%s\": %s\n", xendev->key, mac, strerror ( rc ) );
156 * Write XenStore numeric value
158 * @v netfront Netfront device
160 * @v num Numeric value
161 * @ret rc Return status code
163 static int netfront_write_num ( struct netfront_nic *netfront,
164 const char *subkey, unsigned long num ) {
165 struct xen_device *xendev = netfront->xendev;
166 struct xen_hypervisor *xen = xendev->xen;
170 if ( ( rc = xenstore_write_num ( xen, num, xendev->key, subkey,
172 DBGC ( netfront, "NETFRONT %s could not set %s=\"%ld\": %s\n",
173 xendev->key, subkey, num, strerror ( rc ) );
181 * Write XenStore flag value
183 * @v netfront Netfront device
185 * @v num Numeric value
186 * @ret rc Return status code
188 static int netfront_write_flag ( struct netfront_nic *netfront,
189 const char *subkey ) {
191 return netfront_write_num ( netfront, subkey, 1 );
195 * Delete XenStore value
197 * @v netfront Netfront device
199 * @ret rc Return status code
201 static int netfront_rm ( struct netfront_nic *netfront, const char *subkey ) {
202 struct xen_device *xendev = netfront->xendev;
203 struct xen_hypervisor *xen = xendev->xen;
207 if ( ( rc = xenstore_rm ( xen, xendev->key, subkey, NULL ) ) != 0 ) {
208 DBGC ( netfront, "NETFRONT %s could not delete %s: %s\n",
209 xendev->key, subkey, strerror ( rc ) );
216 /******************************************************************************
220 ******************************************************************************
224 * Create event channel
226 * @v netfront Netfront device
227 * @ret rc Return status code
229 static int netfront_create_event ( struct netfront_nic *netfront ) {
230 struct xen_device *xendev = netfront->xendev;
231 struct xen_hypervisor *xen = xendev->xen;
232 struct evtchn_alloc_unbound alloc_unbound;
233 struct evtchn_close close;
238 alloc_unbound.dom = DOMID_SELF;
239 alloc_unbound.remote_dom = xendev->backend_id;
240 if ( ( xenrc = xenevent_alloc_unbound ( xen, &alloc_unbound ) ) != 0 ) {
241 rc = -EXEN ( xenrc );
242 DBGC ( netfront, "NETFRONT %s could not allocate event: %s\n",
243 xendev->key, strerror ( rc ) );
244 goto err_alloc_unbound;
246 netfront->event.port = alloc_unbound.port;
248 /* Publish event channel */
249 if ( ( rc = netfront_write_num ( netfront, "event-channel",
250 netfront->event.port ) ) != 0 )
253 DBGC ( netfront, "NETFRONT %s event-channel=\"%d\"\n",
254 xendev->key, netfront->event.port );
257 netfront_rm ( netfront, "event-channel" );
259 close.port = netfront->event.port;
260 xenevent_close ( xen, &close );
268 * @v netfront Netfront device
269 * @ret rc Return status code
271 static inline __attribute__ (( always_inline )) int
272 netfront_send_event ( struct netfront_nic *netfront ) {
273 struct xen_device *xendev = netfront->xendev;
274 struct xen_hypervisor *xen = xendev->xen;
279 if ( ( xenrc = xenevent_send ( xen, &netfront->event ) ) != 0 ) {
280 rc = -EXEN ( xenrc );
281 DBGC ( netfront, "NETFRONT %s could not send event: %s\n",
282 xendev->key, strerror ( rc ) );
290 * Destroy event channel
292 * @v netfront Netfront device
294 static void netfront_destroy_event ( struct netfront_nic *netfront ) {
295 struct xen_device *xendev = netfront->xendev;
296 struct xen_hypervisor *xen = xendev->xen;
297 struct evtchn_close close;
299 /* Unpublish event channel */
300 netfront_rm ( netfront, "event-channel" );
302 /* Close event channel */
303 close.port = netfront->event.port;
304 xenevent_close ( xen, &close );
307 /******************************************************************************
311 ******************************************************************************
315 * Create descriptor ring
317 * @v netfront Netfront device
318 * @v ring Descriptor ring
319 * @ret rc Return status code
321 static int netfront_create_ring ( struct netfront_nic *netfront,
322 struct netfront_ring *ring ) {
323 struct xen_device *xendev = netfront->xendev;
324 struct xen_hypervisor *xen = xendev->xen;
328 /* Initialise buffer ID ring */
329 for ( i = 0 ; i < ring->count ; i++ ) {
331 assert ( ring->iobufs[i] == NULL );
336 /* Allocate and initialise shared ring */
337 ring->sring.raw = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
338 if ( ! ring->sring.raw ) {
343 /* Grant access to shared ring */
344 if ( ( rc = xengrant_permit_access ( xen, ring->ref, xendev->backend_id,
345 0, ring->sring.raw ) ) != 0 ) {
346 DBGC ( netfront, "NETFRONT %s could not permit access to "
347 "%#08lx: %s\n", xendev->key,
348 virt_to_phys ( ring->sring.raw ), strerror ( rc ) );
349 goto err_permit_access;
352 /* Publish shared ring reference */
353 if ( ( rc = netfront_write_num ( netfront, ring->ref_key,
357 DBGC ( netfront, "NETFRONT %s %s=\"%d\" [%08lx,%08lx)\n",
358 xendev->key, ring->ref_key, ring->ref,
359 virt_to_phys ( ring->sring.raw ),
360 ( virt_to_phys ( ring->sring.raw ) + PAGE_SIZE ) );
363 netfront_rm ( netfront, ring->ref_key );
365 xengrant_invalidate ( xen, ring->ref );
367 free_dma ( ring->sring.raw, PAGE_SIZE );
373 * Add buffer to descriptor ring
375 * @v netfront Netfront device
376 * @v ring Descriptor ring
377 * @v iobuf I/O buffer
378 * @v id Buffer ID to fill in
379 * @v ref Grant reference to fill in
380 * @ret rc Return status code
382 * The caller is responsible for ensuring that there is space in the
385 static int netfront_push ( struct netfront_nic *netfront,
386 struct netfront_ring *ring, struct io_buffer *iobuf,
387 uint16_t *id, grant_ref_t *ref ) {
388 struct xen_device *xendev = netfront->xendev;
389 struct xen_hypervisor *xen = xendev->xen;
390 unsigned int next_id;
391 unsigned int next_ref;
395 assert ( ! netfront_ring_is_full ( ring ) );
397 /* Allocate buffer ID */
398 next_id = ring->ids[ ring->id_prod & ( ring->count - 1 ) ];
399 next_ref = ring->refs[next_id];
401 /* Grant access to I/O buffer page. I/O buffers are naturally
402 * aligned, so we never need to worry about crossing a page
405 if ( ( rc = xengrant_permit_access ( xen, next_ref, xendev->backend_id,
406 0, iobuf->data ) ) != 0 ) {
407 DBGC ( netfront, "NETFRONT %s could not permit access to "
408 "%#08lx: %s\n", xendev->key,
409 virt_to_phys ( iobuf->data ), strerror ( rc ) );
413 /* Store I/O buffer */
414 assert ( ring->iobufs[next_id] == NULL );
415 ring->iobufs[next_id] = iobuf;
417 /* Consume buffer ID */
420 /* Return buffer ID and grant reference */
428 * Remove buffer from descriptor ring
430 * @v netfront Netfront device
431 * @v ring Descriptor ring
433 * @ret iobuf I/O buffer
435 static struct io_buffer * netfront_pull ( struct netfront_nic *netfront,
436 struct netfront_ring *ring,
438 struct xen_device *xendev = netfront->xendev;
439 struct xen_hypervisor *xen = xendev->xen;
440 struct io_buffer *iobuf;
443 assert ( id < ring->count );
445 /* Revoke access from I/O buffer page */
446 xengrant_invalidate ( xen, ring->refs[id] );
448 /* Retrieve I/O buffer */
449 iobuf = ring->iobufs[id];
450 assert ( iobuf != NULL );
451 ring->iobufs[id] = NULL;
454 ring->ids[ ( ring->id_cons++ ) & ( ring->count - 1 ) ] = id;
460 * Destroy descriptor ring
462 * @v netfront Netfront device
463 * @v ring Descriptor ring
464 * @v discard Method used to discard outstanding buffer, or NULL
466 static void netfront_destroy_ring ( struct netfront_nic *netfront,
467 struct netfront_ring *ring,
468 void ( * discard ) ( struct io_buffer * ) ){
469 struct xen_device *xendev = netfront->xendev;
470 struct xen_hypervisor *xen = xendev->xen;
471 struct io_buffer *iobuf;
474 /* Flush any outstanding buffers */
475 while ( ! netfront_ring_is_empty ( ring ) ) {
476 id = ring->ids[ ring->id_cons & ( ring->count - 1 ) ];
477 iobuf = netfront_pull ( netfront, ring, id );
482 /* Unpublish shared ring reference */
483 netfront_rm ( netfront, ring->ref_key );
485 /* Revoke access from shared ring */
486 xengrant_invalidate ( xen, ring->ref );
489 free_dma ( ring->sring.raw, PAGE_SIZE );
490 ring->sring.raw = NULL;
493 /******************************************************************************
495 * Network device interface
497 ******************************************************************************
501 * Refill receive descriptor ring
503 * @v netdev Network device
505 static void netfront_refill_rx ( struct net_device *netdev ) {
506 struct netfront_nic *netfront = netdev->priv;
507 struct xen_device *xendev = netfront->xendev;
508 struct io_buffer *iobuf;
509 struct netif_rx_request *request;
513 /* Do nothing if ring is already full */
514 if ( netfront_ring_is_full ( &netfront->rx ) )
520 /* Allocate I/O buffer */
521 iobuf = alloc_iob ( PAGE_SIZE );
523 /* Wait for next refill */
527 /* Add to descriptor ring */
528 request = RING_GET_REQUEST ( &netfront->rx_fring,
529 netfront->rx_fring.req_prod_pvt );
530 if ( ( rc = netfront_push ( netfront, &netfront->rx,
532 &request->gref ) ) != 0 ) {
533 netdev_rx_err ( netdev, iobuf, rc );
536 DBGC2 ( netfront, "NETFRONT %s RX id %d ref %d is %#08lx+%zx\n",
537 xendev->key, request->id, request->gref,
538 virt_to_phys ( iobuf->data ), iob_tailroom ( iobuf ) );
540 /* Move to next descriptor */
541 netfront->rx_fring.req_prod_pvt++;
543 } while ( ! netfront_ring_is_full ( &netfront->rx ) );
545 /* Push new descriptors and notify backend if applicable */
546 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->rx_fring, notify );
548 netfront_send_event ( netfront );
552 * Open network device
554 * @v netdev Network device
555 * @ret rc Return status code
557 static int netfront_open ( struct net_device *netdev ) {
558 struct netfront_nic *netfront = netdev->priv;
559 struct xen_device *xendev = netfront->xendev;
562 /* Ensure device is in a suitable initial state */
563 if ( ( rc = netfront_reset ( netfront ) ) != 0 )
566 /* Create transmit descriptor ring */
567 if ( ( rc = netfront_create_ring ( netfront, &netfront->tx ) ) != 0 )
569 SHARED_RING_INIT ( netfront->tx_sring );
570 FRONT_RING_INIT ( &netfront->tx_fring, netfront->tx_sring, PAGE_SIZE );
571 assert ( RING_SIZE ( &netfront->tx_fring ) >= netfront->tx.count );
573 /* Create receive descriptor ring */
574 if ( ( rc = netfront_create_ring ( netfront, &netfront->rx ) ) != 0 )
576 SHARED_RING_INIT ( netfront->rx_sring );
577 FRONT_RING_INIT ( &netfront->rx_fring, netfront->rx_sring, PAGE_SIZE );
578 assert ( RING_SIZE ( &netfront->rx_fring ) >= netfront->rx.count );
580 /* Create event channel */
581 if ( ( rc = netfront_create_event ( netfront ) ) != 0 )
582 goto err_create_event;
584 /* "Request" the rx-copy feature. Current versions of
585 * xen_netback.ko will fail silently if this parameter is not
588 if ( ( rc = netfront_write_flag ( netfront, "request-rx-copy" ) ) != 0 )
589 goto err_request_rx_copy;
591 /* Disable checksum offload, since we will always do the work anyway */
592 if ( ( rc = netfront_write_flag ( netfront,
593 "feature-no-csum-offload" ) ) != 0 )
594 goto err_feature_no_csum_offload;
596 /* Set state to Connected */
597 if ( ( rc = xenbus_set_state ( xendev, XenbusStateConnected ) ) != 0 ) {
598 DBGC ( netfront, "NETFRONT %s could not set state=\"%d\": %s\n",
599 xendev->key, XenbusStateConnected, strerror ( rc ) );
603 /* Wait for backend to connect */
604 if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateConnected ) ) !=0){
605 DBGC ( netfront, "NETFRONT %s could not connect to backend: "
606 "%s\n", xendev->key, strerror ( rc ) );
607 goto err_backend_wait;
610 /* Refill receive descriptor ring */
611 netfront_refill_rx ( netdev );
614 netdev_link_up ( netdev );
619 netfront_reset ( netfront );
621 netfront_rm ( netfront, "feature-no-csum-offload" );
622 err_feature_no_csum_offload:
623 netfront_rm ( netfront, "request-rx-copy" );
625 netfront_destroy_event ( netfront );
627 netfront_destroy_ring ( netfront, &netfront->rx, NULL );
629 netfront_destroy_ring ( netfront, &netfront->tx, NULL );
636 * Close network device
638 * @v netdev Network device
640 static void netfront_close ( struct net_device *netdev ) {
641 struct netfront_nic *netfront = netdev->priv;
642 struct xen_device *xendev = netfront->xendev;
645 /* Reset devic, thereby ensuring that grant references are no
646 * longer in use, etc.
648 if ( ( rc = netfront_reset ( netfront ) ) != 0 ) {
649 DBGC ( netfront, "NETFRONT %s could not disconnect from "
650 "backend: %s\n", xendev->key, strerror ( rc ) );
651 /* Things will probably go _very_ badly wrong if this
652 * happens, since it means the backend may still write
653 * to the outstanding RX buffers that we are about to
654 * free. The best we can do is report the error via
655 * the link status, but there's a good chance the
656 * machine will crash soon.
658 netdev_link_err ( netdev, rc );
660 netdev_link_down ( netdev );
664 netfront_rm ( netfront, "feature-no-csum-offload" );
665 netfront_rm ( netfront, "request-rx-copy" );
667 /* Destroy event channel */
668 netfront_destroy_event ( netfront );
670 /* Destroy receive descriptor ring, freeing any outstanding
673 netfront_destroy_ring ( netfront, &netfront->rx, free_iob );
675 /* Destroy transmit descriptor ring. Leave any outstanding
676 * I/O buffers to be freed by netdev_tx_flush().
678 netfront_destroy_ring ( netfront, &netfront->tx, NULL );
684 * @v netdev Network device
685 * @v iobuf I/O buffer
686 * @ret rc Return status code
688 static int netfront_transmit ( struct net_device *netdev,
689 struct io_buffer *iobuf ) {
690 struct netfront_nic *netfront = netdev->priv;
691 struct xen_device *xendev = netfront->xendev;
692 struct netif_tx_request *request;
696 /* Check that we have space in the ring */
697 if ( netfront_ring_is_full ( &netfront->tx ) ) {
698 DBGC ( netfront, "NETFRONT %s out of transmit descriptors\n",
703 /* Add to descriptor ring */
704 request = RING_GET_REQUEST ( &netfront->tx_fring,
705 netfront->tx_fring.req_prod_pvt );
706 if ( ( rc = netfront_push ( netfront, &netfront->tx, iobuf,
707 &request->id, &request->gref ) ) != 0 ) {
710 request->offset = ( virt_to_phys ( iobuf->data ) & ( PAGE_SIZE - 1 ) );
711 request->flags = NETTXF_data_validated;
712 request->size = iob_len ( iobuf );
713 DBGC2 ( netfront, "NETFRONT %s TX id %d ref %d is %#08lx+%zx\n",
714 xendev->key, request->id, request->gref,
715 virt_to_phys ( iobuf->data ), iob_len ( iobuf ) );
717 /* Consume descriptor */
718 netfront->tx_fring.req_prod_pvt++;
720 /* Push new descriptor and notify backend if applicable */
721 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->tx_fring, notify );
723 netfront_send_event ( netfront );
729 * Poll for completed packets
731 * @v netdev Network device
733 static void netfront_poll_tx ( struct net_device *netdev ) {
734 struct netfront_nic *netfront = netdev->priv;
735 struct xen_device *xendev = netfront->xendev;
736 struct netif_tx_response *response;
737 struct io_buffer *iobuf;
741 /* Consume any unconsumed responses */
742 while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->tx_fring ) ) {
744 /* Get next response */
745 response = RING_GET_RESPONSE ( &netfront->tx_fring,
746 netfront->tx_fring.rsp_cons++ );
748 /* Retrieve from descriptor ring */
749 iobuf = netfront_pull ( netfront, &netfront->tx, response->id );
750 status = response->status;
751 if ( status == NETIF_RSP_OKAY ) {
752 DBGC2 ( netfront, "NETFRONT %s TX id %d complete\n",
753 xendev->key, response->id );
754 netdev_tx_complete ( netdev, iobuf );
756 rc = -EIO_NETIF_RSP ( status );
757 DBGC2 ( netfront, "NETFRONT %s TX id %d error %d: %s\n",
758 xendev->key, response->id, status,
760 netdev_tx_complete_err ( netdev, iobuf, rc );
766 * Poll for received packets
768 * @v netdev Network device
770 static void netfront_poll_rx ( struct net_device *netdev ) {
771 struct netfront_nic *netfront = netdev->priv;
772 struct xen_device *xendev = netfront->xendev;
773 struct netif_rx_response *response;
774 struct io_buffer *iobuf;
779 /* Consume any unconsumed responses */
780 while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->rx_fring ) ) {
782 /* Get next response */
783 response = RING_GET_RESPONSE ( &netfront->rx_fring,
784 netfront->rx_fring.rsp_cons++ );
786 /* Retrieve from descriptor ring */
787 iobuf = netfront_pull ( netfront, &netfront->rx, response->id );
788 status = response->status;
791 iob_reserve ( iobuf, response->offset );
792 iob_put ( iobuf, len );
793 DBGC2 ( netfront, "NETFRONT %s RX id %d complete "
794 "%#08lx+%zx\n", xendev->key, response->id,
795 virt_to_phys ( iobuf->data ), len );
796 netdev_rx ( netdev, iobuf );
798 rc = -EIO_NETIF_RSP ( status );
799 DBGC2 ( netfront, "NETFRONT %s RX id %d error %d: %s\n",
800 xendev->key, response->id, status,
802 netdev_rx_err ( netdev, iobuf, rc );
808 * Poll for completed and received packets
810 * @v netdev Network device
812 static void netfront_poll ( struct net_device *netdev ) {
814 /* Poll for TX completions */
815 netfront_poll_tx ( netdev );
817 /* Poll for RX completions */
818 netfront_poll_rx ( netdev );
820 /* Refill RX descriptor ring */
821 netfront_refill_rx ( netdev );
824 /** Network device operations */
825 static struct net_device_operations netfront_operations = {
826 .open = netfront_open,
827 .close = netfront_close,
828 .transmit = netfront_transmit,
829 .poll = netfront_poll,
832 /******************************************************************************
834 * Xen device bus interface
836 ******************************************************************************
842 * @v xendev Xen device
843 * @ret rc Return status code
845 static int netfront_probe ( struct xen_device *xendev ) {
846 struct xen_hypervisor *xen = xendev->xen;
847 struct net_device *netdev;
848 struct netfront_nic *netfront;
851 /* Allocate and initialise structure */
852 netdev = alloc_etherdev ( sizeof ( *netfront ) );
857 netdev_init ( netdev, &netfront_operations );
858 netdev->dev = &xendev->dev;
859 netfront = netdev->priv;
860 netfront->xendev = xendev;
861 DBGC ( netfront, "NETFRONT %s backend=\"%s\" in domain %ld\n",
862 xendev->key, xendev->backend, xendev->backend_id );
864 /* Allocate grant references and initialise descriptor rings */
865 if ( ( rc = xengrant_alloc ( xen, netfront->refs,
866 NETFRONT_REF_COUNT ) ) != 0 ) {
867 DBGC ( netfront, "NETFRONT %s could not allocate grant "
868 "references: %s\n", xendev->key, strerror ( rc ) );
869 goto err_grant_alloc;
871 netfront_init_ring ( &netfront->tx, "tx-ring-ref",
872 netfront->refs[NETFRONT_REF_TX_RING],
873 NETFRONT_NUM_TX_DESC, netfront->tx_iobufs,
874 &netfront->refs[NETFRONT_REF_TX_BASE],
876 netfront_init_ring ( &netfront->rx, "rx-ring-ref",
877 netfront->refs[NETFRONT_REF_RX_RING],
878 NETFRONT_NUM_RX_DESC, netfront->rx_iobufs,
879 &netfront->refs[NETFRONT_REF_RX_BASE],
882 /* Fetch MAC address */
883 if ( ( rc = netfront_read_mac ( netfront, netdev->hw_addr ) ) != 0 )
886 /* Reset device. Ignore failures; allow the device to be
887 * registered so that reset errors can be observed by the user
888 * when attempting to open the device.
890 netfront_reset ( netfront );
892 /* Register network device */
893 if ( ( rc = register_netdev ( netdev ) ) != 0 )
894 goto err_register_netdev;
896 /* Set initial link state */
897 netdev_link_down ( netdev );
899 xen_set_drvdata ( xendev, netdev );
902 unregister_netdev ( netdev );
905 xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
907 netdev_nullify ( netdev );
908 netdev_put ( netdev );
916 * @v xendev Xen device
918 static void netfront_remove ( struct xen_device *xendev ) {
919 struct net_device *netdev = xen_get_drvdata ( xendev );
920 struct netfront_nic *netfront = netdev->priv;
921 struct xen_hypervisor *xen = xendev->xen;
923 /* Unregister network device */
924 unregister_netdev ( netdev );
927 xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
929 /* Free network device */
930 netdev_nullify ( netdev );
931 netdev_put ( netdev );
934 /** Xen netfront driver */
935 struct xen_driver netfront_driver __xen_driver = {
938 .probe = netfront_probe,
939 .remove = netfront_remove,