1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/types.h>
4 #include <linux/module.h>
5 #include <linux/list.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/pagemap.h>
9 #include <linux/sched.h>
10 #include <linux/dmapool.h>
11 #include <linux/mempool.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/interrupt.h>
15 #include <linux/errno.h>
16 #include <linux/ioport.h>
19 #include <linux/ipv6.h>
21 #include <linux/tcp.h>
22 #include <linux/udp.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/skbuff.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/if_vlan.h>
31 #include <linux/delay.h>
33 #include <linux/vmalloc.h>
38 static const char ql_gstrings_test[][ETH_GSTRING_LEN] = {
39 "Loopback test (offline)"
41 #define QLGE_TEST_LEN (sizeof(ql_gstrings_test) / ETH_GSTRING_LEN)
43 static int ql_update_ring_coalescing(struct ql_adapter *qdev)
46 struct rx_ring *rx_ring;
49 if (!netif_running(qdev->ndev))
52 /* Skip the default queue, and update the outbound handler
53 * queues if they changed.
55 cqicb = (struct cqicb *)&qdev->rx_ring[qdev->rss_ring_count];
56 if (le16_to_cpu(cqicb->irq_delay) != qdev->tx_coalesce_usecs ||
57 le16_to_cpu(cqicb->pkt_delay) !=
58 qdev->tx_max_coalesced_frames) {
59 for (i = qdev->rss_ring_count; i < qdev->rx_ring_count; i++) {
60 rx_ring = &qdev->rx_ring[i];
61 cqicb = (struct cqicb *)rx_ring;
62 cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
64 cpu_to_le16(qdev->tx_max_coalesced_frames);
65 cqicb->flags = FLAGS_LI;
66 status = ql_write_cfg(qdev, cqicb, sizeof(*cqicb),
67 CFG_LCQ, rx_ring->cq_id);
69 netif_err(qdev, ifup, qdev->ndev,
70 "Failed to load CQICB.\n");
76 /* Update the inbound (RSS) handler queues if they changed. */
77 cqicb = (struct cqicb *)&qdev->rx_ring[0];
78 if (le16_to_cpu(cqicb->irq_delay) != qdev->rx_coalesce_usecs ||
79 le16_to_cpu(cqicb->pkt_delay) !=
80 qdev->rx_max_coalesced_frames) {
81 for (i = 0; i < qdev->rss_ring_count; i++, rx_ring++) {
82 rx_ring = &qdev->rx_ring[i];
83 cqicb = (struct cqicb *)rx_ring;
84 cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
86 cpu_to_le16(qdev->rx_max_coalesced_frames);
87 cqicb->flags = FLAGS_LI;
88 status = ql_write_cfg(qdev, cqicb, sizeof(*cqicb),
89 CFG_LCQ, rx_ring->cq_id);
91 netif_err(qdev, ifup, qdev->ndev,
92 "Failed to load CQICB.\n");
101 static void ql_update_stats(struct ql_adapter *qdev)
105 u64 *iter = &qdev->nic_stats.tx_pkts;
107 spin_lock(&qdev->stats_lock);
108 if (ql_sem_spinlock(qdev, qdev->xg_sem_mask)) {
109 netif_err(qdev, drv, qdev->ndev,
110 "Couldn't get xgmac sem.\n");
116 for (i = 0x200; i < 0x280; i += 8) {
117 if (ql_read_xgmac_reg64(qdev, i, &data)) {
118 netif_err(qdev, drv, qdev->ndev,
119 "Error reading status register 0x%.04x.\n",
130 for (i = 0x300; i < 0x3d0; i += 8) {
131 if (ql_read_xgmac_reg64(qdev, i, &data)) {
132 netif_err(qdev, drv, qdev->ndev,
133 "Error reading status register 0x%.04x.\n",
142 * Get Per-priority TX pause frame counter statistics.
144 for (i = 0x500; i < 0x540; i += 8) {
145 if (ql_read_xgmac_reg64(qdev, i, &data)) {
146 netif_err(qdev, drv, qdev->ndev,
147 "Error reading status register 0x%.04x.\n",
156 * Get Per-priority RX pause frame counter statistics.
158 for (i = 0x568; i < 0x5a8; i += 8) {
159 if (ql_read_xgmac_reg64(qdev, i, &data)) {
160 netif_err(qdev, drv, qdev->ndev,
161 "Error reading status register 0x%.04x.\n",
170 * Get RX NIC FIFO DROP statistics.
172 if (ql_read_xgmac_reg64(qdev, 0x5b8, &data)) {
173 netif_err(qdev, drv, qdev->ndev,
174 "Error reading status register 0x%.04x.\n", i);
179 ql_sem_unlock(qdev, qdev->xg_sem_mask);
181 spin_unlock(&qdev->stats_lock);
188 static char ql_stats_str_arr[][ETH_GSTRING_LEN] = {
197 {"tx_65_to_127_pkts"},
198 {"tx_128_to_255_pkts"},
200 {"tx_512_to_1023_pkts"},
201 {"tx_1024_to_1518_pkts"},
202 {"tx_1519_to_max_pkts"},
203 {"tx_undersize_pkts"},
204 {"tx_oversize_pkts"},
212 {"rx_undersize_pkts"},
213 {"rx_oversize_pkts"},
215 {"rx_undersize_fcerr_pkts"},
224 {"rx_65_to_127_pkts"},
227 {"rx_512_to_1023_pkts"},
228 {"rx_1024_to_1518_pkts"},
229 {"rx_1519_to_max_pkts"},
231 {"tx_cbfc_pause_frames0"},
232 {"tx_cbfc_pause_frames1"},
233 {"tx_cbfc_pause_frames2"},
234 {"tx_cbfc_pause_frames3"},
235 {"tx_cbfc_pause_frames4"},
236 {"tx_cbfc_pause_frames5"},
237 {"tx_cbfc_pause_frames6"},
238 {"tx_cbfc_pause_frames7"},
239 {"rx_cbfc_pause_frames0"},
240 {"rx_cbfc_pause_frames1"},
241 {"rx_cbfc_pause_frames2"},
242 {"rx_cbfc_pause_frames3"},
243 {"rx_cbfc_pause_frames4"},
244 {"rx_cbfc_pause_frames5"},
245 {"rx_cbfc_pause_frames6"},
246 {"rx_cbfc_pause_frames7"},
247 {"rx_nic_fifo_drop"},
250 static void ql_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
254 memcpy(buf, ql_stats_str_arr, sizeof(ql_stats_str_arr));
259 static int ql_get_sset_count(struct net_device *dev, int sset)
263 return QLGE_TEST_LEN;
265 return ARRAY_SIZE(ql_stats_str_arr);
272 ql_get_ethtool_stats(struct net_device *ndev,
273 struct ethtool_stats *stats, u64 *data)
275 struct ql_adapter *qdev = netdev_priv(ndev);
276 struct nic_stats *s = &qdev->nic_stats;
278 ql_update_stats(qdev);
280 *data++ = s->tx_pkts;
281 *data++ = s->tx_bytes;
282 *data++ = s->tx_mcast_pkts;
283 *data++ = s->tx_bcast_pkts;
284 *data++ = s->tx_ucast_pkts;
285 *data++ = s->tx_ctl_pkts;
286 *data++ = s->tx_pause_pkts;
287 *data++ = s->tx_64_pkt;
288 *data++ = s->tx_65_to_127_pkt;
289 *data++ = s->tx_128_to_255_pkt;
290 *data++ = s->tx_256_511_pkt;
291 *data++ = s->tx_512_to_1023_pkt;
292 *data++ = s->tx_1024_to_1518_pkt;
293 *data++ = s->tx_1519_to_max_pkt;
294 *data++ = s->tx_undersize_pkt;
295 *data++ = s->tx_oversize_pkt;
296 *data++ = s->rx_bytes;
297 *data++ = s->rx_bytes_ok;
298 *data++ = s->rx_pkts;
299 *data++ = s->rx_pkts_ok;
300 *data++ = s->rx_bcast_pkts;
301 *data++ = s->rx_mcast_pkts;
302 *data++ = s->rx_ucast_pkts;
303 *data++ = s->rx_undersize_pkts;
304 *data++ = s->rx_oversize_pkts;
305 *data++ = s->rx_jabber_pkts;
306 *data++ = s->rx_undersize_fcerr_pkts;
307 *data++ = s->rx_drop_events;
308 *data++ = s->rx_fcerr_pkts;
309 *data++ = s->rx_align_err;
310 *data++ = s->rx_symbol_err;
311 *data++ = s->rx_mac_err;
312 *data++ = s->rx_ctl_pkts;
313 *data++ = s->rx_pause_pkts;
314 *data++ = s->rx_64_pkts;
315 *data++ = s->rx_65_to_127_pkts;
316 *data++ = s->rx_128_255_pkts;
317 *data++ = s->rx_256_511_pkts;
318 *data++ = s->rx_512_to_1023_pkts;
319 *data++ = s->rx_1024_to_1518_pkts;
320 *data++ = s->rx_1519_to_max_pkts;
321 *data++ = s->rx_len_err_pkts;
322 *data++ = s->tx_cbfc_pause_frames0;
323 *data++ = s->tx_cbfc_pause_frames1;
324 *data++ = s->tx_cbfc_pause_frames2;
325 *data++ = s->tx_cbfc_pause_frames3;
326 *data++ = s->tx_cbfc_pause_frames4;
327 *data++ = s->tx_cbfc_pause_frames5;
328 *data++ = s->tx_cbfc_pause_frames6;
329 *data++ = s->tx_cbfc_pause_frames7;
330 *data++ = s->rx_cbfc_pause_frames0;
331 *data++ = s->rx_cbfc_pause_frames1;
332 *data++ = s->rx_cbfc_pause_frames2;
333 *data++ = s->rx_cbfc_pause_frames3;
334 *data++ = s->rx_cbfc_pause_frames4;
335 *data++ = s->rx_cbfc_pause_frames5;
336 *data++ = s->rx_cbfc_pause_frames6;
337 *data++ = s->rx_cbfc_pause_frames7;
338 *data++ = s->rx_nic_fifo_drop;
341 static int ql_get_settings(struct net_device *ndev,
342 struct ethtool_cmd *ecmd)
344 struct ql_adapter *qdev = netdev_priv(ndev);
346 ecmd->supported = SUPPORTED_10000baseT_Full;
347 ecmd->advertising = ADVERTISED_10000baseT_Full;
348 ecmd->autoneg = AUTONEG_ENABLE;
349 ecmd->transceiver = XCVR_EXTERNAL;
350 if ((qdev->link_status & STS_LINK_TYPE_MASK) ==
351 STS_LINK_TYPE_10GBASET) {
352 ecmd->supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
353 ecmd->advertising |= (ADVERTISED_TP | ADVERTISED_Autoneg);
354 ecmd->port = PORT_TP;
356 ecmd->supported |= SUPPORTED_FIBRE;
357 ecmd->advertising |= ADVERTISED_FIBRE;
358 ecmd->port = PORT_FIBRE;
361 ecmd->speed = SPEED_10000;
362 ecmd->duplex = DUPLEX_FULL;
367 static void ql_get_drvinfo(struct net_device *ndev,
368 struct ethtool_drvinfo *drvinfo)
370 struct ql_adapter *qdev = netdev_priv(ndev);
371 strncpy(drvinfo->driver, qlge_driver_name, 32);
372 strncpy(drvinfo->version, qlge_driver_version, 32);
373 snprintf(drvinfo->fw_version, 32, "v%d.%d.%d",
374 (qdev->fw_rev_id & 0x00ff0000) >> 16,
375 (qdev->fw_rev_id & 0x0000ff00) >> 8,
376 (qdev->fw_rev_id & 0x000000ff));
377 strncpy(drvinfo->bus_info, pci_name(qdev->pdev), 32);
378 drvinfo->n_stats = 0;
379 drvinfo->testinfo_len = 0;
380 drvinfo->regdump_len = 0;
381 drvinfo->eedump_len = 0;
384 static void ql_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
386 struct ql_adapter *qdev = netdev_priv(ndev);
387 /* What we support. */
388 wol->supported = WAKE_MAGIC;
389 /* What we've currently got set. */
390 wol->wolopts = qdev->wol;
393 static int ql_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
395 struct ql_adapter *qdev = netdev_priv(ndev);
398 if (wol->wolopts & ~WAKE_MAGIC)
400 qdev->wol = wol->wolopts;
402 netif_info(qdev, drv, qdev->ndev, "Set wol option 0x%x\n", qdev->wol);
405 status = ql_mb_wol_mode(qdev, wol);
406 netif_err(qdev, drv, qdev->ndev, "WOL %s (wol code 0x%x)\n",
407 status == 0 ? "cleared successfully" : "clear failed",
414 static int ql_phys_id(struct net_device *ndev, u32 data)
416 struct ql_adapter *qdev = netdev_priv(ndev);
420 /* Save the current LED settings */
421 status = ql_mb_get_led_cfg(qdev);
424 led_reg = qdev->led_config;
426 /* Start blinking the led */
427 if (!data || data > 300)
430 for (i = 0; i < (data * 10); i++)
431 ql_mb_set_led_cfg(qdev, QL_LED_BLINK);
433 /* Restore LED settings */
434 status = ql_mb_set_led_cfg(qdev, led_reg);
441 static int ql_start_loopback(struct ql_adapter *qdev)
443 if (netif_carrier_ok(qdev->ndev)) {
444 set_bit(QL_LB_LINK_UP, &qdev->flags);
445 netif_carrier_off(qdev->ndev);
447 clear_bit(QL_LB_LINK_UP, &qdev->flags);
448 qdev->link_config |= CFG_LOOPBACK_PCS;
449 return ql_mb_set_port_cfg(qdev);
452 static void ql_stop_loopback(struct ql_adapter *qdev)
454 qdev->link_config &= ~CFG_LOOPBACK_PCS;
455 ql_mb_set_port_cfg(qdev);
456 if (test_bit(QL_LB_LINK_UP, &qdev->flags)) {
457 netif_carrier_on(qdev->ndev);
458 clear_bit(QL_LB_LINK_UP, &qdev->flags);
462 static void ql_create_lb_frame(struct sk_buff *skb,
463 unsigned int frame_size)
465 memset(skb->data, 0xFF, frame_size);
467 memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
468 memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
469 memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
472 void ql_check_lb_frame(struct ql_adapter *qdev,
475 unsigned int frame_size = skb->len;
477 if ((*(skb->data + 3) == 0xFF) &&
478 (*(skb->data + frame_size / 2 + 10) == 0xBE) &&
479 (*(skb->data + frame_size / 2 + 12) == 0xAF)) {
480 atomic_dec(&qdev->lb_count);
485 static int ql_run_loopback_test(struct ql_adapter *qdev)
490 unsigned int size = SMALL_BUF_MAP_SIZE;
492 for (i = 0; i < 64; i++) {
493 skb = netdev_alloc_skb(qdev->ndev, size);
497 skb->queue_mapping = 0;
499 ql_create_lb_frame(skb, size);
500 rc = ql_lb_send(skb, qdev->ndev);
501 if (rc != NETDEV_TX_OK)
503 atomic_inc(&qdev->lb_count);
505 /* Give queue time to settle before testing results. */
507 ql_clean_lb_rx_ring(&qdev->rx_ring[0], 128);
508 return atomic_read(&qdev->lb_count) ? -EIO : 0;
511 static int ql_loopback_test(struct ql_adapter *qdev, u64 *data)
513 *data = ql_start_loopback(qdev);
516 *data = ql_run_loopback_test(qdev);
518 ql_stop_loopback(qdev);
522 static void ql_self_test(struct net_device *ndev,
523 struct ethtool_test *eth_test, u64 *data)
525 struct ql_adapter *qdev = netdev_priv(ndev);
527 if (netif_running(ndev)) {
528 set_bit(QL_SELFTEST, &qdev->flags);
529 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
531 if (ql_loopback_test(qdev, &data[0]))
532 eth_test->flags |= ETH_TEST_FL_FAILED;
538 clear_bit(QL_SELFTEST, &qdev->flags);
539 /* Give link time to come up after
540 * port configuration changes.
542 msleep_interruptible(4 * 1000);
544 netif_err(qdev, drv, qdev->ndev,
545 "is down, Loopback test will fail.\n");
546 eth_test->flags |= ETH_TEST_FL_FAILED;
550 static int ql_get_regs_len(struct net_device *ndev)
552 return sizeof(struct ql_reg_dump);
555 static void ql_get_regs(struct net_device *ndev,
556 struct ethtool_regs *regs, void *p)
558 struct ql_adapter *qdev = netdev_priv(ndev);
560 ql_gen_reg_dump(qdev, p);
563 static int ql_get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
565 struct ql_adapter *qdev = netdev_priv(dev);
567 c->rx_coalesce_usecs = qdev->rx_coalesce_usecs;
568 c->tx_coalesce_usecs = qdev->tx_coalesce_usecs;
570 /* This chip coalesces as follows:
571 * If a packet arrives, hold off interrupts until
572 * cqicb->int_delay expires, but if no other packets arrive don't
573 * wait longer than cqicb->pkt_int_delay. But ethtool doesn't use a
574 * timer to coalesce on a frame basis. So, we have to take ethtool's
575 * max_coalesced_frames value and convert it to a delay in microseconds.
576 * We do this by using a basic thoughput of 1,000,000 frames per
577 * second @ (1024 bytes). This means one frame per usec. So it's a
578 * simple one to one ratio.
580 c->rx_max_coalesced_frames = qdev->rx_max_coalesced_frames;
581 c->tx_max_coalesced_frames = qdev->tx_max_coalesced_frames;
586 static int ql_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *c)
588 struct ql_adapter *qdev = netdev_priv(ndev);
590 /* Validate user parameters. */
591 if (c->rx_coalesce_usecs > qdev->rx_ring_size / 2)
593 /* Don't wait more than 10 usec. */
594 if (c->rx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
596 if (c->tx_coalesce_usecs > qdev->tx_ring_size / 2)
598 if (c->tx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
601 /* Verify a change took place before updating the hardware. */
602 if (qdev->rx_coalesce_usecs == c->rx_coalesce_usecs &&
603 qdev->tx_coalesce_usecs == c->tx_coalesce_usecs &&
604 qdev->rx_max_coalesced_frames == c->rx_max_coalesced_frames &&
605 qdev->tx_max_coalesced_frames == c->tx_max_coalesced_frames)
608 qdev->rx_coalesce_usecs = c->rx_coalesce_usecs;
609 qdev->tx_coalesce_usecs = c->tx_coalesce_usecs;
610 qdev->rx_max_coalesced_frames = c->rx_max_coalesced_frames;
611 qdev->tx_max_coalesced_frames = c->tx_max_coalesced_frames;
613 return ql_update_ring_coalescing(qdev);
616 static void ql_get_pauseparam(struct net_device *netdev,
617 struct ethtool_pauseparam *pause)
619 struct ql_adapter *qdev = netdev_priv(netdev);
621 ql_mb_get_port_cfg(qdev);
622 if (qdev->link_config & CFG_PAUSE_STD) {
628 static int ql_set_pauseparam(struct net_device *netdev,
629 struct ethtool_pauseparam *pause)
631 struct ql_adapter *qdev = netdev_priv(netdev);
634 if ((pause->rx_pause) && (pause->tx_pause))
635 qdev->link_config |= CFG_PAUSE_STD;
636 else if (!pause->rx_pause && !pause->tx_pause)
637 qdev->link_config &= ~CFG_PAUSE_STD;
641 status = ql_mb_set_port_cfg(qdev);
647 static u32 ql_get_rx_csum(struct net_device *netdev)
649 struct ql_adapter *qdev = netdev_priv(netdev);
650 return qdev->rx_csum;
653 static int ql_set_rx_csum(struct net_device *netdev, uint32_t data)
655 struct ql_adapter *qdev = netdev_priv(netdev);
656 qdev->rx_csum = data;
660 static int ql_set_tso(struct net_device *ndev, uint32_t data)
664 ndev->features |= NETIF_F_TSO;
665 ndev->features |= NETIF_F_TSO6;
667 ndev->features &= ~NETIF_F_TSO;
668 ndev->features &= ~NETIF_F_TSO6;
673 static u32 ql_get_msglevel(struct net_device *ndev)
675 struct ql_adapter *qdev = netdev_priv(ndev);
676 return qdev->msg_enable;
679 static void ql_set_msglevel(struct net_device *ndev, u32 value)
681 struct ql_adapter *qdev = netdev_priv(ndev);
682 qdev->msg_enable = value;
685 const struct ethtool_ops qlge_ethtool_ops = {
686 .get_settings = ql_get_settings,
687 .get_drvinfo = ql_get_drvinfo,
688 .get_wol = ql_get_wol,
689 .set_wol = ql_set_wol,
690 .get_regs_len = ql_get_regs_len,
691 .get_regs = ql_get_regs,
692 .get_msglevel = ql_get_msglevel,
693 .set_msglevel = ql_set_msglevel,
694 .get_link = ethtool_op_get_link,
695 .phys_id = ql_phys_id,
696 .self_test = ql_self_test,
697 .get_pauseparam = ql_get_pauseparam,
698 .set_pauseparam = ql_set_pauseparam,
699 .get_rx_csum = ql_get_rx_csum,
700 .set_rx_csum = ql_set_rx_csum,
701 .get_tx_csum = ethtool_op_get_tx_csum,
702 .set_tx_csum = ethtool_op_set_tx_csum,
703 .get_sg = ethtool_op_get_sg,
704 .set_sg = ethtool_op_set_sg,
705 .get_tso = ethtool_op_get_tso,
706 .set_tso = ql_set_tso,
707 .get_coalesce = ql_get_coalesce,
708 .set_coalesce = ql_set_coalesce,
709 .get_sset_count = ql_get_sset_count,
710 .get_strings = ql_get_strings,
711 .get_ethtool_stats = ql_get_ethtool_stats,