i40evf: request reset on tx hang
[deliverable/linux.git] / drivers / net / ethernet / intel / i40evf / i40evf_main.c
CommitLineData
5eae00c5
GR
1/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
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
218 for (i = 1; i < adapter->num_msix_vectors; i++) {
219 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
220 synchronize_irq(adapter->msix_entries[i].vector);
221 }
222 /* read flush */
223 rd32(hw, I40E_VFGEN_RSTAT);
224
225}
226
227/**
228 * i40evf_irq_enable_queues - Enable interrupt for specified queues
229 * @adapter: board private structure
230 * @mask: bitmap of queues to enable
231 **/
232void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
233{
234 struct i40e_hw *hw = &adapter->hw;
235 int i;
236
237 for (i = 1; i < adapter->num_msix_vectors; i++) {
238 if (mask & (1 << (i - 1))) {
239 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
240 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
241 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
242 }
243 }
244}
245
246/**
247 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
248 * @adapter: board private structure
249 * @mask: bitmap of vectors to trigger
250 **/
251static void i40evf_fire_sw_int(struct i40evf_adapter *adapter,
252 u32 mask)
253{
254 struct i40e_hw *hw = &adapter->hw;
255 int i;
256 uint32_t dyn_ctl;
257
258 for (i = 1; i < adapter->num_msix_vectors; i++) {
259 if (mask & (1 << i)) {
260 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
261 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
262 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
263 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
264 }
265 }
266}
267
268/**
269 * i40evf_irq_enable - Enable default interrupt generation settings
270 * @adapter: board private structure
271 **/
272void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
273{
274 struct i40e_hw *hw = &adapter->hw;
275
276 i40evf_irq_enable_queues(adapter, ~0);
277
278 if (flush)
279 rd32(hw, I40E_VFGEN_RSTAT);
280}
281
282/**
283 * i40evf_msix_aq - Interrupt handler for vector 0
284 * @irq: interrupt number
285 * @data: pointer to netdev
286 **/
287static irqreturn_t i40evf_msix_aq(int irq, void *data)
288{
289 struct net_device *netdev = data;
290 struct i40evf_adapter *adapter = netdev_priv(netdev);
291 struct i40e_hw *hw = &adapter->hw;
292 u32 val;
293 u32 ena_mask;
294
295 /* handle non-queue interrupts */
296 val = rd32(hw, I40E_VFINT_ICR01);
297 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
298
299
300 val = rd32(hw, I40E_VFINT_DYN_CTL01);
301 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
302 wr32(hw, I40E_VFINT_DYN_CTL01, val);
303
304 /* re-enable interrupt causes */
305 wr32(hw, I40E_VFINT_ICR0_ENA1, ena_mask);
306 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK);
307
308 /* schedule work on the private workqueue */
309 schedule_work(&adapter->adminq_task);
310
311 return IRQ_HANDLED;
312}
313
314/**
315 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
316 * @irq: interrupt number
317 * @data: pointer to a q_vector
318 **/
319static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
320{
321 struct i40e_q_vector *q_vector = data;
322
323 if (!q_vector->tx.ring && !q_vector->rx.ring)
324 return IRQ_HANDLED;
325
326 napi_schedule(&q_vector->napi);
327
328 return IRQ_HANDLED;
329}
330
331/**
332 * i40evf_map_vector_to_rxq - associate irqs with rx queues
333 * @adapter: board private structure
334 * @v_idx: interrupt number
335 * @r_idx: queue number
336 **/
337static void
338i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
339{
340 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
341 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
342
343 rx_ring->q_vector = q_vector;
344 rx_ring->next = q_vector->rx.ring;
345 rx_ring->vsi = &adapter->vsi;
346 q_vector->rx.ring = rx_ring;
347 q_vector->rx.count++;
348 q_vector->rx.latency_range = I40E_LOW_LATENCY;
349}
350
351/**
352 * i40evf_map_vector_to_txq - associate irqs with tx queues
353 * @adapter: board private structure
354 * @v_idx: interrupt number
355 * @t_idx: queue number
356 **/
357static void
358i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
359{
360 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
361 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
362
363 tx_ring->q_vector = q_vector;
364 tx_ring->next = q_vector->tx.ring;
365 tx_ring->vsi = &adapter->vsi;
366 q_vector->tx.ring = tx_ring;
367 q_vector->tx.count++;
368 q_vector->tx.latency_range = I40E_LOW_LATENCY;
369 q_vector->num_ringpairs++;
370 q_vector->ring_mask |= (1 << t_idx);
371}
372
373/**
374 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
375 * @adapter: board private structure to initialize
376 *
377 * This function maps descriptor rings to the queue-specific vectors
378 * we were allotted through the MSI-X enabling code. Ideally, we'd have
379 * one vector per ring/queue, but on a constrained vector budget, we
380 * group the rings as "efficiently" as possible. You would add new
381 * mapping configurations in here.
382 **/
383static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
384{
385 int q_vectors;
386 int v_start = 0;
387 int rxr_idx = 0, txr_idx = 0;
388 int rxr_remaining = adapter->vsi_res->num_queue_pairs;
389 int txr_remaining = adapter->vsi_res->num_queue_pairs;
390 int i, j;
391 int rqpv, tqpv;
392 int err = 0;
393
394 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
395
396 /* The ideal configuration...
397 * We have enough vectors to map one per queue.
398 */
399 if (q_vectors == (rxr_remaining * 2)) {
400 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
401 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
402
403 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
404 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
405 goto out;
406 }
407
408 /* If we don't have enough vectors for a 1-to-1
409 * mapping, we'll have to group them so there are
410 * multiple queues per vector.
411 * Re-adjusting *qpv takes care of the remainder.
412 */
413 for (i = v_start; i < q_vectors; i++) {
414 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
415 for (j = 0; j < rqpv; j++) {
416 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
417 rxr_idx++;
418 rxr_remaining--;
419 }
420 }
421 for (i = v_start; i < q_vectors; i++) {
422 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
423 for (j = 0; j < tqpv; j++) {
424 i40evf_map_vector_to_txq(adapter, i, txr_idx);
425 txr_idx++;
426 txr_remaining--;
427 }
428 }
429
430out:
431 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
432
433 return err;
434}
435
436/**
437 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
438 * @adapter: board private structure
439 *
440 * Allocates MSI-X vectors for tx and rx handling, and requests
441 * interrupts from the kernel.
442 **/
443static int
444i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
445{
446 int vector, err, q_vectors;
447 int rx_int_idx = 0, tx_int_idx = 0;
448
449 i40evf_irq_disable(adapter);
450 /* Decrement for Other and TCP Timer vectors */
451 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
452
453 for (vector = 0; vector < q_vectors; vector++) {
454 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
455
456 if (q_vector->tx.ring && q_vector->rx.ring) {
457 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
458 "i40evf-%s-%s-%d", basename,
459 "TxRx", rx_int_idx++);
460 tx_int_idx++;
461 } else if (q_vector->rx.ring) {
462 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
463 "i40evf-%s-%s-%d", basename,
464 "rx", rx_int_idx++);
465 } else if (q_vector->tx.ring) {
466 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
467 "i40evf-%s-%s-%d", basename,
468 "tx", tx_int_idx++);
469 } else {
470 /* skip this unused q_vector */
471 continue;
472 }
473 err = request_irq(
474 adapter->msix_entries[vector + NONQ_VECS].vector,
475 i40evf_msix_clean_rings,
476 0,
477 q_vector->name,
478 q_vector);
479 if (err) {
480 dev_info(&adapter->pdev->dev,
481 "%s: request_irq failed, error: %d\n",
482 __func__, err);
483 goto free_queue_irqs;
484 }
485 /* assign the mask for this irq */
486 irq_set_affinity_hint(
487 adapter->msix_entries[vector + NONQ_VECS].vector,
488 q_vector->affinity_mask);
489 }
490
491 return 0;
492
493free_queue_irqs:
494 while (vector) {
495 vector--;
496 irq_set_affinity_hint(
497 adapter->msix_entries[vector + NONQ_VECS].vector,
498 NULL);
499 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
500 adapter->q_vector[vector]);
501 }
502 return err;
503}
504
505/**
506 * i40evf_request_misc_irq - Initialize MSI-X interrupts
507 * @adapter: board private structure
508 *
509 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
510 * vector is only for the admin queue, and stays active even when the netdev
511 * is closed.
512 **/
513static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
514{
515 struct net_device *netdev = adapter->netdev;
516 int err;
517
e1dfee8e 518 sprintf(adapter->misc_vector_name, "i40evf:mbx");
5eae00c5 519 err = request_irq(adapter->msix_entries[0].vector,
e1dfee8e
MW
520 &i40evf_msix_aq, 0,
521 adapter->misc_vector_name, netdev);
5eae00c5
GR
522 if (err) {
523 dev_err(&adapter->pdev->dev,
524 "request_irq for msix_aq failed: %d\n", err);
525 free_irq(adapter->msix_entries[0].vector, netdev);
526 }
527 return err;
528}
529
530/**
531 * i40evf_free_traffic_irqs - Free MSI-X interrupts
532 * @adapter: board private structure
533 *
534 * Frees all MSI-X vectors other than 0.
535 **/
536static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
537{
538 int i;
539 int q_vectors;
540 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
541
542 for (i = 0; i < q_vectors; i++) {
543 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
544 NULL);
545 free_irq(adapter->msix_entries[i+1].vector,
546 adapter->q_vector[i]);
547 }
548}
549
550/**
551 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
552 * @adapter: board private structure
553 *
554 * Frees MSI-X vector 0.
555 **/
556static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
557{
558 struct net_device *netdev = adapter->netdev;
559
560 free_irq(adapter->msix_entries[0].vector, netdev);
561}
562
563/**
564 * i40evf_configure_tx - Configure Transmit Unit after Reset
565 * @adapter: board private structure
566 *
567 * Configure the Tx unit of the MAC after a reset.
568 **/
569static void i40evf_configure_tx(struct i40evf_adapter *adapter)
570{
571 struct i40e_hw *hw = &adapter->hw;
572 int i;
573 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
574 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
575}
576
577/**
578 * i40evf_configure_rx - Configure Receive Unit after Reset
579 * @adapter: board private structure
580 *
581 * Configure the Rx unit of the MAC after a reset.
582 **/
583static void i40evf_configure_rx(struct i40evf_adapter *adapter)
584{
585 struct i40e_hw *hw = &adapter->hw;
586 struct net_device *netdev = adapter->netdev;
587 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
588 int i;
589 int rx_buf_len;
590
591
592 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
593 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
594
595 /* Decide whether to use packet split mode or not */
596 if (netdev->mtu > ETH_DATA_LEN) {
597 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
598 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
599 else
600 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
601 } else {
602 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
603 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
604 else
605 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
606 }
607
608 /* Set the RX buffer length according to the mode */
609 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
610 rx_buf_len = I40E_RX_HDR_SIZE;
611 } else {
612 if (netdev->mtu <= ETH_DATA_LEN)
613 rx_buf_len = I40EVF_RXBUFFER_2048;
614 else
615 rx_buf_len = ALIGN(max_frame, 1024);
616 }
617
618 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
619 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
620 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
621 }
622}
623
624/**
625 * i40evf_find_vlan - Search filter list for specific vlan filter
626 * @adapter: board private structure
627 * @vlan: vlan tag
628 *
629 * Returns ptr to the filter object or NULL
630 **/
631static struct
632i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
633{
634 struct i40evf_vlan_filter *f;
635
636 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
637 if (vlan == f->vlan)
638 return f;
639 }
640 return NULL;
641}
642
643/**
644 * i40evf_add_vlan - Add a vlan filter to the list
645 * @adapter: board private structure
646 * @vlan: VLAN tag
647 *
648 * Returns ptr to the filter object or NULL when no memory available.
649 **/
650static struct
651i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
652{
653 struct i40evf_vlan_filter *f;
654
655 f = i40evf_find_vlan(adapter, vlan);
656 if (NULL == f) {
657 f = kzalloc(sizeof(*f), GFP_ATOMIC);
658 if (NULL == f) {
659 dev_info(&adapter->pdev->dev,
660 "%s: no memory for new VLAN filter\n",
661 __func__);
662 return NULL;
663 }
664 f->vlan = vlan;
665
666 INIT_LIST_HEAD(&f->list);
667 list_add(&f->list, &adapter->vlan_filter_list);
668 f->add = true;
669 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
670 }
671
672 return f;
673}
674
675/**
676 * i40evf_del_vlan - Remove a vlan filter from the list
677 * @adapter: board private structure
678 * @vlan: VLAN tag
679 **/
680static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
681{
682 struct i40evf_vlan_filter *f;
683
684 f = i40evf_find_vlan(adapter, vlan);
685 if (f) {
686 f->remove = true;
687 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
688 }
689 return;
690}
691
692/**
693 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
694 * @netdev: network device struct
695 * @vid: VLAN tag
696 **/
697static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
698 __always_unused __be16 proto, u16 vid)
699{
700 struct i40evf_adapter *adapter = netdev_priv(netdev);
701
702 if (i40evf_add_vlan(adapter, vid) == NULL)
703 return -ENOMEM;
704 return 0;
705}
706
707/**
708 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
709 * @netdev: network device struct
710 * @vid: VLAN tag
711 **/
712static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
713 __always_unused __be16 proto, u16 vid)
714{
715 struct i40evf_adapter *adapter = netdev_priv(netdev);
716
717 i40evf_del_vlan(adapter, vid);
718 return 0;
719}
720
721/**
722 * i40evf_find_filter - Search filter list for specific mac filter
723 * @adapter: board private structure
724 * @macaddr: the MAC address
725 *
726 * Returns ptr to the filter object or NULL
727 **/
728static struct
729i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
730 u8 *macaddr)
731{
732 struct i40evf_mac_filter *f;
733
734 if (!macaddr)
735 return NULL;
736
737 list_for_each_entry(f, &adapter->mac_filter_list, list) {
738 if (ether_addr_equal(macaddr, f->macaddr))
739 return f;
740 }
741 return NULL;
742}
743
744/**
745 * i40e_add_filter - Add a mac filter to the filter list
746 * @adapter: board private structure
747 * @macaddr: the MAC address
748 *
749 * Returns ptr to the filter object or NULL when no memory available.
750 **/
751static struct
752i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
753 u8 *macaddr)
754{
755 struct i40evf_mac_filter *f;
756
757 if (!macaddr)
758 return NULL;
759
760 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
761 &adapter->crit_section))
762 mdelay(1);
763
764 f = i40evf_find_filter(adapter, macaddr);
765 if (NULL == f) {
766 f = kzalloc(sizeof(*f), GFP_ATOMIC);
767 if (NULL == f) {
768 dev_info(&adapter->pdev->dev,
769 "%s: no memory for new filter\n", __func__);
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
842 if (f->macaddr[0] & 0x01) {
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
ef8693eb 971 /* remove all MAC filters */
5eae00c5
GR
972 list_for_each_entry(f, &adapter->mac_filter_list, list) {
973 f->remove = true;
974 }
ef8693eb
MW
975 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
976 adapter->state != __I40EVF_RESETTING) {
977 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
978 /* disable receives */
979 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
980 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
981 msleep(20);
982 }
5eae00c5
GR
983 netif_tx_disable(netdev);
984
985 netif_tx_stop_all_queues(netdev);
986
987 i40evf_irq_disable(adapter);
988
989 i40evf_napi_disable_all(adapter);
990
991 netif_carrier_off(netdev);
992
993 i40evf_clean_all_tx_rings(adapter);
994 i40evf_clean_all_rx_rings(adapter);
995}
996
997/**
998 * i40evf_acquire_msix_vectors - Setup the MSIX capability
999 * @adapter: board private structure
1000 * @vectors: number of vectors to request
1001 *
1002 * Work with the OS to set up the MSIX vectors needed.
1003 *
1004 * Returns 0 on success, negative on failure
1005 **/
1006static int
1007i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1008{
1009 int err, vector_threshold;
1010
1011 /* We'll want at least 3 (vector_threshold):
1012 * 0) Other (Admin Queue and link, mostly)
1013 * 1) TxQ[0] Cleanup
1014 * 2) RxQ[0] Cleanup
1015 */
1016 vector_threshold = MIN_MSIX_COUNT;
1017
1018 /* The more we get, the more we will assign to Tx/Rx Cleanup
1019 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1020 * Right now, we simply care about how many we'll get; we'll
1021 * set them up later while requesting irq's.
1022 */
1023 while (vectors >= vector_threshold) {
1024 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1025 vectors);
1026 if (!err) /* Success in acquiring all requested vectors. */
1027 break;
1028 else if (err < 0)
1029 vectors = 0; /* Nasty failure, quit now */
1030 else /* err == number of vectors we should try again with */
1031 vectors = err;
1032 }
1033
1034 if (vectors < vector_threshold) {
1035 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts.\n");
1036 kfree(adapter->msix_entries);
1037 adapter->msix_entries = NULL;
1038 err = -EIO;
1039 } else {
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 = vectors;
1045 }
1046 return err;
1047}
1048
1049/**
1050 * i40evf_free_queues - Free memory for all rings
1051 * @adapter: board private structure to initialize
1052 *
1053 * Free all of the memory associated with queue pairs.
1054 **/
1055static void i40evf_free_queues(struct i40evf_adapter *adapter)
1056{
1057 int i;
1058
1059 if (!adapter->vsi_res)
1060 return;
1061 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1062 if (adapter->tx_rings[i])
1063 kfree_rcu(adapter->tx_rings[i], rcu);
1064 adapter->tx_rings[i] = NULL;
1065 adapter->rx_rings[i] = NULL;
1066 }
1067}
1068
1069/**
1070 * i40evf_alloc_queues - Allocate memory for all rings
1071 * @adapter: board private structure to initialize
1072 *
1073 * We allocate one ring per queue at run-time since we don't know the
1074 * number of queues at compile-time. The polling_netdev array is
1075 * intended for Multiqueue, but should work fine with a single queue.
1076 **/
1077static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1078{
1079 int i;
1080
1081 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1082 struct i40e_ring *tx_ring;
1083 struct i40e_ring *rx_ring;
1084
1085 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
1086 if (!tx_ring)
1087 goto err_out;
1088
1089 tx_ring->queue_index = i;
1090 tx_ring->netdev = adapter->netdev;
1091 tx_ring->dev = &adapter->pdev->dev;
1092 tx_ring->count = I40EVF_DEFAULT_TXD;
1093 adapter->tx_rings[i] = tx_ring;
1094
1095 rx_ring = &tx_ring[1];
1096 rx_ring->queue_index = i;
1097 rx_ring->netdev = adapter->netdev;
1098 rx_ring->dev = &adapter->pdev->dev;
1099 rx_ring->count = I40EVF_DEFAULT_RXD;
1100 adapter->rx_rings[i] = rx_ring;
1101 }
1102
1103 return 0;
1104
1105err_out:
1106 i40evf_free_queues(adapter);
1107 return -ENOMEM;
1108}
1109
1110/**
1111 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1112 * @adapter: board private structure to initialize
1113 *
1114 * Attempt to configure the interrupts using the best available
1115 * capabilities of the hardware and the kernel.
1116 **/
1117static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1118{
1119 int vector, v_budget;
1120 int pairs = 0;
1121 int err = 0;
1122
1123 if (!adapter->vsi_res) {
1124 err = -EIO;
1125 goto out;
1126 }
1127 pairs = adapter->vsi_res->num_queue_pairs;
1128
1129 /* It's easy to be greedy for MSI-X vectors, but it really
1130 * doesn't do us much good if we have a lot more vectors
1131 * than CPU's. So let's be conservative and only ask for
1132 * (roughly) twice the number of vectors as there are CPU's.
1133 */
1134 v_budget = min(pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1135 v_budget = min(v_budget, (int)adapter->vf_res->max_vectors + 1);
1136
1137 /* A failure in MSI-X entry allocation isn't fatal, but it does
1138 * mean we disable MSI-X capabilities of the adapter.
1139 */
1140 adapter->msix_entries = kcalloc(v_budget,
1141 sizeof(struct msix_entry), GFP_KERNEL);
1142 if (!adapter->msix_entries) {
1143 err = -ENOMEM;
1144 goto out;
1145 }
1146
1147 for (vector = 0; vector < v_budget; vector++)
1148 adapter->msix_entries[vector].entry = vector;
1149
1150 i40evf_acquire_msix_vectors(adapter, v_budget);
1151
1152out:
1153 adapter->netdev->real_num_tx_queues = pairs;
1154 return err;
1155}
1156
1157/**
1158 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1159 * @adapter: board private structure to initialize
1160 *
1161 * We allocate one q_vector per queue interrupt. If allocation fails we
1162 * return -ENOMEM.
1163 **/
1164static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1165{
1166 int q_idx, num_q_vectors;
1167 struct i40e_q_vector *q_vector;
1168
1169 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1170
1171 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1172 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
1173 if (!q_vector)
1174 goto err_out;
1175 q_vector->adapter = adapter;
1176 q_vector->vsi = &adapter->vsi;
1177 q_vector->v_idx = q_idx;
1178 netif_napi_add(adapter->netdev, &q_vector->napi,
1179 i40evf_napi_poll, 64);
1180 adapter->q_vector[q_idx] = q_vector;
1181 }
1182
1183 return 0;
1184
1185err_out:
1186 while (q_idx) {
1187 q_idx--;
1188 q_vector = adapter->q_vector[q_idx];
1189 netif_napi_del(&q_vector->napi);
1190 kfree(q_vector);
1191 adapter->q_vector[q_idx] = NULL;
1192 }
1193 return -ENOMEM;
1194}
1195
1196/**
1197 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1198 * @adapter: board private structure to initialize
1199 *
1200 * This function frees the memory allocated to the q_vectors. In addition if
1201 * NAPI is enabled it will delete any references to the NAPI struct prior
1202 * to freeing the q_vector.
1203 **/
1204static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1205{
1206 int q_idx, num_q_vectors;
1207 int napi_vectors;
1208
1209 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1210 napi_vectors = adapter->vsi_res->num_queue_pairs;
1211
1212 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1213 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1214
1215 adapter->q_vector[q_idx] = NULL;
1216 if (q_idx < napi_vectors)
1217 netif_napi_del(&q_vector->napi);
1218 kfree(q_vector);
1219 }
1220}
1221
1222/**
1223 * i40evf_reset_interrupt_capability - Reset MSIX setup
1224 * @adapter: board private structure
1225 *
1226 **/
1227void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1228{
1229 pci_disable_msix(adapter->pdev);
1230 kfree(adapter->msix_entries);
1231 adapter->msix_entries = NULL;
1232
1233 return;
1234}
1235
1236/**
1237 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1238 * @adapter: board private structure to initialize
1239 *
1240 **/
1241int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1242{
1243 int err;
1244
1245 err = i40evf_set_interrupt_capability(adapter);
1246 if (err) {
1247 dev_err(&adapter->pdev->dev,
1248 "Unable to setup interrupt capabilities\n");
1249 goto err_set_interrupt;
1250 }
1251
1252 err = i40evf_alloc_q_vectors(adapter);
1253 if (err) {
1254 dev_err(&adapter->pdev->dev,
1255 "Unable to allocate memory for queue vectors\n");
1256 goto err_alloc_q_vectors;
1257 }
1258
1259 err = i40evf_alloc_queues(adapter);
1260 if (err) {
1261 dev_err(&adapter->pdev->dev,
1262 "Unable to allocate memory for queues\n");
1263 goto err_alloc_queues;
1264 }
1265
1266 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1267 (adapter->vsi_res->num_queue_pairs > 1) ? "Enabled" :
1268 "Disabled", adapter->vsi_res->num_queue_pairs);
1269
1270 return 0;
1271err_alloc_queues:
1272 i40evf_free_q_vectors(adapter);
1273err_alloc_q_vectors:
1274 i40evf_reset_interrupt_capability(adapter);
1275err_set_interrupt:
1276 return err;
1277}
1278
1279/**
1280 * i40evf_watchdog_timer - Periodic call-back timer
1281 * @data: pointer to adapter disguised as unsigned long
1282 **/
1283static void i40evf_watchdog_timer(unsigned long data)
1284{
1285 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1286 schedule_work(&adapter->watchdog_task);
1287 /* timer will be rescheduled in watchdog task */
1288}
1289
1290/**
1291 * i40evf_watchdog_task - Periodic call-back task
1292 * @work: pointer to work_struct
1293 **/
1294static void i40evf_watchdog_task(struct work_struct *work)
1295{
1296 struct i40evf_adapter *adapter = container_of(work,
1297 struct i40evf_adapter,
1298 watchdog_task);
1299 struct i40e_hw *hw = &adapter->hw;
1300
ef8693eb
MW
1301 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1302 goto restart_watchdog;
1303
1304 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1305 dev_info(&adapter->pdev->dev, "Checking for redemption\n");
1306 if ((rd32(hw, I40E_VFGEN_RSTAT) & 0x3) == I40E_VFR_VFACTIVE) {
1307 /* A chance for redemption! */
1308 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1309 adapter->state = __I40EVF_STARTUP;
1310 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1311 schedule_delayed_work(&adapter->init_task, 10);
1312 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1313 &adapter->crit_section);
1314 /* Don't reschedule the watchdog, since we've restarted
1315 * the init task. When init_task contacts the PF and
1316 * gets everything set up again, it'll restart the
1317 * watchdog for us. Down, boy. Sit. Stay. Woof.
1318 */
1319 return;
1320 }
1321 adapter->aq_pending = 0;
1322 adapter->aq_required = 0;
1323 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
5eae00c5 1324 goto watchdog_done;
ef8693eb 1325 }
5eae00c5 1326
ef8693eb
MW
1327 if ((adapter->state < __I40EVF_DOWN) ||
1328 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
5eae00c5
GR
1329 goto watchdog_done;
1330
ef8693eb
MW
1331 /* check for reset */
1332 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
5eae00c5
GR
1333 (rd32(hw, I40E_VFGEN_RSTAT) & 0x3) != I40E_VFR_VFACTIVE) {
1334 adapter->state = __I40EVF_RESETTING;
ef8693eb
MW
1335 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1336 dev_err(&adapter->pdev->dev, "Hardware reset detected.\n");
1337 dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
5eae00c5 1338 schedule_work(&adapter->reset_task);
ef8693eb
MW
1339 adapter->aq_pending = 0;
1340 adapter->aq_required = 0;
1341 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
5eae00c5
GR
1342 goto watchdog_done;
1343 }
1344
1345 /* Process admin queue tasks. After init, everything gets done
1346 * here so we don't race on the admin queue.
1347 */
1348 if (adapter->aq_pending)
1349 goto watchdog_done;
1350
1351 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1352 i40evf_map_queues(adapter);
1353 goto watchdog_done;
1354 }
1355
1356 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1357 i40evf_add_ether_addrs(adapter);
1358 goto watchdog_done;
1359 }
1360
1361 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1362 i40evf_add_vlans(adapter);
1363 goto watchdog_done;
1364 }
1365
1366 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1367 i40evf_del_ether_addrs(adapter);
1368 goto watchdog_done;
1369 }
1370
1371 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1372 i40evf_del_vlans(adapter);
1373 goto watchdog_done;
1374 }
1375
1376 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1377 i40evf_disable_queues(adapter);
1378 goto watchdog_done;
1379 }
1380
1381 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1382 i40evf_configure_queues(adapter);
1383 goto watchdog_done;
1384 }
1385
1386 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1387 i40evf_enable_queues(adapter);
1388 goto watchdog_done;
1389 }
1390
1391 if (adapter->state == __I40EVF_RUNNING)
1392 i40evf_request_stats(adapter);
1393
1394 i40evf_irq_enable(adapter, true);
1395 i40evf_fire_sw_int(adapter, 0xFF);
ef8693eb 1396
5eae00c5 1397watchdog_done:
ef8693eb
MW
1398 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1399restart_watchdog:
5eae00c5
GR
1400 if (adapter->aq_required)
1401 mod_timer(&adapter->watchdog_timer,
1402 jiffies + msecs_to_jiffies(20));
1403 else
1404 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
5eae00c5
GR
1405 schedule_work(&adapter->adminq_task);
1406}
1407
1408/**
1409 * i40evf_configure_rss - Prepare for RSS if used
1410 * @adapter: board private structure
1411 **/
1412static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1413{
1414 struct i40e_hw *hw = &adapter->hw;
1415 u32 lut = 0;
1416 int i, j;
1417 u64 hena;
1418
1419 /* Set of random keys generated using kernel random number generator */
1420 static const u32 seed[I40E_VFQF_HKEY_MAX_INDEX + 1] = {
1421 0x794221b4, 0xbca0c5ab, 0x6cd5ebd9, 0x1ada6127,
1422 0x983b3aa1, 0x1c4e71eb, 0x7f6328b2, 0xfcdc0da0,
1423 0xc135cafa, 0x7a6f7e2d, 0xe7102d28, 0x163cd12e,
1424 0x4954b126 };
1425
1426 /* Hash type is configured by the PF - we just supply the key */
1427
1428 /* Fill out hash function seed */
1429 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1430 wr32(hw, I40E_VFQF_HKEY(i), seed[i]);
1431
1432 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1433 hena = I40E_DEFAULT_RSS_HENA;
1434 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1435 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1436
1437 /* Populate the LUT with max no. of queues in round robin fashion */
1438 for (i = 0, j = 0; i < I40E_VFQF_HLUT_MAX_INDEX; i++, j++) {
1439 if (j == adapter->vsi_res->num_queue_pairs)
1440 j = 0;
1441 /* lut = 4-byte sliding window of 4 lut entries */
1442 lut = (lut << 8) | (j &
1443 ((0x1 << 8) - 1));
1444 /* On i = 3, we have 4 entries in lut; write to the register */
1445 if ((i & 3) == 3)
1446 wr32(hw, I40E_VFQF_HLUT(i >> 2), lut);
1447 }
1448 i40e_flush(hw);
1449}
1450
ef8693eb
MW
1451#define I40EVF_RESET_WAIT_MS 100
1452#define I40EVF_RESET_WAIT_COUNT 200
5eae00c5
GR
1453/**
1454 * i40evf_reset_task - Call-back task to handle hardware reset
1455 * @work: pointer to work_struct
1456 *
1457 * During reset we need to shut down and reinitialize the admin queue
1458 * before we can use it to communicate with the PF again. We also clear
1459 * and reinit the rings because that context is lost as well.
1460 **/
1461static void i40evf_reset_task(struct work_struct *work)
1462{
ef8693eb
MW
1463 struct i40evf_adapter *adapter = container_of(work,
1464 struct i40evf_adapter,
1465 reset_task);
5eae00c5
GR
1466 struct i40e_hw *hw = &adapter->hw;
1467 int i = 0, err;
1468 uint32_t rstat_val;
1469
1470 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1471 &adapter->crit_section))
1472 udelay(500);
ef8693eb
MW
1473 /* poll until we see the reset actually happen */
1474 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1475 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1476 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1477 if (rstat_val != I40E_VFR_VFACTIVE) {
1478 dev_info(&adapter->pdev->dev, "Reset now occurring\n");
1479 break;
1480 } else {
1481 msleep(I40EVF_RESET_WAIT_MS);
1482 }
1483 }
1484 if (i == I40EVF_RESET_WAIT_COUNT) {
1485 dev_err(&adapter->pdev->dev, "Reset was not detected\n");
1486 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1487 goto continue_reset; /* act like the reset happened */
1488 }
5eae00c5 1489
ef8693eb
MW
1490 /* wait until the reset is complete and the PF is responding to us */
1491 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
5eae00c5
GR
1492 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1493 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
ef8693eb
MW
1494 if (rstat_val == I40E_VFR_VFACTIVE) {
1495 dev_info(&adapter->pdev->dev, "Reset is complete. Reinitializing.\n");
5eae00c5 1496 break;
ef8693eb
MW
1497 } else {
1498 msleep(I40EVF_RESET_WAIT_MS);
1499 }
5eae00c5 1500 }
ef8693eb 1501 if (i == I40EVF_RESET_WAIT_COUNT) {
5eae00c5 1502 /* reset never finished */
ef8693eb
MW
1503 dev_err(&adapter->pdev->dev, "Reset never finished (%x). PF driver is dead, and so am I.\n",
1504 rstat_val);
1505 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1506
1507 if (netif_running(adapter->netdev))
1508 i40evf_close(adapter->netdev);
1509
1510 i40evf_free_misc_irq(adapter);
1511 i40evf_reset_interrupt_capability(adapter);
1512 i40evf_free_queues(adapter);
1513 kfree(adapter->vf_res);
1514 i40evf_shutdown_adminq(hw);
1515 adapter->netdev->flags &= ~IFF_UP;
1516 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1517 return; /* Do not attempt to reinit. It's dead, Jim. */
5eae00c5 1518 }
ef8693eb
MW
1519
1520continue_reset:
1521 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1522
5eae00c5
GR
1523 i40evf_down(adapter);
1524 adapter->state = __I40EVF_RESETTING;
1525
1526 /* kill and reinit the admin queue */
1527 if (i40evf_shutdown_adminq(hw))
1528 dev_warn(&adapter->pdev->dev,
1529 "%s: Failed to destroy the Admin Queue resources\n",
1530 __func__);
1531 err = i40evf_init_adminq(hw);
1532 if (err)
1533 dev_info(&adapter->pdev->dev, "%s: init_adminq failed: %d\n",
1534 __func__, err);
1535
1536 adapter->aq_pending = 0;
1537 adapter->aq_required = 0;
1538 i40evf_map_queues(adapter);
1539 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1540
1541 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1542
1543 if (netif_running(adapter->netdev)) {
1544 /* allocate transmit descriptors */
1545 err = i40evf_setup_all_tx_resources(adapter);
1546 if (err)
1547 goto reset_err;
1548
1549 /* allocate receive descriptors */
1550 err = i40evf_setup_all_rx_resources(adapter);
1551 if (err)
1552 goto reset_err;
1553
1554 i40evf_configure(adapter);
1555
1556 err = i40evf_up_complete(adapter);
1557 if (err)
1558 goto reset_err;
1559
1560 i40evf_irq_enable(adapter, true);
1561 }
1562 return;
1563reset_err:
1564 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit.\n");
1565 i40evf_close(adapter->netdev);
1566}
1567
1568/**
1569 * i40evf_adminq_task - worker thread to clean the admin queue
1570 * @work: pointer to work_struct containing our data
1571 **/
1572static void i40evf_adminq_task(struct work_struct *work)
1573{
1574 struct i40evf_adapter *adapter =
1575 container_of(work, struct i40evf_adapter, adminq_task);
1576 struct i40e_hw *hw = &adapter->hw;
1577 struct i40e_arq_event_info event;
1578 struct i40e_virtchnl_msg *v_msg;
1579 i40e_status ret;
1580 u16 pending;
1581
ef8693eb
MW
1582 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1583 return;
1584
5eae00c5
GR
1585 event.msg_size = I40EVF_MAX_AQ_BUF_SIZE;
1586 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
1587 if (!event.msg_buf) {
1588 dev_info(&adapter->pdev->dev, "%s: no memory for ARQ clean\n",
1589 __func__);
1590 return;
1591 }
1592 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1593 do {
1594 ret = i40evf_clean_arq_element(hw, &event, &pending);
1595 if (ret)
1596 break; /* No event to process or error cleaning ARQ */
1597
1598 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1599 v_msg->v_retval, event.msg_buf,
1600 event.msg_size);
1601 if (pending != 0) {
1602 dev_info(&adapter->pdev->dev,
1603 "%s: ARQ: Pending events %d\n",
1604 __func__, pending);
1605 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1606 }
1607 } while (pending);
1608
1609 /* re-enable Admin queue interrupt cause */
1610 i40evf_misc_irq_enable(adapter);
1611
1612 kfree(event.msg_buf);
1613}
1614
1615/**
1616 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1617 * @adapter: board private structure
1618 *
1619 * Free all transmit software resources
1620 **/
1621static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1622{
1623 int i;
1624
1625 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1626 if (adapter->tx_rings[i]->desc)
1627 i40evf_free_tx_resources(adapter->tx_rings[i]);
1628
1629}
1630
1631/**
1632 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1633 * @adapter: board private structure
1634 *
1635 * If this function returns with an error, then it's possible one or
1636 * more of the rings is populated (while the rest are not). It is the
1637 * callers duty to clean those orphaned rings.
1638 *
1639 * Return 0 on success, negative on failure
1640 **/
1641static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1642{
1643 int i, err = 0;
1644
1645 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1646 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1647 if (!err)
1648 continue;
1649 dev_err(&adapter->pdev->dev,
1650 "%s: Allocation for Tx Queue %u failed\n",
1651 __func__, i);
1652 break;
1653 }
1654
1655 return err;
1656}
1657
1658/**
1659 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1660 * @adapter: board private structure
1661 *
1662 * If this function returns with an error, then it's possible one or
1663 * more of the rings is populated (while the rest are not). It is the
1664 * callers duty to clean those orphaned rings.
1665 *
1666 * Return 0 on success, negative on failure
1667 **/
1668static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1669{
1670 int i, err = 0;
1671
1672 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1673 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1674 if (!err)
1675 continue;
1676 dev_err(&adapter->pdev->dev,
1677 "%s: Allocation for Rx Queue %u failed\n",
1678 __func__, i);
1679 break;
1680 }
1681 return err;
1682}
1683
1684/**
1685 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1686 * @adapter: board private structure
1687 *
1688 * Free all receive software resources
1689 **/
1690static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1691{
1692 int i;
1693
1694 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1695 if (adapter->rx_rings[i]->desc)
1696 i40evf_free_rx_resources(adapter->rx_rings[i]);
1697}
1698
1699/**
1700 * i40evf_open - Called when a network interface is made active
1701 * @netdev: network interface device structure
1702 *
1703 * Returns 0 on success, negative value on failure
1704 *
1705 * The open entry point is called when a network interface is made
1706 * active by the system (IFF_UP). At this point all resources needed
1707 * for transmit and receive operations are allocated, the interrupt
1708 * handler is registered with the OS, the watchdog timer is started,
1709 * and the stack is notified that the interface is ready.
1710 **/
1711static int i40evf_open(struct net_device *netdev)
1712{
1713 struct i40evf_adapter *adapter = netdev_priv(netdev);
1714 int err;
1715
ef8693eb
MW
1716 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1717 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1718 return -EIO;
1719 }
5eae00c5
GR
1720 if (adapter->state != __I40EVF_DOWN)
1721 return -EBUSY;
1722
1723 /* allocate transmit descriptors */
1724 err = i40evf_setup_all_tx_resources(adapter);
1725 if (err)
1726 goto err_setup_tx;
1727
1728 /* allocate receive descriptors */
1729 err = i40evf_setup_all_rx_resources(adapter);
1730 if (err)
1731 goto err_setup_rx;
1732
1733 /* clear any pending interrupts, may auto mask */
1734 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1735 if (err)
1736 goto err_req_irq;
1737
1738 i40evf_configure(adapter);
1739
1740 err = i40evf_up_complete(adapter);
1741 if (err)
1742 goto err_req_irq;
1743
1744 i40evf_irq_enable(adapter, true);
1745
1746 return 0;
1747
1748err_req_irq:
1749 i40evf_down(adapter);
1750 i40evf_free_traffic_irqs(adapter);
1751err_setup_rx:
1752 i40evf_free_all_rx_resources(adapter);
1753err_setup_tx:
1754 i40evf_free_all_tx_resources(adapter);
1755
1756 return err;
1757}
1758
1759/**
1760 * i40evf_close - Disables a network interface
1761 * @netdev: network interface device structure
1762 *
1763 * Returns 0, this is not allowed to fail
1764 *
1765 * The close entry point is called when an interface is de-activated
1766 * by the OS. The hardware is still under the drivers control, but
1767 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1768 * are freed, along with all transmit and receive resources.
1769 **/
1770static int i40evf_close(struct net_device *netdev)
1771{
1772 struct i40evf_adapter *adapter = netdev_priv(netdev);
1773
ef8693eb
MW
1774 if (adapter->state <= __I40EVF_DOWN)
1775 return 0;
1776
5eae00c5
GR
1777 /* signal that we are down to the interrupt handler */
1778 adapter->state = __I40EVF_DOWN;
ef8693eb 1779
5eae00c5
GR
1780 set_bit(__I40E_DOWN, &adapter->vsi.state);
1781
1782 i40evf_down(adapter);
1783 i40evf_free_traffic_irqs(adapter);
1784
1785 i40evf_free_all_tx_resources(adapter);
1786 i40evf_free_all_rx_resources(adapter);
1787
1788 return 0;
1789}
1790
1791/**
1792 * i40evf_get_stats - Get System Network Statistics
1793 * @netdev: network interface device structure
1794 *
1795 * Returns the address of the device statistics structure.
1796 * The statistics are actually updated from the timer callback.
1797 **/
1798static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1799{
1800 struct i40evf_adapter *adapter = netdev_priv(netdev);
1801
1802 /* only return the current stats */
1803 return &adapter->net_stats;
1804}
1805
1806/**
1807 * i40evf_reinit_locked - Software reinit
1808 * @adapter: board private structure
1809 *
1810 * Reinititalizes the ring structures in response to a software configuration
1811 * change. Roughly the same as close followed by open, but skips releasing
1812 * and reallocating the interrupts.
1813 **/
1814void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1815{
1816 struct net_device *netdev = adapter->netdev;
1817 int err;
1818
1819 WARN_ON(in_interrupt());
1820
1821 adapter->state = __I40EVF_RESETTING;
1822
1823 i40evf_down(adapter);
1824
1825 /* allocate transmit descriptors */
1826 err = i40evf_setup_all_tx_resources(adapter);
1827 if (err)
1828 goto err_reinit;
1829
1830 /* allocate receive descriptors */
1831 err = i40evf_setup_all_rx_resources(adapter);
1832 if (err)
1833 goto err_reinit;
1834
1835 i40evf_configure(adapter);
1836
1837 err = i40evf_up_complete(adapter);
1838 if (err)
1839 goto err_reinit;
1840
1841 i40evf_irq_enable(adapter, true);
1842 return;
1843
1844err_reinit:
1845 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit.\n");
1846 i40evf_close(netdev);
1847}
1848
1849/**
1850 * i40evf_change_mtu - Change the Maximum Transfer Unit
1851 * @netdev: network interface device structure
1852 * @new_mtu: new value for maximum frame size
1853 *
1854 * Returns 0 on success, negative on failure
1855 **/
1856static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1857{
1858 struct i40evf_adapter *adapter = netdev_priv(netdev);
1859 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1860
1861 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1862 return -EINVAL;
1863
1864 /* must set new MTU before calling down or up */
1865 netdev->mtu = new_mtu;
1866 i40evf_reinit_locked(adapter);
1867 return 0;
1868}
1869
1870static const struct net_device_ops i40evf_netdev_ops = {
1871 .ndo_open = i40evf_open,
1872 .ndo_stop = i40evf_close,
1873 .ndo_start_xmit = i40evf_xmit_frame,
1874 .ndo_get_stats = i40evf_get_stats,
1875 .ndo_set_rx_mode = i40evf_set_rx_mode,
1876 .ndo_validate_addr = eth_validate_addr,
1877 .ndo_set_mac_address = i40evf_set_mac,
1878 .ndo_change_mtu = i40evf_change_mtu,
1879 .ndo_tx_timeout = i40evf_tx_timeout,
1880 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1881 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1882};
1883
1884/**
1885 * i40evf_check_reset_complete - check that VF reset is complete
1886 * @hw: pointer to hw struct
1887 *
1888 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1889 **/
1890static int i40evf_check_reset_complete(struct i40e_hw *hw)
1891{
1892 u32 rstat;
1893 int i;
1894
1895 for (i = 0; i < 100; i++) {
1896 rstat = rd32(hw, I40E_VFGEN_RSTAT);
1897 if (rstat == I40E_VFR_VFACTIVE)
1898 return 0;
1899 udelay(10);
1900 }
1901 return -EBUSY;
1902}
1903
1904/**
1905 * i40evf_init_task - worker thread to perform delayed initialization
1906 * @work: pointer to work_struct containing our data
1907 *
1908 * This task completes the work that was begun in probe. Due to the nature
1909 * of VF-PF communications, we may need to wait tens of milliseconds to get
1910 * reponses back from the PF. Rather than busy-wait in probe and bog down the
1911 * whole system, we'll do it in a task so we can sleep.
1912 * This task only runs during driver init. Once we've established
1913 * communications with the PF driver and set up our netdev, the watchdog
1914 * takes over.
1915 **/
1916static void i40evf_init_task(struct work_struct *work)
1917{
1918 struct i40evf_adapter *adapter = container_of(work,
1919 struct i40evf_adapter,
1920 init_task.work);
1921 struct net_device *netdev = adapter->netdev;
1922 struct i40evf_mac_filter *f;
1923 struct i40e_hw *hw = &adapter->hw;
1924 struct pci_dev *pdev = adapter->pdev;
1925 int i, err, bufsz;
1926
1927 switch (adapter->state) {
1928 case __I40EVF_STARTUP:
1929 /* driver loaded, probe complete */
ef8693eb
MW
1930 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1931 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
5eae00c5
GR
1932 err = i40e_set_mac_type(hw);
1933 if (err) {
1934 dev_info(&pdev->dev, "%s: set_mac_type failed: %d\n",
1935 __func__, err);
1936 goto err;
1937 }
1938 err = i40evf_check_reset_complete(hw);
1939 if (err) {
1940 dev_info(&pdev->dev, "%s: device is still in reset (%d).\n",
1941 __func__, err);
1942 goto err;
1943 }
1944 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
1945 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
1946 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
1947 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
1948
1949 err = i40evf_init_adminq(hw);
1950 if (err) {
1951 dev_info(&pdev->dev, "%s: init_adminq failed: %d\n",
1952 __func__, err);
1953 goto err;
1954 }
1955 err = i40evf_send_api_ver(adapter);
1956 if (err) {
1957 dev_info(&pdev->dev, "%s: unable to send to PF (%d)\n",
1958 __func__, err);
1959 i40evf_shutdown_adminq(hw);
1960 goto err;
1961 }
1962 adapter->state = __I40EVF_INIT_VERSION_CHECK;
1963 goto restart;
1964 break;
1965 case __I40EVF_INIT_VERSION_CHECK:
1966 if (!i40evf_asq_done(hw))
1967 goto err;
1968
1969 /* aq msg sent, awaiting reply */
1970 err = i40evf_verify_api_ver(adapter);
1971 if (err) {
1972 dev_err(&pdev->dev, "Unable to verify API version, error %d\n",
1973 err);
1974 goto err;
1975 }
1976 err = i40evf_send_vf_config_msg(adapter);
1977 if (err) {
1978 dev_err(&pdev->dev, "Unable send config request, error %d\n",
1979 err);
1980 goto err;
1981 }
1982 adapter->state = __I40EVF_INIT_GET_RESOURCES;
1983 goto restart;
1984 break;
1985 case __I40EVF_INIT_GET_RESOURCES:
1986 /* aq msg sent, awaiting reply */
1987 if (!adapter->vf_res) {
1988 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
1989 (I40E_MAX_VF_VSI *
1990 sizeof(struct i40e_virtchnl_vsi_resource));
1991 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
1992 if (!adapter->vf_res) {
1993 dev_err(&pdev->dev, "%s: unable to allocate memory\n",
1994 __func__);
1995 goto err;
1996 }
1997 }
1998 err = i40evf_get_vf_config(adapter);
1999 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2000 goto restart;
2001 if (err) {
2002 dev_info(&pdev->dev, "%s: unable to get VF config (%d)\n",
2003 __func__, err);
2004 goto err_alloc;
2005 }
2006 adapter->state = __I40EVF_INIT_SW;
2007 break;
2008 default:
2009 goto err_alloc;
2010 }
2011 /* got VF config message back from PF, now we can parse it */
2012 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2013 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2014 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2015 }
2016 if (!adapter->vsi_res) {
2017 dev_info(&pdev->dev, "%s: no LAN VSI found\n", __func__);
2018 goto err_alloc;
2019 }
2020
2021 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2022
2023 adapter->txd_count = I40EVF_DEFAULT_TXD;
2024 adapter->rxd_count = I40EVF_DEFAULT_RXD;
2025
2026 netdev->netdev_ops = &i40evf_netdev_ops;
2027 i40evf_set_ethtool_ops(netdev);
2028 netdev->watchdog_timeo = 5 * HZ;
2029
2030 netdev->features |= NETIF_F_SG |
2031 NETIF_F_IP_CSUM |
2032 NETIF_F_SCTP_CSUM |
2033 NETIF_F_IPV6_CSUM |
2034 NETIF_F_TSO |
2035 NETIF_F_TSO6 |
2036 NETIF_F_GRO;
2037
2038 if (adapter->vf_res->vf_offload_flags
2039 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2040 netdev->vlan_features = netdev->features;
2041 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2042 NETIF_F_HW_VLAN_CTAG_RX |
2043 NETIF_F_HW_VLAN_CTAG_FILTER;
2044 }
2045
2046 /* The HW MAC address was set and/or determined in sw_init */
2047 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2048 dev_info(&pdev->dev,
2049 "Invalid MAC address %pMAC, using random\n",
2050 adapter->hw.mac.addr);
2051 random_ether_addr(adapter->hw.mac.addr);
2052 }
2053 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
2054 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
2055
2056 INIT_LIST_HEAD(&adapter->mac_filter_list);
2057 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2058 f = kzalloc(sizeof(*f), GFP_ATOMIC);
2059 if (NULL == f)
2060 goto err_sw_init;
2061
2062 memcpy(f->macaddr, adapter->hw.mac.addr, ETH_ALEN);
2063 f->add = true;
2064 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2065
2066 list_add(&f->list, &adapter->mac_filter_list);
2067
2068 init_timer(&adapter->watchdog_timer);
2069 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2070 adapter->watchdog_timer.data = (unsigned long)adapter;
2071 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2072
2073 err = i40evf_init_interrupt_scheme(adapter);
2074 if (err)
2075 goto err_sw_init;
2076 i40evf_map_rings_to_vectors(adapter);
2077 i40evf_configure_rss(adapter);
2078 err = i40evf_request_misc_irq(adapter);
2079 if (err)
2080 goto err_sw_init;
2081
2082 netif_carrier_off(netdev);
2083
2084 strcpy(netdev->name, "eth%d");
2085
2086 adapter->vsi.id = adapter->vsi_res->vsi_id;
2087 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2088 adapter->vsi.back = adapter;
2089 adapter->vsi.base_vector = 1;
2090 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2091 adapter->vsi.rx_itr_setting = I40E_ITR_DYNAMIC;
2092 adapter->vsi.tx_itr_setting = I40E_ITR_DYNAMIC;
2093 adapter->vsi.netdev = adapter->netdev;
2094
ef8693eb
MW
2095 if (!adapter->netdev_registered) {
2096 err = register_netdev(netdev);
2097 if (err)
2098 goto err_register;
2099 }
5eae00c5
GR
2100
2101 adapter->netdev_registered = true;
2102
2103 netif_tx_stop_all_queues(netdev);
2104
2105 dev_info(&pdev->dev, "MAC address: %pMAC\n", adapter->hw.mac.addr);
2106 if (netdev->features & NETIF_F_GRO)
2107 dev_info(&pdev->dev, "GRO is enabled\n");
2108
2109 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2110 adapter->state = __I40EVF_DOWN;
2111 set_bit(__I40E_DOWN, &adapter->vsi.state);
2112 i40evf_misc_irq_enable(adapter);
2113 return;
2114restart:
2115 schedule_delayed_work(&adapter->init_task,
2116 msecs_to_jiffies(50));
2117 return;
2118
2119err_register:
2120 i40evf_free_misc_irq(adapter);
2121err_sw_init:
2122 i40evf_reset_interrupt_capability(adapter);
5eae00c5
GR
2123err_alloc:
2124 kfree(adapter->vf_res);
2125 adapter->vf_res = NULL;
2126err:
ef8693eb
MW
2127 if (hw->aq.asq.count)
2128 i40evf_shutdown_adminq(hw); /* ignore error */
5eae00c5
GR
2129 /* Things went into the weeds, so try again later */
2130 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2131 dev_err(&pdev->dev, "Failed to communicate with PF; giving up.\n");
ef8693eb 2132 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
5eae00c5
GR
2133 return; /* do not reschedule */
2134 }
2135 schedule_delayed_work(&adapter->init_task, HZ * 3);
2136 return;
2137}
2138
2139/**
2140 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2141 * @pdev: pci device structure
2142 **/
2143static void i40evf_shutdown(struct pci_dev *pdev)
2144{
2145 struct net_device *netdev = pci_get_drvdata(pdev);
2146
2147 netif_device_detach(netdev);
2148
2149 if (netif_running(netdev))
2150 i40evf_close(netdev);
2151
2152#ifdef CONFIG_PM
2153 pci_save_state(pdev);
2154
2155#endif
2156 pci_disable_device(pdev);
2157}
2158
2159/**
2160 * i40evf_probe - Device Initialization Routine
2161 * @pdev: PCI device information struct
2162 * @ent: entry in i40evf_pci_tbl
2163 *
2164 * Returns 0 on success, negative on failure
2165 *
2166 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2167 * The OS initialization, configuring of the adapter private structure,
2168 * and a hardware reset occur.
2169 **/
2170static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2171{
2172 struct net_device *netdev;
2173 struct i40evf_adapter *adapter = NULL;
2174 struct i40e_hw *hw = NULL;
2175 int err, pci_using_dac;
2176
2177 err = pci_enable_device(pdev);
2178 if (err)
2179 return err;
2180
2181 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
2182 pci_using_dac = true;
2183 /* coherent mask for the same size will always succeed if
2184 * dma_set_mask does
2185 */
2186 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
2187 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2188 pci_using_dac = false;
2189 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
2190 } else {
2191 dev_err(&pdev->dev, "%s: DMA configuration failed: %d\n",
2192 __func__, err);
2193 err = -EIO;
2194 goto err_dma;
2195 }
2196
2197 err = pci_request_regions(pdev, i40evf_driver_name);
2198 if (err) {
2199 dev_err(&pdev->dev,
2200 "pci_request_regions failed 0x%x\n", err);
2201 goto err_pci_reg;
2202 }
2203
2204 pci_enable_pcie_error_reporting(pdev);
2205
2206 pci_set_master(pdev);
2207
2208 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2209 MAX_TX_QUEUES);
2210 if (!netdev) {
2211 err = -ENOMEM;
2212 goto err_alloc_etherdev;
2213 }
2214
2215 SET_NETDEV_DEV(netdev, &pdev->dev);
2216
2217 pci_set_drvdata(pdev, netdev);
2218 adapter = netdev_priv(netdev);
2219 if (pci_using_dac)
2220 netdev->features |= NETIF_F_HIGHDMA;
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
2370 if (adapter->num_msix_vectors) {
2371 i40evf_misc_irq_disable(adapter);
2372 del_timer_sync(&adapter->watchdog_timer);
2373
2374 flush_scheduled_work();
2375
2376 i40evf_free_misc_irq(adapter);
2377
2378 i40evf_reset_interrupt_capability(adapter);
2379 }
2380
2381 if (hw->aq.asq.count)
2382 i40evf_shutdown_adminq(hw);
2383
2384 iounmap(hw->hw_addr);
2385 pci_release_regions(pdev);
2386
2387 i40evf_free_queues(adapter);
2388 kfree(adapter->vf_res);
2389
2390 free_netdev(netdev);
2391
2392 pci_disable_pcie_error_reporting(pdev);
2393
2394 pci_disable_device(pdev);
2395}
2396
2397static struct pci_driver i40evf_driver = {
2398 .name = i40evf_driver_name,
2399 .id_table = i40evf_pci_tbl,
2400 .probe = i40evf_probe,
2401 .remove = i40evf_remove,
2402#ifdef CONFIG_PM
2403 .suspend = i40evf_suspend,
2404 .resume = i40evf_resume,
2405#endif
2406 .shutdown = i40evf_shutdown,
2407};
2408
2409/**
2410 * i40e_init_module - Driver Registration Routine
2411 *
2412 * i40e_init_module is the first routine called when the driver is
2413 * loaded. All it does is register with the PCI subsystem.
2414 **/
2415static int __init i40evf_init_module(void)
2416{
2417 int ret;
2418 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2419 i40evf_driver_version);
2420
2421 pr_info("%s\n", i40evf_copyright);
2422
2423 ret = pci_register_driver(&i40evf_driver);
2424 return ret;
2425}
2426
2427module_init(i40evf_init_module);
2428
2429/**
2430 * i40e_exit_module - Driver Exit Cleanup Routine
2431 *
2432 * i40e_exit_module is called just before the driver is removed
2433 * from memory.
2434 **/
2435static void __exit i40evf_exit_module(void)
2436{
2437 pci_unregister_driver(&i40evf_driver);
2438}
2439
2440module_exit(i40evf_exit_module);
2441
2442/* i40evf_main.c */
This page took 0.239114 seconds and 5 git commands to generate.