blob: 0bf2114738bea563192d58bec9c9347b43112516 [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:
467 de->rx_ring[rx_tail].opts1 = cpu_to_le32(DescOwn);
468 if (rx_tail == (DE_RX_RING_SIZE - 1))
469 de->rx_ring[rx_tail].opts2 =
470 cpu_to_le32(RingEnd | de->rx_buf_sz);
471 else
472 de->rx_ring[rx_tail].opts2 = cpu_to_le32(de->rx_buf_sz);
473 de->rx_ring[rx_tail].addr1 = cpu_to_le32(mapping);
474 rx_tail = NEXT_RX(rx_tail);
475 }
476
477 if (!rx_work)
478 printk(KERN_WARNING "%s: rx work limit reached\n", de->dev->name);
479
480 de->rx_tail = rx_tail;
481}
482
David Howells7d12e782006-10-05 14:55:46 +0100483static irqreturn_t de_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct net_device *dev = dev_instance;
Wang Chen8f15ea42008-11-12 23:38:36 -0800486 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 u32 status;
488
489 status = dr32(MacStatus);
490 if ((!(status & (IntrOK|IntrErr))) || (status == 0xFFFF))
491 return IRQ_NONE;
492
493 if (netif_msg_intr(de))
494 printk(KERN_DEBUG "%s: intr, status %08x mode %08x desc %u/%u/%u\n",
495 dev->name, status, dr32(MacMode), de->rx_tail, de->tx_head, de->tx_tail);
496
497 dw32(MacStatus, status);
498
499 if (status & (RxIntr | RxEmpty)) {
500 de_rx(de);
501 if (status & RxEmpty)
502 dw32(RxPoll, NormalRxPoll);
503 }
504
505 spin_lock(&de->lock);
506
507 if (status & (TxIntr | TxEmpty))
508 de_tx(de);
509
510 if (status & (LinkPass | LinkFail))
511 de_media_interrupt(de, status);
512
513 spin_unlock(&de->lock);
514
515 if (status & PciErr) {
516 u16 pci_status;
517
518 pci_read_config_word(de->pdev, PCI_STATUS, &pci_status);
519 pci_write_config_word(de->pdev, PCI_STATUS, pci_status);
520 printk(KERN_ERR "%s: PCI bus error, status=%08x, PCI status=%04x\n",
521 dev->name, status, pci_status);
522 }
523
524 return IRQ_HANDLED;
525}
526
527static void de_tx (struct de_private *de)
528{
529 unsigned tx_head = de->tx_head;
530 unsigned tx_tail = de->tx_tail;
531
532 while (tx_tail != tx_head) {
533 struct sk_buff *skb;
534 u32 status;
535
536 rmb();
537 status = le32_to_cpu(de->tx_ring[tx_tail].opts1);
538 if (status & DescOwn)
539 break;
540
541 skb = de->tx_skb[tx_tail].skb;
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +0100542 BUG_ON(!skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (unlikely(skb == DE_DUMMY_SKB))
544 goto next;
545
546 if (unlikely(skb == DE_SETUP_SKB)) {
547 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
548 sizeof(de->setup_frame), PCI_DMA_TODEVICE);
549 goto next;
550 }
551
552 pci_unmap_single(de->pdev, de->tx_skb[tx_tail].mapping,
553 skb->len, PCI_DMA_TODEVICE);
554
555 if (status & LastFrag) {
556 if (status & TxError) {
557 if (netif_msg_tx_err(de))
558 printk(KERN_DEBUG "%s: tx err, status 0x%x\n",
559 de->dev->name, status);
560 de->net_stats.tx_errors++;
561 if (status & TxOWC)
562 de->net_stats.tx_window_errors++;
563 if (status & TxMaxCol)
564 de->net_stats.tx_aborted_errors++;
565 if (status & TxLinkFail)
566 de->net_stats.tx_carrier_errors++;
567 if (status & TxFIFOUnder)
568 de->net_stats.tx_fifo_errors++;
569 } else {
570 de->net_stats.tx_packets++;
571 de->net_stats.tx_bytes += skb->len;
572 if (netif_msg_tx_done(de))
573 printk(KERN_DEBUG "%s: tx done, slot %d\n", de->dev->name, tx_tail);
574 }
575 dev_kfree_skb_irq(skb);
576 }
577
578next:
579 de->tx_skb[tx_tail].skb = NULL;
580
581 tx_tail = NEXT_TX(tx_tail);
582 }
583
584 de->tx_tail = tx_tail;
585
586 if (netif_queue_stopped(de->dev) && (TX_BUFFS_AVAIL(de) > (DE_TX_RING_SIZE / 4)))
587 netif_wake_queue(de->dev);
588}
589
590static int de_start_xmit (struct sk_buff *skb, struct net_device *dev)
591{
Wang Chen8f15ea42008-11-12 23:38:36 -0800592 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned int entry, tx_free;
594 u32 mapping, len, flags = FirstFrag | LastFrag;
595 struct de_desc *txd;
596
597 spin_lock_irq(&de->lock);
598
599 tx_free = TX_BUFFS_AVAIL(de);
600 if (tx_free == 0) {
601 netif_stop_queue(dev);
602 spin_unlock_irq(&de->lock);
603 return 1;
604 }
605 tx_free--;
606
607 entry = de->tx_head;
608
609 txd = &de->tx_ring[entry];
610
611 len = skb->len;
612 mapping = pci_map_single(de->pdev, skb->data, len, PCI_DMA_TODEVICE);
613 if (entry == (DE_TX_RING_SIZE - 1))
614 flags |= RingEnd;
615 if (!tx_free || (tx_free == (DE_TX_RING_SIZE / 2)))
616 flags |= TxSwInt;
617 flags |= len;
618 txd->opts2 = cpu_to_le32(flags);
619 txd->addr1 = cpu_to_le32(mapping);
620
621 de->tx_skb[entry].skb = skb;
622 de->tx_skb[entry].mapping = mapping;
623 wmb();
624
625 txd->opts1 = cpu_to_le32(DescOwn);
626 wmb();
627
628 de->tx_head = NEXT_TX(entry);
629 if (netif_msg_tx_queued(de))
630 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
631 dev->name, entry, skb->len);
632
633 if (tx_free == 0)
634 netif_stop_queue(dev);
635
636 spin_unlock_irq(&de->lock);
637
638 /* Trigger an immediate transmit demand. */
639 dw32(TxPoll, NormalTxPoll);
640 dev->trans_start = jiffies;
641
642 return 0;
643}
644
645/* Set or clear the multicast filter for this adaptor.
646 Note that we only use exclusion around actually queueing the
647 new frame, not around filling de->setup_frame. This is non-deterministic
648 when re-entered but still correct. */
649
650#undef set_bit_le
651#define set_bit_le(i,p) do { ((char *)(p))[(i)/8] |= (1<<((i)%8)); } while(0)
652
653static void build_setup_frame_hash(u16 *setup_frm, struct net_device *dev)
654{
Wang Chen8f15ea42008-11-12 23:38:36 -0800655 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u16 hash_table[32];
657 struct dev_mc_list *mclist;
658 int i;
659 u16 *eaddrs;
660
661 memset(hash_table, 0, sizeof(hash_table));
662 set_bit_le(255, hash_table); /* Broadcast entry */
663 /* This should work on big-endian machines as well. */
664 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
665 i++, mclist = mclist->next) {
666 int index = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff;
667
668 set_bit_le(index, hash_table);
669
670 for (i = 0; i < 32; i++) {
671 *setup_frm++ = hash_table[i];
672 *setup_frm++ = hash_table[i];
673 }
674 setup_frm = &de->setup_frame[13*6];
675 }
676
677 /* Fill the final entry with our physical address. */
678 eaddrs = (u16 *)dev->dev_addr;
679 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
680 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
681 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
682}
683
684static void build_setup_frame_perfect(u16 *setup_frm, struct net_device *dev)
685{
Wang Chen8f15ea42008-11-12 23:38:36 -0800686 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct dev_mc_list *mclist;
688 int i;
689 u16 *eaddrs;
690
691 /* We have <= 14 addresses so we can use the wonderful
692 16 address perfect filtering of the Tulip. */
693 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
694 i++, mclist = mclist->next) {
695 eaddrs = (u16 *)mclist->dmi_addr;
696 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
697 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
698 *setup_frm++ = *eaddrs; *setup_frm++ = *eaddrs++;
699 }
700 /* Fill the unused entries with the broadcast address. */
701 memset(setup_frm, 0xff, (15-i)*12);
702 setup_frm = &de->setup_frame[15*6];
703
704 /* Fill the final entry with our physical address. */
705 eaddrs = (u16 *)dev->dev_addr;
706 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
707 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
708 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
709}
710
711
712static void __de_set_rx_mode (struct net_device *dev)
713{
Wang Chen8f15ea42008-11-12 23:38:36 -0800714 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 u32 macmode;
716 unsigned int entry;
717 u32 mapping;
718 struct de_desc *txd;
719 struct de_desc *dummy_txd = NULL;
720
721 macmode = dr32(MacMode) & ~(AcceptAllMulticast | AcceptAllPhys);
722
723 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
724 macmode |= AcceptAllMulticast | AcceptAllPhys;
725 goto out;
726 }
727
728 if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
729 /* Too many to filter well -- accept all multicasts. */
730 macmode |= AcceptAllMulticast;
731 goto out;
732 }
733
734 /* Note that only the low-address shortword of setup_frame is valid!
735 The values are doubled for big-endian architectures. */
736 if (dev->mc_count > 14) /* Must use a multicast hash table. */
737 build_setup_frame_hash (de->setup_frame, dev);
738 else
739 build_setup_frame_perfect (de->setup_frame, dev);
740
741 /*
742 * Now add this frame to the Tx list.
743 */
744
745 entry = de->tx_head;
746
747 /* Avoid a chip errata by prefixing a dummy entry. */
748 if (entry != 0) {
749 de->tx_skb[entry].skb = DE_DUMMY_SKB;
750
751 dummy_txd = &de->tx_ring[entry];
752 dummy_txd->opts2 = (entry == (DE_TX_RING_SIZE - 1)) ?
753 cpu_to_le32(RingEnd) : 0;
754 dummy_txd->addr1 = 0;
755
756 /* Must set DescOwned later to avoid race with chip */
757
758 entry = NEXT_TX(entry);
759 }
760
761 de->tx_skb[entry].skb = DE_SETUP_SKB;
762 de->tx_skb[entry].mapping = mapping =
763 pci_map_single (de->pdev, de->setup_frame,
764 sizeof (de->setup_frame), PCI_DMA_TODEVICE);
765
766 /* Put the setup frame on the Tx list. */
767 txd = &de->tx_ring[entry];
768 if (entry == (DE_TX_RING_SIZE - 1))
769 txd->opts2 = cpu_to_le32(SetupFrame | RingEnd | sizeof (de->setup_frame));
770 else
771 txd->opts2 = cpu_to_le32(SetupFrame | sizeof (de->setup_frame));
772 txd->addr1 = cpu_to_le32(mapping);
773 wmb();
774
775 txd->opts1 = cpu_to_le32(DescOwn);
776 wmb();
777
778 if (dummy_txd) {
779 dummy_txd->opts1 = cpu_to_le32(DescOwn);
780 wmb();
781 }
782
783 de->tx_head = NEXT_TX(entry);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (TX_BUFFS_AVAIL(de) == 0)
786 netif_stop_queue(dev);
787
788 /* Trigger an immediate transmit demand. */
789 dw32(TxPoll, NormalTxPoll);
790
791out:
792 if (macmode != dr32(MacMode))
793 dw32(MacMode, macmode);
794}
795
796static void de_set_rx_mode (struct net_device *dev)
797{
798 unsigned long flags;
Wang Chen8f15ea42008-11-12 23:38:36 -0800799 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 spin_lock_irqsave (&de->lock, flags);
802 __de_set_rx_mode(dev);
803 spin_unlock_irqrestore (&de->lock, flags);
804}
805
806static inline void de_rx_missed(struct de_private *de, u32 rx_missed)
807{
808 if (unlikely(rx_missed & RxMissedOver))
809 de->net_stats.rx_missed_errors += RxMissedMask;
810 else
811 de->net_stats.rx_missed_errors += (rx_missed & RxMissedMask);
812}
813
814static void __de_get_stats(struct de_private *de)
815{
816 u32 tmp = dr32(RxMissed); /* self-clearing */
817
818 de_rx_missed(de, tmp);
819}
820
821static struct net_device_stats *de_get_stats(struct net_device *dev)
822{
Wang Chen8f15ea42008-11-12 23:38:36 -0800823 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* The chip only need report frame silently dropped. */
826 spin_lock_irq(&de->lock);
827 if (netif_running(dev) && netif_device_present(dev))
828 __de_get_stats(de);
829 spin_unlock_irq(&de->lock);
830
831 return &de->net_stats;
832}
833
834static inline int de_is_running (struct de_private *de)
835{
836 return (dr32(MacStatus) & (RxState | TxState)) ? 1 : 0;
837}
838
839static void de_stop_rxtx (struct de_private *de)
840{
841 u32 macmode;
Grant Grundler69cac982008-03-08 18:33:16 -0700842 unsigned int i = 1300/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 macmode = dr32(MacMode);
845 if (macmode & RxTx) {
846 dw32(MacMode, macmode & ~RxTx);
847 dr32(MacMode);
848 }
849
Grant Grundler69cac982008-03-08 18:33:16 -0700850 /* wait until in-flight frame completes.
851 * Max time @ 10BT: 1500*8b/10Mbps == 1200us (+ 100us margin)
852 * Typically expect this loop to end in < 50 us on 100BT.
853 */
854 while (--i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!de_is_running(de))
856 return;
Grant Grundler69cac982008-03-08 18:33:16 -0700857 udelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 printk(KERN_WARNING "%s: timeout expired stopping DMA\n", de->dev->name);
861}
862
863static inline void de_start_rxtx (struct de_private *de)
864{
865 u32 macmode;
866
867 macmode = dr32(MacMode);
868 if ((macmode & RxTx) != RxTx) {
869 dw32(MacMode, macmode | RxTx);
870 dr32(MacMode);
871 }
872}
873
874static void de_stop_hw (struct de_private *de)
875{
876
877 udelay(5);
878 dw32(IntrMask, 0);
879
880 de_stop_rxtx(de);
881
882 dw32(MacStatus, dr32(MacStatus));
883
884 udelay(10);
885
886 de->rx_tail = 0;
887 de->tx_head = de->tx_tail = 0;
888}
889
890static void de_link_up(struct de_private *de)
891{
892 if (!netif_carrier_ok(de->dev)) {
893 netif_carrier_on(de->dev);
894 if (netif_msg_link(de))
895 printk(KERN_INFO "%s: link up, media %s\n",
896 de->dev->name, media_name[de->media_type]);
897 }
898}
899
900static void de_link_down(struct de_private *de)
901{
902 if (netif_carrier_ok(de->dev)) {
903 netif_carrier_off(de->dev);
904 if (netif_msg_link(de))
905 printk(KERN_INFO "%s: link down\n", de->dev->name);
906 }
907}
908
909static void de_set_media (struct de_private *de)
910{
911 unsigned media = de->media_type;
912 u32 macmode = dr32(MacMode);
913
Ondrej Zaryf25f0f82008-02-25 18:45:46 +0100914 if (de_is_running(de))
915 printk(KERN_WARNING "%s: chip is running while changing media!\n", de->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 if (de->de21040)
918 dw32(CSR11, FULL_DUPLEX_MAGIC);
919 dw32(CSR13, 0); /* Reset phy */
920 dw32(CSR14, de->media[media].csr14);
921 dw32(CSR15, de->media[media].csr15);
922 dw32(CSR13, de->media[media].csr13);
923
924 /* must delay 10ms before writing to other registers,
925 * especially CSR6
926 */
927 mdelay(10);
928
929 if (media == DE_MEDIA_TP_FD)
930 macmode |= FullDuplex;
931 else
932 macmode &= ~FullDuplex;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (netif_msg_link(de)) {
935 printk(KERN_INFO "%s: set link %s\n"
936 KERN_INFO "%s: mode 0x%x, sia 0x%x,0x%x,0x%x,0x%x\n"
937 KERN_INFO "%s: set mode 0x%x, set sia 0x%x,0x%x,0x%x\n",
938 de->dev->name, media_name[media],
939 de->dev->name, dr32(MacMode), dr32(SIAStatus),
940 dr32(CSR13), dr32(CSR14), dr32(CSR15),
941 de->dev->name, macmode, de->media[media].csr13,
942 de->media[media].csr14, de->media[media].csr15);
943 }
944 if (macmode != dr32(MacMode))
945 dw32(MacMode, macmode);
946}
947
948static void de_next_media (struct de_private *de, u32 *media,
949 unsigned int n_media)
950{
951 unsigned int i;
952
953 for (i = 0; i < n_media; i++) {
954 if (de_ok_to_advertise(de, media[i])) {
955 de->media_type = media[i];
956 return;
957 }
958 }
959}
960
961static void de21040_media_timer (unsigned long data)
962{
963 struct de_private *de = (struct de_private *) data;
964 struct net_device *dev = de->dev;
965 u32 status = dr32(SIAStatus);
966 unsigned int carrier;
967 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 carrier = (status & NetCxnErr) ? 0 : 1;
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (carrier) {
972 if (de->media_type != DE_MEDIA_AUI && (status & LinkFailStatus))
973 goto no_link_yet;
974
975 de->media_timer.expires = jiffies + DE_TIMER_LINK;
976 add_timer(&de->media_timer);
977 if (!netif_carrier_ok(dev))
978 de_link_up(de);
979 else
980 if (netif_msg_timer(de))
981 printk(KERN_INFO "%s: %s link ok, status %x\n",
982 dev->name, media_name[de->media_type],
983 status);
984 return;
985 }
986
Jeff Garzikf3b197a2006-05-26 21:39:03 -0400987 de_link_down(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (de->media_lock)
990 return;
991
992 if (de->media_type == DE_MEDIA_AUI) {
993 u32 next_state = DE_MEDIA_TP;
994 de_next_media(de, &next_state, 1);
995 } else {
996 u32 next_state = DE_MEDIA_AUI;
997 de_next_media(de, &next_state, 1);
998 }
999
1000 spin_lock_irqsave(&de->lock, flags);
1001 de_stop_rxtx(de);
1002 spin_unlock_irqrestore(&de->lock, flags);
1003 de_set_media(de);
1004 de_start_rxtx(de);
1005
1006no_link_yet:
1007 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1008 add_timer(&de->media_timer);
1009
1010 if (netif_msg_timer(de))
1011 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1012 dev->name, media_name[de->media_type], status);
1013}
1014
1015static unsigned int de_ok_to_advertise (struct de_private *de, u32 new_media)
1016{
1017 switch (new_media) {
1018 case DE_MEDIA_TP_AUTO:
1019 if (!(de->media_advertise & ADVERTISED_Autoneg))
1020 return 0;
1021 if (!(de->media_advertise & (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full)))
1022 return 0;
1023 break;
1024 case DE_MEDIA_BNC:
1025 if (!(de->media_advertise & ADVERTISED_BNC))
1026 return 0;
1027 break;
1028 case DE_MEDIA_AUI:
1029 if (!(de->media_advertise & ADVERTISED_AUI))
1030 return 0;
1031 break;
1032 case DE_MEDIA_TP:
1033 if (!(de->media_advertise & ADVERTISED_10baseT_Half))
1034 return 0;
1035 break;
1036 case DE_MEDIA_TP_FD:
1037 if (!(de->media_advertise & ADVERTISED_10baseT_Full))
1038 return 0;
1039 break;
1040 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 1;
1043}
1044
1045static void de21041_media_timer (unsigned long data)
1046{
1047 struct de_private *de = (struct de_private *) data;
1048 struct net_device *dev = de->dev;
1049 u32 status = dr32(SIAStatus);
1050 unsigned int carrier;
1051 unsigned long flags;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 carrier = (status & NetCxnErr) ? 0 : 1;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (carrier) {
1056 if ((de->media_type == DE_MEDIA_TP_AUTO ||
1057 de->media_type == DE_MEDIA_TP ||
1058 de->media_type == DE_MEDIA_TP_FD) &&
1059 (status & LinkFailStatus))
1060 goto no_link_yet;
1061
1062 de->media_timer.expires = jiffies + DE_TIMER_LINK;
1063 add_timer(&de->media_timer);
1064 if (!netif_carrier_ok(dev))
1065 de_link_up(de);
1066 else
1067 if (netif_msg_timer(de))
1068 printk(KERN_INFO "%s: %s link ok, mode %x status %x\n",
1069 dev->name, media_name[de->media_type],
1070 dr32(MacMode), status);
1071 return;
1072 }
1073
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001074 de_link_down(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* if media type locked, don't switch media */
1077 if (de->media_lock)
1078 goto set_media;
1079
1080 /* if activity detected, use that as hint for new media type */
1081 if (status & NonselPortActive) {
1082 unsigned int have_media = 1;
1083
1084 /* if AUI/BNC selected, then activity is on TP port */
1085 if (de->media_type == DE_MEDIA_AUI ||
1086 de->media_type == DE_MEDIA_BNC) {
1087 if (de_ok_to_advertise(de, DE_MEDIA_TP_AUTO))
1088 de->media_type = DE_MEDIA_TP_AUTO;
1089 else
1090 have_media = 0;
1091 }
1092
1093 /* TP selected. If there is only TP and BNC, then it's BNC */
1094 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_BNC) &&
1095 de_ok_to_advertise(de, DE_MEDIA_BNC))
1096 de->media_type = DE_MEDIA_BNC;
1097
1098 /* TP selected. If there is only TP and AUI, then it's AUI */
1099 else if (((de->media_supported & DE_AUI_BNC) == SUPPORTED_AUI) &&
1100 de_ok_to_advertise(de, DE_MEDIA_AUI))
1101 de->media_type = DE_MEDIA_AUI;
1102
1103 /* otherwise, ignore the hint */
1104 else
1105 have_media = 0;
1106
1107 if (have_media)
1108 goto set_media;
1109 }
1110
1111 /*
1112 * Absent or ambiguous activity hint, move to next advertised
1113 * media state. If de->media_type is left unchanged, this
1114 * simply resets the PHY and reloads the current media settings.
1115 */
1116 if (de->media_type == DE_MEDIA_AUI) {
1117 u32 next_states[] = { DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1118 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1119 } else if (de->media_type == DE_MEDIA_BNC) {
1120 u32 next_states[] = { DE_MEDIA_TP_AUTO, DE_MEDIA_AUI };
1121 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1122 } else {
1123 u32 next_states[] = { DE_MEDIA_AUI, DE_MEDIA_BNC, DE_MEDIA_TP_AUTO };
1124 de_next_media(de, next_states, ARRAY_SIZE(next_states));
1125 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127set_media:
1128 spin_lock_irqsave(&de->lock, flags);
1129 de_stop_rxtx(de);
1130 spin_unlock_irqrestore(&de->lock, flags);
1131 de_set_media(de);
1132 de_start_rxtx(de);
1133
1134no_link_yet:
1135 de->media_timer.expires = jiffies + DE_TIMER_NO_LINK;
1136 add_timer(&de->media_timer);
1137
1138 if (netif_msg_timer(de))
1139 printk(KERN_INFO "%s: no link, trying media %s, status %x\n",
1140 dev->name, media_name[de->media_type], status);
1141}
1142
1143static void de_media_interrupt (struct de_private *de, u32 status)
1144{
1145 if (status & LinkPass) {
1146 de_link_up(de);
1147 mod_timer(&de->media_timer, jiffies + DE_TIMER_LINK);
1148 return;
1149 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001150
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +01001151 BUG_ON(!(status & LinkFail));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 if (netif_carrier_ok(de->dev)) {
1154 de_link_down(de);
1155 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1156 }
1157}
1158
1159static int de_reset_mac (struct de_private *de)
1160{
1161 u32 status, tmp;
1162
1163 /*
1164 * Reset MAC. de4x5.c and tulip.c examined for "advice"
1165 * in this area.
1166 */
1167
1168 if (dr32(BusMode) == 0xffffffff)
1169 return -EBUSY;
1170
1171 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
1172 dw32 (BusMode, CmdReset);
1173 mdelay (1);
1174
1175 dw32 (BusMode, de_bus_mode);
1176 mdelay (1);
1177
1178 for (tmp = 0; tmp < 5; tmp++) {
1179 dr32 (BusMode);
1180 mdelay (1);
1181 }
1182
1183 mdelay (1);
1184
1185 status = dr32(MacStatus);
1186 if (status & (RxState | TxState))
1187 return -EBUSY;
1188 if (status == 0xffffffff)
1189 return -ENODEV;
1190 return 0;
1191}
1192
1193static void de_adapter_wake (struct de_private *de)
1194{
1195 u32 pmctl;
1196
1197 if (de->de21040)
1198 return;
1199
1200 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1201 if (pmctl & PM_Mask) {
1202 pmctl &= ~PM_Mask;
1203 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1204
1205 /* de4x5.c delays, so we do too */
1206 msleep(10);
1207 }
1208}
1209
1210static void de_adapter_sleep (struct de_private *de)
1211{
1212 u32 pmctl;
1213
1214 if (de->de21040)
1215 return;
1216
1217 pci_read_config_dword(de->pdev, PCIPM, &pmctl);
1218 pmctl |= PM_Sleep;
1219 pci_write_config_dword(de->pdev, PCIPM, pmctl);
1220}
1221
1222static int de_init_hw (struct de_private *de)
1223{
1224 struct net_device *dev = de->dev;
1225 u32 macmode;
1226 int rc;
1227
1228 de_adapter_wake(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 macmode = dr32(MacMode) & ~MacModeClear;
1231
1232 rc = de_reset_mac(de);
1233 if (rc)
1234 return rc;
1235
1236 de_set_media(de); /* reset phy */
1237
1238 dw32(RxRingAddr, de->ring_dma);
1239 dw32(TxRingAddr, de->ring_dma + (sizeof(struct de_desc) * DE_RX_RING_SIZE));
1240
1241 dw32(MacMode, RxTx | macmode);
1242
1243 dr32(RxMissed); /* self-clearing */
1244
1245 dw32(IntrMask, de_intr_mask);
1246
1247 de_set_rx_mode(dev);
1248
1249 return 0;
1250}
1251
1252static int de_refill_rx (struct de_private *de)
1253{
1254 unsigned i;
1255
1256 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1257 struct sk_buff *skb;
1258
1259 skb = dev_alloc_skb(de->rx_buf_sz);
1260 if (!skb)
1261 goto err_out;
1262
1263 skb->dev = de->dev;
1264
1265 de->rx_skb[i].mapping = pci_map_single(de->pdev,
David S. Miller689be432005-06-28 15:25:31 -07001266 skb->data, de->rx_buf_sz, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 de->rx_skb[i].skb = skb;
1268
1269 de->rx_ring[i].opts1 = cpu_to_le32(DescOwn);
1270 if (i == (DE_RX_RING_SIZE - 1))
1271 de->rx_ring[i].opts2 =
1272 cpu_to_le32(RingEnd | de->rx_buf_sz);
1273 else
1274 de->rx_ring[i].opts2 = cpu_to_le32(de->rx_buf_sz);
1275 de->rx_ring[i].addr1 = cpu_to_le32(de->rx_skb[i].mapping);
1276 de->rx_ring[i].addr2 = 0;
1277 }
1278
1279 return 0;
1280
1281err_out:
1282 de_clean_rings(de);
1283 return -ENOMEM;
1284}
1285
1286static int de_init_rings (struct de_private *de)
1287{
1288 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1289 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1290
1291 de->rx_tail = 0;
1292 de->tx_head = de->tx_tail = 0;
1293
1294 return de_refill_rx (de);
1295}
1296
1297static int de_alloc_rings (struct de_private *de)
1298{
1299 de->rx_ring = pci_alloc_consistent(de->pdev, DE_RING_BYTES, &de->ring_dma);
1300 if (!de->rx_ring)
1301 return -ENOMEM;
1302 de->tx_ring = &de->rx_ring[DE_RX_RING_SIZE];
1303 return de_init_rings(de);
1304}
1305
1306static void de_clean_rings (struct de_private *de)
1307{
1308 unsigned i;
1309
1310 memset(de->rx_ring, 0, sizeof(struct de_desc) * DE_RX_RING_SIZE);
1311 de->rx_ring[DE_RX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1312 wmb();
1313 memset(de->tx_ring, 0, sizeof(struct de_desc) * DE_TX_RING_SIZE);
1314 de->tx_ring[DE_TX_RING_SIZE - 1].opts2 = cpu_to_le32(RingEnd);
1315 wmb();
1316
1317 for (i = 0; i < DE_RX_RING_SIZE; i++) {
1318 if (de->rx_skb[i].skb) {
1319 pci_unmap_single(de->pdev, de->rx_skb[i].mapping,
1320 de->rx_buf_sz, PCI_DMA_FROMDEVICE);
1321 dev_kfree_skb(de->rx_skb[i].skb);
1322 }
1323 }
1324
1325 for (i = 0; i < DE_TX_RING_SIZE; i++) {
1326 struct sk_buff *skb = de->tx_skb[i].skb;
1327 if ((skb) && (skb != DE_DUMMY_SKB)) {
1328 if (skb != DE_SETUP_SKB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 de->net_stats.tx_dropped++;
1330 pci_unmap_single(de->pdev,
1331 de->tx_skb[i].mapping,
1332 skb->len, PCI_DMA_TODEVICE);
Eric Sesterhenn5185c7c2006-03-22 22:30:34 +01001333 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 } else {
1335 pci_unmap_single(de->pdev,
1336 de->tx_skb[i].mapping,
1337 sizeof(de->setup_frame),
1338 PCI_DMA_TODEVICE);
1339 }
1340 }
1341 }
1342
1343 memset(&de->rx_skb, 0, sizeof(struct ring_info) * DE_RX_RING_SIZE);
1344 memset(&de->tx_skb, 0, sizeof(struct ring_info) * DE_TX_RING_SIZE);
1345}
1346
1347static void de_free_rings (struct de_private *de)
1348{
1349 de_clean_rings(de);
1350 pci_free_consistent(de->pdev, DE_RING_BYTES, de->rx_ring, de->ring_dma);
1351 de->rx_ring = NULL;
1352 de->tx_ring = NULL;
1353}
1354
1355static int de_open (struct net_device *dev)
1356{
Wang Chen8f15ea42008-11-12 23:38:36 -08001357 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (netif_msg_ifup(de))
1361 printk(KERN_DEBUG "%s: enabling interface\n", dev->name);
1362
1363 de->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1364
1365 rc = de_alloc_rings(de);
1366 if (rc) {
1367 printk(KERN_ERR "%s: ring allocation failure, err=%d\n",
1368 dev->name, rc);
1369 return rc;
1370 }
1371
Francois Romieu3f735b72006-03-08 22:41:15 +01001372 dw32(IntrMask, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001374 rc = request_irq(dev->irq, de_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (rc) {
1376 printk(KERN_ERR "%s: IRQ %d request failure, err=%d\n",
1377 dev->name, dev->irq, rc);
Francois Romieu3f735b72006-03-08 22:41:15 +01001378 goto err_out_free;
1379 }
1380
1381 rc = de_init_hw(de);
1382 if (rc) {
1383 printk(KERN_ERR "%s: h/w init failure, err=%d\n",
1384 dev->name, rc);
1385 goto err_out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
1388 netif_start_queue(dev);
1389 mod_timer(&de->media_timer, jiffies + DE_TIMER_NO_LINK);
1390
1391 return 0;
1392
Francois Romieu3f735b72006-03-08 22:41:15 +01001393err_out_free_irq:
1394 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395err_out_free:
1396 de_free_rings(de);
1397 return rc;
1398}
1399
1400static int de_close (struct net_device *dev)
1401{
Wang Chen8f15ea42008-11-12 23:38:36 -08001402 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned long flags;
1404
1405 if (netif_msg_ifdown(de))
1406 printk(KERN_DEBUG "%s: disabling interface\n", dev->name);
1407
1408 del_timer_sync(&de->media_timer);
1409
1410 spin_lock_irqsave(&de->lock, flags);
1411 de_stop_hw(de);
1412 netif_stop_queue(dev);
1413 netif_carrier_off(dev);
1414 spin_unlock_irqrestore(&de->lock, flags);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 free_irq(dev->irq, dev);
1417
1418 de_free_rings(de);
1419 de_adapter_sleep(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
1422
1423static void de_tx_timeout (struct net_device *dev)
1424{
Wang Chen8f15ea42008-11-12 23:38:36 -08001425 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 printk(KERN_DEBUG "%s: NIC status %08x mode %08x sia %08x desc %u/%u/%u\n",
1428 dev->name, dr32(MacStatus), dr32(MacMode), dr32(SIAStatus),
1429 de->rx_tail, de->tx_head, de->tx_tail);
1430
1431 del_timer_sync(&de->media_timer);
1432
1433 disable_irq(dev->irq);
1434 spin_lock_irq(&de->lock);
1435
1436 de_stop_hw(de);
1437 netif_stop_queue(dev);
1438 netif_carrier_off(dev);
1439
1440 spin_unlock_irq(&de->lock);
1441 enable_irq(dev->irq);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Update the error counts. */
1444 __de_get_stats(de);
1445
1446 synchronize_irq(dev->irq);
1447 de_clean_rings(de);
1448
Francois Romieu39bf4292006-03-08 22:45:52 +01001449 de_init_rings(de);
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 de_init_hw(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 netif_wake_queue(dev);
1454}
1455
1456static void __de_get_regs(struct de_private *de, u8 *buf)
1457{
1458 int i;
1459 u32 *rbuf = (u32 *)buf;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* read all CSRs */
1462 for (i = 0; i < DE_NUM_REGS; i++)
1463 rbuf[i] = dr32(i * 8);
1464
1465 /* handle self-clearing RxMissed counter, CSR8 */
1466 de_rx_missed(de, rbuf[8]);
1467}
1468
1469static int __de_get_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1470{
1471 ecmd->supported = de->media_supported;
1472 ecmd->transceiver = XCVR_INTERNAL;
1473 ecmd->phy_address = 0;
1474 ecmd->advertising = de->media_advertise;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 switch (de->media_type) {
1477 case DE_MEDIA_AUI:
1478 ecmd->port = PORT_AUI;
1479 ecmd->speed = 5;
1480 break;
1481 case DE_MEDIA_BNC:
1482 ecmd->port = PORT_BNC;
1483 ecmd->speed = 2;
1484 break;
1485 default:
1486 ecmd->port = PORT_TP;
1487 ecmd->speed = SPEED_10;
1488 break;
1489 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (dr32(MacMode) & FullDuplex)
1492 ecmd->duplex = DUPLEX_FULL;
1493 else
1494 ecmd->duplex = DUPLEX_HALF;
1495
1496 if (de->media_lock)
1497 ecmd->autoneg = AUTONEG_DISABLE;
1498 else
1499 ecmd->autoneg = AUTONEG_ENABLE;
1500
1501 /* ignore maxtxpkt, maxrxpkt for now */
1502
1503 return 0;
1504}
1505
1506static int __de_set_settings(struct de_private *de, struct ethtool_cmd *ecmd)
1507{
1508 u32 new_media;
1509 unsigned int media_lock;
1510
1511 if (ecmd->speed != SPEED_10 && ecmd->speed != 5 && ecmd->speed != 2)
1512 return -EINVAL;
1513 if (de->de21040 && ecmd->speed == 2)
1514 return -EINVAL;
1515 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1516 return -EINVAL;
1517 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI && ecmd->port != PORT_BNC)
1518 return -EINVAL;
1519 if (de->de21040 && ecmd->port == PORT_BNC)
1520 return -EINVAL;
1521 if (ecmd->transceiver != XCVR_INTERNAL)
1522 return -EINVAL;
1523 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1524 return -EINVAL;
1525 if (ecmd->advertising & ~de->media_supported)
1526 return -EINVAL;
1527 if (ecmd->autoneg == AUTONEG_ENABLE &&
1528 (!(ecmd->advertising & ADVERTISED_Autoneg)))
1529 return -EINVAL;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 switch (ecmd->port) {
1532 case PORT_AUI:
1533 new_media = DE_MEDIA_AUI;
1534 if (!(ecmd->advertising & ADVERTISED_AUI))
1535 return -EINVAL;
1536 break;
1537 case PORT_BNC:
1538 new_media = DE_MEDIA_BNC;
1539 if (!(ecmd->advertising & ADVERTISED_BNC))
1540 return -EINVAL;
1541 break;
1542 default:
1543 if (ecmd->autoneg == AUTONEG_ENABLE)
1544 new_media = DE_MEDIA_TP_AUTO;
1545 else if (ecmd->duplex == DUPLEX_FULL)
1546 new_media = DE_MEDIA_TP_FD;
1547 else
1548 new_media = DE_MEDIA_TP;
1549 if (!(ecmd->advertising & ADVERTISED_TP))
1550 return -EINVAL;
1551 if (!(ecmd->advertising & (ADVERTISED_10baseT_Full | ADVERTISED_10baseT_Half)))
1552 return -EINVAL;
1553 break;
1554 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 media_lock = (ecmd->autoneg == AUTONEG_ENABLE) ? 0 : 1;
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if ((new_media == de->media_type) &&
1559 (media_lock == de->media_lock) &&
1560 (ecmd->advertising == de->media_advertise))
1561 return 0; /* nothing to change */
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 de_link_down(de);
1564 de_stop_rxtx(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 de->media_type = new_media;
1567 de->media_lock = media_lock;
1568 de->media_advertise = ecmd->advertising;
1569 de_set_media(de);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
1574static void de_get_drvinfo (struct net_device *dev,struct ethtool_drvinfo *info)
1575{
Wang Chen8f15ea42008-11-12 23:38:36 -08001576 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 strcpy (info->driver, DRV_NAME);
1579 strcpy (info->version, DRV_VERSION);
1580 strcpy (info->bus_info, pci_name(de->pdev));
1581 info->eedump_len = DE_EEPROM_SIZE;
1582}
1583
1584static int de_get_regs_len(struct net_device *dev)
1585{
1586 return DE_REGS_SIZE;
1587}
1588
1589static int de_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1590{
Wang Chen8f15ea42008-11-12 23:38:36 -08001591 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int rc;
1593
1594 spin_lock_irq(&de->lock);
1595 rc = __de_get_settings(de, ecmd);
1596 spin_unlock_irq(&de->lock);
1597
1598 return rc;
1599}
1600
1601static int de_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1602{
Wang Chen8f15ea42008-11-12 23:38:36 -08001603 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int rc;
1605
1606 spin_lock_irq(&de->lock);
1607 rc = __de_set_settings(de, ecmd);
1608 spin_unlock_irq(&de->lock);
1609
1610 return rc;
1611}
1612
1613static u32 de_get_msglevel(struct net_device *dev)
1614{
Wang Chen8f15ea42008-11-12 23:38:36 -08001615 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 return de->msg_enable;
1618}
1619
1620static void de_set_msglevel(struct net_device *dev, u32 msglvl)
1621{
Wang Chen8f15ea42008-11-12 23:38:36 -08001622 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 de->msg_enable = msglvl;
1625}
1626
1627static int de_get_eeprom(struct net_device *dev,
1628 struct ethtool_eeprom *eeprom, u8 *data)
1629{
Wang Chen8f15ea42008-11-12 23:38:36 -08001630 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 if (!de->ee_data)
1633 return -EOPNOTSUPP;
1634 if ((eeprom->offset != 0) || (eeprom->magic != 0) ||
1635 (eeprom->len != DE_EEPROM_SIZE))
1636 return -EINVAL;
1637 memcpy(data, de->ee_data, eeprom->len);
1638
1639 return 0;
1640}
1641
1642static int de_nway_reset(struct net_device *dev)
1643{
Wang Chen8f15ea42008-11-12 23:38:36 -08001644 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 u32 status;
1646
1647 if (de->media_type != DE_MEDIA_TP_AUTO)
1648 return -EINVAL;
1649 if (netif_carrier_ok(de->dev))
1650 de_link_down(de);
1651
1652 status = dr32(SIAStatus);
1653 dw32(SIAStatus, (status & ~NWayState) | NWayRestart);
1654 if (netif_msg_link(de))
1655 printk(KERN_INFO "%s: link nway restart, status %x,%x\n",
1656 de->dev->name, status, dr32(SIAStatus));
1657 return 0;
1658}
1659
1660static void de_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1661 void *data)
1662{
Wang Chen8f15ea42008-11-12 23:38:36 -08001663 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 regs->version = (DE_REGS_VER << 2) | de->de21040;
1666
1667 spin_lock_irq(&de->lock);
1668 __de_get_regs(de, data);
1669 spin_unlock_irq(&de->lock);
1670}
1671
Jeff Garzik7282d492006-09-13 14:30:00 -04001672static const struct ethtool_ops de_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 .get_drvinfo = de_get_drvinfo,
1675 .get_regs_len = de_get_regs_len,
1676 .get_settings = de_get_settings,
1677 .set_settings = de_set_settings,
1678 .get_msglevel = de_get_msglevel,
1679 .set_msglevel = de_set_msglevel,
1680 .get_eeprom = de_get_eeprom,
1681 .nway_reset = de_nway_reset,
1682 .get_regs = de_get_regs,
1683};
1684
Prarit Bhargava4c44fd02007-03-06 02:42:00 -08001685static void __devinit de21040_get_mac_address (struct de_private *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 unsigned i;
1688
1689 dw32 (ROMCmd, 0); /* Reset the pointer with a dummy write. */
Martin Langerbc0da3f2008-10-13 18:49:38 -07001690 udelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 for (i = 0; i < 6; i++) {
1693 int value, boguscnt = 100000;
Hannes Ederec1d1ebb2008-12-26 00:07:45 -08001694 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 value = dr32(ROMCmd);
Hannes Ederec1d1ebb2008-12-26 00:07:45 -08001696 } while (value < 0 && --boguscnt > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 de->dev->dev_addr[i] = value;
1698 udelay(1);
1699 if (boguscnt <= 0)
1700 printk(KERN_WARNING PFX "timeout reading 21040 MAC address byte %u\n", i);
1701 }
1702}
1703
Prarit Bhargava4c44fd02007-03-06 02:42:00 -08001704static void __devinit de21040_get_media_info(struct de_private *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 unsigned int i;
1707
1708 de->media_type = DE_MEDIA_TP;
1709 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full |
1710 SUPPORTED_10baseT_Half | SUPPORTED_AUI;
1711 de->media_advertise = de->media_supported;
1712
1713 for (i = 0; i < DE_MAX_MEDIA; i++) {
1714 switch (i) {
1715 case DE_MEDIA_AUI:
1716 case DE_MEDIA_TP:
1717 case DE_MEDIA_TP_FD:
1718 de->media[i].type = i;
1719 de->media[i].csr13 = t21040_csr13[i];
1720 de->media[i].csr14 = t21040_csr14[i];
1721 de->media[i].csr15 = t21040_csr15[i];
1722 break;
1723 default:
1724 de->media[i].type = DE_MEDIA_INVALID;
1725 break;
1726 }
1727 }
1728}
1729
1730/* Note: this routine returns extra data bits for size detection. */
Helge Deller4a1d2d82006-10-06 12:12:34 -06001731static unsigned __devinit tulip_read_eeprom(void __iomem *regs, int location, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 int i;
1734 unsigned retval = 0;
1735 void __iomem *ee_addr = regs + ROMCmd;
1736 int read_cmd = location | (EE_READ_CMD << addr_len);
1737
1738 writel(EE_ENB & ~EE_CS, ee_addr);
1739 writel(EE_ENB, ee_addr);
1740
1741 /* Shift the read command bits out. */
1742 for (i = 4 + addr_len; i >= 0; i--) {
1743 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1744 writel(EE_ENB | dataval, ee_addr);
1745 readl(ee_addr);
1746 writel(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1747 readl(ee_addr);
1748 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1749 }
1750 writel(EE_ENB, ee_addr);
1751 readl(ee_addr);
1752
1753 for (i = 16; i > 0; i--) {
1754 writel(EE_ENB | EE_SHIFT_CLK, ee_addr);
1755 readl(ee_addr);
1756 retval = (retval << 1) | ((readl(ee_addr) & EE_DATA_READ) ? 1 : 0);
1757 writel(EE_ENB, ee_addr);
1758 readl(ee_addr);
1759 }
1760
1761 /* Terminate the EEPROM access. */
1762 writel(EE_ENB & ~EE_CS, ee_addr);
1763 return retval;
1764}
1765
Prarit Bhargava4c44fd02007-03-06 02:42:00 -08001766static void __devinit de21041_get_srom_info (struct de_private *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 unsigned i, sa_offset = 0, ofs;
1769 u8 ee_data[DE_EEPROM_SIZE + 6] = {};
1770 unsigned ee_addr_size = tulip_read_eeprom(de->regs, 0xff, 8) & 0x40000 ? 8 : 6;
1771 struct de_srom_info_leaf *il;
1772 void *bufp;
1773
1774 /* download entire eeprom */
1775 for (i = 0; i < DE_EEPROM_WORDS; i++)
Al Viroc559a5b2007-08-23 00:43:22 -04001776 ((__le16 *)ee_data)[i] =
1777 cpu_to_le16(tulip_read_eeprom(de->regs, i, ee_addr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 /* DEC now has a specification but early board makers
1780 just put the address in the first EEPROM locations. */
1781 /* This does memcmp(eedata, eedata+16, 8) */
Ralf Baechlebc053d42005-10-10 14:50:46 +01001782
1783#ifndef CONFIG_MIPS_COBALT
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 for (i = 0; i < 8; i ++)
1786 if (ee_data[i] != ee_data[16+i])
1787 sa_offset = 20;
1788
Ralf Baechlebc053d42005-10-10 14:50:46 +01001789#endif
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /* store MAC address */
1792 for (i = 0; i < 6; i ++)
1793 de->dev->dev_addr[i] = ee_data[i + sa_offset];
1794
1795 /* get offset of controller 0 info leaf. ignore 2nd byte. */
1796 ofs = ee_data[SROMC0InfoLeaf];
1797 if (ofs >= (sizeof(ee_data) - sizeof(struct de_srom_info_leaf) - sizeof(struct de_srom_media_block)))
1798 goto bad_srom;
1799
1800 /* get pointer to info leaf */
1801 il = (struct de_srom_info_leaf *) &ee_data[ofs];
1802
1803 /* paranoia checks */
1804 if (il->n_blocks == 0)
1805 goto bad_srom;
1806 if ((sizeof(ee_data) - ofs) <
1807 (sizeof(struct de_srom_info_leaf) + (sizeof(struct de_srom_media_block) * il->n_blocks)))
1808 goto bad_srom;
1809
1810 /* get default media type */
Harvey Harrison445854f2008-05-28 16:51:04 -07001811 switch (get_unaligned(&il->default_media)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 case 0x0001: de->media_type = DE_MEDIA_BNC; break;
1813 case 0x0002: de->media_type = DE_MEDIA_AUI; break;
1814 case 0x0204: de->media_type = DE_MEDIA_TP_FD; break;
1815 default: de->media_type = DE_MEDIA_TP_AUTO; break;
1816 }
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (netif_msg_probe(de))
1819 printk(KERN_INFO "de%d: SROM leaf offset %u, default media %s\n",
1820 de->board_idx, ofs,
1821 media_name[de->media_type]);
1822
1823 /* init SIA register values to defaults */
1824 for (i = 0; i < DE_MAX_MEDIA; i++) {
1825 de->media[i].type = DE_MEDIA_INVALID;
1826 de->media[i].csr13 = 0xffff;
1827 de->media[i].csr14 = 0xffff;
1828 de->media[i].csr15 = 0xffff;
1829 }
1830
1831 /* parse media blocks to see what medias are supported,
1832 * and if any custom CSR values are provided
1833 */
1834 bufp = ((void *)il) + sizeof(*il);
1835 for (i = 0; i < il->n_blocks; i++) {
1836 struct de_srom_media_block *ib = bufp;
1837 unsigned idx;
1838
1839 /* index based on media type in media block */
1840 switch(ib->opts & MediaBlockMask) {
1841 case 0: /* 10baseT */
1842 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half
1843 | SUPPORTED_Autoneg;
1844 idx = DE_MEDIA_TP;
1845 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1846 break;
1847 case 1: /* BNC */
1848 de->media_supported |= SUPPORTED_BNC;
1849 idx = DE_MEDIA_BNC;
1850 break;
1851 case 2: /* AUI */
1852 de->media_supported |= SUPPORTED_AUI;
1853 idx = DE_MEDIA_AUI;
1854 break;
1855 case 4: /* 10baseT-FD */
1856 de->media_supported |= SUPPORTED_TP | SUPPORTED_10baseT_Full
1857 | SUPPORTED_Autoneg;
1858 idx = DE_MEDIA_TP_FD;
1859 de->media[DE_MEDIA_TP_AUTO].type = DE_MEDIA_TP_AUTO;
1860 break;
1861 default:
1862 goto bad_srom;
1863 }
1864
1865 de->media[idx].type = idx;
1866
1867 if (netif_msg_probe(de))
1868 printk(KERN_INFO "de%d: media block #%u: %s",
1869 de->board_idx, i,
1870 media_name[de->media[idx].type]);
1871
1872 bufp += sizeof (ib->opts);
1873
1874 if (ib->opts & MediaCustomCSRs) {
Harvey Harrison445854f2008-05-28 16:51:04 -07001875 de->media[idx].csr13 = get_unaligned(&ib->csr13);
1876 de->media[idx].csr14 = get_unaligned(&ib->csr14);
1877 de->media[idx].csr15 = get_unaligned(&ib->csr15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 bufp += sizeof(ib->csr13) + sizeof(ib->csr14) +
1879 sizeof(ib->csr15);
1880
1881 if (netif_msg_probe(de))
1882 printk(" (%x,%x,%x)\n",
1883 de->media[idx].csr13,
1884 de->media[idx].csr14,
1885 de->media[idx].csr15);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 } else if (netif_msg_probe(de))
1888 printk("\n");
1889
1890 if (bufp > ((void *)&ee_data[DE_EEPROM_SIZE - 3]))
1891 break;
1892 }
1893
1894 de->media_advertise = de->media_supported;
1895
1896fill_defaults:
1897 /* fill in defaults, for cases where custom CSRs not used */
1898 for (i = 0; i < DE_MAX_MEDIA; i++) {
1899 if (de->media[i].csr13 == 0xffff)
1900 de->media[i].csr13 = t21041_csr13[i];
1901 if (de->media[i].csr14 == 0xffff)
1902 de->media[i].csr14 = t21041_csr14[i];
1903 if (de->media[i].csr15 == 0xffff)
1904 de->media[i].csr15 = t21041_csr15[i];
1905 }
1906
Eric Sesterhennc3a9392e2006-10-23 22:20:15 +02001907 de->ee_data = kmemdup(&ee_data[0], DE_EEPROM_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 return;
1910
1911bad_srom:
1912 /* for error cases, it's ok to assume we support all these */
1913 for (i = 0; i < DE_MAX_MEDIA; i++)
1914 de->media[i].type = i;
1915 de->media_supported =
1916 SUPPORTED_10baseT_Half |
1917 SUPPORTED_10baseT_Full |
1918 SUPPORTED_Autoneg |
1919 SUPPORTED_TP |
1920 SUPPORTED_AUI |
1921 SUPPORTED_BNC;
1922 goto fill_defaults;
1923}
1924
Stephen Hemminger90d87432009-01-07 17:59:47 -08001925static const struct net_device_ops de_netdev_ops = {
1926 .ndo_open = de_open,
1927 .ndo_stop = de_close,
1928 .ndo_set_multicast_list = de_set_rx_mode,
1929 .ndo_start_xmit = de_start_xmit,
1930 .ndo_get_stats = de_get_stats,
1931 .ndo_tx_timeout = de_tx_timeout,
1932 .ndo_change_mtu = eth_change_mtu,
1933 .ndo_set_mac_address = eth_mac_addr,
1934 .ndo_validate_addr = eth_validate_addr,
1935};
1936
Helge Deller4a1d2d82006-10-06 12:12:34 -06001937static int __devinit de_init_one (struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 const struct pci_device_id *ent)
1939{
1940 struct net_device *dev;
1941 struct de_private *de;
1942 int rc;
1943 void __iomem *regs;
Jeff Garzikafc70972005-08-31 06:11:16 -04001944 unsigned long pciaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 static int board_idx = -1;
1946
1947 board_idx++;
1948
1949#ifndef MODULE
1950 if (board_idx == 0)
1951 printk("%s", version);
1952#endif
1953
1954 /* allocate a new ethernet device structure, and fill in defaults */
1955 dev = alloc_etherdev(sizeof(struct de_private));
1956 if (!dev)
1957 return -ENOMEM;
1958
Stephen Hemminger90d87432009-01-07 17:59:47 -08001959 dev->netdev_ops = &de_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 dev->ethtool_ops = &de_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 dev->watchdog_timeo = TX_TIMEOUT;
1963
Wang Chen8f15ea42008-11-12 23:38:36 -08001964 de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 de->de21040 = ent->driver_data == 0 ? 1 : 0;
1966 de->pdev = pdev;
1967 de->dev = dev;
1968 de->msg_enable = (debug < 0 ? DE_DEF_MSG_ENABLE : debug);
1969 de->board_idx = board_idx;
1970 spin_lock_init (&de->lock);
1971 init_timer(&de->media_timer);
1972 if (de->de21040)
1973 de->media_timer.function = de21040_media_timer;
1974 else
1975 de->media_timer.function = de21041_media_timer;
1976 de->media_timer.data = (unsigned long) de;
1977
1978 netif_carrier_off(dev);
1979 netif_stop_queue(dev);
1980
1981 /* wake up device, assign resources */
1982 rc = pci_enable_device(pdev);
1983 if (rc)
1984 goto err_out_free;
1985
1986 /* reserve PCI resources to ensure driver atomicity */
1987 rc = pci_request_regions(pdev, DRV_NAME);
1988 if (rc)
1989 goto err_out_disable;
1990
1991 /* check for invalid IRQ value */
1992 if (pdev->irq < 2) {
1993 rc = -EIO;
1994 printk(KERN_ERR PFX "invalid irq (%d) for pci dev %s\n",
1995 pdev->irq, pci_name(pdev));
1996 goto err_out_res;
1997 }
1998
1999 dev->irq = pdev->irq;
2000
2001 /* obtain and check validity of PCI I/O address */
2002 pciaddr = pci_resource_start(pdev, 1);
2003 if (!pciaddr) {
2004 rc = -EIO;
2005 printk(KERN_ERR PFX "no MMIO resource for pci dev %s\n",
2006 pci_name(pdev));
2007 goto err_out_res;
2008 }
2009 if (pci_resource_len(pdev, 1) < DE_REGS_SIZE) {
2010 rc = -EIO;
Greg Kroah-Hartman7c7459d2006-06-12 15:13:08 -07002011 printk(KERN_ERR PFX "MMIO resource (%llx) too small on pci dev %s\n",
2012 (unsigned long long)pci_resource_len(pdev, 1), pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto err_out_res;
2014 }
2015
2016 /* remap CSR registers */
2017 regs = ioremap_nocache(pciaddr, DE_REGS_SIZE);
2018 if (!regs) {
2019 rc = -EIO;
Greg Kroah-Hartman7c7459d2006-06-12 15:13:08 -07002020 printk(KERN_ERR PFX "Cannot map PCI MMIO (%llx@%lx) on pci dev %s\n",
2021 (unsigned long long)pci_resource_len(pdev, 1),
2022 pciaddr, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto err_out_res;
2024 }
2025 dev->base_addr = (unsigned long) regs;
2026 de->regs = regs;
2027
2028 de_adapter_wake(de);
2029
2030 /* make sure hardware is not running */
2031 rc = de_reset_mac(de);
2032 if (rc) {
2033 printk(KERN_ERR PFX "Cannot reset MAC, pci dev %s\n",
2034 pci_name(pdev));
2035 goto err_out_iomap;
2036 }
2037
2038 /* get MAC address, initialize default media type and
2039 * get list of supported media
2040 */
2041 if (de->de21040) {
2042 de21040_get_mac_address(de);
2043 de21040_get_media_info(de);
2044 } else {
2045 de21041_get_srom_info(de);
2046 }
2047
2048 /* register new network interface with kernel */
2049 rc = register_netdev(dev);
2050 if (rc)
2051 goto err_out_iomap;
2052
2053 /* print info about board and interface just registered */
Johannes Berge1749612008-10-27 15:59:26 -07002054 printk (KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 dev->name,
2056 de->de21040 ? "21040" : "21041",
2057 dev->base_addr,
Johannes Berge1749612008-10-27 15:59:26 -07002058 dev->dev_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 dev->irq);
2060
2061 pci_set_drvdata(pdev, dev);
2062
2063 /* enable busmastering */
2064 pci_set_master(pdev);
2065
2066 /* put adapter to sleep */
2067 de_adapter_sleep(de);
2068
2069 return 0;
2070
2071err_out_iomap:
Jesper Juhlb4558ea2005-10-28 16:53:13 -04002072 kfree(de->ee_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 iounmap(regs);
2074err_out_res:
2075 pci_release_regions(pdev);
2076err_out_disable:
2077 pci_disable_device(pdev);
2078err_out_free:
2079 free_netdev(dev);
2080 return rc;
2081}
2082
Helge Deller4a1d2d82006-10-06 12:12:34 -06002083static void __devexit de_remove_one (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002086 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Eric Sesterhenn / snakebyte7e0b58f2006-01-26 22:02:43 +01002088 BUG_ON(!dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unregister_netdev(dev);
Jesper Juhlb4558ea2005-10-28 16:53:13 -04002090 kfree(de->ee_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 iounmap(de->regs);
2092 pci_release_regions(pdev);
2093 pci_disable_device(pdev);
2094 pci_set_drvdata(pdev, NULL);
2095 free_netdev(dev);
2096}
2097
2098#ifdef CONFIG_PM
2099
Pavel Machek05adc3b2005-04-16 15:25:25 -07002100static int de_suspend (struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct net_device *dev = pci_get_drvdata (pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002103 struct de_private *de = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 rtnl_lock();
2106 if (netif_running (dev)) {
2107 del_timer_sync(&de->media_timer);
2108
2109 disable_irq(dev->irq);
2110 spin_lock_irq(&de->lock);
2111
2112 de_stop_hw(de);
2113 netif_stop_queue(dev);
2114 netif_device_detach(dev);
2115 netif_carrier_off(dev);
2116
2117 spin_unlock_irq(&de->lock);
2118 enable_irq(dev->irq);
Jeff Garzikf3b197a2006-05-26 21:39:03 -04002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* Update the error counts. */
2121 __de_get_stats(de);
2122
2123 synchronize_irq(dev->irq);
2124 de_clean_rings(de);
2125
2126 de_adapter_sleep(de);
2127 pci_disable_device(pdev);
2128 } else {
2129 netif_device_detach(dev);
2130 }
2131 rtnl_unlock();
2132 return 0;
2133}
2134
2135static int de_resume (struct pci_dev *pdev)
2136{
2137 struct net_device *dev = pci_get_drvdata (pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002138 struct de_private *de = netdev_priv(dev);
Valerie Henson9f486ae2006-09-08 11:15:41 -07002139 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 rtnl_lock();
2142 if (netif_device_present(dev))
2143 goto out;
Valerie Henson9f486ae2006-09-08 11:15:41 -07002144 if (!netif_running(dev))
2145 goto out_attach;
2146 if ((retval = pci_enable_device(pdev))) {
2147 printk (KERN_ERR "%s: pci_enable_device failed in resume\n",
2148 dev->name);
2149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
Valerie Henson9f486ae2006-09-08 11:15:41 -07002151 de_init_hw(de);
2152out_attach:
2153 netif_device_attach(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154out:
2155 rtnl_unlock();
2156 return 0;
2157}
2158
2159#endif /* CONFIG_PM */
2160
2161static struct pci_driver de_driver = {
2162 .name = DRV_NAME,
2163 .id_table = de_pci_tbl,
2164 .probe = de_init_one,
Helge Deller4a1d2d82006-10-06 12:12:34 -06002165 .remove = __devexit_p(de_remove_one),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166#ifdef CONFIG_PM
2167 .suspend = de_suspend,
2168 .resume = de_resume,
2169#endif
2170};
2171
2172static int __init de_init (void)
2173{
2174#ifdef MODULE
2175 printk("%s", version);
2176#endif
Jeff Garzik29917622006-08-19 17:48:59 -04002177 return pci_register_driver(&de_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180static void __exit de_exit (void)
2181{
2182 pci_unregister_driver (&de_driver);
2183}
2184
2185module_init(de_init);
2186module_exit(de_exit);