blob: e02471b2f2b5c483e9238b2e2c783e843edefa74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*------------------------------------------------------------------------
2 . smc9194.c
3 . This is a driver for SMC's 9000 series of Ethernet cards.
4 .
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.
8 .
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 )
13 .
14 . Arguments:
15 . io = for the base address
16 . irq = for the IRQ
17 . ifport = 0 for autodetect, 1 for TP, 2 for AUI ( or 10base2 )
18 .
19 . author:
20 . Erik Stahlman ( erik@vt.edu )
21 . contributors:
22 . Arnaldo Carvalho de Melo <acme@conectiva.com.br>
23 .
24 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
25 .
26 . Sources:
27 . o SMC databook
28 . o skeleton.c by Donald Becker ( becker@scyld.com )
29 . o ( a LOT of advice from Becker as well )
30 .
31 . History:
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
51 . allocation
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 ----------------------------------------------------------------------------*/
56
57static const char version[] =
58 "smc9194.c:v0.14 12/15/00 by Erik Stahlman (erik@vt.edu)\n";
59
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>
66#include <linux/in.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>
75#include <linux/bitops.h>
76
77#include <asm/io.h>
78
79#include "smc9194.h"
80
81#define DRV_NAME "smc9194"
82
83/*------------------------------------------------------------------------
84 .
85 . Configuration options, for the experienced user to change.
86 .
87 -------------------------------------------------------------------------*/
88
89/*
90 . Do you want to use 32 bit xfers? This should work on all chips, as
91 . the chipset is designed to accommodate them.
92*/
93#ifdef __sh__
94#undef USE_32_BIT
95#else
96#define USE_32_BIT 1
97#endif
98
99#if defined(__H8300H__) || defined(__H8300S__)
100#define NO_AUTOPROBE
101#undef insl
102#undef outsl
103#define insl(a,b,l) io_insl_noswap(a,b,l)
104#define outsl(a,b,l) io_outsl_noswap(a,b,l)
105#endif
106
107/*
108 .the SMC9194 can be at any of the following port addresses. To change,
109 .for a slightly different card, you can add it to the array. Keep in
110 .mind that the array must end in zero.
111*/
112
113struct devlist {
114 unsigned int port;
115 unsigned int irq;
116};
117
118#if defined(CONFIG_H8S_EDOSK2674)
119static struct devlist smc_devlist[] __initdata = {
120 {.port = 0xf80000, .irq = 16},
121 {.port = 0, .irq = 0 },
122};
123#else
124static struct devlist smc_devlist[] __initdata = {
125 {.port = 0x200, .irq = 0},
126 {.port = 0x220, .irq = 0},
127 {.port = 0x240, .irq = 0},
128 {.port = 0x260, .irq = 0},
129 {.port = 0x280, .irq = 0},
130 {.port = 0x2A0, .irq = 0},
131 {.port = 0x2C0, .irq = 0},
132 {.port = 0x2E0, .irq = 0},
133 {.port = 0x300, .irq = 0},
134 {.port = 0x320, .irq = 0},
135 {.port = 0x340, .irq = 0},
136 {.port = 0x360, .irq = 0},
137 {.port = 0x380, .irq = 0},
138 {.port = 0x3A0, .irq = 0},
139 {.port = 0x3C0, .irq = 0},
140 {.port = 0x3E0, .irq = 0},
141 {.port = 0, .irq = 0},
142};
143#endif
144/*
145 . Wait time for memory to be free. This probably shouldn't be
146 . tuned that much, as waiting for this means nothing else happens
147 . in the system
148*/
149#define MEMORY_WAIT_TIME 16
150
151/*
152 . DEBUGGING LEVELS
153 .
154 . 0 for normal operation
155 . 1 for slightly more details
156 . >2 for various levels of increasingly useless information
157 . 2 for interrupt tracking, status flags
158 . 3 for packet dumps, etc.
159*/
160#define SMC_DEBUG 0
161
162#if (SMC_DEBUG > 2 )
163#define PRINTK3(x) printk x
164#else
165#define PRINTK3(x)
166#endif
167
168#if SMC_DEBUG > 1
169#define PRINTK2(x) printk x
170#else
171#define PRINTK2(x)
172#endif
173
174#ifdef SMC_DEBUG
175#define PRINTK(x) printk x
176#else
177#define PRINTK(x)
178#endif
179
180
181/*------------------------------------------------------------------------
182 .
183 . The internal workings of the driver. If you are changing anything
184 . here with the SMC stuff, you should have the datasheet and known
185 . what you are doing.
186 .
187 -------------------------------------------------------------------------*/
188#define CARDNAME "SMC9194"
189
190
191/* store this information for the driver.. */
192struct smc_local {
193 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 If I have to wait until memory is available to send
195 a packet, I will store the skbuff here, until I get the
196 desired memory. Then, I'll send it out and free it.
197 */
198 struct sk_buff * saved_skb;
199
200 /*
201 . This keeps track of how many packets that I have
202 . sent out. When an TX_EMPTY interrupt comes, I know
203 . that all of these have been sent.
204 */
205 int packets_waiting;
206};
207
208
209/*-----------------------------------------------------------------
210 .
211 . The driver can be entered at any of the following entry points.
212 .
213 .------------------------------------------------------------------ */
214
215/*
216 . This is called by register_netdev(). It is responsible for
217 . checking the portlist for the SMC9000 series chipset. If it finds
218 . one, then it will initialize the device, find the hardware information,
219 . and sets up the appropriate device parameters.
220 . NOTE: Interrupts are *OFF* when this procedure is called.
221 .
222 . NB:This shouldn't be static since it is referred to externally.
223*/
224struct net_device *smc_init(int unit);
225
226/*
227 . The kernel calls this function when someone wants to use the device,
228 . typically 'ifconfig ethX up'.
229*/
230static int smc_open(struct net_device *dev);
231
232/*
233 . Our watchdog timed out. Called by the networking layer
234*/
235static void smc_timeout(struct net_device *dev);
236
237/*
238 . This is called by the kernel in response to 'ifconfig ethX down'. It
239 . is responsible for cleaning up everything that the open routine
240 . does, and maybe putting the card into a powerdown state.
241*/
242static int smc_close(struct net_device *dev);
243
244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
246 . programs ) and multicast modes.
247*/
248static void smc_set_multicast_list(struct net_device *dev);
249
250
251/*---------------------------------------------------------------
252 .
253 . Interrupt level calls..
254 .
255 ----------------------------------------------------------------*/
256
257/*
258 . Handles the actual interrupt
259*/
David Howells7d12e782006-10-05 14:55:46 +0100260static irqreturn_t smc_interrupt(int irq, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
262 . This is a separate procedure to handle the receipt of a packet, to
263 . leave the interrupt code looking slightly cleaner
264*/
265static inline void smc_rcv( struct net_device *dev );
266/*
267 . This handles a TX interrupt, which is only called when an error
268 . relating to a packet is sent.
269*/
270static inline void smc_tx( struct net_device * dev );
271
272/*
273 ------------------------------------------------------------
274 .
275 . Internal routines
276 .
277 ------------------------------------------------------------
278*/
279
280/*
281 . Test if a given location contains a chip, trying to cause as
282 . little damage as possible if it's not a SMC chip.
283*/
284static int smc_probe(struct net_device *dev, int ioaddr);
285
286/*
287 . A rather simple routine to print out a packet for debugging purposes.
288*/
289#if SMC_DEBUG > 2
290static void print_packet( byte *, int );
291#endif
292
293#define tx_done(dev) 1
294
295/* this is called to actually send the packet to the chip */
296static void smc_hardware_send_packet( struct net_device * dev );
297
298/* Since I am not sure if I will have enough room in the chip's ram
299 . to store the packet, I call this routine, which either sends it
300 . now, or generates an interrupt when the card is ready for the
301 . packet */
302static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );
303
304/* this does a soft reset on the device */
305static void smc_reset( int ioaddr );
306
307/* Enable Interrupts, Receive, and Transmit */
308static void smc_enable( int ioaddr );
309
310/* this puts the device in an inactive state */
311static void smc_shutdown( int ioaddr );
312
313/* This routine will find the IRQ of the driver if one is not
314 . specified in the input to the device. */
315static int smc_findirq( int ioaddr );
316
317/*
318 . Function: smc_reset( int ioaddr )
319 . Purpose:
320 . This sets the SMC91xx chip to its normal state, hopefully from whatever
321 . mess that any other DOS driver has put it in.
322 .
323 . Maybe I should reset more registers to defaults in here? SOFTRESET should
324 . do that for me.
325 .
326 . Method:
327 . 1. send a SOFT RESET
328 . 2. wait for it to finish
329 . 3. enable autorelease mode
330 . 4. reset the memory management unit
331 . 5. clear all interrupts
332 .
333*/
334static void smc_reset( int ioaddr )
335{
336 /* This resets the registers mostly to defaults, but doesn't
337 affect EEPROM. That seems unnecessary */
338 SMC_SELECT_BANK( 0 );
339 outw( RCR_SOFTRESET, ioaddr + RCR );
340
341 /* this should pause enough for the chip to be happy */
342 SMC_DELAY( );
343
344 /* Set the transmit and receive configuration registers to
345 default values */
346 outw( RCR_CLEAR, ioaddr + RCR );
347 outw( TCR_CLEAR, ioaddr + TCR );
348
349 /* set the control register to automatically
350 release successfully transmitted packets, to make the best
351 use out of our limited memory */
352 SMC_SELECT_BANK( 1 );
353 outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
354
355 /* Reset the MMU */
356 SMC_SELECT_BANK( 2 );
357 outw( MC_RESET, ioaddr + MMU_CMD );
358
359 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
360 but this is a place where future chipsets _COULD_ break. Be wary
361 of issuing another MMU command right after this */
362
363 outb( 0, ioaddr + INT_MASK );
364}
365
366/*
367 . Function: smc_enable
368 . Purpose: let the chip talk to the outside work
369 . Method:
370 . 1. Enable the transmitter
371 . 2. Enable the receiver
372 . 3. Enable interrupts
373*/
374static void smc_enable( int ioaddr )
375{
376 SMC_SELECT_BANK( 0 );
377 /* see the header file for options in TCR/RCR NORMAL*/
378 outw( TCR_NORMAL, ioaddr + TCR );
379 outw( RCR_NORMAL, ioaddr + RCR );
380
381 /* now, enable interrupts */
382 SMC_SELECT_BANK( 2 );
383 outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
384}
385
386/*
387 . Function: smc_shutdown
388 . Purpose: closes down the SMC91xxx chip.
389 . Method:
390 . 1. zero the interrupt mask
391 . 2. clear the enable receive flag
392 . 3. clear the enable xmit flags
393 .
394 . TODO:
395 . (1) maybe utilize power down mode.
396 . Why not yet? Because while the chip will go into power down mode,
397 . the manual says that it will wake up in response to any I/O requests
398 . in the register space. Empirical results do not show this working.
399*/
400static void smc_shutdown( int ioaddr )
401{
402 /* no more interrupts for me */
403 SMC_SELECT_BANK( 2 );
404 outb( 0, ioaddr + INT_MASK );
405
406 /* and tell the card to stay away from that nasty outside world */
407 SMC_SELECT_BANK( 0 );
408 outb( RCR_CLEAR, ioaddr + RCR );
409 outb( TCR_CLEAR, ioaddr + TCR );
410#if 0
411 /* finally, shut the chip down */
412 SMC_SELECT_BANK( 1 );
413 outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL );
414#endif
415}
416
417
418/*
419 . Function: smc_setmulticast( int ioaddr, int count, dev_mc_list * adds )
420 . Purpose:
421 . This sets the internal hardware table to filter out unwanted multicast
422 . packets before they take up memory.
423 .
424 . The SMC chip uses a hash table where the high 6 bits of the CRC of
425 . address are the offset into the table. If that bit is 1, then the
426 . multicast packet is accepted. Otherwise, it's dropped silently.
427 .
428 . To use the 6 bits as an offset into the table, the high 3 bits are the
429 . number of the 8 bit register, while the low 3 bits are the bit within
430 . that register.
431 .
432 . This routine is based very heavily on the one provided by Peter Cammaert.
433*/
434
435
436static void smc_setmulticast( int ioaddr, int count, struct dev_mc_list * addrs ) {
437 int i;
438 unsigned char multicast_table[ 8 ];
439 struct dev_mc_list * cur_addr;
440 /* table for flipping the order of 3 bits */
441 unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
442
443 /* start with a table of all zeros: reject all */
444 memset( multicast_table, 0, sizeof( multicast_table ) );
445
446 cur_addr = addrs;
447 for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next ) {
448 int position;
449
450 /* do we have a pointer here? */
451 if ( !cur_addr )
452 break;
453 /* make sure this is a multicast address - shouldn't this
454 be a given if we have it here ? */
455 if ( !( *cur_addr->dmi_addr & 1 ) )
456 continue;
457
458 /* only use the low order bits */
459 position = ether_crc_le(6, cur_addr->dmi_addr) & 0x3f;
460
461 /* do some messy swapping to put the bit in the right spot */
462 multicast_table[invert3[position&7]] |=
463 (1<<invert3[(position>>3)&7]);
464
465 }
466 /* now, the table can be loaded into the chipset */
467 SMC_SELECT_BANK( 3 );
468
469 for ( i = 0; i < 8 ; i++ ) {
470 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
471 }
472}
473
474/*
475 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
476 . Purpose:
477 . Attempt to allocate memory for a packet, if chip-memory is not
478 . available, then tell the card to generate an interrupt when it
479 . is available.
480 .
481 . Algorithm:
482 .
483 . o if the saved_skb is not currently null, then drop this packet
484 . on the floor. This should never happen, because of TBUSY.
485 . o if the saved_skb is null, then replace it with the current packet,
486 . o See if I can sending it now.
487 . o (NO): Enable interrupts and let the interrupt handler deal with it.
488 . o (YES):Send it now.
489*/
490static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
491{
492 struct smc_local *lp = netdev_priv(dev);
493 unsigned int ioaddr = dev->base_addr;
494 word length;
495 unsigned short numPages;
496 word time_out;
497
498 netif_stop_queue(dev);
499 /* Well, I want to send the packet.. but I don't know
500 if I can send it right now... */
501
502 if ( lp->saved_skb) {
503 /* THIS SHOULD NEVER HAPPEN. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700504 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
Patrick McHardy5b548142009-06-12 06:22:29 +0000506 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 lp->saved_skb = skb;
509
510 length = skb->len;
511
512 if (length < ETH_ZLEN) {
Herbert Xu5b057c62006-06-23 02:06:41 -0700513 if (skb_padto(skb, ETH_ZLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 netif_wake_queue(dev);
515 return 0;
516 }
517 length = ETH_ZLEN;
518 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
521 ** The MMU wants the number of pages to be the number of 256 bytes
522 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
523 **
524 ** Pkt size for allocating is data length +6 (for additional status words,
525 ** length and ctl!) If odd size last byte is included in this header.
526 */
527 numPages = ((length & 0xfffe) + 6) / 256;
528
529 if (numPages > 7 ) {
530 printk(CARDNAME": Far too big packet error. \n");
531 /* freeing the packet is a good thing here... but should
532 . any packets of this size get down here? */
533 dev_kfree_skb (skb);
534 lp->saved_skb = NULL;
535 /* this IS an error, but, i don't want the skb saved */
536 netif_wake_queue(dev);
537 return 0;
538 }
539 /* either way, a packet is waiting now */
540 lp->packets_waiting++;
541
542 /* now, try to allocate the memory */
543 SMC_SELECT_BANK( 2 );
544 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
545 /*
546 . Performance Hack
547 .
548 . wait a short amount of time.. if I can send a packet now, I send
549 . it now. Otherwise, I enable an interrupt and wait for one to be
550 . available.
551 .
552 . I could have handled this a slightly different way, by checking to
553 . see if any memory was available in the FREE MEMORY register. However,
554 . either way, I need to generate an allocation, and the allocation works
555 . no matter what, so I saw no point in checking free memory.
556 */
557 time_out = MEMORY_WAIT_TIME;
558 do {
559 word status;
560
561 status = inb( ioaddr + INTERRUPT );
562 if ( status & IM_ALLOC_INT ) {
563 /* acknowledge the interrupt */
564 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
565 break;
566 }
567 } while ( -- time_out );
568
569 if ( !time_out ) {
570 /* oh well, wait until the chip finds memory later */
571 SMC_ENABLE_INT( IM_ALLOC_INT );
572 PRINTK2((CARDNAME": memory allocation deferred. \n"));
573 /* it's deferred, but I'll handle it later */
574 return 0;
575 }
576 /* or YES! I can send the packet now.. */
577 smc_hardware_send_packet(dev);
578 netif_wake_queue(dev);
579 return 0;
580}
581
582/*
583 . Function: smc_hardware_send_packet(struct net_device * )
584 . Purpose:
585 . This sends the actual packet to the SMC9xxx chip.
586 .
587 . Algorithm:
588 . First, see if a saved_skb is available.
589 . ( this should NOT be called if there is no 'saved_skb'
590 . Now, find the packet number that the chip allocated
591 . Point the data pointers at it in memory
592 . Set the length word in the chip's memory
593 . Dump the packet to chip memory
594 . Check if a last byte is needed ( odd length packet )
595 . if so, set the control flag right
596 . Tell the card to send it
597 . Enable the transmit interrupt, so I know if it failed
598 . Free the kernel data if I actually sent it.
599*/
600static void smc_hardware_send_packet( struct net_device * dev )
601{
602 struct smc_local *lp = netdev_priv(dev);
603 byte packet_no;
604 struct sk_buff * skb = lp->saved_skb;
605 word length;
606 unsigned int ioaddr;
607 byte * buf;
608
609 ioaddr = dev->base_addr;
610
611 if ( !skb ) {
612 PRINTK((CARDNAME": In XMIT with no packet to send \n"));
613 return;
614 }
615 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
616 buf = skb->data;
617
618 /* If I get here, I _know_ there is a packet slot waiting for me */
619 packet_no = inb( ioaddr + PNR_ARR + 1 );
620 if ( packet_no & 0x80 ) {
621 /* or isn't there? BAD CHIP! */
622 printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
623 dev_kfree_skb_any(skb);
624 lp->saved_skb = NULL;
625 netif_wake_queue(dev);
626 return;
627 }
628
629 /* we have a packet address, so tell the card to use it */
630 outb( packet_no, ioaddr + PNR_ARR );
631
632 /* point to the beginning of the packet */
633 outw( PTR_AUTOINC , ioaddr + POINTER );
634
635 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
636#if SMC_DEBUG > 2
637 print_packet( buf, length );
638#endif
639
640 /* send the packet length ( +6 for status, length and ctl byte )
641 and the status word ( set to zeros ) */
642#ifdef USE_32_BIT
643 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
644#else
645 outw( 0, ioaddr + DATA_1 );
646 /* send the packet length ( +6 for status words, length, and ctl*/
647 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
648 outb( (length+6) >> 8 , ioaddr + DATA_1 );
649#endif
650
651 /* send the actual data
652 . I _think_ it's faster to send the longs first, and then
653 . mop up by sending the last word. It depends heavily
654 . on alignment, at least on the 486. Maybe it would be
655 . a good idea to check which is optimal? But that could take
656 . almost as much time as is saved?
657 */
658#ifdef USE_32_BIT
659 if ( length & 0x2 ) {
660 outsl(ioaddr + DATA_1, buf, length >> 2 );
661#if !defined(__H8300H__) && !defined(__H8300S__)
662 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
663#else
664 ctrl_outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
665#endif
666 }
667 else
668 outsl(ioaddr + DATA_1, buf, length >> 2 );
669#else
670 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
671#endif
672 /* Send the last byte, if there is one. */
673
674 if ( (length & 1) == 0 ) {
675 outw( 0, ioaddr + DATA_1 );
676 } else {
677 outb( buf[length -1 ], ioaddr + DATA_1 );
678 outb( 0x20, ioaddr + DATA_1);
679 }
680
681 /* enable the interrupts */
682 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
683
684 /* and let the chipset deal with it */
685 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
686
687 PRINTK2((CARDNAME": Sent packet of length %d \n",length));
688
689 lp->saved_skb = NULL;
690 dev_kfree_skb_any (skb);
691
692 dev->trans_start = jiffies;
693
694 /* we can send another packet */
695 netif_wake_queue(dev);
696
697 return;
698}
699
700/*-------------------------------------------------------------------------
701 |
702 | smc_init(int unit)
703 | Input parameters:
704 | dev->base_addr == 0, try to find all possible locations
705 | dev->base_addr == 1, return failure code
706 | dev->base_addr == 2, always allocate space, and return success
707 | dev->base_addr == <anything else> this is the address to check
708 |
709 | Output:
710 | pointer to net_device or ERR_PTR(error)
711 |
712 ---------------------------------------------------------------------------
713*/
714static int io;
715static int irq;
716static int ifport;
717
718struct net_device * __init smc_init(int unit)
719{
720 struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
Randy Dunlapa2bd2ec2006-06-10 13:30:10 -0700721 struct devlist *smcdev = smc_devlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int err = 0;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!dev)
725 return ERR_PTR(-ENODEV);
726
727 if (unit >= 0) {
728 sprintf(dev->name, "eth%d", unit);
729 netdev_boot_setup_check(dev);
730 io = dev->base_addr;
731 irq = dev->irq;
732 }
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (io > 0x1ff) { /* Check a single specified location. */
735 err = smc_probe(dev, io);
736 } else if (io != 0) { /* Don't probe at all. */
737 err = -ENXIO;
738 } else {
739 for (;smcdev->port; smcdev++) {
740 if (smc_probe(dev, smcdev->port) == 0)
741 break;
742 }
743 if (!smcdev->port)
744 err = -ENODEV;
745 }
746 if (err)
747 goto out;
748 err = register_netdev(dev);
749 if (err)
750 goto out1;
751 return dev;
752out1:
753 free_irq(dev->irq, dev);
754 release_region(dev->base_addr, SMC_IO_EXTENT);
755out:
756 free_netdev(dev);
757 return ERR_PTR(err);
758}
759
760/*----------------------------------------------------------------------
761 . smc_findirq
762 .
763 . This routine has a simple purpose -- make the SMC chip generate an
764 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
765 ------------------------------------------------------------------------
766*/
Hannes Ederdac499f2008-12-25 23:56:45 -0800767static int __init smc_findirq(int ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769#ifndef NO_AUTOPROBE
770 int timeout = 20;
771 unsigned long cookie;
772
773
774 cookie = probe_irq_on();
775
776 /*
777 * What I try to do here is trigger an ALLOC_INT. This is done
778 * by allocating a small chunk of memory, which will give an interrupt
779 * when done.
780 */
781
782
783 SMC_SELECT_BANK(2);
784 /* enable ALLOCation interrupts ONLY */
785 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
786
787 /*
788 . Allocate 512 bytes of memory. Note that the chip was just
789 . reset so all the memory is available
790 */
791 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
792
793 /*
794 . Wait until positive that the interrupt has been generated
795 */
796 while ( timeout ) {
797 byte int_status;
798
799 int_status = inb( ioaddr + INTERRUPT );
800
801 if ( int_status & IM_ALLOC_INT )
802 break; /* got the interrupt */
803 timeout--;
804 }
805 /* there is really nothing that I can do here if timeout fails,
806 as probe_irq_off will return a 0 anyway, which is what I
807 want in this case. Plus, the clean up is needed in both
808 cases. */
809
810 /* DELAY HERE!
811 On a fast machine, the status might change before the interrupt
812 is given to the processor. This means that the interrupt was
813 never detected, and probe_irq_off fails to report anything.
814 This should fix probe_irq_* problems.
815 */
816 SMC_DELAY();
817 SMC_DELAY();
818
819 /* and disable all interrupts again */
820 outb( 0, ioaddr + INT_MASK );
821
822 /* and return what I found */
823 return probe_irq_off(cookie);
824#else /* NO_AUTOPROBE */
825 struct devlist *smcdev;
826 for (smcdev = smc_devlist; smcdev->port; smcdev++) {
827 if (smcdev->port == ioaddr)
828 return smcdev->irq;
829 }
830 return 0;
831#endif
832}
833
Stephen Hemminger32670c32009-03-26 15:11:29 +0000834static const struct net_device_ops smc_netdev_ops = {
835 .ndo_open = smc_open,
836 .ndo_stop = smc_close,
837 .ndo_start_xmit = smc_wait_to_send_packet,
838 .ndo_tx_timeout = smc_timeout,
839 .ndo_set_multicast_list = smc_set_multicast_list,
840 .ndo_change_mtu = eth_change_mtu,
841 .ndo_set_mac_address = eth_mac_addr,
842 .ndo_validate_addr = eth_validate_addr,
843};
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/*----------------------------------------------------------------------
846 . Function: smc_probe( int ioaddr )
847 .
848 . Purpose:
849 . Tests to see if a given ioaddr points to an SMC9xxx chip.
850 . Returns a 0 on success
851 .
852 . Algorithm:
853 . (1) see if the high byte of BANK_SELECT is 0x33
854 . (2) compare the ioaddr with the base register's address
855 . (3) see if I recognize the chip ID in the appropriate register
856 .
857 .---------------------------------------------------------------------
858 */
859
860/*---------------------------------------------------------------
861 . Here I do typical initialization tasks.
862 .
863 . o Initialize the structure if needed
864 . o print out my vanity message if not done so already
865 . o print out what type of hardware is detected
866 . o print out the ethernet address
867 . o find the IRQ
868 . o set up my private data
869 . o configure the dev structure with my subroutines
870 . o actually GRAB the irq.
871 . o GRAB the region
872 .-----------------------------------------------------------------
873*/
874static int __init smc_probe(struct net_device *dev, int ioaddr)
875{
876 int i, memory, retval;
877 static unsigned version_printed;
878 unsigned int bank;
879
880 const char *version_string;
881 const char *if_string;
882
883 /* registers */
884 word revision_register;
885 word base_address_register;
886 word configuration_register;
887 word memory_info_register;
888 word memory_cfg_register;
889
890 /* Grab the region so that no one else tries to probe our ioports. */
891 if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
892 return -EBUSY;
893
894 dev->irq = irq;
895 dev->if_port = ifport;
896
897 /* First, see if the high byte is 0x33 */
898 bank = inw( ioaddr + BANK_SELECT );
899 if ( (bank & 0xFF00) != 0x3300 ) {
900 retval = -ENODEV;
901 goto err_out;
902 }
903 /* The above MIGHT indicate a device, but I need to write to further
904 test this. */
905 outw( 0x0, ioaddr + BANK_SELECT );
906 bank = inw( ioaddr + BANK_SELECT );
907 if ( (bank & 0xFF00 ) != 0x3300 ) {
908 retval = -ENODEV;
909 goto err_out;
910 }
911#if !defined(CONFIG_H8S_EDOSK2674)
912 /* well, we've already written once, so hopefully another time won't
913 hurt. This time, I need to switch the bank register to bank 1,
914 so I can access the base address register */
915 SMC_SELECT_BANK(1);
916 base_address_register = inw( ioaddr + BASE );
917 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
Joe Perches24500222007-11-19 17:48:28 -0800918 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 "Probably not a SMC chip\n",
920 ioaddr, base_address_register >> 3 & 0x3E0 );
921 /* well, the base address register didn't match. Must not have
922 been a SMC chip after all. */
923 retval = -ENODEV;
924 goto err_out;
925 }
926#else
927 (void)base_address_register; /* Warning suppression */
928#endif
929
930
931 /* check if the revision register is something that I recognize.
932 These might need to be added to later, as future revisions
933 could be added. */
934 SMC_SELECT_BANK(3);
935 revision_register = inw( ioaddr + REVISION );
936 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
937 /* I don't recognize this chip, so... */
938 printk(CARDNAME ": IO %x: Unrecognized revision register:"
939 " %x, Contact author. \n", ioaddr, revision_register );
940
941 retval = -ENODEV;
942 goto err_out;
943 }
944
945 /* at this point I'll assume that the chip is an SMC9xxx.
946 It might be prudent to check a listing of MAC addresses
947 against the hardware address, or do some other tests. */
948
949 if (version_printed++ == 0)
950 printk("%s", version);
951
952 /* fill in some of the fields */
953 dev->base_addr = ioaddr;
954
955 /*
956 . Get the MAC address ( bank 1, regs 4 - 9 )
957 */
958 SMC_SELECT_BANK( 1 );
959 for ( i = 0; i < 6; i += 2 ) {
960 word address;
961
962 address = inw( ioaddr + ADDR0 + i );
963 dev->dev_addr[ i + 1] = address >> 8;
964 dev->dev_addr[ i ] = address & 0xFF;
965 }
966
967 /* get the memory information */
968
969 SMC_SELECT_BANK( 0 );
970 memory_info_register = inw( ioaddr + MIR );
971 memory_cfg_register = inw( ioaddr + MCR );
972 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
973 memory *= 256 * ( memory_info_register & 0xFF );
974
975 /*
976 Now, I want to find out more about the chip. This is sort of
977 redundant, but it's cleaner to have it in both, rather than having
978 one VERY long probe procedure.
979 */
980 SMC_SELECT_BANK(3);
981 revision_register = inw( ioaddr + REVISION );
982 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
983 if ( !version_string ) {
984 /* I shouldn't get here because this call was done before.... */
985 retval = -ENODEV;
986 goto err_out;
987 }
988
989 /* is it using AUI or 10BaseT ? */
990 if ( dev->if_port == 0 ) {
991 SMC_SELECT_BANK(1);
992 configuration_register = inw( ioaddr + CONFIG );
993 if ( configuration_register & CFG_AUI_SELECT )
994 dev->if_port = 2;
995 else
996 dev->if_port = 1;
997 }
998 if_string = interfaces[ dev->if_port - 1 ];
999
1000 /* now, reset the chip, and put it into a known state */
1001 smc_reset( ioaddr );
1002
1003 /*
1004 . If dev->irq is 0, then the device has to be banged on to see
1005 . what the IRQ is.
1006 .
1007 . This banging doesn't always detect the IRQ, for unknown reasons.
1008 . a workaround is to reset the chip and try again.
1009 .
1010 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1011 . be what is requested on the command line. I don't do that, mostly
1012 . because the card that I have uses a non-standard method of accessing
1013 . the IRQs, and because this _should_ work in most configurations.
1014 .
1015 . Specifying an IRQ is done with the assumption that the user knows
1016 . what (s)he is doing. No checking is done!!!!
1017 .
1018 */
1019 if ( dev->irq < 2 ) {
1020 int trials;
1021
1022 trials = 3;
1023 while ( trials-- ) {
1024 dev->irq = smc_findirq( ioaddr );
1025 if ( dev->irq )
1026 break;
1027 /* kick the card and try again */
1028 smc_reset( ioaddr );
1029 }
1030 }
1031 if (dev->irq == 0 ) {
1032 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
1033 retval = -ENODEV;
1034 goto err_out;
1035 }
1036
1037 /* now, print out the card info, in a short format.. */
1038
1039 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
1040 version_string, revision_register & 0xF, ioaddr, dev->irq,
1041 if_string, memory );
1042 /*
1043 . Print the Ethernet address
1044 */
Johannes Berge1749612008-10-27 15:59:26 -07001045 printk("ADDR: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* set the private data to zero by default */
Wang Chen8f15ea42008-11-12 23:38:36 -08001048 memset(netdev_priv(dev), 0, sizeof(struct smc_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* Grab the IRQ */
1051 retval = request_irq(dev->irq, &smc_interrupt, 0, DRV_NAME, dev);
1052 if (retval) {
1053 printk("%s: unable to get IRQ %d (irqval=%d).\n", DRV_NAME,
1054 dev->irq, retval);
1055 goto err_out;
1056 }
1057
Stephen Hemminger32670c32009-03-26 15:11:29 +00001058 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 dev->watchdog_timeo = HZ/20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 return 0;
1062
1063err_out:
1064 release_region(ioaddr, SMC_IO_EXTENT);
1065 return retval;
1066}
1067
1068#if SMC_DEBUG > 2
1069static void print_packet( byte * buf, int length )
1070{
1071#if 0
1072 int i;
1073 int remainder;
1074 int lines;
1075
1076 printk("Packet of length %d \n", length );
1077 lines = length / 16;
1078 remainder = length % 16;
1079
1080 for ( i = 0; i < lines ; i ++ ) {
1081 int cur;
1082
1083 for ( cur = 0; cur < 8; cur ++ ) {
1084 byte a, b;
1085
1086 a = *(buf ++ );
1087 b = *(buf ++ );
1088 printk("%02x%02x ", a, b );
1089 }
1090 printk("\n");
1091 }
1092 for ( i = 0; i < remainder/2 ; i++ ) {
1093 byte a, b;
1094
1095 a = *(buf ++ );
1096 b = *(buf ++ );
1097 printk("%02x%02x ", a, b );
1098 }
1099 printk("\n");
1100#endif
1101}
1102#endif
1103
1104
1105/*
1106 * Open and Initialize the board
1107 *
1108 * Set up everything, reset the card, etc ..
1109 *
1110 */
1111static int smc_open(struct net_device *dev)
1112{
1113 int ioaddr = dev->base_addr;
1114
1115 int i; /* used to set hw ethernet address */
1116
1117 /* clear out all the junk that was put here before... */
Wang Chen8f15ea42008-11-12 23:38:36 -08001118 memset(netdev_priv(dev), 0, sizeof(struct smc_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* reset the hardware */
1121
1122 smc_reset( ioaddr );
1123 smc_enable( ioaddr );
1124
1125 /* Select which interface to use */
1126
1127 SMC_SELECT_BANK( 1 );
1128 if ( dev->if_port == 1 ) {
1129 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1130 ioaddr + CONFIG );
1131 }
1132 else if ( dev->if_port == 2 ) {
1133 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1134 ioaddr + CONFIG );
1135 }
1136
1137 /*
1138 According to Becker, I have to set the hardware address
1139 at this point, because the (l)user can set it with an
1140 ioctl. Easily done...
1141 */
1142 SMC_SELECT_BANK( 1 );
1143 for ( i = 0; i < 6; i += 2 ) {
1144 word address;
1145
1146 address = dev->dev_addr[ i + 1 ] << 8 ;
1147 address |= dev->dev_addr[ i ];
1148 outw( address, ioaddr + ADDR0 + i );
1149 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 netif_start_queue(dev);
1152 return 0;
1153}
1154
1155/*--------------------------------------------------------
1156 . Called by the kernel to send a packet out into the void
1157 . of the net. This routine is largely based on
1158 . skeleton.c, from Becker.
1159 .--------------------------------------------------------
1160*/
1161
1162static void smc_timeout(struct net_device *dev)
1163{
1164 /* If we get here, some higher level has decided we are broken.
1165 There should really be a "kick me" function call instead. */
1166 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1167 tx_done(dev) ? "IRQ conflict" :
1168 "network cable problem");
1169 /* "kick" the adaptor */
1170 smc_reset( dev->base_addr );
1171 smc_enable( dev->base_addr );
1172 dev->trans_start = jiffies;
1173 /* clear anything saved */
Wang Chen8f15ea42008-11-12 23:38:36 -08001174 ((struct smc_local *)netdev_priv(dev))->saved_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 netif_wake_queue(dev);
1176}
1177
1178/*-------------------------------------------------------------
1179 .
1180 . smc_rcv - receive a packet from the card
1181 .
1182 . There is ( at least ) a packet waiting to be read from
1183 . chip-memory.
1184 .
1185 . o Read the status
1186 . o If an error, record it
1187 . o otherwise, read in the packet
1188 --------------------------------------------------------------
1189*/
1190static void smc_rcv(struct net_device *dev)
1191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int ioaddr = dev->base_addr;
1193 int packet_number;
1194 word status;
1195 word packet_length;
1196
1197 /* assume bank 2 */
1198
1199 packet_number = inw( ioaddr + FIFO_PORTS );
1200
1201 if ( packet_number & FP_RXEMPTY ) {
1202 /* we got called , but nothing was on the FIFO */
1203 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
1204 /* don't need to restore anything */
1205 return;
1206 }
1207
1208 /* start reading from the start of the packet */
1209 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1210
1211 /* First two words are status and packet_length */
1212 status = inw( ioaddr + DATA_1 );
1213 packet_length = inw( ioaddr + DATA_1 );
1214
1215 packet_length &= 0x07ff; /* mask off top bits */
1216
1217 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1218 /*
1219 . the packet length contains 3 extra words :
1220 . status, length, and an extra word with an odd byte .
1221 */
1222 packet_length -= 6;
1223
1224 if ( !(status & RS_ERRORS ) ){
1225 /* do stuff to make a new packet */
1226 struct sk_buff * skb;
1227 byte * data;
1228
1229 /* read one extra byte */
1230 if ( status & RS_ODDFRAME )
1231 packet_length++;
1232
1233 /* set multicast stats */
1234 if ( status & RS_MULTICAST )
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001235 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 skb = dev_alloc_skb( packet_length + 5);
1238
1239 if ( skb == NULL ) {
1240 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001241 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto done;
1243 }
1244
1245 /*
1246 ! This should work without alignment, but it could be
1247 ! in the worse case
1248 */
1249
1250 skb_reserve( skb, 2 ); /* 16 bit alignment */
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 data = skb_put( skb, packet_length);
1253
1254#ifdef USE_32_BIT
1255 /* QUESTION: Like in the TX routine, do I want
1256 to send the DWORDs or the bytes first, or some
1257 mixture. A mixture might improve already slow PIO
1258 performance */
1259 PRINTK3((" Reading %d dwords (and %d bytes) \n",
1260 packet_length >> 2, packet_length & 3 ));
1261 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1262 /* read the left over bytes */
1263 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1264 packet_length & 0x3 );
1265#else
1266 PRINTK3((" Reading %d words and %d byte(s) \n",
1267 (packet_length >> 1 ), packet_length & 1 ));
1268 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1269 if ( packet_length & 1 ) {
1270 data += packet_length & ~1;
1271 *(data++) = inb( ioaddr + DATA_1 );
1272 }
1273#endif
1274#if SMC_DEBUG > 2
1275 print_packet( data, packet_length );
1276#endif
1277
1278 skb->protocol = eth_type_trans(skb, dev );
1279 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001280 dev->stats.rx_packets++;
1281 dev->stats.rx_bytes += packet_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 } else {
1283 /* error ... */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001284 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001286 if ( status & RS_ALGNERR ) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001288 dev->stats.rx_length_errors++;
1289 if ( status & RS_BADCRC) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291
1292done:
1293 /* error or good, tell the card to get rid of this packet */
1294 outw( MC_RELEASE, ioaddr + MMU_CMD );
1295}
1296
1297
1298/*************************************************************************
1299 . smc_tx
1300 .
1301 . Purpose: Handle a transmit error message. This will only be called
1302 . when an error, because of the AUTO_RELEASE mode.
1303 .
1304 . Algorithm:
1305 . Save pointer and packet no
1306 . Get the packet no from the top of the queue
1307 . check if it's valid ( if not, is this an error??? )
1308 . read the status word
1309 . record the error
1310 . ( resend? Not really, since we don't want old packets around )
1311 . Restore saved values
1312 ************************************************************************/
1313static void smc_tx( struct net_device * dev )
1314{
1315 int ioaddr = dev->base_addr;
1316 struct smc_local *lp = netdev_priv(dev);
1317 byte saved_packet;
1318 byte packet_no;
1319 word tx_status;
1320
1321
1322 /* assume bank 2 */
1323
1324 saved_packet = inb( ioaddr + PNR_ARR );
1325 packet_no = inw( ioaddr + FIFO_PORTS );
1326 packet_no &= 0x7F;
1327
1328 /* select this as the packet to read from */
1329 outb( packet_no, ioaddr + PNR_ARR );
1330
1331 /* read the first word from this packet */
1332 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1333
1334 tx_status = inw( ioaddr + DATA_1 );
1335 PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));
1336
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001337 dev->stats.tx_errors++;
1338 if ( tx_status & TS_LOSTCAR ) dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if ( tx_status & TS_LATCOL ) {
1340 printk(KERN_DEBUG CARDNAME
1341 ": Late collision occurred on last xmit.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001342 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344#if 0
1345 if ( tx_status & TS_16COL ) { ... }
1346#endif
1347
1348 if ( tx_status & TS_SUCCESS ) {
1349 printk(CARDNAME": Successful packet caused interrupt \n");
1350 }
1351 /* re-enable transmit */
1352 SMC_SELECT_BANK( 0 );
1353 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1354
1355 /* kill the packet */
1356 SMC_SELECT_BANK( 2 );
1357 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1358
1359 /* one less packet waiting for me */
1360 lp->packets_waiting--;
1361
1362 outb( saved_packet, ioaddr + PNR_ARR );
1363 return;
1364}
1365
1366/*--------------------------------------------------------------------
1367 .
1368 . This is the main routine of the driver, to handle the device when
1369 . it needs some attention.
1370 .
1371 . So:
1372 . first, save state of the chipset
1373 . branch off into routines to handle each case, and acknowledge
1374 . each to the interrupt register
1375 . and finally restore state.
1376 .
1377 ---------------------------------------------------------------------*/
1378
David Howells7d12e782006-10-05 14:55:46 +01001379static irqreturn_t smc_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct net_device *dev = dev_id;
1382 int ioaddr = dev->base_addr;
1383 struct smc_local *lp = netdev_priv(dev);
1384
1385 byte status;
1386 word card_stats;
1387 byte mask;
1388 int timeout;
1389 /* state registers */
1390 word saved_bank;
1391 word saved_pointer;
1392 int handled = 0;
1393
1394
1395 PRINTK3((CARDNAME": SMC interrupt started \n"));
1396
1397 saved_bank = inw( ioaddr + BANK_SELECT );
1398
1399 SMC_SELECT_BANK(2);
1400 saved_pointer = inw( ioaddr + POINTER );
1401
1402 mask = inb( ioaddr + INT_MASK );
1403 /* clear all interrupts */
1404 outb( 0, ioaddr + INT_MASK );
1405
1406
1407 /* set a timeout value, so I don't stay here forever */
1408 timeout = 4;
1409
1410 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
1411 do {
1412 /* read the status flag, and mask it */
1413 status = inb( ioaddr + INTERRUPT ) & mask;
1414 if (!status )
1415 break;
1416
1417 handled = 1;
1418
1419 PRINTK3((KERN_WARNING CARDNAME
1420 ": Handling interrupt status %x \n", status ));
1421
1422 if (status & IM_RCV_INT) {
1423 /* Got a packet(s). */
1424 PRINTK2((KERN_WARNING CARDNAME
1425 ": Receive Interrupt\n"));
1426 smc_rcv(dev);
1427 } else if (status & IM_TX_INT ) {
1428 PRINTK2((KERN_WARNING CARDNAME
1429 ": TX ERROR handled\n"));
1430 smc_tx(dev);
1431 outb(IM_TX_INT, ioaddr + INTERRUPT );
1432 } else if (status & IM_TX_EMPTY_INT ) {
1433 /* update stats */
1434 SMC_SELECT_BANK( 0 );
1435 card_stats = inw( ioaddr + COUNTER );
1436 /* single collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001437 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 card_stats >>= 4;
1439 /* multiple collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001440 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* these are for when linux supports these statistics */
1443
1444 SMC_SELECT_BANK( 2 );
1445 PRINTK2((KERN_WARNING CARDNAME
1446 ": TX_BUFFER_EMPTY handled\n"));
1447 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1448 mask &= ~IM_TX_EMPTY_INT;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001449 dev->stats.tx_packets += lp->packets_waiting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 lp->packets_waiting = 0;
1451
1452 } else if (status & IM_ALLOC_INT ) {
1453 PRINTK2((KERN_DEBUG CARDNAME
1454 ": Allocation interrupt \n"));
1455 /* clear this interrupt so it doesn't happen again */
1456 mask &= ~IM_ALLOC_INT;
1457
1458 smc_hardware_send_packet( dev );
1459
1460 /* enable xmit interrupts based on this */
1461 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1462
1463 /* and let the card send more packets to me */
1464 netif_wake_queue(dev);
1465
1466 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1467 } else if (status & IM_RX_OVRN_INT ) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001468 dev->stats.rx_errors++;
1469 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1471 } else if (status & IM_EPH_INT ) {
1472 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
1473 } else if (status & IM_ERCV_INT ) {
1474 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
1475 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1476 }
1477 } while ( timeout -- );
1478
1479
1480 /* restore state register */
1481 SMC_SELECT_BANK( 2 );
1482 outb( mask, ioaddr + INT_MASK );
1483
1484 PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
1485 outw( saved_pointer, ioaddr + POINTER );
1486
1487 SMC_SELECT_BANK( saved_bank );
1488
1489 PRINTK3((CARDNAME ": Interrupt done\n"));
1490 return IRQ_RETVAL(handled);
1491}
1492
1493
1494/*----------------------------------------------------
1495 . smc_close
1496 .
1497 . this makes the board clean up everything that it can
1498 . and not talk to the outside world. Caused by
1499 . an 'ifconfig ethX down'
1500 .
1501 -----------------------------------------------------*/
1502static int smc_close(struct net_device *dev)
1503{
1504 netif_stop_queue(dev);
1505 /* clear everything */
1506 smc_shutdown( dev->base_addr );
1507
1508 /* Update the statistics here. */
1509 return 0;
1510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/*-----------------------------------------------------------
1513 . smc_set_multicast_list
1514 .
1515 . This routine will, depending on the values passed to it,
1516 . either make it accept multicast packets, go into
1517 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1518 . a select set of multicast packets
1519*/
1520static void smc_set_multicast_list(struct net_device *dev)
1521{
1522 short ioaddr = dev->base_addr;
1523
1524 SMC_SELECT_BANK(0);
1525 if ( dev->flags & IFF_PROMISC )
1526 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1527
1528/* BUG? I never disable promiscuous mode if multicasting was turned on.
1529 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1530 when promiscuous mode is turned on.
1531*/
1532
1533 /* Here, I am setting this to accept all multicast packets.
1534 I don't need to zero the multicast table, because the flag is
1535 checked before the table is
1536 */
1537 else if (dev->flags & IFF_ALLMULTI)
1538 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1539
1540 /* We just get all multicast packets even if we only want them
1541 . from one source. This will be changed at some future
1542 . point. */
1543 else if (dev->mc_count ) {
1544 /* support hardware multicasting */
1545
1546 /* be sure I get rid of flags I might have set */
1547 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1548 ioaddr + RCR );
1549 /* NOTE: this has to set the bank, so make sure it is the
1550 last thing called. The bank is set to zero at the top */
1551 smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
1552 }
1553 else {
1554 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1555 ioaddr + RCR );
1556
1557 /*
1558 since I'm disabling all multicast entirely, I need to
1559 clear the multicast list
1560 */
1561 SMC_SELECT_BANK( 3 );
1562 outw( 0, ioaddr + MULTICAST1 );
1563 outw( 0, ioaddr + MULTICAST2 );
1564 outw( 0, ioaddr + MULTICAST3 );
1565 outw( 0, ioaddr + MULTICAST4 );
1566 }
1567}
1568
1569#ifdef MODULE
1570
1571static struct net_device *devSMC9194;
1572MODULE_LICENSE("GPL");
1573
1574module_param(io, int, 0);
1575module_param(irq, int, 0);
1576module_param(ifport, int, 0);
1577MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1578MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1579MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1580
Randy Dunlapa2bd2ec2006-06-10 13:30:10 -07001581int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 if (io == 0)
1584 printk(KERN_WARNING
1585 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1586
1587 /* copy the parameters from insmod into the device structure */
1588 devSMC9194 = smc_init(-1);
1589 if (IS_ERR(devSMC9194))
1590 return PTR_ERR(devSMC9194);
1591 return 0;
1592}
1593
Al Viroafc8eb42006-06-14 18:50:53 -04001594void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 unregister_netdev(devSMC9194);
1597 free_irq(devSMC9194->irq, devSMC9194);
1598 release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1599 free_netdev(devSMC9194);
1600}
1601
1602#endif /* MODULE */