blob: d59bf060196b6b36267edfa78d5b93a5930cb09e [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;
Jacob Keller759dc4a2017-07-14 09:10:10 -04001390 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Greg Rose5eae00c2013-12-21 06:12:45 +00001391 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001392 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001393 }
1394
1395 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001396}
1397
1398/**
1399 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1400 * @adapter: board private structure to initialize
1401 *
1402 * This function frees the memory allocated to the q_vectors. In addition if
1403 * NAPI is enabled it will delete any references to the NAPI struct prior
1404 * to freeing the q_vector.
1405 **/
1406static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1407{
1408 int q_idx, num_q_vectors;
1409 int napi_vectors;
1410
Jacob Kelleref4603e2016-11-08 13:05:08 -08001411 if (!adapter->q_vectors)
1412 return;
1413
Greg Rose5eae00c2013-12-21 06:12:45 +00001414 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001415 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001416
1417 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams7d96ba12015-10-26 19:44:39 -04001418 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
Greg Rose5eae00c2013-12-21 06:12:45 +00001419 if (q_idx < napi_vectors)
1420 netif_napi_del(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +00001421 }
Mitch Williams7d96ba12015-10-26 19:44:39 -04001422 kfree(adapter->q_vectors);
Jacob Kelleref4603e2016-11-08 13:05:08 -08001423 adapter->q_vectors = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001424}
1425
1426/**
1427 * i40evf_reset_interrupt_capability - Reset MSIX setup
1428 * @adapter: board private structure
1429 *
1430 **/
1431void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1432{
Alan Brady47d2a5d2016-11-08 13:05:05 -08001433 if (!adapter->msix_entries)
1434 return;
1435
Greg Rose5eae00c2013-12-21 06:12:45 +00001436 pci_disable_msix(adapter->pdev);
1437 kfree(adapter->msix_entries);
1438 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001439}
1440
1441/**
1442 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1443 * @adapter: board private structure to initialize
1444 *
1445 **/
1446int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1447{
1448 int err;
1449
Jacob Keller283aeaf2017-04-19 09:25:59 -04001450 err = i40evf_alloc_queues(adapter);
1451 if (err) {
1452 dev_err(&adapter->pdev->dev,
1453 "Unable to allocate memory for queues\n");
1454 goto err_alloc_queues;
1455 }
1456
Jacob Keller62fe2a82016-07-27 12:02:33 -07001457 rtnl_lock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001458 err = i40evf_set_interrupt_capability(adapter);
Jacob Keller62fe2a82016-07-27 12:02:33 -07001459 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00001460 if (err) {
1461 dev_err(&adapter->pdev->dev,
1462 "Unable to setup interrupt capabilities\n");
1463 goto err_set_interrupt;
1464 }
1465
1466 err = i40evf_alloc_q_vectors(adapter);
1467 if (err) {
1468 dev_err(&adapter->pdev->dev,
1469 "Unable to allocate memory for queue vectors\n");
1470 goto err_alloc_q_vectors;
1471 }
1472
Greg Rose5eae00c2013-12-21 06:12:45 +00001473 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001474 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1475 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001476
1477 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001478err_alloc_q_vectors:
1479 i40evf_reset_interrupt_capability(adapter);
1480err_set_interrupt:
Jacob Keller283aeaf2017-04-19 09:25:59 -04001481 i40evf_free_queues(adapter);
1482err_alloc_queues:
Greg Rose5eae00c2013-12-21 06:12:45 +00001483 return err;
1484}
1485
1486/**
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001487 * i40evf_free_rss - Free memory used by RSS structs
1488 * @adapter: board private structure
Helin Zhang66f9af852015-10-26 19:44:34 -04001489 **/
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001490static void i40evf_free_rss(struct i40evf_adapter *adapter)
Helin Zhang66f9af852015-10-26 19:44:34 -04001491{
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001492 kfree(adapter->rss_key);
1493 adapter->rss_key = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001494
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001495 kfree(adapter->rss_lut);
1496 adapter->rss_lut = NULL;
Helin Zhang66f9af852015-10-26 19:44:34 -04001497}
1498
1499/**
Alan Brady5b36e8d2017-08-22 06:57:50 -04001500 * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1501 * @adapter: board private structure
1502 *
1503 * Returns 0 on success, negative on failure
1504 **/
1505static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1506{
1507 struct net_device *netdev = adapter->netdev;
1508 int err;
1509
1510 if (netif_running(netdev))
1511 i40evf_free_traffic_irqs(adapter);
1512 i40evf_free_misc_irq(adapter);
1513 i40evf_reset_interrupt_capability(adapter);
1514 i40evf_free_q_vectors(adapter);
1515 i40evf_free_queues(adapter);
1516
1517 err = i40evf_init_interrupt_scheme(adapter);
1518 if (err)
1519 goto err;
1520
1521 netif_tx_stop_all_queues(netdev);
1522
1523 err = i40evf_request_misc_irq(adapter);
1524 if (err)
1525 goto err;
1526
1527 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1528
Mitch Williams1b7b7592017-08-22 06:57:51 -04001529 i40evf_map_rings_to_vectors(adapter);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001530
1531 if (RSS_AQ(adapter))
1532 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1533 else
1534 err = i40evf_init_rss(adapter);
1535err:
1536 return err;
1537}
1538
1539/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001540 * i40evf_watchdog_timer - Periodic call-back timer
1541 * @data: pointer to adapter disguised as unsigned long
1542 **/
Kees Cook26566ea2017-10-16 17:29:35 -07001543static void i40evf_watchdog_timer(struct timer_list *t)
Greg Rose5eae00c2013-12-21 06:12:45 +00001544{
Kees Cook26566ea2017-10-16 17:29:35 -07001545 struct i40evf_adapter *adapter = from_timer(adapter, t,
1546 watchdog_timer);
Mitch Williams75a64432014-11-11 20:02:42 +00001547
Greg Rose5eae00c2013-12-21 06:12:45 +00001548 schedule_work(&adapter->watchdog_task);
1549 /* timer will be rescheduled in watchdog task */
1550}
1551
1552/**
1553 * i40evf_watchdog_task - Periodic call-back task
1554 * @work: pointer to work_struct
1555 **/
1556static void i40evf_watchdog_task(struct work_struct *work)
1557{
1558 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001559 struct i40evf_adapter,
1560 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001561 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001562 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001563
Greg Rose5eae00c2013-12-21 06:12:45 +00001564 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001565 goto restart_watchdog;
1566
1567 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001568 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1569 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001570 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1571 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001572 /* A chance for redemption! */
1573 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1574 adapter->state = __I40EVF_STARTUP;
1575 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1576 schedule_delayed_work(&adapter->init_task, 10);
1577 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1578 &adapter->crit_section);
1579 /* Don't reschedule the watchdog, since we've restarted
1580 * the init task. When init_task contacts the PF and
1581 * gets everything set up again, it'll restart the
1582 * watchdog for us. Down, boy. Sit. Stay. Woof.
1583 */
1584 return;
1585 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001586 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001587 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001588 goto watchdog_done;
1589 }
1590
1591 if ((adapter->state < __I40EVF_DOWN) ||
1592 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001593 goto watchdog_done;
1594
Mitch Williamsef8693e2014-02-13 03:48:53 -08001595 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001596 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1597 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001598 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001599 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001600 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001601 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001602 adapter->aq_required = 0;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001603 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001604 goto watchdog_done;
1605 }
1606
1607 /* Process admin queue tasks. After init, everything gets done
1608 * here so we don't race on the admin queue.
1609 */
Mitch Williamsed636962015-04-07 19:45:32 -04001610 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001611 if (!i40evf_asq_done(hw)) {
1612 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1613 i40evf_send_api_ver(adapter);
1614 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001615 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001616 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001617 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1618 i40evf_send_vf_config_msg(adapter);
1619 goto watchdog_done;
1620 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001621
Mitch Williamse284fc82015-03-27 00:12:09 -07001622 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1623 i40evf_disable_queues(adapter);
1624 goto watchdog_done;
1625 }
1626
Greg Rose5eae00c2013-12-21 06:12:45 +00001627 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1628 i40evf_map_queues(adapter);
1629 goto watchdog_done;
1630 }
1631
1632 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1633 i40evf_add_ether_addrs(adapter);
1634 goto watchdog_done;
1635 }
1636
1637 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1638 i40evf_add_vlans(adapter);
1639 goto watchdog_done;
1640 }
1641
1642 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1643 i40evf_del_ether_addrs(adapter);
1644 goto watchdog_done;
1645 }
1646
1647 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1648 i40evf_del_vlans(adapter);
1649 goto watchdog_done;
1650 }
1651
Mariusz Stachura87743702017-07-17 22:09:45 -07001652 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1653 i40evf_enable_vlan_stripping(adapter);
1654 goto watchdog_done;
1655 }
1656
1657 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1658 i40evf_disable_vlan_stripping(adapter);
1659 goto watchdog_done;
1660 }
1661
Greg Rose5eae00c2013-12-21 06:12:45 +00001662 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1663 i40evf_configure_queues(adapter);
1664 goto watchdog_done;
1665 }
1666
1667 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1668 i40evf_enable_queues(adapter);
1669 goto watchdog_done;
1670 }
1671
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001672 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1673 /* This message goes straight to the firmware, not the
1674 * PF, so we don't have to set current_op as we will
1675 * not get a response through the ARQ.
1676 */
Helin Zhang96a81982015-10-26 19:44:31 -04001677 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001678 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1679 goto watchdog_done;
1680 }
Mitch Williams43a3d9b2016-04-12 08:30:44 -07001681 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1682 i40evf_get_hena(adapter);
1683 goto watchdog_done;
1684 }
1685 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1686 i40evf_set_hena(adapter);
1687 goto watchdog_done;
1688 }
1689 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1690 i40evf_set_rss_key(adapter);
1691 goto watchdog_done;
1692 }
1693 if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1694 i40evf_set_rss_lut(adapter);
1695 goto watchdog_done;
1696 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001697
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001698 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001699 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1700 FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001701 goto watchdog_done;
1702 }
1703
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001704 if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07001705 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
Anjali Singhai Jainf42a5c72016-05-03 15:13:10 -07001706 goto watchdog_done;
1707 }
1708
1709 if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1710 (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001711 i40evf_set_promiscuous(adapter, 0);
1712 goto watchdog_done;
1713 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08001714 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
Anjali Singhai Jain47d34832016-04-12 08:30:52 -07001715
Greg Rose5eae00c2013-12-21 06:12:45 +00001716 if (adapter->state == __I40EVF_RUNNING)
1717 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001718watchdog_done:
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05001719 if (adapter->state == __I40EVF_RUNNING)
1720 i40evf_detect_recover_hung(&adapter->vsi);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001721 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1722restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001723 if (adapter->state == __I40EVF_REMOVE)
1724 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001725 if (adapter->aq_required)
1726 mod_timer(&adapter->watchdog_timer,
1727 jiffies + msecs_to_jiffies(20));
1728 else
1729 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001730 schedule_work(&adapter->adminq_task);
1731}
1732
Joe Perchesdedecb62016-11-01 15:35:14 -07001733static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1734{
1735 struct i40evf_mac_filter *f, *ftmp;
1736 struct i40evf_vlan_filter *fv, *fvtmp;
1737
1738 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1739
Jacob Keller44b034b2017-10-27 11:06:49 -04001740 /* We don't use netif_running() because it may be true prior to
1741 * ndo_open() returning, so we can't assume it means all our open
1742 * tasks have finished, since we're not holding the rtnl_lock here.
1743 */
1744 if (adapter->state == __I40EVF_RUNNING) {
Jacob Keller0da36b92017-04-19 09:25:55 -04001745 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Joe Perchesdedecb62016-11-01 15:35:14 -07001746 netif_carrier_off(adapter->netdev);
1747 netif_tx_disable(adapter->netdev);
1748 adapter->link_up = false;
1749 i40evf_napi_disable_all(adapter);
1750 i40evf_irq_disable(adapter);
1751 i40evf_free_traffic_irqs(adapter);
1752 i40evf_free_all_tx_resources(adapter);
1753 i40evf_free_all_rx_resources(adapter);
1754 }
1755
Jacob Keller504398f2017-10-27 11:06:50 -04001756 spin_lock_bh(&adapter->mac_vlan_list_lock);
1757
Joe Perchesdedecb62016-11-01 15:35:14 -07001758 /* Delete all of the filters, both MAC and VLAN. */
1759 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1760 list_del(&f->list);
1761 kfree(f);
1762 }
1763
1764 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1765 list_del(&fv->list);
1766 kfree(fv);
1767 }
1768
Jacob Keller504398f2017-10-27 11:06:50 -04001769 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1770
Joe Perchesdedecb62016-11-01 15:35:14 -07001771 i40evf_free_misc_irq(adapter);
1772 i40evf_reset_interrupt_capability(adapter);
1773 i40evf_free_queues(adapter);
1774 i40evf_free_q_vectors(adapter);
1775 kfree(adapter->vf_res);
1776 i40evf_shutdown_adminq(&adapter->hw);
1777 adapter->netdev->flags &= ~IFF_UP;
1778 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1779 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1780 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001781 wake_up(&adapter->down_waitqueue);
Joe Perchesdedecb62016-11-01 15:35:14 -07001782 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1783}
1784
Mitch Williams67c818a2015-06-19 08:56:30 -07001785#define I40EVF_RESET_WAIT_MS 10
1786#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001787/**
1788 * i40evf_reset_task - Call-back task to handle hardware reset
1789 * @work: pointer to work_struct
1790 *
1791 * During reset we need to shut down and reinitialize the admin queue
1792 * before we can use it to communicate with the PF again. We also clear
1793 * and reinit the rings because that context is lost as well.
1794 **/
1795static void i40evf_reset_task(struct work_struct *work)
1796{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001797 struct i40evf_adapter *adapter = container_of(work,
1798 struct i40evf_adapter,
1799 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001800 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001801 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001802 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001803 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001804 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001805 int i = 0, err;
Jacob Keller44b034b2017-10-27 11:06:49 -04001806 bool running;
Greg Rose5eae00c2013-12-21 06:12:45 +00001807
Avinash Dayanand06aa0402017-12-18 05:16:43 -05001808 /* When device is being removed it doesn't make sense to run the reset
1809 * task, just return in such a case.
1810 */
1811 if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1812 return;
1813
Mitch Williamsed0e8942017-01-24 10:23:59 -08001814 while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
Greg Rose5eae00c2013-12-21 06:12:45 +00001815 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001816 usleep_range(500, 1000);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001817 if (CLIENT_ENABLED(adapter)) {
1818 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1819 I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1820 I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1821 I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1822 cancel_delayed_work_sync(&adapter->client_task);
1823 i40evf_notify_client_close(&adapter->vsi, true);
1824 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001825 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001826 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001827 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1828 /* Restart the AQ here. If we have been reset but didn't
1829 * detect it, or if the PF had to reinit, our AQ will be hosed.
1830 */
1831 i40evf_shutdown_adminq(hw);
1832 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001833 i40evf_request_reset(adapter);
1834 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001835 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001836
Mitch Williamsef8693e2014-02-13 03:48:53 -08001837 /* poll until we see the reset actually happen */
1838 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001839 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1840 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1841 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001842 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001843 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001844 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001845 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001846 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001847 goto continue_reset; /* act like the reset happened */
1848 }
1849
1850 /* wait until the reset is complete and the PF is responding to us */
1851 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001852 /* sleep first to make sure a minimum wait time is met */
1853 msleep(I40EVF_RESET_WAIT_MS);
1854
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001855 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1856 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001857 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001858 break;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001859 }
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001860
Mitch Williams509a4472015-12-23 12:05:52 -08001861 pci_set_master(adapter->pdev);
Jacob Keller7d3f04a2016-10-05 09:30:45 -07001862
Mitch Williamsef8693e2014-02-13 03:48:53 -08001863 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams80e72892014-05-10 04:49:06 +00001864 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001865 reg_val);
Joe Perchesdedecb62016-11-01 15:35:14 -07001866 i40evf_disable_vf(adapter);
Mitch Williamsed0e8942017-01-24 10:23:59 -08001867 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001868 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001869 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001870
1871continue_reset:
Jacob Keller44b034b2017-10-27 11:06:49 -04001872 /* We don't use netif_running() because it may be true prior to
1873 * ndo_open() returning, so we can't assume it means all our open
1874 * tasks have finished, since we're not holding the rtnl_lock here.
1875 */
1876 running = (adapter->state == __I40EVF_RUNNING);
1877
1878 if (running) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001879 netif_carrier_off(netdev);
1880 netif_tx_stop_all_queues(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02001881 adapter->link_up = false;
Mitch Williams67c818a2015-06-19 08:56:30 -07001882 i40evf_napi_disable_all(adapter);
1883 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001884 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001885
Greg Rose5eae00c2013-12-21 06:12:45 +00001886 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001887 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1888
1889 /* free the Tx/Rx rings and descriptors, might be better to just
1890 * re-use them sometime in the future
1891 */
1892 i40evf_free_all_rx_resources(adapter);
1893 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001894
1895 /* kill and reinit the admin queue */
Lihong Yang903e6832016-09-06 18:05:05 -07001896 i40evf_shutdown_adminq(hw);
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07001897 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001898 err = i40evf_init_adminq(hw);
1899 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001900 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1901 err);
Alan Brady5b36e8d2017-08-22 06:57:50 -04001902 adapter->aq_required = 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001903
Alan Brady5b36e8d2017-08-22 06:57:50 -04001904 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1905 err = i40evf_reinit_interrupt_scheme(adapter);
1906 if (err)
1907 goto reset_err;
1908 }
1909
1910 adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
Mitch Williamse6d038d2015-06-04 16:23:58 -04001911 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001912
Jacob Keller504398f2017-10-27 11:06:50 -04001913 spin_lock_bh(&adapter->mac_vlan_list_lock);
1914
Mitch Williamsac833bb2015-01-29 07:17:19 +00001915 /* re-add all MAC filters */
1916 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1917 f->add = true;
1918 }
1919 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001920 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1921 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001922 }
Jacob Keller504398f2017-10-27 11:06:50 -04001923
1924 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1925
Mitch Williamse6d038d2015-06-04 16:23:58 -04001926 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001927 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Mitch Williams67c818a2015-06-19 08:56:30 -07001928 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001929
1930 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1931
Jacob Keller44b034b2017-10-27 11:06:49 -04001932 /* We were running when the reset started, so we need to restore some
1933 * state here.
1934 */
1935 if (running) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001936 /* allocate transmit descriptors */
1937 err = i40evf_setup_all_tx_resources(adapter);
1938 if (err)
1939 goto reset_err;
1940
1941 /* allocate receive descriptors */
1942 err = i40evf_setup_all_rx_resources(adapter);
1943 if (err)
1944 goto reset_err;
1945
Alan Brady5b36e8d2017-08-22 06:57:50 -04001946 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1947 err = i40evf_request_traffic_irqs(adapter,
1948 netdev->name);
1949 if (err)
1950 goto reset_err;
1951
1952 adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
1953 }
1954
Greg Rose5eae00c2013-12-21 06:12:45 +00001955 i40evf_configure(adapter);
1956
Bimmy Pujaricb130a02016-09-06 18:05:03 -07001957 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001958
1959 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001960 } else {
1961 adapter->state = __I40EVF_DOWN;
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04001962 wake_up(&adapter->down_waitqueue);
Greg Rose5eae00c2013-12-21 06:12:45 +00001963 }
Jacob Keller9b2aef12017-10-27 11:06:52 -04001964 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1965 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001966
Greg Rose5eae00c2013-12-21 06:12:45 +00001967 return;
1968reset_err:
Jacob Keller9b2aef12017-10-27 11:06:52 -04001969 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1970 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams80e72892014-05-10 04:49:06 +00001971 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Alan Bradyf0db7892017-07-12 05:46:04 -04001972 i40evf_close(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00001973}
1974
1975/**
1976 * i40evf_adminq_task - worker thread to clean the admin queue
1977 * @work: pointer to work_struct containing our data
1978 **/
1979static void i40evf_adminq_task(struct work_struct *work)
1980{
1981 struct i40evf_adapter *adapter =
1982 container_of(work, struct i40evf_adapter, adminq_task);
1983 struct i40e_hw *hw = &adapter->hw;
1984 struct i40e_arq_event_info event;
Tushar Davec969ef42017-06-22 09:44:32 -07001985 enum virtchnl_ops v_op;
1986 i40e_status ret, v_ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001987 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001988 u16 pending;
1989
Mitch Williamsef8693e2014-02-13 03:48:53 -08001990 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001991 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001992
Mitch Williams1001dc32014-11-11 20:02:19 +00001993 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1994 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001995 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001996 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001997
Greg Rose5eae00c2013-12-21 06:12:45 +00001998 do {
1999 ret = i40evf_clean_arq_element(hw, &event, &pending);
Tushar Davec969ef42017-06-22 09:44:32 -07002000 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2001 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2002
2003 if (ret || !v_op)
Greg Rose5eae00c2013-12-21 06:12:45 +00002004 break; /* No event to process or error cleaning ARQ */
2005
Tushar Davec969ef42017-06-22 09:44:32 -07002006 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00002007 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00002008 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00002009 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00002010 } while (pending);
2011
Mitch Williams67c818a2015-06-19 08:56:30 -07002012 if ((adapter->flags &
2013 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2014 adapter->state == __I40EVF_RESETTING)
2015 goto freedom;
2016
Mitch Williams912257e2014-05-22 06:32:07 +00002017 /* check for error indications */
2018 val = rd32(hw, hw->aq.arq.len);
Mitch Williams19b73d82016-03-10 14:59:49 -08002019 if (val == 0xdeadbeef) /* indicates device in reset */
2020 goto freedom;
Mitch Williams912257e2014-05-22 06:32:07 +00002021 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002022 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002023 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002024 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002025 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002026 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002027 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002028 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002029 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002030 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002031 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002032 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002033 }
2034 if (oldval != val)
2035 wr32(hw, hw->aq.arq.len, val);
2036
2037 val = rd32(hw, hw->aq.asq.len);
2038 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002039 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002040 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002041 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002042 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002043 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002044 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002045 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002046 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002047 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00002048 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04002049 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00002050 }
2051 if (oldval != val)
2052 wr32(hw, hw->aq.asq.len, val);
2053
Mitch Williams67c818a2015-06-19 08:56:30 -07002054freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00002055 kfree(event.msg_buf);
2056out:
Greg Rose5eae00c2013-12-21 06:12:45 +00002057 /* re-enable Admin queue interrupt cause */
2058 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002059}
2060
2061/**
Mitch Williamsed0e8942017-01-24 10:23:59 -08002062 * i40evf_client_task - worker thread to perform client work
2063 * @work: pointer to work_struct containing our data
2064 *
2065 * This task handles client interactions. Because client calls can be
2066 * reentrant, we can't handle them in the watchdog.
2067 **/
2068static void i40evf_client_task(struct work_struct *work)
2069{
2070 struct i40evf_adapter *adapter =
2071 container_of(work, struct i40evf_adapter, client_task.work);
2072
2073 /* If we can't get the client bit, just give up. We'll be rescheduled
2074 * later.
2075 */
2076
2077 if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2078 return;
2079
2080 if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2081 i40evf_client_subtask(adapter);
2082 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2083 goto out;
2084 }
Alan Brady01acc732017-11-14 07:00:51 -05002085 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2086 i40evf_notify_client_l2_params(&adapter->vsi);
2087 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2088 goto out;
2089 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08002090 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2091 i40evf_notify_client_close(&adapter->vsi, false);
2092 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2093 goto out;
2094 }
2095 if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2096 i40evf_notify_client_open(&adapter->vsi);
2097 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002098 }
2099out:
2100 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2101}
2102
2103/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002104 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2105 * @adapter: board private structure
2106 *
2107 * Free all transmit software resources
2108 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002109void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002110{
2111 int i;
2112
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002113 if (!adapter->tx_rings)
2114 return;
2115
Mitch Williamscc052922014-10-25 03:24:34 +00002116 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002117 if (adapter->tx_rings[i].desc)
2118 i40evf_free_tx_resources(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002119}
2120
2121/**
2122 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2123 * @adapter: board private structure
2124 *
2125 * If this function returns with an error, then it's possible one or
2126 * more of the rings is populated (while the rest are not). It is the
2127 * callers duty to clean those orphaned rings.
2128 *
2129 * Return 0 on success, negative on failure
2130 **/
2131static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2132{
2133 int i, err = 0;
2134
Mitch Williamscc052922014-10-25 03:24:34 +00002135 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002136 adapter->tx_rings[i].count = adapter->tx_desc_count;
2137 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002138 if (!err)
2139 continue;
2140 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002141 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002142 break;
2143 }
2144
2145 return err;
2146}
2147
2148/**
2149 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2150 * @adapter: board private structure
2151 *
2152 * If this function returns with an error, then it's possible one or
2153 * more of the rings is populated (while the rest are not). It is the
2154 * callers duty to clean those orphaned rings.
2155 *
2156 * Return 0 on success, negative on failure
2157 **/
2158static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2159{
2160 int i, err = 0;
2161
Mitch Williamscc052922014-10-25 03:24:34 +00002162 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williams0dd438d2015-10-26 19:44:40 -04002163 adapter->rx_rings[i].count = adapter->rx_desc_count;
2164 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002165 if (!err)
2166 continue;
2167 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04002168 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00002169 break;
2170 }
2171 return err;
2172}
2173
2174/**
2175 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2176 * @adapter: board private structure
2177 *
2178 * Free all receive software resources
2179 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07002180void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00002181{
2182 int i;
2183
Mitch Williamsfdb47ae2015-11-19 11:34:18 -08002184 if (!adapter->rx_rings)
2185 return;
2186
Mitch Williamscc052922014-10-25 03:24:34 +00002187 for (i = 0; i < adapter->num_active_queues; i++)
Mitch Williams0dd438d2015-10-26 19:44:40 -04002188 if (adapter->rx_rings[i].desc)
2189 i40evf_free_rx_resources(&adapter->rx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00002190}
2191
2192/**
2193 * i40evf_open - Called when a network interface is made active
2194 * @netdev: network interface device structure
2195 *
2196 * Returns 0 on success, negative value on failure
2197 *
2198 * The open entry point is called when a network interface is made
2199 * active by the system (IFF_UP). At this point all resources needed
2200 * for transmit and receive operations are allocated, the interrupt
2201 * handler is registered with the OS, the watchdog timer is started,
2202 * and the stack is notified that the interface is ready.
2203 **/
2204static int i40evf_open(struct net_device *netdev)
2205{
2206 struct i40evf_adapter *adapter = netdev_priv(netdev);
2207 int err;
2208
Mitch Williamsef8693e2014-02-13 03:48:53 -08002209 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2210 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2211 return -EIO;
2212 }
Mitch Williams209dc4d2015-12-09 15:50:27 -08002213
Jacob Keller9b2aef12017-10-27 11:06:52 -04002214 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2215 &adapter->crit_section))
2216 usleep_range(500, 1000);
2217
2218 if (adapter->state != __I40EVF_DOWN) {
2219 err = -EBUSY;
2220 goto err_unlock;
2221 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002222
2223 /* allocate transmit descriptors */
2224 err = i40evf_setup_all_tx_resources(adapter);
2225 if (err)
2226 goto err_setup_tx;
2227
2228 /* allocate receive descriptors */
2229 err = i40evf_setup_all_rx_resources(adapter);
2230 if (err)
2231 goto err_setup_rx;
2232
2233 /* clear any pending interrupts, may auto mask */
2234 err = i40evf_request_traffic_irqs(adapter, netdev->name);
2235 if (err)
2236 goto err_req_irq;
2237
Mitch Williams44151cd2015-04-27 14:57:17 -04002238 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002239 i40evf_configure(adapter);
2240
Bimmy Pujaricb130a02016-09-06 18:05:03 -07002241 i40evf_up_complete(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002242
2243 i40evf_irq_enable(adapter, true);
2244
Jacob Keller9b2aef12017-10-27 11:06:52 -04002245 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2246
Greg Rose5eae00c2013-12-21 06:12:45 +00002247 return 0;
2248
2249err_req_irq:
2250 i40evf_down(adapter);
2251 i40evf_free_traffic_irqs(adapter);
2252err_setup_rx:
2253 i40evf_free_all_rx_resources(adapter);
2254err_setup_tx:
2255 i40evf_free_all_tx_resources(adapter);
Jacob Keller9b2aef12017-10-27 11:06:52 -04002256err_unlock:
2257 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00002258
2259 return err;
2260}
2261
2262/**
2263 * i40evf_close - Disables a network interface
2264 * @netdev: network interface device structure
2265 *
2266 * Returns 0, this is not allowed to fail
2267 *
2268 * The close entry point is called when an interface is de-activated
2269 * by the OS. The hardware is still under the drivers control, but
2270 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2271 * are freed, along with all transmit and receive resources.
2272 **/
2273static int i40evf_close(struct net_device *netdev)
2274{
2275 struct i40evf_adapter *adapter = netdev_priv(netdev);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002276 int status;
Greg Rose5eae00c2013-12-21 06:12:45 +00002277
Mitch Williams209dc4d2015-12-09 15:50:27 -08002278 if (adapter->state <= __I40EVF_DOWN_PENDING)
Mitch Williamsef8693e2014-02-13 03:48:53 -08002279 return 0;
2280
Jacob Keller9b2aef12017-10-27 11:06:52 -04002281 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2282 &adapter->crit_section))
2283 usleep_range(500, 1000);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002284
Jacob Keller0da36b92017-04-19 09:25:55 -04002285 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002286 if (CLIENT_ENABLED(adapter))
2287 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
Greg Rose5eae00c2013-12-21 06:12:45 +00002288
2289 i40evf_down(adapter);
Mitch Williams209dc4d2015-12-09 15:50:27 -08002290 adapter->state = __I40EVF_DOWN_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002291 i40evf_free_traffic_irqs(adapter);
2292
Jacob Keller9b2aef12017-10-27 11:06:52 -04002293 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2294
Mitch Williams51f38262016-12-12 15:44:11 -08002295 /* We explicitly don't free resources here because the hardware is
2296 * still active and can DMA into memory. Resources are cleared in
2297 * i40evf_virtchnl_completion() after we get confirmation from the PF
2298 * driver that the rings have been stopped.
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002299 *
2300 * Also, we wait for state to transition to __I40EVF_DOWN before
2301 * returning. State change occurs in i40evf_virtchnl_completion() after
2302 * VF resources are released (which occurs after PF driver processes and
2303 * responds to admin queue commands).
Mitch Williams51f38262016-12-12 15:44:11 -08002304 */
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002305
2306 status = wait_event_timeout(adapter->down_waitqueue,
2307 adapter->state == __I40EVF_DOWN,
2308 msecs_to_jiffies(200));
2309 if (!status)
2310 netdev_warn(netdev, "Device resources not yet released\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002311 return 0;
2312}
2313
2314/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002315 * i40evf_change_mtu - Change the Maximum Transfer Unit
2316 * @netdev: network interface device structure
2317 * @new_mtu: new value for maximum frame size
2318 *
2319 * Returns 0 on success, negative on failure
2320 **/
2321static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2322{
2323 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002324
Greg Rose5eae00c2013-12-21 06:12:45 +00002325 netdev->mtu = new_mtu;
Mitch Williamsed0e8942017-01-24 10:23:59 -08002326 if (CLIENT_ENABLED(adapter)) {
2327 i40evf_notify_client_l2_params(&adapter->vsi);
2328 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2329 }
Mitch Williams67c818a2015-06-19 08:56:30 -07002330 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2331 schedule_work(&adapter->reset_task);
2332
Greg Rose5eae00c2013-12-21 06:12:45 +00002333 return 0;
2334}
2335
Alexander Duyck06fc0162016-10-25 16:08:47 -07002336/**
Mariusz Stachura87743702017-07-17 22:09:45 -07002337 * i40e_set_features - set the netdev feature flags
2338 * @netdev: ptr to the netdev being adjusted
2339 * @features: the feature set that the stack is suggesting
2340 * Note: expects to be called while under rtnl_lock()
2341 **/
2342static int i40evf_set_features(struct net_device *netdev,
2343 netdev_features_t features)
2344{
2345 struct i40evf_adapter *adapter = netdev_priv(netdev);
2346
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002347 /* Don't allow changing VLAN_RX flag when VLAN is set for VF
2348 * and return an error in this case
2349 */
2350 if (VLAN_ALLOWED(adapter)) {
2351 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2352 adapter->aq_required |=
2353 I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2354 else
2355 adapter->aq_required |=
2356 I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2357 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
Mariusz Stachura87743702017-07-17 22:09:45 -07002358 return -EINVAL;
Paweł Jabłońskie0f60a82017-12-27 07:32:32 -05002359 }
Mariusz Stachura87743702017-07-17 22:09:45 -07002360
2361 return 0;
2362}
2363
2364/**
Alexander Duyck06fc0162016-10-25 16:08:47 -07002365 * i40evf_features_check - Validate encapsulated packet conforms to limits
2366 * @skb: skb buff
2367 * @netdev: This physical port's netdev
2368 * @features: Offload features that the stack believes apply
2369 **/
2370static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2371 struct net_device *dev,
2372 netdev_features_t features)
2373{
2374 size_t len;
2375
2376 /* No point in doing any of this if neither checksum nor GSO are
2377 * being requested for this frame. We can rule out both by just
2378 * checking for CHECKSUM_PARTIAL
2379 */
2380 if (skb->ip_summed != CHECKSUM_PARTIAL)
2381 return features;
2382
2383 /* We cannot support GSO if the MSS is going to be less than
2384 * 64 bytes. If it is then we need to drop support for GSO.
2385 */
2386 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2387 features &= ~NETIF_F_GSO_MASK;
2388
2389 /* MACLEN can support at most 63 words */
2390 len = skb_network_header(skb) - skb->data;
2391 if (len & ~(63 * 2))
2392 goto out_err;
2393
2394 /* IPLEN and EIPLEN can support at most 127 dwords */
2395 len = skb_transport_header(skb) - skb_network_header(skb);
2396 if (len & ~(127 * 4))
2397 goto out_err;
2398
2399 if (skb->encapsulation) {
2400 /* L4TUNLEN can support 127 words */
2401 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2402 if (len & ~(127 * 2))
2403 goto out_err;
2404
2405 /* IPLEN can support at most 127 dwords */
2406 len = skb_inner_transport_header(skb) -
2407 skb_inner_network_header(skb);
2408 if (len & ~(127 * 4))
2409 goto out_err;
2410 }
2411
2412 /* No need to validate L4LEN as TCP is the only protocol with a
2413 * a flexible value and we support all possible values supported
2414 * by TCP, which is at most 15 dwords
2415 */
2416
2417 return features;
2418out_err:
2419 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2420}
2421
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002422/**
2423 * i40evf_fix_features - fix up the netdev feature bits
2424 * @netdev: our net device
2425 * @features: desired feature bits
2426 *
2427 * Returns fixed-up features bits
2428 **/
2429static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2430 netdev_features_t features)
2431{
2432 struct i40evf_adapter *adapter = netdev_priv(netdev);
2433
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002434 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
2435 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
2436 NETIF_F_HW_VLAN_CTAG_RX |
2437 NETIF_F_HW_VLAN_CTAG_FILTER);
2438
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002439 return features;
2440}
2441
Greg Rose5eae00c2013-12-21 06:12:45 +00002442static const struct net_device_ops i40evf_netdev_ops = {
2443 .ndo_open = i40evf_open,
2444 .ndo_stop = i40evf_close,
2445 .ndo_start_xmit = i40evf_xmit_frame,
Greg Rose5eae00c2013-12-21 06:12:45 +00002446 .ndo_set_rx_mode = i40evf_set_rx_mode,
2447 .ndo_validate_addr = eth_validate_addr,
2448 .ndo_set_mac_address = i40evf_set_mac,
2449 .ndo_change_mtu = i40evf_change_mtu,
2450 .ndo_tx_timeout = i40evf_tx_timeout,
2451 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2452 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck06fc0162016-10-25 16:08:47 -07002453 .ndo_features_check = i40evf_features_check,
Mitch Williamsc4445ae2016-03-18 12:18:07 -07002454 .ndo_fix_features = i40evf_fix_features,
Mariusz Stachura87743702017-07-17 22:09:45 -07002455 .ndo_set_features = i40evf_set_features,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002456#ifdef CONFIG_NET_POLL_CONTROLLER
2457 .ndo_poll_controller = i40evf_netpoll,
2458#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002459};
2460
2461/**
2462 * i40evf_check_reset_complete - check that VF reset is complete
2463 * @hw: pointer to hw struct
2464 *
2465 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2466 **/
2467static int i40evf_check_reset_complete(struct i40e_hw *hw)
2468{
2469 u32 rstat;
2470 int i;
2471
2472 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002473 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2474 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002475 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2476 (rstat == VIRTCHNL_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002477 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002478 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002479 }
2480 return -EBUSY;
2481}
2482
2483/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002484 * i40evf_process_config - Process the config information we got from the PF
2485 * @adapter: board private structure
2486 *
2487 * Verify that we have a valid config struct, and set up our netdev features
2488 * and our VSI struct.
2489 **/
2490int i40evf_process_config(struct i40evf_adapter *adapter)
2491{
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002492 struct virtchnl_vf_resource *vfres = adapter->vf_res;
Alan Brady5b36e8d2017-08-22 06:57:50 -04002493 int i, num_req_queues = adapter->num_req_queues;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002494 struct net_device *netdev = adapter->netdev;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002495 struct i40e_vsi *vsi = &adapter->vsi;
Preethi Banalabacd75c2017-03-27 14:43:18 -07002496 netdev_features_t hw_enc_features;
2497 netdev_features_t hw_features;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002498
2499 /* got VF config message back from PF, now we can parse it */
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002500 for (i = 0; i < vfres->num_vsis; i++) {
Jesse Brandeburgff3f4cc2017-05-11 11:23:16 -07002501 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002502 adapter->vsi_res = &vfres->vsi_res[i];
Mitch Williamse6d038d2015-06-04 16:23:58 -04002503 }
2504 if (!adapter->vsi_res) {
2505 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2506 return -ENODEV;
2507 }
2508
Alan Brady5b36e8d2017-08-22 06:57:50 -04002509 if (num_req_queues &&
2510 num_req_queues != adapter->vsi_res->num_queue_pairs) {
2511 /* Problem. The PF gave us fewer queues than what we had
2512 * negotiated in our request. Need a reset to see if we can't
2513 * get back to a working state.
2514 */
2515 dev_err(&adapter->pdev->dev,
2516 "Requested %d queues, but PF only gave us %d.\n",
2517 num_req_queues,
2518 adapter->vsi_res->num_queue_pairs);
2519 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
2520 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2521 i40evf_schedule_reset(adapter);
2522 return -ENODEV;
2523 }
2524 adapter->num_req_queues = 0;
2525
Preethi Banalabacd75c2017-03-27 14:43:18 -07002526 hw_enc_features = NETIF_F_SG |
2527 NETIF_F_IP_CSUM |
2528 NETIF_F_IPV6_CSUM |
2529 NETIF_F_HIGHDMA |
2530 NETIF_F_SOFT_FEATURES |
2531 NETIF_F_TSO |
2532 NETIF_F_TSO_ECN |
2533 NETIF_F_TSO6 |
2534 NETIF_F_SCTP_CRC |
2535 NETIF_F_RXHASH |
2536 NETIF_F_RXCSUM |
2537 0;
2538
2539 /* advertise to stack only if offloads for encapsulated packets is
2540 * supported
2541 */
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002542 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
Preethi Banalabacd75c2017-03-27 14:43:18 -07002543 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002544 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002545 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002546 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002547 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002548 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002549 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002550 0;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002551
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002552 if (!(vfres->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002553 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
Preethi Banalabacd75c2017-03-27 14:43:18 -07002554 netdev->gso_partial_features |=
2555 NETIF_F_GSO_UDP_TUNNEL_CSUM;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002556
Preethi Banalabacd75c2017-03-27 14:43:18 -07002557 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2558 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2559 netdev->hw_enc_features |= hw_enc_features;
2560 }
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002561 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002562 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002563
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002564 /* Write features and hw_features separately to avoid polluting
Preethi Banalabacd75c2017-03-27 14:43:18 -07002565 * with, or dropping, features that are set when we registered.
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002566 */
Preethi Banalabacd75c2017-03-27 14:43:18 -07002567 hw_features = hw_enc_features;
Mitch Williamsba6cc7f2016-04-01 13:34:31 -07002568
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002569 /* Enable VLAN features if supported */
2570 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2571 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
2572 NETIF_F_HW_VLAN_CTAG_RX);
2573
Preethi Banalabacd75c2017-03-27 14:43:18 -07002574 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07002575
Jacob Keller0a3b4f72017-08-29 05:32:41 -04002576 netdev->features |= hw_features;
2577
2578 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2579 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002580
2581 adapter->vsi.id = adapter->vsi_res->vsi_id;
2582
2583 adapter->vsi.back = adapter;
2584 adapter->vsi.base_vector = 1;
2585 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002586 vsi->netdev = adapter->netdev;
2587 vsi->qs_handle = adapter->vsi_res->qset_handle;
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002588 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002589 adapter->rss_key_size = vfres->rss_key_size;
2590 adapter->rss_lut_size = vfres->rss_lut_size;
2591 } else {
2592 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2593 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2594 }
2595
Mitch Williamse6d038d2015-06-04 16:23:58 -04002596 return 0;
2597}
2598
2599/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002600 * i40evf_init_task - worker thread to perform delayed initialization
2601 * @work: pointer to work_struct containing our data
2602 *
2603 * This task completes the work that was begun in probe. Due to the nature
2604 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002605 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002606 * whole system, we'll do it in a task so we can sleep.
2607 * This task only runs during driver init. Once we've established
2608 * communications with the PF driver and set up our netdev, the watchdog
2609 * takes over.
2610 **/
2611static void i40evf_init_task(struct work_struct *work)
2612{
2613 struct i40evf_adapter *adapter = container_of(work,
2614 struct i40evf_adapter,
2615 init_task.work);
2616 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002617 struct i40e_hw *hw = &adapter->hw;
2618 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002619 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002620
2621 switch (adapter->state) {
2622 case __I40EVF_STARTUP:
2623 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002624 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2625 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002626 err = i40e_set_mac_type(hw);
2627 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002628 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2629 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002630 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002631 }
2632 err = i40evf_check_reset_complete(hw);
2633 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002634 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002635 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002636 goto err;
2637 }
2638 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2639 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2640 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2641 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2642
2643 err = i40evf_init_adminq(hw);
2644 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002645 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2646 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002647 goto err;
2648 }
2649 err = i40evf_send_api_ver(adapter);
2650 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002651 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002652 i40evf_shutdown_adminq(hw);
2653 goto err;
2654 }
2655 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2656 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002657 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002658 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002659 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002660 i40evf_shutdown_adminq(hw);
2661 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002662 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002663 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002664
2665 /* aq msg sent, awaiting reply */
2666 err = i40evf_verify_api_ver(adapter);
2667 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002668 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002669 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002670 else
2671 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2672 adapter->pf_version.major,
2673 adapter->pf_version.minor,
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002674 VIRTCHNL_VERSION_MAJOR,
2675 VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002676 goto err;
2677 }
2678 err = i40evf_send_vf_config_msg(adapter);
2679 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002680 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002681 err);
2682 goto err;
2683 }
2684 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2685 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002686 case __I40EVF_INIT_GET_RESOURCES:
2687 /* aq msg sent, awaiting reply */
2688 if (!adapter->vf_res) {
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002689 bufsz = sizeof(struct virtchnl_vf_resource) +
Greg Rose5eae00c2013-12-21 06:12:45 +00002690 (I40E_MAX_VF_VSI *
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002691 sizeof(struct virtchnl_vsi_resource));
Greg Rose5eae00c2013-12-21 06:12:45 +00002692 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002693 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002694 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002695 }
2696 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002697 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002698 err = i40evf_send_vf_config_msg(adapter);
2699 goto err;
Mitch Williamse7430722015-10-26 19:44:38 -04002700 } else if (err == I40E_ERR_PARAM) {
2701 /* We only get ERR_PARAM if the device is in a very bad
2702 * state or if we've been disabled for previous bad
2703 * behavior. Either way, we're done now.
2704 */
2705 i40evf_shutdown_adminq(hw);
2706 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2707 return;
Mitch Williams906a6932014-11-13 03:06:12 +00002708 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002709 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002710 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2711 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002712 goto err_alloc;
2713 }
2714 adapter->state = __I40EVF_INIT_SW;
2715 break;
2716 default:
2717 goto err_alloc;
2718 }
Alexander Duyckf608e6a2016-01-24 21:17:57 -08002719
Mitch Williamse6d038d2015-06-04 16:23:58 -04002720 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002721 goto err_alloc;
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002722 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002723
2724 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2725
Greg Rose5eae00c2013-12-21 06:12:45 +00002726 netdev->netdev_ops = &i40evf_netdev_ops;
2727 i40evf_set_ethtool_ops(netdev);
2728 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002729
Jarod Wilson91c527a2016-10-17 15:54:05 -04002730 /* MTU range: 68 - 9710 */
2731 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -04002732 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -04002733
Greg Rose5eae00c2013-12-21 06:12:45 +00002734 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002735 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002736 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002737 eth_hw_addr_random(netdev);
2738 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2739 } else {
2740 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2741 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2742 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002743 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002744
Kees Cook26566ea2017-10-16 17:29:35 -07002745 timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
Greg Rose5eae00c2013-12-21 06:12:45 +00002746 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2747
Mitch Williamsd732a182014-04-24 06:41:37 +00002748 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2749 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002750 err = i40evf_init_interrupt_scheme(adapter);
2751 if (err)
2752 goto err_sw_init;
2753 i40evf_map_rings_to_vectors(adapter);
Stefan Assmannfbb113f2017-06-29 15:12:24 +02002754 if (adapter->vf_res->vf_cap_flags &
Jesse Brandeburg310a2ad2017-05-11 11:23:11 -07002755 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -08002756 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2757
Greg Rose5eae00c2013-12-21 06:12:45 +00002758 err = i40evf_request_misc_irq(adapter);
2759 if (err)
2760 goto err_sw_init;
2761
2762 netif_carrier_off(netdev);
Sridhar Samudrala3f341ac2016-09-01 22:27:27 +02002763 adapter->link_up = false;
Greg Rose5eae00c2013-12-21 06:12:45 +00002764
Mitch Williamsef8693e2014-02-13 03:48:53 -08002765 if (!adapter->netdev_registered) {
2766 err = register_netdev(netdev);
2767 if (err)
2768 goto err_register;
2769 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002770
2771 adapter->netdev_registered = true;
2772
2773 netif_tx_stop_all_queues(netdev);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002774 if (CLIENT_ALLOWED(adapter)) {
2775 err = i40evf_lan_add_device(adapter);
2776 if (err)
2777 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2778 err);
2779 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002780
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002781 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002782 if (netdev->features & NETIF_F_GRO)
2783 dev_info(&pdev->dev, "GRO is enabled\n");
2784
Greg Rose5eae00c2013-12-21 06:12:45 +00002785 adapter->state = __I40EVF_DOWN;
Jacob Keller0da36b92017-04-19 09:25:55 -04002786 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
Greg Rose5eae00c2013-12-21 06:12:45 +00002787 i40evf_misc_irq_enable(adapter);
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002788 wake_up(&adapter->down_waitqueue);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002789
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002790 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2791 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2792 if (!adapter->rss_key || !adapter->rss_lut)
2793 goto err_mem;
2794
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002795 if (RSS_AQ(adapter)) {
2796 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2797 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2798 } else {
Helin Zhang96a81982015-10-26 19:44:31 -04002799 i40evf_init_rss(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002800 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002801 return;
2802restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002803 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002804 return;
Mitch Williams43a3d9b2016-04-12 08:30:44 -07002805err_mem:
2806 i40evf_free_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002807err_register:
2808 i40evf_free_misc_irq(adapter);
2809err_sw_init:
2810 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002811err_alloc:
2812 kfree(adapter->vf_res);
2813 adapter->vf_res = NULL;
2814err:
2815 /* Things went into the weeds, so try again later */
2816 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002817 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002818 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002819 i40evf_shutdown_adminq(hw);
2820 adapter->state = __I40EVF_STARTUP;
2821 schedule_delayed_work(&adapter->init_task, HZ * 5);
2822 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002823 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002824 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002825}
2826
2827/**
2828 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2829 * @pdev: pci device structure
2830 **/
2831static void i40evf_shutdown(struct pci_dev *pdev)
2832{
2833 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002834 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002835
2836 netif_device_detach(netdev);
2837
2838 if (netif_running(netdev))
2839 i40evf_close(netdev);
2840
Mitch Williams00293fd2015-01-09 11:18:18 +00002841 /* Prevent the watchdog from running. */
2842 adapter->state = __I40EVF_REMOVE;
2843 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002844
Greg Rose5eae00c2013-12-21 06:12:45 +00002845#ifdef CONFIG_PM
2846 pci_save_state(pdev);
2847
2848#endif
2849 pci_disable_device(pdev);
2850}
2851
2852/**
2853 * i40evf_probe - Device Initialization Routine
2854 * @pdev: PCI device information struct
2855 * @ent: entry in i40evf_pci_tbl
2856 *
2857 * Returns 0 on success, negative on failure
2858 *
2859 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2860 * The OS initialization, configuring of the adapter private structure,
2861 * and a hardware reset occur.
2862 **/
2863static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2864{
2865 struct net_device *netdev;
2866 struct i40evf_adapter *adapter = NULL;
2867 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002868 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002869
2870 err = pci_enable_device(pdev);
2871 if (err)
2872 return err;
2873
Mitch Williams64942942014-02-11 08:26:33 +00002874 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002875 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002876 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2877 if (err) {
2878 dev_err(&pdev->dev,
2879 "DMA configuration failed: 0x%x\n", err);
2880 goto err_dma;
2881 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002882 }
2883
2884 err = pci_request_regions(pdev, i40evf_driver_name);
2885 if (err) {
2886 dev_err(&pdev->dev,
2887 "pci_request_regions failed 0x%x\n", err);
2888 goto err_pci_reg;
2889 }
2890
2891 pci_enable_pcie_error_reporting(pdev);
2892
2893 pci_set_master(pdev);
2894
Mitch Williams1255b7a2015-11-06 15:25:59 -08002895 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
Greg Rose5eae00c2013-12-21 06:12:45 +00002896 if (!netdev) {
2897 err = -ENOMEM;
2898 goto err_alloc_etherdev;
2899 }
2900
2901 SET_NETDEV_DEV(netdev, &pdev->dev);
2902
2903 pci_set_drvdata(pdev, netdev);
2904 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002905
2906 adapter->netdev = netdev;
2907 adapter->pdev = pdev;
2908
2909 hw = &adapter->hw;
2910 hw->back = adapter;
2911
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002912 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002913 adapter->state = __I40EVF_STARTUP;
2914
2915 /* Call save state here because it relies on the adapter struct. */
2916 pci_save_state(pdev);
2917
2918 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2919 pci_resource_len(pdev, 0));
2920 if (!hw->hw_addr) {
2921 err = -EIO;
2922 goto err_ioremap;
2923 }
2924 hw->vendor_id = pdev->vendor;
2925 hw->device_id = pdev->device;
2926 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2927 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2928 hw->subsystem_device_id = pdev->subsystem_device;
2929 hw->bus.device = PCI_SLOT(pdev->devfn);
2930 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -08002931 hw->bus.bus_id = pdev->bus->number;
Greg Rose5eae00c2013-12-21 06:12:45 +00002932
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002933 /* set up the locks for the AQ, do this only once in probe
2934 * and destroy them only once in remove
2935 */
2936 mutex_init(&hw->aq.asq_mutex);
2937 mutex_init(&hw->aq.arq_mutex);
2938
Jacob Keller504398f2017-10-27 11:06:50 -04002939 spin_lock_init(&adapter->mac_vlan_list_lock);
2940
Serey Kong8bb1a542014-08-01 13:27:15 -07002941 INIT_LIST_HEAD(&adapter->mac_filter_list);
2942 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2943
Greg Rose5eae00c2013-12-21 06:12:45 +00002944 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2945 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2946 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08002947 INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002948 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002949 schedule_delayed_work(&adapter->init_task,
2950 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002951
Sudheer Mogilappagarife2647a2017-06-23 04:24:44 -04002952 /* Setup the wait queue for indicating transition to down status */
2953 init_waitqueue_head(&adapter->down_waitqueue);
2954
Greg Rose5eae00c2013-12-21 06:12:45 +00002955 return 0;
2956
2957err_ioremap:
2958 free_netdev(netdev);
2959err_alloc_etherdev:
2960 pci_release_regions(pdev);
2961err_pci_reg:
2962err_dma:
2963 pci_disable_device(pdev);
2964 return err;
2965}
2966
2967#ifdef CONFIG_PM
2968/**
2969 * i40evf_suspend - Power management suspend routine
2970 * @pdev: PCI device information struct
2971 * @state: unused
2972 *
2973 * Called when the system (VM) is entering sleep/suspend.
2974 **/
2975static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2976{
2977 struct net_device *netdev = pci_get_drvdata(pdev);
2978 struct i40evf_adapter *adapter = netdev_priv(netdev);
2979 int retval = 0;
2980
2981 netif_device_detach(netdev);
2982
Jacob Keller9b2aef12017-10-27 11:06:52 -04002983 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2984 &adapter->crit_section))
2985 usleep_range(500, 1000);
2986
Greg Rose5eae00c2013-12-21 06:12:45 +00002987 if (netif_running(netdev)) {
2988 rtnl_lock();
2989 i40evf_down(adapter);
2990 rtnl_unlock();
2991 }
2992 i40evf_free_misc_irq(adapter);
2993 i40evf_reset_interrupt_capability(adapter);
2994
Jacob Keller9b2aef12017-10-27 11:06:52 -04002995 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2996
Greg Rose5eae00c2013-12-21 06:12:45 +00002997 retval = pci_save_state(pdev);
2998 if (retval)
2999 return retval;
3000
3001 pci_disable_device(pdev);
3002
3003 return 0;
3004}
3005
3006/**
Joe Perchesdbedd442015-03-06 20:49:12 -08003007 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00003008 * @pdev: PCI device information struct
3009 *
3010 * Called when the system (VM) is resumed from sleep/suspend.
3011 **/
3012static int i40evf_resume(struct pci_dev *pdev)
3013{
3014 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3015 struct net_device *netdev = adapter->netdev;
3016 u32 err;
3017
3018 pci_set_power_state(pdev, PCI_D0);
3019 pci_restore_state(pdev);
3020 /* pci_restore_state clears dev->state_saved so call
3021 * pci_save_state to restore it.
3022 */
3023 pci_save_state(pdev);
3024
3025 err = pci_enable_device_mem(pdev);
3026 if (err) {
3027 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3028 return err;
3029 }
3030 pci_set_master(pdev);
3031
3032 rtnl_lock();
3033 err = i40evf_set_interrupt_capability(adapter);
3034 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03003035 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00003036 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3037 return err;
3038 }
3039 err = i40evf_request_misc_irq(adapter);
3040 rtnl_unlock();
3041 if (err) {
3042 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3043 return err;
3044 }
3045
3046 schedule_work(&adapter->reset_task);
3047
3048 netif_device_attach(netdev);
3049
3050 return err;
3051}
3052
3053#endif /* CONFIG_PM */
3054/**
3055 * i40evf_remove - Device Removal Routine
3056 * @pdev: PCI device information struct
3057 *
3058 * i40evf_remove is called by the PCI subsystem to alert the driver
3059 * that it should release a PCI device. The could be caused by a
3060 * Hot-Plug event, or because the driver is going to be removed from
3061 * memory.
3062 **/
3063static void i40evf_remove(struct pci_dev *pdev)
3064{
3065 struct net_device *netdev = pci_get_drvdata(pdev);
3066 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003067 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00003068 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsed0e8942017-01-24 10:23:59 -08003069 int err;
Avinash Dayanand06aa0402017-12-18 05:16:43 -05003070 /* Indicate we are in remove and not to run reset_task */
3071 set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00003072 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08003073 cancel_work_sync(&adapter->reset_task);
Mitch Williamsed0e8942017-01-24 10:23:59 -08003074 cancel_delayed_work_sync(&adapter->client_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00003075 if (adapter->netdev_registered) {
3076 unregister_netdev(netdev);
3077 adapter->netdev_registered = false;
3078 }
Mitch Williamsed0e8942017-01-24 10:23:59 -08003079 if (CLIENT_ALLOWED(adapter)) {
3080 err = i40evf_lan_del_device(adapter);
3081 if (err)
3082 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3083 err);
3084 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00003085
Mitch Williamsf4a71882015-01-09 11:18:16 +00003086 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00003087 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003088 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00003089 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003090 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003091 /* If the FW isn't responding, kick it once, but only once. */
3092 if (!i40evf_asq_done(hw)) {
3093 i40evf_request_reset(adapter);
Mitch Williams22ead372016-03-18 12:18:10 -07003094 msleep(50);
Mitch Williamsf4a71882015-01-09 11:18:16 +00003095 }
Mitch Williams8a68bad2016-12-12 15:44:10 -08003096 i40evf_free_all_tx_resources(adapter);
3097 i40evf_free_all_rx_resources(adapter);
Jacob Kelleref4603e2016-11-08 13:05:08 -08003098 i40evf_misc_irq_disable(adapter);
3099 i40evf_free_misc_irq(adapter);
3100 i40evf_reset_interrupt_capability(adapter);
3101 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003102
Mitch Williamse5d17c32014-06-04 04:22:38 +00003103 if (adapter->watchdog_timer.function)
3104 del_timer_sync(&adapter->watchdog_timer);
3105
Mitch Williams43a3d9b2016-04-12 08:30:44 -07003106 i40evf_free_rss(adapter);
Helin Zhang66f9af852015-10-26 19:44:34 -04003107
Greg Rose5eae00c2013-12-21 06:12:45 +00003108 if (hw->aq.asq.count)
3109 i40evf_shutdown_adminq(hw);
3110
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08003111 /* destroy the locks only once, here */
3112 mutex_destroy(&hw->aq.arq_mutex);
3113 mutex_destroy(&hw->aq.asq_mutex);
3114
Greg Rose5eae00c2013-12-21 06:12:45 +00003115 iounmap(hw->hw_addr);
3116 pci_release_regions(pdev);
Mitch Williamse284fc82015-03-27 00:12:09 -07003117 i40evf_free_all_tx_resources(adapter);
3118 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00003119 i40evf_free_queues(adapter);
3120 kfree(adapter->vf_res);
Jacob Keller504398f2017-10-27 11:06:50 -04003121 spin_lock_bh(&adapter->mac_vlan_list_lock);
Mitch Williams6ba36a22014-08-01 13:27:14 -07003122 /* If we got removed before an up/down sequence, we've got a filter
3123 * hanging out there that we need to get rid of.
3124 */
3125 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3126 list_del(&f->list);
3127 kfree(f);
3128 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00003129 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3130 list_del(&f->list);
3131 kfree(f);
3132 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003133
Jacob Keller504398f2017-10-27 11:06:50 -04003134 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3135
Greg Rose5eae00c2013-12-21 06:12:45 +00003136 free_netdev(netdev);
3137
3138 pci_disable_pcie_error_reporting(pdev);
3139
3140 pci_disable_device(pdev);
3141}
3142
3143static struct pci_driver i40evf_driver = {
3144 .name = i40evf_driver_name,
3145 .id_table = i40evf_pci_tbl,
3146 .probe = i40evf_probe,
3147 .remove = i40evf_remove,
3148#ifdef CONFIG_PM
3149 .suspend = i40evf_suspend,
3150 .resume = i40evf_resume,
3151#endif
3152 .shutdown = i40evf_shutdown,
3153};
3154
3155/**
3156 * i40e_init_module - Driver Registration Routine
3157 *
3158 * i40e_init_module is the first routine called when the driver is
3159 * loaded. All it does is register with the PCI subsystem.
3160 **/
3161static int __init i40evf_init_module(void)
3162{
3163 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00003164
Greg Rose5eae00c2013-12-21 06:12:45 +00003165 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00003166 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00003167
3168 pr_info("%s\n", i40evf_copyright);
3169
Jacob Keller6992a6c2016-08-04 11:37:01 -07003170 i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3171 i40evf_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003172 if (!i40evf_wq) {
3173 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3174 return -ENOMEM;
3175 }
Greg Rose5eae00c2013-12-21 06:12:45 +00003176 ret = pci_register_driver(&i40evf_driver);
3177 return ret;
3178}
3179
3180module_init(i40evf_init_module);
3181
3182/**
3183 * i40e_exit_module - Driver Exit Cleanup Routine
3184 *
3185 * i40e_exit_module is called just before the driver is removed
3186 * from memory.
3187 **/
3188static void __exit i40evf_exit_module(void)
3189{
3190 pci_unregister_driver(&i40evf_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -08003191 destroy_workqueue(i40evf_wq);
Greg Rose5eae00c2013-12-21 06:12:45 +00003192}
3193
3194module_exit(i40evf_exit_module);
3195
3196/* i40evf_main.c */