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