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