make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / net / depca.c
1 /*  depca.c: A DIGITAL DEPCA  & EtherWORKS ethernet driver for linux.
2
3     Written 1994, 1995 by David C. Davies.
4
5
6                       Copyright 1994 David C. Davies
7                                    and 
8                          United States Government
9          (as represented by the Director, National Security Agency).  
10
11                Copyright 1995  Digital Equipment Corporation.
12
13
14     This software may be used and distributed according to the terms of
15     the GNU General Public License, incorporated herein by reference.
16
17     This driver is written for the Digital Equipment Corporation series
18     of DEPCA and EtherWORKS ethernet cards:
19
20         DEPCA       (the original)
21         DE100
22         DE101
23         DE200 Turbo
24         DE201 Turbo
25         DE202 Turbo (TP BNC)
26         DE210
27         DE422       (EISA)
28
29     The  driver has been tested on DE100, DE200 and DE202 cards  in  a
30     relatively busy network. The DE422 has been tested a little.
31
32     This  driver will NOT work   for the DE203,  DE204  and DE205 series  of
33     cards,  since they have  a  new custom ASIC in   place of the AMD  LANCE
34     chip.  See the 'ewrk3.c'   driver in the  Linux  source tree for running
35     those cards.
36
37     I have benchmarked the driver with a  DE100 at 595kB/s to (542kB/s from)
38     a DECstation 5000/200.
39
40     The author may be reached at davies@maniac.ultranet.com
41
42     =========================================================================
43
44     The  driver was originally based  on   the 'lance.c' driver from  Donald
45     Becker   which  is included with  the  standard  driver distribution for
46     linux.  V0.4  is  a complete  re-write  with only  the kernel  interface
47     remaining from the original code.
48
49     1) Lance.c code in /linux/drivers/net/
50     2) "Ethernet/IEEE 802.3 Family. 1992 World Network Data Book/Handbook",
51        AMD, 1992 [(800) 222-9323].
52     3) "Am79C90 CMOS Local Area Network Controller for Ethernet (C-LANCE)",
53        AMD, Pub. #17881, May 1993.
54     4) "Am79C960 PCnet-ISA(tm), Single-Chip Ethernet Controller for ISA",
55        AMD, Pub. #16907, May 1992
56     5) "DEC EtherWORKS LC Ethernet Controller Owners Manual",
57        Digital Equipment corporation, 1990, Pub. #EK-DE100-OM.003
58     6) "DEC EtherWORKS Turbo Ethernet Controller Owners Manual",
59        Digital Equipment corporation, 1990, Pub. #EK-DE200-OM.003
60     7) "DEPCA Hardware Reference Manual", Pub. #EK-DEPCA-PR
61        Digital Equipment Corporation, 1989
62     8) "DEC EtherWORKS Turbo_(TP BNC) Ethernet Controller Owners Manual",
63        Digital Equipment corporation, 1991, Pub. #EK-DE202-OM.001
64     
65
66     Peter Bauer's depca.c (V0.5) was referred to when debugging V0.1 of this
67     driver.
68
69     The original DEPCA  card requires that the  ethernet ROM address counter
70     be enabled to count and has an 8 bit NICSR.  The ROM counter enabling is
71     only  done when a  0x08 is read as the  first address octet (to minimise
72     the chances  of writing over some  other hardware's  I/O register).  The
73     NICSR accesses   have been changed  to  byte accesses  for all the cards
74     supported by this driver, since there is only one  useful bit in the MSB
75     (remote boot timeout) and it  is not used.  Also, there  is a maximum of
76     only 48kB network  RAM for this  card.  My thanks  to Torbjorn Lindh for
77     help debugging all this (and holding my feet to  the fire until I got it
78     right).
79
80     The DE200  series  boards have  on-board 64kB  RAM for  use  as a shared
81     memory network  buffer. Only the DE100  cards make use  of a  2kB buffer
82     mode which has not  been implemented in  this driver (only the 32kB  and
83     64kB modes are supported [16kB/48kB for the original DEPCA]).
84
85     At the most only 2 DEPCA cards can  be supported on  the ISA bus because
86     there is only provision  for two I/O base addresses  on each card (0x300
87     and 0x200). The I/O address is detected by searching for a byte sequence
88     in the Ethernet station address PROM at the expected I/O address for the
89     Ethernet  PROM.   The shared memory  base   address  is 'autoprobed'  by
90     looking  for the self  test PROM  and detecting the  card name.   When a
91     second  DEPCA is  detected,  information  is   placed in the   base_addr
92     variable of the  next device structure (which  is created if necessary),
93     thus  enabling ethif_probe  initialization  for the device.  More than 2
94     EISA cards can  be  supported, but  care will  be  needed assigning  the
95     shared memory to ensure that each slot has the  correct IRQ, I/O address
96     and shared memory address assigned.
97
98     ************************************************************************
99
100     NOTE: If you are using two  ISA DEPCAs, it is  important that you assign
101     the base memory addresses correctly.   The  driver autoprobes I/O  0x300
102     then 0x200.  The  base memory address for  the first device must be less
103     than that of the second so that the auto probe will correctly assign the
104     I/O and memory addresses on the same card.  I can't think of a way to do
105     this unambiguously at the moment, since there is nothing on the cards to
106     tie I/O and memory information together.
107
108     I am unable  to  test  2 cards   together for now,    so this  code   is
109     unchecked. All reports, good or bad, are welcome.
110
111     ************************************************************************
112
113     The board IRQ   setting must be  at an  unused IRQ which  is auto-probed
114     using Donald Becker's autoprobe routines. DEPCA and DE100 board IRQs are
115     {2,3,4,5,7}, whereas the  DE200 is at {5,9,10,11,15}.  Note that IRQ2 is
116     really IRQ9 in machines with 16 IRQ lines.
117
118     No 16MB memory  limitation should exist with this  driver as DMA is  not
119     used and the common memory area is in low memory on the network card (my
120     current system has 20MB and I've not had problems yet).
121
122     The ability to load this driver as a loadable module has been added. To
123     utilise this ability, you have to do <8 things:
124
125     0) have a copy of the loadable modules code installed on your system.
126     1) copy depca.c from the  /linux/drivers/net directory to your favourite
127     temporary directory.
128     2) if you wish, edit the  source code near  line 1530 to reflect the I/O
129     address and IRQ you're using (see also 5).
130     3) compile  depca.c, but include -DMODULE in  the command line to ensure
131     that the correct bits are compiled (see end of source code).
132     4) if you are wanting to add a new  card, goto 5. Otherwise, recompile a
133     kernel with the depca configuration turned off and reboot.
134     5) insmod depca.o [irq=7] [io=0x200] [mem=0xd0000] [adapter_name=DE100]
135        [Alan Cox: Changed the code to allow command line irq/io assignments]
136        [Dave Davies: Changed the code to allow command line mem/name
137                                                                 assignments]
138     6) run the net startup bits for your eth?? interface manually 
139     (usually /etc/rc.inet[12] at boot time). 
140     7) enjoy!
141
142     Note that autoprobing is not allowed in loadable modules - the system is
143     already up and running and you're messing with interrupts.
144
145     To unload a module, turn off the associated interface 
146     'ifconfig eth?? down' then 'rmmod depca'.
147
148     To assign a base memory address for the shared memory  when running as a
149     loadable module, see 5 above.  To include the adapter  name (if you have
150     no PROM  but know the card name)  also see 5  above. Note that this last
151     option  will not work  with kernel  built-in  depca's. 
152
153     The shared memory assignment for a loadable module  makes sense to avoid
154     the 'memory autoprobe' picking the wrong shared memory  (for the case of
155     2 depca's in a PC).
156
157     ************************************************************************
158     Support for MCA EtherWORKS cards added 11-3-98.
159     Verified to work with up to 2 DE212 cards in a system (although not
160       fully stress-tested).  
161
162     Currently known bugs/limitations:
163
164     Note:  with the MCA stuff as a module, it trusts the MCA configuration,
165            not the command line for IRQ and memory address.  You can
166            specify them if you want, but it will throw your values out.
167            You still have to pass the IO address it was configured as
168            though.
169
170     ************************************************************************
171     TO DO:
172     ------
173
174
175     Revision History
176     ----------------
177
178     Version   Date        Description
179   
180       0.1     25-jan-94   Initial writing.
181       0.2     27-jan-94   Added LANCE TX hardware buffer chaining.
182       0.3      1-feb-94   Added multiple DEPCA support.
183       0.31     4-feb-94   Added DE202 recognition.
184       0.32    19-feb-94   Tidy up. Improve multi-DEPCA support.
185       0.33    25-feb-94   Fix DEPCA ethernet ROM counter enable.
186                           Add jabber packet fix from murf@perftech.com
187                           and becker@super.org
188       0.34     7-mar-94   Fix DEPCA max network memory RAM & NICSR access.
189       0.35     8-mar-94   Added DE201 recognition. Tidied up.
190       0.351   30-apr-94   Added EISA support. Added DE422 recognition.
191       0.36    16-may-94   DE422 fix released.
192       0.37    22-jul-94   Added MODULE support
193       0.38    15-aug-94   Added DBR ROM switch in depca_close(). 
194                           Multi DEPCA bug fix.
195       0.38axp 15-sep-94   Special version for Alpha AXP Linux V1.0.
196       0.381   12-dec-94   Added DE101 recognition, fix multicast bug.
197       0.382    9-feb-95   Fix recognition bug reported by <bkm@star.rl.ac.uk>.
198       0.383   22-feb-95   Fix for conflict with VESA SCSI reported by
199                           <stromain@alf.dec.com>
200       0.384   17-mar-95   Fix a ring full bug reported by <bkm@star.rl.ac.uk>
201       0.385    3-apr-95   Fix a recognition bug reported by 
202                                                 <ryan.niemi@lastfrontier.com>
203       0.386   21-apr-95   Fix the last fix...sorry, must be galloping senility
204       0.40    25-May-95   Rewrite for portability & updated.
205                           ALPHA support from <jestabro@amt.tay1.dec.com>
206       0.41    26-Jun-95   Added verify_area() calls in depca_ioctl() from
207                           suggestion by <heiko@colossus.escape.de>
208       0.42    27-Dec-95   Add 'mem' shared memory assignment for loadable 
209                           modules.
210                           Add 'adapter_name' for loadable modules when no PROM.
211                           Both above from a suggestion by 
212                           <pchen@woodruffs121.residence.gatech.edu>.
213                           Add new multicasting code.
214       0.421   22-Apr-96   Fix alloc_device() bug <jari@markkus2.fimr.fi>
215       0.422   29-Apr-96   Fix depca_hw_init() bug <jari@markkus2.fimr.fi>
216       0.423    7-Jun-96   Fix module load bug <kmg@barco.be>
217       0.43    16-Aug-96   Update alloc_device() to conform to de4x5.c
218       0.44     1-Sep-97   Fix *_probe() to test check_region() first - bug
219                            reported by <mmogilvi@elbert.uccs.edu>
220       0.45     3-Nov-98   Added support for MCA EtherWORKS (DE210/DE212) cards
221                            by <tymm@computer.org> 
222       0.451    5-Nov-98   Fixed mca stuff cuz I'm a dummy. <tymm@computer.org>
223       0.5     14-Nov-98   Re-spin for 2.1.x kernels.
224       0.51    27-Jun-99   Correct received packet length for CRC from
225                            report by <worm@dkik.dk>
226       0.52    16-Oct-00   Fixes for 2.3 io memory accesses
227                           Fix show-stopper (ints left masked) in depca_interrupt
228                            by <peterd@pnd-pc.demon.co.uk>
229       0.53    12-Jan-01   Release resources on failure, bss tidbits
230                            by acme@conectiva.com.br
231       0.54    08-Nov-01   use library crc32 functions
232                            by Matt_Domsch@dell.com
233
234     =========================================================================
235 */
236
237 #include <linux/config.h>
238 #include <linux/module.h>
239
240 #include <linux/kernel.h>
241 #include <linux/sched.h>
242 #include <linux/string.h>
243 #include <linux/ptrace.h>
244 #include <linux/errno.h>
245 #include <linux/ioport.h>
246 #include <linux/slab.h>
247 #include <linux/interrupt.h>
248 #include <linux/delay.h>
249 #include <linux/init.h>
250 #include <linux/crc32.h>
251 #include <asm/uaccess.h>
252 #include <asm/bitops.h>
253 #include <asm/io.h>
254 #include <asm/dma.h>
255
256 #include <linux/netdevice.h>
257 #include <linux/etherdevice.h>
258 #include <linux/skbuff.h>
259
260 #include <linux/time.h>
261 #include <linux/types.h>
262 #include <linux/unistd.h>
263 #include <linux/ctype.h>
264
265 #ifdef CONFIG_MCA
266 #include <linux/mca.h>
267 #endif
268
269 #include "depca.h"
270
271 static char version[] __initdata = "depca.c:v0.53 2001/1/12 davies@maniac.ultranet.com\n";
272
273 #ifdef DEPCA_DEBUG
274 static int depca_debug = DEPCA_DEBUG;
275 #else
276 static int depca_debug = 1;
277 #endif
278
279 #define DEPCA_NDA 0xffe0        /* No Device Address */
280
281 #define TX_TIMEOUT (1*HZ)
282
283 /*
284 ** Ethernet PROM defines
285 */
286 #define PROBE_LENGTH    32
287 #define ETH_PROM_SIG    0xAA5500FFUL
288
289 /*
290 ** Set the number of Tx and Rx buffers. Ensure that the memory requested
291 ** here is <= to the amount of shared memory set up by the board switches.
292 ** The number of descriptors MUST BE A POWER OF 2.
293 **
294 ** total_memory = NUM_RX_DESC*(8+RX_BUFF_SZ) + NUM_TX_DESC*(8+TX_BUFF_SZ)
295 */
296 #define NUM_RX_DESC     8       /* Number of RX descriptors */
297 #define NUM_TX_DESC     8       /* Number of TX descriptors */
298 #define RX_BUFF_SZ      1536    /* Buffer size for each Rx buffer */
299 #define TX_BUFF_SZ      1536    /* Buffer size for each Tx buffer */
300
301 /*
302 ** EISA bus defines
303 */
304 #define DEPCA_EISA_IO_PORTS 0x0c00      /* I/O port base address, slot 0 */
305 #define MAX_EISA_SLOTS 16
306 #define EISA_SLOT_INC 0x1000
307
308 /*
309 ** ISA Bus defines
310 */
311 #define DEPCA_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0xe0000,0x00000}
312 #define DEPCA_IO_PORTS {0x300, 0x200, 0}
313 #define DEPCA_TOTAL_SIZE 0x10
314 static short mem_chkd;
315
316 /*
317 ** Adapter ID for the MCA EtherWORKS DE210/212 adapter
318 */
319 #define DE212_ID 0x6def
320
321 /*
322 ** Name <-> Adapter mapping
323 */
324 #define DEPCA_SIGNATURE {"DEPCA",\
325                          "DE100","DE101",\
326                          "DE200","DE201","DE202",\
327                          "DE210","DE212",\
328                          "DE422",\
329                          ""}
330 static enum {
331         DEPCA, de100, de101, de200, de201, de202, de210, de212, de422, unknown
332 } adapter;
333
334 /*
335 ** Miscellaneous info...
336 */
337 #define DEPCA_STRLEN 16
338 #define MAX_NUM_DEPCAS 2
339
340 /*
341 ** Memory Alignment. Each descriptor is 4 longwords long. To force a
342 ** particular alignment on the TX descriptor, adjust DESC_SKIP_LEN and
343 ** DESC_ALIGN. ALIGN aligns the start address of the private memory area
344 ** and hence the RX descriptor ring's first entry. 
345 */
346 #define ALIGN4      ((u_long)4 - 1)     /* 1 longword align */
347 #define ALIGN8      ((u_long)8 - 1)     /* 2 longword (quadword) align */
348 #define ALIGN         ALIGN8    /* Keep the LANCE happy... */
349
350 /*
351 ** The DEPCA Rx and Tx ring descriptors. 
352 */
353 struct depca_rx_desc {
354         volatile s32 base;
355         s16 buf_length;         /* This length is negative 2's complement! */
356         s16 msg_length;         /* This length is "normal". */
357 };
358
359 struct depca_tx_desc {
360         volatile s32 base;
361         s16 length;             /* This length is negative 2's complement! */
362         s16 misc;               /* Errors and TDR info */
363 };
364
365 #define LA_MASK 0x0000ffff      /* LANCE address mask for mapping network RAM
366                                    to LANCE memory address space */
367
368 /*
369 ** The Lance initialization block, described in databook, in common memory.
370 */
371 struct depca_init {
372         u16 mode;               /* Mode register */
373         u8 phys_addr[ETH_ALEN]; /* Physical ethernet address */
374         u8 mcast_table[8];      /* Multicast Hash Table. */
375         u32 rx_ring;            /* Rx ring base pointer & ring length */
376         u32 tx_ring;            /* Tx ring base pointer & ring length */
377 };
378
379 #define DEPCA_PKT_STAT_SZ 16
380 #define DEPCA_PKT_BIN_SZ  128   /* Should be >=100 unless you
381                                    increase DEPCA_PKT_STAT_SZ */
382 struct depca_private {
383         char devname[DEPCA_STRLEN];     /* Device Product String                  */
384         char adapter_name[DEPCA_STRLEN];        /* /proc/ioports string                  */
385         char adapter;           /* Adapter type                           */
386         char mca_slot;          /* MCA slot, if MCA else -1               */
387         struct depca_init init_block;   /* Shadow Initialization block            */
388 /* CPU address space fields */
389         struct depca_rx_desc *rx_ring;  /* Pointer to start of RX descriptor ring */
390         struct depca_tx_desc *tx_ring;  /* Pointer to start of TX descriptor ring */
391         void *rx_buff[NUM_RX_DESC];     /* CPU virt address of sh'd memory buffs  */
392         void *tx_buff[NUM_TX_DESC];     /* CPU virt address of sh'd memory buffs  */
393         void *sh_mem;           /* CPU mapped virt address of device RAM  */
394 /* Device address space fields */
395         u_long device_ram_start;        /* Start of RAM in device addr space      */
396 /* Offsets used in both address spaces */
397         u_long rx_ring_offset;  /* Offset from start of RAM to rx_ring    */
398         u_long tx_ring_offset;  /* Offset from start of RAM to tx_ring    */
399         u_long buffs_offset;    /* LANCE Rx and Tx buffers start address. */
400 /* Kernel-only (not device) fields */
401         int rx_new, tx_new;     /* The next free ring entry               */
402         int rx_old, tx_old;     /* The ring entries to be free()ed.       */
403         struct net_device_stats stats;
404         spinlock_t lock;
405         struct {                /* Private stats counters                 */
406                 u32 bins[DEPCA_PKT_STAT_SZ];
407                 u32 unicast;
408                 u32 multicast;
409                 u32 broadcast;
410                 u32 excessive_collisions;
411                 u32 tx_underruns;
412                 u32 excessive_underruns;
413         } pktStats;
414         int txRingMask;         /* TX ring mask                           */
415         int rxRingMask;         /* RX ring mask                           */
416         s32 rx_rlen;            /* log2(rxRingMask+1) for the descriptors */
417         s32 tx_rlen;            /* log2(txRingMask+1) for the descriptors */
418 };
419
420 /*
421 ** The transmit ring full condition is described by the tx_old and tx_new
422 ** pointers by:
423 **    tx_old            = tx_new    Empty ring
424 **    tx_old            = tx_new+1  Full ring
425 **    tx_old+txRingMask = tx_new    Full ring  (wrapped condition)
426 */
427 #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
428                          lp->tx_old+lp->txRingMask-lp->tx_new:\
429                          lp->tx_old               -lp->tx_new-1)
430
431 /*
432 ** Public Functions
433 */
434 static int depca_open(struct net_device *dev);
435 static int depca_start_xmit(struct sk_buff *skb, struct net_device *dev);
436 static void depca_interrupt(int irq, void *dev_id, struct pt_regs *regs);
437 static int depca_close(struct net_device *dev);
438 static int depca_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
439 static void depca_tx_timeout(struct net_device *dev);
440 static struct net_device_stats *depca_get_stats(struct net_device *dev);
441 static void set_multicast_list(struct net_device *dev);
442
443 /*
444 ** Private functions
445 */
446 static int depca_hw_init(struct net_device *dev, u_long ioaddr, int mca_slot);
447 static void depca_init_ring(struct net_device *dev);
448 static int depca_rx(struct net_device *dev);
449 static int depca_tx(struct net_device *dev);
450
451 static void LoadCSRs(struct net_device *dev);
452 static int InitRestartDepca(struct net_device *dev);
453 static void DepcaSignature(char *name, u_long paddr);
454 static int DevicePresent(u_long ioaddr);
455 static int get_hw_addr(struct net_device *dev);
456 static int EISA_signature(char *name, s32 eisa_id);
457 static void SetMulticastFilter(struct net_device *dev);
458 static void isa_probe(struct net_device *dev, u_long iobase);
459 static void eisa_probe(struct net_device *dev, u_long iobase);
460 #ifdef CONFIG_MCA
461 static void mca_probe(struct net_device *dev, u_long iobase);
462 #endif
463 static struct net_device *alloc_device(struct net_device *dev, u_long iobase);
464 static int depca_dev_index(char *s);
465 static struct net_device *insert_device(struct net_device *dev, u_long iobase, int (*init) (struct net_device *));
466 static int load_packet(struct net_device *dev, struct sk_buff *skb);
467 static void depca_dbg_open(struct net_device *dev);
468
469 #ifdef MODULE
470 int init_module(void);
471 void cleanup_module(void);
472 static int autoprobed = 1, loading_module = 1;
473 # else
474 static u_char de1xx_irq[] __initdata = { 2, 3, 4, 5, 7, 9, 0 };
475 static u_char de2xx_irq[] __initdata = { 5, 9, 10, 11, 15, 0 };
476 static u_char de422_irq[] __initdata = { 5, 9, 10, 11, 0 };
477 static u_char *depca_irq;
478 static int autoprobed, loading_module;
479 #endif                          /* MODULE */
480
481 static char name[DEPCA_STRLEN];
482 static int num_depcas, num_eth;
483 static int mem;                 /* For loadable module assignment
484                                    use insmod mem=0x????? .... */
485 static char *adapter_name;      /* = '\0';     If no PROM when loadable module
486                                    use insmod adapter_name=DE??? ...
487                                    bss initializes this to zero
488                                  */
489 /*
490 ** Miscellaneous defines...
491 */
492 #define STOP_DEPCA \
493     outw(CSR0, DEPCA_ADDR);\
494     outw(STOP, DEPCA_DATA)
495
496 int __init depca_probe(struct net_device *dev)
497 {
498         int tmp = num_depcas, status = -ENODEV;
499         u_long iobase = dev->base_addr;
500
501         SET_MODULE_OWNER(dev);
502
503         if ((iobase == 0) && loading_module) {
504                 printk("Autoprobing is not supported when loading a module based driver.\n");
505                 status = -EIO;
506         } else {
507 #ifdef CONFIG_MCA
508                 mca_probe(dev, iobase);
509 #endif
510                 isa_probe(dev, iobase);
511                 eisa_probe(dev, iobase);
512
513                 if ((tmp == num_depcas) && (iobase != 0) && loading_module) {
514                         printk("%s: depca_probe() cannot find device at 0x%04lx.\n", dev->name, iobase);
515                 }
516
517                 /*
518                    ** Walk the device list to check that at least one device
519                    ** initialised OK
520                  */
521                 for (; (dev->priv == NULL) && (dev->next != NULL); dev = dev->next);
522
523                 if (dev->priv)
524                         status = 0;
525                 if (iobase == 0)
526                         autoprobed = 1;
527         }
528
529         return status;
530 }
531
532 static int __init depca_hw_init(struct net_device *dev, u_long ioaddr, int mca_slot)
533 {
534         struct depca_private *lp;
535         int i, j, offset, netRAM, mem_len, status = 0;
536         s16 nicsr;
537         u_long mem_start = 0, mem_base[] = DEPCA_RAM_BASE_ADDRESSES;
538
539         STOP_DEPCA;
540
541         nicsr = inb(DEPCA_NICSR);
542         nicsr = ((nicsr & ~SHE & ~RBE & ~IEN) | IM);
543         outb(nicsr, DEPCA_NICSR);
544
545         if (inw(DEPCA_DATA) != STOP) {
546                 return -ENXIO;
547         }
548
549         do {
550                 strcpy(name, (adapter_name ? adapter_name : ""));
551                 mem_start = (mem ? mem & 0xf0000 : mem_base[mem_chkd++]);
552                 DepcaSignature(name, mem_start);
553         } while (!mem && mem_base[mem_chkd] && (adapter == unknown));
554
555         if ((adapter == unknown) || !mem_start) {       /* DEPCA device not found */
556                 return -ENXIO;
557         }
558
559         dev->base_addr = ioaddr;
560
561         if (mca_slot != -1) {
562                 printk("%s: %s at 0x%04lx (MCA slot %d)", dev->name, name, ioaddr, mca_slot);
563         } else if ((ioaddr & 0x0fff) == DEPCA_EISA_IO_PORTS) {  /* EISA slot address */
564                 printk("%s: %s at 0x%04lx (EISA slot %d)", dev->name, name, ioaddr, (int) ((ioaddr >> 12) & 0x0f));
565         } else {                /* ISA port address */
566                 printk("%s: %s at 0x%04lx", dev->name, name, ioaddr);
567         }
568
569         printk(", h/w address ");
570         status = get_hw_addr(dev);
571         if (status != 0) {
572                 printk("      which has an Ethernet PROM CRC error.\n");
573                 return -ENXIO;
574         }
575         for (i = 0; i < ETH_ALEN - 1; i++) {    /* get the ethernet address */
576                 printk("%2.2x:", dev->dev_addr[i]);
577         }
578         printk("%2.2x", dev->dev_addr[i]);
579
580         /* Set up the maximum amount of network RAM(kB) */
581         netRAM = ((adapter != DEPCA) ? 64 : 48);
582         if ((nicsr & _128KB) && (adapter == de422))
583                 netRAM = 128;
584         offset = 0x0000;
585
586         /* Shared Memory Base Address */
587         if (nicsr & BUF) {
588                 offset = 0x8000;        /* 32kbyte RAM offset */
589                 nicsr &= ~BS;   /* DEPCA RAM in top 32k */
590                 netRAM -= 32;
591         }
592         mem_start += offset;    /* (E)ISA start address */
593         if ((mem_len = (NUM_RX_DESC * (sizeof(struct depca_rx_desc) + RX_BUFF_SZ) + NUM_TX_DESC * (sizeof(struct depca_tx_desc) + TX_BUFF_SZ) + sizeof(struct depca_init)))
594             > (netRAM << 10)) {
595                 printk(",\n       requests %dkB RAM: only %dkB is available!\n", (mem_len >> 10), netRAM);
596                 return -ENXIO;
597         }
598
599         printk(",\n      has %dkB RAM at 0x%.5lx", netRAM, mem_start);
600
601         /* Enable the shadow RAM. */
602         if (adapter != DEPCA) {
603                 nicsr |= SHE;
604                 outb(nicsr, DEPCA_NICSR);
605         }
606
607         /* Define the device private memory */
608         dev->priv = (void *) kmalloc(sizeof(struct depca_private), GFP_KERNEL);
609         if (dev->priv == NULL)
610                 return -ENOMEM;
611         lp = (struct depca_private *) dev->priv;
612         memset((char *) dev->priv, 0, sizeof(struct depca_private));
613         lp->adapter = adapter;
614         lp->mca_slot = mca_slot;
615         lp->lock = SPIN_LOCK_UNLOCKED;
616         sprintf(lp->adapter_name, "%s (%s)", name, dev->name);
617         status = -EBUSY;
618         if (!request_region(ioaddr, DEPCA_TOTAL_SIZE, lp->adapter_name)) {
619                 printk(KERN_ERR "depca: I/O resource 0x%x @ 0x%lx busy\n", DEPCA_TOTAL_SIZE, ioaddr);
620                 goto out_priv;
621         }
622
623         /* Initialisation Block */
624         lp->sh_mem = ioremap(mem_start, mem_len);
625         status = -EIO;
626         if (lp->sh_mem == NULL) {
627                 printk(KERN_ERR "depca: cannot remap ISA memory, aborting\n");
628                 goto out_region;
629         }
630         lp->device_ram_start = mem_start & LA_MASK;
631
632         offset = 0;
633         offset += sizeof(struct depca_init);
634
635         /* Tx & Rx descriptors (aligned to a quadword boundary) */
636         offset = (offset + ALIGN) & ~ALIGN;
637         lp->rx_ring = (struct depca_rx_desc *) (lp->sh_mem + offset);
638         lp->rx_ring_offset = offset;
639
640         offset += (sizeof(struct depca_rx_desc) * NUM_RX_DESC);
641         lp->tx_ring = (struct depca_tx_desc *) (lp->sh_mem + offset);
642         lp->tx_ring_offset = offset;
643
644         offset += (sizeof(struct depca_tx_desc) * NUM_TX_DESC);
645
646         lp->buffs_offset = offset;
647
648         /* Finish initialising the ring information. */
649         lp->rxRingMask = NUM_RX_DESC - 1;
650         lp->txRingMask = NUM_TX_DESC - 1;
651
652         /* Calculate Tx/Rx RLEN size for the descriptors. */
653         for (i = 0, j = lp->rxRingMask; j > 0; i++) {
654                 j >>= 1;
655         }
656         lp->rx_rlen = (s32) (i << 29);
657         for (i = 0, j = lp->txRingMask; j > 0; i++) {
658                 j >>= 1;
659         }
660         lp->tx_rlen = (s32) (i << 29);
661
662         /* Load the initialisation block */
663         depca_init_ring(dev);
664
665         /* Initialise the control and status registers */
666         LoadCSRs(dev);
667
668         /* Enable DEPCA board interrupts for autoprobing */
669         nicsr = ((nicsr & ~IM) | IEN);
670         outb(nicsr, DEPCA_NICSR);
671
672         /* To auto-IRQ we enable the initialization-done and DMA err,
673            interrupts. For now we will always get a DMA error. */
674         if (dev->irq < 2) {
675 #ifndef MODULE
676                 unsigned char irqnum;
677                 autoirq_setup(0);
678
679                 /* Assign the correct irq list */
680                 switch (lp->adapter) {
681                 case DEPCA:
682                 case de100:
683                 case de101:
684                         depca_irq = de1xx_irq;
685                         break;
686                 case de200:
687                 case de201:
688                 case de202:
689                 case de210:
690                 case de212:
691                         depca_irq = de2xx_irq;
692                         break;
693                 case de422:
694                         depca_irq = de422_irq;
695                         break;
696                 }
697
698                 /* Trigger an initialization just for the interrupt. */
699                 outw(INEA | INIT, DEPCA_DATA);
700
701                 irqnum = autoirq_report(1);
702                 status = -ENXIO;
703                 if (!irqnum) {
704                         printk(" and failed to detect IRQ line.\n");
705                         goto out_region;
706                 } else {
707                         for (dev->irq = 0, i = 0; (depca_irq[i]) && (!dev->irq); i++)
708                                 if (irqnum == depca_irq[i]) {
709                                         dev->irq = irqnum;
710                                         printk(" and uses IRQ%d.\n", dev->irq);
711                                 }
712
713                         status = -ENXIO;
714                         if (!dev->irq) {
715                                 printk(" but incorrect IRQ line detected.\n");
716                                 goto out_region;
717                         }
718                 }
719 #endif                          /* MODULE */
720         } else {
721                 printk(" and assigned IRQ%d.\n", dev->irq);
722         }
723
724         if (depca_debug > 1) {
725                 printk(version);
726         }
727
728         /* The DEPCA-specific entries in the device structure. */
729         dev->open = &depca_open;
730         dev->hard_start_xmit = &depca_start_xmit;
731         dev->stop = &depca_close;
732         dev->get_stats = &depca_get_stats;
733         dev->set_multicast_list = &set_multicast_list;
734         dev->do_ioctl = &depca_ioctl;
735         dev->tx_timeout = depca_tx_timeout;
736         dev->watchdog_timeo = TX_TIMEOUT;
737
738         dev->mem_start = 0;
739
740         /* Fill in the generic field of the device structure. */
741         ether_setup(dev);
742         return 0;
743       out_region:
744         release_region(ioaddr, DEPCA_TOTAL_SIZE);
745       out_priv:
746         kfree(dev->priv);
747         dev->priv = NULL;
748         return status;
749 }
750 \f
751
752 static int depca_open(struct net_device *dev)
753 {
754         struct depca_private *lp = (struct depca_private *) dev->priv;
755         u_long ioaddr = dev->base_addr;
756         s16 nicsr;
757         int status = 0;
758
759         STOP_DEPCA;
760         nicsr = inb(DEPCA_NICSR);
761
762         /* Make sure the shadow RAM is enabled */
763         if (lp->adapter != DEPCA) {
764                 nicsr |= SHE;
765                 outb(nicsr, DEPCA_NICSR);
766         }
767
768         /* Re-initialize the DEPCA... */
769         depca_init_ring(dev);
770         LoadCSRs(dev);
771
772         depca_dbg_open(dev);
773
774         if (request_irq(dev->irq, &depca_interrupt, 0, lp->adapter_name, dev)) {
775                 printk("depca_open(): Requested IRQ%d is busy\n", dev->irq);
776                 status = -EAGAIN;
777         } else {
778
779                 /* Enable DEPCA board interrupts and turn off LED */
780                 nicsr = ((nicsr & ~IM & ~LED) | IEN);
781                 outb(nicsr, DEPCA_NICSR);
782                 outw(CSR0, DEPCA_ADDR);
783
784                 netif_start_queue(dev);
785
786                 status = InitRestartDepca(dev);
787
788                 if (depca_debug > 1) {
789                         printk("CSR0: 0x%4.4x\n", inw(DEPCA_DATA));
790                         printk("nicsr: 0x%02x\n", inb(DEPCA_NICSR));
791                 }
792         }
793         return status;
794 }
795
796 /* Initialize the lance Rx and Tx descriptor rings. */
797 static void depca_init_ring(struct net_device *dev)
798 {
799         struct depca_private *lp = (struct depca_private *) dev->priv;
800         u_int i;
801         u_long offset;
802
803         /* Lock out other processes whilst setting up the hardware */
804         netif_stop_queue(dev);
805
806         lp->rx_new = lp->tx_new = 0;
807         lp->rx_old = lp->tx_old = 0;
808
809         /* Initialize the base address and length of each buffer in the ring */
810         for (i = 0; i <= lp->rxRingMask; i++) {
811                 offset = lp->buffs_offset + i * RX_BUFF_SZ;
812                 writel((lp->device_ram_start + offset) | R_OWN, &lp->rx_ring[i].base);
813                 writew(-RX_BUFF_SZ, &lp->rx_ring[i].buf_length);
814                 lp->rx_buff[i] = lp->sh_mem + offset;
815         }
816
817         for (i = 0; i <= lp->txRingMask; i++) {
818                 offset = lp->buffs_offset + (i + lp->rxRingMask + 1) * TX_BUFF_SZ;
819                 writel((lp->device_ram_start + offset) & 0x00ffffff, &lp->tx_ring[i].base);
820                 lp->tx_buff[i] = lp->sh_mem + offset;
821         }
822
823         /* Set up the initialization block */
824         lp->init_block.rx_ring = (lp->device_ram_start + lp->rx_ring_offset) | lp->rx_rlen;
825         lp->init_block.tx_ring = (lp->device_ram_start + lp->tx_ring_offset) | lp->tx_rlen;
826
827         SetMulticastFilter(dev);
828
829         for (i = 0; i < ETH_ALEN; i++) {
830                 lp->init_block.phys_addr[i] = dev->dev_addr[i];
831         }
832
833         lp->init_block.mode = 0x0000;   /* Enable the Tx and Rx */
834 }
835
836
837 static void depca_tx_timeout(struct net_device *dev)
838 {
839         u_long ioaddr = dev->base_addr;
840
841         printk("%s: transmit timed out, status %04x, resetting.\n", dev->name, inw(DEPCA_DATA));
842
843         STOP_DEPCA;
844         depca_init_ring(dev);
845         LoadCSRs(dev);
846         dev->trans_start = jiffies;
847         netif_wake_queue(dev);
848         InitRestartDepca(dev);
849 }
850
851
852 /* 
853 ** Writes a socket buffer to TX descriptor ring and starts transmission 
854 */
855 static int depca_start_xmit(struct sk_buff *skb, struct net_device *dev)
856 {
857         struct depca_private *lp = (struct depca_private *) dev->priv;
858         u_long ioaddr = dev->base_addr;
859         int status = 0;
860
861         /* Transmitter timeout, serious problems. */
862         if (skb->len < 1)
863                 goto out;
864
865         netif_stop_queue(dev);
866
867         if (TX_BUFFS_AVAIL) {   /* Fill in a Tx ring entry */
868                 status = load_packet(dev, skb);
869
870                 if (!status) {
871                         /* Trigger an immediate send demand. */
872                         outw(CSR0, DEPCA_ADDR);
873                         outw(INEA | TDMD, DEPCA_DATA);
874
875                         dev->trans_start = jiffies;
876                         dev_kfree_skb(skb);
877                 }
878                 if (TX_BUFFS_AVAIL)
879                         netif_start_queue(dev);
880         } else
881                 status = -1;
882
883       out:
884         return status;
885 }
886
887 /*
888 ** The DEPCA interrupt handler. 
889 */
890 static void depca_interrupt(int irq, void *dev_id, struct pt_regs *regs)
891 {
892         struct net_device *dev = dev_id;
893         struct depca_private *lp;
894         s16 csr0, nicsr;
895         u_long ioaddr;
896
897         if (dev == NULL) {
898                 printk("depca_interrupt(): irq %d for unknown device.\n", irq);
899                 return;
900         }
901
902         lp = (struct depca_private *) dev->priv;
903         ioaddr = dev->base_addr;
904
905         spin_lock(&lp->lock);
906
907         /* mask the DEPCA board interrupts and turn on the LED */
908         nicsr = inb(DEPCA_NICSR);
909         nicsr |= (IM | LED);
910         outb(nicsr, DEPCA_NICSR);
911
912         outw(CSR0, DEPCA_ADDR);
913         csr0 = inw(DEPCA_DATA);
914
915         /* Acknowledge all of the current interrupt sources ASAP. */
916         outw(csr0 & INTE, DEPCA_DATA);
917
918         if (csr0 & RINT)        /* Rx interrupt (packet arrived) */
919                 depca_rx(dev);
920
921         if (csr0 & TINT)        /* Tx interrupt (packet sent) */
922                 depca_tx(dev);
923
924         /* Any resources available? */
925         if ((TX_BUFFS_AVAIL >= 0) && netif_queue_stopped(dev)) {
926                 netif_wake_queue(dev);
927         }
928
929         /* Unmask the DEPCA board interrupts and turn off the LED */
930         nicsr = (nicsr & ~IM & ~LED);
931         outb(nicsr, DEPCA_NICSR);
932
933         spin_unlock(&lp->lock);
934 }
935
936
937 static int depca_rx(struct net_device *dev)
938 {
939         struct depca_private *lp = (struct depca_private *) dev->priv;
940         int i, entry;
941         s32 status;
942
943         for (entry = lp->rx_new; !(readl(&lp->rx_ring[entry].base) & R_OWN); entry = lp->rx_new) {
944                 status = readl(&lp->rx_ring[entry].base) >> 16;
945                 if (status & R_STP) {   /* Remember start of frame */
946                         lp->rx_old = entry;
947                 }
948                 if (status & R_ENP) {   /* Valid frame status */
949                         if (status & R_ERR) {   /* There was an error. */
950                                 lp->stats.rx_errors++;  /* Update the error stats. */
951                                 if (status & R_FRAM)
952                                         lp->stats.rx_frame_errors++;
953                                 if (status & R_OFLO)
954                                         lp->stats.rx_over_errors++;
955                                 if (status & R_CRC)
956                                         lp->stats.rx_crc_errors++;
957                                 if (status & R_BUFF)
958                                         lp->stats.rx_fifo_errors++;
959                         } else {
960                                 short len, pkt_len = readw(&lp->rx_ring[entry].msg_length) - 4;
961                                 struct sk_buff *skb;
962
963                                 skb = dev_alloc_skb(pkt_len + 2);
964                                 if (skb != NULL) {
965                                         unsigned char *buf;
966                                         skb_reserve(skb, 2);    /* 16 byte align the IP header */
967                                         buf = skb_put(skb, pkt_len);
968                                         skb->dev = dev;
969                                         if (entry < lp->rx_old) {       /* Wrapped buffer */
970                                                 len = (lp->rxRingMask - lp->rx_old + 1) * RX_BUFF_SZ;
971                                                 memcpy_fromio(buf, lp->rx_buff[lp->rx_old], len);
972                                                 memcpy_fromio(buf + len, lp->rx_buff[0], pkt_len - len);
973                                         } else {        /* Linear buffer */
974                                                 memcpy_fromio(buf, lp->rx_buff[lp->rx_old], pkt_len);
975                                         }
976
977                                         /* 
978                                            ** Notify the upper protocol layers that there is another 
979                                            ** packet to handle
980                                          */
981                                         skb->protocol = eth_type_trans(skb, dev);
982                                         netif_rx(skb);
983
984                                         /*
985                                            ** Update stats
986                                          */
987                                         dev->last_rx = jiffies;
988                                         lp->stats.rx_packets++;
989                                         lp->stats.rx_bytes += pkt_len;
990                                         for (i = 1; i < DEPCA_PKT_STAT_SZ - 1; i++) {
991                                                 if (pkt_len < (i * DEPCA_PKT_BIN_SZ)) {
992                                                         lp->pktStats.bins[i]++;
993                                                         i = DEPCA_PKT_STAT_SZ;
994                                                 }
995                                         }
996                                         if (buf[0] & 0x01) {    /* Multicast/Broadcast */
997                                                 if ((*(s16 *) & buf[0] == -1) && (*(s16 *) & buf[2] == -1) && (*(s16 *) & buf[4] == -1)) {
998                                                         lp->pktStats.broadcast++;
999                                                 } else {
1000                                                         lp->pktStats.multicast++;
1001                                                 }
1002                                         } else if ((*(s16 *) & buf[0] == *(s16 *) & dev->dev_addr[0]) && (*(s16 *) & buf[2] == *(s16 *) & dev->dev_addr[2]) && (*(s16 *) & buf[4] == *(s16 *) & dev->dev_addr[4])) {
1003                                                 lp->pktStats.unicast++;
1004                                         }
1005
1006                                         lp->pktStats.bins[0]++; /* Duplicates stats.rx_packets */
1007                                         if (lp->pktStats.bins[0] == 0) {        /* Reset counters */
1008                                                 memset((char *) &lp->pktStats, 0, sizeof(lp->pktStats));
1009                                         }
1010                                 } else {
1011                                         printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1012                                         lp->stats.rx_dropped++; /* Really, deferred. */
1013                                         break;
1014                                 }
1015                         }
1016                         /* Change buffer ownership for this last frame, back to the adapter */
1017                         for (; lp->rx_old != entry; lp->rx_old = (++lp->rx_old) & lp->rxRingMask) {
1018                                 writel(readl(&lp->rx_ring[lp->rx_old].base) | R_OWN, &lp->rx_ring[lp->rx_old].base);
1019                         }
1020                         writel(readl(&lp->rx_ring[entry].base) | R_OWN, &lp->rx_ring[entry].base);
1021                 }
1022
1023                 /*
1024                    ** Update entry information
1025                  */
1026                 lp->rx_new = (++lp->rx_new) & lp->rxRingMask;
1027         }
1028
1029         return 0;
1030 }
1031
1032 /*
1033 ** Buffer sent - check for buffer errors.
1034 */
1035 static int depca_tx(struct net_device *dev)
1036 {
1037         struct depca_private *lp = (struct depca_private *) dev->priv;
1038         int entry;
1039         s32 status;
1040         u_long ioaddr = dev->base_addr;
1041
1042         for (entry = lp->tx_old; entry != lp->tx_new; entry = lp->tx_old) {
1043                 status = readl(&lp->tx_ring[entry].base) >> 16;
1044
1045                 if (status < 0) {       /* Packet not yet sent! */
1046                         break;
1047                 } else if (status & T_ERR) {    /* An error occurred. */
1048                         status = readl(&lp->tx_ring[entry].misc);
1049                         lp->stats.tx_errors++;
1050                         if (status & TMD3_RTRY)
1051                                 lp->stats.tx_aborted_errors++;
1052                         if (status & TMD3_LCAR)
1053                                 lp->stats.tx_carrier_errors++;
1054                         if (status & TMD3_LCOL)
1055                                 lp->stats.tx_window_errors++;
1056                         if (status & TMD3_UFLO)
1057                                 lp->stats.tx_fifo_errors++;
1058                         if (status & (TMD3_BUFF | TMD3_UFLO)) {
1059                                 /* Trigger an immediate send demand. */
1060                                 outw(CSR0, DEPCA_ADDR);
1061                                 outw(INEA | TDMD, DEPCA_DATA);
1062                         }
1063                 } else if (status & (T_MORE | T_ONE)) {
1064                         lp->stats.collisions++;
1065                 } else {
1066                         lp->stats.tx_packets++;
1067                 }
1068
1069                 /* Update all the pointers */
1070                 lp->tx_old = (++lp->tx_old) & lp->txRingMask;
1071         }
1072
1073         return 0;
1074 }
1075
1076 static int depca_close(struct net_device *dev)
1077 {
1078         struct depca_private *lp = (struct depca_private *) dev->priv;
1079         s16 nicsr;
1080         u_long ioaddr = dev->base_addr;
1081
1082         netif_stop_queue(dev);
1083
1084         outw(CSR0, DEPCA_ADDR);
1085
1086         if (depca_debug > 1) {
1087                 printk("%s: Shutting down ethercard, status was %2.2x.\n", dev->name, inw(DEPCA_DATA));
1088         }
1089
1090         /* 
1091            ** We stop the DEPCA here -- it occasionally polls
1092            ** memory if we don't. 
1093          */
1094         outw(STOP, DEPCA_DATA);
1095
1096         /*
1097            ** Give back the ROM in case the user wants to go to DOS
1098          */
1099         if (lp->adapter != DEPCA) {
1100                 nicsr = inb(DEPCA_NICSR);
1101                 nicsr &= ~SHE;
1102                 outb(nicsr, DEPCA_NICSR);
1103         }
1104
1105         /*
1106            ** Free the associated irq
1107          */
1108         free_irq(dev->irq, dev);
1109         return 0;
1110 }
1111
1112 static void LoadCSRs(struct net_device *dev)
1113 {
1114         struct depca_private *lp = (struct depca_private *) dev->priv;
1115         u_long ioaddr = dev->base_addr;
1116
1117         outw(CSR1, DEPCA_ADDR); /* initialisation block address LSW */
1118         outw((u16) lp->device_ram_start, DEPCA_DATA);
1119         outw(CSR2, DEPCA_ADDR); /* initialisation block address MSW */
1120         outw((u16) (lp->device_ram_start >> 16), DEPCA_DATA);
1121         outw(CSR3, DEPCA_ADDR); /* ALE control */
1122         outw(ACON, DEPCA_DATA);
1123
1124         outw(CSR0, DEPCA_ADDR); /* Point back to CSR0 */
1125
1126         return;
1127 }
1128
1129 static int InitRestartDepca(struct net_device *dev)
1130 {
1131         struct depca_private *lp = (struct depca_private *) dev->priv;
1132         u_long ioaddr = dev->base_addr;
1133         int i, status = 0;
1134
1135         /* Copy the shadow init_block to shared memory */
1136         memcpy_toio(lp->sh_mem, &lp->init_block, sizeof(struct depca_init));
1137
1138         outw(CSR0, DEPCA_ADDR); /* point back to CSR0 */
1139         outw(INIT, DEPCA_DATA); /* initialize DEPCA */
1140
1141         /* wait for lance to complete initialisation */
1142         for (i = 0; (i < 100) && !(inw(DEPCA_DATA) & IDON); i++);
1143
1144         if (i != 100) {
1145                 /* clear IDON by writing a "1", enable interrupts and start lance */
1146                 outw(IDON | INEA | STRT, DEPCA_DATA);
1147                 if (depca_debug > 2) {
1148                         printk("%s: DEPCA open after %d ticks, init block 0x%08lx csr0 %4.4x.\n", dev->name, i, virt_to_phys(lp->sh_mem), inw(DEPCA_DATA));
1149                 }
1150         } else {
1151                 printk("%s: DEPCA unopen after %d ticks, init block 0x%08lx csr0 %4.4x.\n", dev->name, i, virt_to_phys(lp->sh_mem), inw(DEPCA_DATA));
1152                 status = -1;
1153         }
1154
1155         return status;
1156 }
1157
1158 static struct net_device_stats *depca_get_stats(struct net_device *dev)
1159 {
1160         struct depca_private *lp = (struct depca_private *) dev->priv;
1161
1162         /* Null body since there is no framing error counter */
1163
1164         return &lp->stats;
1165 }
1166
1167 /*
1168 ** Set or clear the multicast filter for this adaptor.
1169 */
1170 static void set_multicast_list(struct net_device *dev)
1171 {
1172         struct depca_private *lp = (struct depca_private *) dev->priv;
1173         u_long ioaddr = dev->base_addr;
1174
1175         if (dev) {
1176                 netif_stop_queue(dev);
1177                 while (lp->tx_old != lp->tx_new);       /* Wait for the ring to empty */
1178
1179                 STOP_DEPCA;     /* Temporarily stop the depca.  */
1180                 depca_init_ring(dev);   /* Initialize the descriptor rings */
1181
1182                 if (dev->flags & IFF_PROMISC) { /* Set promiscuous mode */
1183                         lp->init_block.mode |= PROM;
1184                 } else {
1185                         SetMulticastFilter(dev);
1186                         lp->init_block.mode &= ~PROM;   /* Unset promiscuous mode */
1187                 }
1188
1189                 LoadCSRs(dev);  /* Reload CSR3 */
1190                 InitRestartDepca(dev);  /* Resume normal operation. */
1191                 netif_start_queue(dev); /* Unlock the TX ring */
1192         }
1193 }
1194
1195 /*
1196 ** Calculate the hash code and update the logical address filter
1197 ** from a list of ethernet multicast addresses.
1198 ** Big endian crc one liner is mine, all mine, ha ha ha ha!
1199 ** LANCE calculates its hash codes big endian.
1200 */
1201 static void SetMulticastFilter(struct net_device *dev)
1202 {
1203         struct depca_private *lp = (struct depca_private *) dev->priv;
1204         struct dev_mc_list *dmi = dev->mc_list;
1205         char *addrs;
1206         int i, j, bit, byte;
1207         u16 hashcode;
1208         u32 crc;
1209
1210         if (dev->flags & IFF_ALLMULTI) {        /* Set all multicast bits */
1211                 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1212                         lp->init_block.mcast_table[i] = (char) 0xff;
1213                 }
1214         } else {
1215                 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {   /* Clear the multicast table */
1216                         lp->init_block.mcast_table[i] = 0;
1217                 }
1218                 /* Add multicast addresses */
1219                 for (i = 0; i < dev->mc_count; i++) {   /* for each address in the list */
1220                         addrs = dmi->dmi_addr;
1221                         dmi = dmi->next;
1222                         if ((*addrs & 0x01) == 1) {     /* multicast address? */
1223                                 crc = ether_crc(ETH_ALEN, addrs);
1224                                 hashcode = (crc & 1);   /* hashcode is 6 LSb of CRC ... */
1225                                 for (j = 0; j < 5; j++) {       /* ... in reverse order. */
1226                                         hashcode = (hashcode << 1) | ((crc >>= 1) & 1);
1227                                 }
1228
1229
1230                                 byte = hashcode >> 3;   /* bit[3-5] -> byte in filter */
1231                                 bit = 1 << (hashcode & 0x07);   /* bit[0-2] -> bit in byte */
1232                                 lp->init_block.mcast_table[byte] |= bit;
1233                         }
1234                 }
1235         }
1236
1237         return;
1238 }
1239
1240 #ifdef CONFIG_MCA
1241 /*
1242 ** Microchannel bus I/O device probe
1243 */
1244 static void __init mca_probe(struct net_device *dev, u_long ioaddr)
1245 {
1246         unsigned char pos[2];
1247         unsigned char where;
1248         unsigned long iobase;
1249         int irq;
1250         int slot = 0;
1251
1252         /*
1253            ** See if we've been here before.
1254          */
1255         if ((!ioaddr && autoprobed) || (ioaddr && !loading_module))
1256                 return;
1257
1258         if (MCA_bus) {
1259                 /*
1260                    ** Search for the adapter.  If an address has been given, search 
1261                    ** specifically for the card at that address.  Otherwise find the
1262                    ** first card in the system.
1263                  */
1264                 while ((dev != NULL) && ((slot = mca_find_adapter(DE212_ID, slot)) != MCA_NOTFOUND)) {
1265                         pos[0] = mca_read_stored_pos(slot, 2);
1266                         pos[1] = mca_read_stored_pos(slot, 3);
1267
1268                         /*
1269                            ** IO of card is handled by bits 1 and 2 of pos0.    
1270                            **
1271                            **    bit2 bit1    IO
1272                            **       0    0    0x2c00
1273                            **       0    1    0x2c10
1274                            **       1    0    0x2c20
1275                            **       1    1    0x2c30
1276                          */
1277                         where = (pos[0] & 6) >> 1;
1278                         iobase = 0x2c00 + (0x10 * where);
1279
1280                         if ((ioaddr) && (ioaddr != iobase)) {
1281                                 /*
1282                                    ** Card was found, but not at the right IO location. Continue 
1283                                    ** scanning from the next MCA slot up for another card.
1284                                  */
1285                                 slot++;
1286                                 continue;
1287                         }
1288
1289                         /*
1290                            ** Found the adapter we were looking for. Now start setting it up.
1291                            ** 
1292                            ** First work on decoding the IRQ.  It's stored in the lower 4 bits
1293                            ** of pos1.  Bits are as follows (from the ADF file):
1294                            **
1295                            **      Bits           
1296                            **   3   2   1   0    IRQ 
1297                            **   --------------------
1298                            **   0   0   1   0     5
1299                            **   0   0   0   1     9
1300                            **   0   1   0   0    10
1301                            **   1   0   0   0    11
1302                            * */
1303                         where = pos[1] & 0x0f;
1304                         switch (where) {
1305                         case 1:
1306                                 irq = 9;
1307                                 break;
1308                         case 2:
1309                                 irq = 5;
1310                                 break;
1311                         case 4:
1312                                 irq = 10;
1313                                 break;
1314                         case 8:
1315                                 irq = 11;
1316                                 break;
1317                         default:
1318                                 printk("%s: mca_probe IRQ error.  You should never get here (%d).\n", dev->name, where);
1319                                 return;
1320                         }
1321
1322                         /*
1323                            ** Shared memory address of adapter is stored in bits 3-5 of pos0.
1324                            ** They are mapped as follows:
1325                            **
1326                            **    Bit
1327                            **   5  4  3       Memory Addresses
1328                            **   0  0  0       C0000-CFFFF (64K)
1329                            **   1  0  0       C8000-CFFFF (32K)
1330                            **   0  0  1       D0000-DFFFF (64K)
1331                            **   1  0  1       D8000-DFFFF (32K)
1332                            **   0  1  0       E0000-EFFFF (64K)
1333                            **   1  1  0       E8000-EFFFF (32K)
1334                          */
1335                         where = (pos[0] & 0x18) >> 3;
1336                         mem = 0xc0000 + (where * 0x10000);
1337                         if (pos[0] & 0x20) {
1338                                 mem += 0x8000;
1339                         }
1340
1341                         /*
1342                            ** Get everything allocated and initialized...  (almost just
1343                            ** like the ISA and EISA probes)
1344                          */
1345                         if (DevicePresent(iobase) != 0) {
1346                                 /*
1347                                    ** If the MCA configuration says the card should be here,
1348                                    ** it really should be here.
1349                                  */
1350                                 printk(KERN_ERR "%s: MCA reports card at 0x%lx but it is not responding.\n", dev->name, iobase);
1351                         }
1352
1353                         if (check_region(iobase, DEPCA_TOTAL_SIZE) == 0) {
1354                                 if ((dev = alloc_device(dev, iobase)) != NULL) {
1355                                         dev->irq = irq;
1356                                         if (depca_hw_init(dev, iobase, slot) == 0) {
1357                                                 /*
1358                                                    ** Adapter initialized correctly:  Name it in
1359                                                    ** /proc/mca.
1360                                                  */
1361                                                 mca_set_adapter_name(slot, "DE210/212 Ethernet Adapter");
1362                                                 mca_mark_as_used(slot);
1363                                                 num_depcas++;
1364                                         }
1365                                         num_eth++;
1366                                 }
1367                         } else if (autoprobed) {
1368                                 printk(KERN_WARNING "%s: region already allocated at 0x%04lx.\n", dev->name, iobase);
1369                         }
1370
1371                         /*
1372                            ** If this is a probe by a module, return after setting up the
1373                            ** given card.
1374                          */
1375                         if (ioaddr)
1376                                 return;
1377
1378                         /*
1379                            ** Set up to check the next slot and loop.
1380                          */
1381                         slot++;
1382                 }
1383         }
1384
1385         return;
1386 }
1387 #endif
1388
1389 /*
1390 ** ISA bus I/O device probe
1391 */
1392 static void __init isa_probe(struct net_device *dev, u_long ioaddr)
1393 {
1394         int i = num_depcas, maxSlots;
1395         s32 ports[] = DEPCA_IO_PORTS;
1396
1397         if (!ioaddr && autoprobed)
1398                 return;         /* Been here before ! */
1399         if (ioaddr > 0x400)
1400                 return;         /* EISA Address */
1401         if (i >= MAX_NUM_DEPCAS)
1402                 return;         /* Too many ISA adapters */
1403
1404         if (ioaddr == 0) {      /* Autoprobing */
1405                 maxSlots = MAX_NUM_DEPCAS;
1406         } else {                /* Probe a specific location */
1407                 ports[i] = ioaddr;
1408                 maxSlots = i + 1;
1409         }
1410
1411         for (; (i < maxSlots) && (dev != NULL) && ports[i]; i++) {
1412                 if (check_region(ports[i], DEPCA_TOTAL_SIZE) == 0) {
1413                         if (DevicePresent(ports[i]) == 0) {
1414                                 if ((dev = alloc_device(dev, ports[i])) != NULL) {
1415                                         if (depca_hw_init(dev, ports[i], -1) == 0) {
1416                                                 num_depcas++;
1417                                         }
1418                                         num_eth++;
1419                                 }
1420                         }
1421                 } else if (autoprobed) {
1422                         printk("%s: region already allocated at 0x%04x.\n", dev->name, ports[i]);
1423                 }
1424         }
1425
1426         return;
1427 }
1428
1429 /*
1430 ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1431 ** the motherboard. Upto 15 EISA devices are supported.
1432 */
1433 static void __init eisa_probe(struct net_device *dev, u_long ioaddr)
1434 {
1435         int i, maxSlots;
1436         u_long iobase;
1437         char name[DEPCA_STRLEN];
1438
1439         if (!ioaddr && autoprobed)
1440                 return;         /* Been here before ! */
1441         if ((ioaddr < 0x400) && (ioaddr > 0))
1442                 return;         /* ISA Address */
1443
1444         if (ioaddr == 0) {      /* Autoprobing */
1445                 iobase = EISA_SLOT_INC; /* Get the first slot address */
1446                 i = 1;
1447                 maxSlots = MAX_EISA_SLOTS;
1448         } else {                /* Probe a specific location */
1449                 iobase = ioaddr;
1450                 i = (ioaddr >> 12);
1451                 maxSlots = i + 1;
1452         }
1453         if ((iobase & 0x0fff) == 0)
1454                 iobase += DEPCA_EISA_IO_PORTS;
1455
1456         for (; (i < maxSlots) && (dev != NULL); i++, iobase += EISA_SLOT_INC) {
1457                 if (check_region(iobase, DEPCA_TOTAL_SIZE) == 0) {
1458                         if (EISA_signature(name, EISA_ID)) {
1459                                 if (DevicePresent(iobase) == 0) {
1460                                         if ((dev = alloc_device(dev, iobase)) != NULL) {
1461                                                 if (depca_hw_init(dev, iobase, -1) == 0) {
1462                                                         num_depcas++;
1463                                                 }
1464                                                 num_eth++;
1465                                         }
1466                                 }
1467                         }
1468                 } else if (autoprobed) {
1469                         printk("%s: region already allocated at 0x%04lx.\n", dev->name, iobase);
1470                 }
1471         }
1472
1473         return;
1474 }
1475
1476 /*
1477 ** Search the entire 'eth' device list for a fixed probe. If a match isn't
1478 ** found then check for an autoprobe or unused device location. If they
1479 ** are not available then insert a new device structure at the end of
1480 ** the current list.
1481 */
1482 static struct net_device *__init alloc_device(struct net_device *dev, u_long iobase)
1483 {
1484         struct net_device *adev = NULL;
1485         int fixed = 0, new_dev = 0;
1486
1487         num_eth = depca_dev_index(dev->name);
1488         if (loading_module)
1489                 return dev;
1490
1491         while (1) {
1492                 if (((dev->base_addr == DEPCA_NDA) || (dev->base_addr == 0)) && !adev) {
1493                         adev = dev;
1494                 } else if ((dev->priv == NULL) && (dev->base_addr == iobase)) {
1495                         fixed = 1;
1496                 } else {
1497                         if (dev->next == NULL) {
1498                                 new_dev = 1;
1499                         } else if (strncmp(dev->next->name, "eth", 3) != 0) {
1500                                 new_dev = 1;
1501                         }
1502                 }
1503                 if ((dev->next == NULL) || new_dev || fixed)
1504                         break;
1505                 dev = dev->next;
1506                 num_eth++;
1507         }
1508         if (adev && !fixed) {
1509                 dev = adev;
1510                 num_eth = depca_dev_index(dev->name);
1511                 new_dev = 0;
1512         }
1513
1514         if (((dev->next == NULL) && ((dev->base_addr != DEPCA_NDA) && (dev->base_addr != 0)) && !fixed) || new_dev) {
1515                 num_eth++;      /* New device */
1516                 dev = insert_device(dev, iobase, depca_probe);
1517         }
1518
1519         return dev;
1520 }
1521
1522 /*
1523 ** If at end of eth device list and can't use current entry, malloc
1524 ** one up. If memory could not be allocated, print an error message.
1525 */
1526 static struct net_device *__init insert_device(struct net_device *dev, u_long iobase, int (*init) (struct net_device *))
1527 {
1528         struct net_device *new;
1529
1530         new = (struct net_device *) kmalloc(sizeof(struct net_device), GFP_KERNEL);
1531         if (new == NULL) {
1532                 printk("eth%d: Device not initialised, insufficient memory\n", num_eth);
1533                 return NULL;
1534         } else {
1535                 new->next = dev->next;
1536                 dev->next = new;
1537                 dev = dev->next;        /* point to the new device */
1538                 if (num_eth > 9999) {
1539                         sprintf(dev->name, "eth????");  /* New device name */
1540                 } else {
1541                         sprintf(dev->name, "eth%d", num_eth);   /* New device name */
1542                 }
1543                 dev->base_addr = iobase;        /* assign the io address */
1544                 dev->init = init;       /* initialisation routine */
1545         }
1546
1547         return dev;
1548 }
1549
1550 static int __init depca_dev_index(char *s)
1551 {
1552         int i = 0, j = 0;
1553
1554         for (; *s; s++) {
1555                 if (isdigit(*s)) {
1556                         j = 1;
1557                         i = (i * 10) + (*s - '0');
1558                 } else if (j)
1559                         break;
1560         }
1561
1562         return i;
1563 }
1564
1565 /*
1566 ** Look for a particular board name in the on-board Remote Diagnostics
1567 ** and Boot (readb) ROM. This will also give us a clue to the network RAM
1568 ** base address.
1569 */
1570 static void __init DepcaSignature(char *name, u_long paddr)
1571 {
1572         u_int i, j, k;
1573         const char *signatures[] = DEPCA_SIGNATURE;
1574         void *ptr;
1575         char tmpstr[16];
1576
1577         /* Copy the first 16 bytes of ROM */
1578         ptr = ioremap(paddr + 0xc000, 16);
1579         if (ptr == NULL) {
1580                 printk(KERN_ERR "depca: I/O remap failed at %lx\n", paddr + 0xc000);
1581                 adapter = unknown;
1582                 return;
1583         }
1584         for (i = 0; i < 16; i++) {
1585                 tmpstr[i] = readb(ptr + i);
1586         }
1587         iounmap(ptr);
1588
1589         /* Check if PROM contains a valid string */
1590         for (i = 0; *signatures[i] != '\0'; i++) {
1591                 for (j = 0, k = 0; j < 16 && k < strlen(signatures[i]); j++) {
1592                         if (signatures[i][k] == tmpstr[j]) {    /* track signature */
1593                                 k++;
1594                         } else {        /* lost signature; begin search again */
1595                                 k = 0;
1596                         }
1597                 }
1598                 if (k == strlen(signatures[i]))
1599                         break;
1600         }
1601
1602         /* Check if name string is valid, provided there's no PROM */
1603         if (*name && (i == unknown)) {
1604                 for (i = 0; *signatures[i] != '\0'; i++) {
1605                         if (strcmp(name, signatures[i]) == 0)
1606                                 break;
1607                 }
1608         }
1609
1610         /* Update search results */
1611         strcpy(name, signatures[i]);
1612         adapter = i;
1613
1614         return;
1615 }
1616
1617 /*
1618 ** Look for a special sequence in the Ethernet station address PROM that
1619 ** is common across all DEPCA products. Note that the original DEPCA needs
1620 ** its ROM address counter to be initialized and enabled. Only enable
1621 ** if the first address octet is a 0x08 - this minimises the chances of
1622 ** messing around with some other hardware, but it assumes that this DEPCA
1623 ** card initialized itself correctly.
1624 ** 
1625 ** Search the Ethernet address ROM for the signature. Since the ROM address
1626 ** counter can start at an arbitrary point, the search must include the entire
1627 ** probe sequence length plus the (length_of_the_signature - 1).
1628 ** Stop the search IMMEDIATELY after the signature is found so that the
1629 ** PROM address counter is correctly positioned at the start of the
1630 ** ethernet address for later read out.
1631 */
1632 static int __init DevicePresent(u_long ioaddr)
1633 {
1634         union {
1635                 struct {
1636                         u32 a;
1637                         u32 b;
1638                 } llsig;
1639                 char Sig[sizeof(u32) << 1];
1640         }
1641         dev;
1642         short sigLength = 0;
1643         s8 data;
1644         s16 nicsr;
1645         int i, j, status = 0;
1646
1647         data = inb(DEPCA_PROM); /* clear counter on DEPCA */
1648         data = inb(DEPCA_PROM); /* read data */
1649
1650         if (data == 0x08) {     /* Enable counter on DEPCA */
1651                 nicsr = inb(DEPCA_NICSR);
1652                 nicsr |= AAC;
1653                 outb(nicsr, DEPCA_NICSR);
1654         }
1655
1656         dev.llsig.a = ETH_PROM_SIG;
1657         dev.llsig.b = ETH_PROM_SIG;
1658         sigLength = sizeof(u32) << 1;
1659
1660         for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1661                 data = inb(DEPCA_PROM);
1662                 if (dev.Sig[j] == data) {       /* track signature */
1663                         j++;
1664                 } else {        /* lost signature; begin search again */
1665                         if (data == dev.Sig[0]) {       /* rare case.... */
1666                                 j = 1;
1667                         } else {
1668                                 j = 0;
1669                         }
1670                 }
1671         }
1672
1673         if (j != sigLength) {
1674                 status = -ENODEV;       /* search failed */
1675         }
1676
1677         return status;
1678 }
1679
1680 /*
1681 ** The DE100 and DE101 PROM accesses were made non-standard for some bizarre
1682 ** reason: access the upper half of the PROM with x=0; access the lower half
1683 ** with x=1.
1684 */
1685 static int __init get_hw_addr(struct net_device *dev)
1686 {
1687         u_long ioaddr = dev->base_addr;
1688         int i, k, tmp, status = 0;
1689         u_short j, x, chksum;
1690
1691         x = (((adapter == de100) || (adapter == de101)) ? 1 : 0);
1692
1693         for (i = 0, k = 0, j = 0; j < 3; j++) {
1694                 k <<= 1;
1695                 if (k > 0xffff)
1696                         k -= 0xffff;
1697
1698                 k += (u_char) (tmp = inb(DEPCA_PROM + x));
1699                 dev->dev_addr[i++] = (u_char) tmp;
1700                 k += (u_short) ((tmp = inb(DEPCA_PROM + x)) << 8);
1701                 dev->dev_addr[i++] = (u_char) tmp;
1702
1703                 if (k > 0xffff)
1704                         k -= 0xffff;
1705         }
1706         if (k == 0xffff)
1707                 k = 0;
1708
1709         chksum = (u_char) inb(DEPCA_PROM + x);
1710         chksum |= (u_short) (inb(DEPCA_PROM + x) << 8);
1711         if (k != chksum)
1712                 status = -1;
1713
1714         return status;
1715 }
1716
1717 /*
1718 ** Load a packet into the shared memory
1719 */
1720 static int load_packet(struct net_device *dev, struct sk_buff *skb)
1721 {
1722         struct depca_private *lp = (struct depca_private *) dev->priv;
1723         int i, entry, end, len, status = 0;
1724
1725         entry = lp->tx_new;     /* Ring around buffer number. */
1726         end = (entry + (skb->len - 1) / TX_BUFF_SZ) & lp->txRingMask;
1727         if (!(readl(&lp->tx_ring[end].base) & T_OWN)) { /* Enough room? */
1728                 /* 
1729                    ** Caution: the write order is important here... don't set up the
1730                    ** ownership rights until all the other information is in place.
1731                  */
1732                 if (end < entry) {      /* wrapped buffer */
1733                         len = (lp->txRingMask - entry + 1) * TX_BUFF_SZ;
1734                         memcpy_toio(lp->tx_buff[entry], skb->data, len);
1735                         memcpy_toio(lp->tx_buff[0], skb->data + len, skb->len - len);
1736                 } else {        /* linear buffer */
1737                         memcpy_toio(lp->tx_buff[entry], skb->data, skb->len);
1738                 }
1739
1740                 /* set up the buffer descriptors */
1741                 len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
1742                 for (i = entry; i != end; i = (++i) & lp->txRingMask) {
1743                         /* clean out flags */
1744                         writel(readl(&lp->tx_ring[i].base) & ~T_FLAGS, &lp->tx_ring[i].base);
1745                         writew(0x0000, &lp->tx_ring[i].misc);   /* clears other error flags */
1746                         writew(-TX_BUFF_SZ, &lp->tx_ring[i].length);    /* packet length in buffer */
1747                         len -= TX_BUFF_SZ;
1748                 }
1749                 /* clean out flags */
1750                 writel(readl(&lp->tx_ring[end].base) & ~T_FLAGS, &lp->tx_ring[end].base);
1751                 writew(0x0000, &lp->tx_ring[end].misc); /* clears other error flags */
1752                 writew(-len, &lp->tx_ring[end].length); /* packet length in last buff */
1753
1754                 /* start of packet */
1755                 writel(readl(&lp->tx_ring[entry].base) | T_STP, &lp->tx_ring[entry].base);
1756                 /* end of packet */
1757                 writel(readl(&lp->tx_ring[end].base) | T_ENP, &lp->tx_ring[end].base);
1758
1759                 for (i = end; i != entry; --i) {
1760                         /* ownership of packet */
1761                         writel(readl(&lp->tx_ring[i].base) | T_OWN, &lp->tx_ring[i].base);
1762                         if (i == 0)
1763                                 i = lp->txRingMask + 1;
1764                 }
1765                 writel(readl(&lp->tx_ring[entry].base) | T_OWN, &lp->tx_ring[entry].base);
1766
1767                 lp->tx_new = (++end) & lp->txRingMask;  /* update current pointers */
1768         } else {
1769                 status = -1;
1770         }
1771
1772         return status;
1773 }
1774
1775 /*
1776 ** Look for a particular board name in the EISA configuration space
1777 */
1778 static int __init EISA_signature(char *name, s32 eisa_id)
1779 {
1780         u_int i;
1781         const char *signatures[] = DEPCA_SIGNATURE;
1782         char ManCode[DEPCA_STRLEN];
1783         union {
1784                 s32 ID;
1785                 char Id[4];
1786         } Eisa;
1787         int status = 0;
1788
1789         *name = '\0';
1790         Eisa.ID = inl(eisa_id);
1791
1792         ManCode[0] = (((Eisa.Id[0] >> 2) & 0x1f) + 0x40);
1793         ManCode[1] = (((Eisa.Id[1] & 0xe0) >> 5) + ((Eisa.Id[0] & 0x03) << 3) + 0x40);
1794         ManCode[2] = (((Eisa.Id[2] >> 4) & 0x0f) + 0x30);
1795         ManCode[3] = ((Eisa.Id[2] & 0x0f) + 0x30);
1796         ManCode[4] = (((Eisa.Id[3] >> 4) & 0x0f) + 0x30);
1797         ManCode[5] = '\0';
1798
1799         for (i = 0; (*signatures[i] != '\0') && (*name == '\0'); i++) {
1800                 if (strstr(ManCode, signatures[i]) != NULL) {
1801                         strcpy(name, ManCode);
1802                         status = 1;
1803                 }
1804         }
1805
1806         return status;
1807 }
1808
1809 static void depca_dbg_open(struct net_device *dev)
1810 {
1811         struct depca_private *lp = (struct depca_private *) dev->priv;
1812         u_long ioaddr = dev->base_addr;
1813         struct depca_init *p = &lp->init_block;
1814         int i;
1815
1816         if (depca_debug > 1) {
1817                 /* Do not copy the shadow init block into shared memory */
1818                 /* Debugging should not affect normal operation! */
1819                 /* The shadow init block will get copied across during InitRestartDepca */
1820                 printk("%s: depca open with irq %d\n", dev->name, dev->irq);
1821                 printk("Descriptor head addresses (CPU):\n");
1822                 printk("        0x%lx  0x%lx\n", (u_long) lp->rx_ring, (u_long) lp->tx_ring);
1823                 printk("Descriptor addresses (CPU):\nRX: ");
1824                 for (i = 0; i < lp->rxRingMask; i++) {
1825                         if (i < 3) {
1826                                 printk("0x%8.8lx ", (long) &lp->rx_ring[i].base);
1827                         }
1828                 }
1829                 printk("...0x%8.8lx\n", (long) &lp->rx_ring[i].base);
1830                 printk("TX: ");
1831                 for (i = 0; i < lp->txRingMask; i++) {
1832                         if (i < 3) {
1833                                 printk("0x%8.8lx ", (long) &lp->tx_ring[i].base);
1834                         }
1835                 }
1836                 printk("...0x%8.8lx\n", (long) &lp->tx_ring[i].base);
1837                 printk("\nDescriptor buffers (Device):\nRX: ");
1838                 for (i = 0; i < lp->rxRingMask; i++) {
1839                         if (i < 3) {
1840                                 printk("0x%8.8x  ", readl(&lp->rx_ring[i].base));
1841                         }
1842                 }
1843                 printk("...0x%8.8x\n", readl(&lp->rx_ring[i].base));
1844                 printk("TX: ");
1845                 for (i = 0; i < lp->txRingMask; i++) {
1846                         if (i < 3) {
1847                                 printk("0x%8.8x  ", readl(&lp->tx_ring[i].base));
1848                         }
1849                 }
1850                 printk("...0x%8.8x\n", readl(&lp->tx_ring[i].base));
1851                 printk("Initialisation block at 0x%8.8lx(Phys)\n", virt_to_phys(lp->sh_mem));
1852                 printk("        mode: 0x%4.4x\n", p->mode);
1853                 printk("        physical address: ");
1854                 for (i = 0; i < ETH_ALEN - 1; i++) {
1855                         printk("%2.2x:", p->phys_addr[i]);
1856                 }
1857                 printk("%2.2x\n", p->phys_addr[i]);
1858                 printk("        multicast hash table: ");
1859                 for (i = 0; i < (HASH_TABLE_LEN >> 3) - 1; i++) {
1860                         printk("%2.2x:", p->mcast_table[i]);
1861                 }
1862                 printk("%2.2x\n", p->mcast_table[i]);
1863                 printk("        rx_ring at: 0x%8.8x\n", p->rx_ring);
1864                 printk("        tx_ring at: 0x%8.8x\n", p->tx_ring);
1865                 printk("buffers (Phys): 0x%8.8lx\n", virt_to_phys(lp->sh_mem) + lp->buffs_offset);
1866                 printk("Ring size:\nRX: %d  Log2(rxRingMask): 0x%8.8x\n", (int) lp->rxRingMask + 1, lp->rx_rlen);
1867                 printk("TX: %d  Log2(txRingMask): 0x%8.8x\n", (int) lp->txRingMask + 1, lp->tx_rlen);
1868                 outw(CSR2, DEPCA_ADDR);
1869                 printk("CSR2&1: 0x%4.4x", inw(DEPCA_DATA));
1870                 outw(CSR1, DEPCA_ADDR);
1871                 printk("%4.4x\n", inw(DEPCA_DATA));
1872                 outw(CSR3, DEPCA_ADDR);
1873                 printk("CSR3: 0x%4.4x\n", inw(DEPCA_DATA));
1874         }
1875
1876         return;
1877 }
1878
1879 /*
1880 ** Perform IOCTL call functions here. Some are privileged operations and the
1881 ** effective uid is checked in those cases.
1882 ** All multicast IOCTLs will not work here and are for testing purposes only.
1883 */
1884 static int depca_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1885 {
1886         struct depca_private *lp = (struct depca_private *) dev->priv;
1887         struct depca_ioctl *ioc = (struct depca_ioctl *) &rq->ifr_data;
1888         int i, status = 0;
1889         u_long ioaddr = dev->base_addr;
1890         union {
1891                 u8 addr[(HASH_TABLE_LEN * ETH_ALEN)];
1892                 u16 sval[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1893                 u32 lval[(HASH_TABLE_LEN * ETH_ALEN) >> 2];
1894         } tmp;
1895
1896         switch (ioc->cmd) {
1897         case DEPCA_GET_HWADDR:  /* Get the hardware address */
1898                 for (i = 0; i < ETH_ALEN; i++) {
1899                         tmp.addr[i] = dev->dev_addr[i];
1900                 }
1901                 ioc->len = ETH_ALEN;
1902                 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
1903                         return -EFAULT;
1904                 break;
1905
1906         case DEPCA_SET_HWADDR:  /* Set the hardware address */
1907                 if (!capable(CAP_NET_ADMIN))
1908                         return -EPERM;
1909                 if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN))
1910                         return -EFAULT;
1911                 for (i = 0; i < ETH_ALEN; i++) {
1912                         dev->dev_addr[i] = tmp.addr[i];
1913                 }
1914                 netif_stop_queue(dev);
1915                 while (lp->tx_old != lp->tx_new)
1916                         cpu_relax();    /* Wait for the ring to empty */
1917
1918                 STOP_DEPCA;     /* Temporarily stop the depca.  */
1919                 depca_init_ring(dev);   /* Initialize the descriptor rings */
1920                 LoadCSRs(dev);  /* Reload CSR3 */
1921                 InitRestartDepca(dev);  /* Resume normal operation. */
1922                 netif_start_queue(dev); /* Unlock the TX ring */
1923                 break;
1924
1925         case DEPCA_SET_PROM:    /* Set Promiscuous Mode */
1926                 if (!capable(CAP_NET_ADMIN))
1927                         return -EPERM;
1928                 netif_stop_queue(dev);
1929                 while (lp->tx_old != lp->tx_new)
1930                         cpu_relax();    /* Wait for the ring to empty */
1931
1932                 STOP_DEPCA;     /* Temporarily stop the depca.  */
1933                 depca_init_ring(dev);   /* Initialize the descriptor rings */
1934                 lp->init_block.mode |= PROM;    /* Set promiscuous mode */
1935
1936                 LoadCSRs(dev);  /* Reload CSR3 */
1937                 InitRestartDepca(dev);  /* Resume normal operation. */
1938                 netif_start_queue(dev); /* Unlock the TX ring */
1939                 break;
1940
1941         case DEPCA_CLR_PROM:    /* Clear Promiscuous Mode */
1942                 if (!capable(CAP_NET_ADMIN))
1943                         return -EPERM;
1944                 netif_stop_queue(dev);
1945                 while (lp->tx_old != lp->tx_new)
1946                         cpu_relax();    /* Wait for the ring to empty */
1947
1948                 STOP_DEPCA;     /* Temporarily stop the depca.  */
1949                 depca_init_ring(dev);   /* Initialize the descriptor rings */
1950                 lp->init_block.mode &= ~PROM;   /* Clear promiscuous mode */
1951
1952                 LoadCSRs(dev);  /* Reload CSR3 */
1953                 InitRestartDepca(dev);  /* Resume normal operation. */
1954                 netif_start_queue(dev); /* Unlock the TX ring */
1955                 break;
1956
1957         case DEPCA_SAY_BOO:     /* Say "Boo!" to the kernel log file */
1958                 if(!capable(CAP_NET_ADMIN))
1959                         return -EPERM;
1960                 printk("%s: Boo!\n", dev->name);
1961                 break;
1962
1963         case DEPCA_GET_MCA:     /* Get the multicast address table */
1964                 ioc->len = (HASH_TABLE_LEN >> 3);
1965                 if (copy_to_user(ioc->data, lp->init_block.mcast_table, ioc->len))
1966                         return -EFAULT;
1967                 break;
1968
1969         case DEPCA_SET_MCA:     /* Set a multicast address */
1970                 if (!capable(CAP_NET_ADMIN))
1971                         return -EPERM;
1972                 if (ioc->len >= HASH_TABLE_LEN)
1973                         return -EINVAL;
1974                 if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN * ioc->len))
1975                         return -EFAULT;
1976                 set_multicast_list(dev);
1977                 break;
1978
1979         case DEPCA_CLR_MCA:     /* Clear all multicast addresses */
1980                 if (!capable(CAP_NET_ADMIN))
1981                         return -EPERM;
1982                 set_multicast_list(dev);
1983                 break;
1984
1985         case DEPCA_MCA_EN:      /* Enable pass all multicast addressing */
1986                 if (!capable(CAP_NET_ADMIN))
1987                         return -EPERM;
1988                 set_multicast_list(dev);
1989                 break;
1990
1991         case DEPCA_GET_STATS:   /* Get the driver statistics */
1992                 cli();
1993                 ioc->len = sizeof(lp->pktStats);
1994                 if (copy_to_user(ioc->data, &lp->pktStats, ioc->len))
1995                         status = -EFAULT;
1996                 sti();
1997                 break;
1998
1999         case DEPCA_CLR_STATS:   /* Zero out the driver statistics */
2000                 if (!capable(CAP_NET_ADMIN))
2001                         return -EPERM;
2002                 cli();
2003                 memset(&lp->pktStats, 0, sizeof(lp->pktStats));
2004                 sti();
2005                 break;
2006
2007         case DEPCA_GET_REG:     /* Get the DEPCA Registers */
2008                 i = 0;
2009                 tmp.sval[i++] = inw(DEPCA_NICSR);
2010                 outw(CSR0, DEPCA_ADDR); /* status register */
2011                 tmp.sval[i++] = inw(DEPCA_DATA);
2012                 memcpy(&tmp.sval[i], &lp->init_block, sizeof(struct depca_init));
2013                 ioc->len = i + sizeof(struct depca_init);
2014                 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
2015                         return -EFAULT;
2016                 break;
2017
2018         default:
2019                 return -EOPNOTSUPP;
2020         }
2021
2022         return status;
2023 }
2024
2025 #ifdef MODULE
2026 static struct net_device thisDepca;
2027 static int irq = 7;             /* EDIT THESE LINE FOR YOUR CONFIGURATION */
2028 static int io = 0x200;          /* Or use the irq= io= options to insmod */
2029 MODULE_PARM(irq, "i");
2030 MODULE_PARM(io, "i");
2031 MODULE_PARM_DESC(irq, "DEPCA IRQ number");
2032 MODULE_PARM_DESC(io, "DEPCA I/O base address");
2033
2034 /* See depca_probe() for autoprobe messages when a module */
2035 int init_module(void)
2036 {
2037         thisDepca.irq = irq;
2038         thisDepca.base_addr = io;
2039         thisDepca.init = depca_probe;
2040
2041         if (register_netdev(&thisDepca) != 0)
2042                 return -EIO;
2043
2044         return 0;
2045 }
2046
2047 void cleanup_module(void)
2048 {
2049         struct depca_private *lp = thisDepca.priv;
2050
2051         unregister_netdev(&thisDepca);
2052         if (lp) {
2053                 iounmap(lp->sh_mem);
2054 #ifdef CONFIG_MCA
2055                 if (lp->mca_slot != -1)
2056                         mca_mark_as_unused(lp->mca_slot);
2057 #endif
2058                 kfree(lp);
2059                 thisDepca.priv = NULL;
2060         }
2061         thisDepca.irq = 0;
2062
2063         release_region(thisDepca.base_addr, DEPCA_TOTAL_SIZE);
2064 }
2065 #endif                          /* MODULE */
2066 MODULE_LICENSE("GPL");
2067 \f
2068
2069 /*
2070  * Local variables:
2071  *  compile-command: "gcc -D__KERNEL__ -I/linux/include -Wall -Wstrict-prototypes -fomit-frame-pointer -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2 -O2 -m486 -c depca.c"
2072  *
2073  *  compile-command: "gcc -D__KERNEL__ -DMODULE -I/linux/include -Wall -Wstrict-prototypes -fomit-frame-pointer -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2 -O2 -m486 -c depca.c"
2074  * End:
2075  */