blob: 24f88ecdcddc97b2563b96681a2cad2e3a367249 [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
Bimmy Pujari07061952016-05-16 10:26:45 -070040#define DRV_VERSION_MINOR 6
Bimmy Pujari93e6fa22016-07-27 12:02:41 -070041#define DRV_VERSION_BUILD 12
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},
Joshua Hay92871412016-06-20 09:10:37 -070060 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040061 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070062 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF_HV), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000063 /* required last entry */
64 {0, }
65};
66
67MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
68
69MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
70MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
71MODULE_LICENSE("GPL");
72MODULE_VERSION(DRV_VERSION);
73
Jesse Brandeburg2803b162015-12-22 14:25:08 -080074static struct workqueue_struct *i40evf_wq;
75
Greg Rose5eae00c2013-12-21 06:12:45 +000076/**
77 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
78 * @hw: pointer to the HW structure
79 * @mem: ptr to mem struct to fill out
80 * @size: size of memory requested
81 * @alignment: what to align the allocation to
82 **/
83i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
84 struct i40e_dma_mem *mem,
85 u64 size, u32 alignment)
86{
87 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
88
89 if (!mem)
90 return I40E_ERR_PARAM;
91
92 mem->size = ALIGN(size, alignment);
93 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
94 (dma_addr_t *)&mem->pa, GFP_KERNEL);
95 if (mem->va)
96 return 0;
97 else
98 return I40E_ERR_NO_MEMORY;
99}
100
101/**
102 * i40evf_free_dma_mem_d - OS specific memory free for shared code
103 * @hw: pointer to the HW structure
104 * @mem: ptr to mem struct to free
105 **/
106i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
107{
108 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
109
110 if (!mem || !mem->va)
111 return I40E_ERR_PARAM;
112 dma_free_coherent(&adapter->pdev->dev, mem->size,
113 mem->va, (dma_addr_t)mem->pa);
114 return 0;
115}
116
117/**
118 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to fill out
121 * @size: size of memory requested
122 **/
123i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
124 struct i40e_virt_mem *mem, u32 size)
125{
126 if (!mem)
127 return I40E_ERR_PARAM;
128
129 mem->size = size;
130 mem->va = kzalloc(size, GFP_KERNEL);
131
132 if (mem->va)
133 return 0;
134 else
135 return I40E_ERR_NO_MEMORY;
136}
137
138/**
139 * i40evf_free_virt_mem_d - OS specific memory free for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to free
142 **/
143i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
144 struct i40e_virt_mem *mem)
145{
146 if (!mem)
147 return I40E_ERR_PARAM;
148
149 /* it's ok to kfree a NULL pointer */
150 kfree(mem->va);
151
152 return 0;
153}
154
155/**
156 * i40evf_debug_d - OS dependent version of debug printing
157 * @hw: pointer to the HW structure
158 * @mask: debug level mask
159 * @fmt_str: printf-type format description
160 **/
161void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
162{
163 char buf[512];
164 va_list argptr;
165
166 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
167 return;
168
169 va_start(argptr, fmt_str);
170 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
171 va_end(argptr);
172
173 /* the debug string is already formatted with a newline */
174 pr_info("%s", buf);
175}
176
177/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800178 * i40evf_schedule_reset - Set the flags and schedule a reset event
179 * @adapter: board private structure
180 **/
181void i40evf_schedule_reset(struct i40evf_adapter *adapter)
182{
183 if (!(adapter->flags &
184 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
185 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
186 schedule_work(&adapter->reset_task);
187 }
188}
189
190/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000191 * i40evf_tx_timeout - Respond to a Tx Hang
192 * @netdev: network interface device structure
193 **/
194static void i40evf_tx_timeout(struct net_device *netdev)
195{
196 struct i40evf_adapter *adapter = netdev_priv(netdev);
197
198 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800199 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000200}
201
202/**
203 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
204 * @adapter: board private structure
205 **/
206static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
207{
208 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000209
Greg Rose5eae00c2013-12-21 06:12:45 +0000210 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
211
212 /* read flush */
213 rd32(hw, I40E_VFGEN_RSTAT);
214
215 synchronize_irq(adapter->msix_entries[0].vector);
216}
217
218/**
219 * i40evf_misc_irq_enable - Enable default interrupt generation settings
220 * @adapter: board private structure
221 **/
222static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
223{
224 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000225
Greg Rose5eae00c2013-12-21 06:12:45 +0000226 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
227 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400228 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000229
230 /* read flush */
231 rd32(hw, I40E_VFGEN_RSTAT);
232}
233
234/**
235 * i40evf_irq_disable - Mask off interrupt generation on the NIC
236 * @adapter: board private structure
237 **/
238static void i40evf_irq_disable(struct i40evf_adapter *adapter)
239{
240 int i;
241 struct i40e_hw *hw = &adapter->hw;
242
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800243 if (!adapter->msix_entries)
244 return;
245
Greg Rose5eae00c2013-12-21 06:12:45 +0000246 for (i = 1; i < adapter->num_msix_vectors; i++) {
247 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
248 synchronize_irq(adapter->msix_entries[i].vector);
249 }
250 /* read flush */
251 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000252}
253
254/**
255 * i40evf_irq_enable_queues - Enable interrupt for specified queues
256 * @adapter: board private structure
257 * @mask: bitmap of queues to enable
258 **/
259void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
260{
261 struct i40e_hw *hw = &adapter->hw;
262 int i;
263
264 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400265 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000266 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
267 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000268 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400269 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000270 }
271 }
272}
273
274/**
275 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
276 * @adapter: board private structure
277 * @mask: bitmap of vectors to trigger
278 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000279static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000280{
281 struct i40e_hw *hw = &adapter->hw;
282 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800283 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000284
Mitch Williams164ec1b2014-06-04 08:45:19 +0000285 if (mask & 1) {
286 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400287 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000288 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400289 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000290 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
291 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000292 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400293 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000294 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400295 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000296 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400297 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000298 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
299 }
300 }
301}
302
303/**
304 * i40evf_irq_enable - Enable default interrupt generation settings
305 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600306 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000307 **/
308void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
309{
310 struct i40e_hw *hw = &adapter->hw;
311
Mitch Williams164ec1b2014-06-04 08:45:19 +0000312 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000313 i40evf_irq_enable_queues(adapter, ~0);
314
315 if (flush)
316 rd32(hw, I40E_VFGEN_RSTAT);
317}
318
319/**
320 * i40evf_msix_aq - Interrupt handler for vector 0
321 * @irq: interrupt number
322 * @data: pointer to netdev
323 **/
324static irqreturn_t i40evf_msix_aq(int irq, void *data)
325{
326 struct net_device *netdev = data;
327 struct i40evf_adapter *adapter = netdev_priv(netdev);
328 struct i40e_hw *hw = &adapter->hw;
329 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000330
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700331 /* handle non-queue interrupts, these reads clear the registers */
332 val = rd32(hw, I40E_VFINT_ICR01);
333 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000334
Jean Sacrened17f7e2015-10-13 01:06:30 -0600335 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
336 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000337 wr32(hw, I40E_VFINT_DYN_CTL01, val);
338
Greg Rose5eae00c2013-12-21 06:12:45 +0000339 /* schedule work on the private workqueue */
340 schedule_work(&adapter->adminq_task);
341
342 return IRQ_HANDLED;
343}
344
345/**
346 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
347 * @irq: interrupt number
348 * @data: pointer to a q_vector
349 **/
350static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
351{
352 struct i40e_q_vector *q_vector = data;
353
354 if (!q_vector->tx.ring && !q_vector->rx.ring)
355 return IRQ_HANDLED;
356
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700357 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000358
359 return IRQ_HANDLED;
360}
361
362/**
363 * i40evf_map_vector_to_rxq - associate irqs with rx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @r_idx: queue number
367 **/
368static void
369i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
370{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400371 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400372 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000373
374 rx_ring->q_vector = q_vector;
375 rx_ring->next = q_vector->rx.ring;
376 rx_ring->vsi = &adapter->vsi;
377 q_vector->rx.ring = rx_ring;
378 q_vector->rx.count++;
379 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400380 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000381}
382
383/**
384 * i40evf_map_vector_to_txq - associate irqs with tx queues
385 * @adapter: board private structure
386 * @v_idx: interrupt number
387 * @t_idx: queue number
388 **/
389static void
390i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
391{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400392 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400393 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000394
395 tx_ring->q_vector = q_vector;
396 tx_ring->next = q_vector->tx.ring;
397 tx_ring->vsi = &adapter->vsi;
398 q_vector->tx.ring = tx_ring;
399 q_vector->tx.count++;
400 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400401 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000402 q_vector->num_ringpairs++;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400403 q_vector->ring_mask |= BIT(t_idx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000404}
405
406/**
407 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
408 * @adapter: board private structure to initialize
409 *
410 * This function maps descriptor rings to the queue-specific vectors
411 * we were allotted through the MSI-X enabling code. Ideally, we'd have
412 * one vector per ring/queue, but on a constrained vector budget, we
413 * group the rings as "efficiently" as possible. You would add new
414 * mapping configurations in here.
415 **/
416static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
417{
418 int q_vectors;
419 int v_start = 0;
420 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000421 int rxr_remaining = adapter->num_active_queues;
422 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000423 int i, j;
424 int rqpv, tqpv;
425 int err = 0;
426
427 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
428
429 /* The ideal configuration...
430 * We have enough vectors to map one per queue.
431 */
Mitch Williams973371d2015-04-27 14:57:09 -0400432 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000433 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
434 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
435
436 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
437 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
438 goto out;
439 }
440
441 /* If we don't have enough vectors for a 1-to-1
442 * mapping, we'll have to group them so there are
443 * multiple queues per vector.
444 * Re-adjusting *qpv takes care of the remainder.
445 */
446 for (i = v_start; i < q_vectors; i++) {
447 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
448 for (j = 0; j < rqpv; j++) {
449 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
450 rxr_idx++;
451 rxr_remaining--;
452 }
453 }
454 for (i = v_start; i < q_vectors; i++) {
455 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
456 for (j = 0; j < tqpv; j++) {
457 i40evf_map_vector_to_txq(adapter, i, txr_idx);
458 txr_idx++;
459 txr_remaining--;
460 }
461 }
462
463out:
464 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
465
466 return err;
467}
468
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700469#ifdef CONFIG_NET_POLL_CONTROLLER
470/**
471 * i40evf_netpoll - A Polling 'interrupt' handler
472 * @netdev: network interface device structure
473 *
474 * This is used by netconsole to send skbs without having to re-enable
475 * interrupts. It's not called while the normal interrupt routine is executing.
476 **/
477static void i40evf_netpoll(struct net_device *netdev)
478{
479 struct i40evf_adapter *adapter = netdev_priv(netdev);
480 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
481 int i;
482
483 /* if interface is down do nothing */
484 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
485 return;
486
487 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400488 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700489}
490
491#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000492/**
493 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
494 * @adapter: board private structure
495 *
496 * Allocates MSI-X vectors for tx and rx handling, and requests
497 * interrupts from the kernel.
498 **/
499static int
500i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
501{
502 int vector, err, q_vectors;
503 int rx_int_idx = 0, tx_int_idx = 0;
504
505 i40evf_irq_disable(adapter);
506 /* Decrement for Other and TCP Timer vectors */
507 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
508
509 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400510 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Greg Rose5eae00c2013-12-21 06:12:45 +0000511
512 if (q_vector->tx.ring && q_vector->rx.ring) {
513 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
514 "i40evf-%s-%s-%d", basename,
515 "TxRx", rx_int_idx++);
516 tx_int_idx++;
517 } else if (q_vector->rx.ring) {
518 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
519 "i40evf-%s-%s-%d", basename,
520 "rx", rx_int_idx++);
521 } else if (q_vector->tx.ring) {
522 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
523 "i40evf-%s-%s-%d", basename,
524 "tx", tx_int_idx++);
525 } else {
526 /* skip this unused q_vector */
527 continue;
528 }
529 err = request_irq(
530 adapter->msix_entries[vector + NONQ_VECS].vector,
531 i40evf_msix_clean_rings,
532 0,
533 q_vector->name,
534 q_vector);
535 if (err) {
536 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400537 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000538 goto free_queue_irqs;
539 }
540 /* assign the mask for this irq */
541 irq_set_affinity_hint(
542 adapter->msix_entries[vector + NONQ_VECS].vector,
543 q_vector->affinity_mask);
544 }
545
546 return 0;
547
548free_queue_irqs:
549 while (vector) {
550 vector--;
551 irq_set_affinity_hint(
552 adapter->msix_entries[vector + NONQ_VECS].vector,
553 NULL);
554 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400555 &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000556 }
557 return err;
558}
559
560/**
561 * i40evf_request_misc_irq - Initialize MSI-X interrupts
562 * @adapter: board private structure
563 *
564 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
565 * vector is only for the admin queue, and stays active even when the netdev
566 * is closed.
567 **/
568static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
569{
570 struct net_device *netdev = adapter->netdev;
571 int err;
572
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700573 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000574 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
575 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000576 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800577 &i40evf_msix_aq, 0,
578 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000579 if (err) {
580 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800581 "request_irq for %s failed: %d\n",
582 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000583 free_irq(adapter->msix_entries[0].vector, netdev);
584 }
585 return err;
586}
587
588/**
589 * i40evf_free_traffic_irqs - Free MSI-X interrupts
590 * @adapter: board private structure
591 *
592 * Frees all MSI-X vectors other than 0.
593 **/
594static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
595{
596 int i;
597 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000598
Greg Rose5eae00c2013-12-21 06:12:45 +0000599 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
600
601 for (i = 0; i < q_vectors; i++) {
602 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
603 NULL);
604 free_irq(adapter->msix_entries[i+1].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400605 &adapter->q_vectors[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000606 }
607}
608
609/**
610 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
611 * @adapter: board private structure
612 *
613 * Frees MSI-X vector 0.
614 **/
615static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
616{
617 struct net_device *netdev = adapter->netdev;
618
619 free_irq(adapter->msix_entries[0].vector, netdev);
620}
621
622/**
623 * i40evf_configure_tx - Configure Transmit Unit after Reset
624 * @adapter: board private structure
625 *
626 * Configure the Tx unit of the MAC after a reset.
627 **/
628static void i40evf_configure_tx(struct i40evf_adapter *adapter)
629{
630 struct i40e_hw *hw = &adapter->hw;
631 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000632
Mitch Williamscc052922014-10-25 03:24:34 +0000633 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400634 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000635}
636
637/**
638 * i40evf_configure_rx - Configure Receive Unit after Reset
639 * @adapter: board private structure
640 *
641 * Configure the Rx unit of the MAC after a reset.
642 **/
643static void i40evf_configure_rx(struct i40evf_adapter *adapter)
644{
645 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000646 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000647
Mitch Williamscc052922014-10-25 03:24:34 +0000648 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400649 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Jesse Brandeburg19b85e62016-04-18 11:33:45 -0700650 adapter->rx_rings[i].rx_buf_len = I40EVF_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000651 }
652}
653
654/**
655 * i40evf_find_vlan - Search filter list for specific vlan filter
656 * @adapter: board private structure
657 * @vlan: vlan tag
658 *
659 * Returns ptr to the filter object or NULL
660 **/
661static struct
662i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
663{
664 struct i40evf_vlan_filter *f;
665
666 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
667 if (vlan == f->vlan)
668 return f;
669 }
670 return NULL;
671}
672
673/**
674 * i40evf_add_vlan - Add a vlan filter to the list
675 * @adapter: board private structure
676 * @vlan: VLAN tag
677 *
678 * Returns ptr to the filter object or NULL when no memory available.
679 **/
680static struct
681i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
682{
Mitch Williams13acb542015-03-31 00:45:05 -0700683 struct i40evf_vlan_filter *f = NULL;
684 int count = 50;
685
686 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
687 &adapter->crit_section)) {
688 udelay(1);
689 if (--count == 0)
690 goto out;
691 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000692
693 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000694 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000695 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000696 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700697 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000698
Greg Rose5eae00c2013-12-21 06:12:45 +0000699 f->vlan = vlan;
700
701 INIT_LIST_HEAD(&f->list);
702 list_add(&f->list, &adapter->vlan_filter_list);
703 f->add = true;
704 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
705 }
706
Mitch Williams13acb542015-03-31 00:45:05 -0700707clearout:
708 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
709out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000710 return f;
711}
712
713/**
714 * i40evf_del_vlan - Remove a vlan filter from the list
715 * @adapter: board private structure
716 * @vlan: VLAN tag
717 **/
718static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
719{
720 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700721 int count = 50;
722
723 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
724 &adapter->crit_section)) {
725 udelay(1);
726 if (--count == 0)
727 return;
728 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000729
730 f = i40evf_find_vlan(adapter, vlan);
731 if (f) {
732 f->remove = true;
733 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
734 }
Mitch Williams13acb542015-03-31 00:45:05 -0700735 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000736}
737
738/**
739 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
740 * @netdev: network device struct
741 * @vid: VLAN tag
742 **/
743static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000744 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000745{
746 struct i40evf_adapter *adapter = netdev_priv(netdev);
747
Mitch Williams8ed995f2015-08-28 17:55:57 -0400748 if (!VLAN_ALLOWED(adapter))
749 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000750 if (i40evf_add_vlan(adapter, vid) == NULL)
751 return -ENOMEM;
752 return 0;
753}
754
755/**
756 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
757 * @netdev: network device struct
758 * @vid: VLAN tag
759 **/
760static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000761 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000762{
763 struct i40evf_adapter *adapter = netdev_priv(netdev);
764
Mitch Williams8ed995f2015-08-28 17:55:57 -0400765 if (VLAN_ALLOWED(adapter)) {
766 i40evf_del_vlan(adapter, vid);
767 return 0;
768 }
769 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000770}
771
772/**
773 * i40evf_find_filter - Search filter list for specific mac filter
774 * @adapter: board private structure
775 * @macaddr: the MAC address
776 *
777 * Returns ptr to the filter object or NULL
778 **/
779static struct
780i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
781 u8 *macaddr)
782{
783 struct i40evf_mac_filter *f;
784
785 if (!macaddr)
786 return NULL;
787
788 list_for_each_entry(f, &adapter->mac_filter_list, list) {
789 if (ether_addr_equal(macaddr, f->macaddr))
790 return f;
791 }
792 return NULL;
793}
794
795/**
796 * i40e_add_filter - Add a mac filter to the filter list
797 * @adapter: board private structure
798 * @macaddr: the MAC address
799 *
800 * Returns ptr to the filter object or NULL when no memory available.
801 **/
802static struct
803i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
804 u8 *macaddr)
805{
806 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000807 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000808
809 if (!macaddr)
810 return NULL;
811
812 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000813 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000814 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000815 if (--count == 0)
816 return NULL;
817 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000818
819 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000820 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000821 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000822 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000823 clear_bit(__I40EVF_IN_CRITICAL_TASK,
824 &adapter->crit_section);
825 return NULL;
826 }
827
Greg Rose9a173902014-05-22 06:32:02 +0000828 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000829
Mitch Williams63590b62016-05-16 10:26:42 -0700830 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000831 f->add = true;
832 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
833 }
834
835 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
836 return f;
837}
838
839/**
840 * i40evf_set_mac - NDO callback to set port mac address
841 * @netdev: network interface device structure
842 * @p: pointer to an address structure
843 *
844 * Returns 0 on success, negative on failure
845 **/
846static int i40evf_set_mac(struct net_device *netdev, void *p)
847{
848 struct i40evf_adapter *adapter = netdev_priv(netdev);
849 struct i40e_hw *hw = &adapter->hw;
850 struct i40evf_mac_filter *f;
851 struct sockaddr *addr = p;
852
853 if (!is_valid_ether_addr(addr->sa_data))
854 return -EADDRNOTAVAIL;
855
856 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
857 return 0;
858
Mitch Williams14e52ee2015-08-31 19:54:44 -0400859 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
860 return -EPERM;
861
862 f = i40evf_find_filter(adapter, hw->mac.addr);
863 if (f) {
864 f->remove = true;
865 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
866 }
867
Greg Rose5eae00c2013-12-21 06:12:45 +0000868 f = i40evf_add_filter(adapter, addr->sa_data);
869 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000870 ether_addr_copy(hw->mac.addr, addr->sa_data);
871 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000872 }
873
874 return (f == NULL) ? -ENOMEM : 0;
875}
876
877/**
878 * i40evf_set_rx_mode - NDO callback to set the netdev filters
879 * @netdev: network interface device structure
880 **/
881static void i40evf_set_rx_mode(struct net_device *netdev)
882{
883 struct i40evf_adapter *adapter = netdev_priv(netdev);
884 struct i40evf_mac_filter *f, *ftmp;
885 struct netdev_hw_addr *uca;
886 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400887 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000888 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000889
890 /* add addr if not already in the filter list */
891 netdev_for_each_uc_addr(uca, netdev) {
892 i40evf_add_filter(adapter, uca->addr);
893 }
894 netdev_for_each_mc_addr(mca, netdev) {
895 i40evf_add_filter(adapter, mca->addr);
896 }
897
898 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000899 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000900 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000901 if (--count == 0) {
902 dev_err(&adapter->pdev->dev,
903 "Failed to get lock in %s\n", __func__);
904 return;
905 }
906 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000907 /* remove filter if not in netdev list */
908 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400909 netdev_for_each_mc_addr(mca, netdev)
910 if (ether_addr_equal(mca->addr, f->macaddr))
911 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000912
Shannon Nelson2f41f332015-08-26 15:14:20 -0400913 netdev_for_each_uc_addr(uca, netdev)
914 if (ether_addr_equal(uca->addr, f->macaddr))
915 goto bottom_of_search_loop;
916
917 for_each_dev_addr(netdev, ha)
918 if (ether_addr_equal(ha->addr, f->macaddr))
919 goto bottom_of_search_loop;
920
921 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
922 goto bottom_of_search_loop;
923
924 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
925 f->remove = true;
926 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
927
928bottom_of_search_loop:
929 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000930 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700931
932 if (netdev->flags & IFF_PROMISC &&
933 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
934 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
935 else if (!(netdev->flags & IFF_PROMISC) &&
936 adapter->flags & I40EVF_FLAG_PROMISC_ON)
937 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
938
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700939 if (netdev->flags & IFF_ALLMULTI &&
940 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
941 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
942 else if (!(netdev->flags & IFF_ALLMULTI) &&
943 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
944 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
945
Greg Rose5eae00c2013-12-21 06:12:45 +0000946 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
947}
948
949/**
950 * i40evf_napi_enable_all - enable NAPI on all queue vectors
951 * @adapter: board private structure
952 **/
953static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
954{
955 int q_idx;
956 struct i40e_q_vector *q_vector;
957 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
958
959 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
960 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000961
Mitch Williams7d96ba12015-10-26 19:44:39 -0400962 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000963 napi = &q_vector->napi;
964 napi_enable(napi);
965 }
966}
967
968/**
969 * i40evf_napi_disable_all - disable NAPI on all queue vectors
970 * @adapter: board private structure
971 **/
972static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
973{
974 int q_idx;
975 struct i40e_q_vector *q_vector;
976 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
977
978 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400979 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000980 napi_disable(&q_vector->napi);
981 }
982}
983
984/**
985 * i40evf_configure - set up transmit and receive data structures
986 * @adapter: board private structure
987 **/
988static void i40evf_configure(struct i40evf_adapter *adapter)
989{
990 struct net_device *netdev = adapter->netdev;
991 int i;
992
993 i40evf_set_rx_mode(netdev);
994
995 i40evf_configure_tx(adapter);
996 i40evf_configure_rx(adapter);
997 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
998
Mitch Williamscc052922014-10-25 03:24:34 +0000999 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001000 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001001
Mitch Williamsb1630982016-04-18 11:33:48 -07001002 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001003 }
1004}
1005
1006/**
1007 * i40evf_up_complete - Finish the last steps of bringing up a connection
1008 * @adapter: board private structure
1009 **/
1010static int i40evf_up_complete(struct i40evf_adapter *adapter)
1011{
1012 adapter->state = __I40EVF_RUNNING;
1013 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1014
1015 i40evf_napi_enable_all(adapter);
1016
1017 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1018 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1019 return 0;
1020}
1021
1022/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001023 * i40e_down - Shutdown the connection processing
1024 * @adapter: board private structure
1025 **/
1026void i40evf_down(struct i40evf_adapter *adapter)
1027{
1028 struct net_device *netdev = adapter->netdev;
1029 struct i40evf_mac_filter *f;
1030
Mitch Williams209dc4d2015-12-09 15:50:27 -08001031 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001032 return;
1033
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001034 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1035 &adapter->crit_section))
1036 usleep_range(500, 1000);
1037
Mitch Williams63e18c22015-03-27 00:12:10 -07001038 netif_carrier_off(netdev);
1039 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +00001040 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001041 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001042
Mitch Williamsef8693e2014-02-13 03:48:53 -08001043 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001044 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1045 f->remove = true;
1046 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001047 /* remove all VLAN filters */
1048 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1049 f->remove = true;
1050 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001051 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1052 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001053 /* cancel any current operation */
1054 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001055 /* Schedule operations to close down the HW. Don't wait
1056 * here for this to complete. The watchdog is still running
1057 * and it will take care of this.
1058 */
1059 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001060 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001061 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001062 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001063
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001064 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001065}
1066
1067/**
1068 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1069 * @adapter: board private structure
1070 * @vectors: number of vectors to request
1071 *
1072 * Work with the OS to set up the MSIX vectors needed.
1073 *
1074 * Returns 0 on success, negative on failure
1075 **/
1076static int
1077i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1078{
1079 int err, vector_threshold;
1080
1081 /* We'll want at least 3 (vector_threshold):
1082 * 0) Other (Admin Queue and link, mostly)
1083 * 1) TxQ[0] Cleanup
1084 * 2) RxQ[0] Cleanup
1085 */
1086 vector_threshold = MIN_MSIX_COUNT;
1087
1088 /* The more we get, the more we will assign to Tx/Rx Cleanup
1089 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1090 * Right now, we simply care about how many we'll get; we'll
1091 * set them up later while requesting irq's.
1092 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001093 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1094 vector_threshold, vectors);
1095 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001096 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001097 kfree(adapter->msix_entries);
1098 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001099 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001100 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001101
1102 /* Adjust for only the vectors we'll use, which is minimum
1103 * of max_msix_q_vectors + NONQ_VECS, or the number of
1104 * vectors we were allocated.
1105 */
1106 adapter->num_msix_vectors = err;
1107 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001108}
1109
1110/**
1111 * i40evf_free_queues - Free memory for all rings
1112 * @adapter: board private structure to initialize
1113 *
1114 * Free all of the memory associated with queue pairs.
1115 **/
1116static void i40evf_free_queues(struct i40evf_adapter *adapter)
1117{
Greg Rose5eae00c2013-12-21 06:12:45 +00001118 if (!adapter->vsi_res)
1119 return;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001120 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001121 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001122 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001123 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001124}
1125
1126/**
1127 * i40evf_alloc_queues - Allocate memory for all rings
1128 * @adapter: board private structure to initialize
1129 *
1130 * We allocate one ring per queue at run-time since we don't know the
1131 * number of queues at compile-time. The polling_netdev array is
1132 * intended for Multiqueue, but should work fine with a single queue.
1133 **/
1134static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1135{
1136 int i;
1137
Mitch Williams0dd438d2015-10-26 19:44:40 -04001138 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1139 sizeof(struct i40e_ring), GFP_KERNEL);
1140 if (!adapter->tx_rings)
1141 goto err_out;
1142 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1143 sizeof(struct i40e_ring), GFP_KERNEL);
1144 if (!adapter->rx_rings)
1145 goto err_out;
1146
Mitch Williamscc052922014-10-25 03:24:34 +00001147 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001148 struct i40e_ring *tx_ring;
1149 struct i40e_ring *rx_ring;
1150
Mitch Williams0dd438d2015-10-26 19:44:40 -04001151 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001152
1153 tx_ring->queue_index = i;
1154 tx_ring->netdev = adapter->netdev;
1155 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001156 tx_ring->count = adapter->tx_desc_count;
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001157 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1158 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001159
Mitch Williams0dd438d2015-10-26 19:44:40 -04001160 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001161 rx_ring->queue_index = i;
1162 rx_ring->netdev = adapter->netdev;
1163 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001164 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001165 }
1166
1167 return 0;
1168
1169err_out:
1170 i40evf_free_queues(adapter);
1171 return -ENOMEM;
1172}
1173
1174/**
1175 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1176 * @adapter: board private structure to initialize
1177 *
1178 * Attempt to configure the interrupts using the best available
1179 * capabilities of the hardware and the kernel.
1180 **/
1181static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1182{
1183 int vector, v_budget;
1184 int pairs = 0;
1185 int err = 0;
1186
1187 if (!adapter->vsi_res) {
1188 err = -EIO;
1189 goto out;
1190 }
Mitch Williamscc052922014-10-25 03:24:34 +00001191 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001192
1193 /* It's easy to be greedy for MSI-X vectors, but it really
1194 * doesn't do us much good if we have a lot more vectors
1195 * than CPU's. So let's be conservative and only ask for
1196 * (roughly) twice the number of vectors as there are CPU's.
1197 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001198 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1199 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001200
Greg Rose5eae00c2013-12-21 06:12:45 +00001201 adapter->msix_entries = kcalloc(v_budget,
1202 sizeof(struct msix_entry), GFP_KERNEL);
1203 if (!adapter->msix_entries) {
1204 err = -ENOMEM;
1205 goto out;
1206 }
1207
1208 for (vector = 0; vector < v_budget; vector++)
1209 adapter->msix_entries[vector].entry = vector;
1210
Mitch Williams313ed2d2015-08-27 11:42:31 -04001211 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001212
1213out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001214 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1215 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001216 return err;
1217}
1218
1219/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001220 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1221 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001222 *
1223 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001224 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001225static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001226{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001227 struct i40e_aqc_get_set_rss_key_data *rss_key =
1228 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001229 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001230 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001231
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001232 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1233 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001234 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001235 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001236 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001237 }
1238
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001239 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1240 if (ret) {
1241 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1242 i40evf_stat_str(hw, ret),
1243 i40evf_aq_str(hw, hw->aq.asq_last_status));
1244 return ret;
1245
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001246 }
1247
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001248 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1249 adapter->rss_lut, adapter->rss_lut_size);
1250 if (ret) {
1251 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1252 i40evf_stat_str(hw, ret),
1253 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001254 }
1255
1256 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001257
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001258}
1259
1260/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001261 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001262 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001263 *
1264 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001265 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001266static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001267{
1268 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001269 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001270 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001271
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001272 dw = (u32 *)adapter->rss_key;
1273 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1274 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001275
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001276 dw = (u32 *)adapter->rss_lut;
1277 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1278 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001279
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001280 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001281
1282 return 0;
1283}
1284
1285/**
1286 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001287 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001288 *
1289 * Returns 0 on success, negative on failure
1290 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001291int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001292{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001293
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001294 if (RSS_PF(adapter)) {
1295 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1296 I40EVF_FLAG_AQ_SET_RSS_KEY;
1297 return 0;
1298 } else if (RSS_AQ(adapter)) {
1299 return i40evf_config_rss_aq(adapter);
1300 } else {
1301 return i40evf_config_rss_reg(adapter);
1302 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001303}
1304
1305/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001306 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001307 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001308 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001309static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001310{
1311 u16 i;
1312
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001313 for (i = 0; i < adapter->rss_lut_size; i++)
1314 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001315}
1316
1317/**
Helin Zhang96a81982015-10-26 19:44:31 -04001318 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001320 *
1321 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001322 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001323static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001324{
1325 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001326 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001327
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001328 if (!RSS_PF(adapter)) {
1329 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1330 if (adapter->vf_res->vf_offload_flags &
1331 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1332 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1333 else
1334 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001335
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001336 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1337 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1338 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001339
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001340 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001341
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001342 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1343 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001344
1345 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001346}
1347
1348/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001349 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1350 * @adapter: board private structure to initialize
1351 *
1352 * We allocate one q_vector per queue interrupt. If allocation fails we
1353 * return -ENOMEM.
1354 **/
1355static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1356{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001357 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001358 struct i40e_q_vector *q_vector;
1359
1360 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001361 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001362 GFP_KERNEL);
1363 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001364 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001365
1366 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001367 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001368 q_vector->adapter = adapter;
1369 q_vector->vsi = &adapter->vsi;
1370 q_vector->v_idx = q_idx;
1371 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001372 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001373 }
1374
1375 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001376}
1377
1378/**
1379 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1380 * @adapter: board private structure to initialize
1381 *
1382 * This function frees the memory allocated to the q_vectors. In addition if
1383 * NAPI is enabled it will delete any references to the NAPI struct prior
1384 * to freeing the q_vector.
1385 **/
1386static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1387{
1388 int q_idx, num_q_vectors;
1389 int napi_vectors;
1390
1391 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001392 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001393
1394 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001395 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001396 if (q_idx < napi_vectors)
1397 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001398 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001399 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001400}
1401
1402/**
1403 * i40evf_reset_interrupt_capability - Reset MSIX setup
1404 * @adapter: board private structure
1405 *
1406 **/
1407void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1408{
1409 pci_disable_msix(adapter->pdev);
1410 kfree(adapter->msix_entries);
1411 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001412}
1413
1414/**
1415 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1416 * @adapter: board private structure to initialize
1417 *
1418 **/
1419int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1420{
1421 int err;
1422
Jacob Keller62fe2a82016-07-27 12:02:33 -07001423 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001424 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001425 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001426 if (err) {
1427 dev_err(&adapter->pdev->dev,
1428 "Unable to setup interrupt capabilities\n");
1429 goto err_set_interrupt;
1430 }
1431
1432 err = i40evf_alloc_q_vectors(adapter);
1433 if (err) {
1434 dev_err(&adapter->pdev->dev,
1435 "Unable to allocate memory for queue vectors\n");
1436 goto err_alloc_q_vectors;
1437 }
1438
1439 err = i40evf_alloc_queues(adapter);
1440 if (err) {
1441 dev_err(&adapter->pdev->dev,
1442 "Unable to allocate memory for queues\n");
1443 goto err_alloc_queues;
1444 }
1445
1446 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001447 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1448 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001449
1450 return 0;
1451err_alloc_queues:
1452 i40evf_free_q_vectors(adapter);
1453err_alloc_q_vectors:
1454 i40evf_reset_interrupt_capability(adapter);
1455err_set_interrupt:
1456 return err;
1457}
1458
1459/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001460 * i40evf_free_rss - Free memory used by RSS structs
1461 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001462 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001463static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001464{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001465 kfree(adapter->rss_key);
1466 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001467
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001468 kfree(adapter->rss_lut);
1469 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001470}
1471
1472/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001473 * i40evf_watchdog_timer - Periodic call-back timer
1474 * @data: pointer to adapter disguised as unsigned long
1475 **/
1476static void i40evf_watchdog_timer(unsigned long data)
1477{
1478 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001479
Greg Rose5eae00c2013-12-21 06:12:45 +00001480 schedule_work(&adapter->watchdog_task);
1481 /* timer will be rescheduled in watchdog task */
1482}
1483
1484/**
1485 * i40evf_watchdog_task - Periodic call-back task
1486 * @work: pointer to work_struct
1487 **/
1488static void i40evf_watchdog_task(struct work_struct *work)
1489{
1490 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001491 struct i40evf_adapter,
1492 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001493 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001494 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001495
Greg Rose5eae00c2013-12-21 06:12:45 +00001496 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001497 goto restart_watchdog;
1498
1499 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001500 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1501 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1502 if ((reg_val == I40E_VFR_VFACTIVE) ||
1503 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001504 /* A chance for redemption! */
1505 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1506 adapter->state = __I40EVF_STARTUP;
1507 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1508 schedule_delayed_work(&adapter->init_task, 10);
1509 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1510 &adapter->crit_section);
1511 /* Don't reschedule the watchdog, since we've restarted
1512 * the init task. When init_task contacts the PF and
1513 * gets everything set up again, it'll restart the
1514 * watchdog for us. Down, boy. Sit. Stay. Woof.
1515 */
1516 return;
1517 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001518 adapter->aq_required = 0;
1519 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1520 goto watchdog_done;
1521 }
1522
1523 if ((adapter->state < __I40EVF_DOWN) ||
1524 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001525 goto watchdog_done;
1526
Mitch Williamsef8693e2014-02-13 03:48:53 -08001527 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001528 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1529 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001530 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001531 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001532 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001533 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001534 adapter->aq_required = 0;
1535 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001536 goto watchdog_done;
1537 }
1538
1539 /* Process admin queue tasks. After init, everything gets done
1540 * here so we don't race on the admin queue.
1541 */
Mitch Williamsed636962015-04-07 19:45:32 -04001542 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001543 if (!i40evf_asq_done(hw)) {
1544 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1545 i40evf_send_api_ver(adapter);
1546 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001547 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001548 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001549 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1550 i40evf_send_vf_config_msg(adapter);
1551 goto watchdog_done;
1552 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001553
Mitch Williamse284fc82015-03-27 00:12:09 -07001554 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1555 i40evf_disable_queues(adapter);
1556 goto watchdog_done;
1557 }
1558
Greg Rose5eae00c2013-12-21 06:12:45 +00001559 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1560 i40evf_map_queues(adapter);
1561 goto watchdog_done;
1562 }
1563
1564 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1565 i40evf_add_ether_addrs(adapter);
1566 goto watchdog_done;
1567 }
1568
1569 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1570 i40evf_add_vlans(adapter);
1571 goto watchdog_done;
1572 }
1573
1574 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1575 i40evf_del_ether_addrs(adapter);
1576 goto watchdog_done;
1577 }
1578
1579 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1580 i40evf_del_vlans(adapter);
1581 goto watchdog_done;
1582 }
1583
Greg Rose5eae00c2013-12-21 06:12:45 +00001584 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1585 i40evf_configure_queues(adapter);
1586 goto watchdog_done;
1587 }
1588
1589 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1590 i40evf_enable_queues(adapter);
1591 goto watchdog_done;
1592 }
1593
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001594 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1595 /* This message goes straight to the firmware, not the
1596 * PF, so we don't have to set current_op as we will
1597 * not get a response through the ARQ.
1598 */
Helin Zhang96a81982015-10-26 19:44:31 -04001599 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001600 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1601 goto watchdog_done;
1602 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001603 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1604 i40evf_get_hena(adapter);
1605 goto watchdog_done;
1606 }
1607 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1608 i40evf_set_hena(adapter);
1609 goto watchdog_done;
1610 }
1611 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1612 i40evf_set_rss_key(adapter);
1613 goto watchdog_done;
1614 }
1615 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1616 i40evf_set_rss_lut(adapter);
1617 goto watchdog_done;
1618 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001619
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001620 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1621 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_UNICAST_PROMISC |
1622 I40E_FLAG_VF_MULTICAST_PROMISC);
1623 goto watchdog_done;
1624 }
1625
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001626 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1627 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_MULTICAST_PROMISC);
1628 goto watchdog_done;
1629 }
1630
1631 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1632 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001633 i40evf_set_promiscuous(adapter, 0);
1634 goto watchdog_done;
1635 }
1636
Greg Rose5eae00c2013-12-21 06:12:45 +00001637 if (adapter->state == __I40EVF_RUNNING)
1638 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001639watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001640 if (adapter->state == __I40EVF_RUNNING) {
1641 i40evf_irq_enable_queues(adapter, ~0);
1642 i40evf_fire_sw_int(adapter, 0xFF);
1643 } else {
1644 i40evf_fire_sw_int(adapter, 0x1);
1645 }
1646
Mitch Williamsef8693e2014-02-13 03:48:53 -08001647 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1648restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001649 if (adapter->state == __I40EVF_REMOVE)
1650 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001651 if (adapter->aq_required)
1652 mod_timer(&adapter->watchdog_timer,
1653 jiffies + msecs_to_jiffies(20));
1654 else
1655 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001656 schedule_work(&adapter->adminq_task);
1657}
1658
Mitch Williams67c818a2015-06-19 08:56:30 -07001659#define I40EVF_RESET_WAIT_MS 10
1660#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001661/**
1662 * i40evf_reset_task - Call-back task to handle hardware reset
1663 * @work: pointer to work_struct
1664 *
1665 * During reset we need to shut down and reinitialize the admin queue
1666 * before we can use it to communicate with the PF again. We also clear
1667 * and reinit the rings because that context is lost as well.
1668 **/
1669static void i40evf_reset_task(struct work_struct *work)
1670{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001671 struct i40evf_adapter *adapter = container_of(work,
1672 struct i40evf_adapter,
1673 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001674 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001675 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001676 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001677 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001678 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001679 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001680
1681 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1682 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001683 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001684
Mitch Williams67c818a2015-06-19 08:56:30 -07001685 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001686 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001687 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1688 /* Restart the AQ here. If we have been reset but didn't
1689 * detect it, or if the PF had to reinit, our AQ will be hosed.
1690 */
1691 i40evf_shutdown_adminq(hw);
1692 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001693 i40evf_request_reset(adapter);
1694 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001695 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001696
Mitch Williamsef8693e2014-02-13 03:48:53 -08001697 /* poll until we see the reset actually happen */
1698 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001699 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1700 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1701 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001702 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001703 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001704 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001705 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001706 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001707 goto continue_reset; /* act like the reset happened */
1708 }
1709
1710 /* wait until the reset is complete and the PF is responding to us */
1711 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001712 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1713 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1714 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001715 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001716 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001717 }
Mitch Williams509a4472015-12-23 12:05:52 -08001718 pci_set_master(adapter->pdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001719 /* extra wait to make sure minimum wait is met */
1720 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001721 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001722 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001723 struct i40evf_vlan_filter *fv, *fvtmp;
1724
Greg Rose5eae00c2013-12-21 06:12:45 +00001725 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001726 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001727 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001728 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1729
Mitch Williams169f4072014-04-01 07:11:50 +00001730 if (netif_running(adapter->netdev)) {
1731 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001732 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001733 netif_tx_disable(netdev);
1734 i40evf_napi_disable_all(adapter);
1735 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001736 i40evf_free_traffic_irqs(adapter);
1737 i40evf_free_all_tx_resources(adapter);
1738 i40evf_free_all_rx_resources(adapter);
1739 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001740
1741 /* Delete all of the filters, both MAC and VLAN. */
1742 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1743 list) {
1744 list_del(&f->list);
1745 kfree(f);
1746 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001747
Mitch Williams6ba36a22014-08-01 13:27:14 -07001748 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1749 list) {
1750 list_del(&fv->list);
1751 kfree(fv);
1752 }
1753
Mitch Williamsef8693e2014-02-13 03:48:53 -08001754 i40evf_free_misc_irq(adapter);
1755 i40evf_reset_interrupt_capability(adapter);
1756 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001757 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001758 kfree(adapter->vf_res);
1759 i40evf_shutdown_adminq(hw);
1760 adapter->netdev->flags &= ~IFF_UP;
1761 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001762 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Mitch Williams9b9344f2016-01-15 14:33:19 -08001763 adapter->state = __I40EVF_DOWN;
Mitch Williams67c818a2015-06-19 08:56:30 -07001764 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001765 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001766 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001767
1768continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001769 if (netif_running(adapter->netdev)) {
1770 netif_carrier_off(netdev);
1771 netif_tx_stop_all_queues(netdev);
1772 i40evf_napi_disable_all(adapter);
1773 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001774 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001775
Greg Rose5eae00c2013-12-21 06:12:45 +00001776 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001777 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1778
1779 /* free the Tx/Rx rings and descriptors, might be better to just
1780 * re-use them sometime in the future
1781 */
1782 i40evf_free_all_rx_resources(adapter);
1783 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001784
1785 /* kill and reinit the admin queue */
1786 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001787 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1788 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001789 err = i40evf_init_adminq(hw);
1790 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001791 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1792 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001793
Mitch Williamse6d038d2015-06-04 16:23:58 -04001794 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1795 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001796
1797 /* re-add all MAC filters */
1798 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1799 f->add = true;
1800 }
1801 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001802 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1803 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001804 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001805 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001806 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001807 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001808 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001809
1810 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1811
1812 if (netif_running(adapter->netdev)) {
1813 /* allocate transmit descriptors */
1814 err = i40evf_setup_all_tx_resources(adapter);
1815 if (err)
1816 goto reset_err;
1817
1818 /* allocate receive descriptors */
1819 err = i40evf_setup_all_rx_resources(adapter);
1820 if (err)
1821 goto reset_err;
1822
1823 i40evf_configure(adapter);
1824
1825 err = i40evf_up_complete(adapter);
1826 if (err)
1827 goto reset_err;
1828
1829 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001830 } else {
1831 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001832 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001833
Greg Rose5eae00c2013-12-21 06:12:45 +00001834 return;
1835reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001836 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001837 i40evf_close(adapter->netdev);
1838}
1839
1840/**
1841 * i40evf_adminq_task - worker thread to clean the admin queue
1842 * @work: pointer to work_struct containing our data
1843 **/
1844static void i40evf_adminq_task(struct work_struct *work)
1845{
1846 struct i40evf_adapter *adapter =
1847 container_of(work, struct i40evf_adapter, adminq_task);
1848 struct i40e_hw *hw = &adapter->hw;
1849 struct i40e_arq_event_info event;
1850 struct i40e_virtchnl_msg *v_msg;
1851 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001852 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001853 u16 pending;
1854
Mitch Williamsef8693e2014-02-13 03:48:53 -08001855 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001856 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001857
Mitch Williams1001dc32014-11-11 20:02:19 +00001858 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1859 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001860 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001861 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001862
Greg Rose5eae00c2013-12-21 06:12:45 +00001863 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1864 do {
1865 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001866 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001867 break; /* No event to process or error cleaning ARQ */
1868
1869 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1870 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001871 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001872 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001873 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001874 } while (pending);
1875
Mitch Williams67c818a2015-06-19 08:56:30 -07001876 if ((adapter->flags &
1877 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1878 adapter->state == __I40EVF_RESETTING)
1879 goto freedom;
1880
Mitch Williams912257e2014-05-22 06:32:07 +00001881 /* check for error indications */
1882 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001883 if (val == 0xdeadbeef) /* indicates device in reset */
1884 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001885 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001886 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001887 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001888 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001889 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001890 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001891 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001892 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001893 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001894 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001895 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001896 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001897 }
1898 if (oldval != val)
1899 wr32(hw, hw->aq.arq.len, val);
1900
1901 val = rd32(hw, hw->aq.asq.len);
1902 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001903 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001904 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001905 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001906 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001907 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001908 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001909 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001910 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001911 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001912 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001913 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001914 }
1915 if (oldval != val)
1916 wr32(hw, hw->aq.asq.len, val);
1917
Mitch Williams67c818a2015-06-19 08:56:30 -07001918freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001919 kfree(event.msg_buf);
1920out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001921 /* re-enable Admin queue interrupt cause */
1922 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001923}
1924
1925/**
1926 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1927 * @adapter: board private structure
1928 *
1929 * Free all transmit software resources
1930 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001931void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001932{
1933 int i;
1934
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08001935 if (!adapter->tx_rings)
1936 return;
1937
Mitch Williamscc052922014-10-25 03:24:34 +00001938 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04001939 if (adapter->tx_rings[i].desc)
1940 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001941}
1942
1943/**
1944 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1945 * @adapter: board private structure
1946 *
1947 * If this function returns with an error, then it's possible one or
1948 * more of the rings is populated (while the rest are not). It is the
1949 * callers duty to clean those orphaned rings.
1950 *
1951 * Return 0 on success, negative on failure
1952 **/
1953static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1954{
1955 int i, err = 0;
1956
Mitch Williamscc052922014-10-25 03:24:34 +00001957 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001958 adapter->tx_rings[i].count = adapter->tx_desc_count;
1959 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001960 if (!err)
1961 continue;
1962 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001963 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001964 break;
1965 }
1966
1967 return err;
1968}
1969
1970/**
1971 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1972 * @adapter: board private structure
1973 *
1974 * If this function returns with an error, then it's possible one or
1975 * more of the rings is populated (while the rest are not). It is the
1976 * callers duty to clean those orphaned rings.
1977 *
1978 * Return 0 on success, negative on failure
1979 **/
1980static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1981{
1982 int i, err = 0;
1983
Mitch Williamscc052922014-10-25 03:24:34 +00001984 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001985 adapter->rx_rings[i].count = adapter->rx_desc_count;
1986 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001987 if (!err)
1988 continue;
1989 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001990 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001991 break;
1992 }
1993 return err;
1994}
1995
1996/**
1997 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1998 * @adapter: board private structure
1999 *
2000 * Free all receive software resources
2001 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002002void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002003{
2004 int i;
2005
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002006 if (!adapter->rx_rings)
2007 return;
2008
Mitch Williamscc052922014-10-25 03:24:34 +00002009 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002010 if (adapter->rx_rings[i].desc)
2011 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002012}
2013
2014/**
2015 * i40evf_open - Called when a network interface is made active
2016 * @netdev: network interface device structure
2017 *
2018 * Returns 0 on success, negative value on failure
2019 *
2020 * The open entry point is called when a network interface is made
2021 * active by the system (IFF_UP). At this point all resources needed
2022 * for transmit and receive operations are allocated, the interrupt
2023 * handler is registered with the OS, the watchdog timer is started,
2024 * and the stack is notified that the interface is ready.
2025 **/
2026static int i40evf_open(struct net_device *netdev)
2027{
2028 struct i40evf_adapter *adapter = netdev_priv(netdev);
2029 int err;
2030
Mitch Williamsef8693e2014-02-13 03:48:53 -08002031 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2032 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2033 return -EIO;
2034 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002035
2036 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002037 return -EBUSY;
2038
2039 /* allocate transmit descriptors */
2040 err = i40evf_setup_all_tx_resources(adapter);
2041 if (err)
2042 goto err_setup_tx;
2043
2044 /* allocate receive descriptors */
2045 err = i40evf_setup_all_rx_resources(adapter);
2046 if (err)
2047 goto err_setup_rx;
2048
2049 /* clear any pending interrupts, may auto mask */
2050 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2051 if (err)
2052 goto err_req_irq;
2053
Mitch Williams44151cd2015-04-27 14:57:17 -04002054 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002055 i40evf_configure(adapter);
2056
2057 err = i40evf_up_complete(adapter);
2058 if (err)
2059 goto err_req_irq;
2060
2061 i40evf_irq_enable(adapter, true);
2062
2063 return 0;
2064
2065err_req_irq:
2066 i40evf_down(adapter);
2067 i40evf_free_traffic_irqs(adapter);
2068err_setup_rx:
2069 i40evf_free_all_rx_resources(adapter);
2070err_setup_tx:
2071 i40evf_free_all_tx_resources(adapter);
2072
2073 return err;
2074}
2075
2076/**
2077 * i40evf_close - Disables a network interface
2078 * @netdev: network interface device structure
2079 *
2080 * Returns 0, this is not allowed to fail
2081 *
2082 * The close entry point is called when an interface is de-activated
2083 * by the OS. The hardware is still under the drivers control, but
2084 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2085 * are freed, along with all transmit and receive resources.
2086 **/
2087static int i40evf_close(struct net_device *netdev)
2088{
2089 struct i40evf_adapter *adapter = netdev_priv(netdev);
2090
Mitch Williams209dc4d2015-12-09 15:50:27 -08002091 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002092 return 0;
2093
Mitch Williamsef8693e2014-02-13 03:48:53 -08002094
Greg Rose5eae00c2013-12-21 06:12:45 +00002095 set_bit(__I40E_DOWN, &adapter->vsi.state);
2096
2097 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002098 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002099 i40evf_free_traffic_irqs(adapter);
2100
Greg Rose5eae00c2013-12-21 06:12:45 +00002101 return 0;
2102}
2103
2104/**
2105 * i40evf_get_stats - Get System Network Statistics
2106 * @netdev: network interface device structure
2107 *
2108 * Returns the address of the device statistics structure.
2109 * The statistics are actually updated from the timer callback.
2110 **/
2111static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2112{
2113 struct i40evf_adapter *adapter = netdev_priv(netdev);
2114
2115 /* only return the current stats */
2116 return &adapter->net_stats;
2117}
2118
2119/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002120 * i40evf_change_mtu - Change the Maximum Transfer Unit
2121 * @netdev: network interface device structure
2122 * @new_mtu: new value for maximum frame size
2123 *
2124 * Returns 0 on success, negative on failure
2125 **/
2126static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2127{
2128 struct i40evf_adapter *adapter = netdev_priv(netdev);
2129 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2130
2131 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2132 return -EINVAL;
2133
Greg Rose5eae00c2013-12-21 06:12:45 +00002134 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002135 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2136 schedule_work(&adapter->reset_task);
2137
Greg Rose5eae00c2013-12-21 06:12:45 +00002138 return 0;
2139}
2140
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002141#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2142 NETIF_F_HW_VLAN_CTAG_RX |\
2143 NETIF_F_HW_VLAN_CTAG_FILTER)
2144
2145/**
2146 * i40evf_fix_features - fix up the netdev feature bits
2147 * @netdev: our net device
2148 * @features: desired feature bits
2149 *
2150 * Returns fixed-up features bits
2151 **/
2152static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2153 netdev_features_t features)
2154{
2155 struct i40evf_adapter *adapter = netdev_priv(netdev);
2156
2157 features &= ~I40EVF_VLAN_FEATURES;
2158 if (adapter->vf_res->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN)
2159 features |= I40EVF_VLAN_FEATURES;
2160 return features;
2161}
2162
Greg Rose5eae00c2013-12-21 06:12:45 +00002163static const struct net_device_ops i40evf_netdev_ops = {
2164 .ndo_open = i40evf_open,
2165 .ndo_stop = i40evf_close,
2166 .ndo_start_xmit = i40evf_xmit_frame,
2167 .ndo_get_stats = i40evf_get_stats,
2168 .ndo_set_rx_mode = i40evf_set_rx_mode,
2169 .ndo_validate_addr = eth_validate_addr,
2170 .ndo_set_mac_address = i40evf_set_mac,
2171 .ndo_change_mtu = i40evf_change_mtu,
2172 .ndo_tx_timeout = i40evf_tx_timeout,
2173 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2174 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002175 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002176#ifdef CONFIG_NET_POLL_CONTROLLER
2177 .ndo_poll_controller = i40evf_netpoll,
2178#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002179};
2180
2181/**
2182 * i40evf_check_reset_complete - check that VF reset is complete
2183 * @hw: pointer to hw struct
2184 *
2185 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2186 **/
2187static int i40evf_check_reset_complete(struct i40e_hw *hw)
2188{
2189 u32 rstat;
2190 int i;
2191
2192 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002193 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2194 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2195 if ((rstat == I40E_VFR_VFACTIVE) ||
2196 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002197 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002198 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002199 }
2200 return -EBUSY;
2201}
2202
2203/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002204 * i40evf_process_config - Process the config information we got from the PF
2205 * @adapter: board private structure
2206 *
2207 * Verify that we have a valid config struct, and set up our netdev features
2208 * and our VSI struct.
2209 **/
2210int i40evf_process_config(struct i40evf_adapter *adapter)
2211{
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002212 struct i40e_virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002213 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002214 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002215 int i;
2216
2217 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002218 for (i = 0; i < vfres->num_vsis; i++) {
2219 if (vfres->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2220 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002221 }
2222 if (!adapter->vsi_res) {
2223 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2224 return -ENODEV;
2225 }
2226
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002227 netdev->hw_enc_features |= NETIF_F_SG |
2228 NETIF_F_IP_CSUM |
2229 NETIF_F_IPV6_CSUM |
2230 NETIF_F_HIGHDMA |
2231 NETIF_F_SOFT_FEATURES |
2232 NETIF_F_TSO |
2233 NETIF_F_TSO_ECN |
2234 NETIF_F_TSO6 |
2235 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002236 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002237 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002238 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002239 NETIF_F_GSO_UDP_TUNNEL |
2240 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002241 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002242 NETIF_F_SCTP_CRC |
2243 NETIF_F_RXHASH |
2244 NETIF_F_RXCSUM |
2245 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002246
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002247 if (!(adapter->flags & I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002248 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2249
2250 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002251
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002252 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002253 netdev->vlan_features |= netdev->hw_enc_features |
2254 NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002255
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002256 /* Write features and hw_features separately to avoid polluting
2257 * with, or dropping, features that are set when we registgered.
2258 */
2259 netdev->hw_features |= netdev->hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002260
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002261 netdev->features |= netdev->hw_enc_features | I40EVF_VLAN_FEATURES;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002262 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002263
2264 /* disable VLAN features if not supported */
2265 if (!(vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN))
2266 netdev->features ^= I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002267
2268 adapter->vsi.id = adapter->vsi_res->vsi_id;
2269
2270 adapter->vsi.back = adapter;
2271 adapter->vsi.base_vector = 1;
2272 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2273 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2274 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2275 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2276 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002277 vsi->netdev = adapter->netdev;
2278 vsi->qs_handle = adapter->vsi_res->qset_handle;
2279 if (vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2280 adapter->rss_key_size = vfres->rss_key_size;
2281 adapter->rss_lut_size = vfres->rss_lut_size;
2282 } else {
2283 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2284 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2285 }
2286
Mitch Williamse6d038d2015-06-04 16:23:58 -04002287 return 0;
2288}
2289
2290/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002291 * i40evf_init_task - worker thread to perform delayed initialization
2292 * @work: pointer to work_struct containing our data
2293 *
2294 * This task completes the work that was begun in probe. Due to the nature
2295 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002296 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002297 * whole system, we'll do it in a task so we can sleep.
2298 * This task only runs during driver init. Once we've established
2299 * communications with the PF driver and set up our netdev, the watchdog
2300 * takes over.
2301 **/
2302static void i40evf_init_task(struct work_struct *work)
2303{
2304 struct i40evf_adapter *adapter = container_of(work,
2305 struct i40evf_adapter,
2306 init_task.work);
2307 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002308 struct i40e_hw *hw = &adapter->hw;
2309 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002310 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002311
2312 switch (adapter->state) {
2313 case __I40EVF_STARTUP:
2314 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002315 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2316 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002317 err = i40e_set_mac_type(hw);
2318 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002319 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2320 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002321 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002322 }
2323 err = i40evf_check_reset_complete(hw);
2324 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002325 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002326 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002327 goto err;
2328 }
2329 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2330 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2331 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2332 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2333
2334 err = i40evf_init_adminq(hw);
2335 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002336 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2337 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002338 goto err;
2339 }
2340 err = i40evf_send_api_ver(adapter);
2341 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002342 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002343 i40evf_shutdown_adminq(hw);
2344 goto err;
2345 }
2346 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2347 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002348 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002349 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002350 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002351 i40evf_shutdown_adminq(hw);
2352 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002353 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002354 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002355
2356 /* aq msg sent, awaiting reply */
2357 err = i40evf_verify_api_ver(adapter);
2358 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002359 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002360 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002361 else
2362 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2363 adapter->pf_version.major,
2364 adapter->pf_version.minor,
2365 I40E_VIRTCHNL_VERSION_MAJOR,
2366 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002367 goto err;
2368 }
2369 err = i40evf_send_vf_config_msg(adapter);
2370 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002371 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002372 err);
2373 goto err;
2374 }
2375 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2376 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002377 case __I40EVF_INIT_GET_RESOURCES:
2378 /* aq msg sent, awaiting reply */
2379 if (!adapter->vf_res) {
2380 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2381 (I40E_MAX_VF_VSI *
2382 sizeof(struct i40e_virtchnl_vsi_resource));
2383 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002384 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002385 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002386 }
2387 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002388 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002389 err = i40evf_send_vf_config_msg(adapter);
2390 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002391 } else if (err == I40E_ERR_PARAM) {
2392 /* We only get ERR_PARAM if the device is in a very bad
2393 * state or if we've been disabled for previous bad
2394 * behavior. Either way, we're done now.
2395 */
2396 i40evf_shutdown_adminq(hw);
2397 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2398 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002399 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002400 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002401 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2402 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002403 goto err_alloc;
2404 }
2405 adapter->state = __I40EVF_INIT_SW;
2406 break;
2407 default:
2408 goto err_alloc;
2409 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002410
2411 if (hw->mac.type == I40E_MAC_X722_VF)
2412 adapter->flags |= I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE;
2413
Mitch Williamse6d038d2015-06-04 16:23:58 -04002414 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002415 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002416 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002417
2418 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2419
Greg Rose5eae00c2013-12-21 06:12:45 +00002420 netdev->netdev_ops = &i40evf_netdev_ops;
2421 i40evf_set_ethtool_ops(netdev);
2422 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002423
Greg Rose5eae00c2013-12-21 06:12:45 +00002424 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002425 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002426 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002427 eth_hw_addr_random(netdev);
2428 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2429 } else {
2430 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2431 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2432 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002433 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002434
Greg Rose5eae00c2013-12-21 06:12:45 +00002435 init_timer(&adapter->watchdog_timer);
2436 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2437 adapter->watchdog_timer.data = (unsigned long)adapter;
2438 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2439
Mitch Williamscc052922014-10-25 03:24:34 +00002440 adapter->num_active_queues = min_t(int,
2441 adapter->vsi_res->num_queue_pairs,
2442 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002443 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2444 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002445 err = i40evf_init_interrupt_scheme(adapter);
2446 if (err)
2447 goto err_sw_init;
2448 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002449 if (adapter->vf_res->vf_offload_flags &
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002450 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2451 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2452
Greg Rose5eae00c2013-12-21 06:12:45 +00002453 err = i40evf_request_misc_irq(adapter);
2454 if (err)
2455 goto err_sw_init;
2456
2457 netif_carrier_off(netdev);
2458
Mitch Williamsef8693e2014-02-13 03:48:53 -08002459 if (!adapter->netdev_registered) {
2460 err = register_netdev(netdev);
2461 if (err)
2462 goto err_register;
2463 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002464
2465 adapter->netdev_registered = true;
2466
2467 netif_tx_stop_all_queues(netdev);
2468
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002469 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002470 if (netdev->features & NETIF_F_GRO)
2471 dev_info(&pdev->dev, "GRO is enabled\n");
2472
Greg Rose5eae00c2013-12-21 06:12:45 +00002473 adapter->state = __I40EVF_DOWN;
2474 set_bit(__I40E_DOWN, &adapter->vsi.state);
2475 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002476
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002477 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2478 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2479 if (!adapter->rss_key || !adapter->rss_lut)
2480 goto err_mem;
2481
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002482 if (RSS_AQ(adapter)) {
2483 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2484 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2485 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002486 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002487 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002488 return;
2489restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002490 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002491 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002492err_mem:
2493 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002494err_register:
2495 i40evf_free_misc_irq(adapter);
2496err_sw_init:
2497 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002498err_alloc:
2499 kfree(adapter->vf_res);
2500 adapter->vf_res = NULL;
2501err:
2502 /* Things went into the weeds, so try again later */
2503 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002504 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002505 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002506 i40evf_shutdown_adminq(hw);
2507 adapter->state = __I40EVF_STARTUP;
2508 schedule_delayed_work(&adapter->init_task, HZ * 5);
2509 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002510 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002511 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002512}
2513
2514/**
2515 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2516 * @pdev: pci device structure
2517 **/
2518static void i40evf_shutdown(struct pci_dev *pdev)
2519{
2520 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002521 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002522
2523 netif_device_detach(netdev);
2524
2525 if (netif_running(netdev))
2526 i40evf_close(netdev);
2527
Mitch Williams00293fd2015-01-09 11:18:18 +00002528 /* Prevent the watchdog from running. */
2529 adapter->state = __I40EVF_REMOVE;
2530 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002531
Greg Rose5eae00c2013-12-21 06:12:45 +00002532#ifdef CONFIG_PM
2533 pci_save_state(pdev);
2534
2535#endif
2536 pci_disable_device(pdev);
2537}
2538
2539/**
2540 * i40evf_probe - Device Initialization Routine
2541 * @pdev: PCI device information struct
2542 * @ent: entry in i40evf_pci_tbl
2543 *
2544 * Returns 0 on success, negative on failure
2545 *
2546 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2547 * The OS initialization, configuring of the adapter private structure,
2548 * and a hardware reset occur.
2549 **/
2550static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2551{
2552 struct net_device *netdev;
2553 struct i40evf_adapter *adapter = NULL;
2554 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002555 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002556
2557 err = pci_enable_device(pdev);
2558 if (err)
2559 return err;
2560
Mitch Williams64942942014-02-11 08:26:33 +00002561 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002562 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002563 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2564 if (err) {
2565 dev_err(&pdev->dev,
2566 "DMA configuration failed: 0x%x\n", err);
2567 goto err_dma;
2568 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002569 }
2570
2571 err = pci_request_regions(pdev, i40evf_driver_name);
2572 if (err) {
2573 dev_err(&pdev->dev,
2574 "pci_request_regions failed 0x%x\n", err);
2575 goto err_pci_reg;
2576 }
2577
2578 pci_enable_pcie_error_reporting(pdev);
2579
2580 pci_set_master(pdev);
2581
Mitch Williams1255b7a2015-11-06 15:25:59 -08002582 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002583 if (!netdev) {
2584 err = -ENOMEM;
2585 goto err_alloc_etherdev;
2586 }
2587
2588 SET_NETDEV_DEV(netdev, &pdev->dev);
2589
2590 pci_set_drvdata(pdev, netdev);
2591 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002592
2593 adapter->netdev = netdev;
2594 adapter->pdev = pdev;
2595
2596 hw = &adapter->hw;
2597 hw->back = adapter;
2598
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002599 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002600 adapter->state = __I40EVF_STARTUP;
2601
2602 /* Call save state here because it relies on the adapter struct. */
2603 pci_save_state(pdev);
2604
2605 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2606 pci_resource_len(pdev, 0));
2607 if (!hw->hw_addr) {
2608 err = -EIO;
2609 goto err_ioremap;
2610 }
2611 hw->vendor_id = pdev->vendor;
2612 hw->device_id = pdev->device;
2613 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2614 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2615 hw->subsystem_device_id = pdev->subsystem_device;
2616 hw->bus.device = PCI_SLOT(pdev->devfn);
2617 hw->bus.func = PCI_FUNC(pdev->devfn);
2618
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002619 /* set up the locks for the AQ, do this only once in probe
2620 * and destroy them only once in remove
2621 */
2622 mutex_init(&hw->aq.asq_mutex);
2623 mutex_init(&hw->aq.arq_mutex);
2624
Serey Kong8bb1a542014-08-01 13:27:15 -07002625 INIT_LIST_HEAD(&adapter->mac_filter_list);
2626 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2627
Greg Rose5eae00c2013-12-21 06:12:45 +00002628 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2629 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2630 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2631 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002632 schedule_delayed_work(&adapter->init_task,
2633 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002634
2635 return 0;
2636
2637err_ioremap:
2638 free_netdev(netdev);
2639err_alloc_etherdev:
2640 pci_release_regions(pdev);
2641err_pci_reg:
2642err_dma:
2643 pci_disable_device(pdev);
2644 return err;
2645}
2646
2647#ifdef CONFIG_PM
2648/**
2649 * i40evf_suspend - Power management suspend routine
2650 * @pdev: PCI device information struct
2651 * @state: unused
2652 *
2653 * Called when the system (VM) is entering sleep/suspend.
2654 **/
2655static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2656{
2657 struct net_device *netdev = pci_get_drvdata(pdev);
2658 struct i40evf_adapter *adapter = netdev_priv(netdev);
2659 int retval = 0;
2660
2661 netif_device_detach(netdev);
2662
2663 if (netif_running(netdev)) {
2664 rtnl_lock();
2665 i40evf_down(adapter);
2666 rtnl_unlock();
2667 }
2668 i40evf_free_misc_irq(adapter);
2669 i40evf_reset_interrupt_capability(adapter);
2670
2671 retval = pci_save_state(pdev);
2672 if (retval)
2673 return retval;
2674
2675 pci_disable_device(pdev);
2676
2677 return 0;
2678}
2679
2680/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002681 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002682 * @pdev: PCI device information struct
2683 *
2684 * Called when the system (VM) is resumed from sleep/suspend.
2685 **/
2686static int i40evf_resume(struct pci_dev *pdev)
2687{
2688 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2689 struct net_device *netdev = adapter->netdev;
2690 u32 err;
2691
2692 pci_set_power_state(pdev, PCI_D0);
2693 pci_restore_state(pdev);
2694 /* pci_restore_state clears dev->state_saved so call
2695 * pci_save_state to restore it.
2696 */
2697 pci_save_state(pdev);
2698
2699 err = pci_enable_device_mem(pdev);
2700 if (err) {
2701 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2702 return err;
2703 }
2704 pci_set_master(pdev);
2705
2706 rtnl_lock();
2707 err = i40evf_set_interrupt_capability(adapter);
2708 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002709 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002710 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2711 return err;
2712 }
2713 err = i40evf_request_misc_irq(adapter);
2714 rtnl_unlock();
2715 if (err) {
2716 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2717 return err;
2718 }
2719
2720 schedule_work(&adapter->reset_task);
2721
2722 netif_device_attach(netdev);
2723
2724 return err;
2725}
2726
2727#endif /* CONFIG_PM */
2728/**
2729 * i40evf_remove - Device Removal Routine
2730 * @pdev: PCI device information struct
2731 *
2732 * i40evf_remove is called by the PCI subsystem to alert the driver
2733 * that it should release a PCI device. The could be caused by a
2734 * Hot-Plug event, or because the driver is going to be removed from
2735 * memory.
2736 **/
2737static void i40evf_remove(struct pci_dev *pdev)
2738{
2739 struct net_device *netdev = pci_get_drvdata(pdev);
2740 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002741 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002742 struct i40e_hw *hw = &adapter->hw;
2743
2744 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002745 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002746
2747 if (adapter->netdev_registered) {
2748 unregister_netdev(netdev);
2749 adapter->netdev_registered = false;
2750 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002751
Mitch Williamsf4a71882015-01-09 11:18:16 +00002752 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002753 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002754 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002755 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002756 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002757 /* If the FW isn't responding, kick it once, but only once. */
2758 if (!i40evf_asq_done(hw)) {
2759 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002760 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002761 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002762
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002763 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002764 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002765 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002766 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002767 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002768 }
2769
Mitch Williamse5d17c32014-06-04 04:22:38 +00002770 if (adapter->watchdog_timer.function)
2771 del_timer_sync(&adapter->watchdog_timer);
2772
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002773 flush_scheduled_work();
2774
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002775 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04002776
Greg Rose5eae00c2013-12-21 06:12:45 +00002777 if (hw->aq.asq.count)
2778 i40evf_shutdown_adminq(hw);
2779
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002780 /* destroy the locks only once, here */
2781 mutex_destroy(&hw->aq.arq_mutex);
2782 mutex_destroy(&hw->aq.asq_mutex);
2783
Greg Rose5eae00c2013-12-21 06:12:45 +00002784 iounmap(hw->hw_addr);
2785 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07002786 i40evf_free_all_tx_resources(adapter);
2787 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002788 i40evf_free_queues(adapter);
2789 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002790 /* If we got removed before an up/down sequence, we've got a filter
2791 * hanging out there that we need to get rid of.
2792 */
2793 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2794 list_del(&f->list);
2795 kfree(f);
2796 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002797 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2798 list_del(&f->list);
2799 kfree(f);
2800 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002801
2802 free_netdev(netdev);
2803
2804 pci_disable_pcie_error_reporting(pdev);
2805
2806 pci_disable_device(pdev);
2807}
2808
2809static struct pci_driver i40evf_driver = {
2810 .name = i40evf_driver_name,
2811 .id_table = i40evf_pci_tbl,
2812 .probe = i40evf_probe,
2813 .remove = i40evf_remove,
2814#ifdef CONFIG_PM
2815 .suspend = i40evf_suspend,
2816 .resume = i40evf_resume,
2817#endif
2818 .shutdown = i40evf_shutdown,
2819};
2820
2821/**
2822 * i40e_init_module - Driver Registration Routine
2823 *
2824 * i40e_init_module is the first routine called when the driver is
2825 * loaded. All it does is register with the PCI subsystem.
2826 **/
2827static int __init i40evf_init_module(void)
2828{
2829 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002830
Greg Rose5eae00c2013-12-21 06:12:45 +00002831 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002832 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002833
2834 pr_info("%s\n", i40evf_copyright);
2835
Jacob Keller6992a6c2016-08-04 11:37:01 -07002836 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
2837 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002838 if (!i40evf_wq) {
2839 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2840 return -ENOMEM;
2841 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002842 ret = pci_register_driver(&i40evf_driver);
2843 return ret;
2844}
2845
2846module_init(i40evf_init_module);
2847
2848/**
2849 * i40e_exit_module - Driver Exit Cleanup Routine
2850 *
2851 * i40e_exit_module is called just before the driver is removed
2852 * from memory.
2853 **/
2854static void __exit i40evf_exit_module(void)
2855{
2856 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002857 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00002858}
2859
2860module_exit(i40evf_exit_module);
2861
2862/* i40evf_main.c */