blob: d1c4335114fc625db2fb78bb8c504fbb5069c71a [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
Mitch Williams69ebe9552015-11-19 11:34:24 -080037#define DRV_KERN "-k"
38
39#define DRV_VERSION_MAJOR 1
40#define DRV_VERSION_MINOR 4
Catherine Sullivanc9c9f1b2015-11-19 11:34:25 -080041#define DRV_VERSION_BUILD 4
Mitch Williams69ebe9552015-11-19 11:34:24 -080042#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
43 __stringify(DRV_VERSION_MINOR) "." \
44 __stringify(DRV_VERSION_BUILD) \
45 DRV_KERN
Greg Rose5eae00c2013-12-21 06:12:45 +000046const char i40evf_driver_version[] = DRV_VERSION;
47static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040048 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000049
50/* i40evf_pci_tbl - PCI Device ID Table
51 *
52 * Wildcard entries (PCI_ANY_ID) should come last
53 * Last entry must be all 0s
54 *
55 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
56 * Class, Class Mask, private data (not used) }
57 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020058static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080059 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040060 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000061 /* required last entry */
62 {0, }
63};
64
65MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
66
67MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
68MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
69MODULE_LICENSE("GPL");
70MODULE_VERSION(DRV_VERSION);
71
72/**
73 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
74 * @hw: pointer to the HW structure
75 * @mem: ptr to mem struct to fill out
76 * @size: size of memory requested
77 * @alignment: what to align the allocation to
78 **/
79i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
80 struct i40e_dma_mem *mem,
81 u64 size, u32 alignment)
82{
83 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
84
85 if (!mem)
86 return I40E_ERR_PARAM;
87
88 mem->size = ALIGN(size, alignment);
89 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
90 (dma_addr_t *)&mem->pa, GFP_KERNEL);
91 if (mem->va)
92 return 0;
93 else
94 return I40E_ERR_NO_MEMORY;
95}
96
97/**
98 * i40evf_free_dma_mem_d - OS specific memory free for shared code
99 * @hw: pointer to the HW structure
100 * @mem: ptr to mem struct to free
101 **/
102i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
103{
104 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
105
106 if (!mem || !mem->va)
107 return I40E_ERR_PARAM;
108 dma_free_coherent(&adapter->pdev->dev, mem->size,
109 mem->va, (dma_addr_t)mem->pa);
110 return 0;
111}
112
113/**
114 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
115 * @hw: pointer to the HW structure
116 * @mem: ptr to mem struct to fill out
117 * @size: size of memory requested
118 **/
119i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
120 struct i40e_virt_mem *mem, u32 size)
121{
122 if (!mem)
123 return I40E_ERR_PARAM;
124
125 mem->size = size;
126 mem->va = kzalloc(size, GFP_KERNEL);
127
128 if (mem->va)
129 return 0;
130 else
131 return I40E_ERR_NO_MEMORY;
132}
133
134/**
135 * i40evf_free_virt_mem_d - OS specific memory free for shared code
136 * @hw: pointer to the HW structure
137 * @mem: ptr to mem struct to free
138 **/
139i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
140 struct i40e_virt_mem *mem)
141{
142 if (!mem)
143 return I40E_ERR_PARAM;
144
145 /* it's ok to kfree a NULL pointer */
146 kfree(mem->va);
147
148 return 0;
149}
150
151/**
152 * i40evf_debug_d - OS dependent version of debug printing
153 * @hw: pointer to the HW structure
154 * @mask: debug level mask
155 * @fmt_str: printf-type format description
156 **/
157void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
158{
159 char buf[512];
160 va_list argptr;
161
162 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
163 return;
164
165 va_start(argptr, fmt_str);
166 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
167 va_end(argptr);
168
169 /* the debug string is already formatted with a newline */
170 pr_info("%s", buf);
171}
172
173/**
174 * i40evf_tx_timeout - Respond to a Tx Hang
175 * @netdev: network interface device structure
176 **/
177static void i40evf_tx_timeout(struct net_device *netdev)
178{
179 struct i40evf_adapter *adapter = netdev_priv(netdev);
180
181 adapter->tx_timeout_count++;
Mitch Williams67c818a2015-06-19 08:56:30 -0700182 if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
183 I40EVF_FLAG_RESET_NEEDED))) {
Mitch Williams3526d802014-03-06 08:59:56 +0000184 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
Mitch Williams625777e2014-02-20 19:29:05 -0800185 schedule_work(&adapter->reset_task);
186 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000187}
188
189/**
190 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
191 * @adapter: board private structure
192 **/
193static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
194{
195 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000196
Greg Rose5eae00c2013-12-21 06:12:45 +0000197 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
198
199 /* read flush */
200 rd32(hw, I40E_VFGEN_RSTAT);
201
202 synchronize_irq(adapter->msix_entries[0].vector);
203}
204
205/**
206 * i40evf_misc_irq_enable - Enable default interrupt generation settings
207 * @adapter: board private structure
208 **/
209static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
210{
211 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000212
Greg Rose5eae00c2013-12-21 06:12:45 +0000213 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
214 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400215 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000216
217 /* read flush */
218 rd32(hw, I40E_VFGEN_RSTAT);
219}
220
221/**
222 * i40evf_irq_disable - Mask off interrupt generation on the NIC
223 * @adapter: board private structure
224 **/
225static void i40evf_irq_disable(struct i40evf_adapter *adapter)
226{
227 int i;
228 struct i40e_hw *hw = &adapter->hw;
229
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800230 if (!adapter->msix_entries)
231 return;
232
Greg Rose5eae00c2013-12-21 06:12:45 +0000233 for (i = 1; i < adapter->num_msix_vectors; i++) {
234 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
235 synchronize_irq(adapter->msix_entries[i].vector);
236 }
237 /* read flush */
238 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000239}
240
241/**
242 * i40evf_irq_enable_queues - Enable interrupt for specified queues
243 * @adapter: board private structure
244 * @mask: bitmap of queues to enable
245 **/
246void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
247{
248 struct i40e_hw *hw = &adapter->hw;
249 int i;
250
251 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400252 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000253 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
254 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000255 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400256 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000257 }
258 }
259}
260
261/**
262 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
263 * @adapter: board private structure
264 * @mask: bitmap of vectors to trigger
265 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000266static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000267{
268 struct i40e_hw *hw = &adapter->hw;
269 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800270 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000271
Mitch Williams164ec1b2014-06-04 08:45:19 +0000272 if (mask & 1) {
273 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
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;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000277 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
278 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000279 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400280 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000281 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400282 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000283 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400284 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000285 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
286 }
287 }
288}
289
290/**
291 * i40evf_irq_enable - Enable default interrupt generation settings
292 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600293 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000294 **/
295void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
296{
297 struct i40e_hw *hw = &adapter->hw;
298
Mitch Williams164ec1b2014-06-04 08:45:19 +0000299 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000300 i40evf_irq_enable_queues(adapter, ~0);
301
302 if (flush)
303 rd32(hw, I40E_VFGEN_RSTAT);
304}
305
306/**
307 * i40evf_msix_aq - Interrupt handler for vector 0
308 * @irq: interrupt number
309 * @data: pointer to netdev
310 **/
311static irqreturn_t i40evf_msix_aq(int irq, void *data)
312{
313 struct net_device *netdev = data;
314 struct i40evf_adapter *adapter = netdev_priv(netdev);
315 struct i40e_hw *hw = &adapter->hw;
316 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000317
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700318 /* handle non-queue interrupts, these reads clear the registers */
319 val = rd32(hw, I40E_VFINT_ICR01);
320 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000321
Jean Sacrened17f7e2015-10-13 01:06:30 -0600322 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
323 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000324 wr32(hw, I40E_VFINT_DYN_CTL01, val);
325
Greg Rose5eae00c2013-12-21 06:12:45 +0000326 /* schedule work on the private workqueue */
327 schedule_work(&adapter->adminq_task);
328
329 return IRQ_HANDLED;
330}
331
332/**
333 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
334 * @irq: interrupt number
335 * @data: pointer to a q_vector
336 **/
337static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
338{
339 struct i40e_q_vector *q_vector = data;
340
341 if (!q_vector->tx.ring && !q_vector->rx.ring)
342 return IRQ_HANDLED;
343
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700344 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000345
346 return IRQ_HANDLED;
347}
348
349/**
350 * i40evf_map_vector_to_rxq - associate irqs with rx queues
351 * @adapter: board private structure
352 * @v_idx: interrupt number
353 * @r_idx: queue number
354 **/
355static void
356i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
357{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400358 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400359 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000360
361 rx_ring->q_vector = q_vector;
362 rx_ring->next = q_vector->rx.ring;
363 rx_ring->vsi = &adapter->vsi;
364 q_vector->rx.ring = rx_ring;
365 q_vector->rx.count++;
366 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400367 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000368}
369
370/**
371 * i40evf_map_vector_to_txq - associate irqs with tx queues
372 * @adapter: board private structure
373 * @v_idx: interrupt number
374 * @t_idx: queue number
375 **/
376static void
377i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
378{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400379 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400380 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000381
382 tx_ring->q_vector = q_vector;
383 tx_ring->next = q_vector->tx.ring;
384 tx_ring->vsi = &adapter->vsi;
385 q_vector->tx.ring = tx_ring;
386 q_vector->tx.count++;
387 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400388 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000389 q_vector->num_ringpairs++;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400390 q_vector->ring_mask |= BIT(t_idx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000391}
392
393/**
394 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
395 * @adapter: board private structure to initialize
396 *
397 * This function maps descriptor rings to the queue-specific vectors
398 * we were allotted through the MSI-X enabling code. Ideally, we'd have
399 * one vector per ring/queue, but on a constrained vector budget, we
400 * group the rings as "efficiently" as possible. You would add new
401 * mapping configurations in here.
402 **/
403static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
404{
405 int q_vectors;
406 int v_start = 0;
407 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000408 int rxr_remaining = adapter->num_active_queues;
409 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000410 int i, j;
411 int rqpv, tqpv;
412 int err = 0;
413
414 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
415
416 /* The ideal configuration...
417 * We have enough vectors to map one per queue.
418 */
Mitch Williams973371d2015-04-27 14:57:09 -0400419 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000420 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
421 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
422
423 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
424 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
425 goto out;
426 }
427
428 /* If we don't have enough vectors for a 1-to-1
429 * mapping, we'll have to group them so there are
430 * multiple queues per vector.
431 * Re-adjusting *qpv takes care of the remainder.
432 */
433 for (i = v_start; i < q_vectors; i++) {
434 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
435 for (j = 0; j < rqpv; j++) {
436 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
437 rxr_idx++;
438 rxr_remaining--;
439 }
440 }
441 for (i = v_start; i < q_vectors; i++) {
442 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
443 for (j = 0; j < tqpv; j++) {
444 i40evf_map_vector_to_txq(adapter, i, txr_idx);
445 txr_idx++;
446 txr_remaining--;
447 }
448 }
449
450out:
451 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
452
453 return err;
454}
455
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700456#ifdef CONFIG_NET_POLL_CONTROLLER
457/**
458 * i40evf_netpoll - A Polling 'interrupt' handler
459 * @netdev: network interface device structure
460 *
461 * This is used by netconsole to send skbs without having to re-enable
462 * interrupts. It's not called while the normal interrupt routine is executing.
463 **/
464static void i40evf_netpoll(struct net_device *netdev)
465{
466 struct i40evf_adapter *adapter = netdev_priv(netdev);
467 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
468 int i;
469
470 /* if interface is down do nothing */
471 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
472 return;
473
474 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400475 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700476}
477
478#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000479/**
480 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
481 * @adapter: board private structure
482 *
483 * Allocates MSI-X vectors for tx and rx handling, and requests
484 * interrupts from the kernel.
485 **/
486static int
487i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
488{
489 int vector, err, q_vectors;
490 int rx_int_idx = 0, tx_int_idx = 0;
491
492 i40evf_irq_disable(adapter);
493 /* Decrement for Other and TCP Timer vectors */
494 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
495
496 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400497 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Greg Rose5eae00c2013-12-21 06:12:45 +0000498
499 if (q_vector->tx.ring && q_vector->rx.ring) {
500 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
501 "i40evf-%s-%s-%d", basename,
502 "TxRx", rx_int_idx++);
503 tx_int_idx++;
504 } else if (q_vector->rx.ring) {
505 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
506 "i40evf-%s-%s-%d", basename,
507 "rx", rx_int_idx++);
508 } else if (q_vector->tx.ring) {
509 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
510 "i40evf-%s-%s-%d", basename,
511 "tx", tx_int_idx++);
512 } else {
513 /* skip this unused q_vector */
514 continue;
515 }
516 err = request_irq(
517 adapter->msix_entries[vector + NONQ_VECS].vector,
518 i40evf_msix_clean_rings,
519 0,
520 q_vector->name,
521 q_vector);
522 if (err) {
523 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400524 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000525 goto free_queue_irqs;
526 }
527 /* assign the mask for this irq */
528 irq_set_affinity_hint(
529 adapter->msix_entries[vector + NONQ_VECS].vector,
530 q_vector->affinity_mask);
531 }
532
533 return 0;
534
535free_queue_irqs:
536 while (vector) {
537 vector--;
538 irq_set_affinity_hint(
539 adapter->msix_entries[vector + NONQ_VECS].vector,
540 NULL);
541 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400542 &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000543 }
544 return err;
545}
546
547/**
548 * i40evf_request_misc_irq - Initialize MSI-X interrupts
549 * @adapter: board private structure
550 *
551 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
552 * vector is only for the admin queue, and stays active even when the netdev
553 * is closed.
554 **/
555static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
556{
557 struct net_device *netdev = adapter->netdev;
558 int err;
559
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700560 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000561 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
562 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000563 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800564 &i40evf_msix_aq, 0,
565 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000566 if (err) {
567 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800568 "request_irq for %s failed: %d\n",
569 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000570 free_irq(adapter->msix_entries[0].vector, netdev);
571 }
572 return err;
573}
574
575/**
576 * i40evf_free_traffic_irqs - Free MSI-X interrupts
577 * @adapter: board private structure
578 *
579 * Frees all MSI-X vectors other than 0.
580 **/
581static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
582{
583 int i;
584 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000585
Greg Rose5eae00c2013-12-21 06:12:45 +0000586 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
587
588 for (i = 0; i < q_vectors; i++) {
589 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
590 NULL);
591 free_irq(adapter->msix_entries[i+1].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400592 &adapter->q_vectors[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000593 }
594}
595
596/**
597 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
598 * @adapter: board private structure
599 *
600 * Frees MSI-X vector 0.
601 **/
602static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
603{
604 struct net_device *netdev = adapter->netdev;
605
606 free_irq(adapter->msix_entries[0].vector, netdev);
607}
608
609/**
610 * i40evf_configure_tx - Configure Transmit Unit after Reset
611 * @adapter: board private structure
612 *
613 * Configure the Tx unit of the MAC after a reset.
614 **/
615static void i40evf_configure_tx(struct i40evf_adapter *adapter)
616{
617 struct i40e_hw *hw = &adapter->hw;
618 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000619
Mitch Williamscc052922014-10-25 03:24:34 +0000620 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400621 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000622}
623
624/**
625 * i40evf_configure_rx - Configure Receive Unit after Reset
626 * @adapter: board private structure
627 *
628 * Configure the Rx unit of the MAC after a reset.
629 **/
630static void i40evf_configure_rx(struct i40evf_adapter *adapter)
631{
632 struct i40e_hw *hw = &adapter->hw;
633 struct net_device *netdev = adapter->netdev;
634 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
635 int i;
636 int rx_buf_len;
637
638
639 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
640 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
641
642 /* Decide whether to use packet split mode or not */
643 if (netdev->mtu > ETH_DATA_LEN) {
644 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
645 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
646 else
647 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
648 } else {
649 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
650 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
651 else
652 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
653 }
654
655 /* Set the RX buffer length according to the mode */
656 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
657 rx_buf_len = I40E_RX_HDR_SIZE;
658 } else {
659 if (netdev->mtu <= ETH_DATA_LEN)
660 rx_buf_len = I40EVF_RXBUFFER_2048;
661 else
662 rx_buf_len = ALIGN(max_frame, 1024);
663 }
664
Mitch Williamscc052922014-10-25 03:24:34 +0000665 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400666 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
667 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Greg Rose5eae00c2013-12-21 06:12:45 +0000668 }
669}
670
671/**
672 * i40evf_find_vlan - Search filter list for specific vlan filter
673 * @adapter: board private structure
674 * @vlan: vlan tag
675 *
676 * Returns ptr to the filter object or NULL
677 **/
678static struct
679i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
680{
681 struct i40evf_vlan_filter *f;
682
683 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
684 if (vlan == f->vlan)
685 return f;
686 }
687 return NULL;
688}
689
690/**
691 * i40evf_add_vlan - Add a vlan filter to the list
692 * @adapter: board private structure
693 * @vlan: VLAN tag
694 *
695 * Returns ptr to the filter object or NULL when no memory available.
696 **/
697static struct
698i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
699{
Mitch Williams13acb542015-03-31 00:45:05 -0700700 struct i40evf_vlan_filter *f = NULL;
701 int count = 50;
702
703 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
704 &adapter->crit_section)) {
705 udelay(1);
706 if (--count == 0)
707 goto out;
708 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000709
710 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000711 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000712 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000713 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700714 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000715
Greg Rose5eae00c2013-12-21 06:12:45 +0000716 f->vlan = vlan;
717
718 INIT_LIST_HEAD(&f->list);
719 list_add(&f->list, &adapter->vlan_filter_list);
720 f->add = true;
721 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
722 }
723
Mitch Williams13acb542015-03-31 00:45:05 -0700724clearout:
725 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
726out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000727 return f;
728}
729
730/**
731 * i40evf_del_vlan - Remove a vlan filter from the list
732 * @adapter: board private structure
733 * @vlan: VLAN tag
734 **/
735static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
736{
737 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700738 int count = 50;
739
740 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
741 &adapter->crit_section)) {
742 udelay(1);
743 if (--count == 0)
744 return;
745 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000746
747 f = i40evf_find_vlan(adapter, vlan);
748 if (f) {
749 f->remove = true;
750 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
751 }
Mitch Williams13acb542015-03-31 00:45:05 -0700752 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000753}
754
755/**
756 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
757 * @netdev: network device struct
758 * @vid: VLAN tag
759 **/
760static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000761 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000762{
763 struct i40evf_adapter *adapter = netdev_priv(netdev);
764
Mitch Williams8ed995f2015-08-28 17:55:57 -0400765 if (!VLAN_ALLOWED(adapter))
766 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000767 if (i40evf_add_vlan(adapter, vid) == NULL)
768 return -ENOMEM;
769 return 0;
770}
771
772/**
773 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
774 * @netdev: network device struct
775 * @vid: VLAN tag
776 **/
777static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000778 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000779{
780 struct i40evf_adapter *adapter = netdev_priv(netdev);
781
Mitch Williams8ed995f2015-08-28 17:55:57 -0400782 if (VLAN_ALLOWED(adapter)) {
783 i40evf_del_vlan(adapter, vid);
784 return 0;
785 }
786 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000787}
788
789/**
790 * i40evf_find_filter - Search filter list for specific mac filter
791 * @adapter: board private structure
792 * @macaddr: the MAC address
793 *
794 * Returns ptr to the filter object or NULL
795 **/
796static struct
797i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
798 u8 *macaddr)
799{
800 struct i40evf_mac_filter *f;
801
802 if (!macaddr)
803 return NULL;
804
805 list_for_each_entry(f, &adapter->mac_filter_list, list) {
806 if (ether_addr_equal(macaddr, f->macaddr))
807 return f;
808 }
809 return NULL;
810}
811
812/**
813 * i40e_add_filter - Add a mac filter to the filter list
814 * @adapter: board private structure
815 * @macaddr: the MAC address
816 *
817 * Returns ptr to the filter object or NULL when no memory available.
818 **/
819static struct
820i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
821 u8 *macaddr)
822{
823 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000824 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000825
826 if (!macaddr)
827 return NULL;
828
829 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000830 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000831 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000832 if (--count == 0)
833 return NULL;
834 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000835
836 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000837 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000838 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000839 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000840 clear_bit(__I40EVF_IN_CRITICAL_TASK,
841 &adapter->crit_section);
842 return NULL;
843 }
844
Greg Rose9a173902014-05-22 06:32:02 +0000845 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000846
847 list_add(&f->list, &adapter->mac_filter_list);
848 f->add = true;
849 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
850 }
851
852 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
853 return f;
854}
855
856/**
857 * i40evf_set_mac - NDO callback to set port mac address
858 * @netdev: network interface device structure
859 * @p: pointer to an address structure
860 *
861 * Returns 0 on success, negative on failure
862 **/
863static int i40evf_set_mac(struct net_device *netdev, void *p)
864{
865 struct i40evf_adapter *adapter = netdev_priv(netdev);
866 struct i40e_hw *hw = &adapter->hw;
867 struct i40evf_mac_filter *f;
868 struct sockaddr *addr = p;
869
870 if (!is_valid_ether_addr(addr->sa_data))
871 return -EADDRNOTAVAIL;
872
873 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
874 return 0;
875
Mitch Williams14e52ee2015-08-31 19:54:44 -0400876 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
877 return -EPERM;
878
879 f = i40evf_find_filter(adapter, hw->mac.addr);
880 if (f) {
881 f->remove = true;
882 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
883 }
884
Greg Rose5eae00c2013-12-21 06:12:45 +0000885 f = i40evf_add_filter(adapter, addr->sa_data);
886 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000887 ether_addr_copy(hw->mac.addr, addr->sa_data);
888 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000889 }
890
891 return (f == NULL) ? -ENOMEM : 0;
892}
893
894/**
895 * i40evf_set_rx_mode - NDO callback to set the netdev filters
896 * @netdev: network interface device structure
897 **/
898static void i40evf_set_rx_mode(struct net_device *netdev)
899{
900 struct i40evf_adapter *adapter = netdev_priv(netdev);
901 struct i40evf_mac_filter *f, *ftmp;
902 struct netdev_hw_addr *uca;
903 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400904 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000905 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000906
907 /* add addr if not already in the filter list */
908 netdev_for_each_uc_addr(uca, netdev) {
909 i40evf_add_filter(adapter, uca->addr);
910 }
911 netdev_for_each_mc_addr(mca, netdev) {
912 i40evf_add_filter(adapter, mca->addr);
913 }
914
915 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000916 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000917 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000918 if (--count == 0) {
919 dev_err(&adapter->pdev->dev,
920 "Failed to get lock in %s\n", __func__);
921 return;
922 }
923 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000924 /* remove filter if not in netdev list */
925 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400926 netdev_for_each_mc_addr(mca, netdev)
927 if (ether_addr_equal(mca->addr, f->macaddr))
928 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000929
Shannon Nelson2f41f332015-08-26 15:14:20 -0400930 netdev_for_each_uc_addr(uca, netdev)
931 if (ether_addr_equal(uca->addr, f->macaddr))
932 goto bottom_of_search_loop;
933
934 for_each_dev_addr(netdev, ha)
935 if (ether_addr_equal(ha->addr, f->macaddr))
936 goto bottom_of_search_loop;
937
938 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
939 goto bottom_of_search_loop;
940
941 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
942 f->remove = true;
943 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
944
945bottom_of_search_loop:
946 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000947 }
948 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
949}
950
951/**
952 * i40evf_napi_enable_all - enable NAPI on all queue vectors
953 * @adapter: board private structure
954 **/
955static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
956{
957 int q_idx;
958 struct i40e_q_vector *q_vector;
959 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
960
961 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
962 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000963
Mitch Williams7d96ba12015-10-26 19:44:39 -0400964 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000965 napi = &q_vector->napi;
966 napi_enable(napi);
967 }
968}
969
970/**
971 * i40evf_napi_disable_all - disable NAPI on all queue vectors
972 * @adapter: board private structure
973 **/
974static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
975{
976 int q_idx;
977 struct i40e_q_vector *q_vector;
978 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
979
980 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400981 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000982 napi_disable(&q_vector->napi);
983 }
984}
985
986/**
987 * i40evf_configure - set up transmit and receive data structures
988 * @adapter: board private structure
989 **/
990static void i40evf_configure(struct i40evf_adapter *adapter)
991{
992 struct net_device *netdev = adapter->netdev;
993 int i;
994
995 i40evf_set_rx_mode(netdev);
996
997 i40evf_configure_tx(adapter);
998 i40evf_configure_rx(adapter);
999 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1000
Mitch Williamscc052922014-10-25 03:24:34 +00001001 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001002 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001003
Mitch Williamsa132af22015-01-24 09:58:35 +00001004 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Greg Rose5eae00c2013-12-21 06:12:45 +00001005 ring->next_to_use = ring->count - 1;
1006 writel(ring->next_to_use, ring->tail);
1007 }
1008}
1009
1010/**
1011 * i40evf_up_complete - Finish the last steps of bringing up a connection
1012 * @adapter: board private structure
1013 **/
1014static int i40evf_up_complete(struct i40evf_adapter *adapter)
1015{
1016 adapter->state = __I40EVF_RUNNING;
1017 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1018
1019 i40evf_napi_enable_all(adapter);
1020
1021 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1022 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1023 return 0;
1024}
1025
1026/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001027 * i40e_down - Shutdown the connection processing
1028 * @adapter: board private structure
1029 **/
1030void i40evf_down(struct i40evf_adapter *adapter)
1031{
1032 struct net_device *netdev = adapter->netdev;
1033 struct i40evf_mac_filter *f;
1034
Mitch Williams209dc4d2015-12-09 15:50:27 -08001035 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001036 return;
1037
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001038 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1039 &adapter->crit_section))
1040 usleep_range(500, 1000);
1041
Mitch Williams63e18c22015-03-27 00:12:10 -07001042 netif_carrier_off(netdev);
1043 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +00001044 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001045 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001046
Mitch Williamsef8693e2014-02-13 03:48:53 -08001047 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001048 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1049 f->remove = true;
1050 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001051 /* remove all VLAN filters */
1052 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1053 f->remove = true;
1054 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001055 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1056 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001057 /* cancel any current operation */
1058 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001059 /* Schedule operations to close down the HW. Don't wait
1060 * here for this to complete. The watchdog is still running
1061 * and it will take care of this.
1062 */
1063 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001064 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001065 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001066 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001067
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001068 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001069}
1070
1071/**
1072 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1073 * @adapter: board private structure
1074 * @vectors: number of vectors to request
1075 *
1076 * Work with the OS to set up the MSIX vectors needed.
1077 *
1078 * Returns 0 on success, negative on failure
1079 **/
1080static int
1081i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1082{
1083 int err, vector_threshold;
1084
1085 /* We'll want at least 3 (vector_threshold):
1086 * 0) Other (Admin Queue and link, mostly)
1087 * 1) TxQ[0] Cleanup
1088 * 2) RxQ[0] Cleanup
1089 */
1090 vector_threshold = MIN_MSIX_COUNT;
1091
1092 /* The more we get, the more we will assign to Tx/Rx Cleanup
1093 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1094 * Right now, we simply care about how many we'll get; we'll
1095 * set them up later while requesting irq's.
1096 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001097 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1098 vector_threshold, vectors);
1099 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001100 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001101 kfree(adapter->msix_entries);
1102 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001103 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001104 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001105
1106 /* Adjust for only the vectors we'll use, which is minimum
1107 * of max_msix_q_vectors + NONQ_VECS, or the number of
1108 * vectors we were allocated.
1109 */
1110 adapter->num_msix_vectors = err;
1111 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001112}
1113
1114/**
1115 * i40evf_free_queues - Free memory for all rings
1116 * @adapter: board private structure to initialize
1117 *
1118 * Free all of the memory associated with queue pairs.
1119 **/
1120static void i40evf_free_queues(struct i40evf_adapter *adapter)
1121{
Greg Rose5eae00c2013-12-21 06:12:45 +00001122 if (!adapter->vsi_res)
1123 return;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001124 kfree(adapter->tx_rings);
1125 kfree(adapter->rx_rings);
Greg Rose5eae00c2013-12-21 06:12:45 +00001126}
1127
1128/**
1129 * i40evf_alloc_queues - Allocate memory for all rings
1130 * @adapter: board private structure to initialize
1131 *
1132 * We allocate one ring per queue at run-time since we don't know the
1133 * number of queues at compile-time. The polling_netdev array is
1134 * intended for Multiqueue, but should work fine with a single queue.
1135 **/
1136static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1137{
1138 int i;
1139
Mitch Williams0dd438d2015-10-26 19:44:40 -04001140 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1141 sizeof(struct i40e_ring), GFP_KERNEL);
1142 if (!adapter->tx_rings)
1143 goto err_out;
1144 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1145 sizeof(struct i40e_ring), GFP_KERNEL);
1146 if (!adapter->rx_rings)
1147 goto err_out;
1148
Mitch Williamscc052922014-10-25 03:24:34 +00001149 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001150 struct i40e_ring *tx_ring;
1151 struct i40e_ring *rx_ring;
1152
Mitch Williams0dd438d2015-10-26 19:44:40 -04001153 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001154
1155 tx_ring->queue_index = i;
1156 tx_ring->netdev = adapter->netdev;
1157 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001158 tx_ring->count = adapter->tx_desc_count;
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001159 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1160 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001161
Mitch Williams0dd438d2015-10-26 19:44:40 -04001162 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001163 rx_ring->queue_index = i;
1164 rx_ring->netdev = adapter->netdev;
1165 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001166 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001167 }
1168
1169 return 0;
1170
1171err_out:
1172 i40evf_free_queues(adapter);
1173 return -ENOMEM;
1174}
1175
1176/**
1177 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1178 * @adapter: board private structure to initialize
1179 *
1180 * Attempt to configure the interrupts using the best available
1181 * capabilities of the hardware and the kernel.
1182 **/
1183static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1184{
1185 int vector, v_budget;
1186 int pairs = 0;
1187 int err = 0;
1188
1189 if (!adapter->vsi_res) {
1190 err = -EIO;
1191 goto out;
1192 }
Mitch Williamscc052922014-10-25 03:24:34 +00001193 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001194
1195 /* It's easy to be greedy for MSI-X vectors, but it really
1196 * doesn't do us much good if we have a lot more vectors
1197 * than CPU's. So let's be conservative and only ask for
1198 * (roughly) twice the number of vectors as there are CPU's.
1199 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001200 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1201 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001202
Greg Rose5eae00c2013-12-21 06:12:45 +00001203 adapter->msix_entries = kcalloc(v_budget,
1204 sizeof(struct msix_entry), GFP_KERNEL);
1205 if (!adapter->msix_entries) {
1206 err = -ENOMEM;
1207 goto out;
1208 }
1209
1210 for (vector = 0; vector < v_budget; vector++)
1211 adapter->msix_entries[vector].entry = vector;
1212
Mitch Williams313ed2d2015-08-27 11:42:31 -04001213 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001214
1215out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001216 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1217 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001218 return err;
1219}
1220
1221/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001222 * i40e_config_rss_aq - Prepare for RSS using AQ commands
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001223 * @vsi: vsi structure
1224 * @seed: RSS hash seed
Helin Zhang2c86ac32015-10-27 16:15:06 -04001225 * @lut: Lookup table
1226 * @lut_size: Lookup table size
1227 *
1228 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001229 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001230static int i40evf_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1231 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001232{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001233 struct i40evf_adapter *adapter = vsi->back;
1234 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001235 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001236
1237 if (!vsi->id)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001238 return -EINVAL;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001239
1240 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1241 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001242 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001243 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001244 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001245 }
1246
Helin Zhang2c86ac32015-10-27 16:15:06 -04001247 if (seed) {
1248 struct i40e_aqc_get_set_rss_key_data *rss_key =
1249 (struct i40e_aqc_get_set_rss_key_data *)seed;
1250 ret = i40evf_aq_set_rss_key(hw, vsi->id, rss_key);
1251 if (ret) {
1252 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1253 i40evf_stat_str(hw, ret),
1254 i40evf_aq_str(hw, hw->aq.asq_last_status));
1255 return ret;
1256 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001257 }
1258
Helin Zhang2c86ac32015-10-27 16:15:06 -04001259 if (lut) {
1260 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, lut, lut_size);
1261 if (ret) {
1262 dev_err(&adapter->pdev->dev,
1263 "Cannot set RSS lut, err %s aq_err %s\n",
1264 i40evf_stat_str(hw, ret),
1265 i40evf_aq_str(hw, hw->aq.asq_last_status));
1266 return ret;
1267 }
1268 }
1269
1270 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001271}
1272
1273/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001274 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1275 * @vsi: Pointer to vsi structure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001276 * @seed: RSS hash seed
Helin Zhang2c86ac32015-10-27 16:15:06 -04001277 * @lut: Lookup table
1278 * @lut_size: Lookup table size
1279 *
1280 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001281 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001282static int i40evf_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
1283 const u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001284{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001285 struct i40evf_adapter *adapter = vsi->back;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001286 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001287 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001288
Helin Zhang2c86ac32015-10-27 16:15:06 -04001289 if (seed) {
1290 u32 *seed_dw = (u32 *)seed;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001291
Helin Zhang2c86ac32015-10-27 16:15:06 -04001292 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1293 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1294 }
1295
1296 if (lut) {
1297 u32 *lut_dw = (u32 *)lut;
1298
1299 if (lut_size != I40EVF_HLUT_ARRAY_SIZE)
1300 return -EINVAL;
1301
1302 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
1303 wr32(hw, I40E_VFQF_HLUT(i), lut_dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001304 }
1305 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001306
1307 return 0;
1308}
1309
1310/**
Helin Zhang90b02b42015-10-26 19:44:33 -04001311 * * i40evf_get_rss_aq - Get RSS keys and lut by using AQ commands
1312 * @vsi: Pointer to vsi structure
1313 * @seed: RSS hash seed
1314 * @lut: Lookup table
1315 * @lut_size: Lookup table size
1316 *
1317 * Return 0 on success, negative on failure
1318 **/
1319static int i40evf_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1320 u8 *lut, u16 lut_size)
1321{
1322 struct i40evf_adapter *adapter = vsi->back;
1323 struct i40e_hw *hw = &adapter->hw;
1324 int ret = 0;
1325
1326 if (seed) {
1327 ret = i40evf_aq_get_rss_key(hw, vsi->id,
1328 (struct i40e_aqc_get_set_rss_key_data *)seed);
1329 if (ret) {
1330 dev_err(&adapter->pdev->dev,
1331 "Cannot get RSS key, err %s aq_err %s\n",
1332 i40evf_stat_str(hw, ret),
1333 i40evf_aq_str(hw, hw->aq.asq_last_status));
1334 return ret;
1335 }
1336 }
1337
1338 if (lut) {
1339 ret = i40evf_aq_get_rss_lut(hw, vsi->id, seed, lut, lut_size);
1340 if (ret) {
1341 dev_err(&adapter->pdev->dev,
1342 "Cannot get RSS lut, err %s aq_err %s\n",
1343 i40evf_stat_str(hw, ret),
1344 i40evf_aq_str(hw, hw->aq.asq_last_status));
1345 return ret;
1346 }
1347 }
1348
1349 return ret;
1350}
1351
1352/**
1353 * * i40evf_get_rss_reg - Get RSS keys and lut by reading registers
1354 * @vsi: Pointer to vsi structure
1355 * @seed: RSS hash seed
1356 * @lut: Lookup table
1357 * @lut_size: Lookup table size
1358 *
1359 * Returns 0 on success, negative on failure
1360 **/
1361static int i40evf_get_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
1362 const u8 *lut, u16 lut_size)
1363{
1364 struct i40evf_adapter *adapter = vsi->back;
1365 struct i40e_hw *hw = &adapter->hw;
1366 u16 i;
1367
1368 if (seed) {
1369 u32 *seed_dw = (u32 *)seed;
1370
1371 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1372 seed_dw[i] = rd32(hw, I40E_VFQF_HKEY(i));
1373 }
1374
1375 if (lut) {
1376 u32 *lut_dw = (u32 *)lut;
1377
1378 if (lut_size != I40EVF_HLUT_ARRAY_SIZE)
1379 return -EINVAL;
1380
1381 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
1382 lut_dw[i] = rd32(hw, I40E_VFQF_HLUT(i));
1383 }
1384
1385 return 0;
1386}
1387
1388/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001389 * i40evf_config_rss - Configure RSS keys and lut
1390 * @vsi: Pointer to vsi structure
1391 * @seed: RSS hash seed
1392 * @lut: Lookup table
1393 * @lut_size: Lookup table size
1394 *
1395 * Returns 0 on success, negative on failure
1396 **/
1397int i40evf_config_rss(struct i40e_vsi *vsi, const u8 *seed,
1398 u8 *lut, u16 lut_size)
1399{
1400 struct i40evf_adapter *adapter = vsi->back;
1401
1402 if (RSS_AQ(adapter))
1403 return i40evf_config_rss_aq(vsi, seed, lut, lut_size);
1404 else
1405 return i40evf_config_rss_reg(vsi, seed, lut, lut_size);
1406}
1407
1408/**
Helin Zhang90b02b42015-10-26 19:44:33 -04001409 * i40evf_get_rss - Get RSS keys and lut
1410 * @vsi: Pointer to vsi structure
1411 * @seed: RSS hash seed
1412 * @lut: Lookup table
1413 * @lut_size: Lookup table size
1414 *
1415 * Returns 0 on success, negative on failure
1416 **/
1417int i40evf_get_rss(struct i40e_vsi *vsi, const u8 *seed, u8 *lut, u16 lut_size)
1418{
1419 struct i40evf_adapter *adapter = vsi->back;
1420
1421 if (RSS_AQ(adapter))
1422 return i40evf_get_rss_aq(vsi, seed, lut, lut_size);
1423 else
1424 return i40evf_get_rss_reg(vsi, seed, lut, lut_size);
1425}
1426
1427/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001428 * i40evf_fill_rss_lut - Fill the lut with default values
1429 * @lut: Lookup table to be filled with
1430 * @rss_table_size: Lookup table size
1431 * @rss_size: Range of queue number for hashing
1432 **/
1433static void i40evf_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
1434{
1435 u16 i;
1436
1437 for (i = 0; i < rss_table_size; i++)
1438 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001439}
1440
1441/**
Helin Zhang96a81982015-10-26 19:44:31 -04001442 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001443 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001444 *
1445 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001446 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001447static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001448{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001449 struct i40e_vsi *vsi = &adapter->vsi;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001450 struct i40e_hw *hw = &adapter->hw;
1451 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1452 u64 hena;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001453 u8 *lut;
1454 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001455
1456 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1457 hena = I40E_DEFAULT_RSS_HENA;
1458 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1459 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1460
Helin Zhang2c86ac32015-10-27 16:15:06 -04001461 lut = kzalloc(I40EVF_HLUT_ARRAY_SIZE, GFP_KERNEL);
1462 if (!lut)
1463 return -ENOMEM;
Helin Zhang66f9af852015-10-26 19:44:34 -04001464
1465 /* Use user configured lut if there is one, otherwise use default */
1466 if (vsi->rss_lut_user)
1467 memcpy(lut, vsi->rss_lut_user, I40EVF_HLUT_ARRAY_SIZE);
1468 else
1469 i40evf_fill_rss_lut(lut, I40EVF_HLUT_ARRAY_SIZE,
1470 adapter->num_active_queues);
1471
1472 /* Use user configured hash key if there is one, otherwise
1473 * user default.
1474 */
1475 if (vsi->rss_hkey_user)
1476 memcpy(seed, vsi->rss_hkey_user, I40EVF_HKEY_ARRAY_SIZE);
1477 else
1478 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001479 ret = i40evf_config_rss(vsi, seed, lut, I40EVF_HLUT_ARRAY_SIZE);
1480 kfree(lut);
1481
1482 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001483}
1484
1485/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001486 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1487 * @adapter: board private structure to initialize
1488 *
1489 * We allocate one q_vector per queue interrupt. If allocation fails we
1490 * return -ENOMEM.
1491 **/
1492static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1493{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001494 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001495 struct i40e_q_vector *q_vector;
1496
1497 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001498 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001499 GFP_KERNEL);
1500 if (!adapter->q_vectors)
1501 goto err_out;
Greg Rose5eae00c2013-12-21 06:12:45 +00001502
1503 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001504 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001505 q_vector->adapter = adapter;
1506 q_vector->vsi = &adapter->vsi;
1507 q_vector->v_idx = q_idx;
1508 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001509 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001510 }
1511
1512 return 0;
1513
1514err_out:
1515 while (q_idx) {
1516 q_idx--;
Mitch Williams7d96ba12015-10-26 19:44:39 -04001517 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001518 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001519 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001520 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001521 return -ENOMEM;
1522}
1523
1524/**
1525 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1526 * @adapter: board private structure to initialize
1527 *
1528 * This function frees the memory allocated to the q_vectors. In addition if
1529 * NAPI is enabled it will delete any references to the NAPI struct prior
1530 * to freeing the q_vector.
1531 **/
1532static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1533{
1534 int q_idx, num_q_vectors;
1535 int napi_vectors;
1536
1537 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001538 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001539
1540 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001541 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001542 if (q_idx < napi_vectors)
1543 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001544 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001545 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001546}
1547
1548/**
1549 * i40evf_reset_interrupt_capability - Reset MSIX setup
1550 * @adapter: board private structure
1551 *
1552 **/
1553void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1554{
1555 pci_disable_msix(adapter->pdev);
1556 kfree(adapter->msix_entries);
1557 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001558}
1559
1560/**
1561 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1562 * @adapter: board private structure to initialize
1563 *
1564 **/
1565int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1566{
1567 int err;
1568
1569 err = i40evf_set_interrupt_capability(adapter);
1570 if (err) {
1571 dev_err(&adapter->pdev->dev,
1572 "Unable to setup interrupt capabilities\n");
1573 goto err_set_interrupt;
1574 }
1575
1576 err = i40evf_alloc_q_vectors(adapter);
1577 if (err) {
1578 dev_err(&adapter->pdev->dev,
1579 "Unable to allocate memory for queue vectors\n");
1580 goto err_alloc_q_vectors;
1581 }
1582
1583 err = i40evf_alloc_queues(adapter);
1584 if (err) {
1585 dev_err(&adapter->pdev->dev,
1586 "Unable to allocate memory for queues\n");
1587 goto err_alloc_queues;
1588 }
1589
1590 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001591 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1592 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001593
1594 return 0;
1595err_alloc_queues:
1596 i40evf_free_q_vectors(adapter);
1597err_alloc_q_vectors:
1598 i40evf_reset_interrupt_capability(adapter);
1599err_set_interrupt:
1600 return err;
1601}
1602
1603/**
Helin Zhang66f9af852015-10-26 19:44:34 -04001604 * i40evf_clear_rss_config_user - Clear user configurations of RSS
1605 * @vsi: Pointer to VSI structure
1606 **/
1607static void i40evf_clear_rss_config_user(struct i40e_vsi *vsi)
1608{
1609 if (!vsi)
1610 return;
1611
1612 kfree(vsi->rss_hkey_user);
1613 vsi->rss_hkey_user = NULL;
1614
1615 kfree(vsi->rss_lut_user);
1616 vsi->rss_lut_user = NULL;
1617}
1618
1619/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001620 * i40evf_watchdog_timer - Periodic call-back timer
1621 * @data: pointer to adapter disguised as unsigned long
1622 **/
1623static void i40evf_watchdog_timer(unsigned long data)
1624{
1625 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001626
Greg Rose5eae00c2013-12-21 06:12:45 +00001627 schedule_work(&adapter->watchdog_task);
1628 /* timer will be rescheduled in watchdog task */
1629}
1630
1631/**
1632 * i40evf_watchdog_task - Periodic call-back task
1633 * @work: pointer to work_struct
1634 **/
1635static void i40evf_watchdog_task(struct work_struct *work)
1636{
1637 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001638 struct i40evf_adapter,
1639 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001640 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001641 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001642
Greg Rose5eae00c2013-12-21 06:12:45 +00001643 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001644 goto restart_watchdog;
1645
1646 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001647 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1648 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1649 if ((reg_val == I40E_VFR_VFACTIVE) ||
1650 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001651 /* A chance for redemption! */
1652 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1653 adapter->state = __I40EVF_STARTUP;
1654 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1655 schedule_delayed_work(&adapter->init_task, 10);
1656 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1657 &adapter->crit_section);
1658 /* Don't reschedule the watchdog, since we've restarted
1659 * the init task. When init_task contacts the PF and
1660 * gets everything set up again, it'll restart the
1661 * watchdog for us. Down, boy. Sit. Stay. Woof.
1662 */
1663 return;
1664 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001665 adapter->aq_required = 0;
1666 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1667 goto watchdog_done;
1668 }
1669
1670 if ((adapter->state < __I40EVF_DOWN) ||
1671 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001672 goto watchdog_done;
1673
Mitch Williamsef8693e2014-02-13 03:48:53 -08001674 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001675 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1676 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001677 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001678 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001679 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001680 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001681 adapter->aq_required = 0;
1682 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001683 goto watchdog_done;
1684 }
1685
1686 /* Process admin queue tasks. After init, everything gets done
1687 * here so we don't race on the admin queue.
1688 */
Mitch Williamsed636962015-04-07 19:45:32 -04001689 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001690 if (!i40evf_asq_done(hw)) {
1691 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1692 i40evf_send_api_ver(adapter);
1693 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001694 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001695 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001696 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1697 i40evf_send_vf_config_msg(adapter);
1698 goto watchdog_done;
1699 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001700
Mitch Williamse284fc82015-03-27 00:12:09 -07001701 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1702 i40evf_disable_queues(adapter);
1703 goto watchdog_done;
1704 }
1705
Greg Rose5eae00c2013-12-21 06:12:45 +00001706 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1707 i40evf_map_queues(adapter);
1708 goto watchdog_done;
1709 }
1710
1711 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1712 i40evf_add_ether_addrs(adapter);
1713 goto watchdog_done;
1714 }
1715
1716 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1717 i40evf_add_vlans(adapter);
1718 goto watchdog_done;
1719 }
1720
1721 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1722 i40evf_del_ether_addrs(adapter);
1723 goto watchdog_done;
1724 }
1725
1726 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1727 i40evf_del_vlans(adapter);
1728 goto watchdog_done;
1729 }
1730
Greg Rose5eae00c2013-12-21 06:12:45 +00001731 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1732 i40evf_configure_queues(adapter);
1733 goto watchdog_done;
1734 }
1735
1736 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1737 i40evf_enable_queues(adapter);
1738 goto watchdog_done;
1739 }
1740
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001741 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1742 /* This message goes straight to the firmware, not the
1743 * PF, so we don't have to set current_op as we will
1744 * not get a response through the ARQ.
1745 */
Helin Zhang96a81982015-10-26 19:44:31 -04001746 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001747 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1748 goto watchdog_done;
1749 }
1750
Greg Rose5eae00c2013-12-21 06:12:45 +00001751 if (adapter->state == __I40EVF_RUNNING)
1752 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001753watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001754 if (adapter->state == __I40EVF_RUNNING) {
1755 i40evf_irq_enable_queues(adapter, ~0);
1756 i40evf_fire_sw_int(adapter, 0xFF);
1757 } else {
1758 i40evf_fire_sw_int(adapter, 0x1);
1759 }
1760
Mitch Williamsef8693e2014-02-13 03:48:53 -08001761 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1762restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001763 if (adapter->state == __I40EVF_REMOVE)
1764 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001765 if (adapter->aq_required)
1766 mod_timer(&adapter->watchdog_timer,
1767 jiffies + msecs_to_jiffies(20));
1768 else
1769 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001770 schedule_work(&adapter->adminq_task);
1771}
1772
Mitch Williams67c818a2015-06-19 08:56:30 -07001773#define I40EVF_RESET_WAIT_MS 10
1774#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001775/**
1776 * i40evf_reset_task - Call-back task to handle hardware reset
1777 * @work: pointer to work_struct
1778 *
1779 * During reset we need to shut down and reinitialize the admin queue
1780 * before we can use it to communicate with the PF again. We also clear
1781 * and reinit the rings because that context is lost as well.
1782 **/
1783static void i40evf_reset_task(struct work_struct *work)
1784{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001785 struct i40evf_adapter *adapter = container_of(work,
1786 struct i40evf_adapter,
1787 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001788 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001789 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001790 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001791 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001792 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001793 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001794
1795 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1796 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001797 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001798
Mitch Williams67c818a2015-06-19 08:56:30 -07001799 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001800 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001801 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1802 /* Restart the AQ here. If we have been reset but didn't
1803 * detect it, or if the PF had to reinit, our AQ will be hosed.
1804 */
1805 i40evf_shutdown_adminq(hw);
1806 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001807 i40evf_request_reset(adapter);
1808 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001809 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001810
Mitch Williamsef8693e2014-02-13 03:48:53 -08001811 /* poll until we see the reset actually happen */
1812 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001813 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1814 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1815 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001816 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001817 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001818 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001819 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001820 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001821 goto continue_reset; /* act like the reset happened */
1822 }
1823
1824 /* wait until the reset is complete and the PF is responding to us */
1825 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001826 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1827 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1828 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001829 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001830 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001831 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001832 /* extra wait to make sure minimum wait is met */
1833 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001834 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001835 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001836 struct i40evf_vlan_filter *fv, *fvtmp;
1837
Greg Rose5eae00c2013-12-21 06:12:45 +00001838 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001839 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001840 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001841 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1842
Mitch Williams169f4072014-04-01 07:11:50 +00001843 if (netif_running(adapter->netdev)) {
1844 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001845 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001846 netif_tx_disable(netdev);
1847 i40evf_napi_disable_all(adapter);
1848 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001849 i40evf_free_traffic_irqs(adapter);
1850 i40evf_free_all_tx_resources(adapter);
1851 i40evf_free_all_rx_resources(adapter);
1852 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001853
1854 /* Delete all of the filters, both MAC and VLAN. */
1855 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1856 list) {
1857 list_del(&f->list);
1858 kfree(f);
1859 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001860
Mitch Williams6ba36a22014-08-01 13:27:14 -07001861 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1862 list) {
1863 list_del(&fv->list);
1864 kfree(fv);
1865 }
1866
Mitch Williamsef8693e2014-02-13 03:48:53 -08001867 i40evf_free_misc_irq(adapter);
1868 i40evf_reset_interrupt_capability(adapter);
1869 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001870 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001871 kfree(adapter->vf_res);
1872 i40evf_shutdown_adminq(hw);
1873 adapter->netdev->flags &= ~IFF_UP;
1874 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001875 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1876 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001877 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001878 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001879
1880continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001881 if (netif_running(adapter->netdev)) {
1882 netif_carrier_off(netdev);
1883 netif_tx_stop_all_queues(netdev);
1884 i40evf_napi_disable_all(adapter);
1885 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001886 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001887
Greg Rose5eae00c2013-12-21 06:12:45 +00001888 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001889 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1890
1891 /* free the Tx/Rx rings and descriptors, might be better to just
1892 * re-use them sometime in the future
1893 */
1894 i40evf_free_all_rx_resources(adapter);
1895 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001896
1897 /* kill and reinit the admin queue */
1898 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001899 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1900 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001901 err = i40evf_init_adminq(hw);
1902 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001903 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1904 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001905
Mitch Williamse6d038d2015-06-04 16:23:58 -04001906 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1907 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001908
1909 /* re-add all MAC filters */
1910 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1911 f->add = true;
1912 }
1913 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001914 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1915 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001916 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001917 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001918 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001919 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001920 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001921
1922 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1923
1924 if (netif_running(adapter->netdev)) {
1925 /* allocate transmit descriptors */
1926 err = i40evf_setup_all_tx_resources(adapter);
1927 if (err)
1928 goto reset_err;
1929
1930 /* allocate receive descriptors */
1931 err = i40evf_setup_all_rx_resources(adapter);
1932 if (err)
1933 goto reset_err;
1934
1935 i40evf_configure(adapter);
1936
1937 err = i40evf_up_complete(adapter);
1938 if (err)
1939 goto reset_err;
1940
1941 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001942 } else {
1943 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001944 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001945
Greg Rose5eae00c2013-12-21 06:12:45 +00001946 return;
1947reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001948 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001949 i40evf_close(adapter->netdev);
1950}
1951
1952/**
1953 * i40evf_adminq_task - worker thread to clean the admin queue
1954 * @work: pointer to work_struct containing our data
1955 **/
1956static void i40evf_adminq_task(struct work_struct *work)
1957{
1958 struct i40evf_adapter *adapter =
1959 container_of(work, struct i40evf_adapter, adminq_task);
1960 struct i40e_hw *hw = &adapter->hw;
1961 struct i40e_arq_event_info event;
1962 struct i40e_virtchnl_msg *v_msg;
1963 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001964 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001965 u16 pending;
1966
Mitch Williamsef8693e2014-02-13 03:48:53 -08001967 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001968 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001969
Mitch Williams1001dc32014-11-11 20:02:19 +00001970 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1971 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001972 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001973 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001974
Greg Rose5eae00c2013-12-21 06:12:45 +00001975 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1976 do {
1977 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001978 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001979 break; /* No event to process or error cleaning ARQ */
1980
1981 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1982 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001983 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001984 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001985 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001986 } while (pending);
1987
Mitch Williams67c818a2015-06-19 08:56:30 -07001988 if ((adapter->flags &
1989 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1990 adapter->state == __I40EVF_RESETTING)
1991 goto freedom;
1992
Mitch Williams912257e2014-05-22 06:32:07 +00001993 /* check for error indications */
1994 val = rd32(hw, hw->aq.arq.len);
1995 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001996 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001997 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001998 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001999 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002000 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002001 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002002 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002003 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002004 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002005 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002006 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002007 }
2008 if (oldval != val)
2009 wr32(hw, hw->aq.arq.len, val);
2010
2011 val = rd32(hw, hw->aq.asq.len);
2012 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002013 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002014 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002015 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002016 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002017 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002018 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002019 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002020 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002021 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002022 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002023 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002024 }
2025 if (oldval != val)
2026 wr32(hw, hw->aq.asq.len, val);
2027
Mitch Williams67c818a2015-06-19 08:56:30 -07002028freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002029 kfree(event.msg_buf);
2030out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002031 /* re-enable Admin queue interrupt cause */
2032 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002033}
2034
2035/**
2036 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2037 * @adapter: board private structure
2038 *
2039 * Free all transmit software resources
2040 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002041void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002042{
2043 int i;
2044
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002045 if (!adapter->tx_rings)
2046 return;
2047
Mitch Williamscc052922014-10-25 03:24:34 +00002048 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002049 if (adapter->tx_rings[i].desc)
2050 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002051}
2052
2053/**
2054 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2055 * @adapter: board private structure
2056 *
2057 * If this function returns with an error, then it's possible one or
2058 * more of the rings is populated (while the rest are not). It is the
2059 * callers duty to clean those orphaned rings.
2060 *
2061 * Return 0 on success, negative on failure
2062 **/
2063static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2064{
2065 int i, err = 0;
2066
Mitch Williamscc052922014-10-25 03:24:34 +00002067 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002068 adapter->tx_rings[i].count = adapter->tx_desc_count;
2069 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002070 if (!err)
2071 continue;
2072 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002073 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002074 break;
2075 }
2076
2077 return err;
2078}
2079
2080/**
2081 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2082 * @adapter: board private structure
2083 *
2084 * If this function returns with an error, then it's possible one or
2085 * more of the rings is populated (while the rest are not). It is the
2086 * callers duty to clean those orphaned rings.
2087 *
2088 * Return 0 on success, negative on failure
2089 **/
2090static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2091{
2092 int i, err = 0;
2093
Mitch Williamscc052922014-10-25 03:24:34 +00002094 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002095 adapter->rx_rings[i].count = adapter->rx_desc_count;
2096 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002097 if (!err)
2098 continue;
2099 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002100 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002101 break;
2102 }
2103 return err;
2104}
2105
2106/**
2107 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2108 * @adapter: board private structure
2109 *
2110 * Free all receive software resources
2111 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002112void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002113{
2114 int i;
2115
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002116 if (!adapter->rx_rings)
2117 return;
2118
Mitch Williamscc052922014-10-25 03:24:34 +00002119 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002120 if (adapter->rx_rings[i].desc)
2121 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002122}
2123
2124/**
2125 * i40evf_open - Called when a network interface is made active
2126 * @netdev: network interface device structure
2127 *
2128 * Returns 0 on success, negative value on failure
2129 *
2130 * The open entry point is called when a network interface is made
2131 * active by the system (IFF_UP). At this point all resources needed
2132 * for transmit and receive operations are allocated, the interrupt
2133 * handler is registered with the OS, the watchdog timer is started,
2134 * and the stack is notified that the interface is ready.
2135 **/
2136static int i40evf_open(struct net_device *netdev)
2137{
2138 struct i40evf_adapter *adapter = netdev_priv(netdev);
2139 int err;
2140
Mitch Williamsef8693e2014-02-13 03:48:53 -08002141 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2142 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2143 return -EIO;
2144 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002145
2146 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002147 return -EBUSY;
2148
2149 /* allocate transmit descriptors */
2150 err = i40evf_setup_all_tx_resources(adapter);
2151 if (err)
2152 goto err_setup_tx;
2153
2154 /* allocate receive descriptors */
2155 err = i40evf_setup_all_rx_resources(adapter);
2156 if (err)
2157 goto err_setup_rx;
2158
2159 /* clear any pending interrupts, may auto mask */
2160 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2161 if (err)
2162 goto err_req_irq;
2163
Mitch Williams44151cd2015-04-27 14:57:17 -04002164 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002165 i40evf_configure(adapter);
2166
2167 err = i40evf_up_complete(adapter);
2168 if (err)
2169 goto err_req_irq;
2170
2171 i40evf_irq_enable(adapter, true);
2172
2173 return 0;
2174
2175err_req_irq:
2176 i40evf_down(adapter);
2177 i40evf_free_traffic_irqs(adapter);
2178err_setup_rx:
2179 i40evf_free_all_rx_resources(adapter);
2180err_setup_tx:
2181 i40evf_free_all_tx_resources(adapter);
2182
2183 return err;
2184}
2185
2186/**
2187 * i40evf_close - Disables a network interface
2188 * @netdev: network interface device structure
2189 *
2190 * Returns 0, this is not allowed to fail
2191 *
2192 * The close entry point is called when an interface is de-activated
2193 * by the OS. The hardware is still under the drivers control, but
2194 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2195 * are freed, along with all transmit and receive resources.
2196 **/
2197static int i40evf_close(struct net_device *netdev)
2198{
2199 struct i40evf_adapter *adapter = netdev_priv(netdev);
2200
Mitch Williams209dc4d2015-12-09 15:50:27 -08002201 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002202 return 0;
2203
Mitch Williamsef8693e2014-02-13 03:48:53 -08002204
Greg Rose5eae00c2013-12-21 06:12:45 +00002205 set_bit(__I40E_DOWN, &adapter->vsi.state);
2206
2207 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002208 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002209 i40evf_free_traffic_irqs(adapter);
2210
Greg Rose5eae00c2013-12-21 06:12:45 +00002211 return 0;
2212}
2213
2214/**
2215 * i40evf_get_stats - Get System Network Statistics
2216 * @netdev: network interface device structure
2217 *
2218 * Returns the address of the device statistics structure.
2219 * The statistics are actually updated from the timer callback.
2220 **/
2221static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2222{
2223 struct i40evf_adapter *adapter = netdev_priv(netdev);
2224
2225 /* only return the current stats */
2226 return &adapter->net_stats;
2227}
2228
2229/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002230 * i40evf_change_mtu - Change the Maximum Transfer Unit
2231 * @netdev: network interface device structure
2232 * @new_mtu: new value for maximum frame size
2233 *
2234 * Returns 0 on success, negative on failure
2235 **/
2236static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2237{
2238 struct i40evf_adapter *adapter = netdev_priv(netdev);
2239 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2240
2241 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2242 return -EINVAL;
2243
Greg Rose5eae00c2013-12-21 06:12:45 +00002244 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002245 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2246 schedule_work(&adapter->reset_task);
2247
Greg Rose5eae00c2013-12-21 06:12:45 +00002248 return 0;
2249}
2250
2251static const struct net_device_ops i40evf_netdev_ops = {
2252 .ndo_open = i40evf_open,
2253 .ndo_stop = i40evf_close,
2254 .ndo_start_xmit = i40evf_xmit_frame,
2255 .ndo_get_stats = i40evf_get_stats,
2256 .ndo_set_rx_mode = i40evf_set_rx_mode,
2257 .ndo_validate_addr = eth_validate_addr,
2258 .ndo_set_mac_address = i40evf_set_mac,
2259 .ndo_change_mtu = i40evf_change_mtu,
2260 .ndo_tx_timeout = i40evf_tx_timeout,
2261 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2262 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002263#ifdef CONFIG_NET_POLL_CONTROLLER
2264 .ndo_poll_controller = i40evf_netpoll,
2265#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002266};
2267
2268/**
2269 * i40evf_check_reset_complete - check that VF reset is complete
2270 * @hw: pointer to hw struct
2271 *
2272 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2273 **/
2274static int i40evf_check_reset_complete(struct i40e_hw *hw)
2275{
2276 u32 rstat;
2277 int i;
2278
2279 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002280 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2281 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2282 if ((rstat == I40E_VFR_VFACTIVE) ||
2283 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002284 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002285 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002286 }
2287 return -EBUSY;
2288}
2289
2290/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002291 * i40evf_process_config - Process the config information we got from the PF
2292 * @adapter: board private structure
2293 *
2294 * Verify that we have a valid config struct, and set up our netdev features
2295 * and our VSI struct.
2296 **/
2297int i40evf_process_config(struct i40evf_adapter *adapter)
2298{
2299 struct net_device *netdev = adapter->netdev;
2300 int i;
2301
2302 /* got VF config message back from PF, now we can parse it */
2303 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2304 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2305 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2306 }
2307 if (!adapter->vsi_res) {
2308 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2309 return -ENODEV;
2310 }
2311
2312 if (adapter->vf_res->vf_offload_flags
2313 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
Mitch Williamscc7e4062015-09-28 14:12:40 -04002314 netdev->vlan_features = netdev->features &
2315 ~(NETIF_F_HW_VLAN_CTAG_TX |
2316 NETIF_F_HW_VLAN_CTAG_RX |
2317 NETIF_F_HW_VLAN_CTAG_FILTER);
Mitch Williamse6d038d2015-06-04 16:23:58 -04002318 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2319 NETIF_F_HW_VLAN_CTAG_RX |
2320 NETIF_F_HW_VLAN_CTAG_FILTER;
2321 }
2322 netdev->features |= NETIF_F_HIGHDMA |
2323 NETIF_F_SG |
2324 NETIF_F_IP_CSUM |
Tom Herbert53692b12015-12-14 11:19:41 -08002325 NETIF_F_SCTP_CRC |
Mitch Williamse6d038d2015-06-04 16:23:58 -04002326 NETIF_F_IPV6_CSUM |
2327 NETIF_F_TSO |
2328 NETIF_F_TSO6 |
2329 NETIF_F_RXCSUM |
2330 NETIF_F_GRO;
2331
2332 /* copy netdev features into list of user selectable features */
2333 netdev->hw_features |= netdev->features;
2334 netdev->hw_features &= ~NETIF_F_RXCSUM;
2335
2336 adapter->vsi.id = adapter->vsi_res->vsi_id;
2337
2338 adapter->vsi.back = adapter;
2339 adapter->vsi.base_vector = 1;
2340 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2341 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2342 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2343 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2344 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2345 adapter->vsi.netdev = adapter->netdev;
Mitch Williamsf578f5f2015-08-28 17:55:58 -04002346 adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002347 return 0;
2348}
2349
2350/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002351 * i40evf_init_task - worker thread to perform delayed initialization
2352 * @work: pointer to work_struct containing our data
2353 *
2354 * This task completes the work that was begun in probe. Due to the nature
2355 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002356 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002357 * whole system, we'll do it in a task so we can sleep.
2358 * This task only runs during driver init. Once we've established
2359 * communications with the PF driver and set up our netdev, the watchdog
2360 * takes over.
2361 **/
2362static void i40evf_init_task(struct work_struct *work)
2363{
2364 struct i40evf_adapter *adapter = container_of(work,
2365 struct i40evf_adapter,
2366 init_task.work);
2367 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002368 struct i40e_hw *hw = &adapter->hw;
2369 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002370 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002371
2372 switch (adapter->state) {
2373 case __I40EVF_STARTUP:
2374 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002375 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2376 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002377 err = i40e_set_mac_type(hw);
2378 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002379 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2380 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002381 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002382 }
2383 err = i40evf_check_reset_complete(hw);
2384 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002385 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002386 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002387 goto err;
2388 }
2389 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2390 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2391 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2392 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2393
2394 err = i40evf_init_adminq(hw);
2395 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002396 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2397 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002398 goto err;
2399 }
2400 err = i40evf_send_api_ver(adapter);
2401 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002402 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002403 i40evf_shutdown_adminq(hw);
2404 goto err;
2405 }
2406 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2407 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002408 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002409 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002410 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002411 i40evf_shutdown_adminq(hw);
2412 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002413 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002414 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002415
2416 /* aq msg sent, awaiting reply */
2417 err = i40evf_verify_api_ver(adapter);
2418 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002419 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002420 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002421 else
2422 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2423 adapter->pf_version.major,
2424 adapter->pf_version.minor,
2425 I40E_VIRTCHNL_VERSION_MAJOR,
2426 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002427 goto err;
2428 }
2429 err = i40evf_send_vf_config_msg(adapter);
2430 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002431 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002432 err);
2433 goto err;
2434 }
2435 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2436 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002437 case __I40EVF_INIT_GET_RESOURCES:
2438 /* aq msg sent, awaiting reply */
2439 if (!adapter->vf_res) {
2440 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2441 (I40E_MAX_VF_VSI *
2442 sizeof(struct i40e_virtchnl_vsi_resource));
2443 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002444 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002445 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002446 }
2447 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002448 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002449 err = i40evf_send_vf_config_msg(adapter);
2450 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002451 } else if (err == I40E_ERR_PARAM) {
2452 /* We only get ERR_PARAM if the device is in a very bad
2453 * state or if we've been disabled for previous bad
2454 * behavior. Either way, we're done now.
2455 */
2456 i40evf_shutdown_adminq(hw);
2457 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2458 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002459 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002460 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002461 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2462 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002463 goto err_alloc;
2464 }
2465 adapter->state = __I40EVF_INIT_SW;
2466 break;
2467 default:
2468 goto err_alloc;
2469 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04002470 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002471 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002472 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002473
2474 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2475
Greg Rose5eae00c2013-12-21 06:12:45 +00002476 netdev->netdev_ops = &i40evf_netdev_ops;
2477 i40evf_set_ethtool_ops(netdev);
2478 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002479
Greg Rose5eae00c2013-12-21 06:12:45 +00002480 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002481 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002482 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002483 eth_hw_addr_random(netdev);
2484 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2485 } else {
2486 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2487 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2488 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002489 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002490
Greg Rose5eae00c2013-12-21 06:12:45 +00002491 init_timer(&adapter->watchdog_timer);
2492 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2493 adapter->watchdog_timer.data = (unsigned long)adapter;
2494 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2495
Mitch Williamscc052922014-10-25 03:24:34 +00002496 adapter->num_active_queues = min_t(int,
2497 adapter->vsi_res->num_queue_pairs,
2498 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002499 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2500 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002501 err = i40evf_init_interrupt_scheme(adapter);
2502 if (err)
2503 goto err_sw_init;
2504 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002505 if (adapter->vf_res->vf_offload_flags &
2506 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2507 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002508 if (!RSS_AQ(adapter))
Helin Zhang96a81982015-10-26 19:44:31 -04002509 i40evf_init_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002510 err = i40evf_request_misc_irq(adapter);
2511 if (err)
2512 goto err_sw_init;
2513
2514 netif_carrier_off(netdev);
2515
Mitch Williamsef8693e2014-02-13 03:48:53 -08002516 if (!adapter->netdev_registered) {
2517 err = register_netdev(netdev);
2518 if (err)
2519 goto err_register;
2520 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002521
2522 adapter->netdev_registered = true;
2523
2524 netif_tx_stop_all_queues(netdev);
2525
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002526 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002527 if (netdev->features & NETIF_F_GRO)
2528 dev_info(&pdev->dev, "GRO is enabled\n");
2529
Greg Rose5eae00c2013-12-21 06:12:45 +00002530 adapter->state = __I40EVF_DOWN;
2531 set_bit(__I40E_DOWN, &adapter->vsi.state);
2532 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002533
2534 if (RSS_AQ(adapter)) {
2535 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2536 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2537 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002538 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002539 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002540 return;
2541restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002542 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002543 return;
2544
2545err_register:
2546 i40evf_free_misc_irq(adapter);
2547err_sw_init:
2548 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002549err_alloc:
2550 kfree(adapter->vf_res);
2551 adapter->vf_res = NULL;
2552err:
2553 /* Things went into the weeds, so try again later */
2554 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002555 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002556 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002557 i40evf_shutdown_adminq(hw);
2558 adapter->state = __I40EVF_STARTUP;
2559 schedule_delayed_work(&adapter->init_task, HZ * 5);
2560 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002561 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002562 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002563}
2564
2565/**
2566 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2567 * @pdev: pci device structure
2568 **/
2569static void i40evf_shutdown(struct pci_dev *pdev)
2570{
2571 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002572 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002573
2574 netif_device_detach(netdev);
2575
2576 if (netif_running(netdev))
2577 i40evf_close(netdev);
2578
Mitch Williams00293fd2015-01-09 11:18:18 +00002579 /* Prevent the watchdog from running. */
2580 adapter->state = __I40EVF_REMOVE;
2581 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002582
Greg Rose5eae00c2013-12-21 06:12:45 +00002583#ifdef CONFIG_PM
2584 pci_save_state(pdev);
2585
2586#endif
2587 pci_disable_device(pdev);
2588}
2589
2590/**
2591 * i40evf_probe - Device Initialization Routine
2592 * @pdev: PCI device information struct
2593 * @ent: entry in i40evf_pci_tbl
2594 *
2595 * Returns 0 on success, negative on failure
2596 *
2597 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2598 * The OS initialization, configuring of the adapter private structure,
2599 * and a hardware reset occur.
2600 **/
2601static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2602{
2603 struct net_device *netdev;
2604 struct i40evf_adapter *adapter = NULL;
2605 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002606 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002607
2608 err = pci_enable_device(pdev);
2609 if (err)
2610 return err;
2611
Mitch Williams64942942014-02-11 08:26:33 +00002612 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002613 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002614 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2615 if (err) {
2616 dev_err(&pdev->dev,
2617 "DMA configuration failed: 0x%x\n", err);
2618 goto err_dma;
2619 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002620 }
2621
2622 err = pci_request_regions(pdev, i40evf_driver_name);
2623 if (err) {
2624 dev_err(&pdev->dev,
2625 "pci_request_regions failed 0x%x\n", err);
2626 goto err_pci_reg;
2627 }
2628
2629 pci_enable_pcie_error_reporting(pdev);
2630
2631 pci_set_master(pdev);
2632
Mitch Williams1255b7a2015-11-06 15:25:59 -08002633 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002634 if (!netdev) {
2635 err = -ENOMEM;
2636 goto err_alloc_etherdev;
2637 }
2638
2639 SET_NETDEV_DEV(netdev, &pdev->dev);
2640
2641 pci_set_drvdata(pdev, netdev);
2642 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002643
2644 adapter->netdev = netdev;
2645 adapter->pdev = pdev;
2646
2647 hw = &adapter->hw;
2648 hw->back = adapter;
2649
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002650 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002651 adapter->state = __I40EVF_STARTUP;
2652
2653 /* Call save state here because it relies on the adapter struct. */
2654 pci_save_state(pdev);
2655
2656 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2657 pci_resource_len(pdev, 0));
2658 if (!hw->hw_addr) {
2659 err = -EIO;
2660 goto err_ioremap;
2661 }
2662 hw->vendor_id = pdev->vendor;
2663 hw->device_id = pdev->device;
2664 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2665 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2666 hw->subsystem_device_id = pdev->subsystem_device;
2667 hw->bus.device = PCI_SLOT(pdev->devfn);
2668 hw->bus.func = PCI_FUNC(pdev->devfn);
2669
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002670 /* set up the locks for the AQ, do this only once in probe
2671 * and destroy them only once in remove
2672 */
2673 mutex_init(&hw->aq.asq_mutex);
2674 mutex_init(&hw->aq.arq_mutex);
2675
Serey Kong8bb1a542014-08-01 13:27:15 -07002676 INIT_LIST_HEAD(&adapter->mac_filter_list);
2677 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2678
Greg Rose5eae00c2013-12-21 06:12:45 +00002679 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2680 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2681 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2682 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002683 schedule_delayed_work(&adapter->init_task,
2684 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002685
2686 return 0;
2687
2688err_ioremap:
2689 free_netdev(netdev);
2690err_alloc_etherdev:
2691 pci_release_regions(pdev);
2692err_pci_reg:
2693err_dma:
2694 pci_disable_device(pdev);
2695 return err;
2696}
2697
2698#ifdef CONFIG_PM
2699/**
2700 * i40evf_suspend - Power management suspend routine
2701 * @pdev: PCI device information struct
2702 * @state: unused
2703 *
2704 * Called when the system (VM) is entering sleep/suspend.
2705 **/
2706static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2707{
2708 struct net_device *netdev = pci_get_drvdata(pdev);
2709 struct i40evf_adapter *adapter = netdev_priv(netdev);
2710 int retval = 0;
2711
2712 netif_device_detach(netdev);
2713
2714 if (netif_running(netdev)) {
2715 rtnl_lock();
2716 i40evf_down(adapter);
2717 rtnl_unlock();
2718 }
2719 i40evf_free_misc_irq(adapter);
2720 i40evf_reset_interrupt_capability(adapter);
2721
2722 retval = pci_save_state(pdev);
2723 if (retval)
2724 return retval;
2725
2726 pci_disable_device(pdev);
2727
2728 return 0;
2729}
2730
2731/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002732 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002733 * @pdev: PCI device information struct
2734 *
2735 * Called when the system (VM) is resumed from sleep/suspend.
2736 **/
2737static int i40evf_resume(struct pci_dev *pdev)
2738{
2739 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2740 struct net_device *netdev = adapter->netdev;
2741 u32 err;
2742
2743 pci_set_power_state(pdev, PCI_D0);
2744 pci_restore_state(pdev);
2745 /* pci_restore_state clears dev->state_saved so call
2746 * pci_save_state to restore it.
2747 */
2748 pci_save_state(pdev);
2749
2750 err = pci_enable_device_mem(pdev);
2751 if (err) {
2752 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2753 return err;
2754 }
2755 pci_set_master(pdev);
2756
2757 rtnl_lock();
2758 err = i40evf_set_interrupt_capability(adapter);
2759 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002760 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002761 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2762 return err;
2763 }
2764 err = i40evf_request_misc_irq(adapter);
2765 rtnl_unlock();
2766 if (err) {
2767 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2768 return err;
2769 }
2770
2771 schedule_work(&adapter->reset_task);
2772
2773 netif_device_attach(netdev);
2774
2775 return err;
2776}
2777
2778#endif /* CONFIG_PM */
2779/**
2780 * i40evf_remove - Device Removal Routine
2781 * @pdev: PCI device information struct
2782 *
2783 * i40evf_remove is called by the PCI subsystem to alert the driver
2784 * that it should release a PCI device. The could be caused by a
2785 * Hot-Plug event, or because the driver is going to be removed from
2786 * memory.
2787 **/
2788static void i40evf_remove(struct pci_dev *pdev)
2789{
2790 struct net_device *netdev = pci_get_drvdata(pdev);
2791 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002792 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002793 struct i40e_hw *hw = &adapter->hw;
2794
2795 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002796 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002797
2798 if (adapter->netdev_registered) {
2799 unregister_netdev(netdev);
2800 adapter->netdev_registered = false;
2801 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002802
Mitch Williamsf4a71882015-01-09 11:18:16 +00002803 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002804 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002805 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002806 i40evf_request_reset(adapter);
2807 msleep(20);
2808 /* If the FW isn't responding, kick it once, but only once. */
2809 if (!i40evf_asq_done(hw)) {
2810 i40evf_request_reset(adapter);
2811 msleep(20);
2812 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002813
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002814 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002815 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002816 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002817 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002818 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002819 }
2820
Mitch Williamse5d17c32014-06-04 04:22:38 +00002821 if (adapter->watchdog_timer.function)
2822 del_timer_sync(&adapter->watchdog_timer);
2823
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002824 flush_scheduled_work();
2825
Helin Zhang66f9af852015-10-26 19:44:34 -04002826 /* Clear user configurations for RSS */
2827 i40evf_clear_rss_config_user(&adapter->vsi);
2828
Greg Rose5eae00c2013-12-21 06:12:45 +00002829 if (hw->aq.asq.count)
2830 i40evf_shutdown_adminq(hw);
2831
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002832 /* destroy the locks only once, here */
2833 mutex_destroy(&hw->aq.arq_mutex);
2834 mutex_destroy(&hw->aq.asq_mutex);
2835
Greg Rose5eae00c2013-12-21 06:12:45 +00002836 iounmap(hw->hw_addr);
2837 pci_release_regions(pdev);
2838
Mitch Williamse284fc82015-03-27 00:12:09 -07002839 i40evf_free_all_tx_resources(adapter);
2840 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002841 i40evf_free_queues(adapter);
2842 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002843 /* If we got removed before an up/down sequence, we've got a filter
2844 * hanging out there that we need to get rid of.
2845 */
2846 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2847 list_del(&f->list);
2848 kfree(f);
2849 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002850 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2851 list_del(&f->list);
2852 kfree(f);
2853 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002854
2855 free_netdev(netdev);
2856
2857 pci_disable_pcie_error_reporting(pdev);
2858
2859 pci_disable_device(pdev);
2860}
2861
2862static struct pci_driver i40evf_driver = {
2863 .name = i40evf_driver_name,
2864 .id_table = i40evf_pci_tbl,
2865 .probe = i40evf_probe,
2866 .remove = i40evf_remove,
2867#ifdef CONFIG_PM
2868 .suspend = i40evf_suspend,
2869 .resume = i40evf_resume,
2870#endif
2871 .shutdown = i40evf_shutdown,
2872};
2873
2874/**
2875 * i40e_init_module - Driver Registration Routine
2876 *
2877 * i40e_init_module is the first routine called when the driver is
2878 * loaded. All it does is register with the PCI subsystem.
2879 **/
2880static int __init i40evf_init_module(void)
2881{
2882 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002883
Greg Rose5eae00c2013-12-21 06:12:45 +00002884 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002885 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002886
2887 pr_info("%s\n", i40evf_copyright);
2888
2889 ret = pci_register_driver(&i40evf_driver);
2890 return ret;
2891}
2892
2893module_init(i40evf_init_module);
2894
2895/**
2896 * i40e_exit_module - Driver Exit Cleanup Routine
2897 *
2898 * i40e_exit_module is called just before the driver is removed
2899 * from memory.
2900 **/
2901static void __exit i40evf_exit_module(void)
2902{
2903 pci_unregister_driver(&i40evf_driver);
2904}
2905
2906module_exit(i40evf_exit_module);
2907
2908/* i40evf_main.c */