blob: 6fd09926181ae096c0924c99e2e48320e226b08e [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,
788 u8 *macaddr)
789{
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,
811 u8 *macaddr)
812{
813 struct i40evf_mac_filter *f;
814
815 if (!macaddr)
816 return NULL;
817
Jacob Keller504398f2017-10-27 11:06:50 -0400818 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000819
820 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000821 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000822 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Jacob Keller504398f2017-10-27 11:06:50 -0400823 if (!f)
824 goto clearout;
Greg Rose5eae00c2013-12-21 06:12:45 +0000825
Greg Rose9a173902014-05-22 06:32:02 +0000826 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000827
Mitch Williams63590b62016-05-16 10:26:42 -0700828 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000829 f->add = true;
830 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Alan Bradyc766b9a2017-09-07 08:05:47 -0400831 } else {
832 f->remove = false;
Greg Rose5eae00c2013-12-21 06:12:45 +0000833 }
834
Jacob Keller504398f2017-10-27 11:06:50 -0400835clearout:
836 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000837 return f;
838}
839
840/**
841 * i40evf_set_mac - NDO callback to set port mac address
842 * @netdev: network interface device structure
843 * @p: pointer to an address structure
844 *
845 * Returns 0 on success, negative on failure
846 **/
847static int i40evf_set_mac(struct net_device *netdev, void *p)
848{
849 struct i40evf_adapter *adapter = netdev_priv(netdev);
850 struct i40e_hw *hw = &adapter->hw;
851 struct i40evf_mac_filter *f;
852 struct sockaddr *addr = p;
853
854 if (!is_valid_ether_addr(addr->sa_data))
855 return -EADDRNOTAVAIL;
856
857 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
858 return 0;
859
Mitch Williams14e52ee2015-08-31 19:54:44 -0400860 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
861 return -EPERM;
862
Jacob Keller504398f2017-10-27 11:06:50 -0400863 spin_lock_bh(&adapter->mac_vlan_list_lock);
864
Mitch Williams14e52ee2015-08-31 19:54:44 -0400865 f = i40evf_find_filter(adapter, hw->mac.addr);
866 if (f) {
867 f->remove = true;
868 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
869 }
870
Jacob Keller504398f2017-10-27 11:06:50 -0400871 spin_unlock_bh(&adapter->mac_vlan_list_lock);
872
Greg Rose5eae00c2013-12-21 06:12:45 +0000873 f = i40evf_add_filter(adapter, addr->sa_data);
874 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000875 ether_addr_copy(hw->mac.addr, addr->sa_data);
876 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000877 }
878
879 return (f == NULL) ? -ENOMEM : 0;
880}
881
882/**
883 * i40evf_set_rx_mode - NDO callback to set the netdev filters
884 * @netdev: network interface device structure
885 **/
886static void i40evf_set_rx_mode(struct net_device *netdev)
887{
888 struct i40evf_adapter *adapter = netdev_priv(netdev);
889 struct i40evf_mac_filter *f, *ftmp;
890 struct netdev_hw_addr *uca;
891 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400892 struct netdev_hw_addr *ha;
Greg Rose5eae00c2013-12-21 06:12:45 +0000893
894 /* add addr if not already in the filter list */
895 netdev_for_each_uc_addr(uca, netdev) {
896 i40evf_add_filter(adapter, uca->addr);
897 }
898 netdev_for_each_mc_addr(mca, netdev) {
899 i40evf_add_filter(adapter, mca->addr);
900 }
901
Jacob Keller504398f2017-10-27 11:06:50 -0400902 spin_lock_bh(&adapter->mac_vlan_list_lock);
903
Greg Rose5eae00c2013-12-21 06:12:45 +0000904 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400905 netdev_for_each_mc_addr(mca, netdev)
906 if (ether_addr_equal(mca->addr, f->macaddr))
907 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000908
Shannon Nelson2f41f332015-08-26 15:14:20 -0400909 netdev_for_each_uc_addr(uca, netdev)
910 if (ether_addr_equal(uca->addr, f->macaddr))
911 goto bottom_of_search_loop;
912
913 for_each_dev_addr(netdev, ha)
914 if (ether_addr_equal(ha->addr, f->macaddr))
915 goto bottom_of_search_loop;
916
917 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
918 goto bottom_of_search_loop;
919
920 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
921 f->remove = true;
922 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
923
924bottom_of_search_loop:
925 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000926 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700927
928 if (netdev->flags & IFF_PROMISC &&
929 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
930 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
931 else if (!(netdev->flags & IFF_PROMISC) &&
932 adapter->flags & I40EVF_FLAG_PROMISC_ON)
933 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
934
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700935 if (netdev->flags & IFF_ALLMULTI &&
936 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
937 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
938 else if (!(netdev->flags & IFF_ALLMULTI) &&
939 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
940 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
941
Jacob Keller504398f2017-10-27 11:06:50 -0400942 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000943}
944
945/**
946 * i40evf_napi_enable_all - enable NAPI on all queue vectors
947 * @adapter: board private structure
948 **/
949static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
950{
951 int q_idx;
952 struct i40e_q_vector *q_vector;
953 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
954
955 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
956 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000957
Mitch Williams7d96ba12015-10-26 19:44:39 -0400958 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000959 napi = &q_vector->napi;
960 napi_enable(napi);
961 }
962}
963
964/**
965 * i40evf_napi_disable_all - disable NAPI on all queue vectors
966 * @adapter: board private structure
967 **/
968static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
969{
970 int q_idx;
971 struct i40e_q_vector *q_vector;
972 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
973
974 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400975 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000976 napi_disable(&q_vector->napi);
977 }
978}
979
980/**
981 * i40evf_configure - set up transmit and receive data structures
982 * @adapter: board private structure
983 **/
984static void i40evf_configure(struct i40evf_adapter *adapter)
985{
986 struct net_device *netdev = adapter->netdev;
987 int i;
988
989 i40evf_set_rx_mode(netdev);
990
991 i40evf_configure_tx(adapter);
992 i40evf_configure_rx(adapter);
993 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
994
Mitch Williamscc052922014-10-25 03:24:34 +0000995 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400996 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000997
Mitch Williamsb1630982016-04-18 11:33:48 -0700998 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +0000999 }
1000}
1001
1002/**
1003 * i40evf_up_complete - Finish the last steps of bringing up a connection
1004 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001005 *
1006 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001007 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001008static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001009{
1010 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001011 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001012
1013 i40evf_napi_enable_all(adapter);
1014
1015 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001016 if (CLIENT_ENABLED(adapter))
1017 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001018 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001019}
1020
1021/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001022 * i40e_down - Shutdown the connection processing
1023 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001024 *
1025 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001026 **/
1027void i40evf_down(struct i40evf_adapter *adapter)
1028{
1029 struct net_device *netdev = adapter->netdev;
1030 struct i40evf_mac_filter *f;
1031
Mitch Williams209dc4d2015-12-09 15:50:27 -08001032 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001033 return;
1034
Mitch Williams63e18c22015-03-27 00:12:10 -07001035 netif_carrier_off(netdev);
1036 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001037 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001038 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001039 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001040
Jacob Keller504398f2017-10-27 11:06:50 -04001041 spin_lock_bh(&adapter->mac_vlan_list_lock);
1042
Mitch Williamsef8693e2014-02-13 03:48:53 -08001043 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001044 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1045 f->remove = true;
1046 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001047 /* remove all VLAN filters */
1048 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1049 f->remove = true;
1050 }
Jacob Keller504398f2017-10-27 11:06:50 -04001051
1052 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1053
Mitch Williamsef8693e2014-02-13 03:48:53 -08001054 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1055 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001056 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001057 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001058 /* Schedule operations to close down the HW. Don't wait
1059 * here for this to complete. The watchdog is still running
1060 * and it will take care of this.
1061 */
1062 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001063 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001064 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001065 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001066
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001067 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001068}
1069
1070/**
1071 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1072 * @adapter: board private structure
1073 * @vectors: number of vectors to request
1074 *
1075 * Work with the OS to set up the MSIX vectors needed.
1076 *
1077 * Returns 0 on success, negative on failure
1078 **/
1079static int
1080i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1081{
1082 int err, vector_threshold;
1083
1084 /* We'll want at least 3 (vector_threshold):
1085 * 0) Other (Admin Queue and link, mostly)
1086 * 1) TxQ[0] Cleanup
1087 * 2) RxQ[0] Cleanup
1088 */
1089 vector_threshold = MIN_MSIX_COUNT;
1090
1091 /* The more we get, the more we will assign to Tx/Rx Cleanup
1092 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1093 * Right now, we simply care about how many we'll get; we'll
1094 * set them up later while requesting irq's.
1095 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001096 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1097 vector_threshold, vectors);
1098 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001099 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001100 kfree(adapter->msix_entries);
1101 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001102 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001103 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001104
1105 /* Adjust for only the vectors we'll use, which is minimum
1106 * of max_msix_q_vectors + NONQ_VECS, or the number of
1107 * vectors we were allocated.
1108 */
1109 adapter->num_msix_vectors = err;
1110 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001111}
1112
1113/**
1114 * i40evf_free_queues - Free memory for all rings
1115 * @adapter: board private structure to initialize
1116 *
1117 * Free all of the memory associated with queue pairs.
1118 **/
1119static void i40evf_free_queues(struct i40evf_adapter *adapter)
1120{
Greg Rose5eae00c2013-12-21 06:12:45 +00001121 if (!adapter->vsi_res)
1122 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001123 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001124 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001125 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001126 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001127 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001128}
1129
1130/**
1131 * i40evf_alloc_queues - Allocate memory for all rings
1132 * @adapter: board private structure to initialize
1133 *
1134 * We allocate one ring per queue at run-time since we don't know the
1135 * number of queues at compile-time. The polling_netdev array is
1136 * intended for Multiqueue, but should work fine with a single queue.
1137 **/
1138static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1139{
Jacob Keller65c70062017-06-07 05:43:01 -04001140 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001141
Alan Brady5b36e8d2017-08-22 06:57:50 -04001142 /* If we're in reset reallocating queues we don't actually know yet for
1143 * certain the PF gave us the number of queues we asked for but we'll
1144 * assume it did. Once basic reset is finished we'll confirm once we
1145 * start negotiating config with PF.
1146 */
1147 if (adapter->num_req_queues)
1148 num_active_queues = adapter->num_req_queues;
1149 else
1150 num_active_queues = min_t(int,
1151 adapter->vsi_res->num_queue_pairs,
1152 (int)(num_online_cpus()));
1153
Jacob Keller65c70062017-06-07 05:43:01 -04001154
1155 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001156 sizeof(struct i40e_ring), GFP_KERNEL);
1157 if (!adapter->tx_rings)
1158 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001159 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001160 sizeof(struct i40e_ring), GFP_KERNEL);
1161 if (!adapter->rx_rings)
1162 goto err_out;
1163
Jacob Keller65c70062017-06-07 05:43:01 -04001164 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001165 struct i40e_ring *tx_ring;
1166 struct i40e_ring *rx_ring;
1167
Mitch Williams0dd438d2015-10-26 19:44:40 -04001168 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001169
1170 tx_ring->queue_index = i;
1171 tx_ring->netdev = adapter->netdev;
1172 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001173 tx_ring->count = adapter->tx_desc_count;
Alexander Duyck40588ca2017-12-29 08:49:28 -05001174 tx_ring->itr_setting = I40E_ITR_TX_DEF;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001175 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001176 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001177
Mitch Williams0dd438d2015-10-26 19:44:40 -04001178 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001179 rx_ring->queue_index = i;
1180 rx_ring->netdev = adapter->netdev;
1181 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001182 rx_ring->count = adapter->rx_desc_count;
Alexander Duyck40588ca2017-12-29 08:49:28 -05001183 rx_ring->itr_setting = I40E_ITR_RX_DEF;
Greg Rose5eae00c2013-12-21 06:12:45 +00001184 }
1185
Jacob Keller65c70062017-06-07 05:43:01 -04001186 adapter->num_active_queues = num_active_queues;
1187
Greg Rose5eae00c2013-12-21 06:12:45 +00001188 return 0;
1189
1190err_out:
1191 i40evf_free_queues(adapter);
1192 return -ENOMEM;
1193}
1194
1195/**
1196 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1197 * @adapter: board private structure to initialize
1198 *
1199 * Attempt to configure the interrupts using the best available
1200 * capabilities of the hardware and the kernel.
1201 **/
1202static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1203{
1204 int vector, v_budget;
1205 int pairs = 0;
1206 int err = 0;
1207
1208 if (!adapter->vsi_res) {
1209 err = -EIO;
1210 goto out;
1211 }
Mitch Williamscc052922014-10-25 03:24:34 +00001212 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001213
Jacob Keller789f38c2017-04-19 09:25:56 -04001214 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1215 * us much good if we have more vectors than CPUs. However, we already
1216 * limit the total number of queues by the number of CPUs so we do not
1217 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001218 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001219 v_budget = min_t(int, pairs + NONQ_VECS,
1220 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001221
Greg Rose5eae00c2013-12-21 06:12:45 +00001222 adapter->msix_entries = kcalloc(v_budget,
1223 sizeof(struct msix_entry), GFP_KERNEL);
1224 if (!adapter->msix_entries) {
1225 err = -ENOMEM;
1226 goto out;
1227 }
1228
1229 for (vector = 0; vector < v_budget; vector++)
1230 adapter->msix_entries[vector].entry = vector;
1231
Mitch Williams313ed2d2015-08-27 11:42:31 -04001232 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001233
1234out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001235 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1236 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001237 return err;
1238}
1239
1240/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001241 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1242 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001243 *
1244 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001245 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001246static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001247{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001248 struct i40e_aqc_get_set_rss_key_data *rss_key =
1249 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001250 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001251 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001252
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001253 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001254 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001255 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001256 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001257 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001258 }
1259
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001260 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1261 if (ret) {
1262 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1263 i40evf_stat_str(hw, ret),
1264 i40evf_aq_str(hw, hw->aq.asq_last_status));
1265 return ret;
1266
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001267 }
1268
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001269 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1270 adapter->rss_lut, adapter->rss_lut_size);
1271 if (ret) {
1272 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1273 i40evf_stat_str(hw, ret),
1274 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001275 }
1276
1277 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001278
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001279}
1280
1281/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001282 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001283 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001284 *
1285 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001286 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001287static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001288{
1289 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001290 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001291 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001292
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001293 dw = (u32 *)adapter->rss_key;
1294 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1295 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001296
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001297 dw = (u32 *)adapter->rss_lut;
1298 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1299 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001300
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001301 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001302
1303 return 0;
1304}
1305
1306/**
1307 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001308 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001309 *
1310 * Returns 0 on success, negative on failure
1311 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001312int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001313{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001314
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001315 if (RSS_PF(adapter)) {
1316 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1317 I40EVF_FLAG_AQ_SET_RSS_KEY;
1318 return 0;
1319 } else if (RSS_AQ(adapter)) {
1320 return i40evf_config_rss_aq(adapter);
1321 } else {
1322 return i40evf_config_rss_reg(adapter);
1323 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001324}
1325
1326/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001327 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001328 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001329 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001330static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001331{
1332 u16 i;
1333
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001334 for (i = 0; i < adapter->rss_lut_size; i++)
1335 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001336}
1337
1338/**
Helin Zhang96a81982015-10-26 19:44:31 -04001339 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001340 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001341 *
1342 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001343 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001344static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001345{
1346 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001347 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001348
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001349 if (!RSS_PF(adapter)) {
1350 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001351 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001352 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001353 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1354 else
1355 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001356
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001357 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1358 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1359 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001360
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001361 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001362
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001363 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1364 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001365
1366 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001367}
1368
1369/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001370 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1371 * @adapter: board private structure to initialize
1372 *
1373 * We allocate one q_vector per queue interrupt. If allocation fails we
1374 * return -ENOMEM.
1375 **/
1376static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1377{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001378 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001379 struct i40e_q_vector *q_vector;
1380
1381 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001382 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001383 GFP_KERNEL);
1384 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001385 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001386
1387 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001388 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001389 q_vector->adapter = adapter;
1390 q_vector->vsi = &adapter->vsi;
1391 q_vector->v_idx = q_idx;
Alexander Duycka3f9fb52017-12-29 08:48:53 -05001392 q_vector->reg_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001393 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001394 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001395 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001396 }
1397
1398 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001399}
1400
1401/**
1402 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1403 * @adapter: board private structure to initialize
1404 *
1405 * This function frees the memory allocated to the q_vectors. In addition if
1406 * NAPI is enabled it will delete any references to the NAPI struct prior
1407 * to freeing the q_vector.
1408 **/
1409static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1410{
1411 int q_idx, num_q_vectors;
1412 int napi_vectors;
1413
Jacob Kelleref4603e2016-11-08 13:05:08 -08001414 if (!adapter->q_vectors)
1415 return;
1416
Greg Rose5eae00c2013-12-21 06:12:45 +00001417 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001418 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001419
1420 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001421 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001422 if (q_idx < napi_vectors)
1423 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001424 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001425 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001426 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001427}
1428
1429/**
1430 * i40evf_reset_interrupt_capability - Reset MSIX setup
1431 * @adapter: board private structure
1432 *
1433 **/
1434void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1435{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001436 if (!adapter->msix_entries)
1437 return;
1438
Greg Rose5eae00c2013-12-21 06:12:45 +00001439 pci_disable_msix(adapter->pdev);
1440 kfree(adapter->msix_entries);
1441 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001442}
1443
1444/**
1445 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1446 * @adapter: board private structure to initialize
1447 *
1448 **/
1449int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1450{
1451 int err;
1452
Jacob Keller283aeaf2017-04-19 09:25:59 -04001453 err = i40evf_alloc_queues(adapter);
1454 if (err) {
1455 dev_err(&adapter->pdev->dev,
1456 "Unable to allocate memory for queues\n");
1457 goto err_alloc_queues;
1458 }
1459
Jacob Keller62fe2a82016-07-27 12:02:33 -07001460 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001461 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001462 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001463 if (err) {
1464 dev_err(&adapter->pdev->dev,
1465 "Unable to setup interrupt capabilities\n");
1466 goto err_set_interrupt;
1467 }
1468
1469 err = i40evf_alloc_q_vectors(adapter);
1470 if (err) {
1471 dev_err(&adapter->pdev->dev,
1472 "Unable to allocate memory for queue vectors\n");
1473 goto err_alloc_q_vectors;
1474 }
1475
Greg Rose5eae00c2013-12-21 06:12:45 +00001476 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001477 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1478 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001479
1480 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001481err_alloc_q_vectors:
1482 i40evf_reset_interrupt_capability(adapter);
1483err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001484 i40evf_free_queues(adapter);
1485err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001486 return err;
1487}
1488
1489/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001490 * i40evf_free_rss - Free memory used by RSS structs
1491 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001492 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001493static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001494{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001495 kfree(adapter->rss_key);
1496 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001497
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001498 kfree(adapter->rss_lut);
1499 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001500}
1501
1502/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001503 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1504 * @adapter: board private structure
1505 *
1506 * Returns 0 on success, negative on failure
1507 **/
1508static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1509{
1510 struct net_device *netdev = adapter->netdev;
1511 int err;
1512
1513 if (netif_running(netdev))
1514 i40evf_free_traffic_irqs(adapter);
1515 i40evf_free_misc_irq(adapter);
1516 i40evf_reset_interrupt_capability(adapter);
1517 i40evf_free_q_vectors(adapter);
1518 i40evf_free_queues(adapter);
1519
1520 err = i40evf_init_interrupt_scheme(adapter);
1521 if (err)
1522 goto err;
1523
1524 netif_tx_stop_all_queues(netdev);
1525
1526 err = i40evf_request_misc_irq(adapter);
1527 if (err)
1528 goto err;
1529
1530 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1531
Mitch Williams1b7b7592017-08-22 06:57:51 -04001532 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001533
1534 if (RSS_AQ(adapter))
1535 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1536 else
1537 err = i40evf_init_rss(adapter);
1538err:
1539 return err;
1540}
1541
1542/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001543 * i40evf_watchdog_timer - Periodic call-back timer
1544 * @data: pointer to adapter disguised as unsigned long
1545 **/
Kees Cook26566ea2017-10-16 17:29:35 -07001546static void i40evf_watchdog_timer(struct timer_list *t)
Greg Rose5eae00c2013-12-21 06:12:45 +00001547{
Kees Cook26566ea2017-10-16 17:29:35 -07001548 struct i40evf_adapter *adapter = from_timer(adapter, t,
1549 watchdog_timer);
Mitch Williams75a64432014-11-11 20:02:42 +00001550
Greg Rose5eae00c2013-12-21 06:12:45 +00001551 schedule_work(&adapter->watchdog_task);
1552 /* timer will be rescheduled in watchdog task */
1553}
1554
1555/**
1556 * i40evf_watchdog_task - Periodic call-back task
1557 * @work: pointer to work_struct
1558 **/
1559static void i40evf_watchdog_task(struct work_struct *work)
1560{
1561 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001562 struct i40evf_adapter,
1563 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001564 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001565 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001566
Greg Rose5eae00c2013-12-21 06:12:45 +00001567 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001568 goto restart_watchdog;
1569
1570 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001571 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1572 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001573 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1574 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001575 /* A chance for redemption! */
1576 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1577 adapter->state = __I40EVF_STARTUP;
1578 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1579 schedule_delayed_work(&adapter->init_task, 10);
1580 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1581 &adapter->crit_section);
1582 /* Don't reschedule the watchdog, since we've restarted
1583 * the init task. When init_task contacts the PF and
1584 * gets everything set up again, it'll restart the
1585 * watchdog for us. Down, boy. Sit. Stay. Woof.
1586 */
1587 return;
1588 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001589 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001590 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001591 goto watchdog_done;
1592 }
1593
1594 if ((adapter->state < __I40EVF_DOWN) ||
1595 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001596 goto watchdog_done;
1597
Mitch Williamsef8693e2014-02-13 03:48:53 -08001598 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001599 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1600 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001601 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001602 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001603 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001604 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001605 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001606 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001607 goto watchdog_done;
1608 }
1609
1610 /* Process admin queue tasks. After init, everything gets done
1611 * here so we don't race on the admin queue.
1612 */
Mitch Williamsed636962015-04-07 19:45:32 -04001613 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001614 if (!i40evf_asq_done(hw)) {
1615 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1616 i40evf_send_api_ver(adapter);
1617 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001618 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001619 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001620 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1621 i40evf_send_vf_config_msg(adapter);
1622 goto watchdog_done;
1623 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001624
Mitch Williamse284fc82015-03-27 00:12:09 -07001625 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1626 i40evf_disable_queues(adapter);
1627 goto watchdog_done;
1628 }
1629
Greg Rose5eae00c2013-12-21 06:12:45 +00001630 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1631 i40evf_map_queues(adapter);
1632 goto watchdog_done;
1633 }
1634
1635 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1636 i40evf_add_ether_addrs(adapter);
1637 goto watchdog_done;
1638 }
1639
1640 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1641 i40evf_add_vlans(adapter);
1642 goto watchdog_done;
1643 }
1644
1645 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1646 i40evf_del_ether_addrs(adapter);
1647 goto watchdog_done;
1648 }
1649
1650 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1651 i40evf_del_vlans(adapter);
1652 goto watchdog_done;
1653 }
1654
Mariusz Stachura87743702017-07-17 22:09:45 -07001655 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1656 i40evf_enable_vlan_stripping(adapter);
1657 goto watchdog_done;
1658 }
1659
1660 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1661 i40evf_disable_vlan_stripping(adapter);
1662 goto watchdog_done;
1663 }
1664
Greg Rose5eae00c2013-12-21 06:12:45 +00001665 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1666 i40evf_configure_queues(adapter);
1667 goto watchdog_done;
1668 }
1669
1670 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1671 i40evf_enable_queues(adapter);
1672 goto watchdog_done;
1673 }
1674
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001675 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1676 /* This message goes straight to the firmware, not the
1677 * PF, so we don't have to set current_op as we will
1678 * not get a response through the ARQ.
1679 */
Helin Zhang96a81982015-10-26 19:44:31 -04001680 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001681 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1682 goto watchdog_done;
1683 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001684 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1685 i40evf_get_hena(adapter);
1686 goto watchdog_done;
1687 }
1688 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1689 i40evf_set_hena(adapter);
1690 goto watchdog_done;
1691 }
1692 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1693 i40evf_set_rss_key(adapter);
1694 goto watchdog_done;
1695 }
1696 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1697 i40evf_set_rss_lut(adapter);
1698 goto watchdog_done;
1699 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001700
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001701 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001702 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1703 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001704 goto watchdog_done;
1705 }
1706
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001707 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001708 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001709 goto watchdog_done;
1710 }
1711
1712 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1713 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001714 i40evf_set_promiscuous(adapter, 0);
1715 goto watchdog_done;
1716 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001717 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001718
Greg Rose5eae00c2013-12-21 06:12:45 +00001719 if (adapter->state == __I40EVF_RUNNING)
1720 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001721watchdog_done:
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05001722 if (adapter->state == __I40EVF_RUNNING)
1723 i40evf_detect_recover_hung(&adapter->vsi);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001724 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1725restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001726 if (adapter->state == __I40EVF_REMOVE)
1727 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001728 if (adapter->aq_required)
1729 mod_timer(&adapter->watchdog_timer,
1730 jiffies + msecs_to_jiffies(20));
1731 else
1732 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001733 schedule_work(&adapter->adminq_task);
1734}
1735
Joe Perchesdedecb62016-11-01 15:35:14 -07001736static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1737{
1738 struct i40evf_mac_filter *f, *ftmp;
1739 struct i40evf_vlan_filter *fv, *fvtmp;
1740
1741 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1742
Jacob Keller44b034b2017-10-27 11:06:49 -04001743 /* We don't use netif_running() because it may be true prior to
1744 * ndo_open() returning, so we can't assume it means all our open
1745 * tasks have finished, since we're not holding the rtnl_lock here.
1746 */
1747 if (adapter->state == __I40EVF_RUNNING) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001748 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001749 netif_carrier_off(adapter->netdev);
1750 netif_tx_disable(adapter->netdev);
1751 adapter->link_up = false;
1752 i40evf_napi_disable_all(adapter);
1753 i40evf_irq_disable(adapter);
1754 i40evf_free_traffic_irqs(adapter);
1755 i40evf_free_all_tx_resources(adapter);
1756 i40evf_free_all_rx_resources(adapter);
1757 }
1758
Jacob Keller504398f2017-10-27 11:06:50 -04001759 spin_lock_bh(&adapter->mac_vlan_list_lock);
1760
Joe Perchesdedecb62016-11-01 15:35:14 -07001761 /* Delete all of the filters, both MAC and VLAN. */
1762 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1763 list_del(&f->list);
1764 kfree(f);
1765 }
1766
1767 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1768 list_del(&fv->list);
1769 kfree(fv);
1770 }
1771
Jacob Keller504398f2017-10-27 11:06:50 -04001772 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1773
Joe Perchesdedecb62016-11-01 15:35:14 -07001774 i40evf_free_misc_irq(adapter);
1775 i40evf_reset_interrupt_capability(adapter);
1776 i40evf_free_queues(adapter);
1777 i40evf_free_q_vectors(adapter);
1778 kfree(adapter->vf_res);
1779 i40evf_shutdown_adminq(&adapter->hw);
1780 adapter->netdev->flags &= ~IFF_UP;
1781 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1782 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1783 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001784 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001785 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1786}
1787
Mitch Williams67c818a2015-06-19 08:56:30 -07001788#define I40EVF_RESET_WAIT_MS 10
1789#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001790/**
1791 * i40evf_reset_task - Call-back task to handle hardware reset
1792 * @work: pointer to work_struct
1793 *
1794 * During reset we need to shut down and reinitialize the admin queue
1795 * before we can use it to communicate with the PF again. We also clear
1796 * and reinit the rings because that context is lost as well.
1797 **/
1798static void i40evf_reset_task(struct work_struct *work)
1799{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001800 struct i40evf_adapter *adapter = container_of(work,
1801 struct i40evf_adapter,
1802 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001803 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001804 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001805 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001806 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001807 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001808 int i = 0, err;
Jacob Keller44b034b2017-10-27 11:06:49 -04001809 bool running;
Greg Rose5eae00c2013-12-21 06:12:45 +00001810
Avinash Dayanand06aa0402017-12-18 05:16:43 -05001811 /* When device is being removed it doesn't make sense to run the reset
1812 * task, just return in such a case.
1813 */
1814 if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1815 return;
1816
Mitch Williamsed0e8942017-01-24 10:23:59 -08001817 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001818 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001819 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001820 if (CLIENT_ENABLED(adapter)) {
1821 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1822 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1823 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1824 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1825 cancel_delayed_work_sync(&adapter->client_task);
1826 i40evf_notify_client_close(&adapter->vsi, true);
1827 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001828 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001829 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001830 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1831 /* Restart the AQ here. If we have been reset but didn't
1832 * detect it, or if the PF had to reinit, our AQ will be hosed.
1833 */
1834 i40evf_shutdown_adminq(hw);
1835 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001836 i40evf_request_reset(adapter);
1837 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001838 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001839
Mitch Williamsef8693e2014-02-13 03:48:53 -08001840 /* poll until we see the reset actually happen */
1841 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001842 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1843 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1844 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001845 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001846 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001847 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001848 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001849 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001850 goto continue_reset; /* act like the reset happened */
1851 }
1852
1853 /* wait until the reset is complete and the PF is responding to us */
1854 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001855 /* sleep first to make sure a minimum wait time is met */
1856 msleep(I40EVF_RESET_WAIT_MS);
1857
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001858 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1859 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001860 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001861 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001862 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001863
Mitch Williams509a4472015-12-23 12:05:52 -08001864 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001865
Mitch Williamsef8693e2014-02-13 03:48:53 -08001866 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001867 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001868 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001869 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001870 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001871 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001872 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001873
1874continue_reset:
Jacob Keller44b034b2017-10-27 11:06:49 -04001875 /* We don't use netif_running() because it may be true prior to
1876 * ndo_open() returning, so we can't assume it means all our open
1877 * tasks have finished, since we're not holding the rtnl_lock here.
1878 */
1879 running = (adapter->state == __I40EVF_RUNNING);
1880
1881 if (running) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001882 netif_carrier_off(netdev);
1883 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001884 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001885 i40evf_napi_disable_all(adapter);
1886 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001887 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001888
Greg Rose5eae00c2013-12-21 06:12:45 +00001889 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001890 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1891
1892 /* free the Tx/Rx rings and descriptors, might be better to just
1893 * re-use them sometime in the future
1894 */
1895 i40evf_free_all_rx_resources(adapter);
1896 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001897
1898 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001899 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001900 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001901 err = i40evf_init_adminq(hw);
1902 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001903 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1904 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001905 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001906
Alan Brady5b36e8d2017-08-22 06:57:50 -04001907 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1908 err = i40evf_reinit_interrupt_scheme(adapter);
1909 if (err)
1910 goto reset_err;
1911 }
1912
1913 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001914 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001915
Jacob Keller504398f2017-10-27 11:06:50 -04001916 spin_lock_bh(&adapter->mac_vlan_list_lock);
1917
Mitch Williamsac833bb2015-01-29 07:17:19 +00001918 /* re-add all MAC filters */
1919 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1920 f->add = true;
1921 }
1922 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001923 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1924 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001925 }
Jacob Keller504398f2017-10-27 11:06:50 -04001926
1927 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1928
Mitch Williamse6d038d2015-06-04 16:23:58 -04001929 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001930 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Mitch Williams67c818a2015-06-19 08:56:30 -07001931 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001932
1933 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1934
Jacob Keller44b034b2017-10-27 11:06:49 -04001935 /* We were running when the reset started, so we need to restore some
1936 * state here.
1937 */
1938 if (running) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001939 /* allocate transmit descriptors */
1940 err = i40evf_setup_all_tx_resources(adapter);
1941 if (err)
1942 goto reset_err;
1943
1944 /* allocate receive descriptors */
1945 err = i40evf_setup_all_rx_resources(adapter);
1946 if (err)
1947 goto reset_err;
1948
Alan Brady5b36e8d2017-08-22 06:57:50 -04001949 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1950 err = i40evf_request_traffic_irqs(adapter,
1951 netdev->name);
1952 if (err)
1953 goto reset_err;
1954
1955 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1956 }
1957
Greg Rose5eae00c2013-12-21 06:12:45 +00001958 i40evf_configure(adapter);
1959
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001960 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001961
1962 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001963 } else {
1964 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001965 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001966 }
Jacob Keller9b2aef12017-10-27 11:06:52 -04001967 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1968 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001969
Greg Rose5eae00c2013-12-21 06:12:45 +00001970 return;
1971reset_err:
Jacob Keller9b2aef12017-10-27 11:06:52 -04001972 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1973 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams80e72892014-05-10 04:49:06 +00001974 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001975 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001976}
1977
1978/**
1979 * i40evf_adminq_task - worker thread to clean the admin queue
1980 * @work: pointer to work_struct containing our data
1981 **/
1982static void i40evf_adminq_task(struct work_struct *work)
1983{
1984 struct i40evf_adapter *adapter =
1985 container_of(work, struct i40evf_adapter, adminq_task);
1986 struct i40e_hw *hw = &adapter->hw;
1987 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001988 enum virtchnl_ops v_op;
1989 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001990 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001991 u16 pending;
1992
Mitch Williamsef8693e2014-02-13 03:48:53 -08001993 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001994 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001995
Mitch Williams1001dc32014-11-11 20:02:19 +00001996 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1997 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001998 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001999 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00002000
Greg Rose5eae00c2013-12-21 06:12:45 +00002001 do {
2002 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002003 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2004 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2005
2006 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002007 break; /* No event to process or error cleaning ARQ */
2008
Tushar Davec969ef42017-06-22 09:44:32 -07002009 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002010 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002011 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002012 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002013 } while (pending);
2014
Mitch Williams67c818a2015-06-19 08:56:30 -07002015 if ((adapter->flags &
2016 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2017 adapter->state == __I40EVF_RESETTING)
2018 goto freedom;
2019
Mitch Williams912257e2014-05-22 06:32:07 +00002020 /* check for error indications */
2021 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002022 if (val == 0xdeadbeef) /* indicates device in reset */
2023 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002024 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002025 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002026 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002027 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002028 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002029 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002030 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002031 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002032 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002033 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002034 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002035 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002036 }
2037 if (oldval != val)
2038 wr32(hw, hw->aq.arq.len, val);
2039
2040 val = rd32(hw, hw->aq.asq.len);
2041 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002042 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002043 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002044 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002045 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002046 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002047 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002048 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002049 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002050 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002051 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002052 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002053 }
2054 if (oldval != val)
2055 wr32(hw, hw->aq.asq.len, val);
2056
Mitch Williams67c818a2015-06-19 08:56:30 -07002057freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002058 kfree(event.msg_buf);
2059out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002060 /* re-enable Admin queue interrupt cause */
2061 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002062}
2063
2064/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002065 * i40evf_client_task - worker thread to perform client work
2066 * @work: pointer to work_struct containing our data
2067 *
2068 * This task handles client interactions. Because client calls can be
2069 * reentrant, we can't handle them in the watchdog.
2070 **/
2071static void i40evf_client_task(struct work_struct *work)
2072{
2073 struct i40evf_adapter *adapter =
2074 container_of(work, struct i40evf_adapter, client_task.work);
2075
2076 /* If we can't get the client bit, just give up. We'll be rescheduled
2077 * later.
2078 */
2079
2080 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2081 return;
2082
2083 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2084 i40evf_client_subtask(adapter);
2085 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2086 goto out;
2087 }
Alan Brady01acc732017-11-14 07:00:51 -05002088 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2089 i40evf_notify_client_l2_params(&adapter->vsi);
2090 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2091 goto out;
2092 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002093 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2094 i40evf_notify_client_close(&adapter->vsi, false);
2095 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2096 goto out;
2097 }
2098 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2099 i40evf_notify_client_open(&adapter->vsi);
2100 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002101 }
2102out:
2103 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2104}
2105
2106/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002107 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2108 * @adapter: board private structure
2109 *
2110 * Free all transmit software resources
2111 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002112void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002113{
2114 int i;
2115
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002116 if (!adapter->tx_rings)
2117 return;
2118
Mitch Williamscc052922014-10-25 03:24:34 +00002119 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002120 if (adapter->tx_rings[i].desc)
2121 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002122}
2123
2124/**
2125 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2126 * @adapter: board private structure
2127 *
2128 * If this function returns with an error, then it's possible one or
2129 * more of the rings is populated (while the rest are not). It is the
2130 * callers duty to clean those orphaned rings.
2131 *
2132 * Return 0 on success, negative on failure
2133 **/
2134static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2135{
2136 int i, err = 0;
2137
Mitch Williamscc052922014-10-25 03:24:34 +00002138 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002139 adapter->tx_rings[i].count = adapter->tx_desc_count;
2140 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002141 if (!err)
2142 continue;
2143 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002144 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002145 break;
2146 }
2147
2148 return err;
2149}
2150
2151/**
2152 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2153 * @adapter: board private structure
2154 *
2155 * If this function returns with an error, then it's possible one or
2156 * more of the rings is populated (while the rest are not). It is the
2157 * callers duty to clean those orphaned rings.
2158 *
2159 * Return 0 on success, negative on failure
2160 **/
2161static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2162{
2163 int i, err = 0;
2164
Mitch Williamscc052922014-10-25 03:24:34 +00002165 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002166 adapter->rx_rings[i].count = adapter->rx_desc_count;
2167 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002168 if (!err)
2169 continue;
2170 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002171 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002172 break;
2173 }
2174 return err;
2175}
2176
2177/**
2178 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2179 * @adapter: board private structure
2180 *
2181 * Free all receive software resources
2182 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002183void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002184{
2185 int i;
2186
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002187 if (!adapter->rx_rings)
2188 return;
2189
Mitch Williamscc052922014-10-25 03:24:34 +00002190 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002191 if (adapter->rx_rings[i].desc)
2192 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002193}
2194
2195/**
2196 * i40evf_open - Called when a network interface is made active
2197 * @netdev: network interface device structure
2198 *
2199 * Returns 0 on success, negative value on failure
2200 *
2201 * The open entry point is called when a network interface is made
2202 * active by the system (IFF_UP). At this point all resources needed
2203 * for transmit and receive operations are allocated, the interrupt
2204 * handler is registered with the OS, the watchdog timer is started,
2205 * and the stack is notified that the interface is ready.
2206 **/
2207static int i40evf_open(struct net_device *netdev)
2208{
2209 struct i40evf_adapter *adapter = netdev_priv(netdev);
2210 int err;
2211
Mitch Williamsef8693e2014-02-13 03:48:53 -08002212 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2213 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2214 return -EIO;
2215 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002216
Jacob Keller9b2aef12017-10-27 11:06:52 -04002217 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2218 &adapter->crit_section))
2219 usleep_range(500, 1000);
2220
2221 if (adapter->state != __I40EVF_DOWN) {
2222 err = -EBUSY;
2223 goto err_unlock;
2224 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002225
2226 /* allocate transmit descriptors */
2227 err = i40evf_setup_all_tx_resources(adapter);
2228 if (err)
2229 goto err_setup_tx;
2230
2231 /* allocate receive descriptors */
2232 err = i40evf_setup_all_rx_resources(adapter);
2233 if (err)
2234 goto err_setup_rx;
2235
2236 /* clear any pending interrupts, may auto mask */
2237 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2238 if (err)
2239 goto err_req_irq;
2240
Mitch Williams44151cd2015-04-27 14:57:17 -04002241 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002242 i40evf_configure(adapter);
2243
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002244 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002245
2246 i40evf_irq_enable(adapter, true);
2247
Jacob Keller9b2aef12017-10-27 11:06:52 -04002248 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2249
Greg Rose5eae00c2013-12-21 06:12:45 +00002250 return 0;
2251
2252err_req_irq:
2253 i40evf_down(adapter);
2254 i40evf_free_traffic_irqs(adapter);
2255err_setup_rx:
2256 i40evf_free_all_rx_resources(adapter);
2257err_setup_tx:
2258 i40evf_free_all_tx_resources(adapter);
Jacob Keller9b2aef12017-10-27 11:06:52 -04002259err_unlock:
2260 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00002261
2262 return err;
2263}
2264
2265/**
2266 * i40evf_close - Disables a network interface
2267 * @netdev: network interface device structure
2268 *
2269 * Returns 0, this is not allowed to fail
2270 *
2271 * The close entry point is called when an interface is de-activated
2272 * by the OS. The hardware is still under the drivers control, but
2273 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2274 * are freed, along with all transmit and receive resources.
2275 **/
2276static int i40evf_close(struct net_device *netdev)
2277{
2278 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002279 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002280
Mitch Williams209dc4d2015-12-09 15:50:27 -08002281 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002282 return 0;
2283
Jacob Keller9b2aef12017-10-27 11:06:52 -04002284 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2285 &adapter->crit_section))
2286 usleep_range(500, 1000);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002287
Jacob Keller0da36b92017-04-19 09:25:55 -04002288 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002289 if (CLIENT_ENABLED(adapter))
2290 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002291
2292 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002293 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002294 i40evf_free_traffic_irqs(adapter);
2295
Jacob Keller9b2aef12017-10-27 11:06:52 -04002296 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2297
Mitch Williams51f38262016-12-12 15:44:11 -08002298 /* We explicitly don't free resources here because the hardware is
2299 * still active and can DMA into memory. Resources are cleared in
2300 * i40evf_virtchnl_completion() after we get confirmation from the PF
2301 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002302 *
2303 * Also, we wait for state to transition to __I40EVF_DOWN before
2304 * returning. State change occurs in i40evf_virtchnl_completion() after
2305 * VF resources are released (which occurs after PF driver processes and
2306 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002307 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002308
2309 status = wait_event_timeout(adapter->down_waitqueue,
2310 adapter->state == __I40EVF_DOWN,
2311 msecs_to_jiffies(200));
2312 if (!status)
2313 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002314 return 0;
2315}
2316
2317/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002318 * i40evf_change_mtu - Change the Maximum Transfer Unit
2319 * @netdev: network interface device structure
2320 * @new_mtu: new value for maximum frame size
2321 *
2322 * Returns 0 on success, negative on failure
2323 **/
2324static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2325{
2326 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002327
Greg Rose5eae00c2013-12-21 06:12:45 +00002328 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002329 if (CLIENT_ENABLED(adapter)) {
2330 i40evf_notify_client_l2_params(&adapter->vsi);
2331 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2332 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002333 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2334 schedule_work(&adapter->reset_task);
2335
Greg Rose5eae00c2013-12-21 06:12:45 +00002336 return 0;
2337}
2338
Alexander Duyck06fc0162016-10-25 16:08:47 -07002339/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002340 * i40e_set_features - set the netdev feature flags
2341 * @netdev: ptr to the netdev being adjusted
2342 * @features: the feature set that the stack is suggesting
2343 * Note: expects to be called while under rtnl_lock()
2344 **/
2345static int i40evf_set_features(struct net_device *netdev,
2346 netdev_features_t features)
2347{
2348 struct i40evf_adapter *adapter = netdev_priv(netdev);
2349
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002350 /* Don't allow changing VLAN_RX flag when VLAN is set for VF
2351 * and return an error in this case
2352 */
2353 if (VLAN_ALLOWED(adapter)) {
2354 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2355 adapter->aq_required |=
2356 I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2357 else
2358 adapter->aq_required |=
2359 I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2360 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
Mariusz Stachura87743702017-07-17 22:09:45 -07002361 return -EINVAL;
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002362 }
Mariusz Stachura87743702017-07-17 22:09:45 -07002363
2364 return 0;
2365}
2366
2367/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002368 * i40evf_features_check - Validate encapsulated packet conforms to limits
2369 * @skb: skb buff
2370 * @netdev: This physical port's netdev
2371 * @features: Offload features that the stack believes apply
2372 **/
2373static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2374 struct net_device *dev,
2375 netdev_features_t features)
2376{
2377 size_t len;
2378
2379 /* No point in doing any of this if neither checksum nor GSO are
2380 * being requested for this frame. We can rule out both by just
2381 * checking for CHECKSUM_PARTIAL
2382 */
2383 if (skb->ip_summed != CHECKSUM_PARTIAL)
2384 return features;
2385
2386 /* We cannot support GSO if the MSS is going to be less than
2387 * 64 bytes. If it is then we need to drop support for GSO.
2388 */
2389 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2390 features &= ~NETIF_F_GSO_MASK;
2391
2392 /* MACLEN can support at most 63 words */
2393 len = skb_network_header(skb) - skb->data;
2394 if (len & ~(63 * 2))
2395 goto out_err;
2396
2397 /* IPLEN and EIPLEN can support at most 127 dwords */
2398 len = skb_transport_header(skb) - skb_network_header(skb);
2399 if (len & ~(127 * 4))
2400 goto out_err;
2401
2402 if (skb->encapsulation) {
2403 /* L4TUNLEN can support 127 words */
2404 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2405 if (len & ~(127 * 2))
2406 goto out_err;
2407
2408 /* IPLEN can support at most 127 dwords */
2409 len = skb_inner_transport_header(skb) -
2410 skb_inner_network_header(skb);
2411 if (len & ~(127 * 4))
2412 goto out_err;
2413 }
2414
2415 /* No need to validate L4LEN as TCP is the only protocol with a
2416 * a flexible value and we support all possible values supported
2417 * by TCP, which is at most 15 dwords
2418 */
2419
2420 return features;
2421out_err:
2422 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2423}
2424
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002425/**
2426 * i40evf_fix_features - fix up the netdev feature bits
2427 * @netdev: our net device
2428 * @features: desired feature bits
2429 *
2430 * Returns fixed-up features bits
2431 **/
2432static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2433 netdev_features_t features)
2434{
2435 struct i40evf_adapter *adapter = netdev_priv(netdev);
2436
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002437 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
2438 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
2439 NETIF_F_HW_VLAN_CTAG_RX |
2440 NETIF_F_HW_VLAN_CTAG_FILTER);
2441
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002442 return features;
2443}
2444
Greg Rose5eae00c2013-12-21 06:12:45 +00002445static const struct net_device_ops i40evf_netdev_ops = {
2446 .ndo_open = i40evf_open,
2447 .ndo_stop = i40evf_close,
2448 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002449 .ndo_set_rx_mode = i40evf_set_rx_mode,
2450 .ndo_validate_addr = eth_validate_addr,
2451 .ndo_set_mac_address = i40evf_set_mac,
2452 .ndo_change_mtu = i40evf_change_mtu,
2453 .ndo_tx_timeout = i40evf_tx_timeout,
2454 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2455 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002456 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002457 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002458 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002459#ifdef CONFIG_NET_POLL_CONTROLLER
2460 .ndo_poll_controller = i40evf_netpoll,
2461#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002462};
2463
2464/**
2465 * i40evf_check_reset_complete - check that VF reset is complete
2466 * @hw: pointer to hw struct
2467 *
2468 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2469 **/
2470static int i40evf_check_reset_complete(struct i40e_hw *hw)
2471{
2472 u32 rstat;
2473 int i;
2474
2475 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002476 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2477 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002478 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2479 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002480 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002481 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002482 }
2483 return -EBUSY;
2484}
2485
2486/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002487 * i40evf_process_config - Process the config information we got from the PF
2488 * @adapter: board private structure
2489 *
2490 * Verify that we have a valid config struct, and set up our netdev features
2491 * and our VSI struct.
2492 **/
2493int i40evf_process_config(struct i40evf_adapter *adapter)
2494{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002495 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002496 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002497 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002498 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002499 netdev_features_t hw_enc_features;
2500 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002501
2502 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002503 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002504 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002505 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002506 }
2507 if (!adapter->vsi_res) {
2508 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2509 return -ENODEV;
2510 }
2511
Alan Brady5b36e8d2017-08-22 06:57:50 -04002512 if (num_req_queues &&
2513 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2514 /* Problem. The PF gave us fewer queues than what we had
2515 * negotiated in our request. Need a reset to see if we can't
2516 * get back to a working state.
2517 */
2518 dev_err(&adapter->pdev->dev,
2519 "Requested %d queues, but PF only gave us %d.\n",
2520 num_req_queues,
2521 adapter->vsi_res->num_queue_pairs);
2522 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2523 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2524 i40evf_schedule_reset(adapter);
2525 return -ENODEV;
2526 }
2527 adapter->num_req_queues = 0;
2528
Preethi Banalabacd75c2017-03-27 14:43:18 -07002529 hw_enc_features = NETIF_F_SG |
2530 NETIF_F_IP_CSUM |
2531 NETIF_F_IPV6_CSUM |
2532 NETIF_F_HIGHDMA |
2533 NETIF_F_SOFT_FEATURES |
2534 NETIF_F_TSO |
2535 NETIF_F_TSO_ECN |
2536 NETIF_F_TSO6 |
2537 NETIF_F_SCTP_CRC |
2538 NETIF_F_RXHASH |
2539 NETIF_F_RXCSUM |
2540 0;
2541
2542 /* advertise to stack only if offloads for encapsulated packets is
2543 * supported
2544 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002545 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002546 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002547 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002548 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002549 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002550 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002551 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002552 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002553 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002554
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002555 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002556 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002557 netdev->gso_partial_features |=
2558 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002559
Preethi Banalabacd75c2017-03-27 14:43:18 -07002560 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2561 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2562 netdev->hw_enc_features |= hw_enc_features;
2563 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002564 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002565 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002566
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002567 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002568 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002569 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002570 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002571
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002572 /* Enable VLAN features if supported */
2573 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2574 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
2575 NETIF_F_HW_VLAN_CTAG_RX);
2576
Preethi Banalabacd75c2017-03-27 14:43:18 -07002577 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002578
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002579 netdev->features |= hw_features;
2580
2581 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2582 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002583
2584 adapter->vsi.id = adapter->vsi_res->vsi_id;
2585
2586 adapter->vsi.back = adapter;
2587 adapter->vsi.base_vector = 1;
2588 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002589 vsi->netdev = adapter->netdev;
2590 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002591 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002592 adapter->rss_key_size = vfres->rss_key_size;
2593 adapter->rss_lut_size = vfres->rss_lut_size;
2594 } else {
2595 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2596 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2597 }
2598
Mitch Williamse6d038d2015-06-04 16:23:58 -04002599 return 0;
2600}
2601
2602/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002603 * i40evf_init_task - worker thread to perform delayed initialization
2604 * @work: pointer to work_struct containing our data
2605 *
2606 * This task completes the work that was begun in probe. Due to the nature
2607 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002608 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002609 * whole system, we'll do it in a task so we can sleep.
2610 * This task only runs during driver init. Once we've established
2611 * communications with the PF driver and set up our netdev, the watchdog
2612 * takes over.
2613 **/
2614static void i40evf_init_task(struct work_struct *work)
2615{
2616 struct i40evf_adapter *adapter = container_of(work,
2617 struct i40evf_adapter,
2618 init_task.work);
2619 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002620 struct i40e_hw *hw = &adapter->hw;
2621 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002622 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002623
2624 switch (adapter->state) {
2625 case __I40EVF_STARTUP:
2626 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002627 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2628 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002629 err = i40e_set_mac_type(hw);
2630 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002631 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2632 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002633 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002634 }
2635 err = i40evf_check_reset_complete(hw);
2636 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002637 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002638 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002639 goto err;
2640 }
2641 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2642 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2643 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2644 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2645
2646 err = i40evf_init_adminq(hw);
2647 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002648 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2649 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002650 goto err;
2651 }
2652 err = i40evf_send_api_ver(adapter);
2653 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002654 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002655 i40evf_shutdown_adminq(hw);
2656 goto err;
2657 }
2658 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2659 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002660 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002661 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002662 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002663 i40evf_shutdown_adminq(hw);
2664 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002665 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002666 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002667
2668 /* aq msg sent, awaiting reply */
2669 err = i40evf_verify_api_ver(adapter);
2670 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002671 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002672 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002673 else
2674 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2675 adapter->pf_version.major,
2676 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002677 VIRTCHNL_VERSION_MAJOR,
2678 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002679 goto err;
2680 }
2681 err = i40evf_send_vf_config_msg(adapter);
2682 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002683 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002684 err);
2685 goto err;
2686 }
2687 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2688 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002689 case __I40EVF_INIT_GET_RESOURCES:
2690 /* aq msg sent, awaiting reply */
2691 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002692 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002693 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002694 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002695 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002696 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002697 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002698 }
2699 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002700 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002701 err = i40evf_send_vf_config_msg(adapter);
2702 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002703 } else if (err == I40E_ERR_PARAM) {
2704 /* We only get ERR_PARAM if the device is in a very bad
2705 * state or if we've been disabled for previous bad
2706 * behavior. Either way, we're done now.
2707 */
2708 i40evf_shutdown_adminq(hw);
2709 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2710 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002711 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002712 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002713 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2714 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002715 goto err_alloc;
2716 }
2717 adapter->state = __I40EVF_INIT_SW;
2718 break;
2719 default:
2720 goto err_alloc;
2721 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002722
Mitch Williamse6d038d2015-06-04 16:23:58 -04002723 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002724 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002725 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002726
2727 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2728
Greg Rose5eae00c2013-12-21 06:12:45 +00002729 netdev->netdev_ops = &i40evf_netdev_ops;
2730 i40evf_set_ethtool_ops(netdev);
2731 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002732
Jarod Wilson91c527a2016-10-17 15:54:05 -04002733 /* MTU range: 68 - 9710 */
2734 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002735 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002736
Greg Rose5eae00c2013-12-21 06:12:45 +00002737 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002738 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002739 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002740 eth_hw_addr_random(netdev);
2741 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2742 } else {
2743 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2744 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2745 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002746 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002747
Kees Cook26566ea2017-10-16 17:29:35 -07002748 timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
Greg Rose5eae00c2013-12-21 06:12:45 +00002749 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2750
Mitch Williamsd732a182014-04-24 06:41:37 +00002751 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2752 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002753 err = i40evf_init_interrupt_scheme(adapter);
2754 if (err)
2755 goto err_sw_init;
2756 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002757 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002758 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002759 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2760
Greg Rose5eae00c2013-12-21 06:12:45 +00002761 err = i40evf_request_misc_irq(adapter);
2762 if (err)
2763 goto err_sw_init;
2764
2765 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002766 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002767
Mitch Williamsef8693e2014-02-13 03:48:53 -08002768 if (!adapter->netdev_registered) {
2769 err = register_netdev(netdev);
2770 if (err)
2771 goto err_register;
2772 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002773
2774 adapter->netdev_registered = true;
2775
2776 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002777 if (CLIENT_ALLOWED(adapter)) {
2778 err = i40evf_lan_add_device(adapter);
2779 if (err)
2780 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2781 err);
2782 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002783
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002784 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002785 if (netdev->features & NETIF_F_GRO)
2786 dev_info(&pdev->dev, "GRO is enabled\n");
2787
Greg Rose5eae00c2013-12-21 06:12:45 +00002788 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002789 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002790 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002791 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002792
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002793 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2794 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2795 if (!adapter->rss_key || !adapter->rss_lut)
2796 goto err_mem;
2797
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002798 if (RSS_AQ(adapter)) {
2799 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2800 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2801 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002802 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002803 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002804 return;
2805restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002806 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002807 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002808err_mem:
2809 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002810err_register:
2811 i40evf_free_misc_irq(adapter);
2812err_sw_init:
2813 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002814err_alloc:
2815 kfree(adapter->vf_res);
2816 adapter->vf_res = NULL;
2817err:
2818 /* Things went into the weeds, so try again later */
2819 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002820 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002821 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002822 i40evf_shutdown_adminq(hw);
2823 adapter->state = __I40EVF_STARTUP;
2824 schedule_delayed_work(&adapter->init_task, HZ * 5);
2825 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002826 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002827 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002828}
2829
2830/**
2831 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2832 * @pdev: pci device structure
2833 **/
2834static void i40evf_shutdown(struct pci_dev *pdev)
2835{
2836 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002837 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002838
2839 netif_device_detach(netdev);
2840
2841 if (netif_running(netdev))
2842 i40evf_close(netdev);
2843
Mitch Williams00293fd2015-01-09 11:18:18 +00002844 /* Prevent the watchdog from running. */
2845 adapter->state = __I40EVF_REMOVE;
2846 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002847
Greg Rose5eae00c2013-12-21 06:12:45 +00002848#ifdef CONFIG_PM
2849 pci_save_state(pdev);
2850
2851#endif
2852 pci_disable_device(pdev);
2853}
2854
2855/**
2856 * i40evf_probe - Device Initialization Routine
2857 * @pdev: PCI device information struct
2858 * @ent: entry in i40evf_pci_tbl
2859 *
2860 * Returns 0 on success, negative on failure
2861 *
2862 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2863 * The OS initialization, configuring of the adapter private structure,
2864 * and a hardware reset occur.
2865 **/
2866static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2867{
2868 struct net_device *netdev;
2869 struct i40evf_adapter *adapter = NULL;
2870 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002871 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002872
2873 err = pci_enable_device(pdev);
2874 if (err)
2875 return err;
2876
Mitch Williams64942942014-02-11 08:26:33 +00002877 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002878 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002879 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2880 if (err) {
2881 dev_err(&pdev->dev,
2882 "DMA configuration failed: 0x%x\n", err);
2883 goto err_dma;
2884 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002885 }
2886
2887 err = pci_request_regions(pdev, i40evf_driver_name);
2888 if (err) {
2889 dev_err(&pdev->dev,
2890 "pci_request_regions failed 0x%x\n", err);
2891 goto err_pci_reg;
2892 }
2893
2894 pci_enable_pcie_error_reporting(pdev);
2895
2896 pci_set_master(pdev);
2897
Mitch Williams1255b7a2015-11-06 15:25:59 -08002898 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002899 if (!netdev) {
2900 err = -ENOMEM;
2901 goto err_alloc_etherdev;
2902 }
2903
2904 SET_NETDEV_DEV(netdev, &pdev->dev);
2905
2906 pci_set_drvdata(pdev, netdev);
2907 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002908
2909 adapter->netdev = netdev;
2910 adapter->pdev = pdev;
2911
2912 hw = &adapter->hw;
2913 hw->back = adapter;
2914
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002915 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002916 adapter->state = __I40EVF_STARTUP;
2917
2918 /* Call save state here because it relies on the adapter struct. */
2919 pci_save_state(pdev);
2920
2921 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2922 pci_resource_len(pdev, 0));
2923 if (!hw->hw_addr) {
2924 err = -EIO;
2925 goto err_ioremap;
2926 }
2927 hw->vendor_id = pdev->vendor;
2928 hw->device_id = pdev->device;
2929 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2930 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2931 hw->subsystem_device_id = pdev->subsystem_device;
2932 hw->bus.device = PCI_SLOT(pdev->devfn);
2933 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002934 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002935
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002936 /* set up the locks for the AQ, do this only once in probe
2937 * and destroy them only once in remove
2938 */
2939 mutex_init(&hw->aq.asq_mutex);
2940 mutex_init(&hw->aq.arq_mutex);
2941
Jacob Keller504398f2017-10-27 11:06:50 -04002942 spin_lock_init(&adapter->mac_vlan_list_lock);
2943
Serey Kong8bb1a542014-08-01 13:27:15 -07002944 INIT_LIST_HEAD(&adapter->mac_filter_list);
2945 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2946
Greg Rose5eae00c2013-12-21 06:12:45 +00002947 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2948 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2949 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002950 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002951 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002952 schedule_delayed_work(&adapter->init_task,
2953 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002954
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002955 /* Setup the wait queue for indicating transition to down status */
2956 init_waitqueue_head(&adapter->down_waitqueue);
2957
Greg Rose5eae00c2013-12-21 06:12:45 +00002958 return 0;
2959
2960err_ioremap:
2961 free_netdev(netdev);
2962err_alloc_etherdev:
2963 pci_release_regions(pdev);
2964err_pci_reg:
2965err_dma:
2966 pci_disable_device(pdev);
2967 return err;
2968}
2969
2970#ifdef CONFIG_PM
2971/**
2972 * i40evf_suspend - Power management suspend routine
2973 * @pdev: PCI device information struct
2974 * @state: unused
2975 *
2976 * Called when the system (VM) is entering sleep/suspend.
2977 **/
2978static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2979{
2980 struct net_device *netdev = pci_get_drvdata(pdev);
2981 struct i40evf_adapter *adapter = netdev_priv(netdev);
2982 int retval = 0;
2983
2984 netif_device_detach(netdev);
2985
Jacob Keller9b2aef12017-10-27 11:06:52 -04002986 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2987 &adapter->crit_section))
2988 usleep_range(500, 1000);
2989
Greg Rose5eae00c2013-12-21 06:12:45 +00002990 if (netif_running(netdev)) {
2991 rtnl_lock();
2992 i40evf_down(adapter);
2993 rtnl_unlock();
2994 }
2995 i40evf_free_misc_irq(adapter);
2996 i40evf_reset_interrupt_capability(adapter);
2997
Jacob Keller9b2aef12017-10-27 11:06:52 -04002998 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2999
Greg Rose5eae00c2013-12-21 06:12:45 +00003000 retval = pci_save_state(pdev);
3001 if (retval)
3002 return retval;
3003
3004 pci_disable_device(pdev);
3005
3006 return 0;
3007}
3008
3009/**
Joe Perchesdbedd442015-03-06 20:49:12 -08003010 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00003011 * @pdev: PCI device information struct
3012 *
3013 * Called when the system (VM) is resumed from sleep/suspend.
3014 **/
3015static int i40evf_resume(struct pci_dev *pdev)
3016{
3017 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3018 struct net_device *netdev = adapter->netdev;
3019 u32 err;
3020
3021 pci_set_power_state(pdev, PCI_D0);
3022 pci_restore_state(pdev);
3023 /* pci_restore_state clears dev->state_saved so call
3024 * pci_save_state to restore it.
3025 */
3026 pci_save_state(pdev);
3027
3028 err = pci_enable_device_mem(pdev);
3029 if (err) {
3030 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3031 return err;
3032 }
3033 pci_set_master(pdev);
3034
3035 rtnl_lock();
3036 err = i40evf_set_interrupt_capability(adapter);
3037 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003038 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003039 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3040 return err;
3041 }
3042 err = i40evf_request_misc_irq(adapter);
3043 rtnl_unlock();
3044 if (err) {
3045 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3046 return err;
3047 }
3048
3049 schedule_work(&adapter->reset_task);
3050
3051 netif_device_attach(netdev);
3052
3053 return err;
3054}
3055
3056#endif /* CONFIG_PM */
3057/**
3058 * i40evf_remove - Device Removal Routine
3059 * @pdev: PCI device information struct
3060 *
3061 * i40evf_remove is called by the PCI subsystem to alert the driver
3062 * that it should release a PCI device. The could be caused by a
3063 * Hot-Plug event, or because the driver is going to be removed from
3064 * memory.
3065 **/
3066static void i40evf_remove(struct pci_dev *pdev)
3067{
3068 struct net_device *netdev = pci_get_drvdata(pdev);
3069 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003070 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003071 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003072 int err;
Avinash Dayanand06aa0402017-12-18 05:16:43 -05003073 /* Indicate we are in remove and not to run reset_task */
3074 set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00003075 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003076 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003077 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003078 if (adapter->netdev_registered) {
3079 unregister_netdev(netdev);
3080 adapter->netdev_registered = false;
3081 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003082 if (CLIENT_ALLOWED(adapter)) {
3083 err = i40evf_lan_del_device(adapter);
3084 if (err)
3085 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3086 err);
3087 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003088
Mitch Williamsf4a71882015-01-09 11:18:16 +00003089 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003090 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003091 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003092 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003093 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003094 /* If the FW isn't responding, kick it once, but only once. */
3095 if (!i40evf_asq_done(hw)) {
3096 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003097 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003098 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003099 i40evf_free_all_tx_resources(adapter);
3100 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003101 i40evf_misc_irq_disable(adapter);
3102 i40evf_free_misc_irq(adapter);
3103 i40evf_reset_interrupt_capability(adapter);
3104 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003105
Mitch Williamse5d17c32014-06-04 04:22:38 +00003106 if (adapter->watchdog_timer.function)
3107 del_timer_sync(&adapter->watchdog_timer);
3108
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003109 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003110
Greg Rose5eae00c2013-12-21 06:12:45 +00003111 if (hw->aq.asq.count)
3112 i40evf_shutdown_adminq(hw);
3113
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003114 /* destroy the locks only once, here */
3115 mutex_destroy(&hw->aq.arq_mutex);
3116 mutex_destroy(&hw->aq.asq_mutex);
3117
Greg Rose5eae00c2013-12-21 06:12:45 +00003118 iounmap(hw->hw_addr);
3119 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003120 i40evf_free_all_tx_resources(adapter);
3121 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003122 i40evf_free_queues(adapter);
3123 kfree(adapter->vf_res);
Jacob Keller504398f2017-10-27 11:06:50 -04003124 spin_lock_bh(&adapter->mac_vlan_list_lock);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003125 /* If we got removed before an up/down sequence, we've got a filter
3126 * hanging out there that we need to get rid of.
3127 */
3128 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3129 list_del(&f->list);
3130 kfree(f);
3131 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003132 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3133 list_del(&f->list);
3134 kfree(f);
3135 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003136
Jacob Keller504398f2017-10-27 11:06:50 -04003137 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3138
Greg Rose5eae00c2013-12-21 06:12:45 +00003139 free_netdev(netdev);
3140
3141 pci_disable_pcie_error_reporting(pdev);
3142
3143 pci_disable_device(pdev);
3144}
3145
3146static struct pci_driver i40evf_driver = {
3147 .name = i40evf_driver_name,
3148 .id_table = i40evf_pci_tbl,
3149 .probe = i40evf_probe,
3150 .remove = i40evf_remove,
3151#ifdef CONFIG_PM
3152 .suspend = i40evf_suspend,
3153 .resume = i40evf_resume,
3154#endif
3155 .shutdown = i40evf_shutdown,
3156};
3157
3158/**
3159 * i40e_init_module - Driver Registration Routine
3160 *
3161 * i40e_init_module is the first routine called when the driver is
3162 * loaded. All it does is register with the PCI subsystem.
3163 **/
3164static int __init i40evf_init_module(void)
3165{
3166 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003167
Greg Rose5eae00c2013-12-21 06:12:45 +00003168 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003169 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003170
3171 pr_info("%s\n", i40evf_copyright);
3172
Jacob Keller6992a6c2016-08-04 11:37:01 -07003173 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3174 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003175 if (!i40evf_wq) {
3176 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3177 return -ENOMEM;
3178 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003179 ret = pci_register_driver(&i40evf_driver);
3180 return ret;
3181}
3182
3183module_init(i40evf_init_module);
3184
3185/**
3186 * i40e_exit_module - Driver Exit Cleanup Routine
3187 *
3188 * i40e_exit_module is called just before the driver is removed
3189 * from memory.
3190 **/
3191static void __exit i40evf_exit_module(void)
3192{
3193 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003194 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003195}
3196
3197module_exit(i40evf_exit_module);
3198
3199/* i40evf_main.c */