1 /*======================================================================
2
3 A PCMCIA ethernet driver for the 3com 3c589 card.
4
5 Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
6
7 3c589_cs.c 1.162 2001/10/13 00:08:50
8
9 The network driver code is based on Donald Becker's 3c589 code:
10
11 Written 1994 by Donald Becker.
12 Copyright 1993 United States Government as represented by the
13 Director, National Security Agency. This software may be used and
14 distributed according to the terms of the GNU General Public License,
15 incorporated herein by reference.
16 Donald Becker may be reached at becker@scyld.com
17
18 ======================================================================*/
19
20 #define DRV_NAME "3c589_cs"
21 #define DRV_VERSION "1.162"
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/ptrace.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/in.h>
33 #include <linux/delay.h>
34 #include <linux/ethtool.h>
35
36 #include <asm/uaccess.h>
37 #include <asm/io.h>
38 #include <asm/system.h>
39 #include <asm/bitops.h>
40
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/if_arp.h>
45 #include <linux/ioport.h>
46
47 #include <pcmcia/version.h>
48 #include <pcmcia/cs_types.h>
49 #include <pcmcia/cs.h>
50 #include <pcmcia/cistpl.h>
51 #include <pcmcia/cisreg.h>
52 #include <pcmcia/ciscode.h>
53 #include <pcmcia/ds.h>
54
55 /* To minimize the size of the driver source I only define operating
56 constants if they are used several times. You'll need the manual
57 if you want to understand driver details. */
58 /* Offsets from base I/O address. */
59 #define EL3_DATA 0x00
60 #define EL3_TIMER 0x0a
61 #define EL3_CMD 0x0e
62 #define EL3_STATUS 0x0e
63
64 #define EEPROM_READ 0x0080
65 #define EEPROM_BUSY 0x8000
66
67 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
68
69 /* The top five bits written to EL3_CMD are a command, the lower
70 11 bits are the parameter, if applicable. */
71 enum c509cmd {
72 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
73 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
74 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
75 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
76 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
77 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
78 StatsDisable = 22<<11, StopCoax = 23<<11,
79 };
80
81 enum c509status {
82 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
83 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
84 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000
85 };
86
87 /* The SetRxFilter command accepts the following classes: */
88 enum RxFilter {
89 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
90 };
91
92 /* Register window 1 offsets, the window used in normal operation. */
93 #define TX_FIFO 0x00
94 #define RX_FIFO 0x00
95 #define RX_STATUS 0x08
96 #define TX_STATUS 0x0B
97 #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
98
99 #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
100 #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
101 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
102 #define MEDIA_LED 0x0001 /* Enable link light on 3C589E cards. */
103
104 /* Time in jiffies before concluding Tx hung */
105 #define TX_TIMEOUT ((400*HZ)/1000)
106
107 struct el3_private {
108 dev_link_t link;
109 struct net_device dev;
110 dev_node_t node;
111 struct net_device_stats stats;
112 /* For transceiver monitoring */
113 struct timer_list media;
114 u_short media_status;
115 u_short fast_poll;
116 u_long last_irq;
117 };
118
119 static char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
120
121 /*====================================================================*/
122
123 /* Module parameters */
124
125 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
126 MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
127 MODULE_LICENSE("GPL");
128
129 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
130
131 /* Special hook for setting if_port when module is loaded */
132 INT_MODULE_PARM(if_port, 0);
133
134 /* Bit map of interrupts to choose from */
135 INT_MODULE_PARM(irq_mask, 0xdeb8);
136 static int irq_list[4] = { -1 };
137 MODULE_PARM(irq_list, "1-4i");
138
139 #ifdef PCMCIA_DEBUG
140 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
141 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
142 static char *version =
143 DRV_NAME ".c " DRV_VERSION " 2001/10/13 00:08:50 (David Hinds)";
144 #else
145 #define DEBUG(n, args...)
146 #endif
147
148 /*====================================================================*/
149
150 static void tc589_config(dev_link_t *link);
151 static void tc589_release(u_long arg);
152 static int tc589_event(event_t event, int priority,
153 event_callback_args_t *args);
154
155 static u_short read_eeprom(ioaddr_t ioaddr, int index);
156 static void tc589_reset(struct net_device *dev);
157 static void media_check(u_long arg);
158 static int el3_config(struct net_device *dev, struct ifmap *map);
159 static int el3_open(struct net_device *dev);
160 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
161 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
162 static void update_stats(struct net_device *dev);
163 static struct net_device_stats *el3_get_stats(struct net_device *dev);
164 static int el3_rx(struct net_device *dev);
165 static int el3_close(struct net_device *dev);
166 static void el3_tx_timeout(struct net_device *dev);
167 static void set_multicast_list(struct net_device *dev);
168 static struct ethtool_ops netdev_ethtool_ops;
169
170 static dev_info_t dev_info = "3c589_cs";
171
172 static dev_link_t *tc589_attach(void);
173 static void tc589_detach(dev_link_t *);
174
175 static dev_link_t *dev_list;
176
177 /*======================================================================
178
179 This bit of code is used to avoid unregistering network devices
180 at inappropriate times. 2.2 and later kernels are fairly picky
181 about when this can happen.
182
183 ======================================================================*/
184
flush_stale_links(void)185 static void flush_stale_links(void)
186 {
187 dev_link_t *link, *next;
188 for (link = dev_list; link; link = next) {
189 next = link->next;
190 if (link->state & DEV_STALE_LINK)
191 tc589_detach(link);
192 }
193 }
194
195 /*====================================================================*/
196
cs_error(client_handle_t handle,int func,int ret)197 static void cs_error(client_handle_t handle, int func, int ret)
198 {
199 error_info_t err = { func, ret };
200 CardServices(ReportError, handle, &err);
201 }
202
203 /*======================================================================
204
205 tc589_attach() creates an "instance" of the driver, allocating
206 local data structures for one device. The device is registered
207 with Card Services.
208
209 ======================================================================*/
210
tc589_attach(void)211 static dev_link_t *tc589_attach(void)
212 {
213 struct el3_private *lp;
214 client_reg_t client_reg;
215 dev_link_t *link;
216 struct net_device *dev;
217 int i, ret;
218
219 DEBUG(0, "3c589_attach()\n");
220 flush_stale_links();
221
222 /* Create new ethernet device */
223 lp = kmalloc(sizeof(*lp), GFP_KERNEL);
224 if (!lp) return NULL;
225 memset(lp, 0, sizeof(*lp));
226 link = &lp->link; dev = &lp->dev;
227 link->priv = dev->priv = link->irq.Instance = lp;
228
229 link->release.function = &tc589_release;
230 link->release.data = (u_long)link;
231 link->io.NumPorts1 = 16;
232 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
233 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
234 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
235 if (irq_list[0] == -1)
236 link->irq.IRQInfo2 = irq_mask;
237 else
238 for (i = 0; i < 4; i++)
239 link->irq.IRQInfo2 |= 1 << irq_list[i];
240 link->irq.Handler = &el3_interrupt;
241 link->conf.Attributes = CONF_ENABLE_IRQ;
242 link->conf.Vcc = 50;
243 link->conf.IntType = INT_MEMORY_AND_IO;
244 link->conf.ConfigIndex = 1;
245 link->conf.Present = PRESENT_OPTION;
246
247 /* The EL3-specific entries in the device structure. */
248 dev->hard_start_xmit = &el3_start_xmit;
249 dev->set_config = &el3_config;
250 dev->get_stats = &el3_get_stats;
251 dev->set_multicast_list = &set_multicast_list;
252 ether_setup(dev);
253 dev->open = &el3_open;
254 dev->stop = &el3_close;
255 #ifdef HAVE_TX_TIMEOUT
256 dev->tx_timeout = el3_tx_timeout;
257 dev->watchdog_timeo = TX_TIMEOUT;
258 #endif
259 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
260
261 /* Register with Card Services */
262 link->next = dev_list;
263 dev_list = link;
264 client_reg.dev_info = &dev_info;
265 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
266 client_reg.EventMask =
267 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
268 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
269 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
270 client_reg.event_handler = &tc589_event;
271 client_reg.Version = 0x0210;
272 client_reg.event_callback_args.client_data = link;
273 ret = CardServices(RegisterClient, &link->handle, &client_reg);
274 if (ret != 0) {
275 cs_error(link->handle, RegisterClient, ret);
276 tc589_detach(link);
277 return NULL;
278 }
279
280 return link;
281 } /* tc589_attach */
282
283 /*======================================================================
284
285 This deletes a driver "instance". The device is de-registered
286 with Card Services. If it has been released, all local data
287 structures are freed. Otherwise, the structures will be freed
288 when the device is released.
289
290 ======================================================================*/
291
tc589_detach(dev_link_t * link)292 static void tc589_detach(dev_link_t *link)
293 {
294 struct el3_private *lp = link->priv;
295 dev_link_t **linkp;
296
297 DEBUG(0, "3c589_detach(0x%p)\n", link);
298
299 /* Locate device structure */
300 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
301 if (*linkp == link) break;
302 if (*linkp == NULL)
303 return;
304
305 del_timer(&link->release);
306 if (link->state & DEV_CONFIG) {
307 tc589_release((u_long)link);
308 if (link->state & DEV_STALE_CONFIG) {
309 link->state |= DEV_STALE_LINK;
310 return;
311 }
312 }
313
314 if (link->handle)
315 CardServices(DeregisterClient, link->handle);
316
317 /* Unlink device structure, free bits */
318 *linkp = link->next;
319 if (link->dev)
320 unregister_netdev(&lp->dev);
321 kfree(lp);
322
323 } /* tc589_detach */
324
325 /*======================================================================
326
327 tc589_config() is scheduled to run after a CARD_INSERTION event
328 is received, to configure the PCMCIA socket, and to make the
329 ethernet device available to the system.
330
331 ======================================================================*/
332
333 #define CS_CHECK(fn, args...) \
334 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
335
tc589_config(dev_link_t * link)336 static void tc589_config(dev_link_t *link)
337 {
338 client_handle_t handle = link->handle;
339 struct el3_private *lp = link->priv;
340 struct net_device *dev = &lp->dev;
341 tuple_t tuple;
342 cisparse_t parse;
343 u_short buf[32], *phys_addr;
344 int last_fn, last_ret, i, j, multi = 0;
345 ioaddr_t ioaddr;
346 char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
347
348 DEBUG(0, "3c589_config(0x%p)\n", link);
349
350 phys_addr = (u_short *)dev->dev_addr;
351 tuple.Attributes = 0;
352 tuple.DesiredTuple = CISTPL_CONFIG;
353 CS_CHECK(GetFirstTuple, handle, &tuple);
354 tuple.TupleData = (cisdata_t *)buf;
355 tuple.TupleDataMax = sizeof(buf);
356 tuple.TupleOffset = 0;
357 CS_CHECK(GetTupleData, handle, &tuple);
358 CS_CHECK(ParseTuple, handle, &tuple, &parse);
359 link->conf.ConfigBase = parse.config.base;
360 link->conf.Present = parse.config.rmask[0];
361
362 /* Is this a 3c562? */
363 tuple.DesiredTuple = CISTPL_MANFID;
364 tuple.Attributes = TUPLE_RETURN_COMMON;
365 if ((CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) &&
366 (CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS)) {
367 if (le16_to_cpu(buf[0]) != MANFID_3COM)
368 printk(KERN_INFO "3c589_cs: hmmm, is this really a "
369 "3Com card??\n");
370 multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
371 }
372
373 /* Configure card */
374 link->state |= DEV_CONFIG;
375
376 /* For the 3c562, the base address must be xx00-xx7f */
377 link->io.IOAddrLines = 16;
378 for (i = j = 0; j < 0x400; j += 0x10) {
379 if (multi && (j & 0x80)) continue;
380 link->io.BasePort1 = j ^ 0x300;
381 i = CardServices(RequestIO, link->handle, &link->io);
382 if (i == CS_SUCCESS) break;
383 }
384 if (i != CS_SUCCESS) {
385 cs_error(link->handle, RequestIO, i);
386 goto failed;
387 }
388 CS_CHECK(RequestIRQ, link->handle, &link->irq);
389 CS_CHECK(RequestConfiguration, link->handle, &link->conf);
390
391 dev->irq = link->irq.AssignedIRQ;
392 dev->base_addr = link->io.BasePort1;
393 if (register_netdev(dev) != 0) {
394 printk(KERN_NOTICE "3c589_cs: register_netdev() failed\n");
395 goto failed;
396 }
397
398 ioaddr = dev->base_addr;
399 EL3WINDOW(0);
400
401 /* The 3c589 has an extra EEPROM for configuration info, including
402 the hardware address. The 3c562 puts the address in the CIS. */
403 tuple.DesiredTuple = 0x88;
404 if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) {
405 CardServices(GetTupleData, handle, &tuple);
406 for (i = 0; i < 3; i++)
407 phys_addr[i] = htons(buf[i]);
408 } else {
409 for (i = 0; i < 3; i++)
410 phys_addr[i] = htons(read_eeprom(ioaddr, i));
411 if (phys_addr[0] == 0x6060) {
412 printk(KERN_NOTICE "3c589_cs: IO port conflict at 0x%03lx"
413 "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
414 goto failed;
415 }
416 }
417
418 strcpy(lp->node.dev_name, dev->name);
419 link->dev = &lp->node;
420 link->state &= ~DEV_CONFIG_PENDING;
421
422 /* The address and resource configuration register aren't loaded from
423 the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
424 outw(0x3f00, ioaddr + 8);
425
426 /* The if_port symbol can be set when the module is loaded */
427 if ((if_port >= 0) && (if_port <= 3))
428 dev->if_port = if_port;
429 else
430 printk(KERN_NOTICE "3c589_cs: invalid if_port requested\n");
431
432 printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
433 dev->name, (multi ? "562" : "589"), dev->base_addr,
434 dev->irq);
435 for (i = 0; i < 6; i++)
436 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
437 i = inl(ioaddr);
438 printk(KERN_INFO " %dK FIFO split %s Rx:Tx, %s xcvr\n",
439 (i & 7) ? 32 : 8, ram_split[(i >> 16) & 3],
440 if_names[dev->if_port]);
441 return;
442
443 cs_failed:
444 cs_error(link->handle, last_fn, last_ret);
445 failed:
446 tc589_release((u_long)link);
447 return;
448
449 } /* tc589_config */
450
451 /*======================================================================
452
453 After a card is removed, tc589_release() will unregister the net
454 device, and release the PCMCIA configuration. If the device is
455 still open, this will be postponed until it is closed.
456
457 ======================================================================*/
458
tc589_release(u_long arg)459 static void tc589_release(u_long arg)
460 {
461 dev_link_t *link = (dev_link_t *)arg;
462
463 DEBUG(0, "3c589_release(0x%p)\n", link);
464
465 if (link->open) {
466 DEBUG(1, "3c589_cs: release postponed, '%s' still open\n",
467 link->dev->dev_name);
468 link->state |= DEV_STALE_CONFIG;
469 return;
470 }
471
472 CardServices(ReleaseConfiguration, link->handle);
473 CardServices(ReleaseIO, link->handle, &link->io);
474 CardServices(ReleaseIRQ, link->handle, &link->irq);
475
476 link->state &= ~DEV_CONFIG;
477
478 } /* tc589_release */
479
480 /*======================================================================
481
482 The card status event handler. Mostly, this schedules other
483 stuff to run after an event is received. A CARD_REMOVAL event
484 also sets some flags to discourage the net drivers from trying
485 to talk to the card any more.
486
487 ======================================================================*/
488
tc589_event(event_t event,int priority,event_callback_args_t * args)489 static int tc589_event(event_t event, int priority,
490 event_callback_args_t *args)
491 {
492 dev_link_t *link = args->client_data;
493 struct el3_private *lp = link->priv;
494 struct net_device *dev = &lp->dev;
495
496 DEBUG(1, "3c589_event(0x%06x)\n", event);
497
498 switch (event) {
499 case CS_EVENT_CARD_REMOVAL:
500 link->state &= ~DEV_PRESENT;
501 if (link->state & DEV_CONFIG) {
502 netif_device_detach(dev);
503 mod_timer(&link->release, jiffies + HZ/20);
504 }
505 break;
506 case CS_EVENT_CARD_INSERTION:
507 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
508 tc589_config(link);
509 break;
510 case CS_EVENT_PM_SUSPEND:
511 link->state |= DEV_SUSPEND;
512 /* Fall through... */
513 case CS_EVENT_RESET_PHYSICAL:
514 if (link->state & DEV_CONFIG) {
515 if (link->open)
516 netif_device_detach(dev);
517 CardServices(ReleaseConfiguration, link->handle);
518 }
519 break;
520 case CS_EVENT_PM_RESUME:
521 link->state &= ~DEV_SUSPEND;
522 /* Fall through... */
523 case CS_EVENT_CARD_RESET:
524 if (link->state & DEV_CONFIG) {
525 CardServices(RequestConfiguration, link->handle, &link->conf);
526 if (link->open) {
527 tc589_reset(dev);
528 netif_device_attach(dev);
529 }
530 }
531 break;
532 }
533 return 0;
534 } /* tc589_event */
535
536 /*====================================================================*/
537
538 /*
539 Use this for commands that may take time to finish
540 */
tc589_wait_for_completion(struct net_device * dev,int cmd)541 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
542 {
543 int i = 100;
544 outw(cmd, dev->base_addr + EL3_CMD);
545 while (--i > 0)
546 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
547 if (i == 0)
548 printk(KERN_NOTICE "%s: command 0x%04x did not complete!\n",
549 dev->name, cmd);
550 }
551
552 /*
553 Read a word from the EEPROM using the regular EEPROM access register.
554 Assume that we are in register window zero.
555 */
read_eeprom(ioaddr_t ioaddr,int index)556 static u_short read_eeprom(ioaddr_t ioaddr, int index)
557 {
558 int i;
559 outw(EEPROM_READ + index, ioaddr + 10);
560 /* Reading the eeprom takes 162 us */
561 for (i = 1620; i >= 0; i--)
562 if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
563 break;
564 return inw(ioaddr + 12);
565 }
566
567 /*
568 Set transceiver type, perhaps to something other than what the user
569 specified in dev->if_port.
570 */
tc589_set_xcvr(struct net_device * dev,int if_port)571 static void tc589_set_xcvr(struct net_device *dev, int if_port)
572 {
573 struct el3_private *lp = (struct el3_private *)dev->priv;
574 ioaddr_t ioaddr = dev->base_addr;
575
576 EL3WINDOW(0);
577 switch (if_port) {
578 case 0: case 1: outw(0, ioaddr + 6); break;
579 case 2: outw(3<<14, ioaddr + 6); break;
580 case 3: outw(1<<14, ioaddr + 6); break;
581 }
582 /* On PCMCIA, this just turns on the LED */
583 outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
584 /* 10baseT interface, enable link beat and jabber check. */
585 EL3WINDOW(4);
586 outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
587 EL3WINDOW(1);
588 if (if_port == 2)
589 lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
590 else
591 lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
592 }
593
dump_status(struct net_device * dev)594 static void dump_status(struct net_device *dev)
595 {
596 ioaddr_t ioaddr = dev->base_addr;
597 EL3WINDOW(1);
598 printk(KERN_INFO " irq status %04x, rx status %04x, tx status "
599 "%02x tx free %04x\n", inw(ioaddr+EL3_STATUS),
600 inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
601 inw(ioaddr+TX_FREE));
602 EL3WINDOW(4);
603 printk(KERN_INFO " diagnostics: fifo %04x net %04x ethernet %04x"
604 " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
605 inw(ioaddr+0x08), inw(ioaddr+0x0a));
606 EL3WINDOW(1);
607 }
608
609 /* Reset and restore all of the 3c589 registers. */
tc589_reset(struct net_device * dev)610 static void tc589_reset(struct net_device *dev)
611 {
612 ioaddr_t ioaddr = dev->base_addr;
613 int i;
614
615 EL3WINDOW(0);
616 outw(0x0001, ioaddr + 4); /* Activate board. */
617 outw(0x3f00, ioaddr + 8); /* Set the IRQ line. */
618
619 /* Set the station address in window 2. */
620 EL3WINDOW(2);
621 for (i = 0; i < 6; i++)
622 outb(dev->dev_addr[i], ioaddr + i);
623
624 tc589_set_xcvr(dev, dev->if_port);
625
626 /* Switch to the stats window, and clear all stats by reading. */
627 outw(StatsDisable, ioaddr + EL3_CMD);
628 EL3WINDOW(6);
629 for (i = 0; i < 9; i++)
630 inb(ioaddr+i);
631 inw(ioaddr + 10);
632 inw(ioaddr + 12);
633
634 /* Switch to register set 1 for normal use. */
635 EL3WINDOW(1);
636
637 /* Accept b-cast and phys addr only. */
638 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
639 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
640 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
641 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
642 /* Allow status bits to be seen. */
643 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
644 /* Ack all pending events, and set active indicator mask. */
645 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
646 ioaddr + EL3_CMD);
647 outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
648 | AdapterFailure, ioaddr + EL3_CMD);
649 }
650
netdev_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)651 static void netdev_get_drvinfo(struct net_device *dev,
652 struct ethtool_drvinfo *info)
653 {
654 strcpy(info->driver, DRV_NAME);
655 strcpy(info->version, DRV_VERSION);
656 sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
657 }
658
659 #ifdef PCMCIA_DEBUG
netdev_get_msglevel(struct net_device * dev)660 static u32 netdev_get_msglevel(struct net_device *dev)
661 {
662 return pc_debug;
663 }
664
netdev_set_msglevel(struct net_device * dev,u32 level)665 static void netdev_set_msglevel(struct net_device *dev, u32 level)
666 {
667 pc_debug = level;
668 }
669 #endif /* PCMCIA_DEBUG */
670
671 static struct ethtool_ops netdev_ethtool_ops = {
672 .get_drvinfo = netdev_get_drvinfo,
673 #ifdef PCMCIA_DEBUG
674 .get_msglevel = netdev_get_msglevel,
675 .set_msglevel = netdev_set_msglevel,
676 #endif /* PCMCIA_DEBUG */
677 };
678
el3_config(struct net_device * dev,struct ifmap * map)679 static int el3_config(struct net_device *dev, struct ifmap *map)
680 {
681 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
682 if (map->port <= 3) {
683 dev->if_port = map->port;
684 printk(KERN_INFO "%s: switched to %s port\n",
685 dev->name, if_names[dev->if_port]);
686 tc589_set_xcvr(dev, dev->if_port);
687 } else
688 return -EINVAL;
689 }
690 return 0;
691 }
692
el3_open(struct net_device * dev)693 static int el3_open(struct net_device *dev)
694 {
695 struct el3_private *lp = (struct el3_private *)dev->priv;
696 dev_link_t *link = &lp->link;
697
698 if (!DEV_OK(link))
699 return -ENODEV;
700
701 link->open++;
702 MOD_INC_USE_COUNT;
703 netif_start_queue(dev);
704
705 tc589_reset(dev);
706 lp->media.function = &media_check;
707 lp->media.data = (u_long)lp;
708 lp->media.expires = jiffies + HZ;
709 add_timer(&lp->media);
710
711 DEBUG(1, "%s: opened, status %4.4x.\n",
712 dev->name, inw(dev->base_addr + EL3_STATUS));
713
714 return 0;
715 }
716
el3_tx_timeout(struct net_device * dev)717 static void el3_tx_timeout(struct net_device *dev)
718 {
719 struct el3_private *lp = (struct el3_private *)dev->priv;
720 ioaddr_t ioaddr = dev->base_addr;
721
722 printk(KERN_NOTICE "%s: Transmit timed out!\n", dev->name);
723 dump_status(dev);
724 lp->stats.tx_errors++;
725 dev->trans_start = jiffies;
726 /* Issue TX_RESET and TX_START commands. */
727 tc589_wait_for_completion(dev, TxReset);
728 outw(TxEnable, ioaddr + EL3_CMD);
729 netif_wake_queue(dev);
730 }
731
pop_tx_status(struct net_device * dev)732 static void pop_tx_status(struct net_device *dev)
733 {
734 struct el3_private *lp = (struct el3_private *)dev->priv;
735 ioaddr_t ioaddr = dev->base_addr;
736 int i;
737
738 /* Clear the Tx status stack. */
739 for (i = 32; i > 0; i--) {
740 u_char tx_status = inb(ioaddr + TX_STATUS);
741 if (!(tx_status & 0x84)) break;
742 /* reset transmitter on jabber error or underrun */
743 if (tx_status & 0x30)
744 tc589_wait_for_completion(dev, TxReset);
745 if (tx_status & 0x38) {
746 DEBUG(1, "%s: transmit error: status 0x%02x\n",
747 dev->name, tx_status);
748 outw(TxEnable, ioaddr + EL3_CMD);
749 lp->stats.tx_aborted_errors++;
750 }
751 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
752 }
753 }
754
el3_start_xmit(struct sk_buff * skb,struct net_device * dev)755 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
756 {
757 ioaddr_t ioaddr = dev->base_addr;
758
759 DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
760 "status %4.4x.\n", dev->name, (long)skb->len,
761 inw(ioaddr + EL3_STATUS));
762
763 ((struct el3_private *)dev->priv)->stats.tx_bytes += skb->len;
764
765 /* Put out the doubleword header... */
766 outw(skb->len, ioaddr + TX_FIFO);
767 outw(0x00, ioaddr + TX_FIFO);
768 /* ... and the packet rounded to a doubleword. */
769 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
770
771 dev->trans_start = jiffies;
772 if (inw(ioaddr + TX_FREE) <= 1536) {
773 netif_stop_queue(dev);
774 /* Interrupt us when the FIFO has room for max-sized packet. */
775 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
776 }
777
778 dev_kfree_skb(skb);
779 pop_tx_status(dev);
780
781 return 0;
782 }
783
784 /* The EL3 interrupt handler. */
el3_interrupt(int irq,void * dev_id,struct pt_regs * regs)785 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
786 {
787 struct el3_private *lp = dev_id;
788 struct net_device *dev = &lp->dev;
789 ioaddr_t ioaddr, status;
790 int i = 0;
791
792 if (!netif_device_present(dev))
793 return;
794 ioaddr = dev->base_addr;
795
796 DEBUG(3, "%s: interrupt, status %4.4x.\n",
797 dev->name, inw(ioaddr + EL3_STATUS));
798
799 while ((status = inw(ioaddr + EL3_STATUS)) &
800 (IntLatch | RxComplete | StatsFull)) {
801 if (!netif_device_present(dev) ||
802 ((status & 0xe000) != 0x2000)) {
803 DEBUG(1, "%s: interrupt from dead card\n", dev->name);
804 break;
805 }
806
807 if (status & RxComplete)
808 el3_rx(dev);
809
810 if (status & TxAvailable) {
811 DEBUG(3, " TX room bit was handled.\n");
812 /* There's room in the FIFO for a full-sized packet. */
813 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
814 netif_wake_queue(dev);
815 }
816
817 if (status & TxComplete)
818 pop_tx_status(dev);
819
820 if (status & (AdapterFailure | RxEarly | StatsFull)) {
821 /* Handle all uncommon interrupts. */
822 if (status & StatsFull) /* Empty statistics. */
823 update_stats(dev);
824 if (status & RxEarly) { /* Rx early is unused. */
825 el3_rx(dev);
826 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
827 }
828 if (status & AdapterFailure) {
829 u16 fifo_diag;
830 EL3WINDOW(4);
831 fifo_diag = inw(ioaddr + 4);
832 EL3WINDOW(1);
833 printk(KERN_NOTICE "%s: adapter failure, FIFO diagnostic"
834 " register %04x.\n", dev->name, fifo_diag);
835 if (fifo_diag & 0x0400) {
836 /* Tx overrun */
837 tc589_wait_for_completion(dev, TxReset);
838 outw(TxEnable, ioaddr + EL3_CMD);
839 }
840 if (fifo_diag & 0x2000) {
841 /* Rx underrun */
842 tc589_wait_for_completion(dev, RxReset);
843 set_multicast_list(dev);
844 outw(RxEnable, ioaddr + EL3_CMD);
845 }
846 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
847 }
848 }
849
850 if (++i > 10) {
851 printk(KERN_NOTICE "%s: infinite loop in interrupt, "
852 "status %4.4x.\n", dev->name, status);
853 /* Clear all interrupts */
854 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
855 break;
856 }
857 /* Acknowledge the IRQ. */
858 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
859 }
860
861 lp->last_irq = jiffies;
862 DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
863 dev->name, inw(ioaddr + EL3_STATUS));
864 return;
865 }
866
media_check(u_long arg)867 static void media_check(u_long arg)
868 {
869 struct el3_private *lp = (struct el3_private *)(arg);
870 struct net_device *dev = &lp->dev;
871 ioaddr_t ioaddr = dev->base_addr;
872 u_short media, errs;
873 u_long flags;
874
875 if (!netif_device_present(dev)) goto reschedule;
876
877 EL3WINDOW(1);
878 /* Check for pending interrupt with expired latency timer: with
879 this, we can limp along even if the interrupt is blocked */
880 if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
881 (inb(ioaddr + EL3_TIMER) == 0xff)) {
882 if (!lp->fast_poll)
883 printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
884 el3_interrupt(dev->irq, lp, NULL);
885 lp->fast_poll = HZ;
886 }
887 if (lp->fast_poll) {
888 lp->fast_poll--;
889 lp->media.expires = jiffies + 1;
890 add_timer(&lp->media);
891 return;
892 }
893
894 save_flags(flags);
895 cli();
896 EL3WINDOW(4);
897 media = inw(ioaddr+WN4_MEDIA) & 0xc810;
898
899 /* Ignore collisions unless we've had no irq's recently */
900 if (jiffies - lp->last_irq < HZ) {
901 media &= ~0x0010;
902 } else {
903 /* Try harder to detect carrier errors */
904 EL3WINDOW(6);
905 outw(StatsDisable, ioaddr + EL3_CMD);
906 errs = inb(ioaddr + 0);
907 outw(StatsEnable, ioaddr + EL3_CMD);
908 lp->stats.tx_carrier_errors += errs;
909 if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
910 }
911
912 if (media != lp->media_status) {
913 if ((media & lp->media_status & 0x8000) &&
914 ((lp->media_status ^ media) & 0x0800))
915 printk(KERN_INFO "%s: %s link beat\n", dev->name,
916 (lp->media_status & 0x0800 ? "lost" : "found"));
917 else if ((media & lp->media_status & 0x4000) &&
918 ((lp->media_status ^ media) & 0x0010))
919 printk(KERN_INFO "%s: coax cable %s\n", dev->name,
920 (lp->media_status & 0x0010 ? "ok" : "problem"));
921 if (dev->if_port == 0) {
922 if (media & 0x8000) {
923 if (media & 0x0800)
924 printk(KERN_INFO "%s: flipped to 10baseT\n",
925 dev->name);
926 else
927 tc589_set_xcvr(dev, 2);
928 } else if (media & 0x4000) {
929 if (media & 0x0010)
930 tc589_set_xcvr(dev, 1);
931 else
932 printk(KERN_INFO "%s: flipped to 10base2\n",
933 dev->name);
934 }
935 }
936 lp->media_status = media;
937 }
938
939 EL3WINDOW(1);
940 restore_flags(flags);
941
942 reschedule:
943 lp->media.expires = jiffies + HZ;
944 add_timer(&lp->media);
945 }
946
el3_get_stats(struct net_device * dev)947 static struct net_device_stats *el3_get_stats(struct net_device *dev)
948 {
949 struct el3_private *lp = (struct el3_private *)dev->priv;
950 unsigned long flags;
951 dev_link_t *link = &lp->link;
952
953 if (DEV_OK(link)) {
954 save_flags(flags);
955 cli();
956 update_stats(dev);
957 restore_flags(flags);
958 }
959 return &lp->stats;
960 }
961
962 /*
963 Update statistics. We change to register window 6, so this should be run
964 single-threaded if the device is active. This is expected to be a rare
965 operation, and it's simpler for the rest of the driver to assume that
966 window 1 is always valid rather than use a special window-state variable.
967 */
update_stats(struct net_device * dev)968 static void update_stats(struct net_device *dev)
969 {
970 struct el3_private *lp = (struct el3_private *)dev->priv;
971 ioaddr_t ioaddr = dev->base_addr;
972
973 DEBUG(2, "%s: updating the statistics.\n", dev->name);
974 /* Turn off statistics updates while reading. */
975 outw(StatsDisable, ioaddr + EL3_CMD);
976 /* Switch to the stats window, and read everything. */
977 EL3WINDOW(6);
978 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
979 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
980 /* Multiple collisions. */ inb(ioaddr + 2);
981 lp->stats.collisions += inb(ioaddr + 3);
982 lp->stats.tx_window_errors += inb(ioaddr + 4);
983 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
984 lp->stats.tx_packets += inb(ioaddr + 6);
985 /* Rx packets */ inb(ioaddr + 7);
986 /* Tx deferrals */ inb(ioaddr + 8);
987 /* Rx octets */ inw(ioaddr + 10);
988 /* Tx octets */ inw(ioaddr + 12);
989
990 /* Back to window 1, and turn statistics back on. */
991 EL3WINDOW(1);
992 outw(StatsEnable, ioaddr + EL3_CMD);
993 }
994
el3_rx(struct net_device * dev)995 static int el3_rx(struct net_device *dev)
996 {
997 struct el3_private *lp = (struct el3_private *)dev->priv;
998 ioaddr_t ioaddr = dev->base_addr;
999 int worklimit = 32;
1000 short rx_status;
1001
1002 DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
1003 dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
1004 while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
1005 (--worklimit >= 0)) {
1006 if (rx_status & 0x4000) { /* Error, update stats. */
1007 short error = rx_status & 0x3800;
1008 lp->stats.rx_errors++;
1009 switch (error) {
1010 case 0x0000: lp->stats.rx_over_errors++; break;
1011 case 0x0800: lp->stats.rx_length_errors++; break;
1012 case 0x1000: lp->stats.rx_frame_errors++; break;
1013 case 0x1800: lp->stats.rx_length_errors++; break;
1014 case 0x2000: lp->stats.rx_frame_errors++; break;
1015 case 0x2800: lp->stats.rx_crc_errors++; break;
1016 }
1017 } else {
1018 short pkt_len = rx_status & 0x7ff;
1019 struct sk_buff *skb;
1020
1021 skb = dev_alloc_skb(pkt_len+5);
1022
1023 DEBUG(3, " Receiving packet size %d status %4.4x.\n",
1024 pkt_len, rx_status);
1025 if (skb != NULL) {
1026 skb->dev = dev;
1027 skb_reserve(skb, 2);
1028 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
1029 (pkt_len+3)>>2);
1030 skb->protocol = eth_type_trans(skb, dev);
1031 netif_rx(skb);
1032 dev->last_rx = jiffies;
1033 lp->stats.rx_packets++;
1034 lp->stats.rx_bytes += pkt_len;
1035 } else {
1036 DEBUG(1, "%s: couldn't allocate a sk_buff of"
1037 " size %d.\n", dev->name, pkt_len);
1038 lp->stats.rx_dropped++;
1039 }
1040 }
1041 /* Pop the top of the Rx FIFO */
1042 tc589_wait_for_completion(dev, RxDiscard);
1043 }
1044 if (worklimit == 0)
1045 printk(KERN_NOTICE "%s: too much work in el3_rx!\n", dev->name);
1046 return 0;
1047 }
1048
set_multicast_list(struct net_device * dev)1049 static void set_multicast_list(struct net_device *dev)
1050 {
1051 struct el3_private *lp = dev->priv;
1052 dev_link_t *link = &lp->link;
1053 ioaddr_t ioaddr = dev->base_addr;
1054 u_short opts = SetRxFilter | RxStation | RxBroadcast;
1055
1056 if (!(DEV_OK(link))) return;
1057 if (dev->flags & IFF_PROMISC)
1058 opts |= RxMulticast | RxProm;
1059 else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1060 opts |= RxMulticast;
1061 outw(opts, ioaddr + EL3_CMD);
1062 }
1063
el3_close(struct net_device * dev)1064 static int el3_close(struct net_device *dev)
1065 {
1066 struct el3_private *lp = dev->priv;
1067 dev_link_t *link = &lp->link;
1068 ioaddr_t ioaddr = dev->base_addr;
1069
1070 DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1071
1072 if (DEV_OK(link)) {
1073 /* Turn off statistics ASAP. We update lp->stats below. */
1074 outw(StatsDisable, ioaddr + EL3_CMD);
1075
1076 /* Disable the receiver and transmitter. */
1077 outw(RxDisable, ioaddr + EL3_CMD);
1078 outw(TxDisable, ioaddr + EL3_CMD);
1079
1080 if (dev->if_port == 2)
1081 /* Turn off thinnet power. Green! */
1082 outw(StopCoax, ioaddr + EL3_CMD);
1083 else if (dev->if_port == 1) {
1084 /* Disable link beat and jabber */
1085 EL3WINDOW(4);
1086 outw(0, ioaddr + WN4_MEDIA);
1087 }
1088
1089 /* Switching back to window 0 disables the IRQ. */
1090 EL3WINDOW(0);
1091 /* But we explicitly zero the IRQ line select anyway. */
1092 outw(0x0f00, ioaddr + WN0_IRQ);
1093
1094 /* Check if the card still exists */
1095 if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1096 update_stats(dev);
1097 }
1098
1099 link->open--;
1100 netif_stop_queue(dev);
1101 del_timer(&lp->media);
1102 if (link->state & DEV_STALE_CONFIG)
1103 mod_timer(&link->release, jiffies + HZ/20);
1104
1105 MOD_DEC_USE_COUNT;
1106
1107 return 0;
1108 }
1109
1110 /*====================================================================*/
1111
init_3c589_cs(void)1112 static int __init init_3c589_cs(void)
1113 {
1114 servinfo_t serv;
1115 DEBUG(0, "%s\n", version);
1116 CardServices(GetCardServicesInfo, &serv);
1117 if (serv.Revision != CS_RELEASE_CODE) {
1118 printk(KERN_NOTICE "3c589_cs: Card Services release "
1119 "does not match!\n");
1120 return -1;
1121 }
1122 register_pccard_driver(&dev_info, &tc589_attach, &tc589_detach);
1123 return 0;
1124 }
1125
exit_3c589_cs(void)1126 static void __exit exit_3c589_cs(void)
1127 {
1128 DEBUG(0, "3c589_cs: unloading\n");
1129 unregister_pccard_driver(&dev_info);
1130 while (dev_list != NULL)
1131 tc589_detach(dev_list);
1132 }
1133
1134 module_init(init_3c589_cs);
1135 module_exit(exit_3c589_cs);
1136