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 #define DRV_NAME "smc9194"
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 struct net_device *smc_init(int unit);
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 irqreturn_t 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 = netdev_priv(dev);
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" );
490 if (length < ETH_ZLEN) {
491 skb = skb_padto(skb, ETH_ZLEN);
493 netif_wake_queue(dev);
500 ** The MMU wants the number of pages to be the number of 256 bytes
501 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
503 ** Pkt size for allocating is data length +6 (for additional status words,
504 ** length and ctl!) If odd size last byte is included in this header.
506 numPages = ((length & 0xfffe) + 6) / 256;
509 printk(CARDNAME": Far too big packet error. \n");
510 /* freeing the packet is a good thing here... but should
511 . any packets of this size get down here? */
513 lp->saved_skb = NULL;
514 /* this IS an error, but, i don't want the skb saved */
515 netif_wake_queue(dev);
518 /* either way, a packet is waiting now */
519 lp->packets_waiting++;
521 /* now, try to allocate the memory */
522 SMC_SELECT_BANK( 2 );
523 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
527 . wait a short amount of time.. if I can send a packet now, I send
528 . it now. Otherwise, I enable an interrupt and wait for one to be
531 . I could have handled this a slightly different way, by checking to
532 . see if any memory was available in the FREE MEMORY register. However,
533 . either way, I need to generate an allocation, and the allocation works
534 . no matter what, so I saw no point in checking free memory.
536 time_out = MEMORY_WAIT_TIME;
540 status = inb( ioaddr + INTERRUPT );
541 if ( status & IM_ALLOC_INT ) {
542 /* acknowledge the interrupt */
543 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
546 } while ( -- time_out );
549 /* oh well, wait until the chip finds memory later */
550 SMC_ENABLE_INT( IM_ALLOC_INT );
551 PRINTK2((CARDNAME": memory allocation deferred. \n"));
552 /* it's deferred, but I'll handle it later */
555 /* or YES! I can send the packet now.. */
556 smc_hardware_send_packet(dev);
557 netif_wake_queue(dev);
562 . Function: smc_hardware_send_packet(struct net_device * )
564 . This sends the actual packet to the SMC9xxx chip.
567 . First, see if a saved_skb is available.
568 . ( this should NOT be called if there is no 'saved_skb'
569 . Now, find the packet number that the chip allocated
570 . Point the data pointers at it in memory
571 . Set the length word in the chip's memory
572 . Dump the packet to chip memory
573 . Check if a last byte is needed ( odd length packet )
574 . if so, set the control flag right
575 . Tell the card to send it
576 . Enable the transmit interrupt, so I know if it failed
577 . Free the kernel data if I actually sent it.
579 static void smc_hardware_send_packet( struct net_device * dev )
581 struct smc_local *lp = netdev_priv(dev);
583 struct sk_buff * skb = lp->saved_skb;
585 unsigned short ioaddr;
588 ioaddr = dev->base_addr;
591 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
594 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
597 /* If I get here, I _know_ there is a packet slot waiting for me */
598 packet_no = inb( ioaddr + PNR_ARR + 1 );
599 if ( packet_no & 0x80 ) {
600 /* or isn't there? BAD CHIP! */
601 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
602 dev_kfree_skb_any(skb);
603 lp->saved_skb = NULL;
604 netif_wake_queue(dev);
608 /* we have a packet address, so tell the card to use it */
609 outb( packet_no, ioaddr + PNR_ARR );
611 /* point to the beginning of the packet */
612 outw( PTR_AUTOINC , ioaddr + POINTER );
614 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
616 print_packet( buf, length );
619 /* send the packet length ( +6 for status, length and ctl byte )
620 and the status word ( set to zeros ) */
622 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
624 outw( 0, ioaddr + DATA_1 );
625 /* send the packet length ( +6 for status words, length, and ctl*/
626 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
627 outb( (length+6) >> 8 , ioaddr + DATA_1 );
630 /* send the actual data
631 . I _think_ it's faster to send the longs first, and then
632 . mop up by sending the last word. It depends heavily
633 . on alignment, at least on the 486. Maybe it would be
634 . a good idea to check which is optimal? But that could take
635 . almost as much time as is saved?
638 if ( length & 0x2 ) {
639 outsl(ioaddr + DATA_1, buf, length >> 2 );
640 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
643 outsl(ioaddr + DATA_1, buf, length >> 2 );
645 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
647 /* Send the last byte, if there is one. */
649 if ( (length & 1) == 0 ) {
650 outw( 0, ioaddr + DATA_1 );
652 outb( buf[length -1 ], ioaddr + DATA_1 );
653 outb( 0x20, ioaddr + DATA_1);
656 /* enable the interrupts */
657 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
659 /* and let the chipset deal with it */
660 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
662 PRINTK2((CARDNAME": Sent packet of length %d \n",length));
664 lp->saved_skb = NULL;
665 dev_kfree_skb_any (skb);
667 dev->trans_start = jiffies;
669 /* we can send another packet */
670 netif_wake_queue(dev);
675 /*-------------------------------------------------------------------------
679 | dev->base_addr == 0, try to find all possible locations
680 | dev->base_addr == 1, return failure code
681 | dev->base_addr == 2, always allocate space, and return success
682 | dev->base_addr == <anything else> this is the address to check
685 | pointer to net_device or ERR_PTR(error)
687 ---------------------------------------------------------------------------
693 struct net_device * __init smc_init(int unit)
695 struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
700 return ERR_PTR(-ENODEV);
703 sprintf(dev->name, "eth%d", unit);
704 netdev_boot_setup_check(dev);
709 SET_MODULE_OWNER(dev);
711 if (io > 0x1ff) { /* Check a single specified location. */
712 err = smc_probe(dev, io);
713 } else if (io != 0) { /* Don't probe at all. */
716 for (port = smc_portlist; *port; port++) {
717 if (smc_probe(dev, *port) == 0)
725 err = register_netdev(dev);
730 free_irq(dev->irq, dev);
731 release_region(dev->base_addr, SMC_IO_EXTENT);
737 /*----------------------------------------------------------------------
740 . This routine has a simple purpose -- make the SMC chip generate an
741 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
742 ------------------------------------------------------------------------
744 int __init smc_findirq( int ioaddr )
747 unsigned long cookie;
750 cookie = probe_irq_on();
753 * What I try to do here is trigger an ALLOC_INT. This is done
754 * by allocating a small chunk of memory, which will give an interrupt
760 /* enable ALLOCation interrupts ONLY */
761 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
764 . Allocate 512 bytes of memory. Note that the chip was just
765 . reset so all the memory is available
767 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
770 . Wait until positive that the interrupt has been generated
775 int_status = inb( ioaddr + INTERRUPT );
777 if ( int_status & IM_ALLOC_INT )
778 break; /* got the interrupt */
781 /* there is really nothing that I can do here if timeout fails,
782 as probe_irq_off will return a 0 anyway, which is what I
783 want in this case. Plus, the clean up is needed in both
787 On a fast machine, the status might change before the interrupt
788 is given to the processor. This means that the interrupt was
789 never detected, and probe_irq_off fails to report anything.
790 This should fix probe_irq_* problems.
795 /* and disable all interrupts again */
796 outb( 0, ioaddr + INT_MASK );
798 /* and return what I found */
799 return probe_irq_off(cookie);
802 /*----------------------------------------------------------------------
803 . Function: smc_probe( int ioaddr )
806 . Tests to see if a given ioaddr points to an SMC9xxx chip.
807 . Returns a 0 on success
810 . (1) see if the high byte of BANK_SELECT is 0x33
811 . (2) compare the ioaddr with the base register's address
812 . (3) see if I recognize the chip ID in the appropriate register
814 .---------------------------------------------------------------------
817 /*---------------------------------------------------------------
818 . Here I do typical initialization tasks.
820 . o Initialize the structure if needed
821 . o print out my vanity message if not done so already
822 . o print out what type of hardware is detected
823 . o print out the ethernet address
825 . o set up my private data
826 . o configure the dev structure with my subroutines
827 . o actually GRAB the irq.
829 .-----------------------------------------------------------------
831 static int __init smc_probe(struct net_device *dev, int ioaddr)
833 int i, memory, retval;
834 static unsigned version_printed;
837 const char *version_string;
838 const char *if_string;
841 word revision_register;
842 word base_address_register;
843 word configuration_register;
844 word memory_info_register;
845 word memory_cfg_register;
847 /* Grab the region so that no one else tries to probe our ioports. */
848 if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
852 dev->if_port = ifport;
854 /* First, see if the high byte is 0x33 */
855 bank = inw( ioaddr + BANK_SELECT );
856 if ( (bank & 0xFF00) != 0x3300 ) {
860 /* The above MIGHT indicate a device, but I need to write to further
862 outw( 0x0, ioaddr + BANK_SELECT );
863 bank = inw( ioaddr + BANK_SELECT );
864 if ( (bank & 0xFF00 ) != 0x3300 ) {
868 /* well, we've already written once, so hopefully another time won't
869 hurt. This time, I need to switch the bank register to bank 1,
870 so I can access the base address register */
872 base_address_register = inw( ioaddr + BASE );
873 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
874 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
875 "Probably not a SMC chip\n",
876 ioaddr, base_address_register >> 3 & 0x3E0 );
877 /* well, the base address register didn't match. Must not have
878 been a SMC chip after all. */
883 /* check if the revision register is something that I recognize.
884 These might need to be added to later, as future revisions
887 revision_register = inw( ioaddr + REVISION );
888 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
889 /* I don't recognize this chip, so... */
890 printk(CARDNAME ": IO %x: Unrecognized revision register:"
891 " %x, Contact author. \n", ioaddr, revision_register );
897 /* at this point I'll assume that the chip is an SMC9xxx.
898 It might be prudent to check a listing of MAC addresses
899 against the hardware address, or do some other tests. */
901 if (version_printed++ == 0)
902 printk("%s", version);
904 /* fill in some of the fields */
905 dev->base_addr = ioaddr;
908 . Get the MAC address ( bank 1, regs 4 - 9 )
910 SMC_SELECT_BANK( 1 );
911 for ( i = 0; i < 6; i += 2 ) {
914 address = inw( ioaddr + ADDR0 + i );
915 dev->dev_addr[ i + 1] = address >> 8;
916 dev->dev_addr[ i ] = address & 0xFF;
919 /* get the memory information */
921 SMC_SELECT_BANK( 0 );
922 memory_info_register = inw( ioaddr + MIR );
923 memory_cfg_register = inw( ioaddr + MCR );
924 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
925 memory *= 256 * ( memory_info_register & 0xFF );
928 Now, I want to find out more about the chip. This is sort of
929 redundant, but it's cleaner to have it in both, rather than having
930 one VERY long probe procedure.
933 revision_register = inw( ioaddr + REVISION );
934 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
935 if ( !version_string ) {
936 /* I shouldn't get here because this call was done before.... */
941 /* is it using AUI or 10BaseT ? */
942 if ( dev->if_port == 0 ) {
944 configuration_register = inw( ioaddr + CONFIG );
945 if ( configuration_register & CFG_AUI_SELECT )
950 if_string = interfaces[ dev->if_port - 1 ];
952 /* now, reset the chip, and put it into a known state */
956 . If dev->irq is 0, then the device has to be banged on to see
959 . This banging doesn't always detect the IRQ, for unknown reasons.
960 . a workaround is to reset the chip and try again.
962 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
963 . be what is requested on the command line. I don't do that, mostly
964 . because the card that I have uses a non-standard method of accessing
965 . the IRQs, and because this _should_ work in most configurations.
967 . Specifying an IRQ is done with the assumption that the user knows
968 . what (s)he is doing. No checking is done!!!!
971 if ( dev->irq < 2 ) {
976 dev->irq = smc_findirq( ioaddr );
979 /* kick the card and try again */
983 if (dev->irq == 0 ) {
984 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
989 /* now, print out the card info, in a short format.. */
991 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
992 version_string, revision_register & 0xF, ioaddr, dev->irq,
995 . Print the Ethernet address
998 for (i = 0; i < 5; i++)
999 printk("%2.2x:", dev->dev_addr[i] );
1000 printk("%2.2x \n", dev->dev_addr[5] );
1002 /* set the private data to zero by default */
1003 memset(dev->priv, 0, sizeof(struct smc_local));
1006 retval = request_irq(dev->irq, &smc_interrupt, 0, DRV_NAME, dev);
1008 printk("%s: unable to get IRQ %d (irqval=%d).\n", DRV_NAME,
1013 dev->open = smc_open;
1014 dev->stop = smc_close;
1015 dev->hard_start_xmit = smc_wait_to_send_packet;
1016 dev->tx_timeout = smc_timeout;
1017 dev->watchdog_timeo = HZ/20;
1018 dev->get_stats = smc_query_statistics;
1019 dev->set_multicast_list = smc_set_multicast_list;
1024 release_region(ioaddr, SMC_IO_EXTENT);
1029 static void print_packet( byte * buf, int length )
1036 printk("Packet of length %d \n", length );
1037 lines = length / 16;
1038 remainder = length % 16;
1040 for ( i = 0; i < lines ; i ++ ) {
1043 for ( cur = 0; cur < 8; cur ++ ) {
1048 printk("%02x%02x ", a, b );
1052 for ( i = 0; i < remainder/2 ; i++ ) {
1057 printk("%02x%02x ", a, b );
1066 * Open and Initialize the board
1068 * Set up everything, reset the card, etc ..
1071 static int smc_open(struct net_device *dev)
1073 int ioaddr = dev->base_addr;
1075 int i; /* used to set hw ethernet address */
1077 /* clear out all the junk that was put here before... */
1078 memset(dev->priv, 0, sizeof(struct smc_local));
1080 /* reset the hardware */
1082 smc_reset( ioaddr );
1083 smc_enable( ioaddr );
1085 /* Select which interface to use */
1087 SMC_SELECT_BANK( 1 );
1088 if ( dev->if_port == 1 ) {
1089 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1092 else if ( dev->if_port == 2 ) {
1093 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1098 According to Becker, I have to set the hardware address
1099 at this point, because the (l)user can set it with an
1100 ioctl. Easily done...
1102 SMC_SELECT_BANK( 1 );
1103 for ( i = 0; i < 6; i += 2 ) {
1106 address = dev->dev_addr[ i + 1 ] << 8 ;
1107 address |= dev->dev_addr[ i ];
1108 outw( address, ioaddr + ADDR0 + i );
1111 netif_start_queue(dev);
1115 /*--------------------------------------------------------
1116 . Called by the kernel to send a packet out into the void
1117 . of the net. This routine is largely based on
1118 . skeleton.c, from Becker.
1119 .--------------------------------------------------------
1122 static void smc_timeout(struct net_device *dev)
1124 /* If we get here, some higher level has decided we are broken.
1125 There should really be a "kick me" function call instead. */
1126 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1127 tx_done(dev) ? "IRQ conflict" :
1128 "network cable problem");
1129 /* "kick" the adaptor */
1130 smc_reset( dev->base_addr );
1131 smc_enable( dev->base_addr );
1132 dev->trans_start = jiffies;
1133 /* clear anything saved */
1134 ((struct smc_local *)dev->priv)->saved_skb = NULL;
1135 netif_wake_queue(dev);
1138 /*--------------------------------------------------------------------
1140 . This is the main routine of the driver, to handle the device when
1141 . it needs some attention.
1144 . first, save state of the chipset
1145 . branch off into routines to handle each case, and acknowledge
1146 . each to the interrupt register
1147 . and finally restore state.
1149 ---------------------------------------------------------------------*/
1151 static irqreturn_t smc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
1153 struct net_device *dev = dev_id;
1154 int ioaddr = dev->base_addr;
1155 struct smc_local *lp = netdev_priv(dev);
1161 /* state registers */
1167 PRINTK3((CARDNAME": SMC interrupt started \n"));
1169 saved_bank = inw( ioaddr + BANK_SELECT );
1172 saved_pointer = inw( ioaddr + POINTER );
1174 mask = inb( ioaddr + INT_MASK );
1175 /* clear all interrupts */
1176 outb( 0, ioaddr + INT_MASK );
1179 /* set a timeout value, so I don't stay here forever */
1182 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1184 /* read the status flag, and mask it */
1185 status = inb( ioaddr + INTERRUPT ) & mask;
1191 PRINTK3((KERN_WARNING CARDNAME
1192 ": Handling interrupt status %x \n", status ));
1194 if (status & IM_RCV_INT) {
1195 /* Got a packet(s). */
1196 PRINTK2((KERN_WARNING CARDNAME
1197 ": Receive Interrupt\n"));
1199 } else if (status & IM_TX_INT ) {
1200 PRINTK2((KERN_WARNING CARDNAME
1201 ": TX ERROR handled\n"));
1203 outb(IM_TX_INT, ioaddr + INTERRUPT );
1204 } else if (status & IM_TX_EMPTY_INT ) {
1206 SMC_SELECT_BANK( 0 );
1207 card_stats = inw( ioaddr + COUNTER );
1208 /* single collisions */
1209 lp->stats.collisions += card_stats & 0xF;
1211 /* multiple collisions */
1212 lp->stats.collisions += card_stats & 0xF;
1214 /* these are for when linux supports these statistics */
1216 SMC_SELECT_BANK( 2 );
1217 PRINTK2((KERN_WARNING CARDNAME
1218 ": TX_BUFFER_EMPTY handled\n"));
1219 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1220 mask &= ~IM_TX_EMPTY_INT;
1221 lp->stats.tx_packets += lp->packets_waiting;
1222 lp->packets_waiting = 0;
1224 } else if (status & IM_ALLOC_INT ) {
1225 PRINTK2((KERN_DEBUG CARDNAME
1226 ": Allocation interrupt \n"));
1227 /* clear this interrupt so it doesn't happen again */
1228 mask &= ~IM_ALLOC_INT;
1230 smc_hardware_send_packet( dev );
1232 /* enable xmit interrupts based on this */
1233 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1235 /* and let the card send more packets to me */
1236 netif_wake_queue(dev);
1238 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1239 } else if (status & IM_RX_OVRN_INT ) {
1240 lp->stats.rx_errors++;
1241 lp->stats.rx_fifo_errors++;
1242 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1243 } else if (status & IM_EPH_INT ) {
1244 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1245 } else if (status & IM_ERCV_INT ) {
1246 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1247 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1249 } while ( timeout -- );
1252 /* restore state register */
1253 SMC_SELECT_BANK( 2 );
1254 outb( mask, ioaddr + INT_MASK );
1256 PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1257 outw( saved_pointer, ioaddr + POINTER );
1259 SMC_SELECT_BANK( saved_bank );
1261 PRINTK3((CARDNAME ": Interrupt done\n"));
1262 return IRQ_RETVAL(handled);
1265 /*-------------------------------------------------------------
1267 . smc_rcv - receive a packet from the card
1269 . There is ( at least ) a packet waiting to be read from
1273 . o If an error, record it
1274 . o otherwise, read in the packet
1275 --------------------------------------------------------------
1277 static void smc_rcv(struct net_device *dev)
1279 struct smc_local *lp = netdev_priv(dev);
1280 int ioaddr = dev->base_addr;
1287 packet_number = inw( ioaddr + FIFO_PORTS );
1289 if ( packet_number & FP_RXEMPTY ) {
1290 /* we got called , but nothing was on the FIFO */
1291 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1292 /* don't need to restore anything */
1296 /* start reading from the start of the packet */
1297 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1299 /* First two words are status and packet_length */
1300 status = inw( ioaddr + DATA_1 );
1301 packet_length = inw( ioaddr + DATA_1 );
1303 packet_length &= 0x07ff; /* mask off top bits */
1305 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1307 . the packet length contains 3 extra words :
1308 . status, length, and an extra word with an odd byte .
1312 if ( !(status & RS_ERRORS ) ){
1313 /* do stuff to make a new packet */
1314 struct sk_buff * skb;
1317 /* read one extra byte */
1318 if ( status & RS_ODDFRAME )
1321 /* set multicast stats */
1322 if ( status & RS_MULTICAST )
1323 lp->stats.multicast++;
1325 skb = dev_alloc_skb( packet_length + 5);
1327 if ( skb == NULL ) {
1328 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
1329 lp->stats.rx_dropped++;
1334 ! This should work without alignment, but it could be
1338 skb_reserve( skb, 2 ); /* 16 bit alignment */
1341 data = skb_put( skb, packet_length);
1344 /* QUESTION: Like in the TX routine, do I want
1345 to send the DWORDs or the bytes first, or some
1346 mixture. A mixture might improve already slow PIO
1348 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1349 packet_length >> 2, packet_length & 3 ));
1350 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1351 /* read the left over bytes */
1352 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1353 packet_length & 0x3 );
1355 PRINTK3((" Reading %d words and %d byte(s) \n",
1356 (packet_length >> 1 ), packet_length & 1 ));
1357 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1358 if ( packet_length & 1 ) {
1359 data += packet_length & ~1;
1360 *(data++) = inb( ioaddr + DATA_1 );
1364 print_packet( data, packet_length );
1367 skb->protocol = eth_type_trans(skb, dev );
1369 dev->last_rx = jiffies;
1370 lp->stats.rx_packets++;
1371 lp->stats.rx_bytes += packet_length;
1374 lp->stats.rx_errors++;
1376 if ( status & RS_ALGNERR ) lp->stats.rx_frame_errors++;
1377 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
1378 lp->stats.rx_length_errors++;
1379 if ( status & RS_BADCRC) lp->stats.rx_crc_errors++;
1383 /* error or good, tell the card to get rid of this packet */
1384 outw( MC_RELEASE, ioaddr + MMU_CMD );
1388 /*************************************************************************
1391 . Purpose: Handle a transmit error message. This will only be called
1392 . when an error, because of the AUTO_RELEASE mode.
1395 . Save pointer and packet no
1396 . Get the packet no from the top of the queue
1397 . check if it's valid ( if not, is this an error??? )
1398 . read the status word
1400 . ( resend? Not really, since we don't want old packets around )
1401 . Restore saved values
1402 ************************************************************************/
1403 static void smc_tx( struct net_device * dev )
1405 int ioaddr = dev->base_addr;
1406 struct smc_local *lp = netdev_priv(dev);
1414 saved_packet = inb( ioaddr + PNR_ARR );
1415 packet_no = inw( ioaddr + FIFO_PORTS );
1418 /* select this as the packet to read from */
1419 outb( packet_no, ioaddr + PNR_ARR );
1421 /* read the first word from this packet */
1422 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1424 tx_status = inw( ioaddr + DATA_1 );
1425 PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1427 lp->stats.tx_errors++;
1428 if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
1429 if ( tx_status & TS_LATCOL ) {
1430 printk(KERN_DEBUG CARDNAME
1431 ": Late collision occurred on last xmit.\n");
1432 lp->stats.tx_window_errors++;
1435 if ( tx_status & TS_16COL ) { ... }
1438 if ( tx_status & TS_SUCCESS ) {
1439 printk(CARDNAME": Successful packet caused interrupt \n");
1441 /* re-enable transmit */
1442 SMC_SELECT_BANK( 0 );
1443 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1445 /* kill the packet */
1446 SMC_SELECT_BANK( 2 );
1447 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1449 /* one less packet waiting for me */
1450 lp->packets_waiting--;
1452 outb( saved_packet, ioaddr + PNR_ARR );
1456 /*----------------------------------------------------
1459 . this makes the board clean up everything that it can
1460 . and not talk to the outside world. Caused by
1461 . an 'ifconfig ethX down'
1463 -----------------------------------------------------*/
1464 static int smc_close(struct net_device *dev)
1466 netif_stop_queue(dev);
1467 /* clear everything */
1468 smc_shutdown( dev->base_addr );
1470 /* Update the statistics here. */
1474 /*------------------------------------------------------------
1475 . Get the current statistics.
1476 . This may be called with the card open or closed.
1477 .-------------------------------------------------------------*/
1478 static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
1479 struct smc_local *lp = netdev_priv(dev);
1484 /*-----------------------------------------------------------
1485 . smc_set_multicast_list
1487 . This routine will, depending on the values passed to it,
1488 . either make it accept multicast packets, go into
1489 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1490 . a select set of multicast packets
1492 static void smc_set_multicast_list(struct net_device *dev)
1494 short ioaddr = dev->base_addr;
1497 if ( dev->flags & IFF_PROMISC )
1498 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1500 /* BUG? I never disable promiscuous mode if multicasting was turned on.
1501 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1502 when promiscuous mode is turned on.
1505 /* Here, I am setting this to accept all multicast packets.
1506 I don't need to zero the multicast table, because the flag is
1507 checked before the table is
1509 else if (dev->flags & IFF_ALLMULTI)
1510 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1512 /* We just get all multicast packets even if we only want them
1513 . from one source. This will be changed at some future
1515 else if (dev->mc_count ) {
1516 /* support hardware multicasting */
1518 /* be sure I get rid of flags I might have set */
1519 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1521 /* NOTE: this has to set the bank, so make sure it is the
1522 last thing called. The bank is set to zero at the top */
1523 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1526 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1530 since I'm disabling all multicast entirely, I need to
1531 clear the multicast list
1533 SMC_SELECT_BANK( 3 );
1534 outw( 0, ioaddr + MULTICAST1 );
1535 outw( 0, ioaddr + MULTICAST2 );
1536 outw( 0, ioaddr + MULTICAST3 );
1537 outw( 0, ioaddr + MULTICAST4 );
1543 static struct net_device *devSMC9194;
1544 MODULE_LICENSE("GPL");
1546 MODULE_PARM(io, "i");
1547 MODULE_PARM(irq, "i");
1548 MODULE_PARM(ifport, "i");
1549 MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1550 MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1551 MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1553 int init_module(void)
1557 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1559 /* copy the parameters from insmod into the device structure */
1560 devSMC9194 = smc_init(-1);
1561 if (IS_ERR(devSMC9194))
1562 return PTR_ERR(devSMC9194);
1566 void cleanup_module(void)
1568 unregister_netdev(devSMC9194);
1569 free_irq(devSMC9194->irq, devSMC9194);
1570 release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1571 free_netdev(devSMC9194);