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