blob: 99d2cffae0cd8c2b4c583f4453aa3c09cc58f3eb [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Sravanthi Tangeda5b8eb172015-02-06 08:52:21 +00004 * Copyright(c) 2013 - 2015 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"
29static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
30static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
31static int i40evf_close(struct net_device *netdev);
32
33char i40evf_driver_name[] = "i40evf";
34static const char i40evf_driver_string[] =
Mitch Williams0af56f42014-06-04 20:42:10 +000035 "Intel(R) XL710/X710 Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000036
Catherine Sullivan0e320512015-10-01 14:37:41 -040037#define DRV_VERSION "1.3.33"
Greg Rose5eae00c2013-12-21 06:12:45 +000038const char i40evf_driver_version[] = DRV_VERSION;
39static const char i40evf_copyright[] =
Catherine Sullivanbf418462015-07-10 19:36:10 -040040 "Copyright (c) 2013 - 2015 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000041
42/* i40evf_pci_tbl - PCI Device ID Table
43 *
44 * Wildcard entries (PCI_ANY_ID) should come last
45 * Last entry must be all 0s
46 *
47 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
48 * Class, Class Mask, private data (not used) }
49 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020050static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080051 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040052 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000053 /* required last entry */
54 {0, }
55};
56
57MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
58
59MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
60MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
61MODULE_LICENSE("GPL");
62MODULE_VERSION(DRV_VERSION);
63
64/**
65 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
66 * @hw: pointer to the HW structure
67 * @mem: ptr to mem struct to fill out
68 * @size: size of memory requested
69 * @alignment: what to align the allocation to
70 **/
71i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
72 struct i40e_dma_mem *mem,
73 u64 size, u32 alignment)
74{
75 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
76
77 if (!mem)
78 return I40E_ERR_PARAM;
79
80 mem->size = ALIGN(size, alignment);
81 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
82 (dma_addr_t *)&mem->pa, GFP_KERNEL);
83 if (mem->va)
84 return 0;
85 else
86 return I40E_ERR_NO_MEMORY;
87}
88
89/**
90 * i40evf_free_dma_mem_d - OS specific memory free for shared code
91 * @hw: pointer to the HW structure
92 * @mem: ptr to mem struct to free
93 **/
94i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
95{
96 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
97
98 if (!mem || !mem->va)
99 return I40E_ERR_PARAM;
100 dma_free_coherent(&adapter->pdev->dev, mem->size,
101 mem->va, (dma_addr_t)mem->pa);
102 return 0;
103}
104
105/**
106 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
107 * @hw: pointer to the HW structure
108 * @mem: ptr to mem struct to fill out
109 * @size: size of memory requested
110 **/
111i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
112 struct i40e_virt_mem *mem, u32 size)
113{
114 if (!mem)
115 return I40E_ERR_PARAM;
116
117 mem->size = size;
118 mem->va = kzalloc(size, GFP_KERNEL);
119
120 if (mem->va)
121 return 0;
122 else
123 return I40E_ERR_NO_MEMORY;
124}
125
126/**
127 * i40evf_free_virt_mem_d - OS specific memory free for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to free
130 **/
131i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem)
133{
134 if (!mem)
135 return I40E_ERR_PARAM;
136
137 /* it's ok to kfree a NULL pointer */
138 kfree(mem->va);
139
140 return 0;
141}
142
143/**
144 * i40evf_debug_d - OS dependent version of debug printing
145 * @hw: pointer to the HW structure
146 * @mask: debug level mask
147 * @fmt_str: printf-type format description
148 **/
149void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
150{
151 char buf[512];
152 va_list argptr;
153
154 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
155 return;
156
157 va_start(argptr, fmt_str);
158 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
159 va_end(argptr);
160
161 /* the debug string is already formatted with a newline */
162 pr_info("%s", buf);
163}
164
165/**
166 * i40evf_tx_timeout - Respond to a Tx Hang
167 * @netdev: network interface device structure
168 **/
169static void i40evf_tx_timeout(struct net_device *netdev)
170{
171 struct i40evf_adapter *adapter = netdev_priv(netdev);
172
173 adapter->tx_timeout_count++;
Mitch Williams67c818a2015-06-19 08:56:30 -0700174 if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
175 I40EVF_FLAG_RESET_NEEDED))) {
Mitch Williams3526d802014-03-06 08:59:56 +0000176 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
Mitch Williams625777e2014-02-20 19:29:05 -0800177 schedule_work(&adapter->reset_task);
178 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000179}
180
181/**
182 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
183 * @adapter: board private structure
184 **/
185static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
186{
187 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000188
Greg Rose5eae00c2013-12-21 06:12:45 +0000189 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
190
191 /* read flush */
192 rd32(hw, I40E_VFGEN_RSTAT);
193
194 synchronize_irq(adapter->msix_entries[0].vector);
195}
196
197/**
198 * i40evf_misc_irq_enable - Enable default interrupt generation settings
199 * @adapter: board private structure
200 **/
201static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
202{
203 struct i40e_hw *hw = &adapter->hw;
Mitch Williams75a64432014-11-11 20:02:42 +0000204
Greg Rose5eae00c2013-12-21 06:12:45 +0000205 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
206 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400207 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000208
209 /* read flush */
210 rd32(hw, I40E_VFGEN_RSTAT);
211}
212
213/**
214 * i40evf_irq_disable - Mask off interrupt generation on the NIC
215 * @adapter: board private structure
216 **/
217static void i40evf_irq_disable(struct i40evf_adapter *adapter)
218{
219 int i;
220 struct i40e_hw *hw = &adapter->hw;
221
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800222 if (!adapter->msix_entries)
223 return;
224
Greg Rose5eae00c2013-12-21 06:12:45 +0000225 for (i = 1; i < adapter->num_msix_vectors; i++) {
226 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
227 synchronize_irq(adapter->msix_entries[i].vector);
228 }
229 /* read flush */
230 rd32(hw, I40E_VFGEN_RSTAT);
Greg Rose5eae00c2013-12-21 06:12:45 +0000231}
232
233/**
234 * i40evf_irq_enable_queues - Enable interrupt for specified queues
235 * @adapter: board private structure
236 * @mask: bitmap of queues to enable
237 **/
238void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
239{
240 struct i40e_hw *hw = &adapter->hw;
241 int i;
242
243 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400244 if (mask & BIT(i - 1)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000247 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400248 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
Greg Rose5eae00c2013-12-21 06:12:45 +0000249 }
250 }
251}
252
253/**
254 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
255 * @adapter: board private structure
256 * @mask: bitmap of vectors to trigger
257 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000258static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000259{
260 struct i40e_hw *hw = &adapter->hw;
261 int i;
262 uint32_t dyn_ctl;
263
Mitch Williams164ec1b2014-06-04 08:45:19 +0000264 if (mask & 1) {
265 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400266 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000267 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400268 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Mitch Williams164ec1b2014-06-04 08:45:19 +0000269 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
270 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000271 for (i = 1; i < adapter->num_msix_vectors; i++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400272 if (mask & BIT(i)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000273 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400274 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
Jesse Brandeburg97bf75f2015-02-27 09:18:32 +0000275 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400276 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
278 }
279 }
280}
281
282/**
283 * i40evf_irq_enable - Enable default interrupt generation settings
284 * @adapter: board private structure
Jean Sacren69c1d702015-10-13 01:06:27 -0600285 * @flush: boolean value whether to run rd32()
Greg Rose5eae00c2013-12-21 06:12:45 +0000286 **/
287void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
288{
289 struct i40e_hw *hw = &adapter->hw;
290
Mitch Williams164ec1b2014-06-04 08:45:19 +0000291 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000292 i40evf_irq_enable_queues(adapter, ~0);
293
294 if (flush)
295 rd32(hw, I40E_VFGEN_RSTAT);
296}
297
298/**
299 * i40evf_msix_aq - Interrupt handler for vector 0
300 * @irq: interrupt number
301 * @data: pointer to netdev
302 **/
303static irqreturn_t i40evf_msix_aq(int irq, void *data)
304{
305 struct net_device *netdev = data;
306 struct i40evf_adapter *adapter = netdev_priv(netdev);
307 struct i40e_hw *hw = &adapter->hw;
308 u32 val;
Greg Rose5eae00c2013-12-21 06:12:45 +0000309
310 /* handle non-queue interrupts */
Jean Sacrened17f7e2015-10-13 01:06:30 -0600311 rd32(hw, I40E_VFINT_ICR01);
312 rd32(hw, I40E_VFINT_ICR0_ENA1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000313
314
Jean Sacrened17f7e2015-10-13 01:06:30 -0600315 val = rd32(hw, I40E_VFINT_DYN_CTL01) |
316 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
Greg Rose5eae00c2013-12-21 06:12:45 +0000317 wr32(hw, I40E_VFINT_DYN_CTL01, val);
318
Greg Rose5eae00c2013-12-21 06:12:45 +0000319 /* schedule work on the private workqueue */
320 schedule_work(&adapter->adminq_task);
321
322 return IRQ_HANDLED;
323}
324
325/**
326 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
327 * @irq: interrupt number
328 * @data: pointer to a q_vector
329 **/
330static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
331{
332 struct i40e_q_vector *q_vector = data;
333
334 if (!q_vector->tx.ring && !q_vector->rx.ring)
335 return IRQ_HANDLED;
336
Alexander Duyck5d3465a2015-09-29 15:19:50 -0700337 napi_schedule_irqoff(&q_vector->napi);
Greg Rose5eae00c2013-12-21 06:12:45 +0000338
339 return IRQ_HANDLED;
340}
341
342/**
343 * i40evf_map_vector_to_rxq - associate irqs with rx queues
344 * @adapter: board private structure
345 * @v_idx: interrupt number
346 * @r_idx: queue number
347 **/
348static void
349i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
350{
351 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
352 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
353
354 rx_ring->q_vector = q_vector;
355 rx_ring->next = q_vector->rx.ring;
356 rx_ring->vsi = &adapter->vsi;
357 q_vector->rx.ring = rx_ring;
358 q_vector->rx.count++;
359 q_vector->rx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400360 q_vector->itr_countdown = ITR_COUNTDOWN_START;
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{
372 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
373 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
374
375 tx_ring->q_vector = q_vector;
376 tx_ring->next = q_vector->tx.ring;
377 tx_ring->vsi = &adapter->vsi;
378 q_vector->tx.ring = tx_ring;
379 q_vector->tx.count++;
380 q_vector->tx.latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400381 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Greg Rose5eae00c2013-12-21 06:12:45 +0000382 q_vector->num_ringpairs++;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400383 q_vector->ring_mask |= BIT(t_idx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000384}
385
386/**
387 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
388 * @adapter: board private structure to initialize
389 *
390 * This function maps descriptor rings to the queue-specific vectors
391 * we were allotted through the MSI-X enabling code. Ideally, we'd have
392 * one vector per ring/queue, but on a constrained vector budget, we
393 * group the rings as "efficiently" as possible. You would add new
394 * mapping configurations in here.
395 **/
396static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
397{
398 int q_vectors;
399 int v_start = 0;
400 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000401 int rxr_remaining = adapter->num_active_queues;
402 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000403 int i, j;
404 int rqpv, tqpv;
405 int err = 0;
406
407 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
408
409 /* The ideal configuration...
410 * We have enough vectors to map one per queue.
411 */
Mitch Williams973371d2015-04-27 14:57:09 -0400412 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000413 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
414 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
415
416 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
417 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
418 goto out;
419 }
420
421 /* If we don't have enough vectors for a 1-to-1
422 * mapping, we'll have to group them so there are
423 * multiple queues per vector.
424 * Re-adjusting *qpv takes care of the remainder.
425 */
426 for (i = v_start; i < q_vectors; i++) {
427 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
428 for (j = 0; j < rqpv; j++) {
429 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
430 rxr_idx++;
431 rxr_remaining--;
432 }
433 }
434 for (i = v_start; i < q_vectors; i++) {
435 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
436 for (j = 0; j < tqpv; j++) {
437 i40evf_map_vector_to_txq(adapter, i, txr_idx);
438 txr_idx++;
439 txr_remaining--;
440 }
441 }
442
443out:
444 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
445
446 return err;
447}
448
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700449#ifdef CONFIG_NET_POLL_CONTROLLER
450/**
451 * i40evf_netpoll - A Polling 'interrupt' handler
452 * @netdev: network interface device structure
453 *
454 * This is used by netconsole to send skbs without having to re-enable
455 * interrupts. It's not called while the normal interrupt routine is executing.
456 **/
457static void i40evf_netpoll(struct net_device *netdev)
458{
459 struct i40evf_adapter *adapter = netdev_priv(netdev);
460 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
461 int i;
462
463 /* if interface is down do nothing */
464 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
465 return;
466
467 for (i = 0; i < q_vectors; i++)
468 i40evf_msix_clean_rings(0, adapter->q_vector[i]);
469}
470
471#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000472/**
473 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
474 * @adapter: board private structure
475 *
476 * Allocates MSI-X vectors for tx and rx handling, and requests
477 * interrupts from the kernel.
478 **/
479static int
480i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
481{
482 int vector, err, q_vectors;
483 int rx_int_idx = 0, tx_int_idx = 0;
484
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++) {
490 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
491
492 if (q_vector->tx.ring && q_vector->rx.ring) {
493 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
494 "i40evf-%s-%s-%d", basename,
495 "TxRx", rx_int_idx++);
496 tx_int_idx++;
497 } else if (q_vector->rx.ring) {
498 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
499 "i40evf-%s-%s-%d", basename,
500 "rx", rx_int_idx++);
501 } else if (q_vector->tx.ring) {
502 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
503 "i40evf-%s-%s-%d", basename,
504 "tx", tx_int_idx++);
505 } else {
506 /* skip this unused q_vector */
507 continue;
508 }
509 err = request_irq(
510 adapter->msix_entries[vector + NONQ_VECS].vector,
511 i40evf_msix_clean_rings,
512 0,
513 q_vector->name,
514 q_vector);
515 if (err) {
516 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400517 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000518 goto free_queue_irqs;
519 }
520 /* assign the mask for this irq */
521 irq_set_affinity_hint(
522 adapter->msix_entries[vector + NONQ_VECS].vector,
523 q_vector->affinity_mask);
524 }
525
526 return 0;
527
528free_queue_irqs:
529 while (vector) {
530 vector--;
531 irq_set_affinity_hint(
532 adapter->msix_entries[vector + NONQ_VECS].vector,
533 NULL);
534 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
535 adapter->q_vector[vector]);
536 }
537 return err;
538}
539
540/**
541 * i40evf_request_misc_irq - Initialize MSI-X interrupts
542 * @adapter: board private structure
543 *
544 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
545 * vector is only for the admin queue, and stays active even when the netdev
546 * is closed.
547 **/
548static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
549{
550 struct net_device *netdev = adapter->netdev;
551 int err;
552
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700553 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000554 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
555 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000556 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800557 &i40evf_msix_aq, 0,
558 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000559 if (err) {
560 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800561 "request_irq for %s failed: %d\n",
562 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000563 free_irq(adapter->msix_entries[0].vector, netdev);
564 }
565 return err;
566}
567
568/**
569 * i40evf_free_traffic_irqs - Free MSI-X interrupts
570 * @adapter: board private structure
571 *
572 * Frees all MSI-X vectors other than 0.
573 **/
574static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
575{
576 int i;
577 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000578
Greg Rose5eae00c2013-12-21 06:12:45 +0000579 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
580
581 for (i = 0; i < q_vectors; i++) {
582 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
583 NULL);
584 free_irq(adapter->msix_entries[i+1].vector,
585 adapter->q_vector[i]);
586 }
587}
588
589/**
590 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
591 * @adapter: board private structure
592 *
593 * Frees MSI-X vector 0.
594 **/
595static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
596{
597 struct net_device *netdev = adapter->netdev;
598
599 free_irq(adapter->msix_entries[0].vector, netdev);
600}
601
602/**
603 * i40evf_configure_tx - Configure Transmit Unit after Reset
604 * @adapter: board private structure
605 *
606 * Configure the Tx unit of the MAC after a reset.
607 **/
608static void i40evf_configure_tx(struct i40evf_adapter *adapter)
609{
610 struct i40e_hw *hw = &adapter->hw;
611 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000612
Mitch Williamscc052922014-10-25 03:24:34 +0000613 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000614 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
615}
616
617/**
618 * i40evf_configure_rx - Configure Receive Unit after Reset
619 * @adapter: board private structure
620 *
621 * Configure the Rx unit of the MAC after a reset.
622 **/
623static void i40evf_configure_rx(struct i40evf_adapter *adapter)
624{
625 struct i40e_hw *hw = &adapter->hw;
626 struct net_device *netdev = adapter->netdev;
627 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
628 int i;
629 int rx_buf_len;
630
631
632 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
633 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
634
635 /* Decide whether to use packet split mode or not */
636 if (netdev->mtu > ETH_DATA_LEN) {
637 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
638 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
639 else
640 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
641 } else {
642 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
643 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
644 else
645 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
646 }
647
648 /* Set the RX buffer length according to the mode */
649 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
650 rx_buf_len = I40E_RX_HDR_SIZE;
651 } else {
652 if (netdev->mtu <= ETH_DATA_LEN)
653 rx_buf_len = I40EVF_RXBUFFER_2048;
654 else
655 rx_buf_len = ALIGN(max_frame, 1024);
656 }
657
Mitch Williamscc052922014-10-25 03:24:34 +0000658 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000659 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
660 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
661 }
662}
663
664/**
665 * i40evf_find_vlan - Search filter list for specific vlan filter
666 * @adapter: board private structure
667 * @vlan: vlan tag
668 *
669 * Returns ptr to the filter object or NULL
670 **/
671static struct
672i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
673{
674 struct i40evf_vlan_filter *f;
675
676 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
677 if (vlan == f->vlan)
678 return f;
679 }
680 return NULL;
681}
682
683/**
684 * i40evf_add_vlan - Add a vlan filter to the list
685 * @adapter: board private structure
686 * @vlan: VLAN tag
687 *
688 * Returns ptr to the filter object or NULL when no memory available.
689 **/
690static struct
691i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
692{
Mitch Williams13acb542015-03-31 00:45:05 -0700693 struct i40evf_vlan_filter *f = NULL;
694 int count = 50;
695
696 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
697 &adapter->crit_section)) {
698 udelay(1);
699 if (--count == 0)
700 goto out;
701 }
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:
718 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
719out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000720 return f;
721}
722
723/**
724 * i40evf_del_vlan - Remove a vlan filter from the list
725 * @adapter: board private structure
726 * @vlan: VLAN tag
727 **/
728static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
729{
730 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700731 int count = 50;
732
733 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
734 &adapter->crit_section)) {
735 udelay(1);
736 if (--count == 0)
737 return;
738 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000739
740 f = i40evf_find_vlan(adapter, vlan);
741 if (f) {
742 f->remove = true;
743 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
744 }
Mitch Williams13acb542015-03-31 00:45:05 -0700745 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000746}
747
748/**
749 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
750 * @netdev: network device struct
751 * @vid: VLAN tag
752 **/
753static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000754 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000755{
756 struct i40evf_adapter *adapter = netdev_priv(netdev);
757
Mitch Williams8ed995f2015-08-28 17:55:57 -0400758 if (!VLAN_ALLOWED(adapter))
759 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000760 if (i40evf_add_vlan(adapter, vid) == NULL)
761 return -ENOMEM;
762 return 0;
763}
764
765/**
766 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
767 * @netdev: network device struct
768 * @vid: VLAN tag
769 **/
770static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000771 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000772{
773 struct i40evf_adapter *adapter = netdev_priv(netdev);
774
Mitch Williams8ed995f2015-08-28 17:55:57 -0400775 if (VLAN_ALLOWED(adapter)) {
776 i40evf_del_vlan(adapter, vid);
777 return 0;
778 }
779 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000780}
781
782/**
783 * i40evf_find_filter - Search filter list for specific mac filter
784 * @adapter: board private structure
785 * @macaddr: the MAC address
786 *
787 * Returns ptr to the filter object or NULL
788 **/
789static struct
790i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
791 u8 *macaddr)
792{
793 struct i40evf_mac_filter *f;
794
795 if (!macaddr)
796 return NULL;
797
798 list_for_each_entry(f, &adapter->mac_filter_list, list) {
799 if (ether_addr_equal(macaddr, f->macaddr))
800 return f;
801 }
802 return NULL;
803}
804
805/**
806 * i40e_add_filter - Add a mac filter to the filter list
807 * @adapter: board private structure
808 * @macaddr: the MAC address
809 *
810 * Returns ptr to the filter object or NULL when no memory available.
811 **/
812static struct
813i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
814 u8 *macaddr)
815{
816 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000817 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000818
819 if (!macaddr)
820 return NULL;
821
822 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000823 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000824 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000825 if (--count == 0)
826 return NULL;
827 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000828
829 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000830 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000831 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000832 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000833 clear_bit(__I40EVF_IN_CRITICAL_TASK,
834 &adapter->crit_section);
835 return NULL;
836 }
837
Greg Rose9a173902014-05-22 06:32:02 +0000838 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000839
840 list_add(&f->list, &adapter->mac_filter_list);
841 f->add = true;
842 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
843 }
844
845 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
846 return f;
847}
848
849/**
850 * i40evf_set_mac - NDO callback to set port mac address
851 * @netdev: network interface device structure
852 * @p: pointer to an address structure
853 *
854 * Returns 0 on success, negative on failure
855 **/
856static int i40evf_set_mac(struct net_device *netdev, void *p)
857{
858 struct i40evf_adapter *adapter = netdev_priv(netdev);
859 struct i40e_hw *hw = &adapter->hw;
860 struct i40evf_mac_filter *f;
861 struct sockaddr *addr = p;
862
863 if (!is_valid_ether_addr(addr->sa_data))
864 return -EADDRNOTAVAIL;
865
866 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
867 return 0;
868
Mitch Williams14e52ee2015-08-31 19:54:44 -0400869 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
870 return -EPERM;
871
872 f = i40evf_find_filter(adapter, hw->mac.addr);
873 if (f) {
874 f->remove = true;
875 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
876 }
877
Greg Rose5eae00c2013-12-21 06:12:45 +0000878 f = i40evf_add_filter(adapter, addr->sa_data);
879 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000880 ether_addr_copy(hw->mac.addr, addr->sa_data);
881 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000882 }
883
884 return (f == NULL) ? -ENOMEM : 0;
885}
886
887/**
888 * i40evf_set_rx_mode - NDO callback to set the netdev filters
889 * @netdev: network interface device structure
890 **/
891static void i40evf_set_rx_mode(struct net_device *netdev)
892{
893 struct i40evf_adapter *adapter = netdev_priv(netdev);
894 struct i40evf_mac_filter *f, *ftmp;
895 struct netdev_hw_addr *uca;
896 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400897 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000898 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000899
900 /* add addr if not already in the filter list */
901 netdev_for_each_uc_addr(uca, netdev) {
902 i40evf_add_filter(adapter, uca->addr);
903 }
904 netdev_for_each_mc_addr(mca, netdev) {
905 i40evf_add_filter(adapter, mca->addr);
906 }
907
908 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000909 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000910 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000911 if (--count == 0) {
912 dev_err(&adapter->pdev->dev,
913 "Failed to get lock in %s\n", __func__);
914 return;
915 }
916 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000917 /* remove filter if not in netdev list */
918 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400919 netdev_for_each_mc_addr(mca, netdev)
920 if (ether_addr_equal(mca->addr, f->macaddr))
921 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000922
Shannon Nelson2f41f332015-08-26 15:14:20 -0400923 netdev_for_each_uc_addr(uca, netdev)
924 if (ether_addr_equal(uca->addr, f->macaddr))
925 goto bottom_of_search_loop;
926
927 for_each_dev_addr(netdev, ha)
928 if (ether_addr_equal(ha->addr, f->macaddr))
929 goto bottom_of_search_loop;
930
931 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
932 goto bottom_of_search_loop;
933
934 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
935 f->remove = true;
936 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
937
938bottom_of_search_loop:
939 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000940 }
941 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
942}
943
944/**
945 * i40evf_napi_enable_all - enable NAPI on all queue vectors
946 * @adapter: board private structure
947 **/
948static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
949{
950 int q_idx;
951 struct i40e_q_vector *q_vector;
952 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
953
954 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
955 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000956
Greg Rose5eae00c2013-12-21 06:12:45 +0000957 q_vector = adapter->q_vector[q_idx];
958 napi = &q_vector->napi;
959 napi_enable(napi);
960 }
961}
962
963/**
964 * i40evf_napi_disable_all - disable NAPI on all queue vectors
965 * @adapter: board private structure
966 **/
967static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
968{
969 int q_idx;
970 struct i40e_q_vector *q_vector;
971 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
972
973 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
974 q_vector = adapter->q_vector[q_idx];
975 napi_disable(&q_vector->napi);
976 }
977}
978
979/**
980 * i40evf_configure - set up transmit and receive data structures
981 * @adapter: board private structure
982 **/
983static void i40evf_configure(struct i40evf_adapter *adapter)
984{
985 struct net_device *netdev = adapter->netdev;
986 int i;
987
988 i40evf_set_rx_mode(netdev);
989
990 i40evf_configure_tx(adapter);
991 i40evf_configure_rx(adapter);
992 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
993
Mitch Williamscc052922014-10-25 03:24:34 +0000994 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000995 struct i40e_ring *ring = adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000996
Mitch Williamsa132af22015-01-24 09:58:35 +0000997 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Greg Rose5eae00c2013-12-21 06:12:45 +0000998 ring->next_to_use = ring->count - 1;
999 writel(ring->next_to_use, ring->tail);
1000 }
1001}
1002
1003/**
1004 * i40evf_up_complete - Finish the last steps of bringing up a connection
1005 * @adapter: board private structure
1006 **/
1007static int i40evf_up_complete(struct i40evf_adapter *adapter)
1008{
1009 adapter->state = __I40EVF_RUNNING;
1010 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1011
1012 i40evf_napi_enable_all(adapter);
1013
1014 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1015 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1016 return 0;
1017}
1018
1019/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001020 * i40e_down - Shutdown the connection processing
1021 * @adapter: board private structure
1022 **/
1023void i40evf_down(struct i40evf_adapter *adapter)
1024{
1025 struct net_device *netdev = adapter->netdev;
1026 struct i40evf_mac_filter *f;
1027
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001028 if (adapter->state == __I40EVF_DOWN)
1029 return;
1030
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001031 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1032 &adapter->crit_section))
1033 usleep_range(500, 1000);
1034
Mitch Williams63e18c22015-03-27 00:12:10 -07001035 netif_carrier_off(netdev);
1036 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +00001037 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001038 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001039
Mitch Williamsef8693e2014-02-13 03:48:53 -08001040 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001041 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1042 f->remove = true;
1043 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001044 /* remove all VLAN filters */
1045 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1046 f->remove = true;
1047 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001048 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1049 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001050 /* cancel any current operation */
1051 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001052 /* Schedule operations to close down the HW. Don't wait
1053 * here for this to complete. The watchdog is still running
1054 * and it will take care of this.
1055 */
1056 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001057 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001058 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001059 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001060
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001061 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001062}
1063
1064/**
1065 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1066 * @adapter: board private structure
1067 * @vectors: number of vectors to request
1068 *
1069 * Work with the OS to set up the MSIX vectors needed.
1070 *
1071 * Returns 0 on success, negative on failure
1072 **/
1073static int
1074i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1075{
1076 int err, vector_threshold;
1077
1078 /* We'll want at least 3 (vector_threshold):
1079 * 0) Other (Admin Queue and link, mostly)
1080 * 1) TxQ[0] Cleanup
1081 * 2) RxQ[0] Cleanup
1082 */
1083 vector_threshold = MIN_MSIX_COUNT;
1084
1085 /* The more we get, the more we will assign to Tx/Rx Cleanup
1086 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1087 * Right now, we simply care about how many we'll get; we'll
1088 * set them up later while requesting irq's.
1089 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001090 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1091 vector_threshold, vectors);
1092 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001093 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001094 kfree(adapter->msix_entries);
1095 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001096 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001097 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001098
1099 /* Adjust for only the vectors we'll use, which is minimum
1100 * of max_msix_q_vectors + NONQ_VECS, or the number of
1101 * vectors we were allocated.
1102 */
1103 adapter->num_msix_vectors = err;
1104 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001105}
1106
1107/**
1108 * i40evf_free_queues - Free memory for all rings
1109 * @adapter: board private structure to initialize
1110 *
1111 * Free all of the memory associated with queue pairs.
1112 **/
1113static void i40evf_free_queues(struct i40evf_adapter *adapter)
1114{
1115 int i;
1116
1117 if (!adapter->vsi_res)
1118 return;
Mitch Williamscc052922014-10-25 03:24:34 +00001119 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001120 if (adapter->tx_rings[i])
1121 kfree_rcu(adapter->tx_rings[i], rcu);
1122 adapter->tx_rings[i] = NULL;
1123 adapter->rx_rings[i] = NULL;
1124 }
1125}
1126
1127/**
1128 * i40evf_alloc_queues - Allocate memory for all rings
1129 * @adapter: board private structure to initialize
1130 *
1131 * We allocate one ring per queue at run-time since we don't know the
1132 * number of queues at compile-time. The polling_netdev array is
1133 * intended for Multiqueue, but should work fine with a single queue.
1134 **/
1135static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1136{
1137 int i;
1138
Mitch Williamscc052922014-10-25 03:24:34 +00001139 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001140 struct i40e_ring *tx_ring;
1141 struct i40e_ring *rx_ring;
1142
Mitch Williams75a64432014-11-11 20:02:42 +00001143 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001144 if (!tx_ring)
1145 goto err_out;
1146
1147 tx_ring->queue_index = i;
1148 tx_ring->netdev = adapter->netdev;
1149 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001150 tx_ring->count = adapter->tx_desc_count;
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001151 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1152 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001153 adapter->tx_rings[i] = tx_ring;
1154
1155 rx_ring = &tx_ring[1];
1156 rx_ring->queue_index = i;
1157 rx_ring->netdev = adapter->netdev;
1158 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001159 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001160 adapter->rx_rings[i] = rx_ring;
1161 }
1162
1163 return 0;
1164
1165err_out:
1166 i40evf_free_queues(adapter);
1167 return -ENOMEM;
1168}
1169
1170/**
1171 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1172 * @adapter: board private structure to initialize
1173 *
1174 * Attempt to configure the interrupts using the best available
1175 * capabilities of the hardware and the kernel.
1176 **/
1177static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1178{
1179 int vector, v_budget;
1180 int pairs = 0;
1181 int err = 0;
1182
1183 if (!adapter->vsi_res) {
1184 err = -EIO;
1185 goto out;
1186 }
Mitch Williamscc052922014-10-25 03:24:34 +00001187 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001188
1189 /* It's easy to be greedy for MSI-X vectors, but it really
1190 * doesn't do us much good if we have a lot more vectors
1191 * than CPU's. So let's be conservative and only ask for
1192 * (roughly) twice the number of vectors as there are CPU's.
1193 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001194 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1195 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001196
Greg Rose5eae00c2013-12-21 06:12:45 +00001197 adapter->msix_entries = kcalloc(v_budget,
1198 sizeof(struct msix_entry), GFP_KERNEL);
1199 if (!adapter->msix_entries) {
1200 err = -ENOMEM;
1201 goto out;
1202 }
1203
1204 for (vector = 0; vector < v_budget; vector++)
1205 adapter->msix_entries[vector].entry = vector;
1206
Mitch Williams313ed2d2015-08-27 11:42:31 -04001207 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001208
1209out:
1210 adapter->netdev->real_num_tx_queues = pairs;
1211 return err;
1212}
1213
1214/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001215 * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1216 * @vsi: vsi structure
1217 * @seed: RSS hash seed
1218 **/
1219static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1220{
1221 struct i40e_aqc_get_set_rss_key_data rss_key;
1222 struct i40evf_adapter *adapter = vsi->back;
1223 struct i40e_hw *hw = &adapter->hw;
1224 int ret = 0, i;
1225 u8 *rss_lut;
1226
1227 if (!vsi->id)
1228 return;
1229
1230 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1231 /* bail because we already have a command pending */
1232 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1233 adapter->current_op);
1234 return;
1235 }
1236
1237 memset(&rss_key, 0, sizeof(rss_key));
1238 memcpy(&rss_key, seed, sizeof(rss_key));
1239
1240 rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1241 if (!rss_lut)
1242 return;
1243
1244 /* Populate the LUT with max no. PF queues in round robin fashion */
1245 for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1246 rss_lut[i] = i % adapter->num_active_queues;
1247
1248 ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1249 if (ret) {
1250 dev_err(&adapter->pdev->dev,
1251 "Cannot set RSS key, err %s aq_err %s\n",
1252 i40evf_stat_str(hw, ret),
1253 i40evf_aq_str(hw, hw->aq.asq_last_status));
1254 return;
1255 }
1256
1257 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1258 (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1259 if (ret)
1260 dev_err(&adapter->pdev->dev,
1261 "Cannot set RSS lut, err %s aq_err %s\n",
1262 i40evf_stat_str(hw, ret),
1263 i40evf_aq_str(hw, hw->aq.asq_last_status));
1264}
1265
1266/**
1267 * i40e_configure_rss_reg - Prepare for RSS if used
1268 * @adapter: board private structure
1269 * @seed: RSS hash seed
1270 **/
1271static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1272 const u8 *seed)
1273{
1274 struct i40e_hw *hw = &adapter->hw;
1275 u32 *seed_dw = (u32 *)seed;
1276 u32 cqueue = 0;
1277 u32 lut = 0;
1278 int i, j;
1279
1280 /* Fill out hash function seed */
1281 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1282 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1283
1284 /* Populate the LUT with max no. PF queues in round robin fashion */
1285 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1286 lut = 0;
1287 for (j = 0; j < 4; j++) {
1288 if (cqueue == adapter->num_active_queues)
1289 cqueue = 0;
1290 lut |= ((cqueue) << (8 * j));
1291 cqueue++;
1292 }
1293 wr32(hw, I40E_VFQF_HLUT(i), lut);
1294 }
1295 i40e_flush(hw);
1296}
1297
1298/**
1299 * i40evf_configure_rss - Prepare for RSS
1300 * @adapter: board private structure
1301 **/
1302static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1303{
1304 struct i40e_hw *hw = &adapter->hw;
1305 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1306 u64 hena;
1307
1308 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1309
1310 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1311 hena = I40E_DEFAULT_RSS_HENA;
1312 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1313 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1314
1315 if (RSS_AQ(adapter))
1316 i40evf_configure_rss_aq(&adapter->vsi, seed);
1317 else
1318 i40evf_configure_rss_reg(adapter, seed);
1319}
1320
1321/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001322 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1323 * @adapter: board private structure to initialize
1324 *
1325 * We allocate one q_vector per queue interrupt. If allocation fails we
1326 * return -ENOMEM.
1327 **/
1328static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1329{
1330 int q_idx, num_q_vectors;
1331 struct i40e_q_vector *q_vector;
1332
1333 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1334
1335 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams75a64432014-11-11 20:02:42 +00001336 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001337 if (!q_vector)
1338 goto err_out;
1339 q_vector->adapter = adapter;
1340 q_vector->vsi = &adapter->vsi;
1341 q_vector->v_idx = q_idx;
1342 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001343 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001344 adapter->q_vector[q_idx] = q_vector;
1345 }
1346
1347 return 0;
1348
1349err_out:
1350 while (q_idx) {
1351 q_idx--;
1352 q_vector = adapter->q_vector[q_idx];
1353 netif_napi_del(&q_vector->napi);
1354 kfree(q_vector);
1355 adapter->q_vector[q_idx] = NULL;
1356 }
1357 return -ENOMEM;
1358}
1359
1360/**
1361 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1362 * @adapter: board private structure to initialize
1363 *
1364 * This function frees the memory allocated to the q_vectors. In addition if
1365 * NAPI is enabled it will delete any references to the NAPI struct prior
1366 * to freeing the q_vector.
1367 **/
1368static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1369{
1370 int q_idx, num_q_vectors;
1371 int napi_vectors;
1372
1373 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001374 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001375
1376 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1377 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1378
1379 adapter->q_vector[q_idx] = NULL;
1380 if (q_idx < napi_vectors)
1381 netif_napi_del(&q_vector->napi);
1382 kfree(q_vector);
1383 }
1384}
1385
1386/**
1387 * i40evf_reset_interrupt_capability - Reset MSIX setup
1388 * @adapter: board private structure
1389 *
1390 **/
1391void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1392{
1393 pci_disable_msix(adapter->pdev);
1394 kfree(adapter->msix_entries);
1395 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001396}
1397
1398/**
1399 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1400 * @adapter: board private structure to initialize
1401 *
1402 **/
1403int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1404{
1405 int err;
1406
1407 err = i40evf_set_interrupt_capability(adapter);
1408 if (err) {
1409 dev_err(&adapter->pdev->dev,
1410 "Unable to setup interrupt capabilities\n");
1411 goto err_set_interrupt;
1412 }
1413
1414 err = i40evf_alloc_q_vectors(adapter);
1415 if (err) {
1416 dev_err(&adapter->pdev->dev,
1417 "Unable to allocate memory for queue vectors\n");
1418 goto err_alloc_q_vectors;
1419 }
1420
1421 err = i40evf_alloc_queues(adapter);
1422 if (err) {
1423 dev_err(&adapter->pdev->dev,
1424 "Unable to allocate memory for queues\n");
1425 goto err_alloc_queues;
1426 }
1427
1428 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001429 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1430 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001431
1432 return 0;
1433err_alloc_queues:
1434 i40evf_free_q_vectors(adapter);
1435err_alloc_q_vectors:
1436 i40evf_reset_interrupt_capability(adapter);
1437err_set_interrupt:
1438 return err;
1439}
1440
1441/**
1442 * i40evf_watchdog_timer - Periodic call-back timer
1443 * @data: pointer to adapter disguised as unsigned long
1444 **/
1445static void i40evf_watchdog_timer(unsigned long data)
1446{
1447 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001448
Greg Rose5eae00c2013-12-21 06:12:45 +00001449 schedule_work(&adapter->watchdog_task);
1450 /* timer will be rescheduled in watchdog task */
1451}
1452
1453/**
1454 * i40evf_watchdog_task - Periodic call-back task
1455 * @work: pointer to work_struct
1456 **/
1457static void i40evf_watchdog_task(struct work_struct *work)
1458{
1459 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001460 struct i40evf_adapter,
1461 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001462 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001463 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001464
Greg Rose5eae00c2013-12-21 06:12:45 +00001465 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001466 goto restart_watchdog;
1467
1468 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001469 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1470 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1471 if ((reg_val == I40E_VFR_VFACTIVE) ||
1472 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001473 /* A chance for redemption! */
1474 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1475 adapter->state = __I40EVF_STARTUP;
1476 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1477 schedule_delayed_work(&adapter->init_task, 10);
1478 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1479 &adapter->crit_section);
1480 /* Don't reschedule the watchdog, since we've restarted
1481 * the init task. When init_task contacts the PF and
1482 * gets everything set up again, it'll restart the
1483 * watchdog for us. Down, boy. Sit. Stay. Woof.
1484 */
1485 return;
1486 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001487 adapter->aq_required = 0;
1488 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1489 goto watchdog_done;
1490 }
1491
1492 if ((adapter->state < __I40EVF_DOWN) ||
1493 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001494 goto watchdog_done;
1495
Mitch Williamsef8693e2014-02-13 03:48:53 -08001496 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001497 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1498 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001499 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001500 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001501 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001502 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001503 adapter->aq_required = 0;
1504 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001505 goto watchdog_done;
1506 }
1507
1508 /* Process admin queue tasks. After init, everything gets done
1509 * here so we don't race on the admin queue.
1510 */
Mitch Williamsed636962015-04-07 19:45:32 -04001511 if (adapter->current_op) {
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001512 if (!i40evf_asq_done(hw)) {
1513 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1514 i40evf_send_api_ver(adapter);
1515 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001516 goto watchdog_done;
Mitch A Williams0758e7cb2014-12-09 08:53:08 +00001517 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001518 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1519 i40evf_send_vf_config_msg(adapter);
1520 goto watchdog_done;
1521 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001522
Mitch Williamse284fc82015-03-27 00:12:09 -07001523 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1524 i40evf_disable_queues(adapter);
1525 goto watchdog_done;
1526 }
1527
Greg Rose5eae00c2013-12-21 06:12:45 +00001528 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1529 i40evf_map_queues(adapter);
1530 goto watchdog_done;
1531 }
1532
1533 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1534 i40evf_add_ether_addrs(adapter);
1535 goto watchdog_done;
1536 }
1537
1538 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1539 i40evf_add_vlans(adapter);
1540 goto watchdog_done;
1541 }
1542
1543 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1544 i40evf_del_ether_addrs(adapter);
1545 goto watchdog_done;
1546 }
1547
1548 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1549 i40evf_del_vlans(adapter);
1550 goto watchdog_done;
1551 }
1552
Greg Rose5eae00c2013-12-21 06:12:45 +00001553 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1554 i40evf_configure_queues(adapter);
1555 goto watchdog_done;
1556 }
1557
1558 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1559 i40evf_enable_queues(adapter);
1560 goto watchdog_done;
1561 }
1562
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001563 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1564 /* This message goes straight to the firmware, not the
1565 * PF, so we don't have to set current_op as we will
1566 * not get a response through the ARQ.
1567 */
1568 i40evf_configure_rss(adapter);
1569 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1570 goto watchdog_done;
1571 }
1572
Greg Rose5eae00c2013-12-21 06:12:45 +00001573 if (adapter->state == __I40EVF_RUNNING)
1574 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001575watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001576 if (adapter->state == __I40EVF_RUNNING) {
1577 i40evf_irq_enable_queues(adapter, ~0);
1578 i40evf_fire_sw_int(adapter, 0xFF);
1579 } else {
1580 i40evf_fire_sw_int(adapter, 0x1);
1581 }
1582
Mitch Williamsef8693e2014-02-13 03:48:53 -08001583 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1584restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001585 if (adapter->state == __I40EVF_REMOVE)
1586 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001587 if (adapter->aq_required)
1588 mod_timer(&adapter->watchdog_timer,
1589 jiffies + msecs_to_jiffies(20));
1590 else
1591 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001592 schedule_work(&adapter->adminq_task);
1593}
1594
Mitch Williams67c818a2015-06-19 08:56:30 -07001595#define I40EVF_RESET_WAIT_MS 10
1596#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001597/**
1598 * i40evf_reset_task - Call-back task to handle hardware reset
1599 * @work: pointer to work_struct
1600 *
1601 * During reset we need to shut down and reinitialize the admin queue
1602 * before we can use it to communicate with the PF again. We also clear
1603 * and reinit the rings because that context is lost as well.
1604 **/
1605static void i40evf_reset_task(struct work_struct *work)
1606{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001607 struct i40evf_adapter *adapter = container_of(work,
1608 struct i40evf_adapter,
1609 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001610 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001611 struct i40e_hw *hw = &adapter->hw;
Mitch Williams40d01362015-10-01 14:37:37 -04001612 struct i40evf_vlan_filter *vlf;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001613 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001614 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001615 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001616
1617 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1618 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001619 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001620
Mitch Williams67c818a2015-06-19 08:56:30 -07001621 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001622 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001623 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1624 /* Restart the AQ here. If we have been reset but didn't
1625 * detect it, or if the PF had to reinit, our AQ will be hosed.
1626 */
1627 i40evf_shutdown_adminq(hw);
1628 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001629 i40evf_request_reset(adapter);
1630 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001631 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001632
Mitch Williamsef8693e2014-02-13 03:48:53 -08001633 /* poll until we see the reset actually happen */
1634 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001635 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1636 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1637 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001638 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001639 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001640 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001641 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001642 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001643 goto continue_reset; /* act like the reset happened */
1644 }
1645
1646 /* wait until the reset is complete and the PF is responding to us */
1647 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001648 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1649 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1650 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001651 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001652 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001653 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001654 /* extra wait to make sure minimum wait is met */
1655 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001656 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001657 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001658 struct i40evf_vlan_filter *fv, *fvtmp;
1659
Greg Rose5eae00c2013-12-21 06:12:45 +00001660 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001661 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001662 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001663 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1664
Mitch Williams169f4072014-04-01 07:11:50 +00001665 if (netif_running(adapter->netdev)) {
1666 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001667 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001668 netif_tx_disable(netdev);
1669 i40evf_napi_disable_all(adapter);
1670 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001671 i40evf_free_traffic_irqs(adapter);
1672 i40evf_free_all_tx_resources(adapter);
1673 i40evf_free_all_rx_resources(adapter);
1674 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001675
1676 /* Delete all of the filters, both MAC and VLAN. */
1677 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1678 list) {
1679 list_del(&f->list);
1680 kfree(f);
1681 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001682
Mitch Williams6ba36a22014-08-01 13:27:14 -07001683 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1684 list) {
1685 list_del(&fv->list);
1686 kfree(fv);
1687 }
1688
Mitch Williamsef8693e2014-02-13 03:48:53 -08001689 i40evf_free_misc_irq(adapter);
1690 i40evf_reset_interrupt_capability(adapter);
1691 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001692 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001693 kfree(adapter->vf_res);
1694 i40evf_shutdown_adminq(hw);
1695 adapter->netdev->flags &= ~IFF_UP;
1696 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001697 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1698 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001699 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001700 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001701
1702continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001703 if (netif_running(adapter->netdev)) {
1704 netif_carrier_off(netdev);
1705 netif_tx_stop_all_queues(netdev);
1706 i40evf_napi_disable_all(adapter);
1707 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001708 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001709
Greg Rose5eae00c2013-12-21 06:12:45 +00001710 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001711 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1712
1713 /* free the Tx/Rx rings and descriptors, might be better to just
1714 * re-use them sometime in the future
1715 */
1716 i40evf_free_all_rx_resources(adapter);
1717 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001718
1719 /* kill and reinit the admin queue */
1720 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001721 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1722 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001723 err = i40evf_init_adminq(hw);
1724 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001725 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1726 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001727
Mitch Williamse6d038d2015-06-04 16:23:58 -04001728 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1729 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001730
1731 /* re-add all MAC filters */
1732 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1733 f->add = true;
1734 }
1735 /* re-add all VLAN filters */
Mitch Williams40d01362015-10-01 14:37:37 -04001736 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1737 vlf->add = true;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001738 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001739 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001740 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001741 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001742 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001743
1744 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1745
1746 if (netif_running(adapter->netdev)) {
1747 /* allocate transmit descriptors */
1748 err = i40evf_setup_all_tx_resources(adapter);
1749 if (err)
1750 goto reset_err;
1751
1752 /* allocate receive descriptors */
1753 err = i40evf_setup_all_rx_resources(adapter);
1754 if (err)
1755 goto reset_err;
1756
1757 i40evf_configure(adapter);
1758
1759 err = i40evf_up_complete(adapter);
1760 if (err)
1761 goto reset_err;
1762
1763 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001764 } else {
1765 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001766 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001767
Greg Rose5eae00c2013-12-21 06:12:45 +00001768 return;
1769reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001770 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001771 i40evf_close(adapter->netdev);
1772}
1773
1774/**
1775 * i40evf_adminq_task - worker thread to clean the admin queue
1776 * @work: pointer to work_struct containing our data
1777 **/
1778static void i40evf_adminq_task(struct work_struct *work)
1779{
1780 struct i40evf_adapter *adapter =
1781 container_of(work, struct i40evf_adapter, adminq_task);
1782 struct i40e_hw *hw = &adapter->hw;
1783 struct i40e_arq_event_info event;
1784 struct i40e_virtchnl_msg *v_msg;
1785 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001786 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001787 u16 pending;
1788
Mitch Williamsef8693e2014-02-13 03:48:53 -08001789 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001790 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001791
Mitch Williams1001dc32014-11-11 20:02:19 +00001792 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1793 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001794 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001795 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001796
Greg Rose5eae00c2013-12-21 06:12:45 +00001797 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1798 do {
1799 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001800 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001801 break; /* No event to process or error cleaning ARQ */
1802
1803 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1804 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001805 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001806 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001807 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001808 } while (pending);
1809
Mitch Williams67c818a2015-06-19 08:56:30 -07001810 if ((adapter->flags &
1811 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1812 adapter->state == __I40EVF_RESETTING)
1813 goto freedom;
1814
Mitch Williams912257e2014-05-22 06:32:07 +00001815 /* check for error indications */
1816 val = rd32(hw, hw->aq.arq.len);
1817 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001818 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001819 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001820 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001821 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001822 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001823 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001824 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001825 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001826 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001827 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001828 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001829 }
1830 if (oldval != val)
1831 wr32(hw, hw->aq.arq.len, val);
1832
1833 val = rd32(hw, hw->aq.asq.len);
1834 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001835 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001836 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001837 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001838 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001839 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001840 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001841 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001842 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001843 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001844 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001845 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001846 }
1847 if (oldval != val)
1848 wr32(hw, hw->aq.asq.len, val);
1849
Mitch Williams67c818a2015-06-19 08:56:30 -07001850freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001851 kfree(event.msg_buf);
1852out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001853 /* re-enable Admin queue interrupt cause */
1854 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001855}
1856
1857/**
1858 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1859 * @adapter: board private structure
1860 *
1861 * Free all transmit software resources
1862 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001863void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001864{
1865 int i;
1866
Mitch Williamscc052922014-10-25 03:24:34 +00001867 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001868 if (adapter->tx_rings[i]->desc)
1869 i40evf_free_tx_resources(adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001870}
1871
1872/**
1873 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1874 * @adapter: board private structure
1875 *
1876 * If this function returns with an error, then it's possible one or
1877 * more of the rings is populated (while the rest are not). It is the
1878 * callers duty to clean those orphaned rings.
1879 *
1880 * Return 0 on success, negative on failure
1881 **/
1882static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1883{
1884 int i, err = 0;
1885
Mitch Williamscc052922014-10-25 03:24:34 +00001886 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001887 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001888 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1889 if (!err)
1890 continue;
1891 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001892 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001893 break;
1894 }
1895
1896 return err;
1897}
1898
1899/**
1900 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1901 * @adapter: board private structure
1902 *
1903 * If this function returns with an error, then it's possible one or
1904 * more of the rings is populated (while the rest are not). It is the
1905 * callers duty to clean those orphaned rings.
1906 *
1907 * Return 0 on success, negative on failure
1908 **/
1909static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1910{
1911 int i, err = 0;
1912
Mitch Williamscc052922014-10-25 03:24:34 +00001913 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001914 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001915 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1916 if (!err)
1917 continue;
1918 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001919 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001920 break;
1921 }
1922 return err;
1923}
1924
1925/**
1926 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1927 * @adapter: board private structure
1928 *
1929 * Free all receive software resources
1930 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001931void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001932{
1933 int i;
1934
Mitch Williamscc052922014-10-25 03:24:34 +00001935 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001936 if (adapter->rx_rings[i]->desc)
1937 i40evf_free_rx_resources(adapter->rx_rings[i]);
1938}
1939
1940/**
1941 * i40evf_open - Called when a network interface is made active
1942 * @netdev: network interface device structure
1943 *
1944 * Returns 0 on success, negative value on failure
1945 *
1946 * The open entry point is called when a network interface is made
1947 * active by the system (IFF_UP). At this point all resources needed
1948 * for transmit and receive operations are allocated, the interrupt
1949 * handler is registered with the OS, the watchdog timer is started,
1950 * and the stack is notified that the interface is ready.
1951 **/
1952static int i40evf_open(struct net_device *netdev)
1953{
1954 struct i40evf_adapter *adapter = netdev_priv(netdev);
1955 int err;
1956
Mitch Williamsef8693e2014-02-13 03:48:53 -08001957 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1958 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1959 return -EIO;
1960 }
Mitch Williamse284fc82015-03-27 00:12:09 -07001961 if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
Greg Rose5eae00c2013-12-21 06:12:45 +00001962 return -EBUSY;
1963
1964 /* allocate transmit descriptors */
1965 err = i40evf_setup_all_tx_resources(adapter);
1966 if (err)
1967 goto err_setup_tx;
1968
1969 /* allocate receive descriptors */
1970 err = i40evf_setup_all_rx_resources(adapter);
1971 if (err)
1972 goto err_setup_rx;
1973
1974 /* clear any pending interrupts, may auto mask */
1975 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1976 if (err)
1977 goto err_req_irq;
1978
Mitch Williams44151cd2015-04-27 14:57:17 -04001979 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00001980 i40evf_configure(adapter);
1981
1982 err = i40evf_up_complete(adapter);
1983 if (err)
1984 goto err_req_irq;
1985
1986 i40evf_irq_enable(adapter, true);
1987
1988 return 0;
1989
1990err_req_irq:
1991 i40evf_down(adapter);
1992 i40evf_free_traffic_irqs(adapter);
1993err_setup_rx:
1994 i40evf_free_all_rx_resources(adapter);
1995err_setup_tx:
1996 i40evf_free_all_tx_resources(adapter);
1997
1998 return err;
1999}
2000
2001/**
2002 * i40evf_close - Disables a network interface
2003 * @netdev: network interface device structure
2004 *
2005 * Returns 0, this is not allowed to fail
2006 *
2007 * The close entry point is called when an interface is de-activated
2008 * by the OS. The hardware is still under the drivers control, but
2009 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2010 * are freed, along with all transmit and receive resources.
2011 **/
2012static int i40evf_close(struct net_device *netdev)
2013{
2014 struct i40evf_adapter *adapter = netdev_priv(netdev);
2015
Mitch Williamsef8693e2014-02-13 03:48:53 -08002016 if (adapter->state <= __I40EVF_DOWN)
2017 return 0;
2018
Mitch Williamsef8693e2014-02-13 03:48:53 -08002019
Greg Rose5eae00c2013-12-21 06:12:45 +00002020 set_bit(__I40E_DOWN, &adapter->vsi.state);
2021
2022 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00002023 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002024 i40evf_free_traffic_irqs(adapter);
2025
Greg Rose5eae00c2013-12-21 06:12:45 +00002026 return 0;
2027}
2028
2029/**
2030 * i40evf_get_stats - Get System Network Statistics
2031 * @netdev: network interface device structure
2032 *
2033 * Returns the address of the device statistics structure.
2034 * The statistics are actually updated from the timer callback.
2035 **/
2036static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2037{
2038 struct i40evf_adapter *adapter = netdev_priv(netdev);
2039
2040 /* only return the current stats */
2041 return &adapter->net_stats;
2042}
2043
2044/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002045 * i40evf_change_mtu - Change the Maximum Transfer Unit
2046 * @netdev: network interface device structure
2047 * @new_mtu: new value for maximum frame size
2048 *
2049 * Returns 0 on success, negative on failure
2050 **/
2051static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2052{
2053 struct i40evf_adapter *adapter = netdev_priv(netdev);
2054 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2055
2056 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2057 return -EINVAL;
2058
Greg Rose5eae00c2013-12-21 06:12:45 +00002059 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002060 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2061 schedule_work(&adapter->reset_task);
2062
Greg Rose5eae00c2013-12-21 06:12:45 +00002063 return 0;
2064}
2065
2066static const struct net_device_ops i40evf_netdev_ops = {
2067 .ndo_open = i40evf_open,
2068 .ndo_stop = i40evf_close,
2069 .ndo_start_xmit = i40evf_xmit_frame,
2070 .ndo_get_stats = i40evf_get_stats,
2071 .ndo_set_rx_mode = i40evf_set_rx_mode,
2072 .ndo_validate_addr = eth_validate_addr,
2073 .ndo_set_mac_address = i40evf_set_mac,
2074 .ndo_change_mtu = i40evf_change_mtu,
2075 .ndo_tx_timeout = i40evf_tx_timeout,
2076 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2077 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002078#ifdef CONFIG_NET_POLL_CONTROLLER
2079 .ndo_poll_controller = i40evf_netpoll,
2080#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002081};
2082
2083/**
2084 * i40evf_check_reset_complete - check that VF reset is complete
2085 * @hw: pointer to hw struct
2086 *
2087 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2088 **/
2089static int i40evf_check_reset_complete(struct i40e_hw *hw)
2090{
2091 u32 rstat;
2092 int i;
2093
2094 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002095 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2096 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2097 if ((rstat == I40E_VFR_VFACTIVE) ||
2098 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002099 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002100 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002101 }
2102 return -EBUSY;
2103}
2104
2105/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002106 * i40evf_process_config - Process the config information we got from the PF
2107 * @adapter: board private structure
2108 *
2109 * Verify that we have a valid config struct, and set up our netdev features
2110 * and our VSI struct.
2111 **/
2112int i40evf_process_config(struct i40evf_adapter *adapter)
2113{
2114 struct net_device *netdev = adapter->netdev;
2115 int i;
2116
2117 /* got VF config message back from PF, now we can parse it */
2118 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2119 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2120 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2121 }
2122 if (!adapter->vsi_res) {
2123 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2124 return -ENODEV;
2125 }
2126
2127 if (adapter->vf_res->vf_offload_flags
2128 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
Mitch Williamscc7e4062015-09-28 14:12:40 -04002129 netdev->vlan_features = netdev->features &
2130 ~(NETIF_F_HW_VLAN_CTAG_TX |
2131 NETIF_F_HW_VLAN_CTAG_RX |
2132 NETIF_F_HW_VLAN_CTAG_FILTER);
Mitch Williamse6d038d2015-06-04 16:23:58 -04002133 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2134 NETIF_F_HW_VLAN_CTAG_RX |
2135 NETIF_F_HW_VLAN_CTAG_FILTER;
2136 }
2137 netdev->features |= NETIF_F_HIGHDMA |
2138 NETIF_F_SG |
2139 NETIF_F_IP_CSUM |
2140 NETIF_F_SCTP_CSUM |
2141 NETIF_F_IPV6_CSUM |
2142 NETIF_F_TSO |
2143 NETIF_F_TSO6 |
2144 NETIF_F_RXCSUM |
2145 NETIF_F_GRO;
2146
2147 /* copy netdev features into list of user selectable features */
2148 netdev->hw_features |= netdev->features;
2149 netdev->hw_features &= ~NETIF_F_RXCSUM;
2150
2151 adapter->vsi.id = adapter->vsi_res->vsi_id;
2152
2153 adapter->vsi.back = adapter;
2154 adapter->vsi.base_vector = 1;
2155 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2156 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2157 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2158 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2159 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2160 adapter->vsi.netdev = adapter->netdev;
Mitch Williamsf578f5f2015-08-28 17:55:58 -04002161 adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002162 return 0;
2163}
2164
2165/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002166 * i40evf_init_task - worker thread to perform delayed initialization
2167 * @work: pointer to work_struct containing our data
2168 *
2169 * This task completes the work that was begun in probe. Due to the nature
2170 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002171 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002172 * whole system, we'll do it in a task so we can sleep.
2173 * This task only runs during driver init. Once we've established
2174 * communications with the PF driver and set up our netdev, the watchdog
2175 * takes over.
2176 **/
2177static void i40evf_init_task(struct work_struct *work)
2178{
2179 struct i40evf_adapter *adapter = container_of(work,
2180 struct i40evf_adapter,
2181 init_task.work);
2182 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002183 struct i40e_hw *hw = &adapter->hw;
2184 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002185 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002186
2187 switch (adapter->state) {
2188 case __I40EVF_STARTUP:
2189 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002190 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2191 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002192 err = i40e_set_mac_type(hw);
2193 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002194 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2195 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002196 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002197 }
2198 err = i40evf_check_reset_complete(hw);
2199 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002200 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002201 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002202 goto err;
2203 }
2204 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2205 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2206 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2207 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2208
2209 err = i40evf_init_adminq(hw);
2210 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002211 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2212 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002213 goto err;
2214 }
2215 err = i40evf_send_api_ver(adapter);
2216 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002217 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002218 i40evf_shutdown_adminq(hw);
2219 goto err;
2220 }
2221 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2222 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002223 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002224 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002225 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002226 i40evf_shutdown_adminq(hw);
2227 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002228 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002229 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002230
2231 /* aq msg sent, awaiting reply */
2232 err = i40evf_verify_api_ver(adapter);
2233 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002234 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002235 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002236 else
2237 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2238 adapter->pf_version.major,
2239 adapter->pf_version.minor,
2240 I40E_VIRTCHNL_VERSION_MAJOR,
2241 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002242 goto err;
2243 }
2244 err = i40evf_send_vf_config_msg(adapter);
2245 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002246 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002247 err);
2248 goto err;
2249 }
2250 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2251 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002252 case __I40EVF_INIT_GET_RESOURCES:
2253 /* aq msg sent, awaiting reply */
2254 if (!adapter->vf_res) {
2255 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2256 (I40E_MAX_VF_VSI *
2257 sizeof(struct i40e_virtchnl_vsi_resource));
2258 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002259 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002260 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002261 }
2262 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002263 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002264 err = i40evf_send_vf_config_msg(adapter);
2265 goto err;
2266 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002267 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002268 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2269 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002270 goto err_alloc;
2271 }
2272 adapter->state = __I40EVF_INIT_SW;
2273 break;
2274 default:
2275 goto err_alloc;
2276 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04002277 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002278 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002279 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002280
2281 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2282
Greg Rose5eae00c2013-12-21 06:12:45 +00002283 netdev->netdev_ops = &i40evf_netdev_ops;
2284 i40evf_set_ethtool_ops(netdev);
2285 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002286
Greg Rose5eae00c2013-12-21 06:12:45 +00002287 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002288 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002289 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002290 eth_hw_addr_random(netdev);
2291 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2292 } else {
2293 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2294 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2295 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002296 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002297
Greg Rose5eae00c2013-12-21 06:12:45 +00002298 init_timer(&adapter->watchdog_timer);
2299 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2300 adapter->watchdog_timer.data = (unsigned long)adapter;
2301 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2302
Mitch Williamscc052922014-10-25 03:24:34 +00002303 adapter->num_active_queues = min_t(int,
2304 adapter->vsi_res->num_queue_pairs,
2305 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002306 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2307 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002308 err = i40evf_init_interrupt_scheme(adapter);
2309 if (err)
2310 goto err_sw_init;
2311 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002312 if (adapter->vf_res->vf_offload_flags &
2313 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2314 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002315 if (!RSS_AQ(adapter))
2316 i40evf_configure_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002317 err = i40evf_request_misc_irq(adapter);
2318 if (err)
2319 goto err_sw_init;
2320
2321 netif_carrier_off(netdev);
2322
Mitch Williamsef8693e2014-02-13 03:48:53 -08002323 if (!adapter->netdev_registered) {
2324 err = register_netdev(netdev);
2325 if (err)
2326 goto err_register;
2327 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002328
2329 adapter->netdev_registered = true;
2330
2331 netif_tx_stop_all_queues(netdev);
2332
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002333 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002334 if (netdev->features & NETIF_F_GRO)
2335 dev_info(&pdev->dev, "GRO is enabled\n");
2336
2337 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2338 adapter->state = __I40EVF_DOWN;
2339 set_bit(__I40E_DOWN, &adapter->vsi.state);
2340 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04002341
2342 if (RSS_AQ(adapter)) {
2343 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2344 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2345 } else {
2346 i40evf_configure_rss(adapter);
2347 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002348 return;
2349restart:
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002350 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
Greg Rose5eae00c2013-12-21 06:12:45 +00002351 return;
2352
2353err_register:
2354 i40evf_free_misc_irq(adapter);
2355err_sw_init:
2356 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002357err_alloc:
2358 kfree(adapter->vf_res);
2359 adapter->vf_res = NULL;
2360err:
2361 /* Things went into the weeds, so try again later */
2362 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williamsb9029e92015-09-28 14:16:50 -04002363 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002364 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Mitch Williamsb9029e92015-09-28 14:16:50 -04002365 i40evf_shutdown_adminq(hw);
2366 adapter->state = __I40EVF_STARTUP;
2367 schedule_delayed_work(&adapter->init_task, HZ * 5);
2368 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00002369 }
Mitch Williams3f7e5c32015-09-28 14:12:42 -04002370 schedule_delayed_work(&adapter->init_task, HZ);
Greg Rose5eae00c2013-12-21 06:12:45 +00002371}
2372
2373/**
2374 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2375 * @pdev: pci device structure
2376 **/
2377static void i40evf_shutdown(struct pci_dev *pdev)
2378{
2379 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002380 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002381
2382 netif_device_detach(netdev);
2383
2384 if (netif_running(netdev))
2385 i40evf_close(netdev);
2386
Mitch Williams00293fd2015-01-09 11:18:18 +00002387 /* Prevent the watchdog from running. */
2388 adapter->state = __I40EVF_REMOVE;
2389 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002390
Greg Rose5eae00c2013-12-21 06:12:45 +00002391#ifdef CONFIG_PM
2392 pci_save_state(pdev);
2393
2394#endif
2395 pci_disable_device(pdev);
2396}
2397
2398/**
2399 * i40evf_probe - Device Initialization Routine
2400 * @pdev: PCI device information struct
2401 * @ent: entry in i40evf_pci_tbl
2402 *
2403 * Returns 0 on success, negative on failure
2404 *
2405 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2406 * The OS initialization, configuring of the adapter private structure,
2407 * and a hardware reset occur.
2408 **/
2409static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2410{
2411 struct net_device *netdev;
2412 struct i40evf_adapter *adapter = NULL;
2413 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002414 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002415
2416 err = pci_enable_device(pdev);
2417 if (err)
2418 return err;
2419
Mitch Williams64942942014-02-11 08:26:33 +00002420 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002421 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002422 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2423 if (err) {
2424 dev_err(&pdev->dev,
2425 "DMA configuration failed: 0x%x\n", err);
2426 goto err_dma;
2427 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002428 }
2429
2430 err = pci_request_regions(pdev, i40evf_driver_name);
2431 if (err) {
2432 dev_err(&pdev->dev,
2433 "pci_request_regions failed 0x%x\n", err);
2434 goto err_pci_reg;
2435 }
2436
2437 pci_enable_pcie_error_reporting(pdev);
2438
2439 pci_set_master(pdev);
2440
2441 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2442 MAX_TX_QUEUES);
2443 if (!netdev) {
2444 err = -ENOMEM;
2445 goto err_alloc_etherdev;
2446 }
2447
2448 SET_NETDEV_DEV(netdev, &pdev->dev);
2449
2450 pci_set_drvdata(pdev, netdev);
2451 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002452
2453 adapter->netdev = netdev;
2454 adapter->pdev = pdev;
2455
2456 hw = &adapter->hw;
2457 hw->back = adapter;
2458
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002459 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002460 adapter->state = __I40EVF_STARTUP;
2461
2462 /* Call save state here because it relies on the adapter struct. */
2463 pci_save_state(pdev);
2464
2465 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2466 pci_resource_len(pdev, 0));
2467 if (!hw->hw_addr) {
2468 err = -EIO;
2469 goto err_ioremap;
2470 }
2471 hw->vendor_id = pdev->vendor;
2472 hw->device_id = pdev->device;
2473 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2474 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2475 hw->subsystem_device_id = pdev->subsystem_device;
2476 hw->bus.device = PCI_SLOT(pdev->devfn);
2477 hw->bus.func = PCI_FUNC(pdev->devfn);
2478
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002479 /* set up the locks for the AQ, do this only once in probe
2480 * and destroy them only once in remove
2481 */
2482 mutex_init(&hw->aq.asq_mutex);
2483 mutex_init(&hw->aq.arq_mutex);
2484
Serey Kong8bb1a542014-08-01 13:27:15 -07002485 INIT_LIST_HEAD(&adapter->mac_filter_list);
2486 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2487
Greg Rose5eae00c2013-12-21 06:12:45 +00002488 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2489 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2490 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2491 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002492 schedule_delayed_work(&adapter->init_task,
2493 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002494
2495 return 0;
2496
2497err_ioremap:
2498 free_netdev(netdev);
2499err_alloc_etherdev:
2500 pci_release_regions(pdev);
2501err_pci_reg:
2502err_dma:
2503 pci_disable_device(pdev);
2504 return err;
2505}
2506
2507#ifdef CONFIG_PM
2508/**
2509 * i40evf_suspend - Power management suspend routine
2510 * @pdev: PCI device information struct
2511 * @state: unused
2512 *
2513 * Called when the system (VM) is entering sleep/suspend.
2514 **/
2515static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2516{
2517 struct net_device *netdev = pci_get_drvdata(pdev);
2518 struct i40evf_adapter *adapter = netdev_priv(netdev);
2519 int retval = 0;
2520
2521 netif_device_detach(netdev);
2522
2523 if (netif_running(netdev)) {
2524 rtnl_lock();
2525 i40evf_down(adapter);
2526 rtnl_unlock();
2527 }
2528 i40evf_free_misc_irq(adapter);
2529 i40evf_reset_interrupt_capability(adapter);
2530
2531 retval = pci_save_state(pdev);
2532 if (retval)
2533 return retval;
2534
2535 pci_disable_device(pdev);
2536
2537 return 0;
2538}
2539
2540/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002541 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002542 * @pdev: PCI device information struct
2543 *
2544 * Called when the system (VM) is resumed from sleep/suspend.
2545 **/
2546static int i40evf_resume(struct pci_dev *pdev)
2547{
2548 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2549 struct net_device *netdev = adapter->netdev;
2550 u32 err;
2551
2552 pci_set_power_state(pdev, PCI_D0);
2553 pci_restore_state(pdev);
2554 /* pci_restore_state clears dev->state_saved so call
2555 * pci_save_state to restore it.
2556 */
2557 pci_save_state(pdev);
2558
2559 err = pci_enable_device_mem(pdev);
2560 if (err) {
2561 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2562 return err;
2563 }
2564 pci_set_master(pdev);
2565
2566 rtnl_lock();
2567 err = i40evf_set_interrupt_capability(adapter);
2568 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002569 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002570 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2571 return err;
2572 }
2573 err = i40evf_request_misc_irq(adapter);
2574 rtnl_unlock();
2575 if (err) {
2576 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2577 return err;
2578 }
2579
2580 schedule_work(&adapter->reset_task);
2581
2582 netif_device_attach(netdev);
2583
2584 return err;
2585}
2586
2587#endif /* CONFIG_PM */
2588/**
2589 * i40evf_remove - Device Removal Routine
2590 * @pdev: PCI device information struct
2591 *
2592 * i40evf_remove is called by the PCI subsystem to alert the driver
2593 * that it should release a PCI device. The could be caused by a
2594 * Hot-Plug event, or because the driver is going to be removed from
2595 * memory.
2596 **/
2597static void i40evf_remove(struct pci_dev *pdev)
2598{
2599 struct net_device *netdev = pci_get_drvdata(pdev);
2600 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002601 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002602 struct i40e_hw *hw = &adapter->hw;
2603
2604 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002605 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002606
2607 if (adapter->netdev_registered) {
2608 unregister_netdev(netdev);
2609 adapter->netdev_registered = false;
2610 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002611
Mitch Williamsf4a71882015-01-09 11:18:16 +00002612 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002613 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002614 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002615 i40evf_request_reset(adapter);
2616 msleep(20);
2617 /* If the FW isn't responding, kick it once, but only once. */
2618 if (!i40evf_asq_done(hw)) {
2619 i40evf_request_reset(adapter);
2620 msleep(20);
2621 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002622
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002623 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002624 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002625 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002626 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002627 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002628 }
2629
Mitch Williamse5d17c32014-06-04 04:22:38 +00002630 if (adapter->watchdog_timer.function)
2631 del_timer_sync(&adapter->watchdog_timer);
2632
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002633 flush_scheduled_work();
2634
Greg Rose5eae00c2013-12-21 06:12:45 +00002635 if (hw->aq.asq.count)
2636 i40evf_shutdown_adminq(hw);
2637
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -08002638 /* destroy the locks only once, here */
2639 mutex_destroy(&hw->aq.arq_mutex);
2640 mutex_destroy(&hw->aq.asq_mutex);
2641
Greg Rose5eae00c2013-12-21 06:12:45 +00002642 iounmap(hw->hw_addr);
2643 pci_release_regions(pdev);
2644
Mitch Williamse284fc82015-03-27 00:12:09 -07002645 i40evf_free_all_tx_resources(adapter);
2646 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002647 i40evf_free_queues(adapter);
2648 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002649 /* If we got removed before an up/down sequence, we've got a filter
2650 * hanging out there that we need to get rid of.
2651 */
2652 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2653 list_del(&f->list);
2654 kfree(f);
2655 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002656 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2657 list_del(&f->list);
2658 kfree(f);
2659 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002660
2661 free_netdev(netdev);
2662
2663 pci_disable_pcie_error_reporting(pdev);
2664
2665 pci_disable_device(pdev);
2666}
2667
2668static struct pci_driver i40evf_driver = {
2669 .name = i40evf_driver_name,
2670 .id_table = i40evf_pci_tbl,
2671 .probe = i40evf_probe,
2672 .remove = i40evf_remove,
2673#ifdef CONFIG_PM
2674 .suspend = i40evf_suspend,
2675 .resume = i40evf_resume,
2676#endif
2677 .shutdown = i40evf_shutdown,
2678};
2679
2680/**
2681 * i40e_init_module - Driver Registration Routine
2682 *
2683 * i40e_init_module is the first routine called when the driver is
2684 * loaded. All it does is register with the PCI subsystem.
2685 **/
2686static int __init i40evf_init_module(void)
2687{
2688 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002689
Greg Rose5eae00c2013-12-21 06:12:45 +00002690 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002691 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002692
2693 pr_info("%s\n", i40evf_copyright);
2694
2695 ret = pci_register_driver(&i40evf_driver);
2696 return ret;
2697}
2698
2699module_init(i40evf_init_module);
2700
2701/**
2702 * i40e_exit_module - Driver Exit Cleanup Routine
2703 *
2704 * i40e_exit_module is called just before the driver is removed
2705 * from memory.
2706 **/
2707static void __exit i40evf_exit_module(void)
2708{
2709 pci_unregister_driver(&i40evf_driver);
2710}
2711
2712module_exit(i40evf_exit_module);
2713
2714/* i40evf_main.c */