]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ixl/if_ixl.c
Merge bmake-20161212
[FreeBSD/FreeBSD.git] / sys / dev / ixl / if_ixl.c
1 /******************************************************************************
2
3   Copyright (c) 2013-2015, Intel Corporation 
4   All rights reserved.
5   
6   Redistribution and use in source and binary forms, with or without 
7   modification, are permitted provided that the following conditions are met:
8   
9    1. Redistributions of source code must retain the above copyright notice, 
10       this list of conditions and the following disclaimer.
11   
12    2. Redistributions in binary form must reproduce the above copyright 
13       notice, this list of conditions and the following disclaimer in the 
14       documentation and/or other materials provided with the distribution.
15   
16    3. Neither the name of the Intel Corporation nor the names of its 
17       contributors may be used to endorse or promote products derived from 
18       this software without specific prior written permission.
19   
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31
32 ******************************************************************************/
33 /*$FreeBSD$*/
34
35 #include "ixl.h"
36 #include "ixl_pf.h"
37
38 #ifdef PCI_IOV
39 #include "ixl_pf_iov.h"
40 #endif
41
42 /*********************************************************************
43  *  Driver version
44  *********************************************************************/
45 char ixl_driver_version[] = "1.6.6-k";
46
47 /*********************************************************************
48  *  PCI Device ID Table
49  *
50  *  Used by probe to select devices to load on
51  *  Last field stores an index into ixl_strings
52  *  Last entry must be all 0s
53  *
54  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
55  *********************************************************************/
56
57 static ixl_vendor_info_t ixl_vendor_info_array[] =
58 {
59         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_XL710, 0, 0, 0},
60         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_B, 0, 0, 0},
61         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_C, 0, 0, 0},
62         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_A, 0, 0, 0},
63         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_B, 0, 0, 0},
64         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_C, 0, 0, 0},
65         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T, 0, 0, 0},
66         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T4, 0, 0, 0},
67         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_X722, 0, 0, 0},
68         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_X722, 0, 0, 0},
69         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_X722, 0, 0, 0},
70         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_1G_BASE_T_X722, 0, 0, 0},
71         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T_X722, 0, 0, 0},
72         {I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_I_X722, 0, 0, 0},
73         /* required last entry */
74         {0, 0, 0, 0, 0}
75 };
76
77 /*********************************************************************
78  *  Table of branding strings
79  *********************************************************************/
80
81 static char    *ixl_strings[] = {
82         "Intel(R) Ethernet Connection XL710/X722 Driver"
83 };
84
85
86 /*********************************************************************
87  *  Function prototypes
88  *********************************************************************/
89 static int      ixl_probe(device_t);
90 static int      ixl_attach(device_t);
91 static int      ixl_detach(device_t);
92 static int      ixl_shutdown(device_t);
93
94 static int      ixl_save_pf_tunables(struct ixl_pf *);
95 static int      ixl_attach_get_link_status(struct ixl_pf *);
96
97 /*********************************************************************
98  *  FreeBSD Device Interface Entry Points
99  *********************************************************************/
100
101 static device_method_t ixl_methods[] = {
102         /* Device interface */
103         DEVMETHOD(device_probe, ixl_probe),
104         DEVMETHOD(device_attach, ixl_attach),
105         DEVMETHOD(device_detach, ixl_detach),
106         DEVMETHOD(device_shutdown, ixl_shutdown),
107 #ifdef PCI_IOV
108         DEVMETHOD(pci_iov_init, ixl_iov_init),
109         DEVMETHOD(pci_iov_uninit, ixl_iov_uninit),
110         DEVMETHOD(pci_iov_add_vf, ixl_add_vf),
111 #endif
112         {0, 0}
113 };
114
115 static driver_t ixl_driver = {
116         "ixl", ixl_methods, sizeof(struct ixl_pf),
117 };
118
119 devclass_t ixl_devclass;
120 DRIVER_MODULE(ixl, pci, ixl_driver, ixl_devclass, 0, 0);
121
122 MODULE_DEPEND(ixl, pci, 1, 1, 1);
123 MODULE_DEPEND(ixl, ether, 1, 1, 1);
124 #ifdef DEV_NETMAP
125 MODULE_DEPEND(ixl, netmap, 1, 1, 1);
126 #endif /* DEV_NETMAP */
127
128 /*
129 ** TUNEABLE PARAMETERS:
130 */
131
132 static SYSCTL_NODE(_hw, OID_AUTO, ixl, CTLFLAG_RD, 0,
133                    "IXL driver parameters");
134
135 /*
136  * MSIX should be the default for best performance,
137  * but this allows it to be forced off for testing.
138  */
139 static int ixl_enable_msix = 1;
140 TUNABLE_INT("hw.ixl.enable_msix", &ixl_enable_msix);
141 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixl_enable_msix, 0,
142     "Enable MSI-X interrupts");
143
144 /*
145 ** Number of descriptors per ring:
146 **   - TX and RX are the same size
147 */
148 static int ixl_ring_size = DEFAULT_RING;
149 TUNABLE_INT("hw.ixl.ring_size", &ixl_ring_size);
150 SYSCTL_INT(_hw_ixl, OID_AUTO, ring_size, CTLFLAG_RDTUN,
151     &ixl_ring_size, 0, "Descriptor Ring Size");
152
153 /* 
154 ** This can be set manually, if left as 0 the
155 ** number of queues will be calculated based
156 ** on cpus and msix vectors available.
157 */
158 static int ixl_max_queues = 0;
159 TUNABLE_INT("hw.ixl.max_queues", &ixl_max_queues);
160 SYSCTL_INT(_hw_ixl, OID_AUTO, max_queues, CTLFLAG_RDTUN,
161     &ixl_max_queues, 0, "Number of Queues");
162
163 static int ixl_enable_tx_fc_filter = 1;
164 TUNABLE_INT("hw.ixl.enable_tx_fc_filter",
165     &ixl_enable_tx_fc_filter);
166 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_tx_fc_filter, CTLFLAG_RDTUN,
167     &ixl_enable_tx_fc_filter, 0,
168     "Filter out packets with Ethertype 0x8808 from being sent out by non-HW sources");
169
170 static int ixl_core_debug_mask = 0;
171 TUNABLE_INT("hw.ixl.core_debug_mask",
172     &ixl_core_debug_mask);
173 SYSCTL_INT(_hw_ixl, OID_AUTO, core_debug_mask, CTLFLAG_RDTUN,
174     &ixl_core_debug_mask, 0,
175     "Display debug statements that are printed in non-shared code");
176
177 static int ixl_shared_debug_mask = 0;
178 TUNABLE_INT("hw.ixl.shared_debug_mask",
179     &ixl_shared_debug_mask);
180 SYSCTL_INT(_hw_ixl, OID_AUTO, shared_debug_mask, CTLFLAG_RDTUN,
181     &ixl_shared_debug_mask, 0,
182     "Display debug statements that are printed in shared code");
183
184 /*
185 ** Controls for Interrupt Throttling 
186 **      - true/false for dynamic adjustment
187 **      - default values for static ITR
188 */
189 static int ixl_dynamic_rx_itr = 1;
190 TUNABLE_INT("hw.ixl.dynamic_rx_itr", &ixl_dynamic_rx_itr);
191 SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_rx_itr, CTLFLAG_RDTUN,
192     &ixl_dynamic_rx_itr, 0, "Dynamic RX Interrupt Rate");
193
194 static int ixl_dynamic_tx_itr = 1;
195 TUNABLE_INT("hw.ixl.dynamic_tx_itr", &ixl_dynamic_tx_itr);
196 SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_tx_itr, CTLFLAG_RDTUN,
197     &ixl_dynamic_tx_itr, 0, "Dynamic TX Interrupt Rate");
198
199 static int ixl_rx_itr = IXL_ITR_8K;
200 TUNABLE_INT("hw.ixl.rx_itr", &ixl_rx_itr);
201 SYSCTL_INT(_hw_ixl, OID_AUTO, rx_itr, CTLFLAG_RDTUN,
202     &ixl_rx_itr, 0, "RX Interrupt Rate");
203
204 static int ixl_tx_itr = IXL_ITR_4K;
205 TUNABLE_INT("hw.ixl.tx_itr", &ixl_tx_itr);
206 SYSCTL_INT(_hw_ixl, OID_AUTO, tx_itr, CTLFLAG_RDTUN,
207     &ixl_tx_itr, 0, "TX Interrupt Rate");
208
209 #ifdef DEV_NETMAP
210 #define NETMAP_IXL_MAIN /* only bring in one part of the netmap code */
211 #include <dev/netmap/if_ixl_netmap.h>
212 #endif /* DEV_NETMAP */
213
214 /*********************************************************************
215  *  Device identification routine
216  *
217  *  ixl_probe determines if the driver should be loaded on
218  *  the hardware based on PCI vendor/device id of the device.
219  *
220  *  return BUS_PROBE_DEFAULT on success, positive on failure
221  *********************************************************************/
222
223 static int
224 ixl_probe(device_t dev)
225 {
226         ixl_vendor_info_t *ent;
227
228         u16     pci_vendor_id, pci_device_id;
229         u16     pci_subvendor_id, pci_subdevice_id;
230         char    device_name[256];
231
232 #if 0
233         INIT_DEBUGOUT("ixl_probe: begin");
234 #endif
235         pci_vendor_id = pci_get_vendor(dev);
236         if (pci_vendor_id != I40E_INTEL_VENDOR_ID)
237                 return (ENXIO);
238
239         pci_device_id = pci_get_device(dev);
240         pci_subvendor_id = pci_get_subvendor(dev);
241         pci_subdevice_id = pci_get_subdevice(dev);
242
243         ent = ixl_vendor_info_array;
244         while (ent->vendor_id != 0) {
245                 if ((pci_vendor_id == ent->vendor_id) &&
246                     (pci_device_id == ent->device_id) &&
247
248                     ((pci_subvendor_id == ent->subvendor_id) ||
249                      (ent->subvendor_id == 0)) &&
250
251                     ((pci_subdevice_id == ent->subdevice_id) ||
252                      (ent->subdevice_id == 0))) {
253                         sprintf(device_name, "%s, Version - %s",
254                                 ixl_strings[ent->index],
255                                 ixl_driver_version);
256                         device_set_desc_copy(dev, device_name);
257                         return (BUS_PROBE_DEFAULT);
258                 }
259                 ent++;
260         }
261         return (ENXIO);
262 }
263
264 static int
265 ixl_attach_get_link_status(struct ixl_pf *pf)
266 {
267         struct i40e_hw *hw = &pf->hw;
268         device_t dev = pf->dev;
269         int error = 0;
270
271         if (((hw->aq.fw_maj_ver == 4) && (hw->aq.fw_min_ver < 33)) ||
272             (hw->aq.fw_maj_ver < 4)) {
273                 i40e_msec_delay(75);
274                 error = i40e_aq_set_link_restart_an(hw, TRUE, NULL);
275                 if (error) {
276                         device_printf(dev, "link restart failed, aq_err=%d\n",
277                             pf->hw.aq.asq_last_status);
278                         return error;
279                 }
280         }
281
282         /* Determine link state */
283         hw->phy.get_link_info = TRUE;
284         i40e_get_link_status(hw, &pf->link_up);
285         return (0);
286 }
287
288 /*
289  * Sanity check and save off tunable values.
290  */
291 static int
292 ixl_save_pf_tunables(struct ixl_pf *pf)
293 {
294         device_t dev = pf->dev;
295
296         /* Save tunable information */
297         pf->enable_msix = ixl_enable_msix;
298         pf->max_queues = ixl_max_queues;
299         pf->ringsz = ixl_ring_size;
300         pf->enable_tx_fc_filter = ixl_enable_tx_fc_filter;
301         pf->dynamic_rx_itr = ixl_dynamic_rx_itr;
302         pf->dynamic_tx_itr = ixl_dynamic_tx_itr;
303         pf->tx_itr = ixl_tx_itr;
304         pf->rx_itr = ixl_rx_itr;
305         pf->dbg_mask = ixl_core_debug_mask;
306         pf->hw.debug_mask = ixl_shared_debug_mask;
307
308         if (ixl_ring_size < IXL_MIN_RING
309              || ixl_ring_size > IXL_MAX_RING
310              || ixl_ring_size % IXL_RING_INCREMENT != 0) {
311                 device_printf(dev, "Invalid ring_size value of %d set!\n",
312                     ixl_ring_size);
313                 device_printf(dev, "ring_size must be between %d and %d, "
314                     "inclusive, and must be a multiple of %d\n",
315                     IXL_MIN_RING, IXL_MAX_RING, IXL_RING_INCREMENT);
316                 return (EINVAL);
317         }
318
319         return (0);
320 }
321
322 /*********************************************************************
323  *  Device initialization routine
324  *
325  *  The attach entry point is called when the driver is being loaded.
326  *  This routine identifies the type of hardware, allocates all resources
327  *  and initializes the hardware.
328  *
329  *  return 0 on success, positive on failure
330  *********************************************************************/
331
332 static int
333 ixl_attach(device_t dev)
334 {
335         struct ixl_pf   *pf;
336         struct i40e_hw  *hw;
337         struct ixl_vsi  *vsi;
338         enum i40e_status_code status;
339         int             error = 0;
340
341         INIT_DEBUGOUT("ixl_attach: begin");
342
343         /* Allocate, clear, and link in our primary soft structure */
344         pf = device_get_softc(dev);
345         pf->dev = pf->osdep.dev = dev;
346         hw = &pf->hw;
347
348         /*
349         ** Note this assumes we have a single embedded VSI,
350         ** this could be enhanced later to allocate multiple
351         */
352         vsi = &pf->vsi;
353         vsi->dev = pf->dev;
354
355         /* Save tunable values */
356         error = ixl_save_pf_tunables(pf);
357         if (error)
358                 return (error);
359
360         /* Core Lock Init*/
361         IXL_PF_LOCK_INIT(pf, device_get_nameunit(dev));
362
363         /* Set up the timer callout */
364         callout_init_mtx(&pf->timer, &pf->pf_mtx, 0);
365
366         /* Do PCI setup - map BAR0, etc */
367         if (ixl_allocate_pci_resources(pf)) {
368                 device_printf(dev, "Allocation of PCI resources failed\n");
369                 error = ENXIO;
370                 goto err_out;
371         }
372
373         /* Establish a clean starting point */
374         i40e_clear_hw(hw);
375         status = i40e_pf_reset(hw);
376         if (status) {
377                 device_printf(dev, "PF reset failure %s\n",
378                     i40e_stat_str(hw, status));
379                 error = EIO;
380                 goto err_out;
381         }
382
383         /* Initialize the shared code */
384         status = i40e_init_shared_code(hw);
385         if (status) {
386                 device_printf(dev, "Unable to initialize shared code, error %s\n",
387                     i40e_stat_str(hw, status));
388                 error = EIO;
389                 goto err_out;
390         }
391
392         /*
393          * Allocate interrupts and figure out number of queues to use
394          * for PF interface
395          */
396         pf->msix = ixl_init_msix(pf);
397
398         /* Set up the admin queue */
399         hw->aq.num_arq_entries = IXL_AQ_LEN;
400         hw->aq.num_asq_entries = IXL_AQ_LEN;
401         hw->aq.arq_buf_size = IXL_AQ_BUF_SZ;
402         hw->aq.asq_buf_size = IXL_AQ_BUF_SZ;
403
404         status = i40e_init_adminq(hw);
405         if (status != 0 && status != I40E_ERR_FIRMWARE_API_VERSION) {
406                 device_printf(dev, "Unable to initialize Admin Queue, error %s\n",
407                     i40e_stat_str(hw, status));
408                 error = EIO;
409                 goto err_out;
410         }
411         ixl_print_nvm_version(pf);
412
413         if (status == I40E_ERR_FIRMWARE_API_VERSION) {
414                 device_printf(dev, "The driver for the device stopped "
415                     "because the NVM image is newer than expected.\n"
416                     "You must install the most recent version of "
417                     "the network driver.\n");
418                 error = EIO;
419                 goto err_out;
420         }
421
422         if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
423             hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
424                 device_printf(dev, "The driver for the device detected "
425                     "a newer version of the NVM image than expected.\n"
426                     "Please install the most recent version of the network driver.\n");
427         else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
428             hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
429                 device_printf(dev, "The driver for the device detected "
430                     "an older version of the NVM image than expected.\n"
431                     "Please update the NVM image.\n");
432
433         /* Clear PXE mode */
434         i40e_clear_pxe_mode(hw);
435
436         /* Get capabilities from the device */
437         error = ixl_get_hw_capabilities(pf);
438         if (error) {
439                 device_printf(dev, "HW capabilities failure!\n");
440                 goto err_get_cap;
441         }
442
443         /* Set up host memory cache */
444         status = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
445             hw->func_caps.num_rx_qp, 0, 0);
446         if (status) {
447                 device_printf(dev, "init_lan_hmc failed: %s\n",
448                     i40e_stat_str(hw, status));
449                 goto err_get_cap;
450         }
451
452         status = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
453         if (status) {
454                 device_printf(dev, "configure_lan_hmc failed: %s\n",
455                     i40e_stat_str(hw, status));
456                 goto err_mac_hmc;
457         }
458
459         /* Init queue allocation manager */
460         error = ixl_pf_qmgr_init(&pf->qmgr, hw->func_caps.num_tx_qp);
461         if (error) {
462                 device_printf(dev, "Failed to init queue manager for PF queues, error %d\n",
463                     error);
464                 goto err_mac_hmc;
465         }
466         /* reserve a contiguous allocation for the PF's VSI */
467         error = ixl_pf_qmgr_alloc_contiguous(&pf->qmgr, vsi->num_queues, &pf->qtag);
468         if (error) {
469                 device_printf(dev, "Failed to reserve queues for PF LAN VSI, error %d\n",
470                     error);
471                 goto err_mac_hmc;
472         }
473         device_printf(dev, "Allocating %d queues for PF LAN VSI; %d queues active\n",
474             pf->qtag.num_allocated, pf->qtag.num_active);
475
476         /* Disable LLDP from the firmware for certain NVM versions */
477         if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
478             (pf->hw.aq.fw_maj_ver < 4))
479                 i40e_aq_stop_lldp(hw, TRUE, NULL);
480
481         /* Get MAC addresses from hardware */
482         i40e_get_mac_addr(hw, hw->mac.addr);
483         error = i40e_validate_mac_addr(hw->mac.addr);
484         if (error) {
485                 device_printf(dev, "validate_mac_addr failed: %d\n", error);
486                 goto err_mac_hmc;
487         }
488         bcopy(hw->mac.addr, hw->mac.perm_addr, ETHER_ADDR_LEN);
489         i40e_get_port_mac_addr(hw, hw->mac.port_addr);
490
491         /* Initialize mac filter list for VSI */
492         SLIST_INIT(&vsi->ftl);
493
494         /* Set up SW VSI and allocate queue memory and rings */
495         if (ixl_setup_stations(pf)) { 
496                 device_printf(dev, "setup stations failed!\n");
497                 error = ENOMEM;
498                 goto err_mac_hmc;
499         }
500
501         /* Setup OS network interface / ifnet */
502         if (ixl_setup_interface(dev, vsi)) {
503                 device_printf(dev, "interface setup failed!\n");
504                 error = EIO;
505                 goto err_late;
506         }
507
508         /* Determine link state */
509         if (ixl_attach_get_link_status(pf)) {
510                 error = EINVAL;
511                 goto err_late;
512         }
513
514         error = ixl_switch_config(pf);
515         if (error) {
516                 device_printf(dev, "Initial ixl_switch_config() failed: %d\n",
517                      error);
518                 goto err_late;
519         }
520
521         /* Limit PHY interrupts to link, autoneg, and modules failure */
522         status = i40e_aq_set_phy_int_mask(hw, IXL_DEFAULT_PHY_INT_MASK,
523             NULL);
524         if (status) {
525                 device_printf(dev, "i40e_aq_set_phy_mask() failed: err %s,"
526                     " aq_err %s\n", i40e_stat_str(hw, status),
527                     i40e_aq_str(hw, hw->aq.asq_last_status));
528                 goto err_late;
529         }
530
531         /* Get the bus configuration and set the shared code's config */
532         ixl_get_bus_info(hw, dev);
533
534         /*
535          * In MSI-X mode, initialize the Admin Queue interrupt,
536          * so userland tools can communicate with the adapter regardless of
537          * the ifnet interface's status.
538          */
539         if (pf->msix > 1) {
540                 error = ixl_setup_adminq_msix(pf);
541                 if (error) {
542                         device_printf(dev, "ixl_setup_adminq_msix error: %d\n",
543                             error);
544                         goto err_late;
545                 }
546                 error = ixl_setup_adminq_tq(pf);
547                 if (error) {
548                         device_printf(dev, "ixl_setup_adminq_tq error: %d\n",
549                             error);
550                         goto err_late;
551                 }
552                 ixl_configure_intr0_msix(pf);
553                 ixl_enable_adminq(hw);
554         }
555
556         /* Initialize statistics & add sysctls */
557         ixl_add_device_sysctls(pf);
558
559         ixl_pf_reset_stats(pf);
560         ixl_update_stats_counters(pf);
561         ixl_add_hw_stats(pf);
562
563         /* Register for VLAN events */
564         vsi->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
565             ixl_register_vlan, vsi, EVENTHANDLER_PRI_FIRST);
566         vsi->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
567             ixl_unregister_vlan, vsi, EVENTHANDLER_PRI_FIRST);
568
569 #ifdef PCI_IOV
570         ixl_initialize_sriov(pf);
571 #endif
572
573 #ifdef DEV_NETMAP
574         ixl_netmap_attach(vsi);
575 #endif /* DEV_NETMAP */
576         INIT_DEBUGOUT("ixl_attach: end");
577         return (0);
578
579 err_late:
580         if (vsi->ifp != NULL) {
581                 ether_ifdetach(vsi->ifp);
582                 if_free(vsi->ifp);
583         }
584 err_mac_hmc:
585         i40e_shutdown_lan_hmc(hw);
586 err_get_cap:
587         i40e_shutdown_adminq(hw);
588 err_out:
589         ixl_free_pci_resources(pf);
590         ixl_free_vsi(vsi);
591         IXL_PF_LOCK_DESTROY(pf);
592         return (error);
593 }
594
595 /*********************************************************************
596  *  Device removal routine
597  *
598  *  The detach entry point is called when the driver is being removed.
599  *  This routine stops the adapter and deallocates all the resources
600  *  that were allocated for driver operation.
601  *
602  *  return 0 on success, positive on failure
603  *********************************************************************/
604
605 static int
606 ixl_detach(device_t dev)
607 {
608         struct ixl_pf           *pf = device_get_softc(dev);
609         struct i40e_hw          *hw = &pf->hw;
610         struct ixl_vsi          *vsi = &pf->vsi;
611         enum i40e_status_code   status;
612 #ifdef PCI_IOV
613         int                     error;
614 #endif
615
616         INIT_DEBUGOUT("ixl_detach: begin");
617
618         /* Make sure VLANS are not using driver */
619         if (vsi->ifp->if_vlantrunk != NULL) {
620                 device_printf(dev, "Vlan in use, detach first\n");
621                 return (EBUSY);
622         }
623
624 #ifdef PCI_IOV
625         error = pci_iov_detach(dev);
626         if (error != 0) {
627                 device_printf(dev, "SR-IOV in use; detach first.\n");
628                 return (error);
629         }
630 #endif
631
632         ether_ifdetach(vsi->ifp);
633         if (vsi->ifp->if_drv_flags & IFF_DRV_RUNNING)
634                 ixl_stop(pf);
635
636         ixl_free_queue_tqs(vsi);
637
638         /* Shutdown LAN HMC */
639         status = i40e_shutdown_lan_hmc(hw);
640         if (status)
641                 device_printf(dev,
642                     "Shutdown LAN HMC failed with code %d\n", status);
643
644         /* Shutdown admin queue */
645         ixl_disable_adminq(hw);
646         ixl_free_adminq_tq(pf);
647         ixl_teardown_adminq_msix(pf);
648         status = i40e_shutdown_adminq(hw);
649         if (status)
650                 device_printf(dev,
651                     "Shutdown Admin queue failed with code %d\n", status);
652
653         /* Unregister VLAN events */
654         if (vsi->vlan_attach != NULL)
655                 EVENTHANDLER_DEREGISTER(vlan_config, vsi->vlan_attach);
656         if (vsi->vlan_detach != NULL)
657                 EVENTHANDLER_DEREGISTER(vlan_unconfig, vsi->vlan_detach);
658
659         callout_drain(&pf->timer);
660 #ifdef DEV_NETMAP
661         netmap_detach(vsi->ifp);
662 #endif /* DEV_NETMAP */
663         ixl_pf_qmgr_destroy(&pf->qmgr);
664         ixl_free_pci_resources(pf);
665         bus_generic_detach(dev);
666         if_free(vsi->ifp);
667         ixl_free_vsi(vsi);
668         IXL_PF_LOCK_DESTROY(pf);
669         return (0);
670 }
671
672 /*********************************************************************
673  *
674  *  Shutdown entry point
675  *
676  **********************************************************************/
677
678 static int
679 ixl_shutdown(device_t dev)
680 {
681         struct ixl_pf *pf = device_get_softc(dev);
682         ixl_stop(pf);
683         return (0);
684 }
685