blob: f2f1e754c2ce31e74a572f711d891d994cd68811 [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;
Greg Rose5eae00c2013-12-21 06:12:45 +0000518
519 i40evf_irq_disable(adapter);
520 /* Decrement for Other and TCP Timer vectors */
521 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
522
523 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400524 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700525 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000526
527 if (q_vector->tx.ring && q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400528 snprintf(q_vector->name, sizeof(q_vector->name),
529 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000530 tx_int_idx++;
531 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400532 snprintf(q_vector->name, sizeof(q_vector->name),
533 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000534 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400535 snprintf(q_vector->name, sizeof(q_vector->name),
536 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000537 } else {
538 /* skip this unused q_vector */
539 continue;
540 }
Alan Brady96db7762016-09-14 16:24:38 -0700541 err = request_irq(irq_num,
542 i40evf_msix_clean_rings,
543 0,
544 q_vector->name,
545 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000546 if (err) {
547 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400548 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000549 goto free_queue_irqs;
550 }
Alan Brady96db7762016-09-14 16:24:38 -0700551 /* register for affinity change notifications */
552 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
553 q_vector->affinity_notify.release =
554 i40evf_irq_affinity_release;
555 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Keller759dc4a2017-07-14 09:10:10 -0400556 /* get_cpu_mask returns a static constant mask with
557 * a permanent lifetime so it's ok to use here.
558 */
559 irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
Greg Rose5eae00c2013-12-21 06:12:45 +0000560 }
561
562 return 0;
563
564free_queue_irqs:
565 while (vector) {
566 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700567 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
568 irq_set_affinity_notifier(irq_num, NULL);
569 irq_set_affinity_hint(irq_num, NULL);
570 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000571 }
572 return err;
573}
574
575/**
576 * i40evf_request_misc_irq - Initialize MSI-X interrupts
577 * @adapter: board private structure
578 *
579 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
580 * vector is only for the admin queue, and stays active even when the netdev
581 * is closed.
582 **/
583static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
584{
585 struct net_device *netdev = adapter->netdev;
586 int err;
587
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700588 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000589 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
590 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000591 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800592 &i40evf_msix_aq, 0,
593 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000594 if (err) {
595 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800596 "request_irq for %s failed: %d\n",
597 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000598 free_irq(adapter->msix_entries[0].vector, netdev);
599 }
600 return err;
601}
602
603/**
604 * i40evf_free_traffic_irqs - Free MSI-X interrupts
605 * @adapter: board private structure
606 *
607 * Frees all MSI-X vectors other than 0.
608 **/
609static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
610{
Alan Brady96db7762016-09-14 16:24:38 -0700611 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000612
Alan Brady47d2a5d2016-11-08 13:05:05 -0800613 if (!adapter->msix_entries)
614 return;
615
Greg Rose5eae00c2013-12-21 06:12:45 +0000616 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
617
Alan Brady96db7762016-09-14 16:24:38 -0700618 for (vector = 0; vector < q_vectors; vector++) {
619 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
620 irq_set_affinity_notifier(irq_num, NULL);
621 irq_set_affinity_hint(irq_num, NULL);
622 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000623 }
624}
625
626/**
627 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
628 * @adapter: board private structure
629 *
630 * Frees MSI-X vector 0.
631 **/
632static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
633{
634 struct net_device *netdev = adapter->netdev;
635
Jacob Kelleref4603e2016-11-08 13:05:08 -0800636 if (!adapter->msix_entries)
637 return;
638
Greg Rose5eae00c2013-12-21 06:12:45 +0000639 free_irq(adapter->msix_entries[0].vector, netdev);
640}
641
642/**
643 * i40evf_configure_tx - Configure Transmit Unit after Reset
644 * @adapter: board private structure
645 *
646 * Configure the Tx unit of the MAC after a reset.
647 **/
648static void i40evf_configure_tx(struct i40evf_adapter *adapter)
649{
650 struct i40e_hw *hw = &adapter->hw;
651 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000652
Mitch Williamscc052922014-10-25 03:24:34 +0000653 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400654 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000655}
656
657/**
658 * i40evf_configure_rx - Configure Receive Unit after Reset
659 * @adapter: board private structure
660 *
661 * Configure the Rx unit of the MAC after a reset.
662 **/
663static void i40evf_configure_rx(struct i40evf_adapter *adapter)
664{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700665 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000666 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000667 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000668
Alexander Duyckdab86af2017-03-14 10:15:27 -0700669 /* Legacy Rx will always default to a 2048 buffer size. */
670#if (PAGE_SIZE < 8192)
671 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200672 struct net_device *netdev = adapter->netdev;
673
Alexander Duyck98efd692017-04-05 07:51:01 -0400674 /* For jumbo frames on systems with 4K pages we have to use
675 * an order 1 page, so we might as well increase the size
676 * of our Rx buffer to make better use of the available space
677 */
678 rx_buf_len = I40E_RXBUFFER_3072;
679
Alexander Duyckdab86af2017-03-14 10:15:27 -0700680 /* We use a 1536 buffer size for configurations with
681 * standard Ethernet mtu. On x86 this gives us enough room
682 * for shared info and 192 bytes of padding.
683 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400684 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
685 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700686 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
687 }
688#endif
689
Mitch Williamscc052922014-10-25 03:24:34 +0000690 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400691 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700692 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400693
694 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
695 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
696 else
697 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000698 }
699}
700
701/**
702 * i40evf_find_vlan - Search filter list for specific vlan filter
703 * @adapter: board private structure
704 * @vlan: vlan tag
705 *
706 * Returns ptr to the filter object or NULL
707 **/
708static struct
709i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
710{
711 struct i40evf_vlan_filter *f;
712
713 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
714 if (vlan == f->vlan)
715 return f;
716 }
717 return NULL;
718}
719
720/**
721 * i40evf_add_vlan - Add a vlan filter to the list
722 * @adapter: board private structure
723 * @vlan: VLAN tag
724 *
725 * Returns ptr to the filter object or NULL when no memory available.
726 **/
727static struct
728i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
729{
Mitch Williams13acb542015-03-31 00:45:05 -0700730 struct i40evf_vlan_filter *f = NULL;
731 int count = 50;
732
733 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
734 &adapter->crit_section)) {
735 udelay(1);
736 if (--count == 0)
737 goto out;
738 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000739
740 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000741 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000742 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000743 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700744 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000745
Greg Rose5eae00c2013-12-21 06:12:45 +0000746 f->vlan = vlan;
747
748 INIT_LIST_HEAD(&f->list);
749 list_add(&f->list, &adapter->vlan_filter_list);
750 f->add = true;
751 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
752 }
753
Mitch Williams13acb542015-03-31 00:45:05 -0700754clearout:
755 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
756out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000757 return f;
758}
759
760/**
761 * i40evf_del_vlan - Remove a vlan filter from the list
762 * @adapter: board private structure
763 * @vlan: VLAN tag
764 **/
765static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
766{
767 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700768 int count = 50;
769
770 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
771 &adapter->crit_section)) {
772 udelay(1);
773 if (--count == 0)
774 return;
775 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000776
777 f = i40evf_find_vlan(adapter, vlan);
778 if (f) {
779 f->remove = true;
780 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
781 }
Mitch Williams13acb542015-03-31 00:45:05 -0700782 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000783}
784
785/**
786 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
787 * @netdev: network device struct
788 * @vid: VLAN tag
789 **/
790static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000791 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000792{
793 struct i40evf_adapter *adapter = netdev_priv(netdev);
794
Mitch Williams8ed995f2015-08-28 17:55:57 -0400795 if (!VLAN_ALLOWED(adapter))
796 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000797 if (i40evf_add_vlan(adapter, vid) == NULL)
798 return -ENOMEM;
799 return 0;
800}
801
802/**
803 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
804 * @netdev: network device struct
805 * @vid: VLAN tag
806 **/
807static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000808 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000809{
810 struct i40evf_adapter *adapter = netdev_priv(netdev);
811
Mitch Williams8ed995f2015-08-28 17:55:57 -0400812 if (VLAN_ALLOWED(adapter)) {
813 i40evf_del_vlan(adapter, vid);
814 return 0;
815 }
816 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000817}
818
819/**
820 * i40evf_find_filter - Search filter list for specific mac filter
821 * @adapter: board private structure
822 * @macaddr: the MAC address
823 *
824 * Returns ptr to the filter object or NULL
825 **/
826static struct
827i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
828 u8 *macaddr)
829{
830 struct i40evf_mac_filter *f;
831
832 if (!macaddr)
833 return NULL;
834
835 list_for_each_entry(f, &adapter->mac_filter_list, list) {
836 if (ether_addr_equal(macaddr, f->macaddr))
837 return f;
838 }
839 return NULL;
840}
841
842/**
843 * i40e_add_filter - Add a mac filter to the filter list
844 * @adapter: board private structure
845 * @macaddr: the MAC address
846 *
847 * Returns ptr to the filter object or NULL when no memory available.
848 **/
849static struct
850i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
851 u8 *macaddr)
852{
853 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000854 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000855
856 if (!macaddr)
857 return NULL;
858
859 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000860 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000861 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000862 if (--count == 0)
863 return NULL;
864 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000865
866 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000867 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000868 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000869 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000870 clear_bit(__I40EVF_IN_CRITICAL_TASK,
871 &adapter->crit_section);
872 return NULL;
873 }
874
Greg Rose9a173902014-05-22 06:32:02 +0000875 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000876
Mitch Williams63590b62016-05-16 10:26:42 -0700877 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000878 f->add = true;
879 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
880 }
881
882 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
883 return f;
884}
885
886/**
887 * i40evf_set_mac - NDO callback to set port mac address
888 * @netdev: network interface device structure
889 * @p: pointer to an address structure
890 *
891 * Returns 0 on success, negative on failure
892 **/
893static int i40evf_set_mac(struct net_device *netdev, void *p)
894{
895 struct i40evf_adapter *adapter = netdev_priv(netdev);
896 struct i40e_hw *hw = &adapter->hw;
897 struct i40evf_mac_filter *f;
898 struct sockaddr *addr = p;
899
900 if (!is_valid_ether_addr(addr->sa_data))
901 return -EADDRNOTAVAIL;
902
903 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
904 return 0;
905
Mitch Williams14e52ee2015-08-31 19:54:44 -0400906 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
907 return -EPERM;
908
909 f = i40evf_find_filter(adapter, hw->mac.addr);
910 if (f) {
911 f->remove = true;
912 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
913 }
914
Greg Rose5eae00c2013-12-21 06:12:45 +0000915 f = i40evf_add_filter(adapter, addr->sa_data);
916 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000917 ether_addr_copy(hw->mac.addr, addr->sa_data);
918 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000919 }
920
921 return (f == NULL) ? -ENOMEM : 0;
922}
923
924/**
925 * i40evf_set_rx_mode - NDO callback to set the netdev filters
926 * @netdev: network interface device structure
927 **/
928static void i40evf_set_rx_mode(struct net_device *netdev)
929{
930 struct i40evf_adapter *adapter = netdev_priv(netdev);
931 struct i40evf_mac_filter *f, *ftmp;
932 struct netdev_hw_addr *uca;
933 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400934 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000935 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000936
937 /* add addr if not already in the filter list */
938 netdev_for_each_uc_addr(uca, netdev) {
939 i40evf_add_filter(adapter, uca->addr);
940 }
941 netdev_for_each_mc_addr(mca, netdev) {
942 i40evf_add_filter(adapter, mca->addr);
943 }
944
945 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000946 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000947 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000948 if (--count == 0) {
949 dev_err(&adapter->pdev->dev,
950 "Failed to get lock in %s\n", __func__);
951 return;
952 }
953 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000954 /* remove filter if not in netdev list */
955 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400956 netdev_for_each_mc_addr(mca, netdev)
957 if (ether_addr_equal(mca->addr, f->macaddr))
958 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000959
Shannon Nelson2f41f332015-08-26 15:14:20 -0400960 netdev_for_each_uc_addr(uca, netdev)
961 if (ether_addr_equal(uca->addr, f->macaddr))
962 goto bottom_of_search_loop;
963
964 for_each_dev_addr(netdev, ha)
965 if (ether_addr_equal(ha->addr, f->macaddr))
966 goto bottom_of_search_loop;
967
968 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
969 goto bottom_of_search_loop;
970
971 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
972 f->remove = true;
973 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
974
975bottom_of_search_loop:
976 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000977 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700978
979 if (netdev->flags & IFF_PROMISC &&
980 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
981 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
982 else if (!(netdev->flags & IFF_PROMISC) &&
983 adapter->flags & I40EVF_FLAG_PROMISC_ON)
984 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
985
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700986 if (netdev->flags & IFF_ALLMULTI &&
987 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
988 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
989 else if (!(netdev->flags & IFF_ALLMULTI) &&
990 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
991 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
992
Greg Rose5eae00c2013-12-21 06:12:45 +0000993 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
994}
995
996/**
997 * i40evf_napi_enable_all - enable NAPI on all queue vectors
998 * @adapter: board private structure
999 **/
1000static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1001{
1002 int q_idx;
1003 struct i40e_q_vector *q_vector;
1004 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1005
1006 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1007 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001008
Mitch Williams7d96ba12015-10-26 19:44:39 -04001009 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001010 napi = &q_vector->napi;
1011 napi_enable(napi);
1012 }
1013}
1014
1015/**
1016 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1017 * @adapter: board private structure
1018 **/
1019static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1020{
1021 int q_idx;
1022 struct i40e_q_vector *q_vector;
1023 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1024
1025 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001026 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001027 napi_disable(&q_vector->napi);
1028 }
1029}
1030
1031/**
1032 * i40evf_configure - set up transmit and receive data structures
1033 * @adapter: board private structure
1034 **/
1035static void i40evf_configure(struct i40evf_adapter *adapter)
1036{
1037 struct net_device *netdev = adapter->netdev;
1038 int i;
1039
1040 i40evf_set_rx_mode(netdev);
1041
1042 i40evf_configure_tx(adapter);
1043 i40evf_configure_rx(adapter);
1044 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1045
Mitch Williamscc052922014-10-25 03:24:34 +00001046 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001047 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001048
Mitch Williamsb1630982016-04-18 11:33:48 -07001049 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001050 }
1051}
1052
1053/**
1054 * i40evf_up_complete - Finish the last steps of bringing up a connection
1055 * @adapter: board private structure
1056 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001057static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001058{
1059 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001060 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001061
1062 i40evf_napi_enable_all(adapter);
1063
1064 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001065 if (CLIENT_ENABLED(adapter))
1066 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001067 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001068}
1069
1070/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001071 * i40e_down - Shutdown the connection processing
1072 * @adapter: board private structure
1073 **/
1074void i40evf_down(struct i40evf_adapter *adapter)
1075{
1076 struct net_device *netdev = adapter->netdev;
1077 struct i40evf_mac_filter *f;
1078
Mitch Williams209dc4d2015-12-09 15:50:27 -08001079 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001080 return;
1081
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001082 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1083 &adapter->crit_section))
1084 usleep_range(500, 1000);
1085
Mitch Williams63e18c22015-03-27 00:12:10 -07001086 netif_carrier_off(netdev);
1087 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001088 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001089 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001090 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001091
Mitch Williamsef8693e2014-02-13 03:48:53 -08001092 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001093 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1094 f->remove = true;
1095 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001096 /* remove all VLAN filters */
1097 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1098 f->remove = true;
1099 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001100 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1101 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001102 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001103 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001104 /* Schedule operations to close down the HW. Don't wait
1105 * here for this to complete. The watchdog is still running
1106 * and it will take care of this.
1107 */
1108 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001109 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001110 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001111 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001112
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001113 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001114 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001115}
1116
1117/**
1118 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1119 * @adapter: board private structure
1120 * @vectors: number of vectors to request
1121 *
1122 * Work with the OS to set up the MSIX vectors needed.
1123 *
1124 * Returns 0 on success, negative on failure
1125 **/
1126static int
1127i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1128{
1129 int err, vector_threshold;
1130
1131 /* We'll want at least 3 (vector_threshold):
1132 * 0) Other (Admin Queue and link, mostly)
1133 * 1) TxQ[0] Cleanup
1134 * 2) RxQ[0] Cleanup
1135 */
1136 vector_threshold = MIN_MSIX_COUNT;
1137
1138 /* The more we get, the more we will assign to Tx/Rx Cleanup
1139 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1140 * Right now, we simply care about how many we'll get; we'll
1141 * set them up later while requesting irq's.
1142 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001143 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1144 vector_threshold, vectors);
1145 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001146 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001147 kfree(adapter->msix_entries);
1148 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001149 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001150 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001151
1152 /* Adjust for only the vectors we'll use, which is minimum
1153 * of max_msix_q_vectors + NONQ_VECS, or the number of
1154 * vectors we were allocated.
1155 */
1156 adapter->num_msix_vectors = err;
1157 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001158}
1159
1160/**
1161 * i40evf_free_queues - Free memory for all rings
1162 * @adapter: board private structure to initialize
1163 *
1164 * Free all of the memory associated with queue pairs.
1165 **/
1166static void i40evf_free_queues(struct i40evf_adapter *adapter)
1167{
Greg Rose5eae00c2013-12-21 06:12:45 +00001168 if (!adapter->vsi_res)
1169 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001170 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001171 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001172 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001173 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001174 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001175}
1176
1177/**
1178 * i40evf_alloc_queues - Allocate memory for all rings
1179 * @adapter: board private structure to initialize
1180 *
1181 * We allocate one ring per queue at run-time since we don't know the
1182 * number of queues at compile-time. The polling_netdev array is
1183 * intended for Multiqueue, but should work fine with a single queue.
1184 **/
1185static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1186{
Jacob Keller65c70062017-06-07 05:43:01 -04001187 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001188
Alan Brady5b36e8d2017-08-22 06:57:50 -04001189 /* If we're in reset reallocating queues we don't actually know yet for
1190 * certain the PF gave us the number of queues we asked for but we'll
1191 * assume it did. Once basic reset is finished we'll confirm once we
1192 * start negotiating config with PF.
1193 */
1194 if (adapter->num_req_queues)
1195 num_active_queues = adapter->num_req_queues;
1196 else
1197 num_active_queues = min_t(int,
1198 adapter->vsi_res->num_queue_pairs,
1199 (int)(num_online_cpus()));
1200
Jacob Keller65c70062017-06-07 05:43:01 -04001201
1202 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001203 sizeof(struct i40e_ring), GFP_KERNEL);
1204 if (!adapter->tx_rings)
1205 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001206 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001207 sizeof(struct i40e_ring), GFP_KERNEL);
1208 if (!adapter->rx_rings)
1209 goto err_out;
1210
Jacob Keller65c70062017-06-07 05:43:01 -04001211 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001212 struct i40e_ring *tx_ring;
1213 struct i40e_ring *rx_ring;
1214
Mitch Williams0dd438d2015-10-26 19:44:40 -04001215 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001216
1217 tx_ring->queue_index = i;
1218 tx_ring->netdev = adapter->netdev;
1219 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001220 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001221 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001222 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001223 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001224
Mitch Williams0dd438d2015-10-26 19:44:40 -04001225 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001226 rx_ring->queue_index = i;
1227 rx_ring->netdev = adapter->netdev;
1228 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001229 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001230 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001231 }
1232
Jacob Keller65c70062017-06-07 05:43:01 -04001233 adapter->num_active_queues = num_active_queues;
1234
Greg Rose5eae00c2013-12-21 06:12:45 +00001235 return 0;
1236
1237err_out:
1238 i40evf_free_queues(adapter);
1239 return -ENOMEM;
1240}
1241
1242/**
1243 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1244 * @adapter: board private structure to initialize
1245 *
1246 * Attempt to configure the interrupts using the best available
1247 * capabilities of the hardware and the kernel.
1248 **/
1249static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1250{
1251 int vector, v_budget;
1252 int pairs = 0;
1253 int err = 0;
1254
1255 if (!adapter->vsi_res) {
1256 err = -EIO;
1257 goto out;
1258 }
Mitch Williamscc052922014-10-25 03:24:34 +00001259 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001260
Jacob Keller789f38c2017-04-19 09:25:56 -04001261 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1262 * us much good if we have more vectors than CPUs. However, we already
1263 * limit the total number of queues by the number of CPUs so we do not
1264 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001265 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001266 v_budget = min_t(int, pairs + NONQ_VECS,
1267 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001268
Greg Rose5eae00c2013-12-21 06:12:45 +00001269 adapter->msix_entries = kcalloc(v_budget,
1270 sizeof(struct msix_entry), GFP_KERNEL);
1271 if (!adapter->msix_entries) {
1272 err = -ENOMEM;
1273 goto out;
1274 }
1275
1276 for (vector = 0; vector < v_budget; vector++)
1277 adapter->msix_entries[vector].entry = vector;
1278
Mitch Williams313ed2d2015-08-27 11:42:31 -04001279 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001280
1281out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001282 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1283 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001284 return err;
1285}
1286
1287/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001288 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1289 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001290 *
1291 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001292 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001293static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001294{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001295 struct i40e_aqc_get_set_rss_key_data *rss_key =
1296 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001297 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001298 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001299
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001300 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001301 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001302 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001303 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001304 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001305 }
1306
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001307 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1308 if (ret) {
1309 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1310 i40evf_stat_str(hw, ret),
1311 i40evf_aq_str(hw, hw->aq.asq_last_status));
1312 return ret;
1313
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001314 }
1315
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001316 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1317 adapter->rss_lut, adapter->rss_lut_size);
1318 if (ret) {
1319 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1320 i40evf_stat_str(hw, ret),
1321 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001322 }
1323
1324 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001325
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001326}
1327
1328/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001329 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001330 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001331 *
1332 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001333 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001334static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001335{
1336 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001337 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001338 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001339
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001340 dw = (u32 *)adapter->rss_key;
1341 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1342 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001343
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001344 dw = (u32 *)adapter->rss_lut;
1345 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1346 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001347
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001348 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001349
1350 return 0;
1351}
1352
1353/**
1354 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001355 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001356 *
1357 * Returns 0 on success, negative on failure
1358 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001359int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001360{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001361
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001362 if (RSS_PF(adapter)) {
1363 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1364 I40EVF_FLAG_AQ_SET_RSS_KEY;
1365 return 0;
1366 } else if (RSS_AQ(adapter)) {
1367 return i40evf_config_rss_aq(adapter);
1368 } else {
1369 return i40evf_config_rss_reg(adapter);
1370 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001371}
1372
1373/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001374 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001375 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001376 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001377static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001378{
1379 u16 i;
1380
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001381 for (i = 0; i < adapter->rss_lut_size; i++)
1382 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001383}
1384
1385/**
Helin Zhang96a81982015-10-26 19:44:31 -04001386 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001387 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001388 *
1389 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001390 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001391static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001392{
1393 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001394 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001395
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001396 if (!RSS_PF(adapter)) {
1397 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001398 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001399 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001400 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1401 else
1402 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001403
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001404 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1405 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1406 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001407
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001408 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001409
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001410 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1411 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001412
1413 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001414}
1415
1416/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001417 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1418 * @adapter: board private structure to initialize
1419 *
1420 * We allocate one q_vector per queue interrupt. If allocation fails we
1421 * return -ENOMEM.
1422 **/
1423static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1424{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001425 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001426 struct i40e_q_vector *q_vector;
1427
1428 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001429 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001430 GFP_KERNEL);
1431 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001432 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001433
1434 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001435 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001436 q_vector->adapter = adapter;
1437 q_vector->vsi = &adapter->vsi;
1438 q_vector->v_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001439 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001440 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001441 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001442 }
1443
1444 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001445}
1446
1447/**
1448 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1449 * @adapter: board private structure to initialize
1450 *
1451 * This function frees the memory allocated to the q_vectors. In addition if
1452 * NAPI is enabled it will delete any references to the NAPI struct prior
1453 * to freeing the q_vector.
1454 **/
1455static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1456{
1457 int q_idx, num_q_vectors;
1458 int napi_vectors;
1459
Jacob Kelleref4603e2016-11-08 13:05:08 -08001460 if (!adapter->q_vectors)
1461 return;
1462
Greg Rose5eae00c2013-12-21 06:12:45 +00001463 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001464 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001465
1466 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001467 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001468 if (q_idx < napi_vectors)
1469 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001470 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001471 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001472 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001473}
1474
1475/**
1476 * i40evf_reset_interrupt_capability - Reset MSIX setup
1477 * @adapter: board private structure
1478 *
1479 **/
1480void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1481{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001482 if (!adapter->msix_entries)
1483 return;
1484
Greg Rose5eae00c2013-12-21 06:12:45 +00001485 pci_disable_msix(adapter->pdev);
1486 kfree(adapter->msix_entries);
1487 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001488}
1489
1490/**
1491 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1492 * @adapter: board private structure to initialize
1493 *
1494 **/
1495int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1496{
1497 int err;
1498
Jacob Keller283aeaf2017-04-19 09:25:59 -04001499 err = i40evf_alloc_queues(adapter);
1500 if (err) {
1501 dev_err(&adapter->pdev->dev,
1502 "Unable to allocate memory for queues\n");
1503 goto err_alloc_queues;
1504 }
1505
Jacob Keller62fe2a82016-07-27 12:02:33 -07001506 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001507 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001508 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001509 if (err) {
1510 dev_err(&adapter->pdev->dev,
1511 "Unable to setup interrupt capabilities\n");
1512 goto err_set_interrupt;
1513 }
1514
1515 err = i40evf_alloc_q_vectors(adapter);
1516 if (err) {
1517 dev_err(&adapter->pdev->dev,
1518 "Unable to allocate memory for queue vectors\n");
1519 goto err_alloc_q_vectors;
1520 }
1521
Greg Rose5eae00c2013-12-21 06:12:45 +00001522 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001523 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1524 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001525
1526 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001527err_alloc_q_vectors:
1528 i40evf_reset_interrupt_capability(adapter);
1529err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001530 i40evf_free_queues(adapter);
1531err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001532 return err;
1533}
1534
1535/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001536 * i40evf_free_rss - Free memory used by RSS structs
1537 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001538 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001539static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001540{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001541 kfree(adapter->rss_key);
1542 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001543
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001544 kfree(adapter->rss_lut);
1545 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001546}
1547
1548/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001549 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1550 * @adapter: board private structure
1551 *
1552 * Returns 0 on success, negative on failure
1553 **/
1554static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1555{
1556 struct net_device *netdev = adapter->netdev;
1557 int err;
1558
1559 if (netif_running(netdev))
1560 i40evf_free_traffic_irqs(adapter);
1561 i40evf_free_misc_irq(adapter);
1562 i40evf_reset_interrupt_capability(adapter);
1563 i40evf_free_q_vectors(adapter);
1564 i40evf_free_queues(adapter);
1565
1566 err = i40evf_init_interrupt_scheme(adapter);
1567 if (err)
1568 goto err;
1569
1570 netif_tx_stop_all_queues(netdev);
1571
1572 err = i40evf_request_misc_irq(adapter);
1573 if (err)
1574 goto err;
1575
1576 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1577
Mitch Williams1b7b7592017-08-22 06:57:51 -04001578 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001579
1580 if (RSS_AQ(adapter))
1581 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1582 else
1583 err = i40evf_init_rss(adapter);
1584err:
1585 return err;
1586}
1587
1588/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001589 * i40evf_watchdog_timer - Periodic call-back timer
1590 * @data: pointer to adapter disguised as unsigned long
1591 **/
1592static void i40evf_watchdog_timer(unsigned long data)
1593{
1594 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001595
Greg Rose5eae00c2013-12-21 06:12:45 +00001596 schedule_work(&adapter->watchdog_task);
1597 /* timer will be rescheduled in watchdog task */
1598}
1599
1600/**
1601 * i40evf_watchdog_task - Periodic call-back task
1602 * @work: pointer to work_struct
1603 **/
1604static void i40evf_watchdog_task(struct work_struct *work)
1605{
1606 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001607 struct i40evf_adapter,
1608 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001609 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001610 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001611
Greg Rose5eae00c2013-12-21 06:12:45 +00001612 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001613 goto restart_watchdog;
1614
1615 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001616 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1617 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001618 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1619 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001620 /* A chance for redemption! */
1621 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1622 adapter->state = __I40EVF_STARTUP;
1623 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1624 schedule_delayed_work(&adapter->init_task, 10);
1625 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1626 &adapter->crit_section);
1627 /* Don't reschedule the watchdog, since we've restarted
1628 * the init task. When init_task contacts the PF and
1629 * gets everything set up again, it'll restart the
1630 * watchdog for us. Down, boy. Sit. Stay. Woof.
1631 */
1632 return;
1633 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001634 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001635 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001636 goto watchdog_done;
1637 }
1638
1639 if ((adapter->state < __I40EVF_DOWN) ||
1640 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001641 goto watchdog_done;
1642
Mitch Williamsef8693e2014-02-13 03:48:53 -08001643 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001644 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1645 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001646 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001647 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001648 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001649 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001650 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001651 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001652 goto watchdog_done;
1653 }
1654
1655 /* Process admin queue tasks. After init, everything gets done
1656 * here so we don't race on the admin queue.
1657 */
Mitch Williamsed636962015-04-07 19:45:32 -04001658 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001659 if (!i40evf_asq_done(hw)) {
1660 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1661 i40evf_send_api_ver(adapter);
1662 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001663 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001664 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001665 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1666 i40evf_send_vf_config_msg(adapter);
1667 goto watchdog_done;
1668 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001669
Mitch Williamse284fc82015-03-27 00:12:09 -07001670 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1671 i40evf_disable_queues(adapter);
1672 goto watchdog_done;
1673 }
1674
Greg Rose5eae00c2013-12-21 06:12:45 +00001675 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1676 i40evf_map_queues(adapter);
1677 goto watchdog_done;
1678 }
1679
1680 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1681 i40evf_add_ether_addrs(adapter);
1682 goto watchdog_done;
1683 }
1684
1685 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1686 i40evf_add_vlans(adapter);
1687 goto watchdog_done;
1688 }
1689
1690 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1691 i40evf_del_ether_addrs(adapter);
1692 goto watchdog_done;
1693 }
1694
1695 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1696 i40evf_del_vlans(adapter);
1697 goto watchdog_done;
1698 }
1699
Mariusz Stachura87743702017-07-17 22:09:45 -07001700 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1701 i40evf_enable_vlan_stripping(adapter);
1702 goto watchdog_done;
1703 }
1704
1705 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1706 i40evf_disable_vlan_stripping(adapter);
1707 goto watchdog_done;
1708 }
1709
Greg Rose5eae00c2013-12-21 06:12:45 +00001710 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1711 i40evf_configure_queues(adapter);
1712 goto watchdog_done;
1713 }
1714
1715 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1716 i40evf_enable_queues(adapter);
1717 goto watchdog_done;
1718 }
1719
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001720 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1721 /* This message goes straight to the firmware, not the
1722 * PF, so we don't have to set current_op as we will
1723 * not get a response through the ARQ.
1724 */
Helin Zhang96a81982015-10-26 19:44:31 -04001725 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001726 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1727 goto watchdog_done;
1728 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001729 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1730 i40evf_get_hena(adapter);
1731 goto watchdog_done;
1732 }
1733 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1734 i40evf_set_hena(adapter);
1735 goto watchdog_done;
1736 }
1737 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1738 i40evf_set_rss_key(adapter);
1739 goto watchdog_done;
1740 }
1741 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1742 i40evf_set_rss_lut(adapter);
1743 goto watchdog_done;
1744 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001745
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001746 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001747 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1748 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001749 goto watchdog_done;
1750 }
1751
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001752 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001753 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001754 goto watchdog_done;
1755 }
1756
1757 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1758 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001759 i40evf_set_promiscuous(adapter, 0);
1760 goto watchdog_done;
1761 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001762 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001763
Greg Rose5eae00c2013-12-21 06:12:45 +00001764 if (adapter->state == __I40EVF_RUNNING)
1765 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001766watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001767 if (adapter->state == __I40EVF_RUNNING) {
1768 i40evf_irq_enable_queues(adapter, ~0);
1769 i40evf_fire_sw_int(adapter, 0xFF);
1770 } else {
1771 i40evf_fire_sw_int(adapter, 0x1);
1772 }
1773
Mitch Williamsef8693e2014-02-13 03:48:53 -08001774 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1775restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001776 if (adapter->state == __I40EVF_REMOVE)
1777 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001778 if (adapter->aq_required)
1779 mod_timer(&adapter->watchdog_timer,
1780 jiffies + msecs_to_jiffies(20));
1781 else
1782 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001783 schedule_work(&adapter->adminq_task);
1784}
1785
Joe Perchesdedecb62016-11-01 15:35:14 -07001786static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1787{
1788 struct i40evf_mac_filter *f, *ftmp;
1789 struct i40evf_vlan_filter *fv, *fvtmp;
1790
1791 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1792
1793 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001794 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001795 netif_carrier_off(adapter->netdev);
1796 netif_tx_disable(adapter->netdev);
1797 adapter->link_up = false;
1798 i40evf_napi_disable_all(adapter);
1799 i40evf_irq_disable(adapter);
1800 i40evf_free_traffic_irqs(adapter);
1801 i40evf_free_all_tx_resources(adapter);
1802 i40evf_free_all_rx_resources(adapter);
1803 }
1804
1805 /* Delete all of the filters, both MAC and VLAN. */
1806 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1807 list_del(&f->list);
1808 kfree(f);
1809 }
1810
1811 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1812 list_del(&fv->list);
1813 kfree(fv);
1814 }
1815
1816 i40evf_free_misc_irq(adapter);
1817 i40evf_reset_interrupt_capability(adapter);
1818 i40evf_free_queues(adapter);
1819 i40evf_free_q_vectors(adapter);
1820 kfree(adapter->vf_res);
1821 i40evf_shutdown_adminq(&adapter->hw);
1822 adapter->netdev->flags &= ~IFF_UP;
1823 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1824 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1825 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001826 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001827 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1828}
1829
Mitch Williams67c818a2015-06-19 08:56:30 -07001830#define I40EVF_RESET_WAIT_MS 10
1831#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001832/**
1833 * i40evf_reset_task - Call-back task to handle hardware reset
1834 * @work: pointer to work_struct
1835 *
1836 * During reset we need to shut down and reinitialize the admin queue
1837 * before we can use it to communicate with the PF again. We also clear
1838 * and reinit the rings because that context is lost as well.
1839 **/
1840static void i40evf_reset_task(struct work_struct *work)
1841{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001842 struct i40evf_adapter *adapter = container_of(work,
1843 struct i40evf_adapter,
1844 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001845 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001846 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001847 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001848 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001849 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001850 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001851
Mitch Williamsed0e8942017-01-24 10:23:59 -08001852 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001853 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001854 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001855 if (CLIENT_ENABLED(adapter)) {
1856 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1857 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1858 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1859 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1860 cancel_delayed_work_sync(&adapter->client_task);
1861 i40evf_notify_client_close(&adapter->vsi, true);
1862 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001863 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001864 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001865 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1866 /* Restart the AQ here. If we have been reset but didn't
1867 * detect it, or if the PF had to reinit, our AQ will be hosed.
1868 */
1869 i40evf_shutdown_adminq(hw);
1870 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001871 i40evf_request_reset(adapter);
1872 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001873 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001874
Mitch Williamsef8693e2014-02-13 03:48:53 -08001875 /* poll until we see the reset actually happen */
1876 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001877 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1878 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1879 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001880 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001881 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001882 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001883 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001884 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001885 goto continue_reset; /* act like the reset happened */
1886 }
1887
1888 /* wait until the reset is complete and the PF is responding to us */
1889 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001890 /* sleep first to make sure a minimum wait time is met */
1891 msleep(I40EVF_RESET_WAIT_MS);
1892
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001893 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1894 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001895 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001896 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001897 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001898
Mitch Williams509a4472015-12-23 12:05:52 -08001899 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001900
Mitch Williamsef8693e2014-02-13 03:48:53 -08001901 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001902 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001903 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001904 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001905 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001906 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001907 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001908
1909continue_reset:
Alan Bradyf0db7892017-07-12 05:46:04 -04001910 if (netif_running(netdev)) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001911 netif_carrier_off(netdev);
1912 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001913 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001914 i40evf_napi_disable_all(adapter);
1915 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001916 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001917
Greg Rose5eae00c2013-12-21 06:12:45 +00001918 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001919 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1920
1921 /* free the Tx/Rx rings and descriptors, might be better to just
1922 * re-use them sometime in the future
1923 */
1924 i40evf_free_all_rx_resources(adapter);
1925 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001926
1927 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001928 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001929 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001930 err = i40evf_init_adminq(hw);
1931 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001932 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1933 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001934 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001935
Alan Brady5b36e8d2017-08-22 06:57:50 -04001936 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1937 err = i40evf_reinit_interrupt_scheme(adapter);
1938 if (err)
1939 goto reset_err;
1940 }
1941
1942 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001943 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001944
1945 /* re-add all MAC filters */
1946 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1947 f->add = true;
1948 }
1949 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001950 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1951 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001952 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001953 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001954 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001955 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001956 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001957 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001958
1959 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1960
1961 if (netif_running(adapter->netdev)) {
1962 /* allocate transmit descriptors */
1963 err = i40evf_setup_all_tx_resources(adapter);
1964 if (err)
1965 goto reset_err;
1966
1967 /* allocate receive descriptors */
1968 err = i40evf_setup_all_rx_resources(adapter);
1969 if (err)
1970 goto reset_err;
1971
Alan Brady5b36e8d2017-08-22 06:57:50 -04001972 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1973 err = i40evf_request_traffic_irqs(adapter,
1974 netdev->name);
1975 if (err)
1976 goto reset_err;
1977
1978 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1979 }
1980
Greg Rose5eae00c2013-12-21 06:12:45 +00001981 i40evf_configure(adapter);
1982
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001983 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001984
1985 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001986 } else {
1987 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001988 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001989 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001990
Greg Rose5eae00c2013-12-21 06:12:45 +00001991 return;
1992reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001993 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001994 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001995}
1996
1997/**
1998 * i40evf_adminq_task - worker thread to clean the admin queue
1999 * @work: pointer to work_struct containing our data
2000 **/
2001static void i40evf_adminq_task(struct work_struct *work)
2002{
2003 struct i40evf_adapter *adapter =
2004 container_of(work, struct i40evf_adapter, adminq_task);
2005 struct i40e_hw *hw = &adapter->hw;
2006 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07002007 enum virtchnl_ops v_op;
2008 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00002009 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00002010 u16 pending;
2011
Mitch Williamsef8693e2014-02-13 03:48:53 -08002012 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00002013 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08002014
Mitch Williams1001dc32014-11-11 20:02:19 +00002015 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2016 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00002017 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00002018 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00002019
Greg Rose5eae00c2013-12-21 06:12:45 +00002020 do {
2021 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002022 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2023 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2024
2025 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002026 break; /* No event to process or error cleaning ARQ */
2027
Tushar Davec969ef42017-06-22 09:44:32 -07002028 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002029 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002030 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002031 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002032 } while (pending);
2033
Mitch Williams67c818a2015-06-19 08:56:30 -07002034 if ((adapter->flags &
2035 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2036 adapter->state == __I40EVF_RESETTING)
2037 goto freedom;
2038
Mitch Williams912257e2014-05-22 06:32:07 +00002039 /* check for error indications */
2040 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002041 if (val == 0xdeadbeef) /* indicates device in reset */
2042 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002043 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002044 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002045 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002046 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002047 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002048 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002049 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002050 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002051 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002052 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002053 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002054 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002055 }
2056 if (oldval != val)
2057 wr32(hw, hw->aq.arq.len, val);
2058
2059 val = rd32(hw, hw->aq.asq.len);
2060 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002061 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002062 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002063 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002064 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002065 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002066 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002067 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002068 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002069 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002070 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002071 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002072 }
2073 if (oldval != val)
2074 wr32(hw, hw->aq.asq.len, val);
2075
Mitch Williams67c818a2015-06-19 08:56:30 -07002076freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002077 kfree(event.msg_buf);
2078out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002079 /* re-enable Admin queue interrupt cause */
2080 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002081}
2082
2083/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002084 * i40evf_client_task - worker thread to perform client work
2085 * @work: pointer to work_struct containing our data
2086 *
2087 * This task handles client interactions. Because client calls can be
2088 * reentrant, we can't handle them in the watchdog.
2089 **/
2090static void i40evf_client_task(struct work_struct *work)
2091{
2092 struct i40evf_adapter *adapter =
2093 container_of(work, struct i40evf_adapter, client_task.work);
2094
2095 /* If we can't get the client bit, just give up. We'll be rescheduled
2096 * later.
2097 */
2098
2099 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2100 return;
2101
2102 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2103 i40evf_client_subtask(adapter);
2104 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2105 goto out;
2106 }
2107 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2108 i40evf_notify_client_close(&adapter->vsi, false);
2109 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2110 goto out;
2111 }
2112 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2113 i40evf_notify_client_open(&adapter->vsi);
2114 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2115 goto out;
2116 }
2117 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2118 i40evf_notify_client_l2_params(&adapter->vsi);
2119 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2120 }
2121out:
2122 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2123}
2124
2125/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002126 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2127 * @adapter: board private structure
2128 *
2129 * Free all transmit software resources
2130 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002131void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002132{
2133 int i;
2134
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002135 if (!adapter->tx_rings)
2136 return;
2137
Mitch Williamscc052922014-10-25 03:24:34 +00002138 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002139 if (adapter->tx_rings[i].desc)
2140 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002141}
2142
2143/**
2144 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2145 * @adapter: board private structure
2146 *
2147 * If this function returns with an error, then it's possible one or
2148 * more of the rings is populated (while the rest are not). It is the
2149 * callers duty to clean those orphaned rings.
2150 *
2151 * Return 0 on success, negative on failure
2152 **/
2153static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2154{
2155 int i, err = 0;
2156
Mitch Williamscc052922014-10-25 03:24:34 +00002157 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002158 adapter->tx_rings[i].count = adapter->tx_desc_count;
2159 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002160 if (!err)
2161 continue;
2162 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002163 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002164 break;
2165 }
2166
2167 return err;
2168}
2169
2170/**
2171 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2172 * @adapter: board private structure
2173 *
2174 * If this function returns with an error, then it's possible one or
2175 * more of the rings is populated (while the rest are not). It is the
2176 * callers duty to clean those orphaned rings.
2177 *
2178 * Return 0 on success, negative on failure
2179 **/
2180static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2181{
2182 int i, err = 0;
2183
Mitch Williamscc052922014-10-25 03:24:34 +00002184 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002185 adapter->rx_rings[i].count = adapter->rx_desc_count;
2186 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002187 if (!err)
2188 continue;
2189 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002190 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002191 break;
2192 }
2193 return err;
2194}
2195
2196/**
2197 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2198 * @adapter: board private structure
2199 *
2200 * Free all receive software resources
2201 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002202void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002203{
2204 int i;
2205
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002206 if (!adapter->rx_rings)
2207 return;
2208
Mitch Williamscc052922014-10-25 03:24:34 +00002209 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002210 if (adapter->rx_rings[i].desc)
2211 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002212}
2213
2214/**
2215 * i40evf_open - Called when a network interface is made active
2216 * @netdev: network interface device structure
2217 *
2218 * Returns 0 on success, negative value on failure
2219 *
2220 * The open entry point is called when a network interface is made
2221 * active by the system (IFF_UP). At this point all resources needed
2222 * for transmit and receive operations are allocated, the interrupt
2223 * handler is registered with the OS, the watchdog timer is started,
2224 * and the stack is notified that the interface is ready.
2225 **/
2226static int i40evf_open(struct net_device *netdev)
2227{
2228 struct i40evf_adapter *adapter = netdev_priv(netdev);
2229 int err;
2230
Mitch Williamsef8693e2014-02-13 03:48:53 -08002231 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2232 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2233 return -EIO;
2234 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002235
2236 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002237 return -EBUSY;
2238
2239 /* allocate transmit descriptors */
2240 err = i40evf_setup_all_tx_resources(adapter);
2241 if (err)
2242 goto err_setup_tx;
2243
2244 /* allocate receive descriptors */
2245 err = i40evf_setup_all_rx_resources(adapter);
2246 if (err)
2247 goto err_setup_rx;
2248
2249 /* clear any pending interrupts, may auto mask */
2250 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2251 if (err)
2252 goto err_req_irq;
2253
Mitch Williams44151cd2015-04-27 14:57:17 -04002254 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002255 i40evf_configure(adapter);
2256
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002257 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002258
2259 i40evf_irq_enable(adapter, true);
2260
2261 return 0;
2262
2263err_req_irq:
2264 i40evf_down(adapter);
2265 i40evf_free_traffic_irqs(adapter);
2266err_setup_rx:
2267 i40evf_free_all_rx_resources(adapter);
2268err_setup_tx:
2269 i40evf_free_all_tx_resources(adapter);
2270
2271 return err;
2272}
2273
2274/**
2275 * i40evf_close - Disables a network interface
2276 * @netdev: network interface device structure
2277 *
2278 * Returns 0, this is not allowed to fail
2279 *
2280 * The close entry point is called when an interface is de-activated
2281 * by the OS. The hardware is still under the drivers control, but
2282 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2283 * are freed, along with all transmit and receive resources.
2284 **/
2285static int i40evf_close(struct net_device *netdev)
2286{
2287 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002288 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002289
Mitch Williams209dc4d2015-12-09 15:50:27 -08002290 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002291 return 0;
2292
Mitch Williamsef8693e2014-02-13 03:48:53 -08002293
Jacob Keller0da36b92017-04-19 09:25:55 -04002294 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002295 if (CLIENT_ENABLED(adapter))
2296 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002297
2298 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002299 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002300 i40evf_free_traffic_irqs(adapter);
2301
Mitch Williams51f38262016-12-12 15:44:11 -08002302 /* We explicitly don't free resources here because the hardware is
2303 * still active and can DMA into memory. Resources are cleared in
2304 * i40evf_virtchnl_completion() after we get confirmation from the PF
2305 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002306 *
2307 * Also, we wait for state to transition to __I40EVF_DOWN before
2308 * returning. State change occurs in i40evf_virtchnl_completion() after
2309 * VF resources are released (which occurs after PF driver processes and
2310 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002311 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002312
2313 status = wait_event_timeout(adapter->down_waitqueue,
2314 adapter->state == __I40EVF_DOWN,
2315 msecs_to_jiffies(200));
2316 if (!status)
2317 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002318 return 0;
2319}
2320
2321/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002322 * i40evf_change_mtu - Change the Maximum Transfer Unit
2323 * @netdev: network interface device structure
2324 * @new_mtu: new value for maximum frame size
2325 *
2326 * Returns 0 on success, negative on failure
2327 **/
2328static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2329{
2330 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002331
Greg Rose5eae00c2013-12-21 06:12:45 +00002332 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002333 if (CLIENT_ENABLED(adapter)) {
2334 i40evf_notify_client_l2_params(&adapter->vsi);
2335 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2336 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002337 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2338 schedule_work(&adapter->reset_task);
2339
Greg Rose5eae00c2013-12-21 06:12:45 +00002340 return 0;
2341}
2342
Alexander Duyck06fc0162016-10-25 16:08:47 -07002343/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002344 * i40e_set_features - set the netdev feature flags
2345 * @netdev: ptr to the netdev being adjusted
2346 * @features: the feature set that the stack is suggesting
2347 * Note: expects to be called while under rtnl_lock()
2348 **/
2349static int i40evf_set_features(struct net_device *netdev,
2350 netdev_features_t features)
2351{
2352 struct i40evf_adapter *adapter = netdev_priv(netdev);
2353
2354 if (!VLAN_ALLOWED(adapter))
2355 return -EINVAL;
2356
2357 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2358 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2359 else
2360 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2361
2362 return 0;
2363}
2364
2365/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002366 * i40evf_features_check - Validate encapsulated packet conforms to limits
2367 * @skb: skb buff
2368 * @netdev: This physical port's netdev
2369 * @features: Offload features that the stack believes apply
2370 **/
2371static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2372 struct net_device *dev,
2373 netdev_features_t features)
2374{
2375 size_t len;
2376
2377 /* No point in doing any of this if neither checksum nor GSO are
2378 * being requested for this frame. We can rule out both by just
2379 * checking for CHECKSUM_PARTIAL
2380 */
2381 if (skb->ip_summed != CHECKSUM_PARTIAL)
2382 return features;
2383
2384 /* We cannot support GSO if the MSS is going to be less than
2385 * 64 bytes. If it is then we need to drop support for GSO.
2386 */
2387 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2388 features &= ~NETIF_F_GSO_MASK;
2389
2390 /* MACLEN can support at most 63 words */
2391 len = skb_network_header(skb) - skb->data;
2392 if (len & ~(63 * 2))
2393 goto out_err;
2394
2395 /* IPLEN and EIPLEN can support at most 127 dwords */
2396 len = skb_transport_header(skb) - skb_network_header(skb);
2397 if (len & ~(127 * 4))
2398 goto out_err;
2399
2400 if (skb->encapsulation) {
2401 /* L4TUNLEN can support 127 words */
2402 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2403 if (len & ~(127 * 2))
2404 goto out_err;
2405
2406 /* IPLEN can support at most 127 dwords */
2407 len = skb_inner_transport_header(skb) -
2408 skb_inner_network_header(skb);
2409 if (len & ~(127 * 4))
2410 goto out_err;
2411 }
2412
2413 /* No need to validate L4LEN as TCP is the only protocol with a
2414 * a flexible value and we support all possible values supported
2415 * by TCP, which is at most 15 dwords
2416 */
2417
2418 return features;
2419out_err:
2420 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2421}
2422
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002423#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2424 NETIF_F_HW_VLAN_CTAG_RX |\
2425 NETIF_F_HW_VLAN_CTAG_FILTER)
2426
2427/**
2428 * i40evf_fix_features - fix up the netdev feature bits
2429 * @netdev: our net device
2430 * @features: desired feature bits
2431 *
2432 * Returns fixed-up features bits
2433 **/
2434static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2435 netdev_features_t features)
2436{
2437 struct i40evf_adapter *adapter = netdev_priv(netdev);
2438
2439 features &= ~I40EVF_VLAN_FEATURES;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002440 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002441 features |= I40EVF_VLAN_FEATURES;
2442 return features;
2443}
2444
Greg Rose5eae00c2013-12-21 06:12:45 +00002445static const struct net_device_ops i40evf_netdev_ops = {
2446 .ndo_open = i40evf_open,
2447 .ndo_stop = i40evf_close,
2448 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002449 .ndo_set_rx_mode = i40evf_set_rx_mode,
2450 .ndo_validate_addr = eth_validate_addr,
2451 .ndo_set_mac_address = i40evf_set_mac,
2452 .ndo_change_mtu = i40evf_change_mtu,
2453 .ndo_tx_timeout = i40evf_tx_timeout,
2454 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2455 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002456 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002457 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002458 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002459#ifdef CONFIG_NET_POLL_CONTROLLER
2460 .ndo_poll_controller = i40evf_netpoll,
2461#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002462};
2463
2464/**
2465 * i40evf_check_reset_complete - check that VF reset is complete
2466 * @hw: pointer to hw struct
2467 *
2468 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2469 **/
2470static int i40evf_check_reset_complete(struct i40e_hw *hw)
2471{
2472 u32 rstat;
2473 int i;
2474
2475 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002476 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2477 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002478 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2479 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002480 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002481 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002482 }
2483 return -EBUSY;
2484}
2485
2486/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002487 * i40evf_process_config - Process the config information we got from the PF
2488 * @adapter: board private structure
2489 *
2490 * Verify that we have a valid config struct, and set up our netdev features
2491 * and our VSI struct.
2492 **/
2493int i40evf_process_config(struct i40evf_adapter *adapter)
2494{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002495 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002496 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002497 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002498 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002499 netdev_features_t hw_enc_features;
2500 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002501
2502 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002503 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002504 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002505 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002506 }
2507 if (!adapter->vsi_res) {
2508 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2509 return -ENODEV;
2510 }
2511
Alan Brady5b36e8d2017-08-22 06:57:50 -04002512 if (num_req_queues &&
2513 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2514 /* Problem. The PF gave us fewer queues than what we had
2515 * negotiated in our request. Need a reset to see if we can't
2516 * get back to a working state.
2517 */
2518 dev_err(&adapter->pdev->dev,
2519 "Requested %d queues, but PF only gave us %d.\n",
2520 num_req_queues,
2521 adapter->vsi_res->num_queue_pairs);
2522 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2523 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2524 i40evf_schedule_reset(adapter);
2525 return -ENODEV;
2526 }
2527 adapter->num_req_queues = 0;
2528
Preethi Banalabacd75c2017-03-27 14:43:18 -07002529 hw_enc_features = NETIF_F_SG |
2530 NETIF_F_IP_CSUM |
2531 NETIF_F_IPV6_CSUM |
2532 NETIF_F_HIGHDMA |
2533 NETIF_F_SOFT_FEATURES |
2534 NETIF_F_TSO |
2535 NETIF_F_TSO_ECN |
2536 NETIF_F_TSO6 |
2537 NETIF_F_SCTP_CRC |
2538 NETIF_F_RXHASH |
2539 NETIF_F_RXCSUM |
2540 0;
2541
2542 /* advertise to stack only if offloads for encapsulated packets is
2543 * supported
2544 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002545 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002546 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002547 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002548 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002549 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002550 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002551 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002552 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002553 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002554
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002555 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002556 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002557 netdev->gso_partial_features |=
2558 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002559
Preethi Banalabacd75c2017-03-27 14:43:18 -07002560 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2561 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2562 netdev->hw_enc_features |= hw_enc_features;
2563 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002564 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002565 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002566
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002567 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002568 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002569 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002570 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002571
Preethi Banalabacd75c2017-03-27 14:43:18 -07002572 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002573
Preethi Banalabacd75c2017-03-27 14:43:18 -07002574 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002575
2576 adapter->vsi.id = adapter->vsi_res->vsi_id;
2577
2578 adapter->vsi.back = adapter;
2579 adapter->vsi.base_vector = 1;
2580 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002581 vsi->netdev = adapter->netdev;
2582 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002583 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002584 adapter->rss_key_size = vfres->rss_key_size;
2585 adapter->rss_lut_size = vfres->rss_lut_size;
2586 } else {
2587 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2588 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2589 }
2590
Mitch Williamse6d038d2015-06-04 16:23:58 -04002591 return 0;
2592}
2593
2594/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002595 * i40evf_init_task - worker thread to perform delayed initialization
2596 * @work: pointer to work_struct containing our data
2597 *
2598 * This task completes the work that was begun in probe. Due to the nature
2599 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002600 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002601 * whole system, we'll do it in a task so we can sleep.
2602 * This task only runs during driver init. Once we've established
2603 * communications with the PF driver and set up our netdev, the watchdog
2604 * takes over.
2605 **/
2606static void i40evf_init_task(struct work_struct *work)
2607{
2608 struct i40evf_adapter *adapter = container_of(work,
2609 struct i40evf_adapter,
2610 init_task.work);
2611 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002612 struct i40e_hw *hw = &adapter->hw;
2613 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002614 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002615
2616 switch (adapter->state) {
2617 case __I40EVF_STARTUP:
2618 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002619 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2620 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002621 err = i40e_set_mac_type(hw);
2622 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002623 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2624 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002625 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002626 }
2627 err = i40evf_check_reset_complete(hw);
2628 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002629 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002630 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002631 goto err;
2632 }
2633 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2634 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2635 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2636 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2637
2638 err = i40evf_init_adminq(hw);
2639 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002640 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2641 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002642 goto err;
2643 }
2644 err = i40evf_send_api_ver(adapter);
2645 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002646 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002647 i40evf_shutdown_adminq(hw);
2648 goto err;
2649 }
2650 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2651 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002652 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002653 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002654 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002655 i40evf_shutdown_adminq(hw);
2656 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002657 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002658 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002659
2660 /* aq msg sent, awaiting reply */
2661 err = i40evf_verify_api_ver(adapter);
2662 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002663 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002664 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002665 else
2666 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2667 adapter->pf_version.major,
2668 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002669 VIRTCHNL_VERSION_MAJOR,
2670 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002671 goto err;
2672 }
2673 err = i40evf_send_vf_config_msg(adapter);
2674 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002675 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002676 err);
2677 goto err;
2678 }
2679 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2680 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002681 case __I40EVF_INIT_GET_RESOURCES:
2682 /* aq msg sent, awaiting reply */
2683 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002684 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002685 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002686 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002687 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002688 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002689 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002690 }
2691 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002692 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002693 err = i40evf_send_vf_config_msg(adapter);
2694 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002695 } else if (err == I40E_ERR_PARAM) {
2696 /* We only get ERR_PARAM if the device is in a very bad
2697 * state or if we've been disabled for previous bad
2698 * behavior. Either way, we're done now.
2699 */
2700 i40evf_shutdown_adminq(hw);
2701 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2702 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002703 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002704 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002705 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2706 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002707 goto err_alloc;
2708 }
2709 adapter->state = __I40EVF_INIT_SW;
2710 break;
2711 default:
2712 goto err_alloc;
2713 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002714
Mitch Williamse6d038d2015-06-04 16:23:58 -04002715 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002716 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002717 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002718
2719 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2720
Greg Rose5eae00c2013-12-21 06:12:45 +00002721 netdev->netdev_ops = &i40evf_netdev_ops;
2722 i40evf_set_ethtool_ops(netdev);
2723 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002724
Jarod Wilson91c527a2016-10-17 15:54:05 -04002725 /* MTU range: 68 - 9710 */
2726 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002727 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002728
Greg Rose5eae00c2013-12-21 06:12:45 +00002729 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002730 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002731 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002732 eth_hw_addr_random(netdev);
2733 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2734 } else {
2735 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2736 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2737 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002738 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002739
Allen Pais7d8fb3a2017-09-21 22:35:20 +05302740 setup_timer(&adapter->watchdog_timer, &i40evf_watchdog_timer,
2741 (unsigned long)adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002742 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2743
Mitch Williamsd732a182014-04-24 06:41:37 +00002744 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2745 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002746 err = i40evf_init_interrupt_scheme(adapter);
2747 if (err)
2748 goto err_sw_init;
2749 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002750 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002751 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002752 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2753
Greg Rose5eae00c2013-12-21 06:12:45 +00002754 err = i40evf_request_misc_irq(adapter);
2755 if (err)
2756 goto err_sw_init;
2757
2758 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002759 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002760
Mitch Williamsef8693e2014-02-13 03:48:53 -08002761 if (!adapter->netdev_registered) {
2762 err = register_netdev(netdev);
2763 if (err)
2764 goto err_register;
2765 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002766
2767 adapter->netdev_registered = true;
2768
2769 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002770 if (CLIENT_ALLOWED(adapter)) {
2771 err = i40evf_lan_add_device(adapter);
2772 if (err)
2773 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2774 err);
2775 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002776
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002777 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002778 if (netdev->features & NETIF_F_GRO)
2779 dev_info(&pdev->dev, "GRO is enabled\n");
2780
Greg Rose5eae00c2013-12-21 06:12:45 +00002781 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002782 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002783 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002784 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002785
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002786 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2787 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2788 if (!adapter->rss_key || !adapter->rss_lut)
2789 goto err_mem;
2790
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002791 if (RSS_AQ(adapter)) {
2792 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2793 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2794 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002795 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002796 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002797 return;
2798restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002799 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002800 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002801err_mem:
2802 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002803err_register:
2804 i40evf_free_misc_irq(adapter);
2805err_sw_init:
2806 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002807err_alloc:
2808 kfree(adapter->vf_res);
2809 adapter->vf_res = NULL;
2810err:
2811 /* Things went into the weeds, so try again later */
2812 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002813 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002814 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002815 i40evf_shutdown_adminq(hw);
2816 adapter->state = __I40EVF_STARTUP;
2817 schedule_delayed_work(&adapter->init_task, HZ * 5);
2818 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002819 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002820 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002821}
2822
2823/**
2824 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2825 * @pdev: pci device structure
2826 **/
2827static void i40evf_shutdown(struct pci_dev *pdev)
2828{
2829 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002830 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002831
2832 netif_device_detach(netdev);
2833
2834 if (netif_running(netdev))
2835 i40evf_close(netdev);
2836
Mitch Williams00293fd2015-01-09 11:18:18 +00002837 /* Prevent the watchdog from running. */
2838 adapter->state = __I40EVF_REMOVE;
2839 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002840
Greg Rose5eae00c2013-12-21 06:12:45 +00002841#ifdef CONFIG_PM
2842 pci_save_state(pdev);
2843
2844#endif
2845 pci_disable_device(pdev);
2846}
2847
2848/**
2849 * i40evf_probe - Device Initialization Routine
2850 * @pdev: PCI device information struct
2851 * @ent: entry in i40evf_pci_tbl
2852 *
2853 * Returns 0 on success, negative on failure
2854 *
2855 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2856 * The OS initialization, configuring of the adapter private structure,
2857 * and a hardware reset occur.
2858 **/
2859static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2860{
2861 struct net_device *netdev;
2862 struct i40evf_adapter *adapter = NULL;
2863 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002864 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002865
2866 err = pci_enable_device(pdev);
2867 if (err)
2868 return err;
2869
Mitch Williams64942942014-02-11 08:26:33 +00002870 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002871 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002872 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2873 if (err) {
2874 dev_err(&pdev->dev,
2875 "DMA configuration failed: 0x%x\n", err);
2876 goto err_dma;
2877 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002878 }
2879
2880 err = pci_request_regions(pdev, i40evf_driver_name);
2881 if (err) {
2882 dev_err(&pdev->dev,
2883 "pci_request_regions failed 0x%x\n", err);
2884 goto err_pci_reg;
2885 }
2886
2887 pci_enable_pcie_error_reporting(pdev);
2888
2889 pci_set_master(pdev);
2890
Mitch Williams1255b7a2015-11-06 15:25:59 -08002891 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002892 if (!netdev) {
2893 err = -ENOMEM;
2894 goto err_alloc_etherdev;
2895 }
2896
2897 SET_NETDEV_DEV(netdev, &pdev->dev);
2898
2899 pci_set_drvdata(pdev, netdev);
2900 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002901
2902 adapter->netdev = netdev;
2903 adapter->pdev = pdev;
2904
2905 hw = &adapter->hw;
2906 hw->back = adapter;
2907
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002908 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002909 adapter->state = __I40EVF_STARTUP;
2910
2911 /* Call save state here because it relies on the adapter struct. */
2912 pci_save_state(pdev);
2913
2914 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2915 pci_resource_len(pdev, 0));
2916 if (!hw->hw_addr) {
2917 err = -EIO;
2918 goto err_ioremap;
2919 }
2920 hw->vendor_id = pdev->vendor;
2921 hw->device_id = pdev->device;
2922 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2923 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2924 hw->subsystem_device_id = pdev->subsystem_device;
2925 hw->bus.device = PCI_SLOT(pdev->devfn);
2926 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002927 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002928
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002929 /* set up the locks for the AQ, do this only once in probe
2930 * and destroy them only once in remove
2931 */
2932 mutex_init(&hw->aq.asq_mutex);
2933 mutex_init(&hw->aq.arq_mutex);
2934
Serey Kong8bb1a542014-08-01 13:27:15 -07002935 INIT_LIST_HEAD(&adapter->mac_filter_list);
2936 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2937
Greg Rose5eae00c2013-12-21 06:12:45 +00002938 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2939 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2940 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002941 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002942 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002943 schedule_delayed_work(&adapter->init_task,
2944 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002945
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002946 /* Setup the wait queue for indicating transition to down status */
2947 init_waitqueue_head(&adapter->down_waitqueue);
2948
Greg Rose5eae00c2013-12-21 06:12:45 +00002949 return 0;
2950
2951err_ioremap:
2952 free_netdev(netdev);
2953err_alloc_etherdev:
2954 pci_release_regions(pdev);
2955err_pci_reg:
2956err_dma:
2957 pci_disable_device(pdev);
2958 return err;
2959}
2960
2961#ifdef CONFIG_PM
2962/**
2963 * i40evf_suspend - Power management suspend routine
2964 * @pdev: PCI device information struct
2965 * @state: unused
2966 *
2967 * Called when the system (VM) is entering sleep/suspend.
2968 **/
2969static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2970{
2971 struct net_device *netdev = pci_get_drvdata(pdev);
2972 struct i40evf_adapter *adapter = netdev_priv(netdev);
2973 int retval = 0;
2974
2975 netif_device_detach(netdev);
2976
2977 if (netif_running(netdev)) {
2978 rtnl_lock();
2979 i40evf_down(adapter);
2980 rtnl_unlock();
2981 }
2982 i40evf_free_misc_irq(adapter);
2983 i40evf_reset_interrupt_capability(adapter);
2984
2985 retval = pci_save_state(pdev);
2986 if (retval)
2987 return retval;
2988
2989 pci_disable_device(pdev);
2990
2991 return 0;
2992}
2993
2994/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002995 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002996 * @pdev: PCI device information struct
2997 *
2998 * Called when the system (VM) is resumed from sleep/suspend.
2999 **/
3000static int i40evf_resume(struct pci_dev *pdev)
3001{
3002 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3003 struct net_device *netdev = adapter->netdev;
3004 u32 err;
3005
3006 pci_set_power_state(pdev, PCI_D0);
3007 pci_restore_state(pdev);
3008 /* pci_restore_state clears dev->state_saved so call
3009 * pci_save_state to restore it.
3010 */
3011 pci_save_state(pdev);
3012
3013 err = pci_enable_device_mem(pdev);
3014 if (err) {
3015 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3016 return err;
3017 }
3018 pci_set_master(pdev);
3019
3020 rtnl_lock();
3021 err = i40evf_set_interrupt_capability(adapter);
3022 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003023 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003024 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3025 return err;
3026 }
3027 err = i40evf_request_misc_irq(adapter);
3028 rtnl_unlock();
3029 if (err) {
3030 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3031 return err;
3032 }
3033
3034 schedule_work(&adapter->reset_task);
3035
3036 netif_device_attach(netdev);
3037
3038 return err;
3039}
3040
3041#endif /* CONFIG_PM */
3042/**
3043 * i40evf_remove - Device Removal Routine
3044 * @pdev: PCI device information struct
3045 *
3046 * i40evf_remove is called by the PCI subsystem to alert the driver
3047 * that it should release a PCI device. The could be caused by a
3048 * Hot-Plug event, or because the driver is going to be removed from
3049 * memory.
3050 **/
3051static void i40evf_remove(struct pci_dev *pdev)
3052{
3053 struct net_device *netdev = pci_get_drvdata(pdev);
3054 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003055 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003056 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003057 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003058
3059 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003060 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003061 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003062 if (adapter->netdev_registered) {
3063 unregister_netdev(netdev);
3064 adapter->netdev_registered = false;
3065 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003066 if (CLIENT_ALLOWED(adapter)) {
3067 err = i40evf_lan_del_device(adapter);
3068 if (err)
3069 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3070 err);
3071 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003072
Mitch Williamsf4a71882015-01-09 11:18:16 +00003073 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003074 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003075 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003076 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003077 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003078 /* If the FW isn't responding, kick it once, but only once. */
3079 if (!i40evf_asq_done(hw)) {
3080 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003081 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003082 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003083 i40evf_free_all_tx_resources(adapter);
3084 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003085 i40evf_misc_irq_disable(adapter);
3086 i40evf_free_misc_irq(adapter);
3087 i40evf_reset_interrupt_capability(adapter);
3088 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003089
Mitch Williamse5d17c32014-06-04 04:22:38 +00003090 if (adapter->watchdog_timer.function)
3091 del_timer_sync(&adapter->watchdog_timer);
3092
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003093 flush_scheduled_work();
3094
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003095 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003096
Greg Rose5eae00c2013-12-21 06:12:45 +00003097 if (hw->aq.asq.count)
3098 i40evf_shutdown_adminq(hw);
3099
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003100 /* destroy the locks only once, here */
3101 mutex_destroy(&hw->aq.arq_mutex);
3102 mutex_destroy(&hw->aq.asq_mutex);
3103
Greg Rose5eae00c2013-12-21 06:12:45 +00003104 iounmap(hw->hw_addr);
3105 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003106 i40evf_free_all_tx_resources(adapter);
3107 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003108 i40evf_free_queues(adapter);
3109 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003110 /* If we got removed before an up/down sequence, we've got a filter
3111 * hanging out there that we need to get rid of.
3112 */
3113 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3114 list_del(&f->list);
3115 kfree(f);
3116 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003117 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3118 list_del(&f->list);
3119 kfree(f);
3120 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003121
3122 free_netdev(netdev);
3123
3124 pci_disable_pcie_error_reporting(pdev);
3125
3126 pci_disable_device(pdev);
3127}
3128
3129static struct pci_driver i40evf_driver = {
3130 .name = i40evf_driver_name,
3131 .id_table = i40evf_pci_tbl,
3132 .probe = i40evf_probe,
3133 .remove = i40evf_remove,
3134#ifdef CONFIG_PM
3135 .suspend = i40evf_suspend,
3136 .resume = i40evf_resume,
3137#endif
3138 .shutdown = i40evf_shutdown,
3139};
3140
3141/**
3142 * i40e_init_module - Driver Registration Routine
3143 *
3144 * i40e_init_module is the first routine called when the driver is
3145 * loaded. All it does is register with the PCI subsystem.
3146 **/
3147static int __init i40evf_init_module(void)
3148{
3149 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003150
Greg Rose5eae00c2013-12-21 06:12:45 +00003151 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003152 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003153
3154 pr_info("%s\n", i40evf_copyright);
3155
Jacob Keller6992a6c2016-08-04 11:37:01 -07003156 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3157 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003158 if (!i40evf_wq) {
3159 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3160 return -ENOMEM;
3161 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003162 ret = pci_register_driver(&i40evf_driver);
3163 return ret;
3164}
3165
3166module_init(i40evf_init_module);
3167
3168/**
3169 * i40e_exit_module - Driver Exit Cleanup Routine
3170 *
3171 * i40e_exit_module is called just before the driver is removed
3172 * from memory.
3173 **/
3174static void __exit i40evf_exit_module(void)
3175{
3176 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003177 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003178}
3179
3180module_exit(i40evf_exit_module);
3181
3182/* i40evf_main.c */