blob: 14372810fc27aefc36ed1244641051eda34ec184 [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 Pujarifa90efa2016-09-06 18:05:12 -070041#define DRV_VERSION_BUILD 16
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];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700373 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000374
375 rx_ring->q_vector = q_vector;
376 rx_ring->next = q_vector->rx.ring;
377 rx_ring->vsi = &adapter->vsi;
378 q_vector->rx.ring = rx_ring;
379 q_vector->rx.count++;
380 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700381 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700382 q_vector->ring_mask |= BIT(r_idx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400383 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700384 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000385}
386
387/**
388 * i40evf_map_vector_to_txq - associate irqs with tx queues
389 * @adapter: board private structure
390 * @v_idx: interrupt number
391 * @t_idx: queue number
392 **/
393static void
394i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
395{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400396 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400397 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700398 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000399
400 tx_ring->q_vector = q_vector;
401 tx_ring->next = q_vector->tx.ring;
402 tx_ring->vsi = &adapter->vsi;
403 q_vector->tx.ring = tx_ring;
404 q_vector->tx.count++;
405 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700406 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400407 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000408 q_vector->num_ringpairs++;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700409 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000410}
411
412/**
413 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
414 * @adapter: board private structure to initialize
415 *
416 * This function maps descriptor rings to the queue-specific vectors
417 * we were allotted through the MSI-X enabling code. Ideally, we'd have
418 * one vector per ring/queue, but on a constrained vector budget, we
419 * group the rings as "efficiently" as possible. You would add new
420 * mapping configurations in here.
421 **/
422static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
423{
424 int q_vectors;
425 int v_start = 0;
426 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000427 int rxr_remaining = adapter->num_active_queues;
428 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000429 int i, j;
430 int rqpv, tqpv;
431 int err = 0;
432
433 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
434
435 /* The ideal configuration...
436 * We have enough vectors to map one per queue.
437 */
Mitch Williams973371d2015-04-27 14:57:09 -0400438 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000439 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
440 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
441
442 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
443 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
444 goto out;
445 }
446
447 /* If we don't have enough vectors for a 1-to-1
448 * mapping, we'll have to group them so there are
449 * multiple queues per vector.
450 * Re-adjusting *qpv takes care of the remainder.
451 */
452 for (i = v_start; i < q_vectors; i++) {
453 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
454 for (j = 0; j < rqpv; j++) {
455 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
456 rxr_idx++;
457 rxr_remaining--;
458 }
459 }
460 for (i = v_start; i < q_vectors; i++) {
461 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
462 for (j = 0; j < tqpv; j++) {
463 i40evf_map_vector_to_txq(adapter, i, txr_idx);
464 txr_idx++;
465 txr_remaining--;
466 }
467 }
468
469out:
470 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
471
472 return err;
473}
474
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700475#ifdef CONFIG_NET_POLL_CONTROLLER
476/**
477 * i40evf_netpoll - A Polling 'interrupt' handler
478 * @netdev: network interface device structure
479 *
480 * This is used by netconsole to send skbs without having to re-enable
481 * interrupts. It's not called while the normal interrupt routine is executing.
482 **/
483static void i40evf_netpoll(struct net_device *netdev)
484{
485 struct i40evf_adapter *adapter = netdev_priv(netdev);
486 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
487 int i;
488
489 /* if interface is down do nothing */
490 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
491 return;
492
493 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400494 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700495}
496
497#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000498/**
499 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
500 * @adapter: board private structure
501 *
502 * Allocates MSI-X vectors for tx and rx handling, and requests
503 * interrupts from the kernel.
504 **/
505static int
506i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
507{
508 int vector, err, q_vectors;
509 int rx_int_idx = 0, tx_int_idx = 0;
510
511 i40evf_irq_disable(adapter);
512 /* Decrement for Other and TCP Timer vectors */
513 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
514
515 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400516 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Greg Rose5eae00c2013-12-21 06:12:45 +0000517
518 if (q_vector->tx.ring && q_vector->rx.ring) {
519 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
520 "i40evf-%s-%s-%d", basename,
521 "TxRx", rx_int_idx++);
522 tx_int_idx++;
523 } else if (q_vector->rx.ring) {
524 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
525 "i40evf-%s-%s-%d", basename,
526 "rx", rx_int_idx++);
527 } else if (q_vector->tx.ring) {
528 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
529 "i40evf-%s-%s-%d", basename,
530 "tx", tx_int_idx++);
531 } else {
532 /* skip this unused q_vector */
533 continue;
534 }
535 err = request_irq(
536 adapter->msix_entries[vector + NONQ_VECS].vector,
537 i40evf_msix_clean_rings,
538 0,
539 q_vector->name,
540 q_vector);
541 if (err) {
542 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400543 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000544 goto free_queue_irqs;
545 }
546 /* assign the mask for this irq */
547 irq_set_affinity_hint(
548 adapter->msix_entries[vector + NONQ_VECS].vector,
549 q_vector->affinity_mask);
550 }
551
552 return 0;
553
554free_queue_irqs:
555 while (vector) {
556 vector--;
557 irq_set_affinity_hint(
558 adapter->msix_entries[vector + NONQ_VECS].vector,
559 NULL);
560 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400561 &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000562 }
563 return err;
564}
565
566/**
567 * i40evf_request_misc_irq - Initialize MSI-X interrupts
568 * @adapter: board private structure
569 *
570 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
571 * vector is only for the admin queue, and stays active even when the netdev
572 * is closed.
573 **/
574static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
575{
576 struct net_device *netdev = adapter->netdev;
577 int err;
578
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700579 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000580 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
581 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000582 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800583 &i40evf_msix_aq, 0,
584 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000585 if (err) {
586 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800587 "request_irq for %s failed: %d\n",
588 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000589 free_irq(adapter->msix_entries[0].vector, netdev);
590 }
591 return err;
592}
593
594/**
595 * i40evf_free_traffic_irqs - Free MSI-X interrupts
596 * @adapter: board private structure
597 *
598 * Frees all MSI-X vectors other than 0.
599 **/
600static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
601{
602 int i;
603 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000604
Greg Rose5eae00c2013-12-21 06:12:45 +0000605 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
606
607 for (i = 0; i < q_vectors; i++) {
608 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
609 NULL);
610 free_irq(adapter->msix_entries[i+1].vector,
Mitch Williams7d96ba12015-10-26 19:44:39 -0400611 &adapter->q_vectors[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000612 }
613}
614
615/**
616 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
617 * @adapter: board private structure
618 *
619 * Frees MSI-X vector 0.
620 **/
621static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
622{
623 struct net_device *netdev = adapter->netdev;
624
625 free_irq(adapter->msix_entries[0].vector, netdev);
626}
627
628/**
629 * i40evf_configure_tx - Configure Transmit Unit after Reset
630 * @adapter: board private structure
631 *
632 * Configure the Tx unit of the MAC after a reset.
633 **/
634static void i40evf_configure_tx(struct i40evf_adapter *adapter)
635{
636 struct i40e_hw *hw = &adapter->hw;
637 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000638
Mitch Williamscc052922014-10-25 03:24:34 +0000639 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400640 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000641}
642
643/**
644 * i40evf_configure_rx - Configure Receive Unit after Reset
645 * @adapter: board private structure
646 *
647 * Configure the Rx unit of the MAC after a reset.
648 **/
649static void i40evf_configure_rx(struct i40evf_adapter *adapter)
650{
651 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000652 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000653
Mitch Williamscc052922014-10-25 03:24:34 +0000654 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400655 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Jesse Brandeburg19b85e62016-04-18 11:33:45 -0700656 adapter->rx_rings[i].rx_buf_len = I40EVF_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000657 }
658}
659
660/**
661 * i40evf_find_vlan - Search filter list for specific vlan filter
662 * @adapter: board private structure
663 * @vlan: vlan tag
664 *
665 * Returns ptr to the filter object or NULL
666 **/
667static struct
668i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
669{
670 struct i40evf_vlan_filter *f;
671
672 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
673 if (vlan == f->vlan)
674 return f;
675 }
676 return NULL;
677}
678
679/**
680 * i40evf_add_vlan - Add a vlan filter to the list
681 * @adapter: board private structure
682 * @vlan: VLAN tag
683 *
684 * Returns ptr to the filter object or NULL when no memory available.
685 **/
686static struct
687i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
688{
Mitch Williams13acb542015-03-31 00:45:05 -0700689 struct i40evf_vlan_filter *f = NULL;
690 int count = 50;
691
692 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
693 &adapter->crit_section)) {
694 udelay(1);
695 if (--count == 0)
696 goto out;
697 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000698
699 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000700 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000701 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000702 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700703 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000704
Greg Rose5eae00c2013-12-21 06:12:45 +0000705 f->vlan = vlan;
706
707 INIT_LIST_HEAD(&f->list);
708 list_add(&f->list, &adapter->vlan_filter_list);
709 f->add = true;
710 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
711 }
712
Mitch Williams13acb542015-03-31 00:45:05 -0700713clearout:
714 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
715out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000716 return f;
717}
718
719/**
720 * i40evf_del_vlan - Remove a vlan filter from the list
721 * @adapter: board private structure
722 * @vlan: VLAN tag
723 **/
724static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
725{
726 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700727 int count = 50;
728
729 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
730 &adapter->crit_section)) {
731 udelay(1);
732 if (--count == 0)
733 return;
734 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000735
736 f = i40evf_find_vlan(adapter, vlan);
737 if (f) {
738 f->remove = true;
739 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
740 }
Mitch Williams13acb542015-03-31 00:45:05 -0700741 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000742}
743
744/**
745 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
746 * @netdev: network device struct
747 * @vid: VLAN tag
748 **/
749static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000750 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000751{
752 struct i40evf_adapter *adapter = netdev_priv(netdev);
753
Mitch Williams8ed995f2015-08-28 17:55:57 -0400754 if (!VLAN_ALLOWED(adapter))
755 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000756 if (i40evf_add_vlan(adapter, vid) == NULL)
757 return -ENOMEM;
758 return 0;
759}
760
761/**
762 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
763 * @netdev: network device struct
764 * @vid: VLAN tag
765 **/
766static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000767 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000768{
769 struct i40evf_adapter *adapter = netdev_priv(netdev);
770
Mitch Williams8ed995f2015-08-28 17:55:57 -0400771 if (VLAN_ALLOWED(adapter)) {
772 i40evf_del_vlan(adapter, vid);
773 return 0;
774 }
775 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000776}
777
778/**
779 * i40evf_find_filter - Search filter list for specific mac filter
780 * @adapter: board private structure
781 * @macaddr: the MAC address
782 *
783 * Returns ptr to the filter object or NULL
784 **/
785static struct
786i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
787 u8 *macaddr)
788{
789 struct i40evf_mac_filter *f;
790
791 if (!macaddr)
792 return NULL;
793
794 list_for_each_entry(f, &adapter->mac_filter_list, list) {
795 if (ether_addr_equal(macaddr, f->macaddr))
796 return f;
797 }
798 return NULL;
799}
800
801/**
802 * i40e_add_filter - Add a mac filter to the filter list
803 * @adapter: board private structure
804 * @macaddr: the MAC address
805 *
806 * Returns ptr to the filter object or NULL when no memory available.
807 **/
808static struct
809i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
810 u8 *macaddr)
811{
812 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000813 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000814
815 if (!macaddr)
816 return NULL;
817
818 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000819 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000820 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000821 if (--count == 0)
822 return NULL;
823 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000824
825 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000826 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000827 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000828 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000829 clear_bit(__I40EVF_IN_CRITICAL_TASK,
830 &adapter->crit_section);
831 return NULL;
832 }
833
Greg Rose9a173902014-05-22 06:32:02 +0000834 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000835
Mitch Williams63590b62016-05-16 10:26:42 -0700836 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000837 f->add = true;
838 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
839 }
840
841 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
842 return f;
843}
844
845/**
846 * i40evf_set_mac - NDO callback to set port mac address
847 * @netdev: network interface device structure
848 * @p: pointer to an address structure
849 *
850 * Returns 0 on success, negative on failure
851 **/
852static int i40evf_set_mac(struct net_device *netdev, void *p)
853{
854 struct i40evf_adapter *adapter = netdev_priv(netdev);
855 struct i40e_hw *hw = &adapter->hw;
856 struct i40evf_mac_filter *f;
857 struct sockaddr *addr = p;
858
859 if (!is_valid_ether_addr(addr->sa_data))
860 return -EADDRNOTAVAIL;
861
862 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
863 return 0;
864
Mitch Williams14e52ee2015-08-31 19:54:44 -0400865 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
866 return -EPERM;
867
868 f = i40evf_find_filter(adapter, hw->mac.addr);
869 if (f) {
870 f->remove = true;
871 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
872 }
873
Greg Rose5eae00c2013-12-21 06:12:45 +0000874 f = i40evf_add_filter(adapter, addr->sa_data);
875 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000876 ether_addr_copy(hw->mac.addr, addr->sa_data);
877 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000878 }
879
880 return (f == NULL) ? -ENOMEM : 0;
881}
882
883/**
884 * i40evf_set_rx_mode - NDO callback to set the netdev filters
885 * @netdev: network interface device structure
886 **/
887static void i40evf_set_rx_mode(struct net_device *netdev)
888{
889 struct i40evf_adapter *adapter = netdev_priv(netdev);
890 struct i40evf_mac_filter *f, *ftmp;
891 struct netdev_hw_addr *uca;
892 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400893 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000894 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000895
896 /* add addr if not already in the filter list */
897 netdev_for_each_uc_addr(uca, netdev) {
898 i40evf_add_filter(adapter, uca->addr);
899 }
900 netdev_for_each_mc_addr(mca, netdev) {
901 i40evf_add_filter(adapter, mca->addr);
902 }
903
904 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000905 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000906 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000907 if (--count == 0) {
908 dev_err(&adapter->pdev->dev,
909 "Failed to get lock in %s\n", __func__);
910 return;
911 }
912 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000913 /* remove filter if not in netdev list */
914 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400915 netdev_for_each_mc_addr(mca, netdev)
916 if (ether_addr_equal(mca->addr, f->macaddr))
917 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000918
Shannon Nelson2f41f332015-08-26 15:14:20 -0400919 netdev_for_each_uc_addr(uca, netdev)
920 if (ether_addr_equal(uca->addr, f->macaddr))
921 goto bottom_of_search_loop;
922
923 for_each_dev_addr(netdev, ha)
924 if (ether_addr_equal(ha->addr, f->macaddr))
925 goto bottom_of_search_loop;
926
927 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
928 goto bottom_of_search_loop;
929
930 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
931 f->remove = true;
932 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
933
934bottom_of_search_loop:
935 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000936 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700937
938 if (netdev->flags & IFF_PROMISC &&
939 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
940 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
941 else if (!(netdev->flags & IFF_PROMISC) &&
942 adapter->flags & I40EVF_FLAG_PROMISC_ON)
943 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
944
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700945 if (netdev->flags & IFF_ALLMULTI &&
946 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
947 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
948 else if (!(netdev->flags & IFF_ALLMULTI) &&
949 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
950 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
951
Greg Rose5eae00c2013-12-21 06:12:45 +0000952 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
953}
954
955/**
956 * i40evf_napi_enable_all - enable NAPI on all queue vectors
957 * @adapter: board private structure
958 **/
959static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
960{
961 int q_idx;
962 struct i40e_q_vector *q_vector;
963 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
964
965 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
966 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000967
Mitch Williams7d96ba12015-10-26 19:44:39 -0400968 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000969 napi = &q_vector->napi;
970 napi_enable(napi);
971 }
972}
973
974/**
975 * i40evf_napi_disable_all - disable NAPI on all queue vectors
976 * @adapter: board private structure
977 **/
978static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
979{
980 int q_idx;
981 struct i40e_q_vector *q_vector;
982 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
983
984 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400985 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000986 napi_disable(&q_vector->napi);
987 }
988}
989
990/**
991 * i40evf_configure - set up transmit and receive data structures
992 * @adapter: board private structure
993 **/
994static void i40evf_configure(struct i40evf_adapter *adapter)
995{
996 struct net_device *netdev = adapter->netdev;
997 int i;
998
999 i40evf_set_rx_mode(netdev);
1000
1001 i40evf_configure_tx(adapter);
1002 i40evf_configure_rx(adapter);
1003 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1004
Mitch Williamscc052922014-10-25 03:24:34 +00001005 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001006 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001007
Mitch Williamsb1630982016-04-18 11:33:48 -07001008 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001009 }
1010}
1011
1012/**
1013 * i40evf_up_complete - Finish the last steps of bringing up a connection
1014 * @adapter: board private structure
1015 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001016static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001017{
1018 adapter->state = __I40EVF_RUNNING;
1019 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1020
1021 i40evf_napi_enable_all(adapter);
1022
1023 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1024 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001025}
1026
1027/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001028 * i40e_down - Shutdown the connection processing
1029 * @adapter: board private structure
1030 **/
1031void i40evf_down(struct i40evf_adapter *adapter)
1032{
1033 struct net_device *netdev = adapter->netdev;
1034 struct i40evf_mac_filter *f;
1035
Mitch Williams209dc4d2015-12-09 15:50:27 -08001036 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001037 return;
1038
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001039 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1040 &adapter->crit_section))
1041 usleep_range(500, 1000);
1042
Mitch Williams63e18c22015-03-27 00:12:10 -07001043 netif_carrier_off(netdev);
1044 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001045 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001046 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001047 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001048
Mitch Williamsef8693e2014-02-13 03:48:53 -08001049 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001050 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1051 f->remove = true;
1052 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001053 /* remove all VLAN filters */
1054 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1055 f->remove = true;
1056 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001057 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1058 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001059 /* cancel any current operation */
1060 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001061 /* Schedule operations to close down the HW. Don't wait
1062 * here for this to complete. The watchdog is still running
1063 * and it will take care of this.
1064 */
1065 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001066 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001067 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001068 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001069
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001070 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001071}
1072
1073/**
1074 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1075 * @adapter: board private structure
1076 * @vectors: number of vectors to request
1077 *
1078 * Work with the OS to set up the MSIX vectors needed.
1079 *
1080 * Returns 0 on success, negative on failure
1081 **/
1082static int
1083i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1084{
1085 int err, vector_threshold;
1086
1087 /* We'll want at least 3 (vector_threshold):
1088 * 0) Other (Admin Queue and link, mostly)
1089 * 1) TxQ[0] Cleanup
1090 * 2) RxQ[0] Cleanup
1091 */
1092 vector_threshold = MIN_MSIX_COUNT;
1093
1094 /* The more we get, the more we will assign to Tx/Rx Cleanup
1095 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1096 * Right now, we simply care about how many we'll get; we'll
1097 * set them up later while requesting irq's.
1098 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001099 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1100 vector_threshold, vectors);
1101 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001102 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001103 kfree(adapter->msix_entries);
1104 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001105 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001106 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001107
1108 /* Adjust for only the vectors we'll use, which is minimum
1109 * of max_msix_q_vectors + NONQ_VECS, or the number of
1110 * vectors we were allocated.
1111 */
1112 adapter->num_msix_vectors = err;
1113 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001114}
1115
1116/**
1117 * i40evf_free_queues - Free memory for all rings
1118 * @adapter: board private structure to initialize
1119 *
1120 * Free all of the memory associated with queue pairs.
1121 **/
1122static void i40evf_free_queues(struct i40evf_adapter *adapter)
1123{
Greg Rose5eae00c2013-12-21 06:12:45 +00001124 if (!adapter->vsi_res)
1125 return;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001126 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001127 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001128 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001129 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001130}
1131
1132/**
1133 * i40evf_alloc_queues - Allocate memory for all rings
1134 * @adapter: board private structure to initialize
1135 *
1136 * We allocate one ring per queue at run-time since we don't know the
1137 * number of queues at compile-time. The polling_netdev array is
1138 * intended for Multiqueue, but should work fine with a single queue.
1139 **/
1140static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1141{
1142 int i;
1143
Mitch Williams0dd438d2015-10-26 19:44:40 -04001144 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1145 sizeof(struct i40e_ring), GFP_KERNEL);
1146 if (!adapter->tx_rings)
1147 goto err_out;
1148 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1149 sizeof(struct i40e_ring), GFP_KERNEL);
1150 if (!adapter->rx_rings)
1151 goto err_out;
1152
Mitch Williamscc052922014-10-25 03:24:34 +00001153 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001154 struct i40e_ring *tx_ring;
1155 struct i40e_ring *rx_ring;
1156
Mitch Williams0dd438d2015-10-26 19:44:40 -04001157 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001158
1159 tx_ring->queue_index = i;
1160 tx_ring->netdev = adapter->netdev;
1161 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001162 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001163 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001164 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1165 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001166
Mitch Williams0dd438d2015-10-26 19:44:40 -04001167 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001168 rx_ring->queue_index = i;
1169 rx_ring->netdev = adapter->netdev;
1170 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001171 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001172 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001173 }
1174
1175 return 0;
1176
1177err_out:
1178 i40evf_free_queues(adapter);
1179 return -ENOMEM;
1180}
1181
1182/**
1183 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1184 * @adapter: board private structure to initialize
1185 *
1186 * Attempt to configure the interrupts using the best available
1187 * capabilities of the hardware and the kernel.
1188 **/
1189static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1190{
1191 int vector, v_budget;
1192 int pairs = 0;
1193 int err = 0;
1194
1195 if (!adapter->vsi_res) {
1196 err = -EIO;
1197 goto out;
1198 }
Mitch Williamscc052922014-10-25 03:24:34 +00001199 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001200
1201 /* It's easy to be greedy for MSI-X vectors, but it really
1202 * doesn't do us much good if we have a lot more vectors
1203 * than CPU's. So let's be conservative and only ask for
1204 * (roughly) twice the number of vectors as there are CPU's.
1205 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001206 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1207 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001208
Greg Rose5eae00c2013-12-21 06:12:45 +00001209 adapter->msix_entries = kcalloc(v_budget,
1210 sizeof(struct msix_entry), GFP_KERNEL);
1211 if (!adapter->msix_entries) {
1212 err = -ENOMEM;
1213 goto out;
1214 }
1215
1216 for (vector = 0; vector < v_budget; vector++)
1217 adapter->msix_entries[vector].entry = vector;
1218
Mitch Williams313ed2d2015-08-27 11:42:31 -04001219 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001220
1221out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001222 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1223 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001224 return err;
1225}
1226
1227/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001228 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1229 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001230 *
1231 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001232 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001233static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001234{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001235 struct i40e_aqc_get_set_rss_key_data *rss_key =
1236 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001237 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001238 int ret = 0;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001239
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001240 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1241 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001242 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001243 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001244 return -EBUSY;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001245 }
1246
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001247 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1248 if (ret) {
1249 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1250 i40evf_stat_str(hw, ret),
1251 i40evf_aq_str(hw, hw->aq.asq_last_status));
1252 return ret;
1253
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001254 }
1255
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001256 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1257 adapter->rss_lut, adapter->rss_lut_size);
1258 if (ret) {
1259 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1260 i40evf_stat_str(hw, ret),
1261 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001262 }
1263
1264 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001265
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001266}
1267
1268/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001269 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001270 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001271 *
1272 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001273 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001274static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001275{
1276 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001277 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001278 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001279
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001280 dw = (u32 *)adapter->rss_key;
1281 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1282 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001283
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001284 dw = (u32 *)adapter->rss_lut;
1285 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1286 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001287
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001288 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001289
1290 return 0;
1291}
1292
1293/**
1294 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001295 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001296 *
1297 * Returns 0 on success, negative on failure
1298 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001299int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001300{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001301
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001302 if (RSS_PF(adapter)) {
1303 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1304 I40EVF_FLAG_AQ_SET_RSS_KEY;
1305 return 0;
1306 } else if (RSS_AQ(adapter)) {
1307 return i40evf_config_rss_aq(adapter);
1308 } else {
1309 return i40evf_config_rss_reg(adapter);
1310 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001311}
1312
1313/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001314 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001315 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001316 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001317static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001318{
1319 u16 i;
1320
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001321 for (i = 0; i < adapter->rss_lut_size; i++)
1322 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001323}
1324
1325/**
Helin Zhang96a81982015-10-26 19:44:31 -04001326 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001327 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001328 *
1329 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001330 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001331static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001332{
1333 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001334 int ret;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001335
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001336 if (!RSS_PF(adapter)) {
1337 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1338 if (adapter->vf_res->vf_offload_flags &
1339 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1340 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1341 else
1342 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001343
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001344 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1345 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1346 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001347
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001348 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001349
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001350 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1351 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001352
1353 return ret;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001354}
1355
1356/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001357 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1358 * @adapter: board private structure to initialize
1359 *
1360 * We allocate one q_vector per queue interrupt. If allocation fails we
1361 * return -ENOMEM.
1362 **/
1363static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1364{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001365 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001366 struct i40e_q_vector *q_vector;
1367
1368 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001369 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001370 GFP_KERNEL);
1371 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001372 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001373
1374 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001375 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001376 q_vector->adapter = adapter;
1377 q_vector->vsi = &adapter->vsi;
1378 q_vector->v_idx = q_idx;
1379 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001380 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001381 }
1382
1383 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001384}
1385
1386/**
1387 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1388 * @adapter: board private structure to initialize
1389 *
1390 * This function frees the memory allocated to the q_vectors. In addition if
1391 * NAPI is enabled it will delete any references to the NAPI struct prior
1392 * to freeing the q_vector.
1393 **/
1394static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1395{
1396 int q_idx, num_q_vectors;
1397 int napi_vectors;
1398
1399 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001400 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001401
1402 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001403 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001404 if (q_idx < napi_vectors)
1405 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001406 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001407 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001408}
1409
1410/**
1411 * i40evf_reset_interrupt_capability - Reset MSIX setup
1412 * @adapter: board private structure
1413 *
1414 **/
1415void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1416{
1417 pci_disable_msix(adapter->pdev);
1418 kfree(adapter->msix_entries);
1419 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001420}
1421
1422/**
1423 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1424 * @adapter: board private structure to initialize
1425 *
1426 **/
1427int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1428{
1429 int err;
1430
Jacob Keller62fe2a82016-07-27 12:02:33 -07001431 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001432 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001433 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001434 if (err) {
1435 dev_err(&adapter->pdev->dev,
1436 "Unable to setup interrupt capabilities\n");
1437 goto err_set_interrupt;
1438 }
1439
1440 err = i40evf_alloc_q_vectors(adapter);
1441 if (err) {
1442 dev_err(&adapter->pdev->dev,
1443 "Unable to allocate memory for queue vectors\n");
1444 goto err_alloc_q_vectors;
1445 }
1446
1447 err = i40evf_alloc_queues(adapter);
1448 if (err) {
1449 dev_err(&adapter->pdev->dev,
1450 "Unable to allocate memory for queues\n");
1451 goto err_alloc_queues;
1452 }
1453
1454 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001455 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1456 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001457
1458 return 0;
1459err_alloc_queues:
1460 i40evf_free_q_vectors(adapter);
1461err_alloc_q_vectors:
1462 i40evf_reset_interrupt_capability(adapter);
1463err_set_interrupt:
1464 return err;
1465}
1466
1467/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001468 * i40evf_free_rss - Free memory used by RSS structs
1469 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001470 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001471static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001472{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001473 kfree(adapter->rss_key);
1474 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001475
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001476 kfree(adapter->rss_lut);
1477 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001478}
1479
1480/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001481 * i40evf_watchdog_timer - Periodic call-back timer
1482 * @data: pointer to adapter disguised as unsigned long
1483 **/
1484static void i40evf_watchdog_timer(unsigned long data)
1485{
1486 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001487
Greg Rose5eae00c2013-12-21 06:12:45 +00001488 schedule_work(&adapter->watchdog_task);
1489 /* timer will be rescheduled in watchdog task */
1490}
1491
1492/**
1493 * i40evf_watchdog_task - Periodic call-back task
1494 * @work: pointer to work_struct
1495 **/
1496static void i40evf_watchdog_task(struct work_struct *work)
1497{
1498 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001499 struct i40evf_adapter,
1500 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001501 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001502 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001503
Greg Rose5eae00c2013-12-21 06:12:45 +00001504 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001505 goto restart_watchdog;
1506
1507 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001508 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1509 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1510 if ((reg_val == I40E_VFR_VFACTIVE) ||
1511 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001512 /* A chance for redemption! */
1513 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1514 adapter->state = __I40EVF_STARTUP;
1515 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1516 schedule_delayed_work(&adapter->init_task, 10);
1517 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1518 &adapter->crit_section);
1519 /* Don't reschedule the watchdog, since we've restarted
1520 * the init task. When init_task contacts the PF and
1521 * gets everything set up again, it'll restart the
1522 * watchdog for us. Down, boy. Sit. Stay. Woof.
1523 */
1524 return;
1525 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001526 adapter->aq_required = 0;
1527 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1528 goto watchdog_done;
1529 }
1530
1531 if ((adapter->state < __I40EVF_DOWN) ||
1532 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001533 goto watchdog_done;
1534
Mitch Williamsef8693e2014-02-13 03:48:53 -08001535 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001536 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1537 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001538 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001539 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001540 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001541 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001542 adapter->aq_required = 0;
1543 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001544 goto watchdog_done;
1545 }
1546
1547 /* Process admin queue tasks. After init, everything gets done
1548 * here so we don't race on the admin queue.
1549 */
Mitch Williamsed636962015-04-07 19:45:32 -04001550 if (adapter->current_op) {
Mitch A Williams0758e7c2014-12-09 08:53:08 +00001551 if (!i40evf_asq_done(hw)) {
1552 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1553 i40evf_send_api_ver(adapter);
1554 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001555 goto watchdog_done;
Mitch A Williams0758e7c2014-12-09 08:53:08 +00001556 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001557 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1558 i40evf_send_vf_config_msg(adapter);
1559 goto watchdog_done;
1560 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001561
Mitch Williamse284fc82015-03-27 00:12:09 -07001562 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1563 i40evf_disable_queues(adapter);
1564 goto watchdog_done;
1565 }
1566
Greg Rose5eae00c2013-12-21 06:12:45 +00001567 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1568 i40evf_map_queues(adapter);
1569 goto watchdog_done;
1570 }
1571
1572 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1573 i40evf_add_ether_addrs(adapter);
1574 goto watchdog_done;
1575 }
1576
1577 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1578 i40evf_add_vlans(adapter);
1579 goto watchdog_done;
1580 }
1581
1582 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1583 i40evf_del_ether_addrs(adapter);
1584 goto watchdog_done;
1585 }
1586
1587 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1588 i40evf_del_vlans(adapter);
1589 goto watchdog_done;
1590 }
1591
Greg Rose5eae00c2013-12-21 06:12:45 +00001592 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1593 i40evf_configure_queues(adapter);
1594 goto watchdog_done;
1595 }
1596
1597 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1598 i40evf_enable_queues(adapter);
1599 goto watchdog_done;
1600 }
1601
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001602 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1603 /* This message goes straight to the firmware, not the
1604 * PF, so we don't have to set current_op as we will
1605 * not get a response through the ARQ.
1606 */
Helin Zhang96a81982015-10-26 19:44:31 -04001607 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001608 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1609 goto watchdog_done;
1610 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001611 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1612 i40evf_get_hena(adapter);
1613 goto watchdog_done;
1614 }
1615 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1616 i40evf_set_hena(adapter);
1617 goto watchdog_done;
1618 }
1619 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1620 i40evf_set_rss_key(adapter);
1621 goto watchdog_done;
1622 }
1623 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1624 i40evf_set_rss_lut(adapter);
1625 goto watchdog_done;
1626 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001627
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001628 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1629 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_UNICAST_PROMISC |
1630 I40E_FLAG_VF_MULTICAST_PROMISC);
1631 goto watchdog_done;
1632 }
1633
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001634 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1635 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_MULTICAST_PROMISC);
1636 goto watchdog_done;
1637 }
1638
1639 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1640 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001641 i40evf_set_promiscuous(adapter, 0);
1642 goto watchdog_done;
1643 }
1644
Greg Rose5eae00c2013-12-21 06:12:45 +00001645 if (adapter->state == __I40EVF_RUNNING)
1646 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001647watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001648 if (adapter->state == __I40EVF_RUNNING) {
1649 i40evf_irq_enable_queues(adapter, ~0);
1650 i40evf_fire_sw_int(adapter, 0xFF);
1651 } else {
1652 i40evf_fire_sw_int(adapter, 0x1);
1653 }
1654
Mitch Williamsef8693e2014-02-13 03:48:53 -08001655 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1656restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001657 if (adapter->state == __I40EVF_REMOVE)
1658 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001659 if (adapter->aq_required)
1660 mod_timer(&adapter->watchdog_timer,
1661 jiffies + msecs_to_jiffies(20));
1662 else
1663 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001664 schedule_work(&adapter->adminq_task);
1665}
1666
Mitch Williams67c818a2015-06-19 08:56:30 -07001667#define I40EVF_RESET_WAIT_MS 10
1668#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001669/**
1670 * i40evf_reset_task - Call-back task to handle hardware reset
1671 * @work: pointer to work_struct
1672 *
1673 * During reset we need to shut down and reinitialize the admin queue
1674 * before we can use it to communicate with the PF again. We also clear
1675 * and reinit the rings because that context is lost as well.
1676 **/
1677static void i40evf_reset_task(struct work_struct *work)
1678{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001679 struct i40evf_adapter *adapter = container_of(work,
1680 struct i40evf_adapter,
1681 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001682 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001683 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001684 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001685 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001686 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001687 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001688
1689 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1690 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001691 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001692
Mitch Williams67c818a2015-06-19 08:56:30 -07001693 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001694 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001695 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1696 /* Restart the AQ here. If we have been reset but didn't
1697 * detect it, or if the PF had to reinit, our AQ will be hosed.
1698 */
1699 i40evf_shutdown_adminq(hw);
1700 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001701 i40evf_request_reset(adapter);
1702 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001703 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001704
Mitch Williamsef8693e2014-02-13 03:48:53 -08001705 /* poll until we see the reset actually happen */
1706 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001707 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1708 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1709 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001710 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001711 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001712 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001713 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001714 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001715 goto continue_reset; /* act like the reset happened */
1716 }
1717
1718 /* wait until the reset is complete and the PF is responding to us */
1719 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001720 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1721 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1722 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001723 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001724 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001725 }
Mitch Williams509a4472015-12-23 12:05:52 -08001726 pci_set_master(adapter->pdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001727 /* extra wait to make sure minimum wait is met */
1728 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001729 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001730 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001731 struct i40evf_vlan_filter *fv, *fvtmp;
1732
Greg Rose5eae00c2013-12-21 06:12:45 +00001733 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001734 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001735 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001736 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1737
Mitch Williams169f4072014-04-01 07:11:50 +00001738 if (netif_running(adapter->netdev)) {
1739 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001740 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001741 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001742 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001743 i40evf_napi_disable_all(adapter);
1744 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001745 i40evf_free_traffic_irqs(adapter);
1746 i40evf_free_all_tx_resources(adapter);
1747 i40evf_free_all_rx_resources(adapter);
1748 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001749
1750 /* Delete all of the filters, both MAC and VLAN. */
1751 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1752 list) {
1753 list_del(&f->list);
1754 kfree(f);
1755 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001756
Mitch Williams6ba36a22014-08-01 13:27:14 -07001757 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1758 list) {
1759 list_del(&fv->list);
1760 kfree(fv);
1761 }
1762
Mitch Williamsef8693e2014-02-13 03:48:53 -08001763 i40evf_free_misc_irq(adapter);
1764 i40evf_reset_interrupt_capability(adapter);
1765 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001766 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001767 kfree(adapter->vf_res);
1768 i40evf_shutdown_adminq(hw);
1769 adapter->netdev->flags &= ~IFF_UP;
1770 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001771 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Mitch Williams9b9344f2016-01-15 14:33:19 -08001772 adapter->state = __I40EVF_DOWN;
Mitch Williams67c818a2015-06-19 08:56:30 -07001773 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001774 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001775 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001776
1777continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001778 if (netif_running(adapter->netdev)) {
1779 netif_carrier_off(netdev);
1780 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001781 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001782 i40evf_napi_disable_all(adapter);
1783 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001784 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001785
Greg Rose5eae00c2013-12-21 06:12:45 +00001786 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001787 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1788
1789 /* free the Tx/Rx rings and descriptors, might be better to just
1790 * re-use them sometime in the future
1791 */
1792 i40evf_free_all_rx_resources(adapter);
1793 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001794
1795 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001796 i40evf_shutdown_adminq(hw);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001797 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001798 err = i40evf_init_adminq(hw);
1799 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001800 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1801 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001802
Mitch Williamse6d038d2015-06-04 16:23:58 -04001803 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1804 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001805
1806 /* re-add all MAC filters */
1807 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1808 f->add = true;
1809 }
1810 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001811 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1812 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001813 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001814 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001815 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Avinash Dayanandbf3a1782016-08-17 16:04:08 -07001816 /* Open RDMA Client again */
1817 adapter->aq_required |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
Greg Rose5eae00c2013-12-21 06:12:45 +00001818 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001819 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001820
1821 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1822
1823 if (netif_running(adapter->netdev)) {
1824 /* allocate transmit descriptors */
1825 err = i40evf_setup_all_tx_resources(adapter);
1826 if (err)
1827 goto reset_err;
1828
1829 /* allocate receive descriptors */
1830 err = i40evf_setup_all_rx_resources(adapter);
1831 if (err)
1832 goto reset_err;
1833
1834 i40evf_configure(adapter);
1835
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001836 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001837
1838 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001839 } else {
1840 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001841 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001842
Greg Rose5eae00c2013-12-21 06:12:45 +00001843 return;
1844reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001845 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001846 i40evf_close(adapter->netdev);
1847}
1848
1849/**
1850 * i40evf_adminq_task - worker thread to clean the admin queue
1851 * @work: pointer to work_struct containing our data
1852 **/
1853static void i40evf_adminq_task(struct work_struct *work)
1854{
1855 struct i40evf_adapter *adapter =
1856 container_of(work, struct i40evf_adapter, adminq_task);
1857 struct i40e_hw *hw = &adapter->hw;
1858 struct i40e_arq_event_info event;
1859 struct i40e_virtchnl_msg *v_msg;
1860 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001861 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001862 u16 pending;
1863
Mitch Williamsef8693e2014-02-13 03:48:53 -08001864 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001865 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001866
Mitch Williams1001dc32014-11-11 20:02:19 +00001867 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1868 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001869 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001870 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001871
Greg Rose5eae00c2013-12-21 06:12:45 +00001872 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1873 do {
1874 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001875 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001876 break; /* No event to process or error cleaning ARQ */
1877
1878 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1879 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001880 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001881 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001882 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001883 } while (pending);
1884
Mitch Williams67c818a2015-06-19 08:56:30 -07001885 if ((adapter->flags &
1886 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1887 adapter->state == __I40EVF_RESETTING)
1888 goto freedom;
1889
Mitch Williams912257e2014-05-22 06:32:07 +00001890 /* check for error indications */
1891 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001892 if (val == 0xdeadbeef) /* indicates device in reset */
1893 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001894 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001895 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001896 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001897 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001898 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001899 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001900 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001901 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001902 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001903 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001904 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001905 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001906 }
1907 if (oldval != val)
1908 wr32(hw, hw->aq.arq.len, val);
1909
1910 val = rd32(hw, hw->aq.asq.len);
1911 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001912 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001913 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001914 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001915 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001916 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001917 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001918 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001919 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001920 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001921 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001922 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001923 }
1924 if (oldval != val)
1925 wr32(hw, hw->aq.asq.len, val);
1926
Mitch Williams67c818a2015-06-19 08:56:30 -07001927freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001928 kfree(event.msg_buf);
1929out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001930 /* re-enable Admin queue interrupt cause */
1931 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001932}
1933
1934/**
1935 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1936 * @adapter: board private structure
1937 *
1938 * Free all transmit software resources
1939 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001940void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001941{
1942 int i;
1943
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08001944 if (!adapter->tx_rings)
1945 return;
1946
Mitch Williamscc052922014-10-25 03:24:34 +00001947 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04001948 if (adapter->tx_rings[i].desc)
1949 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001950}
1951
1952/**
1953 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1954 * @adapter: board private structure
1955 *
1956 * If this function returns with an error, then it's possible one or
1957 * more of the rings is populated (while the rest are not). It is the
1958 * callers duty to clean those orphaned rings.
1959 *
1960 * Return 0 on success, negative on failure
1961 **/
1962static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1963{
1964 int i, err = 0;
1965
Mitch Williamscc052922014-10-25 03:24:34 +00001966 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001967 adapter->tx_rings[i].count = adapter->tx_desc_count;
1968 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001969 if (!err)
1970 continue;
1971 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001972 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001973 break;
1974 }
1975
1976 return err;
1977}
1978
1979/**
1980 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1981 * @adapter: board private structure
1982 *
1983 * If this function returns with an error, then it's possible one or
1984 * more of the rings is populated (while the rest are not). It is the
1985 * callers duty to clean those orphaned rings.
1986 *
1987 * Return 0 on success, negative on failure
1988 **/
1989static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1990{
1991 int i, err = 0;
1992
Mitch Williamscc052922014-10-25 03:24:34 +00001993 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001994 adapter->rx_rings[i].count = adapter->rx_desc_count;
1995 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001996 if (!err)
1997 continue;
1998 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001999 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002000 break;
2001 }
2002 return err;
2003}
2004
2005/**
2006 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2007 * @adapter: board private structure
2008 *
2009 * Free all receive software resources
2010 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002011void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002012{
2013 int i;
2014
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002015 if (!adapter->rx_rings)
2016 return;
2017
Mitch Williamscc052922014-10-25 03:24:34 +00002018 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002019 if (adapter->rx_rings[i].desc)
2020 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002021}
2022
2023/**
2024 * i40evf_open - Called when a network interface is made active
2025 * @netdev: network interface device structure
2026 *
2027 * Returns 0 on success, negative value on failure
2028 *
2029 * The open entry point is called when a network interface is made
2030 * active by the system (IFF_UP). At this point all resources needed
2031 * for transmit and receive operations are allocated, the interrupt
2032 * handler is registered with the OS, the watchdog timer is started,
2033 * and the stack is notified that the interface is ready.
2034 **/
2035static int i40evf_open(struct net_device *netdev)
2036{
2037 struct i40evf_adapter *adapter = netdev_priv(netdev);
2038 int err;
2039
Mitch Williamsef8693e2014-02-13 03:48:53 -08002040 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2041 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2042 return -EIO;
2043 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002044
2045 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002046 return -EBUSY;
2047
2048 /* allocate transmit descriptors */
2049 err = i40evf_setup_all_tx_resources(adapter);
2050 if (err)
2051 goto err_setup_tx;
2052
2053 /* allocate receive descriptors */
2054 err = i40evf_setup_all_rx_resources(adapter);
2055 if (err)
2056 goto err_setup_rx;
2057
2058 /* clear any pending interrupts, may auto mask */
2059 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2060 if (err)
2061 goto err_req_irq;
2062
Mitch Williams44151cd2015-04-27 14:57:17 -04002063 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002064 i40evf_configure(adapter);
2065
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002066 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002067
2068 i40evf_irq_enable(adapter, true);
2069
2070 return 0;
2071
2072err_req_irq:
2073 i40evf_down(adapter);
2074 i40evf_free_traffic_irqs(adapter);
2075err_setup_rx:
2076 i40evf_free_all_rx_resources(adapter);
2077err_setup_tx:
2078 i40evf_free_all_tx_resources(adapter);
2079
2080 return err;
2081}
2082
2083/**
2084 * i40evf_close - Disables a network interface
2085 * @netdev: network interface device structure
2086 *
2087 * Returns 0, this is not allowed to fail
2088 *
2089 * The close entry point is called when an interface is de-activated
2090 * by the OS. The hardware is still under the drivers control, but
2091 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2092 * are freed, along with all transmit and receive resources.
2093 **/
2094static int i40evf_close(struct net_device *netdev)
2095{
2096 struct i40evf_adapter *adapter = netdev_priv(netdev);
2097
Mitch Williams209dc4d2015-12-09 15:50:27 -08002098 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002099 return 0;
2100
Mitch Williamsef8693e2014-02-13 03:48:53 -08002101
Greg Rose5eae00c2013-12-21 06:12:45 +00002102 set_bit(__I40E_DOWN, &adapter->vsi.state);
2103
2104 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002105 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002106 i40evf_free_traffic_irqs(adapter);
2107
Greg Rose5eae00c2013-12-21 06:12:45 +00002108 return 0;
2109}
2110
2111/**
2112 * i40evf_get_stats - Get System Network Statistics
2113 * @netdev: network interface device structure
2114 *
2115 * Returns the address of the device statistics structure.
2116 * The statistics are actually updated from the timer callback.
2117 **/
2118static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2119{
2120 struct i40evf_adapter *adapter = netdev_priv(netdev);
2121
2122 /* only return the current stats */
2123 return &adapter->net_stats;
2124}
2125
2126/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002127 * i40evf_change_mtu - Change the Maximum Transfer Unit
2128 * @netdev: network interface device structure
2129 * @new_mtu: new value for maximum frame size
2130 *
2131 * Returns 0 on success, negative on failure
2132 **/
2133static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2134{
2135 struct i40evf_adapter *adapter = netdev_priv(netdev);
2136 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2137
2138 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2139 return -EINVAL;
2140
Greg Rose5eae00c2013-12-21 06:12:45 +00002141 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002142 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2143 schedule_work(&adapter->reset_task);
2144
Greg Rose5eae00c2013-12-21 06:12:45 +00002145 return 0;
2146}
2147
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002148#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2149 NETIF_F_HW_VLAN_CTAG_RX |\
2150 NETIF_F_HW_VLAN_CTAG_FILTER)
2151
2152/**
2153 * i40evf_fix_features - fix up the netdev feature bits
2154 * @netdev: our net device
2155 * @features: desired feature bits
2156 *
2157 * Returns fixed-up features bits
2158 **/
2159static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2160 netdev_features_t features)
2161{
2162 struct i40evf_adapter *adapter = netdev_priv(netdev);
2163
2164 features &= ~I40EVF_VLAN_FEATURES;
2165 if (adapter->vf_res->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN)
2166 features |= I40EVF_VLAN_FEATURES;
2167 return features;
2168}
2169
Greg Rose5eae00c2013-12-21 06:12:45 +00002170static const struct net_device_ops i40evf_netdev_ops = {
2171 .ndo_open = i40evf_open,
2172 .ndo_stop = i40evf_close,
2173 .ndo_start_xmit = i40evf_xmit_frame,
2174 .ndo_get_stats = i40evf_get_stats,
2175 .ndo_set_rx_mode = i40evf_set_rx_mode,
2176 .ndo_validate_addr = eth_validate_addr,
2177 .ndo_set_mac_address = i40evf_set_mac,
2178 .ndo_change_mtu = i40evf_change_mtu,
2179 .ndo_tx_timeout = i40evf_tx_timeout,
2180 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2181 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002182 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002183#ifdef CONFIG_NET_POLL_CONTROLLER
2184 .ndo_poll_controller = i40evf_netpoll,
2185#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002186};
2187
2188/**
2189 * i40evf_check_reset_complete - check that VF reset is complete
2190 * @hw: pointer to hw struct
2191 *
2192 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2193 **/
2194static int i40evf_check_reset_complete(struct i40e_hw *hw)
2195{
2196 u32 rstat;
2197 int i;
2198
2199 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002200 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2201 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2202 if ((rstat == I40E_VFR_VFACTIVE) ||
2203 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002204 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002205 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002206 }
2207 return -EBUSY;
2208}
2209
2210/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002211 * i40evf_process_config - Process the config information we got from the PF
2212 * @adapter: board private structure
2213 *
2214 * Verify that we have a valid config struct, and set up our netdev features
2215 * and our VSI struct.
2216 **/
2217int i40evf_process_config(struct i40evf_adapter *adapter)
2218{
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002219 struct i40e_virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002220 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002221 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002222 int i;
2223
2224 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002225 for (i = 0; i < vfres->num_vsis; i++) {
2226 if (vfres->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2227 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002228 }
2229 if (!adapter->vsi_res) {
2230 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2231 return -ENODEV;
2232 }
2233
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002234 netdev->hw_enc_features |= NETIF_F_SG |
2235 NETIF_F_IP_CSUM |
2236 NETIF_F_IPV6_CSUM |
2237 NETIF_F_HIGHDMA |
2238 NETIF_F_SOFT_FEATURES |
2239 NETIF_F_TSO |
2240 NETIF_F_TSO_ECN |
2241 NETIF_F_TSO6 |
2242 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002243 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002244 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002245 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002246 NETIF_F_GSO_UDP_TUNNEL |
2247 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002248 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002249 NETIF_F_SCTP_CRC |
2250 NETIF_F_RXHASH |
2251 NETIF_F_RXCSUM |
2252 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002253
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002254 if (!(adapter->flags & I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002255 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2256
2257 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002258
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002259 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002260 netdev->vlan_features |= netdev->hw_enc_features |
2261 NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002262
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002263 /* Write features and hw_features separately to avoid polluting
2264 * with, or dropping, features that are set when we registgered.
2265 */
2266 netdev->hw_features |= netdev->hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002267
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002268 netdev->features |= netdev->hw_enc_features | I40EVF_VLAN_FEATURES;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002269 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002270
2271 /* disable VLAN features if not supported */
2272 if (!(vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN))
2273 netdev->features ^= I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002274
2275 adapter->vsi.id = adapter->vsi_res->vsi_id;
2276
2277 adapter->vsi.back = adapter;
2278 adapter->vsi.base_vector = 1;
2279 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002280 vsi->netdev = adapter->netdev;
2281 vsi->qs_handle = adapter->vsi_res->qset_handle;
2282 if (vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2283 adapter->rss_key_size = vfres->rss_key_size;
2284 adapter->rss_lut_size = vfres->rss_lut_size;
2285 } else {
2286 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2287 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2288 }
2289
Mitch Williamse6d038d2015-06-04 16:23:58 -04002290 return 0;
2291}
2292
2293/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002294 * i40evf_init_task - worker thread to perform delayed initialization
2295 * @work: pointer to work_struct containing our data
2296 *
2297 * This task completes the work that was begun in probe. Due to the nature
2298 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002299 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002300 * whole system, we'll do it in a task so we can sleep.
2301 * This task only runs during driver init. Once we've established
2302 * communications with the PF driver and set up our netdev, the watchdog
2303 * takes over.
2304 **/
2305static void i40evf_init_task(struct work_struct *work)
2306{
2307 struct i40evf_adapter *adapter = container_of(work,
2308 struct i40evf_adapter,
2309 init_task.work);
2310 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002311 struct i40e_hw *hw = &adapter->hw;
2312 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002313 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002314
2315 switch (adapter->state) {
2316 case __I40EVF_STARTUP:
2317 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002318 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2319 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002320 err = i40e_set_mac_type(hw);
2321 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002322 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2323 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002324 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002325 }
2326 err = i40evf_check_reset_complete(hw);
2327 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002328 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002329 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002330 goto err;
2331 }
2332 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2333 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2334 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2335 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2336
2337 err = i40evf_init_adminq(hw);
2338 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002339 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2340 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002341 goto err;
2342 }
2343 err = i40evf_send_api_ver(adapter);
2344 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002345 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002346 i40evf_shutdown_adminq(hw);
2347 goto err;
2348 }
2349 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2350 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002351 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002352 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002353 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002354 i40evf_shutdown_adminq(hw);
2355 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002356 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002357 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002358
2359 /* aq msg sent, awaiting reply */
2360 err = i40evf_verify_api_ver(adapter);
2361 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002362 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002363 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002364 else
2365 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2366 adapter->pf_version.major,
2367 adapter->pf_version.minor,
2368 I40E_VIRTCHNL_VERSION_MAJOR,
2369 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002370 goto err;
2371 }
2372 err = i40evf_send_vf_config_msg(adapter);
2373 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002374 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002375 err);
2376 goto err;
2377 }
2378 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2379 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002380 case __I40EVF_INIT_GET_RESOURCES:
2381 /* aq msg sent, awaiting reply */
2382 if (!adapter->vf_res) {
2383 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2384 (I40E_MAX_VF_VSI *
2385 sizeof(struct i40e_virtchnl_vsi_resource));
2386 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002387 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002388 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002389 }
2390 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002391 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002392 err = i40evf_send_vf_config_msg(adapter);
2393 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002394 } else if (err == I40E_ERR_PARAM) {
2395 /* We only get ERR_PARAM if the device is in a very bad
2396 * state or if we've been disabled for previous bad
2397 * behavior. Either way, we're done now.
2398 */
2399 i40evf_shutdown_adminq(hw);
2400 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2401 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002402 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002403 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002404 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2405 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002406 goto err_alloc;
2407 }
2408 adapter->state = __I40EVF_INIT_SW;
2409 break;
2410 default:
2411 goto err_alloc;
2412 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002413
2414 if (hw->mac.type == I40E_MAC_X722_VF)
2415 adapter->flags |= I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE;
2416
Mitch Williamse6d038d2015-06-04 16:23:58 -04002417 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002418 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002419 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002420
2421 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2422
Greg Rose5eae00c2013-12-21 06:12:45 +00002423 netdev->netdev_ops = &i40evf_netdev_ops;
2424 i40evf_set_ethtool_ops(netdev);
2425 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002426
Greg Rose5eae00c2013-12-21 06:12:45 +00002427 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002428 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002429 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002430 eth_hw_addr_random(netdev);
2431 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2432 } else {
2433 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2434 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2435 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002436 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002437
Greg Rose5eae00c2013-12-21 06:12:45 +00002438 init_timer(&adapter->watchdog_timer);
2439 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2440 adapter->watchdog_timer.data = (unsigned long)adapter;
2441 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2442
Mitch Williamscc052922014-10-25 03:24:34 +00002443 adapter->num_active_queues = min_t(int,
2444 adapter->vsi_res->num_queue_pairs,
2445 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002446 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2447 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002448 err = i40evf_init_interrupt_scheme(adapter);
2449 if (err)
2450 goto err_sw_init;
2451 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002452 if (adapter->vf_res->vf_offload_flags &
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002453 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2454 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2455
Greg Rose5eae00c2013-12-21 06:12:45 +00002456 err = i40evf_request_misc_irq(adapter);
2457 if (err)
2458 goto err_sw_init;
2459
2460 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002461 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002462
Mitch Williamsef8693e2014-02-13 03:48:53 -08002463 if (!adapter->netdev_registered) {
2464 err = register_netdev(netdev);
2465 if (err)
2466 goto err_register;
2467 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002468
2469 adapter->netdev_registered = true;
2470
2471 netif_tx_stop_all_queues(netdev);
2472
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002473 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002474 if (netdev->features & NETIF_F_GRO)
2475 dev_info(&pdev->dev, "GRO is enabled\n");
2476
Greg Rose5eae00c2013-12-21 06:12:45 +00002477 adapter->state = __I40EVF_DOWN;
2478 set_bit(__I40E_DOWN, &adapter->vsi.state);
2479 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04002480
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002481 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2482 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2483 if (!adapter->rss_key || !adapter->rss_lut)
2484 goto err_mem;
2485
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04002486 if (RSS_AQ(adapter)) {
2487 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2488 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2489 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002490 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04002491 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002492 return;
2493restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002494 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002495 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002496err_mem:
2497 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002498err_register:
2499 i40evf_free_misc_irq(adapter);
2500err_sw_init:
2501 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002502err_alloc:
2503 kfree(adapter->vf_res);
2504 adapter->vf_res = NULL;
2505err:
2506 /* Things went into the weeds, so try again later */
2507 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002508 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002509 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002510 i40evf_shutdown_adminq(hw);
2511 adapter->state = __I40EVF_STARTUP;
2512 schedule_delayed_work(&adapter->init_task, HZ * 5);
2513 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002514 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002515 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002516}
2517
2518/**
2519 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2520 * @pdev: pci device structure
2521 **/
2522static void i40evf_shutdown(struct pci_dev *pdev)
2523{
2524 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002525 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002526
2527 netif_device_detach(netdev);
2528
2529 if (netif_running(netdev))
2530 i40evf_close(netdev);
2531
Mitch Williams00293fd2015-01-09 11:18:18 +00002532 /* Prevent the watchdog from running. */
2533 adapter->state = __I40EVF_REMOVE;
2534 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002535
Greg Rose5eae00c2013-12-21 06:12:45 +00002536#ifdef CONFIG_PM
2537 pci_save_state(pdev);
2538
2539#endif
2540 pci_disable_device(pdev);
2541}
2542
2543/**
2544 * i40evf_probe - Device Initialization Routine
2545 * @pdev: PCI device information struct
2546 * @ent: entry in i40evf_pci_tbl
2547 *
2548 * Returns 0 on success, negative on failure
2549 *
2550 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2551 * The OS initialization, configuring of the adapter private structure,
2552 * and a hardware reset occur.
2553 **/
2554static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2555{
2556 struct net_device *netdev;
2557 struct i40evf_adapter *adapter = NULL;
2558 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002559 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002560
2561 err = pci_enable_device(pdev);
2562 if (err)
2563 return err;
2564
Mitch Williams64942942014-02-11 08:26:33 +00002565 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002566 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002567 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2568 if (err) {
2569 dev_err(&pdev->dev,
2570 "DMA configuration failed: 0x%x\n", err);
2571 goto err_dma;
2572 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002573 }
2574
2575 err = pci_request_regions(pdev, i40evf_driver_name);
2576 if (err) {
2577 dev_err(&pdev->dev,
2578 "pci_request_regions failed 0x%x\n", err);
2579 goto err_pci_reg;
2580 }
2581
2582 pci_enable_pcie_error_reporting(pdev);
2583
2584 pci_set_master(pdev);
2585
Mitch Williams1255b7a2015-11-06 15:25:59 -08002586 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002587 if (!netdev) {
2588 err = -ENOMEM;
2589 goto err_alloc_etherdev;
2590 }
2591
2592 SET_NETDEV_DEV(netdev, &pdev->dev);
2593
2594 pci_set_drvdata(pdev, netdev);
2595 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002596
2597 adapter->netdev = netdev;
2598 adapter->pdev = pdev;
2599
2600 hw = &adapter->hw;
2601 hw->back = adapter;
2602
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002603 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002604 adapter->state = __I40EVF_STARTUP;
2605
2606 /* Call save state here because it relies on the adapter struct. */
2607 pci_save_state(pdev);
2608
2609 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2610 pci_resource_len(pdev, 0));
2611 if (!hw->hw_addr) {
2612 err = -EIO;
2613 goto err_ioremap;
2614 }
2615 hw->vendor_id = pdev->vendor;
2616 hw->device_id = pdev->device;
2617 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2618 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2619 hw->subsystem_device_id = pdev->subsystem_device;
2620 hw->bus.device = PCI_SLOT(pdev->devfn);
2621 hw->bus.func = PCI_FUNC(pdev->devfn);
2622
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002623 /* set up the locks for the AQ, do this only once in probe
2624 * and destroy them only once in remove
2625 */
2626 mutex_init(&hw->aq.asq_mutex);
2627 mutex_init(&hw->aq.arq_mutex);
2628
Serey Kong8bb1a542014-08-01 13:27:15 -07002629 INIT_LIST_HEAD(&adapter->mac_filter_list);
2630 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2631
Greg Rose5eae00c2013-12-21 06:12:45 +00002632 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2633 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2634 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2635 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002636 schedule_delayed_work(&adapter->init_task,
2637 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002638
2639 return 0;
2640
2641err_ioremap:
2642 free_netdev(netdev);
2643err_alloc_etherdev:
2644 pci_release_regions(pdev);
2645err_pci_reg:
2646err_dma:
2647 pci_disable_device(pdev);
2648 return err;
2649}
2650
2651#ifdef CONFIG_PM
2652/**
2653 * i40evf_suspend - Power management suspend routine
2654 * @pdev: PCI device information struct
2655 * @state: unused
2656 *
2657 * Called when the system (VM) is entering sleep/suspend.
2658 **/
2659static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2660{
2661 struct net_device *netdev = pci_get_drvdata(pdev);
2662 struct i40evf_adapter *adapter = netdev_priv(netdev);
2663 int retval = 0;
2664
2665 netif_device_detach(netdev);
2666
2667 if (netif_running(netdev)) {
2668 rtnl_lock();
2669 i40evf_down(adapter);
2670 rtnl_unlock();
2671 }
2672 i40evf_free_misc_irq(adapter);
2673 i40evf_reset_interrupt_capability(adapter);
2674
2675 retval = pci_save_state(pdev);
2676 if (retval)
2677 return retval;
2678
2679 pci_disable_device(pdev);
2680
2681 return 0;
2682}
2683
2684/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002685 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002686 * @pdev: PCI device information struct
2687 *
2688 * Called when the system (VM) is resumed from sleep/suspend.
2689 **/
2690static int i40evf_resume(struct pci_dev *pdev)
2691{
2692 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2693 struct net_device *netdev = adapter->netdev;
2694 u32 err;
2695
2696 pci_set_power_state(pdev, PCI_D0);
2697 pci_restore_state(pdev);
2698 /* pci_restore_state clears dev->state_saved so call
2699 * pci_save_state to restore it.
2700 */
2701 pci_save_state(pdev);
2702
2703 err = pci_enable_device_mem(pdev);
2704 if (err) {
2705 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2706 return err;
2707 }
2708 pci_set_master(pdev);
2709
2710 rtnl_lock();
2711 err = i40evf_set_interrupt_capability(adapter);
2712 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002713 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002714 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2715 return err;
2716 }
2717 err = i40evf_request_misc_irq(adapter);
2718 rtnl_unlock();
2719 if (err) {
2720 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2721 return err;
2722 }
2723
2724 schedule_work(&adapter->reset_task);
2725
2726 netif_device_attach(netdev);
2727
2728 return err;
2729}
2730
2731#endif /* CONFIG_PM */
2732/**
2733 * i40evf_remove - Device Removal Routine
2734 * @pdev: PCI device information struct
2735 *
2736 * i40evf_remove is called by the PCI subsystem to alert the driver
2737 * that it should release a PCI device. The could be caused by a
2738 * Hot-Plug event, or because the driver is going to be removed from
2739 * memory.
2740 **/
2741static void i40evf_remove(struct pci_dev *pdev)
2742{
2743 struct net_device *netdev = pci_get_drvdata(pdev);
2744 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002745 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002746 struct i40e_hw *hw = &adapter->hw;
2747
2748 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002749 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002750
2751 if (adapter->netdev_registered) {
2752 unregister_netdev(netdev);
2753 adapter->netdev_registered = false;
2754 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002755
Mitch Williamsf4a71882015-01-09 11:18:16 +00002756 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002757 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002758 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002759 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002760 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002761 /* If the FW isn't responding, kick it once, but only once. */
2762 if (!i40evf_asq_done(hw)) {
2763 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002764 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002765 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002766
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002767 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002768 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002769 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002770 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002771 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002772 }
2773
Mitch Williamse5d17c32014-06-04 04:22:38 +00002774 if (adapter->watchdog_timer.function)
2775 del_timer_sync(&adapter->watchdog_timer);
2776
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002777 flush_scheduled_work();
2778
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002779 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04002780
Greg Rose5eae00c2013-12-21 06:12:45 +00002781 if (hw->aq.asq.count)
2782 i40evf_shutdown_adminq(hw);
2783
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002784 /* destroy the locks only once, here */
2785 mutex_destroy(&hw->aq.arq_mutex);
2786 mutex_destroy(&hw->aq.asq_mutex);
2787
Greg Rose5eae00c2013-12-21 06:12:45 +00002788 iounmap(hw->hw_addr);
2789 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07002790 i40evf_free_all_tx_resources(adapter);
2791 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002792 i40evf_free_queues(adapter);
2793 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002794 /* If we got removed before an up/down sequence, we've got a filter
2795 * hanging out there that we need to get rid of.
2796 */
2797 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2798 list_del(&f->list);
2799 kfree(f);
2800 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002801 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2802 list_del(&f->list);
2803 kfree(f);
2804 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002805
2806 free_netdev(netdev);
2807
2808 pci_disable_pcie_error_reporting(pdev);
2809
2810 pci_disable_device(pdev);
2811}
2812
2813static struct pci_driver i40evf_driver = {
2814 .name = i40evf_driver_name,
2815 .id_table = i40evf_pci_tbl,
2816 .probe = i40evf_probe,
2817 .remove = i40evf_remove,
2818#ifdef CONFIG_PM
2819 .suspend = i40evf_suspend,
2820 .resume = i40evf_resume,
2821#endif
2822 .shutdown = i40evf_shutdown,
2823};
2824
2825/**
2826 * i40e_init_module - Driver Registration Routine
2827 *
2828 * i40e_init_module is the first routine called when the driver is
2829 * loaded. All it does is register with the PCI subsystem.
2830 **/
2831static int __init i40evf_init_module(void)
2832{
2833 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002834
Greg Rose5eae00c2013-12-21 06:12:45 +00002835 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002836 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002837
2838 pr_info("%s\n", i40evf_copyright);
2839
Jacob Keller6992a6c2016-08-04 11:37:01 -07002840 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
2841 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002842 if (!i40evf_wq) {
2843 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2844 return -ENOMEM;
2845 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002846 ret = pci_register_driver(&i40evf_driver);
2847 return ret;
2848}
2849
2850module_init(i40evf_init_module);
2851
2852/**
2853 * i40e_exit_module - Driver Exit Cleanup Routine
2854 *
2855 * i40e_exit_module is called just before the driver is removed
2856 * from memory.
2857 **/
2858static void __exit i40evf_exit_module(void)
2859{
2860 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002861 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00002862}
2863
2864module_exit(i40evf_exit_module);
2865
2866/* i40evf_main.c */