Back to home page

OSCL-LXR

 
 

    


0001 /*------------------------------------------------------------------------
0002  . smc9194.c
0003  . This is a driver for SMC's 9000 series of Ethernet cards.
0004  .
0005  . Copyright (C) 1996 by Erik Stahlman
0006  . This software may be used and distributed according to the terms
0007  . of the GNU General Public License, incorporated herein by reference.
0008  .
0009  . "Features" of the SMC chip:
0010  .   4608 byte packet memory. ( for the 91C92.  Others have more )
0011  .   EEPROM for configuration
0012  .   AUI/TP selection  ( mine has 10Base2/10BaseT select )
0013  .
0014  . Arguments:
0015  .  io       = for the base address
0016  .  irq  = for the IRQ
0017  .  ifport = 0 for autodetect, 1 for TP, 2 for AUI ( or 10base2 )
0018  .
0019  . author:
0020  .  Erik Stahlman               ( erik@vt.edu )
0021  . contributors:
0022  .      Arnaldo Carvalho de Melo <acme@conectiva.com.br>
0023  .
0024  . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
0025  .
0026  . Sources:
0027  .    o   SMC databook
0028  .    o   skeleton.c by Donald Becker ( becker@scyld.com )
0029  .    o   ( a LOT of advice from Becker as well )
0030  .
0031  . History:
0032  .  12/07/95  Erik Stahlman  written, got receive/xmit handled
0033  .  01/03/96  Erik Stahlman  worked out some bugs, actually usable!!! :-)
0034  .  01/06/96  Erik Stahlman  cleaned up some, better testing, etc
0035  .  01/29/96  Erik Stahlman  fixed autoirq, added multicast
0036  .  02/01/96  Erik Stahlman  1. disabled all interrupts in smc_reset
0037  .               2. got rid of post-decrementing bug -- UGH.
0038  .  02/13/96  Erik Stahlman  Tried to fix autoirq failure.  Added more
0039  .               descriptive error messages.
0040  .  02/15/96  Erik Stahlman  Fixed typo that caused detection failure
0041  .  02/23/96  Erik Stahlman  Modified it to fit into kernel tree
0042  .               Added support to change hardware address
0043  .               Cleared stats on opens
0044  .  02/26/96  Erik Stahlman  Trial support for Kernel 1.2.13
0045  .               Kludge for automatic IRQ detection
0046  .  03/04/96  Erik Stahlman  Fixed kernel 1.3.70 +
0047  .               Fixed bug reported by Gardner Buchanan in
0048  .                 smc_enable, with outw instead of outb
0049  .  03/06/96  Erik Stahlman  Added hardware multicast from Peter Cammaert
0050  .  04/14/00  Heiko Pruessing (SMA Regelsysteme)  Fixed bug in chip memory
0051  .               allocation
0052  .      08/20/00  Arnaldo Melo   fix kfree(skb) in smc_hardware_send_packet
0053  .      12/15/00  Christian Jullien fix "Warning: kfree_skb on hard IRQ"
0054  .      11/08/01 Matt Domsch     Use common crc32 function
0055  ----------------------------------------------------------------------------*/
0056 
0057 static const char version[] =
0058     "smc9194.c:v0.14 12/15/00 by Erik Stahlman (erik@vt.edu)";
0059 
0060 #include <linux/module.h>
0061 #include <linux/kernel.h>
0062 #include <linux/types.h>
0063 #include <linux/fcntl.h>
0064 #include <linux/interrupt.h>
0065 #include <linux/ioport.h>
0066 #include <linux/in.h>
0067 #include <linux/string.h>
0068 #include <linux/init.h>
0069 #include <linux/crc32.h>
0070 #include <linux/errno.h>
0071 #include <linux/netdevice.h>
0072 #include <linux/etherdevice.h>
0073 #include <linux/skbuff.h>
0074 #include <linux/bitops.h>
0075 
0076 #include <asm/io.h>
0077 
0078 #include "smc9194.h"
0079 
0080 #define DRV_NAME "smc9194"
0081 
0082 /*------------------------------------------------------------------------
0083  .
0084  . Configuration options, for the experienced user to change.
0085  .
0086  -------------------------------------------------------------------------*/
0087 
0088 /*
0089  . Do you want to use 32 bit xfers?  This should work on all chips, as
0090  . the chipset is designed to accommodate them.
0091 */
0092 #ifdef __sh__
0093 #undef USE_32_BIT
0094 #else
0095 #define USE_32_BIT 1
0096 #endif
0097 
0098 /*
0099  .the SMC9194 can be at any of the following port addresses.  To change,
0100  .for a slightly different card, you can add it to the array.  Keep in
0101  .mind that the array must end in zero.
0102 */
0103 
0104 struct devlist {
0105     unsigned int port;
0106     unsigned int irq;
0107 };
0108 
0109 static struct devlist smc_devlist[] __initdata = {
0110     {.port = 0x200, .irq = 0},
0111     {.port = 0x220, .irq = 0},
0112     {.port = 0x240, .irq = 0},
0113     {.port = 0x260, .irq = 0},
0114     {.port = 0x280, .irq = 0},
0115     {.port = 0x2A0, .irq = 0},
0116     {.port = 0x2C0, .irq = 0},
0117     {.port = 0x2E0, .irq = 0},
0118     {.port = 0x300, .irq = 0},
0119     {.port = 0x320, .irq = 0},
0120     {.port = 0x340, .irq = 0},
0121     {.port = 0x360, .irq = 0},
0122     {.port = 0x380, .irq = 0},
0123     {.port = 0x3A0, .irq = 0},
0124     {.port = 0x3C0, .irq = 0},
0125     {.port = 0x3E0, .irq = 0},
0126     {.port = 0,     .irq = 0},
0127 };
0128 /*
0129  . Wait time for memory to be free.  This probably shouldn't be
0130  . tuned that much, as waiting for this means nothing else happens
0131  . in the system
0132 */
0133 #define MEMORY_WAIT_TIME 16
0134 
0135 /*
0136  . DEBUGGING LEVELS
0137  .
0138  . 0 for normal operation
0139  . 1 for slightly more details
0140  . >2 for various levels of increasingly useless information
0141  .    2 for interrupt tracking, status flags
0142  .    3 for packet dumps, etc.
0143 */
0144 #define SMC_DEBUG 0
0145 
0146 #if (SMC_DEBUG > 2 )
0147 #define PRINTK3(x) printk x
0148 #else
0149 #define PRINTK3(x)
0150 #endif
0151 
0152 #if SMC_DEBUG > 1
0153 #define PRINTK2(x) printk x
0154 #else
0155 #define PRINTK2(x)
0156 #endif
0157 
0158 #ifdef SMC_DEBUG
0159 #define PRINTK(x) printk x
0160 #else
0161 #define PRINTK(x)
0162 #endif
0163 
0164 
0165 /*------------------------------------------------------------------------
0166  .
0167  . The internal workings of the driver.  If you are changing anything
0168  . here with the SMC stuff, you should have the datasheet and known
0169  . what you are doing.
0170  .
0171  -------------------------------------------------------------------------*/
0172 #define CARDNAME "SMC9194"
0173 
0174 
0175 /* store this information for the driver.. */
0176 struct smc_local {
0177     /*
0178        If I have to wait until memory is available to send
0179        a packet, I will store the skbuff here, until I get the
0180        desired memory.  Then, I'll send it out and free it.
0181     */
0182     struct sk_buff * saved_skb;
0183 
0184     /*
0185      . This keeps track of how many packets that I have
0186      . sent out.  When an TX_EMPTY interrupt comes, I know
0187      . that all of these have been sent.
0188     */
0189     int packets_waiting;
0190 };
0191 
0192 
0193 /*-----------------------------------------------------------------
0194  .
0195  .  The driver can be entered at any of the following entry points.
0196  .
0197  .------------------------------------------------------------------  */
0198 
0199 /*
0200  . This is called by  register_netdev().  It is responsible for
0201  . checking the portlist for the SMC9000 series chipset.  If it finds
0202  . one, then it will initialize the device, find the hardware information,
0203  . and sets up the appropriate device parameters.
0204  . NOTE: Interrupts are *OFF* when this procedure is called.
0205  .
0206  . NB:This shouldn't be static since it is referred to externally.
0207 */
0208 struct net_device *smc_init(int unit);
0209 
0210 /*
0211  . The kernel calls this function when someone wants to use the device,
0212  . typically 'ifconfig ethX up'.
0213 */
0214 static int smc_open(struct net_device *dev);
0215 
0216 /*
0217  . Our watchdog timed out. Called by the networking layer
0218 */
0219 static void smc_timeout(struct net_device *dev, unsigned int txqueue);
0220 
0221 /*
0222  . This is called by the kernel in response to 'ifconfig ethX down'.  It
0223  . is responsible for cleaning up everything that the open routine
0224  . does, and maybe putting the card into a powerdown state.
0225 */
0226 static int smc_close(struct net_device *dev);
0227 
0228 /*
0229  . Finally, a call to set promiscuous mode ( for TCPDUMP and related
0230  . programs ) and multicast modes.
0231 */
0232 static void smc_set_multicast_list(struct net_device *dev);
0233 
0234 
0235 /*---------------------------------------------------------------
0236  .
0237  . Interrupt level calls..
0238  .
0239  ----------------------------------------------------------------*/
0240 
0241 /*
0242  . Handles the actual interrupt
0243 */
0244 static irqreturn_t smc_interrupt(int irq, void *);
0245 /*
0246  . This is a separate procedure to handle the receipt of a packet, to
0247  . leave the interrupt code looking slightly cleaner
0248 */
0249 static inline void smc_rcv( struct net_device *dev );
0250 /*
0251  . This handles a TX interrupt, which is only called when an error
0252  . relating to a packet is sent.
0253 */
0254 static inline void smc_tx( struct net_device * dev );
0255 
0256 /*
0257  ------------------------------------------------------------
0258  .
0259  . Internal routines
0260  .
0261  ------------------------------------------------------------
0262 */
0263 
0264 /*
0265  . Test if a given location contains a chip, trying to cause as
0266  . little damage as possible if it's not a SMC chip.
0267 */
0268 static int smc_probe(struct net_device *dev, int ioaddr);
0269 
0270 /*
0271  . A rather simple routine to print out a packet for debugging purposes.
0272 */
0273 #if SMC_DEBUG > 2
0274 static void print_packet( byte *, int );
0275 #endif
0276 
0277 #define tx_done(dev) 1
0278 
0279 /* this is called to actually send the packet to the chip */
0280 static void smc_hardware_send_packet( struct net_device * dev );
0281 
0282 /* Since I am not sure if I will have enough room in the chip's ram
0283  . to store the packet, I call this routine, which either sends it
0284  . now, or generates an interrupt when the card is ready for the
0285  . packet */
0286 static netdev_tx_t  smc_wait_to_send_packet( struct sk_buff * skb,
0287                          struct net_device *dev );
0288 
0289 /* this does a soft reset on the device */
0290 static void smc_reset( int ioaddr );
0291 
0292 /* Enable Interrupts, Receive, and Transmit */
0293 static void smc_enable( int ioaddr );
0294 
0295 /* this puts the device in an inactive state */
0296 static void smc_shutdown( int ioaddr );
0297 
0298 /* This routine will find the IRQ of the driver if one is not
0299  . specified in the input to the device.  */
0300 static int smc_findirq( int ioaddr );
0301 
0302 /*
0303  . Function: smc_reset( int ioaddr )
0304  . Purpose:
0305  .      This sets the SMC91xx chip to its normal state, hopefully from whatever
0306  .  mess that any other DOS driver has put it in.
0307  .
0308  . Maybe I should reset more registers to defaults in here?  SOFTRESET  should
0309  . do that for me.
0310  .
0311  . Method:
0312  .  1.  send a SOFT RESET
0313  .  2.  wait for it to finish
0314  .  3.  enable autorelease mode
0315  .  4.  reset the memory management unit
0316  .  5.  clear all interrupts
0317  .
0318 */
0319 static void smc_reset( int ioaddr )
0320 {
0321     /* This resets the registers mostly to defaults, but doesn't
0322        affect EEPROM.  That seems unnecessary */
0323     SMC_SELECT_BANK( 0 );
0324     outw( RCR_SOFTRESET, ioaddr + RCR );
0325 
0326     /* this should pause enough for the chip to be happy */
0327     SMC_DELAY( );
0328 
0329     /* Set the transmit and receive configuration registers to
0330        default values */
0331     outw( RCR_CLEAR, ioaddr + RCR );
0332     outw( TCR_CLEAR, ioaddr + TCR );
0333 
0334     /* set the control register to automatically
0335        release successfully transmitted packets, to make the best
0336        use out of our limited memory */
0337     SMC_SELECT_BANK( 1 );
0338     outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
0339 
0340     /* Reset the MMU */
0341     SMC_SELECT_BANK( 2 );
0342     outw( MC_RESET, ioaddr + MMU_CMD );
0343 
0344     /* Note:  It doesn't seem that waiting for the MMU busy is needed here,
0345        but this is a place where future chipsets _COULD_ break.  Be wary
0346        of issuing another MMU command right after this */
0347 
0348     outb( 0, ioaddr + INT_MASK );
0349 }
0350 
0351 /*
0352  . Function: smc_enable
0353  . Purpose: let the chip talk to the outside work
0354  . Method:
0355  .  1.  Enable the transmitter
0356  .  2.  Enable the receiver
0357  .  3.  Enable interrupts
0358 */
0359 static void smc_enable( int ioaddr )
0360 {
0361     SMC_SELECT_BANK( 0 );
0362     /* see the header file for options in TCR/RCR NORMAL*/
0363     outw( TCR_NORMAL, ioaddr + TCR );
0364     outw( RCR_NORMAL, ioaddr + RCR );
0365 
0366     /* now, enable interrupts */
0367     SMC_SELECT_BANK( 2 );
0368     outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
0369 }
0370 
0371 /*
0372  . Function: smc_shutdown
0373  . Purpose:  closes down the SMC91xxx chip.
0374  . Method:
0375  .  1. zero the interrupt mask
0376  .  2. clear the enable receive flag
0377  .  3. clear the enable xmit flags
0378  .
0379  . TODO:
0380  .   (1) maybe utilize power down mode.
0381  .  Why not yet?  Because while the chip will go into power down mode,
0382  .  the manual says that it will wake up in response to any I/O requests
0383  .  in the register space.   Empirical results do not show this working.
0384 */
0385 static void smc_shutdown( int ioaddr )
0386 {
0387     /* no more interrupts for me */
0388     SMC_SELECT_BANK( 2 );
0389     outb( 0, ioaddr + INT_MASK );
0390 
0391     /* and tell the card to stay away from that nasty outside world */
0392     SMC_SELECT_BANK( 0 );
0393     outb( RCR_CLEAR, ioaddr + RCR );
0394     outb( TCR_CLEAR, ioaddr + TCR );
0395 #if 0
0396     /* finally, shut the chip down */
0397     SMC_SELECT_BANK( 1 );
0398     outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL  );
0399 #endif
0400 }
0401 
0402 
0403 /*
0404  . Function: smc_setmulticast( int ioaddr, struct net_device *dev )
0405  . Purpose:
0406  .    This sets the internal hardware table to filter out unwanted multicast
0407  .    packets before they take up memory.
0408  .
0409  .    The SMC chip uses a hash table where the high 6 bits of the CRC of
0410  .    address are the offset into the table.  If that bit is 1, then the
0411  .    multicast packet is accepted.  Otherwise, it's dropped silently.
0412  .
0413  .    To use the 6 bits as an offset into the table, the high 3 bits are the
0414  .    number of the 8 bit register, while the low 3 bits are the bit within
0415  .    that register.
0416  .
0417  . This routine is based very heavily on the one provided by Peter Cammaert.
0418 */
0419 
0420 
0421 static void smc_setmulticast(int ioaddr, struct net_device *dev)
0422 {
0423     int         i;
0424     unsigned char       multicast_table[ 8 ];
0425     struct netdev_hw_addr *ha;
0426     /* table for flipping the order of 3 bits */
0427     unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
0428 
0429     /* start with a table of all zeros: reject all */
0430     memset( multicast_table, 0, sizeof( multicast_table ) );
0431 
0432     netdev_for_each_mc_addr(ha, dev) {
0433         int position;
0434 
0435         /* only use the low order bits */
0436         position = ether_crc_le(6, ha->addr) & 0x3f;
0437 
0438         /* do some messy swapping to put the bit in the right spot */
0439         multicast_table[invert3[position&7]] |=
0440                     (1<<invert3[(position>>3)&7]);
0441 
0442     }
0443     /* now, the table can be loaded into the chipset */
0444     SMC_SELECT_BANK( 3 );
0445 
0446     for ( i = 0; i < 8 ; i++ ) {
0447         outb( multicast_table[i], ioaddr + MULTICAST1 + i );
0448     }
0449 }
0450 
0451 /*
0452  . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
0453  . Purpose:
0454  .    Attempt to allocate memory for a packet, if chip-memory is not
0455  .    available, then tell the card to generate an interrupt when it
0456  .    is available.
0457  .
0458  . Algorithm:
0459  .
0460  . o    if the saved_skb is not currently null, then drop this packet
0461  .  on the floor.  This should never happen, because of TBUSY.
0462  . o    if the saved_skb is null, then replace it with the current packet,
0463  . o    See if I can sending it now.
0464  . o    (NO): Enable interrupts and let the interrupt handler deal with it.
0465  . o    (YES):Send it now.
0466 */
0467 static netdev_tx_t smc_wait_to_send_packet(struct sk_buff *skb,
0468                        struct net_device *dev)
0469 {
0470     struct smc_local *lp = netdev_priv(dev);
0471     unsigned int ioaddr     = dev->base_addr;
0472     word            length;
0473     unsigned short      numPages;
0474     word            time_out;
0475 
0476     netif_stop_queue(dev);
0477     /* Well, I want to send the packet.. but I don't know
0478        if I can send it right now...  */
0479 
0480     if ( lp->saved_skb) {
0481         /* THIS SHOULD NEVER HAPPEN. */
0482         dev->stats.tx_aborted_errors++;
0483         printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
0484         return NETDEV_TX_BUSY;
0485     }
0486     lp->saved_skb = skb;
0487 
0488     length = skb->len;
0489 
0490     if (length < ETH_ZLEN) {
0491         if (skb_padto(skb, ETH_ZLEN)) {
0492             netif_wake_queue(dev);
0493             return NETDEV_TX_OK;
0494         }
0495         length = ETH_ZLEN;
0496     }
0497 
0498     /*
0499     ** The MMU wants the number of pages to be the number of 256 bytes
0500     ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
0501     **
0502     ** Pkt size for allocating is data length +6 (for additional status words,
0503     ** length and ctl!) If odd size last byte is included in this header.
0504     */
0505     numPages =  ((length & 0xfffe) + 6) / 256;
0506 
0507     if (numPages > 7 ) {
0508         printk(CARDNAME": Far too big packet error.\n");
0509         /* freeing the packet is a good thing here... but should
0510          . any packets of this size get down here?   */
0511         dev_kfree_skb (skb);
0512         lp->saved_skb = NULL;
0513         /* this IS an error, but, i don't want the skb saved */
0514         netif_wake_queue(dev);
0515         return NETDEV_TX_OK;
0516     }
0517     /* either way, a packet is waiting now */
0518     lp->packets_waiting++;
0519 
0520     /* now, try to allocate the memory */
0521     SMC_SELECT_BANK( 2 );
0522     outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
0523     /*
0524     . Performance Hack
0525     .
0526     . wait a short amount of time.. if I can send a packet now, I send
0527     . it now.  Otherwise, I enable an interrupt and wait for one to be
0528     . available.
0529     .
0530     . I could have handled this a slightly different way, by checking to
0531     . see if any memory was available in the FREE MEMORY register.  However,
0532     . either way, I need to generate an allocation, and the allocation works
0533     . no matter what, so I saw no point in checking free memory.
0534     */
0535     time_out = MEMORY_WAIT_TIME;
0536     do {
0537         word    status;
0538 
0539         status = inb( ioaddr + INTERRUPT );
0540         if ( status & IM_ALLOC_INT ) {
0541             /* acknowledge the interrupt */
0542             outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
0543             break;
0544         }
0545     } while ( -- time_out );
0546 
0547     if ( !time_out ) {
0548         /* oh well, wait until the chip finds memory later */
0549         SMC_ENABLE_INT( IM_ALLOC_INT );
0550         PRINTK2((CARDNAME": memory allocation deferred.\n"));
0551         /* it's deferred, but I'll handle it later */
0552         return NETDEV_TX_OK;
0553     }
0554     /* or YES! I can send the packet now.. */
0555     smc_hardware_send_packet(dev);
0556     netif_wake_queue(dev);
0557     return NETDEV_TX_OK;
0558 }
0559 
0560 /*
0561  . Function:  smc_hardware_send_packet(struct net_device * )
0562  . Purpose:
0563  .  This sends the actual packet to the SMC9xxx chip.
0564  .
0565  . Algorithm:
0566  .  First, see if a saved_skb is available.
0567  .      ( this should NOT be called if there is no 'saved_skb'
0568  .  Now, find the packet number that the chip allocated
0569  .  Point the data pointers at it in memory
0570  .  Set the length word in the chip's memory
0571  .  Dump the packet to chip memory
0572  .  Check if a last byte is needed ( odd length packet )
0573  .      if so, set the control flag right
0574  .  Tell the card to send it
0575  .  Enable the transmit interrupt, so I know if it failed
0576  .  Free the kernel data if I actually sent it.
0577 */
0578 static void smc_hardware_send_packet( struct net_device * dev )
0579 {
0580     struct smc_local *lp = netdev_priv(dev);
0581     byte            packet_no;
0582     struct sk_buff *    skb = lp->saved_skb;
0583     word            length;
0584     unsigned int        ioaddr;
0585     byte            * buf;
0586 
0587     ioaddr = dev->base_addr;
0588 
0589     if ( !skb ) {
0590         PRINTK((CARDNAME": In XMIT with no packet to send\n"));
0591         return;
0592     }
0593     length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
0594     buf = skb->data;
0595 
0596     /* If I get here, I _know_ there is a packet slot waiting for me */
0597     packet_no = inb( ioaddr + PNR_ARR + 1 );
0598     if ( packet_no & 0x80 ) {
0599         /* or isn't there?  BAD CHIP! */
0600         netdev_dbg(dev, CARDNAME": Memory allocation failed.\n");
0601         dev_kfree_skb_any(skb);
0602         lp->saved_skb = NULL;
0603         netif_wake_queue(dev);
0604         return;
0605     }
0606 
0607     /* we have a packet address, so tell the card to use it */
0608     outb( packet_no, ioaddr + PNR_ARR );
0609 
0610     /* point to the beginning of the packet */
0611     outw( PTR_AUTOINC , ioaddr + POINTER );
0612 
0613     PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length));
0614 #if SMC_DEBUG > 2
0615     print_packet( buf, length );
0616 #endif
0617 
0618     /* send the packet length ( +6 for status, length and ctl byte )
0619        and the status word ( set to zeros ) */
0620 #ifdef USE_32_BIT
0621     outl(  (length +6 ) << 16 , ioaddr + DATA_1 );
0622 #else
0623     outw( 0, ioaddr + DATA_1 );
0624     /* send the packet length ( +6 for status words, length, and ctl*/
0625     outb( (length+6) & 0xFF,ioaddr + DATA_1 );
0626     outb( (length+6) >> 8 , ioaddr + DATA_1 );
0627 #endif
0628 
0629     /* send the actual data
0630      . I _think_ it's faster to send the longs first, and then
0631      . mop up by sending the last word.  It depends heavily
0632      . on alignment, at least on the 486.  Maybe it would be
0633      . a good idea to check which is optimal?  But that could take
0634      . almost as much time as is saved?
0635     */
0636 #ifdef USE_32_BIT
0637     if ( length & 0x2  ) {
0638         outsl(ioaddr + DATA_1, buf,  length >> 2 );
0639         outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
0640     }
0641     else
0642         outsl(ioaddr + DATA_1, buf,  length >> 2 );
0643 #else
0644     outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
0645 #endif
0646     /* Send the last byte, if there is one.   */
0647 
0648     if ( (length & 1) == 0 ) {
0649         outw( 0, ioaddr + DATA_1 );
0650     } else {
0651         outb( buf[length -1 ], ioaddr + DATA_1 );
0652         outb( 0x20, ioaddr + DATA_1);
0653     }
0654 
0655     /* enable the interrupts */
0656     SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
0657 
0658     /* and let the chipset deal with it */
0659     outw( MC_ENQUEUE , ioaddr + MMU_CMD );
0660 
0661     PRINTK2((CARDNAME": Sent packet of length %d\n", length));
0662 
0663     lp->saved_skb = NULL;
0664     dev_kfree_skb_any (skb);
0665 
0666     netif_trans_update(dev);
0667 
0668     /* we can send another packet */
0669     netif_wake_queue(dev);
0670 }
0671 
0672 /*-------------------------------------------------------------------------
0673  |
0674  | smc_init(int unit)
0675  |   Input parameters:
0676  |  dev->base_addr == 0, try to find all possible locations
0677  |  dev->base_addr == 1, return failure code
0678  |  dev->base_addr == 2, always allocate space,  and return success
0679  |  dev->base_addr == <anything else>   this is the address to check
0680  |
0681  |   Output:
0682  |  pointer to net_device or ERR_PTR(error)
0683  |
0684  ---------------------------------------------------------------------------
0685 */
0686 static int io;
0687 static int irq;
0688 static int ifport;
0689 
0690 struct net_device * __init smc_init(int unit)
0691 {
0692     struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
0693     struct devlist *smcdev = smc_devlist;
0694     int err = 0;
0695 
0696     if (!dev)
0697         return ERR_PTR(-ENODEV);
0698 
0699     if (unit >= 0) {
0700         sprintf(dev->name, "eth%d", unit);
0701         netdev_boot_setup_check(dev);
0702         io = dev->base_addr;
0703         irq = dev->irq;
0704     }
0705 
0706     if (io > 0x1ff) {   /* Check a single specified location. */
0707         err = smc_probe(dev, io);
0708     } else if (io != 0) {   /* Don't probe at all. */
0709         err = -ENXIO;
0710     } else {
0711         for (;smcdev->port; smcdev++) {
0712             if (smc_probe(dev, smcdev->port) == 0)
0713                 break;
0714         }
0715         if (!smcdev->port)
0716             err = -ENODEV;
0717     }
0718     if (err)
0719         goto out;
0720     err = register_netdev(dev);
0721     if (err)
0722         goto out1;
0723     return dev;
0724 out1:
0725     free_irq(dev->irq, dev);
0726     release_region(dev->base_addr, SMC_IO_EXTENT);
0727 out:
0728     free_netdev(dev);
0729     return ERR_PTR(err);
0730 }
0731 
0732 /*----------------------------------------------------------------------
0733  . smc_findirq
0734  .
0735  . This routine has a simple purpose -- make the SMC chip generate an
0736  . interrupt, so an auto-detect routine can detect it, and find the IRQ,
0737  ------------------------------------------------------------------------
0738 */
0739 static int __init smc_findirq(int ioaddr)
0740 {
0741 #ifndef NO_AUTOPROBE
0742     int timeout = 20;
0743     unsigned long cookie;
0744 
0745 
0746     cookie = probe_irq_on();
0747 
0748     /*
0749      * What I try to do here is trigger an ALLOC_INT. This is done
0750      * by allocating a small chunk of memory, which will give an interrupt
0751      * when done.
0752      */
0753 
0754 
0755     SMC_SELECT_BANK(2);
0756     /* enable ALLOCation interrupts ONLY */
0757     outb( IM_ALLOC_INT, ioaddr + INT_MASK );
0758 
0759     /*
0760      . Allocate 512 bytes of memory.  Note that the chip was just
0761      . reset so all the memory is available
0762     */
0763     outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
0764 
0765     /*
0766      . Wait until positive that the interrupt has been generated
0767     */
0768     while ( timeout ) {
0769         byte    int_status;
0770 
0771         int_status = inb( ioaddr + INTERRUPT );
0772 
0773         if ( int_status & IM_ALLOC_INT )
0774             break;      /* got the interrupt */
0775         timeout--;
0776     }
0777     /* there is really nothing that I can do here if timeout fails,
0778        as probe_irq_off will return a 0 anyway, which is what I
0779        want in this case.   Plus, the clean up is needed in both
0780        cases.  */
0781 
0782     /* DELAY HERE!
0783        On a fast machine, the status might change before the interrupt
0784        is given to the processor.  This means that the interrupt was
0785        never detected, and probe_irq_off fails to report anything.
0786        This should fix probe_irq_* problems.
0787     */
0788     SMC_DELAY();
0789     SMC_DELAY();
0790 
0791     /* and disable all interrupts again */
0792     outb( 0, ioaddr + INT_MASK );
0793 
0794     /* and return what I found */
0795     return probe_irq_off(cookie);
0796 #else /* NO_AUTOPROBE */
0797     struct devlist *smcdev;
0798     for (smcdev = smc_devlist; smcdev->port; smcdev++) {
0799         if (smcdev->port == ioaddr)
0800             return smcdev->irq;
0801     }
0802     return 0;
0803 #endif
0804 }
0805 
0806 static const struct net_device_ops smc_netdev_ops = {
0807     .ndo_open        = smc_open,
0808     .ndo_stop       = smc_close,
0809     .ndo_start_xmit     = smc_wait_to_send_packet,
0810     .ndo_tx_timeout         = smc_timeout,
0811     .ndo_set_rx_mode    = smc_set_multicast_list,
0812     .ndo_set_mac_address    = eth_mac_addr,
0813     .ndo_validate_addr  = eth_validate_addr,
0814 };
0815 
0816 /*----------------------------------------------------------------------
0817  . Function: smc_probe( int ioaddr )
0818  .
0819  . Purpose:
0820  .  Tests to see if a given ioaddr points to an SMC9xxx chip.
0821  .  Returns a 0 on success
0822  .
0823  . Algorithm:
0824  .  (1) see if the high byte of BANK_SELECT is 0x33
0825  .  (2) compare the ioaddr with the base register's address
0826  .  (3) see if I recognize the chip ID in the appropriate register
0827  .
0828  .---------------------------------------------------------------------
0829  */
0830 
0831 /*---------------------------------------------------------------
0832  . Here I do typical initialization tasks.
0833  .
0834  . o  Initialize the structure if needed
0835  . o  print out my vanity message if not done so already
0836  . o  print out what type of hardware is detected
0837  . o  print out the ethernet address
0838  . o  find the IRQ
0839  . o  set up my private data
0840  . o  configure the dev structure with my subroutines
0841  . o  actually GRAB the irq.
0842  . o  GRAB the region
0843  .-----------------------------------------------------------------
0844 */
0845 static int __init smc_probe(struct net_device *dev, int ioaddr)
0846 {
0847     int i, memory, retval;
0848     unsigned int bank;
0849 
0850     const char *version_string;
0851     const char *if_string;
0852 
0853     /* registers */
0854     word revision_register;
0855     word base_address_register;
0856     word configuration_register;
0857     word memory_info_register;
0858     word memory_cfg_register;
0859     u8 addr[ETH_ALEN];
0860 
0861     /* Grab the region so that no one else tries to probe our ioports. */
0862     if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
0863         return -EBUSY;
0864 
0865     dev->irq = irq;
0866     dev->if_port = ifport;
0867 
0868     /* First, see if the high byte is 0x33 */
0869     bank = inw( ioaddr + BANK_SELECT );
0870     if ( (bank & 0xFF00) != 0x3300 ) {
0871         retval = -ENODEV;
0872         goto err_out;
0873     }
0874     /* The above MIGHT indicate a device, but I need to write to further
0875         test this.  */
0876     outw( 0x0, ioaddr + BANK_SELECT );
0877     bank = inw( ioaddr + BANK_SELECT );
0878     if ( (bank & 0xFF00 ) != 0x3300 ) {
0879         retval = -ENODEV;
0880         goto err_out;
0881     }
0882     /* well, we've already written once, so hopefully another time won't
0883        hurt.  This time, I need to switch the bank register to bank 1,
0884        so I can access the base address register */
0885     SMC_SELECT_BANK(1);
0886     base_address_register = inw( ioaddr + BASE );
0887     if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) )  {
0888         printk(CARDNAME ": IOADDR %x doesn't match configuration (%x). "
0889             "Probably not a SMC chip\n",
0890             ioaddr, base_address_register >> 3 & 0x3E0 );
0891         /* well, the base address register didn't match.  Must not have
0892            been a SMC chip after all. */
0893         retval = -ENODEV;
0894         goto err_out;
0895     }
0896 
0897     /*  check if the revision register is something that I recognize.
0898         These might need to be added to later, as future revisions
0899         could be added.  */
0900     SMC_SELECT_BANK(3);
0901     revision_register  = inw( ioaddr + REVISION );
0902     if ( !chip_ids[ ( revision_register  >> 4 ) & 0xF  ] ) {
0903         /* I don't recognize this chip, so... */
0904         printk(CARDNAME ": IO %x: Unrecognized revision register:"
0905             " %x, Contact author.\n", ioaddr, revision_register);
0906 
0907         retval = -ENODEV;
0908         goto err_out;
0909     }
0910 
0911     /* at this point I'll assume that the chip is an SMC9xxx.
0912        It might be prudent to check a listing of MAC addresses
0913        against the hardware address, or do some other tests. */
0914 
0915     pr_info_once("%s\n", version);
0916 
0917     /* fill in some of the fields */
0918     dev->base_addr = ioaddr;
0919 
0920     /*
0921      . Get the MAC address ( bank 1, regs 4 - 9 )
0922     */
0923     SMC_SELECT_BANK( 1 );
0924     for ( i = 0; i < 6; i += 2 ) {
0925         word    address;
0926 
0927         address = inw( ioaddr + ADDR0 + i  );
0928         addr[i + 1] = address >> 8;
0929         addr[i] = address & 0xFF;
0930     }
0931     eth_hw_addr_set(dev, addr);
0932 
0933     /* get the memory information */
0934 
0935     SMC_SELECT_BANK( 0 );
0936     memory_info_register = inw( ioaddr + MIR );
0937     memory_cfg_register  = inw( ioaddr + MCR );
0938     memory = ( memory_cfg_register >> 9 )  & 0x7;  /* multiplier */
0939     memory *= 256 * ( memory_info_register & 0xFF );
0940 
0941     /*
0942      Now, I want to find out more about the chip.  This is sort of
0943      redundant, but it's cleaner to have it in both, rather than having
0944      one VERY long probe procedure.
0945     */
0946     SMC_SELECT_BANK(3);
0947     revision_register  = inw( ioaddr + REVISION );
0948     version_string = chip_ids[ ( revision_register  >> 4 ) & 0xF  ];
0949     if ( !version_string ) {
0950         /* I shouldn't get here because this call was done before.... */
0951         retval = -ENODEV;
0952         goto err_out;
0953     }
0954 
0955     /* is it using AUI or 10BaseT ? */
0956     if ( dev->if_port == 0 ) {
0957         SMC_SELECT_BANK(1);
0958         configuration_register = inw( ioaddr + CONFIG );
0959         if ( configuration_register & CFG_AUI_SELECT )
0960             dev->if_port = 2;
0961         else
0962             dev->if_port = 1;
0963     }
0964     if_string = interfaces[ dev->if_port - 1 ];
0965 
0966     /* now, reset the chip, and put it into a known state */
0967     smc_reset( ioaddr );
0968 
0969     /*
0970      . If dev->irq is 0, then the device has to be banged on to see
0971      . what the IRQ is.
0972      .
0973      . This banging doesn't always detect the IRQ, for unknown reasons.
0974      . a workaround is to reset the chip and try again.
0975      .
0976      . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
0977      . be what is requested on the command line.   I don't do that, mostly
0978      . because the card that I have uses a non-standard method of accessing
0979      . the IRQs, and because this _should_ work in most configurations.
0980      .
0981      . Specifying an IRQ is done with the assumption that the user knows
0982      . what (s)he is doing.  No checking is done!!!!
0983      .
0984     */
0985     if ( dev->irq < 2 ) {
0986         int trials;
0987 
0988         trials = 3;
0989         while ( trials-- ) {
0990             dev->irq = smc_findirq( ioaddr );
0991             if ( dev->irq )
0992                 break;
0993             /* kick the card and try again */
0994             smc_reset( ioaddr );
0995         }
0996     }
0997     if (dev->irq == 0 ) {
0998         printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
0999         retval = -ENODEV;
1000         goto err_out;
1001     }
1002 
1003     /* now, print out the card info, in a short format.. */
1004 
1005     netdev_info(dev, "%s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ",
1006             version_string, revision_register & 0xF, ioaddr, dev->irq,
1007             if_string, memory);
1008     /*
1009      . Print the Ethernet address
1010     */
1011     netdev_info(dev, "ADDR: %pM\n", dev->dev_addr);
1012 
1013     /* Grab the IRQ */
1014     retval = request_irq(dev->irq, smc_interrupt, 0, DRV_NAME, dev);
1015     if (retval) {
1016         netdev_warn(dev, "%s: unable to get IRQ %d (irqval=%d).\n",
1017                 DRV_NAME, dev->irq, retval);
1018         goto err_out;
1019     }
1020 
1021     dev->netdev_ops         = &smc_netdev_ops;
1022     dev->watchdog_timeo     = HZ/20;
1023 
1024     return 0;
1025 
1026 err_out:
1027     release_region(ioaddr, SMC_IO_EXTENT);
1028     return retval;
1029 }
1030 
1031 #if SMC_DEBUG > 2
1032 static void print_packet( byte * buf, int length )
1033 {
1034 #if 0
1035     print_hex_dump_debug(DRV_NAME, DUMP_PREFIX_OFFSET, 16, 1,
1036                  buf, length, true);
1037 #endif
1038 }
1039 #endif
1040 
1041 
1042 /*
1043  * Open and Initialize the board
1044  *
1045  * Set up everything, reset the card, etc ..
1046  *
1047  */
1048 static int smc_open(struct net_device *dev)
1049 {
1050     int ioaddr = dev->base_addr;
1051 
1052     int i;  /* used to set hw ethernet address */
1053 
1054     /* clear out all the junk that was put here before... */
1055     memset(netdev_priv(dev), 0, sizeof(struct smc_local));
1056 
1057     /* reset the hardware */
1058 
1059     smc_reset( ioaddr );
1060     smc_enable( ioaddr );
1061 
1062     /* Select which interface to use */
1063 
1064     SMC_SELECT_BANK( 1 );
1065     if ( dev->if_port == 1 ) {
1066         outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1067             ioaddr + CONFIG );
1068     }
1069     else if ( dev->if_port == 2 ) {
1070         outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1071             ioaddr + CONFIG );
1072     }
1073 
1074     /*
1075         According to Becker, I have to set the hardware address
1076         at this point, because the (l)user can set it with an
1077         ioctl.  Easily done...
1078     */
1079     SMC_SELECT_BANK( 1 );
1080     for ( i = 0; i < 6; i += 2 ) {
1081         word    address;
1082 
1083         address = dev->dev_addr[ i + 1 ] << 8 ;
1084         address  |= dev->dev_addr[ i ];
1085         outw( address, ioaddr + ADDR0 + i );
1086     }
1087 
1088     netif_start_queue(dev);
1089     return 0;
1090 }
1091 
1092 /*--------------------------------------------------------
1093  . Called by the kernel to send a packet out into the void
1094  . of the net.  This routine is largely based on
1095  . skeleton.c, from Becker.
1096  .--------------------------------------------------------
1097 */
1098 
1099 static void smc_timeout(struct net_device *dev, unsigned int txqueue)
1100 {
1101     /* If we get here, some higher level has decided we are broken.
1102        There should really be a "kick me" function call instead. */
1103     netdev_warn(dev, CARDNAME": transmit timed out, %s?\n",
1104             tx_done(dev) ? "IRQ conflict" : "network cable problem");
1105     /* "kick" the adaptor */
1106     smc_reset( dev->base_addr );
1107     smc_enable( dev->base_addr );
1108     netif_trans_update(dev); /* prevent tx timeout */
1109     /* clear anything saved */
1110     ((struct smc_local *)netdev_priv(dev))->saved_skb = NULL;
1111     netif_wake_queue(dev);
1112 }
1113 
1114 /*-------------------------------------------------------------
1115  .
1116  . smc_rcv -  receive a packet from the card
1117  .
1118  . There is ( at least ) a packet waiting to be read from
1119  . chip-memory.
1120  .
1121  . o Read the status
1122  . o If an error, record it
1123  . o otherwise, read in the packet
1124  --------------------------------------------------------------
1125 */
1126 static void smc_rcv(struct net_device *dev)
1127 {
1128     int     ioaddr = dev->base_addr;
1129     int     packet_number;
1130     word    status;
1131     word    packet_length;
1132 
1133     /* assume bank 2 */
1134 
1135     packet_number = inw( ioaddr + FIFO_PORTS );
1136 
1137     if ( packet_number & FP_RXEMPTY ) {
1138         /* we got called , but nothing was on the FIFO */
1139         PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO.\n"));
1140         /* don't need to restore anything */
1141         return;
1142     }
1143 
1144     /*  start reading from the start of the packet */
1145     outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1146 
1147     /* First two words are status and packet_length */
1148     status      = inw( ioaddr + DATA_1 );
1149     packet_length   = inw( ioaddr + DATA_1 );
1150 
1151     packet_length &= 0x07ff;  /* mask off top bits */
1152 
1153     PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1154     /*
1155      . the packet length contains 3 extra words :
1156      . status, length, and an extra word with an odd byte .
1157     */
1158     packet_length -= 6;
1159 
1160     if ( !(status & RS_ERRORS ) ){
1161         /* do stuff to make a new packet */
1162         struct sk_buff  * skb;
1163         byte        * data;
1164 
1165         /* read one extra byte */
1166         if ( status & RS_ODDFRAME )
1167             packet_length++;
1168 
1169         /* set multicast stats */
1170         if ( status & RS_MULTICAST )
1171             dev->stats.multicast++;
1172 
1173         skb = netdev_alloc_skb(dev, packet_length + 5);
1174         if ( skb == NULL ) {
1175             dev->stats.rx_dropped++;
1176             goto done;
1177         }
1178 
1179         /*
1180          ! This should work without alignment, but it could be
1181          ! in the worse case
1182         */
1183 
1184         skb_reserve( skb, 2 );   /* 16 bit alignment */
1185 
1186         data = skb_put( skb, packet_length);
1187 
1188 #ifdef USE_32_BIT
1189         /* QUESTION:  Like in the TX routine, do I want
1190            to send the DWORDs or the bytes first, or some
1191            mixture.  A mixture might improve already slow PIO
1192            performance  */
1193         PRINTK3((" Reading %d dwords (and %d bytes)\n",
1194             packet_length >> 2, packet_length & 3 ));
1195         insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1196         /* read the left over bytes */
1197         insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1198             packet_length & 0x3  );
1199 #else
1200         PRINTK3((" Reading %d words and %d byte(s)\n",
1201             (packet_length >> 1 ), packet_length & 1 ));
1202         insw(ioaddr + DATA_1 , data, packet_length >> 1);
1203         if ( packet_length & 1 ) {
1204             data += packet_length & ~1;
1205             *(data++) = inb( ioaddr + DATA_1 );
1206         }
1207 #endif
1208 #if SMC_DEBUG > 2
1209             print_packet( data, packet_length );
1210 #endif
1211 
1212         skb->protocol = eth_type_trans(skb, dev );
1213         netif_rx(skb);
1214         dev->stats.rx_packets++;
1215         dev->stats.rx_bytes += packet_length;
1216     } else {
1217         /* error ... */
1218         dev->stats.rx_errors++;
1219 
1220         if ( status & RS_ALGNERR )  dev->stats.rx_frame_errors++;
1221         if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1222             dev->stats.rx_length_errors++;
1223         if ( status & RS_BADCRC)    dev->stats.rx_crc_errors++;
1224     }
1225 
1226 done:
1227     /*  error or good, tell the card to get rid of this packet */
1228     outw( MC_RELEASE, ioaddr + MMU_CMD );
1229 }
1230 
1231 
1232 /*************************************************************************
1233  . smc_tx
1234  .
1235  . Purpose:  Handle a transmit error message.   This will only be called
1236  .   when an error, because of the AUTO_RELEASE mode.
1237  .
1238  . Algorithm:
1239  .  Save pointer and packet no
1240  .  Get the packet no from the top of the queue
1241  .  check if it's valid ( if not, is this an error??? )
1242  .  read the status word
1243  .  record the error
1244  .  ( resend?  Not really, since we don't want old packets around )
1245  .  Restore saved values
1246  ************************************************************************/
1247 static void smc_tx( struct net_device * dev )
1248 {
1249     int ioaddr = dev->base_addr;
1250     struct smc_local *lp = netdev_priv(dev);
1251     byte saved_packet;
1252     byte packet_no;
1253     word tx_status;
1254 
1255 
1256     /* assume bank 2  */
1257 
1258     saved_packet = inb( ioaddr + PNR_ARR );
1259     packet_no = inw( ioaddr + FIFO_PORTS );
1260     packet_no &= 0x7F;
1261 
1262     /* select this as the packet to read from */
1263     outb( packet_no, ioaddr + PNR_ARR );
1264 
1265     /* read the first word from this packet */
1266     outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1267 
1268     tx_status = inw( ioaddr + DATA_1 );
1269     PRINTK3((CARDNAME": TX DONE STATUS: %4x\n", tx_status));
1270 
1271     dev->stats.tx_errors++;
1272     if ( tx_status & TS_LOSTCAR ) dev->stats.tx_carrier_errors++;
1273     if ( tx_status & TS_LATCOL  ) {
1274         netdev_dbg(dev, CARDNAME": Late collision occurred on last xmit.\n");
1275         dev->stats.tx_window_errors++;
1276     }
1277 #if 0
1278         if ( tx_status & TS_16COL ) { ... }
1279 #endif
1280 
1281     if ( tx_status & TS_SUCCESS ) {
1282         netdev_info(dev, CARDNAME": Successful packet caused interrupt\n");
1283     }
1284     /* re-enable transmit */
1285     SMC_SELECT_BANK( 0 );
1286     outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1287 
1288     /* kill the packet */
1289     SMC_SELECT_BANK( 2 );
1290     outw( MC_FREEPKT, ioaddr + MMU_CMD );
1291 
1292     /* one less packet waiting for me */
1293     lp->packets_waiting--;
1294 
1295     outb( saved_packet, ioaddr + PNR_ARR );
1296 }
1297 
1298 /*--------------------------------------------------------------------
1299  .
1300  . This is the main routine of the driver, to handle the device when
1301  . it needs some attention.
1302  .
1303  . So:
1304  .   first, save state of the chipset
1305  .   branch off into routines to handle each case, and acknowledge
1306  .      each to the interrupt register
1307  .   and finally restore state.
1308  .
1309  ---------------------------------------------------------------------*/
1310 
1311 static irqreturn_t smc_interrupt(int irq, void * dev_id)
1312 {
1313     struct net_device *dev  = dev_id;
1314     int ioaddr      = dev->base_addr;
1315     struct smc_local *lp = netdev_priv(dev);
1316 
1317     byte    status;
1318     word    card_stats;
1319     byte    mask;
1320     int timeout;
1321     /* state registers */
1322     word    saved_bank;
1323     word    saved_pointer;
1324     int handled = 0;
1325 
1326 
1327     PRINTK3((CARDNAME": SMC interrupt started\n"));
1328 
1329     saved_bank = inw( ioaddr + BANK_SELECT );
1330 
1331     SMC_SELECT_BANK(2);
1332     saved_pointer = inw( ioaddr + POINTER );
1333 
1334     mask = inb( ioaddr + INT_MASK );
1335     /* clear all interrupts */
1336     outb( 0, ioaddr + INT_MASK );
1337 
1338 
1339     /* set a timeout value, so I don't stay here forever */
1340     timeout = 4;
1341 
1342     PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x\n", mask));
1343     do {
1344         /* read the status flag, and mask it */
1345         status = inb( ioaddr + INTERRUPT ) & mask;
1346         if (!status )
1347             break;
1348 
1349         handled = 1;
1350 
1351         PRINTK3((KERN_WARNING CARDNAME
1352             ": Handling interrupt status %x\n", status));
1353 
1354         if (status & IM_RCV_INT) {
1355             /* Got a packet(s). */
1356             PRINTK2((KERN_WARNING CARDNAME
1357                 ": Receive Interrupt\n"));
1358             smc_rcv(dev);
1359         } else if (status & IM_TX_INT ) {
1360             PRINTK2((KERN_WARNING CARDNAME
1361                 ": TX ERROR handled\n"));
1362             smc_tx(dev);
1363             outb(IM_TX_INT, ioaddr + INTERRUPT );
1364         } else if (status & IM_TX_EMPTY_INT ) {
1365             /* update stats */
1366             SMC_SELECT_BANK( 0 );
1367             card_stats = inw( ioaddr + COUNTER );
1368             /* single collisions */
1369             dev->stats.collisions += card_stats & 0xF;
1370             card_stats >>= 4;
1371             /* multiple collisions */
1372             dev->stats.collisions += card_stats & 0xF;
1373 
1374             /* these are for when linux supports these statistics */
1375 
1376             SMC_SELECT_BANK( 2 );
1377             PRINTK2((KERN_WARNING CARDNAME
1378                 ": TX_BUFFER_EMPTY handled\n"));
1379             outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1380             mask &= ~IM_TX_EMPTY_INT;
1381             dev->stats.tx_packets += lp->packets_waiting;
1382             lp->packets_waiting = 0;
1383 
1384         } else if (status & IM_ALLOC_INT ) {
1385             PRINTK2((KERN_DEBUG CARDNAME
1386                 ": Allocation interrupt\n"));
1387             /* clear this interrupt so it doesn't happen again */
1388             mask &= ~IM_ALLOC_INT;
1389 
1390             smc_hardware_send_packet( dev );
1391 
1392             /* enable xmit interrupts based on this */
1393             mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1394 
1395             /* and let the card send more packets to me */
1396             netif_wake_queue(dev);
1397 
1398             PRINTK2((CARDNAME": Handoff done successfully.\n"));
1399         } else if (status & IM_RX_OVRN_INT ) {
1400             dev->stats.rx_errors++;
1401             dev->stats.rx_fifo_errors++;
1402             outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1403         } else if (status & IM_EPH_INT ) {
1404             PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT\n"));
1405         } else if (status & IM_ERCV_INT ) {
1406             PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT\n"));
1407             outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1408         }
1409     } while ( timeout -- );
1410 
1411 
1412     /* restore state register */
1413     SMC_SELECT_BANK( 2 );
1414     outb( mask, ioaddr + INT_MASK );
1415 
1416     PRINTK3((KERN_WARNING CARDNAME ": MASK is now %x\n", mask));
1417     outw( saved_pointer, ioaddr + POINTER );
1418 
1419     SMC_SELECT_BANK( saved_bank );
1420 
1421     PRINTK3((CARDNAME ": Interrupt done\n"));
1422     return IRQ_RETVAL(handled);
1423 }
1424 
1425 
1426 /*----------------------------------------------------
1427  . smc_close
1428  .
1429  . this makes the board clean up everything that it can
1430  . and not talk to the outside world.   Caused by
1431  . an 'ifconfig ethX down'
1432  .
1433  -----------------------------------------------------*/
1434 static int smc_close(struct net_device *dev)
1435 {
1436     netif_stop_queue(dev);
1437     /* clear everything */
1438     smc_shutdown( dev->base_addr );
1439 
1440     /* Update the statistics here. */
1441     return 0;
1442 }
1443 
1444 /*-----------------------------------------------------------
1445  . smc_set_multicast_list
1446  .
1447  . This routine will, depending on the values passed to it,
1448  . either make it accept multicast packets, go into
1449  . promiscuous mode ( for TCPDUMP and cousins ) or accept
1450  . a select set of multicast packets
1451 */
1452 static void smc_set_multicast_list(struct net_device *dev)
1453 {
1454     short ioaddr = dev->base_addr;
1455 
1456     SMC_SELECT_BANK(0);
1457     if ( dev->flags & IFF_PROMISC )
1458         outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1459 
1460 /* BUG?  I never disable promiscuous mode if multicasting was turned on.
1461    Now, I turn off promiscuous mode, but I don't do anything to multicasting
1462    when promiscuous mode is turned on.
1463 */
1464 
1465     /* Here, I am setting this to accept all multicast packets.
1466        I don't need to zero the multicast table, because the flag is
1467        checked before the table is
1468     */
1469     else if (dev->flags & IFF_ALLMULTI)
1470         outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1471 
1472     /* We just get all multicast packets even if we only want them
1473      . from one source.  This will be changed at some future
1474      . point. */
1475     else if (!netdev_mc_empty(dev)) {
1476         /* support hardware multicasting */
1477 
1478         /* be sure I get rid of flags I might have set */
1479         outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1480             ioaddr + RCR );
1481         /* NOTE: this has to set the bank, so make sure it is the
1482            last thing called.  The bank is set to zero at the top */
1483         smc_setmulticast(ioaddr, dev);
1484     }
1485     else  {
1486         outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1487             ioaddr + RCR );
1488 
1489         /*
1490           since I'm disabling all multicast entirely, I need to
1491           clear the multicast list
1492         */
1493         SMC_SELECT_BANK( 3 );
1494         outw( 0, ioaddr + MULTICAST1 );
1495         outw( 0, ioaddr + MULTICAST2 );
1496         outw( 0, ioaddr + MULTICAST3 );
1497         outw( 0, ioaddr + MULTICAST4 );
1498     }
1499 }
1500 
1501 #ifdef MODULE
1502 
1503 static struct net_device *devSMC9194;
1504 MODULE_LICENSE("GPL");
1505 
1506 module_param_hw(io, int, ioport, 0);
1507 module_param_hw(irq, int, irq, 0);
1508 module_param(ifport, int, 0);
1509 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1510 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1511 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1512 
1513 static int __init smc_init_module(void)
1514 {
1515     if (io == 0)
1516         printk(KERN_WARNING
1517         CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1518 
1519     /* copy the parameters from insmod into the device structure */
1520     devSMC9194 = smc_init(-1);
1521     return PTR_ERR_OR_ZERO(devSMC9194);
1522 }
1523 module_init(smc_init_module);
1524 
1525 static void __exit smc_cleanup_module(void)
1526 {
1527     unregister_netdev(devSMC9194);
1528     free_irq(devSMC9194->irq, devSMC9194);
1529     release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1530     free_netdev(devSMC9194);
1531 }
1532 module_exit(smc_cleanup_module);
1533 
1534 #endif /* MODULE */