blob: 69ef6c1d5364f447c21cfde90470920b4413018c [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
Jacob Kellerb980c062017-07-14 09:27:06 -040049#define DRV_VERSION_BUILD 1
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{
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400435 int rings_remaining = adapter->num_active_queues;
436 int ridx = 0, vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000437 int q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +0000438 int err = 0;
439
440 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
441
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400442 for (; ridx < rings_remaining; ridx++) {
443 i40evf_map_vector_to_rxq(adapter, vidx, ridx);
444 i40evf_map_vector_to_txq(adapter, vidx, ridx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000445
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400446 /* In the case where we have more queues than vectors, continue
447 * round-robin on vectors until all queues are mapped.
448 */
449 if (++vidx >= q_vectors)
450 vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000451 }
452
Greg Rose5eae00c2013-12-21 06:12:45 +0000453 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
454
455 return err;
456}
457
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700458#ifdef CONFIG_NET_POLL_CONTROLLER
459/**
460 * i40evf_netpoll - A Polling 'interrupt' handler
461 * @netdev: network interface device structure
462 *
463 * This is used by netconsole to send skbs without having to re-enable
464 * interrupts. It's not called while the normal interrupt routine is executing.
465 **/
466static void i40evf_netpoll(struct net_device *netdev)
467{
468 struct i40evf_adapter *adapter = netdev_priv(netdev);
469 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
470 int i;
471
472 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400473 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700474 return;
475
476 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400477 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700478}
479
480#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000481/**
Alan Brady96db7762016-09-14 16:24:38 -0700482 * i40evf_irq_affinity_notify - Callback for affinity changes
483 * @notify: context as to what irq was changed
484 * @mask: the new affinity mask
485 *
486 * This is a callback function used by the irq_set_affinity_notifier function
487 * so that we may register to receive changes to the irq affinity masks.
488 **/
489static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
490 const cpumask_t *mask)
491{
492 struct i40e_q_vector *q_vector =
493 container_of(notify, struct i40e_q_vector, affinity_notify);
494
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400495 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700496}
497
498/**
499 * i40evf_irq_affinity_release - Callback for affinity notifier release
500 * @ref: internal core kernel usage
501 *
502 * This is a callback function used by the irq_set_affinity_notifier function
503 * to inform the current notification subscriber that they will no longer
504 * receive notifications.
505 **/
506static void i40evf_irq_affinity_release(struct kref *ref) {}
507
508/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000509 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
510 * @adapter: board private structure
511 *
512 * Allocates MSI-X vectors for tx and rx handling, and requests
513 * interrupts from the kernel.
514 **/
515static int
516i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
517{
Jacob Keller696ac80a2017-07-12 05:46:11 -0400518 unsigned int vector, q_vectors;
519 unsigned int rx_int_idx = 0, tx_int_idx = 0;
520 int irq_num, err;
Greg Rose5eae00c2013-12-21 06:12:45 +0000521
522 i40evf_irq_disable(adapter);
523 /* Decrement for Other and TCP Timer vectors */
524 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
525
526 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400527 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700528 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000529
530 if (q_vector->tx.ring && q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400531 snprintf(q_vector->name, sizeof(q_vector->name),
532 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000533 tx_int_idx++;
534 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400535 snprintf(q_vector->name, sizeof(q_vector->name),
536 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000537 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400538 snprintf(q_vector->name, sizeof(q_vector->name),
539 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000540 } else {
541 /* skip this unused q_vector */
542 continue;
543 }
Alan Brady96db7762016-09-14 16:24:38 -0700544 err = request_irq(irq_num,
545 i40evf_msix_clean_rings,
546 0,
547 q_vector->name,
548 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000549 if (err) {
550 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400551 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000552 goto free_queue_irqs;
553 }
Alan Brady96db7762016-09-14 16:24:38 -0700554 /* register for affinity change notifications */
555 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
556 q_vector->affinity_notify.release =
557 i40evf_irq_affinity_release;
558 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Keller759dc4a2017-07-14 09:10:10 -0400559 /* get_cpu_mask returns a static constant mask with
560 * a permanent lifetime so it's ok to use here.
561 */
562 irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
Greg Rose5eae00c2013-12-21 06:12:45 +0000563 }
564
565 return 0;
566
567free_queue_irqs:
568 while (vector) {
569 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700570 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
571 irq_set_affinity_notifier(irq_num, NULL);
572 irq_set_affinity_hint(irq_num, NULL);
573 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000574 }
575 return err;
576}
577
578/**
579 * i40evf_request_misc_irq - Initialize MSI-X interrupts
580 * @adapter: board private structure
581 *
582 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
583 * vector is only for the admin queue, and stays active even when the netdev
584 * is closed.
585 **/
586static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
587{
588 struct net_device *netdev = adapter->netdev;
589 int err;
590
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700591 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000592 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
593 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000594 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800595 &i40evf_msix_aq, 0,
596 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000597 if (err) {
598 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800599 "request_irq for %s failed: %d\n",
600 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000601 free_irq(adapter->msix_entries[0].vector, netdev);
602 }
603 return err;
604}
605
606/**
607 * i40evf_free_traffic_irqs - Free MSI-X interrupts
608 * @adapter: board private structure
609 *
610 * Frees all MSI-X vectors other than 0.
611 **/
612static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
613{
Alan Brady96db7762016-09-14 16:24:38 -0700614 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000615
Alan Brady47d2a5d2016-11-08 13:05:05 -0800616 if (!adapter->msix_entries)
617 return;
618
Greg Rose5eae00c2013-12-21 06:12:45 +0000619 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
620
Alan Brady96db7762016-09-14 16:24:38 -0700621 for (vector = 0; vector < q_vectors; vector++) {
622 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
623 irq_set_affinity_notifier(irq_num, NULL);
624 irq_set_affinity_hint(irq_num, NULL);
625 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000626 }
627}
628
629/**
630 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
631 * @adapter: board private structure
632 *
633 * Frees MSI-X vector 0.
634 **/
635static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
636{
637 struct net_device *netdev = adapter->netdev;
638
Jacob Kelleref4603e2016-11-08 13:05:08 -0800639 if (!adapter->msix_entries)
640 return;
641
Greg Rose5eae00c2013-12-21 06:12:45 +0000642 free_irq(adapter->msix_entries[0].vector, netdev);
643}
644
645/**
646 * i40evf_configure_tx - Configure Transmit Unit after Reset
647 * @adapter: board private structure
648 *
649 * Configure the Tx unit of the MAC after a reset.
650 **/
651static void i40evf_configure_tx(struct i40evf_adapter *adapter)
652{
653 struct i40e_hw *hw = &adapter->hw;
654 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000655
Mitch Williamscc052922014-10-25 03:24:34 +0000656 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400657 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000658}
659
660/**
661 * i40evf_configure_rx - Configure Receive Unit after Reset
662 * @adapter: board private structure
663 *
664 * Configure the Rx unit of the MAC after a reset.
665 **/
666static void i40evf_configure_rx(struct i40evf_adapter *adapter)
667{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700668 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000669 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000670 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000671
Alexander Duyckdab86af2017-03-14 10:15:27 -0700672 /* Legacy Rx will always default to a 2048 buffer size. */
673#if (PAGE_SIZE < 8192)
674 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200675 struct net_device *netdev = adapter->netdev;
676
Alexander Duyck98efd692017-04-05 07:51:01 -0400677 /* For jumbo frames on systems with 4K pages we have to use
678 * an order 1 page, so we might as well increase the size
679 * of our Rx buffer to make better use of the available space
680 */
681 rx_buf_len = I40E_RXBUFFER_3072;
682
Alexander Duyckdab86af2017-03-14 10:15:27 -0700683 /* We use a 1536 buffer size for configurations with
684 * standard Ethernet mtu. On x86 this gives us enough room
685 * for shared info and 192 bytes of padding.
686 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400687 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
688 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700689 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
690 }
691#endif
692
Mitch Williamscc052922014-10-25 03:24:34 +0000693 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400694 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700695 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400696
697 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
698 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
699 else
700 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000701 }
702}
703
704/**
705 * i40evf_find_vlan - Search filter list for specific vlan filter
706 * @adapter: board private structure
707 * @vlan: vlan tag
708 *
709 * Returns ptr to the filter object or NULL
710 **/
711static struct
712i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
713{
714 struct i40evf_vlan_filter *f;
715
716 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
717 if (vlan == f->vlan)
718 return f;
719 }
720 return NULL;
721}
722
723/**
724 * i40evf_add_vlan - Add a vlan filter to the list
725 * @adapter: board private structure
726 * @vlan: VLAN tag
727 *
728 * Returns ptr to the filter object or NULL when no memory available.
729 **/
730static struct
731i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
732{
Mitch Williams13acb542015-03-31 00:45:05 -0700733 struct i40evf_vlan_filter *f = NULL;
734 int count = 50;
735
736 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
737 &adapter->crit_section)) {
738 udelay(1);
739 if (--count == 0)
740 goto out;
741 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000742
743 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000744 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000745 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000746 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700747 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000748
Greg Rose5eae00c2013-12-21 06:12:45 +0000749 f->vlan = vlan;
750
751 INIT_LIST_HEAD(&f->list);
752 list_add(&f->list, &adapter->vlan_filter_list);
753 f->add = true;
754 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
755 }
756
Mitch Williams13acb542015-03-31 00:45:05 -0700757clearout:
758 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
759out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000760 return f;
761}
762
763/**
764 * i40evf_del_vlan - Remove a vlan filter from the list
765 * @adapter: board private structure
766 * @vlan: VLAN tag
767 **/
768static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
769{
770 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700771 int count = 50;
772
773 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
774 &adapter->crit_section)) {
775 udelay(1);
776 if (--count == 0)
777 return;
778 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000779
780 f = i40evf_find_vlan(adapter, vlan);
781 if (f) {
782 f->remove = true;
783 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
784 }
Mitch Williams13acb542015-03-31 00:45:05 -0700785 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000786}
787
788/**
789 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
790 * @netdev: network device struct
791 * @vid: VLAN tag
792 **/
793static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000794 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000795{
796 struct i40evf_adapter *adapter = netdev_priv(netdev);
797
Mitch Williams8ed995f2015-08-28 17:55:57 -0400798 if (!VLAN_ALLOWED(adapter))
799 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000800 if (i40evf_add_vlan(adapter, vid) == NULL)
801 return -ENOMEM;
802 return 0;
803}
804
805/**
806 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
807 * @netdev: network device struct
808 * @vid: VLAN tag
809 **/
810static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000811 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000812{
813 struct i40evf_adapter *adapter = netdev_priv(netdev);
814
Mitch Williams8ed995f2015-08-28 17:55:57 -0400815 if (VLAN_ALLOWED(adapter)) {
816 i40evf_del_vlan(adapter, vid);
817 return 0;
818 }
819 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000820}
821
822/**
823 * i40evf_find_filter - Search filter list for specific mac filter
824 * @adapter: board private structure
825 * @macaddr: the MAC address
826 *
827 * Returns ptr to the filter object or NULL
828 **/
829static struct
830i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
831 u8 *macaddr)
832{
833 struct i40evf_mac_filter *f;
834
835 if (!macaddr)
836 return NULL;
837
838 list_for_each_entry(f, &adapter->mac_filter_list, list) {
839 if (ether_addr_equal(macaddr, f->macaddr))
840 return f;
841 }
842 return NULL;
843}
844
845/**
846 * i40e_add_filter - Add a mac filter to the filter list
847 * @adapter: board private structure
848 * @macaddr: the MAC address
849 *
850 * Returns ptr to the filter object or NULL when no memory available.
851 **/
852static struct
853i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
854 u8 *macaddr)
855{
856 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000857 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000858
859 if (!macaddr)
860 return NULL;
861
862 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000863 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000864 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000865 if (--count == 0)
866 return NULL;
867 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000868
869 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000870 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000871 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000872 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000873 clear_bit(__I40EVF_IN_CRITICAL_TASK,
874 &adapter->crit_section);
875 return NULL;
876 }
877
Greg Rose9a173902014-05-22 06:32:02 +0000878 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000879
Mitch Williams63590b62016-05-16 10:26:42 -0700880 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000881 f->add = true;
882 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
883 }
884
885 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
886 return f;
887}
888
889/**
890 * i40evf_set_mac - NDO callback to set port mac address
891 * @netdev: network interface device structure
892 * @p: pointer to an address structure
893 *
894 * Returns 0 on success, negative on failure
895 **/
896static int i40evf_set_mac(struct net_device *netdev, void *p)
897{
898 struct i40evf_adapter *adapter = netdev_priv(netdev);
899 struct i40e_hw *hw = &adapter->hw;
900 struct i40evf_mac_filter *f;
901 struct sockaddr *addr = p;
902
903 if (!is_valid_ether_addr(addr->sa_data))
904 return -EADDRNOTAVAIL;
905
906 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
907 return 0;
908
Mitch Williams14e52ee2015-08-31 19:54:44 -0400909 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
910 return -EPERM;
911
912 f = i40evf_find_filter(adapter, hw->mac.addr);
913 if (f) {
914 f->remove = true;
915 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
916 }
917
Greg Rose5eae00c2013-12-21 06:12:45 +0000918 f = i40evf_add_filter(adapter, addr->sa_data);
919 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000920 ether_addr_copy(hw->mac.addr, addr->sa_data);
921 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000922 }
923
924 return (f == NULL) ? -ENOMEM : 0;
925}
926
927/**
928 * i40evf_set_rx_mode - NDO callback to set the netdev filters
929 * @netdev: network interface device structure
930 **/
931static void i40evf_set_rx_mode(struct net_device *netdev)
932{
933 struct i40evf_adapter *adapter = netdev_priv(netdev);
934 struct i40evf_mac_filter *f, *ftmp;
935 struct netdev_hw_addr *uca;
936 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400937 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000938 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000939
940 /* add addr if not already in the filter list */
941 netdev_for_each_uc_addr(uca, netdev) {
942 i40evf_add_filter(adapter, uca->addr);
943 }
944 netdev_for_each_mc_addr(mca, netdev) {
945 i40evf_add_filter(adapter, mca->addr);
946 }
947
948 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000949 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000950 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000951 if (--count == 0) {
952 dev_err(&adapter->pdev->dev,
953 "Failed to get lock in %s\n", __func__);
954 return;
955 }
956 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000957 /* remove filter if not in netdev list */
958 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400959 netdev_for_each_mc_addr(mca, netdev)
960 if (ether_addr_equal(mca->addr, f->macaddr))
961 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000962
Shannon Nelson2f41f332015-08-26 15:14:20 -0400963 netdev_for_each_uc_addr(uca, netdev)
964 if (ether_addr_equal(uca->addr, f->macaddr))
965 goto bottom_of_search_loop;
966
967 for_each_dev_addr(netdev, ha)
968 if (ether_addr_equal(ha->addr, f->macaddr))
969 goto bottom_of_search_loop;
970
971 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
972 goto bottom_of_search_loop;
973
974 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
975 f->remove = true;
976 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
977
978bottom_of_search_loop:
979 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000980 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700981
982 if (netdev->flags & IFF_PROMISC &&
983 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
984 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
985 else if (!(netdev->flags & IFF_PROMISC) &&
986 adapter->flags & I40EVF_FLAG_PROMISC_ON)
987 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
988
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700989 if (netdev->flags & IFF_ALLMULTI &&
990 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
991 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
992 else if (!(netdev->flags & IFF_ALLMULTI) &&
993 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
994 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
995
Greg Rose5eae00c2013-12-21 06:12:45 +0000996 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
997}
998
999/**
1000 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1001 * @adapter: board private structure
1002 **/
1003static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1004{
1005 int q_idx;
1006 struct i40e_q_vector *q_vector;
1007 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1008
1009 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1010 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001011
Mitch Williams7d96ba12015-10-26 19:44:39 -04001012 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001013 napi = &q_vector->napi;
1014 napi_enable(napi);
1015 }
1016}
1017
1018/**
1019 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1020 * @adapter: board private structure
1021 **/
1022static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1023{
1024 int q_idx;
1025 struct i40e_q_vector *q_vector;
1026 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1027
1028 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001029 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001030 napi_disable(&q_vector->napi);
1031 }
1032}
1033
1034/**
1035 * i40evf_configure - set up transmit and receive data structures
1036 * @adapter: board private structure
1037 **/
1038static void i40evf_configure(struct i40evf_adapter *adapter)
1039{
1040 struct net_device *netdev = adapter->netdev;
1041 int i;
1042
1043 i40evf_set_rx_mode(netdev);
1044
1045 i40evf_configure_tx(adapter);
1046 i40evf_configure_rx(adapter);
1047 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1048
Mitch Williamscc052922014-10-25 03:24:34 +00001049 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001050 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001051
Mitch Williamsb1630982016-04-18 11:33:48 -07001052 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001053 }
1054}
1055
1056/**
1057 * i40evf_up_complete - Finish the last steps of bringing up a connection
1058 * @adapter: board private structure
1059 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001060static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001061{
1062 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001063 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001064
1065 i40evf_napi_enable_all(adapter);
1066
1067 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001068 if (CLIENT_ENABLED(adapter))
1069 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001070 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001071}
1072
1073/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001074 * i40e_down - Shutdown the connection processing
1075 * @adapter: board private structure
1076 **/
1077void i40evf_down(struct i40evf_adapter *adapter)
1078{
1079 struct net_device *netdev = adapter->netdev;
1080 struct i40evf_mac_filter *f;
1081
Mitch Williams209dc4d2015-12-09 15:50:27 -08001082 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001083 return;
1084
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001085 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1086 &adapter->crit_section))
1087 usleep_range(500, 1000);
1088
Mitch Williams63e18c22015-03-27 00:12:10 -07001089 netif_carrier_off(netdev);
1090 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001091 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001092 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001093 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001094
Mitch Williamsef8693e2014-02-13 03:48:53 -08001095 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001096 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1097 f->remove = true;
1098 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001099 /* remove all VLAN filters */
1100 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1101 f->remove = true;
1102 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001103 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1104 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001105 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001106 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001107 /* Schedule operations to close down the HW. Don't wait
1108 * here for this to complete. The watchdog is still running
1109 * and it will take care of this.
1110 */
1111 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001112 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001113 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001114 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001115
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001116 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001117 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001118}
1119
1120/**
1121 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1122 * @adapter: board private structure
1123 * @vectors: number of vectors to request
1124 *
1125 * Work with the OS to set up the MSIX vectors needed.
1126 *
1127 * Returns 0 on success, negative on failure
1128 **/
1129static int
1130i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1131{
1132 int err, vector_threshold;
1133
1134 /* We'll want at least 3 (vector_threshold):
1135 * 0) Other (Admin Queue and link, mostly)
1136 * 1) TxQ[0] Cleanup
1137 * 2) RxQ[0] Cleanup
1138 */
1139 vector_threshold = MIN_MSIX_COUNT;
1140
1141 /* The more we get, the more we will assign to Tx/Rx Cleanup
1142 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1143 * Right now, we simply care about how many we'll get; we'll
1144 * set them up later while requesting irq's.
1145 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001146 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1147 vector_threshold, vectors);
1148 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001149 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001150 kfree(adapter->msix_entries);
1151 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001152 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001153 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001154
1155 /* Adjust for only the vectors we'll use, which is minimum
1156 * of max_msix_q_vectors + NONQ_VECS, or the number of
1157 * vectors we were allocated.
1158 */
1159 adapter->num_msix_vectors = err;
1160 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001161}
1162
1163/**
1164 * i40evf_free_queues - Free memory for all rings
1165 * @adapter: board private structure to initialize
1166 *
1167 * Free all of the memory associated with queue pairs.
1168 **/
1169static void i40evf_free_queues(struct i40evf_adapter *adapter)
1170{
Greg Rose5eae00c2013-12-21 06:12:45 +00001171 if (!adapter->vsi_res)
1172 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001173 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001174 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001175 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001176 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001177 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001178}
1179
1180/**
1181 * i40evf_alloc_queues - Allocate memory for all rings
1182 * @adapter: board private structure to initialize
1183 *
1184 * We allocate one ring per queue at run-time since we don't know the
1185 * number of queues at compile-time. The polling_netdev array is
1186 * intended for Multiqueue, but should work fine with a single queue.
1187 **/
1188static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1189{
Jacob Keller65c70062017-06-07 05:43:01 -04001190 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001191
Jacob Keller65c70062017-06-07 05:43:01 -04001192 num_active_queues = min_t(int,
1193 adapter->vsi_res->num_queue_pairs,
1194 (int)(num_online_cpus()));
1195
1196 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001197 sizeof(struct i40e_ring), GFP_KERNEL);
1198 if (!adapter->tx_rings)
1199 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001200 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001201 sizeof(struct i40e_ring), GFP_KERNEL);
1202 if (!adapter->rx_rings)
1203 goto err_out;
1204
Jacob Keller65c70062017-06-07 05:43:01 -04001205 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001206 struct i40e_ring *tx_ring;
1207 struct i40e_ring *rx_ring;
1208
Mitch Williams0dd438d2015-10-26 19:44:40 -04001209 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001210
1211 tx_ring->queue_index = i;
1212 tx_ring->netdev = adapter->netdev;
1213 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001214 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001215 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001216 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001217 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001218
Mitch Williams0dd438d2015-10-26 19:44:40 -04001219 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001220 rx_ring->queue_index = i;
1221 rx_ring->netdev = adapter->netdev;
1222 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001223 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001224 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001225 }
1226
Jacob Keller65c70062017-06-07 05:43:01 -04001227 adapter->num_active_queues = num_active_queues;
1228
Greg Rose5eae00c2013-12-21 06:12:45 +00001229 return 0;
1230
1231err_out:
1232 i40evf_free_queues(adapter);
1233 return -ENOMEM;
1234}
1235
1236/**
1237 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1238 * @adapter: board private structure to initialize
1239 *
1240 * Attempt to configure the interrupts using the best available
1241 * capabilities of the hardware and the kernel.
1242 **/
1243static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1244{
1245 int vector, v_budget;
1246 int pairs = 0;
1247 int err = 0;
1248
1249 if (!adapter->vsi_res) {
1250 err = -EIO;
1251 goto out;
1252 }
Mitch Williamscc052922014-10-25 03:24:34 +00001253 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001254
Jacob Keller789f38c2017-04-19 09:25:56 -04001255 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1256 * us much good if we have more vectors than CPUs. However, we already
1257 * limit the total number of queues by the number of CPUs so we do not
1258 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001259 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001260 v_budget = min_t(int, pairs + NONQ_VECS,
1261 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001262
Greg Rose5eae00c2013-12-21 06:12:45 +00001263 adapter->msix_entries = kcalloc(v_budget,
1264 sizeof(struct msix_entry), GFP_KERNEL);
1265 if (!adapter->msix_entries) {
1266 err = -ENOMEM;
1267 goto out;
1268 }
1269
1270 for (vector = 0; vector < v_budget; vector++)
1271 adapter->msix_entries[vector].entry = vector;
1272
Mitch Williams313ed2d2015-08-27 11:42:31 -04001273 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001274
1275out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001276 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1277 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001278 return err;
1279}
1280
1281/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001282 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1283 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001284 *
1285 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001286 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001287static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001288{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001289 struct i40e_aqc_get_set_rss_key_data *rss_key =
1290 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001291 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001292 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001293
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001294 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001295 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001296 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001297 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001298 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001299 }
1300
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001301 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1302 if (ret) {
1303 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1304 i40evf_stat_str(hw, ret),
1305 i40evf_aq_str(hw, hw->aq.asq_last_status));
1306 return ret;
1307
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001308 }
1309
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1311 adapter->rss_lut, adapter->rss_lut_size);
1312 if (ret) {
1313 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1314 i40evf_stat_str(hw, ret),
1315 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001316 }
1317
1318 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001319
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001320}
1321
1322/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001323 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001324 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001325 *
1326 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001327 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001328static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001329{
1330 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001331 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001332 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001333
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001334 dw = (u32 *)adapter->rss_key;
1335 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1336 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001337
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001338 dw = (u32 *)adapter->rss_lut;
1339 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1340 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001341
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001342 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001343
1344 return 0;
1345}
1346
1347/**
1348 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001349 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001350 *
1351 * Returns 0 on success, negative on failure
1352 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001353int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001354{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001355
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001356 if (RSS_PF(adapter)) {
1357 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1358 I40EVF_FLAG_AQ_SET_RSS_KEY;
1359 return 0;
1360 } else if (RSS_AQ(adapter)) {
1361 return i40evf_config_rss_aq(adapter);
1362 } else {
1363 return i40evf_config_rss_reg(adapter);
1364 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001365}
1366
1367/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001368 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001369 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001370 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001371static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001372{
1373 u16 i;
1374
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001375 for (i = 0; i < adapter->rss_lut_size; i++)
1376 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001377}
1378
1379/**
Helin Zhang96a81982015-10-26 19:44:31 -04001380 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001381 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001382 *
1383 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001384 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001385static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001386{
1387 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001388 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001389
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001390 if (!RSS_PF(adapter)) {
1391 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001392 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001393 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001394 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1395 else
1396 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001397
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001398 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1399 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1400 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001401
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001402 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001403
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001404 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1405 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001406
1407 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001408}
1409
1410/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001411 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1412 * @adapter: board private structure to initialize
1413 *
1414 * We allocate one q_vector per queue interrupt. If allocation fails we
1415 * return -ENOMEM.
1416 **/
1417static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1418{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001419 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001420 struct i40e_q_vector *q_vector;
1421
1422 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001423 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001424 GFP_KERNEL);
1425 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001426 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001427
1428 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001429 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001430 q_vector->adapter = adapter;
1431 q_vector->vsi = &adapter->vsi;
1432 q_vector->v_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001433 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001434 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001435 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001436 }
1437
1438 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001439}
1440
1441/**
1442 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1443 * @adapter: board private structure to initialize
1444 *
1445 * This function frees the memory allocated to the q_vectors. In addition if
1446 * NAPI is enabled it will delete any references to the NAPI struct prior
1447 * to freeing the q_vector.
1448 **/
1449static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1450{
1451 int q_idx, num_q_vectors;
1452 int napi_vectors;
1453
Jacob Kelleref4603e2016-11-08 13:05:08 -08001454 if (!adapter->q_vectors)
1455 return;
1456
Greg Rose5eae00c2013-12-21 06:12:45 +00001457 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001458 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001459
1460 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001461 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001462 if (q_idx < napi_vectors)
1463 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001464 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001465 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001466 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001467}
1468
1469/**
1470 * i40evf_reset_interrupt_capability - Reset MSIX setup
1471 * @adapter: board private structure
1472 *
1473 **/
1474void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1475{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001476 if (!adapter->msix_entries)
1477 return;
1478
Greg Rose5eae00c2013-12-21 06:12:45 +00001479 pci_disable_msix(adapter->pdev);
1480 kfree(adapter->msix_entries);
1481 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001482}
1483
1484/**
1485 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1486 * @adapter: board private structure to initialize
1487 *
1488 **/
1489int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1490{
1491 int err;
1492
Jacob Keller283aeaf2017-04-19 09:25:59 -04001493 err = i40evf_alloc_queues(adapter);
1494 if (err) {
1495 dev_err(&adapter->pdev->dev,
1496 "Unable to allocate memory for queues\n");
1497 goto err_alloc_queues;
1498 }
1499
Jacob Keller62fe2a82016-07-27 12:02:33 -07001500 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001501 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001502 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001503 if (err) {
1504 dev_err(&adapter->pdev->dev,
1505 "Unable to setup interrupt capabilities\n");
1506 goto err_set_interrupt;
1507 }
1508
1509 err = i40evf_alloc_q_vectors(adapter);
1510 if (err) {
1511 dev_err(&adapter->pdev->dev,
1512 "Unable to allocate memory for queue vectors\n");
1513 goto err_alloc_q_vectors;
1514 }
1515
Greg Rose5eae00c2013-12-21 06:12:45 +00001516 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001517 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1518 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001519
1520 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001521err_alloc_q_vectors:
1522 i40evf_reset_interrupt_capability(adapter);
1523err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001524 i40evf_free_queues(adapter);
1525err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001526 return err;
1527}
1528
1529/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001530 * i40evf_free_rss - Free memory used by RSS structs
1531 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001532 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001533static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001534{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001535 kfree(adapter->rss_key);
1536 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001537
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001538 kfree(adapter->rss_lut);
1539 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001540}
1541
1542/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001543 * i40evf_watchdog_timer - Periodic call-back timer
1544 * @data: pointer to adapter disguised as unsigned long
1545 **/
1546static void i40evf_watchdog_timer(unsigned long data)
1547{
1548 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001549
Greg Rose5eae00c2013-12-21 06:12:45 +00001550 schedule_work(&adapter->watchdog_task);
1551 /* timer will be rescheduled in watchdog task */
1552}
1553
1554/**
1555 * i40evf_watchdog_task - Periodic call-back task
1556 * @work: pointer to work_struct
1557 **/
1558static void i40evf_watchdog_task(struct work_struct *work)
1559{
1560 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001561 struct i40evf_adapter,
1562 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001563 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001564 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001565
Greg Rose5eae00c2013-12-21 06:12:45 +00001566 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001567 goto restart_watchdog;
1568
1569 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001570 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1571 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001572 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1573 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001574 /* A chance for redemption! */
1575 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1576 adapter->state = __I40EVF_STARTUP;
1577 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1578 schedule_delayed_work(&adapter->init_task, 10);
1579 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1580 &adapter->crit_section);
1581 /* Don't reschedule the watchdog, since we've restarted
1582 * the init task. When init_task contacts the PF and
1583 * gets everything set up again, it'll restart the
1584 * watchdog for us. Down, boy. Sit. Stay. Woof.
1585 */
1586 return;
1587 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001588 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001589 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001590 goto watchdog_done;
1591 }
1592
1593 if ((adapter->state < __I40EVF_DOWN) ||
1594 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001595 goto watchdog_done;
1596
Mitch Williamsef8693e2014-02-13 03:48:53 -08001597 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001598 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1599 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001600 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001601 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001602 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001603 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001604 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001605 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001606 goto watchdog_done;
1607 }
1608
1609 /* Process admin queue tasks. After init, everything gets done
1610 * here so we don't race on the admin queue.
1611 */
Mitch Williamsed636962015-04-07 19:45:32 -04001612 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001613 if (!i40evf_asq_done(hw)) {
1614 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1615 i40evf_send_api_ver(adapter);
1616 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001617 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001618 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001619 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1620 i40evf_send_vf_config_msg(adapter);
1621 goto watchdog_done;
1622 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001623
Mitch Williamse284fc82015-03-27 00:12:09 -07001624 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1625 i40evf_disable_queues(adapter);
1626 goto watchdog_done;
1627 }
1628
Greg Rose5eae00c2013-12-21 06:12:45 +00001629 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1630 i40evf_map_queues(adapter);
1631 goto watchdog_done;
1632 }
1633
1634 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1635 i40evf_add_ether_addrs(adapter);
1636 goto watchdog_done;
1637 }
1638
1639 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1640 i40evf_add_vlans(adapter);
1641 goto watchdog_done;
1642 }
1643
1644 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1645 i40evf_del_ether_addrs(adapter);
1646 goto watchdog_done;
1647 }
1648
1649 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1650 i40evf_del_vlans(adapter);
1651 goto watchdog_done;
1652 }
1653
Mariusz Stachura87743702017-07-17 22:09:45 -07001654 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1655 i40evf_enable_vlan_stripping(adapter);
1656 goto watchdog_done;
1657 }
1658
1659 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1660 i40evf_disable_vlan_stripping(adapter);
1661 goto watchdog_done;
1662 }
1663
Greg Rose5eae00c2013-12-21 06:12:45 +00001664 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1665 i40evf_configure_queues(adapter);
1666 goto watchdog_done;
1667 }
1668
1669 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1670 i40evf_enable_queues(adapter);
1671 goto watchdog_done;
1672 }
1673
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001674 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1675 /* This message goes straight to the firmware, not the
1676 * PF, so we don't have to set current_op as we will
1677 * not get a response through the ARQ.
1678 */
Helin Zhang96a81982015-10-26 19:44:31 -04001679 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001680 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1681 goto watchdog_done;
1682 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001683 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1684 i40evf_get_hena(adapter);
1685 goto watchdog_done;
1686 }
1687 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1688 i40evf_set_hena(adapter);
1689 goto watchdog_done;
1690 }
1691 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1692 i40evf_set_rss_key(adapter);
1693 goto watchdog_done;
1694 }
1695 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1696 i40evf_set_rss_lut(adapter);
1697 goto watchdog_done;
1698 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001699
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001700 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001701 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1702 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001703 goto watchdog_done;
1704 }
1705
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001706 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001707 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001708 goto watchdog_done;
1709 }
1710
1711 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1712 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001713 i40evf_set_promiscuous(adapter, 0);
1714 goto watchdog_done;
1715 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001716 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001717
Greg Rose5eae00c2013-12-21 06:12:45 +00001718 if (adapter->state == __I40EVF_RUNNING)
1719 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001720watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001721 if (adapter->state == __I40EVF_RUNNING) {
1722 i40evf_irq_enable_queues(adapter, ~0);
1723 i40evf_fire_sw_int(adapter, 0xFF);
1724 } else {
1725 i40evf_fire_sw_int(adapter, 0x1);
1726 }
1727
Mitch Williamsef8693e2014-02-13 03:48:53 -08001728 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1729restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001730 if (adapter->state == __I40EVF_REMOVE)
1731 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001732 if (adapter->aq_required)
1733 mod_timer(&adapter->watchdog_timer,
1734 jiffies + msecs_to_jiffies(20));
1735 else
1736 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001737 schedule_work(&adapter->adminq_task);
1738}
1739
Joe Perchesdedecb62016-11-01 15:35:14 -07001740static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1741{
1742 struct i40evf_mac_filter *f, *ftmp;
1743 struct i40evf_vlan_filter *fv, *fvtmp;
1744
1745 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1746
1747 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001748 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001749 netif_carrier_off(adapter->netdev);
1750 netif_tx_disable(adapter->netdev);
1751 adapter->link_up = false;
1752 i40evf_napi_disable_all(adapter);
1753 i40evf_irq_disable(adapter);
1754 i40evf_free_traffic_irqs(adapter);
1755 i40evf_free_all_tx_resources(adapter);
1756 i40evf_free_all_rx_resources(adapter);
1757 }
1758
1759 /* Delete all of the filters, both MAC and VLAN. */
1760 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1761 list_del(&f->list);
1762 kfree(f);
1763 }
1764
1765 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1766 list_del(&fv->list);
1767 kfree(fv);
1768 }
1769
1770 i40evf_free_misc_irq(adapter);
1771 i40evf_reset_interrupt_capability(adapter);
1772 i40evf_free_queues(adapter);
1773 i40evf_free_q_vectors(adapter);
1774 kfree(adapter->vf_res);
1775 i40evf_shutdown_adminq(&adapter->hw);
1776 adapter->netdev->flags &= ~IFF_UP;
1777 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1778 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1779 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001780 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001781 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1782}
1783
Mitch Williams67c818a2015-06-19 08:56:30 -07001784#define I40EVF_RESET_WAIT_MS 10
1785#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001786/**
1787 * i40evf_reset_task - Call-back task to handle hardware reset
1788 * @work: pointer to work_struct
1789 *
1790 * During reset we need to shut down and reinitialize the admin queue
1791 * before we can use it to communicate with the PF again. We also clear
1792 * and reinit the rings because that context is lost as well.
1793 **/
1794static void i40evf_reset_task(struct work_struct *work)
1795{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001796 struct i40evf_adapter *adapter = container_of(work,
1797 struct i40evf_adapter,
1798 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001799 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001800 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001801 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001802 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001803 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001804 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001805
Mitch Williamsed0e8942017-01-24 10:23:59 -08001806 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001807 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001808 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001809 if (CLIENT_ENABLED(adapter)) {
1810 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1811 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1812 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1813 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1814 cancel_delayed_work_sync(&adapter->client_task);
1815 i40evf_notify_client_close(&adapter->vsi, true);
1816 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001817 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001818 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001819 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1820 /* Restart the AQ here. If we have been reset but didn't
1821 * detect it, or if the PF had to reinit, our AQ will be hosed.
1822 */
1823 i40evf_shutdown_adminq(hw);
1824 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001825 i40evf_request_reset(adapter);
1826 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001827 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001828
Mitch Williamsef8693e2014-02-13 03:48:53 -08001829 /* poll until we see the reset actually happen */
1830 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001831 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1832 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1833 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001834 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001835 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001836 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001837 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001838 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001839 goto continue_reset; /* act like the reset happened */
1840 }
1841
1842 /* wait until the reset is complete and the PF is responding to us */
1843 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001844 /* sleep first to make sure a minimum wait time is met */
1845 msleep(I40EVF_RESET_WAIT_MS);
1846
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001847 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1848 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001849 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001850 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001851 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001852
Mitch Williams509a4472015-12-23 12:05:52 -08001853 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001854
Mitch Williamsef8693e2014-02-13 03:48:53 -08001855 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001856 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001857 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001858 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001859 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001860 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001861 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001862
1863continue_reset:
Alan Bradyf0db7892017-07-12 05:46:04 -04001864 if (netif_running(netdev)) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001865 netif_carrier_off(netdev);
1866 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001867 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001868 i40evf_napi_disable_all(adapter);
1869 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001870 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001871
Greg Rose5eae00c2013-12-21 06:12:45 +00001872 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001873 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1874
1875 /* free the Tx/Rx rings and descriptors, might be better to just
1876 * re-use them sometime in the future
1877 */
1878 i40evf_free_all_rx_resources(adapter);
1879 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001880
1881 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001882 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001883 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001884 err = i40evf_init_adminq(hw);
1885 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001886 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1887 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001888
Mitch Williamse6d038d2015-06-04 16:23:58 -04001889 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1890 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001891
1892 /* re-add all MAC filters */
1893 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1894 f->add = true;
1895 }
1896 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001897 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1898 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001899 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001900 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001901 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001902 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001903 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001904 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001905
1906 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1907
1908 if (netif_running(adapter->netdev)) {
1909 /* allocate transmit descriptors */
1910 err = i40evf_setup_all_tx_resources(adapter);
1911 if (err)
1912 goto reset_err;
1913
1914 /* allocate receive descriptors */
1915 err = i40evf_setup_all_rx_resources(adapter);
1916 if (err)
1917 goto reset_err;
1918
1919 i40evf_configure(adapter);
1920
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001921 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001922
1923 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001924 } else {
1925 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001926 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001927 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001928
Greg Rose5eae00c2013-12-21 06:12:45 +00001929 return;
1930reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001931 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001932 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001933}
1934
1935/**
1936 * i40evf_adminq_task - worker thread to clean the admin queue
1937 * @work: pointer to work_struct containing our data
1938 **/
1939static void i40evf_adminq_task(struct work_struct *work)
1940{
1941 struct i40evf_adapter *adapter =
1942 container_of(work, struct i40evf_adapter, adminq_task);
1943 struct i40e_hw *hw = &adapter->hw;
1944 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001945 enum virtchnl_ops v_op;
1946 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001947 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001948 u16 pending;
1949
Mitch Williamsef8693e2014-02-13 03:48:53 -08001950 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001951 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001952
Mitch Williams1001dc32014-11-11 20:02:19 +00001953 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1954 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001955 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001956 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001957
Greg Rose5eae00c2013-12-21 06:12:45 +00001958 do {
1959 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07001960 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
1961 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
1962
1963 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00001964 break; /* No event to process or error cleaning ARQ */
1965
Tushar Davec969ef42017-06-22 09:44:32 -07001966 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001967 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001968 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001969 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001970 } while (pending);
1971
Mitch Williams67c818a2015-06-19 08:56:30 -07001972 if ((adapter->flags &
1973 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1974 adapter->state == __I40EVF_RESETTING)
1975 goto freedom;
1976
Mitch Williams912257e2014-05-22 06:32:07 +00001977 /* check for error indications */
1978 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001979 if (val == 0xdeadbeef) /* indicates device in reset */
1980 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001981 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001982 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001983 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001984 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001985 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001986 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001987 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001988 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001989 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001990 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001991 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001992 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001993 }
1994 if (oldval != val)
1995 wr32(hw, hw->aq.arq.len, val);
1996
1997 val = rd32(hw, hw->aq.asq.len);
1998 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001999 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002000 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002001 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002002 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002003 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002004 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002005 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002006 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002007 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002008 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002009 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002010 }
2011 if (oldval != val)
2012 wr32(hw, hw->aq.asq.len, val);
2013
Mitch Williams67c818a2015-06-19 08:56:30 -07002014freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002015 kfree(event.msg_buf);
2016out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002017 /* re-enable Admin queue interrupt cause */
2018 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002019}
2020
2021/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002022 * i40evf_client_task - worker thread to perform client work
2023 * @work: pointer to work_struct containing our data
2024 *
2025 * This task handles client interactions. Because client calls can be
2026 * reentrant, we can't handle them in the watchdog.
2027 **/
2028static void i40evf_client_task(struct work_struct *work)
2029{
2030 struct i40evf_adapter *adapter =
2031 container_of(work, struct i40evf_adapter, client_task.work);
2032
2033 /* If we can't get the client bit, just give up. We'll be rescheduled
2034 * later.
2035 */
2036
2037 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2038 return;
2039
2040 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2041 i40evf_client_subtask(adapter);
2042 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2043 goto out;
2044 }
2045 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2046 i40evf_notify_client_close(&adapter->vsi, false);
2047 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2048 goto out;
2049 }
2050 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2051 i40evf_notify_client_open(&adapter->vsi);
2052 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2053 goto out;
2054 }
2055 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2056 i40evf_notify_client_l2_params(&adapter->vsi);
2057 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2058 }
2059out:
2060 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2061}
2062
2063/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002064 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2065 * @adapter: board private structure
2066 *
2067 * Free all transmit software resources
2068 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002069void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002070{
2071 int i;
2072
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002073 if (!adapter->tx_rings)
2074 return;
2075
Mitch Williamscc052922014-10-25 03:24:34 +00002076 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002077 if (adapter->tx_rings[i].desc)
2078 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002079}
2080
2081/**
2082 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2083 * @adapter: board private structure
2084 *
2085 * If this function returns with an error, then it's possible one or
2086 * more of the rings is populated (while the rest are not). It is the
2087 * callers duty to clean those orphaned rings.
2088 *
2089 * Return 0 on success, negative on failure
2090 **/
2091static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2092{
2093 int i, err = 0;
2094
Mitch Williamscc052922014-10-25 03:24:34 +00002095 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002096 adapter->tx_rings[i].count = adapter->tx_desc_count;
2097 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002098 if (!err)
2099 continue;
2100 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002101 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002102 break;
2103 }
2104
2105 return err;
2106}
2107
2108/**
2109 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2110 * @adapter: board private structure
2111 *
2112 * If this function returns with an error, then it's possible one or
2113 * more of the rings is populated (while the rest are not). It is the
2114 * callers duty to clean those orphaned rings.
2115 *
2116 * Return 0 on success, negative on failure
2117 **/
2118static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2119{
2120 int i, err = 0;
2121
Mitch Williamscc052922014-10-25 03:24:34 +00002122 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002123 adapter->rx_rings[i].count = adapter->rx_desc_count;
2124 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002125 if (!err)
2126 continue;
2127 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002128 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002129 break;
2130 }
2131 return err;
2132}
2133
2134/**
2135 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2136 * @adapter: board private structure
2137 *
2138 * Free all receive software resources
2139 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002140void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002141{
2142 int i;
2143
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002144 if (!adapter->rx_rings)
2145 return;
2146
Mitch Williamscc052922014-10-25 03:24:34 +00002147 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002148 if (adapter->rx_rings[i].desc)
2149 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002150}
2151
2152/**
2153 * i40evf_open - Called when a network interface is made active
2154 * @netdev: network interface device structure
2155 *
2156 * Returns 0 on success, negative value on failure
2157 *
2158 * The open entry point is called when a network interface is made
2159 * active by the system (IFF_UP). At this point all resources needed
2160 * for transmit and receive operations are allocated, the interrupt
2161 * handler is registered with the OS, the watchdog timer is started,
2162 * and the stack is notified that the interface is ready.
2163 **/
2164static int i40evf_open(struct net_device *netdev)
2165{
2166 struct i40evf_adapter *adapter = netdev_priv(netdev);
2167 int err;
2168
Mitch Williamsef8693e2014-02-13 03:48:53 -08002169 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2170 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2171 return -EIO;
2172 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002173
2174 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002175 return -EBUSY;
2176
2177 /* allocate transmit descriptors */
2178 err = i40evf_setup_all_tx_resources(adapter);
2179 if (err)
2180 goto err_setup_tx;
2181
2182 /* allocate receive descriptors */
2183 err = i40evf_setup_all_rx_resources(adapter);
2184 if (err)
2185 goto err_setup_rx;
2186
2187 /* clear any pending interrupts, may auto mask */
2188 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2189 if (err)
2190 goto err_req_irq;
2191
Mitch Williams44151cd2015-04-27 14:57:17 -04002192 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002193 i40evf_configure(adapter);
2194
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002195 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002196
2197 i40evf_irq_enable(adapter, true);
2198
2199 return 0;
2200
2201err_req_irq:
2202 i40evf_down(adapter);
2203 i40evf_free_traffic_irqs(adapter);
2204err_setup_rx:
2205 i40evf_free_all_rx_resources(adapter);
2206err_setup_tx:
2207 i40evf_free_all_tx_resources(adapter);
2208
2209 return err;
2210}
2211
2212/**
2213 * i40evf_close - Disables a network interface
2214 * @netdev: network interface device structure
2215 *
2216 * Returns 0, this is not allowed to fail
2217 *
2218 * The close entry point is called when an interface is de-activated
2219 * by the OS. The hardware is still under the drivers control, but
2220 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2221 * are freed, along with all transmit and receive resources.
2222 **/
2223static int i40evf_close(struct net_device *netdev)
2224{
2225 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002226 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002227
Mitch Williams209dc4d2015-12-09 15:50:27 -08002228 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002229 return 0;
2230
Mitch Williamsef8693e2014-02-13 03:48:53 -08002231
Jacob Keller0da36b92017-04-19 09:25:55 -04002232 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002233 if (CLIENT_ENABLED(adapter))
2234 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002235
2236 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002237 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002238 i40evf_free_traffic_irqs(adapter);
2239
Mitch Williams51f38262016-12-12 15:44:11 -08002240 /* We explicitly don't free resources here because the hardware is
2241 * still active and can DMA into memory. Resources are cleared in
2242 * i40evf_virtchnl_completion() after we get confirmation from the PF
2243 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002244 *
2245 * Also, we wait for state to transition to __I40EVF_DOWN before
2246 * returning. State change occurs in i40evf_virtchnl_completion() after
2247 * VF resources are released (which occurs after PF driver processes and
2248 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002249 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002250
2251 status = wait_event_timeout(adapter->down_waitqueue,
2252 adapter->state == __I40EVF_DOWN,
2253 msecs_to_jiffies(200));
2254 if (!status)
2255 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002256 return 0;
2257}
2258
2259/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002260 * i40evf_change_mtu - Change the Maximum Transfer Unit
2261 * @netdev: network interface device structure
2262 * @new_mtu: new value for maximum frame size
2263 *
2264 * Returns 0 on success, negative on failure
2265 **/
2266static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2267{
2268 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002269
Greg Rose5eae00c2013-12-21 06:12:45 +00002270 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002271 if (CLIENT_ENABLED(adapter)) {
2272 i40evf_notify_client_l2_params(&adapter->vsi);
2273 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2274 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002275 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2276 schedule_work(&adapter->reset_task);
2277
Greg Rose5eae00c2013-12-21 06:12:45 +00002278 return 0;
2279}
2280
Alexander Duyck06fc0162016-10-25 16:08:47 -07002281/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002282 * i40e_set_features - set the netdev feature flags
2283 * @netdev: ptr to the netdev being adjusted
2284 * @features: the feature set that the stack is suggesting
2285 * Note: expects to be called while under rtnl_lock()
2286 **/
2287static int i40evf_set_features(struct net_device *netdev,
2288 netdev_features_t features)
2289{
2290 struct i40evf_adapter *adapter = netdev_priv(netdev);
2291
2292 if (!VLAN_ALLOWED(adapter))
2293 return -EINVAL;
2294
2295 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2296 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2297 else
2298 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2299
2300 return 0;
2301}
2302
2303/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002304 * i40evf_features_check - Validate encapsulated packet conforms to limits
2305 * @skb: skb buff
2306 * @netdev: This physical port's netdev
2307 * @features: Offload features that the stack believes apply
2308 **/
2309static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2310 struct net_device *dev,
2311 netdev_features_t features)
2312{
2313 size_t len;
2314
2315 /* No point in doing any of this if neither checksum nor GSO are
2316 * being requested for this frame. We can rule out both by just
2317 * checking for CHECKSUM_PARTIAL
2318 */
2319 if (skb->ip_summed != CHECKSUM_PARTIAL)
2320 return features;
2321
2322 /* We cannot support GSO if the MSS is going to be less than
2323 * 64 bytes. If it is then we need to drop support for GSO.
2324 */
2325 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2326 features &= ~NETIF_F_GSO_MASK;
2327
2328 /* MACLEN can support at most 63 words */
2329 len = skb_network_header(skb) - skb->data;
2330 if (len & ~(63 * 2))
2331 goto out_err;
2332
2333 /* IPLEN and EIPLEN can support at most 127 dwords */
2334 len = skb_transport_header(skb) - skb_network_header(skb);
2335 if (len & ~(127 * 4))
2336 goto out_err;
2337
2338 if (skb->encapsulation) {
2339 /* L4TUNLEN can support 127 words */
2340 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2341 if (len & ~(127 * 2))
2342 goto out_err;
2343
2344 /* IPLEN can support at most 127 dwords */
2345 len = skb_inner_transport_header(skb) -
2346 skb_inner_network_header(skb);
2347 if (len & ~(127 * 4))
2348 goto out_err;
2349 }
2350
2351 /* No need to validate L4LEN as TCP is the only protocol with a
2352 * a flexible value and we support all possible values supported
2353 * by TCP, which is at most 15 dwords
2354 */
2355
2356 return features;
2357out_err:
2358 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2359}
2360
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002361#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2362 NETIF_F_HW_VLAN_CTAG_RX |\
2363 NETIF_F_HW_VLAN_CTAG_FILTER)
2364
2365/**
2366 * i40evf_fix_features - fix up the netdev feature bits
2367 * @netdev: our net device
2368 * @features: desired feature bits
2369 *
2370 * Returns fixed-up features bits
2371 **/
2372static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2373 netdev_features_t features)
2374{
2375 struct i40evf_adapter *adapter = netdev_priv(netdev);
2376
2377 features &= ~I40EVF_VLAN_FEATURES;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002378 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002379 features |= I40EVF_VLAN_FEATURES;
2380 return features;
2381}
2382
Greg Rose5eae00c2013-12-21 06:12:45 +00002383static const struct net_device_ops i40evf_netdev_ops = {
2384 .ndo_open = i40evf_open,
2385 .ndo_stop = i40evf_close,
2386 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002387 .ndo_set_rx_mode = i40evf_set_rx_mode,
2388 .ndo_validate_addr = eth_validate_addr,
2389 .ndo_set_mac_address = i40evf_set_mac,
2390 .ndo_change_mtu = i40evf_change_mtu,
2391 .ndo_tx_timeout = i40evf_tx_timeout,
2392 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2393 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002394 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002395 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002396 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002397#ifdef CONFIG_NET_POLL_CONTROLLER
2398 .ndo_poll_controller = i40evf_netpoll,
2399#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002400};
2401
2402/**
2403 * i40evf_check_reset_complete - check that VF reset is complete
2404 * @hw: pointer to hw struct
2405 *
2406 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2407 **/
2408static int i40evf_check_reset_complete(struct i40e_hw *hw)
2409{
2410 u32 rstat;
2411 int i;
2412
2413 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002414 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2415 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002416 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2417 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002418 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002419 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002420 }
2421 return -EBUSY;
2422}
2423
2424/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002425 * i40evf_process_config - Process the config information we got from the PF
2426 * @adapter: board private structure
2427 *
2428 * Verify that we have a valid config struct, and set up our netdev features
2429 * and our VSI struct.
2430 **/
2431int i40evf_process_config(struct i40evf_adapter *adapter)
2432{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002433 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002434 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002435 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002436 int i;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002437 netdev_features_t hw_enc_features;
2438 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002439
2440 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002441 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002442 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002443 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002444 }
2445 if (!adapter->vsi_res) {
2446 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2447 return -ENODEV;
2448 }
2449
Preethi Banalabacd75c2017-03-27 14:43:18 -07002450 hw_enc_features = NETIF_F_SG |
2451 NETIF_F_IP_CSUM |
2452 NETIF_F_IPV6_CSUM |
2453 NETIF_F_HIGHDMA |
2454 NETIF_F_SOFT_FEATURES |
2455 NETIF_F_TSO |
2456 NETIF_F_TSO_ECN |
2457 NETIF_F_TSO6 |
2458 NETIF_F_SCTP_CRC |
2459 NETIF_F_RXHASH |
2460 NETIF_F_RXCSUM |
2461 0;
2462
2463 /* advertise to stack only if offloads for encapsulated packets is
2464 * supported
2465 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002466 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002467 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002468 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002469 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002470 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002471 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002472 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002473 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002474 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002475
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002476 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002477 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002478 netdev->gso_partial_features |=
2479 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002480
Preethi Banalabacd75c2017-03-27 14:43:18 -07002481 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2482 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2483 netdev->hw_enc_features |= hw_enc_features;
2484 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002485 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002486 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002487
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002488 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002489 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002490 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002491 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002492
Preethi Banalabacd75c2017-03-27 14:43:18 -07002493 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002494
Preethi Banalabacd75c2017-03-27 14:43:18 -07002495 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002496
2497 adapter->vsi.id = adapter->vsi_res->vsi_id;
2498
2499 adapter->vsi.back = adapter;
2500 adapter->vsi.base_vector = 1;
2501 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002502 vsi->netdev = adapter->netdev;
2503 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002504 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002505 adapter->rss_key_size = vfres->rss_key_size;
2506 adapter->rss_lut_size = vfres->rss_lut_size;
2507 } else {
2508 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2509 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2510 }
2511
Mitch Williamse6d038d2015-06-04 16:23:58 -04002512 return 0;
2513}
2514
2515/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002516 * i40evf_init_task - worker thread to perform delayed initialization
2517 * @work: pointer to work_struct containing our data
2518 *
2519 * This task completes the work that was begun in probe. Due to the nature
2520 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002521 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002522 * whole system, we'll do it in a task so we can sleep.
2523 * This task only runs during driver init. Once we've established
2524 * communications with the PF driver and set up our netdev, the watchdog
2525 * takes over.
2526 **/
2527static void i40evf_init_task(struct work_struct *work)
2528{
2529 struct i40evf_adapter *adapter = container_of(work,
2530 struct i40evf_adapter,
2531 init_task.work);
2532 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002533 struct i40e_hw *hw = &adapter->hw;
2534 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002535 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002536
2537 switch (adapter->state) {
2538 case __I40EVF_STARTUP:
2539 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002540 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2541 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002542 err = i40e_set_mac_type(hw);
2543 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002544 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2545 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002546 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002547 }
2548 err = i40evf_check_reset_complete(hw);
2549 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002550 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002551 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002552 goto err;
2553 }
2554 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2555 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2556 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2557 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2558
2559 err = i40evf_init_adminq(hw);
2560 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002561 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2562 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002563 goto err;
2564 }
2565 err = i40evf_send_api_ver(adapter);
2566 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002567 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002568 i40evf_shutdown_adminq(hw);
2569 goto err;
2570 }
2571 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2572 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002573 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002574 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002575 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002576 i40evf_shutdown_adminq(hw);
2577 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002578 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002579 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002580
2581 /* aq msg sent, awaiting reply */
2582 err = i40evf_verify_api_ver(adapter);
2583 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002584 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002585 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002586 else
2587 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2588 adapter->pf_version.major,
2589 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002590 VIRTCHNL_VERSION_MAJOR,
2591 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002592 goto err;
2593 }
2594 err = i40evf_send_vf_config_msg(adapter);
2595 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002596 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002597 err);
2598 goto err;
2599 }
2600 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2601 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002602 case __I40EVF_INIT_GET_RESOURCES:
2603 /* aq msg sent, awaiting reply */
2604 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002605 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002606 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002607 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002608 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002609 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002610 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002611 }
2612 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002613 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002614 err = i40evf_send_vf_config_msg(adapter);
2615 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002616 } else if (err == I40E_ERR_PARAM) {
2617 /* We only get ERR_PARAM if the device is in a very bad
2618 * state or if we've been disabled for previous bad
2619 * behavior. Either way, we're done now.
2620 */
2621 i40evf_shutdown_adminq(hw);
2622 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2623 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002624 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002625 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002626 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2627 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002628 goto err_alloc;
2629 }
2630 adapter->state = __I40EVF_INIT_SW;
2631 break;
2632 default:
2633 goto err_alloc;
2634 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002635
Mitch Williamse6d038d2015-06-04 16:23:58 -04002636 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002637 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002638 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002639
2640 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2641
Greg Rose5eae00c2013-12-21 06:12:45 +00002642 netdev->netdev_ops = &i40evf_netdev_ops;
2643 i40evf_set_ethtool_ops(netdev);
2644 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002645
Jarod Wilson91c527a2016-10-17 15:54:05 -04002646 /* MTU range: 68 - 9710 */
2647 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002648 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002649
Greg Rose5eae00c2013-12-21 06:12:45 +00002650 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002651 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002652 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002653 eth_hw_addr_random(netdev);
2654 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2655 } else {
2656 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2657 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2658 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002659 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002660
Allen Pais7d8fb3a2017-09-21 22:35:20 +05302661 setup_timer(&adapter->watchdog_timer, &i40evf_watchdog_timer,
2662 (unsigned long)adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002663 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2664
Mitch Williamsd732a182014-04-24 06:41:37 +00002665 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2666 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002667 err = i40evf_init_interrupt_scheme(adapter);
2668 if (err)
2669 goto err_sw_init;
2670 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002671 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002672 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002673 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2674
Greg Rose5eae00c2013-12-21 06:12:45 +00002675 err = i40evf_request_misc_irq(adapter);
2676 if (err)
2677 goto err_sw_init;
2678
2679 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002680 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002681
Mitch Williamsef8693e2014-02-13 03:48:53 -08002682 if (!adapter->netdev_registered) {
2683 err = register_netdev(netdev);
2684 if (err)
2685 goto err_register;
2686 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002687
2688 adapter->netdev_registered = true;
2689
2690 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002691 if (CLIENT_ALLOWED(adapter)) {
2692 err = i40evf_lan_add_device(adapter);
2693 if (err)
2694 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2695 err);
2696 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002697
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002698 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002699 if (netdev->features & NETIF_F_GRO)
2700 dev_info(&pdev->dev, "GRO is enabled\n");
2701
Greg Rose5eae00c2013-12-21 06:12:45 +00002702 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002703 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002704 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002705 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002706
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002707 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2708 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2709 if (!adapter->rss_key || !adapter->rss_lut)
2710 goto err_mem;
2711
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002712 if (RSS_AQ(adapter)) {
2713 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2714 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2715 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002716 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002717 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002718 return;
2719restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002720 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002721 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002722err_mem:
2723 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002724err_register:
2725 i40evf_free_misc_irq(adapter);
2726err_sw_init:
2727 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002728err_alloc:
2729 kfree(adapter->vf_res);
2730 adapter->vf_res = NULL;
2731err:
2732 /* Things went into the weeds, so try again later */
2733 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002734 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002735 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002736 i40evf_shutdown_adminq(hw);
2737 adapter->state = __I40EVF_STARTUP;
2738 schedule_delayed_work(&adapter->init_task, HZ * 5);
2739 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002740 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002741 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002742}
2743
2744/**
2745 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2746 * @pdev: pci device structure
2747 **/
2748static void i40evf_shutdown(struct pci_dev *pdev)
2749{
2750 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002751 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002752
2753 netif_device_detach(netdev);
2754
2755 if (netif_running(netdev))
2756 i40evf_close(netdev);
2757
Mitch Williams00293fd2015-01-09 11:18:18 +00002758 /* Prevent the watchdog from running. */
2759 adapter->state = __I40EVF_REMOVE;
2760 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002761
Greg Rose5eae00c2013-12-21 06:12:45 +00002762#ifdef CONFIG_PM
2763 pci_save_state(pdev);
2764
2765#endif
2766 pci_disable_device(pdev);
2767}
2768
2769/**
2770 * i40evf_probe - Device Initialization Routine
2771 * @pdev: PCI device information struct
2772 * @ent: entry in i40evf_pci_tbl
2773 *
2774 * Returns 0 on success, negative on failure
2775 *
2776 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2777 * The OS initialization, configuring of the adapter private structure,
2778 * and a hardware reset occur.
2779 **/
2780static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2781{
2782 struct net_device *netdev;
2783 struct i40evf_adapter *adapter = NULL;
2784 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002785 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002786
2787 err = pci_enable_device(pdev);
2788 if (err)
2789 return err;
2790
Mitch Williams64942942014-02-11 08:26:33 +00002791 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002792 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002793 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2794 if (err) {
2795 dev_err(&pdev->dev,
2796 "DMA configuration failed: 0x%x\n", err);
2797 goto err_dma;
2798 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002799 }
2800
2801 err = pci_request_regions(pdev, i40evf_driver_name);
2802 if (err) {
2803 dev_err(&pdev->dev,
2804 "pci_request_regions failed 0x%x\n", err);
2805 goto err_pci_reg;
2806 }
2807
2808 pci_enable_pcie_error_reporting(pdev);
2809
2810 pci_set_master(pdev);
2811
Mitch Williams1255b7a2015-11-06 15:25:59 -08002812 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002813 if (!netdev) {
2814 err = -ENOMEM;
2815 goto err_alloc_etherdev;
2816 }
2817
2818 SET_NETDEV_DEV(netdev, &pdev->dev);
2819
2820 pci_set_drvdata(pdev, netdev);
2821 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002822
2823 adapter->netdev = netdev;
2824 adapter->pdev = pdev;
2825
2826 hw = &adapter->hw;
2827 hw->back = adapter;
2828
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002829 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002830 adapter->state = __I40EVF_STARTUP;
2831
2832 /* Call save state here because it relies on the adapter struct. */
2833 pci_save_state(pdev);
2834
2835 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2836 pci_resource_len(pdev, 0));
2837 if (!hw->hw_addr) {
2838 err = -EIO;
2839 goto err_ioremap;
2840 }
2841 hw->vendor_id = pdev->vendor;
2842 hw->device_id = pdev->device;
2843 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2844 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2845 hw->subsystem_device_id = pdev->subsystem_device;
2846 hw->bus.device = PCI_SLOT(pdev->devfn);
2847 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002848 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002849
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002850 /* set up the locks for the AQ, do this only once in probe
2851 * and destroy them only once in remove
2852 */
2853 mutex_init(&hw->aq.asq_mutex);
2854 mutex_init(&hw->aq.arq_mutex);
2855
Serey Kong8bb1a542014-08-01 13:27:15 -07002856 INIT_LIST_HEAD(&adapter->mac_filter_list);
2857 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2858
Greg Rose5eae00c2013-12-21 06:12:45 +00002859 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2860 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2861 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002862 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002863 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002864 schedule_delayed_work(&adapter->init_task,
2865 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002866
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002867 /* Setup the wait queue for indicating transition to down status */
2868 init_waitqueue_head(&adapter->down_waitqueue);
2869
Greg Rose5eae00c2013-12-21 06:12:45 +00002870 return 0;
2871
2872err_ioremap:
2873 free_netdev(netdev);
2874err_alloc_etherdev:
2875 pci_release_regions(pdev);
2876err_pci_reg:
2877err_dma:
2878 pci_disable_device(pdev);
2879 return err;
2880}
2881
2882#ifdef CONFIG_PM
2883/**
2884 * i40evf_suspend - Power management suspend routine
2885 * @pdev: PCI device information struct
2886 * @state: unused
2887 *
2888 * Called when the system (VM) is entering sleep/suspend.
2889 **/
2890static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2891{
2892 struct net_device *netdev = pci_get_drvdata(pdev);
2893 struct i40evf_adapter *adapter = netdev_priv(netdev);
2894 int retval = 0;
2895
2896 netif_device_detach(netdev);
2897
2898 if (netif_running(netdev)) {
2899 rtnl_lock();
2900 i40evf_down(adapter);
2901 rtnl_unlock();
2902 }
2903 i40evf_free_misc_irq(adapter);
2904 i40evf_reset_interrupt_capability(adapter);
2905
2906 retval = pci_save_state(pdev);
2907 if (retval)
2908 return retval;
2909
2910 pci_disable_device(pdev);
2911
2912 return 0;
2913}
2914
2915/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002916 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002917 * @pdev: PCI device information struct
2918 *
2919 * Called when the system (VM) is resumed from sleep/suspend.
2920 **/
2921static int i40evf_resume(struct pci_dev *pdev)
2922{
2923 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2924 struct net_device *netdev = adapter->netdev;
2925 u32 err;
2926
2927 pci_set_power_state(pdev, PCI_D0);
2928 pci_restore_state(pdev);
2929 /* pci_restore_state clears dev->state_saved so call
2930 * pci_save_state to restore it.
2931 */
2932 pci_save_state(pdev);
2933
2934 err = pci_enable_device_mem(pdev);
2935 if (err) {
2936 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2937 return err;
2938 }
2939 pci_set_master(pdev);
2940
2941 rtnl_lock();
2942 err = i40evf_set_interrupt_capability(adapter);
2943 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002944 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002945 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2946 return err;
2947 }
2948 err = i40evf_request_misc_irq(adapter);
2949 rtnl_unlock();
2950 if (err) {
2951 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2952 return err;
2953 }
2954
2955 schedule_work(&adapter->reset_task);
2956
2957 netif_device_attach(netdev);
2958
2959 return err;
2960}
2961
2962#endif /* CONFIG_PM */
2963/**
2964 * i40evf_remove - Device Removal Routine
2965 * @pdev: PCI device information struct
2966 *
2967 * i40evf_remove is called by the PCI subsystem to alert the driver
2968 * that it should release a PCI device. The could be caused by a
2969 * Hot-Plug event, or because the driver is going to be removed from
2970 * memory.
2971 **/
2972static void i40evf_remove(struct pci_dev *pdev)
2973{
2974 struct net_device *netdev = pci_get_drvdata(pdev);
2975 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002976 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002977 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002978 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002979
2980 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002981 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002982 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002983 if (adapter->netdev_registered) {
2984 unregister_netdev(netdev);
2985 adapter->netdev_registered = false;
2986 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002987 if (CLIENT_ALLOWED(adapter)) {
2988 err = i40evf_lan_del_device(adapter);
2989 if (err)
2990 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
2991 err);
2992 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002993
Mitch Williamsf4a71882015-01-09 11:18:16 +00002994 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002995 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002996 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002997 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002998 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002999 /* If the FW isn't responding, kick it once, but only once. */
3000 if (!i40evf_asq_done(hw)) {
3001 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003002 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003003 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003004 i40evf_free_all_tx_resources(adapter);
3005 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003006 i40evf_misc_irq_disable(adapter);
3007 i40evf_free_misc_irq(adapter);
3008 i40evf_reset_interrupt_capability(adapter);
3009 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003010
Mitch Williamse5d17c32014-06-04 04:22:38 +00003011 if (adapter->watchdog_timer.function)
3012 del_timer_sync(&adapter->watchdog_timer);
3013
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003014 flush_scheduled_work();
3015
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003016 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003017
Greg Rose5eae00c2013-12-21 06:12:45 +00003018 if (hw->aq.asq.count)
3019 i40evf_shutdown_adminq(hw);
3020
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003021 /* destroy the locks only once, here */
3022 mutex_destroy(&hw->aq.arq_mutex);
3023 mutex_destroy(&hw->aq.asq_mutex);
3024
Greg Rose5eae00c2013-12-21 06:12:45 +00003025 iounmap(hw->hw_addr);
3026 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003027 i40evf_free_all_tx_resources(adapter);
3028 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003029 i40evf_free_queues(adapter);
3030 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003031 /* If we got removed before an up/down sequence, we've got a filter
3032 * hanging out there that we need to get rid of.
3033 */
3034 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3035 list_del(&f->list);
3036 kfree(f);
3037 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003038 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3039 list_del(&f->list);
3040 kfree(f);
3041 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003042
3043 free_netdev(netdev);
3044
3045 pci_disable_pcie_error_reporting(pdev);
3046
3047 pci_disable_device(pdev);
3048}
3049
3050static struct pci_driver i40evf_driver = {
3051 .name = i40evf_driver_name,
3052 .id_table = i40evf_pci_tbl,
3053 .probe = i40evf_probe,
3054 .remove = i40evf_remove,
3055#ifdef CONFIG_PM
3056 .suspend = i40evf_suspend,
3057 .resume = i40evf_resume,
3058#endif
3059 .shutdown = i40evf_shutdown,
3060};
3061
3062/**
3063 * i40e_init_module - Driver Registration Routine
3064 *
3065 * i40e_init_module is the first routine called when the driver is
3066 * loaded. All it does is register with the PCI subsystem.
3067 **/
3068static int __init i40evf_init_module(void)
3069{
3070 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003071
Greg Rose5eae00c2013-12-21 06:12:45 +00003072 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003073 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003074
3075 pr_info("%s\n", i40evf_copyright);
3076
Jacob Keller6992a6c2016-08-04 11:37:01 -07003077 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3078 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003079 if (!i40evf_wq) {
3080 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3081 return -ENOMEM;
3082 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003083 ret = pci_register_driver(&i40evf_driver);
3084 return ret;
3085}
3086
3087module_init(i40evf_init_module);
3088
3089/**
3090 * i40e_exit_module - Driver Exit Cleanup Routine
3091 *
3092 * i40e_exit_module is called just before the driver is removed
3093 * from memory.
3094 **/
3095static void __exit i40evf_exit_module(void)
3096{
3097 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003098 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003099}
3100
3101module_exit(i40evf_exit_module);
3102
3103/* i40evf_main.c */