iPXE
netfront.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <errno.h>
00029 #include <ipxe/netdevice.h>
00030 #include <ipxe/ethernet.h>
00031 #include <ipxe/if_ether.h>
00032 #include <ipxe/malloc.h>
00033 #include <ipxe/base16.h>
00034 #include <ipxe/xen.h>
00035 #include <ipxe/xenstore.h>
00036 #include <ipxe/xenbus.h>
00037 #include <ipxe/xengrant.h>
00038 #include <ipxe/xenevent.h>
00039 #include "netfront.h"
00040 
00041 /** @file
00042  *
00043  * Xen netfront driver
00044  *
00045  */
00046 
00047 /* Disambiguate the various error causes */
00048 #define EIO_NETIF_RSP_ERROR                                             \
00049         __einfo_error ( EINFO_EIO_NETIF_RSP_ERROR )
00050 #define EINFO_EIO_NETIF_RSP_ERROR                                       \
00051         __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_ERROR,                  \
00052                           "Unspecified network error" )
00053 #define EIO_NETIF_RSP_DROPPED                                           \
00054         __einfo_error ( EINFO_EIO_NETIF_RSP_DROPPED )
00055 #define EINFO_EIO_NETIF_RSP_DROPPED                                     \
00056         __einfo_uniqify ( EINFO_EIO, -NETIF_RSP_DROPPED,                \
00057                           "Packet dropped" )
00058 #define EIO_NETIF_RSP( status )                                         \
00059         EUNIQ ( EINFO_EIO, -(status),                                   \
00060                 EIO_NETIF_RSP_ERROR, EIO_NETIF_RSP_DROPPED )
00061 
00062 /******************************************************************************
00063  *
00064  * XenStore interface
00065  *
00066  ******************************************************************************
00067  */
00068 
00069 /**
00070  * Reset device
00071  *
00072  * @v netfront          Netfront device
00073  * @ret rc              Return status code
00074  */
00075 static int netfront_reset ( struct netfront_nic *netfront ) {
00076         struct xen_device *xendev = netfront->xendev;
00077         int state;
00078         int rc;
00079 
00080         /* Get current backend state */
00081         if ( ( state = xenbus_backend_state ( xendev ) ) < 0 ) {
00082                 rc = state;
00083                 DBGC ( netfront, "NETFRONT %s could not read backend state: "
00084                        "%s\n", xendev->key, strerror ( rc ) );
00085                 return rc;
00086         }
00087 
00088         /* If the backend is not already in InitWait, then mark
00089          * frontend as Closed to shut down the backend.
00090          */
00091         if ( state != XenbusStateInitWait ) {
00092 
00093                 /* Set state to Closed */
00094                 xenbus_set_state ( xendev, XenbusStateClosed );
00095 
00096                 /* Wait for backend to reach Closed */
00097                 if ( ( rc = xenbus_backend_wait ( xendev,
00098                                                   XenbusStateClosed ) ) != 0 ) {
00099                         DBGC ( netfront, "NETFRONT %s backend did not reach "
00100                                "Closed: %s\n", xendev->key, strerror ( rc ) );
00101                         return rc;
00102                 }
00103         }
00104 
00105         /* Reset state to Initialising */
00106         xenbus_set_state ( xendev, XenbusStateInitialising );
00107 
00108         /* Wait for backend to reach InitWait */
00109         if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateInitWait ) ) != 0){
00110                 DBGC ( netfront, "NETFRONT %s backend did not reach InitWait: "
00111                        "%s\n", xendev->key, strerror ( rc ) );
00112                 return rc;
00113         }
00114 
00115         return 0;
00116 }
00117 
00118 /**
00119  * Fetch MAC address
00120  *
00121  * @v netfront          Netfront device
00122  * @v hw_addr           Hardware address to fill in
00123  * @ret rc              Return status code
00124  */
00125 static int netfront_read_mac ( struct netfront_nic *netfront, void *hw_addr ) {
00126         struct xen_device *xendev = netfront->xendev;
00127         struct xen_hypervisor *xen = xendev->xen;
00128         char *mac;
00129         int len;
00130         int rc;
00131 
00132         /* Fetch MAC address */
00133         if ( ( rc = xenstore_read ( xen, &mac, xendev->key, "mac", NULL ) )!=0){
00134                 DBGC ( netfront, "NETFRONT %s could not read MAC address: %s\n",
00135                        xendev->key, strerror ( rc ) );
00136                 goto err_xenstore_read;
00137         }
00138         DBGC2 ( netfront, "NETFRONT %s has MAC address \"%s\"\n",
00139                 xendev->key, mac );
00140 
00141         /* Decode MAC address */
00142         len = hex_decode ( ':', mac, hw_addr, ETH_ALEN );
00143         if ( len < 0 ) {
00144                 rc = len;
00145                 DBGC ( netfront, "NETFRONT %s could not decode MAC address "
00146                        "\"%s\": %s\n", xendev->key, mac, strerror ( rc ) );
00147                 goto err_decode;
00148         }
00149 
00150         /* Success */
00151         rc = 0;
00152 
00153  err_decode:
00154         free ( mac );
00155  err_xenstore_read:
00156         return rc;
00157 }
00158 
00159 /**
00160  * Write XenStore numeric value
00161  *
00162  * @v netfront          Netfront device
00163  * @v subkey            Subkey
00164  * @v num               Numeric value
00165  * @ret rc              Return status code
00166  */
00167 static int netfront_write_num ( struct netfront_nic *netfront,
00168                                 const char *subkey, unsigned long num ) {
00169         struct xen_device *xendev = netfront->xendev;
00170         struct xen_hypervisor *xen = xendev->xen;
00171         int rc;
00172 
00173         /* Write value */
00174         if ( ( rc = xenstore_write_num ( xen, num, xendev->key, subkey,
00175                                          NULL ) ) != 0 ) {
00176                 DBGC ( netfront, "NETFRONT %s could not set %s=\"%ld\": %s\n",
00177                        xendev->key, subkey, num, strerror ( rc ) );
00178                 return rc;
00179         }
00180 
00181         return 0;
00182 }
00183 
00184 /**
00185  * Write XenStore flag value
00186  *
00187  * @v netfront          Netfront device
00188  * @v subkey            Subkey
00189  * @v num               Numeric value
00190  * @ret rc              Return status code
00191  */
00192 static int netfront_write_flag ( struct netfront_nic *netfront,
00193                                  const char *subkey ) {
00194 
00195         return netfront_write_num ( netfront, subkey, 1 );
00196 }
00197 
00198 /**
00199  * Delete XenStore value
00200  *
00201  * @v netfront          Netfront device
00202  * @v subkey            Subkey
00203  * @ret rc              Return status code
00204  */
00205 static int netfront_rm ( struct netfront_nic *netfront, const char *subkey ) {
00206         struct xen_device *xendev = netfront->xendev;
00207         struct xen_hypervisor *xen = xendev->xen;
00208         int rc;
00209 
00210         /* Remove value */
00211         if ( ( rc = xenstore_rm ( xen, xendev->key, subkey, NULL ) ) != 0 ) {
00212                 DBGC ( netfront, "NETFRONT %s could not delete %s: %s\n",
00213                        xendev->key, subkey, strerror ( rc ) );
00214                 return rc;
00215         }
00216 
00217         return 0;
00218 }
00219 
00220 /******************************************************************************
00221  *
00222  * Events
00223  *
00224  ******************************************************************************
00225  */
00226 
00227 /**
00228  * Create event channel
00229  *
00230  * @v netfront          Netfront device
00231  * @ret rc              Return status code
00232  */
00233 static int netfront_create_event ( struct netfront_nic *netfront ) {
00234         struct xen_device *xendev = netfront->xendev;
00235         struct xen_hypervisor *xen = xendev->xen;
00236         struct evtchn_alloc_unbound alloc_unbound;
00237         struct evtchn_close close;
00238         int xenrc;
00239         int rc;
00240 
00241         /* Allocate event */
00242         alloc_unbound.dom = DOMID_SELF;
00243         alloc_unbound.remote_dom = xendev->backend_id;
00244         if ( ( xenrc = xenevent_alloc_unbound ( xen, &alloc_unbound ) ) != 0 ) {
00245                 rc = -EXEN ( xenrc );
00246                 DBGC ( netfront, "NETFRONT %s could not allocate event: %s\n",
00247                        xendev->key, strerror ( rc ) );
00248                 goto err_alloc_unbound;
00249         }
00250         netfront->event.port = alloc_unbound.port;
00251 
00252         /* Publish event channel */
00253         if ( ( rc = netfront_write_num ( netfront, "event-channel",
00254                                          netfront->event.port ) ) != 0 )
00255                 goto err_write_num;
00256 
00257         DBGC ( netfront, "NETFRONT %s event-channel=\"%d\"\n",
00258                xendev->key, netfront->event.port );
00259         return 0;
00260 
00261         netfront_rm ( netfront, "event-channel" );
00262  err_write_num:
00263         close.port = netfront->event.port;
00264         xenevent_close ( xen, &close );
00265  err_alloc_unbound:
00266         return rc;
00267 }
00268 
00269 /**
00270  * Send event
00271  *
00272  * @v netfront          Netfront device
00273  * @ret rc              Return status code
00274  */
00275 static inline __attribute__ (( always_inline )) int
00276 netfront_send_event ( struct netfront_nic *netfront ) {
00277         struct xen_device *xendev = netfront->xendev;
00278         struct xen_hypervisor *xen = xendev->xen;
00279         int xenrc;
00280         int rc;
00281 
00282         /* Send event */
00283         if ( ( xenrc = xenevent_send ( xen, &netfront->event ) ) != 0 ) {
00284                 rc = -EXEN ( xenrc );
00285                 DBGC ( netfront, "NETFRONT %s could not send event: %s\n",
00286                        xendev->key, strerror ( rc ) );
00287                 return rc;
00288         }
00289 
00290         return 0;
00291 }
00292 
00293 /**
00294  * Destroy event channel
00295  *
00296  * @v netfront          Netfront device
00297  */
00298 static void netfront_destroy_event ( struct netfront_nic *netfront ) {
00299         struct xen_device *xendev = netfront->xendev;
00300         struct xen_hypervisor *xen = xendev->xen;
00301         struct evtchn_close close;
00302 
00303         /* Unpublish event channel */
00304         netfront_rm ( netfront, "event-channel" );
00305 
00306         /* Close event channel */
00307         close.port = netfront->event.port;
00308         xenevent_close ( xen, &close );
00309 }
00310 
00311 /******************************************************************************
00312  *
00313  * Descriptor rings
00314  *
00315  ******************************************************************************
00316  */
00317 
00318 /**
00319  * Create descriptor ring
00320  *
00321  * @v netfront          Netfront device
00322  * @v ring              Descriptor ring
00323  * @ret rc              Return status code
00324  */
00325 static int netfront_create_ring ( struct netfront_nic *netfront,
00326                                   struct netfront_ring *ring ) {
00327         struct xen_device *xendev = netfront->xendev;
00328         struct xen_hypervisor *xen = xendev->xen;
00329         unsigned int i;
00330         int rc;
00331 
00332         /* Initialise buffer ID ring */
00333         for ( i = 0 ; i < ring->count ; i++ ) {
00334                 ring->ids[i] = i;
00335                 assert ( ring->iobufs[i] == NULL );
00336         }
00337         ring->id_prod = 0;
00338         ring->id_cons = 0;
00339 
00340         /* Allocate and initialise shared ring */
00341         ring->sring.raw = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
00342         if ( ! ring->sring.raw ) {
00343                 rc = -ENOMEM;
00344                 goto err_alloc;
00345         }
00346 
00347         /* Grant access to shared ring */
00348         if ( ( rc = xengrant_permit_access ( xen, ring->ref, xendev->backend_id,
00349                                              0, ring->sring.raw ) ) != 0 ) {
00350                 DBGC ( netfront, "NETFRONT %s could not permit access to "
00351                        "%#08lx: %s\n", xendev->key,
00352                        virt_to_phys ( ring->sring.raw ), strerror ( rc ) );
00353                 goto err_permit_access;
00354         }
00355 
00356         /* Publish shared ring reference */
00357         if ( ( rc = netfront_write_num ( netfront, ring->ref_key,
00358                                          ring->ref ) ) != 0 )
00359                 goto err_write_num;
00360 
00361         DBGC ( netfront, "NETFRONT %s %s=\"%d\" [%08lx,%08lx)\n",
00362                xendev->key, ring->ref_key, ring->ref,
00363                virt_to_phys ( ring->sring.raw ),
00364                ( virt_to_phys ( ring->sring.raw ) + PAGE_SIZE ) );
00365         return 0;
00366 
00367         netfront_rm ( netfront, ring->ref_key );
00368  err_write_num:
00369         xengrant_invalidate ( xen, ring->ref );
00370  err_permit_access:
00371         free_dma ( ring->sring.raw, PAGE_SIZE );
00372  err_alloc:
00373         return rc;
00374 }
00375 
00376 /**
00377  * Add buffer to descriptor ring
00378  *
00379  * @v netfront          Netfront device
00380  * @v ring              Descriptor ring
00381  * @v iobuf             I/O buffer
00382  * @v id                Buffer ID to fill in
00383  * @v ref               Grant reference to fill in
00384  * @ret rc              Return status code
00385  *
00386  * The caller is responsible for ensuring that there is space in the
00387  * ring.
00388  */
00389 static int netfront_push ( struct netfront_nic *netfront,
00390                            struct netfront_ring *ring, struct io_buffer *iobuf,
00391                            uint16_t *id, grant_ref_t *ref ) {
00392         struct xen_device *xendev = netfront->xendev;
00393         struct xen_hypervisor *xen = xendev->xen;
00394         unsigned int next_id;
00395         unsigned int next_ref;
00396         int rc;
00397 
00398         /* Sanity check */
00399         assert ( ! netfront_ring_is_full ( ring ) );
00400 
00401         /* Allocate buffer ID */
00402         next_id = ring->ids[ ring->id_prod & ( ring->count - 1 ) ];
00403         next_ref = ring->refs[next_id];
00404 
00405         /* Grant access to I/O buffer page.  I/O buffers are naturally
00406          * aligned, so we never need to worry about crossing a page
00407          * boundary.
00408          */
00409         if ( ( rc = xengrant_permit_access ( xen, next_ref, xendev->backend_id,
00410                                              0, iobuf->data ) ) != 0 ) {
00411                 DBGC ( netfront, "NETFRONT %s could not permit access to "
00412                        "%#08lx: %s\n", xendev->key,
00413                        virt_to_phys ( iobuf->data ), strerror ( rc ) );
00414                 return rc;
00415         }
00416 
00417         /* Store I/O buffer */
00418         assert ( ring->iobufs[next_id] == NULL );
00419         ring->iobufs[next_id] = iobuf;
00420 
00421         /* Consume buffer ID */
00422         ring->id_prod++;
00423 
00424         /* Return buffer ID and grant reference */
00425         *id = next_id;
00426         *ref = next_ref;
00427 
00428         return 0;
00429 }
00430 
00431 /**
00432  * Remove buffer from descriptor ring
00433  *
00434  * @v netfront          Netfront device
00435  * @v ring              Descriptor ring
00436  * @v id                Buffer ID
00437  * @ret iobuf           I/O buffer
00438  */
00439 static struct io_buffer * netfront_pull ( struct netfront_nic *netfront,
00440                                           struct netfront_ring *ring,
00441                                           unsigned int id ) {
00442         struct xen_device *xendev = netfront->xendev;
00443         struct xen_hypervisor *xen = xendev->xen;
00444         struct io_buffer *iobuf;
00445 
00446         /* Sanity check */
00447         assert ( id < ring->count );
00448 
00449         /* Revoke access from I/O buffer page */
00450         xengrant_invalidate ( xen, ring->refs[id] );
00451 
00452         /* Retrieve I/O buffer */
00453         iobuf = ring->iobufs[id];
00454         assert ( iobuf != NULL );
00455         ring->iobufs[id] = NULL;
00456 
00457         /* Free buffer ID */
00458         ring->ids[ ( ring->id_cons++ ) & ( ring->count - 1 ) ] = id;
00459 
00460         return iobuf;
00461 }
00462 
00463 /**
00464  * Destroy descriptor ring
00465  *
00466  * @v netfront          Netfront device
00467  * @v ring              Descriptor ring
00468  * @v discard           Method used to discard outstanding buffer, or NULL
00469  */
00470 static void netfront_destroy_ring ( struct netfront_nic *netfront,
00471                                     struct netfront_ring *ring,
00472                                     void ( * discard ) ( struct io_buffer * ) ){
00473         struct xen_device *xendev = netfront->xendev;
00474         struct xen_hypervisor *xen = xendev->xen;
00475         struct io_buffer *iobuf;
00476         unsigned int id;
00477 
00478         /* Flush any outstanding buffers */
00479         while ( ! netfront_ring_is_empty ( ring ) ) {
00480                 id = ring->ids[ ring->id_cons & ( ring->count - 1 ) ];
00481                 iobuf = netfront_pull ( netfront, ring, id );
00482                 if ( discard )
00483                         discard ( iobuf );
00484         }
00485 
00486         /* Unpublish shared ring reference */
00487         netfront_rm ( netfront, ring->ref_key );
00488 
00489         /* Revoke access from shared ring */
00490         xengrant_invalidate ( xen, ring->ref );
00491 
00492         /* Free page */
00493         free_dma ( ring->sring.raw, PAGE_SIZE );
00494         ring->sring.raw = NULL;
00495 }
00496 
00497 /******************************************************************************
00498  *
00499  * Network device interface
00500  *
00501  ******************************************************************************
00502  */
00503 
00504 /**
00505  * Refill receive descriptor ring
00506  *
00507  * @v netdev            Network device
00508  */
00509 static void netfront_refill_rx ( struct net_device *netdev ) {
00510         struct netfront_nic *netfront = netdev->priv;
00511         struct xen_device *xendev = netfront->xendev;
00512         struct io_buffer *iobuf;
00513         struct netif_rx_request *request;
00514         unsigned int refilled = 0;
00515         int notify;
00516         int rc;
00517 
00518         /* Refill ring */
00519         while ( netfront_ring_fill ( &netfront->rx ) < NETFRONT_RX_FILL ) {
00520 
00521                 /* Allocate I/O buffer */
00522                 iobuf = alloc_iob ( PAGE_SIZE );
00523                 if ( ! iobuf ) {
00524                         /* Wait for next refill */
00525                         break;
00526                 }
00527 
00528                 /* Add to descriptor ring */
00529                 request = RING_GET_REQUEST ( &netfront->rx_fring,
00530                                              netfront->rx_fring.req_prod_pvt );
00531                 if ( ( rc = netfront_push ( netfront, &netfront->rx,
00532                                             iobuf, &request->id,
00533                                             &request->gref ) ) != 0 ) {
00534                         netdev_rx_err ( netdev, iobuf, rc );
00535                         break;
00536                 }
00537                 DBGC2 ( netfront, "NETFRONT %s RX id %d ref %d is %#08lx+%zx\n",
00538                         xendev->key, request->id, request->gref,
00539                         virt_to_phys ( iobuf->data ), iob_tailroom ( iobuf ) );
00540 
00541                 /* Move to next descriptor */
00542                 netfront->rx_fring.req_prod_pvt++;
00543                 refilled++;
00544 
00545         }
00546 
00547         /* Push new descriptors and notify backend if applicable */
00548         if ( refilled ) {
00549                 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->rx_fring,
00550                                                       notify );
00551                 if ( notify )
00552                         netfront_send_event ( netfront );
00553         }
00554 }
00555 
00556 /**
00557  * Open network device
00558  *
00559  * @v netdev            Network device
00560  * @ret rc              Return status code
00561  */
00562 static int netfront_open ( struct net_device *netdev ) {
00563         struct netfront_nic *netfront = netdev->priv;
00564         struct xen_device *xendev = netfront->xendev;
00565         int rc;
00566 
00567         /* Ensure device is in a suitable initial state */
00568         if ( ( rc = netfront_reset ( netfront ) ) != 0 )
00569                 goto err_reset;
00570 
00571         /* Create transmit descriptor ring */
00572         if ( ( rc = netfront_create_ring ( netfront, &netfront->tx ) ) != 0 )
00573                 goto err_create_tx;
00574         SHARED_RING_INIT ( netfront->tx_sring );
00575         FRONT_RING_INIT ( &netfront->tx_fring, netfront->tx_sring, PAGE_SIZE );
00576         assert ( RING_SIZE ( &netfront->tx_fring ) >= netfront->tx.count );
00577 
00578         /* Create receive descriptor ring */
00579         if ( ( rc = netfront_create_ring ( netfront, &netfront->rx ) ) != 0 )
00580                 goto err_create_rx;
00581         SHARED_RING_INIT ( netfront->rx_sring );
00582         FRONT_RING_INIT ( &netfront->rx_fring, netfront->rx_sring, PAGE_SIZE );
00583         assert ( RING_SIZE ( &netfront->rx_fring ) >= netfront->rx.count );
00584 
00585         /* Create event channel */
00586         if ( ( rc = netfront_create_event ( netfront ) ) != 0 )
00587                 goto err_create_event;
00588 
00589         /* "Request" the rx-copy feature.  Current versions of
00590          * xen_netback.ko will fail silently if this parameter is not
00591          * present.
00592          */
00593         if ( ( rc = netfront_write_flag ( netfront, "request-rx-copy" ) ) != 0 )
00594                 goto err_request_rx_copy;
00595 
00596         /* Disable checksum offload, since we will always do the work anyway */
00597         if ( ( rc = netfront_write_flag ( netfront,
00598                                           "feature-no-csum-offload" ) ) != 0 )
00599                 goto err_feature_no_csum_offload;
00600 
00601         /* Inform backend that we will send notifications for RX requests */
00602         if ( ( rc = netfront_write_flag ( netfront,
00603                                           "feature-rx-notify" ) ) != 0 )
00604                 goto err_feature_rx_notify;
00605 
00606         /* Set state to Connected */
00607         if ( ( rc = xenbus_set_state ( xendev, XenbusStateConnected ) ) != 0 ) {
00608                 DBGC ( netfront, "NETFRONT %s could not set state=\"%d\": %s\n",
00609                        xendev->key, XenbusStateConnected, strerror ( rc ) );
00610                 goto err_set_state;
00611         }
00612 
00613         /* Wait for backend to connect */
00614         if ( ( rc = xenbus_backend_wait ( xendev, XenbusStateConnected ) ) !=0){
00615                 DBGC ( netfront, "NETFRONT %s could not connect to backend: "
00616                        "%s\n", xendev->key, strerror ( rc ) );
00617                 goto err_backend_wait;
00618         }
00619 
00620         /* Refill receive descriptor ring */
00621         netfront_refill_rx ( netdev );
00622 
00623         /* Set link up */
00624         netdev_link_up ( netdev );
00625 
00626         return 0;
00627 
00628  err_backend_wait:
00629         netfront_reset ( netfront );
00630  err_set_state:
00631         netfront_rm ( netfront, "feature-rx-notify" );
00632  err_feature_rx_notify:
00633         netfront_rm ( netfront, "feature-no-csum-offload" );
00634  err_feature_no_csum_offload:
00635         netfront_rm ( netfront, "request-rx-copy" );
00636  err_request_rx_copy:
00637         netfront_destroy_event ( netfront );
00638  err_create_event:
00639         netfront_destroy_ring ( netfront, &netfront->rx, NULL );
00640  err_create_rx:
00641         netfront_destroy_ring ( netfront, &netfront->tx, NULL );
00642  err_create_tx:
00643  err_reset:
00644         return rc;
00645 }
00646 
00647 /**
00648  * Close network device
00649  *
00650  * @v netdev            Network device
00651  */
00652 static void netfront_close ( struct net_device *netdev ) {
00653         struct netfront_nic *netfront = netdev->priv;
00654         struct xen_device *xendev = netfront->xendev;
00655         int rc;
00656 
00657         /* Reset devic, thereby ensuring that grant references are no
00658          * longer in use, etc.
00659          */
00660         if ( ( rc = netfront_reset ( netfront ) ) != 0 ) {
00661                 DBGC ( netfront, "NETFRONT %s could not disconnect from "
00662                        "backend: %s\n", xendev->key, strerror ( rc ) );
00663                 /* Things will probably go _very_ badly wrong if this
00664                  * happens, since it means the backend may still write
00665                  * to the outstanding RX buffers that we are about to
00666                  * free.  The best we can do is report the error via
00667                  * the link status, but there's a good chance the
00668                  * machine will crash soon.
00669                  */
00670                 netdev_link_err ( netdev, rc );
00671         } else {
00672                 netdev_link_down ( netdev );
00673         }
00674 
00675         /* Delete flags */
00676         netfront_rm ( netfront, "feature-rx-notify" );
00677         netfront_rm ( netfront, "feature-no-csum-offload" );
00678         netfront_rm ( netfront, "request-rx-copy" );
00679 
00680         /* Destroy event channel */
00681         netfront_destroy_event ( netfront );
00682 
00683         /* Destroy receive descriptor ring, freeing any outstanding
00684          * I/O buffers.
00685          */
00686         netfront_destroy_ring ( netfront, &netfront->rx, free_iob );
00687 
00688         /* Destroy transmit descriptor ring.  Leave any outstanding
00689          * I/O buffers to be freed by netdev_tx_flush().
00690          */
00691         netfront_destroy_ring ( netfront, &netfront->tx, NULL );
00692 }
00693 
00694 /**
00695  * Transmit packet
00696  *
00697  * @v netdev            Network device
00698  * @v iobuf             I/O buffer
00699  * @ret rc              Return status code
00700  */
00701 static int netfront_transmit ( struct net_device *netdev,
00702                                struct io_buffer *iobuf ) {
00703         struct netfront_nic *netfront = netdev->priv;
00704         struct xen_device *xendev = netfront->xendev;
00705         struct netif_tx_request *request;
00706         int notify;
00707         int rc;
00708 
00709         /* Check that we have space in the ring */
00710         if ( netfront_ring_is_full ( &netfront->tx ) ) {
00711                 DBGC ( netfront, "NETFRONT %s out of transmit descriptors\n",
00712                        xendev->key );
00713                 return -ENOBUFS;
00714         }
00715 
00716         /* Add to descriptor ring */
00717         request = RING_GET_REQUEST ( &netfront->tx_fring,
00718                                      netfront->tx_fring.req_prod_pvt );
00719         if ( ( rc = netfront_push ( netfront, &netfront->tx, iobuf,
00720                                     &request->id, &request->gref ) ) != 0 ) {
00721                 return rc;
00722         }
00723         request->offset = ( virt_to_phys ( iobuf->data ) & ( PAGE_SIZE - 1 ) );
00724         request->flags = NETTXF_data_validated;
00725         request->size = iob_len ( iobuf );
00726         DBGC2 ( netfront, "NETFRONT %s TX id %d ref %d is %#08lx+%zx\n",
00727                 xendev->key, request->id, request->gref,
00728                 virt_to_phys ( iobuf->data ), iob_len ( iobuf ) );
00729 
00730         /* Consume descriptor */
00731         netfront->tx_fring.req_prod_pvt++;
00732 
00733         /* Push new descriptor and notify backend if applicable */
00734         RING_PUSH_REQUESTS_AND_CHECK_NOTIFY ( &netfront->tx_fring, notify );
00735         if ( notify )
00736                 netfront_send_event ( netfront );
00737 
00738         return 0;
00739 }
00740 
00741 /**
00742  * Poll for completed packets
00743  *
00744  * @v netdev            Network device
00745  */
00746 static void netfront_poll_tx ( struct net_device *netdev ) {
00747         struct netfront_nic *netfront = netdev->priv;
00748         struct xen_device *xendev = netfront->xendev;
00749         struct netif_tx_response *response;
00750         struct io_buffer *iobuf;
00751         unsigned int status;
00752         int rc;
00753 
00754         /* Consume any unconsumed responses */
00755         while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->tx_fring ) ) {
00756 
00757                 /* Get next response */
00758                 response = RING_GET_RESPONSE ( &netfront->tx_fring,
00759                                                netfront->tx_fring.rsp_cons++ );
00760 
00761                 /* Retrieve from descriptor ring */
00762                 iobuf = netfront_pull ( netfront, &netfront->tx, response->id );
00763                 status = response->status;
00764                 if ( status == NETIF_RSP_OKAY ) {
00765                         DBGC2 ( netfront, "NETFRONT %s TX id %d complete\n",
00766                                 xendev->key, response->id );
00767                         netdev_tx_complete ( netdev, iobuf );
00768                 } else {
00769                         rc = -EIO_NETIF_RSP ( status );
00770                         DBGC2 ( netfront, "NETFRONT %s TX id %d error %d: %s\n",
00771                                 xendev->key, response->id, status,
00772                                 strerror ( rc ) );
00773                         netdev_tx_complete_err ( netdev, iobuf, rc );
00774                 }
00775         }
00776 }
00777 
00778 /**
00779  * Poll for received packets
00780  *
00781  * @v netdev            Network device
00782  */
00783 static void netfront_poll_rx ( struct net_device *netdev ) {
00784         struct netfront_nic *netfront = netdev->priv;
00785         struct xen_device *xendev = netfront->xendev;
00786         struct netif_rx_response *response;
00787         struct io_buffer *iobuf;
00788         int status;
00789         size_t len;
00790         int rc;
00791 
00792         /* Consume any unconsumed responses */
00793         while ( RING_HAS_UNCONSUMED_RESPONSES ( &netfront->rx_fring ) ) {
00794 
00795                 /* Get next response */
00796                 response = RING_GET_RESPONSE ( &netfront->rx_fring,
00797                                                netfront->rx_fring.rsp_cons++ );
00798 
00799                 /* Retrieve from descriptor ring */
00800                 iobuf = netfront_pull ( netfront, &netfront->rx, response->id );
00801                 status = response->status;
00802                 if ( status >= 0 ) {
00803                         len = status;
00804                         iob_reserve ( iobuf, response->offset );
00805                         iob_put ( iobuf, len );
00806                         DBGC2 ( netfront, "NETFRONT %s RX id %d complete "
00807                                 "%#08lx+%zx\n", xendev->key, response->id,
00808                                 virt_to_phys ( iobuf->data ), len );
00809                         netdev_rx ( netdev, iobuf );
00810                 } else {
00811                         rc = -EIO_NETIF_RSP ( status );
00812                         DBGC2 ( netfront, "NETFRONT %s RX id %d error %d: %s\n",
00813                                 xendev->key, response->id, status,
00814                                 strerror ( rc ) );
00815                         netdev_rx_err ( netdev, iobuf, rc );
00816                 }
00817         }
00818 }
00819 
00820 /**
00821  * Poll for completed and received packets
00822  *
00823  * @v netdev            Network device
00824  */
00825 static void netfront_poll ( struct net_device *netdev ) {
00826 
00827         /* Poll for TX completions */
00828         netfront_poll_tx ( netdev );
00829 
00830         /* Poll for RX completions */
00831         netfront_poll_rx ( netdev );
00832 
00833         /* Refill RX descriptor ring */
00834         netfront_refill_rx ( netdev );
00835 }
00836 
00837 /** Network device operations */
00838 static struct net_device_operations netfront_operations = {
00839         .open           = netfront_open,
00840         .close          = netfront_close,
00841         .transmit       = netfront_transmit,
00842         .poll           = netfront_poll,
00843 };
00844 
00845 /******************************************************************************
00846  *
00847  * Xen device bus interface
00848  *
00849  ******************************************************************************
00850  */
00851 
00852 /**
00853  * Probe Xen device
00854  *
00855  * @v xendev            Xen device
00856  * @ret rc              Return status code
00857  */
00858 static int netfront_probe ( struct xen_device *xendev ) {
00859         struct xen_hypervisor *xen = xendev->xen;
00860         struct net_device *netdev;
00861         struct netfront_nic *netfront;
00862         int rc;
00863 
00864         /* Allocate and initialise structure */
00865         netdev = alloc_etherdev ( sizeof ( *netfront ) );
00866         if ( ! netdev ) {
00867                 rc = -ENOMEM;
00868                 goto err_alloc;
00869         }
00870         netdev_init ( netdev, &netfront_operations );
00871         netdev->dev = &xendev->dev;
00872         netfront = netdev->priv;
00873         netfront->xendev = xendev;
00874         DBGC ( netfront, "NETFRONT %s backend=\"%s\" in domain %ld\n",
00875                xendev->key, xendev->backend, xendev->backend_id );
00876 
00877         /* Allocate grant references and initialise descriptor rings */
00878         if ( ( rc = xengrant_alloc ( xen, netfront->refs,
00879                                      NETFRONT_REF_COUNT ) ) != 0 ) {
00880                 DBGC ( netfront, "NETFRONT %s could not allocate grant "
00881                        "references: %s\n", xendev->key, strerror ( rc ) );
00882                 goto err_grant_alloc;
00883         }
00884         netfront_init_ring ( &netfront->tx, "tx-ring-ref",
00885                              netfront->refs[NETFRONT_REF_TX_RING],
00886                              NETFRONT_NUM_TX_DESC, netfront->tx_iobufs,
00887                              &netfront->refs[NETFRONT_REF_TX_BASE],
00888                              netfront->tx_ids );
00889         netfront_init_ring ( &netfront->rx, "rx-ring-ref",
00890                              netfront->refs[NETFRONT_REF_RX_RING],
00891                              NETFRONT_NUM_RX_DESC, netfront->rx_iobufs,
00892                              &netfront->refs[NETFRONT_REF_RX_BASE],
00893                              netfront->rx_ids );
00894 
00895         /* Fetch MAC address */
00896         if ( ( rc = netfront_read_mac ( netfront, netdev->hw_addr ) ) != 0 )
00897                 goto err_read_mac;
00898 
00899         /* Reset device.  Ignore failures; allow the device to be
00900          * registered so that reset errors can be observed by the user
00901          * when attempting to open the device.
00902          */
00903         netfront_reset ( netfront );
00904 
00905         /* Register network device */
00906         if ( ( rc = register_netdev ( netdev ) ) != 0 )
00907                 goto err_register_netdev;
00908 
00909         /* Set initial link state */
00910         netdev_link_down ( netdev );
00911 
00912         xen_set_drvdata ( xendev, netdev );
00913         return 0;
00914 
00915         unregister_netdev ( netdev );
00916  err_register_netdev:
00917  err_read_mac:
00918         xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
00919  err_grant_alloc:
00920         netdev_nullify ( netdev );
00921         netdev_put ( netdev );
00922  err_alloc:
00923         return rc;
00924 }
00925 
00926 /**
00927  * Remove Xen device
00928  *
00929  * @v xendev            Xen device
00930  */
00931 static void netfront_remove ( struct xen_device *xendev ) {
00932         struct net_device *netdev = xen_get_drvdata ( xendev );
00933         struct netfront_nic *netfront = netdev->priv;
00934         struct xen_hypervisor *xen = xendev->xen;
00935 
00936         /* Unregister network device */
00937         unregister_netdev ( netdev );
00938 
00939         /* Free resources */
00940         xengrant_free ( xen, netfront->refs, NETFRONT_REF_COUNT );
00941 
00942         /* Free network device */
00943         netdev_nullify ( netdev );
00944         netdev_put ( netdev );
00945 }
00946 
00947 /** Xen netfront driver */
00948 struct xen_driver netfront_driver __xen_driver = {
00949         .name = "netfront",
00950         .type = "vif",
00951         .probe = netfront_probe,
00952         .remove = netfront_remove,
00953 };