1 /* de2104x.c: A Linux PCI Ethernet driver for Intel/Digital 21040/1 chips. */
3 Copyright 2001,2003 Jeff Garzik <jgarzik@pobox.com>
5 Copyright 1994, 1995 Digital Equipment Corporation. [de4x5.c]
6 Written/copyright 1994-2001 by Donald Becker. [tulip.c]
8 This software may be used and distributed according to the terms of
9 the GNU General Public License (GPL), incorporated herein by reference.
10 Drivers based on or derived from this code fall under the GPL and must
11 retain the authorship, copyright and license notice. This file is not
12 a complete program and may only be used when the entire operating
13 system is licensed under the GPL.
15 See the file COPYING in this distribution for more information.
17 TODO, in rough priority order:
18 * Support forcing media type with a module parameter,
19 like dl2k.c/sundance.c
20 * Constants (module parms?) for Rx work limit
21 * Complete reset on PciErr
22 * Jumbo frames / dev->change_mtu
23 * Adjust Rx FIFO threshold and Max Rx DMA burst on Rx FIFO error
24 * Adjust Tx FIFO threshold and Max Tx DMA burst on Tx FIFO error
25 * Implement Tx software interrupt mitigation via
30 #define DRV_NAME "de2104x"
31 #define DRV_VERSION "0.7"
32 #define DRV_RELDATE "Mar 17, 2004"
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/init.h>
39 #include <linux/pci.h>
40 #include <linux/delay.h>
41 #include <linux/ethtool.h>
42 #include <linux/compiler.h>
43 #include <linux/rtnetlink.h>
44 #include <linux/crc32.h>
48 #include <asm/uaccess.h>
49 #include <asm/unaligned.h>
51 /* These identify the driver base version and may not be removed. */
52 static char version[] =
53 KERN_INFO DRV_NAME " PCI Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";
55 MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
56 MODULE_DESCRIPTION("Intel/Digital 21040/1 series PCI Ethernet driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(DRV_VERSION);
60 static int debug = -1;
61 module_param (debug, int, 0);
62 MODULE_PARM_DESC (debug, "de2104x bitmapped message enable number");
64 /* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
65 #if defined(__alpha__) || defined(__arm__) || defined(__hppa__) \
66 || defined(CONFIG_SPARC) || defined(__ia64__) \
67 || defined(__sh__) || defined(__mips__)
68 static int rx_copybreak = 1518;
70 static int rx_copybreak = 100;
72 module_param (rx_copybreak, int, 0);
73 MODULE_PARM_DESC (rx_copybreak, "de2104x Breakpoint at which Rx packets are copied");
75 #define PFX DRV_NAME ": "
77 #define DE_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
85 /* Descriptor skip length in 32 bit longwords. */
86 #ifndef CONFIG_DE2104X_DSL
89 #define DSL CONFIG_DE2104X_DSL
92 #define DE_RX_RING_SIZE 64
93 #define DE_TX_RING_SIZE 64
94 #define DE_RING_BYTES \
95 ((sizeof(struct de_desc) * DE_RX_RING_SIZE) + \
96 (sizeof(struct de_desc) * DE_TX_RING_SIZE))
97 #define NEXT_TX(N) (((N) + 1) & (DE_TX_RING_SIZE - 1))
98 #define NEXT_RX(N) (((N) + 1) & (DE_RX_RING_SIZE - 1))
99 #define TX_BUFFS_AVAIL(CP) \
100 (((CP)->tx_tail <= (CP)->tx_head) ? \
101 (CP)->tx_tail + (DE_TX_RING_SIZE - 1) - (CP)->tx_head : \
102 (CP)->tx_tail - (CP)->tx_head - 1)
104 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
107 #define DE_SETUP_SKB ((struct sk_buff *) 1)
108 #define DE_DUMMY_SKB ((struct sk_buff *) 2)
109 #define DE_SETUP_FRAME_WORDS 96
110 #define DE_EEPROM_WORDS 256
111 #define DE_EEPROM_SIZE (DE_EEPROM_WORDS * sizeof(u16))
112 #define DE_MAX_MEDIA 5
114 #define DE_MEDIA_TP_AUTO 0
115 #define DE_MEDIA_BNC 1
116 #define DE_MEDIA_AUI 2
117 #define DE_MEDIA_TP 3
118 #define DE_MEDIA_TP_FD 4
119 #define DE_MEDIA_INVALID DE_MAX_MEDIA
120 #define DE_MEDIA_FIRST 0
121 #define DE_MEDIA_LAST (DE_MAX_MEDIA - 1)
122 #define DE_AUI_BNC (SUPPORTED_AUI | SUPPORTED_BNC)
124 #define DE_TIMER_LINK (60 * HZ)
125 #define DE_TIMER_NO_LINK (5 * HZ)
127 #define DE_NUM_REGS 16
128 #define DE_REGS_SIZE (DE_NUM_REGS * sizeof(u32))
129 #define DE_REGS_VER 1
131 /* Time in jiffies before concluding the transmitter is hung. */
132 #define TX_TIMEOUT (6*HZ)
134 /* This is a mysterious value that can be written to CSR11 in the 21040 (only)
135 to support a pre-NWay full-duplex signaling mechanism using short frames.
136 No one knows what it should be, but if left at its default value some
137 10base2(!) packets trigger a full-duplex-request interrupt. */
138 #define FULL_DUPLEX_MAGIC 0x6969
161 CacheAlign16 = 0x00008000,
162 BurstLen4 = 0x00000400,
163 DescSkipLen = (DSL << 2),
166 NormalTxPoll = (1 << 0),
167 NormalRxPoll = (1 << 0),
169 /* Tx/Rx descriptor status bits */
172 RxErrLong = (1 << 7),
174 RxErrFIFO = (1 << 0),
175 RxErrRunt = (1 << 11),
176 RxErrFrame = (1 << 14),
178 FirstFrag = (1 << 29),
179 LastFrag = (1 << 30),
181 TxFIFOUnder = (1 << 1),
182 TxLinkFail = (1 << 2) | (1 << 10) | (1 << 11),
185 TxJabber = (1 << 14),
186 SetupFrame = (1 << 27),
197 TxState = (1 << 22) | (1 << 21) | (1 << 20),
198 RxState = (1 << 19) | (1 << 18) | (1 << 17),
199 LinkFail = (1 << 12),
201 RxStopped = (1 << 8),
202 TxStopped = (1 << 1),
205 TxEnable = (1 << 13),
207 RxTx = TxEnable | RxEnable,
208 FullDuplex = (1 << 9),
209 AcceptAllMulticast = (1 << 7),
210 AcceptAllPhys = (1 << 6),
212 MacModeClear = (1<<12) | (1<<11) | (1<<10) | (1<<8) | (1<<3) |
213 RxTx | BOCnt | AcceptAllPhys | AcceptAllMulticast,
216 EE_SHIFT_CLK = 0x02, /* EEPROM shift clock. */
217 EE_CS = 0x01, /* EEPROM chip select. */
218 EE_DATA_WRITE = 0x04, /* Data from the Tulip to EEPROM. */
221 EE_DATA_READ = 0x08, /* Data from the EEPROM chip. */
222 EE_ENB = (0x4800 | EE_CS),
224 /* The EEPROM commands include the alway-set leading bit. */
228 RxMissedOver = (1 << 16),
229 RxMissedMask = 0xffff,
231 /* SROM-related bits */
233 MediaBlockMask = 0x3f,
234 MediaCustomCSRs = (1 << 6),
237 PM_Sleep = (1 << 31),
238 PM_Snooze = (1 << 30),
239 PM_Mask = PM_Sleep | PM_Snooze,
242 NWayState = (1 << 14) | (1 << 13) | (1 << 12),
243 NWayRestart = (1 << 12),
244 NonselPortActive = (1 << 9),
245 LinkFailStatus = (1 << 2),
246 NetCxnErr = (1 << 1),
249 static const u32 de_intr_mask =
250 IntrOK | IntrErr | RxIntr | RxEmpty | TxIntr | TxEmpty |
251 LinkPass | LinkFail | PciErr;
254 * Set the programmable burst length to 4 longwords for all:
255 * DMA errors result without these values. Cache align 16 long.
257 static const u32 de_bus_mode = CacheAlign16 | BurstLen4 | DescSkipLen;
259 struct de_srom_media_block {
264 } __attribute__((packed));
266 struct de_srom_info_leaf {
270 } __attribute__((packed));
283 u16 type; /* DE_MEDIA_xxx */
300 struct net_device *dev;
303 struct de_desc *rx_ring;
304 struct de_desc *tx_ring;
305 struct ring_info tx_skb[DE_TX_RING_SIZE];
306 struct ring_info rx_skb[DE_RX_RING_SIZE];
312 struct net_device_stats net_stats;
314 struct pci_dev *pdev;
316 u16 setup_frame[DE_SETUP_FRAME_WORDS];
321 struct media_info media[DE_MAX_MEDIA];
322 struct timer_list media_timer;
326 unsigned de21040 : 1;
327 unsigned media_lock : 1;
331 static void de_set_rx_mode (struct net_device *dev);
332 static void de_tx (struct de_private *de);
333 static void de_clean_rings (struct de_private *de);
334 static void de_media_interrupt (struct de_private *de, u32 status);
335 static void de21040_media_timer (unsigned long data);
336 static void de21041_media_timer (unsigned long data);
337 static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media);
340 static struct pci_device_id de_pci_tbl[] = {
341 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP,
342 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
343 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_PLUS,
344 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
347 MODULE_DEVICE_TABLE(pci, de_pci_tbl);
349 static const char * const media_name[DE_MAX_MEDIA] = {
357 /* 21040 transceiver register settings:
358 * TP AUTO(unused), BNC(unused), AUI, TP, TP FD*/
359 static u16 t21040_csr13[] = { 0, 0, 0x8F09, 0x8F01, 0x8F01, };
360 static u16 t21040_csr14[] = { 0, 0, 0x0705, 0xFFFF, 0xFFFD, };
361 static u16 t21040_csr15[] = { 0, 0, 0x0006, 0x0000, 0x0000, };
363 /* 21041 transceiver register settings: TP AUTO, BNC, AUI, TP, TP FD*/
364 static u16 t21041_csr13[] = { 0xEF01, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
365 static u16 t21041_csr14[] = { 0xFFFF, 0xF7FD, 0xF7FD, 0x6F3F, 0x6F3D, };
366 static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
369 #define dr32(reg) readl(de->regs + (reg))
370 #define dw32(reg,val) writel((val), de->regs + (reg))
373 static void de_rx_err_acct (struct de_private *de, unsigned rx_tail,
376 if (netif_msg_rx_err (de))
378 "%s: rx err, slot %d status 0x%x len %d\n",
379 de->dev->name, rx_tail, status, len);
381 if ((status & 0x38000300) != 0x0300) {
382 /* Ingore earlier buffers. */
383 if ((status & 0xffff) != 0x7fff) {
384 if (netif_msg_rx_err(de))
385 printk(KERN_WARNING "%s: Oversized Ethernet frame "
386 "spanned multiple buffers, status %8.8x!\n",
387 de->dev->name, status);
388 de->net_stats.rx_length_errors++;
390 } else if (status & RxError) {
391 /* There was a fatal error. */
392 de->net_stats.rx_errors++; /* end of a packet.*/
393 if (status & 0x0890) de->net_stats.rx_length_errors++;
394 if (status & RxErrCRC) de->net_stats.rx_crc_errors++;
395 if (status & RxErrFIFO) de->net_stats.rx_fifo_errors++;
399 static void de_rx (struct de_private *de)
401 unsigned rx_tail = de->rx_tail;
402 unsigned rx_work = DE_RX_RING_SIZE;
409 struct sk_buff *skb, *copy_skb;
410 unsigned copying_skb, buflen;
412 skb = de->rx_skb[rx_tail].skb;
415 status = le32_to_cpu(de->rx_ring[rx_tail].opts1);
416 if (status & DescOwn)
419 len = ((status >> 16) & 0x7ff) - 4;
420 mapping = de->rx_skb[rx_tail].mapping;
422 if (unlikely(drop)) {
423 de->net_stats.rx_dropped++;
427 if (unlikely((status & 0x38008300) != 0x0300)) {
428 de_rx_err_acct(de, rx_tail, status, len);
432 copying_skb = (len <= rx_copybreak);
434 if (unlikely(netif_msg_rx_status(de)))
435 printk(KERN_DEBUG "%s: rx slot %d status 0x%x len %d copying? %d\n",
436 de->dev->name, rx_tail, status, len,
439 buflen = copying_skb ? (len + RX_OFFSET) : de->rx_buf_sz;
440 copy_skb = dev_alloc_skb (buflen);
441 if (unlikely(!copy_skb)) {
442 de->net_stats.rx_dropped++;
449 pci_unmap_single(de->pdev, mapping,
450 buflen, PCI_DMA_FROMDEVICE);
454 de->rx_skb[rx_tail].mapping =
455 pci_map_single(de->pdev, copy_skb->data,
456 buflen, PCI_DMA_FROMDEVICE);
457 de->rx_skb[rx_tail].skb = copy_skb;
459 pci_dma_sync_single_for_cpu(de->pdev, mapping, len, PCI_DMA_FROMDEVICE);
460 skb_reserve(copy_skb, RX_OFFSET);
461 skb_copy_from_linear_data(skb, skb_put(copy_skb, len),
463 pci_dma_sync_single_for_device(de->pdev, mapping, len, PCI_DMA_FROMDEVICE);
465 /* We'll reuse the original ring buffer. */
469 skb->protocol = eth_type_trans (skb, de->dev);
471 de->net_stats.rx_packets++;
472 de->net_stats.rx_bytes += skb->len;
474 if (rc == NET_RX_DROP)
478 if (rx_tail == (DE_RX_RING_SIZE - 1))
479 de->rx_ring[rx_tail].opts2 =
480 cpu_to_le32(RingEnd | de->rx_buf_sz);
482 de->rx_ring[rx_tail].opts2 = cpu_to_le32(de->rx_buf_sz);
483 de->rx_ring[rx_tail].addr1 = cpu_to_le32(mapping);
485 de->rx_ring[rx_tail].opts1 = cpu_to_le32(DescOwn);
486 rx_tail = NEXT_RX(rx_tail);
490 printk(KERN_WARNING "%s: rx work limit reached\n", de->dev->name);
492 de->rx_tail = rx_tail;
495 static irqreturn_t de_interrupt (int irq, void *dev_instance)
497 struct net_device *dev = dev_instance;
498 struct de_private *de = netdev_priv(dev);
501 status = dr32(MacStatus);
502 if ((!(status & (IntrOK|IntrErr))) || (status == 0xFFFF))
505 if (netif_msg_intr(de))
506 printk(KERN_DEBUG "%s: intr, status %08x mode %08x desc %u/%u/%u\n",
507 dev->name, status, dr32(MacMode), de->rx_tail, de->tx_head, de->tx_tail);
509 dw32(MacStatus, status);
511 if (status & (RxIntr | RxEmpty)) {
513 if (status & RxEmpty)
514 dw32(RxPoll, NormalRxPoll);
517 spin_lock(&de->lock);
519 if (status & (TxIntr | TxEmpty))
522 if (status & (LinkPass | LinkFail))
523 de_media_interrupt(de, status);
525 spin_unlock(&de->lock);
527 if (status & PciErr) {
530 pci_read_config_word(de->pdev, PCI_STATUS, &pci_status);
531 pci_write_config_word(de->pdev, PCI_STATUS, pci_status);
532 printk(KERN_ERR "%s: PCI bus error, status=%08x, PCI status=%04x\n",
533 dev->name, status, pci_status);
539 static void de_tx (struct de_private *de)
541 unsigned tx_head = de->tx_head;
542 unsigned tx_tail = de->tx_tail;
544 while (tx_tail != tx_head) {
549 status = le32_to_cpu(de->tx_ring[tx_tail].opts1);
550 if (status & DescOwn)
553 skb = de->tx_skb[tx_tail].skb;
555 if (unlikely(skb == DE_DUMMY_SKB))
558 if (unlikely(skb == DE_SETUP_SKB)) {
559 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
560 sizeof(de->setup_frame), PCI_DMA_TODEVICE);
564 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
565 skb->len, PCI_DMA_TODEVICE);
567 if (status & LastFrag) {
568 if (status & TxError) {
569 if (netif_msg_tx_err(de))
570 printk(KERN_DEBUG "%s: tx err, status 0x%x\n",
571 de->dev->name, status);
572 de->net_stats.tx_errors++;
574 de->net_stats.tx_window_errors++;
575 if (status & TxMaxCol)
576 de->net_stats.tx_aborted_errors++;
577 if (status & TxLinkFail)
578 de->net_stats.tx_carrier_errors++;
579 if (status & TxFIFOUnder)
580 de->net_stats.tx_fifo_errors++;
582 de->net_stats.tx_packets++;
583 de->net_stats.tx_bytes += skb->len;
584 if (netif_msg_tx_done(de))
585 printk(KERN_DEBUG "%s: tx done, slot %d\n", de->dev->name, tx_tail);
587 dev_kfree_skb_irq(skb);
591 de->tx_skb[tx_tail].skb = NULL;
593 tx_tail = NEXT_TX(tx_tail);
596 de->tx_tail = tx_tail;
598 if (netif_queue_stopped(de->dev) && (TX_BUFFS_AVAIL(de) > (DE_TX_RING_SIZE / 4)))
599 netif_wake_queue(de->dev);
602 static int de_start_xmit (struct sk_buff *skb, struct net_device *dev)
604 struct de_private *de = netdev_priv(dev);
605 unsigned int entry, tx_free;
606 u32 mapping, len, flags = FirstFrag | LastFrag;
609 spin_lock_irq(&de->lock);
611 tx_free = TX_BUFFS_AVAIL(de);
613 netif_stop_queue(dev);
614 spin_unlock_irq(&de->lock);
615 return NETDEV_TX_BUSY;
621 txd = &de->tx_ring[entry];
624 mapping = pci_map_single(de->pdev, skb->data, len, PCI_DMA_TODEVICE);
625 if (entry == (DE_TX_RING_SIZE - 1))
627 if (!tx_free || (tx_free == (DE_TX_RING_SIZE / 2)))
630 txd->opts2 = cpu_to_le32(flags);
631 txd->addr1 = cpu_to_le32(mapping);
633 de->tx_skb[entry].skb = skb;
634 de->tx_skb[entry].mapping = mapping;
637 txd->opts1 = cpu_to_le32(DescOwn);
640 de->tx_head = NEXT_TX(entry);
641 if (netif_msg_tx_queued(de))
642 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
643 dev->name, entry, skb->len);
646 netif_stop_queue(dev);
648 spin_unlock_irq(&de->lock);
650 /* Trigger an immediate transmit demand. */
651 dw32(TxPoll, NormalTxPoll);
652 dev->trans_start = jiffies;
657 /* Set or clear the multicast filter for this adaptor.
658 Note that we only use exclusion around actually queueing the
659 new frame, not around filling de->setup_frame. This is non-deterministic
660 when re-entered but still correct. */
663 #define set_bit_le(i,p) do { ((char *)(p))[(i)/8] |= (1<<((i)%8)); } while(0)
665 static void build_setup_frame_hash(u16 *setup_frm, struct net_device *dev)
667 struct de_private *de = netdev_priv(dev);
669 struct dev_mc_list *mclist;
673 memset(hash_table, 0, sizeof(hash_table));
674 set_bit_le(255, hash_table); /* Broadcast entry */
675 /* This should work on big-endian machines as well. */
676 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
677 i++, mclist = mclist->next) {
678 int index = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff;
680 set_bit_le(index, hash_table);
682 for (i = 0; i < 32; i++) {
683 *setup_frm++ = hash_table[i];
684 *setup_frm++ = hash_table[i];
686 setup_frm = &de->setup_frame[13*6];
689 /* Fill the final entry with our physical address. */
690 eaddrs = (u16 *)dev->dev_addr;
691 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
692 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
693 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
696 static void build_setup_frame_perfect(u16 *setup_frm, struct net_device *dev)
698 struct de_private *de = netdev_priv(dev);
699 struct dev_mc_list *mclist;
703 /* We have <= 14 addresses so we can use the wonderful
704 16 address perfect filtering of the Tulip. */
705 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
706 i++, mclist = mclist->next) {
707 eaddrs = (u16 *)mclist->dmi_addr;
708 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
709 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
710 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
712 /* Fill the unused entries with the broadcast address. */
713 memset(setup_frm, 0xff, (15-i)*12);
714 setup_frm = &de->setup_frame[15*6];
716 /* Fill the final entry with our physical address. */
717 eaddrs = (u16 *)dev->dev_addr;
718 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
719 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
720 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
724 static void __de_set_rx_mode (struct net_device *dev)
726 struct de_private *de = netdev_priv(dev);
731 struct de_desc *dummy_txd = NULL;
733 macmode = dr32(MacMode) & ~(AcceptAllMulticast | AcceptAllPhys);
735 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
736 macmode |= AcceptAllMulticast | AcceptAllPhys;
740 if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
741 /* Too many to filter well -- accept all multicasts. */
742 macmode |= AcceptAllMulticast;
746 /* Note that only the low-address shortword of setup_frame is valid!
747 The values are doubled for big-endian architectures. */
748 if (dev->mc_count > 14) /* Must use a multicast hash table. */
749 build_setup_frame_hash (de->setup_frame, dev);
751 build_setup_frame_perfect (de->setup_frame, dev);
754 * Now add this frame to the Tx list.
759 /* Avoid a chip errata by prefixing a dummy entry. */
761 de->tx_skb[entry].skb = DE_DUMMY_SKB;
763 dummy_txd = &de->tx_ring[entry];
764 dummy_txd->opts2 = (entry == (DE_TX_RING_SIZE - 1)) ?
765 cpu_to_le32(RingEnd) : 0;
766 dummy_txd->addr1 = 0;
768 /* Must set DescOwned later to avoid race with chip */
770 entry = NEXT_TX(entry);
773 de->tx_skb[entry].skb = DE_SETUP_SKB;
774 de->tx_skb[entry].mapping = mapping =
775 pci_map_single (de->pdev, de->setup_frame,
776 sizeof (de->setup_frame), PCI_DMA_TODEVICE);
778 /* Put the setup frame on the Tx list. */
779 txd = &de->tx_ring[entry];
780 if (entry == (DE_TX_RING_SIZE - 1))
781 txd->opts2 = cpu_to_le32(SetupFrame | RingEnd | sizeof (de->setup_frame));
783 txd->opts2 = cpu_to_le32(SetupFrame | sizeof (de->setup_frame));
784 txd->addr1 = cpu_to_le32(mapping);
787 txd->opts1 = cpu_to_le32(DescOwn);
791 dummy_txd->opts1 = cpu_to_le32(DescOwn);
795 de->tx_head = NEXT_TX(entry);
797 if (TX_BUFFS_AVAIL(de) == 0)
798 netif_stop_queue(dev);
800 /* Trigger an immediate transmit demand. */
801 dw32(TxPoll, NormalTxPoll);
804 if (macmode != dr32(MacMode))
805 dw32(MacMode, macmode);
808 static void de_set_rx_mode (struct net_device *dev)
811 struct de_private *de = netdev_priv(dev);
813 spin_lock_irqsave (&de->lock, flags);
814 __de_set_rx_mode(dev);
815 spin_unlock_irqrestore (&de->lock, flags);
818 static inline void de_rx_missed(struct de_private *de, u32 rx_missed)
820 if (unlikely(rx_missed & RxMissedOver))
821 de->net_stats.rx_missed_errors += RxMissedMask;
823 de->net_stats.rx_missed_errors += (rx_missed & RxMissedMask);
826 static void __de_get_stats(struct de_private *de)
828 u32 tmp = dr32(RxMissed); /* self-clearing */
830 de_rx_missed(de, tmp);
833 static struct net_device_stats *de_get_stats(struct net_device *dev)
835 struct de_private *de = netdev_priv(dev);
837 /* The chip only need report frame silently dropped. */
838 spin_lock_irq(&de->lock);
839 if (netif_running(dev) && netif_device_present(dev))
841 spin_unlock_irq(&de->lock);
843 return &de->net_stats;
846 static inline int de_is_running (struct de_private *de)
848 return (dr32(MacStatus) & (RxState | TxState)) ? 1 : 0;
851 static void de_stop_rxtx (struct de_private *de)
854 unsigned int i = 1300/100;
856 macmode = dr32(MacMode);
857 if (macmode & RxTx) {
858 dw32(MacMode, macmode & ~RxTx);
862 /* wait until in-flight frame completes.
863 * Max time @ 10BT: 1500*8b/10Mbps == 1200us (+ 100us margin)
864 * Typically expect this loop to end in < 50 us on 100BT.
867 if (!de_is_running(de))
872 printk(KERN_WARNING "%s: timeout expired stopping DMA\n", de->dev->name);
875 static inline void de_start_rxtx (struct de_private *de)
879 macmode = dr32(MacMode);
880 if ((macmode & RxTx) != RxTx) {
881 dw32(MacMode, macmode | RxTx);
886 static void de_stop_hw (struct de_private *de)
894 dw32(MacStatus, dr32(MacStatus));
899 de->tx_head = de->tx_tail = 0;
902 static void de_link_up(struct de_private *de)
904 if (!netif_carrier_ok(de->dev)) {
905 netif_carrier_on(de->dev);
906 if (netif_msg_link(de))
907 printk(KERN_INFO "%s: link up, media %s\n",
908 de->dev->name, media_name[de->media_type]);
912 static void de_link_down(struct de_private *de)
914 if (netif_carrier_ok(de->dev)) {
915 netif_carrier_off(de->dev);
916 if (netif_msg_link(de))
917 printk(KERN_INFO "%s: link down\n", de->dev->name);
921 static void de_set_media (struct de_private *de)
923 unsigned media = de->media_type;
924 u32 macmode = dr32(MacMode);
926 if (de_is_running(de))
927 printk(KERN_WARNING "%s: chip is running while changing media!\n", de->dev->name);
930 dw32(CSR11, FULL_DUPLEX_MAGIC);
931 dw32(CSR13, 0); /* Reset phy */
932 dw32(CSR14, de->media[media].csr14);
933 dw32(CSR15, de->media[media].csr15);
934 dw32(CSR13, de->media[media].csr13);
936 /* must delay 10ms before writing to other registers,
941 if (media == DE_MEDIA_TP_FD)
942 macmode |= FullDuplex;
944 macmode &= ~FullDuplex;
946 if (netif_msg_link(de)) {
947 printk(KERN_INFO "%s: set link %s\n"
948 KERN_INFO "%s: mode 0x%x, sia 0x%x,0x%x,0x%x,0x%x\n"
949 KERN_INFO "%s: set mode 0x%x, set sia 0x%x,0x%x,0x%x\n",
950 de->dev->name, media_name[media],
951 de->dev->name, dr32(MacMode), dr32(SIAStatus),
952 dr32(CSR13), dr32(CSR14), dr32(CSR15),
953 de->dev->name, macmode, de->media[media].csr13,
954 de->media[media].csr14, de->media[media].csr15);
956 if (macmode != dr32(MacMode))
957 dw32(MacMode, macmode);
960 static void de_next_media (struct de_private *de, u32 *media,
961 unsigned int n_media)
965 for (i = 0; i < n_media; i++) {
966 if (de_ok_to_advertise(de, media[i])) {
967 de->media_type = media[i];
973 static void de21040_media_timer (unsigned long data)
975 struct de_private *de = (struct de_private *) data;
976 struct net_device *dev = de->dev;
977 u32 status = dr32(SIAStatus);
978 unsigned int carrier;
981 carrier = (status & NetCxnErr) ? 0 : 1;
984 if (de->media_type != DE_MEDIA_AUI && (status & LinkFailStatus))
987 de->media_timer.expires = jiffies + DE_TIMER_LINK;
988 add_timer(&de->media_timer);
989 if (!netif_carrier_ok(dev))
992 if (netif_msg_timer(de))
993 printk(KERN_INFO "%s: %s link ok, status %x\n",
994 dev->name, media_name[de->media_type],
1004 if (de->media_type == DE_MEDIA_AUI) {
1005 u32 next_state = DE_MEDIA_TP;
1006 de_next_media(de, &next_state, 1);
1008 u32 next_state = DE_MEDIA_AUI;
1009 de_next_media(de, &next_state, 1);
1012 spin_lock_irqsave(&de->lock, flags);
1014 spin_unlock_irqrestore(&de->lock, flags);
1019 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1020 add_timer(&de->media_timer);
1022 if (netif_msg_timer(de))
1023 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1024 dev->name, media_name[de->media_type], status);
1027 static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media)
1029 switch (new_media) {
1030 case DE_MEDIA_TP_AUTO:
1031 if (!(de->media_advertise & ADVERTISED_Autoneg))
1033 if (!(de->media_advertise & (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full)))
1037 if (!(de->media_advertise & ADVERTISED_BNC))
1041 if (!(de->media_advertise & ADVERTISED_AUI))
1045 if (!(de->media_advertise & ADVERTISED_10baseT_Half))
1048 case DE_MEDIA_TP_FD:
1049 if (!(de->media_advertise & ADVERTISED_10baseT_Full))
1057 static void de21041_media_timer (unsigned long data)
1059 struct de_private *de = (struct de_private *) data;
1060 struct net_device *dev = de->dev;
1061 u32 status = dr32(SIAStatus);
1062 unsigned int carrier;
1063 unsigned long flags;
1065 carrier = (status & NetCxnErr) ? 0 : 1;
1068 if ((de->media_type == DE_MEDIA_TP_AUTO ||
1069 de->media_type == DE_MEDIA_TP ||
1070 de->media_type == DE_MEDIA_TP_FD) &&
1071 (status & LinkFailStatus))
1074 de->media_timer.expires = jiffies + DE_TIMER_LINK;
1075 add_timer(&de->media_timer);
1076 if (!netif_carrier_ok(dev))
1079 if (netif_msg_timer(de))
1080 printk(KERN_INFO "%s: %s link ok, mode %x status %x\n",
1081 dev->name, media_name[de->media_type],
1082 dr32(MacMode), status);
1088 /* if media type locked, don't switch media */
1092 /* if activity detected, use that as hint for new media type */
1093 if (status & NonselPortActive) {
1094 unsigned int have_media = 1;
1096 /* if AUI/BNC selected, then activity is on TP port */
1097 if (de->media_type == DE_MEDIA_AUI ||
1098 de->media_type == DE_MEDIA_BNC) {
1099 if (de_ok_to_advertise(de, DE_MEDIA_TP_AUTO))
1100 de->media_type = DE_MEDIA_TP_AUTO;
1105 /* TP selected. If there is only TP and BNC, then it's BNC */
1106 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_BNC) &&
1107 de_ok_to_advertise(de, DE_MEDIA_BNC))
1108 de->media_type = DE_MEDIA_BNC;
1110 /* TP selected. If there is only TP and AUI, then it's AUI */
1111 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_AUI) &&
1112 de_ok_to_advertise(de, DE_MEDIA_AUI))
1113 de->media_type = DE_MEDIA_AUI;
1115 /* otherwise, ignore the hint */
1124 * Absent or ambiguous activity hint, move to next advertised
1125 * media state. If de->media_type is left unchanged, this
1126 * simply resets the PHY and reloads the current media settings.
1128 if (de->media_type == DE_MEDIA_AUI) {
1129 u32 next_states[] = { DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1130 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1131 } else if (de->media_type == DE_MEDIA_BNC) {
1132 u32 next_states[] = { DE_MEDIA_TP_AUTO, DE_MEDIA_AUI };
1133 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1135 u32 next_states[] = { DE_MEDIA_AUI, DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1136 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1140 spin_lock_irqsave(&de->lock, flags);
1142 spin_unlock_irqrestore(&de->lock, flags);
1147 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1148 add_timer(&de->media_timer);
1150 if (netif_msg_timer(de))
1151 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1152 dev->name, media_name[de->media_type], status);
1155 static void de_media_interrupt (struct de_private *de, u32 status)
1157 if (status & LinkPass) {
1159 mod_timer(&de->media_timer, jiffies + DE_TIMER_LINK);
1163 BUG_ON(!(status & LinkFail));
1165 if (netif_carrier_ok(de->dev)) {
1167 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1171 static int de_reset_mac (struct de_private *de)
1176 * Reset MAC. de4x5.c and tulip.c examined for "advice"
1180 if (dr32(BusMode) == 0xffffffff)
1183 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
1184 dw32 (BusMode, CmdReset);
1187 dw32 (BusMode, de_bus_mode);
1190 for (tmp = 0; tmp < 5; tmp++) {
1197 status = dr32(MacStatus);
1198 if (status & (RxState | TxState))
1200 if (status == 0xffffffff)
1205 static void de_adapter_wake (struct de_private *de)
1212 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1213 if (pmctl & PM_Mask) {
1215 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1217 /* de4x5.c delays, so we do too */
1222 static void de_adapter_sleep (struct de_private *de)
1229 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1231 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1234 static int de_init_hw (struct de_private *de)
1236 struct net_device *dev = de->dev;
1240 de_adapter_wake(de);
1242 macmode = dr32(MacMode) & ~MacModeClear;
1244 rc = de_reset_mac(de);
1248 de_set_media(de); /* reset phy */
1250 dw32(RxRingAddr, de->ring_dma);
1251 dw32(TxRingAddr, de->ring_dma + (sizeof(struct de_desc) * DE_RX_RING_SIZE));
1253 dw32(MacMode, RxTx | macmode);
1255 dr32(RxMissed); /* self-clearing */
1257 dw32(IntrMask, de_intr_mask);
1259 de_set_rx_mode(dev);
1264 static int de_refill_rx (struct de_private *de)
1268 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1269 struct sk_buff *skb;
1271 skb = dev_alloc_skb(de->rx_buf_sz);
1277 de->rx_skb[i].mapping = pci_map_single(de->pdev,
1278 skb->data, de->rx_buf_sz, PCI_DMA_FROMDEVICE);
1279 de->rx_skb[i].skb = skb;
1281 de->rx_ring[i].opts1 = cpu_to_le32(DescOwn);
1282 if (i == (DE_RX_RING_SIZE - 1))
1283 de->rx_ring[i].opts2 =
1284 cpu_to_le32(RingEnd | de->rx_buf_sz);
1286 de->rx_ring[i].opts2 = cpu_to_le32(de->rx_buf_sz);
1287 de->rx_ring[i].addr1 = cpu_to_le32(de->rx_skb[i].mapping);
1288 de->rx_ring[i].addr2 = 0;
1298 static int de_init_rings (struct de_private *de)
1300 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1301 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1304 de->tx_head = de->tx_tail = 0;
1306 return de_refill_rx (de);
1309 static int de_alloc_rings (struct de_private *de)
1311 de->rx_ring = pci_alloc_consistent(de->pdev, DE_RING_BYTES, &de->ring_dma);
1314 de->tx_ring = &de->rx_ring[DE_RX_RING_SIZE];
1315 return de_init_rings(de);
1318 static void de_clean_rings (struct de_private *de)
1322 memset(de->rx_ring, 0, sizeof(struct de_desc) * DE_RX_RING_SIZE);
1323 de->rx_ring[DE_RX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1325 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1326 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1329 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1330 if (de->rx_skb[i].skb) {
1331 pci_unmap_single(de->pdev, de->rx_skb[i].mapping,
1332 de->rx_buf_sz, PCI_DMA_FROMDEVICE);
1333 dev_kfree_skb(de->rx_skb[i].skb);
1337 for (i = 0; i < DE_TX_RING_SIZE; i++) {
1338 struct sk_buff *skb = de->tx_skb[i].skb;
1339 if ((skb) && (skb != DE_DUMMY_SKB)) {
1340 if (skb != DE_SETUP_SKB) {
1341 de->net_stats.tx_dropped++;
1342 pci_unmap_single(de->pdev,
1343 de->tx_skb[i].mapping,
1344 skb->len, PCI_DMA_TODEVICE);
1347 pci_unmap_single(de->pdev,
1348 de->tx_skb[i].mapping,
1349 sizeof(de->setup_frame),
1355 memset(&de->rx_skb, 0, sizeof(struct ring_info) * DE_RX_RING_SIZE);
1356 memset(&de->tx_skb, 0, sizeof(struct ring_info) * DE_TX_RING_SIZE);
1359 static void de_free_rings (struct de_private *de)
1362 pci_free_consistent(de->pdev, DE_RING_BYTES, de->rx_ring, de->ring_dma);
1367 static int de_open (struct net_device *dev)
1369 struct de_private *de = netdev_priv(dev);
1372 if (netif_msg_ifup(de))
1373 printk(KERN_DEBUG "%s: enabling interface\n", dev->name);
1375 de->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1377 rc = de_alloc_rings(de);
1379 printk(KERN_ERR "%s: ring allocation failure, err=%d\n",
1386 rc = request_irq(dev->irq, de_interrupt, IRQF_SHARED, dev->name, dev);
1388 printk(KERN_ERR "%s: IRQ %d request failure, err=%d\n",
1389 dev->name, dev->irq, rc);
1393 rc = de_init_hw(de);
1395 printk(KERN_ERR "%s: h/w init failure, err=%d\n",
1397 goto err_out_free_irq;
1400 netif_start_queue(dev);
1401 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1406 free_irq(dev->irq, dev);
1412 static int de_close (struct net_device *dev)
1414 struct de_private *de = netdev_priv(dev);
1415 unsigned long flags;
1417 if (netif_msg_ifdown(de))
1418 printk(KERN_DEBUG "%s: disabling interface\n", dev->name);
1420 del_timer_sync(&de->media_timer);
1422 spin_lock_irqsave(&de->lock, flags);
1424 netif_stop_queue(dev);
1425 netif_carrier_off(dev);
1426 spin_unlock_irqrestore(&de->lock, flags);
1428 free_irq(dev->irq, dev);
1431 de_adapter_sleep(de);
1435 static void de_tx_timeout (struct net_device *dev)
1437 struct de_private *de = netdev_priv(dev);
1439 printk(KERN_DEBUG "%s: NIC status %08x mode %08x sia %08x desc %u/%u/%u\n",
1440 dev->name, dr32(MacStatus), dr32(MacMode), dr32(SIAStatus),
1441 de->rx_tail, de->tx_head, de->tx_tail);
1443 del_timer_sync(&de->media_timer);
1445 disable_irq(dev->irq);
1446 spin_lock_irq(&de->lock);
1449 netif_stop_queue(dev);
1450 netif_carrier_off(dev);
1452 spin_unlock_irq(&de->lock);
1453 enable_irq(dev->irq);
1455 /* Update the error counts. */
1458 synchronize_irq(dev->irq);
1465 netif_wake_queue(dev);
1468 static void __de_get_regs(struct de_private *de, u8 *buf)
1471 u32 *rbuf = (u32 *)buf;
1474 for (i = 0; i < DE_NUM_REGS; i++)
1475 rbuf[i] = dr32(i * 8);
1477 /* handle self-clearing RxMissed counter, CSR8 */
1478 de_rx_missed(de, rbuf[8]);
1481 static int __de_get_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1483 ecmd->supported = de->media_supported;
1484 ecmd->transceiver = XCVR_INTERNAL;
1485 ecmd->phy_address = 0;
1486 ecmd->advertising = de->media_advertise;
1488 switch (de->media_type) {
1490 ecmd->port = PORT_AUI;
1494 ecmd->port = PORT_BNC;
1498 ecmd->port = PORT_TP;
1499 ecmd->speed = SPEED_10;
1503 if (dr32(MacMode) & FullDuplex)
1504 ecmd->duplex = DUPLEX_FULL;
1506 ecmd->duplex = DUPLEX_HALF;
1509 ecmd->autoneg = AUTONEG_DISABLE;
1511 ecmd->autoneg = AUTONEG_ENABLE;
1513 /* ignore maxtxpkt, maxrxpkt for now */
1518 static int __de_set_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1521 unsigned int media_lock;
1523 if (ecmd->speed != SPEED_10 && ecmd->speed != 5 && ecmd->speed != 2)
1525 if (de->de21040 && ecmd->speed == 2)
1527 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1529 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI && ecmd->port != PORT_BNC)
1531 if (de->de21040 && ecmd->port == PORT_BNC)
1533 if (ecmd->transceiver != XCVR_INTERNAL)
1535 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1537 if (ecmd->advertising & ~de->media_supported)
1539 if (ecmd->autoneg == AUTONEG_ENABLE &&
1540 (!(ecmd->advertising & ADVERTISED_Autoneg)))
1543 switch (ecmd->port) {
1545 new_media = DE_MEDIA_AUI;
1546 if (!(ecmd->advertising & ADVERTISED_AUI))
1550 new_media = DE_MEDIA_BNC;
1551 if (!(ecmd->advertising & ADVERTISED_BNC))
1555 if (ecmd->autoneg == AUTONEG_ENABLE)
1556 new_media = DE_MEDIA_TP_AUTO;
1557 else if (ecmd->duplex == DUPLEX_FULL)
1558 new_media = DE_MEDIA_TP_FD;
1560 new_media = DE_MEDIA_TP;
1561 if (!(ecmd->advertising & ADVERTISED_TP))
1563 if (!(ecmd->advertising & (ADVERTISED_10baseT_Full | ADVERTISED_10baseT_Half)))
1568 media_lock = (ecmd->autoneg == AUTONEG_ENABLE) ? 0 : 1;
1570 if ((new_media == de->media_type) &&
1571 (media_lock == de->media_lock) &&
1572 (ecmd->advertising == de->media_advertise))
1573 return 0; /* nothing to change */
1578 de->media_type = new_media;
1579 de->media_lock = media_lock;
1580 de->media_advertise = ecmd->advertising;
1586 static void de_get_drvinfo (struct net_device *dev,struct ethtool_drvinfo *info)
1588 struct de_private *de = netdev_priv(dev);
1590 strcpy (info->driver, DRV_NAME);
1591 strcpy (info->version, DRV_VERSION);
1592 strcpy (info->bus_info, pci_name(de->pdev));
1593 info->eedump_len = DE_EEPROM_SIZE;
1596 static int de_get_regs_len(struct net_device *dev)
1598 return DE_REGS_SIZE;
1601 static int de_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1603 struct de_private *de = netdev_priv(dev);
1606 spin_lock_irq(&de->lock);
1607 rc = __de_get_settings(de, ecmd);
1608 spin_unlock_irq(&de->lock);
1613 static int de_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1615 struct de_private *de = netdev_priv(dev);
1618 spin_lock_irq(&de->lock);
1619 rc = __de_set_settings(de, ecmd);
1620 spin_unlock_irq(&de->lock);
1625 static u32 de_get_msglevel(struct net_device *dev)
1627 struct de_private *de = netdev_priv(dev);
1629 return de->msg_enable;
1632 static void de_set_msglevel(struct net_device *dev, u32 msglvl)
1634 struct de_private *de = netdev_priv(dev);
1636 de->msg_enable = msglvl;
1639 static int de_get_eeprom(struct net_device *dev,
1640 struct ethtool_eeprom *eeprom, u8 *data)
1642 struct de_private *de = netdev_priv(dev);
1646 if ((eeprom->offset != 0) || (eeprom->magic != 0) ||
1647 (eeprom->len != DE_EEPROM_SIZE))
1649 memcpy(data, de->ee_data, eeprom->len);
1654 static int de_nway_reset(struct net_device *dev)
1656 struct de_private *de = netdev_priv(dev);
1659 if (de->media_type != DE_MEDIA_TP_AUTO)
1661 if (netif_carrier_ok(de->dev))
1664 status = dr32(SIAStatus);
1665 dw32(SIAStatus, (status & ~NWayState) | NWayRestart);
1666 if (netif_msg_link(de))
1667 printk(KERN_INFO "%s: link nway restart, status %x,%x\n",
1668 de->dev->name, status, dr32(SIAStatus));
1672 static void de_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1675 struct de_private *de = netdev_priv(dev);
1677 regs->version = (DE_REGS_VER << 2) | de->de21040;
1679 spin_lock_irq(&de->lock);
1680 __de_get_regs(de, data);
1681 spin_unlock_irq(&de->lock);
1684 static const struct ethtool_ops de_ethtool_ops = {
1685 .get_link = ethtool_op_get_link,
1686 .get_drvinfo = de_get_drvinfo,
1687 .get_regs_len = de_get_regs_len,
1688 .get_settings = de_get_settings,
1689 .set_settings = de_set_settings,
1690 .get_msglevel = de_get_msglevel,
1691 .set_msglevel = de_set_msglevel,
1692 .get_eeprom = de_get_eeprom,
1693 .nway_reset = de_nway_reset,
1694 .get_regs = de_get_regs,
1697 static void __devinit de21040_get_mac_address (struct de_private *de)
1701 dw32 (ROMCmd, 0); /* Reset the pointer with a dummy write. */
1704 for (i = 0; i < 6; i++) {
1705 int value, boguscnt = 100000;
1707 value = dr32(ROMCmd);
1708 } while (value < 0 && --boguscnt > 0);
1709 de->dev->dev_addr[i] = value;
1712 printk(KERN_WARNING PFX "timeout reading 21040 MAC address byte %u\n", i);
1716 static void __devinit de21040_get_media_info(struct de_private *de)
1720 de->media_type = DE_MEDIA_TP;
1721 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full |
1722 SUPPORTED_10baseT_Half | SUPPORTED_AUI;
1723 de->media_advertise = de->media_supported;
1725 for (i = 0; i < DE_MAX_MEDIA; i++) {
1729 case DE_MEDIA_TP_FD:
1730 de->media[i].type = i;
1731 de->media[i].csr13 = t21040_csr13[i];
1732 de->media[i].csr14 = t21040_csr14[i];
1733 de->media[i].csr15 = t21040_csr15[i];
1736 de->media[i].type = DE_MEDIA_INVALID;
1742 /* Note: this routine returns extra data bits for size detection. */
1743 static unsigned __devinit tulip_read_eeprom(void __iomem *regs, int location, int addr_len)
1746 unsigned retval = 0;
1747 void __iomem *ee_addr = regs + ROMCmd;
1748 int read_cmd = location | (EE_READ_CMD << addr_len);
1750 writel(EE_ENB & ~EE_CS, ee_addr);
1751 writel(EE_ENB, ee_addr);
1753 /* Shift the read command bits out. */
1754 for (i = 4 + addr_len; i >= 0; i--) {
1755 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1756 writel(EE_ENB | dataval, ee_addr);
1758 writel(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1760 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1762 writel(EE_ENB, ee_addr);
1765 for (i = 16; i > 0; i--) {
1766 writel(EE_ENB | EE_SHIFT_CLK, ee_addr);
1768 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1769 writel(EE_ENB, ee_addr);
1773 /* Terminate the EEPROM access. */
1774 writel(EE_ENB & ~EE_CS, ee_addr);
1778 static void __devinit de21041_get_srom_info (struct de_private *de)
1780 unsigned i, sa_offset = 0, ofs;
1781 u8 ee_data[DE_EEPROM_SIZE + 6] = {};
1782 unsigned ee_addr_size = tulip_read_eeprom(de->regs, 0xff, 8) & 0x40000 ? 8 : 6;
1783 struct de_srom_info_leaf *il;
1786 /* download entire eeprom */
1787 for (i = 0; i < DE_EEPROM_WORDS; i++)
1788 ((__le16 *)ee_data)[i] =
1789 cpu_to_le16(tulip_read_eeprom(de->regs, i, ee_addr_size));
1791 /* DEC now has a specification but early board makers
1792 just put the address in the first EEPROM locations. */
1793 /* This does memcmp(eedata, eedata+16, 8) */
1795 #ifndef CONFIG_MIPS_COBALT
1797 for (i = 0; i < 8; i ++)
1798 if (ee_data[i] != ee_data[16+i])
1803 /* store MAC address */
1804 for (i = 0; i < 6; i ++)
1805 de->dev->dev_addr[i] = ee_data[i + sa_offset];
1807 /* get offset of controller 0 info leaf. ignore 2nd byte. */
1808 ofs = ee_data[SROMC0InfoLeaf];
1809 if (ofs >= (sizeof(ee_data) - sizeof(struct de_srom_info_leaf) - sizeof(struct de_srom_media_block)))
1812 /* get pointer to info leaf */
1813 il = (struct de_srom_info_leaf *) &ee_data[ofs];
1815 /* paranoia checks */
1816 if (il->n_blocks == 0)
1818 if ((sizeof(ee_data) - ofs) <
1819 (sizeof(struct de_srom_info_leaf) + (sizeof(struct de_srom_media_block) * il->n_blocks)))
1822 /* get default media type */
1823 switch (get_unaligned(&il->default_media)) {
1824 case 0x0001: de->media_type = DE_MEDIA_BNC; break;
1825 case 0x0002: de->media_type = DE_MEDIA_AUI; break;
1826 case 0x0204: de->media_type = DE_MEDIA_TP_FD; break;
1827 default: de->media_type = DE_MEDIA_TP_AUTO; break;
1830 if (netif_msg_probe(de))
1831 printk(KERN_INFO "de%d: SROM leaf offset %u, default media %s\n",
1833 media_name[de->media_type]);
1835 /* init SIA register values to defaults */
1836 for (i = 0; i < DE_MAX_MEDIA; i++) {
1837 de->media[i].type = DE_MEDIA_INVALID;
1838 de->media[i].csr13 = 0xffff;
1839 de->media[i].csr14 = 0xffff;
1840 de->media[i].csr15 = 0xffff;
1843 /* parse media blocks to see what medias are supported,
1844 * and if any custom CSR values are provided
1846 bufp = ((void *)il) + sizeof(*il);
1847 for (i = 0; i < il->n_blocks; i++) {
1848 struct de_srom_media_block *ib = bufp;
1851 /* index based on media type in media block */
1852 switch(ib->opts & MediaBlockMask) {
1853 case 0: /* 10baseT */
1854 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half
1855 | SUPPORTED_Autoneg;
1857 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1860 de->media_supported |= SUPPORTED_BNC;
1864 de->media_supported |= SUPPORTED_AUI;
1867 case 4: /* 10baseT-FD */
1868 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full
1869 | SUPPORTED_Autoneg;
1870 idx = DE_MEDIA_TP_FD;
1871 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1877 de->media[idx].type = idx;
1879 if (netif_msg_probe(de))
1880 printk(KERN_INFO "de%d: media block #%u: %s",
1882 media_name[de->media[idx].type]);
1884 bufp += sizeof (ib->opts);
1886 if (ib->opts & MediaCustomCSRs) {
1887 de->media[idx].csr13 = get_unaligned(&ib->csr13);
1888 de->media[idx].csr14 = get_unaligned(&ib->csr14);
1889 de->media[idx].csr15 = get_unaligned(&ib->csr15);
1890 bufp += sizeof(ib->csr13) + sizeof(ib->csr14) +
1893 if (netif_msg_probe(de))
1894 printk(" (%x,%x,%x)\n",
1895 de->media[idx].csr13,
1896 de->media[idx].csr14,
1897 de->media[idx].csr15);
1899 } else if (netif_msg_probe(de))
1902 if (bufp > ((void *)&ee_data[DE_EEPROM_SIZE - 3]))
1906 de->media_advertise = de->media_supported;
1909 /* fill in defaults, for cases where custom CSRs not used */
1910 for (i = 0; i < DE_MAX_MEDIA; i++) {
1911 if (de->media[i].csr13 == 0xffff)
1912 de->media[i].csr13 = t21041_csr13[i];
1913 if (de->media[i].csr14 == 0xffff)
1914 de->media[i].csr14 = t21041_csr14[i];
1915 if (de->media[i].csr15 == 0xffff)
1916 de->media[i].csr15 = t21041_csr15[i];
1919 de->ee_data = kmemdup(&ee_data[0], DE_EEPROM_SIZE, GFP_KERNEL);
1924 /* for error cases, it's ok to assume we support all these */
1925 for (i = 0; i < DE_MAX_MEDIA; i++)
1926 de->media[i].type = i;
1927 de->media_supported =
1928 SUPPORTED_10baseT_Half |
1929 SUPPORTED_10baseT_Full |
1937 static const struct net_device_ops de_netdev_ops = {
1938 .ndo_open = de_open,
1939 .ndo_stop = de_close,
1940 .ndo_set_multicast_list = de_set_rx_mode,
1941 .ndo_start_xmit = de_start_xmit,
1942 .ndo_get_stats = de_get_stats,
1943 .ndo_tx_timeout = de_tx_timeout,
1944 .ndo_change_mtu = eth_change_mtu,
1945 .ndo_set_mac_address = eth_mac_addr,
1946 .ndo_validate_addr = eth_validate_addr,
1949 static int __devinit de_init_one (struct pci_dev *pdev,
1950 const struct pci_device_id *ent)
1952 struct net_device *dev;
1953 struct de_private *de;
1956 unsigned long pciaddr;
1957 static int board_idx = -1;
1963 printk("%s", version);
1966 /* allocate a new ethernet device structure, and fill in defaults */
1967 dev = alloc_etherdev(sizeof(struct de_private));
1971 dev->netdev_ops = &de_netdev_ops;
1972 SET_NETDEV_DEV(dev, &pdev->dev);
1973 dev->ethtool_ops = &de_ethtool_ops;
1974 dev->watchdog_timeo = TX_TIMEOUT;
1976 de = netdev_priv(dev);
1977 de->de21040 = ent->driver_data == 0 ? 1 : 0;
1980 de->msg_enable = (debug < 0 ? DE_DEF_MSG_ENABLE : debug);
1981 de->board_idx = board_idx;
1982 spin_lock_init (&de->lock);
1983 init_timer(&de->media_timer);
1985 de->media_timer.function = de21040_media_timer;
1987 de->media_timer.function = de21041_media_timer;
1988 de->media_timer.data = (unsigned long) de;
1990 netif_carrier_off(dev);
1991 netif_stop_queue(dev);
1993 /* wake up device, assign resources */
1994 rc = pci_enable_device(pdev);
1998 /* reserve PCI resources to ensure driver atomicity */
1999 rc = pci_request_regions(pdev, DRV_NAME);
2001 goto err_out_disable;
2003 /* check for invalid IRQ value */
2004 if (pdev->irq < 2) {
2006 printk(KERN_ERR PFX "invalid irq (%d) for pci dev %s\n",
2007 pdev->irq, pci_name(pdev));
2011 dev->irq = pdev->irq;
2013 /* obtain and check validity of PCI I/O address */
2014 pciaddr = pci_resource_start(pdev, 1);
2017 printk(KERN_ERR PFX "no MMIO resource for pci dev %s\n",
2021 if (pci_resource_len(pdev, 1) < DE_REGS_SIZE) {
2023 printk(KERN_ERR PFX "MMIO resource (%llx) too small on pci dev %s\n",
2024 (unsigned long long)pci_resource_len(pdev, 1), pci_name(pdev));
2028 /* remap CSR registers */
2029 regs = ioremap_nocache(pciaddr, DE_REGS_SIZE);
2032 printk(KERN_ERR PFX "Cannot map PCI MMIO (%llx@%lx) on pci dev %s\n",
2033 (unsigned long long)pci_resource_len(pdev, 1),
2034 pciaddr, pci_name(pdev));
2037 dev->base_addr = (unsigned long) regs;
2040 de_adapter_wake(de);
2042 /* make sure hardware is not running */
2043 rc = de_reset_mac(de);
2045 printk(KERN_ERR PFX "Cannot reset MAC, pci dev %s\n",
2050 /* get MAC address, initialize default media type and
2051 * get list of supported media
2054 de21040_get_mac_address(de);
2055 de21040_get_media_info(de);
2057 de21041_get_srom_info(de);
2060 /* register new network interface with kernel */
2061 rc = register_netdev(dev);
2065 /* print info about board and interface just registered */
2066 printk (KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
2068 de->de21040 ? "21040" : "21041",
2073 pci_set_drvdata(pdev, dev);
2075 /* enable busmastering */
2076 pci_set_master(pdev);
2078 /* put adapter to sleep */
2079 de_adapter_sleep(de);
2087 pci_release_regions(pdev);
2089 pci_disable_device(pdev);
2095 static void __devexit de_remove_one (struct pci_dev *pdev)
2097 struct net_device *dev = pci_get_drvdata(pdev);
2098 struct de_private *de = netdev_priv(dev);
2101 unregister_netdev(dev);
2104 pci_release_regions(pdev);
2105 pci_disable_device(pdev);
2106 pci_set_drvdata(pdev, NULL);
2112 static int de_suspend (struct pci_dev *pdev, pm_message_t state)
2114 struct net_device *dev = pci_get_drvdata (pdev);
2115 struct de_private *de = netdev_priv(dev);
2118 if (netif_running (dev)) {
2119 del_timer_sync(&de->media_timer);
2121 disable_irq(dev->irq);
2122 spin_lock_irq(&de->lock);
2125 netif_stop_queue(dev);
2126 netif_device_detach(dev);
2127 netif_carrier_off(dev);
2129 spin_unlock_irq(&de->lock);
2130 enable_irq(dev->irq);
2132 /* Update the error counts. */
2135 synchronize_irq(dev->irq);
2138 de_adapter_sleep(de);
2139 pci_disable_device(pdev);
2141 netif_device_detach(dev);
2147 static int de_resume (struct pci_dev *pdev)
2149 struct net_device *dev = pci_get_drvdata (pdev);
2150 struct de_private *de = netdev_priv(dev);
2154 if (netif_device_present(dev))
2156 if (!netif_running(dev))
2158 if ((retval = pci_enable_device(pdev))) {
2159 printk (KERN_ERR "%s: pci_enable_device failed in resume\n",
2165 netif_device_attach(dev);
2171 #endif /* CONFIG_PM */
2173 static struct pci_driver de_driver = {
2175 .id_table = de_pci_tbl,
2176 .probe = de_init_one,
2177 .remove = __devexit_p(de_remove_one),
2179 .suspend = de_suspend,
2180 .resume = de_resume,
2184 static int __init de_init (void)
2187 printk("%s", version);
2189 return pci_register_driver(&de_driver);
2192 static void __exit de_exit (void)
2194 pci_unregister_driver (&de_driver);
2197 module_init(de_init);
2198 module_exit(de_exit);