Linux Audio

Check our new training course

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