blob: 0b23bf6d7873b49eb3bfaaaeba50e9e6709149ab [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 **/
Mitch Williams1b7b7592017-08-22 06:57:51 -0400433static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +0000434{
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
439 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
440
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400441 for (; ridx < rings_remaining; ridx++) {
442 i40evf_map_vector_to_rxq(adapter, vidx, ridx);
443 i40evf_map_vector_to_txq(adapter, vidx, ridx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000444
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400445 /* In the case where we have more queues than vectors, continue
446 * round-robin on vectors until all queues are mapped.
447 */
448 if (++vidx >= q_vectors)
449 vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000450 }
451
Greg Rose5eae00c2013-12-21 06:12:45 +0000452 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Greg Rose5eae00c2013-12-21 06:12:45 +0000453}
454
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700455#ifdef CONFIG_NET_POLL_CONTROLLER
456/**
457 * i40evf_netpoll - A Polling 'interrupt' handler
458 * @netdev: network interface device structure
459 *
460 * This is used by netconsole to send skbs without having to re-enable
461 * interrupts. It's not called while the normal interrupt routine is executing.
462 **/
463static void i40evf_netpoll(struct net_device *netdev)
464{
465 struct i40evf_adapter *adapter = netdev_priv(netdev);
466 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
467 int i;
468
469 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400470 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700471 return;
472
473 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400474 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700475}
476
477#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000478/**
Alan Brady96db7762016-09-14 16:24:38 -0700479 * i40evf_irq_affinity_notify - Callback for affinity changes
480 * @notify: context as to what irq was changed
481 * @mask: the new affinity mask
482 *
483 * This is a callback function used by the irq_set_affinity_notifier function
484 * so that we may register to receive changes to the irq affinity masks.
485 **/
486static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
487 const cpumask_t *mask)
488{
489 struct i40e_q_vector *q_vector =
490 container_of(notify, struct i40e_q_vector, affinity_notify);
491
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400492 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700493}
494
495/**
496 * i40evf_irq_affinity_release - Callback for affinity notifier release
497 * @ref: internal core kernel usage
498 *
499 * This is a callback function used by the irq_set_affinity_notifier function
500 * to inform the current notification subscriber that they will no longer
501 * receive notifications.
502 **/
503static void i40evf_irq_affinity_release(struct kref *ref) {}
504
505/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000506 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
507 * @adapter: board private structure
508 *
509 * Allocates MSI-X vectors for tx and rx handling, and requests
510 * interrupts from the kernel.
511 **/
512static int
513i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
514{
Jacob Keller696ac80a2017-07-12 05:46:11 -0400515 unsigned int vector, q_vectors;
516 unsigned int rx_int_idx = 0, tx_int_idx = 0;
517 int irq_num, err;
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400518 int cpu;
Greg Rose5eae00c2013-12-21 06:12:45 +0000519
520 i40evf_irq_disable(adapter);
521 /* Decrement for Other and TCP Timer vectors */
522 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
523
524 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400525 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700526 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000527
528 if (q_vector->tx.ring && q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400529 snprintf(q_vector->name, sizeof(q_vector->name),
530 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000531 tx_int_idx++;
532 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400533 snprintf(q_vector->name, sizeof(q_vector->name),
534 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000535 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400536 snprintf(q_vector->name, sizeof(q_vector->name),
537 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000538 } else {
539 /* skip this unused q_vector */
540 continue;
541 }
Alan Brady96db7762016-09-14 16:24:38 -0700542 err = request_irq(irq_num,
543 i40evf_msix_clean_rings,
544 0,
545 q_vector->name,
546 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000547 if (err) {
548 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400549 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000550 goto free_queue_irqs;
551 }
Alan Brady96db7762016-09-14 16:24:38 -0700552 /* register for affinity change notifications */
553 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
554 q_vector->affinity_notify.release =
555 i40evf_irq_affinity_release;
556 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400557 /* Spread the IRQ affinity hints across online CPUs. Note that
558 * get_cpu_mask returns a mask with a permanent lifetime so
559 * it's safe to use as a hint for irq_set_affinity_hint.
Jacob Keller759dc4a2017-07-14 09:10:10 -0400560 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400561 cpu = cpumask_local_spread(q_vector->v_idx, -1);
562 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
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;
Alan Bradyc766b9a2017-09-07 08:05:47 -0400883 } else {
884 f->remove = false;
Greg Rose5eae00c2013-12-21 06:12:45 +0000885 }
886
887 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
888 return f;
889}
890
891/**
892 * i40evf_set_mac - NDO callback to set port mac address
893 * @netdev: network interface device structure
894 * @p: pointer to an address structure
895 *
896 * Returns 0 on success, negative on failure
897 **/
898static int i40evf_set_mac(struct net_device *netdev, void *p)
899{
900 struct i40evf_adapter *adapter = netdev_priv(netdev);
901 struct i40e_hw *hw = &adapter->hw;
902 struct i40evf_mac_filter *f;
903 struct sockaddr *addr = p;
904
905 if (!is_valid_ether_addr(addr->sa_data))
906 return -EADDRNOTAVAIL;
907
908 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
909 return 0;
910
Mitch Williams14e52ee2015-08-31 19:54:44 -0400911 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
912 return -EPERM;
913
914 f = i40evf_find_filter(adapter, hw->mac.addr);
915 if (f) {
916 f->remove = true;
917 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
918 }
919
Greg Rose5eae00c2013-12-21 06:12:45 +0000920 f = i40evf_add_filter(adapter, addr->sa_data);
921 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000922 ether_addr_copy(hw->mac.addr, addr->sa_data);
923 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000924 }
925
926 return (f == NULL) ? -ENOMEM : 0;
927}
928
929/**
930 * i40evf_set_rx_mode - NDO callback to set the netdev filters
931 * @netdev: network interface device structure
932 **/
933static void i40evf_set_rx_mode(struct net_device *netdev)
934{
935 struct i40evf_adapter *adapter = netdev_priv(netdev);
936 struct i40evf_mac_filter *f, *ftmp;
937 struct netdev_hw_addr *uca;
938 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400939 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000940 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000941
942 /* add addr if not already in the filter list */
943 netdev_for_each_uc_addr(uca, netdev) {
944 i40evf_add_filter(adapter, uca->addr);
945 }
946 netdev_for_each_mc_addr(mca, netdev) {
947 i40evf_add_filter(adapter, mca->addr);
948 }
949
950 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000951 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000952 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000953 if (--count == 0) {
954 dev_err(&adapter->pdev->dev,
955 "Failed to get lock in %s\n", __func__);
956 return;
957 }
958 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000959 /* remove filter if not in netdev list */
960 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400961 netdev_for_each_mc_addr(mca, netdev)
962 if (ether_addr_equal(mca->addr, f->macaddr))
963 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000964
Shannon Nelson2f41f332015-08-26 15:14:20 -0400965 netdev_for_each_uc_addr(uca, netdev)
966 if (ether_addr_equal(uca->addr, f->macaddr))
967 goto bottom_of_search_loop;
968
969 for_each_dev_addr(netdev, ha)
970 if (ether_addr_equal(ha->addr, f->macaddr))
971 goto bottom_of_search_loop;
972
973 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
974 goto bottom_of_search_loop;
975
976 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
977 f->remove = true;
978 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
979
980bottom_of_search_loop:
981 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000982 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700983
984 if (netdev->flags & IFF_PROMISC &&
985 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
986 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
987 else if (!(netdev->flags & IFF_PROMISC) &&
988 adapter->flags & I40EVF_FLAG_PROMISC_ON)
989 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
990
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700991 if (netdev->flags & IFF_ALLMULTI &&
992 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
993 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
994 else if (!(netdev->flags & IFF_ALLMULTI) &&
995 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
996 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
997
Greg Rose5eae00c2013-12-21 06:12:45 +0000998 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
999}
1000
1001/**
1002 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1003 * @adapter: board private structure
1004 **/
1005static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1006{
1007 int q_idx;
1008 struct i40e_q_vector *q_vector;
1009 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1010
1011 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1012 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001013
Mitch Williams7d96ba12015-10-26 19:44:39 -04001014 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001015 napi = &q_vector->napi;
1016 napi_enable(napi);
1017 }
1018}
1019
1020/**
1021 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1022 * @adapter: board private structure
1023 **/
1024static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1025{
1026 int q_idx;
1027 struct i40e_q_vector *q_vector;
1028 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1029
1030 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001031 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001032 napi_disable(&q_vector->napi);
1033 }
1034}
1035
1036/**
1037 * i40evf_configure - set up transmit and receive data structures
1038 * @adapter: board private structure
1039 **/
1040static void i40evf_configure(struct i40evf_adapter *adapter)
1041{
1042 struct net_device *netdev = adapter->netdev;
1043 int i;
1044
1045 i40evf_set_rx_mode(netdev);
1046
1047 i40evf_configure_tx(adapter);
1048 i40evf_configure_rx(adapter);
1049 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1050
Mitch Williamscc052922014-10-25 03:24:34 +00001051 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001052 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001053
Mitch Williamsb1630982016-04-18 11:33:48 -07001054 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001055 }
1056}
1057
1058/**
1059 * i40evf_up_complete - Finish the last steps of bringing up a connection
1060 * @adapter: board private structure
1061 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001062static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001063{
1064 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001065 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001066
1067 i40evf_napi_enable_all(adapter);
1068
1069 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001070 if (CLIENT_ENABLED(adapter))
1071 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001072 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001073}
1074
1075/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001076 * i40e_down - Shutdown the connection processing
1077 * @adapter: board private structure
1078 **/
1079void i40evf_down(struct i40evf_adapter *adapter)
1080{
1081 struct net_device *netdev = adapter->netdev;
1082 struct i40evf_mac_filter *f;
1083
Mitch Williams209dc4d2015-12-09 15:50:27 -08001084 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001085 return;
1086
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001087 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1088 &adapter->crit_section))
1089 usleep_range(500, 1000);
1090
Mitch Williams63e18c22015-03-27 00:12:10 -07001091 netif_carrier_off(netdev);
1092 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001093 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001094 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001095 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001096
Mitch Williamsef8693e2014-02-13 03:48:53 -08001097 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001098 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1099 f->remove = true;
1100 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001101 /* remove all VLAN filters */
1102 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1103 f->remove = true;
1104 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001105 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1106 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001107 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001108 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001109 /* Schedule operations to close down the HW. Don't wait
1110 * here for this to complete. The watchdog is still running
1111 * and it will take care of this.
1112 */
1113 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001114 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001115 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001116 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001117
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001118 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001119 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001120}
1121
1122/**
1123 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1124 * @adapter: board private structure
1125 * @vectors: number of vectors to request
1126 *
1127 * Work with the OS to set up the MSIX vectors needed.
1128 *
1129 * Returns 0 on success, negative on failure
1130 **/
1131static int
1132i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1133{
1134 int err, vector_threshold;
1135
1136 /* We'll want at least 3 (vector_threshold):
1137 * 0) Other (Admin Queue and link, mostly)
1138 * 1) TxQ[0] Cleanup
1139 * 2) RxQ[0] Cleanup
1140 */
1141 vector_threshold = MIN_MSIX_COUNT;
1142
1143 /* The more we get, the more we will assign to Tx/Rx Cleanup
1144 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1145 * Right now, we simply care about how many we'll get; we'll
1146 * set them up later while requesting irq's.
1147 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001148 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1149 vector_threshold, vectors);
1150 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001151 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001152 kfree(adapter->msix_entries);
1153 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001154 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001155 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001156
1157 /* Adjust for only the vectors we'll use, which is minimum
1158 * of max_msix_q_vectors + NONQ_VECS, or the number of
1159 * vectors we were allocated.
1160 */
1161 adapter->num_msix_vectors = err;
1162 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001163}
1164
1165/**
1166 * i40evf_free_queues - Free memory for all rings
1167 * @adapter: board private structure to initialize
1168 *
1169 * Free all of the memory associated with queue pairs.
1170 **/
1171static void i40evf_free_queues(struct i40evf_adapter *adapter)
1172{
Greg Rose5eae00c2013-12-21 06:12:45 +00001173 if (!adapter->vsi_res)
1174 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001175 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001176 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001177 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001178 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001179 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001180}
1181
1182/**
1183 * i40evf_alloc_queues - Allocate memory for all rings
1184 * @adapter: board private structure to initialize
1185 *
1186 * We allocate one ring per queue at run-time since we don't know the
1187 * number of queues at compile-time. The polling_netdev array is
1188 * intended for Multiqueue, but should work fine with a single queue.
1189 **/
1190static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1191{
Jacob Keller65c70062017-06-07 05:43:01 -04001192 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001193
Alan Brady5b36e8d2017-08-22 06:57:50 -04001194 /* If we're in reset reallocating queues we don't actually know yet for
1195 * certain the PF gave us the number of queues we asked for but we'll
1196 * assume it did. Once basic reset is finished we'll confirm once we
1197 * start negotiating config with PF.
1198 */
1199 if (adapter->num_req_queues)
1200 num_active_queues = adapter->num_req_queues;
1201 else
1202 num_active_queues = min_t(int,
1203 adapter->vsi_res->num_queue_pairs,
1204 (int)(num_online_cpus()));
1205
Jacob Keller65c70062017-06-07 05:43:01 -04001206
1207 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001208 sizeof(struct i40e_ring), GFP_KERNEL);
1209 if (!adapter->tx_rings)
1210 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001211 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001212 sizeof(struct i40e_ring), GFP_KERNEL);
1213 if (!adapter->rx_rings)
1214 goto err_out;
1215
Jacob Keller65c70062017-06-07 05:43:01 -04001216 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001217 struct i40e_ring *tx_ring;
1218 struct i40e_ring *rx_ring;
1219
Mitch Williams0dd438d2015-10-26 19:44:40 -04001220 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001221
1222 tx_ring->queue_index = i;
1223 tx_ring->netdev = adapter->netdev;
1224 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001225 tx_ring->count = adapter->tx_desc_count;
Jacob Keller42702552017-09-07 08:05:48 -04001226 tx_ring->tx_itr_setting = I40E_ITR_TX_DEF;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001227 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001228 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001229
Mitch Williams0dd438d2015-10-26 19:44:40 -04001230 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001231 rx_ring->queue_index = i;
1232 rx_ring->netdev = adapter->netdev;
1233 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001234 rx_ring->count = adapter->rx_desc_count;
Jacob Keller42702552017-09-07 08:05:48 -04001235 rx_ring->rx_itr_setting = I40E_ITR_RX_DEF;
Greg Rose5eae00c2013-12-21 06:12:45 +00001236 }
1237
Jacob Keller65c70062017-06-07 05:43:01 -04001238 adapter->num_active_queues = num_active_queues;
1239
Greg Rose5eae00c2013-12-21 06:12:45 +00001240 return 0;
1241
1242err_out:
1243 i40evf_free_queues(adapter);
1244 return -ENOMEM;
1245}
1246
1247/**
1248 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1249 * @adapter: board private structure to initialize
1250 *
1251 * Attempt to configure the interrupts using the best available
1252 * capabilities of the hardware and the kernel.
1253 **/
1254static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1255{
1256 int vector, v_budget;
1257 int pairs = 0;
1258 int err = 0;
1259
1260 if (!adapter->vsi_res) {
1261 err = -EIO;
1262 goto out;
1263 }
Mitch Williamscc052922014-10-25 03:24:34 +00001264 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001265
Jacob Keller789f38c2017-04-19 09:25:56 -04001266 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1267 * us much good if we have more vectors than CPUs. However, we already
1268 * limit the total number of queues by the number of CPUs so we do not
1269 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001270 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001271 v_budget = min_t(int, pairs + NONQ_VECS,
1272 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001273
Greg Rose5eae00c2013-12-21 06:12:45 +00001274 adapter->msix_entries = kcalloc(v_budget,
1275 sizeof(struct msix_entry), GFP_KERNEL);
1276 if (!adapter->msix_entries) {
1277 err = -ENOMEM;
1278 goto out;
1279 }
1280
1281 for (vector = 0; vector < v_budget; vector++)
1282 adapter->msix_entries[vector].entry = vector;
1283
Mitch Williams313ed2d2015-08-27 11:42:31 -04001284 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001285
1286out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001287 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1288 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001289 return err;
1290}
1291
1292/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001293 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1294 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001295 *
1296 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001297 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001298static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001299{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001300 struct i40e_aqc_get_set_rss_key_data *rss_key =
1301 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001302 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001303 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001304
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001305 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001306 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001307 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001308 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001309 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001310 }
1311
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001312 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1313 if (ret) {
1314 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1315 i40evf_stat_str(hw, ret),
1316 i40evf_aq_str(hw, hw->aq.asq_last_status));
1317 return ret;
1318
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319 }
1320
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001321 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1322 adapter->rss_lut, adapter->rss_lut_size);
1323 if (ret) {
1324 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1325 i40evf_stat_str(hw, ret),
1326 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001327 }
1328
1329 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001330
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001331}
1332
1333/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001334 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001335 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001336 *
1337 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001338 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001339static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001340{
1341 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001342 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001343 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001344
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001345 dw = (u32 *)adapter->rss_key;
1346 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1347 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001348
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001349 dw = (u32 *)adapter->rss_lut;
1350 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1351 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001352
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001353 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001354
1355 return 0;
1356}
1357
1358/**
1359 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001360 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001361 *
1362 * Returns 0 on success, negative on failure
1363 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001364int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001365{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001366
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001367 if (RSS_PF(adapter)) {
1368 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1369 I40EVF_FLAG_AQ_SET_RSS_KEY;
1370 return 0;
1371 } else if (RSS_AQ(adapter)) {
1372 return i40evf_config_rss_aq(adapter);
1373 } else {
1374 return i40evf_config_rss_reg(adapter);
1375 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001376}
1377
1378/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001379 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001380 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001381 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001382static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001383{
1384 u16 i;
1385
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001386 for (i = 0; i < adapter->rss_lut_size; i++)
1387 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001388}
1389
1390/**
Helin Zhang96a81982015-10-26 19:44:31 -04001391 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001392 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001393 *
1394 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001395 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001396static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001397{
1398 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001399 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001400
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001401 if (!RSS_PF(adapter)) {
1402 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001403 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001404 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001405 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1406 else
1407 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001408
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001409 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1410 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1411 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001412
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001413 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001414
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001415 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1416 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001417
1418 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001419}
1420
1421/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001422 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1423 * @adapter: board private structure to initialize
1424 *
1425 * We allocate one q_vector per queue interrupt. If allocation fails we
1426 * return -ENOMEM.
1427 **/
1428static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1429{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001430 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001431 struct i40e_q_vector *q_vector;
1432
1433 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001434 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001435 GFP_KERNEL);
1436 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001437 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001438
1439 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001440 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001441 q_vector->adapter = adapter;
1442 q_vector->vsi = &adapter->vsi;
1443 q_vector->v_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001444 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001445 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001446 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001447 }
1448
1449 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001450}
1451
1452/**
1453 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1454 * @adapter: board private structure to initialize
1455 *
1456 * This function frees the memory allocated to the q_vectors. In addition if
1457 * NAPI is enabled it will delete any references to the NAPI struct prior
1458 * to freeing the q_vector.
1459 **/
1460static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1461{
1462 int q_idx, num_q_vectors;
1463 int napi_vectors;
1464
Jacob Kelleref4603e2016-11-08 13:05:08 -08001465 if (!adapter->q_vectors)
1466 return;
1467
Greg Rose5eae00c2013-12-21 06:12:45 +00001468 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001469 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001470
1471 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001472 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001473 if (q_idx < napi_vectors)
1474 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001475 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001476 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001477 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001478}
1479
1480/**
1481 * i40evf_reset_interrupt_capability - Reset MSIX setup
1482 * @adapter: board private structure
1483 *
1484 **/
1485void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1486{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001487 if (!adapter->msix_entries)
1488 return;
1489
Greg Rose5eae00c2013-12-21 06:12:45 +00001490 pci_disable_msix(adapter->pdev);
1491 kfree(adapter->msix_entries);
1492 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001493}
1494
1495/**
1496 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1497 * @adapter: board private structure to initialize
1498 *
1499 **/
1500int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1501{
1502 int err;
1503
Jacob Keller283aeaf2017-04-19 09:25:59 -04001504 err = i40evf_alloc_queues(adapter);
1505 if (err) {
1506 dev_err(&adapter->pdev->dev,
1507 "Unable to allocate memory for queues\n");
1508 goto err_alloc_queues;
1509 }
1510
Jacob Keller62fe2a82016-07-27 12:02:33 -07001511 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001512 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001513 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001514 if (err) {
1515 dev_err(&adapter->pdev->dev,
1516 "Unable to setup interrupt capabilities\n");
1517 goto err_set_interrupt;
1518 }
1519
1520 err = i40evf_alloc_q_vectors(adapter);
1521 if (err) {
1522 dev_err(&adapter->pdev->dev,
1523 "Unable to allocate memory for queue vectors\n");
1524 goto err_alloc_q_vectors;
1525 }
1526
Greg Rose5eae00c2013-12-21 06:12:45 +00001527 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001528 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1529 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001530
1531 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001532err_alloc_q_vectors:
1533 i40evf_reset_interrupt_capability(adapter);
1534err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001535 i40evf_free_queues(adapter);
1536err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001537 return err;
1538}
1539
1540/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001541 * i40evf_free_rss - Free memory used by RSS structs
1542 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001543 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001544static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001545{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001546 kfree(adapter->rss_key);
1547 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001548
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001549 kfree(adapter->rss_lut);
1550 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001551}
1552
1553/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001554 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1555 * @adapter: board private structure
1556 *
1557 * Returns 0 on success, negative on failure
1558 **/
1559static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1560{
1561 struct net_device *netdev = adapter->netdev;
1562 int err;
1563
1564 if (netif_running(netdev))
1565 i40evf_free_traffic_irqs(adapter);
1566 i40evf_free_misc_irq(adapter);
1567 i40evf_reset_interrupt_capability(adapter);
1568 i40evf_free_q_vectors(adapter);
1569 i40evf_free_queues(adapter);
1570
1571 err = i40evf_init_interrupt_scheme(adapter);
1572 if (err)
1573 goto err;
1574
1575 netif_tx_stop_all_queues(netdev);
1576
1577 err = i40evf_request_misc_irq(adapter);
1578 if (err)
1579 goto err;
1580
1581 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1582
Mitch Williams1b7b7592017-08-22 06:57:51 -04001583 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001584
1585 if (RSS_AQ(adapter))
1586 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1587 else
1588 err = i40evf_init_rss(adapter);
1589err:
1590 return err;
1591}
1592
1593/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001594 * i40evf_watchdog_timer - Periodic call-back timer
1595 * @data: pointer to adapter disguised as unsigned long
1596 **/
Kees Cook26566ea2017-10-16 17:29:35 -07001597static void i40evf_watchdog_timer(struct timer_list *t)
Greg Rose5eae00c2013-12-21 06:12:45 +00001598{
Kees Cook26566ea2017-10-16 17:29:35 -07001599 struct i40evf_adapter *adapter = from_timer(adapter, t,
1600 watchdog_timer);
Mitch Williams75a64432014-11-11 20:02:42 +00001601
Greg Rose5eae00c2013-12-21 06:12:45 +00001602 schedule_work(&adapter->watchdog_task);
1603 /* timer will be rescheduled in watchdog task */
1604}
1605
1606/**
1607 * i40evf_watchdog_task - Periodic call-back task
1608 * @work: pointer to work_struct
1609 **/
1610static void i40evf_watchdog_task(struct work_struct *work)
1611{
1612 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001613 struct i40evf_adapter,
1614 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001615 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001616 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001617
Greg Rose5eae00c2013-12-21 06:12:45 +00001618 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001619 goto restart_watchdog;
1620
1621 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001622 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1623 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001624 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1625 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001626 /* A chance for redemption! */
1627 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1628 adapter->state = __I40EVF_STARTUP;
1629 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1630 schedule_delayed_work(&adapter->init_task, 10);
1631 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1632 &adapter->crit_section);
1633 /* Don't reschedule the watchdog, since we've restarted
1634 * the init task. When init_task contacts the PF and
1635 * gets everything set up again, it'll restart the
1636 * watchdog for us. Down, boy. Sit. Stay. Woof.
1637 */
1638 return;
1639 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001640 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001641 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001642 goto watchdog_done;
1643 }
1644
1645 if ((adapter->state < __I40EVF_DOWN) ||
1646 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001647 goto watchdog_done;
1648
Mitch Williamsef8693e2014-02-13 03:48:53 -08001649 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001650 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1651 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001652 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001653 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001654 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001655 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001656 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001657 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001658 goto watchdog_done;
1659 }
1660
1661 /* Process admin queue tasks. After init, everything gets done
1662 * here so we don't race on the admin queue.
1663 */
Mitch Williamsed636962015-04-07 19:45:32 -04001664 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001665 if (!i40evf_asq_done(hw)) {
1666 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1667 i40evf_send_api_ver(adapter);
1668 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001669 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001670 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001671 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1672 i40evf_send_vf_config_msg(adapter);
1673 goto watchdog_done;
1674 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001675
Mitch Williamse284fc82015-03-27 00:12:09 -07001676 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1677 i40evf_disable_queues(adapter);
1678 goto watchdog_done;
1679 }
1680
Greg Rose5eae00c2013-12-21 06:12:45 +00001681 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1682 i40evf_map_queues(adapter);
1683 goto watchdog_done;
1684 }
1685
1686 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1687 i40evf_add_ether_addrs(adapter);
1688 goto watchdog_done;
1689 }
1690
1691 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1692 i40evf_add_vlans(adapter);
1693 goto watchdog_done;
1694 }
1695
1696 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1697 i40evf_del_ether_addrs(adapter);
1698 goto watchdog_done;
1699 }
1700
1701 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1702 i40evf_del_vlans(adapter);
1703 goto watchdog_done;
1704 }
1705
Mariusz Stachura87743702017-07-17 22:09:45 -07001706 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1707 i40evf_enable_vlan_stripping(adapter);
1708 goto watchdog_done;
1709 }
1710
1711 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1712 i40evf_disable_vlan_stripping(adapter);
1713 goto watchdog_done;
1714 }
1715
Greg Rose5eae00c2013-12-21 06:12:45 +00001716 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1717 i40evf_configure_queues(adapter);
1718 goto watchdog_done;
1719 }
1720
1721 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1722 i40evf_enable_queues(adapter);
1723 goto watchdog_done;
1724 }
1725
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001726 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1727 /* This message goes straight to the firmware, not the
1728 * PF, so we don't have to set current_op as we will
1729 * not get a response through the ARQ.
1730 */
Helin Zhang96a81982015-10-26 19:44:31 -04001731 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001732 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1733 goto watchdog_done;
1734 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001735 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1736 i40evf_get_hena(adapter);
1737 goto watchdog_done;
1738 }
1739 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1740 i40evf_set_hena(adapter);
1741 goto watchdog_done;
1742 }
1743 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1744 i40evf_set_rss_key(adapter);
1745 goto watchdog_done;
1746 }
1747 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1748 i40evf_set_rss_lut(adapter);
1749 goto watchdog_done;
1750 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001751
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001752 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001753 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1754 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001755 goto watchdog_done;
1756 }
1757
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001758 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001759 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001760 goto watchdog_done;
1761 }
1762
1763 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1764 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001765 i40evf_set_promiscuous(adapter, 0);
1766 goto watchdog_done;
1767 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001768 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001769
Greg Rose5eae00c2013-12-21 06:12:45 +00001770 if (adapter->state == __I40EVF_RUNNING)
1771 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001772watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001773 if (adapter->state == __I40EVF_RUNNING) {
1774 i40evf_irq_enable_queues(adapter, ~0);
1775 i40evf_fire_sw_int(adapter, 0xFF);
1776 } else {
1777 i40evf_fire_sw_int(adapter, 0x1);
1778 }
1779
Mitch Williamsef8693e2014-02-13 03:48:53 -08001780 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1781restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001782 if (adapter->state == __I40EVF_REMOVE)
1783 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001784 if (adapter->aq_required)
1785 mod_timer(&adapter->watchdog_timer,
1786 jiffies + msecs_to_jiffies(20));
1787 else
1788 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001789 schedule_work(&adapter->adminq_task);
1790}
1791
Joe Perchesdedecb62016-11-01 15:35:14 -07001792static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1793{
1794 struct i40evf_mac_filter *f, *ftmp;
1795 struct i40evf_vlan_filter *fv, *fvtmp;
1796
1797 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1798
Jacob Keller44b034b2017-10-27 11:06:49 -04001799 /* We don't use netif_running() because it may be true prior to
1800 * ndo_open() returning, so we can't assume it means all our open
1801 * tasks have finished, since we're not holding the rtnl_lock here.
1802 */
1803 if (adapter->state == __I40EVF_RUNNING) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001804 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001805 netif_carrier_off(adapter->netdev);
1806 netif_tx_disable(adapter->netdev);
1807 adapter->link_up = false;
1808 i40evf_napi_disable_all(adapter);
1809 i40evf_irq_disable(adapter);
1810 i40evf_free_traffic_irqs(adapter);
1811 i40evf_free_all_tx_resources(adapter);
1812 i40evf_free_all_rx_resources(adapter);
1813 }
1814
1815 /* Delete all of the filters, both MAC and VLAN. */
1816 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1817 list_del(&f->list);
1818 kfree(f);
1819 }
1820
1821 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1822 list_del(&fv->list);
1823 kfree(fv);
1824 }
1825
1826 i40evf_free_misc_irq(adapter);
1827 i40evf_reset_interrupt_capability(adapter);
1828 i40evf_free_queues(adapter);
1829 i40evf_free_q_vectors(adapter);
1830 kfree(adapter->vf_res);
1831 i40evf_shutdown_adminq(&adapter->hw);
1832 adapter->netdev->flags &= ~IFF_UP;
1833 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1834 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1835 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001836 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001837 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1838}
1839
Mitch Williams67c818a2015-06-19 08:56:30 -07001840#define I40EVF_RESET_WAIT_MS 10
1841#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001842/**
1843 * i40evf_reset_task - Call-back task to handle hardware reset
1844 * @work: pointer to work_struct
1845 *
1846 * During reset we need to shut down and reinitialize the admin queue
1847 * before we can use it to communicate with the PF again. We also clear
1848 * and reinit the rings because that context is lost as well.
1849 **/
1850static void i40evf_reset_task(struct work_struct *work)
1851{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001852 struct i40evf_adapter *adapter = container_of(work,
1853 struct i40evf_adapter,
1854 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001855 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001856 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001857 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001858 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001859 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001860 int i = 0, err;
Jacob Keller44b034b2017-10-27 11:06:49 -04001861 bool running;
Greg Rose5eae00c2013-12-21 06:12:45 +00001862
Mitch Williamsed0e8942017-01-24 10:23:59 -08001863 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001864 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001865 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001866 if (CLIENT_ENABLED(adapter)) {
1867 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1868 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1869 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1870 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1871 cancel_delayed_work_sync(&adapter->client_task);
1872 i40evf_notify_client_close(&adapter->vsi, true);
1873 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001874 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001875 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001876 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1877 /* Restart the AQ here. If we have been reset but didn't
1878 * detect it, or if the PF had to reinit, our AQ will be hosed.
1879 */
1880 i40evf_shutdown_adminq(hw);
1881 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001882 i40evf_request_reset(adapter);
1883 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001884 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001885
Mitch Williamsef8693e2014-02-13 03:48:53 -08001886 /* poll until we see the reset actually happen */
1887 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001888 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1889 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1890 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001891 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001892 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001893 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001894 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001895 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001896 goto continue_reset; /* act like the reset happened */
1897 }
1898
1899 /* wait until the reset is complete and the PF is responding to us */
1900 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001901 /* sleep first to make sure a minimum wait time is met */
1902 msleep(I40EVF_RESET_WAIT_MS);
1903
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001904 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1905 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001906 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001907 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001908 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001909
Mitch Williams509a4472015-12-23 12:05:52 -08001910 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001911
Mitch Williamsef8693e2014-02-13 03:48:53 -08001912 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001913 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001914 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001915 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001916 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001917 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001918 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001919
1920continue_reset:
Jacob Keller44b034b2017-10-27 11:06:49 -04001921 /* We don't use netif_running() because it may be true prior to
1922 * ndo_open() returning, so we can't assume it means all our open
1923 * tasks have finished, since we're not holding the rtnl_lock here.
1924 */
1925 running = (adapter->state == __I40EVF_RUNNING);
1926
1927 if (running) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001928 netif_carrier_off(netdev);
1929 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001930 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001931 i40evf_napi_disable_all(adapter);
1932 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001933 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001934
Greg Rose5eae00c2013-12-21 06:12:45 +00001935 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001936 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1937
1938 /* free the Tx/Rx rings and descriptors, might be better to just
1939 * re-use them sometime in the future
1940 */
1941 i40evf_free_all_rx_resources(adapter);
1942 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001943
1944 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001945 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001946 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001947 err = i40evf_init_adminq(hw);
1948 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001949 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1950 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001951 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001952
Alan Brady5b36e8d2017-08-22 06:57:50 -04001953 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1954 err = i40evf_reinit_interrupt_scheme(adapter);
1955 if (err)
1956 goto reset_err;
1957 }
1958
1959 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001960 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001961
1962 /* re-add all MAC filters */
1963 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1964 f->add = true;
1965 }
1966 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001967 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1968 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001969 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001970 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001971 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001972 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001973 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001974 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001975
1976 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1977
Jacob Keller44b034b2017-10-27 11:06:49 -04001978 /* We were running when the reset started, so we need to restore some
1979 * state here.
1980 */
1981 if (running) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001982 /* allocate transmit descriptors */
1983 err = i40evf_setup_all_tx_resources(adapter);
1984 if (err)
1985 goto reset_err;
1986
1987 /* allocate receive descriptors */
1988 err = i40evf_setup_all_rx_resources(adapter);
1989 if (err)
1990 goto reset_err;
1991
Alan Brady5b36e8d2017-08-22 06:57:50 -04001992 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1993 err = i40evf_request_traffic_irqs(adapter,
1994 netdev->name);
1995 if (err)
1996 goto reset_err;
1997
1998 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1999 }
2000
Greg Rose5eae00c2013-12-21 06:12:45 +00002001 i40evf_configure(adapter);
2002
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002003 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002004
2005 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07002006 } else {
2007 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002008 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00002009 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002010
Greg Rose5eae00c2013-12-21 06:12:45 +00002011 return;
2012reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00002013 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04002014 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002015}
2016
2017/**
2018 * i40evf_adminq_task - worker thread to clean the admin queue
2019 * @work: pointer to work_struct containing our data
2020 **/
2021static void i40evf_adminq_task(struct work_struct *work)
2022{
2023 struct i40evf_adapter *adapter =
2024 container_of(work, struct i40evf_adapter, adminq_task);
2025 struct i40e_hw *hw = &adapter->hw;
2026 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07002027 enum virtchnl_ops v_op;
2028 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00002029 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00002030 u16 pending;
2031
Mitch Williamsef8693e2014-02-13 03:48:53 -08002032 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00002033 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08002034
Mitch Williams1001dc32014-11-11 20:02:19 +00002035 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2036 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00002037 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00002038 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00002039
Greg Rose5eae00c2013-12-21 06:12:45 +00002040 do {
2041 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002042 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2043 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2044
2045 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002046 break; /* No event to process or error cleaning ARQ */
2047
Tushar Davec969ef42017-06-22 09:44:32 -07002048 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002049 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002050 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002051 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002052 } while (pending);
2053
Mitch Williams67c818a2015-06-19 08:56:30 -07002054 if ((adapter->flags &
2055 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2056 adapter->state == __I40EVF_RESETTING)
2057 goto freedom;
2058
Mitch Williams912257e2014-05-22 06:32:07 +00002059 /* check for error indications */
2060 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002061 if (val == 0xdeadbeef) /* indicates device in reset */
2062 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002063 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002064 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002065 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002066 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002067 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002068 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002069 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002070 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002071 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002072 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002073 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002074 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002075 }
2076 if (oldval != val)
2077 wr32(hw, hw->aq.arq.len, val);
2078
2079 val = rd32(hw, hw->aq.asq.len);
2080 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002081 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002082 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002083 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002084 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002085 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002086 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002087 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002088 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002089 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002090 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002091 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002092 }
2093 if (oldval != val)
2094 wr32(hw, hw->aq.asq.len, val);
2095
Mitch Williams67c818a2015-06-19 08:56:30 -07002096freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002097 kfree(event.msg_buf);
2098out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002099 /* re-enable Admin queue interrupt cause */
2100 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002101}
2102
2103/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002104 * i40evf_client_task - worker thread to perform client work
2105 * @work: pointer to work_struct containing our data
2106 *
2107 * This task handles client interactions. Because client calls can be
2108 * reentrant, we can't handle them in the watchdog.
2109 **/
2110static void i40evf_client_task(struct work_struct *work)
2111{
2112 struct i40evf_adapter *adapter =
2113 container_of(work, struct i40evf_adapter, client_task.work);
2114
2115 /* If we can't get the client bit, just give up. We'll be rescheduled
2116 * later.
2117 */
2118
2119 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2120 return;
2121
2122 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2123 i40evf_client_subtask(adapter);
2124 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2125 goto out;
2126 }
Alan Brady01acc732017-11-14 07:00:51 -05002127 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2128 i40evf_notify_client_l2_params(&adapter->vsi);
2129 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2130 goto out;
2131 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002132 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2133 i40evf_notify_client_close(&adapter->vsi, false);
2134 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2135 goto out;
2136 }
2137 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2138 i40evf_notify_client_open(&adapter->vsi);
2139 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002140 }
2141out:
2142 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2143}
2144
2145/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002146 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2147 * @adapter: board private structure
2148 *
2149 * Free all transmit software resources
2150 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002151void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002152{
2153 int i;
2154
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002155 if (!adapter->tx_rings)
2156 return;
2157
Mitch Williamscc052922014-10-25 03:24:34 +00002158 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002159 if (adapter->tx_rings[i].desc)
2160 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002161}
2162
2163/**
2164 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2165 * @adapter: board private structure
2166 *
2167 * If this function returns with an error, then it's possible one or
2168 * more of the rings is populated (while the rest are not). It is the
2169 * callers duty to clean those orphaned rings.
2170 *
2171 * Return 0 on success, negative on failure
2172 **/
2173static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2174{
2175 int i, err = 0;
2176
Mitch Williamscc052922014-10-25 03:24:34 +00002177 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002178 adapter->tx_rings[i].count = adapter->tx_desc_count;
2179 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002180 if (!err)
2181 continue;
2182 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002183 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002184 break;
2185 }
2186
2187 return err;
2188}
2189
2190/**
2191 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2192 * @adapter: board private structure
2193 *
2194 * If this function returns with an error, then it's possible one or
2195 * more of the rings is populated (while the rest are not). It is the
2196 * callers duty to clean those orphaned rings.
2197 *
2198 * Return 0 on success, negative on failure
2199 **/
2200static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2201{
2202 int i, err = 0;
2203
Mitch Williamscc052922014-10-25 03:24:34 +00002204 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002205 adapter->rx_rings[i].count = adapter->rx_desc_count;
2206 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002207 if (!err)
2208 continue;
2209 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002210 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002211 break;
2212 }
2213 return err;
2214}
2215
2216/**
2217 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2218 * @adapter: board private structure
2219 *
2220 * Free all receive software resources
2221 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002222void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002223{
2224 int i;
2225
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002226 if (!adapter->rx_rings)
2227 return;
2228
Mitch Williamscc052922014-10-25 03:24:34 +00002229 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002230 if (adapter->rx_rings[i].desc)
2231 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002232}
2233
2234/**
2235 * i40evf_open - Called when a network interface is made active
2236 * @netdev: network interface device structure
2237 *
2238 * Returns 0 on success, negative value on failure
2239 *
2240 * The open entry point is called when a network interface is made
2241 * active by the system (IFF_UP). At this point all resources needed
2242 * for transmit and receive operations are allocated, the interrupt
2243 * handler is registered with the OS, the watchdog timer is started,
2244 * and the stack is notified that the interface is ready.
2245 **/
2246static int i40evf_open(struct net_device *netdev)
2247{
2248 struct i40evf_adapter *adapter = netdev_priv(netdev);
2249 int err;
2250
Mitch Williamsef8693e2014-02-13 03:48:53 -08002251 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2252 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2253 return -EIO;
2254 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002255
2256 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002257 return -EBUSY;
2258
2259 /* allocate transmit descriptors */
2260 err = i40evf_setup_all_tx_resources(adapter);
2261 if (err)
2262 goto err_setup_tx;
2263
2264 /* allocate receive descriptors */
2265 err = i40evf_setup_all_rx_resources(adapter);
2266 if (err)
2267 goto err_setup_rx;
2268
2269 /* clear any pending interrupts, may auto mask */
2270 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2271 if (err)
2272 goto err_req_irq;
2273
Mitch Williams44151cd2015-04-27 14:57:17 -04002274 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002275 i40evf_configure(adapter);
2276
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002277 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002278
2279 i40evf_irq_enable(adapter, true);
2280
2281 return 0;
2282
2283err_req_irq:
2284 i40evf_down(adapter);
2285 i40evf_free_traffic_irqs(adapter);
2286err_setup_rx:
2287 i40evf_free_all_rx_resources(adapter);
2288err_setup_tx:
2289 i40evf_free_all_tx_resources(adapter);
2290
2291 return err;
2292}
2293
2294/**
2295 * i40evf_close - Disables a network interface
2296 * @netdev: network interface device structure
2297 *
2298 * Returns 0, this is not allowed to fail
2299 *
2300 * The close entry point is called when an interface is de-activated
2301 * by the OS. The hardware is still under the drivers control, but
2302 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2303 * are freed, along with all transmit and receive resources.
2304 **/
2305static int i40evf_close(struct net_device *netdev)
2306{
2307 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002308 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002309
Mitch Williams209dc4d2015-12-09 15:50:27 -08002310 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002311 return 0;
2312
Mitch Williamsef8693e2014-02-13 03:48:53 -08002313
Jacob Keller0da36b92017-04-19 09:25:55 -04002314 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002315 if (CLIENT_ENABLED(adapter))
2316 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002317
2318 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002319 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002320 i40evf_free_traffic_irqs(adapter);
2321
Mitch Williams51f38262016-12-12 15:44:11 -08002322 /* We explicitly don't free resources here because the hardware is
2323 * still active and can DMA into memory. Resources are cleared in
2324 * i40evf_virtchnl_completion() after we get confirmation from the PF
2325 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002326 *
2327 * Also, we wait for state to transition to __I40EVF_DOWN before
2328 * returning. State change occurs in i40evf_virtchnl_completion() after
2329 * VF resources are released (which occurs after PF driver processes and
2330 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002331 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002332
2333 status = wait_event_timeout(adapter->down_waitqueue,
2334 adapter->state == __I40EVF_DOWN,
2335 msecs_to_jiffies(200));
2336 if (!status)
2337 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002338 return 0;
2339}
2340
2341/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002342 * i40evf_change_mtu - Change the Maximum Transfer Unit
2343 * @netdev: network interface device structure
2344 * @new_mtu: new value for maximum frame size
2345 *
2346 * Returns 0 on success, negative on failure
2347 **/
2348static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2349{
2350 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002351
Greg Rose5eae00c2013-12-21 06:12:45 +00002352 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002353 if (CLIENT_ENABLED(adapter)) {
2354 i40evf_notify_client_l2_params(&adapter->vsi);
2355 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2356 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002357 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2358 schedule_work(&adapter->reset_task);
2359
Greg Rose5eae00c2013-12-21 06:12:45 +00002360 return 0;
2361}
2362
Alexander Duyck06fc0162016-10-25 16:08:47 -07002363/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002364 * i40e_set_features - set the netdev feature flags
2365 * @netdev: ptr to the netdev being adjusted
2366 * @features: the feature set that the stack is suggesting
2367 * Note: expects to be called while under rtnl_lock()
2368 **/
2369static int i40evf_set_features(struct net_device *netdev,
2370 netdev_features_t features)
2371{
2372 struct i40evf_adapter *adapter = netdev_priv(netdev);
2373
2374 if (!VLAN_ALLOWED(adapter))
2375 return -EINVAL;
2376
2377 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2378 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2379 else
2380 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2381
2382 return 0;
2383}
2384
2385/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002386 * i40evf_features_check - Validate encapsulated packet conforms to limits
2387 * @skb: skb buff
2388 * @netdev: This physical port's netdev
2389 * @features: Offload features that the stack believes apply
2390 **/
2391static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2392 struct net_device *dev,
2393 netdev_features_t features)
2394{
2395 size_t len;
2396
2397 /* No point in doing any of this if neither checksum nor GSO are
2398 * being requested for this frame. We can rule out both by just
2399 * checking for CHECKSUM_PARTIAL
2400 */
2401 if (skb->ip_summed != CHECKSUM_PARTIAL)
2402 return features;
2403
2404 /* We cannot support GSO if the MSS is going to be less than
2405 * 64 bytes. If it is then we need to drop support for GSO.
2406 */
2407 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2408 features &= ~NETIF_F_GSO_MASK;
2409
2410 /* MACLEN can support at most 63 words */
2411 len = skb_network_header(skb) - skb->data;
2412 if (len & ~(63 * 2))
2413 goto out_err;
2414
2415 /* IPLEN and EIPLEN can support at most 127 dwords */
2416 len = skb_transport_header(skb) - skb_network_header(skb);
2417 if (len & ~(127 * 4))
2418 goto out_err;
2419
2420 if (skb->encapsulation) {
2421 /* L4TUNLEN can support 127 words */
2422 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2423 if (len & ~(127 * 2))
2424 goto out_err;
2425
2426 /* IPLEN can support at most 127 dwords */
2427 len = skb_inner_transport_header(skb) -
2428 skb_inner_network_header(skb);
2429 if (len & ~(127 * 4))
2430 goto out_err;
2431 }
2432
2433 /* No need to validate L4LEN as TCP is the only protocol with a
2434 * a flexible value and we support all possible values supported
2435 * by TCP, which is at most 15 dwords
2436 */
2437
2438 return features;
2439out_err:
2440 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2441}
2442
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002443/**
2444 * i40evf_fix_features - fix up the netdev feature bits
2445 * @netdev: our net device
2446 * @features: desired feature bits
2447 *
2448 * Returns fixed-up features bits
2449 **/
2450static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2451 netdev_features_t features)
2452{
2453 struct i40evf_adapter *adapter = netdev_priv(netdev);
2454
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002455 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
2456 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
2457 NETIF_F_HW_VLAN_CTAG_RX |
2458 NETIF_F_HW_VLAN_CTAG_FILTER);
2459
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002460 return features;
2461}
2462
Greg Rose5eae00c2013-12-21 06:12:45 +00002463static const struct net_device_ops i40evf_netdev_ops = {
2464 .ndo_open = i40evf_open,
2465 .ndo_stop = i40evf_close,
2466 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002467 .ndo_set_rx_mode = i40evf_set_rx_mode,
2468 .ndo_validate_addr = eth_validate_addr,
2469 .ndo_set_mac_address = i40evf_set_mac,
2470 .ndo_change_mtu = i40evf_change_mtu,
2471 .ndo_tx_timeout = i40evf_tx_timeout,
2472 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2473 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002474 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002475 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002476 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002477#ifdef CONFIG_NET_POLL_CONTROLLER
2478 .ndo_poll_controller = i40evf_netpoll,
2479#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002480};
2481
2482/**
2483 * i40evf_check_reset_complete - check that VF reset is complete
2484 * @hw: pointer to hw struct
2485 *
2486 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2487 **/
2488static int i40evf_check_reset_complete(struct i40e_hw *hw)
2489{
2490 u32 rstat;
2491 int i;
2492
2493 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002494 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2495 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002496 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2497 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002498 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002499 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002500 }
2501 return -EBUSY;
2502}
2503
2504/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002505 * i40evf_process_config - Process the config information we got from the PF
2506 * @adapter: board private structure
2507 *
2508 * Verify that we have a valid config struct, and set up our netdev features
2509 * and our VSI struct.
2510 **/
2511int i40evf_process_config(struct i40evf_adapter *adapter)
2512{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002513 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002514 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002515 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002516 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002517 netdev_features_t hw_enc_features;
2518 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002519
2520 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002521 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002522 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002523 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002524 }
2525 if (!adapter->vsi_res) {
2526 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2527 return -ENODEV;
2528 }
2529
Alan Brady5b36e8d2017-08-22 06:57:50 -04002530 if (num_req_queues &&
2531 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2532 /* Problem. The PF gave us fewer queues than what we had
2533 * negotiated in our request. Need a reset to see if we can't
2534 * get back to a working state.
2535 */
2536 dev_err(&adapter->pdev->dev,
2537 "Requested %d queues, but PF only gave us %d.\n",
2538 num_req_queues,
2539 adapter->vsi_res->num_queue_pairs);
2540 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2541 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2542 i40evf_schedule_reset(adapter);
2543 return -ENODEV;
2544 }
2545 adapter->num_req_queues = 0;
2546
Preethi Banalabacd75c2017-03-27 14:43:18 -07002547 hw_enc_features = NETIF_F_SG |
2548 NETIF_F_IP_CSUM |
2549 NETIF_F_IPV6_CSUM |
2550 NETIF_F_HIGHDMA |
2551 NETIF_F_SOFT_FEATURES |
2552 NETIF_F_TSO |
2553 NETIF_F_TSO_ECN |
2554 NETIF_F_TSO6 |
2555 NETIF_F_SCTP_CRC |
2556 NETIF_F_RXHASH |
2557 NETIF_F_RXCSUM |
2558 0;
2559
2560 /* advertise to stack only if offloads for encapsulated packets is
2561 * supported
2562 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002563 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002564 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002565 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002566 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002567 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002568 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002569 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002570 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002571 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002572
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002573 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002574 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002575 netdev->gso_partial_features |=
2576 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002577
Preethi Banalabacd75c2017-03-27 14:43:18 -07002578 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2579 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2580 netdev->hw_enc_features |= hw_enc_features;
2581 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002582 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002583 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002584
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002585 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002586 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002587 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002588 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002589
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002590 /* Enable VLAN features if supported */
2591 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2592 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
2593 NETIF_F_HW_VLAN_CTAG_RX);
2594
Preethi Banalabacd75c2017-03-27 14:43:18 -07002595 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002596
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002597 netdev->features |= hw_features;
2598
2599 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2600 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002601
2602 adapter->vsi.id = adapter->vsi_res->vsi_id;
2603
2604 adapter->vsi.back = adapter;
2605 adapter->vsi.base_vector = 1;
2606 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002607 vsi->netdev = adapter->netdev;
2608 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002609 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002610 adapter->rss_key_size = vfres->rss_key_size;
2611 adapter->rss_lut_size = vfres->rss_lut_size;
2612 } else {
2613 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2614 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2615 }
2616
Mitch Williamse6d038d2015-06-04 16:23:58 -04002617 return 0;
2618}
2619
2620/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002621 * i40evf_init_task - worker thread to perform delayed initialization
2622 * @work: pointer to work_struct containing our data
2623 *
2624 * This task completes the work that was begun in probe. Due to the nature
2625 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002626 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002627 * whole system, we'll do it in a task so we can sleep.
2628 * This task only runs during driver init. Once we've established
2629 * communications with the PF driver and set up our netdev, the watchdog
2630 * takes over.
2631 **/
2632static void i40evf_init_task(struct work_struct *work)
2633{
2634 struct i40evf_adapter *adapter = container_of(work,
2635 struct i40evf_adapter,
2636 init_task.work);
2637 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002638 struct i40e_hw *hw = &adapter->hw;
2639 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002640 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002641
2642 switch (adapter->state) {
2643 case __I40EVF_STARTUP:
2644 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002645 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2646 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002647 err = i40e_set_mac_type(hw);
2648 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002649 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2650 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002651 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002652 }
2653 err = i40evf_check_reset_complete(hw);
2654 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002655 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002656 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002657 goto err;
2658 }
2659 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2660 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2661 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2662 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2663
2664 err = i40evf_init_adminq(hw);
2665 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002666 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2667 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002668 goto err;
2669 }
2670 err = i40evf_send_api_ver(adapter);
2671 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002672 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002673 i40evf_shutdown_adminq(hw);
2674 goto err;
2675 }
2676 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2677 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002678 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002679 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002680 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002681 i40evf_shutdown_adminq(hw);
2682 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002683 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002684 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002685
2686 /* aq msg sent, awaiting reply */
2687 err = i40evf_verify_api_ver(adapter);
2688 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002689 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002690 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002691 else
2692 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2693 adapter->pf_version.major,
2694 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002695 VIRTCHNL_VERSION_MAJOR,
2696 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002697 goto err;
2698 }
2699 err = i40evf_send_vf_config_msg(adapter);
2700 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002701 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002702 err);
2703 goto err;
2704 }
2705 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2706 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002707 case __I40EVF_INIT_GET_RESOURCES:
2708 /* aq msg sent, awaiting reply */
2709 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002710 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002711 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002712 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002713 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002714 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002715 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002716 }
2717 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002718 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002719 err = i40evf_send_vf_config_msg(adapter);
2720 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002721 } else if (err == I40E_ERR_PARAM) {
2722 /* We only get ERR_PARAM if the device is in a very bad
2723 * state or if we've been disabled for previous bad
2724 * behavior. Either way, we're done now.
2725 */
2726 i40evf_shutdown_adminq(hw);
2727 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2728 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002729 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002730 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002731 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2732 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002733 goto err_alloc;
2734 }
2735 adapter->state = __I40EVF_INIT_SW;
2736 break;
2737 default:
2738 goto err_alloc;
2739 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002740
Mitch Williamse6d038d2015-06-04 16:23:58 -04002741 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002742 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002743 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002744
2745 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2746
Greg Rose5eae00c2013-12-21 06:12:45 +00002747 netdev->netdev_ops = &i40evf_netdev_ops;
2748 i40evf_set_ethtool_ops(netdev);
2749 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002750
Jarod Wilson91c527a2016-10-17 15:54:05 -04002751 /* MTU range: 68 - 9710 */
2752 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002753 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002754
Greg Rose5eae00c2013-12-21 06:12:45 +00002755 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002756 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002757 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002758 eth_hw_addr_random(netdev);
2759 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2760 } else {
2761 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2762 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2763 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002764 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002765
Kees Cook26566ea2017-10-16 17:29:35 -07002766 timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
Greg Rose5eae00c2013-12-21 06:12:45 +00002767 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2768
Mitch Williamsd732a182014-04-24 06:41:37 +00002769 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2770 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002771 err = i40evf_init_interrupt_scheme(adapter);
2772 if (err)
2773 goto err_sw_init;
2774 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002775 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002776 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002777 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2778
Greg Rose5eae00c2013-12-21 06:12:45 +00002779 err = i40evf_request_misc_irq(adapter);
2780 if (err)
2781 goto err_sw_init;
2782
2783 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002784 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002785
Mitch Williamsef8693e2014-02-13 03:48:53 -08002786 if (!adapter->netdev_registered) {
2787 err = register_netdev(netdev);
2788 if (err)
2789 goto err_register;
2790 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002791
2792 adapter->netdev_registered = true;
2793
2794 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002795 if (CLIENT_ALLOWED(adapter)) {
2796 err = i40evf_lan_add_device(adapter);
2797 if (err)
2798 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2799 err);
2800 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002801
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002802 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002803 if (netdev->features & NETIF_F_GRO)
2804 dev_info(&pdev->dev, "GRO is enabled\n");
2805
Greg Rose5eae00c2013-12-21 06:12:45 +00002806 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002807 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002808 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002809 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002810
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002811 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2812 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2813 if (!adapter->rss_key || !adapter->rss_lut)
2814 goto err_mem;
2815
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002816 if (RSS_AQ(adapter)) {
2817 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2818 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2819 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002820 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002821 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002822 return;
2823restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002824 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002825 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002826err_mem:
2827 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002828err_register:
2829 i40evf_free_misc_irq(adapter);
2830err_sw_init:
2831 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002832err_alloc:
2833 kfree(adapter->vf_res);
2834 adapter->vf_res = NULL;
2835err:
2836 /* Things went into the weeds, so try again later */
2837 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002838 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002839 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002840 i40evf_shutdown_adminq(hw);
2841 adapter->state = __I40EVF_STARTUP;
2842 schedule_delayed_work(&adapter->init_task, HZ * 5);
2843 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002844 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002845 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002846}
2847
2848/**
2849 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2850 * @pdev: pci device structure
2851 **/
2852static void i40evf_shutdown(struct pci_dev *pdev)
2853{
2854 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002855 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002856
2857 netif_device_detach(netdev);
2858
2859 if (netif_running(netdev))
2860 i40evf_close(netdev);
2861
Mitch Williams00293fd2015-01-09 11:18:18 +00002862 /* Prevent the watchdog from running. */
2863 adapter->state = __I40EVF_REMOVE;
2864 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002865
Greg Rose5eae00c2013-12-21 06:12:45 +00002866#ifdef CONFIG_PM
2867 pci_save_state(pdev);
2868
2869#endif
2870 pci_disable_device(pdev);
2871}
2872
2873/**
2874 * i40evf_probe - Device Initialization Routine
2875 * @pdev: PCI device information struct
2876 * @ent: entry in i40evf_pci_tbl
2877 *
2878 * Returns 0 on success, negative on failure
2879 *
2880 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2881 * The OS initialization, configuring of the adapter private structure,
2882 * and a hardware reset occur.
2883 **/
2884static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2885{
2886 struct net_device *netdev;
2887 struct i40evf_adapter *adapter = NULL;
2888 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002889 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002890
2891 err = pci_enable_device(pdev);
2892 if (err)
2893 return err;
2894
Mitch Williams64942942014-02-11 08:26:33 +00002895 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002896 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002897 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2898 if (err) {
2899 dev_err(&pdev->dev,
2900 "DMA configuration failed: 0x%x\n", err);
2901 goto err_dma;
2902 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002903 }
2904
2905 err = pci_request_regions(pdev, i40evf_driver_name);
2906 if (err) {
2907 dev_err(&pdev->dev,
2908 "pci_request_regions failed 0x%x\n", err);
2909 goto err_pci_reg;
2910 }
2911
2912 pci_enable_pcie_error_reporting(pdev);
2913
2914 pci_set_master(pdev);
2915
Mitch Williams1255b7a2015-11-06 15:25:59 -08002916 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002917 if (!netdev) {
2918 err = -ENOMEM;
2919 goto err_alloc_etherdev;
2920 }
2921
2922 SET_NETDEV_DEV(netdev, &pdev->dev);
2923
2924 pci_set_drvdata(pdev, netdev);
2925 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002926
2927 adapter->netdev = netdev;
2928 adapter->pdev = pdev;
2929
2930 hw = &adapter->hw;
2931 hw->back = adapter;
2932
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002933 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002934 adapter->state = __I40EVF_STARTUP;
2935
2936 /* Call save state here because it relies on the adapter struct. */
2937 pci_save_state(pdev);
2938
2939 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2940 pci_resource_len(pdev, 0));
2941 if (!hw->hw_addr) {
2942 err = -EIO;
2943 goto err_ioremap;
2944 }
2945 hw->vendor_id = pdev->vendor;
2946 hw->device_id = pdev->device;
2947 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2948 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2949 hw->subsystem_device_id = pdev->subsystem_device;
2950 hw->bus.device = PCI_SLOT(pdev->devfn);
2951 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002952 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002953
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002954 /* set up the locks for the AQ, do this only once in probe
2955 * and destroy them only once in remove
2956 */
2957 mutex_init(&hw->aq.asq_mutex);
2958 mutex_init(&hw->aq.arq_mutex);
2959
Serey Kong8bb1a542014-08-01 13:27:15 -07002960 INIT_LIST_HEAD(&adapter->mac_filter_list);
2961 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2962
Greg Rose5eae00c2013-12-21 06:12:45 +00002963 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2964 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2965 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002966 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002967 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002968 schedule_delayed_work(&adapter->init_task,
2969 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002970
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002971 /* Setup the wait queue for indicating transition to down status */
2972 init_waitqueue_head(&adapter->down_waitqueue);
2973
Greg Rose5eae00c2013-12-21 06:12:45 +00002974 return 0;
2975
2976err_ioremap:
2977 free_netdev(netdev);
2978err_alloc_etherdev:
2979 pci_release_regions(pdev);
2980err_pci_reg:
2981err_dma:
2982 pci_disable_device(pdev);
2983 return err;
2984}
2985
2986#ifdef CONFIG_PM
2987/**
2988 * i40evf_suspend - Power management suspend routine
2989 * @pdev: PCI device information struct
2990 * @state: unused
2991 *
2992 * Called when the system (VM) is entering sleep/suspend.
2993 **/
2994static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2995{
2996 struct net_device *netdev = pci_get_drvdata(pdev);
2997 struct i40evf_adapter *adapter = netdev_priv(netdev);
2998 int retval = 0;
2999
3000 netif_device_detach(netdev);
3001
3002 if (netif_running(netdev)) {
3003 rtnl_lock();
3004 i40evf_down(adapter);
3005 rtnl_unlock();
3006 }
3007 i40evf_free_misc_irq(adapter);
3008 i40evf_reset_interrupt_capability(adapter);
3009
3010 retval = pci_save_state(pdev);
3011 if (retval)
3012 return retval;
3013
3014 pci_disable_device(pdev);
3015
3016 return 0;
3017}
3018
3019/**
Joe Perchesdbedd442015-03-06 20:49:12 -08003020 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00003021 * @pdev: PCI device information struct
3022 *
3023 * Called when the system (VM) is resumed from sleep/suspend.
3024 **/
3025static int i40evf_resume(struct pci_dev *pdev)
3026{
3027 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3028 struct net_device *netdev = adapter->netdev;
3029 u32 err;
3030
3031 pci_set_power_state(pdev, PCI_D0);
3032 pci_restore_state(pdev);
3033 /* pci_restore_state clears dev->state_saved so call
3034 * pci_save_state to restore it.
3035 */
3036 pci_save_state(pdev);
3037
3038 err = pci_enable_device_mem(pdev);
3039 if (err) {
3040 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3041 return err;
3042 }
3043 pci_set_master(pdev);
3044
3045 rtnl_lock();
3046 err = i40evf_set_interrupt_capability(adapter);
3047 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003048 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003049 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3050 return err;
3051 }
3052 err = i40evf_request_misc_irq(adapter);
3053 rtnl_unlock();
3054 if (err) {
3055 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3056 return err;
3057 }
3058
3059 schedule_work(&adapter->reset_task);
3060
3061 netif_device_attach(netdev);
3062
3063 return err;
3064}
3065
3066#endif /* CONFIG_PM */
3067/**
3068 * i40evf_remove - Device Removal Routine
3069 * @pdev: PCI device information struct
3070 *
3071 * i40evf_remove is called by the PCI subsystem to alert the driver
3072 * that it should release a PCI device. The could be caused by a
3073 * Hot-Plug event, or because the driver is going to be removed from
3074 * memory.
3075 **/
3076static void i40evf_remove(struct pci_dev *pdev)
3077{
3078 struct net_device *netdev = pci_get_drvdata(pdev);
3079 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003080 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003081 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003082 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003083
3084 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003085 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003086 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003087 if (adapter->netdev_registered) {
3088 unregister_netdev(netdev);
3089 adapter->netdev_registered = false;
3090 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003091 if (CLIENT_ALLOWED(adapter)) {
3092 err = i40evf_lan_del_device(adapter);
3093 if (err)
3094 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3095 err);
3096 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003097
Mitch Williamsf4a71882015-01-09 11:18:16 +00003098 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003099 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003100 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003101 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003102 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003103 /* If the FW isn't responding, kick it once, but only once. */
3104 if (!i40evf_asq_done(hw)) {
3105 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003106 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003107 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003108 i40evf_free_all_tx_resources(adapter);
3109 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003110 i40evf_misc_irq_disable(adapter);
3111 i40evf_free_misc_irq(adapter);
3112 i40evf_reset_interrupt_capability(adapter);
3113 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003114
Mitch Williamse5d17c32014-06-04 04:22:38 +00003115 if (adapter->watchdog_timer.function)
3116 del_timer_sync(&adapter->watchdog_timer);
3117
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003118 flush_scheduled_work();
3119
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003120 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003121
Greg Rose5eae00c2013-12-21 06:12:45 +00003122 if (hw->aq.asq.count)
3123 i40evf_shutdown_adminq(hw);
3124
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003125 /* destroy the locks only once, here */
3126 mutex_destroy(&hw->aq.arq_mutex);
3127 mutex_destroy(&hw->aq.asq_mutex);
3128
Greg Rose5eae00c2013-12-21 06:12:45 +00003129 iounmap(hw->hw_addr);
3130 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003131 i40evf_free_all_tx_resources(adapter);
3132 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003133 i40evf_free_queues(adapter);
3134 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003135 /* If we got removed before an up/down sequence, we've got a filter
3136 * hanging out there that we need to get rid of.
3137 */
3138 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3139 list_del(&f->list);
3140 kfree(f);
3141 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003142 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3143 list_del(&f->list);
3144 kfree(f);
3145 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003146
3147 free_netdev(netdev);
3148
3149 pci_disable_pcie_error_reporting(pdev);
3150
3151 pci_disable_device(pdev);
3152}
3153
3154static struct pci_driver i40evf_driver = {
3155 .name = i40evf_driver_name,
3156 .id_table = i40evf_pci_tbl,
3157 .probe = i40evf_probe,
3158 .remove = i40evf_remove,
3159#ifdef CONFIG_PM
3160 .suspend = i40evf_suspend,
3161 .resume = i40evf_resume,
3162#endif
3163 .shutdown = i40evf_shutdown,
3164};
3165
3166/**
3167 * i40e_init_module - Driver Registration Routine
3168 *
3169 * i40e_init_module is the first routine called when the driver is
3170 * loaded. All it does is register with the PCI subsystem.
3171 **/
3172static int __init i40evf_init_module(void)
3173{
3174 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003175
Greg Rose5eae00c2013-12-21 06:12:45 +00003176 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003177 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003178
3179 pr_info("%s\n", i40evf_copyright);
3180
Jacob Keller6992a6c2016-08-04 11:37:01 -07003181 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3182 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003183 if (!i40evf_wq) {
3184 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3185 return -ENOMEM;
3186 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003187 ret = pci_register_driver(&i40evf_driver);
3188 return ret;
3189}
3190
3191module_init(i40evf_init_module);
3192
3193/**
3194 * i40e_exit_module - Driver Exit Cleanup Routine
3195 *
3196 * i40e_exit_module is called just before the driver is removed
3197 * from memory.
3198 **/
3199static void __exit i40evf_exit_module(void)
3200{
3201 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003202 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003203}
3204
3205module_exit(i40evf_exit_module);
3206
3207/* i40evf_main.c */