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