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)\n";
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/slab.h>
68 #include <linux/string.h>
69 #include <linux/init.h>
70 #include <linux/crc32.h>
71 #include <linux/errno.h>
72 #include <linux/netdevice.h>
73 #include <linux/etherdevice.h>
74 #include <linux/skbuff.h>
76 #include <asm/bitops.h>
81 /*------------------------------------------------------------------------
83 . Configuration options, for the experienced user to change.
85 -------------------------------------------------------------------------*/
88 . Do you want to use 32 bit xfers? This should work on all chips, as
89 . the chipset is designed to accommodate them.
98 .the SMC9194 can be at any of the following port addresses. To change,
99 .for a slightly different card, you can add it to the array. Keep in
100 .mind that the array must end in zero.
102 static unsigned int smc_portlist[] __initdata = {
103 0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
104 0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0, 0
108 . Wait time for memory to be free. This probably shouldn't be
109 . tuned that much, as waiting for this means nothing else happens
112 #define MEMORY_WAIT_TIME 16
117 . 0 for normal operation
118 . 1 for slightly more details
119 . >2 for various levels of increasingly useless information
120 . 2 for interrupt tracking, status flags
121 . 3 for packet dumps, etc.
126 #define PRINTK3(x) printk x
132 #define PRINTK2(x) printk x
138 #define PRINTK(x) printk x
144 /*------------------------------------------------------------------------
146 . The internal workings of the driver. If you are changing anything
147 . here with the SMC stuff, you should have the datasheet and known
148 . what you are doing.
150 -------------------------------------------------------------------------*/
151 #define CARDNAME "SMC9194"
154 /* store this information for the driver.. */
157 these are things that the kernel wants me to keep, so users
158 can find out semi-useless statistics of how well the card is
161 struct net_device_stats stats;
164 If I have to wait until memory is available to send
165 a packet, I will store the skbuff here, until I get the
166 desired memory. Then, I'll send it out and free it.
168 struct sk_buff * saved_skb;
171 . This keeps track of how many packets that I have
172 . sent out. When an TX_EMPTY interrupt comes, I know
173 . that all of these have been sent.
179 /*-----------------------------------------------------------------
181 . The driver can be entered at any of the following entry points.
183 .------------------------------------------------------------------ */
186 . This is called by register_netdev(). It is responsible for
187 . checking the portlist for the SMC9000 series chipset. If it finds
188 . one, then it will initialize the device, find the hardware information,
189 . and sets up the appropriate device parameters.
190 . NOTE: Interrupts are *OFF* when this procedure is called.
192 . NB:This shouldn't be static since it is referred to externally.
194 struct net_device *smc_init(int unit);
197 . The kernel calls this function when someone wants to use the device,
198 . typically 'ifconfig ethX up'.
200 static int smc_open(struct net_device *dev);
203 . Our watchdog timed out. Called by the networking layer
205 static void smc_timeout(struct net_device *dev);
208 . This is called by the kernel in response to 'ifconfig ethX down'. It
209 . is responsible for cleaning up everything that the open routine
210 . does, and maybe putting the card into a powerdown state.
212 static int smc_close(struct net_device *dev);
215 . This routine allows the proc file system to query the driver's
218 static struct net_device_stats * smc_query_statistics( struct net_device *dev);
221 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
222 . programs ) and multicast modes.
224 static void smc_set_multicast_list(struct net_device *dev);
227 /*---------------------------------------------------------------
229 . Interrupt level calls..
231 ----------------------------------------------------------------*/
234 . Handles the actual interrupt
236 static irqreturn_t smc_interrupt(int irq, void *, struct pt_regs *regs);
238 . This is a separate procedure to handle the receipt of a packet, to
239 . leave the interrupt code looking slightly cleaner
241 static inline void smc_rcv( struct net_device *dev );
243 . This handles a TX interrupt, which is only called when an error
244 . relating to a packet is sent.
246 static inline void smc_tx( struct net_device * dev );
249 ------------------------------------------------------------
253 ------------------------------------------------------------
257 . Test if a given location contains a chip, trying to cause as
258 . little damage as possible if it's not a SMC chip.
260 static int smc_probe(struct net_device *dev, int ioaddr);
263 . A rather simple routine to print out a packet for debugging purposes.
266 static void print_packet( byte *, int );
269 #define tx_done(dev) 1
271 /* this is called to actually send the packet to the chip */
272 static void smc_hardware_send_packet( struct net_device * dev );
274 /* Since I am not sure if I will have enough room in the chip's ram
275 . to store the packet, I call this routine, which either sends it
276 . now, or generates an interrupt when the card is ready for the
278 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );
280 /* this does a soft reset on the device */
281 static void smc_reset( int ioaddr );
283 /* Enable Interrupts, Receive, and Transmit */
284 static void smc_enable( int ioaddr );
286 /* this puts the device in an inactive state */
287 static void smc_shutdown( int ioaddr );
289 /* This routine will find the IRQ of the driver if one is not
290 . specified in the input to the device. */
291 static int smc_findirq( int ioaddr );
294 . Function: smc_reset( int ioaddr )
296 . This sets the SMC91xx chip to its normal state, hopefully from whatever
297 . mess that any other DOS driver has put it in.
299 . Maybe I should reset more registers to defaults in here? SOFTRESET should
303 . 1. send a SOFT RESET
304 . 2. wait for it to finish
305 . 3. enable autorelease mode
306 . 4. reset the memory management unit
307 . 5. clear all interrupts
310 static void smc_reset( int ioaddr )
312 /* This resets the registers mostly to defaults, but doesn't
313 affect EEPROM. That seems unnecessary */
314 SMC_SELECT_BANK( 0 );
315 outw( RCR_SOFTRESET, ioaddr + RCR );
317 /* this should pause enough for the chip to be happy */
320 /* Set the transmit and receive configuration registers to
322 outw( RCR_CLEAR, ioaddr + RCR );
323 outw( TCR_CLEAR, ioaddr + TCR );
325 /* set the control register to automatically
326 release successfully transmitted packets, to make the best
327 use out of our limited memory */
328 SMC_SELECT_BANK( 1 );
329 outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
332 SMC_SELECT_BANK( 2 );
333 outw( MC_RESET, ioaddr + MMU_CMD );
335 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
336 but this is a place where future chipsets _COULD_ break. Be wary
337 of issuing another MMU command right after this */
339 outb( 0, ioaddr + INT_MASK );
343 . Function: smc_enable
344 . Purpose: let the chip talk to the outside work
346 . 1. Enable the transmitter
347 . 2. Enable the receiver
348 . 3. Enable interrupts
350 static void smc_enable( int ioaddr )
352 SMC_SELECT_BANK( 0 );
353 /* see the header file for options in TCR/RCR NORMAL*/
354 outw( TCR_NORMAL, ioaddr + TCR );
355 outw( RCR_NORMAL, ioaddr + RCR );
357 /* now, enable interrupts */
358 SMC_SELECT_BANK( 2 );
359 outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
363 . Function: smc_shutdown
364 . Purpose: closes down the SMC91xxx chip.
366 . 1. zero the interrupt mask
367 . 2. clear the enable receive flag
368 . 3. clear the enable xmit flags
371 . (1) maybe utilize power down mode.
372 . Why not yet? Because while the chip will go into power down mode,
373 . the manual says that it will wake up in response to any I/O requests
374 . in the register space. Empirical results do not show this working.
376 static void smc_shutdown( int ioaddr )
378 /* no more interrupts for me */
379 SMC_SELECT_BANK( 2 );
380 outb( 0, ioaddr + INT_MASK );
382 /* and tell the card to stay away from that nasty outside world */
383 SMC_SELECT_BANK( 0 );
384 outb( RCR_CLEAR, ioaddr + RCR );
385 outb( TCR_CLEAR, ioaddr + TCR );
387 /* finally, shut the chip down */
388 SMC_SELECT_BANK( 1 );
389 outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL );
395 . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
397 . This sets the internal hardware table to filter out unwanted multicast
398 . packets before they take up memory.
400 . The SMC chip uses a hash table where the high 6 bits of the CRC of
401 . address are the offset into the table. If that bit is 1, then the
402 . multicast packet is accepted. Otherwise, it's dropped silently.
404 . To use the 6 bits as an offset into the table, the high 3 bits are the
405 . number of the 8 bit register, while the low 3 bits are the bit within
408 . This routine is based very heavily on the one provided by Peter Cammaert.
412 static void smc_setmulticast( int ioaddr, int count, struct dev_mc_list * addrs ) {
414 unsigned char multicast_table[ 8 ];
415 struct dev_mc_list * cur_addr;
416 /* table for flipping the order of 3 bits */
417 unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
419 /* start with a table of all zeros: reject all */
420 memset( multicast_table, 0, sizeof( multicast_table ) );
423 for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next ) {
426 /* do we have a pointer here? */
429 /* make sure this is a multicast address - shouldn't this
430 be a given if we have it here ? */
431 if ( !( *cur_addr->dmi_addr & 1 ) )
434 /* only use the low order bits */
435 position = ether_crc_le(6, cur_addr->dmi_addr) & 0x3f;
437 /* do some messy swapping to put the bit in the right spot */
438 multicast_table[invert3[position&7]] |=
439 (1<<invert3[(position>>3)&7]);
442 /* now, the table can be loaded into the chipset */
443 SMC_SELECT_BANK( 3 );
445 for ( i = 0; i < 8 ; i++ ) {
446 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
451 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
453 . Attempt to allocate memory for a packet, if chip-memory is not
454 . available, then tell the card to generate an interrupt when it
459 . o if the saved_skb is not currently null, then drop this packet
460 . on the floor. This should never happen, because of TBUSY.
461 . o if the saved_skb is null, then replace it with the current packet,
462 . o See if I can sending it now.
463 . o (NO): Enable interrupts and let the interrupt handler deal with it.
464 . o (YES):Send it now.
466 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
468 struct smc_local *lp = netdev_priv(dev);
469 unsigned short ioaddr = dev->base_addr;
471 unsigned short numPages;
474 netif_stop_queue(dev);
475 /* Well, I want to send the packet.. but I don't know
476 if I can send it right now... */
478 if ( lp->saved_skb) {
479 /* THIS SHOULD NEVER HAPPEN. */
480 lp->stats.tx_aborted_errors++;
481 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
488 if (length < ETH_ZLEN) {
489 skb = skb_padto(skb, ETH_ZLEN);
491 netif_wake_queue(dev);
498 ** The MMU wants the number of pages to be the number of 256 bytes
499 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
501 ** Pkt size for allocating is data length +6 (for additional status words,
502 ** length and ctl!) If odd size last byte is included in this header.
504 numPages = ((length & 0xfffe) + 6) / 256;
507 printk(CARDNAME": Far too big packet error. \n");
508 /* freeing the packet is a good thing here... but should
509 . any packets of this size get down here? */
511 lp->saved_skb = NULL;
512 /* this IS an error, but, i don't want the skb saved */
513 netif_wake_queue(dev);
516 /* either way, a packet is waiting now */
517 lp->packets_waiting++;
519 /* now, try to allocate the memory */
520 SMC_SELECT_BANK( 2 );
521 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
525 . wait a short amount of time.. if I can send a packet now, I send
526 . it now. Otherwise, I enable an interrupt and wait for one to be
529 . I could have handled this a slightly different way, by checking to
530 . see if any memory was available in the FREE MEMORY register. However,
531 . either way, I need to generate an allocation, and the allocation works
532 . no matter what, so I saw no point in checking free memory.
534 time_out = MEMORY_WAIT_TIME;
538 status = inb( ioaddr + INTERRUPT );
539 if ( status & IM_ALLOC_INT ) {
540 /* acknowledge the interrupt */
541 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
544 } while ( -- time_out );
547 /* oh well, wait until the chip finds memory later */
548 SMC_ENABLE_INT( IM_ALLOC_INT );
549 PRINTK2((CARDNAME": memory allocation deferred. \n"));
550 /* it's deferred, but I'll handle it later */
553 /* or YES! I can send the packet now.. */
554 smc_hardware_send_packet(dev);
555 netif_wake_queue(dev);
560 . Function: smc_hardware_send_packet(struct net_device * )
562 . This sends the actual packet to the SMC9xxx chip.
565 . First, see if a saved_skb is available.
566 . ( this should NOT be called if there is no 'saved_skb'
567 . Now, find the packet number that the chip allocated
568 . Point the data pointers at it in memory
569 . Set the length word in the chip's memory
570 . Dump the packet to chip memory
571 . Check if a last byte is needed ( odd length packet )
572 . if so, set the control flag right
573 . Tell the card to send it
574 . Enable the transmit interrupt, so I know if it failed
575 . Free the kernel data if I actually sent it.
577 static void smc_hardware_send_packet( struct net_device * dev )
579 struct smc_local *lp = netdev_priv(dev);
581 struct sk_buff * skb = lp->saved_skb;
583 unsigned short ioaddr;
586 ioaddr = dev->base_addr;
589 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
592 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
595 /* If I get here, I _know_ there is a packet slot waiting for me */
596 packet_no = inb( ioaddr + PNR_ARR + 1 );
597 if ( packet_no & 0x80 ) {
598 /* or isn't there? BAD CHIP! */
599 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
600 dev_kfree_skb_any(skb);
601 lp->saved_skb = NULL;
602 netif_wake_queue(dev);
606 /* we have a packet address, so tell the card to use it */
607 outb( packet_no, ioaddr + PNR_ARR );
609 /* point to the beginning of the packet */
610 outw( PTR_AUTOINC , ioaddr + POINTER );
612 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
614 print_packet( buf, length );
617 /* send the packet length ( +6 for status, length and ctl byte )
618 and the status word ( set to zeros ) */
620 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
622 outw( 0, ioaddr + DATA_1 );
623 /* send the packet length ( +6 for status words, length, and ctl*/
624 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
625 outb( (length+6) >> 8 , ioaddr + DATA_1 );
628 /* send the actual data
629 . I _think_ it's faster to send the longs first, and then
630 . mop up by sending the last word. It depends heavily
631 . on alignment, at least on the 486. Maybe it would be
632 . a good idea to check which is optimal? But that could take
633 . almost as much time as is saved?
636 if ( length & 0x2 ) {
637 outsl(ioaddr + DATA_1, buf, length >> 2 );
638 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
641 outsl(ioaddr + DATA_1, buf, length >> 2 );
643 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
645 /* Send the last byte, if there is one. */
647 if ( (length & 1) == 0 ) {
648 outw( 0, ioaddr + DATA_1 );
650 outb( buf[length -1 ], ioaddr + DATA_1 );
651 outb( 0x20, ioaddr + DATA_1);
654 /* enable the interrupts */
655 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
657 /* and let the chipset deal with it */
658 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
660 PRINTK2((CARDNAME": Sent packet of length %d \n",length));
662 lp->saved_skb = NULL;
663 dev_kfree_skb_any (skb);
665 dev->trans_start = jiffies;
667 /* we can send another packet */
668 netif_wake_queue(dev);
673 /*-------------------------------------------------------------------------
677 | dev->base_addr == 0, try to find all possible locations
678 | dev->base_addr == 1, return failure code
679 | dev->base_addr == 2, always allocate space, and return success
680 | dev->base_addr == <anything else> this is the address to check
683 | pointer to net_device or ERR_PTR(error)
685 ---------------------------------------------------------------------------
691 struct net_device * __init smc_init(int unit)
693 struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
698 return ERR_PTR(-ENODEV);
701 sprintf(dev->name, "eth%d", unit);
702 netdev_boot_setup_check(dev);
707 SET_MODULE_OWNER(dev);
709 if (io > 0x1ff) { /* Check a single specified location. */
710 err = smc_probe(dev, io);
711 } else if (io != 0) { /* Don't probe at all. */
714 for (port = smc_portlist; *port; port++) {
715 if (smc_probe(dev, *port) == 0)
723 err = register_netdev(dev);
728 free_irq(dev->irq, dev);
729 release_region(dev->base_addr, SMC_IO_EXTENT);
735 /*----------------------------------------------------------------------
738 . This routine has a simple purpose -- make the SMC chip generate an
739 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
740 ------------------------------------------------------------------------
742 int __init smc_findirq( int ioaddr )
745 unsigned long cookie;
748 cookie = probe_irq_on();
751 * What I try to do here is trigger an ALLOC_INT. This is done
752 * by allocating a small chunk of memory, which will give an interrupt
758 /* enable ALLOCation interrupts ONLY */
759 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
762 . Allocate 512 bytes of memory. Note that the chip was just
763 . reset so all the memory is available
765 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
768 . Wait until positive that the interrupt has been generated
773 int_status = inb( ioaddr + INTERRUPT );
775 if ( int_status & IM_ALLOC_INT )
776 break; /* got the interrupt */
779 /* there is really nothing that I can do here if timeout fails,
780 as probe_irq_off will return a 0 anyway, which is what I
781 want in this case. Plus, the clean up is needed in both
785 On a fast machine, the status might change before the interrupt
786 is given to the processor. This means that the interrupt was
787 never detected, and probe_irq_off fails to report anything.
788 This should fix probe_irq_* problems.
793 /* and disable all interrupts again */
794 outb( 0, ioaddr + INT_MASK );
796 /* and return what I found */
797 return probe_irq_off(cookie);
800 /*----------------------------------------------------------------------
801 . Function: smc_probe( int ioaddr )
804 . Tests to see if a given ioaddr points to an SMC9xxx chip.
805 . Returns a 0 on success
808 . (1) see if the high byte of BANK_SELECT is 0x33
809 . (2) compare the ioaddr with the base register's address
810 . (3) see if I recognize the chip ID in the appropriate register
812 .---------------------------------------------------------------------
815 /*---------------------------------------------------------------
816 . Here I do typical initialization tasks.
818 . o Initialize the structure if needed
819 . o print out my vanity message if not done so already
820 . o print out what type of hardware is detected
821 . o print out the ethernet address
823 . o set up my private data
824 . o configure the dev structure with my subroutines
825 . o actually GRAB the irq.
827 .-----------------------------------------------------------------
829 static int __init smc_probe(struct net_device *dev, int ioaddr)
831 int i, memory, retval;
832 static unsigned version_printed;
835 const char *version_string;
836 const char *if_string;
839 word revision_register;
840 word base_address_register;
841 word configuration_register;
842 word memory_info_register;
843 word memory_cfg_register;
845 /* Grab the region so that no one else tries to probe our ioports. */
846 if (!request_region(ioaddr, SMC_IO_EXTENT, dev->name))
850 dev->if_port = ifport;
852 /* First, see if the high byte is 0x33 */
853 bank = inw( ioaddr + BANK_SELECT );
854 if ( (bank & 0xFF00) != 0x3300 ) {
858 /* The above MIGHT indicate a device, but I need to write to further
860 outw( 0x0, ioaddr + BANK_SELECT );
861 bank = inw( ioaddr + BANK_SELECT );
862 if ( (bank & 0xFF00 ) != 0x3300 ) {
866 /* well, we've already written once, so hopefully another time won't
867 hurt. This time, I need to switch the bank register to bank 1,
868 so I can access the base address register */
870 base_address_register = inw( ioaddr + BASE );
871 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
872 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
873 "Probably not a SMC chip\n",
874 ioaddr, base_address_register >> 3 & 0x3E0 );
875 /* well, the base address register didn't match. Must not have
876 been a SMC chip after all. */
881 /* check if the revision register is something that I recognize.
882 These might need to be added to later, as future revisions
885 revision_register = inw( ioaddr + REVISION );
886 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
887 /* I don't recognize this chip, so... */
888 printk(CARDNAME ": IO %x: Unrecognized revision register:"
889 " %x, Contact author. \n", ioaddr, revision_register );
895 /* at this point I'll assume that the chip is an SMC9xxx.
896 It might be prudent to check a listing of MAC addresses
897 against the hardware address, or do some other tests. */
899 if (version_printed++ == 0)
900 printk("%s", version);
902 /* fill in some of the fields */
903 dev->base_addr = ioaddr;
906 . Get the MAC address ( bank 1, regs 4 - 9 )
908 SMC_SELECT_BANK( 1 );
909 for ( i = 0; i < 6; i += 2 ) {
912 address = inw( ioaddr + ADDR0 + i );
913 dev->dev_addr[ i + 1] = address >> 8;
914 dev->dev_addr[ i ] = address & 0xFF;
917 /* get the memory information */
919 SMC_SELECT_BANK( 0 );
920 memory_info_register = inw( ioaddr + MIR );
921 memory_cfg_register = inw( ioaddr + MCR );
922 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
923 memory *= 256 * ( memory_info_register & 0xFF );
926 Now, I want to find out more about the chip. This is sort of
927 redundant, but it's cleaner to have it in both, rather than having
928 one VERY long probe procedure.
931 revision_register = inw( ioaddr + REVISION );
932 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
933 if ( !version_string ) {
934 /* I shouldn't get here because this call was done before.... */
939 /* is it using AUI or 10BaseT ? */
940 if ( dev->if_port == 0 ) {
942 configuration_register = inw( ioaddr + CONFIG );
943 if ( configuration_register & CFG_AUI_SELECT )
948 if_string = interfaces[ dev->if_port - 1 ];
950 /* now, reset the chip, and put it into a known state */
954 . If dev->irq is 0, then the device has to be banged on to see
957 . This banging doesn't always detect the IRQ, for unknown reasons.
958 . a workaround is to reset the chip and try again.
960 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
961 . be what is requested on the command line. I don't do that, mostly
962 . because the card that I have uses a non-standard method of accessing
963 . the IRQs, and because this _should_ work in most configurations.
965 . Specifying an IRQ is done with the assumption that the user knows
966 . what (s)he is doing. No checking is done!!!!
969 if ( dev->irq < 2 ) {
974 dev->irq = smc_findirq( ioaddr );
977 /* kick the card and try again */
981 if (dev->irq == 0 ) {
982 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
987 /* now, print out the card info, in a short format.. */
989 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
990 version_string, revision_register & 0xF, ioaddr, dev->irq,
993 . Print the Ethernet address
996 for (i = 0; i < 5; i++)
997 printk("%2.2x:", dev->dev_addr[i] );
998 printk("%2.2x \n", dev->dev_addr[5] );
1000 /* set the private data to zero by default */
1001 memset(dev->priv, 0, sizeof(struct smc_local));
1004 retval = request_irq(dev->irq, &smc_interrupt, 0, dev->name, dev);
1006 printk("%s: unable to get IRQ %d (irqval=%d).\n", dev->name,
1011 dev->open = smc_open;
1012 dev->stop = smc_close;
1013 dev->hard_start_xmit = smc_wait_to_send_packet;
1014 dev->tx_timeout = smc_timeout;
1015 dev->watchdog_timeo = HZ/20;
1016 dev->get_stats = smc_query_statistics;
1017 dev->set_multicast_list = smc_set_multicast_list;
1022 release_region(ioaddr, SMC_IO_EXTENT);
1027 static void print_packet( byte * buf, int length )
1034 printk("Packet of length %d \n", length );
1035 lines = length / 16;
1036 remainder = length % 16;
1038 for ( i = 0; i < lines ; i ++ ) {
1041 for ( cur = 0; cur < 8; cur ++ ) {
1046 printk("%02x%02x ", a, b );
1050 for ( i = 0; i < remainder/2 ; i++ ) {
1055 printk("%02x%02x ", a, b );
1064 * Open and Initialize the board
1066 * Set up everything, reset the card, etc ..
1069 static int smc_open(struct net_device *dev)
1071 int ioaddr = dev->base_addr;
1073 int i; /* used to set hw ethernet address */
1075 /* clear out all the junk that was put here before... */
1076 memset(dev->priv, 0, sizeof(struct smc_local));
1078 /* reset the hardware */
1080 smc_reset( ioaddr );
1081 smc_enable( ioaddr );
1083 /* Select which interface to use */
1085 SMC_SELECT_BANK( 1 );
1086 if ( dev->if_port == 1 ) {
1087 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1090 else if ( dev->if_port == 2 ) {
1091 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1096 According to Becker, I have to set the hardware address
1097 at this point, because the (l)user can set it with an
1098 ioctl. Easily done...
1100 SMC_SELECT_BANK( 1 );
1101 for ( i = 0; i < 6; i += 2 ) {
1104 address = dev->dev_addr[ i + 1 ] << 8 ;
1105 address |= dev->dev_addr[ i ];
1106 outw( address, ioaddr + ADDR0 + i );
1109 netif_start_queue(dev);
1113 /*--------------------------------------------------------
1114 . Called by the kernel to send a packet out into the void
1115 . of the net. This routine is largely based on
1116 . skeleton.c, from Becker.
1117 .--------------------------------------------------------
1120 static void smc_timeout(struct net_device *dev)
1122 /* If we get here, some higher level has decided we are broken.
1123 There should really be a "kick me" function call instead. */
1124 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1125 tx_done(dev) ? "IRQ conflict" :
1126 "network cable problem");
1127 /* "kick" the adaptor */
1128 smc_reset( dev->base_addr );
1129 smc_enable( dev->base_addr );
1130 dev->trans_start = jiffies;
1131 /* clear anything saved */
1132 ((struct smc_local *)dev->priv)->saved_skb = NULL;
1133 netif_wake_queue(dev);
1136 /*--------------------------------------------------------------------
1138 . This is the main routine of the driver, to handle the device when
1139 . it needs some attention.
1142 . first, save state of the chipset
1143 . branch off into routines to handle each case, and acknowledge
1144 . each to the interrupt register
1145 . and finally restore state.
1147 ---------------------------------------------------------------------*/
1149 static irqreturn_t smc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
1151 struct net_device *dev = dev_id;
1152 int ioaddr = dev->base_addr;
1153 struct smc_local *lp = netdev_priv(dev);
1159 /* state registers */
1165 PRINTK3((CARDNAME": SMC interrupt started \n"));
1167 saved_bank = inw( ioaddr + BANK_SELECT );
1170 saved_pointer = inw( ioaddr + POINTER );
1172 mask = inb( ioaddr + INT_MASK );
1173 /* clear all interrupts */
1174 outb( 0, ioaddr + INT_MASK );
1177 /* set a timeout value, so I don't stay here forever */
1180 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1182 /* read the status flag, and mask it */
1183 status = inb( ioaddr + INTERRUPT ) & mask;
1189 PRINTK3((KERN_WARNING CARDNAME
1190 ": Handling interrupt status %x \n", status ));
1192 if (status & IM_RCV_INT) {
1193 /* Got a packet(s). */
1194 PRINTK2((KERN_WARNING CARDNAME
1195 ": Receive Interrupt\n"));
1197 } else if (status & IM_TX_INT ) {
1198 PRINTK2((KERN_WARNING CARDNAME
1199 ": TX ERROR handled\n"));
1201 outb(IM_TX_INT, ioaddr + INTERRUPT );
1202 } else if (status & IM_TX_EMPTY_INT ) {
1204 SMC_SELECT_BANK( 0 );
1205 card_stats = inw( ioaddr + COUNTER );
1206 /* single collisions */
1207 lp->stats.collisions += card_stats & 0xF;
1209 /* multiple collisions */
1210 lp->stats.collisions += card_stats & 0xF;
1212 /* these are for when linux supports these statistics */
1214 SMC_SELECT_BANK( 2 );
1215 PRINTK2((KERN_WARNING CARDNAME
1216 ": TX_BUFFER_EMPTY handled\n"));
1217 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1218 mask &= ~IM_TX_EMPTY_INT;
1219 lp->stats.tx_packets += lp->packets_waiting;
1220 lp->packets_waiting = 0;
1222 } else if (status & IM_ALLOC_INT ) {
1223 PRINTK2((KERN_DEBUG CARDNAME
1224 ": Allocation interrupt \n"));
1225 /* clear this interrupt so it doesn't happen again */
1226 mask &= ~IM_ALLOC_INT;
1228 smc_hardware_send_packet( dev );
1230 /* enable xmit interrupts based on this */
1231 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1233 /* and let the card send more packets to me */
1234 netif_wake_queue(dev);
1236 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1237 } else if (status & IM_RX_OVRN_INT ) {
1238 lp->stats.rx_errors++;
1239 lp->stats.rx_fifo_errors++;
1240 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1241 } else if (status & IM_EPH_INT ) {
1242 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1243 } else if (status & IM_ERCV_INT ) {
1244 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1245 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1247 } while ( timeout -- );
1250 /* restore state register */
1251 SMC_SELECT_BANK( 2 );
1252 outb( mask, ioaddr + INT_MASK );
1254 PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1255 outw( saved_pointer, ioaddr + POINTER );
1257 SMC_SELECT_BANK( saved_bank );
1259 PRINTK3((CARDNAME ": Interrupt done\n"));
1260 return IRQ_RETVAL(handled);
1263 /*-------------------------------------------------------------
1265 . smc_rcv - receive a packet from the card
1267 . There is ( at least ) a packet waiting to be read from
1271 . o If an error, record it
1272 . o otherwise, read in the packet
1273 --------------------------------------------------------------
1275 static void smc_rcv(struct net_device *dev)
1277 struct smc_local *lp = netdev_priv(dev);
1278 int ioaddr = dev->base_addr;
1285 packet_number = inw( ioaddr + FIFO_PORTS );
1287 if ( packet_number & FP_RXEMPTY ) {
1288 /* we got called , but nothing was on the FIFO */
1289 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1290 /* don't need to restore anything */
1294 /* start reading from the start of the packet */
1295 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1297 /* First two words are status and packet_length */
1298 status = inw( ioaddr + DATA_1 );
1299 packet_length = inw( ioaddr + DATA_1 );
1301 packet_length &= 0x07ff; /* mask off top bits */
1303 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1305 . the packet length contains 3 extra words :
1306 . status, length, and an extra word with an odd byte .
1310 if ( !(status & RS_ERRORS ) ){
1311 /* do stuff to make a new packet */
1312 struct sk_buff * skb;
1315 /* read one extra byte */
1316 if ( status & RS_ODDFRAME )
1319 /* set multicast stats */
1320 if ( status & RS_MULTICAST )
1321 lp->stats.multicast++;
1323 skb = dev_alloc_skb( packet_length + 5);
1325 if ( skb == NULL ) {
1326 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
1327 lp->stats.rx_dropped++;
1332 ! This should work without alignment, but it could be
1336 skb_reserve( skb, 2 ); /* 16 bit alignment */
1339 data = skb_put( skb, packet_length);
1342 /* QUESTION: Like in the TX routine, do I want
1343 to send the DWORDs or the bytes first, or some
1344 mixture. A mixture might improve already slow PIO
1346 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1347 packet_length >> 2, packet_length & 3 ));
1348 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1349 /* read the left over bytes */
1350 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1351 packet_length & 0x3 );
1353 PRINTK3((" Reading %d words and %d byte(s) \n",
1354 (packet_length >> 1 ), packet_length & 1 ));
1355 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1356 if ( packet_length & 1 ) {
1357 data += packet_length & ~1;
1358 *(data++) = inb( ioaddr + DATA_1 );
1362 print_packet( data, packet_length );
1365 skb->protocol = eth_type_trans(skb, dev );
1367 dev->last_rx = jiffies;
1368 lp->stats.rx_packets++;
1369 lp->stats.rx_bytes += packet_length;
1372 lp->stats.rx_errors++;
1374 if ( status & RS_ALGNERR ) lp->stats.rx_frame_errors++;
1375 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1376 lp->stats.rx_length_errors++;
1377 if ( status & RS_BADCRC) lp->stats.rx_crc_errors++;
1381 /* error or good, tell the card to get rid of this packet */
1382 outw( MC_RELEASE, ioaddr + MMU_CMD );
1386 /*************************************************************************
1389 . Purpose: Handle a transmit error message. This will only be called
1390 . when an error, because of the AUTO_RELEASE mode.
1393 . Save pointer and packet no
1394 . Get the packet no from the top of the queue
1395 . check if it's valid ( if not, is this an error??? )
1396 . read the status word
1398 . ( resend? Not really, since we don't want old packets around )
1399 . Restore saved values
1400 ************************************************************************/
1401 static void smc_tx( struct net_device * dev )
1403 int ioaddr = dev->base_addr;
1404 struct smc_local *lp = netdev_priv(dev);
1412 saved_packet = inb( ioaddr + PNR_ARR );
1413 packet_no = inw( ioaddr + FIFO_PORTS );
1416 /* select this as the packet to read from */
1417 outb( packet_no, ioaddr + PNR_ARR );
1419 /* read the first word from this packet */
1420 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1422 tx_status = inw( ioaddr + DATA_1 );
1423 PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1425 lp->stats.tx_errors++;
1426 if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
1427 if ( tx_status & TS_LATCOL ) {
1428 printk(KERN_DEBUG CARDNAME
1429 ": Late collision occurred on last xmit.\n");
1430 lp->stats.tx_window_errors++;
1433 if ( tx_status & TS_16COL ) { ... }
1436 if ( tx_status & TS_SUCCESS ) {
1437 printk(CARDNAME": Successful packet caused interrupt \n");
1439 /* re-enable transmit */
1440 SMC_SELECT_BANK( 0 );
1441 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1443 /* kill the packet */
1444 SMC_SELECT_BANK( 2 );
1445 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1447 /* one less packet waiting for me */
1448 lp->packets_waiting--;
1450 outb( saved_packet, ioaddr + PNR_ARR );
1454 /*----------------------------------------------------
1457 . this makes the board clean up everything that it can
1458 . and not talk to the outside world. Caused by
1459 . an 'ifconfig ethX down'
1461 -----------------------------------------------------*/
1462 static int smc_close(struct net_device *dev)
1464 netif_stop_queue(dev);
1465 /* clear everything */
1466 smc_shutdown( dev->base_addr );
1468 /* Update the statistics here. */
1472 /*------------------------------------------------------------
1473 . Get the current statistics.
1474 . This may be called with the card open or closed.
1475 .-------------------------------------------------------------*/
1476 static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
1477 struct smc_local *lp = netdev_priv(dev);
1482 /*-----------------------------------------------------------
1483 . smc_set_multicast_list
1485 . This routine will, depending on the values passed to it,
1486 . either make it accept multicast packets, go into
1487 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1488 . a select set of multicast packets
1490 static void smc_set_multicast_list(struct net_device *dev)
1492 short ioaddr = dev->base_addr;
1495 if ( dev->flags & IFF_PROMISC )
1496 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1498 /* BUG? I never disable promiscuous mode if multicasting was turned on.
1499 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1500 when promiscuous mode is turned on.
1503 /* Here, I am setting this to accept all multicast packets.
1504 I don't need to zero the multicast table, because the flag is
1505 checked before the table is
1507 else if (dev->flags & IFF_ALLMULTI)
1508 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1510 /* We just get all multicast packets even if we only want them
1511 . from one source. This will be changed at some future
1513 else if (dev->mc_count ) {
1514 /* support hardware multicasting */
1516 /* be sure I get rid of flags I might have set */
1517 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1519 /* NOTE: this has to set the bank, so make sure it is the
1520 last thing called. The bank is set to zero at the top */
1521 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1524 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1528 since I'm disabling all multicast entirely, I need to
1529 clear the multicast list
1531 SMC_SELECT_BANK( 3 );
1532 outw( 0, ioaddr + MULTICAST1 );
1533 outw( 0, ioaddr + MULTICAST2 );
1534 outw( 0, ioaddr + MULTICAST3 );
1535 outw( 0, ioaddr + MULTICAST4 );
1541 static struct net_device *devSMC9194;
1542 MODULE_LICENSE("GPL");
1544 MODULE_PARM(io, "i");
1545 MODULE_PARM(irq, "i");
1546 MODULE_PARM(ifport, "i");
1547 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1548 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1549 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1551 int init_module(void)
1555 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1557 /* copy the parameters from insmod into the device structure */
1558 devSMC9194 = smc_init(-1);
1559 if (IS_ERR(devSMC9194))
1560 return PTR_ERR(devSMC9194);
1564 void cleanup_module(void)
1566 unregister_netdev(devSMC9194);
1567 free_irq(devSMC9194->irq, devSMC9194);
1568 release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1569 free_netdev(devSMC9194);