1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4 * Copyright(c) 2013 - 2015 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 static int i40evf_setup_all_tx_resources(struct i40evf_adapter
*adapter
);
30 static int i40evf_setup_all_rx_resources(struct i40evf_adapter
*adapter
);
31 static int i40evf_close(struct net_device
*netdev
);
33 char i40evf_driver_name
[] = "i40evf";
34 static const char i40evf_driver_string
[] =
35 "Intel(R) XL710/X710 Virtual Function Network Driver";
37 #define DRV_VERSION "1.3.33"
38 const char i40evf_driver_version
[] = DRV_VERSION
;
39 static const char i40evf_copyright
[] =
40 "Copyright (c) 2013 - 2015 Intel Corporation.";
42 /* i40evf_pci_tbl - PCI Device ID Table
44 * Wildcard entries (PCI_ANY_ID) should come last
45 * Last entry must be all 0s
47 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
48 * Class, Class Mask, private data (not used) }
50 static const struct pci_device_id i40evf_pci_tbl
[] = {
51 {PCI_VDEVICE(INTEL
, I40E_DEV_ID_VF
), 0},
52 {PCI_VDEVICE(INTEL
, I40E_DEV_ID_X722_VF
), 0},
53 /* required last entry */
57 MODULE_DEVICE_TABLE(pci
, i40evf_pci_tbl
);
59 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
60 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
61 MODULE_LICENSE("GPL");
62 MODULE_VERSION(DRV_VERSION
);
65 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
66 * @hw: pointer to the HW structure
67 * @mem: ptr to mem struct to fill out
68 * @size: size of memory requested
69 * @alignment: what to align the allocation to
71 i40e_status
i40evf_allocate_dma_mem_d(struct i40e_hw
*hw
,
72 struct i40e_dma_mem
*mem
,
73 u64 size
, u32 alignment
)
75 struct i40evf_adapter
*adapter
= (struct i40evf_adapter
*)hw
->back
;
78 return I40E_ERR_PARAM
;
80 mem
->size
= ALIGN(size
, alignment
);
81 mem
->va
= dma_alloc_coherent(&adapter
->pdev
->dev
, mem
->size
,
82 (dma_addr_t
*)&mem
->pa
, GFP_KERNEL
);
86 return I40E_ERR_NO_MEMORY
;
90 * i40evf_free_dma_mem_d - OS specific memory free for shared code
91 * @hw: pointer to the HW structure
92 * @mem: ptr to mem struct to free
94 i40e_status
i40evf_free_dma_mem_d(struct i40e_hw
*hw
, struct i40e_dma_mem
*mem
)
96 struct i40evf_adapter
*adapter
= (struct i40evf_adapter
*)hw
->back
;
99 return I40E_ERR_PARAM
;
100 dma_free_coherent(&adapter
->pdev
->dev
, mem
->size
,
101 mem
->va
, (dma_addr_t
)mem
->pa
);
106 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
107 * @hw: pointer to the HW structure
108 * @mem: ptr to mem struct to fill out
109 * @size: size of memory requested
111 i40e_status
i40evf_allocate_virt_mem_d(struct i40e_hw
*hw
,
112 struct i40e_virt_mem
*mem
, u32 size
)
115 return I40E_ERR_PARAM
;
118 mem
->va
= kzalloc(size
, GFP_KERNEL
);
123 return I40E_ERR_NO_MEMORY
;
127 * i40evf_free_virt_mem_d - OS specific memory free for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to free
131 i40e_status
i40evf_free_virt_mem_d(struct i40e_hw
*hw
,
132 struct i40e_virt_mem
*mem
)
135 return I40E_ERR_PARAM
;
137 /* it's ok to kfree a NULL pointer */
144 * i40evf_debug_d - OS dependent version of debug printing
145 * @hw: pointer to the HW structure
146 * @mask: debug level mask
147 * @fmt_str: printf-type format description
149 void i40evf_debug_d(void *hw
, u32 mask
, char *fmt_str
, ...)
154 if (!(mask
& ((struct i40e_hw
*)hw
)->debug_mask
))
157 va_start(argptr
, fmt_str
);
158 vsnprintf(buf
, sizeof(buf
), fmt_str
, argptr
);
161 /* the debug string is already formatted with a newline */
166 * i40evf_tx_timeout - Respond to a Tx Hang
167 * @netdev: network interface device structure
169 static void i40evf_tx_timeout(struct net_device
*netdev
)
171 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
173 adapter
->tx_timeout_count
++;
174 if (!(adapter
->flags
& (I40EVF_FLAG_RESET_PENDING
|
175 I40EVF_FLAG_RESET_NEEDED
))) {
176 adapter
->flags
|= I40EVF_FLAG_RESET_NEEDED
;
177 schedule_work(&adapter
->reset_task
);
182 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
183 * @adapter: board private structure
185 static void i40evf_misc_irq_disable(struct i40evf_adapter
*adapter
)
187 struct i40e_hw
*hw
= &adapter
->hw
;
189 wr32(hw
, I40E_VFINT_DYN_CTL01
, 0);
192 rd32(hw
, I40E_VFGEN_RSTAT
);
194 synchronize_irq(adapter
->msix_entries
[0].vector
);
198 * i40evf_misc_irq_enable - Enable default interrupt generation settings
199 * @adapter: board private structure
201 static void i40evf_misc_irq_enable(struct i40evf_adapter
*adapter
)
203 struct i40e_hw
*hw
= &adapter
->hw
;
205 wr32(hw
, I40E_VFINT_DYN_CTL01
, I40E_VFINT_DYN_CTL01_INTENA_MASK
|
206 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK
);
207 wr32(hw
, I40E_VFINT_ICR0_ENA1
, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK
);
210 rd32(hw
, I40E_VFGEN_RSTAT
);
214 * i40evf_irq_disable - Mask off interrupt generation on the NIC
215 * @adapter: board private structure
217 static void i40evf_irq_disable(struct i40evf_adapter
*adapter
)
220 struct i40e_hw
*hw
= &adapter
->hw
;
222 if (!adapter
->msix_entries
)
225 for (i
= 1; i
< adapter
->num_msix_vectors
; i
++) {
226 wr32(hw
, I40E_VFINT_DYN_CTLN1(i
- 1), 0);
227 synchronize_irq(adapter
->msix_entries
[i
].vector
);
230 rd32(hw
, I40E_VFGEN_RSTAT
);
234 * i40evf_irq_enable_queues - Enable interrupt for specified queues
235 * @adapter: board private structure
236 * @mask: bitmap of queues to enable
238 void i40evf_irq_enable_queues(struct i40evf_adapter
*adapter
, u32 mask
)
240 struct i40e_hw
*hw
= &adapter
->hw
;
243 for (i
= 1; i
< adapter
->num_msix_vectors
; i
++) {
244 if (mask
& BIT(i
- 1)) {
245 wr32(hw
, I40E_VFINT_DYN_CTLN1(i
- 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK
|
247 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK
|
248 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK
);
254 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
255 * @adapter: board private structure
256 * @mask: bitmap of vectors to trigger
258 static void i40evf_fire_sw_int(struct i40evf_adapter
*adapter
, u32 mask
)
260 struct i40e_hw
*hw
= &adapter
->hw
;
265 dyn_ctl
= rd32(hw
, I40E_VFINT_DYN_CTL01
);
266 dyn_ctl
|= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK
|
267 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK
|
268 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK
;
269 wr32(hw
, I40E_VFINT_DYN_CTL01
, dyn_ctl
);
271 for (i
= 1; i
< adapter
->num_msix_vectors
; i
++) {
273 dyn_ctl
= rd32(hw
, I40E_VFINT_DYN_CTLN1(i
- 1));
274 dyn_ctl
|= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK
|
275 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK
|
276 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK
;
277 wr32(hw
, I40E_VFINT_DYN_CTLN1(i
- 1), dyn_ctl
);
283 * i40evf_irq_enable - Enable default interrupt generation settings
284 * @adapter: board private structure
285 * @flush: boolean value whether to run rd32()
287 void i40evf_irq_enable(struct i40evf_adapter
*adapter
, bool flush
)
289 struct i40e_hw
*hw
= &adapter
->hw
;
291 i40evf_misc_irq_enable(adapter
);
292 i40evf_irq_enable_queues(adapter
, ~0);
295 rd32(hw
, I40E_VFGEN_RSTAT
);
299 * i40evf_msix_aq - Interrupt handler for vector 0
300 * @irq: interrupt number
301 * @data: pointer to netdev
303 static irqreturn_t
i40evf_msix_aq(int irq
, void *data
)
305 struct net_device
*netdev
= data
;
306 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
307 struct i40e_hw
*hw
= &adapter
->hw
;
310 /* handle non-queue interrupts, these reads clear the registers */
311 val
= rd32(hw
, I40E_VFINT_ICR01
);
312 val
= rd32(hw
, I40E_VFINT_ICR0_ENA1
);
314 val
= rd32(hw
, I40E_VFINT_DYN_CTL01
) |
315 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK
;
316 wr32(hw
, I40E_VFINT_DYN_CTL01
, val
);
318 /* schedule work on the private workqueue */
319 schedule_work(&adapter
->adminq_task
);
325 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
326 * @irq: interrupt number
327 * @data: pointer to a q_vector
329 static irqreturn_t
i40evf_msix_clean_rings(int irq
, void *data
)
331 struct i40e_q_vector
*q_vector
= data
;
333 if (!q_vector
->tx
.ring
&& !q_vector
->rx
.ring
)
336 napi_schedule_irqoff(&q_vector
->napi
);
342 * i40evf_map_vector_to_rxq - associate irqs with rx queues
343 * @adapter: board private structure
344 * @v_idx: interrupt number
345 * @r_idx: queue number
348 i40evf_map_vector_to_rxq(struct i40evf_adapter
*adapter
, int v_idx
, int r_idx
)
350 struct i40e_q_vector
*q_vector
= adapter
->q_vector
[v_idx
];
351 struct i40e_ring
*rx_ring
= adapter
->rx_rings
[r_idx
];
353 rx_ring
->q_vector
= q_vector
;
354 rx_ring
->next
= q_vector
->rx
.ring
;
355 rx_ring
->vsi
= &adapter
->vsi
;
356 q_vector
->rx
.ring
= rx_ring
;
357 q_vector
->rx
.count
++;
358 q_vector
->rx
.latency_range
= I40E_LOW_LATENCY
;
359 q_vector
->itr_countdown
= ITR_COUNTDOWN_START
;
363 * i40evf_map_vector_to_txq - associate irqs with tx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @t_idx: queue number
369 i40evf_map_vector_to_txq(struct i40evf_adapter
*adapter
, int v_idx
, int t_idx
)
371 struct i40e_q_vector
*q_vector
= adapter
->q_vector
[v_idx
];
372 struct i40e_ring
*tx_ring
= adapter
->tx_rings
[t_idx
];
374 tx_ring
->q_vector
= q_vector
;
375 tx_ring
->next
= q_vector
->tx
.ring
;
376 tx_ring
->vsi
= &adapter
->vsi
;
377 q_vector
->tx
.ring
= tx_ring
;
378 q_vector
->tx
.count
++;
379 q_vector
->tx
.latency_range
= I40E_LOW_LATENCY
;
380 q_vector
->itr_countdown
= ITR_COUNTDOWN_START
;
381 q_vector
->num_ringpairs
++;
382 q_vector
->ring_mask
|= BIT(t_idx
);
386 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
387 * @adapter: board private structure to initialize
389 * This function maps descriptor rings to the queue-specific vectors
390 * we were allotted through the MSI-X enabling code. Ideally, we'd have
391 * one vector per ring/queue, but on a constrained vector budget, we
392 * group the rings as "efficiently" as possible. You would add new
393 * mapping configurations in here.
395 static int i40evf_map_rings_to_vectors(struct i40evf_adapter
*adapter
)
399 int rxr_idx
= 0, txr_idx
= 0;
400 int rxr_remaining
= adapter
->num_active_queues
;
401 int txr_remaining
= adapter
->num_active_queues
;
406 q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
408 /* The ideal configuration...
409 * We have enough vectors to map one per queue.
411 if (q_vectors
>= (rxr_remaining
* 2)) {
412 for (; rxr_idx
< rxr_remaining
; v_start
++, rxr_idx
++)
413 i40evf_map_vector_to_rxq(adapter
, v_start
, rxr_idx
);
415 for (; txr_idx
< txr_remaining
; v_start
++, txr_idx
++)
416 i40evf_map_vector_to_txq(adapter
, v_start
, txr_idx
);
420 /* If we don't have enough vectors for a 1-to-1
421 * mapping, we'll have to group them so there are
422 * multiple queues per vector.
423 * Re-adjusting *qpv takes care of the remainder.
425 for (i
= v_start
; i
< q_vectors
; i
++) {
426 rqpv
= DIV_ROUND_UP(rxr_remaining
, q_vectors
- i
);
427 for (j
= 0; j
< rqpv
; j
++) {
428 i40evf_map_vector_to_rxq(adapter
, i
, rxr_idx
);
433 for (i
= v_start
; i
< q_vectors
; i
++) {
434 tqpv
= DIV_ROUND_UP(txr_remaining
, q_vectors
- i
);
435 for (j
= 0; j
< tqpv
; j
++) {
436 i40evf_map_vector_to_txq(adapter
, i
, txr_idx
);
443 adapter
->aq_required
|= I40EVF_FLAG_AQ_MAP_VECTORS
;
448 #ifdef CONFIG_NET_POLL_CONTROLLER
450 * i40evf_netpoll - A Polling 'interrupt' handler
451 * @netdev: network interface device structure
453 * This is used by netconsole to send skbs without having to re-enable
454 * interrupts. It's not called while the normal interrupt routine is executing.
456 static void i40evf_netpoll(struct net_device
*netdev
)
458 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
459 int q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
462 /* if interface is down do nothing */
463 if (test_bit(__I40E_DOWN
, &adapter
->vsi
.state
))
466 for (i
= 0; i
< q_vectors
; i
++)
467 i40evf_msix_clean_rings(0, adapter
->q_vector
[i
]);
472 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
473 * @adapter: board private structure
475 * Allocates MSI-X vectors for tx and rx handling, and requests
476 * interrupts from the kernel.
479 i40evf_request_traffic_irqs(struct i40evf_adapter
*adapter
, char *basename
)
481 int vector
, err
, q_vectors
;
482 int rx_int_idx
= 0, tx_int_idx
= 0;
484 i40evf_irq_disable(adapter
);
485 /* Decrement for Other and TCP Timer vectors */
486 q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
488 for (vector
= 0; vector
< q_vectors
; vector
++) {
489 struct i40e_q_vector
*q_vector
= adapter
->q_vector
[vector
];
491 if (q_vector
->tx
.ring
&& q_vector
->rx
.ring
) {
492 snprintf(q_vector
->name
, sizeof(q_vector
->name
) - 1,
493 "i40evf-%s-%s-%d", basename
,
494 "TxRx", rx_int_idx
++);
496 } else if (q_vector
->rx
.ring
) {
497 snprintf(q_vector
->name
, sizeof(q_vector
->name
) - 1,
498 "i40evf-%s-%s-%d", basename
,
500 } else if (q_vector
->tx
.ring
) {
501 snprintf(q_vector
->name
, sizeof(q_vector
->name
) - 1,
502 "i40evf-%s-%s-%d", basename
,
505 /* skip this unused q_vector */
509 adapter
->msix_entries
[vector
+ NONQ_VECS
].vector
,
510 i40evf_msix_clean_rings
,
515 dev_info(&adapter
->pdev
->dev
,
516 "Request_irq failed, error: %d\n", err
);
517 goto free_queue_irqs
;
519 /* assign the mask for this irq */
520 irq_set_affinity_hint(
521 adapter
->msix_entries
[vector
+ NONQ_VECS
].vector
,
522 q_vector
->affinity_mask
);
530 irq_set_affinity_hint(
531 adapter
->msix_entries
[vector
+ NONQ_VECS
].vector
,
533 free_irq(adapter
->msix_entries
[vector
+ NONQ_VECS
].vector
,
534 adapter
->q_vector
[vector
]);
540 * i40evf_request_misc_irq - Initialize MSI-X interrupts
541 * @adapter: board private structure
543 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
544 * vector is only for the admin queue, and stays active even when the netdev
547 static int i40evf_request_misc_irq(struct i40evf_adapter
*adapter
)
549 struct net_device
*netdev
= adapter
->netdev
;
552 snprintf(adapter
->misc_vector_name
,
553 sizeof(adapter
->misc_vector_name
) - 1, "i40evf-%s:mbx",
554 dev_name(&adapter
->pdev
->dev
));
555 err
= request_irq(adapter
->msix_entries
[0].vector
,
557 adapter
->misc_vector_name
, netdev
);
559 dev_err(&adapter
->pdev
->dev
,
560 "request_irq for %s failed: %d\n",
561 adapter
->misc_vector_name
, err
);
562 free_irq(adapter
->msix_entries
[0].vector
, netdev
);
568 * i40evf_free_traffic_irqs - Free MSI-X interrupts
569 * @adapter: board private structure
571 * Frees all MSI-X vectors other than 0.
573 static void i40evf_free_traffic_irqs(struct i40evf_adapter
*adapter
)
578 q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
580 for (i
= 0; i
< q_vectors
; i
++) {
581 irq_set_affinity_hint(adapter
->msix_entries
[i
+1].vector
,
583 free_irq(adapter
->msix_entries
[i
+1].vector
,
584 adapter
->q_vector
[i
]);
589 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
590 * @adapter: board private structure
592 * Frees MSI-X vector 0.
594 static void i40evf_free_misc_irq(struct i40evf_adapter
*adapter
)
596 struct net_device
*netdev
= adapter
->netdev
;
598 free_irq(adapter
->msix_entries
[0].vector
, netdev
);
602 * i40evf_configure_tx - Configure Transmit Unit after Reset
603 * @adapter: board private structure
605 * Configure the Tx unit of the MAC after a reset.
607 static void i40evf_configure_tx(struct i40evf_adapter
*adapter
)
609 struct i40e_hw
*hw
= &adapter
->hw
;
612 for (i
= 0; i
< adapter
->num_active_queues
; i
++)
613 adapter
->tx_rings
[i
]->tail
= hw
->hw_addr
+ I40E_QTX_TAIL1(i
);
617 * i40evf_configure_rx - Configure Receive Unit after Reset
618 * @adapter: board private structure
620 * Configure the Rx unit of the MAC after a reset.
622 static void i40evf_configure_rx(struct i40evf_adapter
*adapter
)
624 struct i40e_hw
*hw
= &adapter
->hw
;
625 struct net_device
*netdev
= adapter
->netdev
;
626 int max_frame
= netdev
->mtu
+ ETH_HLEN
+ ETH_FCS_LEN
;
631 adapter
->flags
&= ~I40EVF_FLAG_RX_PS_CAPABLE
;
632 adapter
->flags
|= I40EVF_FLAG_RX_1BUF_CAPABLE
;
634 /* Decide whether to use packet split mode or not */
635 if (netdev
->mtu
> ETH_DATA_LEN
) {
636 if (adapter
->flags
& I40EVF_FLAG_RX_PS_CAPABLE
)
637 adapter
->flags
|= I40EVF_FLAG_RX_PS_ENABLED
;
639 adapter
->flags
&= ~I40EVF_FLAG_RX_PS_ENABLED
;
641 if (adapter
->flags
& I40EVF_FLAG_RX_1BUF_CAPABLE
)
642 adapter
->flags
&= ~I40EVF_FLAG_RX_PS_ENABLED
;
644 adapter
->flags
|= I40EVF_FLAG_RX_PS_ENABLED
;
647 /* Set the RX buffer length according to the mode */
648 if (adapter
->flags
& I40EVF_FLAG_RX_PS_ENABLED
) {
649 rx_buf_len
= I40E_RX_HDR_SIZE
;
651 if (netdev
->mtu
<= ETH_DATA_LEN
)
652 rx_buf_len
= I40EVF_RXBUFFER_2048
;
654 rx_buf_len
= ALIGN(max_frame
, 1024);
657 for (i
= 0; i
< adapter
->num_active_queues
; i
++) {
658 adapter
->rx_rings
[i
]->tail
= hw
->hw_addr
+ I40E_QRX_TAIL1(i
);
659 adapter
->rx_rings
[i
]->rx_buf_len
= rx_buf_len
;
664 * i40evf_find_vlan - Search filter list for specific vlan filter
665 * @adapter: board private structure
668 * Returns ptr to the filter object or NULL
671 i40evf_vlan_filter
*i40evf_find_vlan(struct i40evf_adapter
*adapter
, u16 vlan
)
673 struct i40evf_vlan_filter
*f
;
675 list_for_each_entry(f
, &adapter
->vlan_filter_list
, list
) {
683 * i40evf_add_vlan - Add a vlan filter to the list
684 * @adapter: board private structure
687 * Returns ptr to the filter object or NULL when no memory available.
690 i40evf_vlan_filter
*i40evf_add_vlan(struct i40evf_adapter
*adapter
, u16 vlan
)
692 struct i40evf_vlan_filter
*f
= NULL
;
695 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
,
696 &adapter
->crit_section
)) {
702 f
= i40evf_find_vlan(adapter
, vlan
);
704 f
= kzalloc(sizeof(*f
), GFP_ATOMIC
);
710 INIT_LIST_HEAD(&f
->list
);
711 list_add(&f
->list
, &adapter
->vlan_filter_list
);
713 adapter
->aq_required
|= I40EVF_FLAG_AQ_ADD_VLAN_FILTER
;
717 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
723 * i40evf_del_vlan - Remove a vlan filter from the list
724 * @adapter: board private structure
727 static void i40evf_del_vlan(struct i40evf_adapter
*adapter
, u16 vlan
)
729 struct i40evf_vlan_filter
*f
;
732 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
,
733 &adapter
->crit_section
)) {
739 f
= i40evf_find_vlan(adapter
, vlan
);
742 adapter
->aq_required
|= I40EVF_FLAG_AQ_DEL_VLAN_FILTER
;
744 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
748 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
749 * @netdev: network device struct
752 static int i40evf_vlan_rx_add_vid(struct net_device
*netdev
,
753 __always_unused __be16 proto
, u16 vid
)
755 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
757 if (!VLAN_ALLOWED(adapter
))
759 if (i40evf_add_vlan(adapter
, vid
) == NULL
)
765 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
766 * @netdev: network device struct
769 static int i40evf_vlan_rx_kill_vid(struct net_device
*netdev
,
770 __always_unused __be16 proto
, u16 vid
)
772 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
774 if (VLAN_ALLOWED(adapter
)) {
775 i40evf_del_vlan(adapter
, vid
);
782 * i40evf_find_filter - Search filter list for specific mac filter
783 * @adapter: board private structure
784 * @macaddr: the MAC address
786 * Returns ptr to the filter object or NULL
789 i40evf_mac_filter
*i40evf_find_filter(struct i40evf_adapter
*adapter
,
792 struct i40evf_mac_filter
*f
;
797 list_for_each_entry(f
, &adapter
->mac_filter_list
, list
) {
798 if (ether_addr_equal(macaddr
, f
->macaddr
))
805 * i40e_add_filter - Add a mac filter to the filter list
806 * @adapter: board private structure
807 * @macaddr: the MAC address
809 * Returns ptr to the filter object or NULL when no memory available.
812 i40evf_mac_filter
*i40evf_add_filter(struct i40evf_adapter
*adapter
,
815 struct i40evf_mac_filter
*f
;
821 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
,
822 &adapter
->crit_section
)) {
828 f
= i40evf_find_filter(adapter
, macaddr
);
830 f
= kzalloc(sizeof(*f
), GFP_ATOMIC
);
832 clear_bit(__I40EVF_IN_CRITICAL_TASK
,
833 &adapter
->crit_section
);
837 ether_addr_copy(f
->macaddr
, macaddr
);
839 list_add(&f
->list
, &adapter
->mac_filter_list
);
841 adapter
->aq_required
|= I40EVF_FLAG_AQ_ADD_MAC_FILTER
;
844 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
849 * i40evf_set_mac - NDO callback to set port mac address
850 * @netdev: network interface device structure
851 * @p: pointer to an address structure
853 * Returns 0 on success, negative on failure
855 static int i40evf_set_mac(struct net_device
*netdev
, void *p
)
857 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
858 struct i40e_hw
*hw
= &adapter
->hw
;
859 struct i40evf_mac_filter
*f
;
860 struct sockaddr
*addr
= p
;
862 if (!is_valid_ether_addr(addr
->sa_data
))
863 return -EADDRNOTAVAIL
;
865 if (ether_addr_equal(netdev
->dev_addr
, addr
->sa_data
))
868 if (adapter
->flags
& I40EVF_FLAG_ADDR_SET_BY_PF
)
871 f
= i40evf_find_filter(adapter
, hw
->mac
.addr
);
874 adapter
->aq_required
|= I40EVF_FLAG_AQ_DEL_MAC_FILTER
;
877 f
= i40evf_add_filter(adapter
, addr
->sa_data
);
879 ether_addr_copy(hw
->mac
.addr
, addr
->sa_data
);
880 ether_addr_copy(netdev
->dev_addr
, adapter
->hw
.mac
.addr
);
883 return (f
== NULL
) ? -ENOMEM
: 0;
887 * i40evf_set_rx_mode - NDO callback to set the netdev filters
888 * @netdev: network interface device structure
890 static void i40evf_set_rx_mode(struct net_device
*netdev
)
892 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
893 struct i40evf_mac_filter
*f
, *ftmp
;
894 struct netdev_hw_addr
*uca
;
895 struct netdev_hw_addr
*mca
;
896 struct netdev_hw_addr
*ha
;
899 /* add addr if not already in the filter list */
900 netdev_for_each_uc_addr(uca
, netdev
) {
901 i40evf_add_filter(adapter
, uca
->addr
);
903 netdev_for_each_mc_addr(mca
, netdev
) {
904 i40evf_add_filter(adapter
, mca
->addr
);
907 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
,
908 &adapter
->crit_section
)) {
911 dev_err(&adapter
->pdev
->dev
,
912 "Failed to get lock in %s\n", __func__
);
916 /* remove filter if not in netdev list */
917 list_for_each_entry_safe(f
, ftmp
, &adapter
->mac_filter_list
, list
) {
918 netdev_for_each_mc_addr(mca
, netdev
)
919 if (ether_addr_equal(mca
->addr
, f
->macaddr
))
920 goto bottom_of_search_loop
;
922 netdev_for_each_uc_addr(uca
, netdev
)
923 if (ether_addr_equal(uca
->addr
, f
->macaddr
))
924 goto bottom_of_search_loop
;
926 for_each_dev_addr(netdev
, ha
)
927 if (ether_addr_equal(ha
->addr
, f
->macaddr
))
928 goto bottom_of_search_loop
;
930 if (ether_addr_equal(f
->macaddr
, adapter
->hw
.mac
.addr
))
931 goto bottom_of_search_loop
;
933 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
935 adapter
->aq_required
|= I40EVF_FLAG_AQ_DEL_MAC_FILTER
;
937 bottom_of_search_loop
:
940 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
944 * i40evf_napi_enable_all - enable NAPI on all queue vectors
945 * @adapter: board private structure
947 static void i40evf_napi_enable_all(struct i40evf_adapter
*adapter
)
950 struct i40e_q_vector
*q_vector
;
951 int q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
953 for (q_idx
= 0; q_idx
< q_vectors
; q_idx
++) {
954 struct napi_struct
*napi
;
956 q_vector
= adapter
->q_vector
[q_idx
];
957 napi
= &q_vector
->napi
;
963 * i40evf_napi_disable_all - disable NAPI on all queue vectors
964 * @adapter: board private structure
966 static void i40evf_napi_disable_all(struct i40evf_adapter
*adapter
)
969 struct i40e_q_vector
*q_vector
;
970 int q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
972 for (q_idx
= 0; q_idx
< q_vectors
; q_idx
++) {
973 q_vector
= adapter
->q_vector
[q_idx
];
974 napi_disable(&q_vector
->napi
);
979 * i40evf_configure - set up transmit and receive data structures
980 * @adapter: board private structure
982 static void i40evf_configure(struct i40evf_adapter
*adapter
)
984 struct net_device
*netdev
= adapter
->netdev
;
987 i40evf_set_rx_mode(netdev
);
989 i40evf_configure_tx(adapter
);
990 i40evf_configure_rx(adapter
);
991 adapter
->aq_required
|= I40EVF_FLAG_AQ_CONFIGURE_QUEUES
;
993 for (i
= 0; i
< adapter
->num_active_queues
; i
++) {
994 struct i40e_ring
*ring
= adapter
->rx_rings
[i
];
996 i40evf_alloc_rx_buffers_1buf(ring
, ring
->count
);
997 ring
->next_to_use
= ring
->count
- 1;
998 writel(ring
->next_to_use
, ring
->tail
);
1003 * i40evf_up_complete - Finish the last steps of bringing up a connection
1004 * @adapter: board private structure
1006 static int i40evf_up_complete(struct i40evf_adapter
*adapter
)
1008 adapter
->state
= __I40EVF_RUNNING
;
1009 clear_bit(__I40E_DOWN
, &adapter
->vsi
.state
);
1011 i40evf_napi_enable_all(adapter
);
1013 adapter
->aq_required
|= I40EVF_FLAG_AQ_ENABLE_QUEUES
;
1014 mod_timer_pending(&adapter
->watchdog_timer
, jiffies
+ 1);
1019 * i40e_down - Shutdown the connection processing
1020 * @adapter: board private structure
1022 void i40evf_down(struct i40evf_adapter
*adapter
)
1024 struct net_device
*netdev
= adapter
->netdev
;
1025 struct i40evf_mac_filter
*f
;
1027 if (adapter
->state
== __I40EVF_DOWN
)
1030 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
,
1031 &adapter
->crit_section
))
1032 usleep_range(500, 1000);
1034 netif_carrier_off(netdev
);
1035 netif_tx_disable(netdev
);
1036 i40evf_napi_disable_all(adapter
);
1037 i40evf_irq_disable(adapter
);
1039 /* remove all MAC filters */
1040 list_for_each_entry(f
, &adapter
->mac_filter_list
, list
) {
1043 /* remove all VLAN filters */
1044 list_for_each_entry(f
, &adapter
->vlan_filter_list
, list
) {
1047 if (!(adapter
->flags
& I40EVF_FLAG_PF_COMMS_FAILED
) &&
1048 adapter
->state
!= __I40EVF_RESETTING
) {
1049 /* cancel any current operation */
1050 adapter
->current_op
= I40E_VIRTCHNL_OP_UNKNOWN
;
1051 /* Schedule operations to close down the HW. Don't wait
1052 * here for this to complete. The watchdog is still running
1053 * and it will take care of this.
1055 adapter
->aq_required
= I40EVF_FLAG_AQ_DEL_MAC_FILTER
;
1056 adapter
->aq_required
|= I40EVF_FLAG_AQ_DEL_VLAN_FILTER
;
1057 adapter
->aq_required
|= I40EVF_FLAG_AQ_DISABLE_QUEUES
;
1060 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
1064 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1065 * @adapter: board private structure
1066 * @vectors: number of vectors to request
1068 * Work with the OS to set up the MSIX vectors needed.
1070 * Returns 0 on success, negative on failure
1073 i40evf_acquire_msix_vectors(struct i40evf_adapter
*adapter
, int vectors
)
1075 int err
, vector_threshold
;
1077 /* We'll want at least 3 (vector_threshold):
1078 * 0) Other (Admin Queue and link, mostly)
1082 vector_threshold
= MIN_MSIX_COUNT
;
1084 /* The more we get, the more we will assign to Tx/Rx Cleanup
1085 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1086 * Right now, we simply care about how many we'll get; we'll
1087 * set them up later while requesting irq's.
1089 err
= pci_enable_msix_range(adapter
->pdev
, adapter
->msix_entries
,
1090 vector_threshold
, vectors
);
1092 dev_err(&adapter
->pdev
->dev
, "Unable to allocate MSI-X interrupts\n");
1093 kfree(adapter
->msix_entries
);
1094 adapter
->msix_entries
= NULL
;
1098 /* Adjust for only the vectors we'll use, which is minimum
1099 * of max_msix_q_vectors + NONQ_VECS, or the number of
1100 * vectors we were allocated.
1102 adapter
->num_msix_vectors
= err
;
1107 * i40evf_free_queues - Free memory for all rings
1108 * @adapter: board private structure to initialize
1110 * Free all of the memory associated with queue pairs.
1112 static void i40evf_free_queues(struct i40evf_adapter
*adapter
)
1116 if (!adapter
->vsi_res
)
1118 for (i
= 0; i
< adapter
->num_active_queues
; i
++) {
1119 if (adapter
->tx_rings
[i
])
1120 kfree_rcu(adapter
->tx_rings
[i
], rcu
);
1121 adapter
->tx_rings
[i
] = NULL
;
1122 adapter
->rx_rings
[i
] = NULL
;
1127 * i40evf_alloc_queues - Allocate memory for all rings
1128 * @adapter: board private structure to initialize
1130 * We allocate one ring per queue at run-time since we don't know the
1131 * number of queues at compile-time. The polling_netdev array is
1132 * intended for Multiqueue, but should work fine with a single queue.
1134 static int i40evf_alloc_queues(struct i40evf_adapter
*adapter
)
1138 for (i
= 0; i
< adapter
->num_active_queues
; i
++) {
1139 struct i40e_ring
*tx_ring
;
1140 struct i40e_ring
*rx_ring
;
1142 tx_ring
= kzalloc(sizeof(*tx_ring
) * 2, GFP_KERNEL
);
1146 tx_ring
->queue_index
= i
;
1147 tx_ring
->netdev
= adapter
->netdev
;
1148 tx_ring
->dev
= &adapter
->pdev
->dev
;
1149 tx_ring
->count
= adapter
->tx_desc_count
;
1150 if (adapter
->flags
& I40E_FLAG_WB_ON_ITR_CAPABLE
)
1151 tx_ring
->flags
|= I40E_TXR_FLAGS_WB_ON_ITR
;
1152 adapter
->tx_rings
[i
] = tx_ring
;
1154 rx_ring
= &tx_ring
[1];
1155 rx_ring
->queue_index
= i
;
1156 rx_ring
->netdev
= adapter
->netdev
;
1157 rx_ring
->dev
= &adapter
->pdev
->dev
;
1158 rx_ring
->count
= adapter
->rx_desc_count
;
1159 adapter
->rx_rings
[i
] = rx_ring
;
1165 i40evf_free_queues(adapter
);
1170 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1171 * @adapter: board private structure to initialize
1173 * Attempt to configure the interrupts using the best available
1174 * capabilities of the hardware and the kernel.
1176 static int i40evf_set_interrupt_capability(struct i40evf_adapter
*adapter
)
1178 int vector
, v_budget
;
1182 if (!adapter
->vsi_res
) {
1186 pairs
= adapter
->num_active_queues
;
1188 /* It's easy to be greedy for MSI-X vectors, but it really
1189 * doesn't do us much good if we have a lot more vectors
1190 * than CPU's. So let's be conservative and only ask for
1191 * (roughly) twice the number of vectors as there are CPU's.
1193 v_budget
= min_t(int, pairs
, (int)(num_online_cpus() * 2)) + NONQ_VECS
;
1194 v_budget
= min_t(int, v_budget
, (int)adapter
->vf_res
->max_vectors
);
1196 adapter
->msix_entries
= kcalloc(v_budget
,
1197 sizeof(struct msix_entry
), GFP_KERNEL
);
1198 if (!adapter
->msix_entries
) {
1203 for (vector
= 0; vector
< v_budget
; vector
++)
1204 adapter
->msix_entries
[vector
].entry
= vector
;
1206 err
= i40evf_acquire_msix_vectors(adapter
, v_budget
);
1209 adapter
->netdev
->real_num_tx_queues
= pairs
;
1214 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1215 * @vsi: vsi structure
1216 * @seed: RSS hash seed
1217 * @lut: Lookup table
1218 * @lut_size: Lookup table size
1220 * Return 0 on success, negative on failure
1222 static int i40evf_config_rss_aq(struct i40e_vsi
*vsi
, const u8
*seed
,
1223 u8
*lut
, u16 lut_size
)
1225 struct i40evf_adapter
*adapter
= vsi
->back
;
1226 struct i40e_hw
*hw
= &adapter
->hw
;
1232 if (adapter
->current_op
!= I40E_VIRTCHNL_OP_UNKNOWN
) {
1233 /* bail because we already have a command pending */
1234 dev_err(&adapter
->pdev
->dev
, "Cannot confiure RSS, command %d pending\n",
1235 adapter
->current_op
);
1240 struct i40e_aqc_get_set_rss_key_data
*rss_key
=
1241 (struct i40e_aqc_get_set_rss_key_data
*)seed
;
1242 ret
= i40evf_aq_set_rss_key(hw
, vsi
->id
, rss_key
);
1244 dev_err(&adapter
->pdev
->dev
, "Cannot set RSS key, err %s aq_err %s\n",
1245 i40evf_stat_str(hw
, ret
),
1246 i40evf_aq_str(hw
, hw
->aq
.asq_last_status
));
1252 ret
= i40evf_aq_set_rss_lut(hw
, vsi
->id
, false, lut
, lut_size
);
1254 dev_err(&adapter
->pdev
->dev
,
1255 "Cannot set RSS lut, err %s aq_err %s\n",
1256 i40evf_stat_str(hw
, ret
),
1257 i40evf_aq_str(hw
, hw
->aq
.asq_last_status
));
1266 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1267 * @vsi: Pointer to vsi structure
1268 * @seed: RSS hash seed
1269 * @lut: Lookup table
1270 * @lut_size: Lookup table size
1272 * Returns 0 on success, negative on failure
1274 static int i40evf_config_rss_reg(struct i40e_vsi
*vsi
, const u8
*seed
,
1275 const u8
*lut
, u16 lut_size
)
1277 struct i40evf_adapter
*adapter
= vsi
->back
;
1278 struct i40e_hw
*hw
= &adapter
->hw
;
1282 u32
*seed_dw
= (u32
*)seed
;
1284 for (i
= 0; i
<= I40E_VFQF_HKEY_MAX_INDEX
; i
++)
1285 wr32(hw
, I40E_VFQF_HKEY(i
), seed_dw
[i
]);
1289 u32
*lut_dw
= (u32
*)lut
;
1291 if (lut_size
!= I40EVF_HLUT_ARRAY_SIZE
)
1294 for (i
= 0; i
<= I40E_VFQF_HLUT_MAX_INDEX
; i
++)
1295 wr32(hw
, I40E_VFQF_HLUT(i
), lut_dw
[i
]);
1303 * * i40evf_get_rss_aq - Get RSS keys and lut by using AQ commands
1304 * @vsi: Pointer to vsi structure
1305 * @seed: RSS hash seed
1306 * @lut: Lookup table
1307 * @lut_size: Lookup table size
1309 * Return 0 on success, negative on failure
1311 static int i40evf_get_rss_aq(struct i40e_vsi
*vsi
, const u8
*seed
,
1312 u8
*lut
, u16 lut_size
)
1314 struct i40evf_adapter
*adapter
= vsi
->back
;
1315 struct i40e_hw
*hw
= &adapter
->hw
;
1319 ret
= i40evf_aq_get_rss_key(hw
, vsi
->id
,
1320 (struct i40e_aqc_get_set_rss_key_data
*)seed
);
1322 dev_err(&adapter
->pdev
->dev
,
1323 "Cannot get RSS key, err %s aq_err %s\n",
1324 i40evf_stat_str(hw
, ret
),
1325 i40evf_aq_str(hw
, hw
->aq
.asq_last_status
));
1331 ret
= i40evf_aq_get_rss_lut(hw
, vsi
->id
, seed
, lut
, lut_size
);
1333 dev_err(&adapter
->pdev
->dev
,
1334 "Cannot get RSS lut, err %s aq_err %s\n",
1335 i40evf_stat_str(hw
, ret
),
1336 i40evf_aq_str(hw
, hw
->aq
.asq_last_status
));
1345 * * i40evf_get_rss_reg - Get RSS keys and lut by reading registers
1346 * @vsi: Pointer to vsi structure
1347 * @seed: RSS hash seed
1348 * @lut: Lookup table
1349 * @lut_size: Lookup table size
1351 * Returns 0 on success, negative on failure
1353 static int i40evf_get_rss_reg(struct i40e_vsi
*vsi
, const u8
*seed
,
1354 const u8
*lut
, u16 lut_size
)
1356 struct i40evf_adapter
*adapter
= vsi
->back
;
1357 struct i40e_hw
*hw
= &adapter
->hw
;
1361 u32
*seed_dw
= (u32
*)seed
;
1363 for (i
= 0; i
<= I40E_VFQF_HKEY_MAX_INDEX
; i
++)
1364 seed_dw
[i
] = rd32(hw
, I40E_VFQF_HKEY(i
));
1368 u32
*lut_dw
= (u32
*)lut
;
1370 if (lut_size
!= I40EVF_HLUT_ARRAY_SIZE
)
1373 for (i
= 0; i
<= I40E_VFQF_HLUT_MAX_INDEX
; i
++)
1374 lut_dw
[i
] = rd32(hw
, I40E_VFQF_HLUT(i
));
1381 * i40evf_config_rss - Configure RSS keys and lut
1382 * @vsi: Pointer to vsi structure
1383 * @seed: RSS hash seed
1384 * @lut: Lookup table
1385 * @lut_size: Lookup table size
1387 * Returns 0 on success, negative on failure
1389 int i40evf_config_rss(struct i40e_vsi
*vsi
, const u8
*seed
,
1390 u8
*lut
, u16 lut_size
)
1392 struct i40evf_adapter
*adapter
= vsi
->back
;
1394 if (RSS_AQ(adapter
))
1395 return i40evf_config_rss_aq(vsi
, seed
, lut
, lut_size
);
1397 return i40evf_config_rss_reg(vsi
, seed
, lut
, lut_size
);
1401 * i40evf_get_rss - Get RSS keys and lut
1402 * @vsi: Pointer to vsi structure
1403 * @seed: RSS hash seed
1404 * @lut: Lookup table
1405 * @lut_size: Lookup table size
1407 * Returns 0 on success, negative on failure
1409 int i40evf_get_rss(struct i40e_vsi
*vsi
, const u8
*seed
, u8
*lut
, u16 lut_size
)
1411 struct i40evf_adapter
*adapter
= vsi
->back
;
1413 if (RSS_AQ(adapter
))
1414 return i40evf_get_rss_aq(vsi
, seed
, lut
, lut_size
);
1416 return i40evf_get_rss_reg(vsi
, seed
, lut
, lut_size
);
1420 * i40evf_fill_rss_lut - Fill the lut with default values
1421 * @lut: Lookup table to be filled with
1422 * @rss_table_size: Lookup table size
1423 * @rss_size: Range of queue number for hashing
1425 static void i40evf_fill_rss_lut(u8
*lut
, u16 rss_table_size
, u16 rss_size
)
1429 for (i
= 0; i
< rss_table_size
; i
++)
1430 lut
[i
] = i
% rss_size
;
1434 * i40evf_init_rss - Prepare for RSS
1435 * @adapter: board private structure
1437 * Return 0 on success, negative on failure
1439 static int i40evf_init_rss(struct i40evf_adapter
*adapter
)
1441 struct i40e_vsi
*vsi
= &adapter
->vsi
;
1442 struct i40e_hw
*hw
= &adapter
->hw
;
1443 u8 seed
[I40EVF_HKEY_ARRAY_SIZE
];
1448 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1449 hena
= I40E_DEFAULT_RSS_HENA
;
1450 wr32(hw
, I40E_VFQF_HENA(0), (u32
)hena
);
1451 wr32(hw
, I40E_VFQF_HENA(1), (u32
)(hena
>> 32));
1453 lut
= kzalloc(I40EVF_HLUT_ARRAY_SIZE
, GFP_KERNEL
);
1456 i40evf_fill_rss_lut(lut
, I40EVF_HLUT_ARRAY_SIZE
,
1457 adapter
->num_active_queues
);
1458 netdev_rss_key_fill((void *)seed
, I40EVF_HKEY_ARRAY_SIZE
);
1459 ret
= i40evf_config_rss(vsi
, seed
, lut
, I40EVF_HLUT_ARRAY_SIZE
);
1466 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1467 * @adapter: board private structure to initialize
1469 * We allocate one q_vector per queue interrupt. If allocation fails we
1472 static int i40evf_alloc_q_vectors(struct i40evf_adapter
*adapter
)
1474 int q_idx
, num_q_vectors
;
1475 struct i40e_q_vector
*q_vector
;
1477 num_q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
1479 for (q_idx
= 0; q_idx
< num_q_vectors
; q_idx
++) {
1480 q_vector
= kzalloc(sizeof(*q_vector
), GFP_KERNEL
);
1483 q_vector
->adapter
= adapter
;
1484 q_vector
->vsi
= &adapter
->vsi
;
1485 q_vector
->v_idx
= q_idx
;
1486 netif_napi_add(adapter
->netdev
, &q_vector
->napi
,
1487 i40evf_napi_poll
, NAPI_POLL_WEIGHT
);
1488 adapter
->q_vector
[q_idx
] = q_vector
;
1496 q_vector
= adapter
->q_vector
[q_idx
];
1497 netif_napi_del(&q_vector
->napi
);
1499 adapter
->q_vector
[q_idx
] = NULL
;
1505 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1506 * @adapter: board private structure to initialize
1508 * This function frees the memory allocated to the q_vectors. In addition if
1509 * NAPI is enabled it will delete any references to the NAPI struct prior
1510 * to freeing the q_vector.
1512 static void i40evf_free_q_vectors(struct i40evf_adapter
*adapter
)
1514 int q_idx
, num_q_vectors
;
1517 num_q_vectors
= adapter
->num_msix_vectors
- NONQ_VECS
;
1518 napi_vectors
= adapter
->num_active_queues
;
1520 for (q_idx
= 0; q_idx
< num_q_vectors
; q_idx
++) {
1521 struct i40e_q_vector
*q_vector
= adapter
->q_vector
[q_idx
];
1523 adapter
->q_vector
[q_idx
] = NULL
;
1524 if (q_idx
< napi_vectors
)
1525 netif_napi_del(&q_vector
->napi
);
1531 * i40evf_reset_interrupt_capability - Reset MSIX setup
1532 * @adapter: board private structure
1535 void i40evf_reset_interrupt_capability(struct i40evf_adapter
*adapter
)
1537 pci_disable_msix(adapter
->pdev
);
1538 kfree(adapter
->msix_entries
);
1539 adapter
->msix_entries
= NULL
;
1543 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1544 * @adapter: board private structure to initialize
1547 int i40evf_init_interrupt_scheme(struct i40evf_adapter
*adapter
)
1551 err
= i40evf_set_interrupt_capability(adapter
);
1553 dev_err(&adapter
->pdev
->dev
,
1554 "Unable to setup interrupt capabilities\n");
1555 goto err_set_interrupt
;
1558 err
= i40evf_alloc_q_vectors(adapter
);
1560 dev_err(&adapter
->pdev
->dev
,
1561 "Unable to allocate memory for queue vectors\n");
1562 goto err_alloc_q_vectors
;
1565 err
= i40evf_alloc_queues(adapter
);
1567 dev_err(&adapter
->pdev
->dev
,
1568 "Unable to allocate memory for queues\n");
1569 goto err_alloc_queues
;
1572 dev_info(&adapter
->pdev
->dev
, "Multiqueue %s: Queue pair count = %u",
1573 (adapter
->num_active_queues
> 1) ? "Enabled" : "Disabled",
1574 adapter
->num_active_queues
);
1578 i40evf_free_q_vectors(adapter
);
1579 err_alloc_q_vectors
:
1580 i40evf_reset_interrupt_capability(adapter
);
1586 * i40evf_watchdog_timer - Periodic call-back timer
1587 * @data: pointer to adapter disguised as unsigned long
1589 static void i40evf_watchdog_timer(unsigned long data
)
1591 struct i40evf_adapter
*adapter
= (struct i40evf_adapter
*)data
;
1593 schedule_work(&adapter
->watchdog_task
);
1594 /* timer will be rescheduled in watchdog task */
1598 * i40evf_watchdog_task - Periodic call-back task
1599 * @work: pointer to work_struct
1601 static void i40evf_watchdog_task(struct work_struct
*work
)
1603 struct i40evf_adapter
*adapter
= container_of(work
,
1604 struct i40evf_adapter
,
1606 struct i40e_hw
*hw
= &adapter
->hw
;
1609 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
))
1610 goto restart_watchdog
;
1612 if (adapter
->flags
& I40EVF_FLAG_PF_COMMS_FAILED
) {
1613 reg_val
= rd32(hw
, I40E_VFGEN_RSTAT
) &
1614 I40E_VFGEN_RSTAT_VFR_STATE_MASK
;
1615 if ((reg_val
== I40E_VFR_VFACTIVE
) ||
1616 (reg_val
== I40E_VFR_COMPLETED
)) {
1617 /* A chance for redemption! */
1618 dev_err(&adapter
->pdev
->dev
, "Hardware came out of reset. Attempting reinit.\n");
1619 adapter
->state
= __I40EVF_STARTUP
;
1620 adapter
->flags
&= ~I40EVF_FLAG_PF_COMMS_FAILED
;
1621 schedule_delayed_work(&adapter
->init_task
, 10);
1622 clear_bit(__I40EVF_IN_CRITICAL_TASK
,
1623 &adapter
->crit_section
);
1624 /* Don't reschedule the watchdog, since we've restarted
1625 * the init task. When init_task contacts the PF and
1626 * gets everything set up again, it'll restart the
1627 * watchdog for us. Down, boy. Sit. Stay. Woof.
1631 adapter
->aq_required
= 0;
1632 adapter
->current_op
= I40E_VIRTCHNL_OP_UNKNOWN
;
1636 if ((adapter
->state
< __I40EVF_DOWN
) ||
1637 (adapter
->flags
& I40EVF_FLAG_RESET_PENDING
))
1640 /* check for reset */
1641 reg_val
= rd32(hw
, I40E_VF_ARQLEN1
) & I40E_VF_ARQLEN1_ARQENABLE_MASK
;
1642 if (!(adapter
->flags
& I40EVF_FLAG_RESET_PENDING
) && !reg_val
) {
1643 adapter
->state
= __I40EVF_RESETTING
;
1644 adapter
->flags
|= I40EVF_FLAG_RESET_PENDING
;
1645 dev_err(&adapter
->pdev
->dev
, "Hardware reset detected\n");
1646 schedule_work(&adapter
->reset_task
);
1647 adapter
->aq_required
= 0;
1648 adapter
->current_op
= I40E_VIRTCHNL_OP_UNKNOWN
;
1652 /* Process admin queue tasks. After init, everything gets done
1653 * here so we don't race on the admin queue.
1655 if (adapter
->current_op
) {
1656 if (!i40evf_asq_done(hw
)) {
1657 dev_dbg(&adapter
->pdev
->dev
, "Admin queue timeout\n");
1658 i40evf_send_api_ver(adapter
);
1662 if (adapter
->aq_required
& I40EVF_FLAG_AQ_GET_CONFIG
) {
1663 i40evf_send_vf_config_msg(adapter
);
1667 if (adapter
->aq_required
& I40EVF_FLAG_AQ_DISABLE_QUEUES
) {
1668 i40evf_disable_queues(adapter
);
1672 if (adapter
->aq_required
& I40EVF_FLAG_AQ_MAP_VECTORS
) {
1673 i40evf_map_queues(adapter
);
1677 if (adapter
->aq_required
& I40EVF_FLAG_AQ_ADD_MAC_FILTER
) {
1678 i40evf_add_ether_addrs(adapter
);
1682 if (adapter
->aq_required
& I40EVF_FLAG_AQ_ADD_VLAN_FILTER
) {
1683 i40evf_add_vlans(adapter
);
1687 if (adapter
->aq_required
& I40EVF_FLAG_AQ_DEL_MAC_FILTER
) {
1688 i40evf_del_ether_addrs(adapter
);
1692 if (adapter
->aq_required
& I40EVF_FLAG_AQ_DEL_VLAN_FILTER
) {
1693 i40evf_del_vlans(adapter
);
1697 if (adapter
->aq_required
& I40EVF_FLAG_AQ_CONFIGURE_QUEUES
) {
1698 i40evf_configure_queues(adapter
);
1702 if (adapter
->aq_required
& I40EVF_FLAG_AQ_ENABLE_QUEUES
) {
1703 i40evf_enable_queues(adapter
);
1707 if (adapter
->aq_required
& I40EVF_FLAG_AQ_CONFIGURE_RSS
) {
1708 /* This message goes straight to the firmware, not the
1709 * PF, so we don't have to set current_op as we will
1710 * not get a response through the ARQ.
1712 i40evf_init_rss(adapter
);
1713 adapter
->aq_required
&= ~I40EVF_FLAG_AQ_CONFIGURE_RSS
;
1717 if (adapter
->state
== __I40EVF_RUNNING
)
1718 i40evf_request_stats(adapter
);
1720 if (adapter
->state
== __I40EVF_RUNNING
) {
1721 i40evf_irq_enable_queues(adapter
, ~0);
1722 i40evf_fire_sw_int(adapter
, 0xFF);
1724 i40evf_fire_sw_int(adapter
, 0x1);
1727 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
1729 if (adapter
->state
== __I40EVF_REMOVE
)
1731 if (adapter
->aq_required
)
1732 mod_timer(&adapter
->watchdog_timer
,
1733 jiffies
+ msecs_to_jiffies(20));
1735 mod_timer(&adapter
->watchdog_timer
, jiffies
+ (HZ
* 2));
1736 schedule_work(&adapter
->adminq_task
);
1739 #define I40EVF_RESET_WAIT_MS 10
1740 #define I40EVF_RESET_WAIT_COUNT 500
1742 * i40evf_reset_task - Call-back task to handle hardware reset
1743 * @work: pointer to work_struct
1745 * During reset we need to shut down and reinitialize the admin queue
1746 * before we can use it to communicate with the PF again. We also clear
1747 * and reinit the rings because that context is lost as well.
1749 static void i40evf_reset_task(struct work_struct
*work
)
1751 struct i40evf_adapter
*adapter
= container_of(work
,
1752 struct i40evf_adapter
,
1754 struct net_device
*netdev
= adapter
->netdev
;
1755 struct i40e_hw
*hw
= &adapter
->hw
;
1756 struct i40evf_vlan_filter
*vlf
;
1757 struct i40evf_mac_filter
*f
;
1761 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK
,
1762 &adapter
->crit_section
))
1763 usleep_range(500, 1000);
1765 i40evf_misc_irq_disable(adapter
);
1766 if (adapter
->flags
& I40EVF_FLAG_RESET_NEEDED
) {
1767 adapter
->flags
&= ~I40EVF_FLAG_RESET_NEEDED
;
1768 /* Restart the AQ here. If we have been reset but didn't
1769 * detect it, or if the PF had to reinit, our AQ will be hosed.
1771 i40evf_shutdown_adminq(hw
);
1772 i40evf_init_adminq(hw
);
1773 i40evf_request_reset(adapter
);
1775 adapter
->flags
|= I40EVF_FLAG_RESET_PENDING
;
1777 /* poll until we see the reset actually happen */
1778 for (i
= 0; i
< I40EVF_RESET_WAIT_COUNT
; i
++) {
1779 reg_val
= rd32(hw
, I40E_VF_ARQLEN1
) &
1780 I40E_VF_ARQLEN1_ARQENABLE_MASK
;
1783 usleep_range(5000, 10000);
1785 if (i
== I40EVF_RESET_WAIT_COUNT
) {
1786 dev_info(&adapter
->pdev
->dev
, "Never saw reset\n");
1787 goto continue_reset
; /* act like the reset happened */
1790 /* wait until the reset is complete and the PF is responding to us */
1791 for (i
= 0; i
< I40EVF_RESET_WAIT_COUNT
; i
++) {
1792 reg_val
= rd32(hw
, I40E_VFGEN_RSTAT
) &
1793 I40E_VFGEN_RSTAT_VFR_STATE_MASK
;
1794 if (reg_val
== I40E_VFR_VFACTIVE
)
1796 msleep(I40EVF_RESET_WAIT_MS
);
1798 /* extra wait to make sure minimum wait is met */
1799 msleep(I40EVF_RESET_WAIT_MS
);
1800 if (i
== I40EVF_RESET_WAIT_COUNT
) {
1801 struct i40evf_mac_filter
*ftmp
;
1802 struct i40evf_vlan_filter
*fv
, *fvtmp
;
1804 /* reset never finished */
1805 dev_err(&adapter
->pdev
->dev
, "Reset never finished (%x)\n",
1807 adapter
->flags
|= I40EVF_FLAG_PF_COMMS_FAILED
;
1809 if (netif_running(adapter
->netdev
)) {
1810 set_bit(__I40E_DOWN
, &adapter
->vsi
.state
);
1811 netif_carrier_off(netdev
);
1812 netif_tx_disable(netdev
);
1813 i40evf_napi_disable_all(adapter
);
1814 i40evf_irq_disable(adapter
);
1815 i40evf_free_traffic_irqs(adapter
);
1816 i40evf_free_all_tx_resources(adapter
);
1817 i40evf_free_all_rx_resources(adapter
);
1820 /* Delete all of the filters, both MAC and VLAN. */
1821 list_for_each_entry_safe(f
, ftmp
, &adapter
->mac_filter_list
,
1827 list_for_each_entry_safe(fv
, fvtmp
, &adapter
->vlan_filter_list
,
1829 list_del(&fv
->list
);
1833 i40evf_free_misc_irq(adapter
);
1834 i40evf_reset_interrupt_capability(adapter
);
1835 i40evf_free_queues(adapter
);
1836 i40evf_free_q_vectors(adapter
);
1837 kfree(adapter
->vf_res
);
1838 i40evf_shutdown_adminq(hw
);
1839 adapter
->netdev
->flags
&= ~IFF_UP
;
1840 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
1841 adapter
->flags
&= ~I40EVF_FLAG_RESET_PENDING
;
1842 dev_info(&adapter
->pdev
->dev
, "Reset task did not complete, VF disabled\n");
1843 return; /* Do not attempt to reinit. It's dead, Jim. */
1847 if (netif_running(adapter
->netdev
)) {
1848 netif_carrier_off(netdev
);
1849 netif_tx_stop_all_queues(netdev
);
1850 i40evf_napi_disable_all(adapter
);
1852 i40evf_irq_disable(adapter
);
1854 adapter
->state
= __I40EVF_RESETTING
;
1855 adapter
->flags
&= ~I40EVF_FLAG_RESET_PENDING
;
1857 /* free the Tx/Rx rings and descriptors, might be better to just
1858 * re-use them sometime in the future
1860 i40evf_free_all_rx_resources(adapter
);
1861 i40evf_free_all_tx_resources(adapter
);
1863 /* kill and reinit the admin queue */
1864 if (i40evf_shutdown_adminq(hw
))
1865 dev_warn(&adapter
->pdev
->dev
, "Failed to shut down adminq\n");
1866 adapter
->current_op
= I40E_VIRTCHNL_OP_UNKNOWN
;
1867 err
= i40evf_init_adminq(hw
);
1869 dev_info(&adapter
->pdev
->dev
, "Failed to init adminq: %d\n",
1872 adapter
->aq_required
= I40EVF_FLAG_AQ_GET_CONFIG
;
1873 adapter
->aq_required
|= I40EVF_FLAG_AQ_MAP_VECTORS
;
1875 /* re-add all MAC filters */
1876 list_for_each_entry(f
, &adapter
->mac_filter_list
, list
) {
1879 /* re-add all VLAN filters */
1880 list_for_each_entry(vlf
, &adapter
->vlan_filter_list
, list
) {
1883 adapter
->aq_required
|= I40EVF_FLAG_AQ_ADD_MAC_FILTER
;
1884 adapter
->aq_required
|= I40EVF_FLAG_AQ_ADD_VLAN_FILTER
;
1885 clear_bit(__I40EVF_IN_CRITICAL_TASK
, &adapter
->crit_section
);
1886 i40evf_misc_irq_enable(adapter
);
1888 mod_timer(&adapter
->watchdog_timer
, jiffies
+ 2);
1890 if (netif_running(adapter
->netdev
)) {
1891 /* allocate transmit descriptors */
1892 err
= i40evf_setup_all_tx_resources(adapter
);
1896 /* allocate receive descriptors */
1897 err
= i40evf_setup_all_rx_resources(adapter
);
1901 i40evf_configure(adapter
);
1903 err
= i40evf_up_complete(adapter
);
1907 i40evf_irq_enable(adapter
, true);
1909 adapter
->state
= __I40EVF_DOWN
;
1914 dev_err(&adapter
->pdev
->dev
, "failed to allocate resources during reinit\n");
1915 i40evf_close(adapter
->netdev
);
1919 * i40evf_adminq_task - worker thread to clean the admin queue
1920 * @work: pointer to work_struct containing our data
1922 static void i40evf_adminq_task(struct work_struct
*work
)
1924 struct i40evf_adapter
*adapter
=
1925 container_of(work
, struct i40evf_adapter
, adminq_task
);
1926 struct i40e_hw
*hw
= &adapter
->hw
;
1927 struct i40e_arq_event_info event
;
1928 struct i40e_virtchnl_msg
*v_msg
;
1933 if (adapter
->flags
& I40EVF_FLAG_PF_COMMS_FAILED
)
1936 event
.buf_len
= I40EVF_MAX_AQ_BUF_SIZE
;
1937 event
.msg_buf
= kzalloc(event
.buf_len
, GFP_KERNEL
);
1941 v_msg
= (struct i40e_virtchnl_msg
*)&event
.desc
;
1943 ret
= i40evf_clean_arq_element(hw
, &event
, &pending
);
1944 if (ret
|| !v_msg
->v_opcode
)
1945 break; /* No event to process or error cleaning ARQ */
1947 i40evf_virtchnl_completion(adapter
, v_msg
->v_opcode
,
1948 v_msg
->v_retval
, event
.msg_buf
,
1951 memset(event
.msg_buf
, 0, I40EVF_MAX_AQ_BUF_SIZE
);
1954 if ((adapter
->flags
&
1955 (I40EVF_FLAG_RESET_PENDING
| I40EVF_FLAG_RESET_NEEDED
)) ||
1956 adapter
->state
== __I40EVF_RESETTING
)
1959 /* check for error indications */
1960 val
= rd32(hw
, hw
->aq
.arq
.len
);
1962 if (val
& I40E_VF_ARQLEN1_ARQVFE_MASK
) {
1963 dev_info(&adapter
->pdev
->dev
, "ARQ VF Error detected\n");
1964 val
&= ~I40E_VF_ARQLEN1_ARQVFE_MASK
;
1966 if (val
& I40E_VF_ARQLEN1_ARQOVFL_MASK
) {
1967 dev_info(&adapter
->pdev
->dev
, "ARQ Overflow Error detected\n");
1968 val
&= ~I40E_VF_ARQLEN1_ARQOVFL_MASK
;
1970 if (val
& I40E_VF_ARQLEN1_ARQCRIT_MASK
) {
1971 dev_info(&adapter
->pdev
->dev
, "ARQ Critical Error detected\n");
1972 val
&= ~I40E_VF_ARQLEN1_ARQCRIT_MASK
;
1975 wr32(hw
, hw
->aq
.arq
.len
, val
);
1977 val
= rd32(hw
, hw
->aq
.asq
.len
);
1979 if (val
& I40E_VF_ATQLEN1_ATQVFE_MASK
) {
1980 dev_info(&adapter
->pdev
->dev
, "ASQ VF Error detected\n");
1981 val
&= ~I40E_VF_ATQLEN1_ATQVFE_MASK
;
1983 if (val
& I40E_VF_ATQLEN1_ATQOVFL_MASK
) {
1984 dev_info(&adapter
->pdev
->dev
, "ASQ Overflow Error detected\n");
1985 val
&= ~I40E_VF_ATQLEN1_ATQOVFL_MASK
;
1987 if (val
& I40E_VF_ATQLEN1_ATQCRIT_MASK
) {
1988 dev_info(&adapter
->pdev
->dev
, "ASQ Critical Error detected\n");
1989 val
&= ~I40E_VF_ATQLEN1_ATQCRIT_MASK
;
1992 wr32(hw
, hw
->aq
.asq
.len
, val
);
1995 kfree(event
.msg_buf
);
1997 /* re-enable Admin queue interrupt cause */
1998 i40evf_misc_irq_enable(adapter
);
2002 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2003 * @adapter: board private structure
2005 * Free all transmit software resources
2007 void i40evf_free_all_tx_resources(struct i40evf_adapter
*adapter
)
2011 for (i
= 0; i
< adapter
->num_active_queues
; i
++)
2012 if (adapter
->tx_rings
[i
]->desc
)
2013 i40evf_free_tx_resources(adapter
->tx_rings
[i
]);
2017 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2018 * @adapter: board private structure
2020 * If this function returns with an error, then it's possible one or
2021 * more of the rings is populated (while the rest are not). It is the
2022 * callers duty to clean those orphaned rings.
2024 * Return 0 on success, negative on failure
2026 static int i40evf_setup_all_tx_resources(struct i40evf_adapter
*adapter
)
2030 for (i
= 0; i
< adapter
->num_active_queues
; i
++) {
2031 adapter
->tx_rings
[i
]->count
= adapter
->tx_desc_count
;
2032 err
= i40evf_setup_tx_descriptors(adapter
->tx_rings
[i
]);
2035 dev_err(&adapter
->pdev
->dev
,
2036 "Allocation for Tx Queue %u failed\n", i
);
2044 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2045 * @adapter: board private structure
2047 * If this function returns with an error, then it's possible one or
2048 * more of the rings is populated (while the rest are not). It is the
2049 * callers duty to clean those orphaned rings.
2051 * Return 0 on success, negative on failure
2053 static int i40evf_setup_all_rx_resources(struct i40evf_adapter
*adapter
)
2057 for (i
= 0; i
< adapter
->num_active_queues
; i
++) {
2058 adapter
->rx_rings
[i
]->count
= adapter
->rx_desc_count
;
2059 err
= i40evf_setup_rx_descriptors(adapter
->rx_rings
[i
]);
2062 dev_err(&adapter
->pdev
->dev
,
2063 "Allocation for Rx Queue %u failed\n", i
);
2070 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2071 * @adapter: board private structure
2073 * Free all receive software resources
2075 void i40evf_free_all_rx_resources(struct i40evf_adapter
*adapter
)
2079 for (i
= 0; i
< adapter
->num_active_queues
; i
++)
2080 if (adapter
->rx_rings
[i
]->desc
)
2081 i40evf_free_rx_resources(adapter
->rx_rings
[i
]);
2085 * i40evf_open - Called when a network interface is made active
2086 * @netdev: network interface device structure
2088 * Returns 0 on success, negative value on failure
2090 * The open entry point is called when a network interface is made
2091 * active by the system (IFF_UP). At this point all resources needed
2092 * for transmit and receive operations are allocated, the interrupt
2093 * handler is registered with the OS, the watchdog timer is started,
2094 * and the stack is notified that the interface is ready.
2096 static int i40evf_open(struct net_device
*netdev
)
2098 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2101 if (adapter
->flags
& I40EVF_FLAG_PF_COMMS_FAILED
) {
2102 dev_err(&adapter
->pdev
->dev
, "Unable to open device due to PF driver failure.\n");
2105 if (adapter
->state
!= __I40EVF_DOWN
|| adapter
->aq_required
)
2108 /* allocate transmit descriptors */
2109 err
= i40evf_setup_all_tx_resources(adapter
);
2113 /* allocate receive descriptors */
2114 err
= i40evf_setup_all_rx_resources(adapter
);
2118 /* clear any pending interrupts, may auto mask */
2119 err
= i40evf_request_traffic_irqs(adapter
, netdev
->name
);
2123 i40evf_add_filter(adapter
, adapter
->hw
.mac
.addr
);
2124 i40evf_configure(adapter
);
2126 err
= i40evf_up_complete(adapter
);
2130 i40evf_irq_enable(adapter
, true);
2135 i40evf_down(adapter
);
2136 i40evf_free_traffic_irqs(adapter
);
2138 i40evf_free_all_rx_resources(adapter
);
2140 i40evf_free_all_tx_resources(adapter
);
2146 * i40evf_close - Disables a network interface
2147 * @netdev: network interface device structure
2149 * Returns 0, this is not allowed to fail
2151 * The close entry point is called when an interface is de-activated
2152 * by the OS. The hardware is still under the drivers control, but
2153 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2154 * are freed, along with all transmit and receive resources.
2156 static int i40evf_close(struct net_device
*netdev
)
2158 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2160 if (adapter
->state
<= __I40EVF_DOWN
)
2164 set_bit(__I40E_DOWN
, &adapter
->vsi
.state
);
2166 i40evf_down(adapter
);
2167 adapter
->state
= __I40EVF_DOWN
;
2168 i40evf_free_traffic_irqs(adapter
);
2174 * i40evf_get_stats - Get System Network Statistics
2175 * @netdev: network interface device structure
2177 * Returns the address of the device statistics structure.
2178 * The statistics are actually updated from the timer callback.
2180 static struct net_device_stats
*i40evf_get_stats(struct net_device
*netdev
)
2182 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2184 /* only return the current stats */
2185 return &adapter
->net_stats
;
2189 * i40evf_change_mtu - Change the Maximum Transfer Unit
2190 * @netdev: network interface device structure
2191 * @new_mtu: new value for maximum frame size
2193 * Returns 0 on success, negative on failure
2195 static int i40evf_change_mtu(struct net_device
*netdev
, int new_mtu
)
2197 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2198 int max_frame
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
;
2200 if ((new_mtu
< 68) || (max_frame
> I40E_MAX_RXBUFFER
))
2203 netdev
->mtu
= new_mtu
;
2204 adapter
->flags
|= I40EVF_FLAG_RESET_NEEDED
;
2205 schedule_work(&adapter
->reset_task
);
2210 static const struct net_device_ops i40evf_netdev_ops
= {
2211 .ndo_open
= i40evf_open
,
2212 .ndo_stop
= i40evf_close
,
2213 .ndo_start_xmit
= i40evf_xmit_frame
,
2214 .ndo_get_stats
= i40evf_get_stats
,
2215 .ndo_set_rx_mode
= i40evf_set_rx_mode
,
2216 .ndo_validate_addr
= eth_validate_addr
,
2217 .ndo_set_mac_address
= i40evf_set_mac
,
2218 .ndo_change_mtu
= i40evf_change_mtu
,
2219 .ndo_tx_timeout
= i40evf_tx_timeout
,
2220 .ndo_vlan_rx_add_vid
= i40evf_vlan_rx_add_vid
,
2221 .ndo_vlan_rx_kill_vid
= i40evf_vlan_rx_kill_vid
,
2222 #ifdef CONFIG_NET_POLL_CONTROLLER
2223 .ndo_poll_controller
= i40evf_netpoll
,
2228 * i40evf_check_reset_complete - check that VF reset is complete
2229 * @hw: pointer to hw struct
2231 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2233 static int i40evf_check_reset_complete(struct i40e_hw
*hw
)
2238 for (i
= 0; i
< 100; i
++) {
2239 rstat
= rd32(hw
, I40E_VFGEN_RSTAT
) &
2240 I40E_VFGEN_RSTAT_VFR_STATE_MASK
;
2241 if ((rstat
== I40E_VFR_VFACTIVE
) ||
2242 (rstat
== I40E_VFR_COMPLETED
))
2244 usleep_range(10, 20);
2250 * i40evf_process_config - Process the config information we got from the PF
2251 * @adapter: board private structure
2253 * Verify that we have a valid config struct, and set up our netdev features
2254 * and our VSI struct.
2256 int i40evf_process_config(struct i40evf_adapter
*adapter
)
2258 struct net_device
*netdev
= adapter
->netdev
;
2261 /* got VF config message back from PF, now we can parse it */
2262 for (i
= 0; i
< adapter
->vf_res
->num_vsis
; i
++) {
2263 if (adapter
->vf_res
->vsi_res
[i
].vsi_type
== I40E_VSI_SRIOV
)
2264 adapter
->vsi_res
= &adapter
->vf_res
->vsi_res
[i
];
2266 if (!adapter
->vsi_res
) {
2267 dev_err(&adapter
->pdev
->dev
, "No LAN VSI found\n");
2271 if (adapter
->vf_res
->vf_offload_flags
2272 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN
) {
2273 netdev
->vlan_features
= netdev
->features
&
2274 ~(NETIF_F_HW_VLAN_CTAG_TX
|
2275 NETIF_F_HW_VLAN_CTAG_RX
|
2276 NETIF_F_HW_VLAN_CTAG_FILTER
);
2277 netdev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
|
2278 NETIF_F_HW_VLAN_CTAG_RX
|
2279 NETIF_F_HW_VLAN_CTAG_FILTER
;
2281 netdev
->features
|= NETIF_F_HIGHDMA
|
2291 /* copy netdev features into list of user selectable features */
2292 netdev
->hw_features
|= netdev
->features
;
2293 netdev
->hw_features
&= ~NETIF_F_RXCSUM
;
2295 adapter
->vsi
.id
= adapter
->vsi_res
->vsi_id
;
2297 adapter
->vsi
.back
= adapter
;
2298 adapter
->vsi
.base_vector
= 1;
2299 adapter
->vsi
.work_limit
= I40E_DEFAULT_IRQ_WORK
;
2300 adapter
->vsi
.rx_itr_setting
= (I40E_ITR_DYNAMIC
|
2301 ITR_REG_TO_USEC(I40E_ITR_RX_DEF
));
2302 adapter
->vsi
.tx_itr_setting
= (I40E_ITR_DYNAMIC
|
2303 ITR_REG_TO_USEC(I40E_ITR_TX_DEF
));
2304 adapter
->vsi
.netdev
= adapter
->netdev
;
2305 adapter
->vsi
.qs_handle
= adapter
->vsi_res
->qset_handle
;
2310 * i40evf_init_task - worker thread to perform delayed initialization
2311 * @work: pointer to work_struct containing our data
2313 * This task completes the work that was begun in probe. Due to the nature
2314 * of VF-PF communications, we may need to wait tens of milliseconds to get
2315 * responses back from the PF. Rather than busy-wait in probe and bog down the
2316 * whole system, we'll do it in a task so we can sleep.
2317 * This task only runs during driver init. Once we've established
2318 * communications with the PF driver and set up our netdev, the watchdog
2321 static void i40evf_init_task(struct work_struct
*work
)
2323 struct i40evf_adapter
*adapter
= container_of(work
,
2324 struct i40evf_adapter
,
2326 struct net_device
*netdev
= adapter
->netdev
;
2327 struct i40e_hw
*hw
= &adapter
->hw
;
2328 struct pci_dev
*pdev
= adapter
->pdev
;
2331 switch (adapter
->state
) {
2332 case __I40EVF_STARTUP
:
2333 /* driver loaded, probe complete */
2334 adapter
->flags
&= ~I40EVF_FLAG_PF_COMMS_FAILED
;
2335 adapter
->flags
&= ~I40EVF_FLAG_RESET_PENDING
;
2336 err
= i40e_set_mac_type(hw
);
2338 dev_err(&pdev
->dev
, "Failed to set MAC type (%d)\n",
2342 err
= i40evf_check_reset_complete(hw
);
2344 dev_info(&pdev
->dev
, "Device is still in reset (%d), retrying\n",
2348 hw
->aq
.num_arq_entries
= I40EVF_AQ_LEN
;
2349 hw
->aq
.num_asq_entries
= I40EVF_AQ_LEN
;
2350 hw
->aq
.arq_buf_size
= I40EVF_MAX_AQ_BUF_SIZE
;
2351 hw
->aq
.asq_buf_size
= I40EVF_MAX_AQ_BUF_SIZE
;
2353 err
= i40evf_init_adminq(hw
);
2355 dev_err(&pdev
->dev
, "Failed to init Admin Queue (%d)\n",
2359 err
= i40evf_send_api_ver(adapter
);
2361 dev_err(&pdev
->dev
, "Unable to send to PF (%d)\n", err
);
2362 i40evf_shutdown_adminq(hw
);
2365 adapter
->state
= __I40EVF_INIT_VERSION_CHECK
;
2367 case __I40EVF_INIT_VERSION_CHECK
:
2368 if (!i40evf_asq_done(hw
)) {
2369 dev_err(&pdev
->dev
, "Admin queue command never completed\n");
2370 i40evf_shutdown_adminq(hw
);
2371 adapter
->state
= __I40EVF_STARTUP
;
2375 /* aq msg sent, awaiting reply */
2376 err
= i40evf_verify_api_ver(adapter
);
2378 if (err
== I40E_ERR_ADMIN_QUEUE_NO_WORK
)
2379 err
= i40evf_send_api_ver(adapter
);
2381 dev_err(&pdev
->dev
, "Unsupported PF API version %d.%d, expected %d.%d\n",
2382 adapter
->pf_version
.major
,
2383 adapter
->pf_version
.minor
,
2384 I40E_VIRTCHNL_VERSION_MAJOR
,
2385 I40E_VIRTCHNL_VERSION_MINOR
);
2388 err
= i40evf_send_vf_config_msg(adapter
);
2390 dev_err(&pdev
->dev
, "Unable to send config request (%d)\n",
2394 adapter
->state
= __I40EVF_INIT_GET_RESOURCES
;
2396 case __I40EVF_INIT_GET_RESOURCES
:
2397 /* aq msg sent, awaiting reply */
2398 if (!adapter
->vf_res
) {
2399 bufsz
= sizeof(struct i40e_virtchnl_vf_resource
) +
2401 sizeof(struct i40e_virtchnl_vsi_resource
));
2402 adapter
->vf_res
= kzalloc(bufsz
, GFP_KERNEL
);
2403 if (!adapter
->vf_res
)
2406 err
= i40evf_get_vf_config(adapter
);
2407 if (err
== I40E_ERR_ADMIN_QUEUE_NO_WORK
) {
2408 err
= i40evf_send_vf_config_msg(adapter
);
2412 dev_err(&pdev
->dev
, "Unable to get VF config (%d)\n",
2416 adapter
->state
= __I40EVF_INIT_SW
;
2421 if (i40evf_process_config(adapter
))
2423 adapter
->current_op
= I40E_VIRTCHNL_OP_UNKNOWN
;
2425 adapter
->flags
|= I40EVF_FLAG_RX_CSUM_ENABLED
;
2427 netdev
->netdev_ops
= &i40evf_netdev_ops
;
2428 i40evf_set_ethtool_ops(netdev
);
2429 netdev
->watchdog_timeo
= 5 * HZ
;
2431 if (!is_valid_ether_addr(adapter
->hw
.mac
.addr
)) {
2432 dev_info(&pdev
->dev
, "Invalid MAC address %pM, using random\n",
2433 adapter
->hw
.mac
.addr
);
2434 eth_hw_addr_random(netdev
);
2435 ether_addr_copy(adapter
->hw
.mac
.addr
, netdev
->dev_addr
);
2437 adapter
->flags
|= I40EVF_FLAG_ADDR_SET_BY_PF
;
2438 ether_addr_copy(netdev
->dev_addr
, adapter
->hw
.mac
.addr
);
2439 ether_addr_copy(netdev
->perm_addr
, adapter
->hw
.mac
.addr
);
2442 init_timer(&adapter
->watchdog_timer
);
2443 adapter
->watchdog_timer
.function
= &i40evf_watchdog_timer
;
2444 adapter
->watchdog_timer
.data
= (unsigned long)adapter
;
2445 mod_timer(&adapter
->watchdog_timer
, jiffies
+ 1);
2447 adapter
->num_active_queues
= min_t(int,
2448 adapter
->vsi_res
->num_queue_pairs
,
2449 (int)(num_online_cpus()));
2450 adapter
->tx_desc_count
= I40EVF_DEFAULT_TXD
;
2451 adapter
->rx_desc_count
= I40EVF_DEFAULT_RXD
;
2452 err
= i40evf_init_interrupt_scheme(adapter
);
2455 i40evf_map_rings_to_vectors(adapter
);
2456 if (adapter
->vf_res
->vf_offload_flags
&
2457 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR
)
2458 adapter
->flags
|= I40EVF_FLAG_WB_ON_ITR_CAPABLE
;
2459 if (!RSS_AQ(adapter
))
2460 i40evf_init_rss(adapter
);
2461 err
= i40evf_request_misc_irq(adapter
);
2465 netif_carrier_off(netdev
);
2467 if (!adapter
->netdev_registered
) {
2468 err
= register_netdev(netdev
);
2473 adapter
->netdev_registered
= true;
2475 netif_tx_stop_all_queues(netdev
);
2477 dev_info(&pdev
->dev
, "MAC address: %pM\n", adapter
->hw
.mac
.addr
);
2478 if (netdev
->features
& NETIF_F_GRO
)
2479 dev_info(&pdev
->dev
, "GRO is enabled\n");
2481 dev_info(&pdev
->dev
, "%s\n", i40evf_driver_string
);
2482 adapter
->state
= __I40EVF_DOWN
;
2483 set_bit(__I40E_DOWN
, &adapter
->vsi
.state
);
2484 i40evf_misc_irq_enable(adapter
);
2486 if (RSS_AQ(adapter
)) {
2487 adapter
->aq_required
|= I40EVF_FLAG_AQ_CONFIGURE_RSS
;
2488 mod_timer_pending(&adapter
->watchdog_timer
, jiffies
+ 1);
2490 i40evf_init_rss(adapter
);
2494 schedule_delayed_work(&adapter
->init_task
, msecs_to_jiffies(30));
2498 i40evf_free_misc_irq(adapter
);
2500 i40evf_reset_interrupt_capability(adapter
);
2502 kfree(adapter
->vf_res
);
2503 adapter
->vf_res
= NULL
;
2505 /* Things went into the weeds, so try again later */
2506 if (++adapter
->aq_wait_count
> I40EVF_AQ_MAX_ERR
) {
2507 dev_err(&pdev
->dev
, "Failed to communicate with PF; waiting before retry\n");
2508 adapter
->flags
|= I40EVF_FLAG_PF_COMMS_FAILED
;
2509 i40evf_shutdown_adminq(hw
);
2510 adapter
->state
= __I40EVF_STARTUP
;
2511 schedule_delayed_work(&adapter
->init_task
, HZ
* 5);
2514 schedule_delayed_work(&adapter
->init_task
, HZ
);
2518 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2519 * @pdev: pci device structure
2521 static void i40evf_shutdown(struct pci_dev
*pdev
)
2523 struct net_device
*netdev
= pci_get_drvdata(pdev
);
2524 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2526 netif_device_detach(netdev
);
2528 if (netif_running(netdev
))
2529 i40evf_close(netdev
);
2531 /* Prevent the watchdog from running. */
2532 adapter
->state
= __I40EVF_REMOVE
;
2533 adapter
->aq_required
= 0;
2536 pci_save_state(pdev
);
2539 pci_disable_device(pdev
);
2543 * i40evf_probe - Device Initialization Routine
2544 * @pdev: PCI device information struct
2545 * @ent: entry in i40evf_pci_tbl
2547 * Returns 0 on success, negative on failure
2549 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2550 * The OS initialization, configuring of the adapter private structure,
2551 * and a hardware reset occur.
2553 static int i40evf_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
2555 struct net_device
*netdev
;
2556 struct i40evf_adapter
*adapter
= NULL
;
2557 struct i40e_hw
*hw
= NULL
;
2560 err
= pci_enable_device(pdev
);
2564 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
2566 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
2569 "DMA configuration failed: 0x%x\n", err
);
2574 err
= pci_request_regions(pdev
, i40evf_driver_name
);
2577 "pci_request_regions failed 0x%x\n", err
);
2581 pci_enable_pcie_error_reporting(pdev
);
2583 pci_set_master(pdev
);
2585 netdev
= alloc_etherdev_mq(sizeof(struct i40evf_adapter
),
2589 goto err_alloc_etherdev
;
2592 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
2594 pci_set_drvdata(pdev
, netdev
);
2595 adapter
= netdev_priv(netdev
);
2597 adapter
->netdev
= netdev
;
2598 adapter
->pdev
= pdev
;
2603 adapter
->msg_enable
= BIT(DEFAULT_DEBUG_LEVEL_SHIFT
) - 1;
2604 adapter
->state
= __I40EVF_STARTUP
;
2606 /* Call save state here because it relies on the adapter struct. */
2607 pci_save_state(pdev
);
2609 hw
->hw_addr
= ioremap(pci_resource_start(pdev
, 0),
2610 pci_resource_len(pdev
, 0));
2615 hw
->vendor_id
= pdev
->vendor
;
2616 hw
->device_id
= pdev
->device
;
2617 pci_read_config_byte(pdev
, PCI_REVISION_ID
, &hw
->revision_id
);
2618 hw
->subsystem_vendor_id
= pdev
->subsystem_vendor
;
2619 hw
->subsystem_device_id
= pdev
->subsystem_device
;
2620 hw
->bus
.device
= PCI_SLOT(pdev
->devfn
);
2621 hw
->bus
.func
= PCI_FUNC(pdev
->devfn
);
2623 INIT_LIST_HEAD(&adapter
->mac_filter_list
);
2624 INIT_LIST_HEAD(&adapter
->vlan_filter_list
);
2626 INIT_WORK(&adapter
->reset_task
, i40evf_reset_task
);
2627 INIT_WORK(&adapter
->adminq_task
, i40evf_adminq_task
);
2628 INIT_WORK(&adapter
->watchdog_task
, i40evf_watchdog_task
);
2629 INIT_DELAYED_WORK(&adapter
->init_task
, i40evf_init_task
);
2630 schedule_delayed_work(&adapter
->init_task
,
2631 msecs_to_jiffies(5 * (pdev
->devfn
& 0x07)));
2636 free_netdev(netdev
);
2638 pci_release_regions(pdev
);
2641 pci_disable_device(pdev
);
2647 * i40evf_suspend - Power management suspend routine
2648 * @pdev: PCI device information struct
2651 * Called when the system (VM) is entering sleep/suspend.
2653 static int i40evf_suspend(struct pci_dev
*pdev
, pm_message_t state
)
2655 struct net_device
*netdev
= pci_get_drvdata(pdev
);
2656 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2659 netif_device_detach(netdev
);
2661 if (netif_running(netdev
)) {
2663 i40evf_down(adapter
);
2666 i40evf_free_misc_irq(adapter
);
2667 i40evf_reset_interrupt_capability(adapter
);
2669 retval
= pci_save_state(pdev
);
2673 pci_disable_device(pdev
);
2679 * i40evf_resume - Power management resume routine
2680 * @pdev: PCI device information struct
2682 * Called when the system (VM) is resumed from sleep/suspend.
2684 static int i40evf_resume(struct pci_dev
*pdev
)
2686 struct i40evf_adapter
*adapter
= pci_get_drvdata(pdev
);
2687 struct net_device
*netdev
= adapter
->netdev
;
2690 pci_set_power_state(pdev
, PCI_D0
);
2691 pci_restore_state(pdev
);
2692 /* pci_restore_state clears dev->state_saved so call
2693 * pci_save_state to restore it.
2695 pci_save_state(pdev
);
2697 err
= pci_enable_device_mem(pdev
);
2699 dev_err(&pdev
->dev
, "Cannot enable PCI device from suspend.\n");
2702 pci_set_master(pdev
);
2705 err
= i40evf_set_interrupt_capability(adapter
);
2708 dev_err(&pdev
->dev
, "Cannot enable MSI-X interrupts.\n");
2711 err
= i40evf_request_misc_irq(adapter
);
2714 dev_err(&pdev
->dev
, "Cannot get interrupt vector.\n");
2718 schedule_work(&adapter
->reset_task
);
2720 netif_device_attach(netdev
);
2725 #endif /* CONFIG_PM */
2727 * i40evf_remove - Device Removal Routine
2728 * @pdev: PCI device information struct
2730 * i40evf_remove is called by the PCI subsystem to alert the driver
2731 * that it should release a PCI device. The could be caused by a
2732 * Hot-Plug event, or because the driver is going to be removed from
2735 static void i40evf_remove(struct pci_dev
*pdev
)
2737 struct net_device
*netdev
= pci_get_drvdata(pdev
);
2738 struct i40evf_adapter
*adapter
= netdev_priv(netdev
);
2739 struct i40evf_mac_filter
*f
, *ftmp
;
2740 struct i40e_hw
*hw
= &adapter
->hw
;
2742 cancel_delayed_work_sync(&adapter
->init_task
);
2743 cancel_work_sync(&adapter
->reset_task
);
2745 if (adapter
->netdev_registered
) {
2746 unregister_netdev(netdev
);
2747 adapter
->netdev_registered
= false;
2750 /* Shut down all the garbage mashers on the detention level */
2751 adapter
->state
= __I40EVF_REMOVE
;
2752 adapter
->aq_required
= 0;
2753 i40evf_request_reset(adapter
);
2755 /* If the FW isn't responding, kick it once, but only once. */
2756 if (!i40evf_asq_done(hw
)) {
2757 i40evf_request_reset(adapter
);
2761 if (adapter
->msix_entries
) {
2762 i40evf_misc_irq_disable(adapter
);
2763 i40evf_free_misc_irq(adapter
);
2764 i40evf_reset_interrupt_capability(adapter
);
2765 i40evf_free_q_vectors(adapter
);
2768 if (adapter
->watchdog_timer
.function
)
2769 del_timer_sync(&adapter
->watchdog_timer
);
2771 flush_scheduled_work();
2773 if (hw
->aq
.asq
.count
)
2774 i40evf_shutdown_adminq(hw
);
2776 iounmap(hw
->hw_addr
);
2777 pci_release_regions(pdev
);
2779 i40evf_free_all_tx_resources(adapter
);
2780 i40evf_free_all_rx_resources(adapter
);
2781 i40evf_free_queues(adapter
);
2782 kfree(adapter
->vf_res
);
2783 /* If we got removed before an up/down sequence, we've got a filter
2784 * hanging out there that we need to get rid of.
2786 list_for_each_entry_safe(f
, ftmp
, &adapter
->mac_filter_list
, list
) {
2790 list_for_each_entry_safe(f
, ftmp
, &adapter
->vlan_filter_list
, list
) {
2795 free_netdev(netdev
);
2797 pci_disable_pcie_error_reporting(pdev
);
2799 pci_disable_device(pdev
);
2802 static struct pci_driver i40evf_driver
= {
2803 .name
= i40evf_driver_name
,
2804 .id_table
= i40evf_pci_tbl
,
2805 .probe
= i40evf_probe
,
2806 .remove
= i40evf_remove
,
2808 .suspend
= i40evf_suspend
,
2809 .resume
= i40evf_resume
,
2811 .shutdown
= i40evf_shutdown
,
2815 * i40e_init_module - Driver Registration Routine
2817 * i40e_init_module is the first routine called when the driver is
2818 * loaded. All it does is register with the PCI subsystem.
2820 static int __init
i40evf_init_module(void)
2824 pr_info("i40evf: %s - version %s\n", i40evf_driver_string
,
2825 i40evf_driver_version
);
2827 pr_info("%s\n", i40evf_copyright
);
2829 ret
= pci_register_driver(&i40evf_driver
);
2833 module_init(i40evf_init_module
);
2836 * i40e_exit_module - Driver Exit Cleanup Routine
2838 * i40e_exit_module is called just before the driver is removed
2841 static void __exit
i40evf_exit_module(void)
2843 pci_unregister_driver(&i40evf_driver
);
2846 module_exit(i40evf_exit_module
);