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