blob: 62456f80a4d0f0a9a8f0e74d1572f697a1c9328a [file] [log] [blame]
Greg Rose5eae00c2013-12-21 06:12:45 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Mitch Williamse1dfee82014-02-13 03:48:51 -08004 * Copyright(c) 2013 - 2014 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);
Mitch Williams169f4072014-04-01 07:11:50 +000031static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter);
32static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +000033static int i40evf_close(struct net_device *netdev);
34
35char i40evf_driver_name[] = "i40evf";
36static const char i40evf_driver_string[] =
Mitch Williams0af56f42014-06-04 20:42:10 +000037 "Intel(R) XL710/X710 Virtual Function Network Driver";
Greg Rose5eae00c2013-12-21 06:12:45 +000038
Catherine Sullivane966d5c2014-07-12 07:28:26 +000039#define DRV_VERSION "1.0.5"
Greg Rose5eae00c2013-12-21 06:12:45 +000040const char i40evf_driver_version[] = DRV_VERSION;
41static const char i40evf_copyright[] =
Mitch Williams673f2eb2014-02-20 19:29:13 -080042 "Copyright (c) 2013 - 2014 Intel Corporation.";
Greg Rose5eae00c2013-12-21 06:12:45 +000043
44/* i40evf_pci_tbl - PCI Device ID Table
45 *
46 * Wildcard entries (PCI_ANY_ID) should come last
47 * Last entry must be all 0s
48 *
49 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
50 * Class, Class Mask, private data (not used) }
51 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020052static const struct pci_device_id i40evf_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080053 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
Greg Rose5eae00c2013-12-21 06:12:45 +000054 /* required last entry */
55 {0, }
56};
57
58MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
59
60MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
61MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
62MODULE_LICENSE("GPL");
63MODULE_VERSION(DRV_VERSION);
64
65/**
66 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
67 * @hw: pointer to the HW structure
68 * @mem: ptr to mem struct to fill out
69 * @size: size of memory requested
70 * @alignment: what to align the allocation to
71 **/
72i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
73 struct i40e_dma_mem *mem,
74 u64 size, u32 alignment)
75{
76 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
77
78 if (!mem)
79 return I40E_ERR_PARAM;
80
81 mem->size = ALIGN(size, alignment);
82 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
83 (dma_addr_t *)&mem->pa, GFP_KERNEL);
84 if (mem->va)
85 return 0;
86 else
87 return I40E_ERR_NO_MEMORY;
88}
89
90/**
91 * i40evf_free_dma_mem_d - OS specific memory free for shared code
92 * @hw: pointer to the HW structure
93 * @mem: ptr to mem struct to free
94 **/
95i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
96{
97 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
98
99 if (!mem || !mem->va)
100 return I40E_ERR_PARAM;
101 dma_free_coherent(&adapter->pdev->dev, mem->size,
102 mem->va, (dma_addr_t)mem->pa);
103 return 0;
104}
105
106/**
107 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
108 * @hw: pointer to the HW structure
109 * @mem: ptr to mem struct to fill out
110 * @size: size of memory requested
111 **/
112i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
113 struct i40e_virt_mem *mem, u32 size)
114{
115 if (!mem)
116 return I40E_ERR_PARAM;
117
118 mem->size = size;
119 mem->va = kzalloc(size, GFP_KERNEL);
120
121 if (mem->va)
122 return 0;
123 else
124 return I40E_ERR_NO_MEMORY;
125}
126
127/**
128 * i40evf_free_virt_mem_d - OS specific memory free for shared code
129 * @hw: pointer to the HW structure
130 * @mem: ptr to mem struct to free
131 **/
132i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
133 struct i40e_virt_mem *mem)
134{
135 if (!mem)
136 return I40E_ERR_PARAM;
137
138 /* it's ok to kfree a NULL pointer */
139 kfree(mem->va);
140
141 return 0;
142}
143
144/**
145 * i40evf_debug_d - OS dependent version of debug printing
146 * @hw: pointer to the HW structure
147 * @mask: debug level mask
148 * @fmt_str: printf-type format description
149 **/
150void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
151{
152 char buf[512];
153 va_list argptr;
154
155 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
156 return;
157
158 va_start(argptr, fmt_str);
159 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
160 va_end(argptr);
161
162 /* the debug string is already formatted with a newline */
163 pr_info("%s", buf);
164}
165
166/**
167 * i40evf_tx_timeout - Respond to a Tx Hang
168 * @netdev: network interface device structure
169 **/
170static void i40evf_tx_timeout(struct net_device *netdev)
171{
172 struct i40evf_adapter *adapter = netdev_priv(netdev);
173
174 adapter->tx_timeout_count++;
Mitch Williams625777e2014-02-20 19:29:05 -0800175 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING)) {
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);
207 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA_ADMINQ_MASK);
208
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++) {
244 if (mask & (1 << (i - 1))) {
245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
247 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
248 }
249 }
250}
251
252/**
253 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
254 * @adapter: board private structure
255 * @mask: bitmap of vectors to trigger
256 **/
Mitch Williams75a64432014-11-11 20:02:42 +0000257static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
Greg Rose5eae00c2013-12-21 06:12:45 +0000258{
259 struct i40e_hw *hw = &adapter->hw;
260 int i;
261 uint32_t dyn_ctl;
262
Mitch Williams164ec1b2014-06-04 08:45:19 +0000263 if (mask & 1) {
264 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
265 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
266 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
267 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
268 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000269 for (i = 1; i < adapter->num_msix_vectors; i++) {
270 if (mask & (1 << i)) {
271 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
272 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
273 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
274 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
275 }
276 }
277}
278
279/**
280 * i40evf_irq_enable - Enable default interrupt generation settings
281 * @adapter: board private structure
282 **/
283void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
284{
285 struct i40e_hw *hw = &adapter->hw;
286
Mitch Williams164ec1b2014-06-04 08:45:19 +0000287 i40evf_misc_irq_enable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +0000288 i40evf_irq_enable_queues(adapter, ~0);
289
290 if (flush)
291 rd32(hw, I40E_VFGEN_RSTAT);
292}
293
294/**
295 * i40evf_msix_aq - Interrupt handler for vector 0
296 * @irq: interrupt number
297 * @data: pointer to netdev
298 **/
299static irqreturn_t i40evf_msix_aq(int irq, void *data)
300{
301 struct net_device *netdev = data;
302 struct i40evf_adapter *adapter = netdev_priv(netdev);
303 struct i40e_hw *hw = &adapter->hw;
304 u32 val;
305 u32 ena_mask;
306
307 /* handle non-queue interrupts */
308 val = rd32(hw, I40E_VFINT_ICR01);
309 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
310
311
312 val = rd32(hw, I40E_VFINT_DYN_CTL01);
313 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
314 wr32(hw, I40E_VFINT_DYN_CTL01, val);
315
316 /* re-enable interrupt causes */
317 wr32(hw, I40E_VFINT_ICR0_ENA1, ena_mask);
318 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK);
319
320 /* schedule work on the private workqueue */
321 schedule_work(&adapter->adminq_task);
322
323 return IRQ_HANDLED;
324}
325
326/**
327 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
328 * @irq: interrupt number
329 * @data: pointer to a q_vector
330 **/
331static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
332{
333 struct i40e_q_vector *q_vector = data;
334
335 if (!q_vector->tx.ring && !q_vector->rx.ring)
336 return IRQ_HANDLED;
337
338 napi_schedule(&q_vector->napi);
339
340 return IRQ_HANDLED;
341}
342
343/**
344 * i40evf_map_vector_to_rxq - associate irqs with rx queues
345 * @adapter: board private structure
346 * @v_idx: interrupt number
347 * @r_idx: queue number
348 **/
349static void
350i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
351{
352 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
353 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
354
355 rx_ring->q_vector = q_vector;
356 rx_ring->next = q_vector->rx.ring;
357 rx_ring->vsi = &adapter->vsi;
358 q_vector->rx.ring = rx_ring;
359 q_vector->rx.count++;
360 q_vector->rx.latency_range = I40E_LOW_LATENCY;
361}
362
363/**
364 * i40evf_map_vector_to_txq - associate irqs with tx queues
365 * @adapter: board private structure
366 * @v_idx: interrupt number
367 * @t_idx: queue number
368 **/
369static void
370i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
371{
372 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
373 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
374
375 tx_ring->q_vector = q_vector;
376 tx_ring->next = q_vector->tx.ring;
377 tx_ring->vsi = &adapter->vsi;
378 q_vector->tx.ring = tx_ring;
379 q_vector->tx.count++;
380 q_vector->tx.latency_range = I40E_LOW_LATENCY;
381 q_vector->num_ringpairs++;
382 q_vector->ring_mask |= (1 << t_idx);
383}
384
385/**
386 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
387 * @adapter: board private structure to initialize
388 *
389 * This function maps descriptor rings to the queue-specific vectors
390 * we were allotted through the MSI-X enabling code. Ideally, we'd have
391 * one vector per ring/queue, but on a constrained vector budget, we
392 * group the rings as "efficiently" as possible. You would add new
393 * mapping configurations in here.
394 **/
395static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
396{
397 int q_vectors;
398 int v_start = 0;
399 int rxr_idx = 0, txr_idx = 0;
Mitch Williamscc052922014-10-25 03:24:34 +0000400 int rxr_remaining = adapter->num_active_queues;
401 int txr_remaining = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +0000402 int i, j;
403 int rqpv, tqpv;
404 int err = 0;
405
406 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
407
408 /* The ideal configuration...
409 * We have enough vectors to map one per queue.
410 */
411 if (q_vectors == (rxr_remaining * 2)) {
412 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
413 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
414
415 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
416 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
417 goto out;
418 }
419
420 /* If we don't have enough vectors for a 1-to-1
421 * mapping, we'll have to group them so there are
422 * multiple queues per vector.
423 * Re-adjusting *qpv takes care of the remainder.
424 */
425 for (i = v_start; i < q_vectors; i++) {
426 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
427 for (j = 0; j < rqpv; j++) {
428 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
429 rxr_idx++;
430 rxr_remaining--;
431 }
432 }
433 for (i = v_start; i < q_vectors; i++) {
434 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
435 for (j = 0; j < tqpv; j++) {
436 i40evf_map_vector_to_txq(adapter, i, txr_idx);
437 txr_idx++;
438 txr_remaining--;
439 }
440 }
441
442out:
443 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
444
445 return err;
446}
447
448/**
449 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
450 * @adapter: board private structure
451 *
452 * Allocates MSI-X vectors for tx and rx handling, and requests
453 * interrupts from the kernel.
454 **/
455static int
456i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
457{
458 int vector, err, q_vectors;
459 int rx_int_idx = 0, tx_int_idx = 0;
460
461 i40evf_irq_disable(adapter);
462 /* Decrement for Other and TCP Timer vectors */
463 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
464
465 for (vector = 0; vector < q_vectors; vector++) {
466 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
467
468 if (q_vector->tx.ring && q_vector->rx.ring) {
469 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
470 "i40evf-%s-%s-%d", basename,
471 "TxRx", rx_int_idx++);
472 tx_int_idx++;
473 } else if (q_vector->rx.ring) {
474 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
475 "i40evf-%s-%s-%d", basename,
476 "rx", rx_int_idx++);
477 } else if (q_vector->tx.ring) {
478 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
479 "i40evf-%s-%s-%d", basename,
480 "tx", tx_int_idx++);
481 } else {
482 /* skip this unused q_vector */
483 continue;
484 }
485 err = request_irq(
486 adapter->msix_entries[vector + NONQ_VECS].vector,
487 i40evf_msix_clean_rings,
488 0,
489 q_vector->name,
490 q_vector);
491 if (err) {
492 dev_info(&adapter->pdev->dev,
493 "%s: request_irq failed, error: %d\n",
494 __func__, err);
495 goto free_queue_irqs;
496 }
497 /* assign the mask for this irq */
498 irq_set_affinity_hint(
499 adapter->msix_entries[vector + NONQ_VECS].vector,
500 q_vector->affinity_mask);
501 }
502
503 return 0;
504
505free_queue_irqs:
506 while (vector) {
507 vector--;
508 irq_set_affinity_hint(
509 adapter->msix_entries[vector + NONQ_VECS].vector,
510 NULL);
511 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
512 adapter->q_vector[vector]);
513 }
514 return err;
515}
516
517/**
518 * i40evf_request_misc_irq - Initialize MSI-X interrupts
519 * @adapter: board private structure
520 *
521 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
522 * vector is only for the admin queue, and stays active even when the netdev
523 * is closed.
524 **/
525static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
526{
527 struct net_device *netdev = adapter->netdev;
528 int err;
529
Jesse Brandeburgb39c1e22014-08-01 13:27:08 -0700530 snprintf(adapter->misc_vector_name,
531 sizeof(adapter->misc_vector_name) - 1, "i40evf:mbx");
Greg Rose5eae00c2013-12-21 06:12:45 +0000532 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800533 &i40evf_msix_aq, 0,
534 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000535 if (err) {
536 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800537 "request_irq for %s failed: %d\n",
538 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000539 free_irq(adapter->msix_entries[0].vector, netdev);
540 }
541 return err;
542}
543
544/**
545 * i40evf_free_traffic_irqs - Free MSI-X interrupts
546 * @adapter: board private structure
547 *
548 * Frees all MSI-X vectors other than 0.
549 **/
550static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
551{
552 int i;
553 int q_vectors;
Mitch Williams75a64432014-11-11 20:02:42 +0000554
Greg Rose5eae00c2013-12-21 06:12:45 +0000555 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
556
557 for (i = 0; i < q_vectors; i++) {
558 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
559 NULL);
560 free_irq(adapter->msix_entries[i+1].vector,
561 adapter->q_vector[i]);
562 }
563}
564
565/**
566 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
567 * @adapter: board private structure
568 *
569 * Frees MSI-X vector 0.
570 **/
571static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
572{
573 struct net_device *netdev = adapter->netdev;
574
575 free_irq(adapter->msix_entries[0].vector, netdev);
576}
577
578/**
579 * i40evf_configure_tx - Configure Transmit Unit after Reset
580 * @adapter: board private structure
581 *
582 * Configure the Tx unit of the MAC after a reset.
583 **/
584static void i40evf_configure_tx(struct i40evf_adapter *adapter)
585{
586 struct i40e_hw *hw = &adapter->hw;
587 int i;
Mitch Williams75a64432014-11-11 20:02:42 +0000588
Mitch Williamscc052922014-10-25 03:24:34 +0000589 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000590 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
591}
592
593/**
594 * i40evf_configure_rx - Configure Receive Unit after Reset
595 * @adapter: board private structure
596 *
597 * Configure the Rx unit of the MAC after a reset.
598 **/
599static void i40evf_configure_rx(struct i40evf_adapter *adapter)
600{
601 struct i40e_hw *hw = &adapter->hw;
602 struct net_device *netdev = adapter->netdev;
603 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
604 int i;
605 int rx_buf_len;
606
607
608 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
609 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
610
611 /* Decide whether to use packet split mode or not */
612 if (netdev->mtu > ETH_DATA_LEN) {
613 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
614 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
615 else
616 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
617 } else {
618 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
619 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
620 else
621 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
622 }
623
624 /* Set the RX buffer length according to the mode */
625 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
626 rx_buf_len = I40E_RX_HDR_SIZE;
627 } else {
628 if (netdev->mtu <= ETH_DATA_LEN)
629 rx_buf_len = I40EVF_RXBUFFER_2048;
630 else
631 rx_buf_len = ALIGN(max_frame, 1024);
632 }
633
Mitch Williamscc052922014-10-25 03:24:34 +0000634 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000635 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
636 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
637 }
638}
639
640/**
641 * i40evf_find_vlan - Search filter list for specific vlan filter
642 * @adapter: board private structure
643 * @vlan: vlan tag
644 *
645 * Returns ptr to the filter object or NULL
646 **/
647static struct
648i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
649{
650 struct i40evf_vlan_filter *f;
651
652 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
653 if (vlan == f->vlan)
654 return f;
655 }
656 return NULL;
657}
658
659/**
660 * i40evf_add_vlan - Add a vlan filter to the list
661 * @adapter: board private structure
662 * @vlan: VLAN tag
663 *
664 * Returns ptr to the filter object or NULL when no memory available.
665 **/
666static struct
667i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
668{
669 struct i40evf_vlan_filter *f;
670
671 f = i40evf_find_vlan(adapter, vlan);
Mitch Williams348d4992014-11-11 20:02:52 +0000672 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000673 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000674 if (!f)
Greg Rose5eae00c2013-12-21 06:12:45 +0000675 return NULL;
Mitch Williams249c8b82014-05-10 04:49:04 +0000676
Greg Rose5eae00c2013-12-21 06:12:45 +0000677 f->vlan = vlan;
678
679 INIT_LIST_HEAD(&f->list);
680 list_add(&f->list, &adapter->vlan_filter_list);
681 f->add = true;
682 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
683 }
684
685 return f;
686}
687
688/**
689 * i40evf_del_vlan - Remove a vlan filter from the list
690 * @adapter: board private structure
691 * @vlan: VLAN tag
692 **/
693static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
694{
695 struct i40evf_vlan_filter *f;
696
697 f = i40evf_find_vlan(adapter, vlan);
698 if (f) {
699 f->remove = true;
700 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
701 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000702}
703
704/**
705 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
706 * @netdev: network device struct
707 * @vid: VLAN tag
708 **/
709static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000710 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000711{
712 struct i40evf_adapter *adapter = netdev_priv(netdev);
713
714 if (i40evf_add_vlan(adapter, vid) == NULL)
715 return -ENOMEM;
716 return 0;
717}
718
719/**
720 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
721 * @netdev: network device struct
722 * @vid: VLAN tag
723 **/
724static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
Mitch Williams75a64432014-11-11 20:02:42 +0000725 __always_unused __be16 proto, u16 vid)
Greg Rose5eae00c2013-12-21 06:12:45 +0000726{
727 struct i40evf_adapter *adapter = netdev_priv(netdev);
728
729 i40evf_del_vlan(adapter, vid);
730 return 0;
731}
732
733/**
734 * i40evf_find_filter - Search filter list for specific mac filter
735 * @adapter: board private structure
736 * @macaddr: the MAC address
737 *
738 * Returns ptr to the filter object or NULL
739 **/
740static struct
741i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
742 u8 *macaddr)
743{
744 struct i40evf_mac_filter *f;
745
746 if (!macaddr)
747 return NULL;
748
749 list_for_each_entry(f, &adapter->mac_filter_list, list) {
750 if (ether_addr_equal(macaddr, f->macaddr))
751 return f;
752 }
753 return NULL;
754}
755
756/**
757 * i40e_add_filter - Add a mac filter to the filter list
758 * @adapter: board private structure
759 * @macaddr: the MAC address
760 *
761 * Returns ptr to the filter object or NULL when no memory available.
762 **/
763static struct
764i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
765 u8 *macaddr)
766{
767 struct i40evf_mac_filter *f;
768
769 if (!macaddr)
770 return NULL;
771
772 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
773 &adapter->crit_section))
Mitch Williamsb65476c2014-07-09 07:46:14 +0000774 udelay(1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000775
776 f = i40evf_find_filter(adapter, macaddr);
Mitch Williams348d4992014-11-11 20:02:52 +0000777 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000778 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +0000779 if (!f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000780 clear_bit(__I40EVF_IN_CRITICAL_TASK,
781 &adapter->crit_section);
782 return NULL;
783 }
784
Greg Rose9a173902014-05-22 06:32:02 +0000785 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000786
787 list_add(&f->list, &adapter->mac_filter_list);
788 f->add = true;
789 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
790 }
791
792 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
793 return f;
794}
795
796/**
797 * i40evf_set_mac - NDO callback to set port mac address
798 * @netdev: network interface device structure
799 * @p: pointer to an address structure
800 *
801 * Returns 0 on success, negative on failure
802 **/
803static int i40evf_set_mac(struct net_device *netdev, void *p)
804{
805 struct i40evf_adapter *adapter = netdev_priv(netdev);
806 struct i40e_hw *hw = &adapter->hw;
807 struct i40evf_mac_filter *f;
808 struct sockaddr *addr = p;
809
810 if (!is_valid_ether_addr(addr->sa_data))
811 return -EADDRNOTAVAIL;
812
813 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
814 return 0;
815
816 f = i40evf_add_filter(adapter, addr->sa_data);
817 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000818 ether_addr_copy(hw->mac.addr, addr->sa_data);
819 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000820 }
821
822 return (f == NULL) ? -ENOMEM : 0;
823}
824
825/**
826 * i40evf_set_rx_mode - NDO callback to set the netdev filters
827 * @netdev: network interface device structure
828 **/
829static void i40evf_set_rx_mode(struct net_device *netdev)
830{
831 struct i40evf_adapter *adapter = netdev_priv(netdev);
832 struct i40evf_mac_filter *f, *ftmp;
833 struct netdev_hw_addr *uca;
834 struct netdev_hw_addr *mca;
835
836 /* add addr if not already in the filter list */
837 netdev_for_each_uc_addr(uca, netdev) {
838 i40evf_add_filter(adapter, uca->addr);
839 }
840 netdev_for_each_mc_addr(mca, netdev) {
841 i40evf_add_filter(adapter, mca->addr);
842 }
843
844 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
845 &adapter->crit_section))
Mitch Williamsb65476c2014-07-09 07:46:14 +0000846 udelay(1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000847 /* remove filter if not in netdev list */
848 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
849 bool found = false;
850
Tobias Klauserdc5f2de2014-05-20 08:23:06 +0000851 if (is_multicast_ether_addr(f->macaddr)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000852 netdev_for_each_mc_addr(mca, netdev) {
853 if (ether_addr_equal(mca->addr, f->macaddr)) {
854 found = true;
855 break;
856 }
857 }
858 } else {
859 netdev_for_each_uc_addr(uca, netdev) {
860 if (ether_addr_equal(uca->addr, f->macaddr)) {
861 found = true;
862 break;
863 }
864 }
865 }
866 if (found) {
867 f->remove = true;
868 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
869 }
870 }
871 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
872}
873
874/**
875 * i40evf_napi_enable_all - enable NAPI on all queue vectors
876 * @adapter: board private structure
877 **/
878static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
879{
880 int q_idx;
881 struct i40e_q_vector *q_vector;
882 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
883
884 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
885 struct napi_struct *napi;
Mitch Williams75a64432014-11-11 20:02:42 +0000886
Greg Rose5eae00c2013-12-21 06:12:45 +0000887 q_vector = adapter->q_vector[q_idx];
888 napi = &q_vector->napi;
889 napi_enable(napi);
890 }
891}
892
893/**
894 * i40evf_napi_disable_all - disable NAPI on all queue vectors
895 * @adapter: board private structure
896 **/
897static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
898{
899 int q_idx;
900 struct i40e_q_vector *q_vector;
901 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
902
903 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
904 q_vector = adapter->q_vector[q_idx];
905 napi_disable(&q_vector->napi);
906 }
907}
908
909/**
910 * i40evf_configure - set up transmit and receive data structures
911 * @adapter: board private structure
912 **/
913static void i40evf_configure(struct i40evf_adapter *adapter)
914{
915 struct net_device *netdev = adapter->netdev;
916 int i;
917
918 i40evf_set_rx_mode(netdev);
919
920 i40evf_configure_tx(adapter);
921 i40evf_configure_rx(adapter);
922 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
923
Mitch Williamscc052922014-10-25 03:24:34 +0000924 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000925 struct i40e_ring *ring = adapter->rx_rings[i];
Mitch Williams75a64432014-11-11 20:02:42 +0000926
Greg Rose5eae00c2013-12-21 06:12:45 +0000927 i40evf_alloc_rx_buffers(ring, ring->count);
928 ring->next_to_use = ring->count - 1;
929 writel(ring->next_to_use, ring->tail);
930 }
931}
932
933/**
934 * i40evf_up_complete - Finish the last steps of bringing up a connection
935 * @adapter: board private structure
936 **/
937static int i40evf_up_complete(struct i40evf_adapter *adapter)
938{
939 adapter->state = __I40EVF_RUNNING;
940 clear_bit(__I40E_DOWN, &adapter->vsi.state);
941
942 i40evf_napi_enable_all(adapter);
943
944 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
945 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
946 return 0;
947}
948
949/**
950 * i40evf_clean_all_rx_rings - Free Rx Buffers for all queues
951 * @adapter: board private structure
952 **/
953static void i40evf_clean_all_rx_rings(struct i40evf_adapter *adapter)
954{
955 int i;
956
Mitch Williamscc052922014-10-25 03:24:34 +0000957 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000958 i40evf_clean_rx_ring(adapter->rx_rings[i]);
959}
960
961/**
962 * i40evf_clean_all_tx_rings - Free Tx Buffers for all queues
963 * @adapter: board private structure
964 **/
965static void i40evf_clean_all_tx_rings(struct i40evf_adapter *adapter)
966{
967 int i;
968
Mitch Williamscc052922014-10-25 03:24:34 +0000969 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +0000970 i40evf_clean_tx_ring(adapter->tx_rings[i]);
971}
972
973/**
974 * i40e_down - Shutdown the connection processing
975 * @adapter: board private structure
976 **/
977void i40evf_down(struct i40evf_adapter *adapter)
978{
979 struct net_device *netdev = adapter->netdev;
980 struct i40evf_mac_filter *f;
981
Mitch Williamsddf0b3a2014-05-22 06:31:46 +0000982 if (adapter->state == __I40EVF_DOWN)
983 return;
984
Mitch Williamsef8693e2014-02-13 03:48:53 -0800985 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +0000986 list_for_each_entry(f, &adapter->mac_filter_list, list) {
987 f->remove = true;
988 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800989 /* remove all VLAN filters */
990 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
991 f->remove = true;
992 }
Mitch Williamsef8693e2014-02-13 03:48:53 -0800993 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
994 adapter->state != __I40EVF_RESETTING) {
995 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800996 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800997 /* disable receives */
998 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
999 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1000 msleep(20);
1001 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001002 netif_tx_disable(netdev);
1003
1004 netif_tx_stop_all_queues(netdev);
1005
1006 i40evf_irq_disable(adapter);
1007
1008 i40evf_napi_disable_all(adapter);
1009
1010 netif_carrier_off(netdev);
1011
1012 i40evf_clean_all_tx_rings(adapter);
1013 i40evf_clean_all_rx_rings(adapter);
1014}
1015
1016/**
1017 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1018 * @adapter: board private structure
1019 * @vectors: number of vectors to request
1020 *
1021 * Work with the OS to set up the MSIX vectors needed.
1022 *
1023 * Returns 0 on success, negative on failure
1024 **/
1025static int
1026i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1027{
1028 int err, vector_threshold;
1029
1030 /* We'll want at least 3 (vector_threshold):
1031 * 0) Other (Admin Queue and link, mostly)
1032 * 1) TxQ[0] Cleanup
1033 * 2) RxQ[0] Cleanup
1034 */
1035 vector_threshold = MIN_MSIX_COUNT;
1036
1037 /* The more we get, the more we will assign to Tx/Rx Cleanup
1038 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1039 * Right now, we simply care about how many we'll get; we'll
1040 * set them up later while requesting irq's.
1041 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001042 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1043 vector_threshold, vectors);
1044 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001045 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001046 kfree(adapter->msix_entries);
1047 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001048 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001049 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001050
1051 /* Adjust for only the vectors we'll use, which is minimum
1052 * of max_msix_q_vectors + NONQ_VECS, or the number of
1053 * vectors we were allocated.
1054 */
1055 adapter->num_msix_vectors = err;
1056 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001057}
1058
1059/**
1060 * i40evf_free_queues - Free memory for all rings
1061 * @adapter: board private structure to initialize
1062 *
1063 * Free all of the memory associated with queue pairs.
1064 **/
1065static void i40evf_free_queues(struct i40evf_adapter *adapter)
1066{
1067 int i;
1068
1069 if (!adapter->vsi_res)
1070 return;
Mitch Williamscc052922014-10-25 03:24:34 +00001071 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001072 if (adapter->tx_rings[i])
1073 kfree_rcu(adapter->tx_rings[i], rcu);
1074 adapter->tx_rings[i] = NULL;
1075 adapter->rx_rings[i] = NULL;
1076 }
1077}
1078
1079/**
1080 * i40evf_alloc_queues - Allocate memory for all rings
1081 * @adapter: board private structure to initialize
1082 *
1083 * We allocate one ring per queue at run-time since we don't know the
1084 * number of queues at compile-time. The polling_netdev array is
1085 * intended for Multiqueue, but should work fine with a single queue.
1086 **/
1087static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1088{
1089 int i;
1090
Mitch Williamscc052922014-10-25 03:24:34 +00001091 for (i = 0; i < adapter->num_active_queues; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001092 struct i40e_ring *tx_ring;
1093 struct i40e_ring *rx_ring;
1094
Mitch Williams75a64432014-11-11 20:02:42 +00001095 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001096 if (!tx_ring)
1097 goto err_out;
1098
1099 tx_ring->queue_index = i;
1100 tx_ring->netdev = adapter->netdev;
1101 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001102 tx_ring->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001103 adapter->tx_rings[i] = tx_ring;
1104
1105 rx_ring = &tx_ring[1];
1106 rx_ring->queue_index = i;
1107 rx_ring->netdev = adapter->netdev;
1108 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001109 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001110 adapter->rx_rings[i] = rx_ring;
1111 }
1112
1113 return 0;
1114
1115err_out:
1116 i40evf_free_queues(adapter);
1117 return -ENOMEM;
1118}
1119
1120/**
1121 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1122 * @adapter: board private structure to initialize
1123 *
1124 * Attempt to configure the interrupts using the best available
1125 * capabilities of the hardware and the kernel.
1126 **/
1127static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1128{
1129 int vector, v_budget;
1130 int pairs = 0;
1131 int err = 0;
1132
1133 if (!adapter->vsi_res) {
1134 err = -EIO;
1135 goto out;
1136 }
Mitch Williamscc052922014-10-25 03:24:34 +00001137 pairs = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001138
1139 /* It's easy to be greedy for MSI-X vectors, but it really
1140 * doesn't do us much good if we have a lot more vectors
1141 * than CPU's. So let's be conservative and only ask for
1142 * (roughly) twice the number of vectors as there are CPU's.
1143 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001144 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1145 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001146
Greg Rose5eae00c2013-12-21 06:12:45 +00001147 adapter->msix_entries = kcalloc(v_budget,
1148 sizeof(struct msix_entry), GFP_KERNEL);
1149 if (!adapter->msix_entries) {
1150 err = -ENOMEM;
1151 goto out;
1152 }
1153
1154 for (vector = 0; vector < v_budget; vector++)
1155 adapter->msix_entries[vector].entry = vector;
1156
1157 i40evf_acquire_msix_vectors(adapter, v_budget);
1158
1159out:
1160 adapter->netdev->real_num_tx_queues = pairs;
1161 return err;
1162}
1163
1164/**
1165 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1166 * @adapter: board private structure to initialize
1167 *
1168 * We allocate one q_vector per queue interrupt. If allocation fails we
1169 * return -ENOMEM.
1170 **/
1171static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1172{
1173 int q_idx, num_q_vectors;
1174 struct i40e_q_vector *q_vector;
1175
1176 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1177
1178 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
Mitch Williams75a64432014-11-11 20:02:42 +00001179 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
Greg Rose5eae00c2013-12-21 06:12:45 +00001180 if (!q_vector)
1181 goto err_out;
1182 q_vector->adapter = adapter;
1183 q_vector->vsi = &adapter->vsi;
1184 q_vector->v_idx = q_idx;
1185 netif_napi_add(adapter->netdev, &q_vector->napi,
Mitch Williams75a64432014-11-11 20:02:42 +00001186 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001187 adapter->q_vector[q_idx] = q_vector;
1188 }
1189
1190 return 0;
1191
1192err_out:
1193 while (q_idx) {
1194 q_idx--;
1195 q_vector = adapter->q_vector[q_idx];
1196 netif_napi_del(&q_vector->napi);
1197 kfree(q_vector);
1198 adapter->q_vector[q_idx] = NULL;
1199 }
1200 return -ENOMEM;
1201}
1202
1203/**
1204 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1205 * @adapter: board private structure to initialize
1206 *
1207 * This function frees the memory allocated to the q_vectors. In addition if
1208 * NAPI is enabled it will delete any references to the NAPI struct prior
1209 * to freeing the q_vector.
1210 **/
1211static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1212{
1213 int q_idx, num_q_vectors;
1214 int napi_vectors;
1215
1216 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
Mitch Williamscc052922014-10-25 03:24:34 +00001217 napi_vectors = adapter->num_active_queues;
Greg Rose5eae00c2013-12-21 06:12:45 +00001218
1219 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1220 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1221
1222 adapter->q_vector[q_idx] = NULL;
1223 if (q_idx < napi_vectors)
1224 netif_napi_del(&q_vector->napi);
1225 kfree(q_vector);
1226 }
1227}
1228
1229/**
1230 * i40evf_reset_interrupt_capability - Reset MSIX setup
1231 * @adapter: board private structure
1232 *
1233 **/
1234void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1235{
1236 pci_disable_msix(adapter->pdev);
1237 kfree(adapter->msix_entries);
1238 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001239}
1240
1241/**
1242 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1243 * @adapter: board private structure to initialize
1244 *
1245 **/
1246int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1247{
1248 int err;
1249
1250 err = i40evf_set_interrupt_capability(adapter);
1251 if (err) {
1252 dev_err(&adapter->pdev->dev,
1253 "Unable to setup interrupt capabilities\n");
1254 goto err_set_interrupt;
1255 }
1256
1257 err = i40evf_alloc_q_vectors(adapter);
1258 if (err) {
1259 dev_err(&adapter->pdev->dev,
1260 "Unable to allocate memory for queue vectors\n");
1261 goto err_alloc_q_vectors;
1262 }
1263
1264 err = i40evf_alloc_queues(adapter);
1265 if (err) {
1266 dev_err(&adapter->pdev->dev,
1267 "Unable to allocate memory for queues\n");
1268 goto err_alloc_queues;
1269 }
1270
1271 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
Mitch Williams75a64432014-11-11 20:02:42 +00001272 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1273 adapter->num_active_queues);
Greg Rose5eae00c2013-12-21 06:12:45 +00001274
1275 return 0;
1276err_alloc_queues:
1277 i40evf_free_q_vectors(adapter);
1278err_alloc_q_vectors:
1279 i40evf_reset_interrupt_capability(adapter);
1280err_set_interrupt:
1281 return err;
1282}
1283
1284/**
1285 * i40evf_watchdog_timer - Periodic call-back timer
1286 * @data: pointer to adapter disguised as unsigned long
1287 **/
1288static void i40evf_watchdog_timer(unsigned long data)
1289{
1290 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
Mitch Williams75a64432014-11-11 20:02:42 +00001291
Greg Rose5eae00c2013-12-21 06:12:45 +00001292 schedule_work(&adapter->watchdog_task);
1293 /* timer will be rescheduled in watchdog task */
1294}
1295
1296/**
1297 * i40evf_watchdog_task - Periodic call-back task
1298 * @work: pointer to work_struct
1299 **/
1300static void i40evf_watchdog_task(struct work_struct *work)
1301{
1302 struct i40evf_adapter *adapter = container_of(work,
Mitch Williams75a64432014-11-11 20:02:42 +00001303 struct i40evf_adapter,
1304 watchdog_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001305 struct i40e_hw *hw = &adapter->hw;
Ashish Shahfd358862014-08-01 13:27:11 -07001306 uint32_t rstat_val;
Greg Rose5eae00c2013-12-21 06:12:45 +00001307
Greg Rose5eae00c2013-12-21 06:12:45 +00001308 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001309 goto restart_watchdog;
1310
1311 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Ashish Shahfd358862014-08-01 13:27:11 -07001312 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1313 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1314 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1315 (rstat_val == I40E_VFR_COMPLETED)) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001316 /* A chance for redemption! */
1317 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1318 adapter->state = __I40EVF_STARTUP;
1319 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1320 schedule_delayed_work(&adapter->init_task, 10);
1321 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1322 &adapter->crit_section);
1323 /* Don't reschedule the watchdog, since we've restarted
1324 * the init task. When init_task contacts the PF and
1325 * gets everything set up again, it'll restart the
1326 * watchdog for us. Down, boy. Sit. Stay. Woof.
1327 */
1328 return;
1329 }
1330 adapter->aq_pending = 0;
1331 adapter->aq_required = 0;
1332 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1333 goto watchdog_done;
1334 }
1335
1336 if ((adapter->state < __I40EVF_DOWN) ||
1337 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001338 goto watchdog_done;
1339
Mitch Williamsef8693e2014-02-13 03:48:53 -08001340 /* check for reset */
Ashish Shahfd358862014-08-01 13:27:11 -07001341 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
Mitch Williams75a64432014-11-11 20:02:42 +00001342 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001343 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
Ashish Shahfd358862014-08-01 13:27:11 -07001344 (rstat_val != I40E_VFR_VFACTIVE) &&
1345 (rstat_val != I40E_VFR_COMPLETED)) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001346 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001347 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001348 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001349 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001350 adapter->aq_pending = 0;
1351 adapter->aq_required = 0;
1352 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001353 goto watchdog_done;
1354 }
1355
1356 /* Process admin queue tasks. After init, everything gets done
1357 * here so we don't race on the admin queue.
1358 */
1359 if (adapter->aq_pending)
1360 goto watchdog_done;
1361
1362 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1363 i40evf_map_queues(adapter);
1364 goto watchdog_done;
1365 }
1366
1367 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1368 i40evf_add_ether_addrs(adapter);
1369 goto watchdog_done;
1370 }
1371
1372 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1373 i40evf_add_vlans(adapter);
1374 goto watchdog_done;
1375 }
1376
1377 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1378 i40evf_del_ether_addrs(adapter);
1379 goto watchdog_done;
1380 }
1381
1382 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1383 i40evf_del_vlans(adapter);
1384 goto watchdog_done;
1385 }
1386
1387 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1388 i40evf_disable_queues(adapter);
1389 goto watchdog_done;
1390 }
1391
1392 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1393 i40evf_configure_queues(adapter);
1394 goto watchdog_done;
1395 }
1396
1397 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1398 i40evf_enable_queues(adapter);
1399 goto watchdog_done;
1400 }
1401
1402 if (adapter->state == __I40EVF_RUNNING)
1403 i40evf_request_stats(adapter);
1404
1405 i40evf_irq_enable(adapter, true);
1406 i40evf_fire_sw_int(adapter, 0xFF);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001407
Greg Rose5eae00c2013-12-21 06:12:45 +00001408watchdog_done:
Mitch Williamsef8693e2014-02-13 03:48:53 -08001409 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1410restart_watchdog:
Ashish Shahd3e2edb2014-08-01 13:27:12 -07001411 if (adapter->state == __I40EVF_REMOVE)
1412 return;
Greg Rose5eae00c2013-12-21 06:12:45 +00001413 if (adapter->aq_required)
1414 mod_timer(&adapter->watchdog_timer,
1415 jiffies + msecs_to_jiffies(20));
1416 else
1417 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001418 schedule_work(&adapter->adminq_task);
1419}
1420
Mitch A Williams5b7af022014-03-28 06:49:02 +00001421/**
Catherine Sullivan3dd55502014-05-20 08:01:36 +00001422 * next_queue - increment to next available tx queue
Mitch A Williams5b7af022014-03-28 06:49:02 +00001423 * @adapter: board private structure
1424 * @j: queue counter
1425 *
1426 * Helper function for RSS programming to increment through available
1427 * queus. Returns the next queue value.
1428 **/
Mitch Williams96d47702014-02-11 08:27:50 +00001429static int next_queue(struct i40evf_adapter *adapter, int j)
1430{
1431 j += 1;
1432
Mitch Williamscc052922014-10-25 03:24:34 +00001433 return j >= adapter->num_active_queues ? 0 : j;
Mitch Williams96d47702014-02-11 08:27:50 +00001434}
1435
Greg Rose5eae00c2013-12-21 06:12:45 +00001436/**
1437 * i40evf_configure_rss - Prepare for RSS if used
1438 * @adapter: board private structure
1439 **/
1440static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1441{
Eric Dumazet22f258a2014-11-16 06:23:13 -08001442 u32 rss_key[I40E_VFQF_HKEY_MAX_INDEX + 1];
Greg Rose5eae00c2013-12-21 06:12:45 +00001443 struct i40e_hw *hw = &adapter->hw;
1444 u32 lut = 0;
1445 int i, j;
1446 u64 hena;
1447
Mitch Williamscc052922014-10-25 03:24:34 +00001448 /* No RSS for single queue. */
1449 if (adapter->num_active_queues == 1) {
1450 wr32(hw, I40E_VFQF_HENA(0), 0);
1451 wr32(hw, I40E_VFQF_HENA(1), 0);
1452 return;
1453 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001454
Mitch Williamscc052922014-10-25 03:24:34 +00001455 /* Hash type is configured by the PF - we just supply the key */
Eric Dumazet22f258a2014-11-16 06:23:13 -08001456 netdev_rss_key_fill(rss_key, sizeof(rss_key));
Greg Rose5eae00c2013-12-21 06:12:45 +00001457 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
Eric Dumazet22f258a2014-11-16 06:23:13 -08001458 wr32(hw, I40E_VFQF_HKEY(i), rss_key[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001459
1460 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1461 hena = I40E_DEFAULT_RSS_HENA;
1462 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1463 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1464
1465 /* Populate the LUT with max no. of queues in round robin fashion */
Mitch Williamscc052922014-10-25 03:24:34 +00001466 j = adapter->num_active_queues;
Mitch Williams96d47702014-02-11 08:27:50 +00001467 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
Mitch A Williams5b7af022014-03-28 06:49:02 +00001468 j = next_queue(adapter, j);
1469 lut = j;
1470 j = next_queue(adapter, j);
1471 lut |= j << 8;
1472 j = next_queue(adapter, j);
1473 lut |= j << 16;
1474 j = next_queue(adapter, j);
1475 lut |= j << 24;
Mitch Williams96d47702014-02-11 08:27:50 +00001476 wr32(hw, I40E_VFQF_HLUT(i), lut);
Greg Rose5eae00c2013-12-21 06:12:45 +00001477 }
1478 i40e_flush(hw);
1479}
1480
Mitch Williamsef8693e2014-02-13 03:48:53 -08001481#define I40EVF_RESET_WAIT_MS 100
1482#define I40EVF_RESET_WAIT_COUNT 200
Greg Rose5eae00c2013-12-21 06:12:45 +00001483/**
1484 * i40evf_reset_task - Call-back task to handle hardware reset
1485 * @work: pointer to work_struct
1486 *
1487 * During reset we need to shut down and reinitialize the admin queue
1488 * before we can use it to communicate with the PF again. We also clear
1489 * and reinit the rings because that context is lost as well.
1490 **/
1491static void i40evf_reset_task(struct work_struct *work)
1492{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001493 struct i40evf_adapter *adapter = container_of(work,
1494 struct i40evf_adapter,
1495 reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001496 struct i40e_hw *hw = &adapter->hw;
1497 int i = 0, err;
1498 uint32_t rstat_val;
1499
1500 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1501 &adapter->crit_section))
Neerav Parikhf98a2002014-09-13 07:40:44 +00001502 usleep_range(500, 1000);
Mitch Williams3526d802014-03-06 08:59:56 +00001503
1504 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1505 dev_info(&adapter->pdev->dev, "Requesting reset from PF\n");
1506 i40evf_request_reset(adapter);
1507 }
1508
Mitch Williamsef8693e2014-02-13 03:48:53 -08001509 /* poll until we see the reset actually happen */
1510 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001511 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1512 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Ashish Shahfd358862014-08-01 13:27:11 -07001513 if ((rstat_val != I40E_VFR_VFACTIVE) &&
1514 (rstat_val != I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00001515 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001516 msleep(I40EVF_RESET_WAIT_MS);
Greg Rose5eae00c2013-12-21 06:12:45 +00001517 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001518 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001519 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1520 goto continue_reset; /* act like the reset happened */
1521 }
1522
1523 /* wait until the reset is complete and the PF is responding to us */
1524 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1525 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1526 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Ashish Shahfd358862014-08-01 13:27:11 -07001527 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1528 (rstat_val == I40E_VFR_COMPLETED))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001529 break;
Mitch Williamsc88e38c2014-11-11 20:03:13 +00001530 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001531 }
1532 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williams6ba36a22014-08-01 13:27:14 -07001533 struct i40evf_mac_filter *f, *ftmp;
1534 struct i40evf_vlan_filter *fv, *fvtmp;
1535
Greg Rose5eae00c2013-12-21 06:12:45 +00001536 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001537 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsef8693e2014-02-13 03:48:53 -08001538 rstat_val);
1539 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1540
Mitch Williams169f4072014-04-01 07:11:50 +00001541 if (netif_running(adapter->netdev)) {
1542 set_bit(__I40E_DOWN, &adapter->vsi.state);
1543 i40evf_down(adapter);
1544 i40evf_free_traffic_irqs(adapter);
1545 i40evf_free_all_tx_resources(adapter);
1546 i40evf_free_all_rx_resources(adapter);
1547 }
Mitch Williams6ba36a22014-08-01 13:27:14 -07001548
1549 /* Delete all of the filters, both MAC and VLAN. */
1550 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1551 list) {
1552 list_del(&f->list);
1553 kfree(f);
1554 }
1555 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1556 list) {
1557 list_del(&fv->list);
1558 kfree(fv);
1559 }
1560
Mitch Williamsef8693e2014-02-13 03:48:53 -08001561 i40evf_free_misc_irq(adapter);
1562 i40evf_reset_interrupt_capability(adapter);
1563 i40evf_free_queues(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07001564 i40evf_free_q_vectors(adapter);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001565 kfree(adapter->vf_res);
1566 i40evf_shutdown_adminq(hw);
1567 adapter->netdev->flags &= ~IFF_UP;
1568 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1569 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001570 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001571
1572continue_reset:
1573 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1574
Greg Rose5eae00c2013-12-21 06:12:45 +00001575 i40evf_down(adapter);
1576 adapter->state = __I40EVF_RESETTING;
1577
1578 /* kill and reinit the admin queue */
1579 if (i40evf_shutdown_adminq(hw))
1580 dev_warn(&adapter->pdev->dev,
Mitch Williams75a64432014-11-11 20:02:42 +00001581 "%s: Failed to destroy the Admin Queue resources\n",
1582 __func__);
Greg Rose5eae00c2013-12-21 06:12:45 +00001583 err = i40evf_init_adminq(hw);
1584 if (err)
1585 dev_info(&adapter->pdev->dev, "%s: init_adminq failed: %d\n",
Mitch Williams75a64432014-11-11 20:02:42 +00001586 __func__, err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001587
1588 adapter->aq_pending = 0;
1589 adapter->aq_required = 0;
1590 i40evf_map_queues(adapter);
1591 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1592
1593 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1594
1595 if (netif_running(adapter->netdev)) {
1596 /* allocate transmit descriptors */
1597 err = i40evf_setup_all_tx_resources(adapter);
1598 if (err)
1599 goto reset_err;
1600
1601 /* allocate receive descriptors */
1602 err = i40evf_setup_all_rx_resources(adapter);
1603 if (err)
1604 goto reset_err;
1605
1606 i40evf_configure(adapter);
1607
1608 err = i40evf_up_complete(adapter);
1609 if (err)
1610 goto reset_err;
1611
1612 i40evf_irq_enable(adapter, true);
1613 }
1614 return;
1615reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001616 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001617 i40evf_close(adapter->netdev);
1618}
1619
1620/**
1621 * i40evf_adminq_task - worker thread to clean the admin queue
1622 * @work: pointer to work_struct containing our data
1623 **/
1624static void i40evf_adminq_task(struct work_struct *work)
1625{
1626 struct i40evf_adapter *adapter =
1627 container_of(work, struct i40evf_adapter, adminq_task);
1628 struct i40e_hw *hw = &adapter->hw;
1629 struct i40e_arq_event_info event;
1630 struct i40e_virtchnl_msg *v_msg;
1631 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001632 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001633 u16 pending;
1634
Mitch Williamsef8693e2014-02-13 03:48:53 -08001635 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1636 return;
1637
Mitch Williams1001dc32014-11-11 20:02:19 +00001638 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1639 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001640 if (!event.msg_buf)
Greg Rose5eae00c2013-12-21 06:12:45 +00001641 return;
Mitch Williams249c8b82014-05-10 04:49:04 +00001642
Greg Rose5eae00c2013-12-21 06:12:45 +00001643 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1644 do {
1645 ret = i40evf_clean_arq_element(hw, &event, &pending);
1646 if (ret)
1647 break; /* No event to process or error cleaning ARQ */
1648
1649 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1650 v_msg->v_retval, event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00001651 event.msg_len);
Mitch Williams75a64432014-11-11 20:02:42 +00001652 if (pending != 0)
Greg Rose5eae00c2013-12-21 06:12:45 +00001653 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
Greg Rose5eae00c2013-12-21 06:12:45 +00001654 } while (pending);
1655
Mitch Williams912257e2014-05-22 06:32:07 +00001656 /* check for error indications */
1657 val = rd32(hw, hw->aq.arq.len);
1658 oldval = val;
1659 if (val & I40E_VF_ARQLEN_ARQVFE_MASK) {
1660 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1661 val &= ~I40E_VF_ARQLEN_ARQVFE_MASK;
1662 }
1663 if (val & I40E_VF_ARQLEN_ARQOVFL_MASK) {
1664 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1665 val &= ~I40E_VF_ARQLEN_ARQOVFL_MASK;
1666 }
1667 if (val & I40E_VF_ARQLEN_ARQCRIT_MASK) {
1668 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1669 val &= ~I40E_VF_ARQLEN_ARQCRIT_MASK;
1670 }
1671 if (oldval != val)
1672 wr32(hw, hw->aq.arq.len, val);
1673
1674 val = rd32(hw, hw->aq.asq.len);
1675 oldval = val;
1676 if (val & I40E_VF_ATQLEN_ATQVFE_MASK) {
1677 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1678 val &= ~I40E_VF_ATQLEN_ATQVFE_MASK;
1679 }
1680 if (val & I40E_VF_ATQLEN_ATQOVFL_MASK) {
1681 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1682 val &= ~I40E_VF_ATQLEN_ATQOVFL_MASK;
1683 }
1684 if (val & I40E_VF_ATQLEN_ATQCRIT_MASK) {
1685 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1686 val &= ~I40E_VF_ATQLEN_ATQCRIT_MASK;
1687 }
1688 if (oldval != val)
1689 wr32(hw, hw->aq.asq.len, val);
1690
Greg Rose5eae00c2013-12-21 06:12:45 +00001691 /* re-enable Admin queue interrupt cause */
1692 i40evf_misc_irq_enable(adapter);
1693
1694 kfree(event.msg_buf);
1695}
1696
1697/**
1698 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1699 * @adapter: board private structure
1700 *
1701 * Free all transmit software resources
1702 **/
1703static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1704{
1705 int i;
1706
Mitch Williamscc052922014-10-25 03:24:34 +00001707 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001708 if (adapter->tx_rings[i]->desc)
1709 i40evf_free_tx_resources(adapter->tx_rings[i]);
Greg Rose5eae00c2013-12-21 06:12:45 +00001710}
1711
1712/**
1713 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1714 * @adapter: board private structure
1715 *
1716 * If this function returns with an error, then it's possible one or
1717 * more of the rings is populated (while the rest are not). It is the
1718 * callers duty to clean those orphaned rings.
1719 *
1720 * Return 0 on success, negative on failure
1721 **/
1722static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1723{
1724 int i, err = 0;
1725
Mitch Williamscc052922014-10-25 03:24:34 +00001726 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001727 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001728 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1729 if (!err)
1730 continue;
1731 dev_err(&adapter->pdev->dev,
1732 "%s: Allocation for Tx Queue %u failed\n",
1733 __func__, i);
1734 break;
1735 }
1736
1737 return err;
1738}
1739
1740/**
1741 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1742 * @adapter: board private structure
1743 *
1744 * If this function returns with an error, then it's possible one or
1745 * more of the rings is populated (while the rest are not). It is the
1746 * callers duty to clean those orphaned rings.
1747 *
1748 * Return 0 on success, negative on failure
1749 **/
1750static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1751{
1752 int i, err = 0;
1753
Mitch Williamscc052922014-10-25 03:24:34 +00001754 for (i = 0; i < adapter->num_active_queues; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001755 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001756 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1757 if (!err)
1758 continue;
1759 dev_err(&adapter->pdev->dev,
1760 "%s: Allocation for Rx Queue %u failed\n",
1761 __func__, i);
1762 break;
1763 }
1764 return err;
1765}
1766
1767/**
1768 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1769 * @adapter: board private structure
1770 *
1771 * Free all receive software resources
1772 **/
1773static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1774{
1775 int i;
1776
Mitch Williamscc052922014-10-25 03:24:34 +00001777 for (i = 0; i < adapter->num_active_queues; i++)
Greg Rose5eae00c2013-12-21 06:12:45 +00001778 if (adapter->rx_rings[i]->desc)
1779 i40evf_free_rx_resources(adapter->rx_rings[i]);
1780}
1781
1782/**
1783 * i40evf_open - Called when a network interface is made active
1784 * @netdev: network interface device structure
1785 *
1786 * Returns 0 on success, negative value on failure
1787 *
1788 * The open entry point is called when a network interface is made
1789 * active by the system (IFF_UP). At this point all resources needed
1790 * for transmit and receive operations are allocated, the interrupt
1791 * handler is registered with the OS, the watchdog timer is started,
1792 * and the stack is notified that the interface is ready.
1793 **/
1794static int i40evf_open(struct net_device *netdev)
1795{
1796 struct i40evf_adapter *adapter = netdev_priv(netdev);
1797 int err;
1798
Mitch Williamsef8693e2014-02-13 03:48:53 -08001799 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1800 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1801 return -EIO;
1802 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001803 if (adapter->state != __I40EVF_DOWN)
1804 return -EBUSY;
1805
1806 /* allocate transmit descriptors */
1807 err = i40evf_setup_all_tx_resources(adapter);
1808 if (err)
1809 goto err_setup_tx;
1810
1811 /* allocate receive descriptors */
1812 err = i40evf_setup_all_rx_resources(adapter);
1813 if (err)
1814 goto err_setup_rx;
1815
1816 /* clear any pending interrupts, may auto mask */
1817 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1818 if (err)
1819 goto err_req_irq;
1820
1821 i40evf_configure(adapter);
1822
1823 err = i40evf_up_complete(adapter);
1824 if (err)
1825 goto err_req_irq;
1826
1827 i40evf_irq_enable(adapter, true);
1828
1829 return 0;
1830
1831err_req_irq:
1832 i40evf_down(adapter);
1833 i40evf_free_traffic_irqs(adapter);
1834err_setup_rx:
1835 i40evf_free_all_rx_resources(adapter);
1836err_setup_tx:
1837 i40evf_free_all_tx_resources(adapter);
1838
1839 return err;
1840}
1841
1842/**
1843 * i40evf_close - Disables a network interface
1844 * @netdev: network interface device structure
1845 *
1846 * Returns 0, this is not allowed to fail
1847 *
1848 * The close entry point is called when an interface is de-activated
1849 * by the OS. The hardware is still under the drivers control, but
1850 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1851 * are freed, along with all transmit and receive resources.
1852 **/
1853static int i40evf_close(struct net_device *netdev)
1854{
1855 struct i40evf_adapter *adapter = netdev_priv(netdev);
1856
Mitch Williamsef8693e2014-02-13 03:48:53 -08001857 if (adapter->state <= __I40EVF_DOWN)
1858 return 0;
1859
Mitch Williamsef8693e2014-02-13 03:48:53 -08001860
Greg Rose5eae00c2013-12-21 06:12:45 +00001861 set_bit(__I40E_DOWN, &adapter->vsi.state);
1862
1863 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001864 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001865 i40evf_free_traffic_irqs(adapter);
1866
1867 i40evf_free_all_tx_resources(adapter);
1868 i40evf_free_all_rx_resources(adapter);
1869
1870 return 0;
1871}
1872
1873/**
1874 * i40evf_get_stats - Get System Network Statistics
1875 * @netdev: network interface device structure
1876 *
1877 * Returns the address of the device statistics structure.
1878 * The statistics are actually updated from the timer callback.
1879 **/
1880static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1881{
1882 struct i40evf_adapter *adapter = netdev_priv(netdev);
1883
1884 /* only return the current stats */
1885 return &adapter->net_stats;
1886}
1887
1888/**
1889 * i40evf_reinit_locked - Software reinit
1890 * @adapter: board private structure
1891 *
1892 * Reinititalizes the ring structures in response to a software configuration
1893 * change. Roughly the same as close followed by open, but skips releasing
1894 * and reallocating the interrupts.
1895 **/
1896void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1897{
1898 struct net_device *netdev = adapter->netdev;
1899 int err;
1900
1901 WARN_ON(in_interrupt());
1902
Greg Rose5eae00c2013-12-21 06:12:45 +00001903 i40evf_down(adapter);
1904
1905 /* allocate transmit descriptors */
1906 err = i40evf_setup_all_tx_resources(adapter);
1907 if (err)
1908 goto err_reinit;
1909
1910 /* allocate receive descriptors */
1911 err = i40evf_setup_all_rx_resources(adapter);
1912 if (err)
1913 goto err_reinit;
1914
1915 i40evf_configure(adapter);
1916
1917 err = i40evf_up_complete(adapter);
1918 if (err)
1919 goto err_reinit;
1920
1921 i40evf_irq_enable(adapter, true);
1922 return;
1923
1924err_reinit:
Mitch Williams80e72892014-05-10 04:49:06 +00001925 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001926 i40evf_close(netdev);
1927}
1928
1929/**
1930 * i40evf_change_mtu - Change the Maximum Transfer Unit
1931 * @netdev: network interface device structure
1932 * @new_mtu: new value for maximum frame size
1933 *
1934 * Returns 0 on success, negative on failure
1935 **/
1936static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1937{
1938 struct i40evf_adapter *adapter = netdev_priv(netdev);
1939 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1940
1941 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1942 return -EINVAL;
1943
1944 /* must set new MTU before calling down or up */
1945 netdev->mtu = new_mtu;
1946 i40evf_reinit_locked(adapter);
1947 return 0;
1948}
1949
1950static const struct net_device_ops i40evf_netdev_ops = {
1951 .ndo_open = i40evf_open,
1952 .ndo_stop = i40evf_close,
1953 .ndo_start_xmit = i40evf_xmit_frame,
1954 .ndo_get_stats = i40evf_get_stats,
1955 .ndo_set_rx_mode = i40evf_set_rx_mode,
1956 .ndo_validate_addr = eth_validate_addr,
1957 .ndo_set_mac_address = i40evf_set_mac,
1958 .ndo_change_mtu = i40evf_change_mtu,
1959 .ndo_tx_timeout = i40evf_tx_timeout,
1960 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1961 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1962};
1963
1964/**
1965 * i40evf_check_reset_complete - check that VF reset is complete
1966 * @hw: pointer to hw struct
1967 *
1968 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1969 **/
1970static int i40evf_check_reset_complete(struct i40e_hw *hw)
1971{
1972 u32 rstat;
1973 int i;
1974
1975 for (i = 0; i < 100; i++) {
Ashish Shahfd358862014-08-01 13:27:11 -07001976 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
1977 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1978 if ((rstat == I40E_VFR_VFACTIVE) ||
1979 (rstat == I40E_VFR_COMPLETED))
Greg Rose5eae00c2013-12-21 06:12:45 +00001980 return 0;
Neerav Parikhf98a2002014-09-13 07:40:44 +00001981 usleep_range(10, 20);
Greg Rose5eae00c2013-12-21 06:12:45 +00001982 }
1983 return -EBUSY;
1984}
1985
1986/**
1987 * i40evf_init_task - worker thread to perform delayed initialization
1988 * @work: pointer to work_struct containing our data
1989 *
1990 * This task completes the work that was begun in probe. Due to the nature
1991 * of VF-PF communications, we may need to wait tens of milliseconds to get
1992 * reponses back from the PF. Rather than busy-wait in probe and bog down the
1993 * whole system, we'll do it in a task so we can sleep.
1994 * This task only runs during driver init. Once we've established
1995 * communications with the PF driver and set up our netdev, the watchdog
1996 * takes over.
1997 **/
1998static void i40evf_init_task(struct work_struct *work)
1999{
2000 struct i40evf_adapter *adapter = container_of(work,
2001 struct i40evf_adapter,
2002 init_task.work);
2003 struct net_device *netdev = adapter->netdev;
2004 struct i40evf_mac_filter *f;
2005 struct i40e_hw *hw = &adapter->hw;
2006 struct pci_dev *pdev = adapter->pdev;
2007 int i, err, bufsz;
2008
2009 switch (adapter->state) {
2010 case __I40EVF_STARTUP:
2011 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08002012 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2013 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00002014 err = i40e_set_mac_type(hw);
2015 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002016 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2017 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002018 goto err;
2019 }
2020 err = i40evf_check_reset_complete(hw);
2021 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002022 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002023 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002024 goto err;
2025 }
2026 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2027 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2028 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2029 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2030
2031 err = i40evf_init_adminq(hw);
2032 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002033 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2034 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002035 goto err;
2036 }
2037 err = i40evf_send_api_ver(adapter);
2038 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002039 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002040 i40evf_shutdown_adminq(hw);
2041 goto err;
2042 }
2043 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2044 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002045 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002046 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002047 dev_err(&pdev->dev, "Admin queue command never completed\n");
Mitch Williams906a6932014-11-13 03:06:12 +00002048 i40evf_shutdown_adminq(hw);
2049 adapter->state = __I40EVF_STARTUP;
Greg Rose5eae00c2013-12-21 06:12:45 +00002050 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002051 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002052
2053 /* aq msg sent, awaiting reply */
2054 err = i40evf_verify_api_ver(adapter);
2055 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002056 dev_info(&pdev->dev, "Unable to verify API version (%d), retrying\n",
Mitch Williams75a64432014-11-11 20:02:42 +00002057 err);
Mitch Williams56f99202014-06-04 04:22:41 +00002058 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2059 dev_info(&pdev->dev, "Resending request\n");
2060 err = i40evf_send_api_ver(adapter);
2061 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002062 goto err;
2063 }
2064 err = i40evf_send_vf_config_msg(adapter);
2065 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002066 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002067 err);
2068 goto err;
2069 }
2070 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2071 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002072 case __I40EVF_INIT_GET_RESOURCES:
2073 /* aq msg sent, awaiting reply */
2074 if (!adapter->vf_res) {
2075 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2076 (I40E_MAX_VF_VSI *
2077 sizeof(struct i40e_virtchnl_vsi_resource));
2078 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002079 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002080 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002081 }
2082 err = i40evf_get_vf_config(adapter);
Mitch Williams906a6932014-11-13 03:06:12 +00002083 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2084 dev_info(&pdev->dev, "Resending VF config request\n");
2085 err = i40evf_send_vf_config_msg(adapter);
2086 goto err;
2087 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002088 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002089 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2090 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002091 goto err_alloc;
2092 }
2093 adapter->state = __I40EVF_INIT_SW;
2094 break;
2095 default:
2096 goto err_alloc;
2097 }
2098 /* got VF config message back from PF, now we can parse it */
2099 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2100 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2101 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2102 }
2103 if (!adapter->vsi_res) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002104 dev_err(&pdev->dev, "No LAN VSI found\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002105 goto err_alloc;
2106 }
2107
2108 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2109
Greg Rose5eae00c2013-12-21 06:12:45 +00002110 netdev->netdev_ops = &i40evf_netdev_ops;
2111 i40evf_set_ethtool_ops(netdev);
2112 netdev->watchdog_timeo = 5 * HZ;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002113 netdev->features |= NETIF_F_HIGHDMA |
2114 NETIF_F_SG |
Greg Rose5eae00c2013-12-21 06:12:45 +00002115 NETIF_F_IP_CSUM |
2116 NETIF_F_SCTP_CSUM |
2117 NETIF_F_IPV6_CSUM |
2118 NETIF_F_TSO |
2119 NETIF_F_TSO6 |
Greg Rose3415e8c2014-01-30 12:40:27 +00002120 NETIF_F_RXCSUM |
Greg Rose5eae00c2013-12-21 06:12:45 +00002121 NETIF_F_GRO;
2122
2123 if (adapter->vf_res->vf_offload_flags
2124 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2125 netdev->vlan_features = netdev->features;
2126 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2127 NETIF_F_HW_VLAN_CTAG_RX |
2128 NETIF_F_HW_VLAN_CTAG_FILTER;
2129 }
2130
Greg Rose3415e8c2014-01-30 12:40:27 +00002131 /* copy netdev features into list of user selectable features */
2132 netdev->hw_features |= netdev->features;
2133 netdev->hw_features &= ~NETIF_F_RXCSUM;
2134
Greg Rose5eae00c2013-12-21 06:12:45 +00002135 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002136 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002137 adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002138 random_ether_addr(adapter->hw.mac.addr);
2139 }
Greg Rose9a173902014-05-22 06:32:02 +00002140 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2141 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002142
Greg Rose5eae00c2013-12-21 06:12:45 +00002143 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams348d4992014-11-11 20:02:52 +00002144 if (!f)
Greg Rose5eae00c2013-12-21 06:12:45 +00002145 goto err_sw_init;
2146
Greg Rose9a173902014-05-22 06:32:02 +00002147 ether_addr_copy(f->macaddr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002148 f->add = true;
2149 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2150
2151 list_add(&f->list, &adapter->mac_filter_list);
2152
2153 init_timer(&adapter->watchdog_timer);
2154 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2155 adapter->watchdog_timer.data = (unsigned long)adapter;
2156 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2157
Mitch Williamscc052922014-10-25 03:24:34 +00002158 adapter->num_active_queues = min_t(int,
2159 adapter->vsi_res->num_queue_pairs,
2160 (int)(num_online_cpus()));
Mitch Williamsd732a182014-04-24 06:41:37 +00002161 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2162 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002163 err = i40evf_init_interrupt_scheme(adapter);
2164 if (err)
2165 goto err_sw_init;
2166 i40evf_map_rings_to_vectors(adapter);
2167 i40evf_configure_rss(adapter);
2168 err = i40evf_request_misc_irq(adapter);
2169 if (err)
2170 goto err_sw_init;
2171
2172 netif_carrier_off(netdev);
2173
Greg Rose5eae00c2013-12-21 06:12:45 +00002174 adapter->vsi.id = adapter->vsi_res->vsi_id;
2175 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2176 adapter->vsi.back = adapter;
2177 adapter->vsi.base_vector = 1;
2178 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williamsca99eb92014-04-04 04:43:07 +00002179 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2180 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2181 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2182 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Greg Rose5eae00c2013-12-21 06:12:45 +00002183 adapter->vsi.netdev = adapter->netdev;
2184
Mitch Williamsef8693e2014-02-13 03:48:53 -08002185 if (!adapter->netdev_registered) {
2186 err = register_netdev(netdev);
2187 if (err)
2188 goto err_register;
2189 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002190
2191 adapter->netdev_registered = true;
2192
2193 netif_tx_stop_all_queues(netdev);
2194
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002195 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002196 if (netdev->features & NETIF_F_GRO)
2197 dev_info(&pdev->dev, "GRO is enabled\n");
2198
2199 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2200 adapter->state = __I40EVF_DOWN;
2201 set_bit(__I40E_DOWN, &adapter->vsi.state);
2202 i40evf_misc_irq_enable(adapter);
2203 return;
2204restart:
2205 schedule_delayed_work(&adapter->init_task,
2206 msecs_to_jiffies(50));
2207 return;
2208
2209err_register:
2210 i40evf_free_misc_irq(adapter);
2211err_sw_init:
2212 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002213err_alloc:
2214 kfree(adapter->vf_res);
2215 adapter->vf_res = NULL;
2216err:
2217 /* Things went into the weeds, so try again later */
2218 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002219 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002220 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002221 return; /* do not reschedule */
2222 }
2223 schedule_delayed_work(&adapter->init_task, HZ * 3);
Greg Rose5eae00c2013-12-21 06:12:45 +00002224}
2225
2226/**
2227 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2228 * @pdev: pci device structure
2229 **/
2230static void i40evf_shutdown(struct pci_dev *pdev)
2231{
2232 struct net_device *netdev = pci_get_drvdata(pdev);
2233
2234 netif_device_detach(netdev);
2235
2236 if (netif_running(netdev))
2237 i40evf_close(netdev);
2238
2239#ifdef CONFIG_PM
2240 pci_save_state(pdev);
2241
2242#endif
2243 pci_disable_device(pdev);
2244}
2245
2246/**
2247 * i40evf_probe - Device Initialization Routine
2248 * @pdev: PCI device information struct
2249 * @ent: entry in i40evf_pci_tbl
2250 *
2251 * Returns 0 on success, negative on failure
2252 *
2253 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2254 * The OS initialization, configuring of the adapter private structure,
2255 * and a hardware reset occur.
2256 **/
2257static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2258{
2259 struct net_device *netdev;
2260 struct i40evf_adapter *adapter = NULL;
2261 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002262 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002263
2264 err = pci_enable_device(pdev);
2265 if (err)
2266 return err;
2267
Mitch Williams64942942014-02-11 08:26:33 +00002268 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002269 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002270 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2271 if (err) {
2272 dev_err(&pdev->dev,
2273 "DMA configuration failed: 0x%x\n", err);
2274 goto err_dma;
2275 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002276 }
2277
2278 err = pci_request_regions(pdev, i40evf_driver_name);
2279 if (err) {
2280 dev_err(&pdev->dev,
2281 "pci_request_regions failed 0x%x\n", err);
2282 goto err_pci_reg;
2283 }
2284
2285 pci_enable_pcie_error_reporting(pdev);
2286
2287 pci_set_master(pdev);
2288
2289 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2290 MAX_TX_QUEUES);
2291 if (!netdev) {
2292 err = -ENOMEM;
2293 goto err_alloc_etherdev;
2294 }
2295
2296 SET_NETDEV_DEV(netdev, &pdev->dev);
2297
2298 pci_set_drvdata(pdev, netdev);
2299 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002300
2301 adapter->netdev = netdev;
2302 adapter->pdev = pdev;
2303
2304 hw = &adapter->hw;
2305 hw->back = adapter;
2306
2307 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2308 adapter->state = __I40EVF_STARTUP;
2309
2310 /* Call save state here because it relies on the adapter struct. */
2311 pci_save_state(pdev);
2312
2313 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2314 pci_resource_len(pdev, 0));
2315 if (!hw->hw_addr) {
2316 err = -EIO;
2317 goto err_ioremap;
2318 }
2319 hw->vendor_id = pdev->vendor;
2320 hw->device_id = pdev->device;
2321 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2322 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2323 hw->subsystem_device_id = pdev->subsystem_device;
2324 hw->bus.device = PCI_SLOT(pdev->devfn);
2325 hw->bus.func = PCI_FUNC(pdev->devfn);
2326
Serey Kong8bb1a542014-08-01 13:27:15 -07002327 INIT_LIST_HEAD(&adapter->mac_filter_list);
2328 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2329
Greg Rose5eae00c2013-12-21 06:12:45 +00002330 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2331 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2332 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2333 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2334 schedule_delayed_work(&adapter->init_task, 10);
2335
2336 return 0;
2337
2338err_ioremap:
2339 free_netdev(netdev);
2340err_alloc_etherdev:
2341 pci_release_regions(pdev);
2342err_pci_reg:
2343err_dma:
2344 pci_disable_device(pdev);
2345 return err;
2346}
2347
2348#ifdef CONFIG_PM
2349/**
2350 * i40evf_suspend - Power management suspend routine
2351 * @pdev: PCI device information struct
2352 * @state: unused
2353 *
2354 * Called when the system (VM) is entering sleep/suspend.
2355 **/
2356static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2357{
2358 struct net_device *netdev = pci_get_drvdata(pdev);
2359 struct i40evf_adapter *adapter = netdev_priv(netdev);
2360 int retval = 0;
2361
2362 netif_device_detach(netdev);
2363
2364 if (netif_running(netdev)) {
2365 rtnl_lock();
2366 i40evf_down(adapter);
2367 rtnl_unlock();
2368 }
2369 i40evf_free_misc_irq(adapter);
2370 i40evf_reset_interrupt_capability(adapter);
2371
2372 retval = pci_save_state(pdev);
2373 if (retval)
2374 return retval;
2375
2376 pci_disable_device(pdev);
2377
2378 return 0;
2379}
2380
2381/**
2382 * i40evf_resume - Power managment resume routine
2383 * @pdev: PCI device information struct
2384 *
2385 * Called when the system (VM) is resumed from sleep/suspend.
2386 **/
2387static int i40evf_resume(struct pci_dev *pdev)
2388{
2389 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2390 struct net_device *netdev = adapter->netdev;
2391 u32 err;
2392
2393 pci_set_power_state(pdev, PCI_D0);
2394 pci_restore_state(pdev);
2395 /* pci_restore_state clears dev->state_saved so call
2396 * pci_save_state to restore it.
2397 */
2398 pci_save_state(pdev);
2399
2400 err = pci_enable_device_mem(pdev);
2401 if (err) {
2402 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2403 return err;
2404 }
2405 pci_set_master(pdev);
2406
2407 rtnl_lock();
2408 err = i40evf_set_interrupt_capability(adapter);
2409 if (err) {
2410 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2411 return err;
2412 }
2413 err = i40evf_request_misc_irq(adapter);
2414 rtnl_unlock();
2415 if (err) {
2416 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2417 return err;
2418 }
2419
2420 schedule_work(&adapter->reset_task);
2421
2422 netif_device_attach(netdev);
2423
2424 return err;
2425}
2426
2427#endif /* CONFIG_PM */
2428/**
2429 * i40evf_remove - Device Removal Routine
2430 * @pdev: PCI device information struct
2431 *
2432 * i40evf_remove is called by the PCI subsystem to alert the driver
2433 * that it should release a PCI device. The could be caused by a
2434 * Hot-Plug event, or because the driver is going to be removed from
2435 * memory.
2436 **/
2437static void i40evf_remove(struct pci_dev *pdev)
2438{
2439 struct net_device *netdev = pci_get_drvdata(pdev);
2440 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002441 struct i40evf_mac_filter *f, *ftmp;
Greg Rose5eae00c2013-12-21 06:12:45 +00002442 struct i40e_hw *hw = &adapter->hw;
2443
2444 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002445 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002446
2447 if (adapter->netdev_registered) {
2448 unregister_netdev(netdev);
2449 adapter->netdev_registered = false;
2450 }
2451 adapter->state = __I40EVF_REMOVE;
2452
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002453 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002454 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002455 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002456 i40evf_reset_interrupt_capability(adapter);
Mitch Williamsd31944d2014-08-01 13:27:13 -07002457 i40evf_free_q_vectors(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002458 }
2459
Mitch Williamse5d17c32014-06-04 04:22:38 +00002460 if (adapter->watchdog_timer.function)
2461 del_timer_sync(&adapter->watchdog_timer);
2462
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002463 flush_scheduled_work();
2464
Greg Rose5eae00c2013-12-21 06:12:45 +00002465 if (hw->aq.asq.count)
2466 i40evf_shutdown_adminq(hw);
2467
2468 iounmap(hw->hw_addr);
2469 pci_release_regions(pdev);
2470
2471 i40evf_free_queues(adapter);
2472 kfree(adapter->vf_res);
Mitch Williams6ba36a22014-08-01 13:27:14 -07002473 /* If we got removed before an up/down sequence, we've got a filter
2474 * hanging out there that we need to get rid of.
2475 */
2476 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2477 list_del(&f->list);
2478 kfree(f);
2479 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002480
2481 free_netdev(netdev);
2482
2483 pci_disable_pcie_error_reporting(pdev);
2484
2485 pci_disable_device(pdev);
2486}
2487
2488static struct pci_driver i40evf_driver = {
2489 .name = i40evf_driver_name,
2490 .id_table = i40evf_pci_tbl,
2491 .probe = i40evf_probe,
2492 .remove = i40evf_remove,
2493#ifdef CONFIG_PM
2494 .suspend = i40evf_suspend,
2495 .resume = i40evf_resume,
2496#endif
2497 .shutdown = i40evf_shutdown,
2498};
2499
2500/**
2501 * i40e_init_module - Driver Registration Routine
2502 *
2503 * i40e_init_module is the first routine called when the driver is
2504 * loaded. All it does is register with the PCI subsystem.
2505 **/
2506static int __init i40evf_init_module(void)
2507{
2508 int ret;
Mitch Williams75a64432014-11-11 20:02:42 +00002509
Greg Rose5eae00c2013-12-21 06:12:45 +00002510 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
Mitch Williams75a64432014-11-11 20:02:42 +00002511 i40evf_driver_version);
Greg Rose5eae00c2013-12-21 06:12:45 +00002512
2513 pr_info("%s\n", i40evf_copyright);
2514
2515 ret = pci_register_driver(&i40evf_driver);
2516 return ret;
2517}
2518
2519module_init(i40evf_init_module);
2520
2521/**
2522 * i40e_exit_module - Driver Exit Cleanup Routine
2523 *
2524 * i40e_exit_module is called just before the driver is removed
2525 * from memory.
2526 **/
2527static void __exit i40evf_exit_module(void)
2528{
2529 pci_unregister_driver(&i40evf_driver);
2530}
2531
2532module_exit(i40evf_exit_module);
2533
2534/* i40evf_main.c */