iPXE
intelx.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 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 <string.h>
00028 #include <unistd.h>
00029 #include <errno.h>
00030 #include <byteswap.h>
00031 #include <ipxe/netdevice.h>
00032 #include <ipxe/ethernet.h>
00033 #include <ipxe/if_ether.h>
00034 #include <ipxe/iobuf.h>
00035 #include <ipxe/malloc.h>
00036 #include <ipxe/pci.h>
00037 #include "intelx.h"
00038 
00039 /** @file
00040  *
00041  * Intel 10 Gigabit Ethernet network card driver
00042  *
00043  */
00044 
00045 /******************************************************************************
00046  *
00047  * MAC address
00048  *
00049  ******************************************************************************
00050  */
00051 
00052 /**
00053  * Try to fetch initial MAC address
00054  *
00055  * @v intel             Intel device
00056  * @v ral0              RAL0 register address
00057  * @v hw_addr           Hardware address to fill in
00058  * @ret rc              Return status code
00059  */
00060 static int intelx_try_fetch_mac ( struct intel_nic *intel, unsigned int ral0,
00061                                   uint8_t *hw_addr ) {
00062         union intel_receive_address mac;
00063 
00064         /* Read current address from RAL0/RAH0 */
00065         mac.reg.low = cpu_to_le32 ( readl ( intel->regs + ral0 ) );
00066         mac.reg.high = cpu_to_le32 ( readl ( intel->regs + ral0 +
00067                                              ( INTELX_RAH0 - INTELX_RAL0 ) ) );
00068 
00069         /* Use current address if valid */
00070         if ( is_valid_ether_addr ( mac.raw ) ) {
00071                 DBGC ( intel, "INTEL %p has autoloaded MAC address %s at "
00072                        "%#05x\n", intel, eth_ntoa ( mac.raw ), ral0 );
00073                 memcpy ( hw_addr, mac.raw, ETH_ALEN );
00074                 return 0;
00075         }
00076 
00077         return -ENOENT;
00078 }
00079 
00080 /**
00081  * Fetch initial MAC address
00082  *
00083  * @v intel             Intel device
00084  * @v hw_addr           Hardware address to fill in
00085  * @ret rc              Return status code
00086  */
00087 static int intelx_fetch_mac ( struct intel_nic *intel, uint8_t *hw_addr ) {
00088         int rc;
00089 
00090         /* Try to fetch address from INTELX_RAL0 */
00091         if ( ( rc = intelx_try_fetch_mac ( intel, INTELX_RAL0,
00092                                            hw_addr ) ) == 0 ) {
00093                 return 0;
00094         }
00095 
00096         /* Try to fetch address from INTELX_RAL0_ALT */
00097         if ( ( rc = intelx_try_fetch_mac ( intel, INTELX_RAL0_ALT,
00098                                            hw_addr ) ) == 0 ) {
00099                 return 0;
00100         }
00101 
00102         DBGC ( intel, "INTEL %p has no MAC address to use\n", intel );
00103         return -ENOENT;
00104 }
00105 
00106 /******************************************************************************
00107  *
00108  * Device reset
00109  *
00110  ******************************************************************************
00111  */
00112 
00113 /**
00114  * Reset hardware
00115  *
00116  * @v intel             Intel device
00117  * @ret rc              Return status code
00118  */
00119 static int intelx_reset ( struct intel_nic *intel ) {
00120         uint32_t ctrl;
00121 
00122         /* Perform a global software reset */
00123         ctrl = readl ( intel->regs + INTELX_CTRL );
00124         writel ( ( ctrl | INTELX_CTRL_RST | INTELX_CTRL_LRST ),
00125                  intel->regs + INTELX_CTRL );
00126         mdelay ( INTELX_RESET_DELAY_MS );
00127 
00128         DBGC ( intel, "INTEL %p reset (ctrl %08x)\n", intel, ctrl );
00129         return 0;
00130 }
00131 
00132 /******************************************************************************
00133  *
00134  * Link state
00135  *
00136  ******************************************************************************
00137  */
00138 
00139 /**
00140  * Check link state
00141  *
00142  * @v netdev            Network device
00143  */
00144 static void intelx_check_link ( struct net_device *netdev ) {
00145         struct intel_nic *intel = netdev->priv;
00146         uint32_t links;
00147 
00148         /* Read link status */
00149         links = readl ( intel->regs + INTELX_LINKS );
00150         DBGC ( intel, "INTEL %p link status is %08x\n", intel, links );
00151 
00152         /* Update network device */
00153         if ( links & INTELX_LINKS_UP ) {
00154                 netdev_link_up ( netdev );
00155         } else {
00156                 netdev_link_down ( netdev );
00157         }
00158 }
00159 
00160 /******************************************************************************
00161  *
00162  * Network device interface
00163  *
00164  ******************************************************************************
00165  */
00166 
00167 /**
00168  * Open network device
00169  *
00170  * @v netdev            Network device
00171  * @ret rc              Return status code
00172  */
00173 static int intelx_open ( struct net_device *netdev ) {
00174         struct intel_nic *intel = netdev->priv;
00175         union intel_receive_address mac;
00176         uint32_t ral0;
00177         uint32_t rah0;
00178         uint32_t dmatxctl;
00179         uint32_t fctrl;
00180         uint32_t srrctl;
00181         uint32_t hlreg0;
00182         uint32_t maxfrs;
00183         uint32_t rdrxctl;
00184         uint32_t rxctrl;
00185         uint32_t dca_rxctrl;
00186         int rc;
00187 
00188         /* Create transmit descriptor ring */
00189         if ( ( rc = intel_create_ring ( intel, &intel->tx ) ) != 0 )
00190                 goto err_create_tx;
00191 
00192         /* Create receive descriptor ring */
00193         if ( ( rc = intel_create_ring ( intel, &intel->rx ) ) != 0 )
00194                 goto err_create_rx;
00195 
00196         /* Program MAC address */
00197         memset ( &mac, 0, sizeof ( mac ) );
00198         memcpy ( mac.raw, netdev->ll_addr, sizeof ( mac.raw ) );
00199         ral0 = le32_to_cpu ( mac.reg.low );
00200         rah0 = ( le32_to_cpu ( mac.reg.high ) | INTELX_RAH0_AV );
00201         writel ( ral0, intel->regs + INTELX_RAL0 );
00202         writel ( rah0, intel->regs + INTELX_RAH0 );
00203         writel ( ral0, intel->regs + INTELX_RAL0_ALT );
00204         writel ( rah0, intel->regs + INTELX_RAH0_ALT );
00205 
00206         /* Allocate interrupt vectors */
00207         writel ( ( INTELX_IVAR_RX0_DEFAULT | INTELX_IVAR_RX0_VALID |
00208                    INTELX_IVAR_TX0_DEFAULT | INTELX_IVAR_TX0_VALID ),
00209                  intel->regs + INTELX_IVAR );
00210 
00211         /* Enable transmitter  */
00212         dmatxctl = readl ( intel->regs + INTELX_DMATXCTL );
00213         dmatxctl |= INTELX_DMATXCTL_TE;
00214         writel ( dmatxctl, intel->regs + INTELX_DMATXCTL );
00215 
00216         /* Configure receive filter */
00217         fctrl = readl ( intel->regs + INTELX_FCTRL );
00218         fctrl |= ( INTELX_FCTRL_BAM | INTELX_FCTRL_UPE | INTELX_FCTRL_MPE );
00219         writel ( fctrl, intel->regs + INTELX_FCTRL );
00220 
00221         /* Configure receive buffer sizes */
00222         srrctl = readl ( intel->regs + INTELX_SRRCTL );
00223         srrctl &= ~INTELX_SRRCTL_BSIZE_MASK;
00224         srrctl |= INTELX_SRRCTL_BSIZE_DEFAULT;
00225         writel ( srrctl, intel->regs + INTELX_SRRCTL );
00226 
00227         /* Configure jumbo frames.  Required to allow the extra 4-byte
00228          * headroom for VLANs, since we don't use the hardware's
00229          * native VLAN offload.
00230          */
00231         hlreg0 = readl ( intel->regs + INTELX_HLREG0 );
00232         hlreg0 |= INTELX_HLREG0_JUMBOEN;
00233         writel ( hlreg0, intel->regs + INTELX_HLREG0 );
00234 
00235         /* Configure frame size */
00236         maxfrs = readl ( intel->regs + INTELX_MAXFRS );
00237         maxfrs &= ~INTELX_MAXFRS_MFS_MASK;
00238         maxfrs |= INTELX_MAXFRS_MFS_DEFAULT;
00239         writel ( maxfrs, intel->regs + INTELX_MAXFRS );
00240 
00241         /* Configure receive DMA */
00242         rdrxctl = readl ( intel->regs + INTELX_RDRXCTL );
00243         rdrxctl |= INTELX_RDRXCTL_SECRC;
00244         writel ( rdrxctl, intel->regs + INTELX_RDRXCTL );
00245 
00246         /* Clear "must-be-zero" bit for direct cache access (DCA).  We
00247          * leave DCA disabled anyway, but if we do not clear this bit
00248          * then the received packets contain garbage data.
00249          */
00250         dca_rxctrl = readl ( intel->regs + INTELX_DCA_RXCTRL );
00251         dca_rxctrl &= ~INTELX_DCA_RXCTRL_MUST_BE_ZERO;
00252         writel ( dca_rxctrl, intel->regs + INTELX_DCA_RXCTRL );
00253 
00254         /* Enable receiver */
00255         rxctrl = readl ( intel->regs + INTELX_RXCTRL );
00256         rxctrl |= INTELX_RXCTRL_RXEN;
00257         writel ( rxctrl, intel->regs + INTELX_RXCTRL );
00258 
00259         /* Fill receive ring */
00260         intel_refill_rx ( intel );
00261 
00262         /* Update link state */
00263         intelx_check_link ( netdev );
00264 
00265         return 0;
00266 
00267         intel_destroy_ring ( intel, &intel->rx );
00268  err_create_rx:
00269         intel_destroy_ring ( intel, &intel->tx );
00270  err_create_tx:
00271         return rc;
00272 }
00273 
00274 /**
00275  * Close network device
00276  *
00277  * @v netdev            Network device
00278  */
00279 static void intelx_close ( struct net_device *netdev ) {
00280         struct intel_nic *intel = netdev->priv;
00281         uint32_t rxctrl;
00282         uint32_t dmatxctl;
00283 
00284         /* Disable receiver */
00285         rxctrl = readl ( intel->regs + INTELX_RXCTRL );
00286         rxctrl &= ~INTELX_RXCTRL_RXEN;
00287         writel ( rxctrl, intel->regs + INTELX_RXCTRL );
00288 
00289         /* Disable transmitter  */
00290         dmatxctl = readl ( intel->regs + INTELX_DMATXCTL );
00291         dmatxctl &= ~INTELX_DMATXCTL_TE;
00292         writel ( dmatxctl, intel->regs + INTELX_DMATXCTL );
00293 
00294         /* Destroy receive descriptor ring */
00295         intel_destroy_ring ( intel, &intel->rx );
00296 
00297         /* Discard any unused receive buffers */
00298         intel_empty_rx ( intel );
00299 
00300         /* Destroy transmit descriptor ring */
00301         intel_destroy_ring ( intel, &intel->tx );
00302 
00303         /* Reset the NIC, to flush the transmit and receive FIFOs */
00304         intelx_reset ( intel );
00305 }
00306 
00307 /**
00308  * Poll for completed and received packets
00309  *
00310  * @v netdev            Network device
00311  */
00312 static void intelx_poll ( struct net_device *netdev ) {
00313         struct intel_nic *intel = netdev->priv;
00314         uint32_t eicr;
00315 
00316         /* Check for and acknowledge interrupts */
00317         eicr = readl ( intel->regs + INTELX_EICR );
00318         if ( ! eicr )
00319                 return;
00320 
00321         /* Poll for TX completions, if applicable */
00322         if ( eicr & INTELX_EIRQ_TX0 )
00323                 intel_poll_tx ( netdev );
00324 
00325         /* Poll for RX completions, if applicable */
00326         if ( eicr & ( INTELX_EIRQ_RX0 | INTELX_EIRQ_RXO ) )
00327                 intel_poll_rx ( netdev );
00328 
00329         /* Report receive overruns */
00330         if ( eicr & INTELX_EIRQ_RXO )
00331                 netdev_rx_err ( netdev, NULL, -ENOBUFS );
00332 
00333         /* Check link state, if applicable */
00334         if ( eicr & INTELX_EIRQ_LSC )
00335                 intelx_check_link ( netdev );
00336 
00337         /* Refill RX ring */
00338         intel_refill_rx ( intel );
00339 }
00340 
00341 /**
00342  * Enable or disable interrupts
00343  *
00344  * @v netdev            Network device
00345  * @v enable            Interrupts should be enabled
00346  */
00347 static void intelx_irq ( struct net_device *netdev, int enable ) {
00348         struct intel_nic *intel = netdev->priv;
00349         uint32_t mask;
00350 
00351         mask = ( INTELX_EIRQ_LSC | INTELX_EIRQ_RXO | INTELX_EIRQ_TX0 |
00352                  INTELX_EIRQ_RX0 );
00353         if ( enable ) {
00354                 writel ( mask, intel->regs + INTELX_EIMS );
00355         } else {
00356                 writel ( mask, intel->regs + INTELX_EIMC );
00357         }
00358 }
00359 
00360 /** Network device operations */
00361 static struct net_device_operations intelx_operations = {
00362         .open           = intelx_open,
00363         .close          = intelx_close,
00364         .transmit       = intel_transmit,
00365         .poll           = intelx_poll,
00366         .irq            = intelx_irq,
00367 };
00368 
00369 /******************************************************************************
00370  *
00371  * PCI interface
00372  *
00373  ******************************************************************************
00374  */
00375 
00376 /**
00377  * Probe PCI device
00378  *
00379  * @v pci               PCI device
00380  * @ret rc              Return status code
00381  */
00382 static int intelx_probe ( struct pci_device *pci ) {
00383         struct net_device *netdev;
00384         struct intel_nic *intel;
00385         int rc;
00386 
00387         /* Allocate and initialise net device */
00388         netdev = alloc_etherdev ( sizeof ( *intel ) );
00389         if ( ! netdev ) {
00390                 rc = -ENOMEM;
00391                 goto err_alloc;
00392         }
00393         netdev_init ( netdev, &intelx_operations );
00394         intel = netdev->priv;
00395         pci_set_drvdata ( pci, netdev );
00396         netdev->dev = &pci->dev;
00397         memset ( intel, 0, sizeof ( *intel ) );
00398         intel->port = PCI_FUNC ( pci->busdevfn );
00399         intel_init_ring ( &intel->tx, INTEL_NUM_TX_DESC, INTELX_TD,
00400                           intel_describe_tx );
00401         intel_init_ring ( &intel->rx, INTEL_NUM_RX_DESC, INTELX_RD,
00402                           intel_describe_rx );
00403 
00404         /* Fix up PCI device */
00405         adjust_pci_device ( pci );
00406 
00407         /* Map registers */
00408         intel->regs = ioremap ( pci->membase, INTEL_BAR_SIZE );
00409         if ( ! intel->regs ) {
00410                 rc = -ENODEV;
00411                 goto err_ioremap;
00412         }
00413 
00414         /* Reset the NIC */
00415         if ( ( rc = intelx_reset ( intel ) ) != 0 )
00416                 goto err_reset;
00417 
00418         /* Fetch MAC address */
00419         if ( ( rc = intelx_fetch_mac ( intel, netdev->hw_addr ) ) != 0 )
00420                 goto err_fetch_mac;
00421 
00422         /* Register network device */
00423         if ( ( rc = register_netdev ( netdev ) ) != 0 )
00424                 goto err_register_netdev;
00425 
00426         /* Set initial link state */
00427         intelx_check_link ( netdev );
00428 
00429         return 0;
00430 
00431         unregister_netdev ( netdev );
00432  err_register_netdev:
00433  err_fetch_mac:
00434         intelx_reset ( intel );
00435  err_reset:
00436         iounmap ( intel->regs );
00437  err_ioremap:
00438         netdev_nullify ( netdev );
00439         netdev_put ( netdev );
00440  err_alloc:
00441         return rc;
00442 }
00443 
00444 /**
00445  * Remove PCI device
00446  *
00447  * @v pci               PCI device
00448  */
00449 static void intelx_remove ( struct pci_device *pci ) {
00450         struct net_device *netdev = pci_get_drvdata ( pci );
00451         struct intel_nic *intel = netdev->priv;
00452 
00453         /* Unregister network device */
00454         unregister_netdev ( netdev );
00455 
00456         /* Reset the NIC */
00457         intelx_reset ( intel );
00458 
00459         /* Free network device */
00460         iounmap ( intel->regs );
00461         netdev_nullify ( netdev );
00462         netdev_put ( netdev );
00463 }
00464 
00465 /** PCI device IDs */
00466 static struct pci_device_id intelx_nics[] = {
00467         PCI_ROM ( 0x8086, 0x10f7, "82599-kx4", "82599 (KX/KX4)", 0 ),
00468         PCI_ROM ( 0x8086, 0x10f8, "82599-combo-backplane", "82599 (combined backplane; KR/KX4/KX)", 0 ),
00469         PCI_ROM ( 0x8086, 0x10f9, "82599-cx4", "82599 (CX4)", 0 ),
00470         PCI_ROM ( 0x8086, 0x10fb, "82599-sfp", "82599 (SFI/SFP+)", 0 ),
00471         PCI_ROM ( 0x8086, 0x10fc, "82599-xaui", "82599 (XAUI/BX4)", 0 ),
00472         PCI_ROM ( 0x8086, 0x1528, "x540t", "X540-AT2/X540-BT2", 0 ),
00473         PCI_ROM ( 0x8086, 0x154d, "82599-sfp-sf2", "82599 (SFI/SFP+)", 0 ),
00474         PCI_ROM ( 0x8086, 0x1557, "82599en-sfp", "82599 (Single Port SFI Only)", 0 ),
00475         PCI_ROM ( 0x8086, 0x1560, "x540t1", "X540-AT2/X540-BT2 (with single port NVM)", 0 ),
00476         PCI_ROM ( 0x8086, 0x1563, "x550t2", "X550-T2", 0 ),
00477         PCI_ROM ( 0x8086, 0x15ab, "x552", "X552", 0 ),
00478         PCI_ROM ( 0x8086, 0x15e5, "x553", "X553", 0 ),
00479 };
00480 
00481 /** PCI driver */
00482 struct pci_driver intelx_driver __pci_driver = {
00483         .ids = intelx_nics,
00484         .id_count = ( sizeof ( intelx_nics ) / sizeof ( intelx_nics[0] ) ),
00485         .probe = intelx_probe,
00486         .remove = intelx_remove,
00487 };