blob: 420e5453effe9bbaf3ec4e4d634a76700581d9ce [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"
29static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
30static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
31static int i40evf_close(struct net_device *netdev);
32
33char i40evf_driver_name[] = "i40evf";
34static const char i40evf_driver_string[] =
Catherine Sullivaneaab59e2016-01-13 16:51:44 -080035 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000036
Mitch Williams69ebe9552015-11-19 11:34:24 -080037#define DRV_KERN "-k"
38
39#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070040#define DRV_VERSION_MINOR 6
Bimmy Pujaricf465fe2016-09-27 11:28:54 -070041#define DRV_VERSION_BUILD 21
Mitch Williams69ebe9552015-11-19 11:34:24 -080042#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
43 __stringify(DRV_VERSION_MINOR) "." \
44 __stringify(DRV_VERSION_BUILD) \
45 DRV_KERN
Greg Rose5eae00c2013-12-21 06:12:45 +000046const char i40evf_driver_version[] = DRV_VERSION;
47static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040048 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000049
50/* i40evf_pci_tbl - PCI Device ID Table
51 *
52 * Wildcard entries (PCI_ANY_ID) should come last
53 * Last entry must be all 0s
54 *
55 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
56 * Class, Class Mask, private data (not used) }
57 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020058static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080059 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070060 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040061 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070062 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF_HV), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000063 /* required last entry */
64 {0, }
65};
66
67MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
68
69MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
70MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
71MODULE_LICENSE("GPL");
72MODULE_VERSION(DRV_VERSION);
73
Jesse Brandeburg2803b162015-12-22 14:25:08 -080074static struct workqueue_struct *i40evf_wq;
75
Greg Rose5eae00c2013-12-21 06:12:45 +000076/**
77 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
78 * @hw: pointer to the HW structure
79 * @mem: ptr to mem struct to fill out
80 * @size: size of memory requested
81 * @alignment: what to align the allocation to
82 **/
83i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
84 struct i40e_dma_mem *mem,
85 u64 size, u32 alignment)
86{
87 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
88
89 if (!mem)
90 return I40E_ERR_PARAM;
91
92 mem->size = ALIGN(size, alignment);
93 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
94 (dma_addr_t *)&mem->pa, GFP_KERNEL);
95 if (mem->va)
96 return 0;
97 else
98 return I40E_ERR_NO_MEMORY;
99}
100
101/**
102 * i40evf_free_dma_mem_d - OS specific memory free for shared code
103 * @hw: pointer to the HW structure
104 * @mem: ptr to mem struct to free
105 **/
106i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
107{
108 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
109
110 if (!mem || !mem->va)
111 return I40E_ERR_PARAM;
112 dma_free_coherent(&adapter->pdev->dev, mem->size,
113 mem->va, (dma_addr_t)mem->pa);
114 return 0;
115}
116
117/**
118 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to fill out
121 * @size: size of memory requested
122 **/
123i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
124 struct i40e_virt_mem *mem, u32 size)
125{
126 if (!mem)
127 return I40E_ERR_PARAM;
128
129 mem->size = size;
130 mem->va = kzalloc(size, GFP_KERNEL);
131
132 if (mem->va)
133 return 0;
134 else
135 return I40E_ERR_NO_MEMORY;
136}
137
138/**
139 * i40evf_free_virt_mem_d - OS specific memory free for shared code
140 * @hw: pointer to the HW structure
141 * @mem: ptr to mem struct to free
142 **/
143i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
144 struct i40e_virt_mem *mem)
145{
146 if (!mem)
147 return I40E_ERR_PARAM;
148
149 /* it's ok to kfree a NULL pointer */
150 kfree(mem->va);
151
152 return 0;
153}
154
155/**
156 * i40evf_debug_d - OS dependent version of debug printing
157 * @hw: pointer to the HW structure
158 * @mask: debug level mask
159 * @fmt_str: printf-type format description
160 **/
161void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
162{
163 char buf[512];
164 va_list argptr;
165
166 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
167 return;
168
169 va_start(argptr, fmt_str);
170 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
171 va_end(argptr);
172
173 /* the debug string is already formatted with a newline */
174 pr_info("%s", buf);
175}
176
177/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800178 * i40evf_schedule_reset - Set the flags and schedule a reset event
179 * @adapter: board private structure
180 **/
181void i40evf_schedule_reset(struct i40evf_adapter *adapter)
182{
183 if (!(adapter->flags &
184 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
185 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
186 schedule_work(&adapter->reset_task);
187 }
188}
189
190/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000191 * i40evf_tx_timeout - Respond to a Tx Hang
192 * @netdev: network interface device structure
193 **/
194static void i40evf_tx_timeout(struct net_device *netdev)
195{
196 struct i40evf_adapter *adapter = netdev_priv(netdev);
197
198 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800199 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000200}
201
202/**
203 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
204 * @adapter: board private structure
205 **/
206static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
207{
208 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000209
Greg Rose5eae00c2013-12-21 06:12:45 +0000210 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
211
212 /* read flush */
213 rd32(hw, I40E_VFGEN_RSTAT);
214
215 synchronize_irq(adapter->msix_entries[0].vector);
216}
217
218/**
219 * i40evf_misc_irq_enable - Enable default interrupt generation settings
220 * @adapter: board private structure
221 **/
222static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
223{
224 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000225
Greg Rose5eae00c2013-12-21 06:12:45 +0000226 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
227 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400228 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000229
230 /* read flush */
231 rd32(hw, I40E_VFGEN_RSTAT);
232}
233
234/**
235 * i40evf_irq_disable - Mask off interrupt generation on the NIC
236 * @adapter: board private structure
237 **/
238static void i40evf_irq_disable(struct i40evf_adapter *adapter)
239{
240 int i;
241 struct i40e_hw *hw = &adapter->hw;
242
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800243 if (!adapter->msix_entries)
244 return;
245
Greg Rose5eae00c2013-12-21 06:12:45 +0000246 for (i = 1; i < adapter->num_msix_vectors; i++) {
247 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
248 synchronize_irq(adapter->msix_entries[i].vector);
249 }
250 /* read flush */
251 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000252}
253
254/**
255 * i40evf_irq_enable_queues - Enable interrupt for specified queues
256 * @adapter: board private structure
257 * @mask: bitmap of queues to enable
258 **/
259void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
260{
261 struct i40e_hw *hw = &adapter->hw;
262 int i;
263
264 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400265 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000266 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
267 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000268 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400269 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000270 }
271 }
272}
273
274/**
275 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
276 * @adapter: board private structure
277 * @mask: bitmap of vectors to trigger
278 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000279static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000280{
281 struct i40e_hw *hw = &adapter->hw;
282 int i;
Mitch Williamsd82acb32015-11-06 15:26:06 -0800283 u32 dyn_ctl;
Greg Rose5eae00c2013-12-21 06:12:45 +0000284
Mitch Williams164ec1b2014-06-04 08:45:19 +0000285 if (mask & 1) {
286 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400287 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000288 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400289 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000290 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
291 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000292 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400293 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000294 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400295 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000296 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400297 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000298 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
299 }
300 }
301}
302
303/**
304 * i40evf_irq_enable - Enable default interrupt generation settings
305 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600306 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000307 **/
308void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
309{
310 struct i40e_hw *hw = &adapter->hw;
311
Mitch Williams164ec1b2014-06-04 08:45:19 +0000312 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000313 i40evf_irq_enable_queues(adapter, ~0);
314
315 if (flush)
316 rd32(hw, I40E_VFGEN_RSTAT);
317}
318
319/**
320 * i40evf_msix_aq - Interrupt handler for vector 0
321 * @irq: interrupt number
322 * @data: pointer to netdev
323 **/
324static irqreturn_t i40evf_msix_aq(int irq, void *data)
325{
326 struct net_device *netdev = data;
327 struct i40evf_adapter *adapter = netdev_priv(netdev);
328 struct i40e_hw *hw = &adapter->hw;
329 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000330
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700331 /* handle non-queue interrupts, these reads clear the registers */
332 val = rd32(hw, I40E_VFINT_ICR01);
333 val = rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000334
Jean Sacrened17f7e2015-10-13 01:06:30 -0600335 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
336 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000337 wr32(hw, I40E_VFINT_DYN_CTL01, val);
338
Greg Rose5eae00c2013-12-21 06:12:45 +0000339 /* schedule work on the private workqueue */
340 schedule_work(&adapter->adminq_task);
341
342 return IRQ_HANDLED;
343}
344
345/**
346 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
347 * @irq: interrupt number
348 * @data: pointer to a q_vector
349 **/
350static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
351{
352 struct i40e_q_vector *q_vector = data;
353
354 if (!q_vector->tx.ring && !q_vector->rx.ring)
355 return IRQ_HANDLED;
356
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700357 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000358
359 return IRQ_HANDLED;
360}
361
362/**
363 * i40evf_map_vector_to_rxq - associate irqs with rx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @r_idx: queue number
367 **/
368static void
369i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
370{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400371 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400372 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700373 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000374
375 rx_ring->q_vector = q_vector;
376 rx_ring->next = q_vector->rx.ring;
377 rx_ring->vsi = &adapter->vsi;
378 q_vector->rx.ring = rx_ring;
379 q_vector->rx.count++;
380 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700381 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700382 q_vector->ring_mask |= BIT(r_idx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400383 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700384 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000385}
386
387/**
388 * i40evf_map_vector_to_txq - associate irqs with tx queues
389 * @adapter: board private structure
390 * @v_idx: interrupt number
391 * @t_idx: queue number
392 **/
393static void
394i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
395{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400396 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400397 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700398 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000399
400 tx_ring->q_vector = q_vector;
401 tx_ring->next = q_vector->tx.ring;
402 tx_ring->vsi = &adapter->vsi;
403 q_vector->tx.ring = tx_ring;
404 q_vector->tx.count++;
405 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700406 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400407 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000408 q_vector->num_ringpairs++;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700409 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000410}
411
412/**
413 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
414 * @adapter: board private structure to initialize
415 *
416 * This function maps descriptor rings to the queue-specific vectors
417 * we were allotted through the MSI-X enabling code. Ideally, we'd have
418 * one vector per ring/queue, but on a constrained vector budget, we
419 * group the rings as "efficiently" as possible. You would add new
420 * mapping configurations in here.
421 **/
422static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
423{
424 int q_vectors;
425 int v_start = 0;
426 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000427 int rxr_remaining = adapter->num_active_queues;
428 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000429 int i, j;
430 int rqpv, tqpv;
431 int err = 0;
432
433 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
434
435 /* The ideal configuration...
436 * We have enough vectors to map one per queue.
437 */
Mitch Williams973371d2015-04-27 14:57:09 -0400438 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000439 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
440 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
441
442 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
443 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
444 goto out;
445 }
446
447 /* If we don't have enough vectors for a 1-to-1
448 * mapping, we'll have to group them so there are
449 * multiple queues per vector.
450 * Re-adjusting *qpv takes care of the remainder.
451 */
452 for (i = v_start; i < q_vectors; i++) {
453 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
454 for (j = 0; j < rqpv; j++) {
455 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
456 rxr_idx++;
457 rxr_remaining--;
458 }
459 }
460 for (i = v_start; i < q_vectors; i++) {
461 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
462 for (j = 0; j < tqpv; j++) {
463 i40evf_map_vector_to_txq(adapter, i, txr_idx);
464 txr_idx++;
465 txr_remaining--;
466 }
467 }
468
469out:
470 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
471
472 return err;
473}
474
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700475#ifdef CONFIG_NET_POLL_CONTROLLER
476/**
477 * i40evf_netpoll - A Polling 'interrupt' handler
478 * @netdev: network interface device structure
479 *
480 * This is used by netconsole to send skbs without having to re-enable
481 * interrupts. It's not called while the normal interrupt routine is executing.
482 **/
483static void i40evf_netpoll(struct net_device *netdev)
484{
485 struct i40evf_adapter *adapter = netdev_priv(netdev);
486 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
487 int i;
488
489 /* if interface is down do nothing */
490 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
491 return;
492
493 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400494 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700495}
496
497#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000498/**
Alan Brady96db7762016-09-14 16:24:38 -0700499 * i40evf_irq_affinity_notify - Callback for affinity changes
500 * @notify: context as to what irq was changed
501 * @mask: the new affinity mask
502 *
503 * This is a callback function used by the irq_set_affinity_notifier function
504 * so that we may register to receive changes to the irq affinity masks.
505 **/
506static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
507 const cpumask_t *mask)
508{
509 struct i40e_q_vector *q_vector =
510 container_of(notify, struct i40e_q_vector, affinity_notify);
511
512 q_vector->affinity_mask = *mask;
513}
514
515/**
516 * i40evf_irq_affinity_release - Callback for affinity notifier release
517 * @ref: internal core kernel usage
518 *
519 * This is a callback function used by the irq_set_affinity_notifier function
520 * to inform the current notification subscriber that they will no longer
521 * receive notifications.
522 **/
523static void i40evf_irq_affinity_release(struct kref *ref) {}
524
525/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000526 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
527 * @adapter: board private structure
528 *
529 * Allocates MSI-X vectors for tx and rx handling, and requests
530 * interrupts from the kernel.
531 **/
532static int
533i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
534{
535 int vector, err, q_vectors;
536 int rx_int_idx = 0, tx_int_idx = 0;
Alan Brady96db7762016-09-14 16:24:38 -0700537 int irq_num;
Greg Rose5eae00c2013-12-21 06:12:45 +0000538
539 i40evf_irq_disable(adapter);
540 /* Decrement for Other and TCP Timer vectors */
541 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
542
543 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400544 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700545 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000546
547 if (q_vector->tx.ring && q_vector->rx.ring) {
548 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
549 "i40evf-%s-%s-%d", basename,
550 "TxRx", rx_int_idx++);
551 tx_int_idx++;
552 } else if (q_vector->rx.ring) {
553 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
554 "i40evf-%s-%s-%d", basename,
555 "rx", rx_int_idx++);
556 } else if (q_vector->tx.ring) {
557 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
558 "i40evf-%s-%s-%d", basename,
559 "tx", tx_int_idx++);
560 } else {
561 /* skip this unused q_vector */
562 continue;
563 }
Alan Brady96db7762016-09-14 16:24:38 -0700564 err = request_irq(irq_num,
565 i40evf_msix_clean_rings,
566 0,
567 q_vector->name,
568 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000569 if (err) {
570 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400571 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000572 goto free_queue_irqs;
573 }
Alan Brady96db7762016-09-14 16:24:38 -0700574 /* register for affinity change notifications */
575 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
576 q_vector->affinity_notify.release =
577 i40evf_irq_affinity_release;
578 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Greg Rose5eae00c2013-12-21 06:12:45 +0000579 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -0700580 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +0000581 }
582
583 return 0;
584
585free_queue_irqs:
586 while (vector) {
587 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700588 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
589 irq_set_affinity_notifier(irq_num, NULL);
590 irq_set_affinity_hint(irq_num, NULL);
591 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000592 }
593 return err;
594}
595
596/**
597 * i40evf_request_misc_irq - Initialize MSI-X interrupts
598 * @adapter: board private structure
599 *
600 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
601 * vector is only for the admin queue, and stays active even when the netdev
602 * is closed.
603 **/
604static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
605{
606 struct net_device *netdev = adapter->netdev;
607 int err;
608
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700609 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000610 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
611 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000612 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800613 &i40evf_msix_aq, 0,
614 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000615 if (err) {
616 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800617 "request_irq for %s failed: %d\n",
618 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000619 free_irq(adapter->msix_entries[0].vector, netdev);
620 }
621 return err;
622}
623
624/**
625 * i40evf_free_traffic_irqs - Free MSI-X interrupts
626 * @adapter: board private structure
627 *
628 * Frees all MSI-X vectors other than 0.
629 **/
630static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
631{
Alan Brady96db7762016-09-14 16:24:38 -0700632 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000633
Alan Brady47d2a5d2016-11-08 13:05:05 -0800634 if (!adapter->msix_entries)
635 return;
636
Greg Rose5eae00c2013-12-21 06:12:45 +0000637 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
638
Alan Brady96db7762016-09-14 16:24:38 -0700639 for (vector = 0; vector < q_vectors; vector++) {
640 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
641 irq_set_affinity_notifier(irq_num, NULL);
642 irq_set_affinity_hint(irq_num, NULL);
643 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000644 }
645}
646
647/**
648 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
649 * @adapter: board private structure
650 *
651 * Frees MSI-X vector 0.
652 **/
653static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
654{
655 struct net_device *netdev = adapter->netdev;
656
657 free_irq(adapter->msix_entries[0].vector, netdev);
658}
659
660/**
661 * i40evf_configure_tx - Configure Transmit Unit after Reset
662 * @adapter: board private structure
663 *
664 * Configure the Tx unit of the MAC after a reset.
665 **/
666static void i40evf_configure_tx(struct i40evf_adapter *adapter)
667{
668 struct i40e_hw *hw = &adapter->hw;
669 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000670
Mitch Williamscc052922014-10-25 03:24:34 +0000671 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400672 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000673}
674
675/**
676 * i40evf_configure_rx - Configure Receive Unit after Reset
677 * @adapter: board private structure
678 *
679 * Configure the Rx unit of the MAC after a reset.
680 **/
681static void i40evf_configure_rx(struct i40evf_adapter *adapter)
682{
683 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000684 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000685
Mitch Williamscc052922014-10-25 03:24:34 +0000686 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400687 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Jesse Brandeburg19b85e62016-04-18 11:33:45 -0700688 adapter->rx_rings[i].rx_buf_len = I40EVF_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000689 }
690}
691
692/**
693 * i40evf_find_vlan - Search filter list for specific vlan filter
694 * @adapter: board private structure
695 * @vlan: vlan tag
696 *
697 * Returns ptr to the filter object or NULL
698 **/
699static struct
700i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
701{
702 struct i40evf_vlan_filter *f;
703
704 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
705 if (vlan == f->vlan)
706 return f;
707 }
708 return NULL;
709}
710
711/**
712 * i40evf_add_vlan - Add a vlan filter to the list
713 * @adapter: board private structure
714 * @vlan: VLAN tag
715 *
716 * Returns ptr to the filter object or NULL when no memory available.
717 **/
718static struct
719i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
720{
Mitch Williams13acb542015-03-31 00:45:05 -0700721 struct i40evf_vlan_filter *f = NULL;
722 int count = 50;
723
724 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
725 &adapter->crit_section)) {
726 udelay(1);
727 if (--count == 0)
728 goto out;
729 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000730
731 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000732 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000733 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000734 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700735 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000736
Greg Rose5eae00c2013-12-21 06:12:45 +0000737 f->vlan = vlan;
738
739 INIT_LIST_HEAD(&f->list);
740 list_add(&f->list, &adapter->vlan_filter_list);
741 f->add = true;
742 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
743 }
744
Mitch Williams13acb542015-03-31 00:45:05 -0700745clearout:
746 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
747out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000748 return f;
749}
750
751/**
752 * i40evf_del_vlan - Remove a vlan filter from the list
753 * @adapter: board private structure
754 * @vlan: VLAN tag
755 **/
756static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
757{
758 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700759 int count = 50;
760
761 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
762 &adapter->crit_section)) {
763 udelay(1);
764 if (--count == 0)
765 return;
766 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000767
768 f = i40evf_find_vlan(adapter, vlan);
769 if (f) {
770 f->remove = true;
771 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
772 }
Mitch Williams13acb542015-03-31 00:45:05 -0700773 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000774}
775
776/**
777 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
778 * @netdev: network device struct
779 * @vid: VLAN tag
780 **/
781static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000782 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000783{
784 struct i40evf_adapter *adapter = netdev_priv(netdev);
785
Mitch Williams8ed995f2015-08-28 17:55:57 -0400786 if (!VLAN_ALLOWED(adapter))
787 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000788 if (i40evf_add_vlan(adapter, vid) == NULL)
789 return -ENOMEM;
790 return 0;
791}
792
793/**
794 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
795 * @netdev: network device struct
796 * @vid: VLAN tag
797 **/
798static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000799 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000800{
801 struct i40evf_adapter *adapter = netdev_priv(netdev);
802
Mitch Williams8ed995f2015-08-28 17:55:57 -0400803 if (VLAN_ALLOWED(adapter)) {
804 i40evf_del_vlan(adapter, vid);
805 return 0;
806 }
807 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000808}
809
810/**
811 * i40evf_find_filter - Search filter list for specific mac filter
812 * @adapter: board private structure
813 * @macaddr: the MAC address
814 *
815 * Returns ptr to the filter object or NULL
816 **/
817static struct
818i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
819 u8 *macaddr)
820{
821 struct i40evf_mac_filter *f;
822
823 if (!macaddr)
824 return NULL;
825
826 list_for_each_entry(f, &adapter->mac_filter_list, list) {
827 if (ether_addr_equal(macaddr, f->macaddr))
828 return f;
829 }
830 return NULL;
831}
832
833/**
834 * i40e_add_filter - Add a mac filter to the filter list
835 * @adapter: board private structure
836 * @macaddr: the MAC address
837 *
838 * Returns ptr to the filter object or NULL when no memory available.
839 **/
840static struct
841i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
842 u8 *macaddr)
843{
844 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000845 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000846
847 if (!macaddr)
848 return NULL;
849
850 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000851 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000852 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000853 if (--count == 0)
854 return NULL;
855 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000856
857 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000858 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000859 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000860 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000861 clear_bit(__I40EVF_IN_CRITICAL_TASK,
862 &adapter->crit_section);
863 return NULL;
864 }
865
Greg Rose9a173902014-05-22 06:32:02 +0000866 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000867
Mitch Williams63590b62016-05-16 10:26:42 -0700868 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000869 f->add = true;
870 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
871 }
872
873 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
874 return f;
875}
876
877/**
878 * i40evf_set_mac - NDO callback to set port mac address
879 * @netdev: network interface device structure
880 * @p: pointer to an address structure
881 *
882 * Returns 0 on success, negative on failure
883 **/
884static int i40evf_set_mac(struct net_device *netdev, void *p)
885{
886 struct i40evf_adapter *adapter = netdev_priv(netdev);
887 struct i40e_hw *hw = &adapter->hw;
888 struct i40evf_mac_filter *f;
889 struct sockaddr *addr = p;
890
891 if (!is_valid_ether_addr(addr->sa_data))
892 return -EADDRNOTAVAIL;
893
894 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
895 return 0;
896
Mitch Williams14e52ee2015-08-31 19:54:44 -0400897 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
898 return -EPERM;
899
900 f = i40evf_find_filter(adapter, hw->mac.addr);
901 if (f) {
902 f->remove = true;
903 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
904 }
905
Greg Rose5eae00c2013-12-21 06:12:45 +0000906 f = i40evf_add_filter(adapter, addr->sa_data);
907 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000908 ether_addr_copy(hw->mac.addr, addr->sa_data);
909 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000910 }
911
912 return (f == NULL) ? -ENOMEM : 0;
913}
914
915/**
916 * i40evf_set_rx_mode - NDO callback to set the netdev filters
917 * @netdev: network interface device structure
918 **/
919static void i40evf_set_rx_mode(struct net_device *netdev)
920{
921 struct i40evf_adapter *adapter = netdev_priv(netdev);
922 struct i40evf_mac_filter *f, *ftmp;
923 struct netdev_hw_addr *uca;
924 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400925 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000926 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000927
928 /* add addr if not already in the filter list */
929 netdev_for_each_uc_addr(uca, netdev) {
930 i40evf_add_filter(adapter, uca->addr);
931 }
932 netdev_for_each_mc_addr(mca, netdev) {
933 i40evf_add_filter(adapter, mca->addr);
934 }
935
936 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000937 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000938 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000939 if (--count == 0) {
940 dev_err(&adapter->pdev->dev,
941 "Failed to get lock in %s\n", __func__);
942 return;
943 }
944 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000945 /* remove filter if not in netdev list */
946 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400947 netdev_for_each_mc_addr(mca, netdev)
948 if (ether_addr_equal(mca->addr, f->macaddr))
949 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000950
Shannon Nelson2f41f332015-08-26 15:14:20 -0400951 netdev_for_each_uc_addr(uca, netdev)
952 if (ether_addr_equal(uca->addr, f->macaddr))
953 goto bottom_of_search_loop;
954
955 for_each_dev_addr(netdev, ha)
956 if (ether_addr_equal(ha->addr, f->macaddr))
957 goto bottom_of_search_loop;
958
959 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
960 goto bottom_of_search_loop;
961
962 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
963 f->remove = true;
964 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
965
966bottom_of_search_loop:
967 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000968 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700969
970 if (netdev->flags & IFF_PROMISC &&
971 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
972 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
973 else if (!(netdev->flags & IFF_PROMISC) &&
974 adapter->flags & I40EVF_FLAG_PROMISC_ON)
975 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
976
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700977 if (netdev->flags & IFF_ALLMULTI &&
978 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
979 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
980 else if (!(netdev->flags & IFF_ALLMULTI) &&
981 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
982 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
983
Greg Rose5eae00c2013-12-21 06:12:45 +0000984 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
985}
986
987/**
988 * i40evf_napi_enable_all - enable NAPI on all queue vectors
989 * @adapter: board private structure
990 **/
991static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
992{
993 int q_idx;
994 struct i40e_q_vector *q_vector;
995 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
996
997 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
998 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000999
Mitch Williams7d96ba12015-10-26 19:44:39 -04001000 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001001 napi = &q_vector->napi;
1002 napi_enable(napi);
1003 }
1004}
1005
1006/**
1007 * i40evf_napi_disable_all - disable NAPI on all queue vectors
1008 * @adapter: board private structure
1009 **/
1010static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1011{
1012 int q_idx;
1013 struct i40e_q_vector *q_vector;
1014 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1015
1016 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001017 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001018 napi_disable(&q_vector->napi);
1019 }
1020}
1021
1022/**
1023 * i40evf_configure - set up transmit and receive data structures
1024 * @adapter: board private structure
1025 **/
1026static void i40evf_configure(struct i40evf_adapter *adapter)
1027{
1028 struct net_device *netdev = adapter->netdev;
1029 int i;
1030
1031 i40evf_set_rx_mode(netdev);
1032
1033 i40evf_configure_tx(adapter);
1034 i40evf_configure_rx(adapter);
1035 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1036
Mitch Williamscc052922014-10-25 03:24:34 +00001037 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001038 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001039
Mitch Williamsb1630982016-04-18 11:33:48 -07001040 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001041 }
1042}
1043
1044/**
1045 * i40evf_up_complete - Finish the last steps of bringing up a connection
1046 * @adapter: board private structure
1047 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001048static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001049{
1050 adapter->state = __I40EVF_RUNNING;
1051 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1052
1053 i40evf_napi_enable_all(adapter);
1054
1055 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1056 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001057}
1058
1059/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001060 * i40e_down - Shutdown the connection processing
1061 * @adapter: board private structure
1062 **/
1063void i40evf_down(struct i40evf_adapter *adapter)
1064{
1065 struct net_device *netdev = adapter->netdev;
1066 struct i40evf_mac_filter *f;
1067
Mitch Williams209dc4d2015-12-09 15:50:27 -08001068 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001069 return;
1070
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001071 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1072 &adapter->crit_section))
1073 usleep_range(500, 1000);
1074
Mitch Williams63e18c22015-03-27 00:12:10 -07001075 netif_carrier_off(netdev);
1076 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001077 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001078 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001079 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001080
Mitch Williamsef8693e2014-02-13 03:48:53 -08001081 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001082 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1083 f->remove = true;
1084 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001085 /* remove all VLAN filters */
1086 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1087 f->remove = true;
1088 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001089 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1090 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001091 /* cancel any current operation */
1092 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001093 /* Schedule operations to close down the HW. Don't wait
1094 * here for this to complete. The watchdog is still running
1095 * and it will take care of this.
1096 */
1097 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001098 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001099 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001100 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001101
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001102 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001103}
1104
1105/**
1106 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1107 * @adapter: board private structure
1108 * @vectors: number of vectors to request
1109 *
1110 * Work with the OS to set up the MSIX vectors needed.
1111 *
1112 * Returns 0 on success, negative on failure
1113 **/
1114static int
1115i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1116{
1117 int err, vector_threshold;
1118
1119 /* We'll want at least 3 (vector_threshold):
1120 * 0) Other (Admin Queue and link, mostly)
1121 * 1) TxQ[0] Cleanup
1122 * 2) RxQ[0] Cleanup
1123 */
1124 vector_threshold = MIN_MSIX_COUNT;
1125
1126 /* The more we get, the more we will assign to Tx/Rx Cleanup
1127 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1128 * Right now, we simply care about how many we'll get; we'll
1129 * set them up later while requesting irq's.
1130 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001131 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1132 vector_threshold, vectors);
1133 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001134 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001135 kfree(adapter->msix_entries);
1136 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001137 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001138 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001139
1140 /* Adjust for only the vectors we'll use, which is minimum
1141 * of max_msix_q_vectors + NONQ_VECS, or the number of
1142 * vectors we were allocated.
1143 */
1144 adapter->num_msix_vectors = err;
1145 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001146}
1147
1148/**
1149 * i40evf_free_queues - Free memory for all rings
1150 * @adapter: board private structure to initialize
1151 *
1152 * Free all of the memory associated with queue pairs.
1153 **/
1154static void i40evf_free_queues(struct i40evf_adapter *adapter)
1155{
Greg Rose5eae00c2013-12-21 06:12:45 +00001156 if (!adapter->vsi_res)
1157 return;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001158 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001159 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001160 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001161 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001162}
1163
1164/**
1165 * i40evf_alloc_queues - Allocate memory for all rings
1166 * @adapter: board private structure to initialize
1167 *
1168 * We allocate one ring per queue at run-time since we don't know the
1169 * number of queues at compile-time. The polling_netdev array is
1170 * intended for Multiqueue, but should work fine with a single queue.
1171 **/
1172static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1173{
1174 int i;
1175
Mitch Williams0dd438d2015-10-26 19:44:40 -04001176 adapter->tx_rings = kcalloc(adapter->num_active_queues,
1177 sizeof(struct i40e_ring), GFP_KERNEL);
1178 if (!adapter->tx_rings)
1179 goto err_out;
1180 adapter->rx_rings = kcalloc(adapter->num_active_queues,
1181 sizeof(struct i40e_ring), GFP_KERNEL);
1182 if (!adapter->rx_rings)
1183 goto err_out;
1184
Mitch Williamscc052922014-10-25 03:24:34 +00001185 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001186 struct i40e_ring *tx_ring;
1187 struct i40e_ring *rx_ring;
1188
Mitch Williams0dd438d2015-10-26 19:44:40 -04001189 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001190
1191 tx_ring->queue_index = i;
1192 tx_ring->netdev = adapter->netdev;
1193 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001194 tx_ring->count = adapter->tx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001195 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001196 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1197 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001198
Mitch Williams0dd438d2015-10-26 19:44:40 -04001199 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001200 rx_ring->queue_index = i;
1201 rx_ring->netdev = adapter->netdev;
1202 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001203 rx_ring->count = adapter->rx_desc_count;
Jacob Keller65e87c02016-09-12 14:18:44 -07001204 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
Greg Rose5eae00c2013-12-21 06:12:45 +00001205 }
1206
1207 return 0;
1208
1209err_out:
1210 i40evf_free_queues(adapter);
1211 return -ENOMEM;
1212}
1213
1214/**
1215 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1216 * @adapter: board private structure to initialize
1217 *
1218 * Attempt to configure the interrupts using the best available
1219 * capabilities of the hardware and the kernel.
1220 **/
1221static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1222{
1223 int vector, v_budget;
1224 int pairs = 0;
1225 int err = 0;
1226
1227 if (!adapter->vsi_res) {
1228 err = -EIO;
1229 goto out;
1230 }
Mitch Williamscc052922014-10-25 03:24:34 +00001231 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001232
1233 /* It's easy to be greedy for MSI-X vectors, but it really
1234 * doesn't do us much good if we have a lot more vectors
1235 * than CPU's. So let's be conservative and only ask for
1236 * (roughly) twice the number of vectors as there are CPU's.
1237 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001238 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1239 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001240
Greg Rose5eae00c2013-12-21 06:12:45 +00001241 adapter->msix_entries = kcalloc(v_budget,
1242 sizeof(struct msix_entry), GFP_KERNEL);
1243 if (!adapter->msix_entries) {
1244 err = -ENOMEM;
1245 goto out;
1246 }
1247
1248 for (vector = 0; vector < v_budget; vector++)
1249 adapter->msix_entries[vector].entry = vector;
1250
Mitch Williams313ed2d2015-08-27 11:42:31 -04001251 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001252
1253out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001254 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1255 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001256 return err;
1257}
1258
1259/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001260 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1261 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001262 *
1263 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001264 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001265static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001266{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001267 struct i40e_aqc_get_set_rss_key_data *rss_key =
1268 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001269 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001270 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001271
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001272 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1273 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001274 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001275 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001276 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001277 }
1278
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001279 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1280 if (ret) {
1281 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1282 i40evf_stat_str(hw, ret),
1283 i40evf_aq_str(hw, hw->aq.asq_last_status));
1284 return ret;
1285
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001286 }
1287
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001288 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1289 adapter->rss_lut, adapter->rss_lut_size);
1290 if (ret) {
1291 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1292 i40evf_stat_str(hw, ret),
1293 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001294 }
1295
1296 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001297
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001298}
1299
1300/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001301 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001302 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001303 *
1304 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001305 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001306static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001307{
1308 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001309 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001310 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001311
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001312 dw = (u32 *)adapter->rss_key;
1313 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1314 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001315
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001316 dw = (u32 *)adapter->rss_lut;
1317 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1318 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001319
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001320 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001321
1322 return 0;
1323}
1324
1325/**
1326 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001327 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001328 *
1329 * Returns 0 on success, negative on failure
1330 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001331int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001332{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001333
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001334 if (RSS_PF(adapter)) {
1335 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1336 I40EVF_FLAG_AQ_SET_RSS_KEY;
1337 return 0;
1338 } else if (RSS_AQ(adapter)) {
1339 return i40evf_config_rss_aq(adapter);
1340 } else {
1341 return i40evf_config_rss_reg(adapter);
1342 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001343}
1344
1345/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001346 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001347 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001348 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001349static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001350{
1351 u16 i;
1352
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001353 for (i = 0; i < adapter->rss_lut_size; i++)
1354 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001355}
1356
1357/**
Helin Zhang96a81982015-10-26 19:44:31 -04001358 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001359 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001360 *
1361 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001362 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001363static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001364{
1365 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001366 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001367
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001368 if (!RSS_PF(adapter)) {
1369 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1370 if (adapter->vf_res->vf_offload_flags &
1371 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1372 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1373 else
1374 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001375
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001376 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1377 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1378 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001379
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001380 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001381
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001382 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1383 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001384
1385 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001386}
1387
1388/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001389 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1390 * @adapter: board private structure to initialize
1391 *
1392 * We allocate one q_vector per queue interrupt. If allocation fails we
1393 * return -ENOMEM.
1394 **/
1395static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1396{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001397 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001398 struct i40e_q_vector *q_vector;
1399
1400 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001401 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001402 GFP_KERNEL);
1403 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001404 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001405
1406 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001407 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001408 q_vector->adapter = adapter;
1409 q_vector->vsi = &adapter->vsi;
1410 q_vector->v_idx = q_idx;
1411 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001412 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001413 }
1414
1415 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001416}
1417
1418/**
1419 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1420 * @adapter: board private structure to initialize
1421 *
1422 * This function frees the memory allocated to the q_vectors. In addition if
1423 * NAPI is enabled it will delete any references to the NAPI struct prior
1424 * to freeing the q_vector.
1425 **/
1426static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1427{
1428 int q_idx, num_q_vectors;
1429 int napi_vectors;
1430
1431 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001432 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001433
1434 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001435 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001436 if (q_idx < napi_vectors)
1437 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001438 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001439 kfree(adapter->q_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001440}
1441
1442/**
1443 * i40evf_reset_interrupt_capability - Reset MSIX setup
1444 * @adapter: board private structure
1445 *
1446 **/
1447void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1448{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001449 if (!adapter->msix_entries)
1450 return;
1451
Greg Rose5eae00c2013-12-21 06:12:45 +00001452 pci_disable_msix(adapter->pdev);
1453 kfree(adapter->msix_entries);
1454 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001455}
1456
1457/**
1458 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1459 * @adapter: board private structure to initialize
1460 *
1461 **/
1462int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1463{
1464 int err;
1465
Jacob Keller62fe2a82016-07-27 12:02:33 -07001466 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001467 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001468 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001469 if (err) {
1470 dev_err(&adapter->pdev->dev,
1471 "Unable to setup interrupt capabilities\n");
1472 goto err_set_interrupt;
1473 }
1474
1475 err = i40evf_alloc_q_vectors(adapter);
1476 if (err) {
1477 dev_err(&adapter->pdev->dev,
1478 "Unable to allocate memory for queue vectors\n");
1479 goto err_alloc_q_vectors;
1480 }
1481
1482 err = i40evf_alloc_queues(adapter);
1483 if (err) {
1484 dev_err(&adapter->pdev->dev,
1485 "Unable to allocate memory for queues\n");
1486 goto err_alloc_queues;
1487 }
1488
1489 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001490 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1491 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001492
1493 return 0;
1494err_alloc_queues:
1495 i40evf_free_q_vectors(adapter);
1496err_alloc_q_vectors:
1497 i40evf_reset_interrupt_capability(adapter);
1498err_set_interrupt:
1499 return err;
1500}
1501
1502/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001503 * i40evf_free_rss - Free memory used by RSS structs
1504 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001505 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001506static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001507{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001508 kfree(adapter->rss_key);
1509 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001510
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001511 kfree(adapter->rss_lut);
1512 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001513}
1514
1515/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001516 * i40evf_watchdog_timer - Periodic call-back timer
1517 * @data: pointer to adapter disguised as unsigned long
1518 **/
1519static void i40evf_watchdog_timer(unsigned long data)
1520{
1521 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001522
Greg Rose5eae00c2013-12-21 06:12:45 +00001523 schedule_work(&adapter->watchdog_task);
1524 /* timer will be rescheduled in watchdog task */
1525}
1526
1527/**
1528 * i40evf_watchdog_task - Periodic call-back task
1529 * @work: pointer to work_struct
1530 **/
1531static void i40evf_watchdog_task(struct work_struct *work)
1532{
1533 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001534 struct i40evf_adapter,
1535 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001536 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001537 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001538
Greg Rose5eae00c2013-12-21 06:12:45 +00001539 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001540 goto restart_watchdog;
1541
1542 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001543 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1544 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1545 if ((reg_val == I40E_VFR_VFACTIVE) ||
1546 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001547 /* A chance for redemption! */
1548 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1549 adapter->state = __I40EVF_STARTUP;
1550 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1551 schedule_delayed_work(&adapter->init_task, 10);
1552 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1553 &adapter->crit_section);
1554 /* Don't reschedule the watchdog, since we've restarted
1555 * the init task. When init_task contacts the PF and
1556 * gets everything set up again, it'll restart the
1557 * watchdog for us. Down, boy. Sit. Stay. Woof.
1558 */
1559 return;
1560 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001561 adapter->aq_required = 0;
1562 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1563 goto watchdog_done;
1564 }
1565
1566 if ((adapter->state < __I40EVF_DOWN) ||
1567 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001568 goto watchdog_done;
1569
Mitch Williamsef8693e2014-02-13 03:48:53 -08001570 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001571 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1572 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001573 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001574 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001575 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001576 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001577 adapter->aq_required = 0;
1578 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001579 goto watchdog_done;
1580 }
1581
1582 /* Process admin queue tasks. After init, everything gets done
1583 * here so we don't race on the admin queue.
1584 */
Mitch Williamsed636962015-04-07 19:45:32 -04001585 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001586 if (!i40evf_asq_done(hw)) {
1587 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1588 i40evf_send_api_ver(adapter);
1589 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001590 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001591 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001592 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1593 i40evf_send_vf_config_msg(adapter);
1594 goto watchdog_done;
1595 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001596
Mitch Williamse284fc82015-03-27 00:12:09 -07001597 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1598 i40evf_disable_queues(adapter);
1599 goto watchdog_done;
1600 }
1601
Greg Rose5eae00c2013-12-21 06:12:45 +00001602 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1603 i40evf_map_queues(adapter);
1604 goto watchdog_done;
1605 }
1606
1607 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1608 i40evf_add_ether_addrs(adapter);
1609 goto watchdog_done;
1610 }
1611
1612 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1613 i40evf_add_vlans(adapter);
1614 goto watchdog_done;
1615 }
1616
1617 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1618 i40evf_del_ether_addrs(adapter);
1619 goto watchdog_done;
1620 }
1621
1622 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1623 i40evf_del_vlans(adapter);
1624 goto watchdog_done;
1625 }
1626
Greg Rose5eae00c2013-12-21 06:12:45 +00001627 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1628 i40evf_configure_queues(adapter);
1629 goto watchdog_done;
1630 }
1631
1632 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1633 i40evf_enable_queues(adapter);
1634 goto watchdog_done;
1635 }
1636
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001637 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1638 /* This message goes straight to the firmware, not the
1639 * PF, so we don't have to set current_op as we will
1640 * not get a response through the ARQ.
1641 */
Helin Zhang96a81982015-10-26 19:44:31 -04001642 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001643 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1644 goto watchdog_done;
1645 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001646 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1647 i40evf_get_hena(adapter);
1648 goto watchdog_done;
1649 }
1650 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1651 i40evf_set_hena(adapter);
1652 goto watchdog_done;
1653 }
1654 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1655 i40evf_set_rss_key(adapter);
1656 goto watchdog_done;
1657 }
1658 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1659 i40evf_set_rss_lut(adapter);
1660 goto watchdog_done;
1661 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001662
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001663 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1664 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_UNICAST_PROMISC |
1665 I40E_FLAG_VF_MULTICAST_PROMISC);
1666 goto watchdog_done;
1667 }
1668
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001669 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1670 i40evf_set_promiscuous(adapter, I40E_FLAG_VF_MULTICAST_PROMISC);
1671 goto watchdog_done;
1672 }
1673
1674 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1675 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001676 i40evf_set_promiscuous(adapter, 0);
1677 goto watchdog_done;
1678 }
1679
Greg Rose5eae00c2013-12-21 06:12:45 +00001680 if (adapter->state == __I40EVF_RUNNING)
1681 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001682watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001683 if (adapter->state == __I40EVF_RUNNING) {
1684 i40evf_irq_enable_queues(adapter, ~0);
1685 i40evf_fire_sw_int(adapter, 0xFF);
1686 } else {
1687 i40evf_fire_sw_int(adapter, 0x1);
1688 }
1689
Mitch Williamsef8693e2014-02-13 03:48:53 -08001690 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1691restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001692 if (adapter->state == __I40EVF_REMOVE)
1693 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001694 if (adapter->aq_required)
1695 mod_timer(&adapter->watchdog_timer,
1696 jiffies + msecs_to_jiffies(20));
1697 else
1698 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001699 schedule_work(&adapter->adminq_task);
1700}
1701
Joe Perchesdedecb62016-11-01 15:35:14 -07001702static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1703{
1704 struct i40evf_mac_filter *f, *ftmp;
1705 struct i40evf_vlan_filter *fv, *fvtmp;
1706
1707 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1708
1709 if (netif_running(adapter->netdev)) {
1710 set_bit(__I40E_DOWN, &adapter->vsi.state);
1711 netif_carrier_off(adapter->netdev);
1712 netif_tx_disable(adapter->netdev);
1713 adapter->link_up = false;
1714 i40evf_napi_disable_all(adapter);
1715 i40evf_irq_disable(adapter);
1716 i40evf_free_traffic_irqs(adapter);
1717 i40evf_free_all_tx_resources(adapter);
1718 i40evf_free_all_rx_resources(adapter);
1719 }
1720
1721 /* Delete all of the filters, both MAC and VLAN. */
1722 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1723 list_del(&f->list);
1724 kfree(f);
1725 }
1726
1727 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1728 list_del(&fv->list);
1729 kfree(fv);
1730 }
1731
1732 i40evf_free_misc_irq(adapter);
1733 i40evf_reset_interrupt_capability(adapter);
1734 i40evf_free_queues(adapter);
1735 i40evf_free_q_vectors(adapter);
1736 kfree(adapter->vf_res);
1737 i40evf_shutdown_adminq(&adapter->hw);
1738 adapter->netdev->flags &= ~IFF_UP;
1739 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1740 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1741 adapter->state = __I40EVF_DOWN;
1742 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1743}
1744
Mitch Williams67c818a2015-06-19 08:56:30 -07001745#define I40EVF_RESET_WAIT_MS 10
1746#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001747/**
1748 * i40evf_reset_task - Call-back task to handle hardware reset
1749 * @work: pointer to work_struct
1750 *
1751 * During reset we need to shut down and reinitialize the admin queue
1752 * before we can use it to communicate with the PF again. We also clear
1753 * and reinit the rings because that context is lost as well.
1754 **/
1755static void i40evf_reset_task(struct work_struct *work)
1756{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001757 struct i40evf_adapter *adapter = container_of(work,
1758 struct i40evf_adapter,
1759 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001760 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001761 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001762 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001763 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001764 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001765 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001766
1767 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1768 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001769 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001770
Mitch Williams67c818a2015-06-19 08:56:30 -07001771 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001772 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001773 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1774 /* Restart the AQ here. If we have been reset but didn't
1775 * detect it, or if the PF had to reinit, our AQ will be hosed.
1776 */
1777 i40evf_shutdown_adminq(hw);
1778 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001779 i40evf_request_reset(adapter);
1780 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001781 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001782
Mitch Williamsef8693e2014-02-13 03:48:53 -08001783 /* poll until we see the reset actually happen */
1784 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001785 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1786 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1787 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001788 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001789 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001790 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001791 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001792 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001793 goto continue_reset; /* act like the reset happened */
1794 }
1795
1796 /* wait until the reset is complete and the PF is responding to us */
1797 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001798 /* sleep first to make sure a minimum wait time is met */
1799 msleep(I40EVF_RESET_WAIT_MS);
1800
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001801 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1802 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1803 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001804 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001805 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001806
Mitch Williams509a4472015-12-23 12:05:52 -08001807 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001808
Mitch Williamsef8693e2014-02-13 03:48:53 -08001809 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001810 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001811 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001812 i40evf_disable_vf(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001813 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001814 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001815
1816continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001817 if (netif_running(adapter->netdev)) {
1818 netif_carrier_off(netdev);
1819 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001820 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001821 i40evf_napi_disable_all(adapter);
1822 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001823 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001824
Greg Rose5eae00c2013-12-21 06:12:45 +00001825 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001826 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1827
1828 /* free the Tx/Rx rings and descriptors, might be better to just
1829 * re-use them sometime in the future
1830 */
1831 i40evf_free_all_rx_resources(adapter);
1832 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001833
1834 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001835 i40evf_shutdown_adminq(hw);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001836 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001837 err = i40evf_init_adminq(hw);
1838 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001839 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1840 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001841
Mitch Williamse6d038d2015-06-04 16:23:58 -04001842 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1843 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001844
1845 /* re-add all MAC filters */
1846 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1847 f->add = true;
1848 }
1849 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001850 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1851 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001852 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001853 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001854 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Avinash Dayanandbf3a1782016-08-17 16:04:08 -07001855 /* Open RDMA Client again */
1856 adapter->aq_required |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
Greg Rose5eae00c2013-12-21 06:12:45 +00001857 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001858 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001859
1860 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1861
1862 if (netif_running(adapter->netdev)) {
1863 /* allocate transmit descriptors */
1864 err = i40evf_setup_all_tx_resources(adapter);
1865 if (err)
1866 goto reset_err;
1867
1868 /* allocate receive descriptors */
1869 err = i40evf_setup_all_rx_resources(adapter);
1870 if (err)
1871 goto reset_err;
1872
1873 i40evf_configure(adapter);
1874
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001875 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001876
1877 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001878 } else {
1879 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001880 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001881
Greg Rose5eae00c2013-12-21 06:12:45 +00001882 return;
1883reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001884 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001885 i40evf_close(adapter->netdev);
1886}
1887
1888/**
1889 * i40evf_adminq_task - worker thread to clean the admin queue
1890 * @work: pointer to work_struct containing our data
1891 **/
1892static void i40evf_adminq_task(struct work_struct *work)
1893{
1894 struct i40evf_adapter *adapter =
1895 container_of(work, struct i40evf_adapter, adminq_task);
1896 struct i40e_hw *hw = &adapter->hw;
1897 struct i40e_arq_event_info event;
1898 struct i40e_virtchnl_msg *v_msg;
1899 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001900 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001901 u16 pending;
1902
Mitch Williamsef8693e2014-02-13 03:48:53 -08001903 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001904 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001905
Mitch Williams1001dc32014-11-11 20:02:19 +00001906 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1907 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001908 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001909 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001910
Greg Rose5eae00c2013-12-21 06:12:45 +00001911 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1912 do {
1913 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001914 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001915 break; /* No event to process or error cleaning ARQ */
1916
1917 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1918 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001919 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001920 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001921 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001922 } while (pending);
1923
Mitch Williams67c818a2015-06-19 08:56:30 -07001924 if ((adapter->flags &
1925 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1926 adapter->state == __I40EVF_RESETTING)
1927 goto freedom;
1928
Mitch Williams912257e2014-05-22 06:32:07 +00001929 /* check for error indications */
1930 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08001931 if (val == 0xdeadbeef) /* indicates device in reset */
1932 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00001933 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001934 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001935 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001936 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001937 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001938 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001939 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001940 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001941 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001942 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001943 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001944 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001945 }
1946 if (oldval != val)
1947 wr32(hw, hw->aq.arq.len, val);
1948
1949 val = rd32(hw, hw->aq.asq.len);
1950 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001951 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001952 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001953 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001954 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001955 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001956 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001957 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001958 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001959 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001960 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001961 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001962 }
1963 if (oldval != val)
1964 wr32(hw, hw->aq.asq.len, val);
1965
Mitch Williams67c818a2015-06-19 08:56:30 -07001966freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001967 kfree(event.msg_buf);
1968out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001969 /* re-enable Admin queue interrupt cause */
1970 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001971}
1972
1973/**
1974 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1975 * @adapter: board private structure
1976 *
1977 * Free all transmit software resources
1978 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001979void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001980{
1981 int i;
1982
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08001983 if (!adapter->tx_rings)
1984 return;
1985
Mitch Williamscc052922014-10-25 03:24:34 +00001986 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04001987 if (adapter->tx_rings[i].desc)
1988 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001989}
1990
1991/**
1992 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1993 * @adapter: board private structure
1994 *
1995 * If this function returns with an error, then it's possible one or
1996 * more of the rings is populated (while the rest are not). It is the
1997 * callers duty to clean those orphaned rings.
1998 *
1999 * Return 0 on success, negative on failure
2000 **/
2001static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2002{
2003 int i, err = 0;
2004
Mitch Williamscc052922014-10-25 03:24:34 +00002005 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002006 adapter->tx_rings[i].count = adapter->tx_desc_count;
2007 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002008 if (!err)
2009 continue;
2010 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002011 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002012 break;
2013 }
2014
2015 return err;
2016}
2017
2018/**
2019 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2020 * @adapter: board private structure
2021 *
2022 * If this function returns with an error, then it's possible one or
2023 * more of the rings is populated (while the rest are not). It is the
2024 * callers duty to clean those orphaned rings.
2025 *
2026 * Return 0 on success, negative on failure
2027 **/
2028static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2029{
2030 int i, err = 0;
2031
Mitch Williamscc052922014-10-25 03:24:34 +00002032 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002033 adapter->rx_rings[i].count = adapter->rx_desc_count;
2034 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002035 if (!err)
2036 continue;
2037 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002038 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002039 break;
2040 }
2041 return err;
2042}
2043
2044/**
2045 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2046 * @adapter: board private structure
2047 *
2048 * Free all receive software resources
2049 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002050void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002051{
2052 int i;
2053
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002054 if (!adapter->rx_rings)
2055 return;
2056
Mitch Williamscc052922014-10-25 03:24:34 +00002057 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002058 if (adapter->rx_rings[i].desc)
2059 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002060}
2061
2062/**
2063 * i40evf_open - Called when a network interface is made active
2064 * @netdev: network interface device structure
2065 *
2066 * Returns 0 on success, negative value on failure
2067 *
2068 * The open entry point is called when a network interface is made
2069 * active by the system (IFF_UP). At this point all resources needed
2070 * for transmit and receive operations are allocated, the interrupt
2071 * handler is registered with the OS, the watchdog timer is started,
2072 * and the stack is notified that the interface is ready.
2073 **/
2074static int i40evf_open(struct net_device *netdev)
2075{
2076 struct i40evf_adapter *adapter = netdev_priv(netdev);
2077 int err;
2078
Mitch Williamsef8693e2014-02-13 03:48:53 -08002079 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2080 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2081 return -EIO;
2082 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002083
2084 if (adapter->state != __I40EVF_DOWN)
Greg Rose5eae00c2013-12-21 06:12:45 +00002085 return -EBUSY;
2086
2087 /* allocate transmit descriptors */
2088 err = i40evf_setup_all_tx_resources(adapter);
2089 if (err)
2090 goto err_setup_tx;
2091
2092 /* allocate receive descriptors */
2093 err = i40evf_setup_all_rx_resources(adapter);
2094 if (err)
2095 goto err_setup_rx;
2096
2097 /* clear any pending interrupts, may auto mask */
2098 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2099 if (err)
2100 goto err_req_irq;
2101
Mitch Williams44151cd2015-04-27 14:57:17 -04002102 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002103 i40evf_configure(adapter);
2104
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002105 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002106
2107 i40evf_irq_enable(adapter, true);
2108
2109 return 0;
2110
2111err_req_irq:
2112 i40evf_down(adapter);
2113 i40evf_free_traffic_irqs(adapter);
2114err_setup_rx:
2115 i40evf_free_all_rx_resources(adapter);
2116err_setup_tx:
2117 i40evf_free_all_tx_resources(adapter);
2118
2119 return err;
2120}
2121
2122/**
2123 * i40evf_close - Disables a network interface
2124 * @netdev: network interface device structure
2125 *
2126 * Returns 0, this is not allowed to fail
2127 *
2128 * The close entry point is called when an interface is de-activated
2129 * by the OS. The hardware is still under the drivers control, but
2130 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2131 * are freed, along with all transmit and receive resources.
2132 **/
2133static int i40evf_close(struct net_device *netdev)
2134{
2135 struct i40evf_adapter *adapter = netdev_priv(netdev);
2136
Mitch Williams209dc4d2015-12-09 15:50:27 -08002137 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002138 return 0;
2139
Mitch Williamsef8693e2014-02-13 03:48:53 -08002140
Greg Rose5eae00c2013-12-21 06:12:45 +00002141 set_bit(__I40E_DOWN, &adapter->vsi.state);
2142
2143 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002144 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002145 i40evf_free_traffic_irqs(adapter);
2146
Greg Rose5eae00c2013-12-21 06:12:45 +00002147 return 0;
2148}
2149
2150/**
2151 * i40evf_get_stats - Get System Network Statistics
2152 * @netdev: network interface device structure
2153 *
2154 * Returns the address of the device statistics structure.
2155 * The statistics are actually updated from the timer callback.
2156 **/
2157static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2158{
2159 struct i40evf_adapter *adapter = netdev_priv(netdev);
2160
2161 /* only return the current stats */
2162 return &adapter->net_stats;
2163}
2164
2165/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002166 * i40evf_change_mtu - Change the Maximum Transfer Unit
2167 * @netdev: network interface device structure
2168 * @new_mtu: new value for maximum frame size
2169 *
2170 * Returns 0 on success, negative on failure
2171 **/
2172static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2173{
2174 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002175
Greg Rose5eae00c2013-12-21 06:12:45 +00002176 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002177 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2178 schedule_work(&adapter->reset_task);
2179
Greg Rose5eae00c2013-12-21 06:12:45 +00002180 return 0;
2181}
2182
Alexander Duyck06fc0162016-10-25 16:08:47 -07002183/**
2184 * i40evf_features_check - Validate encapsulated packet conforms to limits
2185 * @skb: skb buff
2186 * @netdev: This physical port's netdev
2187 * @features: Offload features that the stack believes apply
2188 **/
2189static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2190 struct net_device *dev,
2191 netdev_features_t features)
2192{
2193 size_t len;
2194
2195 /* No point in doing any of this if neither checksum nor GSO are
2196 * being requested for this frame. We can rule out both by just
2197 * checking for CHECKSUM_PARTIAL
2198 */
2199 if (skb->ip_summed != CHECKSUM_PARTIAL)
2200 return features;
2201
2202 /* We cannot support GSO if the MSS is going to be less than
2203 * 64 bytes. If it is then we need to drop support for GSO.
2204 */
2205 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2206 features &= ~NETIF_F_GSO_MASK;
2207
2208 /* MACLEN can support at most 63 words */
2209 len = skb_network_header(skb) - skb->data;
2210 if (len & ~(63 * 2))
2211 goto out_err;
2212
2213 /* IPLEN and EIPLEN can support at most 127 dwords */
2214 len = skb_transport_header(skb) - skb_network_header(skb);
2215 if (len & ~(127 * 4))
2216 goto out_err;
2217
2218 if (skb->encapsulation) {
2219 /* L4TUNLEN can support 127 words */
2220 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2221 if (len & ~(127 * 2))
2222 goto out_err;
2223
2224 /* IPLEN can support at most 127 dwords */
2225 len = skb_inner_transport_header(skb) -
2226 skb_inner_network_header(skb);
2227 if (len & ~(127 * 4))
2228 goto out_err;
2229 }
2230
2231 /* No need to validate L4LEN as TCP is the only protocol with a
2232 * a flexible value and we support all possible values supported
2233 * by TCP, which is at most 15 dwords
2234 */
2235
2236 return features;
2237out_err:
2238 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2239}
2240
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002241#define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2242 NETIF_F_HW_VLAN_CTAG_RX |\
2243 NETIF_F_HW_VLAN_CTAG_FILTER)
2244
2245/**
2246 * i40evf_fix_features - fix up the netdev feature bits
2247 * @netdev: our net device
2248 * @features: desired feature bits
2249 *
2250 * Returns fixed-up features bits
2251 **/
2252static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2253 netdev_features_t features)
2254{
2255 struct i40evf_adapter *adapter = netdev_priv(netdev);
2256
2257 features &= ~I40EVF_VLAN_FEATURES;
2258 if (adapter->vf_res->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN)
2259 features |= I40EVF_VLAN_FEATURES;
2260 return features;
2261}
2262
Greg Rose5eae00c2013-12-21 06:12:45 +00002263static const struct net_device_ops i40evf_netdev_ops = {
2264 .ndo_open = i40evf_open,
2265 .ndo_stop = i40evf_close,
2266 .ndo_start_xmit = i40evf_xmit_frame,
2267 .ndo_get_stats = i40evf_get_stats,
2268 .ndo_set_rx_mode = i40evf_set_rx_mode,
2269 .ndo_validate_addr = eth_validate_addr,
2270 .ndo_set_mac_address = i40evf_set_mac,
2271 .ndo_change_mtu = i40evf_change_mtu,
2272 .ndo_tx_timeout = i40evf_tx_timeout,
2273 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2274 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002275 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002276 .ndo_fix_features = i40evf_fix_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002277#ifdef CONFIG_NET_POLL_CONTROLLER
2278 .ndo_poll_controller = i40evf_netpoll,
2279#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002280};
2281
2282/**
2283 * i40evf_check_reset_complete - check that VF reset is complete
2284 * @hw: pointer to hw struct
2285 *
2286 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2287 **/
2288static int i40evf_check_reset_complete(struct i40e_hw *hw)
2289{
2290 u32 rstat;
2291 int i;
2292
2293 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002294 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2295 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2296 if ((rstat == I40E_VFR_VFACTIVE) ||
2297 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002298 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002299 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002300 }
2301 return -EBUSY;
2302}
2303
2304/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002305 * i40evf_process_config - Process the config information we got from the PF
2306 * @adapter: board private structure
2307 *
2308 * Verify that we have a valid config struct, and set up our netdev features
2309 * and our VSI struct.
2310 **/
2311int i40evf_process_config(struct i40evf_adapter *adapter)
2312{
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002313 struct i40e_virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002314 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002315 struct i40e_vsi *vsi = &adapter->vsi;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002316 int i;
2317
2318 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002319 for (i = 0; i < vfres->num_vsis; i++) {
2320 if (vfres->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2321 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002322 }
2323 if (!adapter->vsi_res) {
2324 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2325 return -ENODEV;
2326 }
2327
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002328 netdev->hw_enc_features |= NETIF_F_SG |
2329 NETIF_F_IP_CSUM |
2330 NETIF_F_IPV6_CSUM |
2331 NETIF_F_HIGHDMA |
2332 NETIF_F_SOFT_FEATURES |
2333 NETIF_F_TSO |
2334 NETIF_F_TSO_ECN |
2335 NETIF_F_TSO6 |
2336 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002337 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002338 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002339 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002340 NETIF_F_GSO_UDP_TUNNEL |
2341 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002342 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002343 NETIF_F_SCTP_CRC |
2344 NETIF_F_RXHASH |
2345 NETIF_F_RXCSUM |
2346 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002347
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002348 if (!(adapter->flags & I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002349 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2350
2351 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002352
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002353 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002354 netdev->vlan_features |= netdev->hw_enc_features |
2355 NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002356
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002357 /* Write features and hw_features separately to avoid polluting
2358 * with, or dropping, features that are set when we registgered.
2359 */
2360 netdev->hw_features |= netdev->hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002361
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002362 netdev->features |= netdev->hw_enc_features | I40EVF_VLAN_FEATURES;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002363 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002364
2365 /* disable VLAN features if not supported */
2366 if (!(vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_VLAN))
2367 netdev->features ^= I40EVF_VLAN_FEATURES;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002368
2369 adapter->vsi.id = adapter->vsi_res->vsi_id;
2370
2371 adapter->vsi.back = adapter;
2372 adapter->vsi.base_vector = 1;
2373 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002374 vsi->netdev = adapter->netdev;
2375 vsi->qs_handle = adapter->vsi_res->qset_handle;
2376 if (vfres->vf_offload_flags & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2377 adapter->rss_key_size = vfres->rss_key_size;
2378 adapter->rss_lut_size = vfres->rss_lut_size;
2379 } else {
2380 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2381 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2382 }
2383
Mitch Williamse6d038d2015-06-04 16:23:58 -04002384 return 0;
2385}
2386
2387/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002388 * i40evf_init_task - worker thread to perform delayed initialization
2389 * @work: pointer to work_struct containing our data
2390 *
2391 * This task completes the work that was begun in probe. Due to the nature
2392 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002393 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002394 * whole system, we'll do it in a task so we can sleep.
2395 * This task only runs during driver init. Once we've established
2396 * communications with the PF driver and set up our netdev, the watchdog
2397 * takes over.
2398 **/
2399static void i40evf_init_task(struct work_struct *work)
2400{
2401 struct i40evf_adapter *adapter = container_of(work,
2402 struct i40evf_adapter,
2403 init_task.work);
2404 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002405 struct i40e_hw *hw = &adapter->hw;
2406 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002407 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002408
2409 switch (adapter->state) {
2410 case __I40EVF_STARTUP:
2411 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002412 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2413 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002414 err = i40e_set_mac_type(hw);
2415 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002416 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2417 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002418 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002419 }
2420 err = i40evf_check_reset_complete(hw);
2421 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002422 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002423 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002424 goto err;
2425 }
2426 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2427 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2428 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2429 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2430
2431 err = i40evf_init_adminq(hw);
2432 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002433 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2434 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002435 goto err;
2436 }
2437 err = i40evf_send_api_ver(adapter);
2438 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002439 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002440 i40evf_shutdown_adminq(hw);
2441 goto err;
2442 }
2443 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2444 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002445 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002446 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002447 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002448 i40evf_shutdown_adminq(hw);
2449 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002450 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002451 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002452
2453 /* aq msg sent, awaiting reply */
2454 err = i40evf_verify_api_ver(adapter);
2455 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002456 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002457 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002458 else
2459 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2460 adapter->pf_version.major,
2461 adapter->pf_version.minor,
2462 I40E_VIRTCHNL_VERSION_MAJOR,
2463 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002464 goto err;
2465 }
2466 err = i40evf_send_vf_config_msg(adapter);
2467 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002468 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002469 err);
2470 goto err;
2471 }
2472 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2473 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002474 case __I40EVF_INIT_GET_RESOURCES:
2475 /* aq msg sent, awaiting reply */
2476 if (!adapter->vf_res) {
2477 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2478 (I40E_MAX_VF_VSI *
2479 sizeof(struct i40e_virtchnl_vsi_resource));
2480 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002481 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002482 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002483 }
2484 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002485 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002486 err = i40evf_send_vf_config_msg(adapter);
2487 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002488 } else if (err == I40E_ERR_PARAM) {
2489 /* We only get ERR_PARAM if the device is in a very bad
2490 * state or if we've been disabled for previous bad
2491 * behavior. Either way, we're done now.
2492 */
2493 i40evf_shutdown_adminq(hw);
2494 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2495 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002496 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002497 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002498 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2499 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002500 goto err_alloc;
2501 }
2502 adapter->state = __I40EVF_INIT_SW;
2503 break;
2504 default:
2505 goto err_alloc;
2506 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002507
2508 if (hw->mac.type == I40E_MAC_X722_VF)
2509 adapter->flags |= I40EVF_FLAG_OUTER_UDP_CSUM_CAPABLE;
2510
Mitch Williamse6d038d2015-06-04 16:23:58 -04002511 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002512 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002513 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002514
2515 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2516
Greg Rose5eae00c2013-12-21 06:12:45 +00002517 netdev->netdev_ops = &i40evf_netdev_ops;
2518 i40evf_set_ethtool_ops(netdev);
2519 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002520
Jarod Wilson91c527a2016-10-17 15:54:05 -04002521 /* MTU range: 68 - 9710 */
2522 netdev->min_mtu = ETH_MIN_MTU;
2523 netdev->max_mtu = I40E_MAX_RXBUFFER - (ETH_HLEN + ETH_FCS_LEN);
2524
Greg Rose5eae00c2013-12-21 06:12:45 +00002525 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002526 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002527 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002528 eth_hw_addr_random(netdev);
2529 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2530 } else {
2531 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2532 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2533 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002534 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002535
Greg Rose5eae00c2013-12-21 06:12:45 +00002536 init_timer(&adapter->watchdog_timer);
2537 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2538 adapter->watchdog_timer.data = (unsigned long)adapter;
2539 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2540
Mitch Williamscc052922014-10-25 03:24:34 +00002541 adapter->num_active_queues = min_t(int,
2542 adapter->vsi_res->num_queue_pairs,
2543 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002544 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2545 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002546 err = i40evf_init_interrupt_scheme(adapter);
2547 if (err)
2548 goto err_sw_init;
2549 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002550 if (adapter->vf_res->vf_offload_flags &
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002551 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2552 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2553
Greg Rose5eae00c2013-12-21 06:12:45 +00002554 err = i40evf_request_misc_irq(adapter);
2555 if (err)
2556 goto err_sw_init;
2557
2558 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002559 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002560
Mitch Williamsef8693e2014-02-13 03:48:53 -08002561 if (!adapter->netdev_registered) {
2562 err = register_netdev(netdev);
2563 if (err)
2564 goto err_register;
2565 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002566
2567 adapter->netdev_registered = true;
2568
2569 netif_tx_stop_all_queues(netdev);
2570
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002571 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002572 if (netdev->features & NETIF_F_GRO)
2573 dev_info(&pdev->dev, "GRO is enabled\n");
2574
Greg Rose5eae00c2013-12-21 06:12:45 +00002575 adapter->state = __I40EVF_DOWN;
2576 set_bit(__I40E_DOWN, &adapter->vsi.state);
2577 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002578
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002579 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2580 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2581 if (!adapter->rss_key || !adapter->rss_lut)
2582 goto err_mem;
2583
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002584 if (RSS_AQ(adapter)) {
2585 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2586 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2587 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002588 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002589 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002590 return;
2591restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002592 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002593 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002594err_mem:
2595 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002596err_register:
2597 i40evf_free_misc_irq(adapter);
2598err_sw_init:
2599 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002600err_alloc:
2601 kfree(adapter->vf_res);
2602 adapter->vf_res = NULL;
2603err:
2604 /* Things went into the weeds, so try again later */
2605 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002606 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002607 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002608 i40evf_shutdown_adminq(hw);
2609 adapter->state = __I40EVF_STARTUP;
2610 schedule_delayed_work(&adapter->init_task, HZ * 5);
2611 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002612 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002613 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002614}
2615
2616/**
2617 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2618 * @pdev: pci device structure
2619 **/
2620static void i40evf_shutdown(struct pci_dev *pdev)
2621{
2622 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002623 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002624
2625 netif_device_detach(netdev);
2626
2627 if (netif_running(netdev))
2628 i40evf_close(netdev);
2629
Mitch Williams00293fd2015-01-09 11:18:18 +00002630 /* Prevent the watchdog from running. */
2631 adapter->state = __I40EVF_REMOVE;
2632 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002633
Greg Rose5eae00c2013-12-21 06:12:45 +00002634#ifdef CONFIG_PM
2635 pci_save_state(pdev);
2636
2637#endif
2638 pci_disable_device(pdev);
2639}
2640
2641/**
2642 * i40evf_probe - Device Initialization Routine
2643 * @pdev: PCI device information struct
2644 * @ent: entry in i40evf_pci_tbl
2645 *
2646 * Returns 0 on success, negative on failure
2647 *
2648 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2649 * The OS initialization, configuring of the adapter private structure,
2650 * and a hardware reset occur.
2651 **/
2652static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2653{
2654 struct net_device *netdev;
2655 struct i40evf_adapter *adapter = NULL;
2656 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002657 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002658
2659 err = pci_enable_device(pdev);
2660 if (err)
2661 return err;
2662
Mitch Williams64942942014-02-11 08:26:33 +00002663 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002664 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002665 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2666 if (err) {
2667 dev_err(&pdev->dev,
2668 "DMA configuration failed: 0x%x\n", err);
2669 goto err_dma;
2670 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002671 }
2672
2673 err = pci_request_regions(pdev, i40evf_driver_name);
2674 if (err) {
2675 dev_err(&pdev->dev,
2676 "pci_request_regions failed 0x%x\n", err);
2677 goto err_pci_reg;
2678 }
2679
2680 pci_enable_pcie_error_reporting(pdev);
2681
2682 pci_set_master(pdev);
2683
Mitch Williams1255b7a2015-11-06 15:25:59 -08002684 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002685 if (!netdev) {
2686 err = -ENOMEM;
2687 goto err_alloc_etherdev;
2688 }
2689
2690 SET_NETDEV_DEV(netdev, &pdev->dev);
2691
2692 pci_set_drvdata(pdev, netdev);
2693 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002694
2695 adapter->netdev = netdev;
2696 adapter->pdev = pdev;
2697
2698 hw = &adapter->hw;
2699 hw->back = adapter;
2700
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002701 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002702 adapter->state = __I40EVF_STARTUP;
2703
2704 /* Call save state here because it relies on the adapter struct. */
2705 pci_save_state(pdev);
2706
2707 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2708 pci_resource_len(pdev, 0));
2709 if (!hw->hw_addr) {
2710 err = -EIO;
2711 goto err_ioremap;
2712 }
2713 hw->vendor_id = pdev->vendor;
2714 hw->device_id = pdev->device;
2715 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2716 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2717 hw->subsystem_device_id = pdev->subsystem_device;
2718 hw->bus.device = PCI_SLOT(pdev->devfn);
2719 hw->bus.func = PCI_FUNC(pdev->devfn);
2720
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002721 /* set up the locks for the AQ, do this only once in probe
2722 * and destroy them only once in remove
2723 */
2724 mutex_init(&hw->aq.asq_mutex);
2725 mutex_init(&hw->aq.arq_mutex);
2726
Serey Kong8bb1a542014-08-01 13:27:15 -07002727 INIT_LIST_HEAD(&adapter->mac_filter_list);
2728 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2729
Greg Rose5eae00c2013-12-21 06:12:45 +00002730 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2731 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2732 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2733 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002734 schedule_delayed_work(&adapter->init_task,
2735 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002736
2737 return 0;
2738
2739err_ioremap:
2740 free_netdev(netdev);
2741err_alloc_etherdev:
2742 pci_release_regions(pdev);
2743err_pci_reg:
2744err_dma:
2745 pci_disable_device(pdev);
2746 return err;
2747}
2748
2749#ifdef CONFIG_PM
2750/**
2751 * i40evf_suspend - Power management suspend routine
2752 * @pdev: PCI device information struct
2753 * @state: unused
2754 *
2755 * Called when the system (VM) is entering sleep/suspend.
2756 **/
2757static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2758{
2759 struct net_device *netdev = pci_get_drvdata(pdev);
2760 struct i40evf_adapter *adapter = netdev_priv(netdev);
2761 int retval = 0;
2762
2763 netif_device_detach(netdev);
2764
2765 if (netif_running(netdev)) {
2766 rtnl_lock();
2767 i40evf_down(adapter);
2768 rtnl_unlock();
2769 }
2770 i40evf_free_misc_irq(adapter);
2771 i40evf_reset_interrupt_capability(adapter);
2772
2773 retval = pci_save_state(pdev);
2774 if (retval)
2775 return retval;
2776
2777 pci_disable_device(pdev);
2778
2779 return 0;
2780}
2781
2782/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002783 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002784 * @pdev: PCI device information struct
2785 *
2786 * Called when the system (VM) is resumed from sleep/suspend.
2787 **/
2788static int i40evf_resume(struct pci_dev *pdev)
2789{
2790 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2791 struct net_device *netdev = adapter->netdev;
2792 u32 err;
2793
2794 pci_set_power_state(pdev, PCI_D0);
2795 pci_restore_state(pdev);
2796 /* pci_restore_state clears dev->state_saved so call
2797 * pci_save_state to restore it.
2798 */
2799 pci_save_state(pdev);
2800
2801 err = pci_enable_device_mem(pdev);
2802 if (err) {
2803 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2804 return err;
2805 }
2806 pci_set_master(pdev);
2807
2808 rtnl_lock();
2809 err = i40evf_set_interrupt_capability(adapter);
2810 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002811 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002812 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2813 return err;
2814 }
2815 err = i40evf_request_misc_irq(adapter);
2816 rtnl_unlock();
2817 if (err) {
2818 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2819 return err;
2820 }
2821
2822 schedule_work(&adapter->reset_task);
2823
2824 netif_device_attach(netdev);
2825
2826 return err;
2827}
2828
2829#endif /* CONFIG_PM */
2830/**
2831 * i40evf_remove - Device Removal Routine
2832 * @pdev: PCI device information struct
2833 *
2834 * i40evf_remove is called by the PCI subsystem to alert the driver
2835 * that it should release a PCI device. The could be caused by a
2836 * Hot-Plug event, or because the driver is going to be removed from
2837 * memory.
2838 **/
2839static void i40evf_remove(struct pci_dev *pdev)
2840{
2841 struct net_device *netdev = pci_get_drvdata(pdev);
2842 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002843 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002844 struct i40e_hw *hw = &adapter->hw;
2845
2846 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002847 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002848
2849 if (adapter->netdev_registered) {
2850 unregister_netdev(netdev);
2851 adapter->netdev_registered = false;
2852 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002853
Mitch Williamsf4a71882015-01-09 11:18:16 +00002854 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002855 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002856 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002857 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002858 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002859 /* If the FW isn't responding, kick it once, but only once. */
2860 if (!i40evf_asq_done(hw)) {
2861 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07002862 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00002863 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002864
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002865 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002866 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002867 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002868 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002869 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002870 }
2871
Mitch Williamse5d17c32014-06-04 04:22:38 +00002872 if (adapter->watchdog_timer.function)
2873 del_timer_sync(&adapter->watchdog_timer);
2874
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002875 flush_scheduled_work();
2876
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002877 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04002878
Greg Rose5eae00c2013-12-21 06:12:45 +00002879 if (hw->aq.asq.count)
2880 i40evf_shutdown_adminq(hw);
2881
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002882 /* destroy the locks only once, here */
2883 mutex_destroy(&hw->aq.arq_mutex);
2884 mutex_destroy(&hw->aq.asq_mutex);
2885
Greg Rose5eae00c2013-12-21 06:12:45 +00002886 iounmap(hw->hw_addr);
2887 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07002888 i40evf_free_all_tx_resources(adapter);
2889 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002890 i40evf_free_queues(adapter);
2891 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002892 /* If we got removed before an up/down sequence, we've got a filter
2893 * hanging out there that we need to get rid of.
2894 */
2895 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2896 list_del(&f->list);
2897 kfree(f);
2898 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002899 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2900 list_del(&f->list);
2901 kfree(f);
2902 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002903
2904 free_netdev(netdev);
2905
2906 pci_disable_pcie_error_reporting(pdev);
2907
2908 pci_disable_device(pdev);
2909}
2910
2911static struct pci_driver i40evf_driver = {
2912 .name = i40evf_driver_name,
2913 .id_table = i40evf_pci_tbl,
2914 .probe = i40evf_probe,
2915 .remove = i40evf_remove,
2916#ifdef CONFIG_PM
2917 .suspend = i40evf_suspend,
2918 .resume = i40evf_resume,
2919#endif
2920 .shutdown = i40evf_shutdown,
2921};
2922
2923/**
2924 * i40e_init_module - Driver Registration Routine
2925 *
2926 * i40e_init_module is the first routine called when the driver is
2927 * loaded. All it does is register with the PCI subsystem.
2928 **/
2929static int __init i40evf_init_module(void)
2930{
2931 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002932
Greg Rose5eae00c2013-12-21 06:12:45 +00002933 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002934 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002935
2936 pr_info("%s\n", i40evf_copyright);
2937
Jacob Keller6992a6c2016-08-04 11:37:01 -07002938 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
2939 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002940 if (!i40evf_wq) {
2941 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
2942 return -ENOMEM;
2943 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002944 ret = pci_register_driver(&i40evf_driver);
2945 return ret;
2946}
2947
2948module_init(i40evf_init_module);
2949
2950/**
2951 * i40e_exit_module - Driver Exit Cleanup Routine
2952 *
2953 * i40e_exit_module is called just before the driver is removed
2954 * from memory.
2955 **/
2956static void __exit i40evf_exit_module(void)
2957{
2958 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08002959 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00002960}
2961
2962module_exit(i40evf_exit_module);
2963
2964/* i40evf_main.c */