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.2.25"
38 const char i40evf_driver_version[] = DRV_VERSION;
39 static const char i40evf_copyright[] =
40 "Copyright (c) 2013 - 2014 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 /* required last entry */
56 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
58 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
59 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(DRV_VERSION);
64 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
65 * @hw: pointer to the HW structure
66 * @mem: ptr to mem struct to fill out
67 * @size: size of memory requested
68 * @alignment: what to align the allocation to
70 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
71 struct i40e_dma_mem *mem,
72 u64 size, u32 alignment)
74 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
77 return I40E_ERR_PARAM;
79 mem->size = ALIGN(size, alignment);
80 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
81 (dma_addr_t *)&mem->pa, GFP_KERNEL);
85 return I40E_ERR_NO_MEMORY;
89 * i40evf_free_dma_mem_d - OS specific memory free for shared code
90 * @hw: pointer to the HW structure
91 * @mem: ptr to mem struct to free
93 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
95 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
98 return I40E_ERR_PARAM;
99 dma_free_coherent(&adapter->pdev->dev, mem->size,
100 mem->va, (dma_addr_t)mem->pa);
105 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
106 * @hw: pointer to the HW structure
107 * @mem: ptr to mem struct to fill out
108 * @size: size of memory requested
110 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
111 struct i40e_virt_mem *mem, u32 size)
114 return I40E_ERR_PARAM;
117 mem->va = kzalloc(size, GFP_KERNEL);
122 return I40E_ERR_NO_MEMORY;
126 * i40evf_free_virt_mem_d - OS specific memory free for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to free
130 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
131 struct i40e_virt_mem *mem)
134 return I40E_ERR_PARAM;
136 /* it's ok to kfree a NULL pointer */
143 * i40evf_debug_d - OS dependent version of debug printing
144 * @hw: pointer to the HW structure
145 * @mask: debug level mask
146 * @fmt_str: printf-type format description
148 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
153 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
156 va_start(argptr, fmt_str);
157 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
160 /* the debug string is already formatted with a newline */
165 * i40evf_tx_timeout - Respond to a Tx Hang
166 * @netdev: network interface device structure
168 static void i40evf_tx_timeout(struct net_device *netdev)
170 struct i40evf_adapter *adapter = netdev_priv(netdev);
172 adapter->tx_timeout_count++;
173 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING)) {
174 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
175 schedule_work(&adapter->reset_task);
180 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
181 * @adapter: board private structure
183 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
185 struct i40e_hw *hw = &adapter->hw;
187 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
190 rd32(hw, I40E_VFGEN_RSTAT);
192 synchronize_irq(adapter->msix_entries[0].vector);
196 * i40evf_misc_irq_enable - Enable default interrupt generation settings
197 * @adapter: board private structure
199 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
201 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);
208 rd32(hw, I40E_VFGEN_RSTAT);
212 * i40evf_irq_disable - Mask off interrupt generation on the NIC
213 * @adapter: board private structure
215 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
218 struct i40e_hw *hw = &adapter->hw;
220 if (!adapter->msix_entries)
223 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);
228 rd32(hw, I40E_VFGEN_RSTAT);
232 * i40evf_irq_enable_queues - Enable interrupt for specified queues
233 * @adapter: board private structure
234 * @mask: bitmap of queues to enable
236 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
238 struct i40e_hw *hw = &adapter->hw;
241 for (i = 1; i < adapter->num_msix_vectors; i++) {
242 if (mask & (1 << (i - 1))) {
243 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
244 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
245 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
246 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
252 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
253 * @adapter: board private structure
254 * @mask: bitmap of vectors to trigger
256 static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
258 struct i40e_hw *hw = &adapter->hw;
263 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
264 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
265 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
266 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
267 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
269 for (i = 1; i < adapter->num_msix_vectors; i++) {
270 if (mask & (1 << i)) {
271 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
272 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
273 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
274 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
275 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
281 * i40evf_irq_enable - Enable default interrupt generation settings
282 * @adapter: board private structure
284 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
286 struct i40e_hw *hw = &adapter->hw;
288 i40evf_misc_irq_enable(adapter);
289 i40evf_irq_enable_queues(adapter, ~0);
292 rd32(hw, I40E_VFGEN_RSTAT);
296 * i40evf_msix_aq - Interrupt handler for vector 0
297 * @irq: interrupt number
298 * @data: pointer to netdev
300 static irqreturn_t i40evf_msix_aq(int irq, void *data)
302 struct net_device *netdev = data;
303 struct i40evf_adapter *adapter = netdev_priv(netdev);
304 struct i40e_hw *hw = &adapter->hw;
308 /* handle non-queue interrupts */
309 val = rd32(hw, I40E_VFINT_ICR01);
310 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
313 val = rd32(hw, I40E_VFINT_DYN_CTL01);
314 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
315 wr32(hw, I40E_VFINT_DYN_CTL01, val);
317 /* schedule work on the private workqueue */
318 schedule_work(&adapter->adminq_task);
324 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
325 * @irq: interrupt number
326 * @data: pointer to a q_vector
328 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
330 struct i40e_q_vector *q_vector = data;
332 if (!q_vector->tx.ring && !q_vector->rx.ring)
335 napi_schedule(&q_vector->napi);
341 * i40evf_map_vector_to_rxq - associate irqs with rx queues
342 * @adapter: board private structure
343 * @v_idx: interrupt number
344 * @r_idx: queue number
347 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
349 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
350 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
352 rx_ring->q_vector = q_vector;
353 rx_ring->next = q_vector->rx.ring;
354 rx_ring->vsi = &adapter->vsi;
355 q_vector->rx.ring = rx_ring;
356 q_vector->rx.count++;
357 q_vector->rx.latency_range = I40E_LOW_LATENCY;
361 * i40evf_map_vector_to_txq - associate irqs with tx queues
362 * @adapter: board private structure
363 * @v_idx: interrupt number
364 * @t_idx: queue number
367 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
369 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
370 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
372 tx_ring->q_vector = q_vector;
373 tx_ring->next = q_vector->tx.ring;
374 tx_ring->vsi = &adapter->vsi;
375 q_vector->tx.ring = tx_ring;
376 q_vector->tx.count++;
377 q_vector->tx.latency_range = I40E_LOW_LATENCY;
378 q_vector->num_ringpairs++;
379 q_vector->ring_mask |= (1 << t_idx);
383 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
384 * @adapter: board private structure to initialize
386 * This function maps descriptor rings to the queue-specific vectors
387 * we were allotted through the MSI-X enabling code. Ideally, we'd have
388 * one vector per ring/queue, but on a constrained vector budget, we
389 * group the rings as "efficiently" as possible. You would add new
390 * mapping configurations in here.
392 static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
396 int rxr_idx = 0, txr_idx = 0;
397 int rxr_remaining = adapter->num_active_queues;
398 int txr_remaining = adapter->num_active_queues;
403 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
405 /* The ideal configuration...
406 * We have enough vectors to map one per queue.
408 if (q_vectors == (rxr_remaining * 2)) {
409 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
410 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
412 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
413 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
417 /* If we don't have enough vectors for a 1-to-1
418 * mapping, we'll have to group them so there are
419 * multiple queues per vector.
420 * Re-adjusting *qpv takes care of the remainder.
422 for (i = v_start; i < q_vectors; i++) {
423 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
424 for (j = 0; j < rqpv; j++) {
425 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
430 for (i = v_start; i < q_vectors; i++) {
431 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
432 for (j = 0; j < tqpv; j++) {
433 i40evf_map_vector_to_txq(adapter, i, txr_idx);
440 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
446 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
447 * @adapter: board private structure
449 * Allocates MSI-X vectors for tx and rx handling, and requests
450 * interrupts from the kernel.
453 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
455 int vector, err, q_vectors;
456 int rx_int_idx = 0, tx_int_idx = 0;
458 i40evf_irq_disable(adapter);
459 /* Decrement for Other and TCP Timer vectors */
460 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
462 for (vector = 0; vector < q_vectors; vector++) {
463 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
465 if (q_vector->tx.ring && q_vector->rx.ring) {
466 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
467 "i40evf-%s-%s-%d", basename,
468 "TxRx", rx_int_idx++);
470 } else if (q_vector->rx.ring) {
471 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
472 "i40evf-%s-%s-%d", basename,
474 } else if (q_vector->tx.ring) {
475 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
476 "i40evf-%s-%s-%d", basename,
479 /* skip this unused q_vector */
483 adapter->msix_entries[vector + NONQ_VECS].vector,
484 i40evf_msix_clean_rings,
489 dev_info(&adapter->pdev->dev,
490 "%s: request_irq failed, error: %d\n",
492 goto free_queue_irqs;
494 /* assign the mask for this irq */
495 irq_set_affinity_hint(
496 adapter->msix_entries[vector + NONQ_VECS].vector,
497 q_vector->affinity_mask);
505 irq_set_affinity_hint(
506 adapter->msix_entries[vector + NONQ_VECS].vector,
508 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
509 adapter->q_vector[vector]);
515 * i40evf_request_misc_irq - Initialize MSI-X interrupts
516 * @adapter: board private structure
518 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
519 * vector is only for the admin queue, and stays active even when the netdev
522 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
524 struct net_device *netdev = adapter->netdev;
527 snprintf(adapter->misc_vector_name,
528 sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
529 dev_name(&adapter->pdev->dev));
530 err = request_irq(adapter->msix_entries[0].vector,
532 adapter->misc_vector_name, netdev);
534 dev_err(&adapter->pdev->dev,
535 "request_irq for %s failed: %d\n",
536 adapter->misc_vector_name, err);
537 free_irq(adapter->msix_entries[0].vector, netdev);
543 * i40evf_free_traffic_irqs - Free MSI-X interrupts
544 * @adapter: board private structure
546 * Frees all MSI-X vectors other than 0.
548 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
553 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
555 for (i = 0; i < q_vectors; i++) {
556 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
558 free_irq(adapter->msix_entries[i+1].vector,
559 adapter->q_vector[i]);
564 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
565 * @adapter: board private structure
567 * Frees MSI-X vector 0.
569 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
571 struct net_device *netdev = adapter->netdev;
573 free_irq(adapter->msix_entries[0].vector, netdev);
577 * i40evf_configure_tx - Configure Transmit Unit after Reset
578 * @adapter: board private structure
580 * Configure the Tx unit of the MAC after a reset.
582 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
584 struct i40e_hw *hw = &adapter->hw;
587 for (i = 0; i < adapter->num_active_queues; i++)
588 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
592 * i40evf_configure_rx - Configure Receive Unit after Reset
593 * @adapter: board private structure
595 * Configure the Rx unit of the MAC after a reset.
597 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
599 struct i40e_hw *hw = &adapter->hw;
600 struct net_device *netdev = adapter->netdev;
601 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
606 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
607 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
609 /* Decide whether to use packet split mode or not */
610 if (netdev->mtu > ETH_DATA_LEN) {
611 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
612 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
614 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
616 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
617 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
619 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
622 /* Set the RX buffer length according to the mode */
623 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
624 rx_buf_len = I40E_RX_HDR_SIZE;
626 if (netdev->mtu <= ETH_DATA_LEN)
627 rx_buf_len = I40EVF_RXBUFFER_2048;
629 rx_buf_len = ALIGN(max_frame, 1024);
632 for (i = 0; i < adapter->num_active_queues; i++) {
633 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
634 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
639 * i40evf_find_vlan - Search filter list for specific vlan filter
640 * @adapter: board private structure
643 * Returns ptr to the filter object or NULL
646 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
648 struct i40evf_vlan_filter *f;
650 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
658 * i40evf_add_vlan - Add a vlan filter to the list
659 * @adapter: board private structure
662 * Returns ptr to the filter object or NULL when no memory available.
665 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
667 struct i40evf_vlan_filter *f = NULL;
670 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
671 &adapter->crit_section)) {
677 f = i40evf_find_vlan(adapter, vlan);
679 f = kzalloc(sizeof(*f), GFP_ATOMIC);
685 INIT_LIST_HEAD(&f->list);
686 list_add(&f->list, &adapter->vlan_filter_list);
688 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
692 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
698 * i40evf_del_vlan - Remove a vlan filter from the list
699 * @adapter: board private structure
702 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
704 struct i40evf_vlan_filter *f;
707 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
708 &adapter->crit_section)) {
714 f = i40evf_find_vlan(adapter, vlan);
717 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
719 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
723 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
724 * @netdev: network device struct
727 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
728 __always_unused __be16 proto, u16 vid)
730 struct i40evf_adapter *adapter = netdev_priv(netdev);
732 if (i40evf_add_vlan(adapter, vid) == NULL)
738 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
739 * @netdev: network device struct
742 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
743 __always_unused __be16 proto, u16 vid)
745 struct i40evf_adapter *adapter = netdev_priv(netdev);
747 i40evf_del_vlan(adapter, vid);
752 * i40evf_find_filter - Search filter list for specific mac filter
753 * @adapter: board private structure
754 * @macaddr: the MAC address
756 * Returns ptr to the filter object or NULL
759 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
762 struct i40evf_mac_filter *f;
767 list_for_each_entry(f, &adapter->mac_filter_list, list) {
768 if (ether_addr_equal(macaddr, f->macaddr))
775 * i40e_add_filter - Add a mac filter to the filter list
776 * @adapter: board private structure
777 * @macaddr: the MAC address
779 * Returns ptr to the filter object or NULL when no memory available.
782 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
785 struct i40evf_mac_filter *f;
791 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
792 &adapter->crit_section)) {
798 f = i40evf_find_filter(adapter, macaddr);
800 f = kzalloc(sizeof(*f), GFP_ATOMIC);
802 clear_bit(__I40EVF_IN_CRITICAL_TASK,
803 &adapter->crit_section);
807 ether_addr_copy(f->macaddr, macaddr);
809 list_add(&f->list, &adapter->mac_filter_list);
811 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
814 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
819 * i40evf_set_mac - NDO callback to set port mac address
820 * @netdev: network interface device structure
821 * @p: pointer to an address structure
823 * Returns 0 on success, negative on failure
825 static int i40evf_set_mac(struct net_device *netdev, void *p)
827 struct i40evf_adapter *adapter = netdev_priv(netdev);
828 struct i40e_hw *hw = &adapter->hw;
829 struct i40evf_mac_filter *f;
830 struct sockaddr *addr = p;
832 if (!is_valid_ether_addr(addr->sa_data))
833 return -EADDRNOTAVAIL;
835 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
838 f = i40evf_add_filter(adapter, addr->sa_data);
840 ether_addr_copy(hw->mac.addr, addr->sa_data);
841 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
844 return (f == NULL) ? -ENOMEM : 0;
848 * i40evf_set_rx_mode - NDO callback to set the netdev filters
849 * @netdev: network interface device structure
851 static void i40evf_set_rx_mode(struct net_device *netdev)
853 struct i40evf_adapter *adapter = netdev_priv(netdev);
854 struct i40evf_mac_filter *f, *ftmp;
855 struct netdev_hw_addr *uca;
856 struct netdev_hw_addr *mca;
859 /* add addr if not already in the filter list */
860 netdev_for_each_uc_addr(uca, netdev) {
861 i40evf_add_filter(adapter, uca->addr);
863 netdev_for_each_mc_addr(mca, netdev) {
864 i40evf_add_filter(adapter, mca->addr);
867 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
868 &adapter->crit_section)) {
871 dev_err(&adapter->pdev->dev,
872 "Failed to get lock in %s\n", __func__);
876 /* remove filter if not in netdev list */
877 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
880 if (is_multicast_ether_addr(f->macaddr)) {
881 netdev_for_each_mc_addr(mca, netdev) {
882 if (ether_addr_equal(mca->addr, f->macaddr)) {
888 netdev_for_each_uc_addr(uca, netdev) {
889 if (ether_addr_equal(uca->addr, f->macaddr)) {
897 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
900 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
904 * i40evf_napi_enable_all - enable NAPI on all queue vectors
905 * @adapter: board private structure
907 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
910 struct i40e_q_vector *q_vector;
911 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
913 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
914 struct napi_struct *napi;
916 q_vector = adapter->q_vector[q_idx];
917 napi = &q_vector->napi;
923 * i40evf_napi_disable_all - disable NAPI on all queue vectors
924 * @adapter: board private structure
926 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
929 struct i40e_q_vector *q_vector;
930 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
932 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
933 q_vector = adapter->q_vector[q_idx];
934 napi_disable(&q_vector->napi);
939 * i40evf_configure - set up transmit and receive data structures
940 * @adapter: board private structure
942 static void i40evf_configure(struct i40evf_adapter *adapter)
944 struct net_device *netdev = adapter->netdev;
947 i40evf_set_rx_mode(netdev);
949 i40evf_configure_tx(adapter);
950 i40evf_configure_rx(adapter);
951 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
953 for (i = 0; i < adapter->num_active_queues; i++) {
954 struct i40e_ring *ring = adapter->rx_rings[i];
956 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
957 ring->next_to_use = ring->count - 1;
958 writel(ring->next_to_use, ring->tail);
963 * i40evf_up_complete - Finish the last steps of bringing up a connection
964 * @adapter: board private structure
966 static int i40evf_up_complete(struct i40evf_adapter *adapter)
968 adapter->state = __I40EVF_RUNNING;
969 clear_bit(__I40E_DOWN, &adapter->vsi.state);
971 i40evf_napi_enable_all(adapter);
973 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
974 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
979 * i40e_down - Shutdown the connection processing
980 * @adapter: board private structure
982 void i40evf_down(struct i40evf_adapter *adapter)
984 struct net_device *netdev = adapter->netdev;
985 struct i40evf_mac_filter *f;
987 if (adapter->state == __I40EVF_DOWN)
990 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
991 &adapter->crit_section))
992 usleep_range(500, 1000);
994 netif_carrier_off(netdev);
995 netif_tx_disable(netdev);
996 i40evf_napi_disable_all(adapter);
997 i40evf_irq_disable(adapter);
999 /* remove all MAC filters */
1000 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1003 /* remove all VLAN filters */
1004 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1007 if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1008 adapter->state != __I40EVF_RESETTING) {
1009 /* cancel any current operation */
1010 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1011 /* Schedule operations to close down the HW. Don't wait
1012 * here for this to complete. The watchdog is still running
1013 * and it will take care of this.
1015 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1016 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1017 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1020 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1024 * i40evf_acquire_msix_vectors - Setup the MSIX capability
1025 * @adapter: board private structure
1026 * @vectors: number of vectors to request
1028 * Work with the OS to set up the MSIX vectors needed.
1030 * Returns 0 on success, negative on failure
1033 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1035 int err, vector_threshold;
1037 /* We'll want at least 3 (vector_threshold):
1038 * 0) Other (Admin Queue and link, mostly)
1042 vector_threshold = MIN_MSIX_COUNT;
1044 /* The more we get, the more we will assign to Tx/Rx Cleanup
1045 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1046 * Right now, we simply care about how many we'll get; we'll
1047 * set them up later while requesting irq's.
1049 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1050 vector_threshold, vectors);
1052 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1053 kfree(adapter->msix_entries);
1054 adapter->msix_entries = NULL;
1058 /* Adjust for only the vectors we'll use, which is minimum
1059 * of max_msix_q_vectors + NONQ_VECS, or the number of
1060 * vectors we were allocated.
1062 adapter->num_msix_vectors = err;
1067 * i40evf_free_queues - Free memory for all rings
1068 * @adapter: board private structure to initialize
1070 * Free all of the memory associated with queue pairs.
1072 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1076 if (!adapter->vsi_res)
1078 for (i = 0; i < adapter->num_active_queues; i++) {
1079 if (adapter->tx_rings[i])
1080 kfree_rcu(adapter->tx_rings[i], rcu);
1081 adapter->tx_rings[i] = NULL;
1082 adapter->rx_rings[i] = NULL;
1087 * i40evf_alloc_queues - Allocate memory for all rings
1088 * @adapter: board private structure to initialize
1090 * We allocate one ring per queue at run-time since we don't know the
1091 * number of queues at compile-time. The polling_netdev array is
1092 * intended for Multiqueue, but should work fine with a single queue.
1094 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1098 for (i = 0; i < adapter->num_active_queues; i++) {
1099 struct i40e_ring *tx_ring;
1100 struct i40e_ring *rx_ring;
1102 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
1106 tx_ring->queue_index = i;
1107 tx_ring->netdev = adapter->netdev;
1108 tx_ring->dev = &adapter->pdev->dev;
1109 tx_ring->count = adapter->tx_desc_count;
1110 adapter->tx_rings[i] = tx_ring;
1112 rx_ring = &tx_ring[1];
1113 rx_ring->queue_index = i;
1114 rx_ring->netdev = adapter->netdev;
1115 rx_ring->dev = &adapter->pdev->dev;
1116 rx_ring->count = adapter->rx_desc_count;
1117 adapter->rx_rings[i] = rx_ring;
1123 i40evf_free_queues(adapter);
1128 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1129 * @adapter: board private structure to initialize
1131 * Attempt to configure the interrupts using the best available
1132 * capabilities of the hardware and the kernel.
1134 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1136 int vector, v_budget;
1140 if (!adapter->vsi_res) {
1144 pairs = adapter->num_active_queues;
1146 /* It's easy to be greedy for MSI-X vectors, but it really
1147 * doesn't do us much good if we have a lot more vectors
1148 * than CPU's. So let's be conservative and only ask for
1149 * (roughly) twice the number of vectors as there are CPU's.
1151 v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1152 v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
1154 adapter->msix_entries = kcalloc(v_budget,
1155 sizeof(struct msix_entry), GFP_KERNEL);
1156 if (!adapter->msix_entries) {
1161 for (vector = 0; vector < v_budget; vector++)
1162 adapter->msix_entries[vector].entry = vector;
1164 i40evf_acquire_msix_vectors(adapter, v_budget);
1167 adapter->netdev->real_num_tx_queues = pairs;
1172 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1173 * @adapter: board private structure to initialize
1175 * We allocate one q_vector per queue interrupt. If allocation fails we
1178 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1180 int q_idx, num_q_vectors;
1181 struct i40e_q_vector *q_vector;
1183 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1185 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1186 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
1189 q_vector->adapter = adapter;
1190 q_vector->vsi = &adapter->vsi;
1191 q_vector->v_idx = q_idx;
1192 netif_napi_add(adapter->netdev, &q_vector->napi,
1193 i40evf_napi_poll, NAPI_POLL_WEIGHT);
1194 adapter->q_vector[q_idx] = q_vector;
1202 q_vector = adapter->q_vector[q_idx];
1203 netif_napi_del(&q_vector->napi);
1205 adapter->q_vector[q_idx] = NULL;
1211 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1212 * @adapter: board private structure to initialize
1214 * This function frees the memory allocated to the q_vectors. In addition if
1215 * NAPI is enabled it will delete any references to the NAPI struct prior
1216 * to freeing the q_vector.
1218 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1220 int q_idx, num_q_vectors;
1223 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1224 napi_vectors = adapter->num_active_queues;
1226 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1227 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1229 adapter->q_vector[q_idx] = NULL;
1230 if (q_idx < napi_vectors)
1231 netif_napi_del(&q_vector->napi);
1237 * i40evf_reset_interrupt_capability - Reset MSIX setup
1238 * @adapter: board private structure
1241 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1243 pci_disable_msix(adapter->pdev);
1244 kfree(adapter->msix_entries);
1245 adapter->msix_entries = NULL;
1249 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1250 * @adapter: board private structure to initialize
1253 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1257 err = i40evf_set_interrupt_capability(adapter);
1259 dev_err(&adapter->pdev->dev,
1260 "Unable to setup interrupt capabilities\n");
1261 goto err_set_interrupt;
1264 err = i40evf_alloc_q_vectors(adapter);
1266 dev_err(&adapter->pdev->dev,
1267 "Unable to allocate memory for queue vectors\n");
1268 goto err_alloc_q_vectors;
1271 err = i40evf_alloc_queues(adapter);
1273 dev_err(&adapter->pdev->dev,
1274 "Unable to allocate memory for queues\n");
1275 goto err_alloc_queues;
1278 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1279 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1280 adapter->num_active_queues);
1284 i40evf_free_q_vectors(adapter);
1285 err_alloc_q_vectors:
1286 i40evf_reset_interrupt_capability(adapter);
1292 * i40evf_watchdog_timer - Periodic call-back timer
1293 * @data: pointer to adapter disguised as unsigned long
1295 static void i40evf_watchdog_timer(unsigned long data)
1297 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1299 schedule_work(&adapter->watchdog_task);
1300 /* timer will be rescheduled in watchdog task */
1304 * i40evf_watchdog_task - Periodic call-back task
1305 * @work: pointer to work_struct
1307 static void i40evf_watchdog_task(struct work_struct *work)
1309 struct i40evf_adapter *adapter = container_of(work,
1310 struct i40evf_adapter,
1312 struct i40e_hw *hw = &adapter->hw;
1315 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1316 goto restart_watchdog;
1318 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1319 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1320 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1321 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1322 (rstat_val == I40E_VFR_COMPLETED)) {
1323 /* A chance for redemption! */
1324 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1325 adapter->state = __I40EVF_STARTUP;
1326 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1327 schedule_delayed_work(&adapter->init_task, 10);
1328 clear_bit(__I40EVF_IN_CRITICAL_TASK,
1329 &adapter->crit_section);
1330 /* Don't reschedule the watchdog, since we've restarted
1331 * the init task. When init_task contacts the PF and
1332 * gets everything set up again, it'll restart the
1333 * watchdog for us. Down, boy. Sit. Stay. Woof.
1337 adapter->aq_required = 0;
1338 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1342 if ((adapter->state < __I40EVF_DOWN) ||
1343 (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1346 /* check for reset */
1347 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1348 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1349 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
1350 (rstat_val != I40E_VFR_VFACTIVE) &&
1351 (rstat_val != I40E_VFR_COMPLETED)) {
1352 adapter->state = __I40EVF_RESETTING;
1353 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1354 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1355 schedule_work(&adapter->reset_task);
1356 adapter->aq_required = 0;
1357 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1361 /* Process admin queue tasks. After init, everything gets done
1362 * here so we don't race on the admin queue.
1364 if (adapter->current_op) {
1365 if (!i40evf_asq_done(hw)) {
1366 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1367 i40evf_send_api_ver(adapter);
1372 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1373 i40evf_disable_queues(adapter);
1377 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1378 i40evf_map_queues(adapter);
1382 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1383 i40evf_add_ether_addrs(adapter);
1387 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1388 i40evf_add_vlans(adapter);
1392 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1393 i40evf_del_ether_addrs(adapter);
1397 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1398 i40evf_del_vlans(adapter);
1402 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1403 i40evf_configure_queues(adapter);
1407 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1408 i40evf_enable_queues(adapter);
1412 if (adapter->state == __I40EVF_RUNNING)
1413 i40evf_request_stats(adapter);
1415 if (adapter->state == __I40EVF_RUNNING) {
1416 i40evf_irq_enable_queues(adapter, ~0);
1417 i40evf_fire_sw_int(adapter, 0xFF);
1419 i40evf_fire_sw_int(adapter, 0x1);
1422 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1424 if (adapter->state == __I40EVF_REMOVE)
1426 if (adapter->aq_required)
1427 mod_timer(&adapter->watchdog_timer,
1428 jiffies + msecs_to_jiffies(20));
1430 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1431 schedule_work(&adapter->adminq_task);
1435 * i40evf_configure_rss - Prepare for RSS
1436 * @adapter: board private structure
1438 static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1440 u32 rss_key[I40E_VFQF_HKEY_MAX_INDEX + 1];
1441 struct i40e_hw *hw = &adapter->hw;
1447 /* Hash type is configured by the PF - we just supply the key */
1448 netdev_rss_key_fill(rss_key, sizeof(rss_key));
1450 /* Fill out hash function seed */
1451 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1452 wr32(hw, I40E_VFQF_HKEY(i), rss_key[i]);
1454 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1455 hena = I40E_DEFAULT_RSS_HENA;
1456 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1457 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1459 /* Populate the LUT with max no. of queues in round robin fashion */
1460 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1462 for (j = 0; j < 4; j++) {
1463 if (cqueue == adapter->vsi_res->num_queue_pairs)
1465 lut |= ((cqueue) << (8 * j));
1468 wr32(hw, I40E_VFQF_HLUT(i), lut);
1473 #define I40EVF_RESET_WAIT_MS 100
1474 #define I40EVF_RESET_WAIT_COUNT 200
1476 * i40evf_reset_task - Call-back task to handle hardware reset
1477 * @work: pointer to work_struct
1479 * During reset we need to shut down and reinitialize the admin queue
1480 * before we can use it to communicate with the PF again. We also clear
1481 * and reinit the rings because that context is lost as well.
1483 static void i40evf_reset_task(struct work_struct *work)
1485 struct i40evf_adapter *adapter = container_of(work,
1486 struct i40evf_adapter,
1488 struct net_device *netdev = adapter->netdev;
1489 struct i40e_hw *hw = &adapter->hw;
1490 struct i40evf_mac_filter *f;
1494 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1495 &adapter->crit_section))
1496 usleep_range(500, 1000);
1498 if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1499 dev_info(&adapter->pdev->dev, "Requesting reset from PF\n");
1500 i40evf_request_reset(adapter);
1503 /* poll until we see the reset actually happen */
1504 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1505 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1506 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1507 if ((rstat_val != I40E_VFR_VFACTIVE) &&
1508 (rstat_val != I40E_VFR_COMPLETED))
1510 msleep(I40EVF_RESET_WAIT_MS);
1512 if (i == I40EVF_RESET_WAIT_COUNT) {
1513 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1514 goto continue_reset; /* act like the reset happened */
1517 /* wait until the reset is complete and the PF is responding to us */
1518 for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1519 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1520 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1521 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1522 (rstat_val == I40E_VFR_COMPLETED))
1524 msleep(I40EVF_RESET_WAIT_MS);
1526 if (i == I40EVF_RESET_WAIT_COUNT) {
1527 struct i40evf_mac_filter *f, *ftmp;
1528 struct i40evf_vlan_filter *fv, *fvtmp;
1530 /* reset never finished */
1531 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1533 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1535 if (netif_running(adapter->netdev)) {
1536 set_bit(__I40E_DOWN, &adapter->vsi.state);
1537 i40evf_irq_disable(adapter);
1538 i40evf_napi_disable_all(adapter);
1539 netif_tx_disable(netdev);
1540 netif_tx_stop_all_queues(netdev);
1541 netif_carrier_off(netdev);
1542 i40evf_free_traffic_irqs(adapter);
1543 i40evf_free_all_tx_resources(adapter);
1544 i40evf_free_all_rx_resources(adapter);
1547 /* Delete all of the filters, both MAC and VLAN. */
1548 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1553 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1555 list_del(&fv->list);
1559 i40evf_free_misc_irq(adapter);
1560 i40evf_reset_interrupt_capability(adapter);
1561 i40evf_free_queues(adapter);
1562 i40evf_free_q_vectors(adapter);
1563 kfree(adapter->vf_res);
1564 i40evf_shutdown_adminq(hw);
1565 adapter->netdev->flags &= ~IFF_UP;
1566 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1567 return; /* Do not attempt to reinit. It's dead, Jim. */
1571 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1573 i40evf_irq_disable(adapter);
1575 if (netif_running(adapter->netdev)) {
1576 i40evf_napi_disable_all(adapter);
1577 netif_tx_disable(netdev);
1578 netif_tx_stop_all_queues(netdev);
1579 netif_carrier_off(netdev);
1582 adapter->state = __I40EVF_RESETTING;
1584 /* kill and reinit the admin queue */
1585 if (i40evf_shutdown_adminq(hw))
1586 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1587 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1588 err = i40evf_init_adminq(hw);
1590 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1593 i40evf_map_queues(adapter);
1595 /* re-add all MAC filters */
1596 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1599 /* re-add all VLAN filters */
1600 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1603 adapter->aq_required = I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1604 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1605 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1607 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1609 if (netif_running(adapter->netdev)) {
1610 /* allocate transmit descriptors */
1611 err = i40evf_setup_all_tx_resources(adapter);
1615 /* allocate receive descriptors */
1616 err = i40evf_setup_all_rx_resources(adapter);
1620 i40evf_configure(adapter);
1622 err = i40evf_up_complete(adapter);
1626 i40evf_irq_enable(adapter, true);
1630 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1631 i40evf_close(adapter->netdev);
1635 * i40evf_adminq_task - worker thread to clean the admin queue
1636 * @work: pointer to work_struct containing our data
1638 static void i40evf_adminq_task(struct work_struct *work)
1640 struct i40evf_adapter *adapter =
1641 container_of(work, struct i40evf_adapter, adminq_task);
1642 struct i40e_hw *hw = &adapter->hw;
1643 struct i40e_arq_event_info event;
1644 struct i40e_virtchnl_msg *v_msg;
1649 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1652 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1653 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1657 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1659 ret = i40evf_clean_arq_element(hw, &event, &pending);
1660 if (ret || !v_msg->v_opcode)
1661 break; /* No event to process or error cleaning ARQ */
1663 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1664 v_msg->v_retval, event.msg_buf,
1667 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1670 /* check for error indications */
1671 val = rd32(hw, hw->aq.arq.len);
1673 if (val & I40E_VF_ARQLEN_ARQVFE_MASK) {
1674 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1675 val &= ~I40E_VF_ARQLEN_ARQVFE_MASK;
1677 if (val & I40E_VF_ARQLEN_ARQOVFL_MASK) {
1678 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1679 val &= ~I40E_VF_ARQLEN_ARQOVFL_MASK;
1681 if (val & I40E_VF_ARQLEN_ARQCRIT_MASK) {
1682 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1683 val &= ~I40E_VF_ARQLEN_ARQCRIT_MASK;
1686 wr32(hw, hw->aq.arq.len, val);
1688 val = rd32(hw, hw->aq.asq.len);
1690 if (val & I40E_VF_ATQLEN_ATQVFE_MASK) {
1691 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1692 val &= ~I40E_VF_ATQLEN_ATQVFE_MASK;
1694 if (val & I40E_VF_ATQLEN_ATQOVFL_MASK) {
1695 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1696 val &= ~I40E_VF_ATQLEN_ATQOVFL_MASK;
1698 if (val & I40E_VF_ATQLEN_ATQCRIT_MASK) {
1699 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1700 val &= ~I40E_VF_ATQLEN_ATQCRIT_MASK;
1703 wr32(hw, hw->aq.asq.len, val);
1705 kfree(event.msg_buf);
1707 /* re-enable Admin queue interrupt cause */
1708 i40evf_misc_irq_enable(adapter);
1712 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1713 * @adapter: board private structure
1715 * Free all transmit software resources
1717 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1721 for (i = 0; i < adapter->num_active_queues; i++)
1722 if (adapter->tx_rings[i]->desc)
1723 i40evf_free_tx_resources(adapter->tx_rings[i]);
1727 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1728 * @adapter: board private structure
1730 * If this function returns with an error, then it's possible one or
1731 * more of the rings is populated (while the rest are not). It is the
1732 * callers duty to clean those orphaned rings.
1734 * Return 0 on success, negative on failure
1736 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1740 for (i = 0; i < adapter->num_active_queues; i++) {
1741 adapter->tx_rings[i]->count = adapter->tx_desc_count;
1742 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1745 dev_err(&adapter->pdev->dev,
1746 "%s: Allocation for Tx Queue %u failed\n",
1755 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1756 * @adapter: board private structure
1758 * If this function returns with an error, then it's possible one or
1759 * more of the rings is populated (while the rest are not). It is the
1760 * callers duty to clean those orphaned rings.
1762 * Return 0 on success, negative on failure
1764 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1768 for (i = 0; i < adapter->num_active_queues; i++) {
1769 adapter->rx_rings[i]->count = adapter->rx_desc_count;
1770 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1773 dev_err(&adapter->pdev->dev,
1774 "%s: Allocation for Rx Queue %u failed\n",
1782 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1783 * @adapter: board private structure
1785 * Free all receive software resources
1787 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1791 for (i = 0; i < adapter->num_active_queues; i++)
1792 if (adapter->rx_rings[i]->desc)
1793 i40evf_free_rx_resources(adapter->rx_rings[i]);
1797 * i40evf_open - Called when a network interface is made active
1798 * @netdev: network interface device structure
1800 * Returns 0 on success, negative value on failure
1802 * The open entry point is called when a network interface is made
1803 * active by the system (IFF_UP). At this point all resources needed
1804 * for transmit and receive operations are allocated, the interrupt
1805 * handler is registered with the OS, the watchdog timer is started,
1806 * and the stack is notified that the interface is ready.
1808 static int i40evf_open(struct net_device *netdev)
1810 struct i40evf_adapter *adapter = netdev_priv(netdev);
1813 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1814 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1817 if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
1820 /* allocate transmit descriptors */
1821 err = i40evf_setup_all_tx_resources(adapter);
1825 /* allocate receive descriptors */
1826 err = i40evf_setup_all_rx_resources(adapter);
1830 /* clear any pending interrupts, may auto mask */
1831 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1835 i40evf_configure(adapter);
1837 err = i40evf_up_complete(adapter);
1841 i40evf_irq_enable(adapter, true);
1846 i40evf_down(adapter);
1847 i40evf_free_traffic_irqs(adapter);
1849 i40evf_free_all_rx_resources(adapter);
1851 i40evf_free_all_tx_resources(adapter);
1857 * i40evf_close - Disables a network interface
1858 * @netdev: network interface device structure
1860 * Returns 0, this is not allowed to fail
1862 * The close entry point is called when an interface is de-activated
1863 * by the OS. The hardware is still under the drivers control, but
1864 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1865 * are freed, along with all transmit and receive resources.
1867 static int i40evf_close(struct net_device *netdev)
1869 struct i40evf_adapter *adapter = netdev_priv(netdev);
1871 if (adapter->state <= __I40EVF_DOWN)
1875 set_bit(__I40E_DOWN, &adapter->vsi.state);
1877 i40evf_down(adapter);
1878 adapter->state = __I40EVF_DOWN;
1879 i40evf_free_traffic_irqs(adapter);
1885 * i40evf_get_stats - Get System Network Statistics
1886 * @netdev: network interface device structure
1888 * Returns the address of the device statistics structure.
1889 * The statistics are actually updated from the timer callback.
1891 static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1893 struct i40evf_adapter *adapter = netdev_priv(netdev);
1895 /* only return the current stats */
1896 return &adapter->net_stats;
1900 * i40evf_reinit_locked - Software reinit
1901 * @adapter: board private structure
1903 * Reinititalizes the ring structures in response to a software configuration
1904 * change. Roughly the same as close followed by open, but skips releasing
1905 * and reallocating the interrupts.
1907 void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1909 struct net_device *netdev = adapter->netdev;
1912 WARN_ON(in_interrupt());
1914 i40evf_down(adapter);
1916 /* allocate transmit descriptors */
1917 err = i40evf_setup_all_tx_resources(adapter);
1921 /* allocate receive descriptors */
1922 err = i40evf_setup_all_rx_resources(adapter);
1926 i40evf_configure(adapter);
1928 err = i40evf_up_complete(adapter);
1932 i40evf_irq_enable(adapter, true);
1936 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1937 i40evf_close(netdev);
1941 * i40evf_change_mtu - Change the Maximum Transfer Unit
1942 * @netdev: network interface device structure
1943 * @new_mtu: new value for maximum frame size
1945 * Returns 0 on success, negative on failure
1947 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1949 struct i40evf_adapter *adapter = netdev_priv(netdev);
1950 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1952 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1955 /* must set new MTU before calling down or up */
1956 netdev->mtu = new_mtu;
1957 i40evf_reinit_locked(adapter);
1961 static const struct net_device_ops i40evf_netdev_ops = {
1962 .ndo_open = i40evf_open,
1963 .ndo_stop = i40evf_close,
1964 .ndo_start_xmit = i40evf_xmit_frame,
1965 .ndo_get_stats = i40evf_get_stats,
1966 .ndo_set_rx_mode = i40evf_set_rx_mode,
1967 .ndo_validate_addr = eth_validate_addr,
1968 .ndo_set_mac_address = i40evf_set_mac,
1969 .ndo_change_mtu = i40evf_change_mtu,
1970 .ndo_tx_timeout = i40evf_tx_timeout,
1971 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1972 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1976 * i40evf_check_reset_complete - check that VF reset is complete
1977 * @hw: pointer to hw struct
1979 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1981 static int i40evf_check_reset_complete(struct i40e_hw *hw)
1986 for (i = 0; i < 100; i++) {
1987 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
1988 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1989 if ((rstat == I40E_VFR_VFACTIVE) ||
1990 (rstat == I40E_VFR_COMPLETED))
1992 usleep_range(10, 20);
1998 * i40evf_init_task - worker thread to perform delayed initialization
1999 * @work: pointer to work_struct containing our data
2001 * This task completes the work that was begun in probe. Due to the nature
2002 * of VF-PF communications, we may need to wait tens of milliseconds to get
2003 * responses back from the PF. Rather than busy-wait in probe and bog down the
2004 * whole system, we'll do it in a task so we can sleep.
2005 * This task only runs during driver init. Once we've established
2006 * communications with the PF driver and set up our netdev, the watchdog
2009 static void i40evf_init_task(struct work_struct *work)
2011 struct i40evf_adapter *adapter = container_of(work,
2012 struct i40evf_adapter,
2014 struct net_device *netdev = adapter->netdev;
2015 struct i40evf_mac_filter *f;
2016 struct i40e_hw *hw = &adapter->hw;
2017 struct pci_dev *pdev = adapter->pdev;
2020 switch (adapter->state) {
2021 case __I40EVF_STARTUP:
2022 /* driver loaded, probe complete */
2023 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2024 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2025 err = i40e_set_mac_type(hw);
2027 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2031 err = i40evf_check_reset_complete(hw);
2033 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2037 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2038 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2039 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2040 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2042 err = i40evf_init_adminq(hw);
2044 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2048 err = i40evf_send_api_ver(adapter);
2050 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2051 i40evf_shutdown_adminq(hw);
2054 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2056 case __I40EVF_INIT_VERSION_CHECK:
2057 if (!i40evf_asq_done(hw)) {
2058 dev_err(&pdev->dev, "Admin queue command never completed\n");
2059 i40evf_shutdown_adminq(hw);
2060 adapter->state = __I40EVF_STARTUP;
2064 /* aq msg sent, awaiting reply */
2065 err = i40evf_verify_api_ver(adapter);
2067 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2068 err = i40evf_send_api_ver(adapter);
2071 err = i40evf_send_vf_config_msg(adapter);
2073 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2077 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2079 case __I40EVF_INIT_GET_RESOURCES:
2080 /* aq msg sent, awaiting reply */
2081 if (!adapter->vf_res) {
2082 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2084 sizeof(struct i40e_virtchnl_vsi_resource));
2085 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2086 if (!adapter->vf_res)
2089 err = i40evf_get_vf_config(adapter);
2090 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2091 err = i40evf_send_vf_config_msg(adapter);
2095 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2099 adapter->state = __I40EVF_INIT_SW;
2104 /* got VF config message back from PF, now we can parse it */
2105 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2106 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2107 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2109 if (!adapter->vsi_res) {
2110 dev_err(&pdev->dev, "No LAN VSI found\n");
2114 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2116 netdev->netdev_ops = &i40evf_netdev_ops;
2117 i40evf_set_ethtool_ops(netdev);
2118 netdev->watchdog_timeo = 5 * HZ;
2119 netdev->features |= NETIF_F_HIGHDMA |
2129 if (adapter->vf_res->vf_offload_flags
2130 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2131 netdev->vlan_features = netdev->features;
2132 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2133 NETIF_F_HW_VLAN_CTAG_RX |
2134 NETIF_F_HW_VLAN_CTAG_FILTER;
2137 /* copy netdev features into list of user selectable features */
2138 netdev->hw_features |= netdev->features;
2139 netdev->hw_features &= ~NETIF_F_RXCSUM;
2141 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2142 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2143 adapter->hw.mac.addr);
2144 random_ether_addr(adapter->hw.mac.addr);
2146 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2147 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2149 f = kzalloc(sizeof(*f), GFP_ATOMIC);
2153 ether_addr_copy(f->macaddr, adapter->hw.mac.addr);
2155 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
2157 list_add(&f->list, &adapter->mac_filter_list);
2159 init_timer(&adapter->watchdog_timer);
2160 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2161 adapter->watchdog_timer.data = (unsigned long)adapter;
2162 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2164 adapter->num_active_queues = min_t(int,
2165 adapter->vsi_res->num_queue_pairs,
2166 (int)(num_online_cpus()));
2167 adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2168 adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2169 err = i40evf_init_interrupt_scheme(adapter);
2172 i40evf_map_rings_to_vectors(adapter);
2173 i40evf_configure_rss(adapter);
2174 err = i40evf_request_misc_irq(adapter);
2178 netif_carrier_off(netdev);
2180 adapter->vsi.id = adapter->vsi_res->vsi_id;
2181 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2182 adapter->vsi.back = adapter;
2183 adapter->vsi.base_vector = 1;
2184 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2185 adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2186 ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2187 adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2188 ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2189 adapter->vsi.netdev = adapter->netdev;
2191 if (!adapter->netdev_registered) {
2192 err = register_netdev(netdev);
2197 adapter->netdev_registered = true;
2199 netif_tx_stop_all_queues(netdev);
2201 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2202 if (netdev->features & NETIF_F_GRO)
2203 dev_info(&pdev->dev, "GRO is enabled\n");
2205 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2206 adapter->state = __I40EVF_DOWN;
2207 set_bit(__I40E_DOWN, &adapter->vsi.state);
2208 i40evf_misc_irq_enable(adapter);
2211 schedule_delayed_work(&adapter->init_task,
2212 msecs_to_jiffies(50));
2216 i40evf_free_misc_irq(adapter);
2218 i40evf_reset_interrupt_capability(adapter);
2220 kfree(adapter->vf_res);
2221 adapter->vf_res = NULL;
2223 /* Things went into the weeds, so try again later */
2224 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2225 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
2226 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2227 return; /* do not reschedule */
2229 schedule_delayed_work(&adapter->init_task, HZ * 3);
2233 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2234 * @pdev: pci device structure
2236 static void i40evf_shutdown(struct pci_dev *pdev)
2238 struct net_device *netdev = pci_get_drvdata(pdev);
2239 struct i40evf_adapter *adapter = netdev_priv(netdev);
2241 netif_device_detach(netdev);
2243 if (netif_running(netdev))
2244 i40evf_close(netdev);
2246 /* Prevent the watchdog from running. */
2247 adapter->state = __I40EVF_REMOVE;
2248 adapter->aq_required = 0;
2251 pci_save_state(pdev);
2254 pci_disable_device(pdev);
2258 * i40evf_probe - Device Initialization Routine
2259 * @pdev: PCI device information struct
2260 * @ent: entry in i40evf_pci_tbl
2262 * Returns 0 on success, negative on failure
2264 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2265 * The OS initialization, configuring of the adapter private structure,
2266 * and a hardware reset occur.
2268 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2270 struct net_device *netdev;
2271 struct i40evf_adapter *adapter = NULL;
2272 struct i40e_hw *hw = NULL;
2275 err = pci_enable_device(pdev);
2279 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2281 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2284 "DMA configuration failed: 0x%x\n", err);
2289 err = pci_request_regions(pdev, i40evf_driver_name);
2292 "pci_request_regions failed 0x%x\n", err);
2296 pci_enable_pcie_error_reporting(pdev);
2298 pci_set_master(pdev);
2300 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2304 goto err_alloc_etherdev;
2307 SET_NETDEV_DEV(netdev, &pdev->dev);
2309 pci_set_drvdata(pdev, netdev);
2310 adapter = netdev_priv(netdev);
2312 adapter->netdev = netdev;
2313 adapter->pdev = pdev;
2318 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2319 adapter->state = __I40EVF_STARTUP;
2321 /* Call save state here because it relies on the adapter struct. */
2322 pci_save_state(pdev);
2324 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2325 pci_resource_len(pdev, 0));
2330 hw->vendor_id = pdev->vendor;
2331 hw->device_id = pdev->device;
2332 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2333 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2334 hw->subsystem_device_id = pdev->subsystem_device;
2335 hw->bus.device = PCI_SLOT(pdev->devfn);
2336 hw->bus.func = PCI_FUNC(pdev->devfn);
2338 INIT_LIST_HEAD(&adapter->mac_filter_list);
2339 INIT_LIST_HEAD(&adapter->vlan_filter_list);
2341 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2342 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2343 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2344 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2345 schedule_delayed_work(&adapter->init_task, 10);
2350 free_netdev(netdev);
2352 pci_release_regions(pdev);
2355 pci_disable_device(pdev);
2361 * i40evf_suspend - Power management suspend routine
2362 * @pdev: PCI device information struct
2365 * Called when the system (VM) is entering sleep/suspend.
2367 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2369 struct net_device *netdev = pci_get_drvdata(pdev);
2370 struct i40evf_adapter *adapter = netdev_priv(netdev);
2373 netif_device_detach(netdev);
2375 if (netif_running(netdev)) {
2377 i40evf_down(adapter);
2380 i40evf_free_misc_irq(adapter);
2381 i40evf_reset_interrupt_capability(adapter);
2383 retval = pci_save_state(pdev);
2387 pci_disable_device(pdev);
2393 * i40evf_resume - Power management resume routine
2394 * @pdev: PCI device information struct
2396 * Called when the system (VM) is resumed from sleep/suspend.
2398 static int i40evf_resume(struct pci_dev *pdev)
2400 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2401 struct net_device *netdev = adapter->netdev;
2404 pci_set_power_state(pdev, PCI_D0);
2405 pci_restore_state(pdev);
2406 /* pci_restore_state clears dev->state_saved so call
2407 * pci_save_state to restore it.
2409 pci_save_state(pdev);
2411 err = pci_enable_device_mem(pdev);
2413 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2416 pci_set_master(pdev);
2419 err = i40evf_set_interrupt_capability(adapter);
2421 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2424 err = i40evf_request_misc_irq(adapter);
2427 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2431 schedule_work(&adapter->reset_task);
2433 netif_device_attach(netdev);
2438 #endif /* CONFIG_PM */
2440 * i40evf_remove - Device Removal Routine
2441 * @pdev: PCI device information struct
2443 * i40evf_remove is called by the PCI subsystem to alert the driver
2444 * that it should release a PCI device. The could be caused by a
2445 * Hot-Plug event, or because the driver is going to be removed from
2448 static void i40evf_remove(struct pci_dev *pdev)
2450 struct net_device *netdev = pci_get_drvdata(pdev);
2451 struct i40evf_adapter *adapter = netdev_priv(netdev);
2452 struct i40evf_mac_filter *f, *ftmp;
2453 struct i40e_hw *hw = &adapter->hw;
2455 cancel_delayed_work_sync(&adapter->init_task);
2456 cancel_work_sync(&adapter->reset_task);
2458 if (adapter->netdev_registered) {
2459 unregister_netdev(netdev);
2460 adapter->netdev_registered = false;
2463 /* Shut down all the garbage mashers on the detention level */
2464 adapter->state = __I40EVF_REMOVE;
2465 adapter->aq_required = 0;
2466 i40evf_request_reset(adapter);
2468 /* If the FW isn't responding, kick it once, but only once. */
2469 if (!i40evf_asq_done(hw)) {
2470 i40evf_request_reset(adapter);
2474 if (adapter->msix_entries) {
2475 i40evf_misc_irq_disable(adapter);
2476 i40evf_free_misc_irq(adapter);
2477 i40evf_reset_interrupt_capability(adapter);
2478 i40evf_free_q_vectors(adapter);
2481 if (adapter->watchdog_timer.function)
2482 del_timer_sync(&adapter->watchdog_timer);
2484 flush_scheduled_work();
2486 if (hw->aq.asq.count)
2487 i40evf_shutdown_adminq(hw);
2489 iounmap(hw->hw_addr);
2490 pci_release_regions(pdev);
2492 i40evf_free_all_tx_resources(adapter);
2493 i40evf_free_all_rx_resources(adapter);
2494 i40evf_free_queues(adapter);
2495 kfree(adapter->vf_res);
2496 /* If we got removed before an up/down sequence, we've got a filter
2497 * hanging out there that we need to get rid of.
2499 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2503 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2508 free_netdev(netdev);
2510 pci_disable_pcie_error_reporting(pdev);
2512 pci_disable_device(pdev);
2515 static struct pci_driver i40evf_driver = {
2516 .name = i40evf_driver_name,
2517 .id_table = i40evf_pci_tbl,
2518 .probe = i40evf_probe,
2519 .remove = i40evf_remove,
2521 .suspend = i40evf_suspend,
2522 .resume = i40evf_resume,
2524 .shutdown = i40evf_shutdown,
2528 * i40e_init_module - Driver Registration Routine
2530 * i40e_init_module is the first routine called when the driver is
2531 * loaded. All it does is register with the PCI subsystem.
2533 static int __init i40evf_init_module(void)
2537 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2538 i40evf_driver_version);
2540 pr_info("%s\n", i40evf_copyright);
2542 ret = pci_register_driver(&i40evf_driver);
2546 module_init(i40evf_init_module);
2549 * i40e_exit_module - Driver Exit Cleanup Routine
2551 * i40e_exit_module is called just before the driver is removed
2554 static void __exit i40evf_exit_module(void)
2556 pci_unregister_driver(&i40evf_driver);
2559 module_exit(i40evf_exit_module);