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