blob: 9ee277e87f103d4df3fb4fcc6233105bc9d4e2cb [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Catherine Sullivaneaab59e2016-01-13 16:51:44 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Greg Rose5eae00c2013-12-21 06:12:45 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
Greg Rose5eae00c2013-12-21 06:12:45 +000018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40evf.h"
28#include "i40e_prototype.h"
Mitch Williamsed0e8942017-01-24 10:23:59 -080029#include "i40evf_client.h"
Scott Petersoned0980c2017-04-13 04:45:44 -040030/* All i40evf tracepoints are defined by the include below, which must
31 * be included exactly once across the whole kernel with
32 * CREATE_TRACE_POINTS defined
33 */
34#define CREATE_TRACE_POINTS
35#include "i40e_trace.h"
36
Greg Rose5eae00c2013-12-21 06:12:45 +000037static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39static int i40evf_close(struct net_device *netdev);
40
41char i40evf_driver_name[] = "i40evf";
42static const char i40evf_driver_string[] =
Catherine Sullivaneaab59e2016-01-13 16:51:44 -080043 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000044
Mitch Williams69ebe9552015-11-19 11:34:24 -080045#define DRV_KERN "-k"
46
Preethi Banalaabf709a2017-05-11 11:23:20 -070047#define DRV_VERSION_MAJOR 3
48#define DRV_VERSION_MINOR 0
49#define DRV_VERSION_BUILD 0
Mitch Williams69ebe9552015-11-19 11:34:24 -080050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) \
53 DRV_KERN
Greg Rose5eae00c2013-12-21 06:12:45 +000054const char i40evf_driver_version[] = DRV_VERSION;
55static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040056 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000057
58/* i40evf_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020066static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080067 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070068 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Preethi Banalaabf709a2017-05-11 11:23:20 -070070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000071 /* required last entry */
72 {0, }
73};
74
75MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
76
77MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(DRV_VERSION);
81
Jesse Brandeburg2803b162015-12-22 14:25:08 -080082static struct workqueue_struct *i40evf_wq;
83
Greg Rose5eae00c2013-12-21 06:12:45 +000084/**
85 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
86 * @hw: pointer to the HW structure
87 * @mem: ptr to mem struct to fill out
88 * @size: size of memory requested
89 * @alignment: what to align the allocation to
90 **/
91i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92 struct i40e_dma_mem *mem,
93 u64 size, u32 alignment)
94{
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
96
97 if (!mem)
98 return I40E_ERR_PARAM;
99
100 mem->size = ALIGN(size, alignment);
101 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
102 (dma_addr_t *)&mem->pa, GFP_KERNEL);
103 if (mem->va)
104 return 0;
105 else
106 return I40E_ERR_NO_MEMORY;
107}
108
109/**
110 * i40evf_free_dma_mem_d - OS specific memory free for shared code
111 * @hw: pointer to the HW structure
112 * @mem: ptr to mem struct to free
113 **/
114i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
115{
116 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
117
118 if (!mem || !mem->va)
119 return I40E_ERR_PARAM;
120 dma_free_coherent(&adapter->pdev->dev, mem->size,
121 mem->va, (dma_addr_t)mem->pa);
122 return 0;
123}
124
125/**
126 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to fill out
129 * @size: size of memory requested
130 **/
131i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem, u32 size)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 mem->size = size;
138 mem->va = kzalloc(size, GFP_KERNEL);
139
140 if (mem->va)
141 return 0;
142 else
143 return I40E_ERR_NO_MEMORY;
144}
145
146/**
147 * i40evf_free_virt_mem_d - OS specific memory free for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to free
150 **/
151i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152 struct i40e_virt_mem *mem)
153{
154 if (!mem)
155 return I40E_ERR_PARAM;
156
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159
160 return 0;
161}
162
163/**
164 * i40evf_debug_d - OS dependent version of debug printing
165 * @hw: pointer to the HW structure
166 * @mask: debug level mask
167 * @fmt_str: printf-type format description
168 **/
169void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
170{
171 char buf[512];
172 va_list argptr;
173
174 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
175 return;
176
177 va_start(argptr, fmt_str);
178 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
179 va_end(argptr);
180
181 /* the debug string is already formatted with a newline */
182 pr_info("%s", buf);
183}
184
185/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800186 * i40evf_schedule_reset - Set the flags and schedule a reset event
187 * @adapter: board private structure
188 **/
189void i40evf_schedule_reset(struct i40evf_adapter *adapter)
190{
191 if (!(adapter->flags &
192 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
193 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
194 schedule_work(&adapter->reset_task);
195 }
196}
197
198/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000199 * i40evf_tx_timeout - Respond to a Tx Hang
200 * @netdev: network interface device structure
201 **/
202static void i40evf_tx_timeout(struct net_device *netdev)
203{
204 struct i40evf_adapter *adapter = netdev_priv(netdev);
205
206 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800207 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000208}
209
210/**
211 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212 * @adapter: board private structure
213 **/
214static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
215{
216 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000217
Jacob Kelleref4603e2016-11-08 13:05:08 -0800218 if (!adapter->msix_entries)
219 return;
220
Greg Rose5eae00c2013-12-21 06:12:45 +0000221 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
222
223 /* read flush */
224 rd32(hw, I40E_VFGEN_RSTAT);
225
226 synchronize_irq(adapter->msix_entries[0].vector);
227}
228
229/**
230 * i40evf_misc_irq_enable - Enable default interrupt generation settings
231 * @adapter: board private structure
232 **/
233static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
234{
235 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000236
Greg Rose5eae00c2013-12-21 06:12:45 +0000237 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400239 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000240
241 /* read flush */
242 rd32(hw, I40E_VFGEN_RSTAT);
243}
244
245/**
246 * i40evf_irq_disable - Mask off interrupt generation on the NIC
247 * @adapter: board private structure
248 **/
249static void i40evf_irq_disable(struct i40evf_adapter *adapter)
250{
251 int i;
252 struct i40e_hw *hw = &adapter->hw;
253
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800254 if (!adapter->msix_entries)
255 return;
256
Greg Rose5eae00c2013-12-21 06:12:45 +0000257 for (i = 1; i < adapter->num_msix_vectors; i++) {
258 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
259 synchronize_irq(adapter->msix_entries[i].vector);
260 }
261 /* read flush */
262 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000263}
264
265/**
266 * i40evf_irq_enable_queues - Enable interrupt for specified queues
267 * @adapter: board private structure
268 * @mask: bitmap of queues to enable
269 **/
270void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
271{
272 struct i40e_hw *hw = &adapter->hw;
273 int i;
274
275 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400276 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000279 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400280 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000281 }
282 }
283}
284
285/**
286 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
287 * @adapter: board private structure
288 * @mask: bitmap of vectors to trigger
289 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000290static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000291{
292 struct i40e_hw *hw = &adapter->hw;
293 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800294 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000295
Mitch Williams164ec1b2014-06-04 08:45:19 +0000296 if (mask & 1) {
297 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400298 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000299 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400300 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000301 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
302 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000303 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400304 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000305 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400306 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000307 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400308 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000309 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
310 }
311 }
312}
313
314/**
315 * i40evf_irq_enable - Enable default interrupt generation settings
316 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600317 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000318 **/
319void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
320{
321 struct i40e_hw *hw = &adapter->hw;
322
Mitch Williams164ec1b2014-06-04 08:45:19 +0000323 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000324 i40evf_irq_enable_queues(adapter, ~0);
325
326 if (flush)
327 rd32(hw, I40E_VFGEN_RSTAT);
328}
329
330/**
331 * i40evf_msix_aq - Interrupt handler for vector 0
332 * @irq: interrupt number
333 * @data: pointer to netdev
334 **/
335static irqreturn_t i40evf_msix_aq(int irq, void *data)
336{
337 struct net_device *netdev = data;
338 struct i40evf_adapter *adapter = netdev_priv(netdev);
339 struct i40e_hw *hw = &adapter->hw;
340 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000341
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700342 /* handle non-queue interrupts, these reads clear the registers */
343 val = rd32(hw, I40E_VFINT_ICR01);
344 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000345
Jean Sacrened17f7e2015-10-13 01:06:30 -0600346 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
347 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000348 wr32(hw, I40E_VFINT_DYN_CTL01, val);
349
Greg Rose5eae00c2013-12-21 06:12:45 +0000350 /* schedule work on the private workqueue */
351 schedule_work(&adapter->adminq_task);
352
353 return IRQ_HANDLED;
354}
355
356/**
357 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
358 * @irq: interrupt number
359 * @data: pointer to a q_vector
360 **/
361static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
362{
363 struct i40e_q_vector *q_vector = data;
364
365 if (!q_vector->tx.ring && !q_vector->rx.ring)
366 return IRQ_HANDLED;
367
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700368 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000369
370 return IRQ_HANDLED;
371}
372
373/**
374 * i40evf_map_vector_to_rxq - associate irqs with rx queues
375 * @adapter: board private structure
376 * @v_idx: interrupt number
377 * @r_idx: queue number
378 **/
379static void
380i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
381{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400382 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400383 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700384 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000385
386 rx_ring->q_vector = q_vector;
387 rx_ring->next = q_vector->rx.ring;
388 rx_ring->vsi = &adapter->vsi;
389 q_vector->rx.ring = rx_ring;
390 q_vector->rx.count++;
391 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700392 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700393 q_vector->ring_mask |= BIT(r_idx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400394 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700395 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000396}
397
398/**
399 * i40evf_map_vector_to_txq - associate irqs with tx queues
400 * @adapter: board private structure
401 * @v_idx: interrupt number
402 * @t_idx: queue number
403 **/
404static void
405i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
406{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400407 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400408 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700409 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000410
411 tx_ring->q_vector = q_vector;
412 tx_ring->next = q_vector->tx.ring;
413 tx_ring->vsi = &adapter->vsi;
414 q_vector->tx.ring = tx_ring;
415 q_vector->tx.count++;
416 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700417 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400418 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000419 q_vector->num_ringpairs++;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700420 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000421}
422
423/**
424 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
425 * @adapter: board private structure to initialize
426 *
427 * This function maps descriptor rings to the queue-specific vectors
428 * we were allotted through the MSI-X enabling code. Ideally, we'd have
429 * one vector per ring/queue, but on a constrained vector budget, we
430 * group the rings as "efficiently" as possible. You would add new
431 * mapping configurations in here.
432 **/
433static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
434{
435 int q_vectors;
436 int v_start = 0;
437 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000438 int rxr_remaining = adapter->num_active_queues;
439 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000440 int i, j;
441 int rqpv, tqpv;
442 int err = 0;
443
444 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
445
446 /* The ideal configuration...
447 * We have enough vectors to map one per queue.
448 */
Mitch Williams973371d2015-04-27 14:57:09 -0400449 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000450 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
451 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
452
453 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
454 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
455 goto out;
456 }
457
458 /* If we don't have enough vectors for a 1-to-1
459 * mapping, we'll have to group them so there are
460 * multiple queues per vector.
461 * Re-adjusting *qpv takes care of the remainder.
462 */
463 for (i = v_start; i < q_vectors; i++) {
464 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
465 for (j = 0; j < rqpv; j++) {
466 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
467 rxr_idx++;
468 rxr_remaining--;
469 }
470 }
471 for (i = v_start; i < q_vectors; i++) {
472 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
473 for (j = 0; j < tqpv; j++) {
474 i40evf_map_vector_to_txq(adapter, i, txr_idx);
475 txr_idx++;
476 txr_remaining--;
477 }
478 }
479
480out:
481 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
482
483 return err;
484}
485
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700486#ifdef CONFIG_NET_POLL_CONTROLLER
487/**
488 * i40evf_netpoll - A Polling 'interrupt' handler
489 * @netdev: network interface device structure
490 *
491 * This is used by netconsole to send skbs without having to re-enable
492 * interrupts. It's not called while the normal interrupt routine is executing.
493 **/
494static void i40evf_netpoll(struct net_device *netdev)
495{
496 struct i40evf_adapter *adapter = netdev_priv(netdev);
497 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
498 int i;
499
500 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400501 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700502 return;
503
504 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400505 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700506}
507
508#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000509/**
Alan Brady96db7762016-09-14 16:24:38 -0700510 * i40evf_irq_affinity_notify - Callback for affinity changes
511 * @notify: context as to what irq was changed
512 * @mask: the new affinity mask
513 *
514 * This is a callback function used by the irq_set_affinity_notifier function
515 * so that we may register to receive changes to the irq affinity masks.
516 **/
517static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
518 const cpumask_t *mask)
519{
520 struct i40e_q_vector *q_vector =
521 container_of(notify, struct i40e_q_vector, affinity_notify);
522
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400523 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700524}
525
526/**
527 * i40evf_irq_affinity_release - Callback for affinity notifier release
528 * @ref: internal core kernel usage
529 *
530 * This is a callback function used by the irq_set_affinity_notifier function
531 * to inform the current notification subscriber that they will no longer
532 * receive notifications.
533 **/
534static void i40evf_irq_affinity_release(struct kref *ref) {}
535
536/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000537 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
538 * @adapter: board private structure
539 *
540 * Allocates MSI-X vectors for tx and rx handling, and requests
541 * interrupts from the kernel.
542 **/
543static int
544i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
545{
Jacob Keller696ac80a2017-07-12 05:46:11 -0400546 unsigned int vector, q_vectors;
547 unsigned int rx_int_idx = 0, tx_int_idx = 0;
548 int irq_num, err;
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) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400559 snprintf(q_vector->name, sizeof(q_vector->name),
560 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000561 tx_int_idx++;
562 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400563 snprintf(q_vector->name, sizeof(q_vector->name),
564 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000565 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400566 snprintf(q_vector->name, sizeof(q_vector->name),
567 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000568 } else {
569 /* skip this unused q_vector */
570 continue;
571 }
Alan Brady96db7762016-09-14 16:24:38 -0700572 err = request_irq(irq_num,
573 i40evf_msix_clean_rings,
574 0,
575 q_vector->name,
576 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000577 if (err) {
578 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400579 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000580 goto free_queue_irqs;
581 }
Alan Brady96db7762016-09-14 16:24:38 -0700582 /* register for affinity change notifications */
583 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
584 q_vector->affinity_notify.release =
585 i40evf_irq_affinity_release;
586 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Greg Rose5eae00c2013-12-21 06:12:45 +0000587 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -0700588 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +0000589 }
590
591 return 0;
592
593free_queue_irqs:
594 while (vector) {
595 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700596 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
597 irq_set_affinity_notifier(irq_num, NULL);
598 irq_set_affinity_hint(irq_num, NULL);
599 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000600 }
601 return err;
602}
603
604/**
605 * i40evf_request_misc_irq - Initialize MSI-X interrupts
606 * @adapter: board private structure
607 *
608 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
609 * vector is only for the admin queue, and stays active even when the netdev
610 * is closed.
611 **/
612static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
613{
614 struct net_device *netdev = adapter->netdev;
615 int err;
616
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700617 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000618 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
619 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000620 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800621 &i40evf_msix_aq, 0,
622 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000623 if (err) {
624 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800625 "request_irq for %s failed: %d\n",
626 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000627 free_irq(adapter->msix_entries[0].vector, netdev);
628 }
629 return err;
630}
631
632/**
633 * i40evf_free_traffic_irqs - Free MSI-X interrupts
634 * @adapter: board private structure
635 *
636 * Frees all MSI-X vectors other than 0.
637 **/
638static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
639{
Alan Brady96db7762016-09-14 16:24:38 -0700640 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000641
Alan Brady47d2a5d2016-11-08 13:05:05 -0800642 if (!adapter->msix_entries)
643 return;
644
Greg Rose5eae00c2013-12-21 06:12:45 +0000645 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
646
Alan Brady96db7762016-09-14 16:24:38 -0700647 for (vector = 0; vector < q_vectors; vector++) {
648 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
649 irq_set_affinity_notifier(irq_num, NULL);
650 irq_set_affinity_hint(irq_num, NULL);
651 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000652 }
653}
654
655/**
656 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
657 * @adapter: board private structure
658 *
659 * Frees MSI-X vector 0.
660 **/
661static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
662{
663 struct net_device *netdev = adapter->netdev;
664
Jacob Kelleref4603e2016-11-08 13:05:08 -0800665 if (!adapter->msix_entries)
666 return;
667
Greg Rose5eae00c2013-12-21 06:12:45 +0000668 free_irq(adapter->msix_entries[0].vector, netdev);
669}
670
671/**
672 * i40evf_configure_tx - Configure Transmit Unit after Reset
673 * @adapter: board private structure
674 *
675 * Configure the Tx unit of the MAC after a reset.
676 **/
677static void i40evf_configure_tx(struct i40evf_adapter *adapter)
678{
679 struct i40e_hw *hw = &adapter->hw;
680 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000681
Mitch Williamscc052922014-10-25 03:24:34 +0000682 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400683 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000684}
685
686/**
687 * i40evf_configure_rx - Configure Receive Unit after Reset
688 * @adapter: board private structure
689 *
690 * Configure the Rx unit of the MAC after a reset.
691 **/
692static void i40evf_configure_rx(struct i40evf_adapter *adapter)
693{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700694 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000695 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000696 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000697
Alexander Duyckdab86af2017-03-14 10:15:27 -0700698 /* Legacy Rx will always default to a 2048 buffer size. */
699#if (PAGE_SIZE < 8192)
700 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200701 struct net_device *netdev = adapter->netdev;
702
Alexander Duyck98efd692017-04-05 07:51:01 -0400703 /* For jumbo frames on systems with 4K pages we have to use
704 * an order 1 page, so we might as well increase the size
705 * of our Rx buffer to make better use of the available space
706 */
707 rx_buf_len = I40E_RXBUFFER_3072;
708
Alexander Duyckdab86af2017-03-14 10:15:27 -0700709 /* We use a 1536 buffer size for configurations with
710 * standard Ethernet mtu. On x86 this gives us enough room
711 * for shared info and 192 bytes of padding.
712 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400713 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
714 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700715 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
716 }
717#endif
718
Mitch Williamscc052922014-10-25 03:24:34 +0000719 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400720 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700721 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400722
723 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
724 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
725 else
726 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000727 }
728}
729
730/**
731 * i40evf_find_vlan - Search filter list for specific vlan filter
732 * @adapter: board private structure
733 * @vlan: vlan tag
734 *
735 * Returns ptr to the filter object or NULL
736 **/
737static struct
738i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
739{
740 struct i40evf_vlan_filter *f;
741
742 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
743 if (vlan == f->vlan)
744 return f;
745 }
746 return NULL;
747}
748
749/**
750 * i40evf_add_vlan - Add a vlan filter to the list
751 * @adapter: board private structure
752 * @vlan: VLAN tag
753 *
754 * Returns ptr to the filter object or NULL when no memory available.
755 **/
756static struct
757i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
758{
Mitch Williams13acb542015-03-31 00:45:05 -0700759 struct i40evf_vlan_filter *f = NULL;
760 int count = 50;
761
762 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
763 &adapter->crit_section)) {
764 udelay(1);
765 if (--count == 0)
766 goto out;
767 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000768
769 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000770 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000771 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000772 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700773 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000774
Greg Rose5eae00c2013-12-21 06:12:45 +0000775 f->vlan = vlan;
776
777 INIT_LIST_HEAD(&f->list);
778 list_add(&f->list, &adapter->vlan_filter_list);
779 f->add = true;
780 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
781 }
782
Mitch Williams13acb542015-03-31 00:45:05 -0700783clearout:
784 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
785out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000786 return f;
787}
788
789/**
790 * i40evf_del_vlan - Remove a vlan filter from the list
791 * @adapter: board private structure
792 * @vlan: VLAN tag
793 **/
794static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
795{
796 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700797 int count = 50;
798
799 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
800 &adapter->crit_section)) {
801 udelay(1);
802 if (--count == 0)
803 return;
804 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000805
806 f = i40evf_find_vlan(adapter, vlan);
807 if (f) {
808 f->remove = true;
809 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
810 }
Mitch Williams13acb542015-03-31 00:45:05 -0700811 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000812}
813
814/**
815 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
816 * @netdev: network device struct
817 * @vid: VLAN tag
818 **/
819static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000820 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000821{
822 struct i40evf_adapter *adapter = netdev_priv(netdev);
823
Mitch Williams8ed995f2015-08-28 17:55:57 -0400824 if (!VLAN_ALLOWED(adapter))
825 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000826 if (i40evf_add_vlan(adapter, vid) == NULL)
827 return -ENOMEM;
828 return 0;
829}
830
831/**
832 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
833 * @netdev: network device struct
834 * @vid: VLAN tag
835 **/
836static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000837 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000838{
839 struct i40evf_adapter *adapter = netdev_priv(netdev);
840
Mitch Williams8ed995f2015-08-28 17:55:57 -0400841 if (VLAN_ALLOWED(adapter)) {
842 i40evf_del_vlan(adapter, vid);
843 return 0;
844 }
845 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000846}
847
848/**
849 * i40evf_find_filter - Search filter list for specific mac filter
850 * @adapter: board private structure
851 * @macaddr: the MAC address
852 *
853 * Returns ptr to the filter object or NULL
854 **/
855static struct
856i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
857 u8 *macaddr)
858{
859 struct i40evf_mac_filter *f;
860
861 if (!macaddr)
862 return NULL;
863
864 list_for_each_entry(f, &adapter->mac_filter_list, list) {
865 if (ether_addr_equal(macaddr, f->macaddr))
866 return f;
867 }
868 return NULL;
869}
870
871/**
872 * i40e_add_filter - Add a mac filter to the filter list
873 * @adapter: board private structure
874 * @macaddr: the MAC address
875 *
876 * Returns ptr to the filter object or NULL when no memory available.
877 **/
878static struct
879i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
880 u8 *macaddr)
881{
882 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000883 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000884
885 if (!macaddr)
886 return NULL;
887
888 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000889 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000890 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000891 if (--count == 0)
892 return NULL;
893 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000894
895 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000896 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000897 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000898 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000899 clear_bit(__I40EVF_IN_CRITICAL_TASK,
900 &adapter->crit_section);
901 return NULL;
902 }
903
Greg Rose9a173902014-05-22 06:32:02 +0000904 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000905
Mitch Williams63590b62016-05-16 10:26:42 -0700906 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000907 f->add = true;
908 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
909 }
910
911 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
912 return f;
913}
914
915/**
916 * i40evf_set_mac - NDO callback to set port mac address
917 * @netdev: network interface device structure
918 * @p: pointer to an address structure
919 *
920 * Returns 0 on success, negative on failure
921 **/
922static int i40evf_set_mac(struct net_device *netdev, void *p)
923{
924 struct i40evf_adapter *adapter = netdev_priv(netdev);
925 struct i40e_hw *hw = &adapter->hw;
926 struct i40evf_mac_filter *f;
927 struct sockaddr *addr = p;
928
929 if (!is_valid_ether_addr(addr->sa_data))
930 return -EADDRNOTAVAIL;
931
932 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
933 return 0;
934
Mitch Williams14e52ee2015-08-31 19:54:44 -0400935 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
936 return -EPERM;
937
938 f = i40evf_find_filter(adapter, hw->mac.addr);
939 if (f) {
940 f->remove = true;
941 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
942 }
943
Greg Rose5eae00c2013-12-21 06:12:45 +0000944 f = i40evf_add_filter(adapter, addr->sa_data);
945 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000946 ether_addr_copy(hw->mac.addr, addr->sa_data);
947 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000948 }
949
950 return (f == NULL) ? -ENOMEM : 0;
951}
952
953/**
954 * i40evf_set_rx_mode - NDO callback to set the netdev filters
955 * @netdev: network interface device structure
956 **/
957static void i40evf_set_rx_mode(struct net_device *netdev)
958{
959 struct i40evf_adapter *adapter = netdev_priv(netdev);
960 struct i40evf_mac_filter *f, *ftmp;
961 struct netdev_hw_addr *uca;
962 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400963 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000964 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000965
966 /* add addr if not already in the filter list */
967 netdev_for_each_uc_addr(uca, netdev) {
968 i40evf_add_filter(adapter, uca->addr);
969 }
970 netdev_for_each_mc_addr(mca, netdev) {
971 i40evf_add_filter(adapter, mca->addr);
972 }
973
974 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000975 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000976 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000977 if (--count == 0) {
978 dev_err(&adapter->pdev->dev,
979 "Failed to get lock in %s\n", __func__);
980 return;
981 }
982 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000983 /* remove filter if not in netdev list */
984 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400985 netdev_for_each_mc_addr(mca, netdev)
986 if (ether_addr_equal(mca->addr, f->macaddr))
987 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000988
Shannon Nelson2f41f332015-08-26 15:14:20 -0400989 netdev_for_each_uc_addr(uca, netdev)
990 if (ether_addr_equal(uca->addr, f->macaddr))
991 goto bottom_of_search_loop;
992
993 for_each_dev_addr(netdev, ha)
994 if (ether_addr_equal(ha->addr, f->macaddr))
995 goto bottom_of_search_loop;
996
997 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
998 goto bottom_of_search_loop;
999
1000 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1001 f->remove = true;
1002 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1003
1004bottom_of_search_loop:
1005 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +00001006 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001007
1008 if (netdev->flags & IFF_PROMISC &&
1009 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
1010 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
1011 else if (!(netdev->flags & IFF_PROMISC) &&
1012 adapter->flags & I40EVF_FLAG_PROMISC_ON)
1013 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
1014
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001015 if (netdev->flags & IFF_ALLMULTI &&
1016 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
1017 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
1018 else if (!(netdev->flags & IFF_ALLMULTI) &&
1019 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
1020 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
1021
Greg Rose5eae00c2013-12-21 06:12:45 +00001022 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1023}
1024
1025/**
1026 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1027 * @adapter: board private structure
1028 **/
1029static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1030{
1031 int q_idx;
1032 struct i40e_q_vector *q_vector;
1033 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1034
1035 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1036 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001037
Mitch Williams7d96ba12015-10-26 19:44:39 -04001038 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001039 napi = &q_vector->napi;
1040 napi_enable(napi);
1041 }
1042}
1043
1044/**
1045 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1046 * @adapter: board private structure
1047 **/
1048static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1049{
1050 int q_idx;
1051 struct i40e_q_vector *q_vector;
1052 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1053
1054 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001055 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001056 napi_disable(&q_vector->napi);
1057 }
1058}
1059
1060/**
1061 * i40evf_configure - set up transmit and receive data structures
1062 * @adapter: board private structure
1063 **/
1064static void i40evf_configure(struct i40evf_adapter *adapter)
1065{
1066 struct net_device *netdev = adapter->netdev;
1067 int i;
1068
1069 i40evf_set_rx_mode(netdev);
1070
1071 i40evf_configure_tx(adapter);
1072 i40evf_configure_rx(adapter);
1073 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1074
Mitch Williamscc052922014-10-25 03:24:34 +00001075 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001076 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001077
Mitch Williamsb1630982016-04-18 11:33:48 -07001078 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001079 }
1080}
1081
1082/**
1083 * i40evf_up_complete - Finish the last steps of bringing up a connection
1084 * @adapter: board private structure
1085 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001086static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001087{
1088 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001089 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001090
1091 i40evf_napi_enable_all(adapter);
1092
1093 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001094 if (CLIENT_ENABLED(adapter))
1095 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001096 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001097}
1098
1099/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001100 * i40e_down - Shutdown the connection processing
1101 * @adapter: board private structure
1102 **/
1103void i40evf_down(struct i40evf_adapter *adapter)
1104{
1105 struct net_device *netdev = adapter->netdev;
1106 struct i40evf_mac_filter *f;
1107
Mitch Williams209dc4d2015-12-09 15:50:27 -08001108 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001109 return;
1110
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001111 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1112 &adapter->crit_section))
1113 usleep_range(500, 1000);
1114
Mitch Williams63e18c22015-03-27 00:12:10 -07001115 netif_carrier_off(netdev);
1116 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001117 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001118 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001119 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001120
Mitch Williamsef8693e2014-02-13 03:48:53 -08001121 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001122 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1123 f->remove = true;
1124 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001125 /* remove all VLAN filters */
1126 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1127 f->remove = true;
1128 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001129 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1130 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001131 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001132 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001133 /* Schedule operations to close down the HW. Don't wait
1134 * here for this to complete. The watchdog is still running
1135 * and it will take care of this.
1136 */
1137 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001138 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001139 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001140 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001141
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001142 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001143 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001144}
1145
1146/**
1147 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1148 * @adapter: board private structure
1149 * @vectors: number of vectors to request
1150 *
1151 * Work with the OS to set up the MSIX vectors needed.
1152 *
1153 * Returns 0 on success, negative on failure
1154 **/
1155static int
1156i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1157{
1158 int err, vector_threshold;
1159
1160 /* We'll want at least 3 (vector_threshold):
1161 * 0) Other (Admin Queue and link, mostly)
1162 * 1) TxQ[0] Cleanup
1163 * 2) RxQ[0] Cleanup
1164 */
1165 vector_threshold = MIN_MSIX_COUNT;
1166
1167 /* The more we get, the more we will assign to Tx/Rx Cleanup
1168 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1169 * Right now, we simply care about how many we'll get; we'll
1170 * set them up later while requesting irq's.
1171 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001172 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1173 vector_threshold, vectors);
1174 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001175 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001176 kfree(adapter->msix_entries);
1177 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001178 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001179 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001180
1181 /* Adjust for only the vectors we'll use, which is minimum
1182 * of max_msix_q_vectors + NONQ_VECS, or the number of
1183 * vectors we were allocated.
1184 */
1185 adapter->num_msix_vectors = err;
1186 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001187}
1188
1189/**
1190 * i40evf_free_queues - Free memory for all rings
1191 * @adapter: board private structure to initialize
1192 *
1193 * Free all of the memory associated with queue pairs.
1194 **/
1195static void i40evf_free_queues(struct i40evf_adapter *adapter)
1196{
Greg Rose5eae00c2013-12-21 06:12:45 +00001197 if (!adapter->vsi_res)
1198 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001199 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001200 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001201 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001202 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001203 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001204}
1205
1206/**
1207 * i40evf_alloc_queues - Allocate memory for all rings
1208 * @adapter: board private structure to initialize
1209 *
1210 * We allocate one ring per queue at run-time since we don't know the
1211 * number of queues at compile-time. The polling_netdev array is
1212 * intended for Multiqueue, but should work fine with a single queue.
1213 **/
1214static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1215{
Jacob Keller65c70062017-06-07 05:43:01 -04001216 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001217
Jacob Keller65c70062017-06-07 05:43:01 -04001218 num_active_queues = min_t(int,
1219 adapter->vsi_res->num_queue_pairs,
1220 (int)(num_online_cpus()));
1221
1222 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001223 sizeof(struct i40e_ring), GFP_KERNEL);
1224 if (!adapter->tx_rings)
1225 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001226 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001227 sizeof(struct i40e_ring), GFP_KERNEL);
1228 if (!adapter->rx_rings)
1229 goto err_out;
1230
Jacob Keller65c70062017-06-07 05:43:01 -04001231 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001232 struct i40e_ring *tx_ring;
1233 struct i40e_ring *rx_ring;
1234
Mitch Williams0dd438d2015-10-26 19:44:40 -04001235 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001236
1237 tx_ring->queue_index = i;
1238 tx_ring->netdev = adapter->netdev;
1239 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001240 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001241 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001242 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001243 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001244
Mitch Williams0dd438d2015-10-26 19:44:40 -04001245 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001246 rx_ring->queue_index = i;
1247 rx_ring->netdev = adapter->netdev;
1248 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001249 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001250 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001251 }
1252
Jacob Keller65c70062017-06-07 05:43:01 -04001253 adapter->num_active_queues = num_active_queues;
1254
Greg Rose5eae00c2013-12-21 06:12:45 +00001255 return 0;
1256
1257err_out:
1258 i40evf_free_queues(adapter);
1259 return -ENOMEM;
1260}
1261
1262/**
1263 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1264 * @adapter: board private structure to initialize
1265 *
1266 * Attempt to configure the interrupts using the best available
1267 * capabilities of the hardware and the kernel.
1268 **/
1269static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1270{
1271 int vector, v_budget;
1272 int pairs = 0;
1273 int err = 0;
1274
1275 if (!adapter->vsi_res) {
1276 err = -EIO;
1277 goto out;
1278 }
Mitch Williamscc052922014-10-25 03:24:34 +00001279 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001280
Jacob Keller789f38c2017-04-19 09:25:56 -04001281 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1282 * us much good if we have more vectors than CPUs. However, we already
1283 * limit the total number of queues by the number of CPUs so we do not
1284 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001285 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001286 v_budget = min_t(int, pairs + NONQ_VECS,
1287 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001288
Greg Rose5eae00c2013-12-21 06:12:45 +00001289 adapter->msix_entries = kcalloc(v_budget,
1290 sizeof(struct msix_entry), GFP_KERNEL);
1291 if (!adapter->msix_entries) {
1292 err = -ENOMEM;
1293 goto out;
1294 }
1295
1296 for (vector = 0; vector < v_budget; vector++)
1297 adapter->msix_entries[vector].entry = vector;
1298
Mitch Williams313ed2d2015-08-27 11:42:31 -04001299 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001300
1301out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001302 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1303 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001304 return err;
1305}
1306
1307/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001308 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1309 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001310 *
1311 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001312 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001313static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001314{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001315 struct i40e_aqc_get_set_rss_key_data *rss_key =
1316 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001317 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001318 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001320 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001321 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001322 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001323 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001324 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001325 }
1326
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001327 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1328 if (ret) {
1329 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1330 i40evf_stat_str(hw, ret),
1331 i40evf_aq_str(hw, hw->aq.asq_last_status));
1332 return ret;
1333
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001334 }
1335
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001336 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1337 adapter->rss_lut, adapter->rss_lut_size);
1338 if (ret) {
1339 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1340 i40evf_stat_str(hw, ret),
1341 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001342 }
1343
1344 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001345
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001346}
1347
1348/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001349 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001350 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001351 *
1352 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001353 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001354static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001355{
1356 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001357 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001358 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001359
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001360 dw = (u32 *)adapter->rss_key;
1361 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1362 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001363
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001364 dw = (u32 *)adapter->rss_lut;
1365 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1366 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001367
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001368 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001369
1370 return 0;
1371}
1372
1373/**
1374 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001375 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001376 *
1377 * Returns 0 on success, negative on failure
1378 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001379int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001380{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001381
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001382 if (RSS_PF(adapter)) {
1383 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1384 I40EVF_FLAG_AQ_SET_RSS_KEY;
1385 return 0;
1386 } else if (RSS_AQ(adapter)) {
1387 return i40evf_config_rss_aq(adapter);
1388 } else {
1389 return i40evf_config_rss_reg(adapter);
1390 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001391}
1392
1393/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001394 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001395 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001396 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001397static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001398{
1399 u16 i;
1400
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001401 for (i = 0; i < adapter->rss_lut_size; i++)
1402 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001403}
1404
1405/**
Helin Zhang96a81982015-10-26 19:44:31 -04001406 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001407 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001408 *
1409 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001410 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001411static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001412{
1413 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001414 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001415
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001416 if (!RSS_PF(adapter)) {
1417 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001418 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001419 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001420 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1421 else
1422 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001423
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001424 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1425 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1426 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001427
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001428 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001429
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001430 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1431 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001432
1433 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001434}
1435
1436/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001437 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1438 * @adapter: board private structure to initialize
1439 *
1440 * We allocate one q_vector per queue interrupt. If allocation fails we
1441 * return -ENOMEM.
1442 **/
1443static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1444{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001445 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001446 struct i40e_q_vector *q_vector;
1447
1448 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001449 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001450 GFP_KERNEL);
1451 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001452 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001453
1454 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001455 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001456 q_vector->adapter = adapter;
1457 q_vector->vsi = &adapter->vsi;
1458 q_vector->v_idx = q_idx;
1459 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001460 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001461 }
1462
1463 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001464}
1465
1466/**
1467 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1468 * @adapter: board private structure to initialize
1469 *
1470 * This function frees the memory allocated to the q_vectors. In addition if
1471 * NAPI is enabled it will delete any references to the NAPI struct prior
1472 * to freeing the q_vector.
1473 **/
1474static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1475{
1476 int q_idx, num_q_vectors;
1477 int napi_vectors;
1478
Jacob Kelleref4603e2016-11-08 13:05:08 -08001479 if (!adapter->q_vectors)
1480 return;
1481
Greg Rose5eae00c2013-12-21 06:12:45 +00001482 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001483 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001484
1485 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001486 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001487 if (q_idx < napi_vectors)
1488 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001489 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001490 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001491 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001492}
1493
1494/**
1495 * i40evf_reset_interrupt_capability - Reset MSIX setup
1496 * @adapter: board private structure
1497 *
1498 **/
1499void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1500{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001501 if (!adapter->msix_entries)
1502 return;
1503
Greg Rose5eae00c2013-12-21 06:12:45 +00001504 pci_disable_msix(adapter->pdev);
1505 kfree(adapter->msix_entries);
1506 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001507}
1508
1509/**
1510 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1511 * @adapter: board private structure to initialize
1512 *
1513 **/
1514int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1515{
1516 int err;
1517
Jacob Keller283aeaf2017-04-19 09:25:59 -04001518 err = i40evf_alloc_queues(adapter);
1519 if (err) {
1520 dev_err(&adapter->pdev->dev,
1521 "Unable to allocate memory for queues\n");
1522 goto err_alloc_queues;
1523 }
1524
Jacob Keller62fe2a82016-07-27 12:02:33 -07001525 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001526 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001527 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001528 if (err) {
1529 dev_err(&adapter->pdev->dev,
1530 "Unable to setup interrupt capabilities\n");
1531 goto err_set_interrupt;
1532 }
1533
1534 err = i40evf_alloc_q_vectors(adapter);
1535 if (err) {
1536 dev_err(&adapter->pdev->dev,
1537 "Unable to allocate memory for queue vectors\n");
1538 goto err_alloc_q_vectors;
1539 }
1540
Greg Rose5eae00c2013-12-21 06:12:45 +00001541 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001542 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1543 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001544
1545 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001546err_alloc_q_vectors:
1547 i40evf_reset_interrupt_capability(adapter);
1548err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001549 i40evf_free_queues(adapter);
1550err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001551 return err;
1552}
1553
1554/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001555 * i40evf_free_rss - Free memory used by RSS structs
1556 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001557 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001558static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001559{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001560 kfree(adapter->rss_key);
1561 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001562
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001563 kfree(adapter->rss_lut);
1564 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001565}
1566
1567/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001568 * i40evf_watchdog_timer - Periodic call-back timer
1569 * @data: pointer to adapter disguised as unsigned long
1570 **/
1571static void i40evf_watchdog_timer(unsigned long data)
1572{
1573 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001574
Greg Rose5eae00c2013-12-21 06:12:45 +00001575 schedule_work(&adapter->watchdog_task);
1576 /* timer will be rescheduled in watchdog task */
1577}
1578
1579/**
1580 * i40evf_watchdog_task - Periodic call-back task
1581 * @work: pointer to work_struct
1582 **/
1583static void i40evf_watchdog_task(struct work_struct *work)
1584{
1585 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001586 struct i40evf_adapter,
1587 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001588 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001589 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001590
Greg Rose5eae00c2013-12-21 06:12:45 +00001591 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001592 goto restart_watchdog;
1593
1594 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001595 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1596 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001597 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1598 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001599 /* A chance for redemption! */
1600 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1601 adapter->state = __I40EVF_STARTUP;
1602 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1603 schedule_delayed_work(&adapter->init_task, 10);
1604 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1605 &adapter->crit_section);
1606 /* Don't reschedule the watchdog, since we've restarted
1607 * the init task. When init_task contacts the PF and
1608 * gets everything set up again, it'll restart the
1609 * watchdog for us. Down, boy. Sit. Stay. Woof.
1610 */
1611 return;
1612 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001613 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001614 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001615 goto watchdog_done;
1616 }
1617
1618 if ((adapter->state < __I40EVF_DOWN) ||
1619 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001620 goto watchdog_done;
1621
Mitch Williamsef8693e2014-02-13 03:48:53 -08001622 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001623 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1624 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001625 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001626 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001627 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001628 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001629 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001630 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001631 goto watchdog_done;
1632 }
1633
1634 /* Process admin queue tasks. After init, everything gets done
1635 * here so we don't race on the admin queue.
1636 */
Mitch Williamsed636962015-04-07 19:45:32 -04001637 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001638 if (!i40evf_asq_done(hw)) {
1639 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1640 i40evf_send_api_ver(adapter);
1641 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001642 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001643 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001644 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1645 i40evf_send_vf_config_msg(adapter);
1646 goto watchdog_done;
1647 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001648
Mitch Williamse284fc82015-03-27 00:12:09 -07001649 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1650 i40evf_disable_queues(adapter);
1651 goto watchdog_done;
1652 }
1653
Greg Rose5eae00c2013-12-21 06:12:45 +00001654 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1655 i40evf_map_queues(adapter);
1656 goto watchdog_done;
1657 }
1658
1659 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1660 i40evf_add_ether_addrs(adapter);
1661 goto watchdog_done;
1662 }
1663
1664 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1665 i40evf_add_vlans(adapter);
1666 goto watchdog_done;
1667 }
1668
1669 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1670 i40evf_del_ether_addrs(adapter);
1671 goto watchdog_done;
1672 }
1673
1674 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1675 i40evf_del_vlans(adapter);
1676 goto watchdog_done;
1677 }
1678
Mariusz Stachura87743702017-07-17 22:09:45 -07001679 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1680 i40evf_enable_vlan_stripping(adapter);
1681 goto watchdog_done;
1682 }
1683
1684 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1685 i40evf_disable_vlan_stripping(adapter);
1686 goto watchdog_done;
1687 }
1688
Greg Rose5eae00c2013-12-21 06:12:45 +00001689 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1690 i40evf_configure_queues(adapter);
1691 goto watchdog_done;
1692 }
1693
1694 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1695 i40evf_enable_queues(adapter);
1696 goto watchdog_done;
1697 }
1698
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001699 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1700 /* This message goes straight to the firmware, not the
1701 * PF, so we don't have to set current_op as we will
1702 * not get a response through the ARQ.
1703 */
Helin Zhang96a81982015-10-26 19:44:31 -04001704 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001705 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1706 goto watchdog_done;
1707 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001708 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1709 i40evf_get_hena(adapter);
1710 goto watchdog_done;
1711 }
1712 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1713 i40evf_set_hena(adapter);
1714 goto watchdog_done;
1715 }
1716 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1717 i40evf_set_rss_key(adapter);
1718 goto watchdog_done;
1719 }
1720 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1721 i40evf_set_rss_lut(adapter);
1722 goto watchdog_done;
1723 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001724
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001725 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001726 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1727 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001728 goto watchdog_done;
1729 }
1730
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001731 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001732 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001733 goto watchdog_done;
1734 }
1735
1736 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1737 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001738 i40evf_set_promiscuous(adapter, 0);
1739 goto watchdog_done;
1740 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001741 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001742
Greg Rose5eae00c2013-12-21 06:12:45 +00001743 if (adapter->state == __I40EVF_RUNNING)
1744 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001745watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001746 if (adapter->state == __I40EVF_RUNNING) {
1747 i40evf_irq_enable_queues(adapter, ~0);
1748 i40evf_fire_sw_int(adapter, 0xFF);
1749 } else {
1750 i40evf_fire_sw_int(adapter, 0x1);
1751 }
1752
Mitch Williamsef8693e2014-02-13 03:48:53 -08001753 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1754restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001755 if (adapter->state == __I40EVF_REMOVE)
1756 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001757 if (adapter->aq_required)
1758 mod_timer(&adapter->watchdog_timer,
1759 jiffies + msecs_to_jiffies(20));
1760 else
1761 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001762 schedule_work(&adapter->adminq_task);
1763}
1764
Joe Perchesdedecb62016-11-01 15:35:14 -07001765static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1766{
1767 struct i40evf_mac_filter *f, *ftmp;
1768 struct i40evf_vlan_filter *fv, *fvtmp;
1769
1770 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1771
1772 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001773 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001774 netif_carrier_off(adapter->netdev);
1775 netif_tx_disable(adapter->netdev);
1776 adapter->link_up = false;
1777 i40evf_napi_disable_all(adapter);
1778 i40evf_irq_disable(adapter);
1779 i40evf_free_traffic_irqs(adapter);
1780 i40evf_free_all_tx_resources(adapter);
1781 i40evf_free_all_rx_resources(adapter);
1782 }
1783
1784 /* Delete all of the filters, both MAC and VLAN. */
1785 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1786 list_del(&f->list);
1787 kfree(f);
1788 }
1789
1790 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1791 list_del(&fv->list);
1792 kfree(fv);
1793 }
1794
1795 i40evf_free_misc_irq(adapter);
1796 i40evf_reset_interrupt_capability(adapter);
1797 i40evf_free_queues(adapter);
1798 i40evf_free_q_vectors(adapter);
1799 kfree(adapter->vf_res);
1800 i40evf_shutdown_adminq(&adapter->hw);
1801 adapter->netdev->flags &= ~IFF_UP;
1802 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1803 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1804 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001805 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001806 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1807}
1808
Mitch Williams67c818a2015-06-19 08:56:30 -07001809#define I40EVF_RESET_WAIT_MS 10
1810#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001811/**
1812 * i40evf_reset_task - Call-back task to handle hardware reset
1813 * @work: pointer to work_struct
1814 *
1815 * During reset we need to shut down and reinitialize the admin queue
1816 * before we can use it to communicate with the PF again. We also clear
1817 * and reinit the rings because that context is lost as well.
1818 **/
1819static void i40evf_reset_task(struct work_struct *work)
1820{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001821 struct i40evf_adapter *adapter = container_of(work,
1822 struct i40evf_adapter,
1823 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001824 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001825 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001826 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001827 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001828 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001829 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001830
Mitch Williamsed0e8942017-01-24 10:23:59 -08001831 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001832 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001833 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001834 if (CLIENT_ENABLED(adapter)) {
1835 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1836 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1837 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1838 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1839 cancel_delayed_work_sync(&adapter->client_task);
1840 i40evf_notify_client_close(&adapter->vsi, true);
1841 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001842 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001843 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001844 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1845 /* Restart the AQ here. If we have been reset but didn't
1846 * detect it, or if the PF had to reinit, our AQ will be hosed.
1847 */
1848 i40evf_shutdown_adminq(hw);
1849 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001850 i40evf_request_reset(adapter);
1851 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001852 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001853
Mitch Williamsef8693e2014-02-13 03:48:53 -08001854 /* poll until we see the reset actually happen */
1855 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001856 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1857 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1858 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001859 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001860 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001861 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001862 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001863 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001864 goto continue_reset; /* act like the reset happened */
1865 }
1866
1867 /* wait until the reset is complete and the PF is responding to us */
1868 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001869 /* sleep first to make sure a minimum wait time is met */
1870 msleep(I40EVF_RESET_WAIT_MS);
1871
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001872 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1873 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001874 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001875 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001876 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001877
Mitch Williams509a4472015-12-23 12:05:52 -08001878 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001879
Mitch Williamsef8693e2014-02-13 03:48:53 -08001880 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001881 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001882 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001883 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001884 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001885 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001886 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001887
1888continue_reset:
Alan Bradyf0db7892017-07-12 05:46:04 -04001889 if (netif_running(netdev)) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001890 netif_carrier_off(netdev);
1891 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001892 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001893 i40evf_napi_disable_all(adapter);
1894 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001895 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001896
Greg Rose5eae00c2013-12-21 06:12:45 +00001897 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001898 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1899
1900 /* free the Tx/Rx rings and descriptors, might be better to just
1901 * re-use them sometime in the future
1902 */
1903 i40evf_free_all_rx_resources(adapter);
1904 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001905
1906 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001907 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001908 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001909 err = i40evf_init_adminq(hw);
1910 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001911 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1912 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001913
Mitch Williamse6d038d2015-06-04 16:23:58 -04001914 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1915 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001916
1917 /* re-add all MAC filters */
1918 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1919 f->add = true;
1920 }
1921 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001922 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1923 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001924 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001925 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001926 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001927 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001928 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001929 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001930
1931 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1932
1933 if (netif_running(adapter->netdev)) {
1934 /* allocate transmit descriptors */
1935 err = i40evf_setup_all_tx_resources(adapter);
1936 if (err)
1937 goto reset_err;
1938
1939 /* allocate receive descriptors */
1940 err = i40evf_setup_all_rx_resources(adapter);
1941 if (err)
1942 goto reset_err;
1943
1944 i40evf_configure(adapter);
1945
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001946 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001947
1948 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001949 } else {
1950 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001951 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001952 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001953
Greg Rose5eae00c2013-12-21 06:12:45 +00001954 return;
1955reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001956 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001957 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001958}
1959
1960/**
1961 * i40evf_adminq_task - worker thread to clean the admin queue
1962 * @work: pointer to work_struct containing our data
1963 **/
1964static void i40evf_adminq_task(struct work_struct *work)
1965{
1966 struct i40evf_adapter *adapter =
1967 container_of(work, struct i40evf_adapter, adminq_task);
1968 struct i40e_hw *hw = &adapter->hw;
1969 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001970 enum virtchnl_ops v_op;
1971 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001972 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001973 u16 pending;
1974
Mitch Williamsef8693e2014-02-13 03:48:53 -08001975 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001976 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001977
Mitch Williams1001dc32014-11-11 20:02:19 +00001978 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1979 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001980 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001981 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001982
Greg Rose5eae00c2013-12-21 06:12:45 +00001983 do {
1984 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07001985 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
1986 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
1987
1988 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00001989 break; /* No event to process or error cleaning ARQ */
1990
Tushar Davec969ef42017-06-22 09:44:32 -07001991 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001992 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001993 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001994 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001995 } while (pending);
1996
Mitch Williams67c818a2015-06-19 08:56:30 -07001997 if ((adapter->flags &
1998 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1999 adapter->state == __I40EVF_RESETTING)
2000 goto freedom;
2001
Mitch Williams912257e2014-05-22 06:32:07 +00002002 /* check for error indications */
2003 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002004 if (val == 0xdeadbeef) /* indicates device in reset */
2005 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002006 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002007 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002008 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002009 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002010 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002011 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002012 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002013 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002014 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002015 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002016 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002017 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002018 }
2019 if (oldval != val)
2020 wr32(hw, hw->aq.arq.len, val);
2021
2022 val = rd32(hw, hw->aq.asq.len);
2023 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002024 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002025 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002026 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002027 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002028 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002029 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002030 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002031 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002032 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002033 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002034 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002035 }
2036 if (oldval != val)
2037 wr32(hw, hw->aq.asq.len, val);
2038
Mitch Williams67c818a2015-06-19 08:56:30 -07002039freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002040 kfree(event.msg_buf);
2041out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002042 /* re-enable Admin queue interrupt cause */
2043 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002044}
2045
2046/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002047 * i40evf_client_task - worker thread to perform client work
2048 * @work: pointer to work_struct containing our data
2049 *
2050 * This task handles client interactions. Because client calls can be
2051 * reentrant, we can't handle them in the watchdog.
2052 **/
2053static void i40evf_client_task(struct work_struct *work)
2054{
2055 struct i40evf_adapter *adapter =
2056 container_of(work, struct i40evf_adapter, client_task.work);
2057
2058 /* If we can't get the client bit, just give up. We'll be rescheduled
2059 * later.
2060 */
2061
2062 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2063 return;
2064
2065 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2066 i40evf_client_subtask(adapter);
2067 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2068 goto out;
2069 }
2070 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2071 i40evf_notify_client_close(&adapter->vsi, false);
2072 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2073 goto out;
2074 }
2075 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2076 i40evf_notify_client_open(&adapter->vsi);
2077 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2078 goto out;
2079 }
2080 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2081 i40evf_notify_client_l2_params(&adapter->vsi);
2082 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2083 }
2084out:
2085 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2086}
2087
2088/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002089 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2090 * @adapter: board private structure
2091 *
2092 * Free all transmit software resources
2093 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002094void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002095{
2096 int i;
2097
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002098 if (!adapter->tx_rings)
2099 return;
2100
Mitch Williamscc052922014-10-25 03:24:34 +00002101 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002102 if (adapter->tx_rings[i].desc)
2103 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002104}
2105
2106/**
2107 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2108 * @adapter: board private structure
2109 *
2110 * If this function returns with an error, then it's possible one or
2111 * more of the rings is populated (while the rest are not). It is the
2112 * callers duty to clean those orphaned rings.
2113 *
2114 * Return 0 on success, negative on failure
2115 **/
2116static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2117{
2118 int i, err = 0;
2119
Mitch Williamscc052922014-10-25 03:24:34 +00002120 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002121 adapter->tx_rings[i].count = adapter->tx_desc_count;
2122 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002123 if (!err)
2124 continue;
2125 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002126 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002127 break;
2128 }
2129
2130 return err;
2131}
2132
2133/**
2134 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2135 * @adapter: board private structure
2136 *
2137 * If this function returns with an error, then it's possible one or
2138 * more of the rings is populated (while the rest are not). It is the
2139 * callers duty to clean those orphaned rings.
2140 *
2141 * Return 0 on success, negative on failure
2142 **/
2143static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2144{
2145 int i, err = 0;
2146
Mitch Williamscc052922014-10-25 03:24:34 +00002147 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002148 adapter->rx_rings[i].count = adapter->rx_desc_count;
2149 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002150 if (!err)
2151 continue;
2152 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002153 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002154 break;
2155 }
2156 return err;
2157}
2158
2159/**
2160 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2161 * @adapter: board private structure
2162 *
2163 * Free all receive software resources
2164 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002165void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002166{
2167 int i;
2168
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002169 if (!adapter->rx_rings)
2170 return;
2171
Mitch Williamscc052922014-10-25 03:24:34 +00002172 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002173 if (adapter->rx_rings[i].desc)
2174 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002175}
2176
2177/**
2178 * i40evf_open - Called when a network interface is made active
2179 * @netdev: network interface device structure
2180 *
2181 * Returns 0 on success, negative value on failure
2182 *
2183 * The open entry point is called when a network interface is made
2184 * active by the system (IFF_UP). At this point all resources needed
2185 * for transmit and receive operations are allocated, the interrupt
2186 * handler is registered with the OS, the watchdog timer is started,
2187 * and the stack is notified that the interface is ready.
2188 **/
2189static int i40evf_open(struct net_device *netdev)
2190{
2191 struct i40evf_adapter *adapter = netdev_priv(netdev);
2192 int err;
2193
Mitch Williamsef8693e2014-02-13 03:48:53 -08002194 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2195 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2196 return -EIO;
2197 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002198
2199 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002200 return -EBUSY;
2201
2202 /* allocate transmit descriptors */
2203 err = i40evf_setup_all_tx_resources(adapter);
2204 if (err)
2205 goto err_setup_tx;
2206
2207 /* allocate receive descriptors */
2208 err = i40evf_setup_all_rx_resources(adapter);
2209 if (err)
2210 goto err_setup_rx;
2211
2212 /* clear any pending interrupts, may auto mask */
2213 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2214 if (err)
2215 goto err_req_irq;
2216
Mitch Williams44151cd2015-04-27 14:57:17 -04002217 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002218 i40evf_configure(adapter);
2219
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002220 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002221
2222 i40evf_irq_enable(adapter, true);
2223
2224 return 0;
2225
2226err_req_irq:
2227 i40evf_down(adapter);
2228 i40evf_free_traffic_irqs(adapter);
2229err_setup_rx:
2230 i40evf_free_all_rx_resources(adapter);
2231err_setup_tx:
2232 i40evf_free_all_tx_resources(adapter);
2233
2234 return err;
2235}
2236
2237/**
2238 * i40evf_close - Disables a network interface
2239 * @netdev: network interface device structure
2240 *
2241 * Returns 0, this is not allowed to fail
2242 *
2243 * The close entry point is called when an interface is de-activated
2244 * by the OS. The hardware is still under the drivers control, but
2245 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2246 * are freed, along with all transmit and receive resources.
2247 **/
2248static int i40evf_close(struct net_device *netdev)
2249{
2250 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002251 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002252
Mitch Williams209dc4d2015-12-09 15:50:27 -08002253 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002254 return 0;
2255
Mitch Williamsef8693e2014-02-13 03:48:53 -08002256
Jacob Keller0da36b92017-04-19 09:25:55 -04002257 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002258 if (CLIENT_ENABLED(adapter))
2259 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002260
2261 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002262 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002263 i40evf_free_traffic_irqs(adapter);
2264
Mitch Williams51f38262016-12-12 15:44:11 -08002265 /* We explicitly don't free resources here because the hardware is
2266 * still active and can DMA into memory. Resources are cleared in
2267 * i40evf_virtchnl_completion() after we get confirmation from the PF
2268 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002269 *
2270 * Also, we wait for state to transition to __I40EVF_DOWN before
2271 * returning. State change occurs in i40evf_virtchnl_completion() after
2272 * VF resources are released (which occurs after PF driver processes and
2273 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002274 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002275
2276 status = wait_event_timeout(adapter->down_waitqueue,
2277 adapter->state == __I40EVF_DOWN,
2278 msecs_to_jiffies(200));
2279 if (!status)
2280 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002281 return 0;
2282}
2283
2284/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002285 * i40evf_change_mtu - Change the Maximum Transfer Unit
2286 * @netdev: network interface device structure
2287 * @new_mtu: new value for maximum frame size
2288 *
2289 * Returns 0 on success, negative on failure
2290 **/
2291static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2292{
2293 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002294
Greg Rose5eae00c2013-12-21 06:12:45 +00002295 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002296 if (CLIENT_ENABLED(adapter)) {
2297 i40evf_notify_client_l2_params(&adapter->vsi);
2298 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2299 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002300 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2301 schedule_work(&adapter->reset_task);
2302
Greg Rose5eae00c2013-12-21 06:12:45 +00002303 return 0;
2304}
2305
Alexander Duyck06fc0162016-10-25 16:08:47 -07002306/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002307 * i40e_set_features - set the netdev feature flags
2308 * @netdev: ptr to the netdev being adjusted
2309 * @features: the feature set that the stack is suggesting
2310 * Note: expects to be called while under rtnl_lock()
2311 **/
2312static int i40evf_set_features(struct net_device *netdev,
2313 netdev_features_t features)
2314{
2315 struct i40evf_adapter *adapter = netdev_priv(netdev);
2316
2317 if (!VLAN_ALLOWED(adapter))
2318 return -EINVAL;
2319
2320 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2321 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2322 else
2323 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2324
2325 return 0;
2326}
2327
2328/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002329 * i40evf_features_check - Validate encapsulated packet conforms to limits
2330 * @skb: skb buff
2331 * @netdev: This physical port's netdev
2332 * @features: Offload features that the stack believes apply
2333 **/
2334static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2335 struct net_device *dev,
2336 netdev_features_t features)
2337{
2338 size_t len;
2339
2340 /* No point in doing any of this if neither checksum nor GSO are
2341 * being requested for this frame. We can rule out both by just
2342 * checking for CHECKSUM_PARTIAL
2343 */
2344 if (skb->ip_summed != CHECKSUM_PARTIAL)
2345 return features;
2346
2347 /* We cannot support GSO if the MSS is going to be less than
2348 * 64 bytes. If it is then we need to drop support for GSO.
2349 */
2350 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2351 features &= ~NETIF_F_GSO_MASK;
2352
2353 /* MACLEN can support at most 63 words */
2354 len = skb_network_header(skb) - skb->data;
2355 if (len & ~(63 * 2))
2356 goto out_err;
2357
2358 /* IPLEN and EIPLEN can support at most 127 dwords */
2359 len = skb_transport_header(skb) - skb_network_header(skb);
2360 if (len & ~(127 * 4))
2361 goto out_err;
2362
2363 if (skb->encapsulation) {
2364 /* L4TUNLEN can support 127 words */
2365 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2366 if (len & ~(127 * 2))
2367 goto out_err;
2368
2369 /* IPLEN can support at most 127 dwords */
2370 len = skb_inner_transport_header(skb) -
2371 skb_inner_network_header(skb);
2372 if (len & ~(127 * 4))
2373 goto out_err;
2374 }
2375
2376 /* No need to validate L4LEN as TCP is the only protocol with a
2377 * a flexible value and we support all possible values supported
2378 * by TCP, which is at most 15 dwords
2379 */
2380
2381 return features;
2382out_err:
2383 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2384}
2385
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002386#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2387 NETIF_F_HW_VLAN_CTAG_RX |\
2388 NETIF_F_HW_VLAN_CTAG_FILTER)
2389
2390/**
2391 * i40evf_fix_features - fix up the netdev feature bits
2392 * @netdev: our net device
2393 * @features: desired feature bits
2394 *
2395 * Returns fixed-up features bits
2396 **/
2397static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2398 netdev_features_t features)
2399{
2400 struct i40evf_adapter *adapter = netdev_priv(netdev);
2401
2402 features &= ~I40EVF_VLAN_FEATURES;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002403 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002404 features |= I40EVF_VLAN_FEATURES;
2405 return features;
2406}
2407
Greg Rose5eae00c2013-12-21 06:12:45 +00002408static const struct net_device_ops i40evf_netdev_ops = {
2409 .ndo_open = i40evf_open,
2410 .ndo_stop = i40evf_close,
2411 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002412 .ndo_set_rx_mode = i40evf_set_rx_mode,
2413 .ndo_validate_addr = eth_validate_addr,
2414 .ndo_set_mac_address = i40evf_set_mac,
2415 .ndo_change_mtu = i40evf_change_mtu,
2416 .ndo_tx_timeout = i40evf_tx_timeout,
2417 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2418 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002419 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002420 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002421 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002422#ifdef CONFIG_NET_POLL_CONTROLLER
2423 .ndo_poll_controller = i40evf_netpoll,
2424#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002425};
2426
2427/**
2428 * i40evf_check_reset_complete - check that VF reset is complete
2429 * @hw: pointer to hw struct
2430 *
2431 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2432 **/
2433static int i40evf_check_reset_complete(struct i40e_hw *hw)
2434{
2435 u32 rstat;
2436 int i;
2437
2438 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002439 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2440 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002441 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2442 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002443 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002444 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002445 }
2446 return -EBUSY;
2447}
2448
2449/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002450 * i40evf_process_config - Process the config information we got from the PF
2451 * @adapter: board private structure
2452 *
2453 * Verify that we have a valid config struct, and set up our netdev features
2454 * and our VSI struct.
2455 **/
2456int i40evf_process_config(struct i40evf_adapter *adapter)
2457{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002458 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002459 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002460 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002461 int i;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002462 netdev_features_t hw_enc_features;
2463 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002464
2465 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002466 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002467 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002468 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002469 }
2470 if (!adapter->vsi_res) {
2471 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2472 return -ENODEV;
2473 }
2474
Preethi Banalabacd75c2017-03-27 14:43:18 -07002475 hw_enc_features = NETIF_F_SG |
2476 NETIF_F_IP_CSUM |
2477 NETIF_F_IPV6_CSUM |
2478 NETIF_F_HIGHDMA |
2479 NETIF_F_SOFT_FEATURES |
2480 NETIF_F_TSO |
2481 NETIF_F_TSO_ECN |
2482 NETIF_F_TSO6 |
2483 NETIF_F_SCTP_CRC |
2484 NETIF_F_RXHASH |
2485 NETIF_F_RXCSUM |
2486 0;
2487
2488 /* advertise to stack only if offloads for encapsulated packets is
2489 * supported
2490 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002491 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002492 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002493 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002494 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002495 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002496 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002497 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002498 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002499 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002500
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002501 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002502 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002503 netdev->gso_partial_features |=
2504 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002505
Preethi Banalabacd75c2017-03-27 14:43:18 -07002506 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2507 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2508 netdev->hw_enc_features |= hw_enc_features;
2509 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002510 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002511 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002512
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002513 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002514 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002515 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002516 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002517
Preethi Banalabacd75c2017-03-27 14:43:18 -07002518 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002519
Preethi Banalabacd75c2017-03-27 14:43:18 -07002520 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002521
2522 adapter->vsi.id = adapter->vsi_res->vsi_id;
2523
2524 adapter->vsi.back = adapter;
2525 adapter->vsi.base_vector = 1;
2526 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002527 vsi->netdev = adapter->netdev;
2528 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002529 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002530 adapter->rss_key_size = vfres->rss_key_size;
2531 adapter->rss_lut_size = vfres->rss_lut_size;
2532 } else {
2533 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2534 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2535 }
2536
Mitch Williamse6d038d2015-06-04 16:23:58 -04002537 return 0;
2538}
2539
2540/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002541 * i40evf_init_task - worker thread to perform delayed initialization
2542 * @work: pointer to work_struct containing our data
2543 *
2544 * This task completes the work that was begun in probe. Due to the nature
2545 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002546 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002547 * whole system, we'll do it in a task so we can sleep.
2548 * This task only runs during driver init. Once we've established
2549 * communications with the PF driver and set up our netdev, the watchdog
2550 * takes over.
2551 **/
2552static void i40evf_init_task(struct work_struct *work)
2553{
2554 struct i40evf_adapter *adapter = container_of(work,
2555 struct i40evf_adapter,
2556 init_task.work);
2557 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002558 struct i40e_hw *hw = &adapter->hw;
2559 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002560 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002561
2562 switch (adapter->state) {
2563 case __I40EVF_STARTUP:
2564 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002565 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2566 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002567 err = i40e_set_mac_type(hw);
2568 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002569 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2570 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002571 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002572 }
2573 err = i40evf_check_reset_complete(hw);
2574 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002575 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002576 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002577 goto err;
2578 }
2579 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2580 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2581 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2582 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2583
2584 err = i40evf_init_adminq(hw);
2585 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002586 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2587 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002588 goto err;
2589 }
2590 err = i40evf_send_api_ver(adapter);
2591 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002592 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002593 i40evf_shutdown_adminq(hw);
2594 goto err;
2595 }
2596 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2597 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002598 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002599 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002600 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002601 i40evf_shutdown_adminq(hw);
2602 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002603 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002604 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002605
2606 /* aq msg sent, awaiting reply */
2607 err = i40evf_verify_api_ver(adapter);
2608 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002609 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002610 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002611 else
2612 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2613 adapter->pf_version.major,
2614 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002615 VIRTCHNL_VERSION_MAJOR,
2616 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002617 goto err;
2618 }
2619 err = i40evf_send_vf_config_msg(adapter);
2620 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002621 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002622 err);
2623 goto err;
2624 }
2625 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2626 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002627 case __I40EVF_INIT_GET_RESOURCES:
2628 /* aq msg sent, awaiting reply */
2629 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002630 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002631 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002632 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002633 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002634 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002635 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002636 }
2637 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002638 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002639 err = i40evf_send_vf_config_msg(adapter);
2640 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002641 } else if (err == I40E_ERR_PARAM) {
2642 /* We only get ERR_PARAM if the device is in a very bad
2643 * state or if we've been disabled for previous bad
2644 * behavior. Either way, we're done now.
2645 */
2646 i40evf_shutdown_adminq(hw);
2647 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2648 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002649 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002650 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002651 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2652 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002653 goto err_alloc;
2654 }
2655 adapter->state = __I40EVF_INIT_SW;
2656 break;
2657 default:
2658 goto err_alloc;
2659 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002660
Mitch Williamse6d038d2015-06-04 16:23:58 -04002661 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002662 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002663 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002664
2665 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2666
Greg Rose5eae00c2013-12-21 06:12:45 +00002667 netdev->netdev_ops = &i40evf_netdev_ops;
2668 i40evf_set_ethtool_ops(netdev);
2669 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002670
Jarod Wilson91c527a2016-10-17 15:54:05 -04002671 /* MTU range: 68 - 9710 */
2672 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002673 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002674
Greg Rose5eae00c2013-12-21 06:12:45 +00002675 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002676 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002677 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002678 eth_hw_addr_random(netdev);
2679 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2680 } else {
2681 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2682 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2683 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002684 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002685
Greg Rose5eae00c2013-12-21 06:12:45 +00002686 init_timer(&adapter->watchdog_timer);
2687 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2688 adapter->watchdog_timer.data = (unsigned long)adapter;
2689 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2690
Mitch Williamsd732a182014-04-24 06:41:37 +00002691 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2692 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002693 err = i40evf_init_interrupt_scheme(adapter);
2694 if (err)
2695 goto err_sw_init;
2696 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002697 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002698 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002699 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2700
Greg Rose5eae00c2013-12-21 06:12:45 +00002701 err = i40evf_request_misc_irq(adapter);
2702 if (err)
2703 goto err_sw_init;
2704
2705 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002706 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002707
Mitch Williamsef8693e2014-02-13 03:48:53 -08002708 if (!adapter->netdev_registered) {
2709 err = register_netdev(netdev);
2710 if (err)
2711 goto err_register;
2712 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002713
2714 adapter->netdev_registered = true;
2715
2716 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002717 if (CLIENT_ALLOWED(adapter)) {
2718 err = i40evf_lan_add_device(adapter);
2719 if (err)
2720 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2721 err);
2722 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002723
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002724 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002725 if (netdev->features & NETIF_F_GRO)
2726 dev_info(&pdev->dev, "GRO is enabled\n");
2727
Greg Rose5eae00c2013-12-21 06:12:45 +00002728 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002729 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002730 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002731 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002732
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002733 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2734 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2735 if (!adapter->rss_key || !adapter->rss_lut)
2736 goto err_mem;
2737
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002738 if (RSS_AQ(adapter)) {
2739 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2740 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2741 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002742 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002743 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002744 return;
2745restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002746 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002747 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002748err_mem:
2749 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002750err_register:
2751 i40evf_free_misc_irq(adapter);
2752err_sw_init:
2753 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002754err_alloc:
2755 kfree(adapter->vf_res);
2756 adapter->vf_res = NULL;
2757err:
2758 /* Things went into the weeds, so try again later */
2759 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002760 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002761 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002762 i40evf_shutdown_adminq(hw);
2763 adapter->state = __I40EVF_STARTUP;
2764 schedule_delayed_work(&adapter->init_task, HZ * 5);
2765 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002766 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002767 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002768}
2769
2770/**
2771 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2772 * @pdev: pci device structure
2773 **/
2774static void i40evf_shutdown(struct pci_dev *pdev)
2775{
2776 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002777 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002778
2779 netif_device_detach(netdev);
2780
2781 if (netif_running(netdev))
2782 i40evf_close(netdev);
2783
Mitch Williams00293fd2015-01-09 11:18:18 +00002784 /* Prevent the watchdog from running. */
2785 adapter->state = __I40EVF_REMOVE;
2786 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002787
Greg Rose5eae00c2013-12-21 06:12:45 +00002788#ifdef CONFIG_PM
2789 pci_save_state(pdev);
2790
2791#endif
2792 pci_disable_device(pdev);
2793}
2794
2795/**
2796 * i40evf_probe - Device Initialization Routine
2797 * @pdev: PCI device information struct
2798 * @ent: entry in i40evf_pci_tbl
2799 *
2800 * Returns 0 on success, negative on failure
2801 *
2802 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2803 * The OS initialization, configuring of the adapter private structure,
2804 * and a hardware reset occur.
2805 **/
2806static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2807{
2808 struct net_device *netdev;
2809 struct i40evf_adapter *adapter = NULL;
2810 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002811 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002812
2813 err = pci_enable_device(pdev);
2814 if (err)
2815 return err;
2816
Mitch Williams64942942014-02-11 08:26:33 +00002817 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002818 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002819 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2820 if (err) {
2821 dev_err(&pdev->dev,
2822 "DMA configuration failed: 0x%x\n", err);
2823 goto err_dma;
2824 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002825 }
2826
2827 err = pci_request_regions(pdev, i40evf_driver_name);
2828 if (err) {
2829 dev_err(&pdev->dev,
2830 "pci_request_regions failed 0x%x\n", err);
2831 goto err_pci_reg;
2832 }
2833
2834 pci_enable_pcie_error_reporting(pdev);
2835
2836 pci_set_master(pdev);
2837
Mitch Williams1255b7a2015-11-06 15:25:59 -08002838 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002839 if (!netdev) {
2840 err = -ENOMEM;
2841 goto err_alloc_etherdev;
2842 }
2843
2844 SET_NETDEV_DEV(netdev, &pdev->dev);
2845
2846 pci_set_drvdata(pdev, netdev);
2847 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002848
2849 adapter->netdev = netdev;
2850 adapter->pdev = pdev;
2851
2852 hw = &adapter->hw;
2853 hw->back = adapter;
2854
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002855 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002856 adapter->state = __I40EVF_STARTUP;
2857
2858 /* Call save state here because it relies on the adapter struct. */
2859 pci_save_state(pdev);
2860
2861 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2862 pci_resource_len(pdev, 0));
2863 if (!hw->hw_addr) {
2864 err = -EIO;
2865 goto err_ioremap;
2866 }
2867 hw->vendor_id = pdev->vendor;
2868 hw->device_id = pdev->device;
2869 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2870 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2871 hw->subsystem_device_id = pdev->subsystem_device;
2872 hw->bus.device = PCI_SLOT(pdev->devfn);
2873 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002874 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002875
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002876 /* set up the locks for the AQ, do this only once in probe
2877 * and destroy them only once in remove
2878 */
2879 mutex_init(&hw->aq.asq_mutex);
2880 mutex_init(&hw->aq.arq_mutex);
2881
Serey Kong8bb1a542014-08-01 13:27:15 -07002882 INIT_LIST_HEAD(&adapter->mac_filter_list);
2883 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2884
Greg Rose5eae00c2013-12-21 06:12:45 +00002885 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2886 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2887 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002888 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002889 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002890 schedule_delayed_work(&adapter->init_task,
2891 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002892
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002893 /* Setup the wait queue for indicating transition to down status */
2894 init_waitqueue_head(&adapter->down_waitqueue);
2895
Greg Rose5eae00c2013-12-21 06:12:45 +00002896 return 0;
2897
2898err_ioremap:
2899 free_netdev(netdev);
2900err_alloc_etherdev:
2901 pci_release_regions(pdev);
2902err_pci_reg:
2903err_dma:
2904 pci_disable_device(pdev);
2905 return err;
2906}
2907
2908#ifdef CONFIG_PM
2909/**
2910 * i40evf_suspend - Power management suspend routine
2911 * @pdev: PCI device information struct
2912 * @state: unused
2913 *
2914 * Called when the system (VM) is entering sleep/suspend.
2915 **/
2916static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2917{
2918 struct net_device *netdev = pci_get_drvdata(pdev);
2919 struct i40evf_adapter *adapter = netdev_priv(netdev);
2920 int retval = 0;
2921
2922 netif_device_detach(netdev);
2923
2924 if (netif_running(netdev)) {
2925 rtnl_lock();
2926 i40evf_down(adapter);
2927 rtnl_unlock();
2928 }
2929 i40evf_free_misc_irq(adapter);
2930 i40evf_reset_interrupt_capability(adapter);
2931
2932 retval = pci_save_state(pdev);
2933 if (retval)
2934 return retval;
2935
2936 pci_disable_device(pdev);
2937
2938 return 0;
2939}
2940
2941/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002942 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002943 * @pdev: PCI device information struct
2944 *
2945 * Called when the system (VM) is resumed from sleep/suspend.
2946 **/
2947static int i40evf_resume(struct pci_dev *pdev)
2948{
2949 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2950 struct net_device *netdev = adapter->netdev;
2951 u32 err;
2952
2953 pci_set_power_state(pdev, PCI_D0);
2954 pci_restore_state(pdev);
2955 /* pci_restore_state clears dev->state_saved so call
2956 * pci_save_state to restore it.
2957 */
2958 pci_save_state(pdev);
2959
2960 err = pci_enable_device_mem(pdev);
2961 if (err) {
2962 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2963 return err;
2964 }
2965 pci_set_master(pdev);
2966
2967 rtnl_lock();
2968 err = i40evf_set_interrupt_capability(adapter);
2969 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002970 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002971 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2972 return err;
2973 }
2974 err = i40evf_request_misc_irq(adapter);
2975 rtnl_unlock();
2976 if (err) {
2977 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2978 return err;
2979 }
2980
2981 schedule_work(&adapter->reset_task);
2982
2983 netif_device_attach(netdev);
2984
2985 return err;
2986}
2987
2988#endif /* CONFIG_PM */
2989/**
2990 * i40evf_remove - Device Removal Routine
2991 * @pdev: PCI device information struct
2992 *
2993 * i40evf_remove is called by the PCI subsystem to alert the driver
2994 * that it should release a PCI device. The could be caused by a
2995 * Hot-Plug event, or because the driver is going to be removed from
2996 * memory.
2997 **/
2998static void i40evf_remove(struct pci_dev *pdev)
2999{
3000 struct net_device *netdev = pci_get_drvdata(pdev);
3001 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003002 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003003 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003004 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003005
3006 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003007 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003008 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003009 if (adapter->netdev_registered) {
3010 unregister_netdev(netdev);
3011 adapter->netdev_registered = false;
3012 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003013 if (CLIENT_ALLOWED(adapter)) {
3014 err = i40evf_lan_del_device(adapter);
3015 if (err)
3016 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3017 err);
3018 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003019
Mitch Williamsf4a71882015-01-09 11:18:16 +00003020 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003021 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003022 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003023 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003024 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003025 /* If the FW isn't responding, kick it once, but only once. */
3026 if (!i40evf_asq_done(hw)) {
3027 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003028 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003029 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003030 i40evf_free_all_tx_resources(adapter);
3031 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003032 i40evf_misc_irq_disable(adapter);
3033 i40evf_free_misc_irq(adapter);
3034 i40evf_reset_interrupt_capability(adapter);
3035 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003036
Mitch Williamse5d17c32014-06-04 04:22:38 +00003037 if (adapter->watchdog_timer.function)
3038 del_timer_sync(&adapter->watchdog_timer);
3039
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003040 flush_scheduled_work();
3041
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003042 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003043
Greg Rose5eae00c2013-12-21 06:12:45 +00003044 if (hw->aq.asq.count)
3045 i40evf_shutdown_adminq(hw);
3046
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003047 /* destroy the locks only once, here */
3048 mutex_destroy(&hw->aq.arq_mutex);
3049 mutex_destroy(&hw->aq.asq_mutex);
3050
Greg Rose5eae00c2013-12-21 06:12:45 +00003051 iounmap(hw->hw_addr);
3052 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003053 i40evf_free_all_tx_resources(adapter);
3054 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003055 i40evf_free_queues(adapter);
3056 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003057 /* If we got removed before an up/down sequence, we've got a filter
3058 * hanging out there that we need to get rid of.
3059 */
3060 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3061 list_del(&f->list);
3062 kfree(f);
3063 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003064 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3065 list_del(&f->list);
3066 kfree(f);
3067 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003068
3069 free_netdev(netdev);
3070
3071 pci_disable_pcie_error_reporting(pdev);
3072
3073 pci_disable_device(pdev);
3074}
3075
3076static struct pci_driver i40evf_driver = {
3077 .name = i40evf_driver_name,
3078 .id_table = i40evf_pci_tbl,
3079 .probe = i40evf_probe,
3080 .remove = i40evf_remove,
3081#ifdef CONFIG_PM
3082 .suspend = i40evf_suspend,
3083 .resume = i40evf_resume,
3084#endif
3085 .shutdown = i40evf_shutdown,
3086};
3087
3088/**
3089 * i40e_init_module - Driver Registration Routine
3090 *
3091 * i40e_init_module is the first routine called when the driver is
3092 * loaded. All it does is register with the PCI subsystem.
3093 **/
3094static int __init i40evf_init_module(void)
3095{
3096 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003097
Greg Rose5eae00c2013-12-21 06:12:45 +00003098 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003099 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003100
3101 pr_info("%s\n", i40evf_copyright);
3102
Jacob Keller6992a6c2016-08-04 11:37:01 -07003103 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3104 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003105 if (!i40evf_wq) {
3106 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3107 return -ENOMEM;
3108 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003109 ret = pci_register_driver(&i40evf_driver);
3110 return ret;
3111}
3112
3113module_init(i40evf_init_module);
3114
3115/**
3116 * i40e_exit_module - Driver Exit Cleanup Routine
3117 *
3118 * i40e_exit_module is called just before the driver is removed
3119 * from memory.
3120 **/
3121static void __exit i40evf_exit_module(void)
3122{
3123 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003124 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003125}
3126
3127module_exit(i40evf_exit_module);
3128
3129/* i40evf_main.c */