blob: 64a267afc3fb8180c2e71ffe1b0a310c6ad801b3 [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 Sullivan25941f92014-06-04 01:23:27 +000039#define DRV_VERSION "0.9.36"
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
263 for (i = 1; i < adapter->num_msix_vectors; i++) {
264 if (mask & (1 << i)) {
265 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
266 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
267 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
268 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
269 }
270 }
271}
272
273/**
274 * i40evf_irq_enable - Enable default interrupt generation settings
275 * @adapter: board private structure
276 **/
277void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
278{
279 struct i40e_hw *hw = &adapter->hw;
280
281 i40evf_irq_enable_queues(adapter, ~0);
282
283 if (flush)
284 rd32(hw, I40E_VFGEN_RSTAT);
285}
286
287/**
288 * i40evf_msix_aq - Interrupt handler for vector 0
289 * @irq: interrupt number
290 * @data: pointer to netdev
291 **/
292static irqreturn_t i40evf_msix_aq(int irq, void *data)
293{
294 struct net_device *netdev = data;
295 struct i40evf_adapter *adapter = netdev_priv(netdev);
296 struct i40e_hw *hw = &adapter->hw;
297 u32 val;
298 u32 ena_mask;
299
300 /* handle non-queue interrupts */
301 val = rd32(hw, I40E_VFINT_ICR01);
302 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
303
304
305 val = rd32(hw, I40E_VFINT_DYN_CTL01);
306 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
307 wr32(hw, I40E_VFINT_DYN_CTL01, val);
308
309 /* re-enable interrupt causes */
310 wr32(hw, I40E_VFINT_ICR0_ENA1, ena_mask);
311 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK);
312
313 /* schedule work on the private workqueue */
314 schedule_work(&adapter->adminq_task);
315
316 return IRQ_HANDLED;
317}
318
319/**
320 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
321 * @irq: interrupt number
322 * @data: pointer to a q_vector
323 **/
324static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
325{
326 struct i40e_q_vector *q_vector = data;
327
328 if (!q_vector->tx.ring && !q_vector->rx.ring)
329 return IRQ_HANDLED;
330
331 napi_schedule(&q_vector->napi);
332
333 return IRQ_HANDLED;
334}
335
336/**
337 * i40evf_map_vector_to_rxq - associate irqs with rx queues
338 * @adapter: board private structure
339 * @v_idx: interrupt number
340 * @r_idx: queue number
341 **/
342static void
343i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
344{
345 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
346 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
347
348 rx_ring->q_vector = q_vector;
349 rx_ring->next = q_vector->rx.ring;
350 rx_ring->vsi = &adapter->vsi;
351 q_vector->rx.ring = rx_ring;
352 q_vector->rx.count++;
353 q_vector->rx.latency_range = I40E_LOW_LATENCY;
354}
355
356/**
357 * i40evf_map_vector_to_txq - associate irqs with tx queues
358 * @adapter: board private structure
359 * @v_idx: interrupt number
360 * @t_idx: queue number
361 **/
362static void
363i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
364{
365 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
366 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
367
368 tx_ring->q_vector = q_vector;
369 tx_ring->next = q_vector->tx.ring;
370 tx_ring->vsi = &adapter->vsi;
371 q_vector->tx.ring = tx_ring;
372 q_vector->tx.count++;
373 q_vector->tx.latency_range = I40E_LOW_LATENCY;
374 q_vector->num_ringpairs++;
375 q_vector->ring_mask |= (1 << t_idx);
376}
377
378/**
379 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
380 * @adapter: board private structure to initialize
381 *
382 * This function maps descriptor rings to the queue-specific vectors
383 * we were allotted through the MSI-X enabling code. Ideally, we'd have
384 * one vector per ring/queue, but on a constrained vector budget, we
385 * group the rings as "efficiently" as possible. You would add new
386 * mapping configurations in here.
387 **/
388static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
389{
390 int q_vectors;
391 int v_start = 0;
392 int rxr_idx = 0, txr_idx = 0;
393 int rxr_remaining = adapter->vsi_res->num_queue_pairs;
394 int txr_remaining = adapter->vsi_res->num_queue_pairs;
395 int i, j;
396 int rqpv, tqpv;
397 int err = 0;
398
399 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
400
401 /* The ideal configuration...
402 * We have enough vectors to map one per queue.
403 */
404 if (q_vectors == (rxr_remaining * 2)) {
405 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
406 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
407
408 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
409 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
410 goto out;
411 }
412
413 /* If we don't have enough vectors for a 1-to-1
414 * mapping, we'll have to group them so there are
415 * multiple queues per vector.
416 * Re-adjusting *qpv takes care of the remainder.
417 */
418 for (i = v_start; i < q_vectors; i++) {
419 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
420 for (j = 0; j < rqpv; j++) {
421 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
422 rxr_idx++;
423 rxr_remaining--;
424 }
425 }
426 for (i = v_start; i < q_vectors; i++) {
427 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
428 for (j = 0; j < tqpv; j++) {
429 i40evf_map_vector_to_txq(adapter, i, txr_idx);
430 txr_idx++;
431 txr_remaining--;
432 }
433 }
434
435out:
436 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
437
438 return err;
439}
440
441/**
442 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
443 * @adapter: board private structure
444 *
445 * Allocates MSI-X vectors for tx and rx handling, and requests
446 * interrupts from the kernel.
447 **/
448static int
449i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
450{
451 int vector, err, q_vectors;
452 int rx_int_idx = 0, tx_int_idx = 0;
453
454 i40evf_irq_disable(adapter);
455 /* Decrement for Other and TCP Timer vectors */
456 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
457
458 for (vector = 0; vector < q_vectors; vector++) {
459 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
460
461 if (q_vector->tx.ring && q_vector->rx.ring) {
462 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
463 "i40evf-%s-%s-%d", basename,
464 "TxRx", rx_int_idx++);
465 tx_int_idx++;
466 } else if (q_vector->rx.ring) {
467 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
468 "i40evf-%s-%s-%d", basename,
469 "rx", rx_int_idx++);
470 } else if (q_vector->tx.ring) {
471 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
472 "i40evf-%s-%s-%d", basename,
473 "tx", tx_int_idx++);
474 } else {
475 /* skip this unused q_vector */
476 continue;
477 }
478 err = request_irq(
479 adapter->msix_entries[vector + NONQ_VECS].vector,
480 i40evf_msix_clean_rings,
481 0,
482 q_vector->name,
483 q_vector);
484 if (err) {
485 dev_info(&adapter->pdev->dev,
486 "%s: request_irq failed, error: %d\n",
487 __func__, err);
488 goto free_queue_irqs;
489 }
490 /* assign the mask for this irq */
491 irq_set_affinity_hint(
492 adapter->msix_entries[vector + NONQ_VECS].vector,
493 q_vector->affinity_mask);
494 }
495
496 return 0;
497
498free_queue_irqs:
499 while (vector) {
500 vector--;
501 irq_set_affinity_hint(
502 adapter->msix_entries[vector + NONQ_VECS].vector,
503 NULL);
504 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
505 adapter->q_vector[vector]);
506 }
507 return err;
508}
509
510/**
511 * i40evf_request_misc_irq - Initialize MSI-X interrupts
512 * @adapter: board private structure
513 *
514 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
515 * vector is only for the admin queue, and stays active even when the netdev
516 * is closed.
517 **/
518static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
519{
520 struct net_device *netdev = adapter->netdev;
521 int err;
522
Mitch Williamse1dfee82014-02-13 03:48:51 -0800523 sprintf(adapter->misc_vector_name, "i40evf:mbx");
Greg Rose5eae00c2013-12-21 06:12:45 +0000524 err = request_irq(adapter->msix_entries[0].vector,
Mitch Williamse1dfee82014-02-13 03:48:51 -0800525 &i40evf_msix_aq, 0,
526 adapter->misc_vector_name, netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +0000527 if (err) {
528 dev_err(&adapter->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -0800529 "request_irq for %s failed: %d\n",
530 adapter->misc_vector_name, err);
Greg Rose5eae00c2013-12-21 06:12:45 +0000531 free_irq(adapter->msix_entries[0].vector, netdev);
532 }
533 return err;
534}
535
536/**
537 * i40evf_free_traffic_irqs - Free MSI-X interrupts
538 * @adapter: board private structure
539 *
540 * Frees all MSI-X vectors other than 0.
541 **/
542static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
543{
544 int i;
545 int q_vectors;
546 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
547
548 for (i = 0; i < q_vectors; i++) {
549 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
550 NULL);
551 free_irq(adapter->msix_entries[i+1].vector,
552 adapter->q_vector[i]);
553 }
554}
555
556/**
557 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
558 * @adapter: board private structure
559 *
560 * Frees MSI-X vector 0.
561 **/
562static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
563{
564 struct net_device *netdev = adapter->netdev;
565
566 free_irq(adapter->msix_entries[0].vector, netdev);
567}
568
569/**
570 * i40evf_configure_tx - Configure Transmit Unit after Reset
571 * @adapter: board private structure
572 *
573 * Configure the Tx unit of the MAC after a reset.
574 **/
575static void i40evf_configure_tx(struct i40evf_adapter *adapter)
576{
577 struct i40e_hw *hw = &adapter->hw;
578 int i;
579 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
580 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
581}
582
583/**
584 * i40evf_configure_rx - Configure Receive Unit after Reset
585 * @adapter: board private structure
586 *
587 * Configure the Rx unit of the MAC after a reset.
588 **/
589static void i40evf_configure_rx(struct i40evf_adapter *adapter)
590{
591 struct i40e_hw *hw = &adapter->hw;
592 struct net_device *netdev = adapter->netdev;
593 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
594 int i;
595 int rx_buf_len;
596
597
598 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
599 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
600
601 /* Decide whether to use packet split mode or not */
602 if (netdev->mtu > ETH_DATA_LEN) {
603 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
604 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
605 else
606 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
607 } else {
608 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
609 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
610 else
611 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
612 }
613
614 /* Set the RX buffer length according to the mode */
615 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
616 rx_buf_len = I40E_RX_HDR_SIZE;
617 } else {
618 if (netdev->mtu <= ETH_DATA_LEN)
619 rx_buf_len = I40EVF_RXBUFFER_2048;
620 else
621 rx_buf_len = ALIGN(max_frame, 1024);
622 }
623
624 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
625 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
626 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
627 }
628}
629
630/**
631 * i40evf_find_vlan - Search filter list for specific vlan filter
632 * @adapter: board private structure
633 * @vlan: vlan tag
634 *
635 * Returns ptr to the filter object or NULL
636 **/
637static struct
638i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
639{
640 struct i40evf_vlan_filter *f;
641
642 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
643 if (vlan == f->vlan)
644 return f;
645 }
646 return NULL;
647}
648
649/**
650 * i40evf_add_vlan - Add a vlan filter to the list
651 * @adapter: board private structure
652 * @vlan: VLAN tag
653 *
654 * Returns ptr to the filter object or NULL when no memory available.
655 **/
656static struct
657i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
658{
659 struct i40evf_vlan_filter *f;
660
661 f = i40evf_find_vlan(adapter, vlan);
662 if (NULL == f) {
663 f = kzalloc(sizeof(*f), GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000664 if (NULL == f)
Greg Rose5eae00c2013-12-21 06:12:45 +0000665 return NULL;
Mitch Williams249c8b82014-05-10 04:49:04 +0000666
Greg Rose5eae00c2013-12-21 06:12:45 +0000667 f->vlan = vlan;
668
669 INIT_LIST_HEAD(&f->list);
670 list_add(&f->list, &adapter->vlan_filter_list);
671 f->add = true;
672 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
673 }
674
675 return f;
676}
677
678/**
679 * i40evf_del_vlan - Remove a vlan filter from the list
680 * @adapter: board private structure
681 * @vlan: VLAN tag
682 **/
683static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
684{
685 struct i40evf_vlan_filter *f;
686
687 f = i40evf_find_vlan(adapter, vlan);
688 if (f) {
689 f->remove = true;
690 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
691 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000692}
693
694/**
695 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
696 * @netdev: network device struct
697 * @vid: VLAN tag
698 **/
699static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
700 __always_unused __be16 proto, u16 vid)
701{
702 struct i40evf_adapter *adapter = netdev_priv(netdev);
703
704 if (i40evf_add_vlan(adapter, vid) == NULL)
705 return -ENOMEM;
706 return 0;
707}
708
709/**
710 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
711 * @netdev: network device struct
712 * @vid: VLAN tag
713 **/
714static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
715 __always_unused __be16 proto, u16 vid)
716{
717 struct i40evf_adapter *adapter = netdev_priv(netdev);
718
719 i40evf_del_vlan(adapter, vid);
720 return 0;
721}
722
723/**
724 * i40evf_find_filter - Search filter list for specific mac filter
725 * @adapter: board private structure
726 * @macaddr: the MAC address
727 *
728 * Returns ptr to the filter object or NULL
729 **/
730static struct
731i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
732 u8 *macaddr)
733{
734 struct i40evf_mac_filter *f;
735
736 if (!macaddr)
737 return NULL;
738
739 list_for_each_entry(f, &adapter->mac_filter_list, list) {
740 if (ether_addr_equal(macaddr, f->macaddr))
741 return f;
742 }
743 return NULL;
744}
745
746/**
747 * i40e_add_filter - Add a mac filter to the filter list
748 * @adapter: board private structure
749 * @macaddr: the MAC address
750 *
751 * Returns ptr to the filter object or NULL when no memory available.
752 **/
753static struct
754i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
755 u8 *macaddr)
756{
757 struct i40evf_mac_filter *f;
758
759 if (!macaddr)
760 return NULL;
761
762 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
763 &adapter->crit_section))
764 mdelay(1);
765
766 f = i40evf_find_filter(adapter, macaddr);
767 if (NULL == f) {
768 f = kzalloc(sizeof(*f), GFP_ATOMIC);
769 if (NULL == f) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000770 clear_bit(__I40EVF_IN_CRITICAL_TASK,
771 &adapter->crit_section);
772 return NULL;
773 }
774
Greg Rose9a173902014-05-22 06:32:02 +0000775 ether_addr_copy(f->macaddr, macaddr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000776
777 list_add(&f->list, &adapter->mac_filter_list);
778 f->add = true;
779 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
780 }
781
782 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
783 return f;
784}
785
786/**
787 * i40evf_set_mac - NDO callback to set port mac address
788 * @netdev: network interface device structure
789 * @p: pointer to an address structure
790 *
791 * Returns 0 on success, negative on failure
792 **/
793static int i40evf_set_mac(struct net_device *netdev, void *p)
794{
795 struct i40evf_adapter *adapter = netdev_priv(netdev);
796 struct i40e_hw *hw = &adapter->hw;
797 struct i40evf_mac_filter *f;
798 struct sockaddr *addr = p;
799
800 if (!is_valid_ether_addr(addr->sa_data))
801 return -EADDRNOTAVAIL;
802
803 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
804 return 0;
805
806 f = i40evf_add_filter(adapter, addr->sa_data);
807 if (f) {
Greg Rose9a173902014-05-22 06:32:02 +0000808 ether_addr_copy(hw->mac.addr, addr->sa_data);
809 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +0000810 }
811
812 return (f == NULL) ? -ENOMEM : 0;
813}
814
815/**
816 * i40evf_set_rx_mode - NDO callback to set the netdev filters
817 * @netdev: network interface device structure
818 **/
819static void i40evf_set_rx_mode(struct net_device *netdev)
820{
821 struct i40evf_adapter *adapter = netdev_priv(netdev);
822 struct i40evf_mac_filter *f, *ftmp;
823 struct netdev_hw_addr *uca;
824 struct netdev_hw_addr *mca;
825
826 /* add addr if not already in the filter list */
827 netdev_for_each_uc_addr(uca, netdev) {
828 i40evf_add_filter(adapter, uca->addr);
829 }
830 netdev_for_each_mc_addr(mca, netdev) {
831 i40evf_add_filter(adapter, mca->addr);
832 }
833
834 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
835 &adapter->crit_section))
836 mdelay(1);
837 /* remove filter if not in netdev list */
838 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
839 bool found = false;
840
Tobias Klauserdc5f2de2014-05-20 08:23:06 +0000841 if (is_multicast_ether_addr(f->macaddr)) {
Greg Rose5eae00c2013-12-21 06:12:45 +0000842 netdev_for_each_mc_addr(mca, netdev) {
843 if (ether_addr_equal(mca->addr, f->macaddr)) {
844 found = true;
845 break;
846 }
847 }
848 } else {
849 netdev_for_each_uc_addr(uca, netdev) {
850 if (ether_addr_equal(uca->addr, f->macaddr)) {
851 found = true;
852 break;
853 }
854 }
855 }
856 if (found) {
857 f->remove = true;
858 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
859 }
860 }
861 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
862}
863
864/**
865 * i40evf_napi_enable_all - enable NAPI on all queue vectors
866 * @adapter: board private structure
867 **/
868static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
869{
870 int q_idx;
871 struct i40e_q_vector *q_vector;
872 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
873
874 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
875 struct napi_struct *napi;
876 q_vector = adapter->q_vector[q_idx];
877 napi = &q_vector->napi;
878 napi_enable(napi);
879 }
880}
881
882/**
883 * i40evf_napi_disable_all - disable NAPI on all queue vectors
884 * @adapter: board private structure
885 **/
886static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
887{
888 int q_idx;
889 struct i40e_q_vector *q_vector;
890 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
891
892 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
893 q_vector = adapter->q_vector[q_idx];
894 napi_disable(&q_vector->napi);
895 }
896}
897
898/**
899 * i40evf_configure - set up transmit and receive data structures
900 * @adapter: board private structure
901 **/
902static void i40evf_configure(struct i40evf_adapter *adapter)
903{
904 struct net_device *netdev = adapter->netdev;
905 int i;
906
907 i40evf_set_rx_mode(netdev);
908
909 i40evf_configure_tx(adapter);
910 i40evf_configure_rx(adapter);
911 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
912
913 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
914 struct i40e_ring *ring = adapter->rx_rings[i];
915 i40evf_alloc_rx_buffers(ring, ring->count);
916 ring->next_to_use = ring->count - 1;
917 writel(ring->next_to_use, ring->tail);
918 }
919}
920
921/**
922 * i40evf_up_complete - Finish the last steps of bringing up a connection
923 * @adapter: board private structure
924 **/
925static int i40evf_up_complete(struct i40evf_adapter *adapter)
926{
927 adapter->state = __I40EVF_RUNNING;
928 clear_bit(__I40E_DOWN, &adapter->vsi.state);
929
930 i40evf_napi_enable_all(adapter);
931
932 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
933 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
934 return 0;
935}
936
937/**
938 * i40evf_clean_all_rx_rings - Free Rx Buffers for all queues
939 * @adapter: board private structure
940 **/
941static void i40evf_clean_all_rx_rings(struct i40evf_adapter *adapter)
942{
943 int i;
944
945 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
946 i40evf_clean_rx_ring(adapter->rx_rings[i]);
947}
948
949/**
950 * i40evf_clean_all_tx_rings - Free Tx Buffers for all queues
951 * @adapter: board private structure
952 **/
953static void i40evf_clean_all_tx_rings(struct i40evf_adapter *adapter)
954{
955 int i;
956
957 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
958 i40evf_clean_tx_ring(adapter->tx_rings[i]);
959}
960
961/**
962 * i40e_down - Shutdown the connection processing
963 * @adapter: board private structure
964 **/
965void i40evf_down(struct i40evf_adapter *adapter)
966{
967 struct net_device *netdev = adapter->netdev;
968 struct i40evf_mac_filter *f;
969
Mitch Williamsddf0b3a2014-05-22 06:31:46 +0000970 if (adapter->state == __I40EVF_DOWN)
971 return;
972
Mitch Williamsef8693e2014-02-13 03:48:53 -0800973 /* remove all MAC filters */
Greg Rose5eae00c2013-12-21 06:12:45 +0000974 list_for_each_entry(f, &adapter->mac_filter_list, list) {
975 f->remove = true;
976 }
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800977 /* remove all VLAN filters */
978 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
979 f->remove = true;
980 }
Mitch Williamsef8693e2014-02-13 03:48:53 -0800981 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
982 adapter->state != __I40EVF_RESETTING) {
983 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Mitch Williamsed1f5b52014-02-20 19:29:06 -0800984 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsef8693e2014-02-13 03:48:53 -0800985 /* disable receives */
986 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
987 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
988 msleep(20);
989 }
Greg Rose5eae00c2013-12-21 06:12:45 +0000990 netif_tx_disable(netdev);
991
992 netif_tx_stop_all_queues(netdev);
993
994 i40evf_irq_disable(adapter);
995
996 i40evf_napi_disable_all(adapter);
997
998 netif_carrier_off(netdev);
999
1000 i40evf_clean_all_tx_rings(adapter);
1001 i40evf_clean_all_rx_rings(adapter);
1002}
1003
1004/**
1005 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1006 * @adapter: board private structure
1007 * @vectors: number of vectors to request
1008 *
1009 * Work with the OS to set up the MSIX vectors needed.
1010 *
1011 * Returns 0 on success, negative on failure
1012 **/
1013static int
1014i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1015{
1016 int err, vector_threshold;
1017
1018 /* We'll want at least 3 (vector_threshold):
1019 * 0) Other (Admin Queue and link, mostly)
1020 * 1) TxQ[0] Cleanup
1021 * 2) RxQ[0] Cleanup
1022 */
1023 vector_threshold = MIN_MSIX_COUNT;
1024
1025 /* The more we get, the more we will assign to Tx/Rx Cleanup
1026 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1027 * Right now, we simply care about how many we'll get; we'll
1028 * set them up later while requesting irq's.
1029 */
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001030 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1031 vector_threshold, vectors);
1032 if (err < 0) {
Mitch Williams80e72892014-05-10 04:49:06 +00001033 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001034 kfree(adapter->msix_entries);
1035 adapter->msix_entries = NULL;
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001036 return err;
Greg Rose5eae00c2013-12-21 06:12:45 +00001037 }
Alexander Gordeevfc2f2f52014-04-28 17:53:16 +00001038
1039 /* Adjust for only the vectors we'll use, which is minimum
1040 * of max_msix_q_vectors + NONQ_VECS, or the number of
1041 * vectors we were allocated.
1042 */
1043 adapter->num_msix_vectors = err;
1044 return 0;
Greg Rose5eae00c2013-12-21 06:12:45 +00001045}
1046
1047/**
1048 * i40evf_free_queues - Free memory for all rings
1049 * @adapter: board private structure to initialize
1050 *
1051 * Free all of the memory associated with queue pairs.
1052 **/
1053static void i40evf_free_queues(struct i40evf_adapter *adapter)
1054{
1055 int i;
1056
1057 if (!adapter->vsi_res)
1058 return;
1059 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1060 if (adapter->tx_rings[i])
1061 kfree_rcu(adapter->tx_rings[i], rcu);
1062 adapter->tx_rings[i] = NULL;
1063 adapter->rx_rings[i] = NULL;
1064 }
1065}
1066
1067/**
1068 * i40evf_alloc_queues - Allocate memory for all rings
1069 * @adapter: board private structure to initialize
1070 *
1071 * We allocate one ring per queue at run-time since we don't know the
1072 * number of queues at compile-time. The polling_netdev array is
1073 * intended for Multiqueue, but should work fine with a single queue.
1074 **/
1075static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1076{
1077 int i;
1078
1079 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1080 struct i40e_ring *tx_ring;
1081 struct i40e_ring *rx_ring;
1082
1083 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
1084 if (!tx_ring)
1085 goto err_out;
1086
1087 tx_ring->queue_index = i;
1088 tx_ring->netdev = adapter->netdev;
1089 tx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001090 tx_ring->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001091 adapter->tx_rings[i] = tx_ring;
1092
1093 rx_ring = &tx_ring[1];
1094 rx_ring->queue_index = i;
1095 rx_ring->netdev = adapter->netdev;
1096 rx_ring->dev = &adapter->pdev->dev;
Mitch Williamsd732a182014-04-24 06:41:37 +00001097 rx_ring->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001098 adapter->rx_rings[i] = rx_ring;
1099 }
1100
1101 return 0;
1102
1103err_out:
1104 i40evf_free_queues(adapter);
1105 return -ENOMEM;
1106}
1107
1108/**
1109 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1110 * @adapter: board private structure to initialize
1111 *
1112 * Attempt to configure the interrupts using the best available
1113 * capabilities of the hardware and the kernel.
1114 **/
1115static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1116{
1117 int vector, v_budget;
1118 int pairs = 0;
1119 int err = 0;
1120
1121 if (!adapter->vsi_res) {
1122 err = -EIO;
1123 goto out;
1124 }
1125 pairs = adapter->vsi_res->num_queue_pairs;
1126
1127 /* It's easy to be greedy for MSI-X vectors, but it really
1128 * doesn't do us much good if we have a lot more vectors
1129 * than CPU's. So let's be conservative and only ask for
1130 * (roughly) twice the number of vectors as there are CPU's.
1131 */
Mitch Williams30a500e2014-02-11 08:27:52 +00001132 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1133 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
Greg Rose5eae00c2013-12-21 06:12:45 +00001134
Greg Rose5eae00c2013-12-21 06:12:45 +00001135 adapter->msix_entries = kcalloc(v_budget,
1136 sizeof(struct msix_entry), GFP_KERNEL);
1137 if (!adapter->msix_entries) {
1138 err = -ENOMEM;
1139 goto out;
1140 }
1141
1142 for (vector = 0; vector < v_budget; vector++)
1143 adapter->msix_entries[vector].entry = vector;
1144
1145 i40evf_acquire_msix_vectors(adapter, v_budget);
1146
1147out:
1148 adapter->netdev->real_num_tx_queues = pairs;
1149 return err;
1150}
1151
1152/**
1153 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1154 * @adapter: board private structure to initialize
1155 *
1156 * We allocate one q_vector per queue interrupt. If allocation fails we
1157 * return -ENOMEM.
1158 **/
1159static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1160{
1161 int q_idx, num_q_vectors;
1162 struct i40e_q_vector *q_vector;
1163
1164 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1165
1166 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1167 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
1168 if (!q_vector)
1169 goto err_out;
1170 q_vector->adapter = adapter;
1171 q_vector->vsi = &adapter->vsi;
1172 q_vector->v_idx = q_idx;
1173 netif_napi_add(adapter->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00001174 i40evf_napi_poll, NAPI_POLL_WEIGHT);
Greg Rose5eae00c2013-12-21 06:12:45 +00001175 adapter->q_vector[q_idx] = q_vector;
1176 }
1177
1178 return 0;
1179
1180err_out:
1181 while (q_idx) {
1182 q_idx--;
1183 q_vector = adapter->q_vector[q_idx];
1184 netif_napi_del(&q_vector->napi);
1185 kfree(q_vector);
1186 adapter->q_vector[q_idx] = NULL;
1187 }
1188 return -ENOMEM;
1189}
1190
1191/**
1192 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1193 * @adapter: board private structure to initialize
1194 *
1195 * This function frees the memory allocated to the q_vectors. In addition if
1196 * NAPI is enabled it will delete any references to the NAPI struct prior
1197 * to freeing the q_vector.
1198 **/
1199static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1200{
1201 int q_idx, num_q_vectors;
1202 int napi_vectors;
1203
1204 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1205 napi_vectors = adapter->vsi_res->num_queue_pairs;
1206
1207 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1208 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1209
1210 adapter->q_vector[q_idx] = NULL;
1211 if (q_idx < napi_vectors)
1212 netif_napi_del(&q_vector->napi);
1213 kfree(q_vector);
1214 }
1215}
1216
1217/**
1218 * i40evf_reset_interrupt_capability - Reset MSIX setup
1219 * @adapter: board private structure
1220 *
1221 **/
1222void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1223{
1224 pci_disable_msix(adapter->pdev);
1225 kfree(adapter->msix_entries);
1226 adapter->msix_entries = NULL;
Greg Rose5eae00c2013-12-21 06:12:45 +00001227}
1228
1229/**
1230 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1231 * @adapter: board private structure to initialize
1232 *
1233 **/
1234int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1235{
1236 int err;
1237
1238 err = i40evf_set_interrupt_capability(adapter);
1239 if (err) {
1240 dev_err(&adapter->pdev->dev,
1241 "Unable to setup interrupt capabilities\n");
1242 goto err_set_interrupt;
1243 }
1244
1245 err = i40evf_alloc_q_vectors(adapter);
1246 if (err) {
1247 dev_err(&adapter->pdev->dev,
1248 "Unable to allocate memory for queue vectors\n");
1249 goto err_alloc_q_vectors;
1250 }
1251
1252 err = i40evf_alloc_queues(adapter);
1253 if (err) {
1254 dev_err(&adapter->pdev->dev,
1255 "Unable to allocate memory for queues\n");
1256 goto err_alloc_queues;
1257 }
1258
1259 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1260 (adapter->vsi_res->num_queue_pairs > 1) ? "Enabled" :
1261 "Disabled", adapter->vsi_res->num_queue_pairs);
1262
1263 return 0;
1264err_alloc_queues:
1265 i40evf_free_q_vectors(adapter);
1266err_alloc_q_vectors:
1267 i40evf_reset_interrupt_capability(adapter);
1268err_set_interrupt:
1269 return err;
1270}
1271
1272/**
1273 * i40evf_watchdog_timer - Periodic call-back timer
1274 * @data: pointer to adapter disguised as unsigned long
1275 **/
1276static void i40evf_watchdog_timer(unsigned long data)
1277{
1278 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1279 schedule_work(&adapter->watchdog_task);
1280 /* timer will be rescheduled in watchdog task */
1281}
1282
1283/**
1284 * i40evf_watchdog_task - Periodic call-back task
1285 * @work: pointer to work_struct
1286 **/
1287static void i40evf_watchdog_task(struct work_struct *work)
1288{
1289 struct i40evf_adapter *adapter = container_of(work,
1290 struct i40evf_adapter,
1291 watchdog_task);
1292 struct i40e_hw *hw = &adapter->hw;
1293
Greg Rose5eae00c2013-12-21 06:12:45 +00001294 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
Mitch Williamsef8693e2014-02-13 03:48:53 -08001295 goto restart_watchdog;
1296
1297 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001298 if ((rd32(hw, I40E_VFGEN_RSTAT) & 0x3) == I40E_VFR_VFACTIVE) {
1299 /* A chance for redemption! */
1300 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1301 adapter->state = __I40EVF_STARTUP;
1302 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1303 schedule_delayed_work(&adapter->init_task, 10);
1304 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1305 &adapter->crit_section);
1306 /* Don't reschedule the watchdog, since we've restarted
1307 * the init task. When init_task contacts the PF and
1308 * gets everything set up again, it'll restart the
1309 * watchdog for us. Down, boy. Sit. Stay. Woof.
1310 */
1311 return;
1312 }
1313 adapter->aq_pending = 0;
1314 adapter->aq_required = 0;
1315 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1316 goto watchdog_done;
1317 }
1318
1319 if ((adapter->state < __I40EVF_DOWN) ||
1320 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
Greg Rose5eae00c2013-12-21 06:12:45 +00001321 goto watchdog_done;
1322
Mitch Williamsef8693e2014-02-13 03:48:53 -08001323 /* check for reset */
1324 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
Greg Rose5eae00c2013-12-21 06:12:45 +00001325 (rd32(hw, I40E_VFGEN_RSTAT) & 0x3) != I40E_VFR_VFACTIVE) {
1326 adapter->state = __I40EVF_RESETTING;
Mitch Williamsef8693e2014-02-13 03:48:53 -08001327 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
Mitch Williams249c8b82014-05-10 04:49:04 +00001328 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001329 schedule_work(&adapter->reset_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001330 adapter->aq_pending = 0;
1331 adapter->aq_required = 0;
1332 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001333 goto watchdog_done;
1334 }
1335
1336 /* Process admin queue tasks. After init, everything gets done
1337 * here so we don't race on the admin queue.
1338 */
1339 if (adapter->aq_pending)
1340 goto watchdog_done;
1341
1342 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1343 i40evf_map_queues(adapter);
1344 goto watchdog_done;
1345 }
1346
1347 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1348 i40evf_add_ether_addrs(adapter);
1349 goto watchdog_done;
1350 }
1351
1352 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1353 i40evf_add_vlans(adapter);
1354 goto watchdog_done;
1355 }
1356
1357 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1358 i40evf_del_ether_addrs(adapter);
1359 goto watchdog_done;
1360 }
1361
1362 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1363 i40evf_del_vlans(adapter);
1364 goto watchdog_done;
1365 }
1366
1367 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1368 i40evf_disable_queues(adapter);
1369 goto watchdog_done;
1370 }
1371
1372 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1373 i40evf_configure_queues(adapter);
1374 goto watchdog_done;
1375 }
1376
1377 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1378 i40evf_enable_queues(adapter);
1379 goto watchdog_done;
1380 }
1381
1382 if (adapter->state == __I40EVF_RUNNING)
1383 i40evf_request_stats(adapter);
1384
1385 i40evf_irq_enable(adapter, true);
1386 i40evf_fire_sw_int(adapter, 0xFF);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001387
Greg Rose5eae00c2013-12-21 06:12:45 +00001388watchdog_done:
Mitch Williamsef8693e2014-02-13 03:48:53 -08001389 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1390restart_watchdog:
Greg Rose5eae00c2013-12-21 06:12:45 +00001391 if (adapter->aq_required)
1392 mod_timer(&adapter->watchdog_timer,
1393 jiffies + msecs_to_jiffies(20));
1394 else
1395 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
Greg Rose5eae00c2013-12-21 06:12:45 +00001396 schedule_work(&adapter->adminq_task);
1397}
1398
Mitch A Williams5b7af022014-03-28 06:49:02 +00001399/**
Catherine Sullivan3dd55502014-05-20 08:01:36 +00001400 * next_queue - increment to next available tx queue
Mitch A Williams5b7af022014-03-28 06:49:02 +00001401 * @adapter: board private structure
1402 * @j: queue counter
1403 *
1404 * Helper function for RSS programming to increment through available
1405 * queus. Returns the next queue value.
1406 **/
Mitch Williams96d47702014-02-11 08:27:50 +00001407static int next_queue(struct i40evf_adapter *adapter, int j)
1408{
1409 j += 1;
1410
1411 return j >= adapter->vsi_res->num_queue_pairs ? 0 : j;
1412}
1413
Greg Rose5eae00c2013-12-21 06:12:45 +00001414/**
1415 * i40evf_configure_rss - Prepare for RSS if used
1416 * @adapter: board private structure
1417 **/
1418static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1419{
1420 struct i40e_hw *hw = &adapter->hw;
1421 u32 lut = 0;
1422 int i, j;
1423 u64 hena;
1424
1425 /* Set of random keys generated using kernel random number generator */
1426 static const u32 seed[I40E_VFQF_HKEY_MAX_INDEX + 1] = {
1427 0x794221b4, 0xbca0c5ab, 0x6cd5ebd9, 0x1ada6127,
1428 0x983b3aa1, 0x1c4e71eb, 0x7f6328b2, 0xfcdc0da0,
1429 0xc135cafa, 0x7a6f7e2d, 0xe7102d28, 0x163cd12e,
1430 0x4954b126 };
1431
1432 /* Hash type is configured by the PF - we just supply the key */
1433
1434 /* Fill out hash function seed */
1435 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1436 wr32(hw, I40E_VFQF_HKEY(i), seed[i]);
1437
1438 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1439 hena = I40E_DEFAULT_RSS_HENA;
1440 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1441 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1442
1443 /* Populate the LUT with max no. of queues in round robin fashion */
Mitch Williams96d47702014-02-11 08:27:50 +00001444 j = adapter->vsi_res->num_queue_pairs;
1445 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
Mitch A Williams5b7af022014-03-28 06:49:02 +00001446 j = next_queue(adapter, j);
1447 lut = j;
1448 j = next_queue(adapter, j);
1449 lut |= j << 8;
1450 j = next_queue(adapter, j);
1451 lut |= j << 16;
1452 j = next_queue(adapter, j);
1453 lut |= j << 24;
Mitch Williams96d47702014-02-11 08:27:50 +00001454 wr32(hw, I40E_VFQF_HLUT(i), lut);
Greg Rose5eae00c2013-12-21 06:12:45 +00001455 }
1456 i40e_flush(hw);
1457}
1458
Mitch Williamsef8693e2014-02-13 03:48:53 -08001459#define I40EVF_RESET_WAIT_MS 100
1460#define I40EVF_RESET_WAIT_COUNT 200
Greg Rose5eae00c2013-12-21 06:12:45 +00001461/**
1462 * i40evf_reset_task - Call-back task to handle hardware reset
1463 * @work: pointer to work_struct
1464 *
1465 * During reset we need to shut down and reinitialize the admin queue
1466 * before we can use it to communicate with the PF again. We also clear
1467 * and reinit the rings because that context is lost as well.
1468 **/
1469static void i40evf_reset_task(struct work_struct *work)
1470{
Mitch Williamsef8693e2014-02-13 03:48:53 -08001471 struct i40evf_adapter *adapter = container_of(work,
1472 struct i40evf_adapter,
1473 reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00001474 struct i40e_hw *hw = &adapter->hw;
1475 int i = 0, err;
1476 uint32_t rstat_val;
1477
1478 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1479 &adapter->crit_section))
1480 udelay(500);
Mitch Williams3526d802014-03-06 08:59:56 +00001481
1482 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1483 dev_info(&adapter->pdev->dev, "Requesting reset from PF\n");
1484 i40evf_request_reset(adapter);
1485 }
1486
Mitch Williamsef8693e2014-02-13 03:48:53 -08001487 /* poll until we see the reset actually happen */
1488 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001489 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1490 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williams63158f92014-05-10 04:49:11 +00001491 if (rstat_val != I40E_VFR_VFACTIVE)
Greg Rose5eae00c2013-12-21 06:12:45 +00001492 break;
Mitch Williams63158f92014-05-10 04:49:11 +00001493 else
Mitch Williamsef8693e2014-02-13 03:48:53 -08001494 msleep(I40EVF_RESET_WAIT_MS);
Greg Rose5eae00c2013-12-21 06:12:45 +00001495 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001496 if (i == I40EVF_RESET_WAIT_COUNT) {
Mitch Williamsef8693e2014-02-13 03:48:53 -08001497 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1498 goto continue_reset; /* act like the reset happened */
1499 }
1500
1501 /* wait until the reset is complete and the PF is responding to us */
1502 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1503 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1504 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
Mitch Williams63158f92014-05-10 04:49:11 +00001505 if (rstat_val == I40E_VFR_VFACTIVE)
Mitch Williamsef8693e2014-02-13 03:48:53 -08001506 break;
Mitch Williams63158f92014-05-10 04:49:11 +00001507 else
Mitch Williamsef8693e2014-02-13 03:48:53 -08001508 msleep(I40EVF_RESET_WAIT_MS);
Mitch Williamsef8693e2014-02-13 03:48:53 -08001509 }
1510 if (i == I40EVF_RESET_WAIT_COUNT) {
Greg Rose5eae00c2013-12-21 06:12:45 +00001511 /* reset never finished */
Mitch Williams80e72892014-05-10 04:49:06 +00001512 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
Mitch Williamsef8693e2014-02-13 03:48:53 -08001513 rstat_val);
1514 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1515
Mitch Williams169f4072014-04-01 07:11:50 +00001516 if (netif_running(adapter->netdev)) {
1517 set_bit(__I40E_DOWN, &adapter->vsi.state);
1518 i40evf_down(adapter);
1519 i40evf_free_traffic_irqs(adapter);
1520 i40evf_free_all_tx_resources(adapter);
1521 i40evf_free_all_rx_resources(adapter);
1522 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001523 i40evf_free_misc_irq(adapter);
1524 i40evf_reset_interrupt_capability(adapter);
1525 i40evf_free_queues(adapter);
1526 kfree(adapter->vf_res);
1527 i40evf_shutdown_adminq(hw);
1528 adapter->netdev->flags &= ~IFF_UP;
1529 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1530 return; /* Do not attempt to reinit. It's dead, Jim. */
Greg Rose5eae00c2013-12-21 06:12:45 +00001531 }
Mitch Williamsef8693e2014-02-13 03:48:53 -08001532
1533continue_reset:
1534 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1535
Greg Rose5eae00c2013-12-21 06:12:45 +00001536 i40evf_down(adapter);
1537 adapter->state = __I40EVF_RESETTING;
1538
1539 /* kill and reinit the admin queue */
1540 if (i40evf_shutdown_adminq(hw))
1541 dev_warn(&adapter->pdev->dev,
1542 "%s: Failed to destroy the Admin Queue resources\n",
1543 __func__);
1544 err = i40evf_init_adminq(hw);
1545 if (err)
1546 dev_info(&adapter->pdev->dev, "%s: init_adminq failed: %d\n",
1547 __func__, err);
1548
1549 adapter->aq_pending = 0;
1550 adapter->aq_required = 0;
1551 i40evf_map_queues(adapter);
1552 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1553
1554 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1555
1556 if (netif_running(adapter->netdev)) {
1557 /* allocate transmit descriptors */
1558 err = i40evf_setup_all_tx_resources(adapter);
1559 if (err)
1560 goto reset_err;
1561
1562 /* allocate receive descriptors */
1563 err = i40evf_setup_all_rx_resources(adapter);
1564 if (err)
1565 goto reset_err;
1566
1567 i40evf_configure(adapter);
1568
1569 err = i40evf_up_complete(adapter);
1570 if (err)
1571 goto reset_err;
1572
1573 i40evf_irq_enable(adapter, true);
1574 }
1575 return;
1576reset_err:
Mitch Williams80e72892014-05-10 04:49:06 +00001577 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001578 i40evf_close(adapter->netdev);
1579}
1580
1581/**
1582 * i40evf_adminq_task - worker thread to clean the admin queue
1583 * @work: pointer to work_struct containing our data
1584 **/
1585static void i40evf_adminq_task(struct work_struct *work)
1586{
1587 struct i40evf_adapter *adapter =
1588 container_of(work, struct i40evf_adapter, adminq_task);
1589 struct i40e_hw *hw = &adapter->hw;
1590 struct i40e_arq_event_info event;
1591 struct i40e_virtchnl_msg *v_msg;
1592 i40e_status ret;
Mitch Williams912257e2014-05-22 06:32:07 +00001593 u32 val, oldval;
Greg Rose5eae00c2013-12-21 06:12:45 +00001594 u16 pending;
1595
Mitch Williamsef8693e2014-02-13 03:48:53 -08001596 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1597 return;
1598
Greg Rose5eae00c2013-12-21 06:12:45 +00001599 event.msg_size = I40EVF_MAX_AQ_BUF_SIZE;
1600 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
Mitch Williams249c8b82014-05-10 04:49:04 +00001601 if (!event.msg_buf)
Greg Rose5eae00c2013-12-21 06:12:45 +00001602 return;
Mitch Williams249c8b82014-05-10 04:49:04 +00001603
Greg Rose5eae00c2013-12-21 06:12:45 +00001604 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1605 do {
1606 ret = i40evf_clean_arq_element(hw, &event, &pending);
1607 if (ret)
1608 break; /* No event to process or error cleaning ARQ */
1609
1610 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1611 v_msg->v_retval, event.msg_buf,
1612 event.msg_size);
1613 if (pending != 0) {
1614 dev_info(&adapter->pdev->dev,
1615 "%s: ARQ: Pending events %d\n",
1616 __func__, pending);
1617 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1618 }
1619 } while (pending);
1620
Mitch Williams912257e2014-05-22 06:32:07 +00001621 /* check for error indications */
1622 val = rd32(hw, hw->aq.arq.len);
1623 oldval = val;
1624 if (val & I40E_VF_ARQLEN_ARQVFE_MASK) {
1625 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1626 val &= ~I40E_VF_ARQLEN_ARQVFE_MASK;
1627 }
1628 if (val & I40E_VF_ARQLEN_ARQOVFL_MASK) {
1629 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1630 val &= ~I40E_VF_ARQLEN_ARQOVFL_MASK;
1631 }
1632 if (val & I40E_VF_ARQLEN_ARQCRIT_MASK) {
1633 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1634 val &= ~I40E_VF_ARQLEN_ARQCRIT_MASK;
1635 }
1636 if (oldval != val)
1637 wr32(hw, hw->aq.arq.len, val);
1638
1639 val = rd32(hw, hw->aq.asq.len);
1640 oldval = val;
1641 if (val & I40E_VF_ATQLEN_ATQVFE_MASK) {
1642 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1643 val &= ~I40E_VF_ATQLEN_ATQVFE_MASK;
1644 }
1645 if (val & I40E_VF_ATQLEN_ATQOVFL_MASK) {
1646 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1647 val &= ~I40E_VF_ATQLEN_ATQOVFL_MASK;
1648 }
1649 if (val & I40E_VF_ATQLEN_ATQCRIT_MASK) {
1650 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1651 val &= ~I40E_VF_ATQLEN_ATQCRIT_MASK;
1652 }
1653 if (oldval != val)
1654 wr32(hw, hw->aq.asq.len, val);
1655
Greg Rose5eae00c2013-12-21 06:12:45 +00001656 /* re-enable Admin queue interrupt cause */
1657 i40evf_misc_irq_enable(adapter);
1658
1659 kfree(event.msg_buf);
1660}
1661
1662/**
1663 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1664 * @adapter: board private structure
1665 *
1666 * Free all transmit software resources
1667 **/
1668static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1669{
1670 int i;
1671
1672 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1673 if (adapter->tx_rings[i]->desc)
1674 i40evf_free_tx_resources(adapter->tx_rings[i]);
1675
1676}
1677
1678/**
1679 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1680 * @adapter: board private structure
1681 *
1682 * If this function returns with an error, then it's possible one or
1683 * more of the rings is populated (while the rest are not). It is the
1684 * callers duty to clean those orphaned rings.
1685 *
1686 * Return 0 on success, negative on failure
1687 **/
1688static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1689{
1690 int i, err = 0;
1691
1692 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001693 adapter->tx_rings[i]->count = adapter->tx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001694 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1695 if (!err)
1696 continue;
1697 dev_err(&adapter->pdev->dev,
1698 "%s: Allocation for Tx Queue %u failed\n",
1699 __func__, i);
1700 break;
1701 }
1702
1703 return err;
1704}
1705
1706/**
1707 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1708 * @adapter: board private structure
1709 *
1710 * If this function returns with an error, then it's possible one or
1711 * more of the rings is populated (while the rest are not). It is the
1712 * callers duty to clean those orphaned rings.
1713 *
1714 * Return 0 on success, negative on failure
1715 **/
1716static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1717{
1718 int i, err = 0;
1719
1720 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
Mitch Williamsd732a182014-04-24 06:41:37 +00001721 adapter->rx_rings[i]->count = adapter->rx_desc_count;
Greg Rose5eae00c2013-12-21 06:12:45 +00001722 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1723 if (!err)
1724 continue;
1725 dev_err(&adapter->pdev->dev,
1726 "%s: Allocation for Rx Queue %u failed\n",
1727 __func__, i);
1728 break;
1729 }
1730 return err;
1731}
1732
1733/**
1734 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1735 * @adapter: board private structure
1736 *
1737 * Free all receive software resources
1738 **/
1739static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1740{
1741 int i;
1742
1743 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1744 if (adapter->rx_rings[i]->desc)
1745 i40evf_free_rx_resources(adapter->rx_rings[i]);
1746}
1747
1748/**
1749 * i40evf_open - Called when a network interface is made active
1750 * @netdev: network interface device structure
1751 *
1752 * Returns 0 on success, negative value on failure
1753 *
1754 * The open entry point is called when a network interface is made
1755 * active by the system (IFF_UP). At this point all resources needed
1756 * for transmit and receive operations are allocated, the interrupt
1757 * handler is registered with the OS, the watchdog timer is started,
1758 * and the stack is notified that the interface is ready.
1759 **/
1760static int i40evf_open(struct net_device *netdev)
1761{
1762 struct i40evf_adapter *adapter = netdev_priv(netdev);
1763 int err;
1764
Mitch Williamsef8693e2014-02-13 03:48:53 -08001765 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1766 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1767 return -EIO;
1768 }
Greg Rose5eae00c2013-12-21 06:12:45 +00001769 if (adapter->state != __I40EVF_DOWN)
1770 return -EBUSY;
1771
1772 /* allocate transmit descriptors */
1773 err = i40evf_setup_all_tx_resources(adapter);
1774 if (err)
1775 goto err_setup_tx;
1776
1777 /* allocate receive descriptors */
1778 err = i40evf_setup_all_rx_resources(adapter);
1779 if (err)
1780 goto err_setup_rx;
1781
1782 /* clear any pending interrupts, may auto mask */
1783 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1784 if (err)
1785 goto err_req_irq;
1786
1787 i40evf_configure(adapter);
1788
1789 err = i40evf_up_complete(adapter);
1790 if (err)
1791 goto err_req_irq;
1792
1793 i40evf_irq_enable(adapter, true);
1794
1795 return 0;
1796
1797err_req_irq:
1798 i40evf_down(adapter);
1799 i40evf_free_traffic_irqs(adapter);
1800err_setup_rx:
1801 i40evf_free_all_rx_resources(adapter);
1802err_setup_tx:
1803 i40evf_free_all_tx_resources(adapter);
1804
1805 return err;
1806}
1807
1808/**
1809 * i40evf_close - Disables a network interface
1810 * @netdev: network interface device structure
1811 *
1812 * Returns 0, this is not allowed to fail
1813 *
1814 * The close entry point is called when an interface is de-activated
1815 * by the OS. The hardware is still under the drivers control, but
1816 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1817 * are freed, along with all transmit and receive resources.
1818 **/
1819static int i40evf_close(struct net_device *netdev)
1820{
1821 struct i40evf_adapter *adapter = netdev_priv(netdev);
1822
Mitch Williamsef8693e2014-02-13 03:48:53 -08001823 if (adapter->state <= __I40EVF_DOWN)
1824 return 0;
1825
Mitch Williamsef8693e2014-02-13 03:48:53 -08001826
Greg Rose5eae00c2013-12-21 06:12:45 +00001827 set_bit(__I40E_DOWN, &adapter->vsi.state);
1828
1829 i40evf_down(adapter);
Mitch Williamsddf0b3a2014-05-22 06:31:46 +00001830 adapter->state = __I40EVF_DOWN;
Greg Rose5eae00c2013-12-21 06:12:45 +00001831 i40evf_free_traffic_irqs(adapter);
1832
1833 i40evf_free_all_tx_resources(adapter);
1834 i40evf_free_all_rx_resources(adapter);
1835
1836 return 0;
1837}
1838
1839/**
1840 * i40evf_get_stats - Get System Network Statistics
1841 * @netdev: network interface device structure
1842 *
1843 * Returns the address of the device statistics structure.
1844 * The statistics are actually updated from the timer callback.
1845 **/
1846static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1847{
1848 struct i40evf_adapter *adapter = netdev_priv(netdev);
1849
1850 /* only return the current stats */
1851 return &adapter->net_stats;
1852}
1853
1854/**
1855 * i40evf_reinit_locked - Software reinit
1856 * @adapter: board private structure
1857 *
1858 * Reinititalizes the ring structures in response to a software configuration
1859 * change. Roughly the same as close followed by open, but skips releasing
1860 * and reallocating the interrupts.
1861 **/
1862void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1863{
1864 struct net_device *netdev = adapter->netdev;
1865 int err;
1866
1867 WARN_ON(in_interrupt());
1868
Greg Rose5eae00c2013-12-21 06:12:45 +00001869 i40evf_down(adapter);
1870
1871 /* allocate transmit descriptors */
1872 err = i40evf_setup_all_tx_resources(adapter);
1873 if (err)
1874 goto err_reinit;
1875
1876 /* allocate receive descriptors */
1877 err = i40evf_setup_all_rx_resources(adapter);
1878 if (err)
1879 goto err_reinit;
1880
1881 i40evf_configure(adapter);
1882
1883 err = i40evf_up_complete(adapter);
1884 if (err)
1885 goto err_reinit;
1886
1887 i40evf_irq_enable(adapter, true);
1888 return;
1889
1890err_reinit:
Mitch Williams80e72892014-05-10 04:49:06 +00001891 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00001892 i40evf_close(netdev);
1893}
1894
1895/**
1896 * i40evf_change_mtu - Change the Maximum Transfer Unit
1897 * @netdev: network interface device structure
1898 * @new_mtu: new value for maximum frame size
1899 *
1900 * Returns 0 on success, negative on failure
1901 **/
1902static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1903{
1904 struct i40evf_adapter *adapter = netdev_priv(netdev);
1905 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1906
1907 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1908 return -EINVAL;
1909
1910 /* must set new MTU before calling down or up */
1911 netdev->mtu = new_mtu;
1912 i40evf_reinit_locked(adapter);
1913 return 0;
1914}
1915
1916static const struct net_device_ops i40evf_netdev_ops = {
1917 .ndo_open = i40evf_open,
1918 .ndo_stop = i40evf_close,
1919 .ndo_start_xmit = i40evf_xmit_frame,
1920 .ndo_get_stats = i40evf_get_stats,
1921 .ndo_set_rx_mode = i40evf_set_rx_mode,
1922 .ndo_validate_addr = eth_validate_addr,
1923 .ndo_set_mac_address = i40evf_set_mac,
1924 .ndo_change_mtu = i40evf_change_mtu,
1925 .ndo_tx_timeout = i40evf_tx_timeout,
1926 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1927 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1928};
1929
1930/**
1931 * i40evf_check_reset_complete - check that VF reset is complete
1932 * @hw: pointer to hw struct
1933 *
1934 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1935 **/
1936static int i40evf_check_reset_complete(struct i40e_hw *hw)
1937{
1938 u32 rstat;
1939 int i;
1940
1941 for (i = 0; i < 100; i++) {
1942 rstat = rd32(hw, I40E_VFGEN_RSTAT);
1943 if (rstat == I40E_VFR_VFACTIVE)
1944 return 0;
1945 udelay(10);
1946 }
1947 return -EBUSY;
1948}
1949
1950/**
1951 * i40evf_init_task - worker thread to perform delayed initialization
1952 * @work: pointer to work_struct containing our data
1953 *
1954 * This task completes the work that was begun in probe. Due to the nature
1955 * of VF-PF communications, we may need to wait tens of milliseconds to get
1956 * reponses back from the PF. Rather than busy-wait in probe and bog down the
1957 * whole system, we'll do it in a task so we can sleep.
1958 * This task only runs during driver init. Once we've established
1959 * communications with the PF driver and set up our netdev, the watchdog
1960 * takes over.
1961 **/
1962static void i40evf_init_task(struct work_struct *work)
1963{
1964 struct i40evf_adapter *adapter = container_of(work,
1965 struct i40evf_adapter,
1966 init_task.work);
1967 struct net_device *netdev = adapter->netdev;
1968 struct i40evf_mac_filter *f;
1969 struct i40e_hw *hw = &adapter->hw;
1970 struct pci_dev *pdev = adapter->pdev;
1971 int i, err, bufsz;
1972
1973 switch (adapter->state) {
1974 case __I40EVF_STARTUP:
1975 /* driver loaded, probe complete */
Mitch Williamsef8693e2014-02-13 03:48:53 -08001976 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1977 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
Greg Rose5eae00c2013-12-21 06:12:45 +00001978 err = i40e_set_mac_type(hw);
1979 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08001980 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
1981 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001982 goto err;
1983 }
1984 err = i40evf_check_reset_complete(hw);
1985 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00001986 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08001987 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001988 goto err;
1989 }
1990 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
1991 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
1992 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
1993 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
1994
1995 err = i40evf_init_adminq(hw);
1996 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08001997 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
1998 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00001999 goto err;
2000 }
2001 err = i40evf_send_api_ver(adapter);
2002 if (err) {
Mitch Williams10bdd672014-03-06 08:59:53 +00002003 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002004 i40evf_shutdown_adminq(hw);
2005 goto err;
2006 }
2007 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2008 goto restart;
2009 break;
2010 case __I40EVF_INIT_VERSION_CHECK:
Mitch Williams10bdd672014-03-06 08:59:53 +00002011 if (!i40evf_asq_done(hw)) {
Mitch Williams80e72892014-05-10 04:49:06 +00002012 dev_err(&pdev->dev, "Admin queue command never completed\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002013 goto err;
Mitch Williams10bdd672014-03-06 08:59:53 +00002014 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002015
2016 /* aq msg sent, awaiting reply */
2017 err = i40evf_verify_api_ver(adapter);
2018 if (err) {
Mitch Williams0d9c7ea2014-04-23 04:50:00 +00002019 dev_info(&pdev->dev, "Unable to verify API version (%d), retrying\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002020 err);
Mitch Williams56f99202014-06-04 04:22:41 +00002021 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2022 dev_info(&pdev->dev, "Resending request\n");
2023 err = i40evf_send_api_ver(adapter);
2024 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002025 goto err;
2026 }
2027 err = i40evf_send_vf_config_msg(adapter);
2028 if (err) {
Mitch Williams3f2ab172014-06-04 04:22:40 +00002029 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
Greg Rose5eae00c2013-12-21 06:12:45 +00002030 err);
2031 goto err;
2032 }
2033 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2034 goto restart;
2035 break;
2036 case __I40EVF_INIT_GET_RESOURCES:
2037 /* aq msg sent, awaiting reply */
2038 if (!adapter->vf_res) {
2039 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2040 (I40E_MAX_VF_VSI *
2041 sizeof(struct i40e_virtchnl_vsi_resource));
2042 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002043 if (!adapter->vf_res)
Greg Rose5eae00c2013-12-21 06:12:45 +00002044 goto err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002045 }
2046 err = i40evf_get_vf_config(adapter);
2047 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2048 goto restart;
2049 if (err) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002050 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2051 err);
Greg Rose5eae00c2013-12-21 06:12:45 +00002052 goto err_alloc;
2053 }
2054 adapter->state = __I40EVF_INIT_SW;
2055 break;
2056 default:
2057 goto err_alloc;
2058 }
2059 /* got VF config message back from PF, now we can parse it */
2060 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2061 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2062 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2063 }
2064 if (!adapter->vsi_res) {
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002065 dev_err(&pdev->dev, "No LAN VSI found\n");
Greg Rose5eae00c2013-12-21 06:12:45 +00002066 goto err_alloc;
2067 }
2068
2069 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2070
Greg Rose5eae00c2013-12-21 06:12:45 +00002071 netdev->netdev_ops = &i40evf_netdev_ops;
2072 i40evf_set_ethtool_ops(netdev);
2073 netdev->watchdog_timeo = 5 * HZ;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002074 netdev->features |= NETIF_F_HIGHDMA |
2075 NETIF_F_SG |
Greg Rose5eae00c2013-12-21 06:12:45 +00002076 NETIF_F_IP_CSUM |
2077 NETIF_F_SCTP_CSUM |
2078 NETIF_F_IPV6_CSUM |
2079 NETIF_F_TSO |
2080 NETIF_F_TSO6 |
Greg Rose3415e8c2014-01-30 12:40:27 +00002081 NETIF_F_RXCSUM |
Greg Rose5eae00c2013-12-21 06:12:45 +00002082 NETIF_F_GRO;
2083
2084 if (adapter->vf_res->vf_offload_flags
2085 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2086 netdev->vlan_features = netdev->features;
2087 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2088 NETIF_F_HW_VLAN_CTAG_RX |
2089 NETIF_F_HW_VLAN_CTAG_FILTER;
2090 }
2091
Greg Rose3415e8c2014-01-30 12:40:27 +00002092 /* copy netdev features into list of user selectable features */
2093 netdev->hw_features |= netdev->features;
2094 netdev->hw_features &= ~NETIF_F_RXCSUM;
2095
Greg Rose5eae00c2013-12-21 06:12:45 +00002096 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002097 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
Mitch Williamsc2a137c2014-02-20 19:29:09 -08002098 adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002099 random_ether_addr(adapter->hw.mac.addr);
2100 }
Greg Rose9a173902014-05-22 06:32:02 +00002101 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2102 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002103
2104 INIT_LIST_HEAD(&adapter->mac_filter_list);
2105 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2106 f = kzalloc(sizeof(*f), GFP_ATOMIC);
2107 if (NULL == f)
2108 goto err_sw_init;
2109
Greg Rose9a173902014-05-22 06:32:02 +00002110 ether_addr_copy(f->macaddr, adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002111 f->add = true;
2112 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2113
2114 list_add(&f->list, &adapter->mac_filter_list);
2115
2116 init_timer(&adapter->watchdog_timer);
2117 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2118 adapter->watchdog_timer.data = (unsigned long)adapter;
2119 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2120
Mitch Williamsd732a182014-04-24 06:41:37 +00002121 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2122 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
Greg Rose5eae00c2013-12-21 06:12:45 +00002123 err = i40evf_init_interrupt_scheme(adapter);
2124 if (err)
2125 goto err_sw_init;
2126 i40evf_map_rings_to_vectors(adapter);
2127 i40evf_configure_rss(adapter);
2128 err = i40evf_request_misc_irq(adapter);
2129 if (err)
2130 goto err_sw_init;
2131
2132 netif_carrier_off(netdev);
2133
Greg Rose5eae00c2013-12-21 06:12:45 +00002134 adapter->vsi.id = adapter->vsi_res->vsi_id;
2135 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2136 adapter->vsi.back = adapter;
2137 adapter->vsi.base_vector = 1;
2138 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
Mitch Williamsca99eb92014-04-04 04:43:07 +00002139 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2140 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2141 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2142 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
Greg Rose5eae00c2013-12-21 06:12:45 +00002143 adapter->vsi.netdev = adapter->netdev;
2144
Mitch Williamsef8693e2014-02-13 03:48:53 -08002145 if (!adapter->netdev_registered) {
2146 err = register_netdev(netdev);
2147 if (err)
2148 goto err_register;
2149 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002150
2151 adapter->netdev_registered = true;
2152
2153 netif_tx_stop_all_queues(netdev);
2154
Mitch Williamsb34f90e2014-05-10 04:49:07 +00002155 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
Greg Rose5eae00c2013-12-21 06:12:45 +00002156 if (netdev->features & NETIF_F_GRO)
2157 dev_info(&pdev->dev, "GRO is enabled\n");
2158
2159 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2160 adapter->state = __I40EVF_DOWN;
2161 set_bit(__I40E_DOWN, &adapter->vsi.state);
2162 i40evf_misc_irq_enable(adapter);
2163 return;
2164restart:
2165 schedule_delayed_work(&adapter->init_task,
2166 msecs_to_jiffies(50));
2167 return;
2168
2169err_register:
2170 i40evf_free_misc_irq(adapter);
2171err_sw_init:
2172 i40evf_reset_interrupt_capability(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002173err_alloc:
2174 kfree(adapter->vf_res);
2175 adapter->vf_res = NULL;
2176err:
2177 /* Things went into the weeds, so try again later */
2178 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
Mitch Williams80e72892014-05-10 04:49:06 +00002179 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
Mitch Williamsef8693e2014-02-13 03:48:53 -08002180 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
Greg Rose5eae00c2013-12-21 06:12:45 +00002181 return; /* do not reschedule */
2182 }
2183 schedule_delayed_work(&adapter->init_task, HZ * 3);
Greg Rose5eae00c2013-12-21 06:12:45 +00002184}
2185
2186/**
2187 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2188 * @pdev: pci device structure
2189 **/
2190static void i40evf_shutdown(struct pci_dev *pdev)
2191{
2192 struct net_device *netdev = pci_get_drvdata(pdev);
2193
2194 netif_device_detach(netdev);
2195
2196 if (netif_running(netdev))
2197 i40evf_close(netdev);
2198
2199#ifdef CONFIG_PM
2200 pci_save_state(pdev);
2201
2202#endif
2203 pci_disable_device(pdev);
2204}
2205
2206/**
2207 * i40evf_probe - Device Initialization Routine
2208 * @pdev: PCI device information struct
2209 * @ent: entry in i40evf_pci_tbl
2210 *
2211 * Returns 0 on success, negative on failure
2212 *
2213 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2214 * The OS initialization, configuring of the adapter private structure,
2215 * and a hardware reset occur.
2216 **/
2217static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2218{
2219 struct net_device *netdev;
2220 struct i40evf_adapter *adapter = NULL;
2221 struct i40e_hw *hw = NULL;
Mitch Williamsdbbd8112014-02-20 19:29:08 -08002222 int err;
Greg Rose5eae00c2013-12-21 06:12:45 +00002223
2224 err = pci_enable_device(pdev);
2225 if (err)
2226 return err;
2227
Mitch Williams64942942014-02-11 08:26:33 +00002228 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00002229 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00002230 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2231 if (err) {
2232 dev_err(&pdev->dev,
2233 "DMA configuration failed: 0x%x\n", err);
2234 goto err_dma;
2235 }
Greg Rose5eae00c2013-12-21 06:12:45 +00002236 }
2237
2238 err = pci_request_regions(pdev, i40evf_driver_name);
2239 if (err) {
2240 dev_err(&pdev->dev,
2241 "pci_request_regions failed 0x%x\n", err);
2242 goto err_pci_reg;
2243 }
2244
2245 pci_enable_pcie_error_reporting(pdev);
2246
2247 pci_set_master(pdev);
2248
2249 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2250 MAX_TX_QUEUES);
2251 if (!netdev) {
2252 err = -ENOMEM;
2253 goto err_alloc_etherdev;
2254 }
2255
2256 SET_NETDEV_DEV(netdev, &pdev->dev);
2257
2258 pci_set_drvdata(pdev, netdev);
2259 adapter = netdev_priv(netdev);
Greg Rose5eae00c2013-12-21 06:12:45 +00002260
2261 adapter->netdev = netdev;
2262 adapter->pdev = pdev;
2263
2264 hw = &adapter->hw;
2265 hw->back = adapter;
2266
2267 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2268 adapter->state = __I40EVF_STARTUP;
2269
2270 /* Call save state here because it relies on the adapter struct. */
2271 pci_save_state(pdev);
2272
2273 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2274 pci_resource_len(pdev, 0));
2275 if (!hw->hw_addr) {
2276 err = -EIO;
2277 goto err_ioremap;
2278 }
2279 hw->vendor_id = pdev->vendor;
2280 hw->device_id = pdev->device;
2281 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2282 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2283 hw->subsystem_device_id = pdev->subsystem_device;
2284 hw->bus.device = PCI_SLOT(pdev->devfn);
2285 hw->bus.func = PCI_FUNC(pdev->devfn);
2286
2287 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2288 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2289 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2290 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2291 schedule_delayed_work(&adapter->init_task, 10);
2292
2293 return 0;
2294
2295err_ioremap:
2296 free_netdev(netdev);
2297err_alloc_etherdev:
2298 pci_release_regions(pdev);
2299err_pci_reg:
2300err_dma:
2301 pci_disable_device(pdev);
2302 return err;
2303}
2304
2305#ifdef CONFIG_PM
2306/**
2307 * i40evf_suspend - Power management suspend routine
2308 * @pdev: PCI device information struct
2309 * @state: unused
2310 *
2311 * Called when the system (VM) is entering sleep/suspend.
2312 **/
2313static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2314{
2315 struct net_device *netdev = pci_get_drvdata(pdev);
2316 struct i40evf_adapter *adapter = netdev_priv(netdev);
2317 int retval = 0;
2318
2319 netif_device_detach(netdev);
2320
2321 if (netif_running(netdev)) {
2322 rtnl_lock();
2323 i40evf_down(adapter);
2324 rtnl_unlock();
2325 }
2326 i40evf_free_misc_irq(adapter);
2327 i40evf_reset_interrupt_capability(adapter);
2328
2329 retval = pci_save_state(pdev);
2330 if (retval)
2331 return retval;
2332
2333 pci_disable_device(pdev);
2334
2335 return 0;
2336}
2337
2338/**
2339 * i40evf_resume - Power managment resume routine
2340 * @pdev: PCI device information struct
2341 *
2342 * Called when the system (VM) is resumed from sleep/suspend.
2343 **/
2344static int i40evf_resume(struct pci_dev *pdev)
2345{
2346 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2347 struct net_device *netdev = adapter->netdev;
2348 u32 err;
2349
2350 pci_set_power_state(pdev, PCI_D0);
2351 pci_restore_state(pdev);
2352 /* pci_restore_state clears dev->state_saved so call
2353 * pci_save_state to restore it.
2354 */
2355 pci_save_state(pdev);
2356
2357 err = pci_enable_device_mem(pdev);
2358 if (err) {
2359 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2360 return err;
2361 }
2362 pci_set_master(pdev);
2363
2364 rtnl_lock();
2365 err = i40evf_set_interrupt_capability(adapter);
2366 if (err) {
2367 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2368 return err;
2369 }
2370 err = i40evf_request_misc_irq(adapter);
2371 rtnl_unlock();
2372 if (err) {
2373 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2374 return err;
2375 }
2376
2377 schedule_work(&adapter->reset_task);
2378
2379 netif_device_attach(netdev);
2380
2381 return err;
2382}
2383
2384#endif /* CONFIG_PM */
2385/**
2386 * i40evf_remove - Device Removal Routine
2387 * @pdev: PCI device information struct
2388 *
2389 * i40evf_remove is called by the PCI subsystem to alert the driver
2390 * that it should release a PCI device. The could be caused by a
2391 * Hot-Plug event, or because the driver is going to be removed from
2392 * memory.
2393 **/
2394static void i40evf_remove(struct pci_dev *pdev)
2395{
2396 struct net_device *netdev = pci_get_drvdata(pdev);
2397 struct i40evf_adapter *adapter = netdev_priv(netdev);
2398 struct i40e_hw *hw = &adapter->hw;
2399
2400 cancel_delayed_work_sync(&adapter->init_task);
Mitch Williamsef8693e2014-02-13 03:48:53 -08002401 cancel_work_sync(&adapter->reset_task);
Greg Rose5eae00c2013-12-21 06:12:45 +00002402
2403 if (adapter->netdev_registered) {
2404 unregister_netdev(netdev);
2405 adapter->netdev_registered = false;
2406 }
2407 adapter->state = __I40EVF_REMOVE;
2408
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002409 if (adapter->msix_entries) {
Greg Rose5eae00c2013-12-21 06:12:45 +00002410 i40evf_misc_irq_disable(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002411 i40evf_free_misc_irq(adapter);
Greg Rose5eae00c2013-12-21 06:12:45 +00002412 i40evf_reset_interrupt_capability(adapter);
2413 }
2414
Mitch Williamse5d17c32014-06-04 04:22:38 +00002415 if (adapter->watchdog_timer.function)
2416 del_timer_sync(&adapter->watchdog_timer);
2417
Mitch Williamsdbb01c82014-02-20 19:29:07 -08002418 flush_scheduled_work();
2419
Greg Rose5eae00c2013-12-21 06:12:45 +00002420 if (hw->aq.asq.count)
2421 i40evf_shutdown_adminq(hw);
2422
2423 iounmap(hw->hw_addr);
2424 pci_release_regions(pdev);
2425
2426 i40evf_free_queues(adapter);
2427 kfree(adapter->vf_res);
2428
2429 free_netdev(netdev);
2430
2431 pci_disable_pcie_error_reporting(pdev);
2432
2433 pci_disable_device(pdev);
2434}
2435
2436static struct pci_driver i40evf_driver = {
2437 .name = i40evf_driver_name,
2438 .id_table = i40evf_pci_tbl,
2439 .probe = i40evf_probe,
2440 .remove = i40evf_remove,
2441#ifdef CONFIG_PM
2442 .suspend = i40evf_suspend,
2443 .resume = i40evf_resume,
2444#endif
2445 .shutdown = i40evf_shutdown,
2446};
2447
2448/**
2449 * i40e_init_module - Driver Registration Routine
2450 *
2451 * i40e_init_module is the first routine called when the driver is
2452 * loaded. All it does is register with the PCI subsystem.
2453 **/
2454static int __init i40evf_init_module(void)
2455{
2456 int ret;
2457 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2458 i40evf_driver_version);
2459
2460 pr_info("%s\n", i40evf_copyright);
2461
2462 ret = pci_register_driver(&i40evf_driver);
2463 return ret;
2464}
2465
2466module_init(i40evf_init_module);
2467
2468/**
2469 * i40e_exit_module - Driver Exit Cleanup Routine
2470 *
2471 * i40e_exit_module is called just before the driver is removed
2472 * from memory.
2473 **/
2474static void __exit i40evf_exit_module(void)
2475{
2476 pci_unregister_driver(&i40evf_driver);
2477}
2478
2479module_exit(i40evf_exit_module);
2480
2481/* i40evf_main.c */