blob: 46af35fe8f07243f894a3a261a9c2783bf74129d [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 Sullivan164f7392015-09-03 17:19:02 -040037#define DRV_VERSION "1.3.19"
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
285 **/
286void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
287{
288 struct i40e_hw *hw = &adapter->hw;
289
Mitch Williams164ec1b2014-06-04 08:45:19 +0000290 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000291 i40evf_irq_enable_queues(adapter, ~0);
292
293 if (flush)
294 rd32(hw, I40E_VFGEN_RSTAT);
295}
296
297/**
298 * i40evf_msix_aq - Interrupt handler for vector 0
299 * @irq: interrupt number
300 * @data: pointer to netdev
301 **/
302static irqreturn_t i40evf_msix_aq(int irq, void *data)
303{
304 struct net_device *netdev = data;
305 struct i40evf_adapter *adapter = netdev_priv(netdev);
306 struct i40e_hw *hw = &adapter->hw;
307 u32 val;
308 u32 ena_mask;
309
310 /* handle non-queue interrupts */
311 val = rd32(hw, I40E_VFINT_ICR01);
312 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
313
314
315 val = rd32(hw, I40E_VFINT_DYN_CTL01);
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -0400316 val = val | 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
337 napi_schedule(&q_vector->napi);
338
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;
360}
361
362/**
363 * i40evf_map_vector_to_txq - associate irqs with tx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @t_idx: queue number
367 **/
368static void
369i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
370{
371 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
372 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
373
374 tx_ring->q_vector = q_vector;
375 tx_ring->next = q_vector->tx.ring;
376 tx_ring->vsi = &adapter->vsi;
377 q_vector->tx.ring = tx_ring;
378 q_vector->tx.count++;
379 q_vector->tx.latency_range = I40E_LOW_LATENCY;
380 q_vector->num_ringpairs++;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400381 q_vector->ring_mask |= BIT(t_idx);
Greg Rose5eae00c2013-12-21 06:12:45 +0000382}
383
384/**
385 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
386 * @adapter: board private structure to initialize
387 *
388 * This function maps descriptor rings to the queue-specific vectors
389 * we were allotted through the MSI-X enabling code. Ideally, we'd have
390 * one vector per ring/queue, but on a constrained vector budget, we
391 * group the rings as "efficiently" as possible. You would add new
392 * mapping configurations in here.
393 **/
394static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
395{
396 int q_vectors;
397 int v_start = 0;
398 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000399 int rxr_remaining = adapter->num_active_queues;
400 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000401 int i, j;
402 int rqpv, tqpv;
403 int err = 0;
404
405 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
406
407 /* The ideal configuration...
408 * We have enough vectors to map one per queue.
409 */
Mitch Williams973371d2015-04-27 14:57:09 -0400410 if (q_vectors >= (rxr_remaining * 2)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000411 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
412 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
413
414 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
415 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
416 goto out;
417 }
418
419 /* If we don't have enough vectors for a 1-to-1
420 * mapping, we'll have to group them so there are
421 * multiple queues per vector.
422 * Re-adjusting *qpv takes care of the remainder.
423 */
424 for (i = v_start; i < q_vectors; i++) {
425 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
426 for (j = 0; j < rqpv; j++) {
427 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
428 rxr_idx++;
429 rxr_remaining--;
430 }
431 }
432 for (i = v_start; i < q_vectors; i++) {
433 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
434 for (j = 0; j < tqpv; j++) {
435 i40evf_map_vector_to_txq(adapter, i, txr_idx);
436 txr_idx++;
437 txr_remaining--;
438 }
439 }
440
441out:
442 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
443
444 return err;
445}
446
Alexander Duyck7709b4c2015-09-24 09:04:38 -0700447#ifdef CONFIG_NET_POLL_CONTROLLER
448/**
449 * i40evf_netpoll - A Polling 'interrupt' handler
450 * @netdev: network interface device structure
451 *
452 * This is used by netconsole to send skbs without having to re-enable
453 * interrupts. It's not called while the normal interrupt routine is executing.
454 **/
455static void i40evf_netpoll(struct net_device *netdev)
456{
457 struct i40evf_adapter *adapter = netdev_priv(netdev);
458 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
459 int i;
460
461 /* if interface is down do nothing */
462 if (test_bit(__I40E_DOWN, &adapter->vsi.state))
463 return;
464
465 for (i = 0; i < q_vectors; i++)
466 i40evf_msix_clean_rings(0, adapter->q_vector[i]);
467}
468
469#endif
Greg Rose5eae00c2013-12-21 06:12:45 +0000470/**
471 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
472 * @adapter: board private structure
473 *
474 * Allocates MSI-X vectors for tx and rx handling, and requests
475 * interrupts from the kernel.
476 **/
477static int
478i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
479{
480 int vector, err, q_vectors;
481 int rx_int_idx = 0, tx_int_idx = 0;
482
483 i40evf_irq_disable(adapter);
484 /* Decrement for Other and TCP Timer vectors */
485 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
486
487 for (vector = 0; vector < q_vectors; vector++) {
488 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
489
490 if (q_vector->tx.ring && q_vector->rx.ring) {
491 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
492 "i40evf-%s-%s-%d", basename,
493 "TxRx", rx_int_idx++);
494 tx_int_idx++;
495 } else if (q_vector->rx.ring) {
496 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
497 "i40evf-%s-%s-%d", basename,
498 "rx", rx_int_idx++);
499 } else if (q_vector->tx.ring) {
500 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
501 "i40evf-%s-%s-%d", basename,
502 "tx", tx_int_idx++);
503 } else {
504 /* skip this unused q_vector */
505 continue;
506 }
507 err = request_irq(
508 adapter->msix_entries[vector + NONQ_VECS].vector,
509 i40evf_msix_clean_rings,
510 0,
511 q_vector->name,
512 q_vector);
513 if (err) {
514 dev_info(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400515 "Request_irq failed, error: %d\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000516 goto free_queue_irqs;
517 }
518 /* assign the mask for this irq */
519 irq_set_affinity_hint(
520 adapter->msix_entries[vector + NONQ_VECS].vector,
521 q_vector->affinity_mask);
522 }
523
524 return 0;
525
526free_queue_irqs:
527 while (vector) {
528 vector--;
529 irq_set_affinity_hint(
530 adapter->msix_entries[vector + NONQ_VECS].vector,
531 NULL);
532 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
533 adapter->q_vector[vector]);
534 }
535 return err;
536}
537
538/**
539 * i40evf_request_misc_irq - Initialize MSI-X interrupts
540 * @adapter: board private structure
541 *
542 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
543 * vector is only for the admin queue, and stays active even when the netdev
544 * is closed.
545 **/
546static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
547{
548 struct net_device *netdev = adapter->netdev;
549 int err;
550
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700551 snprintf(adapter->misc_vector_name,
Carolyn Wyborny9a21a002015-02-06 08:52:20 +0000552 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
553 dev_name(&adapter->pdev->dev));
Greg Rose5eae00c2013-12-21 06:12:45 +0000554 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800555 &i40evf_msix_aq, 0,
556 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000557 if (err) {
558 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800559 "request_irq for %s failed: %d\n",
560 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000561 free_irq(adapter->msix_entries[0].vector, netdev);
562 }
563 return err;
564}
565
566/**
567 * i40evf_free_traffic_irqs - Free MSI-X interrupts
568 * @adapter: board private structure
569 *
570 * Frees all MSI-X vectors other than 0.
571 **/
572static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
573{
574 int i;
575 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000576
Greg Rose5eae00c2013-12-21 06:12:45 +0000577 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
578
579 for (i = 0; i < q_vectors; i++) {
580 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
581 NULL);
582 free_irq(adapter->msix_entries[i+1].vector,
583 adapter->q_vector[i]);
584 }
585}
586
587/**
588 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
589 * @adapter: board private structure
590 *
591 * Frees MSI-X vector 0.
592 **/
593static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
594{
595 struct net_device *netdev = adapter->netdev;
596
597 free_irq(adapter->msix_entries[0].vector, netdev);
598}
599
600/**
601 * i40evf_configure_tx - Configure Transmit Unit after Reset
602 * @adapter: board private structure
603 *
604 * Configure the Tx unit of the MAC after a reset.
605 **/
606static void i40evf_configure_tx(struct i40evf_adapter *adapter)
607{
608 struct i40e_hw *hw = &adapter->hw;
609 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000610
Mitch Williamscc052922014-10-25 03:24:34 +0000611 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000612 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
613}
614
615/**
616 * i40evf_configure_rx - Configure Receive Unit after Reset
617 * @adapter: board private structure
618 *
619 * Configure the Rx unit of the MAC after a reset.
620 **/
621static void i40evf_configure_rx(struct i40evf_adapter *adapter)
622{
623 struct i40e_hw *hw = &adapter->hw;
624 struct net_device *netdev = adapter->netdev;
625 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
626 int i;
627 int rx_buf_len;
628
629
630 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
631 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
632
633 /* Decide whether to use packet split mode or not */
634 if (netdev->mtu > ETH_DATA_LEN) {
635 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
636 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
637 else
638 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
639 } else {
640 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
641 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
642 else
643 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
644 }
645
646 /* Set the RX buffer length according to the mode */
647 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
648 rx_buf_len = I40E_RX_HDR_SIZE;
649 } else {
650 if (netdev->mtu <= ETH_DATA_LEN)
651 rx_buf_len = I40EVF_RXBUFFER_2048;
652 else
653 rx_buf_len = ALIGN(max_frame, 1024);
654 }
655
Mitch Williamscc052922014-10-25 03:24:34 +0000656 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000657 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
658 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
659 }
660}
661
662/**
663 * i40evf_find_vlan - Search filter list for specific vlan filter
664 * @adapter: board private structure
665 * @vlan: vlan tag
666 *
667 * Returns ptr to the filter object or NULL
668 **/
669static struct
670i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
671{
672 struct i40evf_vlan_filter *f;
673
674 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
675 if (vlan == f->vlan)
676 return f;
677 }
678 return NULL;
679}
680
681/**
682 * i40evf_add_vlan - Add a vlan filter to the list
683 * @adapter: board private structure
684 * @vlan: VLAN tag
685 *
686 * Returns ptr to the filter object or NULL when no memory available.
687 **/
688static struct
689i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
690{
Mitch Williams13acb542015-03-31 00:45:05 -0700691 struct i40evf_vlan_filter *f = NULL;
692 int count = 50;
693
694 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
695 &adapter->crit_section)) {
696 udelay(1);
697 if (--count == 0)
698 goto out;
699 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000700
701 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000702 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000703 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000704 if (!f)
Mitch Williams13acb542015-03-31 00:45:05 -0700705 goto clearout;
Mitch Williams249c8b82014-05-10 04:49:04 +0000706
Greg Rose5eae00c2013-12-21 06:12:45 +0000707 f->vlan = vlan;
708
709 INIT_LIST_HEAD(&f->list);
710 list_add(&f->list, &adapter->vlan_filter_list);
711 f->add = true;
712 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
713 }
714
Mitch Williams13acb542015-03-31 00:45:05 -0700715clearout:
716 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
717out:
Greg Rose5eae00c2013-12-21 06:12:45 +0000718 return f;
719}
720
721/**
722 * i40evf_del_vlan - Remove a vlan filter from the list
723 * @adapter: board private structure
724 * @vlan: VLAN tag
725 **/
726static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
727{
728 struct i40evf_vlan_filter *f;
Mitch Williams13acb542015-03-31 00:45:05 -0700729 int count = 50;
730
731 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
732 &adapter->crit_section)) {
733 udelay(1);
734 if (--count == 0)
735 return;
736 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000737
738 f = i40evf_find_vlan(adapter, vlan);
739 if (f) {
740 f->remove = true;
741 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
742 }
Mitch Williams13acb542015-03-31 00:45:05 -0700743 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +0000744}
745
746/**
747 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
748 * @netdev: network device struct
749 * @vid: VLAN tag
750 **/
751static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000752 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000753{
754 struct i40evf_adapter *adapter = netdev_priv(netdev);
755
Mitch Williams8ed995f2015-08-28 17:55:57 -0400756 if (!VLAN_ALLOWED(adapter))
757 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000758 if (i40evf_add_vlan(adapter, vid) == NULL)
759 return -ENOMEM;
760 return 0;
761}
762
763/**
764 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
765 * @netdev: network device struct
766 * @vid: VLAN tag
767 **/
768static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000769 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000770{
771 struct i40evf_adapter *adapter = netdev_priv(netdev);
772
Mitch Williams8ed995f2015-08-28 17:55:57 -0400773 if (VLAN_ALLOWED(adapter)) {
774 i40evf_del_vlan(adapter, vid);
775 return 0;
776 }
777 return -EIO;
Greg Rose5eae00c2013-12-21 06:12:45 +0000778}
779
780/**
781 * i40evf_find_filter - Search filter list for specific mac filter
782 * @adapter: board private structure
783 * @macaddr: the MAC address
784 *
785 * Returns ptr to the filter object or NULL
786 **/
787static struct
788i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
789 u8 *macaddr)
790{
791 struct i40evf_mac_filter *f;
792
793 if (!macaddr)
794 return NULL;
795
796 list_for_each_entry(f, &adapter->mac_filter_list, list) {
797 if (ether_addr_equal(macaddr, f->macaddr))
798 return f;
799 }
800 return NULL;
801}
802
803/**
804 * i40e_add_filter - Add a mac filter to the filter list
805 * @adapter: board private structure
806 * @macaddr: the MAC address
807 *
808 * Returns ptr to the filter object or NULL when no memory available.
809 **/
810static struct
811i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
812 u8 *macaddr)
813{
814 struct i40evf_mac_filter *f;
Mitch Williams54e16f62015-01-29 07:17:20 +0000815 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000816
817 if (!macaddr)
818 return NULL;
819
820 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000821 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000822 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000823 if (--count == 0)
824 return NULL;
825 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000826
827 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000828 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000829 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000830 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000831 clear_bit(__I40EVF_IN_CRITICAL_TASK,
832 &adapter->crit_section);
833 return NULL;
834 }
835
Greg Rose9a173902014-05-22 06:32:02 +0000836 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000837
838 list_add(&f->list, &adapter->mac_filter_list);
839 f->add = true;
840 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
841 }
842
843 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
844 return f;
845}
846
847/**
848 * i40evf_set_mac - NDO callback to set port mac address
849 * @netdev: network interface device structure
850 * @p: pointer to an address structure
851 *
852 * Returns 0 on success, negative on failure
853 **/
854static int i40evf_set_mac(struct net_device *netdev, void *p)
855{
856 struct i40evf_adapter *adapter = netdev_priv(netdev);
857 struct i40e_hw *hw = &adapter->hw;
858 struct i40evf_mac_filter *f;
859 struct sockaddr *addr = p;
860
861 if (!is_valid_ether_addr(addr->sa_data))
862 return -EADDRNOTAVAIL;
863
864 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
865 return 0;
866
Mitch Williams14e52ee2015-08-31 19:54:44 -0400867 if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
868 return -EPERM;
869
870 f = i40evf_find_filter(adapter, hw->mac.addr);
871 if (f) {
872 f->remove = true;
873 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
874 }
875
Greg Rose5eae00c2013-12-21 06:12:45 +0000876 f = i40evf_add_filter(adapter, addr->sa_data);
877 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000878 ether_addr_copy(hw->mac.addr, addr->sa_data);
879 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000880 }
881
882 return (f == NULL) ? -ENOMEM : 0;
883}
884
885/**
886 * i40evf_set_rx_mode - NDO callback to set the netdev filters
887 * @netdev: network interface device structure
888 **/
889static void i40evf_set_rx_mode(struct net_device *netdev)
890{
891 struct i40evf_adapter *adapter = netdev_priv(netdev);
892 struct i40evf_mac_filter *f, *ftmp;
893 struct netdev_hw_addr *uca;
894 struct netdev_hw_addr *mca;
Shannon Nelson2f41f332015-08-26 15:14:20 -0400895 struct netdev_hw_addr *ha;
Mitch Williams54e16f62015-01-29 07:17:20 +0000896 int count = 50;
Greg Rose5eae00c2013-12-21 06:12:45 +0000897
898 /* add addr if not already in the filter list */
899 netdev_for_each_uc_addr(uca, netdev) {
900 i40evf_add_filter(adapter, uca->addr);
901 }
902 netdev_for_each_mc_addr(mca, netdev) {
903 i40evf_add_filter(adapter, mca->addr);
904 }
905
906 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
Mitch Williams54e16f62015-01-29 07:17:20 +0000907 &adapter->crit_section)) {
Mitch Williamsb65476c2014-07-09 07:46:14 +0000908 udelay(1);
Mitch Williams54e16f62015-01-29 07:17:20 +0000909 if (--count == 0) {
910 dev_err(&adapter->pdev->dev,
911 "Failed to get lock in %s\n", __func__);
912 return;
913 }
914 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000915 /* remove filter if not in netdev list */
916 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
Shannon Nelson2f41f332015-08-26 15:14:20 -0400917 netdev_for_each_mc_addr(mca, netdev)
918 if (ether_addr_equal(mca->addr, f->macaddr))
919 goto bottom_of_search_loop;
Greg Rose5eae00c2013-12-21 06:12:45 +0000920
Shannon Nelson2f41f332015-08-26 15:14:20 -0400921 netdev_for_each_uc_addr(uca, netdev)
922 if (ether_addr_equal(uca->addr, f->macaddr))
923 goto bottom_of_search_loop;
924
925 for_each_dev_addr(netdev, ha)
926 if (ether_addr_equal(ha->addr, f->macaddr))
927 goto bottom_of_search_loop;
928
929 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
930 goto bottom_of_search_loop;
931
932 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
933 f->remove = true;
934 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
935
936bottom_of_search_loop:
937 continue;
Greg Rose5eae00c2013-12-21 06:12:45 +0000938 }
939 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
940}
941
942/**
943 * i40evf_napi_enable_all - enable NAPI on all queue vectors
944 * @adapter: board private structure
945 **/
946static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
947{
948 int q_idx;
949 struct i40e_q_vector *q_vector;
950 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
951
952 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
953 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000954
Greg Rose5eae00c2013-12-21 06:12:45 +0000955 q_vector = adapter->q_vector[q_idx];
956 napi = &q_vector->napi;
957 napi_enable(napi);
958 }
959}
960
961/**
962 * i40evf_napi_disable_all - disable NAPI on all queue vectors
963 * @adapter: board private structure
964 **/
965static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
966{
967 int q_idx;
968 struct i40e_q_vector *q_vector;
969 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
970
971 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
972 q_vector = adapter->q_vector[q_idx];
973 napi_disable(&q_vector->napi);
974 }
975}
976
977/**
978 * i40evf_configure - set up transmit and receive data structures
979 * @adapter: board private structure
980 **/
981static void i40evf_configure(struct i40evf_adapter *adapter)
982{
983 struct net_device *netdev = adapter->netdev;
984 int i;
985
986 i40evf_set_rx_mode(netdev);
987
988 i40evf_configure_tx(adapter);
989 i40evf_configure_rx(adapter);
990 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
991
Mitch Williamscc052922014-10-25 03:24:34 +0000992 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000993 struct i40e_ring *ring = adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000994
Mitch Williamsa132af22015-01-24 09:58:35 +0000995 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
Greg Rose5eae00c2013-12-21 06:12:45 +0000996 ring->next_to_use = ring->count - 1;
997 writel(ring->next_to_use, ring->tail);
998 }
999}
1000
1001/**
1002 * i40evf_up_complete - Finish the last steps of bringing up a connection
1003 * @adapter: board private structure
1004 **/
1005static int i40evf_up_complete(struct i40evf_adapter *adapter)
1006{
1007 adapter->state = __I40EVF_RUNNING;
1008 clear_bit(__I40E_DOWN, &adapter->vsi.state);
1009
1010 i40evf_napi_enable_all(adapter);
1011
1012 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1013 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1014 return 0;
1015}
1016
1017/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001018 * i40e_down - Shutdown the connection processing
1019 * @adapter: board private structure
1020 **/
1021void i40evf_down(struct i40evf_adapter *adapter)
1022{
1023 struct net_device *netdev = adapter->netdev;
1024 struct i40evf_mac_filter *f;
1025
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001026 if (adapter->state == __I40EVF_DOWN)
1027 return;
1028
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001029 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1030 &adapter->crit_section))
1031 usleep_range(500, 1000);
1032
Mitch Williams63e18c22015-03-27 00:12:10 -07001033 netif_carrier_off(netdev);
1034 netif_tx_disable(netdev);
Mitch Williams748c4342015-01-29 07:17:18 +00001035 i40evf_napi_disable_all(adapter);
Mitch Williams63e18c22015-03-27 00:12:10 -07001036 i40evf_irq_disable(adapter);
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001037
Mitch Williamsef8693e2014-02-13 03:48:53 -08001038 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +00001039 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1040 f->remove = true;
1041 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001042 /* remove all VLAN filters */
1043 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1044 f->remove = true;
1045 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001046 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1047 adapter->state != __I40EVF_RESETTING) {
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001048 /* cancel any current operation */
1049 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001050 /* Schedule operations to close down the HW. Don't wait
1051 * here for this to complete. The watchdog is still running
1052 * and it will take care of this.
1053 */
1054 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -08001055 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001056 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001057 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001058
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00001059 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Greg Rose5eae00c2013-12-21 06:12:45 +00001060}
1061
1062/**
1063 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1064 * @adapter: board private structure
1065 * @vectors: number of vectors to request
1066 *
1067 * Work with the OS to set up the MSIX vectors needed.
1068 *
1069 * Returns 0 on success, negative on failure
1070 **/
1071static int
1072i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1073{
1074 int err, vector_threshold;
1075
1076 /* We'll want at least 3 (vector_threshold):
1077 * 0) Other (Admin Queue and link, mostly)
1078 * 1) TxQ[0] Cleanup
1079 * 2) RxQ[0] Cleanup
1080 */
1081 vector_threshold = MIN_MSIX_COUNT;
1082
1083 /* The more we get, the more we will assign to Tx/Rx Cleanup
1084 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1085 * Right now, we simply care about how many we'll get; we'll
1086 * set them up later while requesting irq's.
1087 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001088 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1089 vector_threshold, vectors);
1090 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001091 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001092 kfree(adapter->msix_entries);
1093 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001094 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001095 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001096
1097 /* Adjust for only the vectors we'll use, which is minimum
1098 * of max_msix_q_vectors + NONQ_VECS, or the number of
1099 * vectors we were allocated.
1100 */
1101 adapter->num_msix_vectors = err;
1102 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001103}
1104
1105/**
1106 * i40evf_free_queues - Free memory for all rings
1107 * @adapter: board private structure to initialize
1108 *
1109 * Free all of the memory associated with queue pairs.
1110 **/
1111static void i40evf_free_queues(struct i40evf_adapter *adapter)
1112{
1113 int i;
1114
1115 if (!adapter->vsi_res)
1116 return;
Mitch Williamscc052922014-10-25 03:24:34 +00001117 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001118 if (adapter->tx_rings[i])
1119 kfree_rcu(adapter->tx_rings[i], rcu);
1120 adapter->tx_rings[i] = NULL;
1121 adapter->rx_rings[i] = NULL;
1122 }
1123}
1124
1125/**
1126 * i40evf_alloc_queues - Allocate memory for all rings
1127 * @adapter: board private structure to initialize
1128 *
1129 * We allocate one ring per queue at run-time since we don't know the
1130 * number of queues at compile-time. The polling_netdev array is
1131 * intended for Multiqueue, but should work fine with a single queue.
1132 **/
1133static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1134{
1135 int i;
1136
Mitch Williamscc052922014-10-25 03:24:34 +00001137 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001138 struct i40e_ring *tx_ring;
1139 struct i40e_ring *rx_ring;
1140
Mitch Williams75a64432014-11-11 20:02:42 +00001141 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001142 if (!tx_ring)
1143 goto err_out;
1144
1145 tx_ring->queue_index = i;
1146 tx_ring->netdev = adapter->netdev;
1147 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001148 tx_ring->count = adapter->tx_desc_count;
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04001149 if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
1150 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
Greg Rose5eae00c2013-12-21 06:12:45 +00001151 adapter->tx_rings[i] = tx_ring;
1152
1153 rx_ring = &tx_ring[1];
1154 rx_ring->queue_index = i;
1155 rx_ring->netdev = adapter->netdev;
1156 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001157 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001158 adapter->rx_rings[i] = rx_ring;
1159 }
1160
1161 return 0;
1162
1163err_out:
1164 i40evf_free_queues(adapter);
1165 return -ENOMEM;
1166}
1167
1168/**
1169 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1170 * @adapter: board private structure to initialize
1171 *
1172 * Attempt to configure the interrupts using the best available
1173 * capabilities of the hardware and the kernel.
1174 **/
1175static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1176{
1177 int vector, v_budget;
1178 int pairs = 0;
1179 int err = 0;
1180
1181 if (!adapter->vsi_res) {
1182 err = -EIO;
1183 goto out;
1184 }
Mitch Williamscc052922014-10-25 03:24:34 +00001185 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001186
1187 /* It's easy to be greedy for MSI-X vectors, but it really
1188 * doesn't do us much good if we have a lot more vectors
1189 * than CPU's. So let's be conservative and only ask for
1190 * (roughly) twice the number of vectors as there are CPU's.
1191 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001192 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1193 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001194
Greg Rose5eae00c2013-12-21 06:12:45 +00001195 adapter->msix_entries = kcalloc(v_budget,
1196 sizeof(struct msix_entry), GFP_KERNEL);
1197 if (!adapter->msix_entries) {
1198 err = -ENOMEM;
1199 goto out;
1200 }
1201
1202 for (vector = 0; vector < v_budget; vector++)
1203 adapter->msix_entries[vector].entry = vector;
1204
Mitch Williams313ed2d2015-08-27 11:42:31 -04001205 err = i40evf_acquire_msix_vectors(adapter, v_budget);
Greg Rose5eae00c2013-12-21 06:12:45 +00001206
1207out:
1208 adapter->netdev->real_num_tx_queues = pairs;
1209 return err;
1210}
1211
1212/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001213 * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1214 * @vsi: vsi structure
1215 * @seed: RSS hash seed
1216 **/
1217static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1218{
1219 struct i40e_aqc_get_set_rss_key_data rss_key;
1220 struct i40evf_adapter *adapter = vsi->back;
1221 struct i40e_hw *hw = &adapter->hw;
1222 int ret = 0, i;
1223 u8 *rss_lut;
1224
1225 if (!vsi->id)
1226 return;
1227
1228 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1229 /* bail because we already have a command pending */
1230 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1231 adapter->current_op);
1232 return;
1233 }
1234
1235 memset(&rss_key, 0, sizeof(rss_key));
1236 memcpy(&rss_key, seed, sizeof(rss_key));
1237
1238 rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1239 if (!rss_lut)
1240 return;
1241
1242 /* Populate the LUT with max no. PF queues in round robin fashion */
1243 for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1244 rss_lut[i] = i % adapter->num_active_queues;
1245
1246 ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1247 if (ret) {
1248 dev_err(&adapter->pdev->dev,
1249 "Cannot set RSS key, err %s aq_err %s\n",
1250 i40evf_stat_str(hw, ret),
1251 i40evf_aq_str(hw, hw->aq.asq_last_status));
1252 return;
1253 }
1254
1255 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1256 (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1257 if (ret)
1258 dev_err(&adapter->pdev->dev,
1259 "Cannot set RSS lut, err %s aq_err %s\n",
1260 i40evf_stat_str(hw, ret),
1261 i40evf_aq_str(hw, hw->aq.asq_last_status));
1262}
1263
1264/**
1265 * i40e_configure_rss_reg - Prepare for RSS if used
1266 * @adapter: board private structure
1267 * @seed: RSS hash seed
1268 **/
1269static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1270 const u8 *seed)
1271{
1272 struct i40e_hw *hw = &adapter->hw;
1273 u32 *seed_dw = (u32 *)seed;
1274 u32 cqueue = 0;
1275 u32 lut = 0;
1276 int i, j;
1277
1278 /* Fill out hash function seed */
1279 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1280 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1281
1282 /* Populate the LUT with max no. PF queues in round robin fashion */
1283 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1284 lut = 0;
1285 for (j = 0; j < 4; j++) {
1286 if (cqueue == adapter->num_active_queues)
1287 cqueue = 0;
1288 lut |= ((cqueue) << (8 * j));
1289 cqueue++;
1290 }
1291 wr32(hw, I40E_VFQF_HLUT(i), lut);
1292 }
1293 i40e_flush(hw);
1294}
1295
1296/**
1297 * i40evf_configure_rss - Prepare for RSS
1298 * @adapter: board private structure
1299 **/
1300static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1301{
1302 struct i40e_hw *hw = &adapter->hw;
1303 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1304 u64 hena;
1305
1306 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1307
1308 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1309 hena = I40E_DEFAULT_RSS_HENA;
1310 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1311 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1312
1313 if (RSS_AQ(adapter))
1314 i40evf_configure_rss_aq(&adapter->vsi, seed);
1315 else
1316 i40evf_configure_rss_reg(adapter, seed);
1317}
1318
1319/**
Greg Rose5eae00c2013-12-21 06:12:45 +00001320 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1321 * @adapter: board private structure to initialize
1322 *
1323 * We allocate one q_vector per queue interrupt. If allocation fails we
1324 * return -ENOMEM.
1325 **/
1326static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1327{
1328 int q_idx, num_q_vectors;
1329 struct i40e_q_vector *q_vector;
1330
1331 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1332
1333 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams75a64432014-11-11 20:02:42 +00001334 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001335 if (!q_vector)
1336 goto err_out;
1337 q_vector->adapter = adapter;
1338 q_vector->vsi = &adapter->vsi;
1339 q_vector->v_idx = q_idx;
1340 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001341 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001342 adapter->q_vector[q_idx] = q_vector;
1343 }
1344
1345 return 0;
1346
1347err_out:
1348 while (q_idx) {
1349 q_idx--;
1350 q_vector = adapter->q_vector[q_idx];
1351 netif_napi_del(&q_vector->napi);
1352 kfree(q_vector);
1353 adapter->q_vector[q_idx] = NULL;
1354 }
1355 return -ENOMEM;
1356}
1357
1358/**
1359 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1360 * @adapter: board private structure to initialize
1361 *
1362 * This function frees the memory allocated to the q_vectors. In addition if
1363 * NAPI is enabled it will delete any references to the NAPI struct prior
1364 * to freeing the q_vector.
1365 **/
1366static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1367{
1368 int q_idx, num_q_vectors;
1369 int napi_vectors;
1370
1371 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001372 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001373
1374 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1375 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1376
1377 adapter->q_vector[q_idx] = NULL;
1378 if (q_idx < napi_vectors)
1379 netif_napi_del(&q_vector->napi);
1380 kfree(q_vector);
1381 }
1382}
1383
1384/**
1385 * i40evf_reset_interrupt_capability - Reset MSIX setup
1386 * @adapter: board private structure
1387 *
1388 **/
1389void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1390{
1391 pci_disable_msix(adapter->pdev);
1392 kfree(adapter->msix_entries);
1393 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001394}
1395
1396/**
1397 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1398 * @adapter: board private structure to initialize
1399 *
1400 **/
1401int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1402{
1403 int err;
1404
1405 err = i40evf_set_interrupt_capability(adapter);
1406 if (err) {
1407 dev_err(&adapter->pdev->dev,
1408 "Unable to setup interrupt capabilities\n");
1409 goto err_set_interrupt;
1410 }
1411
1412 err = i40evf_alloc_q_vectors(adapter);
1413 if (err) {
1414 dev_err(&adapter->pdev->dev,
1415 "Unable to allocate memory for queue vectors\n");
1416 goto err_alloc_q_vectors;
1417 }
1418
1419 err = i40evf_alloc_queues(adapter);
1420 if (err) {
1421 dev_err(&adapter->pdev->dev,
1422 "Unable to allocate memory for queues\n");
1423 goto err_alloc_queues;
1424 }
1425
1426 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001427 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1428 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001429
1430 return 0;
1431err_alloc_queues:
1432 i40evf_free_q_vectors(adapter);
1433err_alloc_q_vectors:
1434 i40evf_reset_interrupt_capability(adapter);
1435err_set_interrupt:
1436 return err;
1437}
1438
1439/**
1440 * i40evf_watchdog_timer - Periodic call-back timer
1441 * @data: pointer to adapter disguised as unsigned long
1442 **/
1443static void i40evf_watchdog_timer(unsigned long data)
1444{
1445 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001446
Greg Rose5eae00c2013-12-21 06:12:45 +00001447 schedule_work(&adapter->watchdog_task);
1448 /* timer will be rescheduled in watchdog task */
1449}
1450
1451/**
1452 * i40evf_watchdog_task - Periodic call-back task
1453 * @work: pointer to work_struct
1454 **/
1455static void i40evf_watchdog_task(struct work_struct *work)
1456{
1457 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001458 struct i40evf_adapter,
1459 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001460 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001461 u32 reg_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001462
Greg Rose5eae00c2013-12-21 06:12:45 +00001463 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001464 goto restart_watchdog;
1465
1466 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001467 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1468 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1469 if ((reg_val == I40E_VFR_VFACTIVE) ||
1470 (reg_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001471 /* A chance for redemption! */
1472 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1473 adapter->state = __I40EVF_STARTUP;
1474 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1475 schedule_delayed_work(&adapter->init_task, 10);
1476 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1477 &adapter->crit_section);
1478 /* Don't reschedule the watchdog, since we've restarted
1479 * the init task. When init_task contacts the PF and
1480 * gets everything set up again, it'll restart the
1481 * watchdog for us. Down, boy. Sit. Stay. Woof.
1482 */
1483 return;
1484 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001485 adapter->aq_required = 0;
1486 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1487 goto watchdog_done;
1488 }
1489
1490 if ((adapter->state < __I40EVF_DOWN) ||
1491 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001492 goto watchdog_done;
1493
Mitch Williamsef8693e2014-02-13 03:48:53 -08001494 /* check for reset */
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001495 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1496 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001497 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001498 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001499 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001500 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001501 adapter->aq_required = 0;
1502 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001503 goto watchdog_done;
1504 }
1505
1506 /* Process admin queue tasks. After init, everything gets done
1507 * here so we don't race on the admin queue.
1508 */
Mitch Williamsed636962015-04-07 19:45:32 -04001509 if (adapter->current_op) {
Mitch A Williams0758e7c2014-12-09 08:53:08 +00001510 if (!i40evf_asq_done(hw)) {
1511 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1512 i40evf_send_api_ver(adapter);
1513 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001514 goto watchdog_done;
Mitch A Williams0758e7c2014-12-09 08:53:08 +00001515 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001516 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1517 i40evf_send_vf_config_msg(adapter);
1518 goto watchdog_done;
1519 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001520
Mitch Williamse284fc82015-03-27 00:12:09 -07001521 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1522 i40evf_disable_queues(adapter);
1523 goto watchdog_done;
1524 }
1525
Greg Rose5eae00c2013-12-21 06:12:45 +00001526 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1527 i40evf_map_queues(adapter);
1528 goto watchdog_done;
1529 }
1530
1531 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1532 i40evf_add_ether_addrs(adapter);
1533 goto watchdog_done;
1534 }
1535
1536 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1537 i40evf_add_vlans(adapter);
1538 goto watchdog_done;
1539 }
1540
1541 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1542 i40evf_del_ether_addrs(adapter);
1543 goto watchdog_done;
1544 }
1545
1546 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1547 i40evf_del_vlans(adapter);
1548 goto watchdog_done;
1549 }
1550
Greg Rose5eae00c2013-12-21 06:12:45 +00001551 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1552 i40evf_configure_queues(adapter);
1553 goto watchdog_done;
1554 }
1555
1556 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1557 i40evf_enable_queues(adapter);
1558 goto watchdog_done;
1559 }
1560
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001561 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1562 /* This message goes straight to the firmware, not the
1563 * PF, so we don't have to set current_op as we will
1564 * not get a response through the ARQ.
1565 */
1566 i40evf_configure_rss(adapter);
1567 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1568 goto watchdog_done;
1569 }
1570
Greg Rose5eae00c2013-12-21 06:12:45 +00001571 if (adapter->state == __I40EVF_RUNNING)
1572 i40evf_request_stats(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001573watchdog_done:
Mitch A Williams4870e172014-12-09 08:53:06 +00001574 if (adapter->state == __I40EVF_RUNNING) {
1575 i40evf_irq_enable_queues(adapter, ~0);
1576 i40evf_fire_sw_int(adapter, 0xFF);
1577 } else {
1578 i40evf_fire_sw_int(adapter, 0x1);
1579 }
1580
Mitch Williamsef8693e2014-02-13 03:48:53 -08001581 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1582restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001583 if (adapter->state == __I40EVF_REMOVE)
1584 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001585 if (adapter->aq_required)
1586 mod_timer(&adapter->watchdog_timer,
1587 jiffies + msecs_to_jiffies(20));
1588 else
1589 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001590 schedule_work(&adapter->adminq_task);
1591}
1592
Mitch Williams67c818a2015-06-19 08:56:30 -07001593#define I40EVF_RESET_WAIT_MS 10
1594#define I40EVF_RESET_WAIT_COUNT 500
Greg Rose5eae00c2013-12-21 06:12:45 +00001595/**
1596 * i40evf_reset_task - Call-back task to handle hardware reset
1597 * @work: pointer to work_struct
1598 *
1599 * During reset we need to shut down and reinitialize the admin queue
1600 * before we can use it to communicate with the PF again. We also clear
1601 * and reinit the rings because that context is lost as well.
1602 **/
1603static void i40evf_reset_task(struct work_struct *work)
1604{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001605 struct i40evf_adapter *adapter = container_of(work,
1606 struct i40evf_adapter,
1607 reset_task);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001608 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00001609 struct i40e_hw *hw = &adapter->hw;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001610 struct i40evf_mac_filter *f;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001611 u32 reg_val;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001612 int i = 0, err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001613
1614 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1615 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001616 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001617
Mitch Williams67c818a2015-06-19 08:56:30 -07001618 i40evf_misc_irq_disable(adapter);
Mitch Williams3526d802014-03-06 08:59:56 +00001619 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001620 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1621 /* Restart the AQ here. If we have been reset but didn't
1622 * detect it, or if the PF had to reinit, our AQ will be hosed.
1623 */
1624 i40evf_shutdown_adminq(hw);
1625 i40evf_init_adminq(hw);
Mitch Williams3526d802014-03-06 08:59:56 +00001626 i40evf_request_reset(adapter);
1627 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001628 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams3526d802014-03-06 08:59:56 +00001629
Mitch Williamsef8693e2014-02-13 03:48:53 -08001630 /* poll until we see the reset actually happen */
1631 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001632 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1633 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1634 if (!reg_val)
Greg Rose5eae00c2013-12-21 06:12:45 +00001635 break;
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001636 usleep_range(5000, 10000);
Greg Rose5eae00c2013-12-21 06:12:45 +00001637 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001638 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams67c818a2015-06-19 08:56:30 -07001639 dev_info(&adapter->pdev->dev, "Never saw reset\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001640 goto continue_reset; /* act like the reset happened */
1641 }
1642
1643 /* wait until the reset is complete and the PF is responding to us */
1644 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001645 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1646 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1647 if (reg_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001648 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001649 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001650 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001651 /* extra wait to make sure minimum wait is met */
1652 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001653 if (i == I40EVF_RESET_WAIT_COUNT) {
Shannon Nelson874d1a12015-09-03 17:18:57 -04001654 struct i40evf_mac_filter *ftmp;
Mitch Williams6ba36a22014-08-01 13:27:14 -07001655 struct i40evf_vlan_filter *fv, *fvtmp;
1656
Greg Rose5eae00c2013-12-21 06:12:45 +00001657 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001658 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsee5c1e92015-08-28 17:55:53 -04001659 reg_val);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001660 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1661
Mitch Williams169f4072014-04-01 07:11:50 +00001662 if (netif_running(adapter->netdev)) {
1663 set_bit(__I40E_DOWN, &adapter->vsi.state);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001664 netif_carrier_off(netdev);
Mitch Williams67c818a2015-06-19 08:56:30 -07001665 netif_tx_disable(netdev);
1666 i40evf_napi_disable_all(adapter);
1667 i40evf_irq_disable(adapter);
Mitch Williams169f4072014-04-01 07:11:50 +00001668 i40evf_free_traffic_irqs(adapter);
1669 i40evf_free_all_tx_resources(adapter);
1670 i40evf_free_all_rx_resources(adapter);
1671 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001672
1673 /* Delete all of the filters, both MAC and VLAN. */
1674 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1675 list) {
1676 list_del(&f->list);
1677 kfree(f);
1678 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001679
Mitch Williams6ba36a22014-08-01 13:27:14 -07001680 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1681 list) {
1682 list_del(&fv->list);
1683 kfree(fv);
1684 }
1685
Mitch Williamsef8693e2014-02-13 03:48:53 -08001686 i40evf_free_misc_irq(adapter);
1687 i40evf_reset_interrupt_capability(adapter);
1688 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001689 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001690 kfree(adapter->vf_res);
1691 i40evf_shutdown_adminq(hw);
1692 adapter->netdev->flags &= ~IFF_UP;
1693 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001694 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1695 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08001696 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001697 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001698
1699continue_reset:
Mitch Williams67c818a2015-06-19 08:56:30 -07001700 if (netif_running(adapter->netdev)) {
1701 netif_carrier_off(netdev);
1702 netif_tx_stop_all_queues(netdev);
1703 i40evf_napi_disable_all(adapter);
1704 }
Mitch Williamsac833bb2015-01-29 07:17:19 +00001705 i40evf_irq_disable(adapter);
Mitch Williamsac833bb2015-01-29 07:17:19 +00001706
Greg Rose5eae00c2013-12-21 06:12:45 +00001707 adapter->state = __I40EVF_RESETTING;
Mitch Williams67c818a2015-06-19 08:56:30 -07001708 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1709
1710 /* free the Tx/Rx rings and descriptors, might be better to just
1711 * re-use them sometime in the future
1712 */
1713 i40evf_free_all_rx_resources(adapter);
1714 i40evf_free_all_tx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001715
1716 /* kill and reinit the admin queue */
1717 if (i40evf_shutdown_adminq(hw))
Mitch Williamsac833bb2015-01-29 07:17:19 +00001718 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1719 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001720 err = i40evf_init_adminq(hw);
1721 if (err)
Mitch Williamsac833bb2015-01-29 07:17:19 +00001722 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1723 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001724
Mitch Williamse6d038d2015-06-04 16:23:58 -04001725 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1726 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001727
1728 /* re-add all MAC filters */
1729 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1730 f->add = true;
1731 }
1732 /* re-add all VLAN filters */
1733 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1734 f->add = true;
1735 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04001736 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Mitch Williamsac833bb2015-01-29 07:17:19 +00001737 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Greg Rose5eae00c2013-12-21 06:12:45 +00001738 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
Mitch Williams67c818a2015-06-19 08:56:30 -07001739 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001740
1741 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1742
1743 if (netif_running(adapter->netdev)) {
1744 /* allocate transmit descriptors */
1745 err = i40evf_setup_all_tx_resources(adapter);
1746 if (err)
1747 goto reset_err;
1748
1749 /* allocate receive descriptors */
1750 err = i40evf_setup_all_rx_resources(adapter);
1751 if (err)
1752 goto reset_err;
1753
1754 i40evf_configure(adapter);
1755
1756 err = i40evf_up_complete(adapter);
1757 if (err)
1758 goto reset_err;
1759
1760 i40evf_irq_enable(adapter, true);
Mitch Williams67c818a2015-06-19 08:56:30 -07001761 } else {
1762 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001763 }
Mitch Williams67c818a2015-06-19 08:56:30 -07001764
Greg Rose5eae00c2013-12-21 06:12:45 +00001765 return;
1766reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001767 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001768 i40evf_close(adapter->netdev);
1769}
1770
1771/**
1772 * i40evf_adminq_task - worker thread to clean the admin queue
1773 * @work: pointer to work_struct containing our data
1774 **/
1775static void i40evf_adminq_task(struct work_struct *work)
1776{
1777 struct i40evf_adapter *adapter =
1778 container_of(work, struct i40evf_adapter, adminq_task);
1779 struct i40e_hw *hw = &adapter->hw;
1780 struct i40e_arq_event_info event;
1781 struct i40e_virtchnl_msg *v_msg;
1782 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001783 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001784 u16 pending;
1785
Mitch Williamsef8693e2014-02-13 03:48:53 -08001786 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
Mitch A Williams72354482014-12-09 08:53:07 +00001787 goto out;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001788
Mitch Williams1001dc32014-11-11 20:02:19 +00001789 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1790 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001791 if (!event.msg_buf)
Mitch A Williams72354482014-12-09 08:53:07 +00001792 goto out;
Mitch Williams249c8b82014-05-10 04:49:04 +00001793
Greg Rose5eae00c2013-12-21 06:12:45 +00001794 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1795 do {
1796 ret = i40evf_clean_arq_element(hw, &event, &pending);
Mitch Williams8b011eb2015-01-09 11:18:17 +00001797 if (ret || !v_msg->v_opcode)
Greg Rose5eae00c2013-12-21 06:12:45 +00001798 break; /* No event to process or error cleaning ARQ */
1799
1800 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1801 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001802 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001803 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001804 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001805 } while (pending);
1806
Mitch Williams67c818a2015-06-19 08:56:30 -07001807 if ((adapter->flags &
1808 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1809 adapter->state == __I40EVF_RESETTING)
1810 goto freedom;
1811
Mitch Williams912257e2014-05-22 06:32:07 +00001812 /* check for error indications */
1813 val = rd32(hw, hw->aq.arq.len);
1814 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001815 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001816 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001817 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001818 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001819 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001820 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001821 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001822 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001823 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001824 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001825 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001826 }
1827 if (oldval != val)
1828 wr32(hw, hw->aq.arq.len, val);
1829
1830 val = rd32(hw, hw->aq.asq.len);
1831 oldval = val;
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001832 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001833 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001834 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001835 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001836 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001837 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001838 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001839 }
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001840 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
Mitch Williams912257e2014-05-22 06:32:07 +00001841 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
Anjali Singhai Jainb1f33662015-07-10 19:36:05 -04001842 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
Mitch Williams912257e2014-05-22 06:32:07 +00001843 }
1844 if (oldval != val)
1845 wr32(hw, hw->aq.asq.len, val);
1846
Mitch Williams67c818a2015-06-19 08:56:30 -07001847freedom:
Mitch A Williams72354482014-12-09 08:53:07 +00001848 kfree(event.msg_buf);
1849out:
Greg Rose5eae00c2013-12-21 06:12:45 +00001850 /* re-enable Admin queue interrupt cause */
1851 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00001852}
1853
1854/**
1855 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1856 * @adapter: board private structure
1857 *
1858 * Free all transmit software resources
1859 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001860void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001861{
1862 int i;
1863
Mitch Williamscc052922014-10-25 03:24:34 +00001864 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001865 if (adapter->tx_rings[i]->desc)
1866 i40evf_free_tx_resources(adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001867}
1868
1869/**
1870 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1871 * @adapter: board private structure
1872 *
1873 * If this function returns with an error, then it's possible one or
1874 * more of the rings is populated (while the rest are not). It is the
1875 * callers duty to clean those orphaned rings.
1876 *
1877 * Return 0 on success, negative on failure
1878 **/
1879static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1880{
1881 int i, err = 0;
1882
Mitch Williamscc052922014-10-25 03:24:34 +00001883 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001884 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001885 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1886 if (!err)
1887 continue;
1888 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001889 "Allocation for Tx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001890 break;
1891 }
1892
1893 return err;
1894}
1895
1896/**
1897 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1898 * @adapter: board private structure
1899 *
1900 * If this function returns with an error, then it's possible one or
1901 * more of the rings is populated (while the rest are not). It is the
1902 * callers duty to clean those orphaned rings.
1903 *
1904 * Return 0 on success, negative on failure
1905 **/
1906static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1907{
1908 int i, err = 0;
1909
Mitch Williamscc052922014-10-25 03:24:34 +00001910 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001911 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001912 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1913 if (!err)
1914 continue;
1915 dev_err(&adapter->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04001916 "Allocation for Rx Queue %u failed\n", i);
Greg Rose5eae00c2013-12-21 06:12:45 +00001917 break;
1918 }
1919 return err;
1920}
1921
1922/**
1923 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1924 * @adapter: board private structure
1925 *
1926 * Free all receive software resources
1927 **/
Mitch Williamse284fc82015-03-27 00:12:09 -07001928void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
Greg Rose5eae00c2013-12-21 06:12:45 +00001929{
1930 int i;
1931
Mitch Williamscc052922014-10-25 03:24:34 +00001932 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001933 if (adapter->rx_rings[i]->desc)
1934 i40evf_free_rx_resources(adapter->rx_rings[i]);
1935}
1936
1937/**
1938 * i40evf_open - Called when a network interface is made active
1939 * @netdev: network interface device structure
1940 *
1941 * Returns 0 on success, negative value on failure
1942 *
1943 * The open entry point is called when a network interface is made
1944 * active by the system (IFF_UP). At this point all resources needed
1945 * for transmit and receive operations are allocated, the interrupt
1946 * handler is registered with the OS, the watchdog timer is started,
1947 * and the stack is notified that the interface is ready.
1948 **/
1949static int i40evf_open(struct net_device *netdev)
1950{
1951 struct i40evf_adapter *adapter = netdev_priv(netdev);
1952 int err;
1953
Mitch Williamsef8693e2014-02-13 03:48:53 -08001954 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1955 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1956 return -EIO;
1957 }
Mitch Williamse284fc82015-03-27 00:12:09 -07001958 if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
Greg Rose5eae00c2013-12-21 06:12:45 +00001959 return -EBUSY;
1960
1961 /* allocate transmit descriptors */
1962 err = i40evf_setup_all_tx_resources(adapter);
1963 if (err)
1964 goto err_setup_tx;
1965
1966 /* allocate receive descriptors */
1967 err = i40evf_setup_all_rx_resources(adapter);
1968 if (err)
1969 goto err_setup_rx;
1970
1971 /* clear any pending interrupts, may auto mask */
1972 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1973 if (err)
1974 goto err_req_irq;
1975
Mitch Williams44151cd2015-04-27 14:57:17 -04001976 i40evf_add_filter(adapter, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00001977 i40evf_configure(adapter);
1978
1979 err = i40evf_up_complete(adapter);
1980 if (err)
1981 goto err_req_irq;
1982
1983 i40evf_irq_enable(adapter, true);
1984
1985 return 0;
1986
1987err_req_irq:
1988 i40evf_down(adapter);
1989 i40evf_free_traffic_irqs(adapter);
1990err_setup_rx:
1991 i40evf_free_all_rx_resources(adapter);
1992err_setup_tx:
1993 i40evf_free_all_tx_resources(adapter);
1994
1995 return err;
1996}
1997
1998/**
1999 * i40evf_close - Disables a network interface
2000 * @netdev: network interface device structure
2001 *
2002 * Returns 0, this is not allowed to fail
2003 *
2004 * The close entry point is called when an interface is de-activated
2005 * by the OS. The hardware is still under the drivers control, but
2006 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2007 * are freed, along with all transmit and receive resources.
2008 **/
2009static int i40evf_close(struct net_device *netdev)
2010{
2011 struct i40evf_adapter *adapter = netdev_priv(netdev);
2012
Mitch Williamsef8693e2014-02-13 03:48:53 -08002013 if (adapter->state <= __I40EVF_DOWN)
2014 return 0;
2015
Mitch Williamsef8693e2014-02-13 03:48:53 -08002016
Greg Rose5eae00c2013-12-21 06:12:45 +00002017 set_bit(__I40E_DOWN, &adapter->vsi.state);
2018
2019 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00002020 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002021 i40evf_free_traffic_irqs(adapter);
2022
Greg Rose5eae00c2013-12-21 06:12:45 +00002023 return 0;
2024}
2025
2026/**
2027 * i40evf_get_stats - Get System Network Statistics
2028 * @netdev: network interface device structure
2029 *
2030 * Returns the address of the device statistics structure.
2031 * The statistics are actually updated from the timer callback.
2032 **/
2033static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2034{
2035 struct i40evf_adapter *adapter = netdev_priv(netdev);
2036
2037 /* only return the current stats */
2038 return &adapter->net_stats;
2039}
2040
2041/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002042 * i40evf_change_mtu - Change the Maximum Transfer Unit
2043 * @netdev: network interface device structure
2044 * @new_mtu: new value for maximum frame size
2045 *
2046 * Returns 0 on success, negative on failure
2047 **/
2048static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2049{
2050 struct i40evf_adapter *adapter = netdev_priv(netdev);
2051 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2052
2053 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2054 return -EINVAL;
2055
Greg Rose5eae00c2013-12-21 06:12:45 +00002056 netdev->mtu = new_mtu;
Mitch Williams67c818a2015-06-19 08:56:30 -07002057 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2058 schedule_work(&adapter->reset_task);
2059
Greg Rose5eae00c2013-12-21 06:12:45 +00002060 return 0;
2061}
2062
2063static const struct net_device_ops i40evf_netdev_ops = {
2064 .ndo_open = i40evf_open,
2065 .ndo_stop = i40evf_close,
2066 .ndo_start_xmit = i40evf_xmit_frame,
2067 .ndo_get_stats = i40evf_get_stats,
2068 .ndo_set_rx_mode = i40evf_set_rx_mode,
2069 .ndo_validate_addr = eth_validate_addr,
2070 .ndo_set_mac_address = i40evf_set_mac,
2071 .ndo_change_mtu = i40evf_change_mtu,
2072 .ndo_tx_timeout = i40evf_tx_timeout,
2073 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2074 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
Alexander Duyck7709b4c2015-09-24 09:04:38 -07002075#ifdef CONFIG_NET_POLL_CONTROLLER
2076 .ndo_poll_controller = i40evf_netpoll,
2077#endif
Greg Rose5eae00c2013-12-21 06:12:45 +00002078};
2079
2080/**
2081 * i40evf_check_reset_complete - check that VF reset is complete
2082 * @hw: pointer to hw struct
2083 *
2084 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2085 **/
2086static int i40evf_check_reset_complete(struct i40e_hw *hw)
2087{
2088 u32 rstat;
2089 int i;
2090
2091 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07002092 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2093 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2094 if ((rstat == I40E_VFR_VFACTIVE) ||
2095 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00002096 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00002097 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00002098 }
2099 return -EBUSY;
2100}
2101
2102/**
Mitch Williamse6d038d2015-06-04 16:23:58 -04002103 * i40evf_process_config - Process the config information we got from the PF
2104 * @adapter: board private structure
2105 *
2106 * Verify that we have a valid config struct, and set up our netdev features
2107 * and our VSI struct.
2108 **/
2109int i40evf_process_config(struct i40evf_adapter *adapter)
2110{
2111 struct net_device *netdev = adapter->netdev;
2112 int i;
2113
2114 /* got VF config message back from PF, now we can parse it */
2115 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2116 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2117 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2118 }
2119 if (!adapter->vsi_res) {
2120 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2121 return -ENODEV;
2122 }
2123
2124 if (adapter->vf_res->vf_offload_flags
2125 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
Mitch Williamscc7e4062015-09-28 14:12:40 -04002126 netdev->vlan_features = netdev->features &
2127 ~(NETIF_F_HW_VLAN_CTAG_TX |
2128 NETIF_F_HW_VLAN_CTAG_RX |
2129 NETIF_F_HW_VLAN_CTAG_FILTER);
Mitch Williamse6d038d2015-06-04 16:23:58 -04002130 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2131 NETIF_F_HW_VLAN_CTAG_RX |
2132 NETIF_F_HW_VLAN_CTAG_FILTER;
2133 }
2134 netdev->features |= NETIF_F_HIGHDMA |
2135 NETIF_F_SG |
2136 NETIF_F_IP_CSUM |
2137 NETIF_F_SCTP_CSUM |
2138 NETIF_F_IPV6_CSUM |
2139 NETIF_F_TSO |
2140 NETIF_F_TSO6 |
2141 NETIF_F_RXCSUM |
2142 NETIF_F_GRO;
2143
2144 /* copy netdev features into list of user selectable features */
2145 netdev->hw_features |= netdev->features;
2146 netdev->hw_features &= ~NETIF_F_RXCSUM;
2147
2148 adapter->vsi.id = adapter->vsi_res->vsi_id;
2149
2150 adapter->vsi.back = adapter;
2151 adapter->vsi.base_vector = 1;
2152 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2153 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2154 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2155 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2156 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2157 adapter->vsi.netdev = adapter->netdev;
Mitch Williamsf578f5f2015-08-28 17:55:58 -04002158 adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002159 return 0;
2160}
2161
2162/**
Greg Rose5eae00c2013-12-21 06:12:45 +00002163 * i40evf_init_task - worker thread to perform delayed initialization
2164 * @work: pointer to work_struct containing our data
2165 *
2166 * This task completes the work that was begun in probe. Due to the nature
2167 * of VF-PF communications, we may need to wait tens of milliseconds to get
Joe Perchesdbedd442015-03-06 20:49:12 -08002168 * responses back from the PF. Rather than busy-wait in probe and bog down the
Greg Rose5eae00c2013-12-21 06:12:45 +00002169 * whole system, we'll do it in a task so we can sleep.
2170 * This task only runs during driver init. Once we've established
2171 * communications with the PF driver and set up our netdev, the watchdog
2172 * takes over.
2173 **/
2174static void i40evf_init_task(struct work_struct *work)
2175{
2176 struct i40evf_adapter *adapter = container_of(work,
2177 struct i40evf_adapter,
2178 init_task.work);
2179 struct net_device *netdev = adapter->netdev;
Greg Rose5eae00c2013-12-21 06:12:45 +00002180 struct i40e_hw *hw = &adapter->hw;
2181 struct pci_dev *pdev = adapter->pdev;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002182 int err, bufsz;
Greg Rose5eae00c2013-12-21 06:12:45 +00002183
2184 switch (adapter->state) {
2185 case __I40EVF_STARTUP:
2186 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002187 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2188 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002189 err = i40e_set_mac_type(hw);
2190 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002191 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2192 err);
Mitch Williams2619ef42015-04-07 19:45:30 -04002193 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002194 }
2195 err = i40evf_check_reset_complete(hw);
2196 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002197 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002198 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002199 goto err;
2200 }
2201 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2202 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2203 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2204 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2205
2206 err = i40evf_init_adminq(hw);
2207 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002208 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2209 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002210 goto err;
2211 }
2212 err = i40evf_send_api_ver(adapter);
2213 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002214 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002215 i40evf_shutdown_adminq(hw);
2216 goto err;
2217 }
2218 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2219 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002220 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002221 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002222 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002223 i40evf_shutdown_adminq(hw);
2224 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002225 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002226 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002227
2228 /* aq msg sent, awaiting reply */
2229 err = i40evf_verify_api_ver(adapter);
2230 if (err) {
Mitch A Williamsd4f82fd2014-12-09 08:53:03 +00002231 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Mitch Williams56f99202014-06-04 04:22:41 +00002232 err = i40evf_send_api_ver(adapter);
Mitch Williamsee1693e2015-06-04 16:23:59 -04002233 else
2234 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2235 adapter->pf_version.major,
2236 adapter->pf_version.minor,
2237 I40E_VIRTCHNL_VERSION_MAJOR,
2238 I40E_VIRTCHNL_VERSION_MINOR);
Greg Rose5eae00c2013-12-21 06:12:45 +00002239 goto err;
2240 }
2241 err = i40evf_send_vf_config_msg(adapter);
2242 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002243 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002244 err);
2245 goto err;
2246 }
2247 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2248 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002249 case __I40EVF_INIT_GET_RESOURCES:
2250 /* aq msg sent, awaiting reply */
2251 if (!adapter->vf_res) {
2252 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2253 (I40E_MAX_VF_VSI *
2254 sizeof(struct i40e_virtchnl_vsi_resource));
2255 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002256 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002257 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002258 }
2259 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002260 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
Mitch Williams906a6932014-11-13 03:06:12 +00002261 err = i40evf_send_vf_config_msg(adapter);
2262 goto err;
2263 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002264 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002265 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2266 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002267 goto err_alloc;
2268 }
2269 adapter->state = __I40EVF_INIT_SW;
2270 break;
2271 default:
2272 goto err_alloc;
2273 }
Mitch Williamse6d038d2015-06-04 16:23:58 -04002274 if (i40evf_process_config(adapter))
Greg Rose5eae00c2013-12-21 06:12:45 +00002275 goto err_alloc;
Mitch Williamse6d038d2015-06-04 16:23:58 -04002276 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00002277
2278 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2279
Greg Rose5eae00c2013-12-21 06:12:45 +00002280 netdev->netdev_ops = &i40evf_netdev_ops;
2281 i40evf_set_ethtool_ops(netdev);
2282 netdev->watchdog_timeo = 5 * HZ;
Greg Rose3415e8c2014-01-30 12:40:27 +00002283
Greg Rose5eae00c2013-12-21 06:12:45 +00002284 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002285 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002286 adapter->hw.mac.addr);
Mitch Williams14e52ee2015-08-31 19:54:44 -04002287 eth_hw_addr_random(netdev);
2288 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2289 } else {
2290 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2291 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2292 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002293 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002294
Greg Rose5eae00c2013-12-21 06:12:45 +00002295 init_timer(&adapter->watchdog_timer);
2296 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2297 adapter->watchdog_timer.data = (unsigned long)adapter;
2298 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2299
Mitch Williamscc052922014-10-25 03:24:34 +00002300 adapter->num_active_queues = min_t(int,
2301 adapter->vsi_res->num_queue_pairs,
2302 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002303 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2304 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002305 err = i40evf_init_interrupt_scheme(adapter);
2306 if (err)
2307 goto err_sw_init;
2308 i40evf_map_rings_to_vectors(adapter);
Anjali Singhai Jain1f012272015-09-03 17:18:53 -04002309 if (adapter->vf_res->vf_offload_flags &
2310 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2311 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04002312 if (!RSS_AQ(adapter))
2313 i40evf_configure_rss(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002314 err = i40evf_request_misc_irq(adapter);
2315 if (err)
2316 goto err_sw_init;
2317
2318 netif_carrier_off(netdev);
2319
Mitch Williamsef8693e2014-02-13 03:48:53 -08002320 if (!adapter->netdev_registered) {
2321 err = register_netdev(netdev);
2322 if (err)
2323 goto err_register;
2324 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002325
2326 adapter->netdev_registered = true;
2327
2328 netif_tx_stop_all_queues(netdev);
2329
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002330 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002331 if (netdev->features & NETIF_F_GRO)
2332 dev_info(&pdev->dev, "GRO is enabled\n");
2333
2334 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2335 adapter->state = __I40EVF_DOWN;
2336 set_bit(__I40E_DOWN, &adapter->vsi.state);
2337 i40evf_misc_irq_enable(adapter);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04002338
2339 if (RSS_AQ(adapter)) {
2340 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2341 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2342 } else {
2343 i40evf_configure_rss(adapter);
2344 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002345 return;
2346restart:
Mitch Williams5be83082015-09-03 17:19:01 -04002347 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(20));
Greg Rose5eae00c2013-12-21 06:12:45 +00002348 return;
2349
2350err_register:
2351 i40evf_free_misc_irq(adapter);
2352err_sw_init:
2353 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002354err_alloc:
2355 kfree(adapter->vf_res);
2356 adapter->vf_res = NULL;
2357err:
2358 /* Things went into the weeds, so try again later */
2359 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002360 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002361 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002362 return; /* do not reschedule */
2363 }
Mitch Williams5be83082015-09-03 17:19:01 -04002364 schedule_delayed_work(&adapter->init_task, HZ / 2);
Greg Rose5eae00c2013-12-21 06:12:45 +00002365}
2366
2367/**
2368 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2369 * @pdev: pci device structure
2370 **/
2371static void i40evf_shutdown(struct pci_dev *pdev)
2372{
2373 struct net_device *netdev = pci_get_drvdata(pdev);
Mitch Williams00293fd2015-01-09 11:18:18 +00002374 struct i40evf_adapter *adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002375
2376 netif_device_detach(netdev);
2377
2378 if (netif_running(netdev))
2379 i40evf_close(netdev);
2380
Mitch Williams00293fd2015-01-09 11:18:18 +00002381 /* Prevent the watchdog from running. */
2382 adapter->state = __I40EVF_REMOVE;
2383 adapter->aq_required = 0;
Mitch Williams00293fd2015-01-09 11:18:18 +00002384
Greg Rose5eae00c2013-12-21 06:12:45 +00002385#ifdef CONFIG_PM
2386 pci_save_state(pdev);
2387
2388#endif
2389 pci_disable_device(pdev);
2390}
2391
2392/**
2393 * i40evf_probe - Device Initialization Routine
2394 * @pdev: PCI device information struct
2395 * @ent: entry in i40evf_pci_tbl
2396 *
2397 * Returns 0 on success, negative on failure
2398 *
2399 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2400 * The OS initialization, configuring of the adapter private structure,
2401 * and a hardware reset occur.
2402 **/
2403static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2404{
2405 struct net_device *netdev;
2406 struct i40evf_adapter *adapter = NULL;
2407 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002408 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002409
2410 err = pci_enable_device(pdev);
2411 if (err)
2412 return err;
2413
Mitch Williams64942942014-02-11 08:26:33 +00002414 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002415 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002416 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2417 if (err) {
2418 dev_err(&pdev->dev,
2419 "DMA configuration failed: 0x%x\n", err);
2420 goto err_dma;
2421 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002422 }
2423
2424 err = pci_request_regions(pdev, i40evf_driver_name);
2425 if (err) {
2426 dev_err(&pdev->dev,
2427 "pci_request_regions failed 0x%x\n", err);
2428 goto err_pci_reg;
2429 }
2430
2431 pci_enable_pcie_error_reporting(pdev);
2432
2433 pci_set_master(pdev);
2434
2435 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2436 MAX_TX_QUEUES);
2437 if (!netdev) {
2438 err = -ENOMEM;
2439 goto err_alloc_etherdev;
2440 }
2441
2442 SET_NETDEV_DEV(netdev, &pdev->dev);
2443
2444 pci_set_drvdata(pdev, netdev);
2445 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002446
2447 adapter->netdev = netdev;
2448 adapter->pdev = pdev;
2449
2450 hw = &adapter->hw;
2451 hw->back = adapter;
2452
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002453 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Greg Rose5eae00c2013-12-21 06:12:45 +00002454 adapter->state = __I40EVF_STARTUP;
2455
2456 /* Call save state here because it relies on the adapter struct. */
2457 pci_save_state(pdev);
2458
2459 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2460 pci_resource_len(pdev, 0));
2461 if (!hw->hw_addr) {
2462 err = -EIO;
2463 goto err_ioremap;
2464 }
2465 hw->vendor_id = pdev->vendor;
2466 hw->device_id = pdev->device;
2467 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2468 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2469 hw->subsystem_device_id = pdev->subsystem_device;
2470 hw->bus.device = PCI_SLOT(pdev->devfn);
2471 hw->bus.func = PCI_FUNC(pdev->devfn);
2472
Serey Kong8bb1a542014-08-01 13:27:15 -07002473 INIT_LIST_HEAD(&adapter->mac_filter_list);
2474 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2475
Greg Rose5eae00c2013-12-21 06:12:45 +00002476 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2477 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2478 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2479 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
Mitch Williams9b32b0b2015-08-13 15:11:32 -07002480 schedule_delayed_work(&adapter->init_task,
2481 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
Greg Rose5eae00c2013-12-21 06:12:45 +00002482
2483 return 0;
2484
2485err_ioremap:
2486 free_netdev(netdev);
2487err_alloc_etherdev:
2488 pci_release_regions(pdev);
2489err_pci_reg:
2490err_dma:
2491 pci_disable_device(pdev);
2492 return err;
2493}
2494
2495#ifdef CONFIG_PM
2496/**
2497 * i40evf_suspend - Power management suspend routine
2498 * @pdev: PCI device information struct
2499 * @state: unused
2500 *
2501 * Called when the system (VM) is entering sleep/suspend.
2502 **/
2503static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2504{
2505 struct net_device *netdev = pci_get_drvdata(pdev);
2506 struct i40evf_adapter *adapter = netdev_priv(netdev);
2507 int retval = 0;
2508
2509 netif_device_detach(netdev);
2510
2511 if (netif_running(netdev)) {
2512 rtnl_lock();
2513 i40evf_down(adapter);
2514 rtnl_unlock();
2515 }
2516 i40evf_free_misc_irq(adapter);
2517 i40evf_reset_interrupt_capability(adapter);
2518
2519 retval = pci_save_state(pdev);
2520 if (retval)
2521 return retval;
2522
2523 pci_disable_device(pdev);
2524
2525 return 0;
2526}
2527
2528/**
Joe Perchesdbedd442015-03-06 20:49:12 -08002529 * i40evf_resume - Power management resume routine
Greg Rose5eae00c2013-12-21 06:12:45 +00002530 * @pdev: PCI device information struct
2531 *
2532 * Called when the system (VM) is resumed from sleep/suspend.
2533 **/
2534static int i40evf_resume(struct pci_dev *pdev)
2535{
2536 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2537 struct net_device *netdev = adapter->netdev;
2538 u32 err;
2539
2540 pci_set_power_state(pdev, PCI_D0);
2541 pci_restore_state(pdev);
2542 /* pci_restore_state clears dev->state_saved so call
2543 * pci_save_state to restore it.
2544 */
2545 pci_save_state(pdev);
2546
2547 err = pci_enable_device_mem(pdev);
2548 if (err) {
2549 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2550 return err;
2551 }
2552 pci_set_master(pdev);
2553
2554 rtnl_lock();
2555 err = i40evf_set_interrupt_capability(adapter);
2556 if (err) {
Vasily Averinf2a1c362015-07-07 18:53:38 +03002557 rtnl_unlock();
Greg Rose5eae00c2013-12-21 06:12:45 +00002558 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2559 return err;
2560 }
2561 err = i40evf_request_misc_irq(adapter);
2562 rtnl_unlock();
2563 if (err) {
2564 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2565 return err;
2566 }
2567
2568 schedule_work(&adapter->reset_task);
2569
2570 netif_device_attach(netdev);
2571
2572 return err;
2573}
2574
2575#endif /* CONFIG_PM */
2576/**
2577 * i40evf_remove - Device Removal Routine
2578 * @pdev: PCI device information struct
2579 *
2580 * i40evf_remove is called by the PCI subsystem to alert the driver
2581 * that it should release a PCI device. The could be caused by a
2582 * Hot-Plug event, or because the driver is going to be removed from
2583 * memory.
2584 **/
2585static void i40evf_remove(struct pci_dev *pdev)
2586{
2587 struct net_device *netdev = pci_get_drvdata(pdev);
2588 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002589 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002590 struct i40e_hw *hw = &adapter->hw;
2591
2592 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002593 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002594
2595 if (adapter->netdev_registered) {
2596 unregister_netdev(netdev);
2597 adapter->netdev_registered = false;
2598 }
Mitch A Williams53d0b3a2014-12-09 08:53:04 +00002599
Mitch Williamsf4a71882015-01-09 11:18:16 +00002600 /* Shut down all the garbage mashers on the detention level */
Greg Rose5eae00c2013-12-21 06:12:45 +00002601 adapter->state = __I40EVF_REMOVE;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002602 adapter->aq_required = 0;
Mitch Williamsf4a71882015-01-09 11:18:16 +00002603 i40evf_request_reset(adapter);
2604 msleep(20);
2605 /* If the FW isn't responding, kick it once, but only once. */
2606 if (!i40evf_asq_done(hw)) {
2607 i40evf_request_reset(adapter);
2608 msleep(20);
2609 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002610
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002611 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002612 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002613 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002614 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002615 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002616 }
2617
Mitch Williamse5d17c32014-06-04 04:22:38 +00002618 if (adapter->watchdog_timer.function)
2619 del_timer_sync(&adapter->watchdog_timer);
2620
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002621 flush_scheduled_work();
2622
Greg Rose5eae00c2013-12-21 06:12:45 +00002623 if (hw->aq.asq.count)
2624 i40evf_shutdown_adminq(hw);
2625
2626 iounmap(hw->hw_addr);
2627 pci_release_regions(pdev);
2628
Mitch Williamse284fc82015-03-27 00:12:09 -07002629 i40evf_free_all_tx_resources(adapter);
2630 i40evf_free_all_rx_resources(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002631 i40evf_free_queues(adapter);
2632 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002633 /* If we got removed before an up/down sequence, we've got a filter
2634 * hanging out there that we need to get rid of.
2635 */
2636 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2637 list_del(&f->list);
2638 kfree(f);
2639 }
Mitch A Williams37dfdf32014-12-09 08:53:05 +00002640 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2641 list_del(&f->list);
2642 kfree(f);
2643 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002644
2645 free_netdev(netdev);
2646
2647 pci_disable_pcie_error_reporting(pdev);
2648
2649 pci_disable_device(pdev);
2650}
2651
2652static struct pci_driver i40evf_driver = {
2653 .name = i40evf_driver_name,
2654 .id_table = i40evf_pci_tbl,
2655 .probe = i40evf_probe,
2656 .remove = i40evf_remove,
2657#ifdef CONFIG_PM
2658 .suspend = i40evf_suspend,
2659 .resume = i40evf_resume,
2660#endif
2661 .shutdown = i40evf_shutdown,
2662};
2663
2664/**
2665 * i40e_init_module - Driver Registration Routine
2666 *
2667 * i40e_init_module is the first routine called when the driver is
2668 * loaded. All it does is register with the PCI subsystem.
2669 **/
2670static int __init i40evf_init_module(void)
2671{
2672 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002673
Greg Rose5eae00c2013-12-21 06:12:45 +00002674 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002675 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002676
2677 pr_info("%s\n", i40evf_copyright);
2678
2679 ret = pci_register_driver(&i40evf_driver);
2680 return ret;
2681}
2682
2683module_init(i40evf_init_module);
2684
2685/**
2686 * i40e_exit_module - Driver Exit Cleanup Routine
2687 *
2688 * i40e_exit_module is called just before the driver is removed
2689 * from memory.
2690 **/
2691static void __exit i40evf_exit_module(void)
2692{
2693 pci_unregister_driver(&i40evf_driver);
2694}
2695
2696module_exit(i40evf_exit_module);
2697
2698/* i40evf_main.c */