blob: 7486d0908064c598a0de1252d4ecfd85aba12789 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/string.h>
68#include <linux/init.h>
69#include <linux/crc32.h>
70#include <linux/errno.h>
71#include <linux/netdevice.h>
72#include <linux/etherdevice.h>
73#include <linux/skbuff.h>
74#include <linux/bitops.h>
75
76#include <asm/io.h>
77
78#include "smc9194.h"
79
80#define DRV_NAME "smc9194"
81
82/*------------------------------------------------------------------------
83 .
84 . Configuration options, for the experienced user to change.
85 .
86 -------------------------------------------------------------------------*/
87
88/*
89 . Do you want to use 32 bit xfers? This should work on all chips, as
90 . the chipset is designed to accommodate them.
91*/
92#ifdef __sh__
93#undef USE_32_BIT
94#else
95#define USE_32_BIT 1
96#endif
97
98#if defined(__H8300H__) || defined(__H8300S__)
99#define NO_AUTOPROBE
100#undef insl
101#undef outsl
102#define insl(a,b,l) io_insl_noswap(a,b,l)
103#define outsl(a,b,l) io_outsl_noswap(a,b,l)
104#endif
105
106/*
107 .the SMC9194 can be at any of the following port addresses. To change,
108 .for a slightly different card, you can add it to the array. Keep in
109 .mind that the array must end in zero.
110*/
111
112struct devlist {
113 unsigned int port;
114 unsigned int irq;
115};
116
117#if defined(CONFIG_H8S_EDOSK2674)
118static struct devlist smc_devlist[] __initdata = {
119 {.port = 0xf80000, .irq = 16},
120 {.port = 0, .irq = 0 },
121};
122#else
123static struct devlist smc_devlist[] __initdata = {
124 {.port = 0x200, .irq = 0},
125 {.port = 0x220, .irq = 0},
126 {.port = 0x240, .irq = 0},
127 {.port = 0x260, .irq = 0},
128 {.port = 0x280, .irq = 0},
129 {.port = 0x2A0, .irq = 0},
130 {.port = 0x2C0, .irq = 0},
131 {.port = 0x2E0, .irq = 0},
132 {.port = 0x300, .irq = 0},
133 {.port = 0x320, .irq = 0},
134 {.port = 0x340, .irq = 0},
135 {.port = 0x360, .irq = 0},
136 {.port = 0x380, .irq = 0},
137 {.port = 0x3A0, .irq = 0},
138 {.port = 0x3C0, .irq = 0},
139 {.port = 0x3E0, .irq = 0},
140 {.port = 0, .irq = 0},
141};
142#endif
143/*
144 . Wait time for memory to be free. This probably shouldn't be
145 . tuned that much, as waiting for this means nothing else happens
146 . in the system
147*/
148#define MEMORY_WAIT_TIME 16
149
150/*
151 . DEBUGGING LEVELS
152 .
153 . 0 for normal operation
154 . 1 for slightly more details
155 . >2 for various levels of increasingly useless information
156 . 2 for interrupt tracking, status flags
157 . 3 for packet dumps, etc.
158*/
159#define SMC_DEBUG 0
160
161#if (SMC_DEBUG > 2 )
162#define PRINTK3(x) printk x
163#else
164#define PRINTK3(x)
165#endif
166
167#if SMC_DEBUG > 1
168#define PRINTK2(x) printk x
169#else
170#define PRINTK2(x)
171#endif
172
173#ifdef SMC_DEBUG
174#define PRINTK(x) printk x
175#else
176#define PRINTK(x)
177#endif
178
179
180/*------------------------------------------------------------------------
181 .
182 . The internal workings of the driver. If you are changing anything
183 . here with the SMC stuff, you should have the datasheet and known
184 . what you are doing.
185 .
186 -------------------------------------------------------------------------*/
187#define CARDNAME "SMC9194"
188
189
190/* store this information for the driver.. */
191struct smc_local {
192 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 If I have to wait until memory is available to send
194 a packet, I will store the skbuff here, until I get the
195 desired memory. Then, I'll send it out and free it.
196 */
197 struct sk_buff * saved_skb;
198
199 /*
200 . This keeps track of how many packets that I have
201 . sent out. When an TX_EMPTY interrupt comes, I know
202 . that all of these have been sent.
203 */
204 int packets_waiting;
205};
206
207
208/*-----------------------------------------------------------------
209 .
210 . The driver can be entered at any of the following entry points.
211 .
212 .------------------------------------------------------------------ */
213
214/*
215 . This is called by register_netdev(). It is responsible for
216 . checking the portlist for the SMC9000 series chipset. If it finds
217 . one, then it will initialize the device, find the hardware information,
218 . and sets up the appropriate device parameters.
219 . NOTE: Interrupts are *OFF* when this procedure is called.
220 .
221 . NB:This shouldn't be static since it is referred to externally.
222*/
223struct net_device *smc_init(int unit);
224
225/*
226 . The kernel calls this function when someone wants to use the device,
227 . typically 'ifconfig ethX up'.
228*/
229static int smc_open(struct net_device *dev);
230
231/*
232 . Our watchdog timed out. Called by the networking layer
233*/
234static void smc_timeout(struct net_device *dev);
235
236/*
237 . This is called by the kernel in response to 'ifconfig ethX down'. It
238 . is responsible for cleaning up everything that the open routine
239 . does, and maybe putting the card into a powerdown state.
240*/
241static int smc_close(struct net_device *dev);
242
243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
245 . programs ) and multicast modes.
246*/
247static void smc_set_multicast_list(struct net_device *dev);
248
249
250/*---------------------------------------------------------------
251 .
252 . Interrupt level calls..
253 .
254 ----------------------------------------------------------------*/
255
256/*
257 . Handles the actual interrupt
258*/
David Howells7d12e782006-10-05 14:55:46 +0100259static irqreturn_t smc_interrupt(int irq, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 . This is a separate procedure to handle the receipt of a packet, to
262 . leave the interrupt code looking slightly cleaner
263*/
264static inline void smc_rcv( struct net_device *dev );
265/*
266 . This handles a TX interrupt, which is only called when an error
267 . relating to a packet is sent.
268*/
269static inline void smc_tx( struct net_device * dev );
270
271/*
272 ------------------------------------------------------------
273 .
274 . Internal routines
275 .
276 ------------------------------------------------------------
277*/
278
279/*
280 . Test if a given location contains a chip, trying to cause as
281 . little damage as possible if it's not a SMC chip.
282*/
283static int smc_probe(struct net_device *dev, int ioaddr);
284
285/*
286 . A rather simple routine to print out a packet for debugging purposes.
287*/
288#if SMC_DEBUG > 2
289static void print_packet( byte *, int );
290#endif
291
292#define tx_done(dev) 1
293
294/* this is called to actually send the packet to the chip */
295static void smc_hardware_send_packet( struct net_device * dev );
296
297/* Since I am not sure if I will have enough room in the chip's ram
298 . to store the packet, I call this routine, which either sends it
299 . now, or generates an interrupt when the card is ready for the
300 . packet */
Stephen Hemminger613573252009-08-31 19:50:58 +0000301static netdev_tx_t smc_wait_to_send_packet( struct sk_buff * skb,
302 struct net_device *dev );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
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/*
Jiri Pirko22bedad32010-04-01 21:22:57 +0000419 . Function: smc_setmulticast( int ioaddr, struct net_device *dev )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 . 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
Jiri Pirko567ec872010-02-23 23:17:07 +0000436static void smc_setmulticast(int ioaddr, struct net_device *dev)
437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int i;
439 unsigned char multicast_table[ 8 ];
Jiri Pirko22bedad32010-04-01 21:22:57 +0000440 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* table for flipping the order of 3 bits */
442 unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
443
444 /* start with a table of all zeros: reject all */
445 memset( multicast_table, 0, sizeof( multicast_table ) );
446
Jiri Pirko22bedad32010-04-01 21:22:57 +0000447 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int position;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* make sure this is a multicast address - shouldn't this
451 be a given if we have it here ? */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000452 if (!(*ha->addr & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 continue;
454
455 /* only use the low order bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000456 position = ether_crc_le(6, ha->addr) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* do some messy swapping to put the bit in the right spot */
459 multicast_table[invert3[position&7]] |=
460 (1<<invert3[(position>>3)&7]);
461
462 }
463 /* now, the table can be loaded into the chipset */
464 SMC_SELECT_BANK( 3 );
465
466 for ( i = 0; i < 8 ; i++ ) {
467 outb( multicast_table[i], ioaddr + MULTICAST1 + i );
468 }
469}
470
471/*
472 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
473 . Purpose:
474 . Attempt to allocate memory for a packet, if chip-memory is not
475 . available, then tell the card to generate an interrupt when it
476 . is available.
477 .
478 . Algorithm:
479 .
480 . o if the saved_skb is not currently null, then drop this packet
481 . on the floor. This should never happen, because of TBUSY.
482 . o if the saved_skb is null, then replace it with the current packet,
483 . o See if I can sending it now.
484 . o (NO): Enable interrupts and let the interrupt handler deal with it.
485 . o (YES):Send it now.
486*/
Stephen Hemminger613573252009-08-31 19:50:58 +0000487static netdev_tx_t smc_wait_to_send_packet(struct sk_buff *skb,
488 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct smc_local *lp = netdev_priv(dev);
491 unsigned int ioaddr = dev->base_addr;
492 word length;
493 unsigned short numPages;
494 word time_out;
495
496 netif_stop_queue(dev);
497 /* Well, I want to send the packet.. but I don't know
498 if I can send it right now... */
499
500 if ( lp->saved_skb) {
501 /* THIS SHOULD NEVER HAPPEN. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700502 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
Patrick McHardy5b548142009-06-12 06:22:29 +0000504 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 lp->saved_skb = skb;
507
508 length = skb->len;
509
510 if (length < ETH_ZLEN) {
Herbert Xu5b057c62006-06-23 02:06:41 -0700511 if (skb_padto(skb, ETH_ZLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000513 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 length = ETH_ZLEN;
516 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 ** The MMU wants the number of pages to be the number of 256 bytes
520 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
521 **
522 ** Pkt size for allocating is data length +6 (for additional status words,
523 ** length and ctl!) If odd size last byte is included in this header.
524 */
525 numPages = ((length & 0xfffe) + 6) / 256;
526
527 if (numPages > 7 ) {
Frans Pop84d57bd2010-03-24 07:57:32 +0000528 printk(CARDNAME": Far too big packet error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* freeing the packet is a good thing here... but should
530 . any packets of this size get down here? */
531 dev_kfree_skb (skb);
532 lp->saved_skb = NULL;
533 /* this IS an error, but, i don't want the skb saved */
534 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000535 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 /* either way, a packet is waiting now */
538 lp->packets_waiting++;
539
540 /* now, try to allocate the memory */
541 SMC_SELECT_BANK( 2 );
542 outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
543 /*
544 . Performance Hack
545 .
546 . wait a short amount of time.. if I can send a packet now, I send
547 . it now. Otherwise, I enable an interrupt and wait for one to be
548 . available.
549 .
550 . I could have handled this a slightly different way, by checking to
551 . see if any memory was available in the FREE MEMORY register. However,
552 . either way, I need to generate an allocation, and the allocation works
553 . no matter what, so I saw no point in checking free memory.
554 */
555 time_out = MEMORY_WAIT_TIME;
556 do {
557 word status;
558
559 status = inb( ioaddr + INTERRUPT );
560 if ( status & IM_ALLOC_INT ) {
561 /* acknowledge the interrupt */
562 outb( IM_ALLOC_INT, ioaddr + INTERRUPT );
563 break;
564 }
565 } while ( -- time_out );
566
567 if ( !time_out ) {
568 /* oh well, wait until the chip finds memory later */
569 SMC_ENABLE_INT( IM_ALLOC_INT );
Frans Pop84d57bd2010-03-24 07:57:32 +0000570 PRINTK2((CARDNAME": memory allocation deferred.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* it's deferred, but I'll handle it later */
Frans Pop84d57bd2010-03-24 07:57:32 +0000572 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 /* or YES! I can send the packet now.. */
575 smc_hardware_send_packet(dev);
576 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000577 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
581 . Function: smc_hardware_send_packet(struct net_device * )
582 . Purpose:
583 . This sends the actual packet to the SMC9xxx chip.
584 .
585 . Algorithm:
586 . First, see if a saved_skb is available.
587 . ( this should NOT be called if there is no 'saved_skb'
588 . Now, find the packet number that the chip allocated
589 . Point the data pointers at it in memory
590 . Set the length word in the chip's memory
591 . Dump the packet to chip memory
592 . Check if a last byte is needed ( odd length packet )
593 . if so, set the control flag right
594 . Tell the card to send it
595 . Enable the transmit interrupt, so I know if it failed
596 . Free the kernel data if I actually sent it.
597*/
598static void smc_hardware_send_packet( struct net_device * dev )
599{
600 struct smc_local *lp = netdev_priv(dev);
601 byte packet_no;
602 struct sk_buff * skb = lp->saved_skb;
603 word length;
604 unsigned int ioaddr;
605 byte * buf;
606
607 ioaddr = dev->base_addr;
608
609 if ( !skb ) {
Frans Pop84d57bd2010-03-24 07:57:32 +0000610 PRINTK((CARDNAME": In XMIT with no packet to send\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return;
612 }
613 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
614 buf = skb->data;
615
616 /* If I get here, I _know_ there is a packet slot waiting for me */
617 packet_no = inb( ioaddr + PNR_ARR + 1 );
618 if ( packet_no & 0x80 ) {
619 /* or isn't there? BAD CHIP! */
Frans Pop84d57bd2010-03-24 07:57:32 +0000620 printk(KERN_DEBUG CARDNAME": Memory allocation failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 dev_kfree_skb_any(skb);
622 lp->saved_skb = NULL;
623 netif_wake_queue(dev);
624 return;
625 }
626
627 /* we have a packet address, so tell the card to use it */
628 outb( packet_no, ioaddr + PNR_ARR );
629
630 /* point to the beginning of the packet */
631 outw( PTR_AUTOINC , ioaddr + POINTER );
632
633 PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
634#if SMC_DEBUG > 2
635 print_packet( buf, length );
636#endif
637
638 /* send the packet length ( +6 for status, length and ctl byte )
639 and the status word ( set to zeros ) */
640#ifdef USE_32_BIT
641 outl( (length +6 ) << 16 , ioaddr + DATA_1 );
642#else
643 outw( 0, ioaddr + DATA_1 );
644 /* send the packet length ( +6 for status words, length, and ctl*/
645 outb( (length+6) & 0xFF,ioaddr + DATA_1 );
646 outb( (length+6) >> 8 , ioaddr + DATA_1 );
647#endif
648
649 /* send the actual data
650 . I _think_ it's faster to send the longs first, and then
651 . mop up by sending the last word. It depends heavily
652 . on alignment, at least on the 486. Maybe it would be
653 . a good idea to check which is optimal? But that could take
654 . almost as much time as is saved?
655 */
656#ifdef USE_32_BIT
657 if ( length & 0x2 ) {
658 outsl(ioaddr + DATA_1, buf, length >> 2 );
659#if !defined(__H8300H__) && !defined(__H8300S__)
660 outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
661#else
662 ctrl_outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
663#endif
664 }
665 else
666 outsl(ioaddr + DATA_1, buf, length >> 2 );
667#else
668 outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
669#endif
670 /* Send the last byte, if there is one. */
671
672 if ( (length & 1) == 0 ) {
673 outw( 0, ioaddr + DATA_1 );
674 } else {
675 outb( buf[length -1 ], ioaddr + DATA_1 );
676 outb( 0x20, ioaddr + DATA_1);
677 }
678
679 /* enable the interrupts */
680 SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );
681
682 /* and let the chipset deal with it */
683 outw( MC_ENQUEUE , ioaddr + MMU_CMD );
684
Frans Pop84d57bd2010-03-24 07:57:32 +0000685 PRINTK2((CARDNAME": Sent packet of length %d\n", length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 lp->saved_skb = NULL;
688 dev_kfree_skb_any (skb);
689
690 dev->trans_start = jiffies;
691
692 /* we can send another packet */
693 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*-------------------------------------------------------------------------
697 |
698 | smc_init(int unit)
699 | Input parameters:
700 | dev->base_addr == 0, try to find all possible locations
701 | dev->base_addr == 1, return failure code
702 | dev->base_addr == 2, always allocate space, and return success
703 | dev->base_addr == <anything else> this is the address to check
704 |
705 | Output:
706 | pointer to net_device or ERR_PTR(error)
707 |
708 ---------------------------------------------------------------------------
709*/
710static int io;
711static int irq;
712static int ifport;
713
714struct net_device * __init smc_init(int unit)
715{
716 struct net_device *dev = alloc_etherdev(sizeof(struct smc_local));
Randy Dunlapa2bd2ec2006-06-10 13:30:10 -0700717 struct devlist *smcdev = smc_devlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int err = 0;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!dev)
721 return ERR_PTR(-ENODEV);
722
723 if (unit >= 0) {
724 sprintf(dev->name, "eth%d", unit);
725 netdev_boot_setup_check(dev);
726 io = dev->base_addr;
727 irq = dev->irq;
728 }
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (io > 0x1ff) { /* Check a single specified location. */
731 err = smc_probe(dev, io);
732 } else if (io != 0) { /* Don't probe at all. */
733 err = -ENXIO;
734 } else {
735 for (;smcdev->port; smcdev++) {
736 if (smc_probe(dev, smcdev->port) == 0)
737 break;
738 }
739 if (!smcdev->port)
740 err = -ENODEV;
741 }
742 if (err)
743 goto out;
744 err = register_netdev(dev);
745 if (err)
746 goto out1;
747 return dev;
748out1:
749 free_irq(dev->irq, dev);
750 release_region(dev->base_addr, SMC_IO_EXTENT);
751out:
752 free_netdev(dev);
753 return ERR_PTR(err);
754}
755
756/*----------------------------------------------------------------------
757 . smc_findirq
758 .
759 . This routine has a simple purpose -- make the SMC chip generate an
760 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
761 ------------------------------------------------------------------------
762*/
Hannes Ederdac499f2008-12-25 23:56:45 -0800763static int __init smc_findirq(int ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765#ifndef NO_AUTOPROBE
766 int timeout = 20;
767 unsigned long cookie;
768
769
770 cookie = probe_irq_on();
771
772 /*
773 * What I try to do here is trigger an ALLOC_INT. This is done
774 * by allocating a small chunk of memory, which will give an interrupt
775 * when done.
776 */
777
778
779 SMC_SELECT_BANK(2);
780 /* enable ALLOCation interrupts ONLY */
781 outb( IM_ALLOC_INT, ioaddr + INT_MASK );
782
783 /*
784 . Allocate 512 bytes of memory. Note that the chip was just
785 . reset so all the memory is available
786 */
787 outw( MC_ALLOC | 1, ioaddr + MMU_CMD );
788
789 /*
790 . Wait until positive that the interrupt has been generated
791 */
792 while ( timeout ) {
793 byte int_status;
794
795 int_status = inb( ioaddr + INTERRUPT );
796
797 if ( int_status & IM_ALLOC_INT )
798 break; /* got the interrupt */
799 timeout--;
800 }
801 /* there is really nothing that I can do here if timeout fails,
802 as probe_irq_off will return a 0 anyway, which is what I
803 want in this case. Plus, the clean up is needed in both
804 cases. */
805
806 /* DELAY HERE!
807 On a fast machine, the status might change before the interrupt
808 is given to the processor. This means that the interrupt was
809 never detected, and probe_irq_off fails to report anything.
810 This should fix probe_irq_* problems.
811 */
812 SMC_DELAY();
813 SMC_DELAY();
814
815 /* and disable all interrupts again */
816 outb( 0, ioaddr + INT_MASK );
817
818 /* and return what I found */
819 return probe_irq_off(cookie);
820#else /* NO_AUTOPROBE */
821 struct devlist *smcdev;
822 for (smcdev = smc_devlist; smcdev->port; smcdev++) {
823 if (smcdev->port == ioaddr)
824 return smcdev->irq;
825 }
826 return 0;
827#endif
828}
829
Stephen Hemminger32670c32009-03-26 15:11:29 +0000830static const struct net_device_ops smc_netdev_ops = {
831 .ndo_open = smc_open,
832 .ndo_stop = smc_close,
833 .ndo_start_xmit = smc_wait_to_send_packet,
834 .ndo_tx_timeout = smc_timeout,
835 .ndo_set_multicast_list = smc_set_multicast_list,
836 .ndo_change_mtu = eth_change_mtu,
837 .ndo_set_mac_address = eth_mac_addr,
838 .ndo_validate_addr = eth_validate_addr,
839};
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*----------------------------------------------------------------------
842 . Function: smc_probe( int ioaddr )
843 .
844 . Purpose:
845 . Tests to see if a given ioaddr points to an SMC9xxx chip.
846 . Returns a 0 on success
847 .
848 . Algorithm:
849 . (1) see if the high byte of BANK_SELECT is 0x33
850 . (2) compare the ioaddr with the base register's address
851 . (3) see if I recognize the chip ID in the appropriate register
852 .
853 .---------------------------------------------------------------------
854 */
855
856/*---------------------------------------------------------------
857 . Here I do typical initialization tasks.
858 .
859 . o Initialize the structure if needed
860 . o print out my vanity message if not done so already
861 . o print out what type of hardware is detected
862 . o print out the ethernet address
863 . o find the IRQ
864 . o set up my private data
865 . o configure the dev structure with my subroutines
866 . o actually GRAB the irq.
867 . o GRAB the region
868 .-----------------------------------------------------------------
869*/
870static int __init smc_probe(struct net_device *dev, int ioaddr)
871{
872 int i, memory, retval;
873 static unsigned version_printed;
874 unsigned int bank;
875
876 const char *version_string;
877 const char *if_string;
878
879 /* registers */
880 word revision_register;
881 word base_address_register;
882 word configuration_register;
883 word memory_info_register;
884 word memory_cfg_register;
885
886 /* Grab the region so that no one else tries to probe our ioports. */
887 if (!request_region(ioaddr, SMC_IO_EXTENT, DRV_NAME))
888 return -EBUSY;
889
890 dev->irq = irq;
891 dev->if_port = ifport;
892
893 /* First, see if the high byte is 0x33 */
894 bank = inw( ioaddr + BANK_SELECT );
895 if ( (bank & 0xFF00) != 0x3300 ) {
896 retval = -ENODEV;
897 goto err_out;
898 }
899 /* The above MIGHT indicate a device, but I need to write to further
900 test this. */
901 outw( 0x0, ioaddr + BANK_SELECT );
902 bank = inw( ioaddr + BANK_SELECT );
903 if ( (bank & 0xFF00 ) != 0x3300 ) {
904 retval = -ENODEV;
905 goto err_out;
906 }
907#if !defined(CONFIG_H8S_EDOSK2674)
908 /* well, we've already written once, so hopefully another time won't
909 hurt. This time, I need to switch the bank register to bank 1,
910 so I can access the base address register */
911 SMC_SELECT_BANK(1);
912 base_address_register = inw( ioaddr + BASE );
913 if ( ioaddr != ( base_address_register >> 3 & 0x3E0 ) ) {
Joe Perches24500222007-11-19 17:48:28 -0800914 printk(CARDNAME ": IOADDR %x doesn't match configuration (%x). "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 "Probably not a SMC chip\n",
916 ioaddr, base_address_register >> 3 & 0x3E0 );
917 /* well, the base address register didn't match. Must not have
918 been a SMC chip after all. */
919 retval = -ENODEV;
920 goto err_out;
921 }
922#else
923 (void)base_address_register; /* Warning suppression */
924#endif
925
926
927 /* check if the revision register is something that I recognize.
928 These might need to be added to later, as future revisions
929 could be added. */
930 SMC_SELECT_BANK(3);
931 revision_register = inw( ioaddr + REVISION );
932 if ( !chip_ids[ ( revision_register >> 4 ) & 0xF ] ) {
933 /* I don't recognize this chip, so... */
934 printk(CARDNAME ": IO %x: Unrecognized revision register:"
Frans Pop84d57bd2010-03-24 07:57:32 +0000935 " %x, Contact author.\n", ioaddr, revision_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 retval = -ENODEV;
938 goto err_out;
939 }
940
941 /* at this point I'll assume that the chip is an SMC9xxx.
942 It might be prudent to check a listing of MAC addresses
943 against the hardware address, or do some other tests. */
944
945 if (version_printed++ == 0)
946 printk("%s", version);
947
948 /* fill in some of the fields */
949 dev->base_addr = ioaddr;
950
951 /*
952 . Get the MAC address ( bank 1, regs 4 - 9 )
953 */
954 SMC_SELECT_BANK( 1 );
955 for ( i = 0; i < 6; i += 2 ) {
956 word address;
957
958 address = inw( ioaddr + ADDR0 + i );
959 dev->dev_addr[ i + 1] = address >> 8;
960 dev->dev_addr[ i ] = address & 0xFF;
961 }
962
963 /* get the memory information */
964
965 SMC_SELECT_BANK( 0 );
966 memory_info_register = inw( ioaddr + MIR );
967 memory_cfg_register = inw( ioaddr + MCR );
968 memory = ( memory_cfg_register >> 9 ) & 0x7; /* multiplier */
969 memory *= 256 * ( memory_info_register & 0xFF );
970
971 /*
972 Now, I want to find out more about the chip. This is sort of
973 redundant, but it's cleaner to have it in both, rather than having
974 one VERY long probe procedure.
975 */
976 SMC_SELECT_BANK(3);
977 revision_register = inw( ioaddr + REVISION );
978 version_string = chip_ids[ ( revision_register >> 4 ) & 0xF ];
979 if ( !version_string ) {
980 /* I shouldn't get here because this call was done before.... */
981 retval = -ENODEV;
982 goto err_out;
983 }
984
985 /* is it using AUI or 10BaseT ? */
986 if ( dev->if_port == 0 ) {
987 SMC_SELECT_BANK(1);
988 configuration_register = inw( ioaddr + CONFIG );
989 if ( configuration_register & CFG_AUI_SELECT )
990 dev->if_port = 2;
991 else
992 dev->if_port = 1;
993 }
994 if_string = interfaces[ dev->if_port - 1 ];
995
996 /* now, reset the chip, and put it into a known state */
997 smc_reset( ioaddr );
998
999 /*
1000 . If dev->irq is 0, then the device has to be banged on to see
1001 . what the IRQ is.
1002 .
1003 . This banging doesn't always detect the IRQ, for unknown reasons.
1004 . a workaround is to reset the chip and try again.
1005 .
1006 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1007 . be what is requested on the command line. I don't do that, mostly
1008 . because the card that I have uses a non-standard method of accessing
1009 . the IRQs, and because this _should_ work in most configurations.
1010 .
1011 . Specifying an IRQ is done with the assumption that the user knows
1012 . what (s)he is doing. No checking is done!!!!
1013 .
1014 */
1015 if ( dev->irq < 2 ) {
1016 int trials;
1017
1018 trials = 3;
1019 while ( trials-- ) {
1020 dev->irq = smc_findirq( ioaddr );
1021 if ( dev->irq )
1022 break;
1023 /* kick the card and try again */
1024 smc_reset( ioaddr );
1025 }
1026 }
1027 if (dev->irq == 0 ) {
1028 printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
1029 retval = -ENODEV;
1030 goto err_out;
1031 }
1032
1033 /* now, print out the card info, in a short format.. */
1034
1035 printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
1036 version_string, revision_register & 0xF, ioaddr, dev->irq,
1037 if_string, memory );
1038 /*
1039 . Print the Ethernet address
1040 */
Johannes Berge1749612008-10-27 15:59:26 -07001041 printk("ADDR: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* Grab the IRQ */
Joe Perchesa0607fd2009-11-18 23:29:17 -08001044 retval = request_irq(dev->irq, smc_interrupt, 0, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (retval) {
1046 printk("%s: unable to get IRQ %d (irqval=%d).\n", DRV_NAME,
1047 dev->irq, retval);
1048 goto err_out;
1049 }
1050
Stephen Hemminger32670c32009-03-26 15:11:29 +00001051 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 dev->watchdog_timeo = HZ/20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 return 0;
1055
1056err_out:
1057 release_region(ioaddr, SMC_IO_EXTENT);
1058 return retval;
1059}
1060
1061#if SMC_DEBUG > 2
1062static void print_packet( byte * buf, int length )
1063{
1064#if 0
1065 int i;
1066 int remainder;
1067 int lines;
1068
Frans Pop84d57bd2010-03-24 07:57:32 +00001069 printk("Packet of length %d\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 lines = length / 16;
1071 remainder = length % 16;
1072
1073 for ( i = 0; i < lines ; i ++ ) {
1074 int cur;
1075
1076 for ( cur = 0; cur < 8; cur ++ ) {
1077 byte a, b;
1078
1079 a = *(buf ++ );
1080 b = *(buf ++ );
1081 printk("%02x%02x ", a, b );
1082 }
1083 printk("\n");
1084 }
1085 for ( i = 0; i < remainder/2 ; i++ ) {
1086 byte a, b;
1087
1088 a = *(buf ++ );
1089 b = *(buf ++ );
1090 printk("%02x%02x ", a, b );
1091 }
1092 printk("\n");
1093#endif
1094}
1095#endif
1096
1097
1098/*
1099 * Open and Initialize the board
1100 *
1101 * Set up everything, reset the card, etc ..
1102 *
1103 */
1104static int smc_open(struct net_device *dev)
1105{
1106 int ioaddr = dev->base_addr;
1107
1108 int i; /* used to set hw ethernet address */
1109
1110 /* clear out all the junk that was put here before... */
Wang Chen8f15ea42008-11-12 23:38:36 -08001111 memset(netdev_priv(dev), 0, sizeof(struct smc_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /* reset the hardware */
1114
1115 smc_reset( ioaddr );
1116 smc_enable( ioaddr );
1117
1118 /* Select which interface to use */
1119
1120 SMC_SELECT_BANK( 1 );
1121 if ( dev->if_port == 1 ) {
1122 outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
1123 ioaddr + CONFIG );
1124 }
1125 else if ( dev->if_port == 2 ) {
1126 outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
1127 ioaddr + CONFIG );
1128 }
1129
1130 /*
1131 According to Becker, I have to set the hardware address
1132 at this point, because the (l)user can set it with an
1133 ioctl. Easily done...
1134 */
1135 SMC_SELECT_BANK( 1 );
1136 for ( i = 0; i < 6; i += 2 ) {
1137 word address;
1138
1139 address = dev->dev_addr[ i + 1 ] << 8 ;
1140 address |= dev->dev_addr[ i ];
1141 outw( address, ioaddr + ADDR0 + i );
1142 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 netif_start_queue(dev);
1145 return 0;
1146}
1147
1148/*--------------------------------------------------------
1149 . Called by the kernel to send a packet out into the void
1150 . of the net. This routine is largely based on
1151 . skeleton.c, from Becker.
1152 .--------------------------------------------------------
1153*/
1154
1155static void smc_timeout(struct net_device *dev)
1156{
1157 /* If we get here, some higher level has decided we are broken.
1158 There should really be a "kick me" function call instead. */
1159 printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
1160 tx_done(dev) ? "IRQ conflict" :
1161 "network cable problem");
1162 /* "kick" the adaptor */
1163 smc_reset( dev->base_addr );
1164 smc_enable( dev->base_addr );
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001165 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* clear anything saved */
Wang Chen8f15ea42008-11-12 23:38:36 -08001167 ((struct smc_local *)netdev_priv(dev))->saved_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 netif_wake_queue(dev);
1169}
1170
1171/*-------------------------------------------------------------
1172 .
1173 . smc_rcv - receive a packet from the card
1174 .
1175 . There is ( at least ) a packet waiting to be read from
1176 . chip-memory.
1177 .
1178 . o Read the status
1179 . o If an error, record it
1180 . o otherwise, read in the packet
1181 --------------------------------------------------------------
1182*/
1183static void smc_rcv(struct net_device *dev)
1184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int ioaddr = dev->base_addr;
1186 int packet_number;
1187 word status;
1188 word packet_length;
1189
1190 /* assume bank 2 */
1191
1192 packet_number = inw( ioaddr + FIFO_PORTS );
1193
1194 if ( packet_number & FP_RXEMPTY ) {
1195 /* we got called , but nothing was on the FIFO */
Frans Pop84d57bd2010-03-24 07:57:32 +00001196 PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* don't need to restore anything */
1198 return;
1199 }
1200
1201 /* start reading from the start of the packet */
1202 outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );
1203
1204 /* First two words are status and packet_length */
1205 status = inw( ioaddr + DATA_1 );
1206 packet_length = inw( ioaddr + DATA_1 );
1207
1208 packet_length &= 0x07ff; /* mask off top bits */
1209
1210 PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
1211 /*
1212 . the packet length contains 3 extra words :
1213 . status, length, and an extra word with an odd byte .
1214 */
1215 packet_length -= 6;
1216
1217 if ( !(status & RS_ERRORS ) ){
1218 /* do stuff to make a new packet */
1219 struct sk_buff * skb;
1220 byte * data;
1221
1222 /* read one extra byte */
1223 if ( status & RS_ODDFRAME )
1224 packet_length++;
1225
1226 /* set multicast stats */
1227 if ( status & RS_MULTICAST )
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001228 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 skb = dev_alloc_skb( packet_length + 5);
1231
1232 if ( skb == NULL ) {
1233 printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001234 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto done;
1236 }
1237
1238 /*
1239 ! This should work without alignment, but it could be
1240 ! in the worse case
1241 */
1242
1243 skb_reserve( skb, 2 ); /* 16 bit alignment */
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 data = skb_put( skb, packet_length);
1246
1247#ifdef USE_32_BIT
1248 /* QUESTION: Like in the TX routine, do I want
1249 to send the DWORDs or the bytes first, or some
1250 mixture. A mixture might improve already slow PIO
1251 performance */
Frans Pop84d57bd2010-03-24 07:57:32 +00001252 PRINTK3((" Reading %d dwords (and %d bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 packet_length >> 2, packet_length & 3 ));
1254 insl(ioaddr + DATA_1 , data, packet_length >> 2 );
1255 /* read the left over bytes */
1256 insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
1257 packet_length & 0x3 );
1258#else
Frans Pop84d57bd2010-03-24 07:57:32 +00001259 PRINTK3((" Reading %d words and %d byte(s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 (packet_length >> 1 ), packet_length & 1 ));
1261 insw(ioaddr + DATA_1 , data, packet_length >> 1);
1262 if ( packet_length & 1 ) {
1263 data += packet_length & ~1;
1264 *(data++) = inb( ioaddr + DATA_1 );
1265 }
1266#endif
1267#if SMC_DEBUG > 2
1268 print_packet( data, packet_length );
1269#endif
1270
1271 skb->protocol = eth_type_trans(skb, dev );
1272 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001273 dev->stats.rx_packets++;
1274 dev->stats.rx_bytes += packet_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 } else {
1276 /* error ... */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001277 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001279 if ( status & RS_ALGNERR ) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001281 dev->stats.rx_length_errors++;
1282 if ( status & RS_BADCRC) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
1285done:
1286 /* error or good, tell the card to get rid of this packet */
1287 outw( MC_RELEASE, ioaddr + MMU_CMD );
1288}
1289
1290
1291/*************************************************************************
1292 . smc_tx
1293 .
1294 . Purpose: Handle a transmit error message. This will only be called
1295 . when an error, because of the AUTO_RELEASE mode.
1296 .
1297 . Algorithm:
1298 . Save pointer and packet no
1299 . Get the packet no from the top of the queue
1300 . check if it's valid ( if not, is this an error??? )
1301 . read the status word
1302 . record the error
1303 . ( resend? Not really, since we don't want old packets around )
1304 . Restore saved values
1305 ************************************************************************/
1306static void smc_tx( struct net_device * dev )
1307{
1308 int ioaddr = dev->base_addr;
1309 struct smc_local *lp = netdev_priv(dev);
1310 byte saved_packet;
1311 byte packet_no;
1312 word tx_status;
1313
1314
1315 /* assume bank 2 */
1316
1317 saved_packet = inb( ioaddr + PNR_ARR );
1318 packet_no = inw( ioaddr + FIFO_PORTS );
1319 packet_no &= 0x7F;
1320
1321 /* select this as the packet to read from */
1322 outb( packet_no, ioaddr + PNR_ARR );
1323
1324 /* read the first word from this packet */
1325 outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );
1326
1327 tx_status = inw( ioaddr + DATA_1 );
Frans Pop84d57bd2010-03-24 07:57:32 +00001328 PRINTK3((CARDNAME": TX DONE STATUS: %4x\n", tx_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001330 dev->stats.tx_errors++;
1331 if ( tx_status & TS_LOSTCAR ) dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if ( tx_status & TS_LATCOL ) {
1333 printk(KERN_DEBUG CARDNAME
1334 ": Late collision occurred on last xmit.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001335 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337#if 0
1338 if ( tx_status & TS_16COL ) { ... }
1339#endif
1340
1341 if ( tx_status & TS_SUCCESS ) {
Frans Pop84d57bd2010-03-24 07:57:32 +00001342 printk(CARDNAME": Successful packet caused interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 /* re-enable transmit */
1345 SMC_SELECT_BANK( 0 );
1346 outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );
1347
1348 /* kill the packet */
1349 SMC_SELECT_BANK( 2 );
1350 outw( MC_FREEPKT, ioaddr + MMU_CMD );
1351
1352 /* one less packet waiting for me */
1353 lp->packets_waiting--;
1354
1355 outb( saved_packet, ioaddr + PNR_ARR );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358/*--------------------------------------------------------------------
1359 .
1360 . This is the main routine of the driver, to handle the device when
1361 . it needs some attention.
1362 .
1363 . So:
1364 . first, save state of the chipset
1365 . branch off into routines to handle each case, and acknowledge
1366 . each to the interrupt register
1367 . and finally restore state.
1368 .
1369 ---------------------------------------------------------------------*/
1370
David Howells7d12e782006-10-05 14:55:46 +01001371static irqreturn_t smc_interrupt(int irq, void * dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct net_device *dev = dev_id;
1374 int ioaddr = dev->base_addr;
1375 struct smc_local *lp = netdev_priv(dev);
1376
1377 byte status;
1378 word card_stats;
1379 byte mask;
1380 int timeout;
1381 /* state registers */
1382 word saved_bank;
1383 word saved_pointer;
1384 int handled = 0;
1385
1386
Frans Pop84d57bd2010-03-24 07:57:32 +00001387 PRINTK3((CARDNAME": SMC interrupt started\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 saved_bank = inw( ioaddr + BANK_SELECT );
1390
1391 SMC_SELECT_BANK(2);
1392 saved_pointer = inw( ioaddr + POINTER );
1393
1394 mask = inb( ioaddr + INT_MASK );
1395 /* clear all interrupts */
1396 outb( 0, ioaddr + INT_MASK );
1397
1398
1399 /* set a timeout value, so I don't stay here forever */
1400 timeout = 4;
1401
Frans Pop84d57bd2010-03-24 07:57:32 +00001402 PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x\n", mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 do {
1404 /* read the status flag, and mask it */
1405 status = inb( ioaddr + INTERRUPT ) & mask;
1406 if (!status )
1407 break;
1408
1409 handled = 1;
1410
1411 PRINTK3((KERN_WARNING CARDNAME
Frans Pop84d57bd2010-03-24 07:57:32 +00001412 ": Handling interrupt status %x\n", status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (status & IM_RCV_INT) {
1415 /* Got a packet(s). */
1416 PRINTK2((KERN_WARNING CARDNAME
1417 ": Receive Interrupt\n"));
1418 smc_rcv(dev);
1419 } else if (status & IM_TX_INT ) {
1420 PRINTK2((KERN_WARNING CARDNAME
1421 ": TX ERROR handled\n"));
1422 smc_tx(dev);
1423 outb(IM_TX_INT, ioaddr + INTERRUPT );
1424 } else if (status & IM_TX_EMPTY_INT ) {
1425 /* update stats */
1426 SMC_SELECT_BANK( 0 );
1427 card_stats = inw( ioaddr + COUNTER );
1428 /* single collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001429 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 card_stats >>= 4;
1431 /* multiple collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001432 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* these are for when linux supports these statistics */
1435
1436 SMC_SELECT_BANK( 2 );
1437 PRINTK2((KERN_WARNING CARDNAME
1438 ": TX_BUFFER_EMPTY handled\n"));
1439 outb( IM_TX_EMPTY_INT, ioaddr + INTERRUPT );
1440 mask &= ~IM_TX_EMPTY_INT;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001441 dev->stats.tx_packets += lp->packets_waiting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 lp->packets_waiting = 0;
1443
1444 } else if (status & IM_ALLOC_INT ) {
1445 PRINTK2((KERN_DEBUG CARDNAME
Frans Pop84d57bd2010-03-24 07:57:32 +00001446 ": Allocation interrupt\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* clear this interrupt so it doesn't happen again */
1448 mask &= ~IM_ALLOC_INT;
1449
1450 smc_hardware_send_packet( dev );
1451
1452 /* enable xmit interrupts based on this */
1453 mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );
1454
1455 /* and let the card send more packets to me */
1456 netif_wake_queue(dev);
1457
1458 PRINTK2((CARDNAME": Handoff done successfully.\n"));
1459 } else if (status & IM_RX_OVRN_INT ) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001460 dev->stats.rx_errors++;
1461 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 outb( IM_RX_OVRN_INT, ioaddr + INTERRUPT );
1463 } else if (status & IM_EPH_INT ) {
Frans Pop84d57bd2010-03-24 07:57:32 +00001464 PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 } else if (status & IM_ERCV_INT ) {
Frans Pop84d57bd2010-03-24 07:57:32 +00001466 PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 outb( IM_ERCV_INT, ioaddr + INTERRUPT );
1468 }
1469 } while ( timeout -- );
1470
1471
1472 /* restore state register */
1473 SMC_SELECT_BANK( 2 );
1474 outb( mask, ioaddr + INT_MASK );
1475
Frans Pop84d57bd2010-03-24 07:57:32 +00001476 PRINTK3((KERN_WARNING CARDNAME ": MASK is now %x\n", mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 outw( saved_pointer, ioaddr + POINTER );
1478
1479 SMC_SELECT_BANK( saved_bank );
1480
1481 PRINTK3((CARDNAME ": Interrupt done\n"));
1482 return IRQ_RETVAL(handled);
1483}
1484
1485
1486/*----------------------------------------------------
1487 . smc_close
1488 .
1489 . this makes the board clean up everything that it can
1490 . and not talk to the outside world. Caused by
1491 . an 'ifconfig ethX down'
1492 .
1493 -----------------------------------------------------*/
1494static int smc_close(struct net_device *dev)
1495{
1496 netif_stop_queue(dev);
1497 /* clear everything */
1498 smc_shutdown( dev->base_addr );
1499
1500 /* Update the statistics here. */
1501 return 0;
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/*-----------------------------------------------------------
1505 . smc_set_multicast_list
1506 .
1507 . This routine will, depending on the values passed to it,
1508 . either make it accept multicast packets, go into
1509 . promiscuous mode ( for TCPDUMP and cousins ) or accept
1510 . a select set of multicast packets
1511*/
1512static void smc_set_multicast_list(struct net_device *dev)
1513{
1514 short ioaddr = dev->base_addr;
1515
1516 SMC_SELECT_BANK(0);
1517 if ( dev->flags & IFF_PROMISC )
1518 outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );
1519
1520/* BUG? I never disable promiscuous mode if multicasting was turned on.
1521 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1522 when promiscuous mode is turned on.
1523*/
1524
1525 /* Here, I am setting this to accept all multicast packets.
1526 I don't need to zero the multicast table, because the flag is
1527 checked before the table is
1528 */
1529 else if (dev->flags & IFF_ALLMULTI)
1530 outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );
1531
1532 /* We just get all multicast packets even if we only want them
1533 . from one source. This will be changed at some future
1534 . point. */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001535 else if (!netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* support hardware multicasting */
1537
1538 /* be sure I get rid of flags I might have set */
1539 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1540 ioaddr + RCR );
1541 /* NOTE: this has to set the bank, so make sure it is the
1542 last thing called. The bank is set to zero at the top */
Jiri Pirko567ec872010-02-23 23:17:07 +00001543 smc_setmulticast(ioaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 else {
1546 outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
1547 ioaddr + RCR );
1548
1549 /*
1550 since I'm disabling all multicast entirely, I need to
1551 clear the multicast list
1552 */
1553 SMC_SELECT_BANK( 3 );
1554 outw( 0, ioaddr + MULTICAST1 );
1555 outw( 0, ioaddr + MULTICAST2 );
1556 outw( 0, ioaddr + MULTICAST3 );
1557 outw( 0, ioaddr + MULTICAST4 );
1558 }
1559}
1560
1561#ifdef MODULE
1562
1563static struct net_device *devSMC9194;
1564MODULE_LICENSE("GPL");
1565
1566module_param(io, int, 0);
1567module_param(irq, int, 0);
1568module_param(ifport, int, 0);
1569MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
1570MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
1571MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");
1572
Randy Dunlapa2bd2ec2006-06-10 13:30:10 -07001573int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 if (io == 0)
1576 printk(KERN_WARNING
1577 CARDNAME": You shouldn't use auto-probing with insmod!\n" );
1578
1579 /* copy the parameters from insmod into the device structure */
1580 devSMC9194 = smc_init(-1);
1581 if (IS_ERR(devSMC9194))
1582 return PTR_ERR(devSMC9194);
1583 return 0;
1584}
1585
Al Viroafc8eb42006-06-14 18:50:53 -04001586void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 unregister_netdev(devSMC9194);
1589 free_irq(devSMC9194->irq, devSMC9194);
1590 release_region(devSMC9194->base_addr, SMC_IO_EXTENT);
1591 free_netdev(devSMC9194);
1592}
1593
1594#endif /* MODULE */