2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/pci.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
32 #include <linux/if_ether.h>
33 #include <linux/if_vlan.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/prefetch.h>
40 #include <net/ip6_checksum.h>
41 #include <linux/ktime.h>
42 #include <linux/numa.h>
43 #ifdef CONFIG_RFS_ACCEL
44 #include <linux/cpu_rmap.h>
46 #include <linux/crash_dump.h>
47 #include <net/busy_poll.h>
48 #include <net/vxlan.h>
50 #include "cq_enet_desc.h"
52 #include "vnic_intr.h"
53 #include "vnic_stats.h"
59 #include "enic_clsf.h"
61 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
62 #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
63 #define MAX_TSO (1 << 16)
64 #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
66 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
67 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
68 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF 0x0071 /* enet SRIOV VF */
70 #define RX_COPYBREAK_DEFAULT 256
72 /* Supported devices */
73 static const struct pci_device_id enic_id_table[] = {
74 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
75 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
76 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
77 { 0, } /* end of table */
80 MODULE_DESCRIPTION(DRV_DESCRIPTION);
81 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
82 MODULE_LICENSE("GPL");
83 MODULE_VERSION(DRV_VERSION);
84 MODULE_DEVICE_TABLE(pci, enic_id_table);
86 #define ENIC_LARGE_PKT_THRESHOLD 1000
87 #define ENIC_MAX_COALESCE_TIMERS 10
88 /* Interrupt moderation table, which will be used to decide the
89 * coalescing timer values
90 * {rx_rate in Mbps, mapping percentage of the range}
92 static struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
106 /* This table helps the driver to pick different ranges for rx coalescing
107 * timer depending on the link speed.
109 static struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
110 {0, 0}, /* 0 - 4 Gbps */
111 {0, 3}, /* 4 - 10 Gbps */
112 {3, 6}, /* 10 - 40 Gbps */
115 static void enic_init_affinity_hint(struct enic *enic)
117 int numa_node = dev_to_node(&enic->pdev->dev);
120 for (i = 0; i < enic->intr_count; i++) {
121 if (enic_is_err_intr(enic, i) || enic_is_notify_intr(enic, i) ||
122 (cpumask_available(enic->msix[i].affinity_mask) &&
123 !cpumask_empty(enic->msix[i].affinity_mask)))
125 if (zalloc_cpumask_var(&enic->msix[i].affinity_mask,
127 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
128 enic->msix[i].affinity_mask);
132 static void enic_free_affinity_hint(struct enic *enic)
136 for (i = 0; i < enic->intr_count; i++) {
137 if (enic_is_err_intr(enic, i) || enic_is_notify_intr(enic, i))
139 free_cpumask_var(enic->msix[i].affinity_mask);
143 static void enic_set_affinity_hint(struct enic *enic)
148 for (i = 0; i < enic->intr_count; i++) {
149 if (enic_is_err_intr(enic, i) ||
150 enic_is_notify_intr(enic, i) ||
151 !cpumask_available(enic->msix[i].affinity_mask) ||
152 cpumask_empty(enic->msix[i].affinity_mask))
154 err = irq_set_affinity_hint(enic->msix_entry[i].vector,
155 enic->msix[i].affinity_mask);
157 netdev_warn(enic->netdev, "irq_set_affinity_hint failed, err %d\n",
161 for (i = 0; i < enic->wq_count; i++) {
162 int wq_intr = enic_msix_wq_intr(enic, i);
164 if (cpumask_available(enic->msix[wq_intr].affinity_mask) &&
165 !cpumask_empty(enic->msix[wq_intr].affinity_mask))
166 netif_set_xps_queue(enic->netdev,
167 enic->msix[wq_intr].affinity_mask,
172 static void enic_unset_affinity_hint(struct enic *enic)
176 for (i = 0; i < enic->intr_count; i++)
177 irq_set_affinity_hint(enic->msix_entry[i].vector, NULL);
180 static void enic_udp_tunnel_add(struct net_device *netdev,
181 struct udp_tunnel_info *ti)
183 struct enic *enic = netdev_priv(netdev);
184 __be16 port = ti->port;
187 spin_lock_bh(&enic->devcmd_lock);
189 if (ti->type != UDP_TUNNEL_TYPE_VXLAN) {
190 netdev_info(netdev, "udp_tnl: only vxlan tunnel offload supported");
194 if (ti->sa_family != AF_INET) {
195 netdev_info(netdev, "vxlan: only IPv4 offload supported");
199 if (enic->vxlan.vxlan_udp_port_number) {
200 if (ntohs(port) == enic->vxlan.vxlan_udp_port_number)
201 netdev_warn(netdev, "vxlan: udp port already offloaded");
203 netdev_info(netdev, "vxlan: offload supported for only one UDP port");
208 err = vnic_dev_overlay_offload_cfg(enic->vdev,
209 OVERLAY_CFG_VXLAN_PORT_UPDATE,
214 err = vnic_dev_overlay_offload_ctrl(enic->vdev, OVERLAY_FEATURE_VXLAN,
215 enic->vxlan.patch_level);
219 enic->vxlan.vxlan_udp_port_number = ntohs(port);
221 netdev_info(netdev, "vxlan fw-vers-%d: offload enabled for udp port: %d, sa_family: %d ",
222 (int)enic->vxlan.patch_level, ntohs(port), ti->sa_family);
227 netdev_info(netdev, "failed to offload udp port: %d, sa_family: %d, type: %d",
228 ntohs(port), ti->sa_family, ti->type);
230 spin_unlock_bh(&enic->devcmd_lock);
233 static void enic_udp_tunnel_del(struct net_device *netdev,
234 struct udp_tunnel_info *ti)
236 struct enic *enic = netdev_priv(netdev);
239 spin_lock_bh(&enic->devcmd_lock);
241 if ((ti->sa_family != AF_INET) ||
242 ((ntohs(ti->port) != enic->vxlan.vxlan_udp_port_number)) ||
243 (ti->type != UDP_TUNNEL_TYPE_VXLAN)) {
244 netdev_info(netdev, "udp_tnl: port:%d, sa_family: %d, type: %d not offloaded",
245 ntohs(ti->port), ti->sa_family, ti->type);
249 err = vnic_dev_overlay_offload_ctrl(enic->vdev, OVERLAY_FEATURE_VXLAN,
250 OVERLAY_OFFLOAD_DISABLE);
252 netdev_err(netdev, "vxlan: del offload udp port: %d failed",
257 enic->vxlan.vxlan_udp_port_number = 0;
259 netdev_info(netdev, "vxlan: del offload udp port %d, family %d\n",
260 ntohs(ti->port), ti->sa_family);
263 spin_unlock_bh(&enic->devcmd_lock);
266 static netdev_features_t enic_features_check(struct sk_buff *skb,
267 struct net_device *dev,
268 netdev_features_t features)
270 const struct ethhdr *eth = (struct ethhdr *)skb_inner_mac_header(skb);
271 struct enic *enic = netdev_priv(dev);
276 if (!skb->encapsulation)
279 features = vxlan_features_check(skb, features);
281 /* hardware only supports IPv4 vxlan tunnel */
282 if (vlan_get_protocol(skb) != htons(ETH_P_IP))
285 /* hardware does not support offload of ipv6 inner pkt */
286 if (eth->h_proto != ntohs(ETH_P_IP))
289 proto = ip_hdr(skb)->protocol;
291 if (proto == IPPROTO_UDP) {
293 port = be16_to_cpu(udph->dest);
296 /* HW supports offload of only one UDP port. Remove CSUM and GSO MASK
297 * for other UDP port tunnels
299 if (port != enic->vxlan.vxlan_udp_port_number)
305 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
308 int enic_is_dynamic(struct enic *enic)
310 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
313 int enic_sriov_enabled(struct enic *enic)
315 return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
318 static int enic_is_sriov_vf(struct enic *enic)
320 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
323 int enic_is_valid_vf(struct enic *enic, int vf)
325 #ifdef CONFIG_PCI_IOV
326 return vf >= 0 && vf < enic->num_vfs;
332 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
334 struct enic *enic = vnic_dev_priv(wq->vdev);
337 pci_unmap_single(enic->pdev, buf->dma_addr,
338 buf->len, PCI_DMA_TODEVICE);
340 pci_unmap_page(enic->pdev, buf->dma_addr,
341 buf->len, PCI_DMA_TODEVICE);
344 dev_kfree_skb_any(buf->os_buf);
347 static void enic_wq_free_buf(struct vnic_wq *wq,
348 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
350 enic_free_wq_buf(wq, buf);
353 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
354 u8 type, u16 q_number, u16 completed_index, void *opaque)
356 struct enic *enic = vnic_dev_priv(vdev);
358 spin_lock(&enic->wq_lock[q_number]);
360 vnic_wq_service(&enic->wq[q_number], cq_desc,
361 completed_index, enic_wq_free_buf,
364 if (netif_tx_queue_stopped(netdev_get_tx_queue(enic->netdev, q_number)) &&
365 vnic_wq_desc_avail(&enic->wq[q_number]) >=
366 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
367 netif_wake_subqueue(enic->netdev, q_number);
369 spin_unlock(&enic->wq_lock[q_number]);
374 static bool enic_log_q_error(struct enic *enic)
380 for (i = 0; i < enic->wq_count; i++) {
381 error_status = vnic_wq_error_status(&enic->wq[i]);
384 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
388 for (i = 0; i < enic->rq_count; i++) {
389 error_status = vnic_rq_error_status(&enic->rq[i]);
392 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
399 static void enic_msglvl_check(struct enic *enic)
401 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
403 if (msg_enable != enic->msg_enable) {
404 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
405 enic->msg_enable, msg_enable);
406 enic->msg_enable = msg_enable;
410 static void enic_mtu_check(struct enic *enic)
412 u32 mtu = vnic_dev_mtu(enic->vdev);
413 struct net_device *netdev = enic->netdev;
415 if (mtu && mtu != enic->port_mtu) {
416 enic->port_mtu = mtu;
417 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
418 mtu = max_t(int, ENIC_MIN_MTU,
419 min_t(int, ENIC_MAX_MTU, mtu));
420 if (mtu != netdev->mtu)
421 schedule_work(&enic->change_mtu_work);
423 if (mtu < netdev->mtu)
425 "interface MTU (%d) set higher "
426 "than switch port MTU (%d)\n",
432 static void enic_link_check(struct enic *enic)
434 int link_status = vnic_dev_link_status(enic->vdev);
435 int carrier_ok = netif_carrier_ok(enic->netdev);
437 if (link_status && !carrier_ok) {
438 netdev_info(enic->netdev, "Link UP\n");
439 netif_carrier_on(enic->netdev);
440 } else if (!link_status && carrier_ok) {
441 netdev_info(enic->netdev, "Link DOWN\n");
442 netif_carrier_off(enic->netdev);
446 static void enic_notify_check(struct enic *enic)
448 enic_msglvl_check(enic);
449 enic_mtu_check(enic);
450 enic_link_check(enic);
453 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
455 static irqreturn_t enic_isr_legacy(int irq, void *data)
457 struct net_device *netdev = data;
458 struct enic *enic = netdev_priv(netdev);
459 unsigned int io_intr = enic_legacy_io_intr();
460 unsigned int err_intr = enic_legacy_err_intr();
461 unsigned int notify_intr = enic_legacy_notify_intr();
464 vnic_intr_mask(&enic->intr[io_intr]);
466 pba = vnic_intr_legacy_pba(enic->legacy_pba);
468 vnic_intr_unmask(&enic->intr[io_intr]);
469 return IRQ_NONE; /* not our interrupt */
472 if (ENIC_TEST_INTR(pba, notify_intr)) {
473 enic_notify_check(enic);
474 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
477 if (ENIC_TEST_INTR(pba, err_intr)) {
478 vnic_intr_return_all_credits(&enic->intr[err_intr]);
479 enic_log_q_error(enic);
480 /* schedule recovery from WQ/RQ error */
481 schedule_work(&enic->reset);
485 if (ENIC_TEST_INTR(pba, io_intr))
486 napi_schedule_irqoff(&enic->napi[0]);
488 vnic_intr_unmask(&enic->intr[io_intr]);
493 static irqreturn_t enic_isr_msi(int irq, void *data)
495 struct enic *enic = data;
497 /* With MSI, there is no sharing of interrupts, so this is
498 * our interrupt and there is no need to ack it. The device
499 * is not providing per-vector masking, so the OS will not
500 * write to PCI config space to mask/unmask the interrupt.
501 * We're using mask_on_assertion for MSI, so the device
502 * automatically masks the interrupt when the interrupt is
503 * generated. Later, when exiting polling, the interrupt
504 * will be unmasked (see enic_poll).
506 * Also, the device uses the same PCIe Traffic Class (TC)
507 * for Memory Write data and MSI, so there are no ordering
508 * issues; the MSI will always arrive at the Root Complex
509 * _after_ corresponding Memory Writes (i.e. descriptor
513 napi_schedule_irqoff(&enic->napi[0]);
518 static irqreturn_t enic_isr_msix(int irq, void *data)
520 struct napi_struct *napi = data;
522 napi_schedule_irqoff(napi);
527 static irqreturn_t enic_isr_msix_err(int irq, void *data)
529 struct enic *enic = data;
530 unsigned int intr = enic_msix_err_intr(enic);
532 vnic_intr_return_all_credits(&enic->intr[intr]);
534 if (enic_log_q_error(enic))
535 /* schedule recovery from WQ/RQ error */
536 schedule_work(&enic->reset);
541 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
543 struct enic *enic = data;
544 unsigned int intr = enic_msix_notify_intr(enic);
546 enic_notify_check(enic);
547 vnic_intr_return_all_credits(&enic->intr[intr]);
552 static int enic_queue_wq_skb_cont(struct enic *enic, struct vnic_wq *wq,
553 struct sk_buff *skb, unsigned int len_left,
556 const skb_frag_t *frag;
559 /* Queue additional data fragments */
560 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
561 len_left -= skb_frag_size(frag);
562 dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag, 0,
565 if (unlikely(enic_dma_map_check(enic, dma_addr)))
567 enic_queue_wq_desc_cont(wq, skb, dma_addr, skb_frag_size(frag),
568 (len_left == 0), /* EOP? */
575 static int enic_queue_wq_skb_vlan(struct enic *enic, struct vnic_wq *wq,
576 struct sk_buff *skb, int vlan_tag_insert,
577 unsigned int vlan_tag, int loopback)
579 unsigned int head_len = skb_headlen(skb);
580 unsigned int len_left = skb->len - head_len;
581 int eop = (len_left == 0);
585 dma_addr = pci_map_single(enic->pdev, skb->data, head_len,
587 if (unlikely(enic_dma_map_check(enic, dma_addr)))
590 /* Queue the main skb fragment. The fragments are no larger
591 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
592 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
593 * per fragment is queued.
595 enic_queue_wq_desc(wq, skb, dma_addr, head_len, vlan_tag_insert,
596 vlan_tag, eop, loopback);
599 err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
604 static int enic_queue_wq_skb_csum_l4(struct enic *enic, struct vnic_wq *wq,
605 struct sk_buff *skb, int vlan_tag_insert,
606 unsigned int vlan_tag, int loopback)
608 unsigned int head_len = skb_headlen(skb);
609 unsigned int len_left = skb->len - head_len;
610 unsigned int hdr_len = skb_checksum_start_offset(skb);
611 unsigned int csum_offset = hdr_len + skb->csum_offset;
612 int eop = (len_left == 0);
616 dma_addr = pci_map_single(enic->pdev, skb->data, head_len,
618 if (unlikely(enic_dma_map_check(enic, dma_addr)))
621 /* Queue the main skb fragment. The fragments are no larger
622 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
623 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
624 * per fragment is queued.
626 enic_queue_wq_desc_csum_l4(wq, skb, dma_addr, head_len, csum_offset,
627 hdr_len, vlan_tag_insert, vlan_tag, eop,
631 err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
636 static void enic_preload_tcp_csum_encap(struct sk_buff *skb)
638 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
639 inner_ip_hdr(skb)->check = 0;
640 inner_tcp_hdr(skb)->check =
641 ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
642 inner_ip_hdr(skb)->daddr, 0,
647 static void enic_preload_tcp_csum(struct sk_buff *skb)
649 /* Preload TCP csum field with IP pseudo hdr calculated
650 * with IP length set to zero. HW will later add in length
651 * to each TCP segment resulting from the TSO.
654 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
655 ip_hdr(skb)->check = 0;
656 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
657 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
658 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
659 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
660 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
664 static int enic_queue_wq_skb_tso(struct enic *enic, struct vnic_wq *wq,
665 struct sk_buff *skb, unsigned int mss,
666 int vlan_tag_insert, unsigned int vlan_tag,
669 unsigned int frag_len_left = skb_headlen(skb);
670 unsigned int len_left = skb->len - frag_len_left;
671 int eop = (len_left == 0);
672 unsigned int offset = 0;
673 unsigned int hdr_len;
678 if (skb->encapsulation) {
679 hdr_len = skb_inner_transport_header(skb) - skb->data;
680 hdr_len += inner_tcp_hdrlen(skb);
681 enic_preload_tcp_csum_encap(skb);
683 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
684 enic_preload_tcp_csum(skb);
687 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
688 * for the main skb fragment
690 while (frag_len_left) {
691 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
692 dma_addr = pci_map_single(enic->pdev, skb->data + offset, len,
694 if (unlikely(enic_dma_map_check(enic, dma_addr)))
696 enic_queue_wq_desc_tso(wq, skb, dma_addr, len, mss, hdr_len,
697 vlan_tag_insert, vlan_tag,
698 eop && (len == frag_len_left), loopback);
699 frag_len_left -= len;
706 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
707 * for additional data fragments
709 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
710 len_left -= skb_frag_size(frag);
711 frag_len_left = skb_frag_size(frag);
714 while (frag_len_left) {
715 len = min(frag_len_left,
716 (unsigned int)WQ_ENET_MAX_DESC_LEN);
717 dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
720 if (unlikely(enic_dma_map_check(enic, dma_addr)))
722 enic_queue_wq_desc_cont(wq, skb, dma_addr, len,
724 (len == frag_len_left),/*EOP*/
726 frag_len_left -= len;
734 static inline int enic_queue_wq_skb_encap(struct enic *enic, struct vnic_wq *wq,
737 unsigned int vlan_tag, int loopback)
739 unsigned int head_len = skb_headlen(skb);
740 unsigned int len_left = skb->len - head_len;
741 /* Hardware will overwrite the checksum fields, calculating from
742 * scratch and ignoring the value placed by software.
744 * mss[2], mss[1], mss[0] bits are set
746 unsigned int mss_or_csum = 7;
747 int eop = (len_left == 0);
751 dma_addr = pci_map_single(enic->pdev, skb->data, head_len,
753 if (unlikely(enic_dma_map_check(enic, dma_addr)))
756 enic_queue_wq_desc_ex(wq, skb, dma_addr, head_len, mss_or_csum, 0,
757 vlan_tag_insert, vlan_tag,
758 WQ_ENET_OFFLOAD_MODE_CSUM, eop, 1 /* SOP */, eop,
761 err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
766 static inline void enic_queue_wq_skb(struct enic *enic,
767 struct vnic_wq *wq, struct sk_buff *skb)
769 unsigned int mss = skb_shinfo(skb)->gso_size;
770 unsigned int vlan_tag = 0;
771 int vlan_tag_insert = 0;
775 if (skb_vlan_tag_present(skb)) {
776 /* VLAN tag from trunking driver */
778 vlan_tag = skb_vlan_tag_get(skb);
779 } else if (enic->loop_enable) {
780 vlan_tag = enic->loop_tag;
785 err = enic_queue_wq_skb_tso(enic, wq, skb, mss,
786 vlan_tag_insert, vlan_tag,
788 else if (skb->encapsulation)
789 err = enic_queue_wq_skb_encap(enic, wq, skb, vlan_tag_insert,
791 else if (skb->ip_summed == CHECKSUM_PARTIAL)
792 err = enic_queue_wq_skb_csum_l4(enic, wq, skb, vlan_tag_insert,
795 err = enic_queue_wq_skb_vlan(enic, wq, skb, vlan_tag_insert,
798 struct vnic_wq_buf *buf;
800 buf = wq->to_use->prev;
801 /* while not EOP of previous pkt && queue not empty.
802 * For all non EOP bufs, os_buf is NULL.
804 while (!buf->os_buf && (buf->next != wq->to_clean)) {
805 enic_free_wq_buf(wq, buf);
806 wq->ring.desc_avail++;
809 wq->to_use = buf->next;
814 /* netif_tx_lock held, process context with BHs disabled, or BH */
815 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
816 struct net_device *netdev)
818 struct enic *enic = netdev_priv(netdev);
820 unsigned int txq_map;
821 struct netdev_queue *txq;
824 dev_kfree_skb_any(skb);
828 txq_map = skb_get_queue_mapping(skb) % enic->wq_count;
829 wq = &enic->wq[txq_map];
830 txq = netdev_get_tx_queue(netdev, txq_map);
832 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
833 * which is very likely. In the off chance it's going to take
834 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
837 if (skb_shinfo(skb)->gso_size == 0 &&
838 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
839 skb_linearize(skb)) {
840 dev_kfree_skb_any(skb);
844 spin_lock(&enic->wq_lock[txq_map]);
846 if (vnic_wq_desc_avail(wq) <
847 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
848 netif_tx_stop_queue(txq);
849 /* This is a hard error, log it */
850 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
851 spin_unlock(&enic->wq_lock[txq_map]);
852 return NETDEV_TX_BUSY;
855 enic_queue_wq_skb(enic, wq, skb);
857 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
858 netif_tx_stop_queue(txq);
859 if (!skb->xmit_more || netif_xmit_stopped(txq))
860 vnic_wq_doorbell(wq);
862 spin_unlock(&enic->wq_lock[txq_map]);
867 /* dev_base_lock rwlock held, nominally process context */
868 static void enic_get_stats(struct net_device *netdev,
869 struct rtnl_link_stats64 *net_stats)
871 struct enic *enic = netdev_priv(netdev);
872 struct vnic_stats *stats;
875 err = enic_dev_stats_dump(enic, &stats);
876 /* return only when pci_zalloc_consistent fails in vnic_dev_stats_dump
877 * For other failures, like devcmd failure, we return previously
883 net_stats->tx_packets = stats->tx.tx_frames_ok;
884 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
885 net_stats->tx_errors = stats->tx.tx_errors;
886 net_stats->tx_dropped = stats->tx.tx_drops;
888 net_stats->rx_packets = stats->rx.rx_frames_ok;
889 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
890 net_stats->rx_errors = stats->rx.rx_errors;
891 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
892 net_stats->rx_over_errors = enic->rq_truncated_pkts;
893 net_stats->rx_crc_errors = enic->rq_bad_fcs;
894 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
897 static int enic_mc_sync(struct net_device *netdev, const u8 *mc_addr)
899 struct enic *enic = netdev_priv(netdev);
901 if (enic->mc_count == ENIC_MULTICAST_PERFECT_FILTERS) {
902 unsigned int mc_count = netdev_mc_count(netdev);
904 netdev_warn(netdev, "Registering only %d out of %d multicast addresses\n",
905 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
910 enic_dev_add_addr(enic, mc_addr);
916 static int enic_mc_unsync(struct net_device *netdev, const u8 *mc_addr)
918 struct enic *enic = netdev_priv(netdev);
920 enic_dev_del_addr(enic, mc_addr);
926 static int enic_uc_sync(struct net_device *netdev, const u8 *uc_addr)
928 struct enic *enic = netdev_priv(netdev);
930 if (enic->uc_count == ENIC_UNICAST_PERFECT_FILTERS) {
931 unsigned int uc_count = netdev_uc_count(netdev);
933 netdev_warn(netdev, "Registering only %d out of %d unicast addresses\n",
934 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
939 enic_dev_add_addr(enic, uc_addr);
945 static int enic_uc_unsync(struct net_device *netdev, const u8 *uc_addr)
947 struct enic *enic = netdev_priv(netdev);
949 enic_dev_del_addr(enic, uc_addr);
955 void enic_reset_addr_lists(struct enic *enic)
957 struct net_device *netdev = enic->netdev;
959 __dev_uc_unsync(netdev, NULL);
960 __dev_mc_unsync(netdev, NULL);
967 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
969 struct enic *enic = netdev_priv(netdev);
971 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
972 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
973 return -EADDRNOTAVAIL;
975 if (!is_valid_ether_addr(addr))
976 return -EADDRNOTAVAIL;
979 memcpy(netdev->dev_addr, addr, netdev->addr_len);
984 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
986 struct enic *enic = netdev_priv(netdev);
987 struct sockaddr *saddr = p;
988 char *addr = saddr->sa_data;
991 if (netif_running(enic->netdev)) {
992 err = enic_dev_del_station_addr(enic);
997 err = enic_set_mac_addr(netdev, addr);
1001 if (netif_running(enic->netdev)) {
1002 err = enic_dev_add_station_addr(enic);
1010 static int enic_set_mac_address(struct net_device *netdev, void *p)
1012 struct sockaddr *saddr = p;
1013 char *addr = saddr->sa_data;
1014 struct enic *enic = netdev_priv(netdev);
1017 err = enic_dev_del_station_addr(enic);
1021 err = enic_set_mac_addr(netdev, addr);
1025 return enic_dev_add_station_addr(enic);
1028 /* netif_tx_lock held, BHs disabled */
1029 static void enic_set_rx_mode(struct net_device *netdev)
1031 struct enic *enic = netdev_priv(netdev);
1033 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1034 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1035 int promisc = (netdev->flags & IFF_PROMISC) ||
1036 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1037 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1038 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1039 unsigned int flags = netdev->flags |
1040 (allmulti ? IFF_ALLMULTI : 0) |
1041 (promisc ? IFF_PROMISC : 0);
1043 if (enic->flags != flags) {
1044 enic->flags = flags;
1045 enic_dev_packet_filter(enic, directed,
1046 multicast, broadcast, promisc, allmulti);
1050 __dev_uc_sync(netdev, enic_uc_sync, enic_uc_unsync);
1052 __dev_mc_sync(netdev, enic_mc_sync, enic_mc_unsync);
1056 /* netif_tx_lock held, BHs disabled */
1057 static void enic_tx_timeout(struct net_device *netdev)
1059 struct enic *enic = netdev_priv(netdev);
1060 schedule_work(&enic->tx_hang_reset);
1063 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1065 struct enic *enic = netdev_priv(netdev);
1066 struct enic_port_profile *pp;
1069 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1073 if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
1074 if (vf == PORT_SELF_VF) {
1075 memcpy(pp->vf_mac, mac, ETH_ALEN);
1079 * For sriov vf's set the mac in hw
1081 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
1082 vnic_dev_set_mac_addr, mac);
1083 return enic_dev_status_to_errno(err);
1089 static int enic_set_vf_port(struct net_device *netdev, int vf,
1090 struct nlattr *port[])
1092 struct enic *enic = netdev_priv(netdev);
1093 struct enic_port_profile prev_pp;
1094 struct enic_port_profile *pp;
1095 int err = 0, restore_pp = 1;
1097 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1101 if (!port[IFLA_PORT_REQUEST])
1104 memcpy(&prev_pp, pp, sizeof(*enic->pp));
1105 memset(pp, 0, sizeof(*enic->pp));
1107 pp->set |= ENIC_SET_REQUEST;
1108 pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1110 if (port[IFLA_PORT_PROFILE]) {
1111 pp->set |= ENIC_SET_NAME;
1112 memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
1116 if (port[IFLA_PORT_INSTANCE_UUID]) {
1117 pp->set |= ENIC_SET_INSTANCE;
1118 memcpy(pp->instance_uuid,
1119 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1122 if (port[IFLA_PORT_HOST_UUID]) {
1123 pp->set |= ENIC_SET_HOST;
1124 memcpy(pp->host_uuid,
1125 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1128 if (vf == PORT_SELF_VF) {
1129 /* Special case handling: mac came from IFLA_VF_MAC */
1130 if (!is_zero_ether_addr(prev_pp.vf_mac))
1131 memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
1133 if (is_zero_ether_addr(netdev->dev_addr))
1134 eth_hw_addr_random(netdev);
1136 /* SR-IOV VF: get mac from adapter */
1137 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
1138 vnic_dev_get_mac_addr, pp->mac_addr);
1140 netdev_err(netdev, "Error getting mac for vf %d\n", vf);
1141 memcpy(pp, &prev_pp, sizeof(*pp));
1142 return enic_dev_status_to_errno(err);
1146 err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
1149 /* Things are still the way they were: Implicit
1150 * DISASSOCIATE failed
1152 memcpy(pp, &prev_pp, sizeof(*pp));
1154 memset(pp, 0, sizeof(*pp));
1155 if (vf == PORT_SELF_VF)
1156 eth_zero_addr(netdev->dev_addr);
1159 /* Set flag to indicate that the port assoc/disassoc
1160 * request has been sent out to fw
1162 pp->set |= ENIC_PORT_REQUEST_APPLIED;
1164 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1165 if (pp->request == PORT_REQUEST_DISASSOCIATE) {
1166 eth_zero_addr(pp->mac_addr);
1167 if (vf == PORT_SELF_VF)
1168 eth_zero_addr(netdev->dev_addr);
1172 if (vf == PORT_SELF_VF)
1173 eth_zero_addr(pp->vf_mac);
1178 static int enic_get_vf_port(struct net_device *netdev, int vf,
1179 struct sk_buff *skb)
1181 struct enic *enic = netdev_priv(netdev);
1182 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1183 struct enic_port_profile *pp;
1186 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1190 if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
1193 err = enic_process_get_pp_request(enic, vf, pp->request, &response);
1197 if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
1198 nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
1199 ((pp->set & ENIC_SET_NAME) &&
1200 nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
1201 ((pp->set & ENIC_SET_INSTANCE) &&
1202 nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1203 pp->instance_uuid)) ||
1204 ((pp->set & ENIC_SET_HOST) &&
1205 nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
1206 goto nla_put_failure;
1213 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1215 struct enic *enic = vnic_dev_priv(rq->vdev);
1220 pci_unmap_single(enic->pdev, buf->dma_addr,
1221 buf->len, PCI_DMA_FROMDEVICE);
1222 dev_kfree_skb_any(buf->os_buf);
1226 static int enic_rq_alloc_buf(struct vnic_rq *rq)
1228 struct enic *enic = vnic_dev_priv(rq->vdev);
1229 struct net_device *netdev = enic->netdev;
1230 struct sk_buff *skb;
1231 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
1232 unsigned int os_buf_index = 0;
1233 dma_addr_t dma_addr;
1234 struct vnic_rq_buf *buf = rq->to_use;
1237 enic_queue_rq_desc(rq, buf->os_buf, os_buf_index, buf->dma_addr,
1242 skb = netdev_alloc_skb_ip_align(netdev, len);
1246 dma_addr = pci_map_single(enic->pdev, skb->data, len,
1247 PCI_DMA_FROMDEVICE);
1248 if (unlikely(enic_dma_map_check(enic, dma_addr))) {
1253 enic_queue_rq_desc(rq, skb, os_buf_index,
1259 static void enic_intr_update_pkt_size(struct vnic_rx_bytes_counter *pkt_size,
1262 if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len)
1263 pkt_size->large_pkt_bytes_cnt += pkt_len;
1265 pkt_size->small_pkt_bytes_cnt += pkt_len;
1268 static bool enic_rxcopybreak(struct net_device *netdev, struct sk_buff **skb,
1269 struct vnic_rq_buf *buf, u16 len)
1271 struct enic *enic = netdev_priv(netdev);
1272 struct sk_buff *new_skb;
1274 if (len > enic->rx_copybreak)
1276 new_skb = netdev_alloc_skb_ip_align(netdev, len);
1279 pci_dma_sync_single_for_cpu(enic->pdev, buf->dma_addr, len,
1281 memcpy(new_skb->data, (*skb)->data, len);
1287 static void enic_rq_indicate_buf(struct vnic_rq *rq,
1288 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1289 int skipped, void *opaque)
1291 struct enic *enic = vnic_dev_priv(rq->vdev);
1292 struct net_device *netdev = enic->netdev;
1293 struct sk_buff *skb;
1294 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1296 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1297 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1298 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1299 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1301 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1303 bool outer_csum_ok = true, encap = false;
1310 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1311 &type, &color, &q_number, &completed_index,
1312 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1313 &csum_not_calc, &rss_hash, &bytes_written,
1314 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
1315 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1316 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1317 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1323 if (bytes_written > 0)
1325 else if (bytes_written == 0)
1326 enic->rq_truncated_pkts++;
1329 pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1330 PCI_DMA_FROMDEVICE);
1331 dev_kfree_skb_any(skb);
1337 if (eop && bytes_written > 0) {
1342 if (!enic_rxcopybreak(netdev, &skb, buf, bytes_written)) {
1344 pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1345 PCI_DMA_FROMDEVICE);
1347 prefetch(skb->data - NET_IP_ALIGN);
1349 skb_put(skb, bytes_written);
1350 skb->protocol = eth_type_trans(skb, netdev);
1351 skb_record_rx_queue(skb, q_number);
1352 if ((netdev->features & NETIF_F_RXHASH) && rss_hash &&
1355 case CQ_ENET_RQ_DESC_RSS_TYPE_TCP_IPv4:
1356 case CQ_ENET_RQ_DESC_RSS_TYPE_TCP_IPv6:
1357 case CQ_ENET_RQ_DESC_RSS_TYPE_TCP_IPv6_EX:
1358 skb_set_hash(skb, rss_hash, PKT_HASH_TYPE_L4);
1360 case CQ_ENET_RQ_DESC_RSS_TYPE_IPv4:
1361 case CQ_ENET_RQ_DESC_RSS_TYPE_IPv6:
1362 case CQ_ENET_RQ_DESC_RSS_TYPE_IPv6_EX:
1363 skb_set_hash(skb, rss_hash, PKT_HASH_TYPE_L3);
1367 if (enic->vxlan.vxlan_udp_port_number) {
1368 switch (enic->vxlan.patch_level) {
1372 outer_csum_ok = fcoe_fc_crc_ok;
1377 (rss_hash & BIT(0))) {
1379 outer_csum_ok = (rss_hash & BIT(1)) &&
1380 (rss_hash & BIT(2));
1386 /* Hardware does not provide whole packet checksum. It only
1387 * provides pseudo checksum. Since hw validates the packet
1388 * checksum but not provide us the checksum value. use
1389 * CHECSUM_UNNECESSARY.
1391 * In case of encap pkt tcp_udp_csum_ok/tcp_udp_csum_ok is
1392 * inner csum_ok. outer_csum_ok is set by hw when outer udp
1393 * csum is correct or is zero.
1395 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc &&
1396 tcp_udp_csum_ok && outer_csum_ok &&
1397 (ipv4_csum_ok || ipv6)) {
1398 skb->ip_summed = CHECKSUM_UNNECESSARY;
1399 skb->csum_level = encap;
1403 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
1405 skb_mark_napi_id(skb, &enic->napi[rq->index]);
1406 if (!(netdev->features & NETIF_F_GRO))
1407 netif_receive_skb(skb);
1409 napi_gro_receive(&enic->napi[q_number], skb);
1410 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1411 enic_intr_update_pkt_size(&cq->pkt_size_counter,
1418 pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1419 PCI_DMA_FROMDEVICE);
1420 dev_kfree_skb_any(skb);
1425 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1426 u8 type, u16 q_number, u16 completed_index, void *opaque)
1428 struct enic *enic = vnic_dev_priv(vdev);
1430 vnic_rq_service(&enic->rq[q_number], cq_desc,
1431 completed_index, VNIC_RQ_RETURN_DESC,
1432 enic_rq_indicate_buf, opaque);
1437 static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
1439 unsigned int intr = enic_msix_rq_intr(enic, rq->index);
1440 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1441 u32 timer = cq->tobe_rx_coal_timeval;
1443 if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
1444 vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
1445 cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
1449 static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
1451 struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1452 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1453 struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
1459 ktime_t now = ktime_get();
1461 delta = ktime_us_delta(now, cq->prev_ts);
1462 if (delta < ENIC_AIC_TS_BREAK)
1466 traffic = pkt_size_counter->large_pkt_bytes_cnt +
1467 pkt_size_counter->small_pkt_bytes_cnt;
1468 /* The table takes Mbps
1469 * traffic *= 8 => bits
1470 * traffic *= (10^6 / delta) => bps
1471 * traffic /= 10^6 => Mbps
1473 * Combining, traffic *= (8 / delta)
1477 traffic = delta > UINT_MAX ? 0 : traffic / (u32)delta;
1479 for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
1480 if (traffic < mod_table[index].rx_rate)
1482 range_start = (pkt_size_counter->small_pkt_bytes_cnt >
1483 pkt_size_counter->large_pkt_bytes_cnt << 1) ?
1484 rx_coal->small_pkt_range_start :
1485 rx_coal->large_pkt_range_start;
1486 timer = range_start + ((rx_coal->range_end - range_start) *
1487 mod_table[index].range_percent / 100);
1489 cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
1491 pkt_size_counter->large_pkt_bytes_cnt = 0;
1492 pkt_size_counter->small_pkt_bytes_cnt = 0;
1495 static int enic_poll(struct napi_struct *napi, int budget)
1497 struct net_device *netdev = napi->dev;
1498 struct enic *enic = netdev_priv(netdev);
1499 unsigned int cq_rq = enic_cq_rq(enic, 0);
1500 unsigned int cq_wq = enic_cq_wq(enic, 0);
1501 unsigned int intr = enic_legacy_io_intr();
1502 unsigned int rq_work_to_do = budget;
1503 unsigned int wq_work_to_do = -1; /* no limit */
1504 unsigned int work_done, rq_work_done = 0, wq_work_done;
1507 wq_work_done = vnic_cq_service(&enic->cq[cq_wq], wq_work_to_do,
1508 enic_wq_service, NULL);
1511 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1512 rq_work_to_do, enic_rq_service, NULL);
1514 /* Accumulate intr event credits for this polling
1515 * cycle. An intr event is the completion of a
1516 * a WQ or RQ packet.
1519 work_done = rq_work_done + wq_work_done;
1522 vnic_intr_return_credits(&enic->intr[intr],
1524 0 /* don't unmask intr */,
1525 0 /* don't reset intr timer */);
1527 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1529 /* Buffer allocation failed. Stay in polling
1530 * mode so we can try to fill the ring again.
1534 rq_work_done = rq_work_to_do;
1535 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1536 /* Call the function which refreshes the intr coalescing timer
1537 * value based on the traffic.
1539 enic_calc_int_moderation(enic, &enic->rq[0]);
1541 if ((rq_work_done < budget) && napi_complete_done(napi, rq_work_done)) {
1543 /* Some work done, but not enough to stay in polling,
1547 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1548 enic_set_int_moderation(enic, &enic->rq[0]);
1549 vnic_intr_unmask(&enic->intr[intr]);
1552 return rq_work_done;
1555 #ifdef CONFIG_RFS_ACCEL
1556 static void enic_free_rx_cpu_rmap(struct enic *enic)
1558 free_irq_cpu_rmap(enic->netdev->rx_cpu_rmap);
1559 enic->netdev->rx_cpu_rmap = NULL;
1562 static void enic_set_rx_cpu_rmap(struct enic *enic)
1566 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
1567 enic->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(enic->rq_count);
1568 if (unlikely(!enic->netdev->rx_cpu_rmap))
1570 for (i = 0; i < enic->rq_count; i++) {
1571 res = irq_cpu_rmap_add(enic->netdev->rx_cpu_rmap,
1572 enic->msix_entry[i].vector);
1573 if (unlikely(res)) {
1574 enic_free_rx_cpu_rmap(enic);
1583 static void enic_free_rx_cpu_rmap(struct enic *enic)
1587 static void enic_set_rx_cpu_rmap(struct enic *enic)
1591 #endif /* CONFIG_RFS_ACCEL */
1593 static int enic_poll_msix_wq(struct napi_struct *napi, int budget)
1595 struct net_device *netdev = napi->dev;
1596 struct enic *enic = netdev_priv(netdev);
1597 unsigned int wq_index = (napi - &enic->napi[0]) - enic->rq_count;
1598 struct vnic_wq *wq = &enic->wq[wq_index];
1601 unsigned int wq_work_to_do = -1; /* clean all desc possible */
1602 unsigned int wq_work_done;
1603 unsigned int wq_irq;
1606 cq = enic_cq_wq(enic, wq_irq);
1607 intr = enic_msix_wq_intr(enic, wq_irq);
1608 wq_work_done = vnic_cq_service(&enic->cq[cq], wq_work_to_do,
1609 enic_wq_service, NULL);
1611 vnic_intr_return_credits(&enic->intr[intr], wq_work_done,
1612 0 /* don't unmask intr */,
1613 1 /* reset intr timer */);
1614 if (!wq_work_done) {
1615 napi_complete(napi);
1616 vnic_intr_unmask(&enic->intr[intr]);
1623 static int enic_poll_msix_rq(struct napi_struct *napi, int budget)
1625 struct net_device *netdev = napi->dev;
1626 struct enic *enic = netdev_priv(netdev);
1627 unsigned int rq = (napi - &enic->napi[0]);
1628 unsigned int cq = enic_cq_rq(enic, rq);
1629 unsigned int intr = enic_msix_rq_intr(enic, rq);
1630 unsigned int work_to_do = budget;
1631 unsigned int work_done = 0;
1638 work_done = vnic_cq_service(&enic->cq[cq],
1639 work_to_do, enic_rq_service, NULL);
1641 /* Return intr event credits for this polling
1642 * cycle. An intr event is the completion of a
1647 vnic_intr_return_credits(&enic->intr[intr],
1649 0 /* don't unmask intr */,
1650 0 /* don't reset intr timer */);
1652 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1654 /* Buffer allocation failed. Stay in polling mode
1655 * so we can try to fill the ring again.
1659 work_done = work_to_do;
1660 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1661 /* Call the function which refreshes the intr coalescing timer
1662 * value based on the traffic.
1664 enic_calc_int_moderation(enic, &enic->rq[rq]);
1666 if ((work_done < budget) && napi_complete_done(napi, work_done)) {
1668 /* Some work done, but not enough to stay in polling,
1672 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1673 enic_set_int_moderation(enic, &enic->rq[rq]);
1674 vnic_intr_unmask(&enic->intr[intr]);
1680 static void enic_notify_timer(unsigned long data)
1682 struct enic *enic = (struct enic *)data;
1684 enic_notify_check(enic);
1686 mod_timer(&enic->notify_timer,
1687 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1690 static void enic_free_intr(struct enic *enic)
1692 struct net_device *netdev = enic->netdev;
1695 enic_free_rx_cpu_rmap(enic);
1696 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1697 case VNIC_DEV_INTR_MODE_INTX:
1698 free_irq(enic->pdev->irq, netdev);
1700 case VNIC_DEV_INTR_MODE_MSI:
1701 free_irq(enic->pdev->irq, enic);
1703 case VNIC_DEV_INTR_MODE_MSIX:
1704 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1705 if (enic->msix[i].requested)
1706 free_irq(enic->msix_entry[i].vector,
1707 enic->msix[i].devid);
1714 static int enic_request_intr(struct enic *enic)
1716 struct net_device *netdev = enic->netdev;
1717 unsigned int i, intr;
1720 enic_set_rx_cpu_rmap(enic);
1721 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1723 case VNIC_DEV_INTR_MODE_INTX:
1725 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1726 IRQF_SHARED, netdev->name, netdev);
1729 case VNIC_DEV_INTR_MODE_MSI:
1731 err = request_irq(enic->pdev->irq, enic_isr_msi,
1732 0, netdev->name, enic);
1735 case VNIC_DEV_INTR_MODE_MSIX:
1737 for (i = 0; i < enic->rq_count; i++) {
1738 intr = enic_msix_rq_intr(enic, i);
1739 snprintf(enic->msix[intr].devname,
1740 sizeof(enic->msix[intr].devname),
1741 "%s-rx-%u", netdev->name, i);
1742 enic->msix[intr].isr = enic_isr_msix;
1743 enic->msix[intr].devid = &enic->napi[i];
1746 for (i = 0; i < enic->wq_count; i++) {
1747 int wq = enic_cq_wq(enic, i);
1749 intr = enic_msix_wq_intr(enic, i);
1750 snprintf(enic->msix[intr].devname,
1751 sizeof(enic->msix[intr].devname),
1752 "%s-tx-%u", netdev->name, i);
1753 enic->msix[intr].isr = enic_isr_msix;
1754 enic->msix[intr].devid = &enic->napi[wq];
1757 intr = enic_msix_err_intr(enic);
1758 snprintf(enic->msix[intr].devname,
1759 sizeof(enic->msix[intr].devname),
1760 "%s-err", netdev->name);
1761 enic->msix[intr].isr = enic_isr_msix_err;
1762 enic->msix[intr].devid = enic;
1764 intr = enic_msix_notify_intr(enic);
1765 snprintf(enic->msix[intr].devname,
1766 sizeof(enic->msix[intr].devname),
1767 "%s-notify", netdev->name);
1768 enic->msix[intr].isr = enic_isr_msix_notify;
1769 enic->msix[intr].devid = enic;
1771 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1772 enic->msix[i].requested = 0;
1774 for (i = 0; i < enic->intr_count; i++) {
1775 err = request_irq(enic->msix_entry[i].vector,
1776 enic->msix[i].isr, 0,
1777 enic->msix[i].devname,
1778 enic->msix[i].devid);
1780 enic_free_intr(enic);
1783 enic->msix[i].requested = 1;
1795 static void enic_synchronize_irqs(struct enic *enic)
1799 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1800 case VNIC_DEV_INTR_MODE_INTX:
1801 case VNIC_DEV_INTR_MODE_MSI:
1802 synchronize_irq(enic->pdev->irq);
1804 case VNIC_DEV_INTR_MODE_MSIX:
1805 for (i = 0; i < enic->intr_count; i++)
1806 synchronize_irq(enic->msix_entry[i].vector);
1813 static void enic_set_rx_coal_setting(struct enic *enic)
1817 struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1819 /* 1. Read the link speed from fw
1820 * 2. Pick the default range for the speed
1821 * 3. Update it in enic->rx_coalesce_setting
1823 speed = vnic_dev_port_speed(enic->vdev);
1824 if (ENIC_LINK_SPEED_10G < speed)
1825 index = ENIC_LINK_40G_INDEX;
1826 else if (ENIC_LINK_SPEED_4G < speed)
1827 index = ENIC_LINK_10G_INDEX;
1829 index = ENIC_LINK_4G_INDEX;
1831 rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
1832 rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
1833 rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
1835 /* Start with the value provided by UCSM */
1836 for (index = 0; index < enic->rq_count; index++)
1837 enic->cq[index].cur_rx_coal_timeval =
1838 enic->config.intr_timer_usec;
1840 rx_coal->use_adaptive_rx_coalesce = 1;
1843 static int enic_dev_notify_set(struct enic *enic)
1847 spin_lock_bh(&enic->devcmd_lock);
1848 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1849 case VNIC_DEV_INTR_MODE_INTX:
1850 err = vnic_dev_notify_set(enic->vdev,
1851 enic_legacy_notify_intr());
1853 case VNIC_DEV_INTR_MODE_MSIX:
1854 err = vnic_dev_notify_set(enic->vdev,
1855 enic_msix_notify_intr(enic));
1858 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1861 spin_unlock_bh(&enic->devcmd_lock);
1866 static void enic_notify_timer_start(struct enic *enic)
1868 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1869 case VNIC_DEV_INTR_MODE_MSI:
1870 mod_timer(&enic->notify_timer, jiffies);
1873 /* Using intr for notification for INTx/MSI-X */
1878 /* rtnl lock is held, process context */
1879 static int enic_open(struct net_device *netdev)
1881 struct enic *enic = netdev_priv(netdev);
1885 err = enic_request_intr(enic);
1887 netdev_err(netdev, "Unable to request irq.\n");
1890 enic_init_affinity_hint(enic);
1891 enic_set_affinity_hint(enic);
1893 err = enic_dev_notify_set(enic);
1896 "Failed to alloc notify buffer, aborting.\n");
1897 goto err_out_free_intr;
1900 for (i = 0; i < enic->rq_count; i++) {
1901 /* enable rq before updating rq desc */
1902 vnic_rq_enable(&enic->rq[i]);
1903 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1904 /* Need at least one buffer on ring to get going */
1905 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1906 netdev_err(netdev, "Unable to alloc receive buffers\n");
1908 goto err_out_free_rq;
1912 for (i = 0; i < enic->wq_count; i++)
1913 vnic_wq_enable(&enic->wq[i]);
1915 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1916 enic_dev_add_station_addr(enic);
1918 enic_set_rx_mode(netdev);
1920 netif_tx_wake_all_queues(netdev);
1922 for (i = 0; i < enic->rq_count; i++)
1923 napi_enable(&enic->napi[i]);
1925 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1926 for (i = 0; i < enic->wq_count; i++)
1927 napi_enable(&enic->napi[enic_cq_wq(enic, i)]);
1928 enic_dev_enable(enic);
1930 for (i = 0; i < enic->intr_count; i++)
1931 vnic_intr_unmask(&enic->intr[i]);
1933 enic_notify_timer_start(enic);
1934 enic_rfs_timer_start(enic);
1939 for (i = 0; i < enic->rq_count; i++) {
1940 ret = vnic_rq_disable(&enic->rq[i]);
1942 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1944 enic_dev_notify_unset(enic);
1946 enic_unset_affinity_hint(enic);
1947 enic_free_intr(enic);
1952 /* rtnl lock is held, process context */
1953 static int enic_stop(struct net_device *netdev)
1955 struct enic *enic = netdev_priv(netdev);
1959 for (i = 0; i < enic->intr_count; i++) {
1960 vnic_intr_mask(&enic->intr[i]);
1961 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1964 enic_synchronize_irqs(enic);
1966 del_timer_sync(&enic->notify_timer);
1967 enic_rfs_flw_tbl_free(enic);
1969 enic_dev_disable(enic);
1971 for (i = 0; i < enic->rq_count; i++)
1972 napi_disable(&enic->napi[i]);
1974 netif_carrier_off(netdev);
1975 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1976 for (i = 0; i < enic->wq_count; i++)
1977 napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
1978 netif_tx_disable(netdev);
1980 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1981 enic_dev_del_station_addr(enic);
1983 for (i = 0; i < enic->wq_count; i++) {
1984 err = vnic_wq_disable(&enic->wq[i]);
1988 for (i = 0; i < enic->rq_count; i++) {
1989 err = vnic_rq_disable(&enic->rq[i]);
1994 enic_dev_notify_unset(enic);
1995 enic_unset_affinity_hint(enic);
1996 enic_free_intr(enic);
1998 for (i = 0; i < enic->wq_count; i++)
1999 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
2000 for (i = 0; i < enic->rq_count; i++)
2001 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
2002 for (i = 0; i < enic->cq_count; i++)
2003 vnic_cq_clean(&enic->cq[i]);
2004 for (i = 0; i < enic->intr_count; i++)
2005 vnic_intr_clean(&enic->intr[i]);
2010 static int _enic_change_mtu(struct net_device *netdev, int new_mtu)
2012 bool running = netif_running(netdev);
2017 err = enic_stop(netdev);
2022 netdev->mtu = new_mtu;
2025 err = enic_open(netdev);
2033 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
2035 struct enic *enic = netdev_priv(netdev);
2037 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
2040 if (netdev->mtu > enic->port_mtu)
2042 "interface MTU (%d) set higher than port MTU (%d)\n",
2043 netdev->mtu, enic->port_mtu);
2045 return _enic_change_mtu(netdev, new_mtu);
2048 static void enic_change_mtu_work(struct work_struct *work)
2050 struct enic *enic = container_of(work, struct enic, change_mtu_work);
2051 struct net_device *netdev = enic->netdev;
2052 int new_mtu = vnic_dev_mtu(enic->vdev);
2055 (void)_enic_change_mtu(netdev, new_mtu);
2058 netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
2061 #ifdef CONFIG_NET_POLL_CONTROLLER
2062 static void enic_poll_controller(struct net_device *netdev)
2064 struct enic *enic = netdev_priv(netdev);
2065 struct vnic_dev *vdev = enic->vdev;
2066 unsigned int i, intr;
2068 switch (vnic_dev_get_intr_mode(vdev)) {
2069 case VNIC_DEV_INTR_MODE_MSIX:
2070 for (i = 0; i < enic->rq_count; i++) {
2071 intr = enic_msix_rq_intr(enic, i);
2072 enic_isr_msix(enic->msix_entry[intr].vector,
2076 for (i = 0; i < enic->wq_count; i++) {
2077 intr = enic_msix_wq_intr(enic, i);
2078 enic_isr_msix(enic->msix_entry[intr].vector,
2079 &enic->napi[enic_cq_wq(enic, i)]);
2083 case VNIC_DEV_INTR_MODE_MSI:
2084 enic_isr_msi(enic->pdev->irq, enic);
2086 case VNIC_DEV_INTR_MODE_INTX:
2087 enic_isr_legacy(enic->pdev->irq, netdev);
2095 static int enic_dev_wait(struct vnic_dev *vdev,
2096 int (*start)(struct vnic_dev *, int),
2097 int (*finished)(struct vnic_dev *, int *),
2104 err = start(vdev, arg);
2108 /* Wait for func to complete...2 seconds max
2111 time = jiffies + (HZ * 2);
2114 err = finished(vdev, &done);
2121 schedule_timeout_uninterruptible(HZ / 10);
2123 } while (time_after(time, jiffies));
2128 static int enic_dev_open(struct enic *enic)
2132 err = enic_dev_wait(enic->vdev, vnic_dev_open,
2133 vnic_dev_open_done, 0);
2135 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
2141 static int enic_dev_soft_reset(struct enic *enic)
2145 err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
2146 vnic_dev_soft_reset_done, 0);
2148 netdev_err(enic->netdev, "vNIC soft reset failed, err %d\n",
2154 static int enic_dev_hang_reset(struct enic *enic)
2158 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
2159 vnic_dev_hang_reset_done, 0);
2161 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
2167 int __enic_set_rsskey(struct enic *enic)
2169 union vnic_rss_key *rss_key_buf_va;
2170 dma_addr_t rss_key_buf_pa;
2171 int i, kidx, bidx, err;
2173 rss_key_buf_va = pci_zalloc_consistent(enic->pdev,
2174 sizeof(union vnic_rss_key),
2176 if (!rss_key_buf_va)
2179 for (i = 0; i < ENIC_RSS_LEN; i++) {
2180 kidx = i / ENIC_RSS_BYTES_PER_KEY;
2181 bidx = i % ENIC_RSS_BYTES_PER_KEY;
2182 rss_key_buf_va->key[kidx].b[bidx] = enic->rss_key[i];
2184 spin_lock_bh(&enic->devcmd_lock);
2185 err = enic_set_rss_key(enic,
2187 sizeof(union vnic_rss_key));
2188 spin_unlock_bh(&enic->devcmd_lock);
2190 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
2191 rss_key_buf_va, rss_key_buf_pa);
2196 static int enic_set_rsskey(struct enic *enic)
2198 netdev_rss_key_fill(enic->rss_key, ENIC_RSS_LEN);
2200 return __enic_set_rsskey(enic);
2203 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
2205 dma_addr_t rss_cpu_buf_pa;
2206 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
2210 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
2211 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
2212 if (!rss_cpu_buf_va)
2215 for (i = 0; i < (1 << rss_hash_bits); i++)
2216 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
2218 spin_lock_bh(&enic->devcmd_lock);
2219 err = enic_set_rss_cpu(enic,
2221 sizeof(union vnic_rss_cpu));
2222 spin_unlock_bh(&enic->devcmd_lock);
2224 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
2225 rss_cpu_buf_va, rss_cpu_buf_pa);
2230 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
2231 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
2233 const u8 tso_ipid_split_en = 0;
2234 const u8 ig_vlan_strip_en = 1;
2237 /* Enable VLAN tag stripping.
2240 spin_lock_bh(&enic->devcmd_lock);
2241 err = enic_set_nic_cfg(enic,
2242 rss_default_cpu, rss_hash_type,
2243 rss_hash_bits, rss_base_cpu,
2244 rss_enable, tso_ipid_split_en,
2246 spin_unlock_bh(&enic->devcmd_lock);
2251 static int enic_set_rss_nic_cfg(struct enic *enic)
2253 struct device *dev = enic_get_dev(enic);
2254 const u8 rss_default_cpu = 0;
2255 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
2256 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
2257 NIC_CFG_RSS_HASH_TYPE_IPV6 |
2258 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
2259 const u8 rss_hash_bits = 7;
2260 const u8 rss_base_cpu = 0;
2261 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
2264 if (!enic_set_rsskey(enic)) {
2265 if (enic_set_rsscpu(enic, rss_hash_bits)) {
2267 dev_warn(dev, "RSS disabled, "
2268 "Failed to set RSS cpu indirection table.");
2272 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
2276 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
2277 rss_hash_bits, rss_base_cpu, rss_enable);
2280 static void enic_set_api_busy(struct enic *enic, bool busy)
2282 spin_lock(&enic->enic_api_lock);
2283 enic->enic_api_busy = busy;
2284 spin_unlock(&enic->enic_api_lock);
2287 static void enic_reset(struct work_struct *work)
2289 struct enic *enic = container_of(work, struct enic, reset);
2291 if (!netif_running(enic->netdev))
2296 /* Stop any activity from infiniband */
2297 enic_set_api_busy(enic, true);
2299 enic_stop(enic->netdev);
2300 enic_dev_soft_reset(enic);
2301 enic_reset_addr_lists(enic);
2302 enic_init_vnic_resources(enic);
2303 enic_set_rss_nic_cfg(enic);
2304 enic_dev_set_ig_vlan_rewrite_mode(enic);
2305 enic_open(enic->netdev);
2307 /* Allow infiniband to fiddle with the device again */
2308 enic_set_api_busy(enic, false);
2310 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
2315 static void enic_tx_hang_reset(struct work_struct *work)
2317 struct enic *enic = container_of(work, struct enic, tx_hang_reset);
2321 /* Stop any activity from infiniband */
2322 enic_set_api_busy(enic, true);
2324 enic_dev_hang_notify(enic);
2325 enic_stop(enic->netdev);
2326 enic_dev_hang_reset(enic);
2327 enic_reset_addr_lists(enic);
2328 enic_init_vnic_resources(enic);
2329 enic_set_rss_nic_cfg(enic);
2330 enic_dev_set_ig_vlan_rewrite_mode(enic);
2331 enic_open(enic->netdev);
2333 /* Allow infiniband to fiddle with the device again */
2334 enic_set_api_busy(enic, false);
2336 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
2341 static int enic_set_intr_mode(struct enic *enic)
2343 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
2344 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
2347 /* Set interrupt mode (INTx, MSI, MSI-X) depending
2348 * on system capabilities.
2352 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2353 * (the second to last INTR is used for WQ/RQ errors)
2354 * (the last INTR is used for notifications)
2357 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2358 for (i = 0; i < n + m + 2; i++)
2359 enic->msix_entry[i].entry = i;
2361 /* Use multiple RQs if RSS is enabled
2364 if (ENIC_SETTING(enic, RSS) &&
2365 enic->config.intr_mode < 1 &&
2366 enic->rq_count >= n &&
2367 enic->wq_count >= m &&
2368 enic->cq_count >= n + m &&
2369 enic->intr_count >= n + m + 2) {
2371 if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
2372 n + m + 2, n + m + 2) > 0) {
2376 enic->cq_count = n + m;
2377 enic->intr_count = n + m + 2;
2379 vnic_dev_set_intr_mode(enic->vdev,
2380 VNIC_DEV_INTR_MODE_MSIX);
2386 if (enic->config.intr_mode < 1 &&
2387 enic->rq_count >= 1 &&
2388 enic->wq_count >= m &&
2389 enic->cq_count >= 1 + m &&
2390 enic->intr_count >= 1 + m + 2) {
2391 if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
2392 1 + m + 2, 1 + m + 2) > 0) {
2396 enic->cq_count = 1 + m;
2397 enic->intr_count = 1 + m + 2;
2399 vnic_dev_set_intr_mode(enic->vdev,
2400 VNIC_DEV_INTR_MODE_MSIX);
2408 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2411 if (enic->config.intr_mode < 2 &&
2412 enic->rq_count >= 1 &&
2413 enic->wq_count >= 1 &&
2414 enic->cq_count >= 2 &&
2415 enic->intr_count >= 1 &&
2416 !pci_enable_msi(enic->pdev)) {
2421 enic->intr_count = 1;
2423 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2430 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2431 * (the first INTR is used for WQ/RQ)
2432 * (the second INTR is used for WQ/RQ errors)
2433 * (the last INTR is used for notifications)
2436 if (enic->config.intr_mode < 3 &&
2437 enic->rq_count >= 1 &&
2438 enic->wq_count >= 1 &&
2439 enic->cq_count >= 2 &&
2440 enic->intr_count >= 3) {
2445 enic->intr_count = 3;
2447 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2452 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2457 static void enic_clear_intr_mode(struct enic *enic)
2459 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2460 case VNIC_DEV_INTR_MODE_MSIX:
2461 pci_disable_msix(enic->pdev);
2463 case VNIC_DEV_INTR_MODE_MSI:
2464 pci_disable_msi(enic->pdev);
2470 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2473 static const struct net_device_ops enic_netdev_dynamic_ops = {
2474 .ndo_open = enic_open,
2475 .ndo_stop = enic_stop,
2476 .ndo_start_xmit = enic_hard_start_xmit,
2477 .ndo_get_stats64 = enic_get_stats,
2478 .ndo_validate_addr = eth_validate_addr,
2479 .ndo_set_rx_mode = enic_set_rx_mode,
2480 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2481 .ndo_change_mtu = enic_change_mtu,
2482 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2483 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2484 .ndo_tx_timeout = enic_tx_timeout,
2485 .ndo_set_vf_port = enic_set_vf_port,
2486 .ndo_get_vf_port = enic_get_vf_port,
2487 .ndo_set_vf_mac = enic_set_vf_mac,
2488 #ifdef CONFIG_NET_POLL_CONTROLLER
2489 .ndo_poll_controller = enic_poll_controller,
2491 #ifdef CONFIG_RFS_ACCEL
2492 .ndo_rx_flow_steer = enic_rx_flow_steer,
2494 .ndo_udp_tunnel_add = enic_udp_tunnel_add,
2495 .ndo_udp_tunnel_del = enic_udp_tunnel_del,
2496 .ndo_features_check = enic_features_check,
2499 static const struct net_device_ops enic_netdev_ops = {
2500 .ndo_open = enic_open,
2501 .ndo_stop = enic_stop,
2502 .ndo_start_xmit = enic_hard_start_xmit,
2503 .ndo_get_stats64 = enic_get_stats,
2504 .ndo_validate_addr = eth_validate_addr,
2505 .ndo_set_mac_address = enic_set_mac_address,
2506 .ndo_set_rx_mode = enic_set_rx_mode,
2507 .ndo_change_mtu = enic_change_mtu,
2508 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2509 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2510 .ndo_tx_timeout = enic_tx_timeout,
2511 .ndo_set_vf_port = enic_set_vf_port,
2512 .ndo_get_vf_port = enic_get_vf_port,
2513 .ndo_set_vf_mac = enic_set_vf_mac,
2514 #ifdef CONFIG_NET_POLL_CONTROLLER
2515 .ndo_poll_controller = enic_poll_controller,
2517 #ifdef CONFIG_RFS_ACCEL
2518 .ndo_rx_flow_steer = enic_rx_flow_steer,
2520 .ndo_udp_tunnel_add = enic_udp_tunnel_add,
2521 .ndo_udp_tunnel_del = enic_udp_tunnel_del,
2522 .ndo_features_check = enic_features_check,
2525 static void enic_dev_deinit(struct enic *enic)
2529 for (i = 0; i < enic->rq_count; i++) {
2530 napi_hash_del(&enic->napi[i]);
2531 netif_napi_del(&enic->napi[i]);
2533 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
2534 for (i = 0; i < enic->wq_count; i++)
2535 netif_napi_del(&enic->napi[enic_cq_wq(enic, i)]);
2537 enic_free_vnic_resources(enic);
2538 enic_clear_intr_mode(enic);
2539 enic_free_affinity_hint(enic);
2542 static void enic_kdump_kernel_config(struct enic *enic)
2544 if (is_kdump_kernel()) {
2545 dev_info(enic_get_dev(enic), "Running from within kdump kernel. Using minimal resources\n");
2548 enic->config.rq_desc_count = ENIC_MIN_RQ_DESCS;
2549 enic->config.wq_desc_count = ENIC_MIN_WQ_DESCS;
2550 enic->config.mtu = min_t(u16, 1500, enic->config.mtu);
2554 static int enic_dev_init(struct enic *enic)
2556 struct device *dev = enic_get_dev(enic);
2557 struct net_device *netdev = enic->netdev;
2561 /* Get interrupt coalesce timer info */
2562 err = enic_dev_intr_coal_timer_info(enic);
2564 dev_warn(dev, "Using default conversion factor for "
2565 "interrupt coalesce timer\n");
2566 vnic_dev_intr_coal_timer_info_default(enic->vdev);
2569 /* Get vNIC configuration
2572 err = enic_get_vnic_config(enic);
2574 dev_err(dev, "Get vNIC configuration failed, aborting\n");
2578 /* Get available resource counts
2581 enic_get_res_counts(enic);
2583 /* modify resource count if we are in kdump_kernel
2585 enic_kdump_kernel_config(enic);
2587 /* Set interrupt mode based on resource counts and system
2591 err = enic_set_intr_mode(enic);
2593 dev_err(dev, "Failed to set intr mode based on resource "
2594 "counts and system capabilities, aborting\n");
2598 /* Allocate and configure vNIC resources
2601 err = enic_alloc_vnic_resources(enic);
2603 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2604 goto err_out_free_vnic_resources;
2607 enic_init_vnic_resources(enic);
2609 err = enic_set_rss_nic_cfg(enic);
2611 dev_err(dev, "Failed to config nic, aborting\n");
2612 goto err_out_free_vnic_resources;
2615 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2617 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
2619 case VNIC_DEV_INTR_MODE_MSIX:
2620 for (i = 0; i < enic->rq_count; i++) {
2621 netif_napi_add(netdev, &enic->napi[i],
2622 enic_poll_msix_rq, NAPI_POLL_WEIGHT);
2624 for (i = 0; i < enic->wq_count; i++)
2625 netif_napi_add(netdev, &enic->napi[enic_cq_wq(enic, i)],
2626 enic_poll_msix_wq, NAPI_POLL_WEIGHT);
2632 err_out_free_vnic_resources:
2633 enic_free_affinity_hint(enic);
2634 enic_clear_intr_mode(enic);
2635 enic_free_vnic_resources(enic);
2640 static void enic_iounmap(struct enic *enic)
2644 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2645 if (enic->bar[i].vaddr)
2646 iounmap(enic->bar[i].vaddr);
2649 static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2651 struct device *dev = &pdev->dev;
2652 struct net_device *netdev;
2657 #ifdef CONFIG_PCI_IOV
2662 /* Allocate net device structure and initialize. Private
2663 * instance data is initialized to zero.
2666 netdev = alloc_etherdev_mqs(sizeof(struct enic),
2667 ENIC_RQ_MAX, ENIC_WQ_MAX);
2671 pci_set_drvdata(pdev, netdev);
2673 SET_NETDEV_DEV(netdev, &pdev->dev);
2675 enic = netdev_priv(netdev);
2676 enic->netdev = netdev;
2679 /* Setup PCI resources
2682 err = pci_enable_device_mem(pdev);
2684 dev_err(dev, "Cannot enable PCI device, aborting\n");
2685 goto err_out_free_netdev;
2688 err = pci_request_regions(pdev, DRV_NAME);
2690 dev_err(dev, "Cannot request PCI regions, aborting\n");
2691 goto err_out_disable_device;
2694 pci_set_master(pdev);
2696 /* Query PCI controller on system for DMA addressing
2697 * limitation for the device. Try 47-bit first, and
2701 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(47));
2703 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2705 dev_err(dev, "No usable DMA configuration, aborting\n");
2706 goto err_out_release_regions;
2708 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2710 dev_err(dev, "Unable to obtain %u-bit DMA "
2711 "for consistent allocations, aborting\n", 32);
2712 goto err_out_release_regions;
2715 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(47));
2717 dev_err(dev, "Unable to obtain %u-bit DMA "
2718 "for consistent allocations, aborting\n", 47);
2719 goto err_out_release_regions;
2724 /* Map vNIC resources from BAR0-5
2727 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2728 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2730 enic->bar[i].len = pci_resource_len(pdev, i);
2731 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2732 if (!enic->bar[i].vaddr) {
2733 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2735 goto err_out_iounmap;
2737 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2740 /* Register vNIC device
2743 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2744 ARRAY_SIZE(enic->bar));
2746 dev_err(dev, "vNIC registration failed, aborting\n");
2748 goto err_out_iounmap;
2751 err = vnic_devcmd_init(enic->vdev);
2754 goto err_out_vnic_unregister;
2756 #ifdef CONFIG_PCI_IOV
2757 /* Get number of subvnics */
2758 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
2760 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
2762 if (enic->num_vfs) {
2763 err = pci_enable_sriov(pdev, enic->num_vfs);
2765 dev_err(dev, "SRIOV enable failed, aborting."
2766 " pci_enable_sriov() returned %d\n",
2768 goto err_out_vnic_unregister;
2770 enic->priv_flags |= ENIC_SRIOV_ENABLED;
2771 num_pps = enic->num_vfs;
2776 /* Allocate structure for port profiles */
2777 enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
2780 goto err_out_disable_sriov_pp;
2783 /* Issue device open to get device in known state
2786 err = enic_dev_open(enic);
2788 dev_err(dev, "vNIC dev open failed, aborting\n");
2789 goto err_out_disable_sriov;
2792 /* Setup devcmd lock
2795 spin_lock_init(&enic->devcmd_lock);
2796 spin_lock_init(&enic->enic_api_lock);
2799 * Set ingress vlan rewrite mode before vnic initialization
2802 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2805 "Failed to set ingress vlan rewrite mode, aborting.\n");
2806 goto err_out_dev_close;
2809 /* Issue device init to initialize the vnic-to-switch link.
2810 * We'll start with carrier off and wait for link UP
2811 * notification later to turn on carrier. We don't need
2812 * to wait here for the vnic-to-switch link initialization
2813 * to complete; link UP notification is the indication that
2814 * the process is complete.
2817 netif_carrier_off(netdev);
2819 /* Do not call dev_init for a dynamic vnic.
2820 * For a dynamic vnic, init_prov_info will be
2821 * called later by an upper layer.
2824 if (!enic_is_dynamic(enic)) {
2825 err = vnic_dev_init(enic->vdev, 0);
2827 dev_err(dev, "vNIC dev init failed, aborting\n");
2828 goto err_out_dev_close;
2832 err = enic_dev_init(enic);
2834 dev_err(dev, "Device initialization failed, aborting\n");
2835 goto err_out_dev_close;
2838 netif_set_real_num_tx_queues(netdev, enic->wq_count);
2839 netif_set_real_num_rx_queues(netdev, enic->rq_count);
2841 /* Setup notification timer, HW reset task, and wq locks
2844 init_timer(&enic->notify_timer);
2845 enic->notify_timer.function = enic_notify_timer;
2846 enic->notify_timer.data = (unsigned long)enic;
2848 enic_rfs_flw_tbl_init(enic);
2849 enic_set_rx_coal_setting(enic);
2850 INIT_WORK(&enic->reset, enic_reset);
2851 INIT_WORK(&enic->tx_hang_reset, enic_tx_hang_reset);
2852 INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
2854 for (i = 0; i < enic->wq_count; i++)
2855 spin_lock_init(&enic->wq_lock[i]);
2857 /* Register net device
2860 enic->port_mtu = enic->config.mtu;
2862 err = enic_set_mac_addr(netdev, enic->mac_addr);
2864 dev_err(dev, "Invalid MAC address, aborting\n");
2865 goto err_out_dev_deinit;
2868 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2869 /* rx coalesce time already got initialized. This gets used
2870 * if adaptive coal is turned off
2872 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2874 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
2875 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2877 netdev->netdev_ops = &enic_netdev_ops;
2879 netdev->watchdog_timeo = 2 * HZ;
2880 enic_set_ethtool_ops(netdev);
2882 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2883 if (ENIC_SETTING(enic, LOOP)) {
2884 netdev->features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2885 enic->loop_enable = 1;
2886 enic->loop_tag = enic->config.loop_tag;
2887 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2889 if (ENIC_SETTING(enic, TXCSUM))
2890 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2891 if (ENIC_SETTING(enic, TSO))
2892 netdev->hw_features |= NETIF_F_TSO |
2893 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2894 if (ENIC_SETTING(enic, RSS))
2895 netdev->hw_features |= NETIF_F_RXHASH;
2896 if (ENIC_SETTING(enic, RXCSUM))
2897 netdev->hw_features |= NETIF_F_RXCSUM;
2898 if (ENIC_SETTING(enic, VXLAN)) {
2901 netdev->hw_enc_features |= NETIF_F_RXCSUM |
2904 NETIF_F_GSO_UDP_TUNNEL |
2906 NETIF_F_GSO_UDP_TUNNEL_CSUM;
2907 netdev->hw_features |= netdev->hw_enc_features;
2908 /* get bit mask from hw about supported offload bit level
2909 * BIT(0) = fw supports patch_level 0
2911 * fcoe_fc_crc_ok = outer csum ok
2912 * BIT(1) = always set by fw
2913 * BIT(2) = fw supports patch_level 2
2914 * BIT(0) in rss_hash = encap
2915 * BIT(1,2) in rss_hash = outer_ip_csum_ok/
2917 * used in enic_rq_indicate_buf
2919 err = vnic_dev_get_supported_feature_ver(enic->vdev,
2924 /* mask bits that are supported by driver
2926 patch_level &= BIT_ULL(0) | BIT_ULL(2);
2927 patch_level = fls(patch_level);
2928 patch_level = patch_level ? patch_level - 1 : 0;
2929 enic->vxlan.patch_level = patch_level;
2932 netdev->features |= netdev->hw_features;
2933 netdev->vlan_features |= netdev->features;
2935 #ifdef CONFIG_RFS_ACCEL
2936 netdev->hw_features |= NETIF_F_NTUPLE;
2940 netdev->features |= NETIF_F_HIGHDMA;
2942 netdev->priv_flags |= IFF_UNICAST_FLT;
2944 /* MTU range: 68 - 9000 */
2945 netdev->min_mtu = ENIC_MIN_MTU;
2946 netdev->max_mtu = ENIC_MAX_MTU;
2947 netdev->mtu = enic->port_mtu;
2949 err = register_netdev(netdev);
2951 dev_err(dev, "Cannot register net device, aborting\n");
2952 goto err_out_dev_deinit;
2954 enic->rx_copybreak = RX_COPYBREAK_DEFAULT;
2959 enic_dev_deinit(enic);
2961 vnic_dev_close(enic->vdev);
2962 err_out_disable_sriov:
2964 err_out_disable_sriov_pp:
2965 #ifdef CONFIG_PCI_IOV
2966 if (enic_sriov_enabled(enic)) {
2967 pci_disable_sriov(pdev);
2968 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2971 err_out_vnic_unregister:
2972 vnic_dev_unregister(enic->vdev);
2975 err_out_release_regions:
2976 pci_release_regions(pdev);
2977 err_out_disable_device:
2978 pci_disable_device(pdev);
2979 err_out_free_netdev:
2980 free_netdev(netdev);
2985 static void enic_remove(struct pci_dev *pdev)
2987 struct net_device *netdev = pci_get_drvdata(pdev);
2990 struct enic *enic = netdev_priv(netdev);
2992 cancel_work_sync(&enic->reset);
2993 cancel_work_sync(&enic->change_mtu_work);
2994 unregister_netdev(netdev);
2995 enic_dev_deinit(enic);
2996 vnic_dev_close(enic->vdev);
2997 #ifdef CONFIG_PCI_IOV
2998 if (enic_sriov_enabled(enic)) {
2999 pci_disable_sriov(pdev);
3000 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
3004 vnic_dev_unregister(enic->vdev);
3006 pci_release_regions(pdev);
3007 pci_disable_device(pdev);
3008 free_netdev(netdev);
3012 static struct pci_driver enic_driver = {
3014 .id_table = enic_id_table,
3015 .probe = enic_probe,
3016 .remove = enic_remove,
3019 static int __init enic_init_module(void)
3021 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
3023 return pci_register_driver(&enic_driver);
3026 static void __exit enic_cleanup_module(void)
3028 pci_unregister_driver(&enic_driver);
3031 module_init(enic_init_module);
3032 module_exit(enic_cleanup_module);