enic: Bug Fix: Fix hardware transmit queue indexing in enic_poll_controller
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / enic / enic_main.c
blob51fba5fe94bda5b291f88b58e7cd6e563dd471bc
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 <linux/prefetch.h>
39 #include <net/ip6_checksum.h>
41 #include "cq_enet_desc.h"
42 #include "vnic_dev.h"
43 #include "vnic_intr.h"
44 #include "vnic_stats.h"
45 #include "vnic_vic.h"
46 #include "enic_res.h"
47 #include "enic.h"
48 #include "enic_dev.h"
49 #include "enic_pp.h"
51 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
52 #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
53 #define MAX_TSO (1 << 16)
54 #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
56 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
57 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
59 /* Supported devices */
60 static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
61 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
62 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
63 { 0, } /* end of table */
66 MODULE_DESCRIPTION(DRV_DESCRIPTION);
67 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
68 MODULE_LICENSE("GPL");
69 MODULE_VERSION(DRV_VERSION);
70 MODULE_DEVICE_TABLE(pci, enic_id_table);
72 struct enic_stat {
73 char name[ETH_GSTRING_LEN];
74 unsigned int offset;
77 #define ENIC_TX_STAT(stat) \
78 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
79 #define ENIC_RX_STAT(stat) \
80 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
82 static const struct enic_stat enic_tx_stats[] = {
83 ENIC_TX_STAT(tx_frames_ok),
84 ENIC_TX_STAT(tx_unicast_frames_ok),
85 ENIC_TX_STAT(tx_multicast_frames_ok),
86 ENIC_TX_STAT(tx_broadcast_frames_ok),
87 ENIC_TX_STAT(tx_bytes_ok),
88 ENIC_TX_STAT(tx_unicast_bytes_ok),
89 ENIC_TX_STAT(tx_multicast_bytes_ok),
90 ENIC_TX_STAT(tx_broadcast_bytes_ok),
91 ENIC_TX_STAT(tx_drops),
92 ENIC_TX_STAT(tx_errors),
93 ENIC_TX_STAT(tx_tso),
96 static const struct enic_stat enic_rx_stats[] = {
97 ENIC_RX_STAT(rx_frames_ok),
98 ENIC_RX_STAT(rx_frames_total),
99 ENIC_RX_STAT(rx_unicast_frames_ok),
100 ENIC_RX_STAT(rx_multicast_frames_ok),
101 ENIC_RX_STAT(rx_broadcast_frames_ok),
102 ENIC_RX_STAT(rx_bytes_ok),
103 ENIC_RX_STAT(rx_unicast_bytes_ok),
104 ENIC_RX_STAT(rx_multicast_bytes_ok),
105 ENIC_RX_STAT(rx_broadcast_bytes_ok),
106 ENIC_RX_STAT(rx_drop),
107 ENIC_RX_STAT(rx_no_bufs),
108 ENIC_RX_STAT(rx_errors),
109 ENIC_RX_STAT(rx_rss),
110 ENIC_RX_STAT(rx_crc_errors),
111 ENIC_RX_STAT(rx_frames_64),
112 ENIC_RX_STAT(rx_frames_127),
113 ENIC_RX_STAT(rx_frames_255),
114 ENIC_RX_STAT(rx_frames_511),
115 ENIC_RX_STAT(rx_frames_1023),
116 ENIC_RX_STAT(rx_frames_1518),
117 ENIC_RX_STAT(rx_frames_to_max),
120 static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
121 static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
123 static int enic_is_dynamic(struct enic *enic)
125 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
128 static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
130 return rq;
133 static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
135 return enic->rq_count + wq;
138 static inline unsigned int enic_legacy_io_intr(void)
140 return 0;
143 static inline unsigned int enic_legacy_err_intr(void)
145 return 1;
148 static inline unsigned int enic_legacy_notify_intr(void)
150 return 2;
153 static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
155 return rq;
158 static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
160 return enic->rq_count + wq;
163 static inline unsigned int enic_msix_err_intr(struct enic *enic)
165 return enic->rq_count + enic->wq_count;
168 static inline unsigned int enic_msix_notify_intr(struct enic *enic)
170 return enic->rq_count + enic->wq_count + 1;
173 static int enic_get_settings(struct net_device *netdev,
174 struct ethtool_cmd *ecmd)
176 struct enic *enic = netdev_priv(netdev);
178 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
179 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
180 ecmd->port = PORT_FIBRE;
181 ecmd->transceiver = XCVR_EXTERNAL;
183 if (netif_carrier_ok(netdev)) {
184 ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
185 ecmd->duplex = DUPLEX_FULL;
186 } else {
187 ethtool_cmd_speed_set(ecmd, -1);
188 ecmd->duplex = -1;
191 ecmd->autoneg = AUTONEG_DISABLE;
193 return 0;
196 static void enic_get_drvinfo(struct net_device *netdev,
197 struct ethtool_drvinfo *drvinfo)
199 struct enic *enic = netdev_priv(netdev);
200 struct vnic_devcmd_fw_info *fw_info;
202 enic_dev_fw_info(enic, &fw_info);
204 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
205 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
206 strncpy(drvinfo->fw_version, fw_info->fw_version,
207 sizeof(drvinfo->fw_version));
208 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
209 sizeof(drvinfo->bus_info));
212 static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
214 unsigned int i;
216 switch (stringset) {
217 case ETH_SS_STATS:
218 for (i = 0; i < enic_n_tx_stats; i++) {
219 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
220 data += ETH_GSTRING_LEN;
222 for (i = 0; i < enic_n_rx_stats; i++) {
223 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
224 data += ETH_GSTRING_LEN;
226 break;
230 static int enic_get_sset_count(struct net_device *netdev, int sset)
232 switch (sset) {
233 case ETH_SS_STATS:
234 return enic_n_tx_stats + enic_n_rx_stats;
235 default:
236 return -EOPNOTSUPP;
240 static void enic_get_ethtool_stats(struct net_device *netdev,
241 struct ethtool_stats *stats, u64 *data)
243 struct enic *enic = netdev_priv(netdev);
244 struct vnic_stats *vstats;
245 unsigned int i;
247 enic_dev_stats_dump(enic, &vstats);
249 for (i = 0; i < enic_n_tx_stats; i++)
250 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
251 for (i = 0; i < enic_n_rx_stats; i++)
252 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
255 static u32 enic_get_msglevel(struct net_device *netdev)
257 struct enic *enic = netdev_priv(netdev);
258 return enic->msg_enable;
261 static void enic_set_msglevel(struct net_device *netdev, u32 value)
263 struct enic *enic = netdev_priv(netdev);
264 enic->msg_enable = value;
267 static int enic_get_coalesce(struct net_device *netdev,
268 struct ethtool_coalesce *ecmd)
270 struct enic *enic = netdev_priv(netdev);
272 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
273 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
275 return 0;
278 static int enic_set_coalesce(struct net_device *netdev,
279 struct ethtool_coalesce *ecmd)
281 struct enic *enic = netdev_priv(netdev);
282 u32 tx_coalesce_usecs;
283 u32 rx_coalesce_usecs;
284 unsigned int i, intr;
286 tx_coalesce_usecs = min_t(u32,
287 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
288 ecmd->tx_coalesce_usecs);
289 rx_coalesce_usecs = min_t(u32,
290 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
291 ecmd->rx_coalesce_usecs);
293 switch (vnic_dev_get_intr_mode(enic->vdev)) {
294 case VNIC_DEV_INTR_MODE_INTX:
295 if (tx_coalesce_usecs != rx_coalesce_usecs)
296 return -EINVAL;
298 intr = enic_legacy_io_intr();
299 vnic_intr_coalescing_timer_set(&enic->intr[intr],
300 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
301 break;
302 case VNIC_DEV_INTR_MODE_MSI:
303 if (tx_coalesce_usecs != rx_coalesce_usecs)
304 return -EINVAL;
306 vnic_intr_coalescing_timer_set(&enic->intr[0],
307 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
308 break;
309 case VNIC_DEV_INTR_MODE_MSIX:
310 for (i = 0; i < enic->wq_count; i++) {
311 intr = enic_msix_wq_intr(enic, i);
312 vnic_intr_coalescing_timer_set(&enic->intr[intr],
313 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
316 for (i = 0; i < enic->rq_count; i++) {
317 intr = enic_msix_rq_intr(enic, i);
318 vnic_intr_coalescing_timer_set(&enic->intr[intr],
319 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
322 break;
323 default:
324 break;
327 enic->tx_coalesce_usecs = tx_coalesce_usecs;
328 enic->rx_coalesce_usecs = rx_coalesce_usecs;
330 return 0;
333 static const struct ethtool_ops enic_ethtool_ops = {
334 .get_settings = enic_get_settings,
335 .get_drvinfo = enic_get_drvinfo,
336 .get_msglevel = enic_get_msglevel,
337 .set_msglevel = enic_set_msglevel,
338 .get_link = ethtool_op_get_link,
339 .get_strings = enic_get_strings,
340 .get_sset_count = enic_get_sset_count,
341 .get_ethtool_stats = enic_get_ethtool_stats,
342 .get_coalesce = enic_get_coalesce,
343 .set_coalesce = enic_set_coalesce,
346 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
348 struct enic *enic = vnic_dev_priv(wq->vdev);
350 if (buf->sop)
351 pci_unmap_single(enic->pdev, buf->dma_addr,
352 buf->len, PCI_DMA_TODEVICE);
353 else
354 pci_unmap_page(enic->pdev, buf->dma_addr,
355 buf->len, PCI_DMA_TODEVICE);
357 if (buf->os_buf)
358 dev_kfree_skb_any(buf->os_buf);
361 static void enic_wq_free_buf(struct vnic_wq *wq,
362 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
364 enic_free_wq_buf(wq, buf);
367 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
368 u8 type, u16 q_number, u16 completed_index, void *opaque)
370 struct enic *enic = vnic_dev_priv(vdev);
372 spin_lock(&enic->wq_lock[q_number]);
374 vnic_wq_service(&enic->wq[q_number], cq_desc,
375 completed_index, enic_wq_free_buf,
376 opaque);
378 if (netif_queue_stopped(enic->netdev) &&
379 vnic_wq_desc_avail(&enic->wq[q_number]) >=
380 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
381 netif_wake_queue(enic->netdev);
383 spin_unlock(&enic->wq_lock[q_number]);
385 return 0;
388 static void enic_log_q_error(struct enic *enic)
390 unsigned int i;
391 u32 error_status;
393 for (i = 0; i < enic->wq_count; i++) {
394 error_status = vnic_wq_error_status(&enic->wq[i]);
395 if (error_status)
396 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
397 i, error_status);
400 for (i = 0; i < enic->rq_count; i++) {
401 error_status = vnic_rq_error_status(&enic->rq[i]);
402 if (error_status)
403 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
404 i, error_status);
408 static void enic_msglvl_check(struct enic *enic)
410 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
412 if (msg_enable != enic->msg_enable) {
413 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
414 enic->msg_enable, msg_enable);
415 enic->msg_enable = msg_enable;
419 static void enic_mtu_check(struct enic *enic)
421 u32 mtu = vnic_dev_mtu(enic->vdev);
422 struct net_device *netdev = enic->netdev;
424 if (mtu && mtu != enic->port_mtu) {
425 enic->port_mtu = mtu;
426 if (mtu < netdev->mtu)
427 netdev_warn(netdev,
428 "interface MTU (%d) set higher "
429 "than switch port MTU (%d)\n",
430 netdev->mtu, mtu);
434 static void enic_link_check(struct enic *enic)
436 int link_status = vnic_dev_link_status(enic->vdev);
437 int carrier_ok = netif_carrier_ok(enic->netdev);
439 if (link_status && !carrier_ok) {
440 netdev_info(enic->netdev, "Link UP\n");
441 netif_carrier_on(enic->netdev);
442 } else if (!link_status && carrier_ok) {
443 netdev_info(enic->netdev, "Link DOWN\n");
444 netif_carrier_off(enic->netdev);
448 static void enic_notify_check(struct enic *enic)
450 enic_msglvl_check(enic);
451 enic_mtu_check(enic);
452 enic_link_check(enic);
455 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
457 static irqreturn_t enic_isr_legacy(int irq, void *data)
459 struct net_device *netdev = data;
460 struct enic *enic = netdev_priv(netdev);
461 unsigned int io_intr = enic_legacy_io_intr();
462 unsigned int err_intr = enic_legacy_err_intr();
463 unsigned int notify_intr = enic_legacy_notify_intr();
464 u32 pba;
466 vnic_intr_mask(&enic->intr[io_intr]);
468 pba = vnic_intr_legacy_pba(enic->legacy_pba);
469 if (!pba) {
470 vnic_intr_unmask(&enic->intr[io_intr]);
471 return IRQ_NONE; /* not our interrupt */
474 if (ENIC_TEST_INTR(pba, notify_intr)) {
475 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
476 enic_notify_check(enic);
479 if (ENIC_TEST_INTR(pba, err_intr)) {
480 vnic_intr_return_all_credits(&enic->intr[err_intr]);
481 enic_log_q_error(enic);
482 /* schedule recovery from WQ/RQ error */
483 schedule_work(&enic->reset);
484 return IRQ_HANDLED;
487 if (ENIC_TEST_INTR(pba, io_intr)) {
488 if (napi_schedule_prep(&enic->napi[0]))
489 __napi_schedule(&enic->napi[0]);
490 } else {
491 vnic_intr_unmask(&enic->intr[io_intr]);
494 return IRQ_HANDLED;
497 static irqreturn_t enic_isr_msi(int irq, void *data)
499 struct enic *enic = data;
501 /* With MSI, there is no sharing of interrupts, so this is
502 * our interrupt and there is no need to ack it. The device
503 * is not providing per-vector masking, so the OS will not
504 * write to PCI config space to mask/unmask the interrupt.
505 * We're using mask_on_assertion for MSI, so the device
506 * automatically masks the interrupt when the interrupt is
507 * generated. Later, when exiting polling, the interrupt
508 * will be unmasked (see enic_poll).
510 * Also, the device uses the same PCIe Traffic Class (TC)
511 * for Memory Write data and MSI, so there are no ordering
512 * issues; the MSI will always arrive at the Root Complex
513 * _after_ corresponding Memory Writes (i.e. descriptor
514 * writes).
517 napi_schedule(&enic->napi[0]);
519 return IRQ_HANDLED;
522 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
524 struct napi_struct *napi = data;
526 /* schedule NAPI polling for RQ cleanup */
527 napi_schedule(napi);
529 return IRQ_HANDLED;
532 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
534 struct enic *enic = data;
535 unsigned int cq = enic_cq_wq(enic, 0);
536 unsigned int intr = enic_msix_wq_intr(enic, 0);
537 unsigned int wq_work_to_do = -1; /* no limit */
538 unsigned int wq_work_done;
540 wq_work_done = vnic_cq_service(&enic->cq[cq],
541 wq_work_to_do, enic_wq_service, NULL);
543 vnic_intr_return_credits(&enic->intr[intr],
544 wq_work_done,
545 1 /* unmask intr */,
546 1 /* reset intr timer */);
548 return IRQ_HANDLED;
551 static irqreturn_t enic_isr_msix_err(int irq, void *data)
553 struct enic *enic = data;
554 unsigned int intr = enic_msix_err_intr(enic);
556 vnic_intr_return_all_credits(&enic->intr[intr]);
558 enic_log_q_error(enic);
560 /* schedule recovery from WQ/RQ error */
561 schedule_work(&enic->reset);
563 return IRQ_HANDLED;
566 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
568 struct enic *enic = data;
569 unsigned int intr = enic_msix_notify_intr(enic);
571 vnic_intr_return_all_credits(&enic->intr[intr]);
572 enic_notify_check(enic);
574 return IRQ_HANDLED;
577 static inline void enic_queue_wq_skb_cont(struct enic *enic,
578 struct vnic_wq *wq, struct sk_buff *skb,
579 unsigned int len_left, int loopback)
581 skb_frag_t *frag;
583 /* Queue additional data fragments */
584 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
585 len_left -= frag->size;
586 enic_queue_wq_desc_cont(wq, skb,
587 pci_map_page(enic->pdev, frag->page,
588 frag->page_offset, frag->size,
589 PCI_DMA_TODEVICE),
590 frag->size,
591 (len_left == 0), /* EOP? */
592 loopback);
596 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
597 struct vnic_wq *wq, struct sk_buff *skb,
598 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
600 unsigned int head_len = skb_headlen(skb);
601 unsigned int len_left = skb->len - head_len;
602 int eop = (len_left == 0);
604 /* Queue the main skb fragment. The fragments are no larger
605 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
606 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
607 * per fragment is queued.
609 enic_queue_wq_desc(wq, skb,
610 pci_map_single(enic->pdev, skb->data,
611 head_len, PCI_DMA_TODEVICE),
612 head_len,
613 vlan_tag_insert, vlan_tag,
614 eop, loopback);
616 if (!eop)
617 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
620 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
621 struct vnic_wq *wq, struct sk_buff *skb,
622 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
624 unsigned int head_len = skb_headlen(skb);
625 unsigned int len_left = skb->len - head_len;
626 unsigned int hdr_len = skb_checksum_start_offset(skb);
627 unsigned int csum_offset = hdr_len + skb->csum_offset;
628 int eop = (len_left == 0);
630 /* Queue the main skb fragment. The fragments are no larger
631 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
632 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
633 * per fragment is queued.
635 enic_queue_wq_desc_csum_l4(wq, skb,
636 pci_map_single(enic->pdev, skb->data,
637 head_len, PCI_DMA_TODEVICE),
638 head_len,
639 csum_offset,
640 hdr_len,
641 vlan_tag_insert, vlan_tag,
642 eop, loopback);
644 if (!eop)
645 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
648 static inline void enic_queue_wq_skb_tso(struct enic *enic,
649 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
650 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
652 unsigned int frag_len_left = skb_headlen(skb);
653 unsigned int len_left = skb->len - frag_len_left;
654 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
655 int eop = (len_left == 0);
656 unsigned int len;
657 dma_addr_t dma_addr;
658 unsigned int offset = 0;
659 skb_frag_t *frag;
661 /* Preload TCP csum field with IP pseudo hdr calculated
662 * with IP length set to zero. HW will later add in length
663 * to each TCP segment resulting from the TSO.
666 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
667 ip_hdr(skb)->check = 0;
668 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
669 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
670 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
671 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
672 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
675 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
676 * for the main skb fragment
678 while (frag_len_left) {
679 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
680 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
681 len, PCI_DMA_TODEVICE);
682 enic_queue_wq_desc_tso(wq, skb,
683 dma_addr,
684 len,
685 mss, hdr_len,
686 vlan_tag_insert, vlan_tag,
687 eop && (len == frag_len_left), loopback);
688 frag_len_left -= len;
689 offset += len;
692 if (eop)
693 return;
695 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
696 * for additional data fragments
698 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
699 len_left -= frag->size;
700 frag_len_left = frag->size;
701 offset = frag->page_offset;
703 while (frag_len_left) {
704 len = min(frag_len_left,
705 (unsigned int)WQ_ENET_MAX_DESC_LEN);
706 dma_addr = pci_map_page(enic->pdev, frag->page,
707 offset, len,
708 PCI_DMA_TODEVICE);
709 enic_queue_wq_desc_cont(wq, skb,
710 dma_addr,
711 len,
712 (len_left == 0) &&
713 (len == frag_len_left), /* EOP? */
714 loopback);
715 frag_len_left -= len;
716 offset += len;
721 static inline void enic_queue_wq_skb(struct enic *enic,
722 struct vnic_wq *wq, struct sk_buff *skb)
724 unsigned int mss = skb_shinfo(skb)->gso_size;
725 unsigned int vlan_tag = 0;
726 int vlan_tag_insert = 0;
727 int loopback = 0;
729 if (vlan_tx_tag_present(skb)) {
730 /* VLAN tag from trunking driver */
731 vlan_tag_insert = 1;
732 vlan_tag = vlan_tx_tag_get(skb);
733 } else if (enic->loop_enable) {
734 vlan_tag = enic->loop_tag;
735 loopback = 1;
738 if (mss)
739 enic_queue_wq_skb_tso(enic, wq, skb, mss,
740 vlan_tag_insert, vlan_tag, loopback);
741 else if (skb->ip_summed == CHECKSUM_PARTIAL)
742 enic_queue_wq_skb_csum_l4(enic, wq, skb,
743 vlan_tag_insert, vlan_tag, loopback);
744 else
745 enic_queue_wq_skb_vlan(enic, wq, skb,
746 vlan_tag_insert, vlan_tag, loopback);
749 /* netif_tx_lock held, process context with BHs disabled, or BH */
750 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
751 struct net_device *netdev)
753 struct enic *enic = netdev_priv(netdev);
754 struct vnic_wq *wq = &enic->wq[0];
755 unsigned long flags;
757 if (skb->len <= 0) {
758 dev_kfree_skb(skb);
759 return NETDEV_TX_OK;
762 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
763 * which is very likely. In the off chance it's going to take
764 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
767 if (skb_shinfo(skb)->gso_size == 0 &&
768 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
769 skb_linearize(skb)) {
770 dev_kfree_skb(skb);
771 return NETDEV_TX_OK;
774 spin_lock_irqsave(&enic->wq_lock[0], flags);
776 if (vnic_wq_desc_avail(wq) <
777 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
778 netif_stop_queue(netdev);
779 /* This is a hard error, log it */
780 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
781 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
782 return NETDEV_TX_BUSY;
785 enic_queue_wq_skb(enic, wq, skb);
787 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
788 netif_stop_queue(netdev);
790 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
792 return NETDEV_TX_OK;
795 /* dev_base_lock rwlock held, nominally process context */
796 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
798 struct enic *enic = netdev_priv(netdev);
799 struct net_device_stats *net_stats = &netdev->stats;
800 struct vnic_stats *stats;
802 enic_dev_stats_dump(enic, &stats);
804 net_stats->tx_packets = stats->tx.tx_frames_ok;
805 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
806 net_stats->tx_errors = stats->tx.tx_errors;
807 net_stats->tx_dropped = stats->tx.tx_drops;
809 net_stats->rx_packets = stats->rx.rx_frames_ok;
810 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
811 net_stats->rx_errors = stats->rx.rx_errors;
812 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
813 net_stats->rx_over_errors = enic->rq_truncated_pkts;
814 net_stats->rx_crc_errors = enic->rq_bad_fcs;
815 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
817 return net_stats;
820 void enic_reset_addr_lists(struct enic *enic)
822 enic->mc_count = 0;
823 enic->uc_count = 0;
824 enic->flags = 0;
827 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
829 struct enic *enic = netdev_priv(netdev);
831 if (enic_is_dynamic(enic)) {
832 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
833 return -EADDRNOTAVAIL;
834 } else {
835 if (!is_valid_ether_addr(addr))
836 return -EADDRNOTAVAIL;
839 memcpy(netdev->dev_addr, addr, netdev->addr_len);
841 return 0;
844 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
846 struct enic *enic = netdev_priv(netdev);
847 struct sockaddr *saddr = p;
848 char *addr = saddr->sa_data;
849 int err;
851 if (netif_running(enic->netdev)) {
852 err = enic_dev_del_station_addr(enic);
853 if (err)
854 return err;
857 err = enic_set_mac_addr(netdev, addr);
858 if (err)
859 return err;
861 if (netif_running(enic->netdev)) {
862 err = enic_dev_add_station_addr(enic);
863 if (err)
864 return err;
867 return err;
870 static int enic_set_mac_address(struct net_device *netdev, void *p)
872 struct sockaddr *saddr = p;
873 char *addr = saddr->sa_data;
874 struct enic *enic = netdev_priv(netdev);
875 int err;
877 err = enic_dev_del_station_addr(enic);
878 if (err)
879 return err;
881 err = enic_set_mac_addr(netdev, addr);
882 if (err)
883 return err;
885 return enic_dev_add_station_addr(enic);
888 static void enic_update_multicast_addr_list(struct enic *enic)
890 struct net_device *netdev = enic->netdev;
891 struct netdev_hw_addr *ha;
892 unsigned int mc_count = netdev_mc_count(netdev);
893 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
894 unsigned int i, j;
896 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
897 netdev_warn(netdev, "Registering only %d out of %d "
898 "multicast addresses\n",
899 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
900 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
903 /* Is there an easier way? Trying to minimize to
904 * calls to add/del multicast addrs. We keep the
905 * addrs from the last call in enic->mc_addr and
906 * look for changes to add/del.
909 i = 0;
910 netdev_for_each_mc_addr(ha, netdev) {
911 if (i == mc_count)
912 break;
913 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
916 for (i = 0; i < enic->mc_count; i++) {
917 for (j = 0; j < mc_count; j++)
918 if (compare_ether_addr(enic->mc_addr[i],
919 mc_addr[j]) == 0)
920 break;
921 if (j == mc_count)
922 enic_dev_del_addr(enic, enic->mc_addr[i]);
925 for (i = 0; i < mc_count; i++) {
926 for (j = 0; j < enic->mc_count; j++)
927 if (compare_ether_addr(mc_addr[i],
928 enic->mc_addr[j]) == 0)
929 break;
930 if (j == enic->mc_count)
931 enic_dev_add_addr(enic, mc_addr[i]);
934 /* Save the list to compare against next time
937 for (i = 0; i < mc_count; i++)
938 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
940 enic->mc_count = mc_count;
943 static void enic_update_unicast_addr_list(struct enic *enic)
945 struct net_device *netdev = enic->netdev;
946 struct netdev_hw_addr *ha;
947 unsigned int uc_count = netdev_uc_count(netdev);
948 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
949 unsigned int i, j;
951 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
952 netdev_warn(netdev, "Registering only %d out of %d "
953 "unicast addresses\n",
954 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
955 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
958 /* Is there an easier way? Trying to minimize to
959 * calls to add/del unicast addrs. We keep the
960 * addrs from the last call in enic->uc_addr and
961 * look for changes to add/del.
964 i = 0;
965 netdev_for_each_uc_addr(ha, netdev) {
966 if (i == uc_count)
967 break;
968 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
971 for (i = 0; i < enic->uc_count; i++) {
972 for (j = 0; j < uc_count; j++)
973 if (compare_ether_addr(enic->uc_addr[i],
974 uc_addr[j]) == 0)
975 break;
976 if (j == uc_count)
977 enic_dev_del_addr(enic, enic->uc_addr[i]);
980 for (i = 0; i < uc_count; i++) {
981 for (j = 0; j < enic->uc_count; j++)
982 if (compare_ether_addr(uc_addr[i],
983 enic->uc_addr[j]) == 0)
984 break;
985 if (j == enic->uc_count)
986 enic_dev_add_addr(enic, uc_addr[i]);
989 /* Save the list to compare against next time
992 for (i = 0; i < uc_count; i++)
993 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
995 enic->uc_count = uc_count;
998 /* netif_tx_lock held, BHs disabled */
999 static void enic_set_rx_mode(struct net_device *netdev)
1001 struct enic *enic = netdev_priv(netdev);
1002 int directed = 1;
1003 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1004 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1005 int promisc = (netdev->flags & IFF_PROMISC) ||
1006 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1007 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1008 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1009 unsigned int flags = netdev->flags |
1010 (allmulti ? IFF_ALLMULTI : 0) |
1011 (promisc ? IFF_PROMISC : 0);
1013 if (enic->flags != flags) {
1014 enic->flags = flags;
1015 enic_dev_packet_filter(enic, directed,
1016 multicast, broadcast, promisc, allmulti);
1019 if (!promisc) {
1020 enic_update_unicast_addr_list(enic);
1021 if (!allmulti)
1022 enic_update_multicast_addr_list(enic);
1026 /* rtnl lock is held */
1027 static void enic_vlan_rx_register(struct net_device *netdev,
1028 struct vlan_group *vlan_group)
1030 struct enic *enic = netdev_priv(netdev);
1031 enic->vlan_group = vlan_group;
1034 /* netif_tx_lock held, BHs disabled */
1035 static void enic_tx_timeout(struct net_device *netdev)
1037 struct enic *enic = netdev_priv(netdev);
1038 schedule_work(&enic->reset);
1041 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1043 struct enic *enic = netdev_priv(netdev);
1045 if (vf != PORT_SELF_VF)
1046 return -EOPNOTSUPP;
1048 /* Ignore the vf argument for now. We can assume the request
1049 * is coming on a vf.
1051 if (is_valid_ether_addr(mac)) {
1052 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1053 return 0;
1054 } else
1055 return -EINVAL;
1058 static int enic_set_vf_port(struct net_device *netdev, int vf,
1059 struct nlattr *port[])
1061 struct enic *enic = netdev_priv(netdev);
1062 struct enic_port_profile prev_pp;
1063 int err = 0, restore_pp = 1;
1065 /* don't support VFs, yet */
1066 if (vf != PORT_SELF_VF)
1067 return -EOPNOTSUPP;
1069 if (!port[IFLA_PORT_REQUEST])
1070 return -EOPNOTSUPP;
1072 memcpy(&prev_pp, &enic->pp, sizeof(enic->pp));
1073 memset(&enic->pp, 0, sizeof(enic->pp));
1075 enic->pp.set |= ENIC_SET_REQUEST;
1076 enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1078 if (port[IFLA_PORT_PROFILE]) {
1079 enic->pp.set |= ENIC_SET_NAME;
1080 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
1081 PORT_PROFILE_MAX);
1084 if (port[IFLA_PORT_INSTANCE_UUID]) {
1085 enic->pp.set |= ENIC_SET_INSTANCE;
1086 memcpy(enic->pp.instance_uuid,
1087 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1090 if (port[IFLA_PORT_HOST_UUID]) {
1091 enic->pp.set |= ENIC_SET_HOST;
1092 memcpy(enic->pp.host_uuid,
1093 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1096 /* Special case handling: mac came from IFLA_VF_MAC */
1097 if (!is_zero_ether_addr(prev_pp.vf_mac))
1098 memcpy(enic->pp.mac_addr, prev_pp.vf_mac, ETH_ALEN);
1100 if (is_zero_ether_addr(netdev->dev_addr))
1101 random_ether_addr(netdev->dev_addr);
1103 err = enic_process_set_pp_request(enic, &prev_pp, &restore_pp);
1104 if (err) {
1105 if (restore_pp) {
1106 /* Things are still the way they were: Implicit
1107 * DISASSOCIATE failed
1109 memcpy(&enic->pp, &prev_pp, sizeof(enic->pp));
1110 } else {
1111 memset(&enic->pp, 0, sizeof(enic->pp));
1112 memset(netdev->dev_addr, 0, ETH_ALEN);
1114 } else {
1115 /* Set flag to indicate that the port assoc/disassoc
1116 * request has been sent out to fw
1118 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1120 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1121 if (enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1122 memset(enic->pp.mac_addr, 0, ETH_ALEN);
1123 memset(netdev->dev_addr, 0, ETH_ALEN);
1127 memset(enic->pp.vf_mac, 0, ETH_ALEN);
1129 return err;
1132 static int enic_get_vf_port(struct net_device *netdev, int vf,
1133 struct sk_buff *skb)
1135 struct enic *enic = netdev_priv(netdev);
1136 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1137 int err;
1139 if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
1140 return -ENODATA;
1142 err = enic_process_get_pp_request(enic, enic->pp.request, &response);
1143 if (err)
1144 return err;
1146 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1147 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
1148 if (enic->pp.set & ENIC_SET_NAME)
1149 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1150 enic->pp.name);
1151 if (enic->pp.set & ENIC_SET_INSTANCE)
1152 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1153 enic->pp.instance_uuid);
1154 if (enic->pp.set & ENIC_SET_HOST)
1155 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1156 enic->pp.host_uuid);
1158 return 0;
1160 nla_put_failure:
1161 return -EMSGSIZE;
1164 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1166 struct enic *enic = vnic_dev_priv(rq->vdev);
1168 if (!buf->os_buf)
1169 return;
1171 pci_unmap_single(enic->pdev, buf->dma_addr,
1172 buf->len, PCI_DMA_FROMDEVICE);
1173 dev_kfree_skb_any(buf->os_buf);
1176 static int enic_rq_alloc_buf(struct vnic_rq *rq)
1178 struct enic *enic = vnic_dev_priv(rq->vdev);
1179 struct net_device *netdev = enic->netdev;
1180 struct sk_buff *skb;
1181 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
1182 unsigned int os_buf_index = 0;
1183 dma_addr_t dma_addr;
1185 skb = netdev_alloc_skb_ip_align(netdev, len);
1186 if (!skb)
1187 return -ENOMEM;
1189 dma_addr = pci_map_single(enic->pdev, skb->data,
1190 len, PCI_DMA_FROMDEVICE);
1192 enic_queue_rq_desc(rq, skb, os_buf_index,
1193 dma_addr, len);
1195 return 0;
1198 static void enic_rq_indicate_buf(struct vnic_rq *rq,
1199 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1200 int skipped, void *opaque)
1202 struct enic *enic = vnic_dev_priv(rq->vdev);
1203 struct net_device *netdev = enic->netdev;
1204 struct sk_buff *skb;
1206 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1207 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1208 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1209 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1210 u8 packet_error;
1211 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1212 u32 rss_hash;
1214 if (skipped)
1215 return;
1217 skb = buf->os_buf;
1218 prefetch(skb->data - NET_IP_ALIGN);
1219 pci_unmap_single(enic->pdev, buf->dma_addr,
1220 buf->len, PCI_DMA_FROMDEVICE);
1222 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1223 &type, &color, &q_number, &completed_index,
1224 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1225 &csum_not_calc, &rss_hash, &bytes_written,
1226 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
1227 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1228 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1229 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1230 &fcs_ok);
1232 if (packet_error) {
1234 if (!fcs_ok) {
1235 if (bytes_written > 0)
1236 enic->rq_bad_fcs++;
1237 else if (bytes_written == 0)
1238 enic->rq_truncated_pkts++;
1241 dev_kfree_skb_any(skb);
1243 return;
1246 if (eop && bytes_written > 0) {
1248 /* Good receive
1251 skb_put(skb, bytes_written);
1252 skb->protocol = eth_type_trans(skb, netdev);
1254 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
1255 skb->csum = htons(checksum);
1256 skb->ip_summed = CHECKSUM_COMPLETE;
1259 skb->dev = netdev;
1261 if (enic->vlan_group && vlan_stripped &&
1262 (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
1264 if (netdev->features & NETIF_F_GRO)
1265 vlan_gro_receive(&enic->napi[q_number],
1266 enic->vlan_group, vlan_tci, skb);
1267 else
1268 vlan_hwaccel_receive_skb(skb,
1269 enic->vlan_group, vlan_tci);
1271 } else {
1273 if (netdev->features & NETIF_F_GRO)
1274 napi_gro_receive(&enic->napi[q_number], skb);
1275 else
1276 netif_receive_skb(skb);
1279 } else {
1281 /* Buffer overflow
1284 dev_kfree_skb_any(skb);
1288 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1289 u8 type, u16 q_number, u16 completed_index, void *opaque)
1291 struct enic *enic = vnic_dev_priv(vdev);
1293 vnic_rq_service(&enic->rq[q_number], cq_desc,
1294 completed_index, VNIC_RQ_RETURN_DESC,
1295 enic_rq_indicate_buf, opaque);
1297 return 0;
1300 static int enic_poll(struct napi_struct *napi, int budget)
1302 struct net_device *netdev = napi->dev;
1303 struct enic *enic = netdev_priv(netdev);
1304 unsigned int cq_rq = enic_cq_rq(enic, 0);
1305 unsigned int cq_wq = enic_cq_wq(enic, 0);
1306 unsigned int intr = enic_legacy_io_intr();
1307 unsigned int rq_work_to_do = budget;
1308 unsigned int wq_work_to_do = -1; /* no limit */
1309 unsigned int work_done, rq_work_done, wq_work_done;
1310 int err;
1312 /* Service RQ (first) and WQ
1315 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1316 rq_work_to_do, enic_rq_service, NULL);
1318 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
1319 wq_work_to_do, enic_wq_service, NULL);
1321 /* Accumulate intr event credits for this polling
1322 * cycle. An intr event is the completion of a
1323 * a WQ or RQ packet.
1326 work_done = rq_work_done + wq_work_done;
1328 if (work_done > 0)
1329 vnic_intr_return_credits(&enic->intr[intr],
1330 work_done,
1331 0 /* don't unmask intr */,
1332 0 /* don't reset intr timer */);
1334 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1336 /* Buffer allocation failed. Stay in polling
1337 * mode so we can try to fill the ring again.
1340 if (err)
1341 rq_work_done = rq_work_to_do;
1343 if (rq_work_done < rq_work_to_do) {
1345 /* Some work done, but not enough to stay in polling,
1346 * exit polling
1349 napi_complete(napi);
1350 vnic_intr_unmask(&enic->intr[intr]);
1353 return rq_work_done;
1356 static int enic_poll_msix(struct napi_struct *napi, int budget)
1358 struct net_device *netdev = napi->dev;
1359 struct enic *enic = netdev_priv(netdev);
1360 unsigned int rq = (napi - &enic->napi[0]);
1361 unsigned int cq = enic_cq_rq(enic, rq);
1362 unsigned int intr = enic_msix_rq_intr(enic, rq);
1363 unsigned int work_to_do = budget;
1364 unsigned int work_done;
1365 int err;
1367 /* Service RQ
1370 work_done = vnic_cq_service(&enic->cq[cq],
1371 work_to_do, enic_rq_service, NULL);
1373 /* Return intr event credits for this polling
1374 * cycle. An intr event is the completion of a
1375 * RQ packet.
1378 if (work_done > 0)
1379 vnic_intr_return_credits(&enic->intr[intr],
1380 work_done,
1381 0 /* don't unmask intr */,
1382 0 /* don't reset intr timer */);
1384 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1386 /* Buffer allocation failed. Stay in polling mode
1387 * so we can try to fill the ring again.
1390 if (err)
1391 work_done = work_to_do;
1393 if (work_done < work_to_do) {
1395 /* Some work done, but not enough to stay in polling,
1396 * exit polling
1399 napi_complete(napi);
1400 vnic_intr_unmask(&enic->intr[intr]);
1403 return work_done;
1406 static void enic_notify_timer(unsigned long data)
1408 struct enic *enic = (struct enic *)data;
1410 enic_notify_check(enic);
1412 mod_timer(&enic->notify_timer,
1413 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1416 static void enic_free_intr(struct enic *enic)
1418 struct net_device *netdev = enic->netdev;
1419 unsigned int i;
1421 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1422 case VNIC_DEV_INTR_MODE_INTX:
1423 free_irq(enic->pdev->irq, netdev);
1424 break;
1425 case VNIC_DEV_INTR_MODE_MSI:
1426 free_irq(enic->pdev->irq, enic);
1427 break;
1428 case VNIC_DEV_INTR_MODE_MSIX:
1429 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1430 if (enic->msix[i].requested)
1431 free_irq(enic->msix_entry[i].vector,
1432 enic->msix[i].devid);
1433 break;
1434 default:
1435 break;
1439 static int enic_request_intr(struct enic *enic)
1441 struct net_device *netdev = enic->netdev;
1442 unsigned int i, intr;
1443 int err = 0;
1445 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1447 case VNIC_DEV_INTR_MODE_INTX:
1449 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1450 IRQF_SHARED, netdev->name, netdev);
1451 break;
1453 case VNIC_DEV_INTR_MODE_MSI:
1455 err = request_irq(enic->pdev->irq, enic_isr_msi,
1456 0, netdev->name, enic);
1457 break;
1459 case VNIC_DEV_INTR_MODE_MSIX:
1461 for (i = 0; i < enic->rq_count; i++) {
1462 intr = enic_msix_rq_intr(enic, i);
1463 sprintf(enic->msix[intr].devname,
1464 "%.11s-rx-%d", netdev->name, i);
1465 enic->msix[intr].isr = enic_isr_msix_rq;
1466 enic->msix[intr].devid = &enic->napi[i];
1469 for (i = 0; i < enic->wq_count; i++) {
1470 intr = enic_msix_wq_intr(enic, i);
1471 sprintf(enic->msix[intr].devname,
1472 "%.11s-tx-%d", netdev->name, i);
1473 enic->msix[intr].isr = enic_isr_msix_wq;
1474 enic->msix[intr].devid = enic;
1477 intr = enic_msix_err_intr(enic);
1478 sprintf(enic->msix[intr].devname,
1479 "%.11s-err", netdev->name);
1480 enic->msix[intr].isr = enic_isr_msix_err;
1481 enic->msix[intr].devid = enic;
1483 intr = enic_msix_notify_intr(enic);
1484 sprintf(enic->msix[intr].devname,
1485 "%.11s-notify", netdev->name);
1486 enic->msix[intr].isr = enic_isr_msix_notify;
1487 enic->msix[intr].devid = enic;
1489 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1490 enic->msix[i].requested = 0;
1492 for (i = 0; i < enic->intr_count; i++) {
1493 err = request_irq(enic->msix_entry[i].vector,
1494 enic->msix[i].isr, 0,
1495 enic->msix[i].devname,
1496 enic->msix[i].devid);
1497 if (err) {
1498 enic_free_intr(enic);
1499 break;
1501 enic->msix[i].requested = 1;
1504 break;
1506 default:
1507 break;
1510 return err;
1513 static void enic_synchronize_irqs(struct enic *enic)
1515 unsigned int i;
1517 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1518 case VNIC_DEV_INTR_MODE_INTX:
1519 case VNIC_DEV_INTR_MODE_MSI:
1520 synchronize_irq(enic->pdev->irq);
1521 break;
1522 case VNIC_DEV_INTR_MODE_MSIX:
1523 for (i = 0; i < enic->intr_count; i++)
1524 synchronize_irq(enic->msix_entry[i].vector);
1525 break;
1526 default:
1527 break;
1531 static int enic_dev_notify_set(struct enic *enic)
1533 int err;
1535 spin_lock(&enic->devcmd_lock);
1536 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1537 case VNIC_DEV_INTR_MODE_INTX:
1538 err = vnic_dev_notify_set(enic->vdev,
1539 enic_legacy_notify_intr());
1540 break;
1541 case VNIC_DEV_INTR_MODE_MSIX:
1542 err = vnic_dev_notify_set(enic->vdev,
1543 enic_msix_notify_intr(enic));
1544 break;
1545 default:
1546 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1547 break;
1549 spin_unlock(&enic->devcmd_lock);
1551 return err;
1554 static void enic_notify_timer_start(struct enic *enic)
1556 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1557 case VNIC_DEV_INTR_MODE_MSI:
1558 mod_timer(&enic->notify_timer, jiffies);
1559 break;
1560 default:
1561 /* Using intr for notification for INTx/MSI-X */
1562 break;
1566 /* rtnl lock is held, process context */
1567 static int enic_open(struct net_device *netdev)
1569 struct enic *enic = netdev_priv(netdev);
1570 unsigned int i;
1571 int err;
1573 err = enic_request_intr(enic);
1574 if (err) {
1575 netdev_err(netdev, "Unable to request irq.\n");
1576 return err;
1579 err = enic_dev_notify_set(enic);
1580 if (err) {
1581 netdev_err(netdev,
1582 "Failed to alloc notify buffer, aborting.\n");
1583 goto err_out_free_intr;
1586 for (i = 0; i < enic->rq_count; i++) {
1587 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1588 /* Need at least one buffer on ring to get going */
1589 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1590 netdev_err(netdev, "Unable to alloc receive buffers\n");
1591 err = -ENOMEM;
1592 goto err_out_notify_unset;
1596 for (i = 0; i < enic->wq_count; i++)
1597 vnic_wq_enable(&enic->wq[i]);
1598 for (i = 0; i < enic->rq_count; i++)
1599 vnic_rq_enable(&enic->rq[i]);
1601 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1602 enic_dev_add_addr(enic, enic->pp.mac_addr);
1603 else
1604 enic_dev_add_station_addr(enic);
1605 enic_set_rx_mode(netdev);
1607 netif_wake_queue(netdev);
1609 for (i = 0; i < enic->rq_count; i++)
1610 napi_enable(&enic->napi[i]);
1612 enic_dev_enable(enic);
1614 for (i = 0; i < enic->intr_count; i++)
1615 vnic_intr_unmask(&enic->intr[i]);
1617 enic_notify_timer_start(enic);
1619 return 0;
1621 err_out_notify_unset:
1622 enic_dev_notify_unset(enic);
1623 err_out_free_intr:
1624 enic_free_intr(enic);
1626 return err;
1629 /* rtnl lock is held, process context */
1630 static int enic_stop(struct net_device *netdev)
1632 struct enic *enic = netdev_priv(netdev);
1633 unsigned int i;
1634 int err;
1636 for (i = 0; i < enic->intr_count; i++) {
1637 vnic_intr_mask(&enic->intr[i]);
1638 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1641 enic_synchronize_irqs(enic);
1643 del_timer_sync(&enic->notify_timer);
1645 enic_dev_disable(enic);
1647 for (i = 0; i < enic->rq_count; i++)
1648 napi_disable(&enic->napi[i]);
1650 netif_carrier_off(netdev);
1651 netif_tx_disable(netdev);
1652 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1653 enic_dev_del_addr(enic, enic->pp.mac_addr);
1654 else
1655 enic_dev_del_station_addr(enic);
1657 for (i = 0; i < enic->wq_count; i++) {
1658 err = vnic_wq_disable(&enic->wq[i]);
1659 if (err)
1660 return err;
1662 for (i = 0; i < enic->rq_count; i++) {
1663 err = vnic_rq_disable(&enic->rq[i]);
1664 if (err)
1665 return err;
1668 enic_dev_notify_unset(enic);
1669 enic_free_intr(enic);
1671 for (i = 0; i < enic->wq_count; i++)
1672 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1673 for (i = 0; i < enic->rq_count; i++)
1674 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1675 for (i = 0; i < enic->cq_count; i++)
1676 vnic_cq_clean(&enic->cq[i]);
1677 for (i = 0; i < enic->intr_count; i++)
1678 vnic_intr_clean(&enic->intr[i]);
1680 return 0;
1683 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1685 struct enic *enic = netdev_priv(netdev);
1686 int running = netif_running(netdev);
1688 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1689 return -EINVAL;
1691 if (running)
1692 enic_stop(netdev);
1694 netdev->mtu = new_mtu;
1696 if (netdev->mtu > enic->port_mtu)
1697 netdev_warn(netdev,
1698 "interface MTU (%d) set higher than port MTU (%d)\n",
1699 netdev->mtu, enic->port_mtu);
1701 if (running)
1702 enic_open(netdev);
1704 return 0;
1707 #ifdef CONFIG_NET_POLL_CONTROLLER
1708 static void enic_poll_controller(struct net_device *netdev)
1710 struct enic *enic = netdev_priv(netdev);
1711 struct vnic_dev *vdev = enic->vdev;
1712 unsigned int i, intr;
1714 switch (vnic_dev_get_intr_mode(vdev)) {
1715 case VNIC_DEV_INTR_MODE_MSIX:
1716 for (i = 0; i < enic->rq_count; i++) {
1717 intr = enic_msix_rq_intr(enic, i);
1718 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1719 &enic->napi[i]);
1722 for (i = 0; i < enic->wq_count; i++) {
1723 intr = enic_msix_wq_intr(enic, i);
1724 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1727 break;
1728 case VNIC_DEV_INTR_MODE_MSI:
1729 enic_isr_msi(enic->pdev->irq, enic);
1730 break;
1731 case VNIC_DEV_INTR_MODE_INTX:
1732 enic_isr_legacy(enic->pdev->irq, netdev);
1733 break;
1734 default:
1735 break;
1738 #endif
1740 static int enic_dev_wait(struct vnic_dev *vdev,
1741 int (*start)(struct vnic_dev *, int),
1742 int (*finished)(struct vnic_dev *, int *),
1743 int arg)
1745 unsigned long time;
1746 int done;
1747 int err;
1749 BUG_ON(in_interrupt());
1751 err = start(vdev, arg);
1752 if (err)
1753 return err;
1755 /* Wait for func to complete...2 seconds max
1758 time = jiffies + (HZ * 2);
1759 do {
1761 err = finished(vdev, &done);
1762 if (err)
1763 return err;
1765 if (done)
1766 return 0;
1768 schedule_timeout_uninterruptible(HZ / 10);
1770 } while (time_after(time, jiffies));
1772 return -ETIMEDOUT;
1775 static int enic_dev_open(struct enic *enic)
1777 int err;
1779 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1780 vnic_dev_open_done, 0);
1781 if (err)
1782 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1783 err);
1785 return err;
1788 static int enic_dev_hang_reset(struct enic *enic)
1790 int err;
1792 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1793 vnic_dev_hang_reset_done, 0);
1794 if (err)
1795 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1796 err);
1798 return err;
1801 static int enic_set_rsskey(struct enic *enic)
1803 dma_addr_t rss_key_buf_pa;
1804 union vnic_rss_key *rss_key_buf_va = NULL;
1805 union vnic_rss_key rss_key = {
1806 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1807 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1808 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1809 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1811 int err;
1813 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1814 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1815 if (!rss_key_buf_va)
1816 return -ENOMEM;
1818 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1820 spin_lock(&enic->devcmd_lock);
1821 err = enic_set_rss_key(enic,
1822 rss_key_buf_pa,
1823 sizeof(union vnic_rss_key));
1824 spin_unlock(&enic->devcmd_lock);
1826 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1827 rss_key_buf_va, rss_key_buf_pa);
1829 return err;
1832 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1834 dma_addr_t rss_cpu_buf_pa;
1835 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1836 unsigned int i;
1837 int err;
1839 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1840 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1841 if (!rss_cpu_buf_va)
1842 return -ENOMEM;
1844 for (i = 0; i < (1 << rss_hash_bits); i++)
1845 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1847 spin_lock(&enic->devcmd_lock);
1848 err = enic_set_rss_cpu(enic,
1849 rss_cpu_buf_pa,
1850 sizeof(union vnic_rss_cpu));
1851 spin_unlock(&enic->devcmd_lock);
1853 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1854 rss_cpu_buf_va, rss_cpu_buf_pa);
1856 return err;
1859 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1860 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1862 const u8 tso_ipid_split_en = 0;
1863 const u8 ig_vlan_strip_en = 1;
1864 int err;
1866 /* Enable VLAN tag stripping.
1869 spin_lock(&enic->devcmd_lock);
1870 err = enic_set_nic_cfg(enic,
1871 rss_default_cpu, rss_hash_type,
1872 rss_hash_bits, rss_base_cpu,
1873 rss_enable, tso_ipid_split_en,
1874 ig_vlan_strip_en);
1875 spin_unlock(&enic->devcmd_lock);
1877 return err;
1880 static int enic_set_rss_nic_cfg(struct enic *enic)
1882 struct device *dev = enic_get_dev(enic);
1883 const u8 rss_default_cpu = 0;
1884 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1885 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1886 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1887 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1888 const u8 rss_hash_bits = 7;
1889 const u8 rss_base_cpu = 0;
1890 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1892 if (rss_enable) {
1893 if (!enic_set_rsskey(enic)) {
1894 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1895 rss_enable = 0;
1896 dev_warn(dev, "RSS disabled, "
1897 "Failed to set RSS cpu indirection table.");
1899 } else {
1900 rss_enable = 0;
1901 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1905 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1906 rss_hash_bits, rss_base_cpu, rss_enable);
1909 static void enic_reset(struct work_struct *work)
1911 struct enic *enic = container_of(work, struct enic, reset);
1913 if (!netif_running(enic->netdev))
1914 return;
1916 rtnl_lock();
1918 enic_dev_hang_notify(enic);
1919 enic_stop(enic->netdev);
1920 enic_dev_hang_reset(enic);
1921 enic_reset_addr_lists(enic);
1922 enic_init_vnic_resources(enic);
1923 enic_set_rss_nic_cfg(enic);
1924 enic_dev_set_ig_vlan_rewrite_mode(enic);
1925 enic_open(enic->netdev);
1927 rtnl_unlock();
1930 static int enic_set_intr_mode(struct enic *enic)
1932 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
1933 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
1934 unsigned int i;
1936 /* Set interrupt mode (INTx, MSI, MSI-X) depending
1937 * on system capabilities.
1939 * Try MSI-X first
1941 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1942 * (the second to last INTR is used for WQ/RQ errors)
1943 * (the last INTR is used for notifications)
1946 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
1947 for (i = 0; i < n + m + 2; i++)
1948 enic->msix_entry[i].entry = i;
1950 /* Use multiple RQs if RSS is enabled
1953 if (ENIC_SETTING(enic, RSS) &&
1954 enic->config.intr_mode < 1 &&
1955 enic->rq_count >= n &&
1956 enic->wq_count >= m &&
1957 enic->cq_count >= n + m &&
1958 enic->intr_count >= n + m + 2) {
1960 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
1962 enic->rq_count = n;
1963 enic->wq_count = m;
1964 enic->cq_count = n + m;
1965 enic->intr_count = n + m + 2;
1967 vnic_dev_set_intr_mode(enic->vdev,
1968 VNIC_DEV_INTR_MODE_MSIX);
1970 return 0;
1974 if (enic->config.intr_mode < 1 &&
1975 enic->rq_count >= 1 &&
1976 enic->wq_count >= m &&
1977 enic->cq_count >= 1 + m &&
1978 enic->intr_count >= 1 + m + 2) {
1979 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
1981 enic->rq_count = 1;
1982 enic->wq_count = m;
1983 enic->cq_count = 1 + m;
1984 enic->intr_count = 1 + m + 2;
1986 vnic_dev_set_intr_mode(enic->vdev,
1987 VNIC_DEV_INTR_MODE_MSIX);
1989 return 0;
1993 /* Next try MSI
1995 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
1998 if (enic->config.intr_mode < 2 &&
1999 enic->rq_count >= 1 &&
2000 enic->wq_count >= 1 &&
2001 enic->cq_count >= 2 &&
2002 enic->intr_count >= 1 &&
2003 !pci_enable_msi(enic->pdev)) {
2005 enic->rq_count = 1;
2006 enic->wq_count = 1;
2007 enic->cq_count = 2;
2008 enic->intr_count = 1;
2010 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2012 return 0;
2015 /* Next try INTx
2017 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2018 * (the first INTR is used for WQ/RQ)
2019 * (the second INTR is used for WQ/RQ errors)
2020 * (the last INTR is used for notifications)
2023 if (enic->config.intr_mode < 3 &&
2024 enic->rq_count >= 1 &&
2025 enic->wq_count >= 1 &&
2026 enic->cq_count >= 2 &&
2027 enic->intr_count >= 3) {
2029 enic->rq_count = 1;
2030 enic->wq_count = 1;
2031 enic->cq_count = 2;
2032 enic->intr_count = 3;
2034 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2036 return 0;
2039 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2041 return -EINVAL;
2044 static void enic_clear_intr_mode(struct enic *enic)
2046 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2047 case VNIC_DEV_INTR_MODE_MSIX:
2048 pci_disable_msix(enic->pdev);
2049 break;
2050 case VNIC_DEV_INTR_MODE_MSI:
2051 pci_disable_msi(enic->pdev);
2052 break;
2053 default:
2054 break;
2057 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2060 static const struct net_device_ops enic_netdev_dynamic_ops = {
2061 .ndo_open = enic_open,
2062 .ndo_stop = enic_stop,
2063 .ndo_start_xmit = enic_hard_start_xmit,
2064 .ndo_get_stats = enic_get_stats,
2065 .ndo_validate_addr = eth_validate_addr,
2066 .ndo_set_rx_mode = enic_set_rx_mode,
2067 .ndo_set_multicast_list = enic_set_rx_mode,
2068 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2069 .ndo_change_mtu = enic_change_mtu,
2070 .ndo_vlan_rx_register = enic_vlan_rx_register,
2071 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2072 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2073 .ndo_tx_timeout = enic_tx_timeout,
2074 .ndo_set_vf_port = enic_set_vf_port,
2075 .ndo_get_vf_port = enic_get_vf_port,
2076 .ndo_set_vf_mac = enic_set_vf_mac,
2077 #ifdef CONFIG_NET_POLL_CONTROLLER
2078 .ndo_poll_controller = enic_poll_controller,
2079 #endif
2082 static const struct net_device_ops enic_netdev_ops = {
2083 .ndo_open = enic_open,
2084 .ndo_stop = enic_stop,
2085 .ndo_start_xmit = enic_hard_start_xmit,
2086 .ndo_get_stats = enic_get_stats,
2087 .ndo_validate_addr = eth_validate_addr,
2088 .ndo_set_mac_address = enic_set_mac_address,
2089 .ndo_set_rx_mode = enic_set_rx_mode,
2090 .ndo_set_multicast_list = enic_set_rx_mode,
2091 .ndo_change_mtu = enic_change_mtu,
2092 .ndo_vlan_rx_register = enic_vlan_rx_register,
2093 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2094 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2095 .ndo_tx_timeout = enic_tx_timeout,
2096 #ifdef CONFIG_NET_POLL_CONTROLLER
2097 .ndo_poll_controller = enic_poll_controller,
2098 #endif
2101 static void enic_dev_deinit(struct enic *enic)
2103 unsigned int i;
2105 for (i = 0; i < enic->rq_count; i++)
2106 netif_napi_del(&enic->napi[i]);
2108 enic_free_vnic_resources(enic);
2109 enic_clear_intr_mode(enic);
2112 static int enic_dev_init(struct enic *enic)
2114 struct device *dev = enic_get_dev(enic);
2115 struct net_device *netdev = enic->netdev;
2116 unsigned int i;
2117 int err;
2119 /* Get vNIC configuration
2122 err = enic_get_vnic_config(enic);
2123 if (err) {
2124 dev_err(dev, "Get vNIC configuration failed, aborting\n");
2125 return err;
2128 /* Get available resource counts
2131 enic_get_res_counts(enic);
2133 /* Set interrupt mode based on resource counts and system
2134 * capabilities
2137 err = enic_set_intr_mode(enic);
2138 if (err) {
2139 dev_err(dev, "Failed to set intr mode based on resource "
2140 "counts and system capabilities, aborting\n");
2141 return err;
2144 /* Allocate and configure vNIC resources
2147 err = enic_alloc_vnic_resources(enic);
2148 if (err) {
2149 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2150 goto err_out_free_vnic_resources;
2153 enic_init_vnic_resources(enic);
2155 err = enic_set_rss_nic_cfg(enic);
2156 if (err) {
2157 dev_err(dev, "Failed to config nic, aborting\n");
2158 goto err_out_free_vnic_resources;
2161 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2162 default:
2163 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
2164 break;
2165 case VNIC_DEV_INTR_MODE_MSIX:
2166 for (i = 0; i < enic->rq_count; i++)
2167 netif_napi_add(netdev, &enic->napi[i],
2168 enic_poll_msix, 64);
2169 break;
2172 return 0;
2174 err_out_free_vnic_resources:
2175 enic_clear_intr_mode(enic);
2176 enic_free_vnic_resources(enic);
2178 return err;
2181 static void enic_iounmap(struct enic *enic)
2183 unsigned int i;
2185 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2186 if (enic->bar[i].vaddr)
2187 iounmap(enic->bar[i].vaddr);
2190 static int __devinit enic_probe(struct pci_dev *pdev,
2191 const struct pci_device_id *ent)
2193 struct device *dev = &pdev->dev;
2194 struct net_device *netdev;
2195 struct enic *enic;
2196 int using_dac = 0;
2197 unsigned int i;
2198 int err;
2200 /* Allocate net device structure and initialize. Private
2201 * instance data is initialized to zero.
2204 netdev = alloc_etherdev(sizeof(struct enic));
2205 if (!netdev) {
2206 pr_err("Etherdev alloc failed, aborting\n");
2207 return -ENOMEM;
2210 pci_set_drvdata(pdev, netdev);
2212 SET_NETDEV_DEV(netdev, &pdev->dev);
2214 enic = netdev_priv(netdev);
2215 enic->netdev = netdev;
2216 enic->pdev = pdev;
2218 /* Setup PCI resources
2221 err = pci_enable_device_mem(pdev);
2222 if (err) {
2223 dev_err(dev, "Cannot enable PCI device, aborting\n");
2224 goto err_out_free_netdev;
2227 err = pci_request_regions(pdev, DRV_NAME);
2228 if (err) {
2229 dev_err(dev, "Cannot request PCI regions, aborting\n");
2230 goto err_out_disable_device;
2233 pci_set_master(pdev);
2235 /* Query PCI controller on system for DMA addressing
2236 * limitation for the device. Try 40-bit first, and
2237 * fail to 32-bit.
2240 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
2241 if (err) {
2242 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2243 if (err) {
2244 dev_err(dev, "No usable DMA configuration, aborting\n");
2245 goto err_out_release_regions;
2247 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2248 if (err) {
2249 dev_err(dev, "Unable to obtain %u-bit DMA "
2250 "for consistent allocations, aborting\n", 32);
2251 goto err_out_release_regions;
2253 } else {
2254 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2255 if (err) {
2256 dev_err(dev, "Unable to obtain %u-bit DMA "
2257 "for consistent allocations, aborting\n", 40);
2258 goto err_out_release_regions;
2260 using_dac = 1;
2263 /* Map vNIC resources from BAR0-5
2266 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2267 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2268 continue;
2269 enic->bar[i].len = pci_resource_len(pdev, i);
2270 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2271 if (!enic->bar[i].vaddr) {
2272 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2273 err = -ENODEV;
2274 goto err_out_iounmap;
2276 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2279 /* Register vNIC device
2282 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2283 ARRAY_SIZE(enic->bar));
2284 if (!enic->vdev) {
2285 dev_err(dev, "vNIC registration failed, aborting\n");
2286 err = -ENODEV;
2287 goto err_out_iounmap;
2290 /* Issue device open to get device in known state
2293 err = enic_dev_open(enic);
2294 if (err) {
2295 dev_err(dev, "vNIC dev open failed, aborting\n");
2296 goto err_out_vnic_unregister;
2299 /* Setup devcmd lock
2302 spin_lock_init(&enic->devcmd_lock);
2305 * Set ingress vlan rewrite mode before vnic initialization
2308 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2309 if (err) {
2310 dev_err(dev,
2311 "Failed to set ingress vlan rewrite mode, aborting.\n");
2312 goto err_out_dev_close;
2315 /* Issue device init to initialize the vnic-to-switch link.
2316 * We'll start with carrier off and wait for link UP
2317 * notification later to turn on carrier. We don't need
2318 * to wait here for the vnic-to-switch link initialization
2319 * to complete; link UP notification is the indication that
2320 * the process is complete.
2323 netif_carrier_off(netdev);
2325 /* Do not call dev_init for a dynamic vnic.
2326 * For a dynamic vnic, init_prov_info will be
2327 * called later by an upper layer.
2330 if (!enic_is_dynamic(enic)) {
2331 err = vnic_dev_init(enic->vdev, 0);
2332 if (err) {
2333 dev_err(dev, "vNIC dev init failed, aborting\n");
2334 goto err_out_dev_close;
2338 err = enic_dev_init(enic);
2339 if (err) {
2340 dev_err(dev, "Device initialization failed, aborting\n");
2341 goto err_out_dev_close;
2344 /* Setup notification timer, HW reset task, and wq locks
2347 init_timer(&enic->notify_timer);
2348 enic->notify_timer.function = enic_notify_timer;
2349 enic->notify_timer.data = (unsigned long)enic;
2351 INIT_WORK(&enic->reset, enic_reset);
2353 for (i = 0; i < enic->wq_count; i++)
2354 spin_lock_init(&enic->wq_lock[i]);
2356 /* Register net device
2359 enic->port_mtu = enic->config.mtu;
2360 (void)enic_change_mtu(netdev, enic->port_mtu);
2362 err = enic_set_mac_addr(netdev, enic->mac_addr);
2363 if (err) {
2364 dev_err(dev, "Invalid MAC address, aborting\n");
2365 goto err_out_dev_deinit;
2368 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2369 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2371 if (enic_is_dynamic(enic))
2372 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2373 else
2374 netdev->netdev_ops = &enic_netdev_ops;
2376 netdev->watchdog_timeo = 2 * HZ;
2377 netdev->ethtool_ops = &enic_ethtool_ops;
2379 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2380 if (ENIC_SETTING(enic, LOOP)) {
2381 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2382 enic->loop_enable = 1;
2383 enic->loop_tag = enic->config.loop_tag;
2384 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2386 if (ENIC_SETTING(enic, TXCSUM))
2387 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2388 if (ENIC_SETTING(enic, TSO))
2389 netdev->hw_features |= NETIF_F_TSO |
2390 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2391 if (ENIC_SETTING(enic, RXCSUM))
2392 netdev->hw_features |= NETIF_F_RXCSUM;
2394 netdev->features |= netdev->hw_features;
2396 if (using_dac)
2397 netdev->features |= NETIF_F_HIGHDMA;
2399 err = register_netdev(netdev);
2400 if (err) {
2401 dev_err(dev, "Cannot register net device, aborting\n");
2402 goto err_out_dev_deinit;
2405 return 0;
2407 err_out_dev_deinit:
2408 enic_dev_deinit(enic);
2409 err_out_dev_close:
2410 vnic_dev_close(enic->vdev);
2411 err_out_vnic_unregister:
2412 vnic_dev_unregister(enic->vdev);
2413 err_out_iounmap:
2414 enic_iounmap(enic);
2415 err_out_release_regions:
2416 pci_release_regions(pdev);
2417 err_out_disable_device:
2418 pci_disable_device(pdev);
2419 err_out_free_netdev:
2420 pci_set_drvdata(pdev, NULL);
2421 free_netdev(netdev);
2423 return err;
2426 static void __devexit enic_remove(struct pci_dev *pdev)
2428 struct net_device *netdev = pci_get_drvdata(pdev);
2430 if (netdev) {
2431 struct enic *enic = netdev_priv(netdev);
2433 cancel_work_sync(&enic->reset);
2434 unregister_netdev(netdev);
2435 enic_dev_deinit(enic);
2436 vnic_dev_close(enic->vdev);
2437 vnic_dev_unregister(enic->vdev);
2438 enic_iounmap(enic);
2439 pci_release_regions(pdev);
2440 pci_disable_device(pdev);
2441 pci_set_drvdata(pdev, NULL);
2442 free_netdev(netdev);
2446 static struct pci_driver enic_driver = {
2447 .name = DRV_NAME,
2448 .id_table = enic_id_table,
2449 .probe = enic_probe,
2450 .remove = __devexit_p(enic_remove),
2453 static int __init enic_init_module(void)
2455 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2457 return pci_register_driver(&enic_driver);
2460 static void __exit enic_cleanup_module(void)
2462 pci_unregister_driver(&enic_driver);
2465 module_init(enic_init_module);
2466 module_exit(enic_cleanup_module);