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