blob: 93536b9fc6292452a6420ec67d39d27eeed7258d [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
523 q_vector->affinity_mask = *mask;
524}
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);
Greg Rose5eae00c2013-12-21 06:12:45 +00001146}
1147
1148/**
1149 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1150 * @adapter: board private structure
1151 * @vectors: number of vectors to request
1152 *
1153 * Work with the OS to set up the MSIX vectors needed.
1154 *
1155 * Returns 0 on success, negative on failure
1156 **/
1157static int
1158i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1159{
1160 int err, vector_threshold;
1161
1162 /* We'll want at least 3 (vector_threshold):
1163 * 0) Other (Admin Queue and link, mostly)
1164 * 1) TxQ[0] Cleanup
1165 * 2) RxQ[0] Cleanup
1166 */
1167 vector_threshold = MIN_MSIX_COUNT;
1168
1169 /* The more we get, the more we will assign to Tx/Rx Cleanup
1170 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1171 * Right now, we simply care about how many we'll get; we'll
1172 * set them up later while requesting irq's.
1173 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001174 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1175 vector_threshold, vectors);
1176 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001177 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001178 kfree(adapter->msix_entries);
1179 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001180 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001181 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001182
1183 /* Adjust for only the vectors we'll use, which is minimum
1184 * of max_msix_q_vectors + NONQ_VECS, or the number of
1185 * vectors we were allocated.
1186 */
1187 adapter->num_msix_vectors = err;
1188 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001189}
1190
1191/**
1192 * i40evf_free_queues - Free memory for all rings
1193 * @adapter: board private structure to initialize
1194 *
1195 * Free all of the memory associated with queue pairs.
1196 **/
1197static void i40evf_free_queues(struct i40evf_adapter *adapter)
1198{
Greg Rose5eae00c2013-12-21 06:12:45 +00001199 if (!adapter->vsi_res)
1200 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001201 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001202 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001203 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001204 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001205 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001206}
1207
1208/**
1209 * i40evf_alloc_queues - Allocate memory for all rings
1210 * @adapter: board private structure to initialize
1211 *
1212 * We allocate one ring per queue at run-time since we don't know the
1213 * number of queues at compile-time. The polling_netdev array is
1214 * intended for Multiqueue, but should work fine with a single queue.
1215 **/
1216static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1217{
Jacob Keller65c70062017-06-07 05:43:01 -04001218 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001219
Jacob Keller65c70062017-06-07 05:43:01 -04001220 num_active_queues = min_t(int,
1221 adapter->vsi_res->num_queue_pairs,
1222 (int)(num_online_cpus()));
1223
1224 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001225 sizeof(struct i40e_ring), GFP_KERNEL);
1226 if (!adapter->tx_rings)
1227 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001228 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001229 sizeof(struct i40e_ring), GFP_KERNEL);
1230 if (!adapter->rx_rings)
1231 goto err_out;
1232
Jacob Keller65c70062017-06-07 05:43:01 -04001233 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001234 struct i40e_ring *tx_ring;
1235 struct i40e_ring *rx_ring;
1236
Mitch Williams0dd438d2015-10-26 19:44:40 -04001237 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001238
1239 tx_ring->queue_index = i;
1240 tx_ring->netdev = adapter->netdev;
1241 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001242 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001243 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001244 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1245 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001246
Mitch Williams0dd438d2015-10-26 19:44:40 -04001247 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001248 rx_ring->queue_index = i;
1249 rx_ring->netdev = adapter->netdev;
1250 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001251 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001252 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001253 }
1254
Jacob Keller65c70062017-06-07 05:43:01 -04001255 adapter->num_active_queues = num_active_queues;
1256
Greg Rose5eae00c2013-12-21 06:12:45 +00001257 return 0;
1258
1259err_out:
1260 i40evf_free_queues(adapter);
1261 return -ENOMEM;
1262}
1263
1264/**
1265 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1266 * @adapter: board private structure to initialize
1267 *
1268 * Attempt to configure the interrupts using the best available
1269 * capabilities of the hardware and the kernel.
1270 **/
1271static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1272{
1273 int vector, v_budget;
1274 int pairs = 0;
1275 int err = 0;
1276
1277 if (!adapter->vsi_res) {
1278 err = -EIO;
1279 goto out;
1280 }
Mitch Williamscc052922014-10-25 03:24:34 +00001281 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001282
Jacob Keller789f38c2017-04-19 09:25:56 -04001283 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1284 * us much good if we have more vectors than CPUs. However, we already
1285 * limit the total number of queues by the number of CPUs so we do not
1286 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001287 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001288 v_budget = min_t(int, pairs + NONQ_VECS,
1289 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001290
Greg Rose5eae00c2013-12-21 06:12:45 +00001291 adapter->msix_entries = kcalloc(v_budget,
1292 sizeof(struct msix_entry), GFP_KERNEL);
1293 if (!adapter->msix_entries) {
1294 err = -ENOMEM;
1295 goto out;
1296 }
1297
1298 for (vector = 0; vector < v_budget; vector++)
1299 adapter->msix_entries[vector].entry = vector;
1300
Mitch Williams313ed2d2015-08-27 11:42:31 -04001301 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001302
1303out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001304 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1305 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001306 return err;
1307}
1308
1309/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1311 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001312 *
1313 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001314 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001315static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001316{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001317 struct i40e_aqc_get_set_rss_key_data *rss_key =
1318 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001320 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001321
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001322 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001323 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001324 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001325 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001326 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001327 }
1328
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001329 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1330 if (ret) {
1331 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1332 i40evf_stat_str(hw, ret),
1333 i40evf_aq_str(hw, hw->aq.asq_last_status));
1334 return ret;
1335
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001336 }
1337
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001338 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1339 adapter->rss_lut, adapter->rss_lut_size);
1340 if (ret) {
1341 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1342 i40evf_stat_str(hw, ret),
1343 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001344 }
1345
1346 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001347
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001348}
1349
1350/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001351 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001352 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001353 *
1354 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001355 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001356static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001357{
1358 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001359 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001360 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001361
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001362 dw = (u32 *)adapter->rss_key;
1363 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1364 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001365
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001366 dw = (u32 *)adapter->rss_lut;
1367 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1368 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001369
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001370 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001371
1372 return 0;
1373}
1374
1375/**
1376 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001377 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001378 *
1379 * Returns 0 on success, negative on failure
1380 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001381int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001382{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001383
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001384 if (RSS_PF(adapter)) {
1385 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1386 I40EVF_FLAG_AQ_SET_RSS_KEY;
1387 return 0;
1388 } else if (RSS_AQ(adapter)) {
1389 return i40evf_config_rss_aq(adapter);
1390 } else {
1391 return i40evf_config_rss_reg(adapter);
1392 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001393}
1394
1395/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001396 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001397 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001398 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001399static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001400{
1401 u16 i;
1402
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001403 for (i = 0; i < adapter->rss_lut_size; i++)
1404 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001405}
1406
1407/**
Helin Zhang96a81982015-10-26 19:44:31 -04001408 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001409 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001410 *
1411 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001412 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001413static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001414{
1415 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001416 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001417
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001418 if (!RSS_PF(adapter)) {
1419 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1420 if (adapter->vf_res->vf_offload_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001421 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001422 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1423 else
1424 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001425
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001426 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1427 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1428 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001429
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001430 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001431
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001432 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1433 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001434
1435 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001436}
1437
1438/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001439 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1440 * @adapter: board private structure to initialize
1441 *
1442 * We allocate one q_vector per queue interrupt. If allocation fails we
1443 * return -ENOMEM.
1444 **/
1445static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1446{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001447 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001448 struct i40e_q_vector *q_vector;
1449
1450 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001451 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001452 GFP_KERNEL);
1453 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001454 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001455
1456 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001457 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001458 q_vector->adapter = adapter;
1459 q_vector->vsi = &adapter->vsi;
1460 q_vector->v_idx = q_idx;
1461 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001462 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001463 }
1464
1465 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001466}
1467
1468/**
1469 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1470 * @adapter: board private structure to initialize
1471 *
1472 * This function frees the memory allocated to the q_vectors. In addition if
1473 * NAPI is enabled it will delete any references to the NAPI struct prior
1474 * to freeing the q_vector.
1475 **/
1476static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1477{
1478 int q_idx, num_q_vectors;
1479 int napi_vectors;
1480
Jacob Kelleref4603e2016-11-08 13:05:08 -08001481 if (!adapter->q_vectors)
1482 return;
1483
Greg Rose5eae00c2013-12-21 06:12:45 +00001484 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001485 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001486
1487 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001488 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001489 if (q_idx < napi_vectors)
1490 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001491 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001492 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001493 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001494}
1495
1496/**
1497 * i40evf_reset_interrupt_capability - Reset MSIX setup
1498 * @adapter: board private structure
1499 *
1500 **/
1501void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1502{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001503 if (!adapter->msix_entries)
1504 return;
1505
Greg Rose5eae00c2013-12-21 06:12:45 +00001506 pci_disable_msix(adapter->pdev);
1507 kfree(adapter->msix_entries);
1508 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001509}
1510
1511/**
1512 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1513 * @adapter: board private structure to initialize
1514 *
1515 **/
1516int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1517{
1518 int err;
1519
Jacob Keller283aeaf2017-04-19 09:25:59 -04001520 err = i40evf_alloc_queues(adapter);
1521 if (err) {
1522 dev_err(&adapter->pdev->dev,
1523 "Unable to allocate memory for queues\n");
1524 goto err_alloc_queues;
1525 }
1526
Jacob Keller62fe2a82016-07-27 12:02:33 -07001527 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001528 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001529 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001530 if (err) {
1531 dev_err(&adapter->pdev->dev,
1532 "Unable to setup interrupt capabilities\n");
1533 goto err_set_interrupt;
1534 }
1535
1536 err = i40evf_alloc_q_vectors(adapter);
1537 if (err) {
1538 dev_err(&adapter->pdev->dev,
1539 "Unable to allocate memory for queue vectors\n");
1540 goto err_alloc_q_vectors;
1541 }
1542
Greg Rose5eae00c2013-12-21 06:12:45 +00001543 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001544 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1545 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001546
1547 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001548err_alloc_q_vectors:
1549 i40evf_reset_interrupt_capability(adapter);
1550err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001551 i40evf_free_queues(adapter);
1552err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001553 return err;
1554}
1555
1556/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001557 * i40evf_free_rss - Free memory used by RSS structs
1558 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001559 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001560static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001561{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001562 kfree(adapter->rss_key);
1563 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001564
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001565 kfree(adapter->rss_lut);
1566 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001567}
1568
1569/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001570 * i40evf_watchdog_timer - Periodic call-back timer
1571 * @data: pointer to adapter disguised as unsigned long
1572 **/
1573static void i40evf_watchdog_timer(unsigned long data)
1574{
1575 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001576
Greg Rose5eae00c2013-12-21 06:12:45 +00001577 schedule_work(&adapter->watchdog_task);
1578 /* timer will be rescheduled in watchdog task */
1579}
1580
1581/**
1582 * i40evf_watchdog_task - Periodic call-back task
1583 * @work: pointer to work_struct
1584 **/
1585static void i40evf_watchdog_task(struct work_struct *work)
1586{
1587 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001588 struct i40evf_adapter,
1589 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001590 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001591 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001592
Greg Rose5eae00c2013-12-21 06:12:45 +00001593 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001594 goto restart_watchdog;
1595
1596 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001597 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1598 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001599 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1600 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001601 /* A chance for redemption! */
1602 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1603 adapter->state = __I40EVF_STARTUP;
1604 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1605 schedule_delayed_work(&adapter->init_task, 10);
1606 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1607 &adapter->crit_section);
1608 /* Don't reschedule the watchdog, since we've restarted
1609 * the init task. When init_task contacts the PF and
1610 * gets everything set up again, it'll restart the
1611 * watchdog for us. Down, boy. Sit. Stay. Woof.
1612 */
1613 return;
1614 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001615 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001616 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001617 goto watchdog_done;
1618 }
1619
1620 if ((adapter->state < __I40EVF_DOWN) ||
1621 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001622 goto watchdog_done;
1623
Mitch Williamsef8693e2014-02-13 03:48:53 -08001624 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001625 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1626 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001627 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001628 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001629 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001630 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001631 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001632 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001633 goto watchdog_done;
1634 }
1635
1636 /* Process admin queue tasks. After init, everything gets done
1637 * here so we don't race on the admin queue.
1638 */
Mitch Williamsed636962015-04-07 19:45:32 -04001639 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001640 if (!i40evf_asq_done(hw)) {
1641 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1642 i40evf_send_api_ver(adapter);
1643 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001644 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001645 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001646 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1647 i40evf_send_vf_config_msg(adapter);
1648 goto watchdog_done;
1649 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001650
Mitch Williamse284fc82015-03-27 00:12:09 -07001651 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1652 i40evf_disable_queues(adapter);
1653 goto watchdog_done;
1654 }
1655
Greg Rose5eae00c2013-12-21 06:12:45 +00001656 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1657 i40evf_map_queues(adapter);
1658 goto watchdog_done;
1659 }
1660
1661 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1662 i40evf_add_ether_addrs(adapter);
1663 goto watchdog_done;
1664 }
1665
1666 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1667 i40evf_add_vlans(adapter);
1668 goto watchdog_done;
1669 }
1670
1671 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1672 i40evf_del_ether_addrs(adapter);
1673 goto watchdog_done;
1674 }
1675
1676 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1677 i40evf_del_vlans(adapter);
1678 goto watchdog_done;
1679 }
1680
Greg Rose5eae00c2013-12-21 06:12:45 +00001681 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1682 i40evf_configure_queues(adapter);
1683 goto watchdog_done;
1684 }
1685
1686 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1687 i40evf_enable_queues(adapter);
1688 goto watchdog_done;
1689 }
1690
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001691 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1692 /* This message goes straight to the firmware, not the
1693 * PF, so we don't have to set current_op as we will
1694 * not get a response through the ARQ.
1695 */
Helin Zhang96a81982015-10-26 19:44:31 -04001696 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001697 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1698 goto watchdog_done;
1699 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001700 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1701 i40evf_get_hena(adapter);
1702 goto watchdog_done;
1703 }
1704 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1705 i40evf_set_hena(adapter);
1706 goto watchdog_done;
1707 }
1708 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1709 i40evf_set_rss_key(adapter);
1710 goto watchdog_done;
1711 }
1712 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1713 i40evf_set_rss_lut(adapter);
1714 goto watchdog_done;
1715 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001716
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001717 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001718 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1719 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001720 goto watchdog_done;
1721 }
1722
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001723 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001724 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001725 goto watchdog_done;
1726 }
1727
1728 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1729 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001730 i40evf_set_promiscuous(adapter, 0);
1731 goto watchdog_done;
1732 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001733 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001734
Greg Rose5eae00c2013-12-21 06:12:45 +00001735 if (adapter->state == __I40EVF_RUNNING)
1736 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001737watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001738 if (adapter->state == __I40EVF_RUNNING) {
1739 i40evf_irq_enable_queues(adapter, ~0);
1740 i40evf_fire_sw_int(adapter, 0xFF);
1741 } else {
1742 i40evf_fire_sw_int(adapter, 0x1);
1743 }
1744
Mitch Williamsef8693e2014-02-13 03:48:53 -08001745 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1746restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001747 if (adapter->state == __I40EVF_REMOVE)
1748 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001749 if (adapter->aq_required)
1750 mod_timer(&adapter->watchdog_timer,
1751 jiffies + msecs_to_jiffies(20));
1752 else
1753 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001754 schedule_work(&adapter->adminq_task);
1755}
1756
Joe Perchesdedecb62016-11-01 15:35:14 -07001757static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1758{
1759 struct i40evf_mac_filter *f, *ftmp;
1760 struct i40evf_vlan_filter *fv, *fvtmp;
1761
1762 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1763
1764 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001765 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001766 netif_carrier_off(adapter->netdev);
1767 netif_tx_disable(adapter->netdev);
1768 adapter->link_up = false;
1769 i40evf_napi_disable_all(adapter);
1770 i40evf_irq_disable(adapter);
1771 i40evf_free_traffic_irqs(adapter);
1772 i40evf_free_all_tx_resources(adapter);
1773 i40evf_free_all_rx_resources(adapter);
1774 }
1775
1776 /* Delete all of the filters, both MAC and VLAN. */
1777 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1778 list_del(&f->list);
1779 kfree(f);
1780 }
1781
1782 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1783 list_del(&fv->list);
1784 kfree(fv);
1785 }
1786
1787 i40evf_free_misc_irq(adapter);
1788 i40evf_reset_interrupt_capability(adapter);
1789 i40evf_free_queues(adapter);
1790 i40evf_free_q_vectors(adapter);
1791 kfree(adapter->vf_res);
1792 i40evf_shutdown_adminq(&adapter->hw);
1793 adapter->netdev->flags &= ~IFF_UP;
1794 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1795 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1796 adapter->state = __I40EVF_DOWN;
1797 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1798}
1799
Mitch Williams67c818a2015-06-19 08:56:30 -07001800#define I40EVF_RESET_WAIT_MS 10
1801#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001802/**
1803 * i40evf_reset_task - Call-back task to handle hardware reset
1804 * @work: pointer to work_struct
1805 *
1806 * During reset we need to shut down and reinitialize the admin queue
1807 * before we can use it to communicate with the PF again. We also clear
1808 * and reinit the rings because that context is lost as well.
1809 **/
1810static void i40evf_reset_task(struct work_struct *work)
1811{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001812 struct i40evf_adapter *adapter = container_of(work,
1813 struct i40evf_adapter,
1814 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001815 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001816 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001817 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001818 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001819 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001820 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001821
Mitch Williamsed0e8942017-01-24 10:23:59 -08001822 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001823 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001824 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001825 if (CLIENT_ENABLED(adapter)) {
1826 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1827 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1828 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1829 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1830 cancel_delayed_work_sync(&adapter->client_task);
1831 i40evf_notify_client_close(&adapter->vsi, true);
1832 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001833 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001834 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001835 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1836 /* Restart the AQ here. If we have been reset but didn't
1837 * detect it, or if the PF had to reinit, our AQ will be hosed.
1838 */
1839 i40evf_shutdown_adminq(hw);
1840 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001841 i40evf_request_reset(adapter);
1842 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001843 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001844
Mitch Williamsef8693e2014-02-13 03:48:53 -08001845 /* poll until we see the reset actually happen */
1846 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001847 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1848 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1849 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001850 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001851 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001852 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001853 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001854 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001855 goto continue_reset; /* act like the reset happened */
1856 }
1857
1858 /* wait until the reset is complete and the PF is responding to us */
1859 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001860 /* sleep first to make sure a minimum wait time is met */
1861 msleep(I40EVF_RESET_WAIT_MS);
1862
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001863 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1864 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001865 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001866 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001867 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001868
Mitch Williams509a4472015-12-23 12:05:52 -08001869 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001870
Mitch Williamsef8693e2014-02-13 03:48:53 -08001871 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001872 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001873 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001874 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001875 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001876 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001877 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001878
1879continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001880 if (netif_running(adapter->netdev)) {
1881 netif_carrier_off(netdev);
1882 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001883 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001884 i40evf_napi_disable_all(adapter);
1885 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001886 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001887
Greg Rose5eae00c2013-12-21 06:12:45 +00001888 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001889 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1890
1891 /* free the Tx/Rx rings and descriptors, might be better to just
1892 * re-use them sometime in the future
1893 */
1894 i40evf_free_all_rx_resources(adapter);
1895 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001896
1897 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001898 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001899 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001900 err = i40evf_init_adminq(hw);
1901 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001902 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1903 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001904
Mitch Williamse6d038d2015-06-04 16:23:58 -04001905 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1906 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001907
1908 /* re-add all MAC filters */
1909 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1910 f->add = true;
1911 }
1912 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001913 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1914 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001915 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001916 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001917 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001918 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001919 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001920 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001921
1922 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1923
1924 if (netif_running(adapter->netdev)) {
1925 /* allocate transmit descriptors */
1926 err = i40evf_setup_all_tx_resources(adapter);
1927 if (err)
1928 goto reset_err;
1929
1930 /* allocate receive descriptors */
1931 err = i40evf_setup_all_rx_resources(adapter);
1932 if (err)
1933 goto reset_err;
1934
1935 i40evf_configure(adapter);
1936
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001937 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001938
1939 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001940 } else {
1941 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001942 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001943
Greg Rose5eae00c2013-12-21 06:12:45 +00001944 return;
1945reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001946 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001947 i40evf_close(adapter->netdev);
1948}
1949
1950/**
1951 * i40evf_adminq_task - worker thread to clean the admin queue
1952 * @work: pointer to work_struct containing our data
1953 **/
1954static void i40evf_adminq_task(struct work_struct *work)
1955{
1956 struct i40evf_adapter *adapter =
1957 container_of(work, struct i40evf_adapter, adminq_task);
1958 struct i40e_hw *hw = &adapter->hw;
1959 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001960 enum virtchnl_ops v_op;
1961 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001962 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001963 u16 pending;
1964
Mitch Williamsef8693e2014-02-13 03:48:53 -08001965 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001966 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001967
Mitch Williams1001dc32014-11-11 20:02:19 +00001968 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1969 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001970 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001971 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001972
Greg Rose5eae00c2013-12-21 06:12:45 +00001973 do {
1974 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07001975 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
1976 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
1977
1978 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00001979 break; /* No event to process or error cleaning ARQ */
1980
Tushar Davec969ef42017-06-22 09:44:32 -07001981 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001982 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001983 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001984 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001985 } while (pending);
1986
Mitch Williams67c818a2015-06-19 08:56:30 -07001987 if ((adapter->flags &
1988 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1989 adapter->state == __I40EVF_RESETTING)
1990 goto freedom;
1991
Mitch Williams912257e2014-05-22 06:32:07 +00001992 /* check for error indications */
1993 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001994 if (val == 0xdeadbeef) /* indicates device in reset */
1995 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001996 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001997 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001998 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001999 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002000 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002001 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002002 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002003 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002004 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002005 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002006 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002007 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002008 }
2009 if (oldval != val)
2010 wr32(hw, hw->aq.arq.len, val);
2011
2012 val = rd32(hw, hw->aq.asq.len);
2013 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002014 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002015 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002016 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002017 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002018 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002019 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002020 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002021 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002022 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002023 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002024 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002025 }
2026 if (oldval != val)
2027 wr32(hw, hw->aq.asq.len, val);
2028
Mitch Williams67c818a2015-06-19 08:56:30 -07002029freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002030 kfree(event.msg_buf);
2031out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002032 /* re-enable Admin queue interrupt cause */
2033 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002034}
2035
2036/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002037 * i40evf_client_task - worker thread to perform client work
2038 * @work: pointer to work_struct containing our data
2039 *
2040 * This task handles client interactions. Because client calls can be
2041 * reentrant, we can't handle them in the watchdog.
2042 **/
2043static void i40evf_client_task(struct work_struct *work)
2044{
2045 struct i40evf_adapter *adapter =
2046 container_of(work, struct i40evf_adapter, client_task.work);
2047
2048 /* If we can't get the client bit, just give up. We'll be rescheduled
2049 * later.
2050 */
2051
2052 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2053 return;
2054
2055 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2056 i40evf_client_subtask(adapter);
2057 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2058 goto out;
2059 }
2060 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2061 i40evf_notify_client_close(&adapter->vsi, false);
2062 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2063 goto out;
2064 }
2065 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2066 i40evf_notify_client_open(&adapter->vsi);
2067 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2068 goto out;
2069 }
2070 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2071 i40evf_notify_client_l2_params(&adapter->vsi);
2072 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2073 }
2074out:
2075 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2076}
2077
2078/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002079 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2080 * @adapter: board private structure
2081 *
2082 * Free all transmit software resources
2083 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002084void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002085{
2086 int i;
2087
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002088 if (!adapter->tx_rings)
2089 return;
2090
Mitch Williamscc052922014-10-25 03:24:34 +00002091 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002092 if (adapter->tx_rings[i].desc)
2093 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002094}
2095
2096/**
2097 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2098 * @adapter: board private structure
2099 *
2100 * If this function returns with an error, then it's possible one or
2101 * more of the rings is populated (while the rest are not). It is the
2102 * callers duty to clean those orphaned rings.
2103 *
2104 * Return 0 on success, negative on failure
2105 **/
2106static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2107{
2108 int i, err = 0;
2109
Mitch Williamscc052922014-10-25 03:24:34 +00002110 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002111 adapter->tx_rings[i].count = adapter->tx_desc_count;
2112 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002113 if (!err)
2114 continue;
2115 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002116 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002117 break;
2118 }
2119
2120 return err;
2121}
2122
2123/**
2124 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2125 * @adapter: board private structure
2126 *
2127 * If this function returns with an error, then it's possible one or
2128 * more of the rings is populated (while the rest are not). It is the
2129 * callers duty to clean those orphaned rings.
2130 *
2131 * Return 0 on success, negative on failure
2132 **/
2133static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2134{
2135 int i, err = 0;
2136
Mitch Williamscc052922014-10-25 03:24:34 +00002137 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002138 adapter->rx_rings[i].count = adapter->rx_desc_count;
2139 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002140 if (!err)
2141 continue;
2142 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002143 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002144 break;
2145 }
2146 return err;
2147}
2148
2149/**
2150 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2151 * @adapter: board private structure
2152 *
2153 * Free all receive software resources
2154 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002155void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002156{
2157 int i;
2158
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002159 if (!adapter->rx_rings)
2160 return;
2161
Mitch Williamscc052922014-10-25 03:24:34 +00002162 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002163 if (adapter->rx_rings[i].desc)
2164 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002165}
2166
2167/**
2168 * i40evf_open - Called when a network interface is made active
2169 * @netdev: network interface device structure
2170 *
2171 * Returns 0 on success, negative value on failure
2172 *
2173 * The open entry point is called when a network interface is made
2174 * active by the system (IFF_UP). At this point all resources needed
2175 * for transmit and receive operations are allocated, the interrupt
2176 * handler is registered with the OS, the watchdog timer is started,
2177 * and the stack is notified that the interface is ready.
2178 **/
2179static int i40evf_open(struct net_device *netdev)
2180{
2181 struct i40evf_adapter *adapter = netdev_priv(netdev);
2182 int err;
2183
Mitch Williamsef8693e2014-02-13 03:48:53 -08002184 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2185 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2186 return -EIO;
2187 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002188
2189 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002190 return -EBUSY;
2191
2192 /* allocate transmit descriptors */
2193 err = i40evf_setup_all_tx_resources(adapter);
2194 if (err)
2195 goto err_setup_tx;
2196
2197 /* allocate receive descriptors */
2198 err = i40evf_setup_all_rx_resources(adapter);
2199 if (err)
2200 goto err_setup_rx;
2201
2202 /* clear any pending interrupts, may auto mask */
2203 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2204 if (err)
2205 goto err_req_irq;
2206
Mitch Williams44151cd2015-04-27 14:57:17 -04002207 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002208 i40evf_configure(adapter);
2209
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002210 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002211
2212 i40evf_irq_enable(adapter, true);
2213
2214 return 0;
2215
2216err_req_irq:
2217 i40evf_down(adapter);
2218 i40evf_free_traffic_irqs(adapter);
2219err_setup_rx:
2220 i40evf_free_all_rx_resources(adapter);
2221err_setup_tx:
2222 i40evf_free_all_tx_resources(adapter);
2223
2224 return err;
2225}
2226
2227/**
2228 * i40evf_close - Disables a network interface
2229 * @netdev: network interface device structure
2230 *
2231 * Returns 0, this is not allowed to fail
2232 *
2233 * The close entry point is called when an interface is de-activated
2234 * by the OS. The hardware is still under the drivers control, but
2235 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2236 * are freed, along with all transmit and receive resources.
2237 **/
2238static int i40evf_close(struct net_device *netdev)
2239{
2240 struct i40evf_adapter *adapter = netdev_priv(netdev);
2241
Mitch Williams209dc4d2015-12-09 15:50:27 -08002242 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002243 return 0;
2244
Mitch Williamsef8693e2014-02-13 03:48:53 -08002245
Jacob Keller0da36b92017-04-19 09:25:55 -04002246 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002247 if (CLIENT_ENABLED(adapter))
2248 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002249
2250 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002251 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002252 i40evf_free_traffic_irqs(adapter);
2253
Mitch Williams51f38262016-12-12 15:44:11 -08002254 /* We explicitly don't free resources here because the hardware is
2255 * still active and can DMA into memory. Resources are cleared in
2256 * i40evf_virtchnl_completion() after we get confirmation from the PF
2257 * driver that the rings have been stopped.
2258 */
Greg Rose5eae00c2013-12-21 06:12:45 +00002259 return 0;
2260}
2261
2262/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002263 * i40evf_change_mtu - Change the Maximum Transfer Unit
2264 * @netdev: network interface device structure
2265 * @new_mtu: new value for maximum frame size
2266 *
2267 * Returns 0 on success, negative on failure
2268 **/
2269static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2270{
2271 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002272
Greg Rose5eae00c2013-12-21 06:12:45 +00002273 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002274 if (CLIENT_ENABLED(adapter)) {
2275 i40evf_notify_client_l2_params(&adapter->vsi);
2276 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2277 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002278 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2279 schedule_work(&adapter->reset_task);
2280
Greg Rose5eae00c2013-12-21 06:12:45 +00002281 return 0;
2282}
2283
Alexander Duyck06fc0162016-10-25 16:08:47 -07002284/**
2285 * i40evf_features_check - Validate encapsulated packet conforms to limits
2286 * @skb: skb buff
2287 * @netdev: This physical port's netdev
2288 * @features: Offload features that the stack believes apply
2289 **/
2290static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2291 struct net_device *dev,
2292 netdev_features_t features)
2293{
2294 size_t len;
2295
2296 /* No point in doing any of this if neither checksum nor GSO are
2297 * being requested for this frame. We can rule out both by just
2298 * checking for CHECKSUM_PARTIAL
2299 */
2300 if (skb->ip_summed != CHECKSUM_PARTIAL)
2301 return features;
2302
2303 /* We cannot support GSO if the MSS is going to be less than
2304 * 64 bytes. If it is then we need to drop support for GSO.
2305 */
2306 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2307 features &= ~NETIF_F_GSO_MASK;
2308
2309 /* MACLEN can support at most 63 words */
2310 len = skb_network_header(skb) - skb->data;
2311 if (len & ~(63 * 2))
2312 goto out_err;
2313
2314 /* IPLEN and EIPLEN can support at most 127 dwords */
2315 len = skb_transport_header(skb) - skb_network_header(skb);
2316 if (len & ~(127 * 4))
2317 goto out_err;
2318
2319 if (skb->encapsulation) {
2320 /* L4TUNLEN can support 127 words */
2321 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2322 if (len & ~(127 * 2))
2323 goto out_err;
2324
2325 /* IPLEN can support at most 127 dwords */
2326 len = skb_inner_transport_header(skb) -
2327 skb_inner_network_header(skb);
2328 if (len & ~(127 * 4))
2329 goto out_err;
2330 }
2331
2332 /* No need to validate L4LEN as TCP is the only protocol with a
2333 * a flexible value and we support all possible values supported
2334 * by TCP, which is at most 15 dwords
2335 */
2336
2337 return features;
2338out_err:
2339 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2340}
2341
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002342#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2343 NETIF_F_HW_VLAN_CTAG_RX |\
2344 NETIF_F_HW_VLAN_CTAG_FILTER)
2345
2346/**
2347 * i40evf_fix_features - fix up the netdev feature bits
2348 * @netdev: our net device
2349 * @features: desired feature bits
2350 *
2351 * Returns fixed-up features bits
2352 **/
2353static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2354 netdev_features_t features)
2355{
2356 struct i40evf_adapter *adapter = netdev_priv(netdev);
2357
2358 features &= ~I40EVF_VLAN_FEATURES;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002359 if (adapter->vf_res->vf_offload_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002360 features |= I40EVF_VLAN_FEATURES;
2361 return features;
2362}
2363
Greg Rose5eae00c2013-12-21 06:12:45 +00002364static const struct net_device_ops i40evf_netdev_ops = {
2365 .ndo_open = i40evf_open,
2366 .ndo_stop = i40evf_close,
2367 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002368 .ndo_set_rx_mode = i40evf_set_rx_mode,
2369 .ndo_validate_addr = eth_validate_addr,
2370 .ndo_set_mac_address = i40evf_set_mac,
2371 .ndo_change_mtu = i40evf_change_mtu,
2372 .ndo_tx_timeout = i40evf_tx_timeout,
2373 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2374 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002375 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002376 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002377#ifdef CONFIG_NET_POLL_CONTROLLER
2378 .ndo_poll_controller = i40evf_netpoll,
2379#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002380};
2381
2382/**
2383 * i40evf_check_reset_complete - check that VF reset is complete
2384 * @hw: pointer to hw struct
2385 *
2386 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2387 **/
2388static int i40evf_check_reset_complete(struct i40e_hw *hw)
2389{
2390 u32 rstat;
2391 int i;
2392
2393 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002394 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2395 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002396 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2397 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002398 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002399 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002400 }
2401 return -EBUSY;
2402}
2403
2404/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002405 * i40evf_process_config - Process the config information we got from the PF
2406 * @adapter: board private structure
2407 *
2408 * Verify that we have a valid config struct, and set up our netdev features
2409 * and our VSI struct.
2410 **/
2411int i40evf_process_config(struct i40evf_adapter *adapter)
2412{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002413 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002414 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002415 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002416 int i;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002417 netdev_features_t hw_enc_features;
2418 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002419
2420 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002421 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002422 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002423 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002424 }
2425 if (!adapter->vsi_res) {
2426 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2427 return -ENODEV;
2428 }
2429
Preethi Banalabacd75c2017-03-27 14:43:18 -07002430 hw_enc_features = NETIF_F_SG |
2431 NETIF_F_IP_CSUM |
2432 NETIF_F_IPV6_CSUM |
2433 NETIF_F_HIGHDMA |
2434 NETIF_F_SOFT_FEATURES |
2435 NETIF_F_TSO |
2436 NETIF_F_TSO_ECN |
2437 NETIF_F_TSO6 |
2438 NETIF_F_SCTP_CRC |
2439 NETIF_F_RXHASH |
2440 NETIF_F_RXCSUM |
2441 0;
2442
2443 /* advertise to stack only if offloads for encapsulated packets is
2444 * supported
2445 */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002446 if (vfres->vf_offload_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002447 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002448 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002449 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002450 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002451 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002452 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002453 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002454 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002455
Preethi Banalabacd75c2017-03-27 14:43:18 -07002456 if (!(vfres->vf_offload_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002457 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002458 netdev->gso_partial_features |=
2459 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002460
Preethi Banalabacd75c2017-03-27 14:43:18 -07002461 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2462 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2463 netdev->hw_enc_features |= hw_enc_features;
2464 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002465 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002466 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002467
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002468 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002469 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002470 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002471 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002472
Preethi Banalabacd75c2017-03-27 14:43:18 -07002473 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002474
Preethi Banalabacd75c2017-03-27 14:43:18 -07002475 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002476
2477 adapter->vsi.id = adapter->vsi_res->vsi_id;
2478
2479 adapter->vsi.back = adapter;
2480 adapter->vsi.base_vector = 1;
2481 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002482 vsi->netdev = adapter->netdev;
2483 vsi->qs_handle = adapter->vsi_res->qset_handle;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002484 if (vfres->vf_offload_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002485 adapter->rss_key_size = vfres->rss_key_size;
2486 adapter->rss_lut_size = vfres->rss_lut_size;
2487 } else {
2488 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2489 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2490 }
2491
Mitch Williamse6d038d2015-06-04 16:23:58 -04002492 return 0;
2493}
2494
2495/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002496 * i40evf_init_task - worker thread to perform delayed initialization
2497 * @work: pointer to work_struct containing our data
2498 *
2499 * This task completes the work that was begun in probe. Due to the nature
2500 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002501 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002502 * whole system, we'll do it in a task so we can sleep.
2503 * This task only runs during driver init. Once we've established
2504 * communications with the PF driver and set up our netdev, the watchdog
2505 * takes over.
2506 **/
2507static void i40evf_init_task(struct work_struct *work)
2508{
2509 struct i40evf_adapter *adapter = container_of(work,
2510 struct i40evf_adapter,
2511 init_task.work);
2512 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002513 struct i40e_hw *hw = &adapter->hw;
2514 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002515 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002516
2517 switch (adapter->state) {
2518 case __I40EVF_STARTUP:
2519 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002520 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2521 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002522 err = i40e_set_mac_type(hw);
2523 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002524 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2525 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002526 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002527 }
2528 err = i40evf_check_reset_complete(hw);
2529 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002530 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002531 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002532 goto err;
2533 }
2534 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2535 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2536 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2537 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2538
2539 err = i40evf_init_adminq(hw);
2540 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002541 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2542 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002543 goto err;
2544 }
2545 err = i40evf_send_api_ver(adapter);
2546 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002547 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002548 i40evf_shutdown_adminq(hw);
2549 goto err;
2550 }
2551 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2552 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002553 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002554 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002555 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002556 i40evf_shutdown_adminq(hw);
2557 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002558 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002559 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002560
2561 /* aq msg sent, awaiting reply */
2562 err = i40evf_verify_api_ver(adapter);
2563 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002564 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002565 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002566 else
2567 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2568 adapter->pf_version.major,
2569 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002570 VIRTCHNL_VERSION_MAJOR,
2571 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002572 goto err;
2573 }
2574 err = i40evf_send_vf_config_msg(adapter);
2575 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002576 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002577 err);
2578 goto err;
2579 }
2580 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2581 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002582 case __I40EVF_INIT_GET_RESOURCES:
2583 /* aq msg sent, awaiting reply */
2584 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002585 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002586 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002587 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002588 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002589 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002590 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002591 }
2592 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002593 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002594 err = i40evf_send_vf_config_msg(adapter);
2595 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002596 } else if (err == I40E_ERR_PARAM) {
2597 /* We only get ERR_PARAM if the device is in a very bad
2598 * state or if we've been disabled for previous bad
2599 * behavior. Either way, we're done now.
2600 */
2601 i40evf_shutdown_adminq(hw);
2602 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2603 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002604 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002605 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002606 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2607 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002608 goto err_alloc;
2609 }
2610 adapter->state = __I40EVF_INIT_SW;
2611 break;
2612 default:
2613 goto err_alloc;
2614 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002615
Mitch Williamse6d038d2015-06-04 16:23:58 -04002616 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002617 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002618 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002619
2620 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2621
Greg Rose5eae00c2013-12-21 06:12:45 +00002622 netdev->netdev_ops = &i40evf_netdev_ops;
2623 i40evf_set_ethtool_ops(netdev);
2624 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002625
Jarod Wilson91c527a2016-10-17 15:54:05 -04002626 /* MTU range: 68 - 9710 */
2627 netdev->min_mtu = ETH_MIN_MTU;
2628 netdev->max_mtu = I40E_MAX_RXBUFFER - (ETH_HLEN + ETH_FCS_LEN);
2629
Greg Rose5eae00c2013-12-21 06:12:45 +00002630 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002631 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002632 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002633 eth_hw_addr_random(netdev);
2634 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2635 } else {
2636 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2637 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2638 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002639 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002640
Greg Rose5eae00c2013-12-21 06:12:45 +00002641 init_timer(&adapter->watchdog_timer);
2642 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2643 adapter->watchdog_timer.data = (unsigned long)adapter;
2644 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2645
Mitch Williamsd732a182014-04-24 06:41:37 +00002646 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2647 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002648 err = i40evf_init_interrupt_scheme(adapter);
2649 if (err)
2650 goto err_sw_init;
2651 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002652 if (adapter->vf_res->vf_offload_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002653 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002654 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2655
Greg Rose5eae00c2013-12-21 06:12:45 +00002656 err = i40evf_request_misc_irq(adapter);
2657 if (err)
2658 goto err_sw_init;
2659
2660 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002661 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002662
Mitch Williamsef8693e2014-02-13 03:48:53 -08002663 if (!adapter->netdev_registered) {
2664 err = register_netdev(netdev);
2665 if (err)
2666 goto err_register;
2667 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002668
2669 adapter->netdev_registered = true;
2670
2671 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002672 if (CLIENT_ALLOWED(adapter)) {
2673 err = i40evf_lan_add_device(adapter);
2674 if (err)
2675 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2676 err);
2677 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002678
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002679 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002680 if (netdev->features & NETIF_F_GRO)
2681 dev_info(&pdev->dev, "GRO is enabled\n");
2682
Greg Rose5eae00c2013-12-21 06:12:45 +00002683 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002684 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002685 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002686
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002687 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2688 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2689 if (!adapter->rss_key || !adapter->rss_lut)
2690 goto err_mem;
2691
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002692 if (RSS_AQ(adapter)) {
2693 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2694 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2695 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002696 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002697 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002698 return;
2699restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002700 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002701 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002702err_mem:
2703 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002704err_register:
2705 i40evf_free_misc_irq(adapter);
2706err_sw_init:
2707 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002708err_alloc:
2709 kfree(adapter->vf_res);
2710 adapter->vf_res = NULL;
2711err:
2712 /* Things went into the weeds, so try again later */
2713 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002714 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002715 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002716 i40evf_shutdown_adminq(hw);
2717 adapter->state = __I40EVF_STARTUP;
2718 schedule_delayed_work(&adapter->init_task, HZ * 5);
2719 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002720 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002721 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002722}
2723
2724/**
2725 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2726 * @pdev: pci device structure
2727 **/
2728static void i40evf_shutdown(struct pci_dev *pdev)
2729{
2730 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002731 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002732
2733 netif_device_detach(netdev);
2734
2735 if (netif_running(netdev))
2736 i40evf_close(netdev);
2737
Mitch Williams00293fd2015-01-09 11:18:18 +00002738 /* Prevent the watchdog from running. */
2739 adapter->state = __I40EVF_REMOVE;
2740 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002741
Greg Rose5eae00c2013-12-21 06:12:45 +00002742#ifdef CONFIG_PM
2743 pci_save_state(pdev);
2744
2745#endif
2746 pci_disable_device(pdev);
2747}
2748
2749/**
2750 * i40evf_probe - Device Initialization Routine
2751 * @pdev: PCI device information struct
2752 * @ent: entry in i40evf_pci_tbl
2753 *
2754 * Returns 0 on success, negative on failure
2755 *
2756 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2757 * The OS initialization, configuring of the adapter private structure,
2758 * and a hardware reset occur.
2759 **/
2760static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2761{
2762 struct net_device *netdev;
2763 struct i40evf_adapter *adapter = NULL;
2764 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002765 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002766
2767 err = pci_enable_device(pdev);
2768 if (err)
2769 return err;
2770
Mitch Williams64942942014-02-11 08:26:33 +00002771 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002772 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002773 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2774 if (err) {
2775 dev_err(&pdev->dev,
2776 "DMA configuration failed: 0x%x\n", err);
2777 goto err_dma;
2778 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002779 }
2780
2781 err = pci_request_regions(pdev, i40evf_driver_name);
2782 if (err) {
2783 dev_err(&pdev->dev,
2784 "pci_request_regions failed 0x%x\n", err);
2785 goto err_pci_reg;
2786 }
2787
2788 pci_enable_pcie_error_reporting(pdev);
2789
2790 pci_set_master(pdev);
2791
Mitch Williams1255b7a2015-11-06 15:25:59 -08002792 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002793 if (!netdev) {
2794 err = -ENOMEM;
2795 goto err_alloc_etherdev;
2796 }
2797
2798 SET_NETDEV_DEV(netdev, &pdev->dev);
2799
2800 pci_set_drvdata(pdev, netdev);
2801 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002802
2803 adapter->netdev = netdev;
2804 adapter->pdev = pdev;
2805
2806 hw = &adapter->hw;
2807 hw->back = adapter;
2808
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002809 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002810 adapter->state = __I40EVF_STARTUP;
2811
2812 /* Call save state here because it relies on the adapter struct. */
2813 pci_save_state(pdev);
2814
2815 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2816 pci_resource_len(pdev, 0));
2817 if (!hw->hw_addr) {
2818 err = -EIO;
2819 goto err_ioremap;
2820 }
2821 hw->vendor_id = pdev->vendor;
2822 hw->device_id = pdev->device;
2823 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2824 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2825 hw->subsystem_device_id = pdev->subsystem_device;
2826 hw->bus.device = PCI_SLOT(pdev->devfn);
2827 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002828 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002829
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002830 /* set up the locks for the AQ, do this only once in probe
2831 * and destroy them only once in remove
2832 */
2833 mutex_init(&hw->aq.asq_mutex);
2834 mutex_init(&hw->aq.arq_mutex);
2835
Serey Kong8bb1a542014-08-01 13:27:15 -07002836 INIT_LIST_HEAD(&adapter->mac_filter_list);
2837 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2838
Greg Rose5eae00c2013-12-21 06:12:45 +00002839 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2840 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2841 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002842 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002843 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002844 schedule_delayed_work(&adapter->init_task,
2845 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002846
2847 return 0;
2848
2849err_ioremap:
2850 free_netdev(netdev);
2851err_alloc_etherdev:
2852 pci_release_regions(pdev);
2853err_pci_reg:
2854err_dma:
2855 pci_disable_device(pdev);
2856 return err;
2857}
2858
2859#ifdef CONFIG_PM
2860/**
2861 * i40evf_suspend - Power management suspend routine
2862 * @pdev: PCI device information struct
2863 * @state: unused
2864 *
2865 * Called when the system (VM) is entering sleep/suspend.
2866 **/
2867static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2868{
2869 struct net_device *netdev = pci_get_drvdata(pdev);
2870 struct i40evf_adapter *adapter = netdev_priv(netdev);
2871 int retval = 0;
2872
2873 netif_device_detach(netdev);
2874
2875 if (netif_running(netdev)) {
2876 rtnl_lock();
2877 i40evf_down(adapter);
2878 rtnl_unlock();
2879 }
2880 i40evf_free_misc_irq(adapter);
2881 i40evf_reset_interrupt_capability(adapter);
2882
2883 retval = pci_save_state(pdev);
2884 if (retval)
2885 return retval;
2886
2887 pci_disable_device(pdev);
2888
2889 return 0;
2890}
2891
2892/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002893 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002894 * @pdev: PCI device information struct
2895 *
2896 * Called when the system (VM) is resumed from sleep/suspend.
2897 **/
2898static int i40evf_resume(struct pci_dev *pdev)
2899{
2900 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2901 struct net_device *netdev = adapter->netdev;
2902 u32 err;
2903
2904 pci_set_power_state(pdev, PCI_D0);
2905 pci_restore_state(pdev);
2906 /* pci_restore_state clears dev->state_saved so call
2907 * pci_save_state to restore it.
2908 */
2909 pci_save_state(pdev);
2910
2911 err = pci_enable_device_mem(pdev);
2912 if (err) {
2913 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2914 return err;
2915 }
2916 pci_set_master(pdev);
2917
2918 rtnl_lock();
2919 err = i40evf_set_interrupt_capability(adapter);
2920 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002921 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002922 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2923 return err;
2924 }
2925 err = i40evf_request_misc_irq(adapter);
2926 rtnl_unlock();
2927 if (err) {
2928 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2929 return err;
2930 }
2931
2932 schedule_work(&adapter->reset_task);
2933
2934 netif_device_attach(netdev);
2935
2936 return err;
2937}
2938
2939#endif /* CONFIG_PM */
2940/**
2941 * i40evf_remove - Device Removal Routine
2942 * @pdev: PCI device information struct
2943 *
2944 * i40evf_remove is called by the PCI subsystem to alert the driver
2945 * that it should release a PCI device. The could be caused by a
2946 * Hot-Plug event, or because the driver is going to be removed from
2947 * memory.
2948 **/
2949static void i40evf_remove(struct pci_dev *pdev)
2950{
2951 struct net_device *netdev = pci_get_drvdata(pdev);
2952 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002953 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002954 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002955 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002956
2957 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002958 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002959 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002960 if (adapter->netdev_registered) {
2961 unregister_netdev(netdev);
2962 adapter->netdev_registered = false;
2963 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002964 if (CLIENT_ALLOWED(adapter)) {
2965 err = i40evf_lan_del_device(adapter);
2966 if (err)
2967 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
2968 err);
2969 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002970
Mitch Williamsf4a71882015-01-09 11:18:16 +00002971 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002972 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002973 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002974 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002975 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002976 /* If the FW isn't responding, kick it once, but only once. */
2977 if (!i40evf_asq_done(hw)) {
2978 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002979 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002980 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08002981 i40evf_free_all_tx_resources(adapter);
2982 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08002983 i40evf_misc_irq_disable(adapter);
2984 i40evf_free_misc_irq(adapter);
2985 i40evf_reset_interrupt_capability(adapter);
2986 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002987
Mitch Williamse5d17c32014-06-04 04:22:38 +00002988 if (adapter->watchdog_timer.function)
2989 del_timer_sync(&adapter->watchdog_timer);
2990
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002991 flush_scheduled_work();
2992
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002993 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04002994
Greg Rose5eae00c2013-12-21 06:12:45 +00002995 if (hw->aq.asq.count)
2996 i40evf_shutdown_adminq(hw);
2997
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002998 /* destroy the locks only once, here */
2999 mutex_destroy(&hw->aq.arq_mutex);
3000 mutex_destroy(&hw->aq.asq_mutex);
3001
Greg Rose5eae00c2013-12-21 06:12:45 +00003002 iounmap(hw->hw_addr);
3003 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003004 i40evf_free_all_tx_resources(adapter);
3005 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003006 i40evf_free_queues(adapter);
3007 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003008 /* If we got removed before an up/down sequence, we've got a filter
3009 * hanging out there that we need to get rid of.
3010 */
3011 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3012 list_del(&f->list);
3013 kfree(f);
3014 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003015 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3016 list_del(&f->list);
3017 kfree(f);
3018 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003019
3020 free_netdev(netdev);
3021
3022 pci_disable_pcie_error_reporting(pdev);
3023
3024 pci_disable_device(pdev);
3025}
3026
3027static struct pci_driver i40evf_driver = {
3028 .name = i40evf_driver_name,
3029 .id_table = i40evf_pci_tbl,
3030 .probe = i40evf_probe,
3031 .remove = i40evf_remove,
3032#ifdef CONFIG_PM
3033 .suspend = i40evf_suspend,
3034 .resume = i40evf_resume,
3035#endif
3036 .shutdown = i40evf_shutdown,
3037};
3038
3039/**
3040 * i40e_init_module - Driver Registration Routine
3041 *
3042 * i40e_init_module is the first routine called when the driver is
3043 * loaded. All it does is register with the PCI subsystem.
3044 **/
3045static int __init i40evf_init_module(void)
3046{
3047 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003048
Greg Rose5eae00c2013-12-21 06:12:45 +00003049 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003050 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003051
3052 pr_info("%s\n", i40evf_copyright);
3053
Jacob Keller6992a6c2016-08-04 11:37:01 -07003054 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3055 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003056 if (!i40evf_wq) {
3057 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3058 return -ENOMEM;
3059 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003060 ret = pci_register_driver(&i40evf_driver);
3061 return ret;
3062}
3063
3064module_init(i40evf_init_module);
3065
3066/**
3067 * i40e_exit_module - Driver Exit Cleanup Routine
3068 *
3069 * i40e_exit_module is called just before the driver is removed
3070 * from memory.
3071 **/
3072static void __exit i40evf_exit_module(void)
3073{
3074 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003075 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003076}
3077
3078module_exit(i40evf_exit_module);
3079
3080/* i40evf_main.c */