1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4 * Copyright(c) 2013 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 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Contact Information:
19 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
20 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
22 ******************************************************************************/
25 #include "i40e_prototype.h"
26 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
27 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
28 static int i40evf_close(struct net_device *netdev);
30 char i40evf_driver_name[] = "i40evf";
31 static const char i40evf_driver_string[] =
32 "Intel(R) XL710 X710 Virtual Function Network Driver";
34 #define DRV_VERSION "0.9.11"
35 const char i40evf_driver_version[] = DRV_VERSION;
36 static const char i40evf_copyright[] =
37 "Copyright (c) 2013 Intel Corporation.";
39 /* i40evf_pci_tbl - PCI Device ID Table
41 * Wildcard entries (PCI_ANY_ID) should come last
42 * Last entry must be all 0s
44 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
45 * Class, Class Mask, private data (not used) }
47 static DEFINE_PCI_DEVICE_TABLE(i40evf_pci_tbl) = {
48 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
49 /* required last entry */
53 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
55 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
56 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(DRV_VERSION);
61 * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
62 * @hw: pointer to the HW structure
63 * @mem: ptr to mem struct to fill out
64 * @size: size of memory requested
65 * @alignment: what to align the allocation to
67 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
68 struct i40e_dma_mem *mem,
69 u64 size, u32 alignment)
71 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
74 return I40E_ERR_PARAM;
76 mem->size = ALIGN(size, alignment);
77 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
78 (dma_addr_t *)&mem->pa, GFP_KERNEL);
82 return I40E_ERR_NO_MEMORY;
86 * i40evf_free_dma_mem_d - OS specific memory free for shared code
87 * @hw: pointer to the HW structure
88 * @mem: ptr to mem struct to free
90 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
92 struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
95 return I40E_ERR_PARAM;
96 dma_free_coherent(&adapter->pdev->dev, mem->size,
97 mem->va, (dma_addr_t)mem->pa);
102 * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
103 * @hw: pointer to the HW structure
104 * @mem: ptr to mem struct to fill out
105 * @size: size of memory requested
107 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
108 struct i40e_virt_mem *mem, u32 size)
111 return I40E_ERR_PARAM;
114 mem->va = kzalloc(size, GFP_KERNEL);
119 return I40E_ERR_NO_MEMORY;
123 * i40evf_free_virt_mem_d - OS specific memory free for shared code
124 * @hw: pointer to the HW structure
125 * @mem: ptr to mem struct to free
127 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
128 struct i40e_virt_mem *mem)
131 return I40E_ERR_PARAM;
133 /* it's ok to kfree a NULL pointer */
140 * i40evf_debug_d - OS dependent version of debug printing
141 * @hw: pointer to the HW structure
142 * @mask: debug level mask
143 * @fmt_str: printf-type format description
145 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
150 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
153 va_start(argptr, fmt_str);
154 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
157 /* the debug string is already formatted with a newline */
162 * i40evf_tx_timeout - Respond to a Tx Hang
163 * @netdev: network interface device structure
165 static void i40evf_tx_timeout(struct net_device *netdev)
167 struct i40evf_adapter *adapter = netdev_priv(netdev);
169 adapter->tx_timeout_count++;
171 /* Do the reset outside of interrupt context */
172 schedule_work(&adapter->reset_task);
176 * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
177 * @adapter: board private structure
179 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
181 struct i40e_hw *hw = &adapter->hw;
182 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
185 rd32(hw, I40E_VFGEN_RSTAT);
187 synchronize_irq(adapter->msix_entries[0].vector);
191 * i40evf_misc_irq_enable - Enable default interrupt generation settings
192 * @adapter: board private structure
194 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
196 struct i40e_hw *hw = &adapter->hw;
197 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
198 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
199 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA_ADMINQ_MASK);
202 rd32(hw, I40E_VFGEN_RSTAT);
206 * i40evf_irq_disable - Mask off interrupt generation on the NIC
207 * @adapter: board private structure
209 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
212 struct i40e_hw *hw = &adapter->hw;
214 for (i = 1; i < adapter->num_msix_vectors; i++) {
215 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
216 synchronize_irq(adapter->msix_entries[i].vector);
219 rd32(hw, I40E_VFGEN_RSTAT);
224 * i40evf_irq_enable_queues - Enable interrupt for specified queues
225 * @adapter: board private structure
226 * @mask: bitmap of queues to enable
228 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
230 struct i40e_hw *hw = &adapter->hw;
233 for (i = 1; i < adapter->num_msix_vectors; i++) {
234 if (mask & (1 << (i - 1))) {
235 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
236 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
237 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
243 * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
244 * @adapter: board private structure
245 * @mask: bitmap of vectors to trigger
247 static void i40evf_fire_sw_int(struct i40evf_adapter *adapter,
250 struct i40e_hw *hw = &adapter->hw;
254 for (i = 1; i < adapter->num_msix_vectors; i++) {
255 if (mask & (1 << i)) {
256 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
257 dyn_ctl |= I40E_VFINT_DYN_CTLN_SWINT_TRIG_MASK |
258 I40E_VFINT_DYN_CTLN_CLEARPBA_MASK;
259 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
265 * i40evf_irq_enable - Enable default interrupt generation settings
266 * @adapter: board private structure
268 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
270 struct i40e_hw *hw = &adapter->hw;
272 i40evf_irq_enable_queues(adapter, ~0);
275 rd32(hw, I40E_VFGEN_RSTAT);
279 * i40evf_msix_aq - Interrupt handler for vector 0
280 * @irq: interrupt number
281 * @data: pointer to netdev
283 static irqreturn_t i40evf_msix_aq(int irq, void *data)
285 struct net_device *netdev = data;
286 struct i40evf_adapter *adapter = netdev_priv(netdev);
287 struct i40e_hw *hw = &adapter->hw;
291 /* handle non-queue interrupts */
292 val = rd32(hw, I40E_VFINT_ICR01);
293 ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
296 val = rd32(hw, I40E_VFINT_DYN_CTL01);
297 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
298 wr32(hw, I40E_VFINT_DYN_CTL01, val);
300 /* re-enable interrupt causes */
301 wr32(hw, I40E_VFINT_ICR0_ENA1, ena_mask);
302 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK);
304 /* schedule work on the private workqueue */
305 schedule_work(&adapter->adminq_task);
311 * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
312 * @irq: interrupt number
313 * @data: pointer to a q_vector
315 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
317 struct i40e_q_vector *q_vector = data;
319 if (!q_vector->tx.ring && !q_vector->rx.ring)
322 napi_schedule(&q_vector->napi);
328 * i40evf_map_vector_to_rxq - associate irqs with rx queues
329 * @adapter: board private structure
330 * @v_idx: interrupt number
331 * @r_idx: queue number
334 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
336 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
337 struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
339 rx_ring->q_vector = q_vector;
340 rx_ring->next = q_vector->rx.ring;
341 rx_ring->vsi = &adapter->vsi;
342 q_vector->rx.ring = rx_ring;
343 q_vector->rx.count++;
344 q_vector->rx.latency_range = I40E_LOW_LATENCY;
348 * i40evf_map_vector_to_txq - associate irqs with tx queues
349 * @adapter: board private structure
350 * @v_idx: interrupt number
351 * @t_idx: queue number
354 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
356 struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
357 struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
359 tx_ring->q_vector = q_vector;
360 tx_ring->next = q_vector->tx.ring;
361 tx_ring->vsi = &adapter->vsi;
362 q_vector->tx.ring = tx_ring;
363 q_vector->tx.count++;
364 q_vector->tx.latency_range = I40E_LOW_LATENCY;
365 q_vector->num_ringpairs++;
366 q_vector->ring_mask |= (1 << t_idx);
370 * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
371 * @adapter: board private structure to initialize
373 * This function maps descriptor rings to the queue-specific vectors
374 * we were allotted through the MSI-X enabling code. Ideally, we'd have
375 * one vector per ring/queue, but on a constrained vector budget, we
376 * group the rings as "efficiently" as possible. You would add new
377 * mapping configurations in here.
379 static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
383 int rxr_idx = 0, txr_idx = 0;
384 int rxr_remaining = adapter->vsi_res->num_queue_pairs;
385 int txr_remaining = adapter->vsi_res->num_queue_pairs;
390 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
392 /* The ideal configuration...
393 * We have enough vectors to map one per queue.
395 if (q_vectors == (rxr_remaining * 2)) {
396 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
397 i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
399 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
400 i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
404 /* If we don't have enough vectors for a 1-to-1
405 * mapping, we'll have to group them so there are
406 * multiple queues per vector.
407 * Re-adjusting *qpv takes care of the remainder.
409 for (i = v_start; i < q_vectors; i++) {
410 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
411 for (j = 0; j < rqpv; j++) {
412 i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
417 for (i = v_start; i < q_vectors; i++) {
418 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
419 for (j = 0; j < tqpv; j++) {
420 i40evf_map_vector_to_txq(adapter, i, txr_idx);
427 adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
433 * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
434 * @adapter: board private structure
436 * Allocates MSI-X vectors for tx and rx handling, and requests
437 * interrupts from the kernel.
440 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
442 int vector, err, q_vectors;
443 int rx_int_idx = 0, tx_int_idx = 0;
445 i40evf_irq_disable(adapter);
446 /* Decrement for Other and TCP Timer vectors */
447 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
449 for (vector = 0; vector < q_vectors; vector++) {
450 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
452 if (q_vector->tx.ring && q_vector->rx.ring) {
453 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
454 "i40evf-%s-%s-%d", basename,
455 "TxRx", rx_int_idx++);
457 } else if (q_vector->rx.ring) {
458 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
459 "i40evf-%s-%s-%d", basename,
461 } else if (q_vector->tx.ring) {
462 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
463 "i40evf-%s-%s-%d", basename,
466 /* skip this unused q_vector */
470 adapter->msix_entries[vector + NONQ_VECS].vector,
471 i40evf_msix_clean_rings,
476 dev_info(&adapter->pdev->dev,
477 "%s: request_irq failed, error: %d\n",
479 goto free_queue_irqs;
481 /* assign the mask for this irq */
482 irq_set_affinity_hint(
483 adapter->msix_entries[vector + NONQ_VECS].vector,
484 q_vector->affinity_mask);
492 irq_set_affinity_hint(
493 adapter->msix_entries[vector + NONQ_VECS].vector,
495 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
496 adapter->q_vector[vector]);
502 * i40evf_request_misc_irq - Initialize MSI-X interrupts
503 * @adapter: board private structure
505 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
506 * vector is only for the admin queue, and stays active even when the netdev
509 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
511 struct net_device *netdev = adapter->netdev;
514 sprintf(adapter->name[0], "i40evf:mbx");
515 err = request_irq(adapter->msix_entries[0].vector,
516 &i40evf_msix_aq, 0, adapter->name[0], netdev);
518 dev_err(&adapter->pdev->dev,
519 "request_irq for msix_aq failed: %d\n", err);
520 free_irq(adapter->msix_entries[0].vector, netdev);
526 * i40evf_free_traffic_irqs - Free MSI-X interrupts
527 * @adapter: board private structure
529 * Frees all MSI-X vectors other than 0.
531 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
535 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
537 for (i = 0; i < q_vectors; i++) {
538 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
540 free_irq(adapter->msix_entries[i+1].vector,
541 adapter->q_vector[i]);
546 * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
547 * @adapter: board private structure
549 * Frees MSI-X vector 0.
551 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
553 struct net_device *netdev = adapter->netdev;
555 free_irq(adapter->msix_entries[0].vector, netdev);
559 * i40evf_configure_tx - Configure Transmit Unit after Reset
560 * @adapter: board private structure
562 * Configure the Tx unit of the MAC after a reset.
564 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
566 struct i40e_hw *hw = &adapter->hw;
568 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
569 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
573 * i40evf_configure_rx - Configure Receive Unit after Reset
574 * @adapter: board private structure
576 * Configure the Rx unit of the MAC after a reset.
578 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
580 struct i40e_hw *hw = &adapter->hw;
581 struct net_device *netdev = adapter->netdev;
582 int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
587 adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
588 adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
590 /* Decide whether to use packet split mode or not */
591 if (netdev->mtu > ETH_DATA_LEN) {
592 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
593 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
595 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
597 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
598 adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
600 adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
603 /* Set the RX buffer length according to the mode */
604 if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
605 rx_buf_len = I40E_RX_HDR_SIZE;
607 if (netdev->mtu <= ETH_DATA_LEN)
608 rx_buf_len = I40EVF_RXBUFFER_2048;
610 rx_buf_len = ALIGN(max_frame, 1024);
613 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
614 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
615 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
620 * i40evf_find_vlan - Search filter list for specific vlan filter
621 * @adapter: board private structure
624 * Returns ptr to the filter object or NULL
627 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
629 struct i40evf_vlan_filter *f;
631 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
639 * i40evf_add_vlan - Add a vlan filter to the list
640 * @adapter: board private structure
643 * Returns ptr to the filter object or NULL when no memory available.
646 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
648 struct i40evf_vlan_filter *f;
650 f = i40evf_find_vlan(adapter, vlan);
652 f = kzalloc(sizeof(*f), GFP_ATOMIC);
654 dev_info(&adapter->pdev->dev,
655 "%s: no memory for new VLAN filter\n",
661 INIT_LIST_HEAD(&f->list);
662 list_add(&f->list, &adapter->vlan_filter_list);
664 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
671 * i40evf_del_vlan - Remove a vlan filter from the list
672 * @adapter: board private structure
675 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
677 struct i40evf_vlan_filter *f;
679 f = i40evf_find_vlan(adapter, vlan);
682 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
688 * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
689 * @netdev: network device struct
692 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
693 __always_unused __be16 proto, u16 vid)
695 struct i40evf_adapter *adapter = netdev_priv(netdev);
697 if (i40evf_add_vlan(adapter, vid) == NULL)
703 * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
704 * @netdev: network device struct
707 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
708 __always_unused __be16 proto, u16 vid)
710 struct i40evf_adapter *adapter = netdev_priv(netdev);
712 i40evf_del_vlan(adapter, vid);
717 * i40evf_find_filter - Search filter list for specific mac filter
718 * @adapter: board private structure
719 * @macaddr: the MAC address
721 * Returns ptr to the filter object or NULL
724 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
727 struct i40evf_mac_filter *f;
732 list_for_each_entry(f, &adapter->mac_filter_list, list) {
733 if (ether_addr_equal(macaddr, f->macaddr))
740 * i40e_add_filter - Add a mac filter to the filter list
741 * @adapter: board private structure
742 * @macaddr: the MAC address
744 * Returns ptr to the filter object or NULL when no memory available.
747 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
750 struct i40evf_mac_filter *f;
755 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
756 &adapter->crit_section))
759 f = i40evf_find_filter(adapter, macaddr);
761 f = kzalloc(sizeof(*f), GFP_ATOMIC);
763 dev_info(&adapter->pdev->dev,
764 "%s: no memory for new filter\n", __func__);
765 clear_bit(__I40EVF_IN_CRITICAL_TASK,
766 &adapter->crit_section);
770 memcpy(f->macaddr, macaddr, ETH_ALEN);
772 list_add(&f->list, &adapter->mac_filter_list);
774 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
777 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
782 * i40evf_set_mac - NDO callback to set port mac address
783 * @netdev: network interface device structure
784 * @p: pointer to an address structure
786 * Returns 0 on success, negative on failure
788 static int i40evf_set_mac(struct net_device *netdev, void *p)
790 struct i40evf_adapter *adapter = netdev_priv(netdev);
791 struct i40e_hw *hw = &adapter->hw;
792 struct i40evf_mac_filter *f;
793 struct sockaddr *addr = p;
795 if (!is_valid_ether_addr(addr->sa_data))
796 return -EADDRNOTAVAIL;
798 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
801 f = i40evf_add_filter(adapter, addr->sa_data);
803 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
804 memcpy(netdev->dev_addr, adapter->hw.mac.addr,
808 return (f == NULL) ? -ENOMEM : 0;
812 * i40evf_set_rx_mode - NDO callback to set the netdev filters
813 * @netdev: network interface device structure
815 static void i40evf_set_rx_mode(struct net_device *netdev)
817 struct i40evf_adapter *adapter = netdev_priv(netdev);
818 struct i40evf_mac_filter *f, *ftmp;
819 struct netdev_hw_addr *uca;
820 struct netdev_hw_addr *mca;
822 /* add addr if not already in the filter list */
823 netdev_for_each_uc_addr(uca, netdev) {
824 i40evf_add_filter(adapter, uca->addr);
826 netdev_for_each_mc_addr(mca, netdev) {
827 i40evf_add_filter(adapter, mca->addr);
830 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
831 &adapter->crit_section))
833 /* remove filter if not in netdev list */
834 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
837 if (f->macaddr[0] & 0x01) {
838 netdev_for_each_mc_addr(mca, netdev) {
839 if (ether_addr_equal(mca->addr, f->macaddr)) {
845 netdev_for_each_uc_addr(uca, netdev) {
846 if (ether_addr_equal(uca->addr, f->macaddr)) {
854 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
857 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
861 * i40evf_napi_enable_all - enable NAPI on all queue vectors
862 * @adapter: board private structure
864 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
867 struct i40e_q_vector *q_vector;
868 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
870 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
871 struct napi_struct *napi;
872 q_vector = adapter->q_vector[q_idx];
873 napi = &q_vector->napi;
879 * i40evf_napi_disable_all - disable NAPI on all queue vectors
880 * @adapter: board private structure
882 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
885 struct i40e_q_vector *q_vector;
886 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
888 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
889 q_vector = adapter->q_vector[q_idx];
890 napi_disable(&q_vector->napi);
895 * i40evf_configure - set up transmit and receive data structures
896 * @adapter: board private structure
898 static void i40evf_configure(struct i40evf_adapter *adapter)
900 struct net_device *netdev = adapter->netdev;
903 i40evf_set_rx_mode(netdev);
905 i40evf_configure_tx(adapter);
906 i40evf_configure_rx(adapter);
907 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
909 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
910 struct i40e_ring *ring = adapter->rx_rings[i];
911 i40evf_alloc_rx_buffers(ring, ring->count);
912 ring->next_to_use = ring->count - 1;
913 writel(ring->next_to_use, ring->tail);
918 * i40evf_up_complete - Finish the last steps of bringing up a connection
919 * @adapter: board private structure
921 static int i40evf_up_complete(struct i40evf_adapter *adapter)
923 adapter->state = __I40EVF_RUNNING;
924 clear_bit(__I40E_DOWN, &adapter->vsi.state);
926 i40evf_napi_enable_all(adapter);
928 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
929 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
934 * i40evf_clean_all_rx_rings - Free Rx Buffers for all queues
935 * @adapter: board private structure
937 static void i40evf_clean_all_rx_rings(struct i40evf_adapter *adapter)
941 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
942 i40evf_clean_rx_ring(adapter->rx_rings[i]);
946 * i40evf_clean_all_tx_rings - Free Tx Buffers for all queues
947 * @adapter: board private structure
949 static void i40evf_clean_all_tx_rings(struct i40evf_adapter *adapter)
953 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
954 i40evf_clean_tx_ring(adapter->tx_rings[i]);
958 * i40e_down - Shutdown the connection processing
959 * @adapter: board private structure
961 void i40evf_down(struct i40evf_adapter *adapter)
963 struct net_device *netdev = adapter->netdev;
964 struct i40evf_mac_filter *f;
966 /* remove all MAC filters from the VSI */
967 list_for_each_entry(f, &adapter->mac_filter_list, list) {
970 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
971 /* disable receives */
972 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
973 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
976 netif_tx_disable(netdev);
978 netif_tx_stop_all_queues(netdev);
980 i40evf_irq_disable(adapter);
982 i40evf_napi_disable_all(adapter);
984 netif_carrier_off(netdev);
986 i40evf_clean_all_tx_rings(adapter);
987 i40evf_clean_all_rx_rings(adapter);
991 * i40evf_acquire_msix_vectors - Setup the MSIX capability
992 * @adapter: board private structure
993 * @vectors: number of vectors to request
995 * Work with the OS to set up the MSIX vectors needed.
997 * Returns 0 on success, negative on failure
1000 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1002 int err, vector_threshold;
1004 /* We'll want at least 3 (vector_threshold):
1005 * 0) Other (Admin Queue and link, mostly)
1009 vector_threshold = MIN_MSIX_COUNT;
1011 /* The more we get, the more we will assign to Tx/Rx Cleanup
1012 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1013 * Right now, we simply care about how many we'll get; we'll
1014 * set them up later while requesting irq's.
1016 while (vectors >= vector_threshold) {
1017 err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
1019 if (!err) /* Success in acquiring all requested vectors. */
1022 vectors = 0; /* Nasty failure, quit now */
1023 else /* err == number of vectors we should try again with */
1027 if (vectors < vector_threshold) {
1028 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts.\n");
1029 kfree(adapter->msix_entries);
1030 adapter->msix_entries = NULL;
1033 /* Adjust for only the vectors we'll use, which is minimum
1034 * of max_msix_q_vectors + NONQ_VECS, or the number of
1035 * vectors we were allocated.
1037 adapter->num_msix_vectors = vectors;
1043 * i40evf_free_queues - Free memory for all rings
1044 * @adapter: board private structure to initialize
1046 * Free all of the memory associated with queue pairs.
1048 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1052 if (!adapter->vsi_res)
1054 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1055 if (adapter->tx_rings[i])
1056 kfree_rcu(adapter->tx_rings[i], rcu);
1057 adapter->tx_rings[i] = NULL;
1058 adapter->rx_rings[i] = NULL;
1063 * i40evf_alloc_queues - Allocate memory for all rings
1064 * @adapter: board private structure to initialize
1066 * We allocate one ring per queue at run-time since we don't know the
1067 * number of queues at compile-time. The polling_netdev array is
1068 * intended for Multiqueue, but should work fine with a single queue.
1070 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1074 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1075 struct i40e_ring *tx_ring;
1076 struct i40e_ring *rx_ring;
1078 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
1082 tx_ring->queue_index = i;
1083 tx_ring->netdev = adapter->netdev;
1084 tx_ring->dev = &adapter->pdev->dev;
1085 tx_ring->count = I40EVF_DEFAULT_TXD;
1086 adapter->tx_rings[i] = tx_ring;
1088 rx_ring = &tx_ring[1];
1089 rx_ring->queue_index = i;
1090 rx_ring->netdev = adapter->netdev;
1091 rx_ring->dev = &adapter->pdev->dev;
1092 rx_ring->count = I40EVF_DEFAULT_RXD;
1093 adapter->rx_rings[i] = rx_ring;
1099 i40evf_free_queues(adapter);
1104 * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1105 * @adapter: board private structure to initialize
1107 * Attempt to configure the interrupts using the best available
1108 * capabilities of the hardware and the kernel.
1110 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1112 int vector, v_budget;
1116 if (!adapter->vsi_res) {
1120 pairs = adapter->vsi_res->num_queue_pairs;
1122 /* It's easy to be greedy for MSI-X vectors, but it really
1123 * doesn't do us much good if we have a lot more vectors
1124 * than CPU's. So let's be conservative and only ask for
1125 * (roughly) twice the number of vectors as there are CPU's.
1127 v_budget = min(pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1128 v_budget = min(v_budget, (int)adapter->vf_res->max_vectors + 1);
1130 /* A failure in MSI-X entry allocation isn't fatal, but it does
1131 * mean we disable MSI-X capabilities of the adapter.
1133 adapter->msix_entries = kcalloc(v_budget,
1134 sizeof(struct msix_entry), GFP_KERNEL);
1135 if (!adapter->msix_entries) {
1140 for (vector = 0; vector < v_budget; vector++)
1141 adapter->msix_entries[vector].entry = vector;
1143 i40evf_acquire_msix_vectors(adapter, v_budget);
1146 adapter->netdev->real_num_tx_queues = pairs;
1151 * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1152 * @adapter: board private structure to initialize
1154 * We allocate one q_vector per queue interrupt. If allocation fails we
1157 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1159 int q_idx, num_q_vectors;
1160 struct i40e_q_vector *q_vector;
1162 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1164 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1165 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
1168 q_vector->adapter = adapter;
1169 q_vector->vsi = &adapter->vsi;
1170 q_vector->v_idx = q_idx;
1171 netif_napi_add(adapter->netdev, &q_vector->napi,
1172 i40evf_napi_poll, 64);
1173 adapter->q_vector[q_idx] = q_vector;
1181 q_vector = adapter->q_vector[q_idx];
1182 netif_napi_del(&q_vector->napi);
1184 adapter->q_vector[q_idx] = NULL;
1190 * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1191 * @adapter: board private structure to initialize
1193 * This function frees the memory allocated to the q_vectors. In addition if
1194 * NAPI is enabled it will delete any references to the NAPI struct prior
1195 * to freeing the q_vector.
1197 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1199 int q_idx, num_q_vectors;
1202 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1203 napi_vectors = adapter->vsi_res->num_queue_pairs;
1205 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1206 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1208 adapter->q_vector[q_idx] = NULL;
1209 if (q_idx < napi_vectors)
1210 netif_napi_del(&q_vector->napi);
1216 * i40evf_reset_interrupt_capability - Reset MSIX setup
1217 * @adapter: board private structure
1220 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1222 pci_disable_msix(adapter->pdev);
1223 kfree(adapter->msix_entries);
1224 adapter->msix_entries = NULL;
1230 * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1231 * @adapter: board private structure to initialize
1234 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1238 err = i40evf_set_interrupt_capability(adapter);
1240 dev_err(&adapter->pdev->dev,
1241 "Unable to setup interrupt capabilities\n");
1242 goto err_set_interrupt;
1245 err = i40evf_alloc_q_vectors(adapter);
1247 dev_err(&adapter->pdev->dev,
1248 "Unable to allocate memory for queue vectors\n");
1249 goto err_alloc_q_vectors;
1252 err = i40evf_alloc_queues(adapter);
1254 dev_err(&adapter->pdev->dev,
1255 "Unable to allocate memory for queues\n");
1256 goto err_alloc_queues;
1259 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1260 (adapter->vsi_res->num_queue_pairs > 1) ? "Enabled" :
1261 "Disabled", adapter->vsi_res->num_queue_pairs);
1265 i40evf_free_q_vectors(adapter);
1266 err_alloc_q_vectors:
1267 i40evf_reset_interrupt_capability(adapter);
1273 * i40evf_watchdog_timer - Periodic call-back timer
1274 * @data: pointer to adapter disguised as unsigned long
1276 static void i40evf_watchdog_timer(unsigned long data)
1278 struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1279 schedule_work(&adapter->watchdog_task);
1280 /* timer will be rescheduled in watchdog task */
1284 * i40evf_watchdog_task - Periodic call-back task
1285 * @work: pointer to work_struct
1287 static void i40evf_watchdog_task(struct work_struct *work)
1289 struct i40evf_adapter *adapter = container_of(work,
1290 struct i40evf_adapter,
1292 struct i40e_hw *hw = &adapter->hw;
1294 if (adapter->state < __I40EVF_DOWN)
1297 if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1300 /* check for unannounced reset */
1301 if ((adapter->state != __I40EVF_RESETTING) &&
1302 (rd32(hw, I40E_VFGEN_RSTAT) & 0x3) != I40E_VFR_VFACTIVE) {
1303 adapter->state = __I40EVF_RESETTING;
1304 schedule_work(&adapter->reset_task);
1305 dev_info(&adapter->pdev->dev, "%s: hardware reset detected\n",
1310 /* Process admin queue tasks. After init, everything gets done
1311 * here so we don't race on the admin queue.
1313 if (adapter->aq_pending)
1316 if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1317 i40evf_map_queues(adapter);
1321 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1322 i40evf_add_ether_addrs(adapter);
1326 if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1327 i40evf_add_vlans(adapter);
1331 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1332 i40evf_del_ether_addrs(adapter);
1336 if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1337 i40evf_del_vlans(adapter);
1341 if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1342 i40evf_disable_queues(adapter);
1346 if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1347 i40evf_configure_queues(adapter);
1351 if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1352 i40evf_enable_queues(adapter);
1356 if (adapter->state == __I40EVF_RUNNING)
1357 i40evf_request_stats(adapter);
1359 i40evf_irq_enable(adapter, true);
1360 i40evf_fire_sw_int(adapter, 0xFF);
1362 if (adapter->aq_required)
1363 mod_timer(&adapter->watchdog_timer,
1364 jiffies + msecs_to_jiffies(20));
1366 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1367 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1368 schedule_work(&adapter->adminq_task);
1372 * i40evf_configure_rss - Prepare for RSS if used
1373 * @adapter: board private structure
1375 static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1377 struct i40e_hw *hw = &adapter->hw;
1382 /* Set of random keys generated using kernel random number generator */
1383 static const u32 seed[I40E_VFQF_HKEY_MAX_INDEX + 1] = {
1384 0x794221b4, 0xbca0c5ab, 0x6cd5ebd9, 0x1ada6127,
1385 0x983b3aa1, 0x1c4e71eb, 0x7f6328b2, 0xfcdc0da0,
1386 0xc135cafa, 0x7a6f7e2d, 0xe7102d28, 0x163cd12e,
1389 /* Hash type is configured by the PF - we just supply the key */
1391 /* Fill out hash function seed */
1392 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1393 wr32(hw, I40E_VFQF_HKEY(i), seed[i]);
1395 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1396 hena = I40E_DEFAULT_RSS_HENA;
1397 wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1398 wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1400 /* Populate the LUT with max no. of queues in round robin fashion */
1401 for (i = 0, j = 0; i < I40E_VFQF_HLUT_MAX_INDEX; i++, j++) {
1402 if (j == adapter->vsi_res->num_queue_pairs)
1404 /* lut = 4-byte sliding window of 4 lut entries */
1405 lut = (lut << 8) | (j &
1407 /* On i = 3, we have 4 entries in lut; write to the register */
1409 wr32(hw, I40E_VFQF_HLUT(i >> 2), lut);
1415 * i40evf_reset_task - Call-back task to handle hardware reset
1416 * @work: pointer to work_struct
1418 * During reset we need to shut down and reinitialize the admin queue
1419 * before we can use it to communicate with the PF again. We also clear
1420 * and reinit the rings because that context is lost as well.
1422 static void i40evf_reset_task(struct work_struct *work)
1424 struct i40evf_adapter *adapter =
1425 container_of(work, struct i40evf_adapter, reset_task);
1426 struct i40e_hw *hw = &adapter->hw;
1430 while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1431 &adapter->crit_section))
1434 /* wait until the reset is complete */
1435 for (i = 0; i < 20; i++) {
1436 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1437 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1438 if (rstat_val == I40E_VFR_COMPLETED)
1444 /* reset never finished */
1445 dev_info(&adapter->pdev->dev, "%s: reset never finished: %x\n",
1446 __func__, rstat_val);
1447 /* carry on anyway */
1449 i40evf_down(adapter);
1450 adapter->state = __I40EVF_RESETTING;
1452 /* kill and reinit the admin queue */
1453 if (i40evf_shutdown_adminq(hw))
1454 dev_warn(&adapter->pdev->dev,
1455 "%s: Failed to destroy the Admin Queue resources\n",
1457 err = i40evf_init_adminq(hw);
1459 dev_info(&adapter->pdev->dev, "%s: init_adminq failed: %d\n",
1462 adapter->aq_pending = 0;
1463 adapter->aq_required = 0;
1464 i40evf_map_queues(adapter);
1465 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1467 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1469 if (netif_running(adapter->netdev)) {
1470 /* allocate transmit descriptors */
1471 err = i40evf_setup_all_tx_resources(adapter);
1475 /* allocate receive descriptors */
1476 err = i40evf_setup_all_rx_resources(adapter);
1480 i40evf_configure(adapter);
1482 err = i40evf_up_complete(adapter);
1486 i40evf_irq_enable(adapter, true);
1490 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit.\n");
1491 i40evf_close(adapter->netdev);
1495 * i40evf_adminq_task - worker thread to clean the admin queue
1496 * @work: pointer to work_struct containing our data
1498 static void i40evf_adminq_task(struct work_struct *work)
1500 struct i40evf_adapter *adapter =
1501 container_of(work, struct i40evf_adapter, adminq_task);
1502 struct i40e_hw *hw = &adapter->hw;
1503 struct i40e_arq_event_info event;
1504 struct i40e_virtchnl_msg *v_msg;
1508 event.msg_size = I40EVF_MAX_AQ_BUF_SIZE;
1509 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
1510 if (!event.msg_buf) {
1511 dev_info(&adapter->pdev->dev, "%s: no memory for ARQ clean\n",
1515 v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1517 ret = i40evf_clean_arq_element(hw, &event, &pending);
1519 break; /* No event to process or error cleaning ARQ */
1521 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1522 v_msg->v_retval, event.msg_buf,
1525 dev_info(&adapter->pdev->dev,
1526 "%s: ARQ: Pending events %d\n",
1528 memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1532 /* re-enable Admin queue interrupt cause */
1533 i40evf_misc_irq_enable(adapter);
1535 kfree(event.msg_buf);
1539 * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1540 * @adapter: board private structure
1542 * Free all transmit software resources
1544 static void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1548 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1549 if (adapter->tx_rings[i]->desc)
1550 i40evf_free_tx_resources(adapter->tx_rings[i]);
1555 * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1556 * @adapter: board private structure
1558 * If this function returns with an error, then it's possible one or
1559 * more of the rings is populated (while the rest are not). It is the
1560 * callers duty to clean those orphaned rings.
1562 * Return 0 on success, negative on failure
1564 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1568 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1569 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1572 dev_err(&adapter->pdev->dev,
1573 "%s: Allocation for Tx Queue %u failed\n",
1582 * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1583 * @adapter: board private structure
1585 * If this function returns with an error, then it's possible one or
1586 * more of the rings is populated (while the rest are not). It is the
1587 * callers duty to clean those orphaned rings.
1589 * Return 0 on success, negative on failure
1591 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1595 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++) {
1596 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1599 dev_err(&adapter->pdev->dev,
1600 "%s: Allocation for Rx Queue %u failed\n",
1608 * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1609 * @adapter: board private structure
1611 * Free all receive software resources
1613 static void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1617 for (i = 0; i < adapter->vsi_res->num_queue_pairs; i++)
1618 if (adapter->rx_rings[i]->desc)
1619 i40evf_free_rx_resources(adapter->rx_rings[i]);
1623 * i40evf_open - Called when a network interface is made active
1624 * @netdev: network interface device structure
1626 * Returns 0 on success, negative value on failure
1628 * The open entry point is called when a network interface is made
1629 * active by the system (IFF_UP). At this point all resources needed
1630 * for transmit and receive operations are allocated, the interrupt
1631 * handler is registered with the OS, the watchdog timer is started,
1632 * and the stack is notified that the interface is ready.
1634 static int i40evf_open(struct net_device *netdev)
1636 struct i40evf_adapter *adapter = netdev_priv(netdev);
1639 if (adapter->state != __I40EVF_DOWN)
1642 /* allocate transmit descriptors */
1643 err = i40evf_setup_all_tx_resources(adapter);
1647 /* allocate receive descriptors */
1648 err = i40evf_setup_all_rx_resources(adapter);
1652 /* clear any pending interrupts, may auto mask */
1653 err = i40evf_request_traffic_irqs(adapter, netdev->name);
1657 i40evf_configure(adapter);
1659 err = i40evf_up_complete(adapter);
1663 i40evf_irq_enable(adapter, true);
1668 i40evf_down(adapter);
1669 i40evf_free_traffic_irqs(adapter);
1671 i40evf_free_all_rx_resources(adapter);
1673 i40evf_free_all_tx_resources(adapter);
1679 * i40evf_close - Disables a network interface
1680 * @netdev: network interface device structure
1682 * Returns 0, this is not allowed to fail
1684 * The close entry point is called when an interface is de-activated
1685 * by the OS. The hardware is still under the drivers control, but
1686 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1687 * are freed, along with all transmit and receive resources.
1689 static int i40evf_close(struct net_device *netdev)
1691 struct i40evf_adapter *adapter = netdev_priv(netdev);
1693 /* signal that we are down to the interrupt handler */
1694 adapter->state = __I40EVF_DOWN;
1695 set_bit(__I40E_DOWN, &adapter->vsi.state);
1697 i40evf_down(adapter);
1698 i40evf_free_traffic_irqs(adapter);
1700 i40evf_free_all_tx_resources(adapter);
1701 i40evf_free_all_rx_resources(adapter);
1707 * i40evf_get_stats - Get System Network Statistics
1708 * @netdev: network interface device structure
1710 * Returns the address of the device statistics structure.
1711 * The statistics are actually updated from the timer callback.
1713 static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
1715 struct i40evf_adapter *adapter = netdev_priv(netdev);
1717 /* only return the current stats */
1718 return &adapter->net_stats;
1722 * i40evf_reinit_locked - Software reinit
1723 * @adapter: board private structure
1725 * Reinititalizes the ring structures in response to a software configuration
1726 * change. Roughly the same as close followed by open, but skips releasing
1727 * and reallocating the interrupts.
1729 void i40evf_reinit_locked(struct i40evf_adapter *adapter)
1731 struct net_device *netdev = adapter->netdev;
1734 WARN_ON(in_interrupt());
1736 adapter->state = __I40EVF_RESETTING;
1738 i40evf_down(adapter);
1740 /* allocate transmit descriptors */
1741 err = i40evf_setup_all_tx_resources(adapter);
1745 /* allocate receive descriptors */
1746 err = i40evf_setup_all_rx_resources(adapter);
1750 i40evf_configure(adapter);
1752 err = i40evf_up_complete(adapter);
1756 i40evf_irq_enable(adapter, true);
1760 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit.\n");
1761 i40evf_close(netdev);
1765 * i40evf_change_mtu - Change the Maximum Transfer Unit
1766 * @netdev: network interface device structure
1767 * @new_mtu: new value for maximum frame size
1769 * Returns 0 on success, negative on failure
1771 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
1773 struct i40evf_adapter *adapter = netdev_priv(netdev);
1774 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1776 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1779 /* must set new MTU before calling down or up */
1780 netdev->mtu = new_mtu;
1781 i40evf_reinit_locked(adapter);
1785 static const struct net_device_ops i40evf_netdev_ops = {
1786 .ndo_open = i40evf_open,
1787 .ndo_stop = i40evf_close,
1788 .ndo_start_xmit = i40evf_xmit_frame,
1789 .ndo_get_stats = i40evf_get_stats,
1790 .ndo_set_rx_mode = i40evf_set_rx_mode,
1791 .ndo_validate_addr = eth_validate_addr,
1792 .ndo_set_mac_address = i40evf_set_mac,
1793 .ndo_change_mtu = i40evf_change_mtu,
1794 .ndo_tx_timeout = i40evf_tx_timeout,
1795 .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
1796 .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
1800 * i40evf_check_reset_complete - check that VF reset is complete
1801 * @hw: pointer to hw struct
1803 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1805 static int i40evf_check_reset_complete(struct i40e_hw *hw)
1810 for (i = 0; i < 100; i++) {
1811 rstat = rd32(hw, I40E_VFGEN_RSTAT);
1812 if (rstat == I40E_VFR_VFACTIVE)
1820 * i40evf_init_task - worker thread to perform delayed initialization
1821 * @work: pointer to work_struct containing our data
1823 * This task completes the work that was begun in probe. Due to the nature
1824 * of VF-PF communications, we may need to wait tens of milliseconds to get
1825 * reponses back from the PF. Rather than busy-wait in probe and bog down the
1826 * whole system, we'll do it in a task so we can sleep.
1827 * This task only runs during driver init. Once we've established
1828 * communications with the PF driver and set up our netdev, the watchdog
1831 static void i40evf_init_task(struct work_struct *work)
1833 struct i40evf_adapter *adapter = container_of(work,
1834 struct i40evf_adapter,
1836 struct net_device *netdev = adapter->netdev;
1837 struct i40evf_mac_filter *f;
1838 struct i40e_hw *hw = &adapter->hw;
1839 struct pci_dev *pdev = adapter->pdev;
1842 switch (adapter->state) {
1843 case __I40EVF_STARTUP:
1844 /* driver loaded, probe complete */
1845 err = i40e_set_mac_type(hw);
1847 dev_info(&pdev->dev, "%s: set_mac_type failed: %d\n",
1851 err = i40evf_check_reset_complete(hw);
1853 dev_info(&pdev->dev, "%s: device is still in reset (%d).\n",
1857 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
1858 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
1859 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
1860 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
1862 err = i40evf_init_adminq(hw);
1864 dev_info(&pdev->dev, "%s: init_adminq failed: %d\n",
1868 err = i40evf_send_api_ver(adapter);
1870 dev_info(&pdev->dev, "%s: unable to send to PF (%d)\n",
1872 i40evf_shutdown_adminq(hw);
1875 adapter->state = __I40EVF_INIT_VERSION_CHECK;
1878 case __I40EVF_INIT_VERSION_CHECK:
1879 if (!i40evf_asq_done(hw))
1882 /* aq msg sent, awaiting reply */
1883 err = i40evf_verify_api_ver(adapter);
1885 dev_err(&pdev->dev, "Unable to verify API version, error %d\n",
1889 err = i40evf_send_vf_config_msg(adapter);
1891 dev_err(&pdev->dev, "Unable send config request, error %d\n",
1895 adapter->state = __I40EVF_INIT_GET_RESOURCES;
1898 case __I40EVF_INIT_GET_RESOURCES:
1899 /* aq msg sent, awaiting reply */
1900 if (!adapter->vf_res) {
1901 bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
1903 sizeof(struct i40e_virtchnl_vsi_resource));
1904 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
1905 if (!adapter->vf_res) {
1906 dev_err(&pdev->dev, "%s: unable to allocate memory\n",
1911 err = i40evf_get_vf_config(adapter);
1912 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
1915 dev_info(&pdev->dev, "%s: unable to get VF config (%d)\n",
1919 adapter->state = __I40EVF_INIT_SW;
1924 /* got VF config message back from PF, now we can parse it */
1925 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
1926 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
1927 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
1929 if (!adapter->vsi_res) {
1930 dev_info(&pdev->dev, "%s: no LAN VSI found\n", __func__);
1934 adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
1936 adapter->txd_count = I40EVF_DEFAULT_TXD;
1937 adapter->rxd_count = I40EVF_DEFAULT_RXD;
1939 netdev->netdev_ops = &i40evf_netdev_ops;
1940 i40evf_set_ethtool_ops(netdev);
1941 netdev->watchdog_timeo = 5 * HZ;
1943 netdev->features |= NETIF_F_SG |
1951 if (adapter->vf_res->vf_offload_flags
1952 & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
1953 netdev->vlan_features = netdev->features;
1954 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1955 NETIF_F_HW_VLAN_CTAG_RX |
1956 NETIF_F_HW_VLAN_CTAG_FILTER;
1959 /* The HW MAC address was set and/or determined in sw_init */
1960 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
1961 dev_info(&pdev->dev,
1962 "Invalid MAC address %pMAC, using random\n",
1963 adapter->hw.mac.addr);
1964 random_ether_addr(adapter->hw.mac.addr);
1966 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
1967 memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
1969 INIT_LIST_HEAD(&adapter->mac_filter_list);
1970 INIT_LIST_HEAD(&adapter->vlan_filter_list);
1971 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1975 memcpy(f->macaddr, adapter->hw.mac.addr, ETH_ALEN);
1977 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1979 list_add(&f->list, &adapter->mac_filter_list);
1981 init_timer(&adapter->watchdog_timer);
1982 adapter->watchdog_timer.function = &i40evf_watchdog_timer;
1983 adapter->watchdog_timer.data = (unsigned long)adapter;
1984 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1986 err = i40evf_init_interrupt_scheme(adapter);
1989 i40evf_map_rings_to_vectors(adapter);
1990 i40evf_configure_rss(adapter);
1991 err = i40evf_request_misc_irq(adapter);
1995 netif_carrier_off(netdev);
1997 strcpy(netdev->name, "eth%d");
1999 adapter->vsi.id = adapter->vsi_res->vsi_id;
2000 adapter->vsi.seid = adapter->vsi_res->vsi_id; /* dummy */
2001 adapter->vsi.back = adapter;
2002 adapter->vsi.base_vector = 1;
2003 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2004 adapter->vsi.rx_itr_setting = I40E_ITR_DYNAMIC;
2005 adapter->vsi.tx_itr_setting = I40E_ITR_DYNAMIC;
2006 adapter->vsi.netdev = adapter->netdev;
2008 err = register_netdev(netdev);
2012 adapter->netdev_registered = true;
2014 netif_tx_stop_all_queues(netdev);
2016 dev_info(&pdev->dev, "MAC address: %pMAC\n", adapter->hw.mac.addr);
2017 if (netdev->features & NETIF_F_GRO)
2018 dev_info(&pdev->dev, "GRO is enabled\n");
2020 dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2021 adapter->state = __I40EVF_DOWN;
2022 set_bit(__I40E_DOWN, &adapter->vsi.state);
2023 i40evf_misc_irq_enable(adapter);
2026 schedule_delayed_work(&adapter->init_task,
2027 msecs_to_jiffies(50));
2031 i40evf_free_misc_irq(adapter);
2033 i40evf_reset_interrupt_capability(adapter);
2034 adapter->state = __I40EVF_FAILED;
2036 kfree(adapter->vf_res);
2037 adapter->vf_res = NULL;
2039 /* Things went into the weeds, so try again later */
2040 if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2041 dev_err(&pdev->dev, "Failed to communicate with PF; giving up.\n");
2042 if (hw->aq.asq.count)
2043 i40evf_shutdown_adminq(hw); /* ignore error */
2044 adapter->state = __I40EVF_FAILED;
2045 return; /* do not reschedule */
2047 schedule_delayed_work(&adapter->init_task, HZ * 3);
2052 * i40evf_shutdown - Shutdown the device in preparation for a reboot
2053 * @pdev: pci device structure
2055 static void i40evf_shutdown(struct pci_dev *pdev)
2057 struct net_device *netdev = pci_get_drvdata(pdev);
2059 netif_device_detach(netdev);
2061 if (netif_running(netdev))
2062 i40evf_close(netdev);
2065 pci_save_state(pdev);
2068 pci_disable_device(pdev);
2072 * i40evf_probe - Device Initialization Routine
2073 * @pdev: PCI device information struct
2074 * @ent: entry in i40evf_pci_tbl
2076 * Returns 0 on success, negative on failure
2078 * i40evf_probe initializes an adapter identified by a pci_dev structure.
2079 * The OS initialization, configuring of the adapter private structure,
2080 * and a hardware reset occur.
2082 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2084 struct net_device *netdev;
2085 struct i40evf_adapter *adapter = NULL;
2086 struct i40e_hw *hw = NULL;
2087 int err, pci_using_dac;
2089 err = pci_enable_device(pdev);
2093 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
2094 pci_using_dac = true;
2095 /* coherent mask for the same size will always succeed if
2098 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
2099 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2100 pci_using_dac = false;
2101 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
2103 dev_err(&pdev->dev, "%s: DMA configuration failed: %d\n",
2109 err = pci_request_regions(pdev, i40evf_driver_name);
2112 "pci_request_regions failed 0x%x\n", err);
2116 pci_enable_pcie_error_reporting(pdev);
2118 pci_set_master(pdev);
2120 netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2124 goto err_alloc_etherdev;
2127 SET_NETDEV_DEV(netdev, &pdev->dev);
2129 pci_set_drvdata(pdev, netdev);
2130 adapter = netdev_priv(netdev);
2132 netdev->features |= NETIF_F_HIGHDMA;
2134 adapter->netdev = netdev;
2135 adapter->pdev = pdev;
2140 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2141 adapter->state = __I40EVF_STARTUP;
2143 /* Call save state here because it relies on the adapter struct. */
2144 pci_save_state(pdev);
2146 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2147 pci_resource_len(pdev, 0));
2152 hw->vendor_id = pdev->vendor;
2153 hw->device_id = pdev->device;
2154 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2155 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2156 hw->subsystem_device_id = pdev->subsystem_device;
2157 hw->bus.device = PCI_SLOT(pdev->devfn);
2158 hw->bus.func = PCI_FUNC(pdev->devfn);
2160 INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2161 INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2162 INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2163 INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2164 schedule_delayed_work(&adapter->init_task, 10);
2169 free_netdev(netdev);
2171 pci_release_regions(pdev);
2174 pci_disable_device(pdev);
2180 * i40evf_suspend - Power management suspend routine
2181 * @pdev: PCI device information struct
2184 * Called when the system (VM) is entering sleep/suspend.
2186 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2188 struct net_device *netdev = pci_get_drvdata(pdev);
2189 struct i40evf_adapter *adapter = netdev_priv(netdev);
2192 netif_device_detach(netdev);
2194 if (netif_running(netdev)) {
2196 i40evf_down(adapter);
2199 i40evf_free_misc_irq(adapter);
2200 i40evf_reset_interrupt_capability(adapter);
2202 retval = pci_save_state(pdev);
2206 pci_disable_device(pdev);
2212 * i40evf_resume - Power managment resume routine
2213 * @pdev: PCI device information struct
2215 * Called when the system (VM) is resumed from sleep/suspend.
2217 static int i40evf_resume(struct pci_dev *pdev)
2219 struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2220 struct net_device *netdev = adapter->netdev;
2223 pci_set_power_state(pdev, PCI_D0);
2224 pci_restore_state(pdev);
2225 /* pci_restore_state clears dev->state_saved so call
2226 * pci_save_state to restore it.
2228 pci_save_state(pdev);
2230 err = pci_enable_device_mem(pdev);
2232 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2235 pci_set_master(pdev);
2238 err = i40evf_set_interrupt_capability(adapter);
2240 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2243 err = i40evf_request_misc_irq(adapter);
2246 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2250 schedule_work(&adapter->reset_task);
2252 netif_device_attach(netdev);
2257 #endif /* CONFIG_PM */
2259 * i40evf_remove - Device Removal Routine
2260 * @pdev: PCI device information struct
2262 * i40evf_remove is called by the PCI subsystem to alert the driver
2263 * that it should release a PCI device. The could be caused by a
2264 * Hot-Plug event, or because the driver is going to be removed from
2267 static void i40evf_remove(struct pci_dev *pdev)
2269 struct net_device *netdev = pci_get_drvdata(pdev);
2270 struct i40evf_adapter *adapter = netdev_priv(netdev);
2271 struct i40e_hw *hw = &adapter->hw;
2273 cancel_delayed_work_sync(&adapter->init_task);
2275 if (adapter->netdev_registered) {
2276 unregister_netdev(netdev);
2277 adapter->netdev_registered = false;
2279 adapter->state = __I40EVF_REMOVE;
2281 if (adapter->num_msix_vectors) {
2282 i40evf_misc_irq_disable(adapter);
2283 del_timer_sync(&adapter->watchdog_timer);
2285 flush_scheduled_work();
2287 i40evf_free_misc_irq(adapter);
2289 i40evf_reset_interrupt_capability(adapter);
2292 if (hw->aq.asq.count)
2293 i40evf_shutdown_adminq(hw);
2295 iounmap(hw->hw_addr);
2296 pci_release_regions(pdev);
2298 i40evf_free_queues(adapter);
2299 kfree(adapter->vf_res);
2301 free_netdev(netdev);
2303 pci_disable_pcie_error_reporting(pdev);
2305 pci_disable_device(pdev);
2308 static struct pci_driver i40evf_driver = {
2309 .name = i40evf_driver_name,
2310 .id_table = i40evf_pci_tbl,
2311 .probe = i40evf_probe,
2312 .remove = i40evf_remove,
2314 .suspend = i40evf_suspend,
2315 .resume = i40evf_resume,
2317 .shutdown = i40evf_shutdown,
2321 * i40e_init_module - Driver Registration Routine
2323 * i40e_init_module is the first routine called when the driver is
2324 * loaded. All it does is register with the PCI subsystem.
2326 static int __init i40evf_init_module(void)
2329 pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2330 i40evf_driver_version);
2332 pr_info("%s\n", i40evf_copyright);
2334 ret = pci_register_driver(&i40evf_driver);
2338 module_init(i40evf_init_module);
2341 * i40e_exit_module - Driver Exit Cleanup Routine
2343 * i40e_exit_module is called just before the driver is removed
2346 static void __exit i40evf_exit_module(void)
2348 pci_unregister_driver(&i40evf_driver);
2351 module_exit(i40evf_exit_module);