i40e/i40evf: Drop useless "IN_NETPOLL" flag
[deliverable/linux.git] / drivers / net / ethernet / intel / i40evf / i40evf_main.c
CommitLineData
5eae00c5
GR
1/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
5b8eb176 4 * Copyright(c) 2013 - 2015 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"
29static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
30static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
31static int i40evf_close(struct net_device *netdev);
32
33char i40evf_driver_name[] = "i40evf";
34static const char i40evf_driver_string[] =
0af56f44 35 "Intel(R) XL710/X710 Virtual Function Network Driver";
5eae00c5 36
164f7393 37#define DRV_VERSION "1.3.19"
5eae00c5
GR
38const char i40evf_driver_version[] = DRV_VERSION;
39static const char i40evf_copyright[] =
bf41846e 40 "Copyright (c) 2013 - 2015 Intel Corporation.";
5eae00c5
GR
41
42/* i40evf_pci_tbl - PCI Device ID Table
43 *
44 * Wildcard entries (PCI_ANY_ID) should come last
45 * Last entry must be all 0s
46 *
47 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
48 * Class, Class Mask, private data (not used) }
49 */
9baa3c34 50static const struct pci_device_id i40evf_pci_tbl[] = {
ab60085e 51 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
87e6c1d7 52 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
5eae00c5
GR
53 /* required last entry */
54 {0, }
55};
56
57MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
58
59MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
60MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
61MODULE_LICENSE("GPL");
62MODULE_VERSION(DRV_VERSION);
63
64/**
65 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
66 * @hw: pointer to the HW structure
67 * @mem: ptr to mem struct to fill out
68 * @size: size of memory requested
69 * @alignment: what to align the allocation to
70 **/
71i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
72 struct i40e_dma_mem *mem,
73 u64 size, u32 alignment)
74{
75 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
76
77 if (!mem)
78 return I40E_ERR_PARAM;
79
80 mem->size = ALIGN(size, alignment);
81 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
82 (dma_addr_t *)&mem->pa, GFP_KERNEL);
83 if (mem->va)
84 return 0;
85 else
86 return I40E_ERR_NO_MEMORY;
87}
88
89/**
90 * i40evf_free_dma_mem_d - OS specific memory free for shared code
91 * @hw: pointer to the HW structure
92 * @mem: ptr to mem struct to free
93 **/
94i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
95{
96 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
97
98 if (!mem || !mem->va)
99 return I40E_ERR_PARAM;
100 dma_free_coherent(&adapter->pdev->dev, mem->size,
101 mem->va, (dma_addr_t)mem->pa);
102 return 0;
103}
104
105/**
106 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
107 * @hw: pointer to the HW structure
108 * @mem: ptr to mem struct to fill out
109 * @size: size of memory requested
110 **/
111i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
112 struct i40e_virt_mem *mem, u32 size)
113{
114 if (!mem)
115 return I40E_ERR_PARAM;
116
117 mem->size = size;
118 mem->va = kzalloc(size, GFP_KERNEL);
119
120 if (mem->va)
121 return 0;
122 else
123 return I40E_ERR_NO_MEMORY;
124}
125
126/**
127 * i40evf_free_virt_mem_d - OS specific memory free for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to free
130 **/
131i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 /* it's ok to kfree a NULL pointer */
138 kfree(mem->va);
139
140 return 0;
141}
142
143/**
144 * i40evf_debug_d - OS dependent version of debug printing
145 * @hw: pointer to the HW structure
146 * @mask: debug level mask
147 * @fmt_str: printf-type format description
148 **/
149void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
150{
151 char buf[512];
152 va_list argptr;
153
154 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
155 return;
156
157 va_start(argptr, fmt_str);
158 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
159 va_end(argptr);
160
161 /* the debug string is already formatted with a newline */
162 pr_info("%s", buf);
163}
164
165/**
166 * i40evf_tx_timeout - Respond to a Tx Hang
167 * @netdev: network interface device structure
168 **/
169static void i40evf_tx_timeout(struct net_device *netdev)
170{
171 struct i40evf_adapter *adapter = netdev_priv(netdev);
172
173 adapter->tx_timeout_count++;
67c818a1
MW
174 if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
175 I40EVF_FLAG_RESET_NEEDED))) {
3526d800 176 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
625777e3
MW
177 schedule_work(&adapter->reset_task);
178 }
5eae00c5
GR
179}
180
181/**
182 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
183 * @adapter: board private structure
184 **/
185static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
186{
187 struct i40e_hw *hw = &adapter->hw;
75a64435 188
5eae00c5
GR
189 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
190
191 /* read flush */
192 rd32(hw, I40E_VFGEN_RSTAT);
193
194 synchronize_irq(adapter->msix_entries[0].vector);
195}
196
197/**
198 * i40evf_misc_irq_enable - Enable default interrupt generation settings
199 * @adapter: board private structure
200 **/
201static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
202{
203 struct i40e_hw *hw = &adapter->hw;
75a64435 204
5eae00c5
GR
205 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
206 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
b1f3366b 207 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
5eae00c5
GR
208
209 /* read flush */
210 rd32(hw, I40E_VFGEN_RSTAT);
211}
212
213/**
214 * i40evf_irq_disable - Mask off interrupt generation on the NIC
215 * @adapter: board private structure
216 **/
217static void i40evf_irq_disable(struct i40evf_adapter *adapter)
218{
219 int i;
220 struct i40e_hw *hw = &adapter->hw;
221
dbb01c8a
MW
222 if (!adapter->msix_entries)
223 return;
224
5eae00c5
GR
225 for (i = 1; i < adapter->num_msix_vectors; i++) {
226 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
227 synchronize_irq(adapter->msix_entries[i].vector);
228 }
229 /* read flush */
230 rd32(hw, I40E_VFGEN_RSTAT);
5eae00c5
GR
231}
232
233/**
234 * i40evf_irq_enable_queues - Enable interrupt for specified queues
235 * @adapter: board private structure
236 * @mask: bitmap of queues to enable
237 **/
238void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
239{
240 struct i40e_hw *hw = &adapter->hw;
241 int i;
242
243 for (i = 1; i < adapter->num_msix_vectors; i++) {
41a1d04b 244 if (mask & BIT(i - 1)) {
5eae00c5
GR
245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
97bf75f1 247 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
b1f3366b 248 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
5eae00c5
GR
249 }
250 }
251}
252
253/**
254 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
255 * @adapter: board private structure
256 * @mask: bitmap of vectors to trigger
257 **/
75a64435 258static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
5eae00c5
GR
259{
260 struct i40e_hw *hw = &adapter->hw;
261 int i;
262 uint32_t dyn_ctl;
263
164ec1bf
MW
264 if (mask & 1) {
265 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
b1f3366b 266 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
97bf75f1 267 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
b1f3366b 268 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
164ec1bf
MW
269 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
270 }
5eae00c5 271 for (i = 1; i < adapter->num_msix_vectors; i++) {
41a1d04b 272 if (mask & BIT(i)) {
5eae00c5 273 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
b1f3366b 274 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
97bf75f1 275 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
b1f3366b 276 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
5eae00c5
GR
277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
278 }
279 }
280}
281
282/**
283 * i40evf_irq_enable - Enable default interrupt generation settings
284 * @adapter: board private structure
285 **/
286void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
287{
288 struct i40e_hw *hw = &adapter->hw;
289
164ec1bf 290 i40evf_misc_irq_enable(adapter);
5eae00c5
GR
291 i40evf_irq_enable_queues(adapter, ~0);
292
293 if (flush)
294 rd32(hw, I40E_VFGEN_RSTAT);
295}
296
297/**
298 * i40evf_msix_aq - Interrupt handler for vector 0
299 * @irq: interrupt number
300 * @data: pointer to netdev
301 **/
302static irqreturn_t i40evf_msix_aq(int irq, void *data)
303{
304 struct net_device *netdev = data;
305 struct i40evf_adapter *adapter = netdev_priv(netdev);
306 struct i40e_hw *hw = &adapter->hw;
307 u32 val;
308 u32 ena_mask;
309
310 /* handle non-queue interrupts */
311 val = rd32(hw, I40E_VFINT_ICR01);
312 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
313
314
315 val = rd32(hw, I40E_VFINT_DYN_CTL01);
b1f3366b 316 val = val | I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
5eae00c5
GR
317 wr32(hw, I40E_VFINT_DYN_CTL01, val);
318
5eae00c5
GR
319 /* schedule work on the private workqueue */
320 schedule_work(&adapter->adminq_task);
321
322 return IRQ_HANDLED;
323}
324
325/**
326 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
327 * @irq: interrupt number
328 * @data: pointer to a q_vector
329 **/
330static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
331{
332 struct i40e_q_vector *q_vector = data;
333
334 if (!q_vector->tx.ring && !q_vector->rx.ring)
335 return IRQ_HANDLED;
336
337 napi_schedule(&q_vector->napi);
338
339 return IRQ_HANDLED;
340}
341
342/**
343 * i40evf_map_vector_to_rxq - associate irqs with rx queues
344 * @adapter: board private structure
345 * @v_idx: interrupt number
346 * @r_idx: queue number
347 **/
348static void
349i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
350{
351 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
352 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
353
354 rx_ring->q_vector = q_vector;
355 rx_ring->next = q_vector->rx.ring;
356 rx_ring->vsi = &adapter->vsi;
357 q_vector->rx.ring = rx_ring;
358 q_vector->rx.count++;
359 q_vector->rx.latency_range = I40E_LOW_LATENCY;
360}
361
362/**
363 * i40evf_map_vector_to_txq - associate irqs with tx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @t_idx: queue number
367 **/
368static void
369i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
370{
371 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
372 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
373
374 tx_ring->q_vector = q_vector;
375 tx_ring->next = q_vector->tx.ring;
376 tx_ring->vsi = &adapter->vsi;
377 q_vector->tx.ring = tx_ring;
378 q_vector->tx.count++;
379 q_vector->tx.latency_range = I40E_LOW_LATENCY;
380 q_vector->num_ringpairs++;
41a1d04b 381 q_vector->ring_mask |= BIT(t_idx);
5eae00c5
GR
382}
383
384/**
385 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
386 * @adapter: board private structure to initialize
387 *
388 * This function maps descriptor rings to the queue-specific vectors
389 * we were allotted through the MSI-X enabling code. Ideally, we'd have
390 * one vector per ring/queue, but on a constrained vector budget, we
391 * group the rings as "efficiently" as possible. You would add new
392 * mapping configurations in here.
393 **/
394static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
395{
396 int q_vectors;
397 int v_start = 0;
398 int rxr_idx = 0, txr_idx = 0;
cc052927
MW
399 int rxr_remaining = adapter->num_active_queues;
400 int txr_remaining = adapter->num_active_queues;
5eae00c5
GR
401 int i, j;
402 int rqpv, tqpv;
403 int err = 0;
404
405 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
406
407 /* The ideal configuration...
408 * We have enough vectors to map one per queue.
409 */
973371da 410 if (q_vectors >= (rxr_remaining * 2)) {
5eae00c5
GR
411 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
412 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
413
414 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
415 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
416 goto out;
417 }
418
419 /* If we don't have enough vectors for a 1-to-1
420 * mapping, we'll have to group them so there are
421 * multiple queues per vector.
422 * Re-adjusting *qpv takes care of the remainder.
423 */
424 for (i = v_start; i < q_vectors; i++) {
425 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
426 for (j = 0; j < rqpv; j++) {
427 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
428 rxr_idx++;
429 rxr_remaining--;
430 }
431 }
432 for (i = v_start; i < q_vectors; i++) {
433 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
434 for (j = 0; j < tqpv; j++) {
435 i40evf_map_vector_to_txq(adapter, i, txr_idx);
436 txr_idx++;
437 txr_remaining--;
438 }
439 }
440
441out:
442 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
443
444 return err;
445}
446
447/**
448 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
449 * @adapter: board private structure
450 *
451 * Allocates MSI-X vectors for tx and rx handling, and requests
452 * interrupts from the kernel.
453 **/
454static int
455i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
456{
457 int vector, err, q_vectors;
458 int rx_int_idx = 0, tx_int_idx = 0;
459
460 i40evf_irq_disable(adapter);
461 /* Decrement for Other and TCP Timer vectors */
462 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
463
464 for (vector = 0; vector < q_vectors; vector++) {
465 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
466
467 if (q_vector->tx.ring && q_vector->rx.ring) {
468 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
469 "i40evf-%s-%s-%d", basename,
470 "TxRx", rx_int_idx++);
471 tx_int_idx++;
472 } else if (q_vector->rx.ring) {
473 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
474 "i40evf-%s-%s-%d", basename,
475 "rx", rx_int_idx++);
476 } else if (q_vector->tx.ring) {
477 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
478 "i40evf-%s-%s-%d", basename,
479 "tx", tx_int_idx++);
480 } else {
481 /* skip this unused q_vector */
482 continue;
483 }
484 err = request_irq(
485 adapter->msix_entries[vector + NONQ_VECS].vector,
486 i40evf_msix_clean_rings,
487 0,
488 q_vector->name,
489 q_vector);
490 if (err) {
491 dev_info(&adapter->pdev->dev,
fb43201f 492 "Request_irq failed, error: %d\n", err);
5eae00c5
GR
493 goto free_queue_irqs;
494 }
495 /* assign the mask for this irq */
496 irq_set_affinity_hint(
497 adapter->msix_entries[vector + NONQ_VECS].vector,
498 q_vector->affinity_mask);
499 }
500
501 return 0;
502
503free_queue_irqs:
504 while (vector) {
505 vector--;
506 irq_set_affinity_hint(
507 adapter->msix_entries[vector + NONQ_VECS].vector,
508 NULL);
509 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
510 adapter->q_vector[vector]);
511 }
512 return err;
513}
514
515/**
516 * i40evf_request_misc_irq - Initialize MSI-X interrupts
517 * @adapter: board private structure
518 *
519 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
520 * vector is only for the admin queue, and stays active even when the netdev
521 * is closed.
522 **/
523static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
524{
525 struct net_device *netdev = adapter->netdev;
526 int err;
527
b39c1e2c 528 snprintf(adapter->misc_vector_name,
9a21a007
CW
529 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
530 dev_name(&adapter->pdev->dev));
5eae00c5 531 err = request_irq(adapter->msix_entries[0].vector,
e1dfee8e
MW
532 &i40evf_msix_aq, 0,
533 adapter->misc_vector_name, netdev);
5eae00c5
GR
534 if (err) {
535 dev_err(&adapter->pdev->dev,
77fa28be
CS
536 "request_irq for %s failed: %d\n",
537 adapter->misc_vector_name, err);
5eae00c5
GR
538 free_irq(adapter->msix_entries[0].vector, netdev);
539 }
540 return err;
541}
542
543/**
544 * i40evf_free_traffic_irqs - Free MSI-X interrupts
545 * @adapter: board private structure
546 *
547 * Frees all MSI-X vectors other than 0.
548 **/
549static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
550{
551 int i;
552 int q_vectors;
75a64435 553
5eae00c5
GR
554 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
555
556 for (i = 0; i < q_vectors; i++) {
557 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
558 NULL);
559 free_irq(adapter->msix_entries[i+1].vector,
560 adapter->q_vector[i]);
561 }
562}
563
564/**
565 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
566 * @adapter: board private structure
567 *
568 * Frees MSI-X vector 0.
569 **/
570static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
571{
572 struct net_device *netdev = adapter->netdev;
573
574 free_irq(adapter->msix_entries[0].vector, netdev);
575}
576
577/**
578 * i40evf_configure_tx - Configure Transmit Unit after Reset
579 * @adapter: board private structure
580 *
581 * Configure the Tx unit of the MAC after a reset.
582 **/
583static void i40evf_configure_tx(struct i40evf_adapter *adapter)
584{
585 struct i40e_hw *hw = &adapter->hw;
586 int i;
75a64435 587
cc052927 588 for (i = 0; i < adapter->num_active_queues; i++)
5eae00c5
GR
589 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
590}
591
592/**
593 * i40evf_configure_rx - Configure Receive Unit after Reset
594 * @adapter: board private structure
595 *
596 * Configure the Rx unit of the MAC after a reset.
597 **/
598static void i40evf_configure_rx(struct i40evf_adapter *adapter)
599{
600 struct i40e_hw *hw = &adapter->hw;
601 struct net_device *netdev = adapter->netdev;
602 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
603 int i;
604 int rx_buf_len;
605
606
607 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
608 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
609
610 /* Decide whether to use packet split mode or not */
611 if (netdev->mtu > ETH_DATA_LEN) {
612 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
613 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
614 else
615 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
616 } else {
617 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
618 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
619 else
620 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
621 }
622
623 /* Set the RX buffer length according to the mode */
624 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
625 rx_buf_len = I40E_RX_HDR_SIZE;
626 } else {
627 if (netdev->mtu <= ETH_DATA_LEN)
628 rx_buf_len = I40EVF_RXBUFFER_2048;
629 else
630 rx_buf_len = ALIGN(max_frame, 1024);
631 }
632
cc052927 633 for (i = 0; i < adapter->num_active_queues; i++) {
5eae00c5
GR
634 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
635 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
636 }
637}
638
639/**
640 * i40evf_find_vlan - Search filter list for specific vlan filter
641 * @adapter: board private structure
642 * @vlan: vlan tag
643 *
644 * Returns ptr to the filter object or NULL
645 **/
646static struct
647i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
648{
649 struct i40evf_vlan_filter *f;
650
651 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
652 if (vlan == f->vlan)
653 return f;
654 }
655 return NULL;
656}
657
658/**
659 * i40evf_add_vlan - Add a vlan filter to the list
660 * @adapter: board private structure
661 * @vlan: VLAN tag
662 *
663 * Returns ptr to the filter object or NULL when no memory available.
664 **/
665static struct
666i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
667{
13acb546
MW
668 struct i40evf_vlan_filter *f = NULL;
669 int count = 50;
670
671 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
672 &adapter->crit_section)) {
673 udelay(1);
674 if (--count == 0)
675 goto out;
676 }
5eae00c5
GR
677
678 f = i40evf_find_vlan(adapter, vlan);
348d4994 679 if (!f) {
5eae00c5 680 f = kzalloc(sizeof(*f), GFP_ATOMIC);
348d4994 681 if (!f)
13acb546 682 goto clearout;
249c8b8d 683
5eae00c5
GR
684 f->vlan = vlan;
685
686 INIT_LIST_HEAD(&f->list);
687 list_add(&f->list, &adapter->vlan_filter_list);
688 f->add = true;
689 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
690 }
691
13acb546
MW
692clearout:
693 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
694out:
5eae00c5
GR
695 return f;
696}
697
698/**
699 * i40evf_del_vlan - Remove a vlan filter from the list
700 * @adapter: board private structure
701 * @vlan: VLAN tag
702 **/
703static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
704{
705 struct i40evf_vlan_filter *f;
13acb546
MW
706 int count = 50;
707
708 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
709 &adapter->crit_section)) {
710 udelay(1);
711 if (--count == 0)
712 return;
713 }
5eae00c5
GR
714
715 f = i40evf_find_vlan(adapter, vlan);
716 if (f) {
717 f->remove = true;
718 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
719 }
13acb546 720 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
5eae00c5
GR
721}
722
723/**
724 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
725 * @netdev: network device struct
726 * @vid: VLAN tag
727 **/
728static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
75a64435 729 __always_unused __be16 proto, u16 vid)
5eae00c5
GR
730{
731 struct i40evf_adapter *adapter = netdev_priv(netdev);
732
8ed995ff
MW
733 if (!VLAN_ALLOWED(adapter))
734 return -EIO;
5eae00c5
GR
735 if (i40evf_add_vlan(adapter, vid) == NULL)
736 return -ENOMEM;
737 return 0;
738}
739
740/**
741 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
742 * @netdev: network device struct
743 * @vid: VLAN tag
744 **/
745static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
75a64435 746 __always_unused __be16 proto, u16 vid)
5eae00c5
GR
747{
748 struct i40evf_adapter *adapter = netdev_priv(netdev);
749
8ed995ff
MW
750 if (VLAN_ALLOWED(adapter)) {
751 i40evf_del_vlan(adapter, vid);
752 return 0;
753 }
754 return -EIO;
5eae00c5
GR
755}
756
757/**
758 * i40evf_find_filter - Search filter list for specific mac filter
759 * @adapter: board private structure
760 * @macaddr: the MAC address
761 *
762 * Returns ptr to the filter object or NULL
763 **/
764static struct
765i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
766 u8 *macaddr)
767{
768 struct i40evf_mac_filter *f;
769
770 if (!macaddr)
771 return NULL;
772
773 list_for_each_entry(f, &adapter->mac_filter_list, list) {
774 if (ether_addr_equal(macaddr, f->macaddr))
775 return f;
776 }
777 return NULL;
778}
779
780/**
781 * i40e_add_filter - Add a mac filter to the filter list
782 * @adapter: board private structure
783 * @macaddr: the MAC address
784 *
785 * Returns ptr to the filter object or NULL when no memory available.
786 **/
787static struct
788i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
789 u8 *macaddr)
790{
791 struct i40evf_mac_filter *f;
54e16f64 792 int count = 50;
5eae00c5
GR
793
794 if (!macaddr)
795 return NULL;
796
797 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
54e16f64 798 &adapter->crit_section)) {
b65476cd 799 udelay(1);
54e16f64
MW
800 if (--count == 0)
801 return NULL;
802 }
5eae00c5
GR
803
804 f = i40evf_find_filter(adapter, macaddr);
348d4994 805 if (!f) {
5eae00c5 806 f = kzalloc(sizeof(*f), GFP_ATOMIC);
348d4994 807 if (!f) {
5eae00c5
GR
808 clear_bit(__I40EVF_IN_CRITICAL_TASK,
809 &adapter->crit_section);
810 return NULL;
811 }
812
9a173901 813 ether_addr_copy(f->macaddr, macaddr);
5eae00c5
GR
814
815 list_add(&f->list, &adapter->mac_filter_list);
816 f->add = true;
817 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
818 }
819
820 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
821 return f;
822}
823
824/**
825 * i40evf_set_mac - NDO callback to set port mac address
826 * @netdev: network interface device structure
827 * @p: pointer to an address structure
828 *
829 * Returns 0 on success, negative on failure
830 **/
831static int i40evf_set_mac(struct net_device *netdev, void *p)
832{
833 struct i40evf_adapter *adapter = netdev_priv(netdev);
834 struct i40e_hw *hw = &adapter->hw;
835 struct i40evf_mac_filter *f;
836 struct sockaddr *addr = p;
837
838 if (!is_valid_ether_addr(addr->sa_data))
839 return -EADDRNOTAVAIL;
840
841 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
842 return 0;
843
14e52ee2
MW
844 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
845 return -EPERM;
846
847 f = i40evf_find_filter(adapter, hw->mac.addr);
848 if (f) {
849 f->remove = true;
850 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
851 }
852
5eae00c5
GR
853 f = i40evf_add_filter(adapter, addr->sa_data);
854 if (f) {
9a173901
GR
855 ether_addr_copy(hw->mac.addr, addr->sa_data);
856 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
5eae00c5
GR
857 }
858
859 return (f == NULL) ? -ENOMEM : 0;
860}
861
862/**
863 * i40evf_set_rx_mode - NDO callback to set the netdev filters
864 * @netdev: network interface device structure
865 **/
866static void i40evf_set_rx_mode(struct net_device *netdev)
867{
868 struct i40evf_adapter *adapter = netdev_priv(netdev);
869 struct i40evf_mac_filter *f, *ftmp;
870 struct netdev_hw_addr *uca;
871 struct netdev_hw_addr *mca;
2f41f335 872 struct netdev_hw_addr *ha;
54e16f64 873 int count = 50;
5eae00c5
GR
874
875 /* add addr if not already in the filter list */
876 netdev_for_each_uc_addr(uca, netdev) {
877 i40evf_add_filter(adapter, uca->addr);
878 }
879 netdev_for_each_mc_addr(mca, netdev) {
880 i40evf_add_filter(adapter, mca->addr);
881 }
882
883 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
54e16f64 884 &adapter->crit_section)) {
b65476cd 885 udelay(1);
54e16f64
MW
886 if (--count == 0) {
887 dev_err(&adapter->pdev->dev,
888 "Failed to get lock in %s\n", __func__);
889 return;
890 }
891 }
5eae00c5
GR
892 /* remove filter if not in netdev list */
893 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2f41f335
SN
894 netdev_for_each_mc_addr(mca, netdev)
895 if (ether_addr_equal(mca->addr, f->macaddr))
896 goto bottom_of_search_loop;
897
898 netdev_for_each_uc_addr(uca, netdev)
899 if (ether_addr_equal(uca->addr, f->macaddr))
900 goto bottom_of_search_loop;
901
902 for_each_dev_addr(netdev, ha)
903 if (ether_addr_equal(ha->addr, f->macaddr))
904 goto bottom_of_search_loop;
905
906 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
907 goto bottom_of_search_loop;
908
909 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
910 f->remove = true;
911 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
912
913bottom_of_search_loop:
914 continue;
5eae00c5
GR
915 }
916 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
917}
918
919/**
920 * i40evf_napi_enable_all - enable NAPI on all queue vectors
921 * @adapter: board private structure
922 **/
923static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
924{
925 int q_idx;
926 struct i40e_q_vector *q_vector;
927 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
928
929 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
930 struct napi_struct *napi;
75a64435 931
5eae00c5
GR
932 q_vector = adapter->q_vector[q_idx];
933 napi = &q_vector->napi;
934 napi_enable(napi);
935 }
936}
937
938/**
939 * i40evf_napi_disable_all - disable NAPI on all queue vectors
940 * @adapter: board private structure
941 **/
942static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
943{
944 int q_idx;
945 struct i40e_q_vector *q_vector;
946 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
947
948 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
949 q_vector = adapter->q_vector[q_idx];
950 napi_disable(&q_vector->napi);
951 }
952}
953
954/**
955 * i40evf_configure - set up transmit and receive data structures
956 * @adapter: board private structure
957 **/
958static void i40evf_configure(struct i40evf_adapter *adapter)
959{
960 struct net_device *netdev = adapter->netdev;
961 int i;
962
963 i40evf_set_rx_mode(netdev);
964
965 i40evf_configure_tx(adapter);
966 i40evf_configure_rx(adapter);
967 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
968
cc052927 969 for (i = 0; i < adapter->num_active_queues; i++) {
5eae00c5 970 struct i40e_ring *ring = adapter->rx_rings[i];
75a64435 971
a132af24 972 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
5eae00c5
GR
973 ring->next_to_use = ring->count - 1;
974 writel(ring->next_to_use, ring->tail);
975 }
976}
977
978/**
979 * i40evf_up_complete - Finish the last steps of bringing up a connection
980 * @adapter: board private structure
981 **/
982static int i40evf_up_complete(struct i40evf_adapter *adapter)
983{
984 adapter->state = __I40EVF_RUNNING;
985 clear_bit(__I40E_DOWN, &adapter->vsi.state);
986
987 i40evf_napi_enable_all(adapter);
988
989 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
990 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
991 return 0;
992}
993
5eae00c5
GR
994/**
995 * i40e_down - Shutdown the connection processing
996 * @adapter: board private structure
997 **/
998void i40evf_down(struct i40evf_adapter *adapter)
999{
1000 struct net_device *netdev = adapter->netdev;
1001 struct i40evf_mac_filter *f;
1002
ddf0b3a6
MW
1003 if (adapter->state == __I40EVF_DOWN)
1004 return;
1005
53d0b3ae
MW
1006 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1007 &adapter->crit_section))
1008 usleep_range(500, 1000);
1009
63e18c25
MW
1010 netif_carrier_off(netdev);
1011 netif_tx_disable(netdev);
748c434b 1012 i40evf_napi_disable_all(adapter);
63e18c25 1013 i40evf_irq_disable(adapter);
53d0b3ae 1014
ef8693eb 1015 /* remove all MAC filters */
5eae00c5
GR
1016 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1017 f->remove = true;
1018 }
ed1f5b58
MW
1019 /* remove all VLAN filters */
1020 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1021 f->remove = true;
1022 }
ef8693eb
MW
1023 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1024 adapter->state != __I40EVF_RESETTING) {
53d0b3ae
MW
1025 /* cancel any current operation */
1026 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
53d0b3ae
MW
1027 /* Schedule operations to close down the HW. Don't wait
1028 * here for this to complete. The watchdog is still running
1029 * and it will take care of this.
1030 */
1031 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
ed1f5b58 1032 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
ef8693eb 1033 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
ef8693eb 1034 }
5eae00c5 1035
53d0b3ae 1036 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
5eae00c5
GR
1037}
1038
1039/**
1040 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1041 * @adapter: board private structure
1042 * @vectors: number of vectors to request
1043 *
1044 * Work with the OS to set up the MSIX vectors needed.
1045 *
1046 * Returns 0 on success, negative on failure
1047 **/
1048static int
1049i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1050{
1051 int err, vector_threshold;
1052
1053 /* We'll want at least 3 (vector_threshold):
1054 * 0) Other (Admin Queue and link, mostly)
1055 * 1) TxQ[0] Cleanup
1056 * 2) RxQ[0] Cleanup
1057 */
1058 vector_threshold = MIN_MSIX_COUNT;
1059
1060 /* The more we get, the more we will assign to Tx/Rx Cleanup
1061 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1062 * Right now, we simply care about how many we'll get; we'll
1063 * set them up later while requesting irq's.
1064 */
fc2f2f5d
AG
1065 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1066 vector_threshold, vectors);
1067 if (err < 0) {
80e72893 1068 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
5eae00c5
GR
1069 kfree(adapter->msix_entries);
1070 adapter->msix_entries = NULL;
fc2f2f5d 1071 return err;
5eae00c5 1072 }
fc2f2f5d
AG
1073
1074 /* Adjust for only the vectors we'll use, which is minimum
1075 * of max_msix_q_vectors + NONQ_VECS, or the number of
1076 * vectors we were allocated.
1077 */
1078 adapter->num_msix_vectors = err;
1079 return 0;
5eae00c5
GR
1080}
1081
1082/**
1083 * i40evf_free_queues - Free memory for all rings
1084 * @adapter: board private structure to initialize
1085 *
1086 * Free all of the memory associated with queue pairs.
1087 **/
1088static void i40evf_free_queues(struct i40evf_adapter *adapter)
1089{
1090 int i;
1091
1092 if (!adapter->vsi_res)
1093 return;
cc052927 1094 for (i = 0; i < adapter->num_active_queues; i++) {
5eae00c5
GR
1095 if (adapter->tx_rings[i])
1096 kfree_rcu(adapter->tx_rings[i], rcu);
1097 adapter->tx_rings[i] = NULL;
1098 adapter->rx_rings[i] = NULL;
1099 }
1100}
1101
1102/**
1103 * i40evf_alloc_queues - Allocate memory for all rings
1104 * @adapter: board private structure to initialize
1105 *
1106 * We allocate one ring per queue at run-time since we don't know the
1107 * number of queues at compile-time. The polling_netdev array is
1108 * intended for Multiqueue, but should work fine with a single queue.
1109 **/
1110static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1111{
1112 int i;
1113
cc052927 1114 for (i = 0; i < adapter->num_active_queues; i++) {
5eae00c5
GR
1115 struct i40e_ring *tx_ring;
1116 struct i40e_ring *rx_ring;
1117
75a64435 1118 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
5eae00c5
GR
1119 if (!tx_ring)
1120 goto err_out;
1121
1122 tx_ring->queue_index = i;
1123 tx_ring->netdev = adapter->netdev;
1124 tx_ring->dev = &adapter->pdev->dev;
d732a184 1125 tx_ring->count = adapter->tx_desc_count;
1f012279
ASJ
1126 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1127 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
5eae00c5
GR
1128 adapter->tx_rings[i] = tx_ring;
1129
1130 rx_ring = &tx_ring[1];
1131 rx_ring->queue_index = i;
1132 rx_ring->netdev = adapter->netdev;
1133 rx_ring->dev = &adapter->pdev->dev;
d732a184 1134 rx_ring->count = adapter->rx_desc_count;
5eae00c5
GR
1135 adapter->rx_rings[i] = rx_ring;
1136 }
1137
1138 return 0;
1139
1140err_out:
1141 i40evf_free_queues(adapter);
1142 return -ENOMEM;
1143}
1144
1145/**
1146 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1147 * @adapter: board private structure to initialize
1148 *
1149 * Attempt to configure the interrupts using the best available
1150 * capabilities of the hardware and the kernel.
1151 **/
1152static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1153{
1154 int vector, v_budget;
1155 int pairs = 0;
1156 int err = 0;
1157
1158 if (!adapter->vsi_res) {
1159 err = -EIO;
1160 goto out;
1161 }
cc052927 1162 pairs = adapter->num_active_queues;
5eae00c5
GR
1163
1164 /* It's easy to be greedy for MSI-X vectors, but it really
1165 * doesn't do us much good if we have a lot more vectors
1166 * than CPU's. So let's be conservative and only ask for
1167 * (roughly) twice the number of vectors as there are CPU's.
1168 */
30a500e2
MW
1169 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1170 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
5eae00c5 1171
5eae00c5
GR
1172 adapter->msix_entries = kcalloc(v_budget,
1173 sizeof(struct msix_entry), GFP_KERNEL);
1174 if (!adapter->msix_entries) {
1175 err = -ENOMEM;
1176 goto out;
1177 }
1178
1179 for (vector = 0; vector < v_budget; vector++)
1180 adapter->msix_entries[vector].entry = vector;
1181
313ed2d5 1182 err = i40evf_acquire_msix_vectors(adapter, v_budget);
5eae00c5
GR
1183
1184out:
1185 adapter->netdev->real_num_tx_queues = pairs;
1186 return err;
1187}
1188
e25d00b8
ASJ
1189/**
1190 * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1191 * @vsi: vsi structure
1192 * @seed: RSS hash seed
1193 **/
1194static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1195{
1196 struct i40e_aqc_get_set_rss_key_data rss_key;
1197 struct i40evf_adapter *adapter = vsi->back;
1198 struct i40e_hw *hw = &adapter->hw;
1199 int ret = 0, i;
1200 u8 *rss_lut;
1201
1202 if (!vsi->id)
1203 return;
1204
1205 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1206 /* bail because we already have a command pending */
1207 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1208 adapter->current_op);
1209 return;
1210 }
1211
1212 memset(&rss_key, 0, sizeof(rss_key));
1213 memcpy(&rss_key, seed, sizeof(rss_key));
1214
1215 rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1216 if (!rss_lut)
1217 return;
1218
1219 /* Populate the LUT with max no. PF queues in round robin fashion */
1220 for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1221 rss_lut[i] = i % adapter->num_active_queues;
1222
1223 ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1224 if (ret) {
1225 dev_err(&adapter->pdev->dev,
1226 "Cannot set RSS key, err %s aq_err %s\n",
1227 i40evf_stat_str(hw, ret),
1228 i40evf_aq_str(hw, hw->aq.asq_last_status));
1229 return;
1230 }
1231
1232 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1233 (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1234 if (ret)
1235 dev_err(&adapter->pdev->dev,
1236 "Cannot set RSS lut, err %s aq_err %s\n",
1237 i40evf_stat_str(hw, ret),
1238 i40evf_aq_str(hw, hw->aq.asq_last_status));
1239}
1240
1241/**
1242 * i40e_configure_rss_reg - Prepare for RSS if used
1243 * @adapter: board private structure
1244 * @seed: RSS hash seed
1245 **/
1246static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1247 const u8 *seed)
1248{
1249 struct i40e_hw *hw = &adapter->hw;
1250 u32 *seed_dw = (u32 *)seed;
1251 u32 cqueue = 0;
1252 u32 lut = 0;
1253 int i, j;
1254
1255 /* Fill out hash function seed */
1256 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1257 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1258
1259 /* Populate the LUT with max no. PF queues in round robin fashion */
1260 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1261 lut = 0;
1262 for (j = 0; j < 4; j++) {
1263 if (cqueue == adapter->num_active_queues)
1264 cqueue = 0;
1265 lut |= ((cqueue) << (8 * j));
1266 cqueue++;
1267 }
1268 wr32(hw, I40E_VFQF_HLUT(i), lut);
1269 }
1270 i40e_flush(hw);
1271}
1272
1273/**
1274 * i40evf_configure_rss - Prepare for RSS
1275 * @adapter: board private structure
1276 **/
1277static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1278{
1279 struct i40e_hw *hw = &adapter->hw;
1280 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1281 u64 hena;
1282
1283 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1284
1285 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1286 hena = I40E_DEFAULT_RSS_HENA;
1287 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1288 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1289
1290 if (RSS_AQ(adapter))
1291 i40evf_configure_rss_aq(&adapter->vsi, seed);
1292 else
1293 i40evf_configure_rss_reg(adapter, seed);
1294}
1295
5eae00c5
GR
1296/**
1297 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1298 * @adapter: board private structure to initialize
1299 *
1300 * We allocate one q_vector per queue interrupt. If allocation fails we
1301 * return -ENOMEM.
1302 **/
1303static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1304{
1305 int q_idx, num_q_vectors;
1306 struct i40e_q_vector *q_vector;
1307
1308 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1309
1310 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
75a64435 1311 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
5eae00c5
GR
1312 if (!q_vector)
1313 goto err_out;
1314 q_vector->adapter = adapter;
1315 q_vector->vsi = &adapter->vsi;
1316 q_vector->v_idx = q_idx;
1317 netif_napi_add(adapter->netdev, &q_vector->napi,
75a64435 1318 i40evf_napi_poll, NAPI_POLL_WEIGHT);
5eae00c5
GR
1319 adapter->q_vector[q_idx] = q_vector;
1320 }
1321
1322 return 0;
1323
1324err_out:
1325 while (q_idx) {
1326 q_idx--;
1327 q_vector = adapter->q_vector[q_idx];
1328 netif_napi_del(&q_vector->napi);
1329 kfree(q_vector);
1330 adapter->q_vector[q_idx] = NULL;
1331 }
1332 return -ENOMEM;
1333}
1334
1335/**
1336 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1337 * @adapter: board private structure to initialize
1338 *
1339 * This function frees the memory allocated to the q_vectors. In addition if
1340 * NAPI is enabled it will delete any references to the NAPI struct prior
1341 * to freeing the q_vector.
1342 **/
1343static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1344{
1345 int q_idx, num_q_vectors;
1346 int napi_vectors;
1347
1348 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
cc052927 1349 napi_vectors = adapter->num_active_queues;
5eae00c5
GR
1350
1351 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1352 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1353
1354 adapter->q_vector[q_idx] = NULL;
1355 if (q_idx < napi_vectors)
1356 netif_napi_del(&q_vector->napi);
1357 kfree(q_vector);
1358 }
1359}
1360
1361/**
1362 * i40evf_reset_interrupt_capability - Reset MSIX setup
1363 * @adapter: board private structure
1364 *
1365 **/
1366void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1367{
1368 pci_disable_msix(adapter->pdev);
1369 kfree(adapter->msix_entries);
1370 adapter->msix_entries = NULL;
5eae00c5
GR
1371}
1372
1373/**
1374 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1375 * @adapter: board private structure to initialize
1376 *
1377 **/
1378int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1379{
1380 int err;
1381
1382 err = i40evf_set_interrupt_capability(adapter);
1383 if (err) {
1384 dev_err(&adapter->pdev->dev,
1385 "Unable to setup interrupt capabilities\n");
1386 goto err_set_interrupt;
1387 }
1388
1389 err = i40evf_alloc_q_vectors(adapter);
1390 if (err) {
1391 dev_err(&adapter->pdev->dev,
1392 "Unable to allocate memory for queue vectors\n");
1393 goto err_alloc_q_vectors;
1394 }
1395
1396 err = i40evf_alloc_queues(adapter);
1397 if (err) {
1398 dev_err(&adapter->pdev->dev,
1399 "Unable to allocate memory for queues\n");
1400 goto err_alloc_queues;
1401 }
1402
1403 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
75a64435
MW
1404 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1405 adapter->num_active_queues);
5eae00c5
GR
1406
1407 return 0;
1408err_alloc_queues:
1409 i40evf_free_q_vectors(adapter);
1410err_alloc_q_vectors:
1411 i40evf_reset_interrupt_capability(adapter);
1412err_set_interrupt:
1413 return err;
1414}
1415
1416/**
1417 * i40evf_watchdog_timer - Periodic call-back timer
1418 * @data: pointer to adapter disguised as unsigned long
1419 **/
1420static void i40evf_watchdog_timer(unsigned long data)
1421{
1422 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
75a64435 1423
5eae00c5
GR
1424 schedule_work(&adapter->watchdog_task);
1425 /* timer will be rescheduled in watchdog task */
1426}
1427
1428/**
1429 * i40evf_watchdog_task - Periodic call-back task
1430 * @work: pointer to work_struct
1431 **/
1432static void i40evf_watchdog_task(struct work_struct *work)
1433{
1434 struct i40evf_adapter *adapter = container_of(work,
75a64435
MW
1435 struct i40evf_adapter,
1436 watchdog_task);
5eae00c5 1437 struct i40e_hw *hw = &adapter->hw;
ee5c1e92 1438 u32 reg_val;
5eae00c5 1439
ef8693eb
MW
1440 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1441 goto restart_watchdog;
1442
1443 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
ee5c1e92
MW
1444 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1445 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1446 if ((reg_val == I40E_VFR_VFACTIVE) ||
1447 (reg_val == I40E_VFR_COMPLETED)) {
ef8693eb
MW
1448 /* A chance for redemption! */
1449 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1450 adapter->state = __I40EVF_STARTUP;
1451 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1452 schedule_delayed_work(&adapter->init_task, 10);
1453 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1454 &adapter->crit_section);
1455 /* Don't reschedule the watchdog, since we've restarted
1456 * the init task. When init_task contacts the PF and
1457 * gets everything set up again, it'll restart the
1458 * watchdog for us. Down, boy. Sit. Stay. Woof.
1459 */
1460 return;
1461 }
ef8693eb
MW
1462 adapter->aq_required = 0;
1463 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
5eae00c5 1464 goto watchdog_done;
ef8693eb 1465 }
5eae00c5 1466
ef8693eb
MW
1467 if ((adapter->state < __I40EVF_DOWN) ||
1468 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
5eae00c5
GR
1469 goto watchdog_done;
1470
ef8693eb 1471 /* check for reset */
ee5c1e92
MW
1472 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1473 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
5eae00c5 1474 adapter->state = __I40EVF_RESETTING;
ef8693eb 1475 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
249c8b8d 1476 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
5eae00c5 1477 schedule_work(&adapter->reset_task);
ef8693eb
MW
1478 adapter->aq_required = 0;
1479 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
1480 goto watchdog_done;
1481 }
1482
1483 /* Process admin queue tasks. After init, everything gets done
1484 * here so we don't race on the admin queue.
1485 */
ed636960 1486 if (adapter->current_op) {
0758e7cb
MW
1487 if (!i40evf_asq_done(hw)) {
1488 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1489 i40evf_send_api_ver(adapter);
1490 }
5eae00c5 1491 goto watchdog_done;
0758e7cb 1492 }
e6d038de
MW
1493 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1494 i40evf_send_vf_config_msg(adapter);
1495 goto watchdog_done;
1496 }
5eae00c5 1497
e284fc88
MW
1498 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1499 i40evf_disable_queues(adapter);
1500 goto watchdog_done;
1501 }
1502
5eae00c5
GR
1503 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1504 i40evf_map_queues(adapter);
1505 goto watchdog_done;
1506 }
1507
1508 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1509 i40evf_add_ether_addrs(adapter);
1510 goto watchdog_done;
1511 }
1512
1513 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1514 i40evf_add_vlans(adapter);
1515 goto watchdog_done;
1516 }
1517
1518 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1519 i40evf_del_ether_addrs(adapter);
1520 goto watchdog_done;
1521 }
1522
1523 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1524 i40evf_del_vlans(adapter);
1525 goto watchdog_done;
1526 }
1527
5eae00c5
GR
1528 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1529 i40evf_configure_queues(adapter);
1530 goto watchdog_done;
1531 }
1532
1533 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1534 i40evf_enable_queues(adapter);
1535 goto watchdog_done;
1536 }
1537
e25d00b8
ASJ
1538 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1539 /* This message goes straight to the firmware, not the
1540 * PF, so we don't have to set current_op as we will
1541 * not get a response through the ARQ.
1542 */
1543 i40evf_configure_rss(adapter);
1544 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1545 goto watchdog_done;
1546 }
1547
5eae00c5
GR
1548 if (adapter->state == __I40EVF_RUNNING)
1549 i40evf_request_stats(adapter);
5eae00c5 1550watchdog_done:
4870e176
MW
1551 if (adapter->state == __I40EVF_RUNNING) {
1552 i40evf_irq_enable_queues(adapter, ~0);
1553 i40evf_fire_sw_int(adapter, 0xFF);
1554 } else {
1555 i40evf_fire_sw_int(adapter, 0x1);
1556 }
1557
ef8693eb
MW
1558 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1559restart_watchdog:
d3e2edb7
AS
1560 if (adapter->state == __I40EVF_REMOVE)
1561 return;
5eae00c5
GR
1562 if (adapter->aq_required)
1563 mod_timer(&adapter->watchdog_timer,
1564 jiffies + msecs_to_jiffies(20));
1565 else
1566 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
5eae00c5
GR
1567 schedule_work(&adapter->adminq_task);
1568}
1569
67c818a1
MW
1570#define I40EVF_RESET_WAIT_MS 10
1571#define I40EVF_RESET_WAIT_COUNT 500
5eae00c5
GR
1572/**
1573 * i40evf_reset_task - Call-back task to handle hardware reset
1574 * @work: pointer to work_struct
1575 *
1576 * During reset we need to shut down and reinitialize the admin queue
1577 * before we can use it to communicate with the PF again. We also clear
1578 * and reinit the rings because that context is lost as well.
1579 **/
1580static void i40evf_reset_task(struct work_struct *work)
1581{
ef8693eb
MW
1582 struct i40evf_adapter *adapter = container_of(work,
1583 struct i40evf_adapter,
1584 reset_task);
ac833bbf 1585 struct net_device *netdev = adapter->netdev;
5eae00c5 1586 struct i40e_hw *hw = &adapter->hw;
ac833bbf 1587 struct i40evf_mac_filter *f;
ee5c1e92 1588 u32 reg_val;
ac833bbf 1589 int i = 0, err;
5eae00c5
GR
1590
1591 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1592 &adapter->crit_section))
f98a2006 1593 usleep_range(500, 1000);
3526d800 1594
67c818a1 1595 i40evf_misc_irq_disable(adapter);
3526d800 1596 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
67c818a1
MW
1597 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1598 /* Restart the AQ here. If we have been reset but didn't
1599 * detect it, or if the PF had to reinit, our AQ will be hosed.
1600 */
1601 i40evf_shutdown_adminq(hw);
1602 i40evf_init_adminq(hw);
3526d800
MW
1603 i40evf_request_reset(adapter);
1604 }
67c818a1 1605 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
3526d800 1606
ef8693eb
MW
1607 /* poll until we see the reset actually happen */
1608 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
ee5c1e92
MW
1609 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1610 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1611 if (!reg_val)
ef8693eb 1612 break;
ee5c1e92 1613 usleep_range(5000, 10000);
ef8693eb
MW
1614 }
1615 if (i == I40EVF_RESET_WAIT_COUNT) {
67c818a1 1616 dev_info(&adapter->pdev->dev, "Never saw reset\n");
ef8693eb
MW
1617 goto continue_reset; /* act like the reset happened */
1618 }
5eae00c5 1619
ef8693eb
MW
1620 /* wait until the reset is complete and the PF is responding to us */
1621 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
ee5c1e92
MW
1622 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1623 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1624 if (reg_val == I40E_VFR_VFACTIVE)
5eae00c5 1625 break;
c88e38cc 1626 msleep(I40EVF_RESET_WAIT_MS);
5eae00c5 1627 }
67c818a1
MW
1628 /* extra wait to make sure minimum wait is met */
1629 msleep(I40EVF_RESET_WAIT_MS);
ef8693eb 1630 if (i == I40EVF_RESET_WAIT_COUNT) {
874d1a10 1631 struct i40evf_mac_filter *ftmp;
6ba36a24
MW
1632 struct i40evf_vlan_filter *fv, *fvtmp;
1633
5eae00c5 1634 /* reset never finished */
80e72893 1635 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
ee5c1e92 1636 reg_val);
ef8693eb
MW
1637 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1638
169f4076
MW
1639 if (netif_running(adapter->netdev)) {
1640 set_bit(__I40E_DOWN, &adapter->vsi.state);
ac833bbf 1641 netif_carrier_off(netdev);
67c818a1
MW
1642 netif_tx_disable(netdev);
1643 i40evf_napi_disable_all(adapter);
1644 i40evf_irq_disable(adapter);
169f4076
MW
1645 i40evf_free_traffic_irqs(adapter);
1646 i40evf_free_all_tx_resources(adapter);
1647 i40evf_free_all_rx_resources(adapter);
1648 }
6ba36a24
MW
1649
1650 /* Delete all of the filters, both MAC and VLAN. */
1651 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1652 list) {
1653 list_del(&f->list);
1654 kfree(f);
1655 }
67c818a1 1656
6ba36a24
MW
1657 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1658 list) {
1659 list_del(&fv->list);
1660 kfree(fv);
1661 }
1662
ef8693eb
MW
1663 i40evf_free_misc_irq(adapter);
1664 i40evf_reset_interrupt_capability(adapter);
1665 i40evf_free_queues(adapter);
d31944d6 1666 i40evf_free_q_vectors(adapter);
ef8693eb
MW
1667 kfree(adapter->vf_res);
1668 i40evf_shutdown_adminq(hw);
1669 adapter->netdev->flags &= ~IFF_UP;
1670 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
67c818a1
MW
1671 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1672 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
ef8693eb 1673 return; /* Do not attempt to reinit. It's dead, Jim. */
5eae00c5 1674 }
ef8693eb
MW
1675
1676continue_reset:
3c8e0b98 1677 if (netif_running(adapter->netdev)) {
3c8e0b98 1678 netif_carrier_off(netdev);
67c818a1
MW
1679 netif_tx_stop_all_queues(netdev);
1680 i40evf_napi_disable_all(adapter);
3c8e0b98 1681 }
67c818a1 1682 i40evf_irq_disable(adapter);
ac833bbf 1683
5eae00c5 1684 adapter->state = __I40EVF_RESETTING;
67c818a1
MW
1685 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1686
1687 /* free the Tx/Rx rings and descriptors, might be better to just
1688 * re-use them sometime in the future
1689 */
1690 i40evf_free_all_rx_resources(adapter);
1691 i40evf_free_all_tx_resources(adapter);
5eae00c5
GR
1692
1693 /* kill and reinit the admin queue */
1694 if (i40evf_shutdown_adminq(hw))
ac833bbf
MW
1695 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1696 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
1697 err = i40evf_init_adminq(hw);
1698 if (err)
ac833bbf
MW
1699 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1700 err);
5eae00c5 1701
e6d038de
MW
1702 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1703 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
ac833bbf
MW
1704
1705 /* re-add all MAC filters */
1706 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1707 f->add = true;
1708 }
1709 /* re-add all VLAN filters */
1710 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1711 f->add = true;
1712 }
e6d038de 1713 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
ac833bbf 1714 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
5eae00c5 1715 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
67c818a1 1716 i40evf_misc_irq_enable(adapter);
5eae00c5
GR
1717
1718 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1719
1720 if (netif_running(adapter->netdev)) {
1721 /* allocate transmit descriptors */
1722 err = i40evf_setup_all_tx_resources(adapter);
1723 if (err)
1724 goto reset_err;
1725
1726 /* allocate receive descriptors */
1727 err = i40evf_setup_all_rx_resources(adapter);
1728 if (err)
1729 goto reset_err;
1730
1731 i40evf_configure(adapter);
1732
1733 err = i40evf_up_complete(adapter);
1734 if (err)
1735 goto reset_err;
1736
1737 i40evf_irq_enable(adapter, true);
67c818a1
MW
1738 } else {
1739 adapter->state = __I40EVF_DOWN;
5eae00c5 1740 }
67c818a1 1741
5eae00c5
GR
1742 return;
1743reset_err:
80e72893 1744 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
5eae00c5
GR
1745 i40evf_close(adapter->netdev);
1746}
1747
1748/**
1749 * i40evf_adminq_task - worker thread to clean the admin queue
1750 * @work: pointer to work_struct containing our data
1751 **/
1752static void i40evf_adminq_task(struct work_struct *work)
1753{
1754 struct i40evf_adapter *adapter =
1755 container_of(work, struct i40evf_adapter, adminq_task);
1756 struct i40e_hw *hw = &adapter->hw;
1757 struct i40e_arq_event_info event;
1758 struct i40e_virtchnl_msg *v_msg;
1759 i40e_status ret;
912257e5 1760 u32 val, oldval;
5eae00c5
GR
1761 u16 pending;
1762
ef8693eb 1763 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
7235448c 1764 goto out;
ef8693eb 1765
1001dc37
MW
1766 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1767 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
249c8b8d 1768 if (!event.msg_buf)
7235448c 1769 goto out;
249c8b8d 1770
5eae00c5
GR
1771 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1772 do {
1773 ret = i40evf_clean_arq_element(hw, &event, &pending);
8b011ebb 1774 if (ret || !v_msg->v_opcode)
5eae00c5
GR
1775 break; /* No event to process or error cleaning ARQ */
1776
1777 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1778 v_msg->v_retval, event.msg_buf,
1001dc37 1779 event.msg_len);
75a64435 1780 if (pending != 0)
5eae00c5 1781 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
5eae00c5
GR
1782 } while (pending);
1783
67c818a1
MW
1784 if ((adapter->flags &
1785 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1786 adapter->state == __I40EVF_RESETTING)
1787 goto freedom;
1788
912257e5
MW
1789 /* check for error indications */
1790 val = rd32(hw, hw->aq.arq.len);
1791 oldval = val;
b1f3366b 1792 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
912257e5 1793 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
b1f3366b 1794 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
912257e5 1795 }
b1f3366b 1796 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
912257e5 1797 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
b1f3366b 1798 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
912257e5 1799 }
b1f3366b 1800 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
912257e5 1801 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
b1f3366b 1802 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
912257e5
MW
1803 }
1804 if (oldval != val)
1805 wr32(hw, hw->aq.arq.len, val);
1806
1807 val = rd32(hw, hw->aq.asq.len);
1808 oldval = val;
b1f3366b 1809 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
912257e5 1810 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
b1f3366b 1811 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
912257e5 1812 }
b1f3366b 1813 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
912257e5 1814 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
b1f3366b 1815 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
912257e5 1816 }
b1f3366b 1817 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
912257e5 1818 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
b1f3366b 1819 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
912257e5
MW
1820 }
1821 if (oldval != val)
1822 wr32(hw, hw->aq.asq.len, val);
1823
67c818a1 1824freedom:
7235448c
MW
1825 kfree(event.msg_buf);
1826out:
5eae00c5
GR
1827 /* re-enable Admin queue interrupt cause */
1828 i40evf_misc_irq_enable(adapter);
5eae00c5
GR
1829}
1830
1831/**
1832 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1833 * @adapter: board private structure
1834 *
1835 * Free all transmit software resources
1836 **/
e284fc88 1837void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
5eae00c5
GR
1838{
1839 int i;
1840
cc052927 1841 for (i = 0; i < adapter->num_active_queues; i++)
5eae00c5
GR
1842 if (adapter->tx_rings[i]->desc)
1843 i40evf_free_tx_resources(adapter->tx_rings[i]);
5eae00c5
GR
1844}
1845
1846/**
1847 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1848 * @adapter: board private structure
1849 *
1850 * If this function returns with an error, then it's possible one or
1851 * more of the rings is populated (while the rest are not). It is the
1852 * callers duty to clean those orphaned rings.
1853 *
1854 * Return 0 on success, negative on failure
1855 **/
1856static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1857{
1858 int i, err = 0;
1859
cc052927 1860 for (i = 0; i < adapter->num_active_queues; i++) {
d732a184 1861 adapter->tx_rings[i]->count = adapter->tx_desc_count;
5eae00c5
GR
1862 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1863 if (!err)
1864 continue;
1865 dev_err(&adapter->pdev->dev,
fb43201f 1866 "Allocation for Tx Queue %u failed\n", i);
5eae00c5
GR
1867 break;
1868 }
1869
1870 return err;
1871}
1872
1873/**
1874 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1875 * @adapter: board private structure
1876 *
1877 * If this function returns with an error, then it's possible one or
1878 * more of the rings is populated (while the rest are not). It is the
1879 * callers duty to clean those orphaned rings.
1880 *
1881 * Return 0 on success, negative on failure
1882 **/
1883static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1884{
1885 int i, err = 0;
1886
cc052927 1887 for (i = 0; i < adapter->num_active_queues; i++) {
d732a184 1888 adapter->rx_rings[i]->count = adapter->rx_desc_count;
5eae00c5
GR
1889 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1890 if (!err)
1891 continue;
1892 dev_err(&adapter->pdev->dev,
fb43201f 1893 "Allocation for Rx Queue %u failed\n", i);
5eae00c5
GR
1894 break;
1895 }
1896 return err;
1897}
1898
1899/**
1900 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1901 * @adapter: board private structure
1902 *
1903 * Free all receive software resources
1904 **/
e284fc88 1905void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
5eae00c5
GR
1906{
1907 int i;
1908
cc052927 1909 for (i = 0; i < adapter->num_active_queues; i++)
5eae00c5
GR
1910 if (adapter->rx_rings[i]->desc)
1911 i40evf_free_rx_resources(adapter->rx_rings[i]);
1912}
1913
1914/**
1915 * i40evf_open - Called when a network interface is made active
1916 * @netdev: network interface device structure
1917 *
1918 * Returns 0 on success, negative value on failure
1919 *
1920 * The open entry point is called when a network interface is made
1921 * active by the system (IFF_UP). At this point all resources needed
1922 * for transmit and receive operations are allocated, the interrupt
1923 * handler is registered with the OS, the watchdog timer is started,
1924 * and the stack is notified that the interface is ready.
1925 **/
1926static int i40evf_open(struct net_device *netdev)
1927{
1928 struct i40evf_adapter *adapter = netdev_priv(netdev);
1929 int err;
1930
ef8693eb
MW
1931 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1932 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1933 return -EIO;
1934 }
e284fc88 1935 if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
5eae00c5
GR
1936 return -EBUSY;
1937
1938 /* allocate transmit descriptors */
1939 err = i40evf_setup_all_tx_resources(adapter);
1940 if (err)
1941 goto err_setup_tx;
1942
1943 /* allocate receive descriptors */
1944 err = i40evf_setup_all_rx_resources(adapter);
1945 if (err)
1946 goto err_setup_rx;
1947
1948 /* clear any pending interrupts, may auto mask */
1949 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1950 if (err)
1951 goto err_req_irq;
1952
44151cd3 1953 i40evf_add_filter(adapter, adapter->hw.mac.addr);
5eae00c5
GR
1954 i40evf_configure(adapter);
1955
1956 err = i40evf_up_complete(adapter);
1957 if (err)
1958 goto err_req_irq;
1959
1960 i40evf_irq_enable(adapter, true);
1961
1962 return 0;
1963
1964err_req_irq:
1965 i40evf_down(adapter);
1966 i40evf_free_traffic_irqs(adapter);
1967err_setup_rx:
1968 i40evf_free_all_rx_resources(adapter);
1969err_setup_tx:
1970 i40evf_free_all_tx_resources(adapter);
1971
1972 return err;
1973}
1974
1975/**
1976 * i40evf_close - Disables a network interface
1977 * @netdev: network interface device structure
1978 *
1979 * Returns 0, this is not allowed to fail
1980 *
1981 * The close entry point is called when an interface is de-activated
1982 * by the OS. The hardware is still under the drivers control, but
1983 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1984 * are freed, along with all transmit and receive resources.
1985 **/
1986static int i40evf_close(struct net_device *netdev)
1987{
1988 struct i40evf_adapter *adapter = netdev_priv(netdev);
1989
ef8693eb
MW
1990 if (adapter->state <= __I40EVF_DOWN)
1991 return 0;
1992
ef8693eb 1993
5eae00c5
GR
1994 set_bit(__I40E_DOWN, &adapter->vsi.state);
1995
1996 i40evf_down(adapter);
ddf0b3a6 1997 adapter->state = __I40EVF_DOWN;
5eae00c5
GR
1998 i40evf_free_traffic_irqs(adapter);
1999
5eae00c5
GR
2000 return 0;
2001}
2002
2003/**
2004 * i40evf_get_stats - Get System Network Statistics
2005 * @netdev: network interface device structure
2006 *
2007 * Returns the address of the device statistics structure.
2008 * The statistics are actually updated from the timer callback.
2009 **/
2010static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2011{
2012 struct i40evf_adapter *adapter = netdev_priv(netdev);
2013
2014 /* only return the current stats */
2015 return &adapter->net_stats;
2016}
2017
5eae00c5
GR
2018/**
2019 * i40evf_change_mtu - Change the Maximum Transfer Unit
2020 * @netdev: network interface device structure
2021 * @new_mtu: new value for maximum frame size
2022 *
2023 * Returns 0 on success, negative on failure
2024 **/
2025static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2026{
2027 struct i40evf_adapter *adapter = netdev_priv(netdev);
2028 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2029
2030 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2031 return -EINVAL;
2032
5eae00c5 2033 netdev->mtu = new_mtu;
67c818a1
MW
2034 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2035 schedule_work(&adapter->reset_task);
2036
5eae00c5
GR
2037 return 0;
2038}
2039
2040static const struct net_device_ops i40evf_netdev_ops = {
2041 .ndo_open = i40evf_open,
2042 .ndo_stop = i40evf_close,
2043 .ndo_start_xmit = i40evf_xmit_frame,
2044 .ndo_get_stats = i40evf_get_stats,
2045 .ndo_set_rx_mode = i40evf_set_rx_mode,
2046 .ndo_validate_addr = eth_validate_addr,
2047 .ndo_set_mac_address = i40evf_set_mac,
2048 .ndo_change_mtu = i40evf_change_mtu,
2049 .ndo_tx_timeout = i40evf_tx_timeout,
2050 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2051 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
2052};
2053
2054/**
2055 * i40evf_check_reset_complete - check that VF reset is complete
2056 * @hw: pointer to hw struct
2057 *
2058 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2059 **/
2060static int i40evf_check_reset_complete(struct i40e_hw *hw)
2061{
2062 u32 rstat;
2063 int i;
2064
2065 for (i = 0; i < 100; i++) {
fd35886a
AS
2066 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2067 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2068 if ((rstat == I40E_VFR_VFACTIVE) ||
2069 (rstat == I40E_VFR_COMPLETED))
5eae00c5 2070 return 0;
f98a2006 2071 usleep_range(10, 20);
5eae00c5
GR
2072 }
2073 return -EBUSY;
2074}
2075
e6d038de
MW
2076/**
2077 * i40evf_process_config - Process the config information we got from the PF
2078 * @adapter: board private structure
2079 *
2080 * Verify that we have a valid config struct, and set up our netdev features
2081 * and our VSI struct.
2082 **/
2083int i40evf_process_config(struct i40evf_adapter *adapter)
2084{
2085 struct net_device *netdev = adapter->netdev;
2086 int i;
2087
2088 /* got VF config message back from PF, now we can parse it */
2089 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2090 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2091 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2092 }
2093 if (!adapter->vsi_res) {
2094 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2095 return -ENODEV;
2096 }
2097
2098 if (adapter->vf_res->vf_offload_flags
2099 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2100 netdev->vlan_features = netdev->features;
2101 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2102 NETIF_F_HW_VLAN_CTAG_RX |
2103 NETIF_F_HW_VLAN_CTAG_FILTER;
2104 }
2105 netdev->features |= NETIF_F_HIGHDMA |
2106 NETIF_F_SG |
2107 NETIF_F_IP_CSUM |
2108 NETIF_F_SCTP_CSUM |
2109 NETIF_F_IPV6_CSUM |
2110 NETIF_F_TSO |
2111 NETIF_F_TSO6 |
2112 NETIF_F_RXCSUM |
2113 NETIF_F_GRO;
2114
2115 /* copy netdev features into list of user selectable features */
2116 netdev->hw_features |= netdev->features;
2117 netdev->hw_features &= ~NETIF_F_RXCSUM;
2118
2119 adapter->vsi.id = adapter->vsi_res->vsi_id;
2120
2121 adapter->vsi.back = adapter;
2122 adapter->vsi.base_vector = 1;
2123 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2124 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2125 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2126 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2127 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2128 adapter->vsi.netdev = adapter->netdev;
f578f5f4 2129 adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
e6d038de
MW
2130 return 0;
2131}
2132
5eae00c5
GR
2133/**
2134 * i40evf_init_task - worker thread to perform delayed initialization
2135 * @work: pointer to work_struct containing our data
2136 *
2137 * This task completes the work that was begun in probe. Due to the nature
2138 * of VF-PF communications, we may need to wait tens of milliseconds to get
dbedd44e 2139 * responses back from the PF. Rather than busy-wait in probe and bog down the
5eae00c5
GR
2140 * whole system, we'll do it in a task so we can sleep.
2141 * This task only runs during driver init. Once we've established
2142 * communications with the PF driver and set up our netdev, the watchdog
2143 * takes over.
2144 **/
2145static void i40evf_init_task(struct work_struct *work)
2146{
2147 struct i40evf_adapter *adapter = container_of(work,
2148 struct i40evf_adapter,
2149 init_task.work);
2150 struct net_device *netdev = adapter->netdev;
5eae00c5
GR
2151 struct i40e_hw *hw = &adapter->hw;
2152 struct pci_dev *pdev = adapter->pdev;
e6d038de 2153 int err, bufsz;
5eae00c5
GR
2154
2155 switch (adapter->state) {
2156 case __I40EVF_STARTUP:
2157 /* driver loaded, probe complete */
ef8693eb
MW
2158 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2159 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
5eae00c5
GR
2160 err = i40e_set_mac_type(hw);
2161 if (err) {
c2a137cb
MW
2162 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2163 err);
2619ef47 2164 goto err;
5eae00c5
GR
2165 }
2166 err = i40evf_check_reset_complete(hw);
2167 if (err) {
0d9c7ea8 2168 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
75a64435 2169 err);
5eae00c5
GR
2170 goto err;
2171 }
2172 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2173 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2174 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2175 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2176
2177 err = i40evf_init_adminq(hw);
2178 if (err) {
c2a137cb
MW
2179 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2180 err);
5eae00c5
GR
2181 goto err;
2182 }
2183 err = i40evf_send_api_ver(adapter);
2184 if (err) {
10bdd67b 2185 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
5eae00c5
GR
2186 i40evf_shutdown_adminq(hw);
2187 goto err;
2188 }
2189 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2190 goto restart;
5eae00c5 2191 case __I40EVF_INIT_VERSION_CHECK:
10bdd67b 2192 if (!i40evf_asq_done(hw)) {
80e72893 2193 dev_err(&pdev->dev, "Admin queue command never completed\n");
906a6937
MW
2194 i40evf_shutdown_adminq(hw);
2195 adapter->state = __I40EVF_STARTUP;
5eae00c5 2196 goto err;
10bdd67b 2197 }
5eae00c5
GR
2198
2199 /* aq msg sent, awaiting reply */
2200 err = i40evf_verify_api_ver(adapter);
2201 if (err) {
d4f82fd3 2202 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
56f9920a 2203 err = i40evf_send_api_ver(adapter);
ee1693e5
MW
2204 else
2205 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2206 adapter->pf_version.major,
2207 adapter->pf_version.minor,
2208 I40E_VIRTCHNL_VERSION_MAJOR,
2209 I40E_VIRTCHNL_VERSION_MINOR);
5eae00c5
GR
2210 goto err;
2211 }
2212 err = i40evf_send_vf_config_msg(adapter);
2213 if (err) {
3f2ab172 2214 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
5eae00c5
GR
2215 err);
2216 goto err;
2217 }
2218 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2219 goto restart;
5eae00c5
GR
2220 case __I40EVF_INIT_GET_RESOURCES:
2221 /* aq msg sent, awaiting reply */
2222 if (!adapter->vf_res) {
2223 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2224 (I40E_MAX_VF_VSI *
2225 sizeof(struct i40e_virtchnl_vsi_resource));
2226 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
c2a137cb 2227 if (!adapter->vf_res)
5eae00c5 2228 goto err;
5eae00c5
GR
2229 }
2230 err = i40evf_get_vf_config(adapter);
906a6937 2231 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
906a6937
MW
2232 err = i40evf_send_vf_config_msg(adapter);
2233 goto err;
2234 }
5eae00c5 2235 if (err) {
c2a137cb
MW
2236 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2237 err);
5eae00c5
GR
2238 goto err_alloc;
2239 }
2240 adapter->state = __I40EVF_INIT_SW;
2241 break;
2242 default:
2243 goto err_alloc;
2244 }
e6d038de 2245 if (i40evf_process_config(adapter))
5eae00c5 2246 goto err_alloc;
e6d038de 2247 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
2248
2249 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2250
5eae00c5
GR
2251 netdev->netdev_ops = &i40evf_netdev_ops;
2252 i40evf_set_ethtool_ops(netdev);
2253 netdev->watchdog_timeo = 5 * HZ;
3415e8ce 2254
5eae00c5 2255 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
b34f90e7 2256 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
c2a137cb 2257 adapter->hw.mac.addr);
14e52ee2
MW
2258 eth_hw_addr_random(netdev);
2259 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2260 } else {
2261 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2262 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2263 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
5eae00c5 2264 }
5eae00c5 2265
5eae00c5
GR
2266 init_timer(&adapter->watchdog_timer);
2267 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2268 adapter->watchdog_timer.data = (unsigned long)adapter;
2269 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2270
cc052927
MW
2271 adapter->num_active_queues = min_t(int,
2272 adapter->vsi_res->num_queue_pairs,
2273 (int)(num_online_cpus()));
d732a184
MW
2274 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2275 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
5eae00c5
GR
2276 err = i40evf_init_interrupt_scheme(adapter);
2277 if (err)
2278 goto err_sw_init;
2279 i40evf_map_rings_to_vectors(adapter);
1f012279
ASJ
2280 if (adapter->vf_res->vf_offload_flags &
2281 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2282 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
e25d00b8
ASJ
2283 if (!RSS_AQ(adapter))
2284 i40evf_configure_rss(adapter);
5eae00c5
GR
2285 err = i40evf_request_misc_irq(adapter);
2286 if (err)
2287 goto err_sw_init;
2288
2289 netif_carrier_off(netdev);
2290
ef8693eb
MW
2291 if (!adapter->netdev_registered) {
2292 err = register_netdev(netdev);
2293 if (err)
2294 goto err_register;
2295 }
5eae00c5
GR
2296
2297 adapter->netdev_registered = true;
2298
2299 netif_tx_stop_all_queues(netdev);
2300
b34f90e7 2301 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
5eae00c5
GR
2302 if (netdev->features & NETIF_F_GRO)
2303 dev_info(&pdev->dev, "GRO is enabled\n");
2304
2305 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2306 adapter->state = __I40EVF_DOWN;
2307 set_bit(__I40E_DOWN, &adapter->vsi.state);
2308 i40evf_misc_irq_enable(adapter);
e25d00b8
ASJ
2309
2310 if (RSS_AQ(adapter)) {
2311 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2312 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2313 } else {
2314 i40evf_configure_rss(adapter);
2315 }
5eae00c5
GR
2316 return;
2317restart:
5be8308b 2318 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(20));
5eae00c5
GR
2319 return;
2320
2321err_register:
2322 i40evf_free_misc_irq(adapter);
2323err_sw_init:
2324 i40evf_reset_interrupt_capability(adapter);
5eae00c5
GR
2325err_alloc:
2326 kfree(adapter->vf_res);
2327 adapter->vf_res = NULL;
2328err:
2329 /* Things went into the weeds, so try again later */
2330 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
80e72893 2331 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
ef8693eb 2332 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
5eae00c5
GR
2333 return; /* do not reschedule */
2334 }
5be8308b 2335 schedule_delayed_work(&adapter->init_task, HZ / 2);
5eae00c5
GR
2336}
2337
2338/**
2339 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2340 * @pdev: pci device structure
2341 **/
2342static void i40evf_shutdown(struct pci_dev *pdev)
2343{
2344 struct net_device *netdev = pci_get_drvdata(pdev);
00293fdc 2345 struct i40evf_adapter *adapter = netdev_priv(netdev);
5eae00c5
GR
2346
2347 netif_device_detach(netdev);
2348
2349 if (netif_running(netdev))
2350 i40evf_close(netdev);
2351
00293fdc
MW
2352 /* Prevent the watchdog from running. */
2353 adapter->state = __I40EVF_REMOVE;
2354 adapter->aq_required = 0;
00293fdc 2355
5eae00c5
GR
2356#ifdef CONFIG_PM
2357 pci_save_state(pdev);
2358
2359#endif
2360 pci_disable_device(pdev);
2361}
2362
2363/**
2364 * i40evf_probe - Device Initialization Routine
2365 * @pdev: PCI device information struct
2366 * @ent: entry in i40evf_pci_tbl
2367 *
2368 * Returns 0 on success, negative on failure
2369 *
2370 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2371 * The OS initialization, configuring of the adapter private structure,
2372 * and a hardware reset occur.
2373 **/
2374static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2375{
2376 struct net_device *netdev;
2377 struct i40evf_adapter *adapter = NULL;
2378 struct i40e_hw *hw = NULL;
dbbd8111 2379 int err;
5eae00c5
GR
2380
2381 err = pci_enable_device(pdev);
2382 if (err)
2383 return err;
2384
6494294f 2385 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6494294f 2386 if (err) {
e3e3bfdd
JS
2387 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2388 if (err) {
2389 dev_err(&pdev->dev,
2390 "DMA configuration failed: 0x%x\n", err);
2391 goto err_dma;
2392 }
5eae00c5
GR
2393 }
2394
2395 err = pci_request_regions(pdev, i40evf_driver_name);
2396 if (err) {
2397 dev_err(&pdev->dev,
2398 "pci_request_regions failed 0x%x\n", err);
2399 goto err_pci_reg;
2400 }
2401
2402 pci_enable_pcie_error_reporting(pdev);
2403
2404 pci_set_master(pdev);
2405
2406 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2407 MAX_TX_QUEUES);
2408 if (!netdev) {
2409 err = -ENOMEM;
2410 goto err_alloc_etherdev;
2411 }
2412
2413 SET_NETDEV_DEV(netdev, &pdev->dev);
2414
2415 pci_set_drvdata(pdev, netdev);
2416 adapter = netdev_priv(netdev);
5eae00c5
GR
2417
2418 adapter->netdev = netdev;
2419 adapter->pdev = pdev;
2420
2421 hw = &adapter->hw;
2422 hw->back = adapter;
2423
41a1d04b 2424 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
5eae00c5
GR
2425 adapter->state = __I40EVF_STARTUP;
2426
2427 /* Call save state here because it relies on the adapter struct. */
2428 pci_save_state(pdev);
2429
2430 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2431 pci_resource_len(pdev, 0));
2432 if (!hw->hw_addr) {
2433 err = -EIO;
2434 goto err_ioremap;
2435 }
2436 hw->vendor_id = pdev->vendor;
2437 hw->device_id = pdev->device;
2438 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2439 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2440 hw->subsystem_device_id = pdev->subsystem_device;
2441 hw->bus.device = PCI_SLOT(pdev->devfn);
2442 hw->bus.func = PCI_FUNC(pdev->devfn);
2443
8bb1a540
SK
2444 INIT_LIST_HEAD(&adapter->mac_filter_list);
2445 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2446
5eae00c5
GR
2447 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2448 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2449 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2450 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
9b32b0b5
MW
2451 schedule_delayed_work(&adapter->init_task,
2452 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
5eae00c5
GR
2453
2454 return 0;
2455
2456err_ioremap:
2457 free_netdev(netdev);
2458err_alloc_etherdev:
2459 pci_release_regions(pdev);
2460err_pci_reg:
2461err_dma:
2462 pci_disable_device(pdev);
2463 return err;
2464}
2465
2466#ifdef CONFIG_PM
2467/**
2468 * i40evf_suspend - Power management suspend routine
2469 * @pdev: PCI device information struct
2470 * @state: unused
2471 *
2472 * Called when the system (VM) is entering sleep/suspend.
2473 **/
2474static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2475{
2476 struct net_device *netdev = pci_get_drvdata(pdev);
2477 struct i40evf_adapter *adapter = netdev_priv(netdev);
2478 int retval = 0;
2479
2480 netif_device_detach(netdev);
2481
2482 if (netif_running(netdev)) {
2483 rtnl_lock();
2484 i40evf_down(adapter);
2485 rtnl_unlock();
2486 }
2487 i40evf_free_misc_irq(adapter);
2488 i40evf_reset_interrupt_capability(adapter);
2489
2490 retval = pci_save_state(pdev);
2491 if (retval)
2492 return retval;
2493
2494 pci_disable_device(pdev);
2495
2496 return 0;
2497}
2498
2499/**
dbedd44e 2500 * i40evf_resume - Power management resume routine
5eae00c5
GR
2501 * @pdev: PCI device information struct
2502 *
2503 * Called when the system (VM) is resumed from sleep/suspend.
2504 **/
2505static int i40evf_resume(struct pci_dev *pdev)
2506{
2507 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2508 struct net_device *netdev = adapter->netdev;
2509 u32 err;
2510
2511 pci_set_power_state(pdev, PCI_D0);
2512 pci_restore_state(pdev);
2513 /* pci_restore_state clears dev->state_saved so call
2514 * pci_save_state to restore it.
2515 */
2516 pci_save_state(pdev);
2517
2518 err = pci_enable_device_mem(pdev);
2519 if (err) {
2520 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2521 return err;
2522 }
2523 pci_set_master(pdev);
2524
2525 rtnl_lock();
2526 err = i40evf_set_interrupt_capability(adapter);
2527 if (err) {
f2a1c368 2528 rtnl_unlock();
5eae00c5
GR
2529 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2530 return err;
2531 }
2532 err = i40evf_request_misc_irq(adapter);
2533 rtnl_unlock();
2534 if (err) {
2535 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2536 return err;
2537 }
2538
2539 schedule_work(&adapter->reset_task);
2540
2541 netif_device_attach(netdev);
2542
2543 return err;
2544}
2545
2546#endif /* CONFIG_PM */
2547/**
2548 * i40evf_remove - Device Removal Routine
2549 * @pdev: PCI device information struct
2550 *
2551 * i40evf_remove is called by the PCI subsystem to alert the driver
2552 * that it should release a PCI device. The could be caused by a
2553 * Hot-Plug event, or because the driver is going to be removed from
2554 * memory.
2555 **/
2556static void i40evf_remove(struct pci_dev *pdev)
2557{
2558 struct net_device *netdev = pci_get_drvdata(pdev);
2559 struct i40evf_adapter *adapter = netdev_priv(netdev);
6ba36a24 2560 struct i40evf_mac_filter *f, *ftmp;
5eae00c5
GR
2561 struct i40e_hw *hw = &adapter->hw;
2562
2563 cancel_delayed_work_sync(&adapter->init_task);
ef8693eb 2564 cancel_work_sync(&adapter->reset_task);
5eae00c5
GR
2565
2566 if (adapter->netdev_registered) {
2567 unregister_netdev(netdev);
2568 adapter->netdev_registered = false;
2569 }
53d0b3ae 2570
f4a71881 2571 /* Shut down all the garbage mashers on the detention level */
5eae00c5 2572 adapter->state = __I40EVF_REMOVE;
f4a71881 2573 adapter->aq_required = 0;
f4a71881
MW
2574 i40evf_request_reset(adapter);
2575 msleep(20);
2576 /* If the FW isn't responding, kick it once, but only once. */
2577 if (!i40evf_asq_done(hw)) {
2578 i40evf_request_reset(adapter);
2579 msleep(20);
2580 }
5eae00c5 2581
dbb01c8a 2582 if (adapter->msix_entries) {
5eae00c5 2583 i40evf_misc_irq_disable(adapter);
5eae00c5 2584 i40evf_free_misc_irq(adapter);
5eae00c5 2585 i40evf_reset_interrupt_capability(adapter);
d31944d6 2586 i40evf_free_q_vectors(adapter);
5eae00c5
GR
2587 }
2588
e5d17c3e
MW
2589 if (adapter->watchdog_timer.function)
2590 del_timer_sync(&adapter->watchdog_timer);
2591
dbb01c8a
MW
2592 flush_scheduled_work();
2593
5eae00c5
GR
2594 if (hw->aq.asq.count)
2595 i40evf_shutdown_adminq(hw);
2596
2597 iounmap(hw->hw_addr);
2598 pci_release_regions(pdev);
2599
e284fc88
MW
2600 i40evf_free_all_tx_resources(adapter);
2601 i40evf_free_all_rx_resources(adapter);
5eae00c5
GR
2602 i40evf_free_queues(adapter);
2603 kfree(adapter->vf_res);
6ba36a24
MW
2604 /* If we got removed before an up/down sequence, we've got a filter
2605 * hanging out there that we need to get rid of.
2606 */
2607 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2608 list_del(&f->list);
2609 kfree(f);
2610 }
37dfdf37
MW
2611 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2612 list_del(&f->list);
2613 kfree(f);
2614 }
5eae00c5
GR
2615
2616 free_netdev(netdev);
2617
2618 pci_disable_pcie_error_reporting(pdev);
2619
2620 pci_disable_device(pdev);
2621}
2622
2623static struct pci_driver i40evf_driver = {
2624 .name = i40evf_driver_name,
2625 .id_table = i40evf_pci_tbl,
2626 .probe = i40evf_probe,
2627 .remove = i40evf_remove,
2628#ifdef CONFIG_PM
2629 .suspend = i40evf_suspend,
2630 .resume = i40evf_resume,
2631#endif
2632 .shutdown = i40evf_shutdown,
2633};
2634
2635/**
2636 * i40e_init_module - Driver Registration Routine
2637 *
2638 * i40e_init_module is the first routine called when the driver is
2639 * loaded. All it does is register with the PCI subsystem.
2640 **/
2641static int __init i40evf_init_module(void)
2642{
2643 int ret;
75a64435 2644
5eae00c5 2645 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
75a64435 2646 i40evf_driver_version);
5eae00c5
GR
2647
2648 pr_info("%s\n", i40evf_copyright);
2649
2650 ret = pci_register_driver(&i40evf_driver);
2651 return ret;
2652}
2653
2654module_init(i40evf_init_module);
2655
2656/**
2657 * i40e_exit_module - Driver Exit Cleanup Routine
2658 *
2659 * i40e_exit_module is called just before the driver is removed
2660 * from memory.
2661 **/
2662static void __exit i40evf_exit_module(void)
2663{
2664 pci_unregister_driver(&i40evf_driver);
2665}
2666
2667module_exit(i40evf_exit_module);
2668
2669/* i40evf_main.c */
This page took 0.300603 seconds and 5 git commands to generate.