blob: d4c5ecc51f779e975eff00a72413a46ae4c171b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* de2104x.c: A Linux PCI Ethernet driver for Intel/Digital 21040/1 chips. */
2/*
3 Copyright 2001,2003 Jeff Garzik <jgarzik@pobox.com>
4
5 Copyright 1994, 1995 Digital Equipment Corporation. [de4x5.c]
6 Written/copyright 1994-2001 by Donald Becker. [tulip.c]
7
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.
14
15 See the file COPYING in this distribution for more information.
16
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
27
28 */
29
30#define DRV_NAME "de2104x"
31#define DRV_VERSION "0.7"
32#define DRV_RELDATE "Mar 17, 2004"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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>
45
46#include <asm/io.h>
47#include <asm/irq.h>
48#include <asm/uaccess.h>
49#include <asm/unaligned.h>
50
51/* These identify the driver base version and may not be removed. */
52static char version[] =
53KERN_INFO DRV_NAME " PCI Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";
54
55MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
56MODULE_DESCRIPTION("Intel/Digital 21040/1 series PCI Ethernet driver");
57MODULE_LICENSE("GPL");
58MODULE_VERSION(DRV_VERSION);
59
60static int debug = -1;
61module_param (debug, int, 0);
62MODULE_PARM_DESC (debug, "de2104x bitmapped message enable number");
63
64/* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
65#if defined(__alpha__) || defined(__arm__) || defined(__hppa__) \
David S. Miller49345102007-03-29 01:39:44 -070066 || defined(CONFIG_SPARC) || defined(__ia64__) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 || defined(__sh__) || defined(__mips__)
68static int rx_copybreak = 1518;
69#else
70static int rx_copybreak = 100;
71#endif
72module_param (rx_copybreak, int, 0);
73MODULE_PARM_DESC (rx_copybreak, "de2104x Breakpoint at which Rx packets are copied");
74
75#define PFX DRV_NAME ": "
76
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)
84
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)
96
97#define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
98#define RX_OFFSET 2
99
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
106
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)
116
117#define DE_TIMER_LINK (60 * HZ)
118#define DE_TIMER_NO_LINK (5 * HZ)
119
120#define DE_NUM_REGS 16
121#define DE_REGS_SIZE (DE_NUM_REGS * sizeof(u32))
122#define DE_REGS_VER 1
123
124/* Time in jiffies before concluding the transmitter is hung. */
125#define TX_TIMEOUT (6*HZ)
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* This is a mysterious value that can be written to CSR11 in the 21040 (only)
128 to support a pre-NWay full-duplex signaling mechanism using short frames.
129 No one knows what it should be, but if left at its default value some
130 10base2(!) packets trigger a full-duplex-request interrupt. */
131#define FULL_DUPLEX_MAGIC 0x6969
132
133enum {
134 /* NIC registers */
135 BusMode = 0x00,
136 TxPoll = 0x08,
137 RxPoll = 0x10,
138 RxRingAddr = 0x18,
139 TxRingAddr = 0x20,
140 MacStatus = 0x28,
141 MacMode = 0x30,
142 IntrMask = 0x38,
143 RxMissed = 0x40,
144 ROMCmd = 0x48,
145 CSR11 = 0x58,
146 SIAStatus = 0x60,
147 CSR13 = 0x68,
148 CSR14 = 0x70,
149 CSR15 = 0x78,
150 PCIPM = 0x40,
151
152 /* BusMode bits */
153 CmdReset = (1 << 0),
154 CacheAlign16 = 0x00008000,
155 BurstLen4 = 0x00000400,
156
157 /* Rx/TxPoll bits */
158 NormalTxPoll = (1 << 0),
159 NormalRxPoll = (1 << 0),
160
161 /* Tx/Rx descriptor status bits */
162 DescOwn = (1 << 31),
163 RxError = (1 << 15),
164 RxErrLong = (1 << 7),
165 RxErrCRC = (1 << 1),
166 RxErrFIFO = (1 << 0),
167 RxErrRunt = (1 << 11),
168 RxErrFrame = (1 << 14),
169 RingEnd = (1 << 25),
170 FirstFrag = (1 << 29),
171 LastFrag = (1 << 30),
172 TxError = (1 << 15),
173 TxFIFOUnder = (1 << 1),
174 TxLinkFail = (1 << 2) | (1 << 10) | (1 << 11),
175 TxMaxCol = (1 << 8),
176 TxOWC = (1 << 9),
177 TxJabber = (1 << 14),
178 SetupFrame = (1 << 27),
179 TxSwInt = (1 << 31),
180
181 /* MacStatus bits */
182 IntrOK = (1 << 16),
183 IntrErr = (1 << 15),
184 RxIntr = (1 << 6),
185 RxEmpty = (1 << 7),
186 TxIntr = (1 << 0),
187 TxEmpty = (1 << 2),
188 PciErr = (1 << 13),
189 TxState = (1 << 22) | (1 << 21) | (1 << 20),
190 RxState = (1 << 19) | (1 << 18) | (1 << 17),
191 LinkFail = (1 << 12),
192 LinkPass = (1 << 4),
193 RxStopped = (1 << 8),
194 TxStopped = (1 << 1),
195
196 /* MacMode bits */
197 TxEnable = (1 << 13),
198 RxEnable = (1 << 1),
199 RxTx = TxEnable | RxEnable,
200 FullDuplex = (1 << 9),
201 AcceptAllMulticast = (1 << 7),
202 AcceptAllPhys = (1 << 6),
203 BOCnt = (1 << 5),
204 MacModeClear = (1<<12) | (1<<11) | (1<<10) | (1<<8) | (1<<3) |
205 RxTx | BOCnt | AcceptAllPhys | AcceptAllMulticast,
206
207 /* ROMCmd bits */
208 EE_SHIFT_CLK = 0x02, /* EEPROM shift clock. */
209 EE_CS = 0x01, /* EEPROM chip select. */
210 EE_DATA_WRITE = 0x04, /* Data from the Tulip to EEPROM. */
211 EE_WRITE_0 = 0x01,
212 EE_WRITE_1 = 0x05,
213 EE_DATA_READ = 0x08, /* Data from the EEPROM chip. */
214 EE_ENB = (0x4800 | EE_CS),
215
216 /* The EEPROM commands include the alway-set leading bit. */
217 EE_READ_CMD = 6,
218
219 /* RxMissed bits */
220 RxMissedOver = (1 << 16),
221 RxMissedMask = 0xffff,
222
223 /* SROM-related bits */
224 SROMC0InfoLeaf = 27,
225 MediaBlockMask = 0x3f,
226 MediaCustomCSRs = (1 << 6),
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* PCIPM bits */
229 PM_Sleep = (1 << 31),
230 PM_Snooze = (1 << 30),
231 PM_Mask = PM_Sleep | PM_Snooze,
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* SIAStatus bits */
234 NWayState = (1 << 14) | (1 << 13) | (1 << 12),
235 NWayRestart = (1 << 12),
236 NonselPortActive = (1 << 9),
237 LinkFailStatus = (1 << 2),
238 NetCxnErr = (1 << 1),
239};
240
241static const u32 de_intr_mask =
242 IntrOK | IntrErr | RxIntr | RxEmpty | TxIntr | TxEmpty |
243 LinkPass | LinkFail | PciErr;
244
245/*
246 * Set the programmable burst length to 4 longwords for all:
247 * DMA errors result without these values. Cache align 16 long.
248 */
249static const u32 de_bus_mode = CacheAlign16 | BurstLen4;
250
251struct de_srom_media_block {
252 u8 opts;
253 u16 csr13;
254 u16 csr14;
255 u16 csr15;
256} __attribute__((packed));
257
258struct de_srom_info_leaf {
259 u16 default_media;
260 u8 n_blocks;
261 u8 unused;
262} __attribute__((packed));
263
264struct de_desc {
Al Viroc559a5b2007-08-23 00:43:22 -0400265 __le32 opts1;
266 __le32 opts2;
267 __le32 addr1;
268 __le32 addr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
271struct media_info {
272 u16 type; /* DE_MEDIA_xxx */
273 u16 csr13;
274 u16 csr14;
275 u16 csr15;
276};
277
278struct ring_info {
279 struct sk_buff *skb;
280 dma_addr_t mapping;
281};
282
283struct de_private {
284 unsigned tx_head;
285 unsigned tx_tail;
286 unsigned rx_tail;
287
288 void __iomem *regs;
289 struct net_device *dev;
290 spinlock_t lock;
291
292 struct de_desc *rx_ring;
293 struct de_desc *tx_ring;
294 struct ring_info tx_skb[DE_TX_RING_SIZE];
295 struct ring_info rx_skb[DE_RX_RING_SIZE];
296 unsigned rx_buf_sz;
297 dma_addr_t ring_dma;
298
299 u32 msg_enable;
300
301 struct net_device_stats net_stats;
302
303 struct pci_dev *pdev;
304
305 u16 setup_frame[DE_SETUP_FRAME_WORDS];
306
307 u32 media_type;
308 u32 media_supported;
309 u32 media_advertise;
310 struct media_info media[DE_MAX_MEDIA];
311 struct timer_list media_timer;
312
313 u8 *ee_data;
314 unsigned board_idx;
315 unsigned de21040 : 1;
316 unsigned media_lock : 1;
317};
318
319
320static void de_set_rx_mode (struct net_device *dev);
321static void de_tx (struct de_private *de);
322static void de_clean_rings (struct de_private *de);
323static void de_media_interrupt (struct de_private *de, u32 status);
324static void de21040_media_timer (unsigned long data);
325static void de21041_media_timer (unsigned long data);
326static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media);
327
328
329static struct pci_device_id de_pci_tbl[] = {
330 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP,
331 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
332 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_PLUS,
333 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
334 { },
335};
336MODULE_DEVICE_TABLE(pci, de_pci_tbl);
337
338static const char * const media_name[DE_MAX_MEDIA] = {
339 "10baseT auto",
340 "BNC",
341 "AUI",
342 "10baseT-HD",
343 "10baseT-FD"
344};
345
346/* 21040 transceiver register settings:
347 * TP AUTO(unused), BNC(unused), AUI, TP, TP FD*/
348static u16 t21040_csr13[] = { 0, 0, 0x8F09, 0x8F01, 0x8F01, };
349static u16 t21040_csr14[] = { 0, 0, 0x0705, 0xFFFF, 0xFFFD, };
350static u16 t21040_csr15[] = { 0, 0, 0x0006, 0x0000, 0x0000, };
351
352/* 21041 transceiver register settings: TP AUTO, BNC, AUI, TP, TP FD*/
353static u16 t21041_csr13[] = { 0xEF01, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
354static u16 t21041_csr14[] = { 0xFFFF, 0xF7FD, 0xF7FD, 0x6F3F, 0x6F3D, };
355static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
356
357
358#define dr32(reg) readl(de->regs + (reg))
359#define dw32(reg,val) writel((val), de->regs + (reg))
360
361
362static void de_rx_err_acct (struct de_private *de, unsigned rx_tail,
363 u32 status, u32 len)
364{
365 if (netif_msg_rx_err (de))
366 printk (KERN_DEBUG
367 "%s: rx err, slot %d status 0x%x len %d\n",
368 de->dev->name, rx_tail, status, len);
369
370 if ((status & 0x38000300) != 0x0300) {
371 /* Ingore earlier buffers. */
372 if ((status & 0xffff) != 0x7fff) {
373 if (netif_msg_rx_err(de))
374 printk(KERN_WARNING "%s: Oversized Ethernet frame "
375 "spanned multiple buffers, status %8.8x!\n",
376 de->dev->name, status);
377 de->net_stats.rx_length_errors++;
378 }
379 } else if (status & RxError) {
380 /* There was a fatal error. */
381 de->net_stats.rx_errors++; /* end of a packet.*/
382 if (status & 0x0890) de->net_stats.rx_length_errors++;
383 if (status & RxErrCRC) de->net_stats.rx_crc_errors++;
384 if (status & RxErrFIFO) de->net_stats.rx_fifo_errors++;
385 }
386}
387
388static void de_rx (struct de_private *de)
389{
390 unsigned rx_tail = de->rx_tail;
391 unsigned rx_work = DE_RX_RING_SIZE;
392 unsigned drop = 0;
393 int rc;
394
Roel Kluin46578a692009-02-02 21:39:02 -0800395 while (--rx_work) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 u32 status, len;
397 dma_addr_t mapping;
398 struct sk_buff *skb, *copy_skb;
399 unsigned copying_skb, buflen;
400
401 skb = de->rx_skb[rx_tail].skb;
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +0100402 BUG_ON(!skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 rmb();
404 status = le32_to_cpu(de->rx_ring[rx_tail].opts1);
405 if (status & DescOwn)
406 break;
407
408 len = ((status >> 16) & 0x7ff) - 4;
409 mapping = de->rx_skb[rx_tail].mapping;
410
411 if (unlikely(drop)) {
412 de->net_stats.rx_dropped++;
413 goto rx_next;
414 }
415
416 if (unlikely((status & 0x38008300) != 0x0300)) {
417 de_rx_err_acct(de, rx_tail, status, len);
418 goto rx_next;
419 }
420
421 copying_skb = (len <= rx_copybreak);
422
423 if (unlikely(netif_msg_rx_status(de)))
424 printk(KERN_DEBUG "%s: rx slot %d status 0x%x len %d copying? %d\n",
425 de->dev->name, rx_tail, status, len,
426 copying_skb);
427
428 buflen = copying_skb ? (len + RX_OFFSET) : de->rx_buf_sz;
429 copy_skb = dev_alloc_skb (buflen);
430 if (unlikely(!copy_skb)) {
431 de->net_stats.rx_dropped++;
432 drop = 1;
433 rx_work = 100;
434 goto rx_next;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (!copying_skb) {
438 pci_unmap_single(de->pdev, mapping,
439 buflen, PCI_DMA_FROMDEVICE);
440 skb_put(skb, len);
441
442 mapping =
443 de->rx_skb[rx_tail].mapping =
David S. Miller689be432005-06-28 15:25:31 -0700444 pci_map_single(de->pdev, copy_skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 buflen, PCI_DMA_FROMDEVICE);
446 de->rx_skb[rx_tail].skb = copy_skb;
447 } else {
448 pci_dma_sync_single_for_cpu(de->pdev, mapping, len, PCI_DMA_FROMDEVICE);
449 skb_reserve(copy_skb, RX_OFFSET);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300450 skb_copy_from_linear_data(skb, skb_put(copy_skb, len),
451 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pci_dma_sync_single_for_device(de->pdev, mapping, len, PCI_DMA_FROMDEVICE);
453
454 /* We'll reuse the original ring buffer. */
455 skb = copy_skb;
456 }
457
458 skb->protocol = eth_type_trans (skb, de->dev);
459
460 de->net_stats.rx_packets++;
461 de->net_stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 rc = netif_rx (skb);
463 if (rc == NET_RX_DROP)
464 drop = 1;
465
466rx_next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (rx_tail == (DE_RX_RING_SIZE - 1))
468 de->rx_ring[rx_tail].opts2 =
469 cpu_to_le32(RingEnd | de->rx_buf_sz);
470 else
471 de->rx_ring[rx_tail].opts2 = cpu_to_le32(de->rx_buf_sz);
472 de->rx_ring[rx_tail].addr1 = cpu_to_le32(mapping);
Risto Suominenb991d2b2009-02-08 17:50:34 -0800473 wmb();
474 de->rx_ring[rx_tail].opts1 = cpu_to_le32(DescOwn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 rx_tail = NEXT_RX(rx_tail);
476 }
477
478 if (!rx_work)
479 printk(KERN_WARNING "%s: rx work limit reached\n", de->dev->name);
480
481 de->rx_tail = rx_tail;
482}
483
David Howells7d12e782006-10-05 14:55:46 +0100484static irqreturn_t de_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct net_device *dev = dev_instance;
Wang Chen8f15ea42008-11-12 23:38:36 -0800487 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u32 status;
489
490 status = dr32(MacStatus);
491 if ((!(status & (IntrOK|IntrErr))) || (status == 0xFFFF))
492 return IRQ_NONE;
493
494 if (netif_msg_intr(de))
495 printk(KERN_DEBUG "%s: intr, status %08x mode %08x desc %u/%u/%u\n",
496 dev->name, status, dr32(MacMode), de->rx_tail, de->tx_head, de->tx_tail);
497
498 dw32(MacStatus, status);
499
500 if (status & (RxIntr | RxEmpty)) {
501 de_rx(de);
502 if (status & RxEmpty)
503 dw32(RxPoll, NormalRxPoll);
504 }
505
506 spin_lock(&de->lock);
507
508 if (status & (TxIntr | TxEmpty))
509 de_tx(de);
510
511 if (status & (LinkPass | LinkFail))
512 de_media_interrupt(de, status);
513
514 spin_unlock(&de->lock);
515
516 if (status & PciErr) {
517 u16 pci_status;
518
519 pci_read_config_word(de->pdev, PCI_STATUS, &pci_status);
520 pci_write_config_word(de->pdev, PCI_STATUS, pci_status);
521 printk(KERN_ERR "%s: PCI bus error, status=%08x, PCI status=%04x\n",
522 dev->name, status, pci_status);
523 }
524
525 return IRQ_HANDLED;
526}
527
528static void de_tx (struct de_private *de)
529{
530 unsigned tx_head = de->tx_head;
531 unsigned tx_tail = de->tx_tail;
532
533 while (tx_tail != tx_head) {
534 struct sk_buff *skb;
535 u32 status;
536
537 rmb();
538 status = le32_to_cpu(de->tx_ring[tx_tail].opts1);
539 if (status & DescOwn)
540 break;
541
542 skb = de->tx_skb[tx_tail].skb;
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +0100543 BUG_ON(!skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (unlikely(skb == DE_DUMMY_SKB))
545 goto next;
546
547 if (unlikely(skb == DE_SETUP_SKB)) {
548 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
549 sizeof(de->setup_frame), PCI_DMA_TODEVICE);
550 goto next;
551 }
552
553 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
554 skb->len, PCI_DMA_TODEVICE);
555
556 if (status & LastFrag) {
557 if (status & TxError) {
558 if (netif_msg_tx_err(de))
559 printk(KERN_DEBUG "%s: tx err, status 0x%x\n",
560 de->dev->name, status);
561 de->net_stats.tx_errors++;
562 if (status & TxOWC)
563 de->net_stats.tx_window_errors++;
564 if (status & TxMaxCol)
565 de->net_stats.tx_aborted_errors++;
566 if (status & TxLinkFail)
567 de->net_stats.tx_carrier_errors++;
568 if (status & TxFIFOUnder)
569 de->net_stats.tx_fifo_errors++;
570 } else {
571 de->net_stats.tx_packets++;
572 de->net_stats.tx_bytes += skb->len;
573 if (netif_msg_tx_done(de))
574 printk(KERN_DEBUG "%s: tx done, slot %d\n", de->dev->name, tx_tail);
575 }
576 dev_kfree_skb_irq(skb);
577 }
578
579next:
580 de->tx_skb[tx_tail].skb = NULL;
581
582 tx_tail = NEXT_TX(tx_tail);
583 }
584
585 de->tx_tail = tx_tail;
586
587 if (netif_queue_stopped(de->dev) && (TX_BUFFS_AVAIL(de) > (DE_TX_RING_SIZE / 4)))
588 netif_wake_queue(de->dev);
589}
590
591static int de_start_xmit (struct sk_buff *skb, struct net_device *dev)
592{
Wang Chen8f15ea42008-11-12 23:38:36 -0800593 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned int entry, tx_free;
595 u32 mapping, len, flags = FirstFrag | LastFrag;
596 struct de_desc *txd;
597
598 spin_lock_irq(&de->lock);
599
600 tx_free = TX_BUFFS_AVAIL(de);
601 if (tx_free == 0) {
602 netif_stop_queue(dev);
603 spin_unlock_irq(&de->lock);
604 return 1;
605 }
606 tx_free--;
607
608 entry = de->tx_head;
609
610 txd = &de->tx_ring[entry];
611
612 len = skb->len;
613 mapping = pci_map_single(de->pdev, skb->data, len, PCI_DMA_TODEVICE);
614 if (entry == (DE_TX_RING_SIZE - 1))
615 flags |= RingEnd;
616 if (!tx_free || (tx_free == (DE_TX_RING_SIZE / 2)))
617 flags |= TxSwInt;
618 flags |= len;
619 txd->opts2 = cpu_to_le32(flags);
620 txd->addr1 = cpu_to_le32(mapping);
621
622 de->tx_skb[entry].skb = skb;
623 de->tx_skb[entry].mapping = mapping;
624 wmb();
625
626 txd->opts1 = cpu_to_le32(DescOwn);
627 wmb();
628
629 de->tx_head = NEXT_TX(entry);
630 if (netif_msg_tx_queued(de))
631 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
632 dev->name, entry, skb->len);
633
634 if (tx_free == 0)
635 netif_stop_queue(dev);
636
637 spin_unlock_irq(&de->lock);
638
639 /* Trigger an immediate transmit demand. */
640 dw32(TxPoll, NormalTxPoll);
641 dev->trans_start = jiffies;
642
643 return 0;
644}
645
646/* Set or clear the multicast filter for this adaptor.
647 Note that we only use exclusion around actually queueing the
648 new frame, not around filling de->setup_frame. This is non-deterministic
649 when re-entered but still correct. */
650
651#undef set_bit_le
652#define set_bit_le(i,p) do { ((char *)(p))[(i)/8] |= (1<<((i)%8)); } while(0)
653
654static void build_setup_frame_hash(u16 *setup_frm, struct net_device *dev)
655{
Wang Chen8f15ea42008-11-12 23:38:36 -0800656 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 u16 hash_table[32];
658 struct dev_mc_list *mclist;
659 int i;
660 u16 *eaddrs;
661
662 memset(hash_table, 0, sizeof(hash_table));
663 set_bit_le(255, hash_table); /* Broadcast entry */
664 /* This should work on big-endian machines as well. */
665 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
666 i++, mclist = mclist->next) {
667 int index = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff;
668
669 set_bit_le(index, hash_table);
670
671 for (i = 0; i < 32; i++) {
672 *setup_frm++ = hash_table[i];
673 *setup_frm++ = hash_table[i];
674 }
675 setup_frm = &de->setup_frame[13*6];
676 }
677
678 /* Fill the final entry with our physical address. */
679 eaddrs = (u16 *)dev->dev_addr;
680 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
681 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
682 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
683}
684
685static void build_setup_frame_perfect(u16 *setup_frm, struct net_device *dev)
686{
Wang Chen8f15ea42008-11-12 23:38:36 -0800687 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct dev_mc_list *mclist;
689 int i;
690 u16 *eaddrs;
691
692 /* We have <= 14 addresses so we can use the wonderful
693 16 address perfect filtering of the Tulip. */
694 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
695 i++, mclist = mclist->next) {
696 eaddrs = (u16 *)mclist->dmi_addr;
697 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
698 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
699 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
700 }
701 /* Fill the unused entries with the broadcast address. */
702 memset(setup_frm, 0xff, (15-i)*12);
703 setup_frm = &de->setup_frame[15*6];
704
705 /* Fill the final entry with our physical address. */
706 eaddrs = (u16 *)dev->dev_addr;
707 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
708 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
709 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
710}
711
712
713static void __de_set_rx_mode (struct net_device *dev)
714{
Wang Chen8f15ea42008-11-12 23:38:36 -0800715 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 u32 macmode;
717 unsigned int entry;
718 u32 mapping;
719 struct de_desc *txd;
720 struct de_desc *dummy_txd = NULL;
721
722 macmode = dr32(MacMode) & ~(AcceptAllMulticast | AcceptAllPhys);
723
724 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
725 macmode |= AcceptAllMulticast | AcceptAllPhys;
726 goto out;
727 }
728
729 if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
730 /* Too many to filter well -- accept all multicasts. */
731 macmode |= AcceptAllMulticast;
732 goto out;
733 }
734
735 /* Note that only the low-address shortword of setup_frame is valid!
736 The values are doubled for big-endian architectures. */
737 if (dev->mc_count > 14) /* Must use a multicast hash table. */
738 build_setup_frame_hash (de->setup_frame, dev);
739 else
740 build_setup_frame_perfect (de->setup_frame, dev);
741
742 /*
743 * Now add this frame to the Tx list.
744 */
745
746 entry = de->tx_head;
747
748 /* Avoid a chip errata by prefixing a dummy entry. */
749 if (entry != 0) {
750 de->tx_skb[entry].skb = DE_DUMMY_SKB;
751
752 dummy_txd = &de->tx_ring[entry];
753 dummy_txd->opts2 = (entry == (DE_TX_RING_SIZE - 1)) ?
754 cpu_to_le32(RingEnd) : 0;
755 dummy_txd->addr1 = 0;
756
757 /* Must set DescOwned later to avoid race with chip */
758
759 entry = NEXT_TX(entry);
760 }
761
762 de->tx_skb[entry].skb = DE_SETUP_SKB;
763 de->tx_skb[entry].mapping = mapping =
764 pci_map_single (de->pdev, de->setup_frame,
765 sizeof (de->setup_frame), PCI_DMA_TODEVICE);
766
767 /* Put the setup frame on the Tx list. */
768 txd = &de->tx_ring[entry];
769 if (entry == (DE_TX_RING_SIZE - 1))
770 txd->opts2 = cpu_to_le32(SetupFrame | RingEnd | sizeof (de->setup_frame));
771 else
772 txd->opts2 = cpu_to_le32(SetupFrame | sizeof (de->setup_frame));
773 txd->addr1 = cpu_to_le32(mapping);
774 wmb();
775
776 txd->opts1 = cpu_to_le32(DescOwn);
777 wmb();
778
779 if (dummy_txd) {
780 dummy_txd->opts1 = cpu_to_le32(DescOwn);
781 wmb();
782 }
783
784 de->tx_head = NEXT_TX(entry);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (TX_BUFFS_AVAIL(de) == 0)
787 netif_stop_queue(dev);
788
789 /* Trigger an immediate transmit demand. */
790 dw32(TxPoll, NormalTxPoll);
791
792out:
793 if (macmode != dr32(MacMode))
794 dw32(MacMode, macmode);
795}
796
797static void de_set_rx_mode (struct net_device *dev)
798{
799 unsigned long flags;
Wang Chen8f15ea42008-11-12 23:38:36 -0800800 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 spin_lock_irqsave (&de->lock, flags);
803 __de_set_rx_mode(dev);
804 spin_unlock_irqrestore (&de->lock, flags);
805}
806
807static inline void de_rx_missed(struct de_private *de, u32 rx_missed)
808{
809 if (unlikely(rx_missed & RxMissedOver))
810 de->net_stats.rx_missed_errors += RxMissedMask;
811 else
812 de->net_stats.rx_missed_errors += (rx_missed & RxMissedMask);
813}
814
815static void __de_get_stats(struct de_private *de)
816{
817 u32 tmp = dr32(RxMissed); /* self-clearing */
818
819 de_rx_missed(de, tmp);
820}
821
822static struct net_device_stats *de_get_stats(struct net_device *dev)
823{
Wang Chen8f15ea42008-11-12 23:38:36 -0800824 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* The chip only need report frame silently dropped. */
827 spin_lock_irq(&de->lock);
828 if (netif_running(dev) && netif_device_present(dev))
829 __de_get_stats(de);
830 spin_unlock_irq(&de->lock);
831
832 return &de->net_stats;
833}
834
835static inline int de_is_running (struct de_private *de)
836{
837 return (dr32(MacStatus) & (RxState | TxState)) ? 1 : 0;
838}
839
840static void de_stop_rxtx (struct de_private *de)
841{
842 u32 macmode;
Grant Grundler69cac982008-03-08 18:33:16 -0700843 unsigned int i = 1300/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 macmode = dr32(MacMode);
846 if (macmode & RxTx) {
847 dw32(MacMode, macmode & ~RxTx);
848 dr32(MacMode);
849 }
850
Grant Grundler69cac982008-03-08 18:33:16 -0700851 /* wait until in-flight frame completes.
852 * Max time @ 10BT: 1500*8b/10Mbps == 1200us (+ 100us margin)
853 * Typically expect this loop to end in < 50 us on 100BT.
854 */
855 while (--i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!de_is_running(de))
857 return;
Grant Grundler69cac982008-03-08 18:33:16 -0700858 udelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 printk(KERN_WARNING "%s: timeout expired stopping DMA\n", de->dev->name);
862}
863
864static inline void de_start_rxtx (struct de_private *de)
865{
866 u32 macmode;
867
868 macmode = dr32(MacMode);
869 if ((macmode & RxTx) != RxTx) {
870 dw32(MacMode, macmode | RxTx);
871 dr32(MacMode);
872 }
873}
874
875static void de_stop_hw (struct de_private *de)
876{
877
878 udelay(5);
879 dw32(IntrMask, 0);
880
881 de_stop_rxtx(de);
882
883 dw32(MacStatus, dr32(MacStatus));
884
885 udelay(10);
886
887 de->rx_tail = 0;
888 de->tx_head = de->tx_tail = 0;
889}
890
891static void de_link_up(struct de_private *de)
892{
893 if (!netif_carrier_ok(de->dev)) {
894 netif_carrier_on(de->dev);
895 if (netif_msg_link(de))
896 printk(KERN_INFO "%s: link up, media %s\n",
897 de->dev->name, media_name[de->media_type]);
898 }
899}
900
901static void de_link_down(struct de_private *de)
902{
903 if (netif_carrier_ok(de->dev)) {
904 netif_carrier_off(de->dev);
905 if (netif_msg_link(de))
906 printk(KERN_INFO "%s: link down\n", de->dev->name);
907 }
908}
909
910static void de_set_media (struct de_private *de)
911{
912 unsigned media = de->media_type;
913 u32 macmode = dr32(MacMode);
914
Ondrej Zaryf25f0f82008-02-25 18:45:46 +0100915 if (de_is_running(de))
916 printk(KERN_WARNING "%s: chip is running while changing media!\n", de->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (de->de21040)
919 dw32(CSR11, FULL_DUPLEX_MAGIC);
920 dw32(CSR13, 0); /* Reset phy */
921 dw32(CSR14, de->media[media].csr14);
922 dw32(CSR15, de->media[media].csr15);
923 dw32(CSR13, de->media[media].csr13);
924
925 /* must delay 10ms before writing to other registers,
926 * especially CSR6
927 */
928 mdelay(10);
929
930 if (media == DE_MEDIA_TP_FD)
931 macmode |= FullDuplex;
932 else
933 macmode &= ~FullDuplex;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (netif_msg_link(de)) {
936 printk(KERN_INFO "%s: set link %s\n"
937 KERN_INFO "%s: mode 0x%x, sia 0x%x,0x%x,0x%x,0x%x\n"
938 KERN_INFO "%s: set mode 0x%x, set sia 0x%x,0x%x,0x%x\n",
939 de->dev->name, media_name[media],
940 de->dev->name, dr32(MacMode), dr32(SIAStatus),
941 dr32(CSR13), dr32(CSR14), dr32(CSR15),
942 de->dev->name, macmode, de->media[media].csr13,
943 de->media[media].csr14, de->media[media].csr15);
944 }
945 if (macmode != dr32(MacMode))
946 dw32(MacMode, macmode);
947}
948
949static void de_next_media (struct de_private *de, u32 *media,
950 unsigned int n_media)
951{
952 unsigned int i;
953
954 for (i = 0; i < n_media; i++) {
955 if (de_ok_to_advertise(de, media[i])) {
956 de->media_type = media[i];
957 return;
958 }
959 }
960}
961
962static void de21040_media_timer (unsigned long data)
963{
964 struct de_private *de = (struct de_private *) data;
965 struct net_device *dev = de->dev;
966 u32 status = dr32(SIAStatus);
967 unsigned int carrier;
968 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 carrier = (status & NetCxnErr) ? 0 : 1;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (carrier) {
973 if (de->media_type != DE_MEDIA_AUI && (status & LinkFailStatus))
974 goto no_link_yet;
975
976 de->media_timer.expires = jiffies + DE_TIMER_LINK;
977 add_timer(&de->media_timer);
978 if (!netif_carrier_ok(dev))
979 de_link_up(de);
980 else
981 if (netif_msg_timer(de))
982 printk(KERN_INFO "%s: %s link ok, status %x\n",
983 dev->name, media_name[de->media_type],
984 status);
985 return;
986 }
987
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400988 de_link_down(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (de->media_lock)
991 return;
992
993 if (de->media_type == DE_MEDIA_AUI) {
994 u32 next_state = DE_MEDIA_TP;
995 de_next_media(de, &next_state, 1);
996 } else {
997 u32 next_state = DE_MEDIA_AUI;
998 de_next_media(de, &next_state, 1);
999 }
1000
1001 spin_lock_irqsave(&de->lock, flags);
1002 de_stop_rxtx(de);
1003 spin_unlock_irqrestore(&de->lock, flags);
1004 de_set_media(de);
1005 de_start_rxtx(de);
1006
1007no_link_yet:
1008 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1009 add_timer(&de->media_timer);
1010
1011 if (netif_msg_timer(de))
1012 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1013 dev->name, media_name[de->media_type], status);
1014}
1015
1016static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media)
1017{
1018 switch (new_media) {
1019 case DE_MEDIA_TP_AUTO:
1020 if (!(de->media_advertise & ADVERTISED_Autoneg))
1021 return 0;
1022 if (!(de->media_advertise & (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full)))
1023 return 0;
1024 break;
1025 case DE_MEDIA_BNC:
1026 if (!(de->media_advertise & ADVERTISED_BNC))
1027 return 0;
1028 break;
1029 case DE_MEDIA_AUI:
1030 if (!(de->media_advertise & ADVERTISED_AUI))
1031 return 0;
1032 break;
1033 case DE_MEDIA_TP:
1034 if (!(de->media_advertise & ADVERTISED_10baseT_Half))
1035 return 0;
1036 break;
1037 case DE_MEDIA_TP_FD:
1038 if (!(de->media_advertise & ADVERTISED_10baseT_Full))
1039 return 0;
1040 break;
1041 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return 1;
1044}
1045
1046static void de21041_media_timer (unsigned long data)
1047{
1048 struct de_private *de = (struct de_private *) data;
1049 struct net_device *dev = de->dev;
1050 u32 status = dr32(SIAStatus);
1051 unsigned int carrier;
1052 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 carrier = (status & NetCxnErr) ? 0 : 1;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (carrier) {
1057 if ((de->media_type == DE_MEDIA_TP_AUTO ||
1058 de->media_type == DE_MEDIA_TP ||
1059 de->media_type == DE_MEDIA_TP_FD) &&
1060 (status & LinkFailStatus))
1061 goto no_link_yet;
1062
1063 de->media_timer.expires = jiffies + DE_TIMER_LINK;
1064 add_timer(&de->media_timer);
1065 if (!netif_carrier_ok(dev))
1066 de_link_up(de);
1067 else
1068 if (netif_msg_timer(de))
1069 printk(KERN_INFO "%s: %s link ok, mode %x status %x\n",
1070 dev->name, media_name[de->media_type],
1071 dr32(MacMode), status);
1072 return;
1073 }
1074
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001075 de_link_down(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /* if media type locked, don't switch media */
1078 if (de->media_lock)
1079 goto set_media;
1080
1081 /* if activity detected, use that as hint for new media type */
1082 if (status & NonselPortActive) {
1083 unsigned int have_media = 1;
1084
1085 /* if AUI/BNC selected, then activity is on TP port */
1086 if (de->media_type == DE_MEDIA_AUI ||
1087 de->media_type == DE_MEDIA_BNC) {
1088 if (de_ok_to_advertise(de, DE_MEDIA_TP_AUTO))
1089 de->media_type = DE_MEDIA_TP_AUTO;
1090 else
1091 have_media = 0;
1092 }
1093
1094 /* TP selected. If there is only TP and BNC, then it's BNC */
1095 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_BNC) &&
1096 de_ok_to_advertise(de, DE_MEDIA_BNC))
1097 de->media_type = DE_MEDIA_BNC;
1098
1099 /* TP selected. If there is only TP and AUI, then it's AUI */
1100 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_AUI) &&
1101 de_ok_to_advertise(de, DE_MEDIA_AUI))
1102 de->media_type = DE_MEDIA_AUI;
1103
1104 /* otherwise, ignore the hint */
1105 else
1106 have_media = 0;
1107
1108 if (have_media)
1109 goto set_media;
1110 }
1111
1112 /*
1113 * Absent or ambiguous activity hint, move to next advertised
1114 * media state. If de->media_type is left unchanged, this
1115 * simply resets the PHY and reloads the current media settings.
1116 */
1117 if (de->media_type == DE_MEDIA_AUI) {
1118 u32 next_states[] = { DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1119 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1120 } else if (de->media_type == DE_MEDIA_BNC) {
1121 u32 next_states[] = { DE_MEDIA_TP_AUTO, DE_MEDIA_AUI };
1122 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1123 } else {
1124 u32 next_states[] = { DE_MEDIA_AUI, DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1125 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1126 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128set_media:
1129 spin_lock_irqsave(&de->lock, flags);
1130 de_stop_rxtx(de);
1131 spin_unlock_irqrestore(&de->lock, flags);
1132 de_set_media(de);
1133 de_start_rxtx(de);
1134
1135no_link_yet:
1136 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1137 add_timer(&de->media_timer);
1138
1139 if (netif_msg_timer(de))
1140 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1141 dev->name, media_name[de->media_type], status);
1142}
1143
1144static void de_media_interrupt (struct de_private *de, u32 status)
1145{
1146 if (status & LinkPass) {
1147 de_link_up(de);
1148 mod_timer(&de->media_timer, jiffies + DE_TIMER_LINK);
1149 return;
1150 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001151
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +01001152 BUG_ON(!(status & LinkFail));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 if (netif_carrier_ok(de->dev)) {
1155 de_link_down(de);
1156 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1157 }
1158}
1159
1160static int de_reset_mac (struct de_private *de)
1161{
1162 u32 status, tmp;
1163
1164 /*
1165 * Reset MAC. de4x5.c and tulip.c examined for "advice"
1166 * in this area.
1167 */
1168
1169 if (dr32(BusMode) == 0xffffffff)
1170 return -EBUSY;
1171
1172 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
1173 dw32 (BusMode, CmdReset);
1174 mdelay (1);
1175
1176 dw32 (BusMode, de_bus_mode);
1177 mdelay (1);
1178
1179 for (tmp = 0; tmp < 5; tmp++) {
1180 dr32 (BusMode);
1181 mdelay (1);
1182 }
1183
1184 mdelay (1);
1185
1186 status = dr32(MacStatus);
1187 if (status & (RxState | TxState))
1188 return -EBUSY;
1189 if (status == 0xffffffff)
1190 return -ENODEV;
1191 return 0;
1192}
1193
1194static void de_adapter_wake (struct de_private *de)
1195{
1196 u32 pmctl;
1197
1198 if (de->de21040)
1199 return;
1200
1201 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1202 if (pmctl & PM_Mask) {
1203 pmctl &= ~PM_Mask;
1204 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1205
1206 /* de4x5.c delays, so we do too */
1207 msleep(10);
1208 }
1209}
1210
1211static void de_adapter_sleep (struct de_private *de)
1212{
1213 u32 pmctl;
1214
1215 if (de->de21040)
1216 return;
1217
1218 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1219 pmctl |= PM_Sleep;
1220 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1221}
1222
1223static int de_init_hw (struct de_private *de)
1224{
1225 struct net_device *dev = de->dev;
1226 u32 macmode;
1227 int rc;
1228
1229 de_adapter_wake(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 macmode = dr32(MacMode) & ~MacModeClear;
1232
1233 rc = de_reset_mac(de);
1234 if (rc)
1235 return rc;
1236
1237 de_set_media(de); /* reset phy */
1238
1239 dw32(RxRingAddr, de->ring_dma);
1240 dw32(TxRingAddr, de->ring_dma + (sizeof(struct de_desc) * DE_RX_RING_SIZE));
1241
1242 dw32(MacMode, RxTx | macmode);
1243
1244 dr32(RxMissed); /* self-clearing */
1245
1246 dw32(IntrMask, de_intr_mask);
1247
1248 de_set_rx_mode(dev);
1249
1250 return 0;
1251}
1252
1253static int de_refill_rx (struct de_private *de)
1254{
1255 unsigned i;
1256
1257 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1258 struct sk_buff *skb;
1259
1260 skb = dev_alloc_skb(de->rx_buf_sz);
1261 if (!skb)
1262 goto err_out;
1263
1264 skb->dev = de->dev;
1265
1266 de->rx_skb[i].mapping = pci_map_single(de->pdev,
David S. Miller689be432005-06-28 15:25:31 -07001267 skb->data, de->rx_buf_sz, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 de->rx_skb[i].skb = skb;
1269
1270 de->rx_ring[i].opts1 = cpu_to_le32(DescOwn);
1271 if (i == (DE_RX_RING_SIZE - 1))
1272 de->rx_ring[i].opts2 =
1273 cpu_to_le32(RingEnd | de->rx_buf_sz);
1274 else
1275 de->rx_ring[i].opts2 = cpu_to_le32(de->rx_buf_sz);
1276 de->rx_ring[i].addr1 = cpu_to_le32(de->rx_skb[i].mapping);
1277 de->rx_ring[i].addr2 = 0;
1278 }
1279
1280 return 0;
1281
1282err_out:
1283 de_clean_rings(de);
1284 return -ENOMEM;
1285}
1286
1287static int de_init_rings (struct de_private *de)
1288{
1289 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1290 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1291
1292 de->rx_tail = 0;
1293 de->tx_head = de->tx_tail = 0;
1294
1295 return de_refill_rx (de);
1296}
1297
1298static int de_alloc_rings (struct de_private *de)
1299{
1300 de->rx_ring = pci_alloc_consistent(de->pdev, DE_RING_BYTES, &de->ring_dma);
1301 if (!de->rx_ring)
1302 return -ENOMEM;
1303 de->tx_ring = &de->rx_ring[DE_RX_RING_SIZE];
1304 return de_init_rings(de);
1305}
1306
1307static void de_clean_rings (struct de_private *de)
1308{
1309 unsigned i;
1310
1311 memset(de->rx_ring, 0, sizeof(struct de_desc) * DE_RX_RING_SIZE);
1312 de->rx_ring[DE_RX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1313 wmb();
1314 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1315 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1316 wmb();
1317
1318 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1319 if (de->rx_skb[i].skb) {
1320 pci_unmap_single(de->pdev, de->rx_skb[i].mapping,
1321 de->rx_buf_sz, PCI_DMA_FROMDEVICE);
1322 dev_kfree_skb(de->rx_skb[i].skb);
1323 }
1324 }
1325
1326 for (i = 0; i < DE_TX_RING_SIZE; i++) {
1327 struct sk_buff *skb = de->tx_skb[i].skb;
1328 if ((skb) && (skb != DE_DUMMY_SKB)) {
1329 if (skb != DE_SETUP_SKB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 de->net_stats.tx_dropped++;
1331 pci_unmap_single(de->pdev,
1332 de->tx_skb[i].mapping,
1333 skb->len, PCI_DMA_TODEVICE);
Eric Sesterhenn5185c7c2006-03-22 22:30:34 +01001334 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 } else {
1336 pci_unmap_single(de->pdev,
1337 de->tx_skb[i].mapping,
1338 sizeof(de->setup_frame),
1339 PCI_DMA_TODEVICE);
1340 }
1341 }
1342 }
1343
1344 memset(&de->rx_skb, 0, sizeof(struct ring_info) * DE_RX_RING_SIZE);
1345 memset(&de->tx_skb, 0, sizeof(struct ring_info) * DE_TX_RING_SIZE);
1346}
1347
1348static void de_free_rings (struct de_private *de)
1349{
1350 de_clean_rings(de);
1351 pci_free_consistent(de->pdev, DE_RING_BYTES, de->rx_ring, de->ring_dma);
1352 de->rx_ring = NULL;
1353 de->tx_ring = NULL;
1354}
1355
1356static int de_open (struct net_device *dev)
1357{
Wang Chen8f15ea42008-11-12 23:38:36 -08001358 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (netif_msg_ifup(de))
1362 printk(KERN_DEBUG "%s: enabling interface\n", dev->name);
1363
1364 de->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1365
1366 rc = de_alloc_rings(de);
1367 if (rc) {
1368 printk(KERN_ERR "%s: ring allocation failure, err=%d\n",
1369 dev->name, rc);
1370 return rc;
1371 }
1372
Francois Romieu3f735b72006-03-08 22:41:15 +01001373 dw32(IntrMask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001375 rc = request_irq(dev->irq, de_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (rc) {
1377 printk(KERN_ERR "%s: IRQ %d request failure, err=%d\n",
1378 dev->name, dev->irq, rc);
Francois Romieu3f735b72006-03-08 22:41:15 +01001379 goto err_out_free;
1380 }
1381
1382 rc = de_init_hw(de);
1383 if (rc) {
1384 printk(KERN_ERR "%s: h/w init failure, err=%d\n",
1385 dev->name, rc);
1386 goto err_out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
1389 netif_start_queue(dev);
1390 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1391
1392 return 0;
1393
Francois Romieu3f735b72006-03-08 22:41:15 +01001394err_out_free_irq:
1395 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396err_out_free:
1397 de_free_rings(de);
1398 return rc;
1399}
1400
1401static int de_close (struct net_device *dev)
1402{
Wang Chen8f15ea42008-11-12 23:38:36 -08001403 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned long flags;
1405
1406 if (netif_msg_ifdown(de))
1407 printk(KERN_DEBUG "%s: disabling interface\n", dev->name);
1408
1409 del_timer_sync(&de->media_timer);
1410
1411 spin_lock_irqsave(&de->lock, flags);
1412 de_stop_hw(de);
1413 netif_stop_queue(dev);
1414 netif_carrier_off(dev);
1415 spin_unlock_irqrestore(&de->lock, flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 free_irq(dev->irq, dev);
1418
1419 de_free_rings(de);
1420 de_adapter_sleep(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return 0;
1422}
1423
1424static void de_tx_timeout (struct net_device *dev)
1425{
Wang Chen8f15ea42008-11-12 23:38:36 -08001426 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 printk(KERN_DEBUG "%s: NIC status %08x mode %08x sia %08x desc %u/%u/%u\n",
1429 dev->name, dr32(MacStatus), dr32(MacMode), dr32(SIAStatus),
1430 de->rx_tail, de->tx_head, de->tx_tail);
1431
1432 del_timer_sync(&de->media_timer);
1433
1434 disable_irq(dev->irq);
1435 spin_lock_irq(&de->lock);
1436
1437 de_stop_hw(de);
1438 netif_stop_queue(dev);
1439 netif_carrier_off(dev);
1440
1441 spin_unlock_irq(&de->lock);
1442 enable_irq(dev->irq);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Update the error counts. */
1445 __de_get_stats(de);
1446
1447 synchronize_irq(dev->irq);
1448 de_clean_rings(de);
1449
Francois Romieu39bf4292006-03-08 22:45:52 +01001450 de_init_rings(de);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 de_init_hw(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 netif_wake_queue(dev);
1455}
1456
1457static void __de_get_regs(struct de_private *de, u8 *buf)
1458{
1459 int i;
1460 u32 *rbuf = (u32 *)buf;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* read all CSRs */
1463 for (i = 0; i < DE_NUM_REGS; i++)
1464 rbuf[i] = dr32(i * 8);
1465
1466 /* handle self-clearing RxMissed counter, CSR8 */
1467 de_rx_missed(de, rbuf[8]);
1468}
1469
1470static int __de_get_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1471{
1472 ecmd->supported = de->media_supported;
1473 ecmd->transceiver = XCVR_INTERNAL;
1474 ecmd->phy_address = 0;
1475 ecmd->advertising = de->media_advertise;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 switch (de->media_type) {
1478 case DE_MEDIA_AUI:
1479 ecmd->port = PORT_AUI;
1480 ecmd->speed = 5;
1481 break;
1482 case DE_MEDIA_BNC:
1483 ecmd->port = PORT_BNC;
1484 ecmd->speed = 2;
1485 break;
1486 default:
1487 ecmd->port = PORT_TP;
1488 ecmd->speed = SPEED_10;
1489 break;
1490 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (dr32(MacMode) & FullDuplex)
1493 ecmd->duplex = DUPLEX_FULL;
1494 else
1495 ecmd->duplex = DUPLEX_HALF;
1496
1497 if (de->media_lock)
1498 ecmd->autoneg = AUTONEG_DISABLE;
1499 else
1500 ecmd->autoneg = AUTONEG_ENABLE;
1501
1502 /* ignore maxtxpkt, maxrxpkt for now */
1503
1504 return 0;
1505}
1506
1507static int __de_set_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1508{
1509 u32 new_media;
1510 unsigned int media_lock;
1511
1512 if (ecmd->speed != SPEED_10 && ecmd->speed != 5 && ecmd->speed != 2)
1513 return -EINVAL;
1514 if (de->de21040 && ecmd->speed == 2)
1515 return -EINVAL;
1516 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1517 return -EINVAL;
1518 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI && ecmd->port != PORT_BNC)
1519 return -EINVAL;
1520 if (de->de21040 && ecmd->port == PORT_BNC)
1521 return -EINVAL;
1522 if (ecmd->transceiver != XCVR_INTERNAL)
1523 return -EINVAL;
1524 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1525 return -EINVAL;
1526 if (ecmd->advertising & ~de->media_supported)
1527 return -EINVAL;
1528 if (ecmd->autoneg == AUTONEG_ENABLE &&
1529 (!(ecmd->advertising & ADVERTISED_Autoneg)))
1530 return -EINVAL;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 switch (ecmd->port) {
1533 case PORT_AUI:
1534 new_media = DE_MEDIA_AUI;
1535 if (!(ecmd->advertising & ADVERTISED_AUI))
1536 return -EINVAL;
1537 break;
1538 case PORT_BNC:
1539 new_media = DE_MEDIA_BNC;
1540 if (!(ecmd->advertising & ADVERTISED_BNC))
1541 return -EINVAL;
1542 break;
1543 default:
1544 if (ecmd->autoneg == AUTONEG_ENABLE)
1545 new_media = DE_MEDIA_TP_AUTO;
1546 else if (ecmd->duplex == DUPLEX_FULL)
1547 new_media = DE_MEDIA_TP_FD;
1548 else
1549 new_media = DE_MEDIA_TP;
1550 if (!(ecmd->advertising & ADVERTISED_TP))
1551 return -EINVAL;
1552 if (!(ecmd->advertising & (ADVERTISED_10baseT_Full | ADVERTISED_10baseT_Half)))
1553 return -EINVAL;
1554 break;
1555 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 media_lock = (ecmd->autoneg == AUTONEG_ENABLE) ? 0 : 1;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if ((new_media == de->media_type) &&
1560 (media_lock == de->media_lock) &&
1561 (ecmd->advertising == de->media_advertise))
1562 return 0; /* nothing to change */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 de_link_down(de);
1565 de_stop_rxtx(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 de->media_type = new_media;
1568 de->media_lock = media_lock;
1569 de->media_advertise = ecmd->advertising;
1570 de_set_media(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return 0;
1573}
1574
1575static void de_get_drvinfo (struct net_device *dev,struct ethtool_drvinfo *info)
1576{
Wang Chen8f15ea42008-11-12 23:38:36 -08001577 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 strcpy (info->driver, DRV_NAME);
1580 strcpy (info->version, DRV_VERSION);
1581 strcpy (info->bus_info, pci_name(de->pdev));
1582 info->eedump_len = DE_EEPROM_SIZE;
1583}
1584
1585static int de_get_regs_len(struct net_device *dev)
1586{
1587 return DE_REGS_SIZE;
1588}
1589
1590static int de_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1591{
Wang Chen8f15ea42008-11-12 23:38:36 -08001592 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int rc;
1594
1595 spin_lock_irq(&de->lock);
1596 rc = __de_get_settings(de, ecmd);
1597 spin_unlock_irq(&de->lock);
1598
1599 return rc;
1600}
1601
1602static int de_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1603{
Wang Chen8f15ea42008-11-12 23:38:36 -08001604 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int rc;
1606
1607 spin_lock_irq(&de->lock);
1608 rc = __de_set_settings(de, ecmd);
1609 spin_unlock_irq(&de->lock);
1610
1611 return rc;
1612}
1613
1614static u32 de_get_msglevel(struct net_device *dev)
1615{
Wang Chen8f15ea42008-11-12 23:38:36 -08001616 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 return de->msg_enable;
1619}
1620
1621static void de_set_msglevel(struct net_device *dev, u32 msglvl)
1622{
Wang Chen8f15ea42008-11-12 23:38:36 -08001623 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 de->msg_enable = msglvl;
1626}
1627
1628static int de_get_eeprom(struct net_device *dev,
1629 struct ethtool_eeprom *eeprom, u8 *data)
1630{
Wang Chen8f15ea42008-11-12 23:38:36 -08001631 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (!de->ee_data)
1634 return -EOPNOTSUPP;
1635 if ((eeprom->offset != 0) || (eeprom->magic != 0) ||
1636 (eeprom->len != DE_EEPROM_SIZE))
1637 return -EINVAL;
1638 memcpy(data, de->ee_data, eeprom->len);
1639
1640 return 0;
1641}
1642
1643static int de_nway_reset(struct net_device *dev)
1644{
Wang Chen8f15ea42008-11-12 23:38:36 -08001645 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 u32 status;
1647
1648 if (de->media_type != DE_MEDIA_TP_AUTO)
1649 return -EINVAL;
1650 if (netif_carrier_ok(de->dev))
1651 de_link_down(de);
1652
1653 status = dr32(SIAStatus);
1654 dw32(SIAStatus, (status & ~NWayState) | NWayRestart);
1655 if (netif_msg_link(de))
1656 printk(KERN_INFO "%s: link nway restart, status %x,%x\n",
1657 de->dev->name, status, dr32(SIAStatus));
1658 return 0;
1659}
1660
1661static void de_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1662 void *data)
1663{
Wang Chen8f15ea42008-11-12 23:38:36 -08001664 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 regs->version = (DE_REGS_VER << 2) | de->de21040;
1667
1668 spin_lock_irq(&de->lock);
1669 __de_get_regs(de, data);
1670 spin_unlock_irq(&de->lock);
1671}
1672
Jeff Garzik7282d492006-09-13 14:30:00 -04001673static const struct ethtool_ops de_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .get_drvinfo = de_get_drvinfo,
1676 .get_regs_len = de_get_regs_len,
1677 .get_settings = de_get_settings,
1678 .set_settings = de_set_settings,
1679 .get_msglevel = de_get_msglevel,
1680 .set_msglevel = de_set_msglevel,
1681 .get_eeprom = de_get_eeprom,
1682 .nway_reset = de_nway_reset,
1683 .get_regs = de_get_regs,
1684};
1685
Prarit Bhargava4c44fd02007-03-06 02:42:00 -08001686static void __devinit de21040_get_mac_address (struct de_private *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 unsigned i;
1689
1690 dw32 (ROMCmd, 0); /* Reset the pointer with a dummy write. */
Martin Langerbc0da3f2008-10-13 18:49:38 -07001691 udelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 for (i = 0; i < 6; i++) {
1694 int value, boguscnt = 100000;
Hannes Ederec1d1ebb2008-12-26 00:07:45 -08001695 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 value = dr32(ROMCmd);
Hannes Ederec1d1ebb2008-12-26 00:07:45 -08001697 } while (value < 0 && --boguscnt > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 de->dev->dev_addr[i] = value;
1699 udelay(1);
1700 if (boguscnt <= 0)
1701 printk(KERN_WARNING PFX "timeout reading 21040 MAC address byte %u\n", i);
1702 }
1703}
1704
Prarit Bhargava4c44fd02007-03-06 02:42:00 -08001705static void __devinit de21040_get_media_info(struct de_private *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 unsigned int i;
1708
1709 de->media_type = DE_MEDIA_TP;
1710 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full |
1711 SUPPORTED_10baseT_Half | SUPPORTED_AUI;
1712 de->media_advertise = de->media_supported;
1713
1714 for (i = 0; i < DE_MAX_MEDIA; i++) {
1715 switch (i) {
1716 case DE_MEDIA_AUI:
1717 case DE_MEDIA_TP:
1718 case DE_MEDIA_TP_FD:
1719 de->media[i].type = i;
1720 de->media[i].csr13 = t21040_csr13[i];
1721 de->media[i].csr14 = t21040_csr14[i];
1722 de->media[i].csr15 = t21040_csr15[i];
1723 break;
1724 default:
1725 de->media[i].type = DE_MEDIA_INVALID;
1726 break;
1727 }
1728 }
1729}
1730
1731/* Note: this routine returns extra data bits for size detection. */
Helge Deller4a1d2d82006-10-06 12:12:34 -06001732static unsigned __devinit tulip_read_eeprom(void __iomem *regs, int location, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 int i;
1735 unsigned retval = 0;
1736 void __iomem *ee_addr = regs + ROMCmd;
1737 int read_cmd = location | (EE_READ_CMD << addr_len);
1738
1739 writel(EE_ENB & ~EE_CS, ee_addr);
1740 writel(EE_ENB, ee_addr);
1741
1742 /* Shift the read command bits out. */
1743 for (i = 4 + addr_len; i >= 0; i--) {
1744 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1745 writel(EE_ENB | dataval, ee_addr);
1746 readl(ee_addr);
1747 writel(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1748 readl(ee_addr);
1749 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1750 }
1751 writel(EE_ENB, ee_addr);
1752 readl(ee_addr);
1753
1754 for (i = 16; i > 0; i--) {
1755 writel(EE_ENB | EE_SHIFT_CLK, ee_addr);
1756 readl(ee_addr);
1757 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1758 writel(EE_ENB, ee_addr);
1759 readl(ee_addr);
1760 }
1761
1762 /* Terminate the EEPROM access. */
1763 writel(EE_ENB & ~EE_CS, ee_addr);
1764 return retval;
1765}
1766
Prarit Bhargava4c44fd02007-03-06 02:42:00 -08001767static void __devinit de21041_get_srom_info (struct de_private *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 unsigned i, sa_offset = 0, ofs;
1770 u8 ee_data[DE_EEPROM_SIZE + 6] = {};
1771 unsigned ee_addr_size = tulip_read_eeprom(de->regs, 0xff, 8) & 0x40000 ? 8 : 6;
1772 struct de_srom_info_leaf *il;
1773 void *bufp;
1774
1775 /* download entire eeprom */
1776 for (i = 0; i < DE_EEPROM_WORDS; i++)
Al Viroc559a5b2007-08-23 00:43:22 -04001777 ((__le16 *)ee_data)[i] =
1778 cpu_to_le16(tulip_read_eeprom(de->regs, i, ee_addr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* DEC now has a specification but early board makers
1781 just put the address in the first EEPROM locations. */
1782 /* This does memcmp(eedata, eedata+16, 8) */
Ralf Baechlebc053d42005-10-10 14:50:46 +01001783
1784#ifndef CONFIG_MIPS_COBALT
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 for (i = 0; i < 8; i ++)
1787 if (ee_data[i] != ee_data[16+i])
1788 sa_offset = 20;
1789
Ralf Baechlebc053d42005-10-10 14:50:46 +01001790#endif
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* store MAC address */
1793 for (i = 0; i < 6; i ++)
1794 de->dev->dev_addr[i] = ee_data[i + sa_offset];
1795
1796 /* get offset of controller 0 info leaf. ignore 2nd byte. */
1797 ofs = ee_data[SROMC0InfoLeaf];
1798 if (ofs >= (sizeof(ee_data) - sizeof(struct de_srom_info_leaf) - sizeof(struct de_srom_media_block)))
1799 goto bad_srom;
1800
1801 /* get pointer to info leaf */
1802 il = (struct de_srom_info_leaf *) &ee_data[ofs];
1803
1804 /* paranoia checks */
1805 if (il->n_blocks == 0)
1806 goto bad_srom;
1807 if ((sizeof(ee_data) - ofs) <
1808 (sizeof(struct de_srom_info_leaf) + (sizeof(struct de_srom_media_block) * il->n_blocks)))
1809 goto bad_srom;
1810
1811 /* get default media type */
Harvey Harrison445854f2008-05-28 16:51:04 -07001812 switch (get_unaligned(&il->default_media)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 case 0x0001: de->media_type = DE_MEDIA_BNC; break;
1814 case 0x0002: de->media_type = DE_MEDIA_AUI; break;
1815 case 0x0204: de->media_type = DE_MEDIA_TP_FD; break;
1816 default: de->media_type = DE_MEDIA_TP_AUTO; break;
1817 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (netif_msg_probe(de))
1820 printk(KERN_INFO "de%d: SROM leaf offset %u, default media %s\n",
1821 de->board_idx, ofs,
1822 media_name[de->media_type]);
1823
1824 /* init SIA register values to defaults */
1825 for (i = 0; i < DE_MAX_MEDIA; i++) {
1826 de->media[i].type = DE_MEDIA_INVALID;
1827 de->media[i].csr13 = 0xffff;
1828 de->media[i].csr14 = 0xffff;
1829 de->media[i].csr15 = 0xffff;
1830 }
1831
1832 /* parse media blocks to see what medias are supported,
1833 * and if any custom CSR values are provided
1834 */
1835 bufp = ((void *)il) + sizeof(*il);
1836 for (i = 0; i < il->n_blocks; i++) {
1837 struct de_srom_media_block *ib = bufp;
1838 unsigned idx;
1839
1840 /* index based on media type in media block */
1841 switch(ib->opts & MediaBlockMask) {
1842 case 0: /* 10baseT */
1843 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half
1844 | SUPPORTED_Autoneg;
1845 idx = DE_MEDIA_TP;
1846 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1847 break;
1848 case 1: /* BNC */
1849 de->media_supported |= SUPPORTED_BNC;
1850 idx = DE_MEDIA_BNC;
1851 break;
1852 case 2: /* AUI */
1853 de->media_supported |= SUPPORTED_AUI;
1854 idx = DE_MEDIA_AUI;
1855 break;
1856 case 4: /* 10baseT-FD */
1857 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full
1858 | SUPPORTED_Autoneg;
1859 idx = DE_MEDIA_TP_FD;
1860 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1861 break;
1862 default:
1863 goto bad_srom;
1864 }
1865
1866 de->media[idx].type = idx;
1867
1868 if (netif_msg_probe(de))
1869 printk(KERN_INFO "de%d: media block #%u: %s",
1870 de->board_idx, i,
1871 media_name[de->media[idx].type]);
1872
1873 bufp += sizeof (ib->opts);
1874
1875 if (ib->opts & MediaCustomCSRs) {
Harvey Harrison445854f2008-05-28 16:51:04 -07001876 de->media[idx].csr13 = get_unaligned(&ib->csr13);
1877 de->media[idx].csr14 = get_unaligned(&ib->csr14);
1878 de->media[idx].csr15 = get_unaligned(&ib->csr15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 bufp += sizeof(ib->csr13) + sizeof(ib->csr14) +
1880 sizeof(ib->csr15);
1881
1882 if (netif_msg_probe(de))
1883 printk(" (%x,%x,%x)\n",
1884 de->media[idx].csr13,
1885 de->media[idx].csr14,
1886 de->media[idx].csr15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 } else if (netif_msg_probe(de))
1889 printk("\n");
1890
1891 if (bufp > ((void *)&ee_data[DE_EEPROM_SIZE - 3]))
1892 break;
1893 }
1894
1895 de->media_advertise = de->media_supported;
1896
1897fill_defaults:
1898 /* fill in defaults, for cases where custom CSRs not used */
1899 for (i = 0; i < DE_MAX_MEDIA; i++) {
1900 if (de->media[i].csr13 == 0xffff)
1901 de->media[i].csr13 = t21041_csr13[i];
1902 if (de->media[i].csr14 == 0xffff)
1903 de->media[i].csr14 = t21041_csr14[i];
1904 if (de->media[i].csr15 == 0xffff)
1905 de->media[i].csr15 = t21041_csr15[i];
1906 }
1907
Eric Sesterhennc3a9392e2006-10-23 22:20:15 +02001908 de->ee_data = kmemdup(&ee_data[0], DE_EEPROM_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 return;
1911
1912bad_srom:
1913 /* for error cases, it's ok to assume we support all these */
1914 for (i = 0; i < DE_MAX_MEDIA; i++)
1915 de->media[i].type = i;
1916 de->media_supported =
1917 SUPPORTED_10baseT_Half |
1918 SUPPORTED_10baseT_Full |
1919 SUPPORTED_Autoneg |
1920 SUPPORTED_TP |
1921 SUPPORTED_AUI |
1922 SUPPORTED_BNC;
1923 goto fill_defaults;
1924}
1925
Stephen Hemminger90d87432009-01-07 17:59:47 -08001926static const struct net_device_ops de_netdev_ops = {
1927 .ndo_open = de_open,
1928 .ndo_stop = de_close,
1929 .ndo_set_multicast_list = de_set_rx_mode,
1930 .ndo_start_xmit = de_start_xmit,
1931 .ndo_get_stats = de_get_stats,
1932 .ndo_tx_timeout = de_tx_timeout,
1933 .ndo_change_mtu = eth_change_mtu,
1934 .ndo_set_mac_address = eth_mac_addr,
1935 .ndo_validate_addr = eth_validate_addr,
1936};
1937
Helge Deller4a1d2d82006-10-06 12:12:34 -06001938static int __devinit de_init_one (struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 const struct pci_device_id *ent)
1940{
1941 struct net_device *dev;
1942 struct de_private *de;
1943 int rc;
1944 void __iomem *regs;
Jeff Garzikafc70972005-08-31 06:11:16 -04001945 unsigned long pciaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 static int board_idx = -1;
1947
1948 board_idx++;
1949
1950#ifndef MODULE
1951 if (board_idx == 0)
1952 printk("%s", version);
1953#endif
1954
1955 /* allocate a new ethernet device structure, and fill in defaults */
1956 dev = alloc_etherdev(sizeof(struct de_private));
1957 if (!dev)
1958 return -ENOMEM;
1959
Stephen Hemminger90d87432009-01-07 17:59:47 -08001960 dev->netdev_ops = &de_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 dev->ethtool_ops = &de_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 dev->watchdog_timeo = TX_TIMEOUT;
1964
Wang Chen8f15ea42008-11-12 23:38:36 -08001965 de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 de->de21040 = ent->driver_data == 0 ? 1 : 0;
1967 de->pdev = pdev;
1968 de->dev = dev;
1969 de->msg_enable = (debug < 0 ? DE_DEF_MSG_ENABLE : debug);
1970 de->board_idx = board_idx;
1971 spin_lock_init (&de->lock);
1972 init_timer(&de->media_timer);
1973 if (de->de21040)
1974 de->media_timer.function = de21040_media_timer;
1975 else
1976 de->media_timer.function = de21041_media_timer;
1977 de->media_timer.data = (unsigned long) de;
1978
1979 netif_carrier_off(dev);
1980 netif_stop_queue(dev);
1981
1982 /* wake up device, assign resources */
1983 rc = pci_enable_device(pdev);
1984 if (rc)
1985 goto err_out_free;
1986
1987 /* reserve PCI resources to ensure driver atomicity */
1988 rc = pci_request_regions(pdev, DRV_NAME);
1989 if (rc)
1990 goto err_out_disable;
1991
1992 /* check for invalid IRQ value */
1993 if (pdev->irq < 2) {
1994 rc = -EIO;
1995 printk(KERN_ERR PFX "invalid irq (%d) for pci dev %s\n",
1996 pdev->irq, pci_name(pdev));
1997 goto err_out_res;
1998 }
1999
2000 dev->irq = pdev->irq;
2001
2002 /* obtain and check validity of PCI I/O address */
2003 pciaddr = pci_resource_start(pdev, 1);
2004 if (!pciaddr) {
2005 rc = -EIO;
2006 printk(KERN_ERR PFX "no MMIO resource for pci dev %s\n",
2007 pci_name(pdev));
2008 goto err_out_res;
2009 }
2010 if (pci_resource_len(pdev, 1) < DE_REGS_SIZE) {
2011 rc = -EIO;
Greg Kroah-Hartman7c7459d2006-06-12 15:13:08 -07002012 printk(KERN_ERR PFX "MMIO resource (%llx) too small on pci dev %s\n",
2013 (unsigned long long)pci_resource_len(pdev, 1), pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 goto err_out_res;
2015 }
2016
2017 /* remap CSR registers */
2018 regs = ioremap_nocache(pciaddr, DE_REGS_SIZE);
2019 if (!regs) {
2020 rc = -EIO;
Greg Kroah-Hartman7c7459d2006-06-12 15:13:08 -07002021 printk(KERN_ERR PFX "Cannot map PCI MMIO (%llx@%lx) on pci dev %s\n",
2022 (unsigned long long)pci_resource_len(pdev, 1),
2023 pciaddr, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto err_out_res;
2025 }
2026 dev->base_addr = (unsigned long) regs;
2027 de->regs = regs;
2028
2029 de_adapter_wake(de);
2030
2031 /* make sure hardware is not running */
2032 rc = de_reset_mac(de);
2033 if (rc) {
2034 printk(KERN_ERR PFX "Cannot reset MAC, pci dev %s\n",
2035 pci_name(pdev));
2036 goto err_out_iomap;
2037 }
2038
2039 /* get MAC address, initialize default media type and
2040 * get list of supported media
2041 */
2042 if (de->de21040) {
2043 de21040_get_mac_address(de);
2044 de21040_get_media_info(de);
2045 } else {
2046 de21041_get_srom_info(de);
2047 }
2048
2049 /* register new network interface with kernel */
2050 rc = register_netdev(dev);
2051 if (rc)
2052 goto err_out_iomap;
2053
2054 /* print info about board and interface just registered */
Johannes Berge1749612008-10-27 15:59:26 -07002055 printk (KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 dev->name,
2057 de->de21040 ? "21040" : "21041",
2058 dev->base_addr,
Johannes Berge1749612008-10-27 15:59:26 -07002059 dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 dev->irq);
2061
2062 pci_set_drvdata(pdev, dev);
2063
2064 /* enable busmastering */
2065 pci_set_master(pdev);
2066
2067 /* put adapter to sleep */
2068 de_adapter_sleep(de);
2069
2070 return 0;
2071
2072err_out_iomap:
Jesper Juhlb4558ea2005-10-28 16:53:13 -04002073 kfree(de->ee_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 iounmap(regs);
2075err_out_res:
2076 pci_release_regions(pdev);
2077err_out_disable:
2078 pci_disable_device(pdev);
2079err_out_free:
2080 free_netdev(dev);
2081 return rc;
2082}
2083
Helge Deller4a1d2d82006-10-06 12:12:34 -06002084static void __devexit de_remove_one (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002087 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +01002089 BUG_ON(!dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 unregister_netdev(dev);
Jesper Juhlb4558ea2005-10-28 16:53:13 -04002091 kfree(de->ee_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 iounmap(de->regs);
2093 pci_release_regions(pdev);
2094 pci_disable_device(pdev);
2095 pci_set_drvdata(pdev, NULL);
2096 free_netdev(dev);
2097}
2098
2099#ifdef CONFIG_PM
2100
Pavel Machek05adc3b2005-04-16 15:25:25 -07002101static int de_suspend (struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 struct net_device *dev = pci_get_drvdata (pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002104 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 rtnl_lock();
2107 if (netif_running (dev)) {
2108 del_timer_sync(&de->media_timer);
2109
2110 disable_irq(dev->irq);
2111 spin_lock_irq(&de->lock);
2112
2113 de_stop_hw(de);
2114 netif_stop_queue(dev);
2115 netif_device_detach(dev);
2116 netif_carrier_off(dev);
2117
2118 spin_unlock_irq(&de->lock);
2119 enable_irq(dev->irq);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* Update the error counts. */
2122 __de_get_stats(de);
2123
2124 synchronize_irq(dev->irq);
2125 de_clean_rings(de);
2126
2127 de_adapter_sleep(de);
2128 pci_disable_device(pdev);
2129 } else {
2130 netif_device_detach(dev);
2131 }
2132 rtnl_unlock();
2133 return 0;
2134}
2135
2136static int de_resume (struct pci_dev *pdev)
2137{
2138 struct net_device *dev = pci_get_drvdata (pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002139 struct de_private *de = netdev_priv(dev);
Valerie Henson9f486ae2006-09-08 11:15:41 -07002140 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 rtnl_lock();
2143 if (netif_device_present(dev))
2144 goto out;
Valerie Henson9f486ae2006-09-08 11:15:41 -07002145 if (!netif_running(dev))
2146 goto out_attach;
2147 if ((retval = pci_enable_device(pdev))) {
2148 printk (KERN_ERR "%s: pci_enable_device failed in resume\n",
2149 dev->name);
2150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
Valerie Henson9f486ae2006-09-08 11:15:41 -07002152 de_init_hw(de);
2153out_attach:
2154 netif_device_attach(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155out:
2156 rtnl_unlock();
2157 return 0;
2158}
2159
2160#endif /* CONFIG_PM */
2161
2162static struct pci_driver de_driver = {
2163 .name = DRV_NAME,
2164 .id_table = de_pci_tbl,
2165 .probe = de_init_one,
Helge Deller4a1d2d82006-10-06 12:12:34 -06002166 .remove = __devexit_p(de_remove_one),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#ifdef CONFIG_PM
2168 .suspend = de_suspend,
2169 .resume = de_resume,
2170#endif
2171};
2172
2173static int __init de_init (void)
2174{
2175#ifdef MODULE
2176 printk("%s", version);
2177#endif
Jeff Garzik29917622006-08-19 17:48:59 -04002178 return pci_register_driver(&de_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181static void __exit de_exit (void)
2182{
2183 pci_unregister_driver (&de_driver);
2184}
2185
2186module_init(de_init);
2187module_exit(de_exit);