blob: e2311affdfe20b2aa6742c5fbf68e77d6a9c2c47 [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
Catherine Sullivana36fdd8e2014-11-11 20:07:41 +000039#define DRV_VERSION "1.0.6"
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
Greg Rose5eae00c2013-12-21 06:12:45 +0000923 i40evf_alloc_rx_buffers(ring, ring->count);
924 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);
962
Mitch Williamsef8693e2014-02-13 03:48:53 -0800963 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +0000964 list_for_each_entry(f, &adapter->mac_filter_list, list) {
965 f->remove = true;
966 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800967 /* remove all VLAN filters */
968 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
969 f->remove = true;
970 }
Mitch Williamsef8693e2014-02-13 03:48:53 -0800971 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
972 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000973 /* cancel any current operation */
974 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
975 adapter->aq_pending = 0;
976 /* Schedule operations to close down the HW. Don't wait
977 * here for this to complete. The watchdog is still running
978 * and it will take care of this.
979 */
980 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800981 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800982 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800983 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000984 netif_tx_disable(netdev);
985
986 netif_tx_stop_all_queues(netdev);
987
Greg Rose5eae00c2013-12-21 06:12:45 +0000988 i40evf_napi_disable_all(adapter);
989
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000990 msleep(20);
991
Greg Rose5eae00c2013-12-21 06:12:45 +0000992 netif_carrier_off(netdev);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000993 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000994}
995
996/**
997 * i40evf_acquire_msix_vectors - Setup the MSIX capability
998 * @adapter: board private structure
999 * @vectors: number of vectors to request
1000 *
1001 * Work with the OS to set up the MSIX vectors needed.
1002 *
1003 * Returns 0 on success, negative on failure
1004 **/
1005static int
1006i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1007{
1008 int err, vector_threshold;
1009
1010 /* We'll want at least 3 (vector_threshold):
1011 * 0) Other (Admin Queue and link, mostly)
1012 * 1) TxQ[0] Cleanup
1013 * 2) RxQ[0] Cleanup
1014 */
1015 vector_threshold = MIN_MSIX_COUNT;
1016
1017 /* The more we get, the more we will assign to Tx/Rx Cleanup
1018 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1019 * Right now, we simply care about how many we'll get; we'll
1020 * set them up later while requesting irq's.
1021 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001022 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1023 vector_threshold, vectors);
1024 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001025 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001026 kfree(adapter->msix_entries);
1027 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001028 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001029 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001030
1031 /* Adjust for only the vectors we'll use, which is minimum
1032 * of max_msix_q_vectors + NONQ_VECS, or the number of
1033 * vectors we were allocated.
1034 */
1035 adapter->num_msix_vectors = err;
1036 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001037}
1038
1039/**
1040 * i40evf_free_queues - Free memory for all rings
1041 * @adapter: board private structure to initialize
1042 *
1043 * Free all of the memory associated with queue pairs.
1044 **/
1045static void i40evf_free_queues(struct i40evf_adapter *adapter)
1046{
1047 int i;
1048
1049 if (!adapter->vsi_res)
1050 return;
Mitch Williamscc052922014-10-25 03:24:34 +00001051 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001052 if (adapter->tx_rings[i])
1053 kfree_rcu(adapter->tx_rings[i], rcu);
1054 adapter->tx_rings[i] = NULL;
1055 adapter->rx_rings[i] = NULL;
1056 }
1057}
1058
1059/**
1060 * i40evf_alloc_queues - Allocate memory for all rings
1061 * @adapter: board private structure to initialize
1062 *
1063 * We allocate one ring per queue at run-time since we don't know the
1064 * number of queues at compile-time. The polling_netdev array is
1065 * intended for Multiqueue, but should work fine with a single queue.
1066 **/
1067static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1068{
1069 int i;
1070
Mitch Williamscc052922014-10-25 03:24:34 +00001071 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001072 struct i40e_ring *tx_ring;
1073 struct i40e_ring *rx_ring;
1074
Mitch Williams75a64432014-11-11 20:02:42 +00001075 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001076 if (!tx_ring)
1077 goto err_out;
1078
1079 tx_ring->queue_index = i;
1080 tx_ring->netdev = adapter->netdev;
1081 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001082 tx_ring->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001083 adapter->tx_rings[i] = tx_ring;
1084
1085 rx_ring = &tx_ring[1];
1086 rx_ring->queue_index = i;
1087 rx_ring->netdev = adapter->netdev;
1088 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001089 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001090 adapter->rx_rings[i] = rx_ring;
1091 }
1092
1093 return 0;
1094
1095err_out:
1096 i40evf_free_queues(adapter);
1097 return -ENOMEM;
1098}
1099
1100/**
1101 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1102 * @adapter: board private structure to initialize
1103 *
1104 * Attempt to configure the interrupts using the best available
1105 * capabilities of the hardware and the kernel.
1106 **/
1107static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1108{
1109 int vector, v_budget;
1110 int pairs = 0;
1111 int err = 0;
1112
1113 if (!adapter->vsi_res) {
1114 err = -EIO;
1115 goto out;
1116 }
Mitch Williamscc052922014-10-25 03:24:34 +00001117 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001118
1119 /* It's easy to be greedy for MSI-X vectors, but it really
1120 * doesn't do us much good if we have a lot more vectors
1121 * than CPU's. So let's be conservative and only ask for
1122 * (roughly) twice the number of vectors as there are CPU's.
1123 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001124 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1125 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001126
Greg Rose5eae00c2013-12-21 06:12:45 +00001127 adapter->msix_entries = kcalloc(v_budget,
1128 sizeof(struct msix_entry), GFP_KERNEL);
1129 if (!adapter->msix_entries) {
1130 err = -ENOMEM;
1131 goto out;
1132 }
1133
1134 for (vector = 0; vector < v_budget; vector++)
1135 adapter->msix_entries[vector].entry = vector;
1136
1137 i40evf_acquire_msix_vectors(adapter, v_budget);
1138
1139out:
1140 adapter->netdev->real_num_tx_queues = pairs;
1141 return err;
1142}
1143
1144/**
1145 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1146 * @adapter: board private structure to initialize
1147 *
1148 * We allocate one q_vector per queue interrupt. If allocation fails we
1149 * return -ENOMEM.
1150 **/
1151static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1152{
1153 int q_idx, num_q_vectors;
1154 struct i40e_q_vector *q_vector;
1155
1156 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1157
1158 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams75a64432014-11-11 20:02:42 +00001159 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001160 if (!q_vector)
1161 goto err_out;
1162 q_vector->adapter = adapter;
1163 q_vector->vsi = &adapter->vsi;
1164 q_vector->v_idx = q_idx;
1165 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001166 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001167 adapter->q_vector[q_idx] = q_vector;
1168 }
1169
1170 return 0;
1171
1172err_out:
1173 while (q_idx) {
1174 q_idx--;
1175 q_vector = adapter->q_vector[q_idx];
1176 netif_napi_del(&q_vector->napi);
1177 kfree(q_vector);
1178 adapter->q_vector[q_idx] = NULL;
1179 }
1180 return -ENOMEM;
1181}
1182
1183/**
1184 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1185 * @adapter: board private structure to initialize
1186 *
1187 * This function frees the memory allocated to the q_vectors. In addition if
1188 * NAPI is enabled it will delete any references to the NAPI struct prior
1189 * to freeing the q_vector.
1190 **/
1191static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1192{
1193 int q_idx, num_q_vectors;
1194 int napi_vectors;
1195
1196 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001197 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001198
1199 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1200 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1201
1202 adapter->q_vector[q_idx] = NULL;
1203 if (q_idx < napi_vectors)
1204 netif_napi_del(&q_vector->napi);
1205 kfree(q_vector);
1206 }
1207}
1208
1209/**
1210 * i40evf_reset_interrupt_capability - Reset MSIX setup
1211 * @adapter: board private structure
1212 *
1213 **/
1214void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1215{
1216 pci_disable_msix(adapter->pdev);
1217 kfree(adapter->msix_entries);
1218 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001219}
1220
1221/**
1222 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1223 * @adapter: board private structure to initialize
1224 *
1225 **/
1226int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1227{
1228 int err;
1229
1230 err = i40evf_set_interrupt_capability(adapter);
1231 if (err) {
1232 dev_err(&adapter->pdev->dev,
1233 "Unable to setup interrupt capabilities\n");
1234 goto err_set_interrupt;
1235 }
1236
1237 err = i40evf_alloc_q_vectors(adapter);
1238 if (err) {
1239 dev_err(&adapter->pdev->dev,
1240 "Unable to allocate memory for queue vectors\n");
1241 goto err_alloc_q_vectors;
1242 }
1243
1244 err = i40evf_alloc_queues(adapter);
1245 if (err) {
1246 dev_err(&adapter->pdev->dev,
1247 "Unable to allocate memory for queues\n");
1248 goto err_alloc_queues;
1249 }
1250
1251 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001252 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1253 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001254
1255 return 0;
1256err_alloc_queues:
1257 i40evf_free_q_vectors(adapter);
1258err_alloc_q_vectors:
1259 i40evf_reset_interrupt_capability(adapter);
1260err_set_interrupt:
1261 return err;
1262}
1263
1264/**
1265 * i40evf_watchdog_timer - Periodic call-back timer
1266 * @data: pointer to adapter disguised as unsigned long
1267 **/
1268static void i40evf_watchdog_timer(unsigned long data)
1269{
1270 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001271
Greg Rose5eae00c2013-12-21 06:12:45 +00001272 schedule_work(&adapter->watchdog_task);
1273 /* timer will be rescheduled in watchdog task */
1274}
1275
1276/**
1277 * i40evf_watchdog_task - Periodic call-back task
1278 * @work: pointer to work_struct
1279 **/
1280static void i40evf_watchdog_task(struct work_struct *work)
1281{
1282 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001283 struct i40evf_adapter,
1284 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001285 struct i40e_hw *hw = &adapter->hw;
Ashish Shahfd358862014-08-01 13:27:11 -07001286 uint32_t rstat_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001287
Greg Rose5eae00c2013-12-21 06:12:45 +00001288 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001289 goto restart_watchdog;
1290
1291 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Ashish Shahfd358862014-08-01 13:27:11 -07001292 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1293 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1294 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1295 (rstat_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001296 /* A chance for redemption! */
1297 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1298 adapter->state = __I40EVF_STARTUP;
1299 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1300 schedule_delayed_work(&adapter->init_task, 10);
1301 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1302 &adapter->crit_section);
1303 /* Don't reschedule the watchdog, since we've restarted
1304 * the init task. When init_task contacts the PF and
1305 * gets everything set up again, it'll restart the
1306 * watchdog for us. Down, boy. Sit. Stay. Woof.
1307 */
1308 return;
1309 }
1310 adapter->aq_pending = 0;
1311 adapter->aq_required = 0;
1312 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1313 goto watchdog_done;
1314 }
1315
1316 if ((adapter->state < __I40EVF_DOWN) ||
1317 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001318 goto watchdog_done;
1319
Mitch Williamsef8693e2014-02-13 03:48:53 -08001320 /* check for reset */
Ashish Shahfd358862014-08-01 13:27:11 -07001321 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
Mitch Williams75a64432014-11-11 20:02:42 +00001322 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001323 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
Ashish Shahfd358862014-08-01 13:27:11 -07001324 (rstat_val != I40E_VFR_VFACTIVE) &&
1325 (rstat_val != I40E_VFR_COMPLETED)) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001326 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001327 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001328 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001329 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001330 adapter->aq_pending = 0;
1331 adapter->aq_required = 0;
1332 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001333 goto watchdog_done;
1334 }
1335
1336 /* Process admin queue tasks. After init, everything gets done
1337 * here so we don't race on the admin queue.
1338 */
1339 if (adapter->aq_pending)
1340 goto watchdog_done;
1341
1342 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1343 i40evf_map_queues(adapter);
1344 goto watchdog_done;
1345 }
1346
1347 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1348 i40evf_add_ether_addrs(adapter);
1349 goto watchdog_done;
1350 }
1351
1352 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1353 i40evf_add_vlans(adapter);
1354 goto watchdog_done;
1355 }
1356
1357 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1358 i40evf_del_ether_addrs(adapter);
1359 goto watchdog_done;
1360 }
1361
1362 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1363 i40evf_del_vlans(adapter);
1364 goto watchdog_done;
1365 }
1366
1367 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1368 i40evf_disable_queues(adapter);
1369 goto watchdog_done;
1370 }
1371
1372 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1373 i40evf_configure_queues(adapter);
1374 goto watchdog_done;
1375 }
1376
1377 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1378 i40evf_enable_queues(adapter);
1379 goto watchdog_done;
1380 }
1381
1382 if (adapter->state == __I40EVF_RUNNING)
1383 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001384watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001385 if (adapter->state == __I40EVF_RUNNING) {
1386 i40evf_irq_enable_queues(adapter, ~0);
1387 i40evf_fire_sw_int(adapter, 0xFF);
1388 } else {
1389 i40evf_fire_sw_int(adapter, 0x1);
1390 }
1391
Mitch Williamsef8693e2014-02-13 03:48:53 -08001392 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1393restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001394 if (adapter->state == __I40EVF_REMOVE)
1395 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001396 if (adapter->aq_required)
1397 mod_timer(&adapter->watchdog_timer,
1398 jiffies + msecs_to_jiffies(20));
1399 else
1400 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001401 schedule_work(&adapter->adminq_task);
1402}
1403
Mitch A Williams5b7af022014-03-28 06:49:02 +00001404/**
Catherine Sullivan3dd55502014-05-20 08:01:36 +00001405 * next_queue - increment to next available tx queue
Mitch A Williams5b7af022014-03-28 06:49:02 +00001406 * @adapter: board private structure
1407 * @j: queue counter
1408 *
1409 * Helper function for RSS programming to increment through available
1410 * queus. Returns the next queue value.
1411 **/
Mitch Williams96d47702014-02-11 08:27:50 +00001412static int next_queue(struct i40evf_adapter *adapter, int j)
1413{
1414 j += 1;
1415
Mitch Williamscc052922014-10-25 03:24:34 +00001416 return j >= adapter->num_active_queues ? 0 : j;
Mitch Williams96d47702014-02-11 08:27:50 +00001417}
1418
Greg Rose5eae00c2013-12-21 06:12:45 +00001419/**
1420 * i40evf_configure_rss - Prepare for RSS if used
1421 * @adapter: board private structure
1422 **/
1423static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1424{
Eric Dumazet22f258a2014-11-16 06:23:13 -08001425 u32 rss_key[I40E_VFQF_HKEY_MAX_INDEX + 1];
Greg Rose5eae00c2013-12-21 06:12:45 +00001426 struct i40e_hw *hw = &adapter->hw;
1427 u32 lut = 0;
1428 int i, j;
1429 u64 hena;
1430
Mitch Williamscc052922014-10-25 03:24:34 +00001431 /* No RSS for single queue. */
1432 if (adapter->num_active_queues == 1) {
1433 wr32(hw, I40E_VFQF_HENA(0), 0);
1434 wr32(hw, I40E_VFQF_HENA(1), 0);
1435 return;
1436 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001437
Mitch Williamscc052922014-10-25 03:24:34 +00001438 /* Hash type is configured by the PF - we just supply the key */
Eric Dumazet22f258a2014-11-16 06:23:13 -08001439 netdev_rss_key_fill(rss_key, sizeof(rss_key));
Greg Rose5eae00c2013-12-21 06:12:45 +00001440 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
Eric Dumazet22f258a2014-11-16 06:23:13 -08001441 wr32(hw, I40E_VFQF_HKEY(i), rss_key[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001442
1443 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1444 hena = I40E_DEFAULT_RSS_HENA;
1445 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1446 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1447
1448 /* Populate the LUT with max no. of queues in round robin fashion */
Mitch Williamscc052922014-10-25 03:24:34 +00001449 j = adapter->num_active_queues;
Mitch Williams96d47702014-02-11 08:27:50 +00001450 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
Mitch A Williams5b7af022014-03-28 06:49:02 +00001451 j = next_queue(adapter, j);
1452 lut = j;
1453 j = next_queue(adapter, j);
1454 lut |= j << 8;
1455 j = next_queue(adapter, j);
1456 lut |= j << 16;
1457 j = next_queue(adapter, j);
1458 lut |= j << 24;
Mitch Williams96d47702014-02-11 08:27:50 +00001459 wr32(hw, I40E_VFQF_HLUT(i), lut);
Greg Rose5eae00c2013-12-21 06:12:45 +00001460 }
1461 i40e_flush(hw);
1462}
1463
Mitch Williamsef8693e2014-02-13 03:48:53 -08001464#define I40EVF_RESET_WAIT_MS 100
1465#define I40EVF_RESET_WAIT_COUNT 200
Greg Rose5eae00c2013-12-21 06:12:45 +00001466/**
1467 * i40evf_reset_task - Call-back task to handle hardware reset
1468 * @work: pointer to work_struct
1469 *
1470 * During reset we need to shut down and reinitialize the admin queue
1471 * before we can use it to communicate with the PF again. We also clear
1472 * and reinit the rings because that context is lost as well.
1473 **/
1474static void i40evf_reset_task(struct work_struct *work)
1475{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001476 struct i40evf_adapter *adapter = container_of(work,
1477 struct i40evf_adapter,
1478 reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001479 struct i40e_hw *hw = &adapter->hw;
1480 int i = 0, err;
1481 uint32_t rstat_val;
1482
1483 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1484 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001485 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001486
1487 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1488 dev_info(&adapter->pdev->dev, "Requesting reset from PF\n");
1489 i40evf_request_reset(adapter);
1490 }
1491
Mitch Williamsef8693e2014-02-13 03:48:53 -08001492 /* poll until we see the reset actually happen */
1493 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001494 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1495 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Ashish Shahfd358862014-08-01 13:27:11 -07001496 if ((rstat_val != I40E_VFR_VFACTIVE) &&
1497 (rstat_val != I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00001498 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001499 msleep(I40EVF_RESET_WAIT_MS);
Greg Rose5eae00c2013-12-21 06:12:45 +00001500 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001501 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001502 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1503 goto continue_reset; /* act like the reset happened */
1504 }
1505
1506 /* wait until the reset is complete and the PF is responding to us */
1507 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1508 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1509 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Ashish Shahfd358862014-08-01 13:27:11 -07001510 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1511 (rstat_val == I40E_VFR_COMPLETED))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001512 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001513 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001514 }
1515 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams6ba36a22014-08-01 13:27:14 -07001516 struct i40evf_mac_filter *f, *ftmp;
1517 struct i40evf_vlan_filter *fv, *fvtmp;
1518
Greg Rose5eae00c2013-12-21 06:12:45 +00001519 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001520 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsef8693e2014-02-13 03:48:53 -08001521 rstat_val);
1522 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1523
Mitch Williams169f4072014-04-01 07:11:50 +00001524 if (netif_running(adapter->netdev)) {
1525 set_bit(__I40E_DOWN, &adapter->vsi.state);
1526 i40evf_down(adapter);
1527 i40evf_free_traffic_irqs(adapter);
1528 i40evf_free_all_tx_resources(adapter);
1529 i40evf_free_all_rx_resources(adapter);
1530 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001531
1532 /* Delete all of the filters, both MAC and VLAN. */
1533 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1534 list) {
1535 list_del(&f->list);
1536 kfree(f);
1537 }
1538 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1539 list) {
1540 list_del(&fv->list);
1541 kfree(fv);
1542 }
1543
Mitch Williamsef8693e2014-02-13 03:48:53 -08001544 i40evf_free_misc_irq(adapter);
1545 i40evf_reset_interrupt_capability(adapter);
1546 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001547 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001548 kfree(adapter->vf_res);
1549 i40evf_shutdown_adminq(hw);
1550 adapter->netdev->flags &= ~IFF_UP;
1551 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1552 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001553 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001554
1555continue_reset:
1556 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1557
Greg Rose5eae00c2013-12-21 06:12:45 +00001558 i40evf_down(adapter);
1559 adapter->state = __I40EVF_RESETTING;
1560
1561 /* kill and reinit the admin queue */
1562 if (i40evf_shutdown_adminq(hw))
1563 dev_warn(&adapter->pdev->dev,
Mitch Williams75a64432014-11-11 20:02:42 +00001564 "%s: Failed to destroy the Admin Queue resources\n",
1565 __func__);
Greg Rose5eae00c2013-12-21 06:12:45 +00001566 err = i40evf_init_adminq(hw);
1567 if (err)
1568 dev_info(&adapter->pdev->dev, "%s: init_adminq failed: %d\n",
Mitch Williams75a64432014-11-11 20:02:42 +00001569 __func__, err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001570
1571 adapter->aq_pending = 0;
1572 adapter->aq_required = 0;
1573 i40evf_map_queues(adapter);
1574 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1575
1576 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1577
1578 if (netif_running(adapter->netdev)) {
1579 /* allocate transmit descriptors */
1580 err = i40evf_setup_all_tx_resources(adapter);
1581 if (err)
1582 goto reset_err;
1583
1584 /* allocate receive descriptors */
1585 err = i40evf_setup_all_rx_resources(adapter);
1586 if (err)
1587 goto reset_err;
1588
1589 i40evf_configure(adapter);
1590
1591 err = i40evf_up_complete(adapter);
1592 if (err)
1593 goto reset_err;
1594
1595 i40evf_irq_enable(adapter, true);
1596 }
1597 return;
1598reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001599 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001600 i40evf_close(adapter->netdev);
1601}
1602
1603/**
1604 * i40evf_adminq_task - worker thread to clean the admin queue
1605 * @work: pointer to work_struct containing our data
1606 **/
1607static void i40evf_adminq_task(struct work_struct *work)
1608{
1609 struct i40evf_adapter *adapter =
1610 container_of(work, struct i40evf_adapter, adminq_task);
1611 struct i40e_hw *hw = &adapter->hw;
1612 struct i40e_arq_event_info event;
1613 struct i40e_virtchnl_msg *v_msg;
1614 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001615 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001616 u16 pending;
1617
Mitch Williamsef8693e2014-02-13 03:48:53 -08001618 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001619 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001620
Mitch Williams1001dc32014-11-11 20:02:19 +00001621 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1622 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001623 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001624 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001625
Greg Rose5eae00c2013-12-21 06:12:45 +00001626 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1627 do {
1628 ret = i40evf_clean_arq_element(hw, &event, &pending);
1629 if (ret)
1630 break; /* No event to process or error cleaning ARQ */
1631
1632 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1633 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001634 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001635 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001636 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001637 } while (pending);
1638
Mitch Williams912257e2014-05-22 06:32:07 +00001639 /* check for error indications */
1640 val = rd32(hw, hw->aq.arq.len);
1641 oldval = val;
1642 if (val & I40E_VF_ARQLEN_ARQVFE_MASK) {
1643 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1644 val &= ~I40E_VF_ARQLEN_ARQVFE_MASK;
1645 }
1646 if (val & I40E_VF_ARQLEN_ARQOVFL_MASK) {
1647 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1648 val &= ~I40E_VF_ARQLEN_ARQOVFL_MASK;
1649 }
1650 if (val & I40E_VF_ARQLEN_ARQCRIT_MASK) {
1651 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1652 val &= ~I40E_VF_ARQLEN_ARQCRIT_MASK;
1653 }
1654 if (oldval != val)
1655 wr32(hw, hw->aq.arq.len, val);
1656
1657 val = rd32(hw, hw->aq.asq.len);
1658 oldval = val;
1659 if (val & I40E_VF_ATQLEN_ATQVFE_MASK) {
1660 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1661 val &= ~I40E_VF_ATQLEN_ATQVFE_MASK;
1662 }
1663 if (val & I40E_VF_ATQLEN_ATQOVFL_MASK) {
1664 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1665 val &= ~I40E_VF_ATQLEN_ATQOVFL_MASK;
1666 }
1667 if (val & I40E_VF_ATQLEN_ATQCRIT_MASK) {
1668 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1669 val &= ~I40E_VF_ATQLEN_ATQCRIT_MASK;
1670 }
1671 if (oldval != val)
1672 wr32(hw, hw->aq.asq.len, val);
1673
Mitch A Williams72354482014-12-09 08:53:07 +00001674 kfree(event.msg_buf);
1675out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001676 /* re-enable Admin queue interrupt cause */
1677 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001678}
1679
1680/**
1681 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1682 * @adapter: board private structure
1683 *
1684 * Free all transmit software resources
1685 **/
1686static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1687{
1688 int i;
1689
Mitch Williamscc052922014-10-25 03:24:34 +00001690 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001691 if (adapter->tx_rings[i]->desc)
1692 i40evf_free_tx_resources(adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001693}
1694
1695/**
1696 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1697 * @adapter: board private structure
1698 *
1699 * If this function returns with an error, then it's possible one or
1700 * more of the rings is populated (while the rest are not). It is the
1701 * callers duty to clean those orphaned rings.
1702 *
1703 * Return 0 on success, negative on failure
1704 **/
1705static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1706{
1707 int i, err = 0;
1708
Mitch Williamscc052922014-10-25 03:24:34 +00001709 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001710 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001711 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1712 if (!err)
1713 continue;
1714 dev_err(&adapter->pdev->dev,
1715 "%s: Allocation for Tx Queue %u failed\n",
1716 __func__, i);
1717 break;
1718 }
1719
1720 return err;
1721}
1722
1723/**
1724 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1725 * @adapter: board private structure
1726 *
1727 * If this function returns with an error, then it's possible one or
1728 * more of the rings is populated (while the rest are not). It is the
1729 * callers duty to clean those orphaned rings.
1730 *
1731 * Return 0 on success, negative on failure
1732 **/
1733static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1734{
1735 int i, err = 0;
1736
Mitch Williamscc052922014-10-25 03:24:34 +00001737 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001738 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001739 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1740 if (!err)
1741 continue;
1742 dev_err(&adapter->pdev->dev,
1743 "%s: Allocation for Rx Queue %u failed\n",
1744 __func__, i);
1745 break;
1746 }
1747 return err;
1748}
1749
1750/**
1751 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1752 * @adapter: board private structure
1753 *
1754 * Free all receive software resources
1755 **/
1756static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1757{
1758 int i;
1759
Mitch Williamscc052922014-10-25 03:24:34 +00001760 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001761 if (adapter->rx_rings[i]->desc)
1762 i40evf_free_rx_resources(adapter->rx_rings[i]);
1763}
1764
1765/**
1766 * i40evf_open - Called when a network interface is made active
1767 * @netdev: network interface device structure
1768 *
1769 * Returns 0 on success, negative value on failure
1770 *
1771 * The open entry point is called when a network interface is made
1772 * active by the system (IFF_UP). At this point all resources needed
1773 * for transmit and receive operations are allocated, the interrupt
1774 * handler is registered with the OS, the watchdog timer is started,
1775 * and the stack is notified that the interface is ready.
1776 **/
1777static int i40evf_open(struct net_device *netdev)
1778{
1779 struct i40evf_adapter *adapter = netdev_priv(netdev);
1780 int err;
1781
Mitch Williamsef8693e2014-02-13 03:48:53 -08001782 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1783 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1784 return -EIO;
1785 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001786 if (adapter->state != __I40EVF_DOWN)
1787 return -EBUSY;
1788
1789 /* allocate transmit descriptors */
1790 err = i40evf_setup_all_tx_resources(adapter);
1791 if (err)
1792 goto err_setup_tx;
1793
1794 /* allocate receive descriptors */
1795 err = i40evf_setup_all_rx_resources(adapter);
1796 if (err)
1797 goto err_setup_rx;
1798
1799 /* clear any pending interrupts, may auto mask */
1800 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1801 if (err)
1802 goto err_req_irq;
1803
1804 i40evf_configure(adapter);
1805
1806 err = i40evf_up_complete(adapter);
1807 if (err)
1808 goto err_req_irq;
1809
1810 i40evf_irq_enable(adapter, true);
1811
1812 return 0;
1813
1814err_req_irq:
1815 i40evf_down(adapter);
1816 i40evf_free_traffic_irqs(adapter);
1817err_setup_rx:
1818 i40evf_free_all_rx_resources(adapter);
1819err_setup_tx:
1820 i40evf_free_all_tx_resources(adapter);
1821
1822 return err;
1823}
1824
1825/**
1826 * i40evf_close - Disables a network interface
1827 * @netdev: network interface device structure
1828 *
1829 * Returns 0, this is not allowed to fail
1830 *
1831 * The close entry point is called when an interface is de-activated
1832 * by the OS. The hardware is still under the drivers control, but
1833 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1834 * are freed, along with all transmit and receive resources.
1835 **/
1836static int i40evf_close(struct net_device *netdev)
1837{
1838 struct i40evf_adapter *adapter = netdev_priv(netdev);
1839
Mitch Williamsef8693e2014-02-13 03:48:53 -08001840 if (adapter->state <= __I40EVF_DOWN)
1841 return 0;
1842
Mitch Williamsef8693e2014-02-13 03:48:53 -08001843
Greg Rose5eae00c2013-12-21 06:12:45 +00001844 set_bit(__I40E_DOWN, &adapter->vsi.state);
1845
1846 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001847 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001848 i40evf_free_traffic_irqs(adapter);
1849
1850 i40evf_free_all_tx_resources(adapter);
1851 i40evf_free_all_rx_resources(adapter);
1852
1853 return 0;
1854}
1855
1856/**
1857 * i40evf_get_stats - Get System Network Statistics
1858 * @netdev: network interface device structure
1859 *
1860 * Returns the address of the device statistics structure.
1861 * The statistics are actually updated from the timer callback.
1862 **/
1863static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1864{
1865 struct i40evf_adapter *adapter = netdev_priv(netdev);
1866
1867 /* only return the current stats */
1868 return &adapter->net_stats;
1869}
1870
1871/**
1872 * i40evf_reinit_locked - Software reinit
1873 * @adapter: board private structure
1874 *
1875 * Reinititalizes the ring structures in response to a software configuration
1876 * change. Roughly the same as close followed by open, but skips releasing
1877 * and reallocating the interrupts.
1878 **/
1879void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1880{
1881 struct net_device *netdev = adapter->netdev;
1882 int err;
1883
1884 WARN_ON(in_interrupt());
1885
Greg Rose5eae00c2013-12-21 06:12:45 +00001886 i40evf_down(adapter);
1887
1888 /* allocate transmit descriptors */
1889 err = i40evf_setup_all_tx_resources(adapter);
1890 if (err)
1891 goto err_reinit;
1892
1893 /* allocate receive descriptors */
1894 err = i40evf_setup_all_rx_resources(adapter);
1895 if (err)
1896 goto err_reinit;
1897
1898 i40evf_configure(adapter);
1899
1900 err = i40evf_up_complete(adapter);
1901 if (err)
1902 goto err_reinit;
1903
1904 i40evf_irq_enable(adapter, true);
1905 return;
1906
1907err_reinit:
Mitch Williams80e72892014-05-10 04:49:06 +00001908 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001909 i40evf_close(netdev);
1910}
1911
1912/**
1913 * i40evf_change_mtu - Change the Maximum Transfer Unit
1914 * @netdev: network interface device structure
1915 * @new_mtu: new value for maximum frame size
1916 *
1917 * Returns 0 on success, negative on failure
1918 **/
1919static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1920{
1921 struct i40evf_adapter *adapter = netdev_priv(netdev);
1922 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1923
1924 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1925 return -EINVAL;
1926
1927 /* must set new MTU before calling down or up */
1928 netdev->mtu = new_mtu;
1929 i40evf_reinit_locked(adapter);
1930 return 0;
1931}
1932
1933static const struct net_device_ops i40evf_netdev_ops = {
1934 .ndo_open = i40evf_open,
1935 .ndo_stop = i40evf_close,
1936 .ndo_start_xmit = i40evf_xmit_frame,
1937 .ndo_get_stats = i40evf_get_stats,
1938 .ndo_set_rx_mode = i40evf_set_rx_mode,
1939 .ndo_validate_addr = eth_validate_addr,
1940 .ndo_set_mac_address = i40evf_set_mac,
1941 .ndo_change_mtu = i40evf_change_mtu,
1942 .ndo_tx_timeout = i40evf_tx_timeout,
1943 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1944 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1945};
1946
1947/**
1948 * i40evf_check_reset_complete - check that VF reset is complete
1949 * @hw: pointer to hw struct
1950 *
1951 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1952 **/
1953static int i40evf_check_reset_complete(struct i40e_hw *hw)
1954{
1955 u32 rstat;
1956 int i;
1957
1958 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07001959 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
1960 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1961 if ((rstat == I40E_VFR_VFACTIVE) ||
1962 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00001963 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00001964 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00001965 }
1966 return -EBUSY;
1967}
1968
1969/**
1970 * i40evf_init_task - worker thread to perform delayed initialization
1971 * @work: pointer to work_struct containing our data
1972 *
1973 * This task completes the work that was begun in probe. Due to the nature
1974 * of VF-PF communications, we may need to wait tens of milliseconds to get
1975 * reponses back from the PF. Rather than busy-wait in probe and bog down the
1976 * whole system, we'll do it in a task so we can sleep.
1977 * This task only runs during driver init. Once we've established
1978 * communications with the PF driver and set up our netdev, the watchdog
1979 * takes over.
1980 **/
1981static void i40evf_init_task(struct work_struct *work)
1982{
1983 struct i40evf_adapter *adapter = container_of(work,
1984 struct i40evf_adapter,
1985 init_task.work);
1986 struct net_device *netdev = adapter->netdev;
1987 struct i40evf_mac_filter *f;
1988 struct i40e_hw *hw = &adapter->hw;
1989 struct pci_dev *pdev = adapter->pdev;
1990 int i, err, bufsz;
1991
1992 switch (adapter->state) {
1993 case __I40EVF_STARTUP:
1994 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08001995 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1996 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00001997 err = i40e_set_mac_type(hw);
1998 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08001999 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2000 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002001 goto err;
2002 }
2003 err = i40evf_check_reset_complete(hw);
2004 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002005 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002006 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002007 goto err;
2008 }
2009 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2010 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2011 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2012 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2013
2014 err = i40evf_init_adminq(hw);
2015 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002016 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2017 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002018 goto err;
2019 }
2020 err = i40evf_send_api_ver(adapter);
2021 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002022 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002023 i40evf_shutdown_adminq(hw);
2024 goto err;
2025 }
2026 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2027 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002028 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002029 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002030 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002031 i40evf_shutdown_adminq(hw);
2032 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002033 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002034 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002035
2036 /* aq msg sent, awaiting reply */
2037 err = i40evf_verify_api_ver(adapter);
2038 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002039 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002040 err = i40evf_send_api_ver(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002041 goto err;
2042 }
2043 err = i40evf_send_vf_config_msg(adapter);
2044 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002045 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002046 err);
2047 goto err;
2048 }
2049 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2050 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002051 case __I40EVF_INIT_GET_RESOURCES:
2052 /* aq msg sent, awaiting reply */
2053 if (!adapter->vf_res) {
2054 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2055 (I40E_MAX_VF_VSI *
2056 sizeof(struct i40e_virtchnl_vsi_resource));
2057 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002058 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002059 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002060 }
2061 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002062 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002063 err = i40evf_send_vf_config_msg(adapter);
2064 goto err;
2065 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002066 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002067 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2068 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002069 goto err_alloc;
2070 }
2071 adapter->state = __I40EVF_INIT_SW;
2072 break;
2073 default:
2074 goto err_alloc;
2075 }
2076 /* got VF config message back from PF, now we can parse it */
2077 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2078 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2079 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2080 }
2081 if (!adapter->vsi_res) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002082 dev_err(&pdev->dev, "No LAN VSI found\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002083 goto err_alloc;
2084 }
2085
2086 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2087
Greg Rose5eae00c2013-12-21 06:12:45 +00002088 netdev->netdev_ops = &i40evf_netdev_ops;
2089 i40evf_set_ethtool_ops(netdev);
2090 netdev->watchdog_timeo = 5 * HZ;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002091 netdev->features |= NETIF_F_HIGHDMA |
2092 NETIF_F_SG |
Greg Rose5eae00c2013-12-21 06:12:45 +00002093 NETIF_F_IP_CSUM |
2094 NETIF_F_SCTP_CSUM |
2095 NETIF_F_IPV6_CSUM |
2096 NETIF_F_TSO |
2097 NETIF_F_TSO6 |
Greg Rose3415e8c2014-01-30 12:40:27 +00002098 NETIF_F_RXCSUM |
Greg Rose5eae00c2013-12-21 06:12:45 +00002099 NETIF_F_GRO;
2100
2101 if (adapter->vf_res->vf_offload_flags
2102 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2103 netdev->vlan_features = netdev->features;
2104 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2105 NETIF_F_HW_VLAN_CTAG_RX |
2106 NETIF_F_HW_VLAN_CTAG_FILTER;
2107 }
2108
Greg Rose3415e8c2014-01-30 12:40:27 +00002109 /* copy netdev features into list of user selectable features */
2110 netdev->hw_features |= netdev->features;
2111 netdev->hw_features &= ~NETIF_F_RXCSUM;
2112
Greg Rose5eae00c2013-12-21 06:12:45 +00002113 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002114 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002115 adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002116 random_ether_addr(adapter->hw.mac.addr);
2117 }
Greg Rose9a173902014-05-22 06:32:02 +00002118 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2119 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002120
Greg Rose5eae00c2013-12-21 06:12:45 +00002121 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +00002122 if (!f)
Greg Rose5eae00c2013-12-21 06:12:45 +00002123 goto err_sw_init;
2124
Greg Rose9a173902014-05-22 06:32:02 +00002125 ether_addr_copy(f->macaddr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002126 f->add = true;
2127 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2128
2129 list_add(&f->list, &adapter->mac_filter_list);
2130
2131 init_timer(&adapter->watchdog_timer);
2132 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2133 adapter->watchdog_timer.data = (unsigned long)adapter;
2134 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2135
Mitch Williamscc052922014-10-25 03:24:34 +00002136 adapter->num_active_queues = min_t(int,
2137 adapter->vsi_res->num_queue_pairs,
2138 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002139 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2140 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002141 err = i40evf_init_interrupt_scheme(adapter);
2142 if (err)
2143 goto err_sw_init;
2144 i40evf_map_rings_to_vectors(adapter);
2145 i40evf_configure_rss(adapter);
2146 err = i40evf_request_misc_irq(adapter);
2147 if (err)
2148 goto err_sw_init;
2149
2150 netif_carrier_off(netdev);
2151
Greg Rose5eae00c2013-12-21 06:12:45 +00002152 adapter->vsi.id = adapter->vsi_res->vsi_id;
2153 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2154 adapter->vsi.back = adapter;
2155 adapter->vsi.base_vector = 1;
2156 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williamsca99eb92014-04-04 04:43:07 +00002157 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2158 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2159 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2160 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Greg Rose5eae00c2013-12-21 06:12:45 +00002161 adapter->vsi.netdev = adapter->netdev;
2162
Mitch Williamsef8693e2014-02-13 03:48:53 -08002163 if (!adapter->netdev_registered) {
2164 err = register_netdev(netdev);
2165 if (err)
2166 goto err_register;
2167 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002168
2169 adapter->netdev_registered = true;
2170
2171 netif_tx_stop_all_queues(netdev);
2172
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002173 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002174 if (netdev->features & NETIF_F_GRO)
2175 dev_info(&pdev->dev, "GRO is enabled\n");
2176
2177 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2178 adapter->state = __I40EVF_DOWN;
2179 set_bit(__I40E_DOWN, &adapter->vsi.state);
2180 i40evf_misc_irq_enable(adapter);
2181 return;
2182restart:
2183 schedule_delayed_work(&adapter->init_task,
2184 msecs_to_jiffies(50));
2185 return;
2186
2187err_register:
2188 i40evf_free_misc_irq(adapter);
2189err_sw_init:
2190 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002191err_alloc:
2192 kfree(adapter->vf_res);
2193 adapter->vf_res = NULL;
2194err:
2195 /* Things went into the weeds, so try again later */
2196 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002197 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002198 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002199 return; /* do not reschedule */
2200 }
2201 schedule_delayed_work(&adapter->init_task, HZ * 3);
Greg Rose5eae00c2013-12-21 06:12:45 +00002202}
2203
2204/**
2205 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2206 * @pdev: pci device structure
2207 **/
2208static void i40evf_shutdown(struct pci_dev *pdev)
2209{
2210 struct net_device *netdev = pci_get_drvdata(pdev);
2211
2212 netif_device_detach(netdev);
2213
2214 if (netif_running(netdev))
2215 i40evf_close(netdev);
2216
2217#ifdef CONFIG_PM
2218 pci_save_state(pdev);
2219
2220#endif
2221 pci_disable_device(pdev);
2222}
2223
2224/**
2225 * i40evf_probe - Device Initialization Routine
2226 * @pdev: PCI device information struct
2227 * @ent: entry in i40evf_pci_tbl
2228 *
2229 * Returns 0 on success, negative on failure
2230 *
2231 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2232 * The OS initialization, configuring of the adapter private structure,
2233 * and a hardware reset occur.
2234 **/
2235static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2236{
2237 struct net_device *netdev;
2238 struct i40evf_adapter *adapter = NULL;
2239 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002240 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002241
2242 err = pci_enable_device(pdev);
2243 if (err)
2244 return err;
2245
Mitch Williams64942942014-02-11 08:26:33 +00002246 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002247 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002248 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2249 if (err) {
2250 dev_err(&pdev->dev,
2251 "DMA configuration failed: 0x%x\n", err);
2252 goto err_dma;
2253 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002254 }
2255
2256 err = pci_request_regions(pdev, i40evf_driver_name);
2257 if (err) {
2258 dev_err(&pdev->dev,
2259 "pci_request_regions failed 0x%x\n", err);
2260 goto err_pci_reg;
2261 }
2262
2263 pci_enable_pcie_error_reporting(pdev);
2264
2265 pci_set_master(pdev);
2266
2267 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2268 MAX_TX_QUEUES);
2269 if (!netdev) {
2270 err = -ENOMEM;
2271 goto err_alloc_etherdev;
2272 }
2273
2274 SET_NETDEV_DEV(netdev, &pdev->dev);
2275
2276 pci_set_drvdata(pdev, netdev);
2277 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002278
2279 adapter->netdev = netdev;
2280 adapter->pdev = pdev;
2281
2282 hw = &adapter->hw;
2283 hw->back = adapter;
2284
2285 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2286 adapter->state = __I40EVF_STARTUP;
2287
2288 /* Call save state here because it relies on the adapter struct. */
2289 pci_save_state(pdev);
2290
2291 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2292 pci_resource_len(pdev, 0));
2293 if (!hw->hw_addr) {
2294 err = -EIO;
2295 goto err_ioremap;
2296 }
2297 hw->vendor_id = pdev->vendor;
2298 hw->device_id = pdev->device;
2299 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2300 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2301 hw->subsystem_device_id = pdev->subsystem_device;
2302 hw->bus.device = PCI_SLOT(pdev->devfn);
2303 hw->bus.func = PCI_FUNC(pdev->devfn);
2304
Serey Kong8bb1a542014-08-01 13:27:15 -07002305 INIT_LIST_HEAD(&adapter->mac_filter_list);
2306 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2307
Greg Rose5eae00c2013-12-21 06:12:45 +00002308 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2309 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2310 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2311 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2312 schedule_delayed_work(&adapter->init_task, 10);
2313
2314 return 0;
2315
2316err_ioremap:
2317 free_netdev(netdev);
2318err_alloc_etherdev:
2319 pci_release_regions(pdev);
2320err_pci_reg:
2321err_dma:
2322 pci_disable_device(pdev);
2323 return err;
2324}
2325
2326#ifdef CONFIG_PM
2327/**
2328 * i40evf_suspend - Power management suspend routine
2329 * @pdev: PCI device information struct
2330 * @state: unused
2331 *
2332 * Called when the system (VM) is entering sleep/suspend.
2333 **/
2334static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2335{
2336 struct net_device *netdev = pci_get_drvdata(pdev);
2337 struct i40evf_adapter *adapter = netdev_priv(netdev);
2338 int retval = 0;
2339
2340 netif_device_detach(netdev);
2341
2342 if (netif_running(netdev)) {
2343 rtnl_lock();
2344 i40evf_down(adapter);
2345 rtnl_unlock();
2346 }
2347 i40evf_free_misc_irq(adapter);
2348 i40evf_reset_interrupt_capability(adapter);
2349
2350 retval = pci_save_state(pdev);
2351 if (retval)
2352 return retval;
2353
2354 pci_disable_device(pdev);
2355
2356 return 0;
2357}
2358
2359/**
2360 * i40evf_resume - Power managment resume routine
2361 * @pdev: PCI device information struct
2362 *
2363 * Called when the system (VM) is resumed from sleep/suspend.
2364 **/
2365static int i40evf_resume(struct pci_dev *pdev)
2366{
2367 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2368 struct net_device *netdev = adapter->netdev;
2369 u32 err;
2370
2371 pci_set_power_state(pdev, PCI_D0);
2372 pci_restore_state(pdev);
2373 /* pci_restore_state clears dev->state_saved so call
2374 * pci_save_state to restore it.
2375 */
2376 pci_save_state(pdev);
2377
2378 err = pci_enable_device_mem(pdev);
2379 if (err) {
2380 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2381 return err;
2382 }
2383 pci_set_master(pdev);
2384
2385 rtnl_lock();
2386 err = i40evf_set_interrupt_capability(adapter);
2387 if (err) {
2388 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2389 return err;
2390 }
2391 err = i40evf_request_misc_irq(adapter);
2392 rtnl_unlock();
2393 if (err) {
2394 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2395 return err;
2396 }
2397
2398 schedule_work(&adapter->reset_task);
2399
2400 netif_device_attach(netdev);
2401
2402 return err;
2403}
2404
2405#endif /* CONFIG_PM */
2406/**
2407 * i40evf_remove - Device Removal Routine
2408 * @pdev: PCI device information struct
2409 *
2410 * i40evf_remove is called by the PCI subsystem to alert the driver
2411 * that it should release a PCI device. The could be caused by a
2412 * Hot-Plug event, or because the driver is going to be removed from
2413 * memory.
2414 **/
2415static void i40evf_remove(struct pci_dev *pdev)
2416{
2417 struct net_device *netdev = pci_get_drvdata(pdev);
2418 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002419 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002420 struct i40e_hw *hw = &adapter->hw;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002421 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +00002422
2423 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002424 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002425
2426 if (adapter->netdev_registered) {
2427 unregister_netdev(netdev);
2428 adapter->netdev_registered = false;
2429 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002430 while (count-- && adapter->aq_required)
2431 msleep(50);
2432
2433 if (count < 0)
2434 dev_err(&pdev->dev, "Timed out waiting for PF driver.\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002435 adapter->state = __I40EVF_REMOVE;
2436
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002437 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002438 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002439 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002440 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002441 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002442 }
2443
Mitch Williamse5d17c32014-06-04 04:22:38 +00002444 if (adapter->watchdog_timer.function)
2445 del_timer_sync(&adapter->watchdog_timer);
2446
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002447 flush_scheduled_work();
2448
Greg Rose5eae00c2013-12-21 06:12:45 +00002449 if (hw->aq.asq.count)
2450 i40evf_shutdown_adminq(hw);
2451
2452 iounmap(hw->hw_addr);
2453 pci_release_regions(pdev);
2454
2455 i40evf_free_queues(adapter);
2456 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002457 /* If we got removed before an up/down sequence, we've got a filter
2458 * hanging out there that we need to get rid of.
2459 */
2460 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2461 list_del(&f->list);
2462 kfree(f);
2463 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002464 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2465 list_del(&f->list);
2466 kfree(f);
2467 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002468
2469 free_netdev(netdev);
2470
2471 pci_disable_pcie_error_reporting(pdev);
2472
2473 pci_disable_device(pdev);
2474}
2475
2476static struct pci_driver i40evf_driver = {
2477 .name = i40evf_driver_name,
2478 .id_table = i40evf_pci_tbl,
2479 .probe = i40evf_probe,
2480 .remove = i40evf_remove,
2481#ifdef CONFIG_PM
2482 .suspend = i40evf_suspend,
2483 .resume = i40evf_resume,
2484#endif
2485 .shutdown = i40evf_shutdown,
2486};
2487
2488/**
2489 * i40e_init_module - Driver Registration Routine
2490 *
2491 * i40e_init_module is the first routine called when the driver is
2492 * loaded. All it does is register with the PCI subsystem.
2493 **/
2494static int __init i40evf_init_module(void)
2495{
2496 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002497
Greg Rose5eae00c2013-12-21 06:12:45 +00002498 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002499 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002500
2501 pr_info("%s\n", i40evf_copyright);
2502
2503 ret = pci_register_driver(&i40evf_driver);
2504 return ret;
2505}
2506
2507module_init(i40evf_init_module);
2508
2509/**
2510 * i40e_exit_module - Driver Exit Cleanup Routine
2511 *
2512 * i40e_exit_module is called just before the driver is removed
2513 * from memory.
2514 **/
2515static void __exit i40evf_exit_module(void)
2516{
2517 pci_unregister_driver(&i40evf_driver);
2518}
2519
2520module_exit(i40evf_exit_module);
2521
2522/* i40evf_main.c */