added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / enic / enic_main.c
blob7d60551d538fccae67b2638ac8b85f985938a8b4
1 /*
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
16 * SOFTWARE.
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>
33 #include <linux/in.h>
34 #include <linux/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/tcp.h>
37 #include <net/ip6_checksum.h>
39 #include "cq_enet_desc.h"
40 #include "vnic_dev.h"
41 #include "vnic_intr.h"
42 #include "vnic_stats.h"
43 #include "enic_res.h"
44 #include "enic.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);
60 struct enic_stat {
61 char name[ETH_GSTRING_LEN];
62 unsigned int offset;
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),
81 ENIC_TX_STAT(tx_tso),
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),
97 ENIC_RX_STAT(rx_rss),
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;
124 } else {
125 ecmd->speed = -1;
126 ecmd->duplex = -1;
129 ecmd->autoneg = AUTONEG_DISABLE;
131 return 0;
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)
154 unsigned int i;
156 switch (stringset) {
157 case ETH_SS_STATS:
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;
166 break;
170 static int enic_get_sset_count(struct net_device *netdev, int sset)
172 switch (sset) {
173 case ETH_SS_STATS:
174 return enic_n_tx_stats + enic_n_rx_stats;
175 default:
176 return -EOPNOTSUPP;
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;
185 unsigned int i;
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))
208 return -EINVAL;
210 enic->csum_rx_enabled = !!data;
212 return 0;
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))
220 return -EINVAL;
222 if (data)
223 netdev->features |= NETIF_F_HW_CSUM;
224 else
225 netdev->features &= ~NETIF_F_HW_CSUM;
227 return 0;
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))
235 return -EINVAL;
237 if (data)
238 netdev->features |=
239 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
240 else
241 netdev->features &=
242 ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
244 return 0;
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,
276 .get_flags = ethtool_op_get_flags,
277 .set_flags = ethtool_op_set_flags,
280 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
282 struct enic *enic = vnic_dev_priv(wq->vdev);
284 if (buf->sop)
285 pci_unmap_single(enic->pdev, buf->dma_addr,
286 buf->len, PCI_DMA_TODEVICE);
287 else
288 pci_unmap_page(enic->pdev, buf->dma_addr,
289 buf->len, PCI_DMA_TODEVICE);
291 if (buf->os_buf)
292 dev_kfree_skb_any(buf->os_buf);
295 static void enic_wq_free_buf(struct vnic_wq *wq,
296 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
298 enic_free_wq_buf(wq, buf);
301 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
302 u8 type, u16 q_number, u16 completed_index, void *opaque)
304 struct enic *enic = vnic_dev_priv(vdev);
306 spin_lock(&enic->wq_lock[q_number]);
308 vnic_wq_service(&enic->wq[q_number], cq_desc,
309 completed_index, enic_wq_free_buf,
310 opaque);
312 if (netif_queue_stopped(enic->netdev) &&
313 vnic_wq_desc_avail(&enic->wq[q_number]) >= MAX_SKB_FRAGS + 1)
314 netif_wake_queue(enic->netdev);
316 spin_unlock(&enic->wq_lock[q_number]);
318 return 0;
321 static void enic_log_q_error(struct enic *enic)
323 unsigned int i;
324 u32 error_status;
326 for (i = 0; i < enic->wq_count; i++) {
327 error_status = vnic_wq_error_status(&enic->wq[i]);
328 if (error_status)
329 printk(KERN_ERR PFX "%s: WQ[%d] error_status %d\n",
330 enic->netdev->name, i, error_status);
333 for (i = 0; i < enic->rq_count; i++) {
334 error_status = vnic_rq_error_status(&enic->rq[i]);
335 if (error_status)
336 printk(KERN_ERR PFX "%s: RQ[%d] error_status %d\n",
337 enic->netdev->name, i, error_status);
341 static void enic_link_check(struct enic *enic)
343 int link_status = vnic_dev_link_status(enic->vdev);
344 int carrier_ok = netif_carrier_ok(enic->netdev);
346 if (link_status && !carrier_ok) {
347 printk(KERN_INFO PFX "%s: Link UP\n", enic->netdev->name);
348 netif_carrier_on(enic->netdev);
349 } else if (!link_status && carrier_ok) {
350 printk(KERN_INFO PFX "%s: Link DOWN\n", enic->netdev->name);
351 netif_carrier_off(enic->netdev);
355 static void enic_mtu_check(struct enic *enic)
357 u32 mtu = vnic_dev_mtu(enic->vdev);
359 if (mtu != enic->port_mtu) {
360 if (mtu < enic->netdev->mtu)
361 printk(KERN_WARNING PFX
362 "%s: interface MTU (%d) set higher "
363 "than switch port MTU (%d)\n",
364 enic->netdev->name, enic->netdev->mtu, mtu);
365 enic->port_mtu = mtu;
369 static void enic_msglvl_check(struct enic *enic)
371 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
373 if (msg_enable != enic->msg_enable) {
374 printk(KERN_INFO PFX "%s: msg lvl changed from 0x%x to 0x%x\n",
375 enic->netdev->name, enic->msg_enable, msg_enable);
376 enic->msg_enable = msg_enable;
380 static void enic_notify_check(struct enic *enic)
382 enic_msglvl_check(enic);
383 enic_mtu_check(enic);
384 enic_link_check(enic);
387 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
389 static irqreturn_t enic_isr_legacy(int irq, void *data)
391 struct net_device *netdev = data;
392 struct enic *enic = netdev_priv(netdev);
393 u32 pba;
395 vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]);
397 pba = vnic_intr_legacy_pba(enic->legacy_pba);
398 if (!pba) {
399 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
400 return IRQ_NONE; /* not our interrupt */
403 if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY))
404 enic_notify_check(enic);
406 if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) {
407 enic_log_q_error(enic);
408 /* schedule recovery from WQ/RQ error */
409 schedule_work(&enic->reset);
410 return IRQ_HANDLED;
413 if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) {
414 if (netif_rx_schedule_prep(&enic->napi))
415 __netif_rx_schedule(&enic->napi);
416 } else {
417 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
420 return IRQ_HANDLED;
423 static irqreturn_t enic_isr_msi(int irq, void *data)
425 struct enic *enic = data;
427 /* With MSI, there is no sharing of interrupts, so this is
428 * our interrupt and there is no need to ack it. The device
429 * is not providing per-vector masking, so the OS will not
430 * write to PCI config space to mask/unmask the interrupt.
431 * We're using mask_on_assertion for MSI, so the device
432 * automatically masks the interrupt when the interrupt is
433 * generated. Later, when exiting polling, the interrupt
434 * will be unmasked (see enic_poll).
436 * Also, the device uses the same PCIe Traffic Class (TC)
437 * for Memory Write data and MSI, so there are no ordering
438 * issues; the MSI will always arrive at the Root Complex
439 * _after_ corresponding Memory Writes (i.e. descriptor
440 * writes).
443 netif_rx_schedule(&enic->napi);
445 return IRQ_HANDLED;
448 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
450 struct enic *enic = data;
452 /* schedule NAPI polling for RQ cleanup */
453 netif_rx_schedule(&enic->napi);
455 return IRQ_HANDLED;
458 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
460 struct enic *enic = data;
461 unsigned int wq_work_to_do = -1; /* no limit */
462 unsigned int wq_work_done;
464 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
465 wq_work_to_do, enic_wq_service, NULL);
467 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ],
468 wq_work_done,
469 1 /* unmask intr */,
470 1 /* reset intr timer */);
472 return IRQ_HANDLED;
475 static irqreturn_t enic_isr_msix_err(int irq, void *data)
477 struct enic *enic = data;
479 enic_log_q_error(enic);
481 /* schedule recovery from WQ/RQ error */
482 schedule_work(&enic->reset);
484 return IRQ_HANDLED;
487 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
489 struct enic *enic = data;
491 enic_notify_check(enic);
492 vnic_intr_unmask(&enic->intr[ENIC_MSIX_NOTIFY]);
494 return IRQ_HANDLED;
497 static inline void enic_queue_wq_skb_cont(struct enic *enic,
498 struct vnic_wq *wq, struct sk_buff *skb,
499 unsigned int len_left)
501 skb_frag_t *frag;
503 /* Queue additional data fragments */
504 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
505 len_left -= frag->size;
506 enic_queue_wq_desc_cont(wq, skb,
507 pci_map_page(enic->pdev, frag->page,
508 frag->page_offset, frag->size,
509 PCI_DMA_TODEVICE),
510 frag->size,
511 (len_left == 0)); /* EOP? */
515 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
516 struct vnic_wq *wq, struct sk_buff *skb,
517 int vlan_tag_insert, unsigned int vlan_tag)
519 unsigned int head_len = skb_headlen(skb);
520 unsigned int len_left = skb->len - head_len;
521 int eop = (len_left == 0);
523 /* Queue the main skb fragment */
524 enic_queue_wq_desc(wq, skb,
525 pci_map_single(enic->pdev, skb->data,
526 head_len, PCI_DMA_TODEVICE),
527 head_len,
528 vlan_tag_insert, vlan_tag,
529 eop);
531 if (!eop)
532 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
535 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
536 struct vnic_wq *wq, struct sk_buff *skb,
537 int vlan_tag_insert, unsigned int vlan_tag)
539 unsigned int head_len = skb_headlen(skb);
540 unsigned int len_left = skb->len - head_len;
541 unsigned int hdr_len = skb_transport_offset(skb);
542 unsigned int csum_offset = hdr_len + skb->csum_offset;
543 int eop = (len_left == 0);
545 /* Queue the main skb fragment */
546 enic_queue_wq_desc_csum_l4(wq, skb,
547 pci_map_single(enic->pdev, skb->data,
548 head_len, PCI_DMA_TODEVICE),
549 head_len,
550 csum_offset,
551 hdr_len,
552 vlan_tag_insert, vlan_tag,
553 eop);
555 if (!eop)
556 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
559 static inline void enic_queue_wq_skb_tso(struct enic *enic,
560 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
561 int vlan_tag_insert, unsigned int vlan_tag)
563 unsigned int head_len = skb_headlen(skb);
564 unsigned int len_left = skb->len - head_len;
565 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
566 int eop = (len_left == 0);
568 /* Preload TCP csum field with IP pseudo hdr calculated
569 * with IP length set to zero. HW will later add in length
570 * to each TCP segment resulting from the TSO.
573 if (skb->protocol == __constant_htons(ETH_P_IP)) {
574 ip_hdr(skb)->check = 0;
575 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
576 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
577 } else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {
578 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
579 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
582 /* Queue the main skb fragment */
583 enic_queue_wq_desc_tso(wq, skb,
584 pci_map_single(enic->pdev, skb->data,
585 head_len, PCI_DMA_TODEVICE),
586 head_len,
587 mss, hdr_len,
588 vlan_tag_insert, vlan_tag,
589 eop);
591 if (!eop)
592 enic_queue_wq_skb_cont(enic, wq, skb, len_left);
595 static inline void enic_queue_wq_skb(struct enic *enic,
596 struct vnic_wq *wq, struct sk_buff *skb)
598 unsigned int mss = skb_shinfo(skb)->gso_size;
599 unsigned int vlan_tag = 0;
600 int vlan_tag_insert = 0;
602 if (enic->vlan_group && vlan_tx_tag_present(skb)) {
603 /* VLAN tag from trunking driver */
604 vlan_tag_insert = 1;
605 vlan_tag = vlan_tx_tag_get(skb);
608 if (mss)
609 enic_queue_wq_skb_tso(enic, wq, skb, mss,
610 vlan_tag_insert, vlan_tag);
611 else if (skb->ip_summed == CHECKSUM_PARTIAL)
612 enic_queue_wq_skb_csum_l4(enic, wq, skb,
613 vlan_tag_insert, vlan_tag);
614 else
615 enic_queue_wq_skb_vlan(enic, wq, skb,
616 vlan_tag_insert, vlan_tag);
619 /* netif_tx_lock held, process context with BHs disabled */
620 static int enic_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
622 struct enic *enic = netdev_priv(netdev);
623 struct vnic_wq *wq = &enic->wq[0];
624 unsigned long flags;
626 if (skb->len <= 0) {
627 dev_kfree_skb(skb);
628 return NETDEV_TX_OK;
631 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
632 * which is very likely. In the off chance it's going to take
633 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
636 if (skb_shinfo(skb)->gso_size == 0 &&
637 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
638 skb_linearize(skb)) {
639 dev_kfree_skb(skb);
640 return NETDEV_TX_OK;
643 spin_lock_irqsave(&enic->wq_lock[0], flags);
645 if (vnic_wq_desc_avail(wq) < skb_shinfo(skb)->nr_frags + 1) {
646 netif_stop_queue(netdev);
647 /* This is a hard error, log it */
648 printk(KERN_ERR PFX "%s: BUG! Tx ring full when "
649 "queue awake!\n", netdev->name);
650 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
651 return NETDEV_TX_BUSY;
654 enic_queue_wq_skb(enic, wq, skb);
656 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + 1)
657 netif_stop_queue(netdev);
659 netdev->trans_start = jiffies;
661 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
663 return NETDEV_TX_OK;
666 /* dev_base_lock rwlock held, nominally process context */
667 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
669 struct enic *enic = netdev_priv(netdev);
670 struct net_device_stats *net_stats = &netdev->stats;
671 struct vnic_stats *stats;
673 spin_lock(&enic->devcmd_lock);
674 vnic_dev_stats_dump(enic->vdev, &stats);
675 spin_unlock(&enic->devcmd_lock);
677 net_stats->tx_packets = stats->tx.tx_frames_ok;
678 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
679 net_stats->tx_errors = stats->tx.tx_errors;
680 net_stats->tx_dropped = stats->tx.tx_drops;
682 net_stats->rx_packets = stats->rx.rx_frames_ok;
683 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
684 net_stats->rx_errors = stats->rx.rx_errors;
685 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
686 net_stats->rx_crc_errors = stats->rx.rx_crc_errors;
687 net_stats->rx_dropped = stats->rx.rx_no_bufs;
689 return net_stats;
692 static void enic_reset_mcaddrs(struct enic *enic)
694 enic->mc_count = 0;
697 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
699 if (!is_valid_ether_addr(addr))
700 return -EADDRNOTAVAIL;
702 memcpy(netdev->dev_addr, addr, netdev->addr_len);
704 return 0;
707 /* netif_tx_lock held, BHs disabled */
708 static void enic_set_multicast_list(struct net_device *netdev)
710 struct enic *enic = netdev_priv(netdev);
711 struct dev_mc_list *list = netdev->mc_list;
712 int directed = 1;
713 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
714 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
715 int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0;
716 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
717 (netdev->mc_count > ENIC_MULTICAST_PERFECT_FILTERS);
718 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
719 unsigned int mc_count = netdev->mc_count;
720 unsigned int i, j;
722 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS)
723 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
725 spin_lock(&enic->devcmd_lock);
727 vnic_dev_packet_filter(enic->vdev, directed,
728 multicast, broadcast, promisc, allmulti);
730 /* Is there an easier way? Trying to minimize to
731 * calls to add/del multicast addrs. We keep the
732 * addrs from the last call in enic->mc_addr and
733 * look for changes to add/del.
736 for (i = 0; list && i < mc_count; i++) {
737 memcpy(mc_addr[i], list->dmi_addr, ETH_ALEN);
738 list = list->next;
741 for (i = 0; i < enic->mc_count; i++) {
742 for (j = 0; j < mc_count; j++)
743 if (compare_ether_addr(enic->mc_addr[i],
744 mc_addr[j]) == 0)
745 break;
746 if (j == mc_count)
747 enic_del_multicast_addr(enic, enic->mc_addr[i]);
750 for (i = 0; i < mc_count; i++) {
751 for (j = 0; j < enic->mc_count; j++)
752 if (compare_ether_addr(mc_addr[i],
753 enic->mc_addr[j]) == 0)
754 break;
755 if (j == enic->mc_count)
756 enic_add_multicast_addr(enic, mc_addr[i]);
759 /* Save the list to compare against next time
762 for (i = 0; i < mc_count; i++)
763 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
765 enic->mc_count = mc_count;
767 spin_unlock(&enic->devcmd_lock);
770 /* rtnl lock is held */
771 static void enic_vlan_rx_register(struct net_device *netdev,
772 struct vlan_group *vlan_group)
774 struct enic *enic = netdev_priv(netdev);
775 enic->vlan_group = vlan_group;
778 /* rtnl lock is held */
779 static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
781 struct enic *enic = netdev_priv(netdev);
783 spin_lock(&enic->devcmd_lock);
784 enic_add_vlan(enic, vid);
785 spin_unlock(&enic->devcmd_lock);
788 /* rtnl lock is held */
789 static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
791 struct enic *enic = netdev_priv(netdev);
793 spin_lock(&enic->devcmd_lock);
794 enic_del_vlan(enic, vid);
795 spin_unlock(&enic->devcmd_lock);
798 /* netif_tx_lock held, BHs disabled */
799 static void enic_tx_timeout(struct net_device *netdev)
801 struct enic *enic = netdev_priv(netdev);
802 schedule_work(&enic->reset);
805 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
807 struct enic *enic = vnic_dev_priv(rq->vdev);
809 if (!buf->os_buf)
810 return;
812 pci_unmap_single(enic->pdev, buf->dma_addr,
813 buf->len, PCI_DMA_FROMDEVICE);
814 dev_kfree_skb_any(buf->os_buf);
817 static inline struct sk_buff *enic_rq_alloc_skb(unsigned int size)
819 struct sk_buff *skb;
821 skb = dev_alloc_skb(size + NET_IP_ALIGN);
823 if (skb)
824 skb_reserve(skb, NET_IP_ALIGN);
826 return skb;
829 static int enic_rq_alloc_buf(struct vnic_rq *rq)
831 struct enic *enic = vnic_dev_priv(rq->vdev);
832 struct sk_buff *skb;
833 unsigned int len = enic->netdev->mtu + ETH_HLEN;
834 unsigned int os_buf_index = 0;
835 dma_addr_t dma_addr;
837 skb = enic_rq_alloc_skb(len);
838 if (!skb)
839 return -ENOMEM;
841 dma_addr = pci_map_single(enic->pdev, skb->data,
842 len, PCI_DMA_FROMDEVICE);
844 enic_queue_rq_desc(rq, skb, os_buf_index,
845 dma_addr, len);
847 return 0;
850 static int enic_get_skb_header(struct sk_buff *skb, void **iphdr,
851 void **tcph, u64 *hdr_flags, void *priv)
853 struct cq_enet_rq_desc *cq_desc = priv;
854 unsigned int ip_len;
855 struct iphdr *iph;
857 u8 type, color, eop, sop, ingress_port, vlan_stripped;
858 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
859 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
860 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
861 u8 packet_error;
862 u16 q_number, completed_index, bytes_written, vlan, checksum;
863 u32 rss_hash;
865 cq_enet_rq_desc_dec(cq_desc,
866 &type, &color, &q_number, &completed_index,
867 &ingress_port, &fcoe, &eop, &sop, &rss_type,
868 &csum_not_calc, &rss_hash, &bytes_written,
869 &packet_error, &vlan_stripped, &vlan, &checksum,
870 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
871 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
872 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
873 &fcs_ok);
875 if (!(ipv4 && tcp && !ipv4_fragment))
876 return -1;
878 skb_reset_network_header(skb);
879 iph = ip_hdr(skb);
881 ip_len = ip_hdrlen(skb);
882 skb_set_transport_header(skb, ip_len);
884 /* check if ip header and tcp header are complete */
885 if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
886 return -1;
888 *hdr_flags = LRO_IPV4 | LRO_TCP;
889 *tcph = tcp_hdr(skb);
890 *iphdr = iph;
892 return 0;
895 static void enic_rq_indicate_buf(struct vnic_rq *rq,
896 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
897 int skipped, void *opaque)
899 struct enic *enic = vnic_dev_priv(rq->vdev);
900 struct net_device *netdev = enic->netdev;
901 struct sk_buff *skb;
903 u8 type, color, eop, sop, ingress_port, vlan_stripped;
904 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
905 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
906 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
907 u8 packet_error;
908 u16 q_number, completed_index, bytes_written, vlan, checksum;
909 u32 rss_hash;
911 if (skipped)
912 return;
914 skb = buf->os_buf;
915 prefetch(skb->data - NET_IP_ALIGN);
916 pci_unmap_single(enic->pdev, buf->dma_addr,
917 buf->len, PCI_DMA_FROMDEVICE);
919 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
920 &type, &color, &q_number, &completed_index,
921 &ingress_port, &fcoe, &eop, &sop, &rss_type,
922 &csum_not_calc, &rss_hash, &bytes_written,
923 &packet_error, &vlan_stripped, &vlan, &checksum,
924 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
925 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
926 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
927 &fcs_ok);
929 if (packet_error) {
931 if (bytes_written > 0 && !fcs_ok) {
932 if (net_ratelimit())
933 printk(KERN_ERR PFX
934 "%s: packet error: bad FCS\n",
935 netdev->name);
938 dev_kfree_skb_any(skb);
940 return;
943 if (eop && bytes_written > 0) {
945 /* Good receive
948 skb_put(skb, bytes_written);
949 skb->protocol = eth_type_trans(skb, netdev);
951 if (enic->csum_rx_enabled && !csum_not_calc) {
952 skb->csum = htons(checksum);
953 skb->ip_summed = CHECKSUM_COMPLETE;
956 skb->dev = netdev;
958 if (enic->vlan_group && vlan_stripped) {
960 if ((netdev->features & NETIF_F_LRO) && ipv4)
961 lro_vlan_hwaccel_receive_skb(&enic->lro_mgr,
962 skb, enic->vlan_group,
963 vlan, cq_desc);
964 else
965 vlan_hwaccel_receive_skb(skb,
966 enic->vlan_group, vlan);
968 } else {
970 if ((netdev->features & NETIF_F_LRO) && ipv4)
971 lro_receive_skb(&enic->lro_mgr, skb, cq_desc);
972 else
973 netif_receive_skb(skb);
977 } else {
979 /* Buffer overflow
982 dev_kfree_skb_any(skb);
986 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
987 u8 type, u16 q_number, u16 completed_index, void *opaque)
989 struct enic *enic = vnic_dev_priv(vdev);
991 vnic_rq_service(&enic->rq[q_number], cq_desc,
992 completed_index, VNIC_RQ_RETURN_DESC,
993 enic_rq_indicate_buf, opaque);
995 return 0;
998 static void enic_rq_drop_buf(struct vnic_rq *rq,
999 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1000 int skipped, void *opaque)
1002 struct enic *enic = vnic_dev_priv(rq->vdev);
1003 struct sk_buff *skb = buf->os_buf;
1005 if (skipped)
1006 return;
1008 pci_unmap_single(enic->pdev, buf->dma_addr,
1009 buf->len, PCI_DMA_FROMDEVICE);
1011 dev_kfree_skb_any(skb);
1014 static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1015 u8 type, u16 q_number, u16 completed_index, void *opaque)
1017 struct enic *enic = vnic_dev_priv(vdev);
1019 vnic_rq_service(&enic->rq[q_number], cq_desc,
1020 completed_index, VNIC_RQ_RETURN_DESC,
1021 enic_rq_drop_buf, opaque);
1023 return 0;
1026 static int enic_poll(struct napi_struct *napi, int budget)
1028 struct enic *enic = container_of(napi, struct enic, napi);
1029 struct net_device *netdev = enic->netdev;
1030 unsigned int rq_work_to_do = budget;
1031 unsigned int wq_work_to_do = -1; /* no limit */
1032 unsigned int work_done, rq_work_done, wq_work_done;
1034 /* Service RQ (first) and WQ
1037 rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1038 rq_work_to_do, enic_rq_service, NULL);
1040 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1041 wq_work_to_do, enic_wq_service, NULL);
1043 /* Accumulate intr event credits for this polling
1044 * cycle. An intr event is the completion of a
1045 * a WQ or RQ packet.
1048 work_done = rq_work_done + wq_work_done;
1050 if (work_done > 0)
1051 vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
1052 work_done,
1053 0 /* don't unmask intr */,
1054 0 /* don't reset intr timer */);
1056 if (rq_work_done > 0) {
1058 /* Replenish RQ
1061 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1063 } else {
1065 /* If no work done, flush all LROs and exit polling
1068 if (netdev->features & NETIF_F_LRO)
1069 lro_flush_all(&enic->lro_mgr);
1071 netif_rx_complete(napi);
1072 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1075 return rq_work_done;
1078 static int enic_poll_msix(struct napi_struct *napi, int budget)
1080 struct enic *enic = container_of(napi, struct enic, napi);
1081 struct net_device *netdev = enic->netdev;
1082 unsigned int work_to_do = budget;
1083 unsigned int work_done;
1085 /* Service RQ
1088 work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1089 work_to_do, enic_rq_service, NULL);
1091 if (work_done > 0) {
1093 /* Replenish RQ
1096 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1098 /* Accumulate intr event credits for this polling
1099 * cycle. An intr event is the completion of a
1100 * a WQ or RQ packet.
1103 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
1104 work_done,
1105 0 /* don't unmask intr */,
1106 0 /* don't reset intr timer */);
1107 } else {
1109 /* If no work done, flush all LROs and exit polling
1112 if (netdev->features & NETIF_F_LRO)
1113 lro_flush_all(&enic->lro_mgr);
1115 netif_rx_complete(napi);
1116 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1119 return work_done;
1122 static void enic_notify_timer(unsigned long data)
1124 struct enic *enic = (struct enic *)data;
1126 enic_notify_check(enic);
1128 mod_timer(&enic->notify_timer,
1129 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1132 static void enic_free_intr(struct enic *enic)
1134 struct net_device *netdev = enic->netdev;
1135 unsigned int i;
1137 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1138 case VNIC_DEV_INTR_MODE_INTX:
1139 free_irq(enic->pdev->irq, netdev);
1140 break;
1141 case VNIC_DEV_INTR_MODE_MSI:
1142 free_irq(enic->pdev->irq, enic);
1143 break;
1144 case VNIC_DEV_INTR_MODE_MSIX:
1145 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1146 if (enic->msix[i].requested)
1147 free_irq(enic->msix_entry[i].vector,
1148 enic->msix[i].devid);
1149 break;
1150 default:
1151 break;
1155 static int enic_request_intr(struct enic *enic)
1157 struct net_device *netdev = enic->netdev;
1158 unsigned int i;
1159 int err = 0;
1161 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1163 case VNIC_DEV_INTR_MODE_INTX:
1165 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1166 IRQF_SHARED, netdev->name, netdev);
1167 break;
1169 case VNIC_DEV_INTR_MODE_MSI:
1171 err = request_irq(enic->pdev->irq, enic_isr_msi,
1172 0, netdev->name, enic);
1173 break;
1175 case VNIC_DEV_INTR_MODE_MSIX:
1177 sprintf(enic->msix[ENIC_MSIX_RQ].devname,
1178 "%.11s-rx-0", netdev->name);
1179 enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
1180 enic->msix[ENIC_MSIX_RQ].devid = enic;
1182 sprintf(enic->msix[ENIC_MSIX_WQ].devname,
1183 "%.11s-tx-0", netdev->name);
1184 enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
1185 enic->msix[ENIC_MSIX_WQ].devid = enic;
1187 sprintf(enic->msix[ENIC_MSIX_ERR].devname,
1188 "%.11s-err", netdev->name);
1189 enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
1190 enic->msix[ENIC_MSIX_ERR].devid = enic;
1192 sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
1193 "%.11s-notify", netdev->name);
1194 enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
1195 enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
1197 for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
1198 err = request_irq(enic->msix_entry[i].vector,
1199 enic->msix[i].isr, 0,
1200 enic->msix[i].devname,
1201 enic->msix[i].devid);
1202 if (err) {
1203 enic_free_intr(enic);
1204 break;
1206 enic->msix[i].requested = 1;
1209 break;
1211 default:
1212 break;
1215 return err;
1218 static int enic_notify_set(struct enic *enic)
1220 int err;
1222 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1223 case VNIC_DEV_INTR_MODE_INTX:
1224 err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
1225 break;
1226 case VNIC_DEV_INTR_MODE_MSIX:
1227 err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
1228 break;
1229 default:
1230 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1231 break;
1234 return err;
1237 static void enic_notify_timer_start(struct enic *enic)
1239 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1240 case VNIC_DEV_INTR_MODE_MSI:
1241 mod_timer(&enic->notify_timer, jiffies);
1242 break;
1243 default:
1244 /* Using intr for notification for INTx/MSI-X */
1245 break;
1249 /* rtnl lock is held, process context */
1250 static int enic_open(struct net_device *netdev)
1252 struct enic *enic = netdev_priv(netdev);
1253 unsigned int i;
1254 int err;
1256 err = enic_request_intr(enic);
1257 if (err) {
1258 printk(KERN_ERR PFX "%s: Unable to request irq.\n",
1259 netdev->name);
1260 return err;
1263 err = enic_notify_set(enic);
1264 if (err) {
1265 printk(KERN_ERR PFX
1266 "%s: Failed to alloc notify buffer, aborting.\n",
1267 netdev->name);
1268 goto err_out_free_intr;
1271 for (i = 0; i < enic->rq_count; i++) {
1272 err = vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1273 if (err) {
1274 printk(KERN_ERR PFX
1275 "%s: Unable to alloc receive buffers.\n",
1276 netdev->name);
1277 goto err_out_notify_unset;
1281 for (i = 0; i < enic->wq_count; i++)
1282 vnic_wq_enable(&enic->wq[i]);
1283 for (i = 0; i < enic->rq_count; i++)
1284 vnic_rq_enable(&enic->rq[i]);
1286 enic_add_station_addr(enic);
1287 enic_set_multicast_list(netdev);
1289 netif_wake_queue(netdev);
1290 napi_enable(&enic->napi);
1291 vnic_dev_enable(enic->vdev);
1293 for (i = 0; i < enic->intr_count; i++)
1294 vnic_intr_unmask(&enic->intr[i]);
1296 enic_notify_timer_start(enic);
1298 return 0;
1300 err_out_notify_unset:
1301 vnic_dev_notify_unset(enic->vdev);
1302 err_out_free_intr:
1303 enic_free_intr(enic);
1305 return err;
1308 /* rtnl lock is held, process context */
1309 static int enic_stop(struct net_device *netdev)
1311 struct enic *enic = netdev_priv(netdev);
1312 unsigned int i;
1313 int err;
1315 del_timer_sync(&enic->notify_timer);
1317 vnic_dev_disable(enic->vdev);
1318 napi_disable(&enic->napi);
1319 netif_stop_queue(netdev);
1321 for (i = 0; i < enic->intr_count; i++)
1322 vnic_intr_mask(&enic->intr[i]);
1324 for (i = 0; i < enic->wq_count; i++) {
1325 err = vnic_wq_disable(&enic->wq[i]);
1326 if (err)
1327 return err;
1329 for (i = 0; i < enic->rq_count; i++) {
1330 err = vnic_rq_disable(&enic->rq[i]);
1331 if (err)
1332 return err;
1335 vnic_dev_notify_unset(enic->vdev);
1336 enic_free_intr(enic);
1338 (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1339 -1, enic_rq_service_drop, NULL);
1340 (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1341 -1, enic_wq_service, NULL);
1343 for (i = 0; i < enic->wq_count; i++)
1344 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1345 for (i = 0; i < enic->rq_count; i++)
1346 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1347 for (i = 0; i < enic->cq_count; i++)
1348 vnic_cq_clean(&enic->cq[i]);
1349 for (i = 0; i < enic->intr_count; i++)
1350 vnic_intr_clean(&enic->intr[i]);
1352 return 0;
1355 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1357 struct enic *enic = netdev_priv(netdev);
1358 int running = netif_running(netdev);
1360 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1361 return -EINVAL;
1363 if (running)
1364 enic_stop(netdev);
1366 netdev->mtu = new_mtu;
1368 if (netdev->mtu > enic->port_mtu)
1369 printk(KERN_WARNING PFX
1370 "%s: interface MTU (%d) set higher "
1371 "than port MTU (%d)\n",
1372 netdev->name, netdev->mtu, enic->port_mtu);
1374 if (running)
1375 enic_open(netdev);
1377 return 0;
1380 #ifdef CONFIG_NET_POLL_CONTROLLER
1381 static void enic_poll_controller(struct net_device *netdev)
1383 struct enic *enic = netdev_priv(netdev);
1384 struct vnic_dev *vdev = enic->vdev;
1386 switch (vnic_dev_get_intr_mode(vdev)) {
1387 case VNIC_DEV_INTR_MODE_MSIX:
1388 enic_isr_msix_rq(enic->pdev->irq, enic);
1389 enic_isr_msix_wq(enic->pdev->irq, enic);
1390 break;
1391 case VNIC_DEV_INTR_MODE_MSI:
1392 enic_isr_msi(enic->pdev->irq, enic);
1393 break;
1394 case VNIC_DEV_INTR_MODE_INTX:
1395 enic_isr_legacy(enic->pdev->irq, netdev);
1396 break;
1397 default:
1398 break;
1401 #endif
1403 static int enic_dev_wait(struct vnic_dev *vdev,
1404 int (*start)(struct vnic_dev *, int),
1405 int (*finished)(struct vnic_dev *, int *),
1406 int arg)
1408 unsigned long time;
1409 int done;
1410 int err;
1412 BUG_ON(in_interrupt());
1414 err = start(vdev, arg);
1415 if (err)
1416 return err;
1418 /* Wait for func to complete...2 seconds max
1421 time = jiffies + (HZ * 2);
1422 do {
1424 err = finished(vdev, &done);
1425 if (err)
1426 return err;
1428 if (done)
1429 return 0;
1431 schedule_timeout_uninterruptible(HZ / 10);
1433 } while (time_after(time, jiffies));
1435 return -ETIMEDOUT;
1438 static int enic_dev_open(struct enic *enic)
1440 int err;
1442 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1443 vnic_dev_open_done, 0);
1444 if (err)
1445 printk(KERN_ERR PFX
1446 "vNIC device open failed, err %d.\n", err);
1448 return err;
1451 static int enic_dev_soft_reset(struct enic *enic)
1453 int err;
1455 err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
1456 vnic_dev_soft_reset_done, 0);
1457 if (err)
1458 printk(KERN_ERR PFX
1459 "vNIC soft reset failed, err %d.\n", err);
1461 return err;
1464 static void enic_reset(struct work_struct *work)
1466 struct enic *enic = container_of(work, struct enic, reset);
1468 if (!netif_running(enic->netdev))
1469 return;
1471 rtnl_lock();
1473 spin_lock(&enic->devcmd_lock);
1474 vnic_dev_hang_notify(enic->vdev);
1475 spin_unlock(&enic->devcmd_lock);
1477 enic_stop(enic->netdev);
1478 enic_dev_soft_reset(enic);
1479 enic_reset_mcaddrs(enic);
1480 enic_init_vnic_resources(enic);
1481 enic_open(enic->netdev);
1483 rtnl_unlock();
1486 static int enic_set_intr_mode(struct enic *enic)
1488 unsigned int n = ARRAY_SIZE(enic->rq);
1489 unsigned int m = ARRAY_SIZE(enic->wq);
1490 unsigned int i;
1492 /* Set interrupt mode (INTx, MSI, MSI-X) depending
1493 * system capabilities.
1495 * Try MSI-X first
1497 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1498 * (the second to last INTR is used for WQ/RQ errors)
1499 * (the last INTR is used for notifications)
1502 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
1503 for (i = 0; i < n + m + 2; i++)
1504 enic->msix_entry[i].entry = i;
1506 if (enic->config.intr_mode < 1 &&
1507 enic->rq_count >= n &&
1508 enic->wq_count >= m &&
1509 enic->cq_count >= n + m &&
1510 enic->intr_count >= n + m + 2 &&
1511 !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
1513 enic->rq_count = n;
1514 enic->wq_count = m;
1515 enic->cq_count = n + m;
1516 enic->intr_count = n + m + 2;
1518 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
1520 return 0;
1523 /* Next try MSI
1525 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
1528 if (enic->config.intr_mode < 2 &&
1529 enic->rq_count >= 1 &&
1530 enic->wq_count >= 1 &&
1531 enic->cq_count >= 2 &&
1532 enic->intr_count >= 1 &&
1533 !pci_enable_msi(enic->pdev)) {
1535 enic->rq_count = 1;
1536 enic->wq_count = 1;
1537 enic->cq_count = 2;
1538 enic->intr_count = 1;
1540 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
1542 return 0;
1545 /* Next try INTx
1547 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
1548 * (the first INTR is used for WQ/RQ)
1549 * (the second INTR is used for WQ/RQ errors)
1550 * (the last INTR is used for notifications)
1553 if (enic->config.intr_mode < 3 &&
1554 enic->rq_count >= 1 &&
1555 enic->wq_count >= 1 &&
1556 enic->cq_count >= 2 &&
1557 enic->intr_count >= 3) {
1559 enic->rq_count = 1;
1560 enic->wq_count = 1;
1561 enic->cq_count = 2;
1562 enic->intr_count = 3;
1564 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
1566 return 0;
1569 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1571 return -EINVAL;
1574 static void enic_clear_intr_mode(struct enic *enic)
1576 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1577 case VNIC_DEV_INTR_MODE_MSIX:
1578 pci_disable_msix(enic->pdev);
1579 break;
1580 case VNIC_DEV_INTR_MODE_MSI:
1581 pci_disable_msi(enic->pdev);
1582 break;
1583 default:
1584 break;
1587 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1590 static void enic_iounmap(struct enic *enic)
1592 if (enic->bar0.vaddr)
1593 iounmap(enic->bar0.vaddr);
1596 static const struct net_device_ops enic_netdev_ops = {
1597 .ndo_open = enic_open,
1598 .ndo_stop = enic_stop,
1599 .ndo_start_xmit = enic_hard_start_xmit,
1600 .ndo_get_stats = enic_get_stats,
1601 .ndo_validate_addr = eth_validate_addr,
1602 .ndo_set_mac_address = eth_mac_addr,
1603 .ndo_set_multicast_list = enic_set_multicast_list,
1604 .ndo_change_mtu = enic_change_mtu,
1605 .ndo_vlan_rx_register = enic_vlan_rx_register,
1606 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
1607 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
1608 .ndo_tx_timeout = enic_tx_timeout,
1609 #ifdef CONFIG_NET_POLL_CONTROLLER
1610 .ndo_poll_controller = enic_poll_controller,
1611 #endif
1614 static int __devinit enic_probe(struct pci_dev *pdev,
1615 const struct pci_device_id *ent)
1617 struct net_device *netdev;
1618 struct enic *enic;
1619 int using_dac = 0;
1620 unsigned int i;
1621 int err;
1623 const u8 rss_default_cpu = 0;
1624 const u8 rss_hash_type = 0;
1625 const u8 rss_hash_bits = 0;
1626 const u8 rss_base_cpu = 0;
1627 const u8 rss_enable = 0;
1628 const u8 tso_ipid_split_en = 0;
1629 const u8 ig_vlan_strip_en = 1;
1631 /* Allocate net device structure and initialize. Private
1632 * instance data is initialized to zero.
1635 netdev = alloc_etherdev(sizeof(struct enic));
1636 if (!netdev) {
1637 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
1638 return -ENOMEM;
1641 pci_set_drvdata(pdev, netdev);
1643 SET_NETDEV_DEV(netdev, &pdev->dev);
1645 enic = netdev_priv(netdev);
1646 enic->netdev = netdev;
1647 enic->pdev = pdev;
1649 /* Setup PCI resources
1652 err = pci_enable_device(pdev);
1653 if (err) {
1654 printk(KERN_ERR PFX
1655 "Cannot enable PCI device, aborting.\n");
1656 goto err_out_free_netdev;
1659 err = pci_request_regions(pdev, DRV_NAME);
1660 if (err) {
1661 printk(KERN_ERR PFX
1662 "Cannot request PCI regions, aborting.\n");
1663 goto err_out_disable_device;
1666 pci_set_master(pdev);
1668 /* Query PCI controller on system for DMA addressing
1669 * limitation for the device. Try 40-bit first, and
1670 * fail to 32-bit.
1673 err = pci_set_dma_mask(pdev, DMA_40BIT_MASK);
1674 if (err) {
1675 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1676 if (err) {
1677 printk(KERN_ERR PFX
1678 "No usable DMA configuration, aborting.\n");
1679 goto err_out_release_regions;
1681 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1682 if (err) {
1683 printk(KERN_ERR PFX
1684 "Unable to obtain 32-bit DMA "
1685 "for consistent allocations, aborting.\n");
1686 goto err_out_release_regions;
1688 } else {
1689 err = pci_set_consistent_dma_mask(pdev, DMA_40BIT_MASK);
1690 if (err) {
1691 printk(KERN_ERR PFX
1692 "Unable to obtain 40-bit DMA "
1693 "for consistent allocations, aborting.\n");
1694 goto err_out_release_regions;
1696 using_dac = 1;
1699 /* Map vNIC resources from BAR0
1702 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1703 printk(KERN_ERR PFX
1704 "BAR0 not memory-map'able, aborting.\n");
1705 err = -ENODEV;
1706 goto err_out_release_regions;
1709 enic->bar0.vaddr = pci_iomap(pdev, 0, enic->bar0.len);
1710 enic->bar0.bus_addr = pci_resource_start(pdev, 0);
1711 enic->bar0.len = pci_resource_len(pdev, 0);
1713 if (!enic->bar0.vaddr) {
1714 printk(KERN_ERR PFX
1715 "Cannot memory-map BAR0 res hdr, aborting.\n");
1716 err = -ENODEV;
1717 goto err_out_release_regions;
1720 /* Register vNIC device
1723 enic->vdev = vnic_dev_register(NULL, enic, pdev, &enic->bar0);
1724 if (!enic->vdev) {
1725 printk(KERN_ERR PFX
1726 "vNIC registration failed, aborting.\n");
1727 err = -ENODEV;
1728 goto err_out_iounmap;
1731 /* Issue device open to get device in known state
1734 err = enic_dev_open(enic);
1735 if (err) {
1736 printk(KERN_ERR PFX
1737 "vNIC dev open failed, aborting.\n");
1738 goto err_out_vnic_unregister;
1741 /* Issue device init to initialize the vnic-to-switch link.
1742 * We'll start with carrier off and wait for link UP
1743 * notification later to turn on carrier. We don't need
1744 * to wait here for the vnic-to-switch link initialization
1745 * to complete; link UP notification is the indication that
1746 * the process is complete.
1749 netif_carrier_off(netdev);
1751 err = vnic_dev_init(enic->vdev, 0);
1752 if (err) {
1753 printk(KERN_ERR PFX
1754 "vNIC dev init failed, aborting.\n");
1755 goto err_out_dev_close;
1758 /* Get vNIC configuration
1761 err = enic_get_vnic_config(enic);
1762 if (err) {
1763 printk(KERN_ERR PFX
1764 "Get vNIC configuration failed, aborting.\n");
1765 goto err_out_dev_close;
1768 /* Get available resource counts
1771 enic_get_res_counts(enic);
1773 /* Set interrupt mode based on resource counts and system
1774 * capabilities
1777 err = enic_set_intr_mode(enic);
1778 if (err) {
1779 printk(KERN_ERR PFX
1780 "Failed to set intr mode, aborting.\n");
1781 goto err_out_dev_close;
1784 /* Allocate and configure vNIC resources
1787 err = enic_alloc_vnic_resources(enic);
1788 if (err) {
1789 printk(KERN_ERR PFX
1790 "Failed to alloc vNIC resources, aborting.\n");
1791 goto err_out_free_vnic_resources;
1794 enic_init_vnic_resources(enic);
1796 /* Enable VLAN tag stripping. RSS not enabled (yet).
1799 err = enic_set_nic_cfg(enic,
1800 rss_default_cpu, rss_hash_type,
1801 rss_hash_bits, rss_base_cpu,
1802 rss_enable, tso_ipid_split_en,
1803 ig_vlan_strip_en);
1804 if (err) {
1805 printk(KERN_ERR PFX
1806 "Failed to config nic, aborting.\n");
1807 goto err_out_free_vnic_resources;
1810 /* Setup notification timer, HW reset task, and locks
1813 init_timer(&enic->notify_timer);
1814 enic->notify_timer.function = enic_notify_timer;
1815 enic->notify_timer.data = (unsigned long)enic;
1817 INIT_WORK(&enic->reset, enic_reset);
1819 for (i = 0; i < enic->wq_count; i++)
1820 spin_lock_init(&enic->wq_lock[i]);
1822 spin_lock_init(&enic->devcmd_lock);
1824 /* Register net device
1827 enic->port_mtu = enic->config.mtu;
1828 (void)enic_change_mtu(netdev, enic->port_mtu);
1830 err = enic_set_mac_addr(netdev, enic->mac_addr);
1831 if (err) {
1832 printk(KERN_ERR PFX
1833 "Invalid MAC address, aborting.\n");
1834 goto err_out_free_vnic_resources;
1837 netdev->netdev_ops = &enic_netdev_ops;
1838 netdev->watchdog_timeo = 2 * HZ;
1839 netdev->ethtool_ops = &enic_ethtool_ops;
1841 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1842 default:
1843 netif_napi_add(netdev, &enic->napi, enic_poll, 64);
1844 break;
1845 case VNIC_DEV_INTR_MODE_MSIX:
1846 netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
1847 break;
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;
1856 if (ENIC_SETTING(enic, LRO))
1857 netdev->features |= NETIF_F_LRO;
1858 if (using_dac)
1859 netdev->features |= NETIF_F_HIGHDMA;
1862 enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
1864 enic->lro_mgr.max_aggr = ENIC_LRO_MAX_AGGR;
1865 enic->lro_mgr.max_desc = ENIC_LRO_MAX_DESC;
1866 enic->lro_mgr.lro_arr = enic->lro_desc;
1867 enic->lro_mgr.get_skb_header = enic_get_skb_header;
1868 enic->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1869 enic->lro_mgr.dev = netdev;
1870 enic->lro_mgr.ip_summed = CHECKSUM_COMPLETE;
1871 enic->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1874 err = register_netdev(netdev);
1875 if (err) {
1876 printk(KERN_ERR PFX
1877 "Cannot register net device, aborting.\n");
1878 goto err_out_free_vnic_resources;
1881 return 0;
1883 err_out_free_vnic_resources:
1884 enic_free_vnic_resources(enic);
1885 err_out_dev_close:
1886 vnic_dev_close(enic->vdev);
1887 err_out_vnic_unregister:
1888 enic_clear_intr_mode(enic);
1889 vnic_dev_unregister(enic->vdev);
1890 err_out_iounmap:
1891 enic_iounmap(enic);
1892 err_out_release_regions:
1893 pci_release_regions(pdev);
1894 err_out_disable_device:
1895 pci_disable_device(pdev);
1896 err_out_free_netdev:
1897 pci_set_drvdata(pdev, NULL);
1898 free_netdev(netdev);
1900 return err;
1903 static void __devexit enic_remove(struct pci_dev *pdev)
1905 struct net_device *netdev = pci_get_drvdata(pdev);
1907 if (netdev) {
1908 struct enic *enic = netdev_priv(netdev);
1910 flush_scheduled_work();
1911 unregister_netdev(netdev);
1912 enic_free_vnic_resources(enic);
1913 vnic_dev_close(enic->vdev);
1914 enic_clear_intr_mode(enic);
1915 vnic_dev_unregister(enic->vdev);
1916 enic_iounmap(enic);
1917 pci_release_regions(pdev);
1918 pci_disable_device(pdev);
1919 pci_set_drvdata(pdev, NULL);
1920 free_netdev(netdev);
1924 static struct pci_driver enic_driver = {
1925 .name = DRV_NAME,
1926 .id_table = enic_id_table,
1927 .probe = enic_probe,
1928 .remove = __devexit_p(enic_remove),
1931 static int __init enic_init_module(void)
1933 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1935 return pci_register_driver(&enic_driver);
1938 static void __exit enic_cleanup_module(void)
1940 pci_unregister_driver(&enic_driver);
1943 module_init(enic_init_module);
1944 module_exit(enic_cleanup_module);