blob: 1257577148ea3c2f394299ca00a4b4782541d0ef [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 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
Greg Rose5eae00c2013-12-21 06:12:45 +000018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40evf.h"
28#include "i40e_prototype.h"
29static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
30static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
Mitch Williams169f4072014-04-01 07:11:50 +000031static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter);
32static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +000033static int i40evf_close(struct net_device *netdev);
34
35char i40evf_driver_name[] = "i40evf";
36static const char i40evf_driver_string[] =
Mitch Williams0af56f42014-06-04 20:42:10 +000037 "Intel(R) XL710/X710 Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000038
Sravanthi Tangedac952f6c2015-01-24 09:58:42 +000039#define DRV_VERSION "1.2.2"
Greg Rose5eae00c2013-12-21 06:12:45 +000040const char i40evf_driver_version[] = DRV_VERSION;
41static const char i40evf_copyright[] =
Mitch Williams673f2eb2014-02-20 19:29:13 -080042 "Copyright (c) 2013 - 2014 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000043
44/* i40evf_pci_tbl - PCI Device ID Table
45 *
46 * Wildcard entries (PCI_ANY_ID) should come last
47 * Last entry must be all 0s
48 *
49 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
50 * Class, Class Mask, private data (not used) }
51 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020052static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080053 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000054 /* required last entry */
55 {0, }
56};
57
58MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
59
60MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
61MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
62MODULE_LICENSE("GPL");
63MODULE_VERSION(DRV_VERSION);
64
65/**
66 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
67 * @hw: pointer to the HW structure
68 * @mem: ptr to mem struct to fill out
69 * @size: size of memory requested
70 * @alignment: what to align the allocation to
71 **/
72i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
73 struct i40e_dma_mem *mem,
74 u64 size, u32 alignment)
75{
76 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
77
78 if (!mem)
79 return I40E_ERR_PARAM;
80
81 mem->size = ALIGN(size, alignment);
82 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
83 (dma_addr_t *)&mem->pa, GFP_KERNEL);
84 if (mem->va)
85 return 0;
86 else
87 return I40E_ERR_NO_MEMORY;
88}
89
90/**
91 * i40evf_free_dma_mem_d - OS specific memory free for shared code
92 * @hw: pointer to the HW structure
93 * @mem: ptr to mem struct to free
94 **/
95i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
96{
97 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
98
99 if (!mem || !mem->va)
100 return I40E_ERR_PARAM;
101 dma_free_coherent(&adapter->pdev->dev, mem->size,
102 mem->va, (dma_addr_t)mem->pa);
103 return 0;
104}
105
106/**
107 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
108 * @hw: pointer to the HW structure
109 * @mem: ptr to mem struct to fill out
110 * @size: size of memory requested
111 **/
112i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
113 struct i40e_virt_mem *mem, u32 size)
114{
115 if (!mem)
116 return I40E_ERR_PARAM;
117
118 mem->size = size;
119 mem->va = kzalloc(size, GFP_KERNEL);
120
121 if (mem->va)
122 return 0;
123 else
124 return I40E_ERR_NO_MEMORY;
125}
126
127/**
128 * i40evf_free_virt_mem_d - OS specific memory free for shared code
129 * @hw: pointer to the HW structure
130 * @mem: ptr to mem struct to free
131 **/
132i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
133 struct i40e_virt_mem *mem)
134{
135 if (!mem)
136 return I40E_ERR_PARAM;
137
138 /* it's ok to kfree a NULL pointer */
139 kfree(mem->va);
140
141 return 0;
142}
143
144/**
145 * i40evf_debug_d - OS dependent version of debug printing
146 * @hw: pointer to the HW structure
147 * @mask: debug level mask
148 * @fmt_str: printf-type format description
149 **/
150void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
151{
152 char buf[512];
153 va_list argptr;
154
155 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
156 return;
157
158 va_start(argptr, fmt_str);
159 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
160 va_end(argptr);
161
162 /* the debug string is already formatted with a newline */
163 pr_info("%s", buf);
164}
165
166/**
167 * i40evf_tx_timeout - Respond to a Tx Hang
168 * @netdev: network interface device structure
169 **/
170static void i40evf_tx_timeout(struct net_device *netdev)
171{
172 struct i40evf_adapter *adapter = netdev_priv(netdev);
173
174 adapter->tx_timeout_count++;
Mitch Williams625777e2014-02-20 19:29:05 -0800175 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING)) {
Mitch Williams3526d802014-03-06 08:59:56 +0000176 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
Mitch Williams625777e2014-02-20 19:29:05 -0800177 schedule_work(&adapter->reset_task);
178 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000179}
180
181/**
182 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
183 * @adapter: board private structure
184 **/
185static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
186{
187 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000188
Greg Rose5eae00c2013-12-21 06:12:45 +0000189 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
190
191 /* read flush */
192 rd32(hw, I40E_VFGEN_RSTAT);
193
194 synchronize_irq(adapter->msix_entries[0].vector);
195}
196
197/**
198 * i40evf_misc_irq_enable - Enable default interrupt generation settings
199 * @adapter: board private structure
200 **/
201static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
202{
203 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000204
Greg Rose5eae00c2013-12-21 06:12:45 +0000205 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
206 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
207 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA_ADMINQ_MASK);
208
209 /* read flush */
210 rd32(hw, I40E_VFGEN_RSTAT);
211}
212
213/**
214 * i40evf_irq_disable - Mask off interrupt generation on the NIC
215 * @adapter: board private structure
216 **/
217static void i40evf_irq_disable(struct i40evf_adapter *adapter)
218{
219 int i;
220 struct i40e_hw *hw = &adapter->hw;
221
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800222 if (!adapter->msix_entries)
223 return;
224
Greg Rose5eae00c2013-12-21 06:12:45 +0000225 for (i = 1; i < adapter->num_msix_vectors; i++) {
226 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
227 synchronize_irq(adapter->msix_entries[i].vector);
228 }
229 /* read flush */
230 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000231}
232
233/**
234 * i40evf_irq_enable_queues - Enable interrupt for specified queues
235 * @adapter: board private structure
236 * @mask: bitmap of queues to enable
237 **/
238void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
239{
240 struct i40e_hw *hw = &adapter->hw;
241 int i;
242
243 for (i = 1; i < adapter->num_msix_vectors; i++) {
244 if (mask & (1 << (i - 1))) {
245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
247 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
248 }
249 }
250}
251
252/**
253 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
254 * @adapter: board private structure
255 * @mask: bitmap of vectors to trigger
256 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000257static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000258{
259 struct i40e_hw *hw = &adapter->hw;
260 int i;
261 uint32_t dyn_ctl;
262
Mitch Williams164ec1b2014-06-04 08:45:19 +0000263 if (mask & 1) {
264 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
265 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
266 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
267 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
268 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000269 for (i = 1; i < adapter->num_msix_vectors; i++) {
270 if (mask & (1 << i)) {
271 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
272 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
273 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
274 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
275 }
276 }
277}
278
279/**
280 * i40evf_irq_enable - Enable default interrupt generation settings
281 * @adapter: board private structure
282 **/
283void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
284{
285 struct i40e_hw *hw = &adapter->hw;
286
Mitch Williams164ec1b2014-06-04 08:45:19 +0000287 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000288 i40evf_irq_enable_queues(adapter, ~0);
289
290 if (flush)
291 rd32(hw, I40E_VFGEN_RSTAT);
292}
293
294/**
295 * i40evf_msix_aq - Interrupt handler for vector 0
296 * @irq: interrupt number
297 * @data: pointer to netdev
298 **/
299static irqreturn_t i40evf_msix_aq(int irq, void *data)
300{
301 struct net_device *netdev = data;
302 struct i40evf_adapter *adapter = netdev_priv(netdev);
303 struct i40e_hw *hw = &adapter->hw;
304 u32 val;
305 u32 ena_mask;
306
307 /* handle non-queue interrupts */
308 val = rd32(hw, I40E_VFINT_ICR01);
309 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
310
311
312 val = rd32(hw, I40E_VFINT_DYN_CTL01);
313 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
314 wr32(hw, I40E_VFINT_DYN_CTL01, val);
315
Greg Rose5eae00c2013-12-21 06:12:45 +0000316 /* schedule work on the private workqueue */
317 schedule_work(&adapter->adminq_task);
318
319 return IRQ_HANDLED;
320}
321
322/**
323 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
324 * @irq: interrupt number
325 * @data: pointer to a q_vector
326 **/
327static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
328{
329 struct i40e_q_vector *q_vector = data;
330
331 if (!q_vector->tx.ring && !q_vector->rx.ring)
332 return IRQ_HANDLED;
333
334 napi_schedule(&q_vector->napi);
335
336 return IRQ_HANDLED;
337}
338
339/**
340 * i40evf_map_vector_to_rxq - associate irqs with rx queues
341 * @adapter: board private structure
342 * @v_idx: interrupt number
343 * @r_idx: queue number
344 **/
345static void
346i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
347{
348 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
349 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
350
351 rx_ring->q_vector = q_vector;
352 rx_ring->next = q_vector->rx.ring;
353 rx_ring->vsi = &adapter->vsi;
354 q_vector->rx.ring = rx_ring;
355 q_vector->rx.count++;
356 q_vector->rx.latency_range = I40E_LOW_LATENCY;
357}
358
359/**
360 * i40evf_map_vector_to_txq - associate irqs with tx queues
361 * @adapter: board private structure
362 * @v_idx: interrupt number
363 * @t_idx: queue number
364 **/
365static void
366i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
367{
368 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
369 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
370
371 tx_ring->q_vector = q_vector;
372 tx_ring->next = q_vector->tx.ring;
373 tx_ring->vsi = &adapter->vsi;
374 q_vector->tx.ring = tx_ring;
375 q_vector->tx.count++;
376 q_vector->tx.latency_range = I40E_LOW_LATENCY;
377 q_vector->num_ringpairs++;
378 q_vector->ring_mask |= (1 << t_idx);
379}
380
381/**
382 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
383 * @adapter: board private structure to initialize
384 *
385 * This function maps descriptor rings to the queue-specific vectors
386 * we were allotted through the MSI-X enabling code. Ideally, we'd have
387 * one vector per ring/queue, but on a constrained vector budget, we
388 * group the rings as "efficiently" as possible. You would add new
389 * mapping configurations in here.
390 **/
391static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
392{
393 int q_vectors;
394 int v_start = 0;
395 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000396 int rxr_remaining = adapter->num_active_queues;
397 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000398 int i, j;
399 int rqpv, tqpv;
400 int err = 0;
401
402 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
403
404 /* The ideal configuration...
405 * We have enough vectors to map one per queue.
406 */
407 if (q_vectors == (rxr_remaining * 2)) {
408 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
409 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
410
411 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
412 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
413 goto out;
414 }
415
416 /* If we don't have enough vectors for a 1-to-1
417 * mapping, we'll have to group them so there are
418 * multiple queues per vector.
419 * Re-adjusting *qpv takes care of the remainder.
420 */
421 for (i = v_start; i < q_vectors; i++) {
422 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
423 for (j = 0; j < rqpv; j++) {
424 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
425 rxr_idx++;
426 rxr_remaining--;
427 }
428 }
429 for (i = v_start; i < q_vectors; i++) {
430 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
431 for (j = 0; j < tqpv; j++) {
432 i40evf_map_vector_to_txq(adapter, i, txr_idx);
433 txr_idx++;
434 txr_remaining--;
435 }
436 }
437
438out:
439 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
440
441 return err;
442}
443
444/**
445 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
446 * @adapter: board private structure
447 *
448 * Allocates MSI-X vectors for tx and rx handling, and requests
449 * interrupts from the kernel.
450 **/
451static int
452i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
453{
454 int vector, err, q_vectors;
455 int rx_int_idx = 0, tx_int_idx = 0;
456
457 i40evf_irq_disable(adapter);
458 /* Decrement for Other and TCP Timer vectors */
459 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
460
461 for (vector = 0; vector < q_vectors; vector++) {
462 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
463
464 if (q_vector->tx.ring && q_vector->rx.ring) {
465 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
466 "i40evf-%s-%s-%d", basename,
467 "TxRx", rx_int_idx++);
468 tx_int_idx++;
469 } else if (q_vector->rx.ring) {
470 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
471 "i40evf-%s-%s-%d", basename,
472 "rx", rx_int_idx++);
473 } else if (q_vector->tx.ring) {
474 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
475 "i40evf-%s-%s-%d", basename,
476 "tx", tx_int_idx++);
477 } else {
478 /* skip this unused q_vector */
479 continue;
480 }
481 err = request_irq(
482 adapter->msix_entries[vector + NONQ_VECS].vector,
483 i40evf_msix_clean_rings,
484 0,
485 q_vector->name,
486 q_vector);
487 if (err) {
488 dev_info(&adapter->pdev->dev,
489 "%s: request_irq failed, error: %d\n",
490 __func__, err);
491 goto free_queue_irqs;
492 }
493 /* assign the mask for this irq */
494 irq_set_affinity_hint(
495 adapter->msix_entries[vector + NONQ_VECS].vector,
496 q_vector->affinity_mask);
497 }
498
499 return 0;
500
501free_queue_irqs:
502 while (vector) {
503 vector--;
504 irq_set_affinity_hint(
505 adapter->msix_entries[vector + NONQ_VECS].vector,
506 NULL);
507 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
508 adapter->q_vector[vector]);
509 }
510 return err;
511}
512
513/**
514 * i40evf_request_misc_irq - Initialize MSI-X interrupts
515 * @adapter: board private structure
516 *
517 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
518 * vector is only for the admin queue, and stays active even when the netdev
519 * is closed.
520 **/
521static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
522{
523 struct net_device *netdev = adapter->netdev;
524 int err;
525
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700526 snprintf(adapter->misc_vector_name,
527 sizeof(adapter->misc_vector_name) - 1, "i40evf:mbx");
Greg Rose5eae00c2013-12-21 06:12:45 +0000528 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800529 &i40evf_msix_aq, 0,
530 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000531 if (err) {
532 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800533 "request_irq for %s failed: %d\n",
534 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000535 free_irq(adapter->msix_entries[0].vector, netdev);
536 }
537 return err;
538}
539
540/**
541 * i40evf_free_traffic_irqs - Free MSI-X interrupts
542 * @adapter: board private structure
543 *
544 * Frees all MSI-X vectors other than 0.
545 **/
546static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
547{
548 int i;
549 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000550
Greg Rose5eae00c2013-12-21 06:12:45 +0000551 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
552
553 for (i = 0; i < q_vectors; i++) {
554 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
555 NULL);
556 free_irq(adapter->msix_entries[i+1].vector,
557 adapter->q_vector[i]);
558 }
559}
560
561/**
562 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
563 * @adapter: board private structure
564 *
565 * Frees MSI-X vector 0.
566 **/
567static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
568{
569 struct net_device *netdev = adapter->netdev;
570
571 free_irq(adapter->msix_entries[0].vector, netdev);
572}
573
574/**
575 * i40evf_configure_tx - Configure Transmit Unit after Reset
576 * @adapter: board private structure
577 *
578 * Configure the Tx unit of the MAC after a reset.
579 **/
580static void i40evf_configure_tx(struct i40evf_adapter *adapter)
581{
582 struct i40e_hw *hw = &adapter->hw;
583 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000584
Mitch Williamscc052922014-10-25 03:24:34 +0000585 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000586 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
587}
588
589/**
590 * i40evf_configure_rx - Configure Receive Unit after Reset
591 * @adapter: board private structure
592 *
593 * Configure the Rx unit of the MAC after a reset.
594 **/
595static void i40evf_configure_rx(struct i40evf_adapter *adapter)
596{
597 struct i40e_hw *hw = &adapter->hw;
598 struct net_device *netdev = adapter->netdev;
599 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
600 int i;
601 int rx_buf_len;
602
603
604 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
605 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
606
607 /* Decide whether to use packet split mode or not */
608 if (netdev->mtu > ETH_DATA_LEN) {
609 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
610 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
611 else
612 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
613 } else {
614 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
615 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
616 else
617 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
618 }
619
620 /* Set the RX buffer length according to the mode */
621 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
622 rx_buf_len = I40E_RX_HDR_SIZE;
623 } else {
624 if (netdev->mtu <= ETH_DATA_LEN)
625 rx_buf_len = I40EVF_RXBUFFER_2048;
626 else
627 rx_buf_len = ALIGN(max_frame, 1024);
628 }
629
Mitch Williamscc052922014-10-25 03:24:34 +0000630 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000631 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
632 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
633 }
634}
635
636/**
637 * i40evf_find_vlan - Search filter list for specific vlan filter
638 * @adapter: board private structure
639 * @vlan: vlan tag
640 *
641 * Returns ptr to the filter object or NULL
642 **/
643static struct
644i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
645{
646 struct i40evf_vlan_filter *f;
647
648 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
649 if (vlan == f->vlan)
650 return f;
651 }
652 return NULL;
653}
654
655/**
656 * i40evf_add_vlan - Add a vlan filter to the list
657 * @adapter: board private structure
658 * @vlan: VLAN tag
659 *
660 * Returns ptr to the filter object or NULL when no memory available.
661 **/
662static struct
663i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
664{
665 struct i40evf_vlan_filter *f;
666
667 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000668 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000669 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000670 if (!f)
Greg Rose5eae00c2013-12-21 06:12:45 +0000671 return NULL;
Mitch Williams249c8b82014-05-10 04:49:04 +0000672
Greg Rose5eae00c2013-12-21 06:12:45 +0000673 f->vlan = vlan;
674
675 INIT_LIST_HEAD(&f->list);
676 list_add(&f->list, &adapter->vlan_filter_list);
677 f->add = true;
678 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
679 }
680
681 return f;
682}
683
684/**
685 * i40evf_del_vlan - Remove a vlan filter from the list
686 * @adapter: board private structure
687 * @vlan: VLAN tag
688 **/
689static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
690{
691 struct i40evf_vlan_filter *f;
692
693 f = i40evf_find_vlan(adapter, vlan);
694 if (f) {
695 f->remove = true;
696 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
697 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000698}
699
700/**
701 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
702 * @netdev: network device struct
703 * @vid: VLAN tag
704 **/
705static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000706 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000707{
708 struct i40evf_adapter *adapter = netdev_priv(netdev);
709
710 if (i40evf_add_vlan(adapter, vid) == NULL)
711 return -ENOMEM;
712 return 0;
713}
714
715/**
716 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
717 * @netdev: network device struct
718 * @vid: VLAN tag
719 **/
720static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000721 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000722{
723 struct i40evf_adapter *adapter = netdev_priv(netdev);
724
725 i40evf_del_vlan(adapter, vid);
726 return 0;
727}
728
729/**
730 * i40evf_find_filter - Search filter list for specific mac filter
731 * @adapter: board private structure
732 * @macaddr: the MAC address
733 *
734 * Returns ptr to the filter object or NULL
735 **/
736static struct
737i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
738 u8 *macaddr)
739{
740 struct i40evf_mac_filter *f;
741
742 if (!macaddr)
743 return NULL;
744
745 list_for_each_entry(f, &adapter->mac_filter_list, list) {
746 if (ether_addr_equal(macaddr, f->macaddr))
747 return f;
748 }
749 return NULL;
750}
751
752/**
753 * i40e_add_filter - Add a mac filter to the filter list
754 * @adapter: board private structure
755 * @macaddr: the MAC address
756 *
757 * Returns ptr to the filter object or NULL when no memory available.
758 **/
759static struct
760i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
761 u8 *macaddr)
762{
763 struct i40evf_mac_filter *f;
764
765 if (!macaddr)
766 return NULL;
767
768 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
769 &adapter->crit_section))
Mitch Williamsb65476c2014-07-09 07:46:14 +0000770 udelay(1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000771
772 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000773 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000774 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000775 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000776 clear_bit(__I40EVF_IN_CRITICAL_TASK,
777 &adapter->crit_section);
778 return NULL;
779 }
780
Greg Rose9a173902014-05-22 06:32:02 +0000781 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000782
783 list_add(&f->list, &adapter->mac_filter_list);
784 f->add = true;
785 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
786 }
787
788 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
789 return f;
790}
791
792/**
793 * i40evf_set_mac - NDO callback to set port mac address
794 * @netdev: network interface device structure
795 * @p: pointer to an address structure
796 *
797 * Returns 0 on success, negative on failure
798 **/
799static int i40evf_set_mac(struct net_device *netdev, void *p)
800{
801 struct i40evf_adapter *adapter = netdev_priv(netdev);
802 struct i40e_hw *hw = &adapter->hw;
803 struct i40evf_mac_filter *f;
804 struct sockaddr *addr = p;
805
806 if (!is_valid_ether_addr(addr->sa_data))
807 return -EADDRNOTAVAIL;
808
809 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
810 return 0;
811
812 f = i40evf_add_filter(adapter, addr->sa_data);
813 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000814 ether_addr_copy(hw->mac.addr, addr->sa_data);
815 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000816 }
817
818 return (f == NULL) ? -ENOMEM : 0;
819}
820
821/**
822 * i40evf_set_rx_mode - NDO callback to set the netdev filters
823 * @netdev: network interface device structure
824 **/
825static void i40evf_set_rx_mode(struct net_device *netdev)
826{
827 struct i40evf_adapter *adapter = netdev_priv(netdev);
828 struct i40evf_mac_filter *f, *ftmp;
829 struct netdev_hw_addr *uca;
830 struct netdev_hw_addr *mca;
831
832 /* add addr if not already in the filter list */
833 netdev_for_each_uc_addr(uca, netdev) {
834 i40evf_add_filter(adapter, uca->addr);
835 }
836 netdev_for_each_mc_addr(mca, netdev) {
837 i40evf_add_filter(adapter, mca->addr);
838 }
839
840 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
841 &adapter->crit_section))
Mitch Williamsb65476c2014-07-09 07:46:14 +0000842 udelay(1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000843 /* remove filter if not in netdev list */
844 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
845 bool found = false;
846
Tobias Klauserdc5f2de2014-05-20 08:23:06 +0000847 if (is_multicast_ether_addr(f->macaddr)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000848 netdev_for_each_mc_addr(mca, netdev) {
849 if (ether_addr_equal(mca->addr, f->macaddr)) {
850 found = true;
851 break;
852 }
853 }
854 } else {
855 netdev_for_each_uc_addr(uca, netdev) {
856 if (ether_addr_equal(uca->addr, f->macaddr)) {
857 found = true;
858 break;
859 }
860 }
861 }
862 if (found) {
863 f->remove = true;
864 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
865 }
866 }
867 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
868}
869
870/**
871 * i40evf_napi_enable_all - enable NAPI on all queue vectors
872 * @adapter: board private structure
873 **/
874static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
875{
876 int q_idx;
877 struct i40e_q_vector *q_vector;
878 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
879
880 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
881 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000882
Greg Rose5eae00c2013-12-21 06:12:45 +0000883 q_vector = adapter->q_vector[q_idx];
884 napi = &q_vector->napi;
885 napi_enable(napi);
886 }
887}
888
889/**
890 * i40evf_napi_disable_all - disable NAPI on all queue vectors
891 * @adapter: board private structure
892 **/
893static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
894{
895 int q_idx;
896 struct i40e_q_vector *q_vector;
897 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
898
899 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
900 q_vector = adapter->q_vector[q_idx];
901 napi_disable(&q_vector->napi);
902 }
903}
904
905/**
906 * i40evf_configure - set up transmit and receive data structures
907 * @adapter: board private structure
908 **/
909static void i40evf_configure(struct i40evf_adapter *adapter)
910{
911 struct net_device *netdev = adapter->netdev;
912 int i;
913
914 i40evf_set_rx_mode(netdev);
915
916 i40evf_configure_tx(adapter);
917 i40evf_configure_rx(adapter);
918 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
919
Mitch Williamscc052922014-10-25 03:24:34 +0000920 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000921 struct i40e_ring *ring = adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000922
Mitch Williamsa132af22015-01-24 09:58:35 +0000923 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Greg Rose5eae00c2013-12-21 06:12:45 +0000924 ring->next_to_use = ring->count - 1;
925 writel(ring->next_to_use, ring->tail);
926 }
927}
928
929/**
930 * i40evf_up_complete - Finish the last steps of bringing up a connection
931 * @adapter: board private structure
932 **/
933static int i40evf_up_complete(struct i40evf_adapter *adapter)
934{
935 adapter->state = __I40EVF_RUNNING;
936 clear_bit(__I40E_DOWN, &adapter->vsi.state);
937
938 i40evf_napi_enable_all(adapter);
939
940 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
941 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
942 return 0;
943}
944
945/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000946 * i40e_down - Shutdown the connection processing
947 * @adapter: board private structure
948 **/
949void i40evf_down(struct i40evf_adapter *adapter)
950{
951 struct net_device *netdev = adapter->netdev;
952 struct i40evf_mac_filter *f;
953
Mitch Williamsddf0b3a2014-05-22 06:31:46 +0000954 if (adapter->state == __I40EVF_DOWN)
955 return;
956
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000957 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
958 &adapter->crit_section))
959 usleep_range(500, 1000);
960
961 i40evf_irq_disable(adapter);
Mitch Williams748c4342015-01-29 07:17:18 +0000962 i40evf_napi_disable_all(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000963
Mitch Williamsef8693e2014-02-13 03:48:53 -0800964 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +0000965 list_for_each_entry(f, &adapter->mac_filter_list, list) {
966 f->remove = true;
967 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800968 /* remove all VLAN filters */
969 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
970 f->remove = true;
971 }
Mitch Williamsef8693e2014-02-13 03:48:53 -0800972 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
973 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000974 /* cancel any current operation */
975 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
976 adapter->aq_pending = 0;
977 /* Schedule operations to close down the HW. Don't wait
978 * here for this to complete. The watchdog is still running
979 * and it will take care of this.
980 */
981 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800982 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800983 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800984 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000985 netif_tx_disable(netdev);
986
987 netif_tx_stop_all_queues(netdev);
988
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000989 msleep(20);
990
Greg Rose5eae00c2013-12-21 06:12:45 +0000991 netif_carrier_off(netdev);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000992 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000993}
994
995/**
996 * i40evf_acquire_msix_vectors - Setup the MSIX capability
997 * @adapter: board private structure
998 * @vectors: number of vectors to request
999 *
1000 * Work with the OS to set up the MSIX vectors needed.
1001 *
1002 * Returns 0 on success, negative on failure
1003 **/
1004static int
1005i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1006{
1007 int err, vector_threshold;
1008
1009 /* We'll want at least 3 (vector_threshold):
1010 * 0) Other (Admin Queue and link, mostly)
1011 * 1) TxQ[0] Cleanup
1012 * 2) RxQ[0] Cleanup
1013 */
1014 vector_threshold = MIN_MSIX_COUNT;
1015
1016 /* The more we get, the more we will assign to Tx/Rx Cleanup
1017 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1018 * Right now, we simply care about how many we'll get; we'll
1019 * set them up later while requesting irq's.
1020 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001021 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1022 vector_threshold, vectors);
1023 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001024 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001025 kfree(adapter->msix_entries);
1026 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001027 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001028 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001029
1030 /* Adjust for only the vectors we'll use, which is minimum
1031 * of max_msix_q_vectors + NONQ_VECS, or the number of
1032 * vectors we were allocated.
1033 */
1034 adapter->num_msix_vectors = err;
1035 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001036}
1037
1038/**
1039 * i40evf_free_queues - Free memory for all rings
1040 * @adapter: board private structure to initialize
1041 *
1042 * Free all of the memory associated with queue pairs.
1043 **/
1044static void i40evf_free_queues(struct i40evf_adapter *adapter)
1045{
1046 int i;
1047
1048 if (!adapter->vsi_res)
1049 return;
Mitch Williamscc052922014-10-25 03:24:34 +00001050 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001051 if (adapter->tx_rings[i])
1052 kfree_rcu(adapter->tx_rings[i], rcu);
1053 adapter->tx_rings[i] = NULL;
1054 adapter->rx_rings[i] = NULL;
1055 }
1056}
1057
1058/**
1059 * i40evf_alloc_queues - Allocate memory for all rings
1060 * @adapter: board private structure to initialize
1061 *
1062 * We allocate one ring per queue at run-time since we don't know the
1063 * number of queues at compile-time. The polling_netdev array is
1064 * intended for Multiqueue, but should work fine with a single queue.
1065 **/
1066static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1067{
1068 int i;
1069
Mitch Williamscc052922014-10-25 03:24:34 +00001070 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001071 struct i40e_ring *tx_ring;
1072 struct i40e_ring *rx_ring;
1073
Mitch Williams75a64432014-11-11 20:02:42 +00001074 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001075 if (!tx_ring)
1076 goto err_out;
1077
1078 tx_ring->queue_index = i;
1079 tx_ring->netdev = adapter->netdev;
1080 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001081 tx_ring->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001082 adapter->tx_rings[i] = tx_ring;
1083
1084 rx_ring = &tx_ring[1];
1085 rx_ring->queue_index = i;
1086 rx_ring->netdev = adapter->netdev;
1087 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001088 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001089 adapter->rx_rings[i] = rx_ring;
1090 }
1091
1092 return 0;
1093
1094err_out:
1095 i40evf_free_queues(adapter);
1096 return -ENOMEM;
1097}
1098
1099/**
1100 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1101 * @adapter: board private structure to initialize
1102 *
1103 * Attempt to configure the interrupts using the best available
1104 * capabilities of the hardware and the kernel.
1105 **/
1106static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1107{
1108 int vector, v_budget;
1109 int pairs = 0;
1110 int err = 0;
1111
1112 if (!adapter->vsi_res) {
1113 err = -EIO;
1114 goto out;
1115 }
Mitch Williamscc052922014-10-25 03:24:34 +00001116 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001117
1118 /* It's easy to be greedy for MSI-X vectors, but it really
1119 * doesn't do us much good if we have a lot more vectors
1120 * than CPU's. So let's be conservative and only ask for
1121 * (roughly) twice the number of vectors as there are CPU's.
1122 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001123 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1124 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001125
Greg Rose5eae00c2013-12-21 06:12:45 +00001126 adapter->msix_entries = kcalloc(v_budget,
1127 sizeof(struct msix_entry), GFP_KERNEL);
1128 if (!adapter->msix_entries) {
1129 err = -ENOMEM;
1130 goto out;
1131 }
1132
1133 for (vector = 0; vector < v_budget; vector++)
1134 adapter->msix_entries[vector].entry = vector;
1135
1136 i40evf_acquire_msix_vectors(adapter, v_budget);
1137
1138out:
1139 adapter->netdev->real_num_tx_queues = pairs;
1140 return err;
1141}
1142
1143/**
1144 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1145 * @adapter: board private structure to initialize
1146 *
1147 * We allocate one q_vector per queue interrupt. If allocation fails we
1148 * return -ENOMEM.
1149 **/
1150static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1151{
1152 int q_idx, num_q_vectors;
1153 struct i40e_q_vector *q_vector;
1154
1155 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1156
1157 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams75a64432014-11-11 20:02:42 +00001158 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001159 if (!q_vector)
1160 goto err_out;
1161 q_vector->adapter = adapter;
1162 q_vector->vsi = &adapter->vsi;
1163 q_vector->v_idx = q_idx;
1164 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001165 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001166 adapter->q_vector[q_idx] = q_vector;
1167 }
1168
1169 return 0;
1170
1171err_out:
1172 while (q_idx) {
1173 q_idx--;
1174 q_vector = adapter->q_vector[q_idx];
1175 netif_napi_del(&q_vector->napi);
1176 kfree(q_vector);
1177 adapter->q_vector[q_idx] = NULL;
1178 }
1179 return -ENOMEM;
1180}
1181
1182/**
1183 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1184 * @adapter: board private structure to initialize
1185 *
1186 * This function frees the memory allocated to the q_vectors. In addition if
1187 * NAPI is enabled it will delete any references to the NAPI struct prior
1188 * to freeing the q_vector.
1189 **/
1190static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1191{
1192 int q_idx, num_q_vectors;
1193 int napi_vectors;
1194
1195 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001196 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001197
1198 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1199 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1200
1201 adapter->q_vector[q_idx] = NULL;
1202 if (q_idx < napi_vectors)
1203 netif_napi_del(&q_vector->napi);
1204 kfree(q_vector);
1205 }
1206}
1207
1208/**
1209 * i40evf_reset_interrupt_capability - Reset MSIX setup
1210 * @adapter: board private structure
1211 *
1212 **/
1213void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1214{
1215 pci_disable_msix(adapter->pdev);
1216 kfree(adapter->msix_entries);
1217 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001218}
1219
1220/**
1221 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1222 * @adapter: board private structure to initialize
1223 *
1224 **/
1225int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1226{
1227 int err;
1228
1229 err = i40evf_set_interrupt_capability(adapter);
1230 if (err) {
1231 dev_err(&adapter->pdev->dev,
1232 "Unable to setup interrupt capabilities\n");
1233 goto err_set_interrupt;
1234 }
1235
1236 err = i40evf_alloc_q_vectors(adapter);
1237 if (err) {
1238 dev_err(&adapter->pdev->dev,
1239 "Unable to allocate memory for queue vectors\n");
1240 goto err_alloc_q_vectors;
1241 }
1242
1243 err = i40evf_alloc_queues(adapter);
1244 if (err) {
1245 dev_err(&adapter->pdev->dev,
1246 "Unable to allocate memory for queues\n");
1247 goto err_alloc_queues;
1248 }
1249
1250 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001251 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1252 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001253
1254 return 0;
1255err_alloc_queues:
1256 i40evf_free_q_vectors(adapter);
1257err_alloc_q_vectors:
1258 i40evf_reset_interrupt_capability(adapter);
1259err_set_interrupt:
1260 return err;
1261}
1262
1263/**
1264 * i40evf_watchdog_timer - Periodic call-back timer
1265 * @data: pointer to adapter disguised as unsigned long
1266 **/
1267static void i40evf_watchdog_timer(unsigned long data)
1268{
1269 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001270
Greg Rose5eae00c2013-12-21 06:12:45 +00001271 schedule_work(&adapter->watchdog_task);
1272 /* timer will be rescheduled in watchdog task */
1273}
1274
1275/**
1276 * i40evf_watchdog_task - Periodic call-back task
1277 * @work: pointer to work_struct
1278 **/
1279static void i40evf_watchdog_task(struct work_struct *work)
1280{
1281 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001282 struct i40evf_adapter,
1283 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001284 struct i40e_hw *hw = &adapter->hw;
Ashish Shahfd358862014-08-01 13:27:11 -07001285 uint32_t rstat_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001286
Greg Rose5eae00c2013-12-21 06:12:45 +00001287 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001288 goto restart_watchdog;
1289
1290 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Ashish Shahfd358862014-08-01 13:27:11 -07001291 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1292 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1293 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1294 (rstat_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001295 /* A chance for redemption! */
1296 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1297 adapter->state = __I40EVF_STARTUP;
1298 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1299 schedule_delayed_work(&adapter->init_task, 10);
1300 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1301 &adapter->crit_section);
1302 /* Don't reschedule the watchdog, since we've restarted
1303 * the init task. When init_task contacts the PF and
1304 * gets everything set up again, it'll restart the
1305 * watchdog for us. Down, boy. Sit. Stay. Woof.
1306 */
1307 return;
1308 }
1309 adapter->aq_pending = 0;
1310 adapter->aq_required = 0;
1311 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1312 goto watchdog_done;
1313 }
1314
1315 if ((adapter->state < __I40EVF_DOWN) ||
1316 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001317 goto watchdog_done;
1318
Mitch Williamsef8693e2014-02-13 03:48:53 -08001319 /* check for reset */
Ashish Shahfd358862014-08-01 13:27:11 -07001320 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
Mitch Williams75a64432014-11-11 20:02:42 +00001321 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001322 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
Ashish Shahfd358862014-08-01 13:27:11 -07001323 (rstat_val != I40E_VFR_VFACTIVE) &&
1324 (rstat_val != I40E_VFR_COMPLETED)) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001325 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001326 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001327 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001328 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001329 adapter->aq_pending = 0;
1330 adapter->aq_required = 0;
1331 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001332 goto watchdog_done;
1333 }
1334
1335 /* Process admin queue tasks. After init, everything gets done
1336 * here so we don't race on the admin queue.
1337 */
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001338 if (adapter->aq_pending) {
1339 if (!i40evf_asq_done(hw)) {
1340 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1341 i40evf_send_api_ver(adapter);
1342 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001343 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001344 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001345
1346 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1347 i40evf_map_queues(adapter);
1348 goto watchdog_done;
1349 }
1350
1351 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1352 i40evf_add_ether_addrs(adapter);
1353 goto watchdog_done;
1354 }
1355
1356 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1357 i40evf_add_vlans(adapter);
1358 goto watchdog_done;
1359 }
1360
1361 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1362 i40evf_del_ether_addrs(adapter);
1363 goto watchdog_done;
1364 }
1365
1366 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1367 i40evf_del_vlans(adapter);
1368 goto watchdog_done;
1369 }
1370
1371 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1372 i40evf_disable_queues(adapter);
1373 goto watchdog_done;
1374 }
1375
1376 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1377 i40evf_configure_queues(adapter);
1378 goto watchdog_done;
1379 }
1380
1381 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1382 i40evf_enable_queues(adapter);
1383 goto watchdog_done;
1384 }
1385
1386 if (adapter->state == __I40EVF_RUNNING)
1387 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001388watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001389 if (adapter->state == __I40EVF_RUNNING) {
1390 i40evf_irq_enable_queues(adapter, ~0);
1391 i40evf_fire_sw_int(adapter, 0xFF);
1392 } else {
1393 i40evf_fire_sw_int(adapter, 0x1);
1394 }
1395
Mitch Williamsef8693e2014-02-13 03:48:53 -08001396 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1397restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001398 if (adapter->state == __I40EVF_REMOVE)
1399 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001400 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));
Greg Rose5eae00c2013-12-21 06:12:45 +00001405 schedule_work(&adapter->adminq_task);
1406}
1407
Mitch A Williams5b7af022014-03-28 06:49:02 +00001408/**
Catherine Sullivan3dd55502014-05-20 08:01:36 +00001409 * next_queue - increment to next available tx queue
Mitch A Williams5b7af022014-03-28 06:49:02 +00001410 * @adapter: board private structure
1411 * @j: queue counter
1412 *
1413 * Helper function for RSS programming to increment through available
1414 * queus. Returns the next queue value.
1415 **/
Mitch Williams96d47702014-02-11 08:27:50 +00001416static int next_queue(struct i40evf_adapter *adapter, int j)
1417{
1418 j += 1;
1419
Mitch Williamscc052922014-10-25 03:24:34 +00001420 return j >= adapter->num_active_queues ? 0 : j;
Mitch Williams96d47702014-02-11 08:27:50 +00001421}
1422
Greg Rose5eae00c2013-12-21 06:12:45 +00001423/**
1424 * i40evf_configure_rss - Prepare for RSS if used
1425 * @adapter: board private structure
1426 **/
1427static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1428{
Eric Dumazet22f258a2014-11-16 06:23:13 -08001429 u32 rss_key[I40E_VFQF_HKEY_MAX_INDEX + 1];
Greg Rose5eae00c2013-12-21 06:12:45 +00001430 struct i40e_hw *hw = &adapter->hw;
1431 u32 lut = 0;
1432 int i, j;
1433 u64 hena;
1434
Mitch Williamscc052922014-10-25 03:24:34 +00001435 /* No RSS for single queue. */
1436 if (adapter->num_active_queues == 1) {
1437 wr32(hw, I40E_VFQF_HENA(0), 0);
1438 wr32(hw, I40E_VFQF_HENA(1), 0);
1439 return;
1440 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001441
Mitch Williamscc052922014-10-25 03:24:34 +00001442 /* Hash type is configured by the PF - we just supply the key */
Eric Dumazet22f258a2014-11-16 06:23:13 -08001443 netdev_rss_key_fill(rss_key, sizeof(rss_key));
Greg Rose5eae00c2013-12-21 06:12:45 +00001444 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
Eric Dumazet22f258a2014-11-16 06:23:13 -08001445 wr32(hw, I40E_VFQF_HKEY(i), rss_key[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001446
1447 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1448 hena = I40E_DEFAULT_RSS_HENA;
1449 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1450 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1451
1452 /* Populate the LUT with max no. of queues in round robin fashion */
Mitch Williamscc052922014-10-25 03:24:34 +00001453 j = adapter->num_active_queues;
Mitch Williams96d47702014-02-11 08:27:50 +00001454 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
Mitch A Williams5b7af022014-03-28 06:49:02 +00001455 j = next_queue(adapter, j);
1456 lut = j;
1457 j = next_queue(adapter, j);
1458 lut |= j << 8;
1459 j = next_queue(adapter, j);
1460 lut |= j << 16;
1461 j = next_queue(adapter, j);
1462 lut |= j << 24;
Mitch Williams96d47702014-02-11 08:27:50 +00001463 wr32(hw, I40E_VFQF_HLUT(i), lut);
Greg Rose5eae00c2013-12-21 06:12:45 +00001464 }
1465 i40e_flush(hw);
1466}
1467
Mitch Williamsef8693e2014-02-13 03:48:53 -08001468#define I40EVF_RESET_WAIT_MS 100
1469#define I40EVF_RESET_WAIT_COUNT 200
Greg Rose5eae00c2013-12-21 06:12:45 +00001470/**
1471 * i40evf_reset_task - Call-back task to handle hardware reset
1472 * @work: pointer to work_struct
1473 *
1474 * During reset we need to shut down and reinitialize the admin queue
1475 * before we can use it to communicate with the PF again. We also clear
1476 * and reinit the rings because that context is lost as well.
1477 **/
1478static void i40evf_reset_task(struct work_struct *work)
1479{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001480 struct i40evf_adapter *adapter = container_of(work,
1481 struct i40evf_adapter,
1482 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001483 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001484 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001485 struct i40evf_mac_filter *f;
Greg Rose5eae00c2013-12-21 06:12:45 +00001486 uint32_t rstat_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001487 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001488
1489 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1490 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001491 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001492
1493 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1494 dev_info(&adapter->pdev->dev, "Requesting reset from PF\n");
1495 i40evf_request_reset(adapter);
1496 }
1497
Mitch Williamsef8693e2014-02-13 03:48:53 -08001498 /* poll until we see the reset actually happen */
1499 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001500 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1501 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Ashish Shahfd358862014-08-01 13:27:11 -07001502 if ((rstat_val != I40E_VFR_VFACTIVE) &&
1503 (rstat_val != I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00001504 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001505 msleep(I40EVF_RESET_WAIT_MS);
Greg Rose5eae00c2013-12-21 06:12:45 +00001506 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001507 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001508 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1509 goto continue_reset; /* act like the reset happened */
1510 }
1511
1512 /* wait until the reset is complete and the PF is responding to us */
1513 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1514 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1515 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Ashish Shahfd358862014-08-01 13:27:11 -07001516 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1517 (rstat_val == I40E_VFR_COMPLETED))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001518 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001519 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001520 }
1521 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams6ba36a22014-08-01 13:27:14 -07001522 struct i40evf_mac_filter *f, *ftmp;
1523 struct i40evf_vlan_filter *fv, *fvtmp;
1524
Greg Rose5eae00c2013-12-21 06:12:45 +00001525 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001526 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsef8693e2014-02-13 03:48:53 -08001527 rstat_val);
1528 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1529
Mitch Williams169f4072014-04-01 07:11:50 +00001530 if (netif_running(adapter->netdev)) {
1531 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001532 i40evf_irq_disable(adapter);
1533 i40evf_napi_disable_all(adapter);
1534 netif_tx_disable(netdev);
1535 netif_tx_stop_all_queues(netdev);
1536 netif_carrier_off(netdev);
Mitch Williams169f4072014-04-01 07:11:50 +00001537 i40evf_free_traffic_irqs(adapter);
1538 i40evf_free_all_tx_resources(adapter);
1539 i40evf_free_all_rx_resources(adapter);
1540 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001541
1542 /* Delete all of the filters, both MAC and VLAN. */
1543 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1544 list) {
1545 list_del(&f->list);
1546 kfree(f);
1547 }
1548 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1549 list) {
1550 list_del(&fv->list);
1551 kfree(fv);
1552 }
1553
Mitch Williamsef8693e2014-02-13 03:48:53 -08001554 i40evf_free_misc_irq(adapter);
1555 i40evf_reset_interrupt_capability(adapter);
1556 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001557 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001558 kfree(adapter->vf_res);
1559 i40evf_shutdown_adminq(hw);
1560 adapter->netdev->flags &= ~IFF_UP;
1561 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1562 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001563 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001564
1565continue_reset:
1566 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1567
Mitch Williamsac833bb2015-01-29 07:17:19 +00001568 i40evf_irq_disable(adapter);
1569 i40evf_napi_disable_all(adapter);
1570
1571 netif_tx_disable(netdev);
1572
1573 netif_tx_stop_all_queues(netdev);
1574
1575 netif_carrier_off(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001576 adapter->state = __I40EVF_RESETTING;
1577
1578 /* kill and reinit the admin queue */
1579 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001580 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1581 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001582 err = i40evf_init_adminq(hw);
1583 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001584 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1585 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001586
Greg Rose5eae00c2013-12-21 06:12:45 +00001587 i40evf_map_queues(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001588
1589 /* re-add all MAC filters */
1590 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1591 f->add = true;
1592 }
1593 /* re-add all VLAN filters */
1594 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1595 f->add = true;
1596 }
1597 adapter->aq_required = I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1598 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001599 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1600
1601 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1602
1603 if (netif_running(adapter->netdev)) {
1604 /* allocate transmit descriptors */
1605 err = i40evf_setup_all_tx_resources(adapter);
1606 if (err)
1607 goto reset_err;
1608
1609 /* allocate receive descriptors */
1610 err = i40evf_setup_all_rx_resources(adapter);
1611 if (err)
1612 goto reset_err;
1613
1614 i40evf_configure(adapter);
1615
1616 err = i40evf_up_complete(adapter);
1617 if (err)
1618 goto reset_err;
1619
1620 i40evf_irq_enable(adapter, true);
1621 }
1622 return;
1623reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001624 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001625 i40evf_close(adapter->netdev);
1626}
1627
1628/**
1629 * i40evf_adminq_task - worker thread to clean the admin queue
1630 * @work: pointer to work_struct containing our data
1631 **/
1632static void i40evf_adminq_task(struct work_struct *work)
1633{
1634 struct i40evf_adapter *adapter =
1635 container_of(work, struct i40evf_adapter, adminq_task);
1636 struct i40e_hw *hw = &adapter->hw;
1637 struct i40e_arq_event_info event;
1638 struct i40e_virtchnl_msg *v_msg;
1639 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001640 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001641 u16 pending;
1642
Mitch Williamsef8693e2014-02-13 03:48:53 -08001643 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001644 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001645
Mitch Williams1001dc32014-11-11 20:02:19 +00001646 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1647 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001648 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001649 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001650
Greg Rose5eae00c2013-12-21 06:12:45 +00001651 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1652 do {
1653 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001654 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001655 break; /* No event to process or error cleaning ARQ */
1656
1657 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1658 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001659 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001660 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001661 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001662 } while (pending);
1663
Mitch Williams912257e2014-05-22 06:32:07 +00001664 /* check for error indications */
1665 val = rd32(hw, hw->aq.arq.len);
1666 oldval = val;
1667 if (val & I40E_VF_ARQLEN_ARQVFE_MASK) {
1668 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1669 val &= ~I40E_VF_ARQLEN_ARQVFE_MASK;
1670 }
1671 if (val & I40E_VF_ARQLEN_ARQOVFL_MASK) {
1672 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1673 val &= ~I40E_VF_ARQLEN_ARQOVFL_MASK;
1674 }
1675 if (val & I40E_VF_ARQLEN_ARQCRIT_MASK) {
1676 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1677 val &= ~I40E_VF_ARQLEN_ARQCRIT_MASK;
1678 }
1679 if (oldval != val)
1680 wr32(hw, hw->aq.arq.len, val);
1681
1682 val = rd32(hw, hw->aq.asq.len);
1683 oldval = val;
1684 if (val & I40E_VF_ATQLEN_ATQVFE_MASK) {
1685 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1686 val &= ~I40E_VF_ATQLEN_ATQVFE_MASK;
1687 }
1688 if (val & I40E_VF_ATQLEN_ATQOVFL_MASK) {
1689 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1690 val &= ~I40E_VF_ATQLEN_ATQOVFL_MASK;
1691 }
1692 if (val & I40E_VF_ATQLEN_ATQCRIT_MASK) {
1693 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1694 val &= ~I40E_VF_ATQLEN_ATQCRIT_MASK;
1695 }
1696 if (oldval != val)
1697 wr32(hw, hw->aq.asq.len, val);
1698
Mitch A Williams72354482014-12-09 08:53:07 +00001699 kfree(event.msg_buf);
1700out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001701 /* re-enable Admin queue interrupt cause */
1702 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001703}
1704
1705/**
1706 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1707 * @adapter: board private structure
1708 *
1709 * Free all transmit software resources
1710 **/
1711static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1712{
1713 int i;
1714
Mitch Williamscc052922014-10-25 03:24:34 +00001715 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001716 if (adapter->tx_rings[i]->desc)
1717 i40evf_free_tx_resources(adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001718}
1719
1720/**
1721 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1722 * @adapter: board private structure
1723 *
1724 * If this function returns with an error, then it's possible one or
1725 * more of the rings is populated (while the rest are not). It is the
1726 * callers duty to clean those orphaned rings.
1727 *
1728 * Return 0 on success, negative on failure
1729 **/
1730static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1731{
1732 int i, err = 0;
1733
Mitch Williamscc052922014-10-25 03:24:34 +00001734 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001735 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001736 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1737 if (!err)
1738 continue;
1739 dev_err(&adapter->pdev->dev,
1740 "%s: Allocation for Tx Queue %u failed\n",
1741 __func__, i);
1742 break;
1743 }
1744
1745 return err;
1746}
1747
1748/**
1749 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1750 * @adapter: board private structure
1751 *
1752 * If this function returns with an error, then it's possible one or
1753 * more of the rings is populated (while the rest are not). It is the
1754 * callers duty to clean those orphaned rings.
1755 *
1756 * Return 0 on success, negative on failure
1757 **/
1758static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1759{
1760 int i, err = 0;
1761
Mitch Williamscc052922014-10-25 03:24:34 +00001762 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001763 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001764 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1765 if (!err)
1766 continue;
1767 dev_err(&adapter->pdev->dev,
1768 "%s: Allocation for Rx Queue %u failed\n",
1769 __func__, i);
1770 break;
1771 }
1772 return err;
1773}
1774
1775/**
1776 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1777 * @adapter: board private structure
1778 *
1779 * Free all receive software resources
1780 **/
1781static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1782{
1783 int i;
1784
Mitch Williamscc052922014-10-25 03:24:34 +00001785 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001786 if (adapter->rx_rings[i]->desc)
1787 i40evf_free_rx_resources(adapter->rx_rings[i]);
1788}
1789
1790/**
1791 * i40evf_open - Called when a network interface is made active
1792 * @netdev: network interface device structure
1793 *
1794 * Returns 0 on success, negative value on failure
1795 *
1796 * The open entry point is called when a network interface is made
1797 * active by the system (IFF_UP). At this point all resources needed
1798 * for transmit and receive operations are allocated, the interrupt
1799 * handler is registered with the OS, the watchdog timer is started,
1800 * and the stack is notified that the interface is ready.
1801 **/
1802static int i40evf_open(struct net_device *netdev)
1803{
1804 struct i40evf_adapter *adapter = netdev_priv(netdev);
1805 int err;
1806
Mitch Williamsef8693e2014-02-13 03:48:53 -08001807 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1808 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1809 return -EIO;
1810 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001811 if (adapter->state != __I40EVF_DOWN)
1812 return -EBUSY;
1813
1814 /* allocate transmit descriptors */
1815 err = i40evf_setup_all_tx_resources(adapter);
1816 if (err)
1817 goto err_setup_tx;
1818
1819 /* allocate receive descriptors */
1820 err = i40evf_setup_all_rx_resources(adapter);
1821 if (err)
1822 goto err_setup_rx;
1823
1824 /* clear any pending interrupts, may auto mask */
1825 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1826 if (err)
1827 goto err_req_irq;
1828
1829 i40evf_configure(adapter);
1830
1831 err = i40evf_up_complete(adapter);
1832 if (err)
1833 goto err_req_irq;
1834
1835 i40evf_irq_enable(adapter, true);
1836
1837 return 0;
1838
1839err_req_irq:
1840 i40evf_down(adapter);
1841 i40evf_free_traffic_irqs(adapter);
1842err_setup_rx:
1843 i40evf_free_all_rx_resources(adapter);
1844err_setup_tx:
1845 i40evf_free_all_tx_resources(adapter);
1846
1847 return err;
1848}
1849
1850/**
1851 * i40evf_close - Disables a network interface
1852 * @netdev: network interface device structure
1853 *
1854 * Returns 0, this is not allowed to fail
1855 *
1856 * The close entry point is called when an interface is de-activated
1857 * by the OS. The hardware is still under the drivers control, but
1858 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1859 * are freed, along with all transmit and receive resources.
1860 **/
1861static int i40evf_close(struct net_device *netdev)
1862{
1863 struct i40evf_adapter *adapter = netdev_priv(netdev);
1864
Mitch Williamsef8693e2014-02-13 03:48:53 -08001865 if (adapter->state <= __I40EVF_DOWN)
1866 return 0;
1867
Mitch Williamsef8693e2014-02-13 03:48:53 -08001868
Greg Rose5eae00c2013-12-21 06:12:45 +00001869 set_bit(__I40E_DOWN, &adapter->vsi.state);
1870
1871 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001872 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001873 i40evf_free_traffic_irqs(adapter);
1874
1875 i40evf_free_all_tx_resources(adapter);
1876 i40evf_free_all_rx_resources(adapter);
1877
1878 return 0;
1879}
1880
1881/**
1882 * i40evf_get_stats - Get System Network Statistics
1883 * @netdev: network interface device structure
1884 *
1885 * Returns the address of the device statistics structure.
1886 * The statistics are actually updated from the timer callback.
1887 **/
1888static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1889{
1890 struct i40evf_adapter *adapter = netdev_priv(netdev);
1891
1892 /* only return the current stats */
1893 return &adapter->net_stats;
1894}
1895
1896/**
1897 * i40evf_reinit_locked - Software reinit
1898 * @adapter: board private structure
1899 *
1900 * Reinititalizes the ring structures in response to a software configuration
1901 * change. Roughly the same as close followed by open, but skips releasing
1902 * and reallocating the interrupts.
1903 **/
1904void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1905{
1906 struct net_device *netdev = adapter->netdev;
1907 int err;
1908
1909 WARN_ON(in_interrupt());
1910
Greg Rose5eae00c2013-12-21 06:12:45 +00001911 i40evf_down(adapter);
1912
1913 /* allocate transmit descriptors */
1914 err = i40evf_setup_all_tx_resources(adapter);
1915 if (err)
1916 goto err_reinit;
1917
1918 /* allocate receive descriptors */
1919 err = i40evf_setup_all_rx_resources(adapter);
1920 if (err)
1921 goto err_reinit;
1922
1923 i40evf_configure(adapter);
1924
1925 err = i40evf_up_complete(adapter);
1926 if (err)
1927 goto err_reinit;
1928
1929 i40evf_irq_enable(adapter, true);
1930 return;
1931
1932err_reinit:
Mitch Williams80e72892014-05-10 04:49:06 +00001933 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001934 i40evf_close(netdev);
1935}
1936
1937/**
1938 * i40evf_change_mtu - Change the Maximum Transfer Unit
1939 * @netdev: network interface device structure
1940 * @new_mtu: new value for maximum frame size
1941 *
1942 * Returns 0 on success, negative on failure
1943 **/
1944static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1945{
1946 struct i40evf_adapter *adapter = netdev_priv(netdev);
1947 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1948
1949 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1950 return -EINVAL;
1951
1952 /* must set new MTU before calling down or up */
1953 netdev->mtu = new_mtu;
1954 i40evf_reinit_locked(adapter);
1955 return 0;
1956}
1957
1958static const struct net_device_ops i40evf_netdev_ops = {
1959 .ndo_open = i40evf_open,
1960 .ndo_stop = i40evf_close,
1961 .ndo_start_xmit = i40evf_xmit_frame,
1962 .ndo_get_stats = i40evf_get_stats,
1963 .ndo_set_rx_mode = i40evf_set_rx_mode,
1964 .ndo_validate_addr = eth_validate_addr,
1965 .ndo_set_mac_address = i40evf_set_mac,
1966 .ndo_change_mtu = i40evf_change_mtu,
1967 .ndo_tx_timeout = i40evf_tx_timeout,
1968 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1969 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1970};
1971
1972/**
1973 * i40evf_check_reset_complete - check that VF reset is complete
1974 * @hw: pointer to hw struct
1975 *
1976 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1977 **/
1978static int i40evf_check_reset_complete(struct i40e_hw *hw)
1979{
1980 u32 rstat;
1981 int i;
1982
1983 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07001984 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
1985 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1986 if ((rstat == I40E_VFR_VFACTIVE) ||
1987 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00001988 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00001989 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00001990 }
1991 return -EBUSY;
1992}
1993
1994/**
1995 * i40evf_init_task - worker thread to perform delayed initialization
1996 * @work: pointer to work_struct containing our data
1997 *
1998 * This task completes the work that was begun in probe. Due to the nature
1999 * of VF-PF communications, we may need to wait tens of milliseconds to get
2000 * reponses back from the PF. Rather than busy-wait in probe and bog down the
2001 * whole system, we'll do it in a task so we can sleep.
2002 * This task only runs during driver init. Once we've established
2003 * communications with the PF driver and set up our netdev, the watchdog
2004 * takes over.
2005 **/
2006static void i40evf_init_task(struct work_struct *work)
2007{
2008 struct i40evf_adapter *adapter = container_of(work,
2009 struct i40evf_adapter,
2010 init_task.work);
2011 struct net_device *netdev = adapter->netdev;
2012 struct i40evf_mac_filter *f;
2013 struct i40e_hw *hw = &adapter->hw;
2014 struct pci_dev *pdev = adapter->pdev;
2015 int i, err, bufsz;
2016
2017 switch (adapter->state) {
2018 case __I40EVF_STARTUP:
2019 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002020 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2021 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002022 err = i40e_set_mac_type(hw);
2023 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002024 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2025 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002026 goto err;
2027 }
2028 err = i40evf_check_reset_complete(hw);
2029 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002030 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002031 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002032 goto err;
2033 }
2034 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2035 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2036 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2037 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2038
2039 err = i40evf_init_adminq(hw);
2040 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002041 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2042 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002043 goto err;
2044 }
2045 err = i40evf_send_api_ver(adapter);
2046 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002047 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002048 i40evf_shutdown_adminq(hw);
2049 goto err;
2050 }
2051 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2052 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002053 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002054 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002055 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002056 i40evf_shutdown_adminq(hw);
2057 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002058 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002059 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002060
2061 /* aq msg sent, awaiting reply */
2062 err = i40evf_verify_api_ver(adapter);
2063 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002064 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002065 err = i40evf_send_api_ver(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002066 goto err;
2067 }
2068 err = i40evf_send_vf_config_msg(adapter);
2069 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002070 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002071 err);
2072 goto err;
2073 }
2074 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2075 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002076 case __I40EVF_INIT_GET_RESOURCES:
2077 /* aq msg sent, awaiting reply */
2078 if (!adapter->vf_res) {
2079 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2080 (I40E_MAX_VF_VSI *
2081 sizeof(struct i40e_virtchnl_vsi_resource));
2082 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002083 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002084 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002085 }
2086 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002087 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002088 err = i40evf_send_vf_config_msg(adapter);
2089 goto err;
2090 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002091 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002092 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2093 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002094 goto err_alloc;
2095 }
2096 adapter->state = __I40EVF_INIT_SW;
2097 break;
2098 default:
2099 goto err_alloc;
2100 }
2101 /* got VF config message back from PF, now we can parse it */
2102 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2103 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2104 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2105 }
2106 if (!adapter->vsi_res) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002107 dev_err(&pdev->dev, "No LAN VSI found\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002108 goto err_alloc;
2109 }
2110
2111 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2112
Greg Rose5eae00c2013-12-21 06:12:45 +00002113 netdev->netdev_ops = &i40evf_netdev_ops;
2114 i40evf_set_ethtool_ops(netdev);
2115 netdev->watchdog_timeo = 5 * HZ;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002116 netdev->features |= NETIF_F_HIGHDMA |
2117 NETIF_F_SG |
Greg Rose5eae00c2013-12-21 06:12:45 +00002118 NETIF_F_IP_CSUM |
2119 NETIF_F_SCTP_CSUM |
2120 NETIF_F_IPV6_CSUM |
2121 NETIF_F_TSO |
2122 NETIF_F_TSO6 |
Greg Rose3415e8c2014-01-30 12:40:27 +00002123 NETIF_F_RXCSUM |
Greg Rose5eae00c2013-12-21 06:12:45 +00002124 NETIF_F_GRO;
2125
2126 if (adapter->vf_res->vf_offload_flags
2127 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2128 netdev->vlan_features = netdev->features;
2129 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2130 NETIF_F_HW_VLAN_CTAG_RX |
2131 NETIF_F_HW_VLAN_CTAG_FILTER;
2132 }
2133
Greg Rose3415e8c2014-01-30 12:40:27 +00002134 /* copy netdev features into list of user selectable features */
2135 netdev->hw_features |= netdev->features;
2136 netdev->hw_features &= ~NETIF_F_RXCSUM;
2137
Greg Rose5eae00c2013-12-21 06:12:45 +00002138 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002139 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002140 adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002141 random_ether_addr(adapter->hw.mac.addr);
2142 }
Greg Rose9a173902014-05-22 06:32:02 +00002143 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2144 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002145
Greg Rose5eae00c2013-12-21 06:12:45 +00002146 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +00002147 if (!f)
Greg Rose5eae00c2013-12-21 06:12:45 +00002148 goto err_sw_init;
2149
Greg Rose9a173902014-05-22 06:32:02 +00002150 ether_addr_copy(f->macaddr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002151 f->add = true;
2152 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2153
2154 list_add(&f->list, &adapter->mac_filter_list);
2155
2156 init_timer(&adapter->watchdog_timer);
2157 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2158 adapter->watchdog_timer.data = (unsigned long)adapter;
2159 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2160
Mitch Williamscc052922014-10-25 03:24:34 +00002161 adapter->num_active_queues = min_t(int,
2162 adapter->vsi_res->num_queue_pairs,
2163 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002164 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2165 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002166 err = i40evf_init_interrupt_scheme(adapter);
2167 if (err)
2168 goto err_sw_init;
2169 i40evf_map_rings_to_vectors(adapter);
2170 i40evf_configure_rss(adapter);
2171 err = i40evf_request_misc_irq(adapter);
2172 if (err)
2173 goto err_sw_init;
2174
2175 netif_carrier_off(netdev);
2176
Greg Rose5eae00c2013-12-21 06:12:45 +00002177 adapter->vsi.id = adapter->vsi_res->vsi_id;
2178 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2179 adapter->vsi.back = adapter;
2180 adapter->vsi.base_vector = 1;
2181 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williamsca99eb92014-04-04 04:43:07 +00002182 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2183 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2184 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2185 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Greg Rose5eae00c2013-12-21 06:12:45 +00002186 adapter->vsi.netdev = adapter->netdev;
2187
Mitch Williamsef8693e2014-02-13 03:48:53 -08002188 if (!adapter->netdev_registered) {
2189 err = register_netdev(netdev);
2190 if (err)
2191 goto err_register;
2192 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002193
2194 adapter->netdev_registered = true;
2195
2196 netif_tx_stop_all_queues(netdev);
2197
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002198 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002199 if (netdev->features & NETIF_F_GRO)
2200 dev_info(&pdev->dev, "GRO is enabled\n");
2201
2202 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2203 adapter->state = __I40EVF_DOWN;
2204 set_bit(__I40E_DOWN, &adapter->vsi.state);
2205 i40evf_misc_irq_enable(adapter);
2206 return;
2207restart:
2208 schedule_delayed_work(&adapter->init_task,
2209 msecs_to_jiffies(50));
2210 return;
2211
2212err_register:
2213 i40evf_free_misc_irq(adapter);
2214err_sw_init:
2215 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002216err_alloc:
2217 kfree(adapter->vf_res);
2218 adapter->vf_res = NULL;
2219err:
2220 /* Things went into the weeds, so try again later */
2221 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002222 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002223 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002224 return; /* do not reschedule */
2225 }
2226 schedule_delayed_work(&adapter->init_task, HZ * 3);
Greg Rose5eae00c2013-12-21 06:12:45 +00002227}
2228
2229/**
2230 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2231 * @pdev: pci device structure
2232 **/
2233static void i40evf_shutdown(struct pci_dev *pdev)
2234{
2235 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002236 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002237
2238 netif_device_detach(netdev);
2239
2240 if (netif_running(netdev))
2241 i40evf_close(netdev);
2242
Mitch Williams00293fd2015-01-09 11:18:18 +00002243 /* Prevent the watchdog from running. */
2244 adapter->state = __I40EVF_REMOVE;
2245 adapter->aq_required = 0;
2246 adapter->aq_pending = 0;
2247
Greg Rose5eae00c2013-12-21 06:12:45 +00002248#ifdef CONFIG_PM
2249 pci_save_state(pdev);
2250
2251#endif
2252 pci_disable_device(pdev);
2253}
2254
2255/**
2256 * i40evf_probe - Device Initialization Routine
2257 * @pdev: PCI device information struct
2258 * @ent: entry in i40evf_pci_tbl
2259 *
2260 * Returns 0 on success, negative on failure
2261 *
2262 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2263 * The OS initialization, configuring of the adapter private structure,
2264 * and a hardware reset occur.
2265 **/
2266static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2267{
2268 struct net_device *netdev;
2269 struct i40evf_adapter *adapter = NULL;
2270 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002271 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002272
2273 err = pci_enable_device(pdev);
2274 if (err)
2275 return err;
2276
Mitch Williams64942942014-02-11 08:26:33 +00002277 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002278 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002279 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2280 if (err) {
2281 dev_err(&pdev->dev,
2282 "DMA configuration failed: 0x%x\n", err);
2283 goto err_dma;
2284 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002285 }
2286
2287 err = pci_request_regions(pdev, i40evf_driver_name);
2288 if (err) {
2289 dev_err(&pdev->dev,
2290 "pci_request_regions failed 0x%x\n", err);
2291 goto err_pci_reg;
2292 }
2293
2294 pci_enable_pcie_error_reporting(pdev);
2295
2296 pci_set_master(pdev);
2297
2298 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2299 MAX_TX_QUEUES);
2300 if (!netdev) {
2301 err = -ENOMEM;
2302 goto err_alloc_etherdev;
2303 }
2304
2305 SET_NETDEV_DEV(netdev, &pdev->dev);
2306
2307 pci_set_drvdata(pdev, netdev);
2308 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002309
2310 adapter->netdev = netdev;
2311 adapter->pdev = pdev;
2312
2313 hw = &adapter->hw;
2314 hw->back = adapter;
2315
2316 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2317 adapter->state = __I40EVF_STARTUP;
2318
2319 /* Call save state here because it relies on the adapter struct. */
2320 pci_save_state(pdev);
2321
2322 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2323 pci_resource_len(pdev, 0));
2324 if (!hw->hw_addr) {
2325 err = -EIO;
2326 goto err_ioremap;
2327 }
2328 hw->vendor_id = pdev->vendor;
2329 hw->device_id = pdev->device;
2330 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2331 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2332 hw->subsystem_device_id = pdev->subsystem_device;
2333 hw->bus.device = PCI_SLOT(pdev->devfn);
2334 hw->bus.func = PCI_FUNC(pdev->devfn);
2335
Serey Kong8bb1a542014-08-01 13:27:15 -07002336 INIT_LIST_HEAD(&adapter->mac_filter_list);
2337 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2338
Greg Rose5eae00c2013-12-21 06:12:45 +00002339 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2340 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2341 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2342 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2343 schedule_delayed_work(&adapter->init_task, 10);
2344
2345 return 0;
2346
2347err_ioremap:
2348 free_netdev(netdev);
2349err_alloc_etherdev:
2350 pci_release_regions(pdev);
2351err_pci_reg:
2352err_dma:
2353 pci_disable_device(pdev);
2354 return err;
2355}
2356
2357#ifdef CONFIG_PM
2358/**
2359 * i40evf_suspend - Power management suspend routine
2360 * @pdev: PCI device information struct
2361 * @state: unused
2362 *
2363 * Called when the system (VM) is entering sleep/suspend.
2364 **/
2365static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2366{
2367 struct net_device *netdev = pci_get_drvdata(pdev);
2368 struct i40evf_adapter *adapter = netdev_priv(netdev);
2369 int retval = 0;
2370
2371 netif_device_detach(netdev);
2372
2373 if (netif_running(netdev)) {
2374 rtnl_lock();
2375 i40evf_down(adapter);
2376 rtnl_unlock();
2377 }
2378 i40evf_free_misc_irq(adapter);
2379 i40evf_reset_interrupt_capability(adapter);
2380
2381 retval = pci_save_state(pdev);
2382 if (retval)
2383 return retval;
2384
2385 pci_disable_device(pdev);
2386
2387 return 0;
2388}
2389
2390/**
2391 * i40evf_resume - Power managment resume routine
2392 * @pdev: PCI device information struct
2393 *
2394 * Called when the system (VM) is resumed from sleep/suspend.
2395 **/
2396static int i40evf_resume(struct pci_dev *pdev)
2397{
2398 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2399 struct net_device *netdev = adapter->netdev;
2400 u32 err;
2401
2402 pci_set_power_state(pdev, PCI_D0);
2403 pci_restore_state(pdev);
2404 /* pci_restore_state clears dev->state_saved so call
2405 * pci_save_state to restore it.
2406 */
2407 pci_save_state(pdev);
2408
2409 err = pci_enable_device_mem(pdev);
2410 if (err) {
2411 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2412 return err;
2413 }
2414 pci_set_master(pdev);
2415
2416 rtnl_lock();
2417 err = i40evf_set_interrupt_capability(adapter);
2418 if (err) {
2419 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2420 return err;
2421 }
2422 err = i40evf_request_misc_irq(adapter);
2423 rtnl_unlock();
2424 if (err) {
2425 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2426 return err;
2427 }
2428
2429 schedule_work(&adapter->reset_task);
2430
2431 netif_device_attach(netdev);
2432
2433 return err;
2434}
2435
2436#endif /* CONFIG_PM */
2437/**
2438 * i40evf_remove - Device Removal Routine
2439 * @pdev: PCI device information struct
2440 *
2441 * i40evf_remove is called by the PCI subsystem to alert the driver
2442 * that it should release a PCI device. The could be caused by a
2443 * Hot-Plug event, or because the driver is going to be removed from
2444 * memory.
2445 **/
2446static void i40evf_remove(struct pci_dev *pdev)
2447{
2448 struct net_device *netdev = pci_get_drvdata(pdev);
2449 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002450 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002451 struct i40e_hw *hw = &adapter->hw;
2452
2453 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002454 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002455
2456 if (adapter->netdev_registered) {
2457 unregister_netdev(netdev);
2458 adapter->netdev_registered = false;
2459 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002460
Mitch Williamsf4a71882015-01-09 11:18:16 +00002461 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002462 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002463 adapter->aq_required = 0;
2464 adapter->aq_pending = 0;
2465 i40evf_request_reset(adapter);
2466 msleep(20);
2467 /* If the FW isn't responding, kick it once, but only once. */
2468 if (!i40evf_asq_done(hw)) {
2469 i40evf_request_reset(adapter);
2470 msleep(20);
2471 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002472
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002473 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002474 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002475 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002476 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002477 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002478 }
2479
Mitch Williamse5d17c32014-06-04 04:22:38 +00002480 if (adapter->watchdog_timer.function)
2481 del_timer_sync(&adapter->watchdog_timer);
2482
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002483 flush_scheduled_work();
2484
Greg Rose5eae00c2013-12-21 06:12:45 +00002485 if (hw->aq.asq.count)
2486 i40evf_shutdown_adminq(hw);
2487
2488 iounmap(hw->hw_addr);
2489 pci_release_regions(pdev);
2490
2491 i40evf_free_queues(adapter);
2492 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002493 /* If we got removed before an up/down sequence, we've got a filter
2494 * hanging out there that we need to get rid of.
2495 */
2496 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2497 list_del(&f->list);
2498 kfree(f);
2499 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002500 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2501 list_del(&f->list);
2502 kfree(f);
2503 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002504
2505 free_netdev(netdev);
2506
2507 pci_disable_pcie_error_reporting(pdev);
2508
2509 pci_disable_device(pdev);
2510}
2511
2512static struct pci_driver i40evf_driver = {
2513 .name = i40evf_driver_name,
2514 .id_table = i40evf_pci_tbl,
2515 .probe = i40evf_probe,
2516 .remove = i40evf_remove,
2517#ifdef CONFIG_PM
2518 .suspend = i40evf_suspend,
2519 .resume = i40evf_resume,
2520#endif
2521 .shutdown = i40evf_shutdown,
2522};
2523
2524/**
2525 * i40e_init_module - Driver Registration Routine
2526 *
2527 * i40e_init_module is the first routine called when the driver is
2528 * loaded. All it does is register with the PCI subsystem.
2529 **/
2530static int __init i40evf_init_module(void)
2531{
2532 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002533
Greg Rose5eae00c2013-12-21 06:12:45 +00002534 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002535 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002536
2537 pr_info("%s\n", i40evf_copyright);
2538
2539 ret = pci_register_driver(&i40evf_driver);
2540 return ret;
2541}
2542
2543module_init(i40evf_init_module);
2544
2545/**
2546 * i40e_exit_module - Driver Exit Cleanup Routine
2547 *
2548 * i40e_exit_module is called just before the driver is removed
2549 * from memory.
2550 **/
2551static void __exit i40evf_exit_module(void)
2552{
2553 pci_unregister_driver(&i40evf_driver);
2554}
2555
2556module_exit(i40evf_exit_module);
2557
2558/* i40evf_main.c */