RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / enic / enic_main.c
blob09ae9481185a232a5f189fb727da6e777736b1d3
1 /*
2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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 <linux/rtnetlink.h>
38 #include <net/ip6_checksum.h>
40 #include "cq_enet_desc.h"
41 #include "vnic_dev.h"
42 #include "vnic_intr.h"
43 #include "vnic_stats.h"
44 #include "vnic_vic.h"
45 #include "enic_res.h"
46 #include "enic.h"
48 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
49 #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
50 #define MAX_TSO (1 << 16)
51 #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
53 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
54 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
56 /* Supported devices */
57 static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
58 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
59 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
60 { 0, } /* end of table */
63 MODULE_DESCRIPTION(DRV_DESCRIPTION);
64 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
65 MODULE_LICENSE("GPL");
66 MODULE_VERSION(DRV_VERSION);
67 MODULE_DEVICE_TABLE(pci, enic_id_table);
69 struct enic_stat {
70 char name[ETH_GSTRING_LEN];
71 unsigned int offset;
74 #define ENIC_TX_STAT(stat) \
75 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
76 #define ENIC_RX_STAT(stat) \
77 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
79 static const struct enic_stat enic_tx_stats[] = {
80 ENIC_TX_STAT(tx_frames_ok),
81 ENIC_TX_STAT(tx_unicast_frames_ok),
82 ENIC_TX_STAT(tx_multicast_frames_ok),
83 ENIC_TX_STAT(tx_broadcast_frames_ok),
84 ENIC_TX_STAT(tx_bytes_ok),
85 ENIC_TX_STAT(tx_unicast_bytes_ok),
86 ENIC_TX_STAT(tx_multicast_bytes_ok),
87 ENIC_TX_STAT(tx_broadcast_bytes_ok),
88 ENIC_TX_STAT(tx_drops),
89 ENIC_TX_STAT(tx_errors),
90 ENIC_TX_STAT(tx_tso),
93 static const struct enic_stat enic_rx_stats[] = {
94 ENIC_RX_STAT(rx_frames_ok),
95 ENIC_RX_STAT(rx_frames_total),
96 ENIC_RX_STAT(rx_unicast_frames_ok),
97 ENIC_RX_STAT(rx_multicast_frames_ok),
98 ENIC_RX_STAT(rx_broadcast_frames_ok),
99 ENIC_RX_STAT(rx_bytes_ok),
100 ENIC_RX_STAT(rx_unicast_bytes_ok),
101 ENIC_RX_STAT(rx_multicast_bytes_ok),
102 ENIC_RX_STAT(rx_broadcast_bytes_ok),
103 ENIC_RX_STAT(rx_drop),
104 ENIC_RX_STAT(rx_no_bufs),
105 ENIC_RX_STAT(rx_errors),
106 ENIC_RX_STAT(rx_rss),
107 ENIC_RX_STAT(rx_crc_errors),
108 ENIC_RX_STAT(rx_frames_64),
109 ENIC_RX_STAT(rx_frames_127),
110 ENIC_RX_STAT(rx_frames_255),
111 ENIC_RX_STAT(rx_frames_511),
112 ENIC_RX_STAT(rx_frames_1023),
113 ENIC_RX_STAT(rx_frames_1518),
114 ENIC_RX_STAT(rx_frames_to_max),
117 static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
118 static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
120 static int enic_is_dynamic(struct enic *enic)
122 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
125 static int enic_get_settings(struct net_device *netdev,
126 struct ethtool_cmd *ecmd)
128 struct enic *enic = netdev_priv(netdev);
130 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
131 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
132 ecmd->port = PORT_FIBRE;
133 ecmd->transceiver = XCVR_EXTERNAL;
135 if (netif_carrier_ok(netdev)) {
136 ecmd->speed = vnic_dev_port_speed(enic->vdev);
137 ecmd->duplex = DUPLEX_FULL;
138 } else {
139 ecmd->speed = -1;
140 ecmd->duplex = -1;
143 ecmd->autoneg = AUTONEG_DISABLE;
145 return 0;
148 static int enic_dev_fw_info(struct enic *enic,
149 struct vnic_devcmd_fw_info **fw_info)
151 int err;
153 spin_lock(&enic->devcmd_lock);
154 err = vnic_dev_fw_info(enic->vdev, fw_info);
155 spin_unlock(&enic->devcmd_lock);
157 return err;
160 static void enic_get_drvinfo(struct net_device *netdev,
161 struct ethtool_drvinfo *drvinfo)
163 struct enic *enic = netdev_priv(netdev);
164 struct vnic_devcmd_fw_info *fw_info;
166 enic_dev_fw_info(enic, &fw_info);
168 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
169 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
170 strncpy(drvinfo->fw_version, fw_info->fw_version,
171 sizeof(drvinfo->fw_version));
172 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
173 sizeof(drvinfo->bus_info));
176 static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
178 unsigned int i;
180 switch (stringset) {
181 case ETH_SS_STATS:
182 for (i = 0; i < enic_n_tx_stats; i++) {
183 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
184 data += ETH_GSTRING_LEN;
186 for (i = 0; i < enic_n_rx_stats; i++) {
187 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
188 data += ETH_GSTRING_LEN;
190 break;
194 static int enic_get_sset_count(struct net_device *netdev, int sset)
196 switch (sset) {
197 case ETH_SS_STATS:
198 return enic_n_tx_stats + enic_n_rx_stats;
199 default:
200 return -EOPNOTSUPP;
204 static int enic_dev_stats_dump(struct enic *enic, struct vnic_stats **vstats)
206 int err;
208 spin_lock(&enic->devcmd_lock);
209 err = vnic_dev_stats_dump(enic->vdev, vstats);
210 spin_unlock(&enic->devcmd_lock);
212 return err;
215 static void enic_get_ethtool_stats(struct net_device *netdev,
216 struct ethtool_stats *stats, u64 *data)
218 struct enic *enic = netdev_priv(netdev);
219 struct vnic_stats *vstats;
220 unsigned int i;
222 enic_dev_stats_dump(enic, &vstats);
224 for (i = 0; i < enic_n_tx_stats; i++)
225 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
226 for (i = 0; i < enic_n_rx_stats; i++)
227 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
230 static u32 enic_get_rx_csum(struct net_device *netdev)
232 struct enic *enic = netdev_priv(netdev);
233 return enic->csum_rx_enabled;
236 static int enic_set_rx_csum(struct net_device *netdev, u32 data)
238 struct enic *enic = netdev_priv(netdev);
240 if (data && !ENIC_SETTING(enic, RXCSUM))
241 return -EINVAL;
243 enic->csum_rx_enabled = !!data;
245 return 0;
248 static int enic_set_tx_csum(struct net_device *netdev, u32 data)
250 struct enic *enic = netdev_priv(netdev);
252 if (data && !ENIC_SETTING(enic, TXCSUM))
253 return -EINVAL;
255 if (data)
256 netdev->features |= NETIF_F_HW_CSUM;
257 else
258 netdev->features &= ~NETIF_F_HW_CSUM;
260 return 0;
263 static int enic_set_tso(struct net_device *netdev, u32 data)
265 struct enic *enic = netdev_priv(netdev);
267 if (data && !ENIC_SETTING(enic, TSO))
268 return -EINVAL;
270 if (data)
271 netdev->features |=
272 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
273 else
274 netdev->features &=
275 ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
277 return 0;
280 static u32 enic_get_msglevel(struct net_device *netdev)
282 struct enic *enic = netdev_priv(netdev);
283 return enic->msg_enable;
286 static void enic_set_msglevel(struct net_device *netdev, u32 value)
288 struct enic *enic = netdev_priv(netdev);
289 enic->msg_enable = value;
292 static int enic_get_coalesce(struct net_device *netdev,
293 struct ethtool_coalesce *ecmd)
295 struct enic *enic = netdev_priv(netdev);
297 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
298 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
300 return 0;
303 static int enic_set_coalesce(struct net_device *netdev,
304 struct ethtool_coalesce *ecmd)
306 struct enic *enic = netdev_priv(netdev);
307 u32 tx_coalesce_usecs;
308 u32 rx_coalesce_usecs;
310 tx_coalesce_usecs = min_t(u32,
311 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
312 ecmd->tx_coalesce_usecs);
313 rx_coalesce_usecs = min_t(u32,
314 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
315 ecmd->rx_coalesce_usecs);
317 switch (vnic_dev_get_intr_mode(enic->vdev)) {
318 case VNIC_DEV_INTR_MODE_INTX:
319 if (tx_coalesce_usecs != rx_coalesce_usecs)
320 return -EINVAL;
322 vnic_intr_coalescing_timer_set(&enic->intr[ENIC_INTX_WQ_RQ],
323 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
324 break;
325 case VNIC_DEV_INTR_MODE_MSI:
326 if (tx_coalesce_usecs != rx_coalesce_usecs)
327 return -EINVAL;
329 vnic_intr_coalescing_timer_set(&enic->intr[0],
330 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
331 break;
332 case VNIC_DEV_INTR_MODE_MSIX:
333 vnic_intr_coalescing_timer_set(&enic->intr[ENIC_MSIX_WQ],
334 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
335 vnic_intr_coalescing_timer_set(&enic->intr[ENIC_MSIX_RQ],
336 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
337 break;
338 default:
339 break;
342 enic->tx_coalesce_usecs = tx_coalesce_usecs;
343 enic->rx_coalesce_usecs = rx_coalesce_usecs;
345 return 0;
348 static const struct ethtool_ops enic_ethtool_ops = {
349 .get_settings = enic_get_settings,
350 .get_drvinfo = enic_get_drvinfo,
351 .get_msglevel = enic_get_msglevel,
352 .set_msglevel = enic_set_msglevel,
353 .get_link = ethtool_op_get_link,
354 .get_strings = enic_get_strings,
355 .get_sset_count = enic_get_sset_count,
356 .get_ethtool_stats = enic_get_ethtool_stats,
357 .get_rx_csum = enic_get_rx_csum,
358 .set_rx_csum = enic_set_rx_csum,
359 .get_tx_csum = ethtool_op_get_tx_csum,
360 .set_tx_csum = enic_set_tx_csum,
361 .get_sg = ethtool_op_get_sg,
362 .set_sg = ethtool_op_set_sg,
363 .get_tso = ethtool_op_get_tso,
364 .set_tso = enic_set_tso,
365 .get_coalesce = enic_get_coalesce,
366 .set_coalesce = enic_set_coalesce,
367 .get_flags = ethtool_op_get_flags,
370 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
372 struct enic *enic = vnic_dev_priv(wq->vdev);
374 if (buf->sop)
375 pci_unmap_single(enic->pdev, buf->dma_addr,
376 buf->len, PCI_DMA_TODEVICE);
377 else
378 pci_unmap_page(enic->pdev, buf->dma_addr,
379 buf->len, PCI_DMA_TODEVICE);
381 if (buf->os_buf)
382 dev_kfree_skb_any(buf->os_buf);
385 static void enic_wq_free_buf(struct vnic_wq *wq,
386 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
388 enic_free_wq_buf(wq, buf);
391 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
392 u8 type, u16 q_number, u16 completed_index, void *opaque)
394 struct enic *enic = vnic_dev_priv(vdev);
396 spin_lock(&enic->wq_lock[q_number]);
398 vnic_wq_service(&enic->wq[q_number], cq_desc,
399 completed_index, enic_wq_free_buf,
400 opaque);
402 if (netif_queue_stopped(enic->netdev) &&
403 vnic_wq_desc_avail(&enic->wq[q_number]) >=
404 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
405 netif_wake_queue(enic->netdev);
407 spin_unlock(&enic->wq_lock[q_number]);
409 return 0;
412 static void enic_log_q_error(struct enic *enic)
414 unsigned int i;
415 u32 error_status;
417 for (i = 0; i < enic->wq_count; i++) {
418 error_status = vnic_wq_error_status(&enic->wq[i]);
419 if (error_status)
420 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
421 i, error_status);
424 for (i = 0; i < enic->rq_count; i++) {
425 error_status = vnic_rq_error_status(&enic->rq[i]);
426 if (error_status)
427 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
428 i, error_status);
432 static void enic_msglvl_check(struct enic *enic)
434 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
436 if (msg_enable != enic->msg_enable) {
437 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
438 enic->msg_enable, msg_enable);
439 enic->msg_enable = msg_enable;
443 static void enic_mtu_check(struct enic *enic)
445 u32 mtu = vnic_dev_mtu(enic->vdev);
446 struct net_device *netdev = enic->netdev;
448 if (mtu && mtu != enic->port_mtu) {
449 enic->port_mtu = mtu;
450 if (mtu < netdev->mtu)
451 netdev_warn(netdev,
452 "interface MTU (%d) set higher "
453 "than switch port MTU (%d)\n",
454 netdev->mtu, mtu);
458 static void enic_link_check(struct enic *enic)
460 int link_status = vnic_dev_link_status(enic->vdev);
461 int carrier_ok = netif_carrier_ok(enic->netdev);
463 if (link_status && !carrier_ok) {
464 netdev_info(enic->netdev, "Link UP\n");
465 netif_carrier_on(enic->netdev);
466 } else if (!link_status && carrier_ok) {
467 netdev_info(enic->netdev, "Link DOWN\n");
468 netif_carrier_off(enic->netdev);
472 static void enic_notify_check(struct enic *enic)
474 enic_msglvl_check(enic);
475 enic_mtu_check(enic);
476 enic_link_check(enic);
479 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
481 static irqreturn_t enic_isr_legacy(int irq, void *data)
483 struct net_device *netdev = data;
484 struct enic *enic = netdev_priv(netdev);
485 u32 pba;
487 vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]);
489 pba = vnic_intr_legacy_pba(enic->legacy_pba);
490 if (!pba) {
491 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
492 return IRQ_NONE; /* not our interrupt */
495 if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY)) {
496 vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_NOTIFY]);
497 enic_notify_check(enic);
500 if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) {
501 vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_ERR]);
502 enic_log_q_error(enic);
503 /* schedule recovery from WQ/RQ error */
504 schedule_work(&enic->reset);
505 return IRQ_HANDLED;
508 if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) {
509 if (napi_schedule_prep(&enic->napi))
510 __napi_schedule(&enic->napi);
511 } else {
512 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
515 return IRQ_HANDLED;
518 static irqreturn_t enic_isr_msi(int irq, void *data)
520 struct enic *enic = data;
522 /* With MSI, there is no sharing of interrupts, so this is
523 * our interrupt and there is no need to ack it. The device
524 * is not providing per-vector masking, so the OS will not
525 * write to PCI config space to mask/unmask the interrupt.
526 * We're using mask_on_assertion for MSI, so the device
527 * automatically masks the interrupt when the interrupt is
528 * generated. Later, when exiting polling, the interrupt
529 * will be unmasked (see enic_poll).
531 * Also, the device uses the same PCIe Traffic Class (TC)
532 * for Memory Write data and MSI, so there are no ordering
533 * issues; the MSI will always arrive at the Root Complex
534 * _after_ corresponding Memory Writes (i.e. descriptor
535 * writes).
538 napi_schedule(&enic->napi);
540 return IRQ_HANDLED;
543 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
545 struct enic *enic = data;
547 /* schedule NAPI polling for RQ cleanup */
548 napi_schedule(&enic->napi);
550 return IRQ_HANDLED;
553 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
555 struct enic *enic = data;
556 unsigned int wq_work_to_do = -1; /* no limit */
557 unsigned int wq_work_done;
559 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
560 wq_work_to_do, enic_wq_service, NULL);
562 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ],
563 wq_work_done,
564 1 /* unmask intr */,
565 1 /* reset intr timer */);
567 return IRQ_HANDLED;
570 static irqreturn_t enic_isr_msix_err(int irq, void *data)
572 struct enic *enic = data;
574 vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_ERR]);
576 enic_log_q_error(enic);
578 /* schedule recovery from WQ/RQ error */
579 schedule_work(&enic->reset);
581 return IRQ_HANDLED;
584 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
586 struct enic *enic = data;
588 vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_NOTIFY]);
589 enic_notify_check(enic);
591 return IRQ_HANDLED;
594 static inline void enic_queue_wq_skb_cont(struct enic *enic,
595 struct vnic_wq *wq, struct sk_buff *skb,
596 unsigned int len_left, int loopback)
598 skb_frag_t *frag;
600 /* Queue additional data fragments */
601 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
602 len_left -= frag->size;
603 enic_queue_wq_desc_cont(wq, skb,
604 pci_map_page(enic->pdev, frag->page,
605 frag->page_offset, frag->size,
606 PCI_DMA_TODEVICE),
607 frag->size,
608 (len_left == 0), /* EOP? */
609 loopback);
613 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
614 struct vnic_wq *wq, struct sk_buff *skb,
615 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
617 unsigned int head_len = skb_headlen(skb);
618 unsigned int len_left = skb->len - head_len;
619 int eop = (len_left == 0);
621 /* Queue the main skb fragment. The fragments are no larger
622 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
623 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
624 * per fragment is queued.
626 enic_queue_wq_desc(wq, skb,
627 pci_map_single(enic->pdev, skb->data,
628 head_len, PCI_DMA_TODEVICE),
629 head_len,
630 vlan_tag_insert, vlan_tag,
631 eop, loopback);
633 if (!eop)
634 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
637 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
638 struct vnic_wq *wq, struct sk_buff *skb,
639 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
641 unsigned int head_len = skb_headlen(skb);
642 unsigned int len_left = skb->len - head_len;
643 unsigned int hdr_len = skb_transport_offset(skb);
644 unsigned int csum_offset = hdr_len + skb->csum_offset;
645 int eop = (len_left == 0);
647 /* Queue the main skb fragment. The fragments are no larger
648 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
649 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
650 * per fragment is queued.
652 enic_queue_wq_desc_csum_l4(wq, skb,
653 pci_map_single(enic->pdev, skb->data,
654 head_len, PCI_DMA_TODEVICE),
655 head_len,
656 csum_offset,
657 hdr_len,
658 vlan_tag_insert, vlan_tag,
659 eop, loopback);
661 if (!eop)
662 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
665 static inline void enic_queue_wq_skb_tso(struct enic *enic,
666 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
667 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
669 unsigned int frag_len_left = skb_headlen(skb);
670 unsigned int len_left = skb->len - frag_len_left;
671 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
672 int eop = (len_left == 0);
673 unsigned int len;
674 dma_addr_t dma_addr;
675 unsigned int offset = 0;
676 skb_frag_t *frag;
678 /* Preload TCP csum field with IP pseudo hdr calculated
679 * with IP length set to zero. HW will later add in length
680 * to each TCP segment resulting from the TSO.
683 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
684 ip_hdr(skb)->check = 0;
685 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
686 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
687 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
688 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
689 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
692 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
693 * for the main skb fragment
695 while (frag_len_left) {
696 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
697 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
698 len, PCI_DMA_TODEVICE);
699 enic_queue_wq_desc_tso(wq, skb,
700 dma_addr,
701 len,
702 mss, hdr_len,
703 vlan_tag_insert, vlan_tag,
704 eop && (len == frag_len_left), loopback);
705 frag_len_left -= len;
706 offset += len;
709 if (eop)
710 return;
712 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
713 * for additional data fragments
715 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
716 len_left -= frag->size;
717 frag_len_left = frag->size;
718 offset = frag->page_offset;
720 while (frag_len_left) {
721 len = min(frag_len_left,
722 (unsigned int)WQ_ENET_MAX_DESC_LEN);
723 dma_addr = pci_map_page(enic->pdev, frag->page,
724 offset, len,
725 PCI_DMA_TODEVICE);
726 enic_queue_wq_desc_cont(wq, skb,
727 dma_addr,
728 len,
729 (len_left == 0) &&
730 (len == frag_len_left), /* EOP? */
731 loopback);
732 frag_len_left -= len;
733 offset += len;
738 static inline void enic_queue_wq_skb(struct enic *enic,
739 struct vnic_wq *wq, struct sk_buff *skb)
741 unsigned int mss = skb_shinfo(skb)->gso_size;
742 unsigned int vlan_tag = 0;
743 int vlan_tag_insert = 0;
744 int loopback = 0;
746 if (enic->vlan_group && vlan_tx_tag_present(skb)) {
747 /* VLAN tag from trunking driver */
748 vlan_tag_insert = 1;
749 vlan_tag = vlan_tx_tag_get(skb);
750 } else if (enic->loop_enable) {
751 vlan_tag = enic->loop_tag;
752 loopback = 1;
755 if (mss)
756 enic_queue_wq_skb_tso(enic, wq, skb, mss,
757 vlan_tag_insert, vlan_tag, loopback);
758 else if (skb->ip_summed == CHECKSUM_PARTIAL)
759 enic_queue_wq_skb_csum_l4(enic, wq, skb,
760 vlan_tag_insert, vlan_tag, loopback);
761 else
762 enic_queue_wq_skb_vlan(enic, wq, skb,
763 vlan_tag_insert, vlan_tag, loopback);
766 /* netif_tx_lock held, process context with BHs disabled, or BH */
767 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
768 struct net_device *netdev)
770 struct enic *enic = netdev_priv(netdev);
771 struct vnic_wq *wq = &enic->wq[0];
772 unsigned long flags;
774 if (skb->len <= 0) {
775 dev_kfree_skb(skb);
776 return NETDEV_TX_OK;
779 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
780 * which is very likely. In the off chance it's going to take
781 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
784 if (skb_shinfo(skb)->gso_size == 0 &&
785 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
786 skb_linearize(skb)) {
787 dev_kfree_skb(skb);
788 return NETDEV_TX_OK;
791 spin_lock_irqsave(&enic->wq_lock[0], flags);
793 if (vnic_wq_desc_avail(wq) <
794 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
795 netif_stop_queue(netdev);
796 /* This is a hard error, log it */
797 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
798 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
799 return NETDEV_TX_BUSY;
802 enic_queue_wq_skb(enic, wq, skb);
804 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
805 netif_stop_queue(netdev);
807 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
809 return NETDEV_TX_OK;
812 /* dev_base_lock rwlock held, nominally process context */
813 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
815 struct enic *enic = netdev_priv(netdev);
816 struct net_device_stats *net_stats = &netdev->stats;
817 struct vnic_stats *stats;
819 enic_dev_stats_dump(enic, &stats);
821 net_stats->tx_packets = stats->tx.tx_frames_ok;
822 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
823 net_stats->tx_errors = stats->tx.tx_errors;
824 net_stats->tx_dropped = stats->tx.tx_drops;
826 net_stats->rx_packets = stats->rx.rx_frames_ok;
827 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
828 net_stats->rx_errors = stats->rx.rx_errors;
829 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
830 net_stats->rx_over_errors = enic->rq_truncated_pkts;
831 net_stats->rx_crc_errors = enic->rq_bad_fcs;
832 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
834 return net_stats;
837 static void enic_reset_multicast_list(struct enic *enic)
839 enic->mc_count = 0;
840 enic->flags = 0;
843 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
845 struct enic *enic = netdev_priv(netdev);
847 if (enic_is_dynamic(enic)) {
848 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
849 return -EADDRNOTAVAIL;
850 } else {
851 if (!is_valid_ether_addr(addr))
852 return -EADDRNOTAVAIL;
855 memcpy(netdev->dev_addr, addr, netdev->addr_len);
857 return 0;
860 static int enic_dev_add_station_addr(struct enic *enic)
862 int err = 0;
864 if (is_valid_ether_addr(enic->netdev->dev_addr)) {
865 spin_lock(&enic->devcmd_lock);
866 err = vnic_dev_add_addr(enic->vdev, enic->netdev->dev_addr);
867 spin_unlock(&enic->devcmd_lock);
870 return err;
873 static int enic_dev_del_station_addr(struct enic *enic)
875 int err = 0;
877 if (is_valid_ether_addr(enic->netdev->dev_addr)) {
878 spin_lock(&enic->devcmd_lock);
879 err = vnic_dev_del_addr(enic->vdev, enic->netdev->dev_addr);
880 spin_unlock(&enic->devcmd_lock);
883 return err;
886 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
888 struct enic *enic = netdev_priv(netdev);
889 struct sockaddr *saddr = p;
890 char *addr = saddr->sa_data;
891 int err;
893 if (netif_running(enic->netdev)) {
894 err = enic_dev_del_station_addr(enic);
895 if (err)
896 return err;
899 err = enic_set_mac_addr(netdev, addr);
900 if (err)
901 return err;
903 if (netif_running(enic->netdev)) {
904 err = enic_dev_add_station_addr(enic);
905 if (err)
906 return err;
909 return err;
912 static int enic_set_mac_address(struct net_device *netdev, void *p)
914 return -EOPNOTSUPP;
917 static int enic_dev_packet_filter(struct enic *enic, int directed,
918 int multicast, int broadcast, int promisc, int allmulti)
920 int err;
922 spin_lock(&enic->devcmd_lock);
923 err = vnic_dev_packet_filter(enic->vdev, directed,
924 multicast, broadcast, promisc, allmulti);
925 spin_unlock(&enic->devcmd_lock);
927 return err;
930 static int enic_dev_add_multicast_addr(struct enic *enic, u8 *addr)
932 int err;
934 spin_lock(&enic->devcmd_lock);
935 err = vnic_dev_add_addr(enic->vdev, addr);
936 spin_unlock(&enic->devcmd_lock);
938 return err;
941 static int enic_dev_del_multicast_addr(struct enic *enic, u8 *addr)
943 int err;
945 spin_lock(&enic->devcmd_lock);
946 err = vnic_dev_del_addr(enic->vdev, addr);
947 spin_unlock(&enic->devcmd_lock);
949 return err;
952 /* netif_tx_lock held, BHs disabled */
953 static void enic_set_multicast_list(struct net_device *netdev)
955 struct enic *enic = netdev_priv(netdev);
956 struct netdev_hw_addr *ha;
957 int directed = 1;
958 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
959 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
960 int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0;
961 unsigned int mc_count = netdev_mc_count(netdev);
962 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
963 mc_count > ENIC_MULTICAST_PERFECT_FILTERS;
964 unsigned int flags = netdev->flags | (allmulti ? IFF_ALLMULTI : 0);
965 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
966 unsigned int i, j;
968 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS)
969 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
971 if (enic->flags != flags) {
972 enic->flags = flags;
973 enic_dev_packet_filter(enic, directed,
974 multicast, broadcast, promisc, allmulti);
977 /* Is there an easier way? Trying to minimize to
978 * calls to add/del multicast addrs. We keep the
979 * addrs from the last call in enic->mc_addr and
980 * look for changes to add/del.
983 i = 0;
984 netdev_for_each_mc_addr(ha, netdev) {
985 if (i == mc_count)
986 break;
987 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
990 for (i = 0; i < enic->mc_count; i++) {
991 for (j = 0; j < mc_count; j++)
992 if (compare_ether_addr(enic->mc_addr[i],
993 mc_addr[j]) == 0)
994 break;
995 if (j == mc_count)
996 enic_dev_del_multicast_addr(enic, enic->mc_addr[i]);
999 for (i = 0; i < mc_count; i++) {
1000 for (j = 0; j < enic->mc_count; j++)
1001 if (compare_ether_addr(mc_addr[i],
1002 enic->mc_addr[j]) == 0)
1003 break;
1004 if (j == enic->mc_count)
1005 enic_dev_add_multicast_addr(enic, mc_addr[i]);
1008 /* Save the list to compare against next time
1011 for (i = 0; i < mc_count; i++)
1012 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
1014 enic->mc_count = mc_count;
1017 /* rtnl lock is held */
1018 static void enic_vlan_rx_register(struct net_device *netdev,
1019 struct vlan_group *vlan_group)
1021 struct enic *enic = netdev_priv(netdev);
1022 enic->vlan_group = vlan_group;
1025 /* rtnl lock is held */
1026 static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
1028 struct enic *enic = netdev_priv(netdev);
1030 spin_lock(&enic->devcmd_lock);
1031 enic_add_vlan(enic, vid);
1032 spin_unlock(&enic->devcmd_lock);
1035 /* rtnl lock is held */
1036 static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
1038 struct enic *enic = netdev_priv(netdev);
1040 spin_lock(&enic->devcmd_lock);
1041 enic_del_vlan(enic, vid);
1042 spin_unlock(&enic->devcmd_lock);
1045 /* netif_tx_lock held, BHs disabled */
1046 static void enic_tx_timeout(struct net_device *netdev)
1048 struct enic *enic = netdev_priv(netdev);
1049 schedule_work(&enic->reset);
1052 static int enic_vnic_dev_deinit(struct enic *enic)
1054 int err;
1056 spin_lock(&enic->devcmd_lock);
1057 err = vnic_dev_deinit(enic->vdev);
1058 spin_unlock(&enic->devcmd_lock);
1060 return err;
1063 static int enic_dev_init_prov(struct enic *enic, struct vic_provinfo *vp)
1065 int err;
1067 spin_lock(&enic->devcmd_lock);
1068 err = vnic_dev_init_prov(enic->vdev,
1069 (u8 *)vp, vic_provinfo_size(vp));
1070 spin_unlock(&enic->devcmd_lock);
1072 return err;
1075 static int enic_dev_init_done(struct enic *enic, int *done, int *error)
1077 int err;
1079 spin_lock(&enic->devcmd_lock);
1080 err = vnic_dev_init_done(enic->vdev, done, error);
1081 spin_unlock(&enic->devcmd_lock);
1083 return err;
1086 static int enic_set_port_profile(struct enic *enic, u8 *mac)
1088 struct vic_provinfo *vp;
1089 u8 oui[3] = VIC_PROVINFO_CISCO_OUI;
1090 char uuid_str[38];
1091 int err;
1093 err = enic_vnic_dev_deinit(enic);
1094 if (err)
1095 return err;
1097 switch (enic->pp.request) {
1099 case PORT_REQUEST_ASSOCIATE:
1101 if (!(enic->pp.set & ENIC_SET_NAME) || !strlen(enic->pp.name))
1102 return -EINVAL;
1104 if (!is_valid_ether_addr(mac))
1105 return -EADDRNOTAVAIL;
1107 vp = vic_provinfo_alloc(GFP_KERNEL, oui,
1108 VIC_PROVINFO_LINUX_TYPE);
1109 if (!vp)
1110 return -ENOMEM;
1112 vic_provinfo_add_tlv(vp,
1113 VIC_LINUX_PROV_TLV_PORT_PROFILE_NAME_STR,
1114 strlen(enic->pp.name) + 1, enic->pp.name);
1116 vic_provinfo_add_tlv(vp,
1117 VIC_LINUX_PROV_TLV_CLIENT_MAC_ADDR,
1118 ETH_ALEN, mac);
1120 if (enic->pp.set & ENIC_SET_INSTANCE) {
1121 sprintf(uuid_str, "%pUB", enic->pp.instance_uuid);
1122 vic_provinfo_add_tlv(vp,
1123 VIC_LINUX_PROV_TLV_CLIENT_UUID_STR,
1124 sizeof(uuid_str), uuid_str);
1127 if (enic->pp.set & ENIC_SET_HOST) {
1128 sprintf(uuid_str, "%pUB", enic->pp.host_uuid);
1129 vic_provinfo_add_tlv(vp,
1130 VIC_LINUX_PROV_TLV_HOST_UUID_STR,
1131 sizeof(uuid_str), uuid_str);
1134 err = enic_dev_init_prov(enic, vp);
1135 vic_provinfo_free(vp);
1136 if (err)
1137 return err;
1138 break;
1140 case PORT_REQUEST_DISASSOCIATE:
1141 break;
1143 default:
1144 return -EINVAL;
1147 enic->pp.set |= ENIC_SET_APPLIED;
1148 return 0;
1151 static int enic_set_vf_port(struct net_device *netdev, int vf,
1152 struct nlattr *port[])
1154 struct enic *enic = netdev_priv(netdev);
1156 memset(&enic->pp, 0, sizeof(enic->pp));
1158 if (port[IFLA_PORT_REQUEST]) {
1159 enic->pp.set |= ENIC_SET_REQUEST;
1160 enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1163 if (port[IFLA_PORT_PROFILE]) {
1164 enic->pp.set |= ENIC_SET_NAME;
1165 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
1166 PORT_PROFILE_MAX);
1169 if (port[IFLA_PORT_INSTANCE_UUID]) {
1170 enic->pp.set |= ENIC_SET_INSTANCE;
1171 memcpy(enic->pp.instance_uuid,
1172 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1175 if (port[IFLA_PORT_HOST_UUID]) {
1176 enic->pp.set |= ENIC_SET_HOST;
1177 memcpy(enic->pp.host_uuid,
1178 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1181 /* don't support VFs, yet */
1182 if (vf != PORT_SELF_VF)
1183 return -EOPNOTSUPP;
1185 if (!(enic->pp.set & ENIC_SET_REQUEST))
1186 return -EOPNOTSUPP;
1188 if (enic->pp.request == PORT_REQUEST_ASSOCIATE) {
1190 /* If the interface mac addr hasn't been assigned,
1191 * assign a random mac addr before setting port-
1192 * profile.
1195 if (is_zero_ether_addr(netdev->dev_addr))
1196 random_ether_addr(netdev->dev_addr);
1199 return enic_set_port_profile(enic, netdev->dev_addr);
1202 static int enic_get_vf_port(struct net_device *netdev, int vf,
1203 struct sk_buff *skb)
1205 struct enic *enic = netdev_priv(netdev);
1206 int err, error, done;
1207 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1209 if (!(enic->pp.set & ENIC_SET_APPLIED))
1210 return -ENODATA;
1212 err = enic_dev_init_done(enic, &done, &error);
1213 if (err)
1214 error = err;
1216 switch (error) {
1217 case ERR_SUCCESS:
1218 if (!done)
1219 response = PORT_PROFILE_RESPONSE_INPROGRESS;
1220 break;
1221 case ERR_EINVAL:
1222 response = PORT_PROFILE_RESPONSE_INVALID;
1223 break;
1224 case ERR_EBADSTATE:
1225 response = PORT_PROFILE_RESPONSE_BADSTATE;
1226 break;
1227 case ERR_ENOMEM:
1228 response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
1229 break;
1230 default:
1231 response = PORT_PROFILE_RESPONSE_ERROR;
1232 break;
1235 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1236 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
1237 if (enic->pp.set & ENIC_SET_NAME)
1238 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1239 enic->pp.name);
1240 if (enic->pp.set & ENIC_SET_INSTANCE)
1241 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1242 enic->pp.instance_uuid);
1243 if (enic->pp.set & ENIC_SET_HOST)
1244 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1245 enic->pp.host_uuid);
1247 return 0;
1249 nla_put_failure:
1250 return -EMSGSIZE;
1253 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1255 struct enic *enic = vnic_dev_priv(rq->vdev);
1257 if (!buf->os_buf)
1258 return;
1260 pci_unmap_single(enic->pdev, buf->dma_addr,
1261 buf->len, PCI_DMA_FROMDEVICE);
1262 dev_kfree_skb_any(buf->os_buf);
1265 static int enic_rq_alloc_buf(struct vnic_rq *rq)
1267 struct enic *enic = vnic_dev_priv(rq->vdev);
1268 struct net_device *netdev = enic->netdev;
1269 struct sk_buff *skb;
1270 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
1271 unsigned int os_buf_index = 0;
1272 dma_addr_t dma_addr;
1274 skb = netdev_alloc_skb_ip_align(netdev, len);
1275 if (!skb)
1276 return -ENOMEM;
1278 dma_addr = pci_map_single(enic->pdev, skb->data,
1279 len, PCI_DMA_FROMDEVICE);
1281 enic_queue_rq_desc(rq, skb, os_buf_index,
1282 dma_addr, len);
1284 return 0;
1287 static int enic_rq_alloc_buf_a1(struct vnic_rq *rq)
1289 struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
1291 if (vnic_rq_posting_soon(rq)) {
1294 rq_enet_desc_enc(desc, 0, RQ_ENET_TYPE_RESV2, 0);
1295 vnic_rq_post(rq, 0, 0, 0, 0);
1296 } else {
1297 return enic_rq_alloc_buf(rq);
1300 return 0;
1303 static int enic_dev_hw_version(struct enic *enic,
1304 enum vnic_dev_hw_version *hw_ver)
1306 int err;
1308 spin_lock(&enic->devcmd_lock);
1309 err = vnic_dev_hw_version(enic->vdev, hw_ver);
1310 spin_unlock(&enic->devcmd_lock);
1312 return err;
1315 static int enic_set_rq_alloc_buf(struct enic *enic)
1317 enum vnic_dev_hw_version hw_ver;
1318 int err;
1320 err = enic_dev_hw_version(enic, &hw_ver);
1321 if (err)
1322 return err;
1324 switch (hw_ver) {
1325 case VNIC_DEV_HW_VER_A1:
1326 enic->rq_alloc_buf = enic_rq_alloc_buf_a1;
1327 break;
1328 case VNIC_DEV_HW_VER_A2:
1329 case VNIC_DEV_HW_VER_UNKNOWN:
1330 enic->rq_alloc_buf = enic_rq_alloc_buf;
1331 break;
1332 default:
1333 return -ENODEV;
1336 return 0;
1339 static void enic_rq_indicate_buf(struct vnic_rq *rq,
1340 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1341 int skipped, void *opaque)
1343 struct enic *enic = vnic_dev_priv(rq->vdev);
1344 struct net_device *netdev = enic->netdev;
1345 struct sk_buff *skb;
1347 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1348 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1349 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1350 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1351 u8 packet_error;
1352 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1353 u32 rss_hash;
1355 if (skipped)
1356 return;
1358 skb = buf->os_buf;
1359 prefetch(skb->data - NET_IP_ALIGN);
1360 pci_unmap_single(enic->pdev, buf->dma_addr,
1361 buf->len, PCI_DMA_FROMDEVICE);
1363 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1364 &type, &color, &q_number, &completed_index,
1365 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1366 &csum_not_calc, &rss_hash, &bytes_written,
1367 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
1368 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1369 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1370 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1371 &fcs_ok);
1373 if (packet_error) {
1375 if (!fcs_ok) {
1376 if (bytes_written > 0)
1377 enic->rq_bad_fcs++;
1378 else if (bytes_written == 0)
1379 enic->rq_truncated_pkts++;
1382 dev_kfree_skb_any(skb);
1384 return;
1387 if (eop && bytes_written > 0) {
1389 /* Good receive
1392 skb_put(skb, bytes_written);
1393 skb->protocol = eth_type_trans(skb, netdev);
1395 if (enic->csum_rx_enabled && !csum_not_calc) {
1396 skb->csum = htons(checksum);
1397 skb->ip_summed = CHECKSUM_COMPLETE;
1400 skb->dev = netdev;
1402 if (enic->vlan_group && vlan_stripped &&
1403 (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
1405 if (netdev->features & NETIF_F_GRO)
1406 vlan_gro_receive(&enic->napi, enic->vlan_group,
1407 vlan_tci, skb);
1408 else
1409 vlan_hwaccel_receive_skb(skb,
1410 enic->vlan_group, vlan_tci);
1412 } else {
1414 if (netdev->features & NETIF_F_GRO)
1415 napi_gro_receive(&enic->napi, skb);
1416 else
1417 netif_receive_skb(skb);
1421 } else {
1423 /* Buffer overflow
1426 dev_kfree_skb_any(skb);
1430 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1431 u8 type, u16 q_number, u16 completed_index, void *opaque)
1433 struct enic *enic = vnic_dev_priv(vdev);
1435 vnic_rq_service(&enic->rq[q_number], cq_desc,
1436 completed_index, VNIC_RQ_RETURN_DESC,
1437 enic_rq_indicate_buf, opaque);
1439 return 0;
1442 static int enic_poll(struct napi_struct *napi, int budget)
1444 struct enic *enic = container_of(napi, struct enic, napi);
1445 unsigned int rq_work_to_do = budget;
1446 unsigned int wq_work_to_do = -1; /* no limit */
1447 unsigned int work_done, rq_work_done, wq_work_done;
1448 int err;
1450 /* Service RQ (first) and WQ
1453 rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1454 rq_work_to_do, enic_rq_service, NULL);
1456 wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
1457 wq_work_to_do, enic_wq_service, NULL);
1459 /* Accumulate intr event credits for this polling
1460 * cycle. An intr event is the completion of a
1461 * a WQ or RQ packet.
1464 work_done = rq_work_done + wq_work_done;
1466 if (work_done > 0)
1467 vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
1468 work_done,
1469 0 /* don't unmask intr */,
1470 0 /* don't reset intr timer */);
1472 err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
1474 /* Buffer allocation failed. Stay in polling
1475 * mode so we can try to fill the ring again.
1478 if (err)
1479 rq_work_done = rq_work_to_do;
1481 if (rq_work_done < rq_work_to_do) {
1483 /* Some work done, but not enough to stay in polling,
1484 * exit polling
1487 napi_complete(napi);
1488 vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
1491 return rq_work_done;
1494 static int enic_poll_msix(struct napi_struct *napi, int budget)
1496 struct enic *enic = container_of(napi, struct enic, napi);
1497 unsigned int work_to_do = budget;
1498 unsigned int work_done;
1499 int err;
1501 /* Service RQ
1504 work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
1505 work_to_do, enic_rq_service, NULL);
1507 /* Return intr event credits for this polling
1508 * cycle. An intr event is the completion of a
1509 * RQ packet.
1512 if (work_done > 0)
1513 vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
1514 work_done,
1515 0 /* don't unmask intr */,
1516 0 /* don't reset intr timer */);
1518 err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
1520 /* Buffer allocation failed. Stay in polling mode
1521 * so we can try to fill the ring again.
1524 if (err)
1525 work_done = work_to_do;
1527 if (work_done < work_to_do) {
1529 /* Some work done, but not enough to stay in polling,
1530 * exit polling
1533 napi_complete(napi);
1534 vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
1537 return work_done;
1540 static void enic_notify_timer(unsigned long data)
1542 struct enic *enic = (struct enic *)data;
1544 enic_notify_check(enic);
1546 mod_timer(&enic->notify_timer,
1547 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1550 static void enic_free_intr(struct enic *enic)
1552 struct net_device *netdev = enic->netdev;
1553 unsigned int i;
1555 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1556 case VNIC_DEV_INTR_MODE_INTX:
1557 free_irq(enic->pdev->irq, netdev);
1558 break;
1559 case VNIC_DEV_INTR_MODE_MSI:
1560 free_irq(enic->pdev->irq, enic);
1561 break;
1562 case VNIC_DEV_INTR_MODE_MSIX:
1563 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1564 if (enic->msix[i].requested)
1565 free_irq(enic->msix_entry[i].vector,
1566 enic->msix[i].devid);
1567 break;
1568 default:
1569 break;
1573 static int enic_request_intr(struct enic *enic)
1575 struct net_device *netdev = enic->netdev;
1576 unsigned int i;
1577 int err = 0;
1579 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1581 case VNIC_DEV_INTR_MODE_INTX:
1583 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1584 IRQF_SHARED, netdev->name, netdev);
1585 break;
1587 case VNIC_DEV_INTR_MODE_MSI:
1589 err = request_irq(enic->pdev->irq, enic_isr_msi,
1590 0, netdev->name, enic);
1591 break;
1593 case VNIC_DEV_INTR_MODE_MSIX:
1595 sprintf(enic->msix[ENIC_MSIX_RQ].devname,
1596 "%.11s-rx-0", netdev->name);
1597 enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
1598 enic->msix[ENIC_MSIX_RQ].devid = enic;
1600 sprintf(enic->msix[ENIC_MSIX_WQ].devname,
1601 "%.11s-tx-0", netdev->name);
1602 enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
1603 enic->msix[ENIC_MSIX_WQ].devid = enic;
1605 sprintf(enic->msix[ENIC_MSIX_ERR].devname,
1606 "%.11s-err", netdev->name);
1607 enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
1608 enic->msix[ENIC_MSIX_ERR].devid = enic;
1610 sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
1611 "%.11s-notify", netdev->name);
1612 enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
1613 enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
1615 for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
1616 err = request_irq(enic->msix_entry[i].vector,
1617 enic->msix[i].isr, 0,
1618 enic->msix[i].devname,
1619 enic->msix[i].devid);
1620 if (err) {
1621 enic_free_intr(enic);
1622 break;
1624 enic->msix[i].requested = 1;
1627 break;
1629 default:
1630 break;
1633 return err;
1636 static void enic_synchronize_irqs(struct enic *enic)
1638 unsigned int i;
1640 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1641 case VNIC_DEV_INTR_MODE_INTX:
1642 case VNIC_DEV_INTR_MODE_MSI:
1643 synchronize_irq(enic->pdev->irq);
1644 break;
1645 case VNIC_DEV_INTR_MODE_MSIX:
1646 for (i = 0; i < enic->intr_count; i++)
1647 synchronize_irq(enic->msix_entry[i].vector);
1648 break;
1649 default:
1650 break;
1654 static int enic_dev_notify_set(struct enic *enic)
1656 int err;
1658 spin_lock(&enic->devcmd_lock);
1659 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1660 case VNIC_DEV_INTR_MODE_INTX:
1661 err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
1662 break;
1663 case VNIC_DEV_INTR_MODE_MSIX:
1664 err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
1665 break;
1666 default:
1667 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1668 break;
1670 spin_unlock(&enic->devcmd_lock);
1672 return err;
1675 static int enic_dev_notify_unset(struct enic *enic)
1677 int err;
1679 spin_lock(&enic->devcmd_lock);
1680 err = vnic_dev_notify_unset(enic->vdev);
1681 spin_unlock(&enic->devcmd_lock);
1683 return err;
1686 static int enic_dev_enable(struct enic *enic)
1688 int err;
1690 spin_lock(&enic->devcmd_lock);
1691 err = vnic_dev_enable(enic->vdev);
1692 spin_unlock(&enic->devcmd_lock);
1694 return err;
1697 static int enic_dev_disable(struct enic *enic)
1699 int err;
1701 spin_lock(&enic->devcmd_lock);
1702 err = vnic_dev_disable(enic->vdev);
1703 spin_unlock(&enic->devcmd_lock);
1705 return err;
1708 static void enic_notify_timer_start(struct enic *enic)
1710 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1711 case VNIC_DEV_INTR_MODE_MSI:
1712 mod_timer(&enic->notify_timer, jiffies);
1713 break;
1714 default:
1715 /* Using intr for notification for INTx/MSI-X */
1716 break;
1720 /* rtnl lock is held, process context */
1721 static int enic_open(struct net_device *netdev)
1723 struct enic *enic = netdev_priv(netdev);
1724 unsigned int i;
1725 int err;
1727 err = enic_request_intr(enic);
1728 if (err) {
1729 netdev_err(netdev, "Unable to request irq.\n");
1730 return err;
1733 err = enic_dev_notify_set(enic);
1734 if (err) {
1735 netdev_err(netdev,
1736 "Failed to alloc notify buffer, aborting.\n");
1737 goto err_out_free_intr;
1740 for (i = 0; i < enic->rq_count; i++) {
1741 vnic_rq_fill(&enic->rq[i], enic->rq_alloc_buf);
1742 /* Need at least one buffer on ring to get going */
1743 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1744 netdev_err(netdev, "Unable to alloc receive buffers\n");
1745 err = -ENOMEM;
1746 goto err_out_notify_unset;
1750 for (i = 0; i < enic->wq_count; i++)
1751 vnic_wq_enable(&enic->wq[i]);
1752 for (i = 0; i < enic->rq_count; i++)
1753 vnic_rq_enable(&enic->rq[i]);
1755 enic_dev_add_station_addr(enic);
1756 enic_set_multicast_list(netdev);
1758 netif_wake_queue(netdev);
1759 napi_enable(&enic->napi);
1760 enic_dev_enable(enic);
1762 for (i = 0; i < enic->intr_count; i++)
1763 vnic_intr_unmask(&enic->intr[i]);
1765 enic_notify_timer_start(enic);
1767 return 0;
1769 err_out_notify_unset:
1770 enic_dev_notify_unset(enic);
1771 err_out_free_intr:
1772 enic_free_intr(enic);
1774 return err;
1777 /* rtnl lock is held, process context */
1778 static int enic_stop(struct net_device *netdev)
1780 struct enic *enic = netdev_priv(netdev);
1781 unsigned int i;
1782 int err;
1784 for (i = 0; i < enic->intr_count; i++) {
1785 vnic_intr_mask(&enic->intr[i]);
1786 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1789 enic_synchronize_irqs(enic);
1791 del_timer_sync(&enic->notify_timer);
1793 enic_dev_disable(enic);
1794 napi_disable(&enic->napi);
1795 netif_carrier_off(netdev);
1796 netif_tx_disable(netdev);
1797 enic_dev_del_station_addr(enic);
1799 for (i = 0; i < enic->wq_count; i++) {
1800 err = vnic_wq_disable(&enic->wq[i]);
1801 if (err)
1802 return err;
1804 for (i = 0; i < enic->rq_count; i++) {
1805 err = vnic_rq_disable(&enic->rq[i]);
1806 if (err)
1807 return err;
1810 enic_dev_notify_unset(enic);
1811 enic_free_intr(enic);
1813 for (i = 0; i < enic->wq_count; i++)
1814 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1815 for (i = 0; i < enic->rq_count; i++)
1816 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1817 for (i = 0; i < enic->cq_count; i++)
1818 vnic_cq_clean(&enic->cq[i]);
1819 for (i = 0; i < enic->intr_count; i++)
1820 vnic_intr_clean(&enic->intr[i]);
1822 return 0;
1825 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1827 struct enic *enic = netdev_priv(netdev);
1828 int running = netif_running(netdev);
1830 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1831 return -EINVAL;
1833 if (running)
1834 enic_stop(netdev);
1836 netdev->mtu = new_mtu;
1838 if (netdev->mtu > enic->port_mtu)
1839 netdev_warn(netdev,
1840 "interface MTU (%d) set higher than port MTU (%d)\n",
1841 netdev->mtu, enic->port_mtu);
1843 if (running)
1844 enic_open(netdev);
1846 return 0;
1849 #ifdef CONFIG_NET_POLL_CONTROLLER
1850 static void enic_poll_controller(struct net_device *netdev)
1852 struct enic *enic = netdev_priv(netdev);
1853 struct vnic_dev *vdev = enic->vdev;
1855 switch (vnic_dev_get_intr_mode(vdev)) {
1856 case VNIC_DEV_INTR_MODE_MSIX:
1857 enic_isr_msix_rq(enic->pdev->irq, enic);
1858 enic_isr_msix_wq(enic->pdev->irq, enic);
1859 break;
1860 case VNIC_DEV_INTR_MODE_MSI:
1861 enic_isr_msi(enic->pdev->irq, enic);
1862 break;
1863 case VNIC_DEV_INTR_MODE_INTX:
1864 enic_isr_legacy(enic->pdev->irq, netdev);
1865 break;
1866 default:
1867 break;
1870 #endif
1872 static int enic_dev_wait(struct vnic_dev *vdev,
1873 int (*start)(struct vnic_dev *, int),
1874 int (*finished)(struct vnic_dev *, int *),
1875 int arg)
1877 unsigned long time;
1878 int done;
1879 int err;
1881 BUG_ON(in_interrupt());
1883 err = start(vdev, arg);
1884 if (err)
1885 return err;
1887 /* Wait for func to complete...2 seconds max
1890 time = jiffies + (HZ * 2);
1891 do {
1893 err = finished(vdev, &done);
1894 if (err)
1895 return err;
1897 if (done)
1898 return 0;
1900 schedule_timeout_uninterruptible(HZ / 10);
1902 } while (time_after(time, jiffies));
1904 return -ETIMEDOUT;
1907 static int enic_dev_open(struct enic *enic)
1909 int err;
1911 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1912 vnic_dev_open_done, 0);
1913 if (err)
1914 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1915 err);
1917 return err;
1920 static int enic_dev_hang_reset(struct enic *enic)
1922 int err;
1924 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1925 vnic_dev_hang_reset_done, 0);
1926 if (err)
1927 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1928 err);
1930 return err;
1933 static int enic_set_niccfg(struct enic *enic)
1935 const u8 rss_default_cpu = 0;
1936 const u8 rss_hash_type = 0;
1937 const u8 rss_hash_bits = 0;
1938 const u8 rss_base_cpu = 0;
1939 const u8 rss_enable = 0;
1940 const u8 tso_ipid_split_en = 0;
1941 const u8 ig_vlan_strip_en = 1;
1942 int err;
1944 /* Enable VLAN tag stripping. RSS not enabled (yet).
1947 spin_lock(&enic->devcmd_lock);
1948 err = enic_set_nic_cfg(enic,
1949 rss_default_cpu, rss_hash_type,
1950 rss_hash_bits, rss_base_cpu,
1951 rss_enable, tso_ipid_split_en,
1952 ig_vlan_strip_en);
1953 spin_unlock(&enic->devcmd_lock);
1955 return err;
1958 static int enic_dev_hang_notify(struct enic *enic)
1960 int err;
1962 spin_lock(&enic->devcmd_lock);
1963 err = vnic_dev_hang_notify(enic->vdev);
1964 spin_unlock(&enic->devcmd_lock);
1966 return err;
1969 int enic_dev_set_ig_vlan_rewrite_mode(struct enic *enic)
1971 int err;
1973 spin_lock(&enic->devcmd_lock);
1974 err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
1975 IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
1976 spin_unlock(&enic->devcmd_lock);
1978 return err;
1981 static void enic_reset(struct work_struct *work)
1983 struct enic *enic = container_of(work, struct enic, reset);
1985 if (!netif_running(enic->netdev))
1986 return;
1988 rtnl_lock();
1990 enic_dev_hang_notify(enic);
1991 enic_stop(enic->netdev);
1992 enic_dev_hang_reset(enic);
1993 enic_reset_multicast_list(enic);
1994 enic_init_vnic_resources(enic);
1995 enic_set_niccfg(enic);
1996 enic_dev_set_ig_vlan_rewrite_mode(enic);
1997 enic_open(enic->netdev);
1999 rtnl_unlock();
2002 static int enic_set_intr_mode(struct enic *enic)
2004 unsigned int n = 1;
2005 unsigned int m = 1;
2006 unsigned int i;
2008 /* Set interrupt mode (INTx, MSI, MSI-X) depending
2009 * system capabilities.
2011 * Try MSI-X first
2013 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2014 * (the second to last INTR is used for WQ/RQ errors)
2015 * (the last INTR is used for notifications)
2018 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2019 for (i = 0; i < n + m + 2; i++)
2020 enic->msix_entry[i].entry = i;
2022 if (enic->config.intr_mode < 1 &&
2023 enic->rq_count >= n &&
2024 enic->wq_count >= m &&
2025 enic->cq_count >= n + m &&
2026 enic->intr_count >= n + m + 2 &&
2027 !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
2029 enic->rq_count = n;
2030 enic->wq_count = m;
2031 enic->cq_count = n + m;
2032 enic->intr_count = n + m + 2;
2034 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
2036 return 0;
2039 /* Next try MSI
2041 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2044 if (enic->config.intr_mode < 2 &&
2045 enic->rq_count >= 1 &&
2046 enic->wq_count >= 1 &&
2047 enic->cq_count >= 2 &&
2048 enic->intr_count >= 1 &&
2049 !pci_enable_msi(enic->pdev)) {
2051 enic->rq_count = 1;
2052 enic->wq_count = 1;
2053 enic->cq_count = 2;
2054 enic->intr_count = 1;
2056 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2058 return 0;
2061 /* Next try INTx
2063 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2064 * (the first INTR is used for WQ/RQ)
2065 * (the second INTR is used for WQ/RQ errors)
2066 * (the last INTR is used for notifications)
2069 if (enic->config.intr_mode < 3 &&
2070 enic->rq_count >= 1 &&
2071 enic->wq_count >= 1 &&
2072 enic->cq_count >= 2 &&
2073 enic->intr_count >= 3) {
2075 enic->rq_count = 1;
2076 enic->wq_count = 1;
2077 enic->cq_count = 2;
2078 enic->intr_count = 3;
2080 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2082 return 0;
2085 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2087 return -EINVAL;
2090 static void enic_clear_intr_mode(struct enic *enic)
2092 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2093 case VNIC_DEV_INTR_MODE_MSIX:
2094 pci_disable_msix(enic->pdev);
2095 break;
2096 case VNIC_DEV_INTR_MODE_MSI:
2097 pci_disable_msi(enic->pdev);
2098 break;
2099 default:
2100 break;
2103 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2106 static const struct net_device_ops enic_netdev_dynamic_ops = {
2107 .ndo_open = enic_open,
2108 .ndo_stop = enic_stop,
2109 .ndo_start_xmit = enic_hard_start_xmit,
2110 .ndo_get_stats = enic_get_stats,
2111 .ndo_validate_addr = eth_validate_addr,
2112 .ndo_set_multicast_list = enic_set_multicast_list,
2113 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2114 .ndo_change_mtu = enic_change_mtu,
2115 .ndo_vlan_rx_register = enic_vlan_rx_register,
2116 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2117 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2118 .ndo_tx_timeout = enic_tx_timeout,
2119 .ndo_set_vf_port = enic_set_vf_port,
2120 .ndo_get_vf_port = enic_get_vf_port,
2121 #ifdef CONFIG_NET_POLL_CONTROLLER
2122 .ndo_poll_controller = enic_poll_controller,
2123 #endif
2126 static const struct net_device_ops enic_netdev_ops = {
2127 .ndo_open = enic_open,
2128 .ndo_stop = enic_stop,
2129 .ndo_start_xmit = enic_hard_start_xmit,
2130 .ndo_get_stats = enic_get_stats,
2131 .ndo_validate_addr = eth_validate_addr,
2132 .ndo_set_mac_address = enic_set_mac_address,
2133 .ndo_set_multicast_list = enic_set_multicast_list,
2134 .ndo_change_mtu = enic_change_mtu,
2135 .ndo_vlan_rx_register = enic_vlan_rx_register,
2136 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2137 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2138 .ndo_tx_timeout = enic_tx_timeout,
2139 #ifdef CONFIG_NET_POLL_CONTROLLER
2140 .ndo_poll_controller = enic_poll_controller,
2141 #endif
2144 void enic_dev_deinit(struct enic *enic)
2146 netif_napi_del(&enic->napi);
2147 enic_free_vnic_resources(enic);
2148 enic_clear_intr_mode(enic);
2151 static int enic_dev_stats_clear(struct enic *enic)
2153 int err;
2155 spin_lock(&enic->devcmd_lock);
2156 err = vnic_dev_stats_clear(enic->vdev);
2157 spin_unlock(&enic->devcmd_lock);
2159 return err;
2162 int enic_dev_init(struct enic *enic)
2164 struct device *dev = enic_get_dev(enic);
2165 struct net_device *netdev = enic->netdev;
2166 int err;
2168 /* Get vNIC configuration
2171 err = enic_get_vnic_config(enic);
2172 if (err) {
2173 dev_err(dev, "Get vNIC configuration failed, aborting\n");
2174 return err;
2177 /* Get available resource counts
2180 enic_get_res_counts(enic);
2182 /* Set interrupt mode based on resource counts and system
2183 * capabilities
2186 err = enic_set_intr_mode(enic);
2187 if (err) {
2188 dev_err(dev, "Failed to set intr mode based on resource "
2189 "counts and system capabilities, aborting\n");
2190 return err;
2193 /* Allocate and configure vNIC resources
2196 err = enic_alloc_vnic_resources(enic);
2197 if (err) {
2198 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2199 goto err_out_free_vnic_resources;
2202 enic_init_vnic_resources(enic);
2204 /* Clear LIF stats
2206 enic_dev_stats_clear(enic);
2208 err = enic_set_rq_alloc_buf(enic);
2209 if (err) {
2210 dev_err(dev, "Failed to set RQ buffer allocator, aborting\n");
2211 goto err_out_free_vnic_resources;
2214 err = enic_set_niccfg(enic);
2215 if (err) {
2216 dev_err(dev, "Failed to config nic, aborting\n");
2217 goto err_out_free_vnic_resources;
2220 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2221 if (err) {
2222 netdev_err(netdev,
2223 "Failed to set ingress vlan rewrite mode, aborting.\n");
2224 goto err_out_free_vnic_resources;
2227 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2228 default:
2229 netif_napi_add(netdev, &enic->napi, enic_poll, 64);
2230 break;
2231 case VNIC_DEV_INTR_MODE_MSIX:
2232 netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
2233 break;
2236 return 0;
2238 err_out_free_vnic_resources:
2239 enic_clear_intr_mode(enic);
2240 enic_free_vnic_resources(enic);
2242 return err;
2245 static void enic_iounmap(struct enic *enic)
2247 unsigned int i;
2249 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2250 if (enic->bar[i].vaddr)
2251 iounmap(enic->bar[i].vaddr);
2254 static int __devinit enic_probe(struct pci_dev *pdev,
2255 const struct pci_device_id *ent)
2257 struct device *dev = &pdev->dev;
2258 struct net_device *netdev;
2259 struct enic *enic;
2260 int using_dac = 0;
2261 unsigned int i;
2262 int err;
2264 /* Allocate net device structure and initialize. Private
2265 * instance data is initialized to zero.
2268 netdev = alloc_etherdev(sizeof(struct enic));
2269 if (!netdev) {
2270 pr_err("Etherdev alloc failed, aborting\n");
2271 return -ENOMEM;
2274 pci_set_drvdata(pdev, netdev);
2276 SET_NETDEV_DEV(netdev, &pdev->dev);
2278 enic = netdev_priv(netdev);
2279 enic->netdev = netdev;
2280 enic->pdev = pdev;
2282 /* Setup PCI resources
2285 err = pci_enable_device_mem(pdev);
2286 if (err) {
2287 dev_err(dev, "Cannot enable PCI device, aborting\n");
2288 goto err_out_free_netdev;
2291 err = pci_request_regions(pdev, DRV_NAME);
2292 if (err) {
2293 dev_err(dev, "Cannot request PCI regions, aborting\n");
2294 goto err_out_disable_device;
2297 pci_set_master(pdev);
2299 /* Query PCI controller on system for DMA addressing
2300 * limitation for the device. Try 40-bit first, and
2301 * fail to 32-bit.
2304 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
2305 if (err) {
2306 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2307 if (err) {
2308 dev_err(dev, "No usable DMA configuration, aborting\n");
2309 goto err_out_release_regions;
2311 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2312 if (err) {
2313 dev_err(dev, "Unable to obtain %u-bit DMA "
2314 "for consistent allocations, aborting\n", 32);
2315 goto err_out_release_regions;
2317 } else {
2318 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2319 if (err) {
2320 dev_err(dev, "Unable to obtain %u-bit DMA "
2321 "for consistent allocations, aborting\n", 40);
2322 goto err_out_release_regions;
2324 using_dac = 1;
2327 /* Map vNIC resources from BAR0-5
2330 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2331 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2332 continue;
2333 enic->bar[i].len = pci_resource_len(pdev, i);
2334 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2335 if (!enic->bar[i].vaddr) {
2336 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2337 err = -ENODEV;
2338 goto err_out_iounmap;
2340 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2343 /* Register vNIC device
2346 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2347 ARRAY_SIZE(enic->bar));
2348 if (!enic->vdev) {
2349 dev_err(dev, "vNIC registration failed, aborting\n");
2350 err = -ENODEV;
2351 goto err_out_iounmap;
2354 /* Issue device open to get device in known state
2357 err = enic_dev_open(enic);
2358 if (err) {
2359 dev_err(dev, "vNIC dev open failed, aborting\n");
2360 goto err_out_vnic_unregister;
2363 /* Issue device init to initialize the vnic-to-switch link.
2364 * We'll start with carrier off and wait for link UP
2365 * notification later to turn on carrier. We don't need
2366 * to wait here for the vnic-to-switch link initialization
2367 * to complete; link UP notification is the indication that
2368 * the process is complete.
2371 netif_carrier_off(netdev);
2373 /* Do not call dev_init for a dynamic vnic.
2374 * For a dynamic vnic, init_prov_info will be
2375 * called later by an upper layer.
2378 if (!enic_is_dynamic(enic)) {
2379 err = vnic_dev_init(enic->vdev, 0);
2380 if (err) {
2381 dev_err(dev, "vNIC dev init failed, aborting\n");
2382 goto err_out_dev_close;
2386 /* Setup devcmd lock
2389 spin_lock_init(&enic->devcmd_lock);
2391 err = enic_dev_init(enic);
2392 if (err) {
2393 dev_err(dev, "Device initialization failed, aborting\n");
2394 goto err_out_dev_close;
2397 /* Setup notification timer, HW reset task, and wq locks
2400 init_timer(&enic->notify_timer);
2401 enic->notify_timer.function = enic_notify_timer;
2402 enic->notify_timer.data = (unsigned long)enic;
2404 INIT_WORK(&enic->reset, enic_reset);
2406 for (i = 0; i < enic->wq_count; i++)
2407 spin_lock_init(&enic->wq_lock[i]);
2409 /* Register net device
2412 enic->port_mtu = enic->config.mtu;
2413 (void)enic_change_mtu(netdev, enic->port_mtu);
2415 err = enic_set_mac_addr(netdev, enic->mac_addr);
2416 if (err) {
2417 dev_err(dev, "Invalid MAC address, aborting\n");
2418 goto err_out_dev_deinit;
2421 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2422 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2424 if (enic_is_dynamic(enic))
2425 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2426 else
2427 netdev->netdev_ops = &enic_netdev_ops;
2429 netdev->watchdog_timeo = 2 * HZ;
2430 netdev->ethtool_ops = &enic_ethtool_ops;
2432 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2433 if (ENIC_SETTING(enic, LOOP)) {
2434 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2435 enic->loop_enable = 1;
2436 enic->loop_tag = enic->config.loop_tag;
2437 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2439 if (ENIC_SETTING(enic, TXCSUM))
2440 netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2441 if (ENIC_SETTING(enic, TSO))
2442 netdev->features |= NETIF_F_TSO |
2443 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2444 if (ENIC_SETTING(enic, LRO))
2445 netdev->features |= NETIF_F_GRO;
2446 if (using_dac)
2447 netdev->features |= NETIF_F_HIGHDMA;
2449 enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
2451 err = register_netdev(netdev);
2452 if (err) {
2453 dev_err(dev, "Cannot register net device, aborting\n");
2454 goto err_out_dev_deinit;
2457 return 0;
2459 err_out_dev_deinit:
2460 enic_dev_deinit(enic);
2461 err_out_dev_close:
2462 vnic_dev_close(enic->vdev);
2463 err_out_vnic_unregister:
2464 vnic_dev_unregister(enic->vdev);
2465 err_out_iounmap:
2466 enic_iounmap(enic);
2467 err_out_release_regions:
2468 pci_release_regions(pdev);
2469 err_out_disable_device:
2470 pci_disable_device(pdev);
2471 err_out_free_netdev:
2472 pci_set_drvdata(pdev, NULL);
2473 free_netdev(netdev);
2475 return err;
2478 static void __devexit enic_remove(struct pci_dev *pdev)
2480 struct net_device *netdev = pci_get_drvdata(pdev);
2482 if (netdev) {
2483 struct enic *enic = netdev_priv(netdev);
2485 flush_scheduled_work();
2486 unregister_netdev(netdev);
2487 enic_dev_deinit(enic);
2488 vnic_dev_close(enic->vdev);
2489 vnic_dev_unregister(enic->vdev);
2490 enic_iounmap(enic);
2491 pci_release_regions(pdev);
2492 pci_disable_device(pdev);
2493 pci_set_drvdata(pdev, NULL);
2494 free_netdev(netdev);
2498 static struct pci_driver enic_driver = {
2499 .name = DRV_NAME,
2500 .id_table = enic_id_table,
2501 .probe = enic_probe,
2502 .remove = __devexit_p(enic_remove),
2505 static int __init enic_init_module(void)
2507 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2509 return pci_register_driver(&enic_driver);
2512 static void __exit enic_cleanup_module(void)
2514 pci_unregister_driver(&enic_driver);
2517 module_init(enic_init_module);
2518 module_exit(enic_cleanup_module);