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