ixv.c revision 1.129 1 /*$NetBSD: ixv.c,v 1.129 2019/09/04 07:29:34 msaitoh Exp $*/
2
3 /******************************************************************************
4
5 Copyright (c) 2001-2017, Intel Corporation
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17
18 3. Neither the name of the Intel Corporation nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 POSSIBILITY OF SUCH DAMAGE.
33
34 ******************************************************************************/
35 /*$FreeBSD: head/sys/dev/ixgbe/if_ixv.c 331224 2018-03-19 20:55:05Z erj $*/
36
37 #ifdef _KERNEL_OPT
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_net_mpsafe.h"
41 #endif
42
43 #include "ixgbe.h"
44 #include "vlan.h"
45
46 /************************************************************************
47 * Driver version
48 ************************************************************************/
49 static const char ixv_driver_version[] = "2.0.1-k";
50 /* XXX NetBSD: + 1.5.17 */
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 ixv_strings
57 * Last entry must be all 0s
58 *
59 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
60 ************************************************************************/
61 static const ixgbe_vendor_info_t ixv_vendor_info_array[] =
62 {
63 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_VF, 0, 0, 0},
64 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_VF, 0, 0, 0},
65 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550_VF, 0, 0, 0},
66 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_VF, 0, 0, 0},
67 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_VF, 0, 0, 0},
68 /* required last entry */
69 {0, 0, 0, 0, 0}
70 };
71
72 /************************************************************************
73 * Table of branding strings
74 ************************************************************************/
75 static const char *ixv_strings[] = {
76 "Intel(R) PRO/10GbE Virtual Function Network Driver"
77 };
78
79 /*********************************************************************
80 * Function prototypes
81 *********************************************************************/
82 static int ixv_probe(device_t, cfdata_t, void *);
83 static void ixv_attach(device_t, device_t, void *);
84 static int ixv_detach(device_t, int);
85 #if 0
86 static int ixv_shutdown(device_t);
87 #endif
88 static int ixv_ifflags_cb(struct ethercom *);
89 static int ixv_ioctl(struct ifnet *, u_long, void *);
90 static int ixv_init(struct ifnet *);
91 static void ixv_init_locked(struct adapter *);
92 static void ixv_ifstop(struct ifnet *, int);
93 static void ixv_stop(void *);
94 static void ixv_init_device_features(struct adapter *);
95 static void ixv_media_status(struct ifnet *, struct ifmediareq *);
96 static int ixv_media_change(struct ifnet *);
97 static int ixv_allocate_pci_resources(struct adapter *,
98 const struct pci_attach_args *);
99 static int ixv_allocate_msix(struct adapter *,
100 const struct pci_attach_args *);
101 static int ixv_configure_interrupts(struct adapter *);
102 static void ixv_free_pci_resources(struct adapter *);
103 static void ixv_local_timer(void *);
104 static void ixv_local_timer_locked(void *);
105 static int ixv_setup_interface(device_t, struct adapter *);
106 static int ixv_negotiate_api(struct adapter *);
107
108 static void ixv_initialize_transmit_units(struct adapter *);
109 static void ixv_initialize_receive_units(struct adapter *);
110 static void ixv_initialize_rss_mapping(struct adapter *);
111 static s32 ixv_check_link(struct adapter *);
112
113 static void ixv_enable_intr(struct adapter *);
114 static void ixv_disable_intr(struct adapter *);
115 static void ixv_set_multi(struct adapter *);
116 static void ixv_update_link_status(struct adapter *);
117 static int ixv_sysctl_debug(SYSCTLFN_PROTO);
118 static void ixv_set_ivar(struct adapter *, u8, u8, s8);
119 static void ixv_configure_ivars(struct adapter *);
120 static u8 * ixv_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
121 static void ixv_eitr_write(struct adapter *, uint32_t, uint32_t);
122
123 static void ixv_setup_vlan_tagging(struct adapter *);
124 static int ixv_setup_vlan_support(struct adapter *);
125 static int ixv_vlan_cb(struct ethercom *, uint16_t, bool);
126 static int ixv_register_vlan(struct adapter *, u16);
127 static int ixv_unregister_vlan(struct adapter *, u16);
128
129 static void ixv_add_device_sysctls(struct adapter *);
130 static void ixv_save_stats(struct adapter *);
131 static void ixv_init_stats(struct adapter *);
132 static void ixv_update_stats(struct adapter *);
133 static void ixv_add_stats_sysctls(struct adapter *);
134
135 /* Sysctl handlers */
136 static void ixv_set_sysctl_value(struct adapter *, const char *,
137 const char *, int *, int);
138 static int ixv_sysctl_interrupt_rate_handler(SYSCTLFN_PROTO);
139 static int ixv_sysctl_next_to_check_handler(SYSCTLFN_PROTO);
140 static int ixv_sysctl_rdh_handler(SYSCTLFN_PROTO);
141 static int ixv_sysctl_rdt_handler(SYSCTLFN_PROTO);
142 static int ixv_sysctl_tdt_handler(SYSCTLFN_PROTO);
143 static int ixv_sysctl_tdh_handler(SYSCTLFN_PROTO);
144
145 /* The MSI-X Interrupt handlers */
146 static int ixv_msix_que(void *);
147 static int ixv_msix_mbx(void *);
148
149 /* Deferred interrupt tasklets */
150 static void ixv_handle_que(void *);
151 static void ixv_handle_link(void *);
152
153 /* Workqueue handler for deferred work */
154 static void ixv_handle_que_work(struct work *, void *);
155
156 const struct sysctlnode *ixv_sysctl_instance(struct adapter *);
157 static const ixgbe_vendor_info_t *ixv_lookup(const struct pci_attach_args *);
158
159 /************************************************************************
160 * FreeBSD Device Interface Entry Points
161 ************************************************************************/
162 CFATTACH_DECL3_NEW(ixv, sizeof(struct adapter),
163 ixv_probe, ixv_attach, ixv_detach, NULL, NULL, NULL,
164 DVF_DETACH_SHUTDOWN);
165
166 #if 0
167 static driver_t ixv_driver = {
168 "ixv", ixv_methods, sizeof(struct adapter),
169 };
170
171 devclass_t ixv_devclass;
172 DRIVER_MODULE(ixv, pci, ixv_driver, ixv_devclass, 0, 0);
173 MODULE_DEPEND(ixv, pci, 1, 1, 1);
174 MODULE_DEPEND(ixv, ether, 1, 1, 1);
175 #endif
176
177 /*
178 * TUNEABLE PARAMETERS:
179 */
180
181 /* Number of Queues - do not exceed MSI-X vectors - 1 */
182 static int ixv_num_queues = 0;
183 #define TUNABLE_INT(__x, __y)
184 TUNABLE_INT("hw.ixv.num_queues", &ixv_num_queues);
185
186 /*
187 * AIM: Adaptive Interrupt Moderation
188 * which means that the interrupt rate
189 * is varied over time based on the
190 * traffic for that interrupt vector
191 */
192 static bool ixv_enable_aim = false;
193 TUNABLE_INT("hw.ixv.enable_aim", &ixv_enable_aim);
194
195 static int ixv_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
196 TUNABLE_INT("hw.ixv.max_interrupt_rate", &ixv_max_interrupt_rate);
197
198 /* How many packets rxeof tries to clean at a time */
199 static int ixv_rx_process_limit = 256;
200 TUNABLE_INT("hw.ixv.rx_process_limit", &ixv_rx_process_limit);
201
202 /* How many packets txeof tries to clean at a time */
203 static int ixv_tx_process_limit = 256;
204 TUNABLE_INT("hw.ixv.tx_process_limit", &ixv_tx_process_limit);
205
206 /* Which packet processing uses workqueue or softint */
207 static bool ixv_txrx_workqueue = false;
208
209 /*
210 * Number of TX descriptors per ring,
211 * setting higher than RX as this seems
212 * the better performing choice.
213 */
214 static int ixv_txd = PERFORM_TXD;
215 TUNABLE_INT("hw.ixv.txd", &ixv_txd);
216
217 /* Number of RX descriptors per ring */
218 static int ixv_rxd = PERFORM_RXD;
219 TUNABLE_INT("hw.ixv.rxd", &ixv_rxd);
220
221 /* Legacy Transmit (single queue) */
222 static int ixv_enable_legacy_tx = 0;
223 TUNABLE_INT("hw.ixv.enable_legacy_tx", &ixv_enable_legacy_tx);
224
225 #ifdef NET_MPSAFE
226 #define IXGBE_MPSAFE 1
227 #define IXGBE_CALLOUT_FLAGS CALLOUT_MPSAFE
228 #define IXGBE_SOFTINFT_FLAGS SOFTINT_MPSAFE
229 #define IXGBE_WORKQUEUE_FLAGS WQ_PERCPU | WQ_MPSAFE
230 #else
231 #define IXGBE_CALLOUT_FLAGS 0
232 #define IXGBE_SOFTINFT_FLAGS 0
233 #define IXGBE_WORKQUEUE_FLAGS WQ_PERCPU
234 #endif
235 #define IXGBE_WORKQUEUE_PRI PRI_SOFTNET
236
237 #if 0
238 static int (*ixv_start_locked)(struct ifnet *, struct tx_ring *);
239 static int (*ixv_ring_empty)(struct ifnet *, struct buf_ring *);
240 #endif
241
242 /************************************************************************
243 * ixv_probe - Device identification routine
244 *
245 * Determines if the driver should be loaded on
246 * adapter based on its PCI vendor/device ID.
247 *
248 * return BUS_PROBE_DEFAULT on success, positive on failure
249 ************************************************************************/
250 static int
251 ixv_probe(device_t dev, cfdata_t cf, void *aux)
252 {
253 #ifdef __HAVE_PCI_MSI_MSIX
254 const struct pci_attach_args *pa = aux;
255
256 return (ixv_lookup(pa) != NULL) ? 1 : 0;
257 #else
258 return 0;
259 #endif
260 } /* ixv_probe */
261
262 static const ixgbe_vendor_info_t *
263 ixv_lookup(const struct pci_attach_args *pa)
264 {
265 const ixgbe_vendor_info_t *ent;
266 pcireg_t subid;
267
268 INIT_DEBUGOUT("ixv_lookup: begin");
269
270 if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
271 return NULL;
272
273 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
274
275 for (ent = ixv_vendor_info_array; ent->vendor_id != 0; ent++) {
276 if ((PCI_VENDOR(pa->pa_id) == ent->vendor_id) &&
277 (PCI_PRODUCT(pa->pa_id) == ent->device_id) &&
278 ((PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id) ||
279 (ent->subvendor_id == 0)) &&
280 ((PCI_SUBSYS_ID(subid) == ent->subdevice_id) ||
281 (ent->subdevice_id == 0))) {
282 return ent;
283 }
284 }
285
286 return NULL;
287 }
288
289 /************************************************************************
290 * ixv_attach - Device initialization routine
291 *
292 * Called when the driver is being loaded.
293 * Identifies the type of hardware, allocates all resources
294 * and initializes the hardware.
295 *
296 * return 0 on success, positive on failure
297 ************************************************************************/
298 static void
299 ixv_attach(device_t parent, device_t dev, void *aux)
300 {
301 struct adapter *adapter;
302 struct ixgbe_hw *hw;
303 int error = 0;
304 pcireg_t id, subid;
305 const ixgbe_vendor_info_t *ent;
306 const struct pci_attach_args *pa = aux;
307 const char *apivstr;
308 const char *str;
309 char buf[256];
310
311 INIT_DEBUGOUT("ixv_attach: begin");
312
313 /*
314 * Make sure BUSMASTER is set, on a VM under
315 * KVM it may not be and will break things.
316 */
317 ixgbe_pci_enable_busmaster(pa->pa_pc, pa->pa_tag);
318
319 /* Allocate, clear, and link in our adapter structure */
320 adapter = device_private(dev);
321 adapter->dev = dev;
322 adapter->hw.back = adapter;
323 hw = &adapter->hw;
324
325 adapter->init_locked = ixv_init_locked;
326 adapter->stop_locked = ixv_stop;
327
328 adapter->osdep.pc = pa->pa_pc;
329 adapter->osdep.tag = pa->pa_tag;
330 if (pci_dma64_available(pa))
331 adapter->osdep.dmat = pa->pa_dmat64;
332 else
333 adapter->osdep.dmat = pa->pa_dmat;
334 adapter->osdep.attached = false;
335
336 ent = ixv_lookup(pa);
337
338 KASSERT(ent != NULL);
339
340 aprint_normal(": %s, Version - %s\n",
341 ixv_strings[ent->index], ixv_driver_version);
342
343 /* Core Lock Init*/
344 IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
345
346 /* Do base PCI setup - map BAR0 */
347 if (ixv_allocate_pci_resources(adapter, pa)) {
348 aprint_error_dev(dev, "ixv_allocate_pci_resources() failed!\n");
349 error = ENXIO;
350 goto err_out;
351 }
352
353 /* SYSCTL APIs */
354 ixv_add_device_sysctls(adapter);
355
356 /* Set up the timer callout */
357 callout_init(&adapter->timer, IXGBE_CALLOUT_FLAGS);
358
359 /* Save off the information about this board */
360 id = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
361 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
362 hw->vendor_id = PCI_VENDOR(id);
363 hw->device_id = PCI_PRODUCT(id);
364 hw->revision_id =
365 PCI_REVISION(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
366 hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
367 hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
368
369 /* A subset of set_mac_type */
370 switch (hw->device_id) {
371 case IXGBE_DEV_ID_82599_VF:
372 hw->mac.type = ixgbe_mac_82599_vf;
373 str = "82599 VF";
374 break;
375 case IXGBE_DEV_ID_X540_VF:
376 hw->mac.type = ixgbe_mac_X540_vf;
377 str = "X540 VF";
378 break;
379 case IXGBE_DEV_ID_X550_VF:
380 hw->mac.type = ixgbe_mac_X550_vf;
381 str = "X550 VF";
382 break;
383 case IXGBE_DEV_ID_X550EM_X_VF:
384 hw->mac.type = ixgbe_mac_X550EM_x_vf;
385 str = "X550EM X VF";
386 break;
387 case IXGBE_DEV_ID_X550EM_A_VF:
388 hw->mac.type = ixgbe_mac_X550EM_a_vf;
389 str = "X550EM A VF";
390 break;
391 default:
392 /* Shouldn't get here since probe succeeded */
393 aprint_error_dev(dev, "Unknown device ID!\n");
394 error = ENXIO;
395 goto err_out;
396 break;
397 }
398 aprint_normal_dev(dev, "device %s\n", str);
399
400 ixv_init_device_features(adapter);
401
402 /* Initialize the shared code */
403 error = ixgbe_init_ops_vf(hw);
404 if (error) {
405 aprint_error_dev(dev, "ixgbe_init_ops_vf() failed!\n");
406 error = EIO;
407 goto err_out;
408 }
409
410 /* Setup the mailbox */
411 ixgbe_init_mbx_params_vf(hw);
412
413 /* Set the right number of segments */
414 adapter->num_segs = IXGBE_82599_SCATTER;
415
416 /* Reset mbox api to 1.0 */
417 error = hw->mac.ops.reset_hw(hw);
418 if (error == IXGBE_ERR_RESET_FAILED)
419 aprint_error_dev(dev, "...reset_hw() failure: Reset Failed!\n");
420 else if (error)
421 aprint_error_dev(dev, "...reset_hw() failed with error %d\n",
422 error);
423 if (error) {
424 error = EIO;
425 goto err_out;
426 }
427
428 error = hw->mac.ops.init_hw(hw);
429 if (error) {
430 aprint_error_dev(dev, "...init_hw() failed!\n");
431 error = EIO;
432 goto err_out;
433 }
434
435 /* Negotiate mailbox API version */
436 error = ixv_negotiate_api(adapter);
437 if (error)
438 aprint_normal_dev(dev,
439 "MBX API negotiation failed during attach!\n");
440 switch (hw->api_version) {
441 case ixgbe_mbox_api_10:
442 apivstr = "1.0";
443 break;
444 case ixgbe_mbox_api_20:
445 apivstr = "2.0";
446 break;
447 case ixgbe_mbox_api_11:
448 apivstr = "1.1";
449 break;
450 case ixgbe_mbox_api_12:
451 apivstr = "1.2";
452 break;
453 case ixgbe_mbox_api_13:
454 apivstr = "1.3";
455 break;
456 default:
457 apivstr = "unknown";
458 break;
459 }
460 aprint_normal_dev(dev, "Mailbox API %s\n", apivstr);
461
462 /* If no mac address was assigned, make a random one */
463 if (!ixv_check_ether_addr(hw->mac.addr)) {
464 u8 addr[ETHER_ADDR_LEN];
465 uint64_t rndval = cprng_strong64();
466
467 memcpy(addr, &rndval, sizeof(addr));
468 addr[0] &= 0xFE;
469 addr[0] |= 0x02;
470 bcopy(addr, hw->mac.addr, sizeof(addr));
471 }
472
473 /* Register for VLAN events */
474 ether_set_vlan_cb(&adapter->osdep.ec, ixv_vlan_cb);
475
476 /* Sysctls for limiting the amount of work done in the taskqueues */
477 ixv_set_sysctl_value(adapter, "rx_processing_limit",
478 "max number of rx packets to process",
479 &adapter->rx_process_limit, ixv_rx_process_limit);
480
481 ixv_set_sysctl_value(adapter, "tx_processing_limit",
482 "max number of tx packets to process",
483 &adapter->tx_process_limit, ixv_tx_process_limit);
484
485 /* Do descriptor calc and sanity checks */
486 if (((ixv_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
487 ixv_txd < MIN_TXD || ixv_txd > MAX_TXD) {
488 aprint_error_dev(dev, "TXD config issue, using default!\n");
489 adapter->num_tx_desc = DEFAULT_TXD;
490 } else
491 adapter->num_tx_desc = ixv_txd;
492
493 if (((ixv_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
494 ixv_rxd < MIN_RXD || ixv_rxd > MAX_RXD) {
495 aprint_error_dev(dev, "RXD config issue, using default!\n");
496 adapter->num_rx_desc = DEFAULT_RXD;
497 } else
498 adapter->num_rx_desc = ixv_rxd;
499
500 /* Setup MSI-X */
501 error = ixv_configure_interrupts(adapter);
502 if (error)
503 goto err_out;
504
505 /* Allocate our TX/RX Queues */
506 if (ixgbe_allocate_queues(adapter)) {
507 aprint_error_dev(dev, "ixgbe_allocate_queues() failed!\n");
508 error = ENOMEM;
509 goto err_out;
510 }
511
512 /* hw.ix defaults init */
513 adapter->enable_aim = ixv_enable_aim;
514
515 adapter->txrx_use_workqueue = ixv_txrx_workqueue;
516
517 error = ixv_allocate_msix(adapter, pa);
518 if (error) {
519 device_printf(dev, "ixv_allocate_msix() failed!\n");
520 goto err_late;
521 }
522
523 /* Setup OS specific network interface */
524 error = ixv_setup_interface(dev, adapter);
525 if (error != 0) {
526 aprint_error_dev(dev, "ixv_setup_interface() failed!\n");
527 goto err_late;
528 }
529
530 /* Do the stats setup */
531 ixv_save_stats(adapter);
532 ixv_init_stats(adapter);
533 ixv_add_stats_sysctls(adapter);
534
535 if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
536 ixgbe_netmap_attach(adapter);
537
538 snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_cap);
539 aprint_verbose_dev(dev, "feature cap %s\n", buf);
540 snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_en);
541 aprint_verbose_dev(dev, "feature ena %s\n", buf);
542
543 INIT_DEBUGOUT("ixv_attach: end");
544 adapter->osdep.attached = true;
545
546 return;
547
548 err_late:
549 ixgbe_free_transmit_structures(adapter);
550 ixgbe_free_receive_structures(adapter);
551 free(adapter->queues, M_DEVBUF);
552 err_out:
553 ixv_free_pci_resources(adapter);
554 IXGBE_CORE_LOCK_DESTROY(adapter);
555
556 return;
557 } /* ixv_attach */
558
559 /************************************************************************
560 * ixv_detach - Device removal routine
561 *
562 * Called when the driver is being removed.
563 * Stops the adapter and deallocates all the resources
564 * that were allocated for driver operation.
565 *
566 * return 0 on success, positive on failure
567 ************************************************************************/
568 static int
569 ixv_detach(device_t dev, int flags)
570 {
571 struct adapter *adapter = device_private(dev);
572 struct ixgbe_hw *hw = &adapter->hw;
573 struct ix_queue *que = adapter->queues;
574 struct tx_ring *txr = adapter->tx_rings;
575 struct rx_ring *rxr = adapter->rx_rings;
576 struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
577
578 INIT_DEBUGOUT("ixv_detach: begin");
579 if (adapter->osdep.attached == false)
580 return 0;
581
582 /* Stop the interface. Callouts are stopped in it. */
583 ixv_ifstop(adapter->ifp, 1);
584
585 #if NVLAN > 0
586 /* Make sure VLANs are not using driver */
587 if (!VLAN_ATTACHED(&adapter->osdep.ec))
588 ; /* nothing to do: no VLANs */
589 else if ((flags & (DETACH_SHUTDOWN | DETACH_FORCE)) != 0)
590 vlan_ifdetach(adapter->ifp);
591 else {
592 aprint_error_dev(dev, "VLANs in use, detach first\n");
593 return EBUSY;
594 }
595 #endif
596
597 for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
598 if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
599 softint_disestablish(txr->txr_si);
600 softint_disestablish(que->que_si);
601 }
602 if (adapter->txr_wq != NULL)
603 workqueue_destroy(adapter->txr_wq);
604 if (adapter->txr_wq_enqueued != NULL)
605 percpu_free(adapter->txr_wq_enqueued, sizeof(u_int));
606 if (adapter->que_wq != NULL)
607 workqueue_destroy(adapter->que_wq);
608
609 /* Drain the Mailbox(link) queue */
610 softint_disestablish(adapter->link_si);
611
612 ether_ifdetach(adapter->ifp);
613 callout_halt(&adapter->timer, NULL);
614
615 if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
616 netmap_detach(adapter->ifp);
617
618 ixv_free_pci_resources(adapter);
619 #if 0 /* XXX the NetBSD port is probably missing something here */
620 bus_generic_detach(dev);
621 #endif
622 if_detach(adapter->ifp);
623 if_percpuq_destroy(adapter->ipq);
624
625 sysctl_teardown(&adapter->sysctllog);
626 evcnt_detach(&adapter->efbig_tx_dma_setup);
627 evcnt_detach(&adapter->mbuf_defrag_failed);
628 evcnt_detach(&adapter->efbig2_tx_dma_setup);
629 evcnt_detach(&adapter->einval_tx_dma_setup);
630 evcnt_detach(&adapter->other_tx_dma_setup);
631 evcnt_detach(&adapter->eagain_tx_dma_setup);
632 evcnt_detach(&adapter->enomem_tx_dma_setup);
633 evcnt_detach(&adapter->watchdog_events);
634 evcnt_detach(&adapter->tso_err);
635 evcnt_detach(&adapter->link_irq);
636
637 txr = adapter->tx_rings;
638 for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
639 evcnt_detach(&adapter->queues[i].irqs);
640 evcnt_detach(&adapter->queues[i].handleq);
641 evcnt_detach(&adapter->queues[i].req);
642 evcnt_detach(&txr->no_desc_avail);
643 evcnt_detach(&txr->total_packets);
644 evcnt_detach(&txr->tso_tx);
645 #ifndef IXGBE_LEGACY_TX
646 evcnt_detach(&txr->pcq_drops);
647 #endif
648
649 evcnt_detach(&rxr->rx_packets);
650 evcnt_detach(&rxr->rx_bytes);
651 evcnt_detach(&rxr->rx_copies);
652 evcnt_detach(&rxr->no_jmbuf);
653 evcnt_detach(&rxr->rx_discarded);
654 }
655 evcnt_detach(&stats->ipcs);
656 evcnt_detach(&stats->l4cs);
657 evcnt_detach(&stats->ipcs_bad);
658 evcnt_detach(&stats->l4cs_bad);
659
660 /* Packet Reception Stats */
661 evcnt_detach(&stats->vfgorc);
662 evcnt_detach(&stats->vfgprc);
663 evcnt_detach(&stats->vfmprc);
664
665 /* Packet Transmission Stats */
666 evcnt_detach(&stats->vfgotc);
667 evcnt_detach(&stats->vfgptc);
668
669 /* Mailbox Stats */
670 evcnt_detach(&hw->mbx.stats.msgs_tx);
671 evcnt_detach(&hw->mbx.stats.msgs_rx);
672 evcnt_detach(&hw->mbx.stats.acks);
673 evcnt_detach(&hw->mbx.stats.reqs);
674 evcnt_detach(&hw->mbx.stats.rsts);
675
676 ixgbe_free_transmit_structures(adapter);
677 ixgbe_free_receive_structures(adapter);
678 for (int i = 0; i < adapter->num_queues; i++) {
679 struct ix_queue *lque = &adapter->queues[i];
680 mutex_destroy(&lque->dc_mtx);
681 }
682 free(adapter->queues, M_DEVBUF);
683
684 IXGBE_CORE_LOCK_DESTROY(adapter);
685
686 return (0);
687 } /* ixv_detach */
688
689 /************************************************************************
690 * ixv_init_locked - Init entry point
691 *
692 * Used in two ways: It is used by the stack as an init entry
693 * point in network interface structure. It is also used
694 * by the driver as a hw/sw initialization routine to get
695 * to a consistent state.
696 *
697 * return 0 on success, positive on failure
698 ************************************************************************/
699 static void
700 ixv_init_locked(struct adapter *adapter)
701 {
702 struct ifnet *ifp = adapter->ifp;
703 device_t dev = adapter->dev;
704 struct ixgbe_hw *hw = &adapter->hw;
705 struct ix_queue *que;
706 int error = 0;
707 uint32_t mask;
708 int i;
709
710 INIT_DEBUGOUT("ixv_init_locked: begin");
711 KASSERT(mutex_owned(&adapter->core_mtx));
712 hw->adapter_stopped = FALSE;
713 hw->mac.ops.stop_adapter(hw);
714 callout_stop(&adapter->timer);
715 for (i = 0, que = adapter->queues; i < adapter->num_queues; i++, que++)
716 que->disabled_count = 0;
717
718 /* reprogram the RAR[0] in case user changed it. */
719 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
720
721 /* Get the latest mac address, User can use a LAA */
722 memcpy(hw->mac.addr, CLLADDR(ifp->if_sadl),
723 IXGBE_ETH_LENGTH_OF_ADDRESS);
724 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, 1);
725
726 /* Prepare transmit descriptors and buffers */
727 if (ixgbe_setup_transmit_structures(adapter)) {
728 aprint_error_dev(dev, "Could not setup transmit structures\n");
729 ixv_stop(adapter);
730 return;
731 }
732
733 /* Reset VF and renegotiate mailbox API version */
734 hw->mac.ops.reset_hw(hw);
735 hw->mac.ops.start_hw(hw);
736 error = ixv_negotiate_api(adapter);
737 if (error)
738 device_printf(dev,
739 "Mailbox API negotiation failed in init_locked!\n");
740
741 ixv_initialize_transmit_units(adapter);
742
743 /* Setup Multicast table */
744 ixv_set_multi(adapter);
745
746 /*
747 * Determine the correct mbuf pool
748 * for doing jumbo/headersplit
749 */
750 if (ifp->if_mtu > ETHERMTU)
751 adapter->rx_mbuf_sz = MJUMPAGESIZE;
752 else
753 adapter->rx_mbuf_sz = MCLBYTES;
754
755 /* Prepare receive descriptors and buffers */
756 if (ixgbe_setup_receive_structures(adapter)) {
757 device_printf(dev, "Could not setup receive structures\n");
758 ixv_stop(adapter);
759 return;
760 }
761
762 /* Configure RX settings */
763 ixv_initialize_receive_units(adapter);
764
765 #if 0 /* XXX isn't it required? -- msaitoh */
766 /* Set the various hardware offload abilities */
767 ifp->if_hwassist = 0;
768 if (ifp->if_capenable & IFCAP_TSO4)
769 ifp->if_hwassist |= CSUM_TSO;
770 if (ifp->if_capenable & IFCAP_TXCSUM) {
771 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
772 #if __FreeBSD_version >= 800000
773 ifp->if_hwassist |= CSUM_SCTP;
774 #endif
775 }
776 #endif
777
778 /* Set up VLAN offload and filter */
779 ixv_setup_vlan_support(adapter);
780
781 /* Set up MSI-X routing */
782 ixv_configure_ivars(adapter);
783
784 /* Set up auto-mask */
785 mask = (1 << adapter->vector);
786 for (i = 0, que = adapter->queues; i < adapter->num_queues; i++, que++)
787 mask |= (1 << que->msix);
788 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, mask);
789
790 /* Set moderation on the Link interrupt */
791 ixv_eitr_write(adapter, adapter->vector, IXGBE_LINK_ITR);
792
793 /* Stats init */
794 ixv_init_stats(adapter);
795
796 /* Config/Enable Link */
797 hw->mac.get_link_status = TRUE;
798 hw->mac.ops.check_link(hw, &adapter->link_speed, &adapter->link_up,
799 FALSE);
800
801 /* Start watchdog */
802 callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
803
804 /* And now turn on interrupts */
805 ixv_enable_intr(adapter);
806
807 /* Update saved flags. See ixgbe_ifflags_cb() */
808 adapter->if_flags = ifp->if_flags;
809 adapter->ec_capenable = adapter->osdep.ec.ec_capenable;
810
811 /* Now inform the stack we're ready */
812 ifp->if_flags |= IFF_RUNNING;
813 ifp->if_flags &= ~IFF_OACTIVE;
814
815 return;
816 } /* ixv_init_locked */
817
818 /************************************************************************
819 * ixv_enable_queue
820 ************************************************************************/
821 static inline void
822 ixv_enable_queue(struct adapter *adapter, u32 vector)
823 {
824 struct ixgbe_hw *hw = &adapter->hw;
825 struct ix_queue *que = &adapter->queues[vector];
826 u32 queue = 1UL << vector;
827 u32 mask;
828
829 mutex_enter(&que->dc_mtx);
830 if (que->disabled_count > 0 && --que->disabled_count > 0)
831 goto out;
832
833 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
834 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
835 out:
836 mutex_exit(&que->dc_mtx);
837 } /* ixv_enable_queue */
838
839 /************************************************************************
840 * ixv_disable_queue
841 ************************************************************************/
842 static inline void
843 ixv_disable_queue(struct adapter *adapter, u32 vector)
844 {
845 struct ixgbe_hw *hw = &adapter->hw;
846 struct ix_queue *que = &adapter->queues[vector];
847 u32 queue = 1UL << vector;
848 u32 mask;
849
850 mutex_enter(&que->dc_mtx);
851 if (que->disabled_count++ > 0)
852 goto out;
853
854 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
855 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, mask);
856 out:
857 mutex_exit(&que->dc_mtx);
858 } /* ixv_disable_queue */
859
860 #if 0
861 static inline void
862 ixv_rearm_queues(struct adapter *adapter, u64 queues)
863 {
864 u32 mask = (IXGBE_EIMS_RTX_QUEUE & queues);
865 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEICS, mask);
866 } /* ixv_rearm_queues */
867 #endif
868
869
870 /************************************************************************
871 * ixv_msix_que - MSI-X Queue Interrupt Service routine
872 ************************************************************************/
873 static int
874 ixv_msix_que(void *arg)
875 {
876 struct ix_queue *que = arg;
877 struct adapter *adapter = que->adapter;
878 struct tx_ring *txr = que->txr;
879 struct rx_ring *rxr = que->rxr;
880 bool more;
881 u32 newitr = 0;
882
883 ixv_disable_queue(adapter, que->msix);
884 ++que->irqs.ev_count;
885
886 #ifdef __NetBSD__
887 /* Don't run ixgbe_rxeof in interrupt context */
888 more = true;
889 #else
890 more = ixgbe_rxeof(que);
891 #endif
892
893 IXGBE_TX_LOCK(txr);
894 ixgbe_txeof(txr);
895 IXGBE_TX_UNLOCK(txr);
896
897 /* Do AIM now? */
898
899 if (adapter->enable_aim == false)
900 goto no_calc;
901 /*
902 * Do Adaptive Interrupt Moderation:
903 * - Write out last calculated setting
904 * - Calculate based on average size over
905 * the last interval.
906 */
907 if (que->eitr_setting)
908 ixv_eitr_write(adapter, que->msix, que->eitr_setting);
909
910 que->eitr_setting = 0;
911
912 /* Idle, do nothing */
913 if ((txr->bytes == 0) && (rxr->bytes == 0))
914 goto no_calc;
915
916 if ((txr->bytes) && (txr->packets))
917 newitr = txr->bytes/txr->packets;
918 if ((rxr->bytes) && (rxr->packets))
919 newitr = uimax(newitr, (rxr->bytes / rxr->packets));
920 newitr += 24; /* account for hardware frame, crc */
921
922 /* set an upper boundary */
923 newitr = uimin(newitr, 3000);
924
925 /* Be nice to the mid range */
926 if ((newitr > 300) && (newitr < 1200))
927 newitr = (newitr / 3);
928 else
929 newitr = (newitr / 2);
930
931 /*
932 * When RSC is used, ITR interval must be larger than RSC_DELAY.
933 * Currently, we use 2us for RSC_DELAY. The minimum value is always
934 * greater than 2us on 100M (and 10M?(not documented)), but it's not
935 * on 1G and higher.
936 */
937 if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
938 && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
939 if (newitr < IXGBE_MIN_RSC_EITR_10G1G)
940 newitr = IXGBE_MIN_RSC_EITR_10G1G;
941 }
942
943 /* save for next interrupt */
944 que->eitr_setting = newitr;
945
946 /* Reset state */
947 txr->bytes = 0;
948 txr->packets = 0;
949 rxr->bytes = 0;
950 rxr->packets = 0;
951
952 no_calc:
953 if (more)
954 softint_schedule(que->que_si);
955 else /* Re-enable this interrupt */
956 ixv_enable_queue(adapter, que->msix);
957
958 return 1;
959 } /* ixv_msix_que */
960
961 /************************************************************************
962 * ixv_msix_mbx
963 ************************************************************************/
964 static int
965 ixv_msix_mbx(void *arg)
966 {
967 struct adapter *adapter = arg;
968 struct ixgbe_hw *hw = &adapter->hw;
969
970 ++adapter->link_irq.ev_count;
971 /* NetBSD: We use auto-clear, so it's not required to write VTEICR */
972
973 /* Link status change */
974 hw->mac.get_link_status = TRUE;
975 softint_schedule(adapter->link_si);
976
977 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, (1 << adapter->vector));
978
979 return 1;
980 } /* ixv_msix_mbx */
981
982 static void
983 ixv_eitr_write(struct adapter *adapter, uint32_t index, uint32_t itr)
984 {
985
986 /*
987 * Newer devices than 82598 have VF function, so this function is
988 * simple.
989 */
990 itr |= IXGBE_EITR_CNT_WDIS;
991
992 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEITR(index), itr);
993 }
994
995
996 /************************************************************************
997 * ixv_media_status - Media Ioctl callback
998 *
999 * Called whenever the user queries the status of
1000 * the interface using ifconfig.
1001 ************************************************************************/
1002 static void
1003 ixv_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1004 {
1005 struct adapter *adapter = ifp->if_softc;
1006
1007 INIT_DEBUGOUT("ixv_media_status: begin");
1008 IXGBE_CORE_LOCK(adapter);
1009 ixv_update_link_status(adapter);
1010
1011 ifmr->ifm_status = IFM_AVALID;
1012 ifmr->ifm_active = IFM_ETHER;
1013
1014 if (adapter->link_active != LINK_STATE_UP) {
1015 ifmr->ifm_active |= IFM_NONE;
1016 IXGBE_CORE_UNLOCK(adapter);
1017 return;
1018 }
1019
1020 ifmr->ifm_status |= IFM_ACTIVE;
1021
1022 switch (adapter->link_speed) {
1023 case IXGBE_LINK_SPEED_10GB_FULL:
1024 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
1025 break;
1026 case IXGBE_LINK_SPEED_5GB_FULL:
1027 ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
1028 break;
1029 case IXGBE_LINK_SPEED_2_5GB_FULL:
1030 ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
1031 break;
1032 case IXGBE_LINK_SPEED_1GB_FULL:
1033 ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
1034 break;
1035 case IXGBE_LINK_SPEED_100_FULL:
1036 ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
1037 break;
1038 case IXGBE_LINK_SPEED_10_FULL:
1039 ifmr->ifm_active |= IFM_10_T | IFM_FDX;
1040 break;
1041 }
1042
1043 ifp->if_baudrate = ifmedia_baudrate(ifmr->ifm_active);
1044
1045 IXGBE_CORE_UNLOCK(adapter);
1046 } /* ixv_media_status */
1047
1048 /************************************************************************
1049 * ixv_media_change - Media Ioctl callback
1050 *
1051 * Called when the user changes speed/duplex using
1052 * media/mediopt option with ifconfig.
1053 ************************************************************************/
1054 static int
1055 ixv_media_change(struct ifnet *ifp)
1056 {
1057 struct adapter *adapter = ifp->if_softc;
1058 struct ifmedia *ifm = &adapter->media;
1059
1060 INIT_DEBUGOUT("ixv_media_change: begin");
1061
1062 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1063 return (EINVAL);
1064
1065 switch (IFM_SUBTYPE(ifm->ifm_media)) {
1066 case IFM_AUTO:
1067 break;
1068 default:
1069 device_printf(adapter->dev, "Only auto media type\n");
1070 return (EINVAL);
1071 }
1072
1073 return (0);
1074 } /* ixv_media_change */
1075
1076
1077 /************************************************************************
1078 * ixv_negotiate_api
1079 *
1080 * Negotiate the Mailbox API with the PF;
1081 * start with the most featured API first.
1082 ************************************************************************/
1083 static int
1084 ixv_negotiate_api(struct adapter *adapter)
1085 {
1086 struct ixgbe_hw *hw = &adapter->hw;
1087 int mbx_api[] = { ixgbe_mbox_api_11,
1088 ixgbe_mbox_api_10,
1089 ixgbe_mbox_api_unknown };
1090 int i = 0;
1091
1092 while (mbx_api[i] != ixgbe_mbox_api_unknown) {
1093 if (ixgbevf_negotiate_api_version(hw, mbx_api[i]) == 0)
1094 return (0);
1095 i++;
1096 }
1097
1098 return (EINVAL);
1099 } /* ixv_negotiate_api */
1100
1101
1102 /************************************************************************
1103 * ixv_set_multi - Multicast Update
1104 *
1105 * Called whenever multicast address list is updated.
1106 ************************************************************************/
1107 static void
1108 ixv_set_multi(struct adapter *adapter)
1109 {
1110 struct ether_multi *enm;
1111 struct ether_multistep step;
1112 struct ethercom *ec = &adapter->osdep.ec;
1113 u8 mta[MAX_NUM_MULTICAST_ADDRESSES * IXGBE_ETH_LENGTH_OF_ADDRESS];
1114 u8 *update_ptr;
1115 int mcnt = 0;
1116
1117 KASSERT(mutex_owned(&adapter->core_mtx));
1118 IOCTL_DEBUGOUT("ixv_set_multi: begin");
1119
1120 ETHER_LOCK(ec);
1121 ETHER_FIRST_MULTI(step, ec, enm);
1122 while (enm != NULL) {
1123 bcopy(enm->enm_addrlo,
1124 &mta[mcnt * IXGBE_ETH_LENGTH_OF_ADDRESS],
1125 IXGBE_ETH_LENGTH_OF_ADDRESS);
1126 mcnt++;
1127 /* XXX This might be required --msaitoh */
1128 if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES)
1129 break;
1130 ETHER_NEXT_MULTI(step, enm);
1131 }
1132 ETHER_UNLOCK(ec);
1133
1134 update_ptr = mta;
1135
1136 adapter->hw.mac.ops.update_mc_addr_list(&adapter->hw, update_ptr, mcnt,
1137 ixv_mc_array_itr, TRUE);
1138 } /* ixv_set_multi */
1139
1140 /************************************************************************
1141 * ixv_mc_array_itr
1142 *
1143 * An iterator function needed by the multicast shared code.
1144 * It feeds the shared code routine the addresses in the
1145 * array of ixv_set_multi() one by one.
1146 ************************************************************************/
1147 static u8 *
1148 ixv_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
1149 {
1150 u8 *addr = *update_ptr;
1151 u8 *newptr;
1152
1153 *vmdq = 0;
1154
1155 newptr = addr + IXGBE_ETH_LENGTH_OF_ADDRESS;
1156 *update_ptr = newptr;
1157
1158 return addr;
1159 } /* ixv_mc_array_itr */
1160
1161 /************************************************************************
1162 * ixv_local_timer - Timer routine
1163 *
1164 * Checks for link status, updates statistics,
1165 * and runs the watchdog check.
1166 ************************************************************************/
1167 static void
1168 ixv_local_timer(void *arg)
1169 {
1170 struct adapter *adapter = arg;
1171
1172 IXGBE_CORE_LOCK(adapter);
1173 ixv_local_timer_locked(adapter);
1174 IXGBE_CORE_UNLOCK(adapter);
1175 }
1176
1177 static void
1178 ixv_local_timer_locked(void *arg)
1179 {
1180 struct adapter *adapter = arg;
1181 device_t dev = adapter->dev;
1182 struct ix_queue *que = adapter->queues;
1183 u64 queues = 0;
1184 u64 v0, v1, v2, v3, v4, v5, v6, v7;
1185 int hung = 0;
1186 int i;
1187
1188 KASSERT(mutex_owned(&adapter->core_mtx));
1189
1190 if (ixv_check_link(adapter)) {
1191 ixv_init_locked(adapter);
1192 return;
1193 }
1194
1195 /* Stats Update */
1196 ixv_update_stats(adapter);
1197
1198 /* Update some event counters */
1199 v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = 0;
1200 que = adapter->queues;
1201 for (i = 0; i < adapter->num_queues; i++, que++) {
1202 struct tx_ring *txr = que->txr;
1203
1204 v0 += txr->q_efbig_tx_dma_setup;
1205 v1 += txr->q_mbuf_defrag_failed;
1206 v2 += txr->q_efbig2_tx_dma_setup;
1207 v3 += txr->q_einval_tx_dma_setup;
1208 v4 += txr->q_other_tx_dma_setup;
1209 v5 += txr->q_eagain_tx_dma_setup;
1210 v6 += txr->q_enomem_tx_dma_setup;
1211 v7 += txr->q_tso_err;
1212 }
1213 adapter->efbig_tx_dma_setup.ev_count = v0;
1214 adapter->mbuf_defrag_failed.ev_count = v1;
1215 adapter->efbig2_tx_dma_setup.ev_count = v2;
1216 adapter->einval_tx_dma_setup.ev_count = v3;
1217 adapter->other_tx_dma_setup.ev_count = v4;
1218 adapter->eagain_tx_dma_setup.ev_count = v5;
1219 adapter->enomem_tx_dma_setup.ev_count = v6;
1220 adapter->tso_err.ev_count = v7;
1221
1222 /*
1223 * Check the TX queues status
1224 * - mark hung queues so we don't schedule on them
1225 * - watchdog only if all queues show hung
1226 */
1227 que = adapter->queues;
1228 for (i = 0; i < adapter->num_queues; i++, que++) {
1229 /* Keep track of queues with work for soft irq */
1230 if (que->txr->busy)
1231 queues |= ((u64)1 << que->me);
1232 /*
1233 * Each time txeof runs without cleaning, but there
1234 * are uncleaned descriptors it increments busy. If
1235 * we get to the MAX we declare it hung.
1236 */
1237 if (que->busy == IXGBE_QUEUE_HUNG) {
1238 ++hung;
1239 /* Mark the queue as inactive */
1240 adapter->active_queues &= ~((u64)1 << que->me);
1241 continue;
1242 } else {
1243 /* Check if we've come back from hung */
1244 if ((adapter->active_queues & ((u64)1 << que->me)) == 0)
1245 adapter->active_queues |= ((u64)1 << que->me);
1246 }
1247 if (que->busy >= IXGBE_MAX_TX_BUSY) {
1248 device_printf(dev,
1249 "Warning queue %d appears to be hung!\n", i);
1250 que->txr->busy = IXGBE_QUEUE_HUNG;
1251 ++hung;
1252 }
1253 }
1254
1255 /* Only truly watchdog if all queues show hung */
1256 if (hung == adapter->num_queues)
1257 goto watchdog;
1258 #if 0
1259 else if (queues != 0) { /* Force an IRQ on queues with work */
1260 ixv_rearm_queues(adapter, queues);
1261 }
1262 #endif
1263
1264 callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
1265
1266 return;
1267
1268 watchdog:
1269
1270 device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
1271 adapter->ifp->if_flags &= ~IFF_RUNNING;
1272 adapter->watchdog_events.ev_count++;
1273 ixv_init_locked(adapter);
1274 } /* ixv_local_timer */
1275
1276 /************************************************************************
1277 * ixv_update_link_status - Update OS on link state
1278 *
1279 * Note: Only updates the OS on the cached link state.
1280 * The real check of the hardware only happens with
1281 * a link interrupt.
1282 ************************************************************************/
1283 static void
1284 ixv_update_link_status(struct adapter *adapter)
1285 {
1286 struct ifnet *ifp = adapter->ifp;
1287 device_t dev = adapter->dev;
1288
1289 KASSERT(mutex_owned(&adapter->core_mtx));
1290
1291 if (adapter->link_up) {
1292 if (adapter->link_active != LINK_STATE_UP) {
1293 if (bootverbose) {
1294 const char *bpsmsg;
1295
1296 switch (adapter->link_speed) {
1297 case IXGBE_LINK_SPEED_10GB_FULL:
1298 bpsmsg = "10 Gbps";
1299 break;
1300 case IXGBE_LINK_SPEED_5GB_FULL:
1301 bpsmsg = "5 Gbps";
1302 break;
1303 case IXGBE_LINK_SPEED_2_5GB_FULL:
1304 bpsmsg = "2.5 Gbps";
1305 break;
1306 case IXGBE_LINK_SPEED_1GB_FULL:
1307 bpsmsg = "1 Gbps";
1308 break;
1309 case IXGBE_LINK_SPEED_100_FULL:
1310 bpsmsg = "100 Mbps";
1311 break;
1312 case IXGBE_LINK_SPEED_10_FULL:
1313 bpsmsg = "10 Mbps";
1314 break;
1315 default:
1316 bpsmsg = "unknown speed";
1317 break;
1318 }
1319 device_printf(dev, "Link is up %s %s \n",
1320 bpsmsg, "Full Duplex");
1321 }
1322 adapter->link_active = LINK_STATE_UP;
1323 if_link_state_change(ifp, LINK_STATE_UP);
1324 }
1325 } else {
1326 /*
1327 * Do it when link active changes to DOWN. i.e.
1328 * a) LINK_STATE_UNKNOWN -> LINK_STATE_DOWN
1329 * b) LINK_STATE_UP -> LINK_STATE_DOWN
1330 */
1331 if (adapter->link_active != LINK_STATE_DOWN) {
1332 if (bootverbose)
1333 device_printf(dev, "Link is Down\n");
1334 if_link_state_change(ifp, LINK_STATE_DOWN);
1335 adapter->link_active = LINK_STATE_DOWN;
1336 }
1337 }
1338 } /* ixv_update_link_status */
1339
1340
1341 /************************************************************************
1342 * ixv_stop - Stop the hardware
1343 *
1344 * Disables all traffic on the adapter by issuing a
1345 * global reset on the MAC and deallocates TX/RX buffers.
1346 ************************************************************************/
1347 static void
1348 ixv_ifstop(struct ifnet *ifp, int disable)
1349 {
1350 struct adapter *adapter = ifp->if_softc;
1351
1352 IXGBE_CORE_LOCK(adapter);
1353 ixv_stop(adapter);
1354 IXGBE_CORE_UNLOCK(adapter);
1355 }
1356
1357 static void
1358 ixv_stop(void *arg)
1359 {
1360 struct ifnet *ifp;
1361 struct adapter *adapter = arg;
1362 struct ixgbe_hw *hw = &adapter->hw;
1363
1364 ifp = adapter->ifp;
1365
1366 KASSERT(mutex_owned(&adapter->core_mtx));
1367
1368 INIT_DEBUGOUT("ixv_stop: begin\n");
1369 ixv_disable_intr(adapter);
1370
1371 /* Tell the stack that the interface is no longer active */
1372 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1373
1374 hw->mac.ops.reset_hw(hw);
1375 adapter->hw.adapter_stopped = FALSE;
1376 hw->mac.ops.stop_adapter(hw);
1377 callout_stop(&adapter->timer);
1378
1379 /* reprogram the RAR[0] in case user changed it. */
1380 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1381
1382 return;
1383 } /* ixv_stop */
1384
1385
1386 /************************************************************************
1387 * ixv_allocate_pci_resources
1388 ************************************************************************/
1389 static int
1390 ixv_allocate_pci_resources(struct adapter *adapter,
1391 const struct pci_attach_args *pa)
1392 {
1393 pcireg_t memtype, csr;
1394 device_t dev = adapter->dev;
1395 bus_addr_t addr;
1396 int flags;
1397
1398 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
1399 switch (memtype) {
1400 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1401 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1402 adapter->osdep.mem_bus_space_tag = pa->pa_memt;
1403 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
1404 memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
1405 goto map_err;
1406 if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
1407 aprint_normal_dev(dev, "clearing prefetchable bit\n");
1408 flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
1409 }
1410 if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
1411 adapter->osdep.mem_size, flags,
1412 &adapter->osdep.mem_bus_space_handle) != 0) {
1413 map_err:
1414 adapter->osdep.mem_size = 0;
1415 aprint_error_dev(dev, "unable to map BAR0\n");
1416 return ENXIO;
1417 }
1418 /*
1419 * Enable address decoding for memory range in case it's not
1420 * set.
1421 */
1422 csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
1423 PCI_COMMAND_STATUS_REG);
1424 csr |= PCI_COMMAND_MEM_ENABLE;
1425 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1426 csr);
1427 break;
1428 default:
1429 aprint_error_dev(dev, "unexpected type on BAR0\n");
1430 return ENXIO;
1431 }
1432
1433 /* Pick up the tuneable queues */
1434 adapter->num_queues = ixv_num_queues;
1435
1436 return (0);
1437 } /* ixv_allocate_pci_resources */
1438
1439 /************************************************************************
1440 * ixv_free_pci_resources
1441 ************************************************************************/
1442 static void
1443 ixv_free_pci_resources(struct adapter * adapter)
1444 {
1445 struct ix_queue *que = adapter->queues;
1446 int rid;
1447
1448 /*
1449 * Release all msix queue resources:
1450 */
1451 for (int i = 0; i < adapter->num_queues; i++, que++) {
1452 if (que->res != NULL)
1453 pci_intr_disestablish(adapter->osdep.pc,
1454 adapter->osdep.ihs[i]);
1455 }
1456
1457
1458 /* Clean the Mailbox interrupt last */
1459 rid = adapter->vector;
1460
1461 if (adapter->osdep.ihs[rid] != NULL) {
1462 pci_intr_disestablish(adapter->osdep.pc,
1463 adapter->osdep.ihs[rid]);
1464 adapter->osdep.ihs[rid] = NULL;
1465 }
1466
1467 pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
1468 adapter->osdep.nintrs);
1469
1470 if (adapter->osdep.mem_size != 0) {
1471 bus_space_unmap(adapter->osdep.mem_bus_space_tag,
1472 adapter->osdep.mem_bus_space_handle,
1473 adapter->osdep.mem_size);
1474 }
1475
1476 return;
1477 } /* ixv_free_pci_resources */
1478
1479 /************************************************************************
1480 * ixv_setup_interface
1481 *
1482 * Setup networking device structure and register an interface.
1483 ************************************************************************/
1484 static int
1485 ixv_setup_interface(device_t dev, struct adapter *adapter)
1486 {
1487 struct ethercom *ec = &adapter->osdep.ec;
1488 struct ifnet *ifp;
1489 int rv;
1490
1491 INIT_DEBUGOUT("ixv_setup_interface: begin");
1492
1493 ifp = adapter->ifp = &ec->ec_if;
1494 strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
1495 ifp->if_baudrate = IF_Gbps(10);
1496 ifp->if_init = ixv_init;
1497 ifp->if_stop = ixv_ifstop;
1498 ifp->if_softc = adapter;
1499 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1500 #ifdef IXGBE_MPSAFE
1501 ifp->if_extflags = IFEF_MPSAFE;
1502 #endif
1503 ifp->if_ioctl = ixv_ioctl;
1504 if (adapter->feat_en & IXGBE_FEATURE_LEGACY_TX) {
1505 #if 0
1506 ixv_start_locked = ixgbe_legacy_start_locked;
1507 #endif
1508 } else {
1509 ifp->if_transmit = ixgbe_mq_start;
1510 #if 0
1511 ixv_start_locked = ixgbe_mq_start_locked;
1512 #endif
1513 }
1514 ifp->if_start = ixgbe_legacy_start;
1515 IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
1516 IFQ_SET_READY(&ifp->if_snd);
1517
1518 rv = if_initialize(ifp);
1519 if (rv != 0) {
1520 aprint_error_dev(dev, "if_initialize failed(%d)\n", rv);
1521 return rv;
1522 }
1523 adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
1524 ether_ifattach(ifp, adapter->hw.mac.addr);
1525 /*
1526 * We use per TX queue softint, so if_deferred_start_init() isn't
1527 * used.
1528 */
1529 ether_set_ifflags_cb(ec, ixv_ifflags_cb);
1530
1531 adapter->max_frame_size = ifp->if_mtu + IXGBE_MTU_HDR;
1532
1533 /*
1534 * Tell the upper layer(s) we support long frames.
1535 */
1536 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1537
1538 /* Set capability flags */
1539 ifp->if_capabilities |= IFCAP_HWCSUM
1540 | IFCAP_TSOv4
1541 | IFCAP_TSOv6;
1542 ifp->if_capenable = 0;
1543
1544 ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER
1545 | ETHERCAP_VLAN_HWTAGGING
1546 | ETHERCAP_VLAN_HWCSUM
1547 | ETHERCAP_JUMBO_MTU
1548 | ETHERCAP_VLAN_MTU;
1549
1550 /* Enable the above capabilities by default */
1551 ec->ec_capenable = ec->ec_capabilities;
1552
1553 /* Don't enable LRO by default */
1554 #if 0
1555 /* NetBSD doesn't support LRO yet */
1556 ifp->if_capabilities |= IFCAP_LRO;
1557 #endif
1558
1559 /*
1560 * Specify the media types supported by this adapter and register
1561 * callbacks to update media and link information
1562 */
1563 ec->ec_ifmedia = &adapter->media;
1564 ifmedia_init(&adapter->media, IFM_IMASK, ixv_media_change,
1565 ixv_media_status);
1566 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1567 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1568
1569 if_register(ifp);
1570
1571 return 0;
1572 } /* ixv_setup_interface */
1573
1574
1575 /************************************************************************
1576 * ixv_initialize_transmit_units - Enable transmit unit.
1577 ************************************************************************/
1578 static void
1579 ixv_initialize_transmit_units(struct adapter *adapter)
1580 {
1581 struct tx_ring *txr = adapter->tx_rings;
1582 struct ixgbe_hw *hw = &adapter->hw;
1583 int i;
1584
1585 for (i = 0; i < adapter->num_queues; i++, txr++) {
1586 u64 tdba = txr->txdma.dma_paddr;
1587 u32 txctrl, txdctl;
1588 int j = txr->me;
1589
1590 /* Set WTHRESH to 8, burst writeback */
1591 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1592 txdctl |= (8 << 16);
1593 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1594
1595 /* Set the HW Tx Head and Tail indices */
1596 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDH(j), 0);
1597 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDT(j), 0);
1598
1599 /* Set Tx Tail register */
1600 txr->tail = IXGBE_VFTDT(j);
1601
1602 txr->txr_no_space = false;
1603
1604 /* Set Ring parameters */
1605 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
1606 (tdba & 0x00000000ffffffffULL));
1607 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
1608 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j),
1609 adapter->num_tx_desc * sizeof(struct ixgbe_legacy_tx_desc));
1610 txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
1611 txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
1612 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
1613
1614 /* Now enable */
1615 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
1616 txdctl |= IXGBE_TXDCTL_ENABLE;
1617 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
1618 }
1619
1620 return;
1621 } /* ixv_initialize_transmit_units */
1622
1623
1624 /************************************************************************
1625 * ixv_initialize_rss_mapping
1626 ************************************************************************/
1627 static void
1628 ixv_initialize_rss_mapping(struct adapter *adapter)
1629 {
1630 struct ixgbe_hw *hw = &adapter->hw;
1631 u32 reta = 0, mrqc, rss_key[10];
1632 int queue_id;
1633 int i, j;
1634 u32 rss_hash_config;
1635
1636 /* force use default RSS key. */
1637 #ifdef __NetBSD__
1638 rss_getkey((uint8_t *) &rss_key);
1639 #else
1640 if (adapter->feat_en & IXGBE_FEATURE_RSS) {
1641 /* Fetch the configured RSS key */
1642 rss_getkey((uint8_t *)&rss_key);
1643 } else {
1644 /* set up random bits */
1645 cprng_fast(&rss_key, sizeof(rss_key));
1646 }
1647 #endif
1648
1649 /* Now fill out hash function seeds */
1650 for (i = 0; i < 10; i++)
1651 IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), rss_key[i]);
1652
1653 /* Set up the redirection table */
1654 for (i = 0, j = 0; i < 64; i++, j++) {
1655 if (j == adapter->num_queues)
1656 j = 0;
1657
1658 if (adapter->feat_en & IXGBE_FEATURE_RSS) {
1659 /*
1660 * Fetch the RSS bucket id for the given indirection
1661 * entry. Cap it at the number of configured buckets
1662 * (which is num_queues.)
1663 */
1664 queue_id = rss_get_indirection_to_bucket(i);
1665 queue_id = queue_id % adapter->num_queues;
1666 } else
1667 queue_id = j;
1668
1669 /*
1670 * The low 8 bits are for hash value (n+0);
1671 * The next 8 bits are for hash value (n+1), etc.
1672 */
1673 reta >>= 8;
1674 reta |= ((uint32_t)queue_id) << 24;
1675 if ((i & 3) == 3) {
1676 IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), reta);
1677 reta = 0;
1678 }
1679 }
1680
1681 /* Perform hash on these packet types */
1682 if (adapter->feat_en & IXGBE_FEATURE_RSS)
1683 rss_hash_config = rss_gethashconfig();
1684 else {
1685 /*
1686 * Disable UDP - IP fragments aren't currently being handled
1687 * and so we end up with a mix of 2-tuple and 4-tuple
1688 * traffic.
1689 */
1690 rss_hash_config = RSS_HASHTYPE_RSS_IPV4
1691 | RSS_HASHTYPE_RSS_TCP_IPV4
1692 | RSS_HASHTYPE_RSS_IPV6
1693 | RSS_HASHTYPE_RSS_TCP_IPV6;
1694 }
1695
1696 mrqc = IXGBE_MRQC_RSSEN;
1697 if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
1698 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
1699 if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
1700 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
1701 if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
1702 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
1703 if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
1704 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
1705 if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
1706 device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_IPV6_EX defined, but not supported\n",
1707 __func__);
1708 if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
1709 device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_TCP_IPV6_EX defined, but not supported\n",
1710 __func__);
1711 if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
1712 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
1713 if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
1714 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
1715 if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
1716 device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_UDP_IPV6_EX defined, but not supported\n",
1717 __func__);
1718 IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, mrqc);
1719 } /* ixv_initialize_rss_mapping */
1720
1721
1722 /************************************************************************
1723 * ixv_initialize_receive_units - Setup receive registers and features.
1724 ************************************************************************/
1725 static void
1726 ixv_initialize_receive_units(struct adapter *adapter)
1727 {
1728 struct rx_ring *rxr = adapter->rx_rings;
1729 struct ixgbe_hw *hw = &adapter->hw;
1730 struct ifnet *ifp = adapter->ifp;
1731 u32 bufsz, psrtype;
1732
1733 if (ifp->if_mtu > ETHERMTU)
1734 bufsz = 4096 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1735 else
1736 bufsz = 2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1737
1738 psrtype = IXGBE_PSRTYPE_TCPHDR
1739 | IXGBE_PSRTYPE_UDPHDR
1740 | IXGBE_PSRTYPE_IPV4HDR
1741 | IXGBE_PSRTYPE_IPV6HDR
1742 | IXGBE_PSRTYPE_L2HDR;
1743
1744 if (adapter->num_queues > 1)
1745 psrtype |= 1 << 29;
1746
1747 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1748
1749 /* Tell PF our max_frame size */
1750 if (ixgbevf_rlpml_set_vf(hw, adapter->max_frame_size) != 0) {
1751 device_printf(adapter->dev, "There is a problem with the PF setup. It is likely the receive unit for this VF will not function correctly.\n");
1752 }
1753
1754 for (int i = 0; i < adapter->num_queues; i++, rxr++) {
1755 u64 rdba = rxr->rxdma.dma_paddr;
1756 u32 reg, rxdctl;
1757 int j = rxr->me;
1758
1759 /* Disable the queue */
1760 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
1761 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1762 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1763 for (int k = 0; k < 10; k++) {
1764 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) &
1765 IXGBE_RXDCTL_ENABLE)
1766 msec_delay(1);
1767 else
1768 break;
1769 }
1770 wmb();
1771 /* Setup the Base and Length of the Rx Descriptor Ring */
1772 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
1773 (rdba & 0x00000000ffffffffULL));
1774 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
1775 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j),
1776 adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
1777
1778 /* Reset the ring indices */
1779 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(rxr->me), 0);
1780 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), 0);
1781
1782 /* Set up the SRRCTL register */
1783 reg = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(j));
1784 reg &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1785 reg &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1786 reg |= bufsz;
1787 reg |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1788 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(j), reg);
1789
1790 /* Capture Rx Tail index */
1791 rxr->tail = IXGBE_VFRDT(rxr->me);
1792
1793 /* Do the queue enabling last */
1794 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1795 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
1796 for (int k = 0; k < 10; k++) {
1797 if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) &
1798 IXGBE_RXDCTL_ENABLE)
1799 break;
1800 msec_delay(1);
1801 }
1802 wmb();
1803
1804 /* Set the Tail Pointer */
1805 #ifdef DEV_NETMAP
1806 /*
1807 * In netmap mode, we must preserve the buffers made
1808 * available to userspace before the if_init()
1809 * (this is true by default on the TX side, because
1810 * init makes all buffers available to userspace).
1811 *
1812 * netmap_reset() and the device specific routines
1813 * (e.g. ixgbe_setup_receive_rings()) map these
1814 * buffers at the end of the NIC ring, so here we
1815 * must set the RDT (tail) register to make sure
1816 * they are not overwritten.
1817 *
1818 * In this driver the NIC ring starts at RDH = 0,
1819 * RDT points to the last slot available for reception (?),
1820 * so RDT = num_rx_desc - 1 means the whole ring is available.
1821 */
1822 if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
1823 (ifp->if_capenable & IFCAP_NETMAP)) {
1824 struct netmap_adapter *na = NA(adapter->ifp);
1825 struct netmap_kring *kring = na->rx_rings[i];
1826 int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
1827
1828 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), t);
1829 } else
1830 #endif /* DEV_NETMAP */
1831 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me),
1832 adapter->num_rx_desc - 1);
1833 }
1834
1835 ixv_initialize_rss_mapping(adapter);
1836 } /* ixv_initialize_receive_units */
1837
1838 /************************************************************************
1839 * ixv_sysctl_tdh_handler - Transmit Descriptor Head handler function
1840 *
1841 * Retrieves the TDH value from the hardware
1842 ************************************************************************/
1843 static int
1844 ixv_sysctl_tdh_handler(SYSCTLFN_ARGS)
1845 {
1846 struct sysctlnode node = *rnode;
1847 struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
1848 uint32_t val;
1849
1850 if (!txr)
1851 return (0);
1852
1853 val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_VFTDH(txr->me));
1854 node.sysctl_data = &val;
1855 return sysctl_lookup(SYSCTLFN_CALL(&node));
1856 } /* ixv_sysctl_tdh_handler */
1857
1858 /************************************************************************
1859 * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
1860 *
1861 * Retrieves the TDT value from the hardware
1862 ************************************************************************/
1863 static int
1864 ixv_sysctl_tdt_handler(SYSCTLFN_ARGS)
1865 {
1866 struct sysctlnode node = *rnode;
1867 struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
1868 uint32_t val;
1869
1870 if (!txr)
1871 return (0);
1872
1873 val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_VFTDT(txr->me));
1874 node.sysctl_data = &val;
1875 return sysctl_lookup(SYSCTLFN_CALL(&node));
1876 } /* ixv_sysctl_tdt_handler */
1877
1878 /************************************************************************
1879 * ixv_sysctl_next_to_check_handler - Receive Descriptor next to check
1880 * handler function
1881 *
1882 * Retrieves the next_to_check value
1883 ************************************************************************/
1884 static int
1885 ixv_sysctl_next_to_check_handler(SYSCTLFN_ARGS)
1886 {
1887 struct sysctlnode node = *rnode;
1888 struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
1889 uint32_t val;
1890
1891 if (!rxr)
1892 return (0);
1893
1894 val = rxr->next_to_check;
1895 node.sysctl_data = &val;
1896 return sysctl_lookup(SYSCTLFN_CALL(&node));
1897 } /* ixv_sysctl_next_to_check_handler */
1898
1899 /************************************************************************
1900 * ixv_sysctl_rdh_handler - Receive Descriptor Head handler function
1901 *
1902 * Retrieves the RDH value from the hardware
1903 ************************************************************************/
1904 static int
1905 ixv_sysctl_rdh_handler(SYSCTLFN_ARGS)
1906 {
1907 struct sysctlnode node = *rnode;
1908 struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
1909 uint32_t val;
1910
1911 if (!rxr)
1912 return (0);
1913
1914 val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_VFRDH(rxr->me));
1915 node.sysctl_data = &val;
1916 return sysctl_lookup(SYSCTLFN_CALL(&node));
1917 } /* ixv_sysctl_rdh_handler */
1918
1919 /************************************************************************
1920 * ixv_sysctl_rdt_handler - Receive Descriptor Tail handler function
1921 *
1922 * Retrieves the RDT value from the hardware
1923 ************************************************************************/
1924 static int
1925 ixv_sysctl_rdt_handler(SYSCTLFN_ARGS)
1926 {
1927 struct sysctlnode node = *rnode;
1928 struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
1929 uint32_t val;
1930
1931 if (!rxr)
1932 return (0);
1933
1934 val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_VFRDT(rxr->me));
1935 node.sysctl_data = &val;
1936 return sysctl_lookup(SYSCTLFN_CALL(&node));
1937 } /* ixv_sysctl_rdt_handler */
1938
1939 static void
1940 ixv_setup_vlan_tagging(struct adapter *adapter)
1941 {
1942 struct ethercom *ec = &adapter->osdep.ec;
1943 struct ixgbe_hw *hw = &adapter->hw;
1944 struct rx_ring *rxr;
1945 u32 ctrl;
1946 int i;
1947 bool hwtagging;
1948
1949 /* Enable HW tagging only if any vlan is attached */
1950 hwtagging = (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING)
1951 && VLAN_ATTACHED(ec);
1952
1953 /* Enable the queues */
1954 for (i = 0; i < adapter->num_queues; i++) {
1955 rxr = &adapter->rx_rings[i];
1956 ctrl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(rxr->me));
1957 if (hwtagging)
1958 ctrl |= IXGBE_RXDCTL_VME;
1959 else
1960 ctrl &= ~IXGBE_RXDCTL_VME;
1961 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(rxr->me), ctrl);
1962 /*
1963 * Let Rx path know that it needs to store VLAN tag
1964 * as part of extra mbuf info.
1965 */
1966 rxr->vtag_strip = hwtagging ? TRUE : FALSE;
1967 }
1968 } /* ixv_setup_vlan_tagging */
1969
1970 /************************************************************************
1971 * ixv_setup_vlan_support
1972 ************************************************************************/
1973 static int
1974 ixv_setup_vlan_support(struct adapter *adapter)
1975 {
1976 struct ethercom *ec = &adapter->osdep.ec;
1977 struct ixgbe_hw *hw = &adapter->hw;
1978 u32 vid, vfta, retry;
1979 struct vlanid_list *vlanidp;
1980 int rv, error = 0;
1981
1982 /*
1983 * This function is called from both if_init and ifflags_cb()
1984 * on NetBSD.
1985 */
1986
1987 /*
1988 * Part 1:
1989 * Setup VLAN HW tagging
1990 */
1991 ixv_setup_vlan_tagging(adapter);
1992
1993 if (!VLAN_ATTACHED(ec))
1994 return 0;
1995
1996 /*
1997 * Part 2:
1998 * Setup VLAN HW filter
1999 */
2000 /* Cleanup shadow_vfta */
2001 for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
2002 adapter->shadow_vfta[i] = 0;
2003 /* Generate shadow_vfta from ec_vids */
2004 ETHER_LOCK(ec);
2005 SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
2006 uint32_t idx;
2007
2008 idx = vlanidp->vid / 32;
2009 KASSERT(idx < IXGBE_VFTA_SIZE);
2010 adapter->shadow_vfta[idx] |= (u32)1 << (vlanidp->vid % 32);
2011 }
2012 ETHER_UNLOCK(ec);
2013
2014 /*
2015 * A soft reset zero's out the VFTA, so
2016 * we need to repopulate it now.
2017 */
2018 for (int i = 0; i < IXGBE_VFTA_SIZE; i++) {
2019 if (adapter->shadow_vfta[i] == 0)
2020 continue;
2021 vfta = adapter->shadow_vfta[i];
2022 /*
2023 * Reconstruct the vlan id's
2024 * based on the bits set in each
2025 * of the array ints.
2026 */
2027 for (int j = 0; j < 32; j++) {
2028 retry = 0;
2029 if ((vfta & ((u32)1 << j)) == 0)
2030 continue;
2031 vid = (i * 32) + j;
2032
2033 /* Call the shared code mailbox routine */
2034 while ((rv = hw->mac.ops.set_vfta(hw, vid, 0, TRUE,
2035 FALSE)) != 0) {
2036 if (++retry > 5) {
2037 device_printf(adapter->dev,
2038 "%s: max retry exceeded\n",
2039 __func__);
2040 break;
2041 }
2042 }
2043 if (rv != 0) {
2044 device_printf(adapter->dev,
2045 "failed to set vlan %d\n", vid);
2046 error = EACCES;
2047 }
2048 }
2049 }
2050 return error;
2051 } /* ixv_setup_vlan_support */
2052
2053 static int
2054 ixv_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
2055 {
2056 struct ifnet *ifp = &ec->ec_if;
2057 struct adapter *adapter = ifp->if_softc;
2058 int rv;
2059
2060 if (set)
2061 rv = ixv_register_vlan(adapter, vid);
2062 else
2063 rv = ixv_unregister_vlan(adapter, vid);
2064
2065 if (rv != 0)
2066 return rv;
2067
2068 /*
2069 * Control VLAN HW tagging when ec_nvlan is changed from 1 to 0
2070 * or 0 to 1.
2071 */
2072 if ((set && (ec->ec_nvlans == 1)) || (!set && (ec->ec_nvlans == 0)))
2073 ixv_setup_vlan_tagging(adapter);
2074
2075 return rv;
2076 }
2077
2078 /************************************************************************
2079 * ixv_register_vlan
2080 *
2081 * Run via a vlan config EVENT, it enables us to use the
2082 * HW Filter table since we can get the vlan id. This just
2083 * creates the entry in the soft version of the VFTA, init
2084 * will repopulate the real table.
2085 ************************************************************************/
2086 static int
2087 ixv_register_vlan(struct adapter *adapter, u16 vtag)
2088 {
2089 struct ixgbe_hw *hw = &adapter->hw;
2090 u16 index, bit;
2091 int error;
2092
2093 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
2094 return EINVAL;
2095 IXGBE_CORE_LOCK(adapter);
2096 index = (vtag >> 5) & 0x7F;
2097 bit = vtag & 0x1F;
2098 adapter->shadow_vfta[index] |= ((u32)1 << bit);
2099 error = hw->mac.ops.set_vfta(hw, vtag, 0, true, false);
2100 IXGBE_CORE_UNLOCK(adapter);
2101
2102 if (error != 0) {
2103 device_printf(adapter->dev, "failed to register vlan %hu\n",
2104 vtag);
2105 error = EACCES;
2106 }
2107 return error;
2108 } /* ixv_register_vlan */
2109
2110 /************************************************************************
2111 * ixv_unregister_vlan
2112 *
2113 * Run via a vlan unconfig EVENT, remove our entry
2114 * in the soft vfta.
2115 ************************************************************************/
2116 static int
2117 ixv_unregister_vlan(struct adapter *adapter, u16 vtag)
2118 {
2119 struct ixgbe_hw *hw = &adapter->hw;
2120 u16 index, bit;
2121 int error;
2122
2123 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
2124 return EINVAL;
2125
2126 IXGBE_CORE_LOCK(adapter);
2127 index = (vtag >> 5) & 0x7F;
2128 bit = vtag & 0x1F;
2129 adapter->shadow_vfta[index] &= ~((u32)1 << bit);
2130 error = hw->mac.ops.set_vfta(hw, vtag, 0, false, false);
2131 IXGBE_CORE_UNLOCK(adapter);
2132
2133 if (error != 0) {
2134 device_printf(adapter->dev, "failed to unregister vlan %hu\n",
2135 vtag);
2136 error = EIO;
2137 }
2138 return error;
2139 } /* ixv_unregister_vlan */
2140
2141 /************************************************************************
2142 * ixv_enable_intr
2143 ************************************************************************/
2144 static void
2145 ixv_enable_intr(struct adapter *adapter)
2146 {
2147 struct ixgbe_hw *hw = &adapter->hw;
2148 struct ix_queue *que = adapter->queues;
2149 u32 mask;
2150 int i;
2151
2152 /* For VTEIAC */
2153 mask = (1 << adapter->vector);
2154 for (i = 0; i < adapter->num_queues; i++, que++)
2155 mask |= (1 << que->msix);
2156 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
2157
2158 /* For VTEIMS */
2159 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, (1 << adapter->vector));
2160 que = adapter->queues;
2161 for (i = 0; i < adapter->num_queues; i++, que++)
2162 ixv_enable_queue(adapter, que->msix);
2163
2164 IXGBE_WRITE_FLUSH(hw);
2165 } /* ixv_enable_intr */
2166
2167 /************************************************************************
2168 * ixv_disable_intr
2169 ************************************************************************/
2170 static void
2171 ixv_disable_intr(struct adapter *adapter)
2172 {
2173 struct ix_queue *que = adapter->queues;
2174
2175 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIAC, 0);
2176
2177 /* disable interrupts other than queues */
2178 IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIMC, adapter->vector);
2179
2180 for (int i = 0; i < adapter->num_queues; i++, que++)
2181 ixv_disable_queue(adapter, que->msix);
2182
2183 IXGBE_WRITE_FLUSH(&adapter->hw);
2184 } /* ixv_disable_intr */
2185
2186 /************************************************************************
2187 * ixv_set_ivar
2188 *
2189 * Setup the correct IVAR register for a particular MSI-X interrupt
2190 * - entry is the register array entry
2191 * - vector is the MSI-X vector for this queue
2192 * - type is RX/TX/MISC
2193 ************************************************************************/
2194 static void
2195 ixv_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
2196 {
2197 struct ixgbe_hw *hw = &adapter->hw;
2198 u32 ivar, index;
2199
2200 vector |= IXGBE_IVAR_ALLOC_VAL;
2201
2202 if (type == -1) { /* MISC IVAR */
2203 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
2204 ivar &= ~0xFF;
2205 ivar |= vector;
2206 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
2207 } else { /* RX/TX IVARS */
2208 index = (16 * (entry & 1)) + (8 * type);
2209 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(entry >> 1));
2210 ivar &= ~(0xffUL << index);
2211 ivar |= ((u32)vector << index);
2212 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(entry >> 1), ivar);
2213 }
2214 } /* ixv_set_ivar */
2215
2216 /************************************************************************
2217 * ixv_configure_ivars
2218 ************************************************************************/
2219 static void
2220 ixv_configure_ivars(struct adapter *adapter)
2221 {
2222 struct ix_queue *que = adapter->queues;
2223
2224 /* XXX We should sync EITR value calculation with ixgbe.c? */
2225
2226 for (int i = 0; i < adapter->num_queues; i++, que++) {
2227 /* First the RX queue entry */
2228 ixv_set_ivar(adapter, i, que->msix, 0);
2229 /* ... and the TX */
2230 ixv_set_ivar(adapter, i, que->msix, 1);
2231 /* Set an initial value in EITR */
2232 ixv_eitr_write(adapter, que->msix, IXGBE_EITR_DEFAULT);
2233 }
2234
2235 /* For the mailbox interrupt */
2236 ixv_set_ivar(adapter, 1, adapter->vector, -1);
2237 } /* ixv_configure_ivars */
2238
2239
2240 /************************************************************************
2241 * ixv_save_stats
2242 *
2243 * The VF stats registers never have a truly virgin
2244 * starting point, so this routine tries to make an
2245 * artificial one, marking ground zero on attach as
2246 * it were.
2247 ************************************************************************/
2248 static void
2249 ixv_save_stats(struct adapter *adapter)
2250 {
2251 struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
2252
2253 if (stats->vfgprc.ev_count || stats->vfgptc.ev_count) {
2254 stats->saved_reset_vfgprc +=
2255 stats->vfgprc.ev_count - stats->base_vfgprc;
2256 stats->saved_reset_vfgptc +=
2257 stats->vfgptc.ev_count - stats->base_vfgptc;
2258 stats->saved_reset_vfgorc +=
2259 stats->vfgorc.ev_count - stats->base_vfgorc;
2260 stats->saved_reset_vfgotc +=
2261 stats->vfgotc.ev_count - stats->base_vfgotc;
2262 stats->saved_reset_vfmprc +=
2263 stats->vfmprc.ev_count - stats->base_vfmprc;
2264 }
2265 } /* ixv_save_stats */
2266
2267 /************************************************************************
2268 * ixv_init_stats
2269 ************************************************************************/
2270 static void
2271 ixv_init_stats(struct adapter *adapter)
2272 {
2273 struct ixgbe_hw *hw = &adapter->hw;
2274
2275 adapter->stats.vf.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2276 adapter->stats.vf.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2277 adapter->stats.vf.last_vfgorc |=
2278 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2279
2280 adapter->stats.vf.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2281 adapter->stats.vf.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2282 adapter->stats.vf.last_vfgotc |=
2283 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2284
2285 adapter->stats.vf.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2286
2287 adapter->stats.vf.base_vfgprc = adapter->stats.vf.last_vfgprc;
2288 adapter->stats.vf.base_vfgorc = adapter->stats.vf.last_vfgorc;
2289 adapter->stats.vf.base_vfgptc = adapter->stats.vf.last_vfgptc;
2290 adapter->stats.vf.base_vfgotc = adapter->stats.vf.last_vfgotc;
2291 adapter->stats.vf.base_vfmprc = adapter->stats.vf.last_vfmprc;
2292 } /* ixv_init_stats */
2293
2294 #define UPDATE_STAT_32(reg, last, count) \
2295 { \
2296 u32 current = IXGBE_READ_REG(hw, (reg)); \
2297 if (current < (last)) \
2298 count.ev_count += 0x100000000LL; \
2299 (last) = current; \
2300 count.ev_count &= 0xFFFFFFFF00000000LL; \
2301 count.ev_count |= current; \
2302 }
2303
2304 #define UPDATE_STAT_36(lsb, msb, last, count) \
2305 { \
2306 u64 cur_lsb = IXGBE_READ_REG(hw, (lsb)); \
2307 u64 cur_msb = IXGBE_READ_REG(hw, (msb)); \
2308 u64 current = ((cur_msb << 32) | cur_lsb); \
2309 if (current < (last)) \
2310 count.ev_count += 0x1000000000LL; \
2311 (last) = current; \
2312 count.ev_count &= 0xFFFFFFF000000000LL; \
2313 count.ev_count |= current; \
2314 }
2315
2316 /************************************************************************
2317 * ixv_update_stats - Update the board statistics counters.
2318 ************************************************************************/
2319 void
2320 ixv_update_stats(struct adapter *adapter)
2321 {
2322 struct ixgbe_hw *hw = &adapter->hw;
2323 struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
2324
2325 UPDATE_STAT_32(IXGBE_VFGPRC, stats->last_vfgprc, stats->vfgprc);
2326 UPDATE_STAT_32(IXGBE_VFGPTC, stats->last_vfgptc, stats->vfgptc);
2327 UPDATE_STAT_36(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB, stats->last_vfgorc,
2328 stats->vfgorc);
2329 UPDATE_STAT_36(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB, stats->last_vfgotc,
2330 stats->vfgotc);
2331 UPDATE_STAT_32(IXGBE_VFMPRC, stats->last_vfmprc, stats->vfmprc);
2332
2333 /* Fill out the OS statistics structure */
2334 /*
2335 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
2336 * adapter->stats counters. It's required to make ifconfig -z
2337 * (SOICZIFDATA) work.
2338 */
2339 } /* ixv_update_stats */
2340
2341 /************************************************************************
2342 * ixv_sysctl_interrupt_rate_handler
2343 ************************************************************************/
2344 static int
2345 ixv_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
2346 {
2347 struct sysctlnode node = *rnode;
2348 struct ix_queue *que = (struct ix_queue *)node.sysctl_data;
2349 struct adapter *adapter = que->adapter;
2350 uint32_t reg, usec, rate;
2351 int error;
2352
2353 if (que == NULL)
2354 return 0;
2355 reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_VTEITR(que->msix));
2356 usec = ((reg & 0x0FF8) >> 3);
2357 if (usec > 0)
2358 rate = 500000 / usec;
2359 else
2360 rate = 0;
2361 node.sysctl_data = &rate;
2362 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2363 if (error || newp == NULL)
2364 return error;
2365 reg &= ~0xfff; /* default, no limitation */
2366 if (rate > 0 && rate < 500000) {
2367 if (rate < 1000)
2368 rate = 1000;
2369 reg |= ((4000000/rate) & 0xff8);
2370 /*
2371 * When RSC is used, ITR interval must be larger than
2372 * RSC_DELAY. Currently, we use 2us for RSC_DELAY.
2373 * The minimum value is always greater than 2us on 100M
2374 * (and 10M?(not documented)), but it's not on 1G and higher.
2375 */
2376 if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
2377 && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
2378 if ((adapter->num_queues > 1)
2379 && (reg < IXGBE_MIN_RSC_EITR_10G1G))
2380 return EINVAL;
2381 }
2382 ixv_max_interrupt_rate = rate;
2383 } else
2384 ixv_max_interrupt_rate = 0;
2385 ixv_eitr_write(adapter, que->msix, reg);
2386
2387 return (0);
2388 } /* ixv_sysctl_interrupt_rate_handler */
2389
2390 const struct sysctlnode *
2391 ixv_sysctl_instance(struct adapter *adapter)
2392 {
2393 const char *dvname;
2394 struct sysctllog **log;
2395 int rc;
2396 const struct sysctlnode *rnode;
2397
2398 log = &adapter->sysctllog;
2399 dvname = device_xname(adapter->dev);
2400
2401 if ((rc = sysctl_createv(log, 0, NULL, &rnode,
2402 0, CTLTYPE_NODE, dvname,
2403 SYSCTL_DESCR("ixv information and settings"),
2404 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
2405 goto err;
2406
2407 return rnode;
2408 err:
2409 device_printf(adapter->dev,
2410 "%s: sysctl_createv failed, rc = %d\n", __func__, rc);
2411 return NULL;
2412 }
2413
2414 static void
2415 ixv_add_device_sysctls(struct adapter *adapter)
2416 {
2417 struct sysctllog **log;
2418 const struct sysctlnode *rnode, *cnode;
2419 device_t dev;
2420
2421 dev = adapter->dev;
2422 log = &adapter->sysctllog;
2423
2424 if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
2425 aprint_error_dev(dev, "could not create sysctl root\n");
2426 return;
2427 }
2428
2429 if (sysctl_createv(log, 0, &rnode, &cnode,
2430 CTLFLAG_READWRITE, CTLTYPE_INT,
2431 "debug", SYSCTL_DESCR("Debug Info"),
2432 ixv_sysctl_debug, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
2433 aprint_error_dev(dev, "could not create sysctl\n");
2434
2435 if (sysctl_createv(log, 0, &rnode, &cnode,
2436 CTLFLAG_READWRITE, CTLTYPE_BOOL,
2437 "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
2438 NULL, 0, &adapter->enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
2439 aprint_error_dev(dev, "could not create sysctl\n");
2440
2441 if (sysctl_createv(log, 0, &rnode, &cnode,
2442 CTLFLAG_READWRITE, CTLTYPE_BOOL,
2443 "txrx_workqueue", SYSCTL_DESCR("Use workqueue for packet processing"),
2444 NULL, 0, &adapter->txrx_use_workqueue, 0, CTL_CREATE, CTL_EOL) != 0)
2445 aprint_error_dev(dev, "could not create sysctl\n");
2446 }
2447
2448 /************************************************************************
2449 * ixv_add_stats_sysctls - Add statistic sysctls for the VF.
2450 ************************************************************************/
2451 static void
2452 ixv_add_stats_sysctls(struct adapter *adapter)
2453 {
2454 device_t dev = adapter->dev;
2455 struct tx_ring *txr = adapter->tx_rings;
2456 struct rx_ring *rxr = adapter->rx_rings;
2457 struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
2458 struct ixgbe_hw *hw = &adapter->hw;
2459 const struct sysctlnode *rnode, *cnode;
2460 struct sysctllog **log = &adapter->sysctllog;
2461 const char *xname = device_xname(dev);
2462
2463 /* Driver Statistics */
2464 evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
2465 NULL, xname, "Driver tx dma soft fail EFBIG");
2466 evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
2467 NULL, xname, "m_defrag() failed");
2468 evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
2469 NULL, xname, "Driver tx dma hard fail EFBIG");
2470 evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
2471 NULL, xname, "Driver tx dma hard fail EINVAL");
2472 evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
2473 NULL, xname, "Driver tx dma hard fail other");
2474 evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
2475 NULL, xname, "Driver tx dma soft fail EAGAIN");
2476 evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
2477 NULL, xname, "Driver tx dma soft fail ENOMEM");
2478 evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
2479 NULL, xname, "Watchdog timeouts");
2480 evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
2481 NULL, xname, "TSO errors");
2482 evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
2483 NULL, xname, "Link MSI-X IRQ Handled");
2484
2485 for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
2486 snprintf(adapter->queues[i].evnamebuf,
2487 sizeof(adapter->queues[i].evnamebuf), "%s q%d",
2488 xname, i);
2489 snprintf(adapter->queues[i].namebuf,
2490 sizeof(adapter->queues[i].namebuf), "q%d", i);
2491
2492 if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
2493 aprint_error_dev(dev, "could not create sysctl root\n");
2494 break;
2495 }
2496
2497 if (sysctl_createv(log, 0, &rnode, &rnode,
2498 0, CTLTYPE_NODE,
2499 adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
2500 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
2501 break;
2502
2503 if (sysctl_createv(log, 0, &rnode, &cnode,
2504 CTLFLAG_READWRITE, CTLTYPE_INT,
2505 "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
2506 ixv_sysctl_interrupt_rate_handler, 0,
2507 (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
2508 break;
2509
2510 if (sysctl_createv(log, 0, &rnode, &cnode,
2511 CTLFLAG_READONLY, CTLTYPE_INT,
2512 "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
2513 ixv_sysctl_tdh_handler, 0, (void *)txr,
2514 0, CTL_CREATE, CTL_EOL) != 0)
2515 break;
2516
2517 if (sysctl_createv(log, 0, &rnode, &cnode,
2518 CTLFLAG_READONLY, CTLTYPE_INT,
2519 "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
2520 ixv_sysctl_tdt_handler, 0, (void *)txr,
2521 0, CTL_CREATE, CTL_EOL) != 0)
2522 break;
2523
2524 evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
2525 NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
2526 evcnt_attach_dynamic(&adapter->queues[i].handleq,
2527 EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
2528 "Handled queue in softint");
2529 evcnt_attach_dynamic(&adapter->queues[i].req, EVCNT_TYPE_MISC,
2530 NULL, adapter->queues[i].evnamebuf, "Requeued in softint");
2531 evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
2532 NULL, adapter->queues[i].evnamebuf, "TSO");
2533 evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
2534 NULL, adapter->queues[i].evnamebuf,
2535 "Queue No Descriptor Available");
2536 evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
2537 NULL, adapter->queues[i].evnamebuf,
2538 "Queue Packets Transmitted");
2539 #ifndef IXGBE_LEGACY_TX
2540 evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
2541 NULL, adapter->queues[i].evnamebuf,
2542 "Packets dropped in pcq");
2543 #endif
2544
2545 #ifdef LRO
2546 struct lro_ctrl *lro = &rxr->lro;
2547 #endif /* LRO */
2548
2549 if (sysctl_createv(log, 0, &rnode, &cnode,
2550 CTLFLAG_READONLY,
2551 CTLTYPE_INT,
2552 "rxd_nxck", SYSCTL_DESCR("Receive Descriptor next to check"),
2553 ixv_sysctl_next_to_check_handler, 0, (void *)rxr, 0,
2554 CTL_CREATE, CTL_EOL) != 0)
2555 break;
2556
2557 if (sysctl_createv(log, 0, &rnode, &cnode,
2558 CTLFLAG_READONLY,
2559 CTLTYPE_INT,
2560 "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
2561 ixv_sysctl_rdh_handler, 0, (void *)rxr, 0,
2562 CTL_CREATE, CTL_EOL) != 0)
2563 break;
2564
2565 if (sysctl_createv(log, 0, &rnode, &cnode,
2566 CTLFLAG_READONLY,
2567 CTLTYPE_INT,
2568 "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
2569 ixv_sysctl_rdt_handler, 0, (void *)rxr, 0,
2570 CTL_CREATE, CTL_EOL) != 0)
2571 break;
2572
2573 evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
2574 NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
2575 evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
2576 NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
2577 evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
2578 NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
2579 evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
2580 NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
2581 evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
2582 NULL, adapter->queues[i].evnamebuf, "Rx discarded");
2583 #ifdef LRO
2584 SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
2585 CTLFLAG_RD, &lro->lro_queued, 0,
2586 "LRO Queued");
2587 SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
2588 CTLFLAG_RD, &lro->lro_flushed, 0,
2589 "LRO Flushed");
2590 #endif /* LRO */
2591 }
2592
2593 /* MAC stats get their own sub node */
2594
2595 snprintf(stats->namebuf,
2596 sizeof(stats->namebuf), "%s MAC Statistics", xname);
2597
2598 evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
2599 stats->namebuf, "rx csum offload - IP");
2600 evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
2601 stats->namebuf, "rx csum offload - L4");
2602 evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
2603 stats->namebuf, "rx csum offload - IP bad");
2604 evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
2605 stats->namebuf, "rx csum offload - L4 bad");
2606
2607 /* Packet Reception Stats */
2608 evcnt_attach_dynamic(&stats->vfgprc, EVCNT_TYPE_MISC, NULL,
2609 xname, "Good Packets Received");
2610 evcnt_attach_dynamic(&stats->vfgorc, EVCNT_TYPE_MISC, NULL,
2611 xname, "Good Octets Received");
2612 evcnt_attach_dynamic(&stats->vfmprc, EVCNT_TYPE_MISC, NULL,
2613 xname, "Multicast Packets Received");
2614 evcnt_attach_dynamic(&stats->vfgptc, EVCNT_TYPE_MISC, NULL,
2615 xname, "Good Packets Transmitted");
2616 evcnt_attach_dynamic(&stats->vfgotc, EVCNT_TYPE_MISC, NULL,
2617 xname, "Good Octets Transmitted");
2618
2619 /* Mailbox Stats */
2620 evcnt_attach_dynamic(&hw->mbx.stats.msgs_tx, EVCNT_TYPE_MISC, NULL,
2621 xname, "message TXs");
2622 evcnt_attach_dynamic(&hw->mbx.stats.msgs_rx, EVCNT_TYPE_MISC, NULL,
2623 xname, "message RXs");
2624 evcnt_attach_dynamic(&hw->mbx.stats.acks, EVCNT_TYPE_MISC, NULL,
2625 xname, "ACKs");
2626 evcnt_attach_dynamic(&hw->mbx.stats.reqs, EVCNT_TYPE_MISC, NULL,
2627 xname, "REQs");
2628 evcnt_attach_dynamic(&hw->mbx.stats.rsts, EVCNT_TYPE_MISC, NULL,
2629 xname, "RSTs");
2630
2631 } /* ixv_add_stats_sysctls */
2632
2633 /************************************************************************
2634 * ixv_set_sysctl_value
2635 ************************************************************************/
2636 static void
2637 ixv_set_sysctl_value(struct adapter *adapter, const char *name,
2638 const char *description, int *limit, int value)
2639 {
2640 device_t dev = adapter->dev;
2641 struct sysctllog **log;
2642 const struct sysctlnode *rnode, *cnode;
2643
2644 log = &adapter->sysctllog;
2645 if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
2646 aprint_error_dev(dev, "could not create sysctl root\n");
2647 return;
2648 }
2649 if (sysctl_createv(log, 0, &rnode, &cnode,
2650 CTLFLAG_READWRITE, CTLTYPE_INT,
2651 name, SYSCTL_DESCR(description),
2652 NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
2653 aprint_error_dev(dev, "could not create sysctl\n");
2654 *limit = value;
2655 } /* ixv_set_sysctl_value */
2656
2657 /************************************************************************
2658 * ixv_print_debug_info
2659 *
2660 * Called only when em_display_debug_stats is enabled.
2661 * Provides a way to take a look at important statistics
2662 * maintained by the driver and hardware.
2663 ************************************************************************/
2664 static void
2665 ixv_print_debug_info(struct adapter *adapter)
2666 {
2667 device_t dev = adapter->dev;
2668 struct ix_queue *que = adapter->queues;
2669 struct rx_ring *rxr;
2670 struct tx_ring *txr;
2671 #ifdef LRO
2672 struct lro_ctrl *lro;
2673 #endif /* LRO */
2674
2675 for (int i = 0; i < adapter->num_queues; i++, que++) {
2676 txr = que->txr;
2677 rxr = que->rxr;
2678 #ifdef LRO
2679 lro = &rxr->lro;
2680 #endif /* LRO */
2681 device_printf(dev, "QUE(%d) IRQs Handled: %lu\n",
2682 que->msix, (long)que->irqs.ev_count);
2683 device_printf(dev, "RX(%d) Packets Received: %lld\n",
2684 rxr->me, (long long)rxr->rx_packets.ev_count);
2685 device_printf(dev, "RX(%d) Bytes Received: %lu\n",
2686 rxr->me, (long)rxr->rx_bytes.ev_count);
2687 #ifdef LRO
2688 device_printf(dev, "RX(%d) LRO Queued= %ju\n",
2689 rxr->me, (uintmax_t)lro->lro_queued);
2690 device_printf(dev, "RX(%d) LRO Flushed= %ju\n",
2691 rxr->me, (uintmax_t)lro->lro_flushed);
2692 #endif /* LRO */
2693 device_printf(dev, "TX(%d) Packets Sent: %lu\n",
2694 txr->me, (long)txr->total_packets.ev_count);
2695 device_printf(dev, "TX(%d) NO Desc Avail: %lu\n",
2696 txr->me, (long)txr->no_desc_avail.ev_count);
2697 }
2698
2699 device_printf(dev, "MBX IRQ Handled: %lu\n",
2700 (long)adapter->link_irq.ev_count);
2701 } /* ixv_print_debug_info */
2702
2703 /************************************************************************
2704 * ixv_sysctl_debug
2705 ************************************************************************/
2706 static int
2707 ixv_sysctl_debug(SYSCTLFN_ARGS)
2708 {
2709 struct sysctlnode node = *rnode;
2710 struct adapter *adapter = (struct adapter *)node.sysctl_data;
2711 int error, result;
2712
2713 node.sysctl_data = &result;
2714 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2715
2716 if (error || newp == NULL)
2717 return error;
2718
2719 if (result == 1)
2720 ixv_print_debug_info(adapter);
2721
2722 return 0;
2723 } /* ixv_sysctl_debug */
2724
2725 /************************************************************************
2726 * ixv_init_device_features
2727 ************************************************************************/
2728 static void
2729 ixv_init_device_features(struct adapter *adapter)
2730 {
2731 adapter->feat_cap = IXGBE_FEATURE_NETMAP
2732 | IXGBE_FEATURE_VF
2733 | IXGBE_FEATURE_RSS
2734 | IXGBE_FEATURE_LEGACY_TX;
2735
2736 /* A tad short on feature flags for VFs, atm. */
2737 switch (adapter->hw.mac.type) {
2738 case ixgbe_mac_82599_vf:
2739 break;
2740 case ixgbe_mac_X540_vf:
2741 break;
2742 case ixgbe_mac_X550_vf:
2743 case ixgbe_mac_X550EM_x_vf:
2744 case ixgbe_mac_X550EM_a_vf:
2745 adapter->feat_cap |= IXGBE_FEATURE_NEEDS_CTXD;
2746 break;
2747 default:
2748 break;
2749 }
2750
2751 /* Enabled by default... */
2752 /* Is a virtual function (VF) */
2753 if (adapter->feat_cap & IXGBE_FEATURE_VF)
2754 adapter->feat_en |= IXGBE_FEATURE_VF;
2755 /* Netmap */
2756 if (adapter->feat_cap & IXGBE_FEATURE_NETMAP)
2757 adapter->feat_en |= IXGBE_FEATURE_NETMAP;
2758 /* Receive-Side Scaling (RSS) */
2759 if (adapter->feat_cap & IXGBE_FEATURE_RSS)
2760 adapter->feat_en |= IXGBE_FEATURE_RSS;
2761 /* Needs advanced context descriptor regardless of offloads req'd */
2762 if (adapter->feat_cap & IXGBE_FEATURE_NEEDS_CTXD)
2763 adapter->feat_en |= IXGBE_FEATURE_NEEDS_CTXD;
2764
2765 /* Enabled via sysctl... */
2766 /* Legacy (single queue) transmit */
2767 if ((adapter->feat_cap & IXGBE_FEATURE_LEGACY_TX) &&
2768 ixv_enable_legacy_tx)
2769 adapter->feat_en |= IXGBE_FEATURE_LEGACY_TX;
2770 } /* ixv_init_device_features */
2771
2772 /************************************************************************
2773 * ixv_shutdown - Shutdown entry point
2774 ************************************************************************/
2775 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
2776 static int
2777 ixv_shutdown(device_t dev)
2778 {
2779 struct adapter *adapter = device_private(dev);
2780 IXGBE_CORE_LOCK(adapter);
2781 ixv_stop(adapter);
2782 IXGBE_CORE_UNLOCK(adapter);
2783
2784 return (0);
2785 } /* ixv_shutdown */
2786 #endif
2787
2788 static int
2789 ixv_ifflags_cb(struct ethercom *ec)
2790 {
2791 struct ifnet *ifp = &ec->ec_if;
2792 struct adapter *adapter = ifp->if_softc;
2793 int change, rv = 0;
2794
2795 IXGBE_CORE_LOCK(adapter);
2796
2797 change = ifp->if_flags ^ adapter->if_flags;
2798 if (change != 0)
2799 adapter->if_flags = ifp->if_flags;
2800
2801 if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
2802 rv = ENETRESET;
2803 goto out;
2804 }
2805
2806 /* Check for ec_capenable. */
2807 change = ec->ec_capenable ^ adapter->ec_capenable;
2808 adapter->ec_capenable = ec->ec_capenable;
2809 if ((change & ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING
2810 | ETHERCAP_VLAN_HWFILTER)) != 0) {
2811 rv = ENETRESET;
2812 goto out;
2813 }
2814
2815 /*
2816 * Special handling is not required for ETHERCAP_VLAN_MTU.
2817 * PF's MAXFRS(MHADD) does not include the 4bytes of the VLAN header.
2818 */
2819
2820 /* Set up VLAN support and filter */
2821 if ((change & (ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_HWFILTER)) != 0)
2822 rv = ixv_setup_vlan_support(adapter);
2823
2824 out:
2825 IXGBE_CORE_UNLOCK(adapter);
2826
2827 return rv;
2828 }
2829
2830
2831 /************************************************************************
2832 * ixv_ioctl - Ioctl entry point
2833 *
2834 * Called when the user wants to configure the interface.
2835 *
2836 * return 0 on success, positive on failure
2837 ************************************************************************/
2838 static int
2839 ixv_ioctl(struct ifnet *ifp, u_long command, void *data)
2840 {
2841 struct adapter *adapter = ifp->if_softc;
2842 struct ifcapreq *ifcr = data;
2843 int error = 0;
2844 int l4csum_en;
2845 const int l4csum = IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
2846 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
2847
2848 switch (command) {
2849 case SIOCSIFFLAGS:
2850 IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
2851 break;
2852 case SIOCADDMULTI:
2853 case SIOCDELMULTI:
2854 IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
2855 break;
2856 case SIOCSIFMEDIA:
2857 case SIOCGIFMEDIA:
2858 IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
2859 break;
2860 case SIOCSIFCAP:
2861 IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
2862 break;
2863 case SIOCSIFMTU:
2864 IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
2865 break;
2866 default:
2867 IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)", (int)command);
2868 break;
2869 }
2870
2871 switch (command) {
2872 case SIOCSIFCAP:
2873 /* Layer-4 Rx checksum offload has to be turned on and
2874 * off as a unit.
2875 */
2876 l4csum_en = ifcr->ifcr_capenable & l4csum;
2877 if (l4csum_en != l4csum && l4csum_en != 0)
2878 return EINVAL;
2879 /*FALLTHROUGH*/
2880 case SIOCADDMULTI:
2881 case SIOCDELMULTI:
2882 case SIOCSIFFLAGS:
2883 case SIOCSIFMTU:
2884 default:
2885 if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
2886 return error;
2887 if ((ifp->if_flags & IFF_RUNNING) == 0)
2888 ;
2889 else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
2890 IXGBE_CORE_LOCK(adapter);
2891 ixv_init_locked(adapter);
2892 IXGBE_CORE_UNLOCK(adapter);
2893 } else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
2894 /*
2895 * Multicast list has changed; set the hardware filter
2896 * accordingly.
2897 */
2898 IXGBE_CORE_LOCK(adapter);
2899 ixv_disable_intr(adapter);
2900 ixv_set_multi(adapter);
2901 ixv_enable_intr(adapter);
2902 IXGBE_CORE_UNLOCK(adapter);
2903 }
2904 return 0;
2905 }
2906 } /* ixv_ioctl */
2907
2908 /************************************************************************
2909 * ixv_init
2910 ************************************************************************/
2911 static int
2912 ixv_init(struct ifnet *ifp)
2913 {
2914 struct adapter *adapter = ifp->if_softc;
2915
2916 IXGBE_CORE_LOCK(adapter);
2917 ixv_init_locked(adapter);
2918 IXGBE_CORE_UNLOCK(adapter);
2919
2920 return 0;
2921 } /* ixv_init */
2922
2923 /************************************************************************
2924 * ixv_handle_que
2925 ************************************************************************/
2926 static void
2927 ixv_handle_que(void *context)
2928 {
2929 struct ix_queue *que = context;
2930 struct adapter *adapter = que->adapter;
2931 struct tx_ring *txr = que->txr;
2932 struct ifnet *ifp = adapter->ifp;
2933 bool more;
2934
2935 que->handleq.ev_count++;
2936
2937 if (ifp->if_flags & IFF_RUNNING) {
2938 more = ixgbe_rxeof(que);
2939 IXGBE_TX_LOCK(txr);
2940 more |= ixgbe_txeof(txr);
2941 if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
2942 if (!ixgbe_mq_ring_empty(ifp, txr->txr_interq))
2943 ixgbe_mq_start_locked(ifp, txr);
2944 /* Only for queue 0 */
2945 /* NetBSD still needs this for CBQ */
2946 if ((&adapter->queues[0] == que)
2947 && (!ixgbe_legacy_ring_empty(ifp, NULL)))
2948 ixgbe_legacy_start_locked(ifp, txr);
2949 IXGBE_TX_UNLOCK(txr);
2950 if (more) {
2951 que->req.ev_count++;
2952 if (adapter->txrx_use_workqueue) {
2953 /*
2954 * "enqueued flag" is not required here
2955 * the same as ixg(4). See ixgbe_msix_que().
2956 */
2957 workqueue_enqueue(adapter->que_wq,
2958 &que->wq_cookie, curcpu());
2959 } else
2960 softint_schedule(que->que_si);
2961 return;
2962 }
2963 }
2964
2965 /* Re-enable this interrupt */
2966 ixv_enable_queue(adapter, que->msix);
2967
2968 return;
2969 } /* ixv_handle_que */
2970
2971 /************************************************************************
2972 * ixv_handle_que_work
2973 ************************************************************************/
2974 static void
2975 ixv_handle_que_work(struct work *wk, void *context)
2976 {
2977 struct ix_queue *que = container_of(wk, struct ix_queue, wq_cookie);
2978
2979 /*
2980 * "enqueued flag" is not required here the same as ixg(4).
2981 * See ixgbe_msix_que().
2982 */
2983 ixv_handle_que(que);
2984 }
2985
2986 /************************************************************************
2987 * ixv_allocate_msix - Setup MSI-X Interrupt resources and handlers
2988 ************************************************************************/
2989 static int
2990 ixv_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
2991 {
2992 device_t dev = adapter->dev;
2993 struct ix_queue *que = adapter->queues;
2994 struct tx_ring *txr = adapter->tx_rings;
2995 int error, msix_ctrl, rid, vector = 0;
2996 pci_chipset_tag_t pc;
2997 pcitag_t tag;
2998 char intrbuf[PCI_INTRSTR_LEN];
2999 char wqname[MAXCOMLEN];
3000 char intr_xname[32];
3001 const char *intrstr = NULL;
3002 kcpuset_t *affinity;
3003 int cpu_id = 0;
3004
3005 pc = adapter->osdep.pc;
3006 tag = adapter->osdep.tag;
3007
3008 adapter->osdep.nintrs = adapter->num_queues + 1;
3009 if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
3010 adapter->osdep.nintrs) != 0) {
3011 aprint_error_dev(dev,
3012 "failed to allocate MSI-X interrupt\n");
3013 return (ENXIO);
3014 }
3015
3016 kcpuset_create(&affinity, false);
3017 for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
3018 snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
3019 device_xname(dev), i);
3020 intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
3021 sizeof(intrbuf));
3022 #ifdef IXGBE_MPSAFE
3023 pci_intr_setattr(pc, &adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
3024 true);
3025 #endif
3026 /* Set the handler function */
3027 que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
3028 adapter->osdep.intrs[i], IPL_NET, ixv_msix_que, que,
3029 intr_xname);
3030 if (que->res == NULL) {
3031 pci_intr_release(pc, adapter->osdep.intrs,
3032 adapter->osdep.nintrs);
3033 aprint_error_dev(dev,
3034 "Failed to register QUE handler\n");
3035 kcpuset_destroy(affinity);
3036 return (ENXIO);
3037 }
3038 que->msix = vector;
3039 adapter->active_queues |= (u64)(1 << que->msix);
3040
3041 cpu_id = i;
3042 /* Round-robin affinity */
3043 kcpuset_zero(affinity);
3044 kcpuset_set(affinity, cpu_id % ncpu);
3045 error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
3046 NULL);
3047 aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
3048 intrstr);
3049 if (error == 0)
3050 aprint_normal(", bound queue %d to cpu %d\n",
3051 i, cpu_id % ncpu);
3052 else
3053 aprint_normal("\n");
3054
3055 #ifndef IXGBE_LEGACY_TX
3056 txr->txr_si
3057 = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
3058 ixgbe_deferred_mq_start, txr);
3059 #endif
3060 que->que_si
3061 = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
3062 ixv_handle_que, que);
3063 if (que->que_si == NULL) {
3064 aprint_error_dev(dev,
3065 "could not establish software interrupt\n");
3066 }
3067 }
3068 snprintf(wqname, sizeof(wqname), "%sdeferTx", device_xname(dev));
3069 error = workqueue_create(&adapter->txr_wq, wqname,
3070 ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
3071 IXGBE_WORKQUEUE_FLAGS);
3072 if (error) {
3073 aprint_error_dev(dev, "couldn't create workqueue for deferred Tx\n");
3074 }
3075 adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
3076
3077 snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
3078 error = workqueue_create(&adapter->que_wq, wqname,
3079 ixv_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
3080 IXGBE_WORKQUEUE_FLAGS);
3081 if (error) {
3082 aprint_error_dev(dev,
3083 "couldn't create workqueue\n");
3084 }
3085
3086 /* and Mailbox */
3087 cpu_id++;
3088 snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
3089 adapter->vector = vector;
3090 intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
3091 sizeof(intrbuf));
3092 #ifdef IXGBE_MPSAFE
3093 pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
3094 true);
3095 #endif
3096 /* Set the mbx handler function */
3097 adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
3098 adapter->osdep.intrs[vector], IPL_NET, ixv_msix_mbx, adapter,
3099 intr_xname);
3100 if (adapter->osdep.ihs[vector] == NULL) {
3101 aprint_error_dev(dev, "Failed to register LINK handler\n");
3102 kcpuset_destroy(affinity);
3103 return (ENXIO);
3104 }
3105 /* Round-robin affinity */
3106 kcpuset_zero(affinity);
3107 kcpuset_set(affinity, cpu_id % ncpu);
3108 error = interrupt_distribute(adapter->osdep.ihs[vector],
3109 affinity, NULL);
3110
3111 aprint_normal_dev(dev,
3112 "for link, interrupting at %s", intrstr);
3113 if (error == 0)
3114 aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
3115 else
3116 aprint_normal("\n");
3117
3118 /* Tasklets for Mailbox */
3119 adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINFT_FLAGS,
3120 ixv_handle_link, adapter);
3121 /*
3122 * Due to a broken design QEMU will fail to properly
3123 * enable the guest for MSI-X unless the vectors in
3124 * the table are all set up, so we must rewrite the
3125 * ENABLE in the MSI-X control register again at this
3126 * point to cause it to successfully initialize us.
3127 */
3128 if (adapter->hw.mac.type == ixgbe_mac_82599_vf) {
3129 pci_get_capability(pc, tag, PCI_CAP_MSIX, &rid, NULL);
3130 rid += PCI_MSIX_CTL;
3131 msix_ctrl = pci_conf_read(pc, tag, rid);
3132 msix_ctrl |= PCI_MSIX_CTL_ENABLE;
3133 pci_conf_write(pc, tag, rid, msix_ctrl);
3134 }
3135
3136 kcpuset_destroy(affinity);
3137 return (0);
3138 } /* ixv_allocate_msix */
3139
3140 /************************************************************************
3141 * ixv_configure_interrupts - Setup MSI-X resources
3142 *
3143 * Note: The VF device MUST use MSI-X, there is no fallback.
3144 ************************************************************************/
3145 static int
3146 ixv_configure_interrupts(struct adapter *adapter)
3147 {
3148 device_t dev = adapter->dev;
3149 int want, queues, msgs;
3150
3151 /* Must have at least 2 MSI-X vectors */
3152 msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
3153 if (msgs < 2) {
3154 aprint_error_dev(dev, "MSIX config error\n");
3155 return (ENXIO);
3156 }
3157 msgs = MIN(msgs, IXG_MAX_NINTR);
3158
3159 /* Figure out a reasonable auto config value */
3160 queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
3161
3162 if (ixv_num_queues != 0)
3163 queues = ixv_num_queues;
3164 else if ((ixv_num_queues == 0) && (queues > IXGBE_VF_MAX_TX_QUEUES))
3165 queues = IXGBE_VF_MAX_TX_QUEUES;
3166
3167 /*
3168 * Want vectors for the queues,
3169 * plus an additional for mailbox.
3170 */
3171 want = queues + 1;
3172 if (msgs >= want)
3173 msgs = want;
3174 else {
3175 aprint_error_dev(dev,
3176 "MSI-X Configuration Problem, "
3177 "%d vectors but %d queues wanted!\n",
3178 msgs, want);
3179 return -1;
3180 }
3181
3182 adapter->msix_mem = (void *)1; /* XXX */
3183 aprint_normal_dev(dev,
3184 "Using MSI-X interrupts with %d vectors\n", msgs);
3185 adapter->num_queues = queues;
3186
3187 return (0);
3188 } /* ixv_configure_interrupts */
3189
3190
3191 /************************************************************************
3192 * ixv_handle_link - Tasklet handler for MSI-X MBX interrupts
3193 *
3194 * Done outside of interrupt context since the driver might sleep
3195 ************************************************************************/
3196 static void
3197 ixv_handle_link(void *context)
3198 {
3199 struct adapter *adapter = context;
3200
3201 IXGBE_CORE_LOCK(adapter);
3202
3203 adapter->hw.mac.ops.check_link(&adapter->hw, &adapter->link_speed,
3204 &adapter->link_up, FALSE);
3205 ixv_update_link_status(adapter);
3206
3207 IXGBE_CORE_UNLOCK(adapter);
3208 } /* ixv_handle_link */
3209
3210 /************************************************************************
3211 * ixv_check_link - Used in the local timer to poll for link changes
3212 ************************************************************************/
3213 static s32
3214 ixv_check_link(struct adapter *adapter)
3215 {
3216 s32 error;
3217
3218 KASSERT(mutex_owned(&adapter->core_mtx));
3219
3220 adapter->hw.mac.get_link_status = TRUE;
3221
3222 error = adapter->hw.mac.ops.check_link(&adapter->hw,
3223 &adapter->link_speed, &adapter->link_up, FALSE);
3224 ixv_update_link_status(adapter);
3225
3226 return error;
3227 } /* ixv_check_link */
3228