blob: bc76378a71e2418dba27e812a2f4d9fee7735eab [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Catherine Sullivaneaab59e2016-01-13 16:51:44 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Greg Rose5eae00c2013-12-21 06:12:45 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
Greg Rose5eae00c2013-12-21 06:12:45 +000018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40evf.h"
28#include "i40e_prototype.h"
Mitch Williamsed0e8942017-01-24 10:23:59 -080029#include "i40evf_client.h"
Scott Petersoned0980c2017-04-13 04:45:44 -040030/* All i40evf tracepoints are defined by the include below, which must
31 * be included exactly once across the whole kernel with
32 * CREATE_TRACE_POINTS defined
33 */
34#define CREATE_TRACE_POINTS
35#include "i40e_trace.h"
36
Greg Rose5eae00c2013-12-21 06:12:45 +000037static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39static int i40evf_close(struct net_device *netdev);
40
41char i40evf_driver_name[] = "i40evf";
42static const char i40evf_driver_string[] =
Catherine Sullivaneaab59e2016-01-13 16:51:44 -080043 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000044
Mitch Williams69ebe9552015-11-19 11:34:24 -080045#define DRV_KERN "-k"
46
Preethi Banalaabf709a2017-05-11 11:23:20 -070047#define DRV_VERSION_MAJOR 3
48#define DRV_VERSION_MINOR 0
Jacob Kellerb980c062017-07-14 09:27:06 -040049#define DRV_VERSION_BUILD 1
Mitch Williams69ebe9552015-11-19 11:34:24 -080050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) \
53 DRV_KERN
Greg Rose5eae00c2013-12-21 06:12:45 +000054const char i40evf_driver_version[] = DRV_VERSION;
55static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040056 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000057
58/* i40evf_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020066static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080067 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070068 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Preethi Banalaabf709a2017-05-11 11:23:20 -070070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000071 /* required last entry */
72 {0, }
73};
74
75MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
76
77MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(DRV_VERSION);
81
Jesse Brandeburg2803b162015-12-22 14:25:08 -080082static struct workqueue_struct *i40evf_wq;
83
Greg Rose5eae00c2013-12-21 06:12:45 +000084/**
85 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
86 * @hw: pointer to the HW structure
87 * @mem: ptr to mem struct to fill out
88 * @size: size of memory requested
89 * @alignment: what to align the allocation to
90 **/
91i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92 struct i40e_dma_mem *mem,
93 u64 size, u32 alignment)
94{
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
96
97 if (!mem)
98 return I40E_ERR_PARAM;
99
100 mem->size = ALIGN(size, alignment);
101 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
102 (dma_addr_t *)&mem->pa, GFP_KERNEL);
103 if (mem->va)
104 return 0;
105 else
106 return I40E_ERR_NO_MEMORY;
107}
108
109/**
110 * i40evf_free_dma_mem_d - OS specific memory free for shared code
111 * @hw: pointer to the HW structure
112 * @mem: ptr to mem struct to free
113 **/
114i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
115{
116 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
117
118 if (!mem || !mem->va)
119 return I40E_ERR_PARAM;
120 dma_free_coherent(&adapter->pdev->dev, mem->size,
121 mem->va, (dma_addr_t)mem->pa);
122 return 0;
123}
124
125/**
126 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to fill out
129 * @size: size of memory requested
130 **/
131i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem, u32 size)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 mem->size = size;
138 mem->va = kzalloc(size, GFP_KERNEL);
139
140 if (mem->va)
141 return 0;
142 else
143 return I40E_ERR_NO_MEMORY;
144}
145
146/**
147 * i40evf_free_virt_mem_d - OS specific memory free for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to free
150 **/
151i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152 struct i40e_virt_mem *mem)
153{
154 if (!mem)
155 return I40E_ERR_PARAM;
156
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159
160 return 0;
161}
162
163/**
164 * i40evf_debug_d - OS dependent version of debug printing
165 * @hw: pointer to the HW structure
166 * @mask: debug level mask
167 * @fmt_str: printf-type format description
168 **/
169void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
170{
171 char buf[512];
172 va_list argptr;
173
174 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
175 return;
176
177 va_start(argptr, fmt_str);
178 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
179 va_end(argptr);
180
181 /* the debug string is already formatted with a newline */
182 pr_info("%s", buf);
183}
184
185/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800186 * i40evf_schedule_reset - Set the flags and schedule a reset event
187 * @adapter: board private structure
188 **/
189void i40evf_schedule_reset(struct i40evf_adapter *adapter)
190{
191 if (!(adapter->flags &
192 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
193 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
194 schedule_work(&adapter->reset_task);
195 }
196}
197
198/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000199 * i40evf_tx_timeout - Respond to a Tx Hang
200 * @netdev: network interface device structure
201 **/
202static void i40evf_tx_timeout(struct net_device *netdev)
203{
204 struct i40evf_adapter *adapter = netdev_priv(netdev);
205
206 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800207 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000208}
209
210/**
211 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212 * @adapter: board private structure
213 **/
214static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
215{
216 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000217
Jacob Kelleref4603e2016-11-08 13:05:08 -0800218 if (!adapter->msix_entries)
219 return;
220
Greg Rose5eae00c2013-12-21 06:12:45 +0000221 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
222
223 /* read flush */
224 rd32(hw, I40E_VFGEN_RSTAT);
225
226 synchronize_irq(adapter->msix_entries[0].vector);
227}
228
229/**
230 * i40evf_misc_irq_enable - Enable default interrupt generation settings
231 * @adapter: board private structure
232 **/
233static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
234{
235 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000236
Greg Rose5eae00c2013-12-21 06:12:45 +0000237 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400239 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000240
241 /* read flush */
242 rd32(hw, I40E_VFGEN_RSTAT);
243}
244
245/**
246 * i40evf_irq_disable - Mask off interrupt generation on the NIC
247 * @adapter: board private structure
248 **/
249static void i40evf_irq_disable(struct i40evf_adapter *adapter)
250{
251 int i;
252 struct i40e_hw *hw = &adapter->hw;
253
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800254 if (!adapter->msix_entries)
255 return;
256
Greg Rose5eae00c2013-12-21 06:12:45 +0000257 for (i = 1; i < adapter->num_msix_vectors; i++) {
258 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
259 synchronize_irq(adapter->msix_entries[i].vector);
260 }
261 /* read flush */
262 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000263}
264
265/**
266 * i40evf_irq_enable_queues - Enable interrupt for specified queues
267 * @adapter: board private structure
268 * @mask: bitmap of queues to enable
269 **/
270void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
271{
272 struct i40e_hw *hw = &adapter->hw;
273 int i;
274
275 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400276 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000279 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400280 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000281 }
282 }
283}
284
285/**
286 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
287 * @adapter: board private structure
288 * @mask: bitmap of vectors to trigger
289 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000290static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000291{
292 struct i40e_hw *hw = &adapter->hw;
293 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800294 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000295
Mitch Williams164ec1b2014-06-04 08:45:19 +0000296 if (mask & 1) {
297 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400298 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000299 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400300 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000301 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
302 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000303 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400304 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000305 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400306 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000307 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400308 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000309 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
310 }
311 }
312}
313
314/**
315 * i40evf_irq_enable - Enable default interrupt generation settings
316 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600317 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000318 **/
319void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
320{
321 struct i40e_hw *hw = &adapter->hw;
322
Mitch Williams164ec1b2014-06-04 08:45:19 +0000323 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000324 i40evf_irq_enable_queues(adapter, ~0);
325
326 if (flush)
327 rd32(hw, I40E_VFGEN_RSTAT);
328}
329
330/**
331 * i40evf_msix_aq - Interrupt handler for vector 0
332 * @irq: interrupt number
333 * @data: pointer to netdev
334 **/
335static irqreturn_t i40evf_msix_aq(int irq, void *data)
336{
337 struct net_device *netdev = data;
338 struct i40evf_adapter *adapter = netdev_priv(netdev);
339 struct i40e_hw *hw = &adapter->hw;
340 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000341
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700342 /* handle non-queue interrupts, these reads clear the registers */
343 val = rd32(hw, I40E_VFINT_ICR01);
344 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000345
Jean Sacrened17f7e2015-10-13 01:06:30 -0600346 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
347 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000348 wr32(hw, I40E_VFINT_DYN_CTL01, val);
349
Greg Rose5eae00c2013-12-21 06:12:45 +0000350 /* schedule work on the private workqueue */
351 schedule_work(&adapter->adminq_task);
352
353 return IRQ_HANDLED;
354}
355
356/**
357 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
358 * @irq: interrupt number
359 * @data: pointer to a q_vector
360 **/
361static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
362{
363 struct i40e_q_vector *q_vector = data;
364
365 if (!q_vector->tx.ring && !q_vector->rx.ring)
366 return IRQ_HANDLED;
367
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700368 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000369
370 return IRQ_HANDLED;
371}
372
373/**
374 * i40evf_map_vector_to_rxq - associate irqs with rx queues
375 * @adapter: board private structure
376 * @v_idx: interrupt number
377 * @r_idx: queue number
378 **/
379static void
380i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
381{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400382 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400383 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700384 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000385
386 rx_ring->q_vector = q_vector;
387 rx_ring->next = q_vector->rx.ring;
388 rx_ring->vsi = &adapter->vsi;
389 q_vector->rx.ring = rx_ring;
390 q_vector->rx.count++;
391 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700392 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700393 q_vector->ring_mask |= BIT(r_idx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400394 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700395 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000396}
397
398/**
399 * i40evf_map_vector_to_txq - associate irqs with tx queues
400 * @adapter: board private structure
401 * @v_idx: interrupt number
402 * @t_idx: queue number
403 **/
404static void
405i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
406{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400407 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400408 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700409 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000410
411 tx_ring->q_vector = q_vector;
412 tx_ring->next = q_vector->tx.ring;
413 tx_ring->vsi = &adapter->vsi;
414 q_vector->tx.ring = tx_ring;
415 q_vector->tx.count++;
416 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700417 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400418 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000419 q_vector->num_ringpairs++;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700420 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000421}
422
423/**
424 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
425 * @adapter: board private structure to initialize
426 *
427 * This function maps descriptor rings to the queue-specific vectors
428 * we were allotted through the MSI-X enabling code. Ideally, we'd have
429 * one vector per ring/queue, but on a constrained vector budget, we
430 * group the rings as "efficiently" as possible. You would add new
431 * mapping configurations in here.
432 **/
Mitch Williams1b7b7592017-08-22 06:57:51 -0400433static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +0000434{
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400435 int rings_remaining = adapter->num_active_queues;
436 int ridx = 0, vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000437 int q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +0000438
439 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
440
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400441 for (; ridx < rings_remaining; ridx++) {
442 i40evf_map_vector_to_rxq(adapter, vidx, ridx);
443 i40evf_map_vector_to_txq(adapter, vidx, ridx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000444
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400445 /* In the case where we have more queues than vectors, continue
446 * round-robin on vectors until all queues are mapped.
447 */
448 if (++vidx >= q_vectors)
449 vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000450 }
451
Greg Rose5eae00c2013-12-21 06:12:45 +0000452 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Greg Rose5eae00c2013-12-21 06:12:45 +0000453}
454
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700455#ifdef CONFIG_NET_POLL_CONTROLLER
456/**
457 * i40evf_netpoll - A Polling 'interrupt' handler
458 * @netdev: network interface device structure
459 *
460 * This is used by netconsole to send skbs without having to re-enable
461 * interrupts. It's not called while the normal interrupt routine is executing.
462 **/
463static void i40evf_netpoll(struct net_device *netdev)
464{
465 struct i40evf_adapter *adapter = netdev_priv(netdev);
466 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
467 int i;
468
469 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400470 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700471 return;
472
473 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400474 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700475}
476
477#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000478/**
Alan Brady96db7762016-09-14 16:24:38 -0700479 * i40evf_irq_affinity_notify - Callback for affinity changes
480 * @notify: context as to what irq was changed
481 * @mask: the new affinity mask
482 *
483 * This is a callback function used by the irq_set_affinity_notifier function
484 * so that we may register to receive changes to the irq affinity masks.
485 **/
486static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
487 const cpumask_t *mask)
488{
489 struct i40e_q_vector *q_vector =
490 container_of(notify, struct i40e_q_vector, affinity_notify);
491
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400492 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700493}
494
495/**
496 * i40evf_irq_affinity_release - Callback for affinity notifier release
497 * @ref: internal core kernel usage
498 *
499 * This is a callback function used by the irq_set_affinity_notifier function
500 * to inform the current notification subscriber that they will no longer
501 * receive notifications.
502 **/
503static void i40evf_irq_affinity_release(struct kref *ref) {}
504
505/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000506 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
507 * @adapter: board private structure
508 *
509 * Allocates MSI-X vectors for tx and rx handling, and requests
510 * interrupts from the kernel.
511 **/
512static int
513i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
514{
Jacob Keller696ac80a2017-07-12 05:46:11 -0400515 unsigned int vector, q_vectors;
516 unsigned int rx_int_idx = 0, tx_int_idx = 0;
517 int irq_num, err;
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400518 int cpu;
Greg Rose5eae00c2013-12-21 06:12:45 +0000519
520 i40evf_irq_disable(adapter);
521 /* Decrement for Other and TCP Timer vectors */
522 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
523
524 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400525 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700526 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000527
528 if (q_vector->tx.ring && q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400529 snprintf(q_vector->name, sizeof(q_vector->name),
530 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000531 tx_int_idx++;
532 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400533 snprintf(q_vector->name, sizeof(q_vector->name),
534 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000535 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400536 snprintf(q_vector->name, sizeof(q_vector->name),
537 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000538 } else {
539 /* skip this unused q_vector */
540 continue;
541 }
Alan Brady96db7762016-09-14 16:24:38 -0700542 err = request_irq(irq_num,
543 i40evf_msix_clean_rings,
544 0,
545 q_vector->name,
546 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000547 if (err) {
548 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400549 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000550 goto free_queue_irqs;
551 }
Alan Brady96db7762016-09-14 16:24:38 -0700552 /* register for affinity change notifications */
553 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
554 q_vector->affinity_notify.release =
555 i40evf_irq_affinity_release;
556 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400557 /* Spread the IRQ affinity hints across online CPUs. Note that
558 * get_cpu_mask returns a mask with a permanent lifetime so
559 * it's safe to use as a hint for irq_set_affinity_hint.
Jacob Keller759dc4a2017-07-14 09:10:10 -0400560 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400561 cpu = cpumask_local_spread(q_vector->v_idx, -1);
562 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Greg Rose5eae00c2013-12-21 06:12:45 +0000563 }
564
565 return 0;
566
567free_queue_irqs:
568 while (vector) {
569 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700570 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
571 irq_set_affinity_notifier(irq_num, NULL);
572 irq_set_affinity_hint(irq_num, NULL);
573 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000574 }
575 return err;
576}
577
578/**
579 * i40evf_request_misc_irq - Initialize MSI-X interrupts
580 * @adapter: board private structure
581 *
582 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
583 * vector is only for the admin queue, and stays active even when the netdev
584 * is closed.
585 **/
586static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
587{
588 struct net_device *netdev = adapter->netdev;
589 int err;
590
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700591 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000592 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
593 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000594 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800595 &i40evf_msix_aq, 0,
596 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000597 if (err) {
598 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800599 "request_irq for %s failed: %d\n",
600 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000601 free_irq(adapter->msix_entries[0].vector, netdev);
602 }
603 return err;
604}
605
606/**
607 * i40evf_free_traffic_irqs - Free MSI-X interrupts
608 * @adapter: board private structure
609 *
610 * Frees all MSI-X vectors other than 0.
611 **/
612static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
613{
Alan Brady96db7762016-09-14 16:24:38 -0700614 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000615
Alan Brady47d2a5d2016-11-08 13:05:05 -0800616 if (!adapter->msix_entries)
617 return;
618
Greg Rose5eae00c2013-12-21 06:12:45 +0000619 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
620
Alan Brady96db7762016-09-14 16:24:38 -0700621 for (vector = 0; vector < q_vectors; vector++) {
622 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
623 irq_set_affinity_notifier(irq_num, NULL);
624 irq_set_affinity_hint(irq_num, NULL);
625 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000626 }
627}
628
629/**
630 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
631 * @adapter: board private structure
632 *
633 * Frees MSI-X vector 0.
634 **/
635static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
636{
637 struct net_device *netdev = adapter->netdev;
638
Jacob Kelleref4603e2016-11-08 13:05:08 -0800639 if (!adapter->msix_entries)
640 return;
641
Greg Rose5eae00c2013-12-21 06:12:45 +0000642 free_irq(adapter->msix_entries[0].vector, netdev);
643}
644
645/**
646 * i40evf_configure_tx - Configure Transmit Unit after Reset
647 * @adapter: board private structure
648 *
649 * Configure the Tx unit of the MAC after a reset.
650 **/
651static void i40evf_configure_tx(struct i40evf_adapter *adapter)
652{
653 struct i40e_hw *hw = &adapter->hw;
654 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000655
Mitch Williamscc052922014-10-25 03:24:34 +0000656 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400657 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000658}
659
660/**
661 * i40evf_configure_rx - Configure Receive Unit after Reset
662 * @adapter: board private structure
663 *
664 * Configure the Rx unit of the MAC after a reset.
665 **/
666static void i40evf_configure_rx(struct i40evf_adapter *adapter)
667{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700668 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000669 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000670 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000671
Alexander Duyckdab86af2017-03-14 10:15:27 -0700672 /* Legacy Rx will always default to a 2048 buffer size. */
673#if (PAGE_SIZE < 8192)
674 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200675 struct net_device *netdev = adapter->netdev;
676
Alexander Duyck98efd692017-04-05 07:51:01 -0400677 /* For jumbo frames on systems with 4K pages we have to use
678 * an order 1 page, so we might as well increase the size
679 * of our Rx buffer to make better use of the available space
680 */
681 rx_buf_len = I40E_RXBUFFER_3072;
682
Alexander Duyckdab86af2017-03-14 10:15:27 -0700683 /* We use a 1536 buffer size for configurations with
684 * standard Ethernet mtu. On x86 this gives us enough room
685 * for shared info and 192 bytes of padding.
686 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400687 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
688 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700689 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
690 }
691#endif
692
Mitch Williamscc052922014-10-25 03:24:34 +0000693 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400694 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700695 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400696
697 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
698 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
699 else
700 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000701 }
702}
703
704/**
705 * i40evf_find_vlan - Search filter list for specific vlan filter
706 * @adapter: board private structure
707 * @vlan: vlan tag
708 *
709 * Returns ptr to the filter object or NULL
710 **/
711static struct
712i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
713{
714 struct i40evf_vlan_filter *f;
715
716 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
717 if (vlan == f->vlan)
718 return f;
719 }
720 return NULL;
721}
722
723/**
724 * i40evf_add_vlan - Add a vlan filter to the list
725 * @adapter: board private structure
726 * @vlan: VLAN tag
727 *
728 * Returns ptr to the filter object or NULL when no memory available.
729 **/
730static struct
731i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
732{
Mitch Williams13acb542015-03-31 00:45:05 -0700733 struct i40evf_vlan_filter *f = NULL;
734 int count = 50;
735
736 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
737 &adapter->crit_section)) {
738 udelay(1);
739 if (--count == 0)
740 goto out;
741 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000742
743 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000744 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000745 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000746 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700747 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000748
Greg Rose5eae00c2013-12-21 06:12:45 +0000749 f->vlan = vlan;
750
751 INIT_LIST_HEAD(&f->list);
752 list_add(&f->list, &adapter->vlan_filter_list);
753 f->add = true;
754 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
755 }
756
Mitch Williams13acb542015-03-31 00:45:05 -0700757clearout:
758 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
759out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000760 return f;
761}
762
763/**
764 * i40evf_del_vlan - Remove a vlan filter from the list
765 * @adapter: board private structure
766 * @vlan: VLAN tag
767 **/
768static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
769{
770 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700771 int count = 50;
772
773 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
774 &adapter->crit_section)) {
775 udelay(1);
776 if (--count == 0)
777 return;
778 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000779
780 f = i40evf_find_vlan(adapter, vlan);
781 if (f) {
782 f->remove = true;
783 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
784 }
Mitch Williams13acb542015-03-31 00:45:05 -0700785 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000786}
787
788/**
789 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
790 * @netdev: network device struct
791 * @vid: VLAN tag
792 **/
793static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000794 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000795{
796 struct i40evf_adapter *adapter = netdev_priv(netdev);
797
Mitch Williams8ed995f2015-08-28 17:55:57 -0400798 if (!VLAN_ALLOWED(adapter))
799 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000800 if (i40evf_add_vlan(adapter, vid) == NULL)
801 return -ENOMEM;
802 return 0;
803}
804
805/**
806 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
807 * @netdev: network device struct
808 * @vid: VLAN tag
809 **/
810static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000811 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000812{
813 struct i40evf_adapter *adapter = netdev_priv(netdev);
814
Mitch Williams8ed995f2015-08-28 17:55:57 -0400815 if (VLAN_ALLOWED(adapter)) {
816 i40evf_del_vlan(adapter, vid);
817 return 0;
818 }
819 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000820}
821
822/**
823 * i40evf_find_filter - Search filter list for specific mac filter
824 * @adapter: board private structure
825 * @macaddr: the MAC address
826 *
827 * Returns ptr to the filter object or NULL
828 **/
829static struct
830i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
831 u8 *macaddr)
832{
833 struct i40evf_mac_filter *f;
834
835 if (!macaddr)
836 return NULL;
837
838 list_for_each_entry(f, &adapter->mac_filter_list, list) {
839 if (ether_addr_equal(macaddr, f->macaddr))
840 return f;
841 }
842 return NULL;
843}
844
845/**
846 * i40e_add_filter - Add a mac filter to the filter list
847 * @adapter: board private structure
848 * @macaddr: the MAC address
849 *
850 * Returns ptr to the filter object or NULL when no memory available.
851 **/
852static struct
853i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
854 u8 *macaddr)
855{
856 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000857 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000858
859 if (!macaddr)
860 return NULL;
861
862 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000863 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000864 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000865 if (--count == 0)
866 return NULL;
867 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000868
869 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000870 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000871 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000872 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000873 clear_bit(__I40EVF_IN_CRITICAL_TASK,
874 &adapter->crit_section);
875 return NULL;
876 }
877
Greg Rose9a173902014-05-22 06:32:02 +0000878 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000879
Mitch Williams63590b62016-05-16 10:26:42 -0700880 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000881 f->add = true;
882 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
883 }
884
885 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
886 return f;
887}
888
889/**
890 * i40evf_set_mac - NDO callback to set port mac address
891 * @netdev: network interface device structure
892 * @p: pointer to an address structure
893 *
894 * Returns 0 on success, negative on failure
895 **/
896static int i40evf_set_mac(struct net_device *netdev, void *p)
897{
898 struct i40evf_adapter *adapter = netdev_priv(netdev);
899 struct i40e_hw *hw = &adapter->hw;
900 struct i40evf_mac_filter *f;
901 struct sockaddr *addr = p;
902
903 if (!is_valid_ether_addr(addr->sa_data))
904 return -EADDRNOTAVAIL;
905
906 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
907 return 0;
908
Mitch Williams14e52ee2015-08-31 19:54:44 -0400909 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
910 return -EPERM;
911
912 f = i40evf_find_filter(adapter, hw->mac.addr);
913 if (f) {
914 f->remove = true;
915 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
916 }
917
Greg Rose5eae00c2013-12-21 06:12:45 +0000918 f = i40evf_add_filter(adapter, addr->sa_data);
919 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000920 ether_addr_copy(hw->mac.addr, addr->sa_data);
921 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000922 }
923
924 return (f == NULL) ? -ENOMEM : 0;
925}
926
927/**
928 * i40evf_set_rx_mode - NDO callback to set the netdev filters
929 * @netdev: network interface device structure
930 **/
931static void i40evf_set_rx_mode(struct net_device *netdev)
932{
933 struct i40evf_adapter *adapter = netdev_priv(netdev);
934 struct i40evf_mac_filter *f, *ftmp;
935 struct netdev_hw_addr *uca;
936 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400937 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000938 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000939
940 /* add addr if not already in the filter list */
941 netdev_for_each_uc_addr(uca, netdev) {
942 i40evf_add_filter(adapter, uca->addr);
943 }
944 netdev_for_each_mc_addr(mca, netdev) {
945 i40evf_add_filter(adapter, mca->addr);
946 }
947
948 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000949 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000950 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000951 if (--count == 0) {
952 dev_err(&adapter->pdev->dev,
953 "Failed to get lock in %s\n", __func__);
954 return;
955 }
956 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000957 /* remove filter if not in netdev list */
958 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400959 netdev_for_each_mc_addr(mca, netdev)
960 if (ether_addr_equal(mca->addr, f->macaddr))
961 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000962
Shannon Nelson2f41f332015-08-26 15:14:20 -0400963 netdev_for_each_uc_addr(uca, netdev)
964 if (ether_addr_equal(uca->addr, f->macaddr))
965 goto bottom_of_search_loop;
966
967 for_each_dev_addr(netdev, ha)
968 if (ether_addr_equal(ha->addr, f->macaddr))
969 goto bottom_of_search_loop;
970
971 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
972 goto bottom_of_search_loop;
973
974 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
975 f->remove = true;
976 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
977
978bottom_of_search_loop:
979 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000980 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700981
982 if (netdev->flags & IFF_PROMISC &&
983 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
984 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
985 else if (!(netdev->flags & IFF_PROMISC) &&
986 adapter->flags & I40EVF_FLAG_PROMISC_ON)
987 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
988
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700989 if (netdev->flags & IFF_ALLMULTI &&
990 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
991 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
992 else if (!(netdev->flags & IFF_ALLMULTI) &&
993 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
994 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
995
Greg Rose5eae00c2013-12-21 06:12:45 +0000996 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
997}
998
999/**
1000 * i40evf_napi_enable_all - enable NAPI on all queue vectors
1001 * @adapter: board private structure
1002 **/
1003static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1004{
1005 int q_idx;
1006 struct i40e_q_vector *q_vector;
1007 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1008
1009 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1010 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +00001011
Mitch Williams7d96ba12015-10-26 19:44:39 -04001012 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001013 napi = &q_vector->napi;
1014 napi_enable(napi);
1015 }
1016}
1017
1018/**
1019 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1020 * @adapter: board private structure
1021 **/
1022static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1023{
1024 int q_idx;
1025 struct i40e_q_vector *q_vector;
1026 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1027
1028 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001029 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001030 napi_disable(&q_vector->napi);
1031 }
1032}
1033
1034/**
1035 * i40evf_configure - set up transmit and receive data structures
1036 * @adapter: board private structure
1037 **/
1038static void i40evf_configure(struct i40evf_adapter *adapter)
1039{
1040 struct net_device *netdev = adapter->netdev;
1041 int i;
1042
1043 i40evf_set_rx_mode(netdev);
1044
1045 i40evf_configure_tx(adapter);
1046 i40evf_configure_rx(adapter);
1047 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1048
Mitch Williamscc052922014-10-25 03:24:34 +00001049 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001050 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001051
Mitch Williamsb1630982016-04-18 11:33:48 -07001052 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001053 }
1054}
1055
1056/**
1057 * i40evf_up_complete - Finish the last steps of bringing up a connection
1058 * @adapter: board private structure
1059 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001060static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001061{
1062 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001063 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001064
1065 i40evf_napi_enable_all(adapter);
1066
1067 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001068 if (CLIENT_ENABLED(adapter))
1069 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001070 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001071}
1072
1073/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001074 * i40e_down - Shutdown the connection processing
1075 * @adapter: board private structure
1076 **/
1077void i40evf_down(struct i40evf_adapter *adapter)
1078{
1079 struct net_device *netdev = adapter->netdev;
1080 struct i40evf_mac_filter *f;
1081
Mitch Williams209dc4d2015-12-09 15:50:27 -08001082 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001083 return;
1084
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001085 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1086 &adapter->crit_section))
1087 usleep_range(500, 1000);
1088
Mitch Williams63e18c22015-03-27 00:12:10 -07001089 netif_carrier_off(netdev);
1090 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001091 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001092 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001093 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001094
Mitch Williamsef8693e2014-02-13 03:48:53 -08001095 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001096 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1097 f->remove = true;
1098 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001099 /* remove all VLAN filters */
1100 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1101 f->remove = true;
1102 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001103 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1104 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001105 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001106 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001107 /* Schedule operations to close down the HW. Don't wait
1108 * here for this to complete. The watchdog is still running
1109 * and it will take care of this.
1110 */
1111 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001112 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001113 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001114 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001115
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001116 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001117 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001118}
1119
1120/**
1121 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1122 * @adapter: board private structure
1123 * @vectors: number of vectors to request
1124 *
1125 * Work with the OS to set up the MSIX vectors needed.
1126 *
1127 * Returns 0 on success, negative on failure
1128 **/
1129static int
1130i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1131{
1132 int err, vector_threshold;
1133
1134 /* We'll want at least 3 (vector_threshold):
1135 * 0) Other (Admin Queue and link, mostly)
1136 * 1) TxQ[0] Cleanup
1137 * 2) RxQ[0] Cleanup
1138 */
1139 vector_threshold = MIN_MSIX_COUNT;
1140
1141 /* The more we get, the more we will assign to Tx/Rx Cleanup
1142 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1143 * Right now, we simply care about how many we'll get; we'll
1144 * set them up later while requesting irq's.
1145 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001146 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1147 vector_threshold, vectors);
1148 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001149 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001150 kfree(adapter->msix_entries);
1151 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001152 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001153 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001154
1155 /* Adjust for only the vectors we'll use, which is minimum
1156 * of max_msix_q_vectors + NONQ_VECS, or the number of
1157 * vectors we were allocated.
1158 */
1159 adapter->num_msix_vectors = err;
1160 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001161}
1162
1163/**
1164 * i40evf_free_queues - Free memory for all rings
1165 * @adapter: board private structure to initialize
1166 *
1167 * Free all of the memory associated with queue pairs.
1168 **/
1169static void i40evf_free_queues(struct i40evf_adapter *adapter)
1170{
Greg Rose5eae00c2013-12-21 06:12:45 +00001171 if (!adapter->vsi_res)
1172 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001173 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001174 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001175 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001176 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001177 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001178}
1179
1180/**
1181 * i40evf_alloc_queues - Allocate memory for all rings
1182 * @adapter: board private structure to initialize
1183 *
1184 * We allocate one ring per queue at run-time since we don't know the
1185 * number of queues at compile-time. The polling_netdev array is
1186 * intended for Multiqueue, but should work fine with a single queue.
1187 **/
1188static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1189{
Jacob Keller65c70062017-06-07 05:43:01 -04001190 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001191
Alan Brady5b36e8d2017-08-22 06:57:50 -04001192 /* If we're in reset reallocating queues we don't actually know yet for
1193 * certain the PF gave us the number of queues we asked for but we'll
1194 * assume it did. Once basic reset is finished we'll confirm once we
1195 * start negotiating config with PF.
1196 */
1197 if (adapter->num_req_queues)
1198 num_active_queues = adapter->num_req_queues;
1199 else
1200 num_active_queues = min_t(int,
1201 adapter->vsi_res->num_queue_pairs,
1202 (int)(num_online_cpus()));
1203
Jacob Keller65c70062017-06-07 05:43:01 -04001204
1205 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001206 sizeof(struct i40e_ring), GFP_KERNEL);
1207 if (!adapter->tx_rings)
1208 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001209 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001210 sizeof(struct i40e_ring), GFP_KERNEL);
1211 if (!adapter->rx_rings)
1212 goto err_out;
1213
Jacob Keller65c70062017-06-07 05:43:01 -04001214 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001215 struct i40e_ring *tx_ring;
1216 struct i40e_ring *rx_ring;
1217
Mitch Williams0dd438d2015-10-26 19:44:40 -04001218 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001219
1220 tx_ring->queue_index = i;
1221 tx_ring->netdev = adapter->netdev;
1222 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001223 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001224 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001225 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001226 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001227
Mitch Williams0dd438d2015-10-26 19:44:40 -04001228 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001229 rx_ring->queue_index = i;
1230 rx_ring->netdev = adapter->netdev;
1231 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001232 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001233 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001234 }
1235
Jacob Keller65c70062017-06-07 05:43:01 -04001236 adapter->num_active_queues = num_active_queues;
1237
Greg Rose5eae00c2013-12-21 06:12:45 +00001238 return 0;
1239
1240err_out:
1241 i40evf_free_queues(adapter);
1242 return -ENOMEM;
1243}
1244
1245/**
1246 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1247 * @adapter: board private structure to initialize
1248 *
1249 * Attempt to configure the interrupts using the best available
1250 * capabilities of the hardware and the kernel.
1251 **/
1252static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1253{
1254 int vector, v_budget;
1255 int pairs = 0;
1256 int err = 0;
1257
1258 if (!adapter->vsi_res) {
1259 err = -EIO;
1260 goto out;
1261 }
Mitch Williamscc052922014-10-25 03:24:34 +00001262 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001263
Jacob Keller789f38c2017-04-19 09:25:56 -04001264 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1265 * us much good if we have more vectors than CPUs. However, we already
1266 * limit the total number of queues by the number of CPUs so we do not
1267 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001268 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001269 v_budget = min_t(int, pairs + NONQ_VECS,
1270 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001271
Greg Rose5eae00c2013-12-21 06:12:45 +00001272 adapter->msix_entries = kcalloc(v_budget,
1273 sizeof(struct msix_entry), GFP_KERNEL);
1274 if (!adapter->msix_entries) {
1275 err = -ENOMEM;
1276 goto out;
1277 }
1278
1279 for (vector = 0; vector < v_budget; vector++)
1280 adapter->msix_entries[vector].entry = vector;
1281
Mitch Williams313ed2d2015-08-27 11:42:31 -04001282 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001283
1284out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001285 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1286 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001287 return err;
1288}
1289
1290/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001291 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1292 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001293 *
1294 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001295 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001296static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001297{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001298 struct i40e_aqc_get_set_rss_key_data *rss_key =
1299 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001300 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001301 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001302
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001303 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001304 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001305 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001306 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001307 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001308 }
1309
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1311 if (ret) {
1312 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1313 i40evf_stat_str(hw, ret),
1314 i40evf_aq_str(hw, hw->aq.asq_last_status));
1315 return ret;
1316
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001317 }
1318
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001319 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1320 adapter->rss_lut, adapter->rss_lut_size);
1321 if (ret) {
1322 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1323 i40evf_stat_str(hw, ret),
1324 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001325 }
1326
1327 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001328
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001329}
1330
1331/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001332 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001333 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001334 *
1335 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001336 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001337static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001338{
1339 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001340 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001341 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001342
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001343 dw = (u32 *)adapter->rss_key;
1344 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1345 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001346
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001347 dw = (u32 *)adapter->rss_lut;
1348 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1349 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001350
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001351 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001352
1353 return 0;
1354}
1355
1356/**
1357 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001358 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001359 *
1360 * Returns 0 on success, negative on failure
1361 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001362int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001363{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001364
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001365 if (RSS_PF(adapter)) {
1366 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1367 I40EVF_FLAG_AQ_SET_RSS_KEY;
1368 return 0;
1369 } else if (RSS_AQ(adapter)) {
1370 return i40evf_config_rss_aq(adapter);
1371 } else {
1372 return i40evf_config_rss_reg(adapter);
1373 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001374}
1375
1376/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001377 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001378 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001379 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001380static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001381{
1382 u16 i;
1383
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001384 for (i = 0; i < adapter->rss_lut_size; i++)
1385 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001386}
1387
1388/**
Helin Zhang96a81982015-10-26 19:44:31 -04001389 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001390 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001391 *
1392 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001393 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001394static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001395{
1396 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001397 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001398
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001399 if (!RSS_PF(adapter)) {
1400 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001401 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001402 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001403 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1404 else
1405 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001406
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001407 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1408 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1409 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001410
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001411 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001412
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001413 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1414 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001415
1416 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001417}
1418
1419/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001420 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1421 * @adapter: board private structure to initialize
1422 *
1423 * We allocate one q_vector per queue interrupt. If allocation fails we
1424 * return -ENOMEM.
1425 **/
1426static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1427{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001428 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001429 struct i40e_q_vector *q_vector;
1430
1431 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001432 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001433 GFP_KERNEL);
1434 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001435 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001436
1437 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001438 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001439 q_vector->adapter = adapter;
1440 q_vector->vsi = &adapter->vsi;
1441 q_vector->v_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001442 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001443 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001444 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001445 }
1446
1447 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001448}
1449
1450/**
1451 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1452 * @adapter: board private structure to initialize
1453 *
1454 * This function frees the memory allocated to the q_vectors. In addition if
1455 * NAPI is enabled it will delete any references to the NAPI struct prior
1456 * to freeing the q_vector.
1457 **/
1458static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1459{
1460 int q_idx, num_q_vectors;
1461 int napi_vectors;
1462
Jacob Kelleref4603e2016-11-08 13:05:08 -08001463 if (!adapter->q_vectors)
1464 return;
1465
Greg Rose5eae00c2013-12-21 06:12:45 +00001466 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001467 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001468
1469 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001470 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001471 if (q_idx < napi_vectors)
1472 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001473 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001474 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001475 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001476}
1477
1478/**
1479 * i40evf_reset_interrupt_capability - Reset MSIX setup
1480 * @adapter: board private structure
1481 *
1482 **/
1483void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1484{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001485 if (!adapter->msix_entries)
1486 return;
1487
Greg Rose5eae00c2013-12-21 06:12:45 +00001488 pci_disable_msix(adapter->pdev);
1489 kfree(adapter->msix_entries);
1490 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001491}
1492
1493/**
1494 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1495 * @adapter: board private structure to initialize
1496 *
1497 **/
1498int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1499{
1500 int err;
1501
Jacob Keller283aeaf2017-04-19 09:25:59 -04001502 err = i40evf_alloc_queues(adapter);
1503 if (err) {
1504 dev_err(&adapter->pdev->dev,
1505 "Unable to allocate memory for queues\n");
1506 goto err_alloc_queues;
1507 }
1508
Jacob Keller62fe2a82016-07-27 12:02:33 -07001509 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001510 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001511 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001512 if (err) {
1513 dev_err(&adapter->pdev->dev,
1514 "Unable to setup interrupt capabilities\n");
1515 goto err_set_interrupt;
1516 }
1517
1518 err = i40evf_alloc_q_vectors(adapter);
1519 if (err) {
1520 dev_err(&adapter->pdev->dev,
1521 "Unable to allocate memory for queue vectors\n");
1522 goto err_alloc_q_vectors;
1523 }
1524
Greg Rose5eae00c2013-12-21 06:12:45 +00001525 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001526 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1527 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001528
1529 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001530err_alloc_q_vectors:
1531 i40evf_reset_interrupt_capability(adapter);
1532err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001533 i40evf_free_queues(adapter);
1534err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001535 return err;
1536}
1537
1538/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001539 * i40evf_free_rss - Free memory used by RSS structs
1540 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001541 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001542static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001543{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001544 kfree(adapter->rss_key);
1545 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001546
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001547 kfree(adapter->rss_lut);
1548 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001549}
1550
1551/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001552 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1553 * @adapter: board private structure
1554 *
1555 * Returns 0 on success, negative on failure
1556 **/
1557static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1558{
1559 struct net_device *netdev = adapter->netdev;
1560 int err;
1561
1562 if (netif_running(netdev))
1563 i40evf_free_traffic_irqs(adapter);
1564 i40evf_free_misc_irq(adapter);
1565 i40evf_reset_interrupt_capability(adapter);
1566 i40evf_free_q_vectors(adapter);
1567 i40evf_free_queues(adapter);
1568
1569 err = i40evf_init_interrupt_scheme(adapter);
1570 if (err)
1571 goto err;
1572
1573 netif_tx_stop_all_queues(netdev);
1574
1575 err = i40evf_request_misc_irq(adapter);
1576 if (err)
1577 goto err;
1578
1579 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1580
Mitch Williams1b7b7592017-08-22 06:57:51 -04001581 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001582
1583 if (RSS_AQ(adapter))
1584 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1585 else
1586 err = i40evf_init_rss(adapter);
1587err:
1588 return err;
1589}
1590
1591/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001592 * i40evf_watchdog_timer - Periodic call-back timer
1593 * @data: pointer to adapter disguised as unsigned long
1594 **/
1595static void i40evf_watchdog_timer(unsigned long data)
1596{
1597 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001598
Greg Rose5eae00c2013-12-21 06:12:45 +00001599 schedule_work(&adapter->watchdog_task);
1600 /* timer will be rescheduled in watchdog task */
1601}
1602
1603/**
1604 * i40evf_watchdog_task - Periodic call-back task
1605 * @work: pointer to work_struct
1606 **/
1607static void i40evf_watchdog_task(struct work_struct *work)
1608{
1609 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001610 struct i40evf_adapter,
1611 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001612 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001613 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001614
Greg Rose5eae00c2013-12-21 06:12:45 +00001615 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001616 goto restart_watchdog;
1617
1618 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001619 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1620 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001621 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1622 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001623 /* A chance for redemption! */
1624 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1625 adapter->state = __I40EVF_STARTUP;
1626 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1627 schedule_delayed_work(&adapter->init_task, 10);
1628 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1629 &adapter->crit_section);
1630 /* Don't reschedule the watchdog, since we've restarted
1631 * the init task. When init_task contacts the PF and
1632 * gets everything set up again, it'll restart the
1633 * watchdog for us. Down, boy. Sit. Stay. Woof.
1634 */
1635 return;
1636 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001637 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001638 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001639 goto watchdog_done;
1640 }
1641
1642 if ((adapter->state < __I40EVF_DOWN) ||
1643 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001644 goto watchdog_done;
1645
Mitch Williamsef8693e2014-02-13 03:48:53 -08001646 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001647 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1648 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001649 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001650 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001651 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001652 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001653 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001654 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001655 goto watchdog_done;
1656 }
1657
1658 /* Process admin queue tasks. After init, everything gets done
1659 * here so we don't race on the admin queue.
1660 */
Mitch Williamsed636962015-04-07 19:45:32 -04001661 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001662 if (!i40evf_asq_done(hw)) {
1663 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1664 i40evf_send_api_ver(adapter);
1665 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001666 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001667 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001668 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1669 i40evf_send_vf_config_msg(adapter);
1670 goto watchdog_done;
1671 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001672
Mitch Williamse284fc82015-03-27 00:12:09 -07001673 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1674 i40evf_disable_queues(adapter);
1675 goto watchdog_done;
1676 }
1677
Greg Rose5eae00c2013-12-21 06:12:45 +00001678 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1679 i40evf_map_queues(adapter);
1680 goto watchdog_done;
1681 }
1682
1683 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1684 i40evf_add_ether_addrs(adapter);
1685 goto watchdog_done;
1686 }
1687
1688 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1689 i40evf_add_vlans(adapter);
1690 goto watchdog_done;
1691 }
1692
1693 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1694 i40evf_del_ether_addrs(adapter);
1695 goto watchdog_done;
1696 }
1697
1698 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1699 i40evf_del_vlans(adapter);
1700 goto watchdog_done;
1701 }
1702
Mariusz Stachura87743702017-07-17 22:09:45 -07001703 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1704 i40evf_enable_vlan_stripping(adapter);
1705 goto watchdog_done;
1706 }
1707
1708 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1709 i40evf_disable_vlan_stripping(adapter);
1710 goto watchdog_done;
1711 }
1712
Greg Rose5eae00c2013-12-21 06:12:45 +00001713 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1714 i40evf_configure_queues(adapter);
1715 goto watchdog_done;
1716 }
1717
1718 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1719 i40evf_enable_queues(adapter);
1720 goto watchdog_done;
1721 }
1722
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001723 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1724 /* This message goes straight to the firmware, not the
1725 * PF, so we don't have to set current_op as we will
1726 * not get a response through the ARQ.
1727 */
Helin Zhang96a81982015-10-26 19:44:31 -04001728 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001729 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1730 goto watchdog_done;
1731 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001732 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1733 i40evf_get_hena(adapter);
1734 goto watchdog_done;
1735 }
1736 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1737 i40evf_set_hena(adapter);
1738 goto watchdog_done;
1739 }
1740 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1741 i40evf_set_rss_key(adapter);
1742 goto watchdog_done;
1743 }
1744 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1745 i40evf_set_rss_lut(adapter);
1746 goto watchdog_done;
1747 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001748
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001749 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001750 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1751 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001752 goto watchdog_done;
1753 }
1754
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001755 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001756 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001757 goto watchdog_done;
1758 }
1759
1760 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1761 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001762 i40evf_set_promiscuous(adapter, 0);
1763 goto watchdog_done;
1764 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001765 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001766
Greg Rose5eae00c2013-12-21 06:12:45 +00001767 if (adapter->state == __I40EVF_RUNNING)
1768 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001769watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001770 if (adapter->state == __I40EVF_RUNNING) {
1771 i40evf_irq_enable_queues(adapter, ~0);
1772 i40evf_fire_sw_int(adapter, 0xFF);
1773 } else {
1774 i40evf_fire_sw_int(adapter, 0x1);
1775 }
1776
Mitch Williamsef8693e2014-02-13 03:48:53 -08001777 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1778restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001779 if (adapter->state == __I40EVF_REMOVE)
1780 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001781 if (adapter->aq_required)
1782 mod_timer(&adapter->watchdog_timer,
1783 jiffies + msecs_to_jiffies(20));
1784 else
1785 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001786 schedule_work(&adapter->adminq_task);
1787}
1788
Joe Perchesdedecb62016-11-01 15:35:14 -07001789static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1790{
1791 struct i40evf_mac_filter *f, *ftmp;
1792 struct i40evf_vlan_filter *fv, *fvtmp;
1793
1794 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1795
1796 if (netif_running(adapter->netdev)) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001797 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001798 netif_carrier_off(adapter->netdev);
1799 netif_tx_disable(adapter->netdev);
1800 adapter->link_up = false;
1801 i40evf_napi_disable_all(adapter);
1802 i40evf_irq_disable(adapter);
1803 i40evf_free_traffic_irqs(adapter);
1804 i40evf_free_all_tx_resources(adapter);
1805 i40evf_free_all_rx_resources(adapter);
1806 }
1807
1808 /* Delete all of the filters, both MAC and VLAN. */
1809 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1810 list_del(&f->list);
1811 kfree(f);
1812 }
1813
1814 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1815 list_del(&fv->list);
1816 kfree(fv);
1817 }
1818
1819 i40evf_free_misc_irq(adapter);
1820 i40evf_reset_interrupt_capability(adapter);
1821 i40evf_free_queues(adapter);
1822 i40evf_free_q_vectors(adapter);
1823 kfree(adapter->vf_res);
1824 i40evf_shutdown_adminq(&adapter->hw);
1825 adapter->netdev->flags &= ~IFF_UP;
1826 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1827 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1828 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001829 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001830 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1831}
1832
Mitch Williams67c818a2015-06-19 08:56:30 -07001833#define I40EVF_RESET_WAIT_MS 10
1834#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001835/**
1836 * i40evf_reset_task - Call-back task to handle hardware reset
1837 * @work: pointer to work_struct
1838 *
1839 * During reset we need to shut down and reinitialize the admin queue
1840 * before we can use it to communicate with the PF again. We also clear
1841 * and reinit the rings because that context is lost as well.
1842 **/
1843static void i40evf_reset_task(struct work_struct *work)
1844{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001845 struct i40evf_adapter *adapter = container_of(work,
1846 struct i40evf_adapter,
1847 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001848 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001849 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001850 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001851 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001852 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001853 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001854
Mitch Williamsed0e8942017-01-24 10:23:59 -08001855 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001856 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001857 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001858 if (CLIENT_ENABLED(adapter)) {
1859 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1860 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1861 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1862 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1863 cancel_delayed_work_sync(&adapter->client_task);
1864 i40evf_notify_client_close(&adapter->vsi, true);
1865 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001866 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001867 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001868 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1869 /* Restart the AQ here. If we have been reset but didn't
1870 * detect it, or if the PF had to reinit, our AQ will be hosed.
1871 */
1872 i40evf_shutdown_adminq(hw);
1873 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001874 i40evf_request_reset(adapter);
1875 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001876 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001877
Mitch Williamsef8693e2014-02-13 03:48:53 -08001878 /* poll until we see the reset actually happen */
1879 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001880 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1881 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1882 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001883 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001884 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001885 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001886 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001887 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001888 goto continue_reset; /* act like the reset happened */
1889 }
1890
1891 /* wait until the reset is complete and the PF is responding to us */
1892 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001893 /* sleep first to make sure a minimum wait time is met */
1894 msleep(I40EVF_RESET_WAIT_MS);
1895
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001896 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1897 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001898 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001899 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001900 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001901
Mitch Williams509a4472015-12-23 12:05:52 -08001902 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001903
Mitch Williamsef8693e2014-02-13 03:48:53 -08001904 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001905 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001906 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001907 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001908 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001909 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001910 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001911
1912continue_reset:
Alan Bradyf0db7892017-07-12 05:46:04 -04001913 if (netif_running(netdev)) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001914 netif_carrier_off(netdev);
1915 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001916 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001917 i40evf_napi_disable_all(adapter);
1918 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001919 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001920
Greg Rose5eae00c2013-12-21 06:12:45 +00001921 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001922 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1923
1924 /* free the Tx/Rx rings and descriptors, might be better to just
1925 * re-use them sometime in the future
1926 */
1927 i40evf_free_all_rx_resources(adapter);
1928 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001929
1930 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001931 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001932 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001933 err = i40evf_init_adminq(hw);
1934 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001935 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1936 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001937 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001938
Alan Brady5b36e8d2017-08-22 06:57:50 -04001939 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1940 err = i40evf_reinit_interrupt_scheme(adapter);
1941 if (err)
1942 goto reset_err;
1943 }
1944
1945 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001946 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001947
1948 /* re-add all MAC filters */
1949 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1950 f->add = true;
1951 }
1952 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001953 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1954 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001955 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001956 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001957 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001958 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001959 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001960 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001961
1962 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1963
1964 if (netif_running(adapter->netdev)) {
1965 /* allocate transmit descriptors */
1966 err = i40evf_setup_all_tx_resources(adapter);
1967 if (err)
1968 goto reset_err;
1969
1970 /* allocate receive descriptors */
1971 err = i40evf_setup_all_rx_resources(adapter);
1972 if (err)
1973 goto reset_err;
1974
Alan Brady5b36e8d2017-08-22 06:57:50 -04001975 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1976 err = i40evf_request_traffic_irqs(adapter,
1977 netdev->name);
1978 if (err)
1979 goto reset_err;
1980
1981 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1982 }
1983
Greg Rose5eae00c2013-12-21 06:12:45 +00001984 i40evf_configure(adapter);
1985
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001986 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001987
1988 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001989 } else {
1990 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001991 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001992 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001993
Greg Rose5eae00c2013-12-21 06:12:45 +00001994 return;
1995reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001996 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001997 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001998}
1999
2000/**
2001 * i40evf_adminq_task - worker thread to clean the admin queue
2002 * @work: pointer to work_struct containing our data
2003 **/
2004static void i40evf_adminq_task(struct work_struct *work)
2005{
2006 struct i40evf_adapter *adapter =
2007 container_of(work, struct i40evf_adapter, adminq_task);
2008 struct i40e_hw *hw = &adapter->hw;
2009 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07002010 enum virtchnl_ops v_op;
2011 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00002012 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00002013 u16 pending;
2014
Mitch Williamsef8693e2014-02-13 03:48:53 -08002015 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00002016 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08002017
Mitch Williams1001dc32014-11-11 20:02:19 +00002018 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2019 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00002020 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00002021 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00002022
Greg Rose5eae00c2013-12-21 06:12:45 +00002023 do {
2024 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002025 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2026 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2027
2028 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002029 break; /* No event to process or error cleaning ARQ */
2030
Tushar Davec969ef42017-06-22 09:44:32 -07002031 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002032 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002033 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002034 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002035 } while (pending);
2036
Mitch Williams67c818a2015-06-19 08:56:30 -07002037 if ((adapter->flags &
2038 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2039 adapter->state == __I40EVF_RESETTING)
2040 goto freedom;
2041
Mitch Williams912257e2014-05-22 06:32:07 +00002042 /* check for error indications */
2043 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002044 if (val == 0xdeadbeef) /* indicates device in reset */
2045 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002046 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002047 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002048 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002049 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002050 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002051 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002052 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002053 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002054 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002055 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002056 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002057 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002058 }
2059 if (oldval != val)
2060 wr32(hw, hw->aq.arq.len, val);
2061
2062 val = rd32(hw, hw->aq.asq.len);
2063 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002064 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002065 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002066 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002067 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002068 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002069 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002070 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002071 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002072 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002073 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002074 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002075 }
2076 if (oldval != val)
2077 wr32(hw, hw->aq.asq.len, val);
2078
Mitch Williams67c818a2015-06-19 08:56:30 -07002079freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002080 kfree(event.msg_buf);
2081out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002082 /* re-enable Admin queue interrupt cause */
2083 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002084}
2085
2086/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002087 * i40evf_client_task - worker thread to perform client work
2088 * @work: pointer to work_struct containing our data
2089 *
2090 * This task handles client interactions. Because client calls can be
2091 * reentrant, we can't handle them in the watchdog.
2092 **/
2093static void i40evf_client_task(struct work_struct *work)
2094{
2095 struct i40evf_adapter *adapter =
2096 container_of(work, struct i40evf_adapter, client_task.work);
2097
2098 /* If we can't get the client bit, just give up. We'll be rescheduled
2099 * later.
2100 */
2101
2102 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2103 return;
2104
2105 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2106 i40evf_client_subtask(adapter);
2107 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2108 goto out;
2109 }
2110 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2111 i40evf_notify_client_close(&adapter->vsi, false);
2112 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2113 goto out;
2114 }
2115 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2116 i40evf_notify_client_open(&adapter->vsi);
2117 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2118 goto out;
2119 }
2120 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2121 i40evf_notify_client_l2_params(&adapter->vsi);
2122 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2123 }
2124out:
2125 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2126}
2127
2128/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002129 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2130 * @adapter: board private structure
2131 *
2132 * Free all transmit software resources
2133 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002134void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002135{
2136 int i;
2137
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002138 if (!adapter->tx_rings)
2139 return;
2140
Mitch Williamscc052922014-10-25 03:24:34 +00002141 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002142 if (adapter->tx_rings[i].desc)
2143 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002144}
2145
2146/**
2147 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2148 * @adapter: board private structure
2149 *
2150 * If this function returns with an error, then it's possible one or
2151 * more of the rings is populated (while the rest are not). It is the
2152 * callers duty to clean those orphaned rings.
2153 *
2154 * Return 0 on success, negative on failure
2155 **/
2156static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2157{
2158 int i, err = 0;
2159
Mitch Williamscc052922014-10-25 03:24:34 +00002160 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002161 adapter->tx_rings[i].count = adapter->tx_desc_count;
2162 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002163 if (!err)
2164 continue;
2165 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002166 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002167 break;
2168 }
2169
2170 return err;
2171}
2172
2173/**
2174 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2175 * @adapter: board private structure
2176 *
2177 * If this function returns with an error, then it's possible one or
2178 * more of the rings is populated (while the rest are not). It is the
2179 * callers duty to clean those orphaned rings.
2180 *
2181 * Return 0 on success, negative on failure
2182 **/
2183static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2184{
2185 int i, err = 0;
2186
Mitch Williamscc052922014-10-25 03:24:34 +00002187 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002188 adapter->rx_rings[i].count = adapter->rx_desc_count;
2189 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002190 if (!err)
2191 continue;
2192 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002193 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002194 break;
2195 }
2196 return err;
2197}
2198
2199/**
2200 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2201 * @adapter: board private structure
2202 *
2203 * Free all receive software resources
2204 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002205void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002206{
2207 int i;
2208
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002209 if (!adapter->rx_rings)
2210 return;
2211
Mitch Williamscc052922014-10-25 03:24:34 +00002212 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002213 if (adapter->rx_rings[i].desc)
2214 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002215}
2216
2217/**
2218 * i40evf_open - Called when a network interface is made active
2219 * @netdev: network interface device structure
2220 *
2221 * Returns 0 on success, negative value on failure
2222 *
2223 * The open entry point is called when a network interface is made
2224 * active by the system (IFF_UP). At this point all resources needed
2225 * for transmit and receive operations are allocated, the interrupt
2226 * handler is registered with the OS, the watchdog timer is started,
2227 * and the stack is notified that the interface is ready.
2228 **/
2229static int i40evf_open(struct net_device *netdev)
2230{
2231 struct i40evf_adapter *adapter = netdev_priv(netdev);
2232 int err;
2233
Mitch Williamsef8693e2014-02-13 03:48:53 -08002234 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2235 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2236 return -EIO;
2237 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002238
2239 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002240 return -EBUSY;
2241
2242 /* allocate transmit descriptors */
2243 err = i40evf_setup_all_tx_resources(adapter);
2244 if (err)
2245 goto err_setup_tx;
2246
2247 /* allocate receive descriptors */
2248 err = i40evf_setup_all_rx_resources(adapter);
2249 if (err)
2250 goto err_setup_rx;
2251
2252 /* clear any pending interrupts, may auto mask */
2253 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2254 if (err)
2255 goto err_req_irq;
2256
Mitch Williams44151cd2015-04-27 14:57:17 -04002257 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002258 i40evf_configure(adapter);
2259
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002260 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002261
2262 i40evf_irq_enable(adapter, true);
2263
2264 return 0;
2265
2266err_req_irq:
2267 i40evf_down(adapter);
2268 i40evf_free_traffic_irqs(adapter);
2269err_setup_rx:
2270 i40evf_free_all_rx_resources(adapter);
2271err_setup_tx:
2272 i40evf_free_all_tx_resources(adapter);
2273
2274 return err;
2275}
2276
2277/**
2278 * i40evf_close - Disables a network interface
2279 * @netdev: network interface device structure
2280 *
2281 * Returns 0, this is not allowed to fail
2282 *
2283 * The close entry point is called when an interface is de-activated
2284 * by the OS. The hardware is still under the drivers control, but
2285 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2286 * are freed, along with all transmit and receive resources.
2287 **/
2288static int i40evf_close(struct net_device *netdev)
2289{
2290 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002291 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002292
Mitch Williams209dc4d2015-12-09 15:50:27 -08002293 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002294 return 0;
2295
Mitch Williamsef8693e2014-02-13 03:48:53 -08002296
Jacob Keller0da36b92017-04-19 09:25:55 -04002297 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002298 if (CLIENT_ENABLED(adapter))
2299 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002300
2301 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002302 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002303 i40evf_free_traffic_irqs(adapter);
2304
Mitch Williams51f38262016-12-12 15:44:11 -08002305 /* We explicitly don't free resources here because the hardware is
2306 * still active and can DMA into memory. Resources are cleared in
2307 * i40evf_virtchnl_completion() after we get confirmation from the PF
2308 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002309 *
2310 * Also, we wait for state to transition to __I40EVF_DOWN before
2311 * returning. State change occurs in i40evf_virtchnl_completion() after
2312 * VF resources are released (which occurs after PF driver processes and
2313 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002314 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002315
2316 status = wait_event_timeout(adapter->down_waitqueue,
2317 adapter->state == __I40EVF_DOWN,
2318 msecs_to_jiffies(200));
2319 if (!status)
2320 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002321 return 0;
2322}
2323
2324/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002325 * i40evf_change_mtu - Change the Maximum Transfer Unit
2326 * @netdev: network interface device structure
2327 * @new_mtu: new value for maximum frame size
2328 *
2329 * Returns 0 on success, negative on failure
2330 **/
2331static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2332{
2333 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002334
Greg Rose5eae00c2013-12-21 06:12:45 +00002335 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002336 if (CLIENT_ENABLED(adapter)) {
2337 i40evf_notify_client_l2_params(&adapter->vsi);
2338 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2339 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002340 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2341 schedule_work(&adapter->reset_task);
2342
Greg Rose5eae00c2013-12-21 06:12:45 +00002343 return 0;
2344}
2345
Alexander Duyck06fc0162016-10-25 16:08:47 -07002346/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002347 * i40e_set_features - set the netdev feature flags
2348 * @netdev: ptr to the netdev being adjusted
2349 * @features: the feature set that the stack is suggesting
2350 * Note: expects to be called while under rtnl_lock()
2351 **/
2352static int i40evf_set_features(struct net_device *netdev,
2353 netdev_features_t features)
2354{
2355 struct i40evf_adapter *adapter = netdev_priv(netdev);
2356
2357 if (!VLAN_ALLOWED(adapter))
2358 return -EINVAL;
2359
2360 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2361 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2362 else
2363 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2364
2365 return 0;
2366}
2367
2368/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002369 * i40evf_features_check - Validate encapsulated packet conforms to limits
2370 * @skb: skb buff
2371 * @netdev: This physical port's netdev
2372 * @features: Offload features that the stack believes apply
2373 **/
2374static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2375 struct net_device *dev,
2376 netdev_features_t features)
2377{
2378 size_t len;
2379
2380 /* No point in doing any of this if neither checksum nor GSO are
2381 * being requested for this frame. We can rule out both by just
2382 * checking for CHECKSUM_PARTIAL
2383 */
2384 if (skb->ip_summed != CHECKSUM_PARTIAL)
2385 return features;
2386
2387 /* We cannot support GSO if the MSS is going to be less than
2388 * 64 bytes. If it is then we need to drop support for GSO.
2389 */
2390 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2391 features &= ~NETIF_F_GSO_MASK;
2392
2393 /* MACLEN can support at most 63 words */
2394 len = skb_network_header(skb) - skb->data;
2395 if (len & ~(63 * 2))
2396 goto out_err;
2397
2398 /* IPLEN and EIPLEN can support at most 127 dwords */
2399 len = skb_transport_header(skb) - skb_network_header(skb);
2400 if (len & ~(127 * 4))
2401 goto out_err;
2402
2403 if (skb->encapsulation) {
2404 /* L4TUNLEN can support 127 words */
2405 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2406 if (len & ~(127 * 2))
2407 goto out_err;
2408
2409 /* IPLEN can support at most 127 dwords */
2410 len = skb_inner_transport_header(skb) -
2411 skb_inner_network_header(skb);
2412 if (len & ~(127 * 4))
2413 goto out_err;
2414 }
2415
2416 /* No need to validate L4LEN as TCP is the only protocol with a
2417 * a flexible value and we support all possible values supported
2418 * by TCP, which is at most 15 dwords
2419 */
2420
2421 return features;
2422out_err:
2423 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2424}
2425
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002426#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2427 NETIF_F_HW_VLAN_CTAG_RX |\
2428 NETIF_F_HW_VLAN_CTAG_FILTER)
2429
2430/**
2431 * i40evf_fix_features - fix up the netdev feature bits
2432 * @netdev: our net device
2433 * @features: desired feature bits
2434 *
2435 * Returns fixed-up features bits
2436 **/
2437static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2438 netdev_features_t features)
2439{
2440 struct i40evf_adapter *adapter = netdev_priv(netdev);
2441
2442 features &= ~I40EVF_VLAN_FEATURES;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002443 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002444 features |= I40EVF_VLAN_FEATURES;
2445 return features;
2446}
2447
Greg Rose5eae00c2013-12-21 06:12:45 +00002448static const struct net_device_ops i40evf_netdev_ops = {
2449 .ndo_open = i40evf_open,
2450 .ndo_stop = i40evf_close,
2451 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002452 .ndo_set_rx_mode = i40evf_set_rx_mode,
2453 .ndo_validate_addr = eth_validate_addr,
2454 .ndo_set_mac_address = i40evf_set_mac,
2455 .ndo_change_mtu = i40evf_change_mtu,
2456 .ndo_tx_timeout = i40evf_tx_timeout,
2457 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2458 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002459 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002460 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002461 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002462#ifdef CONFIG_NET_POLL_CONTROLLER
2463 .ndo_poll_controller = i40evf_netpoll,
2464#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002465};
2466
2467/**
2468 * i40evf_check_reset_complete - check that VF reset is complete
2469 * @hw: pointer to hw struct
2470 *
2471 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2472 **/
2473static int i40evf_check_reset_complete(struct i40e_hw *hw)
2474{
2475 u32 rstat;
2476 int i;
2477
2478 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002479 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2480 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002481 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2482 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002483 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002484 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002485 }
2486 return -EBUSY;
2487}
2488
2489/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002490 * i40evf_process_config - Process the config information we got from the PF
2491 * @adapter: board private structure
2492 *
2493 * Verify that we have a valid config struct, and set up our netdev features
2494 * and our VSI struct.
2495 **/
2496int i40evf_process_config(struct i40evf_adapter *adapter)
2497{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002498 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002499 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002500 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002501 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002502 netdev_features_t hw_enc_features;
2503 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002504
2505 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002506 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002507 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002508 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002509 }
2510 if (!adapter->vsi_res) {
2511 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2512 return -ENODEV;
2513 }
2514
Alan Brady5b36e8d2017-08-22 06:57:50 -04002515 if (num_req_queues &&
2516 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2517 /* Problem. The PF gave us fewer queues than what we had
2518 * negotiated in our request. Need a reset to see if we can't
2519 * get back to a working state.
2520 */
2521 dev_err(&adapter->pdev->dev,
2522 "Requested %d queues, but PF only gave us %d.\n",
2523 num_req_queues,
2524 adapter->vsi_res->num_queue_pairs);
2525 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2526 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2527 i40evf_schedule_reset(adapter);
2528 return -ENODEV;
2529 }
2530 adapter->num_req_queues = 0;
2531
Preethi Banalabacd75c2017-03-27 14:43:18 -07002532 hw_enc_features = NETIF_F_SG |
2533 NETIF_F_IP_CSUM |
2534 NETIF_F_IPV6_CSUM |
2535 NETIF_F_HIGHDMA |
2536 NETIF_F_SOFT_FEATURES |
2537 NETIF_F_TSO |
2538 NETIF_F_TSO_ECN |
2539 NETIF_F_TSO6 |
2540 NETIF_F_SCTP_CRC |
2541 NETIF_F_RXHASH |
2542 NETIF_F_RXCSUM |
2543 0;
2544
2545 /* advertise to stack only if offloads for encapsulated packets is
2546 * supported
2547 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002548 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002549 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002550 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002551 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002552 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002553 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002554 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002555 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002556 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002557
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002558 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002559 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002560 netdev->gso_partial_features |=
2561 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002562
Preethi Banalabacd75c2017-03-27 14:43:18 -07002563 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2564 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2565 netdev->hw_enc_features |= hw_enc_features;
2566 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002567 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002568 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002569
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002570 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002571 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002572 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002573 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002574
Preethi Banalabacd75c2017-03-27 14:43:18 -07002575 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002576
Preethi Banalabacd75c2017-03-27 14:43:18 -07002577 netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002578
2579 adapter->vsi.id = adapter->vsi_res->vsi_id;
2580
2581 adapter->vsi.back = adapter;
2582 adapter->vsi.base_vector = 1;
2583 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002584 vsi->netdev = adapter->netdev;
2585 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002586 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002587 adapter->rss_key_size = vfres->rss_key_size;
2588 adapter->rss_lut_size = vfres->rss_lut_size;
2589 } else {
2590 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2591 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2592 }
2593
Mitch Williamse6d038d2015-06-04 16:23:58 -04002594 return 0;
2595}
2596
2597/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002598 * i40evf_init_task - worker thread to perform delayed initialization
2599 * @work: pointer to work_struct containing our data
2600 *
2601 * This task completes the work that was begun in probe. Due to the nature
2602 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002603 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002604 * whole system, we'll do it in a task so we can sleep.
2605 * This task only runs during driver init. Once we've established
2606 * communications with the PF driver and set up our netdev, the watchdog
2607 * takes over.
2608 **/
2609static void i40evf_init_task(struct work_struct *work)
2610{
2611 struct i40evf_adapter *adapter = container_of(work,
2612 struct i40evf_adapter,
2613 init_task.work);
2614 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002615 struct i40e_hw *hw = &adapter->hw;
2616 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002617 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002618
2619 switch (adapter->state) {
2620 case __I40EVF_STARTUP:
2621 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002622 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2623 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002624 err = i40e_set_mac_type(hw);
2625 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002626 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2627 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002628 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002629 }
2630 err = i40evf_check_reset_complete(hw);
2631 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002632 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002633 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002634 goto err;
2635 }
2636 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2637 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2638 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2639 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2640
2641 err = i40evf_init_adminq(hw);
2642 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002643 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2644 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002645 goto err;
2646 }
2647 err = i40evf_send_api_ver(adapter);
2648 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002649 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002650 i40evf_shutdown_adminq(hw);
2651 goto err;
2652 }
2653 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2654 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002655 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002656 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002657 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002658 i40evf_shutdown_adminq(hw);
2659 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002660 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002661 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002662
2663 /* aq msg sent, awaiting reply */
2664 err = i40evf_verify_api_ver(adapter);
2665 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002666 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002667 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002668 else
2669 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2670 adapter->pf_version.major,
2671 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002672 VIRTCHNL_VERSION_MAJOR,
2673 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002674 goto err;
2675 }
2676 err = i40evf_send_vf_config_msg(adapter);
2677 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002678 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002679 err);
2680 goto err;
2681 }
2682 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2683 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002684 case __I40EVF_INIT_GET_RESOURCES:
2685 /* aq msg sent, awaiting reply */
2686 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002687 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002688 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002689 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002690 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002691 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002692 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002693 }
2694 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002695 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002696 err = i40evf_send_vf_config_msg(adapter);
2697 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002698 } else if (err == I40E_ERR_PARAM) {
2699 /* We only get ERR_PARAM if the device is in a very bad
2700 * state or if we've been disabled for previous bad
2701 * behavior. Either way, we're done now.
2702 */
2703 i40evf_shutdown_adminq(hw);
2704 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2705 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002706 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002707 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002708 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2709 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002710 goto err_alloc;
2711 }
2712 adapter->state = __I40EVF_INIT_SW;
2713 break;
2714 default:
2715 goto err_alloc;
2716 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002717
Mitch Williamse6d038d2015-06-04 16:23:58 -04002718 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002719 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002720 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002721
2722 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2723
Greg Rose5eae00c2013-12-21 06:12:45 +00002724 netdev->netdev_ops = &i40evf_netdev_ops;
2725 i40evf_set_ethtool_ops(netdev);
2726 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002727
Jarod Wilson91c527a2016-10-17 15:54:05 -04002728 /* MTU range: 68 - 9710 */
2729 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002730 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002731
Greg Rose5eae00c2013-12-21 06:12:45 +00002732 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002733 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002734 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002735 eth_hw_addr_random(netdev);
2736 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2737 } else {
2738 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2739 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2740 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002741 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002742
Allen Pais7d8fb3a2017-09-21 22:35:20 +05302743 setup_timer(&adapter->watchdog_timer, &i40evf_watchdog_timer,
2744 (unsigned long)adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002745 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2746
Mitch Williamsd732a182014-04-24 06:41:37 +00002747 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2748 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002749 err = i40evf_init_interrupt_scheme(adapter);
2750 if (err)
2751 goto err_sw_init;
2752 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002753 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002754 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002755 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2756
Greg Rose5eae00c2013-12-21 06:12:45 +00002757 err = i40evf_request_misc_irq(adapter);
2758 if (err)
2759 goto err_sw_init;
2760
2761 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002762 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002763
Mitch Williamsef8693e2014-02-13 03:48:53 -08002764 if (!adapter->netdev_registered) {
2765 err = register_netdev(netdev);
2766 if (err)
2767 goto err_register;
2768 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002769
2770 adapter->netdev_registered = true;
2771
2772 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002773 if (CLIENT_ALLOWED(adapter)) {
2774 err = i40evf_lan_add_device(adapter);
2775 if (err)
2776 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2777 err);
2778 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002779
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002780 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002781 if (netdev->features & NETIF_F_GRO)
2782 dev_info(&pdev->dev, "GRO is enabled\n");
2783
Greg Rose5eae00c2013-12-21 06:12:45 +00002784 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002785 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002786 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002787 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002788
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002789 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2790 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2791 if (!adapter->rss_key || !adapter->rss_lut)
2792 goto err_mem;
2793
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002794 if (RSS_AQ(adapter)) {
2795 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2796 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2797 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002798 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002799 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002800 return;
2801restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002802 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002803 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002804err_mem:
2805 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002806err_register:
2807 i40evf_free_misc_irq(adapter);
2808err_sw_init:
2809 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002810err_alloc:
2811 kfree(adapter->vf_res);
2812 adapter->vf_res = NULL;
2813err:
2814 /* Things went into the weeds, so try again later */
2815 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002816 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002817 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002818 i40evf_shutdown_adminq(hw);
2819 adapter->state = __I40EVF_STARTUP;
2820 schedule_delayed_work(&adapter->init_task, HZ * 5);
2821 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002822 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002823 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002824}
2825
2826/**
2827 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2828 * @pdev: pci device structure
2829 **/
2830static void i40evf_shutdown(struct pci_dev *pdev)
2831{
2832 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002833 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002834
2835 netif_device_detach(netdev);
2836
2837 if (netif_running(netdev))
2838 i40evf_close(netdev);
2839
Mitch Williams00293fd2015-01-09 11:18:18 +00002840 /* Prevent the watchdog from running. */
2841 adapter->state = __I40EVF_REMOVE;
2842 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002843
Greg Rose5eae00c2013-12-21 06:12:45 +00002844#ifdef CONFIG_PM
2845 pci_save_state(pdev);
2846
2847#endif
2848 pci_disable_device(pdev);
2849}
2850
2851/**
2852 * i40evf_probe - Device Initialization Routine
2853 * @pdev: PCI device information struct
2854 * @ent: entry in i40evf_pci_tbl
2855 *
2856 * Returns 0 on success, negative on failure
2857 *
2858 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2859 * The OS initialization, configuring of the adapter private structure,
2860 * and a hardware reset occur.
2861 **/
2862static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2863{
2864 struct net_device *netdev;
2865 struct i40evf_adapter *adapter = NULL;
2866 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002867 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002868
2869 err = pci_enable_device(pdev);
2870 if (err)
2871 return err;
2872
Mitch Williams64942942014-02-11 08:26:33 +00002873 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002874 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002875 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2876 if (err) {
2877 dev_err(&pdev->dev,
2878 "DMA configuration failed: 0x%x\n", err);
2879 goto err_dma;
2880 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002881 }
2882
2883 err = pci_request_regions(pdev, i40evf_driver_name);
2884 if (err) {
2885 dev_err(&pdev->dev,
2886 "pci_request_regions failed 0x%x\n", err);
2887 goto err_pci_reg;
2888 }
2889
2890 pci_enable_pcie_error_reporting(pdev);
2891
2892 pci_set_master(pdev);
2893
Mitch Williams1255b7a2015-11-06 15:25:59 -08002894 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002895 if (!netdev) {
2896 err = -ENOMEM;
2897 goto err_alloc_etherdev;
2898 }
2899
2900 SET_NETDEV_DEV(netdev, &pdev->dev);
2901
2902 pci_set_drvdata(pdev, netdev);
2903 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002904
2905 adapter->netdev = netdev;
2906 adapter->pdev = pdev;
2907
2908 hw = &adapter->hw;
2909 hw->back = adapter;
2910
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002911 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002912 adapter->state = __I40EVF_STARTUP;
2913
2914 /* Call save state here because it relies on the adapter struct. */
2915 pci_save_state(pdev);
2916
2917 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2918 pci_resource_len(pdev, 0));
2919 if (!hw->hw_addr) {
2920 err = -EIO;
2921 goto err_ioremap;
2922 }
2923 hw->vendor_id = pdev->vendor;
2924 hw->device_id = pdev->device;
2925 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2926 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2927 hw->subsystem_device_id = pdev->subsystem_device;
2928 hw->bus.device = PCI_SLOT(pdev->devfn);
2929 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002930 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002931
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002932 /* set up the locks for the AQ, do this only once in probe
2933 * and destroy them only once in remove
2934 */
2935 mutex_init(&hw->aq.asq_mutex);
2936 mutex_init(&hw->aq.arq_mutex);
2937
Serey Kong8bb1a542014-08-01 13:27:15 -07002938 INIT_LIST_HEAD(&adapter->mac_filter_list);
2939 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2940
Greg Rose5eae00c2013-12-21 06:12:45 +00002941 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2942 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2943 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002944 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002945 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002946 schedule_delayed_work(&adapter->init_task,
2947 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002948
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002949 /* Setup the wait queue for indicating transition to down status */
2950 init_waitqueue_head(&adapter->down_waitqueue);
2951
Greg Rose5eae00c2013-12-21 06:12:45 +00002952 return 0;
2953
2954err_ioremap:
2955 free_netdev(netdev);
2956err_alloc_etherdev:
2957 pci_release_regions(pdev);
2958err_pci_reg:
2959err_dma:
2960 pci_disable_device(pdev);
2961 return err;
2962}
2963
2964#ifdef CONFIG_PM
2965/**
2966 * i40evf_suspend - Power management suspend routine
2967 * @pdev: PCI device information struct
2968 * @state: unused
2969 *
2970 * Called when the system (VM) is entering sleep/suspend.
2971 **/
2972static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2973{
2974 struct net_device *netdev = pci_get_drvdata(pdev);
2975 struct i40evf_adapter *adapter = netdev_priv(netdev);
2976 int retval = 0;
2977
2978 netif_device_detach(netdev);
2979
2980 if (netif_running(netdev)) {
2981 rtnl_lock();
2982 i40evf_down(adapter);
2983 rtnl_unlock();
2984 }
2985 i40evf_free_misc_irq(adapter);
2986 i40evf_reset_interrupt_capability(adapter);
2987
2988 retval = pci_save_state(pdev);
2989 if (retval)
2990 return retval;
2991
2992 pci_disable_device(pdev);
2993
2994 return 0;
2995}
2996
2997/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002998 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002999 * @pdev: PCI device information struct
3000 *
3001 * Called when the system (VM) is resumed from sleep/suspend.
3002 **/
3003static int i40evf_resume(struct pci_dev *pdev)
3004{
3005 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3006 struct net_device *netdev = adapter->netdev;
3007 u32 err;
3008
3009 pci_set_power_state(pdev, PCI_D0);
3010 pci_restore_state(pdev);
3011 /* pci_restore_state clears dev->state_saved so call
3012 * pci_save_state to restore it.
3013 */
3014 pci_save_state(pdev);
3015
3016 err = pci_enable_device_mem(pdev);
3017 if (err) {
3018 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3019 return err;
3020 }
3021 pci_set_master(pdev);
3022
3023 rtnl_lock();
3024 err = i40evf_set_interrupt_capability(adapter);
3025 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003026 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003027 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3028 return err;
3029 }
3030 err = i40evf_request_misc_irq(adapter);
3031 rtnl_unlock();
3032 if (err) {
3033 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3034 return err;
3035 }
3036
3037 schedule_work(&adapter->reset_task);
3038
3039 netif_device_attach(netdev);
3040
3041 return err;
3042}
3043
3044#endif /* CONFIG_PM */
3045/**
3046 * i40evf_remove - Device Removal Routine
3047 * @pdev: PCI device information struct
3048 *
3049 * i40evf_remove is called by the PCI subsystem to alert the driver
3050 * that it should release a PCI device. The could be caused by a
3051 * Hot-Plug event, or because the driver is going to be removed from
3052 * memory.
3053 **/
3054static void i40evf_remove(struct pci_dev *pdev)
3055{
3056 struct net_device *netdev = pci_get_drvdata(pdev);
3057 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003058 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003059 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003060 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003061
3062 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003063 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003064 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003065 if (adapter->netdev_registered) {
3066 unregister_netdev(netdev);
3067 adapter->netdev_registered = false;
3068 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003069 if (CLIENT_ALLOWED(adapter)) {
3070 err = i40evf_lan_del_device(adapter);
3071 if (err)
3072 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3073 err);
3074 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003075
Mitch Williamsf4a71882015-01-09 11:18:16 +00003076 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003077 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003078 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003079 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003080 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003081 /* If the FW isn't responding, kick it once, but only once. */
3082 if (!i40evf_asq_done(hw)) {
3083 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003084 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003085 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003086 i40evf_free_all_tx_resources(adapter);
3087 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003088 i40evf_misc_irq_disable(adapter);
3089 i40evf_free_misc_irq(adapter);
3090 i40evf_reset_interrupt_capability(adapter);
3091 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003092
Mitch Williamse5d17c32014-06-04 04:22:38 +00003093 if (adapter->watchdog_timer.function)
3094 del_timer_sync(&adapter->watchdog_timer);
3095
Mitch Williamsdbb01c82014-02-20 19:29:07 -08003096 flush_scheduled_work();
3097
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003098 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003099
Greg Rose5eae00c2013-12-21 06:12:45 +00003100 if (hw->aq.asq.count)
3101 i40evf_shutdown_adminq(hw);
3102
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003103 /* destroy the locks only once, here */
3104 mutex_destroy(&hw->aq.arq_mutex);
3105 mutex_destroy(&hw->aq.asq_mutex);
3106
Greg Rose5eae00c2013-12-21 06:12:45 +00003107 iounmap(hw->hw_addr);
3108 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003109 i40evf_free_all_tx_resources(adapter);
3110 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003111 i40evf_free_queues(adapter);
3112 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003113 /* If we got removed before an up/down sequence, we've got a filter
3114 * hanging out there that we need to get rid of.
3115 */
3116 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3117 list_del(&f->list);
3118 kfree(f);
3119 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003120 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3121 list_del(&f->list);
3122 kfree(f);
3123 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003124
3125 free_netdev(netdev);
3126
3127 pci_disable_pcie_error_reporting(pdev);
3128
3129 pci_disable_device(pdev);
3130}
3131
3132static struct pci_driver i40evf_driver = {
3133 .name = i40evf_driver_name,
3134 .id_table = i40evf_pci_tbl,
3135 .probe = i40evf_probe,
3136 .remove = i40evf_remove,
3137#ifdef CONFIG_PM
3138 .suspend = i40evf_suspend,
3139 .resume = i40evf_resume,
3140#endif
3141 .shutdown = i40evf_shutdown,
3142};
3143
3144/**
3145 * i40e_init_module - Driver Registration Routine
3146 *
3147 * i40e_init_module is the first routine called when the driver is
3148 * loaded. All it does is register with the PCI subsystem.
3149 **/
3150static int __init i40evf_init_module(void)
3151{
3152 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003153
Greg Rose5eae00c2013-12-21 06:12:45 +00003154 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003155 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003156
3157 pr_info("%s\n", i40evf_copyright);
3158
Jacob Keller6992a6c2016-08-04 11:37:01 -07003159 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3160 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003161 if (!i40evf_wq) {
3162 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3163 return -ENOMEM;
3164 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003165 ret = pci_register_driver(&i40evf_driver);
3166 return ret;
3167}
3168
3169module_init(i40evf_init_module);
3170
3171/**
3172 * i40e_exit_module - Driver Exit Cleanup Routine
3173 *
3174 * i40e_exit_module is called just before the driver is removed
3175 * from memory.
3176 **/
3177static void __exit i40evf_exit_module(void)
3178{
3179 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003180 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003181}
3182
3183module_exit(i40evf_exit_module);
3184
3185/* i40evf_main.c */