1 /*------------------------------------------------------------------------
3 . This is a driver for SMC's 9000 series of Ethernet cards.
5 . Copyright (C) 1996 by Erik Stahlman
6 . This software may be used and distributed according to the terms
7 . of the GNU Public License, incorporated herein by reference.
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 )
15 . io = for the base address
17 . ifport = 0 for autodetect, 1 for TP, 2 for AUI ( or 10base2 )
20 . Erik Stahlman ( erik@vt.edu )
22 . Arnaldo Carvalho de Melo <acme@conectiva.com.br>
24 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
28 . o skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov )
29 . o ( a LOT of advice from Becker as well )
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
52 . 08/20/00 Arnaldo Melo fix kfree(skb) in smc_hardware_send_packet
53 ----------------------------------------------------------------------------*/
55 static const char *version
=
56 "smc9194.c:v0.13 04/14/00 by Erik Stahlman (erik@vt.edu)\n";
58 #include <linux/module.h>
59 #include <linux/version.h>
60 #include <linux/kernel.h>
61 #include <linux/sched.h>
62 #include <linux/types.h>
63 #include <linux/fcntl.h>
64 #include <linux/interrupt.h>
65 #include <linux/ptrace.h>
66 #include <linux/ioport.h>
68 #include <linux/malloc.h>
69 #include <linux/string.h>
70 #include <linux/init.h>
71 #include <asm/bitops.h>
73 #include <linux/errno.h>
75 #include <linux/netdevice.h>
76 #include <linux/etherdevice.h>
77 #include <linux/skbuff.h>
80 /*------------------------------------------------------------------------
82 . Configuration options, for the experienced user to change.
84 -------------------------------------------------------------------------*/
87 . Do you want to use 32 bit xfers? This should work on all chips, as
88 . the chipset is designed to accommodate them.
93 .the SMC9194 can be at any of the following port addresses. To change,
94 .for a slightly different card, you can add it to the array. Keep in
95 .mind that the array must end in zero.
97 static unsigned int smc_portlist
[] __initdata
= {
98 0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
99 0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0, 0
103 . Wait time for memory to be free. This probably shouldn't be
104 . tuned that much, as waiting for this means nothing else happens
107 #define MEMORY_WAIT_TIME 16
112 . 0 for normal operation
113 . 1 for slightly more details
114 . >2 for various levels of increasingly useless information
115 . 2 for interrupt tracking, status flags
116 . 3 for packet dumps, etc.
121 #define PRINTK3(x) printk x
127 #define PRINTK2(x) printk x
133 #define PRINTK(x) printk x
139 /*------------------------------------------------------------------------
141 . The internal workings of the driver. If you are changing anything
142 . here with the SMC stuff, you should have the datasheet and known
143 . what you are doing.
145 -------------------------------------------------------------------------*/
146 #define CARDNAME "SMC9194"
149 /* store this information for the driver.. */
152 these are things that the kernel wants me to keep, so users
153 can find out semi-useless statistics of how well the card is
156 struct net_device_stats stats
;
159 If I have to wait until memory is available to send
160 a packet, I will store the skbuff here, until I get the
161 desired memory. Then, I'll send it out and free it.
163 struct sk_buff
* saved_skb
;
166 . This keeps track of how many packets that I have
167 . sent out. When an TX_EMPTY interrupt comes, I know
168 . that all of these have been sent.
174 /*-----------------------------------------------------------------
176 . The driver can be entered at any of the following entry points.
178 .------------------------------------------------------------------ */
181 . This is called by register_netdev(). It is responsible for
182 . checking the portlist for the SMC9000 series chipset. If it finds
183 . one, then it will initialize the device, find the hardware information,
184 . and sets up the appropriate device parameters.
185 . NOTE: Interrupts are *OFF* when this procedure is called.
187 . NB:This shouldn't be static since it is referred to externally.
189 int smc_init(struct net_device
*dev
);
192 . The kernel calls this function when someone wants to use the device,
193 . typically 'ifconfig ethX up'.
195 static int smc_open(struct net_device
*dev
);
198 . Our watchdog timed out. Called by the networking layer
200 static void smc_timeout(struct net_device
*dev
);
203 . This is called by the kernel to send a packet out into the net. it's
204 . responsible for doing a best-effort send, but if it's simply not possible
205 . to send it, the packet gets dropped.
207 static int smc_send_packet(struct sk_buff
*skb
, struct net_device
*dev
);
210 . This is called by the kernel in response to 'ifconfig ethX down'. It
211 . is responsible for cleaning up everything that the open routine
212 . does, and maybe putting the card into a powerdown state.
214 static int smc_close(struct net_device
*dev
);
217 . This routine allows the proc file system to query the driver's
220 static struct net_device_stats
* smc_query_statistics( struct net_device
*dev
);
223 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
224 . programs ) and multicast modes.
226 static void smc_set_multicast_list(struct net_device
*dev
);
231 static int crc32( char * s
, int length
);
233 /*---------------------------------------------------------------
235 . Interrupt level calls..
237 ----------------------------------------------------------------*/
240 . Handles the actual interrupt
242 static void smc_interrupt(int irq
, void *, struct pt_regs
*regs
);
244 . This is a separate procedure to handle the receipt of a packet, to
245 . leave the interrupt code looking slightly cleaner
247 inline static void smc_rcv( struct net_device
*dev
);
249 . This handles a TX interrupt, which is only called when an error
250 . relating to a packet is sent.
252 inline static void smc_tx( struct net_device
* dev
);
255 ------------------------------------------------------------
259 ------------------------------------------------------------
263 . Test if a given location contains a chip, trying to cause as
264 . little damage as possible if it's not a SMC chip.
266 static int smc_probe( int ioaddr
);
269 . this routine initializes the cards hardware, prints out the configuration
270 . to the system log as well as the vanity message, and handles the setup
271 . of a device parameter.
272 . It will give an error if it can't initialize the card.
274 static int smc_initcard( struct net_device
*, int ioaddr
);
277 . A rather simple routine to print out a packet for debugging purposes.
280 static void print_packet( byte
*, int );
283 #define tx_done(dev) 1
285 /* this is called to actually send the packet to the chip */
286 static void smc_hardware_send_packet( struct net_device
* dev
);
288 /* Since I am not sure if I will have enough room in the chip's ram
289 . to store the packet, I call this routine, which either sends it
290 . now, or generates an interrupt when the card is ready for the
292 static int smc_wait_to_send_packet( struct sk_buff
* skb
, struct net_device
*dev
);
294 /* this does a soft reset on the device */
295 static void smc_reset( int ioaddr
);
297 /* Enable Interrupts, Receive, and Transmit */
298 static void smc_enable( int ioaddr
);
300 /* this puts the device in an inactive state */
301 static void smc_shutdown( int ioaddr
);
303 /* This routine will find the IRQ of the driver if one is not
304 . specified in the input to the device. */
305 static int smc_findirq( int ioaddr
);
308 . Function: smc_reset( int ioaddr )
310 . This sets the SMC91xx chip to its normal state, hopefully from whatever
311 . mess that any other DOS driver has put it in.
313 . Maybe I should reset more registers to defaults in here? SOFTRESET should
317 . 1. send a SOFT RESET
318 . 2. wait for it to finish
319 . 3. enable autorelease mode
320 . 4. reset the memory management unit
321 . 5. clear all interrupts
324 static void smc_reset( int ioaddr
)
326 /* This resets the registers mostly to defaults, but doesn't
327 affect EEPROM. That seems unnecessary */
328 SMC_SELECT_BANK( 0 );
329 outw( RCR_SOFTRESET
, ioaddr
+ RCR
);
331 /* this should pause enough for the chip to be happy */
334 /* Set the transmit and receive configuration registers to
336 outw( RCR_CLEAR
, ioaddr
+ RCR
);
337 outw( TCR_CLEAR
, ioaddr
+ TCR
);
339 /* set the control register to automatically
340 release successfully transmitted packets, to make the best
341 use out of our limited memory */
342 SMC_SELECT_BANK( 1 );
343 outw( inw( ioaddr
+ CONTROL
) | CTL_AUTO_RELEASE
, ioaddr
+ CONTROL
);
346 SMC_SELECT_BANK( 2 );
347 outw( MC_RESET
, ioaddr
+ MMU_CMD
);
349 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
350 but this is a place where future chipsets _COULD_ break. Be wary
351 of issuing another MMU command right after this */
353 outb( 0, ioaddr
+ INT_MASK
);
357 . Function: smc_enable
358 . Purpose: let the chip talk to the outside work
360 . 1. Enable the transmitter
361 . 2. Enable the receiver
362 . 3. Enable interrupts
364 static void smc_enable( int ioaddr
)
366 SMC_SELECT_BANK( 0 );
367 /* see the header file for options in TCR/RCR NORMAL*/
368 outw( TCR_NORMAL
, ioaddr
+ TCR
);
369 outw( RCR_NORMAL
, ioaddr
+ RCR
);
371 /* now, enable interrupts */
372 SMC_SELECT_BANK( 2 );
373 outb( SMC_INTERRUPT_MASK
, ioaddr
+ INT_MASK
);
377 . Function: smc_shutdown
378 . Purpose: closes down the SMC91xxx chip.
380 . 1. zero the interrupt mask
381 . 2. clear the enable receive flag
382 . 3. clear the enable xmit flags
385 . (1) maybe utilize power down mode.
386 . Why not yet? Because while the chip will go into power down mode,
387 . the manual says that it will wake up in response to any I/O requests
388 . in the register space. Empirical results do not show this working.
390 static void smc_shutdown( int ioaddr
)
392 /* no more interrupts for me */
393 SMC_SELECT_BANK( 2 );
394 outb( 0, ioaddr
+ INT_MASK
);
396 /* and tell the card to stay away from that nasty outside world */
397 SMC_SELECT_BANK( 0 );
398 outb( RCR_CLEAR
, ioaddr
+ RCR
);
399 outb( TCR_CLEAR
, ioaddr
+ TCR
);
401 /* finally, shut the chip down */
402 SMC_SELECT_BANK( 1 );
403 outw( inw( ioaddr
+ CONTROL
), CTL_POWERDOWN
, ioaddr
+ CONTROL
);
409 . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
411 . This sets the internal hardware table to filter out unwanted multicast
412 . packets before they take up memory.
414 . The SMC chip uses a hash table where the high 6 bits of the CRC of
415 . address are the offset into the table. If that bit is 1, then the
416 . multicast packet is accepted. Otherwise, it's dropped silently.
418 . To use the 6 bits as an offset into the table, the high 3 bits are the
419 . number of the 8 bit register, while the low 3 bits are the bit within
422 . This routine is based very heavily on the one provided by Peter Cammaert.
426 static void smc_setmulticast( int ioaddr
, int count
, struct dev_mc_list
* addrs
) {
428 unsigned char multicast_table
[ 8 ];
429 struct dev_mc_list
* cur_addr
;
430 /* table for flipping the order of 3 bits */
431 unsigned char invert3
[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
433 /* start with a table of all zeros: reject all */
434 memset( multicast_table
, 0, sizeof( multicast_table
) );
437 for ( i
= 0; i
< count
; i
++, cur_addr
= cur_addr
->next
) {
440 /* do we have a pointer here? */
443 /* make sure this is a multicast address - shouldn't this
444 be a given if we have it here ? */
445 if ( !( *cur_addr
->dmi_addr
& 1 ) )
448 /* only use the low order bits */
449 position
= crc32( cur_addr
->dmi_addr
, 6 ) & 0x3f;
451 /* do some messy swapping to put the bit in the right spot */
452 multicast_table
[invert3
[position
&7]] |=
453 (1<<invert3
[(position
>>3)&7]);
456 /* now, the table can be loaded into the chipset */
457 SMC_SELECT_BANK( 3 );
459 for ( i
= 0; i
< 8 ; i
++ ) {
460 outb( multicast_table
[i
], ioaddr
+ MULTICAST1
+ i
);
465 Finds the CRC32 of a set of bytes.
466 Again, from Peter Cammaert's code.
468 static int crc32( char * s
, int length
) {
472 /* crc polynomial for Ethernet */
473 const unsigned long poly
= 0xedb88320;
474 /* crc value - preinitialized to all 1's */
475 unsigned long crc_value
= 0xffffffff;
477 for ( perByte
= 0; perByte
< length
; perByte
++ ) {
481 for ( perBit
= 0; perBit
< 8; perBit
++ ) {
482 crc_value
= (crc_value
>>1)^
483 (((crc_value
^c
)&0x01)?poly
:0);
492 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
494 . Attempt to allocate memory for a packet, if chip-memory is not
495 . available, then tell the card to generate an interrupt when it
500 . o if the saved_skb is not currently null, then drop this packet
501 . on the floor. This should never happen, because of TBUSY.
502 . o if the saved_skb is null, then replace it with the current packet,
503 . o See if I can sending it now.
504 . o (NO): Enable interrupts and let the interrupt handler deal with it.
505 . o (YES):Send it now.
507 static int smc_wait_to_send_packet( struct sk_buff
* skb
, struct net_device
* dev
)
509 struct smc_local
*lp
= (struct smc_local
*)dev
->priv
;
510 unsigned short ioaddr
= dev
->base_addr
;
512 unsigned short numPages
;
515 if ( lp
->saved_skb
) {
516 /* THIS SHOULD NEVER HAPPEN. */
517 lp
->stats
.tx_aborted_errors
++;
518 printk(CARDNAME
": Bad Craziness - sent packet while busy.\n" );
523 length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
527 ** The MMU wants the number of pages to be the number of 256 bytes
528 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
530 ** Pkt size for allocating is data length +6 (for additional status words,
531 ** length and ctl!) If odd size last byte is included in this header.
533 numPages
= ((length
& 0xfffe) + 6) / 256;
536 printk(CARDNAME
": Far too big packet error. \n");
537 /* freeing the packet is a good thing here... but should
538 . any packets of this size get down here? */
540 lp
->saved_skb
= NULL
;
541 /* this IS an error, but, i don't want the skb saved */
542 netif_wake_queue(dev
);
545 /* either way, a packet is waiting now */
546 lp
->packets_waiting
++;
548 /* now, try to allocate the memory */
549 SMC_SELECT_BANK( 2 );
550 outw( MC_ALLOC
| numPages
, ioaddr
+ MMU_CMD
);
554 . wait a short amount of time.. if I can send a packet now, I send
555 . it now. Otherwise, I enable an interrupt and wait for one to be
558 . I could have handled this a slightly different way, by checking to
559 . see if any memory was available in the FREE MEMORY register. However,
560 . either way, I need to generate an allocation, and the allocation works
561 . no matter what, so I saw no point in checking free memory.
563 time_out
= MEMORY_WAIT_TIME
;
567 status
= inb( ioaddr
+ INTERRUPT
);
568 if ( status
& IM_ALLOC_INT
) {
569 /* acknowledge the interrupt */
570 outb( IM_ALLOC_INT
, ioaddr
+ INTERRUPT
);
573 } while ( -- time_out
);
576 /* oh well, wait until the chip finds memory later */
577 SMC_ENABLE_INT( IM_ALLOC_INT
);
578 PRINTK2((CARDNAME
": memory allocation deferred. \n"));
579 /* it's deferred, but I'll handle it later */
582 /* or YES! I can send the packet now.. */
583 smc_hardware_send_packet(dev
);
584 netif_wake_queue(dev
);
589 . Function: smc_hardware_send_packet(struct net_device * )
591 . This sends the actual packet to the SMC9xxx chip.
594 . First, see if a saved_skb is available.
595 . ( this should NOT be called if there is no 'saved_skb'
596 . Now, find the packet number that the chip allocated
597 . Point the data pointers at it in memory
598 . Set the length word in the chip's memory
599 . Dump the packet to chip memory
600 . Check if a last byte is needed ( odd length packet )
601 . if so, set the control flag right
602 . Tell the card to send it
603 . Enable the transmit interrupt, so I know if it failed
604 . Free the kernel data if I actually sent it.
606 static void smc_hardware_send_packet( struct net_device
* dev
)
608 struct smc_local
*lp
= (struct smc_local
*)dev
->priv
;
610 struct sk_buff
* skb
= lp
->saved_skb
;
612 unsigned short ioaddr
;
615 ioaddr
= dev
->base_addr
;
618 PRINTK((CARDNAME
": In XMIT with no packet to send \n"));
621 length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
624 /* If I get here, I _know_ there is a packet slot waiting for me */
625 packet_no
= inb( ioaddr
+ PNR_ARR
+ 1 );
626 if ( packet_no
& 0x80 ) {
627 /* or isn't there? BAD CHIP! */
628 printk(KERN_DEBUG CARDNAME
": Memory allocation failed. \n");
630 lp
->saved_skb
= NULL
;
631 netif_wake_queue(dev
);
635 /* we have a packet address, so tell the card to use it */
636 outb( packet_no
, ioaddr
+ PNR_ARR
);
638 /* point to the beginning of the packet */
639 outw( PTR_AUTOINC
, ioaddr
+ POINTER
);
641 PRINTK3((CARDNAME
": Trying to xmit packet of length %x\n", length
));
643 print_packet( buf
, length
);
646 /* send the packet length ( +6 for status, length and ctl byte )
647 and the status word ( set to zeros ) */
649 outl( (length
+6 ) << 16 , ioaddr
+ DATA_1
);
651 outw( 0, ioaddr
+ DATA_1
);
652 /* send the packet length ( +6 for status words, length, and ctl*/
653 outb( (length
+6) & 0xFF,ioaddr
+ DATA_1
);
654 outb( (length
+6) >> 8 , ioaddr
+ DATA_1
);
657 /* send the actual data
658 . I _think_ it's faster to send the longs first, and then
659 . mop up by sending the last word. It depends heavily
660 . on alignment, at least on the 486. Maybe it would be
661 . a good idea to check which is optimal? But that could take
662 . almost as much time as is saved?
665 if ( length
& 0x2 ) {
666 outsl(ioaddr
+ DATA_1
, buf
, length
>> 2 );
667 outw( *((word
*)(buf
+ (length
& 0xFFFFFFFC))),ioaddr
+DATA_1
);
670 outsl(ioaddr
+ DATA_1
, buf
, length
>> 2 );
672 outsw(ioaddr
+ DATA_1
, buf
, (length
) >> 1);
674 /* Send the last byte, if there is one. */
676 if ( (length
& 1) == 0 ) {
677 outw( 0, ioaddr
+ DATA_1
);
679 outb( buf
[length
-1 ], ioaddr
+ DATA_1
);
680 outb( 0x20, ioaddr
+ DATA_1
);
683 /* enable the interrupts */
684 SMC_ENABLE_INT( (IM_TX_INT
| IM_TX_EMPTY_INT
) );
686 /* and let the chipset deal with it */
687 outw( MC_ENQUEUE
, ioaddr
+ MMU_CMD
);
689 PRINTK2((CARDNAME
": Sent packet of length %d \n",length
));
691 lp
->saved_skb
= NULL
;
694 dev
->trans_start
= jiffies
;
696 /* we can send another packet */
697 netif_wake_queue(dev
);
702 /*-------------------------------------------------------------------------
704 | smc_init( struct net_device * dev )
706 | dev->base_addr == 0, try to find all possible locations
707 | dev->base_addr == 1, return failure code
708 | dev->base_addr == 2, always allocate space, and return success
709 | dev->base_addr == <anything else> this is the address to check
712 | 0 --> there is a device
713 | anything else, error
715 ---------------------------------------------------------------------------
717 int __init
smc_init(struct net_device
*dev
)
720 int base_addr
= dev
? dev
->base_addr
: 0;
722 /* try a specific location */
723 if (base_addr
> 0x1ff) {
725 error
= smc_probe(base_addr
);
727 return smc_initcard( dev
, base_addr
);
731 if ( 0 != base_addr
) {
736 /* check every ethernet address */
737 for (i
= 0; smc_portlist
[i
]; i
++) {
738 int ioaddr
= smc_portlist
[i
];
740 /* check if the area is available */
741 if (check_region( ioaddr
, SMC_IO_EXTENT
))
744 /* check this specific address */
745 if ( smc_probe( ioaddr
) == 0) {
746 return smc_initcard( dev
, ioaddr
);
750 /* couldn't find anything */
754 /*----------------------------------------------------------------------
757 . This routine has a simple purpose -- make the SMC chip generate an
758 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
759 ------------------------------------------------------------------------
761 int __init
smc_findirq( int ioaddr
)
766 /* I have to do a STI() here, because this is called from
767 a routine that does an CLI during this process, making it
768 rather difficult to get interrupts for auto detection */
774 * What I try to do here is trigger an ALLOC_INT. This is done
775 * by allocating a small chunk of memory, which will give an interrupt
781 /* enable ALLOCation interrupts ONLY */
782 outb( IM_ALLOC_INT
, ioaddr
+ INT_MASK
);
785 . Allocate 512 bytes of memory. Note that the chip was just
786 . reset so all the memory is available
788 outw( MC_ALLOC
| 1, ioaddr
+ MMU_CMD
);
791 . Wait until positive that the interrupt has been generated
796 int_status
= inb( ioaddr
+ INTERRUPT
);
798 if ( int_status
& IM_ALLOC_INT
)
799 break; /* got the interrupt */
802 /* there is really nothing that I can do here if timeout fails,
803 as autoirq_report will return a 0 anyway, which is what I
804 want in this case. Plus, the clean up is needed in both
808 On a fast machine, the status might change before the interrupt
809 is given to the processor. This means that the interrupt was
810 never detected, and autoirq_report fails to report anything.
811 This should fix autoirq_* problems.
816 /* and disable all interrupts again */
817 outb( 0, ioaddr
+ INT_MASK
);
819 /* clear hardware interrupts again, because that's how it
820 was when I was called... */
823 /* and return what I found */
824 return autoirq_report( 0 );
827 /*----------------------------------------------------------------------
828 . Function: smc_probe( int ioaddr )
831 . Tests to see if a given ioaddr points to an SMC9xxx chip.
832 . Returns a 0 on success
835 . (1) see if the high byte of BANK_SELECT is 0x33
836 . (2) compare the ioaddr with the base register's address
837 . (3) see if I recognize the chip ID in the appropriate register
839 .---------------------------------------------------------------------
842 static int __init
smc_probe( int ioaddr
)
845 word revision_register
;
846 word base_address_register
;
848 /* First, see if the high byte is 0x33 */
849 bank
= inw( ioaddr
+ BANK_SELECT
);
850 if ( (bank
& 0xFF00) != 0x3300 ) {
853 /* The above MIGHT indicate a device, but I need to write to further
855 outw( 0x0, ioaddr
+ BANK_SELECT
);
856 bank
= inw( ioaddr
+ BANK_SELECT
);
857 if ( (bank
& 0xFF00 ) != 0x3300 ) {
860 /* well, we've already written once, so hopefully another time won't
861 hurt. This time, I need to switch the bank register to bank 1,
862 so I can access the base address register */
864 base_address_register
= inw( ioaddr
+ BASE
);
865 if ( ioaddr
!= ( base_address_register
>> 3 & 0x3E0 ) ) {
866 printk(CARDNAME
": IOADDR %x doesn't match configuration (%x)."
867 "Probably not a SMC chip\n",
868 ioaddr
, base_address_register
>> 3 & 0x3E0 );
869 /* well, the base address register didn't match. Must not have
870 been a SMC chip after all. */
874 /* check if the revision register is something that I recognize.
875 These might need to be added to later, as future revisions
878 revision_register
= inw( ioaddr
+ REVISION
);
879 if ( !chip_ids
[ ( revision_register
>> 4 ) & 0xF ] ) {
880 /* I don't recognize this chip, so... */
881 printk(CARDNAME
": IO %x: Unrecognized revision register:"
882 " %x, Contact author. \n", ioaddr
, revision_register
);
887 /* at this point I'll assume that the chip is an SMC9xxx.
888 It might be prudent to check a listing of MAC addresses
889 against the hardware address, or do some other tests. */
893 /*---------------------------------------------------------------
894 . Here I do typical initialization tasks.
896 . o Initialize the structure if needed
897 . o print out my vanity message if not done so already
898 . o print out what type of hardware is detected
899 . o print out the ethernet address
901 . o set up my private data
902 . o configure the dev structure with my subroutines
903 . o actually GRAB the irq.
905 .-----------------------------------------------------------------
907 static int __init
smc_initcard(struct net_device
*dev
, int ioaddr
)
911 static unsigned version_printed
= 0;
914 word revision_register
;
915 word configuration_register
;
916 word memory_info_register
;
917 word memory_cfg_register
;
919 const char * version_string
;
920 const char * if_string
;
925 /* see if I need to initialize the ethernet card structure */
927 dev
= init_etherdev(0, 0);
932 if (version_printed
++ == 0)
933 printk("%s", version
);
935 /* fill in some of the fields */
936 dev
->base_addr
= ioaddr
;
939 . Get the MAC address ( bank 1, regs 4 - 9 )
941 SMC_SELECT_BANK( 1 );
942 for ( i
= 0; i
< 6; i
+= 2 ) {
945 address
= inw( ioaddr
+ ADDR0
+ i
);
946 dev
->dev_addr
[ i
+ 1] = address
>> 8;
947 dev
->dev_addr
[ i
] = address
& 0xFF;
950 /* get the memory information */
952 SMC_SELECT_BANK( 0 );
953 memory_info_register
= inw( ioaddr
+ MIR
);
954 memory_cfg_register
= inw( ioaddr
+ MCR
);
955 memory
= ( memory_cfg_register
>> 9 ) & 0x7; /* multiplier */
956 memory
*= 256 * ( memory_info_register
& 0xFF );
959 Now, I want to find out more about the chip. This is sort of
960 redundant, but it's cleaner to have it in both, rather than having
961 one VERY long probe procedure.
964 revision_register
= inw( ioaddr
+ REVISION
);
965 version_string
= chip_ids
[ ( revision_register
>> 4 ) & 0xF ];
966 if ( !version_string
) {
967 /* I shouldn't get here because this call was done before.... */
971 /* is it using AUI or 10BaseT ? */
972 if ( dev
->if_port
== 0 ) {
974 configuration_register
= inw( ioaddr
+ CONFIG
);
975 if ( configuration_register
& CFG_AUI_SELECT
)
980 if_string
= interfaces
[ dev
->if_port
- 1 ];
982 /* now, reset the chip, and put it into a known state */
986 . If dev->irq is 0, then the device has to be banged on to see
989 . This banging doesn't always detect the IRQ, for unknown reasons.
990 . a workaround is to reset the chip and try again.
992 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
993 . be what is requested on the command line. I don't do that, mostly
994 . because the card that I have uses a non-standard method of accessing
995 . the IRQs, and because this _should_ work in most configurations.
997 . Specifying an IRQ is done with the assumption that the user knows
998 . what (s)he is doing. No checking is done!!!!
1001 if ( dev
->irq
< 2 ) {
1005 while ( trials
-- ) {
1006 dev
->irq
= smc_findirq( ioaddr
);
1009 /* kick the card and try again */
1010 smc_reset( ioaddr
);
1013 if (dev
->irq
== 0 ) {
1014 printk(CARDNAME
": Couldn't autodetect your IRQ. Use irq=xx.\n");
1017 if (dev
->irq
== 2) {
1018 /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
1019 * or don't know which one to set.
1024 /* now, print out the card info, in a short format.. */
1026 printk(CARDNAME
": %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ",
1027 version_string
, revision_register
& 0xF, ioaddr
, dev
->irq
,
1028 if_string
, memory
);
1030 . Print the Ethernet address
1033 for (i
= 0; i
< 5; i
++)
1034 printk("%2.2x:", dev
->dev_addr
[i
] );
1035 printk("%2.2x \n", dev
->dev_addr
[5] );
1038 /* Initialize the private structure. */
1039 if (dev
->priv
== NULL
) {
1040 dev
->priv
= kmalloc(sizeof(struct smc_local
), GFP_KERNEL
);
1041 if (dev
->priv
== NULL
)
1044 /* set the private data to zero by default */
1045 memset(dev
->priv
, 0, sizeof(struct smc_local
));
1047 /* Fill in the fields of the device structure with ethernet values. */
1051 irqval
= request_irq(dev
->irq
, &smc_interrupt
, 0, CARDNAME
, dev
);
1053 printk(CARDNAME
": unable to get IRQ %d (irqval=%d).\n",
1058 /* Grab the region so that no one else tries to probe our ioports. */
1059 request_region(ioaddr
, SMC_IO_EXTENT
, CARDNAME
);
1061 dev
->open
= smc_open
;
1062 dev
->stop
= smc_close
;
1063 dev
->hard_start_xmit
= smc_send_packet
;
1064 dev
->tx_timeout
= smc_timeout
;
1065 dev
->watchdog_timeo
= HZ
/20;
1066 dev
->get_stats
= smc_query_statistics
;
1067 dev
->set_multicast_list
= smc_set_multicast_list
;
1073 static void print_packet( byte
* buf
, int length
)
1080 printk("Packet of length %d \n", length
);
1081 lines
= length
/ 16;
1082 remainder
= length
% 16;
1084 for ( i
= 0; i
< lines
; i
++ ) {
1087 for ( cur
= 0; cur
< 8; cur
++ ) {
1092 printk("%02x%02x ", a
, b
);
1096 for ( i
= 0; i
< remainder
/2 ; i
++ ) {
1101 printk("%02x%02x ", a
, b
);
1110 * Open and Initialize the board
1112 * Set up everything, reset the card, etc ..
1115 static int smc_open(struct net_device
*dev
)
1117 int ioaddr
= dev
->base_addr
;
1119 int i
; /* used to set hw ethernet address */
1121 /* clear out all the junk that was put here before... */
1122 memset(dev
->priv
, 0, sizeof(struct smc_local
));
1126 /* reset the hardware */
1128 smc_reset( ioaddr
);
1129 smc_enable( ioaddr
);
1131 /* Select which interface to use */
1133 SMC_SELECT_BANK( 1 );
1134 if ( dev
->if_port
== 1 ) {
1135 outw( inw( ioaddr
+ CONFIG
) & ~CFG_AUI_SELECT
,
1138 else if ( dev
->if_port
== 2 ) {
1139 outw( inw( ioaddr
+ CONFIG
) | CFG_AUI_SELECT
,
1144 According to Becker, I have to set the hardware address
1145 at this point, because the (l)user can set it with an
1146 ioctl. Easily done...
1148 SMC_SELECT_BANK( 1 );
1149 for ( i
= 0; i
< 6; i
+= 2 ) {
1152 address
= dev
->dev_addr
[ i
+ 1 ] << 8 ;
1153 address
|= dev
->dev_addr
[ i
];
1154 outw( address
, ioaddr
+ ADDR0
+ i
);
1157 netif_start_queue(dev
);
1161 /*--------------------------------------------------------
1162 . Called by the kernel to send a packet out into the void
1163 . of the net. This routine is largely based on
1164 . skeleton.c, from Becker.
1165 .--------------------------------------------------------
1168 static void smc_timeout(struct net_device
*dev
)
1170 /* If we get here, some higher level has decided we are broken.
1171 There should really be a "kick me" function call instead. */
1172 printk(KERN_WARNING CARDNAME
": transmit timed out, %s?\n",
1173 tx_done(dev
) ? "IRQ conflict" :
1174 "network cable problem");
1175 /* "kick" the adaptor */
1176 smc_reset( dev
->base_addr
);
1177 smc_enable( dev
->base_addr
);
1178 dev
->trans_start
= jiffies
;
1179 /* clear anything saved */
1180 ((struct smc_local
*)dev
->priv
)->saved_skb
= NULL
;
1181 netif_wake_queue(dev
);
1184 static int smc_send_packet(struct sk_buff
*skb
, struct net_device
*dev
)
1186 netif_stop_queue(dev
);
1187 /* Well, I want to send the packet.. but I don't know
1188 if I can send it right now... */
1189 return smc_wait_to_send_packet( skb
, dev
);
1192 /*--------------------------------------------------------------------
1194 . This is the main routine of the driver, to handle the device when
1195 . it needs some attention.
1198 . first, save state of the chipset
1199 . branch off into routines to handle each case, and acknowledge
1200 . each to the interrupt register
1201 . and finally restore state.
1203 ---------------------------------------------------------------------*/
1205 static void smc_interrupt(int irq
, void * dev_id
, struct pt_regs
* regs
)
1207 struct net_device
*dev
= dev_id
;
1208 int ioaddr
= dev
->base_addr
;
1209 struct smc_local
*lp
= (struct smc_local
*)dev
->priv
;
1215 /* state registers */
1221 PRINTK3((CARDNAME
": SMC interrupt started \n"));
1223 saved_bank
= inw( ioaddr
+ BANK_SELECT
);
1226 saved_pointer
= inw( ioaddr
+ POINTER
);
1228 mask
= inb( ioaddr
+ INT_MASK
);
1229 /* clear all interrupts */
1230 outb( 0, ioaddr
+ INT_MASK
);
1233 /* set a timeout value, so I don't stay here forever */
1236 PRINTK2((KERN_WARNING CARDNAME
": MASK IS %x \n", mask
));
1238 /* read the status flag, and mask it */
1239 status
= inb( ioaddr
+ INTERRUPT
) & mask
;
1243 PRINTK3((KERN_WARNING CARDNAME
1244 ": Handling interrupt status %x \n", status
));
1246 if (status
& IM_RCV_INT
) {
1247 /* Got a packet(s). */
1248 PRINTK2((KERN_WARNING CARDNAME
1249 ": Receive Interrupt\n"));
1251 } else if (status
& IM_TX_INT
) {
1252 PRINTK2((KERN_WARNING CARDNAME
1253 ": TX ERROR handled\n"));
1255 outb(IM_TX_INT
, ioaddr
+ INTERRUPT
);
1256 } else if (status
& IM_TX_EMPTY_INT
) {
1258 SMC_SELECT_BANK( 0 );
1259 card_stats
= inw( ioaddr
+ COUNTER
);
1260 /* single collisions */
1261 lp
->stats
.collisions
+= card_stats
& 0xF;
1263 /* multiple collisions */
1264 lp
->stats
.collisions
+= card_stats
& 0xF;
1266 /* these are for when linux supports these statistics */
1268 SMC_SELECT_BANK( 2 );
1269 PRINTK2((KERN_WARNING CARDNAME
1270 ": TX_BUFFER_EMPTY handled\n"));
1271 outb( IM_TX_EMPTY_INT
, ioaddr
+ INTERRUPT
);
1272 mask
&= ~IM_TX_EMPTY_INT
;
1273 lp
->stats
.tx_packets
+= lp
->packets_waiting
;
1274 lp
->packets_waiting
= 0;
1276 } else if (status
& IM_ALLOC_INT
) {
1277 PRINTK2((KERN_DEBUG CARDNAME
1278 ": Allocation interrupt \n"));
1279 /* clear this interrupt so it doesn't happen again */
1280 mask
&= ~IM_ALLOC_INT
;
1282 smc_hardware_send_packet( dev
);
1284 /* enable xmit interrupts based on this */
1285 mask
|= ( IM_TX_EMPTY_INT
| IM_TX_INT
);
1287 /* and let the card send more packets to me */
1288 netif_wake_queue(dev
);
1290 PRINTK2((CARDNAME
": Handoff done successfully.\n"));
1291 } else if (status
& IM_RX_OVRN_INT
) {
1292 lp
->stats
.rx_errors
++;
1293 lp
->stats
.rx_fifo_errors
++;
1294 outb( IM_RX_OVRN_INT
, ioaddr
+ INTERRUPT
);
1295 } else if (status
& IM_EPH_INT
) {
1296 PRINTK((CARDNAME
": UNSUPPORTED: EPH INTERRUPT \n"));
1297 } else if (status
& IM_ERCV_INT
) {
1298 PRINTK((CARDNAME
": UNSUPPORTED: ERCV INTERRUPT \n"));
1299 outb( IM_ERCV_INT
, ioaddr
+ INTERRUPT
);
1301 } while ( timeout
-- );
1304 /* restore state register */
1305 SMC_SELECT_BANK( 2 );
1306 outb( mask
, ioaddr
+ INT_MASK
);
1308 PRINTK3(( KERN_WARNING CARDNAME
": MASK is now %x \n", mask
));
1309 outw( saved_pointer
, ioaddr
+ POINTER
);
1311 SMC_SELECT_BANK( saved_bank
);
1313 PRINTK3((CARDNAME
": Interrupt done\n"));
1317 /*-------------------------------------------------------------
1319 . smc_rcv - receive a packet from the card
1321 . There is ( at least ) a packet waiting to be read from
1325 . o If an error, record it
1326 . o otherwise, read in the packet
1327 --------------------------------------------------------------
1329 static void smc_rcv(struct net_device
*dev
)
1331 struct smc_local
*lp
= (struct smc_local
*)dev
->priv
;
1332 int ioaddr
= dev
->base_addr
;
1339 packet_number
= inw( ioaddr
+ FIFO_PORTS
);
1341 if ( packet_number
& FP_RXEMPTY
) {
1342 /* we got called , but nothing was on the FIFO */
1343 PRINTK((CARDNAME
": WARNING: smc_rcv with nothing on FIFO. \n"));
1344 /* don't need to restore anything */
1348 /* start reading from the start of the packet */
1349 outw( PTR_READ
| PTR_RCV
| PTR_AUTOINC
, ioaddr
+ POINTER
);
1351 /* First two words are status and packet_length */
1352 status
= inw( ioaddr
+ DATA_1
);
1353 packet_length
= inw( ioaddr
+ DATA_1
);
1355 packet_length
&= 0x07ff; /* mask off top bits */
1357 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status
, packet_length
));
1359 . the packet length contains 3 extra words :
1360 . status, length, and an extra word with an odd byte .
1364 if ( !(status
& RS_ERRORS
) ){
1365 /* do stuff to make a new packet */
1366 struct sk_buff
* skb
;
1369 /* read one extra byte */
1370 if ( status
& RS_ODDFRAME
)
1373 /* set multicast stats */
1374 if ( status
& RS_MULTICAST
)
1375 lp
->stats
.multicast
++;
1377 skb
= dev_alloc_skb( packet_length
+ 5);
1379 if ( skb
== NULL
) {
1380 printk(KERN_NOTICE CARDNAME
1381 ": Low memory, packet dropped.\n");
1382 lp
->stats
.rx_dropped
++;
1386 ! This should work without alignment, but it could be
1390 skb_reserve( skb
, 2 ); /* 16 bit alignment */
1393 data
= skb_put( skb
, packet_length
);
1396 /* QUESTION: Like in the TX routine, do I want
1397 to send the DWORDs or the bytes first, or some
1398 mixture. A mixture might improve already slow PIO
1400 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1401 packet_length
>> 2, packet_length
& 3 ));
1402 insl(ioaddr
+ DATA_1
, data
, packet_length
>> 2 );
1403 /* read the left over bytes */
1404 insb( ioaddr
+ DATA_1
, data
+ (packet_length
& 0xFFFFFC),
1405 packet_length
& 0x3 );
1407 PRINTK3((" Reading %d words and %d byte(s) \n",
1408 (packet_length
>> 1 ), packet_length
& 1 );
1409 if ( packet_length
& 1 )
1410 *(data
++) = inb( ioaddr
+ DATA_1
);
1411 insw(ioaddr
+ DATA_1
, data
, (packet_length
+ 1 ) >> 1);
1412 if ( packet_length
& 1 ) {
1413 data
+= packet_length
& ~1;
1414 *((data
++) = inb( ioaddr
+ DATA_1
);
1418 print_packet( data
, packet_length
);
1421 skb
->protocol
= eth_type_trans(skb
, dev
);
1423 lp
->stats
.rx_packets
++;
1426 lp
->stats
.rx_errors
++;
1428 if ( status
& RS_ALGNERR
) lp
->stats
.rx_frame_errors
++;
1429 if ( status
& (RS_TOOSHORT
| RS_TOOLONG
) )
1430 lp
->stats
.rx_length_errors
++;
1431 if ( status
& RS_BADCRC
) lp
->stats
.rx_crc_errors
++;
1433 /* error or good, tell the card to get rid of this packet */
1434 outw( MC_RELEASE
, ioaddr
+ MMU_CMD
);
1441 /*************************************************************************
1444 . Purpose: Handle a transmit error message. This will only be called
1445 . when an error, because of the AUTO_RELEASE mode.
1448 . Save pointer and packet no
1449 . Get the packet no from the top of the queue
1450 . check if it's valid ( if not, is this an error??? )
1451 . read the status word
1453 . ( resend? Not really, since we don't want old packets around )
1454 . Restore saved values
1455 ************************************************************************/
1456 static void smc_tx( struct net_device
* dev
)
1458 int ioaddr
= dev
->base_addr
;
1459 struct smc_local
*lp
= (struct smc_local
*)dev
->priv
;
1467 saved_packet
= inb( ioaddr
+ PNR_ARR
);
1468 packet_no
= inw( ioaddr
+ FIFO_PORTS
);
1471 /* select this as the packet to read from */
1472 outb( packet_no
, ioaddr
+ PNR_ARR
);
1474 /* read the first word from this packet */
1475 outw( PTR_AUTOINC
| PTR_READ
, ioaddr
+ POINTER
);
1477 tx_status
= inw( ioaddr
+ DATA_1
);
1478 PRINTK3((CARDNAME
": TX DONE STATUS: %4x \n", tx_status
));
1480 lp
->stats
.tx_errors
++;
1481 if ( tx_status
& TS_LOSTCAR
) lp
->stats
.tx_carrier_errors
++;
1482 if ( tx_status
& TS_LATCOL
) {
1483 printk(KERN_DEBUG CARDNAME
1484 ": Late collision occurred on last xmit.\n");
1485 lp
->stats
.tx_window_errors
++;
1488 if ( tx_status
& TS_16COL
) { ... }
1491 if ( tx_status
& TS_SUCCESS
) {
1492 printk(CARDNAME
": Successful packet caused interrupt \n");
1494 /* re-enable transmit */
1495 SMC_SELECT_BANK( 0 );
1496 outw( inw( ioaddr
+ TCR
) | TCR_ENABLE
, ioaddr
+ TCR
);
1498 /* kill the packet */
1499 SMC_SELECT_BANK( 2 );
1500 outw( MC_FREEPKT
, ioaddr
+ MMU_CMD
);
1502 /* one less packet waiting for me */
1503 lp
->packets_waiting
--;
1505 outb( saved_packet
, ioaddr
+ PNR_ARR
);
1509 /*----------------------------------------------------
1512 . this makes the board clean up everything that it can
1513 . and not talk to the outside world. Caused by
1514 . an 'ifconfig ethX down'
1516 -----------------------------------------------------*/
1517 static int smc_close(struct net_device
*dev
)
1519 netif_stop_queue(dev
);
1520 /* clear everything */
1521 smc_shutdown( dev
->base_addr
);
1523 /* Update the statistics here. */
1528 /*------------------------------------------------------------
1529 . Get the current statistics.
1530 . This may be called with the card open or closed.
1531 .-------------------------------------------------------------*/
1532 static struct net_device_stats
* smc_query_statistics(struct net_device
*dev
) {
1533 struct smc_local
*lp
= (struct smc_local
*)dev
->priv
;
1538 /*-----------------------------------------------------------
1539 . smc_set_multicast_list
1541 . This routine will, depending on the values passed to it,
1542 . either make it accept multicast packets, go into
1543 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1544 . a select set of multicast packets
1546 static void smc_set_multicast_list(struct net_device
*dev
)
1548 short ioaddr
= dev
->base_addr
;
1551 if ( dev
->flags
& IFF_PROMISC
)
1552 outw( inw(ioaddr
+ RCR
) | RCR_PROMISC
, ioaddr
+ RCR
);
1554 /* BUG? I never disable promiscuous mode if multicasting was turned on.
1555 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1556 when promiscuous mode is turned on.
1559 /* Here, I am setting this to accept all multicast packets.
1560 I don't need to zero the multicast table, because the flag is
1561 checked before the table is
1563 else if (dev
->flags
& IFF_ALLMULTI
)
1564 outw( inw(ioaddr
+ RCR
) | RCR_ALMUL
, ioaddr
+ RCR
);
1566 /* We just get all multicast packets even if we only want them
1567 . from one source. This will be changed at some future
1569 else if (dev
->mc_count
) {
1570 /* support hardware multicasting */
1572 /* be sure I get rid of flags I might have set */
1573 outw( inw( ioaddr
+ RCR
) & ~(RCR_PROMISC
| RCR_ALMUL
),
1575 /* NOTE: this has to set the bank, so make sure it is the
1576 last thing called. The bank is set to zero at the top */
1577 smc_setmulticast( ioaddr
, dev
->mc_count
, dev
->mc_list
);
1580 outw( inw( ioaddr
+ RCR
) & ~(RCR_PROMISC
| RCR_ALMUL
),
1584 since I'm disabling all multicast entirely, I need to
1585 clear the multicast list
1587 SMC_SELECT_BANK( 3 );
1588 outw( 0, ioaddr
+ MULTICAST1
);
1589 outw( 0, ioaddr
+ MULTICAST2
);
1590 outw( 0, ioaddr
+ MULTICAST3
);
1591 outw( 0, ioaddr
+ MULTICAST4
);
1597 static struct net_device devSMC9194
= {
1598 "", /* device name is inserted by linux/drivers/net/net_init.c */
1600 0, 0, /* I/O address, IRQ */
1601 0, 0, 0, NULL
, smc_init
};
1607 MODULE_PARM(io
, "i");
1608 MODULE_PARM(irq
, "i");
1609 MODULE_PARM(ifport
, "i");
1611 int init_module(void)
1617 CARDNAME
": You shouldn't use auto-probing with insmod!\n" );
1619 /* copy the parameters from insmod into the device structure */
1620 devSMC9194
.base_addr
= io
;
1621 devSMC9194
.irq
= irq
;
1622 devSMC9194
.if_port
= ifport
;
1623 if ((result
= register_netdev(&devSMC9194
)) != 0)
1629 void cleanup_module(void)
1631 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1632 unregister_netdev(&devSMC9194
);
1634 free_irq(devSMC9194
.irq
, &devSMC9194
);
1635 release_region(devSMC9194
.base_addr
, SMC_IO_EXTENT
);
1637 if (devSMC9194
.priv
)
1638 kfree(devSMC9194
.priv
);