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