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