blob: a7b0e5e43a529f6f5cb5ecf08f41d94ec97b5efb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 net-3-driver for the 3c523 Etherlink/MC card (i82586 Ethernet chip)
3
4
5 This is an extension to the Linux operating system, and is covered by the
6 same GNU General Public License that covers that work.
7
8 Copyright 1995, 1996 by Chris Beauregard (cpbeaure@undergrad.math.uwaterloo.ca)
9
10 This is basically Michael Hipp's ni52 driver, with a new probing
11 algorithm and some minor changes to the 82586 CA and reset routines.
12 Thanks a lot Michael for a really clean i82586 implementation! Unless
13 otherwise documented in ni52.c, any bugs are mine.
14
15 Contrary to the Ethernet-HOWTO, this isn't based on the 3c507 driver in
16 any way. The ni52 is a lot easier to modify.
17
18 sources:
19 ni52.c
20
21 Crynwr packet driver collection was a great reference for my first
22 attempt at this sucker. The 3c507 driver also helped, until I noticed
23 that ni52.c was a lot nicer.
24
25 EtherLink/MC: Micro Channel Ethernet Adapter Technical Reference
26 Manual, courtesy of 3Com CardFacts, documents the 3c523-specific
27 stuff. Information on CardFacts is found in the Ethernet HOWTO.
28 Also see <a href="http://www.3com.com/">
29
30 Microprocessor Communications Support Chips, T.J. Byers, ISBN
31 0-444-01224-9, has a section on the i82586. It tells you just enough
32 to know that you really don't want to learn how to program the chip.
33
34 The original device probe code was stolen from ps2esdi.c
35
36 Known Problems:
37 Since most of the code was stolen from ni52.c, you'll run across the
38 same bugs in the 0.62 version of ni52.c, plus maybe a few because of
39 the 3c523 idiosynchacies. The 3c523 has 16K of RAM though, so there
40 shouldn't be the overrun problem that the 8K ni52 has.
41
42 This driver is for a 16K adapter. It should work fine on the 64K
43 adapters, but it will only use one of the 4 banks of RAM. Modifying
44 this for the 64K version would require a lot of heinous bank
45 switching, which I'm sure not interested in doing. If you try to
46 implement a bank switching version, you'll basically have to remember
47 what bank is enabled and do a switch everytime you access a memory
48 location that's not current. You'll also have to remap pointers on
49 the driver side, because it only knows about 16K of the memory.
50 Anyone desperate or masochistic enough to try?
51
52 It seems to be stable now when multiple transmit buffers are used. I
53 can't see any performance difference, but then I'm working on a 386SX.
54
55 Multicast doesn't work. It doesn't even pretend to work. Don't use
56 it. Don't compile your kernel with multicast support. I don't know
57 why.
58
59 Features:
60 This driver is useable as a loadable module. If you try to specify an
61 IRQ or a IO address (via insmod 3c523.o irq=xx io=0xyyy), it will
62 search the MCA slots until it finds a 3c523 with the specified
63 parameters.
64
65 This driver does support multiple ethernet cards when used as a module
66 (up to MAX_3C523_CARDS, the default being 4)
67
68 This has been tested with both BNC and TP versions, internal and
69 external transceivers. Haven't tested with the 64K version (that I
70 know of).
71
72 History:
73 Jan 1st, 1996
74 first public release
75 Feb 4th, 1996
76 update to 1.3.59, incorporated multicast diffs from ni52.c
77 Feb 15th, 1996
78 added shared irq support
79 Apr 1999
80 added support for multiple cards when used as a module
81 added option to disable multicast as is causes problems
82 Ganesh Sittampalam <ganesh.sittampalam@magdalen.oxford.ac.uk>
83 Stuart Adamson <stuart.adamson@compsoc.net>
84 Nov 2001
85 added support for ethtool (jgarzik)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 $Header: /fsys2/home/chrisb/linux-1.3.59-MCA/drivers/net/RCS/3c523.c,v 1.1 1996/02/05 01:53:46 chrisb Exp chrisb $
88 */
89
90#define DRV_NAME "3c523"
91#define DRV_VERSION "17-Nov-2001"
92
93#include <linux/init.h>
94#include <linux/netdevice.h>
95#include <linux/etherdevice.h>
96#include <linux/module.h>
97#include <linux/kernel.h>
98#include <linux/string.h>
99#include <linux/errno.h>
100#include <linux/ioport.h>
101#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/interrupt.h>
103#include <linux/delay.h>
104#include <linux/mca-legacy.h>
105#include <linux/ethtool.h>
106#include <linux/bitops.h>
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800107#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109#include <asm/uaccess.h>
110#include <asm/processor.h>
111#include <asm/io.h>
112
113#include "3c523.h"
114
115/*************************************************************************/
116#define DEBUG /* debug on */
117#define SYSBUSVAL 0 /* 1 = 8 Bit, 0 = 16 bit - 3c523 only does 16 bit */
118#undef ELMC_MULTICAST /* Disable multicast support as it is somewhat seriously broken at the moment */
119
120#define make32(ptr16) (p->memtop + (short) (ptr16) )
121#define make24(ptr32) ((char *) (ptr32) - p->base)
122#define make16(ptr32) ((unsigned short) ((unsigned long) (ptr32) - (unsigned long) p->memtop ))
123
124/*************************************************************************/
125/*
126 Tables to which we can map values in the configuration registers.
127 */
128static int irq_table[] __initdata = {
129 12, 7, 3, 9
130};
131
132static int csr_table[] __initdata = {
133 0x300, 0x1300, 0x2300, 0x3300
134};
135
136static int shm_table[] __initdata = {
137 0x0c0000, 0x0c8000, 0x0d0000, 0x0d8000
138};
139
140/******************* how to calculate the buffers *****************************
141
142
143 * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
144 * --------------- in a different (more stable?) mode. Only in this mode it's
145 * possible to configure the driver with 'NO_NOPCOMMANDS'
146
147sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
148sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
149sizeof(rfd) = 24; sizeof(rbd) = 12;
150sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
151sizeof(nop_cmd) = 8;
152
153 * if you don't know the driver, better do not change this values: */
154
155#define RECV_BUFF_SIZE 1524 /* slightly oversized */
156#define XMIT_BUFF_SIZE 1524 /* slightly oversized */
157#define NUM_XMIT_BUFFS 1 /* config for both, 8K and 16K shmem */
158#define NUM_RECV_BUFFS_8 4 /* config for 8K shared mem */
159#define NUM_RECV_BUFFS_16 9 /* config for 16K shared mem */
160
161#if (NUM_XMIT_BUFFS == 1)
162#define NO_NOPCOMMANDS /* only possible with NUM_XMIT_BUFFS=1 */
163#endif
164
165/**************************************************************************/
166
167#define DELAY(x) { mdelay(32 * x); }
168
169/* a much shorter delay: */
170#define DELAY_16(); { udelay(16) ; }
171
172/* wait for command with timeout: */
173#define WAIT_4_SCB_CMD() { int i; \
174 for(i=0;i<1024;i++) { \
175 if(!p->scb->cmd) break; \
176 DELAY_16(); \
177 if(i == 1023) { \
Alexander Beregalov39738e12009-05-26 12:35:26 +0000178 pr_warning("%s:%d: scb_cmd timed out .. resetting i82586\n",\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dev->name,__LINE__); \
180 elmc_id_reset586(); } } }
181
David Howells7d12e782006-10-05 14:55:46 +0100182static irqreturn_t elmc_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int elmc_open(struct net_device *dev);
184static int elmc_close(struct net_device *dev);
Stephen Hemminger27a1de92009-08-31 19:50:54 +0000185static netdev_tx_t elmc_send_packet(struct sk_buff *, struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static struct net_device_stats *elmc_get_stats(struct net_device *dev);
187static void elmc_timeout(struct net_device *dev);
188#ifdef ELMC_MULTICAST
189static void set_multicast_list(struct net_device *dev);
190#endif
Jeff Garzik7282d492006-09-13 14:30:00 -0400191static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/* helper-functions */
194static int init586(struct net_device *dev);
195static int check586(struct net_device *dev, unsigned long where, unsigned size);
196static void alloc586(struct net_device *dev);
197static void startrecv586(struct net_device *dev);
198static void *alloc_rfa(struct net_device *dev, void *ptr);
199static void elmc_rcv_int(struct net_device *dev);
200static void elmc_xmt_int(struct net_device *dev);
201static void elmc_rnr_int(struct net_device *dev);
202
203struct priv {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned long base;
205 char *memtop;
206 unsigned long mapped_start; /* Start of ioremap */
207 volatile struct rfd_struct *rfd_last, *rfd_top, *rfd_first;
208 volatile struct scp_struct *scp; /* volatile is important */
209 volatile struct iscp_struct *iscp; /* volatile is important */
210 volatile struct scb_struct *scb; /* volatile is important */
211 volatile struct tbd_struct *xmit_buffs[NUM_XMIT_BUFFS];
212#if (NUM_XMIT_BUFFS == 1)
213 volatile struct transmit_cmd_struct *xmit_cmds[2];
214 volatile struct nop_cmd_struct *nop_cmds[2];
215#else
216 volatile struct transmit_cmd_struct *xmit_cmds[NUM_XMIT_BUFFS];
217 volatile struct nop_cmd_struct *nop_cmds[NUM_XMIT_BUFFS];
218#endif
219 volatile int nop_point, num_recv_buffs;
220 volatile char *xmit_cbuffs[NUM_XMIT_BUFFS];
221 volatile int xmit_count, xmit_last;
222 volatile int slot;
223};
224
225#define elmc_attn586() {elmc_do_attn586(dev->base_addr,ELMC_CTRL_INTE);}
226#define elmc_reset586() {elmc_do_reset586(dev->base_addr,ELMC_CTRL_INTE);}
227
228/* with interrupts disabled - this will clear the interrupt bit in the
229 3c523 control register, and won't put it back. This effectively
230 disables interrupts on the card. */
231#define elmc_id_attn586() {elmc_do_attn586(dev->base_addr,0);}
232#define elmc_id_reset586() {elmc_do_reset586(dev->base_addr,0);}
233
234/*************************************************************************/
235/*
236 Do a Channel Attention on the 3c523. This is extremely board dependent.
237 */
238static void elmc_do_attn586(int ioaddr, int ints)
239{
240 /* the 3c523 requires a minimum of 500 ns. The delays here might be
241 a little too large, and hence they may cut the performance of the
242 card slightly. If someone who knows a little more about Linux
243 timing would care to play with these, I'd appreciate it. */
244
245 /* this bit masking stuff is crap. I'd rather have separate
246 registers with strobe triggers for each of these functions. <sigh>
247 Ya take what ya got. */
248
249 outb(ELMC_CTRL_RST | 0x3 | ELMC_CTRL_CA | ints, ioaddr + ELMC_CTRL);
250 DELAY_16(); /* > 500 ns */
251 outb(ELMC_CTRL_RST | 0x3 | ints, ioaddr + ELMC_CTRL);
252}
253
254/*************************************************************************/
255/*
256 Reset the 82586 on the 3c523. Also very board dependent.
257 */
258static void elmc_do_reset586(int ioaddr, int ints)
259{
260 /* toggle the RST bit low then high */
261 outb(0x3 | ELMC_CTRL_LBK, ioaddr + ELMC_CTRL);
262 DELAY_16(); /* > 500 ns */
263 outb(ELMC_CTRL_RST | ELMC_CTRL_LBK | 0x3, ioaddr + ELMC_CTRL);
264
265 elmc_do_attn586(ioaddr, ints);
266}
267
268/**********************************************
269 * close device
270 */
271
272static int elmc_close(struct net_device *dev)
273{
274 netif_stop_queue(dev);
275 elmc_id_reset586(); /* the hard way to stop the receiver */
276 free_irq(dev->irq, dev);
277 return 0;
278}
279
280/**********************************************
281 * open device
282 */
283
284static int elmc_open(struct net_device *dev)
285{
286 int ret;
287
288 elmc_id_attn586(); /* disable interrupts */
289
Joe Perchesa0607fd2009-11-18 23:29:17 -0800290 ret = request_irq(dev->irq, elmc_interrupt, IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dev->name, dev);
292 if (ret) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000293 pr_err("%s: couldn't get irq %d\n", dev->name, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 elmc_id_reset586();
295 return ret;
296 }
297 alloc586(dev);
298 init586(dev);
299 startrecv586(dev);
300 netif_start_queue(dev);
301 return 0; /* most done by init */
302}
303
304/**********************************************
305 * Check to see if there's an 82586 out there.
306 */
307
308static int __init check586(struct net_device *dev, unsigned long where, unsigned size)
309{
Wang Chen454d7c92008-11-12 23:37:49 -0800310 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 char *iscp_addrs[2];
312 int i = 0;
313
314 p->base = (unsigned long) isa_bus_to_virt((unsigned long)where) + size - 0x01000000;
315 p->memtop = isa_bus_to_virt((unsigned long)where) + size;
316 p->scp = (struct scp_struct *)(p->base + SCP_DEFAULT_ADDRESS);
317 memset((char *) p->scp, 0, sizeof(struct scp_struct));
318 p->scp->sysbus = SYSBUSVAL; /* 1 = 8Bit-Bus, 0 = 16 Bit */
319
320 iscp_addrs[0] = isa_bus_to_virt((unsigned long)where);
321 iscp_addrs[1] = (char *) p->scp - sizeof(struct iscp_struct);
322
323 for (i = 0; i < 2; i++) {
324 p->iscp = (struct iscp_struct *) iscp_addrs[i];
325 memset((char *) p->iscp, 0, sizeof(struct iscp_struct));
326
327 p->scp->iscp = make24(p->iscp);
328 p->iscp->busy = 1;
329
330 elmc_id_reset586();
331
332 /* reset586 does an implicit CA */
333
334 /* apparently, you sometimes have to kick the 82586 twice... */
335 elmc_id_attn586();
336 DELAY(1);
337
338 if (p->iscp->busy) { /* i82586 clears 'busy' after successful init */
339 return 0;
340 }
341 }
342 return 1;
343}
344
345/******************************************************************
346 * set iscp at the right place, called by elmc_probe and open586.
347 */
348
Hannes Ederdac499f2008-12-25 23:56:45 -0800349static void alloc586(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Wang Chen454d7c92008-11-12 23:37:49 -0800351 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 elmc_id_reset586();
354 DELAY(2);
355
356 p->scp = (struct scp_struct *) (p->base + SCP_DEFAULT_ADDRESS);
357 p->scb = (struct scb_struct *) isa_bus_to_virt(dev->mem_start);
358 p->iscp = (struct iscp_struct *) ((char *) p->scp - sizeof(struct iscp_struct));
359
360 memset((char *) p->iscp, 0, sizeof(struct iscp_struct));
361 memset((char *) p->scp, 0, sizeof(struct scp_struct));
362
363 p->scp->iscp = make24(p->iscp);
364 p->scp->sysbus = SYSBUSVAL;
365 p->iscp->scb_offset = make16(p->scb);
366
367 p->iscp->busy = 1;
368 elmc_id_reset586();
369 elmc_id_attn586();
370
371 DELAY(2);
372
Alexander Beregalov39738e12009-05-26 12:35:26 +0000373 if (p->iscp->busy)
374 pr_err("%s: Init-Problems (alloc).\n", dev->name);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 memset((char *) p->scb, 0, sizeof(struct scb_struct));
377}
378
379/*****************************************************************/
380
381static int elmc_getinfo(char *buf, int slot, void *d)
382{
383 int len = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700384 struct net_device *dev = d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (dev == NULL)
387 return len;
388
389 len += sprintf(buf + len, "Revision: 0x%x\n",
390 inb(dev->base_addr + ELMC_REVISION) & 0xf);
391 len += sprintf(buf + len, "IRQ: %d\n", dev->irq);
392 len += sprintf(buf + len, "IO Address: %#lx-%#lx\n", dev->base_addr,
393 dev->base_addr + ELMC_IO_EXTENT);
394 len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
395 dev->mem_end - 1);
396 len += sprintf(buf + len, "Transceiver: %s\n", dev->if_port ?
397 "External" : "Internal");
398 len += sprintf(buf + len, "Device: %s\n", dev->name);
Johannes Berge1749612008-10-27 15:59:26 -0700399 len += sprintf(buf + len, "Hardware Address: %pM\n",
400 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 return len;
403} /* elmc_getinfo() */
404
Stephen Hemminger90e64c62009-01-09 13:01:16 +0000405static const struct net_device_ops netdev_ops = {
406 .ndo_open = elmc_open,
407 .ndo_stop = elmc_close,
408 .ndo_get_stats = elmc_get_stats,
409 .ndo_start_xmit = elmc_send_packet,
410 .ndo_tx_timeout = elmc_timeout,
411#ifdef ELMC_MULTICAST
412 .ndo_set_multicast_list = set_multicast_list,
413#endif
414 .ndo_change_mtu = eth_change_mtu,
415 .ndo_set_mac_address = eth_mac_addr,
416 .ndo_validate_addr = eth_validate_addr,
417};
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*****************************************************************/
420
421static int __init do_elmc_probe(struct net_device *dev)
422{
423 static int slot;
424 int base_addr = dev->base_addr;
425 int irq = dev->irq;
426 u_char status = 0;
427 u_char revision = 0;
428 int i = 0;
429 unsigned int size = 0;
430 int retval;
Wang Chen454d7c92008-11-12 23:37:49 -0800431 struct priv *pr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (MCA_bus == 0) {
434 return -ENODEV;
435 }
436 /* search through the slots for the 3c523. */
437 slot = mca_find_adapter(ELMC_MCA_ID, 0);
438 while (slot != -1) {
439 status = mca_read_stored_pos(slot, 2);
440
441 dev->irq=irq_table[(status & ELMC_STATUS_IRQ_SELECT) >> 6];
442 dev->base_addr=csr_table[(status & ELMC_STATUS_CSR_SELECT) >> 1];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 If we're trying to match a specified irq or IO address,
446 we'll reject a match unless it's what we're looking for.
447 Also reject it if the card is already in use.
448 */
449
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400450 if ((irq && irq != dev->irq) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 (base_addr && base_addr != dev->base_addr)) {
452 slot = mca_find_adapter(ELMC_MCA_ID, slot + 1);
453 continue;
454 }
455 if (!request_region(dev->base_addr, ELMC_IO_EXTENT, DRV_NAME)) {
456 slot = mca_find_adapter(ELMC_MCA_ID, slot + 1);
457 continue;
458 }
459
460 /* found what we're looking for... */
461 break;
462 }
463
464 /* we didn't find any 3c523 in the slots we checked for */
465 if (slot == MCA_NOTFOUND)
466 return ((base_addr || irq) ? -ENXIO : -ENODEV);
467
468 mca_set_adapter_name(slot, "3Com 3c523 Etherlink/MC");
469 mca_set_adapter_procfn(slot, (MCA_ProcFn) elmc_getinfo, dev);
470
471 /* if we get this far, adapter has been found - carry on */
Alexander Beregalov39738e12009-05-26 12:35:26 +0000472 pr_info("%s: 3c523 adapter found in slot %d\n", dev->name, slot + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* Now we extract configuration info from the card.
475 The 3c523 provides information in two of the POS registers, but
476 the second one is only needed if we want to tell the card what IRQ
477 to use. I suspect that whoever sets the thing up initially would
478 prefer we don't screw with those things.
479
480 Note that we read the status info when we found the card...
481
482 See 3c523.h for more details.
483 */
484
485 /* revision is stored in the first 4 bits of the revision register */
486 revision = inb(dev->base_addr + ELMC_REVISION) & 0xf;
487
488 /* according to docs, we read the interrupt and write it back to
489 the IRQ select register, since the POST might not configure the IRQ
490 properly. */
491 switch (dev->irq) {
492 case 3:
493 mca_write_pos(slot, 3, 0x04);
494 break;
495 case 7:
496 mca_write_pos(slot, 3, 0x02);
497 break;
498 case 9:
499 mca_write_pos(slot, 3, 0x08);
500 break;
501 case 12:
502 mca_write_pos(slot, 3, 0x01);
503 break;
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 pr->slot = slot;
507
Alexander Beregalov39738e12009-05-26 12:35:26 +0000508 pr_info("%s: 3Com 3c523 Rev 0x%x at %#lx\n", dev->name, (int) revision,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 dev->base_addr);
510
511 /* Determine if we're using the on-board transceiver (i.e. coax) or
512 an external one. The information is pretty much useless, but I
513 guess it's worth brownie points. */
514 dev->if_port = (status & ELMC_STATUS_DISABLE_THIN);
515
516 /* The 3c523 has a 24K chunk of memory. The first 16K is the
517 shared memory, while the last 8K is for the EtherStart BIOS ROM.
518 Which we don't care much about here. We'll just tell Linux that
519 we're using 16K. MCA won't permit address space conflicts caused
520 by not mapping the other 8K. */
521 dev->mem_start = shm_table[(status & ELMC_STATUS_MEMORY_SELECT) >> 3];
522
523 /* We're using MCA, so it's a given that the information about memory
524 size is correct. The Crynwr drivers do something like this. */
525
526 elmc_id_reset586(); /* seems like a good idea before checking it... */
527
528 size = 0x4000; /* check for 16K mem */
529 if (!check586(dev, dev->mem_start, size)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000530 pr_err("%s: memprobe, Can't find memory at 0x%lx!\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dev->mem_start);
532 retval = -ENODEV;
533 goto err_out;
534 }
535 dev->mem_end = dev->mem_start + size; /* set mem_end showed by 'ifconfig' */
536
537 pr->memtop = isa_bus_to_virt(dev->mem_start) + size;
538 pr->base = (unsigned long) isa_bus_to_virt(dev->mem_start) + size - 0x01000000;
539 alloc586(dev);
540
541 elmc_id_reset586(); /* make sure it doesn't generate spurious ints */
542
543 /* set number of receive-buffs according to memsize */
544 pr->num_recv_buffs = NUM_RECV_BUFFS_16;
545
546 /* dump all the assorted information */
Alexander Beregalov39738e12009-05-26 12:35:26 +0000547 pr_info("%s: IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->name,
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400548 dev->irq, dev->if_port ? "ex" : "in",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dev->mem_start, dev->mem_end - 1);
550
551 /* The hardware address for the 3c523 is stored in the first six
552 bytes of the IO address. */
Joe Perches0795af52007-10-03 17:59:30 -0700553 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 dev->dev_addr[i] = inb(dev->base_addr + i);
Joe Perches0795af52007-10-03 17:59:30 -0700555
Alexander Beregalov39738e12009-05-26 12:35:26 +0000556 pr_info("%s: hardware address %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700557 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Stephen Hemminger90e64c62009-01-09 13:01:16 +0000559 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 dev->ethtool_ops = &netdev_ethtool_ops;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* note that we haven't actually requested the IRQ from the kernel.
564 That gets done in elmc_open(). I'm not sure that's such a good idea,
565 but it works, so I'll go with it. */
566
567#ifndef ELMC_MULTICAST
568 dev->flags&=~IFF_MULTICAST; /* Multicast doesn't work */
569#endif
570
b1fc5502005-05-12 20:11:55 -0400571 retval = register_netdev(dev);
572 if (retval)
573 goto err_out;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
576err_out:
577 mca_set_adapter_procfn(slot, NULL, NULL);
578 release_region(dev->base_addr, ELMC_IO_EXTENT);
579 return retval;
580}
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400581
Ingo Molnar46a84942008-11-25 17:02:20 -0800582#ifdef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static void cleanup_card(struct net_device *dev)
584{
Wang Chen454d7c92008-11-12 23:37:49 -0800585 mca_set_adapter_procfn(((struct priv *)netdev_priv(dev))->slot,
586 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 release_region(dev->base_addr, ELMC_IO_EXTENT);
588}
Ingo Molnar46a84942008-11-25 17:02:20 -0800589#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590struct net_device * __init elmc_probe(int unit)
591{
592 struct net_device *dev = alloc_etherdev(sizeof(struct priv));
593 int err;
594
595 if (!dev)
596 return ERR_PTR(-ENOMEM);
597
598 sprintf(dev->name, "eth%d", unit);
599 netdev_boot_setup_check(dev);
600
601 err = do_elmc_probe(dev);
602 if (err)
603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605out:
606 free_netdev(dev);
607 return ERR_PTR(err);
608}
609#endif
610
611/**********************************************
612 * init the chip (elmc-interrupt should be disabled?!)
613 * needs a correct 'allocated' memory
614 */
615
616static int init586(struct net_device *dev)
617{
618 void *ptr;
619 unsigned long s;
620 int i, result = 0;
Wang Chen454d7c92008-11-12 23:37:49 -0800621 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 volatile struct configure_cmd_struct *cfg_cmd;
623 volatile struct iasetup_cmd_struct *ias_cmd;
624 volatile struct tdr_cmd_struct *tdr_cmd;
625 volatile struct mcsetup_cmd_struct *mc_cmd;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000626 struct netdev_hw_addr *ha;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000627 int num_addrs = netdev_mc_count(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 ptr = (void *) ((char *) p->scb + sizeof(struct scb_struct));
630
631 cfg_cmd = (struct configure_cmd_struct *) ptr; /* configure-command */
632 cfg_cmd->cmd_status = 0;
633 cfg_cmd->cmd_cmd = CMD_CONFIGURE | CMD_LAST;
634 cfg_cmd->cmd_link = 0xffff;
635
636 cfg_cmd->byte_cnt = 0x0a; /* number of cfg bytes */
637 cfg_cmd->fifo = 0x08; /* fifo-limit (8=tx:32/rx:64) */
638 cfg_cmd->sav_bf = 0x40; /* hold or discard bad recv frames (bit 7) */
639 cfg_cmd->adr_len = 0x2e; /* addr_len |!src_insert |pre-len |loopback */
640 cfg_cmd->priority = 0x00;
641 cfg_cmd->ifs = 0x60;
642 cfg_cmd->time_low = 0x00;
643 cfg_cmd->time_high = 0xf2;
644 cfg_cmd->promisc = 0;
Wang Chenc16d1182008-07-22 13:13:12 +0800645 if (dev->flags & (IFF_ALLMULTI | IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 cfg_cmd->promisc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 cfg_cmd->carr_coll = 0x00;
648
649 p->scb->cbl_offset = make16(cfg_cmd);
650
651 p->scb->cmd = CUC_START; /* cmd.-unit start */
652 elmc_id_attn586();
653
654 s = jiffies; /* warning: only active with interrupts on !! */
655 while (!(cfg_cmd->cmd_status & STAT_COMPL)) {
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800656 if (time_after(jiffies, s + 30*HZ/100))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 }
659
660 if ((cfg_cmd->cmd_status & (STAT_OK | STAT_COMPL)) != (STAT_COMPL | STAT_OK)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000661 pr_warning("%s (elmc): configure command failed: %x\n", dev->name, cfg_cmd->cmd_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 1;
663 }
664 /*
665 * individual address setup
666 */
667 ias_cmd = (struct iasetup_cmd_struct *) ptr;
668
669 ias_cmd->cmd_status = 0;
670 ias_cmd->cmd_cmd = CMD_IASETUP | CMD_LAST;
671 ias_cmd->cmd_link = 0xffff;
672
673 memcpy((char *) &ias_cmd->iaddr, (char *) dev->dev_addr, ETH_ALEN);
674
675 p->scb->cbl_offset = make16(ias_cmd);
676
677 p->scb->cmd = CUC_START; /* cmd.-unit start */
678 elmc_id_attn586();
679
680 s = jiffies;
681 while (!(ias_cmd->cmd_status & STAT_COMPL)) {
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800682 if (time_after(jiffies, s + 30*HZ/100))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684 }
685
686 if ((ias_cmd->cmd_status & (STAT_OK | STAT_COMPL)) != (STAT_OK | STAT_COMPL)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000687 pr_warning("%s (elmc): individual address setup command failed: %04x\n",
688 dev->name, ias_cmd->cmd_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 1;
690 }
691 /*
692 * TDR, wire check .. e.g. no resistor e.t.c
693 */
694 tdr_cmd = (struct tdr_cmd_struct *) ptr;
695
696 tdr_cmd->cmd_status = 0;
697 tdr_cmd->cmd_cmd = CMD_TDR | CMD_LAST;
698 tdr_cmd->cmd_link = 0xffff;
699 tdr_cmd->status = 0;
700
701 p->scb->cbl_offset = make16(tdr_cmd);
702
703 p->scb->cmd = CUC_START; /* cmd.-unit start */
704 elmc_attn586();
705
706 s = jiffies;
707 while (!(tdr_cmd->cmd_status & STAT_COMPL)) {
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800708 if (time_after(jiffies, s + 30*HZ/100)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000709 pr_warning("%s: %d Problems while running the TDR.\n", dev->name, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 result = 1;
711 break;
712 }
713 }
714
715 if (!result) {
716 DELAY(2); /* wait for result */
717 result = tdr_cmd->status;
718
719 p->scb->cmd = p->scb->status & STAT_MASK;
720 elmc_id_attn586(); /* ack the interrupts */
721
722 if (result & TDR_LNK_OK) {
723 /* empty */
724 } else if (result & TDR_XCVR_PRB) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000725 pr_warning("%s: TDR: Transceiver problem!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 } else if (result & TDR_ET_OPN) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000727 pr_warning("%s: TDR: No correct termination %d clocks away.\n", dev->name, result & TDR_TIMEMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } else if (result & TDR_ET_SRT) {
729 if (result & TDR_TIMEMASK) /* time == 0 -> strange :-) */
Alexander Beregalov39738e12009-05-26 12:35:26 +0000730 pr_warning("%s: TDR: Detected a short circuit %d clocks away.\n", dev->name, result & TDR_TIMEMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000732 pr_warning("%s: TDR: Unknown status %04x\n", dev->name, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735 /*
736 * ack interrupts
737 */
738 p->scb->cmd = p->scb->status & STAT_MASK;
739 elmc_id_attn586();
740
741 /*
742 * alloc nop/xmit-cmds
743 */
744#if (NUM_XMIT_BUFFS == 1)
745 for (i = 0; i < 2; i++) {
746 p->nop_cmds[i] = (struct nop_cmd_struct *) ptr;
747 p->nop_cmds[i]->cmd_cmd = CMD_NOP;
748 p->nop_cmds[i]->cmd_status = 0;
749 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
750 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
751 }
752 p->xmit_cmds[0] = (struct transmit_cmd_struct *) ptr; /* transmit cmd/buff 0 */
753 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
754#else
755 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
756 p->nop_cmds[i] = (struct nop_cmd_struct *) ptr;
757 p->nop_cmds[i]->cmd_cmd = CMD_NOP;
758 p->nop_cmds[i]->cmd_status = 0;
759 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
760 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
761 p->xmit_cmds[i] = (struct transmit_cmd_struct *) ptr; /*transmit cmd/buff 0 */
762 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
763 }
764#endif
765
766 ptr = alloc_rfa(dev, (void *) ptr); /* init receive-frame-area */
767
768 /*
769 * Multicast setup
770 */
771
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000772 if (num_addrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* I don't understand this: do we really need memory after the init? */
774 int len = ((char *) p->iscp - (char *) ptr - 8) / 6;
775 if (len <= 0) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000776 pr_err("%s: Ooooops, no memory for MC-Setup!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 } else {
778 if (len < num_addrs) {
779 num_addrs = len;
Alexander Beregalov39738e12009-05-26 12:35:26 +0000780 pr_warning("%s: Sorry, can only apply %d MC-Address(es).\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 dev->name, num_addrs);
782 }
783 mc_cmd = (struct mcsetup_cmd_struct *) ptr;
784 mc_cmd->cmd_status = 0;
785 mc_cmd->cmd_cmd = CMD_MCSETUP | CMD_LAST;
786 mc_cmd->cmd_link = 0xffff;
787 mc_cmd->mc_cnt = num_addrs * 6;
Jiri Pirko59ce25d2010-02-17 23:12:15 +0000788 i = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000789 netdev_for_each_mc_addr(ha, dev)
790 memcpy((char *) mc_cmd->mc_list[i++],
791 ha->addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 p->scb->cbl_offset = make16(mc_cmd);
793 p->scb->cmd = CUC_START;
794 elmc_id_attn586();
795 s = jiffies;
796 while (!(mc_cmd->cmd_status & STAT_COMPL)) {
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800797 if (time_after(jiffies, s + 30*HZ/100))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799 }
800 if (!(mc_cmd->cmd_status & STAT_COMPL)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000801 pr_warning("%s: Can't apply multicast-address-list.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 }
804 }
805 /*
806 * alloc xmit-buffs / init xmit_cmds
807 */
808 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
809 p->xmit_cbuffs[i] = (char *) ptr; /* char-buffs */
810 ptr = (char *) ptr + XMIT_BUFF_SIZE;
811 p->xmit_buffs[i] = (struct tbd_struct *) ptr; /* TBD */
812 ptr = (char *) ptr + sizeof(struct tbd_struct);
813 if ((void *) ptr > (void *) p->iscp) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000814 pr_err("%s: not enough shared-mem for your configuration!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 1;
816 }
817 memset((char *) (p->xmit_cmds[i]), 0, sizeof(struct transmit_cmd_struct));
818 memset((char *) (p->xmit_buffs[i]), 0, sizeof(struct tbd_struct));
819 p->xmit_cmds[i]->cmd_status = STAT_COMPL;
820 p->xmit_cmds[i]->cmd_cmd = CMD_XMIT | CMD_INT;
821 p->xmit_cmds[i]->tbd_offset = make16((p->xmit_buffs[i]));
822 p->xmit_buffs[i]->next = 0xffff;
823 p->xmit_buffs[i]->buffer = make24((p->xmit_cbuffs[i]));
824 }
825
826 p->xmit_count = 0;
827 p->xmit_last = 0;
828#ifndef NO_NOPCOMMANDS
829 p->nop_point = 0;
830#endif
831
832 /*
833 * 'start transmitter' (nop-loop)
834 */
835#ifndef NO_NOPCOMMANDS
836 p->scb->cbl_offset = make16(p->nop_cmds[0]);
837 p->scb->cmd = CUC_START;
838 elmc_id_attn586();
839 WAIT_4_SCB_CMD();
840#else
841 p->xmit_cmds[0]->cmd_link = 0xffff;
842 p->xmit_cmds[0]->cmd_cmd = CMD_XMIT | CMD_LAST | CMD_INT;
843#endif
844
845 return 0;
846}
847
848/******************************************************
849 * This is a helper routine for elmc_rnr_int() and init586().
850 * It sets up the Receive Frame Area (RFA).
851 */
852
853static void *alloc_rfa(struct net_device *dev, void *ptr)
854{
855 volatile struct rfd_struct *rfd = (struct rfd_struct *) ptr;
856 volatile struct rbd_struct *rbd;
857 int i;
Wang Chen454d7c92008-11-12 23:37:49 -0800858 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 memset((char *) rfd, 0, sizeof(struct rfd_struct) * p->num_recv_buffs);
861 p->rfd_first = rfd;
862
863 for (i = 0; i < p->num_recv_buffs; i++) {
864 rfd[i].next = make16(rfd + (i + 1) % p->num_recv_buffs);
865 }
866 rfd[p->num_recv_buffs - 1].last = RFD_SUSP; /* RU suspend */
867
868 ptr = (void *) (rfd + p->num_recv_buffs);
869
870 rbd = (struct rbd_struct *) ptr;
871 ptr = (void *) (rbd + p->num_recv_buffs);
872
873 /* clr descriptors */
874 memset((char *) rbd, 0, sizeof(struct rbd_struct) * p->num_recv_buffs);
875
876 for (i = 0; i < p->num_recv_buffs; i++) {
877 rbd[i].next = make16((rbd + (i + 1) % p->num_recv_buffs));
878 rbd[i].size = RECV_BUFF_SIZE;
879 rbd[i].buffer = make24(ptr);
880 ptr = (char *) ptr + RECV_BUFF_SIZE;
881 }
882
883 p->rfd_top = p->rfd_first;
884 p->rfd_last = p->rfd_first + p->num_recv_buffs - 1;
885
886 p->scb->rfa_offset = make16(p->rfd_first);
887 p->rfd_first->rbd_offset = make16(rbd);
888
889 return ptr;
890}
891
892
893/**************************************************
894 * Interrupt Handler ...
895 */
896
897static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100898elmc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400900 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned short stat;
902 struct priv *p;
903
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400904 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* The 3c523 has this habit of generating interrupts during the
906 reset. I'm not sure if the ni52 has this same problem, but it's
907 really annoying if we haven't finished initializing it. I was
908 hoping all the elmc_id_* commands would disable this, but I
909 might have missed a few. */
910
911 elmc_id_attn586(); /* ack inter. and disable any more */
912 return IRQ_HANDLED;
913 } else if (!(ELMC_CTRL_INT & inb(dev->base_addr + ELMC_CTRL))) {
914 /* wasn't this device */
915 return IRQ_NONE;
916 }
917 /* reading ELMC_CTRL also clears the INT bit. */
918
Wang Chen454d7c92008-11-12 23:37:49 -0800919 p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400921 while ((stat = p->scb->status & STAT_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 {
923 p->scb->cmd = stat;
924 elmc_attn586(); /* ack inter. */
925
926 if (stat & STAT_CX) {
927 /* command with I-bit set complete */
928 elmc_xmt_int(dev);
929 }
930 if (stat & STAT_FR) {
931 /* received a frame */
932 elmc_rcv_int(dev);
933 }
934#ifndef NO_NOPCOMMANDS
935 if (stat & STAT_CNA) {
936 /* CU went 'not ready' */
937 if (netif_running(dev)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000938 pr_warning("%s: oops! CU has left active state. stat: %04x/%04x.\n",
939 dev->name, (int) stat, (int) p->scb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 }
942#endif
943
944 if (stat & STAT_RNR) {
945 /* RU went 'not ready' */
946
947 if (p->scb->status & RU_SUSPEND) {
948 /* special case: RU_SUSPEND */
949
950 WAIT_4_SCB_CMD();
951 p->scb->cmd = RUC_RESUME;
952 elmc_attn586();
953 } else {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000954 pr_warning("%s: Receiver-Unit went 'NOT READY': %04x/%04x.\n",
955 dev->name, (int) stat, (int) p->scb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 elmc_rnr_int(dev);
957 }
958 }
959 WAIT_4_SCB_CMD(); /* wait for ack. (elmc_xmt_int can be faster than ack!!) */
960 if (p->scb->cmd) { /* timed out? */
961 break;
962 }
963 }
964 return IRQ_HANDLED;
965}
966
967/*******************************************************
968 * receive-interrupt
969 */
970
971static void elmc_rcv_int(struct net_device *dev)
972{
973 int status;
974 unsigned short totlen;
975 struct sk_buff *skb;
976 struct rbd_struct *rbd;
Wang Chen454d7c92008-11-12 23:37:49 -0800977 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 for (; (status = p->rfd_top->status) & STAT_COMPL;) {
980 rbd = (struct rbd_struct *) make32(p->rfd_top->rbd_offset);
981
982 if (status & STAT_OK) { /* frame received without error? */
983 if ((totlen = rbd->status) & RBD_LAST) { /* the first and the last buffer? */
984 totlen &= RBD_MASK; /* length of this frame */
985 rbd->status = 0;
986 skb = (struct sk_buff *) dev_alloc_skb(totlen + 2);
987 if (skb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 skb_reserve(skb, 2); /* 16 byte alignment */
989 skb_put(skb,totlen);
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700990 skb_copy_to_linear_data(skb, (char *) p->base+(unsigned long) rbd->buffer,totlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 skb->protocol = eth_type_trans(skb, dev);
992 netif_rx(skb);
Paulius Zaleckasaa509112008-04-29 11:58:42 +0300993 dev->stats.rx_packets++;
994 dev->stats.rx_bytes += totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 } else {
Paulius Zaleckasaa509112008-04-29 11:58:42 +0300996 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 } else {
Alexander Beregalov39738e12009-05-26 12:35:26 +0000999 pr_warning("%s: received oversized frame.\n", dev->name);
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001000 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 } else { /* frame !(ok), only with 'save-bad-frames' */
Alexander Beregalov39738e12009-05-26 12:35:26 +00001003 pr_warning("%s: oops! rfd-error-status: %04x\n", dev->name, status);
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001004 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006 p->rfd_top->status = 0;
1007 p->rfd_top->last = RFD_SUSP;
1008 p->rfd_last->last = 0; /* delete RU_SUSP */
1009 p->rfd_last = p->rfd_top;
1010 p->rfd_top = (struct rfd_struct *) make32(p->rfd_top->next); /* step to next RFD */
1011 }
1012}
1013
1014/**********************************************************
1015 * handle 'Receiver went not ready'.
1016 */
1017
1018static void elmc_rnr_int(struct net_device *dev)
1019{
Wang Chen454d7c92008-11-12 23:37:49 -08001020 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001022 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 WAIT_4_SCB_CMD(); /* wait for the last cmd */
1025 p->scb->cmd = RUC_ABORT; /* usually the RU is in the 'no resource'-state .. abort it now. */
1026 elmc_attn586();
1027 WAIT_4_SCB_CMD(); /* wait for accept cmd. */
1028
1029 alloc_rfa(dev, (char *) p->rfd_first);
1030 startrecv586(dev); /* restart RU */
1031
Alexander Beregalov39738e12009-05-26 12:35:26 +00001032 pr_warning("%s: Receive-Unit restarted. Status: %04x\n", dev->name, p->scb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034}
1035
1036/**********************************************************
1037 * handle xmit - interrupt
1038 */
1039
1040static void elmc_xmt_int(struct net_device *dev)
1041{
1042 int status;
Wang Chen454d7c92008-11-12 23:37:49 -08001043 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 status = p->xmit_cmds[p->xmit_last]->cmd_status;
1046 if (!(status & STAT_COMPL)) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001047 pr_warning("%s: strange .. xmit-int without a 'COMPLETE'\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 if (status & STAT_OK) {
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001050 dev->stats.tx_packets++;
1051 dev->stats.collisions += (status & TCMD_MAXCOLLMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 } else {
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001053 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (status & TCMD_LATECOLL) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001055 pr_warning("%s: late collision detected.\n", dev->name);
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001056 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 } else if (status & TCMD_NOCARRIER) {
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001058 dev->stats.tx_carrier_errors++;
Alexander Beregalov39738e12009-05-26 12:35:26 +00001059 pr_warning("%s: no carrier detected.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 } else if (status & TCMD_LOSTCTS) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001061 pr_warning("%s: loss of CTS detected.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 } else if (status & TCMD_UNDERRUN) {
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001063 dev->stats.tx_fifo_errors++;
Alexander Beregalov39738e12009-05-26 12:35:26 +00001064 pr_warning("%s: DMA underrun detected.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 } else if (status & TCMD_MAXCOLL) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001066 pr_warning("%s: Max. collisions exceeded.\n", dev->name);
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001067 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 }
1070
1071#if (NUM_XMIT_BUFFS != 1)
1072 if ((++p->xmit_last) == NUM_XMIT_BUFFS) {
1073 p->xmit_last = 0;
1074 }
1075#endif
1076
1077 netif_wake_queue(dev);
1078}
1079
1080/***********************************************************
1081 * (re)start the receiver
1082 */
1083
1084static void startrecv586(struct net_device *dev)
1085{
Wang Chen454d7c92008-11-12 23:37:49 -08001086 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 p->scb->rfa_offset = make16(p->rfd_first);
1089 p->scb->cmd = RUC_START;
1090 elmc_attn586(); /* start cmd. */
1091 WAIT_4_SCB_CMD(); /* wait for accept cmd. (no timeout!!) */
1092}
1093
1094/******************************************************
1095 * timeout
1096 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098static void elmc_timeout(struct net_device *dev)
1099{
Wang Chen454d7c92008-11-12 23:37:49 -08001100 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* COMMAND-UNIT active? */
1102 if (p->scb->status & CU_ACTIVE) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001103 pr_debug("%s: strange ... timeout with CU active?!?\n", dev->name);
1104 pr_debug("%s: X0: %04x N0: %04x N1: %04x %d\n", dev->name,
1105 (int)p->xmit_cmds[0]->cmd_status,
1106 (int)p->nop_cmds[0]->cmd_status,
1107 (int)p->nop_cmds[1]->cmd_status, (int)p->nop_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 p->scb->cmd = CUC_ABORT;
1109 elmc_attn586();
1110 WAIT_4_SCB_CMD();
1111 p->scb->cbl_offset = make16(p->nop_cmds[p->nop_point]);
1112 p->scb->cmd = CUC_START;
1113 elmc_attn586();
1114 WAIT_4_SCB_CMD();
1115 netif_wake_queue(dev);
1116 } else {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001117 pr_debug("%s: xmitter timed out, try to restart! stat: %04x\n",
1118 dev->name, p->scb->status);
1119 pr_debug("%s: command-stats: %04x %04x\n", dev->name,
1120 p->xmit_cmds[0]->cmd_status, p->xmit_cmds[1]->cmd_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 elmc_close(dev);
1122 elmc_open(dev);
1123 }
1124}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/******************************************************
1127 * send frame
1128 */
1129
Stephen Hemminger27a1de92009-08-31 19:50:54 +00001130static netdev_tx_t elmc_send_packet(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 int len;
1133 int i;
1134#ifndef NO_NOPCOMMANDS
1135 int next_nop;
1136#endif
Wang Chen454d7c92008-11-12 23:37:49 -08001137 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 netif_stop_queue(dev);
1140
1141 len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (len != skb->len)
1144 memset((char *) p->xmit_cbuffs[p->xmit_count], 0, ETH_ZLEN);
David S. Miller3dbad802007-03-29 19:16:03 -07001145 skb_copy_from_linear_data(skb, (char *) p->xmit_cbuffs[p->xmit_count], skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147#if (NUM_XMIT_BUFFS == 1)
1148#ifdef NO_NOPCOMMANDS
1149 p->xmit_buffs[0]->size = TBD_LAST | len;
1150 for (i = 0; i < 16; i++) {
1151 p->scb->cbl_offset = make16(p->xmit_cmds[0]);
1152 p->scb->cmd = CUC_START;
1153 p->xmit_cmds[0]->cmd_status = 0;
1154 elmc_attn586();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!i) {
1156 dev_kfree_skb(skb);
1157 }
1158 WAIT_4_SCB_CMD();
1159 if ((p->scb->status & CU_ACTIVE)) { /* test it, because CU sometimes doesn't start immediately */
1160 break;
1161 }
1162 if (p->xmit_cmds[0]->cmd_status) {
1163 break;
1164 }
1165 if (i == 15) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001166 pr_warning("%s: Can't start transmit-command.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 }
1169#else
1170 next_nop = (p->nop_point + 1) & 0x1;
1171 p->xmit_buffs[0]->size = TBD_LAST | len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 p->xmit_cmds[0]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1174 = make16((p->nop_cmds[next_nop]));
1175 p->xmit_cmds[0]->cmd_status = p->nop_cmds[next_nop]->cmd_status = 0;
1176
1177 p->nop_cmds[p->nop_point]->cmd_link = make16((p->xmit_cmds[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 p->nop_point = next_nop;
1179 dev_kfree_skb(skb);
1180#endif
1181#else
1182 p->xmit_buffs[p->xmit_count]->size = TBD_LAST | len;
1183 if ((next_nop = p->xmit_count + 1) == NUM_XMIT_BUFFS) {
1184 next_nop = 0;
1185 }
1186 p->xmit_cmds[p->xmit_count]->cmd_status = 0;
1187 p->xmit_cmds[p->xmit_count]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1188 = make16((p->nop_cmds[next_nop]));
1189 p->nop_cmds[next_nop]->cmd_status = 0;
1190 p->nop_cmds[p->xmit_count]->cmd_link = make16((p->xmit_cmds[p->xmit_count]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 p->xmit_count = next_nop;
1192 if (p->xmit_count != p->xmit_last)
1193 netif_wake_queue(dev);
1194 dev_kfree_skb(skb);
1195#endif
Patrick McHardy6ed10652009-06-23 06:03:08 +00001196 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199/*******************************************
1200 * Someone wanna have the statistics
1201 */
1202
1203static struct net_device_stats *elmc_get_stats(struct net_device *dev)
1204{
Wang Chen454d7c92008-11-12 23:37:49 -08001205 struct priv *p = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 unsigned short crc, aln, rsc, ovrn;
1207
1208 crc = p->scb->crc_errs; /* get error-statistic from the ni82586 */
1209 p->scb->crc_errs -= crc;
1210 aln = p->scb->aln_errs;
1211 p->scb->aln_errs -= aln;
1212 rsc = p->scb->rsc_errs;
1213 p->scb->rsc_errs -= rsc;
1214 ovrn = p->scb->ovrn_errs;
1215 p->scb->ovrn_errs -= ovrn;
1216
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001217 dev->stats.rx_crc_errors += crc;
1218 dev->stats.rx_fifo_errors += ovrn;
1219 dev->stats.rx_frame_errors += aln;
1220 dev->stats.rx_dropped += rsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Paulius Zaleckasaa509112008-04-29 11:58:42 +03001222 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225/********************************************************
1226 * Set MC list ..
1227 */
1228
1229#ifdef ELMC_MULTICAST
1230static void set_multicast_list(struct net_device *dev)
1231{
1232 if (!dev->start) {
1233 /* without a running interface, promiscuous doesn't work */
1234 return;
1235 }
1236 dev->start = 0;
1237 alloc586(dev);
1238 init586(dev);
1239 startrecv586(dev);
1240 dev->start = 1;
1241}
1242#endif
1243
1244static void netdev_get_drvinfo(struct net_device *dev,
1245 struct ethtool_drvinfo *info)
1246{
1247 strcpy(info->driver, DRV_NAME);
1248 strcpy(info->version, DRV_VERSION);
1249 sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1250}
1251
Jeff Garzik7282d492006-09-13 14:30:00 -04001252static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .get_drvinfo = netdev_get_drvinfo,
1254};
1255
1256#ifdef MODULE
1257
1258/* Increase if needed ;) */
1259#define MAX_3C523_CARDS 4
1260
1261static struct net_device *dev_elmc[MAX_3C523_CARDS];
1262static int irq[MAX_3C523_CARDS];
1263static int io[MAX_3C523_CARDS];
1264module_param_array(irq, int, NULL, 0);
1265module_param_array(io, int, NULL, 0);
1266MODULE_PARM_DESC(io, "EtherLink/MC I/O base address(es)");
1267MODULE_PARM_DESC(irq, "EtherLink/MC IRQ number(s)");
Randy Dunlap7b240172005-06-13 12:31:53 -07001268MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Randy Dunlap96e672c2006-06-10 13:33:48 -07001270int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 int this_dev,found = 0;
1273
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001274 /* Loop until we either can't find any more cards, or we have MAX_3C523_CARDS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 for(this_dev=0; this_dev<MAX_3C523_CARDS; this_dev++) {
1276 struct net_device *dev = alloc_etherdev(sizeof(struct priv));
1277 if (!dev)
1278 break;
1279 dev->irq=irq[this_dev];
1280 dev->base_addr=io[this_dev];
1281 if (do_elmc_probe(dev) == 0) {
b1fc5502005-05-12 20:11:55 -04001282 dev_elmc[this_dev] = dev;
1283 found++;
1284 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 free_netdev(dev);
1287 if (io[this_dev]==0)
1288 break;
Alexander Beregalov39738e12009-05-26 12:35:26 +00001289 pr_warning("3c523.c: No 3c523 card found at io=%#x\n",io[this_dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291
1292 if(found==0) {
Alexander Beregalov39738e12009-05-26 12:35:26 +00001293 if (io[0]==0)
1294 pr_notice("3c523.c: No 3c523 cards found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -ENXIO;
1296 } else return 0;
1297}
1298
Al Viroafc8eb42006-06-14 18:50:53 -04001299void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 int this_dev;
1302 for (this_dev=0; this_dev<MAX_3C523_CARDS; this_dev++) {
1303 struct net_device *dev = dev_elmc[this_dev];
1304 if (dev) {
1305 unregister_netdev(dev);
1306 cleanup_card(dev);
1307 free_netdev(dev);
1308 }
1309 }
1310}
1311
1312#endif /* MODULE */