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/version.h>
62 #include <linux/kernel.h>
63 #include <linux/sched.h>
64 #include <linux/types.h>
65 #include <linux/fcntl.h>
66 #include <linux/interrupt.h>
67 #include <linux/ptrace.h>
68 #include <linux/ioport.h>
70 #include <linux/slab.h>
71 #include <linux/string.h>
72 #include <linux/init.h>
73 #include <linux/crc32.h>
74 #include <asm/bitops.h>
76 #include <linux/errno.h>
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/skbuff.h>
83 /*------------------------------------------------------------------------
85 . Configuration options, for the experienced user to change.
87 -------------------------------------------------------------------------*/
90 . Do you want to use 32 bit xfers? This should work on all chips, as
91 . the chipset is designed to accommodate them.
100 .the SMC9194 can be at any of the following port addresses. To change,
101 .for a slightly different card, you can add it to the array. Keep in
102 .mind that the array must end in zero.
104 static unsigned int smc_portlist[] __initdata = {
105 0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
106 0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0, 0
110 . Wait time for memory to be free. This probably shouldn't be
111 . tuned that much, as waiting for this means nothing else happens
114 #define MEMORY_WAIT_TIME 16
119 . 0 for normal operation
120 . 1 for slightly more details
121 . >2 for various levels of increasingly useless information
122 . 2 for interrupt tracking, status flags
123 . 3 for packet dumps, etc.
128 #define PRINTK3(x) printk x
134 #define PRINTK2(x) printk x
140 #define PRINTK(x) printk x
146 /*------------------------------------------------------------------------
148 . The internal workings of the driver. If you are changing anything
149 . here with the SMC stuff, you should have the datasheet and known
150 . what you are doing.
152 -------------------------------------------------------------------------*/
153 #define CARDNAME "SMC9194"
156 /* store this information for the driver.. */
159 these are things that the kernel wants me to keep, so users
160 can find out semi-useless statistics of how well the card is
163 struct net_device_stats stats;
166 If I have to wait until memory is available to send
167 a packet, I will store the skbuff here, until I get the
168 desired memory. Then, I'll send it out and free it.
170 struct sk_buff * saved_skb;
173 . This keeps track of how many packets that I have
174 . sent out. When an TX_EMPTY interrupt comes, I know
175 . that all of these have been sent.
181 /*-----------------------------------------------------------------
183 . The driver can be entered at any of the following entry points.
185 .------------------------------------------------------------------ */
188 . This is called by register_netdev(). It is responsible for
189 . checking the portlist for the SMC9000 series chipset. If it finds
190 . one, then it will initialize the device, find the hardware information,
191 . and sets up the appropriate device parameters.
192 . NOTE: Interrupts are *OFF* when this procedure is called.
194 . NB:This shouldn't be static since it is referred to externally.
196 int smc_init(struct net_device *dev);
199 . The kernel calls this function when someone wants to use the device,
200 . typically 'ifconfig ethX up'.
202 static int smc_open(struct net_device *dev);
205 . Our watchdog timed out. Called by the networking layer
207 static void smc_timeout(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);
229 /*---------------------------------------------------------------
231 . Interrupt level calls..
233 ----------------------------------------------------------------*/
236 . Handles the actual interrupt
238 static void smc_interrupt(int irq, void *, struct pt_regs *regs);
240 . This is a separate procedure to handle the receipt of a packet, to
241 . leave the interrupt code looking slightly cleaner
243 static inline void smc_rcv( struct net_device *dev );
245 . This handles a TX interrupt, which is only called when an error
246 . relating to a packet is sent.
248 static inline void smc_tx( struct net_device * dev );
251 ------------------------------------------------------------
255 ------------------------------------------------------------
259 . Test if a given location contains a chip, trying to cause as
260 . little damage as possible if it's not a SMC chip.
262 static int smc_probe(struct net_device *dev, int ioaddr);
265 . A rather simple routine to print out a packet for debugging purposes.
268 static void print_packet( byte *, int );
271 #define tx_done(dev) 1
273 /* this is called to actually send the packet to the chip */
274 static void smc_hardware_send_packet( struct net_device * dev );
276 /* Since I am not sure if I will have enough room in the chip's ram
277 . to store the packet, I call this routine, which either sends it
278 . now, or generates an interrupt when the card is ready for the
280 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );
282 /* this does a soft reset on the device */
283 static void smc_reset( int ioaddr );
285 /* Enable Interrupts, Receive, and Transmit */
286 static void smc_enable( int ioaddr );
288 /* this puts the device in an inactive state */
289 static void smc_shutdown( int ioaddr );
291 /* This routine will find the IRQ of the driver if one is not
292 . specified in the input to the device. */
293 static int smc_findirq( int ioaddr );
296 . Function: smc_reset( int ioaddr )
298 . This sets the SMC91xx chip to its normal state, hopefully from whatever
299 . mess that any other DOS driver has put it in.
301 . Maybe I should reset more registers to defaults in here? SOFTRESET should
305 . 1. send a SOFT RESET
306 . 2. wait for it to finish
307 . 3. enable autorelease mode
308 . 4. reset the memory management unit
309 . 5. clear all interrupts
312 static void smc_reset( int ioaddr )
314 /* This resets the registers mostly to defaults, but doesn't
315 affect EEPROM. That seems unnecessary */
316 SMC_SELECT_BANK( 0 );
317 outw( RCR_SOFTRESET, ioaddr + RCR );
319 /* this should pause enough for the chip to be happy */
322 /* Set the transmit and receive configuration registers to
324 outw( RCR_CLEAR, ioaddr + RCR );
325 outw( TCR_CLEAR, ioaddr + TCR );
327 /* set the control register to automatically
328 release successfully transmitted packets, to make the best
329 use out of our limited memory */
330 SMC_SELECT_BANK( 1 );
331 outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
334 SMC_SELECT_BANK( 2 );
335 outw( MC_RESET, ioaddr + MMU_CMD );
337 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
338 but this is a place where future chipsets _COULD_ break. Be wary
339 of issuing another MMU command right after this */
341 outb( 0, ioaddr + INT_MASK );
345 . Function: smc_enable
346 . Purpose: let the chip talk to the outside work
348 . 1. Enable the transmitter
349 . 2. Enable the receiver
350 . 3. Enable interrupts
352 static void smc_enable( int ioaddr )
354 SMC_SELECT_BANK( 0 );
355 /* see the header file for options in TCR/RCR NORMAL*/
356 outw( TCR_NORMAL, ioaddr + TCR );
357 outw( RCR_NORMAL, ioaddr + RCR );
359 /* now, enable interrupts */
360 SMC_SELECT_BANK( 2 );
361 outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
365 . Function: smc_shutdown
366 . Purpose: closes down the SMC91xxx chip.
368 . 1. zero the interrupt mask
369 . 2. clear the enable receive flag
370 . 3. clear the enable xmit flags
373 . (1) maybe utilize power down mode.
374 . Why not yet? Because while the chip will go into power down mode,
375 . the manual says that it will wake up in response to any I/O requests
376 . in the register space. Empirical results do not show this working.
378 static void smc_shutdown( int ioaddr )
380 /* no more interrupts for me */
381 SMC_SELECT_BANK( 2 );
382 outb( 0, ioaddr + INT_MASK );
384 /* and tell the card to stay away from that nasty outside world */
385 SMC_SELECT_BANK( 0 );
386 outb( RCR_CLEAR, ioaddr + RCR );
387 outb( TCR_CLEAR, ioaddr + TCR );
389 /* finally, shut the chip down */
390 SMC_SELECT_BANK( 1 );
391 outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL );
397 . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
399 . This sets the internal hardware table to filter out unwanted multicast
400 . packets before they take up memory.
402 . The SMC chip uses a hash table where the high 6 bits of the CRC of
403 . address are the offset into the table. If that bit is 1, then the
404 . multicast packet is accepted. Otherwise, it's dropped silently.
406 . To use the 6 bits as an offset into the table, the high 3 bits are the
407 . number of the 8 bit register, while the low 3 bits are the bit within
410 . This routine is based very heavily on the one provided by Peter Cammaert.
414 static void smc_setmulticast( int ioaddr, int count, struct dev_mc_list * addrs ) {
416 unsigned char multicast_table[ 8 ];
417 struct dev_mc_list * cur_addr;
418 /* table for flipping the order of 3 bits */
419 unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
421 /* start with a table of all zeros: reject all */
422 memset( multicast_table, 0, sizeof( multicast_table ) );
425 for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next ) {
428 /* do we have a pointer here? */
431 /* make sure this is a multicast address - shouldn't this
432 be a given if we have it here ? */
433 if ( !( *cur_addr->dmi_addr & 1 ) )
436 /* only use the low order bits */
437 position = ether_crc_le(6, cur_addr->dmi_addr) & 0x3f;
439 /* do some messy swapping to put the bit in the right spot */
440 multicast_table[invert3[position&7]] |=
441 (1<<invert3[(position>>3)&7]);
444 /* now, the table can be loaded into the chipset */
445 SMC_SELECT_BANK( 3 );
447 for ( i = 0; i < 8 ; i++ ) {
448 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
453 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
455 . Attempt to allocate memory for a packet, if chip-memory is not
456 . available, then tell the card to generate an interrupt when it
461 . o if the saved_skb is not currently null, then drop this packet
462 . on the floor. This should never happen, because of TBUSY.
463 . o if the saved_skb is null, then replace it with the current packet,
464 . o See if I can sending it now.
465 . o (NO): Enable interrupts and let the interrupt handler deal with it.
466 . o (YES):Send it now.
468 static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
470 struct smc_local *lp = (struct smc_local *)dev->priv;
471 unsigned short 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 lp->stats.tx_aborted_errors++;
483 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
488 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
492 ** The MMU wants the number of pages to be the number of 256 bytes
493 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
495 ** Pkt size for allocating is data length +6 (for additional status words,
496 ** length and ctl!) If odd size last byte is included in this header.
498 numPages = ((length & 0xfffe) + 6) / 256;
501 printk(CARDNAME": Far too big packet error. \n");
502 /* freeing the packet is a good thing here... but should
503 . any packets of this size get down here? */
505 lp->saved_skb = NULL;
506 /* this IS an error, but, i don't want the skb saved */
507 netif_wake_queue(dev);
510 /* either way, a packet is waiting now */
511 lp->packets_waiting++;
513 /* now, try to allocate the memory */
514 SMC_SELECT_BANK( 2 );
515 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
519 . wait a short amount of time.. if I can send a packet now, I send
520 . it now. Otherwise, I enable an interrupt and wait for one to be
523 . I could have handled this a slightly different way, by checking to
524 . see if any memory was available in the FREE MEMORY register. However,
525 . either way, I need to generate an allocation, and the allocation works
526 . no matter what, so I saw no point in checking free memory.
528 time_out = MEMORY_WAIT_TIME;
532 status = inb( ioaddr + INTERRUPT );
533 if ( status & IM_ALLOC_INT ) {
534 /* acknowledge the interrupt */
535 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
538 } while ( -- time_out );
541 /* oh well, wait until the chip finds memory later */
542 SMC_ENABLE_INT( IM_ALLOC_INT );
543 PRINTK2((CARDNAME": memory allocation deferred. \n"));
544 /* it's deferred, but I'll handle it later */
547 /* or YES! I can send the packet now.. */
548 smc_hardware_send_packet(dev);
549 netif_wake_queue(dev);
554 . Function: smc_hardware_send_packet(struct net_device * )
556 . This sends the actual packet to the SMC9xxx chip.
559 . First, see if a saved_skb is available.
560 . ( this should NOT be called if there is no 'saved_skb'
561 . Now, find the packet number that the chip allocated
562 . Point the data pointers at it in memory
563 . Set the length word in the chip's memory
564 . Dump the packet to chip memory
565 . Check if a last byte is needed ( odd length packet )
566 . if so, set the control flag right
567 . Tell the card to send it
568 . Enable the transmit interrupt, so I know if it failed
569 . Free the kernel data if I actually sent it.
571 static void smc_hardware_send_packet( struct net_device * dev )
573 struct smc_local *lp = (struct smc_local *)dev->priv;
575 struct sk_buff * skb = lp->saved_skb;
577 unsigned short ioaddr;
580 ioaddr = dev->base_addr;
583 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
586 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
589 /* If I get here, I _know_ there is a packet slot waiting for me */
590 packet_no = inb( ioaddr + PNR_ARR + 1 );
591 if ( packet_no & 0x80 ) {
592 /* or isn't there? BAD CHIP! */
593 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
594 dev_kfree_skb_any(skb);
595 lp->saved_skb = NULL;
596 netif_wake_queue(dev);
600 /* we have a packet address, so tell the card to use it */
601 outb( packet_no, ioaddr + PNR_ARR );
603 /* point to the beginning of the packet */
604 outw( PTR_AUTOINC , ioaddr + POINTER );
606 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
608 print_packet( buf, length );
611 /* send the packet length ( +6 for status, length and ctl byte )
612 and the status word ( set to zeros ) */
614 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
616 outw( 0, ioaddr + DATA_1 );
617 /* send the packet length ( +6 for status words, length, and ctl*/
618 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
619 outb( (length+6) >> 8 , ioaddr + DATA_1 );
622 /* send the actual data
623 . I _think_ it's faster to send the longs first, and then
624 . mop up by sending the last word. It depends heavily
625 . on alignment, at least on the 486. Maybe it would be
626 . a good idea to check which is optimal? But that could take
627 . almost as much time as is saved?
630 if ( length & 0x2 ) {
631 outsl(ioaddr + DATA_1, buf, length >> 2 );
632 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
635 outsl(ioaddr + DATA_1, buf, length >> 2 );
637 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
639 /* Send the last byte, if there is one. */
641 if ( (length & 1) == 0 ) {
642 outw( 0, ioaddr + DATA_1 );
644 outb( buf[length -1 ], ioaddr + DATA_1 );
645 outb( 0x20, ioaddr + DATA_1);
648 /* enable the interrupts */
649 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
651 /* and let the chipset deal with it */
652 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
654 PRINTK2((CARDNAME": Sent packet of length %d \n",length));
656 lp->saved_skb = NULL;
657 dev_kfree_skb_any (skb);
659 dev->trans_start = jiffies;
661 /* we can send another packet */
662 netif_wake_queue(dev);
667 /*-------------------------------------------------------------------------
669 | smc_init( struct net_device * dev )
671 | dev->base_addr == 0, try to find all possible locations
672 | dev->base_addr == 1, return failure code
673 | dev->base_addr == 2, always allocate space, and return success
674 | dev->base_addr == <anything else> this is the address to check
677 | 0 --> there is a device
678 | anything else, error
680 ---------------------------------------------------------------------------
682 int __init smc_init(struct net_device *dev)
685 int base_addr = dev->base_addr;
687 SET_MODULE_OWNER(dev);
689 /* try a specific location */
690 if (base_addr > 0x1ff)
691 return smc_probe(dev, base_addr);
692 else if (base_addr != 0)
695 /* check every ethernet address */
696 for (i = 0; smc_portlist[i]; i++)
697 if (smc_probe(dev, smc_portlist[i]) == 0)
700 /* couldn't find anything */
704 /*----------------------------------------------------------------------
707 . This routine has a simple purpose -- make the SMC chip generate an
708 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
709 ------------------------------------------------------------------------
711 int __init smc_findirq( int ioaddr )
714 unsigned long cookie;
717 /* I have to do a STI() here, because this is called from
718 a routine that does an CLI during this process, making it
719 rather difficult to get interrupts for auto detection */
722 cookie = probe_irq_on();
725 * What I try to do here is trigger an ALLOC_INT. This is done
726 * by allocating a small chunk of memory, which will give an interrupt
732 /* enable ALLOCation interrupts ONLY */
733 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
736 . Allocate 512 bytes of memory. Note that the chip was just
737 . reset so all the memory is available
739 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
742 . Wait until positive that the interrupt has been generated
747 int_status = inb( ioaddr + INTERRUPT );
749 if ( int_status & IM_ALLOC_INT )
750 break; /* got the interrupt */
753 /* there is really nothing that I can do here if timeout fails,
754 as autoirq_report will return a 0 anyway, which is what I
755 want in this case. Plus, the clean up is needed in both
759 On a fast machine, the status might change before the interrupt
760 is given to the processor. This means that the interrupt was
761 never detected, and autoirq_report fails to report anything.
762 This should fix autoirq_* problems.
767 /* and disable all interrupts again */
768 outb( 0, ioaddr + INT_MASK );
770 /* clear hardware interrupts again, because that's how it
771 was when I was called... */
774 /* and return what I found */
775 return probe_irq_off(cookie);
778 /*----------------------------------------------------------------------
779 . Function: smc_probe( int ioaddr )
782 . Tests to see if a given ioaddr points to an SMC9xxx chip.
783 . Returns a 0 on success
786 . (1) see if the high byte of BANK_SELECT is 0x33
787 . (2) compare the ioaddr with the base register's address
788 . (3) see if I recognize the chip ID in the appropriate register
790 .---------------------------------------------------------------------
793 /*---------------------------------------------------------------
794 . Here I do typical initialization tasks.
796 . o Initialize the structure if needed
797 . o print out my vanity message if not done so already
798 . o print out what type of hardware is detected
799 . o print out the ethernet address
801 . o set up my private data
802 . o configure the dev structure with my subroutines
803 . o actually GRAB the irq.
805 .-----------------------------------------------------------------
807 static int __init smc_probe(struct net_device *dev, int ioaddr)
809 int i, memory, retval;
810 static unsigned version_printed;
813 const char *version_string;
814 const char *if_string;
817 word revision_register;
818 word base_address_register;
819 word configuration_register;
820 word memory_info_register;
821 word memory_cfg_register;
823 /* Grab the region so that no one else tries to probe our ioports. */
824 if (!request_region(ioaddr, SMC_IO_EXTENT, dev->name))
827 /* First, see if the high byte is 0x33 */
828 bank = inw( ioaddr + BANK_SELECT );
829 if ( (bank & 0xFF00) != 0x3300 ) {
833 /* The above MIGHT indicate a device, but I need to write to further
835 outw( 0x0, ioaddr + BANK_SELECT );
836 bank = inw( ioaddr + BANK_SELECT );
837 if ( (bank & 0xFF00 ) != 0x3300 ) {
841 /* well, we've already written once, so hopefully another time won't
842 hurt. This time, I need to switch the bank register to bank 1,
843 so I can access the base address register */
845 base_address_register = inw( ioaddr + BASE );
846 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
847 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
848 "Probably not a SMC chip\n",
849 ioaddr, base_address_register >> 3 & 0x3E0 );
850 /* well, the base address register didn't match. Must not have
851 been a SMC chip after all. */
856 /* check if the revision register is something that I recognize.
857 These might need to be added to later, as future revisions
860 revision_register = inw( ioaddr + REVISION );
861 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
862 /* I don't recognize this chip, so... */
863 printk(CARDNAME ": IO %x: Unrecognized revision register:"
864 " %x, Contact author. \n", ioaddr, revision_register );
870 /* at this point I'll assume that the chip is an SMC9xxx.
871 It might be prudent to check a listing of MAC addresses
872 against the hardware address, or do some other tests. */
874 if (version_printed++ == 0)
875 printk("%s", version);
877 /* fill in some of the fields */
878 dev->base_addr = ioaddr;
881 . Get the MAC address ( bank 1, regs 4 - 9 )
883 SMC_SELECT_BANK( 1 );
884 for ( i = 0; i < 6; i += 2 ) {
887 address = inw( ioaddr + ADDR0 + i );
888 dev->dev_addr[ i + 1] = address >> 8;
889 dev->dev_addr[ i ] = address & 0xFF;
892 /* get the memory information */
894 SMC_SELECT_BANK( 0 );
895 memory_info_register = inw( ioaddr + MIR );
896 memory_cfg_register = inw( ioaddr + MCR );
897 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
898 memory *= 256 * ( memory_info_register & 0xFF );
901 Now, I want to find out more about the chip. This is sort of
902 redundant, but it's cleaner to have it in both, rather than having
903 one VERY long probe procedure.
906 revision_register = inw( ioaddr + REVISION );
907 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
908 if ( !version_string ) {
909 /* I shouldn't get here because this call was done before.... */
914 /* is it using AUI or 10BaseT ? */
915 if ( dev->if_port == 0 ) {
917 configuration_register = inw( ioaddr + CONFIG );
918 if ( configuration_register & CFG_AUI_SELECT )
923 if_string = interfaces[ dev->if_port - 1 ];
925 /* now, reset the chip, and put it into a known state */
929 . If dev->irq is 0, then the device has to be banged on to see
932 . This banging doesn't always detect the IRQ, for unknown reasons.
933 . a workaround is to reset the chip and try again.
935 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
936 . be what is requested on the command line. I don't do that, mostly
937 . because the card that I have uses a non-standard method of accessing
938 . the IRQs, and because this _should_ work in most configurations.
940 . Specifying an IRQ is done with the assumption that the user knows
941 . what (s)he is doing. No checking is done!!!!
944 if ( dev->irq < 2 ) {
949 dev->irq = smc_findirq( ioaddr );
952 /* kick the card and try again */
956 if (dev->irq == 0 ) {
957 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
962 /* now, print out the card info, in a short format.. */
964 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
965 version_string, revision_register & 0xF, ioaddr, dev->irq,
968 . Print the Ethernet address
971 for (i = 0; i < 5; i++)
972 printk("%2.2x:", dev->dev_addr[i] );
973 printk("%2.2x \n", dev->dev_addr[5] );
976 /* Initialize the private structure. */
977 if (dev->priv == NULL) {
978 dev->priv = kmalloc(sizeof(struct smc_local), GFP_KERNEL);
979 if (dev->priv == NULL) {
984 /* set the private data to zero by default */
985 memset(dev->priv, 0, sizeof(struct smc_local));
987 /* Fill in the fields of the device structure with ethernet values. */
991 retval = request_irq(dev->irq, &smc_interrupt, 0, dev->name, dev);
993 printk("%s: unable to get IRQ %d (irqval=%d).\n", dev->name,
1000 dev->open = smc_open;
1001 dev->stop = smc_close;
1002 dev->hard_start_xmit = smc_wait_to_send_packet;
1003 dev->tx_timeout = smc_timeout;
1004 dev->watchdog_timeo = HZ/20;
1005 dev->get_stats = smc_query_statistics;
1006 dev->set_multicast_list = smc_set_multicast_list;
1011 release_region(ioaddr, SMC_IO_EXTENT);
1016 static void print_packet( byte * buf, int length )
1023 printk("Packet of length %d \n", length );
1024 lines = length / 16;
1025 remainder = length % 16;
1027 for ( i = 0; i < lines ; i ++ ) {
1030 for ( cur = 0; cur < 8; cur ++ ) {
1035 printk("%02x%02x ", a, b );
1039 for ( i = 0; i < remainder/2 ; i++ ) {
1044 printk("%02x%02x ", a, b );
1053 * Open and Initialize the board
1055 * Set up everything, reset the card, etc ..
1058 static int smc_open(struct net_device *dev)
1060 int ioaddr = dev->base_addr;
1062 int i; /* used to set hw ethernet address */
1064 /* clear out all the junk that was put here before... */
1065 memset(dev->priv, 0, sizeof(struct smc_local));
1067 /* reset the hardware */
1069 smc_reset( ioaddr );
1070 smc_enable( ioaddr );
1072 /* Select which interface to use */
1074 SMC_SELECT_BANK( 1 );
1075 if ( dev->if_port == 1 ) {
1076 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1079 else if ( dev->if_port == 2 ) {
1080 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1085 According to Becker, I have to set the hardware address
1086 at this point, because the (l)user can set it with an
1087 ioctl. Easily done...
1089 SMC_SELECT_BANK( 1 );
1090 for ( i = 0; i < 6; i += 2 ) {
1093 address = dev->dev_addr[ i + 1 ] << 8 ;
1094 address |= dev->dev_addr[ i ];
1095 outw( address, ioaddr + ADDR0 + i );
1098 netif_start_queue(dev);
1102 /*--------------------------------------------------------
1103 . Called by the kernel to send a packet out into the void
1104 . of the net. This routine is largely based on
1105 . skeleton.c, from Becker.
1106 .--------------------------------------------------------
1109 static void smc_timeout(struct net_device *dev)
1111 /* If we get here, some higher level has decided we are broken.
1112 There should really be a "kick me" function call instead. */
1113 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1114 tx_done(dev) ? "IRQ conflict" :
1115 "network cable problem");
1116 /* "kick" the adaptor */
1117 smc_reset( dev->base_addr );
1118 smc_enable( dev->base_addr );
1119 dev->trans_start = jiffies;
1120 /* clear anything saved */
1121 ((struct smc_local *)dev->priv)->saved_skb = NULL;
1122 netif_wake_queue(dev);
1125 /*--------------------------------------------------------------------
1127 . This is the main routine of the driver, to handle the device when
1128 . it needs some attention.
1131 . first, save state of the chipset
1132 . branch off into routines to handle each case, and acknowledge
1133 . each to the interrupt register
1134 . and finally restore state.
1136 ---------------------------------------------------------------------*/
1138 static void smc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
1140 struct net_device *dev = dev_id;
1141 int ioaddr = dev->base_addr;
1142 struct smc_local *lp = (struct smc_local *)dev->priv;
1148 /* state registers */
1154 PRINTK3((CARDNAME": SMC interrupt started \n"));
1156 saved_bank = inw( ioaddr + BANK_SELECT );
1159 saved_pointer = inw( ioaddr + POINTER );
1161 mask = inb( ioaddr + INT_MASK );
1162 /* clear all interrupts */
1163 outb( 0, ioaddr + INT_MASK );
1166 /* set a timeout value, so I don't stay here forever */
1169 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1171 /* read the status flag, and mask it */
1172 status = inb( ioaddr + INTERRUPT ) & mask;
1176 PRINTK3((KERN_WARNING CARDNAME
1177 ": Handling interrupt status %x \n", status ));
1179 if (status & IM_RCV_INT) {
1180 /* Got a packet(s). */
1181 PRINTK2((KERN_WARNING CARDNAME
1182 ": Receive Interrupt\n"));
1184 } else if (status & IM_TX_INT ) {
1185 PRINTK2((KERN_WARNING CARDNAME
1186 ": TX ERROR handled\n"));
1188 outb(IM_TX_INT, ioaddr + INTERRUPT );
1189 } else if (status & IM_TX_EMPTY_INT ) {
1191 SMC_SELECT_BANK( 0 );
1192 card_stats = inw( ioaddr + COUNTER );
1193 /* single collisions */
1194 lp->stats.collisions += card_stats & 0xF;
1196 /* multiple collisions */
1197 lp->stats.collisions += card_stats & 0xF;
1199 /* these are for when linux supports these statistics */
1201 SMC_SELECT_BANK( 2 );
1202 PRINTK2((KERN_WARNING CARDNAME
1203 ": TX_BUFFER_EMPTY handled\n"));
1204 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1205 mask &= ~IM_TX_EMPTY_INT;
1206 lp->stats.tx_packets += lp->packets_waiting;
1207 lp->packets_waiting = 0;
1209 } else if (status & IM_ALLOC_INT ) {
1210 PRINTK2((KERN_DEBUG CARDNAME
1211 ": Allocation interrupt \n"));
1212 /* clear this interrupt so it doesn't happen again */
1213 mask &= ~IM_ALLOC_INT;
1215 smc_hardware_send_packet( dev );
1217 /* enable xmit interrupts based on this */
1218 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1220 /* and let the card send more packets to me */
1221 netif_wake_queue(dev);
1223 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1224 } else if (status & IM_RX_OVRN_INT ) {
1225 lp->stats.rx_errors++;
1226 lp->stats.rx_fifo_errors++;
1227 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1228 } else if (status & IM_EPH_INT ) {
1229 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1230 } else if (status & IM_ERCV_INT ) {
1231 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1232 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1234 } while ( timeout -- );
1237 /* restore state register */
1238 SMC_SELECT_BANK( 2 );
1239 outb( mask, ioaddr + INT_MASK );
1241 PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1242 outw( saved_pointer, ioaddr + POINTER );
1244 SMC_SELECT_BANK( saved_bank );
1246 PRINTK3((CARDNAME ": Interrupt done\n"));
1250 /*-------------------------------------------------------------
1252 . smc_rcv - receive a packet from the card
1254 . There is ( at least ) a packet waiting to be read from
1258 . o If an error, record it
1259 . o otherwise, read in the packet
1260 --------------------------------------------------------------
1262 static void smc_rcv(struct net_device *dev)
1264 struct smc_local *lp = (struct smc_local *)dev->priv;
1265 int ioaddr = dev->base_addr;
1272 packet_number = inw( ioaddr + FIFO_PORTS );
1274 if ( packet_number & FP_RXEMPTY ) {
1275 /* we got called , but nothing was on the FIFO */
1276 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1277 /* don't need to restore anything */
1281 /* start reading from the start of the packet */
1282 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1284 /* First two words are status and packet_length */
1285 status = inw( ioaddr + DATA_1 );
1286 packet_length = inw( ioaddr + DATA_1 );
1288 packet_length &= 0x07ff; /* mask off top bits */
1290 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1292 . the packet length contains 3 extra words :
1293 . status, length, and an extra word with an odd byte .
1297 if ( !(status & RS_ERRORS ) ){
1298 /* do stuff to make a new packet */
1299 struct sk_buff * skb;
1302 /* read one extra byte */
1303 if ( status & RS_ODDFRAME )
1306 /* set multicast stats */
1307 if ( status & RS_MULTICAST )
1308 lp->stats.multicast++;
1310 skb = dev_alloc_skb( packet_length + 5);
1312 if ( skb == NULL ) {
1313 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
1314 lp->stats.rx_dropped++;
1319 ! This should work without alignment, but it could be
1323 skb_reserve( skb, 2 ); /* 16 bit alignment */
1326 data = skb_put( skb, packet_length);
1329 /* QUESTION: Like in the TX routine, do I want
1330 to send the DWORDs or the bytes first, or some
1331 mixture. A mixture might improve already slow PIO
1333 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1334 packet_length >> 2, packet_length & 3 ));
1335 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1336 /* read the left over bytes */
1337 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1338 packet_length & 0x3 );
1340 PRINTK3((" Reading %d words and %d byte(s) \n",
1341 (packet_length >> 1 ), packet_length & 1 ));
1342 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1343 if ( packet_length & 1 ) {
1344 data += packet_length & ~1;
1345 *(data++) = inb( ioaddr + DATA_1 );
1349 print_packet( data, packet_length );
1352 skb->protocol = eth_type_trans(skb, dev );
1354 dev->last_rx = jiffies;
1355 lp->stats.rx_packets++;
1356 lp->stats.rx_bytes += packet_length;
1359 lp->stats.rx_errors++;
1361 if ( status & RS_ALGNERR ) lp->stats.rx_frame_errors++;
1362 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1363 lp->stats.rx_length_errors++;
1364 if ( status & RS_BADCRC) lp->stats.rx_crc_errors++;
1368 /* error or good, tell the card to get rid of this packet */
1369 outw( MC_RELEASE, ioaddr + MMU_CMD );
1373 /*************************************************************************
1376 . Purpose: Handle a transmit error message. This will only be called
1377 . when an error, because of the AUTO_RELEASE mode.
1380 . Save pointer and packet no
1381 . Get the packet no from the top of the queue
1382 . check if it's valid ( if not, is this an error??? )
1383 . read the status word
1385 . ( resend? Not really, since we don't want old packets around )
1386 . Restore saved values
1387 ************************************************************************/
1388 static void smc_tx( struct net_device * dev )
1390 int ioaddr = dev->base_addr;
1391 struct smc_local *lp = (struct smc_local *)dev->priv;
1399 saved_packet = inb( ioaddr + PNR_ARR );
1400 packet_no = inw( ioaddr + FIFO_PORTS );
1403 /* select this as the packet to read from */
1404 outb( packet_no, ioaddr + PNR_ARR );
1406 /* read the first word from this packet */
1407 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1409 tx_status = inw( ioaddr + DATA_1 );
1410 PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1412 lp->stats.tx_errors++;
1413 if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
1414 if ( tx_status & TS_LATCOL ) {
1415 printk(KERN_DEBUG CARDNAME
1416 ": Late collision occurred on last xmit.\n");
1417 lp->stats.tx_window_errors++;
1420 if ( tx_status & TS_16COL ) { ... }
1423 if ( tx_status & TS_SUCCESS ) {
1424 printk(CARDNAME": Successful packet caused interrupt \n");
1426 /* re-enable transmit */
1427 SMC_SELECT_BANK( 0 );
1428 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1430 /* kill the packet */
1431 SMC_SELECT_BANK( 2 );
1432 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1434 /* one less packet waiting for me */
1435 lp->packets_waiting--;
1437 outb( saved_packet, ioaddr + PNR_ARR );
1441 /*----------------------------------------------------
1444 . this makes the board clean up everything that it can
1445 . and not talk to the outside world. Caused by
1446 . an 'ifconfig ethX down'
1448 -----------------------------------------------------*/
1449 static int smc_close(struct net_device *dev)
1451 netif_stop_queue(dev);
1452 /* clear everything */
1453 smc_shutdown( dev->base_addr );
1455 /* Update the statistics here. */
1459 /*------------------------------------------------------------
1460 . Get the current statistics.
1461 . This may be called with the card open or closed.
1462 .-------------------------------------------------------------*/
1463 static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
1464 struct smc_local *lp = (struct smc_local *)dev->priv;
1469 /*-----------------------------------------------------------
1470 . smc_set_multicast_list
1472 . This routine will, depending on the values passed to it,
1473 . either make it accept multicast packets, go into
1474 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1475 . a select set of multicast packets
1477 static void smc_set_multicast_list(struct net_device *dev)
1479 short ioaddr = dev->base_addr;
1482 if ( dev->flags & IFF_PROMISC )
1483 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1485 /* BUG? I never disable promiscuous mode if multicasting was turned on.
1486 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1487 when promiscuous mode is turned on.
1490 /* Here, I am setting this to accept all multicast packets.
1491 I don't need to zero the multicast table, because the flag is
1492 checked before the table is
1494 else if (dev->flags & IFF_ALLMULTI)
1495 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1497 /* We just get all multicast packets even if we only want them
1498 . from one source. This will be changed at some future
1500 else if (dev->mc_count ) {
1501 /* support hardware multicasting */
1503 /* be sure I get rid of flags I might have set */
1504 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1506 /* NOTE: this has to set the bank, so make sure it is the
1507 last thing called. The bank is set to zero at the top */
1508 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1511 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1515 since I'm disabling all multicast entirely, I need to
1516 clear the multicast list
1518 SMC_SELECT_BANK( 3 );
1519 outw( 0, ioaddr + MULTICAST1 );
1520 outw( 0, ioaddr + MULTICAST2 );
1521 outw( 0, ioaddr + MULTICAST3 );
1522 outw( 0, ioaddr + MULTICAST4 );
1528 static struct net_device devSMC9194;
1532 MODULE_LICENSE("GPL");
1534 MODULE_PARM(io, "i");
1535 MODULE_PARM(irq, "i");
1536 MODULE_PARM(ifport, "i");
1537 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1538 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1539 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1541 int init_module(void)
1547 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1549 /* copy the parameters from insmod into the device structure */
1550 devSMC9194.base_addr = io;
1551 devSMC9194.irq = irq;
1552 devSMC9194.if_port = ifport;
1553 devSMC9194.init = smc_init;
1554 if ((result = register_netdev(&devSMC9194)) != 0)
1560 void cleanup_module(void)
1562 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1563 unregister_netdev(&devSMC9194);
1565 free_irq(devSMC9194.irq, &devSMC9194);
1566 release_region(devSMC9194.base_addr, SMC_IO_EXTENT);
1568 if (devSMC9194.priv)
1569 kfree(devSMC9194.priv);