1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4 * Copyright(c) 2013 - 2016 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 ******************************************************************************/
28 #include "i40e_prototype.h"
29 #include "i40evf_client.h"
30 /* All i40evf tracepoints are defined by the include below, which must
31 * be included exactly once across the whole kernel with
32 * CREATE_TRACE_POINTS defined
34 #define CREATE_TRACE_POINTS
35 #include "i40e_trace.h"
37 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39 static int i40evf_close(struct net_device *netdev);
41 char i40evf_driver_name[] = "i40evf";
42 static const char i40evf_driver_string[] =
43 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
47 #define DRV_VERSION_MAJOR 3
48 #define DRV_VERSION_MINOR 0
49 #define DRV_VERSION_BUILD 0
50 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) \
54 const char i40evf_driver_version[] = DRV_VERSION;
55 static const char i40evf_copyright[] =
56 "Copyright (c) 2013 - 2015 Intel Corporation.";
58 /* i40evf_pci_tbl - PCI Device ID Table
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
66 static const struct pci_device_id i40evf_pci_tbl[] = {
67 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
68 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
69 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
70 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
71 /* required last entry */
75 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
77 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(DRV_VERSION);
82 static struct workqueue_struct *i40evf_wq;
85 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
86 * @hw: pointer to the HW structure
87 * @mem: ptr to mem struct to fill out
88 * @size: size of memory requested
89 * @alignment: what to align the allocation to
91 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92 struct i40e_dma_mem *mem,
93 u64 size, u32 alignment)
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
98 return I40E_ERR_PARAM;
100 mem->size = ALIGN(size, alignment);
101 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
102 (dma_addr_t *)&mem->pa, GFP_KERNEL);
106 return I40E_ERR_NO_MEMORY;
110 * i40evf_free_dma_mem_d - OS specific memory free for shared code
111 * @hw: pointer to the HW structure
112 * @mem: ptr to mem struct to free
114 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
116 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
118 if (!mem || !mem->va)
119 return I40E_ERR_PARAM;
120 dma_free_coherent(&adapter->pdev->dev, mem->size,
121 mem->va, (dma_addr_t)mem->pa);
126 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to fill out
129 * @size: size of memory requested
131 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem, u32 size)
135 return I40E_ERR_PARAM;
138 mem->va = kzalloc(size, GFP_KERNEL);
143 return I40E_ERR_NO_MEMORY;
147 * i40evf_free_virt_mem_d - OS specific memory free for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to free
151 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152 struct i40e_virt_mem *mem)
155 return I40E_ERR_PARAM;
157 /* it's ok to kfree a NULL pointer */
164 * i40evf_debug_d - OS dependent version of debug printing
165 * @hw: pointer to the HW structure
166 * @mask: debug level mask
167 * @fmt_str: printf-type format description
169 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
174 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
177 va_start(argptr, fmt_str);
178 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
181 /* the debug string is already formatted with a newline */
186 * i40evf_schedule_reset - Set the flags and schedule a reset event
187 * @adapter: board private structure
189 void i40evf_schedule_reset(struct i40evf_adapter *adapter)
191 if (!(adapter->flags &
192 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
193 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
194 schedule_work(&adapter->reset_task);
199 * i40evf_tx_timeout - Respond to a Tx Hang
200 * @netdev: network interface device structure
202 static void i40evf_tx_timeout(struct net_device *netdev)
204 struct i40evf_adapter *adapter = netdev_priv(netdev);
206 adapter->tx_timeout_count++;
207 i40evf_schedule_reset(adapter);
211 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212 * @adapter: board private structure
214 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
216 struct i40e_hw *hw = &adapter->hw;
218 if (!adapter->msix_entries)
221 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
224 rd32(hw, I40E_VFGEN_RSTAT);
226 synchronize_irq(adapter->msix_entries[0].vector);
230 * i40evf_misc_irq_enable - Enable default interrupt generation settings
231 * @adapter: board private structure
233 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
235 struct i40e_hw *hw = &adapter->hw;
237 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
239 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
242 rd32(hw, I40E_VFGEN_RSTAT);
246 * i40evf_irq_disable - Mask off interrupt generation on the NIC
247 * @adapter: board private structure
249 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
252 struct i40e_hw *hw = &adapter->hw;
254 if (!adapter->msix_entries)
257 for (i = 1; i < adapter->num_msix_vectors; i++) {
258 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
259 synchronize_irq(adapter->msix_entries[i].vector);
262 rd32(hw, I40E_VFGEN_RSTAT);
266 * i40evf_irq_enable_queues - Enable interrupt for specified queues
267 * @adapter: board private structure
268 * @mask: bitmap of queues to enable
270 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
272 struct i40e_hw *hw = &adapter->hw;
275 for (i = 1; i < adapter->num_msix_vectors; i++) {
276 if (mask & BIT(i - 1)) {
277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
279 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
280 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
286 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
287 * @adapter: board private structure
288 * @mask: bitmap of vectors to trigger
290 static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
292 struct i40e_hw *hw = &adapter->hw;
297 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
298 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
299 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
300 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
301 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
303 for (i = 1; i < adapter->num_msix_vectors; i++) {
305 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
306 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
307 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
308 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
309 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
315 * i40evf_irq_enable - Enable default interrupt generation settings
316 * @adapter: board private structure
317 * @flush: boolean value whether to run rd32()
319 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
321 struct i40e_hw *hw = &adapter->hw;
323 i40evf_misc_irq_enable(adapter);
324 i40evf_irq_enable_queues(adapter, ~0);
327 rd32(hw, I40E_VFGEN_RSTAT);
331 * i40evf_msix_aq - Interrupt handler for vector 0
332 * @irq: interrupt number
333 * @data: pointer to netdev
335 static irqreturn_t i40evf_msix_aq(int irq, void *data)
337 struct net_device *netdev = data;
338 struct i40evf_adapter *adapter = netdev_priv(netdev);
339 struct i40e_hw *hw = &adapter->hw;
342 /* handle non-queue interrupts, these reads clear the registers */
343 val = rd32(hw, I40E_VFINT_ICR01);
344 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
346 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
347 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
348 wr32(hw, I40E_VFINT_DYN_CTL01, val);
350 /* schedule work on the private workqueue */
351 schedule_work(&adapter->adminq_task);
357 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
358 * @irq: interrupt number
359 * @data: pointer to a q_vector
361 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
363 struct i40e_q_vector *q_vector = data;
365 if (!q_vector->tx.ring && !q_vector->rx.ring)
368 napi_schedule_irqoff(&q_vector->napi);
374 * i40evf_map_vector_to_rxq - associate irqs with rx queues
375 * @adapter: board private structure
376 * @v_idx: interrupt number
377 * @r_idx: queue number
380 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
382 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
383 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
384 struct i40e_hw *hw = &adapter->hw;
386 rx_ring->q_vector = q_vector;
387 rx_ring->next = q_vector->rx.ring;
388 rx_ring->vsi = &adapter->vsi;
389 q_vector->rx.ring = rx_ring;
390 q_vector->rx.count++;
391 q_vector->rx.latency_range = I40E_LOW_LATENCY;
392 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
393 q_vector->ring_mask |= BIT(r_idx);
394 q_vector->itr_countdown = ITR_COUNTDOWN_START;
395 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
399 * i40evf_map_vector_to_txq - associate irqs with tx queues
400 * @adapter: board private structure
401 * @v_idx: interrupt number
402 * @t_idx: queue number
405 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
407 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
408 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
409 struct i40e_hw *hw = &adapter->hw;
411 tx_ring->q_vector = q_vector;
412 tx_ring->next = q_vector->tx.ring;
413 tx_ring->vsi = &adapter->vsi;
414 q_vector->tx.ring = tx_ring;
415 q_vector->tx.count++;
416 q_vector->tx.latency_range = I40E_LOW_LATENCY;
417 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
418 q_vector->itr_countdown = ITR_COUNTDOWN_START;
419 q_vector->num_ringpairs++;
420 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
424 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
425 * @adapter: board private structure to initialize
427 * This function maps descriptor rings to the queue-specific vectors
428 * we were allotted through the MSI-X enabling code. Ideally, we'd have
429 * one vector per ring/queue, but on a constrained vector budget, we
430 * group the rings as "efficiently" as possible. You would add new
431 * mapping configurations in here.
433 static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
437 int rxr_idx = 0, txr_idx = 0;
438 int rxr_remaining = adapter->num_active_queues;
439 int txr_remaining = adapter->num_active_queues;
444 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
446 /* The ideal configuration...
447 * We have enough vectors to map one per queue.
449 if (q_vectors >= (rxr_remaining * 2)) {
450 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
451 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
453 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
454 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
458 /* If we don't have enough vectors for a 1-to-1
459 * mapping, we'll have to group them so there are
460 * multiple queues per vector.
461 * Re-adjusting *qpv takes care of the remainder.
463 for (i = v_start; i < q_vectors; i++) {
464 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
465 for (j = 0; j < rqpv; j++) {
466 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
471 for (i = v_start; i < q_vectors; i++) {
472 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
473 for (j = 0; j < tqpv; j++) {
474 i40evf_map_vector_to_txq(adapter, i, txr_idx);
481 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
486 #ifdef CONFIG_NET_POLL_CONTROLLER
488 * i40evf_netpoll - A Polling 'interrupt' handler
489 * @netdev: network interface device structure
491 * This is used by netconsole to send skbs without having to re-enable
492 * interrupts. It's not called while the normal interrupt routine is executing.
494 static void i40evf_netpoll(struct net_device *netdev)
496 struct i40evf_adapter *adapter = netdev_priv(netdev);
497 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
500 /* if interface is down do nothing */
501 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
504 for (i = 0; i < q_vectors; i++)
505 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
510 * i40evf_irq_affinity_notify - Callback for affinity changes
511 * @notify: context as to what irq was changed
512 * @mask: the new affinity mask
514 * This is a callback function used by the irq_set_affinity_notifier function
515 * so that we may register to receive changes to the irq affinity masks.
517 static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
518 const cpumask_t *mask)
520 struct i40e_q_vector *q_vector =
521 container_of(notify, struct i40e_q_vector, affinity_notify);
523 cpumask_copy(&q_vector->affinity_mask, mask);
527 * i40evf_irq_affinity_release - Callback for affinity notifier release
528 * @ref: internal core kernel usage
530 * This is a callback function used by the irq_set_affinity_notifier function
531 * to inform the current notification subscriber that they will no longer
532 * receive notifications.
534 static void i40evf_irq_affinity_release(struct kref *ref) {}
537 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
538 * @adapter: board private structure
540 * Allocates MSI-X vectors for tx and rx handling, and requests
541 * interrupts from the kernel.
544 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
546 unsigned int vector, q_vectors;
547 unsigned int rx_int_idx = 0, tx_int_idx = 0;
551 i40evf_irq_disable(adapter);
552 /* Decrement for Other and TCP Timer vectors */
553 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
555 for (vector = 0; vector < q_vectors; vector++) {
556 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
557 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
559 if (q_vector->tx.ring && q_vector->rx.ring) {
560 snprintf(q_vector->name, sizeof(q_vector->name),
561 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
563 } else if (q_vector->rx.ring) {
564 snprintf(q_vector->name, sizeof(q_vector->name),
565 "i40evf-%s-rx-%d", basename, rx_int_idx++);
566 } else if (q_vector->tx.ring) {
567 snprintf(q_vector->name, sizeof(q_vector->name),
568 "i40evf-%s-tx-%d", basename, tx_int_idx++);
570 /* skip this unused q_vector */
573 err = request_irq(irq_num,
574 i40evf_msix_clean_rings,
579 dev_info(&adapter->pdev->dev,
580 "Request_irq failed, error: %d\n", err);
581 goto free_queue_irqs;
583 /* register for affinity change notifications */
584 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
585 q_vector->affinity_notify.release =
586 i40evf_irq_affinity_release;
587 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
588 /* Spread the IRQ affinity hints across online CPUs. Note that
589 * get_cpu_mask returns a mask with a permanent lifetime so
590 * it's safe to use as a hint for irq_set_affinity_hint.
592 cpu = cpumask_local_spread(q_vector->v_idx, -1);
593 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
601 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
602 irq_set_affinity_notifier(irq_num, NULL);
603 irq_set_affinity_hint(irq_num, NULL);
604 free_irq(irq_num, &adapter->q_vectors[vector]);
610 * i40evf_request_misc_irq - Initialize MSI-X interrupts
611 * @adapter: board private structure
613 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
614 * vector is only for the admin queue, and stays active even when the netdev
617 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
619 struct net_device *netdev = adapter->netdev;
622 snprintf(adapter->misc_vector_name,
623 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
624 dev_name(&adapter->pdev->dev));
625 err = request_irq(adapter->msix_entries[0].vector,
627 adapter->misc_vector_name, netdev);
629 dev_err(&adapter->pdev->dev,
630 "request_irq for %s failed: %d\n",
631 adapter->misc_vector_name, err);
632 free_irq(adapter->msix_entries[0].vector, netdev);
638 * i40evf_free_traffic_irqs - Free MSI-X interrupts
639 * @adapter: board private structure
641 * Frees all MSI-X vectors other than 0.
643 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
645 int vector, irq_num, q_vectors;
647 if (!adapter->msix_entries)
650 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
652 for (vector = 0; vector < q_vectors; vector++) {
653 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
654 irq_set_affinity_notifier(irq_num, NULL);
655 irq_set_affinity_hint(irq_num, NULL);
656 free_irq(irq_num, &adapter->q_vectors[vector]);
661 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
662 * @adapter: board private structure
664 * Frees MSI-X vector 0.
666 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
668 struct net_device *netdev = adapter->netdev;
670 if (!adapter->msix_entries)
673 free_irq(adapter->msix_entries[0].vector, netdev);
677 * i40evf_configure_tx - Configure Transmit Unit after Reset
678 * @adapter: board private structure
680 * Configure the Tx unit of the MAC after a reset.
682 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
684 struct i40e_hw *hw = &adapter->hw;
687 for (i = 0; i < adapter->num_active_queues; i++)
688 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
692 * i40evf_configure_rx - Configure Receive Unit after Reset
693 * @adapter: board private structure
695 * Configure the Rx unit of the MAC after a reset.
697 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
699 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
700 struct i40e_hw *hw = &adapter->hw;
703 /* Legacy Rx will always default to a 2048 buffer size. */
704 #if (PAGE_SIZE < 8192)
705 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
706 struct net_device *netdev = adapter->netdev;
708 /* For jumbo frames on systems with 4K pages we have to use
709 * an order 1 page, so we might as well increase the size
710 * of our Rx buffer to make better use of the available space
712 rx_buf_len = I40E_RXBUFFER_3072;
714 /* We use a 1536 buffer size for configurations with
715 * standard Ethernet mtu. On x86 this gives us enough room
716 * for shared info and 192 bytes of padding.
718 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
719 (netdev->mtu <= ETH_DATA_LEN))
720 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
724 for (i = 0; i < adapter->num_active_queues; i++) {
725 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
726 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
728 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
729 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
731 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
736 * i40evf_find_vlan - Search filter list for specific vlan filter
737 * @adapter: board private structure
740 * Returns ptr to the filter object or NULL
743 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
745 struct i40evf_vlan_filter *f;
747 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
755 * i40evf_add_vlan - Add a vlan filter to the list
756 * @adapter: board private structure
759 * Returns ptr to the filter object or NULL when no memory available.
762 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
764 struct i40evf_vlan_filter *f = NULL;
767 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
768 &adapter->crit_section)) {
774 f = i40evf_find_vlan(adapter, vlan);
776 f = kzalloc(sizeof(*f), GFP_ATOMIC);
782 INIT_LIST_HEAD(&f->list);
783 list_add(&f->list, &adapter->vlan_filter_list);
785 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
789 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
795 * i40evf_del_vlan - Remove a vlan filter from the list
796 * @adapter: board private structure
799 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
801 struct i40evf_vlan_filter *f;
804 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
805 &adapter->crit_section)) {
811 f = i40evf_find_vlan(adapter, vlan);
814 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
816 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
820 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
821 * @netdev: network device struct
824 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
825 __always_unused __be16 proto, u16 vid)
827 struct i40evf_adapter *adapter = netdev_priv(netdev);
829 if (!VLAN_ALLOWED(adapter))
831 if (i40evf_add_vlan(adapter, vid) == NULL)
837 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
838 * @netdev: network device struct
841 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
842 __always_unused __be16 proto, u16 vid)
844 struct i40evf_adapter *adapter = netdev_priv(netdev);
846 if (VLAN_ALLOWED(adapter)) {
847 i40evf_del_vlan(adapter, vid);
854 * i40evf_find_filter - Search filter list for specific mac filter
855 * @adapter: board private structure
856 * @macaddr: the MAC address
858 * Returns ptr to the filter object or NULL
861 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
864 struct i40evf_mac_filter *f;
869 list_for_each_entry(f, &adapter->mac_filter_list, list) {
870 if (ether_addr_equal(macaddr, f->macaddr))
877 * i40e_add_filter - Add a mac filter to the filter list
878 * @adapter: board private structure
879 * @macaddr: the MAC address
881 * Returns ptr to the filter object or NULL when no memory available.
884 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
887 struct i40evf_mac_filter *f;
893 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
894 &adapter->crit_section)) {
900 f = i40evf_find_filter(adapter, macaddr);
902 f = kzalloc(sizeof(*f), GFP_ATOMIC);
904 clear_bit(__I40EVF_IN_CRITICAL_TASK,
905 &adapter->crit_section);
909 ether_addr_copy(f->macaddr, macaddr);
911 list_add_tail(&f->list, &adapter->mac_filter_list);
913 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
916 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
921 * i40evf_set_mac - NDO callback to set port mac address
922 * @netdev: network interface device structure
923 * @p: pointer to an address structure
925 * Returns 0 on success, negative on failure
927 static int i40evf_set_mac(struct net_device *netdev, void *p)
929 struct i40evf_adapter *adapter = netdev_priv(netdev);
930 struct i40e_hw *hw = &adapter->hw;
931 struct i40evf_mac_filter *f;
932 struct sockaddr *addr = p;
934 if (!is_valid_ether_addr(addr->sa_data))
935 return -EADDRNOTAVAIL;
937 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
940 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
943 f = i40evf_find_filter(adapter, hw->mac.addr);
946 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
949 f = i40evf_add_filter(adapter, addr->sa_data);
951 ether_addr_copy(hw->mac.addr, addr->sa_data);
952 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
955 return (f == NULL) ? -ENOMEM : 0;
959 * i40evf_set_rx_mode - NDO callback to set the netdev filters
960 * @netdev: network interface device structure
962 static void i40evf_set_rx_mode(struct net_device *netdev)
964 struct i40evf_adapter *adapter = netdev_priv(netdev);
965 struct i40evf_mac_filter *f, *ftmp;
966 struct netdev_hw_addr *uca;
967 struct netdev_hw_addr *mca;
968 struct netdev_hw_addr *ha;
971 /* add addr if not already in the filter list */
972 netdev_for_each_uc_addr(uca, netdev) {
973 i40evf_add_filter(adapter, uca->addr);
975 netdev_for_each_mc_addr(mca, netdev) {
976 i40evf_add_filter(adapter, mca->addr);
979 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
980 &adapter->crit_section)) {
983 dev_err(&adapter->pdev->dev,
984 "Failed to get lock in %s\n", __func__);
988 /* remove filter if not in netdev list */
989 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
990 netdev_for_each_mc_addr(mca, netdev)
991 if (ether_addr_equal(mca->addr, f->macaddr))
992 goto bottom_of_search_loop;
994 netdev_for_each_uc_addr(uca, netdev)
995 if (ether_addr_equal(uca->addr, f->macaddr))
996 goto bottom_of_search_loop;
998 for_each_dev_addr(netdev, ha)
999 if (ether_addr_equal(ha->addr, f->macaddr))
1000 goto bottom_of_search_loop;
1002 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
1003 goto bottom_of_search_loop;
1005 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1007 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1009 bottom_of_search_loop:
1013 if (netdev->flags & IFF_PROMISC &&
1014 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
1015 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
1016 else if (!(netdev->flags & IFF_PROMISC) &&
1017 adapter->flags & I40EVF_FLAG_PROMISC_ON)
1018 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
1020 if (netdev->flags & IFF_ALLMULTI &&
1021 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
1022 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
1023 else if (!(netdev->flags & IFF_ALLMULTI) &&
1024 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
1025 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
1027 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1031 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1032 * @adapter: board private structure
1034 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1037 struct i40e_q_vector *q_vector;
1038 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1040 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1041 struct napi_struct *napi;
1043 q_vector = &adapter->q_vectors[q_idx];
1044 napi = &q_vector->napi;
1050 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1051 * @adapter: board private structure
1053 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1056 struct i40e_q_vector *q_vector;
1057 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1059 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1060 q_vector = &adapter->q_vectors[q_idx];
1061 napi_disable(&q_vector->napi);
1066 * i40evf_configure - set up transmit and receive data structures
1067 * @adapter: board private structure
1069 static void i40evf_configure(struct i40evf_adapter *adapter)
1071 struct net_device *netdev = adapter->netdev;
1074 i40evf_set_rx_mode(netdev);
1076 i40evf_configure_tx(adapter);
1077 i40evf_configure_rx(adapter);
1078 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1080 for (i = 0; i < adapter->num_active_queues; i++) {
1081 struct i40e_ring *ring = &adapter->rx_rings[i];
1083 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
1088 * i40evf_up_complete - Finish the last steps of bringing up a connection
1089 * @adapter: board private structure
1091 static void i40evf_up_complete(struct i40evf_adapter *adapter)
1093 adapter->state = __I40EVF_RUNNING;
1094 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1096 i40evf_napi_enable_all(adapter);
1098 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1099 if (CLIENT_ENABLED(adapter))
1100 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
1101 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1105 * i40e_down - Shutdown the connection processing
1106 * @adapter: board private structure
1108 void i40evf_down(struct i40evf_adapter *adapter)
1110 struct net_device *netdev = adapter->netdev;
1111 struct i40evf_mac_filter *f;
1113 if (adapter->state <= __I40EVF_DOWN_PENDING)
1116 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1117 &adapter->crit_section))
1118 usleep_range(500, 1000);
1120 netif_carrier_off(netdev);
1121 netif_tx_disable(netdev);
1122 adapter->link_up = false;
1123 i40evf_napi_disable_all(adapter);
1124 i40evf_irq_disable(adapter);
1126 /* remove all MAC filters */
1127 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1130 /* remove all VLAN filters */
1131 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1134 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1135 adapter->state != __I40EVF_RESETTING) {
1136 /* cancel any current operation */
1137 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1138 /* Schedule operations to close down the HW. Don't wait
1139 * here for this to complete. The watchdog is still running
1140 * and it will take care of this.
1142 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1143 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1144 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1147 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1148 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1152 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1153 * @adapter: board private structure
1154 * @vectors: number of vectors to request
1156 * Work with the OS to set up the MSIX vectors needed.
1158 * Returns 0 on success, negative on failure
1161 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1163 int err, vector_threshold;
1165 /* We'll want at least 3 (vector_threshold):
1166 * 0) Other (Admin Queue and link, mostly)
1170 vector_threshold = MIN_MSIX_COUNT;
1172 /* The more we get, the more we will assign to Tx/Rx Cleanup
1173 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1174 * Right now, we simply care about how many we'll get; we'll
1175 * set them up later while requesting irq's.
1177 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1178 vector_threshold, vectors);
1180 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1181 kfree(adapter->msix_entries);
1182 adapter->msix_entries = NULL;
1186 /* Adjust for only the vectors we'll use, which is minimum
1187 * of max_msix_q_vectors + NONQ_VECS, or the number of
1188 * vectors we were allocated.
1190 adapter->num_msix_vectors = err;
1195 * i40evf_free_queues - Free memory for all rings
1196 * @adapter: board private structure to initialize
1198 * Free all of the memory associated with queue pairs.
1200 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1202 if (!adapter->vsi_res)
1204 adapter->num_active_queues = 0;
1205 kfree(adapter->tx_rings);
1206 adapter->tx_rings = NULL;
1207 kfree(adapter->rx_rings);
1208 adapter->rx_rings = NULL;
1212 * i40evf_alloc_queues - Allocate memory for all rings
1213 * @adapter: board private structure to initialize
1215 * We allocate one ring per queue at run-time since we don't know the
1216 * number of queues at compile-time. The polling_netdev array is
1217 * intended for Multiqueue, but should work fine with a single queue.
1219 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1221 int i, num_active_queues;
1223 num_active_queues = min_t(int,
1224 adapter->vsi_res->num_queue_pairs,
1225 (int)(num_online_cpus()));
1227 adapter->tx_rings = kcalloc(num_active_queues,
1228 sizeof(struct i40e_ring), GFP_KERNEL);
1229 if (!adapter->tx_rings)
1231 adapter->rx_rings = kcalloc(num_active_queues,
1232 sizeof(struct i40e_ring), GFP_KERNEL);
1233 if (!adapter->rx_rings)
1236 for (i = 0; i < num_active_queues; i++) {
1237 struct i40e_ring *tx_ring;
1238 struct i40e_ring *rx_ring;
1240 tx_ring = &adapter->tx_rings[i];
1242 tx_ring->queue_index = i;
1243 tx_ring->netdev = adapter->netdev;
1244 tx_ring->dev = &adapter->pdev->dev;
1245 tx_ring->count = adapter->tx_desc_count;
1246 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
1247 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
1248 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1250 rx_ring = &adapter->rx_rings[i];
1251 rx_ring->queue_index = i;
1252 rx_ring->netdev = adapter->netdev;
1253 rx_ring->dev = &adapter->pdev->dev;
1254 rx_ring->count = adapter->rx_desc_count;
1255 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
1258 adapter->num_active_queues = num_active_queues;
1263 i40evf_free_queues(adapter);
1268 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1269 * @adapter: board private structure to initialize
1271 * Attempt to configure the interrupts using the best available
1272 * capabilities of the hardware and the kernel.
1274 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1276 int vector, v_budget;
1280 if (!adapter->vsi_res) {
1284 pairs = adapter->num_active_queues;
1286 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1287 * us much good if we have more vectors than CPUs. However, we already
1288 * limit the total number of queues by the number of CPUs so we do not
1289 * need any further limiting here.
1291 v_budget = min_t(int, pairs + NONQ_VECS,
1292 (int)adapter->vf_res->max_vectors);
1294 adapter->msix_entries = kcalloc(v_budget,
1295 sizeof(struct msix_entry), GFP_KERNEL);
1296 if (!adapter->msix_entries) {
1301 for (vector = 0; vector < v_budget; vector++)
1302 adapter->msix_entries[vector].entry = vector;
1304 err = i40evf_acquire_msix_vectors(adapter, v_budget);
1307 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1308 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1313 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1314 * @adapter: board private structure
1316 * Return 0 on success, negative on failure
1318 static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
1320 struct i40e_aqc_get_set_rss_key_data *rss_key =
1321 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1322 struct i40e_hw *hw = &adapter->hw;
1325 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1326 /* bail because we already have a command pending */
1327 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1328 adapter->current_op);
1332 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1334 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1335 i40evf_stat_str(hw, ret),
1336 i40evf_aq_str(hw, hw->aq.asq_last_status));
1341 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1342 adapter->rss_lut, adapter->rss_lut_size);
1344 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1345 i40evf_stat_str(hw, ret),
1346 i40evf_aq_str(hw, hw->aq.asq_last_status));
1354 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1355 * @adapter: board private structure
1357 * Returns 0 on success, negative on failure
1359 static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
1361 struct i40e_hw *hw = &adapter->hw;
1365 dw = (u32 *)adapter->rss_key;
1366 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1367 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1369 dw = (u32 *)adapter->rss_lut;
1370 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1371 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1379 * i40evf_config_rss - Configure RSS keys and lut
1380 * @adapter: board private structure
1382 * Returns 0 on success, negative on failure
1384 int i40evf_config_rss(struct i40evf_adapter *adapter)
1387 if (RSS_PF(adapter)) {
1388 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1389 I40EVF_FLAG_AQ_SET_RSS_KEY;
1391 } else if (RSS_AQ(adapter)) {
1392 return i40evf_config_rss_aq(adapter);
1394 return i40evf_config_rss_reg(adapter);
1399 * i40evf_fill_rss_lut - Fill the lut with default values
1400 * @adapter: board private structure
1402 static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
1406 for (i = 0; i < adapter->rss_lut_size; i++)
1407 adapter->rss_lut[i] = i % adapter->num_active_queues;
1411 * i40evf_init_rss - Prepare for RSS
1412 * @adapter: board private structure
1414 * Return 0 on success, negative on failure
1416 static int i40evf_init_rss(struct i40evf_adapter *adapter)
1418 struct i40e_hw *hw = &adapter->hw;
1421 if (!RSS_PF(adapter)) {
1422 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1423 if (adapter->vf_res->vf_cap_flags &
1424 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1425 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1427 adapter->hena = I40E_DEFAULT_RSS_HENA;
1429 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1430 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1433 i40evf_fill_rss_lut(adapter);
1435 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1436 ret = i40evf_config_rss(adapter);
1442 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1443 * @adapter: board private structure to initialize
1445 * We allocate one q_vector per queue interrupt. If allocation fails we
1448 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1450 int q_idx = 0, num_q_vectors;
1451 struct i40e_q_vector *q_vector;
1453 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1454 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1456 if (!adapter->q_vectors)
1459 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1460 q_vector = &adapter->q_vectors[q_idx];
1461 q_vector->adapter = adapter;
1462 q_vector->vsi = &adapter->vsi;
1463 q_vector->v_idx = q_idx;
1464 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1465 netif_napi_add(adapter->netdev, &q_vector->napi,
1466 i40evf_napi_poll, NAPI_POLL_WEIGHT);
1473 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1474 * @adapter: board private structure to initialize
1476 * This function frees the memory allocated to the q_vectors. In addition if
1477 * NAPI is enabled it will delete any references to the NAPI struct prior
1478 * to freeing the q_vector.
1480 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1482 int q_idx, num_q_vectors;
1485 if (!adapter->q_vectors)
1488 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1489 napi_vectors = adapter->num_active_queues;
1491 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1492 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1493 if (q_idx < napi_vectors)
1494 netif_napi_del(&q_vector->napi);
1496 kfree(adapter->q_vectors);
1497 adapter->q_vectors = NULL;
1501 * i40evf_reset_interrupt_capability - Reset MSIX setup
1502 * @adapter: board private structure
1505 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1507 if (!adapter->msix_entries)
1510 pci_disable_msix(adapter->pdev);
1511 kfree(adapter->msix_entries);
1512 adapter->msix_entries = NULL;
1516 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1517 * @adapter: board private structure to initialize
1520 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1524 err = i40evf_alloc_queues(adapter);
1526 dev_err(&adapter->pdev->dev,
1527 "Unable to allocate memory for queues\n");
1528 goto err_alloc_queues;
1532 err = i40evf_set_interrupt_capability(adapter);
1535 dev_err(&adapter->pdev->dev,
1536 "Unable to setup interrupt capabilities\n");
1537 goto err_set_interrupt;
1540 err = i40evf_alloc_q_vectors(adapter);
1542 dev_err(&adapter->pdev->dev,
1543 "Unable to allocate memory for queue vectors\n");
1544 goto err_alloc_q_vectors;
1547 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1548 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1549 adapter->num_active_queues);
1552 err_alloc_q_vectors:
1553 i40evf_reset_interrupt_capability(adapter);
1555 i40evf_free_queues(adapter);
1561 * i40evf_free_rss - Free memory used by RSS structs
1562 * @adapter: board private structure
1564 static void i40evf_free_rss(struct i40evf_adapter *adapter)
1566 kfree(adapter->rss_key);
1567 adapter->rss_key = NULL;
1569 kfree(adapter->rss_lut);
1570 adapter->rss_lut = NULL;
1574 * i40evf_watchdog_timer - Periodic call-back timer
1575 * @data: pointer to adapter disguised as unsigned long
1577 static void i40evf_watchdog_timer(unsigned long data)
1579 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1581 schedule_work(&adapter->watchdog_task);
1582 /* timer will be rescheduled in watchdog task */
1586 * i40evf_watchdog_task - Periodic call-back task
1587 * @work: pointer to work_struct
1589 static void i40evf_watchdog_task(struct work_struct *work)
1591 struct i40evf_adapter *adapter = container_of(work,
1592 struct i40evf_adapter,
1594 struct i40e_hw *hw = &adapter->hw;
1597 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1598 goto restart_watchdog;
1600 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1601 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1602 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1603 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1604 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1605 /* A chance for redemption! */
1606 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1607 adapter->state = __I40EVF_STARTUP;
1608 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1609 schedule_delayed_work(&adapter->init_task, 10);
1610 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1611 &adapter->crit_section);
1612 /* Don't reschedule the watchdog, since we've restarted
1613 * the init task. When init_task contacts the PF and
1614 * gets everything set up again, it'll restart the
1615 * watchdog for us. Down, boy. Sit. Stay. Woof.
1619 adapter->aq_required = 0;
1620 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1624 if ((adapter->state < __I40EVF_DOWN) ||
1625 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1628 /* check for reset */
1629 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1630 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
1631 adapter->state = __I40EVF_RESETTING;
1632 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1633 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1634 schedule_work(&adapter->reset_task);
1635 adapter->aq_required = 0;
1636 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1640 /* Process admin queue tasks. After init, everything gets done
1641 * here so we don't race on the admin queue.
1643 if (adapter->current_op) {
1644 if (!i40evf_asq_done(hw)) {
1645 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1646 i40evf_send_api_ver(adapter);
1650 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1651 i40evf_send_vf_config_msg(adapter);
1655 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1656 i40evf_disable_queues(adapter);
1660 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1661 i40evf_map_queues(adapter);
1665 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1666 i40evf_add_ether_addrs(adapter);
1670 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1671 i40evf_add_vlans(adapter);
1675 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1676 i40evf_del_ether_addrs(adapter);
1680 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1681 i40evf_del_vlans(adapter);
1685 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1686 i40evf_enable_vlan_stripping(adapter);
1690 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1691 i40evf_disable_vlan_stripping(adapter);
1695 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1696 i40evf_configure_queues(adapter);
1700 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1701 i40evf_enable_queues(adapter);
1705 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1706 /* This message goes straight to the firmware, not the
1707 * PF, so we don't have to set current_op as we will
1708 * not get a response through the ARQ.
1710 i40evf_init_rss(adapter);
1711 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1714 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1715 i40evf_get_hena(adapter);
1718 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1719 i40evf_set_hena(adapter);
1722 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1723 i40evf_set_rss_key(adapter);
1726 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1727 i40evf_set_rss_lut(adapter);
1731 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1732 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1733 FLAG_VF_MULTICAST_PROMISC);
1737 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1738 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1742 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1743 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1744 i40evf_set_promiscuous(adapter, 0);
1747 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1749 if (adapter->state == __I40EVF_RUNNING)
1750 i40evf_request_stats(adapter);
1752 if (adapter->state == __I40EVF_RUNNING) {
1753 i40evf_irq_enable_queues(adapter, ~0);
1754 i40evf_fire_sw_int(adapter, 0xFF);
1756 i40evf_fire_sw_int(adapter, 0x1);
1759 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1761 if (adapter->state == __I40EVF_REMOVE)
1763 if (adapter->aq_required)
1764 mod_timer(&adapter->watchdog_timer,
1765 jiffies + msecs_to_jiffies(20));
1767 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1768 schedule_work(&adapter->adminq_task);
1771 static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1773 struct i40evf_mac_filter *f, *ftmp;
1774 struct i40evf_vlan_filter *fv, *fvtmp;
1776 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1778 /* We don't use netif_running() because it may be true prior to
1779 * ndo_open() returning, so we can't assume it means all our open
1780 * tasks have finished, since we're not holding the rtnl_lock here.
1782 if (adapter->state == __I40EVF_RUNNING) {
1783 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1784 netif_carrier_off(adapter->netdev);
1785 netif_tx_disable(adapter->netdev);
1786 adapter->link_up = false;
1787 i40evf_napi_disable_all(adapter);
1788 i40evf_irq_disable(adapter);
1789 i40evf_free_traffic_irqs(adapter);
1790 i40evf_free_all_tx_resources(adapter);
1791 i40evf_free_all_rx_resources(adapter);
1794 /* Delete all of the filters, both MAC and VLAN. */
1795 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1800 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1801 list_del(&fv->list);
1805 i40evf_free_misc_irq(adapter);
1806 i40evf_reset_interrupt_capability(adapter);
1807 i40evf_free_queues(adapter);
1808 i40evf_free_q_vectors(adapter);
1809 kfree(adapter->vf_res);
1810 i40evf_shutdown_adminq(&adapter->hw);
1811 adapter->netdev->flags &= ~IFF_UP;
1812 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1813 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1814 adapter->state = __I40EVF_DOWN;
1815 wake_up(&adapter->down_waitqueue);
1816 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1819 #define I40EVF_RESET_WAIT_MS 10
1820 #define I40EVF_RESET_WAIT_COUNT 500
1822 * i40evf_reset_task - Call-back task to handle hardware reset
1823 * @work: pointer to work_struct
1825 * During reset we need to shut down and reinitialize the admin queue
1826 * before we can use it to communicate with the PF again. We also clear
1827 * and reinit the rings because that context is lost as well.
1829 static void i40evf_reset_task(struct work_struct *work)
1831 struct i40evf_adapter *adapter = container_of(work,
1832 struct i40evf_adapter,
1834 struct net_device *netdev = adapter->netdev;
1835 struct i40e_hw *hw = &adapter->hw;
1836 struct i40evf_vlan_filter *vlf;
1837 struct i40evf_mac_filter *f;
1842 /* When device is being removed it doesn't make sense to run the reset
1843 * task, just return in such a case.
1845 if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1848 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
1849 &adapter->crit_section))
1850 usleep_range(500, 1000);
1851 if (CLIENT_ENABLED(adapter)) {
1852 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1853 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1854 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1855 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1856 cancel_delayed_work_sync(&adapter->client_task);
1857 i40evf_notify_client_close(&adapter->vsi, true);
1859 i40evf_misc_irq_disable(adapter);
1860 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1861 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1862 /* Restart the AQ here. If we have been reset but didn't
1863 * detect it, or if the PF had to reinit, our AQ will be hosed.
1865 i40evf_shutdown_adminq(hw);
1866 i40evf_init_adminq(hw);
1867 i40evf_request_reset(adapter);
1869 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1871 /* poll until we see the reset actually happen */
1872 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1873 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1874 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1877 usleep_range(5000, 10000);
1879 if (i == I40EVF_RESET_WAIT_COUNT) {
1880 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1881 goto continue_reset; /* act like the reset happened */
1884 /* wait until the reset is complete and the PF is responding to us */
1885 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1886 /* sleep first to make sure a minimum wait time is met */
1887 msleep(I40EVF_RESET_WAIT_MS);
1889 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1890 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1891 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1895 pci_set_master(adapter->pdev);
1897 if (i == I40EVF_RESET_WAIT_COUNT) {
1898 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1900 i40evf_disable_vf(adapter);
1901 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1902 return; /* Do not attempt to reinit. It's dead, Jim. */
1906 /* We don't use netif_running() because it may be true prior to
1907 * ndo_open() returning, so we can't assume it means all our open
1908 * tasks have finished, since we're not holding the rtnl_lock here.
1910 running = (adapter->state == __I40EVF_RUNNING);
1913 netif_carrier_off(netdev);
1914 netif_tx_stop_all_queues(netdev);
1915 adapter->link_up = false;
1916 i40evf_napi_disable_all(adapter);
1918 i40evf_irq_disable(adapter);
1920 adapter->state = __I40EVF_RESETTING;
1921 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1923 /* free the Tx/Rx rings and descriptors, might be better to just
1924 * re-use them sometime in the future
1926 i40evf_free_all_rx_resources(adapter);
1927 i40evf_free_all_tx_resources(adapter);
1929 /* kill and reinit the admin queue */
1930 i40evf_shutdown_adminq(hw);
1931 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1932 err = i40evf_init_adminq(hw);
1934 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1937 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1938 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1940 /* re-add all MAC filters */
1941 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1944 /* re-add all VLAN filters */
1945 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1948 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1949 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1950 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1951 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1952 i40evf_misc_irq_enable(adapter);
1954 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1956 /* We were running when the reset started, so we need to restore some
1960 /* allocate transmit descriptors */
1961 err = i40evf_setup_all_tx_resources(adapter);
1965 /* allocate receive descriptors */
1966 err = i40evf_setup_all_rx_resources(adapter);
1970 i40evf_configure(adapter);
1972 i40evf_up_complete(adapter);
1974 i40evf_irq_enable(adapter, true);
1976 adapter->state = __I40EVF_DOWN;
1977 wake_up(&adapter->down_waitqueue);
1982 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1983 i40evf_close(netdev);
1987 * i40evf_adminq_task - worker thread to clean the admin queue
1988 * @work: pointer to work_struct containing our data
1990 static void i40evf_adminq_task(struct work_struct *work)
1992 struct i40evf_adapter *adapter =
1993 container_of(work, struct i40evf_adapter, adminq_task);
1994 struct i40e_hw *hw = &adapter->hw;
1995 struct i40e_arq_event_info event;
1996 enum virtchnl_ops v_op;
1997 i40e_status ret, v_ret;
2001 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
2004 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2005 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2010 ret = i40evf_clean_arq_element(hw, &event, &pending);
2011 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2012 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2015 break; /* No event to process or error cleaning ARQ */
2017 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2020 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
2023 if ((adapter->flags &
2024 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2025 adapter->state == __I40EVF_RESETTING)
2028 /* check for error indications */
2029 val = rd32(hw, hw->aq.arq.len);
2030 if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
2033 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
2034 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2035 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
2037 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
2038 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2039 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
2041 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
2042 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2043 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
2046 wr32(hw, hw->aq.arq.len, val);
2048 val = rd32(hw, hw->aq.asq.len);
2050 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
2051 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2052 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
2054 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
2055 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2056 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
2058 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
2059 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2060 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
2063 wr32(hw, hw->aq.asq.len, val);
2066 kfree(event.msg_buf);
2068 /* re-enable Admin queue interrupt cause */
2069 i40evf_misc_irq_enable(adapter);
2073 * i40evf_client_task - worker thread to perform client work
2074 * @work: pointer to work_struct containing our data
2076 * This task handles client interactions. Because client calls can be
2077 * reentrant, we can't handle them in the watchdog.
2079 static void i40evf_client_task(struct work_struct *work)
2081 struct i40evf_adapter *adapter =
2082 container_of(work, struct i40evf_adapter, client_task.work);
2084 /* If we can't get the client bit, just give up. We'll be rescheduled
2088 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2091 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2092 i40evf_client_subtask(adapter);
2093 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2096 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2097 i40evf_notify_client_close(&adapter->vsi, false);
2098 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2101 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2102 i40evf_notify_client_open(&adapter->vsi);
2103 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2106 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2107 i40evf_notify_client_l2_params(&adapter->vsi);
2108 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2111 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2115 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2116 * @adapter: board private structure
2118 * Free all transmit software resources
2120 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
2124 if (!adapter->tx_rings)
2127 for (i = 0; i < adapter->num_active_queues; i++)
2128 if (adapter->tx_rings[i].desc)
2129 i40evf_free_tx_resources(&adapter->tx_rings[i]);
2133 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2134 * @adapter: board private structure
2136 * If this function returns with an error, then it's possible one or
2137 * more of the rings is populated (while the rest are not). It is the
2138 * callers duty to clean those orphaned rings.
2140 * Return 0 on success, negative on failure
2142 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2146 for (i = 0; i < adapter->num_active_queues; i++) {
2147 adapter->tx_rings[i].count = adapter->tx_desc_count;
2148 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
2151 dev_err(&adapter->pdev->dev,
2152 "Allocation for Tx Queue %u failed\n", i);
2160 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2161 * @adapter: board private structure
2163 * If this function returns with an error, then it's possible one or
2164 * more of the rings is populated (while the rest are not). It is the
2165 * callers duty to clean those orphaned rings.
2167 * Return 0 on success, negative on failure
2169 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2173 for (i = 0; i < adapter->num_active_queues; i++) {
2174 adapter->rx_rings[i].count = adapter->rx_desc_count;
2175 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
2178 dev_err(&adapter->pdev->dev,
2179 "Allocation for Rx Queue %u failed\n", i);
2186 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2187 * @adapter: board private structure
2189 * Free all receive software resources
2191 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
2195 if (!adapter->rx_rings)
2198 for (i = 0; i < adapter->num_active_queues; i++)
2199 if (adapter->rx_rings[i].desc)
2200 i40evf_free_rx_resources(&adapter->rx_rings[i]);
2204 * i40evf_open - Called when a network interface is made active
2205 * @netdev: network interface device structure
2207 * Returns 0 on success, negative value on failure
2209 * The open entry point is called when a network interface is made
2210 * active by the system (IFF_UP). At this point all resources needed
2211 * for transmit and receive operations are allocated, the interrupt
2212 * handler is registered with the OS, the watchdog timer is started,
2213 * and the stack is notified that the interface is ready.
2215 static int i40evf_open(struct net_device *netdev)
2217 struct i40evf_adapter *adapter = netdev_priv(netdev);
2220 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2221 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2225 if (adapter->state != __I40EVF_DOWN)
2228 /* allocate transmit descriptors */
2229 err = i40evf_setup_all_tx_resources(adapter);
2233 /* allocate receive descriptors */
2234 err = i40evf_setup_all_rx_resources(adapter);
2238 /* clear any pending interrupts, may auto mask */
2239 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2243 i40evf_add_filter(adapter, adapter->hw.mac.addr);
2244 i40evf_configure(adapter);
2246 i40evf_up_complete(adapter);
2248 i40evf_irq_enable(adapter, true);
2253 i40evf_down(adapter);
2254 i40evf_free_traffic_irqs(adapter);
2256 i40evf_free_all_rx_resources(adapter);
2258 i40evf_free_all_tx_resources(adapter);
2264 * i40evf_close - Disables a network interface
2265 * @netdev: network interface device structure
2267 * Returns 0, this is not allowed to fail
2269 * The close entry point is called when an interface is de-activated
2270 * by the OS. The hardware is still under the drivers control, but
2271 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2272 * are freed, along with all transmit and receive resources.
2274 static int i40evf_close(struct net_device *netdev)
2276 struct i40evf_adapter *adapter = netdev_priv(netdev);
2279 if (adapter->state <= __I40EVF_DOWN_PENDING)
2283 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
2284 if (CLIENT_ENABLED(adapter))
2285 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2287 i40evf_down(adapter);
2288 adapter->state = __I40EVF_DOWN_PENDING;
2289 i40evf_free_traffic_irqs(adapter);
2291 /* We explicitly don't free resources here because the hardware is
2292 * still active and can DMA into memory. Resources are cleared in
2293 * i40evf_virtchnl_completion() after we get confirmation from the PF
2294 * driver that the rings have been stopped.
2296 * Also, we wait for state to transition to __I40EVF_DOWN before
2297 * returning. State change occurs in i40evf_virtchnl_completion() after
2298 * VF resources are released (which occurs after PF driver processes and
2299 * responds to admin queue commands).
2302 status = wait_event_timeout(adapter->down_waitqueue,
2303 adapter->state == __I40EVF_DOWN,
2304 msecs_to_jiffies(200));
2306 netdev_warn(netdev, "Device resources not yet released\n");
2311 * i40evf_change_mtu - Change the Maximum Transfer Unit
2312 * @netdev: network interface device structure
2313 * @new_mtu: new value for maximum frame size
2315 * Returns 0 on success, negative on failure
2317 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2319 struct i40evf_adapter *adapter = netdev_priv(netdev);
2321 netdev->mtu = new_mtu;
2322 if (CLIENT_ENABLED(adapter)) {
2323 i40evf_notify_client_l2_params(&adapter->vsi);
2324 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2326 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2327 schedule_work(&adapter->reset_task);
2333 * i40e_set_features - set the netdev feature flags
2334 * @netdev: ptr to the netdev being adjusted
2335 * @features: the feature set that the stack is suggesting
2336 * Note: expects to be called while under rtnl_lock()
2338 static int i40evf_set_features(struct net_device *netdev,
2339 netdev_features_t features)
2341 struct i40evf_adapter *adapter = netdev_priv(netdev);
2343 if (!VLAN_ALLOWED(adapter))
2346 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2347 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2349 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2355 * i40evf_features_check - Validate encapsulated packet conforms to limits
2357 * @netdev: This physical port's netdev
2358 * @features: Offload features that the stack believes apply
2360 static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2361 struct net_device *dev,
2362 netdev_features_t features)
2366 /* No point in doing any of this if neither checksum nor GSO are
2367 * being requested for this frame. We can rule out both by just
2368 * checking for CHECKSUM_PARTIAL
2370 if (skb->ip_summed != CHECKSUM_PARTIAL)
2373 /* We cannot support GSO if the MSS is going to be less than
2374 * 64 bytes. If it is then we need to drop support for GSO.
2376 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2377 features &= ~NETIF_F_GSO_MASK;
2379 /* MACLEN can support at most 63 words */
2380 len = skb_network_header(skb) - skb->data;
2381 if (len & ~(63 * 2))
2384 /* IPLEN and EIPLEN can support at most 127 dwords */
2385 len = skb_transport_header(skb) - skb_network_header(skb);
2386 if (len & ~(127 * 4))
2389 if (skb->encapsulation) {
2390 /* L4TUNLEN can support 127 words */
2391 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2392 if (len & ~(127 * 2))
2395 /* IPLEN can support at most 127 dwords */
2396 len = skb_inner_transport_header(skb) -
2397 skb_inner_network_header(skb);
2398 if (len & ~(127 * 4))
2402 /* No need to validate L4LEN as TCP is the only protocol with a
2403 * a flexible value and we support all possible values supported
2404 * by TCP, which is at most 15 dwords
2409 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2412 #define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2413 NETIF_F_HW_VLAN_CTAG_RX |\
2414 NETIF_F_HW_VLAN_CTAG_FILTER)
2417 * i40evf_fix_features - fix up the netdev feature bits
2418 * @netdev: our net device
2419 * @features: desired feature bits
2421 * Returns fixed-up features bits
2423 static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2424 netdev_features_t features)
2426 struct i40evf_adapter *adapter = netdev_priv(netdev);
2428 features &= ~I40EVF_VLAN_FEATURES;
2429 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2430 features |= I40EVF_VLAN_FEATURES;
2434 static const struct net_device_ops i40evf_netdev_ops = {
2435 .ndo_open = i40evf_open,
2436 .ndo_stop = i40evf_close,
2437 .ndo_start_xmit = i40evf_xmit_frame,
2438 .ndo_set_rx_mode = i40evf_set_rx_mode,
2439 .ndo_validate_addr = eth_validate_addr,
2440 .ndo_set_mac_address = i40evf_set_mac,
2441 .ndo_change_mtu = i40evf_change_mtu,
2442 .ndo_tx_timeout = i40evf_tx_timeout,
2443 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2444 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
2445 .ndo_features_check = i40evf_features_check,
2446 .ndo_fix_features = i40evf_fix_features,
2447 .ndo_set_features = i40evf_set_features,
2448 #ifdef CONFIG_NET_POLL_CONTROLLER
2449 .ndo_poll_controller = i40evf_netpoll,
2454 * i40evf_check_reset_complete - check that VF reset is complete
2455 * @hw: pointer to hw struct
2457 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2459 static int i40evf_check_reset_complete(struct i40e_hw *hw)
2464 for (i = 0; i < 100; i++) {
2465 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2466 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2467 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2468 (rstat == VIRTCHNL_VFR_COMPLETED))
2470 usleep_range(10, 20);
2476 * i40evf_process_config - Process the config information we got from the PF
2477 * @adapter: board private structure
2479 * Verify that we have a valid config struct, and set up our netdev features
2480 * and our VSI struct.
2482 int i40evf_process_config(struct i40evf_adapter *adapter)
2484 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2485 struct net_device *netdev = adapter->netdev;
2486 struct i40e_vsi *vsi = &adapter->vsi;
2488 netdev_features_t hw_enc_features;
2489 netdev_features_t hw_features;
2491 /* got VF config message back from PF, now we can parse it */
2492 for (i = 0; i < vfres->num_vsis; i++) {
2493 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2494 adapter->vsi_res = &vfres->vsi_res[i];
2496 if (!adapter->vsi_res) {
2497 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2501 hw_enc_features = NETIF_F_SG |
2505 NETIF_F_SOFT_FEATURES |
2514 /* advertise to stack only if offloads for encapsulated packets is
2517 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
2518 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
2520 NETIF_F_GSO_GRE_CSUM |
2521 NETIF_F_GSO_IPXIP4 |
2522 NETIF_F_GSO_IPXIP6 |
2523 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2524 NETIF_F_GSO_PARTIAL |
2527 if (!(vfres->vf_cap_flags &
2528 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
2529 netdev->gso_partial_features |=
2530 NETIF_F_GSO_UDP_TUNNEL_CSUM;
2532 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2533 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2534 netdev->hw_enc_features |= hw_enc_features;
2536 /* record features VLANs can make use of */
2537 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
2539 /* Write features and hw_features separately to avoid polluting
2540 * with, or dropping, features that are set when we registered.
2542 hw_features = hw_enc_features;
2544 netdev->hw_features |= hw_features;
2546 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
2548 adapter->vsi.id = adapter->vsi_res->vsi_id;
2550 adapter->vsi.back = adapter;
2551 adapter->vsi.base_vector = 1;
2552 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2553 vsi->netdev = adapter->netdev;
2554 vsi->qs_handle = adapter->vsi_res->qset_handle;
2555 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2556 adapter->rss_key_size = vfres->rss_key_size;
2557 adapter->rss_lut_size = vfres->rss_lut_size;
2559 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2560 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2567 * i40evf_init_task - worker thread to perform delayed initialization
2568 * @work: pointer to work_struct containing our data
2570 * This task completes the work that was begun in probe. Due to the nature
2571 * of VF-PF communications, we may need to wait tens of milliseconds to get
2572 * responses back from the PF. Rather than busy-wait in probe and bog down the
2573 * whole system, we'll do it in a task so we can sleep.
2574 * This task only runs during driver init. Once we've established
2575 * communications with the PF driver and set up our netdev, the watchdog
2578 static void i40evf_init_task(struct work_struct *work)
2580 struct i40evf_adapter *adapter = container_of(work,
2581 struct i40evf_adapter,
2583 struct net_device *netdev = adapter->netdev;
2584 struct i40e_hw *hw = &adapter->hw;
2585 struct pci_dev *pdev = adapter->pdev;
2588 switch (adapter->state) {
2589 case __I40EVF_STARTUP:
2590 /* driver loaded, probe complete */
2591 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2592 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2593 err = i40e_set_mac_type(hw);
2595 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2599 err = i40evf_check_reset_complete(hw);
2601 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2605 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2606 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2607 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2608 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2610 err = i40evf_init_adminq(hw);
2612 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2616 err = i40evf_send_api_ver(adapter);
2618 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2619 i40evf_shutdown_adminq(hw);
2622 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2624 case __I40EVF_INIT_VERSION_CHECK:
2625 if (!i40evf_asq_done(hw)) {
2626 dev_err(&pdev->dev, "Admin queue command never completed\n");
2627 i40evf_shutdown_adminq(hw);
2628 adapter->state = __I40EVF_STARTUP;
2632 /* aq msg sent, awaiting reply */
2633 err = i40evf_verify_api_ver(adapter);
2635 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2636 err = i40evf_send_api_ver(adapter);
2638 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2639 adapter->pf_version.major,
2640 adapter->pf_version.minor,
2641 VIRTCHNL_VERSION_MAJOR,
2642 VIRTCHNL_VERSION_MINOR);
2645 err = i40evf_send_vf_config_msg(adapter);
2647 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2651 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2653 case __I40EVF_INIT_GET_RESOURCES:
2654 /* aq msg sent, awaiting reply */
2655 if (!adapter->vf_res) {
2656 bufsz = sizeof(struct virtchnl_vf_resource) +
2658 sizeof(struct virtchnl_vsi_resource));
2659 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2660 if (!adapter->vf_res)
2663 err = i40evf_get_vf_config(adapter);
2664 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2665 err = i40evf_send_vf_config_msg(adapter);
2667 } else if (err == I40E_ERR_PARAM) {
2668 /* We only get ERR_PARAM if the device is in a very bad
2669 * state or if we've been disabled for previous bad
2670 * behavior. Either way, we're done now.
2672 i40evf_shutdown_adminq(hw);
2673 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2677 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2681 adapter->state = __I40EVF_INIT_SW;
2687 if (i40evf_process_config(adapter))
2689 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2691 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2693 netdev->netdev_ops = &i40evf_netdev_ops;
2694 i40evf_set_ethtool_ops(netdev);
2695 netdev->watchdog_timeo = 5 * HZ;
2697 /* MTU range: 68 - 9710 */
2698 netdev->min_mtu = ETH_MIN_MTU;
2699 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
2701 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2702 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2703 adapter->hw.mac.addr);
2704 eth_hw_addr_random(netdev);
2705 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2707 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2708 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2709 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2712 init_timer(&adapter->watchdog_timer);
2713 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2714 adapter->watchdog_timer.data = (unsigned long)adapter;
2715 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2717 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2718 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2719 err = i40evf_init_interrupt_scheme(adapter);
2722 i40evf_map_rings_to_vectors(adapter);
2723 if (adapter->vf_res->vf_cap_flags &
2724 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2725 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2727 err = i40evf_request_misc_irq(adapter);
2731 netif_carrier_off(netdev);
2732 adapter->link_up = false;
2734 if (!adapter->netdev_registered) {
2735 err = register_netdev(netdev);
2740 adapter->netdev_registered = true;
2742 netif_tx_stop_all_queues(netdev);
2743 if (CLIENT_ALLOWED(adapter)) {
2744 err = i40evf_lan_add_device(adapter);
2746 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2750 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2751 if (netdev->features & NETIF_F_GRO)
2752 dev_info(&pdev->dev, "GRO is enabled\n");
2754 adapter->state = __I40EVF_DOWN;
2755 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
2756 i40evf_misc_irq_enable(adapter);
2757 wake_up(&adapter->down_waitqueue);
2759 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2760 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2761 if (!adapter->rss_key || !adapter->rss_lut)
2764 if (RSS_AQ(adapter)) {
2765 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2766 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2768 i40evf_init_rss(adapter);
2772 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
2775 i40evf_free_rss(adapter);
2777 i40evf_free_misc_irq(adapter);
2779 i40evf_reset_interrupt_capability(adapter);
2781 kfree(adapter->vf_res);
2782 adapter->vf_res = NULL;
2784 /* Things went into the weeds, so try again later */
2785 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2786 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
2787 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2788 i40evf_shutdown_adminq(hw);
2789 adapter->state = __I40EVF_STARTUP;
2790 schedule_delayed_work(&adapter->init_task, HZ * 5);
2793 schedule_delayed_work(&adapter->init_task, HZ);
2797 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2798 * @pdev: pci device structure
2800 static void i40evf_shutdown(struct pci_dev *pdev)
2802 struct net_device *netdev = pci_get_drvdata(pdev);
2803 struct i40evf_adapter *adapter = netdev_priv(netdev);
2805 netif_device_detach(netdev);
2807 if (netif_running(netdev))
2808 i40evf_close(netdev);
2810 /* Prevent the watchdog from running. */
2811 adapter->state = __I40EVF_REMOVE;
2812 adapter->aq_required = 0;
2815 pci_save_state(pdev);
2818 pci_disable_device(pdev);
2822 * i40evf_probe - Device Initialization Routine
2823 * @pdev: PCI device information struct
2824 * @ent: entry in i40evf_pci_tbl
2826 * Returns 0 on success, negative on failure
2828 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2829 * The OS initialization, configuring of the adapter private structure,
2830 * and a hardware reset occur.
2832 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2834 struct net_device *netdev;
2835 struct i40evf_adapter *adapter = NULL;
2836 struct i40e_hw *hw = NULL;
2839 err = pci_enable_device(pdev);
2843 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2845 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2848 "DMA configuration failed: 0x%x\n", err);
2853 err = pci_request_regions(pdev, i40evf_driver_name);
2856 "pci_request_regions failed 0x%x\n", err);
2860 pci_enable_pcie_error_reporting(pdev);
2862 pci_set_master(pdev);
2864 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
2867 goto err_alloc_etherdev;
2870 SET_NETDEV_DEV(netdev, &pdev->dev);
2872 pci_set_drvdata(pdev, netdev);
2873 adapter = netdev_priv(netdev);
2875 adapter->netdev = netdev;
2876 adapter->pdev = pdev;
2881 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2882 adapter->state = __I40EVF_STARTUP;
2884 /* Call save state here because it relies on the adapter struct. */
2885 pci_save_state(pdev);
2887 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2888 pci_resource_len(pdev, 0));
2893 hw->vendor_id = pdev->vendor;
2894 hw->device_id = pdev->device;
2895 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2896 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2897 hw->subsystem_device_id = pdev->subsystem_device;
2898 hw->bus.device = PCI_SLOT(pdev->devfn);
2899 hw->bus.func = PCI_FUNC(pdev->devfn);
2900 hw->bus.bus_id = pdev->bus->number;
2902 /* set up the locks for the AQ, do this only once in probe
2903 * and destroy them only once in remove
2905 mutex_init(&hw->aq.asq_mutex);
2906 mutex_init(&hw->aq.arq_mutex);
2908 INIT_LIST_HEAD(&adapter->mac_filter_list);
2909 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2911 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2912 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2913 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2914 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
2915 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2916 schedule_delayed_work(&adapter->init_task,
2917 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
2919 /* Setup the wait queue for indicating transition to down status */
2920 init_waitqueue_head(&adapter->down_waitqueue);
2925 free_netdev(netdev);
2927 pci_disable_pcie_error_reporting(pdev);
2928 pci_release_regions(pdev);
2931 pci_disable_device(pdev);
2937 * i40evf_suspend - Power management suspend routine
2938 * @pdev: PCI device information struct
2941 * Called when the system (VM) is entering sleep/suspend.
2943 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2945 struct net_device *netdev = pci_get_drvdata(pdev);
2946 struct i40evf_adapter *adapter = netdev_priv(netdev);
2949 netif_device_detach(netdev);
2951 if (netif_running(netdev)) {
2953 i40evf_down(adapter);
2956 i40evf_free_misc_irq(adapter);
2957 i40evf_reset_interrupt_capability(adapter);
2959 retval = pci_save_state(pdev);
2963 pci_disable_device(pdev);
2969 * i40evf_resume - Power management resume routine
2970 * @pdev: PCI device information struct
2972 * Called when the system (VM) is resumed from sleep/suspend.
2974 static int i40evf_resume(struct pci_dev *pdev)
2976 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2977 struct net_device *netdev = adapter->netdev;
2980 pci_set_power_state(pdev, PCI_D0);
2981 pci_restore_state(pdev);
2982 /* pci_restore_state clears dev->state_saved so call
2983 * pci_save_state to restore it.
2985 pci_save_state(pdev);
2987 err = pci_enable_device_mem(pdev);
2989 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2992 pci_set_master(pdev);
2995 err = i40evf_set_interrupt_capability(adapter);
2998 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3001 err = i40evf_request_misc_irq(adapter);
3004 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3008 schedule_work(&adapter->reset_task);
3010 netif_device_attach(netdev);
3015 #endif /* CONFIG_PM */
3017 * i40evf_remove - Device Removal Routine
3018 * @pdev: PCI device information struct
3020 * i40evf_remove is called by the PCI subsystem to alert the driver
3021 * that it should release a PCI device. The could be caused by a
3022 * Hot-Plug event, or because the driver is going to be removed from
3025 static void i40evf_remove(struct pci_dev *pdev)
3027 struct net_device *netdev = pci_get_drvdata(pdev);
3028 struct i40evf_adapter *adapter = netdev_priv(netdev);
3029 struct i40evf_mac_filter *f, *ftmp;
3030 struct i40e_hw *hw = &adapter->hw;
3032 /* Indicate we are in remove and not to run reset_task */
3033 set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
3034 cancel_delayed_work_sync(&adapter->init_task);
3035 cancel_work_sync(&adapter->reset_task);
3036 cancel_delayed_work_sync(&adapter->client_task);
3037 if (adapter->netdev_registered) {
3038 unregister_netdev(netdev);
3039 adapter->netdev_registered = false;
3041 if (CLIENT_ALLOWED(adapter)) {
3042 err = i40evf_lan_del_device(adapter);
3044 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3048 /* Shut down all the garbage mashers on the detention level */
3049 adapter->state = __I40EVF_REMOVE;
3050 adapter->aq_required = 0;
3051 i40evf_request_reset(adapter);
3053 /* If the FW isn't responding, kick it once, but only once. */
3054 if (!i40evf_asq_done(hw)) {
3055 i40evf_request_reset(adapter);
3058 i40evf_free_all_tx_resources(adapter);
3059 i40evf_free_all_rx_resources(adapter);
3060 i40evf_misc_irq_disable(adapter);
3061 i40evf_free_misc_irq(adapter);
3062 i40evf_reset_interrupt_capability(adapter);
3063 i40evf_free_q_vectors(adapter);
3065 if (adapter->watchdog_timer.function)
3066 del_timer_sync(&adapter->watchdog_timer);
3068 flush_scheduled_work();
3070 i40evf_free_rss(adapter);
3072 if (hw->aq.asq.count)
3073 i40evf_shutdown_adminq(hw);
3075 /* destroy the locks only once, here */
3076 mutex_destroy(&hw->aq.arq_mutex);
3077 mutex_destroy(&hw->aq.asq_mutex);
3079 iounmap(hw->hw_addr);
3080 pci_release_regions(pdev);
3081 i40evf_free_all_tx_resources(adapter);
3082 i40evf_free_all_rx_resources(adapter);
3083 i40evf_free_queues(adapter);
3084 kfree(adapter->vf_res);
3085 /* If we got removed before an up/down sequence, we've got a filter
3086 * hanging out there that we need to get rid of.
3088 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3092 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3097 free_netdev(netdev);
3099 pci_disable_pcie_error_reporting(pdev);
3101 pci_disable_device(pdev);
3104 static struct pci_driver i40evf_driver = {
3105 .name = i40evf_driver_name,
3106 .id_table = i40evf_pci_tbl,
3107 .probe = i40evf_probe,
3108 .remove = i40evf_remove,
3110 .suspend = i40evf_suspend,
3111 .resume = i40evf_resume,
3113 .shutdown = i40evf_shutdown,
3117 * i40e_init_module - Driver Registration Routine
3119 * i40e_init_module is the first routine called when the driver is
3120 * loaded. All it does is register with the PCI subsystem.
3122 static int __init i40evf_init_module(void)
3126 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
3127 i40evf_driver_version);
3129 pr_info("%s\n", i40evf_copyright);
3131 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3132 i40evf_driver_name);
3134 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3137 ret = pci_register_driver(&i40evf_driver);
3141 module_init(i40evf_init_module);
3144 * i40e_exit_module - Driver Exit Cleanup Routine
3146 * i40e_exit_module is called just before the driver is removed
3149 static void __exit i40evf_exit_module(void)
3151 pci_unregister_driver(&i40evf_driver);
3152 destroy_workqueue(i40evf_wq);
3155 module_exit(i40evf_exit_module);