1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4 * Copyright(c) 2013 - 2015 Intel Corporation.
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.
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
15 * 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/>.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
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
25 ******************************************************************************/
28 #include "i40e_prototype.h"
29 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
30 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
31 static int i40evf_close(struct net_device *netdev);
33 char i40evf_driver_name[] = "i40evf";
34 static const char i40evf_driver_string[] =
35 "Intel(R) XL710/X710 Virtual Function Network Driver";
37 #define DRV_VERSION "1.3.13"
38 const char i40evf_driver_version[] = DRV_VERSION;
39 static const char i40evf_copyright[] =
40 "Copyright (c) 2013 - 2015 Intel Corporation.";
42 /* i40evf_pci_tbl - PCI Device ID Table
44 * Wildcard entries (PCI_ANY_ID) should come last
45 * Last entry must be all 0s
47 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
48 * Class, Class Mask, private data (not used) }
50 static const struct pci_device_id i40evf_pci_tbl[] = {
51 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
52 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
53 /* required last entry */
57 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
59 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
60 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
61 MODULE_LICENSE("GPL");
62 MODULE_VERSION(DRV_VERSION);
65 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
66 * @hw: pointer to the HW structure
67 * @mem: ptr to mem struct to fill out
68 * @size: size of memory requested
69 * @alignment: what to align the allocation to
71 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
72 struct i40e_dma_mem *mem,
73 u64 size, u32 alignment)
75 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
78 return I40E_ERR_PARAM;
80 mem->size = ALIGN(size, alignment);
81 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
82 (dma_addr_t *)&mem->pa, GFP_KERNEL);
86 return I40E_ERR_NO_MEMORY;
90 * i40evf_free_dma_mem_d - OS specific memory free for shared code
91 * @hw: pointer to the HW structure
92 * @mem: ptr to mem struct to free
94 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
96 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
99 return I40E_ERR_PARAM;
100 dma_free_coherent(&adapter->pdev->dev, mem->size,
101 mem->va, (dma_addr_t)mem->pa);
106 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
107 * @hw: pointer to the HW structure
108 * @mem: ptr to mem struct to fill out
109 * @size: size of memory requested
111 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
112 struct i40e_virt_mem *mem, u32 size)
115 return I40E_ERR_PARAM;
118 mem->va = kzalloc(size, GFP_KERNEL);
123 return I40E_ERR_NO_MEMORY;
127 * i40evf_free_virt_mem_d - OS specific memory free for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to free
131 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
132 struct i40e_virt_mem *mem)
135 return I40E_ERR_PARAM;
137 /* it's ok to kfree a NULL pointer */
144 * i40evf_debug_d - OS dependent version of debug printing
145 * @hw: pointer to the HW structure
146 * @mask: debug level mask
147 * @fmt_str: printf-type format description
149 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
154 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
157 va_start(argptr, fmt_str);
158 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
161 /* the debug string is already formatted with a newline */
166 * i40evf_tx_timeout - Respond to a Tx Hang
167 * @netdev: network interface device structure
169 static void i40evf_tx_timeout(struct net_device *netdev)
171 struct i40evf_adapter *adapter = netdev_priv(netdev);
173 adapter->tx_timeout_count++;
174 if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
175 I40EVF_FLAG_RESET_NEEDED))) {
176 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
177 schedule_work(&adapter->reset_task);
182 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
183 * @adapter: board private structure
185 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
187 struct i40e_hw *hw = &adapter->hw;
189 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
192 rd32(hw, I40E_VFGEN_RSTAT);
194 synchronize_irq(adapter->msix_entries[0].vector);
198 * i40evf_misc_irq_enable - Enable default interrupt generation settings
199 * @adapter: board private structure
201 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
203 struct i40e_hw *hw = &adapter->hw;
205 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
206 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
207 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
210 rd32(hw, I40E_VFGEN_RSTAT);
214 * i40evf_irq_disable - Mask off interrupt generation on the NIC
215 * @adapter: board private structure
217 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
220 struct i40e_hw *hw = &adapter->hw;
222 if (!adapter->msix_entries)
225 for (i = 1; i < adapter->num_msix_vectors; i++) {
226 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
227 synchronize_irq(adapter->msix_entries[i].vector);
230 rd32(hw, I40E_VFGEN_RSTAT);
234 * i40evf_irq_enable_queues - Enable interrupt for specified queues
235 * @adapter: board private structure
236 * @mask: bitmap of queues to enable
238 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
240 struct i40e_hw *hw = &adapter->hw;
243 for (i = 1; i < adapter->num_msix_vectors; i++) {
244 if (mask & BIT(i - 1)) {
245 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
247 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
248 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
254 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
255 * @adapter: board private structure
256 * @mask: bitmap of vectors to trigger
258 static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
260 struct i40e_hw *hw = &adapter->hw;
265 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
266 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
267 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
268 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
269 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
271 for (i = 1; i < adapter->num_msix_vectors; i++) {
273 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
274 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
275 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
276 I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
277 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
283 * i40evf_irq_enable - Enable default interrupt generation settings
284 * @adapter: board private structure
286 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
288 struct i40e_hw *hw = &adapter->hw;
290 i40evf_misc_irq_enable(adapter);
291 i40evf_irq_enable_queues(adapter, ~0);
294 rd32(hw, I40E_VFGEN_RSTAT);
298 * i40evf_msix_aq - Interrupt handler for vector 0
299 * @irq: interrupt number
300 * @data: pointer to netdev
302 static irqreturn_t i40evf_msix_aq(int irq, void *data)
304 struct net_device *netdev = data;
305 struct i40evf_adapter *adapter = netdev_priv(netdev);
306 struct i40e_hw *hw = &adapter->hw;
310 /* handle non-queue interrupts */
311 val = rd32(hw, I40E_VFINT_ICR01);
312 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
315 val = rd32(hw, I40E_VFINT_DYN_CTL01);
316 val = val | I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
317 wr32(hw, I40E_VFINT_DYN_CTL01, val);
319 /* schedule work on the private workqueue */
320 schedule_work(&adapter->adminq_task);
326 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
327 * @irq: interrupt number
328 * @data: pointer to a q_vector
330 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
332 struct i40e_q_vector *q_vector = data;
334 if (!q_vector->tx.ring && !q_vector->rx.ring)
337 napi_schedule(&q_vector->napi);
343 * i40evf_map_vector_to_rxq - associate irqs with rx queues
344 * @adapter: board private structure
345 * @v_idx: interrupt number
346 * @r_idx: queue number
349 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
351 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
352 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
354 rx_ring->q_vector = q_vector;
355 rx_ring->next = q_vector->rx.ring;
356 rx_ring->vsi = &adapter->vsi;
357 q_vector->rx.ring = rx_ring;
358 q_vector->rx.count++;
359 q_vector->rx.latency_range = I40E_LOW_LATENCY;
363 * i40evf_map_vector_to_txq - associate irqs with tx queues
364 * @adapter: board private structure
365 * @v_idx: interrupt number
366 * @t_idx: queue number
369 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
371 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
372 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
374 tx_ring->q_vector = q_vector;
375 tx_ring->next = q_vector->tx.ring;
376 tx_ring->vsi = &adapter->vsi;
377 q_vector->tx.ring = tx_ring;
378 q_vector->tx.count++;
379 q_vector->tx.latency_range = I40E_LOW_LATENCY;
380 q_vector->num_ringpairs++;
381 q_vector->ring_mask |= BIT(t_idx);
385 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
386 * @adapter: board private structure to initialize
388 * This function maps descriptor rings to the queue-specific vectors
389 * we were allotted through the MSI-X enabling code. Ideally, we'd have
390 * one vector per ring/queue, but on a constrained vector budget, we
391 * group the rings as "efficiently" as possible. You would add new
392 * mapping configurations in here.
394 static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
398 int rxr_idx = 0, txr_idx = 0;
399 int rxr_remaining = adapter->num_active_queues;
400 int txr_remaining = adapter->num_active_queues;
405 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
407 /* The ideal configuration...
408 * We have enough vectors to map one per queue.
410 if (q_vectors >= (rxr_remaining * 2)) {
411 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
412 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
414 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
415 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
419 /* If we don't have enough vectors for a 1-to-1
420 * mapping, we'll have to group them so there are
421 * multiple queues per vector.
422 * Re-adjusting *qpv takes care of the remainder.
424 for (i = v_start; i < q_vectors; i++) {
425 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
426 for (j = 0; j < rqpv; j++) {
427 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
432 for (i = v_start; i < q_vectors; i++) {
433 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
434 for (j = 0; j < tqpv; j++) {
435 i40evf_map_vector_to_txq(adapter, i, txr_idx);
442 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
448 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
449 * @adapter: board private structure
451 * Allocates MSI-X vectors for tx and rx handling, and requests
452 * interrupts from the kernel.
455 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
457 int vector, err, q_vectors;
458 int rx_int_idx = 0, tx_int_idx = 0;
460 i40evf_irq_disable(adapter);
461 /* Decrement for Other and TCP Timer vectors */
462 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
464 for (vector = 0; vector < q_vectors; vector++) {
465 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
467 if (q_vector->tx.ring && q_vector->rx.ring) {
468 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
469 "i40evf-%s-%s-%d", basename,
470 "TxRx", rx_int_idx++);
472 } else if (q_vector->rx.ring) {
473 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
474 "i40evf-%s-%s-%d", basename,
476 } else if (q_vector->tx.ring) {
477 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
478 "i40evf-%s-%s-%d", basename,
481 /* skip this unused q_vector */
485 adapter->msix_entries[vector + NONQ_VECS].vector,
486 i40evf_msix_clean_rings,
491 dev_info(&adapter->pdev->dev,
492 "Request_irq failed, error: %d\n", err);
493 goto free_queue_irqs;
495 /* assign the mask for this irq */
496 irq_set_affinity_hint(
497 adapter->msix_entries[vector + NONQ_VECS].vector,
498 q_vector->affinity_mask);
506 irq_set_affinity_hint(
507 adapter->msix_entries[vector + NONQ_VECS].vector,
509 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
510 adapter->q_vector[vector]);
516 * i40evf_request_misc_irq - Initialize MSI-X interrupts
517 * @adapter: board private structure
519 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
520 * vector is only for the admin queue, and stays active even when the netdev
523 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
525 struct net_device *netdev = adapter->netdev;
528 snprintf(adapter->misc_vector_name,
529 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
530 dev_name(&adapter->pdev->dev));
531 err = request_irq(adapter->msix_entries[0].vector,
533 adapter->misc_vector_name, netdev);
535 dev_err(&adapter->pdev->dev,
536 "request_irq for %s failed: %d\n",
537 adapter->misc_vector_name, err);
538 free_irq(adapter->msix_entries[0].vector, netdev);
544 * i40evf_free_traffic_irqs - Free MSI-X interrupts
545 * @adapter: board private structure
547 * Frees all MSI-X vectors other than 0.
549 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
554 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
556 for (i = 0; i < q_vectors; i++) {
557 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
559 free_irq(adapter->msix_entries[i+1].vector,
560 adapter->q_vector[i]);
565 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
566 * @adapter: board private structure
568 * Frees MSI-X vector 0.
570 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
572 struct net_device *netdev = adapter->netdev;
574 free_irq(adapter->msix_entries[0].vector, netdev);
578 * i40evf_configure_tx - Configure Transmit Unit after Reset
579 * @adapter: board private structure
581 * Configure the Tx unit of the MAC after a reset.
583 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
585 struct i40e_hw *hw = &adapter->hw;
588 for (i = 0; i < adapter->num_active_queues; i++)
589 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
593 * i40evf_configure_rx - Configure Receive Unit after Reset
594 * @adapter: board private structure
596 * Configure the Rx unit of the MAC after a reset.
598 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
600 struct i40e_hw *hw = &adapter->hw;
601 struct net_device *netdev = adapter->netdev;
602 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
607 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
608 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
610 /* Decide whether to use packet split mode or not */
611 if (netdev->mtu > ETH_DATA_LEN) {
612 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
613 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
615 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
617 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
618 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
620 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
623 /* Set the RX buffer length according to the mode */
624 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
625 rx_buf_len = I40E_RX_HDR_SIZE;
627 if (netdev->mtu <= ETH_DATA_LEN)
628 rx_buf_len = I40EVF_RXBUFFER_2048;
630 rx_buf_len = ALIGN(max_frame, 1024);
633 for (i = 0; i < adapter->num_active_queues; i++) {
634 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
635 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
640 * i40evf_find_vlan - Search filter list for specific vlan filter
641 * @adapter: board private structure
644 * Returns ptr to the filter object or NULL
647 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
649 struct i40evf_vlan_filter *f;
651 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
659 * i40evf_add_vlan - Add a vlan filter to the list
660 * @adapter: board private structure
663 * Returns ptr to the filter object or NULL when no memory available.
666 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
668 struct i40evf_vlan_filter *f = NULL;
671 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
672 &adapter->crit_section)) {
678 f = i40evf_find_vlan(adapter, vlan);
680 f = kzalloc(sizeof(*f), GFP_ATOMIC);
686 INIT_LIST_HEAD(&f->list);
687 list_add(&f->list, &adapter->vlan_filter_list);
689 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
693 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
699 * i40evf_del_vlan - Remove a vlan filter from the list
700 * @adapter: board private structure
703 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
705 struct i40evf_vlan_filter *f;
708 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
709 &adapter->crit_section)) {
715 f = i40evf_find_vlan(adapter, vlan);
718 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
720 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
724 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
725 * @netdev: network device struct
728 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
729 __always_unused __be16 proto, u16 vid)
731 struct i40evf_adapter *adapter = netdev_priv(netdev);
733 if (i40evf_add_vlan(adapter, vid) == NULL)
739 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
740 * @netdev: network device struct
743 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
744 __always_unused __be16 proto, u16 vid)
746 struct i40evf_adapter *adapter = netdev_priv(netdev);
748 i40evf_del_vlan(adapter, vid);
753 * i40evf_find_filter - Search filter list for specific mac filter
754 * @adapter: board private structure
755 * @macaddr: the MAC address
757 * Returns ptr to the filter object or NULL
760 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
763 struct i40evf_mac_filter *f;
768 list_for_each_entry(f, &adapter->mac_filter_list, list) {
769 if (ether_addr_equal(macaddr, f->macaddr))
776 * i40e_add_filter - Add a mac filter to the filter list
777 * @adapter: board private structure
778 * @macaddr: the MAC address
780 * Returns ptr to the filter object or NULL when no memory available.
783 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
786 struct i40evf_mac_filter *f;
792 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
793 &adapter->crit_section)) {
799 f = i40evf_find_filter(adapter, macaddr);
801 f = kzalloc(sizeof(*f), GFP_ATOMIC);
803 clear_bit(__I40EVF_IN_CRITICAL_TASK,
804 &adapter->crit_section);
808 ether_addr_copy(f->macaddr, macaddr);
810 list_add(&f->list, &adapter->mac_filter_list);
812 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
815 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
820 * i40evf_set_mac - NDO callback to set port mac address
821 * @netdev: network interface device structure
822 * @p: pointer to an address structure
824 * Returns 0 on success, negative on failure
826 static int i40evf_set_mac(struct net_device *netdev, void *p)
828 struct i40evf_adapter *adapter = netdev_priv(netdev);
829 struct i40e_hw *hw = &adapter->hw;
830 struct i40evf_mac_filter *f;
831 struct sockaddr *addr = p;
833 if (!is_valid_ether_addr(addr->sa_data))
834 return -EADDRNOTAVAIL;
836 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
839 f = i40evf_add_filter(adapter, addr->sa_data);
841 ether_addr_copy(hw->mac.addr, addr->sa_data);
842 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
845 return (f == NULL) ? -ENOMEM : 0;
849 * i40evf_set_rx_mode - NDO callback to set the netdev filters
850 * @netdev: network interface device structure
852 static void i40evf_set_rx_mode(struct net_device *netdev)
854 struct i40evf_adapter *adapter = netdev_priv(netdev);
855 struct i40evf_mac_filter *f, *ftmp;
856 struct netdev_hw_addr *uca;
857 struct netdev_hw_addr *mca;
858 struct netdev_hw_addr *ha;
861 /* add addr if not already in the filter list */
862 netdev_for_each_uc_addr(uca, netdev) {
863 i40evf_add_filter(adapter, uca->addr);
865 netdev_for_each_mc_addr(mca, netdev) {
866 i40evf_add_filter(adapter, mca->addr);
869 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
870 &adapter->crit_section)) {
873 dev_err(&adapter->pdev->dev,
874 "Failed to get lock in %s\n", __func__);
878 /* remove filter if not in netdev list */
879 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
880 netdev_for_each_mc_addr(mca, netdev)
881 if (ether_addr_equal(mca->addr, f->macaddr))
882 goto bottom_of_search_loop;
884 netdev_for_each_uc_addr(uca, netdev)
885 if (ether_addr_equal(uca->addr, f->macaddr))
886 goto bottom_of_search_loop;
888 for_each_dev_addr(netdev, ha)
889 if (ether_addr_equal(ha->addr, f->macaddr))
890 goto bottom_of_search_loop;
892 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
893 goto bottom_of_search_loop;
895 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
897 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
899 bottom_of_search_loop:
902 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
906 * i40evf_napi_enable_all - enable NAPI on all queue vectors
907 * @adapter: board private structure
909 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
912 struct i40e_q_vector *q_vector;
913 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
915 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
916 struct napi_struct *napi;
918 q_vector = adapter->q_vector[q_idx];
919 napi = &q_vector->napi;
925 * i40evf_napi_disable_all - disable NAPI on all queue vectors
926 * @adapter: board private structure
928 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
931 struct i40e_q_vector *q_vector;
932 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
934 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
935 q_vector = adapter->q_vector[q_idx];
936 napi_disable(&q_vector->napi);
941 * i40evf_configure - set up transmit and receive data structures
942 * @adapter: board private structure
944 static void i40evf_configure(struct i40evf_adapter *adapter)
946 struct net_device *netdev = adapter->netdev;
949 i40evf_set_rx_mode(netdev);
951 i40evf_configure_tx(adapter);
952 i40evf_configure_rx(adapter);
953 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
955 for (i = 0; i < adapter->num_active_queues; i++) {
956 struct i40e_ring *ring = adapter->rx_rings[i];
958 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
959 ring->next_to_use = ring->count - 1;
960 writel(ring->next_to_use, ring->tail);
965 * i40evf_up_complete - Finish the last steps of bringing up a connection
966 * @adapter: board private structure
968 static int i40evf_up_complete(struct i40evf_adapter *adapter)
970 adapter->state = __I40EVF_RUNNING;
971 clear_bit(__I40E_DOWN, &adapter->vsi.state);
973 i40evf_napi_enable_all(adapter);
975 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
976 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
981 * i40e_down - Shutdown the connection processing
982 * @adapter: board private structure
984 void i40evf_down(struct i40evf_adapter *adapter)
986 struct net_device *netdev = adapter->netdev;
987 struct i40evf_mac_filter *f;
989 if (adapter->state == __I40EVF_DOWN)
992 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
993 &adapter->crit_section))
994 usleep_range(500, 1000);
996 netif_carrier_off(netdev);
997 netif_tx_disable(netdev);
998 i40evf_napi_disable_all(adapter);
999 i40evf_irq_disable(adapter);
1001 /* remove all MAC filters */
1002 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1005 /* remove all VLAN filters */
1006 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1009 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1010 adapter->state != __I40EVF_RESETTING) {
1011 /* cancel any current operation */
1012 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1013 /* Schedule operations to close down the HW. Don't wait
1014 * here for this to complete. The watchdog is still running
1015 * and it will take care of this.
1017 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1018 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1019 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1022 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1026 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1027 * @adapter: board private structure
1028 * @vectors: number of vectors to request
1030 * Work with the OS to set up the MSIX vectors needed.
1032 * Returns 0 on success, negative on failure
1035 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1037 int err, vector_threshold;
1039 /* We'll want at least 3 (vector_threshold):
1040 * 0) Other (Admin Queue and link, mostly)
1044 vector_threshold = MIN_MSIX_COUNT;
1046 /* The more we get, the more we will assign to Tx/Rx Cleanup
1047 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1048 * Right now, we simply care about how many we'll get; we'll
1049 * set them up later while requesting irq's.
1051 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1052 vector_threshold, vectors);
1054 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1055 kfree(adapter->msix_entries);
1056 adapter->msix_entries = NULL;
1060 /* Adjust for only the vectors we'll use, which is minimum
1061 * of max_msix_q_vectors + NONQ_VECS, or the number of
1062 * vectors we were allocated.
1064 adapter->num_msix_vectors = err;
1069 * i40evf_free_queues - Free memory for all rings
1070 * @adapter: board private structure to initialize
1072 * Free all of the memory associated with queue pairs.
1074 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1078 if (!adapter->vsi_res)
1080 for (i = 0; i < adapter->num_active_queues; i++) {
1081 if (adapter->tx_rings[i])
1082 kfree_rcu(adapter->tx_rings[i], rcu);
1083 adapter->tx_rings[i] = NULL;
1084 adapter->rx_rings[i] = NULL;
1089 * i40evf_alloc_queues - Allocate memory for all rings
1090 * @adapter: board private structure to initialize
1092 * We allocate one ring per queue at run-time since we don't know the
1093 * number of queues at compile-time. The polling_netdev array is
1094 * intended for Multiqueue, but should work fine with a single queue.
1096 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1100 for (i = 0; i < adapter->num_active_queues; i++) {
1101 struct i40e_ring *tx_ring;
1102 struct i40e_ring *rx_ring;
1104 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
1108 tx_ring->queue_index = i;
1109 tx_ring->netdev = adapter->netdev;
1110 tx_ring->dev = &adapter->pdev->dev;
1111 tx_ring->count = adapter->tx_desc_count;
1112 adapter->tx_rings[i] = tx_ring;
1114 rx_ring = &tx_ring[1];
1115 rx_ring->queue_index = i;
1116 rx_ring->netdev = adapter->netdev;
1117 rx_ring->dev = &adapter->pdev->dev;
1118 rx_ring->count = adapter->rx_desc_count;
1119 adapter->rx_rings[i] = rx_ring;
1125 i40evf_free_queues(adapter);
1130 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1131 * @adapter: board private structure to initialize
1133 * Attempt to configure the interrupts using the best available
1134 * capabilities of the hardware and the kernel.
1136 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1138 int vector, v_budget;
1142 if (!adapter->vsi_res) {
1146 pairs = adapter->num_active_queues;
1148 /* It's easy to be greedy for MSI-X vectors, but it really
1149 * doesn't do us much good if we have a lot more vectors
1150 * than CPU's. So let's be conservative and only ask for
1151 * (roughly) twice the number of vectors as there are CPU's.
1153 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1154 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
1156 adapter->msix_entries = kcalloc(v_budget,
1157 sizeof(struct msix_entry), GFP_KERNEL);
1158 if (!adapter->msix_entries) {
1163 for (vector = 0; vector < v_budget; vector++)
1164 adapter->msix_entries[vector].entry = vector;
1166 err = i40evf_acquire_msix_vectors(adapter, v_budget);
1169 adapter->netdev->real_num_tx_queues = pairs;
1174 * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1175 * @vsi: vsi structure
1176 * @seed: RSS hash seed
1178 static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1180 struct i40e_aqc_get_set_rss_key_data rss_key;
1181 struct i40evf_adapter *adapter = vsi->back;
1182 struct i40e_hw *hw = &adapter->hw;
1189 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1190 /* bail because we already have a command pending */
1191 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1192 adapter->current_op);
1196 memset(&rss_key, 0, sizeof(rss_key));
1197 memcpy(&rss_key, seed, sizeof(rss_key));
1199 rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1203 /* Populate the LUT with max no. PF queues in round robin fashion */
1204 for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1205 rss_lut[i] = i % adapter->num_active_queues;
1207 ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1209 dev_err(&adapter->pdev->dev,
1210 "Cannot set RSS key, err %s aq_err %s\n",
1211 i40evf_stat_str(hw, ret),
1212 i40evf_aq_str(hw, hw->aq.asq_last_status));
1216 ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1217 (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1219 dev_err(&adapter->pdev->dev,
1220 "Cannot set RSS lut, err %s aq_err %s\n",
1221 i40evf_stat_str(hw, ret),
1222 i40evf_aq_str(hw, hw->aq.asq_last_status));
1226 * i40e_configure_rss_reg - Prepare for RSS if used
1227 * @adapter: board private structure
1228 * @seed: RSS hash seed
1230 static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1233 struct i40e_hw *hw = &adapter->hw;
1234 u32 *seed_dw = (u32 *)seed;
1239 /* Fill out hash function seed */
1240 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1241 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1243 /* Populate the LUT with max no. PF queues in round robin fashion */
1244 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1246 for (j = 0; j < 4; j++) {
1247 if (cqueue == adapter->num_active_queues)
1249 lut |= ((cqueue) << (8 * j));
1252 wr32(hw, I40E_VFQF_HLUT(i), lut);
1258 * i40evf_configure_rss - Prepare for RSS
1259 * @adapter: board private structure
1261 static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1263 struct i40e_hw *hw = &adapter->hw;
1264 u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1267 netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1269 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1270 hena = I40E_DEFAULT_RSS_HENA;
1271 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1272 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1274 if (RSS_AQ(adapter))
1275 i40evf_configure_rss_aq(&adapter->vsi, seed);
1277 i40evf_configure_rss_reg(adapter, seed);
1281 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1282 * @adapter: board private structure to initialize
1284 * We allocate one q_vector per queue interrupt. If allocation fails we
1287 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1289 int q_idx, num_q_vectors;
1290 struct i40e_q_vector *q_vector;
1292 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1294 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1295 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
1298 q_vector->adapter = adapter;
1299 q_vector->vsi = &adapter->vsi;
1300 q_vector->v_idx = q_idx;
1301 netif_napi_add(adapter->netdev, &q_vector->napi,
1302 i40evf_napi_poll, NAPI_POLL_WEIGHT);
1303 adapter->q_vector[q_idx] = q_vector;
1311 q_vector = adapter->q_vector[q_idx];
1312 netif_napi_del(&q_vector->napi);
1314 adapter->q_vector[q_idx] = NULL;
1320 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1321 * @adapter: board private structure to initialize
1323 * This function frees the memory allocated to the q_vectors. In addition if
1324 * NAPI is enabled it will delete any references to the NAPI struct prior
1325 * to freeing the q_vector.
1327 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1329 int q_idx, num_q_vectors;
1332 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1333 napi_vectors = adapter->num_active_queues;
1335 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1336 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1338 adapter->q_vector[q_idx] = NULL;
1339 if (q_idx < napi_vectors)
1340 netif_napi_del(&q_vector->napi);
1346 * i40evf_reset_interrupt_capability - Reset MSIX setup
1347 * @adapter: board private structure
1350 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1352 pci_disable_msix(adapter->pdev);
1353 kfree(adapter->msix_entries);
1354 adapter->msix_entries = NULL;
1358 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1359 * @adapter: board private structure to initialize
1362 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1366 err = i40evf_set_interrupt_capability(adapter);
1368 dev_err(&adapter->pdev->dev,
1369 "Unable to setup interrupt capabilities\n");
1370 goto err_set_interrupt;
1373 err = i40evf_alloc_q_vectors(adapter);
1375 dev_err(&adapter->pdev->dev,
1376 "Unable to allocate memory for queue vectors\n");
1377 goto err_alloc_q_vectors;
1380 err = i40evf_alloc_queues(adapter);
1382 dev_err(&adapter->pdev->dev,
1383 "Unable to allocate memory for queues\n");
1384 goto err_alloc_queues;
1387 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1388 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1389 adapter->num_active_queues);
1393 i40evf_free_q_vectors(adapter);
1394 err_alloc_q_vectors:
1395 i40evf_reset_interrupt_capability(adapter);
1401 * i40evf_watchdog_timer - Periodic call-back timer
1402 * @data: pointer to adapter disguised as unsigned long
1404 static void i40evf_watchdog_timer(unsigned long data)
1406 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1408 schedule_work(&adapter->watchdog_task);
1409 /* timer will be rescheduled in watchdog task */
1413 * i40evf_watchdog_task - Periodic call-back task
1414 * @work: pointer to work_struct
1416 static void i40evf_watchdog_task(struct work_struct *work)
1418 struct i40evf_adapter *adapter = container_of(work,
1419 struct i40evf_adapter,
1421 struct i40e_hw *hw = &adapter->hw;
1424 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1425 goto restart_watchdog;
1427 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1428 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1429 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1430 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1431 (rstat_val == I40E_VFR_COMPLETED)) {
1432 /* A chance for redemption! */
1433 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1434 adapter->state = __I40EVF_STARTUP;
1435 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1436 schedule_delayed_work(&adapter->init_task, 10);
1437 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1438 &adapter->crit_section);
1439 /* Don't reschedule the watchdog, since we've restarted
1440 * the init task. When init_task contacts the PF and
1441 * gets everything set up again, it'll restart the
1442 * watchdog for us. Down, boy. Sit. Stay. Woof.
1446 adapter->aq_required = 0;
1447 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1451 if ((adapter->state < __I40EVF_DOWN) ||
1452 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1455 /* check for reset */
1456 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1457 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1458 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
1459 (rstat_val != I40E_VFR_VFACTIVE) &&
1460 (rstat_val != I40E_VFR_COMPLETED)) {
1461 adapter->state = __I40EVF_RESETTING;
1462 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1463 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1464 schedule_work(&adapter->reset_task);
1465 adapter->aq_required = 0;
1466 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1470 /* Process admin queue tasks. After init, everything gets done
1471 * here so we don't race on the admin queue.
1473 if (adapter->current_op) {
1474 if (!i40evf_asq_done(hw)) {
1475 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1476 i40evf_send_api_ver(adapter);
1480 if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1481 i40evf_send_vf_config_msg(adapter);
1485 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1486 i40evf_disable_queues(adapter);
1490 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1491 i40evf_map_queues(adapter);
1495 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1496 i40evf_add_ether_addrs(adapter);
1500 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1501 i40evf_add_vlans(adapter);
1505 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1506 i40evf_del_ether_addrs(adapter);
1510 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1511 i40evf_del_vlans(adapter);
1515 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1516 i40evf_configure_queues(adapter);
1520 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1521 i40evf_enable_queues(adapter);
1525 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1526 /* This message goes straight to the firmware, not the
1527 * PF, so we don't have to set current_op as we will
1528 * not get a response through the ARQ.
1530 i40evf_configure_rss(adapter);
1531 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1535 if (adapter->state == __I40EVF_RUNNING)
1536 i40evf_request_stats(adapter);
1538 if (adapter->state == __I40EVF_RUNNING) {
1539 i40evf_irq_enable_queues(adapter, ~0);
1540 i40evf_fire_sw_int(adapter, 0xFF);
1542 i40evf_fire_sw_int(adapter, 0x1);
1545 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1547 if (adapter->state == __I40EVF_REMOVE)
1549 if (adapter->aq_required)
1550 mod_timer(&adapter->watchdog_timer,
1551 jiffies + msecs_to_jiffies(20));
1553 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1554 schedule_work(&adapter->adminq_task);
1557 #define I40EVF_RESET_WAIT_MS 10
1558 #define I40EVF_RESET_WAIT_COUNT 500
1560 * i40evf_reset_task - Call-back task to handle hardware reset
1561 * @work: pointer to work_struct
1563 * During reset we need to shut down and reinitialize the admin queue
1564 * before we can use it to communicate with the PF again. We also clear
1565 * and reinit the rings because that context is lost as well.
1567 static void i40evf_reset_task(struct work_struct *work)
1569 struct i40evf_adapter *adapter = container_of(work,
1570 struct i40evf_adapter,
1572 struct net_device *netdev = adapter->netdev;
1573 struct i40e_hw *hw = &adapter->hw;
1574 struct i40evf_mac_filter *f;
1578 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1579 &adapter->crit_section))
1580 usleep_range(500, 1000);
1582 i40evf_misc_irq_disable(adapter);
1583 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1584 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1585 /* Restart the AQ here. If we have been reset but didn't
1586 * detect it, or if the PF had to reinit, our AQ will be hosed.
1588 i40evf_shutdown_adminq(hw);
1589 i40evf_init_adminq(hw);
1590 i40evf_request_reset(adapter);
1592 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1594 /* poll until we see the reset actually happen */
1595 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1596 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1597 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1598 if ((rstat_val != I40E_VFR_VFACTIVE) &&
1599 (rstat_val != I40E_VFR_COMPLETED))
1601 usleep_range(500, 1000);
1603 if (i == I40EVF_RESET_WAIT_COUNT) {
1604 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1605 goto continue_reset; /* act like the reset happened */
1608 /* wait until the reset is complete and the PF is responding to us */
1609 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1610 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1611 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1612 if (rstat_val == I40E_VFR_VFACTIVE)
1614 msleep(I40EVF_RESET_WAIT_MS);
1616 /* extra wait to make sure minimum wait is met */
1617 msleep(I40EVF_RESET_WAIT_MS);
1618 if (i == I40EVF_RESET_WAIT_COUNT) {
1619 struct i40evf_mac_filter *f, *ftmp;
1620 struct i40evf_vlan_filter *fv, *fvtmp;
1622 /* reset never finished */
1623 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1625 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1627 if (netif_running(adapter->netdev)) {
1628 set_bit(__I40E_DOWN, &adapter->vsi.state);
1629 netif_carrier_off(netdev);
1630 netif_tx_disable(netdev);
1631 i40evf_napi_disable_all(adapter);
1632 i40evf_irq_disable(adapter);
1633 i40evf_free_traffic_irqs(adapter);
1634 i40evf_free_all_tx_resources(adapter);
1635 i40evf_free_all_rx_resources(adapter);
1638 /* Delete all of the filters, both MAC and VLAN. */
1639 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1645 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1647 list_del(&fv->list);
1651 i40evf_free_misc_irq(adapter);
1652 i40evf_reset_interrupt_capability(adapter);
1653 i40evf_free_queues(adapter);
1654 i40evf_free_q_vectors(adapter);
1655 kfree(adapter->vf_res);
1656 i40evf_shutdown_adminq(hw);
1657 adapter->netdev->flags &= ~IFF_UP;
1658 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1659 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1660 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1661 return; /* Do not attempt to reinit. It's dead, Jim. */
1665 if (netif_running(adapter->netdev)) {
1666 netif_carrier_off(netdev);
1667 netif_tx_stop_all_queues(netdev);
1668 i40evf_napi_disable_all(adapter);
1670 i40evf_irq_disable(adapter);
1672 adapter->state = __I40EVF_RESETTING;
1673 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1675 /* free the Tx/Rx rings and descriptors, might be better to just
1676 * re-use them sometime in the future
1678 i40evf_free_all_rx_resources(adapter);
1679 i40evf_free_all_tx_resources(adapter);
1681 /* kill and reinit the admin queue */
1682 if (i40evf_shutdown_adminq(hw))
1683 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1684 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1685 err = i40evf_init_adminq(hw);
1687 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1690 adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1691 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1693 /* re-add all MAC filters */
1694 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1697 /* re-add all VLAN filters */
1698 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1701 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1702 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1703 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1704 i40evf_misc_irq_enable(adapter);
1706 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1708 if (netif_running(adapter->netdev)) {
1709 /* allocate transmit descriptors */
1710 err = i40evf_setup_all_tx_resources(adapter);
1714 /* allocate receive descriptors */
1715 err = i40evf_setup_all_rx_resources(adapter);
1719 i40evf_configure(adapter);
1721 err = i40evf_up_complete(adapter);
1725 i40evf_irq_enable(adapter, true);
1727 adapter->state = __I40EVF_DOWN;
1732 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1733 i40evf_close(adapter->netdev);
1737 * i40evf_adminq_task - worker thread to clean the admin queue
1738 * @work: pointer to work_struct containing our data
1740 static void i40evf_adminq_task(struct work_struct *work)
1742 struct i40evf_adapter *adapter =
1743 container_of(work, struct i40evf_adapter, adminq_task);
1744 struct i40e_hw *hw = &adapter->hw;
1745 struct i40e_arq_event_info event;
1746 struct i40e_virtchnl_msg *v_msg;
1751 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1754 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1755 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1759 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1761 ret = i40evf_clean_arq_element(hw, &event, &pending);
1762 if (ret || !v_msg->v_opcode)
1763 break; /* No event to process or error cleaning ARQ */
1765 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1766 v_msg->v_retval, event.msg_buf,
1769 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1772 if ((adapter->flags &
1773 (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1774 adapter->state == __I40EVF_RESETTING)
1777 /* check for error indications */
1778 val = rd32(hw, hw->aq.arq.len);
1780 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
1781 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1782 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
1784 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
1785 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1786 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
1788 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
1789 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1790 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
1793 wr32(hw, hw->aq.arq.len, val);
1795 val = rd32(hw, hw->aq.asq.len);
1797 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
1798 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1799 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
1801 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
1802 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1803 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
1805 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
1806 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1807 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
1810 wr32(hw, hw->aq.asq.len, val);
1813 kfree(event.msg_buf);
1815 /* re-enable Admin queue interrupt cause */
1816 i40evf_misc_irq_enable(adapter);
1820 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1821 * @adapter: board private structure
1823 * Free all transmit software resources
1825 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1829 for (i = 0; i < adapter->num_active_queues; i++)
1830 if (adapter->tx_rings[i]->desc)
1831 i40evf_free_tx_resources(adapter->tx_rings[i]);
1835 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1836 * @adapter: board private structure
1838 * If this function returns with an error, then it's possible one or
1839 * more of the rings is populated (while the rest are not). It is the
1840 * callers duty to clean those orphaned rings.
1842 * Return 0 on success, negative on failure
1844 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1848 for (i = 0; i < adapter->num_active_queues; i++) {
1849 adapter->tx_rings[i]->count = adapter->tx_desc_count;
1850 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1853 dev_err(&adapter->pdev->dev,
1854 "Allocation for Tx Queue %u failed\n", i);
1862 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1863 * @adapter: board private structure
1865 * If this function returns with an error, then it's possible one or
1866 * more of the rings is populated (while the rest are not). It is the
1867 * callers duty to clean those orphaned rings.
1869 * Return 0 on success, negative on failure
1871 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1875 for (i = 0; i < adapter->num_active_queues; i++) {
1876 adapter->rx_rings[i]->count = adapter->rx_desc_count;
1877 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1880 dev_err(&adapter->pdev->dev,
1881 "Allocation for Rx Queue %u failed\n", i);
1888 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1889 * @adapter: board private structure
1891 * Free all receive software resources
1893 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1897 for (i = 0; i < adapter->num_active_queues; i++)
1898 if (adapter->rx_rings[i]->desc)
1899 i40evf_free_rx_resources(adapter->rx_rings[i]);
1903 * i40evf_open - Called when a network interface is made active
1904 * @netdev: network interface device structure
1906 * Returns 0 on success, negative value on failure
1908 * The open entry point is called when a network interface is made
1909 * active by the system (IFF_UP). At this point all resources needed
1910 * for transmit and receive operations are allocated, the interrupt
1911 * handler is registered with the OS, the watchdog timer is started,
1912 * and the stack is notified that the interface is ready.
1914 static int i40evf_open(struct net_device *netdev)
1916 struct i40evf_adapter *adapter = netdev_priv(netdev);
1919 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1920 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1923 if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
1926 /* allocate transmit descriptors */
1927 err = i40evf_setup_all_tx_resources(adapter);
1931 /* allocate receive descriptors */
1932 err = i40evf_setup_all_rx_resources(adapter);
1936 /* clear any pending interrupts, may auto mask */
1937 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1941 i40evf_add_filter(adapter, adapter->hw.mac.addr);
1942 i40evf_configure(adapter);
1944 err = i40evf_up_complete(adapter);
1948 i40evf_irq_enable(adapter, true);
1953 i40evf_down(adapter);
1954 i40evf_free_traffic_irqs(adapter);
1956 i40evf_free_all_rx_resources(adapter);
1958 i40evf_free_all_tx_resources(adapter);
1964 * i40evf_close - Disables a network interface
1965 * @netdev: network interface device structure
1967 * Returns 0, this is not allowed to fail
1969 * The close entry point is called when an interface is de-activated
1970 * by the OS. The hardware is still under the drivers control, but
1971 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1972 * are freed, along with all transmit and receive resources.
1974 static int i40evf_close(struct net_device *netdev)
1976 struct i40evf_adapter *adapter = netdev_priv(netdev);
1978 if (adapter->state <= __I40EVF_DOWN)
1982 set_bit(__I40E_DOWN, &adapter->vsi.state);
1984 i40evf_down(adapter);
1985 adapter->state = __I40EVF_DOWN;
1986 i40evf_free_traffic_irqs(adapter);
1992 * i40evf_get_stats - Get System Network Statistics
1993 * @netdev: network interface device structure
1995 * Returns the address of the device statistics structure.
1996 * The statistics are actually updated from the timer callback.
1998 static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2000 struct i40evf_adapter *adapter = netdev_priv(netdev);
2002 /* only return the current stats */
2003 return &adapter->net_stats;
2007 * i40evf_change_mtu - Change the Maximum Transfer Unit
2008 * @netdev: network interface device structure
2009 * @new_mtu: new value for maximum frame size
2011 * Returns 0 on success, negative on failure
2013 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2015 struct i40evf_adapter *adapter = netdev_priv(netdev);
2016 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2018 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2021 netdev->mtu = new_mtu;
2022 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2023 schedule_work(&adapter->reset_task);
2028 static const struct net_device_ops i40evf_netdev_ops = {
2029 .ndo_open = i40evf_open,
2030 .ndo_stop = i40evf_close,
2031 .ndo_start_xmit = i40evf_xmit_frame,
2032 .ndo_get_stats = i40evf_get_stats,
2033 .ndo_set_rx_mode = i40evf_set_rx_mode,
2034 .ndo_validate_addr = eth_validate_addr,
2035 .ndo_set_mac_address = i40evf_set_mac,
2036 .ndo_change_mtu = i40evf_change_mtu,
2037 .ndo_tx_timeout = i40evf_tx_timeout,
2038 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
2039 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
2043 * i40evf_check_reset_complete - check that VF reset is complete
2044 * @hw: pointer to hw struct
2046 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2048 static int i40evf_check_reset_complete(struct i40e_hw *hw)
2053 for (i = 0; i < 100; i++) {
2054 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2055 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2056 if ((rstat == I40E_VFR_VFACTIVE) ||
2057 (rstat == I40E_VFR_COMPLETED))
2059 usleep_range(10, 20);
2065 * i40evf_process_config - Process the config information we got from the PF
2066 * @adapter: board private structure
2068 * Verify that we have a valid config struct, and set up our netdev features
2069 * and our VSI struct.
2071 int i40evf_process_config(struct i40evf_adapter *adapter)
2073 struct net_device *netdev = adapter->netdev;
2076 /* got VF config message back from PF, now we can parse it */
2077 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2078 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2079 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2081 if (!adapter->vsi_res) {
2082 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2086 if (adapter->vf_res->vf_offload_flags
2087 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2088 netdev->vlan_features = netdev->features;
2089 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2090 NETIF_F_HW_VLAN_CTAG_RX |
2091 NETIF_F_HW_VLAN_CTAG_FILTER;
2093 netdev->features |= NETIF_F_HIGHDMA |
2103 /* copy netdev features into list of user selectable features */
2104 netdev->hw_features |= netdev->features;
2105 netdev->hw_features &= ~NETIF_F_RXCSUM;
2107 adapter->vsi.id = adapter->vsi_res->vsi_id;
2109 adapter->vsi.back = adapter;
2110 adapter->vsi.base_vector = 1;
2111 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2112 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2113 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2114 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2115 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2116 adapter->vsi.netdev = adapter->netdev;
2121 * i40evf_init_task - worker thread to perform delayed initialization
2122 * @work: pointer to work_struct containing our data
2124 * This task completes the work that was begun in probe. Due to the nature
2125 * of VF-PF communications, we may need to wait tens of milliseconds to get
2126 * responses back from the PF. Rather than busy-wait in probe and bog down the
2127 * whole system, we'll do it in a task so we can sleep.
2128 * This task only runs during driver init. Once we've established
2129 * communications with the PF driver and set up our netdev, the watchdog
2132 static void i40evf_init_task(struct work_struct *work)
2134 struct i40evf_adapter *adapter = container_of(work,
2135 struct i40evf_adapter,
2137 struct net_device *netdev = adapter->netdev;
2138 struct i40e_hw *hw = &adapter->hw;
2139 struct pci_dev *pdev = adapter->pdev;
2142 switch (adapter->state) {
2143 case __I40EVF_STARTUP:
2144 /* driver loaded, probe complete */
2145 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2146 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2147 err = i40e_set_mac_type(hw);
2149 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2153 err = i40evf_check_reset_complete(hw);
2155 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2159 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2160 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2161 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2162 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2164 err = i40evf_init_adminq(hw);
2166 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2170 err = i40evf_send_api_ver(adapter);
2172 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2173 i40evf_shutdown_adminq(hw);
2176 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2178 case __I40EVF_INIT_VERSION_CHECK:
2179 if (!i40evf_asq_done(hw)) {
2180 dev_err(&pdev->dev, "Admin queue command never completed\n");
2181 i40evf_shutdown_adminq(hw);
2182 adapter->state = __I40EVF_STARTUP;
2186 /* aq msg sent, awaiting reply */
2187 err = i40evf_verify_api_ver(adapter);
2189 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2190 err = i40evf_send_api_ver(adapter);
2192 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2193 adapter->pf_version.major,
2194 adapter->pf_version.minor,
2195 I40E_VIRTCHNL_VERSION_MAJOR,
2196 I40E_VIRTCHNL_VERSION_MINOR);
2199 err = i40evf_send_vf_config_msg(adapter);
2201 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2205 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2207 case __I40EVF_INIT_GET_RESOURCES:
2208 /* aq msg sent, awaiting reply */
2209 if (!adapter->vf_res) {
2210 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2212 sizeof(struct i40e_virtchnl_vsi_resource));
2213 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2214 if (!adapter->vf_res)
2217 err = i40evf_get_vf_config(adapter);
2218 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2219 err = i40evf_send_vf_config_msg(adapter);
2223 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2227 adapter->state = __I40EVF_INIT_SW;
2232 if (i40evf_process_config(adapter))
2234 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
2236 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2238 netdev->netdev_ops = &i40evf_netdev_ops;
2239 i40evf_set_ethtool_ops(netdev);
2240 netdev->watchdog_timeo = 5 * HZ;
2242 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2243 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2244 adapter->hw.mac.addr);
2245 random_ether_addr(adapter->hw.mac.addr);
2247 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2248 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2250 init_timer(&adapter->watchdog_timer);
2251 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2252 adapter->watchdog_timer.data = (unsigned long)adapter;
2253 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2255 adapter->num_active_queues = min_t(int,
2256 adapter->vsi_res->num_queue_pairs,
2257 (int)(num_online_cpus()));
2258 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2259 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2260 err = i40evf_init_interrupt_scheme(adapter);
2263 i40evf_map_rings_to_vectors(adapter);
2264 if (!RSS_AQ(adapter))
2265 i40evf_configure_rss(adapter);
2266 err = i40evf_request_misc_irq(adapter);
2270 netif_carrier_off(netdev);
2272 if (!adapter->netdev_registered) {
2273 err = register_netdev(netdev);
2278 adapter->netdev_registered = true;
2280 netif_tx_stop_all_queues(netdev);
2282 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2283 if (netdev->features & NETIF_F_GRO)
2284 dev_info(&pdev->dev, "GRO is enabled\n");
2286 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2287 adapter->state = __I40EVF_DOWN;
2288 set_bit(__I40E_DOWN, &adapter->vsi.state);
2289 i40evf_misc_irq_enable(adapter);
2291 if (RSS_AQ(adapter)) {
2292 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2293 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2295 i40evf_configure_rss(adapter);
2299 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
2303 i40evf_free_misc_irq(adapter);
2305 i40evf_reset_interrupt_capability(adapter);
2307 kfree(adapter->vf_res);
2308 adapter->vf_res = NULL;
2310 /* Things went into the weeds, so try again later */
2311 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2312 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
2313 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2314 return; /* do not reschedule */
2316 schedule_delayed_work(&adapter->init_task, HZ * 3);
2320 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2321 * @pdev: pci device structure
2323 static void i40evf_shutdown(struct pci_dev *pdev)
2325 struct net_device *netdev = pci_get_drvdata(pdev);
2326 struct i40evf_adapter *adapter = netdev_priv(netdev);
2328 netif_device_detach(netdev);
2330 if (netif_running(netdev))
2331 i40evf_close(netdev);
2333 /* Prevent the watchdog from running. */
2334 adapter->state = __I40EVF_REMOVE;
2335 adapter->aq_required = 0;
2338 pci_save_state(pdev);
2341 pci_disable_device(pdev);
2345 * i40evf_probe - Device Initialization Routine
2346 * @pdev: PCI device information struct
2347 * @ent: entry in i40evf_pci_tbl
2349 * Returns 0 on success, negative on failure
2351 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2352 * The OS initialization, configuring of the adapter private structure,
2353 * and a hardware reset occur.
2355 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2357 struct net_device *netdev;
2358 struct i40evf_adapter *adapter = NULL;
2359 struct i40e_hw *hw = NULL;
2362 err = pci_enable_device(pdev);
2366 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2368 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2371 "DMA configuration failed: 0x%x\n", err);
2376 err = pci_request_regions(pdev, i40evf_driver_name);
2379 "pci_request_regions failed 0x%x\n", err);
2383 pci_enable_pcie_error_reporting(pdev);
2385 pci_set_master(pdev);
2387 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2391 goto err_alloc_etherdev;
2394 SET_NETDEV_DEV(netdev, &pdev->dev);
2396 pci_set_drvdata(pdev, netdev);
2397 adapter = netdev_priv(netdev);
2399 adapter->netdev = netdev;
2400 adapter->pdev = pdev;
2405 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2406 adapter->state = __I40EVF_STARTUP;
2408 /* Call save state here because it relies on the adapter struct. */
2409 pci_save_state(pdev);
2411 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2412 pci_resource_len(pdev, 0));
2417 hw->vendor_id = pdev->vendor;
2418 hw->device_id = pdev->device;
2419 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2420 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2421 hw->subsystem_device_id = pdev->subsystem_device;
2422 hw->bus.device = PCI_SLOT(pdev->devfn);
2423 hw->bus.func = PCI_FUNC(pdev->devfn);
2425 INIT_LIST_HEAD(&adapter->mac_filter_list);
2426 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2428 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2429 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2430 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2431 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2432 schedule_delayed_work(&adapter->init_task,
2433 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
2438 free_netdev(netdev);
2440 pci_release_regions(pdev);
2443 pci_disable_device(pdev);
2449 * i40evf_suspend - Power management suspend routine
2450 * @pdev: PCI device information struct
2453 * Called when the system (VM) is entering sleep/suspend.
2455 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2457 struct net_device *netdev = pci_get_drvdata(pdev);
2458 struct i40evf_adapter *adapter = netdev_priv(netdev);
2461 netif_device_detach(netdev);
2463 if (netif_running(netdev)) {
2465 i40evf_down(adapter);
2468 i40evf_free_misc_irq(adapter);
2469 i40evf_reset_interrupt_capability(adapter);
2471 retval = pci_save_state(pdev);
2475 pci_disable_device(pdev);
2481 * i40evf_resume - Power management resume routine
2482 * @pdev: PCI device information struct
2484 * Called when the system (VM) is resumed from sleep/suspend.
2486 static int i40evf_resume(struct pci_dev *pdev)
2488 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2489 struct net_device *netdev = adapter->netdev;
2492 pci_set_power_state(pdev, PCI_D0);
2493 pci_restore_state(pdev);
2494 /* pci_restore_state clears dev->state_saved so call
2495 * pci_save_state to restore it.
2497 pci_save_state(pdev);
2499 err = pci_enable_device_mem(pdev);
2501 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2504 pci_set_master(pdev);
2507 err = i40evf_set_interrupt_capability(adapter);
2510 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2513 err = i40evf_request_misc_irq(adapter);
2516 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2520 schedule_work(&adapter->reset_task);
2522 netif_device_attach(netdev);
2527 #endif /* CONFIG_PM */
2529 * i40evf_remove - Device Removal Routine
2530 * @pdev: PCI device information struct
2532 * i40evf_remove is called by the PCI subsystem to alert the driver
2533 * that it should release a PCI device. The could be caused by a
2534 * Hot-Plug event, or because the driver is going to be removed from
2537 static void i40evf_remove(struct pci_dev *pdev)
2539 struct net_device *netdev = pci_get_drvdata(pdev);
2540 struct i40evf_adapter *adapter = netdev_priv(netdev);
2541 struct i40evf_mac_filter *f, *ftmp;
2542 struct i40e_hw *hw = &adapter->hw;
2544 cancel_delayed_work_sync(&adapter->init_task);
2545 cancel_work_sync(&adapter->reset_task);
2547 if (adapter->netdev_registered) {
2548 unregister_netdev(netdev);
2549 adapter->netdev_registered = false;
2552 /* Shut down all the garbage mashers on the detention level */
2553 adapter->state = __I40EVF_REMOVE;
2554 adapter->aq_required = 0;
2555 i40evf_request_reset(adapter);
2557 /* If the FW isn't responding, kick it once, but only once. */
2558 if (!i40evf_asq_done(hw)) {
2559 i40evf_request_reset(adapter);
2563 if (adapter->msix_entries) {
2564 i40evf_misc_irq_disable(adapter);
2565 i40evf_free_misc_irq(adapter);
2566 i40evf_reset_interrupt_capability(adapter);
2567 i40evf_free_q_vectors(adapter);
2570 if (adapter->watchdog_timer.function)
2571 del_timer_sync(&adapter->watchdog_timer);
2573 flush_scheduled_work();
2575 if (hw->aq.asq.count)
2576 i40evf_shutdown_adminq(hw);
2578 iounmap(hw->hw_addr);
2579 pci_release_regions(pdev);
2581 i40evf_free_all_tx_resources(adapter);
2582 i40evf_free_all_rx_resources(adapter);
2583 i40evf_free_queues(adapter);
2584 kfree(adapter->vf_res);
2585 /* If we got removed before an up/down sequence, we've got a filter
2586 * hanging out there that we need to get rid of.
2588 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2592 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2597 free_netdev(netdev);
2599 pci_disable_pcie_error_reporting(pdev);
2601 pci_disable_device(pdev);
2604 static struct pci_driver i40evf_driver = {
2605 .name = i40evf_driver_name,
2606 .id_table = i40evf_pci_tbl,
2607 .probe = i40evf_probe,
2608 .remove = i40evf_remove,
2610 .suspend = i40evf_suspend,
2611 .resume = i40evf_resume,
2613 .shutdown = i40evf_shutdown,
2617 * i40e_init_module - Driver Registration Routine
2619 * i40e_init_module is the first routine called when the driver is
2620 * loaded. All it does is register with the PCI subsystem.
2622 static int __init i40evf_init_module(void)
2626 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2627 i40evf_driver_version);
2629 pr_info("%s\n", i40evf_copyright);
2631 ret = pci_register_driver(&i40evf_driver);
2635 module_init(i40evf_init_module);
2638 * i40e_exit_module - Driver Exit Cleanup Routine
2640 * i40e_exit_module is called just before the driver is removed
2643 static void __exit i40evf_exit_module(void)
2645 pci_unregister_driver(&i40evf_driver);
2648 module_exit(i40evf_exit_module);