iPXE
igbvf_main.c
Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003   Intel(R) 82576 Virtual Function Linux driver
00004   Copyright(c) 2009 Intel Corporation.
00005 
00006   Copyright(c) 2010 Eric Keller <ekeller@princeton.edu>
00007   Copyright(c) 2010 Red Hat Inc.
00008         Alex Williamson <alex.williamson@redhat.com>
00009 
00010   This program is free software; you can redistribute it and/or modify it
00011   under the terms and conditions of the GNU General Public License,
00012   version 2, as published by the Free Software Foundation.
00013 
00014   This program is distributed in the hope it will be useful, but WITHOUT
00015   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00017   more details.
00018 
00019   You should have received a copy of the GNU General Public License along with
00020   this program; if not, write to the Free Software Foundation, Inc.,
00021   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
00022 
00023   The full GNU General Public License is included in this distribution in
00024   the file called "COPYING".
00025 
00026   Contact Information:
00027   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
00028   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
00029 
00030 *******************************************************************************/
00031 
00032 FILE_LICENCE ( GPL2_ONLY );
00033 
00034 #include "igbvf.h"
00035 
00036 /**
00037  * igbvf_setup_tx_resources - allocate Tx resources (Descriptors)
00038  *
00039  * @v adapter   e1000 private structure
00040  *
00041  * @ret rc       Returns 0 on success, negative on failure
00042  **/
00043 int igbvf_setup_tx_resources ( struct igbvf_adapter *adapter )
00044 {
00045         DBG ( "igbvf_setup_tx_resources\n" );
00046 
00047         /* Allocate transmit descriptor ring memory.
00048            It must not cross a 64K boundary because of hardware errata #23
00049            so we use malloc_dma() requesting a 128 byte block that is
00050            128 byte aligned. This should guarantee that the memory
00051            allocated will not cross a 64K boundary, because 128 is an
00052            even multiple of 65536 ( 65536 / 128 == 512 ), so all possible
00053            allocations of 128 bytes on a 128 byte boundary will not
00054            cross 64K bytes.
00055          */
00056 
00057         adapter->tx_base =
00058                 malloc_dma ( adapter->tx_ring_size, adapter->tx_ring_size );
00059 
00060         if ( ! adapter->tx_base ) {
00061                 return -ENOMEM;
00062         }
00063 
00064         memset ( adapter->tx_base, 0, adapter->tx_ring_size );
00065 
00066         DBG ( "adapter->tx_base = %#08lx\n", virt_to_bus ( adapter->tx_base ) );
00067 
00068         return 0;
00069 }
00070 
00071 /**
00072  * igbvf_free_tx_resources - Free Tx Resources per Queue
00073  * @adapter: board private structure
00074  *
00075  * Free all transmit software resources
00076  **/
00077 void igbvf_free_tx_resources ( struct igbvf_adapter *adapter )
00078 {
00079         DBG ( "igbvf_free_tx_resources\n" );
00080 
00081         free_dma ( adapter->tx_base, adapter->tx_ring_size );
00082 }
00083 
00084 /**
00085  * igbvf_free_rx_resources - Free Rx Resources
00086  * @adapter: board private structure
00087  *
00088  * Free all receive software resources
00089  **/
00090 void igbvf_free_rx_resources ( struct igbvf_adapter *adapter )
00091 {
00092         int i;
00093 
00094         DBG ( "igbvf_free_rx_resources\n" );
00095 
00096         free_dma ( adapter->rx_base, adapter->rx_ring_size );
00097 
00098         for ( i = 0; i < NUM_RX_DESC; i++ ) {
00099                 free_iob ( adapter->rx_iobuf[i] );
00100         }
00101 }
00102 
00103 /**
00104  * igbvf_refill_rx_ring - allocate Rx io_buffers
00105  *
00106  * @v adapter   e1000 private structure
00107  *
00108  * @ret rc       Returns 0 on success, negative on failure
00109  **/
00110 static int igbvf_refill_rx_ring ( struct igbvf_adapter *adapter )
00111 {
00112         int i, rx_curr;
00113         int rc = 0;
00114         union e1000_adv_rx_desc *rx_curr_desc;
00115         struct e1000_hw *hw = &adapter->hw;
00116         struct io_buffer *iob;
00117 
00118         DBGP ("igbvf_refill_rx_ring\n");
00119 
00120         for ( i = 0; i < NUM_RX_DESC; i++ ) {
00121                 rx_curr = ( ( adapter->rx_curr + i ) % NUM_RX_DESC );
00122                 rx_curr_desc = adapter->rx_base + rx_curr;
00123 
00124                 if ( rx_curr_desc->wb.upper.status_error & E1000_RXD_STAT_DD )
00125                         continue;
00126 
00127                 if ( adapter->rx_iobuf[rx_curr] != NULL )
00128                         continue;
00129 
00130                 DBG2 ( "Refilling rx desc %d\n", rx_curr );
00131 
00132                 iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
00133                 adapter->rx_iobuf[rx_curr] = iob;
00134 
00135                 rx_curr_desc->wb.upper.status_error = 0;
00136 
00137                 if ( ! iob ) {
00138                         DBG ( "alloc_iob failed\n" );
00139                         rc = -ENOMEM;
00140                         break;
00141                 } else {
00142                         rx_curr_desc->read.pkt_addr = virt_to_bus ( iob->data );
00143                         rx_curr_desc->read.hdr_addr = 0;
00144                         ew32 ( RDT(0), rx_curr );
00145                 }
00146         }
00147         return rc;
00148 }
00149 
00150 /**
00151  * igbvf_irq_disable - Mask off interrupt generation on the NIC
00152  * @adapter: board private structure
00153  **/
00154 static void igbvf_irq_disable ( struct igbvf_adapter *adapter )
00155 {
00156         struct e1000_hw *hw = &adapter->hw;
00157 
00158         ew32 ( EIMC, ~0 );
00159 }
00160 
00161 /**
00162  * igbvf_irq_enable - Enable default interrupt generation settings
00163  * @adapter: board private structure
00164  **/
00165 static void igbvf_irq_enable ( struct igbvf_adapter *adapter )
00166 {
00167         struct e1000_hw *hw = &adapter->hw;
00168 
00169         ew32 ( EIAC, IMS_ENABLE_MASK );
00170         ew32 ( EIAM, IMS_ENABLE_MASK );
00171         ew32 ( EIMS, IMS_ENABLE_MASK );
00172 }
00173 
00174 /**
00175  * igbvf_irq - enable or Disable interrupts
00176  *
00177  * @v adapter   e1000 adapter
00178  * @v action    requested interrupt action
00179  **/
00180 static void igbvf_irq ( struct net_device *netdev, int enable )
00181 {
00182         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00183 
00184         DBG ( "igbvf_irq\n" );
00185 
00186         if ( enable ) {
00187                 igbvf_irq_enable ( adapter );
00188         } else {
00189                 igbvf_irq_disable ( adapter );
00190         }
00191 }
00192 
00193 /**
00194  * igbvf_process_tx_packets - process transmitted packets
00195  *
00196  * @v netdev    network interface device structure
00197  **/
00198 static void igbvf_process_tx_packets ( struct net_device *netdev )
00199 {
00200         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00201         uint32_t i;
00202         uint32_t tx_status;
00203         union e1000_adv_tx_desc *tx_curr_desc;
00204 
00205         /* Check status of transmitted packets
00206          */
00207         DBGP ( "process_tx_packets: tx_head = %d, tx_tail = %d\n", adapter->tx_head,
00208               adapter->tx_tail );
00209 
00210         while ( ( i = adapter->tx_head ) != adapter->tx_tail ) {
00211 
00212                 tx_curr_desc = ( void * )  ( adapter->tx_base ) +
00213                                            ( i * sizeof ( *adapter->tx_base ) );
00214 
00215                 tx_status = tx_curr_desc->wb.status;
00216                 DBG ( "  tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
00217                 DBG ( "  tx_status = %#08x\n", tx_status );
00218 
00219                 /* if the packet at tx_head is not owned by hardware it is for us */
00220                 if ( ! ( tx_status & E1000_TXD_STAT_DD ) )
00221                         break;
00222 
00223                 DBG ( "Sent packet. tx_head: %d tx_tail: %d tx_status: %#08x\n",
00224                       adapter->tx_head, adapter->tx_tail, tx_status );
00225 
00226                 netdev_tx_complete ( netdev, adapter->tx_iobuf[i] );
00227                 DBG ( "Success transmitting packet, tx_status: %#08x\n",
00228                       tx_status );
00229 
00230                 /* Decrement count of used descriptors, clear this descriptor
00231                  */
00232                 adapter->tx_fill_ctr--;
00233                 memset ( tx_curr_desc, 0, sizeof ( *tx_curr_desc ) );
00234 
00235                 adapter->tx_head = ( adapter->tx_head + 1 ) % NUM_TX_DESC;
00236         }
00237 }
00238 
00239 /**
00240  * igbvf_process_rx_packets - process received packets
00241  *
00242  * @v netdev    network interface device structure
00243  **/
00244 static void igbvf_process_rx_packets ( struct net_device *netdev )
00245 {
00246         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00247         struct e1000_hw *hw = &adapter->hw;
00248         uint32_t i;
00249         uint32_t rx_status;
00250         uint32_t rx_len;
00251         uint32_t rx_err;
00252         union e1000_adv_rx_desc *rx_curr_desc;
00253 
00254         DBGP ( "igbvf_process_rx_packets\n" );
00255 
00256         /* Process received packets
00257          */
00258         while ( 1 ) {
00259                 i = adapter->rx_curr;
00260 
00261                 rx_curr_desc = ( void * )  ( adapter->rx_base ) +
00262                                   ( i * sizeof ( *adapter->rx_base ) );
00263                 rx_status = rx_curr_desc->wb.upper.status_error;
00264 
00265                 DBG2 ( "Before DD Check RX_status: %#08x, rx_curr: %d\n",
00266                        rx_status, i );
00267 
00268                 if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
00269                         break;
00270 
00271                 if ( adapter->rx_iobuf[i] == NULL )
00272                         break;
00273 
00274                 DBG ( "E1000_RCTL = %#08x\n", er32 (RCTL) );
00275 
00276                 rx_len = rx_curr_desc->wb.upper.length;
00277 
00278                 DBG ( "Received packet, rx_curr: %d  rx_status: %#08x  rx_len: %d\n",
00279                       i, rx_status, rx_len );
00280 
00281                 rx_err = rx_status;
00282 
00283                 iob_put ( adapter->rx_iobuf[i], rx_len );
00284 
00285                 if ( rx_err & E1000_RXDEXT_ERR_FRAME_ERR_MASK ) {
00286 
00287                         netdev_rx_err ( netdev, adapter->rx_iobuf[i], -EINVAL );
00288                         DBG ( "igbvf_process_rx_packets: Corrupted packet received!"
00289                               " rx_err: %#08x\n", rx_err );
00290                 } else  {
00291                         /* Add this packet to the receive queue. */
00292                         netdev_rx ( netdev, adapter->rx_iobuf[i] );
00293                 }
00294                 adapter->rx_iobuf[i] = NULL;
00295 
00296                 memset ( rx_curr_desc, 0, sizeof ( *rx_curr_desc ) );
00297 
00298                 adapter->rx_curr = ( adapter->rx_curr + 1 ) % NUM_RX_DESC;
00299         }
00300 }
00301 
00302 /**
00303  * igbvf_poll - Poll for received packets
00304  *
00305  * @v netdev    Network device
00306  */
00307 static void igbvf_poll ( struct net_device *netdev )
00308 {
00309         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00310         uint32_t rx_status;
00311         union e1000_adv_rx_desc *rx_curr_desc;
00312 
00313         DBGP ( "igbvf_poll\n" );
00314 
00315         rx_curr_desc = ( void * )  ( adapter->rx_base ) +
00316                         ( adapter->rx_curr * sizeof ( *adapter->rx_base ) );
00317         rx_status = rx_curr_desc->wb.upper.status_error;
00318 
00319         if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
00320                 return;
00321 
00322         igbvf_process_tx_packets ( netdev );
00323 
00324         igbvf_process_rx_packets ( netdev );
00325 
00326         igbvf_refill_rx_ring ( adapter );
00327 }
00328 
00329 /**
00330  *  igbvf_config_collision_dist_generic - Configure collision distance
00331  *  @hw: pointer to the HW structure
00332  *
00333  *  Configures the collision distance to the default value and is used
00334  *  during link setup. Currently no func pointer exists and all
00335  *  implementations are handled in the generic version of this function.
00336  **/
00337 void igbvf_config_collision_dist ( struct e1000_hw *hw )
00338 {
00339         u32 tctl;
00340 
00341         DBG ("igbvf_config_collision_dist");
00342 
00343         tctl = er32 (TCTL);
00344 
00345         tctl &= ~E1000_TCTL_COLD;
00346         tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
00347 
00348         ew32 (TCTL, tctl);
00349         e1e_flush();
00350 }
00351 
00352 /**
00353  * igbvf_configure_tx - Configure Transmit Unit after Reset
00354  * @adapter: board private structure
00355  *
00356  * Configure the Tx unit of the MAC after a reset.
00357  **/
00358 static void igbvf_configure_tx ( struct igbvf_adapter *adapter )
00359 {
00360         struct e1000_hw *hw = &adapter->hw;
00361         u32 tctl, txdctl;
00362 
00363         DBG ( "igbvf_configure_tx\n" );
00364 
00365         /* disable transmits while setting up the descriptors */
00366         tctl = er32 ( TCTL );
00367         ew32 ( TCTL, tctl & ~E1000_TCTL_EN );
00368         e1e_flush();
00369         mdelay (10);
00370 
00371         ew32 ( TDBAH(0), 0 );
00372         ew32 ( TDBAL(0), virt_to_bus ( adapter->tx_base ) );
00373         ew32 ( TDLEN(0), adapter->tx_ring_size );
00374 
00375         DBG ( "E1000_TDBAL(0): %#08x\n",  er32 ( TDBAL(0) ) );
00376         DBG ( "E1000_TDLEN(0): %d\n",     er32 ( TDLEN(0) ) );
00377 
00378         /* Setup the HW Tx Head and Tail descriptor pointers */
00379         ew32 ( TDH(0), 0 );
00380         ew32 ( TDT(0), 0 );
00381 
00382         adapter->tx_head = 0;
00383         adapter->tx_tail = 0;
00384         adapter->tx_fill_ctr = 0;
00385 
00386         txdctl = er32(TXDCTL(0));
00387         txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
00388         ew32 ( TXDCTL(0), txdctl );
00389 
00390         txdctl = er32 ( TXDCTL(0) );
00391         txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
00392         ew32 ( TXDCTL(0), txdctl );
00393 
00394         /* Setup Transmit Descriptor Settings for eop descriptor */
00395         adapter->txd_cmd  = E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_IFCS;
00396 
00397         /* Advanced descriptor */
00398         adapter->txd_cmd |= E1000_ADVTXD_DCMD_DEXT;
00399 
00400         /* (not part of cmd, but in same 32 bit word...) */
00401         adapter->txd_cmd |= E1000_ADVTXD_DTYP_DATA;
00402 
00403         /* enable Report Status bit */
00404         adapter->txd_cmd |= E1000_ADVTXD_DCMD_RS;
00405 
00406         /* Program the Transmit Control Register */
00407         tctl &= ~E1000_TCTL_CT;
00408         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
00409                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
00410 
00411         igbvf_config_collision_dist ( hw );
00412 
00413         /* Enable transmits */
00414         tctl |= E1000_TCTL_EN;
00415         ew32(TCTL, tctl);
00416         e1e_flush();
00417 }
00418 
00419 /* igbvf_reset - bring the hardware into a known good state
00420  *
00421  * This function boots the hardware and enables some settings that
00422  * require a configuration cycle of the hardware - those cannot be
00423  * set/changed during runtime. After reset the device needs to be
00424  * properly configured for Rx, Tx etc.
00425  */
00426 void igbvf_reset ( struct igbvf_adapter *adapter )
00427 {
00428         struct e1000_mac_info *mac = &adapter->hw.mac;
00429         struct net_device *netdev = adapter->netdev;
00430         struct e1000_hw *hw = &adapter->hw;
00431 
00432         /* Allow time for pending master requests to run */
00433         if ( mac->ops.reset_hw(hw) )
00434                 DBG ("PF still resetting\n");
00435 
00436         mac->ops.init_hw ( hw );
00437 
00438         if ( is_valid_ether_addr(adapter->hw.mac.addr) ) {
00439                 memcpy ( netdev->hw_addr, adapter->hw.mac.addr, ETH_ALEN );
00440         }
00441 }
00442 
00443 extern void igbvf_init_function_pointers_vf(struct e1000_hw *hw);
00444 
00445 /**
00446  * igbvf_sw_init - Initialize general software structures (struct igbvf_adapter)
00447  * @adapter: board private structure to initialize
00448  *
00449  * igbvf_sw_init initializes the Adapter private data structure.
00450  * Fields are initialized based on PCI device information and
00451  * OS network device settings (MTU size).
00452  **/
00453 static int __devinit igbvf_sw_init ( struct igbvf_adapter *adapter )
00454 {
00455         struct e1000_hw *hw = &adapter->hw;
00456         struct pci_device *pdev = adapter->pdev;
00457         int rc;
00458 
00459         /* PCI config space info */
00460 
00461         hw->vendor_id = pdev->vendor;
00462         hw->device_id = pdev->device;
00463 
00464         pci_read_config_byte ( pdev, PCI_REVISION, &hw->revision_id );
00465 
00466         pci_read_config_word ( pdev, PCI_COMMAND, &hw->bus.pci_cmd_word );
00467 
00468         adapter->max_frame_size = MAXIMUM_ETHERNET_VLAN_SIZE + ETH_HLEN + ETH_FCS_LEN;
00469         adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
00470 
00471         /* Set various function pointers */
00472         igbvf_init_function_pointers_vf ( &adapter->hw );
00473 
00474         rc = adapter->hw.mac.ops.init_params ( &adapter->hw );
00475         if (rc) {
00476                 DBG ("hw.mac.ops.init_params(&adapter->hw) Failure\n");
00477                 return rc;
00478         }
00479 
00480         rc = adapter->hw.mbx.ops.init_params ( &adapter->hw );
00481         if (rc) {
00482                 DBG ("hw.mbx.ops.init_params(&adapter->hw) Failure\n");
00483                 return rc;
00484         }
00485 
00486         /* Explicitly disable IRQ since the NIC can be in any state. */
00487         igbvf_irq_disable ( adapter );
00488 
00489         return 0;
00490 }
00491 
00492 /**
00493  * igbvf_setup_srrctl - configure the receive control registers
00494  * @adapter: Board private structure
00495  **/
00496 static void igbvf_setup_srrctl ( struct igbvf_adapter *adapter )
00497 {
00498         struct e1000_hw *hw = &adapter->hw;
00499         u32 srrctl = 0;
00500 
00501         DBG ( "igbvf_setup_srrctl\n" );
00502 
00503         srrctl &= ~(E1000_SRRCTL_DESCTYPE_MASK |
00504                     E1000_SRRCTL_BSIZEHDR_MASK |
00505                     E1000_SRRCTL_BSIZEPKT_MASK);
00506 
00507         /* Enable queue drop to avoid head of line blocking */
00508         srrctl |= E1000_SRRCTL_DROP_EN;
00509 
00510         /* Setup buffer sizes */
00511         srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
00512         srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
00513 
00514         ew32 ( SRRCTL(0), srrctl );
00515 }
00516 
00517 /**
00518  * igbvf_configure_rx - Configure 8254x Receive Unit after Reset
00519  * @adapter: board private structure
00520  *
00521  * Configure the Rx unit of the MAC after a reset.
00522  **/
00523 static void igbvf_configure_rx ( struct igbvf_adapter *adapter )
00524 {
00525         struct e1000_hw *hw = &adapter->hw;
00526         u32 rxdctl;
00527 
00528         DBG ( "igbvf_configure_rx\n" );
00529 
00530         /* disable receives */
00531         rxdctl = er32 ( RXDCTL(0) );
00532         ew32 ( RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE );
00533         msleep ( 10 );
00534 
00535         /*
00536          * Setup the HW Rx Head and Tail Descriptor Pointers and
00537          * the Base and Length of the Rx Descriptor Ring
00538          */
00539         ew32 ( RDBAL(0), virt_to_bus (adapter->rx_base) );
00540         ew32 ( RDBAH(0), 0 );
00541         ew32 ( RDLEN(0), adapter->rx_ring_size );
00542         adapter->rx_curr = 0;
00543         ew32 ( RDH(0), 0 );
00544         ew32 ( RDT(0), 0 );
00545 
00546         rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
00547         rxdctl &= 0xFFF00000;
00548         rxdctl |= IGBVF_RX_PTHRESH;
00549         rxdctl |= IGBVF_RX_HTHRESH << 8;
00550         rxdctl |= IGBVF_RX_WTHRESH << 16;
00551 
00552         igbvf_rlpml_set_vf ( hw, adapter->max_frame_size );
00553 
00554         /* enable receives */
00555         ew32 ( RXDCTL(0), rxdctl );
00556         ew32 ( RDT(0), NUM_RX_DESC );
00557 }
00558 
00559 /**
00560  * igbvf_setup_rx_resources - allocate Rx resources (Descriptors)
00561  *
00562  * @v adapter   e1000 private structure
00563  **/
00564 int igbvf_setup_rx_resources ( struct igbvf_adapter *adapter )
00565 {
00566         int i;
00567         union e1000_adv_rx_desc *rx_curr_desc;
00568         struct io_buffer *iob;
00569 
00570         DBG ( "igbvf_setup_rx_resources\n" );
00571 
00572         /* Allocate receive descriptor ring memory.
00573            It must not cross a 64K boundary because of hardware errata
00574          */
00575 
00576         adapter->rx_base =
00577                 malloc_dma ( adapter->rx_ring_size, adapter->rx_ring_size );
00578 
00579         if ( ! adapter->rx_base ) {
00580                 return -ENOMEM;
00581         }
00582         memset ( adapter->rx_base, 0, adapter->rx_ring_size );
00583 
00584         for ( i = 0; i < NUM_RX_DESC; i++ ) {
00585                 rx_curr_desc = adapter->rx_base + i;
00586                 iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
00587                 adapter->rx_iobuf[i] = iob;
00588                 rx_curr_desc->wb.upper.status_error = 0;
00589                 if ( ! iob ) {
00590                         DBG ( "alloc_iob failed\n" );
00591                         return -ENOMEM;
00592                 } else {
00593                         rx_curr_desc->read.pkt_addr = virt_to_bus ( iob->data );
00594                         rx_curr_desc->read.hdr_addr = 0;
00595                 }
00596         }
00597 
00598         return 0;
00599 }
00600 
00601 /**
00602  * igbvf_open - Called when a network interface is made active
00603  * @netdev: network interface device structure
00604  *
00605  * Returns 0 on success, negative value on failure
00606  *
00607  * The open entry point is called when a network interface is made
00608  * active by the system (IFF_UP).  At this point all resources needed
00609  * for transmit and receive operations are allocated, the interrupt
00610  * handler is registered with the OS, the watchdog timer is started,
00611  * and the stack is notified that the interface is ready.
00612  **/
00613 static int igbvf_open ( struct net_device *netdev )
00614 {
00615         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00616         int err;
00617 
00618         DBG ("igbvf_open\n");
00619 
00620         /* Update MAC address */
00621         memcpy ( adapter->hw.mac.addr, netdev->ll_addr, ETH_ALEN );
00622         igbvf_reset( adapter );
00623 
00624         /* allocate transmit descriptors */
00625         err = igbvf_setup_tx_resources ( adapter );
00626         if (err) {
00627                 DBG ( "Error setting up TX resources!\n" );
00628                 goto err_setup_tx;
00629         }
00630 
00631         igbvf_configure_tx ( adapter );
00632 
00633         igbvf_setup_srrctl( adapter );
00634 
00635         err = igbvf_setup_rx_resources( adapter );
00636         if (err) {
00637                 DBG ( "Error setting up RX resources!\n" );
00638                 goto err_setup_rx;
00639         }
00640 
00641         igbvf_configure_rx ( adapter );
00642 
00643         return 0;
00644 
00645 err_setup_rx:
00646         DBG ( "err_setup_rx\n" );
00647         igbvf_free_tx_resources ( adapter );
00648         return err;
00649 
00650 err_setup_tx:
00651         DBG ( "err_setup_tx\n" );
00652         igbvf_reset ( adapter );
00653 
00654         return err;
00655 }
00656 
00657 /**
00658  * igbvf_close - Disables a network interface
00659  * @netdev: network interface device structure
00660  *
00661  * Returns 0, this is not allowed to fail
00662  *
00663  * The close entry point is called when an interface is de-activated
00664  * by the OS.  The hardware is still under the drivers control, but
00665  * needs to be disabled.  A global MAC reset is issued to stop the
00666  * hardware, and all transmit and receive resources are freed.
00667  **/
00668 static void igbvf_close ( struct net_device *netdev )
00669 {
00670         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00671         struct e1000_hw *hw = &adapter->hw;
00672         uint32_t rxdctl;
00673 
00674         DBG ( "igbvf_close\n" );
00675 
00676         /* Disable and acknowledge interrupts */
00677         igbvf_irq_disable ( adapter );
00678         er32(EICR);
00679 
00680         /* disable receives */
00681         rxdctl = er32 ( RXDCTL(0) );
00682         ew32 ( RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE );
00683         mdelay ( 10 );
00684 
00685         igbvf_reset ( adapter );
00686 
00687         igbvf_free_tx_resources( adapter );
00688         igbvf_free_rx_resources( adapter );
00689 }
00690 
00691 /**
00692  * igbvf_transmit - Transmit a packet
00693  *
00694  * @v netdev    Network device
00695  * @v iobuf     I/O buffer
00696  *
00697  * @ret rc       Returns 0 on success, negative on failure
00698  */
00699 static int igbvf_transmit ( struct net_device *netdev, struct io_buffer *iobuf )
00700 {
00701         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00702         struct e1000_hw *hw = &adapter->hw;
00703         uint32_t tx_curr = adapter->tx_tail;
00704         union e1000_adv_tx_desc *tx_curr_desc;
00705 
00706         DBGP ("igbvf_transmit\n");
00707 
00708         if ( adapter->tx_fill_ctr == NUM_TX_DESC ) {
00709                 DBG ("TX overflow\n");
00710                 return -ENOBUFS;
00711         }
00712 
00713         /* Save pointer to iobuf we have been given to transmit,
00714            netdev_tx_complete() will need it later
00715          */
00716         adapter->tx_iobuf[tx_curr] = iobuf;
00717 
00718         tx_curr_desc = ( void * ) ( adapter->tx_base ) +
00719                        ( tx_curr * sizeof ( *adapter->tx_base ) );
00720 
00721         DBG ( "tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
00722         DBG ( "tx_curr_desc + 16 = %#08lx\n", virt_to_bus ( tx_curr_desc ) + 16 );
00723         DBG ( "iobuf->data = %#08lx\n", virt_to_bus ( iobuf->data ) );
00724 
00725         /* Add the packet to TX ring
00726          */
00727         tx_curr_desc->read.buffer_addr = virt_to_bus ( iobuf->data );
00728         tx_curr_desc->read.cmd_type_len = adapter->txd_cmd |(iob_len ( iobuf )) ;
00729         // minus hdr_len ????
00730         tx_curr_desc->read.olinfo_status = ((iob_len ( iobuf )) << E1000_ADVTXD_PAYLEN_SHIFT);
00731 
00732         DBG ( "TX fill: %d tx_curr: %d addr: %#08lx len: %zd\n", adapter->tx_fill_ctr,
00733               tx_curr, virt_to_bus ( iobuf->data ), iob_len ( iobuf ) );
00734 
00735         /* Point to next free descriptor */
00736         adapter->tx_tail = ( adapter->tx_tail + 1 ) % NUM_TX_DESC;
00737         adapter->tx_fill_ctr++;
00738 
00739         /* Write new tail to NIC, making packet available for transmit
00740          */
00741         ew32 ( TDT(0), adapter->tx_tail );
00742         e1e_flush ();
00743 
00744         return 0;
00745 }
00746 
00747 /** igbvf net device operations */
00748 static struct net_device_operations igbvf_operations = {
00749         .open           = igbvf_open,
00750         .close          = igbvf_close,
00751         .transmit       = igbvf_transmit,
00752         .poll           = igbvf_poll,
00753         .irq            = igbvf_irq,
00754 };
00755 
00756 /**
00757  * igbvf_get_hw_control - get control of the h/w from f/w
00758  * @adapter: address of board private structure
00759  *
00760  * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
00761  * For ASF and Pass Through versions of f/w this means that
00762  * the driver is loaded.
00763  *
00764  **/
00765 void igbvf_get_hw_control ( struct igbvf_adapter *adapter )
00766 {
00767         struct e1000_hw *hw = &adapter->hw;
00768         u32 ctrl_ext;
00769 
00770         /* Let firmware know the driver has taken over */
00771         ctrl_ext = er32 ( CTRL_EXT );
00772         ew32 ( CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD );
00773 }
00774 
00775 /**
00776  * igbvf_probe - Device Initialization Routine
00777  * @pdev: PCI device information struct
00778  * @ent: entry in igbvf_pci_tbl
00779  *
00780  * Returns 0 on success, negative on failure
00781  *
00782  * igbvf_probe initializes an adapter identified by a pci_dev structure.
00783  * The OS initialization, configuring of the adapter private structure,
00784  * and a hardware reset occur.
00785  **/
00786 int igbvf_probe ( struct pci_device *pdev )
00787 {
00788         int err;
00789         struct net_device *netdev;
00790         struct igbvf_adapter *adapter;
00791         unsigned long mmio_start, mmio_len;
00792         struct e1000_hw *hw;
00793 
00794         DBG ( "igbvf_probe\n" );
00795 
00796         err = -ENOMEM;
00797 
00798         /* Allocate net device ( also allocates memory for netdev->priv
00799           and makes netdev-priv point to it ) */
00800         netdev = alloc_etherdev ( sizeof ( struct igbvf_adapter ) );
00801         if ( ! netdev )
00802                 goto err_alloc_etherdev;
00803 
00804         /* Associate igbvf-specific network operations operations with
00805          * generic network device layer */
00806         netdev_init ( netdev, &igbvf_operations );
00807 
00808         /* Associate this network device with given PCI device */
00809         pci_set_drvdata ( pdev, netdev );
00810         netdev->dev = &pdev->dev;
00811 
00812         /* Initialize driver private storage */
00813         adapter = netdev_priv ( netdev );
00814         memset ( adapter, 0, ( sizeof ( *adapter ) ) );
00815 
00816         adapter->pdev = pdev;
00817 
00818         adapter->ioaddr = pdev->ioaddr;
00819         adapter->hw.io_base = pdev->ioaddr;
00820 
00821         hw = &adapter->hw;
00822         hw->vendor_id = pdev->vendor;
00823         hw->device_id = pdev->device;
00824 
00825         adapter->irqno = pdev->irq;
00826         adapter->netdev = netdev;
00827         adapter->hw.back = adapter;
00828 
00829         adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
00830         adapter->max_hw_frame_size = ETH_FRAME_LEN + ETH_FCS_LEN;
00831 
00832         adapter->tx_ring_size = sizeof ( *adapter->tx_base ) * NUM_TX_DESC;
00833         adapter->rx_ring_size = sizeof ( *adapter->rx_base ) * NUM_RX_DESC;
00834 
00835         /* Fix up PCI device */
00836         adjust_pci_device ( pdev );
00837 
00838         err = -EIO;
00839 
00840         mmio_start = pci_bar_start ( pdev, PCI_BASE_ADDRESS_0 );
00841         mmio_len   = pci_bar_size  ( pdev, PCI_BASE_ADDRESS_0 );
00842 
00843         DBG ( "mmio_start: %#08lx\n", mmio_start );
00844         DBG ( "mmio_len: %#08lx\n", mmio_len );
00845 
00846         adapter->hw.hw_addr = ioremap ( mmio_start, mmio_len );
00847         DBG ( "adapter->hw.hw_addr: %p\n", adapter->hw.hw_addr );
00848 
00849         if ( ! adapter->hw.hw_addr ) {
00850                 DBG ( "err_ioremap\n" );
00851                 goto err_ioremap;
00852         }
00853 
00854         /* setup adapter struct */
00855         err = igbvf_sw_init ( adapter );
00856         if (err) {
00857                 DBG ( "err_sw_init\n" );
00858                 goto err_sw_init;
00859         }
00860 
00861         /* reset the controller to put the device in a known good state */
00862         err = hw->mac.ops.reset_hw ( hw );
00863         if ( err ) {
00864                 DBG ("PF still in reset state, assigning new address\n");
00865                 netdev->hw_addr[0] = 0x21;
00866                 netdev->hw_addr[1] = 0x21;
00867                 netdev->hw_addr[2] = 0x21;
00868                 netdev->hw_addr[3] = 0x21;
00869                 netdev->hw_addr[4] = 0x21;
00870                 netdev->hw_addr[5] = 0x21;
00871                 netdev->hw_addr[6] = 0x21;
00872         } else {
00873                 err = hw->mac.ops.read_mac_addr(hw);
00874                 if (err) {
00875                         DBG ("Error reading MAC address\n");
00876                         goto err_hw_init;
00877                 }
00878                 if ( ! is_valid_ether_addr(adapter->hw.mac.addr) ) {
00879                         /* Assign random MAC address */
00880                         eth_random_addr(adapter->hw.mac.addr);
00881                 }
00882         }
00883 
00884         memcpy ( netdev->hw_addr, adapter->hw.mac.addr, ETH_ALEN );
00885 
00886         /* reset the hardware with the new settings */
00887         igbvf_reset ( adapter );
00888 
00889         /* let the f/w know that the h/w is now under the control of the
00890          * driver. */
00891         igbvf_get_hw_control ( adapter );
00892 
00893         /* Mark as link up; we don't yet handle link state */
00894         netdev_link_up ( netdev );
00895 
00896         if ( ( err = register_netdev ( netdev ) ) != 0) {
00897                 DBG ( "err_register\n" );
00898                 goto err_register;
00899         }
00900 
00901         DBG ("igbvf_probe_succeeded\n");
00902 
00903         return 0;
00904 
00905 err_register:
00906 err_hw_init:
00907 err_sw_init:
00908         iounmap ( adapter->hw.hw_addr );
00909 err_ioremap:
00910         netdev_put ( netdev );
00911 err_alloc_etherdev:
00912         return err;
00913 }
00914 
00915 /**
00916  * igbvf_remove - Device Removal Routine
00917  * @pdev: PCI device information struct
00918  *
00919  * igbvf_remove is called by the PCI subsystem to alert the driver
00920  * that it should release a PCI device.  The could be caused by a
00921  * Hot-Plug event, or because the driver is going to be removed from
00922  * memory.
00923  **/
00924 void igbvf_remove ( struct pci_device *pdev )
00925 {
00926         struct net_device *netdev = pci_get_drvdata ( pdev );
00927         struct igbvf_adapter *adapter = netdev_priv ( netdev );
00928 
00929         DBG ( "igbvf_remove\n" );
00930 
00931         if ( adapter->hw.flash_address )
00932                 iounmap ( adapter->hw.flash_address );
00933         if  ( adapter->hw.hw_addr )
00934                 iounmap ( adapter->hw.hw_addr );
00935 
00936         unregister_netdev ( netdev );
00937         igbvf_reset  ( adapter );
00938         netdev_nullify ( netdev );
00939         netdev_put ( netdev );
00940 }
00941 
00942 static struct pci_device_id igbvf_pci_tbl[] = {
00943         PCI_ROM(0x8086, 0x10CA, "igbvf", "E1000_DEV_ID_82576_VF", 0),
00944         PCI_ROM(0x8086, 0x1520, "i350vf", "E1000_DEV_ID_I350_VF", 0),
00945 };
00946 
00947 
00948 struct pci_driver igbvf_driver __pci_driver = {
00949         .ids = igbvf_pci_tbl,
00950         .id_count = (sizeof(igbvf_pci_tbl) / sizeof(igbvf_pci_tbl[0])),
00951         .probe = igbvf_probe,
00952         .remove = igbvf_remove,
00953 };