blob: cbb7507d40f4c4feaa7d0c281bd5f4ab68bc8f9b [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Catherine Sullivaneaab59e2016-01-13 16:51:44 -08004 * Copyright(c) 2013 - 2016 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[] =
Catherine Sullivaneaab59e2016-01-13 16:51:44 -080035 "Intel(R) 40-10 Gigabit 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
Jesse Brandeburgc24215c2016-01-13 16:51:52 -080041#define DRV_VERSION_BUILD 9
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
Jesse Brandeburg2803b162015-12-22 14:25:08 -080072static struct workqueue_struct *i40evf_wq;
73
Greg Rose5eae00c2013-12-21 06:12:45 +000074/**
75 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
76 * @hw: pointer to the HW structure
77 * @mem: ptr to mem struct to fill out
78 * @size: size of memory requested
79 * @alignment: what to align the allocation to
80 **/
81i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
82 struct i40e_dma_mem *mem,
83 u64 size, u32 alignment)
84{
85 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
86
87 if (!mem)
88 return I40E_ERR_PARAM;
89
90 mem->size = ALIGN(size, alignment);
91 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
92 (dma_addr_t *)&mem->pa, GFP_KERNEL);
93 if (mem->va)
94 return 0;
95 else
96 return I40E_ERR_NO_MEMORY;
97}
98
99/**
100 * i40evf_free_dma_mem_d - OS specific memory free for shared code
101 * @hw: pointer to the HW structure
102 * @mem: ptr to mem struct to free
103 **/
104i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
105{
106 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
107
108 if (!mem || !mem->va)
109 return I40E_ERR_PARAM;
110 dma_free_coherent(&adapter->pdev->dev, mem->size,
111 mem->va, (dma_addr_t)mem->pa);
112 return 0;
113}
114
115/**
116 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
117 * @hw: pointer to the HW structure
118 * @mem: ptr to mem struct to fill out
119 * @size: size of memory requested
120 **/
121i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
122 struct i40e_virt_mem *mem, u32 size)
123{
124 if (!mem)
125 return I40E_ERR_PARAM;
126
127 mem->size = size;
128 mem->va = kzalloc(size, GFP_KERNEL);
129
130 if (mem->va)
131 return 0;
132 else
133 return I40E_ERR_NO_MEMORY;
134}
135
136/**
137 * i40evf_free_virt_mem_d - OS specific memory free for shared code
138 * @hw: pointer to the HW structure
139 * @mem: ptr to mem struct to free
140 **/
141i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
142 struct i40e_virt_mem *mem)
143{
144 if (!mem)
145 return I40E_ERR_PARAM;
146
147 /* it's ok to kfree a NULL pointer */
148 kfree(mem->va);
149
150 return 0;
151}
152
153/**
154 * i40evf_debug_d - OS dependent version of debug printing
155 * @hw: pointer to the HW structure
156 * @mask: debug level mask
157 * @fmt_str: printf-type format description
158 **/
159void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
160{
161 char buf[512];
162 va_list argptr;
163
164 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
165 return;
166
167 va_start(argptr, fmt_str);
168 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
169 va_end(argptr);
170
171 /* the debug string is already formatted with a newline */
172 pr_info("%s", buf);
173}
174
175/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800176 * i40evf_schedule_reset - Set the flags and schedule a reset event
177 * @adapter: board private structure
178 **/
179void i40evf_schedule_reset(struct i40evf_adapter *adapter)
180{
181 if (!(adapter->flags &
182 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
183 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
184 schedule_work(&adapter->reset_task);
185 }
186}
187
188/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000189 * i40evf_tx_timeout - Respond to a Tx Hang
190 * @netdev: network interface device structure
191 **/
192static void i40evf_tx_timeout(struct net_device *netdev)
193{
194 struct i40evf_adapter *adapter = netdev_priv(netdev);
195
196 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800197 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000198}
199
200/**
201 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
202 * @adapter: board private structure
203 **/
204static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
205{
206 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000207
Greg Rose5eae00c2013-12-21 06:12:45 +0000208 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
209
210 /* read flush */
211 rd32(hw, I40E_VFGEN_RSTAT);
212
213 synchronize_irq(adapter->msix_entries[0].vector);
214}
215
216/**
217 * i40evf_misc_irq_enable - Enable default interrupt generation settings
218 * @adapter: board private structure
219 **/
220static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
221{
222 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000223
Greg Rose5eae00c2013-12-21 06:12:45 +0000224 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
225 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400226 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000227
228 /* read flush */
229 rd32(hw, I40E_VFGEN_RSTAT);
230}
231
232/**
233 * i40evf_irq_disable - Mask off interrupt generation on the NIC
234 * @adapter: board private structure
235 **/
236static void i40evf_irq_disable(struct i40evf_adapter *adapter)
237{
238 int i;
239 struct i40e_hw *hw = &adapter->hw;
240
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800241 if (!adapter->msix_entries)
242 return;
243
Greg Rose5eae00c2013-12-21 06:12:45 +0000244 for (i = 1; i < adapter->num_msix_vectors; i++) {
245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
246 synchronize_irq(adapter->msix_entries[i].vector);
247 }
248 /* read flush */
249 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000250}
251
252/**
253 * i40evf_irq_enable_queues - Enable interrupt for specified queues
254 * @adapter: board private structure
255 * @mask: bitmap of queues to enable
256 **/
257void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
258{
259 struct i40e_hw *hw = &adapter->hw;
260 int i;
261
262 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400263 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000264 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
265 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000266 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400267 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000268 }
269 }
270}
271
272/**
273 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
274 * @adapter: board private structure
275 * @mask: bitmap of vectors to trigger
276 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000277static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000278{
279 struct i40e_hw *hw = &adapter->hw;
280 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800281 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000282
Mitch Williams164ec1b2014-06-04 08:45:19 +0000283 if (mask & 1) {
284 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400285 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000286 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400287 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000288 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
289 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000290 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400291 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000292 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400293 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000294 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400295 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000296 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
297 }
298 }
299}
300
301/**
302 * i40evf_irq_enable - Enable default interrupt generation settings
303 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600304 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000305 **/
306void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
307{
308 struct i40e_hw *hw = &adapter->hw;
309
Mitch Williams164ec1b2014-06-04 08:45:19 +0000310 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000311 i40evf_irq_enable_queues(adapter, ~0);
312
313 if (flush)
314 rd32(hw, I40E_VFGEN_RSTAT);
315}
316
317/**
318 * i40evf_msix_aq - Interrupt handler for vector 0
319 * @irq: interrupt number
320 * @data: pointer to netdev
321 **/
322static irqreturn_t i40evf_msix_aq(int irq, void *data)
323{
324 struct net_device *netdev = data;
325 struct i40evf_adapter *adapter = netdev_priv(netdev);
326 struct i40e_hw *hw = &adapter->hw;
327 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000328
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700329 /* handle non-queue interrupts, these reads clear the registers */
330 val = rd32(hw, I40E_VFINT_ICR01);
331 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000332
Jean Sacrened17f7e2015-10-13 01:06:30 -0600333 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
334 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000335 wr32(hw, I40E_VFINT_DYN_CTL01, val);
336
Greg Rose5eae00c2013-12-21 06:12:45 +0000337 /* schedule work on the private workqueue */
338 schedule_work(&adapter->adminq_task);
339
340 return IRQ_HANDLED;
341}
342
343/**
344 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
345 * @irq: interrupt number
346 * @data: pointer to a q_vector
347 **/
348static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
349{
350 struct i40e_q_vector *q_vector = data;
351
352 if (!q_vector->tx.ring && !q_vector->rx.ring)
353 return IRQ_HANDLED;
354
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700355 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000356
357 return IRQ_HANDLED;
358}
359
360/**
361 * i40evf_map_vector_to_rxq - associate irqs with rx queues
362 * @adapter: board private structure
363 * @v_idx: interrupt number
364 * @r_idx: queue number
365 **/
366static void
367i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
368{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400369 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400370 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000371
372 rx_ring->q_vector = q_vector;
373 rx_ring->next = q_vector->rx.ring;
374 rx_ring->vsi = &adapter->vsi;
375 q_vector->rx.ring = rx_ring;
376 q_vector->rx.count++;
377 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400378 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000379}
380
381/**
382 * i40evf_map_vector_to_txq - associate irqs with tx queues
383 * @adapter: board private structure
384 * @v_idx: interrupt number
385 * @t_idx: queue number
386 **/
387static void
388i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
389{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400390 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400391 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000392
393 tx_ring->q_vector = q_vector;
394 tx_ring->next = q_vector->tx.ring;
395 tx_ring->vsi = &adapter->vsi;
396 q_vector->tx.ring = tx_ring;
397 q_vector->tx.count++;
398 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400399 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000400 q_vector->num_ringpairs++;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400401 q_vector->ring_mask |= BIT(t_idx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000402}
403
404/**
405 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
406 * @adapter: board private structure to initialize
407 *
408 * This function maps descriptor rings to the queue-specific vectors
409 * we were allotted through the MSI-X enabling code. Ideally, we'd have
410 * one vector per ring/queue, but on a constrained vector budget, we
411 * group the rings as "efficiently" as possible. You would add new
412 * mapping configurations in here.
413 **/
414static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
415{
416 int q_vectors;
417 int v_start = 0;
418 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000419 int rxr_remaining = adapter->num_active_queues;
420 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000421 int i, j;
422 int rqpv, tqpv;
423 int err = 0;
424
425 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
426
427 /* The ideal configuration...
428 * We have enough vectors to map one per queue.
429 */
Mitch Williams973371d2015-04-27 14:57:09 -0400430 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000431 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
432 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
433
434 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
435 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
436 goto out;
437 }
438
439 /* If we don't have enough vectors for a 1-to-1
440 * mapping, we'll have to group them so there are
441 * multiple queues per vector.
442 * Re-adjusting *qpv takes care of the remainder.
443 */
444 for (i = v_start; i < q_vectors; i++) {
445 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
446 for (j = 0; j < rqpv; j++) {
447 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
448 rxr_idx++;
449 rxr_remaining--;
450 }
451 }
452 for (i = v_start; i < q_vectors; i++) {
453 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
454 for (j = 0; j < tqpv; j++) {
455 i40evf_map_vector_to_txq(adapter, i, txr_idx);
456 txr_idx++;
457 txr_remaining--;
458 }
459 }
460
461out:
462 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
463
464 return err;
465}
466
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700467#ifdef CONFIG_NET_POLL_CONTROLLER
468/**
469 * i40evf_netpoll - A Polling 'interrupt' handler
470 * @netdev: network interface device structure
471 *
472 * This is used by netconsole to send skbs without having to re-enable
473 * interrupts. It's not called while the normal interrupt routine is executing.
474 **/
475static void i40evf_netpoll(struct net_device *netdev)
476{
477 struct i40evf_adapter *adapter = netdev_priv(netdev);
478 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
479 int i;
480
481 /* if interface is down do nothing */
482 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
483 return;
484
485 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400486 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700487}
488
489#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000490/**
491 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
492 * @adapter: board private structure
493 *
494 * Allocates MSI-X vectors for tx and rx handling, and requests
495 * interrupts from the kernel.
496 **/
497static int
498i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
499{
500 int vector, err, q_vectors;
501 int rx_int_idx = 0, tx_int_idx = 0;
502
503 i40evf_irq_disable(adapter);
504 /* Decrement for Other and TCP Timer vectors */
505 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
506
507 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400508 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Greg Rose5eae00c2013-12-21 06:12:45 +0000509
510 if (q_vector->tx.ring && q_vector->rx.ring) {
511 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
512 "i40evf-%s-%s-%d", basename,
513 "TxRx", rx_int_idx++);
514 tx_int_idx++;
515 } else if (q_vector->rx.ring) {
516 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
517 "i40evf-%s-%s-%d", basename,
518 "rx", rx_int_idx++);
519 } else if (q_vector->tx.ring) {
520 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
521 "i40evf-%s-%s-%d", basename,
522 "tx", tx_int_idx++);
523 } else {
524 /* skip this unused q_vector */
525 continue;
526 }
527 err = request_irq(
528 adapter->msix_entries[vector + NONQ_VECS].vector,
529 i40evf_msix_clean_rings,
530 0,
531 q_vector->name,
532 q_vector);
533 if (err) {
534 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400535 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000536 goto free_queue_irqs;
537 }
538 /* assign the mask for this irq */
539 irq_set_affinity_hint(
540 adapter->msix_entries[vector + NONQ_VECS].vector,
541 q_vector->affinity_mask);
542 }
543
544 return 0;
545
546free_queue_irqs:
547 while (vector) {
548 vector--;
549 irq_set_affinity_hint(
550 adapter->msix_entries[vector + NONQ_VECS].vector,
551 NULL);
552 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400553 &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000554 }
555 return err;
556}
557
558/**
559 * i40evf_request_misc_irq - Initialize MSI-X interrupts
560 * @adapter: board private structure
561 *
562 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
563 * vector is only for the admin queue, and stays active even when the netdev
564 * is closed.
565 **/
566static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
567{
568 struct net_device *netdev = adapter->netdev;
569 int err;
570
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700571 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000572 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
573 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000574 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800575 &i40evf_msix_aq, 0,
576 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000577 if (err) {
578 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800579 "request_irq for %s failed: %d\n",
580 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000581 free_irq(adapter->msix_entries[0].vector, netdev);
582 }
583 return err;
584}
585
586/**
587 * i40evf_free_traffic_irqs - Free MSI-X interrupts
588 * @adapter: board private structure
589 *
590 * Frees all MSI-X vectors other than 0.
591 **/
592static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
593{
594 int i;
595 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000596
Greg Rose5eae00c2013-12-21 06:12:45 +0000597 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
598
599 for (i = 0; i < q_vectors; i++) {
600 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
601 NULL);
602 free_irq(adapter->msix_entries[i+1].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400603 &adapter->q_vectors[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000604 }
605}
606
607/**
608 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
609 * @adapter: board private structure
610 *
611 * Frees MSI-X vector 0.
612 **/
613static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
614{
615 struct net_device *netdev = adapter->netdev;
616
617 free_irq(adapter->msix_entries[0].vector, netdev);
618}
619
620/**
621 * i40evf_configure_tx - Configure Transmit Unit after Reset
622 * @adapter: board private structure
623 *
624 * Configure the Tx unit of the MAC after a reset.
625 **/
626static void i40evf_configure_tx(struct i40evf_adapter *adapter)
627{
628 struct i40e_hw *hw = &adapter->hw;
629 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000630
Mitch Williamscc052922014-10-25 03:24:34 +0000631 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400632 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000633}
634
635/**
636 * i40evf_configure_rx - Configure Receive Unit after Reset
637 * @adapter: board private structure
638 *
639 * Configure the Rx unit of the MAC after a reset.
640 **/
641static void i40evf_configure_rx(struct i40evf_adapter *adapter)
642{
643 struct i40e_hw *hw = &adapter->hw;
644 struct net_device *netdev = adapter->netdev;
645 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
646 int i;
647 int rx_buf_len;
648
649
Greg Rose5eae00c2013-12-21 06:12:45 +0000650 /* Set the RX buffer length according to the mode */
Mitch Williams00e5ec42016-01-15 14:33:10 -0800651 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED ||
652 netdev->mtu <= ETH_DATA_LEN)
653 rx_buf_len = I40EVF_RXBUFFER_2048;
654 else
655 rx_buf_len = ALIGN(max_frame, 1024);
Greg Rose5eae00c2013-12-21 06:12:45 +0000656
Mitch Williamscc052922014-10-25 03:24:34 +0000657 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400658 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
659 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800660 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
661 set_ring_ps_enabled(&adapter->rx_rings[i]);
662 adapter->rx_rings[i].rx_hdr_len = I40E_RX_HDR_SIZE;
663 } else {
664 clear_ring_ps_enabled(&adapter->rx_rings[i]);
665 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000666 }
667}
668
669/**
670 * i40evf_find_vlan - Search filter list for specific vlan filter
671 * @adapter: board private structure
672 * @vlan: vlan tag
673 *
674 * Returns ptr to the filter object or NULL
675 **/
676static struct
677i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
678{
679 struct i40evf_vlan_filter *f;
680
681 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
682 if (vlan == f->vlan)
683 return f;
684 }
685 return NULL;
686}
687
688/**
689 * i40evf_add_vlan - Add a vlan filter to the list
690 * @adapter: board private structure
691 * @vlan: VLAN tag
692 *
693 * Returns ptr to the filter object or NULL when no memory available.
694 **/
695static struct
696i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
697{
Mitch Williams13acb542015-03-31 00:45:05 -0700698 struct i40evf_vlan_filter *f = NULL;
699 int count = 50;
700
701 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
702 &adapter->crit_section)) {
703 udelay(1);
704 if (--count == 0)
705 goto out;
706 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000707
708 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000709 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000710 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000711 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700712 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000713
Greg Rose5eae00c2013-12-21 06:12:45 +0000714 f->vlan = vlan;
715
716 INIT_LIST_HEAD(&f->list);
717 list_add(&f->list, &adapter->vlan_filter_list);
718 f->add = true;
719 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
720 }
721
Mitch Williams13acb542015-03-31 00:45:05 -0700722clearout:
723 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
724out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000725 return f;
726}
727
728/**
729 * i40evf_del_vlan - Remove a vlan filter from the list
730 * @adapter: board private structure
731 * @vlan: VLAN tag
732 **/
733static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
734{
735 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700736 int count = 50;
737
738 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
739 &adapter->crit_section)) {
740 udelay(1);
741 if (--count == 0)
742 return;
743 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000744
745 f = i40evf_find_vlan(adapter, vlan);
746 if (f) {
747 f->remove = true;
748 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
749 }
Mitch Williams13acb542015-03-31 00:45:05 -0700750 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000751}
752
753/**
754 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
755 * @netdev: network device struct
756 * @vid: VLAN tag
757 **/
758static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000759 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000760{
761 struct i40evf_adapter *adapter = netdev_priv(netdev);
762
Mitch Williams8ed995f2015-08-28 17:55:57 -0400763 if (!VLAN_ALLOWED(adapter))
764 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000765 if (i40evf_add_vlan(adapter, vid) == NULL)
766 return -ENOMEM;
767 return 0;
768}
769
770/**
771 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
772 * @netdev: network device struct
773 * @vid: VLAN tag
774 **/
775static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000776 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000777{
778 struct i40evf_adapter *adapter = netdev_priv(netdev);
779
Mitch Williams8ed995f2015-08-28 17:55:57 -0400780 if (VLAN_ALLOWED(adapter)) {
781 i40evf_del_vlan(adapter, vid);
782 return 0;
783 }
784 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000785}
786
787/**
788 * i40evf_find_filter - Search filter list for specific mac filter
789 * @adapter: board private structure
790 * @macaddr: the MAC address
791 *
792 * Returns ptr to the filter object or NULL
793 **/
794static struct
795i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
796 u8 *macaddr)
797{
798 struct i40evf_mac_filter *f;
799
800 if (!macaddr)
801 return NULL;
802
803 list_for_each_entry(f, &adapter->mac_filter_list, list) {
804 if (ether_addr_equal(macaddr, f->macaddr))
805 return f;
806 }
807 return NULL;
808}
809
810/**
811 * i40e_add_filter - Add a mac filter to the filter list
812 * @adapter: board private structure
813 * @macaddr: the MAC address
814 *
815 * Returns ptr to the filter object or NULL when no memory available.
816 **/
817static struct
818i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
819 u8 *macaddr)
820{
821 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000822 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000823
824 if (!macaddr)
825 return NULL;
826
827 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000828 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000829 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000830 if (--count == 0)
831 return NULL;
832 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000833
834 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000835 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000836 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000837 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000838 clear_bit(__I40EVF_IN_CRITICAL_TASK,
839 &adapter->crit_section);
840 return NULL;
841 }
842
Greg Rose9a173902014-05-22 06:32:02 +0000843 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000844
845 list_add(&f->list, &adapter->mac_filter_list);
846 f->add = true;
847 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
848 }
849
850 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
851 return f;
852}
853
854/**
855 * i40evf_set_mac - NDO callback to set port mac address
856 * @netdev: network interface device structure
857 * @p: pointer to an address structure
858 *
859 * Returns 0 on success, negative on failure
860 **/
861static int i40evf_set_mac(struct net_device *netdev, void *p)
862{
863 struct i40evf_adapter *adapter = netdev_priv(netdev);
864 struct i40e_hw *hw = &adapter->hw;
865 struct i40evf_mac_filter *f;
866 struct sockaddr *addr = p;
867
868 if (!is_valid_ether_addr(addr->sa_data))
869 return -EADDRNOTAVAIL;
870
871 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
872 return 0;
873
Mitch Williams14e52ee2015-08-31 19:54:44 -0400874 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
875 return -EPERM;
876
877 f = i40evf_find_filter(adapter, hw->mac.addr);
878 if (f) {
879 f->remove = true;
880 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
881 }
882
Greg Rose5eae00c2013-12-21 06:12:45 +0000883 f = i40evf_add_filter(adapter, addr->sa_data);
884 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000885 ether_addr_copy(hw->mac.addr, addr->sa_data);
886 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000887 }
888
889 return (f == NULL) ? -ENOMEM : 0;
890}
891
892/**
893 * i40evf_set_rx_mode - NDO callback to set the netdev filters
894 * @netdev: network interface device structure
895 **/
896static void i40evf_set_rx_mode(struct net_device *netdev)
897{
898 struct i40evf_adapter *adapter = netdev_priv(netdev);
899 struct i40evf_mac_filter *f, *ftmp;
900 struct netdev_hw_addr *uca;
901 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400902 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000903 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000904
905 /* add addr if not already in the filter list */
906 netdev_for_each_uc_addr(uca, netdev) {
907 i40evf_add_filter(adapter, uca->addr);
908 }
909 netdev_for_each_mc_addr(mca, netdev) {
910 i40evf_add_filter(adapter, mca->addr);
911 }
912
913 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000914 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000915 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000916 if (--count == 0) {
917 dev_err(&adapter->pdev->dev,
918 "Failed to get lock in %s\n", __func__);
919 return;
920 }
921 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000922 /* remove filter if not in netdev list */
923 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400924 netdev_for_each_mc_addr(mca, netdev)
925 if (ether_addr_equal(mca->addr, f->macaddr))
926 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000927
Shannon Nelson2f41f332015-08-26 15:14:20 -0400928 netdev_for_each_uc_addr(uca, netdev)
929 if (ether_addr_equal(uca->addr, f->macaddr))
930 goto bottom_of_search_loop;
931
932 for_each_dev_addr(netdev, ha)
933 if (ether_addr_equal(ha->addr, f->macaddr))
934 goto bottom_of_search_loop;
935
936 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
937 goto bottom_of_search_loop;
938
939 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
940 f->remove = true;
941 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
942
943bottom_of_search_loop:
944 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000945 }
946 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
947}
948
949/**
950 * i40evf_napi_enable_all - enable NAPI on all queue vectors
951 * @adapter: board private structure
952 **/
953static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
954{
955 int q_idx;
956 struct i40e_q_vector *q_vector;
957 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
958
959 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
960 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000961
Mitch Williams7d96ba12015-10-26 19:44:39 -0400962 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000963 napi = &q_vector->napi;
964 napi_enable(napi);
965 }
966}
967
968/**
969 * i40evf_napi_disable_all - disable NAPI on all queue vectors
970 * @adapter: board private structure
971 **/
972static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
973{
974 int q_idx;
975 struct i40e_q_vector *q_vector;
976 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
977
978 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400979 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000980 napi_disable(&q_vector->napi);
981 }
982}
983
984/**
985 * i40evf_configure - set up transmit and receive data structures
986 * @adapter: board private structure
987 **/
988static void i40evf_configure(struct i40evf_adapter *adapter)
989{
990 struct net_device *netdev = adapter->netdev;
991 int i;
992
993 i40evf_set_rx_mode(netdev);
994
995 i40evf_configure_tx(adapter);
996 i40evf_configure_rx(adapter);
997 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
998
Mitch Williamscc052922014-10-25 03:24:34 +0000999 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001000 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001001
Mitch Williams00e5ec42016-01-15 14:33:10 -08001002 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
1003 i40evf_alloc_rx_headers(ring);
1004 i40evf_alloc_rx_buffers_ps(ring, ring->count);
1005 } else {
Mitch Williamsa132af22015-01-24 09:58:35 +00001006 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Mitch Williams00e5ec42016-01-15 14:33:10 -08001007 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001008 ring->next_to_use = ring->count - 1;
1009 writel(ring->next_to_use, ring->tail);
1010 }
1011}
1012
1013/**
1014 * i40evf_up_complete - Finish the last steps of bringing up a connection
1015 * @adapter: board private structure
1016 **/
1017static int i40evf_up_complete(struct i40evf_adapter *adapter)
1018{
1019 adapter->state = __I40EVF_RUNNING;
1020 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1021
1022 i40evf_napi_enable_all(adapter);
1023
1024 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1025 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1026 return 0;
1027}
1028
1029/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001030 * i40e_down - Shutdown the connection processing
1031 * @adapter: board private structure
1032 **/
1033void i40evf_down(struct i40evf_adapter *adapter)
1034{
1035 struct net_device *netdev = adapter->netdev;
1036 struct i40evf_mac_filter *f;
1037
Mitch Williams209dc4d2015-12-09 15:50:27 -08001038 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001039 return;
1040
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001041 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1042 &adapter->crit_section))
1043 usleep_range(500, 1000);
1044
Mitch Williams63e18c22015-03-27 00:12:10 -07001045 netif_carrier_off(netdev);
1046 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +00001047 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001048 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001049
Mitch Williamsef8693e2014-02-13 03:48:53 -08001050 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001051 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1052 f->remove = true;
1053 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001054 /* remove all VLAN filters */
1055 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1056 f->remove = true;
1057 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001058 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1059 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001060 /* cancel any current operation */
1061 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001062 /* Schedule operations to close down the HW. Don't wait
1063 * here for this to complete. The watchdog is still running
1064 * and it will take care of this.
1065 */
1066 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001067 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001068 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001069 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001070
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001071 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001072}
1073
1074/**
1075 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1076 * @adapter: board private structure
1077 * @vectors: number of vectors to request
1078 *
1079 * Work with the OS to set up the MSIX vectors needed.
1080 *
1081 * Returns 0 on success, negative on failure
1082 **/
1083static int
1084i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1085{
1086 int err, vector_threshold;
1087
1088 /* We'll want at least 3 (vector_threshold):
1089 * 0) Other (Admin Queue and link, mostly)
1090 * 1) TxQ[0] Cleanup
1091 * 2) RxQ[0] Cleanup
1092 */
1093 vector_threshold = MIN_MSIX_COUNT;
1094
1095 /* The more we get, the more we will assign to Tx/Rx Cleanup
1096 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1097 * Right now, we simply care about how many we'll get; we'll
1098 * set them up later while requesting irq's.
1099 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001100 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1101 vector_threshold, vectors);
1102 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001103 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001104 kfree(adapter->msix_entries);
1105 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001106 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001107 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001108
1109 /* Adjust for only the vectors we'll use, which is minimum
1110 * of max_msix_q_vectors + NONQ_VECS, or the number of
1111 * vectors we were allocated.
1112 */
1113 adapter->num_msix_vectors = err;
1114 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001115}
1116
1117/**
1118 * i40evf_free_queues - Free memory for all rings
1119 * @adapter: board private structure to initialize
1120 *
1121 * Free all of the memory associated with queue pairs.
1122 **/
1123static void i40evf_free_queues(struct i40evf_adapter *adapter)
1124{
Greg Rose5eae00c2013-12-21 06:12:45 +00001125 if (!adapter->vsi_res)
1126 return;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001127 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001128 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001129 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001130 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001131}
1132
1133/**
1134 * i40evf_alloc_queues - Allocate memory for all rings
1135 * @adapter: board private structure to initialize
1136 *
1137 * We allocate one ring per queue at run-time since we don't know the
1138 * number of queues at compile-time. The polling_netdev array is
1139 * intended for Multiqueue, but should work fine with a single queue.
1140 **/
1141static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1142{
1143 int i;
1144
Mitch Williams0dd438d2015-10-26 19:44:40 -04001145 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1146 sizeof(struct i40e_ring), GFP_KERNEL);
1147 if (!adapter->tx_rings)
1148 goto err_out;
1149 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1150 sizeof(struct i40e_ring), GFP_KERNEL);
1151 if (!adapter->rx_rings)
1152 goto err_out;
1153
Mitch Williamscc052922014-10-25 03:24:34 +00001154 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001155 struct i40e_ring *tx_ring;
1156 struct i40e_ring *rx_ring;
1157
Mitch Williams0dd438d2015-10-26 19:44:40 -04001158 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001159
1160 tx_ring->queue_index = i;
1161 tx_ring->netdev = adapter->netdev;
1162 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001163 tx_ring->count = adapter->tx_desc_count;
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001164 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1165 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001166
Mitch Williams0dd438d2015-10-26 19:44:40 -04001167 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001168 rx_ring->queue_index = i;
1169 rx_ring->netdev = adapter->netdev;
1170 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001171 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001172 }
1173
1174 return 0;
1175
1176err_out:
1177 i40evf_free_queues(adapter);
1178 return -ENOMEM;
1179}
1180
1181/**
1182 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1183 * @adapter: board private structure to initialize
1184 *
1185 * Attempt to configure the interrupts using the best available
1186 * capabilities of the hardware and the kernel.
1187 **/
1188static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1189{
1190 int vector, v_budget;
1191 int pairs = 0;
1192 int err = 0;
1193
1194 if (!adapter->vsi_res) {
1195 err = -EIO;
1196 goto out;
1197 }
Mitch Williamscc052922014-10-25 03:24:34 +00001198 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001199
1200 /* It's easy to be greedy for MSI-X vectors, but it really
1201 * doesn't do us much good if we have a lot more vectors
1202 * than CPU's. So let's be conservative and only ask for
1203 * (roughly) twice the number of vectors as there are CPU's.
1204 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001205 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1206 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001207
Greg Rose5eae00c2013-12-21 06:12:45 +00001208 adapter->msix_entries = kcalloc(v_budget,
1209 sizeof(struct msix_entry), GFP_KERNEL);
1210 if (!adapter->msix_entries) {
1211 err = -ENOMEM;
1212 goto out;
1213 }
1214
1215 for (vector = 0; vector < v_budget; vector++)
1216 adapter->msix_entries[vector].entry = vector;
1217
Mitch Williams313ed2d2015-08-27 11:42:31 -04001218 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001219
1220out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001221 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1222 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001223 return err;
1224}
1225
1226/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001227 * i40e_config_rss_aq - Prepare for RSS using AQ commands
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001228 * @vsi: vsi structure
1229 * @seed: RSS hash seed
Helin Zhang2c86ac32015-10-27 16:15:06 -04001230 * @lut: Lookup table
1231 * @lut_size: Lookup table size
1232 *
1233 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001234 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001235static int i40evf_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1236 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001237{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001238 struct i40evf_adapter *adapter = vsi->back;
1239 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001240 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001241
1242 if (!vsi->id)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001243 return -EINVAL;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001244
1245 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1246 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001247 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001248 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001249 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001250 }
1251
Helin Zhang2c86ac32015-10-27 16:15:06 -04001252 if (seed) {
1253 struct i40e_aqc_get_set_rss_key_data *rss_key =
1254 (struct i40e_aqc_get_set_rss_key_data *)seed;
1255 ret = i40evf_aq_set_rss_key(hw, vsi->id, rss_key);
1256 if (ret) {
1257 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1258 i40evf_stat_str(hw, ret),
1259 i40evf_aq_str(hw, hw->aq.asq_last_status));
1260 return ret;
1261 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001262 }
1263
Helin Zhang2c86ac32015-10-27 16:15:06 -04001264 if (lut) {
1265 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, lut, lut_size);
1266 if (ret) {
1267 dev_err(&adapter->pdev->dev,
1268 "Cannot set RSS lut, err %s aq_err %s\n",
1269 i40evf_stat_str(hw, ret),
1270 i40evf_aq_str(hw, hw->aq.asq_last_status));
1271 return ret;
1272 }
1273 }
1274
1275 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001276}
1277
1278/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001279 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1280 * @vsi: Pointer to vsi structure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001281 * @seed: RSS hash seed
Helin Zhang2c86ac32015-10-27 16:15:06 -04001282 * @lut: Lookup table
1283 * @lut_size: Lookup table size
1284 *
1285 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001286 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001287static int i40evf_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
1288 const u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001289{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001290 struct i40evf_adapter *adapter = vsi->back;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001291 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001292 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001293
Helin Zhang2c86ac32015-10-27 16:15:06 -04001294 if (seed) {
1295 u32 *seed_dw = (u32 *)seed;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001296
Helin Zhang2c86ac32015-10-27 16:15:06 -04001297 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1298 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1299 }
1300
1301 if (lut) {
1302 u32 *lut_dw = (u32 *)lut;
1303
1304 if (lut_size != I40EVF_HLUT_ARRAY_SIZE)
1305 return -EINVAL;
1306
1307 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
1308 wr32(hw, I40E_VFQF_HLUT(i), lut_dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001309 }
1310 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001311
1312 return 0;
1313}
1314
1315/**
Helin Zhang90b02b42015-10-26 19:44:33 -04001316 * * i40evf_get_rss_aq - Get RSS keys and lut by using AQ commands
1317 * @vsi: Pointer to vsi structure
1318 * @seed: RSS hash seed
1319 * @lut: Lookup table
1320 * @lut_size: Lookup table size
1321 *
1322 * Return 0 on success, negative on failure
1323 **/
1324static int i40evf_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1325 u8 *lut, u16 lut_size)
1326{
1327 struct i40evf_adapter *adapter = vsi->back;
1328 struct i40e_hw *hw = &adapter->hw;
1329 int ret = 0;
1330
1331 if (seed) {
1332 ret = i40evf_aq_get_rss_key(hw, vsi->id,
1333 (struct i40e_aqc_get_set_rss_key_data *)seed);
1334 if (ret) {
1335 dev_err(&adapter->pdev->dev,
1336 "Cannot get RSS key, err %s aq_err %s\n",
1337 i40evf_stat_str(hw, ret),
1338 i40evf_aq_str(hw, hw->aq.asq_last_status));
1339 return ret;
1340 }
1341 }
1342
1343 if (lut) {
1344 ret = i40evf_aq_get_rss_lut(hw, vsi->id, seed, lut, lut_size);
1345 if (ret) {
1346 dev_err(&adapter->pdev->dev,
1347 "Cannot get RSS lut, err %s aq_err %s\n",
1348 i40evf_stat_str(hw, ret),
1349 i40evf_aq_str(hw, hw->aq.asq_last_status));
1350 return ret;
1351 }
1352 }
1353
1354 return ret;
1355}
1356
1357/**
1358 * * i40evf_get_rss_reg - Get RSS keys and lut by reading registers
1359 * @vsi: Pointer to vsi structure
1360 * @seed: RSS hash seed
1361 * @lut: Lookup table
1362 * @lut_size: Lookup table size
1363 *
1364 * Returns 0 on success, negative on failure
1365 **/
1366static int i40evf_get_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
1367 const u8 *lut, u16 lut_size)
1368{
1369 struct i40evf_adapter *adapter = vsi->back;
1370 struct i40e_hw *hw = &adapter->hw;
1371 u16 i;
1372
1373 if (seed) {
1374 u32 *seed_dw = (u32 *)seed;
1375
1376 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1377 seed_dw[i] = rd32(hw, I40E_VFQF_HKEY(i));
1378 }
1379
1380 if (lut) {
1381 u32 *lut_dw = (u32 *)lut;
1382
1383 if (lut_size != I40EVF_HLUT_ARRAY_SIZE)
1384 return -EINVAL;
1385
1386 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
1387 lut_dw[i] = rd32(hw, I40E_VFQF_HLUT(i));
1388 }
1389
1390 return 0;
1391}
1392
1393/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001394 * i40evf_config_rss - Configure RSS keys and lut
1395 * @vsi: Pointer to vsi structure
1396 * @seed: RSS hash seed
1397 * @lut: Lookup table
1398 * @lut_size: Lookup table size
1399 *
1400 * Returns 0 on success, negative on failure
1401 **/
1402int i40evf_config_rss(struct i40e_vsi *vsi, const u8 *seed,
1403 u8 *lut, u16 lut_size)
1404{
1405 struct i40evf_adapter *adapter = vsi->back;
1406
1407 if (RSS_AQ(adapter))
1408 return i40evf_config_rss_aq(vsi, seed, lut, lut_size);
1409 else
1410 return i40evf_config_rss_reg(vsi, seed, lut, lut_size);
1411}
1412
1413/**
Helin Zhang90b02b42015-10-26 19:44:33 -04001414 * i40evf_get_rss - Get RSS keys and lut
1415 * @vsi: Pointer to vsi structure
1416 * @seed: RSS hash seed
1417 * @lut: Lookup table
1418 * @lut_size: Lookup table size
1419 *
1420 * Returns 0 on success, negative on failure
1421 **/
1422int i40evf_get_rss(struct i40e_vsi *vsi, const u8 *seed, u8 *lut, u16 lut_size)
1423{
1424 struct i40evf_adapter *adapter = vsi->back;
1425
1426 if (RSS_AQ(adapter))
1427 return i40evf_get_rss_aq(vsi, seed, lut, lut_size);
1428 else
1429 return i40evf_get_rss_reg(vsi, seed, lut, lut_size);
1430}
1431
1432/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001433 * i40evf_fill_rss_lut - Fill the lut with default values
1434 * @lut: Lookup table to be filled with
1435 * @rss_table_size: Lookup table size
1436 * @rss_size: Range of queue number for hashing
1437 **/
1438static void i40evf_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
1439{
1440 u16 i;
1441
1442 for (i = 0; i < rss_table_size; i++)
1443 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001444}
1445
1446/**
Helin Zhang96a81982015-10-26 19:44:31 -04001447 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001448 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001449 *
1450 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001451 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001452static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001453{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001454 struct i40e_vsi *vsi = &adapter->vsi;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001455 struct i40e_hw *hw = &adapter->hw;
1456 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1457 u64 hena;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001458 u8 *lut;
1459 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001460
1461 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Anjali Singhai Jain3d0da5b2015-12-22 14:25:05 -08001462 if (adapter->vf_res->vf_offload_flags &
1463 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1464 hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1465 else
1466 hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001467 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1468 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1469
Helin Zhang2c86ac32015-10-27 16:15:06 -04001470 lut = kzalloc(I40EVF_HLUT_ARRAY_SIZE, GFP_KERNEL);
1471 if (!lut)
1472 return -ENOMEM;
Helin Zhang66f9af852015-10-26 19:44:34 -04001473
1474 /* Use user configured lut if there is one, otherwise use default */
1475 if (vsi->rss_lut_user)
1476 memcpy(lut, vsi->rss_lut_user, I40EVF_HLUT_ARRAY_SIZE);
1477 else
1478 i40evf_fill_rss_lut(lut, I40EVF_HLUT_ARRAY_SIZE,
1479 adapter->num_active_queues);
1480
1481 /* Use user configured hash key if there is one, otherwise
1482 * user default.
1483 */
1484 if (vsi->rss_hkey_user)
1485 memcpy(seed, vsi->rss_hkey_user, I40EVF_HKEY_ARRAY_SIZE);
1486 else
1487 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001488 ret = i40evf_config_rss(vsi, seed, lut, I40EVF_HLUT_ARRAY_SIZE);
1489 kfree(lut);
1490
1491 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001492}
1493
1494/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001495 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1496 * @adapter: board private structure to initialize
1497 *
1498 * We allocate one q_vector per queue interrupt. If allocation fails we
1499 * return -ENOMEM.
1500 **/
1501static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1502{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001503 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001504 struct i40e_q_vector *q_vector;
1505
1506 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001507 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001508 GFP_KERNEL);
1509 if (!adapter->q_vectors)
1510 goto err_out;
Greg Rose5eae00c2013-12-21 06:12:45 +00001511
1512 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001513 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001514 q_vector->adapter = adapter;
1515 q_vector->vsi = &adapter->vsi;
1516 q_vector->v_idx = q_idx;
1517 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001518 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001519 }
1520
1521 return 0;
1522
1523err_out:
1524 while (q_idx) {
1525 q_idx--;
Mitch Williams7d96ba12015-10-26 19:44:39 -04001526 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001527 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001528 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001529 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001530 return -ENOMEM;
1531}
1532
1533/**
1534 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1535 * @adapter: board private structure to initialize
1536 *
1537 * This function frees the memory allocated to the q_vectors. In addition if
1538 * NAPI is enabled it will delete any references to the NAPI struct prior
1539 * to freeing the q_vector.
1540 **/
1541static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1542{
1543 int q_idx, num_q_vectors;
1544 int napi_vectors;
1545
1546 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001547 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001548
1549 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001550 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001551 if (q_idx < napi_vectors)
1552 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001553 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001554 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001555}
1556
1557/**
1558 * i40evf_reset_interrupt_capability - Reset MSIX setup
1559 * @adapter: board private structure
1560 *
1561 **/
1562void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1563{
1564 pci_disable_msix(adapter->pdev);
1565 kfree(adapter->msix_entries);
1566 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001567}
1568
1569/**
1570 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1571 * @adapter: board private structure to initialize
1572 *
1573 **/
1574int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1575{
1576 int err;
1577
1578 err = i40evf_set_interrupt_capability(adapter);
1579 if (err) {
1580 dev_err(&adapter->pdev->dev,
1581 "Unable to setup interrupt capabilities\n");
1582 goto err_set_interrupt;
1583 }
1584
1585 err = i40evf_alloc_q_vectors(adapter);
1586 if (err) {
1587 dev_err(&adapter->pdev->dev,
1588 "Unable to allocate memory for queue vectors\n");
1589 goto err_alloc_q_vectors;
1590 }
1591
1592 err = i40evf_alloc_queues(adapter);
1593 if (err) {
1594 dev_err(&adapter->pdev->dev,
1595 "Unable to allocate memory for queues\n");
1596 goto err_alloc_queues;
1597 }
1598
1599 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001600 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1601 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001602
1603 return 0;
1604err_alloc_queues:
1605 i40evf_free_q_vectors(adapter);
1606err_alloc_q_vectors:
1607 i40evf_reset_interrupt_capability(adapter);
1608err_set_interrupt:
1609 return err;
1610}
1611
1612/**
Helin Zhang66f9af852015-10-26 19:44:34 -04001613 * i40evf_clear_rss_config_user - Clear user configurations of RSS
1614 * @vsi: Pointer to VSI structure
1615 **/
1616static void i40evf_clear_rss_config_user(struct i40e_vsi *vsi)
1617{
1618 if (!vsi)
1619 return;
1620
1621 kfree(vsi->rss_hkey_user);
1622 vsi->rss_hkey_user = NULL;
1623
1624 kfree(vsi->rss_lut_user);
1625 vsi->rss_lut_user = NULL;
1626}
1627
1628/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001629 * i40evf_watchdog_timer - Periodic call-back timer
1630 * @data: pointer to adapter disguised as unsigned long
1631 **/
1632static void i40evf_watchdog_timer(unsigned long data)
1633{
1634 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001635
Greg Rose5eae00c2013-12-21 06:12:45 +00001636 schedule_work(&adapter->watchdog_task);
1637 /* timer will be rescheduled in watchdog task */
1638}
1639
1640/**
1641 * i40evf_watchdog_task - Periodic call-back task
1642 * @work: pointer to work_struct
1643 **/
1644static void i40evf_watchdog_task(struct work_struct *work)
1645{
1646 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001647 struct i40evf_adapter,
1648 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001649 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001650 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001651
Greg Rose5eae00c2013-12-21 06:12:45 +00001652 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001653 goto restart_watchdog;
1654
1655 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001656 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1657 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1658 if ((reg_val == I40E_VFR_VFACTIVE) ||
1659 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001660 /* A chance for redemption! */
1661 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1662 adapter->state = __I40EVF_STARTUP;
1663 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1664 schedule_delayed_work(&adapter->init_task, 10);
1665 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1666 &adapter->crit_section);
1667 /* Don't reschedule the watchdog, since we've restarted
1668 * the init task. When init_task contacts the PF and
1669 * gets everything set up again, it'll restart the
1670 * watchdog for us. Down, boy. Sit. Stay. Woof.
1671 */
1672 return;
1673 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001674 adapter->aq_required = 0;
1675 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1676 goto watchdog_done;
1677 }
1678
1679 if ((adapter->state < __I40EVF_DOWN) ||
1680 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001681 goto watchdog_done;
1682
Mitch Williamsef8693e2014-02-13 03:48:53 -08001683 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001684 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1685 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001686 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001687 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001688 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001689 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001690 adapter->aq_required = 0;
1691 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001692 goto watchdog_done;
1693 }
1694
1695 /* Process admin queue tasks. After init, everything gets done
1696 * here so we don't race on the admin queue.
1697 */
Mitch Williamsed636962015-04-07 19:45:32 -04001698 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001699 if (!i40evf_asq_done(hw)) {
1700 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1701 i40evf_send_api_ver(adapter);
1702 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001703 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001704 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001705 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1706 i40evf_send_vf_config_msg(adapter);
1707 goto watchdog_done;
1708 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001709
Mitch Williamse284fc82015-03-27 00:12:09 -07001710 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1711 i40evf_disable_queues(adapter);
1712 goto watchdog_done;
1713 }
1714
Greg Rose5eae00c2013-12-21 06:12:45 +00001715 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1716 i40evf_map_queues(adapter);
1717 goto watchdog_done;
1718 }
1719
1720 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1721 i40evf_add_ether_addrs(adapter);
1722 goto watchdog_done;
1723 }
1724
1725 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1726 i40evf_add_vlans(adapter);
1727 goto watchdog_done;
1728 }
1729
1730 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1731 i40evf_del_ether_addrs(adapter);
1732 goto watchdog_done;
1733 }
1734
1735 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1736 i40evf_del_vlans(adapter);
1737 goto watchdog_done;
1738 }
1739
Greg Rose5eae00c2013-12-21 06:12:45 +00001740 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1741 i40evf_configure_queues(adapter);
1742 goto watchdog_done;
1743 }
1744
1745 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1746 i40evf_enable_queues(adapter);
1747 goto watchdog_done;
1748 }
1749
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001750 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1751 /* This message goes straight to the firmware, not the
1752 * PF, so we don't have to set current_op as we will
1753 * not get a response through the ARQ.
1754 */
Helin Zhang96a81982015-10-26 19:44:31 -04001755 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001756 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1757 goto watchdog_done;
1758 }
1759
Greg Rose5eae00c2013-12-21 06:12:45 +00001760 if (adapter->state == __I40EVF_RUNNING)
1761 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001762watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001763 if (adapter->state == __I40EVF_RUNNING) {
1764 i40evf_irq_enable_queues(adapter, ~0);
1765 i40evf_fire_sw_int(adapter, 0xFF);
1766 } else {
1767 i40evf_fire_sw_int(adapter, 0x1);
1768 }
1769
Mitch Williamsef8693e2014-02-13 03:48:53 -08001770 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1771restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001772 if (adapter->state == __I40EVF_REMOVE)
1773 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001774 if (adapter->aq_required)
1775 mod_timer(&adapter->watchdog_timer,
1776 jiffies + msecs_to_jiffies(20));
1777 else
1778 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001779 schedule_work(&adapter->adminq_task);
1780}
1781
Mitch Williams67c818a2015-06-19 08:56:30 -07001782#define I40EVF_RESET_WAIT_MS 10
1783#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001784/**
1785 * i40evf_reset_task - Call-back task to handle hardware reset
1786 * @work: pointer to work_struct
1787 *
1788 * During reset we need to shut down and reinitialize the admin queue
1789 * before we can use it to communicate with the PF again. We also clear
1790 * and reinit the rings because that context is lost as well.
1791 **/
1792static void i40evf_reset_task(struct work_struct *work)
1793{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001794 struct i40evf_adapter *adapter = container_of(work,
1795 struct i40evf_adapter,
1796 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001797 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001798 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001799 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001800 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001801 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001802 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001803
1804 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1805 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001806 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001807
Mitch Williams67c818a2015-06-19 08:56:30 -07001808 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001809 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001810 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1811 /* Restart the AQ here. If we have been reset but didn't
1812 * detect it, or if the PF had to reinit, our AQ will be hosed.
1813 */
1814 i40evf_shutdown_adminq(hw);
1815 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001816 i40evf_request_reset(adapter);
1817 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001818 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001819
Mitch Williamsef8693e2014-02-13 03:48:53 -08001820 /* poll until we see the reset actually happen */
1821 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001822 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1823 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1824 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001825 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001826 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001827 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001828 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001829 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001830 goto continue_reset; /* act like the reset happened */
1831 }
1832
1833 /* wait until the reset is complete and the PF is responding to us */
1834 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001835 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1836 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1837 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001838 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001839 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001840 }
Mitch Williams509a4472015-12-23 12:05:52 -08001841 pci_set_master(adapter->pdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001842 /* extra wait to make sure minimum wait is met */
1843 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001844 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001845 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001846 struct i40evf_vlan_filter *fv, *fvtmp;
1847
Greg Rose5eae00c2013-12-21 06:12:45 +00001848 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001849 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001850 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001851 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1852
Mitch Williams169f4072014-04-01 07:11:50 +00001853 if (netif_running(adapter->netdev)) {
1854 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001855 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001856 netif_tx_disable(netdev);
1857 i40evf_napi_disable_all(adapter);
1858 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001859 i40evf_free_traffic_irqs(adapter);
1860 i40evf_free_all_tx_resources(adapter);
1861 i40evf_free_all_rx_resources(adapter);
1862 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001863
1864 /* Delete all of the filters, both MAC and VLAN. */
1865 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1866 list) {
1867 list_del(&f->list);
1868 kfree(f);
1869 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001870
Mitch Williams6ba36a22014-08-01 13:27:14 -07001871 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1872 list) {
1873 list_del(&fv->list);
1874 kfree(fv);
1875 }
1876
Mitch Williamsef8693e2014-02-13 03:48:53 -08001877 i40evf_free_misc_irq(adapter);
1878 i40evf_reset_interrupt_capability(adapter);
1879 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001880 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001881 kfree(adapter->vf_res);
1882 i40evf_shutdown_adminq(hw);
1883 adapter->netdev->flags &= ~IFF_UP;
1884 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001885 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Mitch Williams9b9344f2016-01-15 14:33:19 -08001886 adapter->state = __I40EVF_DOWN;
Mitch Williams67c818a2015-06-19 08:56:30 -07001887 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001888 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001889 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001890
1891continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001892 if (netif_running(adapter->netdev)) {
1893 netif_carrier_off(netdev);
1894 netif_tx_stop_all_queues(netdev);
1895 i40evf_napi_disable_all(adapter);
1896 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001897 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001898
Greg Rose5eae00c2013-12-21 06:12:45 +00001899 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001900 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1901
1902 /* free the Tx/Rx rings and descriptors, might be better to just
1903 * re-use them sometime in the future
1904 */
1905 i40evf_free_all_rx_resources(adapter);
1906 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001907
1908 /* kill and reinit the admin queue */
1909 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001910 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1911 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001912 err = i40evf_init_adminq(hw);
1913 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001914 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1915 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001916
Mitch Williamse6d038d2015-06-04 16:23:58 -04001917 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1918 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001919
1920 /* re-add all MAC filters */
1921 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1922 f->add = true;
1923 }
1924 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001925 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1926 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001927 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001928 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001929 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001930 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001931 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001932
1933 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1934
1935 if (netif_running(adapter->netdev)) {
1936 /* allocate transmit descriptors */
1937 err = i40evf_setup_all_tx_resources(adapter);
1938 if (err)
1939 goto reset_err;
1940
1941 /* allocate receive descriptors */
1942 err = i40evf_setup_all_rx_resources(adapter);
1943 if (err)
1944 goto reset_err;
1945
1946 i40evf_configure(adapter);
1947
1948 err = i40evf_up_complete(adapter);
1949 if (err)
1950 goto reset_err;
1951
1952 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001953 } else {
1954 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001955 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001956
Greg Rose5eae00c2013-12-21 06:12:45 +00001957 return;
1958reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001959 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001960 i40evf_close(adapter->netdev);
1961}
1962
1963/**
1964 * i40evf_adminq_task - worker thread to clean the admin queue
1965 * @work: pointer to work_struct containing our data
1966 **/
1967static void i40evf_adminq_task(struct work_struct *work)
1968{
1969 struct i40evf_adapter *adapter =
1970 container_of(work, struct i40evf_adapter, adminq_task);
1971 struct i40e_hw *hw = &adapter->hw;
1972 struct i40e_arq_event_info event;
1973 struct i40e_virtchnl_msg *v_msg;
1974 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001975 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001976 u16 pending;
1977
Mitch Williamsef8693e2014-02-13 03:48:53 -08001978 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001979 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001980
Mitch Williams1001dc32014-11-11 20:02:19 +00001981 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1982 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001983 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001984 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001985
Greg Rose5eae00c2013-12-21 06:12:45 +00001986 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1987 do {
1988 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001989 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001990 break; /* No event to process or error cleaning ARQ */
1991
1992 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1993 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001994 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001995 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001996 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001997 } while (pending);
1998
Mitch Williams67c818a2015-06-19 08:56:30 -07001999 if ((adapter->flags &
2000 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2001 adapter->state == __I40EVF_RESETTING)
2002 goto freedom;
2003
Mitch Williams912257e2014-05-22 06:32:07 +00002004 /* check for error indications */
2005 val = rd32(hw, hw->aq.arq.len);
2006 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002007 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002008 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002009 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002010 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002011 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002012 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002013 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002014 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002015 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002016 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002017 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002018 }
2019 if (oldval != val)
2020 wr32(hw, hw->aq.arq.len, val);
2021
2022 val = rd32(hw, hw->aq.asq.len);
2023 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002024 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002025 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002026 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002027 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002028 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002029 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002030 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002031 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002032 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002033 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002034 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002035 }
2036 if (oldval != val)
2037 wr32(hw, hw->aq.asq.len, val);
2038
Mitch Williams67c818a2015-06-19 08:56:30 -07002039freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002040 kfree(event.msg_buf);
2041out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002042 /* re-enable Admin queue interrupt cause */
2043 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002044}
2045
2046/**
2047 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2048 * @adapter: board private structure
2049 *
2050 * Free all transmit software resources
2051 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002052void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002053{
2054 int i;
2055
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002056 if (!adapter->tx_rings)
2057 return;
2058
Mitch Williamscc052922014-10-25 03:24:34 +00002059 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002060 if (adapter->tx_rings[i].desc)
2061 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002062}
2063
2064/**
2065 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2066 * @adapter: board private structure
2067 *
2068 * If this function returns with an error, then it's possible one or
2069 * more of the rings is populated (while the rest are not). It is the
2070 * callers duty to clean those orphaned rings.
2071 *
2072 * Return 0 on success, negative on failure
2073 **/
2074static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2075{
2076 int i, err = 0;
2077
Mitch Williamscc052922014-10-25 03:24:34 +00002078 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002079 adapter->tx_rings[i].count = adapter->tx_desc_count;
2080 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002081 if (!err)
2082 continue;
2083 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002084 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002085 break;
2086 }
2087
2088 return err;
2089}
2090
2091/**
2092 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2093 * @adapter: board private structure
2094 *
2095 * If this function returns with an error, then it's possible one or
2096 * more of the rings is populated (while the rest are not). It is the
2097 * callers duty to clean those orphaned rings.
2098 *
2099 * Return 0 on success, negative on failure
2100 **/
2101static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2102{
2103 int i, err = 0;
2104
Mitch Williamscc052922014-10-25 03:24:34 +00002105 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002106 adapter->rx_rings[i].count = adapter->rx_desc_count;
2107 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002108 if (!err)
2109 continue;
2110 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002111 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002112 break;
2113 }
2114 return err;
2115}
2116
2117/**
2118 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2119 * @adapter: board private structure
2120 *
2121 * Free all receive software resources
2122 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002123void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002124{
2125 int i;
2126
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002127 if (!adapter->rx_rings)
2128 return;
2129
Mitch Williamscc052922014-10-25 03:24:34 +00002130 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002131 if (adapter->rx_rings[i].desc)
2132 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002133}
2134
2135/**
2136 * i40evf_open - Called when a network interface is made active
2137 * @netdev: network interface device structure
2138 *
2139 * Returns 0 on success, negative value on failure
2140 *
2141 * The open entry point is called when a network interface is made
2142 * active by the system (IFF_UP). At this point all resources needed
2143 * for transmit and receive operations are allocated, the interrupt
2144 * handler is registered with the OS, the watchdog timer is started,
2145 * and the stack is notified that the interface is ready.
2146 **/
2147static int i40evf_open(struct net_device *netdev)
2148{
2149 struct i40evf_adapter *adapter = netdev_priv(netdev);
2150 int err;
2151
Mitch Williamsef8693e2014-02-13 03:48:53 -08002152 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2153 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2154 return -EIO;
2155 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002156
2157 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002158 return -EBUSY;
2159
2160 /* allocate transmit descriptors */
2161 err = i40evf_setup_all_tx_resources(adapter);
2162 if (err)
2163 goto err_setup_tx;
2164
2165 /* allocate receive descriptors */
2166 err = i40evf_setup_all_rx_resources(adapter);
2167 if (err)
2168 goto err_setup_rx;
2169
2170 /* clear any pending interrupts, may auto mask */
2171 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2172 if (err)
2173 goto err_req_irq;
2174
Mitch Williams44151cd2015-04-27 14:57:17 -04002175 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002176 i40evf_configure(adapter);
2177
2178 err = i40evf_up_complete(adapter);
2179 if (err)
2180 goto err_req_irq;
2181
2182 i40evf_irq_enable(adapter, true);
2183
2184 return 0;
2185
2186err_req_irq:
2187 i40evf_down(adapter);
2188 i40evf_free_traffic_irqs(adapter);
2189err_setup_rx:
2190 i40evf_free_all_rx_resources(adapter);
2191err_setup_tx:
2192 i40evf_free_all_tx_resources(adapter);
2193
2194 return err;
2195}
2196
2197/**
2198 * i40evf_close - Disables a network interface
2199 * @netdev: network interface device structure
2200 *
2201 * Returns 0, this is not allowed to fail
2202 *
2203 * The close entry point is called when an interface is de-activated
2204 * by the OS. The hardware is still under the drivers control, but
2205 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2206 * are freed, along with all transmit and receive resources.
2207 **/
2208static int i40evf_close(struct net_device *netdev)
2209{
2210 struct i40evf_adapter *adapter = netdev_priv(netdev);
2211
Mitch Williams209dc4d2015-12-09 15:50:27 -08002212 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002213 return 0;
2214
Mitch Williamsef8693e2014-02-13 03:48:53 -08002215
Greg Rose5eae00c2013-12-21 06:12:45 +00002216 set_bit(__I40E_DOWN, &adapter->vsi.state);
2217
2218 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002219 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002220 i40evf_free_traffic_irqs(adapter);
2221
Greg Rose5eae00c2013-12-21 06:12:45 +00002222 return 0;
2223}
2224
2225/**
2226 * i40evf_get_stats - Get System Network Statistics
2227 * @netdev: network interface device structure
2228 *
2229 * Returns the address of the device statistics structure.
2230 * The statistics are actually updated from the timer callback.
2231 **/
2232static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2233{
2234 struct i40evf_adapter *adapter = netdev_priv(netdev);
2235
2236 /* only return the current stats */
2237 return &adapter->net_stats;
2238}
2239
2240/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002241 * i40evf_change_mtu - Change the Maximum Transfer Unit
2242 * @netdev: network interface device structure
2243 * @new_mtu: new value for maximum frame size
2244 *
2245 * Returns 0 on success, negative on failure
2246 **/
2247static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2248{
2249 struct i40evf_adapter *adapter = netdev_priv(netdev);
2250 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2251
2252 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2253 return -EINVAL;
2254
Greg Rose5eae00c2013-12-21 06:12:45 +00002255 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002256 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2257 schedule_work(&adapter->reset_task);
2258
Greg Rose5eae00c2013-12-21 06:12:45 +00002259 return 0;
2260}
2261
2262static const struct net_device_ops i40evf_netdev_ops = {
2263 .ndo_open = i40evf_open,
2264 .ndo_stop = i40evf_close,
2265 .ndo_start_xmit = i40evf_xmit_frame,
2266 .ndo_get_stats = i40evf_get_stats,
2267 .ndo_set_rx_mode = i40evf_set_rx_mode,
2268 .ndo_validate_addr = eth_validate_addr,
2269 .ndo_set_mac_address = i40evf_set_mac,
2270 .ndo_change_mtu = i40evf_change_mtu,
2271 .ndo_tx_timeout = i40evf_tx_timeout,
2272 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2273 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002274#ifdef CONFIG_NET_POLL_CONTROLLER
2275 .ndo_poll_controller = i40evf_netpoll,
2276#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002277};
2278
2279/**
2280 * i40evf_check_reset_complete - check that VF reset is complete
2281 * @hw: pointer to hw struct
2282 *
2283 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2284 **/
2285static int i40evf_check_reset_complete(struct i40e_hw *hw)
2286{
2287 u32 rstat;
2288 int i;
2289
2290 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002291 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2292 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2293 if ((rstat == I40E_VFR_VFACTIVE) ||
2294 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002295 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002296 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002297 }
2298 return -EBUSY;
2299}
2300
2301/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002302 * i40evf_process_config - Process the config information we got from the PF
2303 * @adapter: board private structure
2304 *
2305 * Verify that we have a valid config struct, and set up our netdev features
2306 * and our VSI struct.
2307 **/
2308int i40evf_process_config(struct i40evf_adapter *adapter)
2309{
2310 struct net_device *netdev = adapter->netdev;
2311 int i;
2312
2313 /* got VF config message back from PF, now we can parse it */
2314 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2315 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2316 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2317 }
2318 if (!adapter->vsi_res) {
2319 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2320 return -ENODEV;
2321 }
2322
2323 if (adapter->vf_res->vf_offload_flags
2324 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
Mitch Williamscc7e4062015-09-28 14:12:40 -04002325 netdev->vlan_features = netdev->features &
2326 ~(NETIF_F_HW_VLAN_CTAG_TX |
2327 NETIF_F_HW_VLAN_CTAG_RX |
2328 NETIF_F_HW_VLAN_CTAG_FILTER);
Mitch Williamse6d038d2015-06-04 16:23:58 -04002329 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2330 NETIF_F_HW_VLAN_CTAG_RX |
2331 NETIF_F_HW_VLAN_CTAG_FILTER;
2332 }
2333 netdev->features |= NETIF_F_HIGHDMA |
2334 NETIF_F_SG |
2335 NETIF_F_IP_CSUM |
Tom Herbert53692b12015-12-14 11:19:41 -08002336 NETIF_F_SCTP_CRC |
Mitch Williamse6d038d2015-06-04 16:23:58 -04002337 NETIF_F_IPV6_CSUM |
2338 NETIF_F_TSO |
2339 NETIF_F_TSO6 |
2340 NETIF_F_RXCSUM |
2341 NETIF_F_GRO;
2342
2343 /* copy netdev features into list of user selectable features */
2344 netdev->hw_features |= netdev->features;
2345 netdev->hw_features &= ~NETIF_F_RXCSUM;
2346
2347 adapter->vsi.id = adapter->vsi_res->vsi_id;
2348
2349 adapter->vsi.back = adapter;
2350 adapter->vsi.base_vector = 1;
2351 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2352 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2353 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2354 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2355 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2356 adapter->vsi.netdev = adapter->netdev;
Mitch Williamsf578f5f2015-08-28 17:55:58 -04002357 adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002358 return 0;
2359}
2360
2361/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002362 * i40evf_init_task - worker thread to perform delayed initialization
2363 * @work: pointer to work_struct containing our data
2364 *
2365 * This task completes the work that was begun in probe. Due to the nature
2366 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002367 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002368 * whole system, we'll do it in a task so we can sleep.
2369 * This task only runs during driver init. Once we've established
2370 * communications with the PF driver and set up our netdev, the watchdog
2371 * takes over.
2372 **/
2373static void i40evf_init_task(struct work_struct *work)
2374{
2375 struct i40evf_adapter *adapter = container_of(work,
2376 struct i40evf_adapter,
2377 init_task.work);
2378 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002379 struct i40e_hw *hw = &adapter->hw;
2380 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002381 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002382
2383 switch (adapter->state) {
2384 case __I40EVF_STARTUP:
2385 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002386 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2387 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002388 err = i40e_set_mac_type(hw);
2389 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002390 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2391 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002392 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002393 }
2394 err = i40evf_check_reset_complete(hw);
2395 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002396 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002397 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002398 goto err;
2399 }
2400 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2401 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2402 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2403 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2404
2405 err = i40evf_init_adminq(hw);
2406 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002407 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2408 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002409 goto err;
2410 }
2411 err = i40evf_send_api_ver(adapter);
2412 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002413 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002414 i40evf_shutdown_adminq(hw);
2415 goto err;
2416 }
2417 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2418 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002419 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002420 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002421 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002422 i40evf_shutdown_adminq(hw);
2423 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002424 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002425 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002426
2427 /* aq msg sent, awaiting reply */
2428 err = i40evf_verify_api_ver(adapter);
2429 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002430 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002431 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002432 else
2433 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2434 adapter->pf_version.major,
2435 adapter->pf_version.minor,
2436 I40E_VIRTCHNL_VERSION_MAJOR,
2437 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002438 goto err;
2439 }
2440 err = i40evf_send_vf_config_msg(adapter);
2441 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002442 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002443 err);
2444 goto err;
2445 }
2446 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2447 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002448 case __I40EVF_INIT_GET_RESOURCES:
2449 /* aq msg sent, awaiting reply */
2450 if (!adapter->vf_res) {
2451 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2452 (I40E_MAX_VF_VSI *
2453 sizeof(struct i40e_virtchnl_vsi_resource));
2454 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002455 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002456 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002457 }
2458 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002459 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002460 err = i40evf_send_vf_config_msg(adapter);
2461 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002462 } else if (err == I40E_ERR_PARAM) {
2463 /* We only get ERR_PARAM if the device is in a very bad
2464 * state or if we've been disabled for previous bad
2465 * behavior. Either way, we're done now.
2466 */
2467 i40evf_shutdown_adminq(hw);
2468 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2469 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002470 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002471 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002472 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2473 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002474 goto err_alloc;
2475 }
2476 adapter->state = __I40EVF_INIT_SW;
2477 break;
2478 default:
2479 goto err_alloc;
2480 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04002481 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002482 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002483 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002484
2485 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
Mitch Williams00e5ec42016-01-15 14:33:10 -08002486 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
2487 adapter->flags |= I40EVF_FLAG_RX_PS_CAPABLE;
2488
2489 /* Default to single buffer rx, can be changed through ethtool. */
2490 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002491
Greg Rose5eae00c2013-12-21 06:12:45 +00002492 netdev->netdev_ops = &i40evf_netdev_ops;
2493 i40evf_set_ethtool_ops(netdev);
2494 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002495
Greg Rose5eae00c2013-12-21 06:12:45 +00002496 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002497 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002498 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002499 eth_hw_addr_random(netdev);
2500 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2501 } else {
2502 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2503 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2504 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002505 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002506
Greg Rose5eae00c2013-12-21 06:12:45 +00002507 init_timer(&adapter->watchdog_timer);
2508 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2509 adapter->watchdog_timer.data = (unsigned long)adapter;
2510 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2511
Mitch Williamscc052922014-10-25 03:24:34 +00002512 adapter->num_active_queues = min_t(int,
2513 adapter->vsi_res->num_queue_pairs,
2514 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002515 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2516 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002517 err = i40evf_init_interrupt_scheme(adapter);
2518 if (err)
2519 goto err_sw_init;
2520 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002521 if (adapter->vf_res->vf_offload_flags &
2522 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2523 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002524
2525 if (adapter->vf_res->vf_offload_flags &
2526 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2527 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2528
Greg Rose5eae00c2013-12-21 06:12:45 +00002529 err = i40evf_request_misc_irq(adapter);
2530 if (err)
2531 goto err_sw_init;
2532
2533 netif_carrier_off(netdev);
2534
Mitch Williamsef8693e2014-02-13 03:48:53 -08002535 if (!adapter->netdev_registered) {
2536 err = register_netdev(netdev);
2537 if (err)
2538 goto err_register;
2539 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002540
2541 adapter->netdev_registered = true;
2542
2543 netif_tx_stop_all_queues(netdev);
2544
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002545 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002546 if (netdev->features & NETIF_F_GRO)
2547 dev_info(&pdev->dev, "GRO is enabled\n");
2548
Greg Rose5eae00c2013-12-21 06:12:45 +00002549 adapter->state = __I40EVF_DOWN;
2550 set_bit(__I40E_DOWN, &adapter->vsi.state);
2551 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002552
2553 if (RSS_AQ(adapter)) {
2554 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2555 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2556 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002557 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002558 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002559 return;
2560restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002561 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002562 return;
2563
2564err_register:
2565 i40evf_free_misc_irq(adapter);
2566err_sw_init:
2567 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002568err_alloc:
2569 kfree(adapter->vf_res);
2570 adapter->vf_res = NULL;
2571err:
2572 /* Things went into the weeds, so try again later */
2573 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002574 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002575 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002576 i40evf_shutdown_adminq(hw);
2577 adapter->state = __I40EVF_STARTUP;
2578 schedule_delayed_work(&adapter->init_task, HZ * 5);
2579 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002580 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002581 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002582}
2583
2584/**
2585 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2586 * @pdev: pci device structure
2587 **/
2588static void i40evf_shutdown(struct pci_dev *pdev)
2589{
2590 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002591 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002592
2593 netif_device_detach(netdev);
2594
2595 if (netif_running(netdev))
2596 i40evf_close(netdev);
2597
Mitch Williams00293fd2015-01-09 11:18:18 +00002598 /* Prevent the watchdog from running. */
2599 adapter->state = __I40EVF_REMOVE;
2600 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002601
Greg Rose5eae00c2013-12-21 06:12:45 +00002602#ifdef CONFIG_PM
2603 pci_save_state(pdev);
2604
2605#endif
2606 pci_disable_device(pdev);
2607}
2608
2609/**
2610 * i40evf_probe - Device Initialization Routine
2611 * @pdev: PCI device information struct
2612 * @ent: entry in i40evf_pci_tbl
2613 *
2614 * Returns 0 on success, negative on failure
2615 *
2616 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2617 * The OS initialization, configuring of the adapter private structure,
2618 * and a hardware reset occur.
2619 **/
2620static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2621{
2622 struct net_device *netdev;
2623 struct i40evf_adapter *adapter = NULL;
2624 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002625 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002626
2627 err = pci_enable_device(pdev);
2628 if (err)
2629 return err;
2630
Mitch Williams64942942014-02-11 08:26:33 +00002631 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002632 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002633 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2634 if (err) {
2635 dev_err(&pdev->dev,
2636 "DMA configuration failed: 0x%x\n", err);
2637 goto err_dma;
2638 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002639 }
2640
2641 err = pci_request_regions(pdev, i40evf_driver_name);
2642 if (err) {
2643 dev_err(&pdev->dev,
2644 "pci_request_regions failed 0x%x\n", err);
2645 goto err_pci_reg;
2646 }
2647
2648 pci_enable_pcie_error_reporting(pdev);
2649
2650 pci_set_master(pdev);
2651
Mitch Williams1255b7a2015-11-06 15:25:59 -08002652 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002653 if (!netdev) {
2654 err = -ENOMEM;
2655 goto err_alloc_etherdev;
2656 }
2657
2658 SET_NETDEV_DEV(netdev, &pdev->dev);
2659
2660 pci_set_drvdata(pdev, netdev);
2661 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002662
2663 adapter->netdev = netdev;
2664 adapter->pdev = pdev;
2665
2666 hw = &adapter->hw;
2667 hw->back = adapter;
2668
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002669 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002670 adapter->state = __I40EVF_STARTUP;
2671
2672 /* Call save state here because it relies on the adapter struct. */
2673 pci_save_state(pdev);
2674
2675 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2676 pci_resource_len(pdev, 0));
2677 if (!hw->hw_addr) {
2678 err = -EIO;
2679 goto err_ioremap;
2680 }
2681 hw->vendor_id = pdev->vendor;
2682 hw->device_id = pdev->device;
2683 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2684 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2685 hw->subsystem_device_id = pdev->subsystem_device;
2686 hw->bus.device = PCI_SLOT(pdev->devfn);
2687 hw->bus.func = PCI_FUNC(pdev->devfn);
2688
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002689 /* set up the locks for the AQ, do this only once in probe
2690 * and destroy them only once in remove
2691 */
2692 mutex_init(&hw->aq.asq_mutex);
2693 mutex_init(&hw->aq.arq_mutex);
2694
Serey Kong8bb1a542014-08-01 13:27:15 -07002695 INIT_LIST_HEAD(&adapter->mac_filter_list);
2696 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2697
Greg Rose5eae00c2013-12-21 06:12:45 +00002698 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2699 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2700 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2701 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002702 schedule_delayed_work(&adapter->init_task,
2703 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002704
2705 return 0;
2706
2707err_ioremap:
2708 free_netdev(netdev);
2709err_alloc_etherdev:
2710 pci_release_regions(pdev);
2711err_pci_reg:
2712err_dma:
2713 pci_disable_device(pdev);
2714 return err;
2715}
2716
2717#ifdef CONFIG_PM
2718/**
2719 * i40evf_suspend - Power management suspend routine
2720 * @pdev: PCI device information struct
2721 * @state: unused
2722 *
2723 * Called when the system (VM) is entering sleep/suspend.
2724 **/
2725static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2726{
2727 struct net_device *netdev = pci_get_drvdata(pdev);
2728 struct i40evf_adapter *adapter = netdev_priv(netdev);
2729 int retval = 0;
2730
2731 netif_device_detach(netdev);
2732
2733 if (netif_running(netdev)) {
2734 rtnl_lock();
2735 i40evf_down(adapter);
2736 rtnl_unlock();
2737 }
2738 i40evf_free_misc_irq(adapter);
2739 i40evf_reset_interrupt_capability(adapter);
2740
2741 retval = pci_save_state(pdev);
2742 if (retval)
2743 return retval;
2744
2745 pci_disable_device(pdev);
2746
2747 return 0;
2748}
2749
2750/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002751 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002752 * @pdev: PCI device information struct
2753 *
2754 * Called when the system (VM) is resumed from sleep/suspend.
2755 **/
2756static int i40evf_resume(struct pci_dev *pdev)
2757{
2758 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2759 struct net_device *netdev = adapter->netdev;
2760 u32 err;
2761
2762 pci_set_power_state(pdev, PCI_D0);
2763 pci_restore_state(pdev);
2764 /* pci_restore_state clears dev->state_saved so call
2765 * pci_save_state to restore it.
2766 */
2767 pci_save_state(pdev);
2768
2769 err = pci_enable_device_mem(pdev);
2770 if (err) {
2771 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2772 return err;
2773 }
2774 pci_set_master(pdev);
2775
2776 rtnl_lock();
2777 err = i40evf_set_interrupt_capability(adapter);
2778 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002779 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002780 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2781 return err;
2782 }
2783 err = i40evf_request_misc_irq(adapter);
2784 rtnl_unlock();
2785 if (err) {
2786 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2787 return err;
2788 }
2789
2790 schedule_work(&adapter->reset_task);
2791
2792 netif_device_attach(netdev);
2793
2794 return err;
2795}
2796
2797#endif /* CONFIG_PM */
2798/**
2799 * i40evf_remove - Device Removal Routine
2800 * @pdev: PCI device information struct
2801 *
2802 * i40evf_remove is called by the PCI subsystem to alert the driver
2803 * that it should release a PCI device. The could be caused by a
2804 * Hot-Plug event, or because the driver is going to be removed from
2805 * memory.
2806 **/
2807static void i40evf_remove(struct pci_dev *pdev)
2808{
2809 struct net_device *netdev = pci_get_drvdata(pdev);
2810 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002811 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002812 struct i40e_hw *hw = &adapter->hw;
2813
2814 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002815 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002816
2817 if (adapter->netdev_registered) {
2818 unregister_netdev(netdev);
2819 adapter->netdev_registered = false;
2820 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002821
Mitch Williamsf4a71882015-01-09 11:18:16 +00002822 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002823 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002824 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002825 i40evf_request_reset(adapter);
2826 msleep(20);
2827 /* If the FW isn't responding, kick it once, but only once. */
2828 if (!i40evf_asq_done(hw)) {
2829 i40evf_request_reset(adapter);
2830 msleep(20);
2831 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002832
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002833 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002834 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002835 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002836 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002837 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002838 }
2839
Mitch Williamse5d17c32014-06-04 04:22:38 +00002840 if (adapter->watchdog_timer.function)
2841 del_timer_sync(&adapter->watchdog_timer);
2842
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002843 flush_scheduled_work();
2844
Helin Zhang66f9af852015-10-26 19:44:34 -04002845 /* Clear user configurations for RSS */
2846 i40evf_clear_rss_config_user(&adapter->vsi);
2847
Greg Rose5eae00c2013-12-21 06:12:45 +00002848 if (hw->aq.asq.count)
2849 i40evf_shutdown_adminq(hw);
2850
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002851 /* destroy the locks only once, here */
2852 mutex_destroy(&hw->aq.arq_mutex);
2853 mutex_destroy(&hw->aq.asq_mutex);
2854
Greg Rose5eae00c2013-12-21 06:12:45 +00002855 iounmap(hw->hw_addr);
2856 pci_release_regions(pdev);
2857
Mitch Williamse284fc82015-03-27 00:12:09 -07002858 i40evf_free_all_tx_resources(adapter);
2859 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002860 i40evf_free_queues(adapter);
2861 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002862 /* If we got removed before an up/down sequence, we've got a filter
2863 * hanging out there that we need to get rid of.
2864 */
2865 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2866 list_del(&f->list);
2867 kfree(f);
2868 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002869 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2870 list_del(&f->list);
2871 kfree(f);
2872 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002873
2874 free_netdev(netdev);
2875
2876 pci_disable_pcie_error_reporting(pdev);
2877
2878 pci_disable_device(pdev);
2879}
2880
2881static struct pci_driver i40evf_driver = {
2882 .name = i40evf_driver_name,
2883 .id_table = i40evf_pci_tbl,
2884 .probe = i40evf_probe,
2885 .remove = i40evf_remove,
2886#ifdef CONFIG_PM
2887 .suspend = i40evf_suspend,
2888 .resume = i40evf_resume,
2889#endif
2890 .shutdown = i40evf_shutdown,
2891};
2892
2893/**
2894 * i40e_init_module - Driver Registration Routine
2895 *
2896 * i40e_init_module is the first routine called when the driver is
2897 * loaded. All it does is register with the PCI subsystem.
2898 **/
2899static int __init i40evf_init_module(void)
2900{
2901 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002902
Greg Rose5eae00c2013-12-21 06:12:45 +00002903 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002904 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002905
2906 pr_info("%s\n", i40evf_copyright);
2907
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002908 i40evf_wq = create_singlethread_workqueue(i40evf_driver_name);
2909 if (!i40evf_wq) {
2910 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2911 return -ENOMEM;
2912 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002913 ret = pci_register_driver(&i40evf_driver);
2914 return ret;
2915}
2916
2917module_init(i40evf_init_module);
2918
2919/**
2920 * i40e_exit_module - Driver Exit Cleanup Routine
2921 *
2922 * i40e_exit_module is called just before the driver is removed
2923 * from memory.
2924 **/
2925static void __exit i40evf_exit_module(void)
2926{
2927 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002928 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00002929}
2930
2931module_exit(i40evf_exit_module);
2932
2933/* i40evf_main.c */