blob: 9e5e9e792e86a384bd95d472b40567895192ec6f [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 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000302static netdev_tx_t smc_wait_to_send_packet( struct sk_buff * skb,
303 struct net_device *dev );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/* this does a soft reset on the device */
306static void smc_reset( int ioaddr );
307
308/* Enable Interrupts, Receive, and Transmit */
309static void smc_enable( int ioaddr );
310
311/* this puts the device in an inactive state */
312static void smc_shutdown( int ioaddr );
313
314/* This routine will find the IRQ of the driver if one is not
315 . specified in the input to the device. */
316static int smc_findirq( int ioaddr );
317
318/*
319 . Function: smc_reset( int ioaddr )
320 . Purpose:
321 . This sets the SMC91xx chip to its normal state, hopefully from whatever
322 . mess that any other DOS driver has put it in.
323 .
324 . Maybe I should reset more registers to defaults in here? SOFTRESET should
325 . do that for me.
326 .
327 . Method:
328 . 1. send a SOFT RESET
329 . 2. wait for it to finish
330 . 3. enable autorelease mode
331 . 4. reset the memory management unit
332 . 5. clear all interrupts
333 .
334*/
335static void smc_reset( int ioaddr )
336{
337 /* This resets the registers mostly to defaults, but doesn't
338 affect EEPROM. That seems unnecessary */
339 SMC_SELECT_BANK( 0 );
340 outw( RCR_SOFTRESET, ioaddr + RCR );
341
342 /* this should pause enough for the chip to be happy */
343 SMC_DELAY( );
344
345 /* Set the transmit and receive configuration registers to
346 default values */
347 outw( RCR_CLEAR, ioaddr + RCR );
348 outw( TCR_CLEAR, ioaddr + TCR );
349
350 /* set the control register to automatically
351 release successfully transmitted packets, to make the best
352 use out of our limited memory */
353 SMC_SELECT_BANK( 1 );
354 outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );
355
356 /* Reset the MMU */
357 SMC_SELECT_BANK( 2 );
358 outw( MC_RESET, ioaddr + MMU_CMD );
359
360 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
361 but this is a place where future chipsets _COULD_ break. Be wary
362 of issuing another MMU command right after this */
363
364 outb( 0, ioaddr + INT_MASK );
365}
366
367/*
368 . Function: smc_enable
369 . Purpose: let the chip talk to the outside work
370 . Method:
371 . 1. Enable the transmitter
372 . 2. Enable the receiver
373 . 3. Enable interrupts
374*/
375static void smc_enable( int ioaddr )
376{
377 SMC_SELECT_BANK( 0 );
378 /* see the header file for options in TCR/RCR NORMAL*/
379 outw( TCR_NORMAL, ioaddr + TCR );
380 outw( RCR_NORMAL, ioaddr + RCR );
381
382 /* now, enable interrupts */
383 SMC_SELECT_BANK( 2 );
384 outb( SMC_INTERRUPT_MASK, ioaddr + INT_MASK );
385}
386
387/*
388 . Function: smc_shutdown
389 . Purpose: closes down the SMC91xxx chip.
390 . Method:
391 . 1. zero the interrupt mask
392 . 2. clear the enable receive flag
393 . 3. clear the enable xmit flags
394 .
395 . TODO:
396 . (1) maybe utilize power down mode.
397 . Why not yet? Because while the chip will go into power down mode,
398 . the manual says that it will wake up in response to any I/O requests
399 . in the register space. Empirical results do not show this working.
400*/
401static void smc_shutdown( int ioaddr )
402{
403 /* no more interrupts for me */
404 SMC_SELECT_BANK( 2 );
405 outb( 0, ioaddr + INT_MASK );
406
407 /* and tell the card to stay away from that nasty outside world */
408 SMC_SELECT_BANK( 0 );
409 outb( RCR_CLEAR, ioaddr + RCR );
410 outb( TCR_CLEAR, ioaddr + TCR );
411#if 0
412 /* finally, shut the chip down */
413 SMC_SELECT_BANK( 1 );
414 outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL );
415#endif
416}
417
418
419/*
Jiri Pirko22bedad2010-04-01 21:22:57 +0000420 . Function: smc_setmulticast( int ioaddr, struct net_device *dev )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 . Purpose:
422 . This sets the internal hardware table to filter out unwanted multicast
423 . packets before they take up memory.
424 .
425 . The SMC chip uses a hash table where the high 6 bits of the CRC of
426 . address are the offset into the table. If that bit is 1, then the
427 . multicast packet is accepted. Otherwise, it's dropped silently.
428 .
429 . To use the 6 bits as an offset into the table, the high 3 bits are the
430 . number of the 8 bit register, while the low 3 bits are the bit within
431 . that register.
432 .
433 . This routine is based very heavily on the one provided by Peter Cammaert.
434*/
435
436
Jiri Pirko567ec872010-02-23 23:17:07 +0000437static void smc_setmulticast(int ioaddr, struct net_device *dev)
438{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int i;
440 unsigned char multicast_table[ 8 ];
Jiri Pirko22bedad2010-04-01 21:22:57 +0000441 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* table for flipping the order of 3 bits */
443 unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
444
445 /* start with a table of all zeros: reject all */
446 memset( multicast_table, 0, sizeof( multicast_table ) );
447
Jiri Pirko22bedad2010-04-01 21:22:57 +0000448 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int position;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* make sure this is a multicast address - shouldn't this
452 be a given if we have it here ? */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000453 if (!(*ha->addr & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 continue;
455
456 /* only use the low order bits */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000457 position = ether_crc_le(6, ha->addr) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* do some messy swapping to put the bit in the right spot */
460 multicast_table[invert3[position&7]] |=
461 (1<<invert3[(position>>3)&7]);
462
463 }
464 /* now, the table can be loaded into the chipset */
465 SMC_SELECT_BANK( 3 );
466
467 for ( i = 0; i < 8 ; i++ ) {
468 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
469 }
470}
471
472/*
473 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
474 . Purpose:
475 . Attempt to allocate memory for a packet, if chip-memory is not
476 . available, then tell the card to generate an interrupt when it
477 . is available.
478 .
479 . Algorithm:
480 .
481 . o if the saved_skb is not currently null, then drop this packet
482 . on the floor. This should never happen, because of TBUSY.
483 . o if the saved_skb is null, then replace it with the current packet,
484 . o See if I can sending it now.
485 . o (NO): Enable interrupts and let the interrupt handler deal with it.
486 . o (YES):Send it now.
487*/
Stephen Hemminger613573252009-08-31 19:50:58 +0000488static netdev_tx_t smc_wait_to_send_packet(struct sk_buff *skb,
489 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct smc_local *lp = netdev_priv(dev);
492 unsigned int ioaddr = dev->base_addr;
493 word length;
494 unsigned short numPages;
495 word time_out;
496
497 netif_stop_queue(dev);
498 /* Well, I want to send the packet.. but I don't know
499 if I can send it right now... */
500
501 if ( lp->saved_skb) {
502 /* THIS SHOULD NEVER HAPPEN. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700503 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
Patrick McHardy5b548142009-06-12 06:22:29 +0000505 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 lp->saved_skb = skb;
508
509 length = skb->len;
510
511 if (length < ETH_ZLEN) {
Herbert Xu5b057c62006-06-23 02:06:41 -0700512 if (skb_padto(skb, ETH_ZLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000514 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 length = ETH_ZLEN;
517 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 ** The MMU wants the number of pages to be the number of 256 bytes
521 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
522 **
523 ** Pkt size for allocating is data length +6 (for additional status words,
524 ** length and ctl!) If odd size last byte is included in this header.
525 */
526 numPages = ((length & 0xfffe) + 6) / 256;
527
528 if (numPages > 7 ) {
Frans Pop84d57bd2010-03-24 07:57:32 +0000529 printk(CARDNAME": Far too big packet error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* freeing the packet is a good thing here... but should
531 . any packets of this size get down here? */
532 dev_kfree_skb (skb);
533 lp->saved_skb = NULL;
534 /* this IS an error, but, i don't want the skb saved */
535 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000536 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 /* either way, a packet is waiting now */
539 lp->packets_waiting++;
540
541 /* now, try to allocate the memory */
542 SMC_SELECT_BANK( 2 );
543 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
544 /*
545 . Performance Hack
546 .
547 . wait a short amount of time.. if I can send a packet now, I send
548 . it now. Otherwise, I enable an interrupt and wait for one to be
549 . available.
550 .
551 . I could have handled this a slightly different way, by checking to
552 . see if any memory was available in the FREE MEMORY register. However,
553 . either way, I need to generate an allocation, and the allocation works
554 . no matter what, so I saw no point in checking free memory.
555 */
556 time_out = MEMORY_WAIT_TIME;
557 do {
558 word status;
559
560 status = inb( ioaddr + INTERRUPT );
561 if ( status & IM_ALLOC_INT ) {
562 /* acknowledge the interrupt */
563 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
564 break;
565 }
566 } while ( -- time_out );
567
568 if ( !time_out ) {
569 /* oh well, wait until the chip finds memory later */
570 SMC_ENABLE_INT( IM_ALLOC_INT );
Frans Pop84d57bd2010-03-24 07:57:32 +0000571 PRINTK2((CARDNAME": memory allocation deferred.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* it's deferred, but I'll handle it later */
Frans Pop84d57bd2010-03-24 07:57:32 +0000573 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 /* or YES! I can send the packet now.. */
576 smc_hardware_send_packet(dev);
577 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000578 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
582 . Function: smc_hardware_send_packet(struct net_device * )
583 . Purpose:
584 . This sends the actual packet to the SMC9xxx chip.
585 .
586 . Algorithm:
587 . First, see if a saved_skb is available.
588 . ( this should NOT be called if there is no 'saved_skb'
589 . Now, find the packet number that the chip allocated
590 . Point the data pointers at it in memory
591 . Set the length word in the chip's memory
592 . Dump the packet to chip memory
593 . Check if a last byte is needed ( odd length packet )
594 . if so, set the control flag right
595 . Tell the card to send it
596 . Enable the transmit interrupt, so I know if it failed
597 . Free the kernel data if I actually sent it.
598*/
599static void smc_hardware_send_packet( struct net_device * dev )
600{
601 struct smc_local *lp = netdev_priv(dev);
602 byte packet_no;
603 struct sk_buff * skb = lp->saved_skb;
604 word length;
605 unsigned int ioaddr;
606 byte * buf;
607
608 ioaddr = dev->base_addr;
609
610 if ( !skb ) {
Frans Pop84d57bd2010-03-24 07:57:32 +0000611 PRINTK((CARDNAME": In XMIT with no packet to send\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return;
613 }
614 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
615 buf = skb->data;
616
617 /* If I get here, I _know_ there is a packet slot waiting for me */
618 packet_no = inb( ioaddr + PNR_ARR + 1 );
619 if ( packet_no & 0x80 ) {
620 /* or isn't there? BAD CHIP! */
Frans Pop84d57bd2010-03-24 07:57:32 +0000621 printk(KERN_DEBUG CARDNAME": Memory allocation failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dev_kfree_skb_any(skb);
623 lp->saved_skb = NULL;
624 netif_wake_queue(dev);
625 return;
626 }
627
628 /* we have a packet address, so tell the card to use it */
629 outb( packet_no, ioaddr + PNR_ARR );
630
631 /* point to the beginning of the packet */
632 outw( PTR_AUTOINC , ioaddr + POINTER );
633
634 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
635#if SMC_DEBUG > 2
636 print_packet( buf, length );
637#endif
638
639 /* send the packet length ( +6 for status, length and ctl byte )
640 and the status word ( set to zeros ) */
641#ifdef USE_32_BIT
642 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
643#else
644 outw( 0, ioaddr + DATA_1 );
645 /* send the packet length ( +6 for status words, length, and ctl*/
646 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
647 outb( (length+6) >> 8 , ioaddr + DATA_1 );
648#endif
649
650 /* send the actual data
651 . I _think_ it's faster to send the longs first, and then
652 . mop up by sending the last word. It depends heavily
653 . on alignment, at least on the 486. Maybe it would be
654 . a good idea to check which is optimal? But that could take
655 . almost as much time as is saved?
656 */
657#ifdef USE_32_BIT
658 if ( length & 0x2 ) {
659 outsl(ioaddr + DATA_1, buf, length >> 2 );
660#if !defined(__H8300H__) && !defined(__H8300S__)
661 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
662#else
663 ctrl_outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
664#endif
665 }
666 else
667 outsl(ioaddr + DATA_1, buf, length >> 2 );
668#else
669 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
670#endif
671 /* Send the last byte, if there is one. */
672
673 if ( (length & 1) == 0 ) {
674 outw( 0, ioaddr + DATA_1 );
675 } else {
676 outb( buf[length -1 ], ioaddr + DATA_1 );
677 outb( 0x20, ioaddr + DATA_1);
678 }
679
680 /* enable the interrupts */
681 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
682
683 /* and let the chipset deal with it */
684 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
685
Frans Pop84d57bd2010-03-24 07:57:32 +0000686 PRINTK2((CARDNAME": Sent packet of length %d\n", length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 lp->saved_skb = NULL;
689 dev_kfree_skb_any (skb);
690
691 dev->trans_start = jiffies;
692
693 /* we can send another packet */
694 netif_wake_queue(dev);
695
696 return;
697}
698
699/*-------------------------------------------------------------------------
700 |
701 | smc_init(int unit)
702 | Input parameters:
703 | dev->base_addr == 0, try to find all possible locations
704 | dev->base_addr == 1, return failure code
705 | dev->base_addr == 2, always allocate space, and return success
706 | dev->base_addr == <anything else> this is the address to check
707 |
708 | Output:
709 | pointer to net_device or ERR_PTR(error)
710 |
711 ---------------------------------------------------------------------------
712*/
713static int io;
714static int irq;
715static int ifport;
716
717struct net_device * __init smc_init(int unit)
718{
719 struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
Randy Dunlapa2bd2ec2006-06-10 13:30:10 -0700720 struct devlist *smcdev = smc_devlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int err = 0;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!dev)
724 return ERR_PTR(-ENODEV);
725
726 if (unit >= 0) {
727 sprintf(dev->name, "eth%d", unit);
728 netdev_boot_setup_check(dev);
729 io = dev->base_addr;
730 irq = dev->irq;
731 }
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (io > 0x1ff) { /* Check a single specified location. */
734 err = smc_probe(dev, io);
735 } else if (io != 0) { /* Don't probe at all. */
736 err = -ENXIO;
737 } else {
738 for (;smcdev->port; smcdev++) {
739 if (smc_probe(dev, smcdev->port) == 0)
740 break;
741 }
742 if (!smcdev->port)
743 err = -ENODEV;
744 }
745 if (err)
746 goto out;
747 err = register_netdev(dev);
748 if (err)
749 goto out1;
750 return dev;
751out1:
752 free_irq(dev->irq, dev);
753 release_region(dev->base_addr, SMC_IO_EXTENT);
754out:
755 free_netdev(dev);
756 return ERR_PTR(err);
757}
758
759/*----------------------------------------------------------------------
760 . smc_findirq
761 .
762 . This routine has a simple purpose -- make the SMC chip generate an
763 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
764 ------------------------------------------------------------------------
765*/
Hannes Ederdac499f2008-12-25 23:56:45 -0800766static int __init smc_findirq(int ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768#ifndef NO_AUTOPROBE
769 int timeout = 20;
770 unsigned long cookie;
771
772
773 cookie = probe_irq_on();
774
775 /*
776 * What I try to do here is trigger an ALLOC_INT. This is done
777 * by allocating a small chunk of memory, which will give an interrupt
778 * when done.
779 */
780
781
782 SMC_SELECT_BANK(2);
783 /* enable ALLOCation interrupts ONLY */
784 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
785
786 /*
787 . Allocate 512 bytes of memory. Note that the chip was just
788 . reset so all the memory is available
789 */
790 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
791
792 /*
793 . Wait until positive that the interrupt has been generated
794 */
795 while ( timeout ) {
796 byte int_status;
797
798 int_status = inb( ioaddr + INTERRUPT );
799
800 if ( int_status & IM_ALLOC_INT )
801 break; /* got the interrupt */
802 timeout--;
803 }
804 /* there is really nothing that I can do here if timeout fails,
805 as probe_irq_off will return a 0 anyway, which is what I
806 want in this case. Plus, the clean up is needed in both
807 cases. */
808
809 /* DELAY HERE!
810 On a fast machine, the status might change before the interrupt
811 is given to the processor. This means that the interrupt was
812 never detected, and probe_irq_off fails to report anything.
813 This should fix probe_irq_* problems.
814 */
815 SMC_DELAY();
816 SMC_DELAY();
817
818 /* and disable all interrupts again */
819 outb( 0, ioaddr + INT_MASK );
820
821 /* and return what I found */
822 return probe_irq_off(cookie);
823#else /* NO_AUTOPROBE */
824 struct devlist *smcdev;
825 for (smcdev = smc_devlist; smcdev->port; smcdev++) {
826 if (smcdev->port == ioaddr)
827 return smcdev->irq;
828 }
829 return 0;
830#endif
831}
832
Stephen Hemminger32670c32009-03-26 15:11:29 +0000833static const struct net_device_ops smc_netdev_ops = {
834 .ndo_open = smc_open,
835 .ndo_stop = smc_close,
836 .ndo_start_xmit = smc_wait_to_send_packet,
837 .ndo_tx_timeout = smc_timeout,
838 .ndo_set_multicast_list = smc_set_multicast_list,
839 .ndo_change_mtu = eth_change_mtu,
840 .ndo_set_mac_address = eth_mac_addr,
841 .ndo_validate_addr = eth_validate_addr,
842};
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*----------------------------------------------------------------------
845 . Function: smc_probe( int ioaddr )
846 .
847 . Purpose:
848 . Tests to see if a given ioaddr points to an SMC9xxx chip.
849 . Returns a 0 on success
850 .
851 . Algorithm:
852 . (1) see if the high byte of BANK_SELECT is 0x33
853 . (2) compare the ioaddr with the base register's address
854 . (3) see if I recognize the chip ID in the appropriate register
855 .
856 .---------------------------------------------------------------------
857 */
858
859/*---------------------------------------------------------------
860 . Here I do typical initialization tasks.
861 .
862 . o Initialize the structure if needed
863 . o print out my vanity message if not done so already
864 . o print out what type of hardware is detected
865 . o print out the ethernet address
866 . o find the IRQ
867 . o set up my private data
868 . o configure the dev structure with my subroutines
869 . o actually GRAB the irq.
870 . o GRAB the region
871 .-----------------------------------------------------------------
872*/
873static int __init smc_probe(struct net_device *dev, int ioaddr)
874{
875 int i, memory, retval;
876 static unsigned version_printed;
877 unsigned int bank;
878
879 const char *version_string;
880 const char *if_string;
881
882 /* registers */
883 word revision_register;
884 word base_address_register;
885 word configuration_register;
886 word memory_info_register;
887 word memory_cfg_register;
888
889 /* Grab the region so that no one else tries to probe our ioports. */
890 if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
891 return -EBUSY;
892
893 dev->irq = irq;
894 dev->if_port = ifport;
895
896 /* First, see if the high byte is 0x33 */
897 bank = inw( ioaddr + BANK_SELECT );
898 if ( (bank & 0xFF00) != 0x3300 ) {
899 retval = -ENODEV;
900 goto err_out;
901 }
902 /* The above MIGHT indicate a device, but I need to write to further
903 test this. */
904 outw( 0x0, ioaddr + BANK_SELECT );
905 bank = inw( ioaddr + BANK_SELECT );
906 if ( (bank & 0xFF00 ) != 0x3300 ) {
907 retval = -ENODEV;
908 goto err_out;
909 }
910#if !defined(CONFIG_H8S_EDOSK2674)
911 /* well, we've already written once, so hopefully another time won't
912 hurt. This time, I need to switch the bank register to bank 1,
913 so I can access the base address register */
914 SMC_SELECT_BANK(1);
915 base_address_register = inw( ioaddr + BASE );
916 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
Joe Perches24500222007-11-19 17:48:28 -0800917 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 "Probably not a SMC chip\n",
919 ioaddr, base_address_register >> 3 & 0x3E0 );
920 /* well, the base address register didn't match. Must not have
921 been a SMC chip after all. */
922 retval = -ENODEV;
923 goto err_out;
924 }
925#else
926 (void)base_address_register; /* Warning suppression */
927#endif
928
929
930 /* check if the revision register is something that I recognize.
931 These might need to be added to later, as future revisions
932 could be added. */
933 SMC_SELECT_BANK(3);
934 revision_register = inw( ioaddr + REVISION );
935 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
936 /* I don't recognize this chip, so... */
937 printk(CARDNAME ": IO %x: Unrecognized revision register:"
Frans Pop84d57bd2010-03-24 07:57:32 +0000938 " %x, Contact author.\n", ioaddr, revision_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 retval = -ENODEV;
941 goto err_out;
942 }
943
944 /* at this point I'll assume that the chip is an SMC9xxx.
945 It might be prudent to check a listing of MAC addresses
946 against the hardware address, or do some other tests. */
947
948 if (version_printed++ == 0)
949 printk("%s", version);
950
951 /* fill in some of the fields */
952 dev->base_addr = ioaddr;
953
954 /*
955 . Get the MAC address ( bank 1, regs 4 - 9 )
956 */
957 SMC_SELECT_BANK( 1 );
958 for ( i = 0; i < 6; i += 2 ) {
959 word address;
960
961 address = inw( ioaddr + ADDR0 + i );
962 dev->dev_addr[ i + 1] = address >> 8;
963 dev->dev_addr[ i ] = address & 0xFF;
964 }
965
966 /* get the memory information */
967
968 SMC_SELECT_BANK( 0 );
969 memory_info_register = inw( ioaddr + MIR );
970 memory_cfg_register = inw( ioaddr + MCR );
971 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
972 memory *= 256 * ( memory_info_register & 0xFF );
973
974 /*
975 Now, I want to find out more about the chip. This is sort of
976 redundant, but it's cleaner to have it in both, rather than having
977 one VERY long probe procedure.
978 */
979 SMC_SELECT_BANK(3);
980 revision_register = inw( ioaddr + REVISION );
981 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
982 if ( !version_string ) {
983 /* I shouldn't get here because this call was done before.... */
984 retval = -ENODEV;
985 goto err_out;
986 }
987
988 /* is it using AUI or 10BaseT ? */
989 if ( dev->if_port == 0 ) {
990 SMC_SELECT_BANK(1);
991 configuration_register = inw( ioaddr + CONFIG );
992 if ( configuration_register & CFG_AUI_SELECT )
993 dev->if_port = 2;
994 else
995 dev->if_port = 1;
996 }
997 if_string = interfaces[ dev->if_port - 1 ];
998
999 /* now, reset the chip, and put it into a known state */
1000 smc_reset( ioaddr );
1001
1002 /*
1003 . If dev->irq is 0, then the device has to be banged on to see
1004 . what the IRQ is.
1005 .
1006 . This banging doesn't always detect the IRQ, for unknown reasons.
1007 . a workaround is to reset the chip and try again.
1008 .
1009 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1010 . be what is requested on the command line. I don't do that, mostly
1011 . because the card that I have uses a non-standard method of accessing
1012 . the IRQs, and because this _should_ work in most configurations.
1013 .
1014 . Specifying an IRQ is done with the assumption that the user knows
1015 . what (s)he is doing. No checking is done!!!!
1016 .
1017 */
1018 if ( dev->irq < 2 ) {
1019 int trials;
1020
1021 trials = 3;
1022 while ( trials-- ) {
1023 dev->irq = smc_findirq( ioaddr );
1024 if ( dev->irq )
1025 break;
1026 /* kick the card and try again */
1027 smc_reset( ioaddr );
1028 }
1029 }
1030 if (dev->irq == 0 ) {
1031 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
1032 retval = -ENODEV;
1033 goto err_out;
1034 }
1035
1036 /* now, print out the card info, in a short format.. */
1037
1038 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
1039 version_string, revision_register & 0xF, ioaddr, dev->irq,
1040 if_string, memory );
1041 /*
1042 . Print the Ethernet address
1043 */
Johannes Berge1749612008-10-27 15:59:26 -07001044 printk("ADDR: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /* set the private data to zero by default */
Wang Chen8f15ea42008-11-12 23:38:36 -08001047 memset(netdev_priv(dev), 0, sizeof(struct smc_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /* Grab the IRQ */
Joe Perchesa0607fd2009-11-18 23:29:17 -08001050 retval = request_irq(dev->irq, smc_interrupt, 0, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (retval) {
1052 printk("%s: unable to get IRQ %d (irqval=%d).\n", DRV_NAME,
1053 dev->irq, retval);
1054 goto err_out;
1055 }
1056
Stephen Hemminger32670c32009-03-26 15:11:29 +00001057 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 dev->watchdog_timeo = HZ/20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 return 0;
1061
1062err_out:
1063 release_region(ioaddr, SMC_IO_EXTENT);
1064 return retval;
1065}
1066
1067#if SMC_DEBUG > 2
1068static void print_packet( byte * buf, int length )
1069{
1070#if 0
1071 int i;
1072 int remainder;
1073 int lines;
1074
Frans Pop84d57bd2010-03-24 07:57:32 +00001075 printk("Packet of length %d\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 lines = length / 16;
1077 remainder = length % 16;
1078
1079 for ( i = 0; i < lines ; i ++ ) {
1080 int cur;
1081
1082 for ( cur = 0; cur < 8; cur ++ ) {
1083 byte a, b;
1084
1085 a = *(buf ++ );
1086 b = *(buf ++ );
1087 printk("%02x%02x ", a, b );
1088 }
1089 printk("\n");
1090 }
1091 for ( i = 0; i < remainder/2 ; i++ ) {
1092 byte a, b;
1093
1094 a = *(buf ++ );
1095 b = *(buf ++ );
1096 printk("%02x%02x ", a, b );
1097 }
1098 printk("\n");
1099#endif
1100}
1101#endif
1102
1103
1104/*
1105 * Open and Initialize the board
1106 *
1107 * Set up everything, reset the card, etc ..
1108 *
1109 */
1110static int smc_open(struct net_device *dev)
1111{
1112 int ioaddr = dev->base_addr;
1113
1114 int i; /* used to set hw ethernet address */
1115
1116 /* clear out all the junk that was put here before... */
Wang Chen8f15ea42008-11-12 23:38:36 -08001117 memset(netdev_priv(dev), 0, sizeof(struct smc_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /* reset the hardware */
1120
1121 smc_reset( ioaddr );
1122 smc_enable( ioaddr );
1123
1124 /* Select which interface to use */
1125
1126 SMC_SELECT_BANK( 1 );
1127 if ( dev->if_port == 1 ) {
1128 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1129 ioaddr + CONFIG );
1130 }
1131 else if ( dev->if_port == 2 ) {
1132 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1133 ioaddr + CONFIG );
1134 }
1135
1136 /*
1137 According to Becker, I have to set the hardware address
1138 at this point, because the (l)user can set it with an
1139 ioctl. Easily done...
1140 */
1141 SMC_SELECT_BANK( 1 );
1142 for ( i = 0; i < 6; i += 2 ) {
1143 word address;
1144
1145 address = dev->dev_addr[ i + 1 ] << 8 ;
1146 address |= dev->dev_addr[ i ];
1147 outw( address, ioaddr + ADDR0 + i );
1148 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 netif_start_queue(dev);
1151 return 0;
1152}
1153
1154/*--------------------------------------------------------
1155 . Called by the kernel to send a packet out into the void
1156 . of the net. This routine is largely based on
1157 . skeleton.c, from Becker.
1158 .--------------------------------------------------------
1159*/
1160
1161static void smc_timeout(struct net_device *dev)
1162{
1163 /* If we get here, some higher level has decided we are broken.
1164 There should really be a "kick me" function call instead. */
1165 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1166 tx_done(dev) ? "IRQ conflict" :
1167 "network cable problem");
1168 /* "kick" the adaptor */
1169 smc_reset( dev->base_addr );
1170 smc_enable( dev->base_addr );
1171 dev->trans_start = jiffies;
1172 /* clear anything saved */
Wang Chen8f15ea42008-11-12 23:38:36 -08001173 ((struct smc_local *)netdev_priv(dev))->saved_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 netif_wake_queue(dev);
1175}
1176
1177/*-------------------------------------------------------------
1178 .
1179 . smc_rcv - receive a packet from the card
1180 .
1181 . There is ( at least ) a packet waiting to be read from
1182 . chip-memory.
1183 .
1184 . o Read the status
1185 . o If an error, record it
1186 . o otherwise, read in the packet
1187 --------------------------------------------------------------
1188*/
1189static void smc_rcv(struct net_device *dev)
1190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int ioaddr = dev->base_addr;
1192 int packet_number;
1193 word status;
1194 word packet_length;
1195
1196 /* assume bank 2 */
1197
1198 packet_number = inw( ioaddr + FIFO_PORTS );
1199
1200 if ( packet_number & FP_RXEMPTY ) {
1201 /* we got called , but nothing was on the FIFO */
Frans Pop84d57bd2010-03-24 07:57:32 +00001202 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* don't need to restore anything */
1204 return;
1205 }
1206
1207 /* start reading from the start of the packet */
1208 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1209
1210 /* First two words are status and packet_length */
1211 status = inw( ioaddr + DATA_1 );
1212 packet_length = inw( ioaddr + DATA_1 );
1213
1214 packet_length &= 0x07ff; /* mask off top bits */
1215
1216 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1217 /*
1218 . the packet length contains 3 extra words :
1219 . status, length, and an extra word with an odd byte .
1220 */
1221 packet_length -= 6;
1222
1223 if ( !(status & RS_ERRORS ) ){
1224 /* do stuff to make a new packet */
1225 struct sk_buff * skb;
1226 byte * data;
1227
1228 /* read one extra byte */
1229 if ( status & RS_ODDFRAME )
1230 packet_length++;
1231
1232 /* set multicast stats */
1233 if ( status & RS_MULTICAST )
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001234 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 skb = dev_alloc_skb( packet_length + 5);
1237
1238 if ( skb == NULL ) {
1239 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001240 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto done;
1242 }
1243
1244 /*
1245 ! This should work without alignment, but it could be
1246 ! in the worse case
1247 */
1248
1249 skb_reserve( skb, 2 ); /* 16 bit alignment */
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 data = skb_put( skb, packet_length);
1252
1253#ifdef USE_32_BIT
1254 /* QUESTION: Like in the TX routine, do I want
1255 to send the DWORDs or the bytes first, or some
1256 mixture. A mixture might improve already slow PIO
1257 performance */
Frans Pop84d57bd2010-03-24 07:57:32 +00001258 PRINTK3((" Reading %d dwords (and %d bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 packet_length >> 2, packet_length & 3 ));
1260 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1261 /* read the left over bytes */
1262 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1263 packet_length & 0x3 );
1264#else
Frans Pop84d57bd2010-03-24 07:57:32 +00001265 PRINTK3((" Reading %d words and %d byte(s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 (packet_length >> 1 ), packet_length & 1 ));
1267 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1268 if ( packet_length & 1 ) {
1269 data += packet_length & ~1;
1270 *(data++) = inb( ioaddr + DATA_1 );
1271 }
1272#endif
1273#if SMC_DEBUG > 2
1274 print_packet( data, packet_length );
1275#endif
1276
1277 skb->protocol = eth_type_trans(skb, dev );
1278 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001279 dev->stats.rx_packets++;
1280 dev->stats.rx_bytes += packet_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 } else {
1282 /* error ... */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001283 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001285 if ( status & RS_ALGNERR ) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001287 dev->stats.rx_length_errors++;
1288 if ( status & RS_BADCRC) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291done:
1292 /* error or good, tell the card to get rid of this packet */
1293 outw( MC_RELEASE, ioaddr + MMU_CMD );
1294}
1295
1296
1297/*************************************************************************
1298 . smc_tx
1299 .
1300 . Purpose: Handle a transmit error message. This will only be called
1301 . when an error, because of the AUTO_RELEASE mode.
1302 .
1303 . Algorithm:
1304 . Save pointer and packet no
1305 . Get the packet no from the top of the queue
1306 . check if it's valid ( if not, is this an error??? )
1307 . read the status word
1308 . record the error
1309 . ( resend? Not really, since we don't want old packets around )
1310 . Restore saved values
1311 ************************************************************************/
1312static void smc_tx( struct net_device * dev )
1313{
1314 int ioaddr = dev->base_addr;
1315 struct smc_local *lp = netdev_priv(dev);
1316 byte saved_packet;
1317 byte packet_no;
1318 word tx_status;
1319
1320
1321 /* assume bank 2 */
1322
1323 saved_packet = inb( ioaddr + PNR_ARR );
1324 packet_no = inw( ioaddr + FIFO_PORTS );
1325 packet_no &= 0x7F;
1326
1327 /* select this as the packet to read from */
1328 outb( packet_no, ioaddr + PNR_ARR );
1329
1330 /* read the first word from this packet */
1331 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1332
1333 tx_status = inw( ioaddr + DATA_1 );
Frans Pop84d57bd2010-03-24 07:57:32 +00001334 PRINTK3((CARDNAME": TX DONE STATUS: %4x\n", tx_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001336 dev->stats.tx_errors++;
1337 if ( tx_status & TS_LOSTCAR ) dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if ( tx_status & TS_LATCOL ) {
1339 printk(KERN_DEBUG CARDNAME
1340 ": Late collision occurred on last xmit.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001341 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343#if 0
1344 if ( tx_status & TS_16COL ) { ... }
1345#endif
1346
1347 if ( tx_status & TS_SUCCESS ) {
Frans Pop84d57bd2010-03-24 07:57:32 +00001348 printk(CARDNAME": Successful packet caused interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 /* re-enable transmit */
1351 SMC_SELECT_BANK( 0 );
1352 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1353
1354 /* kill the packet */
1355 SMC_SELECT_BANK( 2 );
1356 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1357
1358 /* one less packet waiting for me */
1359 lp->packets_waiting--;
1360
1361 outb( saved_packet, ioaddr + PNR_ARR );
1362 return;
1363}
1364
1365/*--------------------------------------------------------------------
1366 .
1367 . This is the main routine of the driver, to handle the device when
1368 . it needs some attention.
1369 .
1370 . So:
1371 . first, save state of the chipset
1372 . branch off into routines to handle each case, and acknowledge
1373 . each to the interrupt register
1374 . and finally restore state.
1375 .
1376 ---------------------------------------------------------------------*/
1377
David Howells7d12e782006-10-05 14:55:46 +01001378static irqreturn_t smc_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct net_device *dev = dev_id;
1381 int ioaddr = dev->base_addr;
1382 struct smc_local *lp = netdev_priv(dev);
1383
1384 byte status;
1385 word card_stats;
1386 byte mask;
1387 int timeout;
1388 /* state registers */
1389 word saved_bank;
1390 word saved_pointer;
1391 int handled = 0;
1392
1393
Frans Pop84d57bd2010-03-24 07:57:32 +00001394 PRINTK3((CARDNAME": SMC interrupt started\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 saved_bank = inw( ioaddr + BANK_SELECT );
1397
1398 SMC_SELECT_BANK(2);
1399 saved_pointer = inw( ioaddr + POINTER );
1400
1401 mask = inb( ioaddr + INT_MASK );
1402 /* clear all interrupts */
1403 outb( 0, ioaddr + INT_MASK );
1404
1405
1406 /* set a timeout value, so I don't stay here forever */
1407 timeout = 4;
1408
Frans Pop84d57bd2010-03-24 07:57:32 +00001409 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x\n", mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 do {
1411 /* read the status flag, and mask it */
1412 status = inb( ioaddr + INTERRUPT ) & mask;
1413 if (!status )
1414 break;
1415
1416 handled = 1;
1417
1418 PRINTK3((KERN_WARNING CARDNAME
Frans Pop84d57bd2010-03-24 07:57:32 +00001419 ": Handling interrupt status %x\n", status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 if (status & IM_RCV_INT) {
1422 /* Got a packet(s). */
1423 PRINTK2((KERN_WARNING CARDNAME
1424 ": Receive Interrupt\n"));
1425 smc_rcv(dev);
1426 } else if (status & IM_TX_INT ) {
1427 PRINTK2((KERN_WARNING CARDNAME
1428 ": TX ERROR handled\n"));
1429 smc_tx(dev);
1430 outb(IM_TX_INT, ioaddr + INTERRUPT );
1431 } else if (status & IM_TX_EMPTY_INT ) {
1432 /* update stats */
1433 SMC_SELECT_BANK( 0 );
1434 card_stats = inw( ioaddr + COUNTER );
1435 /* single collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001436 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 card_stats >>= 4;
1438 /* multiple collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001439 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /* these are for when linux supports these statistics */
1442
1443 SMC_SELECT_BANK( 2 );
1444 PRINTK2((KERN_WARNING CARDNAME
1445 ": TX_BUFFER_EMPTY handled\n"));
1446 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1447 mask &= ~IM_TX_EMPTY_INT;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001448 dev->stats.tx_packets += lp->packets_waiting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 lp->packets_waiting = 0;
1450
1451 } else if (status & IM_ALLOC_INT ) {
1452 PRINTK2((KERN_DEBUG CARDNAME
Frans Pop84d57bd2010-03-24 07:57:32 +00001453 ": Allocation interrupt\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* clear this interrupt so it doesn't happen again */
1455 mask &= ~IM_ALLOC_INT;
1456
1457 smc_hardware_send_packet( dev );
1458
1459 /* enable xmit interrupts based on this */
1460 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1461
1462 /* and let the card send more packets to me */
1463 netif_wake_queue(dev);
1464
1465 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1466 } else if (status & IM_RX_OVRN_INT ) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001467 dev->stats.rx_errors++;
1468 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1470 } else if (status & IM_EPH_INT ) {
Frans Pop84d57bd2010-03-24 07:57:32 +00001471 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 } else if (status & IM_ERCV_INT ) {
Frans Pop84d57bd2010-03-24 07:57:32 +00001473 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1475 }
1476 } while ( timeout -- );
1477
1478
1479 /* restore state register */
1480 SMC_SELECT_BANK( 2 );
1481 outb( mask, ioaddr + INT_MASK );
1482
Frans Pop84d57bd2010-03-24 07:57:32 +00001483 PRINTK3((KERN_WARNING CARDNAME ": MASK is now %x\n", mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 outw( saved_pointer, ioaddr + POINTER );
1485
1486 SMC_SELECT_BANK( saved_bank );
1487
1488 PRINTK3((CARDNAME ": Interrupt done\n"));
1489 return IRQ_RETVAL(handled);
1490}
1491
1492
1493/*----------------------------------------------------
1494 . smc_close
1495 .
1496 . this makes the board clean up everything that it can
1497 . and not talk to the outside world. Caused by
1498 . an 'ifconfig ethX down'
1499 .
1500 -----------------------------------------------------*/
1501static int smc_close(struct net_device *dev)
1502{
1503 netif_stop_queue(dev);
1504 /* clear everything */
1505 smc_shutdown( dev->base_addr );
1506
1507 /* Update the statistics here. */
1508 return 0;
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/*-----------------------------------------------------------
1512 . smc_set_multicast_list
1513 .
1514 . This routine will, depending on the values passed to it,
1515 . either make it accept multicast packets, go into
1516 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1517 . a select set of multicast packets
1518*/
1519static void smc_set_multicast_list(struct net_device *dev)
1520{
1521 short ioaddr = dev->base_addr;
1522
1523 SMC_SELECT_BANK(0);
1524 if ( dev->flags & IFF_PROMISC )
1525 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1526
1527/* BUG? I never disable promiscuous mode if multicasting was turned on.
1528 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1529 when promiscuous mode is turned on.
1530*/
1531
1532 /* Here, I am setting this to accept all multicast packets.
1533 I don't need to zero the multicast table, because the flag is
1534 checked before the table is
1535 */
1536 else if (dev->flags & IFF_ALLMULTI)
1537 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1538
1539 /* We just get all multicast packets even if we only want them
1540 . from one source. This will be changed at some future
1541 . point. */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001542 else if (!netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /* support hardware multicasting */
1544
1545 /* be sure I get rid of flags I might have set */
1546 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1547 ioaddr + RCR );
1548 /* NOTE: this has to set the bank, so make sure it is the
1549 last thing called. The bank is set to zero at the top */
Jiri Pirko567ec872010-02-23 23:17:07 +00001550 smc_setmulticast(ioaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 else {
1553 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1554 ioaddr + RCR );
1555
1556 /*
1557 since I'm disabling all multicast entirely, I need to
1558 clear the multicast list
1559 */
1560 SMC_SELECT_BANK( 3 );
1561 outw( 0, ioaddr + MULTICAST1 );
1562 outw( 0, ioaddr + MULTICAST2 );
1563 outw( 0, ioaddr + MULTICAST3 );
1564 outw( 0, ioaddr + MULTICAST4 );
1565 }
1566}
1567
1568#ifdef MODULE
1569
1570static struct net_device *devSMC9194;
1571MODULE_LICENSE("GPL");
1572
1573module_param(io, int, 0);
1574module_param(irq, int, 0);
1575module_param(ifport, int, 0);
1576MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1577MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1578MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1579
Randy Dunlapa2bd2ec2006-06-10 13:30:10 -07001580int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 if (io == 0)
1583 printk(KERN_WARNING
1584 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1585
1586 /* copy the parameters from insmod into the device structure */
1587 devSMC9194 = smc_init(-1);
1588 if (IS_ERR(devSMC9194))
1589 return PTR_ERR(devSMC9194);
1590 return 0;
1591}
1592
Al Viroafc8eb42006-06-14 18:50:53 -04001593void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 unregister_netdev(devSMC9194);
1596 free_irq(devSMC9194->irq, devSMC9194);
1597 release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1598 free_netdev(devSMC9194);
1599}
1600
1601#endif /* MODULE */