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