blob: 664fde68b537ba2190875cffefc7a96d12cd2683 [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Sravanthi Tangeda5b8eb172015-02-06 08:52:21 +00004 * Copyright(c) 2013 - 2015 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);
31static int i40evf_close(struct net_device *netdev);
32
33char i40evf_driver_name[] = "i40evf";
34static const char i40evf_driver_string[] =
Mitch Williams0af56f42014-06-04 20:42:10 +000035 "Intel(R) XL710/X710 Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000036
Catherine Sullivanf91638a2015-08-28 17:56:01 -040037#define DRV_VERSION "1.3.13"
Greg Rose5eae00c2013-12-21 06:12:45 +000038const char i40evf_driver_version[] = DRV_VERSION;
39static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040040 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000041
42/* i40evf_pci_tbl - PCI Device ID Table
43 *
44 * Wildcard entries (PCI_ANY_ID) should come last
45 * Last entry must be all 0s
46 *
47 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
48 * Class, Class Mask, private data (not used) }
49 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020050static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080051 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040052 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000053 /* required last entry */
54 {0, }
55};
56
57MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
58
59MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
60MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
61MODULE_LICENSE("GPL");
62MODULE_VERSION(DRV_VERSION);
63
64/**
65 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
66 * @hw: pointer to the HW structure
67 * @mem: ptr to mem struct to fill out
68 * @size: size of memory requested
69 * @alignment: what to align the allocation to
70 **/
71i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
72 struct i40e_dma_mem *mem,
73 u64 size, u32 alignment)
74{
75 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
76
77 if (!mem)
78 return I40E_ERR_PARAM;
79
80 mem->size = ALIGN(size, alignment);
81 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
82 (dma_addr_t *)&mem->pa, GFP_KERNEL);
83 if (mem->va)
84 return 0;
85 else
86 return I40E_ERR_NO_MEMORY;
87}
88
89/**
90 * i40evf_free_dma_mem_d - OS specific memory free for shared code
91 * @hw: pointer to the HW structure
92 * @mem: ptr to mem struct to free
93 **/
94i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
95{
96 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
97
98 if (!mem || !mem->va)
99 return I40E_ERR_PARAM;
100 dma_free_coherent(&adapter->pdev->dev, mem->size,
101 mem->va, (dma_addr_t)mem->pa);
102 return 0;
103}
104
105/**
106 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
107 * @hw: pointer to the HW structure
108 * @mem: ptr to mem struct to fill out
109 * @size: size of memory requested
110 **/
111i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
112 struct i40e_virt_mem *mem, u32 size)
113{
114 if (!mem)
115 return I40E_ERR_PARAM;
116
117 mem->size = size;
118 mem->va = kzalloc(size, GFP_KERNEL);
119
120 if (mem->va)
121 return 0;
122 else
123 return I40E_ERR_NO_MEMORY;
124}
125
126/**
127 * i40evf_free_virt_mem_d - OS specific memory free for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to free
130 **/
131i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 /* it's ok to kfree a NULL pointer */
138 kfree(mem->va);
139
140 return 0;
141}
142
143/**
144 * i40evf_debug_d - OS dependent version of debug printing
145 * @hw: pointer to the HW structure
146 * @mask: debug level mask
147 * @fmt_str: printf-type format description
148 **/
149void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
150{
151 char buf[512];
152 va_list argptr;
153
154 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
155 return;
156
157 va_start(argptr, fmt_str);
158 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
159 va_end(argptr);
160
161 /* the debug string is already formatted with a newline */
162 pr_info("%s", buf);
163}
164
165/**
166 * i40evf_tx_timeout - Respond to a Tx Hang
167 * @netdev: network interface device structure
168 **/
169static void i40evf_tx_timeout(struct net_device *netdev)
170{
171 struct i40evf_adapter *adapter = netdev_priv(netdev);
172
173 adapter->tx_timeout_count++;
Mitch Williams67c818a2015-06-19 08:56:30 -0700174 if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
175 I40EVF_FLAG_RESET_NEEDED))) {
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);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400207 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000208
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++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400244 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000247 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400248 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000249 }
250 }
251}
252
253/**
254 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
255 * @adapter: board private structure
256 * @mask: bitmap of vectors to trigger
257 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000258static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000259{
260 struct i40e_hw *hw = &adapter->hw;
261 int i;
262 uint32_t dyn_ctl;
263
Mitch Williams164ec1b2014-06-04 08:45:19 +0000264 if (mask & 1) {
265 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400266 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000267 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400268 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000269 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
270 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000271 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400272 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000273 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400274 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000275 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400276 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
278 }
279 }
280}
281
282/**
283 * i40evf_irq_enable - Enable default interrupt generation settings
284 * @adapter: board private structure
285 **/
286void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
287{
288 struct i40e_hw *hw = &adapter->hw;
289
Mitch Williams164ec1b2014-06-04 08:45:19 +0000290 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000291 i40evf_irq_enable_queues(adapter, ~0);
292
293 if (flush)
294 rd32(hw, I40E_VFGEN_RSTAT);
295}
296
297/**
298 * i40evf_msix_aq - Interrupt handler for vector 0
299 * @irq: interrupt number
300 * @data: pointer to netdev
301 **/
302static irqreturn_t i40evf_msix_aq(int irq, void *data)
303{
304 struct net_device *netdev = data;
305 struct i40evf_adapter *adapter = netdev_priv(netdev);
306 struct i40e_hw *hw = &adapter->hw;
307 u32 val;
308 u32 ena_mask;
309
310 /* handle non-queue interrupts */
311 val = rd32(hw, I40E_VFINT_ICR01);
312 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
313
314
315 val = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400316 val = val | I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000317 wr32(hw, I40E_VFINT_DYN_CTL01, val);
318
Greg Rose5eae00c2013-12-21 06:12:45 +0000319 /* schedule work on the private workqueue */
320 schedule_work(&adapter->adminq_task);
321
322 return IRQ_HANDLED;
323}
324
325/**
326 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
327 * @irq: interrupt number
328 * @data: pointer to a q_vector
329 **/
330static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
331{
332 struct i40e_q_vector *q_vector = data;
333
334 if (!q_vector->tx.ring && !q_vector->rx.ring)
335 return IRQ_HANDLED;
336
337 napi_schedule(&q_vector->napi);
338
339 return IRQ_HANDLED;
340}
341
342/**
343 * i40evf_map_vector_to_rxq - associate irqs with rx queues
344 * @adapter: board private structure
345 * @v_idx: interrupt number
346 * @r_idx: queue number
347 **/
348static void
349i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
350{
351 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
352 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
353
354 rx_ring->q_vector = q_vector;
355 rx_ring->next = q_vector->rx.ring;
356 rx_ring->vsi = &adapter->vsi;
357 q_vector->rx.ring = rx_ring;
358 q_vector->rx.count++;
359 q_vector->rx.latency_range = I40E_LOW_LATENCY;
360}
361
362/**
363 * i40evf_map_vector_to_txq - associate irqs with tx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @t_idx: queue number
367 **/
368static void
369i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
370{
371 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
372 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
373
374 tx_ring->q_vector = q_vector;
375 tx_ring->next = q_vector->tx.ring;
376 tx_ring->vsi = &adapter->vsi;
377 q_vector->tx.ring = tx_ring;
378 q_vector->tx.count++;
379 q_vector->tx.latency_range = I40E_LOW_LATENCY;
380 q_vector->num_ringpairs++;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400381 q_vector->ring_mask |= BIT(t_idx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000382}
383
384/**
385 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
386 * @adapter: board private structure to initialize
387 *
388 * This function maps descriptor rings to the queue-specific vectors
389 * we were allotted through the MSI-X enabling code. Ideally, we'd have
390 * one vector per ring/queue, but on a constrained vector budget, we
391 * group the rings as "efficiently" as possible. You would add new
392 * mapping configurations in here.
393 **/
394static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
395{
396 int q_vectors;
397 int v_start = 0;
398 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000399 int rxr_remaining = adapter->num_active_queues;
400 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000401 int i, j;
402 int rqpv, tqpv;
403 int err = 0;
404
405 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
406
407 /* The ideal configuration...
408 * We have enough vectors to map one per queue.
409 */
Mitch Williams973371d2015-04-27 14:57:09 -0400410 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000411 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
412 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
413
414 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
415 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
416 goto out;
417 }
418
419 /* If we don't have enough vectors for a 1-to-1
420 * mapping, we'll have to group them so there are
421 * multiple queues per vector.
422 * Re-adjusting *qpv takes care of the remainder.
423 */
424 for (i = v_start; i < q_vectors; i++) {
425 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
426 for (j = 0; j < rqpv; j++) {
427 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
428 rxr_idx++;
429 rxr_remaining--;
430 }
431 }
432 for (i = v_start; i < q_vectors; i++) {
433 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
434 for (j = 0; j < tqpv; j++) {
435 i40evf_map_vector_to_txq(adapter, i, txr_idx);
436 txr_idx++;
437 txr_remaining--;
438 }
439 }
440
441out:
442 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
443
444 return err;
445}
446
447/**
448 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
449 * @adapter: board private structure
450 *
451 * Allocates MSI-X vectors for tx and rx handling, and requests
452 * interrupts from the kernel.
453 **/
454static int
455i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
456{
457 int vector, err, q_vectors;
458 int rx_int_idx = 0, tx_int_idx = 0;
459
460 i40evf_irq_disable(adapter);
461 /* Decrement for Other and TCP Timer vectors */
462 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
463
464 for (vector = 0; vector < q_vectors; vector++) {
465 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
466
467 if (q_vector->tx.ring && q_vector->rx.ring) {
468 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
469 "i40evf-%s-%s-%d", basename,
470 "TxRx", rx_int_idx++);
471 tx_int_idx++;
472 } else if (q_vector->rx.ring) {
473 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
474 "i40evf-%s-%s-%d", basename,
475 "rx", rx_int_idx++);
476 } else if (q_vector->tx.ring) {
477 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
478 "i40evf-%s-%s-%d", basename,
479 "tx", tx_int_idx++);
480 } else {
481 /* skip this unused q_vector */
482 continue;
483 }
484 err = request_irq(
485 adapter->msix_entries[vector + NONQ_VECS].vector,
486 i40evf_msix_clean_rings,
487 0,
488 q_vector->name,
489 q_vector);
490 if (err) {
491 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400492 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000493 goto free_queue_irqs;
494 }
495 /* assign the mask for this irq */
496 irq_set_affinity_hint(
497 adapter->msix_entries[vector + NONQ_VECS].vector,
498 q_vector->affinity_mask);
499 }
500
501 return 0;
502
503free_queue_irqs:
504 while (vector) {
505 vector--;
506 irq_set_affinity_hint(
507 adapter->msix_entries[vector + NONQ_VECS].vector,
508 NULL);
509 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
510 adapter->q_vector[vector]);
511 }
512 return err;
513}
514
515/**
516 * i40evf_request_misc_irq - Initialize MSI-X interrupts
517 * @adapter: board private structure
518 *
519 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
520 * vector is only for the admin queue, and stays active even when the netdev
521 * is closed.
522 **/
523static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
524{
525 struct net_device *netdev = adapter->netdev;
526 int err;
527
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700528 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000529 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
530 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000531 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800532 &i40evf_msix_aq, 0,
533 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000534 if (err) {
535 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800536 "request_irq for %s failed: %d\n",
537 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000538 free_irq(adapter->msix_entries[0].vector, netdev);
539 }
540 return err;
541}
542
543/**
544 * i40evf_free_traffic_irqs - Free MSI-X interrupts
545 * @adapter: board private structure
546 *
547 * Frees all MSI-X vectors other than 0.
548 **/
549static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
550{
551 int i;
552 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000553
Greg Rose5eae00c2013-12-21 06:12:45 +0000554 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
555
556 for (i = 0; i < q_vectors; i++) {
557 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
558 NULL);
559 free_irq(adapter->msix_entries[i+1].vector,
560 adapter->q_vector[i]);
561 }
562}
563
564/**
565 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
566 * @adapter: board private structure
567 *
568 * Frees MSI-X vector 0.
569 **/
570static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
571{
572 struct net_device *netdev = adapter->netdev;
573
574 free_irq(adapter->msix_entries[0].vector, netdev);
575}
576
577/**
578 * i40evf_configure_tx - Configure Transmit Unit after Reset
579 * @adapter: board private structure
580 *
581 * Configure the Tx unit of the MAC after a reset.
582 **/
583static void i40evf_configure_tx(struct i40evf_adapter *adapter)
584{
585 struct i40e_hw *hw = &adapter->hw;
586 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000587
Mitch Williamscc052922014-10-25 03:24:34 +0000588 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000589 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
590}
591
592/**
593 * i40evf_configure_rx - Configure Receive Unit after Reset
594 * @adapter: board private structure
595 *
596 * Configure the Rx unit of the MAC after a reset.
597 **/
598static void i40evf_configure_rx(struct i40evf_adapter *adapter)
599{
600 struct i40e_hw *hw = &adapter->hw;
601 struct net_device *netdev = adapter->netdev;
602 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
603 int i;
604 int rx_buf_len;
605
606
607 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
608 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
609
610 /* Decide whether to use packet split mode or not */
611 if (netdev->mtu > ETH_DATA_LEN) {
612 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
613 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
614 else
615 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
616 } else {
617 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
618 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
619 else
620 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
621 }
622
623 /* Set the RX buffer length according to the mode */
624 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
625 rx_buf_len = I40E_RX_HDR_SIZE;
626 } else {
627 if (netdev->mtu <= ETH_DATA_LEN)
628 rx_buf_len = I40EVF_RXBUFFER_2048;
629 else
630 rx_buf_len = ALIGN(max_frame, 1024);
631 }
632
Mitch Williamscc052922014-10-25 03:24:34 +0000633 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000634 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
635 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
636 }
637}
638
639/**
640 * i40evf_find_vlan - Search filter list for specific vlan filter
641 * @adapter: board private structure
642 * @vlan: vlan tag
643 *
644 * Returns ptr to the filter object or NULL
645 **/
646static struct
647i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
648{
649 struct i40evf_vlan_filter *f;
650
651 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
652 if (vlan == f->vlan)
653 return f;
654 }
655 return NULL;
656}
657
658/**
659 * i40evf_add_vlan - Add a vlan filter to the list
660 * @adapter: board private structure
661 * @vlan: VLAN tag
662 *
663 * Returns ptr to the filter object or NULL when no memory available.
664 **/
665static struct
666i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
667{
Mitch Williams13acb542015-03-31 00:45:05 -0700668 struct i40evf_vlan_filter *f = NULL;
669 int count = 50;
670
671 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
672 &adapter->crit_section)) {
673 udelay(1);
674 if (--count == 0)
675 goto out;
676 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000677
678 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000679 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000680 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000681 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700682 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000683
Greg Rose5eae00c2013-12-21 06:12:45 +0000684 f->vlan = vlan;
685
686 INIT_LIST_HEAD(&f->list);
687 list_add(&f->list, &adapter->vlan_filter_list);
688 f->add = true;
689 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
690 }
691
Mitch Williams13acb542015-03-31 00:45:05 -0700692clearout:
693 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
694out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000695 return f;
696}
697
698/**
699 * i40evf_del_vlan - Remove a vlan filter from the list
700 * @adapter: board private structure
701 * @vlan: VLAN tag
702 **/
703static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
704{
705 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700706 int count = 50;
707
708 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
709 &adapter->crit_section)) {
710 udelay(1);
711 if (--count == 0)
712 return;
713 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000714
715 f = i40evf_find_vlan(adapter, vlan);
716 if (f) {
717 f->remove = true;
718 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
719 }
Mitch Williams13acb542015-03-31 00:45:05 -0700720 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000721}
722
723/**
724 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
725 * @netdev: network device struct
726 * @vid: VLAN tag
727 **/
728static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000729 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000730{
731 struct i40evf_adapter *adapter = netdev_priv(netdev);
732
733 if (i40evf_add_vlan(adapter, vid) == NULL)
734 return -ENOMEM;
735 return 0;
736}
737
738/**
739 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
740 * @netdev: network device struct
741 * @vid: VLAN tag
742 **/
743static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000744 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000745{
746 struct i40evf_adapter *adapter = netdev_priv(netdev);
747
748 i40evf_del_vlan(adapter, vid);
749 return 0;
750}
751
752/**
753 * i40evf_find_filter - Search filter list for specific mac filter
754 * @adapter: board private structure
755 * @macaddr: the MAC address
756 *
757 * Returns ptr to the filter object or NULL
758 **/
759static struct
760i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
761 u8 *macaddr)
762{
763 struct i40evf_mac_filter *f;
764
765 if (!macaddr)
766 return NULL;
767
768 list_for_each_entry(f, &adapter->mac_filter_list, list) {
769 if (ether_addr_equal(macaddr, f->macaddr))
770 return f;
771 }
772 return NULL;
773}
774
775/**
776 * i40e_add_filter - Add a mac filter to the filter list
777 * @adapter: board private structure
778 * @macaddr: the MAC address
779 *
780 * Returns ptr to the filter object or NULL when no memory available.
781 **/
782static struct
783i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
784 u8 *macaddr)
785{
786 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000787 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000788
789 if (!macaddr)
790 return NULL;
791
792 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000793 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000794 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000795 if (--count == 0)
796 return NULL;
797 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000798
799 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000800 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000801 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000802 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000803 clear_bit(__I40EVF_IN_CRITICAL_TASK,
804 &adapter->crit_section);
805 return NULL;
806 }
807
Greg Rose9a173902014-05-22 06:32:02 +0000808 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000809
810 list_add(&f->list, &adapter->mac_filter_list);
811 f->add = true;
812 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
813 }
814
815 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
816 return f;
817}
818
819/**
820 * i40evf_set_mac - NDO callback to set port mac address
821 * @netdev: network interface device structure
822 * @p: pointer to an address structure
823 *
824 * Returns 0 on success, negative on failure
825 **/
826static int i40evf_set_mac(struct net_device *netdev, void *p)
827{
828 struct i40evf_adapter *adapter = netdev_priv(netdev);
829 struct i40e_hw *hw = &adapter->hw;
830 struct i40evf_mac_filter *f;
831 struct sockaddr *addr = p;
832
833 if (!is_valid_ether_addr(addr->sa_data))
834 return -EADDRNOTAVAIL;
835
836 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
837 return 0;
838
839 f = i40evf_add_filter(adapter, addr->sa_data);
840 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000841 ether_addr_copy(hw->mac.addr, addr->sa_data);
842 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000843 }
844
845 return (f == NULL) ? -ENOMEM : 0;
846}
847
848/**
849 * i40evf_set_rx_mode - NDO callback to set the netdev filters
850 * @netdev: network interface device structure
851 **/
852static void i40evf_set_rx_mode(struct net_device *netdev)
853{
854 struct i40evf_adapter *adapter = netdev_priv(netdev);
855 struct i40evf_mac_filter *f, *ftmp;
856 struct netdev_hw_addr *uca;
857 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400858 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000859 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000860
861 /* add addr if not already in the filter list */
862 netdev_for_each_uc_addr(uca, netdev) {
863 i40evf_add_filter(adapter, uca->addr);
864 }
865 netdev_for_each_mc_addr(mca, netdev) {
866 i40evf_add_filter(adapter, mca->addr);
867 }
868
869 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000870 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000871 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000872 if (--count == 0) {
873 dev_err(&adapter->pdev->dev,
874 "Failed to get lock in %s\n", __func__);
875 return;
876 }
877 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000878 /* remove filter if not in netdev list */
879 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400880 netdev_for_each_mc_addr(mca, netdev)
881 if (ether_addr_equal(mca->addr, f->macaddr))
882 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000883
Shannon Nelson2f41f332015-08-26 15:14:20 -0400884 netdev_for_each_uc_addr(uca, netdev)
885 if (ether_addr_equal(uca->addr, f->macaddr))
886 goto bottom_of_search_loop;
887
888 for_each_dev_addr(netdev, ha)
889 if (ether_addr_equal(ha->addr, f->macaddr))
890 goto bottom_of_search_loop;
891
892 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
893 goto bottom_of_search_loop;
894
895 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
896 f->remove = true;
897 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
898
899bottom_of_search_loop:
900 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000901 }
902 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
903}
904
905/**
906 * i40evf_napi_enable_all - enable NAPI on all queue vectors
907 * @adapter: board private structure
908 **/
909static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
910{
911 int q_idx;
912 struct i40e_q_vector *q_vector;
913 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
914
915 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
916 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000917
Greg Rose5eae00c2013-12-21 06:12:45 +0000918 q_vector = adapter->q_vector[q_idx];
919 napi = &q_vector->napi;
920 napi_enable(napi);
921 }
922}
923
924/**
925 * i40evf_napi_disable_all - disable NAPI on all queue vectors
926 * @adapter: board private structure
927 **/
928static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
929{
930 int q_idx;
931 struct i40e_q_vector *q_vector;
932 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
933
934 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
935 q_vector = adapter->q_vector[q_idx];
936 napi_disable(&q_vector->napi);
937 }
938}
939
940/**
941 * i40evf_configure - set up transmit and receive data structures
942 * @adapter: board private structure
943 **/
944static void i40evf_configure(struct i40evf_adapter *adapter)
945{
946 struct net_device *netdev = adapter->netdev;
947 int i;
948
949 i40evf_set_rx_mode(netdev);
950
951 i40evf_configure_tx(adapter);
952 i40evf_configure_rx(adapter);
953 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
954
Mitch Williamscc052922014-10-25 03:24:34 +0000955 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000956 struct i40e_ring *ring = adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000957
Mitch Williamsa132af22015-01-24 09:58:35 +0000958 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Greg Rose5eae00c2013-12-21 06:12:45 +0000959 ring->next_to_use = ring->count - 1;
960 writel(ring->next_to_use, ring->tail);
961 }
962}
963
964/**
965 * i40evf_up_complete - Finish the last steps of bringing up a connection
966 * @adapter: board private structure
967 **/
968static int i40evf_up_complete(struct i40evf_adapter *adapter)
969{
970 adapter->state = __I40EVF_RUNNING;
971 clear_bit(__I40E_DOWN, &adapter->vsi.state);
972
973 i40evf_napi_enable_all(adapter);
974
975 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
976 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
977 return 0;
978}
979
980/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000981 * i40e_down - Shutdown the connection processing
982 * @adapter: board private structure
983 **/
984void i40evf_down(struct i40evf_adapter *adapter)
985{
986 struct net_device *netdev = adapter->netdev;
987 struct i40evf_mac_filter *f;
988
Mitch Williamsddf0b3a2014-05-22 06:31:46 +0000989 if (adapter->state == __I40EVF_DOWN)
990 return;
991
Mitch A Williams53d0b3a2014-12-09 08:53:04 +0000992 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
993 &adapter->crit_section))
994 usleep_range(500, 1000);
995
Mitch Williams63e18c22015-03-27 00:12:10 -0700996 netif_carrier_off(netdev);
997 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +0000998 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -0700999 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001000
Mitch Williamsef8693e2014-02-13 03:48:53 -08001001 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001002 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1003 f->remove = true;
1004 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001005 /* remove all VLAN filters */
1006 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1007 f->remove = true;
1008 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001009 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1010 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001011 /* cancel any current operation */
1012 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001013 /* Schedule operations to close down the HW. Don't wait
1014 * here for this to complete. The watchdog is still running
1015 * and it will take care of this.
1016 */
1017 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001018 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001019 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001020 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001021
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001022 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001023}
1024
1025/**
1026 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1027 * @adapter: board private structure
1028 * @vectors: number of vectors to request
1029 *
1030 * Work with the OS to set up the MSIX vectors needed.
1031 *
1032 * Returns 0 on success, negative on failure
1033 **/
1034static int
1035i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1036{
1037 int err, vector_threshold;
1038
1039 /* We'll want at least 3 (vector_threshold):
1040 * 0) Other (Admin Queue and link, mostly)
1041 * 1) TxQ[0] Cleanup
1042 * 2) RxQ[0] Cleanup
1043 */
1044 vector_threshold = MIN_MSIX_COUNT;
1045
1046 /* The more we get, the more we will assign to Tx/Rx Cleanup
1047 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1048 * Right now, we simply care about how many we'll get; we'll
1049 * set them up later while requesting irq's.
1050 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001051 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1052 vector_threshold, vectors);
1053 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001054 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001055 kfree(adapter->msix_entries);
1056 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001057 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001058 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001059
1060 /* Adjust for only the vectors we'll use, which is minimum
1061 * of max_msix_q_vectors + NONQ_VECS, or the number of
1062 * vectors we were allocated.
1063 */
1064 adapter->num_msix_vectors = err;
1065 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001066}
1067
1068/**
1069 * i40evf_free_queues - Free memory for all rings
1070 * @adapter: board private structure to initialize
1071 *
1072 * Free all of the memory associated with queue pairs.
1073 **/
1074static void i40evf_free_queues(struct i40evf_adapter *adapter)
1075{
1076 int i;
1077
1078 if (!adapter->vsi_res)
1079 return;
Mitch Williamscc052922014-10-25 03:24:34 +00001080 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001081 if (adapter->tx_rings[i])
1082 kfree_rcu(adapter->tx_rings[i], rcu);
1083 adapter->tx_rings[i] = NULL;
1084 adapter->rx_rings[i] = NULL;
1085 }
1086}
1087
1088/**
1089 * i40evf_alloc_queues - Allocate memory for all rings
1090 * @adapter: board private structure to initialize
1091 *
1092 * We allocate one ring per queue at run-time since we don't know the
1093 * number of queues at compile-time. The polling_netdev array is
1094 * intended for Multiqueue, but should work fine with a single queue.
1095 **/
1096static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1097{
1098 int i;
1099
Mitch Williamscc052922014-10-25 03:24:34 +00001100 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001101 struct i40e_ring *tx_ring;
1102 struct i40e_ring *rx_ring;
1103
Mitch Williams75a64432014-11-11 20:02:42 +00001104 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001105 if (!tx_ring)
1106 goto err_out;
1107
1108 tx_ring->queue_index = i;
1109 tx_ring->netdev = adapter->netdev;
1110 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001111 tx_ring->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001112 adapter->tx_rings[i] = tx_ring;
1113
1114 rx_ring = &tx_ring[1];
1115 rx_ring->queue_index = i;
1116 rx_ring->netdev = adapter->netdev;
1117 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001118 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001119 adapter->rx_rings[i] = rx_ring;
1120 }
1121
1122 return 0;
1123
1124err_out:
1125 i40evf_free_queues(adapter);
1126 return -ENOMEM;
1127}
1128
1129/**
1130 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1131 * @adapter: board private structure to initialize
1132 *
1133 * Attempt to configure the interrupts using the best available
1134 * capabilities of the hardware and the kernel.
1135 **/
1136static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1137{
1138 int vector, v_budget;
1139 int pairs = 0;
1140 int err = 0;
1141
1142 if (!adapter->vsi_res) {
1143 err = -EIO;
1144 goto out;
1145 }
Mitch Williamscc052922014-10-25 03:24:34 +00001146 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001147
1148 /* It's easy to be greedy for MSI-X vectors, but it really
1149 * doesn't do us much good if we have a lot more vectors
1150 * than CPU's. So let's be conservative and only ask for
1151 * (roughly) twice the number of vectors as there are CPU's.
1152 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001153 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1154 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001155
Greg Rose5eae00c2013-12-21 06:12:45 +00001156 adapter->msix_entries = kcalloc(v_budget,
1157 sizeof(struct msix_entry), GFP_KERNEL);
1158 if (!adapter->msix_entries) {
1159 err = -ENOMEM;
1160 goto out;
1161 }
1162
1163 for (vector = 0; vector < v_budget; vector++)
1164 adapter->msix_entries[vector].entry = vector;
1165
Mitch Williams313ed2d2015-08-27 11:42:31 -04001166 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001167
1168out:
1169 adapter->netdev->real_num_tx_queues = pairs;
1170 return err;
1171}
1172
1173/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001174 * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1175 * @vsi: vsi structure
1176 * @seed: RSS hash seed
1177 **/
1178static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1179{
1180 struct i40e_aqc_get_set_rss_key_data rss_key;
1181 struct i40evf_adapter *adapter = vsi->back;
1182 struct i40e_hw *hw = &adapter->hw;
1183 int ret = 0, i;
1184 u8 *rss_lut;
1185
1186 if (!vsi->id)
1187 return;
1188
1189 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1190 /* bail because we already have a command pending */
1191 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1192 adapter->current_op);
1193 return;
1194 }
1195
1196 memset(&rss_key, 0, sizeof(rss_key));
1197 memcpy(&rss_key, seed, sizeof(rss_key));
1198
1199 rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1200 if (!rss_lut)
1201 return;
1202
1203 /* Populate the LUT with max no. PF queues in round robin fashion */
1204 for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1205 rss_lut[i] = i % adapter->num_active_queues;
1206
1207 ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1208 if (ret) {
1209 dev_err(&adapter->pdev->dev,
1210 "Cannot set RSS key, err %s aq_err %s\n",
1211 i40evf_stat_str(hw, ret),
1212 i40evf_aq_str(hw, hw->aq.asq_last_status));
1213 return;
1214 }
1215
1216 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1217 (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1218 if (ret)
1219 dev_err(&adapter->pdev->dev,
1220 "Cannot set RSS lut, err %s aq_err %s\n",
1221 i40evf_stat_str(hw, ret),
1222 i40evf_aq_str(hw, hw->aq.asq_last_status));
1223}
1224
1225/**
1226 * i40e_configure_rss_reg - Prepare for RSS if used
1227 * @adapter: board private structure
1228 * @seed: RSS hash seed
1229 **/
1230static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1231 const u8 *seed)
1232{
1233 struct i40e_hw *hw = &adapter->hw;
1234 u32 *seed_dw = (u32 *)seed;
1235 u32 cqueue = 0;
1236 u32 lut = 0;
1237 int i, j;
1238
1239 /* Fill out hash function seed */
1240 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1241 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1242
1243 /* Populate the LUT with max no. PF queues in round robin fashion */
1244 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1245 lut = 0;
1246 for (j = 0; j < 4; j++) {
1247 if (cqueue == adapter->num_active_queues)
1248 cqueue = 0;
1249 lut |= ((cqueue) << (8 * j));
1250 cqueue++;
1251 }
1252 wr32(hw, I40E_VFQF_HLUT(i), lut);
1253 }
1254 i40e_flush(hw);
1255}
1256
1257/**
1258 * i40evf_configure_rss - Prepare for RSS
1259 * @adapter: board private structure
1260 **/
1261static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1262{
1263 struct i40e_hw *hw = &adapter->hw;
1264 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1265 u64 hena;
1266
1267 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1268
1269 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1270 hena = I40E_DEFAULT_RSS_HENA;
1271 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1272 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1273
1274 if (RSS_AQ(adapter))
1275 i40evf_configure_rss_aq(&adapter->vsi, seed);
1276 else
1277 i40evf_configure_rss_reg(adapter, seed);
1278}
1279
1280/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001281 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1282 * @adapter: board private structure to initialize
1283 *
1284 * We allocate one q_vector per queue interrupt. If allocation fails we
1285 * return -ENOMEM.
1286 **/
1287static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1288{
1289 int q_idx, num_q_vectors;
1290 struct i40e_q_vector *q_vector;
1291
1292 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1293
1294 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams75a64432014-11-11 20:02:42 +00001295 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001296 if (!q_vector)
1297 goto err_out;
1298 q_vector->adapter = adapter;
1299 q_vector->vsi = &adapter->vsi;
1300 q_vector->v_idx = q_idx;
1301 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001302 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001303 adapter->q_vector[q_idx] = q_vector;
1304 }
1305
1306 return 0;
1307
1308err_out:
1309 while (q_idx) {
1310 q_idx--;
1311 q_vector = adapter->q_vector[q_idx];
1312 netif_napi_del(&q_vector->napi);
1313 kfree(q_vector);
1314 adapter->q_vector[q_idx] = NULL;
1315 }
1316 return -ENOMEM;
1317}
1318
1319/**
1320 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1321 * @adapter: board private structure to initialize
1322 *
1323 * This function frees the memory allocated to the q_vectors. In addition if
1324 * NAPI is enabled it will delete any references to the NAPI struct prior
1325 * to freeing the q_vector.
1326 **/
1327static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1328{
1329 int q_idx, num_q_vectors;
1330 int napi_vectors;
1331
1332 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001333 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001334
1335 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1336 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1337
1338 adapter->q_vector[q_idx] = NULL;
1339 if (q_idx < napi_vectors)
1340 netif_napi_del(&q_vector->napi);
1341 kfree(q_vector);
1342 }
1343}
1344
1345/**
1346 * i40evf_reset_interrupt_capability - Reset MSIX setup
1347 * @adapter: board private structure
1348 *
1349 **/
1350void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1351{
1352 pci_disable_msix(adapter->pdev);
1353 kfree(adapter->msix_entries);
1354 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001355}
1356
1357/**
1358 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1359 * @adapter: board private structure to initialize
1360 *
1361 **/
1362int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1363{
1364 int err;
1365
1366 err = i40evf_set_interrupt_capability(adapter);
1367 if (err) {
1368 dev_err(&adapter->pdev->dev,
1369 "Unable to setup interrupt capabilities\n");
1370 goto err_set_interrupt;
1371 }
1372
1373 err = i40evf_alloc_q_vectors(adapter);
1374 if (err) {
1375 dev_err(&adapter->pdev->dev,
1376 "Unable to allocate memory for queue vectors\n");
1377 goto err_alloc_q_vectors;
1378 }
1379
1380 err = i40evf_alloc_queues(adapter);
1381 if (err) {
1382 dev_err(&adapter->pdev->dev,
1383 "Unable to allocate memory for queues\n");
1384 goto err_alloc_queues;
1385 }
1386
1387 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001388 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1389 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001390
1391 return 0;
1392err_alloc_queues:
1393 i40evf_free_q_vectors(adapter);
1394err_alloc_q_vectors:
1395 i40evf_reset_interrupt_capability(adapter);
1396err_set_interrupt:
1397 return err;
1398}
1399
1400/**
1401 * i40evf_watchdog_timer - Periodic call-back timer
1402 * @data: pointer to adapter disguised as unsigned long
1403 **/
1404static void i40evf_watchdog_timer(unsigned long data)
1405{
1406 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001407
Greg Rose5eae00c2013-12-21 06:12:45 +00001408 schedule_work(&adapter->watchdog_task);
1409 /* timer will be rescheduled in watchdog task */
1410}
1411
1412/**
1413 * i40evf_watchdog_task - Periodic call-back task
1414 * @work: pointer to work_struct
1415 **/
1416static void i40evf_watchdog_task(struct work_struct *work)
1417{
1418 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001419 struct i40evf_adapter,
1420 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001421 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001422 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001423
Greg Rose5eae00c2013-12-21 06:12:45 +00001424 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001425 goto restart_watchdog;
1426
1427 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001428 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1429 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1430 if ((reg_val == I40E_VFR_VFACTIVE) ||
1431 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001432 /* A chance for redemption! */
1433 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1434 adapter->state = __I40EVF_STARTUP;
1435 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1436 schedule_delayed_work(&adapter->init_task, 10);
1437 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1438 &adapter->crit_section);
1439 /* Don't reschedule the watchdog, since we've restarted
1440 * the init task. When init_task contacts the PF and
1441 * gets everything set up again, it'll restart the
1442 * watchdog for us. Down, boy. Sit. Stay. Woof.
1443 */
1444 return;
1445 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001446 adapter->aq_required = 0;
1447 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1448 goto watchdog_done;
1449 }
1450
1451 if ((adapter->state < __I40EVF_DOWN) ||
1452 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001453 goto watchdog_done;
1454
Mitch Williamsef8693e2014-02-13 03:48:53 -08001455 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001456 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1457 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001458 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001459 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001460 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001461 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001462 adapter->aq_required = 0;
1463 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001464 goto watchdog_done;
1465 }
1466
1467 /* Process admin queue tasks. After init, everything gets done
1468 * here so we don't race on the admin queue.
1469 */
Mitch Williamsed636962015-04-07 19:45:32 -04001470 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001471 if (!i40evf_asq_done(hw)) {
1472 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1473 i40evf_send_api_ver(adapter);
1474 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001475 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001476 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001477 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1478 i40evf_send_vf_config_msg(adapter);
1479 goto watchdog_done;
1480 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001481
Mitch Williamse284fc82015-03-27 00:12:09 -07001482 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1483 i40evf_disable_queues(adapter);
1484 goto watchdog_done;
1485 }
1486
Greg Rose5eae00c2013-12-21 06:12:45 +00001487 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1488 i40evf_map_queues(adapter);
1489 goto watchdog_done;
1490 }
1491
1492 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1493 i40evf_add_ether_addrs(adapter);
1494 goto watchdog_done;
1495 }
1496
1497 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1498 i40evf_add_vlans(adapter);
1499 goto watchdog_done;
1500 }
1501
1502 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1503 i40evf_del_ether_addrs(adapter);
1504 goto watchdog_done;
1505 }
1506
1507 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1508 i40evf_del_vlans(adapter);
1509 goto watchdog_done;
1510 }
1511
Greg Rose5eae00c2013-12-21 06:12:45 +00001512 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1513 i40evf_configure_queues(adapter);
1514 goto watchdog_done;
1515 }
1516
1517 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1518 i40evf_enable_queues(adapter);
1519 goto watchdog_done;
1520 }
1521
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001522 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1523 /* This message goes straight to the firmware, not the
1524 * PF, so we don't have to set current_op as we will
1525 * not get a response through the ARQ.
1526 */
1527 i40evf_configure_rss(adapter);
1528 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1529 goto watchdog_done;
1530 }
1531
Greg Rose5eae00c2013-12-21 06:12:45 +00001532 if (adapter->state == __I40EVF_RUNNING)
1533 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001534watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001535 if (adapter->state == __I40EVF_RUNNING) {
1536 i40evf_irq_enable_queues(adapter, ~0);
1537 i40evf_fire_sw_int(adapter, 0xFF);
1538 } else {
1539 i40evf_fire_sw_int(adapter, 0x1);
1540 }
1541
Mitch Williamsef8693e2014-02-13 03:48:53 -08001542 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1543restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001544 if (adapter->state == __I40EVF_REMOVE)
1545 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001546 if (adapter->aq_required)
1547 mod_timer(&adapter->watchdog_timer,
1548 jiffies + msecs_to_jiffies(20));
1549 else
1550 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001551 schedule_work(&adapter->adminq_task);
1552}
1553
Mitch Williams67c818a2015-06-19 08:56:30 -07001554#define I40EVF_RESET_WAIT_MS 10
1555#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001556/**
1557 * i40evf_reset_task - Call-back task to handle hardware reset
1558 * @work: pointer to work_struct
1559 *
1560 * During reset we need to shut down and reinitialize the admin queue
1561 * before we can use it to communicate with the PF again. We also clear
1562 * and reinit the rings because that context is lost as well.
1563 **/
1564static void i40evf_reset_task(struct work_struct *work)
1565{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001566 struct i40evf_adapter *adapter = container_of(work,
1567 struct i40evf_adapter,
1568 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001569 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001570 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001571 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001572 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001573 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001574
1575 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1576 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001577 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001578
Mitch Williams67c818a2015-06-19 08:56:30 -07001579 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001580 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001581 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1582 /* Restart the AQ here. If we have been reset but didn't
1583 * detect it, or if the PF had to reinit, our AQ will be hosed.
1584 */
1585 i40evf_shutdown_adminq(hw);
1586 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001587 i40evf_request_reset(adapter);
1588 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001589 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001590
Mitch Williamsef8693e2014-02-13 03:48:53 -08001591 /* poll until we see the reset actually happen */
1592 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001593 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1594 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1595 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001596 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001597 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001598 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001599 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001600 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001601 goto continue_reset; /* act like the reset happened */
1602 }
1603
1604 /* wait until the reset is complete and the PF is responding to us */
1605 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001606 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1607 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1608 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001609 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001610 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001611 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001612 /* extra wait to make sure minimum wait is met */
1613 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001614 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams6ba36a22014-08-01 13:27:14 -07001615 struct i40evf_mac_filter *f, *ftmp;
1616 struct i40evf_vlan_filter *fv, *fvtmp;
1617
Greg Rose5eae00c2013-12-21 06:12:45 +00001618 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001619 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001620 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001621 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1622
Mitch Williams169f4072014-04-01 07:11:50 +00001623 if (netif_running(adapter->netdev)) {
1624 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001625 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001626 netif_tx_disable(netdev);
1627 i40evf_napi_disable_all(adapter);
1628 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001629 i40evf_free_traffic_irqs(adapter);
1630 i40evf_free_all_tx_resources(adapter);
1631 i40evf_free_all_rx_resources(adapter);
1632 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001633
1634 /* Delete all of the filters, both MAC and VLAN. */
1635 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1636 list) {
1637 list_del(&f->list);
1638 kfree(f);
1639 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001640
Mitch Williams6ba36a22014-08-01 13:27:14 -07001641 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1642 list) {
1643 list_del(&fv->list);
1644 kfree(fv);
1645 }
1646
Mitch Williamsef8693e2014-02-13 03:48:53 -08001647 i40evf_free_misc_irq(adapter);
1648 i40evf_reset_interrupt_capability(adapter);
1649 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001650 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001651 kfree(adapter->vf_res);
1652 i40evf_shutdown_adminq(hw);
1653 adapter->netdev->flags &= ~IFF_UP;
1654 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001655 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1656 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001657 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001658 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001659
1660continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001661 if (netif_running(adapter->netdev)) {
1662 netif_carrier_off(netdev);
1663 netif_tx_stop_all_queues(netdev);
1664 i40evf_napi_disable_all(adapter);
1665 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001666 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001667
Greg Rose5eae00c2013-12-21 06:12:45 +00001668 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001669 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1670
1671 /* free the Tx/Rx rings and descriptors, might be better to just
1672 * re-use them sometime in the future
1673 */
1674 i40evf_free_all_rx_resources(adapter);
1675 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001676
1677 /* kill and reinit the admin queue */
1678 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001679 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1680 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001681 err = i40evf_init_adminq(hw);
1682 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001683 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1684 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001685
Mitch Williamse6d038d2015-06-04 16:23:58 -04001686 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1687 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001688
1689 /* re-add all MAC filters */
1690 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1691 f->add = true;
1692 }
1693 /* re-add all VLAN filters */
1694 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1695 f->add = true;
1696 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001697 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001698 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001699 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001700 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001701
1702 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1703
1704 if (netif_running(adapter->netdev)) {
1705 /* allocate transmit descriptors */
1706 err = i40evf_setup_all_tx_resources(adapter);
1707 if (err)
1708 goto reset_err;
1709
1710 /* allocate receive descriptors */
1711 err = i40evf_setup_all_rx_resources(adapter);
1712 if (err)
1713 goto reset_err;
1714
1715 i40evf_configure(adapter);
1716
1717 err = i40evf_up_complete(adapter);
1718 if (err)
1719 goto reset_err;
1720
1721 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001722 } else {
1723 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001724 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001725
Greg Rose5eae00c2013-12-21 06:12:45 +00001726 return;
1727reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001728 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001729 i40evf_close(adapter->netdev);
1730}
1731
1732/**
1733 * i40evf_adminq_task - worker thread to clean the admin queue
1734 * @work: pointer to work_struct containing our data
1735 **/
1736static void i40evf_adminq_task(struct work_struct *work)
1737{
1738 struct i40evf_adapter *adapter =
1739 container_of(work, struct i40evf_adapter, adminq_task);
1740 struct i40e_hw *hw = &adapter->hw;
1741 struct i40e_arq_event_info event;
1742 struct i40e_virtchnl_msg *v_msg;
1743 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001744 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001745 u16 pending;
1746
Mitch Williamsef8693e2014-02-13 03:48:53 -08001747 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001748 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001749
Mitch Williams1001dc32014-11-11 20:02:19 +00001750 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1751 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001752 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001753 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001754
Greg Rose5eae00c2013-12-21 06:12:45 +00001755 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1756 do {
1757 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001758 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001759 break; /* No event to process or error cleaning ARQ */
1760
1761 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1762 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001763 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001764 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001765 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001766 } while (pending);
1767
Mitch Williams67c818a2015-06-19 08:56:30 -07001768 if ((adapter->flags &
1769 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1770 adapter->state == __I40EVF_RESETTING)
1771 goto freedom;
1772
Mitch Williams912257e2014-05-22 06:32:07 +00001773 /* check for error indications */
1774 val = rd32(hw, hw->aq.arq.len);
1775 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001776 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001777 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001778 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001779 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001780 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001781 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001782 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001783 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001784 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001785 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001786 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001787 }
1788 if (oldval != val)
1789 wr32(hw, hw->aq.arq.len, val);
1790
1791 val = rd32(hw, hw->aq.asq.len);
1792 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001793 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001794 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001795 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001796 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001797 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001798 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001799 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001800 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001801 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001802 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001803 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001804 }
1805 if (oldval != val)
1806 wr32(hw, hw->aq.asq.len, val);
1807
Mitch Williams67c818a2015-06-19 08:56:30 -07001808freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001809 kfree(event.msg_buf);
1810out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001811 /* re-enable Admin queue interrupt cause */
1812 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001813}
1814
1815/**
1816 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1817 * @adapter: board private structure
1818 *
1819 * Free all transmit software resources
1820 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001821void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001822{
1823 int i;
1824
Mitch Williamscc052922014-10-25 03:24:34 +00001825 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001826 if (adapter->tx_rings[i]->desc)
1827 i40evf_free_tx_resources(adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001828}
1829
1830/**
1831 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1832 * @adapter: board private structure
1833 *
1834 * If this function returns with an error, then it's possible one or
1835 * more of the rings is populated (while the rest are not). It is the
1836 * callers duty to clean those orphaned rings.
1837 *
1838 * Return 0 on success, negative on failure
1839 **/
1840static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1841{
1842 int i, err = 0;
1843
Mitch Williamscc052922014-10-25 03:24:34 +00001844 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001845 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001846 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1847 if (!err)
1848 continue;
1849 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001850 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001851 break;
1852 }
1853
1854 return err;
1855}
1856
1857/**
1858 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1859 * @adapter: board private structure
1860 *
1861 * If this function returns with an error, then it's possible one or
1862 * more of the rings is populated (while the rest are not). It is the
1863 * callers duty to clean those orphaned rings.
1864 *
1865 * Return 0 on success, negative on failure
1866 **/
1867static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1868{
1869 int i, err = 0;
1870
Mitch Williamscc052922014-10-25 03:24:34 +00001871 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001872 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001873 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1874 if (!err)
1875 continue;
1876 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001877 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001878 break;
1879 }
1880 return err;
1881}
1882
1883/**
1884 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1885 * @adapter: board private structure
1886 *
1887 * Free all receive software resources
1888 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001889void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001890{
1891 int i;
1892
Mitch Williamscc052922014-10-25 03:24:34 +00001893 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001894 if (adapter->rx_rings[i]->desc)
1895 i40evf_free_rx_resources(adapter->rx_rings[i]);
1896}
1897
1898/**
1899 * i40evf_open - Called when a network interface is made active
1900 * @netdev: network interface device structure
1901 *
1902 * Returns 0 on success, negative value on failure
1903 *
1904 * The open entry point is called when a network interface is made
1905 * active by the system (IFF_UP). At this point all resources needed
1906 * for transmit and receive operations are allocated, the interrupt
1907 * handler is registered with the OS, the watchdog timer is started,
1908 * and the stack is notified that the interface is ready.
1909 **/
1910static int i40evf_open(struct net_device *netdev)
1911{
1912 struct i40evf_adapter *adapter = netdev_priv(netdev);
1913 int err;
1914
Mitch Williamsef8693e2014-02-13 03:48:53 -08001915 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1916 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1917 return -EIO;
1918 }
Mitch Williamse284fc82015-03-27 00:12:09 -07001919 if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
Greg Rose5eae00c2013-12-21 06:12:45 +00001920 return -EBUSY;
1921
1922 /* allocate transmit descriptors */
1923 err = i40evf_setup_all_tx_resources(adapter);
1924 if (err)
1925 goto err_setup_tx;
1926
1927 /* allocate receive descriptors */
1928 err = i40evf_setup_all_rx_resources(adapter);
1929 if (err)
1930 goto err_setup_rx;
1931
1932 /* clear any pending interrupts, may auto mask */
1933 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1934 if (err)
1935 goto err_req_irq;
1936
Mitch Williams44151cd2015-04-27 14:57:17 -04001937 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00001938 i40evf_configure(adapter);
1939
1940 err = i40evf_up_complete(adapter);
1941 if (err)
1942 goto err_req_irq;
1943
1944 i40evf_irq_enable(adapter, true);
1945
1946 return 0;
1947
1948err_req_irq:
1949 i40evf_down(adapter);
1950 i40evf_free_traffic_irqs(adapter);
1951err_setup_rx:
1952 i40evf_free_all_rx_resources(adapter);
1953err_setup_tx:
1954 i40evf_free_all_tx_resources(adapter);
1955
1956 return err;
1957}
1958
1959/**
1960 * i40evf_close - Disables a network interface
1961 * @netdev: network interface device structure
1962 *
1963 * Returns 0, this is not allowed to fail
1964 *
1965 * The close entry point is called when an interface is de-activated
1966 * by the OS. The hardware is still under the drivers control, but
1967 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1968 * are freed, along with all transmit and receive resources.
1969 **/
1970static int i40evf_close(struct net_device *netdev)
1971{
1972 struct i40evf_adapter *adapter = netdev_priv(netdev);
1973
Mitch Williamsef8693e2014-02-13 03:48:53 -08001974 if (adapter->state <= __I40EVF_DOWN)
1975 return 0;
1976
Mitch Williamsef8693e2014-02-13 03:48:53 -08001977
Greg Rose5eae00c2013-12-21 06:12:45 +00001978 set_bit(__I40E_DOWN, &adapter->vsi.state);
1979
1980 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001981 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001982 i40evf_free_traffic_irqs(adapter);
1983
Greg Rose5eae00c2013-12-21 06:12:45 +00001984 return 0;
1985}
1986
1987/**
1988 * i40evf_get_stats - Get System Network Statistics
1989 * @netdev: network interface device structure
1990 *
1991 * Returns the address of the device statistics structure.
1992 * The statistics are actually updated from the timer callback.
1993 **/
1994static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1995{
1996 struct i40evf_adapter *adapter = netdev_priv(netdev);
1997
1998 /* only return the current stats */
1999 return &adapter->net_stats;
2000}
2001
2002/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002003 * i40evf_change_mtu - Change the Maximum Transfer Unit
2004 * @netdev: network interface device structure
2005 * @new_mtu: new value for maximum frame size
2006 *
2007 * Returns 0 on success, negative on failure
2008 **/
2009static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2010{
2011 struct i40evf_adapter *adapter = netdev_priv(netdev);
2012 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2013
2014 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2015 return -EINVAL;
2016
Greg Rose5eae00c2013-12-21 06:12:45 +00002017 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002018 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2019 schedule_work(&adapter->reset_task);
2020
Greg Rose5eae00c2013-12-21 06:12:45 +00002021 return 0;
2022}
2023
2024static const struct net_device_ops i40evf_netdev_ops = {
2025 .ndo_open = i40evf_open,
2026 .ndo_stop = i40evf_close,
2027 .ndo_start_xmit = i40evf_xmit_frame,
2028 .ndo_get_stats = i40evf_get_stats,
2029 .ndo_set_rx_mode = i40evf_set_rx_mode,
2030 .ndo_validate_addr = eth_validate_addr,
2031 .ndo_set_mac_address = i40evf_set_mac,
2032 .ndo_change_mtu = i40evf_change_mtu,
2033 .ndo_tx_timeout = i40evf_tx_timeout,
2034 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2035 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
2036};
2037
2038/**
2039 * i40evf_check_reset_complete - check that VF reset is complete
2040 * @hw: pointer to hw struct
2041 *
2042 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2043 **/
2044static int i40evf_check_reset_complete(struct i40e_hw *hw)
2045{
2046 u32 rstat;
2047 int i;
2048
2049 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002050 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2051 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2052 if ((rstat == I40E_VFR_VFACTIVE) ||
2053 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002054 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002055 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002056 }
2057 return -EBUSY;
2058}
2059
2060/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002061 * i40evf_process_config - Process the config information we got from the PF
2062 * @adapter: board private structure
2063 *
2064 * Verify that we have a valid config struct, and set up our netdev features
2065 * and our VSI struct.
2066 **/
2067int i40evf_process_config(struct i40evf_adapter *adapter)
2068{
2069 struct net_device *netdev = adapter->netdev;
2070 int i;
2071
2072 /* got VF config message back from PF, now we can parse it */
2073 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2074 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2075 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2076 }
2077 if (!adapter->vsi_res) {
2078 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2079 return -ENODEV;
2080 }
2081
2082 if (adapter->vf_res->vf_offload_flags
2083 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2084 netdev->vlan_features = netdev->features;
2085 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2086 NETIF_F_HW_VLAN_CTAG_RX |
2087 NETIF_F_HW_VLAN_CTAG_FILTER;
2088 }
2089 netdev->features |= NETIF_F_HIGHDMA |
2090 NETIF_F_SG |
2091 NETIF_F_IP_CSUM |
2092 NETIF_F_SCTP_CSUM |
2093 NETIF_F_IPV6_CSUM |
2094 NETIF_F_TSO |
2095 NETIF_F_TSO6 |
2096 NETIF_F_RXCSUM |
2097 NETIF_F_GRO;
2098
2099 /* copy netdev features into list of user selectable features */
2100 netdev->hw_features |= netdev->features;
2101 netdev->hw_features &= ~NETIF_F_RXCSUM;
2102
2103 adapter->vsi.id = adapter->vsi_res->vsi_id;
2104
2105 adapter->vsi.back = adapter;
2106 adapter->vsi.base_vector = 1;
2107 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2108 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2109 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2110 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2111 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2112 adapter->vsi.netdev = adapter->netdev;
2113 return 0;
2114}
2115
2116/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002117 * i40evf_init_task - worker thread to perform delayed initialization
2118 * @work: pointer to work_struct containing our data
2119 *
2120 * This task completes the work that was begun in probe. Due to the nature
2121 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002122 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002123 * whole system, we'll do it in a task so we can sleep.
2124 * This task only runs during driver init. Once we've established
2125 * communications with the PF driver and set up our netdev, the watchdog
2126 * takes over.
2127 **/
2128static void i40evf_init_task(struct work_struct *work)
2129{
2130 struct i40evf_adapter *adapter = container_of(work,
2131 struct i40evf_adapter,
2132 init_task.work);
2133 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002134 struct i40e_hw *hw = &adapter->hw;
2135 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002136 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002137
2138 switch (adapter->state) {
2139 case __I40EVF_STARTUP:
2140 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002141 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2142 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002143 err = i40e_set_mac_type(hw);
2144 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002145 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2146 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002147 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002148 }
2149 err = i40evf_check_reset_complete(hw);
2150 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002151 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002152 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002153 goto err;
2154 }
2155 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2156 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2157 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2158 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2159
2160 err = i40evf_init_adminq(hw);
2161 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002162 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2163 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002164 goto err;
2165 }
2166 err = i40evf_send_api_ver(adapter);
2167 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002168 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002169 i40evf_shutdown_adminq(hw);
2170 goto err;
2171 }
2172 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2173 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002174 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002175 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002176 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002177 i40evf_shutdown_adminq(hw);
2178 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002179 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002180 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002181
2182 /* aq msg sent, awaiting reply */
2183 err = i40evf_verify_api_ver(adapter);
2184 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002185 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002186 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002187 else
2188 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2189 adapter->pf_version.major,
2190 adapter->pf_version.minor,
2191 I40E_VIRTCHNL_VERSION_MAJOR,
2192 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002193 goto err;
2194 }
2195 err = i40evf_send_vf_config_msg(adapter);
2196 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002197 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002198 err);
2199 goto err;
2200 }
2201 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2202 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002203 case __I40EVF_INIT_GET_RESOURCES:
2204 /* aq msg sent, awaiting reply */
2205 if (!adapter->vf_res) {
2206 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2207 (I40E_MAX_VF_VSI *
2208 sizeof(struct i40e_virtchnl_vsi_resource));
2209 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002210 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002211 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002212 }
2213 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002214 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002215 err = i40evf_send_vf_config_msg(adapter);
2216 goto err;
2217 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002218 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002219 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2220 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002221 goto err_alloc;
2222 }
2223 adapter->state = __I40EVF_INIT_SW;
2224 break;
2225 default:
2226 goto err_alloc;
2227 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04002228 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002229 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002230 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002231
2232 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2233
Greg Rose5eae00c2013-12-21 06:12:45 +00002234 netdev->netdev_ops = &i40evf_netdev_ops;
2235 i40evf_set_ethtool_ops(netdev);
2236 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002237
Greg Rose5eae00c2013-12-21 06:12:45 +00002238 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002239 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002240 adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002241 random_ether_addr(adapter->hw.mac.addr);
2242 }
Greg Rose9a173902014-05-22 06:32:02 +00002243 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2244 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002245
Greg Rose5eae00c2013-12-21 06:12:45 +00002246 init_timer(&adapter->watchdog_timer);
2247 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2248 adapter->watchdog_timer.data = (unsigned long)adapter;
2249 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2250
Mitch Williamscc052922014-10-25 03:24:34 +00002251 adapter->num_active_queues = min_t(int,
2252 adapter->vsi_res->num_queue_pairs,
2253 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002254 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2255 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002256 err = i40evf_init_interrupt_scheme(adapter);
2257 if (err)
2258 goto err_sw_init;
2259 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002260 if (!RSS_AQ(adapter))
2261 i40evf_configure_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002262 err = i40evf_request_misc_irq(adapter);
2263 if (err)
2264 goto err_sw_init;
2265
2266 netif_carrier_off(netdev);
2267
Mitch Williamsef8693e2014-02-13 03:48:53 -08002268 if (!adapter->netdev_registered) {
2269 err = register_netdev(netdev);
2270 if (err)
2271 goto err_register;
2272 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002273
2274 adapter->netdev_registered = true;
2275
2276 netif_tx_stop_all_queues(netdev);
2277
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002278 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002279 if (netdev->features & NETIF_F_GRO)
2280 dev_info(&pdev->dev, "GRO is enabled\n");
2281
2282 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2283 adapter->state = __I40EVF_DOWN;
2284 set_bit(__I40E_DOWN, &adapter->vsi.state);
2285 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002286
2287 if (RSS_AQ(adapter)) {
2288 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2289 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2290 } else {
2291 i40evf_configure_rss(adapter);
2292 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002293 return;
2294restart:
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002295 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002296 return;
2297
2298err_register:
2299 i40evf_free_misc_irq(adapter);
2300err_sw_init:
2301 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002302err_alloc:
2303 kfree(adapter->vf_res);
2304 adapter->vf_res = NULL;
2305err:
2306 /* Things went into the weeds, so try again later */
2307 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002308 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002309 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002310 return; /* do not reschedule */
2311 }
2312 schedule_delayed_work(&adapter->init_task, HZ * 3);
Greg Rose5eae00c2013-12-21 06:12:45 +00002313}
2314
2315/**
2316 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2317 * @pdev: pci device structure
2318 **/
2319static void i40evf_shutdown(struct pci_dev *pdev)
2320{
2321 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002322 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002323
2324 netif_device_detach(netdev);
2325
2326 if (netif_running(netdev))
2327 i40evf_close(netdev);
2328
Mitch Williams00293fd2015-01-09 11:18:18 +00002329 /* Prevent the watchdog from running. */
2330 adapter->state = __I40EVF_REMOVE;
2331 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002332
Greg Rose5eae00c2013-12-21 06:12:45 +00002333#ifdef CONFIG_PM
2334 pci_save_state(pdev);
2335
2336#endif
2337 pci_disable_device(pdev);
2338}
2339
2340/**
2341 * i40evf_probe - Device Initialization Routine
2342 * @pdev: PCI device information struct
2343 * @ent: entry in i40evf_pci_tbl
2344 *
2345 * Returns 0 on success, negative on failure
2346 *
2347 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2348 * The OS initialization, configuring of the adapter private structure,
2349 * and a hardware reset occur.
2350 **/
2351static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2352{
2353 struct net_device *netdev;
2354 struct i40evf_adapter *adapter = NULL;
2355 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002356 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002357
2358 err = pci_enable_device(pdev);
2359 if (err)
2360 return err;
2361
Mitch Williams64942942014-02-11 08:26:33 +00002362 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002363 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002364 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2365 if (err) {
2366 dev_err(&pdev->dev,
2367 "DMA configuration failed: 0x%x\n", err);
2368 goto err_dma;
2369 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002370 }
2371
2372 err = pci_request_regions(pdev, i40evf_driver_name);
2373 if (err) {
2374 dev_err(&pdev->dev,
2375 "pci_request_regions failed 0x%x\n", err);
2376 goto err_pci_reg;
2377 }
2378
2379 pci_enable_pcie_error_reporting(pdev);
2380
2381 pci_set_master(pdev);
2382
2383 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2384 MAX_TX_QUEUES);
2385 if (!netdev) {
2386 err = -ENOMEM;
2387 goto err_alloc_etherdev;
2388 }
2389
2390 SET_NETDEV_DEV(netdev, &pdev->dev);
2391
2392 pci_set_drvdata(pdev, netdev);
2393 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002394
2395 adapter->netdev = netdev;
2396 adapter->pdev = pdev;
2397
2398 hw = &adapter->hw;
2399 hw->back = adapter;
2400
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002401 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002402 adapter->state = __I40EVF_STARTUP;
2403
2404 /* Call save state here because it relies on the adapter struct. */
2405 pci_save_state(pdev);
2406
2407 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2408 pci_resource_len(pdev, 0));
2409 if (!hw->hw_addr) {
2410 err = -EIO;
2411 goto err_ioremap;
2412 }
2413 hw->vendor_id = pdev->vendor;
2414 hw->device_id = pdev->device;
2415 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2416 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2417 hw->subsystem_device_id = pdev->subsystem_device;
2418 hw->bus.device = PCI_SLOT(pdev->devfn);
2419 hw->bus.func = PCI_FUNC(pdev->devfn);
2420
Serey Kong8bb1a542014-08-01 13:27:15 -07002421 INIT_LIST_HEAD(&adapter->mac_filter_list);
2422 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2423
Greg Rose5eae00c2013-12-21 06:12:45 +00002424 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2425 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2426 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2427 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002428 schedule_delayed_work(&adapter->init_task,
2429 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002430
2431 return 0;
2432
2433err_ioremap:
2434 free_netdev(netdev);
2435err_alloc_etherdev:
2436 pci_release_regions(pdev);
2437err_pci_reg:
2438err_dma:
2439 pci_disable_device(pdev);
2440 return err;
2441}
2442
2443#ifdef CONFIG_PM
2444/**
2445 * i40evf_suspend - Power management suspend routine
2446 * @pdev: PCI device information struct
2447 * @state: unused
2448 *
2449 * Called when the system (VM) is entering sleep/suspend.
2450 **/
2451static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2452{
2453 struct net_device *netdev = pci_get_drvdata(pdev);
2454 struct i40evf_adapter *adapter = netdev_priv(netdev);
2455 int retval = 0;
2456
2457 netif_device_detach(netdev);
2458
2459 if (netif_running(netdev)) {
2460 rtnl_lock();
2461 i40evf_down(adapter);
2462 rtnl_unlock();
2463 }
2464 i40evf_free_misc_irq(adapter);
2465 i40evf_reset_interrupt_capability(adapter);
2466
2467 retval = pci_save_state(pdev);
2468 if (retval)
2469 return retval;
2470
2471 pci_disable_device(pdev);
2472
2473 return 0;
2474}
2475
2476/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002477 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002478 * @pdev: PCI device information struct
2479 *
2480 * Called when the system (VM) is resumed from sleep/suspend.
2481 **/
2482static int i40evf_resume(struct pci_dev *pdev)
2483{
2484 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2485 struct net_device *netdev = adapter->netdev;
2486 u32 err;
2487
2488 pci_set_power_state(pdev, PCI_D0);
2489 pci_restore_state(pdev);
2490 /* pci_restore_state clears dev->state_saved so call
2491 * pci_save_state to restore it.
2492 */
2493 pci_save_state(pdev);
2494
2495 err = pci_enable_device_mem(pdev);
2496 if (err) {
2497 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2498 return err;
2499 }
2500 pci_set_master(pdev);
2501
2502 rtnl_lock();
2503 err = i40evf_set_interrupt_capability(adapter);
2504 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002505 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002506 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2507 return err;
2508 }
2509 err = i40evf_request_misc_irq(adapter);
2510 rtnl_unlock();
2511 if (err) {
2512 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2513 return err;
2514 }
2515
2516 schedule_work(&adapter->reset_task);
2517
2518 netif_device_attach(netdev);
2519
2520 return err;
2521}
2522
2523#endif /* CONFIG_PM */
2524/**
2525 * i40evf_remove - Device Removal Routine
2526 * @pdev: PCI device information struct
2527 *
2528 * i40evf_remove is called by the PCI subsystem to alert the driver
2529 * that it should release a PCI device. The could be caused by a
2530 * Hot-Plug event, or because the driver is going to be removed from
2531 * memory.
2532 **/
2533static void i40evf_remove(struct pci_dev *pdev)
2534{
2535 struct net_device *netdev = pci_get_drvdata(pdev);
2536 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002537 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002538 struct i40e_hw *hw = &adapter->hw;
2539
2540 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002541 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002542
2543 if (adapter->netdev_registered) {
2544 unregister_netdev(netdev);
2545 adapter->netdev_registered = false;
2546 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002547
Mitch Williamsf4a71882015-01-09 11:18:16 +00002548 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002549 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002550 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002551 i40evf_request_reset(adapter);
2552 msleep(20);
2553 /* If the FW isn't responding, kick it once, but only once. */
2554 if (!i40evf_asq_done(hw)) {
2555 i40evf_request_reset(adapter);
2556 msleep(20);
2557 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002558
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002559 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002560 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002561 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002562 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002563 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002564 }
2565
Mitch Williamse5d17c32014-06-04 04:22:38 +00002566 if (adapter->watchdog_timer.function)
2567 del_timer_sync(&adapter->watchdog_timer);
2568
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002569 flush_scheduled_work();
2570
Greg Rose5eae00c2013-12-21 06:12:45 +00002571 if (hw->aq.asq.count)
2572 i40evf_shutdown_adminq(hw);
2573
2574 iounmap(hw->hw_addr);
2575 pci_release_regions(pdev);
2576
Mitch Williamse284fc82015-03-27 00:12:09 -07002577 i40evf_free_all_tx_resources(adapter);
2578 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002579 i40evf_free_queues(adapter);
2580 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002581 /* If we got removed before an up/down sequence, we've got a filter
2582 * hanging out there that we need to get rid of.
2583 */
2584 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2585 list_del(&f->list);
2586 kfree(f);
2587 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002588 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2589 list_del(&f->list);
2590 kfree(f);
2591 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002592
2593 free_netdev(netdev);
2594
2595 pci_disable_pcie_error_reporting(pdev);
2596
2597 pci_disable_device(pdev);
2598}
2599
2600static struct pci_driver i40evf_driver = {
2601 .name = i40evf_driver_name,
2602 .id_table = i40evf_pci_tbl,
2603 .probe = i40evf_probe,
2604 .remove = i40evf_remove,
2605#ifdef CONFIG_PM
2606 .suspend = i40evf_suspend,
2607 .resume = i40evf_resume,
2608#endif
2609 .shutdown = i40evf_shutdown,
2610};
2611
2612/**
2613 * i40e_init_module - Driver Registration Routine
2614 *
2615 * i40e_init_module is the first routine called when the driver is
2616 * loaded. All it does is register with the PCI subsystem.
2617 **/
2618static int __init i40evf_init_module(void)
2619{
2620 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002621
Greg Rose5eae00c2013-12-21 06:12:45 +00002622 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002623 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002624
2625 pr_info("%s\n", i40evf_copyright);
2626
2627 ret = pci_register_driver(&i40evf_driver);
2628 return ret;
2629}
2630
2631module_init(i40evf_init_module);
2632
2633/**
2634 * i40e_exit_module - Driver Exit Cleanup Routine
2635 *
2636 * i40e_exit_module is called just before the driver is removed
2637 * from memory.
2638 **/
2639static void __exit i40evf_exit_module(void)
2640{
2641 pci_unregister_driver(&i40evf_driver);
2642}
2643
2644module_exit(i40evf_exit_module);
2645
2646/* i40evf_main.c */