pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / tulip / de2104x.c
blob77d9dd7ea34f647f77e04d1c30bc20257146de55
1 /* de2104x.c: A Linux PCI Ethernet driver for Intel/Digital 21040/1 chips. */
2 /*
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
26 Tx descriptor bit
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>
46 #include <asm/io.h>
47 #include <asm/irq.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;
69 #else
70 static int rx_copybreak = 100;
71 #endif
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 | \
78 NETIF_MSG_PROBE | \
79 NETIF_MSG_LINK | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
85 #define DE_RX_RING_SIZE 64
86 #define DE_TX_RING_SIZE 64
87 #define DE_RING_BYTES \
88 ((sizeof(struct de_desc) * DE_RX_RING_SIZE) + \
89 (sizeof(struct de_desc) * DE_TX_RING_SIZE))
90 #define NEXT_TX(N) (((N) + 1) & (DE_TX_RING_SIZE - 1))
91 #define NEXT_RX(N) (((N) + 1) & (DE_RX_RING_SIZE - 1))
92 #define TX_BUFFS_AVAIL(CP) \
93 (((CP)->tx_tail <= (CP)->tx_head) ? \
94 (CP)->tx_tail + (DE_TX_RING_SIZE - 1) - (CP)->tx_head : \
95 (CP)->tx_tail - (CP)->tx_head - 1)
97 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
98 #define RX_OFFSET 2
100 #define DE_SETUP_SKB ((struct sk_buff *) 1)
101 #define DE_DUMMY_SKB ((struct sk_buff *) 2)
102 #define DE_SETUP_FRAME_WORDS 96
103 #define DE_EEPROM_WORDS 256
104 #define DE_EEPROM_SIZE (DE_EEPROM_WORDS * sizeof(u16))
105 #define DE_MAX_MEDIA 5
107 #define DE_MEDIA_TP_AUTO 0
108 #define DE_MEDIA_BNC 1
109 #define DE_MEDIA_AUI 2
110 #define DE_MEDIA_TP 3
111 #define DE_MEDIA_TP_FD 4
112 #define DE_MEDIA_INVALID DE_MAX_MEDIA
113 #define DE_MEDIA_FIRST 0
114 #define DE_MEDIA_LAST (DE_MAX_MEDIA - 1)
115 #define DE_AUI_BNC (SUPPORTED_AUI | SUPPORTED_BNC)
117 #define DE_TIMER_LINK (60 * HZ)
118 #define DE_TIMER_NO_LINK (5 * HZ)
120 #define DE_NUM_REGS 16
121 #define DE_REGS_SIZE (DE_NUM_REGS * sizeof(u32))
122 #define DE_REGS_VER 1
124 /* Time in jiffies before concluding the transmitter is hung. */
125 #define TX_TIMEOUT (6*HZ)
127 #define DE_UNALIGNED_16(a) (u16)(get_unaligned((u16 *)(a)))
129 /* This is a mysterious value that can be written to CSR11 in the 21040 (only)
130 to support a pre-NWay full-duplex signaling mechanism using short frames.
131 No one knows what it should be, but if left at its default value some
132 10base2(!) packets trigger a full-duplex-request interrupt. */
133 #define FULL_DUPLEX_MAGIC 0x6969
135 enum {
136 /* NIC registers */
137 BusMode = 0x00,
138 TxPoll = 0x08,
139 RxPoll = 0x10,
140 RxRingAddr = 0x18,
141 TxRingAddr = 0x20,
142 MacStatus = 0x28,
143 MacMode = 0x30,
144 IntrMask = 0x38,
145 RxMissed = 0x40,
146 ROMCmd = 0x48,
147 CSR11 = 0x58,
148 SIAStatus = 0x60,
149 CSR13 = 0x68,
150 CSR14 = 0x70,
151 CSR15 = 0x78,
152 PCIPM = 0x40,
154 /* BusMode bits */
155 CmdReset = (1 << 0),
156 CacheAlign16 = 0x00008000,
157 BurstLen4 = 0x00000400,
159 /* Rx/TxPoll bits */
160 NormalTxPoll = (1 << 0),
161 NormalRxPoll = (1 << 0),
163 /* Tx/Rx descriptor status bits */
164 DescOwn = (1 << 31),
165 RxError = (1 << 15),
166 RxErrLong = (1 << 7),
167 RxErrCRC = (1 << 1),
168 RxErrFIFO = (1 << 0),
169 RxErrRunt = (1 << 11),
170 RxErrFrame = (1 << 14),
171 RingEnd = (1 << 25),
172 FirstFrag = (1 << 29),
173 LastFrag = (1 << 30),
174 TxError = (1 << 15),
175 TxFIFOUnder = (1 << 1),
176 TxLinkFail = (1 << 2) | (1 << 10) | (1 << 11),
177 TxMaxCol = (1 << 8),
178 TxOWC = (1 << 9),
179 TxJabber = (1 << 14),
180 SetupFrame = (1 << 27),
181 TxSwInt = (1 << 31),
183 /* MacStatus bits */
184 IntrOK = (1 << 16),
185 IntrErr = (1 << 15),
186 RxIntr = (1 << 6),
187 RxEmpty = (1 << 7),
188 TxIntr = (1 << 0),
189 TxEmpty = (1 << 2),
190 PciErr = (1 << 13),
191 TxState = (1 << 22) | (1 << 21) | (1 << 20),
192 RxState = (1 << 19) | (1 << 18) | (1 << 17),
193 LinkFail = (1 << 12),
194 LinkPass = (1 << 4),
195 RxStopped = (1 << 8),
196 TxStopped = (1 << 1),
198 /* MacMode bits */
199 TxEnable = (1 << 13),
200 RxEnable = (1 << 1),
201 RxTx = TxEnable | RxEnable,
202 FullDuplex = (1 << 9),
203 AcceptAllMulticast = (1 << 7),
204 AcceptAllPhys = (1 << 6),
205 BOCnt = (1 << 5),
206 MacModeClear = (1<<12) | (1<<11) | (1<<10) | (1<<8) | (1<<3) |
207 RxTx | BOCnt | AcceptAllPhys | AcceptAllMulticast,
209 /* ROMCmd bits */
210 EE_SHIFT_CLK = 0x02, /* EEPROM shift clock. */
211 EE_CS = 0x01, /* EEPROM chip select. */
212 EE_DATA_WRITE = 0x04, /* Data from the Tulip to EEPROM. */
213 EE_WRITE_0 = 0x01,
214 EE_WRITE_1 = 0x05,
215 EE_DATA_READ = 0x08, /* Data from the EEPROM chip. */
216 EE_ENB = (0x4800 | EE_CS),
218 /* The EEPROM commands include the alway-set leading bit. */
219 EE_READ_CMD = 6,
221 /* RxMissed bits */
222 RxMissedOver = (1 << 16),
223 RxMissedMask = 0xffff,
225 /* SROM-related bits */
226 SROMC0InfoLeaf = 27,
227 MediaBlockMask = 0x3f,
228 MediaCustomCSRs = (1 << 6),
230 /* PCIPM bits */
231 PM_Sleep = (1 << 31),
232 PM_Snooze = (1 << 30),
233 PM_Mask = PM_Sleep | PM_Snooze,
235 /* SIAStatus bits */
236 NWayState = (1 << 14) | (1 << 13) | (1 << 12),
237 NWayRestart = (1 << 12),
238 NonselPortActive = (1 << 9),
239 LinkFailStatus = (1 << 2),
240 NetCxnErr = (1 << 1),
243 static const u32 de_intr_mask =
244 IntrOK | IntrErr | RxIntr | RxEmpty | TxIntr | TxEmpty |
245 LinkPass | LinkFail | PciErr;
248 * Set the programmable burst length to 4 longwords for all:
249 * DMA errors result without these values. Cache align 16 long.
251 static const u32 de_bus_mode = CacheAlign16 | BurstLen4;
253 struct de_srom_media_block {
254 u8 opts;
255 u16 csr13;
256 u16 csr14;
257 u16 csr15;
258 } __attribute__((packed));
260 struct de_srom_info_leaf {
261 u16 default_media;
262 u8 n_blocks;
263 u8 unused;
264 } __attribute__((packed));
266 struct de_desc {
267 __le32 opts1;
268 __le32 opts2;
269 __le32 addr1;
270 __le32 addr2;
273 struct media_info {
274 u16 type; /* DE_MEDIA_xxx */
275 u16 csr13;
276 u16 csr14;
277 u16 csr15;
280 struct ring_info {
281 struct sk_buff *skb;
282 dma_addr_t mapping;
285 struct de_private {
286 unsigned tx_head;
287 unsigned tx_tail;
288 unsigned rx_tail;
290 void __iomem *regs;
291 struct net_device *dev;
292 spinlock_t lock;
294 struct de_desc *rx_ring;
295 struct de_desc *tx_ring;
296 struct ring_info tx_skb[DE_TX_RING_SIZE];
297 struct ring_info rx_skb[DE_RX_RING_SIZE];
298 unsigned rx_buf_sz;
299 dma_addr_t ring_dma;
301 u32 msg_enable;
303 struct net_device_stats net_stats;
305 struct pci_dev *pdev;
307 u16 setup_frame[DE_SETUP_FRAME_WORDS];
309 u32 media_type;
310 u32 media_supported;
311 u32 media_advertise;
312 struct media_info media[DE_MAX_MEDIA];
313 struct timer_list media_timer;
315 u8 *ee_data;
316 unsigned board_idx;
317 unsigned de21040 : 1;
318 unsigned media_lock : 1;
322 static void de_set_rx_mode (struct net_device *dev);
323 static void de_tx (struct de_private *de);
324 static void de_clean_rings (struct de_private *de);
325 static void de_media_interrupt (struct de_private *de, u32 status);
326 static void de21040_media_timer (unsigned long data);
327 static void de21041_media_timer (unsigned long data);
328 static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media);
331 static struct pci_device_id de_pci_tbl[] = {
332 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP,
333 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
334 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_PLUS,
335 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
336 { },
338 MODULE_DEVICE_TABLE(pci, de_pci_tbl);
340 static const char * const media_name[DE_MAX_MEDIA] = {
341 "10baseT auto",
342 "BNC",
343 "AUI",
344 "10baseT-HD",
345 "10baseT-FD"
348 /* 21040 transceiver register settings:
349 * TP AUTO(unused), BNC(unused), AUI, TP, TP FD*/
350 static u16 t21040_csr13[] = { 0, 0, 0x8F09, 0x8F01, 0x8F01, };
351 static u16 t21040_csr14[] = { 0, 0, 0x0705, 0xFFFF, 0xFFFD, };
352 static u16 t21040_csr15[] = { 0, 0, 0x0006, 0x0000, 0x0000, };
354 /* 21041 transceiver register settings: TP AUTO, BNC, AUI, TP, TP FD*/
355 static u16 t21041_csr13[] = { 0xEF01, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
356 static u16 t21041_csr14[] = { 0xFFFF, 0xF7FD, 0xF7FD, 0x6F3F, 0x6F3D, };
357 static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
360 #define dr32(reg) readl(de->regs + (reg))
361 #define dw32(reg,val) writel((val), de->regs + (reg))
364 static void de_rx_err_acct (struct de_private *de, unsigned rx_tail,
365 u32 status, u32 len)
367 if (netif_msg_rx_err (de))
368 printk (KERN_DEBUG
369 "%s: rx err, slot %d status 0x%x len %d\n",
370 de->dev->name, rx_tail, status, len);
372 if ((status & 0x38000300) != 0x0300) {
373 /* Ingore earlier buffers. */
374 if ((status & 0xffff) != 0x7fff) {
375 if (netif_msg_rx_err(de))
376 printk(KERN_WARNING "%s: Oversized Ethernet frame "
377 "spanned multiple buffers, status %8.8x!\n",
378 de->dev->name, status);
379 de->net_stats.rx_length_errors++;
381 } else if (status & RxError) {
382 /* There was a fatal error. */
383 de->net_stats.rx_errors++; /* end of a packet.*/
384 if (status & 0x0890) de->net_stats.rx_length_errors++;
385 if (status & RxErrCRC) de->net_stats.rx_crc_errors++;
386 if (status & RxErrFIFO) de->net_stats.rx_fifo_errors++;
390 static void de_rx (struct de_private *de)
392 unsigned rx_tail = de->rx_tail;
393 unsigned rx_work = DE_RX_RING_SIZE;
394 unsigned drop = 0;
395 int rc;
397 while (rx_work--) {
398 u32 status, len;
399 dma_addr_t mapping;
400 struct sk_buff *skb, *copy_skb;
401 unsigned copying_skb, buflen;
403 skb = de->rx_skb[rx_tail].skb;
404 BUG_ON(!skb);
405 rmb();
406 status = le32_to_cpu(de->rx_ring[rx_tail].opts1);
407 if (status & DescOwn)
408 break;
410 len = ((status >> 16) & 0x7ff) - 4;
411 mapping = de->rx_skb[rx_tail].mapping;
413 if (unlikely(drop)) {
414 de->net_stats.rx_dropped++;
415 goto rx_next;
418 if (unlikely((status & 0x38008300) != 0x0300)) {
419 de_rx_err_acct(de, rx_tail, status, len);
420 goto rx_next;
423 copying_skb = (len <= rx_copybreak);
425 if (unlikely(netif_msg_rx_status(de)))
426 printk(KERN_DEBUG "%s: rx slot %d status 0x%x len %d copying? %d\n",
427 de->dev->name, rx_tail, status, len,
428 copying_skb);
430 buflen = copying_skb ? (len + RX_OFFSET) : de->rx_buf_sz;
431 copy_skb = dev_alloc_skb (buflen);
432 if (unlikely(!copy_skb)) {
433 de->net_stats.rx_dropped++;
434 drop = 1;
435 rx_work = 100;
436 goto rx_next;
439 if (!copying_skb) {
440 pci_unmap_single(de->pdev, mapping,
441 buflen, PCI_DMA_FROMDEVICE);
442 skb_put(skb, len);
444 mapping =
445 de->rx_skb[rx_tail].mapping =
446 pci_map_single(de->pdev, copy_skb->data,
447 buflen, PCI_DMA_FROMDEVICE);
448 de->rx_skb[rx_tail].skb = copy_skb;
449 } else {
450 pci_dma_sync_single_for_cpu(de->pdev, mapping, len, PCI_DMA_FROMDEVICE);
451 skb_reserve(copy_skb, RX_OFFSET);
452 skb_copy_from_linear_data(skb, skb_put(copy_skb, len),
453 len);
454 pci_dma_sync_single_for_device(de->pdev, mapping, len, PCI_DMA_FROMDEVICE);
456 /* We'll reuse the original ring buffer. */
457 skb = copy_skb;
460 skb->protocol = eth_type_trans (skb, de->dev);
462 de->net_stats.rx_packets++;
463 de->net_stats.rx_bytes += skb->len;
464 de->dev->last_rx = jiffies;
465 rc = netif_rx (skb);
466 if (rc == NET_RX_DROP)
467 drop = 1;
469 rx_next:
470 de->rx_ring[rx_tail].opts1 = cpu_to_le32(DescOwn);
471 if (rx_tail == (DE_RX_RING_SIZE - 1))
472 de->rx_ring[rx_tail].opts2 =
473 cpu_to_le32(RingEnd | de->rx_buf_sz);
474 else
475 de->rx_ring[rx_tail].opts2 = cpu_to_le32(de->rx_buf_sz);
476 de->rx_ring[rx_tail].addr1 = cpu_to_le32(mapping);
477 rx_tail = NEXT_RX(rx_tail);
480 if (!rx_work)
481 printk(KERN_WARNING "%s: rx work limit reached\n", de->dev->name);
483 de->rx_tail = rx_tail;
486 static irqreturn_t de_interrupt (int irq, void *dev_instance)
488 struct net_device *dev = dev_instance;
489 struct de_private *de = dev->priv;
490 u32 status;
492 status = dr32(MacStatus);
493 if ((!(status & (IntrOK|IntrErr))) || (status == 0xFFFF))
494 return IRQ_NONE;
496 if (netif_msg_intr(de))
497 printk(KERN_DEBUG "%s: intr, status %08x mode %08x desc %u/%u/%u\n",
498 dev->name, status, dr32(MacMode), de->rx_tail, de->tx_head, de->tx_tail);
500 dw32(MacStatus, status);
502 if (status & (RxIntr | RxEmpty)) {
503 de_rx(de);
504 if (status & RxEmpty)
505 dw32(RxPoll, NormalRxPoll);
508 spin_lock(&de->lock);
510 if (status & (TxIntr | TxEmpty))
511 de_tx(de);
513 if (status & (LinkPass | LinkFail))
514 de_media_interrupt(de, status);
516 spin_unlock(&de->lock);
518 if (status & PciErr) {
519 u16 pci_status;
521 pci_read_config_word(de->pdev, PCI_STATUS, &pci_status);
522 pci_write_config_word(de->pdev, PCI_STATUS, pci_status);
523 printk(KERN_ERR "%s: PCI bus error, status=%08x, PCI status=%04x\n",
524 dev->name, status, pci_status);
527 return IRQ_HANDLED;
530 static void de_tx (struct de_private *de)
532 unsigned tx_head = de->tx_head;
533 unsigned tx_tail = de->tx_tail;
535 while (tx_tail != tx_head) {
536 struct sk_buff *skb;
537 u32 status;
539 rmb();
540 status = le32_to_cpu(de->tx_ring[tx_tail].opts1);
541 if (status & DescOwn)
542 break;
544 skb = de->tx_skb[tx_tail].skb;
545 BUG_ON(!skb);
546 if (unlikely(skb == DE_DUMMY_SKB))
547 goto next;
549 if (unlikely(skb == DE_SETUP_SKB)) {
550 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
551 sizeof(de->setup_frame), PCI_DMA_TODEVICE);
552 goto next;
555 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
556 skb->len, PCI_DMA_TODEVICE);
558 if (status & LastFrag) {
559 if (status & TxError) {
560 if (netif_msg_tx_err(de))
561 printk(KERN_DEBUG "%s: tx err, status 0x%x\n",
562 de->dev->name, status);
563 de->net_stats.tx_errors++;
564 if (status & TxOWC)
565 de->net_stats.tx_window_errors++;
566 if (status & TxMaxCol)
567 de->net_stats.tx_aborted_errors++;
568 if (status & TxLinkFail)
569 de->net_stats.tx_carrier_errors++;
570 if (status & TxFIFOUnder)
571 de->net_stats.tx_fifo_errors++;
572 } else {
573 de->net_stats.tx_packets++;
574 de->net_stats.tx_bytes += skb->len;
575 if (netif_msg_tx_done(de))
576 printk(KERN_DEBUG "%s: tx done, slot %d\n", de->dev->name, tx_tail);
578 dev_kfree_skb_irq(skb);
581 next:
582 de->tx_skb[tx_tail].skb = NULL;
584 tx_tail = NEXT_TX(tx_tail);
587 de->tx_tail = tx_tail;
589 if (netif_queue_stopped(de->dev) && (TX_BUFFS_AVAIL(de) > (DE_TX_RING_SIZE / 4)))
590 netif_wake_queue(de->dev);
593 static int de_start_xmit (struct sk_buff *skb, struct net_device *dev)
595 struct de_private *de = dev->priv;
596 unsigned int entry, tx_free;
597 u32 mapping, len, flags = FirstFrag | LastFrag;
598 struct de_desc *txd;
600 spin_lock_irq(&de->lock);
602 tx_free = TX_BUFFS_AVAIL(de);
603 if (tx_free == 0) {
604 netif_stop_queue(dev);
605 spin_unlock_irq(&de->lock);
606 return 1;
608 tx_free--;
610 entry = de->tx_head;
612 txd = &de->tx_ring[entry];
614 len = skb->len;
615 mapping = pci_map_single(de->pdev, skb->data, len, PCI_DMA_TODEVICE);
616 if (entry == (DE_TX_RING_SIZE - 1))
617 flags |= RingEnd;
618 if (!tx_free || (tx_free == (DE_TX_RING_SIZE / 2)))
619 flags |= TxSwInt;
620 flags |= len;
621 txd->opts2 = cpu_to_le32(flags);
622 txd->addr1 = cpu_to_le32(mapping);
624 de->tx_skb[entry].skb = skb;
625 de->tx_skb[entry].mapping = mapping;
626 wmb();
628 txd->opts1 = cpu_to_le32(DescOwn);
629 wmb();
631 de->tx_head = NEXT_TX(entry);
632 if (netif_msg_tx_queued(de))
633 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
634 dev->name, entry, skb->len);
636 if (tx_free == 0)
637 netif_stop_queue(dev);
639 spin_unlock_irq(&de->lock);
641 /* Trigger an immediate transmit demand. */
642 dw32(TxPoll, NormalTxPoll);
643 dev->trans_start = jiffies;
645 return 0;
648 /* Set or clear the multicast filter for this adaptor.
649 Note that we only use exclusion around actually queueing the
650 new frame, not around filling de->setup_frame. This is non-deterministic
651 when re-entered but still correct. */
653 #undef set_bit_le
654 #define set_bit_le(i,p) do { ((char *)(p))[(i)/8] |= (1<<((i)%8)); } while(0)
656 static void build_setup_frame_hash(u16 *setup_frm, struct net_device *dev)
658 struct de_private *de = dev->priv;
659 u16 hash_table[32];
660 struct dev_mc_list *mclist;
661 int i;
662 u16 *eaddrs;
664 memset(hash_table, 0, sizeof(hash_table));
665 set_bit_le(255, hash_table); /* Broadcast entry */
666 /* This should work on big-endian machines as well. */
667 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
668 i++, mclist = mclist->next) {
669 int index = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff;
671 set_bit_le(index, hash_table);
673 for (i = 0; i < 32; i++) {
674 *setup_frm++ = hash_table[i];
675 *setup_frm++ = hash_table[i];
677 setup_frm = &de->setup_frame[13*6];
680 /* Fill the final entry with our physical address. */
681 eaddrs = (u16 *)dev->dev_addr;
682 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
683 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
684 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
687 static void build_setup_frame_perfect(u16 *setup_frm, struct net_device *dev)
689 struct de_private *de = dev->priv;
690 struct dev_mc_list *mclist;
691 int i;
692 u16 *eaddrs;
694 /* We have <= 14 addresses so we can use the wonderful
695 16 address perfect filtering of the Tulip. */
696 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
697 i++, mclist = mclist->next) {
698 eaddrs = (u16 *)mclist->dmi_addr;
699 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
700 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
701 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
703 /* Fill the unused entries with the broadcast address. */
704 memset(setup_frm, 0xff, (15-i)*12);
705 setup_frm = &de->setup_frame[15*6];
707 /* Fill the final entry with our physical address. */
708 eaddrs = (u16 *)dev->dev_addr;
709 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
710 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
711 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
715 static void __de_set_rx_mode (struct net_device *dev)
717 struct de_private *de = dev->priv;
718 u32 macmode;
719 unsigned int entry;
720 u32 mapping;
721 struct de_desc *txd;
722 struct de_desc *dummy_txd = NULL;
724 macmode = dr32(MacMode) & ~(AcceptAllMulticast | AcceptAllPhys);
726 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
727 macmode |= AcceptAllMulticast | AcceptAllPhys;
728 goto out;
731 if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
732 /* Too many to filter well -- accept all multicasts. */
733 macmode |= AcceptAllMulticast;
734 goto out;
737 /* Note that only the low-address shortword of setup_frame is valid!
738 The values are doubled for big-endian architectures. */
739 if (dev->mc_count > 14) /* Must use a multicast hash table. */
740 build_setup_frame_hash (de->setup_frame, dev);
741 else
742 build_setup_frame_perfect (de->setup_frame, dev);
745 * Now add this frame to the Tx list.
748 entry = de->tx_head;
750 /* Avoid a chip errata by prefixing a dummy entry. */
751 if (entry != 0) {
752 de->tx_skb[entry].skb = DE_DUMMY_SKB;
754 dummy_txd = &de->tx_ring[entry];
755 dummy_txd->opts2 = (entry == (DE_TX_RING_SIZE - 1)) ?
756 cpu_to_le32(RingEnd) : 0;
757 dummy_txd->addr1 = 0;
759 /* Must set DescOwned later to avoid race with chip */
761 entry = NEXT_TX(entry);
764 de->tx_skb[entry].skb = DE_SETUP_SKB;
765 de->tx_skb[entry].mapping = mapping =
766 pci_map_single (de->pdev, de->setup_frame,
767 sizeof (de->setup_frame), PCI_DMA_TODEVICE);
769 /* Put the setup frame on the Tx list. */
770 txd = &de->tx_ring[entry];
771 if (entry == (DE_TX_RING_SIZE - 1))
772 txd->opts2 = cpu_to_le32(SetupFrame | RingEnd | sizeof (de->setup_frame));
773 else
774 txd->opts2 = cpu_to_le32(SetupFrame | sizeof (de->setup_frame));
775 txd->addr1 = cpu_to_le32(mapping);
776 wmb();
778 txd->opts1 = cpu_to_le32(DescOwn);
779 wmb();
781 if (dummy_txd) {
782 dummy_txd->opts1 = cpu_to_le32(DescOwn);
783 wmb();
786 de->tx_head = NEXT_TX(entry);
788 if (TX_BUFFS_AVAIL(de) == 0)
789 netif_stop_queue(dev);
791 /* Trigger an immediate transmit demand. */
792 dw32(TxPoll, NormalTxPoll);
794 out:
795 if (macmode != dr32(MacMode))
796 dw32(MacMode, macmode);
799 static void de_set_rx_mode (struct net_device *dev)
801 unsigned long flags;
802 struct de_private *de = dev->priv;
804 spin_lock_irqsave (&de->lock, flags);
805 __de_set_rx_mode(dev);
806 spin_unlock_irqrestore (&de->lock, flags);
809 static inline void de_rx_missed(struct de_private *de, u32 rx_missed)
811 if (unlikely(rx_missed & RxMissedOver))
812 de->net_stats.rx_missed_errors += RxMissedMask;
813 else
814 de->net_stats.rx_missed_errors += (rx_missed & RxMissedMask);
817 static void __de_get_stats(struct de_private *de)
819 u32 tmp = dr32(RxMissed); /* self-clearing */
821 de_rx_missed(de, tmp);
824 static struct net_device_stats *de_get_stats(struct net_device *dev)
826 struct de_private *de = dev->priv;
828 /* The chip only need report frame silently dropped. */
829 spin_lock_irq(&de->lock);
830 if (netif_running(dev) && netif_device_present(dev))
831 __de_get_stats(de);
832 spin_unlock_irq(&de->lock);
834 return &de->net_stats;
837 static inline int de_is_running (struct de_private *de)
839 return (dr32(MacStatus) & (RxState | TxState)) ? 1 : 0;
842 static void de_stop_rxtx (struct de_private *de)
844 u32 macmode;
845 unsigned int work = 1000;
847 macmode = dr32(MacMode);
848 if (macmode & RxTx) {
849 dw32(MacMode, macmode & ~RxTx);
850 dr32(MacMode);
853 while (--work > 0) {
854 if (!de_is_running(de))
855 return;
856 cpu_relax();
859 printk(KERN_WARNING "%s: timeout expired stopping DMA\n", de->dev->name);
862 static inline void de_start_rxtx (struct de_private *de)
864 u32 macmode;
866 macmode = dr32(MacMode);
867 if ((macmode & RxTx) != RxTx) {
868 dw32(MacMode, macmode | RxTx);
869 dr32(MacMode);
873 static void de_stop_hw (struct de_private *de)
876 udelay(5);
877 dw32(IntrMask, 0);
879 de_stop_rxtx(de);
881 dw32(MacStatus, dr32(MacStatus));
883 udelay(10);
885 de->rx_tail = 0;
886 de->tx_head = de->tx_tail = 0;
889 static void de_link_up(struct de_private *de)
891 if (!netif_carrier_ok(de->dev)) {
892 netif_carrier_on(de->dev);
893 if (netif_msg_link(de))
894 printk(KERN_INFO "%s: link up, media %s\n",
895 de->dev->name, media_name[de->media_type]);
899 static void de_link_down(struct de_private *de)
901 if (netif_carrier_ok(de->dev)) {
902 netif_carrier_off(de->dev);
903 if (netif_msg_link(de))
904 printk(KERN_INFO "%s: link down\n", de->dev->name);
908 static void de_set_media (struct de_private *de)
910 unsigned media = de->media_type;
911 u32 macmode = dr32(MacMode);
913 BUG_ON(de_is_running(de));
915 if (de->de21040)
916 dw32(CSR11, FULL_DUPLEX_MAGIC);
917 dw32(CSR13, 0); /* Reset phy */
918 dw32(CSR14, de->media[media].csr14);
919 dw32(CSR15, de->media[media].csr15);
920 dw32(CSR13, de->media[media].csr13);
922 /* must delay 10ms before writing to other registers,
923 * especially CSR6
925 mdelay(10);
927 if (media == DE_MEDIA_TP_FD)
928 macmode |= FullDuplex;
929 else
930 macmode &= ~FullDuplex;
932 if (netif_msg_link(de)) {
933 printk(KERN_INFO "%s: set link %s\n"
934 KERN_INFO "%s: mode 0x%x, sia 0x%x,0x%x,0x%x,0x%x\n"
935 KERN_INFO "%s: set mode 0x%x, set sia 0x%x,0x%x,0x%x\n",
936 de->dev->name, media_name[media],
937 de->dev->name, dr32(MacMode), dr32(SIAStatus),
938 dr32(CSR13), dr32(CSR14), dr32(CSR15),
939 de->dev->name, macmode, de->media[media].csr13,
940 de->media[media].csr14, de->media[media].csr15);
942 if (macmode != dr32(MacMode))
943 dw32(MacMode, macmode);
946 static void de_next_media (struct de_private *de, u32 *media,
947 unsigned int n_media)
949 unsigned int i;
951 for (i = 0; i < n_media; i++) {
952 if (de_ok_to_advertise(de, media[i])) {
953 de->media_type = media[i];
954 return;
959 static void de21040_media_timer (unsigned long data)
961 struct de_private *de = (struct de_private *) data;
962 struct net_device *dev = de->dev;
963 u32 status = dr32(SIAStatus);
964 unsigned int carrier;
965 unsigned long flags;
967 carrier = (status & NetCxnErr) ? 0 : 1;
969 if (carrier) {
970 if (de->media_type != DE_MEDIA_AUI && (status & LinkFailStatus))
971 goto no_link_yet;
973 de->media_timer.expires = jiffies + DE_TIMER_LINK;
974 add_timer(&de->media_timer);
975 if (!netif_carrier_ok(dev))
976 de_link_up(de);
977 else
978 if (netif_msg_timer(de))
979 printk(KERN_INFO "%s: %s link ok, status %x\n",
980 dev->name, media_name[de->media_type],
981 status);
982 return;
985 de_link_down(de);
987 if (de->media_lock)
988 return;
990 if (de->media_type == DE_MEDIA_AUI) {
991 u32 next_state = DE_MEDIA_TP;
992 de_next_media(de, &next_state, 1);
993 } else {
994 u32 next_state = DE_MEDIA_AUI;
995 de_next_media(de, &next_state, 1);
998 spin_lock_irqsave(&de->lock, flags);
999 de_stop_rxtx(de);
1000 spin_unlock_irqrestore(&de->lock, flags);
1001 de_set_media(de);
1002 de_start_rxtx(de);
1004 no_link_yet:
1005 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1006 add_timer(&de->media_timer);
1008 if (netif_msg_timer(de))
1009 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1010 dev->name, media_name[de->media_type], status);
1013 static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media)
1015 switch (new_media) {
1016 case DE_MEDIA_TP_AUTO:
1017 if (!(de->media_advertise & ADVERTISED_Autoneg))
1018 return 0;
1019 if (!(de->media_advertise & (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full)))
1020 return 0;
1021 break;
1022 case DE_MEDIA_BNC:
1023 if (!(de->media_advertise & ADVERTISED_BNC))
1024 return 0;
1025 break;
1026 case DE_MEDIA_AUI:
1027 if (!(de->media_advertise & ADVERTISED_AUI))
1028 return 0;
1029 break;
1030 case DE_MEDIA_TP:
1031 if (!(de->media_advertise & ADVERTISED_10baseT_Half))
1032 return 0;
1033 break;
1034 case DE_MEDIA_TP_FD:
1035 if (!(de->media_advertise & ADVERTISED_10baseT_Full))
1036 return 0;
1037 break;
1040 return 1;
1043 static void de21041_media_timer (unsigned long data)
1045 struct de_private *de = (struct de_private *) data;
1046 struct net_device *dev = de->dev;
1047 u32 status = dr32(SIAStatus);
1048 unsigned int carrier;
1049 unsigned long flags;
1051 carrier = (status & NetCxnErr) ? 0 : 1;
1053 if (carrier) {
1054 if ((de->media_type == DE_MEDIA_TP_AUTO ||
1055 de->media_type == DE_MEDIA_TP ||
1056 de->media_type == DE_MEDIA_TP_FD) &&
1057 (status & LinkFailStatus))
1058 goto no_link_yet;
1060 de->media_timer.expires = jiffies + DE_TIMER_LINK;
1061 add_timer(&de->media_timer);
1062 if (!netif_carrier_ok(dev))
1063 de_link_up(de);
1064 else
1065 if (netif_msg_timer(de))
1066 printk(KERN_INFO "%s: %s link ok, mode %x status %x\n",
1067 dev->name, media_name[de->media_type],
1068 dr32(MacMode), status);
1069 return;
1072 de_link_down(de);
1074 /* if media type locked, don't switch media */
1075 if (de->media_lock)
1076 goto set_media;
1078 /* if activity detected, use that as hint for new media type */
1079 if (status & NonselPortActive) {
1080 unsigned int have_media = 1;
1082 /* if AUI/BNC selected, then activity is on TP port */
1083 if (de->media_type == DE_MEDIA_AUI ||
1084 de->media_type == DE_MEDIA_BNC) {
1085 if (de_ok_to_advertise(de, DE_MEDIA_TP_AUTO))
1086 de->media_type = DE_MEDIA_TP_AUTO;
1087 else
1088 have_media = 0;
1091 /* TP selected. If there is only TP and BNC, then it's BNC */
1092 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_BNC) &&
1093 de_ok_to_advertise(de, DE_MEDIA_BNC))
1094 de->media_type = DE_MEDIA_BNC;
1096 /* TP selected. If there is only TP and AUI, then it's AUI */
1097 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_AUI) &&
1098 de_ok_to_advertise(de, DE_MEDIA_AUI))
1099 de->media_type = DE_MEDIA_AUI;
1101 /* otherwise, ignore the hint */
1102 else
1103 have_media = 0;
1105 if (have_media)
1106 goto set_media;
1110 * Absent or ambiguous activity hint, move to next advertised
1111 * media state. If de->media_type is left unchanged, this
1112 * simply resets the PHY and reloads the current media settings.
1114 if (de->media_type == DE_MEDIA_AUI) {
1115 u32 next_states[] = { DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1116 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1117 } else if (de->media_type == DE_MEDIA_BNC) {
1118 u32 next_states[] = { DE_MEDIA_TP_AUTO, DE_MEDIA_AUI };
1119 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1120 } else {
1121 u32 next_states[] = { DE_MEDIA_AUI, DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1122 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1125 set_media:
1126 spin_lock_irqsave(&de->lock, flags);
1127 de_stop_rxtx(de);
1128 spin_unlock_irqrestore(&de->lock, flags);
1129 de_set_media(de);
1130 de_start_rxtx(de);
1132 no_link_yet:
1133 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1134 add_timer(&de->media_timer);
1136 if (netif_msg_timer(de))
1137 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1138 dev->name, media_name[de->media_type], status);
1141 static void de_media_interrupt (struct de_private *de, u32 status)
1143 if (status & LinkPass) {
1144 de_link_up(de);
1145 mod_timer(&de->media_timer, jiffies + DE_TIMER_LINK);
1146 return;
1149 BUG_ON(!(status & LinkFail));
1151 if (netif_carrier_ok(de->dev)) {
1152 de_link_down(de);
1153 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1157 static int de_reset_mac (struct de_private *de)
1159 u32 status, tmp;
1162 * Reset MAC. de4x5.c and tulip.c examined for "advice"
1163 * in this area.
1166 if (dr32(BusMode) == 0xffffffff)
1167 return -EBUSY;
1169 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
1170 dw32 (BusMode, CmdReset);
1171 mdelay (1);
1173 dw32 (BusMode, de_bus_mode);
1174 mdelay (1);
1176 for (tmp = 0; tmp < 5; tmp++) {
1177 dr32 (BusMode);
1178 mdelay (1);
1181 mdelay (1);
1183 status = dr32(MacStatus);
1184 if (status & (RxState | TxState))
1185 return -EBUSY;
1186 if (status == 0xffffffff)
1187 return -ENODEV;
1188 return 0;
1191 static void de_adapter_wake (struct de_private *de)
1193 u32 pmctl;
1195 if (de->de21040)
1196 return;
1198 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1199 if (pmctl & PM_Mask) {
1200 pmctl &= ~PM_Mask;
1201 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1203 /* de4x5.c delays, so we do too */
1204 msleep(10);
1208 static void de_adapter_sleep (struct de_private *de)
1210 u32 pmctl;
1212 if (de->de21040)
1213 return;
1215 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1216 pmctl |= PM_Sleep;
1217 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1220 static int de_init_hw (struct de_private *de)
1222 struct net_device *dev = de->dev;
1223 u32 macmode;
1224 int rc;
1226 de_adapter_wake(de);
1228 macmode = dr32(MacMode) & ~MacModeClear;
1230 rc = de_reset_mac(de);
1231 if (rc)
1232 return rc;
1234 de_set_media(de); /* reset phy */
1236 dw32(RxRingAddr, de->ring_dma);
1237 dw32(TxRingAddr, de->ring_dma + (sizeof(struct de_desc) * DE_RX_RING_SIZE));
1239 dw32(MacMode, RxTx | macmode);
1241 dr32(RxMissed); /* self-clearing */
1243 dw32(IntrMask, de_intr_mask);
1245 de_set_rx_mode(dev);
1247 return 0;
1250 static int de_refill_rx (struct de_private *de)
1252 unsigned i;
1254 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1255 struct sk_buff *skb;
1257 skb = dev_alloc_skb(de->rx_buf_sz);
1258 if (!skb)
1259 goto err_out;
1261 skb->dev = de->dev;
1263 de->rx_skb[i].mapping = pci_map_single(de->pdev,
1264 skb->data, de->rx_buf_sz, PCI_DMA_FROMDEVICE);
1265 de->rx_skb[i].skb = skb;
1267 de->rx_ring[i].opts1 = cpu_to_le32(DescOwn);
1268 if (i == (DE_RX_RING_SIZE - 1))
1269 de->rx_ring[i].opts2 =
1270 cpu_to_le32(RingEnd | de->rx_buf_sz);
1271 else
1272 de->rx_ring[i].opts2 = cpu_to_le32(de->rx_buf_sz);
1273 de->rx_ring[i].addr1 = cpu_to_le32(de->rx_skb[i].mapping);
1274 de->rx_ring[i].addr2 = 0;
1277 return 0;
1279 err_out:
1280 de_clean_rings(de);
1281 return -ENOMEM;
1284 static int de_init_rings (struct de_private *de)
1286 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1287 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1289 de->rx_tail = 0;
1290 de->tx_head = de->tx_tail = 0;
1292 return de_refill_rx (de);
1295 static int de_alloc_rings (struct de_private *de)
1297 de->rx_ring = pci_alloc_consistent(de->pdev, DE_RING_BYTES, &de->ring_dma);
1298 if (!de->rx_ring)
1299 return -ENOMEM;
1300 de->tx_ring = &de->rx_ring[DE_RX_RING_SIZE];
1301 return de_init_rings(de);
1304 static void de_clean_rings (struct de_private *de)
1306 unsigned i;
1308 memset(de->rx_ring, 0, sizeof(struct de_desc) * DE_RX_RING_SIZE);
1309 de->rx_ring[DE_RX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1310 wmb();
1311 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1312 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1313 wmb();
1315 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1316 if (de->rx_skb[i].skb) {
1317 pci_unmap_single(de->pdev, de->rx_skb[i].mapping,
1318 de->rx_buf_sz, PCI_DMA_FROMDEVICE);
1319 dev_kfree_skb(de->rx_skb[i].skb);
1323 for (i = 0; i < DE_TX_RING_SIZE; i++) {
1324 struct sk_buff *skb = de->tx_skb[i].skb;
1325 if ((skb) && (skb != DE_DUMMY_SKB)) {
1326 if (skb != DE_SETUP_SKB) {
1327 de->net_stats.tx_dropped++;
1328 pci_unmap_single(de->pdev,
1329 de->tx_skb[i].mapping,
1330 skb->len, PCI_DMA_TODEVICE);
1331 dev_kfree_skb(skb);
1332 } else {
1333 pci_unmap_single(de->pdev,
1334 de->tx_skb[i].mapping,
1335 sizeof(de->setup_frame),
1336 PCI_DMA_TODEVICE);
1341 memset(&de->rx_skb, 0, sizeof(struct ring_info) * DE_RX_RING_SIZE);
1342 memset(&de->tx_skb, 0, sizeof(struct ring_info) * DE_TX_RING_SIZE);
1345 static void de_free_rings (struct de_private *de)
1347 de_clean_rings(de);
1348 pci_free_consistent(de->pdev, DE_RING_BYTES, de->rx_ring, de->ring_dma);
1349 de->rx_ring = NULL;
1350 de->tx_ring = NULL;
1353 static int de_open (struct net_device *dev)
1355 struct de_private *de = dev->priv;
1356 int rc;
1358 if (netif_msg_ifup(de))
1359 printk(KERN_DEBUG "%s: enabling interface\n", dev->name);
1361 de->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1363 rc = de_alloc_rings(de);
1364 if (rc) {
1365 printk(KERN_ERR "%s: ring allocation failure, err=%d\n",
1366 dev->name, rc);
1367 return rc;
1370 dw32(IntrMask, 0);
1372 rc = request_irq(dev->irq, de_interrupt, IRQF_SHARED, dev->name, dev);
1373 if (rc) {
1374 printk(KERN_ERR "%s: IRQ %d request failure, err=%d\n",
1375 dev->name, dev->irq, rc);
1376 goto err_out_free;
1379 rc = de_init_hw(de);
1380 if (rc) {
1381 printk(KERN_ERR "%s: h/w init failure, err=%d\n",
1382 dev->name, rc);
1383 goto err_out_free_irq;
1386 netif_start_queue(dev);
1387 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1389 return 0;
1391 err_out_free_irq:
1392 free_irq(dev->irq, dev);
1393 err_out_free:
1394 de_free_rings(de);
1395 return rc;
1398 static int de_close (struct net_device *dev)
1400 struct de_private *de = dev->priv;
1401 unsigned long flags;
1403 if (netif_msg_ifdown(de))
1404 printk(KERN_DEBUG "%s: disabling interface\n", dev->name);
1406 del_timer_sync(&de->media_timer);
1408 spin_lock_irqsave(&de->lock, flags);
1409 de_stop_hw(de);
1410 netif_stop_queue(dev);
1411 netif_carrier_off(dev);
1412 spin_unlock_irqrestore(&de->lock, flags);
1414 free_irq(dev->irq, dev);
1416 de_free_rings(de);
1417 de_adapter_sleep(de);
1418 pci_disable_device(de->pdev);
1419 return 0;
1422 static void de_tx_timeout (struct net_device *dev)
1424 struct de_private *de = dev->priv;
1426 printk(KERN_DEBUG "%s: NIC status %08x mode %08x sia %08x desc %u/%u/%u\n",
1427 dev->name, dr32(MacStatus), dr32(MacMode), dr32(SIAStatus),
1428 de->rx_tail, de->tx_head, de->tx_tail);
1430 del_timer_sync(&de->media_timer);
1432 disable_irq(dev->irq);
1433 spin_lock_irq(&de->lock);
1435 de_stop_hw(de);
1436 netif_stop_queue(dev);
1437 netif_carrier_off(dev);
1439 spin_unlock_irq(&de->lock);
1440 enable_irq(dev->irq);
1442 /* Update the error counts. */
1443 __de_get_stats(de);
1445 synchronize_irq(dev->irq);
1446 de_clean_rings(de);
1448 de_init_rings(de);
1450 de_init_hw(de);
1452 netif_wake_queue(dev);
1455 static void __de_get_regs(struct de_private *de, u8 *buf)
1457 int i;
1458 u32 *rbuf = (u32 *)buf;
1460 /* read all CSRs */
1461 for (i = 0; i < DE_NUM_REGS; i++)
1462 rbuf[i] = dr32(i * 8);
1464 /* handle self-clearing RxMissed counter, CSR8 */
1465 de_rx_missed(de, rbuf[8]);
1468 static int __de_get_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1470 ecmd->supported = de->media_supported;
1471 ecmd->transceiver = XCVR_INTERNAL;
1472 ecmd->phy_address = 0;
1473 ecmd->advertising = de->media_advertise;
1475 switch (de->media_type) {
1476 case DE_MEDIA_AUI:
1477 ecmd->port = PORT_AUI;
1478 ecmd->speed = 5;
1479 break;
1480 case DE_MEDIA_BNC:
1481 ecmd->port = PORT_BNC;
1482 ecmd->speed = 2;
1483 break;
1484 default:
1485 ecmd->port = PORT_TP;
1486 ecmd->speed = SPEED_10;
1487 break;
1490 if (dr32(MacMode) & FullDuplex)
1491 ecmd->duplex = DUPLEX_FULL;
1492 else
1493 ecmd->duplex = DUPLEX_HALF;
1495 if (de->media_lock)
1496 ecmd->autoneg = AUTONEG_DISABLE;
1497 else
1498 ecmd->autoneg = AUTONEG_ENABLE;
1500 /* ignore maxtxpkt, maxrxpkt for now */
1502 return 0;
1505 static int __de_set_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1507 u32 new_media;
1508 unsigned int media_lock;
1510 if (ecmd->speed != SPEED_10 && ecmd->speed != 5 && ecmd->speed != 2)
1511 return -EINVAL;
1512 if (de->de21040 && ecmd->speed == 2)
1513 return -EINVAL;
1514 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1515 return -EINVAL;
1516 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI && ecmd->port != PORT_BNC)
1517 return -EINVAL;
1518 if (de->de21040 && ecmd->port == PORT_BNC)
1519 return -EINVAL;
1520 if (ecmd->transceiver != XCVR_INTERNAL)
1521 return -EINVAL;
1522 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1523 return -EINVAL;
1524 if (ecmd->advertising & ~de->media_supported)
1525 return -EINVAL;
1526 if (ecmd->autoneg == AUTONEG_ENABLE &&
1527 (!(ecmd->advertising & ADVERTISED_Autoneg)))
1528 return -EINVAL;
1530 switch (ecmd->port) {
1531 case PORT_AUI:
1532 new_media = DE_MEDIA_AUI;
1533 if (!(ecmd->advertising & ADVERTISED_AUI))
1534 return -EINVAL;
1535 break;
1536 case PORT_BNC:
1537 new_media = DE_MEDIA_BNC;
1538 if (!(ecmd->advertising & ADVERTISED_BNC))
1539 return -EINVAL;
1540 break;
1541 default:
1542 if (ecmd->autoneg == AUTONEG_ENABLE)
1543 new_media = DE_MEDIA_TP_AUTO;
1544 else if (ecmd->duplex == DUPLEX_FULL)
1545 new_media = DE_MEDIA_TP_FD;
1546 else
1547 new_media = DE_MEDIA_TP;
1548 if (!(ecmd->advertising & ADVERTISED_TP))
1549 return -EINVAL;
1550 if (!(ecmd->advertising & (ADVERTISED_10baseT_Full | ADVERTISED_10baseT_Half)))
1551 return -EINVAL;
1552 break;
1555 media_lock = (ecmd->autoneg == AUTONEG_ENABLE) ? 0 : 1;
1557 if ((new_media == de->media_type) &&
1558 (media_lock == de->media_lock) &&
1559 (ecmd->advertising == de->media_advertise))
1560 return 0; /* nothing to change */
1562 de_link_down(de);
1563 de_stop_rxtx(de);
1565 de->media_type = new_media;
1566 de->media_lock = media_lock;
1567 de->media_advertise = ecmd->advertising;
1568 de_set_media(de);
1570 return 0;
1573 static void de_get_drvinfo (struct net_device *dev,struct ethtool_drvinfo *info)
1575 struct de_private *de = dev->priv;
1577 strcpy (info->driver, DRV_NAME);
1578 strcpy (info->version, DRV_VERSION);
1579 strcpy (info->bus_info, pci_name(de->pdev));
1580 info->eedump_len = DE_EEPROM_SIZE;
1583 static int de_get_regs_len(struct net_device *dev)
1585 return DE_REGS_SIZE;
1588 static int de_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1590 struct de_private *de = dev->priv;
1591 int rc;
1593 spin_lock_irq(&de->lock);
1594 rc = __de_get_settings(de, ecmd);
1595 spin_unlock_irq(&de->lock);
1597 return rc;
1600 static int de_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1602 struct de_private *de = dev->priv;
1603 int rc;
1605 spin_lock_irq(&de->lock);
1606 rc = __de_set_settings(de, ecmd);
1607 spin_unlock_irq(&de->lock);
1609 return rc;
1612 static u32 de_get_msglevel(struct net_device *dev)
1614 struct de_private *de = dev->priv;
1616 return de->msg_enable;
1619 static void de_set_msglevel(struct net_device *dev, u32 msglvl)
1621 struct de_private *de = dev->priv;
1623 de->msg_enable = msglvl;
1626 static int de_get_eeprom(struct net_device *dev,
1627 struct ethtool_eeprom *eeprom, u8 *data)
1629 struct de_private *de = dev->priv;
1631 if (!de->ee_data)
1632 return -EOPNOTSUPP;
1633 if ((eeprom->offset != 0) || (eeprom->magic != 0) ||
1634 (eeprom->len != DE_EEPROM_SIZE))
1635 return -EINVAL;
1636 memcpy(data, de->ee_data, eeprom->len);
1638 return 0;
1641 static int de_nway_reset(struct net_device *dev)
1643 struct de_private *de = dev->priv;
1644 u32 status;
1646 if (de->media_type != DE_MEDIA_TP_AUTO)
1647 return -EINVAL;
1648 if (netif_carrier_ok(de->dev))
1649 de_link_down(de);
1651 status = dr32(SIAStatus);
1652 dw32(SIAStatus, (status & ~NWayState) | NWayRestart);
1653 if (netif_msg_link(de))
1654 printk(KERN_INFO "%s: link nway restart, status %x,%x\n",
1655 de->dev->name, status, dr32(SIAStatus));
1656 return 0;
1659 static void de_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1660 void *data)
1662 struct de_private *de = dev->priv;
1664 regs->version = (DE_REGS_VER << 2) | de->de21040;
1666 spin_lock_irq(&de->lock);
1667 __de_get_regs(de, data);
1668 spin_unlock_irq(&de->lock);
1671 static const struct ethtool_ops de_ethtool_ops = {
1672 .get_link = ethtool_op_get_link,
1673 .get_drvinfo = de_get_drvinfo,
1674 .get_regs_len = de_get_regs_len,
1675 .get_settings = de_get_settings,
1676 .set_settings = de_set_settings,
1677 .get_msglevel = de_get_msglevel,
1678 .set_msglevel = de_set_msglevel,
1679 .get_eeprom = de_get_eeprom,
1680 .nway_reset = de_nway_reset,
1681 .get_regs = de_get_regs,
1684 static void __devinit de21040_get_mac_address (struct de_private *de)
1686 unsigned i;
1688 dw32 (ROMCmd, 0); /* Reset the pointer with a dummy write. */
1690 for (i = 0; i < 6; i++) {
1691 int value, boguscnt = 100000;
1693 value = dr32(ROMCmd);
1694 while (value < 0 && --boguscnt > 0);
1695 de->dev->dev_addr[i] = value;
1696 udelay(1);
1697 if (boguscnt <= 0)
1698 printk(KERN_WARNING PFX "timeout reading 21040 MAC address byte %u\n", i);
1702 static void __devinit de21040_get_media_info(struct de_private *de)
1704 unsigned int i;
1706 de->media_type = DE_MEDIA_TP;
1707 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full |
1708 SUPPORTED_10baseT_Half | SUPPORTED_AUI;
1709 de->media_advertise = de->media_supported;
1711 for (i = 0; i < DE_MAX_MEDIA; i++) {
1712 switch (i) {
1713 case DE_MEDIA_AUI:
1714 case DE_MEDIA_TP:
1715 case DE_MEDIA_TP_FD:
1716 de->media[i].type = i;
1717 de->media[i].csr13 = t21040_csr13[i];
1718 de->media[i].csr14 = t21040_csr14[i];
1719 de->media[i].csr15 = t21040_csr15[i];
1720 break;
1721 default:
1722 de->media[i].type = DE_MEDIA_INVALID;
1723 break;
1728 /* Note: this routine returns extra data bits for size detection. */
1729 static unsigned __devinit tulip_read_eeprom(void __iomem *regs, int location, int addr_len)
1731 int i;
1732 unsigned retval = 0;
1733 void __iomem *ee_addr = regs + ROMCmd;
1734 int read_cmd = location | (EE_READ_CMD << addr_len);
1736 writel(EE_ENB & ~EE_CS, ee_addr);
1737 writel(EE_ENB, ee_addr);
1739 /* Shift the read command bits out. */
1740 for (i = 4 + addr_len; i >= 0; i--) {
1741 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1742 writel(EE_ENB | dataval, ee_addr);
1743 readl(ee_addr);
1744 writel(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1745 readl(ee_addr);
1746 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1748 writel(EE_ENB, ee_addr);
1749 readl(ee_addr);
1751 for (i = 16; i > 0; i--) {
1752 writel(EE_ENB | EE_SHIFT_CLK, ee_addr);
1753 readl(ee_addr);
1754 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1755 writel(EE_ENB, ee_addr);
1756 readl(ee_addr);
1759 /* Terminate the EEPROM access. */
1760 writel(EE_ENB & ~EE_CS, ee_addr);
1761 return retval;
1764 static void __devinit de21041_get_srom_info (struct de_private *de)
1766 unsigned i, sa_offset = 0, ofs;
1767 u8 ee_data[DE_EEPROM_SIZE + 6] = {};
1768 unsigned ee_addr_size = tulip_read_eeprom(de->regs, 0xff, 8) & 0x40000 ? 8 : 6;
1769 struct de_srom_info_leaf *il;
1770 void *bufp;
1772 /* download entire eeprom */
1773 for (i = 0; i < DE_EEPROM_WORDS; i++)
1774 ((__le16 *)ee_data)[i] =
1775 cpu_to_le16(tulip_read_eeprom(de->regs, i, ee_addr_size));
1777 /* DEC now has a specification but early board makers
1778 just put the address in the first EEPROM locations. */
1779 /* This does memcmp(eedata, eedata+16, 8) */
1781 #ifndef CONFIG_MIPS_COBALT
1783 for (i = 0; i < 8; i ++)
1784 if (ee_data[i] != ee_data[16+i])
1785 sa_offset = 20;
1787 #endif
1789 /* store MAC address */
1790 for (i = 0; i < 6; i ++)
1791 de->dev->dev_addr[i] = ee_data[i + sa_offset];
1793 /* get offset of controller 0 info leaf. ignore 2nd byte. */
1794 ofs = ee_data[SROMC0InfoLeaf];
1795 if (ofs >= (sizeof(ee_data) - sizeof(struct de_srom_info_leaf) - sizeof(struct de_srom_media_block)))
1796 goto bad_srom;
1798 /* get pointer to info leaf */
1799 il = (struct de_srom_info_leaf *) &ee_data[ofs];
1801 /* paranoia checks */
1802 if (il->n_blocks == 0)
1803 goto bad_srom;
1804 if ((sizeof(ee_data) - ofs) <
1805 (sizeof(struct de_srom_info_leaf) + (sizeof(struct de_srom_media_block) * il->n_blocks)))
1806 goto bad_srom;
1808 /* get default media type */
1809 switch (DE_UNALIGNED_16(&il->default_media)) {
1810 case 0x0001: de->media_type = DE_MEDIA_BNC; break;
1811 case 0x0002: de->media_type = DE_MEDIA_AUI; break;
1812 case 0x0204: de->media_type = DE_MEDIA_TP_FD; break;
1813 default: de->media_type = DE_MEDIA_TP_AUTO; break;
1816 if (netif_msg_probe(de))
1817 printk(KERN_INFO "de%d: SROM leaf offset %u, default media %s\n",
1818 de->board_idx, ofs,
1819 media_name[de->media_type]);
1821 /* init SIA register values to defaults */
1822 for (i = 0; i < DE_MAX_MEDIA; i++) {
1823 de->media[i].type = DE_MEDIA_INVALID;
1824 de->media[i].csr13 = 0xffff;
1825 de->media[i].csr14 = 0xffff;
1826 de->media[i].csr15 = 0xffff;
1829 /* parse media blocks to see what medias are supported,
1830 * and if any custom CSR values are provided
1832 bufp = ((void *)il) + sizeof(*il);
1833 for (i = 0; i < il->n_blocks; i++) {
1834 struct de_srom_media_block *ib = bufp;
1835 unsigned idx;
1837 /* index based on media type in media block */
1838 switch(ib->opts & MediaBlockMask) {
1839 case 0: /* 10baseT */
1840 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half
1841 | SUPPORTED_Autoneg;
1842 idx = DE_MEDIA_TP;
1843 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1844 break;
1845 case 1: /* BNC */
1846 de->media_supported |= SUPPORTED_BNC;
1847 idx = DE_MEDIA_BNC;
1848 break;
1849 case 2: /* AUI */
1850 de->media_supported |= SUPPORTED_AUI;
1851 idx = DE_MEDIA_AUI;
1852 break;
1853 case 4: /* 10baseT-FD */
1854 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full
1855 | SUPPORTED_Autoneg;
1856 idx = DE_MEDIA_TP_FD;
1857 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1858 break;
1859 default:
1860 goto bad_srom;
1863 de->media[idx].type = idx;
1865 if (netif_msg_probe(de))
1866 printk(KERN_INFO "de%d: media block #%u: %s",
1867 de->board_idx, i,
1868 media_name[de->media[idx].type]);
1870 bufp += sizeof (ib->opts);
1872 if (ib->opts & MediaCustomCSRs) {
1873 de->media[idx].csr13 = DE_UNALIGNED_16(&ib->csr13);
1874 de->media[idx].csr14 = DE_UNALIGNED_16(&ib->csr14);
1875 de->media[idx].csr15 = DE_UNALIGNED_16(&ib->csr15);
1876 bufp += sizeof(ib->csr13) + sizeof(ib->csr14) +
1877 sizeof(ib->csr15);
1879 if (netif_msg_probe(de))
1880 printk(" (%x,%x,%x)\n",
1881 de->media[idx].csr13,
1882 de->media[idx].csr14,
1883 de->media[idx].csr15);
1885 } else if (netif_msg_probe(de))
1886 printk("\n");
1888 if (bufp > ((void *)&ee_data[DE_EEPROM_SIZE - 3]))
1889 break;
1892 de->media_advertise = de->media_supported;
1894 fill_defaults:
1895 /* fill in defaults, for cases where custom CSRs not used */
1896 for (i = 0; i < DE_MAX_MEDIA; i++) {
1897 if (de->media[i].csr13 == 0xffff)
1898 de->media[i].csr13 = t21041_csr13[i];
1899 if (de->media[i].csr14 == 0xffff)
1900 de->media[i].csr14 = t21041_csr14[i];
1901 if (de->media[i].csr15 == 0xffff)
1902 de->media[i].csr15 = t21041_csr15[i];
1905 de->ee_data = kmemdup(&ee_data[0], DE_EEPROM_SIZE, GFP_KERNEL);
1907 return;
1909 bad_srom:
1910 /* for error cases, it's ok to assume we support all these */
1911 for (i = 0; i < DE_MAX_MEDIA; i++)
1912 de->media[i].type = i;
1913 de->media_supported =
1914 SUPPORTED_10baseT_Half |
1915 SUPPORTED_10baseT_Full |
1916 SUPPORTED_Autoneg |
1917 SUPPORTED_TP |
1918 SUPPORTED_AUI |
1919 SUPPORTED_BNC;
1920 goto fill_defaults;
1923 static int __devinit de_init_one (struct pci_dev *pdev,
1924 const struct pci_device_id *ent)
1926 struct net_device *dev;
1927 struct de_private *de;
1928 int rc;
1929 void __iomem *regs;
1930 unsigned long pciaddr;
1931 static int board_idx = -1;
1932 DECLARE_MAC_BUF(mac);
1934 board_idx++;
1936 #ifndef MODULE
1937 if (board_idx == 0)
1938 printk("%s", version);
1939 #endif
1941 /* allocate a new ethernet device structure, and fill in defaults */
1942 dev = alloc_etherdev(sizeof(struct de_private));
1943 if (!dev)
1944 return -ENOMEM;
1946 SET_NETDEV_DEV(dev, &pdev->dev);
1947 dev->open = de_open;
1948 dev->stop = de_close;
1949 dev->set_multicast_list = de_set_rx_mode;
1950 dev->hard_start_xmit = de_start_xmit;
1951 dev->get_stats = de_get_stats;
1952 dev->ethtool_ops = &de_ethtool_ops;
1953 dev->tx_timeout = de_tx_timeout;
1954 dev->watchdog_timeo = TX_TIMEOUT;
1956 de = dev->priv;
1957 de->de21040 = ent->driver_data == 0 ? 1 : 0;
1958 de->pdev = pdev;
1959 de->dev = dev;
1960 de->msg_enable = (debug < 0 ? DE_DEF_MSG_ENABLE : debug);
1961 de->board_idx = board_idx;
1962 spin_lock_init (&de->lock);
1963 init_timer(&de->media_timer);
1964 if (de->de21040)
1965 de->media_timer.function = de21040_media_timer;
1966 else
1967 de->media_timer.function = de21041_media_timer;
1968 de->media_timer.data = (unsigned long) de;
1970 netif_carrier_off(dev);
1971 netif_stop_queue(dev);
1973 /* wake up device, assign resources */
1974 rc = pci_enable_device(pdev);
1975 if (rc)
1976 goto err_out_free;
1978 /* reserve PCI resources to ensure driver atomicity */
1979 rc = pci_request_regions(pdev, DRV_NAME);
1980 if (rc)
1981 goto err_out_disable;
1983 /* check for invalid IRQ value */
1984 if (pdev->irq < 2) {
1985 rc = -EIO;
1986 printk(KERN_ERR PFX "invalid irq (%d) for pci dev %s\n",
1987 pdev->irq, pci_name(pdev));
1988 goto err_out_res;
1991 dev->irq = pdev->irq;
1993 /* obtain and check validity of PCI I/O address */
1994 pciaddr = pci_resource_start(pdev, 1);
1995 if (!pciaddr) {
1996 rc = -EIO;
1997 printk(KERN_ERR PFX "no MMIO resource for pci dev %s\n",
1998 pci_name(pdev));
1999 goto err_out_res;
2001 if (pci_resource_len(pdev, 1) < DE_REGS_SIZE) {
2002 rc = -EIO;
2003 printk(KERN_ERR PFX "MMIO resource (%llx) too small on pci dev %s\n",
2004 (unsigned long long)pci_resource_len(pdev, 1), pci_name(pdev));
2005 goto err_out_res;
2008 /* remap CSR registers */
2009 regs = ioremap_nocache(pciaddr, DE_REGS_SIZE);
2010 if (!regs) {
2011 rc = -EIO;
2012 printk(KERN_ERR PFX "Cannot map PCI MMIO (%llx@%lx) on pci dev %s\n",
2013 (unsigned long long)pci_resource_len(pdev, 1),
2014 pciaddr, pci_name(pdev));
2015 goto err_out_res;
2017 dev->base_addr = (unsigned long) regs;
2018 de->regs = regs;
2020 de_adapter_wake(de);
2022 /* make sure hardware is not running */
2023 rc = de_reset_mac(de);
2024 if (rc) {
2025 printk(KERN_ERR PFX "Cannot reset MAC, pci dev %s\n",
2026 pci_name(pdev));
2027 goto err_out_iomap;
2030 /* get MAC address, initialize default media type and
2031 * get list of supported media
2033 if (de->de21040) {
2034 de21040_get_mac_address(de);
2035 de21040_get_media_info(de);
2036 } else {
2037 de21041_get_srom_info(de);
2040 /* register new network interface with kernel */
2041 rc = register_netdev(dev);
2042 if (rc)
2043 goto err_out_iomap;
2045 /* print info about board and interface just registered */
2046 printk (KERN_INFO "%s: %s at 0x%lx, %s, IRQ %d\n",
2047 dev->name,
2048 de->de21040 ? "21040" : "21041",
2049 dev->base_addr,
2050 print_mac(mac, dev->dev_addr),
2051 dev->irq);
2053 pci_set_drvdata(pdev, dev);
2055 /* enable busmastering */
2056 pci_set_master(pdev);
2058 /* put adapter to sleep */
2059 de_adapter_sleep(de);
2061 return 0;
2063 err_out_iomap:
2064 kfree(de->ee_data);
2065 iounmap(regs);
2066 err_out_res:
2067 pci_release_regions(pdev);
2068 err_out_disable:
2069 pci_disable_device(pdev);
2070 err_out_free:
2071 free_netdev(dev);
2072 return rc;
2075 static void __devexit de_remove_one (struct pci_dev *pdev)
2077 struct net_device *dev = pci_get_drvdata(pdev);
2078 struct de_private *de = dev->priv;
2080 BUG_ON(!dev);
2081 unregister_netdev(dev);
2082 kfree(de->ee_data);
2083 iounmap(de->regs);
2084 pci_release_regions(pdev);
2085 pci_disable_device(pdev);
2086 pci_set_drvdata(pdev, NULL);
2087 free_netdev(dev);
2090 #ifdef CONFIG_PM
2092 static int de_suspend (struct pci_dev *pdev, pm_message_t state)
2094 struct net_device *dev = pci_get_drvdata (pdev);
2095 struct de_private *de = dev->priv;
2097 rtnl_lock();
2098 if (netif_running (dev)) {
2099 del_timer_sync(&de->media_timer);
2101 disable_irq(dev->irq);
2102 spin_lock_irq(&de->lock);
2104 de_stop_hw(de);
2105 netif_stop_queue(dev);
2106 netif_device_detach(dev);
2107 netif_carrier_off(dev);
2109 spin_unlock_irq(&de->lock);
2110 enable_irq(dev->irq);
2112 /* Update the error counts. */
2113 __de_get_stats(de);
2115 synchronize_irq(dev->irq);
2116 de_clean_rings(de);
2118 de_adapter_sleep(de);
2119 pci_disable_device(pdev);
2120 } else {
2121 netif_device_detach(dev);
2123 rtnl_unlock();
2124 return 0;
2127 static int de_resume (struct pci_dev *pdev)
2129 struct net_device *dev = pci_get_drvdata (pdev);
2130 struct de_private *de = dev->priv;
2131 int retval = 0;
2133 rtnl_lock();
2134 if (netif_device_present(dev))
2135 goto out;
2136 if (!netif_running(dev))
2137 goto out_attach;
2138 if ((retval = pci_enable_device(pdev))) {
2139 printk (KERN_ERR "%s: pci_enable_device failed in resume\n",
2140 dev->name);
2141 goto out;
2143 de_init_hw(de);
2144 out_attach:
2145 netif_device_attach(dev);
2146 out:
2147 rtnl_unlock();
2148 return 0;
2151 #endif /* CONFIG_PM */
2153 static struct pci_driver de_driver = {
2154 .name = DRV_NAME,
2155 .id_table = de_pci_tbl,
2156 .probe = de_init_one,
2157 .remove = __devexit_p(de_remove_one),
2158 #ifdef CONFIG_PM
2159 .suspend = de_suspend,
2160 .resume = de_resume,
2161 #endif
2164 static int __init de_init (void)
2166 #ifdef MODULE
2167 printk("%s", version);
2168 #endif
2169 return pci_register_driver(&de_driver);
2172 static void __exit de_exit (void)
2174 pci_unregister_driver (&de_driver);
2177 module_init(de_init);
2178 module_exit(de_exit);