]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/ethernet/intel/i40evf/i40evf_main.c
i40e: force VMDQ device name truncation
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ethernet / intel / i40evf / i40evf_main.c
CommitLineData
5eae00c5
GR
1/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
eaab59e9 4 * Copyright(c) 2013 - 2016 Intel Corporation.
5eae00c5
GR
5 *
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.
9 *
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
13 * more details.
14 *
b831607d
JB
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/>.
17 *
5eae00c5
GR
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
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
24 *
25 ******************************************************************************/
26
27#include "i40evf.h"
28#include "i40e_prototype.h"
ed0e894d 29#include "i40evf_client.h"
ed0980c4
SP
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
33 */
34#define CREATE_TRACE_POINTS
35#include "i40e_trace.h"
36
5eae00c5
GR
37static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39static int i40evf_close(struct net_device *netdev);
40
41char i40evf_driver_name[] = "i40evf";
42static const char i40evf_driver_string[] =
eaab59e9 43 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
5eae00c5 44
69ebe955
MW
45#define DRV_KERN "-k"
46
abf709a1
PB
47#define DRV_VERSION_MAJOR 3
48#define DRV_VERSION_MINOR 0
49#define DRV_VERSION_BUILD 0
69ebe955
MW
50#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) \
53 DRV_KERN
5eae00c5
GR
54const char i40evf_driver_version[] = DRV_VERSION;
55static const char i40evf_copyright[] =
bf41846e 56 "Copyright (c) 2013 - 2015 Intel Corporation.";
5eae00c5
GR
57
58/* i40evf_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
9baa3c34 66static const struct pci_device_id i40evf_pci_tbl[] = {
ab60085e 67 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
92871412 68 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
87e6c1d7 69 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
abf709a1 70 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
5eae00c5
GR
71 /* required last entry */
72 {0, }
73};
74
75MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
76
77MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(DRV_VERSION);
81
2803b16c
JB
82static struct workqueue_struct *i40evf_wq;
83
5eae00c5
GR
84/**
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
90 **/
91i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92 struct i40e_dma_mem *mem,
93 u64 size, u32 alignment)
94{
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
96
97 if (!mem)
98 return I40E_ERR_PARAM;
99
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);
103 if (mem->va)
104 return 0;
105 else
106 return I40E_ERR_NO_MEMORY;
107}
108
109/**
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
113 **/
114i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
115{
116 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
117
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);
122 return 0;
123}
124
125/**
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
130 **/
131i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem, u32 size)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 mem->size = size;
138 mem->va = kzalloc(size, GFP_KERNEL);
139
140 if (mem->va)
141 return 0;
142 else
143 return I40E_ERR_NO_MEMORY;
144}
145
146/**
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
150 **/
151i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152 struct i40e_virt_mem *mem)
153{
154 if (!mem)
155 return I40E_ERR_PARAM;
156
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159
160 return 0;
161}
162
163/**
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
168 **/
169void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
170{
171 char buf[512];
172 va_list argptr;
173
174 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
175 return;
176
177 va_start(argptr, fmt_str);
178 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
179 va_end(argptr);
180
181 /* the debug string is already formatted with a newline */
182 pr_info("%s", buf);
183}
184
00e5ec4b
MW
185/**
186 * i40evf_schedule_reset - Set the flags and schedule a reset event
187 * @adapter: board private structure
188 **/
189void i40evf_schedule_reset(struct i40evf_adapter *adapter)
190{
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);
195 }
196}
197
5eae00c5
GR
198/**
199 * i40evf_tx_timeout - Respond to a Tx Hang
200 * @netdev: network interface device structure
201 **/
202static void i40evf_tx_timeout(struct net_device *netdev)
203{
204 struct i40evf_adapter *adapter = netdev_priv(netdev);
205
206 adapter->tx_timeout_count++;
00e5ec4b 207 i40evf_schedule_reset(adapter);
5eae00c5
GR
208}
209
210/**
211 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212 * @adapter: board private structure
213 **/
214static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
215{
216 struct i40e_hw *hw = &adapter->hw;
75a64435 217
ef4603e8
JK
218 if (!adapter->msix_entries)
219 return;
220
5eae00c5
GR
221 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
222
223 /* read flush */
224 rd32(hw, I40E_VFGEN_RSTAT);
225
226 synchronize_irq(adapter->msix_entries[0].vector);
227}
228
229/**
230 * i40evf_misc_irq_enable - Enable default interrupt generation settings
231 * @adapter: board private structure
232 **/
233static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
234{
235 struct i40e_hw *hw = &adapter->hw;
75a64435 236
5eae00c5
GR
237 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
b1f3366b 239 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
5eae00c5
GR
240
241 /* read flush */
242 rd32(hw, I40E_VFGEN_RSTAT);
243}
244
245/**
246 * i40evf_irq_disable - Mask off interrupt generation on the NIC
247 * @adapter: board private structure
248 **/
249static void i40evf_irq_disable(struct i40evf_adapter *adapter)
250{
251 int i;
252 struct i40e_hw *hw = &adapter->hw;
253
dbb01c8a
MW
254 if (!adapter->msix_entries)
255 return;
256
5eae00c5
GR
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);
260 }
261 /* read flush */
262 rd32(hw, I40E_VFGEN_RSTAT);
5eae00c5
GR
263}
264
265/**
266 * i40evf_irq_enable_queues - Enable interrupt for specified queues
267 * @adapter: board private structure
268 * @mask: bitmap of queues to enable
269 **/
270void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
271{
272 struct i40e_hw *hw = &adapter->hw;
273 int i;
274
275 for (i = 1; i < adapter->num_msix_vectors; i++) {
41a1d04b 276 if (mask & BIT(i - 1)) {
5eae00c5
GR
277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
97bf75f1 279 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
b1f3366b 280 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
5eae00c5
GR
281 }
282 }
283}
284
285/**
286 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
287 * @adapter: board private structure
288 * @mask: bitmap of vectors to trigger
289 **/
75a64435 290static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
5eae00c5
GR
291{
292 struct i40e_hw *hw = &adapter->hw;
293 int i;
d82acb35 294 u32 dyn_ctl;
5eae00c5 295
164ec1bf
MW
296 if (mask & 1) {
297 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
b1f3366b 298 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
97bf75f1 299 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
b1f3366b 300 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
164ec1bf
MW
301 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
302 }
5eae00c5 303 for (i = 1; i < adapter->num_msix_vectors; i++) {
41a1d04b 304 if (mask & BIT(i)) {
5eae00c5 305 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
b1f3366b 306 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
97bf75f1 307 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
b1f3366b 308 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
5eae00c5
GR
309 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
310 }
311 }
312}
313
314/**
315 * i40evf_irq_enable - Enable default interrupt generation settings
316 * @adapter: board private structure
69c1d70a 317 * @flush: boolean value whether to run rd32()
5eae00c5
GR
318 **/
319void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
320{
321 struct i40e_hw *hw = &adapter->hw;
322
164ec1bf 323 i40evf_misc_irq_enable(adapter);
5eae00c5
GR
324 i40evf_irq_enable_queues(adapter, ~0);
325
326 if (flush)
327 rd32(hw, I40E_VFGEN_RSTAT);
328}
329
330/**
331 * i40evf_msix_aq - Interrupt handler for vector 0
332 * @irq: interrupt number
333 * @data: pointer to netdev
334 **/
335static irqreturn_t i40evf_msix_aq(int irq, void *data)
336{
337 struct net_device *netdev = data;
338 struct i40evf_adapter *adapter = netdev_priv(netdev);
339 struct i40e_hw *hw = &adapter->hw;
340 u32 val;
5eae00c5 341
cfbe4dba
JB
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);
5eae00c5 345
ed17f7e5
JS
346 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
347 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
5eae00c5
GR
348 wr32(hw, I40E_VFINT_DYN_CTL01, val);
349
5eae00c5
GR
350 /* schedule work on the private workqueue */
351 schedule_work(&adapter->adminq_task);
352
353 return IRQ_HANDLED;
354}
355
356/**
357 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
358 * @irq: interrupt number
359 * @data: pointer to a q_vector
360 **/
361static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
362{
363 struct i40e_q_vector *q_vector = data;
364
365 if (!q_vector->tx.ring && !q_vector->rx.ring)
366 return IRQ_HANDLED;
367
5d3465a1 368 napi_schedule_irqoff(&q_vector->napi);
5eae00c5
GR
369
370 return IRQ_HANDLED;
371}
372
373/**
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
378 **/
379static void
380i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
381{
7d96ba1a 382 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
0dd438d8 383 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
f19a973f 384 struct i40e_hw *hw = &adapter->hw;
5eae00c5
GR
385
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;
65e87c03 392 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
f19a973f 393 q_vector->ring_mask |= BIT(r_idx);
ee2319cf 394 q_vector->itr_countdown = ITR_COUNTDOWN_START;
f19a973f 395 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
5eae00c5
GR
396}
397
398/**
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
403 **/
404static void
405i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
406{
7d96ba1a 407 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
0dd438d8 408 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
f19a973f 409 struct i40e_hw *hw = &adapter->hw;
5eae00c5
GR
410
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;
65e87c03 417 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
ee2319cf 418 q_vector->itr_countdown = ITR_COUNTDOWN_START;
5eae00c5 419 q_vector->num_ringpairs++;
f19a973f 420 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
5eae00c5
GR
421}
422
423/**
424 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
425 * @adapter: board private structure to initialize
426 *
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.
432 **/
433static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
434{
435 int q_vectors;
436 int v_start = 0;
437 int rxr_idx = 0, txr_idx = 0;
cc052927
MW
438 int rxr_remaining = adapter->num_active_queues;
439 int txr_remaining = adapter->num_active_queues;
5eae00c5
GR
440 int i, j;
441 int rqpv, tqpv;
442 int err = 0;
443
444 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
445
446 /* The ideal configuration...
447 * We have enough vectors to map one per queue.
448 */
973371da 449 if (q_vectors >= (rxr_remaining * 2)) {
5eae00c5
GR
450 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
451 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
452
453 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
454 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
455 goto out;
456 }
457
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.
462 */
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);
467 rxr_idx++;
468 rxr_remaining--;
469 }
470 }
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);
475 txr_idx++;
476 txr_remaining--;
477 }
478 }
479
480out:
481 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
482
483 return err;
484}
485
7709b4c1
AD
486#ifdef CONFIG_NET_POLL_CONTROLLER
487/**
488 * i40evf_netpoll - A Polling 'interrupt' handler
489 * @netdev: network interface device structure
490 *
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.
493 **/
494static void i40evf_netpoll(struct net_device *netdev)
495{
496 struct i40evf_adapter *adapter = netdev_priv(netdev);
497 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
498 int i;
499
500 /* if interface is down do nothing */
0da36b97 501 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
7709b4c1
AD
502 return;
503
504 for (i = 0; i < q_vectors; i++)
7d96ba1a 505 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
7709b4c1
AD
506}
507
508#endif
96db776a
AB
509/**
510 * i40evf_irq_affinity_notify - Callback for affinity changes
511 * @notify: context as to what irq was changed
512 * @mask: the new affinity mask
513 *
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.
516 **/
517static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
518 const cpumask_t *mask)
519{
520 struct i40e_q_vector *q_vector =
521 container_of(notify, struct i40e_q_vector, affinity_notify);
522
7e4d01e7 523 cpumask_copy(&q_vector->affinity_mask, mask);
96db776a
AB
524}
525
526/**
527 * i40evf_irq_affinity_release - Callback for affinity notifier release
528 * @ref: internal core kernel usage
529 *
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.
533 **/
534static void i40evf_irq_affinity_release(struct kref *ref) {}
535
5eae00c5
GR
536/**
537 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
538 * @adapter: board private structure
539 *
540 * Allocates MSI-X vectors for tx and rx handling, and requests
541 * interrupts from the kernel.
542 **/
543static int
544i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
545{
696ac80a
JK
546 unsigned int vector, q_vectors;
547 unsigned int rx_int_idx = 0, tx_int_idx = 0;
548 int irq_num, err;
5eae00c5
GR
549
550 i40evf_irq_disable(adapter);
551 /* Decrement for Other and TCP Timer vectors */
552 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
553
554 for (vector = 0; vector < q_vectors; vector++) {
7d96ba1a 555 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
96db776a 556 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
5eae00c5
GR
557
558 if (q_vector->tx.ring && q_vector->rx.ring) {
696ac80a
JK
559 snprintf(q_vector->name, sizeof(q_vector->name),
560 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
5eae00c5
GR
561 tx_int_idx++;
562 } else if (q_vector->rx.ring) {
696ac80a
JK
563 snprintf(q_vector->name, sizeof(q_vector->name),
564 "i40evf-%s-rx-%d", basename, rx_int_idx++);
5eae00c5 565 } else if (q_vector->tx.ring) {
696ac80a
JK
566 snprintf(q_vector->name, sizeof(q_vector->name),
567 "i40evf-%s-tx-%d", basename, tx_int_idx++);
5eae00c5
GR
568 } else {
569 /* skip this unused q_vector */
570 continue;
571 }
96db776a
AB
572 err = request_irq(irq_num,
573 i40evf_msix_clean_rings,
574 0,
575 q_vector->name,
576 q_vector);
5eae00c5
GR
577 if (err) {
578 dev_info(&adapter->pdev->dev,
fb43201f 579 "Request_irq failed, error: %d\n", err);
5eae00c5
GR
580 goto free_queue_irqs;
581 }
96db776a
AB
582 /* register for affinity change notifications */
583 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
584 q_vector->affinity_notify.release =
585 i40evf_irq_affinity_release;
586 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
5eae00c5 587 /* assign the mask for this irq */
96db776a 588 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
5eae00c5
GR
589 }
590
591 return 0;
592
593free_queue_irqs:
594 while (vector) {
595 vector--;
96db776a
AB
596 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
597 irq_set_affinity_notifier(irq_num, NULL);
598 irq_set_affinity_hint(irq_num, NULL);
599 free_irq(irq_num, &adapter->q_vectors[vector]);
5eae00c5
GR
600 }
601 return err;
602}
603
604/**
605 * i40evf_request_misc_irq - Initialize MSI-X interrupts
606 * @adapter: board private structure
607 *
608 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
609 * vector is only for the admin queue, and stays active even when the netdev
610 * is closed.
611 **/
612static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
613{
614 struct net_device *netdev = adapter->netdev;
615 int err;
616
b39c1e2c 617 snprintf(adapter->misc_vector_name,
9a21a007
CW
618 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
619 dev_name(&adapter->pdev->dev));
5eae00c5 620 err = request_irq(adapter->msix_entries[0].vector,
e1dfee8e
MW
621 &i40evf_msix_aq, 0,
622 adapter->misc_vector_name, netdev);
5eae00c5
GR
623 if (err) {
624 dev_err(&adapter->pdev->dev,
77fa28be
CS
625 "request_irq for %s failed: %d\n",
626 adapter->misc_vector_name, err);
5eae00c5
GR
627 free_irq(adapter->msix_entries[0].vector, netdev);
628 }
629 return err;
630}
631
632/**
633 * i40evf_free_traffic_irqs - Free MSI-X interrupts
634 * @adapter: board private structure
635 *
636 * Frees all MSI-X vectors other than 0.
637 **/
638static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
639{
96db776a 640 int vector, irq_num, q_vectors;
75a64435 641
47d2a5d8
AB
642 if (!adapter->msix_entries)
643 return;
644
5eae00c5
GR
645 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
646
96db776a
AB
647 for (vector = 0; vector < q_vectors; vector++) {
648 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
649 irq_set_affinity_notifier(irq_num, NULL);
650 irq_set_affinity_hint(irq_num, NULL);
651 free_irq(irq_num, &adapter->q_vectors[vector]);
5eae00c5
GR
652 }
653}
654
655/**
656 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
657 * @adapter: board private structure
658 *
659 * Frees MSI-X vector 0.
660 **/
661static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
662{
663 struct net_device *netdev = adapter->netdev;
664
ef4603e8
JK
665 if (!adapter->msix_entries)
666 return;
667
5eae00c5
GR
668 free_irq(adapter->msix_entries[0].vector, netdev);
669}
670
671/**
672 * i40evf_configure_tx - Configure Transmit Unit after Reset
673 * @adapter: board private structure
674 *
675 * Configure the Tx unit of the MAC after a reset.
676 **/
677static void i40evf_configure_tx(struct i40evf_adapter *adapter)
678{
679 struct i40e_hw *hw = &adapter->hw;
680 int i;
75a64435 681
cc052927 682 for (i = 0; i < adapter->num_active_queues; i++)
0dd438d8 683 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
5eae00c5
GR
684}
685
686/**
687 * i40evf_configure_rx - Configure Receive Unit after Reset
688 * @adapter: board private structure
689 *
690 * Configure the Rx unit of the MAC after a reset.
691 **/
692static void i40evf_configure_rx(struct i40evf_adapter *adapter)
693{
dab86afd 694 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
5eae00c5 695 struct i40e_hw *hw = &adapter->hw;
5eae00c5 696 int i;
5eae00c5 697
dab86afd
AD
698 /* Legacy Rx will always default to a 2048 buffer size. */
699#if (PAGE_SIZE < 8192)
700 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
3dfc3eb5
AB
701 struct net_device *netdev = adapter->netdev;
702
98efd694
AD
703 /* For jumbo frames on systems with 4K pages we have to use
704 * an order 1 page, so we might as well increase the size
705 * of our Rx buffer to make better use of the available space
706 */
707 rx_buf_len = I40E_RXBUFFER_3072;
708
dab86afd
AD
709 /* We use a 1536 buffer size for configurations with
710 * standard Ethernet mtu. On x86 this gives us enough room
711 * for shared info and 192 bytes of padding.
712 */
ca9ec088
AD
713 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
714 (netdev->mtu <= ETH_DATA_LEN))
dab86afd
AD
715 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
716 }
717#endif
718
cc052927 719 for (i = 0; i < adapter->num_active_queues; i++) {
0dd438d8 720 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
dab86afd 721 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
ca9ec088
AD
722
723 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
724 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
725 else
726 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
5eae00c5
GR
727 }
728}
729
730/**
731 * i40evf_find_vlan - Search filter list for specific vlan filter
732 * @adapter: board private structure
733 * @vlan: vlan tag
734 *
735 * Returns ptr to the filter object or NULL
736 **/
737static struct
738i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
739{
740 struct i40evf_vlan_filter *f;
741
742 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
743 if (vlan == f->vlan)
744 return f;
745 }
746 return NULL;
747}
748
749/**
750 * i40evf_add_vlan - Add a vlan filter to the list
751 * @adapter: board private structure
752 * @vlan: VLAN tag
753 *
754 * Returns ptr to the filter object or NULL when no memory available.
755 **/
756static struct
757i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
758{
13acb546
MW
759 struct i40evf_vlan_filter *f = NULL;
760 int count = 50;
761
762 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
763 &adapter->crit_section)) {
764 udelay(1);
765 if (--count == 0)
766 goto out;
767 }
5eae00c5
GR
768
769 f = i40evf_find_vlan(adapter, vlan);
348d4994 770 if (!f) {
5eae00c5 771 f = kzalloc(sizeof(*f), GFP_ATOMIC);
348d4994 772 if (!f)
13acb546 773 goto clearout;
249c8b8d 774
5eae00c5
GR
775 f->vlan = vlan;
776
777 INIT_LIST_HEAD(&f->list);
778 list_add(&f->list, &adapter->vlan_filter_list);
779 f->add = true;
780 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
781 }
782
13acb546
MW
783clearout:
784 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
785out:
5eae00c5
GR
786 return f;
787}
788
789/**
790 * i40evf_del_vlan - Remove a vlan filter from the list
791 * @adapter: board private structure
792 * @vlan: VLAN tag
793 **/
794static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
795{
796 struct i40evf_vlan_filter *f;
13acb546
MW
797 int count = 50;
798
799 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
800 &adapter->crit_section)) {
801 udelay(1);
802 if (--count == 0)
803 return;
804 }
5eae00c5
GR
805
806 f = i40evf_find_vlan(adapter, vlan);
807 if (f) {
808 f->remove = true;
809 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
810 }
13acb546 811 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
5eae00c5
GR
812}
813
814/**
815 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
816 * @netdev: network device struct
817 * @vid: VLAN tag
818 **/
819static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
75a64435 820 __always_unused __be16 proto, u16 vid)
5eae00c5
GR
821{
822 struct i40evf_adapter *adapter = netdev_priv(netdev);
823
8ed995ff
MW
824 if (!VLAN_ALLOWED(adapter))
825 return -EIO;
5eae00c5
GR
826 if (i40evf_add_vlan(adapter, vid) == NULL)
827 return -ENOMEM;
828 return 0;
829}
830
831/**
832 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
833 * @netdev: network device struct
834 * @vid: VLAN tag
835 **/
836static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
75a64435 837 __always_unused __be16 proto, u16 vid)
5eae00c5
GR
838{
839 struct i40evf_adapter *adapter = netdev_priv(netdev);
840
8ed995ff
MW
841 if (VLAN_ALLOWED(adapter)) {
842 i40evf_del_vlan(adapter, vid);
843 return 0;
844 }
845 return -EIO;
5eae00c5
GR
846}
847
848/**
849 * i40evf_find_filter - Search filter list for specific mac filter
850 * @adapter: board private structure
851 * @macaddr: the MAC address
852 *
853 * Returns ptr to the filter object or NULL
854 **/
855static struct
856i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
857 u8 *macaddr)
858{
859 struct i40evf_mac_filter *f;
860
861 if (!macaddr)
862 return NULL;
863
864 list_for_each_entry(f, &adapter->mac_filter_list, list) {
865 if (ether_addr_equal(macaddr, f->macaddr))
866 return f;
867 }
868 return NULL;
869}
870
871/**
872 * i40e_add_filter - Add a mac filter to the filter list
873 * @adapter: board private structure
874 * @macaddr: the MAC address
875 *
876 * Returns ptr to the filter object or NULL when no memory available.
877 **/
878static struct
879i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
880 u8 *macaddr)
881{
882 struct i40evf_mac_filter *f;
54e16f64 883 int count = 50;
5eae00c5
GR
884
885 if (!macaddr)
886 return NULL;
887
888 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
54e16f64 889 &adapter->crit_section)) {
b65476cd 890 udelay(1);
54e16f64
MW
891 if (--count == 0)
892 return NULL;
893 }
5eae00c5
GR
894
895 f = i40evf_find_filter(adapter, macaddr);
348d4994 896 if (!f) {
5eae00c5 897 f = kzalloc(sizeof(*f), GFP_ATOMIC);
348d4994 898 if (!f) {
5eae00c5
GR
899 clear_bit(__I40EVF_IN_CRITICAL_TASK,
900 &adapter->crit_section);
901 return NULL;
902 }
903
9a173901 904 ether_addr_copy(f->macaddr, macaddr);
5eae00c5 905
63590b61 906 list_add_tail(&f->list, &adapter->mac_filter_list);
5eae00c5
GR
907 f->add = true;
908 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
909 }
910
911 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
912 return f;
913}
914
915/**
916 * i40evf_set_mac - NDO callback to set port mac address
917 * @netdev: network interface device structure
918 * @p: pointer to an address structure
919 *
920 * Returns 0 on success, negative on failure
921 **/
922static int i40evf_set_mac(struct net_device *netdev, void *p)
923{
924 struct i40evf_adapter *adapter = netdev_priv(netdev);
925 struct i40e_hw *hw = &adapter->hw;
926 struct i40evf_mac_filter *f;
927 struct sockaddr *addr = p;
928
929 if (!is_valid_ether_addr(addr->sa_data))
930 return -EADDRNOTAVAIL;
931
932 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
933 return 0;
934
14e52ee2
MW
935 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
936 return -EPERM;
937
938 f = i40evf_find_filter(adapter, hw->mac.addr);
939 if (f) {
940 f->remove = true;
941 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
942 }
943
5eae00c5
GR
944 f = i40evf_add_filter(adapter, addr->sa_data);
945 if (f) {
9a173901
GR
946 ether_addr_copy(hw->mac.addr, addr->sa_data);
947 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
5eae00c5
GR
948 }
949
950 return (f == NULL) ? -ENOMEM : 0;
951}
952
953/**
954 * i40evf_set_rx_mode - NDO callback to set the netdev filters
955 * @netdev: network interface device structure
956 **/
957static void i40evf_set_rx_mode(struct net_device *netdev)
958{
959 struct i40evf_adapter *adapter = netdev_priv(netdev);
960 struct i40evf_mac_filter *f, *ftmp;
961 struct netdev_hw_addr *uca;
962 struct netdev_hw_addr *mca;
2f41f335 963 struct netdev_hw_addr *ha;
54e16f64 964 int count = 50;
5eae00c5
GR
965
966 /* add addr if not already in the filter list */
967 netdev_for_each_uc_addr(uca, netdev) {
968 i40evf_add_filter(adapter, uca->addr);
969 }
970 netdev_for_each_mc_addr(mca, netdev) {
971 i40evf_add_filter(adapter, mca->addr);
972 }
973
974 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
54e16f64 975 &adapter->crit_section)) {
b65476cd 976 udelay(1);
54e16f64
MW
977 if (--count == 0) {
978 dev_err(&adapter->pdev->dev,
979 "Failed to get lock in %s\n", __func__);
980 return;
981 }
982 }
5eae00c5
GR
983 /* remove filter if not in netdev list */
984 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2f41f335
SN
985 netdev_for_each_mc_addr(mca, netdev)
986 if (ether_addr_equal(mca->addr, f->macaddr))
987 goto bottom_of_search_loop;
988
989 netdev_for_each_uc_addr(uca, netdev)
990 if (ether_addr_equal(uca->addr, f->macaddr))
991 goto bottom_of_search_loop;
992
993 for_each_dev_addr(netdev, ha)
994 if (ether_addr_equal(ha->addr, f->macaddr))
995 goto bottom_of_search_loop;
996
997 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
998 goto bottom_of_search_loop;
999
1000 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1001 f->remove = true;
1002 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1003
1004bottom_of_search_loop:
1005 continue;
5eae00c5 1006 }
47d34839
ASJ
1007
1008 if (netdev->flags & IFF_PROMISC &&
1009 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
1010 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
1011 else if (!(netdev->flags & IFF_PROMISC) &&
1012 adapter->flags & I40EVF_FLAG_PROMISC_ON)
1013 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
1014
f42a5c74
ASJ
1015 if (netdev->flags & IFF_ALLMULTI &&
1016 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
1017 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
1018 else if (!(netdev->flags & IFF_ALLMULTI) &&
1019 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
1020 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
1021
5eae00c5
GR
1022 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1023}
1024
1025/**
1026 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1027 * @adapter: board private structure
1028 **/
1029static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1030{
1031 int q_idx;
1032 struct i40e_q_vector *q_vector;
1033 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1034
1035 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1036 struct napi_struct *napi;
75a64435 1037
7d96ba1a 1038 q_vector = &adapter->q_vectors[q_idx];
5eae00c5
GR
1039 napi = &q_vector->napi;
1040 napi_enable(napi);
1041 }
1042}
1043
1044/**
1045 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1046 * @adapter: board private structure
1047 **/
1048static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1049{
1050 int q_idx;
1051 struct i40e_q_vector *q_vector;
1052 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1053
1054 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
7d96ba1a 1055 q_vector = &adapter->q_vectors[q_idx];
5eae00c5
GR
1056 napi_disable(&q_vector->napi);
1057 }
1058}
1059
1060/**
1061 * i40evf_configure - set up transmit and receive data structures
1062 * @adapter: board private structure
1063 **/
1064static void i40evf_configure(struct i40evf_adapter *adapter)
1065{
1066 struct net_device *netdev = adapter->netdev;
1067 int i;
1068
1069 i40evf_set_rx_mode(netdev);
1070
1071 i40evf_configure_tx(adapter);
1072 i40evf_configure_rx(adapter);
1073 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1074
cc052927 1075 for (i = 0; i < adapter->num_active_queues; i++) {
0dd438d8 1076 struct i40e_ring *ring = &adapter->rx_rings[i];
75a64435 1077
b163098e 1078 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
5eae00c5
GR
1079 }
1080}
1081
1082/**
1083 * i40evf_up_complete - Finish the last steps of bringing up a connection
1084 * @adapter: board private structure
1085 **/
cb130a0b 1086static void i40evf_up_complete(struct i40evf_adapter *adapter)
5eae00c5
GR
1087{
1088 adapter->state = __I40EVF_RUNNING;
0da36b97 1089 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
5eae00c5
GR
1090
1091 i40evf_napi_enable_all(adapter);
1092
1093 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
ed0e894d
MW
1094 if (CLIENT_ENABLED(adapter))
1095 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
5eae00c5 1096 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
5eae00c5
GR
1097}
1098
5eae00c5
GR
1099/**
1100 * i40e_down - Shutdown the connection processing
1101 * @adapter: board private structure
1102 **/
1103void i40evf_down(struct i40evf_adapter *adapter)
1104{
1105 struct net_device *netdev = adapter->netdev;
1106 struct i40evf_mac_filter *f;
1107
209dc4da 1108 if (adapter->state <= __I40EVF_DOWN_PENDING)
ddf0b3a6
MW
1109 return;
1110
53d0b3ae
MW
1111 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1112 &adapter->crit_section))
1113 usleep_range(500, 1000);
1114
63e18c25
MW
1115 netif_carrier_off(netdev);
1116 netif_tx_disable(netdev);
3f341acc 1117 adapter->link_up = false;
748c434b 1118 i40evf_napi_disable_all(adapter);
63e18c25 1119 i40evf_irq_disable(adapter);
53d0b3ae 1120
ef8693eb 1121 /* remove all MAC filters */
5eae00c5
GR
1122 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1123 f->remove = true;
1124 }
ed1f5b58
MW
1125 /* remove all VLAN filters */
1126 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1127 f->remove = true;
1128 }
ef8693eb
MW
1129 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1130 adapter->state != __I40EVF_RESETTING) {
53d0b3ae 1131 /* cancel any current operation */
310a2ad9 1132 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
53d0b3ae
MW
1133 /* Schedule operations to close down the HW. Don't wait
1134 * here for this to complete. The watchdog is still running
1135 * and it will take care of this.
1136 */
1137 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
ed1f5b58 1138 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
ef8693eb 1139 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
ef8693eb 1140 }
5eae00c5 1141
53d0b3ae 1142 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
fe2647ab 1143 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
5eae00c5
GR
1144}
1145
1146/**
1147 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1148 * @adapter: board private structure
1149 * @vectors: number of vectors to request
1150 *
1151 * Work with the OS to set up the MSIX vectors needed.
1152 *
1153 * Returns 0 on success, negative on failure
1154 **/
1155static int
1156i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1157{
1158 int err, vector_threshold;
1159
1160 /* We'll want at least 3 (vector_threshold):
1161 * 0) Other (Admin Queue and link, mostly)
1162 * 1) TxQ[0] Cleanup
1163 * 2) RxQ[0] Cleanup
1164 */
1165 vector_threshold = MIN_MSIX_COUNT;
1166
1167 /* The more we get, the more we will assign to Tx/Rx Cleanup
1168 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1169 * Right now, we simply care about how many we'll get; we'll
1170 * set them up later while requesting irq's.
1171 */
fc2f2f5d
AG
1172 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1173 vector_threshold, vectors);
1174 if (err < 0) {
80e72893 1175 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
5eae00c5
GR
1176 kfree(adapter->msix_entries);
1177 adapter->msix_entries = NULL;
fc2f2f5d 1178 return err;
5eae00c5 1179 }
fc2f2f5d
AG
1180
1181 /* Adjust for only the vectors we'll use, which is minimum
1182 * of max_msix_q_vectors + NONQ_VECS, or the number of
1183 * vectors we were allocated.
1184 */
1185 adapter->num_msix_vectors = err;
1186 return 0;
5eae00c5
GR
1187}
1188
1189/**
1190 * i40evf_free_queues - Free memory for all rings
1191 * @adapter: board private structure to initialize
1192 *
1193 * Free all of the memory associated with queue pairs.
1194 **/
1195static void i40evf_free_queues(struct i40evf_adapter *adapter)
1196{
5eae00c5
GR
1197 if (!adapter->vsi_res)
1198 return;
65c7006f 1199 adapter->num_active_queues = 0;
0dd438d8 1200 kfree(adapter->tx_rings);
10311540 1201 adapter->tx_rings = NULL;
0dd438d8 1202 kfree(adapter->rx_rings);
10311540 1203 adapter->rx_rings = NULL;
5eae00c5
GR
1204}
1205
1206/**
1207 * i40evf_alloc_queues - Allocate memory for all rings
1208 * @adapter: board private structure to initialize
1209 *
1210 * We allocate one ring per queue at run-time since we don't know the
1211 * number of queues at compile-time. The polling_netdev array is
1212 * intended for Multiqueue, but should work fine with a single queue.
1213 **/
1214static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1215{
65c7006f
JK
1216 int i, num_active_queues;
1217
1218 num_active_queues = min_t(int,
1219 adapter->vsi_res->num_queue_pairs,
1220 (int)(num_online_cpus()));
5eae00c5 1221
65c7006f 1222 adapter->tx_rings = kcalloc(num_active_queues,
0dd438d8
MW
1223 sizeof(struct i40e_ring), GFP_KERNEL);
1224 if (!adapter->tx_rings)
1225 goto err_out;
65c7006f 1226 adapter->rx_rings = kcalloc(num_active_queues,
0dd438d8
MW
1227 sizeof(struct i40e_ring), GFP_KERNEL);
1228 if (!adapter->rx_rings)
1229 goto err_out;
1230
65c7006f 1231 for (i = 0; i < num_active_queues; i++) {
5eae00c5
GR
1232 struct i40e_ring *tx_ring;
1233 struct i40e_ring *rx_ring;
1234
0dd438d8 1235 tx_ring = &adapter->tx_rings[i];
5eae00c5
GR
1236
1237 tx_ring->queue_index = i;
1238 tx_ring->netdev = adapter->netdev;
1239 tx_ring->dev = &adapter->pdev->dev;
d732a184 1240 tx_ring->count = adapter->tx_desc_count;
65e87c03 1241 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
d36e41dc 1242 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
1f012279 1243 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
5eae00c5 1244
0dd438d8 1245 rx_ring = &adapter->rx_rings[i];
5eae00c5
GR
1246 rx_ring->queue_index = i;
1247 rx_ring->netdev = adapter->netdev;
1248 rx_ring->dev = &adapter->pdev->dev;
d732a184 1249 rx_ring->count = adapter->rx_desc_count;
65e87c03 1250 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
5eae00c5
GR
1251 }
1252
65c7006f
JK
1253 adapter->num_active_queues = num_active_queues;
1254
5eae00c5
GR
1255 return 0;
1256
1257err_out:
1258 i40evf_free_queues(adapter);
1259 return -ENOMEM;
1260}
1261
1262/**
1263 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1264 * @adapter: board private structure to initialize
1265 *
1266 * Attempt to configure the interrupts using the best available
1267 * capabilities of the hardware and the kernel.
1268 **/
1269static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1270{
1271 int vector, v_budget;
1272 int pairs = 0;
1273 int err = 0;
1274
1275 if (!adapter->vsi_res) {
1276 err = -EIO;
1277 goto out;
1278 }
cc052927 1279 pairs = adapter->num_active_queues;
5eae00c5 1280
789f38ca
JK
1281 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1282 * us much good if we have more vectors than CPUs. However, we already
1283 * limit the total number of queues by the number of CPUs so we do not
1284 * need any further limiting here.
5eae00c5 1285 */
789f38ca
JK
1286 v_budget = min_t(int, pairs + NONQ_VECS,
1287 (int)adapter->vf_res->max_vectors);
5eae00c5 1288
5eae00c5
GR
1289 adapter->msix_entries = kcalloc(v_budget,
1290 sizeof(struct msix_entry), GFP_KERNEL);
1291 if (!adapter->msix_entries) {
1292 err = -ENOMEM;
1293 goto out;
1294 }
1295
1296 for (vector = 0; vector < v_budget; vector++)
1297 adapter->msix_entries[vector].entry = vector;
1298
313ed2d5 1299 err = i40evf_acquire_msix_vectors(adapter, v_budget);
5eae00c5
GR
1300
1301out:
e6c4cf6f
MW
1302 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1303 netif_set_real_num_tx_queues(adapter->netdev, pairs);
5eae00c5
GR
1304 return err;
1305}
1306
e25d00b8 1307/**
43a3d9ba
MW
1308 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1309 * @adapter: board private structure
2c86ac3c
HZ
1310 *
1311 * Return 0 on success, negative on failure
e25d00b8 1312 **/
43a3d9ba 1313static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
e25d00b8 1314{
43a3d9ba
MW
1315 struct i40e_aqc_get_set_rss_key_data *rss_key =
1316 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
e25d00b8 1317 struct i40e_hw *hw = &adapter->hw;
2c86ac3c 1318 int ret = 0;
e25d00b8 1319
310a2ad9 1320 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
e25d00b8 1321 /* bail because we already have a command pending */
e3d132d1 1322 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
e25d00b8 1323 adapter->current_op);
2c86ac3c 1324 return -EBUSY;
e25d00b8
ASJ
1325 }
1326
43a3d9ba
MW
1327 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1328 if (ret) {
1329 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1330 i40evf_stat_str(hw, ret),
1331 i40evf_aq_str(hw, hw->aq.asq_last_status));
1332 return ret;
1333
2c86ac3c 1334 }
e25d00b8 1335
43a3d9ba
MW
1336 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1337 adapter->rss_lut, adapter->rss_lut_size);
1338 if (ret) {
1339 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1340 i40evf_stat_str(hw, ret),
1341 i40evf_aq_str(hw, hw->aq.asq_last_status));
e25d00b8
ASJ
1342 }
1343
2c86ac3c 1344 return ret;
43a3d9ba 1345
e25d00b8
ASJ
1346}
1347
1348/**
2c86ac3c 1349 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
43a3d9ba 1350 * @adapter: board private structure
2c86ac3c
HZ
1351 *
1352 * Returns 0 on success, negative on failure
e25d00b8 1353 **/
43a3d9ba 1354static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
e25d00b8
ASJ
1355{
1356 struct i40e_hw *hw = &adapter->hw;
43a3d9ba 1357 u32 *dw;
2c86ac3c 1358 u16 i;
e25d00b8 1359
43a3d9ba
MW
1360 dw = (u32 *)adapter->rss_key;
1361 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1362 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
2c86ac3c 1363
43a3d9ba
MW
1364 dw = (u32 *)adapter->rss_lut;
1365 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1366 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
2c86ac3c 1367
e25d00b8 1368 i40e_flush(hw);
2c86ac3c
HZ
1369
1370 return 0;
1371}
1372
1373/**
1374 * i40evf_config_rss - Configure RSS keys and lut
43a3d9ba 1375 * @adapter: board private structure
90b02b43
HZ
1376 *
1377 * Returns 0 on success, negative on failure
1378 **/
43a3d9ba 1379int i40evf_config_rss(struct i40evf_adapter *adapter)
90b02b43 1380{
90b02b43 1381
43a3d9ba
MW
1382 if (RSS_PF(adapter)) {
1383 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1384 I40EVF_FLAG_AQ_SET_RSS_KEY;
1385 return 0;
1386 } else if (RSS_AQ(adapter)) {
1387 return i40evf_config_rss_aq(adapter);
1388 } else {
1389 return i40evf_config_rss_reg(adapter);
1390 }
90b02b43
HZ
1391}
1392
2c86ac3c
HZ
1393/**
1394 * i40evf_fill_rss_lut - Fill the lut with default values
43a3d9ba 1395 * @adapter: board private structure
2c86ac3c 1396 **/
43a3d9ba 1397static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
2c86ac3c
HZ
1398{
1399 u16 i;
1400
43a3d9ba
MW
1401 for (i = 0; i < adapter->rss_lut_size; i++)
1402 adapter->rss_lut[i] = i % adapter->num_active_queues;
e25d00b8
ASJ
1403}
1404
1405/**
96a81986 1406 * i40evf_init_rss - Prepare for RSS
e25d00b8 1407 * @adapter: board private structure
2c86ac3c
HZ
1408 *
1409 * Return 0 on success, negative on failure
e25d00b8 1410 **/
2c86ac3c 1411static int i40evf_init_rss(struct i40evf_adapter *adapter)
e25d00b8
ASJ
1412{
1413 struct i40e_hw *hw = &adapter->hw;
2c86ac3c 1414 int ret;
e25d00b8 1415
43a3d9ba
MW
1416 if (!RSS_PF(adapter)) {
1417 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
fbb113f7 1418 if (adapter->vf_res->vf_cap_flags &
310a2ad9 1419 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
43a3d9ba
MW
1420 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1421 else
1422 adapter->hena = I40E_DEFAULT_RSS_HENA;
e25d00b8 1423
43a3d9ba
MW
1424 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1425 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1426 }
66f9af85 1427
43a3d9ba 1428 i40evf_fill_rss_lut(adapter);
66f9af85 1429
43a3d9ba
MW
1430 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1431 ret = i40evf_config_rss(adapter);
2c86ac3c
HZ
1432
1433 return ret;
e25d00b8
ASJ
1434}
1435
5eae00c5
GR
1436/**
1437 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1438 * @adapter: board private structure to initialize
1439 *
1440 * We allocate one q_vector per queue interrupt. If allocation fails we
1441 * return -ENOMEM.
1442 **/
1443static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1444{
7d96ba1a 1445 int q_idx = 0, num_q_vectors;
5eae00c5
GR
1446 struct i40e_q_vector *q_vector;
1447
1448 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
0dd438d8 1449 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
7d96ba1a
MW
1450 GFP_KERNEL);
1451 if (!adapter->q_vectors)
311f23e9 1452 return -ENOMEM;
5eae00c5
GR
1453
1454 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
7d96ba1a 1455 q_vector = &adapter->q_vectors[q_idx];
5eae00c5
GR
1456 q_vector->adapter = adapter;
1457 q_vector->vsi = &adapter->vsi;
1458 q_vector->v_idx = q_idx;
1459 netif_napi_add(adapter->netdev, &q_vector->napi,
75a64435 1460 i40evf_napi_poll, NAPI_POLL_WEIGHT);
5eae00c5
GR
1461 }
1462
1463 return 0;
5eae00c5
GR
1464}
1465
1466/**
1467 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1468 * @adapter: board private structure to initialize
1469 *
1470 * This function frees the memory allocated to the q_vectors. In addition if
1471 * NAPI is enabled it will delete any references to the NAPI struct prior
1472 * to freeing the q_vector.
1473 **/
1474static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1475{
1476 int q_idx, num_q_vectors;
1477 int napi_vectors;
1478
ef4603e8
JK
1479 if (!adapter->q_vectors)
1480 return;
1481
5eae00c5 1482 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
cc052927 1483 napi_vectors = adapter->num_active_queues;
5eae00c5
GR
1484
1485 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
7d96ba1a 1486 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
5eae00c5
GR
1487 if (q_idx < napi_vectors)
1488 netif_napi_del(&q_vector->napi);
5eae00c5 1489 }
7d96ba1a 1490 kfree(adapter->q_vectors);
ef4603e8 1491 adapter->q_vectors = NULL;
5eae00c5
GR
1492}
1493
1494/**
1495 * i40evf_reset_interrupt_capability - Reset MSIX setup
1496 * @adapter: board private structure
1497 *
1498 **/
1499void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1500{
47d2a5d8
AB
1501 if (!adapter->msix_entries)
1502 return;
1503
5eae00c5
GR
1504 pci_disable_msix(adapter->pdev);
1505 kfree(adapter->msix_entries);
1506 adapter->msix_entries = NULL;
5eae00c5
GR
1507}
1508
1509/**
1510 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1511 * @adapter: board private structure to initialize
1512 *
1513 **/
1514int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1515{
1516 int err;
1517
283aeafe
JK
1518 err = i40evf_alloc_queues(adapter);
1519 if (err) {
1520 dev_err(&adapter->pdev->dev,
1521 "Unable to allocate memory for queues\n");
1522 goto err_alloc_queues;
1523 }
1524
62fe2a86 1525 rtnl_lock();
5eae00c5 1526 err = i40evf_set_interrupt_capability(adapter);
62fe2a86 1527 rtnl_unlock();
5eae00c5
GR
1528 if (err) {
1529 dev_err(&adapter->pdev->dev,
1530 "Unable to setup interrupt capabilities\n");
1531 goto err_set_interrupt;
1532 }
1533
1534 err = i40evf_alloc_q_vectors(adapter);
1535 if (err) {
1536 dev_err(&adapter->pdev->dev,
1537 "Unable to allocate memory for queue vectors\n");
1538 goto err_alloc_q_vectors;
1539 }
1540
5eae00c5 1541 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
75a64435
MW
1542 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1543 adapter->num_active_queues);
5eae00c5
GR
1544
1545 return 0;
5eae00c5
GR
1546err_alloc_q_vectors:
1547 i40evf_reset_interrupt_capability(adapter);
1548err_set_interrupt:
283aeafe
JK
1549 i40evf_free_queues(adapter);
1550err_alloc_queues:
5eae00c5
GR
1551 return err;
1552}
1553
66f9af85 1554/**
43a3d9ba
MW
1555 * i40evf_free_rss - Free memory used by RSS structs
1556 * @adapter: board private structure
66f9af85 1557 **/
43a3d9ba 1558static void i40evf_free_rss(struct i40evf_adapter *adapter)
66f9af85 1559{
43a3d9ba
MW
1560 kfree(adapter->rss_key);
1561 adapter->rss_key = NULL;
66f9af85 1562
43a3d9ba
MW
1563 kfree(adapter->rss_lut);
1564 adapter->rss_lut = NULL;
66f9af85
HZ
1565}
1566
5eae00c5
GR
1567/**
1568 * i40evf_watchdog_timer - Periodic call-back timer
1569 * @data: pointer to adapter disguised as unsigned long
1570 **/
1571static void i40evf_watchdog_timer(unsigned long data)
1572{
1573 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
75a64435 1574
5eae00c5
GR
1575 schedule_work(&adapter->watchdog_task);
1576 /* timer will be rescheduled in watchdog task */
1577}
1578
1579/**
1580 * i40evf_watchdog_task - Periodic call-back task
1581 * @work: pointer to work_struct
1582 **/
1583static void i40evf_watchdog_task(struct work_struct *work)
1584{
1585 struct i40evf_adapter *adapter = container_of(work,
75a64435
MW
1586 struct i40evf_adapter,
1587 watchdog_task);
5eae00c5 1588 struct i40e_hw *hw = &adapter->hw;
ee5c1e92 1589 u32 reg_val;
5eae00c5 1590
ef8693eb
MW
1591 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1592 goto restart_watchdog;
1593
1594 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
ee5c1e92
MW
1595 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1596 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
310a2ad9
JB
1597 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1598 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
ef8693eb
MW
1599 /* A chance for redemption! */
1600 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1601 adapter->state = __I40EVF_STARTUP;
1602 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1603 schedule_delayed_work(&adapter->init_task, 10);
1604 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1605 &adapter->crit_section);
1606 /* Don't reschedule the watchdog, since we've restarted
1607 * the init task. When init_task contacts the PF and
1608 * gets everything set up again, it'll restart the
1609 * watchdog for us. Down, boy. Sit. Stay. Woof.
1610 */
1611 return;
1612 }
ef8693eb 1613 adapter->aq_required = 0;
310a2ad9 1614 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
5eae00c5 1615 goto watchdog_done;
ef8693eb 1616 }
5eae00c5 1617
ef8693eb
MW
1618 if ((adapter->state < __I40EVF_DOWN) ||
1619 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
5eae00c5
GR
1620 goto watchdog_done;
1621
ef8693eb 1622 /* check for reset */
ee5c1e92
MW
1623 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1624 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
5eae00c5 1625 adapter->state = __I40EVF_RESETTING;
ef8693eb 1626 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
249c8b8d 1627 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
5eae00c5 1628 schedule_work(&adapter->reset_task);
ef8693eb 1629 adapter->aq_required = 0;
310a2ad9 1630 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
1631 goto watchdog_done;
1632 }
1633
1634 /* Process admin queue tasks. After init, everything gets done
1635 * here so we don't race on the admin queue.
1636 */
ed636960 1637 if (adapter->current_op) {
0758e7cb
MW
1638 if (!i40evf_asq_done(hw)) {
1639 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1640 i40evf_send_api_ver(adapter);
1641 }
5eae00c5 1642 goto watchdog_done;
0758e7cb 1643 }
e6d038de
MW
1644 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1645 i40evf_send_vf_config_msg(adapter);
1646 goto watchdog_done;
1647 }
5eae00c5 1648
e284fc88
MW
1649 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1650 i40evf_disable_queues(adapter);
1651 goto watchdog_done;
1652 }
1653
5eae00c5
GR
1654 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1655 i40evf_map_queues(adapter);
1656 goto watchdog_done;
1657 }
1658
1659 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1660 i40evf_add_ether_addrs(adapter);
1661 goto watchdog_done;
1662 }
1663
1664 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1665 i40evf_add_vlans(adapter);
1666 goto watchdog_done;
1667 }
1668
1669 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1670 i40evf_del_ether_addrs(adapter);
1671 goto watchdog_done;
1672 }
1673
1674 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1675 i40evf_del_vlans(adapter);
1676 goto watchdog_done;
1677 }
1678
5eae00c5
GR
1679 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1680 i40evf_configure_queues(adapter);
1681 goto watchdog_done;
1682 }
1683
1684 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1685 i40evf_enable_queues(adapter);
1686 goto watchdog_done;
1687 }
1688
e25d00b8
ASJ
1689 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1690 /* This message goes straight to the firmware, not the
1691 * PF, so we don't have to set current_op as we will
1692 * not get a response through the ARQ.
1693 */
96a81986 1694 i40evf_init_rss(adapter);
e25d00b8
ASJ
1695 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1696 goto watchdog_done;
1697 }
43a3d9ba
MW
1698 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1699 i40evf_get_hena(adapter);
1700 goto watchdog_done;
1701 }
1702 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1703 i40evf_set_hena(adapter);
1704 goto watchdog_done;
1705 }
1706 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1707 i40evf_set_rss_key(adapter);
1708 goto watchdog_done;
1709 }
1710 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1711 i40evf_set_rss_lut(adapter);
1712 goto watchdog_done;
1713 }
e25d00b8 1714
47d34839 1715 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
ff3f4cc2
JB
1716 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1717 FLAG_VF_MULTICAST_PROMISC);
47d34839
ASJ
1718 goto watchdog_done;
1719 }
1720
f42a5c74 1721 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
ff3f4cc2 1722 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
f42a5c74
ASJ
1723 goto watchdog_done;
1724 }
1725
1726 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1727 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
47d34839
ASJ
1728 i40evf_set_promiscuous(adapter, 0);
1729 goto watchdog_done;
1730 }
ed0e894d 1731 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
47d34839 1732
5eae00c5
GR
1733 if (adapter->state == __I40EVF_RUNNING)
1734 i40evf_request_stats(adapter);
5eae00c5 1735watchdog_done:
4870e176
MW
1736 if (adapter->state == __I40EVF_RUNNING) {
1737 i40evf_irq_enable_queues(adapter, ~0);
1738 i40evf_fire_sw_int(adapter, 0xFF);
1739 } else {
1740 i40evf_fire_sw_int(adapter, 0x1);
1741 }
1742
ef8693eb
MW
1743 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1744restart_watchdog:
d3e2edb7
AS
1745 if (adapter->state == __I40EVF_REMOVE)
1746 return;
5eae00c5
GR
1747 if (adapter->aq_required)
1748 mod_timer(&adapter->watchdog_timer,
1749 jiffies + msecs_to_jiffies(20));
1750 else
1751 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
5eae00c5
GR
1752 schedule_work(&adapter->adminq_task);
1753}
1754
dedecb6d
JP
1755static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1756{
1757 struct i40evf_mac_filter *f, *ftmp;
1758 struct i40evf_vlan_filter *fv, *fvtmp;
1759
1760 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1761
1762 if (netif_running(adapter->netdev)) {
0da36b97 1763 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
dedecb6d
JP
1764 netif_carrier_off(adapter->netdev);
1765 netif_tx_disable(adapter->netdev);
1766 adapter->link_up = false;
1767 i40evf_napi_disable_all(adapter);
1768 i40evf_irq_disable(adapter);
1769 i40evf_free_traffic_irqs(adapter);
1770 i40evf_free_all_tx_resources(adapter);
1771 i40evf_free_all_rx_resources(adapter);
1772 }
1773
1774 /* Delete all of the filters, both MAC and VLAN. */
1775 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1776 list_del(&f->list);
1777 kfree(f);
1778 }
1779
1780 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1781 list_del(&fv->list);
1782 kfree(fv);
1783 }
1784
1785 i40evf_free_misc_irq(adapter);
1786 i40evf_reset_interrupt_capability(adapter);
1787 i40evf_free_queues(adapter);
1788 i40evf_free_q_vectors(adapter);
1789 kfree(adapter->vf_res);
1790 i40evf_shutdown_adminq(&adapter->hw);
1791 adapter->netdev->flags &= ~IFF_UP;
1792 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1793 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1794 adapter->state = __I40EVF_DOWN;
fe2647ab 1795 wake_up(&adapter->down_waitqueue);
dedecb6d
JP
1796 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1797}
1798
67c818a1
MW
1799#define I40EVF_RESET_WAIT_MS 10
1800#define I40EVF_RESET_WAIT_COUNT 500
5eae00c5
GR
1801/**
1802 * i40evf_reset_task - Call-back task to handle hardware reset
1803 * @work: pointer to work_struct
1804 *
1805 * During reset we need to shut down and reinitialize the admin queue
1806 * before we can use it to communicate with the PF again. We also clear
1807 * and reinit the rings because that context is lost as well.
1808 **/
1809static void i40evf_reset_task(struct work_struct *work)
1810{
ef8693eb
MW
1811 struct i40evf_adapter *adapter = container_of(work,
1812 struct i40evf_adapter,
1813 reset_task);
ac833bbf 1814 struct net_device *netdev = adapter->netdev;
5eae00c5 1815 struct i40e_hw *hw = &adapter->hw;
40d01366 1816 struct i40evf_vlan_filter *vlf;
ac833bbf 1817 struct i40evf_mac_filter *f;
ee5c1e92 1818 u32 reg_val;
ac833bbf 1819 int i = 0, err;
5eae00c5 1820
ed0e894d 1821 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
5eae00c5 1822 &adapter->crit_section))
f98a2006 1823 usleep_range(500, 1000);
ed0e894d
MW
1824 if (CLIENT_ENABLED(adapter)) {
1825 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1826 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1827 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1828 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1829 cancel_delayed_work_sync(&adapter->client_task);
1830 i40evf_notify_client_close(&adapter->vsi, true);
1831 }
67c818a1 1832 i40evf_misc_irq_disable(adapter);
3526d800 1833 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
67c818a1
MW
1834 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1835 /* Restart the AQ here. If we have been reset but didn't
1836 * detect it, or if the PF had to reinit, our AQ will be hosed.
1837 */
1838 i40evf_shutdown_adminq(hw);
1839 i40evf_init_adminq(hw);
3526d800
MW
1840 i40evf_request_reset(adapter);
1841 }
67c818a1 1842 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
3526d800 1843
ef8693eb
MW
1844 /* poll until we see the reset actually happen */
1845 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
ee5c1e92
MW
1846 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1847 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1848 if (!reg_val)
ef8693eb 1849 break;
ee5c1e92 1850 usleep_range(5000, 10000);
ef8693eb
MW
1851 }
1852 if (i == I40EVF_RESET_WAIT_COUNT) {
67c818a1 1853 dev_info(&adapter->pdev->dev, "Never saw reset\n");
ef8693eb
MW
1854 goto continue_reset; /* act like the reset happened */
1855 }
5eae00c5 1856
ef8693eb
MW
1857 /* wait until the reset is complete and the PF is responding to us */
1858 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
7d3f04af
JK
1859 /* sleep first to make sure a minimum wait time is met */
1860 msleep(I40EVF_RESET_WAIT_MS);
1861
ee5c1e92
MW
1862 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1863 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
310a2ad9 1864 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
5eae00c5 1865 break;
5eae00c5 1866 }
7d3f04af 1867
509a447a 1868 pci_set_master(adapter->pdev);
7d3f04af 1869
ef8693eb 1870 if (i == I40EVF_RESET_WAIT_COUNT) {
80e72893 1871 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
ee5c1e92 1872 reg_val);
dedecb6d 1873 i40evf_disable_vf(adapter);
ed0e894d 1874 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
ef8693eb 1875 return; /* Do not attempt to reinit. It's dead, Jim. */
5eae00c5 1876 }
ef8693eb
MW
1877
1878continue_reset:
f0db7892 1879 if (netif_running(netdev)) {
3c8e0b98 1880 netif_carrier_off(netdev);
67c818a1 1881 netif_tx_stop_all_queues(netdev);
3f341acc 1882 adapter->link_up = false;
67c818a1 1883 i40evf_napi_disable_all(adapter);
3c8e0b98 1884 }
67c818a1 1885 i40evf_irq_disable(adapter);
ac833bbf 1886
5eae00c5 1887 adapter->state = __I40EVF_RESETTING;
67c818a1
MW
1888 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1889
1890 /* free the Tx/Rx rings and descriptors, might be better to just
1891 * re-use them sometime in the future
1892 */
1893 i40evf_free_all_rx_resources(adapter);
1894 i40evf_free_all_tx_resources(adapter);
5eae00c5
GR
1895
1896 /* kill and reinit the admin queue */
903e6832 1897 i40evf_shutdown_adminq(hw);
310a2ad9 1898 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
1899 err = i40evf_init_adminq(hw);
1900 if (err)
ac833bbf
MW
1901 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1902 err);
5eae00c5 1903
e6d038de
MW
1904 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1905 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
ac833bbf
MW
1906
1907 /* re-add all MAC filters */
1908 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1909 f->add = true;
1910 }
1911 /* re-add all VLAN filters */
40d01366
MW
1912 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1913 vlf->add = true;
ac833bbf 1914 }
e6d038de 1915 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
ac833bbf 1916 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
5eae00c5 1917 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
ed0e894d 1918 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
67c818a1 1919 i40evf_misc_irq_enable(adapter);
5eae00c5
GR
1920
1921 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1922
1923 if (netif_running(adapter->netdev)) {
1924 /* allocate transmit descriptors */
1925 err = i40evf_setup_all_tx_resources(adapter);
1926 if (err)
1927 goto reset_err;
1928
1929 /* allocate receive descriptors */
1930 err = i40evf_setup_all_rx_resources(adapter);
1931 if (err)
1932 goto reset_err;
1933
1934 i40evf_configure(adapter);
1935
cb130a0b 1936 i40evf_up_complete(adapter);
5eae00c5
GR
1937
1938 i40evf_irq_enable(adapter, true);
67c818a1
MW
1939 } else {
1940 adapter->state = __I40EVF_DOWN;
fe2647ab 1941 wake_up(&adapter->down_waitqueue);
5eae00c5 1942 }
67c818a1 1943
5eae00c5
GR
1944 return;
1945reset_err:
80e72893 1946 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
f0db7892 1947 i40evf_close(netdev);
5eae00c5
GR
1948}
1949
1950/**
1951 * i40evf_adminq_task - worker thread to clean the admin queue
1952 * @work: pointer to work_struct containing our data
1953 **/
1954static void i40evf_adminq_task(struct work_struct *work)
1955{
1956 struct i40evf_adapter *adapter =
1957 container_of(work, struct i40evf_adapter, adminq_task);
1958 struct i40e_hw *hw = &adapter->hw;
1959 struct i40e_arq_event_info event;
c969ef4e
TD
1960 enum virtchnl_ops v_op;
1961 i40e_status ret, v_ret;
912257e5 1962 u32 val, oldval;
5eae00c5
GR
1963 u16 pending;
1964
ef8693eb 1965 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
7235448c 1966 goto out;
ef8693eb 1967
1001dc37
MW
1968 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1969 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
249c8b8d 1970 if (!event.msg_buf)
7235448c 1971 goto out;
249c8b8d 1972
5eae00c5
GR
1973 do {
1974 ret = i40evf_clean_arq_element(hw, &event, &pending);
c969ef4e
TD
1975 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
1976 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
1977
1978 if (ret || !v_op)
5eae00c5
GR
1979 break; /* No event to process or error cleaning ARQ */
1980
c969ef4e 1981 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
1001dc37 1982 event.msg_len);
75a64435 1983 if (pending != 0)
5eae00c5 1984 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
5eae00c5
GR
1985 } while (pending);
1986
67c818a1
MW
1987 if ((adapter->flags &
1988 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1989 adapter->state == __I40EVF_RESETTING)
1990 goto freedom;
1991
912257e5
MW
1992 /* check for error indications */
1993 val = rd32(hw, hw->aq.arq.len);
19b73d8e
MW
1994 if (val == 0xdeadbeef) /* indicates device in reset */
1995 goto freedom;
912257e5 1996 oldval = val;
b1f3366b 1997 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
912257e5 1998 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
b1f3366b 1999 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
912257e5 2000 }
b1f3366b 2001 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
912257e5 2002 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
b1f3366b 2003 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
912257e5 2004 }
b1f3366b 2005 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
912257e5 2006 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
b1f3366b 2007 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
912257e5
MW
2008 }
2009 if (oldval != val)
2010 wr32(hw, hw->aq.arq.len, val);
2011
2012 val = rd32(hw, hw->aq.asq.len);
2013 oldval = val;
b1f3366b 2014 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
912257e5 2015 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
b1f3366b 2016 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
912257e5 2017 }
b1f3366b 2018 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
912257e5 2019 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
b1f3366b 2020 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
912257e5 2021 }
b1f3366b 2022 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
912257e5 2023 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
b1f3366b 2024 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
912257e5
MW
2025 }
2026 if (oldval != val)
2027 wr32(hw, hw->aq.asq.len, val);
2028
67c818a1 2029freedom:
7235448c
MW
2030 kfree(event.msg_buf);
2031out:
5eae00c5
GR
2032 /* re-enable Admin queue interrupt cause */
2033 i40evf_misc_irq_enable(adapter);
5eae00c5
GR
2034}
2035
ed0e894d
MW
2036/**
2037 * i40evf_client_task - worker thread to perform client work
2038 * @work: pointer to work_struct containing our data
2039 *
2040 * This task handles client interactions. Because client calls can be
2041 * reentrant, we can't handle them in the watchdog.
2042 **/
2043static void i40evf_client_task(struct work_struct *work)
2044{
2045 struct i40evf_adapter *adapter =
2046 container_of(work, struct i40evf_adapter, client_task.work);
2047
2048 /* If we can't get the client bit, just give up. We'll be rescheduled
2049 * later.
2050 */
2051
2052 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2053 return;
2054
2055 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2056 i40evf_client_subtask(adapter);
2057 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2058 goto out;
2059 }
2060 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2061 i40evf_notify_client_close(&adapter->vsi, false);
2062 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2063 goto out;
2064 }
2065 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2066 i40evf_notify_client_open(&adapter->vsi);
2067 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2068 goto out;
2069 }
2070 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2071 i40evf_notify_client_l2_params(&adapter->vsi);
2072 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2073 }
2074out:
2075 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2076}
2077
5eae00c5
GR
2078/**
2079 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2080 * @adapter: board private structure
2081 *
2082 * Free all transmit software resources
2083 **/
e284fc88 2084void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
5eae00c5
GR
2085{
2086 int i;
2087
fdb47ae8
MW
2088 if (!adapter->tx_rings)
2089 return;
2090
cc052927 2091 for (i = 0; i < adapter->num_active_queues; i++)
0dd438d8
MW
2092 if (adapter->tx_rings[i].desc)
2093 i40evf_free_tx_resources(&adapter->tx_rings[i]);
5eae00c5
GR
2094}
2095
2096/**
2097 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2098 * @adapter: board private structure
2099 *
2100 * If this function returns with an error, then it's possible one or
2101 * more of the rings is populated (while the rest are not). It is the
2102 * callers duty to clean those orphaned rings.
2103 *
2104 * Return 0 on success, negative on failure
2105 **/
2106static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2107{
2108 int i, err = 0;
2109
cc052927 2110 for (i = 0; i < adapter->num_active_queues; i++) {
0dd438d8
MW
2111 adapter->tx_rings[i].count = adapter->tx_desc_count;
2112 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
5eae00c5
GR
2113 if (!err)
2114 continue;
2115 dev_err(&adapter->pdev->dev,
fb43201f 2116 "Allocation for Tx Queue %u failed\n", i);
5eae00c5
GR
2117 break;
2118 }
2119
2120 return err;
2121}
2122
2123/**
2124 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2125 * @adapter: board private structure
2126 *
2127 * If this function returns with an error, then it's possible one or
2128 * more of the rings is populated (while the rest are not). It is the
2129 * callers duty to clean those orphaned rings.
2130 *
2131 * Return 0 on success, negative on failure
2132 **/
2133static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2134{
2135 int i, err = 0;
2136
cc052927 2137 for (i = 0; i < adapter->num_active_queues; i++) {
0dd438d8
MW
2138 adapter->rx_rings[i].count = adapter->rx_desc_count;
2139 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
5eae00c5
GR
2140 if (!err)
2141 continue;
2142 dev_err(&adapter->pdev->dev,
fb43201f 2143 "Allocation for Rx Queue %u failed\n", i);
5eae00c5
GR
2144 break;
2145 }
2146 return err;
2147}
2148
2149/**
2150 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2151 * @adapter: board private structure
2152 *
2153 * Free all receive software resources
2154 **/
e284fc88 2155void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
5eae00c5
GR
2156{
2157 int i;
2158
fdb47ae8
MW
2159 if (!adapter->rx_rings)
2160 return;
2161
cc052927 2162 for (i = 0; i < adapter->num_active_queues; i++)
0dd438d8
MW
2163 if (adapter->rx_rings[i].desc)
2164 i40evf_free_rx_resources(&adapter->rx_rings[i]);
5eae00c5
GR
2165}
2166
2167/**
2168 * i40evf_open - Called when a network interface is made active
2169 * @netdev: network interface device structure
2170 *
2171 * Returns 0 on success, negative value on failure
2172 *
2173 * The open entry point is called when a network interface is made
2174 * active by the system (IFF_UP). At this point all resources needed
2175 * for transmit and receive operations are allocated, the interrupt
2176 * handler is registered with the OS, the watchdog timer is started,
2177 * and the stack is notified that the interface is ready.
2178 **/
2179static int i40evf_open(struct net_device *netdev)
2180{
2181 struct i40evf_adapter *adapter = netdev_priv(netdev);
2182 int err;
2183
ef8693eb
MW
2184 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2185 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2186 return -EIO;
2187 }
209dc4da
MW
2188
2189 if (adapter->state != __I40EVF_DOWN)
5eae00c5
GR
2190 return -EBUSY;
2191
2192 /* allocate transmit descriptors */
2193 err = i40evf_setup_all_tx_resources(adapter);
2194 if (err)
2195 goto err_setup_tx;
2196
2197 /* allocate receive descriptors */
2198 err = i40evf_setup_all_rx_resources(adapter);
2199 if (err)
2200 goto err_setup_rx;
2201
2202 /* clear any pending interrupts, may auto mask */
2203 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2204 if (err)
2205 goto err_req_irq;
2206
44151cd3 2207 i40evf_add_filter(adapter, adapter->hw.mac.addr);
5eae00c5
GR
2208 i40evf_configure(adapter);
2209
cb130a0b 2210 i40evf_up_complete(adapter);
5eae00c5
GR
2211
2212 i40evf_irq_enable(adapter, true);
2213
2214 return 0;
2215
2216err_req_irq:
2217 i40evf_down(adapter);
2218 i40evf_free_traffic_irqs(adapter);
2219err_setup_rx:
2220 i40evf_free_all_rx_resources(adapter);
2221err_setup_tx:
2222 i40evf_free_all_tx_resources(adapter);
2223
2224 return err;
2225}
2226
2227/**
2228 * i40evf_close - Disables a network interface
2229 * @netdev: network interface device structure
2230 *
2231 * Returns 0, this is not allowed to fail
2232 *
2233 * The close entry point is called when an interface is de-activated
2234 * by the OS. The hardware is still under the drivers control, but
2235 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2236 * are freed, along with all transmit and receive resources.
2237 **/
2238static int i40evf_close(struct net_device *netdev)
2239{
2240 struct i40evf_adapter *adapter = netdev_priv(netdev);
fe2647ab 2241 int status;
5eae00c5 2242
209dc4da 2243 if (adapter->state <= __I40EVF_DOWN_PENDING)
ef8693eb
MW
2244 return 0;
2245
ef8693eb 2246
0da36b97 2247 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
ed0e894d
MW
2248 if (CLIENT_ENABLED(adapter))
2249 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
5eae00c5
GR
2250
2251 i40evf_down(adapter);
209dc4da 2252 adapter->state = __I40EVF_DOWN_PENDING;
5eae00c5
GR
2253 i40evf_free_traffic_irqs(adapter);
2254
51f38262
MW
2255 /* We explicitly don't free resources here because the hardware is
2256 * still active and can DMA into memory. Resources are cleared in
2257 * i40evf_virtchnl_completion() after we get confirmation from the PF
2258 * driver that the rings have been stopped.
fe2647ab
SM
2259 *
2260 * Also, we wait for state to transition to __I40EVF_DOWN before
2261 * returning. State change occurs in i40evf_virtchnl_completion() after
2262 * VF resources are released (which occurs after PF driver processes and
2263 * responds to admin queue commands).
51f38262 2264 */
fe2647ab
SM
2265
2266 status = wait_event_timeout(adapter->down_waitqueue,
2267 adapter->state == __I40EVF_DOWN,
2268 msecs_to_jiffies(200));
2269 if (!status)
2270 netdev_warn(netdev, "Device resources not yet released\n");
5eae00c5
GR
2271 return 0;
2272}
2273
5eae00c5
GR
2274/**
2275 * i40evf_change_mtu - Change the Maximum Transfer Unit
2276 * @netdev: network interface device structure
2277 * @new_mtu: new value for maximum frame size
2278 *
2279 * Returns 0 on success, negative on failure
2280 **/
2281static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2282{
2283 struct i40evf_adapter *adapter = netdev_priv(netdev);
5eae00c5 2284
5eae00c5 2285 netdev->mtu = new_mtu;
ed0e894d
MW
2286 if (CLIENT_ENABLED(adapter)) {
2287 i40evf_notify_client_l2_params(&adapter->vsi);
2288 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2289 }
67c818a1
MW
2290 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2291 schedule_work(&adapter->reset_task);
2292
5eae00c5
GR
2293 return 0;
2294}
2295
06fc016c
AD
2296/**
2297 * i40evf_features_check - Validate encapsulated packet conforms to limits
2298 * @skb: skb buff
2299 * @netdev: This physical port's netdev
2300 * @features: Offload features that the stack believes apply
2301 **/
2302static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2303 struct net_device *dev,
2304 netdev_features_t features)
2305{
2306 size_t len;
2307
2308 /* No point in doing any of this if neither checksum nor GSO are
2309 * being requested for this frame. We can rule out both by just
2310 * checking for CHECKSUM_PARTIAL
2311 */
2312 if (skb->ip_summed != CHECKSUM_PARTIAL)
2313 return features;
2314
2315 /* We cannot support GSO if the MSS is going to be less than
2316 * 64 bytes. If it is then we need to drop support for GSO.
2317 */
2318 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2319 features &= ~NETIF_F_GSO_MASK;
2320
2321 /* MACLEN can support at most 63 words */
2322 len = skb_network_header(skb) - skb->data;
2323 if (len & ~(63 * 2))
2324 goto out_err;
2325
2326 /* IPLEN and EIPLEN can support at most 127 dwords */
2327 len = skb_transport_header(skb) - skb_network_header(skb);
2328 if (len & ~(127 * 4))
2329 goto out_err;
2330
2331 if (skb->encapsulation) {
2332 /* L4TUNLEN can support 127 words */
2333 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2334 if (len & ~(127 * 2))
2335 goto out_err;
2336
2337 /* IPLEN can support at most 127 dwords */
2338 len = skb_inner_transport_header(skb) -
2339 skb_inner_network_header(skb);
2340 if (len & ~(127 * 4))
2341 goto out_err;
2342 }
2343
2344 /* No need to validate L4LEN as TCP is the only protocol with a
2345 * a flexible value and we support all possible values supported
2346 * by TCP, which is at most 15 dwords
2347 */
2348
2349 return features;
2350out_err:
2351 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2352}
2353
c4445aed
MW
2354#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2355 NETIF_F_HW_VLAN_CTAG_RX |\
2356 NETIF_F_HW_VLAN_CTAG_FILTER)
2357
2358/**
2359 * i40evf_fix_features - fix up the netdev feature bits
2360 * @netdev: our net device
2361 * @features: desired feature bits
2362 *
2363 * Returns fixed-up features bits
2364 **/
2365static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2366 netdev_features_t features)
2367{
2368 struct i40evf_adapter *adapter = netdev_priv(netdev);
2369
2370 features &= ~I40EVF_VLAN_FEATURES;
fbb113f7 2371 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
c4445aed
MW
2372 features |= I40EVF_VLAN_FEATURES;
2373 return features;
2374}
2375
5eae00c5
GR
2376static const struct net_device_ops i40evf_netdev_ops = {
2377 .ndo_open = i40evf_open,
2378 .ndo_stop = i40evf_close,
2379 .ndo_start_xmit = i40evf_xmit_frame,
5eae00c5
GR
2380 .ndo_set_rx_mode = i40evf_set_rx_mode,
2381 .ndo_validate_addr = eth_validate_addr,
2382 .ndo_set_mac_address = i40evf_set_mac,
2383 .ndo_change_mtu = i40evf_change_mtu,
2384 .ndo_tx_timeout = i40evf_tx_timeout,
2385 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2386 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
06fc016c 2387 .ndo_features_check = i40evf_features_check,
c4445aed 2388 .ndo_fix_features = i40evf_fix_features,
7709b4c1
AD
2389#ifdef CONFIG_NET_POLL_CONTROLLER
2390 .ndo_poll_controller = i40evf_netpoll,
2391#endif
5eae00c5
GR
2392};
2393
2394/**
2395 * i40evf_check_reset_complete - check that VF reset is complete
2396 * @hw: pointer to hw struct
2397 *
2398 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2399 **/
2400static int i40evf_check_reset_complete(struct i40e_hw *hw)
2401{
2402 u32 rstat;
2403 int i;
2404
2405 for (i = 0; i < 100; i++) {
fd35886a
AS
2406 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2407 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
310a2ad9
JB
2408 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2409 (rstat == VIRTCHNL_VFR_COMPLETED))
5eae00c5 2410 return 0;
f98a2006 2411 usleep_range(10, 20);
5eae00c5
GR
2412 }
2413 return -EBUSY;
2414}
2415
e6d038de
MW
2416/**
2417 * i40evf_process_config - Process the config information we got from the PF
2418 * @adapter: board private structure
2419 *
2420 * Verify that we have a valid config struct, and set up our netdev features
2421 * and our VSI struct.
2422 **/
2423int i40evf_process_config(struct i40evf_adapter *adapter)
2424{
310a2ad9 2425 struct virtchnl_vf_resource *vfres = adapter->vf_res;
e6d038de 2426 struct net_device *netdev = adapter->netdev;
43a3d9ba 2427 struct i40e_vsi *vsi = &adapter->vsi;
e6d038de 2428 int i;
bacd75cf
PB
2429 netdev_features_t hw_enc_features;
2430 netdev_features_t hw_features;
e6d038de
MW
2431
2432 /* got VF config message back from PF, now we can parse it */
ba6cc7f6 2433 for (i = 0; i < vfres->num_vsis; i++) {
ff3f4cc2 2434 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
ba6cc7f6 2435 adapter->vsi_res = &vfres->vsi_res[i];
e6d038de
MW
2436 }
2437 if (!adapter->vsi_res) {
2438 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2439 return -ENODEV;
2440 }
2441
bacd75cf
PB
2442 hw_enc_features = NETIF_F_SG |
2443 NETIF_F_IP_CSUM |
2444 NETIF_F_IPV6_CSUM |
2445 NETIF_F_HIGHDMA |
2446 NETIF_F_SOFT_FEATURES |
2447 NETIF_F_TSO |
2448 NETIF_F_TSO_ECN |
2449 NETIF_F_TSO6 |
2450 NETIF_F_SCTP_CRC |
2451 NETIF_F_RXHASH |
2452 NETIF_F_RXCSUM |
2453 0;
2454
2455 /* advertise to stack only if offloads for encapsulated packets is
2456 * supported
2457 */
fbb113f7 2458 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
bacd75cf 2459 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
b0fe3306 2460 NETIF_F_GSO_GRE |
1c7b4a23 2461 NETIF_F_GSO_GRE_CSUM |
7e13318d 2462 NETIF_F_GSO_IPXIP4 |
bf2d1df3 2463 NETIF_F_GSO_IPXIP6 |
b0fe3306 2464 NETIF_F_GSO_UDP_TUNNEL_CSUM |
1c7b4a23 2465 NETIF_F_GSO_PARTIAL |
b0fe3306
AD
2466 0;
2467
fbb113f7 2468 if (!(vfres->vf_cap_flags &
310a2ad9 2469 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
bacd75cf
PB
2470 netdev->gso_partial_features |=
2471 NETIF_F_GSO_UDP_TUNNEL_CSUM;
b0fe3306 2472
bacd75cf
PB
2473 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2474 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2475 netdev->hw_enc_features |= hw_enc_features;
2476 }
b0fe3306 2477 /* record features VLANs can make use of */
bacd75cf 2478 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
b0fe3306
AD
2479
2480 /* Write features and hw_features separately to avoid polluting
bacd75cf 2481 * with, or dropping, features that are set when we registered.
b0fe3306 2482 */
bacd75cf 2483 hw_features = hw_enc_features;
b0fe3306 2484
bacd75cf 2485 netdev->hw_features |= hw_features;
b0fe3306 2486
bacd75cf 2487 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
e6d038de
MW
2488
2489 adapter->vsi.id = adapter->vsi_res->vsi_id;
2490
2491 adapter->vsi.back = adapter;
2492 adapter->vsi.base_vector = 1;
2493 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
43a3d9ba
MW
2494 vsi->netdev = adapter->netdev;
2495 vsi->qs_handle = adapter->vsi_res->qset_handle;
fbb113f7 2496 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
43a3d9ba
MW
2497 adapter->rss_key_size = vfres->rss_key_size;
2498 adapter->rss_lut_size = vfres->rss_lut_size;
2499 } else {
2500 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2501 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2502 }
2503
e6d038de
MW
2504 return 0;
2505}
2506
5eae00c5
GR
2507/**
2508 * i40evf_init_task - worker thread to perform delayed initialization
2509 * @work: pointer to work_struct containing our data
2510 *
2511 * This task completes the work that was begun in probe. Due to the nature
2512 * of VF-PF communications, we may need to wait tens of milliseconds to get
dbedd44e 2513 * responses back from the PF. Rather than busy-wait in probe and bog down the
5eae00c5
GR
2514 * whole system, we'll do it in a task so we can sleep.
2515 * This task only runs during driver init. Once we've established
2516 * communications with the PF driver and set up our netdev, the watchdog
2517 * takes over.
2518 **/
2519static void i40evf_init_task(struct work_struct *work)
2520{
2521 struct i40evf_adapter *adapter = container_of(work,
2522 struct i40evf_adapter,
2523 init_task.work);
2524 struct net_device *netdev = adapter->netdev;
5eae00c5
GR
2525 struct i40e_hw *hw = &adapter->hw;
2526 struct pci_dev *pdev = adapter->pdev;
e6d038de 2527 int err, bufsz;
5eae00c5
GR
2528
2529 switch (adapter->state) {
2530 case __I40EVF_STARTUP:
2531 /* driver loaded, probe complete */
ef8693eb
MW
2532 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2533 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
5eae00c5
GR
2534 err = i40e_set_mac_type(hw);
2535 if (err) {
c2a137cb
MW
2536 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2537 err);
2619ef47 2538 goto err;
5eae00c5
GR
2539 }
2540 err = i40evf_check_reset_complete(hw);
2541 if (err) {
0d9c7ea8 2542 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
75a64435 2543 err);
5eae00c5
GR
2544 goto err;
2545 }
2546 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2547 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2548 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2549 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2550
2551 err = i40evf_init_adminq(hw);
2552 if (err) {
c2a137cb
MW
2553 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2554 err);
5eae00c5
GR
2555 goto err;
2556 }
2557 err = i40evf_send_api_ver(adapter);
2558 if (err) {
10bdd67b 2559 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
5eae00c5
GR
2560 i40evf_shutdown_adminq(hw);
2561 goto err;
2562 }
2563 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2564 goto restart;
5eae00c5 2565 case __I40EVF_INIT_VERSION_CHECK:
10bdd67b 2566 if (!i40evf_asq_done(hw)) {
80e72893 2567 dev_err(&pdev->dev, "Admin queue command never completed\n");
906a6937
MW
2568 i40evf_shutdown_adminq(hw);
2569 adapter->state = __I40EVF_STARTUP;
5eae00c5 2570 goto err;
10bdd67b 2571 }
5eae00c5
GR
2572
2573 /* aq msg sent, awaiting reply */
2574 err = i40evf_verify_api_ver(adapter);
2575 if (err) {
d4f82fd3 2576 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
56f9920a 2577 err = i40evf_send_api_ver(adapter);
ee1693e5
MW
2578 else
2579 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2580 adapter->pf_version.major,
2581 adapter->pf_version.minor,
310a2ad9
JB
2582 VIRTCHNL_VERSION_MAJOR,
2583 VIRTCHNL_VERSION_MINOR);
5eae00c5
GR
2584 goto err;
2585 }
2586 err = i40evf_send_vf_config_msg(adapter);
2587 if (err) {
3f2ab172 2588 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
5eae00c5
GR
2589 err);
2590 goto err;
2591 }
2592 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2593 goto restart;
5eae00c5
GR
2594 case __I40EVF_INIT_GET_RESOURCES:
2595 /* aq msg sent, awaiting reply */
2596 if (!adapter->vf_res) {
310a2ad9 2597 bufsz = sizeof(struct virtchnl_vf_resource) +
5eae00c5 2598 (I40E_MAX_VF_VSI *
310a2ad9 2599 sizeof(struct virtchnl_vsi_resource));
5eae00c5 2600 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
c2a137cb 2601 if (!adapter->vf_res)
5eae00c5 2602 goto err;
5eae00c5
GR
2603 }
2604 err = i40evf_get_vf_config(adapter);
906a6937 2605 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
906a6937
MW
2606 err = i40evf_send_vf_config_msg(adapter);
2607 goto err;
e743072f
MW
2608 } else if (err == I40E_ERR_PARAM) {
2609 /* We only get ERR_PARAM if the device is in a very bad
2610 * state or if we've been disabled for previous bad
2611 * behavior. Either way, we're done now.
2612 */
2613 i40evf_shutdown_adminq(hw);
2614 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2615 return;
906a6937 2616 }
5eae00c5 2617 if (err) {
c2a137cb
MW
2618 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2619 err);
5eae00c5
GR
2620 goto err_alloc;
2621 }
2622 adapter->state = __I40EVF_INIT_SW;
2623 break;
2624 default:
2625 goto err_alloc;
2626 }
f608e6a6 2627
e6d038de 2628 if (i40evf_process_config(adapter))
5eae00c5 2629 goto err_alloc;
310a2ad9 2630 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
2631
2632 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2633
5eae00c5
GR
2634 netdev->netdev_ops = &i40evf_netdev_ops;
2635 i40evf_set_ethtool_ops(netdev);
2636 netdev->watchdog_timeo = 5 * HZ;
3415e8ce 2637
91c527a5
JW
2638 /* MTU range: 68 - 9710 */
2639 netdev->min_mtu = ETH_MIN_MTU;
1e3a5fd5 2640 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
91c527a5 2641
5eae00c5 2642 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
b34f90e7 2643 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
c2a137cb 2644 adapter->hw.mac.addr);
14e52ee2
MW
2645 eth_hw_addr_random(netdev);
2646 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2647 } else {
2648 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2649 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2650 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
5eae00c5 2651 }
5eae00c5 2652
5eae00c5
GR
2653 init_timer(&adapter->watchdog_timer);
2654 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2655 adapter->watchdog_timer.data = (unsigned long)adapter;
2656 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2657
d732a184
MW
2658 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2659 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
5eae00c5
GR
2660 err = i40evf_init_interrupt_scheme(adapter);
2661 if (err)
2662 goto err_sw_init;
2663 i40evf_map_rings_to_vectors(adapter);
fbb113f7 2664 if (adapter->vf_res->vf_cap_flags &
310a2ad9 2665 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
f6d83d13
ASJ
2666 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2667
5eae00c5
GR
2668 err = i40evf_request_misc_irq(adapter);
2669 if (err)
2670 goto err_sw_init;
2671
2672 netif_carrier_off(netdev);
3f341acc 2673 adapter->link_up = false;
5eae00c5 2674
ef8693eb
MW
2675 if (!adapter->netdev_registered) {
2676 err = register_netdev(netdev);
2677 if (err)
2678 goto err_register;
2679 }
5eae00c5
GR
2680
2681 adapter->netdev_registered = true;
2682
2683 netif_tx_stop_all_queues(netdev);
ed0e894d
MW
2684 if (CLIENT_ALLOWED(adapter)) {
2685 err = i40evf_lan_add_device(adapter);
2686 if (err)
2687 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2688 err);
2689 }
5eae00c5 2690
b34f90e7 2691 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
5eae00c5
GR
2692 if (netdev->features & NETIF_F_GRO)
2693 dev_info(&pdev->dev, "GRO is enabled\n");
2694
5eae00c5 2695 adapter->state = __I40EVF_DOWN;
0da36b97 2696 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
5eae00c5 2697 i40evf_misc_irq_enable(adapter);
fe2647ab 2698 wake_up(&adapter->down_waitqueue);
e25d00b8 2699
43a3d9ba
MW
2700 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2701 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2702 if (!adapter->rss_key || !adapter->rss_lut)
2703 goto err_mem;
2704
e25d00b8
ASJ
2705 if (RSS_AQ(adapter)) {
2706 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2707 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2708 } else {
96a81986 2709 i40evf_init_rss(adapter);
e25d00b8 2710 }
5eae00c5
GR
2711 return;
2712restart:
3f7e5c33 2713 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
5eae00c5 2714 return;
43a3d9ba
MW
2715err_mem:
2716 i40evf_free_rss(adapter);
5eae00c5
GR
2717err_register:
2718 i40evf_free_misc_irq(adapter);
2719err_sw_init:
2720 i40evf_reset_interrupt_capability(adapter);
5eae00c5
GR
2721err_alloc:
2722 kfree(adapter->vf_res);
2723 adapter->vf_res = NULL;
2724err:
2725 /* Things went into the weeds, so try again later */
2726 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
b9029e94 2727 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
ef8693eb 2728 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
b9029e94
MW
2729 i40evf_shutdown_adminq(hw);
2730 adapter->state = __I40EVF_STARTUP;
2731 schedule_delayed_work(&adapter->init_task, HZ * 5);
2732 return;
5eae00c5 2733 }
3f7e5c33 2734 schedule_delayed_work(&adapter->init_task, HZ);
5eae00c5
GR
2735}
2736
2737/**
2738 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2739 * @pdev: pci device structure
2740 **/
2741static void i40evf_shutdown(struct pci_dev *pdev)
2742{
2743 struct net_device *netdev = pci_get_drvdata(pdev);
00293fdc 2744 struct i40evf_adapter *adapter = netdev_priv(netdev);
5eae00c5
GR
2745
2746 netif_device_detach(netdev);
2747
2748 if (netif_running(netdev))
2749 i40evf_close(netdev);
2750
00293fdc
MW
2751 /* Prevent the watchdog from running. */
2752 adapter->state = __I40EVF_REMOVE;
2753 adapter->aq_required = 0;
00293fdc 2754
5eae00c5
GR
2755#ifdef CONFIG_PM
2756 pci_save_state(pdev);
2757
2758#endif
2759 pci_disable_device(pdev);
2760}
2761
2762/**
2763 * i40evf_probe - Device Initialization Routine
2764 * @pdev: PCI device information struct
2765 * @ent: entry in i40evf_pci_tbl
2766 *
2767 * Returns 0 on success, negative on failure
2768 *
2769 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2770 * The OS initialization, configuring of the adapter private structure,
2771 * and a hardware reset occur.
2772 **/
2773static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2774{
2775 struct net_device *netdev;
2776 struct i40evf_adapter *adapter = NULL;
2777 struct i40e_hw *hw = NULL;
dbbd8111 2778 int err;
5eae00c5
GR
2779
2780 err = pci_enable_device(pdev);
2781 if (err)
2782 return err;
2783
6494294f 2784 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6494294f 2785 if (err) {
e3e3bfdd
JS
2786 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2787 if (err) {
2788 dev_err(&pdev->dev,
2789 "DMA configuration failed: 0x%x\n", err);
2790 goto err_dma;
2791 }
5eae00c5
GR
2792 }
2793
2794 err = pci_request_regions(pdev, i40evf_driver_name);
2795 if (err) {
2796 dev_err(&pdev->dev,
2797 "pci_request_regions failed 0x%x\n", err);
2798 goto err_pci_reg;
2799 }
2800
2801 pci_enable_pcie_error_reporting(pdev);
2802
2803 pci_set_master(pdev);
2804
1255b7a1 2805 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
5eae00c5
GR
2806 if (!netdev) {
2807 err = -ENOMEM;
2808 goto err_alloc_etherdev;
2809 }
2810
2811 SET_NETDEV_DEV(netdev, &pdev->dev);
2812
2813 pci_set_drvdata(pdev, netdev);
2814 adapter = netdev_priv(netdev);
5eae00c5
GR
2815
2816 adapter->netdev = netdev;
2817 adapter->pdev = pdev;
2818
2819 hw = &adapter->hw;
2820 hw->back = adapter;
2821
41a1d04b 2822 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
5eae00c5
GR
2823 adapter->state = __I40EVF_STARTUP;
2824
2825 /* Call save state here because it relies on the adapter struct. */
2826 pci_save_state(pdev);
2827
2828 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2829 pci_resource_len(pdev, 0));
2830 if (!hw->hw_addr) {
2831 err = -EIO;
2832 goto err_ioremap;
2833 }
2834 hw->vendor_id = pdev->vendor;
2835 hw->device_id = pdev->device;
2836 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2837 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2838 hw->subsystem_device_id = pdev->subsystem_device;
2839 hw->bus.device = PCI_SLOT(pdev->devfn);
2840 hw->bus.func = PCI_FUNC(pdev->devfn);
b3f028fc 2841 hw->bus.bus_id = pdev->bus->number;
5eae00c5 2842
8ddb3326
JB
2843 /* set up the locks for the AQ, do this only once in probe
2844 * and destroy them only once in remove
2845 */
2846 mutex_init(&hw->aq.asq_mutex);
2847 mutex_init(&hw->aq.arq_mutex);
2848
8bb1a540
SK
2849 INIT_LIST_HEAD(&adapter->mac_filter_list);
2850 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2851
5eae00c5
GR
2852 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2853 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2854 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
ed0e894d 2855 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
5eae00c5 2856 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
9b32b0b5
MW
2857 schedule_delayed_work(&adapter->init_task,
2858 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
5eae00c5 2859
fe2647ab
SM
2860 /* Setup the wait queue for indicating transition to down status */
2861 init_waitqueue_head(&adapter->down_waitqueue);
2862
5eae00c5
GR
2863 return 0;
2864
2865err_ioremap:
2866 free_netdev(netdev);
2867err_alloc_etherdev:
2868 pci_release_regions(pdev);
2869err_pci_reg:
2870err_dma:
2871 pci_disable_device(pdev);
2872 return err;
2873}
2874
2875#ifdef CONFIG_PM
2876/**
2877 * i40evf_suspend - Power management suspend routine
2878 * @pdev: PCI device information struct
2879 * @state: unused
2880 *
2881 * Called when the system (VM) is entering sleep/suspend.
2882 **/
2883static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2884{
2885 struct net_device *netdev = pci_get_drvdata(pdev);
2886 struct i40evf_adapter *adapter = netdev_priv(netdev);
2887 int retval = 0;
2888
2889 netif_device_detach(netdev);
2890
2891 if (netif_running(netdev)) {
2892 rtnl_lock();
2893 i40evf_down(adapter);
2894 rtnl_unlock();
2895 }
2896 i40evf_free_misc_irq(adapter);
2897 i40evf_reset_interrupt_capability(adapter);
2898
2899 retval = pci_save_state(pdev);
2900 if (retval)
2901 return retval;
2902
2903 pci_disable_device(pdev);
2904
2905 return 0;
2906}
2907
2908/**
dbedd44e 2909 * i40evf_resume - Power management resume routine
5eae00c5
GR
2910 * @pdev: PCI device information struct
2911 *
2912 * Called when the system (VM) is resumed from sleep/suspend.
2913 **/
2914static int i40evf_resume(struct pci_dev *pdev)
2915{
2916 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2917 struct net_device *netdev = adapter->netdev;
2918 u32 err;
2919
2920 pci_set_power_state(pdev, PCI_D0);
2921 pci_restore_state(pdev);
2922 /* pci_restore_state clears dev->state_saved so call
2923 * pci_save_state to restore it.
2924 */
2925 pci_save_state(pdev);
2926
2927 err = pci_enable_device_mem(pdev);
2928 if (err) {
2929 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2930 return err;
2931 }
2932 pci_set_master(pdev);
2933
2934 rtnl_lock();
2935 err = i40evf_set_interrupt_capability(adapter);
2936 if (err) {
f2a1c368 2937 rtnl_unlock();
5eae00c5
GR
2938 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2939 return err;
2940 }
2941 err = i40evf_request_misc_irq(adapter);
2942 rtnl_unlock();
2943 if (err) {
2944 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2945 return err;
2946 }
2947
2948 schedule_work(&adapter->reset_task);
2949
2950 netif_device_attach(netdev);
2951
2952 return err;
2953}
2954
2955#endif /* CONFIG_PM */
2956/**
2957 * i40evf_remove - Device Removal Routine
2958 * @pdev: PCI device information struct
2959 *
2960 * i40evf_remove is called by the PCI subsystem to alert the driver
2961 * that it should release a PCI device. The could be caused by a
2962 * Hot-Plug event, or because the driver is going to be removed from
2963 * memory.
2964 **/
2965static void i40evf_remove(struct pci_dev *pdev)
2966{
2967 struct net_device *netdev = pci_get_drvdata(pdev);
2968 struct i40evf_adapter *adapter = netdev_priv(netdev);
6ba36a24 2969 struct i40evf_mac_filter *f, *ftmp;
5eae00c5 2970 struct i40e_hw *hw = &adapter->hw;
ed0e894d 2971 int err;
5eae00c5
GR
2972
2973 cancel_delayed_work_sync(&adapter->init_task);
ef8693eb 2974 cancel_work_sync(&adapter->reset_task);
ed0e894d 2975 cancel_delayed_work_sync(&adapter->client_task);
5eae00c5
GR
2976 if (adapter->netdev_registered) {
2977 unregister_netdev(netdev);
2978 adapter->netdev_registered = false;
2979 }
ed0e894d
MW
2980 if (CLIENT_ALLOWED(adapter)) {
2981 err = i40evf_lan_del_device(adapter);
2982 if (err)
2983 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
2984 err);
2985 }
53d0b3ae 2986
f4a71881 2987 /* Shut down all the garbage mashers on the detention level */
5eae00c5 2988 adapter->state = __I40EVF_REMOVE;
f4a71881 2989 adapter->aq_required = 0;
f4a71881 2990 i40evf_request_reset(adapter);
22ead37f 2991 msleep(50);
f4a71881
MW
2992 /* If the FW isn't responding, kick it once, but only once. */
2993 if (!i40evf_asq_done(hw)) {
2994 i40evf_request_reset(adapter);
22ead37f 2995 msleep(50);
f4a71881 2996 }
8a68badd
MW
2997 i40evf_free_all_tx_resources(adapter);
2998 i40evf_free_all_rx_resources(adapter);
ef4603e8
JK
2999 i40evf_misc_irq_disable(adapter);
3000 i40evf_free_misc_irq(adapter);
3001 i40evf_reset_interrupt_capability(adapter);
3002 i40evf_free_q_vectors(adapter);
5eae00c5 3003
e5d17c3e
MW
3004 if (adapter->watchdog_timer.function)
3005 del_timer_sync(&adapter->watchdog_timer);
3006
dbb01c8a
MW
3007 flush_scheduled_work();
3008
43a3d9ba 3009 i40evf_free_rss(adapter);
66f9af85 3010
5eae00c5
GR
3011 if (hw->aq.asq.count)
3012 i40evf_shutdown_adminq(hw);
3013
8ddb3326
JB
3014 /* destroy the locks only once, here */
3015 mutex_destroy(&hw->aq.arq_mutex);
3016 mutex_destroy(&hw->aq.asq_mutex);
3017
5eae00c5
GR
3018 iounmap(hw->hw_addr);
3019 pci_release_regions(pdev);
e284fc88
MW
3020 i40evf_free_all_tx_resources(adapter);
3021 i40evf_free_all_rx_resources(adapter);
5eae00c5
GR
3022 i40evf_free_queues(adapter);
3023 kfree(adapter->vf_res);
6ba36a24
MW
3024 /* If we got removed before an up/down sequence, we've got a filter
3025 * hanging out there that we need to get rid of.
3026 */
3027 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3028 list_del(&f->list);
3029 kfree(f);
3030 }
37dfdf37
MW
3031 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3032 list_del(&f->list);
3033 kfree(f);
3034 }
5eae00c5
GR
3035
3036 free_netdev(netdev);
3037
3038 pci_disable_pcie_error_reporting(pdev);
3039
3040 pci_disable_device(pdev);
3041}
3042
3043static struct pci_driver i40evf_driver = {
3044 .name = i40evf_driver_name,
3045 .id_table = i40evf_pci_tbl,
3046 .probe = i40evf_probe,
3047 .remove = i40evf_remove,
3048#ifdef CONFIG_PM
3049 .suspend = i40evf_suspend,
3050 .resume = i40evf_resume,
3051#endif
3052 .shutdown = i40evf_shutdown,
3053};
3054
3055/**
3056 * i40e_init_module - Driver Registration Routine
3057 *
3058 * i40e_init_module is the first routine called when the driver is
3059 * loaded. All it does is register with the PCI subsystem.
3060 **/
3061static int __init i40evf_init_module(void)
3062{
3063 int ret;
75a64435 3064
5eae00c5 3065 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
75a64435 3066 i40evf_driver_version);
5eae00c5
GR
3067
3068 pr_info("%s\n", i40evf_copyright);
3069
6992a6c9
JK
3070 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3071 i40evf_driver_name);
2803b16c
JB
3072 if (!i40evf_wq) {
3073 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3074 return -ENOMEM;
3075 }
5eae00c5
GR
3076 ret = pci_register_driver(&i40evf_driver);
3077 return ret;
3078}
3079
3080module_init(i40evf_init_module);
3081
3082/**
3083 * i40e_exit_module - Driver Exit Cleanup Routine
3084 *
3085 * i40e_exit_module is called just before the driver is removed
3086 * from memory.
3087 **/
3088static void __exit i40evf_exit_module(void)
3089{
3090 pci_unregister_driver(&i40evf_driver);
2803b16c 3091 destroy_workqueue(i40evf_wq);
5eae00c5
GR
3092}
3093
3094module_exit(i40evf_exit_module);
3095
3096/* i40evf_main.c */