blob: 3a3ca965b24272ee3896c39c6dd9ada9648d11eb [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;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001201 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001202 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001203 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001204 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001205}
1206
1207/**
1208 * i40evf_alloc_queues - Allocate memory for all rings
1209 * @adapter: board private structure to initialize
1210 *
1211 * We allocate one ring per queue at run-time since we don't know the
1212 * number of queues at compile-time. The polling_netdev array is
1213 * intended for Multiqueue, but should work fine with a single queue.
1214 **/
1215static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1216{
1217 int i;
1218
Mitch Williams0dd438d2015-10-26 19:44:40 -04001219 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1220 sizeof(struct i40e_ring), GFP_KERNEL);
1221 if (!adapter->tx_rings)
1222 goto err_out;
1223 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1224 sizeof(struct i40e_ring), GFP_KERNEL);
1225 if (!adapter->rx_rings)
1226 goto err_out;
1227
Mitch Williamscc052922014-10-25 03:24:34 +00001228 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001229 struct i40e_ring *tx_ring;
1230 struct i40e_ring *rx_ring;
1231
Mitch Williams0dd438d2015-10-26 19:44:40 -04001232 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001233
1234 tx_ring->queue_index = i;
1235 tx_ring->netdev = adapter->netdev;
1236 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001237 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001238 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001239 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1240 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001241
Mitch Williams0dd438d2015-10-26 19:44:40 -04001242 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001243 rx_ring->queue_index = i;
1244 rx_ring->netdev = adapter->netdev;
1245 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001246 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001247 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001248 }
1249
1250 return 0;
1251
1252err_out:
1253 i40evf_free_queues(adapter);
1254 return -ENOMEM;
1255}
1256
1257/**
1258 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1259 * @adapter: board private structure to initialize
1260 *
1261 * Attempt to configure the interrupts using the best available
1262 * capabilities of the hardware and the kernel.
1263 **/
1264static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1265{
1266 int vector, v_budget;
1267 int pairs = 0;
1268 int err = 0;
1269
1270 if (!adapter->vsi_res) {
1271 err = -EIO;
1272 goto out;
1273 }
Mitch Williamscc052922014-10-25 03:24:34 +00001274 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001275
Jacob Keller789f38c2017-04-19 09:25:56 -04001276 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1277 * us much good if we have more vectors than CPUs. However, we already
1278 * limit the total number of queues by the number of CPUs so we do not
1279 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001280 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001281 v_budget = min_t(int, pairs + NONQ_VECS,
1282 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001283
Greg Rose5eae00c2013-12-21 06:12:45 +00001284 adapter->msix_entries = kcalloc(v_budget,
1285 sizeof(struct msix_entry), GFP_KERNEL);
1286 if (!adapter->msix_entries) {
1287 err = -ENOMEM;
1288 goto out;
1289 }
1290
1291 for (vector = 0; vector < v_budget; vector++)
1292 adapter->msix_entries[vector].entry = vector;
1293
Mitch Williams313ed2d2015-08-27 11:42:31 -04001294 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001295
1296out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001297 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1298 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001299 return err;
1300}
1301
1302/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001303 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1304 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001305 *
1306 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001307 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001308static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001309{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310 struct i40e_aqc_get_set_rss_key_data *rss_key =
1311 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001312 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001313 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001314
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001315 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001316 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001317 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001318 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001319 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001320 }
1321
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001322 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1323 if (ret) {
1324 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1325 i40evf_stat_str(hw, ret),
1326 i40evf_aq_str(hw, hw->aq.asq_last_status));
1327 return ret;
1328
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001329 }
1330
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001331 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1332 adapter->rss_lut, adapter->rss_lut_size);
1333 if (ret) {
1334 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1335 i40evf_stat_str(hw, ret),
1336 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001337 }
1338
1339 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001340
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001341}
1342
1343/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001344 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001345 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001346 *
1347 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001348 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001349static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001350{
1351 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001352 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001353 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001354
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001355 dw = (u32 *)adapter->rss_key;
1356 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1357 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001358
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001359 dw = (u32 *)adapter->rss_lut;
1360 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1361 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001362
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001363 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001364
1365 return 0;
1366}
1367
1368/**
1369 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001370 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001371 *
1372 * Returns 0 on success, negative on failure
1373 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001374int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001375{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001376
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001377 if (RSS_PF(adapter)) {
1378 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1379 I40EVF_FLAG_AQ_SET_RSS_KEY;
1380 return 0;
1381 } else if (RSS_AQ(adapter)) {
1382 return i40evf_config_rss_aq(adapter);
1383 } else {
1384 return i40evf_config_rss_reg(adapter);
1385 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001386}
1387
1388/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001389 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001390 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001391 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001392static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001393{
1394 u16 i;
1395
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001396 for (i = 0; i < adapter->rss_lut_size; i++)
1397 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001398}
1399
1400/**
Helin Zhang96a81982015-10-26 19:44:31 -04001401 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001402 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001403 *
1404 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001405 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001406static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001407{
1408 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001409 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001410
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001411 if (!RSS_PF(adapter)) {
1412 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1413 if (adapter->vf_res->vf_offload_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001414 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001415 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1416 else
1417 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001418
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001419 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1420 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1421 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001422
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001423 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001424
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001425 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1426 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001427
1428 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001429}
1430
1431/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001432 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1433 * @adapter: board private structure to initialize
1434 *
1435 * We allocate one q_vector per queue interrupt. If allocation fails we
1436 * return -ENOMEM.
1437 **/
1438static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1439{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001440 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001441 struct i40e_q_vector *q_vector;
1442
1443 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001444 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001445 GFP_KERNEL);
1446 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001447 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001448
1449 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001450 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001451 q_vector->adapter = adapter;
1452 q_vector->vsi = &adapter->vsi;
1453 q_vector->v_idx = q_idx;
1454 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001455 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001456 }
1457
1458 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001459}
1460
1461/**
1462 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1463 * @adapter: board private structure to initialize
1464 *
1465 * This function frees the memory allocated to the q_vectors. In addition if
1466 * NAPI is enabled it will delete any references to the NAPI struct prior
1467 * to freeing the q_vector.
1468 **/
1469static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1470{
1471 int q_idx, num_q_vectors;
1472 int napi_vectors;
1473
Jacob Kelleref4603e2016-11-08 13:05:08 -08001474 if (!adapter->q_vectors)
1475 return;
1476
Greg Rose5eae00c2013-12-21 06:12:45 +00001477 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001478 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001479
1480 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001481 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001482 if (q_idx < napi_vectors)
1483 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001484 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001485 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001486 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001487}
1488
1489/**
1490 * i40evf_reset_interrupt_capability - Reset MSIX setup
1491 * @adapter: board private structure
1492 *
1493 **/
1494void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1495{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001496 if (!adapter->msix_entries)
1497 return;
1498
Greg Rose5eae00c2013-12-21 06:12:45 +00001499 pci_disable_msix(adapter->pdev);
1500 kfree(adapter->msix_entries);
1501 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001502}
1503
1504/**
1505 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1506 * @adapter: board private structure to initialize
1507 *
1508 **/
1509int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1510{
1511 int err;
1512
Jacob Keller283aeaf2017-04-19 09:25:59 -04001513 err = i40evf_alloc_queues(adapter);
1514 if (err) {
1515 dev_err(&adapter->pdev->dev,
1516 "Unable to allocate memory for queues\n");
1517 goto err_alloc_queues;
1518 }
1519
Jacob Keller62fe2a82016-07-27 12:02:33 -07001520 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001521 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001522 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001523 if (err) {
1524 dev_err(&adapter->pdev->dev,
1525 "Unable to setup interrupt capabilities\n");
1526 goto err_set_interrupt;
1527 }
1528
1529 err = i40evf_alloc_q_vectors(adapter);
1530 if (err) {
1531 dev_err(&adapter->pdev->dev,
1532 "Unable to allocate memory for queue vectors\n");
1533 goto err_alloc_q_vectors;
1534 }
1535
Greg Rose5eae00c2013-12-21 06:12:45 +00001536 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001537 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1538 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001539
1540 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001541err_alloc_q_vectors:
1542 i40evf_reset_interrupt_capability(adapter);
1543err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001544 i40evf_free_queues(adapter);
1545err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001546 return err;
1547}
1548
1549/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001550 * i40evf_free_rss - Free memory used by RSS structs
1551 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001552 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001553static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001554{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001555 kfree(adapter->rss_key);
1556 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001557
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001558 kfree(adapter->rss_lut);
1559 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001560}
1561
1562/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001563 * i40evf_watchdog_timer - Periodic call-back timer
1564 * @data: pointer to adapter disguised as unsigned long
1565 **/
1566static void i40evf_watchdog_timer(unsigned long data)
1567{
1568 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001569
Greg Rose5eae00c2013-12-21 06:12:45 +00001570 schedule_work(&adapter->watchdog_task);
1571 /* timer will be rescheduled in watchdog task */
1572}
1573
1574/**
1575 * i40evf_watchdog_task - Periodic call-back task
1576 * @work: pointer to work_struct
1577 **/
1578static void i40evf_watchdog_task(struct work_struct *work)
1579{
1580 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001581 struct i40evf_adapter,
1582 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001583 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001584 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001585
Greg Rose5eae00c2013-12-21 06:12:45 +00001586 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001587 goto restart_watchdog;
1588
1589 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001590 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1591 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001592 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1593 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001594 /* A chance for redemption! */
1595 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1596 adapter->state = __I40EVF_STARTUP;
1597 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1598 schedule_delayed_work(&adapter->init_task, 10);
1599 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1600 &adapter->crit_section);
1601 /* Don't reschedule the watchdog, since we've restarted
1602 * the init task. When init_task contacts the PF and
1603 * gets everything set up again, it'll restart the
1604 * watchdog for us. Down, boy. Sit. Stay. Woof.
1605 */
1606 return;
1607 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001608 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001609 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001610 goto watchdog_done;
1611 }
1612
1613 if ((adapter->state < __I40EVF_DOWN) ||
1614 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001615 goto watchdog_done;
1616
Mitch Williamsef8693e2014-02-13 03:48:53 -08001617 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001618 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1619 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001620 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001621 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001622 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001623 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001624 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001625 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001626 goto watchdog_done;
1627 }
1628
1629 /* Process admin queue tasks. After init, everything gets done
1630 * here so we don't race on the admin queue.
1631 */
Mitch Williamsed636962015-04-07 19:45:32 -04001632 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001633 if (!i40evf_asq_done(hw)) {
1634 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1635 i40evf_send_api_ver(adapter);
1636 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001637 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001638 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001639 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1640 i40evf_send_vf_config_msg(adapter);
1641 goto watchdog_done;
1642 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001643
Mitch Williamse284fc82015-03-27 00:12:09 -07001644 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1645 i40evf_disable_queues(adapter);
1646 goto watchdog_done;
1647 }
1648
Greg Rose5eae00c2013-12-21 06:12:45 +00001649 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1650 i40evf_map_queues(adapter);
1651 goto watchdog_done;
1652 }
1653
1654 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1655 i40evf_add_ether_addrs(adapter);
1656 goto watchdog_done;
1657 }
1658
1659 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1660 i40evf_add_vlans(adapter);
1661 goto watchdog_done;
1662 }
1663
1664 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1665 i40evf_del_ether_addrs(adapter);
1666 goto watchdog_done;
1667 }
1668
1669 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1670 i40evf_del_vlans(adapter);
1671 goto watchdog_done;
1672 }
1673
Greg Rose5eae00c2013-12-21 06:12:45 +00001674 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1675 i40evf_configure_queues(adapter);
1676 goto watchdog_done;
1677 }
1678
1679 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1680 i40evf_enable_queues(adapter);
1681 goto watchdog_done;
1682 }
1683
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001684 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1685 /* This message goes straight to the firmware, not the
1686 * PF, so we don't have to set current_op as we will
1687 * not get a response through the ARQ.
1688 */
Helin Zhang96a81982015-10-26 19:44:31 -04001689 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001690 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1691 goto watchdog_done;
1692 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001693 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1694 i40evf_get_hena(adapter);
1695 goto watchdog_done;
1696 }
1697 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1698 i40evf_set_hena(adapter);
1699 goto watchdog_done;
1700 }
1701 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1702 i40evf_set_rss_key(adapter);
1703 goto watchdog_done;
1704 }
1705 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1706 i40evf_set_rss_lut(adapter);
1707 goto watchdog_done;
1708 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001709
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001710 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001711 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1712 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001713 goto watchdog_done;
1714 }
1715
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001716 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001717 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001718 goto watchdog_done;
1719 }
1720
1721 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1722 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001723 i40evf_set_promiscuous(adapter, 0);
1724 goto watchdog_done;
1725 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001726 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001727
Greg Rose5eae00c2013-12-21 06:12:45 +00001728 if (adapter->state == __I40EVF_RUNNING)
1729 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001730watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001731 if (adapter->state == __I40EVF_RUNNING) {
1732 i40evf_irq_enable_queues(adapter, ~0);
1733 i40evf_fire_sw_int(adapter, 0xFF);
1734 } else {
1735 i40evf_fire_sw_int(adapter, 0x1);
1736 }
1737
Mitch Williamsef8693e2014-02-13 03:48:53 -08001738 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1739restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001740 if (adapter->state == __I40EVF_REMOVE)
1741 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001742 if (adapter->aq_required)
1743 mod_timer(&adapter->watchdog_timer,
1744 jiffies + msecs_to_jiffies(20));
1745 else
1746 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001747 schedule_work(&adapter->adminq_task);
1748}
1749
Joe Perchesdedecb62016-11-01 15:35:14 -07001750static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1751{
1752 struct i40evf_mac_filter *f, *ftmp;
1753 struct i40evf_vlan_filter *fv, *fvtmp;
1754
1755 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1756
1757 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001758 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001759 netif_carrier_off(adapter->netdev);
1760 netif_tx_disable(adapter->netdev);
1761 adapter->link_up = false;
1762 i40evf_napi_disable_all(adapter);
1763 i40evf_irq_disable(adapter);
1764 i40evf_free_traffic_irqs(adapter);
1765 i40evf_free_all_tx_resources(adapter);
1766 i40evf_free_all_rx_resources(adapter);
1767 }
1768
1769 /* Delete all of the filters, both MAC and VLAN. */
1770 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1771 list_del(&f->list);
1772 kfree(f);
1773 }
1774
1775 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1776 list_del(&fv->list);
1777 kfree(fv);
1778 }
1779
1780 i40evf_free_misc_irq(adapter);
1781 i40evf_reset_interrupt_capability(adapter);
1782 i40evf_free_queues(adapter);
1783 i40evf_free_q_vectors(adapter);
1784 kfree(adapter->vf_res);
1785 i40evf_shutdown_adminq(&adapter->hw);
1786 adapter->netdev->flags &= ~IFF_UP;
1787 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1788 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1789 adapter->state = __I40EVF_DOWN;
1790 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1791}
1792
Mitch Williams67c818a2015-06-19 08:56:30 -07001793#define I40EVF_RESET_WAIT_MS 10
1794#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001795/**
1796 * i40evf_reset_task - Call-back task to handle hardware reset
1797 * @work: pointer to work_struct
1798 *
1799 * During reset we need to shut down and reinitialize the admin queue
1800 * before we can use it to communicate with the PF again. We also clear
1801 * and reinit the rings because that context is lost as well.
1802 **/
1803static void i40evf_reset_task(struct work_struct *work)
1804{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001805 struct i40evf_adapter *adapter = container_of(work,
1806 struct i40evf_adapter,
1807 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001808 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001809 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001810 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001811 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001812 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001813 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001814
Mitch Williamsed0e8942017-01-24 10:23:59 -08001815 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001816 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001817 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001818 if (CLIENT_ENABLED(adapter)) {
1819 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1820 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1821 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1822 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1823 cancel_delayed_work_sync(&adapter->client_task);
1824 i40evf_notify_client_close(&adapter->vsi, true);
1825 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001826 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001827 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001828 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1829 /* Restart the AQ here. If we have been reset but didn't
1830 * detect it, or if the PF had to reinit, our AQ will be hosed.
1831 */
1832 i40evf_shutdown_adminq(hw);
1833 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001834 i40evf_request_reset(adapter);
1835 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001836 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001837
Mitch Williamsef8693e2014-02-13 03:48:53 -08001838 /* poll until we see the reset actually happen */
1839 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001840 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1841 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1842 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001843 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001844 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001845 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001846 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001847 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001848 goto continue_reset; /* act like the reset happened */
1849 }
1850
1851 /* wait until the reset is complete and the PF is responding to us */
1852 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001853 /* sleep first to make sure a minimum wait time is met */
1854 msleep(I40EVF_RESET_WAIT_MS);
1855
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001856 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1857 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001858 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001859 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001860 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001861
Mitch Williams509a4472015-12-23 12:05:52 -08001862 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001863
Mitch Williamsef8693e2014-02-13 03:48:53 -08001864 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001865 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001866 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001867 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001868 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001869 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001870 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001871
1872continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001873 if (netif_running(adapter->netdev)) {
1874 netif_carrier_off(netdev);
1875 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001876 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001877 i40evf_napi_disable_all(adapter);
1878 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001879 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001880
Greg Rose5eae00c2013-12-21 06:12:45 +00001881 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001882 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1883
1884 /* free the Tx/Rx rings and descriptors, might be better to just
1885 * re-use them sometime in the future
1886 */
1887 i40evf_free_all_rx_resources(adapter);
1888 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001889
1890 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001891 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001892 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001893 err = i40evf_init_adminq(hw);
1894 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001895 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1896 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001897
Mitch Williamse6d038d2015-06-04 16:23:58 -04001898 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1899 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001900
1901 /* re-add all MAC filters */
1902 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1903 f->add = true;
1904 }
1905 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001906 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1907 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001908 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001909 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001910 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001911 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001912 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001913 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001914
1915 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1916
1917 if (netif_running(adapter->netdev)) {
1918 /* allocate transmit descriptors */
1919 err = i40evf_setup_all_tx_resources(adapter);
1920 if (err)
1921 goto reset_err;
1922
1923 /* allocate receive descriptors */
1924 err = i40evf_setup_all_rx_resources(adapter);
1925 if (err)
1926 goto reset_err;
1927
1928 i40evf_configure(adapter);
1929
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001930 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001931
1932 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001933 } else {
1934 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001935 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001936
Greg Rose5eae00c2013-12-21 06:12:45 +00001937 return;
1938reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001939 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001940 i40evf_close(adapter->netdev);
1941}
1942
1943/**
1944 * i40evf_adminq_task - worker thread to clean the admin queue
1945 * @work: pointer to work_struct containing our data
1946 **/
1947static void i40evf_adminq_task(struct work_struct *work)
1948{
1949 struct i40evf_adapter *adapter =
1950 container_of(work, struct i40evf_adapter, adminq_task);
1951 struct i40e_hw *hw = &adapter->hw;
1952 struct i40e_arq_event_info event;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001953 struct virtchnl_msg *v_msg;
Greg Rose5eae00c2013-12-21 06:12:45 +00001954 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001955 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001956 u16 pending;
1957
Mitch Williamsef8693e2014-02-13 03:48:53 -08001958 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001959 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001960
Mitch Williams1001dc32014-11-11 20:02:19 +00001961 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1962 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001963 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001964 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001965
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001966 v_msg = (struct virtchnl_msg *)&event.desc;
Greg Rose5eae00c2013-12-21 06:12:45 +00001967 do {
1968 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001969 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001970 break; /* No event to process or error cleaning ARQ */
1971
1972 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001973 (i40e_status)v_msg->v_retval,
1974 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001975 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001976 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001977 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001978 } while (pending);
1979
Mitch Williams67c818a2015-06-19 08:56:30 -07001980 if ((adapter->flags &
1981 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1982 adapter->state == __I40EVF_RESETTING)
1983 goto freedom;
1984
Mitch Williams912257e2014-05-22 06:32:07 +00001985 /* check for error indications */
1986 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001987 if (val == 0xdeadbeef) /* indicates device in reset */
1988 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001989 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001990 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001991 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001992 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001993 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001994 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001995 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001996 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001997 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001998 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001999 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002000 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002001 }
2002 if (oldval != val)
2003 wr32(hw, hw->aq.arq.len, val);
2004
2005 val = rd32(hw, hw->aq.asq.len);
2006 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002007 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002008 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002009 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002010 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002011 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002012 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002013 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002014 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002015 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002016 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002017 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002018 }
2019 if (oldval != val)
2020 wr32(hw, hw->aq.asq.len, val);
2021
Mitch Williams67c818a2015-06-19 08:56:30 -07002022freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002023 kfree(event.msg_buf);
2024out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002025 /* re-enable Admin queue interrupt cause */
2026 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002027}
2028
2029/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002030 * i40evf_client_task - worker thread to perform client work
2031 * @work: pointer to work_struct containing our data
2032 *
2033 * This task handles client interactions. Because client calls can be
2034 * reentrant, we can't handle them in the watchdog.
2035 **/
2036static void i40evf_client_task(struct work_struct *work)
2037{
2038 struct i40evf_adapter *adapter =
2039 container_of(work, struct i40evf_adapter, client_task.work);
2040
2041 /* If we can't get the client bit, just give up. We'll be rescheduled
2042 * later.
2043 */
2044
2045 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2046 return;
2047
2048 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2049 i40evf_client_subtask(adapter);
2050 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2051 goto out;
2052 }
2053 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2054 i40evf_notify_client_close(&adapter->vsi, false);
2055 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2056 goto out;
2057 }
2058 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2059 i40evf_notify_client_open(&adapter->vsi);
2060 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2061 goto out;
2062 }
2063 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2064 i40evf_notify_client_l2_params(&adapter->vsi);
2065 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2066 }
2067out:
2068 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2069}
2070
2071/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002072 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2073 * @adapter: board private structure
2074 *
2075 * Free all transmit software resources
2076 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002077void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002078{
2079 int i;
2080
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002081 if (!adapter->tx_rings)
2082 return;
2083
Mitch Williamscc052922014-10-25 03:24:34 +00002084 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002085 if (adapter->tx_rings[i].desc)
2086 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002087}
2088
2089/**
2090 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2091 * @adapter: board private structure
2092 *
2093 * If this function returns with an error, then it's possible one or
2094 * more of the rings is populated (while the rest are not). It is the
2095 * callers duty to clean those orphaned rings.
2096 *
2097 * Return 0 on success, negative on failure
2098 **/
2099static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2100{
2101 int i, err = 0;
2102
Mitch Williamscc052922014-10-25 03:24:34 +00002103 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002104 adapter->tx_rings[i].count = adapter->tx_desc_count;
2105 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002106 if (!err)
2107 continue;
2108 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002109 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002110 break;
2111 }
2112
2113 return err;
2114}
2115
2116/**
2117 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2118 * @adapter: board private structure
2119 *
2120 * If this function returns with an error, then it's possible one or
2121 * more of the rings is populated (while the rest are not). It is the
2122 * callers duty to clean those orphaned rings.
2123 *
2124 * Return 0 on success, negative on failure
2125 **/
2126static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2127{
2128 int i, err = 0;
2129
Mitch Williamscc052922014-10-25 03:24:34 +00002130 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002131 adapter->rx_rings[i].count = adapter->rx_desc_count;
2132 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002133 if (!err)
2134 continue;
2135 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002136 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002137 break;
2138 }
2139 return err;
2140}
2141
2142/**
2143 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2144 * @adapter: board private structure
2145 *
2146 * Free all receive software resources
2147 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002148void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002149{
2150 int i;
2151
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002152 if (!adapter->rx_rings)
2153 return;
2154
Mitch Williamscc052922014-10-25 03:24:34 +00002155 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002156 if (adapter->rx_rings[i].desc)
2157 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002158}
2159
2160/**
2161 * i40evf_open - Called when a network interface is made active
2162 * @netdev: network interface device structure
2163 *
2164 * Returns 0 on success, negative value on failure
2165 *
2166 * The open entry point is called when a network interface is made
2167 * active by the system (IFF_UP). At this point all resources needed
2168 * for transmit and receive operations are allocated, the interrupt
2169 * handler is registered with the OS, the watchdog timer is started,
2170 * and the stack is notified that the interface is ready.
2171 **/
2172static int i40evf_open(struct net_device *netdev)
2173{
2174 struct i40evf_adapter *adapter = netdev_priv(netdev);
2175 int err;
2176
Mitch Williamsef8693e2014-02-13 03:48:53 -08002177 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2178 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2179 return -EIO;
2180 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002181
2182 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002183 return -EBUSY;
2184
2185 /* allocate transmit descriptors */
2186 err = i40evf_setup_all_tx_resources(adapter);
2187 if (err)
2188 goto err_setup_tx;
2189
2190 /* allocate receive descriptors */
2191 err = i40evf_setup_all_rx_resources(adapter);
2192 if (err)
2193 goto err_setup_rx;
2194
2195 /* clear any pending interrupts, may auto mask */
2196 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2197 if (err)
2198 goto err_req_irq;
2199
Mitch Williams44151cd2015-04-27 14:57:17 -04002200 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002201 i40evf_configure(adapter);
2202
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002203 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002204
2205 i40evf_irq_enable(adapter, true);
2206
2207 return 0;
2208
2209err_req_irq:
2210 i40evf_down(adapter);
2211 i40evf_free_traffic_irqs(adapter);
2212err_setup_rx:
2213 i40evf_free_all_rx_resources(adapter);
2214err_setup_tx:
2215 i40evf_free_all_tx_resources(adapter);
2216
2217 return err;
2218}
2219
2220/**
2221 * i40evf_close - Disables a network interface
2222 * @netdev: network interface device structure
2223 *
2224 * Returns 0, this is not allowed to fail
2225 *
2226 * The close entry point is called when an interface is de-activated
2227 * by the OS. The hardware is still under the drivers control, but
2228 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2229 * are freed, along with all transmit and receive resources.
2230 **/
2231static int i40evf_close(struct net_device *netdev)
2232{
2233 struct i40evf_adapter *adapter = netdev_priv(netdev);
2234
Mitch Williams209dc4d2015-12-09 15:50:27 -08002235 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002236 return 0;
2237
Mitch Williamsef8693e2014-02-13 03:48:53 -08002238
Jacob Keller0da36b92017-04-19 09:25:55 -04002239 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002240 if (CLIENT_ENABLED(adapter))
2241 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002242
2243 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002244 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002245 i40evf_free_traffic_irqs(adapter);
2246
Mitch Williams51f38262016-12-12 15:44:11 -08002247 /* We explicitly don't free resources here because the hardware is
2248 * still active and can DMA into memory. Resources are cleared in
2249 * i40evf_virtchnl_completion() after we get confirmation from the PF
2250 * driver that the rings have been stopped.
2251 */
Greg Rose5eae00c2013-12-21 06:12:45 +00002252 return 0;
2253}
2254
2255/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002256 * i40evf_change_mtu - Change the Maximum Transfer Unit
2257 * @netdev: network interface device structure
2258 * @new_mtu: new value for maximum frame size
2259 *
2260 * Returns 0 on success, negative on failure
2261 **/
2262static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2263{
2264 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002265
Greg Rose5eae00c2013-12-21 06:12:45 +00002266 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002267 if (CLIENT_ENABLED(adapter)) {
2268 i40evf_notify_client_l2_params(&adapter->vsi);
2269 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2270 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002271 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2272 schedule_work(&adapter->reset_task);
2273
Greg Rose5eae00c2013-12-21 06:12:45 +00002274 return 0;
2275}
2276
Alexander Duyck06fc0162016-10-25 16:08:47 -07002277/**
2278 * i40evf_features_check - Validate encapsulated packet conforms to limits
2279 * @skb: skb buff
2280 * @netdev: This physical port's netdev
2281 * @features: Offload features that the stack believes apply
2282 **/
2283static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2284 struct net_device *dev,
2285 netdev_features_t features)
2286{
2287 size_t len;
2288
2289 /* No point in doing any of this if neither checksum nor GSO are
2290 * being requested for this frame. We can rule out both by just
2291 * checking for CHECKSUM_PARTIAL
2292 */
2293 if (skb->ip_summed != CHECKSUM_PARTIAL)
2294 return features;
2295
2296 /* We cannot support GSO if the MSS is going to be less than
2297 * 64 bytes. If it is then we need to drop support for GSO.
2298 */
2299 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2300 features &= ~NETIF_F_GSO_MASK;
2301
2302 /* MACLEN can support at most 63 words */
2303 len = skb_network_header(skb) - skb->data;
2304 if (len & ~(63 * 2))
2305 goto out_err;
2306
2307 /* IPLEN and EIPLEN can support at most 127 dwords */
2308 len = skb_transport_header(skb) - skb_network_header(skb);
2309 if (len & ~(127 * 4))
2310 goto out_err;
2311
2312 if (skb->encapsulation) {
2313 /* L4TUNLEN can support 127 words */
2314 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2315 if (len & ~(127 * 2))
2316 goto out_err;
2317
2318 /* IPLEN can support at most 127 dwords */
2319 len = skb_inner_transport_header(skb) -
2320 skb_inner_network_header(skb);
2321 if (len & ~(127 * 4))
2322 goto out_err;
2323 }
2324
2325 /* No need to validate L4LEN as TCP is the only protocol with a
2326 * a flexible value and we support all possible values supported
2327 * by TCP, which is at most 15 dwords
2328 */
2329
2330 return features;
2331out_err:
2332 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2333}
2334
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002335#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2336 NETIF_F_HW_VLAN_CTAG_RX |\
2337 NETIF_F_HW_VLAN_CTAG_FILTER)
2338
2339/**
2340 * i40evf_fix_features - fix up the netdev feature bits
2341 * @netdev: our net device
2342 * @features: desired feature bits
2343 *
2344 * Returns fixed-up features bits
2345 **/
2346static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2347 netdev_features_t features)
2348{
2349 struct i40evf_adapter *adapter = netdev_priv(netdev);
2350
2351 features &= ~I40EVF_VLAN_FEATURES;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002352 if (adapter->vf_res->vf_offload_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002353 features |= I40EVF_VLAN_FEATURES;
2354 return features;
2355}
2356
Greg Rose5eae00c2013-12-21 06:12:45 +00002357static const struct net_device_ops i40evf_netdev_ops = {
2358 .ndo_open = i40evf_open,
2359 .ndo_stop = i40evf_close,
2360 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002361 .ndo_set_rx_mode = i40evf_set_rx_mode,
2362 .ndo_validate_addr = eth_validate_addr,
2363 .ndo_set_mac_address = i40evf_set_mac,
2364 .ndo_change_mtu = i40evf_change_mtu,
2365 .ndo_tx_timeout = i40evf_tx_timeout,
2366 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2367 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002368 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002369 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002370#ifdef CONFIG_NET_POLL_CONTROLLER
2371 .ndo_poll_controller = i40evf_netpoll,
2372#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002373};
2374
2375/**
2376 * i40evf_check_reset_complete - check that VF reset is complete
2377 * @hw: pointer to hw struct
2378 *
2379 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2380 **/
2381static int i40evf_check_reset_complete(struct i40e_hw *hw)
2382{
2383 u32 rstat;
2384 int i;
2385
2386 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002387 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2388 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002389 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2390 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002391 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002392 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002393 }
2394 return -EBUSY;
2395}
2396
2397/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002398 * i40evf_process_config - Process the config information we got from the PF
2399 * @adapter: board private structure
2400 *
2401 * Verify that we have a valid config struct, and set up our netdev features
2402 * and our VSI struct.
2403 **/
2404int i40evf_process_config(struct i40evf_adapter *adapter)
2405{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002406 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002407 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002408 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002409 int i;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002410 netdev_features_t hw_enc_features;
2411 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002412
2413 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002414 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002415 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002416 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002417 }
2418 if (!adapter->vsi_res) {
2419 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2420 return -ENODEV;
2421 }
2422
Preethi Banalabacd75c2017-03-27 14:43:18 -07002423 hw_enc_features = NETIF_F_SG |
2424 NETIF_F_IP_CSUM |
2425 NETIF_F_IPV6_CSUM |
2426 NETIF_F_HIGHDMA |
2427 NETIF_F_SOFT_FEATURES |
2428 NETIF_F_TSO |
2429 NETIF_F_TSO_ECN |
2430 NETIF_F_TSO6 |
2431 NETIF_F_SCTP_CRC |
2432 NETIF_F_RXHASH |
2433 NETIF_F_RXCSUM |
2434 0;
2435
2436 /* advertise to stack only if offloads for encapsulated packets is
2437 * supported
2438 */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002439 if (vfres->vf_offload_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002440 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002441 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002442 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002443 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002444 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002445 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002446 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002447 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002448
Preethi Banalabacd75c2017-03-27 14:43:18 -07002449 if (!(vfres->vf_offload_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002450 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002451 netdev->gso_partial_features |=
2452 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002453
Preethi Banalabacd75c2017-03-27 14:43:18 -07002454 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2455 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2456 netdev->hw_enc_features |= hw_enc_features;
2457 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002458 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002459 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002460
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002461 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002462 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002463 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002464 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002465
Preethi Banalabacd75c2017-03-27 14:43:18 -07002466 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002467
Preethi Banalabacd75c2017-03-27 14:43:18 -07002468 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002469
2470 adapter->vsi.id = adapter->vsi_res->vsi_id;
2471
2472 adapter->vsi.back = adapter;
2473 adapter->vsi.base_vector = 1;
2474 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002475 vsi->netdev = adapter->netdev;
2476 vsi->qs_handle = adapter->vsi_res->qset_handle;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002477 if (vfres->vf_offload_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002478 adapter->rss_key_size = vfres->rss_key_size;
2479 adapter->rss_lut_size = vfres->rss_lut_size;
2480 } else {
2481 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2482 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2483 }
2484
Mitch Williamse6d038d2015-06-04 16:23:58 -04002485 return 0;
2486}
2487
2488/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002489 * i40evf_init_task - worker thread to perform delayed initialization
2490 * @work: pointer to work_struct containing our data
2491 *
2492 * This task completes the work that was begun in probe. Due to the nature
2493 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002494 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002495 * whole system, we'll do it in a task so we can sleep.
2496 * This task only runs during driver init. Once we've established
2497 * communications with the PF driver and set up our netdev, the watchdog
2498 * takes over.
2499 **/
2500static void i40evf_init_task(struct work_struct *work)
2501{
2502 struct i40evf_adapter *adapter = container_of(work,
2503 struct i40evf_adapter,
2504 init_task.work);
2505 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002506 struct i40e_hw *hw = &adapter->hw;
2507 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002508 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002509
2510 switch (adapter->state) {
2511 case __I40EVF_STARTUP:
2512 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002513 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2514 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002515 err = i40e_set_mac_type(hw);
2516 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002517 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2518 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002519 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002520 }
2521 err = i40evf_check_reset_complete(hw);
2522 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002523 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002524 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002525 goto err;
2526 }
2527 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2528 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2529 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2530 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2531
2532 err = i40evf_init_adminq(hw);
2533 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002534 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2535 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002536 goto err;
2537 }
2538 err = i40evf_send_api_ver(adapter);
2539 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002540 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002541 i40evf_shutdown_adminq(hw);
2542 goto err;
2543 }
2544 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2545 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002546 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002547 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002548 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002549 i40evf_shutdown_adminq(hw);
2550 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002551 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002552 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002553
2554 /* aq msg sent, awaiting reply */
2555 err = i40evf_verify_api_ver(adapter);
2556 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002557 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002558 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002559 else
2560 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2561 adapter->pf_version.major,
2562 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002563 VIRTCHNL_VERSION_MAJOR,
2564 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002565 goto err;
2566 }
2567 err = i40evf_send_vf_config_msg(adapter);
2568 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002569 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002570 err);
2571 goto err;
2572 }
2573 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2574 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002575 case __I40EVF_INIT_GET_RESOURCES:
2576 /* aq msg sent, awaiting reply */
2577 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002578 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002579 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002580 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002581 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002582 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002583 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002584 }
2585 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002586 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002587 err = i40evf_send_vf_config_msg(adapter);
2588 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002589 } else if (err == I40E_ERR_PARAM) {
2590 /* We only get ERR_PARAM if the device is in a very bad
2591 * state or if we've been disabled for previous bad
2592 * behavior. Either way, we're done now.
2593 */
2594 i40evf_shutdown_adminq(hw);
2595 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2596 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002597 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002598 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002599 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2600 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002601 goto err_alloc;
2602 }
2603 adapter->state = __I40EVF_INIT_SW;
2604 break;
2605 default:
2606 goto err_alloc;
2607 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002608
Mitch Williamse6d038d2015-06-04 16:23:58 -04002609 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002610 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002611 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002612
2613 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2614
Greg Rose5eae00c2013-12-21 06:12:45 +00002615 netdev->netdev_ops = &i40evf_netdev_ops;
2616 i40evf_set_ethtool_ops(netdev);
2617 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002618
Jarod Wilson91c527a2016-10-17 15:54:05 -04002619 /* MTU range: 68 - 9710 */
2620 netdev->min_mtu = ETH_MIN_MTU;
2621 netdev->max_mtu = I40E_MAX_RXBUFFER - (ETH_HLEN + ETH_FCS_LEN);
2622
Greg Rose5eae00c2013-12-21 06:12:45 +00002623 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002624 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002625 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002626 eth_hw_addr_random(netdev);
2627 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2628 } else {
2629 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2630 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2631 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002632 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002633
Greg Rose5eae00c2013-12-21 06:12:45 +00002634 init_timer(&adapter->watchdog_timer);
2635 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2636 adapter->watchdog_timer.data = (unsigned long)adapter;
2637 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2638
Mitch Williamscc052922014-10-25 03:24:34 +00002639 adapter->num_active_queues = min_t(int,
2640 adapter->vsi_res->num_queue_pairs,
2641 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002642 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2643 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002644 err = i40evf_init_interrupt_scheme(adapter);
2645 if (err)
2646 goto err_sw_init;
2647 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002648 if (adapter->vf_res->vf_offload_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002649 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002650 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2651
Greg Rose5eae00c2013-12-21 06:12:45 +00002652 err = i40evf_request_misc_irq(adapter);
2653 if (err)
2654 goto err_sw_init;
2655
2656 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002657 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002658
Mitch Williamsef8693e2014-02-13 03:48:53 -08002659 if (!adapter->netdev_registered) {
2660 err = register_netdev(netdev);
2661 if (err)
2662 goto err_register;
2663 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002664
2665 adapter->netdev_registered = true;
2666
2667 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002668 if (CLIENT_ALLOWED(adapter)) {
2669 err = i40evf_lan_add_device(adapter);
2670 if (err)
2671 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2672 err);
2673 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002674
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002675 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002676 if (netdev->features & NETIF_F_GRO)
2677 dev_info(&pdev->dev, "GRO is enabled\n");
2678
Greg Rose5eae00c2013-12-21 06:12:45 +00002679 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002680 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002681 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002682
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002683 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2684 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2685 if (!adapter->rss_key || !adapter->rss_lut)
2686 goto err_mem;
2687
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002688 if (RSS_AQ(adapter)) {
2689 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2690 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2691 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002692 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002693 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002694 return;
2695restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002696 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002697 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002698err_mem:
2699 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002700err_register:
2701 i40evf_free_misc_irq(adapter);
2702err_sw_init:
2703 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002704err_alloc:
2705 kfree(adapter->vf_res);
2706 adapter->vf_res = NULL;
2707err:
2708 /* Things went into the weeds, so try again later */
2709 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002710 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002711 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002712 i40evf_shutdown_adminq(hw);
2713 adapter->state = __I40EVF_STARTUP;
2714 schedule_delayed_work(&adapter->init_task, HZ * 5);
2715 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002716 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002717 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002718}
2719
2720/**
2721 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2722 * @pdev: pci device structure
2723 **/
2724static void i40evf_shutdown(struct pci_dev *pdev)
2725{
2726 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002727 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002728
2729 netif_device_detach(netdev);
2730
2731 if (netif_running(netdev))
2732 i40evf_close(netdev);
2733
Mitch Williams00293fd2015-01-09 11:18:18 +00002734 /* Prevent the watchdog from running. */
2735 adapter->state = __I40EVF_REMOVE;
2736 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002737
Greg Rose5eae00c2013-12-21 06:12:45 +00002738#ifdef CONFIG_PM
2739 pci_save_state(pdev);
2740
2741#endif
2742 pci_disable_device(pdev);
2743}
2744
2745/**
2746 * i40evf_probe - Device Initialization Routine
2747 * @pdev: PCI device information struct
2748 * @ent: entry in i40evf_pci_tbl
2749 *
2750 * Returns 0 on success, negative on failure
2751 *
2752 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2753 * The OS initialization, configuring of the adapter private structure,
2754 * and a hardware reset occur.
2755 **/
2756static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2757{
2758 struct net_device *netdev;
2759 struct i40evf_adapter *adapter = NULL;
2760 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002761 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002762
2763 err = pci_enable_device(pdev);
2764 if (err)
2765 return err;
2766
Mitch Williams64942942014-02-11 08:26:33 +00002767 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002768 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002769 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2770 if (err) {
2771 dev_err(&pdev->dev,
2772 "DMA configuration failed: 0x%x\n", err);
2773 goto err_dma;
2774 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002775 }
2776
2777 err = pci_request_regions(pdev, i40evf_driver_name);
2778 if (err) {
2779 dev_err(&pdev->dev,
2780 "pci_request_regions failed 0x%x\n", err);
2781 goto err_pci_reg;
2782 }
2783
2784 pci_enable_pcie_error_reporting(pdev);
2785
2786 pci_set_master(pdev);
2787
Mitch Williams1255b7a2015-11-06 15:25:59 -08002788 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002789 if (!netdev) {
2790 err = -ENOMEM;
2791 goto err_alloc_etherdev;
2792 }
2793
2794 SET_NETDEV_DEV(netdev, &pdev->dev);
2795
2796 pci_set_drvdata(pdev, netdev);
2797 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002798
2799 adapter->netdev = netdev;
2800 adapter->pdev = pdev;
2801
2802 hw = &adapter->hw;
2803 hw->back = adapter;
2804
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002805 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002806 adapter->state = __I40EVF_STARTUP;
2807
2808 /* Call save state here because it relies on the adapter struct. */
2809 pci_save_state(pdev);
2810
2811 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2812 pci_resource_len(pdev, 0));
2813 if (!hw->hw_addr) {
2814 err = -EIO;
2815 goto err_ioremap;
2816 }
2817 hw->vendor_id = pdev->vendor;
2818 hw->device_id = pdev->device;
2819 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2820 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2821 hw->subsystem_device_id = pdev->subsystem_device;
2822 hw->bus.device = PCI_SLOT(pdev->devfn);
2823 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002824 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002825
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002826 /* set up the locks for the AQ, do this only once in probe
2827 * and destroy them only once in remove
2828 */
2829 mutex_init(&hw->aq.asq_mutex);
2830 mutex_init(&hw->aq.arq_mutex);
2831
Serey Kong8bb1a542014-08-01 13:27:15 -07002832 INIT_LIST_HEAD(&adapter->mac_filter_list);
2833 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2834
Greg Rose5eae00c2013-12-21 06:12:45 +00002835 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2836 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2837 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002838 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002839 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002840 schedule_delayed_work(&adapter->init_task,
2841 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002842
2843 return 0;
2844
2845err_ioremap:
2846 free_netdev(netdev);
2847err_alloc_etherdev:
2848 pci_release_regions(pdev);
2849err_pci_reg:
2850err_dma:
2851 pci_disable_device(pdev);
2852 return err;
2853}
2854
2855#ifdef CONFIG_PM
2856/**
2857 * i40evf_suspend - Power management suspend routine
2858 * @pdev: PCI device information struct
2859 * @state: unused
2860 *
2861 * Called when the system (VM) is entering sleep/suspend.
2862 **/
2863static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2864{
2865 struct net_device *netdev = pci_get_drvdata(pdev);
2866 struct i40evf_adapter *adapter = netdev_priv(netdev);
2867 int retval = 0;
2868
2869 netif_device_detach(netdev);
2870
2871 if (netif_running(netdev)) {
2872 rtnl_lock();
2873 i40evf_down(adapter);
2874 rtnl_unlock();
2875 }
2876 i40evf_free_misc_irq(adapter);
2877 i40evf_reset_interrupt_capability(adapter);
2878
2879 retval = pci_save_state(pdev);
2880 if (retval)
2881 return retval;
2882
2883 pci_disable_device(pdev);
2884
2885 return 0;
2886}
2887
2888/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002889 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002890 * @pdev: PCI device information struct
2891 *
2892 * Called when the system (VM) is resumed from sleep/suspend.
2893 **/
2894static int i40evf_resume(struct pci_dev *pdev)
2895{
2896 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2897 struct net_device *netdev = adapter->netdev;
2898 u32 err;
2899
2900 pci_set_power_state(pdev, PCI_D0);
2901 pci_restore_state(pdev);
2902 /* pci_restore_state clears dev->state_saved so call
2903 * pci_save_state to restore it.
2904 */
2905 pci_save_state(pdev);
2906
2907 err = pci_enable_device_mem(pdev);
2908 if (err) {
2909 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2910 return err;
2911 }
2912 pci_set_master(pdev);
2913
2914 rtnl_lock();
2915 err = i40evf_set_interrupt_capability(adapter);
2916 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002917 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002918 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2919 return err;
2920 }
2921 err = i40evf_request_misc_irq(adapter);
2922 rtnl_unlock();
2923 if (err) {
2924 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2925 return err;
2926 }
2927
2928 schedule_work(&adapter->reset_task);
2929
2930 netif_device_attach(netdev);
2931
2932 return err;
2933}
2934
2935#endif /* CONFIG_PM */
2936/**
2937 * i40evf_remove - Device Removal Routine
2938 * @pdev: PCI device information struct
2939 *
2940 * i40evf_remove is called by the PCI subsystem to alert the driver
2941 * that it should release a PCI device. The could be caused by a
2942 * Hot-Plug event, or because the driver is going to be removed from
2943 * memory.
2944 **/
2945static void i40evf_remove(struct pci_dev *pdev)
2946{
2947 struct net_device *netdev = pci_get_drvdata(pdev);
2948 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002949 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002950 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002951 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002952
2953 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002954 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002955 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002956 if (adapter->netdev_registered) {
2957 unregister_netdev(netdev);
2958 adapter->netdev_registered = false;
2959 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002960 if (CLIENT_ALLOWED(adapter)) {
2961 err = i40evf_lan_del_device(adapter);
2962 if (err)
2963 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
2964 err);
2965 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002966
Mitch Williamsf4a71882015-01-09 11:18:16 +00002967 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002968 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002969 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002970 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002971 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002972 /* If the FW isn't responding, kick it once, but only once. */
2973 if (!i40evf_asq_done(hw)) {
2974 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002975 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002976 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08002977 i40evf_free_all_tx_resources(adapter);
2978 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08002979 i40evf_misc_irq_disable(adapter);
2980 i40evf_free_misc_irq(adapter);
2981 i40evf_reset_interrupt_capability(adapter);
2982 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002983
Mitch Williamse5d17c32014-06-04 04:22:38 +00002984 if (adapter->watchdog_timer.function)
2985 del_timer_sync(&adapter->watchdog_timer);
2986
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002987 flush_scheduled_work();
2988
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002989 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04002990
Greg Rose5eae00c2013-12-21 06:12:45 +00002991 if (hw->aq.asq.count)
2992 i40evf_shutdown_adminq(hw);
2993
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002994 /* destroy the locks only once, here */
2995 mutex_destroy(&hw->aq.arq_mutex);
2996 mutex_destroy(&hw->aq.asq_mutex);
2997
Greg Rose5eae00c2013-12-21 06:12:45 +00002998 iounmap(hw->hw_addr);
2999 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003000 i40evf_free_all_tx_resources(adapter);
3001 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003002 i40evf_free_queues(adapter);
3003 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003004 /* If we got removed before an up/down sequence, we've got a filter
3005 * hanging out there that we need to get rid of.
3006 */
3007 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3008 list_del(&f->list);
3009 kfree(f);
3010 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003011 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3012 list_del(&f->list);
3013 kfree(f);
3014 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003015
3016 free_netdev(netdev);
3017
3018 pci_disable_pcie_error_reporting(pdev);
3019
3020 pci_disable_device(pdev);
3021}
3022
3023static struct pci_driver i40evf_driver = {
3024 .name = i40evf_driver_name,
3025 .id_table = i40evf_pci_tbl,
3026 .probe = i40evf_probe,
3027 .remove = i40evf_remove,
3028#ifdef CONFIG_PM
3029 .suspend = i40evf_suspend,
3030 .resume = i40evf_resume,
3031#endif
3032 .shutdown = i40evf_shutdown,
3033};
3034
3035/**
3036 * i40e_init_module - Driver Registration Routine
3037 *
3038 * i40e_init_module is the first routine called when the driver is
3039 * loaded. All it does is register with the PCI subsystem.
3040 **/
3041static int __init i40evf_init_module(void)
3042{
3043 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003044
Greg Rose5eae00c2013-12-21 06:12:45 +00003045 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003046 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003047
3048 pr_info("%s\n", i40evf_copyright);
3049
Jacob Keller6992a6c2016-08-04 11:37:01 -07003050 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3051 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003052 if (!i40evf_wq) {
3053 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3054 return -ENOMEM;
3055 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003056 ret = pci_register_driver(&i40evf_driver);
3057 return ret;
3058}
3059
3060module_init(i40evf_init_module);
3061
3062/**
3063 * i40e_exit_module - Driver Exit Cleanup Routine
3064 *
3065 * i40e_exit_module is called just before the driver is removed
3066 * from memory.
3067 **/
3068static void __exit i40evf_exit_module(void)
3069{
3070 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003071 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003072}
3073
3074module_exit(i40evf_exit_module);
3075
3076/* i40evf_main.c */