smc9194.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:43k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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