iPXE
intelxvf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 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 <string.h>
00027 #include <unistd.h>
00028 #include <errno.h>
00029 #include <ipxe/io.h>
00030 #include <ipxe/pci.h>
00031 #include <ipxe/netdevice.h>
00032 #include <ipxe/ethernet.h>
00033 #include "intelx.h"
00034 #include "intelxvf.h"
00035 
00036 /** @file
00037  *
00038  * Intel 10 Gigabit Ethernet virtual function network card driver
00039  *
00040  */
00041 
00042 /******************************************************************************
00043  *
00044  * Diagnostics
00045  *
00046  ******************************************************************************
00047  */
00048 
00049 /**
00050  * Dump statistics
00051  *
00052  * @v intel             Intel device
00053  */
00054 static __attribute__ (( unused )) void
00055 intelxvf_stats ( struct intel_nic *intel ) {
00056 
00057         DBGC ( intel, "INTEL %p TX %d (%#x%08x) RX %d (%#x%08x) multi %d\n",
00058                intel, readl ( intel->regs + INTELXVF_GPTC ),
00059                readl ( intel->regs + INTELXVF_GOTCH ),
00060                readl ( intel->regs + INTELXVF_GOTCL ),
00061                readl ( intel->regs + INTELXVF_GPRC ),
00062                readl ( intel->regs + INTELXVF_GORCH ),
00063                readl ( intel->regs + INTELXVF_GORCL ),
00064                readl ( intel->regs + INTELXVF_MPRC ) );
00065 }
00066 
00067 /******************************************************************************
00068  *
00069  * Device reset
00070  *
00071  ******************************************************************************
00072  */
00073 
00074 /**
00075  * Reset hardware
00076  *
00077  * @v intel             Intel device
00078  */
00079 static void intelxvf_reset ( struct intel_nic *intel ) {
00080 
00081         /* Perform a function-level reset */
00082         writel ( INTELXVF_CTRL_RST, intel->regs + INTELXVF_CTRL );
00083 }
00084 
00085 /******************************************************************************
00086  *
00087  * Link state
00088  *
00089  ******************************************************************************
00090  */
00091 
00092 /**
00093  * Check link state
00094  *
00095  * @v netdev            Network device
00096  */
00097 static void intelxvf_check_link ( struct net_device *netdev ) {
00098         struct intel_nic *intel = netdev->priv;
00099         uint32_t links;
00100 
00101         /* Read link status */
00102         links = readl ( intel->regs + INTELXVF_LINKS );
00103         DBGC ( intel, "INTEL %p link status is %08x\n", intel, links );
00104 
00105         /* Update network device */
00106         if ( links & INTELXVF_LINKS_UP ) {
00107                 netdev_link_up ( netdev );
00108         } else {
00109                 netdev_link_down ( netdev );
00110         }
00111 }
00112 
00113 /******************************************************************************
00114  *
00115  * Mailbox messages
00116  *
00117  ******************************************************************************
00118  */
00119 
00120 /**
00121  * Send negotiate API version message
00122  *
00123  * @v intel             Intel device
00124  * @v version           Requested version
00125  * @ret rc              Return status code
00126  */
00127 static int intelxvf_mbox_version ( struct intel_nic *intel,
00128                                    unsigned int version ) {
00129         union intelvf_msg msg;
00130         int rc;
00131 
00132         /* Send set MTU message */
00133         memset ( &msg, 0, sizeof ( msg ) );
00134         msg.hdr = INTELXVF_MSG_TYPE_VERSION;
00135         msg.version.version = version;
00136         if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
00137                 DBGC ( intel, "INTEL %p negotiate API version failed: %s\n",
00138                        intel, strerror ( rc ) );
00139                 return rc;
00140         }
00141 
00142         /* Check response */
00143         if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) != INTELXVF_MSG_TYPE_VERSION ){
00144                 DBGC ( intel, "INTEL %p negotiate API version unexpected "
00145                        "response:\n", intel );
00146                 DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
00147                 return -EPROTO;
00148         }
00149 
00150         /* Check that this version is supported */
00151         if ( ! ( msg.hdr & INTELVF_MSG_ACK ) ) {
00152                 DBGC ( intel, "INTEL %p negotiate API version failed\n",
00153                        intel );
00154                 return -EPERM;
00155         }
00156 
00157         return 0;
00158 }
00159 
00160 /**
00161  * Get queue configuration
00162  *
00163  * @v intel             Intel device
00164  * @v vlan_thing        VLAN hand-waving thing to fill in
00165  * @ret rc              Return status code
00166  */
00167 static int intelxvf_mbox_queues ( struct intel_nic *intel, int *vlan_thing ) {
00168         union intelvf_msg msg;
00169         int rc;
00170 
00171         /* Send queue configuration message */
00172         memset ( &msg, 0, sizeof ( msg ) );
00173         msg.hdr = INTELVF_MSG_TYPE_GET_QUEUES;
00174         if ( ( rc = intelvf_mbox_msg ( intel, &msg ) ) != 0 ) {
00175                 DBGC ( intel, "INTEL %p get queue configuration failed: %s\n",
00176                        intel, strerror ( rc ) );
00177                 return rc;
00178         }
00179 
00180         /* Check response */
00181         if ( ( msg.hdr & INTELVF_MSG_TYPE_MASK ) !=INTELVF_MSG_TYPE_GET_QUEUES){
00182                 DBGC ( intel, "INTEL %p get queue configuration unexpected "
00183                        "response:\n", intel );
00184                 DBGC_HDA ( intel, 0, &msg, sizeof ( msg ) );
00185                 return -EPROTO;
00186         }
00187 
00188         /* Check that we were allowed to get the queue configuration */
00189         if ( ! ( msg.hdr & INTELVF_MSG_ACK ) ) {
00190                 DBGC ( intel, "INTEL %p get queue configuration refused\n",
00191                        intel );
00192                 return -EPERM;
00193         }
00194 
00195         /* Extract VLAN hand-waving thing */
00196         *vlan_thing = msg.queues.vlan_thing;
00197 
00198         return 0;
00199 }
00200 
00201 /******************************************************************************
00202  *
00203  * Network device interface
00204  *
00205  ******************************************************************************
00206  */
00207 
00208 /**
00209  * Open network device
00210  *
00211  * @v netdev            Network device
00212  * @ret rc              Return status code
00213  */
00214 static int intelxvf_open ( struct net_device *netdev ) {
00215         struct intel_nic *intel = netdev->priv;
00216         uint32_t rxdctl;
00217         uint32_t srrctl;
00218         uint32_t dca_rxctrl;
00219         unsigned int i;
00220         int vlan_thing;
00221         int rc;
00222 
00223         /* Reset the function */
00224         intelxvf_reset ( intel );
00225 
00226         /* Notify PF that reset is complete */
00227         if ( ( rc = intelvf_mbox_reset ( intel, NULL ) ) != 0 ) {
00228                 DBGC ( intel, "INTEL %p could not reset: %s\n",
00229                        intel, strerror ( rc ) );
00230                 goto err_mbox_reset;
00231         }
00232 
00233         /* Negotiate API version 1.1.  If we do not negotiate at least
00234          * this version, then the RX datapath will remain disabled if
00235          * the PF has jumbo frames enabled.
00236          *
00237          * Ignore failures, since the host may not actually support
00238          * v1.1.
00239          */
00240         intelxvf_mbox_version ( intel, INTELXVF_MSG_VERSION_1_1 );
00241 
00242         /* Set MAC address */
00243         if ( ( rc = intelvf_mbox_set_mac ( intel, netdev->ll_addr ) ) != 0 ) {
00244                 DBGC ( intel, "INTEL %p could not set MAC address: %s\n",
00245                        intel, strerror ( rc ) );
00246                 goto err_mbox_set_mac;
00247         }
00248 
00249         /* Set MTU */
00250         if ( ( rc = intelvf_mbox_set_mtu ( intel, netdev->max_pkt_len ) ) != 0){
00251                 DBGC ( intel, "INTEL %p could not set MTU %zd: %s\n",
00252                        intel, netdev->max_pkt_len, strerror ( rc ) );
00253                 goto err_mbox_set_mtu;
00254         }
00255 
00256         /* Reset all descriptor rings */
00257         for ( i = 0 ; i < INTELXVF_NUM_RINGS ; i++ ) {
00258                 intel_reset_ring ( intel, INTELXVF_TD ( i ) );
00259                 intel_reset_ring ( intel, INTELXVF_RD ( i ) );
00260         }
00261 
00262         /* Reset packet split receive type register */
00263         writel ( 0, intel->regs + INTELXVF_PSRTYPE );
00264 
00265         /* Get queue configuration.  Ignore failures, since the host
00266          * may not support this message.
00267          */
00268         vlan_thing = 0;
00269         intelxvf_mbox_queues ( intel, &vlan_thing );
00270         if ( vlan_thing ) {
00271                 DBGC ( intel, "INTEL %p stripping VLAN tags (thing=%d)\n",
00272                        intel, vlan_thing );
00273                 rxdctl = readl ( intel->regs + INTELXVF_RD(0) + INTEL_xDCTL );
00274                 rxdctl |= INTELX_RXDCTL_VME;
00275                 writel ( rxdctl, intel->regs + INTELXVF_RD(0) + INTEL_xDCTL );
00276         }
00277 
00278         /* Create transmit descriptor ring */
00279         if ( ( rc = intel_create_ring ( intel, &intel->tx ) ) != 0 )
00280                 goto err_create_tx;
00281 
00282         /* Create receive descriptor ring */
00283         if ( ( rc = intel_create_ring ( intel, &intel->rx ) ) != 0 )
00284                 goto err_create_rx;
00285 
00286         /* Allocate interrupt vectors */
00287         writel ( ( INTELXVF_IVAR_RX0_DEFAULT | INTELXVF_IVAR_RX0_VALID |
00288                    INTELXVF_IVAR_TX0_DEFAULT | INTELXVF_IVAR_TX0_VALID ),
00289                  intel->regs + INTELXVF_IVAR );
00290         writel ( ( INTELXVF_IVARM_MBOX_DEFAULT | INTELXVF_IVARM_MBOX_VALID ),
00291                  intel->regs + INTELXVF_IVARM );
00292 
00293         /* Configure receive buffer sizes and set receive descriptor type */
00294         srrctl = readl ( intel->regs + INTELXVF_SRRCTL );
00295         srrctl &= ~( INTELXVF_SRRCTL_BSIZE_MASK |
00296                      INTELXVF_SRRCTL_BHDRSIZE_MASK |
00297                      INTELXVF_SRRCTL_DESCTYPE_MASK );
00298         srrctl |= ( INTELXVF_SRRCTL_BSIZE_DEFAULT |
00299                     INTELXVF_SRRCTL_BHDRSIZE_DEFAULT |
00300                     INTELXVF_SRRCTL_DESCTYPE_DEFAULT |
00301                     INTELXVF_SRRCTL_DROP_EN );
00302         writel ( srrctl, intel->regs + INTELXVF_SRRCTL );
00303 
00304         /* Clear "must-be-zero" bit for direct cache access (DCA).  We
00305          * leave DCA disabled anyway, but if we do not clear this bit
00306          * then the received packets contain garbage data.
00307          */
00308         dca_rxctrl = readl ( intel->regs + INTELXVF_DCA_RXCTRL );
00309         dca_rxctrl &= ~INTELXVF_DCA_RXCTRL_MUST_BE_ZERO;
00310         writel ( dca_rxctrl, intel->regs + INTELXVF_DCA_RXCTRL );
00311 
00312         /* Fill receive ring */
00313         intel_refill_rx ( intel );
00314 
00315         /* Update link state */
00316         intelxvf_check_link ( netdev );
00317 
00318         return 0;
00319 
00320         intel_destroy_ring ( intel, &intel->rx );
00321  err_create_rx:
00322         intel_destroy_ring ( intel, &intel->tx );
00323  err_create_tx:
00324  err_mbox_set_mtu:
00325  err_mbox_set_mac:
00326  err_mbox_reset:
00327         intelxvf_reset ( intel );
00328         return rc;
00329 }
00330 
00331 /**
00332  * Close network device
00333  *
00334  * @v netdev            Network device
00335  */
00336 static void intelxvf_close ( struct net_device *netdev ) {
00337         struct intel_nic *intel = netdev->priv;
00338 
00339         /* Destroy receive descriptor ring */
00340         intel_destroy_ring ( intel, &intel->rx );
00341 
00342         /* Discard any unused receive buffers */
00343         intel_empty_rx ( intel );
00344 
00345         /* Destroy transmit descriptor ring */
00346         intel_destroy_ring ( intel, &intel->tx );
00347 
00348         /* Reset the function */
00349         intelxvf_reset ( intel );
00350 }
00351 
00352 /**
00353  * Poll for completed and received packets
00354  *
00355  * @v netdev            Network device
00356  */
00357 static void intelxvf_poll ( struct net_device *netdev ) {
00358         struct intel_nic *intel = netdev->priv;
00359         uint32_t eicr;
00360         int rc;
00361 
00362         /* Check for and acknowledge interrupts */
00363         eicr = readl ( intel->regs + INTELXVF_EICR );
00364         if ( ! eicr )
00365                 return;
00366 
00367         /* Poll for TX completions, if applicable */
00368         if ( eicr & INTELXVF_EIRQ_TX0 )
00369                 intel_poll_tx ( netdev );
00370 
00371         /* Poll for RX completions, if applicable */
00372         if ( eicr & INTELXVF_EIRQ_RX0 )
00373                 intel_poll_rx ( netdev );
00374 
00375         /* Poll for mailbox messages, if applicable */
00376         if ( eicr & INTELXVF_EIRQ_MBOX ) {
00377 
00378                 /* Poll mailbox */
00379                 if ( ( rc = intelvf_mbox_poll ( intel ) ) != 0 ) {
00380                         DBGC ( intel, "INTEL %p mailbox poll failed!\n",
00381                                intel );
00382                         netdev_rx_err ( netdev, NULL, rc );
00383                 }
00384 
00385                 /* Update link state */
00386                 intelxvf_check_link ( netdev );
00387         }
00388 
00389         /* Refill RX ring */
00390         intel_refill_rx ( intel );
00391 }
00392 
00393 /**
00394  * Enable or disable interrupts
00395  *
00396  * @v netdev            Network device
00397  * @v enable            Interrupts should be enabled
00398  */
00399 static void intelxvf_irq ( struct net_device *netdev, int enable ) {
00400         struct intel_nic *intel = netdev->priv;
00401         uint32_t mask;
00402 
00403         mask = ( INTELXVF_EIRQ_MBOX | INTELXVF_EIRQ_TX0 | INTELXVF_EIRQ_RX0 );
00404         if ( enable ) {
00405                 writel ( mask, intel->regs + INTELXVF_EIMS );
00406         } else {
00407                 writel ( mask, intel->regs + INTELXVF_EIMC );
00408         }
00409 }
00410 
00411 /** Network device operations */
00412 static struct net_device_operations intelxvf_operations = {
00413         .open           = intelxvf_open,
00414         .close          = intelxvf_close,
00415         .transmit       = intel_transmit,
00416         .poll           = intelxvf_poll,
00417         .irq            = intelxvf_irq,
00418 };
00419 
00420 /******************************************************************************
00421  *
00422  * PCI interface
00423  *
00424  ******************************************************************************
00425  */
00426 
00427 /**
00428  * Probe PCI device
00429  *
00430  * @v pci               PCI device
00431  * @ret rc              Return status code
00432  */
00433 static int intelxvf_probe ( struct pci_device *pci ) {
00434         struct net_device *netdev;
00435         struct intel_nic *intel;
00436         int rc;
00437 
00438         /* Allocate and initialise net device */
00439         netdev = alloc_etherdev ( sizeof ( *intel ) );
00440         if ( ! netdev ) {
00441                 rc = -ENOMEM;
00442                 goto err_alloc;
00443         }
00444         netdev_init ( netdev, &intelxvf_operations );
00445         intel = netdev->priv;
00446         pci_set_drvdata ( pci, netdev );
00447         netdev->dev = &pci->dev;
00448         memset ( intel, 0, sizeof ( *intel ) );
00449         intel_init_mbox ( &intel->mbox, INTELXVF_MBCTRL, INTELXVF_MBMEM );
00450         intel_init_ring ( &intel->tx, INTEL_NUM_TX_DESC, INTELXVF_TD(0),
00451                           intel_describe_tx_adv );
00452         intel_init_ring ( &intel->rx, INTEL_NUM_RX_DESC, INTELXVF_RD(0),
00453                           intel_describe_rx );
00454 
00455         /* Fix up PCI device */
00456         adjust_pci_device ( pci );
00457 
00458         /* Map registers */
00459         intel->regs = ioremap ( pci->membase, INTELVF_BAR_SIZE );
00460         if ( ! intel->regs ) {
00461                 rc = -ENODEV;
00462                 goto err_ioremap;
00463         }
00464 
00465         /* Reset the function */
00466         intelxvf_reset ( intel );
00467 
00468         /* Send reset message and fetch MAC address */
00469         if ( ( rc = intelvf_mbox_reset ( intel, netdev->hw_addr ) ) != 0 ) {
00470                 DBGC ( intel, "INTEL %p could not reset and fetch MAC: %s\n",
00471                        intel, strerror ( rc ) );
00472                 goto err_mbox_reset;
00473         }
00474 
00475         /* Reset the function (since we will not respond to Control
00476          * ("ping") mailbox messages until the network device is opened.
00477          */
00478         intelxvf_reset ( intel );
00479 
00480         /* Register network device */
00481         if ( ( rc = register_netdev ( netdev ) ) != 0 )
00482                 goto err_register_netdev;
00483 
00484         /* Set initial link state */
00485         intelxvf_check_link ( netdev );
00486 
00487         return 0;
00488 
00489         unregister_netdev ( netdev );
00490  err_register_netdev:
00491  err_mbox_reset:
00492         intelxvf_reset ( intel );
00493         iounmap ( intel->regs );
00494  err_ioremap:
00495         netdev_nullify ( netdev );
00496         netdev_put ( netdev );
00497  err_alloc:
00498         return rc;
00499 }
00500 
00501 /**
00502  * Remove PCI device
00503  *
00504  * @v pci               PCI device
00505  */
00506 static void intelxvf_remove ( struct pci_device *pci ) {
00507         struct net_device *netdev = pci_get_drvdata ( pci );
00508         struct intel_nic *intel = netdev->priv;
00509 
00510         /* Unregister network device */
00511         unregister_netdev ( netdev );
00512 
00513         /* Reset the NIC */
00514         intelxvf_reset ( intel );
00515 
00516         /* Free network device */
00517         iounmap ( intel->regs );
00518         netdev_nullify ( netdev );
00519         netdev_put ( netdev );
00520 }
00521 
00522 /** PCI device IDs */
00523 static struct pci_device_id intelxvf_nics[] = {
00524         PCI_ROM ( 0x8086, 0x10ed, "82599-vf", "82599 VF", 0 ),
00525         PCI_ROM ( 0x8086, 0x1515, "x540-vf", "X540 VF", 0 ),
00526         PCI_ROM ( 0x8086, 0x1565, "x550-vf", "X550 VF", 0 ),
00527         PCI_ROM ( 0x8086, 0x15a8, "x552-vf", "X552 VF", 0 ),
00528 };
00529 
00530 /** PCI driver */
00531 struct pci_driver intelxvf_driver __pci_driver = {
00532         .ids = intelxvf_nics,
00533         .id_count = ( sizeof ( intelxvf_nics ) / sizeof ( intelxvf_nics[0] ) ),
00534         .probe = intelxvf_probe,
00535         .remove = intelxvf_remove,
00536 };