make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / net / smc9194.c
1 /*------------------------------------------------------------------------
2  . smc9194.c
3  . This is a driver for SMC's 9000 series of Ethernet cards.
4  .
5  . Copyright (C) 1996 by Erik Stahlman
6  . This software may be used and distributed according to the terms
7  . of the GNU General Public License, incorporated herein by reference.
8  .
9  . "Features" of the SMC chip:
10  .   4608 byte packet memory. ( for the 91C92.  Others have more )
11  .   EEPROM for configuration
12  .   AUI/TP selection  ( mine has 10Base2/10BaseT select )
13  .
14  . Arguments:
15  .      io               = for the base address
16  .      irq      = for the IRQ
17  .      ifport = 0 for autodetect, 1 for TP, 2 for AUI ( or 10base2 )
18  .
19  . author:
20  .      Erik Stahlman                           ( erik@vt.edu )
21  . contributors:
22  .      Arnaldo Carvalho de Melo <acme@conectiva.com.br>
23  .
24  . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
25  .
26  . Sources:
27  .    o   SMC databook
28  .    o   skeleton.c by Donald Becker ( becker@scyld.com )
29  .    o   ( a LOT of advice from Becker as well )
30  .
31  . History:
32  .      12/07/95  Erik Stahlman  written, got receive/xmit handled
33  .      01/03/96  Erik Stahlman  worked out some bugs, actually usable!!! :-)
34  .      01/06/96  Erik Stahlman  cleaned up some, better testing, etc
35  .      01/29/96  Erik Stahlman  fixed autoirq, added multicast
36  .      02/01/96  Erik Stahlman  1. disabled all interrupts in smc_reset
37  .                               2. got rid of post-decrementing bug -- UGH.
38  .      02/13/96  Erik Stahlman  Tried to fix autoirq failure.  Added more
39  .                               descriptive error messages.
40  .      02/15/96  Erik Stahlman  Fixed typo that caused detection failure
41  .      02/23/96  Erik Stahlman  Modified it to fit into kernel tree
42  .                               Added support to change hardware address
43  .                               Cleared stats on opens
44  .      02/26/96  Erik Stahlman  Trial support for Kernel 1.2.13
45  .                               Kludge for automatic IRQ detection
46  .      03/04/96  Erik Stahlman  Fixed kernel 1.3.70 +
47  .                               Fixed bug reported by Gardner Buchanan in
48  .                                 smc_enable, with outw instead of outb
49  .      03/06/96  Erik Stahlman  Added hardware multicast from Peter Cammaert
50  .      04/14/00  Heiko Pruessing (SMA Regelsysteme)  Fixed bug in chip memory
51  .                               allocation
52  .      08/20/00  Arnaldo Melo   fix kfree(skb) in smc_hardware_send_packet
53  .      12/15/00  Christian Jullien fix "Warning: kfree_skb on hard IRQ"
54  .      11/08/01 Matt Domsch     Use common crc32 function
55  ----------------------------------------------------------------------------*/
56
57 static const char version[] =
58         "smc9194.c:v0.14 12/15/00 by Erik Stahlman (erik@vt.edu)\n";
59
60 #include <linux/module.h>
61 #include <linux/version.h>
62 #include <linux/kernel.h>
63 #include <linux/sched.h>
64 #include <linux/types.h>
65 #include <linux/fcntl.h>
66 #include <linux/interrupt.h>
67 #include <linux/ptrace.h>
68 #include <linux/ioport.h>
69 #include <linux/in.h>
70 #include <linux/slab.h>
71 #include <linux/string.h>
72 #include <linux/init.h>
73 #include <linux/crc32.h>
74 #include <asm/bitops.h>
75 #include <asm/io.h>
76 #include <linux/errno.h>
77
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/skbuff.h>
81
82 #include "smc9194.h"
83 /*------------------------------------------------------------------------
84  .
85  . Configuration options, for the experienced user to change.
86  .
87  -------------------------------------------------------------------------*/
88
89 /*
90  . Do you want to use 32 bit xfers?  This should work on all chips, as
91  . the chipset is designed to accommodate them.
92 */
93 #ifdef __sh__
94 #undef USE_32_BIT
95 #else
96 #define USE_32_BIT 1
97 #endif
98
99 /*
100  .the SMC9194 can be at any of the following port addresses.  To change,
101  .for a slightly different card, you can add it to the array.  Keep in
102  .mind that the array must end in zero.
103 */
104 static unsigned int smc_portlist[] __initdata = { 
105         0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
106         0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0, 0
107 };
108
109 /*
110  . Wait time for memory to be free.  This probably shouldn't be
111  . tuned that much, as waiting for this means nothing else happens
112  . in the system
113 */
114 #define MEMORY_WAIT_TIME 16
115
116 /*
117  . DEBUGGING LEVELS
118  .
119  . 0 for normal operation
120  . 1 for slightly more details
121  . >2 for various levels of increasingly useless information
122  .    2 for interrupt tracking, status flags
123  .    3 for packet dumps, etc.
124 */
125 #define SMC_DEBUG 0
126
127 #if (SMC_DEBUG > 2 )
128 #define PRINTK3(x) printk x
129 #else
130 #define PRINTK3(x)
131 #endif
132
133 #if SMC_DEBUG > 1
134 #define PRINTK2(x) printk x
135 #else
136 #define PRINTK2(x)
137 #endif
138
139 #ifdef SMC_DEBUG
140 #define PRINTK(x) printk x
141 #else
142 #define PRINTK(x)
143 #endif
144
145
146 /*------------------------------------------------------------------------
147  .
148  . The internal workings of the driver.  If you are changing anything
149  . here with the SMC stuff, you should have the datasheet and known
150  . what you are doing.
151  .
152  -------------------------------------------------------------------------*/
153 #define CARDNAME "SMC9194"
154
155
156 /* store this information for the driver.. */
157 struct smc_local {
158         /*
159            these are things that the kernel wants me to keep, so users
160            can find out semi-useless statistics of how well the card is
161            performing
162         */
163         struct net_device_stats stats;
164
165         /*
166            If I have to wait until memory is available to send
167            a packet, I will store the skbuff here, until I get the
168            desired memory.  Then, I'll send it out and free it.
169         */
170         struct sk_buff * saved_skb;
171
172         /*
173          . This keeps track of how many packets that I have
174          . sent out.  When an TX_EMPTY interrupt comes, I know
175          . that all of these have been sent.
176         */
177         int     packets_waiting;
178 };
179
180
181 /*-----------------------------------------------------------------
182  .
183  .  The driver can be entered at any of the following entry points.
184  .
185  .------------------------------------------------------------------  */
186
187 /*
188  . This is called by  register_netdev().  It is responsible for
189  . checking the portlist for the SMC9000 series chipset.  If it finds
190  . one, then it will initialize the device, find the hardware information,
191  . and sets up the appropriate device parameters.
192  . NOTE: Interrupts are *OFF* when this procedure is called.
193  .
194  . NB:This shouldn't be static since it is referred to externally.
195 */
196 int smc_init(struct net_device *dev);
197
198 /*
199  . The kernel calls this function when someone wants to use the device,
200  . typically 'ifconfig ethX up'.
201 */
202 static int smc_open(struct net_device *dev);
203
204 /*
205  . Our watchdog timed out. Called by the networking layer
206 */
207 static void smc_timeout(struct net_device *dev);
208
209 /*
210  . This is called by the kernel in response to 'ifconfig ethX down'.  It
211  . is responsible for cleaning up everything that the open routine
212  . does, and maybe putting the card into a powerdown state.
213 */
214 static int smc_close(struct net_device *dev);
215
216 /*
217  . This routine allows the proc file system to query the driver's
218  . statistics.
219 */
220 static struct net_device_stats * smc_query_statistics( struct net_device *dev);
221
222 /*
223  . Finally, a call to set promiscuous mode ( for TCPDUMP and related
224  . programs ) and multicast modes.
225 */
226 static void smc_set_multicast_list(struct net_device *dev);
227
228
229 /*---------------------------------------------------------------
230  .
231  . Interrupt level calls..
232  .
233  ----------------------------------------------------------------*/
234
235 /*
236  . Handles the actual interrupt
237 */
238 static void smc_interrupt(int irq, void *, struct pt_regs *regs);
239 /*
240  . This is a separate procedure to handle the receipt of a packet, to
241  . leave the interrupt code looking slightly cleaner
242 */
243 static inline void smc_rcv( struct net_device *dev );
244 /*
245  . This handles a TX interrupt, which is only called when an error
246  . relating to a packet is sent.
247 */
248 static inline void smc_tx( struct net_device * dev );
249
250 /*
251  ------------------------------------------------------------
252  .
253  . Internal routines
254  .
255  ------------------------------------------------------------
256 */
257
258 /*
259  . Test if a given location contains a chip, trying to cause as
260  . little damage as possible if it's not a SMC chip.
261 */
262 static int smc_probe(struct net_device *dev, int ioaddr);
263
264 /*
265  . A rather simple routine to print out a packet for debugging purposes.
266 */
267 #if SMC_DEBUG > 2
268 static void print_packet( byte *, int );
269 #endif
270
271 #define tx_done(dev) 1
272
273 /* this is called to actually send the packet to the chip */
274 static void smc_hardware_send_packet( struct net_device * dev );
275
276 /* Since I am not sure if I will have enough room in the chip's ram
277  . to store the packet, I call this routine, which either sends it
278  . now, or generates an interrupt when the card is ready for the
279  . packet */
280 static int  smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );
281
282 /* this does a soft reset on the device */
283 static void smc_reset( int ioaddr );
284
285 /* Enable Interrupts, Receive, and Transmit */
286 static void smc_enable( int ioaddr );
287
288 /* this puts the device in an inactive state */
289 static void smc_shutdown( int ioaddr );
290
291 /* This routine will find the IRQ of the driver if one is not
292  . specified in the input to the device.  */
293 static int smc_findirq( int ioaddr );
294
295 /*
296  . Function: smc_reset( int ioaddr )
297  . Purpose:
298  .      This sets the SMC91xx chip to its normal state, hopefully from whatever
299  .      mess that any other DOS driver has put it in.
300  .
301  . Maybe I should reset more registers to defaults in here?  SOFTRESET  should
302  . do that for me.
303  .
304  . Method:
305  .      1.  send a SOFT RESET
306  .      2.  wait for it to finish
307  .      3.  enable autorelease mode
308  .      4.  reset the memory management unit
309  .      5.  clear all interrupts
310  .
311 */
312 static void smc_reset( int ioaddr )
313 {
314         /* This resets the registers mostly to defaults, but doesn't
315            affect EEPROM.  That seems unnecessary */
316         SMC_SELECT_BANK( 0 );
317         outw( RCR_SOFTRESET, ioaddr + RCR );
318
319         /* this should pause enough for the chip to be happy */
320         SMC_DELAY( );
321
322         /* Set the transmit and receive configuration registers to
323            default values */
324         outw( RCR_CLEAR, ioaddr + RCR );
325         outw( TCR_CLEAR, ioaddr + TCR );
326
327         /* set the control register to automatically
328            release successfully transmitted packets, to make the best
329            use out of our limited memory */
330         SMC_SELECT_BANK( 1 );
331         outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
332
333         /* Reset the MMU */
334         SMC_SELECT_BANK( 2 );
335         outw( MC_RESET, ioaddr + MMU_CMD );
336
337         /* Note:  It doesn't seem that waiting for the MMU busy is needed here,
338            but this is a place where future chipsets _COULD_ break.  Be wary
339            of issuing another MMU command right after this */
340
341         outb( 0, ioaddr + INT_MASK );
342 }
343
344 /*
345  . Function: smc_enable
346  . Purpose: let the chip talk to the outside work
347  . Method:
348  .      1.  Enable the transmitter
349  .      2.  Enable the receiver
350  .      3.  Enable interrupts
351 */
352 static void smc_enable( int ioaddr )
353 {
354         SMC_SELECT_BANK( 0 );
355         /* see the header file for options in TCR/RCR NORMAL*/
356         outw( TCR_NORMAL, ioaddr + TCR );
357         outw( RCR_NORMAL, ioaddr + RCR );
358
359         /* now, enable interrupts */
360         SMC_SELECT_BANK( 2 );
361         outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
362 }
363
364 /*
365  . Function: smc_shutdown
366  . Purpose:  closes down the SMC91xxx chip.
367  . Method:
368  .      1. zero the interrupt mask
369  .      2. clear the enable receive flag
370  .      3. clear the enable xmit flags
371  .
372  . TODO:
373  .   (1) maybe utilize power down mode.
374  .      Why not yet?  Because while the chip will go into power down mode,
375  .      the manual says that it will wake up in response to any I/O requests
376  .      in the register space.   Empirical results do not show this working.
377 */
378 static void smc_shutdown( int ioaddr )
379 {
380         /* no more interrupts for me */
381         SMC_SELECT_BANK( 2 );
382         outb( 0, ioaddr + INT_MASK );
383
384         /* and tell the card to stay away from that nasty outside world */
385         SMC_SELECT_BANK( 0 );
386         outb( RCR_CLEAR, ioaddr + RCR );
387         outb( TCR_CLEAR, ioaddr + TCR );
388 #if 0
389         /* finally, shut the chip down */
390         SMC_SELECT_BANK( 1 );
391         outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL  );
392 #endif
393 }
394
395
396 /*
397  . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
398  . Purpose:
399  .    This sets the internal hardware table to filter out unwanted multicast
400  .    packets before they take up memory.
401  .
402  .    The SMC chip uses a hash table where the high 6 bits of the CRC of
403  .    address are the offset into the table.  If that bit is 1, then the
404  .    multicast packet is accepted.  Otherwise, it's dropped silently.
405  .
406  .    To use the 6 bits as an offset into the table, the high 3 bits are the
407  .    number of the 8 bit register, while the low 3 bits are the bit within
408  .    that register.
409  .
410  . This routine is based very heavily on the one provided by Peter Cammaert.
411 */
412
413
414 static void smc_setmulticast( int ioaddr, int count, struct dev_mc_list * addrs ) {
415         int                     i;
416         unsigned char           multicast_table[ 8 ];
417         struct dev_mc_list      * cur_addr;
418         /* table for flipping the order of 3 bits */
419         unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
420
421         /* start with a table of all zeros: reject all */
422         memset( multicast_table, 0, sizeof( multicast_table ) );
423
424         cur_addr = addrs;
425         for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next  ) {
426                 int position;
427
428                 /* do we have a pointer here? */
429                 if ( !cur_addr )
430                         break;
431                 /* make sure this is a multicast address - shouldn't this
432                    be a given if we have it here ? */
433                 if ( !( *cur_addr->dmi_addr & 1 ) )
434                         continue;
435
436                 /* only use the low order bits */
437                 position = ether_crc_le(6, cur_addr->dmi_addr) & 0x3f;
438
439                 /* do some messy swapping to put the bit in the right spot */
440                 multicast_table[invert3[position&7]] |=
441                                         (1<<invert3[(position>>3)&7]);
442
443         }
444         /* now, the table can be loaded into the chipset */
445         SMC_SELECT_BANK( 3 );
446
447         for ( i = 0; i < 8 ; i++ ) {
448                 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
449         }
450 }
451
452 /*
453  . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
454  . Purpose:
455  .    Attempt to allocate memory for a packet, if chip-memory is not
456  .    available, then tell the card to generate an interrupt when it
457  .    is available.
458  .
459  . Algorithm:
460  .
461  . o    if the saved_skb is not currently null, then drop this packet
462  .      on the floor.  This should never happen, because of TBUSY.
463  . o    if the saved_skb is null, then replace it with the current packet,
464  . o    See if I can sending it now.
465  . o    (NO): Enable interrupts and let the interrupt handler deal with it.
466  . o    (YES):Send it now.
467 */
468 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
469 {
470         struct smc_local *lp    = (struct smc_local *)dev->priv;
471         unsigned short ioaddr   = dev->base_addr;
472         word                    length;
473         unsigned short          numPages;
474         word                    time_out;
475
476         netif_stop_queue(dev);
477         /* Well, I want to send the packet.. but I don't know
478            if I can send it right now...  */
479
480         if ( lp->saved_skb) {
481                 /* THIS SHOULD NEVER HAPPEN. */
482                 lp->stats.tx_aborted_errors++;
483                 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
484                 return 1;
485         }
486         lp->saved_skb = skb;
487
488         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
489
490                 
491         /*
492         ** The MMU wants the number of pages to be the number of 256 bytes
493         ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
494         **
495         ** Pkt size for allocating is data length +6 (for additional status words,
496         ** length and ctl!) If odd size last byte is included in this header.
497         */
498         numPages =  ((length & 0xfffe) + 6) / 256;
499
500         if (numPages > 7 ) {
501                 printk(CARDNAME": Far too big packet error. \n");
502                 /* freeing the packet is a good thing here... but should
503                  . any packets of this size get down here?   */
504                 dev_kfree_skb (skb);
505                 lp->saved_skb = NULL;
506                 /* this IS an error, but, i don't want the skb saved */
507                 netif_wake_queue(dev);
508                 return 0;
509         }
510         /* either way, a packet is waiting now */
511         lp->packets_waiting++;
512
513         /* now, try to allocate the memory */
514         SMC_SELECT_BANK( 2 );
515         outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
516         /*
517         . Performance Hack
518         .
519         . wait a short amount of time.. if I can send a packet now, I send
520         . it now.  Otherwise, I enable an interrupt and wait for one to be
521         . available.
522         .
523         . I could have handled this a slightly different way, by checking to
524         . see if any memory was available in the FREE MEMORY register.  However,
525         . either way, I need to generate an allocation, and the allocation works
526         . no matter what, so I saw no point in checking free memory.
527         */
528         time_out = MEMORY_WAIT_TIME;
529         do {
530                 word    status;
531
532                 status = inb( ioaddr + INTERRUPT );
533                 if ( status & IM_ALLOC_INT ) {
534                         /* acknowledge the interrupt */
535                         outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
536                         break;
537                 }
538         } while ( -- time_out );
539
540         if ( !time_out ) {
541                 /* oh well, wait until the chip finds memory later */
542                 SMC_ENABLE_INT( IM_ALLOC_INT );
543                 PRINTK2((CARDNAME": memory allocation deferred. \n"));
544                 /* it's deferred, but I'll handle it later */
545                 return 0;
546         }
547         /* or YES! I can send the packet now.. */
548         smc_hardware_send_packet(dev);
549         netif_wake_queue(dev);
550         return 0;
551 }
552
553 /*
554  . Function:  smc_hardware_send_packet(struct net_device * )
555  . Purpose:
556  .      This sends the actual packet to the SMC9xxx chip.
557  .
558  . Algorithm:
559  .      First, see if a saved_skb is available.
560  .              ( this should NOT be called if there is no 'saved_skb'
561  .      Now, find the packet number that the chip allocated
562  .      Point the data pointers at it in memory
563  .      Set the length word in the chip's memory
564  .      Dump the packet to chip memory
565  .      Check if a last byte is needed ( odd length packet )
566  .              if so, set the control flag right
567  .      Tell the card to send it
568  .      Enable the transmit interrupt, so I know if it failed
569  .      Free the kernel data if I actually sent it.
570 */
571 static void smc_hardware_send_packet( struct net_device * dev )
572 {
573         struct smc_local *lp = (struct smc_local *)dev->priv;
574         byte                    packet_no;
575         struct sk_buff *        skb = lp->saved_skb;
576         word                    length;
577         unsigned short          ioaddr;
578         byte                    * buf;
579
580         ioaddr = dev->base_addr;
581
582         if ( !skb ) {
583                 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
584                 return;
585         }
586         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
587         buf = skb->data;
588
589         /* If I get here, I _know_ there is a packet slot waiting for me */
590         packet_no = inb( ioaddr + PNR_ARR + 1 );
591         if ( packet_no & 0x80 ) {
592                 /* or isn't there?  BAD CHIP! */
593                 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
594                 dev_kfree_skb_any(skb);
595                 lp->saved_skb = NULL;
596                 netif_wake_queue(dev);
597                 return;
598         }
599
600         /* we have a packet address, so tell the card to use it */
601         outb( packet_no, ioaddr + PNR_ARR );
602
603         /* point to the beginning of the packet */
604         outw( PTR_AUTOINC , ioaddr + POINTER );
605
606         PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
607 #if SMC_DEBUG > 2
608         print_packet( buf, length );
609 #endif
610
611         /* send the packet length ( +6 for status, length and ctl byte )
612            and the status word ( set to zeros ) */
613 #ifdef USE_32_BIT
614         outl(  (length +6 ) << 16 , ioaddr + DATA_1 );
615 #else
616         outw( 0, ioaddr + DATA_1 );
617         /* send the packet length ( +6 for status words, length, and ctl*/
618         outb( (length+6) & 0xFF,ioaddr + DATA_1 );
619         outb( (length+6) >> 8 , ioaddr + DATA_1 );
620 #endif
621
622         /* send the actual data
623          . I _think_ it's faster to send the longs first, and then
624          . mop up by sending the last word.  It depends heavily
625          . on alignment, at least on the 486.  Maybe it would be
626          . a good idea to check which is optimal?  But that could take
627          . almost as much time as is saved?
628         */
629 #ifdef USE_32_BIT
630         if ( length & 0x2  ) {
631                 outsl(ioaddr + DATA_1, buf,  length >> 2 );
632                 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
633         }
634         else
635                 outsl(ioaddr + DATA_1, buf,  length >> 2 );
636 #else
637         outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
638 #endif
639         /* Send the last byte, if there is one.   */
640
641         if ( (length & 1) == 0 ) {
642                 outw( 0, ioaddr + DATA_1 );
643         } else {
644                 outb( buf[length -1 ], ioaddr + DATA_1 );
645                 outb( 0x20, ioaddr + DATA_1);
646         }
647
648         /* enable the interrupts */
649         SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
650
651         /* and let the chipset deal with it */
652         outw( MC_ENQUEUE , ioaddr + MMU_CMD );
653
654         PRINTK2((CARDNAME": Sent packet of length %d \n",length));
655
656         lp->saved_skb = NULL;
657         dev_kfree_skb_any (skb);
658
659         dev->trans_start = jiffies;
660
661         /* we can send another packet */
662         netif_wake_queue(dev);
663
664         return;
665 }
666
667 /*-------------------------------------------------------------------------
668  |
669  | smc_init( struct net_device * dev )
670  |   Input parameters:
671  |      dev->base_addr == 0, try to find all possible locations
672  |      dev->base_addr == 1, return failure code
673  |      dev->base_addr == 2, always allocate space,  and return success
674  |      dev->base_addr == <anything else>   this is the address to check
675  |
676  |   Output:
677  |      0 --> there is a device
678  |      anything else, error
679  |
680  ---------------------------------------------------------------------------
681 */
682 int __init smc_init(struct net_device *dev)
683 {
684         int i;
685         int base_addr = dev->base_addr;
686
687         SET_MODULE_OWNER(dev);
688
689         /*  try a specific location */
690         if (base_addr > 0x1ff)
691                 return smc_probe(dev, base_addr);
692         else if (base_addr != 0)
693                 return -ENXIO;
694
695         /* check every ethernet address */
696         for (i = 0; smc_portlist[i]; i++)
697                 if (smc_probe(dev, smc_portlist[i]) == 0)
698                         return 0;
699
700         /* couldn't find anything */
701         return -ENODEV;
702 }
703
704 /*----------------------------------------------------------------------
705  . smc_findirq
706  .
707  . This routine has a simple purpose -- make the SMC chip generate an
708  . interrupt, so an auto-detect routine can detect it, and find the IRQ,
709  ------------------------------------------------------------------------
710 */
711 int __init smc_findirq( int ioaddr )
712 {
713         int     timeout = 20;
714         unsigned long cookie;
715
716
717         /* I have to do a STI() here, because this is called from
718            a routine that does an CLI during this process, making it
719            rather difficult to get interrupts for auto detection */
720         sti();
721
722         cookie = probe_irq_on();
723
724         /*
725          * What I try to do here is trigger an ALLOC_INT. This is done
726          * by allocating a small chunk of memory, which will give an interrupt
727          * when done.
728          */
729
730
731         SMC_SELECT_BANK(2);
732         /* enable ALLOCation interrupts ONLY */
733         outb( IM_ALLOC_INT, ioaddr + INT_MASK );
734
735         /*
736          . Allocate 512 bytes of memory.  Note that the chip was just
737          . reset so all the memory is available
738         */
739         outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
740
741         /*
742          . Wait until positive that the interrupt has been generated
743         */
744         while ( timeout ) {
745                 byte    int_status;
746
747                 int_status = inb( ioaddr + INTERRUPT );
748
749                 if ( int_status & IM_ALLOC_INT )
750                         break;          /* got the interrupt */
751                 timeout--;
752         }
753         /* there is really nothing that I can do here if timeout fails,
754            as autoirq_report will return a 0 anyway, which is what I
755            want in this case.   Plus, the clean up is needed in both
756            cases.  */
757
758         /* DELAY HERE!
759            On a fast machine, the status might change before the interrupt
760            is given to the processor.  This means that the interrupt was
761            never detected, and autoirq_report fails to report anything.
762            This should fix autoirq_* problems.
763         */
764         SMC_DELAY();
765         SMC_DELAY();
766
767         /* and disable all interrupts again */
768         outb( 0, ioaddr + INT_MASK );
769
770         /* clear hardware interrupts again, because that's how it
771            was when I was called... */
772         cli();
773
774         /* and return what I found */
775         return probe_irq_off(cookie);
776 }
777
778 /*----------------------------------------------------------------------
779  . Function: smc_probe( int ioaddr )
780  .
781  . Purpose:
782  .      Tests to see if a given ioaddr points to an SMC9xxx chip.
783  .      Returns a 0 on success
784  .
785  . Algorithm:
786  .      (1) see if the high byte of BANK_SELECT is 0x33
787  .      (2) compare the ioaddr with the base register's address
788  .      (3) see if I recognize the chip ID in the appropriate register
789  .
790  .---------------------------------------------------------------------
791  */
792
793 /*---------------------------------------------------------------
794  . Here I do typical initialization tasks.
795  .
796  . o  Initialize the structure if needed
797  . o  print out my vanity message if not done so already
798  . o  print out what type of hardware is detected
799  . o  print out the ethernet address
800  . o  find the IRQ
801  . o  set up my private data
802  . o  configure the dev structure with my subroutines
803  . o  actually GRAB the irq.
804  . o  GRAB the region
805  .-----------------------------------------------------------------
806 */
807 static int __init smc_probe(struct net_device *dev, int ioaddr)
808 {
809         int i, memory, retval;
810         static unsigned version_printed;
811         unsigned int bank;
812
813         const char *version_string;
814         const char *if_string;
815
816         /* registers */
817         word revision_register;
818         word base_address_register;
819         word configuration_register;
820         word memory_info_register;
821         word memory_cfg_register;
822
823         /* Grab the region so that no one else tries to probe our ioports. */
824         if (!request_region(ioaddr, SMC_IO_EXTENT, dev->name))
825                 return -EBUSY;
826
827         /* First, see if the high byte is 0x33 */
828         bank = inw( ioaddr + BANK_SELECT );
829         if ( (bank & 0xFF00) != 0x3300 ) {
830                 retval = -ENODEV;
831                 goto err_out;
832         }
833         /* The above MIGHT indicate a device, but I need to write to further
834                 test this.  */
835         outw( 0x0, ioaddr + BANK_SELECT );
836         bank = inw( ioaddr + BANK_SELECT );
837         if ( (bank & 0xFF00 ) != 0x3300 ) {
838                 retval = -ENODEV;
839                 goto err_out;
840         }
841         /* well, we've already written once, so hopefully another time won't
842            hurt.  This time, I need to switch the bank register to bank 1,
843            so I can access the base address register */
844         SMC_SELECT_BANK(1);
845         base_address_register = inw( ioaddr + BASE );
846         if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) )  {
847                 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
848                         "Probably not a SMC chip\n",
849                         ioaddr, base_address_register >> 3 & 0x3E0 );
850                 /* well, the base address register didn't match.  Must not have
851                    been a SMC chip after all. */
852                 retval = -ENODEV;
853                 goto err_out;
854         }
855
856         /*  check if the revision register is something that I recognize.
857             These might need to be added to later, as future revisions
858             could be added.  */
859         SMC_SELECT_BANK(3);
860         revision_register  = inw( ioaddr + REVISION );
861         if ( !chip_ids[ ( revision_register  >> 4 ) & 0xF  ] ) {
862                 /* I don't recognize this chip, so... */
863                 printk(CARDNAME ": IO %x: Unrecognized revision register:"
864                         " %x, Contact author. \n", ioaddr, revision_register );
865
866                 retval = -ENODEV;
867                 goto err_out;
868         }
869
870         /* at this point I'll assume that the chip is an SMC9xxx.
871            It might be prudent to check a listing of MAC addresses
872            against the hardware address, or do some other tests. */
873
874         if (version_printed++ == 0)
875                 printk("%s", version);
876
877         /* fill in some of the fields */
878         dev->base_addr = ioaddr;
879
880         /*
881          . Get the MAC address ( bank 1, regs 4 - 9 )
882         */
883         SMC_SELECT_BANK( 1 );
884         for ( i = 0; i < 6; i += 2 ) {
885                 word    address;
886
887                 address = inw( ioaddr + ADDR0 + i  );
888                 dev->dev_addr[ i + 1] = address >> 8;
889                 dev->dev_addr[ i ] = address & 0xFF;
890         }
891
892         /* get the memory information */
893
894         SMC_SELECT_BANK( 0 );
895         memory_info_register = inw( ioaddr + MIR );
896         memory_cfg_register  = inw( ioaddr + MCR );
897         memory = ( memory_cfg_register >> 9 )  & 0x7;  /* multiplier */
898         memory *= 256 * ( memory_info_register & 0xFF );
899
900         /*
901          Now, I want to find out more about the chip.  This is sort of
902          redundant, but it's cleaner to have it in both, rather than having
903          one VERY long probe procedure.
904         */
905         SMC_SELECT_BANK(3);
906         revision_register  = inw( ioaddr + REVISION );
907         version_string = chip_ids[ ( revision_register  >> 4 ) & 0xF  ];
908         if ( !version_string ) {
909                 /* I shouldn't get here because this call was done before.... */
910                 retval = -ENODEV;
911                 goto err_out;
912         }
913
914         /* is it using AUI or 10BaseT ? */
915         if ( dev->if_port == 0 ) {
916                 SMC_SELECT_BANK(1);
917                 configuration_register = inw( ioaddr + CONFIG );
918                 if ( configuration_register & CFG_AUI_SELECT )
919                         dev->if_port = 2;
920                 else
921                         dev->if_port = 1;
922         }
923         if_string = interfaces[ dev->if_port - 1 ];
924
925         /* now, reset the chip, and put it into a known state */
926         smc_reset( ioaddr );
927
928         /*
929          . If dev->irq is 0, then the device has to be banged on to see
930          . what the IRQ is.
931          .
932          . This banging doesn't always detect the IRQ, for unknown reasons.
933          . a workaround is to reset the chip and try again.
934          .
935          . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
936          . be what is requested on the command line.   I don't do that, mostly
937          . because the card that I have uses a non-standard method of accessing
938          . the IRQs, and because this _should_ work in most configurations.
939          .
940          . Specifying an IRQ is done with the assumption that the user knows
941          . what (s)he is doing.  No checking is done!!!!
942          .
943         */
944         if ( dev->irq < 2 ) {
945                 int     trials;
946
947                 trials = 3;
948                 while ( trials-- ) {
949                         dev->irq = smc_findirq( ioaddr );
950                         if ( dev->irq )
951                                 break;
952                         /* kick the card and try again */
953                         smc_reset( ioaddr );
954                 }
955         }
956         if (dev->irq == 0 ) {
957                 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
958                 retval = -ENODEV;
959                 goto err_out;
960         }
961
962         /* now, print out the card info, in a short format.. */
963
964         printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
965                 version_string, revision_register & 0xF, ioaddr, dev->irq,
966                 if_string, memory );
967         /*
968          . Print the Ethernet address
969         */
970         printk("ADDR: ");
971         for (i = 0; i < 5; i++)
972                 printk("%2.2x:", dev->dev_addr[i] );
973         printk("%2.2x \n", dev->dev_addr[5] );
974
975
976         /* Initialize the private structure. */
977         if (dev->priv == NULL) {
978                 dev->priv = kmalloc(sizeof(struct smc_local), GFP_KERNEL);
979                 if (dev->priv == NULL) {
980                         retval = -ENOMEM;
981                         goto err_out;
982                 }
983         }
984         /* set the private data to zero by default */
985         memset(dev->priv, 0, sizeof(struct smc_local));
986
987         /* Fill in the fields of the device structure with ethernet values. */
988         ether_setup(dev);
989
990         /* Grab the IRQ */
991         retval = request_irq(dev->irq, &smc_interrupt, 0, dev->name, dev);
992         if (retval) {
993                 printk("%s: unable to get IRQ %d (irqval=%d).\n", dev->name,
994                         dev->irq, retval);
995                 kfree(dev->priv);
996                 dev->priv = NULL;
997                 goto err_out;
998         }
999
1000         dev->open                       = smc_open;
1001         dev->stop                       = smc_close;
1002         dev->hard_start_xmit            = smc_wait_to_send_packet;
1003         dev->tx_timeout                 = smc_timeout;
1004         dev->watchdog_timeo             = HZ/20;
1005         dev->get_stats                  = smc_query_statistics;
1006         dev->set_multicast_list         = smc_set_multicast_list;
1007
1008         return 0;
1009
1010 err_out:
1011         release_region(ioaddr, SMC_IO_EXTENT);
1012         return retval;
1013 }
1014
1015 #if SMC_DEBUG > 2
1016 static void print_packet( byte * buf, int length )
1017 {
1018 #if 0
1019         int i;
1020         int remainder;
1021         int lines;
1022
1023         printk("Packet of length %d \n", length );
1024         lines = length / 16;
1025         remainder = length % 16;
1026
1027         for ( i = 0; i < lines ; i ++ ) {
1028                 int cur;
1029
1030                 for ( cur = 0; cur < 8; cur ++ ) {
1031                         byte a, b;
1032
1033                         a = *(buf ++ );
1034                         b = *(buf ++ );
1035                         printk("%02x%02x ", a, b );
1036                 }
1037                 printk("\n");
1038         }
1039         for ( i = 0; i < remainder/2 ; i++ ) {
1040                 byte a, b;
1041
1042                 a = *(buf ++ );
1043                 b = *(buf ++ );
1044                 printk("%02x%02x ", a, b );
1045         }
1046         printk("\n");
1047 #endif
1048 }
1049 #endif
1050
1051
1052 /*
1053  * Open and Initialize the board
1054  *
1055  * Set up everything, reset the card, etc ..
1056  *
1057  */
1058 static int smc_open(struct net_device *dev)
1059 {
1060         int     ioaddr = dev->base_addr;
1061
1062         int     i;      /* used to set hw ethernet address */
1063
1064         /* clear out all the junk that was put here before... */
1065         memset(dev->priv, 0, sizeof(struct smc_local));
1066
1067         /* reset the hardware */
1068
1069         smc_reset( ioaddr );
1070         smc_enable( ioaddr );
1071
1072         /* Select which interface to use */
1073
1074         SMC_SELECT_BANK( 1 );
1075         if ( dev->if_port == 1 ) {
1076                 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1077                         ioaddr + CONFIG );
1078         }
1079         else if ( dev->if_port == 2 ) {
1080                 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1081                         ioaddr + CONFIG );
1082         }
1083
1084         /*
1085                 According to Becker, I have to set the hardware address
1086                 at this point, because the (l)user can set it with an
1087                 ioctl.  Easily done...
1088         */
1089         SMC_SELECT_BANK( 1 );
1090         for ( i = 0; i < 6; i += 2 ) {
1091                 word    address;
1092
1093                 address = dev->dev_addr[ i + 1 ] << 8 ;
1094                 address  |= dev->dev_addr[ i ];
1095                 outw( address, ioaddr + ADDR0 + i );
1096         }
1097         
1098         netif_start_queue(dev);
1099         return 0;
1100 }
1101
1102 /*--------------------------------------------------------
1103  . Called by the kernel to send a packet out into the void
1104  . of the net.  This routine is largely based on
1105  . skeleton.c, from Becker.
1106  .--------------------------------------------------------
1107 */
1108
1109 static void smc_timeout(struct net_device *dev)
1110 {
1111         /* If we get here, some higher level has decided we are broken.
1112            There should really be a "kick me" function call instead. */
1113         printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1114                 tx_done(dev) ? "IRQ conflict" :
1115                 "network cable problem");
1116         /* "kick" the adaptor */
1117         smc_reset( dev->base_addr );
1118         smc_enable( dev->base_addr );
1119         dev->trans_start = jiffies;
1120         /* clear anything saved */
1121         ((struct smc_local *)dev->priv)->saved_skb = NULL;
1122         netif_wake_queue(dev);
1123 }
1124
1125 /*--------------------------------------------------------------------
1126  .
1127  . This is the main routine of the driver, to handle the device when
1128  . it needs some attention.
1129  .
1130  . So:
1131  .   first, save state of the chipset
1132  .   branch off into routines to handle each case, and acknowledge
1133  .          each to the interrupt register
1134  .   and finally restore state.
1135  .
1136  ---------------------------------------------------------------------*/
1137
1138 static void smc_interrupt(int irq, void * dev_id,  struct pt_regs * regs)
1139 {
1140         struct net_device *dev  = dev_id;
1141         int ioaddr              = dev->base_addr;
1142         struct smc_local *lp    = (struct smc_local *)dev->priv;
1143
1144         byte    status;
1145         word    card_stats;
1146         byte    mask;
1147         int     timeout;
1148         /* state registers */
1149         word    saved_bank;
1150         word    saved_pointer;
1151
1152
1153
1154         PRINTK3((CARDNAME": SMC interrupt started \n"));
1155
1156         saved_bank = inw( ioaddr + BANK_SELECT );
1157
1158         SMC_SELECT_BANK(2);
1159         saved_pointer = inw( ioaddr + POINTER );
1160
1161         mask = inb( ioaddr + INT_MASK );
1162         /* clear all interrupts */
1163         outb( 0, ioaddr + INT_MASK );
1164
1165
1166         /* set a timeout value, so I don't stay here forever */
1167         timeout = 4;
1168
1169         PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1170         do {
1171                 /* read the status flag, and mask it */
1172                 status = inb( ioaddr + INTERRUPT ) & mask;
1173                 if (!status )
1174                         break;
1175
1176                 PRINTK3((KERN_WARNING CARDNAME
1177                         ": Handling interrupt status %x \n", status ));
1178
1179                 if (status & IM_RCV_INT) {
1180                         /* Got a packet(s). */
1181                         PRINTK2((KERN_WARNING CARDNAME
1182                                 ": Receive Interrupt\n"));
1183                         smc_rcv(dev);
1184                 } else if (status & IM_TX_INT ) {
1185                         PRINTK2((KERN_WARNING CARDNAME
1186                                 ": TX ERROR handled\n"));
1187                         smc_tx(dev);
1188                         outb(IM_TX_INT, ioaddr + INTERRUPT );
1189                 } else if (status & IM_TX_EMPTY_INT ) {
1190                         /* update stats */
1191                         SMC_SELECT_BANK( 0 );
1192                         card_stats = inw( ioaddr + COUNTER );
1193                         /* single collisions */
1194                         lp->stats.collisions += card_stats & 0xF;
1195                         card_stats >>= 4;
1196                         /* multiple collisions */
1197                         lp->stats.collisions += card_stats & 0xF;
1198
1199                         /* these are for when linux supports these statistics */
1200
1201                         SMC_SELECT_BANK( 2 );
1202                         PRINTK2((KERN_WARNING CARDNAME
1203                                 ": TX_BUFFER_EMPTY handled\n"));
1204                         outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1205                         mask &= ~IM_TX_EMPTY_INT;
1206                         lp->stats.tx_packets += lp->packets_waiting;
1207                         lp->packets_waiting = 0;
1208
1209                 } else if (status & IM_ALLOC_INT ) {
1210                         PRINTK2((KERN_DEBUG CARDNAME
1211                                 ": Allocation interrupt \n"));
1212                         /* clear this interrupt so it doesn't happen again */
1213                         mask &= ~IM_ALLOC_INT;
1214
1215                         smc_hardware_send_packet( dev );
1216
1217                         /* enable xmit interrupts based on this */
1218                         mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1219
1220                         /* and let the card send more packets to me */
1221                         netif_wake_queue(dev);
1222                         
1223                         PRINTK2((CARDNAME": Handoff done successfully.\n"));
1224                 } else if (status & IM_RX_OVRN_INT ) {
1225                         lp->stats.rx_errors++;
1226                         lp->stats.rx_fifo_errors++;
1227                         outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1228                 } else if (status & IM_EPH_INT ) {
1229                         PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1230                 } else if (status & IM_ERCV_INT ) {
1231                         PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1232                         outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1233                 }
1234         } while ( timeout -- );
1235
1236
1237         /* restore state register */
1238         SMC_SELECT_BANK( 2 );
1239         outb( mask, ioaddr + INT_MASK );
1240
1241         PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1242         outw( saved_pointer, ioaddr + POINTER );
1243
1244         SMC_SELECT_BANK( saved_bank );
1245
1246         PRINTK3((CARDNAME ": Interrupt done\n"));
1247         return;
1248 }
1249
1250 /*-------------------------------------------------------------
1251  .
1252  . smc_rcv -  receive a packet from the card
1253  .
1254  . There is ( at least ) a packet waiting to be read from
1255  . chip-memory.
1256  .
1257  . o Read the status
1258  . o If an error, record it
1259  . o otherwise, read in the packet
1260  --------------------------------------------------------------
1261 */
1262 static void smc_rcv(struct net_device *dev)
1263 {
1264         struct smc_local *lp = (struct smc_local *)dev->priv;
1265         int     ioaddr = dev->base_addr;
1266         int     packet_number;
1267         word    status;
1268         word    packet_length;
1269
1270         /* assume bank 2 */
1271
1272         packet_number = inw( ioaddr + FIFO_PORTS );
1273
1274         if ( packet_number & FP_RXEMPTY ) {
1275                 /* we got called , but nothing was on the FIFO */
1276                 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1277                 /* don't need to restore anything */
1278                 return;
1279         }
1280
1281         /*  start reading from the start of the packet */
1282         outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1283
1284         /* First two words are status and packet_length */
1285         status          = inw( ioaddr + DATA_1 );
1286         packet_length   = inw( ioaddr + DATA_1 );
1287
1288         packet_length &= 0x07ff;  /* mask off top bits */
1289
1290         PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1291         /*
1292          . the packet length contains 3 extra words :
1293          . status, length, and an extra word with an odd byte .
1294         */
1295         packet_length -= 6;
1296
1297         if ( !(status & RS_ERRORS ) ){
1298                 /* do stuff to make a new packet */
1299                 struct sk_buff  * skb;
1300                 byte            * data;
1301
1302                 /* read one extra byte */
1303                 if ( status & RS_ODDFRAME )
1304                         packet_length++;
1305
1306                 /* set multicast stats */
1307                 if ( status & RS_MULTICAST )
1308                         lp->stats.multicast++;
1309
1310                 skb = dev_alloc_skb( packet_length + 5);
1311
1312                 if ( skb == NULL ) {
1313                         printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
1314                         lp->stats.rx_dropped++;
1315                         goto done;
1316                 }
1317
1318                 /*
1319                  ! This should work without alignment, but it could be
1320                  ! in the worse case
1321                 */
1322
1323                 skb_reserve( skb, 2 );   /* 16 bit alignment */
1324
1325                 skb->dev = dev;
1326                 data = skb_put( skb, packet_length);
1327
1328 #ifdef USE_32_BIT
1329                 /* QUESTION:  Like in the TX routine, do I want
1330                    to send the DWORDs or the bytes first, or some
1331                    mixture.  A mixture might improve already slow PIO
1332                    performance  */
1333                 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1334                         packet_length >> 2, packet_length & 3 ));
1335                 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1336                 /* read the left over bytes */
1337                 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1338                         packet_length & 0x3  );
1339 #else
1340                 PRINTK3((" Reading %d words and %d byte(s) \n",
1341                         (packet_length >> 1 ), packet_length & 1 ));
1342                 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1343                 if ( packet_length & 1 ) {
1344                         data += packet_length & ~1;
1345                         *(data++) = inb( ioaddr + DATA_1 );
1346                 }
1347 #endif
1348 #if     SMC_DEBUG > 2
1349                         print_packet( data, packet_length );
1350 #endif
1351
1352                 skb->protocol = eth_type_trans(skb, dev );
1353                 netif_rx(skb);
1354                 dev->last_rx = jiffies;
1355                 lp->stats.rx_packets++;
1356                 lp->stats.rx_bytes += packet_length;
1357         } else {
1358                 /* error ... */
1359                 lp->stats.rx_errors++;
1360
1361                 if ( status & RS_ALGNERR )  lp->stats.rx_frame_errors++;
1362                 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1363                         lp->stats.rx_length_errors++;
1364                 if ( status & RS_BADCRC)        lp->stats.rx_crc_errors++;
1365         }
1366
1367 done:
1368         /*  error or good, tell the card to get rid of this packet */
1369         outw( MC_RELEASE, ioaddr + MMU_CMD );
1370 }
1371
1372
1373 /*************************************************************************
1374  . smc_tx
1375  .
1376  . Purpose:  Handle a transmit error message.   This will only be called
1377  .   when an error, because of the AUTO_RELEASE mode.
1378  .
1379  . Algorithm:
1380  .      Save pointer and packet no
1381  .      Get the packet no from the top of the queue
1382  .      check if it's valid ( if not, is this an error??? )
1383  .      read the status word
1384  .      record the error
1385  .      ( resend?  Not really, since we don't want old packets around )
1386  .      Restore saved values
1387  ************************************************************************/
1388 static void smc_tx( struct net_device * dev )
1389 {
1390         int     ioaddr = dev->base_addr;
1391         struct smc_local *lp = (struct smc_local *)dev->priv;
1392         byte saved_packet;
1393         byte packet_no;
1394         word tx_status;
1395
1396
1397         /* assume bank 2  */
1398
1399         saved_packet = inb( ioaddr + PNR_ARR );
1400         packet_no = inw( ioaddr + FIFO_PORTS );
1401         packet_no &= 0x7F;
1402
1403         /* select this as the packet to read from */
1404         outb( packet_no, ioaddr + PNR_ARR );
1405
1406         /* read the first word from this packet */
1407         outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1408
1409         tx_status = inw( ioaddr + DATA_1 );
1410         PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1411
1412         lp->stats.tx_errors++;
1413         if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
1414         if ( tx_status & TS_LATCOL  ) {
1415                 printk(KERN_DEBUG CARDNAME
1416                         ": Late collision occurred on last xmit.\n");
1417                 lp->stats.tx_window_errors++;
1418         }
1419 #if 0
1420                 if ( tx_status & TS_16COL ) { ... }
1421 #endif
1422
1423         if ( tx_status & TS_SUCCESS ) {
1424                 printk(CARDNAME": Successful packet caused interrupt \n");
1425         }
1426         /* re-enable transmit */
1427         SMC_SELECT_BANK( 0 );
1428         outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1429
1430         /* kill the packet */
1431         SMC_SELECT_BANK( 2 );
1432         outw( MC_FREEPKT, ioaddr + MMU_CMD );
1433
1434         /* one less packet waiting for me */
1435         lp->packets_waiting--;
1436
1437         outb( saved_packet, ioaddr + PNR_ARR );
1438         return;
1439 }
1440
1441 /*----------------------------------------------------
1442  . smc_close
1443  .
1444  . this makes the board clean up everything that it can
1445  . and not talk to the outside world.   Caused by
1446  . an 'ifconfig ethX down'
1447  .
1448  -----------------------------------------------------*/
1449 static int smc_close(struct net_device *dev)
1450 {
1451         netif_stop_queue(dev);
1452         /* clear everything */
1453         smc_shutdown( dev->base_addr );
1454
1455         /* Update the statistics here. */
1456         return 0;
1457 }
1458
1459 /*------------------------------------------------------------
1460  . Get the current statistics.
1461  . This may be called with the card open or closed.
1462  .-------------------------------------------------------------*/
1463 static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
1464         struct smc_local *lp = (struct smc_local *)dev->priv;
1465
1466         return &lp->stats;
1467 }
1468
1469 /*-----------------------------------------------------------
1470  . smc_set_multicast_list
1471  .
1472  . This routine will, depending on the values passed to it,
1473  . either make it accept multicast packets, go into
1474  . promiscuous mode ( for TCPDUMP and cousins ) or accept
1475  . a select set of multicast packets
1476 */
1477 static void smc_set_multicast_list(struct net_device *dev)
1478 {
1479         short ioaddr = dev->base_addr;
1480
1481         SMC_SELECT_BANK(0);
1482         if ( dev->flags & IFF_PROMISC )
1483                 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1484
1485 /* BUG?  I never disable promiscuous mode if multicasting was turned on.
1486    Now, I turn off promiscuous mode, but I don't do anything to multicasting
1487    when promiscuous mode is turned on.
1488 */
1489
1490         /* Here, I am setting this to accept all multicast packets.
1491            I don't need to zero the multicast table, because the flag is
1492            checked before the table is
1493         */
1494         else if (dev->flags & IFF_ALLMULTI)
1495                 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1496
1497         /* We just get all multicast packets even if we only want them
1498          . from one source.  This will be changed at some future
1499          . point. */
1500         else if (dev->mc_count )  {
1501                 /* support hardware multicasting */
1502
1503                 /* be sure I get rid of flags I might have set */
1504                 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1505                         ioaddr + RCR );
1506                 /* NOTE: this has to set the bank, so make sure it is the
1507                    last thing called.  The bank is set to zero at the top */
1508                 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1509         }
1510         else  {
1511                 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1512                         ioaddr + RCR );
1513
1514                 /*
1515                   since I'm disabling all multicast entirely, I need to
1516                   clear the multicast list
1517                 */
1518                 SMC_SELECT_BANK( 3 );
1519                 outw( 0, ioaddr + MULTICAST1 );
1520                 outw( 0, ioaddr + MULTICAST2 );
1521                 outw( 0, ioaddr + MULTICAST3 );
1522                 outw( 0, ioaddr + MULTICAST4 );
1523         }
1524 }
1525
1526 #ifdef MODULE
1527
1528 static struct net_device devSMC9194;
1529 static int io;
1530 static int irq;
1531 static int ifport;
1532 MODULE_LICENSE("GPL");
1533
1534 MODULE_PARM(io, "i");
1535 MODULE_PARM(irq, "i");
1536 MODULE_PARM(ifport, "i");
1537 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1538 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1539 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1540
1541 int init_module(void)
1542 {
1543         int result;
1544
1545         if (io == 0)
1546                 printk(KERN_WARNING
1547                 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1548
1549         /* copy the parameters from insmod into the device structure */
1550         devSMC9194.base_addr = io;
1551         devSMC9194.irq       = irq;
1552         devSMC9194.if_port      = ifport;
1553         devSMC9194.init         = smc_init;
1554         if ((result = register_netdev(&devSMC9194)) != 0)
1555                 return result;
1556
1557         return 0;
1558 }
1559
1560 void cleanup_module(void)
1561 {
1562         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1563         unregister_netdev(&devSMC9194);
1564
1565         free_irq(devSMC9194.irq, &devSMC9194);
1566         release_region(devSMC9194.base_addr, SMC_IO_EXTENT);
1567
1568         if (devSMC9194.priv)
1569                 kfree(devSMC9194.priv);
1570 }
1571
1572 #endif /* MODULE */