blob: 0d87191b6baca8984bc1c5b995dace8f8d9499cb [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"
Mitch Williamsed0e8942017-01-24 10:23:59 -080029#include "i40evf_client.h"
Scott Petersoned0980c2017-04-13 04:45:44 -040030/* All i40evf tracepoints are defined by the include below, which must
31 * be included exactly once across the whole kernel with
32 * CREATE_TRACE_POINTS defined
33 */
34#define CREATE_TRACE_POINTS
35#include "i40e_trace.h"
36
Greg Rose5eae00c2013-12-21 06:12:45 +000037static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39static int i40evf_close(struct net_device *netdev);
40
41char i40evf_driver_name[] = "i40evf";
42static const char i40evf_driver_string[] =
Catherine Sullivaneaab59e2016-01-13 16:51:44 -080043 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000044
Mitch Williams69ebe9552015-11-19 11:34:24 -080045#define DRV_KERN "-k"
46
Preethi Banalaabf709a2017-05-11 11:23:20 -070047#define DRV_VERSION_MAJOR 3
48#define DRV_VERSION_MINOR 0
49#define DRV_VERSION_BUILD 0
Mitch Williams69ebe9552015-11-19 11:34:24 -080050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) \
53 DRV_KERN
Greg Rose5eae00c2013-12-21 06:12:45 +000054const char i40evf_driver_version[] = DRV_VERSION;
55static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040056 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000057
58/* i40evf_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020066static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080067 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070068 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Preethi Banalaabf709a2017-05-11 11:23:20 -070070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000071 /* required last entry */
72 {0, }
73};
74
75MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
76
77MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(DRV_VERSION);
81
Jesse Brandeburg2803b162015-12-22 14:25:08 -080082static struct workqueue_struct *i40evf_wq;
83
Greg Rose5eae00c2013-12-21 06:12:45 +000084/**
85 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
86 * @hw: pointer to the HW structure
87 * @mem: ptr to mem struct to fill out
88 * @size: size of memory requested
89 * @alignment: what to align the allocation to
90 **/
91i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92 struct i40e_dma_mem *mem,
93 u64 size, u32 alignment)
94{
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
96
97 if (!mem)
98 return I40E_ERR_PARAM;
99
100 mem->size = ALIGN(size, alignment);
101 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
102 (dma_addr_t *)&mem->pa, GFP_KERNEL);
103 if (mem->va)
104 return 0;
105 else
106 return I40E_ERR_NO_MEMORY;
107}
108
109/**
110 * i40evf_free_dma_mem_d - OS specific memory free for shared code
111 * @hw: pointer to the HW structure
112 * @mem: ptr to mem struct to free
113 **/
114i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
115{
116 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
117
118 if (!mem || !mem->va)
119 return I40E_ERR_PARAM;
120 dma_free_coherent(&adapter->pdev->dev, mem->size,
121 mem->va, (dma_addr_t)mem->pa);
122 return 0;
123}
124
125/**
126 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to fill out
129 * @size: size of memory requested
130 **/
131i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem, u32 size)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 mem->size = size;
138 mem->va = kzalloc(size, GFP_KERNEL);
139
140 if (mem->va)
141 return 0;
142 else
143 return I40E_ERR_NO_MEMORY;
144}
145
146/**
147 * i40evf_free_virt_mem_d - OS specific memory free for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to free
150 **/
151i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152 struct i40e_virt_mem *mem)
153{
154 if (!mem)
155 return I40E_ERR_PARAM;
156
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159
160 return 0;
161}
162
163/**
164 * i40evf_debug_d - OS dependent version of debug printing
165 * @hw: pointer to the HW structure
166 * @mask: debug level mask
167 * @fmt_str: printf-type format description
168 **/
169void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
170{
171 char buf[512];
172 va_list argptr;
173
174 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
175 return;
176
177 va_start(argptr, fmt_str);
178 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
179 va_end(argptr);
180
181 /* the debug string is already formatted with a newline */
182 pr_info("%s", buf);
183}
184
185/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800186 * i40evf_schedule_reset - Set the flags and schedule a reset event
187 * @adapter: board private structure
188 **/
189void i40evf_schedule_reset(struct i40evf_adapter *adapter)
190{
191 if (!(adapter->flags &
192 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
193 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
194 schedule_work(&adapter->reset_task);
195 }
196}
197
198/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000199 * i40evf_tx_timeout - Respond to a Tx Hang
200 * @netdev: network interface device structure
201 **/
202static void i40evf_tx_timeout(struct net_device *netdev)
203{
204 struct i40evf_adapter *adapter = netdev_priv(netdev);
205
206 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800207 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000208}
209
210/**
211 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212 * @adapter: board private structure
213 **/
214static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
215{
216 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000217
Jacob Kelleref4603e2016-11-08 13:05:08 -0800218 if (!adapter->msix_entries)
219 return;
220
Greg Rose5eae00c2013-12-21 06:12:45 +0000221 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
222
223 /* read flush */
224 rd32(hw, I40E_VFGEN_RSTAT);
225
226 synchronize_irq(adapter->msix_entries[0].vector);
227}
228
229/**
230 * i40evf_misc_irq_enable - Enable default interrupt generation settings
231 * @adapter: board private structure
232 **/
233static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
234{
235 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000236
Greg Rose5eae00c2013-12-21 06:12:45 +0000237 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400239 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000240
241 /* read flush */
242 rd32(hw, I40E_VFGEN_RSTAT);
243}
244
245/**
246 * i40evf_irq_disable - Mask off interrupt generation on the NIC
247 * @adapter: board private structure
248 **/
249static void i40evf_irq_disable(struct i40evf_adapter *adapter)
250{
251 int i;
252 struct i40e_hw *hw = &adapter->hw;
253
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800254 if (!adapter->msix_entries)
255 return;
256
Greg Rose5eae00c2013-12-21 06:12:45 +0000257 for (i = 1; i < adapter->num_msix_vectors; i++) {
258 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
259 synchronize_irq(adapter->msix_entries[i].vector);
260 }
261 /* read flush */
262 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000263}
264
265/**
266 * i40evf_irq_enable_queues - Enable interrupt for specified queues
267 * @adapter: board private structure
268 * @mask: bitmap of queues to enable
269 **/
270void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
271{
272 struct i40e_hw *hw = &adapter->hw;
273 int i;
274
275 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400276 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000279 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400280 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000281 }
282 }
283}
284
285/**
286 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
287 * @adapter: board private structure
288 * @mask: bitmap of vectors to trigger
289 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000290static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000291{
292 struct i40e_hw *hw = &adapter->hw;
293 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800294 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000295
Mitch Williams164ec1b2014-06-04 08:45:19 +0000296 if (mask & 1) {
297 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400298 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000299 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400300 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000301 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
302 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000303 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400304 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000305 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400306 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000307 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400308 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000309 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
310 }
311 }
312}
313
314/**
315 * i40evf_irq_enable - Enable default interrupt generation settings
316 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600317 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000318 **/
319void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
320{
321 struct i40e_hw *hw = &adapter->hw;
322
Mitch Williams164ec1b2014-06-04 08:45:19 +0000323 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000324 i40evf_irq_enable_queues(adapter, ~0);
325
326 if (flush)
327 rd32(hw, I40E_VFGEN_RSTAT);
328}
329
330/**
331 * i40evf_msix_aq - Interrupt handler for vector 0
332 * @irq: interrupt number
333 * @data: pointer to netdev
334 **/
335static irqreturn_t i40evf_msix_aq(int irq, void *data)
336{
337 struct net_device *netdev = data;
338 struct i40evf_adapter *adapter = netdev_priv(netdev);
339 struct i40e_hw *hw = &adapter->hw;
340 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000341
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700342 /* handle non-queue interrupts, these reads clear the registers */
343 val = rd32(hw, I40E_VFINT_ICR01);
344 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000345
Jean Sacrened17f7e2015-10-13 01:06:30 -0600346 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
347 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000348 wr32(hw, I40E_VFINT_DYN_CTL01, val);
349
Greg Rose5eae00c2013-12-21 06:12:45 +0000350 /* schedule work on the private workqueue */
351 schedule_work(&adapter->adminq_task);
352
353 return IRQ_HANDLED;
354}
355
356/**
357 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
358 * @irq: interrupt number
359 * @data: pointer to a q_vector
360 **/
361static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
362{
363 struct i40e_q_vector *q_vector = data;
364
365 if (!q_vector->tx.ring && !q_vector->rx.ring)
366 return IRQ_HANDLED;
367
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700368 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000369
370 return IRQ_HANDLED;
371}
372
373/**
374 * i40evf_map_vector_to_rxq - associate irqs with rx queues
375 * @adapter: board private structure
376 * @v_idx: interrupt number
377 * @r_idx: queue number
378 **/
379static void
380i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
381{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400382 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400383 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700384 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000385
386 rx_ring->q_vector = q_vector;
387 rx_ring->next = q_vector->rx.ring;
388 rx_ring->vsi = &adapter->vsi;
389 q_vector->rx.ring = rx_ring;
390 q_vector->rx.count++;
391 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700392 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700393 q_vector->ring_mask |= BIT(r_idx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400394 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700395 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000396}
397
398/**
399 * i40evf_map_vector_to_txq - associate irqs with tx queues
400 * @adapter: board private structure
401 * @v_idx: interrupt number
402 * @t_idx: queue number
403 **/
404static void
405i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
406{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400407 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400408 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700409 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000410
411 tx_ring->q_vector = q_vector;
412 tx_ring->next = q_vector->tx.ring;
413 tx_ring->vsi = &adapter->vsi;
414 q_vector->tx.ring = tx_ring;
415 q_vector->tx.count++;
416 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700417 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400418 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000419 q_vector->num_ringpairs++;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700420 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000421}
422
423/**
424 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
425 * @adapter: board private structure to initialize
426 *
427 * This function maps descriptor rings to the queue-specific vectors
428 * we were allotted through the MSI-X enabling code. Ideally, we'd have
429 * one vector per ring/queue, but on a constrained vector budget, we
430 * group the rings as "efficiently" as possible. You would add new
431 * mapping configurations in here.
432 **/
433static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
434{
435 int q_vectors;
436 int v_start = 0;
437 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000438 int rxr_remaining = adapter->num_active_queues;
439 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000440 int i, j;
441 int rqpv, tqpv;
442 int err = 0;
443
444 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
445
446 /* The ideal configuration...
447 * We have enough vectors to map one per queue.
448 */
Mitch Williams973371d2015-04-27 14:57:09 -0400449 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000450 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
451 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
452
453 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
454 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
455 goto out;
456 }
457
458 /* If we don't have enough vectors for a 1-to-1
459 * mapping, we'll have to group them so there are
460 * multiple queues per vector.
461 * Re-adjusting *qpv takes care of the remainder.
462 */
463 for (i = v_start; i < q_vectors; i++) {
464 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
465 for (j = 0; j < rqpv; j++) {
466 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
467 rxr_idx++;
468 rxr_remaining--;
469 }
470 }
471 for (i = v_start; i < q_vectors; i++) {
472 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
473 for (j = 0; j < tqpv; j++) {
474 i40evf_map_vector_to_txq(adapter, i, txr_idx);
475 txr_idx++;
476 txr_remaining--;
477 }
478 }
479
480out:
481 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
482
483 return err;
484}
485
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700486#ifdef CONFIG_NET_POLL_CONTROLLER
487/**
488 * i40evf_netpoll - A Polling 'interrupt' handler
489 * @netdev: network interface device structure
490 *
491 * This is used by netconsole to send skbs without having to re-enable
492 * interrupts. It's not called while the normal interrupt routine is executing.
493 **/
494static void i40evf_netpoll(struct net_device *netdev)
495{
496 struct i40evf_adapter *adapter = netdev_priv(netdev);
497 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
498 int i;
499
500 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400501 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700502 return;
503
504 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400505 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700506}
507
508#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000509/**
Alan Brady96db7762016-09-14 16:24:38 -0700510 * i40evf_irq_affinity_notify - Callback for affinity changes
511 * @notify: context as to what irq was changed
512 * @mask: the new affinity mask
513 *
514 * This is a callback function used by the irq_set_affinity_notifier function
515 * so that we may register to receive changes to the irq affinity masks.
516 **/
517static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
518 const cpumask_t *mask)
519{
520 struct i40e_q_vector *q_vector =
521 container_of(notify, struct i40e_q_vector, affinity_notify);
522
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400523 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700524}
525
526/**
527 * i40evf_irq_affinity_release - Callback for affinity notifier release
528 * @ref: internal core kernel usage
529 *
530 * This is a callback function used by the irq_set_affinity_notifier function
531 * to inform the current notification subscriber that they will no longer
532 * receive notifications.
533 **/
534static void i40evf_irq_affinity_release(struct kref *ref) {}
535
536/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000537 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
538 * @adapter: board private structure
539 *
540 * Allocates MSI-X vectors for tx and rx handling, and requests
541 * interrupts from the kernel.
542 **/
543static int
544i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
545{
546 int vector, err, q_vectors;
547 int rx_int_idx = 0, tx_int_idx = 0;
Alan Brady96db7762016-09-14 16:24:38 -0700548 int irq_num;
Greg Rose5eae00c2013-12-21 06:12:45 +0000549
550 i40evf_irq_disable(adapter);
551 /* Decrement for Other and TCP Timer vectors */
552 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
553
554 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400555 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700556 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000557
558 if (q_vector->tx.ring && q_vector->rx.ring) {
559 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
560 "i40evf-%s-%s-%d", basename,
561 "TxRx", rx_int_idx++);
562 tx_int_idx++;
563 } else if (q_vector->rx.ring) {
564 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
565 "i40evf-%s-%s-%d", basename,
566 "rx", rx_int_idx++);
567 } else if (q_vector->tx.ring) {
568 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
569 "i40evf-%s-%s-%d", basename,
570 "tx", tx_int_idx++);
571 } else {
572 /* skip this unused q_vector */
573 continue;
574 }
Alan Brady96db7762016-09-14 16:24:38 -0700575 err = request_irq(irq_num,
576 i40evf_msix_clean_rings,
577 0,
578 q_vector->name,
579 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000580 if (err) {
581 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400582 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000583 goto free_queue_irqs;
584 }
Alan Brady96db7762016-09-14 16:24:38 -0700585 /* register for affinity change notifications */
586 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
587 q_vector->affinity_notify.release =
588 i40evf_irq_affinity_release;
589 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Greg Rose5eae00c2013-12-21 06:12:45 +0000590 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -0700591 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +0000592 }
593
594 return 0;
595
596free_queue_irqs:
597 while (vector) {
598 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700599 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
600 irq_set_affinity_notifier(irq_num, NULL);
601 irq_set_affinity_hint(irq_num, NULL);
602 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000603 }
604 return err;
605}
606
607/**
608 * i40evf_request_misc_irq - Initialize MSI-X interrupts
609 * @adapter: board private structure
610 *
611 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
612 * vector is only for the admin queue, and stays active even when the netdev
613 * is closed.
614 **/
615static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
616{
617 struct net_device *netdev = adapter->netdev;
618 int err;
619
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700620 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000621 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
622 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000623 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800624 &i40evf_msix_aq, 0,
625 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000626 if (err) {
627 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800628 "request_irq for %s failed: %d\n",
629 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000630 free_irq(adapter->msix_entries[0].vector, netdev);
631 }
632 return err;
633}
634
635/**
636 * i40evf_free_traffic_irqs - Free MSI-X interrupts
637 * @adapter: board private structure
638 *
639 * Frees all MSI-X vectors other than 0.
640 **/
641static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
642{
Alan Brady96db7762016-09-14 16:24:38 -0700643 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000644
Alan Brady47d2a5d2016-11-08 13:05:05 -0800645 if (!adapter->msix_entries)
646 return;
647
Greg Rose5eae00c2013-12-21 06:12:45 +0000648 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
649
Alan Brady96db7762016-09-14 16:24:38 -0700650 for (vector = 0; vector < q_vectors; vector++) {
651 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
652 irq_set_affinity_notifier(irq_num, NULL);
653 irq_set_affinity_hint(irq_num, NULL);
654 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000655 }
656}
657
658/**
659 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
660 * @adapter: board private structure
661 *
662 * Frees MSI-X vector 0.
663 **/
664static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
665{
666 struct net_device *netdev = adapter->netdev;
667
Jacob Kelleref4603e2016-11-08 13:05:08 -0800668 if (!adapter->msix_entries)
669 return;
670
Greg Rose5eae00c2013-12-21 06:12:45 +0000671 free_irq(adapter->msix_entries[0].vector, netdev);
672}
673
674/**
675 * i40evf_configure_tx - Configure Transmit Unit after Reset
676 * @adapter: board private structure
677 *
678 * Configure the Tx unit of the MAC after a reset.
679 **/
680static void i40evf_configure_tx(struct i40evf_adapter *adapter)
681{
682 struct i40e_hw *hw = &adapter->hw;
683 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000684
Mitch Williamscc052922014-10-25 03:24:34 +0000685 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400686 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000687}
688
689/**
690 * i40evf_configure_rx - Configure Receive Unit after Reset
691 * @adapter: board private structure
692 *
693 * Configure the Rx unit of the MAC after a reset.
694 **/
695static void i40evf_configure_rx(struct i40evf_adapter *adapter)
696{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700697 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000698 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000699 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000700
Alexander Duyckdab86af2017-03-14 10:15:27 -0700701 /* Legacy Rx will always default to a 2048 buffer size. */
702#if (PAGE_SIZE < 8192)
703 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200704 struct net_device *netdev = adapter->netdev;
705
Alexander Duyck98efd692017-04-05 07:51:01 -0400706 /* For jumbo frames on systems with 4K pages we have to use
707 * an order 1 page, so we might as well increase the size
708 * of our Rx buffer to make better use of the available space
709 */
710 rx_buf_len = I40E_RXBUFFER_3072;
711
Alexander Duyckdab86af2017-03-14 10:15:27 -0700712 /* We use a 1536 buffer size for configurations with
713 * standard Ethernet mtu. On x86 this gives us enough room
714 * for shared info and 192 bytes of padding.
715 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400716 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
717 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700718 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
719 }
720#endif
721
Mitch Williamscc052922014-10-25 03:24:34 +0000722 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400723 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700724 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400725
726 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
727 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
728 else
729 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000730 }
731}
732
733/**
734 * i40evf_find_vlan - Search filter list for specific vlan filter
735 * @adapter: board private structure
736 * @vlan: vlan tag
737 *
738 * Returns ptr to the filter object or NULL
739 **/
740static struct
741i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
742{
743 struct i40evf_vlan_filter *f;
744
745 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
746 if (vlan == f->vlan)
747 return f;
748 }
749 return NULL;
750}
751
752/**
753 * i40evf_add_vlan - Add a vlan filter to the list
754 * @adapter: board private structure
755 * @vlan: VLAN tag
756 *
757 * Returns ptr to the filter object or NULL when no memory available.
758 **/
759static struct
760i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
761{
Mitch Williams13acb542015-03-31 00:45:05 -0700762 struct i40evf_vlan_filter *f = NULL;
763 int count = 50;
764
765 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
766 &adapter->crit_section)) {
767 udelay(1);
768 if (--count == 0)
769 goto out;
770 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000771
772 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000773 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000774 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000775 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700776 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000777
Greg Rose5eae00c2013-12-21 06:12:45 +0000778 f->vlan = vlan;
779
780 INIT_LIST_HEAD(&f->list);
781 list_add(&f->list, &adapter->vlan_filter_list);
782 f->add = true;
783 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
784 }
785
Mitch Williams13acb542015-03-31 00:45:05 -0700786clearout:
787 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
788out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000789 return f;
790}
791
792/**
793 * i40evf_del_vlan - Remove a vlan filter from the list
794 * @adapter: board private structure
795 * @vlan: VLAN tag
796 **/
797static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
798{
799 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700800 int count = 50;
801
802 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
803 &adapter->crit_section)) {
804 udelay(1);
805 if (--count == 0)
806 return;
807 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000808
809 f = i40evf_find_vlan(adapter, vlan);
810 if (f) {
811 f->remove = true;
812 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
813 }
Mitch Williams13acb542015-03-31 00:45:05 -0700814 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000815}
816
817/**
818 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
819 * @netdev: network device struct
820 * @vid: VLAN tag
821 **/
822static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000823 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000824{
825 struct i40evf_adapter *adapter = netdev_priv(netdev);
826
Mitch Williams8ed995f2015-08-28 17:55:57 -0400827 if (!VLAN_ALLOWED(adapter))
828 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000829 if (i40evf_add_vlan(adapter, vid) == NULL)
830 return -ENOMEM;
831 return 0;
832}
833
834/**
835 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
836 * @netdev: network device struct
837 * @vid: VLAN tag
838 **/
839static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000840 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000841{
842 struct i40evf_adapter *adapter = netdev_priv(netdev);
843
Mitch Williams8ed995f2015-08-28 17:55:57 -0400844 if (VLAN_ALLOWED(adapter)) {
845 i40evf_del_vlan(adapter, vid);
846 return 0;
847 }
848 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000849}
850
851/**
852 * i40evf_find_filter - Search filter list for specific mac filter
853 * @adapter: board private structure
854 * @macaddr: the MAC address
855 *
856 * Returns ptr to the filter object or NULL
857 **/
858static struct
859i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
860 u8 *macaddr)
861{
862 struct i40evf_mac_filter *f;
863
864 if (!macaddr)
865 return NULL;
866
867 list_for_each_entry(f, &adapter->mac_filter_list, list) {
868 if (ether_addr_equal(macaddr, f->macaddr))
869 return f;
870 }
871 return NULL;
872}
873
874/**
875 * i40e_add_filter - Add a mac filter to the filter list
876 * @adapter: board private structure
877 * @macaddr: the MAC address
878 *
879 * Returns ptr to the filter object or NULL when no memory available.
880 **/
881static struct
882i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
883 u8 *macaddr)
884{
885 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000886 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000887
888 if (!macaddr)
889 return NULL;
890
891 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000892 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000893 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000894 if (--count == 0)
895 return NULL;
896 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000897
898 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000899 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000900 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000901 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000902 clear_bit(__I40EVF_IN_CRITICAL_TASK,
903 &adapter->crit_section);
904 return NULL;
905 }
906
Greg Rose9a173902014-05-22 06:32:02 +0000907 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000908
Mitch Williams63590b62016-05-16 10:26:42 -0700909 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000910 f->add = true;
911 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
912 }
913
914 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
915 return f;
916}
917
918/**
919 * i40evf_set_mac - NDO callback to set port mac address
920 * @netdev: network interface device structure
921 * @p: pointer to an address structure
922 *
923 * Returns 0 on success, negative on failure
924 **/
925static int i40evf_set_mac(struct net_device *netdev, void *p)
926{
927 struct i40evf_adapter *adapter = netdev_priv(netdev);
928 struct i40e_hw *hw = &adapter->hw;
929 struct i40evf_mac_filter *f;
930 struct sockaddr *addr = p;
931
932 if (!is_valid_ether_addr(addr->sa_data))
933 return -EADDRNOTAVAIL;
934
935 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
936 return 0;
937
Mitch Williams14e52ee2015-08-31 19:54:44 -0400938 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
939 return -EPERM;
940
941 f = i40evf_find_filter(adapter, hw->mac.addr);
942 if (f) {
943 f->remove = true;
944 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
945 }
946
Greg Rose5eae00c2013-12-21 06:12:45 +0000947 f = i40evf_add_filter(adapter, addr->sa_data);
948 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000949 ether_addr_copy(hw->mac.addr, addr->sa_data);
950 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000951 }
952
953 return (f == NULL) ? -ENOMEM : 0;
954}
955
956/**
957 * i40evf_set_rx_mode - NDO callback to set the netdev filters
958 * @netdev: network interface device structure
959 **/
960static void i40evf_set_rx_mode(struct net_device *netdev)
961{
962 struct i40evf_adapter *adapter = netdev_priv(netdev);
963 struct i40evf_mac_filter *f, *ftmp;
964 struct netdev_hw_addr *uca;
965 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400966 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000967 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000968
969 /* add addr if not already in the filter list */
970 netdev_for_each_uc_addr(uca, netdev) {
971 i40evf_add_filter(adapter, uca->addr);
972 }
973 netdev_for_each_mc_addr(mca, netdev) {
974 i40evf_add_filter(adapter, mca->addr);
975 }
976
977 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000978 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000979 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000980 if (--count == 0) {
981 dev_err(&adapter->pdev->dev,
982 "Failed to get lock in %s\n", __func__);
983 return;
984 }
985 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000986 /* remove filter if not in netdev list */
987 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400988 netdev_for_each_mc_addr(mca, netdev)
989 if (ether_addr_equal(mca->addr, f->macaddr))
990 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000991
Shannon Nelson2f41f332015-08-26 15:14:20 -0400992 netdev_for_each_uc_addr(uca, netdev)
993 if (ether_addr_equal(uca->addr, f->macaddr))
994 goto bottom_of_search_loop;
995
996 for_each_dev_addr(netdev, ha)
997 if (ether_addr_equal(ha->addr, f->macaddr))
998 goto bottom_of_search_loop;
999
1000 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
1001 goto bottom_of_search_loop;
1002
1003 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1004 f->remove = true;
1005 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1006
1007bottom_of_search_loop:
1008 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +00001009 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001010
1011 if (netdev->flags & IFF_PROMISC &&
1012 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
1013 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
1014 else if (!(netdev->flags & IFF_PROMISC) &&
1015 adapter->flags & I40EVF_FLAG_PROMISC_ON)
1016 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
1017
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001018 if (netdev->flags & IFF_ALLMULTI &&
1019 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
1020 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
1021 else if (!(netdev->flags & IFF_ALLMULTI) &&
1022 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
1023 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
1024
Greg Rose5eae00c2013-12-21 06:12:45 +00001025 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1026}
1027
1028/**
1029 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1030 * @adapter: board private structure
1031 **/
1032static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1033{
1034 int q_idx;
1035 struct i40e_q_vector *q_vector;
1036 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1037
1038 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1039 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001040
Mitch Williams7d96ba12015-10-26 19:44:39 -04001041 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001042 napi = &q_vector->napi;
1043 napi_enable(napi);
1044 }
1045}
1046
1047/**
1048 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1049 * @adapter: board private structure
1050 **/
1051static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1052{
1053 int q_idx;
1054 struct i40e_q_vector *q_vector;
1055 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1056
1057 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001058 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001059 napi_disable(&q_vector->napi);
1060 }
1061}
1062
1063/**
1064 * i40evf_configure - set up transmit and receive data structures
1065 * @adapter: board private structure
1066 **/
1067static void i40evf_configure(struct i40evf_adapter *adapter)
1068{
1069 struct net_device *netdev = adapter->netdev;
1070 int i;
1071
1072 i40evf_set_rx_mode(netdev);
1073
1074 i40evf_configure_tx(adapter);
1075 i40evf_configure_rx(adapter);
1076 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1077
Mitch Williamscc052922014-10-25 03:24:34 +00001078 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001079 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001080
Mitch Williamsb1630982016-04-18 11:33:48 -07001081 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001082 }
1083}
1084
1085/**
1086 * i40evf_up_complete - Finish the last steps of bringing up a connection
1087 * @adapter: board private structure
1088 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001089static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001090{
1091 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001092 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001093
1094 i40evf_napi_enable_all(adapter);
1095
1096 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001097 if (CLIENT_ENABLED(adapter))
1098 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001099 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001100}
1101
1102/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001103 * i40e_down - Shutdown the connection processing
1104 * @adapter: board private structure
1105 **/
1106void i40evf_down(struct i40evf_adapter *adapter)
1107{
1108 struct net_device *netdev = adapter->netdev;
1109 struct i40evf_mac_filter *f;
1110
Mitch Williams209dc4d2015-12-09 15:50:27 -08001111 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001112 return;
1113
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001114 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1115 &adapter->crit_section))
1116 usleep_range(500, 1000);
1117
Mitch Williams63e18c22015-03-27 00:12:10 -07001118 netif_carrier_off(netdev);
1119 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001120 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001121 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001122 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001123
Mitch Williamsef8693e2014-02-13 03:48:53 -08001124 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001125 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1126 f->remove = true;
1127 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001128 /* remove all VLAN filters */
1129 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1130 f->remove = true;
1131 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001132 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1133 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001134 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001135 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001136 /* Schedule operations to close down the HW. Don't wait
1137 * here for this to complete. The watchdog is still running
1138 * and it will take care of this.
1139 */
1140 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001141 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001142 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001143 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001144
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001145 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001146 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001147}
1148
1149/**
1150 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1151 * @adapter: board private structure
1152 * @vectors: number of vectors to request
1153 *
1154 * Work with the OS to set up the MSIX vectors needed.
1155 *
1156 * Returns 0 on success, negative on failure
1157 **/
1158static int
1159i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1160{
1161 int err, vector_threshold;
1162
1163 /* We'll want at least 3 (vector_threshold):
1164 * 0) Other (Admin Queue and link, mostly)
1165 * 1) TxQ[0] Cleanup
1166 * 2) RxQ[0] Cleanup
1167 */
1168 vector_threshold = MIN_MSIX_COUNT;
1169
1170 /* The more we get, the more we will assign to Tx/Rx Cleanup
1171 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1172 * Right now, we simply care about how many we'll get; we'll
1173 * set them up later while requesting irq's.
1174 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001175 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1176 vector_threshold, vectors);
1177 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001178 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001179 kfree(adapter->msix_entries);
1180 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001181 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001182 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001183
1184 /* Adjust for only the vectors we'll use, which is minimum
1185 * of max_msix_q_vectors + NONQ_VECS, or the number of
1186 * vectors we were allocated.
1187 */
1188 adapter->num_msix_vectors = err;
1189 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001190}
1191
1192/**
1193 * i40evf_free_queues - Free memory for all rings
1194 * @adapter: board private structure to initialize
1195 *
1196 * Free all of the memory associated with queue pairs.
1197 **/
1198static void i40evf_free_queues(struct i40evf_adapter *adapter)
1199{
Greg Rose5eae00c2013-12-21 06:12:45 +00001200 if (!adapter->vsi_res)
1201 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001202 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001203 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001204 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001205 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001206 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001207}
1208
1209/**
1210 * i40evf_alloc_queues - Allocate memory for all rings
1211 * @adapter: board private structure to initialize
1212 *
1213 * We allocate one ring per queue at run-time since we don't know the
1214 * number of queues at compile-time. The polling_netdev array is
1215 * intended for Multiqueue, but should work fine with a single queue.
1216 **/
1217static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1218{
Jacob Keller65c70062017-06-07 05:43:01 -04001219 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001220
Jacob Keller65c70062017-06-07 05:43:01 -04001221 num_active_queues = min_t(int,
1222 adapter->vsi_res->num_queue_pairs,
1223 (int)(num_online_cpus()));
1224
1225 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001226 sizeof(struct i40e_ring), GFP_KERNEL);
1227 if (!adapter->tx_rings)
1228 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001229 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001230 sizeof(struct i40e_ring), GFP_KERNEL);
1231 if (!adapter->rx_rings)
1232 goto err_out;
1233
Jacob Keller65c70062017-06-07 05:43:01 -04001234 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001235 struct i40e_ring *tx_ring;
1236 struct i40e_ring *rx_ring;
1237
Mitch Williams0dd438d2015-10-26 19:44:40 -04001238 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001239
1240 tx_ring->queue_index = i;
1241 tx_ring->netdev = adapter->netdev;
1242 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001243 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001244 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001245 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001246 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001247
Mitch Williams0dd438d2015-10-26 19:44:40 -04001248 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001249 rx_ring->queue_index = i;
1250 rx_ring->netdev = adapter->netdev;
1251 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001252 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001253 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001254 }
1255
Jacob Keller65c70062017-06-07 05:43:01 -04001256 adapter->num_active_queues = num_active_queues;
1257
Greg Rose5eae00c2013-12-21 06:12:45 +00001258 return 0;
1259
1260err_out:
1261 i40evf_free_queues(adapter);
1262 return -ENOMEM;
1263}
1264
1265/**
1266 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1267 * @adapter: board private structure to initialize
1268 *
1269 * Attempt to configure the interrupts using the best available
1270 * capabilities of the hardware and the kernel.
1271 **/
1272static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1273{
1274 int vector, v_budget;
1275 int pairs = 0;
1276 int err = 0;
1277
1278 if (!adapter->vsi_res) {
1279 err = -EIO;
1280 goto out;
1281 }
Mitch Williamscc052922014-10-25 03:24:34 +00001282 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001283
Jacob Keller789f38c2017-04-19 09:25:56 -04001284 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1285 * us much good if we have more vectors than CPUs. However, we already
1286 * limit the total number of queues by the number of CPUs so we do not
1287 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001288 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001289 v_budget = min_t(int, pairs + NONQ_VECS,
1290 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001291
Greg Rose5eae00c2013-12-21 06:12:45 +00001292 adapter->msix_entries = kcalloc(v_budget,
1293 sizeof(struct msix_entry), GFP_KERNEL);
1294 if (!adapter->msix_entries) {
1295 err = -ENOMEM;
1296 goto out;
1297 }
1298
1299 for (vector = 0; vector < v_budget; vector++)
1300 adapter->msix_entries[vector].entry = vector;
1301
Mitch Williams313ed2d2015-08-27 11:42:31 -04001302 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001303
1304out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001305 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1306 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001307 return err;
1308}
1309
1310/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001311 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1312 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001313 *
1314 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001315 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001316static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001317{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001318 struct i40e_aqc_get_set_rss_key_data *rss_key =
1319 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001320 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001321 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001322
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001323 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001324 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001325 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001326 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001327 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001328 }
1329
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001330 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1331 if (ret) {
1332 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1333 i40evf_stat_str(hw, ret),
1334 i40evf_aq_str(hw, hw->aq.asq_last_status));
1335 return ret;
1336
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001337 }
1338
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001339 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1340 adapter->rss_lut, adapter->rss_lut_size);
1341 if (ret) {
1342 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1343 i40evf_stat_str(hw, ret),
1344 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001345 }
1346
1347 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001348
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001349}
1350
1351/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001352 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001353 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001354 *
1355 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001356 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001357static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001358{
1359 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001360 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001361 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001362
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001363 dw = (u32 *)adapter->rss_key;
1364 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1365 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001366
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001367 dw = (u32 *)adapter->rss_lut;
1368 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1369 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001370
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001371 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001372
1373 return 0;
1374}
1375
1376/**
1377 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001378 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001379 *
1380 * Returns 0 on success, negative on failure
1381 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001382int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001383{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001384
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001385 if (RSS_PF(adapter)) {
1386 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1387 I40EVF_FLAG_AQ_SET_RSS_KEY;
1388 return 0;
1389 } else if (RSS_AQ(adapter)) {
1390 return i40evf_config_rss_aq(adapter);
1391 } else {
1392 return i40evf_config_rss_reg(adapter);
1393 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001394}
1395
1396/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001397 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001398 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001399 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001400static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001401{
1402 u16 i;
1403
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001404 for (i = 0; i < adapter->rss_lut_size; i++)
1405 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001406}
1407
1408/**
Helin Zhang96a81982015-10-26 19:44:31 -04001409 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001410 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001411 *
1412 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001413 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001414static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001415{
1416 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001417 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001418
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001419 if (!RSS_PF(adapter)) {
1420 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001421 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001422 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001423 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1424 else
1425 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001426
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001427 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1428 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1429 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001430
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001431 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001432
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001433 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1434 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001435
1436 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001437}
1438
1439/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001440 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1441 * @adapter: board private structure to initialize
1442 *
1443 * We allocate one q_vector per queue interrupt. If allocation fails we
1444 * return -ENOMEM.
1445 **/
1446static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1447{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001448 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001449 struct i40e_q_vector *q_vector;
1450
1451 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001452 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001453 GFP_KERNEL);
1454 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001455 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001456
1457 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001458 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001459 q_vector->adapter = adapter;
1460 q_vector->vsi = &adapter->vsi;
1461 q_vector->v_idx = q_idx;
1462 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001463 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001464 }
1465
1466 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001467}
1468
1469/**
1470 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1471 * @adapter: board private structure to initialize
1472 *
1473 * This function frees the memory allocated to the q_vectors. In addition if
1474 * NAPI is enabled it will delete any references to the NAPI struct prior
1475 * to freeing the q_vector.
1476 **/
1477static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1478{
1479 int q_idx, num_q_vectors;
1480 int napi_vectors;
1481
Jacob Kelleref4603e2016-11-08 13:05:08 -08001482 if (!adapter->q_vectors)
1483 return;
1484
Greg Rose5eae00c2013-12-21 06:12:45 +00001485 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001486 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001487
1488 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001489 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001490 if (q_idx < napi_vectors)
1491 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001492 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001493 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001494 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001495}
1496
1497/**
1498 * i40evf_reset_interrupt_capability - Reset MSIX setup
1499 * @adapter: board private structure
1500 *
1501 **/
1502void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1503{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001504 if (!adapter->msix_entries)
1505 return;
1506
Greg Rose5eae00c2013-12-21 06:12:45 +00001507 pci_disable_msix(adapter->pdev);
1508 kfree(adapter->msix_entries);
1509 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001510}
1511
1512/**
1513 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1514 * @adapter: board private structure to initialize
1515 *
1516 **/
1517int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1518{
1519 int err;
1520
Jacob Keller283aeaf2017-04-19 09:25:59 -04001521 err = i40evf_alloc_queues(adapter);
1522 if (err) {
1523 dev_err(&adapter->pdev->dev,
1524 "Unable to allocate memory for queues\n");
1525 goto err_alloc_queues;
1526 }
1527
Jacob Keller62fe2a82016-07-27 12:02:33 -07001528 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001529 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001530 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001531 if (err) {
1532 dev_err(&adapter->pdev->dev,
1533 "Unable to setup interrupt capabilities\n");
1534 goto err_set_interrupt;
1535 }
1536
1537 err = i40evf_alloc_q_vectors(adapter);
1538 if (err) {
1539 dev_err(&adapter->pdev->dev,
1540 "Unable to allocate memory for queue vectors\n");
1541 goto err_alloc_q_vectors;
1542 }
1543
Greg Rose5eae00c2013-12-21 06:12:45 +00001544 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001545 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1546 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001547
1548 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001549err_alloc_q_vectors:
1550 i40evf_reset_interrupt_capability(adapter);
1551err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001552 i40evf_free_queues(adapter);
1553err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001554 return err;
1555}
1556
1557/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001558 * i40evf_free_rss - Free memory used by RSS structs
1559 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001560 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001561static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001562{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001563 kfree(adapter->rss_key);
1564 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001565
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001566 kfree(adapter->rss_lut);
1567 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001568}
1569
1570/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001571 * i40evf_watchdog_timer - Periodic call-back timer
1572 * @data: pointer to adapter disguised as unsigned long
1573 **/
1574static void i40evf_watchdog_timer(unsigned long data)
1575{
1576 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001577
Greg Rose5eae00c2013-12-21 06:12:45 +00001578 schedule_work(&adapter->watchdog_task);
1579 /* timer will be rescheduled in watchdog task */
1580}
1581
1582/**
1583 * i40evf_watchdog_task - Periodic call-back task
1584 * @work: pointer to work_struct
1585 **/
1586static void i40evf_watchdog_task(struct work_struct *work)
1587{
1588 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001589 struct i40evf_adapter,
1590 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001591 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001592 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001593
Greg Rose5eae00c2013-12-21 06:12:45 +00001594 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001595 goto restart_watchdog;
1596
1597 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001598 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1599 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001600 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1601 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001602 /* A chance for redemption! */
1603 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1604 adapter->state = __I40EVF_STARTUP;
1605 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1606 schedule_delayed_work(&adapter->init_task, 10);
1607 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1608 &adapter->crit_section);
1609 /* Don't reschedule the watchdog, since we've restarted
1610 * the init task. When init_task contacts the PF and
1611 * gets everything set up again, it'll restart the
1612 * watchdog for us. Down, boy. Sit. Stay. Woof.
1613 */
1614 return;
1615 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001616 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001617 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001618 goto watchdog_done;
1619 }
1620
1621 if ((adapter->state < __I40EVF_DOWN) ||
1622 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001623 goto watchdog_done;
1624
Mitch Williamsef8693e2014-02-13 03:48:53 -08001625 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001626 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1627 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001628 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001629 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001630 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001631 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001632 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001633 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001634 goto watchdog_done;
1635 }
1636
1637 /* Process admin queue tasks. After init, everything gets done
1638 * here so we don't race on the admin queue.
1639 */
Mitch Williamsed636962015-04-07 19:45:32 -04001640 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001641 if (!i40evf_asq_done(hw)) {
1642 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1643 i40evf_send_api_ver(adapter);
1644 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001645 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001646 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001647 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1648 i40evf_send_vf_config_msg(adapter);
1649 goto watchdog_done;
1650 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001651
Mitch Williamse284fc82015-03-27 00:12:09 -07001652 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1653 i40evf_disable_queues(adapter);
1654 goto watchdog_done;
1655 }
1656
Greg Rose5eae00c2013-12-21 06:12:45 +00001657 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1658 i40evf_map_queues(adapter);
1659 goto watchdog_done;
1660 }
1661
1662 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1663 i40evf_add_ether_addrs(adapter);
1664 goto watchdog_done;
1665 }
1666
1667 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1668 i40evf_add_vlans(adapter);
1669 goto watchdog_done;
1670 }
1671
1672 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1673 i40evf_del_ether_addrs(adapter);
1674 goto watchdog_done;
1675 }
1676
1677 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1678 i40evf_del_vlans(adapter);
1679 goto watchdog_done;
1680 }
1681
Greg Rose5eae00c2013-12-21 06:12:45 +00001682 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1683 i40evf_configure_queues(adapter);
1684 goto watchdog_done;
1685 }
1686
1687 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1688 i40evf_enable_queues(adapter);
1689 goto watchdog_done;
1690 }
1691
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001692 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1693 /* This message goes straight to the firmware, not the
1694 * PF, so we don't have to set current_op as we will
1695 * not get a response through the ARQ.
1696 */
Helin Zhang96a81982015-10-26 19:44:31 -04001697 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001698 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1699 goto watchdog_done;
1700 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001701 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1702 i40evf_get_hena(adapter);
1703 goto watchdog_done;
1704 }
1705 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1706 i40evf_set_hena(adapter);
1707 goto watchdog_done;
1708 }
1709 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1710 i40evf_set_rss_key(adapter);
1711 goto watchdog_done;
1712 }
1713 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1714 i40evf_set_rss_lut(adapter);
1715 goto watchdog_done;
1716 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001717
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001718 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001719 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1720 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001721 goto watchdog_done;
1722 }
1723
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001724 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001725 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001726 goto watchdog_done;
1727 }
1728
1729 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1730 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001731 i40evf_set_promiscuous(adapter, 0);
1732 goto watchdog_done;
1733 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001734 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001735
Greg Rose5eae00c2013-12-21 06:12:45 +00001736 if (adapter->state == __I40EVF_RUNNING)
1737 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001738watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001739 if (adapter->state == __I40EVF_RUNNING) {
1740 i40evf_irq_enable_queues(adapter, ~0);
1741 i40evf_fire_sw_int(adapter, 0xFF);
1742 } else {
1743 i40evf_fire_sw_int(adapter, 0x1);
1744 }
1745
Mitch Williamsef8693e2014-02-13 03:48:53 -08001746 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1747restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001748 if (adapter->state == __I40EVF_REMOVE)
1749 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001750 if (adapter->aq_required)
1751 mod_timer(&adapter->watchdog_timer,
1752 jiffies + msecs_to_jiffies(20));
1753 else
1754 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001755 schedule_work(&adapter->adminq_task);
1756}
1757
Joe Perchesdedecb62016-11-01 15:35:14 -07001758static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1759{
1760 struct i40evf_mac_filter *f, *ftmp;
1761 struct i40evf_vlan_filter *fv, *fvtmp;
1762
1763 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1764
1765 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001766 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001767 netif_carrier_off(adapter->netdev);
1768 netif_tx_disable(adapter->netdev);
1769 adapter->link_up = false;
1770 i40evf_napi_disable_all(adapter);
1771 i40evf_irq_disable(adapter);
1772 i40evf_free_traffic_irqs(adapter);
1773 i40evf_free_all_tx_resources(adapter);
1774 i40evf_free_all_rx_resources(adapter);
1775 }
1776
1777 /* Delete all of the filters, both MAC and VLAN. */
1778 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1779 list_del(&f->list);
1780 kfree(f);
1781 }
1782
1783 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1784 list_del(&fv->list);
1785 kfree(fv);
1786 }
1787
1788 i40evf_free_misc_irq(adapter);
1789 i40evf_reset_interrupt_capability(adapter);
1790 i40evf_free_queues(adapter);
1791 i40evf_free_q_vectors(adapter);
1792 kfree(adapter->vf_res);
1793 i40evf_shutdown_adminq(&adapter->hw);
1794 adapter->netdev->flags &= ~IFF_UP;
1795 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1796 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1797 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001798 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001799 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1800}
1801
Mitch Williams67c818a2015-06-19 08:56:30 -07001802#define I40EVF_RESET_WAIT_MS 10
1803#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001804/**
1805 * i40evf_reset_task - Call-back task to handle hardware reset
1806 * @work: pointer to work_struct
1807 *
1808 * During reset we need to shut down and reinitialize the admin queue
1809 * before we can use it to communicate with the PF again. We also clear
1810 * and reinit the rings because that context is lost as well.
1811 **/
1812static void i40evf_reset_task(struct work_struct *work)
1813{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001814 struct i40evf_adapter *adapter = container_of(work,
1815 struct i40evf_adapter,
1816 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001817 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001818 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001819 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001820 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001821 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001822 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001823
Mitch Williamsed0e8942017-01-24 10:23:59 -08001824 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001825 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001826 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001827 if (CLIENT_ENABLED(adapter)) {
1828 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1829 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1830 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1831 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1832 cancel_delayed_work_sync(&adapter->client_task);
1833 i40evf_notify_client_close(&adapter->vsi, true);
1834 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001835 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001836 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001837 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1838 /* Restart the AQ here. If we have been reset but didn't
1839 * detect it, or if the PF had to reinit, our AQ will be hosed.
1840 */
1841 i40evf_shutdown_adminq(hw);
1842 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001843 i40evf_request_reset(adapter);
1844 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001845 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001846
Mitch Williamsef8693e2014-02-13 03:48:53 -08001847 /* poll until we see the reset actually happen */
1848 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001849 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1850 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1851 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001852 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001853 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001854 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001855 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001856 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001857 goto continue_reset; /* act like the reset happened */
1858 }
1859
1860 /* wait until the reset is complete and the PF is responding to us */
1861 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001862 /* sleep first to make sure a minimum wait time is met */
1863 msleep(I40EVF_RESET_WAIT_MS);
1864
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001865 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1866 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001867 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001868 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001869 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001870
Mitch Williams509a4472015-12-23 12:05:52 -08001871 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001872
Mitch Williamsef8693e2014-02-13 03:48:53 -08001873 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001874 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001875 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001876 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001877 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001878 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001879 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001880
1881continue_reset:
Alan Bradyf0db7892017-07-12 05:46:04 -04001882 if (netif_running(netdev)) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001883 netif_carrier_off(netdev);
1884 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001885 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001886 i40evf_napi_disable_all(adapter);
1887 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001888 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001889
Greg Rose5eae00c2013-12-21 06:12:45 +00001890 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001891 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1892
1893 /* free the Tx/Rx rings and descriptors, might be better to just
1894 * re-use them sometime in the future
1895 */
1896 i40evf_free_all_rx_resources(adapter);
1897 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001898
1899 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001900 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001901 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001902 err = i40evf_init_adminq(hw);
1903 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001904 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1905 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001906
Mitch Williamse6d038d2015-06-04 16:23:58 -04001907 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1908 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001909
1910 /* re-add all MAC filters */
1911 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1912 f->add = true;
1913 }
1914 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001915 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1916 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001917 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001918 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001919 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001920 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001921 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001922 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001923
1924 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1925
1926 if (netif_running(adapter->netdev)) {
1927 /* allocate transmit descriptors */
1928 err = i40evf_setup_all_tx_resources(adapter);
1929 if (err)
1930 goto reset_err;
1931
1932 /* allocate receive descriptors */
1933 err = i40evf_setup_all_rx_resources(adapter);
1934 if (err)
1935 goto reset_err;
1936
1937 i40evf_configure(adapter);
1938
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001939 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001940
1941 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001942 } else {
1943 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001944 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001945 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001946
Greg Rose5eae00c2013-12-21 06:12:45 +00001947 return;
1948reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001949 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001950 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001951}
1952
1953/**
1954 * i40evf_adminq_task - worker thread to clean the admin queue
1955 * @work: pointer to work_struct containing our data
1956 **/
1957static void i40evf_adminq_task(struct work_struct *work)
1958{
1959 struct i40evf_adapter *adapter =
1960 container_of(work, struct i40evf_adapter, adminq_task);
1961 struct i40e_hw *hw = &adapter->hw;
1962 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001963 enum virtchnl_ops v_op;
1964 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001965 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001966 u16 pending;
1967
Mitch Williamsef8693e2014-02-13 03:48:53 -08001968 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001969 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001970
Mitch Williams1001dc32014-11-11 20:02:19 +00001971 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1972 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001973 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001974 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001975
Greg Rose5eae00c2013-12-21 06:12:45 +00001976 do {
1977 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07001978 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
1979 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
1980
1981 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00001982 break; /* No event to process or error cleaning ARQ */
1983
Tushar Davec969ef42017-06-22 09:44:32 -07001984 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001985 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001986 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001987 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001988 } while (pending);
1989
Mitch Williams67c818a2015-06-19 08:56:30 -07001990 if ((adapter->flags &
1991 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1992 adapter->state == __I40EVF_RESETTING)
1993 goto freedom;
1994
Mitch Williams912257e2014-05-22 06:32:07 +00001995 /* check for error indications */
1996 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001997 if (val == 0xdeadbeef) /* indicates device in reset */
1998 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001999 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002000 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002001 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002002 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002003 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002004 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002005 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002006 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002007 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002008 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002009 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002010 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002011 }
2012 if (oldval != val)
2013 wr32(hw, hw->aq.arq.len, val);
2014
2015 val = rd32(hw, hw->aq.asq.len);
2016 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002017 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002018 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002019 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002020 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002021 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002022 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002023 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002024 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002025 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002026 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002027 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002028 }
2029 if (oldval != val)
2030 wr32(hw, hw->aq.asq.len, val);
2031
Mitch Williams67c818a2015-06-19 08:56:30 -07002032freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002033 kfree(event.msg_buf);
2034out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002035 /* re-enable Admin queue interrupt cause */
2036 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002037}
2038
2039/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002040 * i40evf_client_task - worker thread to perform client work
2041 * @work: pointer to work_struct containing our data
2042 *
2043 * This task handles client interactions. Because client calls can be
2044 * reentrant, we can't handle them in the watchdog.
2045 **/
2046static void i40evf_client_task(struct work_struct *work)
2047{
2048 struct i40evf_adapter *adapter =
2049 container_of(work, struct i40evf_adapter, client_task.work);
2050
2051 /* If we can't get the client bit, just give up. We'll be rescheduled
2052 * later.
2053 */
2054
2055 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2056 return;
2057
2058 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2059 i40evf_client_subtask(adapter);
2060 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2061 goto out;
2062 }
2063 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2064 i40evf_notify_client_close(&adapter->vsi, false);
2065 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2066 goto out;
2067 }
2068 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2069 i40evf_notify_client_open(&adapter->vsi);
2070 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2071 goto out;
2072 }
2073 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2074 i40evf_notify_client_l2_params(&adapter->vsi);
2075 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2076 }
2077out:
2078 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2079}
2080
2081/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002082 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2083 * @adapter: board private structure
2084 *
2085 * Free all transmit software resources
2086 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002087void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002088{
2089 int i;
2090
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002091 if (!adapter->tx_rings)
2092 return;
2093
Mitch Williamscc052922014-10-25 03:24:34 +00002094 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002095 if (adapter->tx_rings[i].desc)
2096 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002097}
2098
2099/**
2100 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2101 * @adapter: board private structure
2102 *
2103 * If this function returns with an error, then it's possible one or
2104 * more of the rings is populated (while the rest are not). It is the
2105 * callers duty to clean those orphaned rings.
2106 *
2107 * Return 0 on success, negative on failure
2108 **/
2109static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2110{
2111 int i, err = 0;
2112
Mitch Williamscc052922014-10-25 03:24:34 +00002113 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002114 adapter->tx_rings[i].count = adapter->tx_desc_count;
2115 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002116 if (!err)
2117 continue;
2118 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002119 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002120 break;
2121 }
2122
2123 return err;
2124}
2125
2126/**
2127 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2128 * @adapter: board private structure
2129 *
2130 * If this function returns with an error, then it's possible one or
2131 * more of the rings is populated (while the rest are not). It is the
2132 * callers duty to clean those orphaned rings.
2133 *
2134 * Return 0 on success, negative on failure
2135 **/
2136static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2137{
2138 int i, err = 0;
2139
Mitch Williamscc052922014-10-25 03:24:34 +00002140 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002141 adapter->rx_rings[i].count = adapter->rx_desc_count;
2142 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002143 if (!err)
2144 continue;
2145 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002146 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002147 break;
2148 }
2149 return err;
2150}
2151
2152/**
2153 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2154 * @adapter: board private structure
2155 *
2156 * Free all receive software resources
2157 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002158void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002159{
2160 int i;
2161
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002162 if (!adapter->rx_rings)
2163 return;
2164
Mitch Williamscc052922014-10-25 03:24:34 +00002165 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002166 if (adapter->rx_rings[i].desc)
2167 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002168}
2169
2170/**
2171 * i40evf_open - Called when a network interface is made active
2172 * @netdev: network interface device structure
2173 *
2174 * Returns 0 on success, negative value on failure
2175 *
2176 * The open entry point is called when a network interface is made
2177 * active by the system (IFF_UP). At this point all resources needed
2178 * for transmit and receive operations are allocated, the interrupt
2179 * handler is registered with the OS, the watchdog timer is started,
2180 * and the stack is notified that the interface is ready.
2181 **/
2182static int i40evf_open(struct net_device *netdev)
2183{
2184 struct i40evf_adapter *adapter = netdev_priv(netdev);
2185 int err;
2186
Mitch Williamsef8693e2014-02-13 03:48:53 -08002187 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2188 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2189 return -EIO;
2190 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002191
2192 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002193 return -EBUSY;
2194
2195 /* allocate transmit descriptors */
2196 err = i40evf_setup_all_tx_resources(adapter);
2197 if (err)
2198 goto err_setup_tx;
2199
2200 /* allocate receive descriptors */
2201 err = i40evf_setup_all_rx_resources(adapter);
2202 if (err)
2203 goto err_setup_rx;
2204
2205 /* clear any pending interrupts, may auto mask */
2206 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2207 if (err)
2208 goto err_req_irq;
2209
Mitch Williams44151cd2015-04-27 14:57:17 -04002210 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002211 i40evf_configure(adapter);
2212
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002213 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002214
2215 i40evf_irq_enable(adapter, true);
2216
2217 return 0;
2218
2219err_req_irq:
2220 i40evf_down(adapter);
2221 i40evf_free_traffic_irqs(adapter);
2222err_setup_rx:
2223 i40evf_free_all_rx_resources(adapter);
2224err_setup_tx:
2225 i40evf_free_all_tx_resources(adapter);
2226
2227 return err;
2228}
2229
2230/**
2231 * i40evf_close - Disables a network interface
2232 * @netdev: network interface device structure
2233 *
2234 * Returns 0, this is not allowed to fail
2235 *
2236 * The close entry point is called when an interface is de-activated
2237 * by the OS. The hardware is still under the drivers control, but
2238 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2239 * are freed, along with all transmit and receive resources.
2240 **/
2241static int i40evf_close(struct net_device *netdev)
2242{
2243 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002244 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002245
Mitch Williams209dc4d2015-12-09 15:50:27 -08002246 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002247 return 0;
2248
Mitch Williamsef8693e2014-02-13 03:48:53 -08002249
Jacob Keller0da36b92017-04-19 09:25:55 -04002250 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002251 if (CLIENT_ENABLED(adapter))
2252 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002253
2254 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002255 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002256 i40evf_free_traffic_irqs(adapter);
2257
Mitch Williams51f38262016-12-12 15:44:11 -08002258 /* We explicitly don't free resources here because the hardware is
2259 * still active and can DMA into memory. Resources are cleared in
2260 * i40evf_virtchnl_completion() after we get confirmation from the PF
2261 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002262 *
2263 * Also, we wait for state to transition to __I40EVF_DOWN before
2264 * returning. State change occurs in i40evf_virtchnl_completion() after
2265 * VF resources are released (which occurs after PF driver processes and
2266 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002267 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002268
2269 status = wait_event_timeout(adapter->down_waitqueue,
2270 adapter->state == __I40EVF_DOWN,
2271 msecs_to_jiffies(200));
2272 if (!status)
2273 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002274 return 0;
2275}
2276
2277/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002278 * i40evf_change_mtu - Change the Maximum Transfer Unit
2279 * @netdev: network interface device structure
2280 * @new_mtu: new value for maximum frame size
2281 *
2282 * Returns 0 on success, negative on failure
2283 **/
2284static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2285{
2286 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002287
Greg Rose5eae00c2013-12-21 06:12:45 +00002288 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002289 if (CLIENT_ENABLED(adapter)) {
2290 i40evf_notify_client_l2_params(&adapter->vsi);
2291 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2292 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002293 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2294 schedule_work(&adapter->reset_task);
2295
Greg Rose5eae00c2013-12-21 06:12:45 +00002296 return 0;
2297}
2298
Alexander Duyck06fc0162016-10-25 16:08:47 -07002299/**
2300 * i40evf_features_check - Validate encapsulated packet conforms to limits
2301 * @skb: skb buff
2302 * @netdev: This physical port's netdev
2303 * @features: Offload features that the stack believes apply
2304 **/
2305static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2306 struct net_device *dev,
2307 netdev_features_t features)
2308{
2309 size_t len;
2310
2311 /* No point in doing any of this if neither checksum nor GSO are
2312 * being requested for this frame. We can rule out both by just
2313 * checking for CHECKSUM_PARTIAL
2314 */
2315 if (skb->ip_summed != CHECKSUM_PARTIAL)
2316 return features;
2317
2318 /* We cannot support GSO if the MSS is going to be less than
2319 * 64 bytes. If it is then we need to drop support for GSO.
2320 */
2321 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2322 features &= ~NETIF_F_GSO_MASK;
2323
2324 /* MACLEN can support at most 63 words */
2325 len = skb_network_header(skb) - skb->data;
2326 if (len & ~(63 * 2))
2327 goto out_err;
2328
2329 /* IPLEN and EIPLEN can support at most 127 dwords */
2330 len = skb_transport_header(skb) - skb_network_header(skb);
2331 if (len & ~(127 * 4))
2332 goto out_err;
2333
2334 if (skb->encapsulation) {
2335 /* L4TUNLEN can support 127 words */
2336 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2337 if (len & ~(127 * 2))
2338 goto out_err;
2339
2340 /* IPLEN can support at most 127 dwords */
2341 len = skb_inner_transport_header(skb) -
2342 skb_inner_network_header(skb);
2343 if (len & ~(127 * 4))
2344 goto out_err;
2345 }
2346
2347 /* No need to validate L4LEN as TCP is the only protocol with a
2348 * a flexible value and we support all possible values supported
2349 * by TCP, which is at most 15 dwords
2350 */
2351
2352 return features;
2353out_err:
2354 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2355}
2356
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002357#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2358 NETIF_F_HW_VLAN_CTAG_RX |\
2359 NETIF_F_HW_VLAN_CTAG_FILTER)
2360
2361/**
2362 * i40evf_fix_features - fix up the netdev feature bits
2363 * @netdev: our net device
2364 * @features: desired feature bits
2365 *
2366 * Returns fixed-up features bits
2367 **/
2368static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2369 netdev_features_t features)
2370{
2371 struct i40evf_adapter *adapter = netdev_priv(netdev);
2372
2373 features &= ~I40EVF_VLAN_FEATURES;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002374 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002375 features |= I40EVF_VLAN_FEATURES;
2376 return features;
2377}
2378
Greg Rose5eae00c2013-12-21 06:12:45 +00002379static const struct net_device_ops i40evf_netdev_ops = {
2380 .ndo_open = i40evf_open,
2381 .ndo_stop = i40evf_close,
2382 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002383 .ndo_set_rx_mode = i40evf_set_rx_mode,
2384 .ndo_validate_addr = eth_validate_addr,
2385 .ndo_set_mac_address = i40evf_set_mac,
2386 .ndo_change_mtu = i40evf_change_mtu,
2387 .ndo_tx_timeout = i40evf_tx_timeout,
2388 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2389 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002390 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002391 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002392#ifdef CONFIG_NET_POLL_CONTROLLER
2393 .ndo_poll_controller = i40evf_netpoll,
2394#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002395};
2396
2397/**
2398 * i40evf_check_reset_complete - check that VF reset is complete
2399 * @hw: pointer to hw struct
2400 *
2401 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2402 **/
2403static int i40evf_check_reset_complete(struct i40e_hw *hw)
2404{
2405 u32 rstat;
2406 int i;
2407
2408 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002409 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2410 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002411 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2412 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002413 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002414 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002415 }
2416 return -EBUSY;
2417}
2418
2419/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002420 * i40evf_process_config - Process the config information we got from the PF
2421 * @adapter: board private structure
2422 *
2423 * Verify that we have a valid config struct, and set up our netdev features
2424 * and our VSI struct.
2425 **/
2426int i40evf_process_config(struct i40evf_adapter *adapter)
2427{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002428 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002429 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002430 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002431 int i;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002432 netdev_features_t hw_enc_features;
2433 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002434
2435 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002436 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002437 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002438 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002439 }
2440 if (!adapter->vsi_res) {
2441 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2442 return -ENODEV;
2443 }
2444
Preethi Banalabacd75c2017-03-27 14:43:18 -07002445 hw_enc_features = NETIF_F_SG |
2446 NETIF_F_IP_CSUM |
2447 NETIF_F_IPV6_CSUM |
2448 NETIF_F_HIGHDMA |
2449 NETIF_F_SOFT_FEATURES |
2450 NETIF_F_TSO |
2451 NETIF_F_TSO_ECN |
2452 NETIF_F_TSO6 |
2453 NETIF_F_SCTP_CRC |
2454 NETIF_F_RXHASH |
2455 NETIF_F_RXCSUM |
2456 0;
2457
2458 /* advertise to stack only if offloads for encapsulated packets is
2459 * supported
2460 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002461 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002462 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002463 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002464 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002465 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002466 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002467 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002468 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002469 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002470
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002471 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002472 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002473 netdev->gso_partial_features |=
2474 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002475
Preethi Banalabacd75c2017-03-27 14:43:18 -07002476 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2477 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2478 netdev->hw_enc_features |= hw_enc_features;
2479 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002480 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002481 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002482
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002483 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002484 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002485 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002486 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002487
Preethi Banalabacd75c2017-03-27 14:43:18 -07002488 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002489
Preethi Banalabacd75c2017-03-27 14:43:18 -07002490 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002491
2492 adapter->vsi.id = adapter->vsi_res->vsi_id;
2493
2494 adapter->vsi.back = adapter;
2495 adapter->vsi.base_vector = 1;
2496 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002497 vsi->netdev = adapter->netdev;
2498 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002499 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002500 adapter->rss_key_size = vfres->rss_key_size;
2501 adapter->rss_lut_size = vfres->rss_lut_size;
2502 } else {
2503 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2504 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2505 }
2506
Mitch Williamse6d038d2015-06-04 16:23:58 -04002507 return 0;
2508}
2509
2510/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002511 * i40evf_init_task - worker thread to perform delayed initialization
2512 * @work: pointer to work_struct containing our data
2513 *
2514 * This task completes the work that was begun in probe. Due to the nature
2515 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002516 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002517 * whole system, we'll do it in a task so we can sleep.
2518 * This task only runs during driver init. Once we've established
2519 * communications with the PF driver and set up our netdev, the watchdog
2520 * takes over.
2521 **/
2522static void i40evf_init_task(struct work_struct *work)
2523{
2524 struct i40evf_adapter *adapter = container_of(work,
2525 struct i40evf_adapter,
2526 init_task.work);
2527 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002528 struct i40e_hw *hw = &adapter->hw;
2529 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002530 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002531
2532 switch (adapter->state) {
2533 case __I40EVF_STARTUP:
2534 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002535 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2536 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002537 err = i40e_set_mac_type(hw);
2538 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002539 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2540 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002541 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002542 }
2543 err = i40evf_check_reset_complete(hw);
2544 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002545 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002546 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002547 goto err;
2548 }
2549 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2550 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2551 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2552 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2553
2554 err = i40evf_init_adminq(hw);
2555 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002556 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2557 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002558 goto err;
2559 }
2560 err = i40evf_send_api_ver(adapter);
2561 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002562 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002563 i40evf_shutdown_adminq(hw);
2564 goto err;
2565 }
2566 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2567 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002568 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002569 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002570 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002571 i40evf_shutdown_adminq(hw);
2572 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002573 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002574 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002575
2576 /* aq msg sent, awaiting reply */
2577 err = i40evf_verify_api_ver(adapter);
2578 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002579 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002580 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002581 else
2582 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2583 adapter->pf_version.major,
2584 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002585 VIRTCHNL_VERSION_MAJOR,
2586 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002587 goto err;
2588 }
2589 err = i40evf_send_vf_config_msg(adapter);
2590 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002591 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002592 err);
2593 goto err;
2594 }
2595 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2596 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002597 case __I40EVF_INIT_GET_RESOURCES:
2598 /* aq msg sent, awaiting reply */
2599 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002600 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002601 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002602 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002603 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002604 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002605 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002606 }
2607 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002608 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002609 err = i40evf_send_vf_config_msg(adapter);
2610 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002611 } else if (err == I40E_ERR_PARAM) {
2612 /* We only get ERR_PARAM if the device is in a very bad
2613 * state or if we've been disabled for previous bad
2614 * behavior. Either way, we're done now.
2615 */
2616 i40evf_shutdown_adminq(hw);
2617 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2618 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002619 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002620 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002621 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2622 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002623 goto err_alloc;
2624 }
2625 adapter->state = __I40EVF_INIT_SW;
2626 break;
2627 default:
2628 goto err_alloc;
2629 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002630
Mitch Williamse6d038d2015-06-04 16:23:58 -04002631 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002632 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002633 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002634
2635 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2636
Greg Rose5eae00c2013-12-21 06:12:45 +00002637 netdev->netdev_ops = &i40evf_netdev_ops;
2638 i40evf_set_ethtool_ops(netdev);
2639 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002640
Jarod Wilson91c527a2016-10-17 15:54:05 -04002641 /* MTU range: 68 - 9710 */
2642 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002643 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002644
Greg Rose5eae00c2013-12-21 06:12:45 +00002645 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002646 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002647 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002648 eth_hw_addr_random(netdev);
2649 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2650 } else {
2651 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2652 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2653 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002654 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002655
Greg Rose5eae00c2013-12-21 06:12:45 +00002656 init_timer(&adapter->watchdog_timer);
2657 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2658 adapter->watchdog_timer.data = (unsigned long)adapter;
2659 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2660
Mitch Williamsd732a182014-04-24 06:41:37 +00002661 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2662 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002663 err = i40evf_init_interrupt_scheme(adapter);
2664 if (err)
2665 goto err_sw_init;
2666 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002667 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002668 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002669 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2670
Greg Rose5eae00c2013-12-21 06:12:45 +00002671 err = i40evf_request_misc_irq(adapter);
2672 if (err)
2673 goto err_sw_init;
2674
2675 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002676 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002677
Mitch Williamsef8693e2014-02-13 03:48:53 -08002678 if (!adapter->netdev_registered) {
2679 err = register_netdev(netdev);
2680 if (err)
2681 goto err_register;
2682 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002683
2684 adapter->netdev_registered = true;
2685
2686 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002687 if (CLIENT_ALLOWED(adapter)) {
2688 err = i40evf_lan_add_device(adapter);
2689 if (err)
2690 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2691 err);
2692 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002693
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002694 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002695 if (netdev->features & NETIF_F_GRO)
2696 dev_info(&pdev->dev, "GRO is enabled\n");
2697
Greg Rose5eae00c2013-12-21 06:12:45 +00002698 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002699 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002700 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002701 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002702
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002703 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2704 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2705 if (!adapter->rss_key || !adapter->rss_lut)
2706 goto err_mem;
2707
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002708 if (RSS_AQ(adapter)) {
2709 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2710 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2711 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002712 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002713 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002714 return;
2715restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002716 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002717 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002718err_mem:
2719 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002720err_register:
2721 i40evf_free_misc_irq(adapter);
2722err_sw_init:
2723 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002724err_alloc:
2725 kfree(adapter->vf_res);
2726 adapter->vf_res = NULL;
2727err:
2728 /* Things went into the weeds, so try again later */
2729 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002730 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002731 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002732 i40evf_shutdown_adminq(hw);
2733 adapter->state = __I40EVF_STARTUP;
2734 schedule_delayed_work(&adapter->init_task, HZ * 5);
2735 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002736 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002737 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002738}
2739
2740/**
2741 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2742 * @pdev: pci device structure
2743 **/
2744static void i40evf_shutdown(struct pci_dev *pdev)
2745{
2746 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002747 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002748
2749 netif_device_detach(netdev);
2750
2751 if (netif_running(netdev))
2752 i40evf_close(netdev);
2753
Mitch Williams00293fd2015-01-09 11:18:18 +00002754 /* Prevent the watchdog from running. */
2755 adapter->state = __I40EVF_REMOVE;
2756 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002757
Greg Rose5eae00c2013-12-21 06:12:45 +00002758#ifdef CONFIG_PM
2759 pci_save_state(pdev);
2760
2761#endif
2762 pci_disable_device(pdev);
2763}
2764
2765/**
2766 * i40evf_probe - Device Initialization Routine
2767 * @pdev: PCI device information struct
2768 * @ent: entry in i40evf_pci_tbl
2769 *
2770 * Returns 0 on success, negative on failure
2771 *
2772 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2773 * The OS initialization, configuring of the adapter private structure,
2774 * and a hardware reset occur.
2775 **/
2776static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2777{
2778 struct net_device *netdev;
2779 struct i40evf_adapter *adapter = NULL;
2780 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002781 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002782
2783 err = pci_enable_device(pdev);
2784 if (err)
2785 return err;
2786
Mitch Williams64942942014-02-11 08:26:33 +00002787 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002788 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002789 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2790 if (err) {
2791 dev_err(&pdev->dev,
2792 "DMA configuration failed: 0x%x\n", err);
2793 goto err_dma;
2794 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002795 }
2796
2797 err = pci_request_regions(pdev, i40evf_driver_name);
2798 if (err) {
2799 dev_err(&pdev->dev,
2800 "pci_request_regions failed 0x%x\n", err);
2801 goto err_pci_reg;
2802 }
2803
2804 pci_enable_pcie_error_reporting(pdev);
2805
2806 pci_set_master(pdev);
2807
Mitch Williams1255b7a2015-11-06 15:25:59 -08002808 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002809 if (!netdev) {
2810 err = -ENOMEM;
2811 goto err_alloc_etherdev;
2812 }
2813
2814 SET_NETDEV_DEV(netdev, &pdev->dev);
2815
2816 pci_set_drvdata(pdev, netdev);
2817 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002818
2819 adapter->netdev = netdev;
2820 adapter->pdev = pdev;
2821
2822 hw = &adapter->hw;
2823 hw->back = adapter;
2824
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002825 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002826 adapter->state = __I40EVF_STARTUP;
2827
2828 /* Call save state here because it relies on the adapter struct. */
2829 pci_save_state(pdev);
2830
2831 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2832 pci_resource_len(pdev, 0));
2833 if (!hw->hw_addr) {
2834 err = -EIO;
2835 goto err_ioremap;
2836 }
2837 hw->vendor_id = pdev->vendor;
2838 hw->device_id = pdev->device;
2839 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2840 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2841 hw->subsystem_device_id = pdev->subsystem_device;
2842 hw->bus.device = PCI_SLOT(pdev->devfn);
2843 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002844 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002845
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002846 /* set up the locks for the AQ, do this only once in probe
2847 * and destroy them only once in remove
2848 */
2849 mutex_init(&hw->aq.asq_mutex);
2850 mutex_init(&hw->aq.arq_mutex);
2851
Serey Kong8bb1a542014-08-01 13:27:15 -07002852 INIT_LIST_HEAD(&adapter->mac_filter_list);
2853 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2854
Greg Rose5eae00c2013-12-21 06:12:45 +00002855 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2856 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2857 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002858 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002859 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002860 schedule_delayed_work(&adapter->init_task,
2861 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002862
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002863 /* Setup the wait queue for indicating transition to down status */
2864 init_waitqueue_head(&adapter->down_waitqueue);
2865
Greg Rose5eae00c2013-12-21 06:12:45 +00002866 return 0;
2867
2868err_ioremap:
2869 free_netdev(netdev);
2870err_alloc_etherdev:
2871 pci_release_regions(pdev);
2872err_pci_reg:
2873err_dma:
2874 pci_disable_device(pdev);
2875 return err;
2876}
2877
2878#ifdef CONFIG_PM
2879/**
2880 * i40evf_suspend - Power management suspend routine
2881 * @pdev: PCI device information struct
2882 * @state: unused
2883 *
2884 * Called when the system (VM) is entering sleep/suspend.
2885 **/
2886static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2887{
2888 struct net_device *netdev = pci_get_drvdata(pdev);
2889 struct i40evf_adapter *adapter = netdev_priv(netdev);
2890 int retval = 0;
2891
2892 netif_device_detach(netdev);
2893
2894 if (netif_running(netdev)) {
2895 rtnl_lock();
2896 i40evf_down(adapter);
2897 rtnl_unlock();
2898 }
2899 i40evf_free_misc_irq(adapter);
2900 i40evf_reset_interrupt_capability(adapter);
2901
2902 retval = pci_save_state(pdev);
2903 if (retval)
2904 return retval;
2905
2906 pci_disable_device(pdev);
2907
2908 return 0;
2909}
2910
2911/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002912 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002913 * @pdev: PCI device information struct
2914 *
2915 * Called when the system (VM) is resumed from sleep/suspend.
2916 **/
2917static int i40evf_resume(struct pci_dev *pdev)
2918{
2919 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2920 struct net_device *netdev = adapter->netdev;
2921 u32 err;
2922
2923 pci_set_power_state(pdev, PCI_D0);
2924 pci_restore_state(pdev);
2925 /* pci_restore_state clears dev->state_saved so call
2926 * pci_save_state to restore it.
2927 */
2928 pci_save_state(pdev);
2929
2930 err = pci_enable_device_mem(pdev);
2931 if (err) {
2932 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2933 return err;
2934 }
2935 pci_set_master(pdev);
2936
2937 rtnl_lock();
2938 err = i40evf_set_interrupt_capability(adapter);
2939 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002940 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002941 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2942 return err;
2943 }
2944 err = i40evf_request_misc_irq(adapter);
2945 rtnl_unlock();
2946 if (err) {
2947 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2948 return err;
2949 }
2950
2951 schedule_work(&adapter->reset_task);
2952
2953 netif_device_attach(netdev);
2954
2955 return err;
2956}
2957
2958#endif /* CONFIG_PM */
2959/**
2960 * i40evf_remove - Device Removal Routine
2961 * @pdev: PCI device information struct
2962 *
2963 * i40evf_remove is called by the PCI subsystem to alert the driver
2964 * that it should release a PCI device. The could be caused by a
2965 * Hot-Plug event, or because the driver is going to be removed from
2966 * memory.
2967 **/
2968static void i40evf_remove(struct pci_dev *pdev)
2969{
2970 struct net_device *netdev = pci_get_drvdata(pdev);
2971 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002972 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002973 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002974 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002975
2976 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002977 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002978 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002979 if (adapter->netdev_registered) {
2980 unregister_netdev(netdev);
2981 adapter->netdev_registered = false;
2982 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002983 if (CLIENT_ALLOWED(adapter)) {
2984 err = i40evf_lan_del_device(adapter);
2985 if (err)
2986 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
2987 err);
2988 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002989
Mitch Williamsf4a71882015-01-09 11:18:16 +00002990 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002991 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002992 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002993 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002994 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002995 /* If the FW isn't responding, kick it once, but only once. */
2996 if (!i40evf_asq_done(hw)) {
2997 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002998 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002999 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003000 i40evf_free_all_tx_resources(adapter);
3001 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003002 i40evf_misc_irq_disable(adapter);
3003 i40evf_free_misc_irq(adapter);
3004 i40evf_reset_interrupt_capability(adapter);
3005 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003006
Mitch Williamse5d17c32014-06-04 04:22:38 +00003007 if (adapter->watchdog_timer.function)
3008 del_timer_sync(&adapter->watchdog_timer);
3009
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003010 flush_scheduled_work();
3011
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003012 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003013
Greg Rose5eae00c2013-12-21 06:12:45 +00003014 if (hw->aq.asq.count)
3015 i40evf_shutdown_adminq(hw);
3016
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003017 /* destroy the locks only once, here */
3018 mutex_destroy(&hw->aq.arq_mutex);
3019 mutex_destroy(&hw->aq.asq_mutex);
3020
Greg Rose5eae00c2013-12-21 06:12:45 +00003021 iounmap(hw->hw_addr);
3022 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003023 i40evf_free_all_tx_resources(adapter);
3024 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003025 i40evf_free_queues(adapter);
3026 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003027 /* If we got removed before an up/down sequence, we've got a filter
3028 * hanging out there that we need to get rid of.
3029 */
3030 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3031 list_del(&f->list);
3032 kfree(f);
3033 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003034 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3035 list_del(&f->list);
3036 kfree(f);
3037 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003038
3039 free_netdev(netdev);
3040
3041 pci_disable_pcie_error_reporting(pdev);
3042
3043 pci_disable_device(pdev);
3044}
3045
3046static struct pci_driver i40evf_driver = {
3047 .name = i40evf_driver_name,
3048 .id_table = i40evf_pci_tbl,
3049 .probe = i40evf_probe,
3050 .remove = i40evf_remove,
3051#ifdef CONFIG_PM
3052 .suspend = i40evf_suspend,
3053 .resume = i40evf_resume,
3054#endif
3055 .shutdown = i40evf_shutdown,
3056};
3057
3058/**
3059 * i40e_init_module - Driver Registration Routine
3060 *
3061 * i40e_init_module is the first routine called when the driver is
3062 * loaded. All it does is register with the PCI subsystem.
3063 **/
3064static int __init i40evf_init_module(void)
3065{
3066 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003067
Greg Rose5eae00c2013-12-21 06:12:45 +00003068 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003069 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003070
3071 pr_info("%s\n", i40evf_copyright);
3072
Jacob Keller6992a6c2016-08-04 11:37:01 -07003073 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3074 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003075 if (!i40evf_wq) {
3076 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3077 return -ENOMEM;
3078 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003079 ret = pci_register_driver(&i40evf_driver);
3080 return ret;
3081}
3082
3083module_init(i40evf_init_module);
3084
3085/**
3086 * i40e_exit_module - Driver Exit Cleanup Routine
3087 *
3088 * i40e_exit_module is called just before the driver is removed
3089 * from memory.
3090 **/
3091static void __exit i40evf_exit_module(void)
3092{
3093 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003094 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003095}
3096
3097module_exit(i40evf_exit_module);
3098
3099/* i40evf_main.c */