Merge remote-tracking branch 'cleancache/linux-next'
[linux-2.6/next.git] / drivers / net / enic / enic_main.c
blob3d99b0f1a236a240bde0e8ccd85ed9efc7b060e2
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"
47 #include "enic_dev.h"
48 #include "enic_pp.h"
50 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
51 #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
52 #define MAX_TSO (1 << 16)
53 #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
55 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
56 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
58 /* Supported devices */
59 static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
60 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
61 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
62 { 0, } /* end of table */
65 MODULE_DESCRIPTION(DRV_DESCRIPTION);
66 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
67 MODULE_LICENSE("GPL");
68 MODULE_VERSION(DRV_VERSION);
69 MODULE_DEVICE_TABLE(pci, enic_id_table);
71 struct enic_stat {
72 char name[ETH_GSTRING_LEN];
73 unsigned int offset;
76 #define ENIC_TX_STAT(stat) \
77 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
78 #define ENIC_RX_STAT(stat) \
79 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
81 static const struct enic_stat enic_tx_stats[] = {
82 ENIC_TX_STAT(tx_frames_ok),
83 ENIC_TX_STAT(tx_unicast_frames_ok),
84 ENIC_TX_STAT(tx_multicast_frames_ok),
85 ENIC_TX_STAT(tx_broadcast_frames_ok),
86 ENIC_TX_STAT(tx_bytes_ok),
87 ENIC_TX_STAT(tx_unicast_bytes_ok),
88 ENIC_TX_STAT(tx_multicast_bytes_ok),
89 ENIC_TX_STAT(tx_broadcast_bytes_ok),
90 ENIC_TX_STAT(tx_drops),
91 ENIC_TX_STAT(tx_errors),
92 ENIC_TX_STAT(tx_tso),
95 static const struct enic_stat enic_rx_stats[] = {
96 ENIC_RX_STAT(rx_frames_ok),
97 ENIC_RX_STAT(rx_frames_total),
98 ENIC_RX_STAT(rx_unicast_frames_ok),
99 ENIC_RX_STAT(rx_multicast_frames_ok),
100 ENIC_RX_STAT(rx_broadcast_frames_ok),
101 ENIC_RX_STAT(rx_bytes_ok),
102 ENIC_RX_STAT(rx_unicast_bytes_ok),
103 ENIC_RX_STAT(rx_multicast_bytes_ok),
104 ENIC_RX_STAT(rx_broadcast_bytes_ok),
105 ENIC_RX_STAT(rx_drop),
106 ENIC_RX_STAT(rx_no_bufs),
107 ENIC_RX_STAT(rx_errors),
108 ENIC_RX_STAT(rx_rss),
109 ENIC_RX_STAT(rx_crc_errors),
110 ENIC_RX_STAT(rx_frames_64),
111 ENIC_RX_STAT(rx_frames_127),
112 ENIC_RX_STAT(rx_frames_255),
113 ENIC_RX_STAT(rx_frames_511),
114 ENIC_RX_STAT(rx_frames_1023),
115 ENIC_RX_STAT(rx_frames_1518),
116 ENIC_RX_STAT(rx_frames_to_max),
119 static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
120 static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
122 static int enic_is_dynamic(struct enic *enic)
124 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
127 static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
129 return rq;
132 static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
134 return enic->rq_count + wq;
137 static inline unsigned int enic_legacy_io_intr(void)
139 return 0;
142 static inline unsigned int enic_legacy_err_intr(void)
144 return 1;
147 static inline unsigned int enic_legacy_notify_intr(void)
149 return 2;
152 static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
154 return rq;
157 static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
159 return enic->rq_count + wq;
162 static inline unsigned int enic_msix_err_intr(struct enic *enic)
164 return enic->rq_count + enic->wq_count;
167 static inline unsigned int enic_msix_notify_intr(struct enic *enic)
169 return enic->rq_count + enic->wq_count + 1;
172 static int enic_get_settings(struct net_device *netdev,
173 struct ethtool_cmd *ecmd)
175 struct enic *enic = netdev_priv(netdev);
177 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
178 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
179 ecmd->port = PORT_FIBRE;
180 ecmd->transceiver = XCVR_EXTERNAL;
182 if (netif_carrier_ok(netdev)) {
183 ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
184 ecmd->duplex = DUPLEX_FULL;
185 } else {
186 ethtool_cmd_speed_set(ecmd, -1);
187 ecmd->duplex = -1;
190 ecmd->autoneg = AUTONEG_DISABLE;
192 return 0;
195 static void enic_get_drvinfo(struct net_device *netdev,
196 struct ethtool_drvinfo *drvinfo)
198 struct enic *enic = netdev_priv(netdev);
199 struct vnic_devcmd_fw_info *fw_info;
201 enic_dev_fw_info(enic, &fw_info);
203 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
204 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
205 strncpy(drvinfo->fw_version, fw_info->fw_version,
206 sizeof(drvinfo->fw_version));
207 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
208 sizeof(drvinfo->bus_info));
211 static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
213 unsigned int i;
215 switch (stringset) {
216 case ETH_SS_STATS:
217 for (i = 0; i < enic_n_tx_stats; i++) {
218 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
219 data += ETH_GSTRING_LEN;
221 for (i = 0; i < enic_n_rx_stats; i++) {
222 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
223 data += ETH_GSTRING_LEN;
225 break;
229 static int enic_get_sset_count(struct net_device *netdev, int sset)
231 switch (sset) {
232 case ETH_SS_STATS:
233 return enic_n_tx_stats + enic_n_rx_stats;
234 default:
235 return -EOPNOTSUPP;
239 static void enic_get_ethtool_stats(struct net_device *netdev,
240 struct ethtool_stats *stats, u64 *data)
242 struct enic *enic = netdev_priv(netdev);
243 struct vnic_stats *vstats;
244 unsigned int i;
246 enic_dev_stats_dump(enic, &vstats);
248 for (i = 0; i < enic_n_tx_stats; i++)
249 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
250 for (i = 0; i < enic_n_rx_stats; i++)
251 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
254 static u32 enic_get_msglevel(struct net_device *netdev)
256 struct enic *enic = netdev_priv(netdev);
257 return enic->msg_enable;
260 static void enic_set_msglevel(struct net_device *netdev, u32 value)
262 struct enic *enic = netdev_priv(netdev);
263 enic->msg_enable = value;
266 static int enic_get_coalesce(struct net_device *netdev,
267 struct ethtool_coalesce *ecmd)
269 struct enic *enic = netdev_priv(netdev);
271 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
272 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
274 return 0;
277 static int enic_set_coalesce(struct net_device *netdev,
278 struct ethtool_coalesce *ecmd)
280 struct enic *enic = netdev_priv(netdev);
281 u32 tx_coalesce_usecs;
282 u32 rx_coalesce_usecs;
283 unsigned int i, intr;
285 tx_coalesce_usecs = min_t(u32,
286 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
287 ecmd->tx_coalesce_usecs);
288 rx_coalesce_usecs = min_t(u32,
289 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
290 ecmd->rx_coalesce_usecs);
292 switch (vnic_dev_get_intr_mode(enic->vdev)) {
293 case VNIC_DEV_INTR_MODE_INTX:
294 if (tx_coalesce_usecs != rx_coalesce_usecs)
295 return -EINVAL;
297 intr = enic_legacy_io_intr();
298 vnic_intr_coalescing_timer_set(&enic->intr[intr],
299 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
300 break;
301 case VNIC_DEV_INTR_MODE_MSI:
302 if (tx_coalesce_usecs != rx_coalesce_usecs)
303 return -EINVAL;
305 vnic_intr_coalescing_timer_set(&enic->intr[0],
306 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
307 break;
308 case VNIC_DEV_INTR_MODE_MSIX:
309 for (i = 0; i < enic->wq_count; i++) {
310 intr = enic_msix_wq_intr(enic, i);
311 vnic_intr_coalescing_timer_set(&enic->intr[intr],
312 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
315 for (i = 0; i < enic->rq_count; i++) {
316 intr = enic_msix_rq_intr(enic, i);
317 vnic_intr_coalescing_timer_set(&enic->intr[intr],
318 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
321 break;
322 default:
323 break;
326 enic->tx_coalesce_usecs = tx_coalesce_usecs;
327 enic->rx_coalesce_usecs = rx_coalesce_usecs;
329 return 0;
332 static const struct ethtool_ops enic_ethtool_ops = {
333 .get_settings = enic_get_settings,
334 .get_drvinfo = enic_get_drvinfo,
335 .get_msglevel = enic_get_msglevel,
336 .set_msglevel = enic_set_msglevel,
337 .get_link = ethtool_op_get_link,
338 .get_strings = enic_get_strings,
339 .get_sset_count = enic_get_sset_count,
340 .get_ethtool_stats = enic_get_ethtool_stats,
341 .get_coalesce = enic_get_coalesce,
342 .set_coalesce = enic_set_coalesce,
345 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
347 struct enic *enic = vnic_dev_priv(wq->vdev);
349 if (buf->sop)
350 pci_unmap_single(enic->pdev, buf->dma_addr,
351 buf->len, PCI_DMA_TODEVICE);
352 else
353 pci_unmap_page(enic->pdev, buf->dma_addr,
354 buf->len, PCI_DMA_TODEVICE);
356 if (buf->os_buf)
357 dev_kfree_skb_any(buf->os_buf);
360 static void enic_wq_free_buf(struct vnic_wq *wq,
361 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
363 enic_free_wq_buf(wq, buf);
366 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
367 u8 type, u16 q_number, u16 completed_index, void *opaque)
369 struct enic *enic = vnic_dev_priv(vdev);
371 spin_lock(&enic->wq_lock[q_number]);
373 vnic_wq_service(&enic->wq[q_number], cq_desc,
374 completed_index, enic_wq_free_buf,
375 opaque);
377 if (netif_queue_stopped(enic->netdev) &&
378 vnic_wq_desc_avail(&enic->wq[q_number]) >=
379 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
380 netif_wake_queue(enic->netdev);
382 spin_unlock(&enic->wq_lock[q_number]);
384 return 0;
387 static void enic_log_q_error(struct enic *enic)
389 unsigned int i;
390 u32 error_status;
392 for (i = 0; i < enic->wq_count; i++) {
393 error_status = vnic_wq_error_status(&enic->wq[i]);
394 if (error_status)
395 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
396 i, error_status);
399 for (i = 0; i < enic->rq_count; i++) {
400 error_status = vnic_rq_error_status(&enic->rq[i]);
401 if (error_status)
402 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
403 i, error_status);
407 static void enic_msglvl_check(struct enic *enic)
409 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
411 if (msg_enable != enic->msg_enable) {
412 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
413 enic->msg_enable, msg_enable);
414 enic->msg_enable = msg_enable;
418 static void enic_mtu_check(struct enic *enic)
420 u32 mtu = vnic_dev_mtu(enic->vdev);
421 struct net_device *netdev = enic->netdev;
423 if (mtu && mtu != enic->port_mtu) {
424 enic->port_mtu = mtu;
425 if (mtu < netdev->mtu)
426 netdev_warn(netdev,
427 "interface MTU (%d) set higher "
428 "than switch port MTU (%d)\n",
429 netdev->mtu, mtu);
433 static void enic_link_check(struct enic *enic)
435 int link_status = vnic_dev_link_status(enic->vdev);
436 int carrier_ok = netif_carrier_ok(enic->netdev);
438 if (link_status && !carrier_ok) {
439 netdev_info(enic->netdev, "Link UP\n");
440 netif_carrier_on(enic->netdev);
441 } else if (!link_status && carrier_ok) {
442 netdev_info(enic->netdev, "Link DOWN\n");
443 netif_carrier_off(enic->netdev);
447 static void enic_notify_check(struct enic *enic)
449 enic_msglvl_check(enic);
450 enic_mtu_check(enic);
451 enic_link_check(enic);
454 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
456 static irqreturn_t enic_isr_legacy(int irq, void *data)
458 struct net_device *netdev = data;
459 struct enic *enic = netdev_priv(netdev);
460 unsigned int io_intr = enic_legacy_io_intr();
461 unsigned int err_intr = enic_legacy_err_intr();
462 unsigned int notify_intr = enic_legacy_notify_intr();
463 u32 pba;
465 vnic_intr_mask(&enic->intr[io_intr]);
467 pba = vnic_intr_legacy_pba(enic->legacy_pba);
468 if (!pba) {
469 vnic_intr_unmask(&enic->intr[io_intr]);
470 return IRQ_NONE; /* not our interrupt */
473 if (ENIC_TEST_INTR(pba, notify_intr)) {
474 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
475 enic_notify_check(enic);
478 if (ENIC_TEST_INTR(pba, err_intr)) {
479 vnic_intr_return_all_credits(&enic->intr[err_intr]);
480 enic_log_q_error(enic);
481 /* schedule recovery from WQ/RQ error */
482 schedule_work(&enic->reset);
483 return IRQ_HANDLED;
486 if (ENIC_TEST_INTR(pba, io_intr)) {
487 if (napi_schedule_prep(&enic->napi[0]))
488 __napi_schedule(&enic->napi[0]);
489 } else {
490 vnic_intr_unmask(&enic->intr[io_intr]);
493 return IRQ_HANDLED;
496 static irqreturn_t enic_isr_msi(int irq, void *data)
498 struct enic *enic = data;
500 /* With MSI, there is no sharing of interrupts, so this is
501 * our interrupt and there is no need to ack it. The device
502 * is not providing per-vector masking, so the OS will not
503 * write to PCI config space to mask/unmask the interrupt.
504 * We're using mask_on_assertion for MSI, so the device
505 * automatically masks the interrupt when the interrupt is
506 * generated. Later, when exiting polling, the interrupt
507 * will be unmasked (see enic_poll).
509 * Also, the device uses the same PCIe Traffic Class (TC)
510 * for Memory Write data and MSI, so there are no ordering
511 * issues; the MSI will always arrive at the Root Complex
512 * _after_ corresponding Memory Writes (i.e. descriptor
513 * writes).
516 napi_schedule(&enic->napi[0]);
518 return IRQ_HANDLED;
521 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
523 struct napi_struct *napi = data;
525 /* schedule NAPI polling for RQ cleanup */
526 napi_schedule(napi);
528 return IRQ_HANDLED;
531 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
533 struct enic *enic = data;
534 unsigned int cq = enic_cq_wq(enic, 0);
535 unsigned int intr = enic_msix_wq_intr(enic, 0);
536 unsigned int wq_work_to_do = -1; /* no limit */
537 unsigned int wq_work_done;
539 wq_work_done = vnic_cq_service(&enic->cq[cq],
540 wq_work_to_do, enic_wq_service, NULL);
542 vnic_intr_return_credits(&enic->intr[intr],
543 wq_work_done,
544 1 /* unmask intr */,
545 1 /* reset intr timer */);
547 return IRQ_HANDLED;
550 static irqreturn_t enic_isr_msix_err(int irq, void *data)
552 struct enic *enic = data;
553 unsigned int intr = enic_msix_err_intr(enic);
555 vnic_intr_return_all_credits(&enic->intr[intr]);
557 enic_log_q_error(enic);
559 /* schedule recovery from WQ/RQ error */
560 schedule_work(&enic->reset);
562 return IRQ_HANDLED;
565 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
567 struct enic *enic = data;
568 unsigned int intr = enic_msix_notify_intr(enic);
570 vnic_intr_return_all_credits(&enic->intr[intr]);
571 enic_notify_check(enic);
573 return IRQ_HANDLED;
576 static inline void enic_queue_wq_skb_cont(struct enic *enic,
577 struct vnic_wq *wq, struct sk_buff *skb,
578 unsigned int len_left, int loopback)
580 skb_frag_t *frag;
582 /* Queue additional data fragments */
583 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
584 len_left -= frag->size;
585 enic_queue_wq_desc_cont(wq, skb,
586 pci_map_page(enic->pdev, frag->page,
587 frag->page_offset, frag->size,
588 PCI_DMA_TODEVICE),
589 frag->size,
590 (len_left == 0), /* EOP? */
591 loopback);
595 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
596 struct vnic_wq *wq, struct sk_buff *skb,
597 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
599 unsigned int head_len = skb_headlen(skb);
600 unsigned int len_left = skb->len - head_len;
601 int eop = (len_left == 0);
603 /* Queue the main skb fragment. The fragments are no larger
604 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
605 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
606 * per fragment is queued.
608 enic_queue_wq_desc(wq, skb,
609 pci_map_single(enic->pdev, skb->data,
610 head_len, PCI_DMA_TODEVICE),
611 head_len,
612 vlan_tag_insert, vlan_tag,
613 eop, loopback);
615 if (!eop)
616 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
619 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
620 struct vnic_wq *wq, struct sk_buff *skb,
621 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
623 unsigned int head_len = skb_headlen(skb);
624 unsigned int len_left = skb->len - head_len;
625 unsigned int hdr_len = skb_checksum_start_offset(skb);
626 unsigned int csum_offset = hdr_len + skb->csum_offset;
627 int eop = (len_left == 0);
629 /* Queue the main skb fragment. The fragments are no larger
630 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
631 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
632 * per fragment is queued.
634 enic_queue_wq_desc_csum_l4(wq, skb,
635 pci_map_single(enic->pdev, skb->data,
636 head_len, PCI_DMA_TODEVICE),
637 head_len,
638 csum_offset,
639 hdr_len,
640 vlan_tag_insert, vlan_tag,
641 eop, loopback);
643 if (!eop)
644 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
647 static inline void enic_queue_wq_skb_tso(struct enic *enic,
648 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
649 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
651 unsigned int frag_len_left = skb_headlen(skb);
652 unsigned int len_left = skb->len - frag_len_left;
653 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
654 int eop = (len_left == 0);
655 unsigned int len;
656 dma_addr_t dma_addr;
657 unsigned int offset = 0;
658 skb_frag_t *frag;
660 /* Preload TCP csum field with IP pseudo hdr calculated
661 * with IP length set to zero. HW will later add in length
662 * to each TCP segment resulting from the TSO.
665 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
666 ip_hdr(skb)->check = 0;
667 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
668 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
669 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
670 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
671 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
674 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
675 * for the main skb fragment
677 while (frag_len_left) {
678 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
679 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
680 len, PCI_DMA_TODEVICE);
681 enic_queue_wq_desc_tso(wq, skb,
682 dma_addr,
683 len,
684 mss, hdr_len,
685 vlan_tag_insert, vlan_tag,
686 eop && (len == frag_len_left), loopback);
687 frag_len_left -= len;
688 offset += len;
691 if (eop)
692 return;
694 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
695 * for additional data fragments
697 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
698 len_left -= frag->size;
699 frag_len_left = frag->size;
700 offset = frag->page_offset;
702 while (frag_len_left) {
703 len = min(frag_len_left,
704 (unsigned int)WQ_ENET_MAX_DESC_LEN);
705 dma_addr = pci_map_page(enic->pdev, frag->page,
706 offset, len,
707 PCI_DMA_TODEVICE);
708 enic_queue_wq_desc_cont(wq, skb,
709 dma_addr,
710 len,
711 (len_left == 0) &&
712 (len == frag_len_left), /* EOP? */
713 loopback);
714 frag_len_left -= len;
715 offset += len;
720 static inline void enic_queue_wq_skb(struct enic *enic,
721 struct vnic_wq *wq, struct sk_buff *skb)
723 unsigned int mss = skb_shinfo(skb)->gso_size;
724 unsigned int vlan_tag = 0;
725 int vlan_tag_insert = 0;
726 int loopback = 0;
728 if (vlan_tx_tag_present(skb)) {
729 /* VLAN tag from trunking driver */
730 vlan_tag_insert = 1;
731 vlan_tag = vlan_tx_tag_get(skb);
732 } else if (enic->loop_enable) {
733 vlan_tag = enic->loop_tag;
734 loopback = 1;
737 if (mss)
738 enic_queue_wq_skb_tso(enic, wq, skb, mss,
739 vlan_tag_insert, vlan_tag, loopback);
740 else if (skb->ip_summed == CHECKSUM_PARTIAL)
741 enic_queue_wq_skb_csum_l4(enic, wq, skb,
742 vlan_tag_insert, vlan_tag, loopback);
743 else
744 enic_queue_wq_skb_vlan(enic, wq, skb,
745 vlan_tag_insert, vlan_tag, loopback);
748 /* netif_tx_lock held, process context with BHs disabled, or BH */
749 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
750 struct net_device *netdev)
752 struct enic *enic = netdev_priv(netdev);
753 struct vnic_wq *wq = &enic->wq[0];
754 unsigned long flags;
756 if (skb->len <= 0) {
757 dev_kfree_skb(skb);
758 return NETDEV_TX_OK;
761 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
762 * which is very likely. In the off chance it's going to take
763 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
766 if (skb_shinfo(skb)->gso_size == 0 &&
767 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
768 skb_linearize(skb)) {
769 dev_kfree_skb(skb);
770 return NETDEV_TX_OK;
773 spin_lock_irqsave(&enic->wq_lock[0], flags);
775 if (vnic_wq_desc_avail(wq) <
776 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
777 netif_stop_queue(netdev);
778 /* This is a hard error, log it */
779 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
780 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
781 return NETDEV_TX_BUSY;
784 enic_queue_wq_skb(enic, wq, skb);
786 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
787 netif_stop_queue(netdev);
789 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
791 return NETDEV_TX_OK;
794 /* dev_base_lock rwlock held, nominally process context */
795 static struct net_device_stats *enic_get_stats(struct net_device *netdev)
797 struct enic *enic = netdev_priv(netdev);
798 struct net_device_stats *net_stats = &netdev->stats;
799 struct vnic_stats *stats;
801 enic_dev_stats_dump(enic, &stats);
803 net_stats->tx_packets = stats->tx.tx_frames_ok;
804 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
805 net_stats->tx_errors = stats->tx.tx_errors;
806 net_stats->tx_dropped = stats->tx.tx_drops;
808 net_stats->rx_packets = stats->rx.rx_frames_ok;
809 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
810 net_stats->rx_errors = stats->rx.rx_errors;
811 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
812 net_stats->rx_over_errors = enic->rq_truncated_pkts;
813 net_stats->rx_crc_errors = enic->rq_bad_fcs;
814 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
816 return net_stats;
819 void enic_reset_addr_lists(struct enic *enic)
821 enic->mc_count = 0;
822 enic->uc_count = 0;
823 enic->flags = 0;
826 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
828 struct enic *enic = netdev_priv(netdev);
830 if (enic_is_dynamic(enic)) {
831 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
832 return -EADDRNOTAVAIL;
833 } else {
834 if (!is_valid_ether_addr(addr))
835 return -EADDRNOTAVAIL;
838 memcpy(netdev->dev_addr, addr, netdev->addr_len);
840 return 0;
843 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
845 struct enic *enic = netdev_priv(netdev);
846 struct sockaddr *saddr = p;
847 char *addr = saddr->sa_data;
848 int err;
850 if (netif_running(enic->netdev)) {
851 err = enic_dev_del_station_addr(enic);
852 if (err)
853 return err;
856 err = enic_set_mac_addr(netdev, addr);
857 if (err)
858 return err;
860 if (netif_running(enic->netdev)) {
861 err = enic_dev_add_station_addr(enic);
862 if (err)
863 return err;
866 return err;
869 static int enic_set_mac_address(struct net_device *netdev, void *p)
871 struct sockaddr *saddr = p;
872 char *addr = saddr->sa_data;
873 struct enic *enic = netdev_priv(netdev);
874 int err;
876 err = enic_dev_del_station_addr(enic);
877 if (err)
878 return err;
880 err = enic_set_mac_addr(netdev, addr);
881 if (err)
882 return err;
884 return enic_dev_add_station_addr(enic);
887 static void enic_update_multicast_addr_list(struct enic *enic)
889 struct net_device *netdev = enic->netdev;
890 struct netdev_hw_addr *ha;
891 unsigned int mc_count = netdev_mc_count(netdev);
892 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
893 unsigned int i, j;
895 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
896 netdev_warn(netdev, "Registering only %d out of %d "
897 "multicast addresses\n",
898 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
899 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
902 /* Is there an easier way? Trying to minimize to
903 * calls to add/del multicast addrs. We keep the
904 * addrs from the last call in enic->mc_addr and
905 * look for changes to add/del.
908 i = 0;
909 netdev_for_each_mc_addr(ha, netdev) {
910 if (i == mc_count)
911 break;
912 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
915 for (i = 0; i < enic->mc_count; i++) {
916 for (j = 0; j < mc_count; j++)
917 if (compare_ether_addr(enic->mc_addr[i],
918 mc_addr[j]) == 0)
919 break;
920 if (j == mc_count)
921 enic_dev_del_addr(enic, enic->mc_addr[i]);
924 for (i = 0; i < mc_count; i++) {
925 for (j = 0; j < enic->mc_count; j++)
926 if (compare_ether_addr(mc_addr[i],
927 enic->mc_addr[j]) == 0)
928 break;
929 if (j == enic->mc_count)
930 enic_dev_add_addr(enic, mc_addr[i]);
933 /* Save the list to compare against next time
936 for (i = 0; i < mc_count; i++)
937 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
939 enic->mc_count = mc_count;
942 static void enic_update_unicast_addr_list(struct enic *enic)
944 struct net_device *netdev = enic->netdev;
945 struct netdev_hw_addr *ha;
946 unsigned int uc_count = netdev_uc_count(netdev);
947 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
948 unsigned int i, j;
950 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
951 netdev_warn(netdev, "Registering only %d out of %d "
952 "unicast addresses\n",
953 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
954 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
957 /* Is there an easier way? Trying to minimize to
958 * calls to add/del unicast addrs. We keep the
959 * addrs from the last call in enic->uc_addr and
960 * look for changes to add/del.
963 i = 0;
964 netdev_for_each_uc_addr(ha, netdev) {
965 if (i == uc_count)
966 break;
967 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
970 for (i = 0; i < enic->uc_count; i++) {
971 for (j = 0; j < uc_count; j++)
972 if (compare_ether_addr(enic->uc_addr[i],
973 uc_addr[j]) == 0)
974 break;
975 if (j == uc_count)
976 enic_dev_del_addr(enic, enic->uc_addr[i]);
979 for (i = 0; i < uc_count; i++) {
980 for (j = 0; j < enic->uc_count; j++)
981 if (compare_ether_addr(uc_addr[i],
982 enic->uc_addr[j]) == 0)
983 break;
984 if (j == enic->uc_count)
985 enic_dev_add_addr(enic, uc_addr[i]);
988 /* Save the list to compare against next time
991 for (i = 0; i < uc_count; i++)
992 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
994 enic->uc_count = uc_count;
997 /* netif_tx_lock held, BHs disabled */
998 static void enic_set_rx_mode(struct net_device *netdev)
1000 struct enic *enic = netdev_priv(netdev);
1001 int directed = 1;
1002 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1003 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1004 int promisc = (netdev->flags & IFF_PROMISC) ||
1005 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1006 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1007 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1008 unsigned int flags = netdev->flags |
1009 (allmulti ? IFF_ALLMULTI : 0) |
1010 (promisc ? IFF_PROMISC : 0);
1012 if (enic->flags != flags) {
1013 enic->flags = flags;
1014 enic_dev_packet_filter(enic, directed,
1015 multicast, broadcast, promisc, allmulti);
1018 if (!promisc) {
1019 enic_update_unicast_addr_list(enic);
1020 if (!allmulti)
1021 enic_update_multicast_addr_list(enic);
1025 /* rtnl lock is held */
1026 static void enic_vlan_rx_register(struct net_device *netdev,
1027 struct vlan_group *vlan_group)
1029 struct enic *enic = netdev_priv(netdev);
1030 enic->vlan_group = vlan_group;
1033 /* netif_tx_lock held, BHs disabled */
1034 static void enic_tx_timeout(struct net_device *netdev)
1036 struct enic *enic = netdev_priv(netdev);
1037 schedule_work(&enic->reset);
1040 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1042 struct enic *enic = netdev_priv(netdev);
1044 if (vf != PORT_SELF_VF)
1045 return -EOPNOTSUPP;
1047 /* Ignore the vf argument for now. We can assume the request
1048 * is coming on a vf.
1050 if (is_valid_ether_addr(mac)) {
1051 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1052 return 0;
1053 } else
1054 return -EINVAL;
1057 static int enic_set_vf_port(struct net_device *netdev, int vf,
1058 struct nlattr *port[])
1060 struct enic *enic = netdev_priv(netdev);
1061 struct enic_port_profile prev_pp;
1062 int err = 0, restore_pp = 1;
1064 /* don't support VFs, yet */
1065 if (vf != PORT_SELF_VF)
1066 return -EOPNOTSUPP;
1068 if (!port[IFLA_PORT_REQUEST])
1069 return -EOPNOTSUPP;
1071 memcpy(&prev_pp, &enic->pp, sizeof(enic->pp));
1072 memset(&enic->pp, 0, sizeof(enic->pp));
1074 enic->pp.set |= ENIC_SET_REQUEST;
1075 enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1077 if (port[IFLA_PORT_PROFILE]) {
1078 enic->pp.set |= ENIC_SET_NAME;
1079 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
1080 PORT_PROFILE_MAX);
1083 if (port[IFLA_PORT_INSTANCE_UUID]) {
1084 enic->pp.set |= ENIC_SET_INSTANCE;
1085 memcpy(enic->pp.instance_uuid,
1086 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1089 if (port[IFLA_PORT_HOST_UUID]) {
1090 enic->pp.set |= ENIC_SET_HOST;
1091 memcpy(enic->pp.host_uuid,
1092 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1095 /* Special case handling: mac came from IFLA_VF_MAC */
1096 if (!is_zero_ether_addr(prev_pp.vf_mac))
1097 memcpy(enic->pp.mac_addr, prev_pp.vf_mac, ETH_ALEN);
1099 if (is_zero_ether_addr(netdev->dev_addr))
1100 random_ether_addr(netdev->dev_addr);
1102 err = enic_process_set_pp_request(enic, &prev_pp, &restore_pp);
1103 if (err) {
1104 if (restore_pp) {
1105 /* Things are still the way they were: Implicit
1106 * DISASSOCIATE failed
1108 memcpy(&enic->pp, &prev_pp, sizeof(enic->pp));
1109 } else {
1110 memset(&enic->pp, 0, sizeof(enic->pp));
1111 memset(netdev->dev_addr, 0, ETH_ALEN);
1113 } else {
1114 /* Set flag to indicate that the port assoc/disassoc
1115 * request has been sent out to fw
1117 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1119 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1120 if (enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1121 memset(enic->pp.mac_addr, 0, ETH_ALEN);
1122 memset(netdev->dev_addr, 0, ETH_ALEN);
1126 memset(enic->pp.vf_mac, 0, ETH_ALEN);
1128 return err;
1131 static int enic_get_vf_port(struct net_device *netdev, int vf,
1132 struct sk_buff *skb)
1134 struct enic *enic = netdev_priv(netdev);
1135 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1136 int err;
1138 if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
1139 return -ENODATA;
1141 err = enic_process_get_pp_request(enic, enic->pp.request, &response);
1142 if (err)
1143 return err;
1145 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1146 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
1147 if (enic->pp.set & ENIC_SET_NAME)
1148 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1149 enic->pp.name);
1150 if (enic->pp.set & ENIC_SET_INSTANCE)
1151 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1152 enic->pp.instance_uuid);
1153 if (enic->pp.set & ENIC_SET_HOST)
1154 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1155 enic->pp.host_uuid);
1157 return 0;
1159 nla_put_failure:
1160 return -EMSGSIZE;
1163 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1165 struct enic *enic = vnic_dev_priv(rq->vdev);
1167 if (!buf->os_buf)
1168 return;
1170 pci_unmap_single(enic->pdev, buf->dma_addr,
1171 buf->len, PCI_DMA_FROMDEVICE);
1172 dev_kfree_skb_any(buf->os_buf);
1175 static int enic_rq_alloc_buf(struct vnic_rq *rq)
1177 struct enic *enic = vnic_dev_priv(rq->vdev);
1178 struct net_device *netdev = enic->netdev;
1179 struct sk_buff *skb;
1180 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
1181 unsigned int os_buf_index = 0;
1182 dma_addr_t dma_addr;
1184 skb = netdev_alloc_skb_ip_align(netdev, len);
1185 if (!skb)
1186 return -ENOMEM;
1188 dma_addr = pci_map_single(enic->pdev, skb->data,
1189 len, PCI_DMA_FROMDEVICE);
1191 enic_queue_rq_desc(rq, skb, os_buf_index,
1192 dma_addr, len);
1194 return 0;
1197 static void enic_rq_indicate_buf(struct vnic_rq *rq,
1198 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1199 int skipped, void *opaque)
1201 struct enic *enic = vnic_dev_priv(rq->vdev);
1202 struct net_device *netdev = enic->netdev;
1203 struct sk_buff *skb;
1205 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1206 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1207 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1208 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1209 u8 packet_error;
1210 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1211 u32 rss_hash;
1213 if (skipped)
1214 return;
1216 skb = buf->os_buf;
1217 prefetch(skb->data - NET_IP_ALIGN);
1218 pci_unmap_single(enic->pdev, buf->dma_addr,
1219 buf->len, PCI_DMA_FROMDEVICE);
1221 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1222 &type, &color, &q_number, &completed_index,
1223 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1224 &csum_not_calc, &rss_hash, &bytes_written,
1225 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
1226 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1227 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1228 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1229 &fcs_ok);
1231 if (packet_error) {
1233 if (!fcs_ok) {
1234 if (bytes_written > 0)
1235 enic->rq_bad_fcs++;
1236 else if (bytes_written == 0)
1237 enic->rq_truncated_pkts++;
1240 dev_kfree_skb_any(skb);
1242 return;
1245 if (eop && bytes_written > 0) {
1247 /* Good receive
1250 skb_put(skb, bytes_written);
1251 skb->protocol = eth_type_trans(skb, netdev);
1253 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
1254 skb->csum = htons(checksum);
1255 skb->ip_summed = CHECKSUM_COMPLETE;
1258 skb->dev = netdev;
1260 if (enic->vlan_group && vlan_stripped &&
1261 (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
1263 if (netdev->features & NETIF_F_GRO)
1264 vlan_gro_receive(&enic->napi[q_number],
1265 enic->vlan_group, vlan_tci, skb);
1266 else
1267 vlan_hwaccel_receive_skb(skb,
1268 enic->vlan_group, vlan_tci);
1270 } else {
1272 if (netdev->features & NETIF_F_GRO)
1273 napi_gro_receive(&enic->napi[q_number], skb);
1274 else
1275 netif_receive_skb(skb);
1278 } else {
1280 /* Buffer overflow
1283 dev_kfree_skb_any(skb);
1287 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1288 u8 type, u16 q_number, u16 completed_index, void *opaque)
1290 struct enic *enic = vnic_dev_priv(vdev);
1292 vnic_rq_service(&enic->rq[q_number], cq_desc,
1293 completed_index, VNIC_RQ_RETURN_DESC,
1294 enic_rq_indicate_buf, opaque);
1296 return 0;
1299 static int enic_poll(struct napi_struct *napi, int budget)
1301 struct net_device *netdev = napi->dev;
1302 struct enic *enic = netdev_priv(netdev);
1303 unsigned int cq_rq = enic_cq_rq(enic, 0);
1304 unsigned int cq_wq = enic_cq_wq(enic, 0);
1305 unsigned int intr = enic_legacy_io_intr();
1306 unsigned int rq_work_to_do = budget;
1307 unsigned int wq_work_to_do = -1; /* no limit */
1308 unsigned int work_done, rq_work_done, wq_work_done;
1309 int err;
1311 /* Service RQ (first) and WQ
1314 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1315 rq_work_to_do, enic_rq_service, NULL);
1317 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
1318 wq_work_to_do, enic_wq_service, NULL);
1320 /* Accumulate intr event credits for this polling
1321 * cycle. An intr event is the completion of a
1322 * a WQ or RQ packet.
1325 work_done = rq_work_done + wq_work_done;
1327 if (work_done > 0)
1328 vnic_intr_return_credits(&enic->intr[intr],
1329 work_done,
1330 0 /* don't unmask intr */,
1331 0 /* don't reset intr timer */);
1333 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1335 /* Buffer allocation failed. Stay in polling
1336 * mode so we can try to fill the ring again.
1339 if (err)
1340 rq_work_done = rq_work_to_do;
1342 if (rq_work_done < rq_work_to_do) {
1344 /* Some work done, but not enough to stay in polling,
1345 * exit polling
1348 napi_complete(napi);
1349 vnic_intr_unmask(&enic->intr[intr]);
1352 return rq_work_done;
1355 static int enic_poll_msix(struct napi_struct *napi, int budget)
1357 struct net_device *netdev = napi->dev;
1358 struct enic *enic = netdev_priv(netdev);
1359 unsigned int rq = (napi - &enic->napi[0]);
1360 unsigned int cq = enic_cq_rq(enic, rq);
1361 unsigned int intr = enic_msix_rq_intr(enic, rq);
1362 unsigned int work_to_do = budget;
1363 unsigned int work_done;
1364 int err;
1366 /* Service RQ
1369 work_done = vnic_cq_service(&enic->cq[cq],
1370 work_to_do, enic_rq_service, NULL);
1372 /* Return intr event credits for this polling
1373 * cycle. An intr event is the completion of a
1374 * RQ packet.
1377 if (work_done > 0)
1378 vnic_intr_return_credits(&enic->intr[intr],
1379 work_done,
1380 0 /* don't unmask intr */,
1381 0 /* don't reset intr timer */);
1383 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1385 /* Buffer allocation failed. Stay in polling mode
1386 * so we can try to fill the ring again.
1389 if (err)
1390 work_done = work_to_do;
1392 if (work_done < work_to_do) {
1394 /* Some work done, but not enough to stay in polling,
1395 * exit polling
1398 napi_complete(napi);
1399 vnic_intr_unmask(&enic->intr[intr]);
1402 return work_done;
1405 static void enic_notify_timer(unsigned long data)
1407 struct enic *enic = (struct enic *)data;
1409 enic_notify_check(enic);
1411 mod_timer(&enic->notify_timer,
1412 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1415 static void enic_free_intr(struct enic *enic)
1417 struct net_device *netdev = enic->netdev;
1418 unsigned int i;
1420 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1421 case VNIC_DEV_INTR_MODE_INTX:
1422 free_irq(enic->pdev->irq, netdev);
1423 break;
1424 case VNIC_DEV_INTR_MODE_MSI:
1425 free_irq(enic->pdev->irq, enic);
1426 break;
1427 case VNIC_DEV_INTR_MODE_MSIX:
1428 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1429 if (enic->msix[i].requested)
1430 free_irq(enic->msix_entry[i].vector,
1431 enic->msix[i].devid);
1432 break;
1433 default:
1434 break;
1438 static int enic_request_intr(struct enic *enic)
1440 struct net_device *netdev = enic->netdev;
1441 unsigned int i, intr;
1442 int err = 0;
1444 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1446 case VNIC_DEV_INTR_MODE_INTX:
1448 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1449 IRQF_SHARED, netdev->name, netdev);
1450 break;
1452 case VNIC_DEV_INTR_MODE_MSI:
1454 err = request_irq(enic->pdev->irq, enic_isr_msi,
1455 0, netdev->name, enic);
1456 break;
1458 case VNIC_DEV_INTR_MODE_MSIX:
1460 for (i = 0; i < enic->rq_count; i++) {
1461 intr = enic_msix_rq_intr(enic, i);
1462 sprintf(enic->msix[intr].devname,
1463 "%.11s-rx-%d", netdev->name, i);
1464 enic->msix[intr].isr = enic_isr_msix_rq;
1465 enic->msix[intr].devid = &enic->napi[i];
1468 for (i = 0; i < enic->wq_count; i++) {
1469 intr = enic_msix_wq_intr(enic, i);
1470 sprintf(enic->msix[intr].devname,
1471 "%.11s-tx-%d", netdev->name, i);
1472 enic->msix[intr].isr = enic_isr_msix_wq;
1473 enic->msix[intr].devid = enic;
1476 intr = enic_msix_err_intr(enic);
1477 sprintf(enic->msix[intr].devname,
1478 "%.11s-err", netdev->name);
1479 enic->msix[intr].isr = enic_isr_msix_err;
1480 enic->msix[intr].devid = enic;
1482 intr = enic_msix_notify_intr(enic);
1483 sprintf(enic->msix[intr].devname,
1484 "%.11s-notify", netdev->name);
1485 enic->msix[intr].isr = enic_isr_msix_notify;
1486 enic->msix[intr].devid = enic;
1488 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1489 enic->msix[i].requested = 0;
1491 for (i = 0; i < enic->intr_count; i++) {
1492 err = request_irq(enic->msix_entry[i].vector,
1493 enic->msix[i].isr, 0,
1494 enic->msix[i].devname,
1495 enic->msix[i].devid);
1496 if (err) {
1497 enic_free_intr(enic);
1498 break;
1500 enic->msix[i].requested = 1;
1503 break;
1505 default:
1506 break;
1509 return err;
1512 static void enic_synchronize_irqs(struct enic *enic)
1514 unsigned int i;
1516 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1517 case VNIC_DEV_INTR_MODE_INTX:
1518 case VNIC_DEV_INTR_MODE_MSI:
1519 synchronize_irq(enic->pdev->irq);
1520 break;
1521 case VNIC_DEV_INTR_MODE_MSIX:
1522 for (i = 0; i < enic->intr_count; i++)
1523 synchronize_irq(enic->msix_entry[i].vector);
1524 break;
1525 default:
1526 break;
1530 static int enic_dev_notify_set(struct enic *enic)
1532 int err;
1534 spin_lock(&enic->devcmd_lock);
1535 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1536 case VNIC_DEV_INTR_MODE_INTX:
1537 err = vnic_dev_notify_set(enic->vdev,
1538 enic_legacy_notify_intr());
1539 break;
1540 case VNIC_DEV_INTR_MODE_MSIX:
1541 err = vnic_dev_notify_set(enic->vdev,
1542 enic_msix_notify_intr(enic));
1543 break;
1544 default:
1545 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1546 break;
1548 spin_unlock(&enic->devcmd_lock);
1550 return err;
1553 static void enic_notify_timer_start(struct enic *enic)
1555 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1556 case VNIC_DEV_INTR_MODE_MSI:
1557 mod_timer(&enic->notify_timer, jiffies);
1558 break;
1559 default:
1560 /* Using intr for notification for INTx/MSI-X */
1561 break;
1565 /* rtnl lock is held, process context */
1566 static int enic_open(struct net_device *netdev)
1568 struct enic *enic = netdev_priv(netdev);
1569 unsigned int i;
1570 int err;
1572 err = enic_request_intr(enic);
1573 if (err) {
1574 netdev_err(netdev, "Unable to request irq.\n");
1575 return err;
1578 err = enic_dev_notify_set(enic);
1579 if (err) {
1580 netdev_err(netdev,
1581 "Failed to alloc notify buffer, aborting.\n");
1582 goto err_out_free_intr;
1585 for (i = 0; i < enic->rq_count; i++) {
1586 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1587 /* Need at least one buffer on ring to get going */
1588 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1589 netdev_err(netdev, "Unable to alloc receive buffers\n");
1590 err = -ENOMEM;
1591 goto err_out_notify_unset;
1595 for (i = 0; i < enic->wq_count; i++)
1596 vnic_wq_enable(&enic->wq[i]);
1597 for (i = 0; i < enic->rq_count; i++)
1598 vnic_rq_enable(&enic->rq[i]);
1600 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1601 enic_dev_add_addr(enic, enic->pp.mac_addr);
1602 else
1603 enic_dev_add_station_addr(enic);
1604 enic_set_rx_mode(netdev);
1606 netif_wake_queue(netdev);
1608 for (i = 0; i < enic->rq_count; i++)
1609 napi_enable(&enic->napi[i]);
1611 enic_dev_enable(enic);
1613 for (i = 0; i < enic->intr_count; i++)
1614 vnic_intr_unmask(&enic->intr[i]);
1616 enic_notify_timer_start(enic);
1618 return 0;
1620 err_out_notify_unset:
1621 enic_dev_notify_unset(enic);
1622 err_out_free_intr:
1623 enic_free_intr(enic);
1625 return err;
1628 /* rtnl lock is held, process context */
1629 static int enic_stop(struct net_device *netdev)
1631 struct enic *enic = netdev_priv(netdev);
1632 unsigned int i;
1633 int err;
1635 for (i = 0; i < enic->intr_count; i++) {
1636 vnic_intr_mask(&enic->intr[i]);
1637 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1640 enic_synchronize_irqs(enic);
1642 del_timer_sync(&enic->notify_timer);
1644 enic_dev_disable(enic);
1646 for (i = 0; i < enic->rq_count; i++)
1647 napi_disable(&enic->napi[i]);
1649 netif_carrier_off(netdev);
1650 netif_tx_disable(netdev);
1651 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1652 enic_dev_del_addr(enic, enic->pp.mac_addr);
1653 else
1654 enic_dev_del_station_addr(enic);
1656 for (i = 0; i < enic->wq_count; i++) {
1657 err = vnic_wq_disable(&enic->wq[i]);
1658 if (err)
1659 return err;
1661 for (i = 0; i < enic->rq_count; i++) {
1662 err = vnic_rq_disable(&enic->rq[i]);
1663 if (err)
1664 return err;
1667 enic_dev_notify_unset(enic);
1668 enic_free_intr(enic);
1670 for (i = 0; i < enic->wq_count; i++)
1671 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1672 for (i = 0; i < enic->rq_count; i++)
1673 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1674 for (i = 0; i < enic->cq_count; i++)
1675 vnic_cq_clean(&enic->cq[i]);
1676 for (i = 0; i < enic->intr_count; i++)
1677 vnic_intr_clean(&enic->intr[i]);
1679 return 0;
1682 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1684 struct enic *enic = netdev_priv(netdev);
1685 int running = netif_running(netdev);
1687 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1688 return -EINVAL;
1690 if (running)
1691 enic_stop(netdev);
1693 netdev->mtu = new_mtu;
1695 if (netdev->mtu > enic->port_mtu)
1696 netdev_warn(netdev,
1697 "interface MTU (%d) set higher than port MTU (%d)\n",
1698 netdev->mtu, enic->port_mtu);
1700 if (running)
1701 enic_open(netdev);
1703 return 0;
1706 #ifdef CONFIG_NET_POLL_CONTROLLER
1707 static void enic_poll_controller(struct net_device *netdev)
1709 struct enic *enic = netdev_priv(netdev);
1710 struct vnic_dev *vdev = enic->vdev;
1711 unsigned int i, intr;
1713 switch (vnic_dev_get_intr_mode(vdev)) {
1714 case VNIC_DEV_INTR_MODE_MSIX:
1715 for (i = 0; i < enic->rq_count; i++) {
1716 intr = enic_msix_rq_intr(enic, i);
1717 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1718 &enic->napi[i]);
1720 intr = enic_msix_wq_intr(enic, i);
1721 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1722 break;
1723 case VNIC_DEV_INTR_MODE_MSI:
1724 enic_isr_msi(enic->pdev->irq, enic);
1725 break;
1726 case VNIC_DEV_INTR_MODE_INTX:
1727 enic_isr_legacy(enic->pdev->irq, netdev);
1728 break;
1729 default:
1730 break;
1733 #endif
1735 static int enic_dev_wait(struct vnic_dev *vdev,
1736 int (*start)(struct vnic_dev *, int),
1737 int (*finished)(struct vnic_dev *, int *),
1738 int arg)
1740 unsigned long time;
1741 int done;
1742 int err;
1744 BUG_ON(in_interrupt());
1746 err = start(vdev, arg);
1747 if (err)
1748 return err;
1750 /* Wait for func to complete...2 seconds max
1753 time = jiffies + (HZ * 2);
1754 do {
1756 err = finished(vdev, &done);
1757 if (err)
1758 return err;
1760 if (done)
1761 return 0;
1763 schedule_timeout_uninterruptible(HZ / 10);
1765 } while (time_after(time, jiffies));
1767 return -ETIMEDOUT;
1770 static int enic_dev_open(struct enic *enic)
1772 int err;
1774 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1775 vnic_dev_open_done, 0);
1776 if (err)
1777 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1778 err);
1780 return err;
1783 static int enic_dev_hang_reset(struct enic *enic)
1785 int err;
1787 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1788 vnic_dev_hang_reset_done, 0);
1789 if (err)
1790 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1791 err);
1793 return err;
1796 static int enic_set_rsskey(struct enic *enic)
1798 dma_addr_t rss_key_buf_pa;
1799 union vnic_rss_key *rss_key_buf_va = NULL;
1800 union vnic_rss_key rss_key = {
1801 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1802 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1803 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1804 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1806 int err;
1808 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1809 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1810 if (!rss_key_buf_va)
1811 return -ENOMEM;
1813 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1815 spin_lock(&enic->devcmd_lock);
1816 err = enic_set_rss_key(enic,
1817 rss_key_buf_pa,
1818 sizeof(union vnic_rss_key));
1819 spin_unlock(&enic->devcmd_lock);
1821 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1822 rss_key_buf_va, rss_key_buf_pa);
1824 return err;
1827 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1829 dma_addr_t rss_cpu_buf_pa;
1830 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1831 unsigned int i;
1832 int err;
1834 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1835 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1836 if (!rss_cpu_buf_va)
1837 return -ENOMEM;
1839 for (i = 0; i < (1 << rss_hash_bits); i++)
1840 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1842 spin_lock(&enic->devcmd_lock);
1843 err = enic_set_rss_cpu(enic,
1844 rss_cpu_buf_pa,
1845 sizeof(union vnic_rss_cpu));
1846 spin_unlock(&enic->devcmd_lock);
1848 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1849 rss_cpu_buf_va, rss_cpu_buf_pa);
1851 return err;
1854 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1855 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1857 const u8 tso_ipid_split_en = 0;
1858 const u8 ig_vlan_strip_en = 1;
1859 int err;
1861 /* Enable VLAN tag stripping.
1864 spin_lock(&enic->devcmd_lock);
1865 err = enic_set_nic_cfg(enic,
1866 rss_default_cpu, rss_hash_type,
1867 rss_hash_bits, rss_base_cpu,
1868 rss_enable, tso_ipid_split_en,
1869 ig_vlan_strip_en);
1870 spin_unlock(&enic->devcmd_lock);
1872 return err;
1875 static int enic_set_rss_nic_cfg(struct enic *enic)
1877 struct device *dev = enic_get_dev(enic);
1878 const u8 rss_default_cpu = 0;
1879 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1880 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1881 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1882 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1883 const u8 rss_hash_bits = 7;
1884 const u8 rss_base_cpu = 0;
1885 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1887 if (rss_enable) {
1888 if (!enic_set_rsskey(enic)) {
1889 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1890 rss_enable = 0;
1891 dev_warn(dev, "RSS disabled, "
1892 "Failed to set RSS cpu indirection table.");
1894 } else {
1895 rss_enable = 0;
1896 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1900 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1901 rss_hash_bits, rss_base_cpu, rss_enable);
1904 static void enic_reset(struct work_struct *work)
1906 struct enic *enic = container_of(work, struct enic, reset);
1908 if (!netif_running(enic->netdev))
1909 return;
1911 rtnl_lock();
1913 enic_dev_hang_notify(enic);
1914 enic_stop(enic->netdev);
1915 enic_dev_hang_reset(enic);
1916 enic_reset_addr_lists(enic);
1917 enic_init_vnic_resources(enic);
1918 enic_set_rss_nic_cfg(enic);
1919 enic_dev_set_ig_vlan_rewrite_mode(enic);
1920 enic_open(enic->netdev);
1922 rtnl_unlock();
1925 static int enic_set_intr_mode(struct enic *enic)
1927 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
1928 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
1929 unsigned int i;
1931 /* Set interrupt mode (INTx, MSI, MSI-X) depending
1932 * on system capabilities.
1934 * Try MSI-X first
1936 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1937 * (the second to last INTR is used for WQ/RQ errors)
1938 * (the last INTR is used for notifications)
1941 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
1942 for (i = 0; i < n + m + 2; i++)
1943 enic->msix_entry[i].entry = i;
1945 /* Use multiple RQs if RSS is enabled
1948 if (ENIC_SETTING(enic, RSS) &&
1949 enic->config.intr_mode < 1 &&
1950 enic->rq_count >= n &&
1951 enic->wq_count >= m &&
1952 enic->cq_count >= n + m &&
1953 enic->intr_count >= n + m + 2) {
1955 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
1957 enic->rq_count = n;
1958 enic->wq_count = m;
1959 enic->cq_count = n + m;
1960 enic->intr_count = n + m + 2;
1962 vnic_dev_set_intr_mode(enic->vdev,
1963 VNIC_DEV_INTR_MODE_MSIX);
1965 return 0;
1969 if (enic->config.intr_mode < 1 &&
1970 enic->rq_count >= 1 &&
1971 enic->wq_count >= m &&
1972 enic->cq_count >= 1 + m &&
1973 enic->intr_count >= 1 + m + 2) {
1974 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
1976 enic->rq_count = 1;
1977 enic->wq_count = m;
1978 enic->cq_count = 1 + m;
1979 enic->intr_count = 1 + m + 2;
1981 vnic_dev_set_intr_mode(enic->vdev,
1982 VNIC_DEV_INTR_MODE_MSIX);
1984 return 0;
1988 /* Next try MSI
1990 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
1993 if (enic->config.intr_mode < 2 &&
1994 enic->rq_count >= 1 &&
1995 enic->wq_count >= 1 &&
1996 enic->cq_count >= 2 &&
1997 enic->intr_count >= 1 &&
1998 !pci_enable_msi(enic->pdev)) {
2000 enic->rq_count = 1;
2001 enic->wq_count = 1;
2002 enic->cq_count = 2;
2003 enic->intr_count = 1;
2005 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2007 return 0;
2010 /* Next try INTx
2012 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2013 * (the first INTR is used for WQ/RQ)
2014 * (the second INTR is used for WQ/RQ errors)
2015 * (the last INTR is used for notifications)
2018 if (enic->config.intr_mode < 3 &&
2019 enic->rq_count >= 1 &&
2020 enic->wq_count >= 1 &&
2021 enic->cq_count >= 2 &&
2022 enic->intr_count >= 3) {
2024 enic->rq_count = 1;
2025 enic->wq_count = 1;
2026 enic->cq_count = 2;
2027 enic->intr_count = 3;
2029 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2031 return 0;
2034 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2036 return -EINVAL;
2039 static void enic_clear_intr_mode(struct enic *enic)
2041 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2042 case VNIC_DEV_INTR_MODE_MSIX:
2043 pci_disable_msix(enic->pdev);
2044 break;
2045 case VNIC_DEV_INTR_MODE_MSI:
2046 pci_disable_msi(enic->pdev);
2047 break;
2048 default:
2049 break;
2052 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2055 static const struct net_device_ops enic_netdev_dynamic_ops = {
2056 .ndo_open = enic_open,
2057 .ndo_stop = enic_stop,
2058 .ndo_start_xmit = enic_hard_start_xmit,
2059 .ndo_get_stats = enic_get_stats,
2060 .ndo_validate_addr = eth_validate_addr,
2061 .ndo_set_rx_mode = enic_set_rx_mode,
2062 .ndo_set_multicast_list = enic_set_rx_mode,
2063 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2064 .ndo_change_mtu = enic_change_mtu,
2065 .ndo_vlan_rx_register = enic_vlan_rx_register,
2066 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2067 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2068 .ndo_tx_timeout = enic_tx_timeout,
2069 .ndo_set_vf_port = enic_set_vf_port,
2070 .ndo_get_vf_port = enic_get_vf_port,
2071 .ndo_set_vf_mac = enic_set_vf_mac,
2072 #ifdef CONFIG_NET_POLL_CONTROLLER
2073 .ndo_poll_controller = enic_poll_controller,
2074 #endif
2077 static const struct net_device_ops enic_netdev_ops = {
2078 .ndo_open = enic_open,
2079 .ndo_stop = enic_stop,
2080 .ndo_start_xmit = enic_hard_start_xmit,
2081 .ndo_get_stats = enic_get_stats,
2082 .ndo_validate_addr = eth_validate_addr,
2083 .ndo_set_mac_address = enic_set_mac_address,
2084 .ndo_set_rx_mode = enic_set_rx_mode,
2085 .ndo_set_multicast_list = enic_set_rx_mode,
2086 .ndo_change_mtu = enic_change_mtu,
2087 .ndo_vlan_rx_register = enic_vlan_rx_register,
2088 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2089 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2090 .ndo_tx_timeout = enic_tx_timeout,
2091 #ifdef CONFIG_NET_POLL_CONTROLLER
2092 .ndo_poll_controller = enic_poll_controller,
2093 #endif
2096 static void enic_dev_deinit(struct enic *enic)
2098 unsigned int i;
2100 for (i = 0; i < enic->rq_count; i++)
2101 netif_napi_del(&enic->napi[i]);
2103 enic_free_vnic_resources(enic);
2104 enic_clear_intr_mode(enic);
2107 static int enic_dev_init(struct enic *enic)
2109 struct device *dev = enic_get_dev(enic);
2110 struct net_device *netdev = enic->netdev;
2111 unsigned int i;
2112 int err;
2114 /* Get vNIC configuration
2117 err = enic_get_vnic_config(enic);
2118 if (err) {
2119 dev_err(dev, "Get vNIC configuration failed, aborting\n");
2120 return err;
2123 /* Get available resource counts
2126 enic_get_res_counts(enic);
2128 /* Set interrupt mode based on resource counts and system
2129 * capabilities
2132 err = enic_set_intr_mode(enic);
2133 if (err) {
2134 dev_err(dev, "Failed to set intr mode based on resource "
2135 "counts and system capabilities, aborting\n");
2136 return err;
2139 /* Allocate and configure vNIC resources
2142 err = enic_alloc_vnic_resources(enic);
2143 if (err) {
2144 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2145 goto err_out_free_vnic_resources;
2148 enic_init_vnic_resources(enic);
2150 err = enic_set_rss_nic_cfg(enic);
2151 if (err) {
2152 dev_err(dev, "Failed to config nic, aborting\n");
2153 goto err_out_free_vnic_resources;
2156 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2157 default:
2158 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
2159 break;
2160 case VNIC_DEV_INTR_MODE_MSIX:
2161 for (i = 0; i < enic->rq_count; i++)
2162 netif_napi_add(netdev, &enic->napi[i],
2163 enic_poll_msix, 64);
2164 break;
2167 return 0;
2169 err_out_free_vnic_resources:
2170 enic_clear_intr_mode(enic);
2171 enic_free_vnic_resources(enic);
2173 return err;
2176 static void enic_iounmap(struct enic *enic)
2178 unsigned int i;
2180 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2181 if (enic->bar[i].vaddr)
2182 iounmap(enic->bar[i].vaddr);
2185 static int __devinit enic_probe(struct pci_dev *pdev,
2186 const struct pci_device_id *ent)
2188 struct device *dev = &pdev->dev;
2189 struct net_device *netdev;
2190 struct enic *enic;
2191 int using_dac = 0;
2192 unsigned int i;
2193 int err;
2195 /* Allocate net device structure and initialize. Private
2196 * instance data is initialized to zero.
2199 netdev = alloc_etherdev(sizeof(struct enic));
2200 if (!netdev) {
2201 pr_err("Etherdev alloc failed, aborting\n");
2202 return -ENOMEM;
2205 pci_set_drvdata(pdev, netdev);
2207 SET_NETDEV_DEV(netdev, &pdev->dev);
2209 enic = netdev_priv(netdev);
2210 enic->netdev = netdev;
2211 enic->pdev = pdev;
2213 /* Setup PCI resources
2216 err = pci_enable_device_mem(pdev);
2217 if (err) {
2218 dev_err(dev, "Cannot enable PCI device, aborting\n");
2219 goto err_out_free_netdev;
2222 err = pci_request_regions(pdev, DRV_NAME);
2223 if (err) {
2224 dev_err(dev, "Cannot request PCI regions, aborting\n");
2225 goto err_out_disable_device;
2228 pci_set_master(pdev);
2230 /* Query PCI controller on system for DMA addressing
2231 * limitation for the device. Try 40-bit first, and
2232 * fail to 32-bit.
2235 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
2236 if (err) {
2237 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2238 if (err) {
2239 dev_err(dev, "No usable DMA configuration, aborting\n");
2240 goto err_out_release_regions;
2242 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2243 if (err) {
2244 dev_err(dev, "Unable to obtain %u-bit DMA "
2245 "for consistent allocations, aborting\n", 32);
2246 goto err_out_release_regions;
2248 } else {
2249 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2250 if (err) {
2251 dev_err(dev, "Unable to obtain %u-bit DMA "
2252 "for consistent allocations, aborting\n", 40);
2253 goto err_out_release_regions;
2255 using_dac = 1;
2258 /* Map vNIC resources from BAR0-5
2261 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2262 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2263 continue;
2264 enic->bar[i].len = pci_resource_len(pdev, i);
2265 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2266 if (!enic->bar[i].vaddr) {
2267 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2268 err = -ENODEV;
2269 goto err_out_iounmap;
2271 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2274 /* Register vNIC device
2277 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2278 ARRAY_SIZE(enic->bar));
2279 if (!enic->vdev) {
2280 dev_err(dev, "vNIC registration failed, aborting\n");
2281 err = -ENODEV;
2282 goto err_out_iounmap;
2285 /* Issue device open to get device in known state
2288 err = enic_dev_open(enic);
2289 if (err) {
2290 dev_err(dev, "vNIC dev open failed, aborting\n");
2291 goto err_out_vnic_unregister;
2294 /* Setup devcmd lock
2297 spin_lock_init(&enic->devcmd_lock);
2300 * Set ingress vlan rewrite mode before vnic initialization
2303 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2304 if (err) {
2305 dev_err(dev,
2306 "Failed to set ingress vlan rewrite mode, aborting.\n");
2307 goto err_out_dev_close;
2310 /* Issue device init to initialize the vnic-to-switch link.
2311 * We'll start with carrier off and wait for link UP
2312 * notification later to turn on carrier. We don't need
2313 * to wait here for the vnic-to-switch link initialization
2314 * to complete; link UP notification is the indication that
2315 * the process is complete.
2318 netif_carrier_off(netdev);
2320 /* Do not call dev_init for a dynamic vnic.
2321 * For a dynamic vnic, init_prov_info will be
2322 * called later by an upper layer.
2325 if (!enic_is_dynamic(enic)) {
2326 err = vnic_dev_init(enic->vdev, 0);
2327 if (err) {
2328 dev_err(dev, "vNIC dev init failed, aborting\n");
2329 goto err_out_dev_close;
2333 err = enic_dev_init(enic);
2334 if (err) {
2335 dev_err(dev, "Device initialization failed, aborting\n");
2336 goto err_out_dev_close;
2339 /* Setup notification timer, HW reset task, and wq locks
2342 init_timer(&enic->notify_timer);
2343 enic->notify_timer.function = enic_notify_timer;
2344 enic->notify_timer.data = (unsigned long)enic;
2346 INIT_WORK(&enic->reset, enic_reset);
2348 for (i = 0; i < enic->wq_count; i++)
2349 spin_lock_init(&enic->wq_lock[i]);
2351 /* Register net device
2354 enic->port_mtu = enic->config.mtu;
2355 (void)enic_change_mtu(netdev, enic->port_mtu);
2357 err = enic_set_mac_addr(netdev, enic->mac_addr);
2358 if (err) {
2359 dev_err(dev, "Invalid MAC address, aborting\n");
2360 goto err_out_dev_deinit;
2363 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2364 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2366 if (enic_is_dynamic(enic))
2367 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2368 else
2369 netdev->netdev_ops = &enic_netdev_ops;
2371 netdev->watchdog_timeo = 2 * HZ;
2372 netdev->ethtool_ops = &enic_ethtool_ops;
2374 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2375 if (ENIC_SETTING(enic, LOOP)) {
2376 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2377 enic->loop_enable = 1;
2378 enic->loop_tag = enic->config.loop_tag;
2379 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2381 if (ENIC_SETTING(enic, TXCSUM))
2382 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2383 if (ENIC_SETTING(enic, TSO))
2384 netdev->hw_features |= NETIF_F_TSO |
2385 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2386 if (ENIC_SETTING(enic, RXCSUM))
2387 netdev->hw_features |= NETIF_F_RXCSUM;
2389 netdev->features |= netdev->hw_features;
2391 if (using_dac)
2392 netdev->features |= NETIF_F_HIGHDMA;
2394 err = register_netdev(netdev);
2395 if (err) {
2396 dev_err(dev, "Cannot register net device, aborting\n");
2397 goto err_out_dev_deinit;
2400 return 0;
2402 err_out_dev_deinit:
2403 enic_dev_deinit(enic);
2404 err_out_dev_close:
2405 vnic_dev_close(enic->vdev);
2406 err_out_vnic_unregister:
2407 vnic_dev_unregister(enic->vdev);
2408 err_out_iounmap:
2409 enic_iounmap(enic);
2410 err_out_release_regions:
2411 pci_release_regions(pdev);
2412 err_out_disable_device:
2413 pci_disable_device(pdev);
2414 err_out_free_netdev:
2415 pci_set_drvdata(pdev, NULL);
2416 free_netdev(netdev);
2418 return err;
2421 static void __devexit enic_remove(struct pci_dev *pdev)
2423 struct net_device *netdev = pci_get_drvdata(pdev);
2425 if (netdev) {
2426 struct enic *enic = netdev_priv(netdev);
2428 cancel_work_sync(&enic->reset);
2429 unregister_netdev(netdev);
2430 enic_dev_deinit(enic);
2431 vnic_dev_close(enic->vdev);
2432 vnic_dev_unregister(enic->vdev);
2433 enic_iounmap(enic);
2434 pci_release_regions(pdev);
2435 pci_disable_device(pdev);
2436 pci_set_drvdata(pdev, NULL);
2437 free_netdev(netdev);
2441 static struct pci_driver enic_driver = {
2442 .name = DRV_NAME,
2443 .id_table = enic_id_table,
2444 .probe = enic_probe,
2445 .remove = __devexit_p(enic_remove),
2448 static int __init enic_init_module(void)
2450 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2452 return pci_register_driver(&enic_driver);
2455 static void __exit enic_cleanup_module(void)
2457 pci_unregister_driver(&enic_driver);
2460 module_init(enic_init_module);
2461 module_exit(enic_cleanup_module);