blob: c243f9da95ae0fbe66d4922e29da4598f147673e [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);
Jacob Keller759dc4a2017-07-14 09:10:10 -0400587 /* get_cpu_mask returns a static constant mask with
588 * a permanent lifetime so it's ok to use here.
589 */
590 irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
Greg Rose5eae00c2013-12-21 06:12:45 +0000591 }
592
593 return 0;
594
595free_queue_irqs:
596 while (vector) {
597 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700598 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
599 irq_set_affinity_notifier(irq_num, NULL);
600 irq_set_affinity_hint(irq_num, NULL);
601 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000602 }
603 return err;
604}
605
606/**
607 * i40evf_request_misc_irq - Initialize MSI-X interrupts
608 * @adapter: board private structure
609 *
610 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
611 * vector is only for the admin queue, and stays active even when the netdev
612 * is closed.
613 **/
614static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
615{
616 struct net_device *netdev = adapter->netdev;
617 int err;
618
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700619 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000620 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
621 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000622 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800623 &i40evf_msix_aq, 0,
624 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000625 if (err) {
626 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800627 "request_irq for %s failed: %d\n",
628 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000629 free_irq(adapter->msix_entries[0].vector, netdev);
630 }
631 return err;
632}
633
634/**
635 * i40evf_free_traffic_irqs - Free MSI-X interrupts
636 * @adapter: board private structure
637 *
638 * Frees all MSI-X vectors other than 0.
639 **/
640static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
641{
Alan Brady96db7762016-09-14 16:24:38 -0700642 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000643
Alan Brady47d2a5d2016-11-08 13:05:05 -0800644 if (!adapter->msix_entries)
645 return;
646
Greg Rose5eae00c2013-12-21 06:12:45 +0000647 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
648
Alan Brady96db7762016-09-14 16:24:38 -0700649 for (vector = 0; vector < q_vectors; vector++) {
650 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
651 irq_set_affinity_notifier(irq_num, NULL);
652 irq_set_affinity_hint(irq_num, NULL);
653 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000654 }
655}
656
657/**
658 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
659 * @adapter: board private structure
660 *
661 * Frees MSI-X vector 0.
662 **/
663static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
664{
665 struct net_device *netdev = adapter->netdev;
666
Jacob Kelleref4603e2016-11-08 13:05:08 -0800667 if (!adapter->msix_entries)
668 return;
669
Greg Rose5eae00c2013-12-21 06:12:45 +0000670 free_irq(adapter->msix_entries[0].vector, netdev);
671}
672
673/**
674 * i40evf_configure_tx - Configure Transmit Unit after Reset
675 * @adapter: board private structure
676 *
677 * Configure the Tx unit of the MAC after a reset.
678 **/
679static void i40evf_configure_tx(struct i40evf_adapter *adapter)
680{
681 struct i40e_hw *hw = &adapter->hw;
682 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000683
Mitch Williamscc052922014-10-25 03:24:34 +0000684 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400685 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000686}
687
688/**
689 * i40evf_configure_rx - Configure Receive Unit after Reset
690 * @adapter: board private structure
691 *
692 * Configure the Rx unit of the MAC after a reset.
693 **/
694static void i40evf_configure_rx(struct i40evf_adapter *adapter)
695{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700696 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000697 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000698 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000699
Alexander Duyckdab86af2017-03-14 10:15:27 -0700700 /* Legacy Rx will always default to a 2048 buffer size. */
701#if (PAGE_SIZE < 8192)
702 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200703 struct net_device *netdev = adapter->netdev;
704
Alexander Duyck98efd692017-04-05 07:51:01 -0400705 /* For jumbo frames on systems with 4K pages we have to use
706 * an order 1 page, so we might as well increase the size
707 * of our Rx buffer to make better use of the available space
708 */
709 rx_buf_len = I40E_RXBUFFER_3072;
710
Alexander Duyckdab86af2017-03-14 10:15:27 -0700711 /* We use a 1536 buffer size for configurations with
712 * standard Ethernet mtu. On x86 this gives us enough room
713 * for shared info and 192 bytes of padding.
714 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400715 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
716 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700717 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
718 }
719#endif
720
Mitch Williamscc052922014-10-25 03:24:34 +0000721 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400722 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700723 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400724
725 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
726 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
727 else
728 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000729 }
730}
731
732/**
733 * i40evf_find_vlan - Search filter list for specific vlan filter
734 * @adapter: board private structure
735 * @vlan: vlan tag
736 *
737 * Returns ptr to the filter object or NULL
738 **/
739static struct
740i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
741{
742 struct i40evf_vlan_filter *f;
743
744 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
745 if (vlan == f->vlan)
746 return f;
747 }
748 return NULL;
749}
750
751/**
752 * i40evf_add_vlan - Add a vlan filter to the list
753 * @adapter: board private structure
754 * @vlan: VLAN tag
755 *
756 * Returns ptr to the filter object or NULL when no memory available.
757 **/
758static struct
759i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
760{
Mitch Williams13acb542015-03-31 00:45:05 -0700761 struct i40evf_vlan_filter *f = NULL;
762 int count = 50;
763
764 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
765 &adapter->crit_section)) {
766 udelay(1);
767 if (--count == 0)
768 goto out;
769 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000770
771 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000772 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000773 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000774 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700775 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000776
Greg Rose5eae00c2013-12-21 06:12:45 +0000777 f->vlan = vlan;
778
779 INIT_LIST_HEAD(&f->list);
780 list_add(&f->list, &adapter->vlan_filter_list);
781 f->add = true;
782 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
783 }
784
Mitch Williams13acb542015-03-31 00:45:05 -0700785clearout:
786 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
787out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000788 return f;
789}
790
791/**
792 * i40evf_del_vlan - Remove a vlan filter from the list
793 * @adapter: board private structure
794 * @vlan: VLAN tag
795 **/
796static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
797{
798 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700799 int count = 50;
800
801 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
802 &adapter->crit_section)) {
803 udelay(1);
804 if (--count == 0)
805 return;
806 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000807
808 f = i40evf_find_vlan(adapter, vlan);
809 if (f) {
810 f->remove = true;
811 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
812 }
Mitch Williams13acb542015-03-31 00:45:05 -0700813 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000814}
815
816/**
817 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
818 * @netdev: network device struct
819 * @vid: VLAN tag
820 **/
821static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000822 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000823{
824 struct i40evf_adapter *adapter = netdev_priv(netdev);
825
Mitch Williams8ed995f2015-08-28 17:55:57 -0400826 if (!VLAN_ALLOWED(adapter))
827 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000828 if (i40evf_add_vlan(adapter, vid) == NULL)
829 return -ENOMEM;
830 return 0;
831}
832
833/**
834 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
835 * @netdev: network device struct
836 * @vid: VLAN tag
837 **/
838static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000839 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000840{
841 struct i40evf_adapter *adapter = netdev_priv(netdev);
842
Mitch Williams8ed995f2015-08-28 17:55:57 -0400843 if (VLAN_ALLOWED(adapter)) {
844 i40evf_del_vlan(adapter, vid);
845 return 0;
846 }
847 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000848}
849
850/**
851 * i40evf_find_filter - Search filter list for specific mac filter
852 * @adapter: board private structure
853 * @macaddr: the MAC address
854 *
855 * Returns ptr to the filter object or NULL
856 **/
857static struct
858i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
859 u8 *macaddr)
860{
861 struct i40evf_mac_filter *f;
862
863 if (!macaddr)
864 return NULL;
865
866 list_for_each_entry(f, &adapter->mac_filter_list, list) {
867 if (ether_addr_equal(macaddr, f->macaddr))
868 return f;
869 }
870 return NULL;
871}
872
873/**
874 * i40e_add_filter - Add a mac filter to the filter list
875 * @adapter: board private structure
876 * @macaddr: the MAC address
877 *
878 * Returns ptr to the filter object or NULL when no memory available.
879 **/
880static struct
881i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
882 u8 *macaddr)
883{
884 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000885 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000886
887 if (!macaddr)
888 return NULL;
889
890 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000891 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000892 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000893 if (--count == 0)
894 return NULL;
895 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000896
897 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000898 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000899 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000900 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000901 clear_bit(__I40EVF_IN_CRITICAL_TASK,
902 &adapter->crit_section);
903 return NULL;
904 }
905
Greg Rose9a173902014-05-22 06:32:02 +0000906 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000907
Mitch Williams63590b62016-05-16 10:26:42 -0700908 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000909 f->add = true;
910 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
911 }
912
913 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
914 return f;
915}
916
917/**
918 * i40evf_set_mac - NDO callback to set port mac address
919 * @netdev: network interface device structure
920 * @p: pointer to an address structure
921 *
922 * Returns 0 on success, negative on failure
923 **/
924static int i40evf_set_mac(struct net_device *netdev, void *p)
925{
926 struct i40evf_adapter *adapter = netdev_priv(netdev);
927 struct i40e_hw *hw = &adapter->hw;
928 struct i40evf_mac_filter *f;
929 struct sockaddr *addr = p;
930
931 if (!is_valid_ether_addr(addr->sa_data))
932 return -EADDRNOTAVAIL;
933
934 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
935 return 0;
936
Mitch Williams14e52ee2015-08-31 19:54:44 -0400937 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
938 return -EPERM;
939
940 f = i40evf_find_filter(adapter, hw->mac.addr);
941 if (f) {
942 f->remove = true;
943 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
944 }
945
Greg Rose5eae00c2013-12-21 06:12:45 +0000946 f = i40evf_add_filter(adapter, addr->sa_data);
947 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000948 ether_addr_copy(hw->mac.addr, addr->sa_data);
949 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000950 }
951
952 return (f == NULL) ? -ENOMEM : 0;
953}
954
955/**
956 * i40evf_set_rx_mode - NDO callback to set the netdev filters
957 * @netdev: network interface device structure
958 **/
959static void i40evf_set_rx_mode(struct net_device *netdev)
960{
961 struct i40evf_adapter *adapter = netdev_priv(netdev);
962 struct i40evf_mac_filter *f, *ftmp;
963 struct netdev_hw_addr *uca;
964 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400965 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000966 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000967
968 /* add addr if not already in the filter list */
969 netdev_for_each_uc_addr(uca, netdev) {
970 i40evf_add_filter(adapter, uca->addr);
971 }
972 netdev_for_each_mc_addr(mca, netdev) {
973 i40evf_add_filter(adapter, mca->addr);
974 }
975
976 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000977 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000978 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000979 if (--count == 0) {
980 dev_err(&adapter->pdev->dev,
981 "Failed to get lock in %s\n", __func__);
982 return;
983 }
984 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000985 /* remove filter if not in netdev list */
986 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400987 netdev_for_each_mc_addr(mca, netdev)
988 if (ether_addr_equal(mca->addr, f->macaddr))
989 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000990
Shannon Nelson2f41f332015-08-26 15:14:20 -0400991 netdev_for_each_uc_addr(uca, netdev)
992 if (ether_addr_equal(uca->addr, f->macaddr))
993 goto bottom_of_search_loop;
994
995 for_each_dev_addr(netdev, ha)
996 if (ether_addr_equal(ha->addr, f->macaddr))
997 goto bottom_of_search_loop;
998
999 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
1000 goto bottom_of_search_loop;
1001
1002 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1003 f->remove = true;
1004 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1005
1006bottom_of_search_loop:
1007 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +00001008 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001009
1010 if (netdev->flags & IFF_PROMISC &&
1011 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
1012 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
1013 else if (!(netdev->flags & IFF_PROMISC) &&
1014 adapter->flags & I40EVF_FLAG_PROMISC_ON)
1015 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
1016
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001017 if (netdev->flags & IFF_ALLMULTI &&
1018 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
1019 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
1020 else if (!(netdev->flags & IFF_ALLMULTI) &&
1021 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
1022 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
1023
Greg Rose5eae00c2013-12-21 06:12:45 +00001024 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1025}
1026
1027/**
1028 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1029 * @adapter: board private structure
1030 **/
1031static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1032{
1033 int q_idx;
1034 struct i40e_q_vector *q_vector;
1035 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1036
1037 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1038 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001039
Mitch Williams7d96ba12015-10-26 19:44:39 -04001040 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001041 napi = &q_vector->napi;
1042 napi_enable(napi);
1043 }
1044}
1045
1046/**
1047 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1048 * @adapter: board private structure
1049 **/
1050static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1051{
1052 int q_idx;
1053 struct i40e_q_vector *q_vector;
1054 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1055
1056 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001057 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001058 napi_disable(&q_vector->napi);
1059 }
1060}
1061
1062/**
1063 * i40evf_configure - set up transmit and receive data structures
1064 * @adapter: board private structure
1065 **/
1066static void i40evf_configure(struct i40evf_adapter *adapter)
1067{
1068 struct net_device *netdev = adapter->netdev;
1069 int i;
1070
1071 i40evf_set_rx_mode(netdev);
1072
1073 i40evf_configure_tx(adapter);
1074 i40evf_configure_rx(adapter);
1075 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1076
Mitch Williamscc052922014-10-25 03:24:34 +00001077 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001078 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001079
Mitch Williamsb1630982016-04-18 11:33:48 -07001080 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001081 }
1082}
1083
1084/**
1085 * i40evf_up_complete - Finish the last steps of bringing up a connection
1086 * @adapter: board private structure
1087 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001088static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001089{
1090 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001091 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001092
1093 i40evf_napi_enable_all(adapter);
1094
1095 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001096 if (CLIENT_ENABLED(adapter))
1097 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001098 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001099}
1100
1101/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001102 * i40e_down - Shutdown the connection processing
1103 * @adapter: board private structure
1104 **/
1105void i40evf_down(struct i40evf_adapter *adapter)
1106{
1107 struct net_device *netdev = adapter->netdev;
1108 struct i40evf_mac_filter *f;
1109
Mitch Williams209dc4d2015-12-09 15:50:27 -08001110 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001111 return;
1112
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001113 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1114 &adapter->crit_section))
1115 usleep_range(500, 1000);
1116
Mitch Williams63e18c22015-03-27 00:12:10 -07001117 netif_carrier_off(netdev);
1118 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001119 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001120 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001121 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001122
Mitch Williamsef8693e2014-02-13 03:48:53 -08001123 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001124 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1125 f->remove = true;
1126 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001127 /* remove all VLAN filters */
1128 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1129 f->remove = true;
1130 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001131 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1132 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001133 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001134 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001135 /* Schedule operations to close down the HW. Don't wait
1136 * here for this to complete. The watchdog is still running
1137 * and it will take care of this.
1138 */
1139 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001140 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001141 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001142 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001143
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001144 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001145 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001146}
1147
1148/**
1149 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1150 * @adapter: board private structure
1151 * @vectors: number of vectors to request
1152 *
1153 * Work with the OS to set up the MSIX vectors needed.
1154 *
1155 * Returns 0 on success, negative on failure
1156 **/
1157static int
1158i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1159{
1160 int err, vector_threshold;
1161
1162 /* We'll want at least 3 (vector_threshold):
1163 * 0) Other (Admin Queue and link, mostly)
1164 * 1) TxQ[0] Cleanup
1165 * 2) RxQ[0] Cleanup
1166 */
1167 vector_threshold = MIN_MSIX_COUNT;
1168
1169 /* The more we get, the more we will assign to Tx/Rx Cleanup
1170 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1171 * Right now, we simply care about how many we'll get; we'll
1172 * set them up later while requesting irq's.
1173 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001174 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1175 vector_threshold, vectors);
1176 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001177 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001178 kfree(adapter->msix_entries);
1179 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001180 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001181 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001182
1183 /* Adjust for only the vectors we'll use, which is minimum
1184 * of max_msix_q_vectors + NONQ_VECS, or the number of
1185 * vectors we were allocated.
1186 */
1187 adapter->num_msix_vectors = err;
1188 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001189}
1190
1191/**
1192 * i40evf_free_queues - Free memory for all rings
1193 * @adapter: board private structure to initialize
1194 *
1195 * Free all of the memory associated with queue pairs.
1196 **/
1197static void i40evf_free_queues(struct i40evf_adapter *adapter)
1198{
Greg Rose5eae00c2013-12-21 06:12:45 +00001199 if (!adapter->vsi_res)
1200 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001201 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001202 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001203 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001204 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001205 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001206}
1207
1208/**
1209 * i40evf_alloc_queues - Allocate memory for all rings
1210 * @adapter: board private structure to initialize
1211 *
1212 * We allocate one ring per queue at run-time since we don't know the
1213 * number of queues at compile-time. The polling_netdev array is
1214 * intended for Multiqueue, but should work fine with a single queue.
1215 **/
1216static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1217{
Jacob Keller65c70062017-06-07 05:43:01 -04001218 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001219
Jacob Keller65c70062017-06-07 05:43:01 -04001220 num_active_queues = min_t(int,
1221 adapter->vsi_res->num_queue_pairs,
1222 (int)(num_online_cpus()));
1223
1224 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001225 sizeof(struct i40e_ring), GFP_KERNEL);
1226 if (!adapter->tx_rings)
1227 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001228 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001229 sizeof(struct i40e_ring), GFP_KERNEL);
1230 if (!adapter->rx_rings)
1231 goto err_out;
1232
Jacob Keller65c70062017-06-07 05:43:01 -04001233 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001234 struct i40e_ring *tx_ring;
1235 struct i40e_ring *rx_ring;
1236
Mitch Williams0dd438d2015-10-26 19:44:40 -04001237 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001238
1239 tx_ring->queue_index = i;
1240 tx_ring->netdev = adapter->netdev;
1241 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001242 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001243 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001244 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001245 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001246
Mitch Williams0dd438d2015-10-26 19:44:40 -04001247 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001248 rx_ring->queue_index = i;
1249 rx_ring->netdev = adapter->netdev;
1250 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001251 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001252 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001253 }
1254
Jacob Keller65c70062017-06-07 05:43:01 -04001255 adapter->num_active_queues = num_active_queues;
1256
Greg Rose5eae00c2013-12-21 06:12:45 +00001257 return 0;
1258
1259err_out:
1260 i40evf_free_queues(adapter);
1261 return -ENOMEM;
1262}
1263
1264/**
1265 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1266 * @adapter: board private structure to initialize
1267 *
1268 * Attempt to configure the interrupts using the best available
1269 * capabilities of the hardware and the kernel.
1270 **/
1271static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1272{
1273 int vector, v_budget;
1274 int pairs = 0;
1275 int err = 0;
1276
1277 if (!adapter->vsi_res) {
1278 err = -EIO;
1279 goto out;
1280 }
Mitch Williamscc052922014-10-25 03:24:34 +00001281 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001282
Jacob Keller789f38c2017-04-19 09:25:56 -04001283 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1284 * us much good if we have more vectors than CPUs. However, we already
1285 * limit the total number of queues by the number of CPUs so we do not
1286 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001287 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001288 v_budget = min_t(int, pairs + NONQ_VECS,
1289 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001290
Greg Rose5eae00c2013-12-21 06:12:45 +00001291 adapter->msix_entries = kcalloc(v_budget,
1292 sizeof(struct msix_entry), GFP_KERNEL);
1293 if (!adapter->msix_entries) {
1294 err = -ENOMEM;
1295 goto out;
1296 }
1297
1298 for (vector = 0; vector < v_budget; vector++)
1299 adapter->msix_entries[vector].entry = vector;
1300
Mitch Williams313ed2d2015-08-27 11:42:31 -04001301 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001302
1303out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001304 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1305 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001306 return err;
1307}
1308
1309/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1311 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001312 *
1313 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001314 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001315static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001316{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001317 struct i40e_aqc_get_set_rss_key_data *rss_key =
1318 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001320 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001321
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001322 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001323 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001324 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001325 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001326 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001327 }
1328
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001329 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1330 if (ret) {
1331 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1332 i40evf_stat_str(hw, ret),
1333 i40evf_aq_str(hw, hw->aq.asq_last_status));
1334 return ret;
1335
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001336 }
1337
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001338 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1339 adapter->rss_lut, adapter->rss_lut_size);
1340 if (ret) {
1341 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1342 i40evf_stat_str(hw, ret),
1343 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001344 }
1345
1346 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001347
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001348}
1349
1350/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001351 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001352 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001353 *
1354 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001355 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001356static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001357{
1358 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001359 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001360 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001361
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001362 dw = (u32 *)adapter->rss_key;
1363 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1364 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001365
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001366 dw = (u32 *)adapter->rss_lut;
1367 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1368 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001369
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001370 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001371
1372 return 0;
1373}
1374
1375/**
1376 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001377 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001378 *
1379 * Returns 0 on success, negative on failure
1380 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001381int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001382{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001383
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001384 if (RSS_PF(adapter)) {
1385 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1386 I40EVF_FLAG_AQ_SET_RSS_KEY;
1387 return 0;
1388 } else if (RSS_AQ(adapter)) {
1389 return i40evf_config_rss_aq(adapter);
1390 } else {
1391 return i40evf_config_rss_reg(adapter);
1392 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001393}
1394
1395/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001396 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001397 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001398 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001399static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001400{
1401 u16 i;
1402
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001403 for (i = 0; i < adapter->rss_lut_size; i++)
1404 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001405}
1406
1407/**
Helin Zhang96a81982015-10-26 19:44:31 -04001408 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001409 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001410 *
1411 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001412 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001413static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001414{
1415 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001416 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001417
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001418 if (!RSS_PF(adapter)) {
1419 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001420 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001421 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001422 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1423 else
1424 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001425
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001426 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1427 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1428 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001429
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001430 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001431
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001432 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1433 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001434
1435 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001436}
1437
1438/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001439 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1440 * @adapter: board private structure to initialize
1441 *
1442 * We allocate one q_vector per queue interrupt. If allocation fails we
1443 * return -ENOMEM.
1444 **/
1445static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1446{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001447 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001448 struct i40e_q_vector *q_vector;
1449
1450 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001451 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001452 GFP_KERNEL);
1453 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001454 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001455
1456 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001457 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001458 q_vector->adapter = adapter;
1459 q_vector->vsi = &adapter->vsi;
1460 q_vector->v_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001461 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001462 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001463 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001464 }
1465
1466 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001467}
1468
1469/**
1470 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1471 * @adapter: board private structure to initialize
1472 *
1473 * This function frees the memory allocated to the q_vectors. In addition if
1474 * NAPI is enabled it will delete any references to the NAPI struct prior
1475 * to freeing the q_vector.
1476 **/
1477static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1478{
1479 int q_idx, num_q_vectors;
1480 int napi_vectors;
1481
Jacob Kelleref4603e2016-11-08 13:05:08 -08001482 if (!adapter->q_vectors)
1483 return;
1484
Greg Rose5eae00c2013-12-21 06:12:45 +00001485 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001486 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001487
1488 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001489 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001490 if (q_idx < napi_vectors)
1491 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001492 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001493 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001494 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001495}
1496
1497/**
1498 * i40evf_reset_interrupt_capability - Reset MSIX setup
1499 * @adapter: board private structure
1500 *
1501 **/
1502void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1503{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001504 if (!adapter->msix_entries)
1505 return;
1506
Greg Rose5eae00c2013-12-21 06:12:45 +00001507 pci_disable_msix(adapter->pdev);
1508 kfree(adapter->msix_entries);
1509 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001510}
1511
1512/**
1513 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1514 * @adapter: board private structure to initialize
1515 *
1516 **/
1517int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1518{
1519 int err;
1520
Jacob Keller283aeaf2017-04-19 09:25:59 -04001521 err = i40evf_alloc_queues(adapter);
1522 if (err) {
1523 dev_err(&adapter->pdev->dev,
1524 "Unable to allocate memory for queues\n");
1525 goto err_alloc_queues;
1526 }
1527
Jacob Keller62fe2a82016-07-27 12:02:33 -07001528 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001529 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001530 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001531 if (err) {
1532 dev_err(&adapter->pdev->dev,
1533 "Unable to setup interrupt capabilities\n");
1534 goto err_set_interrupt;
1535 }
1536
1537 err = i40evf_alloc_q_vectors(adapter);
1538 if (err) {
1539 dev_err(&adapter->pdev->dev,
1540 "Unable to allocate memory for queue vectors\n");
1541 goto err_alloc_q_vectors;
1542 }
1543
Greg Rose5eae00c2013-12-21 06:12:45 +00001544 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001545 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1546 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001547
1548 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001549err_alloc_q_vectors:
1550 i40evf_reset_interrupt_capability(adapter);
1551err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001552 i40evf_free_queues(adapter);
1553err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001554 return err;
1555}
1556
1557/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001558 * i40evf_free_rss - Free memory used by RSS structs
1559 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001560 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001561static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001562{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001563 kfree(adapter->rss_key);
1564 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001565
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001566 kfree(adapter->rss_lut);
1567 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001568}
1569
1570/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001571 * i40evf_watchdog_timer - Periodic call-back timer
1572 * @data: pointer to adapter disguised as unsigned long
1573 **/
1574static void i40evf_watchdog_timer(unsigned long data)
1575{
1576 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001577
Greg Rose5eae00c2013-12-21 06:12:45 +00001578 schedule_work(&adapter->watchdog_task);
1579 /* timer will be rescheduled in watchdog task */
1580}
1581
1582/**
1583 * i40evf_watchdog_task - Periodic call-back task
1584 * @work: pointer to work_struct
1585 **/
1586static void i40evf_watchdog_task(struct work_struct *work)
1587{
1588 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001589 struct i40evf_adapter,
1590 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001591 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001592 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001593
Greg Rose5eae00c2013-12-21 06:12:45 +00001594 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001595 goto restart_watchdog;
1596
1597 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001598 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1599 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001600 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1601 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001602 /* A chance for redemption! */
1603 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1604 adapter->state = __I40EVF_STARTUP;
1605 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1606 schedule_delayed_work(&adapter->init_task, 10);
1607 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1608 &adapter->crit_section);
1609 /* Don't reschedule the watchdog, since we've restarted
1610 * the init task. When init_task contacts the PF and
1611 * gets everything set up again, it'll restart the
1612 * watchdog for us. Down, boy. Sit. Stay. Woof.
1613 */
1614 return;
1615 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001616 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001617 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001618 goto watchdog_done;
1619 }
1620
1621 if ((adapter->state < __I40EVF_DOWN) ||
1622 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001623 goto watchdog_done;
1624
Mitch Williamsef8693e2014-02-13 03:48:53 -08001625 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001626 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1627 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001628 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001629 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001630 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001631 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001632 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001633 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001634 goto watchdog_done;
1635 }
1636
1637 /* Process admin queue tasks. After init, everything gets done
1638 * here so we don't race on the admin queue.
1639 */
Mitch Williamsed636962015-04-07 19:45:32 -04001640 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001641 if (!i40evf_asq_done(hw)) {
1642 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1643 i40evf_send_api_ver(adapter);
1644 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001645 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001646 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001647 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1648 i40evf_send_vf_config_msg(adapter);
1649 goto watchdog_done;
1650 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001651
Mitch Williamse284fc82015-03-27 00:12:09 -07001652 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1653 i40evf_disable_queues(adapter);
1654 goto watchdog_done;
1655 }
1656
Greg Rose5eae00c2013-12-21 06:12:45 +00001657 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1658 i40evf_map_queues(adapter);
1659 goto watchdog_done;
1660 }
1661
1662 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1663 i40evf_add_ether_addrs(adapter);
1664 goto watchdog_done;
1665 }
1666
1667 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1668 i40evf_add_vlans(adapter);
1669 goto watchdog_done;
1670 }
1671
1672 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1673 i40evf_del_ether_addrs(adapter);
1674 goto watchdog_done;
1675 }
1676
1677 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1678 i40evf_del_vlans(adapter);
1679 goto watchdog_done;
1680 }
1681
Mariusz Stachura87743702017-07-17 22:09:45 -07001682 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1683 i40evf_enable_vlan_stripping(adapter);
1684 goto watchdog_done;
1685 }
1686
1687 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1688 i40evf_disable_vlan_stripping(adapter);
1689 goto watchdog_done;
1690 }
1691
Greg Rose5eae00c2013-12-21 06:12:45 +00001692 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1693 i40evf_configure_queues(adapter);
1694 goto watchdog_done;
1695 }
1696
1697 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1698 i40evf_enable_queues(adapter);
1699 goto watchdog_done;
1700 }
1701
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001702 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1703 /* This message goes straight to the firmware, not the
1704 * PF, so we don't have to set current_op as we will
1705 * not get a response through the ARQ.
1706 */
Helin Zhang96a81982015-10-26 19:44:31 -04001707 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001708 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1709 goto watchdog_done;
1710 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001711 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1712 i40evf_get_hena(adapter);
1713 goto watchdog_done;
1714 }
1715 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1716 i40evf_set_hena(adapter);
1717 goto watchdog_done;
1718 }
1719 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1720 i40evf_set_rss_key(adapter);
1721 goto watchdog_done;
1722 }
1723 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1724 i40evf_set_rss_lut(adapter);
1725 goto watchdog_done;
1726 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001727
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001728 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001729 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1730 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001731 goto watchdog_done;
1732 }
1733
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001734 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001735 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001736 goto watchdog_done;
1737 }
1738
1739 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1740 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001741 i40evf_set_promiscuous(adapter, 0);
1742 goto watchdog_done;
1743 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001744 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001745
Greg Rose5eae00c2013-12-21 06:12:45 +00001746 if (adapter->state == __I40EVF_RUNNING)
1747 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001748watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001749 if (adapter->state == __I40EVF_RUNNING) {
1750 i40evf_irq_enable_queues(adapter, ~0);
1751 i40evf_fire_sw_int(adapter, 0xFF);
1752 } else {
1753 i40evf_fire_sw_int(adapter, 0x1);
1754 }
1755
Mitch Williamsef8693e2014-02-13 03:48:53 -08001756 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1757restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001758 if (adapter->state == __I40EVF_REMOVE)
1759 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001760 if (adapter->aq_required)
1761 mod_timer(&adapter->watchdog_timer,
1762 jiffies + msecs_to_jiffies(20));
1763 else
1764 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001765 schedule_work(&adapter->adminq_task);
1766}
1767
Joe Perchesdedecb62016-11-01 15:35:14 -07001768static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1769{
1770 struct i40evf_mac_filter *f, *ftmp;
1771 struct i40evf_vlan_filter *fv, *fvtmp;
1772
1773 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1774
1775 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001776 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001777 netif_carrier_off(adapter->netdev);
1778 netif_tx_disable(adapter->netdev);
1779 adapter->link_up = false;
1780 i40evf_napi_disable_all(adapter);
1781 i40evf_irq_disable(adapter);
1782 i40evf_free_traffic_irqs(adapter);
1783 i40evf_free_all_tx_resources(adapter);
1784 i40evf_free_all_rx_resources(adapter);
1785 }
1786
1787 /* Delete all of the filters, both MAC and VLAN. */
1788 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1789 list_del(&f->list);
1790 kfree(f);
1791 }
1792
1793 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1794 list_del(&fv->list);
1795 kfree(fv);
1796 }
1797
1798 i40evf_free_misc_irq(adapter);
1799 i40evf_reset_interrupt_capability(adapter);
1800 i40evf_free_queues(adapter);
1801 i40evf_free_q_vectors(adapter);
1802 kfree(adapter->vf_res);
1803 i40evf_shutdown_adminq(&adapter->hw);
1804 adapter->netdev->flags &= ~IFF_UP;
1805 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1806 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1807 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001808 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001809 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1810}
1811
Mitch Williams67c818a2015-06-19 08:56:30 -07001812#define I40EVF_RESET_WAIT_MS 10
1813#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001814/**
1815 * i40evf_reset_task - Call-back task to handle hardware reset
1816 * @work: pointer to work_struct
1817 *
1818 * During reset we need to shut down and reinitialize the admin queue
1819 * before we can use it to communicate with the PF again. We also clear
1820 * and reinit the rings because that context is lost as well.
1821 **/
1822static void i40evf_reset_task(struct work_struct *work)
1823{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001824 struct i40evf_adapter *adapter = container_of(work,
1825 struct i40evf_adapter,
1826 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001827 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001828 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001829 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001830 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001831 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001832 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001833
Mitch Williamsed0e8942017-01-24 10:23:59 -08001834 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001835 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001836 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001837 if (CLIENT_ENABLED(adapter)) {
1838 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1839 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1840 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1841 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1842 cancel_delayed_work_sync(&adapter->client_task);
1843 i40evf_notify_client_close(&adapter->vsi, true);
1844 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001845 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001846 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001847 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1848 /* Restart the AQ here. If we have been reset but didn't
1849 * detect it, or if the PF had to reinit, our AQ will be hosed.
1850 */
1851 i40evf_shutdown_adminq(hw);
1852 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001853 i40evf_request_reset(adapter);
1854 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001855 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001856
Mitch Williamsef8693e2014-02-13 03:48:53 -08001857 /* poll until we see the reset actually happen */
1858 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001859 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1860 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1861 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001862 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001863 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001864 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001865 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001866 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001867 goto continue_reset; /* act like the reset happened */
1868 }
1869
1870 /* wait until the reset is complete and the PF is responding to us */
1871 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001872 /* sleep first to make sure a minimum wait time is met */
1873 msleep(I40EVF_RESET_WAIT_MS);
1874
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001875 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1876 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001877 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001878 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001879 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001880
Mitch Williams509a4472015-12-23 12:05:52 -08001881 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001882
Mitch Williamsef8693e2014-02-13 03:48:53 -08001883 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001884 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001885 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001886 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001887 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001888 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001889 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001890
1891continue_reset:
Alan Bradyf0db7892017-07-12 05:46:04 -04001892 if (netif_running(netdev)) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001893 netif_carrier_off(netdev);
1894 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001895 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001896 i40evf_napi_disable_all(adapter);
1897 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001898 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001899
Greg Rose5eae00c2013-12-21 06:12:45 +00001900 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001901 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1902
1903 /* free the Tx/Rx rings and descriptors, might be better to just
1904 * re-use them sometime in the future
1905 */
1906 i40evf_free_all_rx_resources(adapter);
1907 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001908
1909 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001910 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001911 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001912 err = i40evf_init_adminq(hw);
1913 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001914 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1915 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001916
Mitch Williamse6d038d2015-06-04 16:23:58 -04001917 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1918 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001919
1920 /* re-add all MAC filters */
1921 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1922 f->add = true;
1923 }
1924 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001925 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1926 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001927 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001928 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001929 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001930 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001931 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001932 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001933
1934 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1935
1936 if (netif_running(adapter->netdev)) {
1937 /* allocate transmit descriptors */
1938 err = i40evf_setup_all_tx_resources(adapter);
1939 if (err)
1940 goto reset_err;
1941
1942 /* allocate receive descriptors */
1943 err = i40evf_setup_all_rx_resources(adapter);
1944 if (err)
1945 goto reset_err;
1946
1947 i40evf_configure(adapter);
1948
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001949 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001950
1951 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001952 } else {
1953 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001954 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001955 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001956
Greg Rose5eae00c2013-12-21 06:12:45 +00001957 return;
1958reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001959 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001960 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001961}
1962
1963/**
1964 * i40evf_adminq_task - worker thread to clean the admin queue
1965 * @work: pointer to work_struct containing our data
1966 **/
1967static void i40evf_adminq_task(struct work_struct *work)
1968{
1969 struct i40evf_adapter *adapter =
1970 container_of(work, struct i40evf_adapter, adminq_task);
1971 struct i40e_hw *hw = &adapter->hw;
1972 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001973 enum virtchnl_ops v_op;
1974 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001975 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001976 u16 pending;
1977
Mitch Williamsef8693e2014-02-13 03:48:53 -08001978 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001979 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001980
Mitch Williams1001dc32014-11-11 20:02:19 +00001981 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1982 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001983 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001984 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001985
Greg Rose5eae00c2013-12-21 06:12:45 +00001986 do {
1987 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07001988 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
1989 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
1990
1991 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00001992 break; /* No event to process or error cleaning ARQ */
1993
Tushar Davec969ef42017-06-22 09:44:32 -07001994 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001995 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001996 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001997 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001998 } while (pending);
1999
Mitch Williams67c818a2015-06-19 08:56:30 -07002000 if ((adapter->flags &
2001 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2002 adapter->state == __I40EVF_RESETTING)
2003 goto freedom;
2004
Mitch Williams912257e2014-05-22 06:32:07 +00002005 /* check for error indications */
2006 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002007 if (val == 0xdeadbeef) /* indicates device in reset */
2008 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002009 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002010 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002011 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002012 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002013 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002014 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002015 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002016 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002017 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002018 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002019 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002020 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002021 }
2022 if (oldval != val)
2023 wr32(hw, hw->aq.arq.len, val);
2024
2025 val = rd32(hw, hw->aq.asq.len);
2026 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002027 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002028 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002029 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002030 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002031 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002032 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002033 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002034 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002035 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002036 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002037 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002038 }
2039 if (oldval != val)
2040 wr32(hw, hw->aq.asq.len, val);
2041
Mitch Williams67c818a2015-06-19 08:56:30 -07002042freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002043 kfree(event.msg_buf);
2044out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002045 /* re-enable Admin queue interrupt cause */
2046 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002047}
2048
2049/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002050 * i40evf_client_task - worker thread to perform client work
2051 * @work: pointer to work_struct containing our data
2052 *
2053 * This task handles client interactions. Because client calls can be
2054 * reentrant, we can't handle them in the watchdog.
2055 **/
2056static void i40evf_client_task(struct work_struct *work)
2057{
2058 struct i40evf_adapter *adapter =
2059 container_of(work, struct i40evf_adapter, client_task.work);
2060
2061 /* If we can't get the client bit, just give up. We'll be rescheduled
2062 * later.
2063 */
2064
2065 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2066 return;
2067
2068 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2069 i40evf_client_subtask(adapter);
2070 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2071 goto out;
2072 }
2073 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2074 i40evf_notify_client_close(&adapter->vsi, false);
2075 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2076 goto out;
2077 }
2078 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2079 i40evf_notify_client_open(&adapter->vsi);
2080 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2081 goto out;
2082 }
2083 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2084 i40evf_notify_client_l2_params(&adapter->vsi);
2085 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2086 }
2087out:
2088 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2089}
2090
2091/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002092 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2093 * @adapter: board private structure
2094 *
2095 * Free all transmit software resources
2096 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002097void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002098{
2099 int i;
2100
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002101 if (!adapter->tx_rings)
2102 return;
2103
Mitch Williamscc052922014-10-25 03:24:34 +00002104 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002105 if (adapter->tx_rings[i].desc)
2106 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002107}
2108
2109/**
2110 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2111 * @adapter: board private structure
2112 *
2113 * If this function returns with an error, then it's possible one or
2114 * more of the rings is populated (while the rest are not). It is the
2115 * callers duty to clean those orphaned rings.
2116 *
2117 * Return 0 on success, negative on failure
2118 **/
2119static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2120{
2121 int i, err = 0;
2122
Mitch Williamscc052922014-10-25 03:24:34 +00002123 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002124 adapter->tx_rings[i].count = adapter->tx_desc_count;
2125 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002126 if (!err)
2127 continue;
2128 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002129 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002130 break;
2131 }
2132
2133 return err;
2134}
2135
2136/**
2137 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2138 * @adapter: board private structure
2139 *
2140 * If this function returns with an error, then it's possible one or
2141 * more of the rings is populated (while the rest are not). It is the
2142 * callers duty to clean those orphaned rings.
2143 *
2144 * Return 0 on success, negative on failure
2145 **/
2146static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2147{
2148 int i, err = 0;
2149
Mitch Williamscc052922014-10-25 03:24:34 +00002150 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002151 adapter->rx_rings[i].count = adapter->rx_desc_count;
2152 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002153 if (!err)
2154 continue;
2155 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002156 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002157 break;
2158 }
2159 return err;
2160}
2161
2162/**
2163 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2164 * @adapter: board private structure
2165 *
2166 * Free all receive software resources
2167 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002168void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002169{
2170 int i;
2171
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002172 if (!adapter->rx_rings)
2173 return;
2174
Mitch Williamscc052922014-10-25 03:24:34 +00002175 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002176 if (adapter->rx_rings[i].desc)
2177 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002178}
2179
2180/**
2181 * i40evf_open - Called when a network interface is made active
2182 * @netdev: network interface device structure
2183 *
2184 * Returns 0 on success, negative value on failure
2185 *
2186 * The open entry point is called when a network interface is made
2187 * active by the system (IFF_UP). At this point all resources needed
2188 * for transmit and receive operations are allocated, the interrupt
2189 * handler is registered with the OS, the watchdog timer is started,
2190 * and the stack is notified that the interface is ready.
2191 **/
2192static int i40evf_open(struct net_device *netdev)
2193{
2194 struct i40evf_adapter *adapter = netdev_priv(netdev);
2195 int err;
2196
Mitch Williamsef8693e2014-02-13 03:48:53 -08002197 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2198 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2199 return -EIO;
2200 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002201
2202 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002203 return -EBUSY;
2204
2205 /* allocate transmit descriptors */
2206 err = i40evf_setup_all_tx_resources(adapter);
2207 if (err)
2208 goto err_setup_tx;
2209
2210 /* allocate receive descriptors */
2211 err = i40evf_setup_all_rx_resources(adapter);
2212 if (err)
2213 goto err_setup_rx;
2214
2215 /* clear any pending interrupts, may auto mask */
2216 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2217 if (err)
2218 goto err_req_irq;
2219
Mitch Williams44151cd2015-04-27 14:57:17 -04002220 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002221 i40evf_configure(adapter);
2222
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002223 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002224
2225 i40evf_irq_enable(adapter, true);
2226
2227 return 0;
2228
2229err_req_irq:
2230 i40evf_down(adapter);
2231 i40evf_free_traffic_irqs(adapter);
2232err_setup_rx:
2233 i40evf_free_all_rx_resources(adapter);
2234err_setup_tx:
2235 i40evf_free_all_tx_resources(adapter);
2236
2237 return err;
2238}
2239
2240/**
2241 * i40evf_close - Disables a network interface
2242 * @netdev: network interface device structure
2243 *
2244 * Returns 0, this is not allowed to fail
2245 *
2246 * The close entry point is called when an interface is de-activated
2247 * by the OS. The hardware is still under the drivers control, but
2248 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2249 * are freed, along with all transmit and receive resources.
2250 **/
2251static int i40evf_close(struct net_device *netdev)
2252{
2253 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002254 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002255
Mitch Williams209dc4d2015-12-09 15:50:27 -08002256 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002257 return 0;
2258
Mitch Williamsef8693e2014-02-13 03:48:53 -08002259
Jacob Keller0da36b92017-04-19 09:25:55 -04002260 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002261 if (CLIENT_ENABLED(adapter))
2262 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002263
2264 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002265 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002266 i40evf_free_traffic_irqs(adapter);
2267
Mitch Williams51f38262016-12-12 15:44:11 -08002268 /* We explicitly don't free resources here because the hardware is
2269 * still active and can DMA into memory. Resources are cleared in
2270 * i40evf_virtchnl_completion() after we get confirmation from the PF
2271 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002272 *
2273 * Also, we wait for state to transition to __I40EVF_DOWN before
2274 * returning. State change occurs in i40evf_virtchnl_completion() after
2275 * VF resources are released (which occurs after PF driver processes and
2276 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002277 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002278
2279 status = wait_event_timeout(adapter->down_waitqueue,
2280 adapter->state == __I40EVF_DOWN,
2281 msecs_to_jiffies(200));
2282 if (!status)
2283 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002284 return 0;
2285}
2286
2287/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002288 * i40evf_change_mtu - Change the Maximum Transfer Unit
2289 * @netdev: network interface device structure
2290 * @new_mtu: new value for maximum frame size
2291 *
2292 * Returns 0 on success, negative on failure
2293 **/
2294static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2295{
2296 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002297
Greg Rose5eae00c2013-12-21 06:12:45 +00002298 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002299 if (CLIENT_ENABLED(adapter)) {
2300 i40evf_notify_client_l2_params(&adapter->vsi);
2301 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2302 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002303 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2304 schedule_work(&adapter->reset_task);
2305
Greg Rose5eae00c2013-12-21 06:12:45 +00002306 return 0;
2307}
2308
Alexander Duyck06fc0162016-10-25 16:08:47 -07002309/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002310 * i40e_set_features - set the netdev feature flags
2311 * @netdev: ptr to the netdev being adjusted
2312 * @features: the feature set that the stack is suggesting
2313 * Note: expects to be called while under rtnl_lock()
2314 **/
2315static int i40evf_set_features(struct net_device *netdev,
2316 netdev_features_t features)
2317{
2318 struct i40evf_adapter *adapter = netdev_priv(netdev);
2319
2320 if (!VLAN_ALLOWED(adapter))
2321 return -EINVAL;
2322
2323 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2324 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2325 else
2326 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2327
2328 return 0;
2329}
2330
2331/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002332 * i40evf_features_check - Validate encapsulated packet conforms to limits
2333 * @skb: skb buff
2334 * @netdev: This physical port's netdev
2335 * @features: Offload features that the stack believes apply
2336 **/
2337static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2338 struct net_device *dev,
2339 netdev_features_t features)
2340{
2341 size_t len;
2342
2343 /* No point in doing any of this if neither checksum nor GSO are
2344 * being requested for this frame. We can rule out both by just
2345 * checking for CHECKSUM_PARTIAL
2346 */
2347 if (skb->ip_summed != CHECKSUM_PARTIAL)
2348 return features;
2349
2350 /* We cannot support GSO if the MSS is going to be less than
2351 * 64 bytes. If it is then we need to drop support for GSO.
2352 */
2353 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2354 features &= ~NETIF_F_GSO_MASK;
2355
2356 /* MACLEN can support at most 63 words */
2357 len = skb_network_header(skb) - skb->data;
2358 if (len & ~(63 * 2))
2359 goto out_err;
2360
2361 /* IPLEN and EIPLEN can support at most 127 dwords */
2362 len = skb_transport_header(skb) - skb_network_header(skb);
2363 if (len & ~(127 * 4))
2364 goto out_err;
2365
2366 if (skb->encapsulation) {
2367 /* L4TUNLEN can support 127 words */
2368 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2369 if (len & ~(127 * 2))
2370 goto out_err;
2371
2372 /* IPLEN can support at most 127 dwords */
2373 len = skb_inner_transport_header(skb) -
2374 skb_inner_network_header(skb);
2375 if (len & ~(127 * 4))
2376 goto out_err;
2377 }
2378
2379 /* No need to validate L4LEN as TCP is the only protocol with a
2380 * a flexible value and we support all possible values supported
2381 * by TCP, which is at most 15 dwords
2382 */
2383
2384 return features;
2385out_err:
2386 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2387}
2388
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002389#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2390 NETIF_F_HW_VLAN_CTAG_RX |\
2391 NETIF_F_HW_VLAN_CTAG_FILTER)
2392
2393/**
2394 * i40evf_fix_features - fix up the netdev feature bits
2395 * @netdev: our net device
2396 * @features: desired feature bits
2397 *
2398 * Returns fixed-up features bits
2399 **/
2400static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2401 netdev_features_t features)
2402{
2403 struct i40evf_adapter *adapter = netdev_priv(netdev);
2404
2405 features &= ~I40EVF_VLAN_FEATURES;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002406 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002407 features |= I40EVF_VLAN_FEATURES;
2408 return features;
2409}
2410
Greg Rose5eae00c2013-12-21 06:12:45 +00002411static const struct net_device_ops i40evf_netdev_ops = {
2412 .ndo_open = i40evf_open,
2413 .ndo_stop = i40evf_close,
2414 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002415 .ndo_set_rx_mode = i40evf_set_rx_mode,
2416 .ndo_validate_addr = eth_validate_addr,
2417 .ndo_set_mac_address = i40evf_set_mac,
2418 .ndo_change_mtu = i40evf_change_mtu,
2419 .ndo_tx_timeout = i40evf_tx_timeout,
2420 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2421 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002422 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002423 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002424 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002425#ifdef CONFIG_NET_POLL_CONTROLLER
2426 .ndo_poll_controller = i40evf_netpoll,
2427#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002428};
2429
2430/**
2431 * i40evf_check_reset_complete - check that VF reset is complete
2432 * @hw: pointer to hw struct
2433 *
2434 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2435 **/
2436static int i40evf_check_reset_complete(struct i40e_hw *hw)
2437{
2438 u32 rstat;
2439 int i;
2440
2441 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002442 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2443 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002444 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2445 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002446 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002447 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002448 }
2449 return -EBUSY;
2450}
2451
2452/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002453 * i40evf_process_config - Process the config information we got from the PF
2454 * @adapter: board private structure
2455 *
2456 * Verify that we have a valid config struct, and set up our netdev features
2457 * and our VSI struct.
2458 **/
2459int i40evf_process_config(struct i40evf_adapter *adapter)
2460{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002461 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002462 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002463 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002464 int i;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002465 netdev_features_t hw_enc_features;
2466 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002467
2468 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002469 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002470 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002471 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002472 }
2473 if (!adapter->vsi_res) {
2474 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2475 return -ENODEV;
2476 }
2477
Preethi Banalabacd75c2017-03-27 14:43:18 -07002478 hw_enc_features = NETIF_F_SG |
2479 NETIF_F_IP_CSUM |
2480 NETIF_F_IPV6_CSUM |
2481 NETIF_F_HIGHDMA |
2482 NETIF_F_SOFT_FEATURES |
2483 NETIF_F_TSO |
2484 NETIF_F_TSO_ECN |
2485 NETIF_F_TSO6 |
2486 NETIF_F_SCTP_CRC |
2487 NETIF_F_RXHASH |
2488 NETIF_F_RXCSUM |
2489 0;
2490
2491 /* advertise to stack only if offloads for encapsulated packets is
2492 * supported
2493 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002494 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002495 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002496 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002497 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002498 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002499 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002500 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002501 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002502 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002503
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002504 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002505 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002506 netdev->gso_partial_features |=
2507 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002508
Preethi Banalabacd75c2017-03-27 14:43:18 -07002509 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2510 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2511 netdev->hw_enc_features |= hw_enc_features;
2512 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002513 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002514 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002515
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002516 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002517 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002518 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002519 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002520
Preethi Banalabacd75c2017-03-27 14:43:18 -07002521 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002522
Preethi Banalabacd75c2017-03-27 14:43:18 -07002523 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002524
2525 adapter->vsi.id = adapter->vsi_res->vsi_id;
2526
2527 adapter->vsi.back = adapter;
2528 adapter->vsi.base_vector = 1;
2529 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002530 vsi->netdev = adapter->netdev;
2531 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002532 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002533 adapter->rss_key_size = vfres->rss_key_size;
2534 adapter->rss_lut_size = vfres->rss_lut_size;
2535 } else {
2536 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2537 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2538 }
2539
Mitch Williamse6d038d2015-06-04 16:23:58 -04002540 return 0;
2541}
2542
2543/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002544 * i40evf_init_task - worker thread to perform delayed initialization
2545 * @work: pointer to work_struct containing our data
2546 *
2547 * This task completes the work that was begun in probe. Due to the nature
2548 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002549 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002550 * whole system, we'll do it in a task so we can sleep.
2551 * This task only runs during driver init. Once we've established
2552 * communications with the PF driver and set up our netdev, the watchdog
2553 * takes over.
2554 **/
2555static void i40evf_init_task(struct work_struct *work)
2556{
2557 struct i40evf_adapter *adapter = container_of(work,
2558 struct i40evf_adapter,
2559 init_task.work);
2560 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002561 struct i40e_hw *hw = &adapter->hw;
2562 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002563 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002564
2565 switch (adapter->state) {
2566 case __I40EVF_STARTUP:
2567 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002568 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2569 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002570 err = i40e_set_mac_type(hw);
2571 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002572 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2573 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002574 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002575 }
2576 err = i40evf_check_reset_complete(hw);
2577 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002578 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002579 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002580 goto err;
2581 }
2582 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2583 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2584 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2585 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2586
2587 err = i40evf_init_adminq(hw);
2588 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002589 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2590 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002591 goto err;
2592 }
2593 err = i40evf_send_api_ver(adapter);
2594 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002595 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002596 i40evf_shutdown_adminq(hw);
2597 goto err;
2598 }
2599 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2600 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002601 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002602 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002603 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002604 i40evf_shutdown_adminq(hw);
2605 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002606 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002607 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002608
2609 /* aq msg sent, awaiting reply */
2610 err = i40evf_verify_api_ver(adapter);
2611 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002612 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002613 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002614 else
2615 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2616 adapter->pf_version.major,
2617 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002618 VIRTCHNL_VERSION_MAJOR,
2619 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002620 goto err;
2621 }
2622 err = i40evf_send_vf_config_msg(adapter);
2623 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002624 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002625 err);
2626 goto err;
2627 }
2628 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2629 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002630 case __I40EVF_INIT_GET_RESOURCES:
2631 /* aq msg sent, awaiting reply */
2632 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002633 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002634 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002635 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002636 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002637 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002638 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002639 }
2640 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002641 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002642 err = i40evf_send_vf_config_msg(adapter);
2643 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002644 } else if (err == I40E_ERR_PARAM) {
2645 /* We only get ERR_PARAM if the device is in a very bad
2646 * state or if we've been disabled for previous bad
2647 * behavior. Either way, we're done now.
2648 */
2649 i40evf_shutdown_adminq(hw);
2650 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2651 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002652 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002653 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002654 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2655 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002656 goto err_alloc;
2657 }
2658 adapter->state = __I40EVF_INIT_SW;
2659 break;
2660 default:
2661 goto err_alloc;
2662 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002663
Mitch Williamse6d038d2015-06-04 16:23:58 -04002664 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002665 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002666 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002667
2668 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2669
Greg Rose5eae00c2013-12-21 06:12:45 +00002670 netdev->netdev_ops = &i40evf_netdev_ops;
2671 i40evf_set_ethtool_ops(netdev);
2672 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002673
Jarod Wilson91c527a2016-10-17 15:54:05 -04002674 /* MTU range: 68 - 9710 */
2675 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002676 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002677
Greg Rose5eae00c2013-12-21 06:12:45 +00002678 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002679 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002680 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002681 eth_hw_addr_random(netdev);
2682 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2683 } else {
2684 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2685 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2686 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002687 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002688
Allen Pais7d8fb3a2017-09-21 22:35:20 +05302689 setup_timer(&adapter->watchdog_timer, &i40evf_watchdog_timer,
2690 (unsigned long)adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002691 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2692
Mitch Williamsd732a182014-04-24 06:41:37 +00002693 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2694 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002695 err = i40evf_init_interrupt_scheme(adapter);
2696 if (err)
2697 goto err_sw_init;
2698 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002699 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002700 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002701 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2702
Greg Rose5eae00c2013-12-21 06:12:45 +00002703 err = i40evf_request_misc_irq(adapter);
2704 if (err)
2705 goto err_sw_init;
2706
2707 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002708 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002709
Mitch Williamsef8693e2014-02-13 03:48:53 -08002710 if (!adapter->netdev_registered) {
2711 err = register_netdev(netdev);
2712 if (err)
2713 goto err_register;
2714 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002715
2716 adapter->netdev_registered = true;
2717
2718 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002719 if (CLIENT_ALLOWED(adapter)) {
2720 err = i40evf_lan_add_device(adapter);
2721 if (err)
2722 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2723 err);
2724 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002725
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002726 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002727 if (netdev->features & NETIF_F_GRO)
2728 dev_info(&pdev->dev, "GRO is enabled\n");
2729
Greg Rose5eae00c2013-12-21 06:12:45 +00002730 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002731 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002732 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002733 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002734
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002735 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2736 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2737 if (!adapter->rss_key || !adapter->rss_lut)
2738 goto err_mem;
2739
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002740 if (RSS_AQ(adapter)) {
2741 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2742 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2743 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002744 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002745 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002746 return;
2747restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002748 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002749 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002750err_mem:
2751 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002752err_register:
2753 i40evf_free_misc_irq(adapter);
2754err_sw_init:
2755 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002756err_alloc:
2757 kfree(adapter->vf_res);
2758 adapter->vf_res = NULL;
2759err:
2760 /* Things went into the weeds, so try again later */
2761 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002762 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002763 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002764 i40evf_shutdown_adminq(hw);
2765 adapter->state = __I40EVF_STARTUP;
2766 schedule_delayed_work(&adapter->init_task, HZ * 5);
2767 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002768 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002769 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002770}
2771
2772/**
2773 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2774 * @pdev: pci device structure
2775 **/
2776static void i40evf_shutdown(struct pci_dev *pdev)
2777{
2778 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002779 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002780
2781 netif_device_detach(netdev);
2782
2783 if (netif_running(netdev))
2784 i40evf_close(netdev);
2785
Mitch Williams00293fd2015-01-09 11:18:18 +00002786 /* Prevent the watchdog from running. */
2787 adapter->state = __I40EVF_REMOVE;
2788 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002789
Greg Rose5eae00c2013-12-21 06:12:45 +00002790#ifdef CONFIG_PM
2791 pci_save_state(pdev);
2792
2793#endif
2794 pci_disable_device(pdev);
2795}
2796
2797/**
2798 * i40evf_probe - Device Initialization Routine
2799 * @pdev: PCI device information struct
2800 * @ent: entry in i40evf_pci_tbl
2801 *
2802 * Returns 0 on success, negative on failure
2803 *
2804 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2805 * The OS initialization, configuring of the adapter private structure,
2806 * and a hardware reset occur.
2807 **/
2808static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2809{
2810 struct net_device *netdev;
2811 struct i40evf_adapter *adapter = NULL;
2812 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002813 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002814
2815 err = pci_enable_device(pdev);
2816 if (err)
2817 return err;
2818
Mitch Williams64942942014-02-11 08:26:33 +00002819 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002820 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002821 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2822 if (err) {
2823 dev_err(&pdev->dev,
2824 "DMA configuration failed: 0x%x\n", err);
2825 goto err_dma;
2826 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002827 }
2828
2829 err = pci_request_regions(pdev, i40evf_driver_name);
2830 if (err) {
2831 dev_err(&pdev->dev,
2832 "pci_request_regions failed 0x%x\n", err);
2833 goto err_pci_reg;
2834 }
2835
2836 pci_enable_pcie_error_reporting(pdev);
2837
2838 pci_set_master(pdev);
2839
Mitch Williams1255b7a2015-11-06 15:25:59 -08002840 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002841 if (!netdev) {
2842 err = -ENOMEM;
2843 goto err_alloc_etherdev;
2844 }
2845
2846 SET_NETDEV_DEV(netdev, &pdev->dev);
2847
2848 pci_set_drvdata(pdev, netdev);
2849 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002850
2851 adapter->netdev = netdev;
2852 adapter->pdev = pdev;
2853
2854 hw = &adapter->hw;
2855 hw->back = adapter;
2856
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002857 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002858 adapter->state = __I40EVF_STARTUP;
2859
2860 /* Call save state here because it relies on the adapter struct. */
2861 pci_save_state(pdev);
2862
2863 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2864 pci_resource_len(pdev, 0));
2865 if (!hw->hw_addr) {
2866 err = -EIO;
2867 goto err_ioremap;
2868 }
2869 hw->vendor_id = pdev->vendor;
2870 hw->device_id = pdev->device;
2871 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2872 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2873 hw->subsystem_device_id = pdev->subsystem_device;
2874 hw->bus.device = PCI_SLOT(pdev->devfn);
2875 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002876 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002877
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002878 /* set up the locks for the AQ, do this only once in probe
2879 * and destroy them only once in remove
2880 */
2881 mutex_init(&hw->aq.asq_mutex);
2882 mutex_init(&hw->aq.arq_mutex);
2883
Serey Kong8bb1a542014-08-01 13:27:15 -07002884 INIT_LIST_HEAD(&adapter->mac_filter_list);
2885 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2886
Greg Rose5eae00c2013-12-21 06:12:45 +00002887 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2888 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2889 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002890 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002891 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002892 schedule_delayed_work(&adapter->init_task,
2893 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002894
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002895 /* Setup the wait queue for indicating transition to down status */
2896 init_waitqueue_head(&adapter->down_waitqueue);
2897
Greg Rose5eae00c2013-12-21 06:12:45 +00002898 return 0;
2899
2900err_ioremap:
2901 free_netdev(netdev);
2902err_alloc_etherdev:
2903 pci_release_regions(pdev);
2904err_pci_reg:
2905err_dma:
2906 pci_disable_device(pdev);
2907 return err;
2908}
2909
2910#ifdef CONFIG_PM
2911/**
2912 * i40evf_suspend - Power management suspend routine
2913 * @pdev: PCI device information struct
2914 * @state: unused
2915 *
2916 * Called when the system (VM) is entering sleep/suspend.
2917 **/
2918static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2919{
2920 struct net_device *netdev = pci_get_drvdata(pdev);
2921 struct i40evf_adapter *adapter = netdev_priv(netdev);
2922 int retval = 0;
2923
2924 netif_device_detach(netdev);
2925
2926 if (netif_running(netdev)) {
2927 rtnl_lock();
2928 i40evf_down(adapter);
2929 rtnl_unlock();
2930 }
2931 i40evf_free_misc_irq(adapter);
2932 i40evf_reset_interrupt_capability(adapter);
2933
2934 retval = pci_save_state(pdev);
2935 if (retval)
2936 return retval;
2937
2938 pci_disable_device(pdev);
2939
2940 return 0;
2941}
2942
2943/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002944 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002945 * @pdev: PCI device information struct
2946 *
2947 * Called when the system (VM) is resumed from sleep/suspend.
2948 **/
2949static int i40evf_resume(struct pci_dev *pdev)
2950{
2951 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2952 struct net_device *netdev = adapter->netdev;
2953 u32 err;
2954
2955 pci_set_power_state(pdev, PCI_D0);
2956 pci_restore_state(pdev);
2957 /* pci_restore_state clears dev->state_saved so call
2958 * pci_save_state to restore it.
2959 */
2960 pci_save_state(pdev);
2961
2962 err = pci_enable_device_mem(pdev);
2963 if (err) {
2964 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2965 return err;
2966 }
2967 pci_set_master(pdev);
2968
2969 rtnl_lock();
2970 err = i40evf_set_interrupt_capability(adapter);
2971 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002972 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002973 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2974 return err;
2975 }
2976 err = i40evf_request_misc_irq(adapter);
2977 rtnl_unlock();
2978 if (err) {
2979 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2980 return err;
2981 }
2982
2983 schedule_work(&adapter->reset_task);
2984
2985 netif_device_attach(netdev);
2986
2987 return err;
2988}
2989
2990#endif /* CONFIG_PM */
2991/**
2992 * i40evf_remove - Device Removal Routine
2993 * @pdev: PCI device information struct
2994 *
2995 * i40evf_remove is called by the PCI subsystem to alert the driver
2996 * that it should release a PCI device. The could be caused by a
2997 * Hot-Plug event, or because the driver is going to be removed from
2998 * memory.
2999 **/
3000static void i40evf_remove(struct pci_dev *pdev)
3001{
3002 struct net_device *netdev = pci_get_drvdata(pdev);
3003 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003004 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003005 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003006 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003007
3008 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003009 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003010 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003011 if (adapter->netdev_registered) {
3012 unregister_netdev(netdev);
3013 adapter->netdev_registered = false;
3014 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003015 if (CLIENT_ALLOWED(adapter)) {
3016 err = i40evf_lan_del_device(adapter);
3017 if (err)
3018 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3019 err);
3020 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003021
Mitch Williamsf4a71882015-01-09 11:18:16 +00003022 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003023 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003024 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003025 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003026 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003027 /* If the FW isn't responding, kick it once, but only once. */
3028 if (!i40evf_asq_done(hw)) {
3029 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003030 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003031 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003032 i40evf_free_all_tx_resources(adapter);
3033 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003034 i40evf_misc_irq_disable(adapter);
3035 i40evf_free_misc_irq(adapter);
3036 i40evf_reset_interrupt_capability(adapter);
3037 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003038
Mitch Williamse5d17c32014-06-04 04:22:38 +00003039 if (adapter->watchdog_timer.function)
3040 del_timer_sync(&adapter->watchdog_timer);
3041
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003042 flush_scheduled_work();
3043
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003044 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003045
Greg Rose5eae00c2013-12-21 06:12:45 +00003046 if (hw->aq.asq.count)
3047 i40evf_shutdown_adminq(hw);
3048
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003049 /* destroy the locks only once, here */
3050 mutex_destroy(&hw->aq.arq_mutex);
3051 mutex_destroy(&hw->aq.asq_mutex);
3052
Greg Rose5eae00c2013-12-21 06:12:45 +00003053 iounmap(hw->hw_addr);
3054 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003055 i40evf_free_all_tx_resources(adapter);
3056 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003057 i40evf_free_queues(adapter);
3058 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003059 /* If we got removed before an up/down sequence, we've got a filter
3060 * hanging out there that we need to get rid of.
3061 */
3062 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3063 list_del(&f->list);
3064 kfree(f);
3065 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003066 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3067 list_del(&f->list);
3068 kfree(f);
3069 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003070
3071 free_netdev(netdev);
3072
3073 pci_disable_pcie_error_reporting(pdev);
3074
3075 pci_disable_device(pdev);
3076}
3077
3078static struct pci_driver i40evf_driver = {
3079 .name = i40evf_driver_name,
3080 .id_table = i40evf_pci_tbl,
3081 .probe = i40evf_probe,
3082 .remove = i40evf_remove,
3083#ifdef CONFIG_PM
3084 .suspend = i40evf_suspend,
3085 .resume = i40evf_resume,
3086#endif
3087 .shutdown = i40evf_shutdown,
3088};
3089
3090/**
3091 * i40e_init_module - Driver Registration Routine
3092 *
3093 * i40e_init_module is the first routine called when the driver is
3094 * loaded. All it does is register with the PCI subsystem.
3095 **/
3096static int __init i40evf_init_module(void)
3097{
3098 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003099
Greg Rose5eae00c2013-12-21 06:12:45 +00003100 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003101 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003102
3103 pr_info("%s\n", i40evf_copyright);
3104
Jacob Keller6992a6c2016-08-04 11:37:01 -07003105 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3106 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003107 if (!i40evf_wq) {
3108 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3109 return -ENOMEM;
3110 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003111 ret = pci_register_driver(&i40evf_driver);
3112 return ret;
3113}
3114
3115module_init(i40evf_init_module);
3116
3117/**
3118 * i40e_exit_module - Driver Exit Cleanup Routine
3119 *
3120 * i40e_exit_module is called just before the driver is removed
3121 * from memory.
3122 **/
3123static void __exit i40evf_exit_module(void)
3124{
3125 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003126 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003127}
3128
3129module_exit(i40evf_exit_module);
3130
3131/* i40evf_main.c */