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