sk_mca.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. net-3-driver for the SKNET MCA-based cards
  3. This is an extension to the Linux operating system, and is covered by the
  4. same GNU General Public License that covers that work.
  5. Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de,
  6. alfred.arnold@lancom.de)
  7. This driver is based both on the 3C523 driver and the SK_G16 driver.
  8. paper sources:
  9. 'PC Hardware: Aufbau, Funktionsweise, Programmierung' by
  10. Hans-Peter Messmer for the basic Microchannel stuff
  11. 'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
  12. for help on Ethernet driver programming
  13. 'Ethernet/IEEE 802.3 Family 1992 World Network Data Book/Handbook' by AMD
  14. for documentation on the AM7990 LANCE
  15. 'SKNET Personal Technisches Manual', Version 1.2 by Schneider&Koch
  16. for documentation on the Junior board
  17. 'SK-NET MC2+ Technical Manual", Version 1.1 by Schneider&Koch for
  18. documentation on the MC2 bord
  19. A big thank you to the S&K support for providing me so quickly with
  20. documentation!
  21. Also see http://www.syskonnect.com/
  22. Missing things:
  23. -> set debug level via ioctl instead of compile-time switches
  24. -> I didn't follow the development of the 2.1.x kernels, so my
  25. assumptions about which things changed with which kernel version
  26. are probably nonsense
  27. History:
  28. May 16th, 1999
  29. startup
  30. May 22st, 1999
  31. added private structure, methods
  32. begun building data structures in RAM
  33. May 23nd, 1999
  34. can receive frames, send frames
  35. May 24th, 1999
  36. modularized initialization of LANCE
  37. loadable as module
  38. still Tx problem :-(
  39. May 26th, 1999
  40. MC2 works
  41. support for multiple devices
  42. display media type for MC2+
  43. May 28th, 1999
  44. fixed problem in GetLANCE leaving interrupts turned off
  45. increase TX queue to 4 packets to improve send performance
  46. May 29th, 1999
  47. a few corrections in statistics, caught rcvr overruns
  48. reinitialization of LANCE/board in critical situations
  49. MCA info implemented
  50. implemented LANCE multicast filter
  51. Jun 6th, 1999
  52. additions for Linux 2.2
  53. Dec 25th, 1999
  54. unfortunately there seem to be newer MC2+ boards that react
  55. on IRQ 3/5/9/10 instead of 3/5/10/11, so we have to autoprobe
  56. in questionable cases...
  57. Dec 28th, 1999
  58. integrated patches from David Weinehall & Bill Wendling for 2.3
  59. kernels (isa_...functions). Things are defined in a way that
  60. it still works with 2.0.x 8-)
  61. Dec 30th, 1999
  62. added handling of the remaining interrupt conditions. That
  63. should cure the spurious hangs.
  64. Jan 30th, 2000
  65. newer kernels automatically probe more than one board, so the
  66. 'startslot' as a variable is also needed here
  67. June 1st, 2000
  68. added changes for recent 2.3 kernels
  69. *************************************************************************/
  70. #include <linux/kernel.h>
  71. #include <linux/string.h>
  72. #include <linux/errno.h>
  73. #include <linux/ioport.h>
  74. #include <linux/slab.h>
  75. #include <linux/interrupt.h>
  76. #include <linux/delay.h>
  77. #include <linux/time.h>
  78. #include <linux/mca-legacy.h>
  79. #include <linux/init.h>
  80. #include <linux/module.h>
  81. #include <linux/netdevice.h>
  82. #include <linux/etherdevice.h>
  83. #include <linux/skbuff.h>
  84. #include <linux/bitops.h>
  85. #include <asm/processor.h>
  86. #include <asm/io.h>
  87. #define _SK_MCA_DRIVER_
  88. #include "sk_mca.h"
  89. /* ------------------------------------------------------------------------
  90. * global static data - not more since we can handle multiple boards and
  91. * have to pack all state info into the device struct!
  92. * ------------------------------------------------------------------------ */
  93. static char *MediaNames[Media_Count] =
  94. { "10Base2", "10BaseT", "10Base5", "Unknown" };
  95. static unsigned char poly[] =
  96. { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0,
  97. 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
  98. };
  99. /* ------------------------------------------------------------------------
  100. * private subfunctions
  101. * ------------------------------------------------------------------------ */
  102. /* dump parts of shared memory - only needed during debugging */
  103. #ifdef DEBUG
  104. static void dumpmem(struct net_device *dev, u32 start, u32 len)
  105. {
  106. skmca_priv *priv = netdev_priv(dev);
  107. int z;
  108. for (z = 0; z < len; z++) {
  109. if ((z & 15) == 0)
  110. printk("%04x:", z);
  111. printk(" %02x", readb(priv->base + start + z));
  112. if ((z & 15) == 15)
  113. printk("\n");
  114. }
  115. }
  116. /* print exact time - ditto */
  117. static void PrTime(void)
  118. {
  119. struct timeval tv;
  120. do_gettimeofday(&tv);
  121. printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);
  122. }
  123. #endif
  124. /* deduce resources out of POS registers */
  125. static void __init getaddrs(int slot, int junior, int *base, int *irq,
  126. skmca_medium * medium)
  127. {
  128. u_char pos0, pos1, pos2;
  129. if (junior) {
  130. pos0 = mca_read_stored_pos(slot, 2);
  131. *base = ((pos0 & 0x0e) << 13) + 0xc0000;
  132. *irq = ((pos0 & 0x10) >> 4) + 10;
  133. *medium = Media_Unknown;
  134. } else {
  135. /* reset POS 104 Bits 0+1 so the shared memory region goes to the
  136. configured area between 640K and 1M. Afterwards, enable the MC2.
  137. I really don't know what rode SK to do this... */
  138. mca_write_pos(slot, 4,
  139. mca_read_stored_pos(slot, 4) & 0xfc);
  140. mca_write_pos(slot, 2,
  141. mca_read_stored_pos(slot, 2) | 0x01);
  142. pos1 = mca_read_stored_pos(slot, 3);
  143. pos2 = mca_read_stored_pos(slot, 4);
  144. *base = ((pos1 & 0x07) << 14) + 0xc0000;
  145. switch (pos2 & 0x0c) {
  146. case 0:
  147. *irq = 3;
  148. break;
  149. case 4:
  150. *irq = 5;
  151. break;
  152. case 8:
  153. *irq = -10;
  154. break;
  155. case 12:
  156. *irq = -11;
  157. break;
  158. }
  159. *medium = (pos2 >> 6) & 3;
  160. }
  161. }
  162. /* check for both cards:
  163. When the MC2 is turned off, it was configured for more than 15MB RAM,
  164. is disabled and won't get detected using the standard probe. We
  165. therefore have to scan the slots manually :-( */
  166. static int __init dofind(int *junior, int firstslot)
  167. {
  168. int slot;
  169. unsigned int id;
  170. for (slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++) {
  171. id = mca_read_stored_pos(slot, 0)
  172. + (((unsigned int) mca_read_stored_pos(slot, 1)) << 8);
  173. *junior = 0;
  174. if (id == SKNET_MCA_ID)
  175. return slot;
  176. *junior = 1;
  177. if (id == SKNET_JUNIOR_MCA_ID)
  178. return slot;
  179. }
  180. return MCA_NOTFOUND;
  181. }
  182. /* reset the whole board */
  183. static void ResetBoard(struct net_device *dev)
  184. {
  185. skmca_priv *priv = netdev_priv(dev);
  186. writeb(CTRL_RESET_ON, priv->ctrladdr);
  187. udelay(10);
  188. writeb(CTRL_RESET_OFF, priv->ctrladdr);
  189. }
  190. /* wait for LANCE interface to become not busy */
  191. static int WaitLANCE(struct net_device *dev)
  192. {
  193. skmca_priv *priv = netdev_priv(dev);
  194. int t = 0;
  195. while ((readb(priv->ctrladdr) & STAT_IO_BUSY) ==
  196. STAT_IO_BUSY) {
  197. udelay(1);
  198. if (++t > 1000) {
  199. printk("%s: LANCE access timeout", dev->name);
  200. return 0;
  201. }
  202. }
  203. return 1;
  204. }
  205. /* set LANCE register - must be atomic */
  206. static void SetLANCE(struct net_device *dev, u16 addr, u16 value)
  207. {
  208. skmca_priv *priv = netdev_priv(dev);
  209. unsigned long flags;
  210. /* disable interrupts */
  211. spin_lock_irqsave(&priv->lock, flags);
  212. /* wait until no transfer is pending */
  213. WaitLANCE(dev);
  214. /* transfer register address to RAP */
  215. writeb(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP, priv->ctrladdr);
  216. writew(addr, priv->ioregaddr);
  217. writeb(IOCMD_GO, priv->cmdaddr);
  218. udelay(1);
  219. WaitLANCE(dev);
  220. /* transfer data to register */
  221. writeb(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA, priv->ctrladdr);
  222. writew(value, priv->ioregaddr);
  223. writeb(IOCMD_GO, priv->cmdaddr);
  224. udelay(1);
  225. WaitLANCE(dev);
  226. /* reenable interrupts */
  227. spin_unlock_irqrestore(&priv->lock, flags);
  228. }
  229. /* get LANCE register */
  230. static u16 GetLANCE(struct net_device *dev, u16 addr)
  231. {
  232. skmca_priv *priv = netdev_priv(dev);
  233. unsigned long flags;
  234. unsigned int res;
  235. /* disable interrupts */
  236. spin_lock_irqsave(&priv->lock, flags);
  237. /* wait until no transfer is pending */
  238. WaitLANCE(dev);
  239. /* transfer register address to RAP */
  240. writeb(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP, priv->ctrladdr);
  241. writew(addr, priv->ioregaddr);
  242. writeb(IOCMD_GO, priv->cmdaddr);
  243. udelay(1);
  244. WaitLANCE(dev);
  245. /* transfer data from register */
  246. writeb(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA, priv->ctrladdr);
  247. writeb(IOCMD_GO, priv->cmdaddr);
  248. udelay(1);
  249. WaitLANCE(dev);
  250. res = readw(priv->ioregaddr);
  251. /* reenable interrupts */
  252. spin_unlock_irqrestore(&priv->lock, flags);
  253. return res;
  254. }
  255. /* build up descriptors in shared RAM */
  256. static void InitDscrs(struct net_device *dev)
  257. {
  258. skmca_priv *priv = netdev_priv(dev);
  259. u32 bufaddr;
  260. /* Set up Tx descriptors. The board has only 16K RAM so bits 16..23
  261. are always 0. */
  262. bufaddr = RAM_DATABASE;
  263. {
  264. LANCE_TxDescr descr;
  265. int z;
  266. for (z = 0; z < TXCOUNT; z++) {
  267. descr.LowAddr = bufaddr;
  268. descr.Flags = 0;
  269. descr.Len = 0xf000;
  270. descr.Status = 0;
  271. memcpy_toio(priv->base + RAM_TXBASE +
  272. (z * sizeof(LANCE_TxDescr)), &descr,
  273. sizeof(LANCE_TxDescr));
  274. memset_io(priv->base + bufaddr, 0, RAM_BUFSIZE);
  275. bufaddr += RAM_BUFSIZE;
  276. }
  277. }
  278. /* do the same for the Rx descriptors */
  279. {
  280. LANCE_RxDescr descr;
  281. int z;
  282. for (z = 0; z < RXCOUNT; z++) {
  283. descr.LowAddr = bufaddr;
  284. descr.Flags = RXDSCR_FLAGS_OWN;
  285. descr.MaxLen = -RAM_BUFSIZE;
  286. descr.Len = 0;
  287. memcpy_toio(priv->base + RAM_RXBASE +
  288. (z * sizeof(LANCE_RxDescr)), &descr,
  289. sizeof(LANCE_RxDescr));
  290. memset_io(priv->base + bufaddr, 0, RAM_BUFSIZE);
  291. bufaddr += RAM_BUFSIZE;
  292. }
  293. }
  294. }
  295. /* calculate the hash bit position for a given multicast address
  296. taken more or less directly from the AMD datasheet... */
  297. static void UpdateCRC(unsigned char *CRC, int bit)
  298. {
  299. int j;
  300. /* shift CRC one bit */
  301. memmove(CRC + 1, CRC, 32 * sizeof(unsigned char));
  302. CRC[0] = 0;
  303. /* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */
  304. if (bit ^ CRC[32])
  305. for (j = 0; j < 32; j++)
  306. CRC[j] ^= poly[j];
  307. }
  308. static unsigned int GetHash(char *address)
  309. {
  310. unsigned char CRC[33];
  311. int i, byte, hashcode;
  312. /* a multicast address has bit 0 in the first byte set */
  313. if ((address[0] & 1) == 0)
  314. return -1;
  315. /* initialize CRC */
  316. memset(CRC, 1, sizeof(CRC));
  317. /* loop through address bits */
  318. for (byte = 0; byte < 6; byte++)
  319. for (i = 0; i < 8; i++)
  320. UpdateCRC(CRC, (address[byte] >> i) & 1);
  321. /* hashcode is the 6 least significant bits of the CRC */
  322. hashcode = 0;
  323. for (i = 0; i < 6; i++)
  324. hashcode = (hashcode << 1) + CRC[i];
  325. return hashcode;
  326. }
  327. /* feed ready-built initialization block into LANCE */
  328. static void InitLANCE(struct net_device *dev)
  329. {
  330. skmca_priv *priv = netdev_priv(dev);
  331. /* build up descriptors. */
  332. InitDscrs(dev);
  333. /* next RX descriptor to be read is the first one. Since the LANCE
  334. will start from the beginning after initialization, we have to
  335. reset out pointers too. */
  336. priv->nextrx = 0;
  337. /* no TX descriptors active */
  338. priv->nexttxput = priv->nexttxdone = priv->txbusy = 0;
  339. /* set up the LANCE bus control register - constant for SKnet boards */
  340. SetLANCE(dev, LANCE_CSR3,
  341. CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD);
  342. /* write address of initialization block into LANCE */
  343. SetLANCE(dev, LANCE_CSR1, RAM_INITBASE & 0xffff);
  344. SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >> 16) & 0xff);
  345. /* we don't get ready until the LANCE has read the init block */
  346. netif_stop_queue(dev);
  347. /* let LANCE read the initialization block. LANCE is ready
  348. when we receive the corresponding interrupt. */
  349. SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);
  350. }
  351. /* stop the LANCE so we can reinitialize it */
  352. static void StopLANCE(struct net_device *dev)
  353. {
  354. /* can't take frames any more */
  355. netif_stop_queue(dev);
  356. /* disable interrupts, stop it */
  357. SetLANCE(dev, LANCE_CSR0, CSR0_STOP);
  358. }
  359. /* initialize card and LANCE for proper operation */
  360. static void InitBoard(struct net_device *dev)
  361. {
  362. skmca_priv *priv = netdev_priv(dev);
  363. LANCE_InitBlock block;
  364. /* Lay out the shared RAM - first we create the init block for the LANCE.
  365. We do not overwrite it later because we need it again when we switch
  366. promiscous mode on/off. */
  367. block.Mode = 0;
  368. if (dev->flags & IFF_PROMISC)
  369. block.Mode |= LANCE_INIT_PROM;
  370. memcpy(block.PAdr, dev->dev_addr, 6);
  371. memset(block.LAdrF, 0, sizeof(block.LAdrF));
  372. block.RdrP = (RAM_RXBASE & 0xffffff) | (LRXCOUNT << 29);
  373. block.TdrP = (RAM_TXBASE & 0xffffff) | (LTXCOUNT << 29);
  374. memcpy_toio(priv->base + RAM_INITBASE, &block, sizeof(block));
  375. /* initialize LANCE. Implicitly sets up other structures in RAM. */
  376. InitLANCE(dev);
  377. }
  378. /* deinitialize card and LANCE */
  379. static void DeinitBoard(struct net_device *dev)
  380. {
  381. /* stop LANCE */
  382. StopLANCE(dev);
  383. /* reset board */
  384. ResetBoard(dev);
  385. }
  386. /* probe for device's irq */
  387. static int __init ProbeIRQ(struct net_device *dev)
  388. {
  389. unsigned long imaskval, njiffies, irq;
  390. u16 csr0val;
  391. /* enable all interrupts */
  392. imaskval = probe_irq_on();
  393. /* initialize the board. Wait for interrupt 'Initialization done'. */
  394. ResetBoard(dev);
  395. InitBoard(dev);
  396. njiffies = jiffies + HZ;
  397. do {
  398. csr0val = GetLANCE(dev, LANCE_CSR0);
  399. }
  400. while (((csr0val & CSR0_IDON) == 0) && (jiffies != njiffies));
  401. /* turn of interrupts again */
  402. irq = probe_irq_off(imaskval);
  403. /* if we found something, ack the interrupt */
  404. if (irq)
  405. SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_IDON);
  406. /* back to idle state */
  407. DeinitBoard(dev);
  408. return irq;
  409. }
  410. /* ------------------------------------------------------------------------
  411. * interrupt handler(s)
  412. * ------------------------------------------------------------------------ */
  413. /* LANCE has read initialization block -> start it */
  414. static u16 irqstart_handler(struct net_device *dev, u16 oldcsr0)
  415. {
  416. /* now we're ready to transmit */
  417. netif_wake_queue(dev);
  418. /* reset IDON bit, start LANCE */
  419. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT);
  420. return GetLANCE(dev, LANCE_CSR0);
  421. }
  422. /* did we lose blocks due to a FIFO overrun ? */
  423. static u16 irqmiss_handler(struct net_device *dev, u16 oldcsr0)
  424. {
  425. skmca_priv *priv = netdev_priv(dev);
  426. /* update statistics */
  427. priv->stat.rx_fifo_errors++;
  428. /* reset MISS bit */
  429. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_MISS);
  430. return GetLANCE(dev, LANCE_CSR0);
  431. }
  432. /* receive interrupt */
  433. static u16 irqrx_handler(struct net_device *dev, u16 oldcsr0)
  434. {
  435. skmca_priv *priv = netdev_priv(dev);
  436. LANCE_RxDescr descr;
  437. unsigned int descraddr;
  438. /* run through queue until we reach a descriptor we do not own */
  439. descraddr = RAM_RXBASE + (priv->nextrx * sizeof(LANCE_RxDescr));
  440. while (1) {
  441. /* read descriptor */
  442. memcpy_fromio(&descr, priv->base + descraddr,
  443. sizeof(LANCE_RxDescr));
  444. /* if we reach a descriptor we do not own, we're done */
  445. if ((descr.Flags & RXDSCR_FLAGS_OWN) != 0)
  446. break;
  447. #ifdef DEBUG
  448. PrTime();
  449. printk("Receive packet on descr %d len %d\n", priv->nextrx,
  450. descr.Len);
  451. #endif
  452. /* erroneous packet ? */
  453. if ((descr.Flags & RXDSCR_FLAGS_ERR) != 0) {
  454. priv->stat.rx_errors++;
  455. if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
  456. priv->stat.rx_crc_errors++;
  457. else if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
  458. priv->stat.rx_frame_errors++;
  459. else if ((descr.Flags & RXDSCR_FLAGS_OFLO) != 0)
  460. priv->stat.rx_fifo_errors++;
  461. }
  462. /* good packet ? */
  463. else {
  464. struct sk_buff *skb;
  465. skb = dev_alloc_skb(descr.Len + 2);
  466. if (skb == NULL)
  467. priv->stat.rx_dropped++;
  468. else {
  469. memcpy_fromio(skb_put(skb, descr.Len),
  470. priv->base +
  471. descr.LowAddr, descr.Len);
  472. skb->dev = dev;
  473. skb->protocol = eth_type_trans(skb, dev);
  474. skb->ip_summed = CHECKSUM_NONE;
  475. priv->stat.rx_packets++;
  476. priv->stat.rx_bytes += descr.Len;
  477. netif_rx(skb);
  478. dev->last_rx = jiffies;
  479. }
  480. }
  481. /* give descriptor back to LANCE */
  482. descr.Len = 0;
  483. descr.Flags |= RXDSCR_FLAGS_OWN;
  484. /* update descriptor in shared RAM */
  485. memcpy_toio(priv->base + descraddr, &descr,
  486. sizeof(LANCE_RxDescr));
  487. /* go to next descriptor */
  488. priv->nextrx++;
  489. descraddr += sizeof(LANCE_RxDescr);
  490. if (priv->nextrx >= RXCOUNT) {
  491. priv->nextrx = 0;
  492. descraddr = RAM_RXBASE;
  493. }
  494. }
  495. /* reset RINT bit */
  496. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_RINT);
  497. return GetLANCE(dev, LANCE_CSR0);
  498. }
  499. /* transmit interrupt */
  500. static u16 irqtx_handler(struct net_device *dev, u16 oldcsr0)
  501. {
  502. skmca_priv *priv = netdev_priv(dev);
  503. LANCE_TxDescr descr;
  504. unsigned int descraddr;
  505. /* check descriptors at most until no busy one is left */
  506. descraddr =
  507. RAM_TXBASE + (priv->nexttxdone * sizeof(LANCE_TxDescr));
  508. while (priv->txbusy > 0) {
  509. /* read descriptor */
  510. memcpy_fromio(&descr, priv->base + descraddr,
  511. sizeof(LANCE_TxDescr));
  512. /* if the LANCE still owns this one, we've worked out all sent packets */
  513. if ((descr.Flags & TXDSCR_FLAGS_OWN) != 0)
  514. break;
  515. #ifdef DEBUG
  516. PrTime();
  517. printk("Send packet done on descr %d\n", priv->nexttxdone);
  518. #endif
  519. /* update statistics */
  520. if ((descr.Flags & TXDSCR_FLAGS_ERR) == 0) {
  521. priv->stat.tx_packets++;
  522. priv->stat.tx_bytes++;
  523. } else {
  524. priv->stat.tx_errors++;
  525. if ((descr.Status & TXDSCR_STATUS_UFLO) != 0) {
  526. priv->stat.tx_fifo_errors++;
  527. InitLANCE(dev);
  528. }
  529. else
  530. if ((descr.Status & TXDSCR_STATUS_LCOL) !=
  531. 0) priv->stat.tx_window_errors++;
  532. else if ((descr.Status & TXDSCR_STATUS_LCAR) != 0)
  533. priv->stat.tx_carrier_errors++;
  534. else if ((descr.Status & TXDSCR_STATUS_RTRY) != 0)
  535. priv->stat.tx_aborted_errors++;
  536. }
  537. /* go to next descriptor */
  538. priv->nexttxdone++;
  539. descraddr += sizeof(LANCE_TxDescr);
  540. if (priv->nexttxdone >= TXCOUNT) {
  541. priv->nexttxdone = 0;
  542. descraddr = RAM_TXBASE;
  543. }
  544. priv->txbusy--;
  545. }
  546. /* reset TX interrupt bit */
  547. SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_TINT);
  548. oldcsr0 = GetLANCE(dev, LANCE_CSR0);
  549. /* at least one descriptor is freed. Therefore we can accept
  550. a new one */
  551. /* inform upper layers we're in business again */
  552. netif_wake_queue(dev);
  553. return oldcsr0;
  554. }
  555. /* general interrupt entry */
  556. static irqreturn_t irq_handler(int irq, void *device, struct pt_regs *regs)
  557. {
  558. struct net_device *dev = (struct net_device *) device;
  559. u16 csr0val;
  560. /* read CSR0 to get interrupt cause */
  561. csr0val = GetLANCE(dev, LANCE_CSR0);
  562. /* in case we're not meant... */
  563. if ((csr0val & CSR0_INTR) == 0)
  564. return IRQ_NONE;
  565. #if 0
  566. set_bit(LINK_STATE_RXSEM, &dev->state);
  567. #endif
  568. /* loop through the interrupt bits until everything is clear */
  569. do {
  570. if ((csr0val & CSR0_IDON) != 0)
  571. csr0val = irqstart_handler(dev, csr0val);
  572. if ((csr0val & CSR0_RINT) != 0)
  573. csr0val = irqrx_handler(dev, csr0val);
  574. if ((csr0val & CSR0_MISS) != 0)
  575. csr0val = irqmiss_handler(dev, csr0val);
  576. if ((csr0val & CSR0_TINT) != 0)
  577. csr0val = irqtx_handler(dev, csr0val);
  578. if ((csr0val & CSR0_MERR) != 0) {
  579. SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_MERR);
  580. csr0val = GetLANCE(dev, LANCE_CSR0);
  581. }
  582. if ((csr0val & CSR0_BABL) != 0) {
  583. SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_BABL);
  584. csr0val = GetLANCE(dev, LANCE_CSR0);
  585. }
  586. }
  587. while ((csr0val & CSR0_INTR) != 0);
  588. #if 0
  589. clear_bit(LINK_STATE_RXSEM, &dev->state);
  590. #endif
  591. return IRQ_HANDLED;
  592. }
  593. /* ------------------------------------------------------------------------
  594. * driver methods
  595. * ------------------------------------------------------------------------ */
  596. /* MCA info */
  597. static int skmca_getinfo(char *buf, int slot, void *d)
  598. {
  599. int len = 0, i;
  600. struct net_device *dev = (struct net_device *) d;
  601. skmca_priv *priv;
  602. /* can't say anything about an uninitialized device... */
  603. if (dev == NULL)
  604. return len;
  605. priv = netdev_priv(dev);
  606. /* print info */
  607. len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
  608. len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
  609. dev->mem_end - 1);
  610. len +=
  611. sprintf(buf + len, "Transceiver: %s\n",
  612. MediaNames[priv->medium]);
  613. len += sprintf(buf + len, "Device: %s\n", dev->name);
  614. len += sprintf(buf + len, "MAC address:");
  615. for (i = 0; i < 6; i++)
  616. len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
  617. buf[len++] = '\n';
  618. buf[len] = 0;
  619. return len;
  620. }
  621. /* open driver. Means also initialization and start of LANCE */
  622. static int skmca_open(struct net_device *dev)
  623. {
  624. int result;
  625. skmca_priv *priv = netdev_priv(dev);
  626. /* register resources - only necessary for IRQ */
  627. result =
  628. request_irq(priv->realirq, irq_handler,
  629. SA_SHIRQ | SA_SAMPLE_RANDOM, "sk_mca", dev);
  630. if (result != 0) {
  631. printk("%s: failed to register irq %d\n", dev->name,
  632. dev->irq);
  633. return result;
  634. }
  635. dev->irq = priv->realirq;
  636. /* set up the card and LANCE */
  637. InitBoard(dev);
  638. /* set up flags */
  639. netif_start_queue(dev);
  640. return 0;
  641. }
  642. /* close driver. Shut down board and free allocated resources */
  643. static int skmca_close(struct net_device *dev)
  644. {
  645. /* turn off board */
  646. DeinitBoard(dev);
  647. /* release resources */
  648. if (dev->irq != 0)
  649. free_irq(dev->irq, dev);
  650. dev->irq = 0;
  651. return 0;
  652. }
  653. /* transmit a block. */
  654. static int skmca_tx(struct sk_buff *skb, struct net_device *dev)
  655. {
  656. skmca_priv *priv = netdev_priv(dev);
  657. LANCE_TxDescr descr;
  658. unsigned int address;
  659. int tmplen, retval = 0;
  660. unsigned long flags;
  661. /* if we get called with a NULL descriptor, the Ethernet layer thinks
  662. our card is stuck an we should reset it. We'll do this completely: */
  663. if (skb == NULL) {
  664. DeinitBoard(dev);
  665. InitBoard(dev);
  666. return 0; /* don't try to free the block here ;-) */
  667. }
  668. /* is there space in the Tx queue ? If no, the upper layer gave us a
  669. packet in spite of us not being ready and is really in trouble.
  670. We'll do the dropping for him: */
  671. if (priv->txbusy >= TXCOUNT) {
  672. priv->stat.tx_dropped++;
  673. retval = -EIO;
  674. goto tx_done;
  675. }
  676. /* get TX descriptor */
  677. address = RAM_TXBASE + (priv->nexttxput * sizeof(LANCE_TxDescr));
  678. memcpy_fromio(&descr, priv->base + address, sizeof(LANCE_TxDescr));
  679. /* enter packet length as 2s complement - assure minimum length */
  680. tmplen = skb->len;
  681. if (tmplen < 60)
  682. tmplen = 60;
  683. descr.Len = 65536 - tmplen;
  684. /* copy filler into RAM - in case we're filling up...
  685. we're filling a bit more than necessary, but that doesn't harm
  686. since the buffer is far larger... */
  687. if (tmplen > skb->len) {
  688. char *fill = "NetBSD is a nice OS too! ";
  689. unsigned int destoffs = 0, l = strlen(fill);
  690. while (destoffs < tmplen) {
  691. memcpy_toio(priv->base + descr.LowAddr +
  692. destoffs, fill, l);
  693. destoffs += l;
  694. }
  695. }
  696. /* do the real data copying */
  697. memcpy_toio(priv->base + descr.LowAddr, skb->data, skb->len);
  698. /* hand descriptor over to LANCE - this is the first and last chunk */
  699. descr.Flags =
  700. TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
  701. #ifdef DEBUG
  702. PrTime();
  703. printk("Send packet on descr %d len %d\n", priv->nexttxput,
  704. skb->len);
  705. #endif
  706. /* one more descriptor busy */
  707. spin_lock_irqsave(&priv->lock, flags);
  708. priv->nexttxput++;
  709. if (priv->nexttxput >= TXCOUNT)
  710. priv->nexttxput = 0;
  711. priv->txbusy++;
  712. /* are we saturated ? */
  713. if (priv->txbusy >= TXCOUNT)
  714. netif_stop_queue(dev);
  715. /* write descriptor back to RAM */
  716. memcpy_toio(priv->base + address, &descr, sizeof(LANCE_TxDescr));
  717. /* if no descriptors were active, give the LANCE a hint to read it
  718. immediately */
  719. if (priv->txbusy == 0)
  720. SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
  721. spin_unlock_irqrestore(&priv->lock, flags);
  722. tx_done:
  723. dev_kfree_skb(skb);
  724. return retval;
  725. }
  726. /* return pointer to Ethernet statistics */
  727. static struct net_device_stats *skmca_stats(struct net_device *dev)
  728. {
  729. skmca_priv *priv = netdev_priv(dev);
  730. return &(priv->stat);
  731. }
  732. /* switch receiver mode. We use the LANCE's multicast filter to prefilter
  733. multicast addresses. */
  734. static void skmca_set_multicast_list(struct net_device *dev)
  735. {
  736. skmca_priv *priv = netdev_priv(dev);
  737. LANCE_InitBlock block;
  738. /* first stop the LANCE... */
  739. StopLANCE(dev);
  740. /* ...then modify the initialization block... */
  741. memcpy_fromio(&block, priv->base + RAM_INITBASE, sizeof(block));
  742. if (dev->flags & IFF_PROMISC)
  743. block.Mode |= LANCE_INIT_PROM;
  744. else
  745. block.Mode &= ~LANCE_INIT_PROM;
  746. if (dev->flags & IFF_ALLMULTI) { /* get all multicasts */
  747. memset(block.LAdrF, 0xff, sizeof(block.LAdrF));
  748. } else { /* get selected/no multicasts */
  749. struct dev_mc_list *mptr;
  750. int code;
  751. memset(block.LAdrF, 0, sizeof(block.LAdrF));
  752. for (mptr = dev->mc_list; mptr != NULL; mptr = mptr->next) {
  753. code = GetHash(mptr->dmi_addr);
  754. block.LAdrF[(code >> 3) & 7] |= 1 << (code & 7);
  755. }
  756. }
  757. memcpy_toio(priv->base + RAM_INITBASE, &block, sizeof(block));
  758. /* ...then reinit LANCE with the correct flags */
  759. InitLANCE(dev);
  760. }
  761. /* ------------------------------------------------------------------------
  762. * hardware check
  763. * ------------------------------------------------------------------------ */
  764. static int startslot; /* counts through slots when probing multiple devices */
  765. static void cleanup_card(struct net_device *dev)
  766. {
  767. skmca_priv *priv = netdev_priv(dev);
  768. DeinitBoard(dev);
  769. if (dev->irq != 0)
  770. free_irq(dev->irq, dev);
  771. iounmap(priv->base);
  772. mca_mark_as_unused(priv->slot);
  773. mca_set_adapter_procfn(priv->slot, NULL, NULL);
  774. }
  775. struct net_device * __init skmca_probe(int unit)
  776. {
  777. struct net_device *dev;
  778. int force_detect = 0;
  779. int junior, slot, i;
  780. int base = 0, irq = 0;
  781. skmca_priv *priv;
  782. skmca_medium medium;
  783. int err;
  784. /* can't work without an MCA bus ;-) */
  785. if (MCA_bus == 0)
  786. return ERR_PTR(-ENODEV);
  787. dev = alloc_etherdev(sizeof(skmca_priv));
  788. if (!dev)
  789. return ERR_PTR(-ENOMEM);
  790. if (unit >= 0) {
  791. sprintf(dev->name, "eth%d", unit);
  792. netdev_boot_setup_check(dev);
  793. }
  794. SET_MODULE_OWNER(dev);
  795. /* start address of 1 --> forced detection */
  796. if (dev->mem_start == 1)
  797. force_detect = 1;
  798. /* search through slots */
  799. base = dev->mem_start;
  800. irq = dev->base_addr;
  801. for (slot = startslot; (slot = dofind(&junior, slot)) != -1; slot++) {
  802. /* deduce card addresses */
  803. getaddrs(slot, junior, &base, &irq, &medium);
  804. /* slot already in use ? */
  805. if (mca_is_adapter_used(slot))
  806. continue;
  807. /* were we looking for something different ? */
  808. if (dev->irq && dev->irq != irq)
  809. continue;
  810. if (dev->mem_start && dev->mem_start != base)
  811. continue;
  812. /* found something that matches */
  813. break;
  814. }
  815. /* nothing found ? */
  816. if (slot == -1) {
  817. free_netdev(dev);
  818. return (base || irq) ? ERR_PTR(-ENXIO) : ERR_PTR(-ENODEV);
  819. }
  820. /* make procfs entries */
  821. if (junior)
  822. mca_set_adapter_name(slot,
  823. "SKNET junior MC2 Ethernet Adapter");
  824. else
  825. mca_set_adapter_name(slot, "SKNET MC2+ Ethernet Adapter");
  826. mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
  827. mca_mark_as_used(slot);
  828. /* announce success */
  829. printk("%s: SKNet %s adapter found in slot %d\n", dev->name,
  830. junior ? "Junior MC2" : "MC2+", slot + 1);
  831. priv = netdev_priv(dev);
  832. priv->base = ioremap(base, 0x4000);
  833. if (!priv->base) {
  834. mca_set_adapter_procfn(slot, NULL, NULL);
  835. mca_mark_as_unused(slot);
  836. free_netdev(dev);
  837. return ERR_PTR(-ENOMEM);
  838. }
  839. priv->slot = slot;
  840. priv->macbase = priv->base + 0x3fc0;
  841. priv->ioregaddr = priv->base + 0x3ff0;
  842. priv->ctrladdr = priv->base + 0x3ff2;
  843. priv->cmdaddr = priv->base + 0x3ff3;
  844. priv->medium = medium;
  845. memset(&priv->stat, 0, sizeof(struct net_device_stats));
  846. spin_lock_init(&priv->lock);
  847. /* set base + irq for this device (irq not allocated so far) */
  848. dev->irq = 0;
  849. dev->mem_start = base;
  850. dev->mem_end = base + 0x4000;
  851. /* autoprobe ? */
  852. if (irq < 0) {
  853. int nirq;
  854. printk
  855. ("%s: ambigous POS bit combination, must probe for IRQ...\n",
  856. dev->name);
  857. nirq = ProbeIRQ(dev);
  858. if (nirq <= 0)
  859. printk("%s: IRQ probe failed, assuming IRQ %d",
  860. dev->name, priv->realirq = -irq);
  861. else
  862. priv->realirq = nirq;
  863. } else
  864. priv->realirq = irq;
  865. /* set methods */
  866. dev->open = skmca_open;
  867. dev->stop = skmca_close;
  868. dev->hard_start_xmit = skmca_tx;
  869. dev->do_ioctl = NULL;
  870. dev->get_stats = skmca_stats;
  871. dev->set_multicast_list = skmca_set_multicast_list;
  872. dev->flags |= IFF_MULTICAST;
  873. /* copy out MAC address */
  874. for (i = 0; i < 6; i++)
  875. dev->dev_addr[i] = readb(priv->macbase + (i << 1));
  876. /* print config */
  877. printk("%s: IRQ %d, memory %#lx-%#lx, "
  878. "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
  879. dev->name, priv->realirq, dev->mem_start, dev->mem_end - 1,
  880. dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  881. dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
  882. printk("%s: %s medium\n", dev->name, MediaNames[priv->medium]);
  883. /* reset board */
  884. ResetBoard(dev);
  885. startslot = slot + 1;
  886. err = register_netdev(dev);
  887. if (err) {
  888. cleanup_card(dev);
  889. free_netdev(dev);
  890. dev = ERR_PTR(err);
  891. }
  892. return dev;
  893. }
  894. /* ------------------------------------------------------------------------
  895. * modularization support
  896. * ------------------------------------------------------------------------ */
  897. #ifdef MODULE
  898. MODULE_LICENSE("GPL");
  899. #define DEVMAX 5
  900. static struct net_device *moddevs[DEVMAX];
  901. int init_module(void)
  902. {
  903. int z;
  904. startslot = 0;
  905. for (z = 0; z < DEVMAX; z++) {
  906. struct net_device *dev = skmca_probe(-1);
  907. if (IS_ERR(dev))
  908. break;
  909. moddevs[z] = dev;
  910. }
  911. if (!z)
  912. return -EIO;
  913. return 0;
  914. }
  915. void cleanup_module(void)
  916. {
  917. int z;
  918. for (z = 0; z < DEVMAX; z++) {
  919. struct net_device *dev = moddevs[z];
  920. if (dev) {
  921. unregister_netdev(dev);
  922. cleanup_card(dev);
  923. free_netdev(dev);
  924. }
  925. }
  926. }
  927. #endif /* MODULE */