smc9194.c 44 KB

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