Merge tag 'mac80211-next-for-davem-2016-05-12' of git://git.kernel.org/pub/scm/linux...
[deliverable/linux.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 int i;
645
646 for (i = 0; i < adapter->num_active_queues; i++) {
647 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
648 adapter->rx_rings[i].rx_buf_len = I40EVF_RXBUFFER_2048;
649 }
650 }
651
652 /**
653 * i40evf_find_vlan - Search filter list for specific vlan filter
654 * @adapter: board private structure
655 * @vlan: vlan tag
656 *
657 * Returns ptr to the filter object or NULL
658 **/
659 static struct
660 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
661 {
662 struct i40evf_vlan_filter *f;
663
664 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
665 if (vlan == f->vlan)
666 return f;
667 }
668 return NULL;
669 }
670
671 /**
672 * i40evf_add_vlan - Add a vlan filter to the list
673 * @adapter: board private structure
674 * @vlan: VLAN tag
675 *
676 * Returns ptr to the filter object or NULL when no memory available.
677 **/
678 static struct
679 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
680 {
681 struct i40evf_vlan_filter *f = NULL;
682 int count = 50;
683
684 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
685 &adapter->crit_section)) {
686 udelay(1);
687 if (--count == 0)
688 goto out;
689 }
690
691 f = i40evf_find_vlan(adapter, vlan);
692 if (!f) {
693 f = kzalloc(sizeof(*f), GFP_ATOMIC);
694 if (!f)
695 goto clearout;
696
697 f->vlan = vlan;
698
699 INIT_LIST_HEAD(&f->list);
700 list_add(&f->list, &adapter->vlan_filter_list);
701 f->add = true;
702 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
703 }
704
705 clearout:
706 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
707 out:
708 return f;
709 }
710
711 /**
712 * i40evf_del_vlan - Remove a vlan filter from the list
713 * @adapter: board private structure
714 * @vlan: VLAN tag
715 **/
716 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
717 {
718 struct i40evf_vlan_filter *f;
719 int count = 50;
720
721 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
722 &adapter->crit_section)) {
723 udelay(1);
724 if (--count == 0)
725 return;
726 }
727
728 f = i40evf_find_vlan(adapter, vlan);
729 if (f) {
730 f->remove = true;
731 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
732 }
733 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
734 }
735
736 /**
737 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
738 * @netdev: network device struct
739 * @vid: VLAN tag
740 **/
741 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
742 __always_unused __be16 proto, u16 vid)
743 {
744 struct i40evf_adapter *adapter = netdev_priv(netdev);
745
746 if (!VLAN_ALLOWED(adapter))
747 return -EIO;
748 if (i40evf_add_vlan(adapter, vid) == NULL)
749 return -ENOMEM;
750 return 0;
751 }
752
753 /**
754 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
755 * @netdev: network device struct
756 * @vid: VLAN tag
757 **/
758 static int i40evf_vlan_rx_kill_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 i40evf_del_vlan(adapter, vid);
765 return 0;
766 }
767 return -EIO;
768 }
769
770 /**
771 * i40evf_find_filter - Search filter list for specific mac filter
772 * @adapter: board private structure
773 * @macaddr: the MAC address
774 *
775 * Returns ptr to the filter object or NULL
776 **/
777 static struct
778 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
779 u8 *macaddr)
780 {
781 struct i40evf_mac_filter *f;
782
783 if (!macaddr)
784 return NULL;
785
786 list_for_each_entry(f, &adapter->mac_filter_list, list) {
787 if (ether_addr_equal(macaddr, f->macaddr))
788 return f;
789 }
790 return NULL;
791 }
792
793 /**
794 * i40e_add_filter - Add a mac filter to the filter list
795 * @adapter: board private structure
796 * @macaddr: the MAC address
797 *
798 * Returns ptr to the filter object or NULL when no memory available.
799 **/
800 static struct
801 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
802 u8 *macaddr)
803 {
804 struct i40evf_mac_filter *f;
805 int count = 50;
806
807 if (!macaddr)
808 return NULL;
809
810 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
811 &adapter->crit_section)) {
812 udelay(1);
813 if (--count == 0)
814 return NULL;
815 }
816
817 f = i40evf_find_filter(adapter, macaddr);
818 if (!f) {
819 f = kzalloc(sizeof(*f), GFP_ATOMIC);
820 if (!f) {
821 clear_bit(__I40EVF_IN_CRITICAL_TASK,
822 &adapter->crit_section);
823 return NULL;
824 }
825
826 ether_addr_copy(f->macaddr, macaddr);
827
828 list_add(&f->list, &adapter->mac_filter_list);
829 f->add = true;
830 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
831 }
832
833 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
834 return f;
835 }
836
837 /**
838 * i40evf_set_mac - NDO callback to set port mac address
839 * @netdev: network interface device structure
840 * @p: pointer to an address structure
841 *
842 * Returns 0 on success, negative on failure
843 **/
844 static int i40evf_set_mac(struct net_device *netdev, void *p)
845 {
846 struct i40evf_adapter *adapter = netdev_priv(netdev);
847 struct i40e_hw *hw = &adapter->hw;
848 struct i40evf_mac_filter *f;
849 struct sockaddr *addr = p;
850
851 if (!is_valid_ether_addr(addr->sa_data))
852 return -EADDRNOTAVAIL;
853
854 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
855 return 0;
856
857 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
858 return -EPERM;
859
860 f = i40evf_find_filter(adapter, hw->mac.addr);
861 if (f) {
862 f->remove = true;
863 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
864 }
865
866 f = i40evf_add_filter(adapter, addr->sa_data);
867 if (f) {
868 ether_addr_copy(hw->mac.addr, addr->sa_data);
869 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
870 }
871
872 return (f == NULL) ? -ENOMEM : 0;
873 }
874
875 /**
876 * i40evf_set_rx_mode - NDO callback to set the netdev filters
877 * @netdev: network interface device structure
878 **/
879 static void i40evf_set_rx_mode(struct net_device *netdev)
880 {
881 struct i40evf_adapter *adapter = netdev_priv(netdev);
882 struct i40evf_mac_filter *f, *ftmp;
883 struct netdev_hw_addr *uca;
884 struct netdev_hw_addr *mca;
885 struct netdev_hw_addr *ha;
886 int count = 50;
887
888 /* add addr if not already in the filter list */
889 netdev_for_each_uc_addr(uca, netdev) {
890 i40evf_add_filter(adapter, uca->addr);
891 }
892 netdev_for_each_mc_addr(mca, netdev) {
893 i40evf_add_filter(adapter, mca->addr);
894 }
895
896 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
897 &adapter->crit_section)) {
898 udelay(1);
899 if (--count == 0) {
900 dev_err(&adapter->pdev->dev,
901 "Failed to get lock in %s\n", __func__);
902 return;
903 }
904 }
905 /* remove filter if not in netdev list */
906 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
907 netdev_for_each_mc_addr(mca, netdev)
908 if (ether_addr_equal(mca->addr, f->macaddr))
909 goto bottom_of_search_loop;
910
911 netdev_for_each_uc_addr(uca, netdev)
912 if (ether_addr_equal(uca->addr, f->macaddr))
913 goto bottom_of_search_loop;
914
915 for_each_dev_addr(netdev, ha)
916 if (ether_addr_equal(ha->addr, f->macaddr))
917 goto bottom_of_search_loop;
918
919 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
920 goto bottom_of_search_loop;
921
922 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
923 f->remove = true;
924 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
925
926 bottom_of_search_loop:
927 continue;
928 }
929
930 if (netdev->flags & IFF_PROMISC &&
931 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
932 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
933 else if (!(netdev->flags & IFF_PROMISC) &&
934 adapter->flags & I40EVF_FLAG_PROMISC_ON)
935 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
936
937 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
938 }
939
940 /**
941 * i40evf_napi_enable_all - enable NAPI on all queue vectors
942 * @adapter: board private structure
943 **/
944 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
945 {
946 int q_idx;
947 struct i40e_q_vector *q_vector;
948 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
949
950 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
951 struct napi_struct *napi;
952
953 q_vector = &adapter->q_vectors[q_idx];
954 napi = &q_vector->napi;
955 napi_enable(napi);
956 }
957 }
958
959 /**
960 * i40evf_napi_disable_all - disable NAPI on all queue vectors
961 * @adapter: board private structure
962 **/
963 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
964 {
965 int q_idx;
966 struct i40e_q_vector *q_vector;
967 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
968
969 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
970 q_vector = &adapter->q_vectors[q_idx];
971 napi_disable(&q_vector->napi);
972 }
973 }
974
975 /**
976 * i40evf_configure - set up transmit and receive data structures
977 * @adapter: board private structure
978 **/
979 static void i40evf_configure(struct i40evf_adapter *adapter)
980 {
981 struct net_device *netdev = adapter->netdev;
982 int i;
983
984 i40evf_set_rx_mode(netdev);
985
986 i40evf_configure_tx(adapter);
987 i40evf_configure_rx(adapter);
988 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
989
990 for (i = 0; i < adapter->num_active_queues; i++) {
991 struct i40e_ring *ring = &adapter->rx_rings[i];
992
993 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
994 }
995 }
996
997 /**
998 * i40evf_up_complete - Finish the last steps of bringing up a connection
999 * @adapter: board private structure
1000 **/
1001 static int i40evf_up_complete(struct i40evf_adapter *adapter)
1002 {
1003 adapter->state = __I40EVF_RUNNING;
1004 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1005
1006 i40evf_napi_enable_all(adapter);
1007
1008 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1009 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1010 return 0;
1011 }
1012
1013 /**
1014 * i40e_down - Shutdown the connection processing
1015 * @adapter: board private structure
1016 **/
1017 void i40evf_down(struct i40evf_adapter *adapter)
1018 {
1019 struct net_device *netdev = adapter->netdev;
1020 struct i40evf_mac_filter *f;
1021
1022 if (adapter->state <= __I40EVF_DOWN_PENDING)
1023 return;
1024
1025 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1026 &adapter->crit_section))
1027 usleep_range(500, 1000);
1028
1029 netif_carrier_off(netdev);
1030 netif_tx_disable(netdev);
1031 i40evf_napi_disable_all(adapter);
1032 i40evf_irq_disable(adapter);
1033
1034 /* remove all MAC filters */
1035 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1036 f->remove = true;
1037 }
1038 /* remove all VLAN filters */
1039 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1040 f->remove = true;
1041 }
1042 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1043 adapter->state != __I40EVF_RESETTING) {
1044 /* cancel any current operation */
1045 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1046 /* Schedule operations to close down the HW. Don't wait
1047 * here for this to complete. The watchdog is still running
1048 * and it will take care of this.
1049 */
1050 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1051 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1052 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1053 }
1054
1055 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1056 }
1057
1058 /**
1059 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1060 * @adapter: board private structure
1061 * @vectors: number of vectors to request
1062 *
1063 * Work with the OS to set up the MSIX vectors needed.
1064 *
1065 * Returns 0 on success, negative on failure
1066 **/
1067 static int
1068 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1069 {
1070 int err, vector_threshold;
1071
1072 /* We'll want at least 3 (vector_threshold):
1073 * 0) Other (Admin Queue and link, mostly)
1074 * 1) TxQ[0] Cleanup
1075 * 2) RxQ[0] Cleanup
1076 */
1077 vector_threshold = MIN_MSIX_COUNT;
1078
1079 /* The more we get, the more we will assign to Tx/Rx Cleanup
1080 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1081 * Right now, we simply care about how many we'll get; we'll
1082 * set them up later while requesting irq's.
1083 */
1084 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1085 vector_threshold, vectors);
1086 if (err < 0) {
1087 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1088 kfree(adapter->msix_entries);
1089 adapter->msix_entries = NULL;
1090 return err;
1091 }
1092
1093 /* Adjust for only the vectors we'll use, which is minimum
1094 * of max_msix_q_vectors + NONQ_VECS, or the number of
1095 * vectors we were allocated.
1096 */
1097 adapter->num_msix_vectors = err;
1098 return 0;
1099 }
1100
1101 /**
1102 * i40evf_free_queues - Free memory for all rings
1103 * @adapter: board private structure to initialize
1104 *
1105 * Free all of the memory associated with queue pairs.
1106 **/
1107 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1108 {
1109 if (!adapter->vsi_res)
1110 return;
1111 kfree(adapter->tx_rings);
1112 adapter->tx_rings = NULL;
1113 kfree(adapter->rx_rings);
1114 adapter->rx_rings = NULL;
1115 }
1116
1117 /**
1118 * i40evf_alloc_queues - Allocate memory for all rings
1119 * @adapter: board private structure to initialize
1120 *
1121 * We allocate one ring per queue at run-time since we don't know the
1122 * number of queues at compile-time. The polling_netdev array is
1123 * intended for Multiqueue, but should work fine with a single queue.
1124 **/
1125 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1126 {
1127 int i;
1128
1129 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1130 sizeof(struct i40e_ring), GFP_KERNEL);
1131 if (!adapter->tx_rings)
1132 goto err_out;
1133 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1134 sizeof(struct i40e_ring), GFP_KERNEL);
1135 if (!adapter->rx_rings)
1136 goto err_out;
1137
1138 for (i = 0; i < adapter->num_active_queues; i++) {
1139 struct i40e_ring *tx_ring;
1140 struct i40e_ring *rx_ring;
1141
1142 tx_ring = &adapter->tx_rings[i];
1143
1144 tx_ring->queue_index = i;
1145 tx_ring->netdev = adapter->netdev;
1146 tx_ring->dev = &adapter->pdev->dev;
1147 tx_ring->count = adapter->tx_desc_count;
1148 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1149 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1150
1151 rx_ring = &adapter->rx_rings[i];
1152 rx_ring->queue_index = i;
1153 rx_ring->netdev = adapter->netdev;
1154 rx_ring->dev = &adapter->pdev->dev;
1155 rx_ring->count = adapter->rx_desc_count;
1156 }
1157
1158 return 0;
1159
1160 err_out:
1161 i40evf_free_queues(adapter);
1162 return -ENOMEM;
1163 }
1164
1165 /**
1166 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1167 * @adapter: board private structure to initialize
1168 *
1169 * Attempt to configure the interrupts using the best available
1170 * capabilities of the hardware and the kernel.
1171 **/
1172 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1173 {
1174 int vector, v_budget;
1175 int pairs = 0;
1176 int err = 0;
1177
1178 if (!adapter->vsi_res) {
1179 err = -EIO;
1180 goto out;
1181 }
1182 pairs = adapter->num_active_queues;
1183
1184 /* It's easy to be greedy for MSI-X vectors, but it really
1185 * doesn't do us much good if we have a lot more vectors
1186 * than CPU's. So let's be conservative and only ask for
1187 * (roughly) twice the number of vectors as there are CPU's.
1188 */
1189 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1190 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
1191
1192 adapter->msix_entries = kcalloc(v_budget,
1193 sizeof(struct msix_entry), GFP_KERNEL);
1194 if (!adapter->msix_entries) {
1195 err = -ENOMEM;
1196 goto out;
1197 }
1198
1199 for (vector = 0; vector < v_budget; vector++)
1200 adapter->msix_entries[vector].entry = vector;
1201
1202 err = i40evf_acquire_msix_vectors(adapter, v_budget);
1203
1204 out:
1205 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1206 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1207 return err;
1208 }
1209
1210 /**
1211 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1212 * @adapter: board private structure
1213 *
1214 * Return 0 on success, negative on failure
1215 **/
1216 static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
1217 {
1218 struct i40e_aqc_get_set_rss_key_data *rss_key =
1219 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1220 struct i40e_hw *hw = &adapter->hw;
1221 int ret = 0;
1222
1223 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1224 /* bail because we already have a command pending */
1225 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1226 adapter->current_op);
1227 return -EBUSY;
1228 }
1229
1230 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1231 if (ret) {
1232 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1233 i40evf_stat_str(hw, ret),
1234 i40evf_aq_str(hw, hw->aq.asq_last_status));
1235 return ret;
1236
1237 }
1238
1239 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1240 adapter->rss_lut, adapter->rss_lut_size);
1241 if (ret) {
1242 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1243 i40evf_stat_str(hw, ret),
1244 i40evf_aq_str(hw, hw->aq.asq_last_status));
1245 }
1246
1247 return ret;
1248
1249 }
1250
1251 /**
1252 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1253 * @adapter: board private structure
1254 *
1255 * Returns 0 on success, negative on failure
1256 **/
1257 static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
1258 {
1259 struct i40e_hw *hw = &adapter->hw;
1260 u32 *dw;
1261 u16 i;
1262
1263 dw = (u32 *)adapter->rss_key;
1264 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1265 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1266
1267 dw = (u32 *)adapter->rss_lut;
1268 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1269 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1270
1271 i40e_flush(hw);
1272
1273 return 0;
1274 }
1275
1276 /**
1277 * i40evf_config_rss - Configure RSS keys and lut
1278 * @adapter: board private structure
1279 *
1280 * Returns 0 on success, negative on failure
1281 **/
1282 int i40evf_config_rss(struct i40evf_adapter *adapter)
1283 {
1284
1285 if (RSS_PF(adapter)) {
1286 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1287 I40EVF_FLAG_AQ_SET_RSS_KEY;
1288 return 0;
1289 } else if (RSS_AQ(adapter)) {
1290 return i40evf_config_rss_aq(adapter);
1291 } else {
1292 return i40evf_config_rss_reg(adapter);
1293 }
1294 }
1295
1296 /**
1297 * i40evf_fill_rss_lut - Fill the lut with default values
1298 * @adapter: board private structure
1299 **/
1300 static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
1301 {
1302 u16 i;
1303
1304 for (i = 0; i < adapter->rss_lut_size; i++)
1305 adapter->rss_lut[i] = i % adapter->num_active_queues;
1306 }
1307
1308 /**
1309 * i40evf_init_rss - Prepare for RSS
1310 * @adapter: board private structure
1311 *
1312 * Return 0 on success, negative on failure
1313 **/
1314 static int i40evf_init_rss(struct i40evf_adapter *adapter)
1315 {
1316 struct i40e_hw *hw = &adapter->hw;
1317 int ret;
1318
1319 if (!RSS_PF(adapter)) {
1320 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1321 if (adapter->vf_res->vf_offload_flags &
1322 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1323 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1324 else
1325 adapter->hena = I40E_DEFAULT_RSS_HENA;
1326
1327 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1328 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1329 }
1330
1331 i40evf_fill_rss_lut(adapter);
1332
1333 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1334 ret = i40evf_config_rss(adapter);
1335
1336 return ret;
1337 }
1338
1339 /**
1340 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1341 * @adapter: board private structure to initialize
1342 *
1343 * We allocate one q_vector per queue interrupt. If allocation fails we
1344 * return -ENOMEM.
1345 **/
1346 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1347 {
1348 int q_idx = 0, num_q_vectors;
1349 struct i40e_q_vector *q_vector;
1350
1351 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1352 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1353 GFP_KERNEL);
1354 if (!adapter->q_vectors)
1355 return -ENOMEM;
1356
1357 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1358 q_vector = &adapter->q_vectors[q_idx];
1359 q_vector->adapter = adapter;
1360 q_vector->vsi = &adapter->vsi;
1361 q_vector->v_idx = q_idx;
1362 netif_napi_add(adapter->netdev, &q_vector->napi,
1363 i40evf_napi_poll, NAPI_POLL_WEIGHT);
1364 }
1365
1366 return 0;
1367 }
1368
1369 /**
1370 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1371 * @adapter: board private structure to initialize
1372 *
1373 * This function frees the memory allocated to the q_vectors. In addition if
1374 * NAPI is enabled it will delete any references to the NAPI struct prior
1375 * to freeing the q_vector.
1376 **/
1377 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1378 {
1379 int q_idx, num_q_vectors;
1380 int napi_vectors;
1381
1382 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1383 napi_vectors = adapter->num_active_queues;
1384
1385 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1386 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1387 if (q_idx < napi_vectors)
1388 netif_napi_del(&q_vector->napi);
1389 }
1390 kfree(adapter->q_vectors);
1391 }
1392
1393 /**
1394 * i40evf_reset_interrupt_capability - Reset MSIX setup
1395 * @adapter: board private structure
1396 *
1397 **/
1398 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1399 {
1400 pci_disable_msix(adapter->pdev);
1401 kfree(adapter->msix_entries);
1402 adapter->msix_entries = NULL;
1403 }
1404
1405 /**
1406 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1407 * @adapter: board private structure to initialize
1408 *
1409 **/
1410 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1411 {
1412 int err;
1413
1414 err = i40evf_set_interrupt_capability(adapter);
1415 if (err) {
1416 dev_err(&adapter->pdev->dev,
1417 "Unable to setup interrupt capabilities\n");
1418 goto err_set_interrupt;
1419 }
1420
1421 err = i40evf_alloc_q_vectors(adapter);
1422 if (err) {
1423 dev_err(&adapter->pdev->dev,
1424 "Unable to allocate memory for queue vectors\n");
1425 goto err_alloc_q_vectors;
1426 }
1427
1428 err = i40evf_alloc_queues(adapter);
1429 if (err) {
1430 dev_err(&adapter->pdev->dev,
1431 "Unable to allocate memory for queues\n");
1432 goto err_alloc_queues;
1433 }
1434
1435 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1436 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1437 adapter->num_active_queues);
1438
1439 return 0;
1440 err_alloc_queues:
1441 i40evf_free_q_vectors(adapter);
1442 err_alloc_q_vectors:
1443 i40evf_reset_interrupt_capability(adapter);
1444 err_set_interrupt:
1445 return err;
1446 }
1447
1448 /**
1449 * i40evf_free_rss - Free memory used by RSS structs
1450 * @adapter: board private structure
1451 **/
1452 static void i40evf_free_rss(struct i40evf_adapter *adapter)
1453 {
1454 kfree(adapter->rss_key);
1455 adapter->rss_key = NULL;
1456
1457 kfree(adapter->rss_lut);
1458 adapter->rss_lut = NULL;
1459 }
1460
1461 /**
1462 * i40evf_watchdog_timer - Periodic call-back timer
1463 * @data: pointer to adapter disguised as unsigned long
1464 **/
1465 static void i40evf_watchdog_timer(unsigned long data)
1466 {
1467 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1468
1469 schedule_work(&adapter->watchdog_task);
1470 /* timer will be rescheduled in watchdog task */
1471 }
1472
1473 /**
1474 * i40evf_watchdog_task - Periodic call-back task
1475 * @work: pointer to work_struct
1476 **/
1477 static void i40evf_watchdog_task(struct work_struct *work)
1478 {
1479 struct i40evf_adapter *adapter = container_of(work,
1480 struct i40evf_adapter,
1481 watchdog_task);
1482 struct i40e_hw *hw = &adapter->hw;
1483 u32 reg_val;
1484
1485 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1486 goto restart_watchdog;
1487
1488 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1489 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1490 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1491 if ((reg_val == I40E_VFR_VFACTIVE) ||
1492 (reg_val == I40E_VFR_COMPLETED)) {
1493 /* A chance for redemption! */
1494 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1495 adapter->state = __I40EVF_STARTUP;
1496 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1497 schedule_delayed_work(&adapter->init_task, 10);
1498 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1499 &adapter->crit_section);
1500 /* Don't reschedule the watchdog, since we've restarted
1501 * the init task. When init_task contacts the PF and
1502 * gets everything set up again, it'll restart the
1503 * watchdog for us. Down, boy. Sit. Stay. Woof.
1504 */
1505 return;
1506 }
1507 adapter->aq_required = 0;
1508 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1509 goto watchdog_done;
1510 }
1511
1512 if ((adapter->state < __I40EVF_DOWN) ||
1513 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1514 goto watchdog_done;
1515
1516 /* check for reset */
1517 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1518 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
1519 adapter->state = __I40EVF_RESETTING;
1520 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1521 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1522 schedule_work(&adapter->reset_task);
1523 adapter->aq_required = 0;
1524 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1525 goto watchdog_done;
1526 }
1527
1528 /* Process admin queue tasks. After init, everything gets done
1529 * here so we don't race on the admin queue.
1530 */
1531 if (adapter->current_op) {
1532 if (!i40evf_asq_done(hw)) {
1533 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1534 i40evf_send_api_ver(adapter);
1535 }
1536 goto watchdog_done;
1537 }
1538 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1539 i40evf_send_vf_config_msg(adapter);
1540 goto watchdog_done;
1541 }
1542
1543 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1544 i40evf_disable_queues(adapter);
1545 goto watchdog_done;
1546 }
1547
1548 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1549 i40evf_map_queues(adapter);
1550 goto watchdog_done;
1551 }
1552
1553 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1554 i40evf_add_ether_addrs(adapter);
1555 goto watchdog_done;
1556 }
1557
1558 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1559 i40evf_add_vlans(adapter);
1560 goto watchdog_done;
1561 }
1562
1563 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1564 i40evf_del_ether_addrs(adapter);
1565 goto watchdog_done;
1566 }
1567
1568 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1569 i40evf_del_vlans(adapter);
1570 goto watchdog_done;
1571 }
1572
1573 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1574 i40evf_configure_queues(adapter);
1575 goto watchdog_done;
1576 }
1577
1578 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1579 i40evf_enable_queues(adapter);
1580 goto watchdog_done;
1581 }
1582
1583 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1584 /* This message goes straight to the firmware, not the
1585 * PF, so we don't have to set current_op as we will
1586 * not get a response through the ARQ.
1587 */
1588 i40evf_init_rss(adapter);
1589 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1590 goto watchdog_done;
1591 }
1592 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1593 i40evf_get_hena(adapter);
1594 goto watchdog_done;
1595 }
1596 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1597 i40evf_set_hena(adapter);
1598 goto watchdog_done;
1599 }
1600 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1601 i40evf_set_rss_key(adapter);
1602 goto watchdog_done;
1603 }
1604 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1605 i40evf_set_rss_lut(adapter);
1606 goto watchdog_done;
1607 }
1608
1609 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1610 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_UNICAST_PROMISC |
1611 I40E_FLAG_VF_MULTICAST_PROMISC);
1612 goto watchdog_done;
1613 }
1614
1615 if (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) {
1616 i40evf_set_promiscuous(adapter, 0);
1617 goto watchdog_done;
1618 }
1619
1620 if (adapter->state == __I40EVF_RUNNING)
1621 i40evf_request_stats(adapter);
1622 watchdog_done:
1623 if (adapter->state == __I40EVF_RUNNING) {
1624 i40evf_irq_enable_queues(adapter, ~0);
1625 i40evf_fire_sw_int(adapter, 0xFF);
1626 } else {
1627 i40evf_fire_sw_int(adapter, 0x1);
1628 }
1629
1630 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1631 restart_watchdog:
1632 if (adapter->state == __I40EVF_REMOVE)
1633 return;
1634 if (adapter->aq_required)
1635 mod_timer(&adapter->watchdog_timer,
1636 jiffies + msecs_to_jiffies(20));
1637 else
1638 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1639 schedule_work(&adapter->adminq_task);
1640 }
1641
1642 #define I40EVF_RESET_WAIT_MS 10
1643 #define I40EVF_RESET_WAIT_COUNT 500
1644 /**
1645 * i40evf_reset_task - Call-back task to handle hardware reset
1646 * @work: pointer to work_struct
1647 *
1648 * During reset we need to shut down and reinitialize the admin queue
1649 * before we can use it to communicate with the PF again. We also clear
1650 * and reinit the rings because that context is lost as well.
1651 **/
1652 static void i40evf_reset_task(struct work_struct *work)
1653 {
1654 struct i40evf_adapter *adapter = container_of(work,
1655 struct i40evf_adapter,
1656 reset_task);
1657 struct net_device *netdev = adapter->netdev;
1658 struct i40e_hw *hw = &adapter->hw;
1659 struct i40evf_vlan_filter *vlf;
1660 struct i40evf_mac_filter *f;
1661 u32 reg_val;
1662 int i = 0, err;
1663
1664 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1665 &adapter->crit_section))
1666 usleep_range(500, 1000);
1667
1668 i40evf_misc_irq_disable(adapter);
1669 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1670 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1671 /* Restart the AQ here. If we have been reset but didn't
1672 * detect it, or if the PF had to reinit, our AQ will be hosed.
1673 */
1674 i40evf_shutdown_adminq(hw);
1675 i40evf_init_adminq(hw);
1676 i40evf_request_reset(adapter);
1677 }
1678 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1679
1680 /* poll until we see the reset actually happen */
1681 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1682 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1683 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1684 if (!reg_val)
1685 break;
1686 usleep_range(5000, 10000);
1687 }
1688 if (i == I40EVF_RESET_WAIT_COUNT) {
1689 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1690 goto continue_reset; /* act like the reset happened */
1691 }
1692
1693 /* wait until the reset is complete and the PF is responding to us */
1694 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1695 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1696 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1697 if (reg_val == I40E_VFR_VFACTIVE)
1698 break;
1699 msleep(I40EVF_RESET_WAIT_MS);
1700 }
1701 pci_set_master(adapter->pdev);
1702 /* extra wait to make sure minimum wait is met */
1703 msleep(I40EVF_RESET_WAIT_MS);
1704 if (i == I40EVF_RESET_WAIT_COUNT) {
1705 struct i40evf_mac_filter *ftmp;
1706 struct i40evf_vlan_filter *fv, *fvtmp;
1707
1708 /* reset never finished */
1709 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1710 reg_val);
1711 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1712
1713 if (netif_running(adapter->netdev)) {
1714 set_bit(__I40E_DOWN, &adapter->vsi.state);
1715 netif_carrier_off(netdev);
1716 netif_tx_disable(netdev);
1717 i40evf_napi_disable_all(adapter);
1718 i40evf_irq_disable(adapter);
1719 i40evf_free_traffic_irqs(adapter);
1720 i40evf_free_all_tx_resources(adapter);
1721 i40evf_free_all_rx_resources(adapter);
1722 }
1723
1724 /* Delete all of the filters, both MAC and VLAN. */
1725 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1726 list) {
1727 list_del(&f->list);
1728 kfree(f);
1729 }
1730
1731 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1732 list) {
1733 list_del(&fv->list);
1734 kfree(fv);
1735 }
1736
1737 i40evf_free_misc_irq(adapter);
1738 i40evf_reset_interrupt_capability(adapter);
1739 i40evf_free_queues(adapter);
1740 i40evf_free_q_vectors(adapter);
1741 kfree(adapter->vf_res);
1742 i40evf_shutdown_adminq(hw);
1743 adapter->netdev->flags &= ~IFF_UP;
1744 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1745 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1746 adapter->state = __I40EVF_DOWN;
1747 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1748 return; /* Do not attempt to reinit. It's dead, Jim. */
1749 }
1750
1751 continue_reset:
1752 if (netif_running(adapter->netdev)) {
1753 netif_carrier_off(netdev);
1754 netif_tx_stop_all_queues(netdev);
1755 i40evf_napi_disable_all(adapter);
1756 }
1757 i40evf_irq_disable(adapter);
1758
1759 adapter->state = __I40EVF_RESETTING;
1760 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1761
1762 /* free the Tx/Rx rings and descriptors, might be better to just
1763 * re-use them sometime in the future
1764 */
1765 i40evf_free_all_rx_resources(adapter);
1766 i40evf_free_all_tx_resources(adapter);
1767
1768 /* kill and reinit the admin queue */
1769 if (i40evf_shutdown_adminq(hw))
1770 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1771 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1772 err = i40evf_init_adminq(hw);
1773 if (err)
1774 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1775 err);
1776
1777 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1778 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1779
1780 /* re-add all MAC filters */
1781 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1782 f->add = true;
1783 }
1784 /* re-add all VLAN filters */
1785 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1786 vlf->add = true;
1787 }
1788 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1789 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1790 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1791 i40evf_misc_irq_enable(adapter);
1792
1793 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1794
1795 if (netif_running(adapter->netdev)) {
1796 /* allocate transmit descriptors */
1797 err = i40evf_setup_all_tx_resources(adapter);
1798 if (err)
1799 goto reset_err;
1800
1801 /* allocate receive descriptors */
1802 err = i40evf_setup_all_rx_resources(adapter);
1803 if (err)
1804 goto reset_err;
1805
1806 i40evf_configure(adapter);
1807
1808 err = i40evf_up_complete(adapter);
1809 if (err)
1810 goto reset_err;
1811
1812 i40evf_irq_enable(adapter, true);
1813 } else {
1814 adapter->state = __I40EVF_DOWN;
1815 }
1816
1817 return;
1818 reset_err:
1819 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1820 i40evf_close(adapter->netdev);
1821 }
1822
1823 /**
1824 * i40evf_adminq_task - worker thread to clean the admin queue
1825 * @work: pointer to work_struct containing our data
1826 **/
1827 static void i40evf_adminq_task(struct work_struct *work)
1828 {
1829 struct i40evf_adapter *adapter =
1830 container_of(work, struct i40evf_adapter, adminq_task);
1831 struct i40e_hw *hw = &adapter->hw;
1832 struct i40e_arq_event_info event;
1833 struct i40e_virtchnl_msg *v_msg;
1834 i40e_status ret;
1835 u32 val, oldval;
1836 u16 pending;
1837
1838 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1839 goto out;
1840
1841 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1842 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1843 if (!event.msg_buf)
1844 goto out;
1845
1846 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1847 do {
1848 ret = i40evf_clean_arq_element(hw, &event, &pending);
1849 if (ret || !v_msg->v_opcode)
1850 break; /* No event to process or error cleaning ARQ */
1851
1852 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1853 v_msg->v_retval, event.msg_buf,
1854 event.msg_len);
1855 if (pending != 0)
1856 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1857 } while (pending);
1858
1859 if ((adapter->flags &
1860 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1861 adapter->state == __I40EVF_RESETTING)
1862 goto freedom;
1863
1864 /* check for error indications */
1865 val = rd32(hw, hw->aq.arq.len);
1866 if (val == 0xdeadbeef) /* indicates device in reset */
1867 goto freedom;
1868 oldval = val;
1869 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
1870 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1871 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
1872 }
1873 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
1874 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1875 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
1876 }
1877 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
1878 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1879 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
1880 }
1881 if (oldval != val)
1882 wr32(hw, hw->aq.arq.len, val);
1883
1884 val = rd32(hw, hw->aq.asq.len);
1885 oldval = val;
1886 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
1887 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1888 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
1889 }
1890 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
1891 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1892 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
1893 }
1894 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
1895 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1896 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
1897 }
1898 if (oldval != val)
1899 wr32(hw, hw->aq.asq.len, val);
1900
1901 freedom:
1902 kfree(event.msg_buf);
1903 out:
1904 /* re-enable Admin queue interrupt cause */
1905 i40evf_misc_irq_enable(adapter);
1906 }
1907
1908 /**
1909 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1910 * @adapter: board private structure
1911 *
1912 * Free all transmit software resources
1913 **/
1914 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1915 {
1916 int i;
1917
1918 if (!adapter->tx_rings)
1919 return;
1920
1921 for (i = 0; i < adapter->num_active_queues; i++)
1922 if (adapter->tx_rings[i].desc)
1923 i40evf_free_tx_resources(&adapter->tx_rings[i]);
1924 }
1925
1926 /**
1927 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1928 * @adapter: board private structure
1929 *
1930 * If this function returns with an error, then it's possible one or
1931 * more of the rings is populated (while the rest are not). It is the
1932 * callers duty to clean those orphaned rings.
1933 *
1934 * Return 0 on success, negative on failure
1935 **/
1936 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1937 {
1938 int i, err = 0;
1939
1940 for (i = 0; i < adapter->num_active_queues; i++) {
1941 adapter->tx_rings[i].count = adapter->tx_desc_count;
1942 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
1943 if (!err)
1944 continue;
1945 dev_err(&adapter->pdev->dev,
1946 "Allocation for Tx Queue %u failed\n", i);
1947 break;
1948 }
1949
1950 return err;
1951 }
1952
1953 /**
1954 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1955 * @adapter: board private structure
1956 *
1957 * If this function returns with an error, then it's possible one or
1958 * more of the rings is populated (while the rest are not). It is the
1959 * callers duty to clean those orphaned rings.
1960 *
1961 * Return 0 on success, negative on failure
1962 **/
1963 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1964 {
1965 int i, err = 0;
1966
1967 for (i = 0; i < adapter->num_active_queues; i++) {
1968 adapter->rx_rings[i].count = adapter->rx_desc_count;
1969 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
1970 if (!err)
1971 continue;
1972 dev_err(&adapter->pdev->dev,
1973 "Allocation for Rx Queue %u failed\n", i);
1974 break;
1975 }
1976 return err;
1977 }
1978
1979 /**
1980 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1981 * @adapter: board private structure
1982 *
1983 * Free all receive software resources
1984 **/
1985 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1986 {
1987 int i;
1988
1989 if (!adapter->rx_rings)
1990 return;
1991
1992 for (i = 0; i < adapter->num_active_queues; i++)
1993 if (adapter->rx_rings[i].desc)
1994 i40evf_free_rx_resources(&adapter->rx_rings[i]);
1995 }
1996
1997 /**
1998 * i40evf_open - Called when a network interface is made active
1999 * @netdev: network interface device structure
2000 *
2001 * Returns 0 on success, negative value on failure
2002 *
2003 * The open entry point is called when a network interface is made
2004 * active by the system (IFF_UP). At this point all resources needed
2005 * for transmit and receive operations are allocated, the interrupt
2006 * handler is registered with the OS, the watchdog timer is started,
2007 * and the stack is notified that the interface is ready.
2008 **/
2009 static int i40evf_open(struct net_device *netdev)
2010 {
2011 struct i40evf_adapter *adapter = netdev_priv(netdev);
2012 int err;
2013
2014 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2015 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2016 return -EIO;
2017 }
2018
2019 if (adapter->state != __I40EVF_DOWN)
2020 return -EBUSY;
2021
2022 /* allocate transmit descriptors */
2023 err = i40evf_setup_all_tx_resources(adapter);
2024 if (err)
2025 goto err_setup_tx;
2026
2027 /* allocate receive descriptors */
2028 err = i40evf_setup_all_rx_resources(adapter);
2029 if (err)
2030 goto err_setup_rx;
2031
2032 /* clear any pending interrupts, may auto mask */
2033 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2034 if (err)
2035 goto err_req_irq;
2036
2037 i40evf_add_filter(adapter, adapter->hw.mac.addr);
2038 i40evf_configure(adapter);
2039
2040 err = i40evf_up_complete(adapter);
2041 if (err)
2042 goto err_req_irq;
2043
2044 i40evf_irq_enable(adapter, true);
2045
2046 return 0;
2047
2048 err_req_irq:
2049 i40evf_down(adapter);
2050 i40evf_free_traffic_irqs(adapter);
2051 err_setup_rx:
2052 i40evf_free_all_rx_resources(adapter);
2053 err_setup_tx:
2054 i40evf_free_all_tx_resources(adapter);
2055
2056 return err;
2057 }
2058
2059 /**
2060 * i40evf_close - Disables a network interface
2061 * @netdev: network interface device structure
2062 *
2063 * Returns 0, this is not allowed to fail
2064 *
2065 * The close entry point is called when an interface is de-activated
2066 * by the OS. The hardware is still under the drivers control, but
2067 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2068 * are freed, along with all transmit and receive resources.
2069 **/
2070 static int i40evf_close(struct net_device *netdev)
2071 {
2072 struct i40evf_adapter *adapter = netdev_priv(netdev);
2073
2074 if (adapter->state <= __I40EVF_DOWN_PENDING)
2075 return 0;
2076
2077
2078 set_bit(__I40E_DOWN, &adapter->vsi.state);
2079
2080 i40evf_down(adapter);
2081 adapter->state = __I40EVF_DOWN_PENDING;
2082 i40evf_free_traffic_irqs(adapter);
2083
2084 return 0;
2085 }
2086
2087 /**
2088 * i40evf_get_stats - Get System Network Statistics
2089 * @netdev: network interface device structure
2090 *
2091 * Returns the address of the device statistics structure.
2092 * The statistics are actually updated from the timer callback.
2093 **/
2094 static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2095 {
2096 struct i40evf_adapter *adapter = netdev_priv(netdev);
2097
2098 /* only return the current stats */
2099 return &adapter->net_stats;
2100 }
2101
2102 /**
2103 * i40evf_change_mtu - Change the Maximum Transfer Unit
2104 * @netdev: network interface device structure
2105 * @new_mtu: new value for maximum frame size
2106 *
2107 * Returns 0 on success, negative on failure
2108 **/
2109 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2110 {
2111 struct i40evf_adapter *adapter = netdev_priv(netdev);
2112 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2113
2114 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2115 return -EINVAL;
2116
2117 netdev->mtu = new_mtu;
2118 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2119 schedule_work(&adapter->reset_task);
2120
2121 return 0;
2122 }
2123
2124 #define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2125 NETIF_F_HW_VLAN_CTAG_RX |\
2126 NETIF_F_HW_VLAN_CTAG_FILTER)
2127
2128 /**
2129 * i40evf_fix_features - fix up the netdev feature bits
2130 * @netdev: our net device
2131 * @features: desired feature bits
2132 *
2133 * Returns fixed-up features bits
2134 **/
2135 static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2136 netdev_features_t features)
2137 {
2138 struct i40evf_adapter *adapter = netdev_priv(netdev);
2139
2140 features &= ~I40EVF_VLAN_FEATURES;
2141 if (adapter->vf_res->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN)
2142 features |= I40EVF_VLAN_FEATURES;
2143 return features;
2144 }
2145
2146 static const struct net_device_ops i40evf_netdev_ops = {
2147 .ndo_open = i40evf_open,
2148 .ndo_stop = i40evf_close,
2149 .ndo_start_xmit = i40evf_xmit_frame,
2150 .ndo_get_stats = i40evf_get_stats,
2151 .ndo_set_rx_mode = i40evf_set_rx_mode,
2152 .ndo_validate_addr = eth_validate_addr,
2153 .ndo_set_mac_address = i40evf_set_mac,
2154 .ndo_change_mtu = i40evf_change_mtu,
2155 .ndo_tx_timeout = i40evf_tx_timeout,
2156 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2157 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
2158 .ndo_fix_features = i40evf_fix_features,
2159 #ifdef CONFIG_NET_POLL_CONTROLLER
2160 .ndo_poll_controller = i40evf_netpoll,
2161 #endif
2162 };
2163
2164 /**
2165 * i40evf_check_reset_complete - check that VF reset is complete
2166 * @hw: pointer to hw struct
2167 *
2168 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2169 **/
2170 static int i40evf_check_reset_complete(struct i40e_hw *hw)
2171 {
2172 u32 rstat;
2173 int i;
2174
2175 for (i = 0; i < 100; i++) {
2176 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2177 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2178 if ((rstat == I40E_VFR_VFACTIVE) ||
2179 (rstat == I40E_VFR_COMPLETED))
2180 return 0;
2181 usleep_range(10, 20);
2182 }
2183 return -EBUSY;
2184 }
2185
2186 /**
2187 * i40evf_process_config - Process the config information we got from the PF
2188 * @adapter: board private structure
2189 *
2190 * Verify that we have a valid config struct, and set up our netdev features
2191 * and our VSI struct.
2192 **/
2193 int i40evf_process_config(struct i40evf_adapter *adapter)
2194 {
2195 struct i40e_virtchnl_vf_resource *vfres = adapter->vf_res;
2196 struct net_device *netdev = adapter->netdev;
2197 struct i40e_vsi *vsi = &adapter->vsi;
2198 int i;
2199
2200 /* got VF config message back from PF, now we can parse it */
2201 for (i = 0; i < vfres->num_vsis; i++) {
2202 if (vfres->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2203 adapter->vsi_res = &vfres->vsi_res[i];
2204 }
2205 if (!adapter->vsi_res) {
2206 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2207 return -ENODEV;
2208 }
2209
2210 netdev->hw_enc_features |= NETIF_F_SG |
2211 NETIF_F_IP_CSUM |
2212 NETIF_F_IPV6_CSUM |
2213 NETIF_F_HIGHDMA |
2214 NETIF_F_SOFT_FEATURES |
2215 NETIF_F_TSO |
2216 NETIF_F_TSO_ECN |
2217 NETIF_F_TSO6 |
2218 NETIF_F_GSO_GRE |
2219 NETIF_F_GSO_GRE_CSUM |
2220 NETIF_F_GSO_IPIP |
2221 NETIF_F_GSO_SIT |
2222 NETIF_F_GSO_UDP_TUNNEL |
2223 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2224 NETIF_F_GSO_PARTIAL |
2225 NETIF_F_SCTP_CRC |
2226 NETIF_F_RXHASH |
2227 NETIF_F_RXCSUM |
2228 0;
2229
2230 if (!(adapter->flags & I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE))
2231 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2232
2233 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2234
2235 /* record features VLANs can make use of */
2236 netdev->vlan_features |= netdev->hw_enc_features |
2237 NETIF_F_TSO_MANGLEID;
2238
2239 /* Write features and hw_features separately to avoid polluting
2240 * with, or dropping, features that are set when we registgered.
2241 */
2242 netdev->hw_features |= netdev->hw_enc_features;
2243
2244 netdev->features |= netdev->hw_enc_features | I40EVF_VLAN_FEATURES;
2245 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2246
2247 /* disable VLAN features if not supported */
2248 if (!(vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN))
2249 netdev->features ^= I40EVF_VLAN_FEATURES;
2250
2251 adapter->vsi.id = adapter->vsi_res->vsi_id;
2252
2253 adapter->vsi.back = adapter;
2254 adapter->vsi.base_vector = 1;
2255 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2256 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2257 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2258 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2259 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2260 vsi->netdev = adapter->netdev;
2261 vsi->qs_handle = adapter->vsi_res->qset_handle;
2262 if (vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2263 adapter->rss_key_size = vfres->rss_key_size;
2264 adapter->rss_lut_size = vfres->rss_lut_size;
2265 } else {
2266 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2267 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2268 }
2269
2270 return 0;
2271 }
2272
2273 /**
2274 * i40evf_init_task - worker thread to perform delayed initialization
2275 * @work: pointer to work_struct containing our data
2276 *
2277 * This task completes the work that was begun in probe. Due to the nature
2278 * of VF-PF communications, we may need to wait tens of milliseconds to get
2279 * responses back from the PF. Rather than busy-wait in probe and bog down the
2280 * whole system, we'll do it in a task so we can sleep.
2281 * This task only runs during driver init. Once we've established
2282 * communications with the PF driver and set up our netdev, the watchdog
2283 * takes over.
2284 **/
2285 static void i40evf_init_task(struct work_struct *work)
2286 {
2287 struct i40evf_adapter *adapter = container_of(work,
2288 struct i40evf_adapter,
2289 init_task.work);
2290 struct net_device *netdev = adapter->netdev;
2291 struct i40e_hw *hw = &adapter->hw;
2292 struct pci_dev *pdev = adapter->pdev;
2293 int err, bufsz;
2294
2295 switch (adapter->state) {
2296 case __I40EVF_STARTUP:
2297 /* driver loaded, probe complete */
2298 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2299 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2300 err = i40e_set_mac_type(hw);
2301 if (err) {
2302 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2303 err);
2304 goto err;
2305 }
2306 err = i40evf_check_reset_complete(hw);
2307 if (err) {
2308 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2309 err);
2310 goto err;
2311 }
2312 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2313 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2314 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2315 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2316
2317 err = i40evf_init_adminq(hw);
2318 if (err) {
2319 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2320 err);
2321 goto err;
2322 }
2323 err = i40evf_send_api_ver(adapter);
2324 if (err) {
2325 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2326 i40evf_shutdown_adminq(hw);
2327 goto err;
2328 }
2329 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2330 goto restart;
2331 case __I40EVF_INIT_VERSION_CHECK:
2332 if (!i40evf_asq_done(hw)) {
2333 dev_err(&pdev->dev, "Admin queue command never completed\n");
2334 i40evf_shutdown_adminq(hw);
2335 adapter->state = __I40EVF_STARTUP;
2336 goto err;
2337 }
2338
2339 /* aq msg sent, awaiting reply */
2340 err = i40evf_verify_api_ver(adapter);
2341 if (err) {
2342 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2343 err = i40evf_send_api_ver(adapter);
2344 else
2345 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2346 adapter->pf_version.major,
2347 adapter->pf_version.minor,
2348 I40E_VIRTCHNL_VERSION_MAJOR,
2349 I40E_VIRTCHNL_VERSION_MINOR);
2350 goto err;
2351 }
2352 err = i40evf_send_vf_config_msg(adapter);
2353 if (err) {
2354 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2355 err);
2356 goto err;
2357 }
2358 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2359 goto restart;
2360 case __I40EVF_INIT_GET_RESOURCES:
2361 /* aq msg sent, awaiting reply */
2362 if (!adapter->vf_res) {
2363 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2364 (I40E_MAX_VF_VSI *
2365 sizeof(struct i40e_virtchnl_vsi_resource));
2366 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2367 if (!adapter->vf_res)
2368 goto err;
2369 }
2370 err = i40evf_get_vf_config(adapter);
2371 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2372 err = i40evf_send_vf_config_msg(adapter);
2373 goto err;
2374 } else if (err == I40E_ERR_PARAM) {
2375 /* We only get ERR_PARAM if the device is in a very bad
2376 * state or if we've been disabled for previous bad
2377 * behavior. Either way, we're done now.
2378 */
2379 i40evf_shutdown_adminq(hw);
2380 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2381 return;
2382 }
2383 if (err) {
2384 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2385 err);
2386 goto err_alloc;
2387 }
2388 adapter->state = __I40EVF_INIT_SW;
2389 break;
2390 default:
2391 goto err_alloc;
2392 }
2393
2394 if (hw->mac.type == I40E_MAC_X722_VF)
2395 adapter->flags |= I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE;
2396
2397 if (i40evf_process_config(adapter))
2398 goto err_alloc;
2399 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
2400
2401 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2402
2403 netdev->netdev_ops = &i40evf_netdev_ops;
2404 i40evf_set_ethtool_ops(netdev);
2405 netdev->watchdog_timeo = 5 * HZ;
2406
2407 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2408 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2409 adapter->hw.mac.addr);
2410 eth_hw_addr_random(netdev);
2411 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2412 } else {
2413 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2414 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2415 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2416 }
2417
2418 init_timer(&adapter->watchdog_timer);
2419 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2420 adapter->watchdog_timer.data = (unsigned long)adapter;
2421 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2422
2423 adapter->num_active_queues = min_t(int,
2424 adapter->vsi_res->num_queue_pairs,
2425 (int)(num_online_cpus()));
2426 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2427 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2428 err = i40evf_init_interrupt_scheme(adapter);
2429 if (err)
2430 goto err_sw_init;
2431 i40evf_map_rings_to_vectors(adapter);
2432 if (adapter->vf_res->vf_offload_flags &
2433 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2434 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2435
2436 err = i40evf_request_misc_irq(adapter);
2437 if (err)
2438 goto err_sw_init;
2439
2440 netif_carrier_off(netdev);
2441
2442 if (!adapter->netdev_registered) {
2443 err = register_netdev(netdev);
2444 if (err)
2445 goto err_register;
2446 }
2447
2448 adapter->netdev_registered = true;
2449
2450 netif_tx_stop_all_queues(netdev);
2451
2452 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2453 if (netdev->features & NETIF_F_GRO)
2454 dev_info(&pdev->dev, "GRO is enabled\n");
2455
2456 adapter->state = __I40EVF_DOWN;
2457 set_bit(__I40E_DOWN, &adapter->vsi.state);
2458 i40evf_misc_irq_enable(adapter);
2459
2460 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2461 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2462 if (!adapter->rss_key || !adapter->rss_lut)
2463 goto err_mem;
2464
2465 if (RSS_AQ(adapter)) {
2466 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2467 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2468 } else {
2469 i40evf_init_rss(adapter);
2470 }
2471 return;
2472 restart:
2473 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
2474 return;
2475 err_mem:
2476 i40evf_free_rss(adapter);
2477 err_register:
2478 i40evf_free_misc_irq(adapter);
2479 err_sw_init:
2480 i40evf_reset_interrupt_capability(adapter);
2481 err_alloc:
2482 kfree(adapter->vf_res);
2483 adapter->vf_res = NULL;
2484 err:
2485 /* Things went into the weeds, so try again later */
2486 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2487 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
2488 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2489 i40evf_shutdown_adminq(hw);
2490 adapter->state = __I40EVF_STARTUP;
2491 schedule_delayed_work(&adapter->init_task, HZ * 5);
2492 return;
2493 }
2494 schedule_delayed_work(&adapter->init_task, HZ);
2495 }
2496
2497 /**
2498 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2499 * @pdev: pci device structure
2500 **/
2501 static void i40evf_shutdown(struct pci_dev *pdev)
2502 {
2503 struct net_device *netdev = pci_get_drvdata(pdev);
2504 struct i40evf_adapter *adapter = netdev_priv(netdev);
2505
2506 netif_device_detach(netdev);
2507
2508 if (netif_running(netdev))
2509 i40evf_close(netdev);
2510
2511 /* Prevent the watchdog from running. */
2512 adapter->state = __I40EVF_REMOVE;
2513 adapter->aq_required = 0;
2514
2515 #ifdef CONFIG_PM
2516 pci_save_state(pdev);
2517
2518 #endif
2519 pci_disable_device(pdev);
2520 }
2521
2522 /**
2523 * i40evf_probe - Device Initialization Routine
2524 * @pdev: PCI device information struct
2525 * @ent: entry in i40evf_pci_tbl
2526 *
2527 * Returns 0 on success, negative on failure
2528 *
2529 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2530 * The OS initialization, configuring of the adapter private structure,
2531 * and a hardware reset occur.
2532 **/
2533 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2534 {
2535 struct net_device *netdev;
2536 struct i40evf_adapter *adapter = NULL;
2537 struct i40e_hw *hw = NULL;
2538 int err;
2539
2540 err = pci_enable_device(pdev);
2541 if (err)
2542 return err;
2543
2544 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2545 if (err) {
2546 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2547 if (err) {
2548 dev_err(&pdev->dev,
2549 "DMA configuration failed: 0x%x\n", err);
2550 goto err_dma;
2551 }
2552 }
2553
2554 err = pci_request_regions(pdev, i40evf_driver_name);
2555 if (err) {
2556 dev_err(&pdev->dev,
2557 "pci_request_regions failed 0x%x\n", err);
2558 goto err_pci_reg;
2559 }
2560
2561 pci_enable_pcie_error_reporting(pdev);
2562
2563 pci_set_master(pdev);
2564
2565 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
2566 if (!netdev) {
2567 err = -ENOMEM;
2568 goto err_alloc_etherdev;
2569 }
2570
2571 SET_NETDEV_DEV(netdev, &pdev->dev);
2572
2573 pci_set_drvdata(pdev, netdev);
2574 adapter = netdev_priv(netdev);
2575
2576 adapter->netdev = netdev;
2577 adapter->pdev = pdev;
2578
2579 hw = &adapter->hw;
2580 hw->back = adapter;
2581
2582 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2583 adapter->state = __I40EVF_STARTUP;
2584
2585 /* Call save state here because it relies on the adapter struct. */
2586 pci_save_state(pdev);
2587
2588 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2589 pci_resource_len(pdev, 0));
2590 if (!hw->hw_addr) {
2591 err = -EIO;
2592 goto err_ioremap;
2593 }
2594 hw->vendor_id = pdev->vendor;
2595 hw->device_id = pdev->device;
2596 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2597 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2598 hw->subsystem_device_id = pdev->subsystem_device;
2599 hw->bus.device = PCI_SLOT(pdev->devfn);
2600 hw->bus.func = PCI_FUNC(pdev->devfn);
2601
2602 /* set up the locks for the AQ, do this only once in probe
2603 * and destroy them only once in remove
2604 */
2605 mutex_init(&hw->aq.asq_mutex);
2606 mutex_init(&hw->aq.arq_mutex);
2607
2608 INIT_LIST_HEAD(&adapter->mac_filter_list);
2609 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2610
2611 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2612 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2613 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2614 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2615 schedule_delayed_work(&adapter->init_task,
2616 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
2617
2618 return 0;
2619
2620 err_ioremap:
2621 free_netdev(netdev);
2622 err_alloc_etherdev:
2623 pci_release_regions(pdev);
2624 err_pci_reg:
2625 err_dma:
2626 pci_disable_device(pdev);
2627 return err;
2628 }
2629
2630 #ifdef CONFIG_PM
2631 /**
2632 * i40evf_suspend - Power management suspend routine
2633 * @pdev: PCI device information struct
2634 * @state: unused
2635 *
2636 * Called when the system (VM) is entering sleep/suspend.
2637 **/
2638 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2639 {
2640 struct net_device *netdev = pci_get_drvdata(pdev);
2641 struct i40evf_adapter *adapter = netdev_priv(netdev);
2642 int retval = 0;
2643
2644 netif_device_detach(netdev);
2645
2646 if (netif_running(netdev)) {
2647 rtnl_lock();
2648 i40evf_down(adapter);
2649 rtnl_unlock();
2650 }
2651 i40evf_free_misc_irq(adapter);
2652 i40evf_reset_interrupt_capability(adapter);
2653
2654 retval = pci_save_state(pdev);
2655 if (retval)
2656 return retval;
2657
2658 pci_disable_device(pdev);
2659
2660 return 0;
2661 }
2662
2663 /**
2664 * i40evf_resume - Power management resume routine
2665 * @pdev: PCI device information struct
2666 *
2667 * Called when the system (VM) is resumed from sleep/suspend.
2668 **/
2669 static int i40evf_resume(struct pci_dev *pdev)
2670 {
2671 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2672 struct net_device *netdev = adapter->netdev;
2673 u32 err;
2674
2675 pci_set_power_state(pdev, PCI_D0);
2676 pci_restore_state(pdev);
2677 /* pci_restore_state clears dev->state_saved so call
2678 * pci_save_state to restore it.
2679 */
2680 pci_save_state(pdev);
2681
2682 err = pci_enable_device_mem(pdev);
2683 if (err) {
2684 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2685 return err;
2686 }
2687 pci_set_master(pdev);
2688
2689 rtnl_lock();
2690 err = i40evf_set_interrupt_capability(adapter);
2691 if (err) {
2692 rtnl_unlock();
2693 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2694 return err;
2695 }
2696 err = i40evf_request_misc_irq(adapter);
2697 rtnl_unlock();
2698 if (err) {
2699 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2700 return err;
2701 }
2702
2703 schedule_work(&adapter->reset_task);
2704
2705 netif_device_attach(netdev);
2706
2707 return err;
2708 }
2709
2710 #endif /* CONFIG_PM */
2711 /**
2712 * i40evf_remove - Device Removal Routine
2713 * @pdev: PCI device information struct
2714 *
2715 * i40evf_remove is called by the PCI subsystem to alert the driver
2716 * that it should release a PCI device. The could be caused by a
2717 * Hot-Plug event, or because the driver is going to be removed from
2718 * memory.
2719 **/
2720 static void i40evf_remove(struct pci_dev *pdev)
2721 {
2722 struct net_device *netdev = pci_get_drvdata(pdev);
2723 struct i40evf_adapter *adapter = netdev_priv(netdev);
2724 struct i40evf_mac_filter *f, *ftmp;
2725 struct i40e_hw *hw = &adapter->hw;
2726
2727 cancel_delayed_work_sync(&adapter->init_task);
2728 cancel_work_sync(&adapter->reset_task);
2729
2730 if (adapter->netdev_registered) {
2731 unregister_netdev(netdev);
2732 adapter->netdev_registered = false;
2733 }
2734
2735 /* Shut down all the garbage mashers on the detention level */
2736 adapter->state = __I40EVF_REMOVE;
2737 adapter->aq_required = 0;
2738 i40evf_request_reset(adapter);
2739 msleep(50);
2740 /* If the FW isn't responding, kick it once, but only once. */
2741 if (!i40evf_asq_done(hw)) {
2742 i40evf_request_reset(adapter);
2743 msleep(50);
2744 }
2745
2746 if (adapter->msix_entries) {
2747 i40evf_misc_irq_disable(adapter);
2748 i40evf_free_misc_irq(adapter);
2749 i40evf_reset_interrupt_capability(adapter);
2750 i40evf_free_q_vectors(adapter);
2751 }
2752
2753 if (adapter->watchdog_timer.function)
2754 del_timer_sync(&adapter->watchdog_timer);
2755
2756 flush_scheduled_work();
2757
2758 i40evf_free_rss(adapter);
2759
2760 if (hw->aq.asq.count)
2761 i40evf_shutdown_adminq(hw);
2762
2763 /* destroy the locks only once, here */
2764 mutex_destroy(&hw->aq.arq_mutex);
2765 mutex_destroy(&hw->aq.asq_mutex);
2766
2767 iounmap(hw->hw_addr);
2768 pci_release_regions(pdev);
2769 i40evf_free_all_tx_resources(adapter);
2770 i40evf_free_all_rx_resources(adapter);
2771 i40evf_free_queues(adapter);
2772 kfree(adapter->vf_res);
2773 /* If we got removed before an up/down sequence, we've got a filter
2774 * hanging out there that we need to get rid of.
2775 */
2776 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2777 list_del(&f->list);
2778 kfree(f);
2779 }
2780 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2781 list_del(&f->list);
2782 kfree(f);
2783 }
2784
2785 free_netdev(netdev);
2786
2787 pci_disable_pcie_error_reporting(pdev);
2788
2789 pci_disable_device(pdev);
2790 }
2791
2792 static struct pci_driver i40evf_driver = {
2793 .name = i40evf_driver_name,
2794 .id_table = i40evf_pci_tbl,
2795 .probe = i40evf_probe,
2796 .remove = i40evf_remove,
2797 #ifdef CONFIG_PM
2798 .suspend = i40evf_suspend,
2799 .resume = i40evf_resume,
2800 #endif
2801 .shutdown = i40evf_shutdown,
2802 };
2803
2804 /**
2805 * i40e_init_module - Driver Registration Routine
2806 *
2807 * i40e_init_module is the first routine called when the driver is
2808 * loaded. All it does is register with the PCI subsystem.
2809 **/
2810 static int __init i40evf_init_module(void)
2811 {
2812 int ret;
2813
2814 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2815 i40evf_driver_version);
2816
2817 pr_info("%s\n", i40evf_copyright);
2818
2819 i40evf_wq = create_singlethread_workqueue(i40evf_driver_name);
2820 if (!i40evf_wq) {
2821 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2822 return -ENOMEM;
2823 }
2824 ret = pci_register_driver(&i40evf_driver);
2825 return ret;
2826 }
2827
2828 module_init(i40evf_init_module);
2829
2830 /**
2831 * i40e_exit_module - Driver Exit Cleanup Routine
2832 *
2833 * i40e_exit_module is called just before the driver is removed
2834 * from memory.
2835 **/
2836 static void __exit i40evf_exit_module(void)
2837 {
2838 pci_unregister_driver(&i40evf_driver);
2839 destroy_workqueue(i40evf_wq);
2840 }
2841
2842 module_exit(i40evf_exit_module);
2843
2844 /* i40evf_main.c */
This page took 0.158278 seconds and 6 git commands to generate.