blob: dae121877935fa53ee2d399bda2ca80423217567 [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
Greg Rose5eae00c2013-12-21 06:12:45 +0000818 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000819 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000820 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Jacob Keller504398f2017-10-27 11:06:50 -0400821 if (!f)
Paweł Jabłoński8cd5fe62018-02-05 13:03:36 -0800822 return f;
Greg Rose5eae00c2013-12-21 06:12:45 +0000823
Greg Rose9a173902014-05-22 06:32:02 +0000824 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000825
Mitch Williams63590b62016-05-16 10:26:42 -0700826 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000827 f->add = true;
828 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Alan Bradyc766b9a2017-09-07 08:05:47 -0400829 } else {
830 f->remove = false;
Greg Rose5eae00c2013-12-21 06:12:45 +0000831 }
832
Greg Rose5eae00c2013-12-21 06:12:45 +0000833 return f;
834}
835
836/**
837 * i40evf_set_mac - NDO callback to set port mac address
838 * @netdev: network interface device structure
839 * @p: pointer to an address structure
840 *
841 * Returns 0 on success, negative on failure
842 **/
843static int i40evf_set_mac(struct net_device *netdev, void *p)
844{
845 struct i40evf_adapter *adapter = netdev_priv(netdev);
846 struct i40e_hw *hw = &adapter->hw;
847 struct i40evf_mac_filter *f;
848 struct sockaddr *addr = p;
849
850 if (!is_valid_ether_addr(addr->sa_data))
851 return -EADDRNOTAVAIL;
852
853 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
854 return 0;
855
Mitch Williams14e52ee2015-08-31 19:54:44 -0400856 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
857 return -EPERM;
858
Jacob Keller504398f2017-10-27 11:06:50 -0400859 spin_lock_bh(&adapter->mac_vlan_list_lock);
860
Mitch Williams14e52ee2015-08-31 19:54:44 -0400861 f = i40evf_find_filter(adapter, hw->mac.addr);
862 if (f) {
863 f->remove = true;
864 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
865 }
866
Paweł Jabłoński8cd5fe62018-02-05 13:03:36 -0800867 f = i40evf_add_filter(adapter, addr->sa_data);
868
Jacob Keller504398f2017-10-27 11:06:50 -0400869 spin_unlock_bh(&adapter->mac_vlan_list_lock);
870
Greg Rose5eae00c2013-12-21 06:12:45 +0000871 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000872 ether_addr_copy(hw->mac.addr, addr->sa_data);
873 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000874 }
875
876 return (f == NULL) ? -ENOMEM : 0;
877}
878
879/**
Jacob Keller8946b562018-01-22 12:00:38 -0500880 * i40evf_addr_sync - Callback for dev_(mc|uc)_sync to add address
881 * @netdev: the netdevice
882 * @addr: address to add
883 *
884 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
885 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
886 */
887static int i40evf_addr_sync(struct net_device *netdev, const u8 *addr)
888{
889 struct i40evf_adapter *adapter = netdev_priv(netdev);
890
891 if (i40evf_add_filter(adapter, addr))
892 return 0;
893 else
894 return -ENOMEM;
895}
896
897/**
898 * i40evf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
899 * @netdev: the netdevice
900 * @addr: address to add
901 *
902 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
903 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
904 */
905static int i40evf_addr_unsync(struct net_device *netdev, const u8 *addr)
906{
907 struct i40evf_adapter *adapter = netdev_priv(netdev);
908 struct i40evf_mac_filter *f;
909
910 /* Under some circumstances, we might receive a request to delete
911 * our own device address from our uc list. Because we store the
912 * device address in the VSI's MAC/VLAN filter list, we need to ignore
913 * such requests and not delete our device address from this list.
914 */
915 if (ether_addr_equal(addr, netdev->dev_addr))
916 return 0;
917
918 f = i40evf_find_filter(adapter, addr);
919 if (f) {
920 f->remove = true;
921 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
922 }
923 return 0;
924}
925
926/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000927 * i40evf_set_rx_mode - NDO callback to set the netdev filters
928 * @netdev: network interface device structure
929 **/
930static void i40evf_set_rx_mode(struct net_device *netdev)
931{
932 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000933
Jacob Keller504398f2017-10-27 11:06:50 -0400934 spin_lock_bh(&adapter->mac_vlan_list_lock);
Jacob Keller8946b562018-01-22 12:00:38 -0500935 __dev_uc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
936 __dev_mc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
937 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700938
939 if (netdev->flags & IFF_PROMISC &&
940 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
941 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
942 else if (!(netdev->flags & IFF_PROMISC) &&
943 adapter->flags & I40EVF_FLAG_PROMISC_ON)
944 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
945
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700946 if (netdev->flags & IFF_ALLMULTI &&
947 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
948 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
949 else if (!(netdev->flags & IFF_ALLMULTI) &&
950 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
951 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
Greg Rose5eae00c2013-12-21 06:12:45 +0000952}
953
954/**
955 * i40evf_napi_enable_all - enable NAPI on all queue vectors
956 * @adapter: board private structure
957 **/
958static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
959{
960 int q_idx;
961 struct i40e_q_vector *q_vector;
962 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
963
964 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
965 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000966
Mitch Williams7d96ba12015-10-26 19:44:39 -0400967 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000968 napi = &q_vector->napi;
969 napi_enable(napi);
970 }
971}
972
973/**
974 * i40evf_napi_disable_all - disable NAPI on all queue vectors
975 * @adapter: board private structure
976 **/
977static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
978{
979 int q_idx;
980 struct i40e_q_vector *q_vector;
981 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
982
983 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400984 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000985 napi_disable(&q_vector->napi);
986 }
987}
988
989/**
990 * i40evf_configure - set up transmit and receive data structures
991 * @adapter: board private structure
992 **/
993static void i40evf_configure(struct i40evf_adapter *adapter)
994{
995 struct net_device *netdev = adapter->netdev;
996 int i;
997
998 i40evf_set_rx_mode(netdev);
999
1000 i40evf_configure_tx(adapter);
1001 i40evf_configure_rx(adapter);
1002 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1003
Mitch Williamscc052922014-10-25 03:24:34 +00001004 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04001005 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +00001006
Mitch Williamsb1630982016-04-18 11:33:48 -07001007 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +00001008 }
1009}
1010
1011/**
1012 * i40evf_up_complete - Finish the last steps of bringing up a connection
1013 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001014 *
1015 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001016 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001017static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001018{
1019 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001020 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001021
1022 i40evf_napi_enable_all(adapter);
1023
1024 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001025 if (CLIENT_ENABLED(adapter))
1026 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001027 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001028}
1029
1030/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001031 * i40e_down - Shutdown the connection processing
1032 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001033 *
1034 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001035 **/
1036void i40evf_down(struct i40evf_adapter *adapter)
1037{
1038 struct net_device *netdev = adapter->netdev;
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05001039 struct i40evf_vlan_filter *vlf;
Greg Rose5eae00c2013-12-21 06:12:45 +00001040 struct i40evf_mac_filter *f;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001041 struct i40evf_cloud_filter *cf;
Greg Rose5eae00c2013-12-21 06:12:45 +00001042
Mitch Williams209dc4d2015-12-09 15:50:27 -08001043 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001044 return;
1045
Mitch Williams63e18c22015-03-27 00:12:10 -07001046 netif_carrier_off(netdev);
1047 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001048 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001049 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001050 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001051
Jacob Keller504398f2017-10-27 11:06:50 -04001052 spin_lock_bh(&adapter->mac_vlan_list_lock);
1053
Jacob Keller8946b562018-01-22 12:00:38 -05001054 /* clear the sync flag on all filters */
1055 __dev_uc_unsync(adapter->netdev, NULL);
1056 __dev_mc_unsync(adapter->netdev, NULL);
1057
Mitch Williamsef8693e2014-02-13 03:48:53 -08001058 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001059 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1060 f->remove = true;
1061 }
Jacob Keller8946b562018-01-22 12:00:38 -05001062
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001063 /* remove all VLAN filters */
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05001064 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001065 vlf->remove = true;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001066 }
Jacob Keller504398f2017-10-27 11:06:50 -04001067
1068 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1069
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001070 /* remove all cloud filters */
1071 spin_lock_bh(&adapter->cloud_filter_list_lock);
1072 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1073 cf->del = true;
1074 }
1075 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1076
Mitch Williamsef8693e2014-02-13 03:48:53 -08001077 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1078 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001079 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001080 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001081 /* Schedule operations to close down the HW. Don't wait
1082 * here for this to complete. The watchdog is still running
1083 * and it will take care of this.
1084 */
1085 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001086 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001087 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001088 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001089 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001090
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001091 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001092}
1093
1094/**
1095 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1096 * @adapter: board private structure
1097 * @vectors: number of vectors to request
1098 *
1099 * Work with the OS to set up the MSIX vectors needed.
1100 *
1101 * Returns 0 on success, negative on failure
1102 **/
1103static int
1104i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1105{
1106 int err, vector_threshold;
1107
1108 /* We'll want at least 3 (vector_threshold):
1109 * 0) Other (Admin Queue and link, mostly)
1110 * 1) TxQ[0] Cleanup
1111 * 2) RxQ[0] Cleanup
1112 */
1113 vector_threshold = MIN_MSIX_COUNT;
1114
1115 /* The more we get, the more we will assign to Tx/Rx Cleanup
1116 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1117 * Right now, we simply care about how many we'll get; we'll
1118 * set them up later while requesting irq's.
1119 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001120 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1121 vector_threshold, vectors);
1122 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001123 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001124 kfree(adapter->msix_entries);
1125 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001126 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001127 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001128
1129 /* Adjust for only the vectors we'll use, which is minimum
1130 * of max_msix_q_vectors + NONQ_VECS, or the number of
1131 * vectors we were allocated.
1132 */
1133 adapter->num_msix_vectors = err;
1134 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001135}
1136
1137/**
1138 * i40evf_free_queues - Free memory for all rings
1139 * @adapter: board private structure to initialize
1140 *
1141 * Free all of the memory associated with queue pairs.
1142 **/
1143static void i40evf_free_queues(struct i40evf_adapter *adapter)
1144{
Greg Rose5eae00c2013-12-21 06:12:45 +00001145 if (!adapter->vsi_res)
1146 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001147 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001148 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001149 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001150 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001151 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001152}
1153
1154/**
1155 * i40evf_alloc_queues - Allocate memory for all rings
1156 * @adapter: board private structure to initialize
1157 *
1158 * We allocate one ring per queue at run-time since we don't know the
1159 * number of queues at compile-time. The polling_netdev array is
1160 * intended for Multiqueue, but should work fine with a single queue.
1161 **/
1162static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1163{
Jacob Keller65c70062017-06-07 05:43:01 -04001164 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001165
Alan Brady5b36e8d2017-08-22 06:57:50 -04001166 /* If we're in reset reallocating queues we don't actually know yet for
1167 * certain the PF gave us the number of queues we asked for but we'll
1168 * assume it did. Once basic reset is finished we'll confirm once we
1169 * start negotiating config with PF.
1170 */
1171 if (adapter->num_req_queues)
1172 num_active_queues = adapter->num_req_queues;
Avinash Dayanand5e97ce62018-01-23 08:50:59 -08001173 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1174 adapter->num_tc)
1175 num_active_queues = adapter->ch_config.total_qps;
Alan Brady5b36e8d2017-08-22 06:57:50 -04001176 else
1177 num_active_queues = min_t(int,
1178 adapter->vsi_res->num_queue_pairs,
1179 (int)(num_online_cpus()));
1180
Jacob Keller65c70062017-06-07 05:43:01 -04001181
1182 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001183 sizeof(struct i40e_ring), GFP_KERNEL);
1184 if (!adapter->tx_rings)
1185 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001186 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001187 sizeof(struct i40e_ring), GFP_KERNEL);
1188 if (!adapter->rx_rings)
1189 goto err_out;
1190
Jacob Keller65c70062017-06-07 05:43:01 -04001191 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001192 struct i40e_ring *tx_ring;
1193 struct i40e_ring *rx_ring;
1194
Mitch Williams0dd438d2015-10-26 19:44:40 -04001195 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001196
1197 tx_ring->queue_index = i;
1198 tx_ring->netdev = adapter->netdev;
1199 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001200 tx_ring->count = adapter->tx_desc_count;
Alexander Duyck40588ca2017-12-29 08:49:28 -05001201 tx_ring->itr_setting = I40E_ITR_TX_DEF;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001202 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001203 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001204
Mitch Williams0dd438d2015-10-26 19:44:40 -04001205 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001206 rx_ring->queue_index = i;
1207 rx_ring->netdev = adapter->netdev;
1208 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001209 rx_ring->count = adapter->rx_desc_count;
Alexander Duyck40588ca2017-12-29 08:49:28 -05001210 rx_ring->itr_setting = I40E_ITR_RX_DEF;
Greg Rose5eae00c2013-12-21 06:12:45 +00001211 }
1212
Jacob Keller65c70062017-06-07 05:43:01 -04001213 adapter->num_active_queues = num_active_queues;
1214
Greg Rose5eae00c2013-12-21 06:12:45 +00001215 return 0;
1216
1217err_out:
1218 i40evf_free_queues(adapter);
1219 return -ENOMEM;
1220}
1221
1222/**
1223 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1224 * @adapter: board private structure to initialize
1225 *
1226 * Attempt to configure the interrupts using the best available
1227 * capabilities of the hardware and the kernel.
1228 **/
1229static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1230{
1231 int vector, v_budget;
1232 int pairs = 0;
1233 int err = 0;
1234
1235 if (!adapter->vsi_res) {
1236 err = -EIO;
1237 goto out;
1238 }
Mitch Williamscc052922014-10-25 03:24:34 +00001239 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001240
Jacob Keller789f38c2017-04-19 09:25:56 -04001241 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1242 * us much good if we have more vectors than CPUs. However, we already
1243 * limit the total number of queues by the number of CPUs so we do not
1244 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001245 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001246 v_budget = min_t(int, pairs + NONQ_VECS,
1247 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001248
Greg Rose5eae00c2013-12-21 06:12:45 +00001249 adapter->msix_entries = kcalloc(v_budget,
1250 sizeof(struct msix_entry), GFP_KERNEL);
1251 if (!adapter->msix_entries) {
1252 err = -ENOMEM;
1253 goto out;
1254 }
1255
1256 for (vector = 0; vector < v_budget; vector++)
1257 adapter->msix_entries[vector].entry = vector;
1258
Mitch Williams313ed2d2015-08-27 11:42:31 -04001259 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001260
1261out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001262 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1263 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001264 return err;
1265}
1266
1267/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001268 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1269 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001270 *
1271 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001272 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001273static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001274{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001275 struct i40e_aqc_get_set_rss_key_data *rss_key =
1276 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001277 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001278 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001279
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001280 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001281 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001282 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001283 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001284 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001285 }
1286
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001287 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1288 if (ret) {
1289 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1290 i40evf_stat_str(hw, ret),
1291 i40evf_aq_str(hw, hw->aq.asq_last_status));
1292 return ret;
1293
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001294 }
1295
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001296 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1297 adapter->rss_lut, adapter->rss_lut_size);
1298 if (ret) {
1299 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1300 i40evf_stat_str(hw, ret),
1301 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001302 }
1303
1304 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001305
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001306}
1307
1308/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001309 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001311 *
1312 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001313 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001314static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001315{
1316 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001317 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001318 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001319
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001320 dw = (u32 *)adapter->rss_key;
1321 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1322 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001323
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001324 dw = (u32 *)adapter->rss_lut;
1325 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1326 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001327
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001328 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001329
1330 return 0;
1331}
1332
1333/**
1334 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001335 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001336 *
1337 * Returns 0 on success, negative on failure
1338 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001339int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001340{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001341
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001342 if (RSS_PF(adapter)) {
1343 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1344 I40EVF_FLAG_AQ_SET_RSS_KEY;
1345 return 0;
1346 } else if (RSS_AQ(adapter)) {
1347 return i40evf_config_rss_aq(adapter);
1348 } else {
1349 return i40evf_config_rss_reg(adapter);
1350 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001351}
1352
1353/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001354 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001355 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001356 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001357static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001358{
1359 u16 i;
1360
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001361 for (i = 0; i < adapter->rss_lut_size; i++)
1362 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001363}
1364
1365/**
Helin Zhang96a81982015-10-26 19:44:31 -04001366 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001367 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001368 *
1369 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001370 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001371static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001372{
1373 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001374 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001375
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001376 if (!RSS_PF(adapter)) {
1377 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001378 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001379 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001380 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1381 else
1382 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001383
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001384 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1385 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1386 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001387
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001388 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001389
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001390 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1391 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001392
1393 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001394}
1395
1396/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001397 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1398 * @adapter: board private structure to initialize
1399 *
1400 * We allocate one q_vector per queue interrupt. If allocation fails we
1401 * return -ENOMEM.
1402 **/
1403static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1404{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001405 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001406 struct i40e_q_vector *q_vector;
1407
1408 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001409 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001410 GFP_KERNEL);
1411 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001412 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001413
1414 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001415 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001416 q_vector->adapter = adapter;
1417 q_vector->vsi = &adapter->vsi;
1418 q_vector->v_idx = q_idx;
Alexander Duycka3f9fb52017-12-29 08:48:53 -05001419 q_vector->reg_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001420 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001421 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001422 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001423 }
1424
1425 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001426}
1427
1428/**
1429 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1430 * @adapter: board private structure to initialize
1431 *
1432 * This function frees the memory allocated to the q_vectors. In addition if
1433 * NAPI is enabled it will delete any references to the NAPI struct prior
1434 * to freeing the q_vector.
1435 **/
1436static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1437{
1438 int q_idx, num_q_vectors;
1439 int napi_vectors;
1440
Jacob Kelleref4603e2016-11-08 13:05:08 -08001441 if (!adapter->q_vectors)
1442 return;
1443
Greg Rose5eae00c2013-12-21 06:12:45 +00001444 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001445 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001446
1447 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001448 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001449 if (q_idx < napi_vectors)
1450 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001451 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001452 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001453 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001454}
1455
1456/**
1457 * i40evf_reset_interrupt_capability - Reset MSIX setup
1458 * @adapter: board private structure
1459 *
1460 **/
1461void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1462{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001463 if (!adapter->msix_entries)
1464 return;
1465
Greg Rose5eae00c2013-12-21 06:12:45 +00001466 pci_disable_msix(adapter->pdev);
1467 kfree(adapter->msix_entries);
1468 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001469}
1470
1471/**
1472 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1473 * @adapter: board private structure to initialize
1474 *
1475 **/
1476int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1477{
1478 int err;
1479
Jacob Keller283aeaf2017-04-19 09:25:59 -04001480 err = i40evf_alloc_queues(adapter);
1481 if (err) {
1482 dev_err(&adapter->pdev->dev,
1483 "Unable to allocate memory for queues\n");
1484 goto err_alloc_queues;
1485 }
1486
Jacob Keller62fe2a82016-07-27 12:02:33 -07001487 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001488 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001489 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001490 if (err) {
1491 dev_err(&adapter->pdev->dev,
1492 "Unable to setup interrupt capabilities\n");
1493 goto err_set_interrupt;
1494 }
1495
1496 err = i40evf_alloc_q_vectors(adapter);
1497 if (err) {
1498 dev_err(&adapter->pdev->dev,
1499 "Unable to allocate memory for queue vectors\n");
1500 goto err_alloc_q_vectors;
1501 }
1502
Avinash Dayanand5e97ce62018-01-23 08:50:59 -08001503 /* If we've made it so far while ADq flag being ON, then we haven't
1504 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1505 * resources have been allocated in the reset path.
1506 * Now we can truly claim that ADq is enabled.
1507 */
1508 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1509 adapter->num_tc)
1510 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1511 adapter->num_tc);
1512
Greg Rose5eae00c2013-12-21 06:12:45 +00001513 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001514 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1515 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001516
1517 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001518err_alloc_q_vectors:
1519 i40evf_reset_interrupt_capability(adapter);
1520err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001521 i40evf_free_queues(adapter);
1522err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001523 return err;
1524}
1525
1526/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001527 * i40evf_free_rss - Free memory used by RSS structs
1528 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001529 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001530static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001531{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001532 kfree(adapter->rss_key);
1533 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001534
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001535 kfree(adapter->rss_lut);
1536 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001537}
1538
1539/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001540 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1541 * @adapter: board private structure
1542 *
1543 * Returns 0 on success, negative on failure
1544 **/
1545static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1546{
1547 struct net_device *netdev = adapter->netdev;
1548 int err;
1549
1550 if (netif_running(netdev))
1551 i40evf_free_traffic_irqs(adapter);
1552 i40evf_free_misc_irq(adapter);
1553 i40evf_reset_interrupt_capability(adapter);
1554 i40evf_free_q_vectors(adapter);
1555 i40evf_free_queues(adapter);
1556
1557 err = i40evf_init_interrupt_scheme(adapter);
1558 if (err)
1559 goto err;
1560
1561 netif_tx_stop_all_queues(netdev);
1562
1563 err = i40evf_request_misc_irq(adapter);
1564 if (err)
1565 goto err;
1566
1567 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1568
Mitch Williams1b7b7592017-08-22 06:57:51 -04001569 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001570
1571 if (RSS_AQ(adapter))
1572 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1573 else
1574 err = i40evf_init_rss(adapter);
1575err:
1576 return err;
1577}
1578
1579/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001580 * i40evf_watchdog_timer - Periodic call-back timer
1581 * @data: pointer to adapter disguised as unsigned long
1582 **/
Kees Cook26566ea2017-10-16 17:29:35 -07001583static void i40evf_watchdog_timer(struct timer_list *t)
Greg Rose5eae00c2013-12-21 06:12:45 +00001584{
Kees Cook26566ea2017-10-16 17:29:35 -07001585 struct i40evf_adapter *adapter = from_timer(adapter, t,
1586 watchdog_timer);
Mitch Williams75a64432014-11-11 20:02:42 +00001587
Greg Rose5eae00c2013-12-21 06:12:45 +00001588 schedule_work(&adapter->watchdog_task);
1589 /* timer will be rescheduled in watchdog task */
1590}
1591
1592/**
1593 * i40evf_watchdog_task - Periodic call-back task
1594 * @work: pointer to work_struct
1595 **/
1596static void i40evf_watchdog_task(struct work_struct *work)
1597{
1598 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001599 struct i40evf_adapter,
1600 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001601 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001602 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001603
Greg Rose5eae00c2013-12-21 06:12:45 +00001604 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001605 goto restart_watchdog;
1606
1607 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001608 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1609 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001610 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1611 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001612 /* A chance for redemption! */
1613 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1614 adapter->state = __I40EVF_STARTUP;
1615 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1616 schedule_delayed_work(&adapter->init_task, 10);
1617 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1618 &adapter->crit_section);
1619 /* Don't reschedule the watchdog, since we've restarted
1620 * the init task. When init_task contacts the PF and
1621 * gets everything set up again, it'll restart the
1622 * watchdog for us. Down, boy. Sit. Stay. Woof.
1623 */
1624 return;
1625 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001626 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001627 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001628 goto watchdog_done;
1629 }
1630
1631 if ((adapter->state < __I40EVF_DOWN) ||
1632 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001633 goto watchdog_done;
1634
Mitch Williamsef8693e2014-02-13 03:48:53 -08001635 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001636 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1637 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001638 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001639 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001640 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001641 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001642 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001643 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001644 goto watchdog_done;
1645 }
1646
1647 /* Process admin queue tasks. After init, everything gets done
1648 * here so we don't race on the admin queue.
1649 */
Mitch Williamsed636962015-04-07 19:45:32 -04001650 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001651 if (!i40evf_asq_done(hw)) {
1652 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1653 i40evf_send_api_ver(adapter);
1654 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001655 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001656 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001657 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1658 i40evf_send_vf_config_msg(adapter);
1659 goto watchdog_done;
1660 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001661
Mitch Williamse284fc82015-03-27 00:12:09 -07001662 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1663 i40evf_disable_queues(adapter);
1664 goto watchdog_done;
1665 }
1666
Greg Rose5eae00c2013-12-21 06:12:45 +00001667 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1668 i40evf_map_queues(adapter);
1669 goto watchdog_done;
1670 }
1671
1672 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1673 i40evf_add_ether_addrs(adapter);
1674 goto watchdog_done;
1675 }
1676
1677 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1678 i40evf_add_vlans(adapter);
1679 goto watchdog_done;
1680 }
1681
1682 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1683 i40evf_del_ether_addrs(adapter);
1684 goto watchdog_done;
1685 }
1686
1687 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1688 i40evf_del_vlans(adapter);
1689 goto watchdog_done;
1690 }
1691
Mariusz Stachura87743702017-07-17 22:09:45 -07001692 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1693 i40evf_enable_vlan_stripping(adapter);
1694 goto watchdog_done;
1695 }
1696
1697 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1698 i40evf_disable_vlan_stripping(adapter);
1699 goto watchdog_done;
1700 }
1701
Greg Rose5eae00c2013-12-21 06:12:45 +00001702 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1703 i40evf_configure_queues(adapter);
1704 goto watchdog_done;
1705 }
1706
1707 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1708 i40evf_enable_queues(adapter);
1709 goto watchdog_done;
1710 }
1711
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001712 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1713 /* This message goes straight to the firmware, not the
1714 * PF, so we don't have to set current_op as we will
1715 * not get a response through the ARQ.
1716 */
Helin Zhang96a81982015-10-26 19:44:31 -04001717 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001718 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1719 goto watchdog_done;
1720 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001721 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1722 i40evf_get_hena(adapter);
1723 goto watchdog_done;
1724 }
1725 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1726 i40evf_set_hena(adapter);
1727 goto watchdog_done;
1728 }
1729 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1730 i40evf_set_rss_key(adapter);
1731 goto watchdog_done;
1732 }
1733 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1734 i40evf_set_rss_lut(adapter);
1735 goto watchdog_done;
1736 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001737
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001738 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001739 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1740 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001741 goto watchdog_done;
1742 }
1743
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001744 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001745 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001746 goto watchdog_done;
1747 }
1748
1749 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1750 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001751 i40evf_set_promiscuous(adapter, 0);
1752 goto watchdog_done;
1753 }
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08001754
1755 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_CHANNELS) {
1756 i40evf_enable_channels(adapter);
1757 goto watchdog_done;
1758 }
1759
1760 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_CHANNELS) {
1761 i40evf_disable_channels(adapter);
1762 goto watchdog_done;
1763 }
1764
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001765 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1766 i40evf_add_cloud_filter(adapter);
1767 goto watchdog_done;
1768 }
1769
1770 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1771 i40evf_del_cloud_filter(adapter);
1772 goto watchdog_done;
1773 }
1774
Mitch Williamsed0e8942017-01-24 10:23:59 -08001775 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001776
Greg Rose5eae00c2013-12-21 06:12:45 +00001777 if (adapter->state == __I40EVF_RUNNING)
1778 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001779watchdog_done:
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05001780 if (adapter->state == __I40EVF_RUNNING)
1781 i40evf_detect_recover_hung(&adapter->vsi);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001782 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1783restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001784 if (adapter->state == __I40EVF_REMOVE)
1785 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001786 if (adapter->aq_required)
1787 mod_timer(&adapter->watchdog_timer,
1788 jiffies + msecs_to_jiffies(20));
1789 else
1790 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001791 schedule_work(&adapter->adminq_task);
1792}
1793
Joe Perchesdedecb62016-11-01 15:35:14 -07001794static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1795{
1796 struct i40evf_mac_filter *f, *ftmp;
1797 struct i40evf_vlan_filter *fv, *fvtmp;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001798 struct i40evf_cloud_filter *cf, *cftmp;
Joe Perchesdedecb62016-11-01 15:35:14 -07001799
1800 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1801
Jacob Keller44b034b2017-10-27 11:06:49 -04001802 /* We don't use netif_running() because it may be true prior to
1803 * ndo_open() returning, so we can't assume it means all our open
1804 * tasks have finished, since we're not holding the rtnl_lock here.
1805 */
1806 if (adapter->state == __I40EVF_RUNNING) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001807 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001808 netif_carrier_off(adapter->netdev);
1809 netif_tx_disable(adapter->netdev);
1810 adapter->link_up = false;
1811 i40evf_napi_disable_all(adapter);
1812 i40evf_irq_disable(adapter);
1813 i40evf_free_traffic_irqs(adapter);
1814 i40evf_free_all_tx_resources(adapter);
1815 i40evf_free_all_rx_resources(adapter);
1816 }
1817
Jacob Keller504398f2017-10-27 11:06:50 -04001818 spin_lock_bh(&adapter->mac_vlan_list_lock);
1819
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001820 /* Delete all of the filters */
Joe Perchesdedecb62016-11-01 15:35:14 -07001821 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1822 list_del(&f->list);
1823 kfree(f);
1824 }
1825
1826 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1827 list_del(&fv->list);
1828 kfree(fv);
1829 }
1830
Jacob Keller504398f2017-10-27 11:06:50 -04001831 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1832
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001833 spin_lock_bh(&adapter->cloud_filter_list_lock);
1834 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1835 list_del(&cf->list);
1836 kfree(cf);
1837 adapter->num_cloud_filters--;
1838 }
1839 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1840
Joe Perchesdedecb62016-11-01 15:35:14 -07001841 i40evf_free_misc_irq(adapter);
1842 i40evf_reset_interrupt_capability(adapter);
1843 i40evf_free_queues(adapter);
1844 i40evf_free_q_vectors(adapter);
1845 kfree(adapter->vf_res);
1846 i40evf_shutdown_adminq(&adapter->hw);
1847 adapter->netdev->flags &= ~IFF_UP;
1848 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1849 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1850 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001851 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001852 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1853}
1854
Mitch Williams67c818a2015-06-19 08:56:30 -07001855#define I40EVF_RESET_WAIT_MS 10
1856#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001857/**
1858 * i40evf_reset_task - Call-back task to handle hardware reset
1859 * @work: pointer to work_struct
1860 *
1861 * During reset we need to shut down and reinitialize the admin queue
1862 * before we can use it to communicate with the PF again. We also clear
1863 * and reinit the rings because that context is lost as well.
1864 **/
1865static void i40evf_reset_task(struct work_struct *work)
1866{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001867 struct i40evf_adapter *adapter = container_of(work,
1868 struct i40evf_adapter,
1869 reset_task);
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001870 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001871 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001872 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001873 struct i40evf_vlan_filter *vlf;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001874 struct i40evf_cloud_filter *cf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001875 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001876 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001877 int i = 0, err;
Jacob Keller44b034b2017-10-27 11:06:49 -04001878 bool running;
Greg Rose5eae00c2013-12-21 06:12:45 +00001879
Avinash Dayanand06aa0402017-12-18 05:16:43 -05001880 /* When device is being removed it doesn't make sense to run the reset
1881 * task, just return in such a case.
1882 */
1883 if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1884 return;
1885
Mitch Williamsed0e8942017-01-24 10:23:59 -08001886 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001887 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001888 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001889 if (CLIENT_ENABLED(adapter)) {
1890 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1891 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1892 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1893 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1894 cancel_delayed_work_sync(&adapter->client_task);
1895 i40evf_notify_client_close(&adapter->vsi, true);
1896 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001897 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001898 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001899 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1900 /* Restart the AQ here. If we have been reset but didn't
1901 * detect it, or if the PF had to reinit, our AQ will be hosed.
1902 */
1903 i40evf_shutdown_adminq(hw);
1904 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001905 i40evf_request_reset(adapter);
1906 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001907 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001908
Mitch Williamsef8693e2014-02-13 03:48:53 -08001909 /* poll until we see the reset actually happen */
1910 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001911 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1912 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1913 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001914 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001915 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001916 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001917 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001918 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001919 goto continue_reset; /* act like the reset happened */
1920 }
1921
1922 /* wait until the reset is complete and the PF is responding to us */
1923 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001924 /* sleep first to make sure a minimum wait time is met */
1925 msleep(I40EVF_RESET_WAIT_MS);
1926
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001927 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1928 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001929 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001930 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001931 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001932
Mitch Williams509a4472015-12-23 12:05:52 -08001933 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001934
Mitch Williamsef8693e2014-02-13 03:48:53 -08001935 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001936 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001937 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001938 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001939 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001940 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001941 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001942
1943continue_reset:
Jacob Keller44b034b2017-10-27 11:06:49 -04001944 /* We don't use netif_running() because it may be true prior to
1945 * ndo_open() returning, so we can't assume it means all our open
1946 * tasks have finished, since we're not holding the rtnl_lock here.
1947 */
1948 running = (adapter->state == __I40EVF_RUNNING);
1949
1950 if (running) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001951 netif_carrier_off(netdev);
1952 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001953 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001954 i40evf_napi_disable_all(adapter);
1955 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001956 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001957
Greg Rose5eae00c2013-12-21 06:12:45 +00001958 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001959 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1960
1961 /* free the Tx/Rx rings and descriptors, might be better to just
1962 * re-use them sometime in the future
1963 */
1964 i40evf_free_all_rx_resources(adapter);
1965 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001966
Avinash Dayanand836ce5e2018-01-23 08:50:55 -08001967 adapter->flags |= I40EVF_FLAG_QUEUES_DISABLED;
Greg Rose5eae00c2013-12-21 06:12:45 +00001968 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001969 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001970 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001971 err = i40evf_init_adminq(hw);
1972 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001973 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1974 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001975 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001976
Alan Brady5b36e8d2017-08-22 06:57:50 -04001977 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1978 err = i40evf_reinit_interrupt_scheme(adapter);
1979 if (err)
1980 goto reset_err;
1981 }
1982
1983 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001984 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001985
Jacob Keller504398f2017-10-27 11:06:50 -04001986 spin_lock_bh(&adapter->mac_vlan_list_lock);
1987
Mitch Williamsac833bb2015-01-29 07:17:19 +00001988 /* re-add all MAC filters */
1989 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1990 f->add = true;
1991 }
1992 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001993 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1994 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001995 }
Jacob Keller504398f2017-10-27 11:06:50 -04001996
1997 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1998
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08001999 /* check if TCs are running and re-add all cloud filters */
2000 spin_lock_bh(&adapter->cloud_filter_list_lock);
2001 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
2002 adapter->num_tc) {
2003 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2004 cf->add = true;
2005 }
2006 }
2007 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2008
Mitch Williamse6d038d2015-06-04 16:23:58 -04002009 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00002010 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002011 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
Mitch Williams67c818a2015-06-19 08:56:30 -07002012 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002013
2014 mod_timer(&adapter->watchdog_timer, jiffies + 2);
2015
Jacob Keller44b034b2017-10-27 11:06:49 -04002016 /* We were running when the reset started, so we need to restore some
2017 * state here.
2018 */
2019 if (running) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002020 /* allocate transmit descriptors */
2021 err = i40evf_setup_all_tx_resources(adapter);
2022 if (err)
2023 goto reset_err;
2024
2025 /* allocate receive descriptors */
2026 err = i40evf_setup_all_rx_resources(adapter);
2027 if (err)
2028 goto reset_err;
2029
Alan Brady5b36e8d2017-08-22 06:57:50 -04002030 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
2031 err = i40evf_request_traffic_irqs(adapter,
2032 netdev->name);
2033 if (err)
2034 goto reset_err;
2035
2036 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
2037 }
2038
Greg Rose5eae00c2013-12-21 06:12:45 +00002039 i40evf_configure(adapter);
2040
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002041 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002042
2043 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07002044 } else {
2045 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002046 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00002047 }
Jacob Keller9b2aef12017-10-27 11:06:52 -04002048 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2049 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07002050
Greg Rose5eae00c2013-12-21 06:12:45 +00002051 return;
2052reset_err:
Jacob Keller9b2aef12017-10-27 11:06:52 -04002053 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2054 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams80e72892014-05-10 04:49:06 +00002055 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04002056 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002057}
2058
2059/**
2060 * i40evf_adminq_task - worker thread to clean the admin queue
2061 * @work: pointer to work_struct containing our data
2062 **/
2063static void i40evf_adminq_task(struct work_struct *work)
2064{
2065 struct i40evf_adapter *adapter =
2066 container_of(work, struct i40evf_adapter, adminq_task);
2067 struct i40e_hw *hw = &adapter->hw;
2068 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07002069 enum virtchnl_ops v_op;
2070 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00002071 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00002072 u16 pending;
2073
Mitch Williamsef8693e2014-02-13 03:48:53 -08002074 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00002075 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08002076
Mitch Williams1001dc32014-11-11 20:02:19 +00002077 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2078 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00002079 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00002080 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00002081
Greg Rose5eae00c2013-12-21 06:12:45 +00002082 do {
2083 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002084 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2085 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2086
2087 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002088 break; /* No event to process or error cleaning ARQ */
2089
Tushar Davec969ef42017-06-22 09:44:32 -07002090 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002091 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002092 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002093 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002094 } while (pending);
2095
Mitch Williams67c818a2015-06-19 08:56:30 -07002096 if ((adapter->flags &
2097 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2098 adapter->state == __I40EVF_RESETTING)
2099 goto freedom;
2100
Mitch Williams912257e2014-05-22 06:32:07 +00002101 /* check for error indications */
2102 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002103 if (val == 0xdeadbeef) /* indicates device in reset */
2104 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002105 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002106 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002107 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002108 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002109 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002110 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002111 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002112 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002113 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002114 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002115 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002116 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002117 }
2118 if (oldval != val)
2119 wr32(hw, hw->aq.arq.len, val);
2120
2121 val = rd32(hw, hw->aq.asq.len);
2122 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002123 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002124 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002125 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002126 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002127 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002128 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002129 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002130 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002131 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002132 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002133 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002134 }
2135 if (oldval != val)
2136 wr32(hw, hw->aq.asq.len, val);
2137
Mitch Williams67c818a2015-06-19 08:56:30 -07002138freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002139 kfree(event.msg_buf);
2140out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002141 /* re-enable Admin queue interrupt cause */
2142 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002143}
2144
2145/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002146 * i40evf_client_task - worker thread to perform client work
2147 * @work: pointer to work_struct containing our data
2148 *
2149 * This task handles client interactions. Because client calls can be
2150 * reentrant, we can't handle them in the watchdog.
2151 **/
2152static void i40evf_client_task(struct work_struct *work)
2153{
2154 struct i40evf_adapter *adapter =
2155 container_of(work, struct i40evf_adapter, client_task.work);
2156
2157 /* If we can't get the client bit, just give up. We'll be rescheduled
2158 * later.
2159 */
2160
2161 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2162 return;
2163
2164 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2165 i40evf_client_subtask(adapter);
2166 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2167 goto out;
2168 }
Alan Brady01acc732017-11-14 07:00:51 -05002169 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2170 i40evf_notify_client_l2_params(&adapter->vsi);
2171 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2172 goto out;
2173 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002174 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2175 i40evf_notify_client_close(&adapter->vsi, false);
2176 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2177 goto out;
2178 }
2179 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2180 i40evf_notify_client_open(&adapter->vsi);
2181 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002182 }
2183out:
2184 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2185}
2186
2187/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002188 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2189 * @adapter: board private structure
2190 *
2191 * Free all transmit software resources
2192 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002193void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002194{
2195 int i;
2196
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002197 if (!adapter->tx_rings)
2198 return;
2199
Mitch Williamscc052922014-10-25 03:24:34 +00002200 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002201 if (adapter->tx_rings[i].desc)
2202 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002203}
2204
2205/**
2206 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2207 * @adapter: board private structure
2208 *
2209 * If this function returns with an error, then it's possible one or
2210 * more of the rings is populated (while the rest are not). It is the
2211 * callers duty to clean those orphaned rings.
2212 *
2213 * Return 0 on success, negative on failure
2214 **/
2215static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2216{
2217 int i, err = 0;
2218
Mitch Williamscc052922014-10-25 03:24:34 +00002219 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002220 adapter->tx_rings[i].count = adapter->tx_desc_count;
2221 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002222 if (!err)
2223 continue;
2224 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002225 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002226 break;
2227 }
2228
2229 return err;
2230}
2231
2232/**
2233 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2234 * @adapter: board private structure
2235 *
2236 * If this function returns with an error, then it's possible one or
2237 * more of the rings is populated (while the rest are not). It is the
2238 * callers duty to clean those orphaned rings.
2239 *
2240 * Return 0 on success, negative on failure
2241 **/
2242static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2243{
2244 int i, err = 0;
2245
Mitch Williamscc052922014-10-25 03:24:34 +00002246 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002247 adapter->rx_rings[i].count = adapter->rx_desc_count;
2248 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002249 if (!err)
2250 continue;
2251 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002252 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002253 break;
2254 }
2255 return err;
2256}
2257
2258/**
2259 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2260 * @adapter: board private structure
2261 *
2262 * Free all receive software resources
2263 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002264void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002265{
2266 int i;
2267
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002268 if (!adapter->rx_rings)
2269 return;
2270
Mitch Williamscc052922014-10-25 03:24:34 +00002271 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002272 if (adapter->rx_rings[i].desc)
2273 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002274}
2275
2276/**
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002277 * i40evf_validate_tx_bandwidth - validate the max Tx bandwidth
2278 * @adapter: board private structure
2279 * @max_tx_rate: max Tx bw for a tc
2280 **/
2281static int i40evf_validate_tx_bandwidth(struct i40evf_adapter *adapter,
2282 u64 max_tx_rate)
2283{
2284 int speed = 0, ret = 0;
2285
2286 switch (adapter->link_speed) {
2287 case I40E_LINK_SPEED_40GB:
2288 speed = 40000;
2289 break;
2290 case I40E_LINK_SPEED_25GB:
2291 speed = 25000;
2292 break;
2293 case I40E_LINK_SPEED_20GB:
2294 speed = 20000;
2295 break;
2296 case I40E_LINK_SPEED_10GB:
2297 speed = 10000;
2298 break;
2299 case I40E_LINK_SPEED_1GB:
2300 speed = 1000;
2301 break;
2302 case I40E_LINK_SPEED_100MB:
2303 speed = 100;
2304 break;
2305 default:
2306 break;
2307 }
2308
2309 if (max_tx_rate > speed) {
2310 dev_err(&adapter->pdev->dev,
2311 "Invalid tx rate specified\n");
2312 ret = -EINVAL;
2313 }
2314
2315 return ret;
2316}
2317
2318/**
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002319 * i40evf_validate_channel_config - validate queue mapping info
2320 * @adapter: board private structure
2321 * @mqprio_qopt: queue parameters
2322 *
2323 * This function validates if the config provided by the user to
2324 * configure queue channels is valid or not. Returns 0 on a valid
2325 * config.
2326 **/
2327static int i40evf_validate_ch_config(struct i40evf_adapter *adapter,
2328 struct tc_mqprio_qopt_offload *mqprio_qopt)
2329{
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002330 u64 total_max_rate = 0;
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002331 int i, num_qps = 0;
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002332 u64 tx_rate = 0;
2333 int ret = 0;
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002334
2335 if (mqprio_qopt->qopt.num_tc > I40EVF_MAX_TRAFFIC_CLASS ||
2336 mqprio_qopt->qopt.num_tc < 1)
2337 return -EINVAL;
2338
2339 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2340 if (!mqprio_qopt->qopt.count[i] ||
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002341 mqprio_qopt->qopt.offset[i] != num_qps)
2342 return -EINVAL;
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002343 if (mqprio_qopt->min_rate[i]) {
2344 dev_err(&adapter->pdev->dev,
2345 "Invalid min tx rate (greater than 0) specified\n");
2346 return -EINVAL;
2347 }
2348 /*convert to Mbps */
2349 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2350 I40EVF_MBPS_DIVISOR);
2351 total_max_rate += tx_rate;
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002352 num_qps += mqprio_qopt->qopt.count[i];
2353 }
2354 if (num_qps > MAX_QUEUES)
2355 return -EINVAL;
2356
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002357 ret = i40evf_validate_tx_bandwidth(adapter, total_max_rate);
2358 return ret;
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002359}
2360
2361/**
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002362 * i40evf_del_all_cloud_filters - delete all cloud filters
2363 * on the traffic classes
2364 **/
2365static void i40evf_del_all_cloud_filters(struct i40evf_adapter *adapter)
2366{
2367 struct i40evf_cloud_filter *cf, *cftmp;
2368
2369 spin_lock_bh(&adapter->cloud_filter_list_lock);
2370 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2371 list) {
2372 list_del(&cf->list);
2373 kfree(cf);
2374 adapter->num_cloud_filters--;
2375 }
2376 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2377}
2378
2379/**
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002380 * __i40evf_setup_tc - configure multiple traffic classes
2381 * @netdev: network interface device structure
2382 * @type_date: tc offload data
2383 *
2384 * This function processes the config information provided by the
2385 * user to configure traffic classes/queue channels and packages the
2386 * information to request the PF to setup traffic classes.
2387 *
2388 * Returns 0 on success.
2389 **/
2390static int __i40evf_setup_tc(struct net_device *netdev, void *type_data)
2391{
2392 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2393 struct i40evf_adapter *adapter = netdev_priv(netdev);
2394 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2395 u8 num_tc = 0, total_qps = 0;
2396 int ret = 0, netdev_tc = 0;
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002397 u64 max_tx_rate;
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002398 u16 mode;
2399 int i;
2400
2401 num_tc = mqprio_qopt->qopt.num_tc;
2402 mode = mqprio_qopt->mode;
2403
2404 /* delete queue_channel */
2405 if (!mqprio_qopt->qopt.hw) {
2406 if (adapter->ch_config.state == __I40EVF_TC_RUNNING) {
2407 /* reset the tc configuration */
2408 netdev_reset_tc(netdev);
2409 adapter->num_tc = 0;
2410 netif_tx_stop_all_queues(netdev);
2411 netif_tx_disable(netdev);
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002412 i40evf_del_all_cloud_filters(adapter);
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002413 adapter->aq_required = I40EVF_FLAG_AQ_DISABLE_CHANNELS;
2414 goto exit;
2415 } else {
2416 return -EINVAL;
2417 }
2418 }
2419
2420 /* add queue channel */
2421 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2422 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2423 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2424 return -EOPNOTSUPP;
2425 }
2426 if (adapter->ch_config.state != __I40EVF_TC_INVALID) {
2427 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2428 return -EINVAL;
2429 }
2430
2431 ret = i40evf_validate_ch_config(adapter, mqprio_qopt);
2432 if (ret)
2433 return ret;
2434 /* Return if same TC config is requested */
2435 if (adapter->num_tc == num_tc)
2436 return 0;
2437 adapter->num_tc = num_tc;
2438
2439 for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2440 if (i < num_tc) {
2441 adapter->ch_config.ch_info[i].count =
2442 mqprio_qopt->qopt.count[i];
2443 adapter->ch_config.ch_info[i].offset =
2444 mqprio_qopt->qopt.offset[i];
2445 total_qps += mqprio_qopt->qopt.count[i];
Harshitha Ramamurthy591532d2018-01-23 08:51:01 -08002446 max_tx_rate = mqprio_qopt->max_rate[i];
2447 /* convert to Mbps */
2448 max_tx_rate = div_u64(max_tx_rate,
2449 I40EVF_MBPS_DIVISOR);
2450 adapter->ch_config.ch_info[i].max_tx_rate =
2451 max_tx_rate;
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002452 } else {
2453 adapter->ch_config.ch_info[i].count = 1;
2454 adapter->ch_config.ch_info[i].offset = 0;
2455 }
2456 }
2457 adapter->ch_config.total_qps = total_qps;
2458 netif_tx_stop_all_queues(netdev);
2459 netif_tx_disable(netdev);
2460 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_CHANNELS;
2461 netdev_reset_tc(netdev);
2462 /* Report the tc mapping up the stack */
2463 netdev_set_num_tc(adapter->netdev, num_tc);
2464 for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2465 u16 qcount = mqprio_qopt->qopt.count[i];
2466 u16 qoffset = mqprio_qopt->qopt.offset[i];
2467
2468 if (i < num_tc)
2469 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2470 qoffset);
2471 }
2472 }
2473exit:
2474 return ret;
2475}
2476
2477/**
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002478 * i40evf_parse_cls_flower - Parse tc flower filters provided by kernel
2479 * @adapter: board private structure
2480 * @cls_flower: pointer to struct tc_cls_flower_offload
2481 * @filter: pointer to cloud filter structure
2482 */
2483static int i40evf_parse_cls_flower(struct i40evf_adapter *adapter,
2484 struct tc_cls_flower_offload *f,
2485 struct i40evf_cloud_filter *filter)
2486{
2487 u16 n_proto_mask = 0;
2488 u16 n_proto_key = 0;
2489 u8 field_flags = 0;
2490 u16 addr_type = 0;
2491 u16 n_proto = 0;
2492 int i = 0;
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002493 struct virtchnl_filter *vf = &filter->f;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002494
2495 if (f->dissector->used_keys &
2496 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2497 BIT(FLOW_DISSECTOR_KEY_BASIC) |
2498 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2499 BIT(FLOW_DISSECTOR_KEY_VLAN) |
2500 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2501 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2502 BIT(FLOW_DISSECTOR_KEY_PORTS) |
2503 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2504 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2505 f->dissector->used_keys);
2506 return -EOPNOTSUPP;
2507 }
2508
2509 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2510 struct flow_dissector_key_keyid *mask =
2511 skb_flow_dissector_target(f->dissector,
2512 FLOW_DISSECTOR_KEY_ENC_KEYID,
2513 f->mask);
2514
2515 if (mask->keyid != 0)
2516 field_flags |= I40EVF_CLOUD_FIELD_TEN_ID;
2517 }
2518
2519 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
2520 struct flow_dissector_key_basic *key =
2521 skb_flow_dissector_target(f->dissector,
2522 FLOW_DISSECTOR_KEY_BASIC,
2523 f->key);
2524
2525 struct flow_dissector_key_basic *mask =
2526 skb_flow_dissector_target(f->dissector,
2527 FLOW_DISSECTOR_KEY_BASIC,
2528 f->mask);
2529 n_proto_key = ntohs(key->n_proto);
2530 n_proto_mask = ntohs(mask->n_proto);
2531
2532 if (n_proto_key == ETH_P_ALL) {
2533 n_proto_key = 0;
2534 n_proto_mask = 0;
2535 }
2536 n_proto = n_proto_key & n_proto_mask;
2537 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2538 return -EINVAL;
2539 if (n_proto == ETH_P_IPV6) {
2540 /* specify flow type as TCP IPv6 */
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002541 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002542 }
2543
2544 if (key->ip_proto != IPPROTO_TCP) {
2545 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2546 return -EINVAL;
2547 }
2548 }
2549
2550 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2551 struct flow_dissector_key_eth_addrs *key =
2552 skb_flow_dissector_target(f->dissector,
2553 FLOW_DISSECTOR_KEY_ETH_ADDRS,
2554 f->key);
2555
2556 struct flow_dissector_key_eth_addrs *mask =
2557 skb_flow_dissector_target(f->dissector,
2558 FLOW_DISSECTOR_KEY_ETH_ADDRS,
2559 f->mask);
2560 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2561 if (!is_zero_ether_addr(mask->dst)) {
2562 if (is_broadcast_ether_addr(mask->dst)) {
2563 field_flags |= I40EVF_CLOUD_FIELD_OMAC;
2564 } else {
2565 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2566 mask->dst);
2567 return I40E_ERR_CONFIG;
2568 }
2569 }
2570
2571 if (!is_zero_ether_addr(mask->src)) {
2572 if (is_broadcast_ether_addr(mask->src)) {
2573 field_flags |= I40EVF_CLOUD_FIELD_IMAC;
2574 } else {
2575 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2576 mask->src);
2577 return I40E_ERR_CONFIG;
2578 }
2579 }
2580
2581 if (!is_zero_ether_addr(key->dst))
2582 if (is_valid_ether_addr(key->dst) ||
2583 is_multicast_ether_addr(key->dst)) {
2584 /* set the mask if a valid dst_mac address */
2585 for (i = 0; i < ETH_ALEN; i++)
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002586 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2587 ether_addr_copy(vf->data.tcp_spec.dst_mac,
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002588 key->dst);
2589 }
2590
2591 if (!is_zero_ether_addr(key->src))
2592 if (is_valid_ether_addr(key->src) ||
2593 is_multicast_ether_addr(key->src)) {
2594 /* set the mask if a valid dst_mac address */
2595 for (i = 0; i < ETH_ALEN; i++)
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002596 vf->mask.tcp_spec.src_mac[i] |= 0xff;
2597 ether_addr_copy(vf->data.tcp_spec.src_mac,
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002598 key->src);
2599 }
2600 }
2601
2602 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
2603 struct flow_dissector_key_vlan *key =
2604 skb_flow_dissector_target(f->dissector,
2605 FLOW_DISSECTOR_KEY_VLAN,
2606 f->key);
2607 struct flow_dissector_key_vlan *mask =
2608 skb_flow_dissector_target(f->dissector,
2609 FLOW_DISSECTOR_KEY_VLAN,
2610 f->mask);
2611
2612 if (mask->vlan_id) {
2613 if (mask->vlan_id == VLAN_VID_MASK) {
2614 field_flags |= I40EVF_CLOUD_FIELD_IVLAN;
2615 } else {
2616 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2617 mask->vlan_id);
2618 return I40E_ERR_CONFIG;
2619 }
2620 }
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002621 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2622 vf->data.tcp_spec.vlan_id = cpu_to_be16(key->vlan_id);
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002623 }
2624
2625 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
2626 struct flow_dissector_key_control *key =
2627 skb_flow_dissector_target(f->dissector,
2628 FLOW_DISSECTOR_KEY_CONTROL,
2629 f->key);
2630
2631 addr_type = key->addr_type;
2632 }
2633
2634 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2635 struct flow_dissector_key_ipv4_addrs *key =
2636 skb_flow_dissector_target(f->dissector,
2637 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2638 f->key);
2639 struct flow_dissector_key_ipv4_addrs *mask =
2640 skb_flow_dissector_target(f->dissector,
2641 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2642 f->mask);
2643
2644 if (mask->dst) {
2645 if (mask->dst == cpu_to_be32(0xffffffff)) {
2646 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2647 } else {
2648 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2649 be32_to_cpu(mask->dst));
2650 return I40E_ERR_CONFIG;
2651 }
2652 }
2653
2654 if (mask->src) {
2655 if (mask->src == cpu_to_be32(0xffffffff)) {
2656 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2657 } else {
2658 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2659 be32_to_cpu(mask->dst));
2660 return I40E_ERR_CONFIG;
2661 }
2662 }
2663
2664 if (field_flags & I40EVF_CLOUD_FIELD_TEN_ID) {
2665 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2666 return I40E_ERR_CONFIG;
2667 }
2668 if (key->dst) {
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002669 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2670 vf->data.tcp_spec.dst_ip[0] = key->dst;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002671 }
2672 if (key->src) {
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002673 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2674 vf->data.tcp_spec.src_ip[0] = key->src;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002675 }
2676 }
2677
2678 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2679 struct flow_dissector_key_ipv6_addrs *key =
2680 skb_flow_dissector_target(f->dissector,
2681 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2682 f->key);
2683 struct flow_dissector_key_ipv6_addrs *mask =
2684 skb_flow_dissector_target(f->dissector,
2685 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2686 f->mask);
2687
2688 /* validate mask, make sure it is not IPV6_ADDR_ANY */
2689 if (ipv6_addr_any(&mask->dst)) {
2690 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2691 IPV6_ADDR_ANY);
2692 return I40E_ERR_CONFIG;
2693 }
2694
2695 /* src and dest IPv6 address should not be LOOPBACK
2696 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2697 */
2698 if (ipv6_addr_loopback(&key->dst) ||
2699 ipv6_addr_loopback(&key->src)) {
2700 dev_err(&adapter->pdev->dev,
2701 "ipv6 addr should not be loopback\n");
2702 return I40E_ERR_CONFIG;
2703 }
2704 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
2705 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2706
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002707 for (i = 0; i < 4; i++)
2708 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2709 memcpy(&vf->data.tcp_spec.dst_ip, &key->dst.s6_addr32,
2710 sizeof(vf->data.tcp_spec.dst_ip));
2711 for (i = 0; i < 4; i++)
2712 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2713 memcpy(&vf->data.tcp_spec.src_ip, &key->src.s6_addr32,
2714 sizeof(vf->data.tcp_spec.src_ip));
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002715 }
2716 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
2717 struct flow_dissector_key_ports *key =
2718 skb_flow_dissector_target(f->dissector,
2719 FLOW_DISSECTOR_KEY_PORTS,
2720 f->key);
2721 struct flow_dissector_key_ports *mask =
2722 skb_flow_dissector_target(f->dissector,
2723 FLOW_DISSECTOR_KEY_PORTS,
2724 f->mask);
2725
2726 if (mask->src) {
2727 if (mask->src == cpu_to_be16(0xffff)) {
2728 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2729 } else {
2730 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2731 be16_to_cpu(mask->src));
2732 return I40E_ERR_CONFIG;
2733 }
2734 }
2735
2736 if (mask->dst) {
2737 if (mask->dst == cpu_to_be16(0xffff)) {
2738 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2739 } else {
2740 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2741 be16_to_cpu(mask->dst));
2742 return I40E_ERR_CONFIG;
2743 }
2744 }
2745 if (key->dst) {
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002746 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2747 vf->data.tcp_spec.dst_port = key->dst;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002748 }
2749
2750 if (key->src) {
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002751 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2752 vf->data.tcp_spec.src_port = key->dst;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002753 }
2754 }
Colin Ian Kingdeb9a9a2018-02-19 10:23:30 +00002755 vf->field_flags = field_flags;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002756
2757 return 0;
2758}
2759
2760/**
2761 * i40evf_handle_tclass - Forward to a traffic class on the device
2762 * @adapter: board private structure
2763 * @tc: traffic class index on the device
2764 * @filter: pointer to cloud filter structure
2765 */
2766static int i40evf_handle_tclass(struct i40evf_adapter *adapter, u32 tc,
2767 struct i40evf_cloud_filter *filter)
2768{
2769 if (tc == 0)
2770 return 0;
2771 if (tc < adapter->num_tc) {
2772 if (!filter->f.data.tcp_spec.dst_port) {
2773 dev_err(&adapter->pdev->dev,
2774 "Specify destination port to redirect to traffic class other than TC0\n");
2775 return -EINVAL;
2776 }
2777 }
2778 /* redirect to a traffic class on the same device */
2779 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2780 filter->f.action_meta = tc;
2781 return 0;
2782}
2783
2784/**
2785 * i40evf_configure_clsflower - Add tc flower filters
2786 * @adapter: board private structure
2787 * @cls_flower: Pointer to struct tc_cls_flower_offload
2788 */
2789static int i40evf_configure_clsflower(struct i40evf_adapter *adapter,
2790 struct tc_cls_flower_offload *cls_flower)
2791{
2792 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2793 struct i40evf_cloud_filter *filter = NULL;
2794 int err = 0, count = 50;
2795
2796 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2797 &adapter->crit_section)) {
2798 udelay(1);
2799 if (--count == 0)
2800 return -EINVAL;
2801 }
2802
2803 if (tc < 0) {
2804 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2805 return -EINVAL;
2806 }
2807
2808 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2809 if (!filter) {
2810 err = -ENOMEM;
2811 goto clearout;
2812 }
2813 filter->cookie = cls_flower->cookie;
2814
2815 /* set the mask to all zeroes to begin with */
2816 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
2817 /* start out with flow type and eth type IPv4 to begin with */
2818 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
2819 err = i40evf_parse_cls_flower(adapter, cls_flower, filter);
2820 if (err < 0)
2821 goto err;
2822
2823 err = i40evf_handle_tclass(adapter, tc, filter);
2824 if (err < 0)
2825 goto err;
2826
2827 /* add filter to the list */
2828 spin_lock_bh(&adapter->cloud_filter_list_lock);
2829 list_add_tail(&filter->list, &adapter->cloud_filter_list);
2830 adapter->num_cloud_filters++;
2831 filter->add = true;
2832 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
2833 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2834err:
2835 if (err)
2836 kfree(filter);
2837clearout:
2838 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2839 return err;
2840}
2841
2842/* i40evf_find_cf - Find the cloud filter in the list
2843 * @adapter: Board private structure
2844 * @cookie: filter specific cookie
2845 *
2846 * Returns ptr to the filter object or NULL. Must be called while holding the
2847 * cloud_filter_list_lock.
2848 */
2849static struct i40evf_cloud_filter *i40evf_find_cf(struct i40evf_adapter *adapter,
2850 unsigned long *cookie)
2851{
2852 struct i40evf_cloud_filter *filter = NULL;
2853
2854 if (!cookie)
2855 return NULL;
2856
2857 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
2858 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
2859 return filter;
2860 }
2861 return NULL;
2862}
2863
2864/**
2865 * i40evf_delete_clsflower - Remove tc flower filters
2866 * @adapter: board private structure
2867 * @cls_flower: Pointer to struct tc_cls_flower_offload
2868 */
2869static int i40evf_delete_clsflower(struct i40evf_adapter *adapter,
2870 struct tc_cls_flower_offload *cls_flower)
2871{
2872 struct i40evf_cloud_filter *filter = NULL;
2873 int err = 0;
2874
2875 spin_lock_bh(&adapter->cloud_filter_list_lock);
2876 filter = i40evf_find_cf(adapter, &cls_flower->cookie);
2877 if (filter) {
2878 filter->del = true;
2879 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
2880 } else {
2881 err = -EINVAL;
2882 }
2883 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2884
2885 return err;
2886}
2887
2888/**
2889 * i40evf_setup_tc_cls_flower - flower classifier offloads
2890 * @netdev: net device to configure
2891 * @type_data: offload data
2892 */
2893static int i40evf_setup_tc_cls_flower(struct i40evf_adapter *adapter,
2894 struct tc_cls_flower_offload *cls_flower)
2895{
2896 if (cls_flower->common.chain_index)
2897 return -EOPNOTSUPP;
2898
2899 switch (cls_flower->command) {
2900 case TC_CLSFLOWER_REPLACE:
2901 return i40evf_configure_clsflower(adapter, cls_flower);
2902 case TC_CLSFLOWER_DESTROY:
2903 return i40evf_delete_clsflower(adapter, cls_flower);
2904 case TC_CLSFLOWER_STATS:
2905 return -EOPNOTSUPP;
2906 default:
2907 return -EINVAL;
2908 }
2909}
2910
2911/**
2912 * i40evf_setup_tc_block_cb - block callback for tc
2913 * @type: type of offload
2914 * @type_data: offload data
2915 * @cb_priv:
2916 *
2917 * This function is the block callback for traffic classes
2918 **/
2919static int i40evf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2920 void *cb_priv)
2921{
2922 switch (type) {
2923 case TC_SETUP_CLSFLOWER:
2924 return i40evf_setup_tc_cls_flower(cb_priv, type_data);
2925 default:
2926 return -EOPNOTSUPP;
2927 }
2928}
2929
2930/**
2931 * i40evf_setup_tc_block - register callbacks for tc
2932 * @netdev: network interface device structure
2933 * @f: tc offload data
2934 *
2935 * This function registers block callbacks for tc
2936 * offloads
2937 **/
2938static int i40evf_setup_tc_block(struct net_device *dev,
2939 struct tc_block_offload *f)
2940{
2941 struct i40evf_adapter *adapter = netdev_priv(dev);
2942
2943 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2944 return -EOPNOTSUPP;
2945
2946 switch (f->command) {
2947 case TC_BLOCK_BIND:
2948 return tcf_block_cb_register(f->block, i40evf_setup_tc_block_cb,
2949 adapter, adapter);
2950 case TC_BLOCK_UNBIND:
2951 tcf_block_cb_unregister(f->block, i40evf_setup_tc_block_cb,
2952 adapter);
2953 return 0;
2954 default:
2955 return -EOPNOTSUPP;
2956 }
2957}
2958
2959/**
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002960 * i40evf_setup_tc - configure multiple traffic classes
2961 * @netdev: network interface device structure
2962 * @type: type of offload
2963 * @type_date: tc offload data
2964 *
2965 * This function is the callback to ndo_setup_tc in the
2966 * netdev_ops.
2967 *
2968 * Returns 0 on success
2969 **/
2970static int i40evf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2971 void *type_data)
2972{
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002973 switch (type) {
2974 case TC_SETUP_QDISC_MQPRIO:
2975 return __i40evf_setup_tc(netdev, type_data);
2976 case TC_SETUP_BLOCK:
2977 return i40evf_setup_tc_block(netdev, type_data);
2978 default:
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002979 return -EOPNOTSUPP;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08002980 }
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08002981}
2982
2983/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002984 * i40evf_open - Called when a network interface is made active
2985 * @netdev: network interface device structure
2986 *
2987 * Returns 0 on success, negative value on failure
2988 *
2989 * The open entry point is called when a network interface is made
2990 * active by the system (IFF_UP). At this point all resources needed
2991 * for transmit and receive operations are allocated, the interrupt
2992 * handler is registered with the OS, the watchdog timer is started,
2993 * and the stack is notified that the interface is ready.
2994 **/
2995static int i40evf_open(struct net_device *netdev)
2996{
2997 struct i40evf_adapter *adapter = netdev_priv(netdev);
2998 int err;
2999
Mitch Williamsef8693e2014-02-13 03:48:53 -08003000 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
3001 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3002 return -EIO;
3003 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08003004
Jacob Keller9b2aef12017-10-27 11:06:52 -04003005 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3006 &adapter->crit_section))
3007 usleep_range(500, 1000);
3008
3009 if (adapter->state != __I40EVF_DOWN) {
3010 err = -EBUSY;
3011 goto err_unlock;
3012 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003013
3014 /* allocate transmit descriptors */
3015 err = i40evf_setup_all_tx_resources(adapter);
3016 if (err)
3017 goto err_setup_tx;
3018
3019 /* allocate receive descriptors */
3020 err = i40evf_setup_all_rx_resources(adapter);
3021 if (err)
3022 goto err_setup_rx;
3023
3024 /* clear any pending interrupts, may auto mask */
3025 err = i40evf_request_traffic_irqs(adapter, netdev->name);
3026 if (err)
3027 goto err_req_irq;
3028
Paweł Jabłoński8cd5fe62018-02-05 13:03:36 -08003029 spin_lock_bh(&adapter->mac_vlan_list_lock);
3030
Mitch Williams44151cd2015-04-27 14:57:17 -04003031 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Paweł Jabłoński8cd5fe62018-02-05 13:03:36 -08003032
3033 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3034
Greg Rose5eae00c2013-12-21 06:12:45 +00003035 i40evf_configure(adapter);
3036
Bimmy Pujaricb130a02016-09-06 18:05:03 -07003037 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003038
3039 i40evf_irq_enable(adapter, true);
3040
Jacob Keller9b2aef12017-10-27 11:06:52 -04003041 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3042
Greg Rose5eae00c2013-12-21 06:12:45 +00003043 return 0;
3044
3045err_req_irq:
3046 i40evf_down(adapter);
3047 i40evf_free_traffic_irqs(adapter);
3048err_setup_rx:
3049 i40evf_free_all_rx_resources(adapter);
3050err_setup_tx:
3051 i40evf_free_all_tx_resources(adapter);
Jacob Keller9b2aef12017-10-27 11:06:52 -04003052err_unlock:
3053 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00003054
3055 return err;
3056}
3057
3058/**
3059 * i40evf_close - Disables a network interface
3060 * @netdev: network interface device structure
3061 *
3062 * Returns 0, this is not allowed to fail
3063 *
3064 * The close entry point is called when an interface is de-activated
3065 * by the OS. The hardware is still under the drivers control, but
3066 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3067 * are freed, along with all transmit and receive resources.
3068 **/
3069static int i40evf_close(struct net_device *netdev)
3070{
3071 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04003072 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00003073
Mitch Williams209dc4d2015-12-09 15:50:27 -08003074 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08003075 return 0;
3076
Jacob Keller9b2aef12017-10-27 11:06:52 -04003077 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3078 &adapter->crit_section))
3079 usleep_range(500, 1000);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003080
Jacob Keller0da36b92017-04-19 09:25:55 -04003081 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003082 if (CLIENT_ENABLED(adapter))
3083 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00003084
3085 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08003086 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00003087 i40evf_free_traffic_irqs(adapter);
3088
Jacob Keller9b2aef12017-10-27 11:06:52 -04003089 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3090
Mitch Williams51f38262016-12-12 15:44:11 -08003091 /* We explicitly don't free resources here because the hardware is
3092 * still active and can DMA into memory. Resources are cleared in
3093 * i40evf_virtchnl_completion() after we get confirmation from the PF
3094 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04003095 *
3096 * Also, we wait for state to transition to __I40EVF_DOWN before
3097 * returning. State change occurs in i40evf_virtchnl_completion() after
3098 * VF resources are released (which occurs after PF driver processes and
3099 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08003100 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04003101
3102 status = wait_event_timeout(adapter->down_waitqueue,
3103 adapter->state == __I40EVF_DOWN,
3104 msecs_to_jiffies(200));
3105 if (!status)
3106 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00003107 return 0;
3108}
3109
3110/**
Greg Rose5eae00c2013-12-21 06:12:45 +00003111 * i40evf_change_mtu - Change the Maximum Transfer Unit
3112 * @netdev: network interface device structure
3113 * @new_mtu: new value for maximum frame size
3114 *
3115 * Returns 0 on success, negative on failure
3116 **/
3117static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
3118{
3119 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00003120
Greg Rose5eae00c2013-12-21 06:12:45 +00003121 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003122 if (CLIENT_ENABLED(adapter)) {
3123 i40evf_notify_client_l2_params(&adapter->vsi);
3124 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
3125 }
Mitch Williams67c818a2015-06-19 08:56:30 -07003126 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
3127 schedule_work(&adapter->reset_task);
3128
Greg Rose5eae00c2013-12-21 06:12:45 +00003129 return 0;
3130}
3131
Alexander Duyck06fc0162016-10-25 16:08:47 -07003132/**
Mariusz Stachura87743702017-07-17 22:09:45 -07003133 * i40e_set_features - set the netdev feature flags
3134 * @netdev: ptr to the netdev being adjusted
3135 * @features: the feature set that the stack is suggesting
3136 * Note: expects to be called while under rtnl_lock()
3137 **/
3138static int i40evf_set_features(struct net_device *netdev,
3139 netdev_features_t features)
3140{
3141 struct i40evf_adapter *adapter = netdev_priv(netdev);
3142
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05003143 /* Don't allow changing VLAN_RX flag when VLAN is set for VF
3144 * and return an error in this case
3145 */
3146 if (VLAN_ALLOWED(adapter)) {
3147 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3148 adapter->aq_required |=
3149 I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3150 else
3151 adapter->aq_required |=
3152 I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3153 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
Mariusz Stachura87743702017-07-17 22:09:45 -07003154 return -EINVAL;
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05003155 }
Mariusz Stachura87743702017-07-17 22:09:45 -07003156
3157 return 0;
3158}
3159
3160/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07003161 * i40evf_features_check - Validate encapsulated packet conforms to limits
3162 * @skb: skb buff
3163 * @netdev: This physical port's netdev
3164 * @features: Offload features that the stack believes apply
3165 **/
3166static netdev_features_t i40evf_features_check(struct sk_buff *skb,
3167 struct net_device *dev,
3168 netdev_features_t features)
3169{
3170 size_t len;
3171
3172 /* No point in doing any of this if neither checksum nor GSO are
3173 * being requested for this frame. We can rule out both by just
3174 * checking for CHECKSUM_PARTIAL
3175 */
3176 if (skb->ip_summed != CHECKSUM_PARTIAL)
3177 return features;
3178
3179 /* We cannot support GSO if the MSS is going to be less than
3180 * 64 bytes. If it is then we need to drop support for GSO.
3181 */
3182 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3183 features &= ~NETIF_F_GSO_MASK;
3184
3185 /* MACLEN can support at most 63 words */
3186 len = skb_network_header(skb) - skb->data;
3187 if (len & ~(63 * 2))
3188 goto out_err;
3189
3190 /* IPLEN and EIPLEN can support at most 127 dwords */
3191 len = skb_transport_header(skb) - skb_network_header(skb);
3192 if (len & ~(127 * 4))
3193 goto out_err;
3194
3195 if (skb->encapsulation) {
3196 /* L4TUNLEN can support 127 words */
3197 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3198 if (len & ~(127 * 2))
3199 goto out_err;
3200
3201 /* IPLEN can support at most 127 dwords */
3202 len = skb_inner_transport_header(skb) -
3203 skb_inner_network_header(skb);
3204 if (len & ~(127 * 4))
3205 goto out_err;
3206 }
3207
3208 /* No need to validate L4LEN as TCP is the only protocol with a
3209 * a flexible value and we support all possible values supported
3210 * by TCP, which is at most 15 dwords
3211 */
3212
3213 return features;
3214out_err:
3215 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3216}
3217
Mitch Williamsc4445ae2016-03-18 12:18:07 -07003218/**
3219 * i40evf_fix_features - fix up the netdev feature bits
3220 * @netdev: our net device
3221 * @features: desired feature bits
3222 *
3223 * Returns fixed-up features bits
3224 **/
3225static netdev_features_t i40evf_fix_features(struct net_device *netdev,
3226 netdev_features_t features)
3227{
3228 struct i40evf_adapter *adapter = netdev_priv(netdev);
3229
Jacob Keller0a3b4f72017-08-29 05:32:41 -04003230 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3231 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3232 NETIF_F_HW_VLAN_CTAG_RX |
3233 NETIF_F_HW_VLAN_CTAG_FILTER);
3234
Mitch Williamsc4445ae2016-03-18 12:18:07 -07003235 return features;
3236}
3237
Greg Rose5eae00c2013-12-21 06:12:45 +00003238static const struct net_device_ops i40evf_netdev_ops = {
3239 .ndo_open = i40evf_open,
3240 .ndo_stop = i40evf_close,
3241 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00003242 .ndo_set_rx_mode = i40evf_set_rx_mode,
3243 .ndo_validate_addr = eth_validate_addr,
3244 .ndo_set_mac_address = i40evf_set_mac,
3245 .ndo_change_mtu = i40evf_change_mtu,
3246 .ndo_tx_timeout = i40evf_tx_timeout,
3247 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
3248 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07003249 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07003250 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07003251 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07003252#ifdef CONFIG_NET_POLL_CONTROLLER
3253 .ndo_poll_controller = i40evf_netpoll,
3254#endif
Harshitha Ramamurthyd5b33d02018-01-23 08:50:57 -08003255 .ndo_setup_tc = i40evf_setup_tc,
Greg Rose5eae00c2013-12-21 06:12:45 +00003256};
3257
3258/**
3259 * i40evf_check_reset_complete - check that VF reset is complete
3260 * @hw: pointer to hw struct
3261 *
3262 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3263 **/
3264static int i40evf_check_reset_complete(struct i40e_hw *hw)
3265{
3266 u32 rstat;
3267 int i;
3268
3269 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07003270 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
3271 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003272 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3273 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00003274 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00003275 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00003276 }
3277 return -EBUSY;
3278}
3279
3280/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04003281 * i40evf_process_config - Process the config information we got from the PF
3282 * @adapter: board private structure
3283 *
3284 * Verify that we have a valid config struct, and set up our netdev features
3285 * and our VSI struct.
3286 **/
3287int i40evf_process_config(struct i40evf_adapter *adapter)
3288{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003289 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04003290 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04003291 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003292 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07003293 netdev_features_t hw_enc_features;
3294 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04003295
3296 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07003297 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07003298 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07003299 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04003300 }
3301 if (!adapter->vsi_res) {
3302 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3303 return -ENODEV;
3304 }
3305
Alan Brady5b36e8d2017-08-22 06:57:50 -04003306 if (num_req_queues &&
3307 num_req_queues != adapter->vsi_res->num_queue_pairs) {
3308 /* Problem. The PF gave us fewer queues than what we had
3309 * negotiated in our request. Need a reset to see if we can't
3310 * get back to a working state.
3311 */
3312 dev_err(&adapter->pdev->dev,
3313 "Requested %d queues, but PF only gave us %d.\n",
3314 num_req_queues,
3315 adapter->vsi_res->num_queue_pairs);
3316 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
3317 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3318 i40evf_schedule_reset(adapter);
3319 return -ENODEV;
3320 }
3321 adapter->num_req_queues = 0;
3322
Preethi Banalabacd75c2017-03-27 14:43:18 -07003323 hw_enc_features = NETIF_F_SG |
3324 NETIF_F_IP_CSUM |
3325 NETIF_F_IPV6_CSUM |
3326 NETIF_F_HIGHDMA |
3327 NETIF_F_SOFT_FEATURES |
3328 NETIF_F_TSO |
3329 NETIF_F_TSO_ECN |
3330 NETIF_F_TSO6 |
3331 NETIF_F_SCTP_CRC |
3332 NETIF_F_RXHASH |
3333 NETIF_F_RXCSUM |
3334 0;
3335
3336 /* advertise to stack only if offloads for encapsulated packets is
3337 * supported
3338 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02003339 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07003340 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003341 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04003342 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07003343 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07003344 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003345 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04003346 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003347 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04003348
Stefan Assmannfbb113f2017-06-29 15:12:24 +02003349 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003350 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07003351 netdev->gso_partial_features |=
3352 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04003353
Preethi Banalabacd75c2017-03-27 14:43:18 -07003354 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3355 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3356 netdev->hw_enc_features |= hw_enc_features;
3357 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003358 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07003359 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08003360
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003361 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07003362 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003363 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07003364 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07003365
Jacob Keller0a3b4f72017-08-29 05:32:41 -04003366 /* Enable VLAN features if supported */
3367 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3368 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3369 NETIF_F_HW_VLAN_CTAG_RX);
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08003370 /* Enable cloud filter if ADQ is supported */
3371 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3372 hw_features |= NETIF_F_HW_TC;
Jacob Keller0a3b4f72017-08-29 05:32:41 -04003373
Preethi Banalabacd75c2017-03-27 14:43:18 -07003374 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07003375
Jacob Keller0a3b4f72017-08-29 05:32:41 -04003376 netdev->features |= hw_features;
3377
3378 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3379 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mitch Williamse6d038d2015-06-04 16:23:58 -04003380
3381 adapter->vsi.id = adapter->vsi_res->vsi_id;
3382
3383 adapter->vsi.back = adapter;
3384 adapter->vsi.base_vector = 1;
3385 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003386 vsi->netdev = adapter->netdev;
3387 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02003388 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003389 adapter->rss_key_size = vfres->rss_key_size;
3390 adapter->rss_lut_size = vfres->rss_lut_size;
3391 } else {
3392 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
3393 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
3394 }
3395
Mitch Williamse6d038d2015-06-04 16:23:58 -04003396 return 0;
3397}
3398
3399/**
Greg Rose5eae00c2013-12-21 06:12:45 +00003400 * i40evf_init_task - worker thread to perform delayed initialization
3401 * @work: pointer to work_struct containing our data
3402 *
3403 * This task completes the work that was begun in probe. Due to the nature
3404 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08003405 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00003406 * whole system, we'll do it in a task so we can sleep.
3407 * This task only runs during driver init. Once we've established
3408 * communications with the PF driver and set up our netdev, the watchdog
3409 * takes over.
3410 **/
3411static void i40evf_init_task(struct work_struct *work)
3412{
3413 struct i40evf_adapter *adapter = container_of(work,
3414 struct i40evf_adapter,
3415 init_task.work);
3416 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00003417 struct i40e_hw *hw = &adapter->hw;
3418 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04003419 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00003420
3421 switch (adapter->state) {
3422 case __I40EVF_STARTUP:
3423 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08003424 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
3425 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00003426 err = i40e_set_mac_type(hw);
3427 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08003428 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
3429 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04003430 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003431 }
3432 err = i40evf_check_reset_complete(hw);
3433 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00003434 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00003435 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00003436 goto err;
3437 }
3438 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
3439 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
3440 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
3441 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
3442
3443 err = i40evf_init_adminq(hw);
3444 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08003445 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
3446 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00003447 goto err;
3448 }
3449 err = i40evf_send_api_ver(adapter);
3450 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00003451 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00003452 i40evf_shutdown_adminq(hw);
3453 goto err;
3454 }
3455 adapter->state = __I40EVF_INIT_VERSION_CHECK;
3456 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00003457 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00003458 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00003459 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00003460 i40evf_shutdown_adminq(hw);
3461 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00003462 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00003463 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003464
3465 /* aq msg sent, awaiting reply */
3466 err = i40evf_verify_api_ver(adapter);
3467 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00003468 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00003469 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04003470 else
3471 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
3472 adapter->pf_version.major,
3473 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003474 VIRTCHNL_VERSION_MAJOR,
3475 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00003476 goto err;
3477 }
3478 err = i40evf_send_vf_config_msg(adapter);
3479 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00003480 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00003481 err);
3482 goto err;
3483 }
3484 adapter->state = __I40EVF_INIT_GET_RESOURCES;
3485 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00003486 case __I40EVF_INIT_GET_RESOURCES:
3487 /* aq msg sent, awaiting reply */
3488 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003489 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00003490 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003491 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00003492 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08003493 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00003494 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003495 }
3496 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00003497 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00003498 err = i40evf_send_vf_config_msg(adapter);
3499 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04003500 } else if (err == I40E_ERR_PARAM) {
3501 /* We only get ERR_PARAM if the device is in a very bad
3502 * state or if we've been disabled for previous bad
3503 * behavior. Either way, we're done now.
3504 */
3505 i40evf_shutdown_adminq(hw);
3506 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
3507 return;
Mitch Williams906a6932014-11-13 03:06:12 +00003508 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003509 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08003510 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
3511 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00003512 goto err_alloc;
3513 }
3514 adapter->state = __I40EVF_INIT_SW;
3515 break;
3516 default:
3517 goto err_alloc;
3518 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08003519
Mitch Williamse6d038d2015-06-04 16:23:58 -04003520 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00003521 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003522 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00003523
3524 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
3525
Greg Rose5eae00c2013-12-21 06:12:45 +00003526 netdev->netdev_ops = &i40evf_netdev_ops;
3527 i40evf_set_ethtool_ops(netdev);
3528 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00003529
Jarod Wilson91c527a2016-10-17 15:54:05 -04003530 /* MTU range: 68 - 9710 */
3531 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04003532 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04003533
Greg Rose5eae00c2013-12-21 06:12:45 +00003534 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00003535 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08003536 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04003537 eth_hw_addr_random(netdev);
3538 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
3539 } else {
3540 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
3541 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
3542 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00003543 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003544
Kees Cook26566ea2017-10-16 17:29:35 -07003545 timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
Greg Rose5eae00c2013-12-21 06:12:45 +00003546 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3547
Mitch Williamsd732a182014-04-24 06:41:37 +00003548 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
3549 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00003550 err = i40evf_init_interrupt_scheme(adapter);
3551 if (err)
3552 goto err_sw_init;
3553 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02003554 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07003555 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08003556 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
3557
Greg Rose5eae00c2013-12-21 06:12:45 +00003558 err = i40evf_request_misc_irq(adapter);
3559 if (err)
3560 goto err_sw_init;
3561
3562 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02003563 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00003564
Mitch Williamsef8693e2014-02-13 03:48:53 -08003565 if (!adapter->netdev_registered) {
3566 err = register_netdev(netdev);
3567 if (err)
3568 goto err_register;
3569 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003570
3571 adapter->netdev_registered = true;
3572
3573 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003574 if (CLIENT_ALLOWED(adapter)) {
3575 err = i40evf_lan_add_device(adapter);
3576 if (err)
3577 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
3578 err);
3579 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003580
Mitch Williamsb34f90e2014-05-10 04:49:07 +00003581 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00003582 if (netdev->features & NETIF_F_GRO)
3583 dev_info(&pdev->dev, "GRO is enabled\n");
3584
Greg Rose5eae00c2013-12-21 06:12:45 +00003585 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04003586 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00003587 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04003588 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04003589
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003590 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
3591 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
3592 if (!adapter->rss_key || !adapter->rss_lut)
3593 goto err_mem;
3594
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04003595 if (RSS_AQ(adapter)) {
3596 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
3597 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
3598 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04003599 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04003600 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003601 return;
3602restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04003603 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00003604 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003605err_mem:
3606 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003607err_register:
3608 i40evf_free_misc_irq(adapter);
3609err_sw_init:
3610 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003611err_alloc:
3612 kfree(adapter->vf_res);
3613 adapter->vf_res = NULL;
3614err:
3615 /* Things went into the weeds, so try again later */
3616 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04003617 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08003618 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04003619 i40evf_shutdown_adminq(hw);
3620 adapter->state = __I40EVF_STARTUP;
3621 schedule_delayed_work(&adapter->init_task, HZ * 5);
3622 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00003623 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04003624 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00003625}
3626
3627/**
3628 * i40evf_shutdown - Shutdown the device in preparation for a reboot
3629 * @pdev: pci device structure
3630 **/
3631static void i40evf_shutdown(struct pci_dev *pdev)
3632{
3633 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00003634 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00003635
3636 netif_device_detach(netdev);
3637
3638 if (netif_running(netdev))
3639 i40evf_close(netdev);
3640
Mitch Williams00293fd2015-01-09 11:18:18 +00003641 /* Prevent the watchdog from running. */
3642 adapter->state = __I40EVF_REMOVE;
3643 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00003644
Greg Rose5eae00c2013-12-21 06:12:45 +00003645#ifdef CONFIG_PM
3646 pci_save_state(pdev);
3647
3648#endif
3649 pci_disable_device(pdev);
3650}
3651
3652/**
3653 * i40evf_probe - Device Initialization Routine
3654 * @pdev: PCI device information struct
3655 * @ent: entry in i40evf_pci_tbl
3656 *
3657 * Returns 0 on success, negative on failure
3658 *
3659 * i40evf_probe initializes an adapter identified by a pci_dev structure.
3660 * The OS initialization, configuring of the adapter private structure,
3661 * and a hardware reset occur.
3662 **/
3663static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3664{
3665 struct net_device *netdev;
3666 struct i40evf_adapter *adapter = NULL;
3667 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08003668 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00003669
3670 err = pci_enable_device(pdev);
3671 if (err)
3672 return err;
3673
Mitch Williams64942942014-02-11 08:26:33 +00003674 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00003675 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00003676 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3677 if (err) {
3678 dev_err(&pdev->dev,
3679 "DMA configuration failed: 0x%x\n", err);
3680 goto err_dma;
3681 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003682 }
3683
3684 err = pci_request_regions(pdev, i40evf_driver_name);
3685 if (err) {
3686 dev_err(&pdev->dev,
3687 "pci_request_regions failed 0x%x\n", err);
3688 goto err_pci_reg;
3689 }
3690
3691 pci_enable_pcie_error_reporting(pdev);
3692
3693 pci_set_master(pdev);
3694
Mitch Williams1255b7a2015-11-06 15:25:59 -08003695 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00003696 if (!netdev) {
3697 err = -ENOMEM;
3698 goto err_alloc_etherdev;
3699 }
3700
3701 SET_NETDEV_DEV(netdev, &pdev->dev);
3702
3703 pci_set_drvdata(pdev, netdev);
3704 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00003705
3706 adapter->netdev = netdev;
3707 adapter->pdev = pdev;
3708
3709 hw = &adapter->hw;
3710 hw->back = adapter;
3711
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003712 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00003713 adapter->state = __I40EVF_STARTUP;
3714
3715 /* Call save state here because it relies on the adapter struct. */
3716 pci_save_state(pdev);
3717
3718 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3719 pci_resource_len(pdev, 0));
3720 if (!hw->hw_addr) {
3721 err = -EIO;
3722 goto err_ioremap;
3723 }
3724 hw->vendor_id = pdev->vendor;
3725 hw->device_id = pdev->device;
3726 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3727 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3728 hw->subsystem_device_id = pdev->subsystem_device;
3729 hw->bus.device = PCI_SLOT(pdev->devfn);
3730 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08003731 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00003732
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003733 /* set up the locks for the AQ, do this only once in probe
3734 * and destroy them only once in remove
3735 */
3736 mutex_init(&hw->aq.asq_mutex);
3737 mutex_init(&hw->aq.arq_mutex);
3738
Jacob Keller504398f2017-10-27 11:06:50 -04003739 spin_lock_init(&adapter->mac_vlan_list_lock);
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08003740 spin_lock_init(&adapter->cloud_filter_list_lock);
Jacob Keller504398f2017-10-27 11:06:50 -04003741
Serey Kong8bb1a542014-08-01 13:27:15 -07003742 INIT_LIST_HEAD(&adapter->mac_filter_list);
3743 INIT_LIST_HEAD(&adapter->vlan_filter_list);
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08003744 INIT_LIST_HEAD(&adapter->cloud_filter_list);
Serey Kong8bb1a542014-08-01 13:27:15 -07003745
Greg Rose5eae00c2013-12-21 06:12:45 +00003746 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
3747 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
3748 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003749 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003750 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07003751 schedule_delayed_work(&adapter->init_task,
3752 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00003753
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04003754 /* Setup the wait queue for indicating transition to down status */
3755 init_waitqueue_head(&adapter->down_waitqueue);
3756
Greg Rose5eae00c2013-12-21 06:12:45 +00003757 return 0;
3758
3759err_ioremap:
3760 free_netdev(netdev);
3761err_alloc_etherdev:
3762 pci_release_regions(pdev);
3763err_pci_reg:
3764err_dma:
3765 pci_disable_device(pdev);
3766 return err;
3767}
3768
3769#ifdef CONFIG_PM
3770/**
3771 * i40evf_suspend - Power management suspend routine
3772 * @pdev: PCI device information struct
3773 * @state: unused
3774 *
3775 * Called when the system (VM) is entering sleep/suspend.
3776 **/
3777static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
3778{
3779 struct net_device *netdev = pci_get_drvdata(pdev);
3780 struct i40evf_adapter *adapter = netdev_priv(netdev);
3781 int retval = 0;
3782
3783 netif_device_detach(netdev);
3784
Jacob Keller9b2aef12017-10-27 11:06:52 -04003785 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3786 &adapter->crit_section))
3787 usleep_range(500, 1000);
3788
Greg Rose5eae00c2013-12-21 06:12:45 +00003789 if (netif_running(netdev)) {
3790 rtnl_lock();
3791 i40evf_down(adapter);
3792 rtnl_unlock();
3793 }
3794 i40evf_free_misc_irq(adapter);
3795 i40evf_reset_interrupt_capability(adapter);
3796
Jacob Keller9b2aef12017-10-27 11:06:52 -04003797 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3798
Greg Rose5eae00c2013-12-21 06:12:45 +00003799 retval = pci_save_state(pdev);
3800 if (retval)
3801 return retval;
3802
3803 pci_disable_device(pdev);
3804
3805 return 0;
3806}
3807
3808/**
Joe Perchesdbedd442015-03-06 20:49:12 -08003809 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00003810 * @pdev: PCI device information struct
3811 *
3812 * Called when the system (VM) is resumed from sleep/suspend.
3813 **/
3814static int i40evf_resume(struct pci_dev *pdev)
3815{
3816 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3817 struct net_device *netdev = adapter->netdev;
3818 u32 err;
3819
3820 pci_set_power_state(pdev, PCI_D0);
3821 pci_restore_state(pdev);
3822 /* pci_restore_state clears dev->state_saved so call
3823 * pci_save_state to restore it.
3824 */
3825 pci_save_state(pdev);
3826
3827 err = pci_enable_device_mem(pdev);
3828 if (err) {
3829 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3830 return err;
3831 }
3832 pci_set_master(pdev);
3833
3834 rtnl_lock();
3835 err = i40evf_set_interrupt_capability(adapter);
3836 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003837 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003838 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3839 return err;
3840 }
3841 err = i40evf_request_misc_irq(adapter);
3842 rtnl_unlock();
3843 if (err) {
3844 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3845 return err;
3846 }
3847
3848 schedule_work(&adapter->reset_task);
3849
3850 netif_device_attach(netdev);
3851
3852 return err;
3853}
3854
3855#endif /* CONFIG_PM */
3856/**
3857 * i40evf_remove - Device Removal Routine
3858 * @pdev: PCI device information struct
3859 *
3860 * i40evf_remove is called by the PCI subsystem to alert the driver
3861 * that it should release a PCI device. The could be caused by a
3862 * Hot-Plug event, or because the driver is going to be removed from
3863 * memory.
3864 **/
3865static void i40evf_remove(struct pci_dev *pdev)
3866{
3867 struct net_device *netdev = pci_get_drvdata(pdev);
3868 struct i40evf_adapter *adapter = netdev_priv(netdev);
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05003869 struct i40evf_vlan_filter *vlf, *vlftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07003870 struct i40evf_mac_filter *f, *ftmp;
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08003871 struct i40evf_cloud_filter *cf, *cftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003872 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003873 int err;
Avinash Dayanand06aa0402017-12-18 05:16:43 -05003874 /* Indicate we are in remove and not to run reset_task */
3875 set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00003876 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003877 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003878 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003879 if (adapter->netdev_registered) {
3880 unregister_netdev(netdev);
3881 adapter->netdev_registered = false;
3882 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003883 if (CLIENT_ALLOWED(adapter)) {
3884 err = i40evf_lan_del_device(adapter);
3885 if (err)
3886 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3887 err);
3888 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003889
Mitch Williamsf4a71882015-01-09 11:18:16 +00003890 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003891 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003892 adapter->aq_required = 0;
Avinash Dayanand5e97ce62018-01-23 08:50:59 -08003893 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003894 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003895 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003896 /* If the FW isn't responding, kick it once, but only once. */
3897 if (!i40evf_asq_done(hw)) {
3898 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003899 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003900 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003901 i40evf_free_all_tx_resources(adapter);
3902 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003903 i40evf_misc_irq_disable(adapter);
3904 i40evf_free_misc_irq(adapter);
3905 i40evf_reset_interrupt_capability(adapter);
3906 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003907
Mitch Williamse5d17c32014-06-04 04:22:38 +00003908 if (adapter->watchdog_timer.function)
3909 del_timer_sync(&adapter->watchdog_timer);
3910
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003911 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003912
Greg Rose5eae00c2013-12-21 06:12:45 +00003913 if (hw->aq.asq.count)
3914 i40evf_shutdown_adminq(hw);
3915
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003916 /* destroy the locks only once, here */
3917 mutex_destroy(&hw->aq.arq_mutex);
3918 mutex_destroy(&hw->aq.asq_mutex);
3919
Greg Rose5eae00c2013-12-21 06:12:45 +00003920 iounmap(hw->hw_addr);
3921 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003922 i40evf_free_all_tx_resources(adapter);
3923 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003924 i40evf_free_queues(adapter);
3925 kfree(adapter->vf_res);
Jacob Keller504398f2017-10-27 11:06:50 -04003926 spin_lock_bh(&adapter->mac_vlan_list_lock);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003927 /* If we got removed before an up/down sequence, we've got a filter
3928 * hanging out there that we need to get rid of.
3929 */
3930 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3931 list_del(&f->list);
3932 kfree(f);
3933 }
Harshitha Ramamurthyfbd5eb52018-01-22 12:00:34 -05003934 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3935 list) {
3936 list_del(&vlf->list);
3937 kfree(vlf);
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003938 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003939
Jacob Keller504398f2017-10-27 11:06:50 -04003940 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3941
Harshitha Ramamurthy0075fa02018-01-23 08:51:05 -08003942 spin_lock_bh(&adapter->cloud_filter_list_lock);
3943 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3944 list_del(&cf->list);
3945 kfree(cf);
3946 }
3947 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3948
Greg Rose5eae00c2013-12-21 06:12:45 +00003949 free_netdev(netdev);
3950
3951 pci_disable_pcie_error_reporting(pdev);
3952
3953 pci_disable_device(pdev);
3954}
3955
3956static struct pci_driver i40evf_driver = {
3957 .name = i40evf_driver_name,
3958 .id_table = i40evf_pci_tbl,
3959 .probe = i40evf_probe,
3960 .remove = i40evf_remove,
3961#ifdef CONFIG_PM
3962 .suspend = i40evf_suspend,
3963 .resume = i40evf_resume,
3964#endif
3965 .shutdown = i40evf_shutdown,
3966};
3967
3968/**
3969 * i40e_init_module - Driver Registration Routine
3970 *
3971 * i40e_init_module is the first routine called when the driver is
3972 * loaded. All it does is register with the PCI subsystem.
3973 **/
3974static int __init i40evf_init_module(void)
3975{
3976 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003977
Greg Rose5eae00c2013-12-21 06:12:45 +00003978 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003979 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003980
3981 pr_info("%s\n", i40evf_copyright);
3982
Jacob Keller6992a6c2016-08-04 11:37:01 -07003983 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3984 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003985 if (!i40evf_wq) {
3986 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3987 return -ENOMEM;
3988 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003989 ret = pci_register_driver(&i40evf_driver);
3990 return ret;
3991}
3992
3993module_init(i40evf_init_module);
3994
3995/**
3996 * i40e_exit_module - Driver Exit Cleanup Routine
3997 *
3998 * i40e_exit_module is called just before the driver is removed
3999 * from memory.
4000 **/
4001static void __exit i40evf_exit_module(void)
4002{
4003 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08004004 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00004005}
4006
4007module_exit(i40evf_exit_module);
4008
4009/* i40evf_main.c */