2 * Copyright 2008 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/workqueue.h>
27 #include <linux/pci.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/ethtool.h>
35 #include <linux/ipv6.h>
36 #include <linux/tcp.h>
37 #include <net/ip6_checksum.h>
39 #include "cq_enet_desc.h"
41 #include "vnic_intr.h"
42 #include "vnic_stats.h"
46 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
48 /* Supported devices */
49 static struct pci_device_id enic_id_table[] = {
50 { PCI_VDEVICE(CISCO, 0x0043) },
51 { 0, } /* end of table */
54 MODULE_DESCRIPTION(DRV_DESCRIPTION);
55 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
56 MODULE_LICENSE("GPL");
57 MODULE_VERSION(DRV_VERSION);
58 MODULE_DEVICE_TABLE(pci, enic_id_table);
61 char name[ETH_GSTRING_LEN];
65 #define ENIC_TX_STAT(stat) \
66 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
67 #define ENIC_RX_STAT(stat) \
68 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
70 static const struct enic_stat enic_tx_stats[] = {
71 ENIC_TX_STAT(tx_frames_ok),
72 ENIC_TX_STAT(tx_unicast_frames_ok),
73 ENIC_TX_STAT(tx_multicast_frames_ok),
74 ENIC_TX_STAT(tx_broadcast_frames_ok),
75 ENIC_TX_STAT(tx_bytes_ok),
76 ENIC_TX_STAT(tx_unicast_bytes_ok),
77 ENIC_TX_STAT(tx_multicast_bytes_ok),
78 ENIC_TX_STAT(tx_broadcast_bytes_ok),
79 ENIC_TX_STAT(tx_drops),
80 ENIC_TX_STAT(tx_errors),
84 static const struct enic_stat enic_rx_stats[] = {
85 ENIC_RX_STAT(rx_frames_ok),
86 ENIC_RX_STAT(rx_frames_total),
87 ENIC_RX_STAT(rx_unicast_frames_ok),
88 ENIC_RX_STAT(rx_multicast_frames_ok),
89 ENIC_RX_STAT(rx_broadcast_frames_ok),
90 ENIC_RX_STAT(rx_bytes_ok),
91 ENIC_RX_STAT(rx_unicast_bytes_ok),
92 ENIC_RX_STAT(rx_multicast_bytes_ok),
93 ENIC_RX_STAT(rx_broadcast_bytes_ok),
94 ENIC_RX_STAT(rx_drop),
95 ENIC_RX_STAT(rx_no_bufs),
96 ENIC_RX_STAT(rx_errors),
98 ENIC_RX_STAT(rx_crc_errors),
99 ENIC_RX_STAT(rx_frames_64),
100 ENIC_RX_STAT(rx_frames_127),
101 ENIC_RX_STAT(rx_frames_255),
102 ENIC_RX_STAT(rx_frames_511),
103 ENIC_RX_STAT(rx_frames_1023),
104 ENIC_RX_STAT(rx_frames_1518),
105 ENIC_RX_STAT(rx_frames_to_max),
108 static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
109 static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
111 static int enic_get_settings(struct net_device *netdev,
112 struct ethtool_cmd *ecmd)
114 struct enic *enic = netdev_priv(netdev);
116 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
117 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
118 ecmd->port = PORT_FIBRE;
119 ecmd->transceiver = XCVR_EXTERNAL;
121 if (netif_carrier_ok(netdev)) {
122 ecmd->speed = vnic_dev_port_speed(enic->vdev);
123 ecmd->duplex = DUPLEX_FULL;
129 ecmd->autoneg = AUTONEG_DISABLE;
134 static void enic_get_drvinfo(struct net_device *netdev,
135 struct ethtool_drvinfo *drvinfo)
137 struct enic *enic = netdev_priv(netdev);
138 struct vnic_devcmd_fw_info *fw_info;
140 spin_lock(&enic->devcmd_lock);
141 vnic_dev_fw_info(enic->vdev, &fw_info);
142 spin_unlock(&enic->devcmd_lock);
144 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
145 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
146 strncpy(drvinfo->fw_version, fw_info->fw_version,
147 sizeof(drvinfo->fw_version));
148 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
149 sizeof(drvinfo->bus_info));
152 static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
158 for (i = 0; i < enic_n_tx_stats; i++) {
159 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
160 data += ETH_GSTRING_LEN;
162 for (i = 0; i < enic_n_rx_stats; i++) {
163 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
164 data += ETH_GSTRING_LEN;
170 static int enic_get_sset_count(struct net_device *netdev, int sset)
174 return enic_n_tx_stats + enic_n_rx_stats;
180 static void enic_get_ethtool_stats(struct net_device *netdev,
181 struct ethtool_stats *stats, u64 *data)
183 struct enic *enic = netdev_priv(netdev);
184 struct vnic_stats *vstats;
187 spin_lock(&enic->devcmd_lock);
188 vnic_dev_stats_dump(enic->vdev, &vstats);
189 spin_unlock(&enic->devcmd_lock);
191 for (i = 0; i < enic_n_tx_stats; i++)
192 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
193 for (i = 0; i < enic_n_rx_stats; i++)
194 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
197 static u32 enic_get_rx_csum(struct net_device *netdev)
199 struct enic *enic = netdev_priv(netdev);
200 return enic->csum_rx_enabled;
203 static int enic_set_rx_csum(struct net_device *netdev, u32 data)
205 struct enic *enic = netdev_priv(netdev);
207 if (data && !ENIC_SETTING(enic, RXCSUM))
210 enic->csum_rx_enabled = !!data;
215 static int enic_set_tx_csum(struct net_device *netdev, u32 data)
217 struct enic *enic = netdev_priv(netdev);
219 if (data && !ENIC_SETTING(enic, TXCSUM))
223 netdev->features |= NETIF_F_HW_CSUM;
225 netdev->features &= ~NETIF_F_HW_CSUM;
230 static int enic_set_tso(struct net_device *netdev, u32 data)
232 struct enic *enic = netdev_priv(netdev);
234 if (data && !ENIC_SETTING(enic, TSO))
239 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
242 ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
247 static u32 enic_get_msglevel(struct net_device *netdev)
249 struct enic *enic = netdev_priv(netdev);
250 return enic->msg_enable;
253 static void enic_set_msglevel(struct net_device *netdev, u32 value)
255 struct enic *enic = netdev_priv(netdev);
256 enic->msg_enable = value;
259 static struct ethtool_ops enic_ethtool_ops = {
260 .get_settings = enic_get_settings,
261 .get_drvinfo = enic_get_drvinfo,
262 .get_msglevel = enic_get_msglevel,
263 .set_msglevel = enic_set_msglevel,
264 .get_link = ethtool_op_get_link,
265 .get_strings = enic_get_strings,
266 .get_sset_count = enic_get_sset_count,
267 .get_ethtool_stats = enic_get_ethtool_stats,
268 .get_rx_csum = enic_get_rx_csum,
269 .set_rx_csum = enic_set_rx_csum,
270 .get_tx_csum = ethtool_op_get_tx_csum,
271 .set_tx_csum = enic_set_tx_csum,
272 .get_sg = ethtool_op_get_sg,
273 .set_sg = ethtool_op_set_sg,
274 .get_tso = ethtool_op_get_tso,
275 .set_tso = enic_set_tso,
278 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
280 struct enic *enic = vnic_dev_priv(wq->vdev);
283 pci_unmap_single(enic->pdev, buf->dma_addr,
284 buf->len, PCI_DMA_TODEVICE);
286 pci_unmap_page(enic->pdev, buf->dma_addr,
287 buf->len, PCI_DMA_TODEVICE);
290 dev_kfree_skb_any(buf->os_buf);
293 static void enic_wq_free_buf(struct vnic_wq *wq,
294 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
296 enic_free_wq_buf(wq, buf);
299 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
300 u8 type, u16 q_number, u16 completed_index, void *opaque)
302 struct enic *enic = vnic_dev_priv(vdev);
304 spin_lock(&enic->wq_lock[q_number]);
306 vnic_wq_service(&enic->wq[q_number], cq_desc,
307 completed_index, enic_wq_free_buf,
310 if (netif_queue_stopped(enic->netdev) &&
311 vnic_wq_desc_avail(&enic->wq[q_number]) >= MAX_SKB_FRAGS + 1)
312 netif_wake_queue(enic->netdev);
314 spin_unlock(&enic->wq_lock[q_number]);
319 static void enic_log_q_error(struct enic *enic)
324 for (i = 0; i < enic->wq_count; i++) {
325 error_status = vnic_wq_error_status(&enic->wq[i]);
327 printk(KERN_ERR PFX "%s: WQ[%d] error_status %d\n",
328 enic->netdev->name, i, error_status);
331 for (i = 0; i < enic->rq_count; i++) {
332 error_status = vnic_rq_error_status(&enic->rq[i]);
334 printk(KERN_ERR PFX "%s: RQ[%d] error_status %d\n",
335 enic->netdev->name, i, error_status);
339 static void enic_link_check(struct enic *enic)
341 int link_status = vnic_dev_link_status(enic->vdev);
342 int carrier_ok = netif_carrier_ok(enic->netdev);
344 if (link_status && !carrier_ok) {
345 printk(KERN_INFO PFX "%s: Link UP\n", enic->netdev->name);
346 netif_carrier_on(enic->netdev);
347 } else if (!link_status && carrier_ok) {
348 printk(KERN_INFO PFX "%s: Link DOWN\n", enic->netdev->name);
349 netif_carrier_off(enic->netdev);
353 static void enic_mtu_check(struct enic *enic)
355 u32 mtu = vnic_dev_mtu(enic->vdev);
357 if (mtu != enic->port_mtu) {
358 if (mtu < enic->netdev->mtu)
359 printk(KERN_WARNING PFX
360 "%s: interface MTU (%d) set higher "
361 "than switch port MTU (%d)\n",
362 enic->netdev->name, enic->netdev->mtu, mtu);
363 enic->port_mtu = mtu;
367 static void enic_msglvl_check(struct enic *enic)
369 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
371 if (msg_enable != enic->msg_enable) {
372 printk(KERN_INFO PFX "%s: msg lvl changed from 0x%x to 0x%x\n",
373 enic->netdev->name, enic->msg_enable, msg_enable);
374 enic->msg_enable = msg_enable;
378 static void enic_notify_check(struct enic *enic)
380 enic_msglvl_check(enic);
381 enic_mtu_check(enic);
382 enic_link_check(enic);
385 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
387 static irqreturn_t enic_isr_legacy(int irq, void *data)
389 struct net_device *netdev = data;
390 struct enic *enic = netdev_priv(netdev);
393 vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]);
395 pba = vnic_intr_legacy_pba(enic->legacy_pba);
397 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
398 return IRQ_NONE; /* not our interrupt */
401 if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY))
402 enic_notify_check(enic);
404 if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) {
405 enic_log_q_error(enic);
406 /* schedule recovery from WQ/RQ error */
407 schedule_work(&enic->reset);
411 if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) {
412 if (netif_rx_schedule_prep(netdev, &enic->napi))
413 __netif_rx_schedule(netdev, &enic->napi);
415 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
421 static irqreturn_t enic_isr_msi(int irq, void *data)
423 struct enic *enic = data;
425 /* With MSI, there is no sharing of interrupts, so this is
426 * our interrupt and there is no need to ack it. The device
427 * is not providing per-vector masking, so the OS will not
428 * write to PCI config space to mask/unmask the interrupt.
429 * We're using mask_on_assertion for MSI, so the device
430 * automatically masks the interrupt when the interrupt is
431 * generated. Later, when exiting polling, the interrupt
432 * will be unmasked (see enic_poll).
434 * Also, the device uses the same PCIe Traffic Class (TC)
435 * for Memory Write data and MSI, so there are no ordering
436 * issues; the MSI will always arrive at the Root Complex
437 * _after_ corresponding Memory Writes (i.e. descriptor
441 netif_rx_schedule(enic->netdev, &enic->napi);
446 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
448 struct enic *enic = data;
450 /* schedule NAPI polling for RQ cleanup */
451 netif_rx_schedule(enic->netdev, &enic->napi);
456 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
458 struct enic *enic = data;
459 unsigned int wq_work_to_do = -1; /* no limit */
460 unsigned int wq_work_done;
462 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
463 wq_work_to_do, enic_wq_service, NULL);
465 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ],
468 1 /* reset intr timer */);
473 static irqreturn_t enic_isr_msix_err(int irq, void *data)
475 struct enic *enic = data;
477 enic_log_q_error(enic);
479 /* schedule recovery from WQ/RQ error */
480 schedule_work(&enic->reset);
485 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
487 struct enic *enic = data;
489 enic_notify_check(enic);
490 vnic_intr_unmask(&enic->intr[ENIC_MSIX_NOTIFY]);
495 static inline void enic_queue_wq_skb_cont(struct enic *enic,
496 struct vnic_wq *wq, struct sk_buff *skb,
497 unsigned int len_left)
501 /* Queue additional data fragments */
502 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
503 len_left -= frag->size;
504 enic_queue_wq_desc_cont(wq, skb,
505 pci_map_page(enic->pdev, frag->page,
506 frag->page_offset, frag->size,
509 (len_left == 0)); /* EOP? */
513 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
514 struct vnic_wq *wq, struct sk_buff *skb,
515 int vlan_tag_insert, unsigned int vlan_tag)
517 unsigned int head_len = skb_headlen(skb);
518 unsigned int len_left = skb->len - head_len;
519 int eop = (len_left == 0);
521 /* Queue the main skb fragment */
522 enic_queue_wq_desc(wq, skb,
523 pci_map_single(enic->pdev, skb->data,
524 head_len, PCI_DMA_TODEVICE),
526 vlan_tag_insert, vlan_tag,
530 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
533 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
534 struct vnic_wq *wq, struct sk_buff *skb,
535 int vlan_tag_insert, unsigned int vlan_tag)
537 unsigned int head_len = skb_headlen(skb);
538 unsigned int len_left = skb->len - head_len;
539 unsigned int hdr_len = skb_transport_offset(skb);
540 unsigned int csum_offset = hdr_len + skb->csum_offset;
541 int eop = (len_left == 0);
543 /* Queue the main skb fragment */
544 enic_queue_wq_desc_csum_l4(wq, skb,
545 pci_map_single(enic->pdev, skb->data,
546 head_len, PCI_DMA_TODEVICE),
550 vlan_tag_insert, vlan_tag,
554 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
557 static inline void enic_queue_wq_skb_tso(struct enic *enic,
558 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
559 int vlan_tag_insert, unsigned int vlan_tag)
561 unsigned int head_len = skb_headlen(skb);
562 unsigned int len_left = skb->len - head_len;
563 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
564 int eop = (len_left == 0);
566 /* Preload TCP csum field with IP pseudo hdr calculated
567 * with IP length set to zero. HW will later add in length
568 * to each TCP segment resulting from the TSO.
571 if (skb->protocol == __constant_htons(ETH_P_IP)) {
572 ip_hdr(skb)->check = 0;
573 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
574 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
575 } else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {
576 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
577 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
580 /* Queue the main skb fragment */
581 enic_queue_wq_desc_tso(wq, skb,
582 pci_map_single(enic->pdev, skb->data,
583 head_len, PCI_DMA_TODEVICE),
586 vlan_tag_insert, vlan_tag,
590 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
593 static inline void enic_queue_wq_skb(struct enic *enic,
594 struct vnic_wq *wq, struct sk_buff *skb)
596 unsigned int mss = skb_shinfo(skb)->gso_size;
597 unsigned int vlan_tag = 0;
598 int vlan_tag_insert = 0;
600 if (enic->vlan_group && vlan_tx_tag_present(skb)) {
601 /* VLAN tag from trunking driver */
603 vlan_tag = vlan_tx_tag_get(skb);
607 enic_queue_wq_skb_tso(enic, wq, skb, mss,
608 vlan_tag_insert, vlan_tag);
609 else if (skb->ip_summed == CHECKSUM_PARTIAL)
610 enic_queue_wq_skb_csum_l4(enic, wq, skb,
611 vlan_tag_insert, vlan_tag);
613 enic_queue_wq_skb_vlan(enic, wq, skb,
614 vlan_tag_insert, vlan_tag);
617 /* netif_tx_lock held, process context with BHs disabled */
618 static int enic_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
620 struct enic *enic = netdev_priv(netdev);
621 struct vnic_wq *wq = &enic->wq[0];
629 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
630 * which is very likely. In the off chance it's going to take
631 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
634 if (skb_shinfo(skb)->gso_size == 0 &&
635 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
636 skb_linearize(skb)) {
641 spin_lock_irqsave(&enic->wq_lock[0], flags);
643 if (vnic_wq_desc_avail(wq) < skb_shinfo(skb)->nr_frags + 1) {
644 netif_stop_queue(netdev);
645 /* This is a hard error, log it */
646 printk(KERN_ERR PFX "%s: BUG! Tx ring full when "
647 "queue awake!\n", netdev->name);
648 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
649 return NETDEV_TX_BUSY;
652 enic_queue_wq_skb(enic, wq, skb);
654 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + 1)
655 netif_stop_queue(netdev);
657 netdev->trans_start = jiffies;
659 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
664 /* dev_base_lock rwlock held, nominally process context */
665 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
667 struct enic *enic = netdev_priv(netdev);
668 struct net_device_stats *net_stats = &netdev->stats;
669 struct vnic_stats *stats;
671 spin_lock(&enic->devcmd_lock);
672 vnic_dev_stats_dump(enic->vdev, &stats);
673 spin_unlock(&enic->devcmd_lock);
675 net_stats->tx_packets = stats->tx.tx_frames_ok;
676 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
677 net_stats->tx_errors = stats->tx.tx_errors;
678 net_stats->tx_dropped = stats->tx.tx_drops;
680 net_stats->rx_packets = stats->rx.rx_frames_ok;
681 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
682 net_stats->rx_errors = stats->rx.rx_errors;
683 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
684 net_stats->rx_crc_errors = stats->rx.rx_crc_errors;
685 net_stats->rx_dropped = stats->rx.rx_no_bufs;
690 static void enic_reset_mcaddrs(struct enic *enic)
695 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
697 if (!is_valid_ether_addr(addr))
698 return -EADDRNOTAVAIL;
700 memcpy(netdev->dev_addr, addr, netdev->addr_len);
705 /* netif_tx_lock held, BHs disabled */
706 static void enic_set_multicast_list(struct net_device *netdev)
708 struct enic *enic = netdev_priv(netdev);
709 struct dev_mc_list *list = netdev->mc_list;
711 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
712 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
713 int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0;
714 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
715 (netdev->mc_count > ENIC_MULTICAST_PERFECT_FILTERS);
716 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
717 unsigned int mc_count = netdev->mc_count;
720 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS)
721 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
723 spin_lock(&enic->devcmd_lock);
725 vnic_dev_packet_filter(enic->vdev, directed,
726 multicast, broadcast, promisc, allmulti);
728 /* Is there an easier way? Trying to minimize to
729 * calls to add/del multicast addrs. We keep the
730 * addrs from the last call in enic->mc_addr and
731 * look for changes to add/del.
734 for (i = 0; list && i < mc_count; i++) {
735 memcpy(mc_addr[i], list->dmi_addr, ETH_ALEN);
739 for (i = 0; i < enic->mc_count; i++) {
740 for (j = 0; j < mc_count; j++)
741 if (compare_ether_addr(enic->mc_addr[i],
745 enic_del_multicast_addr(enic, enic->mc_addr[i]);
748 for (i = 0; i < mc_count; i++) {
749 for (j = 0; j < enic->mc_count; j++)
750 if (compare_ether_addr(mc_addr[i],
751 enic->mc_addr[j]) == 0)
753 if (j == enic->mc_count)
754 enic_add_multicast_addr(enic, mc_addr[i]);
757 /* Save the list to compare against next time
760 for (i = 0; i < mc_count; i++)
761 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
763 enic->mc_count = mc_count;
765 spin_unlock(&enic->devcmd_lock);
768 /* rtnl lock is held */
769 static void enic_vlan_rx_register(struct net_device *netdev,
770 struct vlan_group *vlan_group)
772 struct enic *enic = netdev_priv(netdev);
773 enic->vlan_group = vlan_group;
776 /* rtnl lock is held */
777 static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
779 struct enic *enic = netdev_priv(netdev);
781 spin_lock(&enic->devcmd_lock);
782 enic_add_vlan(enic, vid);
783 spin_unlock(&enic->devcmd_lock);
786 /* rtnl lock is held */
787 static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
789 struct enic *enic = netdev_priv(netdev);
791 spin_lock(&enic->devcmd_lock);
792 enic_del_vlan(enic, vid);
793 spin_unlock(&enic->devcmd_lock);
796 /* netif_tx_lock held, BHs disabled */
797 static void enic_tx_timeout(struct net_device *netdev)
799 struct enic *enic = netdev_priv(netdev);
800 schedule_work(&enic->reset);
803 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
805 struct enic *enic = vnic_dev_priv(rq->vdev);
810 pci_unmap_single(enic->pdev, buf->dma_addr,
811 buf->len, PCI_DMA_FROMDEVICE);
812 dev_kfree_skb_any(buf->os_buf);
815 static inline struct sk_buff *enic_rq_alloc_skb(unsigned int size)
819 skb = dev_alloc_skb(size + NET_IP_ALIGN);
822 skb_reserve(skb, NET_IP_ALIGN);
827 static int enic_rq_alloc_buf(struct vnic_rq *rq)
829 struct enic *enic = vnic_dev_priv(rq->vdev);
831 unsigned int len = enic->netdev->mtu + ETH_HLEN;
832 unsigned int os_buf_index = 0;
835 skb = enic_rq_alloc_skb(len);
839 dma_addr = pci_map_single(enic->pdev, skb->data,
840 len, PCI_DMA_FROMDEVICE);
842 enic_queue_rq_desc(rq, skb, os_buf_index,
848 static int enic_get_skb_header(struct sk_buff *skb, void **iphdr,
849 void **tcph, u64 *hdr_flags, void *priv)
851 struct cq_enet_rq_desc *cq_desc = priv;
855 u8 type, color, eop, sop, ingress_port, vlan_stripped;
856 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
857 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
858 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
860 u16 q_number, completed_index, bytes_written, vlan, checksum;
863 cq_enet_rq_desc_dec(cq_desc,
864 &type, &color, &q_number, &completed_index,
865 &ingress_port, &fcoe, &eop, &sop, &rss_type,
866 &csum_not_calc, &rss_hash, &bytes_written,
867 &packet_error, &vlan_stripped, &vlan, &checksum,
868 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
869 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
870 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
873 if (!(ipv4 && tcp && !ipv4_fragment))
876 skb_reset_network_header(skb);
879 ip_len = ip_hdrlen(skb);
880 skb_set_transport_header(skb, ip_len);
882 /* check if ip header and tcp header are complete */
883 if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
886 *hdr_flags = LRO_IPV4 | LRO_TCP;
887 *tcph = tcp_hdr(skb);
893 static void enic_rq_indicate_buf(struct vnic_rq *rq,
894 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
895 int skipped, void *opaque)
897 struct enic *enic = vnic_dev_priv(rq->vdev);
900 u8 type, color, eop, sop, ingress_port, vlan_stripped;
901 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
902 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
903 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
905 u16 q_number, completed_index, bytes_written, vlan, checksum;
912 prefetch(skb->data - NET_IP_ALIGN);
913 pci_unmap_single(enic->pdev, buf->dma_addr,
914 buf->len, PCI_DMA_FROMDEVICE);
916 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
917 &type, &color, &q_number, &completed_index,
918 &ingress_port, &fcoe, &eop, &sop, &rss_type,
919 &csum_not_calc, &rss_hash, &bytes_written,
920 &packet_error, &vlan_stripped, &vlan, &checksum,
921 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
922 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
923 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
928 if (bytes_written > 0 && !fcs_ok) {
931 "%s: packet error: bad FCS\n",
935 dev_kfree_skb_any(skb);
940 if (eop && bytes_written > 0) {
945 skb_put(skb, bytes_written);
946 skb->protocol = eth_type_trans(skb, enic->netdev);
948 if (enic->csum_rx_enabled && !csum_not_calc) {
949 skb->csum = htons(checksum);
950 skb->ip_summed = CHECKSUM_COMPLETE;
953 skb->dev = enic->netdev;
954 enic->netdev->last_rx = jiffies;
956 if (enic->vlan_group && vlan_stripped) {
958 if (ENIC_SETTING(enic, LRO) && ipv4)
959 lro_vlan_hwaccel_receive_skb(&enic->lro_mgr,
960 skb, enic->vlan_group,
963 vlan_hwaccel_receive_skb(skb,
964 enic->vlan_group, vlan);
968 if (ENIC_SETTING(enic, LRO) && ipv4)
969 lro_receive_skb(&enic->lro_mgr, skb, cq_desc);
971 netif_receive_skb(skb);
980 dev_kfree_skb_any(skb);
984 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
985 u8 type, u16 q_number, u16 completed_index, void *opaque)
987 struct enic *enic = vnic_dev_priv(vdev);
989 vnic_rq_service(&enic->rq[q_number], cq_desc,
990 completed_index, VNIC_RQ_RETURN_DESC,
991 enic_rq_indicate_buf, opaque);
996 static void enic_rq_drop_buf(struct vnic_rq *rq,
997 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
998 int skipped, void *opaque)
1000 struct enic *enic = vnic_dev_priv(rq->vdev);
1001 struct sk_buff *skb = buf->os_buf;
1006 pci_unmap_single(enic->pdev, buf->dma_addr,
1007 buf->len, PCI_DMA_FROMDEVICE);
1009 dev_kfree_skb_any(skb);
1012 static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1013 u8 type, u16 q_number, u16 completed_index, void *opaque)
1015 struct enic *enic = vnic_dev_priv(vdev);
1017 vnic_rq_service(&enic->rq[q_number], cq_desc,
1018 completed_index, VNIC_RQ_RETURN_DESC,
1019 enic_rq_drop_buf, opaque);
1024 static int enic_poll(struct napi_struct *napi, int budget)
1026 struct enic *enic = container_of(napi, struct enic, napi);
1027 struct net_device *netdev = enic->netdev;
1028 unsigned int rq_work_to_do = budget;
1029 unsigned int wq_work_to_do = -1; /* no limit */
1030 unsigned int work_done, rq_work_done, wq_work_done;
1032 /* Service RQ (first) and WQ
1035 rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1036 rq_work_to_do, enic_rq_service, NULL);
1038 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1039 wq_work_to_do, enic_wq_service, NULL);
1041 /* Accumulate intr event credits for this polling
1042 * cycle. An intr event is the completion of a
1043 * a WQ or RQ packet.
1046 work_done = rq_work_done + wq_work_done;
1049 vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
1051 0 /* don't unmask intr */,
1052 0 /* don't reset intr timer */);
1054 if (rq_work_done > 0) {
1059 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1063 /* If no work done, flush all LROs and exit polling
1066 if (ENIC_SETTING(enic, LRO))
1067 lro_flush_all(&enic->lro_mgr);
1069 netif_rx_complete(netdev, napi);
1070 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1073 return rq_work_done;
1076 static int enic_poll_msix(struct napi_struct *napi, int budget)
1078 struct enic *enic = container_of(napi, struct enic, napi);
1079 struct net_device *netdev = enic->netdev;
1080 unsigned int work_to_do = budget;
1081 unsigned int work_done;
1086 work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1087 work_to_do, enic_rq_service, NULL);
1089 if (work_done > 0) {
1094 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1096 /* Accumulate intr event credits for this polling
1097 * cycle. An intr event is the completion of a
1098 * a WQ or RQ packet.
1101 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
1103 0 /* don't unmask intr */,
1104 0 /* don't reset intr timer */);
1107 /* If no work done, flush all LROs and exit polling
1110 if (ENIC_SETTING(enic, LRO))
1111 lro_flush_all(&enic->lro_mgr);
1113 netif_rx_complete(netdev, napi);
1114 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1120 static void enic_notify_timer(unsigned long data)
1122 struct enic *enic = (struct enic *)data;
1124 enic_notify_check(enic);
1126 mod_timer(&enic->notify_timer,
1127 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1130 static void enic_free_intr(struct enic *enic)
1132 struct net_device *netdev = enic->netdev;
1135 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1136 case VNIC_DEV_INTR_MODE_INTX:
1137 free_irq(enic->pdev->irq, netdev);
1139 case VNIC_DEV_INTR_MODE_MSI:
1140 free_irq(enic->pdev->irq, enic);
1142 case VNIC_DEV_INTR_MODE_MSIX:
1143 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1144 if (enic->msix[i].requested)
1145 free_irq(enic->msix_entry[i].vector,
1146 enic->msix[i].devid);
1153 static int enic_request_intr(struct enic *enic)
1155 struct net_device *netdev = enic->netdev;
1159 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1161 case VNIC_DEV_INTR_MODE_INTX:
1163 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1164 IRQF_SHARED, netdev->name, netdev);
1167 case VNIC_DEV_INTR_MODE_MSI:
1169 err = request_irq(enic->pdev->irq, enic_isr_msi,
1170 0, netdev->name, enic);
1173 case VNIC_DEV_INTR_MODE_MSIX:
1175 sprintf(enic->msix[ENIC_MSIX_RQ].devname,
1176 "%.11s-rx-0", netdev->name);
1177 enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
1178 enic->msix[ENIC_MSIX_RQ].devid = enic;
1180 sprintf(enic->msix[ENIC_MSIX_WQ].devname,
1181 "%.11s-tx-0", netdev->name);
1182 enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
1183 enic->msix[ENIC_MSIX_WQ].devid = enic;
1185 sprintf(enic->msix[ENIC_MSIX_ERR].devname,
1186 "%.11s-err", netdev->name);
1187 enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
1188 enic->msix[ENIC_MSIX_ERR].devid = enic;
1190 sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
1191 "%.11s-notify", netdev->name);
1192 enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
1193 enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
1195 for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
1196 err = request_irq(enic->msix_entry[i].vector,
1197 enic->msix[i].isr, 0,
1198 enic->msix[i].devname,
1199 enic->msix[i].devid);
1201 enic_free_intr(enic);
1204 enic->msix[i].requested = 1;
1216 static int enic_notify_set(struct enic *enic)
1220 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1221 case VNIC_DEV_INTR_MODE_INTX:
1222 err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
1224 case VNIC_DEV_INTR_MODE_MSIX:
1225 err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
1228 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1235 static void enic_notify_timer_start(struct enic *enic)
1237 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1238 case VNIC_DEV_INTR_MODE_MSI:
1239 mod_timer(&enic->notify_timer, jiffies);
1242 /* Using intr for notification for INTx/MSI-X */
1247 /* rtnl lock is held, process context */
1248 static int enic_open(struct net_device *netdev)
1250 struct enic *enic = netdev_priv(netdev);
1254 err = enic_request_intr(enic);
1256 printk(KERN_ERR PFX "%s: Unable to request irq.\n",
1261 err = enic_notify_set(enic);
1264 "%s: Failed to alloc notify buffer, aborting.\n",
1266 goto err_out_free_intr;
1269 for (i = 0; i < enic->rq_count; i++) {
1270 err = vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1273 "%s: Unable to alloc receive buffers.\n",
1275 goto err_out_notify_unset;
1279 for (i = 0; i < enic->wq_count; i++)
1280 vnic_wq_enable(&enic->wq[i]);
1281 for (i = 0; i < enic->rq_count; i++)
1282 vnic_rq_enable(&enic->rq[i]);
1284 enic_add_station_addr(enic);
1285 enic_set_multicast_list(netdev);
1287 netif_wake_queue(netdev);
1288 napi_enable(&enic->napi);
1289 vnic_dev_enable(enic->vdev);
1291 for (i = 0; i < enic->intr_count; i++)
1292 vnic_intr_unmask(&enic->intr[i]);
1294 enic_notify_timer_start(enic);
1298 err_out_notify_unset:
1299 vnic_dev_notify_unset(enic->vdev);
1301 enic_free_intr(enic);
1306 /* rtnl lock is held, process context */
1307 static int enic_stop(struct net_device *netdev)
1309 struct enic *enic = netdev_priv(netdev);
1313 del_timer_sync(&enic->notify_timer);
1315 vnic_dev_disable(enic->vdev);
1316 napi_disable(&enic->napi);
1317 netif_stop_queue(netdev);
1319 for (i = 0; i < enic->intr_count; i++)
1320 vnic_intr_mask(&enic->intr[i]);
1322 for (i = 0; i < enic->wq_count; i++) {
1323 err = vnic_wq_disable(&enic->wq[i]);
1327 for (i = 0; i < enic->rq_count; i++) {
1328 err = vnic_rq_disable(&enic->rq[i]);
1333 vnic_dev_notify_unset(enic->vdev);
1334 enic_free_intr(enic);
1336 (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1337 -1, enic_rq_service_drop, NULL);
1338 (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1339 -1, enic_wq_service, NULL);
1341 for (i = 0; i < enic->wq_count; i++)
1342 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1343 for (i = 0; i < enic->rq_count; i++)
1344 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1345 for (i = 0; i < enic->cq_count; i++)
1346 vnic_cq_clean(&enic->cq[i]);
1347 for (i = 0; i < enic->intr_count; i++)
1348 vnic_intr_clean(&enic->intr[i]);
1353 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1355 struct enic *enic = netdev_priv(netdev);
1356 int running = netif_running(netdev);
1358 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1364 netdev->mtu = new_mtu;
1366 if (netdev->mtu > enic->port_mtu)
1367 printk(KERN_WARNING PFX
1368 "%s: interface MTU (%d) set higher "
1369 "than port MTU (%d)\n",
1370 netdev->name, netdev->mtu, enic->port_mtu);
1378 #ifdef CONFIG_NET_POLL_CONTROLLER
1379 static void enic_poll_controller(struct net_device *netdev)
1381 struct enic *enic = netdev_priv(netdev);
1382 struct vnic_dev *vdev = enic->vdev;
1384 switch (vnic_dev_get_intr_mode(vdev)) {
1385 case VNIC_DEV_INTR_MODE_MSIX:
1386 enic_isr_msix_rq(enic->pdev->irq, enic);
1387 enic_isr_msix_wq(enic->pdev->irq, enic);
1389 case VNIC_DEV_INTR_MODE_MSI:
1390 enic_isr_msi(enic->pdev->irq, enic);
1392 case VNIC_DEV_INTR_MODE_INTX:
1393 enic_isr_legacy(enic->pdev->irq, netdev);
1401 static int enic_dev_wait(struct vnic_dev *vdev,
1402 int (*start)(struct vnic_dev *, int),
1403 int (*finished)(struct vnic_dev *, int *),
1410 BUG_ON(in_interrupt());
1412 err = start(vdev, arg);
1416 /* Wait for func to complete...2 seconds max
1419 time = jiffies + (HZ * 2);
1422 err = finished(vdev, &done);
1429 schedule_timeout_uninterruptible(HZ / 10);
1431 } while (time_after(time, jiffies));
1436 static int enic_dev_open(struct enic *enic)
1440 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1441 vnic_dev_open_done, 0);
1444 "vNIC device open failed, err %d.\n", err);
1449 static int enic_dev_soft_reset(struct enic *enic)
1453 err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
1454 vnic_dev_soft_reset_done, 0);
1457 "vNIC soft reset failed, err %d.\n", err);
1462 static void enic_reset(struct work_struct *work)
1464 struct enic *enic = container_of(work, struct enic, reset);
1466 if (!netif_running(enic->netdev))
1471 spin_lock(&enic->devcmd_lock);
1472 vnic_dev_hang_notify(enic->vdev);
1473 spin_unlock(&enic->devcmd_lock);
1475 enic_stop(enic->netdev);
1476 enic_dev_soft_reset(enic);
1477 enic_reset_mcaddrs(enic);
1478 enic_init_vnic_resources(enic);
1479 enic_open(enic->netdev);
1484 static int enic_set_intr_mode(struct enic *enic)
1486 unsigned int n = ARRAY_SIZE(enic->rq);
1487 unsigned int m = ARRAY_SIZE(enic->wq);
1490 /* Set interrupt mode (INTx, MSI, MSI-X) depending
1491 * system capabilities.
1495 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1496 * (the second to last INTR is used for WQ/RQ errors)
1497 * (the last INTR is used for notifications)
1500 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
1501 for (i = 0; i < n + m + 2; i++)
1502 enic->msix_entry[i].entry = i;
1504 if (enic->config.intr_mode < 1 &&
1505 enic->rq_count >= n &&
1506 enic->wq_count >= m &&
1507 enic->cq_count >= n + m &&
1508 enic->intr_count >= n + m + 2 &&
1509 !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
1513 enic->cq_count = n + m;
1514 enic->intr_count = n + m + 2;
1516 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
1523 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
1526 if (enic->config.intr_mode < 2 &&
1527 enic->rq_count >= 1 &&
1528 enic->wq_count >= 1 &&
1529 enic->cq_count >= 2 &&
1530 enic->intr_count >= 1 &&
1531 !pci_enable_msi(enic->pdev)) {
1536 enic->intr_count = 1;
1538 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
1545 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
1546 * (the first INTR is used for WQ/RQ)
1547 * (the second INTR is used for WQ/RQ errors)
1548 * (the last INTR is used for notifications)
1551 if (enic->config.intr_mode < 3 &&
1552 enic->rq_count >= 1 &&
1553 enic->wq_count >= 1 &&
1554 enic->cq_count >= 2 &&
1555 enic->intr_count >= 3) {
1560 enic->intr_count = 3;
1562 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
1567 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1572 static void enic_clear_intr_mode(struct enic *enic)
1574 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1575 case VNIC_DEV_INTR_MODE_MSIX:
1576 pci_disable_msix(enic->pdev);
1578 case VNIC_DEV_INTR_MODE_MSI:
1579 pci_disable_msi(enic->pdev);
1585 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1588 static void enic_iounmap(struct enic *enic)
1590 if (enic->bar0.vaddr)
1591 iounmap(enic->bar0.vaddr);
1594 static int __devinit enic_probe(struct pci_dev *pdev,
1595 const struct pci_device_id *ent)
1597 struct net_device *netdev;
1603 const u8 rss_default_cpu = 0;
1604 const u8 rss_hash_type = 0;
1605 const u8 rss_hash_bits = 0;
1606 const u8 rss_base_cpu = 0;
1607 const u8 rss_enable = 0;
1608 const u8 tso_ipid_split_en = 0;
1609 const u8 ig_vlan_strip_en = 1;
1611 /* Allocate net device structure and initialize. Private
1612 * instance data is initialized to zero.
1615 netdev = alloc_etherdev(sizeof(struct enic));
1617 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
1621 pci_set_drvdata(pdev, netdev);
1623 SET_NETDEV_DEV(netdev, &pdev->dev);
1625 enic = netdev_priv(netdev);
1626 enic->netdev = netdev;
1629 /* Setup PCI resources
1632 err = pci_enable_device(pdev);
1635 "Cannot enable PCI device, aborting.\n");
1636 goto err_out_free_netdev;
1639 err = pci_request_regions(pdev, DRV_NAME);
1642 "Cannot request PCI regions, aborting.\n");
1643 goto err_out_disable_device;
1646 pci_set_master(pdev);
1648 /* Query PCI controller on system for DMA addressing
1649 * limitation for the device. Try 40-bit first, and
1653 err = pci_set_dma_mask(pdev, DMA_40BIT_MASK);
1655 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1658 "No usable DMA configuration, aborting.\n");
1659 goto err_out_release_regions;
1661 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1664 "Unable to obtain 32-bit DMA "
1665 "for consistent allocations, aborting.\n");
1666 goto err_out_release_regions;
1669 err = pci_set_consistent_dma_mask(pdev, DMA_40BIT_MASK);
1672 "Unable to obtain 40-bit DMA "
1673 "for consistent allocations, aborting.\n");
1674 goto err_out_release_regions;
1679 /* Map vNIC resources from BAR0
1682 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1684 "BAR0 not memory-map'able, aborting.\n");
1686 goto err_out_release_regions;
1689 enic->bar0.vaddr = pci_iomap(pdev, 0, enic->bar0.len);
1690 enic->bar0.bus_addr = pci_resource_start(pdev, 0);
1691 enic->bar0.len = pci_resource_len(pdev, 0);
1693 if (!enic->bar0.vaddr) {
1695 "Cannot memory-map BAR0 res hdr, aborting.\n");
1697 goto err_out_release_regions;
1700 /* Register vNIC device
1703 enic->vdev = vnic_dev_register(NULL, enic, pdev, &enic->bar0);
1706 "vNIC registration failed, aborting.\n");
1708 goto err_out_iounmap;
1711 /* Issue device open to get device in known state
1714 err = enic_dev_open(enic);
1717 "vNIC dev open failed, aborting.\n");
1718 goto err_out_vnic_unregister;
1721 /* Issue device init to initialize the vnic-to-switch link.
1722 * We'll start with carrier off and wait for link UP
1723 * notification later to turn on carrier. We don't need
1724 * to wait here for the vnic-to-switch link initialization
1725 * to complete; link UP notification is the indication that
1726 * the process is complete.
1729 netif_carrier_off(netdev);
1731 err = vnic_dev_init(enic->vdev, 0);
1734 "vNIC dev init failed, aborting.\n");
1735 goto err_out_dev_close;
1738 /* Get vNIC configuration
1741 err = enic_get_vnic_config(enic);
1744 "Get vNIC configuration failed, aborting.\n");
1745 goto err_out_dev_close;
1748 /* Get available resource counts
1751 enic_get_res_counts(enic);
1753 /* Set interrupt mode based on resource counts and system
1757 err = enic_set_intr_mode(enic);
1760 "Failed to set intr mode, aborting.\n");
1761 goto err_out_dev_close;
1764 /* Allocate and configure vNIC resources
1767 err = enic_alloc_vnic_resources(enic);
1770 "Failed to alloc vNIC resources, aborting.\n");
1771 goto err_out_free_vnic_resources;
1774 enic_init_vnic_resources(enic);
1776 /* Enable VLAN tag stripping. RSS not enabled (yet).
1779 err = enic_set_nic_cfg(enic,
1780 rss_default_cpu, rss_hash_type,
1781 rss_hash_bits, rss_base_cpu,
1782 rss_enable, tso_ipid_split_en,
1786 "Failed to config nic, aborting.\n");
1787 goto err_out_free_vnic_resources;
1790 /* Setup notification timer, HW reset task, and locks
1793 init_timer(&enic->notify_timer);
1794 enic->notify_timer.function = enic_notify_timer;
1795 enic->notify_timer.data = (unsigned long)enic;
1797 INIT_WORK(&enic->reset, enic_reset);
1799 for (i = 0; i < enic->wq_count; i++)
1800 spin_lock_init(&enic->wq_lock[i]);
1802 spin_lock_init(&enic->devcmd_lock);
1804 /* Register net device
1807 enic->port_mtu = enic->config.mtu;
1808 (void)enic_change_mtu(netdev, enic->port_mtu);
1810 err = enic_set_mac_addr(netdev, enic->mac_addr);
1813 "Invalid MAC address, aborting.\n");
1814 goto err_out_free_vnic_resources;
1817 netdev->open = enic_open;
1818 netdev->stop = enic_stop;
1819 netdev->hard_start_xmit = enic_hard_start_xmit;
1820 netdev->get_stats = enic_get_stats;
1821 netdev->set_multicast_list = enic_set_multicast_list;
1822 netdev->change_mtu = enic_change_mtu;
1823 netdev->vlan_rx_register = enic_vlan_rx_register;
1824 netdev->vlan_rx_add_vid = enic_vlan_rx_add_vid;
1825 netdev->vlan_rx_kill_vid = enic_vlan_rx_kill_vid;
1826 netdev->tx_timeout = enic_tx_timeout;
1827 netdev->watchdog_timeo = 2 * HZ;
1828 netdev->ethtool_ops = &enic_ethtool_ops;
1829 #ifdef CONFIG_NET_POLL_CONTROLLER
1830 netdev->poll_controller = enic_poll_controller;
1833 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1835 netif_napi_add(netdev, &enic->napi, enic_poll, 64);
1837 case VNIC_DEV_INTR_MODE_MSIX:
1838 netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
1842 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1843 if (ENIC_SETTING(enic, TXCSUM))
1844 netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1845 if (ENIC_SETTING(enic, TSO))
1846 netdev->features |= NETIF_F_TSO |
1847 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
1849 netdev->features |= NETIF_F_HIGHDMA;
1852 enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
1854 if (ENIC_SETTING(enic, LRO)) {
1855 enic->lro_mgr.max_aggr = ENIC_LRO_MAX_AGGR;
1856 enic->lro_mgr.max_desc = ENIC_LRO_MAX_DESC;
1857 enic->lro_mgr.lro_arr = enic->lro_desc;
1858 enic->lro_mgr.get_skb_header = enic_get_skb_header;
1859 enic->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1860 enic->lro_mgr.dev = netdev;
1861 enic->lro_mgr.ip_summed = CHECKSUM_COMPLETE;
1862 enic->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1865 err = register_netdev(netdev);
1868 "Cannot register net device, aborting.\n");
1869 goto err_out_free_vnic_resources;
1874 err_out_free_vnic_resources:
1875 enic_free_vnic_resources(enic);
1877 vnic_dev_close(enic->vdev);
1878 err_out_vnic_unregister:
1879 enic_clear_intr_mode(enic);
1880 vnic_dev_unregister(enic->vdev);
1883 err_out_release_regions:
1884 pci_release_regions(pdev);
1885 err_out_disable_device:
1886 pci_disable_device(pdev);
1887 err_out_free_netdev:
1888 pci_set_drvdata(pdev, NULL);
1889 free_netdev(netdev);
1894 static void __devexit enic_remove(struct pci_dev *pdev)
1896 struct net_device *netdev = pci_get_drvdata(pdev);
1899 struct enic *enic = netdev_priv(netdev);
1901 flush_scheduled_work();
1902 unregister_netdev(netdev);
1903 enic_free_vnic_resources(enic);
1904 vnic_dev_close(enic->vdev);
1905 enic_clear_intr_mode(enic);
1906 vnic_dev_unregister(enic->vdev);
1908 pci_release_regions(pdev);
1909 pci_disable_device(pdev);
1910 pci_set_drvdata(pdev, NULL);
1911 free_netdev(netdev);
1915 static struct pci_driver enic_driver = {
1917 .id_table = enic_id_table,
1918 .probe = enic_probe,
1919 .remove = __devexit_p(enic_remove),
1922 static int __init enic_init_module(void)
1924 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1926 return pci_register_driver(&enic_driver);
1929 static void __exit enic_cleanup_module(void)
1931 pci_unregister_driver(&enic_driver);
1934 module_init(enic_init_module);
1935 module_exit(enic_cleanup_module);