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>
38 #include "cq_enet_desc.h"
40 #include "vnic_intr.h"
41 #include "vnic_stats.h"
45 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
46 #define ENIC_JUMBO_FIRST_BUF_SIZE 256
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_stats_count(struct net_device *netdev)
172 return enic_n_tx_stats + enic_n_rx_stats;
175 static void enic_get_ethtool_stats(struct net_device *netdev,
176 struct ethtool_stats *stats, u64 *data)
178 struct enic *enic = netdev_priv(netdev);
179 struct vnic_stats *vstats;
182 spin_lock(&enic->devcmd_lock);
183 vnic_dev_stats_dump(enic->vdev, &vstats);
184 spin_unlock(&enic->devcmd_lock);
186 for (i = 0; i < enic_n_tx_stats; i++)
187 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
188 for (i = 0; i < enic_n_rx_stats; i++)
189 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
192 static u32 enic_get_rx_csum(struct net_device *netdev)
194 struct enic *enic = netdev_priv(netdev);
195 return enic->csum_rx_enabled;
198 static int enic_set_rx_csum(struct net_device *netdev, u32 data)
200 struct enic *enic = netdev_priv(netdev);
202 enic->csum_rx_enabled =
203 (data && ENIC_SETTING(enic, RXCSUM)) ? 1 : 0;
208 static int enic_set_tx_csum(struct net_device *netdev, u32 data)
210 struct enic *enic = netdev_priv(netdev);
212 if (data && ENIC_SETTING(enic, TXCSUM))
213 netdev->features |= NETIF_F_HW_CSUM;
215 netdev->features &= ~NETIF_F_HW_CSUM;
220 static int enic_set_tso(struct net_device *netdev, u32 data)
222 struct enic *enic = netdev_priv(netdev);
224 if (data && ENIC_SETTING(enic, TSO))
226 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
229 ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
234 static u32 enic_get_msglevel(struct net_device *netdev)
236 struct enic *enic = netdev_priv(netdev);
237 return enic->msg_enable;
240 static void enic_set_msglevel(struct net_device *netdev, u32 value)
242 struct enic *enic = netdev_priv(netdev);
243 enic->msg_enable = value;
246 static struct ethtool_ops enic_ethtool_ops = {
247 .get_settings = enic_get_settings,
248 .get_drvinfo = enic_get_drvinfo,
249 .get_msglevel = enic_get_msglevel,
250 .set_msglevel = enic_set_msglevel,
251 .get_link = ethtool_op_get_link,
252 .get_strings = enic_get_strings,
253 .get_stats_count = enic_get_stats_count,
254 .get_ethtool_stats = enic_get_ethtool_stats,
255 .get_rx_csum = enic_get_rx_csum,
256 .set_rx_csum = enic_set_rx_csum,
257 .get_tx_csum = ethtool_op_get_tx_csum,
258 .set_tx_csum = enic_set_tx_csum,
259 .get_sg = ethtool_op_get_sg,
260 .set_sg = ethtool_op_set_sg,
261 .get_tso = ethtool_op_get_tso,
262 .set_tso = enic_set_tso,
265 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
267 struct enic *enic = vnic_dev_priv(wq->vdev);
270 pci_unmap_single(enic->pdev, buf->dma_addr,
271 buf->len, PCI_DMA_TODEVICE);
273 pci_unmap_page(enic->pdev, buf->dma_addr,
274 buf->len, PCI_DMA_TODEVICE);
277 dev_kfree_skb_any(buf->os_buf);
280 static void enic_wq_free_buf(struct vnic_wq *wq,
281 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
283 enic_free_wq_buf(wq, buf);
286 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
287 u8 type, u16 q_number, u16 completed_index, void *opaque)
289 struct enic *enic = vnic_dev_priv(vdev);
291 spin_lock(&enic->wq_lock[q_number]);
293 vnic_wq_service(&enic->wq[q_number], cq_desc,
294 completed_index, enic_wq_free_buf,
297 if (netif_queue_stopped(enic->netdev) &&
298 vnic_wq_desc_avail(&enic->wq[q_number]) >= MAX_SKB_FRAGS + 1)
299 netif_wake_queue(enic->netdev);
301 spin_unlock(&enic->wq_lock[q_number]);
306 static void enic_log_q_error(struct enic *enic)
311 for (i = 0; i < enic->wq_count; i++) {
312 error_status = vnic_wq_error_status(&enic->wq[i]);
314 printk(KERN_ERR PFX "%s: WQ[%d] error_status %d\n",
315 enic->netdev->name, i, error_status);
318 for (i = 0; i < enic->rq_count; i++) {
319 error_status = vnic_rq_error_status(&enic->rq[i]);
321 printk(KERN_ERR PFX "%s: RQ[%d] error_status %d\n",
322 enic->netdev->name, i, error_status);
326 static void enic_link_check(struct enic *enic)
328 int link_status = vnic_dev_link_status(enic->vdev);
329 int carrier_ok = netif_carrier_ok(enic->netdev);
331 if (link_status && !carrier_ok) {
332 printk(KERN_INFO PFX "%s: Link UP\n", enic->netdev->name);
333 netif_carrier_on(enic->netdev);
334 } else if (!link_status && carrier_ok) {
335 printk(KERN_INFO PFX "%s: Link DOWN\n", enic->netdev->name);
336 netif_carrier_off(enic->netdev);
340 static void enic_mtu_check(struct enic *enic)
342 u32 mtu = vnic_dev_mtu(enic->vdev);
344 if (mtu != enic->port_mtu) {
345 if (mtu < enic->netdev->mtu)
346 printk(KERN_WARNING PFX
347 "%s: interface MTU (%d) set higher "
348 "than switch port MTU (%d)\n",
349 enic->netdev->name, enic->netdev->mtu, mtu);
350 enic->port_mtu = mtu;
354 static void enic_msglvl_check(struct enic *enic)
356 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
358 if (msg_enable != enic->msg_enable) {
359 printk(KERN_INFO PFX "%s: msg lvl changed from 0x%x to 0x%x\n",
360 enic->netdev->name, enic->msg_enable, msg_enable);
361 enic->msg_enable = msg_enable;
365 static void enic_notify_check(struct enic *enic)
367 enic_msglvl_check(enic);
368 enic_mtu_check(enic);
369 enic_link_check(enic);
372 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
374 static irqreturn_t enic_isr_legacy(int irq, void *data)
376 struct net_device *netdev = data;
377 struct enic *enic = netdev_priv(netdev);
380 vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]);
382 pba = vnic_intr_legacy_pba(enic->legacy_pba);
384 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
385 return IRQ_NONE; /* not our interrupt */
388 if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY))
389 enic_notify_check(enic);
391 if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) {
392 enic_log_q_error(enic);
393 /* schedule recovery from WQ/RQ error */
394 schedule_work(&enic->reset);
398 if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) {
399 if (netif_rx_schedule_prep(netdev, &enic->napi))
400 __netif_rx_schedule(netdev, &enic->napi);
402 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
408 static irqreturn_t enic_isr_msi(int irq, void *data)
410 struct enic *enic = data;
412 /* With MSI, there is no sharing of interrupts, so this is
413 * our interrupt and there is no need to ack it. The device
414 * is not providing per-vector masking, so the OS will not
415 * write to PCI config space to mask/unmask the interrupt.
416 * We're using mask_on_assertion for MSI, so the device
417 * automatically masks the interrupt when the interrupt is
418 * generated. Later, when exiting polling, the interrupt
419 * will be unmasked (see enic_poll).
421 * Also, the device uses the same PCIe Traffic Class (TC)
422 * for Memory Write data and MSI, so there are no ordering
423 * issues; the MSI will always arrive at the Root Complex
424 * _after_ corresponding Memory Writes (i.e. descriptor
428 netif_rx_schedule(enic->netdev, &enic->napi);
433 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
435 struct enic *enic = data;
437 /* schedule NAPI polling for RQ cleanup */
438 netif_rx_schedule(enic->netdev, &enic->napi);
443 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
445 struct enic *enic = data;
446 unsigned int wq_work_to_do = -1; /* no limit */
447 unsigned int wq_work_done;
449 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
450 wq_work_to_do, enic_wq_service, NULL);
452 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ],
455 1 /* reset intr timer */);
460 static irqreturn_t enic_isr_msix_err(int irq, void *data)
462 struct enic *enic = data;
464 enic_log_q_error(enic);
466 /* schedule recovery from WQ/RQ error */
467 schedule_work(&enic->reset);
472 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
474 struct enic *enic = data;
476 enic_notify_check(enic);
477 vnic_intr_unmask(&enic->intr[ENIC_MSIX_NOTIFY]);
482 static inline void enic_queue_wq_skb_cont(struct enic *enic,
483 struct vnic_wq *wq, struct sk_buff *skb,
484 unsigned int len_left)
488 /* Queue additional data fragments */
489 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
490 len_left -= frag->size;
491 enic_queue_wq_desc_cont(wq, skb,
492 pci_map_page(enic->pdev, frag->page,
493 frag->page_offset, frag->size,
496 (len_left == 0)); /* EOP? */
500 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
501 struct vnic_wq *wq, struct sk_buff *skb,
502 int vlan_tag_insert, unsigned int vlan_tag)
504 unsigned int head_len = skb_headlen(skb);
505 unsigned int len_left = skb->len - head_len;
506 int eop = (len_left == 0);
508 /* Queue the main skb fragment */
509 enic_queue_wq_desc(wq, skb,
510 pci_map_single(enic->pdev, skb->data,
511 head_len, PCI_DMA_TODEVICE),
513 vlan_tag_insert, vlan_tag,
517 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
520 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
521 struct vnic_wq *wq, struct sk_buff *skb,
522 int vlan_tag_insert, unsigned int vlan_tag)
524 unsigned int head_len = skb_headlen(skb);
525 unsigned int len_left = skb->len - head_len;
526 unsigned int hdr_len = skb_transport_offset(skb);
527 unsigned int csum_offset = hdr_len + skb->csum_offset;
528 int eop = (len_left == 0);
530 /* Queue the main skb fragment */
531 enic_queue_wq_desc_csum_l4(wq, skb,
532 pci_map_single(enic->pdev, skb->data,
533 head_len, PCI_DMA_TODEVICE),
537 vlan_tag_insert, vlan_tag,
541 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
544 static inline void enic_queue_wq_skb_tso(struct enic *enic,
545 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
546 int vlan_tag_insert, unsigned int vlan_tag)
548 unsigned int head_len = skb_headlen(skb);
549 unsigned int len_left = skb->len - head_len;
550 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
551 int eop = (len_left == 0);
553 /* Preload TCP csum field with IP pseudo hdr calculated
554 * with IP length set to zero. HW will later add in length
555 * to each TCP segment resulting from the TSO.
558 if (skb->protocol == __constant_htons(ETH_P_IP)) {
559 ip_hdr(skb)->check = 0;
560 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
561 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
562 } else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {
563 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
564 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
567 /* Queue the main skb fragment */
568 enic_queue_wq_desc_tso(wq, skb,
569 pci_map_single(enic->pdev, skb->data,
570 head_len, PCI_DMA_TODEVICE),
573 vlan_tag_insert, vlan_tag,
577 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
580 static inline void enic_queue_wq_skb(struct enic *enic,
581 struct vnic_wq *wq, struct sk_buff *skb)
583 unsigned int mss = skb_shinfo(skb)->gso_size;
584 unsigned int vlan_tag = 0;
585 int vlan_tag_insert = 0;
587 if (enic->vlan_group && vlan_tx_tag_present(skb)) {
588 /* VLAN tag from trunking driver */
590 vlan_tag = vlan_tx_tag_get(skb);
594 enic_queue_wq_skb_tso(enic, wq, skb, mss,
595 vlan_tag_insert, vlan_tag);
596 else if (skb->ip_summed == CHECKSUM_PARTIAL)
597 enic_queue_wq_skb_csum_l4(enic, wq, skb,
598 vlan_tag_insert, vlan_tag);
600 enic_queue_wq_skb_vlan(enic, wq, skb,
601 vlan_tag_insert, vlan_tag);
604 /* netif_tx_lock held, process context with BHs disabled */
605 static int enic_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
607 struct enic *enic = netdev_priv(netdev);
608 struct vnic_wq *wq = &enic->wq[0];
616 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
617 * which is very likely. In the off chance it's going to take
618 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
621 if (skb_shinfo(skb)->gso_size == 0 &&
622 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
623 skb_linearize(skb)) {
628 spin_lock_irqsave(&enic->wq_lock[0], flags);
630 if (vnic_wq_desc_avail(wq) < skb_shinfo(skb)->nr_frags + 1) {
631 netif_stop_queue(netdev);
632 /* This is a hard error, log it */
633 printk(KERN_ERR PFX "%s: BUG! Tx ring full when "
634 "queue awake!\n", netdev->name);
635 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
636 return NETDEV_TX_BUSY;
639 enic_queue_wq_skb(enic, wq, skb);
641 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + 1)
642 netif_stop_queue(netdev);
644 netdev->trans_start = jiffies;
646 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
651 /* dev_base_lock rwlock held, nominally process context */
652 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
654 struct enic *enic = netdev_priv(netdev);
655 struct vnic_stats *stats;
657 spin_lock(&enic->devcmd_lock);
658 vnic_dev_stats_dump(enic->vdev, &stats);
659 spin_unlock(&enic->devcmd_lock);
661 enic->net_stats.tx_packets = stats->tx.tx_frames_ok;
662 enic->net_stats.tx_bytes = stats->tx.tx_bytes_ok;
663 enic->net_stats.tx_errors = stats->tx.tx_errors;
664 enic->net_stats.tx_dropped = stats->tx.tx_drops;
666 enic->net_stats.rx_packets = stats->rx.rx_frames_ok;
667 enic->net_stats.rx_bytes = stats->rx.rx_bytes_ok;
668 enic->net_stats.rx_errors = stats->rx.rx_errors;
669 enic->net_stats.multicast = stats->rx.rx_multicast_frames_ok;
670 enic->net_stats.rx_crc_errors = stats->rx.rx_crc_errors;
671 enic->net_stats.rx_dropped = stats->rx.rx_no_bufs;
673 return &enic->net_stats;
676 static void enic_reset_mcaddrs(struct enic *enic)
681 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
683 if (!is_valid_ether_addr(addr))
684 return -EADDRNOTAVAIL;
686 memcpy(netdev->dev_addr, addr, netdev->addr_len);
691 /* netif_tx_lock held, BHs disabled */
692 static void enic_set_multicast_list(struct net_device *netdev)
694 struct enic *enic = netdev_priv(netdev);
695 struct dev_mc_list *list = netdev->mc_list;
697 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
698 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
699 int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0;
700 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
701 (netdev->mc_count > ENIC_MULTICAST_PERFECT_FILTERS);
702 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
703 unsigned int mc_count = netdev->mc_count;
706 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS)
707 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
709 spin_lock(&enic->devcmd_lock);
711 vnic_dev_packet_filter(enic->vdev, directed,
712 multicast, broadcast, promisc, allmulti);
714 /* Is there an easier way? Trying to minimize to
715 * calls to add/del multicast addrs. We keep the
716 * addrs from the last call in enic->mc_addr and
717 * look for changes to add/del.
720 for (i = 0; list && i < mc_count; i++) {
721 memcpy(mc_addr[i], list->dmi_addr, ETH_ALEN);
725 for (i = 0; i < enic->mc_count; i++) {
726 for (j = 0; j < mc_count; j++)
727 if (compare_ether_addr(enic->mc_addr[i],
731 enic_del_multicast_addr(enic, enic->mc_addr[i]);
734 for (i = 0; i < mc_count; i++) {
735 for (j = 0; j < enic->mc_count; j++)
736 if (compare_ether_addr(mc_addr[i],
737 enic->mc_addr[j]) == 0)
739 if (j == enic->mc_count)
740 enic_add_multicast_addr(enic, mc_addr[i]);
743 /* Save the list to compare against next time
746 for (i = 0; i < mc_count; i++)
747 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
749 enic->mc_count = mc_count;
751 spin_unlock(&enic->devcmd_lock);
754 /* rtnl lock is held */
755 static void enic_vlan_rx_register(struct net_device *netdev,
756 struct vlan_group *vlan_group)
758 struct enic *enic = netdev_priv(netdev);
759 enic->vlan_group = vlan_group;
762 /* rtnl lock is held */
763 static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
765 struct enic *enic = netdev_priv(netdev);
767 spin_lock(&enic->devcmd_lock);
768 enic_add_vlan(enic, vid);
769 spin_unlock(&enic->devcmd_lock);
772 /* rtnl lock is held */
773 static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
775 struct enic *enic = netdev_priv(netdev);
777 spin_lock(&enic->devcmd_lock);
778 enic_del_vlan(enic, vid);
779 spin_unlock(&enic->devcmd_lock);
782 /* netif_tx_lock held, BHs disabled */
783 static void enic_tx_timeout(struct net_device *netdev)
785 struct enic *enic = netdev_priv(netdev);
786 schedule_work(&enic->reset);
789 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
791 struct enic *enic = vnic_dev_priv(rq->vdev);
796 pci_unmap_single(enic->pdev, buf->dma_addr,
797 buf->len, PCI_DMA_FROMDEVICE);
798 dev_kfree_skb_any(buf->os_buf);
801 static inline struct sk_buff *enic_rq_alloc_skb(unsigned int size)
805 skb = dev_alloc_skb(size + NET_IP_ALIGN);
808 skb_reserve(skb, NET_IP_ALIGN);
813 static int enic_rq_alloc_buf(struct vnic_rq *rq)
815 struct enic *enic = vnic_dev_priv(rq->vdev);
817 unsigned int len = enic->netdev->mtu + ETH_HLEN;
818 unsigned int os_buf_index = 0;
821 skb = enic_rq_alloc_skb(len);
825 dma_addr = pci_map_single(enic->pdev, skb->data,
826 len, PCI_DMA_FROMDEVICE);
828 enic_queue_rq_desc(rq, skb, os_buf_index,
834 static int enic_get_skb_header(struct sk_buff *skb, void **iphdr,
835 void **tcph, u64 *hdr_flags, void *priv)
837 struct cq_enet_rq_desc *cq_desc = priv;
841 u8 type, color, eop, sop, ingress_port, vlan_stripped;
842 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
843 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
844 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
846 u16 q_number, completed_index, bytes_written, vlan, checksum;
849 cq_enet_rq_desc_dec(cq_desc,
850 &type, &color, &q_number, &completed_index,
851 &ingress_port, &fcoe, &eop, &sop, &rss_type,
852 &csum_not_calc, &rss_hash, &bytes_written,
853 &packet_error, &vlan_stripped, &vlan, &checksum,
854 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
855 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
856 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
859 if (!(ipv4 && tcp && !ipv4_fragment))
862 skb_reset_network_header(skb);
865 ip_len = ip_hdrlen(skb);
866 skb_set_transport_header(skb, ip_len);
868 /* check if ip header and tcp header are complete */
869 if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
872 *hdr_flags = LRO_IPV4 | LRO_TCP;
873 *tcph = tcp_hdr(skb);
879 static void enic_rq_indicate_buf(struct vnic_rq *rq,
880 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
881 int skipped, void *opaque)
883 struct enic *enic = vnic_dev_priv(rq->vdev);
886 u8 type, color, eop, sop, ingress_port, vlan_stripped;
887 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
888 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
889 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
891 u16 q_number, completed_index, bytes_written, vlan, checksum;
898 prefetch(skb->data - NET_IP_ALIGN);
899 pci_unmap_single(enic->pdev, buf->dma_addr,
900 buf->len, PCI_DMA_FROMDEVICE);
902 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
903 &type, &color, &q_number, &completed_index,
904 &ingress_port, &fcoe, &eop, &sop, &rss_type,
905 &csum_not_calc, &rss_hash, &bytes_written,
906 &packet_error, &vlan_stripped, &vlan, &checksum,
907 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
908 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
909 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
914 if (bytes_written > 0 && !fcs_ok) {
917 "%s: packet error: bad FCS\n",
921 dev_kfree_skb_any(skb);
926 if (eop && bytes_written > 0) {
931 skb_put(skb, bytes_written);
932 skb->protocol = eth_type_trans(skb, enic->netdev);
934 if (enic->csum_rx_enabled && !csum_not_calc) {
935 skb->csum = htons(checksum);
936 skb->ip_summed = CHECKSUM_COMPLETE;
939 skb->dev = enic->netdev;
940 enic->netdev->last_rx = jiffies;
942 if (enic->vlan_group && vlan_stripped) {
944 if (ENIC_SETTING(enic, LRO))
945 lro_vlan_hwaccel_receive_skb(&enic->lro_mgr,
946 skb, enic->vlan_group,
949 vlan_hwaccel_receive_skb(skb,
950 enic->vlan_group, vlan);
954 if (ENIC_SETTING(enic, LRO))
955 lro_receive_skb(&enic->lro_mgr, skb, cq_desc);
957 netif_receive_skb(skb);
966 dev_kfree_skb_any(skb);
970 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
971 u8 type, u16 q_number, u16 completed_index, void *opaque)
973 struct enic *enic = vnic_dev_priv(vdev);
975 vnic_rq_service(&enic->rq[q_number], cq_desc,
976 completed_index, VNIC_RQ_RETURN_DESC,
977 enic_rq_indicate_buf, opaque);
982 static void enic_rq_drop_buf(struct vnic_rq *rq,
983 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
984 int skipped, void *opaque)
986 struct enic *enic = vnic_dev_priv(rq->vdev);
987 struct sk_buff *skb = buf->os_buf;
992 pci_unmap_single(enic->pdev, buf->dma_addr,
993 buf->len, PCI_DMA_FROMDEVICE);
995 dev_kfree_skb_any(skb);
998 static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc,
999 u8 type, u16 q_number, u16 completed_index, void *opaque)
1001 struct enic *enic = vnic_dev_priv(vdev);
1003 vnic_rq_service(&enic->rq[q_number], cq_desc,
1004 completed_index, VNIC_RQ_RETURN_DESC,
1005 enic_rq_drop_buf, opaque);
1010 static int enic_poll(struct napi_struct *napi, int budget)
1012 struct enic *enic = container_of(napi, struct enic, napi);
1013 struct net_device *netdev = enic->netdev;
1014 unsigned int rq_work_to_do = budget;
1015 unsigned int wq_work_to_do = -1; /* no limit */
1016 unsigned int work_done, rq_work_done, wq_work_done;
1018 /* Service RQ (first) and WQ
1021 rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1022 rq_work_to_do, enic_rq_service, NULL);
1024 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1025 wq_work_to_do, enic_wq_service, NULL);
1027 /* Accumulate intr event credits for this polling
1028 * cycle. An intr event is the completion of a
1029 * a WQ or RQ packet.
1032 work_done = rq_work_done + wq_work_done;
1035 vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
1037 0 /* don't unmask intr */,
1038 0 /* don't reset intr timer */);
1040 if (rq_work_done > 0) {
1045 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1049 /* If no work done, flush all LROs and exit polling
1052 if (ENIC_SETTING(enic, LRO))
1053 lro_flush_all(&enic->lro_mgr);
1055 netif_rx_complete(netdev, napi);
1056 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1059 return rq_work_done;
1062 static int enic_poll_msix(struct napi_struct *napi, int budget)
1064 struct enic *enic = container_of(napi, struct enic, napi);
1065 struct net_device *netdev = enic->netdev;
1066 unsigned int work_to_do = budget;
1067 unsigned int work_done;
1072 work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1073 work_to_do, enic_rq_service, NULL);
1075 if (work_done > 0) {
1080 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1082 /* Accumulate intr event credits for this polling
1083 * cycle. An intr event is the completion of a
1084 * a WQ or RQ packet.
1087 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
1089 0 /* don't unmask intr */,
1090 0 /* don't reset intr timer */);
1093 /* If no work done, flush all LROs and exit polling
1096 if (ENIC_SETTING(enic, LRO))
1097 lro_flush_all(&enic->lro_mgr);
1099 netif_rx_complete(netdev, napi);
1100 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1106 static void enic_notify_timer(unsigned long data)
1108 struct enic *enic = (struct enic *)data;
1110 enic_notify_check(enic);
1112 mod_timer(&enic->notify_timer, round_jiffies(ENIC_NOTIFY_TIMER_PERIOD));
1115 static void enic_free_intr(struct enic *enic)
1117 struct net_device *netdev = enic->netdev;
1120 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1121 case VNIC_DEV_INTR_MODE_INTX:
1122 case VNIC_DEV_INTR_MODE_MSI:
1123 free_irq(enic->pdev->irq, netdev);
1125 case VNIC_DEV_INTR_MODE_MSIX:
1126 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1127 if (enic->msix[i].requested)
1128 free_irq(enic->msix_entry[i].vector,
1129 enic->msix[i].devid);
1136 static int enic_request_intr(struct enic *enic)
1138 struct net_device *netdev = enic->netdev;
1142 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1144 case VNIC_DEV_INTR_MODE_INTX:
1146 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1147 IRQF_SHARED, netdev->name, netdev);
1150 case VNIC_DEV_INTR_MODE_MSI:
1152 err = request_irq(enic->pdev->irq, enic_isr_msi,
1153 0, netdev->name, enic);
1156 case VNIC_DEV_INTR_MODE_MSIX:
1158 sprintf(enic->msix[ENIC_MSIX_RQ].devname,
1159 "%.11s-rx", netdev->name);
1160 enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
1161 enic->msix[ENIC_MSIX_RQ].devid = enic;
1163 sprintf(enic->msix[ENIC_MSIX_WQ].devname,
1164 "%.11s-tx", netdev->name);
1165 enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
1166 enic->msix[ENIC_MSIX_WQ].devid = enic;
1168 sprintf(enic->msix[ENIC_MSIX_ERR].devname,
1169 "%.11s-err", netdev->name);
1170 enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
1171 enic->msix[ENIC_MSIX_ERR].devid = enic;
1173 sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
1174 "%.11s-notify", netdev->name);
1175 enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
1176 enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
1178 for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
1179 err = request_irq(enic->msix_entry[i].vector,
1180 enic->msix[i].isr, 0,
1181 enic->msix[i].devname,
1182 enic->msix[i].devid);
1184 enic_free_intr(enic);
1187 enic->msix[i].requested = 1;
1199 static int enic_notify_set(struct enic *enic)
1203 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1204 case VNIC_DEV_INTR_MODE_INTX:
1205 err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
1207 case VNIC_DEV_INTR_MODE_MSIX:
1208 err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
1211 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1218 static void enic_notify_timer_start(struct enic *enic)
1220 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1221 case VNIC_DEV_INTR_MODE_MSI:
1222 mod_timer(&enic->notify_timer, jiffies);
1225 /* Using intr for notification for INTx/MSI-X */
1230 /* rtnl lock is held, process context */
1231 static int enic_open(struct net_device *netdev)
1233 struct enic *enic = netdev_priv(netdev);
1237 for (i = 0; i < enic->rq_count; i++) {
1238 err = vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1241 "%s: Unable to alloc receive buffers.\n",
1247 for (i = 0; i < enic->wq_count; i++)
1248 vnic_wq_enable(&enic->wq[i]);
1249 for (i = 0; i < enic->rq_count; i++)
1250 vnic_rq_enable(&enic->rq[i]);
1252 enic_add_station_addr(enic);
1253 enic_set_multicast_list(netdev);
1255 netif_wake_queue(netdev);
1256 napi_enable(&enic->napi);
1257 vnic_dev_enable(enic->vdev);
1259 for (i = 0; i < enic->intr_count; i++)
1260 vnic_intr_unmask(&enic->intr[i]);
1262 enic_notify_timer_start(enic);
1267 /* rtnl lock is held, process context */
1268 static int enic_stop(struct net_device *netdev)
1270 struct enic *enic = netdev_priv(netdev);
1274 del_timer_sync(&enic->notify_timer);
1276 vnic_dev_disable(enic->vdev);
1277 napi_disable(&enic->napi);
1278 netif_stop_queue(netdev);
1280 for (i = 0; i < enic->intr_count; i++)
1281 vnic_intr_mask(&enic->intr[i]);
1283 for (i = 0; i < enic->wq_count; i++) {
1284 err = vnic_wq_disable(&enic->wq[i]);
1288 for (i = 0; i < enic->rq_count; i++) {
1289 err = vnic_rq_disable(&enic->rq[i]);
1294 (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1295 -1, enic_rq_service_drop, NULL);
1296 (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1297 -1, enic_wq_service, NULL);
1299 for (i = 0; i < enic->wq_count; i++)
1300 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1301 for (i = 0; i < enic->rq_count; i++)
1302 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1303 for (i = 0; i < enic->cq_count; i++)
1304 vnic_cq_clean(&enic->cq[i]);
1305 for (i = 0; i < enic->intr_count; i++)
1306 vnic_intr_clean(&enic->intr[i]);
1311 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1313 struct enic *enic = netdev_priv(netdev);
1314 int running = netif_running(netdev);
1319 if (new_mtu < ENIC_MIN_MTU)
1320 new_mtu = ENIC_MIN_MTU;
1321 if (new_mtu > ENIC_MAX_MTU)
1322 new_mtu = ENIC_MAX_MTU;
1324 netdev->mtu = new_mtu;
1326 if (netdev->mtu > enic->port_mtu)
1327 printk(KERN_WARNING PFX
1328 "%s: interface MTU (%d) set higher "
1329 "than port MTU (%d)\n",
1330 netdev->name, netdev->mtu, enic->port_mtu);
1338 #ifdef CONFIG_NET_POLL_CONTROLLER
1339 static void enic_poll_controller(struct net_device *netdev)
1341 struct enic *enic = netdev_priv(netdev);
1342 struct vnic_dev *vdev = enic->vdev;
1344 switch (vnic_dev_get_intr_mode(vdev)) {
1345 case VNIC_DEV_INTR_MODE_MSIX:
1346 enic_isr_msix_rq(enic->pdev->irq, enic);
1347 enic_isr_msix_wq(enic->pdev->irq, enic);
1349 case VNIC_DEV_INTR_MODE_MSI:
1350 enic_isr_msi(enic->pdev->irq, enic);
1352 case VNIC_DEV_INTR_MODE_INTX:
1353 enic_isr_legacy(enic->pdev->irq, netdev);
1361 static int enic_dev_wait(struct vnic_dev *vdev,
1362 int (*start)(struct vnic_dev *, int),
1363 int (*finished)(struct vnic_dev *, int *),
1370 BUG_ON(in_interrupt());
1372 err = start(vdev, arg);
1376 /* Wait for func to complete...2 seconds max
1379 time = jiffies + (HZ * 2);
1382 err = finished(vdev, &done);
1389 schedule_timeout_uninterruptible(HZ / 10);
1391 } while (time_after(time, jiffies));
1396 static int enic_dev_open(struct enic *enic)
1400 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1401 vnic_dev_open_done, 0);
1404 "vNIC device open failed, err %d.\n", err);
1409 static int enic_dev_soft_reset(struct enic *enic)
1413 err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
1414 vnic_dev_soft_reset_done, 0);
1417 "vNIC soft reset failed, err %d.\n", err);
1422 static void enic_reset(struct work_struct *work)
1424 struct enic *enic = container_of(work, struct enic, reset);
1426 if (!netif_running(enic->netdev))
1431 spin_lock(&enic->devcmd_lock);
1432 vnic_dev_hang_notify(enic->vdev);
1433 spin_unlock(&enic->devcmd_lock);
1435 enic_stop(enic->netdev);
1436 enic_dev_soft_reset(enic);
1437 enic_reset_mcaddrs(enic);
1438 enic_init_vnic_resources(enic);
1439 enic_open(enic->netdev);
1444 static int enic_set_intr_mode(struct enic *enic)
1446 unsigned int n = ARRAY_SIZE(enic->rq);
1447 unsigned int m = ARRAY_SIZE(enic->wq);
1450 /* Set interrupt mode (INTx, MSI, MSI-X) depending
1451 * system capabilities.
1455 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1456 * (the second to last INTR is used for WQ/RQ errors)
1457 * (the last INTR is used for notifications)
1460 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
1461 for (i = 0; i < n + m + 2; i++)
1462 enic->msix_entry[i].entry = i;
1464 if (enic->config.intr_mode < 1 &&
1465 enic->rq_count >= n &&
1466 enic->wq_count >= m &&
1467 enic->cq_count >= n + m &&
1468 enic->intr_count >= n + m + 2 &&
1469 !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
1473 enic->cq_count = n + m;
1474 enic->intr_count = n + m + 2;
1476 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
1483 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
1486 if (enic->config.intr_mode < 2 &&
1487 enic->rq_count >= 1 &&
1488 enic->wq_count >= 1 &&
1489 enic->cq_count >= 2 &&
1490 enic->intr_count >= 1 &&
1491 !pci_enable_msi(enic->pdev)) {
1496 enic->intr_count = 1;
1498 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
1505 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
1506 * (the first INTR is used for WQ/RQ)
1507 * (the second INTR is used for WQ/RQ errors)
1508 * (the last INTR is used for notifications)
1511 if (enic->config.intr_mode < 3 &&
1512 enic->rq_count >= 1 &&
1513 enic->wq_count >= 1 &&
1514 enic->cq_count >= 2 &&
1515 enic->intr_count >= 3) {
1520 enic->intr_count = 3;
1522 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
1527 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1532 static void enic_clear_intr_mode(struct enic *enic)
1534 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1535 case VNIC_DEV_INTR_MODE_MSIX:
1536 pci_disable_msix(enic->pdev);
1538 case VNIC_DEV_INTR_MODE_MSI:
1539 pci_disable_msi(enic->pdev);
1545 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1548 static void enic_iounmap(struct enic *enic)
1550 if (enic->bar0.vaddr)
1551 iounmap(enic->bar0.vaddr);
1554 static int __devinit enic_probe(struct pci_dev *pdev,
1555 const struct pci_device_id *ent)
1557 struct net_device *netdev;
1563 const u8 rss_default_cpu = 0;
1564 const u8 rss_hash_type = 0;
1565 const u8 rss_hash_bits = 0;
1566 const u8 rss_base_cpu = 0;
1567 const u8 rss_enable = 0;
1568 const u8 tso_ipid_split_en = 0;
1569 const u8 ig_vlan_strip_en = 1;
1571 /* Allocate net device structure and initialize. Private
1572 * instance data is initialized to zero.
1575 netdev = alloc_etherdev(sizeof(struct enic));
1577 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
1581 /* Set the netdev name early so intr vectors are properly
1582 * named and any error msgs can include netdev->name
1586 err = dev_alloc_name(netdev, netdev->name);
1589 printk(KERN_ERR PFX "Unable to allocate netdev name.\n");
1590 goto err_out_free_netdev;
1593 pci_set_drvdata(pdev, netdev);
1595 SET_NETDEV_DEV(netdev, &pdev->dev);
1597 enic = netdev_priv(netdev);
1598 enic->netdev = netdev;
1601 /* Setup PCI resources
1604 err = pci_enable_device(pdev);
1607 "%s: Cannot enable PCI device, aborting.\n",
1609 goto err_out_free_netdev;
1612 err = pci_request_regions(pdev, DRV_NAME);
1615 "%s: Cannot request PCI regions, aborting.\n",
1617 goto err_out_disable_device;
1620 pci_set_master(pdev);
1622 /* Query PCI controller on system for DMA addressing
1623 * limitation for the device. Try 40-bit first, and
1627 err = pci_set_dma_mask(pdev, DMA_40BIT_MASK);
1629 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1632 "%s: No usable DMA configuration, aborting.\n",
1634 goto err_out_release_regions;
1636 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1639 "%s: Unable to obtain 32-bit DMA "
1640 "for consistent allocations, aborting.\n",
1642 goto err_out_release_regions;
1645 err = pci_set_consistent_dma_mask(pdev, DMA_40BIT_MASK);
1648 "%s: Unable to obtain 40-bit DMA "
1649 "for consistent allocations, aborting.\n",
1651 goto err_out_release_regions;
1656 /* Map vNIC resources from BAR0
1659 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1661 "%s: BAR0 not memory-map'able, aborting.\n",
1664 goto err_out_release_regions;
1667 enic->bar0.vaddr = pci_iomap(pdev, 0, enic->bar0.len);
1668 enic->bar0.bus_addr = pci_resource_start(pdev, 0);
1669 enic->bar0.len = pci_resource_len(pdev, 0);
1671 if (!enic->bar0.vaddr) {
1673 "%s: Cannot memory-map BAR0 res hdr, aborting.\n",
1676 goto err_out_release_regions;
1679 /* Register vNIC device
1682 enic->vdev = vnic_dev_register(NULL, enic, pdev, &enic->bar0);
1685 "%s: vNIC registration failed, aborting.\n",
1688 goto err_out_iounmap;
1691 /* Issue device open to get device in known state
1694 err = enic_dev_open(enic);
1697 "%s: vNIC dev open failed, aborting.\n",
1699 goto err_out_vnic_unregister;
1702 /* Issue device init to initialize the vnic-to-switch link.
1703 * We'll start with carrier off and wait for link UP
1704 * notification later to turn on carrier. We don't need
1705 * to wait here for the vnic-to-switch link initialization
1706 * to complete; link UP notification is the indication that
1707 * the process is complete.
1710 netif_carrier_off(netdev);
1712 err = vnic_dev_init(enic->vdev, 0);
1715 "%s: vNIC dev init failed, aborting.\n",
1717 goto err_out_dev_close;
1720 /* Get vNIC configuration
1723 err = enic_get_vnic_config(enic);
1726 "%s: Get vNIC configuration failed, aborting.\n",
1728 goto err_out_dev_close;
1731 /* Get available resource counts
1734 enic_get_res_counts(enic);
1736 /* Set interrupt mode based on resource counts and system
1740 err = enic_set_intr_mode(enic);
1743 "%s: Failed to set intr mode, aborting.\n",
1745 goto err_out_dev_close;
1748 /* Request interrupt vector(s)
1751 err = enic_request_intr(enic);
1753 printk(KERN_ERR PFX "%s: Unable to request irq.\n",
1755 goto err_out_dev_close;
1758 /* Allocate and configure vNIC resources
1761 err = enic_alloc_vnic_resources(enic);
1764 "%s: Failed to alloc vNIC resources, aborting.\n",
1766 goto err_out_free_vnic_resources;
1769 enic_init_vnic_resources(enic);
1771 /* Enable VLAN tag stripping. RSS not enabled (yet).
1774 err = enic_set_nic_cfg(enic,
1775 rss_default_cpu, rss_hash_type,
1776 rss_hash_bits, rss_base_cpu,
1777 rss_enable, tso_ipid_split_en,
1781 "%s: Failed to config nic, aborting.\n",
1783 goto err_out_free_vnic_resources;
1786 /* Setup notification buffer area
1789 err = enic_notify_set(enic);
1792 "%s: Failed to alloc notify buffer, aborting.\n",
1794 goto err_out_free_vnic_resources;
1797 /* Setup notification timer, HW reset task, and locks
1800 init_timer(&enic->notify_timer);
1801 enic->notify_timer.function = enic_notify_timer;
1802 enic->notify_timer.data = (unsigned long)enic;
1804 INIT_WORK(&enic->reset, enic_reset);
1806 for (i = 0; i < enic->wq_count; i++)
1807 spin_lock_init(&enic->wq_lock[i]);
1809 spin_lock_init(&enic->devcmd_lock);
1811 /* Register net device
1814 enic->port_mtu = enic->config.mtu;
1815 (void)enic_change_mtu(netdev, enic->port_mtu);
1817 err = enic_set_mac_addr(netdev, enic->mac_addr);
1820 "%s: Invalid MAC address, aborting.\n",
1822 goto err_out_notify_unset;
1825 netdev->open = enic_open;
1826 netdev->stop = enic_stop;
1827 netdev->hard_start_xmit = enic_hard_start_xmit;
1828 netdev->get_stats = enic_get_stats;
1829 netdev->set_multicast_list = enic_set_multicast_list;
1830 netdev->change_mtu = enic_change_mtu;
1831 netdev->vlan_rx_register = enic_vlan_rx_register;
1832 netdev->vlan_rx_add_vid = enic_vlan_rx_add_vid;
1833 netdev->vlan_rx_kill_vid = enic_vlan_rx_kill_vid;
1834 netdev->tx_timeout = enic_tx_timeout;
1835 netdev->watchdog_timeo = 2 * HZ;
1836 netdev->ethtool_ops = &enic_ethtool_ops;
1837 #ifdef CONFIG_NET_POLL_CONTROLLER
1838 netdev->poll_controller = enic_poll_controller;
1841 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1843 netif_napi_add(netdev, &enic->napi, enic_poll, 64);
1845 case VNIC_DEV_INTR_MODE_MSIX:
1846 netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
1850 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1851 if (ENIC_SETTING(enic, TXCSUM))
1852 netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
1853 if (ENIC_SETTING(enic, TSO))
1854 netdev->features |= NETIF_F_TSO |
1855 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
1857 netdev->features |= NETIF_F_HIGHDMA;
1860 enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
1862 if (ENIC_SETTING(enic, LRO)) {
1863 enic->lro_mgr.max_aggr = ENIC_LRO_MAX_AGGR;
1864 enic->lro_mgr.max_desc = ENIC_LRO_MAX_DESC;
1865 enic->lro_mgr.lro_arr = enic->lro_desc;
1866 enic->lro_mgr.get_skb_header = enic_get_skb_header;
1867 enic->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1868 enic->lro_mgr.dev = netdev;
1869 enic->lro_mgr.ip_summed = CHECKSUM_COMPLETE;
1870 enic->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1873 err = register_netdev(netdev);
1876 "%s: Cannot register net device, aborting.\n",
1878 goto err_out_notify_unset;
1883 err_out_notify_unset:
1884 vnic_dev_notify_unset(enic->vdev);
1885 err_out_free_vnic_resources:
1886 enic_free_vnic_resources(enic);
1887 enic_free_intr(enic);
1889 vnic_dev_close(enic->vdev);
1890 err_out_vnic_unregister:
1891 enic_clear_intr_mode(enic);
1892 vnic_dev_unregister(enic->vdev);
1895 err_out_release_regions:
1896 pci_release_regions(pdev);
1897 err_out_disable_device:
1898 pci_disable_device(pdev);
1899 err_out_free_netdev:
1900 pci_set_drvdata(pdev, NULL);
1901 free_netdev(netdev);
1906 static void __devexit enic_remove(struct pci_dev *pdev)
1908 struct net_device *netdev = pci_get_drvdata(pdev);
1911 struct enic *enic = netdev_priv(netdev);
1913 flush_scheduled_work();
1914 unregister_netdev(netdev);
1915 vnic_dev_notify_unset(enic->vdev);
1916 enic_free_vnic_resources(enic);
1917 enic_free_intr(enic);
1918 vnic_dev_close(enic->vdev);
1919 enic_clear_intr_mode(enic);
1920 vnic_dev_unregister(enic->vdev);
1922 pci_release_regions(pdev);
1923 pci_disable_device(pdev);
1924 pci_set_drvdata(pdev, NULL);
1925 free_netdev(netdev);
1929 static struct pci_driver enic_driver = {
1931 .id_table = enic_id_table,
1932 .probe = enic_probe,
1933 .remove = __devexit_p(enic_remove),
1936 static int __init enic_init_module(void)
1938 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1940 return pci_register_driver(&enic_driver);
1943 static void __exit enic_cleanup_module(void)
1945 pci_unregister_driver(&enic_driver);
1948 module_init(enic_init_module);
1949 module_exit(enic_cleanup_module);