blob: 52408bc103d6f176dc094ed7c55bf7a06194909a [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
Catherine Sullivan50f26a52016-03-10 14:59:51 -080040#define DRV_VERSION_MINOR 5
Harshitha Ramamurthy5a6fc252016-04-13 03:08:32 -070041#define DRV_VERSION_BUILD 10
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;
Greg Rose5eae00c2013-12-21 06:12:45 +0000644 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000645
Mitch Williamscc052922014-10-25 03:24:34 +0000646 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400647 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Jesse Brandeburg19b85e62016-04-18 11:33:45 -0700648 adapter->rx_rings[i].rx_buf_len = I40EVF_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000649 }
650}
651
652/**
653 * i40evf_find_vlan - Search filter list for specific vlan filter
654 * @adapter: board private structure
655 * @vlan: vlan tag
656 *
657 * Returns ptr to the filter object or NULL
658 **/
659static struct
660i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
661{
662 struct i40evf_vlan_filter *f;
663
664 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
665 if (vlan == f->vlan)
666 return f;
667 }
668 return NULL;
669}
670
671/**
672 * i40evf_add_vlan - Add a vlan filter to the list
673 * @adapter: board private structure
674 * @vlan: VLAN tag
675 *
676 * Returns ptr to the filter object or NULL when no memory available.
677 **/
678static struct
679i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
680{
Mitch Williams13acb542015-03-31 00:45:05 -0700681 struct i40evf_vlan_filter *f = NULL;
682 int count = 50;
683
684 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
685 &adapter->crit_section)) {
686 udelay(1);
687 if (--count == 0)
688 goto out;
689 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000690
691 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000692 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000693 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000694 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700695 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000696
Greg Rose5eae00c2013-12-21 06:12:45 +0000697 f->vlan = vlan;
698
699 INIT_LIST_HEAD(&f->list);
700 list_add(&f->list, &adapter->vlan_filter_list);
701 f->add = true;
702 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
703 }
704
Mitch Williams13acb542015-03-31 00:45:05 -0700705clearout:
706 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
707out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000708 return f;
709}
710
711/**
712 * i40evf_del_vlan - Remove a vlan filter from the list
713 * @adapter: board private structure
714 * @vlan: VLAN tag
715 **/
716static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
717{
718 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700719 int count = 50;
720
721 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
722 &adapter->crit_section)) {
723 udelay(1);
724 if (--count == 0)
725 return;
726 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000727
728 f = i40evf_find_vlan(adapter, vlan);
729 if (f) {
730 f->remove = true;
731 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
732 }
Mitch Williams13acb542015-03-31 00:45:05 -0700733 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000734}
735
736/**
737 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
738 * @netdev: network device struct
739 * @vid: VLAN tag
740 **/
741static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000742 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000743{
744 struct i40evf_adapter *adapter = netdev_priv(netdev);
745
Mitch Williams8ed995f2015-08-28 17:55:57 -0400746 if (!VLAN_ALLOWED(adapter))
747 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000748 if (i40evf_add_vlan(adapter, vid) == NULL)
749 return -ENOMEM;
750 return 0;
751}
752
753/**
754 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
755 * @netdev: network device struct
756 * @vid: VLAN tag
757 **/
758static int i40evf_vlan_rx_kill_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 i40evf_del_vlan(adapter, vid);
765 return 0;
766 }
767 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000768}
769
770/**
771 * i40evf_find_filter - Search filter list for specific mac filter
772 * @adapter: board private structure
773 * @macaddr: the MAC address
774 *
775 * Returns ptr to the filter object or NULL
776 **/
777static struct
778i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
779 u8 *macaddr)
780{
781 struct i40evf_mac_filter *f;
782
783 if (!macaddr)
784 return NULL;
785
786 list_for_each_entry(f, &adapter->mac_filter_list, list) {
787 if (ether_addr_equal(macaddr, f->macaddr))
788 return f;
789 }
790 return NULL;
791}
792
793/**
794 * i40e_add_filter - Add a mac filter to the filter list
795 * @adapter: board private structure
796 * @macaddr: the MAC address
797 *
798 * Returns ptr to the filter object or NULL when no memory available.
799 **/
800static struct
801i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
802 u8 *macaddr)
803{
804 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000805 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000806
807 if (!macaddr)
808 return NULL;
809
810 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000811 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000812 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000813 if (--count == 0)
814 return NULL;
815 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000816
817 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000818 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000819 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000820 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000821 clear_bit(__I40EVF_IN_CRITICAL_TASK,
822 &adapter->crit_section);
823 return NULL;
824 }
825
Greg Rose9a173902014-05-22 06:32:02 +0000826 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000827
828 list_add(&f->list, &adapter->mac_filter_list);
829 f->add = true;
830 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
831 }
832
833 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
834 return f;
835}
836
837/**
838 * i40evf_set_mac - NDO callback to set port mac address
839 * @netdev: network interface device structure
840 * @p: pointer to an address structure
841 *
842 * Returns 0 on success, negative on failure
843 **/
844static int i40evf_set_mac(struct net_device *netdev, void *p)
845{
846 struct i40evf_adapter *adapter = netdev_priv(netdev);
847 struct i40e_hw *hw = &adapter->hw;
848 struct i40evf_mac_filter *f;
849 struct sockaddr *addr = p;
850
851 if (!is_valid_ether_addr(addr->sa_data))
852 return -EADDRNOTAVAIL;
853
854 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
855 return 0;
856
Mitch Williams14e52ee2015-08-31 19:54:44 -0400857 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
858 return -EPERM;
859
860 f = i40evf_find_filter(adapter, hw->mac.addr);
861 if (f) {
862 f->remove = true;
863 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
864 }
865
Greg Rose5eae00c2013-12-21 06:12:45 +0000866 f = i40evf_add_filter(adapter, addr->sa_data);
867 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000868 ether_addr_copy(hw->mac.addr, addr->sa_data);
869 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000870 }
871
872 return (f == NULL) ? -ENOMEM : 0;
873}
874
875/**
876 * i40evf_set_rx_mode - NDO callback to set the netdev filters
877 * @netdev: network interface device structure
878 **/
879static void i40evf_set_rx_mode(struct net_device *netdev)
880{
881 struct i40evf_adapter *adapter = netdev_priv(netdev);
882 struct i40evf_mac_filter *f, *ftmp;
883 struct netdev_hw_addr *uca;
884 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400885 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000886 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000887
888 /* add addr if not already in the filter list */
889 netdev_for_each_uc_addr(uca, netdev) {
890 i40evf_add_filter(adapter, uca->addr);
891 }
892 netdev_for_each_mc_addr(mca, netdev) {
893 i40evf_add_filter(adapter, mca->addr);
894 }
895
896 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000897 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000898 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000899 if (--count == 0) {
900 dev_err(&adapter->pdev->dev,
901 "Failed to get lock in %s\n", __func__);
902 return;
903 }
904 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000905 /* remove filter if not in netdev list */
906 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400907 netdev_for_each_mc_addr(mca, netdev)
908 if (ether_addr_equal(mca->addr, f->macaddr))
909 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000910
Shannon Nelson2f41f332015-08-26 15:14:20 -0400911 netdev_for_each_uc_addr(uca, netdev)
912 if (ether_addr_equal(uca->addr, f->macaddr))
913 goto bottom_of_search_loop;
914
915 for_each_dev_addr(netdev, ha)
916 if (ether_addr_equal(ha->addr, f->macaddr))
917 goto bottom_of_search_loop;
918
919 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
920 goto bottom_of_search_loop;
921
922 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
923 f->remove = true;
924 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
925
926bottom_of_search_loop:
927 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000928 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700929
930 if (netdev->flags & IFF_PROMISC &&
931 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
932 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
933 else if (!(netdev->flags & IFF_PROMISC) &&
934 adapter->flags & I40EVF_FLAG_PROMISC_ON)
935 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
936
Greg Rose5eae00c2013-12-21 06:12:45 +0000937 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
938}
939
940/**
941 * i40evf_napi_enable_all - enable NAPI on all queue vectors
942 * @adapter: board private structure
943 **/
944static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
945{
946 int q_idx;
947 struct i40e_q_vector *q_vector;
948 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
949
950 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
951 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000952
Mitch Williams7d96ba12015-10-26 19:44:39 -0400953 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000954 napi = &q_vector->napi;
955 napi_enable(napi);
956 }
957}
958
959/**
960 * i40evf_napi_disable_all - disable NAPI on all queue vectors
961 * @adapter: board private structure
962 **/
963static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
964{
965 int q_idx;
966 struct i40e_q_vector *q_vector;
967 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
968
969 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400970 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000971 napi_disable(&q_vector->napi);
972 }
973}
974
975/**
976 * i40evf_configure - set up transmit and receive data structures
977 * @adapter: board private structure
978 **/
979static void i40evf_configure(struct i40evf_adapter *adapter)
980{
981 struct net_device *netdev = adapter->netdev;
982 int i;
983
984 i40evf_set_rx_mode(netdev);
985
986 i40evf_configure_tx(adapter);
987 i40evf_configure_rx(adapter);
988 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
989
Mitch Williamscc052922014-10-25 03:24:34 +0000990 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400991 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000992
Mitch Williamsa132af22015-01-24 09:58:35 +0000993 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Greg Rose5eae00c2013-12-21 06:12:45 +0000994 ring->next_to_use = ring->count - 1;
995 writel(ring->next_to_use, ring->tail);
996 }
997}
998
999/**
1000 * i40evf_up_complete - Finish the last steps of bringing up a connection
1001 * @adapter: board private structure
1002 **/
1003static int i40evf_up_complete(struct i40evf_adapter *adapter)
1004{
1005 adapter->state = __I40EVF_RUNNING;
1006 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1007
1008 i40evf_napi_enable_all(adapter);
1009
1010 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1011 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1012 return 0;
1013}
1014
1015/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001016 * i40e_down - Shutdown the connection processing
1017 * @adapter: board private structure
1018 **/
1019void i40evf_down(struct i40evf_adapter *adapter)
1020{
1021 struct net_device *netdev = adapter->netdev;
1022 struct i40evf_mac_filter *f;
1023
Mitch Williams209dc4d2015-12-09 15:50:27 -08001024 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001025 return;
1026
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001027 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1028 &adapter->crit_section))
1029 usleep_range(500, 1000);
1030
Mitch Williams63e18c22015-03-27 00:12:10 -07001031 netif_carrier_off(netdev);
1032 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +00001033 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001034 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001035
Mitch Williamsef8693e2014-02-13 03:48:53 -08001036 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001037 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1038 f->remove = true;
1039 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001040 /* remove all VLAN filters */
1041 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1042 f->remove = true;
1043 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001044 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1045 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001046 /* cancel any current operation */
1047 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001048 /* Schedule operations to close down the HW. Don't wait
1049 * here for this to complete. The watchdog is still running
1050 * and it will take care of this.
1051 */
1052 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001053 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001054 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001055 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001056
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001057 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001058}
1059
1060/**
1061 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1062 * @adapter: board private structure
1063 * @vectors: number of vectors to request
1064 *
1065 * Work with the OS to set up the MSIX vectors needed.
1066 *
1067 * Returns 0 on success, negative on failure
1068 **/
1069static int
1070i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1071{
1072 int err, vector_threshold;
1073
1074 /* We'll want at least 3 (vector_threshold):
1075 * 0) Other (Admin Queue and link, mostly)
1076 * 1) TxQ[0] Cleanup
1077 * 2) RxQ[0] Cleanup
1078 */
1079 vector_threshold = MIN_MSIX_COUNT;
1080
1081 /* The more we get, the more we will assign to Tx/Rx Cleanup
1082 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1083 * Right now, we simply care about how many we'll get; we'll
1084 * set them up later while requesting irq's.
1085 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001086 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1087 vector_threshold, vectors);
1088 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001089 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001090 kfree(adapter->msix_entries);
1091 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001092 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001093 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001094
1095 /* Adjust for only the vectors we'll use, which is minimum
1096 * of max_msix_q_vectors + NONQ_VECS, or the number of
1097 * vectors we were allocated.
1098 */
1099 adapter->num_msix_vectors = err;
1100 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001101}
1102
1103/**
1104 * i40evf_free_queues - Free memory for all rings
1105 * @adapter: board private structure to initialize
1106 *
1107 * Free all of the memory associated with queue pairs.
1108 **/
1109static void i40evf_free_queues(struct i40evf_adapter *adapter)
1110{
Greg Rose5eae00c2013-12-21 06:12:45 +00001111 if (!adapter->vsi_res)
1112 return;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001113 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001114 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001115 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001116 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001117}
1118
1119/**
1120 * i40evf_alloc_queues - Allocate memory for all rings
1121 * @adapter: board private structure to initialize
1122 *
1123 * We allocate one ring per queue at run-time since we don't know the
1124 * number of queues at compile-time. The polling_netdev array is
1125 * intended for Multiqueue, but should work fine with a single queue.
1126 **/
1127static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1128{
1129 int i;
1130
Mitch Williams0dd438d2015-10-26 19:44:40 -04001131 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1132 sizeof(struct i40e_ring), GFP_KERNEL);
1133 if (!adapter->tx_rings)
1134 goto err_out;
1135 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1136 sizeof(struct i40e_ring), GFP_KERNEL);
1137 if (!adapter->rx_rings)
1138 goto err_out;
1139
Mitch Williamscc052922014-10-25 03:24:34 +00001140 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001141 struct i40e_ring *tx_ring;
1142 struct i40e_ring *rx_ring;
1143
Mitch Williams0dd438d2015-10-26 19:44:40 -04001144 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001145
1146 tx_ring->queue_index = i;
1147 tx_ring->netdev = adapter->netdev;
1148 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001149 tx_ring->count = adapter->tx_desc_count;
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001150 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1151 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001152
Mitch Williams0dd438d2015-10-26 19:44:40 -04001153 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001154 rx_ring->queue_index = i;
1155 rx_ring->netdev = adapter->netdev;
1156 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001157 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001158 }
1159
1160 return 0;
1161
1162err_out:
1163 i40evf_free_queues(adapter);
1164 return -ENOMEM;
1165}
1166
1167/**
1168 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1169 * @adapter: board private structure to initialize
1170 *
1171 * Attempt to configure the interrupts using the best available
1172 * capabilities of the hardware and the kernel.
1173 **/
1174static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1175{
1176 int vector, v_budget;
1177 int pairs = 0;
1178 int err = 0;
1179
1180 if (!adapter->vsi_res) {
1181 err = -EIO;
1182 goto out;
1183 }
Mitch Williamscc052922014-10-25 03:24:34 +00001184 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001185
1186 /* It's easy to be greedy for MSI-X vectors, but it really
1187 * doesn't do us much good if we have a lot more vectors
1188 * than CPU's. So let's be conservative and only ask for
1189 * (roughly) twice the number of vectors as there are CPU's.
1190 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001191 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1192 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001193
Greg Rose5eae00c2013-12-21 06:12:45 +00001194 adapter->msix_entries = kcalloc(v_budget,
1195 sizeof(struct msix_entry), GFP_KERNEL);
1196 if (!adapter->msix_entries) {
1197 err = -ENOMEM;
1198 goto out;
1199 }
1200
1201 for (vector = 0; vector < v_budget; vector++)
1202 adapter->msix_entries[vector].entry = vector;
1203
Mitch Williams313ed2d2015-08-27 11:42:31 -04001204 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001205
1206out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001207 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1208 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001209 return err;
1210}
1211
1212/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001213 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1214 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001215 *
1216 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001217 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001218static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001219{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001220 struct i40e_aqc_get_set_rss_key_data *rss_key =
1221 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001222 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001223 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001224
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001225 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1226 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001227 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001228 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001229 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001230 }
1231
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001232 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1233 if (ret) {
1234 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1235 i40evf_stat_str(hw, ret),
1236 i40evf_aq_str(hw, hw->aq.asq_last_status));
1237 return ret;
1238
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001239 }
1240
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001241 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1242 adapter->rss_lut, adapter->rss_lut_size);
1243 if (ret) {
1244 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1245 i40evf_stat_str(hw, ret),
1246 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001247 }
1248
1249 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001250
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001251}
1252
1253/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001254 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001255 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001256 *
1257 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001258 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001259static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001260{
1261 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001262 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001263 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001264
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001265 dw = (u32 *)adapter->rss_key;
1266 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1267 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001268
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001269 dw = (u32 *)adapter->rss_lut;
1270 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1271 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001272
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001273 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001274
1275 return 0;
1276}
1277
1278/**
1279 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001280 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001281 *
1282 * Returns 0 on success, negative on failure
1283 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001284int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001285{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001286
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001287 if (RSS_PF(adapter)) {
1288 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1289 I40EVF_FLAG_AQ_SET_RSS_KEY;
1290 return 0;
1291 } else if (RSS_AQ(adapter)) {
1292 return i40evf_config_rss_aq(adapter);
1293 } else {
1294 return i40evf_config_rss_reg(adapter);
1295 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001296}
1297
1298/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001299 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001300 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001301 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001302static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001303{
1304 u16 i;
1305
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001306 for (i = 0; i < adapter->rss_lut_size; i++)
1307 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001308}
1309
1310/**
Helin Zhang96a81982015-10-26 19:44:31 -04001311 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001312 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001313 *
1314 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001315 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001316static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001317{
1318 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001319 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001320
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001321 if (!RSS_PF(adapter)) {
1322 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1323 if (adapter->vf_res->vf_offload_flags &
1324 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1325 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1326 else
1327 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001328
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001329 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1330 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1331 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001332
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001333 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001334
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001335 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1336 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001337
1338 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001339}
1340
1341/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001342 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1343 * @adapter: board private structure to initialize
1344 *
1345 * We allocate one q_vector per queue interrupt. If allocation fails we
1346 * return -ENOMEM.
1347 **/
1348static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1349{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001350 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001351 struct i40e_q_vector *q_vector;
1352
1353 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001354 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001355 GFP_KERNEL);
1356 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001357 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001358
1359 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001360 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001361 q_vector->adapter = adapter;
1362 q_vector->vsi = &adapter->vsi;
1363 q_vector->v_idx = q_idx;
1364 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001365 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001366 }
1367
1368 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001369}
1370
1371/**
1372 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1373 * @adapter: board private structure to initialize
1374 *
1375 * This function frees the memory allocated to the q_vectors. In addition if
1376 * NAPI is enabled it will delete any references to the NAPI struct prior
1377 * to freeing the q_vector.
1378 **/
1379static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1380{
1381 int q_idx, num_q_vectors;
1382 int napi_vectors;
1383
1384 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001385 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001386
1387 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001388 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001389 if (q_idx < napi_vectors)
1390 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001391 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001392 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001393}
1394
1395/**
1396 * i40evf_reset_interrupt_capability - Reset MSIX setup
1397 * @adapter: board private structure
1398 *
1399 **/
1400void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1401{
1402 pci_disable_msix(adapter->pdev);
1403 kfree(adapter->msix_entries);
1404 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001405}
1406
1407/**
1408 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1409 * @adapter: board private structure to initialize
1410 *
1411 **/
1412int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1413{
1414 int err;
1415
1416 err = i40evf_set_interrupt_capability(adapter);
1417 if (err) {
1418 dev_err(&adapter->pdev->dev,
1419 "Unable to setup interrupt capabilities\n");
1420 goto err_set_interrupt;
1421 }
1422
1423 err = i40evf_alloc_q_vectors(adapter);
1424 if (err) {
1425 dev_err(&adapter->pdev->dev,
1426 "Unable to allocate memory for queue vectors\n");
1427 goto err_alloc_q_vectors;
1428 }
1429
1430 err = i40evf_alloc_queues(adapter);
1431 if (err) {
1432 dev_err(&adapter->pdev->dev,
1433 "Unable to allocate memory for queues\n");
1434 goto err_alloc_queues;
1435 }
1436
1437 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001438 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1439 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001440
1441 return 0;
1442err_alloc_queues:
1443 i40evf_free_q_vectors(adapter);
1444err_alloc_q_vectors:
1445 i40evf_reset_interrupt_capability(adapter);
1446err_set_interrupt:
1447 return err;
1448}
1449
1450/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001451 * i40evf_free_rss - Free memory used by RSS structs
1452 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001453 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001454static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001455{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001456 kfree(adapter->rss_key);
1457 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001458
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001459 kfree(adapter->rss_lut);
1460 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001461}
1462
1463/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001464 * i40evf_watchdog_timer - Periodic call-back timer
1465 * @data: pointer to adapter disguised as unsigned long
1466 **/
1467static void i40evf_watchdog_timer(unsigned long data)
1468{
1469 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001470
Greg Rose5eae00c2013-12-21 06:12:45 +00001471 schedule_work(&adapter->watchdog_task);
1472 /* timer will be rescheduled in watchdog task */
1473}
1474
1475/**
1476 * i40evf_watchdog_task - Periodic call-back task
1477 * @work: pointer to work_struct
1478 **/
1479static void i40evf_watchdog_task(struct work_struct *work)
1480{
1481 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001482 struct i40evf_adapter,
1483 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001484 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001485 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001486
Greg Rose5eae00c2013-12-21 06:12:45 +00001487 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001488 goto restart_watchdog;
1489
1490 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001491 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1492 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1493 if ((reg_val == I40E_VFR_VFACTIVE) ||
1494 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001495 /* A chance for redemption! */
1496 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1497 adapter->state = __I40EVF_STARTUP;
1498 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1499 schedule_delayed_work(&adapter->init_task, 10);
1500 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1501 &adapter->crit_section);
1502 /* Don't reschedule the watchdog, since we've restarted
1503 * the init task. When init_task contacts the PF and
1504 * gets everything set up again, it'll restart the
1505 * watchdog for us. Down, boy. Sit. Stay. Woof.
1506 */
1507 return;
1508 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001509 adapter->aq_required = 0;
1510 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1511 goto watchdog_done;
1512 }
1513
1514 if ((adapter->state < __I40EVF_DOWN) ||
1515 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001516 goto watchdog_done;
1517
Mitch Williamsef8693e2014-02-13 03:48:53 -08001518 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001519 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1520 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001521 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001522 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001523 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001524 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001525 adapter->aq_required = 0;
1526 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001527 goto watchdog_done;
1528 }
1529
1530 /* Process admin queue tasks. After init, everything gets done
1531 * here so we don't race on the admin queue.
1532 */
Mitch Williamsed636962015-04-07 19:45:32 -04001533 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001534 if (!i40evf_asq_done(hw)) {
1535 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1536 i40evf_send_api_ver(adapter);
1537 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001538 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001539 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001540 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1541 i40evf_send_vf_config_msg(adapter);
1542 goto watchdog_done;
1543 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001544
Mitch Williamse284fc82015-03-27 00:12:09 -07001545 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1546 i40evf_disable_queues(adapter);
1547 goto watchdog_done;
1548 }
1549
Greg Rose5eae00c2013-12-21 06:12:45 +00001550 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1551 i40evf_map_queues(adapter);
1552 goto watchdog_done;
1553 }
1554
1555 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1556 i40evf_add_ether_addrs(adapter);
1557 goto watchdog_done;
1558 }
1559
1560 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1561 i40evf_add_vlans(adapter);
1562 goto watchdog_done;
1563 }
1564
1565 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1566 i40evf_del_ether_addrs(adapter);
1567 goto watchdog_done;
1568 }
1569
1570 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1571 i40evf_del_vlans(adapter);
1572 goto watchdog_done;
1573 }
1574
Greg Rose5eae00c2013-12-21 06:12:45 +00001575 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1576 i40evf_configure_queues(adapter);
1577 goto watchdog_done;
1578 }
1579
1580 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1581 i40evf_enable_queues(adapter);
1582 goto watchdog_done;
1583 }
1584
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001585 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1586 /* This message goes straight to the firmware, not the
1587 * PF, so we don't have to set current_op as we will
1588 * not get a response through the ARQ.
1589 */
Helin Zhang96a81982015-10-26 19:44:31 -04001590 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001591 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1592 goto watchdog_done;
1593 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001594 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1595 i40evf_get_hena(adapter);
1596 goto watchdog_done;
1597 }
1598 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1599 i40evf_set_hena(adapter);
1600 goto watchdog_done;
1601 }
1602 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1603 i40evf_set_rss_key(adapter);
1604 goto watchdog_done;
1605 }
1606 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1607 i40evf_set_rss_lut(adapter);
1608 goto watchdog_done;
1609 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001610
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001611 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1612 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_UNICAST_PROMISC |
1613 I40E_FLAG_VF_MULTICAST_PROMISC);
1614 goto watchdog_done;
1615 }
1616
1617 if (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) {
1618 i40evf_set_promiscuous(adapter, 0);
1619 goto watchdog_done;
1620 }
1621
Greg Rose5eae00c2013-12-21 06:12:45 +00001622 if (adapter->state == __I40EVF_RUNNING)
1623 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001624watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001625 if (adapter->state == __I40EVF_RUNNING) {
1626 i40evf_irq_enable_queues(adapter, ~0);
1627 i40evf_fire_sw_int(adapter, 0xFF);
1628 } else {
1629 i40evf_fire_sw_int(adapter, 0x1);
1630 }
1631
Mitch Williamsef8693e2014-02-13 03:48:53 -08001632 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1633restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001634 if (adapter->state == __I40EVF_REMOVE)
1635 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001636 if (adapter->aq_required)
1637 mod_timer(&adapter->watchdog_timer,
1638 jiffies + msecs_to_jiffies(20));
1639 else
1640 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001641 schedule_work(&adapter->adminq_task);
1642}
1643
Mitch Williams67c818a2015-06-19 08:56:30 -07001644#define I40EVF_RESET_WAIT_MS 10
1645#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001646/**
1647 * i40evf_reset_task - Call-back task to handle hardware reset
1648 * @work: pointer to work_struct
1649 *
1650 * During reset we need to shut down and reinitialize the admin queue
1651 * before we can use it to communicate with the PF again. We also clear
1652 * and reinit the rings because that context is lost as well.
1653 **/
1654static void i40evf_reset_task(struct work_struct *work)
1655{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001656 struct i40evf_adapter *adapter = container_of(work,
1657 struct i40evf_adapter,
1658 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001659 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001660 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001661 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001662 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001663 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001664 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001665
1666 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1667 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001668 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001669
Mitch Williams67c818a2015-06-19 08:56:30 -07001670 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001671 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001672 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1673 /* Restart the AQ here. If we have been reset but didn't
1674 * detect it, or if the PF had to reinit, our AQ will be hosed.
1675 */
1676 i40evf_shutdown_adminq(hw);
1677 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001678 i40evf_request_reset(adapter);
1679 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001680 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001681
Mitch Williamsef8693e2014-02-13 03:48:53 -08001682 /* poll until we see the reset actually happen */
1683 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001684 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1685 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1686 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001687 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001688 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001689 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001690 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001691 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001692 goto continue_reset; /* act like the reset happened */
1693 }
1694
1695 /* wait until the reset is complete and the PF is responding to us */
1696 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001697 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1698 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1699 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001700 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001701 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001702 }
Mitch Williams509a4472015-12-23 12:05:52 -08001703 pci_set_master(adapter->pdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001704 /* extra wait to make sure minimum wait is met */
1705 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001706 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001707 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001708 struct i40evf_vlan_filter *fv, *fvtmp;
1709
Greg Rose5eae00c2013-12-21 06:12:45 +00001710 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001711 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001712 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001713 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1714
Mitch Williams169f4072014-04-01 07:11:50 +00001715 if (netif_running(adapter->netdev)) {
1716 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001717 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001718 netif_tx_disable(netdev);
1719 i40evf_napi_disable_all(adapter);
1720 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001721 i40evf_free_traffic_irqs(adapter);
1722 i40evf_free_all_tx_resources(adapter);
1723 i40evf_free_all_rx_resources(adapter);
1724 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001725
1726 /* Delete all of the filters, both MAC and VLAN. */
1727 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1728 list) {
1729 list_del(&f->list);
1730 kfree(f);
1731 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001732
Mitch Williams6ba36a22014-08-01 13:27:14 -07001733 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1734 list) {
1735 list_del(&fv->list);
1736 kfree(fv);
1737 }
1738
Mitch Williamsef8693e2014-02-13 03:48:53 -08001739 i40evf_free_misc_irq(adapter);
1740 i40evf_reset_interrupt_capability(adapter);
1741 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001742 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001743 kfree(adapter->vf_res);
1744 i40evf_shutdown_adminq(hw);
1745 adapter->netdev->flags &= ~IFF_UP;
1746 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001747 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Mitch Williams9b9344f2016-01-15 14:33:19 -08001748 adapter->state = __I40EVF_DOWN;
Mitch Williams67c818a2015-06-19 08:56:30 -07001749 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001750 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001751 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001752
1753continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001754 if (netif_running(adapter->netdev)) {
1755 netif_carrier_off(netdev);
1756 netif_tx_stop_all_queues(netdev);
1757 i40evf_napi_disable_all(adapter);
1758 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001759 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001760
Greg Rose5eae00c2013-12-21 06:12:45 +00001761 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001762 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1763
1764 /* free the Tx/Rx rings and descriptors, might be better to just
1765 * re-use them sometime in the future
1766 */
1767 i40evf_free_all_rx_resources(adapter);
1768 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001769
1770 /* kill and reinit the admin queue */
1771 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001772 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1773 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001774 err = i40evf_init_adminq(hw);
1775 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001776 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1777 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001778
Mitch Williamse6d038d2015-06-04 16:23:58 -04001779 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1780 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001781
1782 /* re-add all MAC filters */
1783 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1784 f->add = true;
1785 }
1786 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001787 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1788 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001789 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001790 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001791 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001792 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001793 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001794
1795 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1796
1797 if (netif_running(adapter->netdev)) {
1798 /* allocate transmit descriptors */
1799 err = i40evf_setup_all_tx_resources(adapter);
1800 if (err)
1801 goto reset_err;
1802
1803 /* allocate receive descriptors */
1804 err = i40evf_setup_all_rx_resources(adapter);
1805 if (err)
1806 goto reset_err;
1807
1808 i40evf_configure(adapter);
1809
1810 err = i40evf_up_complete(adapter);
1811 if (err)
1812 goto reset_err;
1813
1814 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001815 } else {
1816 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001817 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001818
Greg Rose5eae00c2013-12-21 06:12:45 +00001819 return;
1820reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001821 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001822 i40evf_close(adapter->netdev);
1823}
1824
1825/**
1826 * i40evf_adminq_task - worker thread to clean the admin queue
1827 * @work: pointer to work_struct containing our data
1828 **/
1829static void i40evf_adminq_task(struct work_struct *work)
1830{
1831 struct i40evf_adapter *adapter =
1832 container_of(work, struct i40evf_adapter, adminq_task);
1833 struct i40e_hw *hw = &adapter->hw;
1834 struct i40e_arq_event_info event;
1835 struct i40e_virtchnl_msg *v_msg;
1836 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001837 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001838 u16 pending;
1839
Mitch Williamsef8693e2014-02-13 03:48:53 -08001840 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001841 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001842
Mitch Williams1001dc32014-11-11 20:02:19 +00001843 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1844 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001845 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001846 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001847
Greg Rose5eae00c2013-12-21 06:12:45 +00001848 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1849 do {
1850 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001851 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001852 break; /* No event to process or error cleaning ARQ */
1853
1854 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1855 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001856 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001857 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001858 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001859 } while (pending);
1860
Mitch Williams67c818a2015-06-19 08:56:30 -07001861 if ((adapter->flags &
1862 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1863 adapter->state == __I40EVF_RESETTING)
1864 goto freedom;
1865
Mitch Williams912257e2014-05-22 06:32:07 +00001866 /* check for error indications */
1867 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001868 if (val == 0xdeadbeef) /* indicates device in reset */
1869 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001870 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001871 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001872 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001873 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001874 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001875 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001876 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001877 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001878 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001879 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001880 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001881 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001882 }
1883 if (oldval != val)
1884 wr32(hw, hw->aq.arq.len, val);
1885
1886 val = rd32(hw, hw->aq.asq.len);
1887 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001888 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001889 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001890 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001891 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001892 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001893 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001894 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001895 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001896 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001897 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001898 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001899 }
1900 if (oldval != val)
1901 wr32(hw, hw->aq.asq.len, val);
1902
Mitch Williams67c818a2015-06-19 08:56:30 -07001903freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001904 kfree(event.msg_buf);
1905out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001906 /* re-enable Admin queue interrupt cause */
1907 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001908}
1909
1910/**
1911 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1912 * @adapter: board private structure
1913 *
1914 * Free all transmit software resources
1915 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001916void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001917{
1918 int i;
1919
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08001920 if (!adapter->tx_rings)
1921 return;
1922
Mitch Williamscc052922014-10-25 03:24:34 +00001923 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04001924 if (adapter->tx_rings[i].desc)
1925 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001926}
1927
1928/**
1929 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1930 * @adapter: board private structure
1931 *
1932 * If this function returns with an error, then it's possible one or
1933 * more of the rings is populated (while the rest are not). It is the
1934 * callers duty to clean those orphaned rings.
1935 *
1936 * Return 0 on success, negative on failure
1937 **/
1938static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1939{
1940 int i, err = 0;
1941
Mitch Williamscc052922014-10-25 03:24:34 +00001942 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001943 adapter->tx_rings[i].count = adapter->tx_desc_count;
1944 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001945 if (!err)
1946 continue;
1947 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001948 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001949 break;
1950 }
1951
1952 return err;
1953}
1954
1955/**
1956 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1957 * @adapter: board private structure
1958 *
1959 * If this function returns with an error, then it's possible one or
1960 * more of the rings is populated (while the rest are not). It is the
1961 * callers duty to clean those orphaned rings.
1962 *
1963 * Return 0 on success, negative on failure
1964 **/
1965static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1966{
1967 int i, err = 0;
1968
Mitch Williamscc052922014-10-25 03:24:34 +00001969 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001970 adapter->rx_rings[i].count = adapter->rx_desc_count;
1971 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001972 if (!err)
1973 continue;
1974 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001975 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001976 break;
1977 }
1978 return err;
1979}
1980
1981/**
1982 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1983 * @adapter: board private structure
1984 *
1985 * Free all receive software resources
1986 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001987void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001988{
1989 int i;
1990
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08001991 if (!adapter->rx_rings)
1992 return;
1993
Mitch Williamscc052922014-10-25 03:24:34 +00001994 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04001995 if (adapter->rx_rings[i].desc)
1996 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001997}
1998
1999/**
2000 * i40evf_open - Called when a network interface is made active
2001 * @netdev: network interface device structure
2002 *
2003 * Returns 0 on success, negative value on failure
2004 *
2005 * The open entry point is called when a network interface is made
2006 * active by the system (IFF_UP). At this point all resources needed
2007 * for transmit and receive operations are allocated, the interrupt
2008 * handler is registered with the OS, the watchdog timer is started,
2009 * and the stack is notified that the interface is ready.
2010 **/
2011static int i40evf_open(struct net_device *netdev)
2012{
2013 struct i40evf_adapter *adapter = netdev_priv(netdev);
2014 int err;
2015
Mitch Williamsef8693e2014-02-13 03:48:53 -08002016 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2017 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2018 return -EIO;
2019 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002020
2021 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002022 return -EBUSY;
2023
2024 /* allocate transmit descriptors */
2025 err = i40evf_setup_all_tx_resources(adapter);
2026 if (err)
2027 goto err_setup_tx;
2028
2029 /* allocate receive descriptors */
2030 err = i40evf_setup_all_rx_resources(adapter);
2031 if (err)
2032 goto err_setup_rx;
2033
2034 /* clear any pending interrupts, may auto mask */
2035 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2036 if (err)
2037 goto err_req_irq;
2038
Mitch Williams44151cd2015-04-27 14:57:17 -04002039 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002040 i40evf_configure(adapter);
2041
2042 err = i40evf_up_complete(adapter);
2043 if (err)
2044 goto err_req_irq;
2045
2046 i40evf_irq_enable(adapter, true);
2047
2048 return 0;
2049
2050err_req_irq:
2051 i40evf_down(adapter);
2052 i40evf_free_traffic_irqs(adapter);
2053err_setup_rx:
2054 i40evf_free_all_rx_resources(adapter);
2055err_setup_tx:
2056 i40evf_free_all_tx_resources(adapter);
2057
2058 return err;
2059}
2060
2061/**
2062 * i40evf_close - Disables a network interface
2063 * @netdev: network interface device structure
2064 *
2065 * Returns 0, this is not allowed to fail
2066 *
2067 * The close entry point is called when an interface is de-activated
2068 * by the OS. The hardware is still under the drivers control, but
2069 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2070 * are freed, along with all transmit and receive resources.
2071 **/
2072static int i40evf_close(struct net_device *netdev)
2073{
2074 struct i40evf_adapter *adapter = netdev_priv(netdev);
2075
Mitch Williams209dc4d2015-12-09 15:50:27 -08002076 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002077 return 0;
2078
Mitch Williamsef8693e2014-02-13 03:48:53 -08002079
Greg Rose5eae00c2013-12-21 06:12:45 +00002080 set_bit(__I40E_DOWN, &adapter->vsi.state);
2081
2082 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002083 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002084 i40evf_free_traffic_irqs(adapter);
2085
Greg Rose5eae00c2013-12-21 06:12:45 +00002086 return 0;
2087}
2088
2089/**
2090 * i40evf_get_stats - Get System Network Statistics
2091 * @netdev: network interface device structure
2092 *
2093 * Returns the address of the device statistics structure.
2094 * The statistics are actually updated from the timer callback.
2095 **/
2096static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2097{
2098 struct i40evf_adapter *adapter = netdev_priv(netdev);
2099
2100 /* only return the current stats */
2101 return &adapter->net_stats;
2102}
2103
2104/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002105 * i40evf_change_mtu - Change the Maximum Transfer Unit
2106 * @netdev: network interface device structure
2107 * @new_mtu: new value for maximum frame size
2108 *
2109 * Returns 0 on success, negative on failure
2110 **/
2111static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2112{
2113 struct i40evf_adapter *adapter = netdev_priv(netdev);
2114 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2115
2116 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2117 return -EINVAL;
2118
Greg Rose5eae00c2013-12-21 06:12:45 +00002119 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002120 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2121 schedule_work(&adapter->reset_task);
2122
Greg Rose5eae00c2013-12-21 06:12:45 +00002123 return 0;
2124}
2125
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002126#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2127 NETIF_F_HW_VLAN_CTAG_RX |\
2128 NETIF_F_HW_VLAN_CTAG_FILTER)
2129
2130/**
2131 * i40evf_fix_features - fix up the netdev feature bits
2132 * @netdev: our net device
2133 * @features: desired feature bits
2134 *
2135 * Returns fixed-up features bits
2136 **/
2137static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2138 netdev_features_t features)
2139{
2140 struct i40evf_adapter *adapter = netdev_priv(netdev);
2141
2142 features &= ~I40EVF_VLAN_FEATURES;
2143 if (adapter->vf_res->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN)
2144 features |= I40EVF_VLAN_FEATURES;
2145 return features;
2146}
2147
Greg Rose5eae00c2013-12-21 06:12:45 +00002148static const struct net_device_ops i40evf_netdev_ops = {
2149 .ndo_open = i40evf_open,
2150 .ndo_stop = i40evf_close,
2151 .ndo_start_xmit = i40evf_xmit_frame,
2152 .ndo_get_stats = i40evf_get_stats,
2153 .ndo_set_rx_mode = i40evf_set_rx_mode,
2154 .ndo_validate_addr = eth_validate_addr,
2155 .ndo_set_mac_address = i40evf_set_mac,
2156 .ndo_change_mtu = i40evf_change_mtu,
2157 .ndo_tx_timeout = i40evf_tx_timeout,
2158 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2159 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002160 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002161#ifdef CONFIG_NET_POLL_CONTROLLER
2162 .ndo_poll_controller = i40evf_netpoll,
2163#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002164};
2165
2166/**
2167 * i40evf_check_reset_complete - check that VF reset is complete
2168 * @hw: pointer to hw struct
2169 *
2170 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2171 **/
2172static int i40evf_check_reset_complete(struct i40e_hw *hw)
2173{
2174 u32 rstat;
2175 int i;
2176
2177 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002178 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2179 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2180 if ((rstat == I40E_VFR_VFACTIVE) ||
2181 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002182 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002183 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002184 }
2185 return -EBUSY;
2186}
2187
2188/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002189 * i40evf_process_config - Process the config information we got from the PF
2190 * @adapter: board private structure
2191 *
2192 * Verify that we have a valid config struct, and set up our netdev features
2193 * and our VSI struct.
2194 **/
2195int i40evf_process_config(struct i40evf_adapter *adapter)
2196{
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002197 struct i40e_virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002198 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002199 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002200 int i;
2201
2202 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002203 for (i = 0; i < vfres->num_vsis; i++) {
2204 if (vfres->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2205 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002206 }
2207 if (!adapter->vsi_res) {
2208 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2209 return -ENODEV;
2210 }
2211
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002212 netdev->hw_enc_features |= NETIF_F_SG |
2213 NETIF_F_IP_CSUM |
2214 NETIF_F_IPV6_CSUM |
2215 NETIF_F_HIGHDMA |
2216 NETIF_F_SOFT_FEATURES |
2217 NETIF_F_TSO |
2218 NETIF_F_TSO_ECN |
2219 NETIF_F_TSO6 |
2220 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002221 NETIF_F_GSO_GRE_CSUM |
Alexander Duyck577389a2016-04-02 00:06:56 -07002222 NETIF_F_GSO_IPIP |
2223 NETIF_F_GSO_SIT |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002224 NETIF_F_GSO_UDP_TUNNEL |
2225 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002226 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002227 NETIF_F_SCTP_CRC |
2228 NETIF_F_RXHASH |
2229 NETIF_F_RXCSUM |
2230 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002231
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002232 if (!(adapter->flags & I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002233 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2234
2235 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002236
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002237 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002238 netdev->vlan_features |= netdev->hw_enc_features |
2239 NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002240
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002241 /* Write features and hw_features separately to avoid polluting
2242 * with, or dropping, features that are set when we registgered.
2243 */
2244 netdev->hw_features |= netdev->hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002245
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002246 netdev->features |= netdev->hw_enc_features | I40EVF_VLAN_FEATURES;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002247 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002248
2249 /* disable VLAN features if not supported */
2250 if (!(vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN))
2251 netdev->features ^= I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002252
2253 adapter->vsi.id = adapter->vsi_res->vsi_id;
2254
2255 adapter->vsi.back = adapter;
2256 adapter->vsi.base_vector = 1;
2257 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2258 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2259 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2260 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2261 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002262 vsi->netdev = adapter->netdev;
2263 vsi->qs_handle = adapter->vsi_res->qset_handle;
2264 if (vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2265 adapter->rss_key_size = vfres->rss_key_size;
2266 adapter->rss_lut_size = vfres->rss_lut_size;
2267 } else {
2268 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2269 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2270 }
2271
Mitch Williamse6d038d2015-06-04 16:23:58 -04002272 return 0;
2273}
2274
2275/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002276 * i40evf_init_task - worker thread to perform delayed initialization
2277 * @work: pointer to work_struct containing our data
2278 *
2279 * This task completes the work that was begun in probe. Due to the nature
2280 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002281 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002282 * whole system, we'll do it in a task so we can sleep.
2283 * This task only runs during driver init. Once we've established
2284 * communications with the PF driver and set up our netdev, the watchdog
2285 * takes over.
2286 **/
2287static void i40evf_init_task(struct work_struct *work)
2288{
2289 struct i40evf_adapter *adapter = container_of(work,
2290 struct i40evf_adapter,
2291 init_task.work);
2292 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002293 struct i40e_hw *hw = &adapter->hw;
2294 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002295 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002296
2297 switch (adapter->state) {
2298 case __I40EVF_STARTUP:
2299 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002300 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2301 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002302 err = i40e_set_mac_type(hw);
2303 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002304 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2305 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002306 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002307 }
2308 err = i40evf_check_reset_complete(hw);
2309 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002310 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002311 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002312 goto err;
2313 }
2314 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2315 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2316 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2317 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2318
2319 err = i40evf_init_adminq(hw);
2320 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002321 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2322 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002323 goto err;
2324 }
2325 err = i40evf_send_api_ver(adapter);
2326 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002327 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002328 i40evf_shutdown_adminq(hw);
2329 goto err;
2330 }
2331 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2332 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002333 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002334 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002335 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002336 i40evf_shutdown_adminq(hw);
2337 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002338 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002339 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002340
2341 /* aq msg sent, awaiting reply */
2342 err = i40evf_verify_api_ver(adapter);
2343 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002344 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002345 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002346 else
2347 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2348 adapter->pf_version.major,
2349 adapter->pf_version.minor,
2350 I40E_VIRTCHNL_VERSION_MAJOR,
2351 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002352 goto err;
2353 }
2354 err = i40evf_send_vf_config_msg(adapter);
2355 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002356 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002357 err);
2358 goto err;
2359 }
2360 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2361 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002362 case __I40EVF_INIT_GET_RESOURCES:
2363 /* aq msg sent, awaiting reply */
2364 if (!adapter->vf_res) {
2365 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2366 (I40E_MAX_VF_VSI *
2367 sizeof(struct i40e_virtchnl_vsi_resource));
2368 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002369 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002370 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002371 }
2372 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002373 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002374 err = i40evf_send_vf_config_msg(adapter);
2375 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002376 } else if (err == I40E_ERR_PARAM) {
2377 /* We only get ERR_PARAM if the device is in a very bad
2378 * state or if we've been disabled for previous bad
2379 * behavior. Either way, we're done now.
2380 */
2381 i40evf_shutdown_adminq(hw);
2382 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2383 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002384 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002385 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002386 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2387 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002388 goto err_alloc;
2389 }
2390 adapter->state = __I40EVF_INIT_SW;
2391 break;
2392 default:
2393 goto err_alloc;
2394 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002395
2396 if (hw->mac.type == I40E_MAC_X722_VF)
2397 adapter->flags |= I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE;
2398
Mitch Williamse6d038d2015-06-04 16:23:58 -04002399 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002400 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002401 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002402
2403 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
Mitch Williams00e5ec42016-01-15 14:33:10 -08002404 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002405
Greg Rose5eae00c2013-12-21 06:12:45 +00002406 netdev->netdev_ops = &i40evf_netdev_ops;
2407 i40evf_set_ethtool_ops(netdev);
2408 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002409
Greg Rose5eae00c2013-12-21 06:12:45 +00002410 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002411 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002412 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002413 eth_hw_addr_random(netdev);
2414 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2415 } else {
2416 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2417 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2418 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002419 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002420
Greg Rose5eae00c2013-12-21 06:12:45 +00002421 init_timer(&adapter->watchdog_timer);
2422 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2423 adapter->watchdog_timer.data = (unsigned long)adapter;
2424 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2425
Mitch Williamscc052922014-10-25 03:24:34 +00002426 adapter->num_active_queues = min_t(int,
2427 adapter->vsi_res->num_queue_pairs,
2428 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002429 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2430 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002431 err = i40evf_init_interrupt_scheme(adapter);
2432 if (err)
2433 goto err_sw_init;
2434 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002435 if (adapter->vf_res->vf_offload_flags &
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002436 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2437 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2438
Greg Rose5eae00c2013-12-21 06:12:45 +00002439 err = i40evf_request_misc_irq(adapter);
2440 if (err)
2441 goto err_sw_init;
2442
2443 netif_carrier_off(netdev);
2444
Mitch Williamsef8693e2014-02-13 03:48:53 -08002445 if (!adapter->netdev_registered) {
2446 err = register_netdev(netdev);
2447 if (err)
2448 goto err_register;
2449 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002450
2451 adapter->netdev_registered = true;
2452
2453 netif_tx_stop_all_queues(netdev);
2454
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002455 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002456 if (netdev->features & NETIF_F_GRO)
2457 dev_info(&pdev->dev, "GRO is enabled\n");
2458
Greg Rose5eae00c2013-12-21 06:12:45 +00002459 adapter->state = __I40EVF_DOWN;
2460 set_bit(__I40E_DOWN, &adapter->vsi.state);
2461 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002462
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002463 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2464 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2465 if (!adapter->rss_key || !adapter->rss_lut)
2466 goto err_mem;
2467
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002468 if (RSS_AQ(adapter)) {
2469 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2470 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2471 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002472 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002473 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002474 return;
2475restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002476 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002477 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002478err_mem:
2479 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002480err_register:
2481 i40evf_free_misc_irq(adapter);
2482err_sw_init:
2483 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002484err_alloc:
2485 kfree(adapter->vf_res);
2486 adapter->vf_res = NULL;
2487err:
2488 /* Things went into the weeds, so try again later */
2489 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002490 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002491 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002492 i40evf_shutdown_adminq(hw);
2493 adapter->state = __I40EVF_STARTUP;
2494 schedule_delayed_work(&adapter->init_task, HZ * 5);
2495 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002496 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002497 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002498}
2499
2500/**
2501 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2502 * @pdev: pci device structure
2503 **/
2504static void i40evf_shutdown(struct pci_dev *pdev)
2505{
2506 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002507 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002508
2509 netif_device_detach(netdev);
2510
2511 if (netif_running(netdev))
2512 i40evf_close(netdev);
2513
Mitch Williams00293fd2015-01-09 11:18:18 +00002514 /* Prevent the watchdog from running. */
2515 adapter->state = __I40EVF_REMOVE;
2516 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002517
Greg Rose5eae00c2013-12-21 06:12:45 +00002518#ifdef CONFIG_PM
2519 pci_save_state(pdev);
2520
2521#endif
2522 pci_disable_device(pdev);
2523}
2524
2525/**
2526 * i40evf_probe - Device Initialization Routine
2527 * @pdev: PCI device information struct
2528 * @ent: entry in i40evf_pci_tbl
2529 *
2530 * Returns 0 on success, negative on failure
2531 *
2532 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2533 * The OS initialization, configuring of the adapter private structure,
2534 * and a hardware reset occur.
2535 **/
2536static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2537{
2538 struct net_device *netdev;
2539 struct i40evf_adapter *adapter = NULL;
2540 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002541 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002542
2543 err = pci_enable_device(pdev);
2544 if (err)
2545 return err;
2546
Mitch Williams64942942014-02-11 08:26:33 +00002547 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002548 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002549 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2550 if (err) {
2551 dev_err(&pdev->dev,
2552 "DMA configuration failed: 0x%x\n", err);
2553 goto err_dma;
2554 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002555 }
2556
2557 err = pci_request_regions(pdev, i40evf_driver_name);
2558 if (err) {
2559 dev_err(&pdev->dev,
2560 "pci_request_regions failed 0x%x\n", err);
2561 goto err_pci_reg;
2562 }
2563
2564 pci_enable_pcie_error_reporting(pdev);
2565
2566 pci_set_master(pdev);
2567
Mitch Williams1255b7a2015-11-06 15:25:59 -08002568 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002569 if (!netdev) {
2570 err = -ENOMEM;
2571 goto err_alloc_etherdev;
2572 }
2573
2574 SET_NETDEV_DEV(netdev, &pdev->dev);
2575
2576 pci_set_drvdata(pdev, netdev);
2577 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002578
2579 adapter->netdev = netdev;
2580 adapter->pdev = pdev;
2581
2582 hw = &adapter->hw;
2583 hw->back = adapter;
2584
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002585 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002586 adapter->state = __I40EVF_STARTUP;
2587
2588 /* Call save state here because it relies on the adapter struct. */
2589 pci_save_state(pdev);
2590
2591 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2592 pci_resource_len(pdev, 0));
2593 if (!hw->hw_addr) {
2594 err = -EIO;
2595 goto err_ioremap;
2596 }
2597 hw->vendor_id = pdev->vendor;
2598 hw->device_id = pdev->device;
2599 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2600 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2601 hw->subsystem_device_id = pdev->subsystem_device;
2602 hw->bus.device = PCI_SLOT(pdev->devfn);
2603 hw->bus.func = PCI_FUNC(pdev->devfn);
2604
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002605 /* set up the locks for the AQ, do this only once in probe
2606 * and destroy them only once in remove
2607 */
2608 mutex_init(&hw->aq.asq_mutex);
2609 mutex_init(&hw->aq.arq_mutex);
2610
Serey Kong8bb1a542014-08-01 13:27:15 -07002611 INIT_LIST_HEAD(&adapter->mac_filter_list);
2612 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2613
Greg Rose5eae00c2013-12-21 06:12:45 +00002614 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2615 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2616 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2617 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002618 schedule_delayed_work(&adapter->init_task,
2619 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002620
2621 return 0;
2622
2623err_ioremap:
2624 free_netdev(netdev);
2625err_alloc_etherdev:
2626 pci_release_regions(pdev);
2627err_pci_reg:
2628err_dma:
2629 pci_disable_device(pdev);
2630 return err;
2631}
2632
2633#ifdef CONFIG_PM
2634/**
2635 * i40evf_suspend - Power management suspend routine
2636 * @pdev: PCI device information struct
2637 * @state: unused
2638 *
2639 * Called when the system (VM) is entering sleep/suspend.
2640 **/
2641static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2642{
2643 struct net_device *netdev = pci_get_drvdata(pdev);
2644 struct i40evf_adapter *adapter = netdev_priv(netdev);
2645 int retval = 0;
2646
2647 netif_device_detach(netdev);
2648
2649 if (netif_running(netdev)) {
2650 rtnl_lock();
2651 i40evf_down(adapter);
2652 rtnl_unlock();
2653 }
2654 i40evf_free_misc_irq(adapter);
2655 i40evf_reset_interrupt_capability(adapter);
2656
2657 retval = pci_save_state(pdev);
2658 if (retval)
2659 return retval;
2660
2661 pci_disable_device(pdev);
2662
2663 return 0;
2664}
2665
2666/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002667 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002668 * @pdev: PCI device information struct
2669 *
2670 * Called when the system (VM) is resumed from sleep/suspend.
2671 **/
2672static int i40evf_resume(struct pci_dev *pdev)
2673{
2674 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2675 struct net_device *netdev = adapter->netdev;
2676 u32 err;
2677
2678 pci_set_power_state(pdev, PCI_D0);
2679 pci_restore_state(pdev);
2680 /* pci_restore_state clears dev->state_saved so call
2681 * pci_save_state to restore it.
2682 */
2683 pci_save_state(pdev);
2684
2685 err = pci_enable_device_mem(pdev);
2686 if (err) {
2687 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2688 return err;
2689 }
2690 pci_set_master(pdev);
2691
2692 rtnl_lock();
2693 err = i40evf_set_interrupt_capability(adapter);
2694 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002695 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002696 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2697 return err;
2698 }
2699 err = i40evf_request_misc_irq(adapter);
2700 rtnl_unlock();
2701 if (err) {
2702 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2703 return err;
2704 }
2705
2706 schedule_work(&adapter->reset_task);
2707
2708 netif_device_attach(netdev);
2709
2710 return err;
2711}
2712
2713#endif /* CONFIG_PM */
2714/**
2715 * i40evf_remove - Device Removal Routine
2716 * @pdev: PCI device information struct
2717 *
2718 * i40evf_remove is called by the PCI subsystem to alert the driver
2719 * that it should release a PCI device. The could be caused by a
2720 * Hot-Plug event, or because the driver is going to be removed from
2721 * memory.
2722 **/
2723static void i40evf_remove(struct pci_dev *pdev)
2724{
2725 struct net_device *netdev = pci_get_drvdata(pdev);
2726 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002727 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002728 struct i40e_hw *hw = &adapter->hw;
2729
2730 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002731 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002732
2733 if (adapter->netdev_registered) {
2734 unregister_netdev(netdev);
2735 adapter->netdev_registered = false;
2736 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002737
Mitch Williamsf4a71882015-01-09 11:18:16 +00002738 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002739 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002740 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002741 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002742 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002743 /* If the FW isn't responding, kick it once, but only once. */
2744 if (!i40evf_asq_done(hw)) {
2745 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002746 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002747 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002748
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002749 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002750 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002751 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002752 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002753 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002754 }
2755
Mitch Williamse5d17c32014-06-04 04:22:38 +00002756 if (adapter->watchdog_timer.function)
2757 del_timer_sync(&adapter->watchdog_timer);
2758
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002759 flush_scheduled_work();
2760
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002761 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04002762
Greg Rose5eae00c2013-12-21 06:12:45 +00002763 if (hw->aq.asq.count)
2764 i40evf_shutdown_adminq(hw);
2765
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002766 /* destroy the locks only once, here */
2767 mutex_destroy(&hw->aq.arq_mutex);
2768 mutex_destroy(&hw->aq.asq_mutex);
2769
Greg Rose5eae00c2013-12-21 06:12:45 +00002770 iounmap(hw->hw_addr);
2771 pci_release_regions(pdev);
2772
Mitch Williamse284fc82015-03-27 00:12:09 -07002773 i40evf_free_all_tx_resources(adapter);
2774 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002775 i40evf_free_queues(adapter);
2776 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002777 /* If we got removed before an up/down sequence, we've got a filter
2778 * hanging out there that we need to get rid of.
2779 */
2780 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2781 list_del(&f->list);
2782 kfree(f);
2783 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002784 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2785 list_del(&f->list);
2786 kfree(f);
2787 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002788
2789 free_netdev(netdev);
2790
2791 pci_disable_pcie_error_reporting(pdev);
2792
2793 pci_disable_device(pdev);
2794}
2795
2796static struct pci_driver i40evf_driver = {
2797 .name = i40evf_driver_name,
2798 .id_table = i40evf_pci_tbl,
2799 .probe = i40evf_probe,
2800 .remove = i40evf_remove,
2801#ifdef CONFIG_PM
2802 .suspend = i40evf_suspend,
2803 .resume = i40evf_resume,
2804#endif
2805 .shutdown = i40evf_shutdown,
2806};
2807
2808/**
2809 * i40e_init_module - Driver Registration Routine
2810 *
2811 * i40e_init_module is the first routine called when the driver is
2812 * loaded. All it does is register with the PCI subsystem.
2813 **/
2814static int __init i40evf_init_module(void)
2815{
2816 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002817
Greg Rose5eae00c2013-12-21 06:12:45 +00002818 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002819 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002820
2821 pr_info("%s\n", i40evf_copyright);
2822
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002823 i40evf_wq = create_singlethread_workqueue(i40evf_driver_name);
2824 if (!i40evf_wq) {
2825 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2826 return -ENOMEM;
2827 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002828 ret = pci_register_driver(&i40evf_driver);
2829 return ret;
2830}
2831
2832module_init(i40evf_init_module);
2833
2834/**
2835 * i40e_exit_module - Driver Exit Cleanup Routine
2836 *
2837 * i40e_exit_module is called just before the driver is removed
2838 * from memory.
2839 **/
2840static void __exit i40evf_exit_module(void)
2841{
2842 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002843 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00002844}
2845
2846module_exit(i40evf_exit_module);
2847
2848/* i40evf_main.c */