Merge to Fedora kernel-2.6.7-1.441
[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 #define DRV_NAME "smc9194"
82
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 struct net_device *smc_init(int unit);
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 irqreturn_t 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 = netdev_priv(dev);
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 = skb->len;
489
490         if (length < ETH_ZLEN) {
491                 skb = skb_padto(skb, ETH_ZLEN);
492                 if (skb == NULL) {
493                         netif_wake_queue(dev);
494                         return 0;
495                 }
496                 length = ETH_ZLEN;
497         }
498                 
499         /*
500         ** The MMU wants the number of pages to be the number of 256 bytes
501         ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
502         **
503         ** Pkt size for allocating is data length +6 (for additional status words,
504         ** length and ctl!) If odd size last byte is included in this header.
505         */
506         numPages =  ((length & 0xfffe) + 6) / 256;
507
508         if (numPages > 7 ) {
509                 printk(CARDNAME": Far too big packet error. \n");
510                 /* freeing the packet is a good thing here... but should
511                  . any packets of this size get down here?   */
512                 dev_kfree_skb (skb);
513                 lp->saved_skb = NULL;
514                 /* this IS an error, but, i don't want the skb saved */
515                 netif_wake_queue(dev);
516                 return 0;
517         }
518         /* either way, a packet is waiting now */
519         lp->packets_waiting++;
520
521         /* now, try to allocate the memory */
522         SMC_SELECT_BANK( 2 );
523         outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
524         /*
525         . Performance Hack
526         .
527         . wait a short amount of time.. if I can send a packet now, I send
528         . it now.  Otherwise, I enable an interrupt and wait for one to be
529         . available.
530         .
531         . I could have handled this a slightly different way, by checking to
532         . see if any memory was available in the FREE MEMORY register.  However,
533         . either way, I need to generate an allocation, and the allocation works
534         . no matter what, so I saw no point in checking free memory.
535         */
536         time_out = MEMORY_WAIT_TIME;
537         do {
538                 word    status;
539
540                 status = inb( ioaddr + INTERRUPT );
541                 if ( status & IM_ALLOC_INT ) {
542                         /* acknowledge the interrupt */
543                         outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
544                         break;
545                 }
546         } while ( -- time_out );
547
548         if ( !time_out ) {
549                 /* oh well, wait until the chip finds memory later */
550                 SMC_ENABLE_INT( IM_ALLOC_INT );
551                 PRINTK2((CARDNAME": memory allocation deferred. \n"));
552                 /* it's deferred, but I'll handle it later */
553                 return 0;
554         }
555         /* or YES! I can send the packet now.. */
556         smc_hardware_send_packet(dev);
557         netif_wake_queue(dev);
558         return 0;
559 }
560
561 /*
562  . Function:  smc_hardware_send_packet(struct net_device * )
563  . Purpose:
564  .      This sends the actual packet to the SMC9xxx chip.
565  .
566  . Algorithm:
567  .      First, see if a saved_skb is available.
568  .              ( this should NOT be called if there is no 'saved_skb'
569  .      Now, find the packet number that the chip allocated
570  .      Point the data pointers at it in memory
571  .      Set the length word in the chip's memory
572  .      Dump the packet to chip memory
573  .      Check if a last byte is needed ( odd length packet )
574  .              if so, set the control flag right
575  .      Tell the card to send it
576  .      Enable the transmit interrupt, so I know if it failed
577  .      Free the kernel data if I actually sent it.
578 */
579 static void smc_hardware_send_packet( struct net_device * dev )
580 {
581         struct smc_local *lp = netdev_priv(dev);
582         byte                    packet_no;
583         struct sk_buff *        skb = lp->saved_skb;
584         word                    length;
585         unsigned short          ioaddr;
586         byte                    * buf;
587
588         ioaddr = dev->base_addr;
589
590         if ( !skb ) {
591                 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
592                 return;
593         }
594         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
595         buf = skb->data;
596
597         /* If I get here, I _know_ there is a packet slot waiting for me */
598         packet_no = inb( ioaddr + PNR_ARR + 1 );
599         if ( packet_no & 0x80 ) {
600                 /* or isn't there?  BAD CHIP! */
601                 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
602                 dev_kfree_skb_any(skb);
603                 lp->saved_skb = NULL;
604                 netif_wake_queue(dev);
605                 return;
606         }
607
608         /* we have a packet address, so tell the card to use it */
609         outb( packet_no, ioaddr + PNR_ARR );
610
611         /* point to the beginning of the packet */
612         outw( PTR_AUTOINC , ioaddr + POINTER );
613
614         PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
615 #if SMC_DEBUG > 2
616         print_packet( buf, length );
617 #endif
618
619         /* send the packet length ( +6 for status, length and ctl byte )
620            and the status word ( set to zeros ) */
621 #ifdef USE_32_BIT
622         outl(  (length +6 ) << 16 , ioaddr + DATA_1 );
623 #else
624         outw( 0, ioaddr + DATA_1 );
625         /* send the packet length ( +6 for status words, length, and ctl*/
626         outb( (length+6) & 0xFF,ioaddr + DATA_1 );
627         outb( (length+6) >> 8 , ioaddr + DATA_1 );
628 #endif
629
630         /* send the actual data
631          . I _think_ it's faster to send the longs first, and then
632          . mop up by sending the last word.  It depends heavily
633          . on alignment, at least on the 486.  Maybe it would be
634          . a good idea to check which is optimal?  But that could take
635          . almost as much time as is saved?
636         */
637 #ifdef USE_32_BIT
638         if ( length & 0x2  ) {
639                 outsl(ioaddr + DATA_1, buf,  length >> 2 );
640                 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
641         }
642         else
643                 outsl(ioaddr + DATA_1, buf,  length >> 2 );
644 #else
645         outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
646 #endif
647         /* Send the last byte, if there is one.   */
648
649         if ( (length & 1) == 0 ) {
650                 outw( 0, ioaddr + DATA_1 );
651         } else {
652                 outb( buf[length -1 ], ioaddr + DATA_1 );
653                 outb( 0x20, ioaddr + DATA_1);
654         }
655
656         /* enable the interrupts */
657         SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
658
659         /* and let the chipset deal with it */
660         outw( MC_ENQUEUE , ioaddr + MMU_CMD );
661
662         PRINTK2((CARDNAME": Sent packet of length %d \n",length));
663
664         lp->saved_skb = NULL;
665         dev_kfree_skb_any (skb);
666
667         dev->trans_start = jiffies;
668
669         /* we can send another packet */
670         netif_wake_queue(dev);
671
672         return;
673 }
674
675 /*-------------------------------------------------------------------------
676  |
677  | smc_init(int unit)
678  |   Input parameters:
679  |      dev->base_addr == 0, try to find all possible locations
680  |      dev->base_addr == 1, return failure code
681  |      dev->base_addr == 2, always allocate space,  and return success
682  |      dev->base_addr == <anything else>   this is the address to check
683  |
684  |   Output:
685  |      pointer to net_device or ERR_PTR(error)
686  |
687  ---------------------------------------------------------------------------
688 */
689 static int io;
690 static int irq;
691 static int ifport;
692
693 struct net_device * __init smc_init(int unit)
694 {
695         struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
696         unsigned *port;
697         int err = 0;
698
699         if (!dev)
700                 return ERR_PTR(-ENODEV);
701
702         if (unit >= 0) {
703                 sprintf(dev->name, "eth%d", unit);
704                 netdev_boot_setup_check(dev);
705                 io = dev->base_addr;
706                 irq = dev->irq;
707         }
708
709         SET_MODULE_OWNER(dev);
710
711         if (io > 0x1ff) {       /* Check a single specified location. */
712                 err = smc_probe(dev, io);
713         } else if (io != 0) {   /* Don't probe at all. */
714                 err = -ENXIO;
715         } else {
716                 for (port = smc_portlist; *port; port++) {
717                         if (smc_probe(dev, *port) == 0)
718                                 break;
719                 }
720                 if (!*port)
721                         err = -ENODEV;
722         }
723         if (err)
724                 goto out;
725         err = register_netdev(dev);
726         if (err)
727                 goto out1;
728         return dev;
729 out1:
730         free_irq(dev->irq, dev);
731         release_region(dev->base_addr, SMC_IO_EXTENT);
732 out:
733         free_netdev(dev);
734         return ERR_PTR(err);
735 }
736
737 /*----------------------------------------------------------------------
738  . smc_findirq
739  .
740  . This routine has a simple purpose -- make the SMC chip generate an
741  . interrupt, so an auto-detect routine can detect it, and find the IRQ,
742  ------------------------------------------------------------------------
743 */
744 int __init smc_findirq( int ioaddr )
745 {
746         int     timeout = 20;
747         unsigned long cookie;
748
749
750         cookie = probe_irq_on();
751
752         /*
753          * What I try to do here is trigger an ALLOC_INT. This is done
754          * by allocating a small chunk of memory, which will give an interrupt
755          * when done.
756          */
757
758
759         SMC_SELECT_BANK(2);
760         /* enable ALLOCation interrupts ONLY */
761         outb( IM_ALLOC_INT, ioaddr + INT_MASK );
762
763         /*
764          . Allocate 512 bytes of memory.  Note that the chip was just
765          . reset so all the memory is available
766         */
767         outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
768
769         /*
770          . Wait until positive that the interrupt has been generated
771         */
772         while ( timeout ) {
773                 byte    int_status;
774
775                 int_status = inb( ioaddr + INTERRUPT );
776
777                 if ( int_status & IM_ALLOC_INT )
778                         break;          /* got the interrupt */
779                 timeout--;
780         }
781         /* there is really nothing that I can do here if timeout fails,
782            as probe_irq_off will return a 0 anyway, which is what I
783            want in this case.   Plus, the clean up is needed in both
784            cases.  */
785
786         /* DELAY HERE!
787            On a fast machine, the status might change before the interrupt
788            is given to the processor.  This means that the interrupt was
789            never detected, and probe_irq_off fails to report anything.
790            This should fix probe_irq_* problems.
791         */
792         SMC_DELAY();
793         SMC_DELAY();
794
795         /* and disable all interrupts again */
796         outb( 0, ioaddr + INT_MASK );
797
798         /* and return what I found */
799         return probe_irq_off(cookie);
800 }
801
802 /*----------------------------------------------------------------------
803  . Function: smc_probe( int ioaddr )
804  .
805  . Purpose:
806  .      Tests to see if a given ioaddr points to an SMC9xxx chip.
807  .      Returns a 0 on success
808  .
809  . Algorithm:
810  .      (1) see if the high byte of BANK_SELECT is 0x33
811  .      (2) compare the ioaddr with the base register's address
812  .      (3) see if I recognize the chip ID in the appropriate register
813  .
814  .---------------------------------------------------------------------
815  */
816
817 /*---------------------------------------------------------------
818  . Here I do typical initialization tasks.
819  .
820  . o  Initialize the structure if needed
821  . o  print out my vanity message if not done so already
822  . o  print out what type of hardware is detected
823  . o  print out the ethernet address
824  . o  find the IRQ
825  . o  set up my private data
826  . o  configure the dev structure with my subroutines
827  . o  actually GRAB the irq.
828  . o  GRAB the region
829  .-----------------------------------------------------------------
830 */
831 static int __init smc_probe(struct net_device *dev, int ioaddr)
832 {
833         int i, memory, retval;
834         static unsigned version_printed;
835         unsigned int bank;
836
837         const char *version_string;
838         const char *if_string;
839
840         /* registers */
841         word revision_register;
842         word base_address_register;
843         word configuration_register;
844         word memory_info_register;
845         word memory_cfg_register;
846
847         /* Grab the region so that no one else tries to probe our ioports. */
848         if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
849                 return -EBUSY;
850
851         dev->irq = irq;
852         dev->if_port = ifport;
853
854         /* First, see if the high byte is 0x33 */
855         bank = inw( ioaddr + BANK_SELECT );
856         if ( (bank & 0xFF00) != 0x3300 ) {
857                 retval = -ENODEV;
858                 goto err_out;
859         }
860         /* The above MIGHT indicate a device, but I need to write to further
861                 test this.  */
862         outw( 0x0, ioaddr + BANK_SELECT );
863         bank = inw( ioaddr + BANK_SELECT );
864         if ( (bank & 0xFF00 ) != 0x3300 ) {
865                 retval = -ENODEV;
866                 goto err_out;
867         }
868         /* well, we've already written once, so hopefully another time won't
869            hurt.  This time, I need to switch the bank register to bank 1,
870            so I can access the base address register */
871         SMC_SELECT_BANK(1);
872         base_address_register = inw( ioaddr + BASE );
873         if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) )  {
874                 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
875                         "Probably not a SMC chip\n",
876                         ioaddr, base_address_register >> 3 & 0x3E0 );
877                 /* well, the base address register didn't match.  Must not have
878                    been a SMC chip after all. */
879                 retval = -ENODEV;
880                 goto err_out;
881         }
882
883         /*  check if the revision register is something that I recognize.
884             These might need to be added to later, as future revisions
885             could be added.  */
886         SMC_SELECT_BANK(3);
887         revision_register  = inw( ioaddr + REVISION );
888         if ( !chip_ids[ ( revision_register  >> 4 ) & 0xF  ] ) {
889                 /* I don't recognize this chip, so... */
890                 printk(CARDNAME ": IO %x: Unrecognized revision register:"
891                         " %x, Contact author. \n", ioaddr, revision_register );
892
893                 retval = -ENODEV;
894                 goto err_out;
895         }
896
897         /* at this point I'll assume that the chip is an SMC9xxx.
898            It might be prudent to check a listing of MAC addresses
899            against the hardware address, or do some other tests. */
900
901         if (version_printed++ == 0)
902                 printk("%s", version);
903
904         /* fill in some of the fields */
905         dev->base_addr = ioaddr;
906
907         /*
908          . Get the MAC address ( bank 1, regs 4 - 9 )
909         */
910         SMC_SELECT_BANK( 1 );
911         for ( i = 0; i < 6; i += 2 ) {
912                 word    address;
913
914                 address = inw( ioaddr + ADDR0 + i  );
915                 dev->dev_addr[ i + 1] = address >> 8;
916                 dev->dev_addr[ i ] = address & 0xFF;
917         }
918
919         /* get the memory information */
920
921         SMC_SELECT_BANK( 0 );
922         memory_info_register = inw( ioaddr + MIR );
923         memory_cfg_register  = inw( ioaddr + MCR );
924         memory = ( memory_cfg_register >> 9 )  & 0x7;  /* multiplier */
925         memory *= 256 * ( memory_info_register & 0xFF );
926
927         /*
928          Now, I want to find out more about the chip.  This is sort of
929          redundant, but it's cleaner to have it in both, rather than having
930          one VERY long probe procedure.
931         */
932         SMC_SELECT_BANK(3);
933         revision_register  = inw( ioaddr + REVISION );
934         version_string = chip_ids[ ( revision_register  >> 4 ) & 0xF  ];
935         if ( !version_string ) {
936                 /* I shouldn't get here because this call was done before.... */
937                 retval = -ENODEV;
938                 goto err_out;
939         }
940
941         /* is it using AUI or 10BaseT ? */
942         if ( dev->if_port == 0 ) {
943                 SMC_SELECT_BANK(1);
944                 configuration_register = inw( ioaddr + CONFIG );
945                 if ( configuration_register & CFG_AUI_SELECT )
946                         dev->if_port = 2;
947                 else
948                         dev->if_port = 1;
949         }
950         if_string = interfaces[ dev->if_port - 1 ];
951
952         /* now, reset the chip, and put it into a known state */
953         smc_reset( ioaddr );
954
955         /*
956          . If dev->irq is 0, then the device has to be banged on to see
957          . what the IRQ is.
958          .
959          . This banging doesn't always detect the IRQ, for unknown reasons.
960          . a workaround is to reset the chip and try again.
961          .
962          . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
963          . be what is requested on the command line.   I don't do that, mostly
964          . because the card that I have uses a non-standard method of accessing
965          . the IRQs, and because this _should_ work in most configurations.
966          .
967          . Specifying an IRQ is done with the assumption that the user knows
968          . what (s)he is doing.  No checking is done!!!!
969          .
970         */
971         if ( dev->irq < 2 ) {
972                 int     trials;
973
974                 trials = 3;
975                 while ( trials-- ) {
976                         dev->irq = smc_findirq( ioaddr );
977                         if ( dev->irq )
978                                 break;
979                         /* kick the card and try again */
980                         smc_reset( ioaddr );
981                 }
982         }
983         if (dev->irq == 0 ) {
984                 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
985                 retval = -ENODEV;
986                 goto err_out;
987         }
988
989         /* now, print out the card info, in a short format.. */
990
991         printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
992                 version_string, revision_register & 0xF, ioaddr, dev->irq,
993                 if_string, memory );
994         /*
995          . Print the Ethernet address
996         */
997         printk("ADDR: ");
998         for (i = 0; i < 5; i++)
999                 printk("%2.2x:", dev->dev_addr[i] );
1000         printk("%2.2x \n", dev->dev_addr[5] );
1001
1002         /* set the private data to zero by default */
1003         memset(dev->priv, 0, sizeof(struct smc_local));
1004
1005         /* Grab the IRQ */
1006         retval = request_irq(dev->irq, &smc_interrupt, 0, DRV_NAME, dev);
1007         if (retval) {
1008                 printk("%s: unable to get IRQ %d (irqval=%d).\n", DRV_NAME,
1009                         dev->irq, retval);
1010                 goto err_out;
1011         }
1012
1013         dev->open                       = smc_open;
1014         dev->stop                       = smc_close;
1015         dev->hard_start_xmit            = smc_wait_to_send_packet;
1016         dev->tx_timeout                 = smc_timeout;
1017         dev->watchdog_timeo             = HZ/20;
1018         dev->get_stats                  = smc_query_statistics;
1019         dev->set_multicast_list         = smc_set_multicast_list;
1020
1021         return 0;
1022
1023 err_out:
1024         release_region(ioaddr, SMC_IO_EXTENT);
1025         return retval;
1026 }
1027
1028 #if SMC_DEBUG > 2
1029 static void print_packet( byte * buf, int length )
1030 {
1031 #if 0
1032         int i;
1033         int remainder;
1034         int lines;
1035
1036         printk("Packet of length %d \n", length );
1037         lines = length / 16;
1038         remainder = length % 16;
1039
1040         for ( i = 0; i < lines ; i ++ ) {
1041                 int cur;
1042
1043                 for ( cur = 0; cur < 8; cur ++ ) {
1044                         byte a, b;
1045
1046                         a = *(buf ++ );
1047                         b = *(buf ++ );
1048                         printk("%02x%02x ", a, b );
1049                 }
1050                 printk("\n");
1051         }
1052         for ( i = 0; i < remainder/2 ; i++ ) {
1053                 byte a, b;
1054
1055                 a = *(buf ++ );
1056                 b = *(buf ++ );
1057                 printk("%02x%02x ", a, b );
1058         }
1059         printk("\n");
1060 #endif
1061 }
1062 #endif
1063
1064
1065 /*
1066  * Open and Initialize the board
1067  *
1068  * Set up everything, reset the card, etc ..
1069  *
1070  */
1071 static int smc_open(struct net_device *dev)
1072 {
1073         int     ioaddr = dev->base_addr;
1074
1075         int     i;      /* used to set hw ethernet address */
1076
1077         /* clear out all the junk that was put here before... */
1078         memset(dev->priv, 0, sizeof(struct smc_local));
1079
1080         /* reset the hardware */
1081
1082         smc_reset( ioaddr );
1083         smc_enable( ioaddr );
1084
1085         /* Select which interface to use */
1086
1087         SMC_SELECT_BANK( 1 );
1088         if ( dev->if_port == 1 ) {
1089                 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1090                         ioaddr + CONFIG );
1091         }
1092         else if ( dev->if_port == 2 ) {
1093                 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1094                         ioaddr + CONFIG );
1095         }
1096
1097         /*
1098                 According to Becker, I have to set the hardware address
1099                 at this point, because the (l)user can set it with an
1100                 ioctl.  Easily done...
1101         */
1102         SMC_SELECT_BANK( 1 );
1103         for ( i = 0; i < 6; i += 2 ) {
1104                 word    address;
1105
1106                 address = dev->dev_addr[ i + 1 ] << 8 ;
1107                 address  |= dev->dev_addr[ i ];
1108                 outw( address, ioaddr + ADDR0 + i );
1109         }
1110         
1111         netif_start_queue(dev);
1112         return 0;
1113 }
1114
1115 /*--------------------------------------------------------
1116  . Called by the kernel to send a packet out into the void
1117  . of the net.  This routine is largely based on
1118  . skeleton.c, from Becker.
1119  .--------------------------------------------------------
1120 */
1121
1122 static void smc_timeout(struct net_device *dev)
1123 {
1124         /* If we get here, some higher level has decided we are broken.
1125            There should really be a "kick me" function call instead. */
1126         printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1127                 tx_done(dev) ? "IRQ conflict" :
1128                 "network cable problem");
1129         /* "kick" the adaptor */
1130         smc_reset( dev->base_addr );
1131         smc_enable( dev->base_addr );
1132         dev->trans_start = jiffies;
1133         /* clear anything saved */
1134         ((struct smc_local *)dev->priv)->saved_skb = NULL;
1135         netif_wake_queue(dev);
1136 }
1137
1138 /*--------------------------------------------------------------------
1139  .
1140  . This is the main routine of the driver, to handle the device when
1141  . it needs some attention.
1142  .
1143  . So:
1144  .   first, save state of the chipset
1145  .   branch off into routines to handle each case, and acknowledge
1146  .          each to the interrupt register
1147  .   and finally restore state.
1148  .
1149  ---------------------------------------------------------------------*/
1150
1151 static irqreturn_t smc_interrupt(int irq, void * dev_id,  struct pt_regs * regs)
1152 {
1153         struct net_device *dev  = dev_id;
1154         int ioaddr              = dev->base_addr;
1155         struct smc_local *lp = netdev_priv(dev);
1156
1157         byte    status;
1158         word    card_stats;
1159         byte    mask;
1160         int     timeout;
1161         /* state registers */
1162         word    saved_bank;
1163         word    saved_pointer;
1164         int handled = 0;
1165
1166
1167         PRINTK3((CARDNAME": SMC interrupt started \n"));
1168
1169         saved_bank = inw( ioaddr + BANK_SELECT );
1170
1171         SMC_SELECT_BANK(2);
1172         saved_pointer = inw( ioaddr + POINTER );
1173
1174         mask = inb( ioaddr + INT_MASK );
1175         /* clear all interrupts */
1176         outb( 0, ioaddr + INT_MASK );
1177
1178
1179         /* set a timeout value, so I don't stay here forever */
1180         timeout = 4;
1181
1182         PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1183         do {
1184                 /* read the status flag, and mask it */
1185                 status = inb( ioaddr + INTERRUPT ) & mask;
1186                 if (!status )
1187                         break;
1188
1189                 handled = 1;
1190
1191                 PRINTK3((KERN_WARNING CARDNAME
1192                         ": Handling interrupt status %x \n", status ));
1193
1194                 if (status & IM_RCV_INT) {
1195                         /* Got a packet(s). */
1196                         PRINTK2((KERN_WARNING CARDNAME
1197                                 ": Receive Interrupt\n"));
1198                         smc_rcv(dev);
1199                 } else if (status & IM_TX_INT ) {
1200                         PRINTK2((KERN_WARNING CARDNAME
1201                                 ": TX ERROR handled\n"));
1202                         smc_tx(dev);
1203                         outb(IM_TX_INT, ioaddr + INTERRUPT );
1204                 } else if (status & IM_TX_EMPTY_INT ) {
1205                         /* update stats */
1206                         SMC_SELECT_BANK( 0 );
1207                         card_stats = inw( ioaddr + COUNTER );
1208                         /* single collisions */
1209                         lp->stats.collisions += card_stats & 0xF;
1210                         card_stats >>= 4;
1211                         /* multiple collisions */
1212                         lp->stats.collisions += card_stats & 0xF;
1213
1214                         /* these are for when linux supports these statistics */
1215
1216                         SMC_SELECT_BANK( 2 );
1217                         PRINTK2((KERN_WARNING CARDNAME
1218                                 ": TX_BUFFER_EMPTY handled\n"));
1219                         outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1220                         mask &= ~IM_TX_EMPTY_INT;
1221                         lp->stats.tx_packets += lp->packets_waiting;
1222                         lp->packets_waiting = 0;
1223
1224                 } else if (status & IM_ALLOC_INT ) {
1225                         PRINTK2((KERN_DEBUG CARDNAME
1226                                 ": Allocation interrupt \n"));
1227                         /* clear this interrupt so it doesn't happen again */
1228                         mask &= ~IM_ALLOC_INT;
1229
1230                         smc_hardware_send_packet( dev );
1231
1232                         /* enable xmit interrupts based on this */
1233                         mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1234
1235                         /* and let the card send more packets to me */
1236                         netif_wake_queue(dev);
1237                         
1238                         PRINTK2((CARDNAME": Handoff done successfully.\n"));
1239                 } else if (status & IM_RX_OVRN_INT ) {
1240                         lp->stats.rx_errors++;
1241                         lp->stats.rx_fifo_errors++;
1242                         outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1243                 } else if (status & IM_EPH_INT ) {
1244                         PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1245                 } else if (status & IM_ERCV_INT ) {
1246                         PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1247                         outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1248                 }
1249         } while ( timeout -- );
1250
1251
1252         /* restore state register */
1253         SMC_SELECT_BANK( 2 );
1254         outb( mask, ioaddr + INT_MASK );
1255
1256         PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1257         outw( saved_pointer, ioaddr + POINTER );
1258
1259         SMC_SELECT_BANK( saved_bank );
1260
1261         PRINTK3((CARDNAME ": Interrupt done\n"));
1262         return IRQ_RETVAL(handled);
1263 }
1264
1265 /*-------------------------------------------------------------
1266  .
1267  . smc_rcv -  receive a packet from the card
1268  .
1269  . There is ( at least ) a packet waiting to be read from
1270  . chip-memory.
1271  .
1272  . o Read the status
1273  . o If an error, record it
1274  . o otherwise, read in the packet
1275  --------------------------------------------------------------
1276 */
1277 static void smc_rcv(struct net_device *dev)
1278 {
1279         struct smc_local *lp = netdev_priv(dev);
1280         int     ioaddr = dev->base_addr;
1281         int     packet_number;
1282         word    status;
1283         word    packet_length;
1284
1285         /* assume bank 2 */
1286
1287         packet_number = inw( ioaddr + FIFO_PORTS );
1288
1289         if ( packet_number & FP_RXEMPTY ) {
1290                 /* we got called , but nothing was on the FIFO */
1291                 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1292                 /* don't need to restore anything */
1293                 return;
1294         }
1295
1296         /*  start reading from the start of the packet */
1297         outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1298
1299         /* First two words are status and packet_length */
1300         status          = inw( ioaddr + DATA_1 );
1301         packet_length   = inw( ioaddr + DATA_1 );
1302
1303         packet_length &= 0x07ff;  /* mask off top bits */
1304
1305         PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1306         /*
1307          . the packet length contains 3 extra words :
1308          . status, length, and an extra word with an odd byte .
1309         */
1310         packet_length -= 6;
1311
1312         if ( !(status & RS_ERRORS ) ){
1313                 /* do stuff to make a new packet */
1314                 struct sk_buff  * skb;
1315                 byte            * data;
1316
1317                 /* read one extra byte */
1318                 if ( status & RS_ODDFRAME )
1319                         packet_length++;
1320
1321                 /* set multicast stats */
1322                 if ( status & RS_MULTICAST )
1323                         lp->stats.multicast++;
1324
1325                 skb = dev_alloc_skb( packet_length + 5);
1326
1327                 if ( skb == NULL ) {
1328                         printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
1329                         lp->stats.rx_dropped++;
1330                         goto done;
1331                 }
1332
1333                 /*
1334                  ! This should work without alignment, but it could be
1335                  ! in the worse case
1336                 */
1337
1338                 skb_reserve( skb, 2 );   /* 16 bit alignment */
1339
1340                 skb->dev = dev;
1341                 data = skb_put( skb, packet_length);
1342
1343 #ifdef USE_32_BIT
1344                 /* QUESTION:  Like in the TX routine, do I want
1345                    to send the DWORDs or the bytes first, or some
1346                    mixture.  A mixture might improve already slow PIO
1347                    performance  */
1348                 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1349                         packet_length >> 2, packet_length & 3 ));
1350                 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1351                 /* read the left over bytes */
1352                 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1353                         packet_length & 0x3  );
1354 #else
1355                 PRINTK3((" Reading %d words and %d byte(s) \n",
1356                         (packet_length >> 1 ), packet_length & 1 ));
1357                 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1358                 if ( packet_length & 1 ) {
1359                         data += packet_length & ~1;
1360                         *(data++) = inb( ioaddr + DATA_1 );
1361                 }
1362 #endif
1363 #if     SMC_DEBUG > 2
1364                         print_packet( data, packet_length );
1365 #endif
1366
1367                 skb->protocol = eth_type_trans(skb, dev );
1368                 netif_rx(skb);
1369                 dev->last_rx = jiffies;
1370                 lp->stats.rx_packets++;
1371                 lp->stats.rx_bytes += packet_length;
1372         } else {
1373                 /* error ... */
1374                 lp->stats.rx_errors++;
1375
1376                 if ( status & RS_ALGNERR )  lp->stats.rx_frame_errors++;
1377                 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1378                         lp->stats.rx_length_errors++;
1379                 if ( status & RS_BADCRC)        lp->stats.rx_crc_errors++;
1380         }
1381
1382 done:
1383         /*  error or good, tell the card to get rid of this packet */
1384         outw( MC_RELEASE, ioaddr + MMU_CMD );
1385 }
1386
1387
1388 /*************************************************************************
1389  . smc_tx
1390  .
1391  . Purpose:  Handle a transmit error message.   This will only be called
1392  .   when an error, because of the AUTO_RELEASE mode.
1393  .
1394  . Algorithm:
1395  .      Save pointer and packet no
1396  .      Get the packet no from the top of the queue
1397  .      check if it's valid ( if not, is this an error??? )
1398  .      read the status word
1399  .      record the error
1400  .      ( resend?  Not really, since we don't want old packets around )
1401  .      Restore saved values
1402  ************************************************************************/
1403 static void smc_tx( struct net_device * dev )
1404 {
1405         int     ioaddr = dev->base_addr;
1406         struct smc_local *lp = netdev_priv(dev);
1407         byte saved_packet;
1408         byte packet_no;
1409         word tx_status;
1410
1411
1412         /* assume bank 2  */
1413
1414         saved_packet = inb( ioaddr + PNR_ARR );
1415         packet_no = inw( ioaddr + FIFO_PORTS );
1416         packet_no &= 0x7F;
1417
1418         /* select this as the packet to read from */
1419         outb( packet_no, ioaddr + PNR_ARR );
1420
1421         /* read the first word from this packet */
1422         outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1423
1424         tx_status = inw( ioaddr + DATA_1 );
1425         PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1426
1427         lp->stats.tx_errors++;
1428         if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
1429         if ( tx_status & TS_LATCOL  ) {
1430                 printk(KERN_DEBUG CARDNAME
1431                         ": Late collision occurred on last xmit.\n");
1432                 lp->stats.tx_window_errors++;
1433         }
1434 #if 0
1435                 if ( tx_status & TS_16COL ) { ... }
1436 #endif
1437
1438         if ( tx_status & TS_SUCCESS ) {
1439                 printk(CARDNAME": Successful packet caused interrupt \n");
1440         }
1441         /* re-enable transmit */
1442         SMC_SELECT_BANK( 0 );
1443         outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1444
1445         /* kill the packet */
1446         SMC_SELECT_BANK( 2 );
1447         outw( MC_FREEPKT, ioaddr + MMU_CMD );
1448
1449         /* one less packet waiting for me */
1450         lp->packets_waiting--;
1451
1452         outb( saved_packet, ioaddr + PNR_ARR );
1453         return;
1454 }
1455
1456 /*----------------------------------------------------
1457  . smc_close
1458  .
1459  . this makes the board clean up everything that it can
1460  . and not talk to the outside world.   Caused by
1461  . an 'ifconfig ethX down'
1462  .
1463  -----------------------------------------------------*/
1464 static int smc_close(struct net_device *dev)
1465 {
1466         netif_stop_queue(dev);
1467         /* clear everything */
1468         smc_shutdown( dev->base_addr );
1469
1470         /* Update the statistics here. */
1471         return 0;
1472 }
1473
1474 /*------------------------------------------------------------
1475  . Get the current statistics.
1476  . This may be called with the card open or closed.
1477  .-------------------------------------------------------------*/
1478 static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
1479         struct smc_local *lp = netdev_priv(dev);
1480
1481         return &lp->stats;
1482 }
1483
1484 /*-----------------------------------------------------------
1485  . smc_set_multicast_list
1486  .
1487  . This routine will, depending on the values passed to it,
1488  . either make it accept multicast packets, go into
1489  . promiscuous mode ( for TCPDUMP and cousins ) or accept
1490  . a select set of multicast packets
1491 */
1492 static void smc_set_multicast_list(struct net_device *dev)
1493 {
1494         short ioaddr = dev->base_addr;
1495
1496         SMC_SELECT_BANK(0);
1497         if ( dev->flags & IFF_PROMISC )
1498                 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1499
1500 /* BUG?  I never disable promiscuous mode if multicasting was turned on.
1501    Now, I turn off promiscuous mode, but I don't do anything to multicasting
1502    when promiscuous mode is turned on.
1503 */
1504
1505         /* Here, I am setting this to accept all multicast packets.
1506            I don't need to zero the multicast table, because the flag is
1507            checked before the table is
1508         */
1509         else if (dev->flags & IFF_ALLMULTI)
1510                 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1511
1512         /* We just get all multicast packets even if we only want them
1513          . from one source.  This will be changed at some future
1514          . point. */
1515         else if (dev->mc_count )  {
1516                 /* support hardware multicasting */
1517
1518                 /* be sure I get rid of flags I might have set */
1519                 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1520                         ioaddr + RCR );
1521                 /* NOTE: this has to set the bank, so make sure it is the
1522                    last thing called.  The bank is set to zero at the top */
1523                 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1524         }
1525         else  {
1526                 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1527                         ioaddr + RCR );
1528
1529                 /*
1530                   since I'm disabling all multicast entirely, I need to
1531                   clear the multicast list
1532                 */
1533                 SMC_SELECT_BANK( 3 );
1534                 outw( 0, ioaddr + MULTICAST1 );
1535                 outw( 0, ioaddr + MULTICAST2 );
1536                 outw( 0, ioaddr + MULTICAST3 );
1537                 outw( 0, ioaddr + MULTICAST4 );
1538         }
1539 }
1540
1541 #ifdef MODULE
1542
1543 static struct net_device *devSMC9194;
1544 MODULE_LICENSE("GPL");
1545
1546 MODULE_PARM(io, "i");
1547 MODULE_PARM(irq, "i");
1548 MODULE_PARM(ifport, "i");
1549 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1550 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1551 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1552
1553 int init_module(void)
1554 {
1555         if (io == 0)
1556                 printk(KERN_WARNING
1557                 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1558
1559         /* copy the parameters from insmod into the device structure */
1560         devSMC9194 = smc_init(-1);
1561         if (IS_ERR(devSMC9194))
1562                 return PTR_ERR(devSMC9194);
1563         return 0;
1564 }
1565
1566 void cleanup_module(void)
1567 {
1568         unregister_netdev(devSMC9194);
1569         free_irq(devSMC9194->irq, devSMC9194);
1570         release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1571         free_netdev(devSMC9194);
1572 }
1573
1574 #endif /* MODULE */