xref: /freebsd/sys/dev/ixl/if_ixl.c (revision 076ad2f8)
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 IXL_IW
39 #include "ixl_iw.h"
40 #include "ixl_iw_int.h"
41 #endif
42 
43 #ifdef PCI_IOV
44 #include "ixl_pf_iov.h"
45 #endif
46 
47 /*********************************************************************
48  *  Driver version
49  *********************************************************************/
50 char ixl_driver_version[] = "1.7.12-k";
51 
52 /*********************************************************************
53  *  PCI Device ID Table
54  *
55  *  Used by probe to select devices to load on
56  *  Last field stores an index into ixl_strings
57  *  Last entry must be all 0s
58  *
59  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
60  *********************************************************************/
61 
62 static ixl_vendor_info_t ixl_vendor_info_array[] =
63 {
64 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_XL710, 0, 0, 0},
65 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_B, 0, 0, 0},
66 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_C, 0, 0, 0},
67 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_A, 0, 0, 0},
68 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_B, 0, 0, 0},
69 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_C, 0, 0, 0},
70 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T, 0, 0, 0},
71 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T4, 0, 0, 0},
72 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_X722, 0, 0, 0},
73 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_X722, 0, 0, 0},
74 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_X722, 0, 0, 0},
75 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_1G_BASE_T_X722, 0, 0, 0},
76 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T_X722, 0, 0, 0},
77 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_I_X722, 0, 0, 0},
78 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_25G_B, 0, 0, 0},
79 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_25G_SFP28, 0, 0, 0},
80 	/* required last entry */
81 	{0, 0, 0, 0, 0}
82 };
83 
84 /*********************************************************************
85  *  Table of branding strings
86  *********************************************************************/
87 
88 static char    *ixl_strings[] = {
89 	"Intel(R) Ethernet Connection XL710/X722 Driver"
90 };
91 
92 
93 /*********************************************************************
94  *  Function prototypes
95  *********************************************************************/
96 static int      ixl_probe(device_t);
97 static int      ixl_attach(device_t);
98 static int      ixl_detach(device_t);
99 static int      ixl_shutdown(device_t);
100 
101 static int	ixl_save_pf_tunables(struct ixl_pf *);
102 static int	ixl_attach_get_link_status(struct ixl_pf *);
103 
104 /*********************************************************************
105  *  FreeBSD Device Interface Entry Points
106  *********************************************************************/
107 
108 static device_method_t ixl_methods[] = {
109 	/* Device interface */
110 	DEVMETHOD(device_probe, ixl_probe),
111 	DEVMETHOD(device_attach, ixl_attach),
112 	DEVMETHOD(device_detach, ixl_detach),
113 	DEVMETHOD(device_shutdown, ixl_shutdown),
114 #ifdef PCI_IOV
115 	DEVMETHOD(pci_iov_init, ixl_iov_init),
116 	DEVMETHOD(pci_iov_uninit, ixl_iov_uninit),
117 	DEVMETHOD(pci_iov_add_vf, ixl_add_vf),
118 #endif
119 	{0, 0}
120 };
121 
122 static driver_t ixl_driver = {
123 	"ixl", ixl_methods, sizeof(struct ixl_pf),
124 };
125 
126 devclass_t ixl_devclass;
127 DRIVER_MODULE(ixl, pci, ixl_driver, ixl_devclass, 0, 0);
128 
129 MODULE_VERSION(ixl, 1);
130 
131 MODULE_DEPEND(ixl, pci, 1, 1, 1);
132 MODULE_DEPEND(ixl, ether, 1, 1, 1);
133 #if defined(DEV_NETMAP) && __FreeBSD_version >= 1100000
134 MODULE_DEPEND(ixl, netmap, 1, 1, 1);
135 #endif /* DEV_NETMAP */
136 
137 /*
138 ** TUNEABLE PARAMETERS:
139 */
140 
141 static SYSCTL_NODE(_hw, OID_AUTO, ixl, CTLFLAG_RD, 0,
142                    "IXL driver parameters");
143 
144 /*
145  * MSIX should be the default for best performance,
146  * but this allows it to be forced off for testing.
147  */
148 static int ixl_enable_msix = 1;
149 TUNABLE_INT("hw.ixl.enable_msix", &ixl_enable_msix);
150 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixl_enable_msix, 0,
151     "Enable MSI-X interrupts");
152 
153 /*
154 ** Number of descriptors per ring:
155 **   - TX and RX are the same size
156 */
157 static int ixl_ring_size = IXL_DEFAULT_RING;
158 TUNABLE_INT("hw.ixl.ring_size", &ixl_ring_size);
159 SYSCTL_INT(_hw_ixl, OID_AUTO, ring_size, CTLFLAG_RDTUN,
160     &ixl_ring_size, 0, "Descriptor Ring Size");
161 
162 /*
163 ** This can be set manually, if left as 0 the
164 ** number of queues will be calculated based
165 ** on cpus and msix vectors available.
166 */
167 static int ixl_max_queues = 0;
168 TUNABLE_INT("hw.ixl.max_queues", &ixl_max_queues);
169 SYSCTL_INT(_hw_ixl, OID_AUTO, max_queues, CTLFLAG_RDTUN,
170     &ixl_max_queues, 0, "Number of Queues");
171 
172 static int ixl_enable_tx_fc_filter = 1;
173 TUNABLE_INT("hw.ixl.enable_tx_fc_filter",
174     &ixl_enable_tx_fc_filter);
175 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_tx_fc_filter, CTLFLAG_RDTUN,
176     &ixl_enable_tx_fc_filter, 0,
177     "Filter out packets with Ethertype 0x8808 from being sent out by non-HW sources");
178 
179 static int ixl_core_debug_mask = 0;
180 TUNABLE_INT("hw.ixl.core_debug_mask",
181     &ixl_core_debug_mask);
182 SYSCTL_INT(_hw_ixl, OID_AUTO, core_debug_mask, CTLFLAG_RDTUN,
183     &ixl_core_debug_mask, 0,
184     "Display debug statements that are printed in non-shared code");
185 
186 static int ixl_shared_debug_mask = 0;
187 TUNABLE_INT("hw.ixl.shared_debug_mask",
188     &ixl_shared_debug_mask);
189 SYSCTL_INT(_hw_ixl, OID_AUTO, shared_debug_mask, CTLFLAG_RDTUN,
190     &ixl_shared_debug_mask, 0,
191     "Display debug statements that are printed in shared code");
192 
193 /*
194 ** Controls for Interrupt Throttling
195 **	- true/false for dynamic adjustment
196 ** 	- default values for static ITR
197 */
198 static int ixl_dynamic_rx_itr = 1;
199 TUNABLE_INT("hw.ixl.dynamic_rx_itr", &ixl_dynamic_rx_itr);
200 SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_rx_itr, CTLFLAG_RDTUN,
201     &ixl_dynamic_rx_itr, 0, "Dynamic RX Interrupt Rate");
202 
203 static int ixl_dynamic_tx_itr = 1;
204 TUNABLE_INT("hw.ixl.dynamic_tx_itr", &ixl_dynamic_tx_itr);
205 SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_tx_itr, CTLFLAG_RDTUN,
206     &ixl_dynamic_tx_itr, 0, "Dynamic TX Interrupt Rate");
207 
208 static int ixl_rx_itr = IXL_ITR_8K;
209 TUNABLE_INT("hw.ixl.rx_itr", &ixl_rx_itr);
210 SYSCTL_INT(_hw_ixl, OID_AUTO, rx_itr, CTLFLAG_RDTUN,
211     &ixl_rx_itr, 0, "RX Interrupt Rate");
212 
213 static int ixl_tx_itr = IXL_ITR_4K;
214 TUNABLE_INT("hw.ixl.tx_itr", &ixl_tx_itr);
215 SYSCTL_INT(_hw_ixl, OID_AUTO, tx_itr, CTLFLAG_RDTUN,
216     &ixl_tx_itr, 0, "TX Interrupt Rate");
217 
218 #ifdef IXL_IW
219 int ixl_enable_iwarp = 0;
220 TUNABLE_INT("hw.ixl.enable_iwarp", &ixl_enable_iwarp);
221 #endif
222 
223 #ifdef DEV_NETMAP
224 #define NETMAP_IXL_MAIN /* only bring in one part of the netmap code */
225 #include <dev/netmap/if_ixl_netmap.h>
226 #endif /* DEV_NETMAP */
227 
228 /*********************************************************************
229  *  Device identification routine
230  *
231  *  ixl_probe determines if the driver should be loaded on
232  *  the hardware based on PCI vendor/device id of the device.
233  *
234  *  return BUS_PROBE_DEFAULT on success, positive on failure
235  *********************************************************************/
236 
237 static int
238 ixl_probe(device_t dev)
239 {
240 	ixl_vendor_info_t *ent;
241 
242 	u16	pci_vendor_id, pci_device_id;
243 	u16	pci_subvendor_id, pci_subdevice_id;
244 	char	device_name[256];
245 
246 #if 0
247 	INIT_DEBUGOUT("ixl_probe: begin");
248 #endif
249 	pci_vendor_id = pci_get_vendor(dev);
250 	if (pci_vendor_id != I40E_INTEL_VENDOR_ID)
251 		return (ENXIO);
252 
253 	pci_device_id = pci_get_device(dev);
254 	pci_subvendor_id = pci_get_subvendor(dev);
255 	pci_subdevice_id = pci_get_subdevice(dev);
256 
257 	ent = ixl_vendor_info_array;
258 	while (ent->vendor_id != 0) {
259 		if ((pci_vendor_id == ent->vendor_id) &&
260 		    (pci_device_id == ent->device_id) &&
261 
262 		    ((pci_subvendor_id == ent->subvendor_id) ||
263 		     (ent->subvendor_id == 0)) &&
264 
265 		    ((pci_subdevice_id == ent->subdevice_id) ||
266 		     (ent->subdevice_id == 0))) {
267 			sprintf(device_name, "%s, Version - %s",
268 				ixl_strings[ent->index],
269 				ixl_driver_version);
270 			device_set_desc_copy(dev, device_name);
271 			return (BUS_PROBE_DEFAULT);
272 		}
273 		ent++;
274 	}
275 	return (ENXIO);
276 }
277 
278 static int
279 ixl_attach_get_link_status(struct ixl_pf *pf)
280 {
281 	struct i40e_hw *hw = &pf->hw;
282 	device_t dev = pf->dev;
283 	int error = 0;
284 
285 	if (((hw->aq.fw_maj_ver == 4) && (hw->aq.fw_min_ver < 33)) ||
286 	    (hw->aq.fw_maj_ver < 4)) {
287 		i40e_msec_delay(75);
288 		error = i40e_aq_set_link_restart_an(hw, TRUE, NULL);
289 		if (error) {
290 			device_printf(dev, "link restart failed, aq_err=%d\n",
291 			    pf->hw.aq.asq_last_status);
292 			return error;
293 		}
294 	}
295 
296 	/* Determine link state */
297 	hw->phy.get_link_info = TRUE;
298 	i40e_get_link_status(hw, &pf->link_up);
299 	return (0);
300 }
301 
302 /*
303  * Sanity check and save off tunable values.
304  */
305 static int
306 ixl_save_pf_tunables(struct ixl_pf *pf)
307 {
308 	device_t dev = pf->dev;
309 
310 	/* Save tunable information */
311 	pf->enable_msix = ixl_enable_msix;
312 	pf->max_queues = ixl_max_queues;
313 	pf->enable_tx_fc_filter = ixl_enable_tx_fc_filter;
314 	pf->dynamic_rx_itr = ixl_dynamic_rx_itr;
315 	pf->dynamic_tx_itr = ixl_dynamic_tx_itr;
316 	pf->dbg_mask = ixl_core_debug_mask;
317 	pf->hw.debug_mask = ixl_shared_debug_mask;
318 
319 	if (ixl_ring_size < IXL_MIN_RING
320 	     || ixl_ring_size > IXL_MAX_RING
321 	     || ixl_ring_size % IXL_RING_INCREMENT != 0) {
322 		device_printf(dev, "Invalid ring_size value of %d set!\n",
323 		    ixl_ring_size);
324 		device_printf(dev, "ring_size must be between %d and %d, "
325 		    "inclusive, and must be a multiple of %d\n",
326 		    IXL_MIN_RING, IXL_MAX_RING, IXL_RING_INCREMENT);
327 		device_printf(dev, "Using default value of %d instead\n",
328 		    IXL_DEFAULT_RING);
329 		pf->ringsz = IXL_DEFAULT_RING;
330 	} else
331 		pf->ringsz = ixl_ring_size;
332 
333 	if (ixl_tx_itr < 0 || ixl_tx_itr > IXL_MAX_ITR) {
334 		device_printf(dev, "Invalid tx_itr value of %d set!\n",
335 		    ixl_tx_itr);
336 		device_printf(dev, "tx_itr must be between %d and %d, "
337 		    "inclusive\n",
338 		    0, IXL_MAX_ITR);
339 		device_printf(dev, "Using default value of %d instead\n",
340 		    IXL_ITR_4K);
341 		pf->tx_itr = IXL_ITR_4K;
342 	} else
343 		pf->tx_itr = ixl_tx_itr;
344 
345 	if (ixl_rx_itr < 0 || ixl_rx_itr > IXL_MAX_ITR) {
346 		device_printf(dev, "Invalid rx_itr value of %d set!\n",
347 		    ixl_rx_itr);
348 		device_printf(dev, "rx_itr must be between %d and %d, "
349 		    "inclusive\n",
350 		    0, IXL_MAX_ITR);
351 		device_printf(dev, "Using default value of %d instead\n",
352 		    IXL_ITR_8K);
353 		pf->rx_itr = IXL_ITR_8K;
354 	} else
355 		pf->rx_itr = ixl_rx_itr;
356 
357 	return (0);
358 }
359 
360 /*********************************************************************
361  *  Device initialization routine
362  *
363  *  The attach entry point is called when the driver is being loaded.
364  *  This routine identifies the type of hardware, allocates all resources
365  *  and initializes the hardware.
366  *
367  *  return 0 on success, positive on failure
368  *********************************************************************/
369 
370 static int
371 ixl_attach(device_t dev)
372 {
373 	struct ixl_pf	*pf;
374 	struct i40e_hw	*hw;
375 	struct ixl_vsi  *vsi;
376 	enum i40e_status_code status;
377 	int             error = 0;
378 
379 	INIT_DEBUGOUT("ixl_attach: begin");
380 
381 	/* Allocate, clear, and link in our primary soft structure */
382 	pf = device_get_softc(dev);
383 	pf->dev = pf->osdep.dev = dev;
384 	hw = &pf->hw;
385 
386 	/*
387 	** Note this assumes we have a single embedded VSI,
388 	** this could be enhanced later to allocate multiple
389 	*/
390 	vsi = &pf->vsi;
391 	vsi->dev = pf->dev;
392 
393 	/* Save tunable values */
394 	error = ixl_save_pf_tunables(pf);
395 	if (error)
396 		return (error);
397 
398 	/* Core Lock Init*/
399 	IXL_PF_LOCK_INIT(pf, device_get_nameunit(dev));
400 
401 	/* Set up the timer callout */
402 	callout_init_mtx(&pf->timer, &pf->pf_mtx, 0);
403 
404 	/* Do PCI setup - map BAR0, etc */
405 	if (ixl_allocate_pci_resources(pf)) {
406 		device_printf(dev, "Allocation of PCI resources failed\n");
407 		error = ENXIO;
408 		goto err_out;
409 	}
410 
411 	/* Establish a clean starting point */
412 	i40e_clear_hw(hw);
413 	status = i40e_pf_reset(hw);
414 	if (status) {
415 		device_printf(dev, "PF reset failure %s\n",
416 		    i40e_stat_str(hw, status));
417 		error = EIO;
418 		goto err_out;
419 	}
420 
421 	/* Initialize the shared code */
422 	status = i40e_init_shared_code(hw);
423 	if (status) {
424 		device_printf(dev, "Unable to initialize shared code, error %s\n",
425 		    i40e_stat_str(hw, status));
426 		error = EIO;
427 		goto err_out;
428 	}
429 
430 	/*
431 	 * Allocate interrupts and figure out number of queues to use
432 	 * for PF interface
433 	 */
434 	pf->msix = ixl_init_msix(pf);
435 
436 	/* Set up the admin queue */
437 	hw->aq.num_arq_entries = IXL_AQ_LEN;
438 	hw->aq.num_asq_entries = IXL_AQ_LEN;
439 	hw->aq.arq_buf_size = IXL_AQ_BUF_SZ;
440 	hw->aq.asq_buf_size = IXL_AQ_BUF_SZ;
441 
442 	status = i40e_init_adminq(hw);
443 	if (status != 0 && status != I40E_ERR_FIRMWARE_API_VERSION) {
444 		device_printf(dev, "Unable to initialize Admin Queue, error %s\n",
445 		    i40e_stat_str(hw, status));
446 		error = EIO;
447 		goto err_out;
448 	}
449 	ixl_print_nvm_version(pf);
450 
451 	if (status == I40E_ERR_FIRMWARE_API_VERSION) {
452 		device_printf(dev, "The driver for the device stopped "
453 		    "because the NVM image is newer than expected.\n"
454 		    "You must install the most recent version of "
455 		    "the network driver.\n");
456 		error = EIO;
457 		goto err_out;
458 	}
459 
460         if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
461 	    hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
462 		device_printf(dev, "The driver for the device detected "
463 		    "a newer version of the NVM image than expected.\n"
464 		    "Please install the most recent version of the network driver.\n");
465 	else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
466 	    hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
467 		device_printf(dev, "The driver for the device detected "
468 		    "an older version of the NVM image than expected.\n"
469 		    "Please update the NVM image.\n");
470 
471 	/* Clear PXE mode */
472 	i40e_clear_pxe_mode(hw);
473 
474 	/* Get capabilities from the device */
475 	error = ixl_get_hw_capabilities(pf);
476 	if (error) {
477 		device_printf(dev, "HW capabilities failure!\n");
478 		goto err_get_cap;
479 	}
480 
481 	/* Set up host memory cache */
482 	status = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
483 	    hw->func_caps.num_rx_qp, 0, 0);
484 	if (status) {
485 		device_printf(dev, "init_lan_hmc failed: %s\n",
486 		    i40e_stat_str(hw, status));
487 		goto err_get_cap;
488 	}
489 
490 	status = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
491 	if (status) {
492 		device_printf(dev, "configure_lan_hmc failed: %s\n",
493 		    i40e_stat_str(hw, status));
494 		goto err_mac_hmc;
495 	}
496 
497 	/* Init queue allocation manager */
498 	error = ixl_pf_qmgr_init(&pf->qmgr, hw->func_caps.num_tx_qp);
499 	if (error) {
500 		device_printf(dev, "Failed to init queue manager for PF queues, error %d\n",
501 		    error);
502 		goto err_mac_hmc;
503 	}
504 	/* reserve a contiguous allocation for the PF's VSI */
505 	error = ixl_pf_qmgr_alloc_contiguous(&pf->qmgr, vsi->num_queues, &pf->qtag);
506 	if (error) {
507 		device_printf(dev, "Failed to reserve queues for PF LAN VSI, error %d\n",
508 		    error);
509 		goto err_mac_hmc;
510 	}
511 	device_printf(dev, "Allocating %d queues for PF LAN VSI; %d queues active\n",
512 	    pf->qtag.num_allocated, pf->qtag.num_active);
513 
514 	/* Disable LLDP from the firmware for certain NVM versions */
515 	if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
516 	    (pf->hw.aq.fw_maj_ver < 4))
517 		i40e_aq_stop_lldp(hw, TRUE, NULL);
518 
519 	/* Get MAC addresses from hardware */
520 	i40e_get_mac_addr(hw, hw->mac.addr);
521 	error = i40e_validate_mac_addr(hw->mac.addr);
522 	if (error) {
523 		device_printf(dev, "validate_mac_addr failed: %d\n", error);
524 		goto err_mac_hmc;
525 	}
526 	bcopy(hw->mac.addr, hw->mac.perm_addr, ETHER_ADDR_LEN);
527 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
528 
529 	/* Initialize mac filter list for VSI */
530 	SLIST_INIT(&vsi->ftl);
531 
532 	/* Set up SW VSI and allocate queue memory and rings */
533 	if (ixl_setup_stations(pf)) {
534 		device_printf(dev, "setup stations failed!\n");
535 		error = ENOMEM;
536 		goto err_mac_hmc;
537 	}
538 
539 	/* Setup OS network interface / ifnet */
540 	if (ixl_setup_interface(dev, vsi)) {
541 		device_printf(dev, "interface setup failed!\n");
542 		error = EIO;
543 		goto err_late;
544 	}
545 
546 	/* Determine link state */
547 	if (ixl_attach_get_link_status(pf)) {
548 		error = EINVAL;
549 		goto err_late;
550 	}
551 
552 	error = ixl_switch_config(pf);
553 	if (error) {
554 		device_printf(dev, "Initial ixl_switch_config() failed: %d\n",
555 		     error);
556 		goto err_late;
557 	}
558 
559 	/* Limit PHY interrupts to link, autoneg, and modules failure */
560 	status = i40e_aq_set_phy_int_mask(hw, IXL_DEFAULT_PHY_INT_MASK,
561 	    NULL);
562         if (status) {
563 		device_printf(dev, "i40e_aq_set_phy_mask() failed: err %s,"
564 		    " aq_err %s\n", i40e_stat_str(hw, status),
565 		    i40e_aq_str(hw, hw->aq.asq_last_status));
566 		goto err_late;
567 	}
568 
569 	/* Get the bus configuration and set the shared code's config */
570 	ixl_get_bus_info(pf);
571 
572 	/*
573 	 * In MSI-X mode, initialize the Admin Queue interrupt,
574 	 * so userland tools can communicate with the adapter regardless of
575 	 * the ifnet interface's status.
576 	 */
577 	if (pf->msix > 1) {
578 		error = ixl_setup_adminq_msix(pf);
579 		if (error) {
580 			device_printf(dev, "ixl_setup_adminq_msix() error: %d\n",
581 			    error);
582 			goto err_late;
583 		}
584 		error = ixl_setup_adminq_tq(pf);
585 		if (error) {
586 			device_printf(dev, "ixl_setup_adminq_tq() error: %d\n",
587 			    error);
588 			goto err_late;
589 		}
590 		ixl_configure_intr0_msix(pf);
591 		ixl_enable_intr0(hw);
592 
593 		error = ixl_setup_queue_msix(vsi);
594 		if (error)
595 			device_printf(dev, "ixl_setup_queue_msix() error: %d\n",
596 			    error);
597 		error = ixl_setup_queue_tqs(vsi);
598 		if (error)
599 			device_printf(dev, "ixl_setup_queue_tqs() error: %d\n",
600 			    error);
601 	} else {
602 		error = ixl_setup_legacy(pf);
603 
604 		error = ixl_setup_adminq_tq(pf);
605 		if (error) {
606 			device_printf(dev, "ixl_setup_adminq_tq() error: %d\n",
607 			    error);
608 			goto err_late;
609 		}
610 
611 		error = ixl_setup_queue_tqs(vsi);
612 		if (error)
613 			device_printf(dev, "ixl_setup_queue_tqs() error: %d\n",
614 			    error);
615 	}
616 
617 	if (error) {
618 		device_printf(dev, "interrupt setup error: %d\n", error);
619 	}
620 
621 	/* Set initial advertised speed sysctl value */
622 	ixl_get_initial_advertised_speeds(pf);
623 
624 	/* Initialize statistics & add sysctls */
625 	ixl_add_device_sysctls(pf);
626 
627 	ixl_pf_reset_stats(pf);
628 	ixl_update_stats_counters(pf);
629 	ixl_add_hw_stats(pf);
630 
631 	/* Register for VLAN events */
632 	vsi->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
633 	    ixl_register_vlan, vsi, EVENTHANDLER_PRI_FIRST);
634 	vsi->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
635 	    ixl_unregister_vlan, vsi, EVENTHANDLER_PRI_FIRST);
636 
637 #ifdef PCI_IOV
638 	ixl_initialize_sriov(pf);
639 #endif
640 
641 #ifdef DEV_NETMAP
642 	ixl_netmap_attach(vsi);
643 #endif /* DEV_NETMAP */
644 
645 #ifdef IXL_IW
646 	if (hw->func_caps.iwarp && ixl_enable_iwarp) {
647 		pf->iw_enabled = (pf->iw_msix > 0) ? true : false;
648 		if (pf->iw_enabled) {
649 			error = ixl_iw_pf_attach(pf);
650 			if (error) {
651 				device_printf(dev,
652 				    "interfacing to iwarp driver failed: %d\n",
653 				    error);
654 				goto err_late;
655 			}
656 		} else
657 			device_printf(dev,
658 			    "iwarp disabled on this device (no msix vectors)\n");
659 	} else {
660 		pf->iw_enabled = false;
661 		device_printf(dev, "The device is not iWARP enabled\n");
662 	}
663 #endif
664 
665 	INIT_DEBUGOUT("ixl_attach: end");
666 	return (0);
667 
668 err_late:
669 	if (vsi->ifp != NULL) {
670 		ether_ifdetach(vsi->ifp);
671 		if_free(vsi->ifp);
672 	}
673 err_mac_hmc:
674 	i40e_shutdown_lan_hmc(hw);
675 err_get_cap:
676 	i40e_shutdown_adminq(hw);
677 err_out:
678 	ixl_free_pci_resources(pf);
679 	ixl_free_vsi(vsi);
680 	IXL_PF_LOCK_DESTROY(pf);
681 	return (error);
682 }
683 
684 /*********************************************************************
685  *  Device removal routine
686  *
687  *  The detach entry point is called when the driver is being removed.
688  *  This routine stops the adapter and deallocates all the resources
689  *  that were allocated for driver operation.
690  *
691  *  return 0 on success, positive on failure
692  *********************************************************************/
693 
694 static int
695 ixl_detach(device_t dev)
696 {
697 	struct ixl_pf		*pf = device_get_softc(dev);
698 	struct i40e_hw		*hw = &pf->hw;
699 	struct ixl_vsi		*vsi = &pf->vsi;
700 	enum i40e_status_code	status;
701 #if defined(PCI_IOV) || defined(IXL_IW)
702 	int			error;
703 #endif
704 
705 	INIT_DEBUGOUT("ixl_detach: begin");
706 
707 	/* Make sure VLANS are not using driver */
708 	if (vsi->ifp->if_vlantrunk != NULL) {
709 		device_printf(dev, "Vlan in use, detach first\n");
710 		return (EBUSY);
711 	}
712 
713 #ifdef PCI_IOV
714 	error = pci_iov_detach(dev);
715 	if (error != 0) {
716 		device_printf(dev, "SR-IOV in use; detach first.\n");
717 		return (error);
718 	}
719 #endif
720 
721 	ether_ifdetach(vsi->ifp);
722 	if (vsi->ifp->if_drv_flags & IFF_DRV_RUNNING)
723 		ixl_stop(pf);
724 
725 	/* Shutdown LAN HMC */
726 	status = i40e_shutdown_lan_hmc(hw);
727 	if (status)
728 		device_printf(dev,
729 		    "Shutdown LAN HMC failed with code %d\n", status);
730 
731 	/* Teardown LAN queue resources */
732 	ixl_teardown_queue_msix(vsi);
733 	ixl_free_queue_tqs(vsi);
734 	/* Shutdown admin queue */
735 	ixl_disable_intr0(hw);
736 	ixl_teardown_adminq_msix(pf);
737 	ixl_free_adminq_tq(pf);
738 	status = i40e_shutdown_adminq(hw);
739 	if (status)
740 		device_printf(dev,
741 		    "Shutdown Admin queue failed with code %d\n", status);
742 
743 	/* Unregister VLAN events */
744 	if (vsi->vlan_attach != NULL)
745 		EVENTHANDLER_DEREGISTER(vlan_config, vsi->vlan_attach);
746 	if (vsi->vlan_detach != NULL)
747 		EVENTHANDLER_DEREGISTER(vlan_unconfig, vsi->vlan_detach);
748 
749 	callout_drain(&pf->timer);
750 
751 #ifdef IXL_IW
752 	if (ixl_enable_iwarp && pf->iw_enabled) {
753 		error = ixl_iw_pf_detach(pf);
754 		if (error == EBUSY) {
755 			device_printf(dev, "iwarp in use; stop it first.\n");
756 			return (error);
757 		}
758 	}
759 #endif
760 
761 #ifdef DEV_NETMAP
762 	netmap_detach(vsi->ifp);
763 #endif /* DEV_NETMAP */
764 	ixl_pf_qmgr_destroy(&pf->qmgr);
765 	ixl_free_pci_resources(pf);
766 	bus_generic_detach(dev);
767 	if_free(vsi->ifp);
768 	ixl_free_vsi(vsi);
769 	IXL_PF_LOCK_DESTROY(pf);
770 	return (0);
771 }
772 
773 /*********************************************************************
774  *
775  *  Shutdown entry point
776  *
777  **********************************************************************/
778 
779 static int
780 ixl_shutdown(device_t dev)
781 {
782 	struct ixl_pf *pf = device_get_softc(dev);
783 	ixl_stop(pf);
784 	return (0);
785 }
786 
787