blob: 099d4f59e44533c860590dc2085f77f92c646c23 [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Catherine Sullivaneaab59e2016-01-13 16:51:44 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Greg Rose5eae00c2013-12-21 06:12:45 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
Greg Rose5eae00c2013-12-21 06:12:45 +000018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40evf.h"
28#include "i40e_prototype.h"
Mitch Williamsed0e8942017-01-24 10:23:59 -080029#include "i40evf_client.h"
Scott Petersoned0980c2017-04-13 04:45:44 -040030/* All i40evf tracepoints are defined by the include below, which must
31 * be included exactly once across the whole kernel with
32 * CREATE_TRACE_POINTS defined
33 */
34#define CREATE_TRACE_POINTS
35#include "i40e_trace.h"
36
Greg Rose5eae00c2013-12-21 06:12:45 +000037static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39static int i40evf_close(struct net_device *netdev);
40
41char i40evf_driver_name[] = "i40evf";
42static const char i40evf_driver_string[] =
Catherine Sullivaneaab59e2016-01-13 16:51:44 -080043 "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000044
Mitch Williams69ebe9552015-11-19 11:34:24 -080045#define DRV_KERN "-k"
46
Preethi Banalaabf709a2017-05-11 11:23:20 -070047#define DRV_VERSION_MAJOR 3
Alice Michaelf2fc31e2017-10-27 11:06:56 -040048#define DRV_VERSION_MINOR 2
49#define DRV_VERSION_BUILD 2
Mitch Williams69ebe9552015-11-19 11:34:24 -080050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) \
53 DRV_KERN
Greg Rose5eae00c2013-12-21 06:12:45 +000054const char i40evf_driver_version[] = DRV_VERSION;
55static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040056 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000057
58/* i40evf_pci_tbl - PCI Device ID Table
59 *
60 * Wildcard entries (PCI_ANY_ID) should come last
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020066static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080067 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Joshua Hay92871412016-06-20 09:10:37 -070068 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Preethi Banalaabf709a2017-05-11 11:23:20 -070070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000071 /* required last entry */
72 {0, }
73};
74
75MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
76
77MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(DRV_VERSION);
81
Jesse Brandeburg2803b162015-12-22 14:25:08 -080082static struct workqueue_struct *i40evf_wq;
83
Greg Rose5eae00c2013-12-21 06:12:45 +000084/**
85 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
86 * @hw: pointer to the HW structure
87 * @mem: ptr to mem struct to fill out
88 * @size: size of memory requested
89 * @alignment: what to align the allocation to
90 **/
91i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92 struct i40e_dma_mem *mem,
93 u64 size, u32 alignment)
94{
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
96
97 if (!mem)
98 return I40E_ERR_PARAM;
99
100 mem->size = ALIGN(size, alignment);
101 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
102 (dma_addr_t *)&mem->pa, GFP_KERNEL);
103 if (mem->va)
104 return 0;
105 else
106 return I40E_ERR_NO_MEMORY;
107}
108
109/**
110 * i40evf_free_dma_mem_d - OS specific memory free for shared code
111 * @hw: pointer to the HW structure
112 * @mem: ptr to mem struct to free
113 **/
114i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
115{
116 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
117
118 if (!mem || !mem->va)
119 return I40E_ERR_PARAM;
120 dma_free_coherent(&adapter->pdev->dev, mem->size,
121 mem->va, (dma_addr_t)mem->pa);
122 return 0;
123}
124
125/**
126 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to fill out
129 * @size: size of memory requested
130 **/
131i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem, u32 size)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 mem->size = size;
138 mem->va = kzalloc(size, GFP_KERNEL);
139
140 if (mem->va)
141 return 0;
142 else
143 return I40E_ERR_NO_MEMORY;
144}
145
146/**
147 * i40evf_free_virt_mem_d - OS specific memory free for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to free
150 **/
151i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152 struct i40e_virt_mem *mem)
153{
154 if (!mem)
155 return I40E_ERR_PARAM;
156
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159
160 return 0;
161}
162
163/**
164 * i40evf_debug_d - OS dependent version of debug printing
165 * @hw: pointer to the HW structure
166 * @mask: debug level mask
167 * @fmt_str: printf-type format description
168 **/
169void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
170{
171 char buf[512];
172 va_list argptr;
173
174 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
175 return;
176
177 va_start(argptr, fmt_str);
178 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
179 va_end(argptr);
180
181 /* the debug string is already formatted with a newline */
182 pr_info("%s", buf);
183}
184
185/**
Mitch Williams00e5ec42016-01-15 14:33:10 -0800186 * i40evf_schedule_reset - Set the flags and schedule a reset event
187 * @adapter: board private structure
188 **/
189void i40evf_schedule_reset(struct i40evf_adapter *adapter)
190{
191 if (!(adapter->flags &
192 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
193 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
194 schedule_work(&adapter->reset_task);
195 }
196}
197
198/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000199 * i40evf_tx_timeout - Respond to a Tx Hang
200 * @netdev: network interface device structure
201 **/
202static void i40evf_tx_timeout(struct net_device *netdev)
203{
204 struct i40evf_adapter *adapter = netdev_priv(netdev);
205
206 adapter->tx_timeout_count++;
Mitch Williams00e5ec42016-01-15 14:33:10 -0800207 i40evf_schedule_reset(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000208}
209
210/**
211 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212 * @adapter: board private structure
213 **/
214static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
215{
216 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000217
Jacob Kelleref4603e2016-11-08 13:05:08 -0800218 if (!adapter->msix_entries)
219 return;
220
Greg Rose5eae00c2013-12-21 06:12:45 +0000221 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
222
223 /* read flush */
224 rd32(hw, I40E_VFGEN_RSTAT);
225
226 synchronize_irq(adapter->msix_entries[0].vector);
227}
228
229/**
230 * i40evf_misc_irq_enable - Enable default interrupt generation settings
231 * @adapter: board private structure
232 **/
233static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
234{
235 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000236
Greg Rose5eae00c2013-12-21 06:12:45 +0000237 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400239 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000240
241 /* read flush */
242 rd32(hw, I40E_VFGEN_RSTAT);
243}
244
245/**
246 * i40evf_irq_disable - Mask off interrupt generation on the NIC
247 * @adapter: board private structure
248 **/
249static void i40evf_irq_disable(struct i40evf_adapter *adapter)
250{
251 int i;
252 struct i40e_hw *hw = &adapter->hw;
253
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800254 if (!adapter->msix_entries)
255 return;
256
Greg Rose5eae00c2013-12-21 06:12:45 +0000257 for (i = 1; i < adapter->num_msix_vectors; i++) {
258 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
259 synchronize_irq(adapter->msix_entries[i].vector);
260 }
261 /* read flush */
262 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000263}
264
265/**
266 * i40evf_irq_enable_queues - Enable interrupt for specified queues
267 * @adapter: board private structure
268 * @mask: bitmap of queues to enable
269 **/
270void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
271{
272 struct i40e_hw *hw = &adapter->hw;
273 int i;
274
275 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400276 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Alexander Duyck498860c2017-11-14 07:00:44 -0500279 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000280 }
281 }
282}
283
284/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000285 * i40evf_irq_enable - Enable default interrupt generation settings
286 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600287 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000288 **/
289void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
290{
291 struct i40e_hw *hw = &adapter->hw;
292
Mitch Williams164ec1b2014-06-04 08:45:19 +0000293 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000294 i40evf_irq_enable_queues(adapter, ~0);
295
296 if (flush)
297 rd32(hw, I40E_VFGEN_RSTAT);
298}
299
300/**
301 * i40evf_msix_aq - Interrupt handler for vector 0
302 * @irq: interrupt number
303 * @data: pointer to netdev
304 **/
305static irqreturn_t i40evf_msix_aq(int irq, void *data)
306{
307 struct net_device *netdev = data;
308 struct i40evf_adapter *adapter = netdev_priv(netdev);
309 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000310
Jesse Brandeburgcfbe4db2015-10-04 01:09:49 -0700311 /* handle non-queue interrupts, these reads clear the registers */
Alexander Duyck498860c2017-11-14 07:00:44 -0500312 rd32(hw, I40E_VFINT_ICR01);
313 rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000314
Greg Rose5eae00c2013-12-21 06:12:45 +0000315 /* schedule work on the private workqueue */
316 schedule_work(&adapter->adminq_task);
317
318 return IRQ_HANDLED;
319}
320
321/**
322 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
323 * @irq: interrupt number
324 * @data: pointer to a q_vector
325 **/
326static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
327{
328 struct i40e_q_vector *q_vector = data;
329
330 if (!q_vector->tx.ring && !q_vector->rx.ring)
331 return IRQ_HANDLED;
332
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700333 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000334
335 return IRQ_HANDLED;
336}
337
338/**
339 * i40evf_map_vector_to_rxq - associate irqs with rx queues
340 * @adapter: board private structure
341 * @v_idx: interrupt number
342 * @r_idx: queue number
343 **/
344static void
345i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
346{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400347 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400348 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700349 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000350
351 rx_ring->q_vector = q_vector;
352 rx_ring->next = q_vector->rx.ring;
353 rx_ring->vsi = &adapter->vsi;
354 q_vector->rx.ring = rx_ring;
355 q_vector->rx.count++;
Alexander Duycka0073a42017-12-29 08:52:19 -0500356 q_vector->rx.next_update = jiffies + 1;
Alexander Duyck556fdfd2017-12-29 08:51:25 -0500357 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700358 q_vector->ring_mask |= BIT(r_idx);
Alexander Duyck8b99b112017-12-29 08:50:44 -0500359 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, q_vector->reg_idx),
Alexander Duyck556fdfd2017-12-29 08:51:25 -0500360 q_vector->rx.current_itr);
361 q_vector->rx.current_itr = q_vector->rx.target_itr;
Greg Rose5eae00c2013-12-21 06:12:45 +0000362}
363
364/**
365 * i40evf_map_vector_to_txq - associate irqs with tx queues
366 * @adapter: board private structure
367 * @v_idx: interrupt number
368 * @t_idx: queue number
369 **/
370static void
371i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
372{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400373 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400374 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700375 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000376
377 tx_ring->q_vector = q_vector;
378 tx_ring->next = q_vector->tx.ring;
379 tx_ring->vsi = &adapter->vsi;
380 q_vector->tx.ring = tx_ring;
381 q_vector->tx.count++;
Alexander Duycka0073a42017-12-29 08:52:19 -0500382 q_vector->tx.next_update = jiffies + 1;
Alexander Duyck556fdfd2017-12-29 08:51:25 -0500383 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
Greg Rose5eae00c2013-12-21 06:12:45 +0000384 q_vector->num_ringpairs++;
Alexander Duyck8b99b112017-12-29 08:50:44 -0500385 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, q_vector->reg_idx),
Alexander Duyck556fdfd2017-12-29 08:51:25 -0500386 q_vector->tx.target_itr);
387 q_vector->tx.current_itr = q_vector->tx.target_itr;
Greg Rose5eae00c2013-12-21 06:12:45 +0000388}
389
390/**
391 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
392 * @adapter: board private structure to initialize
393 *
394 * This function maps descriptor rings to the queue-specific vectors
395 * we were allotted through the MSI-X enabling code. Ideally, we'd have
396 * one vector per ring/queue, but on a constrained vector budget, we
397 * group the rings as "efficiently" as possible. You would add new
398 * mapping configurations in here.
399 **/
Mitch Williams1b7b7592017-08-22 06:57:51 -0400400static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +0000401{
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400402 int rings_remaining = adapter->num_active_queues;
403 int ridx = 0, vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000404 int q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +0000405
406 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
407
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400408 for (; ridx < rings_remaining; ridx++) {
409 i40evf_map_vector_to_rxq(adapter, vidx, ridx);
410 i40evf_map_vector_to_txq(adapter, vidx, ridx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000411
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400412 /* In the case where we have more queues than vectors, continue
413 * round-robin on vectors until all queues are mapped.
414 */
415 if (++vidx >= q_vectors)
416 vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000417 }
418
Greg Rose5eae00c2013-12-21 06:12:45 +0000419 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Greg Rose5eae00c2013-12-21 06:12:45 +0000420}
421
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700422#ifdef CONFIG_NET_POLL_CONTROLLER
423/**
424 * i40evf_netpoll - A Polling 'interrupt' handler
425 * @netdev: network interface device structure
426 *
427 * This is used by netconsole to send skbs without having to re-enable
428 * interrupts. It's not called while the normal interrupt routine is executing.
429 **/
430static void i40evf_netpoll(struct net_device *netdev)
431{
432 struct i40evf_adapter *adapter = netdev_priv(netdev);
433 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
434 int i;
435
436 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400437 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700438 return;
439
440 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400441 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700442}
443
444#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000445/**
Alan Brady96db7762016-09-14 16:24:38 -0700446 * i40evf_irq_affinity_notify - Callback for affinity changes
447 * @notify: context as to what irq was changed
448 * @mask: the new affinity mask
449 *
450 * This is a callback function used by the irq_set_affinity_notifier function
451 * so that we may register to receive changes to the irq affinity masks.
452 **/
453static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
454 const cpumask_t *mask)
455{
456 struct i40e_q_vector *q_vector =
457 container_of(notify, struct i40e_q_vector, affinity_notify);
458
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400459 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700460}
461
462/**
463 * i40evf_irq_affinity_release - Callback for affinity notifier release
464 * @ref: internal core kernel usage
465 *
466 * This is a callback function used by the irq_set_affinity_notifier function
467 * to inform the current notification subscriber that they will no longer
468 * receive notifications.
469 **/
470static void i40evf_irq_affinity_release(struct kref *ref) {}
471
472/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000473 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
474 * @adapter: board private structure
475 *
476 * Allocates MSI-X vectors for tx and rx handling, and requests
477 * interrupts from the kernel.
478 **/
479static int
480i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
481{
Jacob Keller696ac80a2017-07-12 05:46:11 -0400482 unsigned int vector, q_vectors;
483 unsigned int rx_int_idx = 0, tx_int_idx = 0;
484 int irq_num, err;
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400485 int cpu;
Greg Rose5eae00c2013-12-21 06:12:45 +0000486
487 i40evf_irq_disable(adapter);
488 /* Decrement for Other and TCP Timer vectors */
489 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
490
491 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400492 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700493 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000494
495 if (q_vector->tx.ring && q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400496 snprintf(q_vector->name, sizeof(q_vector->name),
497 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000498 tx_int_idx++;
499 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400500 snprintf(q_vector->name, sizeof(q_vector->name),
501 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000502 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400503 snprintf(q_vector->name, sizeof(q_vector->name),
504 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000505 } else {
506 /* skip this unused q_vector */
507 continue;
508 }
Alan Brady96db7762016-09-14 16:24:38 -0700509 err = request_irq(irq_num,
510 i40evf_msix_clean_rings,
511 0,
512 q_vector->name,
513 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000514 if (err) {
515 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400516 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000517 goto free_queue_irqs;
518 }
Alan Brady96db7762016-09-14 16:24:38 -0700519 /* register for affinity change notifications */
520 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
521 q_vector->affinity_notify.release =
522 i40evf_irq_affinity_release;
523 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400524 /* Spread the IRQ affinity hints across online CPUs. Note that
525 * get_cpu_mask returns a mask with a permanent lifetime so
526 * it's safe to use as a hint for irq_set_affinity_hint.
Jacob Keller759dc4a2017-07-14 09:10:10 -0400527 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400528 cpu = cpumask_local_spread(q_vector->v_idx, -1);
529 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Greg Rose5eae00c2013-12-21 06:12:45 +0000530 }
531
532 return 0;
533
534free_queue_irqs:
535 while (vector) {
536 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700537 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
538 irq_set_affinity_notifier(irq_num, NULL);
539 irq_set_affinity_hint(irq_num, NULL);
540 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000541 }
542 return err;
543}
544
545/**
546 * i40evf_request_misc_irq - Initialize MSI-X interrupts
547 * @adapter: board private structure
548 *
549 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
550 * vector is only for the admin queue, and stays active even when the netdev
551 * is closed.
552 **/
553static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
554{
555 struct net_device *netdev = adapter->netdev;
556 int err;
557
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700558 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000559 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
560 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000561 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800562 &i40evf_msix_aq, 0,
563 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000564 if (err) {
565 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800566 "request_irq for %s failed: %d\n",
567 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000568 free_irq(adapter->msix_entries[0].vector, netdev);
569 }
570 return err;
571}
572
573/**
574 * i40evf_free_traffic_irqs - Free MSI-X interrupts
575 * @adapter: board private structure
576 *
577 * Frees all MSI-X vectors other than 0.
578 **/
579static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
580{
Alan Brady96db7762016-09-14 16:24:38 -0700581 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000582
Alan Brady47d2a5d2016-11-08 13:05:05 -0800583 if (!adapter->msix_entries)
584 return;
585
Greg Rose5eae00c2013-12-21 06:12:45 +0000586 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
587
Alan Brady96db7762016-09-14 16:24:38 -0700588 for (vector = 0; vector < q_vectors; vector++) {
589 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
590 irq_set_affinity_notifier(irq_num, NULL);
591 irq_set_affinity_hint(irq_num, NULL);
592 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000593 }
594}
595
596/**
597 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
598 * @adapter: board private structure
599 *
600 * Frees MSI-X vector 0.
601 **/
602static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
603{
604 struct net_device *netdev = adapter->netdev;
605
Jacob Kelleref4603e2016-11-08 13:05:08 -0800606 if (!adapter->msix_entries)
607 return;
608
Greg Rose5eae00c2013-12-21 06:12:45 +0000609 free_irq(adapter->msix_entries[0].vector, netdev);
610}
611
612/**
613 * i40evf_configure_tx - Configure Transmit Unit after Reset
614 * @adapter: board private structure
615 *
616 * Configure the Tx unit of the MAC after a reset.
617 **/
618static void i40evf_configure_tx(struct i40evf_adapter *adapter)
619{
620 struct i40e_hw *hw = &adapter->hw;
621 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000622
Mitch Williamscc052922014-10-25 03:24:34 +0000623 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400624 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000625}
626
627/**
628 * i40evf_configure_rx - Configure Receive Unit after Reset
629 * @adapter: board private structure
630 *
631 * Configure the Rx unit of the MAC after a reset.
632 **/
633static void i40evf_configure_rx(struct i40evf_adapter *adapter)
634{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700635 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000636 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000637 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000638
Alexander Duyckdab86af2017-03-14 10:15:27 -0700639 /* Legacy Rx will always default to a 2048 buffer size. */
640#if (PAGE_SIZE < 8192)
641 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200642 struct net_device *netdev = adapter->netdev;
643
Alexander Duyck98efd692017-04-05 07:51:01 -0400644 /* For jumbo frames on systems with 4K pages we have to use
645 * an order 1 page, so we might as well increase the size
646 * of our Rx buffer to make better use of the available space
647 */
648 rx_buf_len = I40E_RXBUFFER_3072;
649
Alexander Duyckdab86af2017-03-14 10:15:27 -0700650 /* We use a 1536 buffer size for configurations with
651 * standard Ethernet mtu. On x86 this gives us enough room
652 * for shared info and 192 bytes of padding.
653 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400654 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
655 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700656 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
657 }
658#endif
659
Mitch Williamscc052922014-10-25 03:24:34 +0000660 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400661 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700662 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400663
664 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
665 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
666 else
667 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000668 }
669}
670
671/**
672 * i40evf_find_vlan - Search filter list for specific vlan filter
673 * @adapter: board private structure
674 * @vlan: vlan tag
675 *
Jacob Keller504398f2017-10-27 11:06:50 -0400676 * Returns ptr to the filter object or NULL. Must be called while holding the
677 * mac_vlan_list_lock.
Greg Rose5eae00c2013-12-21 06:12:45 +0000678 **/
679static struct
680i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
681{
682 struct i40evf_vlan_filter *f;
683
684 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
685 if (vlan == f->vlan)
686 return f;
687 }
688 return NULL;
689}
690
691/**
692 * i40evf_add_vlan - Add a vlan filter to the list
693 * @adapter: board private structure
694 * @vlan: VLAN tag
695 *
696 * Returns ptr to the filter object or NULL when no memory available.
697 **/
698static struct
699i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
700{
Mitch Williams13acb542015-03-31 00:45:05 -0700701 struct i40evf_vlan_filter *f = NULL;
Mitch Williams13acb542015-03-31 00:45:05 -0700702
Jacob Keller504398f2017-10-27 11:06:50 -0400703 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000704
705 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000706 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000707 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000708 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700709 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000710
Greg Rose5eae00c2013-12-21 06:12:45 +0000711 f->vlan = vlan;
712
713 INIT_LIST_HEAD(&f->list);
714 list_add(&f->list, &adapter->vlan_filter_list);
715 f->add = true;
716 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
717 }
718
Mitch Williams13acb542015-03-31 00:45:05 -0700719clearout:
Jacob Keller504398f2017-10-27 11:06:50 -0400720 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000721 return f;
722}
723
724/**
725 * i40evf_del_vlan - Remove a vlan filter from the list
726 * @adapter: board private structure
727 * @vlan: VLAN tag
728 **/
729static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
730{
731 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700732
Jacob Keller504398f2017-10-27 11:06:50 -0400733 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000734
735 f = i40evf_find_vlan(adapter, vlan);
736 if (f) {
737 f->remove = true;
738 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
739 }
Jacob Keller504398f2017-10-27 11:06:50 -0400740
741 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000742}
743
744/**
745 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
746 * @netdev: network device struct
747 * @vid: VLAN tag
748 **/
749static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000750 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000751{
752 struct i40evf_adapter *adapter = netdev_priv(netdev);
753
Mitch Williams8ed995f2015-08-28 17:55:57 -0400754 if (!VLAN_ALLOWED(adapter))
755 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000756 if (i40evf_add_vlan(adapter, vid) == NULL)
757 return -ENOMEM;
758 return 0;
759}
760
761/**
762 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
763 * @netdev: network device struct
764 * @vid: VLAN tag
765 **/
766static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000767 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000768{
769 struct i40evf_adapter *adapter = netdev_priv(netdev);
770
Mitch Williams8ed995f2015-08-28 17:55:57 -0400771 if (VLAN_ALLOWED(adapter)) {
772 i40evf_del_vlan(adapter, vid);
773 return 0;
774 }
775 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000776}
777
778/**
779 * i40evf_find_filter - Search filter list for specific mac filter
780 * @adapter: board private structure
781 * @macaddr: the MAC address
782 *
Jacob Keller504398f2017-10-27 11:06:50 -0400783 * Returns ptr to the filter object or NULL. Must be called while holding the
784 * mac_vlan_list_lock.
Greg Rose5eae00c2013-12-21 06:12:45 +0000785 **/
786static struct
787i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
Jacob Keller8946b562018-01-22 12:00:38 -0500788 const u8 *macaddr)
Greg Rose5eae00c2013-12-21 06:12:45 +0000789{
790 struct i40evf_mac_filter *f;
791
792 if (!macaddr)
793 return NULL;
794
795 list_for_each_entry(f, &adapter->mac_filter_list, list) {
796 if (ether_addr_equal(macaddr, f->macaddr))
797 return f;
798 }
799 return NULL;
800}
801
802/**
803 * i40e_add_filter - Add a mac filter to the filter list
804 * @adapter: board private structure
805 * @macaddr: the MAC address
806 *
807 * Returns ptr to the filter object or NULL when no memory available.
808 **/
809static struct
810i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
Jacob Keller8946b562018-01-22 12:00:38 -0500811 const u8 *macaddr)
Greg Rose5eae00c2013-12-21 06:12:45 +0000812{
813 struct i40evf_mac_filter *f;
814
815 if (!macaddr)
816 return NULL;
817
Jacob Keller504398f2017-10-27 11:06:50 -0400818 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000819
820 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000821 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000822 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Jacob Keller504398f2017-10-27 11:06:50 -0400823 if (!f)
824 goto clearout;
Greg Rose5eae00c2013-12-21 06:12:45 +0000825
Greg Rose9a173902014-05-22 06:32:02 +0000826 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000827
Mitch Williams63590b62016-05-16 10:26:42 -0700828 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000829 f->add = true;
830 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Alan Bradyc766b9a2017-09-07 08:05:47 -0400831 } else {
832 f->remove = false;
Greg Rose5eae00c2013-12-21 06:12:45 +0000833 }
834
Jacob Keller504398f2017-10-27 11:06:50 -0400835clearout:
836 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000837 return f;
838}
839
840/**
841 * i40evf_set_mac - NDO callback to set port mac address
842 * @netdev: network interface device structure
843 * @p: pointer to an address structure
844 *
845 * Returns 0 on success, negative on failure
846 **/
847static int i40evf_set_mac(struct net_device *netdev, void *p)
848{
849 struct i40evf_adapter *adapter = netdev_priv(netdev);
850 struct i40e_hw *hw = &adapter->hw;
851 struct i40evf_mac_filter *f;
852 struct sockaddr *addr = p;
853
854 if (!is_valid_ether_addr(addr->sa_data))
855 return -EADDRNOTAVAIL;
856
857 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
858 return 0;
859
Mitch Williams14e52ee2015-08-31 19:54:44 -0400860 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
861 return -EPERM;
862
Jacob Keller504398f2017-10-27 11:06:50 -0400863 spin_lock_bh(&adapter->mac_vlan_list_lock);
864
Mitch Williams14e52ee2015-08-31 19:54:44 -0400865 f = i40evf_find_filter(adapter, hw->mac.addr);
866 if (f) {
867 f->remove = true;
868 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
869 }
870
Jacob Keller504398f2017-10-27 11:06:50 -0400871 spin_unlock_bh(&adapter->mac_vlan_list_lock);
872
Greg Rose5eae00c2013-12-21 06:12:45 +0000873 f = i40evf_add_filter(adapter, addr->sa_data);
874 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000875 ether_addr_copy(hw->mac.addr, addr->sa_data);
876 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000877 }
878
879 return (f == NULL) ? -ENOMEM : 0;
880}
881
882/**
Jacob Keller8946b562018-01-22 12:00:38 -0500883 * i40evf_addr_sync - Callback for dev_(mc|uc)_sync to add address
884 * @netdev: the netdevice
885 * @addr: address to add
886 *
887 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
888 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
889 */
890static int i40evf_addr_sync(struct net_device *netdev, const u8 *addr)
891{
892 struct i40evf_adapter *adapter = netdev_priv(netdev);
893
894 if (i40evf_add_filter(adapter, addr))
895 return 0;
896 else
897 return -ENOMEM;
898}
899
900/**
901 * i40evf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
902 * @netdev: the netdevice
903 * @addr: address to add
904 *
905 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
906 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
907 */
908static int i40evf_addr_unsync(struct net_device *netdev, const u8 *addr)
909{
910 struct i40evf_adapter *adapter = netdev_priv(netdev);
911 struct i40evf_mac_filter *f;
912
913 /* Under some circumstances, we might receive a request to delete
914 * our own device address from our uc list. Because we store the
915 * device address in the VSI's MAC/VLAN filter list, we need to ignore
916 * such requests and not delete our device address from this list.
917 */
918 if (ether_addr_equal(addr, netdev->dev_addr))
919 return 0;
920
921 f = i40evf_find_filter(adapter, addr);
922 if (f) {
923 f->remove = true;
924 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
925 }
926 return 0;
927}
928
929/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000930 * i40evf_set_rx_mode - NDO callback to set the netdev filters
931 * @netdev: network interface device structure
932 **/
933static void i40evf_set_rx_mode(struct net_device *netdev)
934{
935 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000936
Jacob Keller504398f2017-10-27 11:06:50 -0400937 spin_lock_bh(&adapter->mac_vlan_list_lock);
Jacob Keller8946b562018-01-22 12:00:38 -0500938 __dev_uc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
939 __dev_mc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
940 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700941
942 if (netdev->flags & IFF_PROMISC &&
943 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
944 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
945 else if (!(netdev->flags & IFF_PROMISC) &&
946 adapter->flags & I40EVF_FLAG_PROMISC_ON)
947 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
948
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700949 if (netdev->flags & IFF_ALLMULTI &&
950 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
951 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
952 else if (!(netdev->flags & IFF_ALLMULTI) &&
953 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
954 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
Greg Rose5eae00c2013-12-21 06:12:45 +0000955}
956
957/**
958 * i40evf_napi_enable_all - enable NAPI on all queue vectors
959 * @adapter: board private structure
960 **/
961static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
962{
963 int q_idx;
964 struct i40e_q_vector *q_vector;
965 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
966
967 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
968 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000969
Mitch Williams7d96ba12015-10-26 19:44:39 -0400970 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000971 napi = &q_vector->napi;
972 napi_enable(napi);
973 }
974}
975
976/**
977 * i40evf_napi_disable_all - disable NAPI on all queue vectors
978 * @adapter: board private structure
979 **/
980static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
981{
982 int q_idx;
983 struct i40e_q_vector *q_vector;
984 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
985
986 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400987 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000988 napi_disable(&q_vector->napi);
989 }
990}
991
992/**
993 * i40evf_configure - set up transmit and receive data structures
994 * @adapter: board private structure
995 **/
996static void i40evf_configure(struct i40evf_adapter *adapter)
997{
998 struct net_device *netdev = adapter->netdev;
999 int i;
1000
1001 i40evf_set_rx_mode(netdev);
1002
1003 i40evf_configure_tx(adapter);
1004 i40evf_configure_rx(adapter);
1005 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1006
Mitch Williamscc052922014-10-25 03:24:34 +00001007 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001008 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001009
Mitch Williamsb1630982016-04-18 11:33:48 -07001010 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001011 }
1012}
1013
1014/**
1015 * i40evf_up_complete - Finish the last steps of bringing up a connection
1016 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001017 *
1018 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001019 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001020static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001021{
1022 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001023 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001024
1025 i40evf_napi_enable_all(adapter);
1026
1027 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001028 if (CLIENT_ENABLED(adapter))
1029 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001030 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001031}
1032
1033/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001034 * i40e_down - Shutdown the connection processing
1035 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001036 *
1037 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001038 **/
1039void i40evf_down(struct i40evf_adapter *adapter)
1040{
1041 struct net_device *netdev = adapter->netdev;
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05001042 struct i40evf_vlan_filter *vlf;
Greg Rose5eae00c2013-12-21 06:12:45 +00001043 struct i40evf_mac_filter *f;
1044
Mitch Williams209dc4d2015-12-09 15:50:27 -08001045 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001046 return;
1047
Mitch Williams63e18c22015-03-27 00:12:10 -07001048 netif_carrier_off(netdev);
1049 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001050 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001051 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001052 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001053
Jacob Keller504398f2017-10-27 11:06:50 -04001054 spin_lock_bh(&adapter->mac_vlan_list_lock);
1055
Jacob Keller8946b562018-01-22 12:00:38 -05001056 /* clear the sync flag on all filters */
1057 __dev_uc_unsync(adapter->netdev, NULL);
1058 __dev_mc_unsync(adapter->netdev, NULL);
1059
Mitch Williamsef8693e2014-02-13 03:48:53 -08001060 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001061 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1062 f->remove = true;
1063 }
Jacob Keller8946b562018-01-22 12:00:38 -05001064
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001065 /* remove all VLAN filters */
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05001066 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001067 f->remove = true;
1068 }
Jacob Keller504398f2017-10-27 11:06:50 -04001069
1070 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1071
Mitch Williamsef8693e2014-02-13 03:48:53 -08001072 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1073 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001074 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001075 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001076 /* Schedule operations to close down the HW. Don't wait
1077 * here for this to complete. The watchdog is still running
1078 * and it will take care of this.
1079 */
1080 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001081 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001082 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001083 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001084
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001085 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001086}
1087
1088/**
1089 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1090 * @adapter: board private structure
1091 * @vectors: number of vectors to request
1092 *
1093 * Work with the OS to set up the MSIX vectors needed.
1094 *
1095 * Returns 0 on success, negative on failure
1096 **/
1097static int
1098i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1099{
1100 int err, vector_threshold;
1101
1102 /* We'll want at least 3 (vector_threshold):
1103 * 0) Other (Admin Queue and link, mostly)
1104 * 1) TxQ[0] Cleanup
1105 * 2) RxQ[0] Cleanup
1106 */
1107 vector_threshold = MIN_MSIX_COUNT;
1108
1109 /* The more we get, the more we will assign to Tx/Rx Cleanup
1110 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1111 * Right now, we simply care about how many we'll get; we'll
1112 * set them up later while requesting irq's.
1113 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001114 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1115 vector_threshold, vectors);
1116 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001117 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001118 kfree(adapter->msix_entries);
1119 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001120 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001121 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001122
1123 /* Adjust for only the vectors we'll use, which is minimum
1124 * of max_msix_q_vectors + NONQ_VECS, or the number of
1125 * vectors we were allocated.
1126 */
1127 adapter->num_msix_vectors = err;
1128 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001129}
1130
1131/**
1132 * i40evf_free_queues - Free memory for all rings
1133 * @adapter: board private structure to initialize
1134 *
1135 * Free all of the memory associated with queue pairs.
1136 **/
1137static void i40evf_free_queues(struct i40evf_adapter *adapter)
1138{
Greg Rose5eae00c2013-12-21 06:12:45 +00001139 if (!adapter->vsi_res)
1140 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001141 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001142 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001143 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001144 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001145 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001146}
1147
1148/**
1149 * i40evf_alloc_queues - Allocate memory for all rings
1150 * @adapter: board private structure to initialize
1151 *
1152 * We allocate one ring per queue at run-time since we don't know the
1153 * number of queues at compile-time. The polling_netdev array is
1154 * intended for Multiqueue, but should work fine with a single queue.
1155 **/
1156static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1157{
Jacob Keller65c70062017-06-07 05:43:01 -04001158 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001159
Alan Brady5b36e8d2017-08-22 06:57:50 -04001160 /* If we're in reset reallocating queues we don't actually know yet for
1161 * certain the PF gave us the number of queues we asked for but we'll
1162 * assume it did. Once basic reset is finished we'll confirm once we
1163 * start negotiating config with PF.
1164 */
1165 if (adapter->num_req_queues)
1166 num_active_queues = adapter->num_req_queues;
1167 else
1168 num_active_queues = min_t(int,
1169 adapter->vsi_res->num_queue_pairs,
1170 (int)(num_online_cpus()));
1171
Jacob Keller65c70062017-06-07 05:43:01 -04001172
1173 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001174 sizeof(struct i40e_ring), GFP_KERNEL);
1175 if (!adapter->tx_rings)
1176 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001177 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001178 sizeof(struct i40e_ring), GFP_KERNEL);
1179 if (!adapter->rx_rings)
1180 goto err_out;
1181
Jacob Keller65c70062017-06-07 05:43:01 -04001182 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001183 struct i40e_ring *tx_ring;
1184 struct i40e_ring *rx_ring;
1185
Mitch Williams0dd438d2015-10-26 19:44:40 -04001186 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001187
1188 tx_ring->queue_index = i;
1189 tx_ring->netdev = adapter->netdev;
1190 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001191 tx_ring->count = adapter->tx_desc_count;
Alexander Duyck40588ca2017-12-29 08:49:28 -05001192 tx_ring->itr_setting = I40E_ITR_TX_DEF;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001193 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001194 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001195
Mitch Williams0dd438d2015-10-26 19:44:40 -04001196 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001197 rx_ring->queue_index = i;
1198 rx_ring->netdev = adapter->netdev;
1199 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001200 rx_ring->count = adapter->rx_desc_count;
Alexander Duyck40588ca2017-12-29 08:49:28 -05001201 rx_ring->itr_setting = I40E_ITR_RX_DEF;
Greg Rose5eae00c2013-12-21 06:12:45 +00001202 }
1203
Jacob Keller65c70062017-06-07 05:43:01 -04001204 adapter->num_active_queues = num_active_queues;
1205
Greg Rose5eae00c2013-12-21 06:12:45 +00001206 return 0;
1207
1208err_out:
1209 i40evf_free_queues(adapter);
1210 return -ENOMEM;
1211}
1212
1213/**
1214 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1215 * @adapter: board private structure to initialize
1216 *
1217 * Attempt to configure the interrupts using the best available
1218 * capabilities of the hardware and the kernel.
1219 **/
1220static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1221{
1222 int vector, v_budget;
1223 int pairs = 0;
1224 int err = 0;
1225
1226 if (!adapter->vsi_res) {
1227 err = -EIO;
1228 goto out;
1229 }
Mitch Williamscc052922014-10-25 03:24:34 +00001230 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001231
Jacob Keller789f38c2017-04-19 09:25:56 -04001232 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1233 * us much good if we have more vectors than CPUs. However, we already
1234 * limit the total number of queues by the number of CPUs so we do not
1235 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001236 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001237 v_budget = min_t(int, pairs + NONQ_VECS,
1238 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001239
Greg Rose5eae00c2013-12-21 06:12:45 +00001240 adapter->msix_entries = kcalloc(v_budget,
1241 sizeof(struct msix_entry), GFP_KERNEL);
1242 if (!adapter->msix_entries) {
1243 err = -ENOMEM;
1244 goto out;
1245 }
1246
1247 for (vector = 0; vector < v_budget; vector++)
1248 adapter->msix_entries[vector].entry = vector;
1249
Mitch Williams313ed2d2015-08-27 11:42:31 -04001250 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001251
1252out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001253 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1254 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001255 return err;
1256}
1257
1258/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001259 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1260 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001261 *
1262 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001263 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001264static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001265{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001266 struct i40e_aqc_get_set_rss_key_data *rss_key =
1267 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001268 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001269 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001270
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001271 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001272 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001273 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001274 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001275 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001276 }
1277
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001278 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1279 if (ret) {
1280 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1281 i40evf_stat_str(hw, ret),
1282 i40evf_aq_str(hw, hw->aq.asq_last_status));
1283 return ret;
1284
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001285 }
1286
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001287 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1288 adapter->rss_lut, adapter->rss_lut_size);
1289 if (ret) {
1290 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1291 i40evf_stat_str(hw, ret),
1292 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001293 }
1294
1295 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001296
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001297}
1298
1299/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001300 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001301 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001302 *
1303 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001304 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001305static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001306{
1307 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001308 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001309 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001310
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001311 dw = (u32 *)adapter->rss_key;
1312 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1313 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001314
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001315 dw = (u32 *)adapter->rss_lut;
1316 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1317 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001318
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001320
1321 return 0;
1322}
1323
1324/**
1325 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001326 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001327 *
1328 * Returns 0 on success, negative on failure
1329 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001330int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001331{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001332
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001333 if (RSS_PF(adapter)) {
1334 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1335 I40EVF_FLAG_AQ_SET_RSS_KEY;
1336 return 0;
1337 } else if (RSS_AQ(adapter)) {
1338 return i40evf_config_rss_aq(adapter);
1339 } else {
1340 return i40evf_config_rss_reg(adapter);
1341 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001342}
1343
1344/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001345 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001346 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001347 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001348static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001349{
1350 u16 i;
1351
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001352 for (i = 0; i < adapter->rss_lut_size; i++)
1353 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001354}
1355
1356/**
Helin Zhang96a81982015-10-26 19:44:31 -04001357 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001358 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001359 *
1360 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001361 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001362static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001363{
1364 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001365 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001366
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001367 if (!RSS_PF(adapter)) {
1368 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001369 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001370 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001371 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1372 else
1373 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001374
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001375 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1376 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1377 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001378
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001379 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001380
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001381 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1382 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001383
1384 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001385}
1386
1387/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001388 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1389 * @adapter: board private structure to initialize
1390 *
1391 * We allocate one q_vector per queue interrupt. If allocation fails we
1392 * return -ENOMEM.
1393 **/
1394static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1395{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001396 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001397 struct i40e_q_vector *q_vector;
1398
1399 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001400 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001401 GFP_KERNEL);
1402 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001403 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001404
1405 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001406 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001407 q_vector->adapter = adapter;
1408 q_vector->vsi = &adapter->vsi;
1409 q_vector->v_idx = q_idx;
Alexander Duycka3f9fb52017-12-29 08:48:53 -05001410 q_vector->reg_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001411 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001412 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001413 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001414 }
1415
1416 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001417}
1418
1419/**
1420 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1421 * @adapter: board private structure to initialize
1422 *
1423 * This function frees the memory allocated to the q_vectors. In addition if
1424 * NAPI is enabled it will delete any references to the NAPI struct prior
1425 * to freeing the q_vector.
1426 **/
1427static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1428{
1429 int q_idx, num_q_vectors;
1430 int napi_vectors;
1431
Jacob Kelleref4603e2016-11-08 13:05:08 -08001432 if (!adapter->q_vectors)
1433 return;
1434
Greg Rose5eae00c2013-12-21 06:12:45 +00001435 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001436 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001437
1438 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001439 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001440 if (q_idx < napi_vectors)
1441 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001442 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001443 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001444 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001445}
1446
1447/**
1448 * i40evf_reset_interrupt_capability - Reset MSIX setup
1449 * @adapter: board private structure
1450 *
1451 **/
1452void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1453{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001454 if (!adapter->msix_entries)
1455 return;
1456
Greg Rose5eae00c2013-12-21 06:12:45 +00001457 pci_disable_msix(adapter->pdev);
1458 kfree(adapter->msix_entries);
1459 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001460}
1461
1462/**
1463 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1464 * @adapter: board private structure to initialize
1465 *
1466 **/
1467int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1468{
1469 int err;
1470
Jacob Keller283aeaf2017-04-19 09:25:59 -04001471 err = i40evf_alloc_queues(adapter);
1472 if (err) {
1473 dev_err(&adapter->pdev->dev,
1474 "Unable to allocate memory for queues\n");
1475 goto err_alloc_queues;
1476 }
1477
Jacob Keller62fe2a82016-07-27 12:02:33 -07001478 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001479 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001480 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001481 if (err) {
1482 dev_err(&adapter->pdev->dev,
1483 "Unable to setup interrupt capabilities\n");
1484 goto err_set_interrupt;
1485 }
1486
1487 err = i40evf_alloc_q_vectors(adapter);
1488 if (err) {
1489 dev_err(&adapter->pdev->dev,
1490 "Unable to allocate memory for queue vectors\n");
1491 goto err_alloc_q_vectors;
1492 }
1493
Greg Rose5eae00c2013-12-21 06:12:45 +00001494 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001495 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1496 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001497
1498 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001499err_alloc_q_vectors:
1500 i40evf_reset_interrupt_capability(adapter);
1501err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001502 i40evf_free_queues(adapter);
1503err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001504 return err;
1505}
1506
1507/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001508 * i40evf_free_rss - Free memory used by RSS structs
1509 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001510 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001511static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001512{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001513 kfree(adapter->rss_key);
1514 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001515
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001516 kfree(adapter->rss_lut);
1517 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001518}
1519
1520/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001521 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1522 * @adapter: board private structure
1523 *
1524 * Returns 0 on success, negative on failure
1525 **/
1526static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1527{
1528 struct net_device *netdev = adapter->netdev;
1529 int err;
1530
1531 if (netif_running(netdev))
1532 i40evf_free_traffic_irqs(adapter);
1533 i40evf_free_misc_irq(adapter);
1534 i40evf_reset_interrupt_capability(adapter);
1535 i40evf_free_q_vectors(adapter);
1536 i40evf_free_queues(adapter);
1537
1538 err = i40evf_init_interrupt_scheme(adapter);
1539 if (err)
1540 goto err;
1541
1542 netif_tx_stop_all_queues(netdev);
1543
1544 err = i40evf_request_misc_irq(adapter);
1545 if (err)
1546 goto err;
1547
1548 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1549
Mitch Williams1b7b7592017-08-22 06:57:51 -04001550 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001551
1552 if (RSS_AQ(adapter))
1553 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1554 else
1555 err = i40evf_init_rss(adapter);
1556err:
1557 return err;
1558}
1559
1560/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001561 * i40evf_watchdog_timer - Periodic call-back timer
1562 * @data: pointer to adapter disguised as unsigned long
1563 **/
Kees Cook26566ea2017-10-16 17:29:35 -07001564static void i40evf_watchdog_timer(struct timer_list *t)
Greg Rose5eae00c2013-12-21 06:12:45 +00001565{
Kees Cook26566ea2017-10-16 17:29:35 -07001566 struct i40evf_adapter *adapter = from_timer(adapter, t,
1567 watchdog_timer);
Mitch Williams75a64432014-11-11 20:02:42 +00001568
Greg Rose5eae00c2013-12-21 06:12:45 +00001569 schedule_work(&adapter->watchdog_task);
1570 /* timer will be rescheduled in watchdog task */
1571}
1572
1573/**
1574 * i40evf_watchdog_task - Periodic call-back task
1575 * @work: pointer to work_struct
1576 **/
1577static void i40evf_watchdog_task(struct work_struct *work)
1578{
1579 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001580 struct i40evf_adapter,
1581 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001582 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001583 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001584
Greg Rose5eae00c2013-12-21 06:12:45 +00001585 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001586 goto restart_watchdog;
1587
1588 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001589 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1590 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001591 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1592 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001593 /* A chance for redemption! */
1594 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1595 adapter->state = __I40EVF_STARTUP;
1596 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1597 schedule_delayed_work(&adapter->init_task, 10);
1598 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1599 &adapter->crit_section);
1600 /* Don't reschedule the watchdog, since we've restarted
1601 * the init task. When init_task contacts the PF and
1602 * gets everything set up again, it'll restart the
1603 * watchdog for us. Down, boy. Sit. Stay. Woof.
1604 */
1605 return;
1606 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001607 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001608 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001609 goto watchdog_done;
1610 }
1611
1612 if ((adapter->state < __I40EVF_DOWN) ||
1613 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001614 goto watchdog_done;
1615
Mitch Williamsef8693e2014-02-13 03:48:53 -08001616 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001617 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1618 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001619 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001620 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001621 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001622 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001623 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001624 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001625 goto watchdog_done;
1626 }
1627
1628 /* Process admin queue tasks. After init, everything gets done
1629 * here so we don't race on the admin queue.
1630 */
Mitch Williamsed636962015-04-07 19:45:32 -04001631 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001632 if (!i40evf_asq_done(hw)) {
1633 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1634 i40evf_send_api_ver(adapter);
1635 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001636 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001637 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001638 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1639 i40evf_send_vf_config_msg(adapter);
1640 goto watchdog_done;
1641 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001642
Mitch Williamse284fc82015-03-27 00:12:09 -07001643 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1644 i40evf_disable_queues(adapter);
1645 goto watchdog_done;
1646 }
1647
Greg Rose5eae00c2013-12-21 06:12:45 +00001648 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1649 i40evf_map_queues(adapter);
1650 goto watchdog_done;
1651 }
1652
1653 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1654 i40evf_add_ether_addrs(adapter);
1655 goto watchdog_done;
1656 }
1657
1658 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1659 i40evf_add_vlans(adapter);
1660 goto watchdog_done;
1661 }
1662
1663 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1664 i40evf_del_ether_addrs(adapter);
1665 goto watchdog_done;
1666 }
1667
1668 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1669 i40evf_del_vlans(adapter);
1670 goto watchdog_done;
1671 }
1672
Mariusz Stachura87743702017-07-17 22:09:45 -07001673 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1674 i40evf_enable_vlan_stripping(adapter);
1675 goto watchdog_done;
1676 }
1677
1678 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1679 i40evf_disable_vlan_stripping(adapter);
1680 goto watchdog_done;
1681 }
1682
Greg Rose5eae00c2013-12-21 06:12:45 +00001683 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1684 i40evf_configure_queues(adapter);
1685 goto watchdog_done;
1686 }
1687
1688 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1689 i40evf_enable_queues(adapter);
1690 goto watchdog_done;
1691 }
1692
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001693 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1694 /* This message goes straight to the firmware, not the
1695 * PF, so we don't have to set current_op as we will
1696 * not get a response through the ARQ.
1697 */
Helin Zhang96a81982015-10-26 19:44:31 -04001698 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001699 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1700 goto watchdog_done;
1701 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001702 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1703 i40evf_get_hena(adapter);
1704 goto watchdog_done;
1705 }
1706 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1707 i40evf_set_hena(adapter);
1708 goto watchdog_done;
1709 }
1710 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1711 i40evf_set_rss_key(adapter);
1712 goto watchdog_done;
1713 }
1714 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1715 i40evf_set_rss_lut(adapter);
1716 goto watchdog_done;
1717 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001718
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001719 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001720 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1721 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001722 goto watchdog_done;
1723 }
1724
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001725 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001726 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001727 goto watchdog_done;
1728 }
1729
1730 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1731 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001732 i40evf_set_promiscuous(adapter, 0);
1733 goto watchdog_done;
1734 }
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08001735
1736 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_CHANNELS) {
1737 i40evf_enable_channels(adapter);
1738 goto watchdog_done;
1739 }
1740
1741 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_CHANNELS) {
1742 i40evf_disable_channels(adapter);
1743 goto watchdog_done;
1744 }
1745
Mitch Williamsed0e8942017-01-24 10:23:59 -08001746 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001747
Greg Rose5eae00c2013-12-21 06:12:45 +00001748 if (adapter->state == __I40EVF_RUNNING)
1749 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001750watchdog_done:
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05001751 if (adapter->state == __I40EVF_RUNNING)
1752 i40evf_detect_recover_hung(&adapter->vsi);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001753 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1754restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001755 if (adapter->state == __I40EVF_REMOVE)
1756 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001757 if (adapter->aq_required)
1758 mod_timer(&adapter->watchdog_timer,
1759 jiffies + msecs_to_jiffies(20));
1760 else
1761 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001762 schedule_work(&adapter->adminq_task);
1763}
1764
Joe Perchesdedecb62016-11-01 15:35:14 -07001765static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1766{
1767 struct i40evf_mac_filter *f, *ftmp;
1768 struct i40evf_vlan_filter *fv, *fvtmp;
1769
1770 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1771
Jacob Keller44b034b2017-10-27 11:06:49 -04001772 /* We don't use netif_running() because it may be true prior to
1773 * ndo_open() returning, so we can't assume it means all our open
1774 * tasks have finished, since we're not holding the rtnl_lock here.
1775 */
1776 if (adapter->state == __I40EVF_RUNNING) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001777 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001778 netif_carrier_off(adapter->netdev);
1779 netif_tx_disable(adapter->netdev);
1780 adapter->link_up = false;
1781 i40evf_napi_disable_all(adapter);
1782 i40evf_irq_disable(adapter);
1783 i40evf_free_traffic_irqs(adapter);
1784 i40evf_free_all_tx_resources(adapter);
1785 i40evf_free_all_rx_resources(adapter);
1786 }
1787
Jacob Keller504398f2017-10-27 11:06:50 -04001788 spin_lock_bh(&adapter->mac_vlan_list_lock);
1789
Joe Perchesdedecb62016-11-01 15:35:14 -07001790 /* Delete all of the filters, both MAC and VLAN. */
1791 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1792 list_del(&f->list);
1793 kfree(f);
1794 }
1795
1796 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1797 list_del(&fv->list);
1798 kfree(fv);
1799 }
1800
Jacob Keller504398f2017-10-27 11:06:50 -04001801 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1802
Joe Perchesdedecb62016-11-01 15:35:14 -07001803 i40evf_free_misc_irq(adapter);
1804 i40evf_reset_interrupt_capability(adapter);
1805 i40evf_free_queues(adapter);
1806 i40evf_free_q_vectors(adapter);
1807 kfree(adapter->vf_res);
1808 i40evf_shutdown_adminq(&adapter->hw);
1809 adapter->netdev->flags &= ~IFF_UP;
1810 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1811 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1812 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001813 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001814 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1815}
1816
Mitch Williams67c818a2015-06-19 08:56:30 -07001817#define I40EVF_RESET_WAIT_MS 10
1818#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001819/**
1820 * i40evf_reset_task - Call-back task to handle hardware reset
1821 * @work: pointer to work_struct
1822 *
1823 * During reset we need to shut down and reinitialize the admin queue
1824 * before we can use it to communicate with the PF again. We also clear
1825 * and reinit the rings because that context is lost as well.
1826 **/
1827static void i40evf_reset_task(struct work_struct *work)
1828{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001829 struct i40evf_adapter *adapter = container_of(work,
1830 struct i40evf_adapter,
1831 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001832 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001833 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001834 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001835 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001836 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001837 int i = 0, err;
Jacob Keller44b034b2017-10-27 11:06:49 -04001838 bool running;
Greg Rose5eae00c2013-12-21 06:12:45 +00001839
Avinash Dayanand06aa0402017-12-18 05:16:43 -05001840 /* When device is being removed it doesn't make sense to run the reset
1841 * task, just return in such a case.
1842 */
1843 if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1844 return;
1845
Mitch Williamsed0e8942017-01-24 10:23:59 -08001846 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001847 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001848 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001849 if (CLIENT_ENABLED(adapter)) {
1850 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1851 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1852 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1853 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1854 cancel_delayed_work_sync(&adapter->client_task);
1855 i40evf_notify_client_close(&adapter->vsi, true);
1856 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001857 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001858 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001859 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1860 /* Restart the AQ here. If we have been reset but didn't
1861 * detect it, or if the PF had to reinit, our AQ will be hosed.
1862 */
1863 i40evf_shutdown_adminq(hw);
1864 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001865 i40evf_request_reset(adapter);
1866 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001867 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001868
Mitch Williamsef8693e2014-02-13 03:48:53 -08001869 /* poll until we see the reset actually happen */
1870 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001871 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1872 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1873 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001874 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001875 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001876 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001877 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001878 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001879 goto continue_reset; /* act like the reset happened */
1880 }
1881
1882 /* wait until the reset is complete and the PF is responding to us */
1883 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001884 /* sleep first to make sure a minimum wait time is met */
1885 msleep(I40EVF_RESET_WAIT_MS);
1886
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001887 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1888 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001889 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001890 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001891 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001892
Mitch Williams509a4472015-12-23 12:05:52 -08001893 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001894
Mitch Williamsef8693e2014-02-13 03:48:53 -08001895 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001896 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001897 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001898 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001899 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001900 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001901 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001902
1903continue_reset:
Jacob Keller44b034b2017-10-27 11:06:49 -04001904 /* We don't use netif_running() because it may be true prior to
1905 * ndo_open() returning, so we can't assume it means all our open
1906 * tasks have finished, since we're not holding the rtnl_lock here.
1907 */
1908 running = (adapter->state == __I40EVF_RUNNING);
1909
1910 if (running) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001911 netif_carrier_off(netdev);
1912 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001913 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001914 i40evf_napi_disable_all(adapter);
1915 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001916 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001917
Greg Rose5eae00c2013-12-21 06:12:45 +00001918 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001919 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1920
1921 /* free the Tx/Rx rings and descriptors, might be better to just
1922 * re-use them sometime in the future
1923 */
1924 i40evf_free_all_rx_resources(adapter);
1925 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001926
Avinash Dayanand836ce5e2018-01-23 08:50:55 -08001927 adapter->flags |= I40EVF_FLAG_QUEUES_DISABLED;
Greg Rose5eae00c2013-12-21 06:12:45 +00001928 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001929 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001930 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001931 err = i40evf_init_adminq(hw);
1932 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001933 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1934 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001935 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001936
Alan Brady5b36e8d2017-08-22 06:57:50 -04001937 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1938 err = i40evf_reinit_interrupt_scheme(adapter);
1939 if (err)
1940 goto reset_err;
1941 }
1942
1943 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001944 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001945
Jacob Keller504398f2017-10-27 11:06:50 -04001946 spin_lock_bh(&adapter->mac_vlan_list_lock);
1947
Mitch Williamsac833bb2015-01-29 07:17:19 +00001948 /* re-add all MAC filters */
1949 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1950 f->add = true;
1951 }
1952 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001953 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1954 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001955 }
Jacob Keller504398f2017-10-27 11:06:50 -04001956
1957 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1958
Mitch Williamse6d038d2015-06-04 16:23:58 -04001959 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001960 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Mitch Williams67c818a2015-06-19 08:56:30 -07001961 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001962
1963 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1964
Jacob Keller44b034b2017-10-27 11:06:49 -04001965 /* We were running when the reset started, so we need to restore some
1966 * state here.
1967 */
1968 if (running) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001969 /* allocate transmit descriptors */
1970 err = i40evf_setup_all_tx_resources(adapter);
1971 if (err)
1972 goto reset_err;
1973
1974 /* allocate receive descriptors */
1975 err = i40evf_setup_all_rx_resources(adapter);
1976 if (err)
1977 goto reset_err;
1978
Alan Brady5b36e8d2017-08-22 06:57:50 -04001979 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1980 err = i40evf_request_traffic_irqs(adapter,
1981 netdev->name);
1982 if (err)
1983 goto reset_err;
1984
1985 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1986 }
1987
Greg Rose5eae00c2013-12-21 06:12:45 +00001988 i40evf_configure(adapter);
1989
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001990 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001991
1992 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001993 } else {
1994 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001995 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001996 }
Jacob Keller9b2aef12017-10-27 11:06:52 -04001997 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1998 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001999
Greg Rose5eae00c2013-12-21 06:12:45 +00002000 return;
2001reset_err:
Jacob Keller9b2aef12017-10-27 11:06:52 -04002002 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2003 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams80e72892014-05-10 04:49:06 +00002004 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04002005 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002006}
2007
2008/**
2009 * i40evf_adminq_task - worker thread to clean the admin queue
2010 * @work: pointer to work_struct containing our data
2011 **/
2012static void i40evf_adminq_task(struct work_struct *work)
2013{
2014 struct i40evf_adapter *adapter =
2015 container_of(work, struct i40evf_adapter, adminq_task);
2016 struct i40e_hw *hw = &adapter->hw;
2017 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07002018 enum virtchnl_ops v_op;
2019 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00002020 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00002021 u16 pending;
2022
Mitch Williamsef8693e2014-02-13 03:48:53 -08002023 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00002024 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08002025
Mitch Williams1001dc32014-11-11 20:02:19 +00002026 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2027 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00002028 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00002029 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00002030
Greg Rose5eae00c2013-12-21 06:12:45 +00002031 do {
2032 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002033 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2034 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2035
2036 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002037 break; /* No event to process or error cleaning ARQ */
2038
Tushar Davec969ef42017-06-22 09:44:32 -07002039 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002040 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002041 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002042 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002043 } while (pending);
2044
Mitch Williams67c818a2015-06-19 08:56:30 -07002045 if ((adapter->flags &
2046 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2047 adapter->state == __I40EVF_RESETTING)
2048 goto freedom;
2049
Mitch Williams912257e2014-05-22 06:32:07 +00002050 /* check for error indications */
2051 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002052 if (val == 0xdeadbeef) /* indicates device in reset */
2053 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002054 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002055 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002056 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002057 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002058 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002059 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002060 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002061 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002062 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002063 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002064 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002065 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002066 }
2067 if (oldval != val)
2068 wr32(hw, hw->aq.arq.len, val);
2069
2070 val = rd32(hw, hw->aq.asq.len);
2071 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002072 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002073 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002074 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002075 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002076 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002077 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002078 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002079 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002080 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002081 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002082 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002083 }
2084 if (oldval != val)
2085 wr32(hw, hw->aq.asq.len, val);
2086
Mitch Williams67c818a2015-06-19 08:56:30 -07002087freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002088 kfree(event.msg_buf);
2089out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002090 /* re-enable Admin queue interrupt cause */
2091 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002092}
2093
2094/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002095 * i40evf_client_task - worker thread to perform client work
2096 * @work: pointer to work_struct containing our data
2097 *
2098 * This task handles client interactions. Because client calls can be
2099 * reentrant, we can't handle them in the watchdog.
2100 **/
2101static void i40evf_client_task(struct work_struct *work)
2102{
2103 struct i40evf_adapter *adapter =
2104 container_of(work, struct i40evf_adapter, client_task.work);
2105
2106 /* If we can't get the client bit, just give up. We'll be rescheduled
2107 * later.
2108 */
2109
2110 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2111 return;
2112
2113 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2114 i40evf_client_subtask(adapter);
2115 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2116 goto out;
2117 }
Alan Brady01acc732017-11-14 07:00:51 -05002118 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2119 i40evf_notify_client_l2_params(&adapter->vsi);
2120 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2121 goto out;
2122 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002123 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2124 i40evf_notify_client_close(&adapter->vsi, false);
2125 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2126 goto out;
2127 }
2128 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2129 i40evf_notify_client_open(&adapter->vsi);
2130 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002131 }
2132out:
2133 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2134}
2135
2136/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002137 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2138 * @adapter: board private structure
2139 *
2140 * Free all transmit software resources
2141 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002142void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002143{
2144 int i;
2145
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002146 if (!adapter->tx_rings)
2147 return;
2148
Mitch Williamscc052922014-10-25 03:24:34 +00002149 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002150 if (adapter->tx_rings[i].desc)
2151 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002152}
2153
2154/**
2155 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2156 * @adapter: board private structure
2157 *
2158 * If this function returns with an error, then it's possible one or
2159 * more of the rings is populated (while the rest are not). It is the
2160 * callers duty to clean those orphaned rings.
2161 *
2162 * Return 0 on success, negative on failure
2163 **/
2164static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2165{
2166 int i, err = 0;
2167
Mitch Williamscc052922014-10-25 03:24:34 +00002168 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002169 adapter->tx_rings[i].count = adapter->tx_desc_count;
2170 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002171 if (!err)
2172 continue;
2173 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002174 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002175 break;
2176 }
2177
2178 return err;
2179}
2180
2181/**
2182 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2183 * @adapter: board private structure
2184 *
2185 * If this function returns with an error, then it's possible one or
2186 * more of the rings is populated (while the rest are not). It is the
2187 * callers duty to clean those orphaned rings.
2188 *
2189 * Return 0 on success, negative on failure
2190 **/
2191static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2192{
2193 int i, err = 0;
2194
Mitch Williamscc052922014-10-25 03:24:34 +00002195 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002196 adapter->rx_rings[i].count = adapter->rx_desc_count;
2197 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002198 if (!err)
2199 continue;
2200 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002201 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002202 break;
2203 }
2204 return err;
2205}
2206
2207/**
2208 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2209 * @adapter: board private structure
2210 *
2211 * Free all receive software resources
2212 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002213void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002214{
2215 int i;
2216
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002217 if (!adapter->rx_rings)
2218 return;
2219
Mitch Williamscc052922014-10-25 03:24:34 +00002220 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002221 if (adapter->rx_rings[i].desc)
2222 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002223}
2224
2225/**
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002226 * i40evf_validate_channel_config - validate queue mapping info
2227 * @adapter: board private structure
2228 * @mqprio_qopt: queue parameters
2229 *
2230 * This function validates if the config provided by the user to
2231 * configure queue channels is valid or not. Returns 0 on a valid
2232 * config.
2233 **/
2234static int i40evf_validate_ch_config(struct i40evf_adapter *adapter,
2235 struct tc_mqprio_qopt_offload *mqprio_qopt)
2236{
2237 int i, num_qps = 0;
2238
2239 if (mqprio_qopt->qopt.num_tc > I40EVF_MAX_TRAFFIC_CLASS ||
2240 mqprio_qopt->qopt.num_tc < 1)
2241 return -EINVAL;
2242
2243 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2244 if (!mqprio_qopt->qopt.count[i] ||
2245 mqprio_qopt->min_rate[i] ||
2246 mqprio_qopt->max_rate[i] ||
2247 mqprio_qopt->qopt.offset[i] != num_qps)
2248 return -EINVAL;
2249 num_qps += mqprio_qopt->qopt.count[i];
2250 }
2251 if (num_qps > MAX_QUEUES)
2252 return -EINVAL;
2253
2254 return 0;
2255}
2256
2257/**
2258 * __i40evf_setup_tc - configure multiple traffic classes
2259 * @netdev: network interface device structure
2260 * @type_date: tc offload data
2261 *
2262 * This function processes the config information provided by the
2263 * user to configure traffic classes/queue channels and packages the
2264 * information to request the PF to setup traffic classes.
2265 *
2266 * Returns 0 on success.
2267 **/
2268static int __i40evf_setup_tc(struct net_device *netdev, void *type_data)
2269{
2270 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2271 struct i40evf_adapter *adapter = netdev_priv(netdev);
2272 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2273 u8 num_tc = 0, total_qps = 0;
2274 int ret = 0, netdev_tc = 0;
2275 u16 mode;
2276 int i;
2277
2278 num_tc = mqprio_qopt->qopt.num_tc;
2279 mode = mqprio_qopt->mode;
2280
2281 /* delete queue_channel */
2282 if (!mqprio_qopt->qopt.hw) {
2283 if (adapter->ch_config.state == __I40EVF_TC_RUNNING) {
2284 /* reset the tc configuration */
2285 netdev_reset_tc(netdev);
2286 adapter->num_tc = 0;
2287 netif_tx_stop_all_queues(netdev);
2288 netif_tx_disable(netdev);
2289 adapter->aq_required = I40EVF_FLAG_AQ_DISABLE_CHANNELS;
2290 goto exit;
2291 } else {
2292 return -EINVAL;
2293 }
2294 }
2295
2296 /* add queue channel */
2297 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2298 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2299 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2300 return -EOPNOTSUPP;
2301 }
2302 if (adapter->ch_config.state != __I40EVF_TC_INVALID) {
2303 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2304 return -EINVAL;
2305 }
2306
2307 ret = i40evf_validate_ch_config(adapter, mqprio_qopt);
2308 if (ret)
2309 return ret;
2310 /* Return if same TC config is requested */
2311 if (adapter->num_tc == num_tc)
2312 return 0;
2313 adapter->num_tc = num_tc;
2314
2315 for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2316 if (i < num_tc) {
2317 adapter->ch_config.ch_info[i].count =
2318 mqprio_qopt->qopt.count[i];
2319 adapter->ch_config.ch_info[i].offset =
2320 mqprio_qopt->qopt.offset[i];
2321 total_qps += mqprio_qopt->qopt.count[i];
2322 } else {
2323 adapter->ch_config.ch_info[i].count = 1;
2324 adapter->ch_config.ch_info[i].offset = 0;
2325 }
2326 }
2327 adapter->ch_config.total_qps = total_qps;
2328 netif_tx_stop_all_queues(netdev);
2329 netif_tx_disable(netdev);
2330 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_CHANNELS;
2331 netdev_reset_tc(netdev);
2332 /* Report the tc mapping up the stack */
2333 netdev_set_num_tc(adapter->netdev, num_tc);
2334 for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2335 u16 qcount = mqprio_qopt->qopt.count[i];
2336 u16 qoffset = mqprio_qopt->qopt.offset[i];
2337
2338 if (i < num_tc)
2339 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2340 qoffset);
2341 }
2342 }
2343exit:
2344 return ret;
2345}
2346
2347/**
2348 * i40evf_setup_tc - configure multiple traffic classes
2349 * @netdev: network interface device structure
2350 * @type: type of offload
2351 * @type_date: tc offload data
2352 *
2353 * This function is the callback to ndo_setup_tc in the
2354 * netdev_ops.
2355 *
2356 * Returns 0 on success
2357 **/
2358static int i40evf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2359 void *type_data)
2360{
2361 if (type != TC_SETUP_QDISC_MQPRIO)
2362 return -EOPNOTSUPP;
2363
2364 return __i40evf_setup_tc(netdev, type_data);
2365}
2366
2367/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002368 * i40evf_open - Called when a network interface is made active
2369 * @netdev: network interface device structure
2370 *
2371 * Returns 0 on success, negative value on failure
2372 *
2373 * The open entry point is called when a network interface is made
2374 * active by the system (IFF_UP). At this point all resources needed
2375 * for transmit and receive operations are allocated, the interrupt
2376 * handler is registered with the OS, the watchdog timer is started,
2377 * and the stack is notified that the interface is ready.
2378 **/
2379static int i40evf_open(struct net_device *netdev)
2380{
2381 struct i40evf_adapter *adapter = netdev_priv(netdev);
2382 int err;
2383
Mitch Williamsef8693e2014-02-13 03:48:53 -08002384 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2385 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2386 return -EIO;
2387 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002388
Jacob Keller9b2aef12017-10-27 11:06:52 -04002389 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2390 &adapter->crit_section))
2391 usleep_range(500, 1000);
2392
2393 if (adapter->state != __I40EVF_DOWN) {
2394 err = -EBUSY;
2395 goto err_unlock;
2396 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002397
2398 /* allocate transmit descriptors */
2399 err = i40evf_setup_all_tx_resources(adapter);
2400 if (err)
2401 goto err_setup_tx;
2402
2403 /* allocate receive descriptors */
2404 err = i40evf_setup_all_rx_resources(adapter);
2405 if (err)
2406 goto err_setup_rx;
2407
2408 /* clear any pending interrupts, may auto mask */
2409 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2410 if (err)
2411 goto err_req_irq;
2412
Mitch Williams44151cd2015-04-27 14:57:17 -04002413 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002414 i40evf_configure(adapter);
2415
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002416 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002417
2418 i40evf_irq_enable(adapter, true);
2419
Jacob Keller9b2aef12017-10-27 11:06:52 -04002420 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2421
Greg Rose5eae00c2013-12-21 06:12:45 +00002422 return 0;
2423
2424err_req_irq:
2425 i40evf_down(adapter);
2426 i40evf_free_traffic_irqs(adapter);
2427err_setup_rx:
2428 i40evf_free_all_rx_resources(adapter);
2429err_setup_tx:
2430 i40evf_free_all_tx_resources(adapter);
Jacob Keller9b2aef12017-10-27 11:06:52 -04002431err_unlock:
2432 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00002433
2434 return err;
2435}
2436
2437/**
2438 * i40evf_close - Disables a network interface
2439 * @netdev: network interface device structure
2440 *
2441 * Returns 0, this is not allowed to fail
2442 *
2443 * The close entry point is called when an interface is de-activated
2444 * by the OS. The hardware is still under the drivers control, but
2445 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2446 * are freed, along with all transmit and receive resources.
2447 **/
2448static int i40evf_close(struct net_device *netdev)
2449{
2450 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002451 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002452
Mitch Williams209dc4d2015-12-09 15:50:27 -08002453 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002454 return 0;
2455
Jacob Keller9b2aef12017-10-27 11:06:52 -04002456 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2457 &adapter->crit_section))
2458 usleep_range(500, 1000);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002459
Jacob Keller0da36b92017-04-19 09:25:55 -04002460 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002461 if (CLIENT_ENABLED(adapter))
2462 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002463
2464 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002465 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002466 i40evf_free_traffic_irqs(adapter);
2467
Jacob Keller9b2aef12017-10-27 11:06:52 -04002468 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2469
Mitch Williams51f38262016-12-12 15:44:11 -08002470 /* We explicitly don't free resources here because the hardware is
2471 * still active and can DMA into memory. Resources are cleared in
2472 * i40evf_virtchnl_completion() after we get confirmation from the PF
2473 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002474 *
2475 * Also, we wait for state to transition to __I40EVF_DOWN before
2476 * returning. State change occurs in i40evf_virtchnl_completion() after
2477 * VF resources are released (which occurs after PF driver processes and
2478 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002479 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002480
2481 status = wait_event_timeout(adapter->down_waitqueue,
2482 adapter->state == __I40EVF_DOWN,
2483 msecs_to_jiffies(200));
2484 if (!status)
2485 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002486 return 0;
2487}
2488
2489/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002490 * i40evf_change_mtu - Change the Maximum Transfer Unit
2491 * @netdev: network interface device structure
2492 * @new_mtu: new value for maximum frame size
2493 *
2494 * Returns 0 on success, negative on failure
2495 **/
2496static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2497{
2498 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002499
Greg Rose5eae00c2013-12-21 06:12:45 +00002500 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002501 if (CLIENT_ENABLED(adapter)) {
2502 i40evf_notify_client_l2_params(&adapter->vsi);
2503 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2504 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002505 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2506 schedule_work(&adapter->reset_task);
2507
Greg Rose5eae00c2013-12-21 06:12:45 +00002508 return 0;
2509}
2510
Alexander Duyck06fc0162016-10-25 16:08:47 -07002511/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002512 * i40e_set_features - set the netdev feature flags
2513 * @netdev: ptr to the netdev being adjusted
2514 * @features: the feature set that the stack is suggesting
2515 * Note: expects to be called while under rtnl_lock()
2516 **/
2517static int i40evf_set_features(struct net_device *netdev,
2518 netdev_features_t features)
2519{
2520 struct i40evf_adapter *adapter = netdev_priv(netdev);
2521
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002522 /* Don't allow changing VLAN_RX flag when VLAN is set for VF
2523 * and return an error in this case
2524 */
2525 if (VLAN_ALLOWED(adapter)) {
2526 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2527 adapter->aq_required |=
2528 I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2529 else
2530 adapter->aq_required |=
2531 I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2532 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
Mariusz Stachura87743702017-07-17 22:09:45 -07002533 return -EINVAL;
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002534 }
Mariusz Stachura87743702017-07-17 22:09:45 -07002535
2536 return 0;
2537}
2538
2539/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002540 * i40evf_features_check - Validate encapsulated packet conforms to limits
2541 * @skb: skb buff
2542 * @netdev: This physical port's netdev
2543 * @features: Offload features that the stack believes apply
2544 **/
2545static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2546 struct net_device *dev,
2547 netdev_features_t features)
2548{
2549 size_t len;
2550
2551 /* No point in doing any of this if neither checksum nor GSO are
2552 * being requested for this frame. We can rule out both by just
2553 * checking for CHECKSUM_PARTIAL
2554 */
2555 if (skb->ip_summed != CHECKSUM_PARTIAL)
2556 return features;
2557
2558 /* We cannot support GSO if the MSS is going to be less than
2559 * 64 bytes. If it is then we need to drop support for GSO.
2560 */
2561 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2562 features &= ~NETIF_F_GSO_MASK;
2563
2564 /* MACLEN can support at most 63 words */
2565 len = skb_network_header(skb) - skb->data;
2566 if (len & ~(63 * 2))
2567 goto out_err;
2568
2569 /* IPLEN and EIPLEN can support at most 127 dwords */
2570 len = skb_transport_header(skb) - skb_network_header(skb);
2571 if (len & ~(127 * 4))
2572 goto out_err;
2573
2574 if (skb->encapsulation) {
2575 /* L4TUNLEN can support 127 words */
2576 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2577 if (len & ~(127 * 2))
2578 goto out_err;
2579
2580 /* IPLEN can support at most 127 dwords */
2581 len = skb_inner_transport_header(skb) -
2582 skb_inner_network_header(skb);
2583 if (len & ~(127 * 4))
2584 goto out_err;
2585 }
2586
2587 /* No need to validate L4LEN as TCP is the only protocol with a
2588 * a flexible value and we support all possible values supported
2589 * by TCP, which is at most 15 dwords
2590 */
2591
2592 return features;
2593out_err:
2594 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2595}
2596
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002597/**
2598 * i40evf_fix_features - fix up the netdev feature bits
2599 * @netdev: our net device
2600 * @features: desired feature bits
2601 *
2602 * Returns fixed-up features bits
2603 **/
2604static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2605 netdev_features_t features)
2606{
2607 struct i40evf_adapter *adapter = netdev_priv(netdev);
2608
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002609 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
2610 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
2611 NETIF_F_HW_VLAN_CTAG_RX |
2612 NETIF_F_HW_VLAN_CTAG_FILTER);
2613
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002614 return features;
2615}
2616
Greg Rose5eae00c2013-12-21 06:12:45 +00002617static const struct net_device_ops i40evf_netdev_ops = {
2618 .ndo_open = i40evf_open,
2619 .ndo_stop = i40evf_close,
2620 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002621 .ndo_set_rx_mode = i40evf_set_rx_mode,
2622 .ndo_validate_addr = eth_validate_addr,
2623 .ndo_set_mac_address = i40evf_set_mac,
2624 .ndo_change_mtu = i40evf_change_mtu,
2625 .ndo_tx_timeout = i40evf_tx_timeout,
2626 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2627 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002628 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002629 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002630 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002631#ifdef CONFIG_NET_POLL_CONTROLLER
2632 .ndo_poll_controller = i40evf_netpoll,
2633#endif
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002634 .ndo_setup_tc = i40evf_setup_tc,
Greg Rose5eae00c2013-12-21 06:12:45 +00002635};
2636
2637/**
2638 * i40evf_check_reset_complete - check that VF reset is complete
2639 * @hw: pointer to hw struct
2640 *
2641 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2642 **/
2643static int i40evf_check_reset_complete(struct i40e_hw *hw)
2644{
2645 u32 rstat;
2646 int i;
2647
2648 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002649 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2650 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002651 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2652 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002653 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002654 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002655 }
2656 return -EBUSY;
2657}
2658
2659/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002660 * i40evf_process_config - Process the config information we got from the PF
2661 * @adapter: board private structure
2662 *
2663 * Verify that we have a valid config struct, and set up our netdev features
2664 * and our VSI struct.
2665 **/
2666int i40evf_process_config(struct i40evf_adapter *adapter)
2667{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002668 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002669 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002670 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002671 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002672 netdev_features_t hw_enc_features;
2673 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002674
2675 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002676 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002677 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002678 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002679 }
2680 if (!adapter->vsi_res) {
2681 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2682 return -ENODEV;
2683 }
2684
Alan Brady5b36e8d2017-08-22 06:57:50 -04002685 if (num_req_queues &&
2686 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2687 /* Problem. The PF gave us fewer queues than what we had
2688 * negotiated in our request. Need a reset to see if we can't
2689 * get back to a working state.
2690 */
2691 dev_err(&adapter->pdev->dev,
2692 "Requested %d queues, but PF only gave us %d.\n",
2693 num_req_queues,
2694 adapter->vsi_res->num_queue_pairs);
2695 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2696 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2697 i40evf_schedule_reset(adapter);
2698 return -ENODEV;
2699 }
2700 adapter->num_req_queues = 0;
2701
Preethi Banalabacd75c2017-03-27 14:43:18 -07002702 hw_enc_features = NETIF_F_SG |
2703 NETIF_F_IP_CSUM |
2704 NETIF_F_IPV6_CSUM |
2705 NETIF_F_HIGHDMA |
2706 NETIF_F_SOFT_FEATURES |
2707 NETIF_F_TSO |
2708 NETIF_F_TSO_ECN |
2709 NETIF_F_TSO6 |
2710 NETIF_F_SCTP_CRC |
2711 NETIF_F_RXHASH |
2712 NETIF_F_RXCSUM |
2713 0;
2714
2715 /* advertise to stack only if offloads for encapsulated packets is
2716 * supported
2717 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002718 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002719 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002720 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002721 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002722 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002723 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002724 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002725 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002726 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002727
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002728 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002729 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002730 netdev->gso_partial_features |=
2731 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002732
Preethi Banalabacd75c2017-03-27 14:43:18 -07002733 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2734 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2735 netdev->hw_enc_features |= hw_enc_features;
2736 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002737 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002738 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002739
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002740 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002741 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002742 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002743 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002744
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002745 /* Enable VLAN features if supported */
2746 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2747 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
2748 NETIF_F_HW_VLAN_CTAG_RX);
2749
Preethi Banalabacd75c2017-03-27 14:43:18 -07002750 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002751
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002752 netdev->features |= hw_features;
2753
2754 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2755 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002756
2757 adapter->vsi.id = adapter->vsi_res->vsi_id;
2758
2759 adapter->vsi.back = adapter;
2760 adapter->vsi.base_vector = 1;
2761 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002762 vsi->netdev = adapter->netdev;
2763 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002764 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002765 adapter->rss_key_size = vfres->rss_key_size;
2766 adapter->rss_lut_size = vfres->rss_lut_size;
2767 } else {
2768 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2769 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2770 }
2771
Mitch Williamse6d038d2015-06-04 16:23:58 -04002772 return 0;
2773}
2774
2775/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002776 * i40evf_init_task - worker thread to perform delayed initialization
2777 * @work: pointer to work_struct containing our data
2778 *
2779 * This task completes the work that was begun in probe. Due to the nature
2780 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002781 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002782 * whole system, we'll do it in a task so we can sleep.
2783 * This task only runs during driver init. Once we've established
2784 * communications with the PF driver and set up our netdev, the watchdog
2785 * takes over.
2786 **/
2787static void i40evf_init_task(struct work_struct *work)
2788{
2789 struct i40evf_adapter *adapter = container_of(work,
2790 struct i40evf_adapter,
2791 init_task.work);
2792 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002793 struct i40e_hw *hw = &adapter->hw;
2794 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002795 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002796
2797 switch (adapter->state) {
2798 case __I40EVF_STARTUP:
2799 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002800 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2801 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002802 err = i40e_set_mac_type(hw);
2803 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002804 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2805 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002806 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002807 }
2808 err = i40evf_check_reset_complete(hw);
2809 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002810 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002811 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002812 goto err;
2813 }
2814 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2815 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2816 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2817 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2818
2819 err = i40evf_init_adminq(hw);
2820 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002821 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2822 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002823 goto err;
2824 }
2825 err = i40evf_send_api_ver(adapter);
2826 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002827 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002828 i40evf_shutdown_adminq(hw);
2829 goto err;
2830 }
2831 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2832 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002833 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002834 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002835 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002836 i40evf_shutdown_adminq(hw);
2837 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002838 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002839 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002840
2841 /* aq msg sent, awaiting reply */
2842 err = i40evf_verify_api_ver(adapter);
2843 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002844 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002845 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002846 else
2847 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2848 adapter->pf_version.major,
2849 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002850 VIRTCHNL_VERSION_MAJOR,
2851 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002852 goto err;
2853 }
2854 err = i40evf_send_vf_config_msg(adapter);
2855 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002856 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002857 err);
2858 goto err;
2859 }
2860 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2861 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002862 case __I40EVF_INIT_GET_RESOURCES:
2863 /* aq msg sent, awaiting reply */
2864 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002865 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002866 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002867 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002868 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002869 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002870 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002871 }
2872 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002873 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002874 err = i40evf_send_vf_config_msg(adapter);
2875 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002876 } else if (err == I40E_ERR_PARAM) {
2877 /* We only get ERR_PARAM if the device is in a very bad
2878 * state or if we've been disabled for previous bad
2879 * behavior. Either way, we're done now.
2880 */
2881 i40evf_shutdown_adminq(hw);
2882 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2883 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002884 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002885 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002886 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2887 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002888 goto err_alloc;
2889 }
2890 adapter->state = __I40EVF_INIT_SW;
2891 break;
2892 default:
2893 goto err_alloc;
2894 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002895
Mitch Williamse6d038d2015-06-04 16:23:58 -04002896 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002897 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002898 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002899
2900 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2901
Greg Rose5eae00c2013-12-21 06:12:45 +00002902 netdev->netdev_ops = &i40evf_netdev_ops;
2903 i40evf_set_ethtool_ops(netdev);
2904 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002905
Jarod Wilson91c527a2016-10-17 15:54:05 -04002906 /* MTU range: 68 - 9710 */
2907 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002908 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002909
Greg Rose5eae00c2013-12-21 06:12:45 +00002910 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002911 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002912 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002913 eth_hw_addr_random(netdev);
2914 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2915 } else {
2916 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2917 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2918 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002919 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002920
Kees Cook26566ea2017-10-16 17:29:35 -07002921 timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
Greg Rose5eae00c2013-12-21 06:12:45 +00002922 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2923
Mitch Williamsd732a182014-04-24 06:41:37 +00002924 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2925 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002926 err = i40evf_init_interrupt_scheme(adapter);
2927 if (err)
2928 goto err_sw_init;
2929 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002930 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002931 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002932 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2933
Greg Rose5eae00c2013-12-21 06:12:45 +00002934 err = i40evf_request_misc_irq(adapter);
2935 if (err)
2936 goto err_sw_init;
2937
2938 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002939 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002940
Mitch Williamsef8693e2014-02-13 03:48:53 -08002941 if (!adapter->netdev_registered) {
2942 err = register_netdev(netdev);
2943 if (err)
2944 goto err_register;
2945 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002946
2947 adapter->netdev_registered = true;
2948
2949 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002950 if (CLIENT_ALLOWED(adapter)) {
2951 err = i40evf_lan_add_device(adapter);
2952 if (err)
2953 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2954 err);
2955 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002956
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002957 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002958 if (netdev->features & NETIF_F_GRO)
2959 dev_info(&pdev->dev, "GRO is enabled\n");
2960
Greg Rose5eae00c2013-12-21 06:12:45 +00002961 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002962 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002963 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002964 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002965
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002966 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2967 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2968 if (!adapter->rss_key || !adapter->rss_lut)
2969 goto err_mem;
2970
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002971 if (RSS_AQ(adapter)) {
2972 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2973 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2974 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002975 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002976 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002977 return;
2978restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002979 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002980 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002981err_mem:
2982 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002983err_register:
2984 i40evf_free_misc_irq(adapter);
2985err_sw_init:
2986 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002987err_alloc:
2988 kfree(adapter->vf_res);
2989 adapter->vf_res = NULL;
2990err:
2991 /* Things went into the weeds, so try again later */
2992 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002993 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002994 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002995 i40evf_shutdown_adminq(hw);
2996 adapter->state = __I40EVF_STARTUP;
2997 schedule_delayed_work(&adapter->init_task, HZ * 5);
2998 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002999 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04003000 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00003001}
3002
3003/**
3004 * i40evf_shutdown - Shutdown the device in preparation for a reboot
3005 * @pdev: pci device structure
3006 **/
3007static void i40evf_shutdown(struct pci_dev *pdev)
3008{
3009 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00003010 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00003011
3012 netif_device_detach(netdev);
3013
3014 if (netif_running(netdev))
3015 i40evf_close(netdev);
3016
Mitch Williams00293fd2015-01-09 11:18:18 +00003017 /* Prevent the watchdog from running. */
3018 adapter->state = __I40EVF_REMOVE;
3019 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00003020
Greg Rose5eae00c2013-12-21 06:12:45 +00003021#ifdef CONFIG_PM
3022 pci_save_state(pdev);
3023
3024#endif
3025 pci_disable_device(pdev);
3026}
3027
3028/**
3029 * i40evf_probe - Device Initialization Routine
3030 * @pdev: PCI device information struct
3031 * @ent: entry in i40evf_pci_tbl
3032 *
3033 * Returns 0 on success, negative on failure
3034 *
3035 * i40evf_probe initializes an adapter identified by a pci_dev structure.
3036 * The OS initialization, configuring of the adapter private structure,
3037 * and a hardware reset occur.
3038 **/
3039static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3040{
3041 struct net_device *netdev;
3042 struct i40evf_adapter *adapter = NULL;
3043 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08003044 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003045
3046 err = pci_enable_device(pdev);
3047 if (err)
3048 return err;
3049
Mitch Williams64942942014-02-11 08:26:33 +00003050 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00003051 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00003052 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3053 if (err) {
3054 dev_err(&pdev->dev,
3055 "DMA configuration failed: 0x%x\n", err);
3056 goto err_dma;
3057 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003058 }
3059
3060 err = pci_request_regions(pdev, i40evf_driver_name);
3061 if (err) {
3062 dev_err(&pdev->dev,
3063 "pci_request_regions failed 0x%x\n", err);
3064 goto err_pci_reg;
3065 }
3066
3067 pci_enable_pcie_error_reporting(pdev);
3068
3069 pci_set_master(pdev);
3070
Mitch Williams1255b7a2015-11-06 15:25:59 -08003071 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00003072 if (!netdev) {
3073 err = -ENOMEM;
3074 goto err_alloc_etherdev;
3075 }
3076
3077 SET_NETDEV_DEV(netdev, &pdev->dev);
3078
3079 pci_set_drvdata(pdev, netdev);
3080 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00003081
3082 adapter->netdev = netdev;
3083 adapter->pdev = pdev;
3084
3085 hw = &adapter->hw;
3086 hw->back = adapter;
3087
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003088 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00003089 adapter->state = __I40EVF_STARTUP;
3090
3091 /* Call save state here because it relies on the adapter struct. */
3092 pci_save_state(pdev);
3093
3094 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3095 pci_resource_len(pdev, 0));
3096 if (!hw->hw_addr) {
3097 err = -EIO;
3098 goto err_ioremap;
3099 }
3100 hw->vendor_id = pdev->vendor;
3101 hw->device_id = pdev->device;
3102 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3103 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3104 hw->subsystem_device_id = pdev->subsystem_device;
3105 hw->bus.device = PCI_SLOT(pdev->devfn);
3106 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08003107 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00003108
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003109 /* set up the locks for the AQ, do this only once in probe
3110 * and destroy them only once in remove
3111 */
3112 mutex_init(&hw->aq.asq_mutex);
3113 mutex_init(&hw->aq.arq_mutex);
3114
Jacob Keller504398f2017-10-27 11:06:50 -04003115 spin_lock_init(&adapter->mac_vlan_list_lock);
3116
Serey Kong8bb1a542014-08-01 13:27:15 -07003117 INIT_LIST_HEAD(&adapter->mac_filter_list);
3118 INIT_LIST_HEAD(&adapter->vlan_filter_list);
3119
Greg Rose5eae00c2013-12-21 06:12:45 +00003120 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
3121 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
3122 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003123 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003124 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07003125 schedule_delayed_work(&adapter->init_task,
3126 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00003127
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04003128 /* Setup the wait queue for indicating transition to down status */
3129 init_waitqueue_head(&adapter->down_waitqueue);
3130
Greg Rose5eae00c2013-12-21 06:12:45 +00003131 return 0;
3132
3133err_ioremap:
3134 free_netdev(netdev);
3135err_alloc_etherdev:
3136 pci_release_regions(pdev);
3137err_pci_reg:
3138err_dma:
3139 pci_disable_device(pdev);
3140 return err;
3141}
3142
3143#ifdef CONFIG_PM
3144/**
3145 * i40evf_suspend - Power management suspend routine
3146 * @pdev: PCI device information struct
3147 * @state: unused
3148 *
3149 * Called when the system (VM) is entering sleep/suspend.
3150 **/
3151static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
3152{
3153 struct net_device *netdev = pci_get_drvdata(pdev);
3154 struct i40evf_adapter *adapter = netdev_priv(netdev);
3155 int retval = 0;
3156
3157 netif_device_detach(netdev);
3158
Jacob Keller9b2aef12017-10-27 11:06:52 -04003159 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3160 &adapter->crit_section))
3161 usleep_range(500, 1000);
3162
Greg Rose5eae00c2013-12-21 06:12:45 +00003163 if (netif_running(netdev)) {
3164 rtnl_lock();
3165 i40evf_down(adapter);
3166 rtnl_unlock();
3167 }
3168 i40evf_free_misc_irq(adapter);
3169 i40evf_reset_interrupt_capability(adapter);
3170
Jacob Keller9b2aef12017-10-27 11:06:52 -04003171 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3172
Greg Rose5eae00c2013-12-21 06:12:45 +00003173 retval = pci_save_state(pdev);
3174 if (retval)
3175 return retval;
3176
3177 pci_disable_device(pdev);
3178
3179 return 0;
3180}
3181
3182/**
Joe Perchesdbedd442015-03-06 20:49:12 -08003183 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00003184 * @pdev: PCI device information struct
3185 *
3186 * Called when the system (VM) is resumed from sleep/suspend.
3187 **/
3188static int i40evf_resume(struct pci_dev *pdev)
3189{
3190 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3191 struct net_device *netdev = adapter->netdev;
3192 u32 err;
3193
3194 pci_set_power_state(pdev, PCI_D0);
3195 pci_restore_state(pdev);
3196 /* pci_restore_state clears dev->state_saved so call
3197 * pci_save_state to restore it.
3198 */
3199 pci_save_state(pdev);
3200
3201 err = pci_enable_device_mem(pdev);
3202 if (err) {
3203 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3204 return err;
3205 }
3206 pci_set_master(pdev);
3207
3208 rtnl_lock();
3209 err = i40evf_set_interrupt_capability(adapter);
3210 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003211 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003212 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3213 return err;
3214 }
3215 err = i40evf_request_misc_irq(adapter);
3216 rtnl_unlock();
3217 if (err) {
3218 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3219 return err;
3220 }
3221
3222 schedule_work(&adapter->reset_task);
3223
3224 netif_device_attach(netdev);
3225
3226 return err;
3227}
3228
3229#endif /* CONFIG_PM */
3230/**
3231 * i40evf_remove - Device Removal Routine
3232 * @pdev: PCI device information struct
3233 *
3234 * i40evf_remove is called by the PCI subsystem to alert the driver
3235 * that it should release a PCI device. The could be caused by a
3236 * Hot-Plug event, or because the driver is going to be removed from
3237 * memory.
3238 **/
3239static void i40evf_remove(struct pci_dev *pdev)
3240{
3241 struct net_device *netdev = pci_get_drvdata(pdev);
3242 struct i40evf_adapter *adapter = netdev_priv(netdev);
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05003243 struct i40evf_vlan_filter *vlf, *vlftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07003244 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003245 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003246 int err;
Avinash Dayanand06aa0402017-12-18 05:16:43 -05003247 /* Indicate we are in remove and not to run reset_task */
3248 set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00003249 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003250 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003251 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003252 if (adapter->netdev_registered) {
3253 unregister_netdev(netdev);
3254 adapter->netdev_registered = false;
3255 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003256 if (CLIENT_ALLOWED(adapter)) {
3257 err = i40evf_lan_del_device(adapter);
3258 if (err)
3259 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3260 err);
3261 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003262
Mitch Williamsf4a71882015-01-09 11:18:16 +00003263 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003264 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003265 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003266 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003267 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003268 /* If the FW isn't responding, kick it once, but only once. */
3269 if (!i40evf_asq_done(hw)) {
3270 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003271 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003272 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003273 i40evf_free_all_tx_resources(adapter);
3274 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003275 i40evf_misc_irq_disable(adapter);
3276 i40evf_free_misc_irq(adapter);
3277 i40evf_reset_interrupt_capability(adapter);
3278 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003279
Mitch Williamse5d17c32014-06-04 04:22:38 +00003280 if (adapter->watchdog_timer.function)
3281 del_timer_sync(&adapter->watchdog_timer);
3282
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003283 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003284
Greg Rose5eae00c2013-12-21 06:12:45 +00003285 if (hw->aq.asq.count)
3286 i40evf_shutdown_adminq(hw);
3287
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003288 /* destroy the locks only once, here */
3289 mutex_destroy(&hw->aq.arq_mutex);
3290 mutex_destroy(&hw->aq.asq_mutex);
3291
Greg Rose5eae00c2013-12-21 06:12:45 +00003292 iounmap(hw->hw_addr);
3293 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003294 i40evf_free_all_tx_resources(adapter);
3295 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003296 i40evf_free_queues(adapter);
3297 kfree(adapter->vf_res);
Jacob Keller504398f2017-10-27 11:06:50 -04003298 spin_lock_bh(&adapter->mac_vlan_list_lock);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003299 /* If we got removed before an up/down sequence, we've got a filter
3300 * hanging out there that we need to get rid of.
3301 */
3302 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3303 list_del(&f->list);
3304 kfree(f);
3305 }
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05003306 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3307 list) {
3308 list_del(&vlf->list);
3309 kfree(vlf);
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003310 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003311
Jacob Keller504398f2017-10-27 11:06:50 -04003312 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3313
Greg Rose5eae00c2013-12-21 06:12:45 +00003314 free_netdev(netdev);
3315
3316 pci_disable_pcie_error_reporting(pdev);
3317
3318 pci_disable_device(pdev);
3319}
3320
3321static struct pci_driver i40evf_driver = {
3322 .name = i40evf_driver_name,
3323 .id_table = i40evf_pci_tbl,
3324 .probe = i40evf_probe,
3325 .remove = i40evf_remove,
3326#ifdef CONFIG_PM
3327 .suspend = i40evf_suspend,
3328 .resume = i40evf_resume,
3329#endif
3330 .shutdown = i40evf_shutdown,
3331};
3332
3333/**
3334 * i40e_init_module - Driver Registration Routine
3335 *
3336 * i40e_init_module is the first routine called when the driver is
3337 * loaded. All it does is register with the PCI subsystem.
3338 **/
3339static int __init i40evf_init_module(void)
3340{
3341 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003342
Greg Rose5eae00c2013-12-21 06:12:45 +00003343 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003344 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003345
3346 pr_info("%s\n", i40evf_copyright);
3347
Jacob Keller6992a6c2016-08-04 11:37:01 -07003348 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3349 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003350 if (!i40evf_wq) {
3351 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3352 return -ENOMEM;
3353 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003354 ret = pci_register_driver(&i40evf_driver);
3355 return ret;
3356}
3357
3358module_init(i40evf_init_module);
3359
3360/**
3361 * i40e_exit_module - Driver Exit Cleanup Routine
3362 *
3363 * i40e_exit_module is called just before the driver is removed
3364 * from memory.
3365 **/
3366static void __exit i40evf_exit_module(void)
3367{
3368 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003369 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003370}
3371
3372module_exit(i40evf_exit_module);
3373
3374/* i40evf_main.c */