blob: 937785db7dff481a002eccd33d3cc76ad4945584 [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 Sullivan4e776382014-06-04 08:45:29 +000039#define DRV_VERSION "0.9.40"
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 */
52static DEFINE_PCI_DEVICE_TABLE(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;
188 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
189
190 /* read flush */
191 rd32(hw, I40E_VFGEN_RSTAT);
192
193 synchronize_irq(adapter->msix_entries[0].vector);
194}
195
196/**
197 * i40evf_misc_irq_enable - Enable default interrupt generation settings
198 * @adapter: board private structure
199 **/
200static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
201{
202 struct i40e_hw *hw = &adapter->hw;
203 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
204 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
205 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA_ADMINQ_MASK);
206
207 /* read flush */
208 rd32(hw, I40E_VFGEN_RSTAT);
209}
210
211/**
212 * i40evf_irq_disable - Mask off interrupt generation on the NIC
213 * @adapter: board private structure
214 **/
215static void i40evf_irq_disable(struct i40evf_adapter *adapter)
216{
217 int i;
218 struct i40e_hw *hw = &adapter->hw;
219
Mitch Williamsdbb01c82014-02-20 19:29:07 -0800220 if (!adapter->msix_entries)
221 return;
222
Greg Rose5eae00c2013-12-21 06:12:45 +0000223 for (i = 1; i < adapter->num_msix_vectors; i++) {
224 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
225 synchronize_irq(adapter->msix_entries[i].vector);
226 }
227 /* read flush */
228 rd32(hw, I40E_VFGEN_RSTAT);
229
230}
231
232/**
233 * i40evf_irq_enable_queues - Enable interrupt for specified queues
234 * @adapter: board private structure
235 * @mask: bitmap of queues to enable
236 **/
237void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
238{
239 struct i40e_hw *hw = &adapter->hw;
240 int i;
241
242 for (i = 1; i < adapter->num_msix_vectors; i++) {
243 if (mask & (1 << (i - 1))) {
244 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
245 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
246 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
247 }
248 }
249}
250
251/**
252 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
253 * @adapter: board private structure
254 * @mask: bitmap of vectors to trigger
255 **/
256static void i40evf_fire_sw_int(struct i40evf_adapter *adapter,
257 u32 mask)
258{
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;
400 int rxr_remaining = adapter->vsi_res->num_queue_pairs;
401 int txr_remaining = adapter->vsi_res->num_queue_pairs;
402 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;
554 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
555
556 for (i = 0; i < q_vectors; i++) {
557 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
558 NULL);
559 free_irq(adapter->msix_entries[i+1].vector,
560 adapter->q_vector[i]);
561 }
562}
563
564/**
565 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
566 * @adapter: board private structure
567 *
568 * Frees MSI-X vector 0.
569 **/
570static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
571{
572 struct net_device *netdev = adapter->netdev;
573
574 free_irq(adapter->msix_entries[0].vector, netdev);
575}
576
577/**
578 * i40evf_configure_tx - Configure Transmit Unit after Reset
579 * @adapter: board private structure
580 *
581 * Configure the Tx unit of the MAC after a reset.
582 **/
583static void i40evf_configure_tx(struct i40evf_adapter *adapter)
584{
585 struct i40e_hw *hw = &adapter->hw;
586 int i;
587 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
588 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
589}
590
591/**
592 * i40evf_configure_rx - Configure Receive Unit after Reset
593 * @adapter: board private structure
594 *
595 * Configure the Rx unit of the MAC after a reset.
596 **/
597static void i40evf_configure_rx(struct i40evf_adapter *adapter)
598{
599 struct i40e_hw *hw = &adapter->hw;
600 struct net_device *netdev = adapter->netdev;
601 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
602 int i;
603 int rx_buf_len;
604
605
606 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
607 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
608
609 /* Decide whether to use packet split mode or not */
610 if (netdev->mtu > ETH_DATA_LEN) {
611 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
612 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
613 else
614 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
615 } else {
616 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
617 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
618 else
619 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
620 }
621
622 /* Set the RX buffer length according to the mode */
623 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
624 rx_buf_len = I40E_RX_HDR_SIZE;
625 } else {
626 if (netdev->mtu <= ETH_DATA_LEN)
627 rx_buf_len = I40EVF_RXBUFFER_2048;
628 else
629 rx_buf_len = ALIGN(max_frame, 1024);
630 }
631
632 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
633 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
634 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
635 }
636}
637
638/**
639 * i40evf_find_vlan - Search filter list for specific vlan filter
640 * @adapter: board private structure
641 * @vlan: vlan tag
642 *
643 * Returns ptr to the filter object or NULL
644 **/
645static struct
646i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
647{
648 struct i40evf_vlan_filter *f;
649
650 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
651 if (vlan == f->vlan)
652 return f;
653 }
654 return NULL;
655}
656
657/**
658 * i40evf_add_vlan - Add a vlan filter to the list
659 * @adapter: board private structure
660 * @vlan: VLAN tag
661 *
662 * Returns ptr to the filter object or NULL when no memory available.
663 **/
664static struct
665i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
666{
667 struct i40evf_vlan_filter *f;
668
669 f = i40evf_find_vlan(adapter, vlan);
670 if (NULL == f) {
671 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000672 if (NULL == f)
Greg Rose5eae00c2013-12-21 06:12:45 +0000673 return NULL;
Mitch Williams249c8b82014-05-10 04:49:04 +0000674
Greg Rose5eae00c2013-12-21 06:12:45 +0000675 f->vlan = vlan;
676
677 INIT_LIST_HEAD(&f->list);
678 list_add(&f->list, &adapter->vlan_filter_list);
679 f->add = true;
680 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
681 }
682
683 return f;
684}
685
686/**
687 * i40evf_del_vlan - Remove a vlan filter from the list
688 * @adapter: board private structure
689 * @vlan: VLAN tag
690 **/
691static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
692{
693 struct i40evf_vlan_filter *f;
694
695 f = i40evf_find_vlan(adapter, vlan);
696 if (f) {
697 f->remove = true;
698 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
699 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000700}
701
702/**
703 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
704 * @netdev: network device struct
705 * @vid: VLAN tag
706 **/
707static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
708 __always_unused __be16 proto, u16 vid)
709{
710 struct i40evf_adapter *adapter = netdev_priv(netdev);
711
712 if (i40evf_add_vlan(adapter, vid) == NULL)
713 return -ENOMEM;
714 return 0;
715}
716
717/**
718 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
719 * @netdev: network device struct
720 * @vid: VLAN tag
721 **/
722static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
723 __always_unused __be16 proto, u16 vid)
724{
725 struct i40evf_adapter *adapter = netdev_priv(netdev);
726
727 i40evf_del_vlan(adapter, vid);
728 return 0;
729}
730
731/**
732 * i40evf_find_filter - Search filter list for specific mac filter
733 * @adapter: board private structure
734 * @macaddr: the MAC address
735 *
736 * Returns ptr to the filter object or NULL
737 **/
738static struct
739i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
740 u8 *macaddr)
741{
742 struct i40evf_mac_filter *f;
743
744 if (!macaddr)
745 return NULL;
746
747 list_for_each_entry(f, &adapter->mac_filter_list, list) {
748 if (ether_addr_equal(macaddr, f->macaddr))
749 return f;
750 }
751 return NULL;
752}
753
754/**
755 * i40e_add_filter - Add a mac filter to the filter list
756 * @adapter: board private structure
757 * @macaddr: the MAC address
758 *
759 * Returns ptr to the filter object or NULL when no memory available.
760 **/
761static struct
762i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
763 u8 *macaddr)
764{
765 struct i40evf_mac_filter *f;
766
767 if (!macaddr)
768 return NULL;
769
770 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
771 &adapter->crit_section))
Mitch Williamsb65476c2014-07-09 07:46:14 +0000772 udelay(1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000773
774 f = i40evf_find_filter(adapter, macaddr);
775 if (NULL == f) {
776 f = kzalloc(sizeof(*f), GFP_ATOMIC);
777 if (NULL == f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000778 clear_bit(__I40EVF_IN_CRITICAL_TASK,
779 &adapter->crit_section);
780 return NULL;
781 }
782
Greg Rose9a173902014-05-22 06:32:02 +0000783 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000784
785 list_add(&f->list, &adapter->mac_filter_list);
786 f->add = true;
787 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
788 }
789
790 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
791 return f;
792}
793
794/**
795 * i40evf_set_mac - NDO callback to set port mac address
796 * @netdev: network interface device structure
797 * @p: pointer to an address structure
798 *
799 * Returns 0 on success, negative on failure
800 **/
801static int i40evf_set_mac(struct net_device *netdev, void *p)
802{
803 struct i40evf_adapter *adapter = netdev_priv(netdev);
804 struct i40e_hw *hw = &adapter->hw;
805 struct i40evf_mac_filter *f;
806 struct sockaddr *addr = p;
807
808 if (!is_valid_ether_addr(addr->sa_data))
809 return -EADDRNOTAVAIL;
810
811 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
812 return 0;
813
814 f = i40evf_add_filter(adapter, addr->sa_data);
815 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000816 ether_addr_copy(hw->mac.addr, addr->sa_data);
817 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000818 }
819
820 return (f == NULL) ? -ENOMEM : 0;
821}
822
823/**
824 * i40evf_set_rx_mode - NDO callback to set the netdev filters
825 * @netdev: network interface device structure
826 **/
827static void i40evf_set_rx_mode(struct net_device *netdev)
828{
829 struct i40evf_adapter *adapter = netdev_priv(netdev);
830 struct i40evf_mac_filter *f, *ftmp;
831 struct netdev_hw_addr *uca;
832 struct netdev_hw_addr *mca;
833
834 /* add addr if not already in the filter list */
835 netdev_for_each_uc_addr(uca, netdev) {
836 i40evf_add_filter(adapter, uca->addr);
837 }
838 netdev_for_each_mc_addr(mca, netdev) {
839 i40evf_add_filter(adapter, mca->addr);
840 }
841
842 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
843 &adapter->crit_section))
Mitch Williamsb65476c2014-07-09 07:46:14 +0000844 udelay(1);
Greg Rose5eae00c2013-12-21 06:12:45 +0000845 /* remove filter if not in netdev list */
846 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
847 bool found = false;
848
Tobias Klauserdc5f2de2014-05-20 08:23:06 +0000849 if (is_multicast_ether_addr(f->macaddr)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000850 netdev_for_each_mc_addr(mca, netdev) {
851 if (ether_addr_equal(mca->addr, f->macaddr)) {
852 found = true;
853 break;
854 }
855 }
856 } else {
857 netdev_for_each_uc_addr(uca, netdev) {
858 if (ether_addr_equal(uca->addr, f->macaddr)) {
859 found = true;
860 break;
861 }
862 }
863 }
864 if (found) {
865 f->remove = true;
866 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
867 }
868 }
869 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
870}
871
872/**
873 * i40evf_napi_enable_all - enable NAPI on all queue vectors
874 * @adapter: board private structure
875 **/
876static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
877{
878 int q_idx;
879 struct i40e_q_vector *q_vector;
880 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
881
882 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
883 struct napi_struct *napi;
884 q_vector = adapter->q_vector[q_idx];
885 napi = &q_vector->napi;
886 napi_enable(napi);
887 }
888}
889
890/**
891 * i40evf_napi_disable_all - disable NAPI on all queue vectors
892 * @adapter: board private structure
893 **/
894static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
895{
896 int q_idx;
897 struct i40e_q_vector *q_vector;
898 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
899
900 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
901 q_vector = adapter->q_vector[q_idx];
902 napi_disable(&q_vector->napi);
903 }
904}
905
906/**
907 * i40evf_configure - set up transmit and receive data structures
908 * @adapter: board private structure
909 **/
910static void i40evf_configure(struct i40evf_adapter *adapter)
911{
912 struct net_device *netdev = adapter->netdev;
913 int i;
914
915 i40evf_set_rx_mode(netdev);
916
917 i40evf_configure_tx(adapter);
918 i40evf_configure_rx(adapter);
919 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
920
921 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
922 struct i40e_ring *ring = adapter->rx_rings[i];
923 i40evf_alloc_rx_buffers(ring, ring->count);
924 ring->next_to_use = ring->count - 1;
925 writel(ring->next_to_use, ring->tail);
926 }
927}
928
929/**
930 * i40evf_up_complete - Finish the last steps of bringing up a connection
931 * @adapter: board private structure
932 **/
933static int i40evf_up_complete(struct i40evf_adapter *adapter)
934{
935 adapter->state = __I40EVF_RUNNING;
936 clear_bit(__I40E_DOWN, &adapter->vsi.state);
937
938 i40evf_napi_enable_all(adapter);
939
940 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
941 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
942 return 0;
943}
944
945/**
946 * i40evf_clean_all_rx_rings - Free Rx Buffers for all queues
947 * @adapter: board private structure
948 **/
949static void i40evf_clean_all_rx_rings(struct i40evf_adapter *adapter)
950{
951 int i;
952
953 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
954 i40evf_clean_rx_ring(adapter->rx_rings[i]);
955}
956
957/**
958 * i40evf_clean_all_tx_rings - Free Tx Buffers for all queues
959 * @adapter: board private structure
960 **/
961static void i40evf_clean_all_tx_rings(struct i40evf_adapter *adapter)
962{
963 int i;
964
965 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
966 i40evf_clean_tx_ring(adapter->tx_rings[i]);
967}
968
969/**
970 * i40e_down - Shutdown the connection processing
971 * @adapter: board private structure
972 **/
973void i40evf_down(struct i40evf_adapter *adapter)
974{
975 struct net_device *netdev = adapter->netdev;
976 struct i40evf_mac_filter *f;
977
Mitch Williamsddf0b3a2014-05-22 06:31:46 +0000978 if (adapter->state == __I40EVF_DOWN)
979 return;
980
Mitch Williamsef8693e2014-02-13 03:48:53 -0800981 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +0000982 list_for_each_entry(f, &adapter->mac_filter_list, list) {
983 f->remove = true;
984 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800985 /* remove all VLAN filters */
986 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
987 f->remove = true;
988 }
Mitch Williamsef8693e2014-02-13 03:48:53 -0800989 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
990 adapter->state != __I40EVF_RESETTING) {
991 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800992 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800993 /* disable receives */
994 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
995 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
996 msleep(20);
997 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000998 netif_tx_disable(netdev);
999
1000 netif_tx_stop_all_queues(netdev);
1001
1002 i40evf_irq_disable(adapter);
1003
1004 i40evf_napi_disable_all(adapter);
1005
1006 netif_carrier_off(netdev);
1007
1008 i40evf_clean_all_tx_rings(adapter);
1009 i40evf_clean_all_rx_rings(adapter);
1010}
1011
1012/**
1013 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1014 * @adapter: board private structure
1015 * @vectors: number of vectors to request
1016 *
1017 * Work with the OS to set up the MSIX vectors needed.
1018 *
1019 * Returns 0 on success, negative on failure
1020 **/
1021static int
1022i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1023{
1024 int err, vector_threshold;
1025
1026 /* We'll want at least 3 (vector_threshold):
1027 * 0) Other (Admin Queue and link, mostly)
1028 * 1) TxQ[0] Cleanup
1029 * 2) RxQ[0] Cleanup
1030 */
1031 vector_threshold = MIN_MSIX_COUNT;
1032
1033 /* The more we get, the more we will assign to Tx/Rx Cleanup
1034 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1035 * Right now, we simply care about how many we'll get; we'll
1036 * set them up later while requesting irq's.
1037 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001038 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1039 vector_threshold, vectors);
1040 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001041 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001042 kfree(adapter->msix_entries);
1043 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001044 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001045 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001046
1047 /* Adjust for only the vectors we'll use, which is minimum
1048 * of max_msix_q_vectors + NONQ_VECS, or the number of
1049 * vectors we were allocated.
1050 */
1051 adapter->num_msix_vectors = err;
1052 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001053}
1054
1055/**
1056 * i40evf_free_queues - Free memory for all rings
1057 * @adapter: board private structure to initialize
1058 *
1059 * Free all of the memory associated with queue pairs.
1060 **/
1061static void i40evf_free_queues(struct i40evf_adapter *adapter)
1062{
1063 int i;
1064
1065 if (!adapter->vsi_res)
1066 return;
1067 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1068 if (adapter->tx_rings[i])
1069 kfree_rcu(adapter->tx_rings[i], rcu);
1070 adapter->tx_rings[i] = NULL;
1071 adapter->rx_rings[i] = NULL;
1072 }
1073}
1074
1075/**
1076 * i40evf_alloc_queues - Allocate memory for all rings
1077 * @adapter: board private structure to initialize
1078 *
1079 * We allocate one ring per queue at run-time since we don't know the
1080 * number of queues at compile-time. The polling_netdev array is
1081 * intended for Multiqueue, but should work fine with a single queue.
1082 **/
1083static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1084{
1085 int i;
1086
1087 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1088 struct i40e_ring *tx_ring;
1089 struct i40e_ring *rx_ring;
1090
1091 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
1092 if (!tx_ring)
1093 goto err_out;
1094
1095 tx_ring->queue_index = i;
1096 tx_ring->netdev = adapter->netdev;
1097 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001098 tx_ring->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001099 adapter->tx_rings[i] = tx_ring;
1100
1101 rx_ring = &tx_ring[1];
1102 rx_ring->queue_index = i;
1103 rx_ring->netdev = adapter->netdev;
1104 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001105 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001106 adapter->rx_rings[i] = rx_ring;
1107 }
1108
1109 return 0;
1110
1111err_out:
1112 i40evf_free_queues(adapter);
1113 return -ENOMEM;
1114}
1115
1116/**
1117 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1118 * @adapter: board private structure to initialize
1119 *
1120 * Attempt to configure the interrupts using the best available
1121 * capabilities of the hardware and the kernel.
1122 **/
1123static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1124{
1125 int vector, v_budget;
1126 int pairs = 0;
1127 int err = 0;
1128
1129 if (!adapter->vsi_res) {
1130 err = -EIO;
1131 goto out;
1132 }
1133 pairs = adapter->vsi_res->num_queue_pairs;
1134
1135 /* It's easy to be greedy for MSI-X vectors, but it really
1136 * doesn't do us much good if we have a lot more vectors
1137 * than CPU's. So let's be conservative and only ask for
1138 * (roughly) twice the number of vectors as there are CPU's.
1139 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001140 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1141 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001142
Greg Rose5eae00c2013-12-21 06:12:45 +00001143 adapter->msix_entries = kcalloc(v_budget,
1144 sizeof(struct msix_entry), GFP_KERNEL);
1145 if (!adapter->msix_entries) {
1146 err = -ENOMEM;
1147 goto out;
1148 }
1149
1150 for (vector = 0; vector < v_budget; vector++)
1151 adapter->msix_entries[vector].entry = vector;
1152
1153 i40evf_acquire_msix_vectors(adapter, v_budget);
1154
1155out:
1156 adapter->netdev->real_num_tx_queues = pairs;
1157 return err;
1158}
1159
1160/**
1161 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1162 * @adapter: board private structure to initialize
1163 *
1164 * We allocate one q_vector per queue interrupt. If allocation fails we
1165 * return -ENOMEM.
1166 **/
1167static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1168{
1169 int q_idx, num_q_vectors;
1170 struct i40e_q_vector *q_vector;
1171
1172 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1173
1174 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1175 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
1176 if (!q_vector)
1177 goto err_out;
1178 q_vector->adapter = adapter;
1179 q_vector->vsi = &adapter->vsi;
1180 q_vector->v_idx = q_idx;
1181 netif_napi_add(adapter->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00001182 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001183 adapter->q_vector[q_idx] = q_vector;
1184 }
1185
1186 return 0;
1187
1188err_out:
1189 while (q_idx) {
1190 q_idx--;
1191 q_vector = adapter->q_vector[q_idx];
1192 netif_napi_del(&q_vector->napi);
1193 kfree(q_vector);
1194 adapter->q_vector[q_idx] = NULL;
1195 }
1196 return -ENOMEM;
1197}
1198
1199/**
1200 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1201 * @adapter: board private structure to initialize
1202 *
1203 * This function frees the memory allocated to the q_vectors. In addition if
1204 * NAPI is enabled it will delete any references to the NAPI struct prior
1205 * to freeing the q_vector.
1206 **/
1207static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1208{
1209 int q_idx, num_q_vectors;
1210 int napi_vectors;
1211
1212 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1213 napi_vectors = adapter->vsi_res->num_queue_pairs;
1214
1215 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1216 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1217
1218 adapter->q_vector[q_idx] = NULL;
1219 if (q_idx < napi_vectors)
1220 netif_napi_del(&q_vector->napi);
1221 kfree(q_vector);
1222 }
1223}
1224
1225/**
1226 * i40evf_reset_interrupt_capability - Reset MSIX setup
1227 * @adapter: board private structure
1228 *
1229 **/
1230void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1231{
1232 pci_disable_msix(adapter->pdev);
1233 kfree(adapter->msix_entries);
1234 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001235}
1236
1237/**
1238 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1239 * @adapter: board private structure to initialize
1240 *
1241 **/
1242int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1243{
1244 int err;
1245
1246 err = i40evf_set_interrupt_capability(adapter);
1247 if (err) {
1248 dev_err(&adapter->pdev->dev,
1249 "Unable to setup interrupt capabilities\n");
1250 goto err_set_interrupt;
1251 }
1252
1253 err = i40evf_alloc_q_vectors(adapter);
1254 if (err) {
1255 dev_err(&adapter->pdev->dev,
1256 "Unable to allocate memory for queue vectors\n");
1257 goto err_alloc_q_vectors;
1258 }
1259
1260 err = i40evf_alloc_queues(adapter);
1261 if (err) {
1262 dev_err(&adapter->pdev->dev,
1263 "Unable to allocate memory for queues\n");
1264 goto err_alloc_queues;
1265 }
1266
1267 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1268 (adapter->vsi_res->num_queue_pairs > 1) ? "Enabled" :
1269 "Disabled", adapter->vsi_res->num_queue_pairs);
1270
1271 return 0;
1272err_alloc_queues:
1273 i40evf_free_q_vectors(adapter);
1274err_alloc_q_vectors:
1275 i40evf_reset_interrupt_capability(adapter);
1276err_set_interrupt:
1277 return err;
1278}
1279
1280/**
1281 * i40evf_watchdog_timer - Periodic call-back timer
1282 * @data: pointer to adapter disguised as unsigned long
1283 **/
1284static void i40evf_watchdog_timer(unsigned long data)
1285{
1286 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1287 schedule_work(&adapter->watchdog_task);
1288 /* timer will be rescheduled in watchdog task */
1289}
1290
1291/**
1292 * i40evf_watchdog_task - Periodic call-back task
1293 * @work: pointer to work_struct
1294 **/
1295static void i40evf_watchdog_task(struct work_struct *work)
1296{
1297 struct i40evf_adapter *adapter = container_of(work,
1298 struct i40evf_adapter,
1299 watchdog_task);
1300 struct i40e_hw *hw = &adapter->hw;
1301
Greg Rose5eae00c2013-12-21 06:12:45 +00001302 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001303 goto restart_watchdog;
1304
1305 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001306 if ((rd32(hw, I40E_VFGEN_RSTAT) & 0x3) == I40E_VFR_VFACTIVE) {
1307 /* A chance for redemption! */
1308 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1309 adapter->state = __I40EVF_STARTUP;
1310 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1311 schedule_delayed_work(&adapter->init_task, 10);
1312 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1313 &adapter->crit_section);
1314 /* Don't reschedule the watchdog, since we've restarted
1315 * the init task. When init_task contacts the PF and
1316 * gets everything set up again, it'll restart the
1317 * watchdog for us. Down, boy. Sit. Stay. Woof.
1318 */
1319 return;
1320 }
1321 adapter->aq_pending = 0;
1322 adapter->aq_required = 0;
1323 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1324 goto watchdog_done;
1325 }
1326
1327 if ((adapter->state < __I40EVF_DOWN) ||
1328 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001329 goto watchdog_done;
1330
Mitch Williamsef8693e2014-02-13 03:48:53 -08001331 /* check for reset */
1332 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
Greg Rose5eae00c2013-12-21 06:12:45 +00001333 (rd32(hw, I40E_VFGEN_RSTAT) & 0x3) != I40E_VFR_VFACTIVE) {
1334 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001335 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001336 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001337 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001338 adapter->aq_pending = 0;
1339 adapter->aq_required = 0;
1340 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001341 goto watchdog_done;
1342 }
1343
1344 /* Process admin queue tasks. After init, everything gets done
1345 * here so we don't race on the admin queue.
1346 */
1347 if (adapter->aq_pending)
1348 goto watchdog_done;
1349
1350 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1351 i40evf_map_queues(adapter);
1352 goto watchdog_done;
1353 }
1354
1355 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1356 i40evf_add_ether_addrs(adapter);
1357 goto watchdog_done;
1358 }
1359
1360 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1361 i40evf_add_vlans(adapter);
1362 goto watchdog_done;
1363 }
1364
1365 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1366 i40evf_del_ether_addrs(adapter);
1367 goto watchdog_done;
1368 }
1369
1370 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1371 i40evf_del_vlans(adapter);
1372 goto watchdog_done;
1373 }
1374
1375 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1376 i40evf_disable_queues(adapter);
1377 goto watchdog_done;
1378 }
1379
1380 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1381 i40evf_configure_queues(adapter);
1382 goto watchdog_done;
1383 }
1384
1385 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1386 i40evf_enable_queues(adapter);
1387 goto watchdog_done;
1388 }
1389
1390 if (adapter->state == __I40EVF_RUNNING)
1391 i40evf_request_stats(adapter);
1392
1393 i40evf_irq_enable(adapter, true);
1394 i40evf_fire_sw_int(adapter, 0xFF);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001395
Greg Rose5eae00c2013-12-21 06:12:45 +00001396watchdog_done:
Mitch Williamsef8693e2014-02-13 03:48:53 -08001397 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1398restart_watchdog:
Greg Rose5eae00c2013-12-21 06:12:45 +00001399 if (adapter->aq_required)
1400 mod_timer(&adapter->watchdog_timer,
1401 jiffies + msecs_to_jiffies(20));
1402 else
1403 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001404 schedule_work(&adapter->adminq_task);
1405}
1406
Mitch A Williams5b7af022014-03-28 06:49:02 +00001407/**
Catherine Sullivan3dd55502014-05-20 08:01:36 +00001408 * next_queue - increment to next available tx queue
Mitch A Williams5b7af022014-03-28 06:49:02 +00001409 * @adapter: board private structure
1410 * @j: queue counter
1411 *
1412 * Helper function for RSS programming to increment through available
1413 * queus. Returns the next queue value.
1414 **/
Mitch Williams96d47702014-02-11 08:27:50 +00001415static int next_queue(struct i40evf_adapter *adapter, int j)
1416{
1417 j += 1;
1418
1419 return j >= adapter->vsi_res->num_queue_pairs ? 0 : j;
1420}
1421
Greg Rose5eae00c2013-12-21 06:12:45 +00001422/**
1423 * i40evf_configure_rss - Prepare for RSS if used
1424 * @adapter: board private structure
1425 **/
1426static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1427{
1428 struct i40e_hw *hw = &adapter->hw;
1429 u32 lut = 0;
1430 int i, j;
1431 u64 hena;
1432
1433 /* Set of random keys generated using kernel random number generator */
1434 static const u32 seed[I40E_VFQF_HKEY_MAX_INDEX + 1] = {
1435 0x794221b4, 0xbca0c5ab, 0x6cd5ebd9, 0x1ada6127,
1436 0x983b3aa1, 0x1c4e71eb, 0x7f6328b2, 0xfcdc0da0,
1437 0xc135cafa, 0x7a6f7e2d, 0xe7102d28, 0x163cd12e,
1438 0x4954b126 };
1439
1440 /* Hash type is configured by the PF - we just supply the key */
1441
1442 /* Fill out hash function seed */
1443 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1444 wr32(hw, I40E_VFQF_HKEY(i), seed[i]);
1445
1446 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1447 hena = I40E_DEFAULT_RSS_HENA;
1448 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1449 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1450
1451 /* Populate the LUT with max no. of queues in round robin fashion */
Mitch Williams96d47702014-02-11 08:27:50 +00001452 j = adapter->vsi_res->num_queue_pairs;
1453 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
Mitch A Williams5b7af022014-03-28 06:49:02 +00001454 j = next_queue(adapter, j);
1455 lut = j;
1456 j = next_queue(adapter, j);
1457 lut |= j << 8;
1458 j = next_queue(adapter, j);
1459 lut |= j << 16;
1460 j = next_queue(adapter, j);
1461 lut |= j << 24;
Mitch Williams96d47702014-02-11 08:27:50 +00001462 wr32(hw, I40E_VFQF_HLUT(i), lut);
Greg Rose5eae00c2013-12-21 06:12:45 +00001463 }
1464 i40e_flush(hw);
1465}
1466
Mitch Williamsef8693e2014-02-13 03:48:53 -08001467#define I40EVF_RESET_WAIT_MS 100
1468#define I40EVF_RESET_WAIT_COUNT 200
Greg Rose5eae00c2013-12-21 06:12:45 +00001469/**
1470 * i40evf_reset_task - Call-back task to handle hardware reset
1471 * @work: pointer to work_struct
1472 *
1473 * During reset we need to shut down and reinitialize the admin queue
1474 * before we can use it to communicate with the PF again. We also clear
1475 * and reinit the rings because that context is lost as well.
1476 **/
1477static void i40evf_reset_task(struct work_struct *work)
1478{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001479 struct i40evf_adapter *adapter = container_of(work,
1480 struct i40evf_adapter,
1481 reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001482 struct i40e_hw *hw = &adapter->hw;
1483 int i = 0, err;
1484 uint32_t rstat_val;
1485
1486 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1487 &adapter->crit_section))
1488 udelay(500);
Mitch Williams3526d802014-03-06 08:59:56 +00001489
1490 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1491 dev_info(&adapter->pdev->dev, "Requesting reset from PF\n");
1492 i40evf_request_reset(adapter);
1493 }
1494
Mitch Williamsef8693e2014-02-13 03:48:53 -08001495 /* poll until we see the reset actually happen */
1496 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001497 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1498 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williams63158f92014-05-10 04:49:11 +00001499 if (rstat_val != I40E_VFR_VFACTIVE)
Greg Rose5eae00c2013-12-21 06:12:45 +00001500 break;
Mitch Williams63158f92014-05-10 04:49:11 +00001501 else
Mitch Williamsef8693e2014-02-13 03:48:53 -08001502 msleep(I40EVF_RESET_WAIT_MS);
Greg Rose5eae00c2013-12-21 06:12:45 +00001503 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001504 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001505 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1506 goto continue_reset; /* act like the reset happened */
1507 }
1508
1509 /* wait until the reset is complete and the PF is responding to us */
1510 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1511 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1512 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williams63158f92014-05-10 04:49:11 +00001513 if (rstat_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001514 break;
Mitch Williams63158f92014-05-10 04:49:11 +00001515 else
Mitch Williamsef8693e2014-02-13 03:48:53 -08001516 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001517 }
1518 if (i == I40EVF_RESET_WAIT_COUNT) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001519 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001520 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsef8693e2014-02-13 03:48:53 -08001521 rstat_val);
1522 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1523
Mitch Williams169f4072014-04-01 07:11:50 +00001524 if (netif_running(adapter->netdev)) {
1525 set_bit(__I40E_DOWN, &adapter->vsi.state);
1526 i40evf_down(adapter);
1527 i40evf_free_traffic_irqs(adapter);
1528 i40evf_free_all_tx_resources(adapter);
1529 i40evf_free_all_rx_resources(adapter);
1530 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001531 i40evf_free_misc_irq(adapter);
1532 i40evf_reset_interrupt_capability(adapter);
1533 i40evf_free_queues(adapter);
1534 kfree(adapter->vf_res);
1535 i40evf_shutdown_adminq(hw);
1536 adapter->netdev->flags &= ~IFF_UP;
1537 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1538 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001539 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001540
1541continue_reset:
1542 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1543
Greg Rose5eae00c2013-12-21 06:12:45 +00001544 i40evf_down(adapter);
1545 adapter->state = __I40EVF_RESETTING;
1546
1547 /* kill and reinit the admin queue */
1548 if (i40evf_shutdown_adminq(hw))
1549 dev_warn(&adapter->pdev->dev,
1550 "%s: Failed to destroy the Admin Queue resources\n",
1551 __func__);
1552 err = i40evf_init_adminq(hw);
1553 if (err)
1554 dev_info(&adapter->pdev->dev, "%s: init_adminq failed: %d\n",
1555 __func__, err);
1556
1557 adapter->aq_pending = 0;
1558 adapter->aq_required = 0;
1559 i40evf_map_queues(adapter);
1560 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1561
1562 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1563
1564 if (netif_running(adapter->netdev)) {
1565 /* allocate transmit descriptors */
1566 err = i40evf_setup_all_tx_resources(adapter);
1567 if (err)
1568 goto reset_err;
1569
1570 /* allocate receive descriptors */
1571 err = i40evf_setup_all_rx_resources(adapter);
1572 if (err)
1573 goto reset_err;
1574
1575 i40evf_configure(adapter);
1576
1577 err = i40evf_up_complete(adapter);
1578 if (err)
1579 goto reset_err;
1580
1581 i40evf_irq_enable(adapter, true);
1582 }
1583 return;
1584reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001585 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001586 i40evf_close(adapter->netdev);
1587}
1588
1589/**
1590 * i40evf_adminq_task - worker thread to clean the admin queue
1591 * @work: pointer to work_struct containing our data
1592 **/
1593static void i40evf_adminq_task(struct work_struct *work)
1594{
1595 struct i40evf_adapter *adapter =
1596 container_of(work, struct i40evf_adapter, adminq_task);
1597 struct i40e_hw *hw = &adapter->hw;
1598 struct i40e_arq_event_info event;
1599 struct i40e_virtchnl_msg *v_msg;
1600 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001601 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001602 u16 pending;
1603
Mitch Williamsef8693e2014-02-13 03:48:53 -08001604 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1605 return;
1606
Greg Rose5eae00c2013-12-21 06:12:45 +00001607 event.msg_size = I40EVF_MAX_AQ_BUF_SIZE;
1608 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001609 if (!event.msg_buf)
Greg Rose5eae00c2013-12-21 06:12:45 +00001610 return;
Mitch Williams249c8b82014-05-10 04:49:04 +00001611
Greg Rose5eae00c2013-12-21 06:12:45 +00001612 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1613 do {
1614 ret = i40evf_clean_arq_element(hw, &event, &pending);
1615 if (ret)
1616 break; /* No event to process or error cleaning ARQ */
1617
1618 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1619 v_msg->v_retval, event.msg_buf,
1620 event.msg_size);
1621 if (pending != 0) {
1622 dev_info(&adapter->pdev->dev,
1623 "%s: ARQ: Pending events %d\n",
1624 __func__, pending);
1625 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1626 }
1627 } while (pending);
1628
Mitch Williams912257e2014-05-22 06:32:07 +00001629 /* check for error indications */
1630 val = rd32(hw, hw->aq.arq.len);
1631 oldval = val;
1632 if (val & I40E_VF_ARQLEN_ARQVFE_MASK) {
1633 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1634 val &= ~I40E_VF_ARQLEN_ARQVFE_MASK;
1635 }
1636 if (val & I40E_VF_ARQLEN_ARQOVFL_MASK) {
1637 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1638 val &= ~I40E_VF_ARQLEN_ARQOVFL_MASK;
1639 }
1640 if (val & I40E_VF_ARQLEN_ARQCRIT_MASK) {
1641 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1642 val &= ~I40E_VF_ARQLEN_ARQCRIT_MASK;
1643 }
1644 if (oldval != val)
1645 wr32(hw, hw->aq.arq.len, val);
1646
1647 val = rd32(hw, hw->aq.asq.len);
1648 oldval = val;
1649 if (val & I40E_VF_ATQLEN_ATQVFE_MASK) {
1650 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1651 val &= ~I40E_VF_ATQLEN_ATQVFE_MASK;
1652 }
1653 if (val & I40E_VF_ATQLEN_ATQOVFL_MASK) {
1654 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1655 val &= ~I40E_VF_ATQLEN_ATQOVFL_MASK;
1656 }
1657 if (val & I40E_VF_ATQLEN_ATQCRIT_MASK) {
1658 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1659 val &= ~I40E_VF_ATQLEN_ATQCRIT_MASK;
1660 }
1661 if (oldval != val)
1662 wr32(hw, hw->aq.asq.len, val);
1663
Greg Rose5eae00c2013-12-21 06:12:45 +00001664 /* re-enable Admin queue interrupt cause */
1665 i40evf_misc_irq_enable(adapter);
1666
1667 kfree(event.msg_buf);
1668}
1669
1670/**
1671 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1672 * @adapter: board private structure
1673 *
1674 * Free all transmit software resources
1675 **/
1676static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1677{
1678 int i;
1679
1680 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1681 if (adapter->tx_rings[i]->desc)
1682 i40evf_free_tx_resources(adapter->tx_rings[i]);
1683
1684}
1685
1686/**
1687 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1688 * @adapter: board private structure
1689 *
1690 * If this function returns with an error, then it's possible one or
1691 * more of the rings is populated (while the rest are not). It is the
1692 * callers duty to clean those orphaned rings.
1693 *
1694 * Return 0 on success, negative on failure
1695 **/
1696static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1697{
1698 int i, err = 0;
1699
1700 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001701 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001702 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1703 if (!err)
1704 continue;
1705 dev_err(&adapter->pdev->dev,
1706 "%s: Allocation for Tx Queue %u failed\n",
1707 __func__, i);
1708 break;
1709 }
1710
1711 return err;
1712}
1713
1714/**
1715 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1716 * @adapter: board private structure
1717 *
1718 * If this function returns with an error, then it's possible one or
1719 * more of the rings is populated (while the rest are not). It is the
1720 * callers duty to clean those orphaned rings.
1721 *
1722 * Return 0 on success, negative on failure
1723 **/
1724static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1725{
1726 int i, err = 0;
1727
1728 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001729 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001730 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1731 if (!err)
1732 continue;
1733 dev_err(&adapter->pdev->dev,
1734 "%s: Allocation for Rx Queue %u failed\n",
1735 __func__, i);
1736 break;
1737 }
1738 return err;
1739}
1740
1741/**
1742 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1743 * @adapter: board private structure
1744 *
1745 * Free all receive software resources
1746 **/
1747static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1748{
1749 int i;
1750
1751 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1752 if (adapter->rx_rings[i]->desc)
1753 i40evf_free_rx_resources(adapter->rx_rings[i]);
1754}
1755
1756/**
1757 * i40evf_open - Called when a network interface is made active
1758 * @netdev: network interface device structure
1759 *
1760 * Returns 0 on success, negative value on failure
1761 *
1762 * The open entry point is called when a network interface is made
1763 * active by the system (IFF_UP). At this point all resources needed
1764 * for transmit and receive operations are allocated, the interrupt
1765 * handler is registered with the OS, the watchdog timer is started,
1766 * and the stack is notified that the interface is ready.
1767 **/
1768static int i40evf_open(struct net_device *netdev)
1769{
1770 struct i40evf_adapter *adapter = netdev_priv(netdev);
1771 int err;
1772
Mitch Williamsef8693e2014-02-13 03:48:53 -08001773 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1774 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1775 return -EIO;
1776 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001777 if (adapter->state != __I40EVF_DOWN)
1778 return -EBUSY;
1779
1780 /* allocate transmit descriptors */
1781 err = i40evf_setup_all_tx_resources(adapter);
1782 if (err)
1783 goto err_setup_tx;
1784
1785 /* allocate receive descriptors */
1786 err = i40evf_setup_all_rx_resources(adapter);
1787 if (err)
1788 goto err_setup_rx;
1789
1790 /* clear any pending interrupts, may auto mask */
1791 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1792 if (err)
1793 goto err_req_irq;
1794
1795 i40evf_configure(adapter);
1796
1797 err = i40evf_up_complete(adapter);
1798 if (err)
1799 goto err_req_irq;
1800
1801 i40evf_irq_enable(adapter, true);
1802
1803 return 0;
1804
1805err_req_irq:
1806 i40evf_down(adapter);
1807 i40evf_free_traffic_irqs(adapter);
1808err_setup_rx:
1809 i40evf_free_all_rx_resources(adapter);
1810err_setup_tx:
1811 i40evf_free_all_tx_resources(adapter);
1812
1813 return err;
1814}
1815
1816/**
1817 * i40evf_close - Disables a network interface
1818 * @netdev: network interface device structure
1819 *
1820 * Returns 0, this is not allowed to fail
1821 *
1822 * The close entry point is called when an interface is de-activated
1823 * by the OS. The hardware is still under the drivers control, but
1824 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1825 * are freed, along with all transmit and receive resources.
1826 **/
1827static int i40evf_close(struct net_device *netdev)
1828{
1829 struct i40evf_adapter *adapter = netdev_priv(netdev);
1830
Mitch Williamsef8693e2014-02-13 03:48:53 -08001831 if (adapter->state <= __I40EVF_DOWN)
1832 return 0;
1833
Mitch Williamsef8693e2014-02-13 03:48:53 -08001834
Greg Rose5eae00c2013-12-21 06:12:45 +00001835 set_bit(__I40E_DOWN, &adapter->vsi.state);
1836
1837 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001838 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001839 i40evf_free_traffic_irqs(adapter);
1840
1841 i40evf_free_all_tx_resources(adapter);
1842 i40evf_free_all_rx_resources(adapter);
1843
1844 return 0;
1845}
1846
1847/**
1848 * i40evf_get_stats - Get System Network Statistics
1849 * @netdev: network interface device structure
1850 *
1851 * Returns the address of the device statistics structure.
1852 * The statistics are actually updated from the timer callback.
1853 **/
1854static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1855{
1856 struct i40evf_adapter *adapter = netdev_priv(netdev);
1857
1858 /* only return the current stats */
1859 return &adapter->net_stats;
1860}
1861
1862/**
1863 * i40evf_reinit_locked - Software reinit
1864 * @adapter: board private structure
1865 *
1866 * Reinititalizes the ring structures in response to a software configuration
1867 * change. Roughly the same as close followed by open, but skips releasing
1868 * and reallocating the interrupts.
1869 **/
1870void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1871{
1872 struct net_device *netdev = adapter->netdev;
1873 int err;
1874
1875 WARN_ON(in_interrupt());
1876
Greg Rose5eae00c2013-12-21 06:12:45 +00001877 i40evf_down(adapter);
1878
1879 /* allocate transmit descriptors */
1880 err = i40evf_setup_all_tx_resources(adapter);
1881 if (err)
1882 goto err_reinit;
1883
1884 /* allocate receive descriptors */
1885 err = i40evf_setup_all_rx_resources(adapter);
1886 if (err)
1887 goto err_reinit;
1888
1889 i40evf_configure(adapter);
1890
1891 err = i40evf_up_complete(adapter);
1892 if (err)
1893 goto err_reinit;
1894
1895 i40evf_irq_enable(adapter, true);
1896 return;
1897
1898err_reinit:
Mitch Williams80e72892014-05-10 04:49:06 +00001899 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001900 i40evf_close(netdev);
1901}
1902
1903/**
1904 * i40evf_change_mtu - Change the Maximum Transfer Unit
1905 * @netdev: network interface device structure
1906 * @new_mtu: new value for maximum frame size
1907 *
1908 * Returns 0 on success, negative on failure
1909 **/
1910static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1911{
1912 struct i40evf_adapter *adapter = netdev_priv(netdev);
1913 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1914
1915 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1916 return -EINVAL;
1917
1918 /* must set new MTU before calling down or up */
1919 netdev->mtu = new_mtu;
1920 i40evf_reinit_locked(adapter);
1921 return 0;
1922}
1923
1924static const struct net_device_ops i40evf_netdev_ops = {
1925 .ndo_open = i40evf_open,
1926 .ndo_stop = i40evf_close,
1927 .ndo_start_xmit = i40evf_xmit_frame,
1928 .ndo_get_stats = i40evf_get_stats,
1929 .ndo_set_rx_mode = i40evf_set_rx_mode,
1930 .ndo_validate_addr = eth_validate_addr,
1931 .ndo_set_mac_address = i40evf_set_mac,
1932 .ndo_change_mtu = i40evf_change_mtu,
1933 .ndo_tx_timeout = i40evf_tx_timeout,
1934 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1935 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1936};
1937
1938/**
1939 * i40evf_check_reset_complete - check that VF reset is complete
1940 * @hw: pointer to hw struct
1941 *
1942 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1943 **/
1944static int i40evf_check_reset_complete(struct i40e_hw *hw)
1945{
1946 u32 rstat;
1947 int i;
1948
1949 for (i = 0; i < 100; i++) {
1950 rstat = rd32(hw, I40E_VFGEN_RSTAT);
1951 if (rstat == I40E_VFR_VFACTIVE)
1952 return 0;
1953 udelay(10);
1954 }
1955 return -EBUSY;
1956}
1957
1958/**
1959 * i40evf_init_task - worker thread to perform delayed initialization
1960 * @work: pointer to work_struct containing our data
1961 *
1962 * This task completes the work that was begun in probe. Due to the nature
1963 * of VF-PF communications, we may need to wait tens of milliseconds to get
1964 * reponses back from the PF. Rather than busy-wait in probe and bog down the
1965 * whole system, we'll do it in a task so we can sleep.
1966 * This task only runs during driver init. Once we've established
1967 * communications with the PF driver and set up our netdev, the watchdog
1968 * takes over.
1969 **/
1970static void i40evf_init_task(struct work_struct *work)
1971{
1972 struct i40evf_adapter *adapter = container_of(work,
1973 struct i40evf_adapter,
1974 init_task.work);
1975 struct net_device *netdev = adapter->netdev;
1976 struct i40evf_mac_filter *f;
1977 struct i40e_hw *hw = &adapter->hw;
1978 struct pci_dev *pdev = adapter->pdev;
1979 int i, err, bufsz;
1980
1981 switch (adapter->state) {
1982 case __I40EVF_STARTUP:
1983 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08001984 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1985 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00001986 err = i40e_set_mac_type(hw);
1987 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08001988 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
1989 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001990 goto err;
1991 }
1992 err = i40evf_check_reset_complete(hw);
1993 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00001994 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08001995 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001996 goto err;
1997 }
1998 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
1999 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2000 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2001 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2002
2003 err = i40evf_init_adminq(hw);
2004 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002005 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2006 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002007 goto err;
2008 }
2009 err = i40evf_send_api_ver(adapter);
2010 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002011 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002012 i40evf_shutdown_adminq(hw);
2013 goto err;
2014 }
2015 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2016 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002017 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002018 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002019 dev_err(&pdev->dev, "Admin queue command never completed\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002020 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002021 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002022
2023 /* aq msg sent, awaiting reply */
2024 err = i40evf_verify_api_ver(adapter);
2025 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002026 dev_info(&pdev->dev, "Unable to verify API version (%d), retrying\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002027 err);
Mitch Williams56f99202014-06-04 04:22:41 +00002028 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2029 dev_info(&pdev->dev, "Resending request\n");
2030 err = i40evf_send_api_ver(adapter);
2031 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002032 goto err;
2033 }
2034 err = i40evf_send_vf_config_msg(adapter);
2035 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002036 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002037 err);
2038 goto err;
2039 }
2040 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2041 goto restart;
Greg Rose5eae00c2013-12-21 06:12:45 +00002042 case __I40EVF_INIT_GET_RESOURCES:
2043 /* aq msg sent, awaiting reply */
2044 if (!adapter->vf_res) {
2045 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2046 (I40E_MAX_VF_VSI *
2047 sizeof(struct i40e_virtchnl_vsi_resource));
2048 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002049 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002050 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002051 }
2052 err = i40evf_get_vf_config(adapter);
2053 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2054 goto restart;
2055 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002056 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2057 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002058 goto err_alloc;
2059 }
2060 adapter->state = __I40EVF_INIT_SW;
2061 break;
2062 default:
2063 goto err_alloc;
2064 }
2065 /* got VF config message back from PF, now we can parse it */
2066 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2067 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2068 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2069 }
2070 if (!adapter->vsi_res) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002071 dev_err(&pdev->dev, "No LAN VSI found\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002072 goto err_alloc;
2073 }
2074
2075 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2076
Greg Rose5eae00c2013-12-21 06:12:45 +00002077 netdev->netdev_ops = &i40evf_netdev_ops;
2078 i40evf_set_ethtool_ops(netdev);
2079 netdev->watchdog_timeo = 5 * HZ;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002080 netdev->features |= NETIF_F_HIGHDMA |
2081 NETIF_F_SG |
Greg Rose5eae00c2013-12-21 06:12:45 +00002082 NETIF_F_IP_CSUM |
2083 NETIF_F_SCTP_CSUM |
2084 NETIF_F_IPV6_CSUM |
2085 NETIF_F_TSO |
2086 NETIF_F_TSO6 |
Greg Rose3415e8c2014-01-30 12:40:27 +00002087 NETIF_F_RXCSUM |
Greg Rose5eae00c2013-12-21 06:12:45 +00002088 NETIF_F_GRO;
2089
2090 if (adapter->vf_res->vf_offload_flags
2091 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2092 netdev->vlan_features = netdev->features;
2093 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2094 NETIF_F_HW_VLAN_CTAG_RX |
2095 NETIF_F_HW_VLAN_CTAG_FILTER;
2096 }
2097
Greg Rose3415e8c2014-01-30 12:40:27 +00002098 /* copy netdev features into list of user selectable features */
2099 netdev->hw_features |= netdev->features;
2100 netdev->hw_features &= ~NETIF_F_RXCSUM;
2101
Greg Rose5eae00c2013-12-21 06:12:45 +00002102 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002103 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002104 adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002105 random_ether_addr(adapter->hw.mac.addr);
2106 }
Greg Rose9a173902014-05-22 06:32:02 +00002107 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2108 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002109
2110 INIT_LIST_HEAD(&adapter->mac_filter_list);
2111 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2112 f = kzalloc(sizeof(*f), GFP_ATOMIC);
2113 if (NULL == f)
2114 goto err_sw_init;
2115
Greg Rose9a173902014-05-22 06:32:02 +00002116 ether_addr_copy(f->macaddr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002117 f->add = true;
2118 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2119
2120 list_add(&f->list, &adapter->mac_filter_list);
2121
2122 init_timer(&adapter->watchdog_timer);
2123 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2124 adapter->watchdog_timer.data = (unsigned long)adapter;
2125 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2126
Mitch Williamsd732a182014-04-24 06:41:37 +00002127 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2128 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002129 err = i40evf_init_interrupt_scheme(adapter);
2130 if (err)
2131 goto err_sw_init;
2132 i40evf_map_rings_to_vectors(adapter);
2133 i40evf_configure_rss(adapter);
2134 err = i40evf_request_misc_irq(adapter);
2135 if (err)
2136 goto err_sw_init;
2137
2138 netif_carrier_off(netdev);
2139
Greg Rose5eae00c2013-12-21 06:12:45 +00002140 adapter->vsi.id = adapter->vsi_res->vsi_id;
2141 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2142 adapter->vsi.back = adapter;
2143 adapter->vsi.base_vector = 1;
2144 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williamsca99eb92014-04-04 04:43:07 +00002145 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2146 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2147 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2148 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Greg Rose5eae00c2013-12-21 06:12:45 +00002149 adapter->vsi.netdev = adapter->netdev;
2150
Mitch Williamsef8693e2014-02-13 03:48:53 -08002151 if (!adapter->netdev_registered) {
2152 err = register_netdev(netdev);
2153 if (err)
2154 goto err_register;
2155 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002156
2157 adapter->netdev_registered = true;
2158
2159 netif_tx_stop_all_queues(netdev);
2160
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002161 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002162 if (netdev->features & NETIF_F_GRO)
2163 dev_info(&pdev->dev, "GRO is enabled\n");
2164
2165 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2166 adapter->state = __I40EVF_DOWN;
2167 set_bit(__I40E_DOWN, &adapter->vsi.state);
2168 i40evf_misc_irq_enable(adapter);
2169 return;
2170restart:
2171 schedule_delayed_work(&adapter->init_task,
2172 msecs_to_jiffies(50));
2173 return;
2174
2175err_register:
2176 i40evf_free_misc_irq(adapter);
2177err_sw_init:
2178 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002179err_alloc:
2180 kfree(adapter->vf_res);
2181 adapter->vf_res = NULL;
2182err:
2183 /* Things went into the weeds, so try again later */
2184 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002185 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002186 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002187 return; /* do not reschedule */
2188 }
2189 schedule_delayed_work(&adapter->init_task, HZ * 3);
Greg Rose5eae00c2013-12-21 06:12:45 +00002190}
2191
2192/**
2193 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2194 * @pdev: pci device structure
2195 **/
2196static void i40evf_shutdown(struct pci_dev *pdev)
2197{
2198 struct net_device *netdev = pci_get_drvdata(pdev);
2199
2200 netif_device_detach(netdev);
2201
2202 if (netif_running(netdev))
2203 i40evf_close(netdev);
2204
2205#ifdef CONFIG_PM
2206 pci_save_state(pdev);
2207
2208#endif
2209 pci_disable_device(pdev);
2210}
2211
2212/**
2213 * i40evf_probe - Device Initialization Routine
2214 * @pdev: PCI device information struct
2215 * @ent: entry in i40evf_pci_tbl
2216 *
2217 * Returns 0 on success, negative on failure
2218 *
2219 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2220 * The OS initialization, configuring of the adapter private structure,
2221 * and a hardware reset occur.
2222 **/
2223static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2224{
2225 struct net_device *netdev;
2226 struct i40evf_adapter *adapter = NULL;
2227 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002228 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002229
2230 err = pci_enable_device(pdev);
2231 if (err)
2232 return err;
2233
Mitch Williams64942942014-02-11 08:26:33 +00002234 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002235 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002236 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2237 if (err) {
2238 dev_err(&pdev->dev,
2239 "DMA configuration failed: 0x%x\n", err);
2240 goto err_dma;
2241 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002242 }
2243
2244 err = pci_request_regions(pdev, i40evf_driver_name);
2245 if (err) {
2246 dev_err(&pdev->dev,
2247 "pci_request_regions failed 0x%x\n", err);
2248 goto err_pci_reg;
2249 }
2250
2251 pci_enable_pcie_error_reporting(pdev);
2252
2253 pci_set_master(pdev);
2254
2255 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2256 MAX_TX_QUEUES);
2257 if (!netdev) {
2258 err = -ENOMEM;
2259 goto err_alloc_etherdev;
2260 }
2261
2262 SET_NETDEV_DEV(netdev, &pdev->dev);
2263
2264 pci_set_drvdata(pdev, netdev);
2265 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002266
2267 adapter->netdev = netdev;
2268 adapter->pdev = pdev;
2269
2270 hw = &adapter->hw;
2271 hw->back = adapter;
2272
2273 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2274 adapter->state = __I40EVF_STARTUP;
2275
2276 /* Call save state here because it relies on the adapter struct. */
2277 pci_save_state(pdev);
2278
2279 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2280 pci_resource_len(pdev, 0));
2281 if (!hw->hw_addr) {
2282 err = -EIO;
2283 goto err_ioremap;
2284 }
2285 hw->vendor_id = pdev->vendor;
2286 hw->device_id = pdev->device;
2287 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2288 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2289 hw->subsystem_device_id = pdev->subsystem_device;
2290 hw->bus.device = PCI_SLOT(pdev->devfn);
2291 hw->bus.func = PCI_FUNC(pdev->devfn);
2292
2293 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2294 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2295 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2296 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2297 schedule_delayed_work(&adapter->init_task, 10);
2298
2299 return 0;
2300
2301err_ioremap:
2302 free_netdev(netdev);
2303err_alloc_etherdev:
2304 pci_release_regions(pdev);
2305err_pci_reg:
2306err_dma:
2307 pci_disable_device(pdev);
2308 return err;
2309}
2310
2311#ifdef CONFIG_PM
2312/**
2313 * i40evf_suspend - Power management suspend routine
2314 * @pdev: PCI device information struct
2315 * @state: unused
2316 *
2317 * Called when the system (VM) is entering sleep/suspend.
2318 **/
2319static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2320{
2321 struct net_device *netdev = pci_get_drvdata(pdev);
2322 struct i40evf_adapter *adapter = netdev_priv(netdev);
2323 int retval = 0;
2324
2325 netif_device_detach(netdev);
2326
2327 if (netif_running(netdev)) {
2328 rtnl_lock();
2329 i40evf_down(adapter);
2330 rtnl_unlock();
2331 }
2332 i40evf_free_misc_irq(adapter);
2333 i40evf_reset_interrupt_capability(adapter);
2334
2335 retval = pci_save_state(pdev);
2336 if (retval)
2337 return retval;
2338
2339 pci_disable_device(pdev);
2340
2341 return 0;
2342}
2343
2344/**
2345 * i40evf_resume - Power managment resume routine
2346 * @pdev: PCI device information struct
2347 *
2348 * Called when the system (VM) is resumed from sleep/suspend.
2349 **/
2350static int i40evf_resume(struct pci_dev *pdev)
2351{
2352 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2353 struct net_device *netdev = adapter->netdev;
2354 u32 err;
2355
2356 pci_set_power_state(pdev, PCI_D0);
2357 pci_restore_state(pdev);
2358 /* pci_restore_state clears dev->state_saved so call
2359 * pci_save_state to restore it.
2360 */
2361 pci_save_state(pdev);
2362
2363 err = pci_enable_device_mem(pdev);
2364 if (err) {
2365 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2366 return err;
2367 }
2368 pci_set_master(pdev);
2369
2370 rtnl_lock();
2371 err = i40evf_set_interrupt_capability(adapter);
2372 if (err) {
2373 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2374 return err;
2375 }
2376 err = i40evf_request_misc_irq(adapter);
2377 rtnl_unlock();
2378 if (err) {
2379 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2380 return err;
2381 }
2382
2383 schedule_work(&adapter->reset_task);
2384
2385 netif_device_attach(netdev);
2386
2387 return err;
2388}
2389
2390#endif /* CONFIG_PM */
2391/**
2392 * i40evf_remove - Device Removal Routine
2393 * @pdev: PCI device information struct
2394 *
2395 * i40evf_remove is called by the PCI subsystem to alert the driver
2396 * that it should release a PCI device. The could be caused by a
2397 * Hot-Plug event, or because the driver is going to be removed from
2398 * memory.
2399 **/
2400static void i40evf_remove(struct pci_dev *pdev)
2401{
2402 struct net_device *netdev = pci_get_drvdata(pdev);
2403 struct i40evf_adapter *adapter = netdev_priv(netdev);
2404 struct i40e_hw *hw = &adapter->hw;
2405
2406 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002407 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002408
2409 if (adapter->netdev_registered) {
2410 unregister_netdev(netdev);
2411 adapter->netdev_registered = false;
2412 }
2413 adapter->state = __I40EVF_REMOVE;
2414
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002415 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002416 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002417 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002418 i40evf_reset_interrupt_capability(adapter);
2419 }
2420
Mitch Williamse5d17c32014-06-04 04:22:38 +00002421 if (adapter->watchdog_timer.function)
2422 del_timer_sync(&adapter->watchdog_timer);
2423
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002424 flush_scheduled_work();
2425
Greg Rose5eae00c2013-12-21 06:12:45 +00002426 if (hw->aq.asq.count)
2427 i40evf_shutdown_adminq(hw);
2428
2429 iounmap(hw->hw_addr);
2430 pci_release_regions(pdev);
2431
2432 i40evf_free_queues(adapter);
2433 kfree(adapter->vf_res);
2434
2435 free_netdev(netdev);
2436
2437 pci_disable_pcie_error_reporting(pdev);
2438
2439 pci_disable_device(pdev);
2440}
2441
2442static struct pci_driver i40evf_driver = {
2443 .name = i40evf_driver_name,
2444 .id_table = i40evf_pci_tbl,
2445 .probe = i40evf_probe,
2446 .remove = i40evf_remove,
2447#ifdef CONFIG_PM
2448 .suspend = i40evf_suspend,
2449 .resume = i40evf_resume,
2450#endif
2451 .shutdown = i40evf_shutdown,
2452};
2453
2454/**
2455 * i40e_init_module - Driver Registration Routine
2456 *
2457 * i40e_init_module is the first routine called when the driver is
2458 * loaded. All it does is register with the PCI subsystem.
2459 **/
2460static int __init i40evf_init_module(void)
2461{
2462 int ret;
2463 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2464 i40evf_driver_version);
2465
2466 pr_info("%s\n", i40evf_copyright);
2467
2468 ret = pci_register_driver(&i40evf_driver);
2469 return ret;
2470}
2471
2472module_init(i40evf_init_module);
2473
2474/**
2475 * i40e_exit_module - Driver Exit Cleanup Routine
2476 *
2477 * i40e_exit_module is called just before the driver is removed
2478 * from memory.
2479 **/
2480static void __exit i40evf_exit_module(void)
2481{
2482 pci_unregister_driver(&i40evf_driver);
2483}
2484
2485module_exit(i40evf_exit_module);
2486
2487/* i40evf_main.c */