📄 dl2k.c
字号:
/* D-Link DL2000-based Gigabit Ethernet Adapter Linux driver *//* Copyright (c) 2001 by D-Link Corporation Written by Edward Peng.<edward_peng@dlink.com.tw> Created 03-May-2001, base on Linux' sundance.c. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.*//* Rev Date Description ========================================================================== 0.01 2001/05/03 Created DL2000-based linux driver 0.02 2001/05/21 Added VLAN and hardware checksum support. 1.00 2001/06/26 Added jumbo frame support. 1.01 2001/08/21 Added two parameters, rx_coalesce and rx_timeout. 1.02 2001/10/08 Supported fiber media. Added flow control parameters. 1.03 2001/10/12 Changed the default media to 1000mbps_fd for the fiber devices. 1.04 2001/11/08 Fixed Tx stopped when tx very busy. 1.05 2001/11/22 Fixed Tx stopped when unidirectional tx busy. 1.06 2001/12/13 Fixed disconnect bug at 10Mbps mode. Fixed tx_full flag incorrect. Added tx_coalesce paramter. 1.07 2002/01/03 Fixed miscount of RX frame error. 1.08 2002/01/17 Fixed the multicast bug. */#include "dl2k.h"static char version[] __devinitdata = KERN_INFO "D-Link DL2000-based linux driver v1.08 2002/01/17\n";#define MAX_UNITS 8static int mtu[MAX_UNITS];static int vlan[MAX_UNITS];static int jumbo[MAX_UNITS];static char *media[MAX_UNITS];static int tx_flow[MAX_UNITS];static int rx_flow[MAX_UNITS];static int copy_thresh;static int rx_coalesce = DEFAULT_RXC;static int rx_timeout = DEFAULT_RXT; static int tx_coalesce = DEFAULT_TXC; MODULE_AUTHOR ("Edward Peng");MODULE_DESCRIPTION ("D-Link DL2000-based Gigabit Ethernet Adapter");MODULE_LICENSE("GPL");MODULE_PARM (mtu, "1-" __MODULE_STRING (MAX_UNITS) "i");MODULE_PARM (media, "1-" __MODULE_STRING (MAX_UNITS) "s");MODULE_PARM (vlan, "1-" __MODULE_STRING (MAX_UNITS) "i");MODULE_PARM (jumbo, "1-" __MODULE_STRING (MAX_UNITS) "i");MODULE_PARM (tx_flow, "1-" __MODULE_STRING (MAX_UNITS) "i");MODULE_PARM (rx_flow, "1-" __MODULE_STRING (MAX_UNITS) "i");MODULE_PARM (copy_thresh, "i");MODULE_PARM (rx_coalesce, "i"); /* Rx frame count each interrupt */MODULE_PARM (rx_timeout, "i"); /* Rx DMA wait time in 64ns increments */MODULE_PARM (tx_coalesce, "i"); /* HW xmit count each TxComplete [1-8] *//* Enable the default interrupts */#define DEFAULT_INTR (RxDMAComplete | HostError | IntRequested | TxComplete| \ UpdateStats | LinkEvent)#define EnableInt() \writew(DEFAULT_INTR, ioaddr + IntEnable)static int max_intrloop = 50;static int multicast_filter_limit = 0x40;static int rio_open (struct net_device *dev);static void tx_timeout (struct net_device *dev);static void alloc_list (struct net_device *dev);static int start_xmit (struct sk_buff *skb, struct net_device *dev);static void rio_interrupt (int irq, void *dev_instance, struct pt_regs *regs);static void tx_error (struct net_device *dev, int tx_status);static int receive_packet (struct net_device *dev);static void rio_error (struct net_device *dev, int int_status);static int change_mtu (struct net_device *dev, int new_mtu);static void set_multicast (struct net_device *dev);static struct net_device_stats *get_stats (struct net_device *dev);static int rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);static int rio_close (struct net_device *dev);static int find_miiphy (struct net_device *dev);static int parse_eeprom (struct net_device *dev);static int read_eeprom (long ioaddr, int eep_addr);static unsigned get_crc (unsigned char *p, int len);static int mii_wait_link (struct net_device *dev, int wait);static int mii_set_media (struct net_device *dev);static int mii_get_media (struct net_device *dev);static int mii_set_media_pcs (struct net_device *dev);static int mii_get_media_pcs (struct net_device *dev);static int mii_read (struct net_device *dev, int phy_addr, int reg_num);static int mii_write (struct net_device *dev, int phy_addr, int reg_num, u16 data);#ifdef RIO_DEBUGstatic int rio_ioctl_ext (struct net_device *dev, struct ioctl_data *iodata);#endifstatic int __devinitrio_probe1 (struct pci_dev *pdev, const struct pci_device_id *ent){ struct net_device *dev; struct netdev_private *np; static int card_idx; int chip_idx = ent->driver_data; int err, irq = pdev->irq; long ioaddr; static int version_printed; void *ring_space; dma_addr_t ring_dma; if (!version_printed++) printk ("%s", version); err = pci_enable_device (pdev); if (err) return err; err = pci_request_regions (pdev, "dl2k"); if (err) goto err_out_disable; pci_set_master (pdev); dev = alloc_etherdev (sizeof (*np)); if (!dev) { err = -ENOMEM; goto err_out_res; } SET_MODULE_OWNER (dev);#ifdef USE_IO_OPS ioaddr = pci_resource_start (pdev, 0);#else ioaddr = pci_resource_start (pdev, 1); ioaddr = (long) ioremap (ioaddr, RIO_IO_SIZE); if (!ioaddr) { err = -ENOMEM; goto err_out_dev; }#endif dev->base_addr = ioaddr; dev->irq = irq; np = dev->priv; np->chip_id = chip_idx; np->pdev = pdev; spin_lock_init (&np->lock); /* Parse manual configuration */ np->an_enable = 1; if (card_idx < MAX_UNITS) { if (media[card_idx] != NULL) { np->an_enable = 0; if (strcmp (media[card_idx], "auto") == 0 || strcmp (media[card_idx], "autosense") == 0 || strcmp (media[card_idx], "0") == 0 ) { np->an_enable = 2; } else if (strcmp (media[card_idx], "100mbps_fd") == 0 || strcmp (media[card_idx], "4") == 0) { np->speed = 100; np->full_duplex = 1; } else if (strcmp (media[card_idx], "100mbps_hd") == 0 || strcmp (media[card_idx], "3") == 0) { np->speed = 100; np->full_duplex = 0; } else if (strcmp (media[card_idx], "10mbps_fd") == 0 || strcmp (media[card_idx], "2") == 0) { np->speed = 10; np->full_duplex = 1; } else if (strcmp (media[card_idx], "10mbps_hd") == 0 || strcmp (media[card_idx], "1") == 0) { np->speed = 10; np->full_duplex = 0; } else if (strcmp (media[card_idx], "1000mbps_fd") == 0 || strcmp (media[card_idx], "6") == 0) { np->speed=1000; np->full_duplex=1; } else if (strcmp (media[card_idx], "1000mbps_hd") == 0 || strcmp (media[card_idx], "5") == 0) { np->speed = 1000; np->full_duplex = 0; } else { np->an_enable = 1; } } if (jumbo[card_idx] != 0) { np->jumbo = 1; dev->mtu = MAX_JUMBO; } else { np->jumbo = 0; if (mtu[card_idx] > 0 && mtu[card_idx] < PACKET_SIZE) dev->mtu = mtu[card_idx]; } np->vlan = (vlan[card_idx] > 0 && vlan[card_idx] < 4096) ? vlan[card_idx] : 0; if (rx_coalesce != 0 && rx_timeout != 0) { np->rx_coalesce = rx_coalesce; np->rx_timeout = rx_timeout; np->coalesce = 1; } np->tx_flow = (tx_flow[card_idx]) ? 1 : 0; np->rx_flow = (rx_flow[card_idx]) ? 1 : 0; if (tx_coalesce < 1) tx_coalesce = 1; if (tx_coalesce > 8) tx_coalesce = 8; } dev->open = &rio_open; dev->hard_start_xmit = &start_xmit; dev->stop = &rio_close; dev->get_stats = &get_stats; dev->set_multicast_list = &set_multicast; dev->do_ioctl = &rio_ioctl; dev->tx_timeout = &tx_timeout; dev->watchdog_timeo = TX_TIMEOUT; dev->change_mtu = &change_mtu;#if 0 dev->features = NETIF_F_IP_CSUM;#endif pci_set_drvdata (pdev, dev); ring_space = pci_alloc_consistent (pdev, TX_TOTAL_SIZE, &ring_dma); if (!ring_space) goto err_out_iounmap; np->tx_ring = (struct netdev_desc *) ring_space; np->tx_ring_dma = ring_dma; ring_space = pci_alloc_consistent (pdev, RX_TOTAL_SIZE, &ring_dma); if (!ring_space) goto err_out_unmap_tx; np->rx_ring = (struct netdev_desc *) ring_space; np->rx_ring_dma = ring_dma; /* Parse eeprom data */ parse_eeprom (dev); /* Find PHY address */ err = find_miiphy (dev); if (err) goto err_out_unmap_rx; /* Fiber device? */ np->phy_media = (readw(ioaddr + ASICCtrl) & PhyMedia) ? 1 : 0; /* Set media and reset PHY */ if (np->phy_media) { /* default 1000mbps_fd for fiber deivices */ if (np->an_enable == 1) { np->an_enable = 0; np->speed = 1000; np->full_duplex = 1; } else if (np->an_enable == 2) { np->an_enable = 1; } mii_set_media_pcs (dev); } else { /* Auto-Negotiation is mandatory for 1000BASE-T, IEEE 802.3ab Annex 28D page 14 */ if (np->speed == 1000) np->an_enable = 1; mii_set_media (dev); } /* Reset all logic functions */ writew (GlobalReset | DMAReset | FIFOReset | NetworkReset | HostReset, ioaddr + ASICCtrl + 2); err = register_netdev (dev); if (err) goto err_out_unmap_rx; card_idx++; printk (KERN_INFO "%s: %s, %02x:%02x:%02x:%02x:%02x:%02x, IRQ %d\n", dev->name, np->name, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5], irq); return 0; err_out_unmap_rx: pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma); err_out_unmap_tx: pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma); err_out_iounmap:#ifndef USE_IO_OPS iounmap ((void *) ioaddr); err_out_dev:#endif kfree (dev); err_out_res: pci_release_regions (pdev); err_out_disable: pci_disable_device (pdev); return err;}intfind_miiphy (struct net_device *dev){ int i, phy_found = 0; struct netdev_private *np; long ioaddr; np = dev->priv; ioaddr = dev->base_addr; np->phy_addr = 1; for (i = 31; i >= 0; i--) { int mii_status = mii_read (dev, i, 1); if (mii_status != 0xffff && mii_status != 0x0000) { np->phy_addr = i; phy_found++; } } if (!phy_found) { printk (KERN_ERR "%s: No MII PHY found!\n", dev->name); return -ENODEV; } return 0;}intparse_eeprom (struct net_device *dev){ int i, j; long ioaddr = dev->base_addr; u8 sromdata[256]; u8 *psib; u32 crc; PSROM_t psrom = (PSROM_t) sromdata; struct netdev_private *np = dev->priv; int cid, next; /* Read eeprom */ for (i = 0; i < 128; i++) { ((u16 *) sromdata)[i] = le16_to_cpu (read_eeprom (ioaddr, i)); } /* Check CRC */ crc = ~get_crc (sromdata, 256 - 4); if (psrom->crc != crc) { printk (KERN_ERR "%s: EEPROM data CRC error.\n", dev->name); return -1; } /* Set MAC address */ for (i = 0; i < 6; i++) dev->dev_addr[i] = psrom->mac_addr[i]; /* Parse Software Infomation Block */ i = 0x30; psib = (u8 *) sromdata; do { cid = psib[i++]; next = psib[i++]; if ((cid == 0 && next == 0) || (cid == 0xff && next == 0xff)) { printk (KERN_ERR "Cell data error\n"); return -1; } switch (cid) { case 0: /* Format version */ break; case 1: /* End of cell */ return 0; case 2: /* Duplex Polarity */ np->duplex_polarity = psib[i]; writeb (readb (ioaddr + PhyCtrl) | psib[i], ioaddr + PhyCtrl); break; case 3: /* Wake Polarity */ np->wake_polarity = psib[i]; break; case 9: /* Adapter description */ j = (next - i > 255) ? 255 : next - i; memcpy (np->name, &(psib[i]), j); break; case 4: case 5: case 6: case 7: case 8: /* Reversed */ break; default: /* Unknown cell */ return -1; } i = next; } while (1); return 0;}static intrio_open (struct net_device *dev){ struct netdev_private *np = dev->priv; long ioaddr = dev->base_addr; int i; i = request_irq (dev->irq, &rio_interrupt, SA_SHIRQ, dev->name, dev); if (i) return i; /* DebugCtrl bit 4, 5, 9 must set */ writel (readl (ioaddr + DebugCtrl) | 0x0230, ioaddr + DebugCtrl); /* Jumbo frame */ if (np->jumbo != 0) writew (MAX_JUMBO+14, ioaddr + MaxFrameSize); alloc_list (dev); /* Get station address */ for (i = 0; i < 6; i++) writeb (dev->dev_addr[i], ioaddr + StationAddr0 + i); set_multicast (dev); if (np->coalesce) { writel (np->rx_coalesce | np->rx_timeout << 16, ioaddr + RxDMAIntCtrl); } /* Set RIO to poll every N*320nsec. */ writeb (0xff, ioaddr + RxDMAPollPeriod); writeb (0xff, ioaddr + TxDMAPollPeriod); netif_start_queue (dev); writel (StatsEnable | RxEnable | TxEnable, ioaddr + MACCtrl); /* VLAN supported */ if (np->vlan) { /* priority field in RxDMAIntCtrl */ writel (readl(ioaddr + RxDMAIntCtrl) | 0x7 << 10, ioaddr + RxDMAIntCtrl); /* VLANId */ writew (np->vlan, ioaddr + VLANId); /* Length/Type should be 0x8100 */ writel (0x8100 << 16 | np->vlan, ioaddr + VLANTag); /* Enable AutoVLANuntagging, but disable AutoVLANtagging. VLAN information tagged by TFC' VID, CFI fields. */ writel (readl (ioaddr + MACCtrl) | AutoVLANuntagging, ioaddr + MACCtrl); } /* Enable default interrupts */ EnableInt (); /* clear statistics */ get_stats (dev); return 0;}static voidtx_timeout (struct net_device *dev){ struct netdev_private *np = dev->priv; long ioaddr = dev->base_addr; printk (KERN_INFO "%s: Tx timed out (%4.4x), is buffer full?\n", dev->name, readl (ioaddr + TxStatus)); /* Free used tx skbuffs */ for (; np->cur_tx - np->old_tx > 0; np->old_tx++) { int entry = np->old_tx % TX_RING_SIZE; struct sk_buff *skb; if (!(np->tx_ring[entry].status & TFDDone)) break; skb = np->tx_skbuff[entry]; pci_unmap_single (np->pdev, np->tx_ring[entry].fraginfo, skb->len, PCI_DMA_TODEVICE); dev_kfree_skb_irq (skb); np->tx_skbuff[entry] = 0; } dev->if_port = 0; dev->trans_start = jiffies; np->stats.tx_errors++; /* If the ring is no longer full, clear tx_full and call netif_wake_queue() */ if (np->tx_full && np->cur_tx - np->old_tx < TX_QUEUE_LEN - 1) { np->tx_full = 0; netif_wake_queue (dev); }} /* allocate and initialize Tx and Rx descriptors */static voidalloc_list (struct net_device *dev){ struct netdev_private *np = dev->priv; int i; np->tx_full = 0; np->cur_rx = np->cur_tx = 0; np->old_rx = np->old_tx = 0; np->rx_buf_sz = (dev->mtu <= 1500 ? PACKET_SIZE : dev->mtu + 32); /* Initialize Tx descriptors, TFDListPtr leaves in start_xmit(). */ for (i = 0; i < TX_RING_SIZE; i++) { np->tx_skbuff[i] = 0; np->tx_ring[i].status = cpu_to_le64 (TFDDone); np->tx_ring[i].next_desc = cpu_to_le64 (np->tx_ring_dma + ((i+1)%TX_RING_SIZE) * sizeof (struct netdev_desc)); } /* Initialize Rx descriptors */ for (i = 0; i < RX_RING_SIZE; i++) { np->rx_ring[i].next_desc = cpu_to_le64 (np->rx_ring_dma + ((i + 1) % RX_RING_SIZE) * sizeof (struct netdev_desc)); np->rx_ring[i].status = 0; np->rx_ring[i].fraginfo = 0; np->rx_skbuff[i] = 0; } /* Allocate the rx buffers */ for (i = 0; i < RX_RING_SIZE; i++) { /* Allocated fixed size of skbuff */ struct sk_buff *skb = dev_alloc_skb (np->rx_buf_sz); np->rx_skbuff[i] = skb; if (skb == NULL) { printk (KERN_ERR "%s: alloc_list: allocate Rx buffer error! ", dev->name); break; } skb->dev = dev; /* Mark as being used by this device. */ skb_reserve (skb, 2); /* 16 byte align the IP header. */ /* Rubicon now supports 40 bits of addressing space. */ np->rx_ring[i].fraginfo = cpu_to_le64 (pci_map_single (np->pdev, skb->tail, np->rx_buf_sz, PCI_DMA_FROMDEVICE)); np->rx_ring[i].fraginfo |= cpu_to_le64 (np->rx_buf_sz) << 48; } /* Set RFDListPtr */ writel (cpu_to_le32 (np->rx_ring_dma), dev->base_addr + RFDListPtr0); writel (0, dev->base_addr + RFDListPtr1); return;}static intstart_xmit (struct sk_buff *skb, struct net_device *dev){ struct netdev_private *np = dev->priv; struct netdev_desc *txdesc; unsigned entry; u32 ioaddr; int tx_shift; unsigned long flags; ioaddr = dev->base_addr; entry = np->cur_tx % TX_RING_SIZE; np->tx_skbuff[entry] = skb; txdesc = &np->tx_ring[entry]; /* Set TFDDone to avoid TxDMA gather this descriptor */ txdesc->status = cpu_to_le64 (TFDDone); txdesc->status |= cpu_to_le64 (entry | WordAlignDisable | (1 << FragCountShift));#if 0 if (skb->ip_summed == CHECKSUM_HW) { txdesc->status |= cpu_to_le64 (TCPChecksumEnable | UDPChecksumEnable | IPChecksumEnable); }#endif if (np->vlan) { txdesc->status |= cpu_to_le64 (VLANTagInsert) | (cpu_to_le64 (np->vlan) << 32) | (cpu_to_le64 (skb->priority) << 45); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -