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 General 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@scyld.com )
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 . 12/15/00 Christian Jullien fix "Warning: kfree_skb on hard IRQ"
54 . 11/08/01 Matt Domsch Use common crc32 function
55 ----------------------------------------------------------------------------*/
57 static const char version
[] =
58 "smc9194.c:v0.14 12/15/00 by Erik Stahlman (erik@vt.edu)";
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>
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>
80 #define DRV_NAME "smc9194"
82 /*------------------------------------------------------------------------
84 . Configuration options, for the experienced user to change.
86 -------------------------------------------------------------------------*/
89 . Do you want to use 32 bit xfers? This should work on all chips, as
90 . the chipset is designed to accommodate them.
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.
109 static 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},
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
133 #define MEMORY_WAIT_TIME 16
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.
147 #define PRINTK3(x) printk x
153 #define PRINTK2(x) printk x
159 #define PRINTK(x) printk x
165 /*------------------------------------------------------------------------
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.
171 -------------------------------------------------------------------------*/
172 #define CARDNAME "SMC9194"
175 /* store this information for the driver.. */
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.
182 struct sk_buff
* saved_skb
;
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.
193 /*-----------------------------------------------------------------
195 . The driver can be entered at any of the following entry points.
197 .------------------------------------------------------------------ */
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.
206 . NB:This shouldn't be static since it is referred to externally.
208 struct net_device
*smc_init(int unit
);
211 . The kernel calls this function when someone wants to use the device,
212 . typically 'ifconfig ethX up'.
214 static int smc_open(struct net_device
*dev
);
217 . Our watchdog timed out. Called by the networking layer
219 static void smc_timeout(struct net_device
*dev
);
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.
226 static int smc_close(struct net_device
*dev
);
229 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
230 . programs ) and multicast modes.
232 static void smc_set_multicast_list(struct net_device
*dev
);
235 /*---------------------------------------------------------------
237 . Interrupt level calls..
239 ----------------------------------------------------------------*/
242 . Handles the actual interrupt
244 static irqreturn_t
smc_interrupt(int irq
, void *);
246 . This is a separate procedure to handle the receipt of a packet, to
247 . leave the interrupt code looking slightly cleaner
249 static inline void smc_rcv( struct net_device
*dev
);
251 . This handles a TX interrupt, which is only called when an error
252 . relating to a packet is sent.
254 static inline void smc_tx( struct net_device
* dev
);
257 ------------------------------------------------------------
261 ------------------------------------------------------------
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.
268 static int smc_probe(struct net_device
*dev
, int ioaddr
);
271 . A rather simple routine to print out a packet for debugging purposes.
274 static void print_packet( byte
*, int );
277 #define tx_done(dev) 1
279 /* this is called to actually send the packet to the chip */
280 static void smc_hardware_send_packet( struct net_device
* dev
);
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
286 static netdev_tx_t
smc_wait_to_send_packet( struct sk_buff
* skb
,
287 struct net_device
*dev
);
289 /* this does a soft reset on the device */
290 static void smc_reset( int ioaddr
);
292 /* Enable Interrupts, Receive, and Transmit */
293 static void smc_enable( int ioaddr
);
295 /* this puts the device in an inactive state */
296 static void smc_shutdown( int ioaddr
);
298 /* This routine will find the IRQ of the driver if one is not
299 . specified in the input to the device. */
300 static int smc_findirq( int ioaddr
);
303 . Function: smc_reset( int ioaddr )
305 . This sets the SMC91xx chip to its normal state, hopefully from whatever
306 . mess that any other DOS driver has put it in.
308 . Maybe I should reset more registers to defaults in here? SOFTRESET should
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
319 static void smc_reset( int ioaddr
)
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
);
326 /* this should pause enough for the chip to be happy */
329 /* Set the transmit and receive configuration registers to
331 outw( RCR_CLEAR
, ioaddr
+ RCR
);
332 outw( TCR_CLEAR
, ioaddr
+ TCR
);
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
);
341 SMC_SELECT_BANK( 2 );
342 outw( MC_RESET
, ioaddr
+ MMU_CMD
);
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 */
348 outb( 0, ioaddr
+ INT_MASK
);
352 . Function: smc_enable
353 . Purpose: let the chip talk to the outside work
355 . 1. Enable the transmitter
356 . 2. Enable the receiver
357 . 3. Enable interrupts
359 static void smc_enable( int ioaddr
)
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
);
366 /* now, enable interrupts */
367 SMC_SELECT_BANK( 2 );
368 outb( SMC_INTERRUPT_MASK
, ioaddr
+ INT_MASK
);
372 . Function: smc_shutdown
373 . Purpose: closes down the SMC91xxx chip.
375 . 1. zero the interrupt mask
376 . 2. clear the enable receive flag
377 . 3. clear the enable xmit flags
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.
385 static void smc_shutdown( int ioaddr
)
387 /* no more interrupts for me */
388 SMC_SELECT_BANK( 2 );
389 outb( 0, ioaddr
+ INT_MASK
);
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
);
396 /* finally, shut the chip down */
397 SMC_SELECT_BANK( 1 );
398 outw( inw( ioaddr
+ CONTROL
), CTL_POWERDOWN
, ioaddr
+ CONTROL
);
404 . Function: smc_setmulticast( int ioaddr, struct net_device *dev )
406 . This sets the internal hardware table to filter out unwanted multicast
407 . packets before they take up memory.
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.
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
417 . This routine is based very heavily on the one provided by Peter Cammaert.
421 static void smc_setmulticast(int ioaddr
, struct net_device
*dev
)
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 };
429 /* start with a table of all zeros: reject all */
430 memset( multicast_table
, 0, sizeof( multicast_table
) );
432 netdev_for_each_mc_addr(ha
, dev
) {
435 /* only use the low order bits */
436 position
= ether_crc_le(6, ha
->addr
) & 0x3f;
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]);
443 /* now, the table can be loaded into the chipset */
444 SMC_SELECT_BANK( 3 );
446 for ( i
= 0; i
< 8 ; i
++ ) {
447 outb( multicast_table
[i
], ioaddr
+ MULTICAST1
+ i
);
452 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
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
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.
467 static netdev_tx_t
smc_wait_to_send_packet(struct sk_buff
*skb
,
468 struct net_device
*dev
)
470 struct smc_local
*lp
= netdev_priv(dev
);
471 unsigned int ioaddr
= dev
->base_addr
;
473 unsigned short numPages
;
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... */
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
;
490 if (length
< ETH_ZLEN
) {
491 if (skb_padto(skb
, ETH_ZLEN
)) {
492 netif_wake_queue(dev
);
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 :) )
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.
505 numPages
= ((length
& 0xfffe) + 6) / 256;
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? */
512 lp
->saved_skb
= NULL
;
513 /* this IS an error, but, i don't want the skb saved */
514 netif_wake_queue(dev
);
517 /* either way, a packet is waiting now */
518 lp
->packets_waiting
++;
520 /* now, try to allocate the memory */
521 SMC_SELECT_BANK( 2 );
522 outw( MC_ALLOC
| numPages
, ioaddr
+ MMU_CMD
);
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
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.
535 time_out
= MEMORY_WAIT_TIME
;
539 status
= inb( ioaddr
+ INTERRUPT
);
540 if ( status
& IM_ALLOC_INT
) {
541 /* acknowledge the interrupt */
542 outb( IM_ALLOC_INT
, ioaddr
+ INTERRUPT
);
545 } while ( -- 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 */
554 /* or YES! I can send the packet now.. */
555 smc_hardware_send_packet(dev
);
556 netif_wake_queue(dev
);
561 . Function: smc_hardware_send_packet(struct net_device * )
563 . This sends the actual packet to the SMC9xxx chip.
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.
578 static void smc_hardware_send_packet( struct net_device
* dev
)
580 struct smc_local
*lp
= netdev_priv(dev
);
582 struct sk_buff
* skb
= lp
->saved_skb
;
587 ioaddr
= dev
->base_addr
;
590 PRINTK((CARDNAME
": In XMIT with no packet to send\n"));
593 length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
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
);
607 /* we have a packet address, so tell the card to use it */
608 outb( packet_no
, ioaddr
+ PNR_ARR
);
610 /* point to the beginning of the packet */
611 outw( PTR_AUTOINC
, ioaddr
+ POINTER
);
613 PRINTK3((CARDNAME
": Trying to xmit packet of length %x\n", length
));
615 print_packet( buf
, length
);
618 /* send the packet length ( +6 for status, length and ctl byte )
619 and the status word ( set to zeros ) */
621 outl( (length
+6 ) << 16 , ioaddr
+ DATA_1
);
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
);
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?
637 if ( length
& 0x2 ) {
638 outsl(ioaddr
+ DATA_1
, buf
, length
>> 2 );
639 outw( *((word
*)(buf
+ (length
& 0xFFFFFFFC))),ioaddr
+DATA_1
);
642 outsl(ioaddr
+ DATA_1
, buf
, length
>> 2 );
644 outsw(ioaddr
+ DATA_1
, buf
, (length
) >> 1);
646 /* Send the last byte, if there is one. */
648 if ( (length
& 1) == 0 ) {
649 outw( 0, ioaddr
+ DATA_1
);
651 outb( buf
[length
-1 ], ioaddr
+ DATA_1
);
652 outb( 0x20, ioaddr
+ DATA_1
);
655 /* enable the interrupts */
656 SMC_ENABLE_INT( (IM_TX_INT
| IM_TX_EMPTY_INT
) );
658 /* and let the chipset deal with it */
659 outw( MC_ENQUEUE
, ioaddr
+ MMU_CMD
);
661 PRINTK2((CARDNAME
": Sent packet of length %d\n", length
));
663 lp
->saved_skb
= NULL
;
664 dev_kfree_skb_any (skb
);
666 netif_trans_update(dev
);
668 /* we can send another packet */
669 netif_wake_queue(dev
);
672 /*-------------------------------------------------------------------------
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
682 | pointer to net_device or ERR_PTR(error)
684 ---------------------------------------------------------------------------
690 struct net_device
* __init
smc_init(int unit
)
692 struct net_device
*dev
= alloc_etherdev(sizeof(struct smc_local
));
693 struct devlist
*smcdev
= smc_devlist
;
697 return ERR_PTR(-ENODEV
);
700 sprintf(dev
->name
, "eth%d", unit
);
701 netdev_boot_setup_check(dev
);
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. */
711 for (;smcdev
->port
; smcdev
++) {
712 if (smc_probe(dev
, smcdev
->port
) == 0)
720 err
= register_netdev(dev
);
725 free_irq(dev
->irq
, dev
);
726 release_region(dev
->base_addr
, SMC_IO_EXTENT
);
732 /*----------------------------------------------------------------------
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 ------------------------------------------------------------------------
739 static int __init
smc_findirq(int ioaddr
)
743 unsigned long cookie
;
746 cookie
= probe_irq_on();
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
756 /* enable ALLOCation interrupts ONLY */
757 outb( IM_ALLOC_INT
, ioaddr
+ INT_MASK
);
760 . Allocate 512 bytes of memory. Note that the chip was just
761 . reset so all the memory is available
763 outw( MC_ALLOC
| 1, ioaddr
+ MMU_CMD
);
766 . Wait until positive that the interrupt has been generated
771 int_status
= inb( ioaddr
+ INTERRUPT
);
773 if ( int_status
& IM_ALLOC_INT
)
774 break; /* got the interrupt */
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
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.
791 /* and disable all interrupts again */
792 outb( 0, ioaddr
+ INT_MASK
);
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
)
806 static 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_change_mtu
= eth_change_mtu
,
813 .ndo_set_mac_address
= eth_mac_addr
,
814 .ndo_validate_addr
= eth_validate_addr
,
817 /*----------------------------------------------------------------------
818 . Function: smc_probe( int ioaddr )
821 . Tests to see if a given ioaddr points to an SMC9xxx chip.
822 . Returns a 0 on success
825 . (1) see if the high byte of BANK_SELECT is 0x33
826 . (2) compare the ioaddr with the base register's address
827 . (3) see if I recognize the chip ID in the appropriate register
829 .---------------------------------------------------------------------
832 /*---------------------------------------------------------------
833 . Here I do typical initialization tasks.
835 . o Initialize the structure if needed
836 . o print out my vanity message if not done so already
837 . o print out what type of hardware is detected
838 . o print out the ethernet address
840 . o set up my private data
841 . o configure the dev structure with my subroutines
842 . o actually GRAB the irq.
844 .-----------------------------------------------------------------
846 static int __init
smc_probe(struct net_device
*dev
, int ioaddr
)
848 int i
, memory
, retval
;
851 const char *version_string
;
852 const char *if_string
;
855 word revision_register
;
856 word base_address_register
;
857 word configuration_register
;
858 word memory_info_register
;
859 word memory_cfg_register
;
861 /* Grab the region so that no one else tries to probe our ioports. */
862 if (!request_region(ioaddr
, SMC_IO_EXTENT
, DRV_NAME
))
866 dev
->if_port
= ifport
;
868 /* First, see if the high byte is 0x33 */
869 bank
= inw( ioaddr
+ BANK_SELECT
);
870 if ( (bank
& 0xFF00) != 0x3300 ) {
874 /* The above MIGHT indicate a device, but I need to write to further
876 outw( 0x0, ioaddr
+ BANK_SELECT
);
877 bank
= inw( ioaddr
+ BANK_SELECT
);
878 if ( (bank
& 0xFF00 ) != 0x3300 ) {
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 */
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. */
897 /* check if the revision register is something that I recognize.
898 These might need to be added to later, as future revisions
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
);
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. */
915 pr_info_once("%s\n", version
);
917 /* fill in some of the fields */
918 dev
->base_addr
= ioaddr
;
921 . Get the MAC address ( bank 1, regs 4 - 9 )
923 SMC_SELECT_BANK( 1 );
924 for ( i
= 0; i
< 6; i
+= 2 ) {
927 address
= inw( ioaddr
+ ADDR0
+ i
);
928 dev
->dev_addr
[ i
+ 1] = address
>> 8;
929 dev
->dev_addr
[ i
] = address
& 0xFF;
932 /* get the memory information */
934 SMC_SELECT_BANK( 0 );
935 memory_info_register
= inw( ioaddr
+ MIR
);
936 memory_cfg_register
= inw( ioaddr
+ MCR
);
937 memory
= ( memory_cfg_register
>> 9 ) & 0x7; /* multiplier */
938 memory
*= 256 * ( memory_info_register
& 0xFF );
941 Now, I want to find out more about the chip. This is sort of
942 redundant, but it's cleaner to have it in both, rather than having
943 one VERY long probe procedure.
946 revision_register
= inw( ioaddr
+ REVISION
);
947 version_string
= chip_ids
[ ( revision_register
>> 4 ) & 0xF ];
948 if ( !version_string
) {
949 /* I shouldn't get here because this call was done before.... */
954 /* is it using AUI or 10BaseT ? */
955 if ( dev
->if_port
== 0 ) {
957 configuration_register
= inw( ioaddr
+ CONFIG
);
958 if ( configuration_register
& CFG_AUI_SELECT
)
963 if_string
= interfaces
[ dev
->if_port
- 1 ];
965 /* now, reset the chip, and put it into a known state */
969 . If dev->irq is 0, then the device has to be banged on to see
972 . This banging doesn't always detect the IRQ, for unknown reasons.
973 . a workaround is to reset the chip and try again.
975 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
976 . be what is requested on the command line. I don't do that, mostly
977 . because the card that I have uses a non-standard method of accessing
978 . the IRQs, and because this _should_ work in most configurations.
980 . Specifying an IRQ is done with the assumption that the user knows
981 . what (s)he is doing. No checking is done!!!!
984 if ( dev
->irq
< 2 ) {
989 dev
->irq
= smc_findirq( ioaddr
);
992 /* kick the card and try again */
996 if (dev
->irq
== 0 ) {
997 printk(CARDNAME
": Couldn't autodetect your IRQ. Use irq=xx.\n");
1002 /* now, print out the card info, in a short format.. */
1004 netdev_info(dev
, "%s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ",
1005 version_string
, revision_register
& 0xF, ioaddr
, dev
->irq
,
1008 . Print the Ethernet address
1010 netdev_info(dev
, "ADDR: %pM\n", dev
->dev_addr
);
1013 retval
= request_irq(dev
->irq
, smc_interrupt
, 0, DRV_NAME
, dev
);
1015 netdev_warn(dev
, "%s: unable to get IRQ %d (irqval=%d).\n",
1016 DRV_NAME
, dev
->irq
, retval
);
1020 dev
->netdev_ops
= &smc_netdev_ops
;
1021 dev
->watchdog_timeo
= HZ
/20;
1026 release_region(ioaddr
, SMC_IO_EXTENT
);
1031 static void print_packet( byte
* buf
, int length
)
1034 print_hex_dump_debug(DRV_NAME
, DUMP_PREFIX_OFFSET
, 16, 1,
1042 * Open and Initialize the board
1044 * Set up everything, reset the card, etc ..
1047 static int smc_open(struct net_device
*dev
)
1049 int ioaddr
= dev
->base_addr
;
1051 int i
; /* used to set hw ethernet address */
1053 /* clear out all the junk that was put here before... */
1054 memset(netdev_priv(dev
), 0, sizeof(struct smc_local
));
1056 /* reset the hardware */
1058 smc_reset( ioaddr
);
1059 smc_enable( ioaddr
);
1061 /* Select which interface to use */
1063 SMC_SELECT_BANK( 1 );
1064 if ( dev
->if_port
== 1 ) {
1065 outw( inw( ioaddr
+ CONFIG
) & ~CFG_AUI_SELECT
,
1068 else if ( dev
->if_port
== 2 ) {
1069 outw( inw( ioaddr
+ CONFIG
) | CFG_AUI_SELECT
,
1074 According to Becker, I have to set the hardware address
1075 at this point, because the (l)user can set it with an
1076 ioctl. Easily done...
1078 SMC_SELECT_BANK( 1 );
1079 for ( i
= 0; i
< 6; i
+= 2 ) {
1082 address
= dev
->dev_addr
[ i
+ 1 ] << 8 ;
1083 address
|= dev
->dev_addr
[ i
];
1084 outw( address
, ioaddr
+ ADDR0
+ i
);
1087 netif_start_queue(dev
);
1091 /*--------------------------------------------------------
1092 . Called by the kernel to send a packet out into the void
1093 . of the net. This routine is largely based on
1094 . skeleton.c, from Becker.
1095 .--------------------------------------------------------
1098 static void smc_timeout(struct net_device
*dev
)
1100 /* If we get here, some higher level has decided we are broken.
1101 There should really be a "kick me" function call instead. */
1102 netdev_warn(dev
, CARDNAME
": transmit timed out, %s?\n",
1103 tx_done(dev
) ? "IRQ conflict" : "network cable problem");
1104 /* "kick" the adaptor */
1105 smc_reset( dev
->base_addr
);
1106 smc_enable( dev
->base_addr
);
1107 netif_trans_update(dev
); /* prevent tx timeout */
1108 /* clear anything saved */
1109 ((struct smc_local
*)netdev_priv(dev
))->saved_skb
= NULL
;
1110 netif_wake_queue(dev
);
1113 /*-------------------------------------------------------------
1115 . smc_rcv - receive a packet from the card
1117 . There is ( at least ) a packet waiting to be read from
1121 . o If an error, record it
1122 . o otherwise, read in the packet
1123 --------------------------------------------------------------
1125 static void smc_rcv(struct net_device
*dev
)
1127 int ioaddr
= dev
->base_addr
;
1134 packet_number
= inw( ioaddr
+ FIFO_PORTS
);
1136 if ( packet_number
& FP_RXEMPTY
) {
1137 /* we got called , but nothing was on the FIFO */
1138 PRINTK((CARDNAME
": WARNING: smc_rcv with nothing on FIFO.\n"));
1139 /* don't need to restore anything */
1143 /* start reading from the start of the packet */
1144 outw( PTR_READ
| PTR_RCV
| PTR_AUTOINC
, ioaddr
+ POINTER
);
1146 /* First two words are status and packet_length */
1147 status
= inw( ioaddr
+ DATA_1
);
1148 packet_length
= inw( ioaddr
+ DATA_1
);
1150 packet_length
&= 0x07ff; /* mask off top bits */
1152 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status
, packet_length
));
1154 . the packet length contains 3 extra words :
1155 . status, length, and an extra word with an odd byte .
1159 if ( !(status
& RS_ERRORS
) ){
1160 /* do stuff to make a new packet */
1161 struct sk_buff
* skb
;
1164 /* read one extra byte */
1165 if ( status
& RS_ODDFRAME
)
1168 /* set multicast stats */
1169 if ( status
& RS_MULTICAST
)
1170 dev
->stats
.multicast
++;
1172 skb
= netdev_alloc_skb(dev
, packet_length
+ 5);
1173 if ( skb
== NULL
) {
1174 dev
->stats
.rx_dropped
++;
1179 ! This should work without alignment, but it could be
1183 skb_reserve( skb
, 2 ); /* 16 bit alignment */
1185 data
= skb_put( skb
, packet_length
);
1188 /* QUESTION: Like in the TX routine, do I want
1189 to send the DWORDs or the bytes first, or some
1190 mixture. A mixture might improve already slow PIO
1192 PRINTK3((" Reading %d dwords (and %d bytes)\n",
1193 packet_length
>> 2, packet_length
& 3 ));
1194 insl(ioaddr
+ DATA_1
, data
, packet_length
>> 2 );
1195 /* read the left over bytes */
1196 insb( ioaddr
+ DATA_1
, data
+ (packet_length
& 0xFFFFFC),
1197 packet_length
& 0x3 );
1199 PRINTK3((" Reading %d words and %d byte(s)\n",
1200 (packet_length
>> 1 ), packet_length
& 1 ));
1201 insw(ioaddr
+ DATA_1
, data
, packet_length
>> 1);
1202 if ( packet_length
& 1 ) {
1203 data
+= packet_length
& ~1;
1204 *(data
++) = inb( ioaddr
+ DATA_1
);
1208 print_packet( data
, packet_length
);
1211 skb
->protocol
= eth_type_trans(skb
, dev
);
1213 dev
->stats
.rx_packets
++;
1214 dev
->stats
.rx_bytes
+= packet_length
;
1217 dev
->stats
.rx_errors
++;
1219 if ( status
& RS_ALGNERR
) dev
->stats
.rx_frame_errors
++;
1220 if ( status
& (RS_TOOSHORT
| RS_TOOLONG
) )
1221 dev
->stats
.rx_length_errors
++;
1222 if ( status
& RS_BADCRC
) dev
->stats
.rx_crc_errors
++;
1226 /* error or good, tell the card to get rid of this packet */
1227 outw( MC_RELEASE
, ioaddr
+ MMU_CMD
);
1231 /*************************************************************************
1234 . Purpose: Handle a transmit error message. This will only be called
1235 . when an error, because of the AUTO_RELEASE mode.
1238 . Save pointer and packet no
1239 . Get the packet no from the top of the queue
1240 . check if it's valid ( if not, is this an error??? )
1241 . read the status word
1243 . ( resend? Not really, since we don't want old packets around )
1244 . Restore saved values
1245 ************************************************************************/
1246 static void smc_tx( struct net_device
* dev
)
1248 int ioaddr
= dev
->base_addr
;
1249 struct smc_local
*lp
= netdev_priv(dev
);
1257 saved_packet
= inb( ioaddr
+ PNR_ARR
);
1258 packet_no
= inw( ioaddr
+ FIFO_PORTS
);
1261 /* select this as the packet to read from */
1262 outb( packet_no
, ioaddr
+ PNR_ARR
);
1264 /* read the first word from this packet */
1265 outw( PTR_AUTOINC
| PTR_READ
, ioaddr
+ POINTER
);
1267 tx_status
= inw( ioaddr
+ DATA_1
);
1268 PRINTK3((CARDNAME
": TX DONE STATUS: %4x\n", tx_status
));
1270 dev
->stats
.tx_errors
++;
1271 if ( tx_status
& TS_LOSTCAR
) dev
->stats
.tx_carrier_errors
++;
1272 if ( tx_status
& TS_LATCOL
) {
1273 netdev_dbg(dev
, CARDNAME
": Late collision occurred on last xmit.\n");
1274 dev
->stats
.tx_window_errors
++;
1277 if ( tx_status
& TS_16COL
) { ... }
1280 if ( tx_status
& TS_SUCCESS
) {
1281 netdev_info(dev
, CARDNAME
": Successful packet caused interrupt\n");
1283 /* re-enable transmit */
1284 SMC_SELECT_BANK( 0 );
1285 outw( inw( ioaddr
+ TCR
) | TCR_ENABLE
, ioaddr
+ TCR
);
1287 /* kill the packet */
1288 SMC_SELECT_BANK( 2 );
1289 outw( MC_FREEPKT
, ioaddr
+ MMU_CMD
);
1291 /* one less packet waiting for me */
1292 lp
->packets_waiting
--;
1294 outb( saved_packet
, ioaddr
+ PNR_ARR
);
1297 /*--------------------------------------------------------------------
1299 . This is the main routine of the driver, to handle the device when
1300 . it needs some attention.
1303 . first, save state of the chipset
1304 . branch off into routines to handle each case, and acknowledge
1305 . each to the interrupt register
1306 . and finally restore state.
1308 ---------------------------------------------------------------------*/
1310 static irqreturn_t
smc_interrupt(int irq
, void * dev_id
)
1312 struct net_device
*dev
= dev_id
;
1313 int ioaddr
= dev
->base_addr
;
1314 struct smc_local
*lp
= netdev_priv(dev
);
1320 /* state registers */
1326 PRINTK3((CARDNAME
": SMC interrupt started\n"));
1328 saved_bank
= inw( ioaddr
+ BANK_SELECT
);
1331 saved_pointer
= inw( ioaddr
+ POINTER
);
1333 mask
= inb( ioaddr
+ INT_MASK
);
1334 /* clear all interrupts */
1335 outb( 0, ioaddr
+ INT_MASK
);
1338 /* set a timeout value, so I don't stay here forever */
1341 PRINTK2((KERN_WARNING CARDNAME
": MASK IS %x\n", mask
));
1343 /* read the status flag, and mask it */
1344 status
= inb( ioaddr
+ INTERRUPT
) & mask
;
1350 PRINTK3((KERN_WARNING CARDNAME
1351 ": Handling interrupt status %x\n", status
));
1353 if (status
& IM_RCV_INT
) {
1354 /* Got a packet(s). */
1355 PRINTK2((KERN_WARNING CARDNAME
1356 ": Receive Interrupt\n"));
1358 } else if (status
& IM_TX_INT
) {
1359 PRINTK2((KERN_WARNING CARDNAME
1360 ": TX ERROR handled\n"));
1362 outb(IM_TX_INT
, ioaddr
+ INTERRUPT
);
1363 } else if (status
& IM_TX_EMPTY_INT
) {
1365 SMC_SELECT_BANK( 0 );
1366 card_stats
= inw( ioaddr
+ COUNTER
);
1367 /* single collisions */
1368 dev
->stats
.collisions
+= card_stats
& 0xF;
1370 /* multiple collisions */
1371 dev
->stats
.collisions
+= card_stats
& 0xF;
1373 /* these are for when linux supports these statistics */
1375 SMC_SELECT_BANK( 2 );
1376 PRINTK2((KERN_WARNING CARDNAME
1377 ": TX_BUFFER_EMPTY handled\n"));
1378 outb( IM_TX_EMPTY_INT
, ioaddr
+ INTERRUPT
);
1379 mask
&= ~IM_TX_EMPTY_INT
;
1380 dev
->stats
.tx_packets
+= lp
->packets_waiting
;
1381 lp
->packets_waiting
= 0;
1383 } else if (status
& IM_ALLOC_INT
) {
1384 PRINTK2((KERN_DEBUG CARDNAME
1385 ": Allocation interrupt\n"));
1386 /* clear this interrupt so it doesn't happen again */
1387 mask
&= ~IM_ALLOC_INT
;
1389 smc_hardware_send_packet( dev
);
1391 /* enable xmit interrupts based on this */
1392 mask
|= ( IM_TX_EMPTY_INT
| IM_TX_INT
);
1394 /* and let the card send more packets to me */
1395 netif_wake_queue(dev
);
1397 PRINTK2((CARDNAME
": Handoff done successfully.\n"));
1398 } else if (status
& IM_RX_OVRN_INT
) {
1399 dev
->stats
.rx_errors
++;
1400 dev
->stats
.rx_fifo_errors
++;
1401 outb( IM_RX_OVRN_INT
, ioaddr
+ INTERRUPT
);
1402 } else if (status
& IM_EPH_INT
) {
1403 PRINTK((CARDNAME
": UNSUPPORTED: EPH INTERRUPT\n"));
1404 } else if (status
& IM_ERCV_INT
) {
1405 PRINTK((CARDNAME
": UNSUPPORTED: ERCV INTERRUPT\n"));
1406 outb( IM_ERCV_INT
, ioaddr
+ INTERRUPT
);
1408 } while ( timeout
-- );
1411 /* restore state register */
1412 SMC_SELECT_BANK( 2 );
1413 outb( mask
, ioaddr
+ INT_MASK
);
1415 PRINTK3((KERN_WARNING CARDNAME
": MASK is now %x\n", mask
));
1416 outw( saved_pointer
, ioaddr
+ POINTER
);
1418 SMC_SELECT_BANK( saved_bank
);
1420 PRINTK3((CARDNAME
": Interrupt done\n"));
1421 return IRQ_RETVAL(handled
);
1425 /*----------------------------------------------------
1428 . this makes the board clean up everything that it can
1429 . and not talk to the outside world. Caused by
1430 . an 'ifconfig ethX down'
1432 -----------------------------------------------------*/
1433 static int smc_close(struct net_device
*dev
)
1435 netif_stop_queue(dev
);
1436 /* clear everything */
1437 smc_shutdown( dev
->base_addr
);
1439 /* Update the statistics here. */
1443 /*-----------------------------------------------------------
1444 . smc_set_multicast_list
1446 . This routine will, depending on the values passed to it,
1447 . either make it accept multicast packets, go into
1448 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1449 . a select set of multicast packets
1451 static void smc_set_multicast_list(struct net_device
*dev
)
1453 short ioaddr
= dev
->base_addr
;
1456 if ( dev
->flags
& IFF_PROMISC
)
1457 outw( inw(ioaddr
+ RCR
) | RCR_PROMISC
, ioaddr
+ RCR
);
1459 /* BUG? I never disable promiscuous mode if multicasting was turned on.
1460 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1461 when promiscuous mode is turned on.
1464 /* Here, I am setting this to accept all multicast packets.
1465 I don't need to zero the multicast table, because the flag is
1466 checked before the table is
1468 else if (dev
->flags
& IFF_ALLMULTI
)
1469 outw( inw(ioaddr
+ RCR
) | RCR_ALMUL
, ioaddr
+ RCR
);
1471 /* We just get all multicast packets even if we only want them
1472 . from one source. This will be changed at some future
1474 else if (!netdev_mc_empty(dev
)) {
1475 /* support hardware multicasting */
1477 /* be sure I get rid of flags I might have set */
1478 outw( inw( ioaddr
+ RCR
) & ~(RCR_PROMISC
| RCR_ALMUL
),
1480 /* NOTE: this has to set the bank, so make sure it is the
1481 last thing called. The bank is set to zero at the top */
1482 smc_setmulticast(ioaddr
, dev
);
1485 outw( inw( ioaddr
+ RCR
) & ~(RCR_PROMISC
| RCR_ALMUL
),
1489 since I'm disabling all multicast entirely, I need to
1490 clear the multicast list
1492 SMC_SELECT_BANK( 3 );
1493 outw( 0, ioaddr
+ MULTICAST1
);
1494 outw( 0, ioaddr
+ MULTICAST2
);
1495 outw( 0, ioaddr
+ MULTICAST3
);
1496 outw( 0, ioaddr
+ MULTICAST4
);
1502 static struct net_device
*devSMC9194
;
1503 MODULE_LICENSE("GPL");
1505 module_param(io
, int, 0);
1506 module_param(irq
, int, 0);
1507 module_param(ifport
, int, 0);
1508 MODULE_PARM_DESC(io
, "SMC 99194 I/O base address");
1509 MODULE_PARM_DESC(irq
, "SMC 99194 IRQ number");
1510 MODULE_PARM_DESC(ifport
, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1512 int __init
init_module(void)
1516 CARDNAME
": You shouldn't use auto-probing with insmod!\n" );
1518 /* copy the parameters from insmod into the device structure */
1519 devSMC9194
= smc_init(-1);
1520 return PTR_ERR_OR_ZERO(devSMC9194
);
1523 void __exit
cleanup_module(void)
1525 unregister_netdev(devSMC9194
);
1526 free_irq(devSMC9194
->irq
, devSMC9194
);
1527 release_region(devSMC9194
->base_addr
, SMC_IO_EXTENT
);
1528 free_netdev(devSMC9194
);