ibmlana.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. net-3-driver for the IBM LAN Adapter/A
  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 SK_MCA driver, which is itself based on the
  8. SK_G16 and 3C523 driver.
  9. paper sources:
  10. 'PC Hardware: Aufbau, Funktionsweise, Programmierung' by
  11. Hans-Peter Messmer for the basic Microchannel stuff
  12. 'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
  13. for help on Ethernet driver programming
  14. 'DP83934CVUL-20/25 MHz SONIC-T Ethernet Controller Datasheet' by National
  15. Semiconductor for info on the MAC chip
  16. 'LAN Technical Reference Ethernet Adapter Interface Version 1 Release 1.0
  17. Document Number SC30-3661-00' by IBM for info on the adapter itself
  18. Also see http://www.natsemi.com/
  19. special acknowledgements to:
  20. - Bob Eager for helping me out with documentation from IBM
  21. - Jim Shorney for his endless patience with me while I was using
  22. him as a beta tester to trace down the address filter bug ;-)
  23. Missing things:
  24. -> set debug level via ioctl instead of compile-time switches
  25. -> I didn't follow the development of the 2.1.x kernels, so my
  26. assumptions about which things changed with which kernel version
  27. are probably nonsense
  28. History:
  29. Nov 6th, 1999
  30. startup from SK_MCA driver
  31. Dec 6th, 1999
  32. finally got docs about the card. A big thank you to Bob Eager!
  33. Dec 12th, 1999
  34. first packet received
  35. Dec 13th, 1999
  36. recv queue done, tcpdump works
  37. Dec 15th, 1999
  38. transmission part works
  39. Dec 28th, 1999
  40. added usage of the isa_functions for Linux 2.3 . Things should
  41. still work with 2.0.x....
  42. Jan 28th, 2000
  43. in Linux 2.2.13, the version.h file mysteriously didn't get
  44. included. Added a workaround for this. Futhermore, it now
  45. not only compiles as a modules ;-)
  46. Jan 30th, 2000
  47. newer kernels automatically probe more than one board, so the
  48. 'startslot' as a variable is also needed here
  49. Apr 12th, 2000
  50. the interrupt mask register is not set 'hard' instead of individually
  51. setting registers, since this seems to set bits that shouldn't be
  52. set
  53. May 21st, 2000
  54. reset interrupt status immediately after CAM load
  55. add a recovery delay after releasing the chip's reset line
  56. May 24th, 2000
  57. finally found the bug in the address filter setup - damned signed
  58. chars!
  59. June 1st, 2000
  60. corrected version codes, added support for the latest 2.3 changes
  61. Oct 28th, 2002
  62. cleaned up for the 2.5 tree <alan@redhat.com>
  63. *************************************************************************/
  64. #include <linux/kernel.h>
  65. #include <linux/string.h>
  66. #include <linux/errno.h>
  67. #include <linux/ioport.h>
  68. #include <linux/slab.h>
  69. #include <linux/interrupt.h>
  70. #include <linux/delay.h>
  71. #include <linux/time.h>
  72. #include <linux/mca-legacy.h>
  73. #include <linux/module.h>
  74. #include <linux/netdevice.h>
  75. #include <linux/etherdevice.h>
  76. #include <linux/skbuff.h>
  77. #include <linux/bitops.h>
  78. #include <asm/processor.h>
  79. #include <asm/io.h>
  80. #define _IBM_LANA_DRIVER_
  81. #include "ibmlana.h"
  82. #undef DEBUG
  83. #define DRV_NAME "ibmlana"
  84. /* ------------------------------------------------------------------------
  85. * global static data - not more since we can handle multiple boards and
  86. * have to pack all state info into the device struct!
  87. * ------------------------------------------------------------------------ */
  88. static char *MediaNames[Media_Count] = {
  89. "10BaseT", "10Base5", "Unknown", "10Base2"
  90. };
  91. /* ------------------------------------------------------------------------
  92. * private subfunctions
  93. * ------------------------------------------------------------------------ */
  94. #ifdef DEBUG
  95. /* dump all registers */
  96. static void dumpregs(struct net_device *dev)
  97. {
  98. int z;
  99. for (z = 0; z < 160; z += 2) {
  100. if (!(z & 15))
  101. printk("REGS: %04x:", z);
  102. printk(" %04x", inw(dev->base_addr + z));
  103. if ((z & 15) == 14)
  104. printk("\n");
  105. }
  106. }
  107. /* dump parts of shared memory - only needed during debugging */
  108. static void dumpmem(struct net_device *dev, u32 start, u32 len)
  109. {
  110. ibmlana_priv *priv = netdev_priv(dev);
  111. int z;
  112. printk("Address %04x:\n", start);
  113. for (z = 0; z < len; z++) {
  114. if ((z & 15) == 0)
  115. printk("%04x:", z);
  116. printk(" %02x", readb(priv->base + start + z));
  117. if ((z & 15) == 15)
  118. printk("\n");
  119. }
  120. if ((z & 15) != 0)
  121. printk("\n");
  122. }
  123. /* print exact time - ditto */
  124. static void PrTime(void)
  125. {
  126. struct timeval tv;
  127. do_gettimeofday(&tv);
  128. printk("%9d:%06d: ", (int) tv.tv_sec, (int) tv.tv_usec);
  129. }
  130. #endif /* DEBUG */
  131. /* deduce resources out of POS registers */
  132. static void getaddrs(int slot, int *base, int *memlen, int *iobase,
  133. int *irq, ibmlana_medium * medium)
  134. {
  135. u_char pos0, pos1;
  136. pos0 = mca_read_stored_pos(slot, 2);
  137. pos1 = mca_read_stored_pos(slot, 3);
  138. *base = 0xc0000 + ((pos1 & 0xf0) << 9);
  139. *memlen = (pos1 & 0x01) ? 0x8000 : 0x4000;
  140. *iobase = (pos0 & 0xe0) << 7;
  141. switch (pos0 & 0x06) {
  142. case 0:
  143. *irq = 5;
  144. break;
  145. case 2:
  146. *irq = 15;
  147. break;
  148. case 4:
  149. *irq = 10;
  150. break;
  151. case 6:
  152. *irq = 11;
  153. break;
  154. }
  155. *medium = (pos0 & 0x18) >> 3;
  156. }
  157. /* wait on register value with mask and timeout */
  158. static int wait_timeout(struct net_device *dev, int regoffs, u16 mask,
  159. u16 value, int timeout)
  160. {
  161. unsigned long fin = jiffies + timeout;
  162. while (time_before(jiffies,fin))
  163. if ((inw(dev->base_addr + regoffs) & mask) == value)
  164. return 1;
  165. return 0;
  166. }
  167. /* reset the whole board */
  168. static void ResetBoard(struct net_device *dev)
  169. {
  170. unsigned char bcmval;
  171. /* read original board control value */
  172. bcmval = inb(dev->base_addr + BCMREG);
  173. /* set reset bit for a while */
  174. bcmval |= BCMREG_RESET;
  175. outb(bcmval, dev->base_addr + BCMREG);
  176. udelay(10);
  177. bcmval &= ~BCMREG_RESET;
  178. outb(bcmval, dev->base_addr + BCMREG);
  179. /* switch over to RAM again */
  180. bcmval |= BCMREG_RAMEN | BCMREG_RAMWIN;
  181. outb(bcmval, dev->base_addr + BCMREG);
  182. }
  183. /* calculate RAM layout & set up descriptors in RAM */
  184. static void InitDscrs(struct net_device *dev)
  185. {
  186. ibmlana_priv *priv = netdev_priv(dev);
  187. u32 addr, baddr, raddr;
  188. int z;
  189. tda_t tda;
  190. rda_t rda;
  191. rra_t rra;
  192. /* initialize RAM */
  193. memset_io(priv->base, 0xaa,
  194. dev->mem_start - dev->mem_start); /* XXX: typo? */
  195. /* setup n TX descriptors - independent of RAM size */
  196. priv->tdastart = addr = 0;
  197. priv->txbufstart = baddr = sizeof(tda_t) * TXBUFCNT;
  198. for (z = 0; z < TXBUFCNT; z++) {
  199. tda.status = 0;
  200. tda.config = 0;
  201. tda.length = 0;
  202. tda.fragcount = 1;
  203. tda.startlo = baddr;
  204. tda.starthi = 0;
  205. tda.fraglength = 0;
  206. if (z == TXBUFCNT - 1)
  207. tda.link = priv->tdastart;
  208. else
  209. tda.link = addr + sizeof(tda_t);
  210. tda.link |= 1;
  211. memcpy_toio(priv->base + addr, &tda, sizeof(tda_t));
  212. addr += sizeof(tda_t);
  213. baddr += PKTSIZE;
  214. }
  215. /* calculate how many receive buffers fit into remaining memory */
  216. priv->rxbufcnt = (dev->mem_end - dev->mem_start - baddr) / (sizeof(rra_t) + sizeof(rda_t) + PKTSIZE);
  217. /* calculate receive addresses */
  218. priv->rrastart = raddr = priv->txbufstart + (TXBUFCNT * PKTSIZE);
  219. priv->rdastart = addr = priv->rrastart + (priv->rxbufcnt * sizeof(rra_t));
  220. priv->rxbufstart = baddr = priv->rdastart + (priv->rxbufcnt * sizeof(rda_t));
  221. for (z = 0; z < priv->rxbufcnt; z++) {
  222. rra.startlo = baddr;
  223. rra.starthi = 0;
  224. rra.cntlo = PKTSIZE >> 1;
  225. rra.cnthi = 0;
  226. memcpy_toio(priv->base + raddr, &rra, sizeof(rra_t));
  227. rda.status = 0;
  228. rda.length = 0;
  229. rda.startlo = 0;
  230. rda.starthi = 0;
  231. rda.seqno = 0;
  232. if (z < priv->rxbufcnt - 1)
  233. rda.link = addr + sizeof(rda_t);
  234. else
  235. rda.link = 1;
  236. rda.inuse = 1;
  237. memcpy_toio(priv->base + addr, &rda, sizeof(rda_t));
  238. baddr += PKTSIZE;
  239. raddr += sizeof(rra_t);
  240. addr += sizeof(rda_t);
  241. }
  242. /* initialize current pointers */
  243. priv->nextrxdescr = 0;
  244. priv->lastrxdescr = priv->rxbufcnt - 1;
  245. priv->nexttxdescr = 0;
  246. priv->currtxdescr = 0;
  247. priv->txusedcnt = 0;
  248. memset(priv->txused, 0, sizeof(priv->txused));
  249. }
  250. /* set up Rx + Tx descriptors in SONIC */
  251. static int InitSONIC(struct net_device *dev)
  252. {
  253. ibmlana_priv *priv = netdev_priv(dev);
  254. /* set up start & end of resource area */
  255. outw(0, SONIC_URRA);
  256. outw(priv->rrastart, dev->base_addr + SONIC_RSA);
  257. outw(priv->rrastart + (priv->rxbufcnt * sizeof(rra_t)), dev->base_addr + SONIC_REA);
  258. outw(priv->rrastart, dev->base_addr + SONIC_RRP);
  259. outw(priv->rrastart, dev->base_addr + SONIC_RWP);
  260. /* set EOBC so that only one packet goes into one buffer */
  261. outw((PKTSIZE - 4) >> 1, dev->base_addr + SONIC_EOBC);
  262. /* let SONIC read the first RRA descriptor */
  263. outw(CMDREG_RRRA, dev->base_addr + SONIC_CMDREG);
  264. if (!wait_timeout(dev, SONIC_CMDREG, CMDREG_RRRA, 0, 2)) {
  265. printk(KERN_ERR "%s: SONIC did not respond on RRRA command - giving up.", dev->name);
  266. return 0;
  267. }
  268. /* point SONIC to the first RDA */
  269. outw(0, dev->base_addr + SONIC_URDA);
  270. outw(priv->rdastart, dev->base_addr + SONIC_CRDA);
  271. /* set upper half of TDA address */
  272. outw(0, dev->base_addr + SONIC_UTDA);
  273. return 1;
  274. }
  275. /* stop SONIC so we can reinitialize it */
  276. static void StopSONIC(struct net_device *dev)
  277. {
  278. /* disable interrupts */
  279. outb(inb(dev->base_addr + BCMREG) & (~BCMREG_IEN), dev->base_addr + BCMREG);
  280. outb(0, dev->base_addr + SONIC_IMREG);
  281. /* reset the SONIC */
  282. outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
  283. udelay(10);
  284. outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
  285. }
  286. /* initialize card and SONIC for proper operation */
  287. static void putcam(camentry_t * cams, int *camcnt, char *addr)
  288. {
  289. camentry_t *pcam = cams + (*camcnt);
  290. u8 *uaddr = (u8 *) addr;
  291. pcam->index = *camcnt;
  292. pcam->addr0 = (((u16) uaddr[1]) << 8) | uaddr[0];
  293. pcam->addr1 = (((u16) uaddr[3]) << 8) | uaddr[2];
  294. pcam->addr2 = (((u16) uaddr[5]) << 8) | uaddr[4];
  295. (*camcnt)++;
  296. }
  297. static void InitBoard(struct net_device *dev)
  298. {
  299. ibmlana_priv *priv = netdev_priv(dev);
  300. int camcnt;
  301. camentry_t cams[16];
  302. u32 cammask;
  303. struct dev_mc_list *mcptr;
  304. u16 rcrval;
  305. /* reset the SONIC */
  306. outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
  307. udelay(10);
  308. /* clear all spurious interrupts */
  309. outw(inw(dev->base_addr + SONIC_ISREG), dev->base_addr + SONIC_ISREG);
  310. /* set up the SONIC's bus interface - constant for this adapter -
  311. must be done while the SONIC is in reset */
  312. outw(DCREG_USR1 | DCREG_USR0 | DCREG_WC1 | DCREG_DW32, dev->base_addr + SONIC_DCREG);
  313. outw(0, dev->base_addr + SONIC_DCREG2);
  314. /* remove reset form the SONIC */
  315. outw(0, dev->base_addr + SONIC_CMDREG);
  316. udelay(10);
  317. /* data sheet requires URRA to be programmed before setting up the CAM contents */
  318. outw(0, dev->base_addr + SONIC_URRA);
  319. /* program the CAM entry 0 to the device address */
  320. camcnt = 0;
  321. putcam(cams, &camcnt, dev->dev_addr);
  322. /* start putting the multicast addresses into the CAM list. Stop if
  323. it is full. */
  324. for (mcptr = dev->mc_list; mcptr != NULL; mcptr = mcptr->next) {
  325. putcam(cams, &camcnt, mcptr->dmi_addr);
  326. if (camcnt == 16)
  327. break;
  328. }
  329. /* calculate CAM mask */
  330. cammask = (1 << camcnt) - 1;
  331. /* feed CDA into SONIC, initialize RCR value (always get broadcasts) */
  332. memcpy_toio(priv->base, cams, sizeof(camentry_t) * camcnt);
  333. memcpy_toio(priv->base + (sizeof(camentry_t) * camcnt), &cammask, sizeof(cammask));
  334. #ifdef DEBUG
  335. printk("CAM setup:\n");
  336. dumpmem(dev, 0, sizeof(camentry_t) * camcnt + sizeof(cammask));
  337. #endif
  338. outw(0, dev->base_addr + SONIC_CAMPTR);
  339. outw(camcnt, dev->base_addr + SONIC_CAMCNT);
  340. outw(CMDREG_LCAM, dev->base_addr + SONIC_CMDREG);
  341. if (!wait_timeout(dev, SONIC_CMDREG, CMDREG_LCAM, 0, 2)) {
  342. printk(KERN_ERR "%s:SONIC did not respond on LCAM command - giving up.", dev->name);
  343. return;
  344. } else {
  345. /* clear interrupt condition */
  346. outw(ISREG_LCD, dev->base_addr + SONIC_ISREG);
  347. #ifdef DEBUG
  348. printk("Loading CAM done, address pointers %04x:%04x\n",
  349. inw(dev->base_addr + SONIC_URRA),
  350. inw(dev->base_addr + SONIC_CAMPTR));
  351. {
  352. int z;
  353. printk("\n-->CAM: PTR %04x CNT %04x\n",
  354. inw(dev->base_addr + SONIC_CAMPTR),
  355. inw(dev->base_addr + SONIC_CAMCNT));
  356. outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
  357. for (z = 0; z < camcnt; z++) {
  358. outw(z, dev->base_addr + SONIC_CAMEPTR);
  359. printk("Entry %d: %04x %04x %04x\n", z,
  360. inw(dev->base_addr + SONIC_CAMADDR0),
  361. inw(dev->base_addr + SONIC_CAMADDR1),
  362. inw(dev->base_addr + SONIC_CAMADDR2));
  363. }
  364. outw(0, dev->base_addr + SONIC_CMDREG);
  365. }
  366. #endif
  367. }
  368. rcrval = RCREG_BRD | RCREG_LB_NONE;
  369. /* if still multicast addresses left or ALLMULTI is set, set the multicast
  370. enable bit */
  371. if ((dev->flags & IFF_ALLMULTI) || (mcptr != NULL))
  372. rcrval |= RCREG_AMC;
  373. /* promiscous mode ? */
  374. if (dev->flags & IFF_PROMISC)
  375. rcrval |= RCREG_PRO;
  376. /* program receive mode */
  377. outw(rcrval, dev->base_addr + SONIC_RCREG);
  378. #ifdef DEBUG
  379. printk("\nRCRVAL: %04x\n", rcrval);
  380. #endif
  381. /* set up descriptors in shared memory + feed them into SONIC registers */
  382. InitDscrs(dev);
  383. if (!InitSONIC(dev))
  384. return;
  385. /* reset all pending interrupts */
  386. outw(0xffff, dev->base_addr + SONIC_ISREG);
  387. /* enable transmitter + receiver interrupts */
  388. outw(CMDREG_RXEN, dev->base_addr + SONIC_CMDREG);
  389. outw(IMREG_PRXEN | IMREG_RBEEN | IMREG_PTXEN | IMREG_TXEREN, dev->base_addr + SONIC_IMREG);
  390. /* turn on card interrupts */
  391. outb(inb(dev->base_addr + BCMREG) | BCMREG_IEN, dev->base_addr + BCMREG);
  392. #ifdef DEBUG
  393. printk("Register dump after initialization:\n");
  394. dumpregs(dev);
  395. #endif
  396. }
  397. /* start transmission of a descriptor */
  398. static void StartTx(struct net_device *dev, int descr)
  399. {
  400. ibmlana_priv *priv = netdev_priv(dev);
  401. int addr;
  402. addr = priv->tdastart + (descr * sizeof(tda_t));
  403. /* put descriptor address into SONIC */
  404. outw(addr, dev->base_addr + SONIC_CTDA);
  405. /* trigger transmitter */
  406. priv->currtxdescr = descr;
  407. outw(CMDREG_TXP, dev->base_addr + SONIC_CMDREG);
  408. }
  409. /* ------------------------------------------------------------------------
  410. * interrupt handler(s)
  411. * ------------------------------------------------------------------------ */
  412. /* receive buffer area exhausted */
  413. static void irqrbe_handler(struct net_device *dev)
  414. {
  415. ibmlana_priv *priv = netdev_priv(dev);
  416. /* point the SONIC back to the RRA start */
  417. outw(priv->rrastart, dev->base_addr + SONIC_RRP);
  418. outw(priv->rrastart, dev->base_addr + SONIC_RWP);
  419. }
  420. /* receive interrupt */
  421. static void irqrx_handler(struct net_device *dev)
  422. {
  423. ibmlana_priv *priv = netdev_priv(dev);
  424. rda_t rda;
  425. u32 rdaaddr, lrdaaddr;
  426. /* loop until ... */
  427. while (1) {
  428. /* read descriptor that was next to be filled by SONIC */
  429. rdaaddr = priv->rdastart + (priv->nextrxdescr * sizeof(rda_t));
  430. lrdaaddr = priv->rdastart + (priv->lastrxdescr * sizeof(rda_t));
  431. memcpy_fromio(&rda, priv->base + rdaaddr, sizeof(rda_t));
  432. /* iron out upper word halves of fields we use - SONIC will duplicate
  433. bits 0..15 to 16..31 */
  434. rda.status &= 0xffff;
  435. rda.length &= 0xffff;
  436. rda.startlo &= 0xffff;
  437. /* stop if the SONIC still owns it, i.e. there is no data for us */
  438. if (rda.inuse)
  439. break;
  440. /* good packet? */
  441. else if (rda.status & RCREG_PRX) {
  442. struct sk_buff *skb;
  443. /* fetch buffer */
  444. skb = dev_alloc_skb(rda.length + 2);
  445. if (skb == NULL)
  446. priv->stat.rx_dropped++;
  447. else {
  448. /* copy out data */
  449. memcpy_fromio(skb_put(skb, rda.length),
  450. priv->base +
  451. rda.startlo, rda.length);
  452. /* set up skb fields */
  453. skb->dev = dev;
  454. skb->protocol = eth_type_trans(skb, dev);
  455. skb->ip_summed = CHECKSUM_NONE;
  456. /* bookkeeping */
  457. dev->last_rx = jiffies;
  458. priv->stat.rx_packets++;
  459. priv->stat.rx_bytes += rda.length;
  460. /* pass to the upper layers */
  461. netif_rx(skb);
  462. }
  463. }
  464. /* otherwise check error status bits and increase statistics */
  465. else {
  466. priv->stat.rx_errors++;
  467. if (rda.status & RCREG_FAER)
  468. priv->stat.rx_frame_errors++;
  469. if (rda.status & RCREG_CRCR)
  470. priv->stat.rx_crc_errors++;
  471. }
  472. /* descriptor processed, will become new last descriptor in queue */
  473. rda.link = 1;
  474. rda.inuse = 1;
  475. memcpy_toio(priv->base + rdaaddr, &rda,
  476. sizeof(rda_t));
  477. /* set up link and EOL = 0 in currently last descriptor. Only write
  478. the link field since the SONIC may currently already access the
  479. other fields. */
  480. memcpy_toio(priv->base + lrdaaddr + 20, &rdaaddr, 4);
  481. /* advance indices */
  482. priv->lastrxdescr = priv->nextrxdescr;
  483. if ((++priv->nextrxdescr) >= priv->rxbufcnt)
  484. priv->nextrxdescr = 0;
  485. }
  486. }
  487. /* transmit interrupt */
  488. static void irqtx_handler(struct net_device *dev)
  489. {
  490. ibmlana_priv *priv = netdev_priv(dev);
  491. tda_t tda;
  492. /* fetch descriptor (we forgot the size ;-) */
  493. memcpy_fromio(&tda, priv->base + priv->tdastart + (priv->currtxdescr * sizeof(tda_t)), sizeof(tda_t));
  494. /* update statistics */
  495. priv->stat.tx_packets++;
  496. priv->stat.tx_bytes += tda.length;
  497. /* update our pointers */
  498. priv->txused[priv->currtxdescr] = 0;
  499. priv->txusedcnt--;
  500. /* if there are more descriptors present in RAM, start them */
  501. if (priv->txusedcnt > 0)
  502. StartTx(dev, (priv->currtxdescr + 1) % TXBUFCNT);
  503. /* tell the upper layer we can go on transmitting */
  504. netif_wake_queue(dev);
  505. }
  506. static void irqtxerr_handler(struct net_device *dev)
  507. {
  508. ibmlana_priv *priv = netdev_priv(dev);
  509. tda_t tda;
  510. /* fetch descriptor to check status */
  511. memcpy_fromio(&tda, priv->base + priv->tdastart + (priv->currtxdescr * sizeof(tda_t)), sizeof(tda_t));
  512. /* update statistics */
  513. priv->stat.tx_errors++;
  514. if (tda.status & (TCREG_NCRS | TCREG_CRSL))
  515. priv->stat.tx_carrier_errors++;
  516. if (tda.status & TCREG_EXC)
  517. priv->stat.tx_aborted_errors++;
  518. if (tda.status & TCREG_OWC)
  519. priv->stat.tx_window_errors++;
  520. if (tda.status & TCREG_FU)
  521. priv->stat.tx_fifo_errors++;
  522. /* update our pointers */
  523. priv->txused[priv->currtxdescr] = 0;
  524. priv->txusedcnt--;
  525. /* if there are more descriptors present in RAM, start them */
  526. if (priv->txusedcnt > 0)
  527. StartTx(dev, (priv->currtxdescr + 1) % TXBUFCNT);
  528. /* tell the upper layer we can go on transmitting */
  529. netif_wake_queue(dev);
  530. }
  531. /* general interrupt entry */
  532. static irqreturn_t irq_handler(int irq, void *device)
  533. {
  534. struct net_device *dev = (struct net_device *) device;
  535. u16 ival;
  536. /* in case we're not meant... */
  537. if (!(inb(dev->base_addr + BCMREG) & BCMREG_IPEND))
  538. return IRQ_NONE;
  539. /* loop through the interrupt bits until everything is clear */
  540. while (1) {
  541. ival = inw(dev->base_addr + SONIC_ISREG);
  542. if (ival & ISREG_RBE) {
  543. irqrbe_handler(dev);
  544. outw(ISREG_RBE, dev->base_addr + SONIC_ISREG);
  545. }
  546. if (ival & ISREG_PKTRX) {
  547. irqrx_handler(dev);
  548. outw(ISREG_PKTRX, dev->base_addr + SONIC_ISREG);
  549. }
  550. if (ival & ISREG_TXDN) {
  551. irqtx_handler(dev);
  552. outw(ISREG_TXDN, dev->base_addr + SONIC_ISREG);
  553. }
  554. if (ival & ISREG_TXER) {
  555. irqtxerr_handler(dev);
  556. outw(ISREG_TXER, dev->base_addr + SONIC_ISREG);
  557. }
  558. break;
  559. }
  560. return IRQ_HANDLED;
  561. }
  562. /* ------------------------------------------------------------------------
  563. * driver methods
  564. * ------------------------------------------------------------------------ */
  565. /* MCA info */
  566. static int ibmlana_getinfo(char *buf, int slot, void *d)
  567. {
  568. int len = 0, i;
  569. struct net_device *dev = (struct net_device *) d;
  570. ibmlana_priv *priv;
  571. /* can't say anything about an uninitialized device... */
  572. if (dev == NULL)
  573. return len;
  574. priv = netdev_priv(dev);
  575. /* print info */
  576. len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
  577. len += sprintf(buf + len, "I/O: %#lx\n", dev->base_addr);
  578. len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start, dev->mem_end - 1);
  579. len += sprintf(buf + len, "Transceiver: %s\n", MediaNames[priv->medium]);
  580. len += sprintf(buf + len, "Device: %s\n", dev->name);
  581. len += sprintf(buf + len, "MAC address:");
  582. for (i = 0; i < 6; i++)
  583. len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
  584. buf[len++] = '\n';
  585. buf[len] = 0;
  586. return len;
  587. }
  588. /* open driver. Means also initialization and start of LANCE */
  589. static int ibmlana_open(struct net_device *dev)
  590. {
  591. int result;
  592. ibmlana_priv *priv = netdev_priv(dev);
  593. /* register resources - only necessary for IRQ */
  594. result = request_irq(priv->realirq, irq_handler, IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
  595. if (result != 0) {
  596. printk(KERN_ERR "%s: failed to register irq %d\n", dev->name, dev->irq);
  597. return result;
  598. }
  599. dev->irq = priv->realirq;
  600. /* set up the card and SONIC */
  601. InitBoard(dev);
  602. /* initialize operational flags */
  603. netif_start_queue(dev);
  604. return 0;
  605. }
  606. /* close driver. Shut down board and free allocated resources */
  607. static int ibmlana_close(struct net_device *dev)
  608. {
  609. /* turn off board */
  610. /* release resources */
  611. if (dev->irq != 0)
  612. free_irq(dev->irq, dev);
  613. dev->irq = 0;
  614. return 0;
  615. }
  616. /* transmit a block. */
  617. static int ibmlana_tx(struct sk_buff *skb, struct net_device *dev)
  618. {
  619. ibmlana_priv *priv = netdev_priv(dev);
  620. int retval = 0, tmplen, addr;
  621. unsigned long flags;
  622. tda_t tda;
  623. int baddr;
  624. /* find out if there are free slots for a frame to transmit. If not,
  625. the upper layer is in deep desperation and we simply ignore the frame. */
  626. if (priv->txusedcnt >= TXBUFCNT) {
  627. retval = -EIO;
  628. priv->stat.tx_dropped++;
  629. goto tx_done;
  630. }
  631. /* copy the frame data into the next free transmit buffer - fillup missing */
  632. tmplen = skb->len;
  633. if (tmplen < 60)
  634. tmplen = 60;
  635. baddr = priv->txbufstart + (priv->nexttxdescr * PKTSIZE);
  636. memcpy_toio(priv->base + baddr, skb->data, skb->len);
  637. /* copy filler into RAM - in case we're filling up...
  638. we're filling a bit more than necessary, but that doesn't harm
  639. since the buffer is far larger...
  640. Sorry Linus for the filler string but I couldn't resist ;-) */
  641. if (tmplen > skb->len) {
  642. char *fill = "NetBSD is a nice OS too! ";
  643. unsigned int destoffs = skb->len, l = strlen(fill);
  644. while (destoffs < tmplen) {
  645. memcpy_toio(priv->base + baddr + destoffs, fill, l);
  646. destoffs += l;
  647. }
  648. }
  649. /* set up the new frame descriptor */
  650. addr = priv->tdastart + (priv->nexttxdescr * sizeof(tda_t));
  651. memcpy_fromio(&tda, priv->base + addr, sizeof(tda_t));
  652. tda.length = tda.fraglength = tmplen;
  653. memcpy_toio(priv->base + addr, &tda, sizeof(tda_t));
  654. /* if there were no active descriptors, trigger the SONIC */
  655. spin_lock_irqsave(&priv->lock, flags);
  656. priv->txusedcnt++;
  657. priv->txused[priv->nexttxdescr] = 1;
  658. /* are all transmission slots used up ? */
  659. if (priv->txusedcnt >= TXBUFCNT)
  660. netif_stop_queue(dev);
  661. if (priv->txusedcnt == 1)
  662. StartTx(dev, priv->nexttxdescr);
  663. priv->nexttxdescr = (priv->nexttxdescr + 1) % TXBUFCNT;
  664. spin_unlock_irqrestore(&priv->lock, flags);
  665. tx_done:
  666. dev_kfree_skb(skb);
  667. return retval;
  668. }
  669. /* return pointer to Ethernet statistics */
  670. static struct net_device_stats *ibmlana_stats(struct net_device *dev)
  671. {
  672. ibmlana_priv *priv = netdev_priv(dev);
  673. return &priv->stat;
  674. }
  675. /* switch receiver mode. */
  676. static void ibmlana_set_multicast_list(struct net_device *dev)
  677. {
  678. /* first stop the SONIC... */
  679. StopSONIC(dev);
  680. /* ...then reinit it with the new flags */
  681. InitBoard(dev);
  682. }
  683. /* ------------------------------------------------------------------------
  684. * hardware check
  685. * ------------------------------------------------------------------------ */
  686. static int startslot; /* counts through slots when probing multiple devices */
  687. static int ibmlana_probe(struct net_device *dev)
  688. {
  689. int slot, z;
  690. int base = 0, irq = 0, iobase = 0, memlen = 0;
  691. ibmlana_priv *priv;
  692. ibmlana_medium medium;
  693. SET_MODULE_OWNER(dev);
  694. /* can't work without an MCA bus ;-) */
  695. if (MCA_bus == 0)
  696. return -ENODEV;
  697. base = dev->mem_start;
  698. irq = dev->irq;
  699. for (slot = startslot; (slot = mca_find_adapter(IBM_LANA_ID, slot)) != -1; slot++) {
  700. /* deduce card addresses */
  701. getaddrs(slot, &base, &memlen, &iobase, &irq, &medium);
  702. /* slot already in use ? */
  703. if (mca_is_adapter_used(slot))
  704. continue;
  705. /* were we looking for something different ? */
  706. if (dev->irq && dev->irq != irq)
  707. continue;
  708. if (dev->mem_start && dev->mem_start != base)
  709. continue;
  710. /* found something that matches */
  711. break;
  712. }
  713. /* nothing found ? */
  714. if (slot == -1)
  715. return (base != 0 || irq != 0) ? -ENXIO : -ENODEV;
  716. /* announce success */
  717. printk(KERN_INFO "%s: IBM LAN Adapter/A found in slot %d\n", dev->name, slot + 1);
  718. /* try to obtain I/O range */
  719. if (!request_region(iobase, IBM_LANA_IORANGE, DRV_NAME)) {
  720. printk(KERN_ERR "%s: cannot allocate I/O range at %#x!\n", DRV_NAME, iobase);
  721. startslot = slot + 1;
  722. return -EBUSY;
  723. }
  724. priv = netdev_priv(dev);
  725. priv->slot = slot;
  726. priv->realirq = irq;
  727. priv->medium = medium;
  728. spin_lock_init(&priv->lock);
  729. /* set base + irq for this device (irq not allocated so far) */
  730. dev->irq = 0;
  731. dev->mem_start = base;
  732. dev->mem_end = base + memlen;
  733. dev->base_addr = iobase;
  734. priv->base = ioremap(base, memlen);
  735. if (!priv->base) {
  736. printk(KERN_ERR "%s: cannot remap memory!\n", DRV_NAME);
  737. startslot = slot + 1;
  738. release_region(iobase, IBM_LANA_IORANGE);
  739. return -EBUSY;
  740. }
  741. /* make procfs entries */
  742. mca_set_adapter_name(slot, "IBM LAN Adapter/A");
  743. mca_set_adapter_procfn(slot, (MCA_ProcFn) ibmlana_getinfo, dev);
  744. mca_mark_as_used(slot);
  745. /* set methods */
  746. dev->open = ibmlana_open;
  747. dev->stop = ibmlana_close;
  748. dev->hard_start_xmit = ibmlana_tx;
  749. dev->do_ioctl = NULL;
  750. dev->get_stats = ibmlana_stats;
  751. dev->set_multicast_list = ibmlana_set_multicast_list;
  752. dev->flags |= IFF_MULTICAST;
  753. /* copy out MAC address */
  754. for (z = 0; z < sizeof(dev->dev_addr); z++)
  755. dev->dev_addr[z] = inb(dev->base_addr + MACADDRPROM + z);
  756. /* print config */
  757. printk(KERN_INFO "%s: IRQ %d, I/O %#lx, memory %#lx-%#lx, "
  758. "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
  759. dev->name, priv->realirq, dev->base_addr,
  760. dev->mem_start, dev->mem_end - 1,
  761. dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  762. dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
  763. printk(KERN_INFO "%s: %s medium\n", dev->name, MediaNames[priv->medium]);
  764. /* reset board */
  765. ResetBoard(dev);
  766. /* next probe will start at next slot */
  767. startslot = slot + 1;
  768. return 0;
  769. }
  770. /* ------------------------------------------------------------------------
  771. * modularization support
  772. * ------------------------------------------------------------------------ */
  773. #ifdef MODULE
  774. #define DEVMAX 5
  775. static struct net_device *moddevs[DEVMAX];
  776. static int irq;
  777. static int io;
  778. module_param(irq, int, 0);
  779. module_param(io, int, 0);
  780. MODULE_PARM_DESC(irq, "IBM LAN/A IRQ number");
  781. MODULE_PARM_DESC(io, "IBM LAN/A I/O base address");
  782. MODULE_LICENSE("GPL");
  783. int init_module(void)
  784. {
  785. int z;
  786. startslot = 0;
  787. for (z = 0; z < DEVMAX; z++) {
  788. struct net_device *dev = alloc_etherdev(sizeof(ibmlana_priv));
  789. if (!dev)
  790. break;
  791. dev->irq = irq;
  792. dev->base_addr = io;
  793. if (ibmlana_probe(dev)) {
  794. free_netdev(dev);
  795. break;
  796. }
  797. if (register_netdev(dev)) {
  798. ibmlana_priv *priv = netdev_priv(dev);
  799. release_region(dev->base_addr, IBM_LANA_IORANGE);
  800. mca_mark_as_unused(priv->slot);
  801. mca_set_adapter_name(priv->slot, "");
  802. mca_set_adapter_procfn(priv->slot, NULL, NULL);
  803. iounmap(priv->base);
  804. free_netdev(dev);
  805. break;
  806. }
  807. moddevs[z] = dev;
  808. }
  809. return (z > 0) ? 0 : -EIO;
  810. }
  811. void cleanup_module(void)
  812. {
  813. int z;
  814. for (z = 0; z < DEVMAX; z++) {
  815. struct net_device *dev = moddevs[z];
  816. if (dev) {
  817. ibmlana_priv *priv = netdev_priv(dev);
  818. unregister_netdev(dev);
  819. /*DeinitBoard(dev); */
  820. release_region(dev->base_addr, IBM_LANA_IORANGE);
  821. mca_mark_as_unused(priv->slot);
  822. mca_set_adapter_name(priv->slot, "");
  823. mca_set_adapter_procfn(priv->slot, NULL, NULL);
  824. iounmap(priv->base);
  825. free_netdev(dev);
  826. }
  827. }
  828. }
  829. #endif /* MODULE */