blob: 16989ad2ca90b0453741344e7b9ca79540f905b9 [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++;
356 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700357 q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
Mitch Williamsf19a9732016-09-12 14:18:38 -0700358 q_vector->ring_mask |= BIT(r_idx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400359 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700360 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000361}
362
363/**
364 * i40evf_map_vector_to_txq - associate irqs with tx queues
365 * @adapter: board private structure
366 * @v_idx: interrupt number
367 * @t_idx: queue number
368 **/
369static void
370i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
371{
Mitch Williams7d96ba12015-10-26 19:44:39 -0400372 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
Mitch Williams0dd438d2015-10-26 19:44:40 -0400373 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
Mitch Williamsf19a9732016-09-12 14:18:38 -0700374 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000375
376 tx_ring->q_vector = q_vector;
377 tx_ring->next = q_vector->tx.ring;
378 tx_ring->vsi = &adapter->vsi;
379 q_vector->tx.ring = tx_ring;
380 q_vector->tx.count++;
381 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jacob Keller65e87c02016-09-12 14:18:44 -0700382 q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400383 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000384 q_vector->num_ringpairs++;
Mitch Williamsf19a9732016-09-12 14:18:38 -0700385 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000386}
387
388/**
389 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
390 * @adapter: board private structure to initialize
391 *
392 * This function maps descriptor rings to the queue-specific vectors
393 * we were allotted through the MSI-X enabling code. Ideally, we'd have
394 * one vector per ring/queue, but on a constrained vector budget, we
395 * group the rings as "efficiently" as possible. You would add new
396 * mapping configurations in here.
397 **/
Mitch Williams1b7b7592017-08-22 06:57:51 -0400398static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +0000399{
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400400 int rings_remaining = adapter->num_active_queues;
401 int ridx = 0, vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000402 int q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +0000403
404 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
405
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400406 for (; ridx < rings_remaining; ridx++) {
407 i40evf_map_vector_to_rxq(adapter, vidx, ridx);
408 i40evf_map_vector_to_txq(adapter, vidx, ridx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000409
Alan Bradyc97fc9b2017-07-14 09:27:07 -0400410 /* In the case where we have more queues than vectors, continue
411 * round-robin on vectors until all queues are mapped.
412 */
413 if (++vidx >= q_vectors)
414 vidx = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +0000415 }
416
Greg Rose5eae00c2013-12-21 06:12:45 +0000417 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Greg Rose5eae00c2013-12-21 06:12:45 +0000418}
419
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700420#ifdef CONFIG_NET_POLL_CONTROLLER
421/**
422 * i40evf_netpoll - A Polling 'interrupt' handler
423 * @netdev: network interface device structure
424 *
425 * This is used by netconsole to send skbs without having to re-enable
426 * interrupts. It's not called while the normal interrupt routine is executing.
427 **/
428static void i40evf_netpoll(struct net_device *netdev)
429{
430 struct i40evf_adapter *adapter = netdev_priv(netdev);
431 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
432 int i;
433
434 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -0400435 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700436 return;
437
438 for (i = 0; i < q_vectors; i++)
Mitch Williams7d96ba12015-10-26 19:44:39 -0400439 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700440}
441
442#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000443/**
Alan Brady96db7762016-09-14 16:24:38 -0700444 * i40evf_irq_affinity_notify - Callback for affinity changes
445 * @notify: context as to what irq was changed
446 * @mask: the new affinity mask
447 *
448 * This is a callback function used by the irq_set_affinity_notifier function
449 * so that we may register to receive changes to the irq affinity masks.
450 **/
451static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
452 const cpumask_t *mask)
453{
454 struct i40e_q_vector *q_vector =
455 container_of(notify, struct i40e_q_vector, affinity_notify);
456
Jacob Keller7e4d01e2017-07-12 05:46:05 -0400457 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -0700458}
459
460/**
461 * i40evf_irq_affinity_release - Callback for affinity notifier release
462 * @ref: internal core kernel usage
463 *
464 * This is a callback function used by the irq_set_affinity_notifier function
465 * to inform the current notification subscriber that they will no longer
466 * receive notifications.
467 **/
468static void i40evf_irq_affinity_release(struct kref *ref) {}
469
470/**
Greg Rose5eae00c2013-12-21 06:12:45 +0000471 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
472 * @adapter: board private structure
473 *
474 * Allocates MSI-X vectors for tx and rx handling, and requests
475 * interrupts from the kernel.
476 **/
477static int
478i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
479{
Jacob Keller696ac80a2017-07-12 05:46:11 -0400480 unsigned int vector, q_vectors;
481 unsigned int rx_int_idx = 0, tx_int_idx = 0;
482 int irq_num, err;
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400483 int cpu;
Greg Rose5eae00c2013-12-21 06:12:45 +0000484
485 i40evf_irq_disable(adapter);
486 /* Decrement for Other and TCP Timer vectors */
487 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
488
489 for (vector = 0; vector < q_vectors; vector++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400490 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
Alan Brady96db7762016-09-14 16:24:38 -0700491 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
Greg Rose5eae00c2013-12-21 06:12:45 +0000492
493 if (q_vector->tx.ring && q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400494 snprintf(q_vector->name, sizeof(q_vector->name),
495 "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000496 tx_int_idx++;
497 } else if (q_vector->rx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400498 snprintf(q_vector->name, sizeof(q_vector->name),
499 "i40evf-%s-rx-%d", basename, rx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000500 } else if (q_vector->tx.ring) {
Jacob Keller696ac80a2017-07-12 05:46:11 -0400501 snprintf(q_vector->name, sizeof(q_vector->name),
502 "i40evf-%s-tx-%d", basename, tx_int_idx++);
Greg Rose5eae00c2013-12-21 06:12:45 +0000503 } else {
504 /* skip this unused q_vector */
505 continue;
506 }
Alan Brady96db7762016-09-14 16:24:38 -0700507 err = request_irq(irq_num,
508 i40evf_msix_clean_rings,
509 0,
510 q_vector->name,
511 q_vector);
Greg Rose5eae00c2013-12-21 06:12:45 +0000512 if (err) {
513 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400514 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000515 goto free_queue_irqs;
516 }
Alan Brady96db7762016-09-14 16:24:38 -0700517 /* register for affinity change notifications */
518 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
519 q_vector->affinity_notify.release =
520 i40evf_irq_affinity_release;
521 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400522 /* Spread the IRQ affinity hints across online CPUs. Note that
523 * get_cpu_mask returns a mask with a permanent lifetime so
524 * it's safe to use as a hint for irq_set_affinity_hint.
Jacob Keller759dc4a2017-07-14 09:10:10 -0400525 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -0400526 cpu = cpumask_local_spread(q_vector->v_idx, -1);
527 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Greg Rose5eae00c2013-12-21 06:12:45 +0000528 }
529
530 return 0;
531
532free_queue_irqs:
533 while (vector) {
534 vector--;
Alan Brady96db7762016-09-14 16:24:38 -0700535 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
536 irq_set_affinity_notifier(irq_num, NULL);
537 irq_set_affinity_hint(irq_num, NULL);
538 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000539 }
540 return err;
541}
542
543/**
544 * i40evf_request_misc_irq - Initialize MSI-X interrupts
545 * @adapter: board private structure
546 *
547 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
548 * vector is only for the admin queue, and stays active even when the netdev
549 * is closed.
550 **/
551static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
552{
553 struct net_device *netdev = adapter->netdev;
554 int err;
555
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700556 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000557 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
558 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000559 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800560 &i40evf_msix_aq, 0,
561 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000562 if (err) {
563 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800564 "request_irq for %s failed: %d\n",
565 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000566 free_irq(adapter->msix_entries[0].vector, netdev);
567 }
568 return err;
569}
570
571/**
572 * i40evf_free_traffic_irqs - Free MSI-X interrupts
573 * @adapter: board private structure
574 *
575 * Frees all MSI-X vectors other than 0.
576 **/
577static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
578{
Alan Brady96db7762016-09-14 16:24:38 -0700579 int vector, irq_num, q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000580
Alan Brady47d2a5d2016-11-08 13:05:05 -0800581 if (!adapter->msix_entries)
582 return;
583
Greg Rose5eae00c2013-12-21 06:12:45 +0000584 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
585
Alan Brady96db7762016-09-14 16:24:38 -0700586 for (vector = 0; vector < q_vectors; vector++) {
587 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
588 irq_set_affinity_notifier(irq_num, NULL);
589 irq_set_affinity_hint(irq_num, NULL);
590 free_irq(irq_num, &adapter->q_vectors[vector]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000591 }
592}
593
594/**
595 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
596 * @adapter: board private structure
597 *
598 * Frees MSI-X vector 0.
599 **/
600static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
601{
602 struct net_device *netdev = adapter->netdev;
603
Jacob Kelleref4603e2016-11-08 13:05:08 -0800604 if (!adapter->msix_entries)
605 return;
606
Greg Rose5eae00c2013-12-21 06:12:45 +0000607 free_irq(adapter->msix_entries[0].vector, netdev);
608}
609
610/**
611 * i40evf_configure_tx - Configure Transmit Unit after Reset
612 * @adapter: board private structure
613 *
614 * Configure the Tx unit of the MAC after a reset.
615 **/
616static void i40evf_configure_tx(struct i40evf_adapter *adapter)
617{
618 struct i40e_hw *hw = &adapter->hw;
619 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000620
Mitch Williamscc052922014-10-25 03:24:34 +0000621 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -0400622 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
Greg Rose5eae00c2013-12-21 06:12:45 +0000623}
624
625/**
626 * i40evf_configure_rx - Configure Receive Unit after Reset
627 * @adapter: board private structure
628 *
629 * Configure the Rx unit of the MAC after a reset.
630 **/
631static void i40evf_configure_rx(struct i40evf_adapter *adapter)
632{
Alexander Duyckdab86af2017-03-14 10:15:27 -0700633 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
Greg Rose5eae00c2013-12-21 06:12:45 +0000634 struct i40e_hw *hw = &adapter->hw;
Greg Rose5eae00c2013-12-21 06:12:45 +0000635 int i;
Greg Rose5eae00c2013-12-21 06:12:45 +0000636
Alexander Duyckdab86af2017-03-14 10:15:27 -0700637 /* Legacy Rx will always default to a 2048 buffer size. */
638#if (PAGE_SIZE < 8192)
639 if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
Arnd Bergmann3dfc3eb2017-04-19 19:29:48 +0200640 struct net_device *netdev = adapter->netdev;
641
Alexander Duyck98efd692017-04-05 07:51:01 -0400642 /* For jumbo frames on systems with 4K pages we have to use
643 * an order 1 page, so we might as well increase the size
644 * of our Rx buffer to make better use of the available space
645 */
646 rx_buf_len = I40E_RXBUFFER_3072;
647
Alexander Duyckdab86af2017-03-14 10:15:27 -0700648 /* We use a 1536 buffer size for configurations with
649 * standard Ethernet mtu. On x86 this gives us enough room
650 * for shared info and 192 bytes of padding.
651 */
Alexander Duyckca9ec082017-04-05 07:51:02 -0400652 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
653 (netdev->mtu <= ETH_DATA_LEN))
Alexander Duyckdab86af2017-03-14 10:15:27 -0700654 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
655 }
656#endif
657
Mitch Williamscc052922014-10-25 03:24:34 +0000658 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400659 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
Alexander Duyckdab86af2017-03-14 10:15:27 -0700660 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
Alexander Duyckca9ec082017-04-05 07:51:02 -0400661
662 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
663 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
664 else
665 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +0000666 }
667}
668
669/**
670 * i40evf_find_vlan - Search filter list for specific vlan filter
671 * @adapter: board private structure
672 * @vlan: vlan tag
673 *
Jacob Keller504398f2017-10-27 11:06:50 -0400674 * Returns ptr to the filter object or NULL. Must be called while holding the
675 * mac_vlan_list_lock.
Greg Rose5eae00c2013-12-21 06:12:45 +0000676 **/
677static struct
678i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
679{
680 struct i40evf_vlan_filter *f;
681
682 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
683 if (vlan == f->vlan)
684 return f;
685 }
686 return NULL;
687}
688
689/**
690 * i40evf_add_vlan - Add a vlan filter to the list
691 * @adapter: board private structure
692 * @vlan: VLAN tag
693 *
694 * Returns ptr to the filter object or NULL when no memory available.
695 **/
696static struct
697i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
698{
Mitch Williams13acb542015-03-31 00:45:05 -0700699 struct i40evf_vlan_filter *f = NULL;
Mitch Williams13acb542015-03-31 00:45:05 -0700700
Jacob Keller504398f2017-10-27 11:06:50 -0400701 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000702
703 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000704 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000705 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000706 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700707 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000708
Greg Rose5eae00c2013-12-21 06:12:45 +0000709 f->vlan = vlan;
710
711 INIT_LIST_HEAD(&f->list);
712 list_add(&f->list, &adapter->vlan_filter_list);
713 f->add = true;
714 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
715 }
716
Mitch Williams13acb542015-03-31 00:45:05 -0700717clearout:
Jacob Keller504398f2017-10-27 11:06:50 -0400718 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000719 return f;
720}
721
722/**
723 * i40evf_del_vlan - Remove a vlan filter from the list
724 * @adapter: board private structure
725 * @vlan: VLAN tag
726 **/
727static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
728{
729 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700730
Jacob Keller504398f2017-10-27 11:06:50 -0400731 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000732
733 f = i40evf_find_vlan(adapter, vlan);
734 if (f) {
735 f->remove = true;
736 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
737 }
Jacob Keller504398f2017-10-27 11:06:50 -0400738
739 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000740}
741
742/**
743 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
744 * @netdev: network device struct
745 * @vid: VLAN tag
746 **/
747static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000748 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000749{
750 struct i40evf_adapter *adapter = netdev_priv(netdev);
751
Mitch Williams8ed995f2015-08-28 17:55:57 -0400752 if (!VLAN_ALLOWED(adapter))
753 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000754 if (i40evf_add_vlan(adapter, vid) == NULL)
755 return -ENOMEM;
756 return 0;
757}
758
759/**
760 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
761 * @netdev: network device struct
762 * @vid: VLAN tag
763 **/
764static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000765 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000766{
767 struct i40evf_adapter *adapter = netdev_priv(netdev);
768
Mitch Williams8ed995f2015-08-28 17:55:57 -0400769 if (VLAN_ALLOWED(adapter)) {
770 i40evf_del_vlan(adapter, vid);
771 return 0;
772 }
773 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000774}
775
776/**
777 * i40evf_find_filter - Search filter list for specific mac filter
778 * @adapter: board private structure
779 * @macaddr: the MAC address
780 *
Jacob Keller504398f2017-10-27 11:06:50 -0400781 * Returns ptr to the filter object or NULL. Must be called while holding the
782 * mac_vlan_list_lock.
Greg Rose5eae00c2013-12-21 06:12:45 +0000783 **/
784static struct
785i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
786 u8 *macaddr)
787{
788 struct i40evf_mac_filter *f;
789
790 if (!macaddr)
791 return NULL;
792
793 list_for_each_entry(f, &adapter->mac_filter_list, list) {
794 if (ether_addr_equal(macaddr, f->macaddr))
795 return f;
796 }
797 return NULL;
798}
799
800/**
801 * i40e_add_filter - Add a mac filter to the filter list
802 * @adapter: board private structure
803 * @macaddr: the MAC address
804 *
805 * Returns ptr to the filter object or NULL when no memory available.
806 **/
807static struct
808i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
809 u8 *macaddr)
810{
811 struct i40evf_mac_filter *f;
812
813 if (!macaddr)
814 return NULL;
815
Jacob Keller504398f2017-10-27 11:06:50 -0400816 spin_lock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000817
818 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000819 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000820 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Jacob Keller504398f2017-10-27 11:06:50 -0400821 if (!f)
822 goto clearout;
Greg Rose5eae00c2013-12-21 06:12:45 +0000823
Greg Rose9a173902014-05-22 06:32:02 +0000824 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000825
Mitch Williams63590b62016-05-16 10:26:42 -0700826 list_add_tail(&f->list, &adapter->mac_filter_list);
Greg Rose5eae00c2013-12-21 06:12:45 +0000827 f->add = true;
828 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Alan Bradyc766b9a2017-09-07 08:05:47 -0400829 } else {
830 f->remove = false;
Greg Rose5eae00c2013-12-21 06:12:45 +0000831 }
832
Jacob Keller504398f2017-10-27 11:06:50 -0400833clearout:
834 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000835 return f;
836}
837
838/**
839 * i40evf_set_mac - NDO callback to set port mac address
840 * @netdev: network interface device structure
841 * @p: pointer to an address structure
842 *
843 * Returns 0 on success, negative on failure
844 **/
845static int i40evf_set_mac(struct net_device *netdev, void *p)
846{
847 struct i40evf_adapter *adapter = netdev_priv(netdev);
848 struct i40e_hw *hw = &adapter->hw;
849 struct i40evf_mac_filter *f;
850 struct sockaddr *addr = p;
851
852 if (!is_valid_ether_addr(addr->sa_data))
853 return -EADDRNOTAVAIL;
854
855 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
856 return 0;
857
Mitch Williams14e52ee2015-08-31 19:54:44 -0400858 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
859 return -EPERM;
860
Jacob Keller504398f2017-10-27 11:06:50 -0400861 spin_lock_bh(&adapter->mac_vlan_list_lock);
862
Mitch Williams14e52ee2015-08-31 19:54:44 -0400863 f = i40evf_find_filter(adapter, hw->mac.addr);
864 if (f) {
865 f->remove = true;
866 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
867 }
868
Jacob Keller504398f2017-10-27 11:06:50 -0400869 spin_unlock_bh(&adapter->mac_vlan_list_lock);
870
Greg Rose5eae00c2013-12-21 06:12:45 +0000871 f = i40evf_add_filter(adapter, addr->sa_data);
872 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000873 ether_addr_copy(hw->mac.addr, addr->sa_data);
874 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000875 }
876
877 return (f == NULL) ? -ENOMEM : 0;
878}
879
880/**
881 * i40evf_set_rx_mode - NDO callback to set the netdev filters
882 * @netdev: network interface device structure
883 **/
884static void i40evf_set_rx_mode(struct net_device *netdev)
885{
886 struct i40evf_adapter *adapter = netdev_priv(netdev);
887 struct i40evf_mac_filter *f, *ftmp;
888 struct netdev_hw_addr *uca;
889 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400890 struct netdev_hw_addr *ha;
Greg Rose5eae00c2013-12-21 06:12:45 +0000891
892 /* add addr if not already in the filter list */
893 netdev_for_each_uc_addr(uca, netdev) {
894 i40evf_add_filter(adapter, uca->addr);
895 }
896 netdev_for_each_mc_addr(mca, netdev) {
897 i40evf_add_filter(adapter, mca->addr);
898 }
899
Jacob Keller504398f2017-10-27 11:06:50 -0400900 spin_lock_bh(&adapter->mac_vlan_list_lock);
901
Greg Rose5eae00c2013-12-21 06:12:45 +0000902 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400903 netdev_for_each_mc_addr(mca, netdev)
904 if (ether_addr_equal(mca->addr, f->macaddr))
905 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000906
Shannon Nelson2f41f332015-08-26 15:14:20 -0400907 netdev_for_each_uc_addr(uca, netdev)
908 if (ether_addr_equal(uca->addr, f->macaddr))
909 goto bottom_of_search_loop;
910
911 for_each_dev_addr(netdev, ha)
912 if (ether_addr_equal(ha->addr, f->macaddr))
913 goto bottom_of_search_loop;
914
915 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
916 goto bottom_of_search_loop;
917
918 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
919 f->remove = true;
920 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
921
922bottom_of_search_loop:
923 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000924 }
Anjali Singhai Jain47d34832016-04-12 08:30:52 -0700925
926 if (netdev->flags & IFF_PROMISC &&
927 !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
928 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
929 else if (!(netdev->flags & IFF_PROMISC) &&
930 adapter->flags & I40EVF_FLAG_PROMISC_ON)
931 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
932
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -0700933 if (netdev->flags & IFF_ALLMULTI &&
934 !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
935 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
936 else if (!(netdev->flags & IFF_ALLMULTI) &&
937 adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
938 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
939
Jacob Keller504398f2017-10-27 11:06:50 -0400940 spin_unlock_bh(&adapter->mac_vlan_list_lock);
Greg Rose5eae00c2013-12-21 06:12:45 +0000941}
942
943/**
944 * i40evf_napi_enable_all - enable NAPI on all queue vectors
945 * @adapter: board private structure
946 **/
947static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
948{
949 int q_idx;
950 struct i40e_q_vector *q_vector;
951 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
952
953 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
954 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000955
Mitch Williams7d96ba12015-10-26 19:44:39 -0400956 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000957 napi = &q_vector->napi;
958 napi_enable(napi);
959 }
960}
961
962/**
963 * i40evf_napi_disable_all - disable NAPI on all queue vectors
964 * @adapter: board private structure
965 **/
966static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
967{
968 int q_idx;
969 struct i40e_q_vector *q_vector;
970 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
971
972 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -0400973 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +0000974 napi_disable(&q_vector->napi);
975 }
976}
977
978/**
979 * i40evf_configure - set up transmit and receive data structures
980 * @adapter: board private structure
981 **/
982static void i40evf_configure(struct i40evf_adapter *adapter)
983{
984 struct net_device *netdev = adapter->netdev;
985 int i;
986
987 i40evf_set_rx_mode(netdev);
988
989 i40evf_configure_tx(adapter);
990 i40evf_configure_rx(adapter);
991 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
992
Mitch Williamscc052922014-10-25 03:24:34 +0000993 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -0400994 struct i40e_ring *ring = &adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000995
Mitch Williamsb1630982016-04-18 11:33:48 -0700996 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Greg Rose5eae00c2013-12-21 06:12:45 +0000997 }
998}
999
1000/**
1001 * i40evf_up_complete - Finish the last steps of bringing up a connection
1002 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001003 *
1004 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001005 **/
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001006static void i40evf_up_complete(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001007{
1008 adapter->state = __I40EVF_RUNNING;
Jacob Keller0da36b92017-04-19 09:25:55 -04001009 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00001010
1011 i40evf_napi_enable_all(adapter);
1012
1013 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsed0e8942017-01-24 10:23:59 -08001014 if (CLIENT_ENABLED(adapter))
1015 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001016 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001017}
1018
1019/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001020 * i40e_down - Shutdown the connection processing
1021 * @adapter: board private structure
Jacob Keller9b2aef12017-10-27 11:06:52 -04001022 *
1023 * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
Greg Rose5eae00c2013-12-21 06:12:45 +00001024 **/
1025void i40evf_down(struct i40evf_adapter *adapter)
1026{
1027 struct net_device *netdev = adapter->netdev;
1028 struct i40evf_mac_filter *f;
1029
Mitch Williams209dc4d2015-12-09 15:50:27 -08001030 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001031 return;
1032
Mitch Williams63e18c22015-03-27 00:12:10 -07001033 netif_carrier_off(netdev);
1034 netif_tx_disable(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001035 adapter->link_up = false;
Mitch Williams748c4342015-01-29 07:17:18 +00001036 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001037 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001038
Jacob Keller504398f2017-10-27 11:06:50 -04001039 spin_lock_bh(&adapter->mac_vlan_list_lock);
1040
Mitch Williamsef8693e2014-02-13 03:48:53 -08001041 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001042 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1043 f->remove = true;
1044 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001045 /* remove all VLAN filters */
1046 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1047 f->remove = true;
1048 }
Jacob Keller504398f2017-10-27 11:06:50 -04001049
1050 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1051
Mitch Williamsef8693e2014-02-13 03:48:53 -08001052 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1053 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001054 /* cancel any current operation */
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001055 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001056 /* Schedule operations to close down the HW. Don't wait
1057 * here for this to complete. The watchdog is still running
1058 * and it will take care of this.
1059 */
1060 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001061 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001062 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001063 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001064
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001065 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
Greg Rose5eae00c2013-12-21 06:12:45 +00001066}
1067
1068/**
1069 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1070 * @adapter: board private structure
1071 * @vectors: number of vectors to request
1072 *
1073 * Work with the OS to set up the MSIX vectors needed.
1074 *
1075 * Returns 0 on success, negative on failure
1076 **/
1077static int
1078i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1079{
1080 int err, vector_threshold;
1081
1082 /* We'll want at least 3 (vector_threshold):
1083 * 0) Other (Admin Queue and link, mostly)
1084 * 1) TxQ[0] Cleanup
1085 * 2) RxQ[0] Cleanup
1086 */
1087 vector_threshold = MIN_MSIX_COUNT;
1088
1089 /* The more we get, the more we will assign to Tx/Rx Cleanup
1090 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1091 * Right now, we simply care about how many we'll get; we'll
1092 * set them up later while requesting irq's.
1093 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001094 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1095 vector_threshold, vectors);
1096 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001097 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001098 kfree(adapter->msix_entries);
1099 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001100 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001101 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001102
1103 /* Adjust for only the vectors we'll use, which is minimum
1104 * of max_msix_q_vectors + NONQ_VECS, or the number of
1105 * vectors we were allocated.
1106 */
1107 adapter->num_msix_vectors = err;
1108 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001109}
1110
1111/**
1112 * i40evf_free_queues - Free memory for all rings
1113 * @adapter: board private structure to initialize
1114 *
1115 * Free all of the memory associated with queue pairs.
1116 **/
1117static void i40evf_free_queues(struct i40evf_adapter *adapter)
1118{
Greg Rose5eae00c2013-12-21 06:12:45 +00001119 if (!adapter->vsi_res)
1120 return;
Jacob Keller65c70062017-06-07 05:43:01 -04001121 adapter->num_active_queues = 0;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001122 kfree(adapter->tx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001123 adapter->tx_rings = NULL;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001124 kfree(adapter->rx_rings);
Mitch Williams10311542015-12-09 15:50:30 -08001125 adapter->rx_rings = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001126}
1127
1128/**
1129 * i40evf_alloc_queues - Allocate memory for all rings
1130 * @adapter: board private structure to initialize
1131 *
1132 * We allocate one ring per queue at run-time since we don't know the
1133 * number of queues at compile-time. The polling_netdev array is
1134 * intended for Multiqueue, but should work fine with a single queue.
1135 **/
1136static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1137{
Jacob Keller65c70062017-06-07 05:43:01 -04001138 int i, num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001139
Alan Brady5b36e8d2017-08-22 06:57:50 -04001140 /* If we're in reset reallocating queues we don't actually know yet for
1141 * certain the PF gave us the number of queues we asked for but we'll
1142 * assume it did. Once basic reset is finished we'll confirm once we
1143 * start negotiating config with PF.
1144 */
1145 if (adapter->num_req_queues)
1146 num_active_queues = adapter->num_req_queues;
1147 else
1148 num_active_queues = min_t(int,
1149 adapter->vsi_res->num_queue_pairs,
1150 (int)(num_online_cpus()));
1151
Jacob Keller65c70062017-06-07 05:43:01 -04001152
1153 adapter->tx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001154 sizeof(struct i40e_ring), GFP_KERNEL);
1155 if (!adapter->tx_rings)
1156 goto err_out;
Jacob Keller65c70062017-06-07 05:43:01 -04001157 adapter->rx_rings = kcalloc(num_active_queues,
Mitch Williams0dd438d2015-10-26 19:44:40 -04001158 sizeof(struct i40e_ring), GFP_KERNEL);
1159 if (!adapter->rx_rings)
1160 goto err_out;
1161
Jacob Keller65c70062017-06-07 05:43:01 -04001162 for (i = 0; i < num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001163 struct i40e_ring *tx_ring;
1164 struct i40e_ring *rx_ring;
1165
Mitch Williams0dd438d2015-10-26 19:44:40 -04001166 tx_ring = &adapter->tx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001167
1168 tx_ring->queue_index = i;
1169 tx_ring->netdev = adapter->netdev;
1170 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001171 tx_ring->count = adapter->tx_desc_count;
Jacob Keller42702552017-09-07 08:05:48 -04001172 tx_ring->tx_itr_setting = I40E_ITR_TX_DEF;
Jacob Kellerd36e41d2017-06-23 04:24:46 -04001173 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001174 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001175
Mitch Williams0dd438d2015-10-26 19:44:40 -04001176 rx_ring = &adapter->rx_rings[i];
Greg Rose5eae00c2013-12-21 06:12:45 +00001177 rx_ring->queue_index = i;
1178 rx_ring->netdev = adapter->netdev;
1179 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001180 rx_ring->count = adapter->rx_desc_count;
Jacob Keller42702552017-09-07 08:05:48 -04001181 rx_ring->rx_itr_setting = I40E_ITR_RX_DEF;
Greg Rose5eae00c2013-12-21 06:12:45 +00001182 }
1183
Jacob Keller65c70062017-06-07 05:43:01 -04001184 adapter->num_active_queues = num_active_queues;
1185
Greg Rose5eae00c2013-12-21 06:12:45 +00001186 return 0;
1187
1188err_out:
1189 i40evf_free_queues(adapter);
1190 return -ENOMEM;
1191}
1192
1193/**
1194 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1195 * @adapter: board private structure to initialize
1196 *
1197 * Attempt to configure the interrupts using the best available
1198 * capabilities of the hardware and the kernel.
1199 **/
1200static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1201{
1202 int vector, v_budget;
1203 int pairs = 0;
1204 int err = 0;
1205
1206 if (!adapter->vsi_res) {
1207 err = -EIO;
1208 goto out;
1209 }
Mitch Williamscc052922014-10-25 03:24:34 +00001210 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001211
Jacob Keller789f38c2017-04-19 09:25:56 -04001212 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1213 * us much good if we have more vectors than CPUs. However, we already
1214 * limit the total number of queues by the number of CPUs so we do not
1215 * need any further limiting here.
Greg Rose5eae00c2013-12-21 06:12:45 +00001216 */
Jacob Keller789f38c2017-04-19 09:25:56 -04001217 v_budget = min_t(int, pairs + NONQ_VECS,
1218 (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001219
Greg Rose5eae00c2013-12-21 06:12:45 +00001220 adapter->msix_entries = kcalloc(v_budget,
1221 sizeof(struct msix_entry), GFP_KERNEL);
1222 if (!adapter->msix_entries) {
1223 err = -ENOMEM;
1224 goto out;
1225 }
1226
1227 for (vector = 0; vector < v_budget; vector++)
1228 adapter->msix_entries[vector].entry = vector;
1229
Mitch Williams313ed2d2015-08-27 11:42:31 -04001230 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001231
1232out:
Mitch Williamse6c4cf62015-11-06 15:26:00 -08001233 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1234 netif_set_real_num_tx_queues(adapter->netdev, pairs);
Greg Rose5eae00c2013-12-21 06:12:45 +00001235 return err;
1236}
1237
1238/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001239 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1240 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001241 *
1242 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001243 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001244static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001245{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001246 struct i40e_aqc_get_set_rss_key_data *rss_key =
1247 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001248 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001249 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001250
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001251 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001252 /* bail because we already have a command pending */
Masanari Iidae3d132d2015-10-16 21:14:29 +09001253 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001254 adapter->current_op);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001255 return -EBUSY;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001256 }
1257
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001258 ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1259 if (ret) {
1260 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1261 i40evf_stat_str(hw, ret),
1262 i40evf_aq_str(hw, hw->aq.asq_last_status));
1263 return ret;
1264
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001265 }
1266
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001267 ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1268 adapter->rss_lut, adapter->rss_lut_size);
1269 if (ret) {
1270 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1271 i40evf_stat_str(hw, ret),
1272 i40evf_aq_str(hw, hw->aq.asq_last_status));
Helin Zhang2c86ac32015-10-27 16:15:06 -04001273 }
1274
1275 return ret;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001276
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001277}
1278
1279/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001280 * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001281 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001282 *
1283 * Returns 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001284 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001285static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001286{
1287 struct i40e_hw *hw = &adapter->hw;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001288 u32 *dw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001289 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001290
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001291 dw = (u32 *)adapter->rss_key;
1292 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1293 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001294
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001295 dw = (u32 *)adapter->rss_lut;
1296 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1297 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001298
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001299 i40e_flush(hw);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001300
1301 return 0;
1302}
1303
1304/**
1305 * i40evf_config_rss - Configure RSS keys and lut
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001306 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001307 *
1308 * Returns 0 on success, negative on failure
1309 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001310int i40evf_config_rss(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001311{
Helin Zhang2c86ac32015-10-27 16:15:06 -04001312
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001313 if (RSS_PF(adapter)) {
1314 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1315 I40EVF_FLAG_AQ_SET_RSS_KEY;
1316 return 0;
1317 } else if (RSS_AQ(adapter)) {
1318 return i40evf_config_rss_aq(adapter);
1319 } else {
1320 return i40evf_config_rss_reg(adapter);
1321 }
Helin Zhang90b02b42015-10-26 19:44:33 -04001322}
1323
1324/**
Helin Zhang2c86ac32015-10-27 16:15:06 -04001325 * i40evf_fill_rss_lut - Fill the lut with default values
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001326 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001327 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001328static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
Helin Zhang2c86ac32015-10-27 16:15:06 -04001329{
1330 u16 i;
1331
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001332 for (i = 0; i < adapter->rss_lut_size; i++)
1333 adapter->rss_lut[i] = i % adapter->num_active_queues;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001334}
1335
1336/**
Helin Zhang96a81982015-10-26 19:44:31 -04001337 * i40evf_init_rss - Prepare for RSS
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001338 * @adapter: board private structure
Helin Zhang2c86ac32015-10-27 16:15:06 -04001339 *
1340 * Return 0 on success, negative on failure
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001341 **/
Helin Zhang2c86ac32015-10-27 16:15:06 -04001342static int i40evf_init_rss(struct i40evf_adapter *adapter)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001343{
1344 struct i40e_hw *hw = &adapter->hw;
Helin Zhang2c86ac32015-10-27 16:15:06 -04001345 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001346
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001347 if (!RSS_PF(adapter)) {
1348 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02001349 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001350 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001351 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1352 else
1353 adapter->hena = I40E_DEFAULT_RSS_HENA;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001354
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001355 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1356 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1357 }
Helin Zhang66f9af852015-10-26 19:44:34 -04001358
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001359 i40evf_fill_rss_lut(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04001360
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001361 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1362 ret = i40evf_config_rss(adapter);
Helin Zhang2c86ac32015-10-27 16:15:06 -04001363
1364 return ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001365}
1366
1367/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001368 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1369 * @adapter: board private structure to initialize
1370 *
1371 * We allocate one q_vector per queue interrupt. If allocation fails we
1372 * return -ENOMEM.
1373 **/
1374static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1375{
Mitch Williams7d96ba12015-10-26 19:44:39 -04001376 int q_idx = 0, num_q_vectors;
Greg Rose5eae00c2013-12-21 06:12:45 +00001377 struct i40e_q_vector *q_vector;
1378
1379 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williams0dd438d2015-10-26 19:44:40 -04001380 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
Mitch Williams7d96ba12015-10-26 19:44:39 -04001381 GFP_KERNEL);
1382 if (!adapter->q_vectors)
Alan Cox311f23e2016-03-01 16:02:15 -08001383 return -ENOMEM;
Greg Rose5eae00c2013-12-21 06:12:45 +00001384
1385 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001386 q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001387 q_vector->adapter = adapter;
1388 q_vector->vsi = &adapter->vsi;
1389 q_vector->v_idx = q_idx;
Alexander Duycka3f9fb52017-12-29 08:48:53 -05001390 q_vector->reg_idx = q_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001391 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001392 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001393 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001394 }
1395
1396 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001397}
1398
1399/**
1400 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1401 * @adapter: board private structure to initialize
1402 *
1403 * This function frees the memory allocated to the q_vectors. In addition if
1404 * NAPI is enabled it will delete any references to the NAPI struct prior
1405 * to freeing the q_vector.
1406 **/
1407static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1408{
1409 int q_idx, num_q_vectors;
1410 int napi_vectors;
1411
Jacob Kelleref4603e2016-11-08 13:05:08 -08001412 if (!adapter->q_vectors)
1413 return;
1414
Greg Rose5eae00c2013-12-21 06:12:45 +00001415 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001416 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001417
1418 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001419 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001420 if (q_idx < napi_vectors)
1421 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001422 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001423 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001424 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001425}
1426
1427/**
1428 * i40evf_reset_interrupt_capability - Reset MSIX setup
1429 * @adapter: board private structure
1430 *
1431 **/
1432void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1433{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001434 if (!adapter->msix_entries)
1435 return;
1436
Greg Rose5eae00c2013-12-21 06:12:45 +00001437 pci_disable_msix(adapter->pdev);
1438 kfree(adapter->msix_entries);
1439 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001440}
1441
1442/**
1443 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1444 * @adapter: board private structure to initialize
1445 *
1446 **/
1447int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1448{
1449 int err;
1450
Jacob Keller283aeaf2017-04-19 09:25:59 -04001451 err = i40evf_alloc_queues(adapter);
1452 if (err) {
1453 dev_err(&adapter->pdev->dev,
1454 "Unable to allocate memory for queues\n");
1455 goto err_alloc_queues;
1456 }
1457
Jacob Keller62fe2a82016-07-27 12:02:33 -07001458 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001459 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001460 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001461 if (err) {
1462 dev_err(&adapter->pdev->dev,
1463 "Unable to setup interrupt capabilities\n");
1464 goto err_set_interrupt;
1465 }
1466
1467 err = i40evf_alloc_q_vectors(adapter);
1468 if (err) {
1469 dev_err(&adapter->pdev->dev,
1470 "Unable to allocate memory for queue vectors\n");
1471 goto err_alloc_q_vectors;
1472 }
1473
Greg Rose5eae00c2013-12-21 06:12:45 +00001474 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001475 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1476 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001477
1478 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001479err_alloc_q_vectors:
1480 i40evf_reset_interrupt_capability(adapter);
1481err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001482 i40evf_free_queues(adapter);
1483err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001484 return err;
1485}
1486
1487/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001488 * i40evf_free_rss - Free memory used by RSS structs
1489 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001490 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001491static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001492{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001493 kfree(adapter->rss_key);
1494 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001495
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001496 kfree(adapter->rss_lut);
1497 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001498}
1499
1500/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001501 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1502 * @adapter: board private structure
1503 *
1504 * Returns 0 on success, negative on failure
1505 **/
1506static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1507{
1508 struct net_device *netdev = adapter->netdev;
1509 int err;
1510
1511 if (netif_running(netdev))
1512 i40evf_free_traffic_irqs(adapter);
1513 i40evf_free_misc_irq(adapter);
1514 i40evf_reset_interrupt_capability(adapter);
1515 i40evf_free_q_vectors(adapter);
1516 i40evf_free_queues(adapter);
1517
1518 err = i40evf_init_interrupt_scheme(adapter);
1519 if (err)
1520 goto err;
1521
1522 netif_tx_stop_all_queues(netdev);
1523
1524 err = i40evf_request_misc_irq(adapter);
1525 if (err)
1526 goto err;
1527
1528 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1529
Mitch Williams1b7b7592017-08-22 06:57:51 -04001530 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001531
1532 if (RSS_AQ(adapter))
1533 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1534 else
1535 err = i40evf_init_rss(adapter);
1536err:
1537 return err;
1538}
1539
1540/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001541 * i40evf_watchdog_timer - Periodic call-back timer
1542 * @data: pointer to adapter disguised as unsigned long
1543 **/
Kees Cook26566ea2017-10-16 17:29:35 -07001544static void i40evf_watchdog_timer(struct timer_list *t)
Greg Rose5eae00c2013-12-21 06:12:45 +00001545{
Kees Cook26566ea2017-10-16 17:29:35 -07001546 struct i40evf_adapter *adapter = from_timer(adapter, t,
1547 watchdog_timer);
Mitch Williams75a64432014-11-11 20:02:42 +00001548
Greg Rose5eae00c2013-12-21 06:12:45 +00001549 schedule_work(&adapter->watchdog_task);
1550 /* timer will be rescheduled in watchdog task */
1551}
1552
1553/**
1554 * i40evf_watchdog_task - Periodic call-back task
1555 * @work: pointer to work_struct
1556 **/
1557static void i40evf_watchdog_task(struct work_struct *work)
1558{
1559 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001560 struct i40evf_adapter,
1561 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001562 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001563 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001564
Greg Rose5eae00c2013-12-21 06:12:45 +00001565 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001566 goto restart_watchdog;
1567
1568 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001569 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1570 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001571 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1572 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001573 /* A chance for redemption! */
1574 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1575 adapter->state = __I40EVF_STARTUP;
1576 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1577 schedule_delayed_work(&adapter->init_task, 10);
1578 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1579 &adapter->crit_section);
1580 /* Don't reschedule the watchdog, since we've restarted
1581 * the init task. When init_task contacts the PF and
1582 * gets everything set up again, it'll restart the
1583 * watchdog for us. Down, boy. Sit. Stay. Woof.
1584 */
1585 return;
1586 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001587 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001588 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001589 goto watchdog_done;
1590 }
1591
1592 if ((adapter->state < __I40EVF_DOWN) ||
1593 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001594 goto watchdog_done;
1595
Mitch Williamsef8693e2014-02-13 03:48:53 -08001596 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001597 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1598 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001599 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001600 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001601 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001602 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001603 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001604 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001605 goto watchdog_done;
1606 }
1607
1608 /* Process admin queue tasks. After init, everything gets done
1609 * here so we don't race on the admin queue.
1610 */
Mitch Williamsed636962015-04-07 19:45:32 -04001611 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001612 if (!i40evf_asq_done(hw)) {
1613 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1614 i40evf_send_api_ver(adapter);
1615 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001616 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001617 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001618 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1619 i40evf_send_vf_config_msg(adapter);
1620 goto watchdog_done;
1621 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001622
Mitch Williamse284fc82015-03-27 00:12:09 -07001623 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1624 i40evf_disable_queues(adapter);
1625 goto watchdog_done;
1626 }
1627
Greg Rose5eae00c2013-12-21 06:12:45 +00001628 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1629 i40evf_map_queues(adapter);
1630 goto watchdog_done;
1631 }
1632
1633 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1634 i40evf_add_ether_addrs(adapter);
1635 goto watchdog_done;
1636 }
1637
1638 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1639 i40evf_add_vlans(adapter);
1640 goto watchdog_done;
1641 }
1642
1643 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1644 i40evf_del_ether_addrs(adapter);
1645 goto watchdog_done;
1646 }
1647
1648 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1649 i40evf_del_vlans(adapter);
1650 goto watchdog_done;
1651 }
1652
Mariusz Stachura87743702017-07-17 22:09:45 -07001653 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1654 i40evf_enable_vlan_stripping(adapter);
1655 goto watchdog_done;
1656 }
1657
1658 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1659 i40evf_disable_vlan_stripping(adapter);
1660 goto watchdog_done;
1661 }
1662
Greg Rose5eae00c2013-12-21 06:12:45 +00001663 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1664 i40evf_configure_queues(adapter);
1665 goto watchdog_done;
1666 }
1667
1668 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1669 i40evf_enable_queues(adapter);
1670 goto watchdog_done;
1671 }
1672
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001673 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1674 /* This message goes straight to the firmware, not the
1675 * PF, so we don't have to set current_op as we will
1676 * not get a response through the ARQ.
1677 */
Helin Zhang96a81982015-10-26 19:44:31 -04001678 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001679 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1680 goto watchdog_done;
1681 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001682 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1683 i40evf_get_hena(adapter);
1684 goto watchdog_done;
1685 }
1686 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1687 i40evf_set_hena(adapter);
1688 goto watchdog_done;
1689 }
1690 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1691 i40evf_set_rss_key(adapter);
1692 goto watchdog_done;
1693 }
1694 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1695 i40evf_set_rss_lut(adapter);
1696 goto watchdog_done;
1697 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001698
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001699 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001700 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1701 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001702 goto watchdog_done;
1703 }
1704
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001705 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001706 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001707 goto watchdog_done;
1708 }
1709
1710 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1711 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001712 i40evf_set_promiscuous(adapter, 0);
1713 goto watchdog_done;
1714 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001715 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001716
Greg Rose5eae00c2013-12-21 06:12:45 +00001717 if (adapter->state == __I40EVF_RUNNING)
1718 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001719watchdog_done:
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05001720 if (adapter->state == __I40EVF_RUNNING)
1721 i40evf_detect_recover_hung(&adapter->vsi);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001722 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1723restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001724 if (adapter->state == __I40EVF_REMOVE)
1725 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001726 if (adapter->aq_required)
1727 mod_timer(&adapter->watchdog_timer,
1728 jiffies + msecs_to_jiffies(20));
1729 else
1730 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001731 schedule_work(&adapter->adminq_task);
1732}
1733
Joe Perchesdedecb62016-11-01 15:35:14 -07001734static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1735{
1736 struct i40evf_mac_filter *f, *ftmp;
1737 struct i40evf_vlan_filter *fv, *fvtmp;
1738
1739 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1740
Jacob Keller44b034b2017-10-27 11:06:49 -04001741 /* We don't use netif_running() because it may be true prior to
1742 * ndo_open() returning, so we can't assume it means all our open
1743 * tasks have finished, since we're not holding the rtnl_lock here.
1744 */
1745 if (adapter->state == __I40EVF_RUNNING) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001746 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001747 netif_carrier_off(adapter->netdev);
1748 netif_tx_disable(adapter->netdev);
1749 adapter->link_up = false;
1750 i40evf_napi_disable_all(adapter);
1751 i40evf_irq_disable(adapter);
1752 i40evf_free_traffic_irqs(adapter);
1753 i40evf_free_all_tx_resources(adapter);
1754 i40evf_free_all_rx_resources(adapter);
1755 }
1756
Jacob Keller504398f2017-10-27 11:06:50 -04001757 spin_lock_bh(&adapter->mac_vlan_list_lock);
1758
Joe Perchesdedecb62016-11-01 15:35:14 -07001759 /* Delete all of the filters, both MAC and VLAN. */
1760 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1761 list_del(&f->list);
1762 kfree(f);
1763 }
1764
1765 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1766 list_del(&fv->list);
1767 kfree(fv);
1768 }
1769
Jacob Keller504398f2017-10-27 11:06:50 -04001770 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1771
Joe Perchesdedecb62016-11-01 15:35:14 -07001772 i40evf_free_misc_irq(adapter);
1773 i40evf_reset_interrupt_capability(adapter);
1774 i40evf_free_queues(adapter);
1775 i40evf_free_q_vectors(adapter);
1776 kfree(adapter->vf_res);
1777 i40evf_shutdown_adminq(&adapter->hw);
1778 adapter->netdev->flags &= ~IFF_UP;
1779 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1780 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1781 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001782 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001783 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1784}
1785
Mitch Williams67c818a2015-06-19 08:56:30 -07001786#define I40EVF_RESET_WAIT_MS 10
1787#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001788/**
1789 * i40evf_reset_task - Call-back task to handle hardware reset
1790 * @work: pointer to work_struct
1791 *
1792 * During reset we need to shut down and reinitialize the admin queue
1793 * before we can use it to communicate with the PF again. We also clear
1794 * and reinit the rings because that context is lost as well.
1795 **/
1796static void i40evf_reset_task(struct work_struct *work)
1797{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001798 struct i40evf_adapter *adapter = container_of(work,
1799 struct i40evf_adapter,
1800 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001801 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001802 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001803 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001804 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001805 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001806 int i = 0, err;
Jacob Keller44b034b2017-10-27 11:06:49 -04001807 bool running;
Greg Rose5eae00c2013-12-21 06:12:45 +00001808
Avinash Dayanand06aa0402017-12-18 05:16:43 -05001809 /* When device is being removed it doesn't make sense to run the reset
1810 * task, just return in such a case.
1811 */
1812 if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1813 return;
1814
Mitch Williamsed0e8942017-01-24 10:23:59 -08001815 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001816 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001817 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001818 if (CLIENT_ENABLED(adapter)) {
1819 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1820 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1821 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1822 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1823 cancel_delayed_work_sync(&adapter->client_task);
1824 i40evf_notify_client_close(&adapter->vsi, true);
1825 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001826 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001827 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001828 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1829 /* Restart the AQ here. If we have been reset but didn't
1830 * detect it, or if the PF had to reinit, our AQ will be hosed.
1831 */
1832 i40evf_shutdown_adminq(hw);
1833 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001834 i40evf_request_reset(adapter);
1835 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001836 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001837
Mitch Williamsef8693e2014-02-13 03:48:53 -08001838 /* poll until we see the reset actually happen */
1839 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001840 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1841 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1842 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001843 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001844 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001845 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001846 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001847 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001848 goto continue_reset; /* act like the reset happened */
1849 }
1850
1851 /* wait until the reset is complete and the PF is responding to us */
1852 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001853 /* sleep first to make sure a minimum wait time is met */
1854 msleep(I40EVF_RESET_WAIT_MS);
1855
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001856 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1857 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001858 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001859 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001860 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001861
Mitch Williams509a4472015-12-23 12:05:52 -08001862 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001863
Mitch Williamsef8693e2014-02-13 03:48:53 -08001864 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001865 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001866 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001867 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001868 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001869 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001870 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001871
1872continue_reset:
Jacob Keller44b034b2017-10-27 11:06:49 -04001873 /* We don't use netif_running() because it may be true prior to
1874 * ndo_open() returning, so we can't assume it means all our open
1875 * tasks have finished, since we're not holding the rtnl_lock here.
1876 */
1877 running = (adapter->state == __I40EVF_RUNNING);
1878
1879 if (running) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001880 netif_carrier_off(netdev);
1881 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001882 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001883 i40evf_napi_disable_all(adapter);
1884 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001885 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001886
Greg Rose5eae00c2013-12-21 06:12:45 +00001887 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001888 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1889
1890 /* free the Tx/Rx rings and descriptors, might be better to just
1891 * re-use them sometime in the future
1892 */
1893 i40evf_free_all_rx_resources(adapter);
1894 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001895
1896 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001897 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001898 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001899 err = i40evf_init_adminq(hw);
1900 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001901 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1902 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001903 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001904
Alan Brady5b36e8d2017-08-22 06:57:50 -04001905 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1906 err = i40evf_reinit_interrupt_scheme(adapter);
1907 if (err)
1908 goto reset_err;
1909 }
1910
1911 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001912 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001913
Jacob Keller504398f2017-10-27 11:06:50 -04001914 spin_lock_bh(&adapter->mac_vlan_list_lock);
1915
Mitch Williamsac833bb2015-01-29 07:17:19 +00001916 /* re-add all MAC filters */
1917 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1918 f->add = true;
1919 }
1920 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001921 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1922 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001923 }
Jacob Keller504398f2017-10-27 11:06:50 -04001924
1925 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1926
Mitch Williamse6d038d2015-06-04 16:23:58 -04001927 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001928 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Mitch Williams67c818a2015-06-19 08:56:30 -07001929 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001930
1931 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1932
Jacob Keller44b034b2017-10-27 11:06:49 -04001933 /* We were running when the reset started, so we need to restore some
1934 * state here.
1935 */
1936 if (running) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001937 /* allocate transmit descriptors */
1938 err = i40evf_setup_all_tx_resources(adapter);
1939 if (err)
1940 goto reset_err;
1941
1942 /* allocate receive descriptors */
1943 err = i40evf_setup_all_rx_resources(adapter);
1944 if (err)
1945 goto reset_err;
1946
Alan Brady5b36e8d2017-08-22 06:57:50 -04001947 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1948 err = i40evf_request_traffic_irqs(adapter,
1949 netdev->name);
1950 if (err)
1951 goto reset_err;
1952
1953 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1954 }
1955
Greg Rose5eae00c2013-12-21 06:12:45 +00001956 i40evf_configure(adapter);
1957
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001958 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001959
1960 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001961 } else {
1962 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001963 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001964 }
Jacob Keller9b2aef12017-10-27 11:06:52 -04001965 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1966 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001967
Greg Rose5eae00c2013-12-21 06:12:45 +00001968 return;
1969reset_err:
Jacob Keller9b2aef12017-10-27 11:06:52 -04001970 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1971 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams80e72892014-05-10 04:49:06 +00001972 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001973 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001974}
1975
1976/**
1977 * i40evf_adminq_task - worker thread to clean the admin queue
1978 * @work: pointer to work_struct containing our data
1979 **/
1980static void i40evf_adminq_task(struct work_struct *work)
1981{
1982 struct i40evf_adapter *adapter =
1983 container_of(work, struct i40evf_adapter, adminq_task);
1984 struct i40e_hw *hw = &adapter->hw;
1985 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001986 enum virtchnl_ops v_op;
1987 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001988 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001989 u16 pending;
1990
Mitch Williamsef8693e2014-02-13 03:48:53 -08001991 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001992 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001993
Mitch Williams1001dc32014-11-11 20:02:19 +00001994 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1995 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001996 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001997 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001998
Greg Rose5eae00c2013-12-21 06:12:45 +00001999 do {
2000 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002001 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2002 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2003
2004 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002005 break; /* No event to process or error cleaning ARQ */
2006
Tushar Davec969ef42017-06-22 09:44:32 -07002007 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002008 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002009 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002010 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002011 } while (pending);
2012
Mitch Williams67c818a2015-06-19 08:56:30 -07002013 if ((adapter->flags &
2014 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2015 adapter->state == __I40EVF_RESETTING)
2016 goto freedom;
2017
Mitch Williams912257e2014-05-22 06:32:07 +00002018 /* check for error indications */
2019 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002020 if (val == 0xdeadbeef) /* indicates device in reset */
2021 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002022 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002023 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002024 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002025 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002026 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002027 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002028 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002029 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002030 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002031 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002032 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002033 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002034 }
2035 if (oldval != val)
2036 wr32(hw, hw->aq.arq.len, val);
2037
2038 val = rd32(hw, hw->aq.asq.len);
2039 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002040 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002041 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002042 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002043 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002044 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002045 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002046 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002047 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002048 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002049 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002050 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002051 }
2052 if (oldval != val)
2053 wr32(hw, hw->aq.asq.len, val);
2054
Mitch Williams67c818a2015-06-19 08:56:30 -07002055freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002056 kfree(event.msg_buf);
2057out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002058 /* re-enable Admin queue interrupt cause */
2059 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002060}
2061
2062/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002063 * i40evf_client_task - worker thread to perform client work
2064 * @work: pointer to work_struct containing our data
2065 *
2066 * This task handles client interactions. Because client calls can be
2067 * reentrant, we can't handle them in the watchdog.
2068 **/
2069static void i40evf_client_task(struct work_struct *work)
2070{
2071 struct i40evf_adapter *adapter =
2072 container_of(work, struct i40evf_adapter, client_task.work);
2073
2074 /* If we can't get the client bit, just give up. We'll be rescheduled
2075 * later.
2076 */
2077
2078 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2079 return;
2080
2081 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2082 i40evf_client_subtask(adapter);
2083 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2084 goto out;
2085 }
Alan Brady01acc732017-11-14 07:00:51 -05002086 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2087 i40evf_notify_client_l2_params(&adapter->vsi);
2088 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2089 goto out;
2090 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002091 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2092 i40evf_notify_client_close(&adapter->vsi, false);
2093 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2094 goto out;
2095 }
2096 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2097 i40evf_notify_client_open(&adapter->vsi);
2098 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002099 }
2100out:
2101 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2102}
2103
2104/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002105 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2106 * @adapter: board private structure
2107 *
2108 * Free all transmit software resources
2109 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002110void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002111{
2112 int i;
2113
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002114 if (!adapter->tx_rings)
2115 return;
2116
Mitch Williamscc052922014-10-25 03:24:34 +00002117 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002118 if (adapter->tx_rings[i].desc)
2119 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002120}
2121
2122/**
2123 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2124 * @adapter: board private structure
2125 *
2126 * If this function returns with an error, then it's possible one or
2127 * more of the rings is populated (while the rest are not). It is the
2128 * callers duty to clean those orphaned rings.
2129 *
2130 * Return 0 on success, negative on failure
2131 **/
2132static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2133{
2134 int i, err = 0;
2135
Mitch Williamscc052922014-10-25 03:24:34 +00002136 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002137 adapter->tx_rings[i].count = adapter->tx_desc_count;
2138 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002139 if (!err)
2140 continue;
2141 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002142 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002143 break;
2144 }
2145
2146 return err;
2147}
2148
2149/**
2150 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2151 * @adapter: board private structure
2152 *
2153 * If this function returns with an error, then it's possible one or
2154 * more of the rings is populated (while the rest are not). It is the
2155 * callers duty to clean those orphaned rings.
2156 *
2157 * Return 0 on success, negative on failure
2158 **/
2159static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2160{
2161 int i, err = 0;
2162
Mitch Williamscc052922014-10-25 03:24:34 +00002163 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002164 adapter->rx_rings[i].count = adapter->rx_desc_count;
2165 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002166 if (!err)
2167 continue;
2168 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002169 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002170 break;
2171 }
2172 return err;
2173}
2174
2175/**
2176 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2177 * @adapter: board private structure
2178 *
2179 * Free all receive software resources
2180 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002181void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002182{
2183 int i;
2184
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002185 if (!adapter->rx_rings)
2186 return;
2187
Mitch Williamscc052922014-10-25 03:24:34 +00002188 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002189 if (adapter->rx_rings[i].desc)
2190 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002191}
2192
2193/**
2194 * i40evf_open - Called when a network interface is made active
2195 * @netdev: network interface device structure
2196 *
2197 * Returns 0 on success, negative value on failure
2198 *
2199 * The open entry point is called when a network interface is made
2200 * active by the system (IFF_UP). At this point all resources needed
2201 * for transmit and receive operations are allocated, the interrupt
2202 * handler is registered with the OS, the watchdog timer is started,
2203 * and the stack is notified that the interface is ready.
2204 **/
2205static int i40evf_open(struct net_device *netdev)
2206{
2207 struct i40evf_adapter *adapter = netdev_priv(netdev);
2208 int err;
2209
Mitch Williamsef8693e2014-02-13 03:48:53 -08002210 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2211 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2212 return -EIO;
2213 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002214
Jacob Keller9b2aef12017-10-27 11:06:52 -04002215 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2216 &adapter->crit_section))
2217 usleep_range(500, 1000);
2218
2219 if (adapter->state != __I40EVF_DOWN) {
2220 err = -EBUSY;
2221 goto err_unlock;
2222 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002223
2224 /* allocate transmit descriptors */
2225 err = i40evf_setup_all_tx_resources(adapter);
2226 if (err)
2227 goto err_setup_tx;
2228
2229 /* allocate receive descriptors */
2230 err = i40evf_setup_all_rx_resources(adapter);
2231 if (err)
2232 goto err_setup_rx;
2233
2234 /* clear any pending interrupts, may auto mask */
2235 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2236 if (err)
2237 goto err_req_irq;
2238
Mitch Williams44151cd2015-04-27 14:57:17 -04002239 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002240 i40evf_configure(adapter);
2241
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002242 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002243
2244 i40evf_irq_enable(adapter, true);
2245
Jacob Keller9b2aef12017-10-27 11:06:52 -04002246 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2247
Greg Rose5eae00c2013-12-21 06:12:45 +00002248 return 0;
2249
2250err_req_irq:
2251 i40evf_down(adapter);
2252 i40evf_free_traffic_irqs(adapter);
2253err_setup_rx:
2254 i40evf_free_all_rx_resources(adapter);
2255err_setup_tx:
2256 i40evf_free_all_tx_resources(adapter);
Jacob Keller9b2aef12017-10-27 11:06:52 -04002257err_unlock:
2258 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00002259
2260 return err;
2261}
2262
2263/**
2264 * i40evf_close - Disables a network interface
2265 * @netdev: network interface device structure
2266 *
2267 * Returns 0, this is not allowed to fail
2268 *
2269 * The close entry point is called when an interface is de-activated
2270 * by the OS. The hardware is still under the drivers control, but
2271 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2272 * are freed, along with all transmit and receive resources.
2273 **/
2274static int i40evf_close(struct net_device *netdev)
2275{
2276 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002277 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002278
Mitch Williams209dc4d2015-12-09 15:50:27 -08002279 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002280 return 0;
2281
Jacob Keller9b2aef12017-10-27 11:06:52 -04002282 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2283 &adapter->crit_section))
2284 usleep_range(500, 1000);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002285
Jacob Keller0da36b92017-04-19 09:25:55 -04002286 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002287 if (CLIENT_ENABLED(adapter))
2288 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002289
2290 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002291 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002292 i40evf_free_traffic_irqs(adapter);
2293
Jacob Keller9b2aef12017-10-27 11:06:52 -04002294 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2295
Mitch Williams51f38262016-12-12 15:44:11 -08002296 /* We explicitly don't free resources here because the hardware is
2297 * still active and can DMA into memory. Resources are cleared in
2298 * i40evf_virtchnl_completion() after we get confirmation from the PF
2299 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002300 *
2301 * Also, we wait for state to transition to __I40EVF_DOWN before
2302 * returning. State change occurs in i40evf_virtchnl_completion() after
2303 * VF resources are released (which occurs after PF driver processes and
2304 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002305 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002306
2307 status = wait_event_timeout(adapter->down_waitqueue,
2308 adapter->state == __I40EVF_DOWN,
2309 msecs_to_jiffies(200));
2310 if (!status)
2311 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002312 return 0;
2313}
2314
2315/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002316 * i40evf_change_mtu - Change the Maximum Transfer Unit
2317 * @netdev: network interface device structure
2318 * @new_mtu: new value for maximum frame size
2319 *
2320 * Returns 0 on success, negative on failure
2321 **/
2322static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2323{
2324 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002325
Greg Rose5eae00c2013-12-21 06:12:45 +00002326 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002327 if (CLIENT_ENABLED(adapter)) {
2328 i40evf_notify_client_l2_params(&adapter->vsi);
2329 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2330 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002331 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2332 schedule_work(&adapter->reset_task);
2333
Greg Rose5eae00c2013-12-21 06:12:45 +00002334 return 0;
2335}
2336
Alexander Duyck06fc0162016-10-25 16:08:47 -07002337/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002338 * i40e_set_features - set the netdev feature flags
2339 * @netdev: ptr to the netdev being adjusted
2340 * @features: the feature set that the stack is suggesting
2341 * Note: expects to be called while under rtnl_lock()
2342 **/
2343static int i40evf_set_features(struct net_device *netdev,
2344 netdev_features_t features)
2345{
2346 struct i40evf_adapter *adapter = netdev_priv(netdev);
2347
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002348 /* Don't allow changing VLAN_RX flag when VLAN is set for VF
2349 * and return an error in this case
2350 */
2351 if (VLAN_ALLOWED(adapter)) {
2352 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2353 adapter->aq_required |=
2354 I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2355 else
2356 adapter->aq_required |=
2357 I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2358 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
Mariusz Stachura87743702017-07-17 22:09:45 -07002359 return -EINVAL;
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002360 }
Mariusz Stachura87743702017-07-17 22:09:45 -07002361
2362 return 0;
2363}
2364
2365/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002366 * i40evf_features_check - Validate encapsulated packet conforms to limits
2367 * @skb: skb buff
2368 * @netdev: This physical port's netdev
2369 * @features: Offload features that the stack believes apply
2370 **/
2371static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2372 struct net_device *dev,
2373 netdev_features_t features)
2374{
2375 size_t len;
2376
2377 /* No point in doing any of this if neither checksum nor GSO are
2378 * being requested for this frame. We can rule out both by just
2379 * checking for CHECKSUM_PARTIAL
2380 */
2381 if (skb->ip_summed != CHECKSUM_PARTIAL)
2382 return features;
2383
2384 /* We cannot support GSO if the MSS is going to be less than
2385 * 64 bytes. If it is then we need to drop support for GSO.
2386 */
2387 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2388 features &= ~NETIF_F_GSO_MASK;
2389
2390 /* MACLEN can support at most 63 words */
2391 len = skb_network_header(skb) - skb->data;
2392 if (len & ~(63 * 2))
2393 goto out_err;
2394
2395 /* IPLEN and EIPLEN can support at most 127 dwords */
2396 len = skb_transport_header(skb) - skb_network_header(skb);
2397 if (len & ~(127 * 4))
2398 goto out_err;
2399
2400 if (skb->encapsulation) {
2401 /* L4TUNLEN can support 127 words */
2402 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2403 if (len & ~(127 * 2))
2404 goto out_err;
2405
2406 /* IPLEN can support at most 127 dwords */
2407 len = skb_inner_transport_header(skb) -
2408 skb_inner_network_header(skb);
2409 if (len & ~(127 * 4))
2410 goto out_err;
2411 }
2412
2413 /* No need to validate L4LEN as TCP is the only protocol with a
2414 * a flexible value and we support all possible values supported
2415 * by TCP, which is at most 15 dwords
2416 */
2417
2418 return features;
2419out_err:
2420 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2421}
2422
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002423/**
2424 * i40evf_fix_features - fix up the netdev feature bits
2425 * @netdev: our net device
2426 * @features: desired feature bits
2427 *
2428 * Returns fixed-up features bits
2429 **/
2430static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2431 netdev_features_t features)
2432{
2433 struct i40evf_adapter *adapter = netdev_priv(netdev);
2434
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002435 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
2436 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
2437 NETIF_F_HW_VLAN_CTAG_RX |
2438 NETIF_F_HW_VLAN_CTAG_FILTER);
2439
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002440 return features;
2441}
2442
Greg Rose5eae00c2013-12-21 06:12:45 +00002443static const struct net_device_ops i40evf_netdev_ops = {
2444 .ndo_open = i40evf_open,
2445 .ndo_stop = i40evf_close,
2446 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002447 .ndo_set_rx_mode = i40evf_set_rx_mode,
2448 .ndo_validate_addr = eth_validate_addr,
2449 .ndo_set_mac_address = i40evf_set_mac,
2450 .ndo_change_mtu = i40evf_change_mtu,
2451 .ndo_tx_timeout = i40evf_tx_timeout,
2452 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2453 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002454 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002455 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002456 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002457#ifdef CONFIG_NET_POLL_CONTROLLER
2458 .ndo_poll_controller = i40evf_netpoll,
2459#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002460};
2461
2462/**
2463 * i40evf_check_reset_complete - check that VF reset is complete
2464 * @hw: pointer to hw struct
2465 *
2466 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2467 **/
2468static int i40evf_check_reset_complete(struct i40e_hw *hw)
2469{
2470 u32 rstat;
2471 int i;
2472
2473 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002474 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2475 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002476 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2477 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002478 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002479 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002480 }
2481 return -EBUSY;
2482}
2483
2484/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002485 * i40evf_process_config - Process the config information we got from the PF
2486 * @adapter: board private structure
2487 *
2488 * Verify that we have a valid config struct, and set up our netdev features
2489 * and our VSI struct.
2490 **/
2491int i40evf_process_config(struct i40evf_adapter *adapter)
2492{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002493 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002494 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002495 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002496 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002497 netdev_features_t hw_enc_features;
2498 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002499
2500 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002501 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002502 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002503 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002504 }
2505 if (!adapter->vsi_res) {
2506 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2507 return -ENODEV;
2508 }
2509
Alan Brady5b36e8d2017-08-22 06:57:50 -04002510 if (num_req_queues &&
2511 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2512 /* Problem. The PF gave us fewer queues than what we had
2513 * negotiated in our request. Need a reset to see if we can't
2514 * get back to a working state.
2515 */
2516 dev_err(&adapter->pdev->dev,
2517 "Requested %d queues, but PF only gave us %d.\n",
2518 num_req_queues,
2519 adapter->vsi_res->num_queue_pairs);
2520 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2521 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2522 i40evf_schedule_reset(adapter);
2523 return -ENODEV;
2524 }
2525 adapter->num_req_queues = 0;
2526
Preethi Banalabacd75c2017-03-27 14:43:18 -07002527 hw_enc_features = NETIF_F_SG |
2528 NETIF_F_IP_CSUM |
2529 NETIF_F_IPV6_CSUM |
2530 NETIF_F_HIGHDMA |
2531 NETIF_F_SOFT_FEATURES |
2532 NETIF_F_TSO |
2533 NETIF_F_TSO_ECN |
2534 NETIF_F_TSO6 |
2535 NETIF_F_SCTP_CRC |
2536 NETIF_F_RXHASH |
2537 NETIF_F_RXCSUM |
2538 0;
2539
2540 /* advertise to stack only if offloads for encapsulated packets is
2541 * supported
2542 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002543 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002544 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002545 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002546 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002547 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002548 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002549 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002550 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002551 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002552
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002553 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002554 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002555 netdev->gso_partial_features |=
2556 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002557
Preethi Banalabacd75c2017-03-27 14:43:18 -07002558 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2559 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2560 netdev->hw_enc_features |= hw_enc_features;
2561 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002562 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002563 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002564
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002565 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002566 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002567 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002568 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002569
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002570 /* Enable VLAN features if supported */
2571 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2572 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
2573 NETIF_F_HW_VLAN_CTAG_RX);
2574
Preethi Banalabacd75c2017-03-27 14:43:18 -07002575 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002576
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002577 netdev->features |= hw_features;
2578
2579 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2580 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002581
2582 adapter->vsi.id = adapter->vsi_res->vsi_id;
2583
2584 adapter->vsi.back = adapter;
2585 adapter->vsi.base_vector = 1;
2586 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002587 vsi->netdev = adapter->netdev;
2588 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002589 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002590 adapter->rss_key_size = vfres->rss_key_size;
2591 adapter->rss_lut_size = vfres->rss_lut_size;
2592 } else {
2593 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2594 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2595 }
2596
Mitch Williamse6d038d2015-06-04 16:23:58 -04002597 return 0;
2598}
2599
2600/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002601 * i40evf_init_task - worker thread to perform delayed initialization
2602 * @work: pointer to work_struct containing our data
2603 *
2604 * This task completes the work that was begun in probe. Due to the nature
2605 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002606 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002607 * whole system, we'll do it in a task so we can sleep.
2608 * This task only runs during driver init. Once we've established
2609 * communications with the PF driver and set up our netdev, the watchdog
2610 * takes over.
2611 **/
2612static void i40evf_init_task(struct work_struct *work)
2613{
2614 struct i40evf_adapter *adapter = container_of(work,
2615 struct i40evf_adapter,
2616 init_task.work);
2617 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002618 struct i40e_hw *hw = &adapter->hw;
2619 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002620 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002621
2622 switch (adapter->state) {
2623 case __I40EVF_STARTUP:
2624 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002625 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2626 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002627 err = i40e_set_mac_type(hw);
2628 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002629 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2630 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002631 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002632 }
2633 err = i40evf_check_reset_complete(hw);
2634 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002635 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002636 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002637 goto err;
2638 }
2639 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2640 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2641 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2642 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2643
2644 err = i40evf_init_adminq(hw);
2645 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002646 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2647 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002648 goto err;
2649 }
2650 err = i40evf_send_api_ver(adapter);
2651 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002652 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002653 i40evf_shutdown_adminq(hw);
2654 goto err;
2655 }
2656 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2657 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002658 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002659 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002660 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002661 i40evf_shutdown_adminq(hw);
2662 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002663 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002664 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002665
2666 /* aq msg sent, awaiting reply */
2667 err = i40evf_verify_api_ver(adapter);
2668 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002669 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002670 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002671 else
2672 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2673 adapter->pf_version.major,
2674 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002675 VIRTCHNL_VERSION_MAJOR,
2676 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002677 goto err;
2678 }
2679 err = i40evf_send_vf_config_msg(adapter);
2680 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002681 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002682 err);
2683 goto err;
2684 }
2685 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2686 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002687 case __I40EVF_INIT_GET_RESOURCES:
2688 /* aq msg sent, awaiting reply */
2689 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002690 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002691 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002692 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002693 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002694 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002695 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002696 }
2697 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002698 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002699 err = i40evf_send_vf_config_msg(adapter);
2700 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002701 } else if (err == I40E_ERR_PARAM) {
2702 /* We only get ERR_PARAM if the device is in a very bad
2703 * state or if we've been disabled for previous bad
2704 * behavior. Either way, we're done now.
2705 */
2706 i40evf_shutdown_adminq(hw);
2707 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2708 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002709 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002710 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002711 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2712 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002713 goto err_alloc;
2714 }
2715 adapter->state = __I40EVF_INIT_SW;
2716 break;
2717 default:
2718 goto err_alloc;
2719 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002720
Mitch Williamse6d038d2015-06-04 16:23:58 -04002721 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002722 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002723 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002724
2725 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2726
Greg Rose5eae00c2013-12-21 06:12:45 +00002727 netdev->netdev_ops = &i40evf_netdev_ops;
2728 i40evf_set_ethtool_ops(netdev);
2729 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002730
Jarod Wilson91c527a2016-10-17 15:54:05 -04002731 /* MTU range: 68 - 9710 */
2732 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002733 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002734
Greg Rose5eae00c2013-12-21 06:12:45 +00002735 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002736 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002737 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002738 eth_hw_addr_random(netdev);
2739 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2740 } else {
2741 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2742 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2743 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002744 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002745
Kees Cook26566ea2017-10-16 17:29:35 -07002746 timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
Greg Rose5eae00c2013-12-21 06:12:45 +00002747 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2748
Mitch Williamsd732a182014-04-24 06:41:37 +00002749 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2750 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002751 err = i40evf_init_interrupt_scheme(adapter);
2752 if (err)
2753 goto err_sw_init;
2754 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002755 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002756 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002757 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2758
Greg Rose5eae00c2013-12-21 06:12:45 +00002759 err = i40evf_request_misc_irq(adapter);
2760 if (err)
2761 goto err_sw_init;
2762
2763 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002764 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002765
Mitch Williamsef8693e2014-02-13 03:48:53 -08002766 if (!adapter->netdev_registered) {
2767 err = register_netdev(netdev);
2768 if (err)
2769 goto err_register;
2770 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002771
2772 adapter->netdev_registered = true;
2773
2774 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002775 if (CLIENT_ALLOWED(adapter)) {
2776 err = i40evf_lan_add_device(adapter);
2777 if (err)
2778 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2779 err);
2780 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002781
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002782 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002783 if (netdev->features & NETIF_F_GRO)
2784 dev_info(&pdev->dev, "GRO is enabled\n");
2785
Greg Rose5eae00c2013-12-21 06:12:45 +00002786 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002787 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002788 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002789 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002790
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002791 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2792 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2793 if (!adapter->rss_key || !adapter->rss_lut)
2794 goto err_mem;
2795
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002796 if (RSS_AQ(adapter)) {
2797 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2798 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2799 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002800 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002801 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002802 return;
2803restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002804 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002805 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002806err_mem:
2807 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002808err_register:
2809 i40evf_free_misc_irq(adapter);
2810err_sw_init:
2811 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002812err_alloc:
2813 kfree(adapter->vf_res);
2814 adapter->vf_res = NULL;
2815err:
2816 /* Things went into the weeds, so try again later */
2817 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002818 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002819 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002820 i40evf_shutdown_adminq(hw);
2821 adapter->state = __I40EVF_STARTUP;
2822 schedule_delayed_work(&adapter->init_task, HZ * 5);
2823 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002824 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002825 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002826}
2827
2828/**
2829 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2830 * @pdev: pci device structure
2831 **/
2832static void i40evf_shutdown(struct pci_dev *pdev)
2833{
2834 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002835 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002836
2837 netif_device_detach(netdev);
2838
2839 if (netif_running(netdev))
2840 i40evf_close(netdev);
2841
Mitch Williams00293fd2015-01-09 11:18:18 +00002842 /* Prevent the watchdog from running. */
2843 adapter->state = __I40EVF_REMOVE;
2844 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002845
Greg Rose5eae00c2013-12-21 06:12:45 +00002846#ifdef CONFIG_PM
2847 pci_save_state(pdev);
2848
2849#endif
2850 pci_disable_device(pdev);
2851}
2852
2853/**
2854 * i40evf_probe - Device Initialization Routine
2855 * @pdev: PCI device information struct
2856 * @ent: entry in i40evf_pci_tbl
2857 *
2858 * Returns 0 on success, negative on failure
2859 *
2860 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2861 * The OS initialization, configuring of the adapter private structure,
2862 * and a hardware reset occur.
2863 **/
2864static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2865{
2866 struct net_device *netdev;
2867 struct i40evf_adapter *adapter = NULL;
2868 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002869 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002870
2871 err = pci_enable_device(pdev);
2872 if (err)
2873 return err;
2874
Mitch Williams64942942014-02-11 08:26:33 +00002875 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002876 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002877 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2878 if (err) {
2879 dev_err(&pdev->dev,
2880 "DMA configuration failed: 0x%x\n", err);
2881 goto err_dma;
2882 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002883 }
2884
2885 err = pci_request_regions(pdev, i40evf_driver_name);
2886 if (err) {
2887 dev_err(&pdev->dev,
2888 "pci_request_regions failed 0x%x\n", err);
2889 goto err_pci_reg;
2890 }
2891
2892 pci_enable_pcie_error_reporting(pdev);
2893
2894 pci_set_master(pdev);
2895
Mitch Williams1255b7a2015-11-06 15:25:59 -08002896 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002897 if (!netdev) {
2898 err = -ENOMEM;
2899 goto err_alloc_etherdev;
2900 }
2901
2902 SET_NETDEV_DEV(netdev, &pdev->dev);
2903
2904 pci_set_drvdata(pdev, netdev);
2905 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002906
2907 adapter->netdev = netdev;
2908 adapter->pdev = pdev;
2909
2910 hw = &adapter->hw;
2911 hw->back = adapter;
2912
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002913 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002914 adapter->state = __I40EVF_STARTUP;
2915
2916 /* Call save state here because it relies on the adapter struct. */
2917 pci_save_state(pdev);
2918
2919 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2920 pci_resource_len(pdev, 0));
2921 if (!hw->hw_addr) {
2922 err = -EIO;
2923 goto err_ioremap;
2924 }
2925 hw->vendor_id = pdev->vendor;
2926 hw->device_id = pdev->device;
2927 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2928 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2929 hw->subsystem_device_id = pdev->subsystem_device;
2930 hw->bus.device = PCI_SLOT(pdev->devfn);
2931 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002932 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002933
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002934 /* set up the locks for the AQ, do this only once in probe
2935 * and destroy them only once in remove
2936 */
2937 mutex_init(&hw->aq.asq_mutex);
2938 mutex_init(&hw->aq.arq_mutex);
2939
Jacob Keller504398f2017-10-27 11:06:50 -04002940 spin_lock_init(&adapter->mac_vlan_list_lock);
2941
Serey Kong8bb1a542014-08-01 13:27:15 -07002942 INIT_LIST_HEAD(&adapter->mac_filter_list);
2943 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2944
Greg Rose5eae00c2013-12-21 06:12:45 +00002945 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2946 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2947 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002948 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002949 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002950 schedule_delayed_work(&adapter->init_task,
2951 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002952
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002953 /* Setup the wait queue for indicating transition to down status */
2954 init_waitqueue_head(&adapter->down_waitqueue);
2955
Greg Rose5eae00c2013-12-21 06:12:45 +00002956 return 0;
2957
2958err_ioremap:
2959 free_netdev(netdev);
2960err_alloc_etherdev:
2961 pci_release_regions(pdev);
2962err_pci_reg:
2963err_dma:
2964 pci_disable_device(pdev);
2965 return err;
2966}
2967
2968#ifdef CONFIG_PM
2969/**
2970 * i40evf_suspend - Power management suspend routine
2971 * @pdev: PCI device information struct
2972 * @state: unused
2973 *
2974 * Called when the system (VM) is entering sleep/suspend.
2975 **/
2976static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2977{
2978 struct net_device *netdev = pci_get_drvdata(pdev);
2979 struct i40evf_adapter *adapter = netdev_priv(netdev);
2980 int retval = 0;
2981
2982 netif_device_detach(netdev);
2983
Jacob Keller9b2aef12017-10-27 11:06:52 -04002984 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2985 &adapter->crit_section))
2986 usleep_range(500, 1000);
2987
Greg Rose5eae00c2013-12-21 06:12:45 +00002988 if (netif_running(netdev)) {
2989 rtnl_lock();
2990 i40evf_down(adapter);
2991 rtnl_unlock();
2992 }
2993 i40evf_free_misc_irq(adapter);
2994 i40evf_reset_interrupt_capability(adapter);
2995
Jacob Keller9b2aef12017-10-27 11:06:52 -04002996 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2997
Greg Rose5eae00c2013-12-21 06:12:45 +00002998 retval = pci_save_state(pdev);
2999 if (retval)
3000 return retval;
3001
3002 pci_disable_device(pdev);
3003
3004 return 0;
3005}
3006
3007/**
Joe Perchesdbedd442015-03-06 20:49:12 -08003008 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00003009 * @pdev: PCI device information struct
3010 *
3011 * Called when the system (VM) is resumed from sleep/suspend.
3012 **/
3013static int i40evf_resume(struct pci_dev *pdev)
3014{
3015 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3016 struct net_device *netdev = adapter->netdev;
3017 u32 err;
3018
3019 pci_set_power_state(pdev, PCI_D0);
3020 pci_restore_state(pdev);
3021 /* pci_restore_state clears dev->state_saved so call
3022 * pci_save_state to restore it.
3023 */
3024 pci_save_state(pdev);
3025
3026 err = pci_enable_device_mem(pdev);
3027 if (err) {
3028 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3029 return err;
3030 }
3031 pci_set_master(pdev);
3032
3033 rtnl_lock();
3034 err = i40evf_set_interrupt_capability(adapter);
3035 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003036 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003037 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3038 return err;
3039 }
3040 err = i40evf_request_misc_irq(adapter);
3041 rtnl_unlock();
3042 if (err) {
3043 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3044 return err;
3045 }
3046
3047 schedule_work(&adapter->reset_task);
3048
3049 netif_device_attach(netdev);
3050
3051 return err;
3052}
3053
3054#endif /* CONFIG_PM */
3055/**
3056 * i40evf_remove - Device Removal Routine
3057 * @pdev: PCI device information struct
3058 *
3059 * i40evf_remove is called by the PCI subsystem to alert the driver
3060 * that it should release a PCI device. The could be caused by a
3061 * Hot-Plug event, or because the driver is going to be removed from
3062 * memory.
3063 **/
3064static void i40evf_remove(struct pci_dev *pdev)
3065{
3066 struct net_device *netdev = pci_get_drvdata(pdev);
3067 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003068 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003069 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003070 int err;
Avinash Dayanand06aa0402017-12-18 05:16:43 -05003071 /* Indicate we are in remove and not to run reset_task */
3072 set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00003073 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003074 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003075 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003076 if (adapter->netdev_registered) {
3077 unregister_netdev(netdev);
3078 adapter->netdev_registered = false;
3079 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003080 if (CLIENT_ALLOWED(adapter)) {
3081 err = i40evf_lan_del_device(adapter);
3082 if (err)
3083 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3084 err);
3085 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003086
Mitch Williamsf4a71882015-01-09 11:18:16 +00003087 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003088 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003089 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003090 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003091 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003092 /* If the FW isn't responding, kick it once, but only once. */
3093 if (!i40evf_asq_done(hw)) {
3094 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003095 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003096 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003097 i40evf_free_all_tx_resources(adapter);
3098 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003099 i40evf_misc_irq_disable(adapter);
3100 i40evf_free_misc_irq(adapter);
3101 i40evf_reset_interrupt_capability(adapter);
3102 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003103
Mitch Williamse5d17c32014-06-04 04:22:38 +00003104 if (adapter->watchdog_timer.function)
3105 del_timer_sync(&adapter->watchdog_timer);
3106
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003107 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003108
Greg Rose5eae00c2013-12-21 06:12:45 +00003109 if (hw->aq.asq.count)
3110 i40evf_shutdown_adminq(hw);
3111
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003112 /* destroy the locks only once, here */
3113 mutex_destroy(&hw->aq.arq_mutex);
3114 mutex_destroy(&hw->aq.asq_mutex);
3115
Greg Rose5eae00c2013-12-21 06:12:45 +00003116 iounmap(hw->hw_addr);
3117 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003118 i40evf_free_all_tx_resources(adapter);
3119 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003120 i40evf_free_queues(adapter);
3121 kfree(adapter->vf_res);
Jacob Keller504398f2017-10-27 11:06:50 -04003122 spin_lock_bh(&adapter->mac_vlan_list_lock);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003123 /* If we got removed before an up/down sequence, we've got a filter
3124 * hanging out there that we need to get rid of.
3125 */
3126 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3127 list_del(&f->list);
3128 kfree(f);
3129 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003130 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3131 list_del(&f->list);
3132 kfree(f);
3133 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003134
Jacob Keller504398f2017-10-27 11:06:50 -04003135 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3136
Greg Rose5eae00c2013-12-21 06:12:45 +00003137 free_netdev(netdev);
3138
3139 pci_disable_pcie_error_reporting(pdev);
3140
3141 pci_disable_device(pdev);
3142}
3143
3144static struct pci_driver i40evf_driver = {
3145 .name = i40evf_driver_name,
3146 .id_table = i40evf_pci_tbl,
3147 .probe = i40evf_probe,
3148 .remove = i40evf_remove,
3149#ifdef CONFIG_PM
3150 .suspend = i40evf_suspend,
3151 .resume = i40evf_resume,
3152#endif
3153 .shutdown = i40evf_shutdown,
3154};
3155
3156/**
3157 * i40e_init_module - Driver Registration Routine
3158 *
3159 * i40e_init_module is the first routine called when the driver is
3160 * loaded. All it does is register with the PCI subsystem.
3161 **/
3162static int __init i40evf_init_module(void)
3163{
3164 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003165
Greg Rose5eae00c2013-12-21 06:12:45 +00003166 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003167 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003168
3169 pr_info("%s\n", i40evf_copyright);
3170
Jacob Keller6992a6c2016-08-04 11:37:01 -07003171 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3172 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003173 if (!i40evf_wq) {
3174 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3175 return -ENOMEM;
3176 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003177 ret = pci_register_driver(&i40evf_driver);
3178 return ret;
3179}
3180
3181module_init(i40evf_init_module);
3182
3183/**
3184 * i40e_exit_module - Driver Exit Cleanup Routine
3185 *
3186 * i40e_exit_module is called just before the driver is removed
3187 * from memory.
3188 **/
3189static void __exit i40evf_exit_module(void)
3190{
3191 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003192 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003193}
3194
3195module_exit(i40evf_exit_module);
3196
3197/* i40evf_main.c */