declance.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. * Lance ethernet driver for the MIPS processor based
  3. * DECstation family
  4. *
  5. *
  6. * adopted from sunlance.c by Richard van den Berg
  7. *
  8. * Copyright (C) 2002, 2003, 2005, 2006 Maciej W. Rozycki
  9. *
  10. * additional sources:
  11. * - PMAD-AA TURBOchannel Ethernet Module Functional Specification,
  12. * Revision 1.2
  13. *
  14. * History:
  15. *
  16. * v0.001: The kernel accepts the code and it shows the hardware address.
  17. *
  18. * v0.002: Removed most sparc stuff, left only some module and dma stuff.
  19. *
  20. * v0.003: Enhanced base address calculation from proposals by
  21. * Harald Koerfgen and Thomas Riemer.
  22. *
  23. * v0.004: lance-regs is pointing at the right addresses, added prom
  24. * check. First start of address mapping and DMA.
  25. *
  26. * v0.005: started to play around with LANCE-DMA. This driver will not
  27. * work for non IOASIC lances. HK
  28. *
  29. * v0.006: added pointer arrays to lance_private and setup routine for
  30. * them in dec_lance_init. HK
  31. *
  32. * v0.007: Big shit. The LANCE seems to use a different DMA mechanism to
  33. * access the init block. This looks like one (short) word at a
  34. * time, but the smallest amount the IOASIC can transfer is a
  35. * (long) word. So we have a 2-2 padding here. Changed
  36. * lance_init_block accordingly. The 16-16 padding for the buffers
  37. * seems to be correct. HK
  38. *
  39. * v0.008: mods to make PMAX_LANCE work. 01/09/1999 triemer
  40. *
  41. * v0.009: Module support fixes, multiple interfaces support, various
  42. * bits. macro
  43. *
  44. * v0.010: Fixes for the PMAD mapping of the LANCE buffer and for the
  45. * PMAX requirement to only use halfword accesses to the
  46. * buffer. macro
  47. *
  48. * v0.011: Converted the PMAD to the driver model. macro
  49. */
  50. #include <linux/crc32.h>
  51. #include <linux/delay.h>
  52. #include <linux/errno.h>
  53. #include <linux/if_ether.h>
  54. #include <linux/init.h>
  55. #include <linux/kernel.h>
  56. #include <linux/module.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/etherdevice.h>
  59. #include <linux/spinlock.h>
  60. #include <linux/stddef.h>
  61. #include <linux/string.h>
  62. #include <linux/tc.h>
  63. #include <linux/types.h>
  64. #include <asm/addrspace.h>
  65. #include <asm/system.h>
  66. #include <asm/dec/interrupts.h>
  67. #include <asm/dec/ioasic.h>
  68. #include <asm/dec/ioasic_addrs.h>
  69. #include <asm/dec/kn01.h>
  70. #include <asm/dec/machtype.h>
  71. #include <asm/dec/system.h>
  72. static char version[] __devinitdata =
  73. "declance.c: v0.011 by Linux MIPS DECstation task force\n";
  74. MODULE_AUTHOR("Linux MIPS DECstation task force");
  75. MODULE_DESCRIPTION("DEC LANCE (DECstation onboard, PMAD-xx) driver");
  76. MODULE_LICENSE("GPL");
  77. #define __unused __attribute__ ((unused))
  78. /*
  79. * card types
  80. */
  81. #define ASIC_LANCE 1
  82. #define PMAD_LANCE 2
  83. #define PMAX_LANCE 3
  84. #define LE_CSR0 0
  85. #define LE_CSR1 1
  86. #define LE_CSR2 2
  87. #define LE_CSR3 3
  88. #define LE_MO_PROM 0x8000 /* Enable promiscuous mode */
  89. #define LE_C0_ERR 0x8000 /* Error: set if BAB, SQE, MISS or ME is set */
  90. #define LE_C0_BABL 0x4000 /* BAB: Babble: tx timeout. */
  91. #define LE_C0_CERR 0x2000 /* SQE: Signal quality error */
  92. #define LE_C0_MISS 0x1000 /* MISS: Missed a packet */
  93. #define LE_C0_MERR 0x0800 /* ME: Memory error */
  94. #define LE_C0_RINT 0x0400 /* Received interrupt */
  95. #define LE_C0_TINT 0x0200 /* Transmitter Interrupt */
  96. #define LE_C0_IDON 0x0100 /* IFIN: Init finished. */
  97. #define LE_C0_INTR 0x0080 /* Interrupt or error */
  98. #define LE_C0_INEA 0x0040 /* Interrupt enable */
  99. #define LE_C0_RXON 0x0020 /* Receiver on */
  100. #define LE_C0_TXON 0x0010 /* Transmitter on */
  101. #define LE_C0_TDMD 0x0008 /* Transmitter demand */
  102. #define LE_C0_STOP 0x0004 /* Stop the card */
  103. #define LE_C0_STRT 0x0002 /* Start the card */
  104. #define LE_C0_INIT 0x0001 /* Init the card */
  105. #define LE_C3_BSWP 0x4 /* SWAP */
  106. #define LE_C3_ACON 0x2 /* ALE Control */
  107. #define LE_C3_BCON 0x1 /* Byte control */
  108. /* Receive message descriptor 1 */
  109. #define LE_R1_OWN 0x8000 /* Who owns the entry */
  110. #define LE_R1_ERR 0x4000 /* Error: if FRA, OFL, CRC or BUF is set */
  111. #define LE_R1_FRA 0x2000 /* FRA: Frame error */
  112. #define LE_R1_OFL 0x1000 /* OFL: Frame overflow */
  113. #define LE_R1_CRC 0x0800 /* CRC error */
  114. #define LE_R1_BUF 0x0400 /* BUF: Buffer error */
  115. #define LE_R1_SOP 0x0200 /* Start of packet */
  116. #define LE_R1_EOP 0x0100 /* End of packet */
  117. #define LE_R1_POK 0x0300 /* Packet is complete: SOP + EOP */
  118. /* Transmit message descriptor 1 */
  119. #define LE_T1_OWN 0x8000 /* Lance owns the packet */
  120. #define LE_T1_ERR 0x4000 /* Error summary */
  121. #define LE_T1_EMORE 0x1000 /* Error: more than one retry needed */
  122. #define LE_T1_EONE 0x0800 /* Error: one retry needed */
  123. #define LE_T1_EDEF 0x0400 /* Error: deferred */
  124. #define LE_T1_SOP 0x0200 /* Start of packet */
  125. #define LE_T1_EOP 0x0100 /* End of packet */
  126. #define LE_T1_POK 0x0300 /* Packet is complete: SOP + EOP */
  127. #define LE_T3_BUF 0x8000 /* Buffer error */
  128. #define LE_T3_UFL 0x4000 /* Error underflow */
  129. #define LE_T3_LCOL 0x1000 /* Error late collision */
  130. #define LE_T3_CLOS 0x0800 /* Error carrier loss */
  131. #define LE_T3_RTY 0x0400 /* Error retry */
  132. #define LE_T3_TDR 0x03ff /* Time Domain Reflectometry counter */
  133. /* Define: 2^4 Tx buffers and 2^4 Rx buffers */
  134. #ifndef LANCE_LOG_TX_BUFFERS
  135. #define LANCE_LOG_TX_BUFFERS 4
  136. #define LANCE_LOG_RX_BUFFERS 4
  137. #endif
  138. #define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
  139. #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
  140. #define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
  141. #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
  142. #define PKT_BUF_SZ 1536
  143. #define RX_BUFF_SIZE PKT_BUF_SZ
  144. #define TX_BUFF_SIZE PKT_BUF_SZ
  145. #undef TEST_HITS
  146. #define ZERO 0
  147. /*
  148. * The DS2100/3100 have a linear 64 kB buffer which supports halfword
  149. * accesses only. Each halfword of the buffer is word-aligned in the
  150. * CPU address space.
  151. *
  152. * The PMAD-AA has a 128 kB buffer on-board.
  153. *
  154. * The IOASIC LANCE devices use a shared memory region. This region
  155. * as seen from the CPU is (max) 128 kB long and has to be on an 128 kB
  156. * boundary. The LANCE sees this as a 64 kB long continuous memory
  157. * region.
  158. *
  159. * The LANCE's DMA address is used as an index in this buffer and DMA
  160. * takes place in bursts of eight 16-bit words which are packed into
  161. * four 32-bit words by the IOASIC. This leads to a strange padding:
  162. * 16 bytes of valid data followed by a 16 byte gap :-(.
  163. */
  164. struct lance_rx_desc {
  165. unsigned short rmd0; /* low address of packet */
  166. unsigned short rmd1; /* high address of packet
  167. and descriptor bits */
  168. short length; /* 2s complement (negative!)
  169. of buffer length */
  170. unsigned short mblength; /* actual number of bytes received */
  171. };
  172. struct lance_tx_desc {
  173. unsigned short tmd0; /* low address of packet */
  174. unsigned short tmd1; /* high address of packet
  175. and descriptor bits */
  176. short length; /* 2s complement (negative!)
  177. of buffer length */
  178. unsigned short misc;
  179. };
  180. /* First part of the LANCE initialization block, described in databook. */
  181. struct lance_init_block {
  182. unsigned short mode; /* pre-set mode (reg. 15) */
  183. unsigned short phys_addr[3]; /* physical ethernet address */
  184. unsigned short filter[4]; /* multicast filter */
  185. /* Receive and transmit ring base, along with extra bits. */
  186. unsigned short rx_ptr; /* receive descriptor addr */
  187. unsigned short rx_len; /* receive len and high addr */
  188. unsigned short tx_ptr; /* transmit descriptor addr */
  189. unsigned short tx_len; /* transmit len and high addr */
  190. short gap[4];
  191. /* The buffer descriptors */
  192. struct lance_rx_desc brx_ring[RX_RING_SIZE];
  193. struct lance_tx_desc btx_ring[TX_RING_SIZE];
  194. };
  195. #define BUF_OFFSET_CPU sizeof(struct lance_init_block)
  196. #define BUF_OFFSET_LNC sizeof(struct lance_init_block)
  197. #define shift_off(off, type) \
  198. (type == ASIC_LANCE || type == PMAX_LANCE ? off << 1 : off)
  199. #define lib_off(rt, type) \
  200. shift_off(offsetof(struct lance_init_block, rt), type)
  201. #define lib_ptr(ib, rt, type) \
  202. ((volatile u16 *)((u8 *)(ib) + lib_off(rt, type)))
  203. #define rds_off(rt, type) \
  204. shift_off(offsetof(struct lance_rx_desc, rt), type)
  205. #define rds_ptr(rd, rt, type) \
  206. ((volatile u16 *)((u8 *)(rd) + rds_off(rt, type)))
  207. #define tds_off(rt, type) \
  208. shift_off(offsetof(struct lance_tx_desc, rt), type)
  209. #define tds_ptr(td, rt, type) \
  210. ((volatile u16 *)((u8 *)(td) + tds_off(rt, type)))
  211. struct lance_private {
  212. struct net_device *next;
  213. int type;
  214. int dma_irq;
  215. volatile struct lance_regs *ll;
  216. spinlock_t lock;
  217. int rx_new, tx_new;
  218. int rx_old, tx_old;
  219. struct net_device_stats stats;
  220. unsigned short busmaster_regval;
  221. struct timer_list multicast_timer;
  222. /* Pointers to the ring buffers as seen from the CPU */
  223. char *rx_buf_ptr_cpu[RX_RING_SIZE];
  224. char *tx_buf_ptr_cpu[TX_RING_SIZE];
  225. /* Pointers to the ring buffers as seen from the LANCE */
  226. uint rx_buf_ptr_lnc[RX_RING_SIZE];
  227. uint tx_buf_ptr_lnc[TX_RING_SIZE];
  228. };
  229. #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
  230. lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
  231. lp->tx_old - lp->tx_new-1)
  232. /* The lance control ports are at an absolute address, machine and tc-slot
  233. * dependent.
  234. * DECstations do only 32-bit access and the LANCE uses 16 bit addresses,
  235. * so we have to give the structure an extra member making rap pointing
  236. * at the right address
  237. */
  238. struct lance_regs {
  239. volatile unsigned short rdp; /* register data port */
  240. unsigned short pad;
  241. volatile unsigned short rap; /* register address port */
  242. };
  243. int dec_lance_debug = 2;
  244. static struct tc_driver dec_lance_tc_driver;
  245. static struct net_device *root_lance_dev;
  246. static inline void writereg(volatile unsigned short *regptr, short value)
  247. {
  248. *regptr = value;
  249. iob();
  250. }
  251. /* Load the CSR registers */
  252. static void load_csrs(struct lance_private *lp)
  253. {
  254. volatile struct lance_regs *ll = lp->ll;
  255. uint leptr;
  256. /* The address space as seen from the LANCE
  257. * begins at address 0. HK
  258. */
  259. leptr = 0;
  260. writereg(&ll->rap, LE_CSR1);
  261. writereg(&ll->rdp, (leptr & 0xFFFF));
  262. writereg(&ll->rap, LE_CSR2);
  263. writereg(&ll->rdp, leptr >> 16);
  264. writereg(&ll->rap, LE_CSR3);
  265. writereg(&ll->rdp, lp->busmaster_regval);
  266. /* Point back to csr0 */
  267. writereg(&ll->rap, LE_CSR0);
  268. }
  269. /*
  270. * Our specialized copy routines
  271. *
  272. */
  273. static void cp_to_buf(const int type, void *to, const void *from, int len)
  274. {
  275. unsigned short *tp, *fp, clen;
  276. unsigned char *rtp, *rfp;
  277. if (type == PMAD_LANCE) {
  278. memcpy(to, from, len);
  279. } else if (type == PMAX_LANCE) {
  280. clen = len >> 1;
  281. tp = (unsigned short *) to;
  282. fp = (unsigned short *) from;
  283. while (clen--) {
  284. *tp++ = *fp++;
  285. tp++;
  286. }
  287. clen = len & 1;
  288. rtp = (unsigned char *) tp;
  289. rfp = (unsigned char *) fp;
  290. while (clen--) {
  291. *rtp++ = *rfp++;
  292. }
  293. } else {
  294. /*
  295. * copy 16 Byte chunks
  296. */
  297. clen = len >> 4;
  298. tp = (unsigned short *) to;
  299. fp = (unsigned short *) from;
  300. while (clen--) {
  301. *tp++ = *fp++;
  302. *tp++ = *fp++;
  303. *tp++ = *fp++;
  304. *tp++ = *fp++;
  305. *tp++ = *fp++;
  306. *tp++ = *fp++;
  307. *tp++ = *fp++;
  308. *tp++ = *fp++;
  309. tp += 8;
  310. }
  311. /*
  312. * do the rest, if any.
  313. */
  314. clen = len & 15;
  315. rtp = (unsigned char *) tp;
  316. rfp = (unsigned char *) fp;
  317. while (clen--) {
  318. *rtp++ = *rfp++;
  319. }
  320. }
  321. iob();
  322. }
  323. static void cp_from_buf(const int type, void *to, const void *from, int len)
  324. {
  325. unsigned short *tp, *fp, clen;
  326. unsigned char *rtp, *rfp;
  327. if (type == PMAD_LANCE) {
  328. memcpy(to, from, len);
  329. } else if (type == PMAX_LANCE) {
  330. clen = len >> 1;
  331. tp = (unsigned short *) to;
  332. fp = (unsigned short *) from;
  333. while (clen--) {
  334. *tp++ = *fp++;
  335. fp++;
  336. }
  337. clen = len & 1;
  338. rtp = (unsigned char *) tp;
  339. rfp = (unsigned char *) fp;
  340. while (clen--) {
  341. *rtp++ = *rfp++;
  342. }
  343. } else {
  344. /*
  345. * copy 16 Byte chunks
  346. */
  347. clen = len >> 4;
  348. tp = (unsigned short *) to;
  349. fp = (unsigned short *) from;
  350. while (clen--) {
  351. *tp++ = *fp++;
  352. *tp++ = *fp++;
  353. *tp++ = *fp++;
  354. *tp++ = *fp++;
  355. *tp++ = *fp++;
  356. *tp++ = *fp++;
  357. *tp++ = *fp++;
  358. *tp++ = *fp++;
  359. fp += 8;
  360. }
  361. /*
  362. * do the rest, if any.
  363. */
  364. clen = len & 15;
  365. rtp = (unsigned char *) tp;
  366. rfp = (unsigned char *) fp;
  367. while (clen--) {
  368. *rtp++ = *rfp++;
  369. }
  370. }
  371. }
  372. /* Setup the Lance Rx and Tx rings */
  373. static void lance_init_ring(struct net_device *dev)
  374. {
  375. struct lance_private *lp = netdev_priv(dev);
  376. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  377. uint leptr;
  378. int i;
  379. /* Lock out other processes while setting up hardware */
  380. netif_stop_queue(dev);
  381. lp->rx_new = lp->tx_new = 0;
  382. lp->rx_old = lp->tx_old = 0;
  383. /* Copy the ethernet address to the lance init block.
  384. * XXX bit 0 of the physical address registers has to be zero
  385. */
  386. *lib_ptr(ib, phys_addr[0], lp->type) = (dev->dev_addr[1] << 8) |
  387. dev->dev_addr[0];
  388. *lib_ptr(ib, phys_addr[1], lp->type) = (dev->dev_addr[3] << 8) |
  389. dev->dev_addr[2];
  390. *lib_ptr(ib, phys_addr[2], lp->type) = (dev->dev_addr[5] << 8) |
  391. dev->dev_addr[4];
  392. /* Setup the initialization block */
  393. /* Setup rx descriptor pointer */
  394. leptr = offsetof(struct lance_init_block, brx_ring);
  395. *lib_ptr(ib, rx_len, lp->type) = (LANCE_LOG_RX_BUFFERS << 13) |
  396. (leptr >> 16);
  397. *lib_ptr(ib, rx_ptr, lp->type) = leptr;
  398. if (ZERO)
  399. printk("RX ptr: %8.8x(%8.8x)\n",
  400. leptr, lib_off(brx_ring, lp->type));
  401. /* Setup tx descriptor pointer */
  402. leptr = offsetof(struct lance_init_block, btx_ring);
  403. *lib_ptr(ib, tx_len, lp->type) = (LANCE_LOG_TX_BUFFERS << 13) |
  404. (leptr >> 16);
  405. *lib_ptr(ib, tx_ptr, lp->type) = leptr;
  406. if (ZERO)
  407. printk("TX ptr: %8.8x(%8.8x)\n",
  408. leptr, lib_off(btx_ring, lp->type));
  409. if (ZERO)
  410. printk("TX rings:\n");
  411. /* Setup the Tx ring entries */
  412. for (i = 0; i < TX_RING_SIZE; i++) {
  413. leptr = lp->tx_buf_ptr_lnc[i];
  414. *lib_ptr(ib, btx_ring[i].tmd0, lp->type) = leptr;
  415. *lib_ptr(ib, btx_ring[i].tmd1, lp->type) = (leptr >> 16) &
  416. 0xff;
  417. *lib_ptr(ib, btx_ring[i].length, lp->type) = 0xf000;
  418. /* The ones required by tmd2 */
  419. *lib_ptr(ib, btx_ring[i].misc, lp->type) = 0;
  420. if (i < 3 && ZERO)
  421. printk("%d: 0x%8.8x(0x%8.8x)\n",
  422. i, leptr, (uint)lp->tx_buf_ptr_cpu[i]);
  423. }
  424. /* Setup the Rx ring entries */
  425. if (ZERO)
  426. printk("RX rings:\n");
  427. for (i = 0; i < RX_RING_SIZE; i++) {
  428. leptr = lp->rx_buf_ptr_lnc[i];
  429. *lib_ptr(ib, brx_ring[i].rmd0, lp->type) = leptr;
  430. *lib_ptr(ib, brx_ring[i].rmd1, lp->type) = ((leptr >> 16) &
  431. 0xff) |
  432. LE_R1_OWN;
  433. *lib_ptr(ib, brx_ring[i].length, lp->type) = -RX_BUFF_SIZE |
  434. 0xf000;
  435. *lib_ptr(ib, brx_ring[i].mblength, lp->type) = 0;
  436. if (i < 3 && ZERO)
  437. printk("%d: 0x%8.8x(0x%8.8x)\n",
  438. i, leptr, (uint)lp->rx_buf_ptr_cpu[i]);
  439. }
  440. iob();
  441. }
  442. static int init_restart_lance(struct lance_private *lp)
  443. {
  444. volatile struct lance_regs *ll = lp->ll;
  445. int i;
  446. writereg(&ll->rap, LE_CSR0);
  447. writereg(&ll->rdp, LE_C0_INIT);
  448. /* Wait for the lance to complete initialization */
  449. for (i = 0; (i < 100) && !(ll->rdp & LE_C0_IDON); i++) {
  450. udelay(10);
  451. }
  452. if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
  453. printk("LANCE unopened after %d ticks, csr0=%4.4x.\n",
  454. i, ll->rdp);
  455. return -1;
  456. }
  457. if ((ll->rdp & LE_C0_ERR)) {
  458. printk("LANCE unopened after %d ticks, csr0=%4.4x.\n",
  459. i, ll->rdp);
  460. return -1;
  461. }
  462. writereg(&ll->rdp, LE_C0_IDON);
  463. writereg(&ll->rdp, LE_C0_STRT);
  464. writereg(&ll->rdp, LE_C0_INEA);
  465. return 0;
  466. }
  467. static int lance_rx(struct net_device *dev)
  468. {
  469. struct lance_private *lp = netdev_priv(dev);
  470. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  471. volatile u16 *rd;
  472. unsigned short bits;
  473. int entry, len;
  474. struct sk_buff *skb;
  475. #ifdef TEST_HITS
  476. {
  477. int i;
  478. printk("[");
  479. for (i = 0; i < RX_RING_SIZE; i++) {
  480. if (i == lp->rx_new)
  481. printk("%s", *lib_ptr(ib, brx_ring[i].rmd1,
  482. lp->type) &
  483. LE_R1_OWN ? "_" : "X");
  484. else
  485. printk("%s", *lib_ptr(ib, brx_ring[i].rmd1,
  486. lp->type) &
  487. LE_R1_OWN ? "." : "1");
  488. }
  489. printk("]");
  490. }
  491. #endif
  492. for (rd = lib_ptr(ib, brx_ring[lp->rx_new], lp->type);
  493. !((bits = *rds_ptr(rd, rmd1, lp->type)) & LE_R1_OWN);
  494. rd = lib_ptr(ib, brx_ring[lp->rx_new], lp->type)) {
  495. entry = lp->rx_new;
  496. /* We got an incomplete frame? */
  497. if ((bits & LE_R1_POK) != LE_R1_POK) {
  498. lp->stats.rx_over_errors++;
  499. lp->stats.rx_errors++;
  500. } else if (bits & LE_R1_ERR) {
  501. /* Count only the end frame as a rx error,
  502. * not the beginning
  503. */
  504. if (bits & LE_R1_BUF)
  505. lp->stats.rx_fifo_errors++;
  506. if (bits & LE_R1_CRC)
  507. lp->stats.rx_crc_errors++;
  508. if (bits & LE_R1_OFL)
  509. lp->stats.rx_over_errors++;
  510. if (bits & LE_R1_FRA)
  511. lp->stats.rx_frame_errors++;
  512. if (bits & LE_R1_EOP)
  513. lp->stats.rx_errors++;
  514. } else {
  515. len = (*rds_ptr(rd, mblength, lp->type) & 0xfff) - 4;
  516. skb = dev_alloc_skb(len + 2);
  517. if (skb == 0) {
  518. printk("%s: Memory squeeze, deferring packet.\n",
  519. dev->name);
  520. lp->stats.rx_dropped++;
  521. *rds_ptr(rd, mblength, lp->type) = 0;
  522. *rds_ptr(rd, rmd1, lp->type) =
  523. ((lp->rx_buf_ptr_lnc[entry] >> 16) &
  524. 0xff) | LE_R1_OWN;
  525. lp->rx_new = (entry + 1) & RX_RING_MOD_MASK;
  526. return 0;
  527. }
  528. lp->stats.rx_bytes += len;
  529. skb_reserve(skb, 2); /* 16 byte align */
  530. skb_put(skb, len); /* make room */
  531. cp_from_buf(lp->type, skb->data,
  532. (char *)lp->rx_buf_ptr_cpu[entry], len);
  533. skb->protocol = eth_type_trans(skb, dev);
  534. netif_rx(skb);
  535. dev->last_rx = jiffies;
  536. lp->stats.rx_packets++;
  537. }
  538. /* Return the packet to the pool */
  539. *rds_ptr(rd, mblength, lp->type) = 0;
  540. *rds_ptr(rd, length, lp->type) = -RX_BUFF_SIZE | 0xf000;
  541. *rds_ptr(rd, rmd1, lp->type) =
  542. ((lp->rx_buf_ptr_lnc[entry] >> 16) & 0xff) | LE_R1_OWN;
  543. lp->rx_new = (entry + 1) & RX_RING_MOD_MASK;
  544. }
  545. return 0;
  546. }
  547. static void lance_tx(struct net_device *dev)
  548. {
  549. struct lance_private *lp = netdev_priv(dev);
  550. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  551. volatile struct lance_regs *ll = lp->ll;
  552. volatile u16 *td;
  553. int i, j;
  554. int status;
  555. j = lp->tx_old;
  556. spin_lock(&lp->lock);
  557. for (i = j; i != lp->tx_new; i = j) {
  558. td = lib_ptr(ib, btx_ring[i], lp->type);
  559. /* If we hit a packet not owned by us, stop */
  560. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_OWN)
  561. break;
  562. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_ERR) {
  563. status = *tds_ptr(td, misc, lp->type);
  564. lp->stats.tx_errors++;
  565. if (status & LE_T3_RTY)
  566. lp->stats.tx_aborted_errors++;
  567. if (status & LE_T3_LCOL)
  568. lp->stats.tx_window_errors++;
  569. if (status & LE_T3_CLOS) {
  570. lp->stats.tx_carrier_errors++;
  571. printk("%s: Carrier Lost\n", dev->name);
  572. /* Stop the lance */
  573. writereg(&ll->rap, LE_CSR0);
  574. writereg(&ll->rdp, LE_C0_STOP);
  575. lance_init_ring(dev);
  576. load_csrs(lp);
  577. init_restart_lance(lp);
  578. goto out;
  579. }
  580. /* Buffer errors and underflows turn off the
  581. * transmitter, restart the adapter.
  582. */
  583. if (status & (LE_T3_BUF | LE_T3_UFL)) {
  584. lp->stats.tx_fifo_errors++;
  585. printk("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
  586. dev->name);
  587. /* Stop the lance */
  588. writereg(&ll->rap, LE_CSR0);
  589. writereg(&ll->rdp, LE_C0_STOP);
  590. lance_init_ring(dev);
  591. load_csrs(lp);
  592. init_restart_lance(lp);
  593. goto out;
  594. }
  595. } else if ((*tds_ptr(td, tmd1, lp->type) & LE_T1_POK) ==
  596. LE_T1_POK) {
  597. /*
  598. * So we don't count the packet more than once.
  599. */
  600. *tds_ptr(td, tmd1, lp->type) &= ~(LE_T1_POK);
  601. /* One collision before packet was sent. */
  602. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_EONE)
  603. lp->stats.collisions++;
  604. /* More than one collision, be optimistic. */
  605. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_EMORE)
  606. lp->stats.collisions += 2;
  607. lp->stats.tx_packets++;
  608. }
  609. j = (j + 1) & TX_RING_MOD_MASK;
  610. }
  611. lp->tx_old = j;
  612. out:
  613. if (netif_queue_stopped(dev) &&
  614. TX_BUFFS_AVAIL > 0)
  615. netif_wake_queue(dev);
  616. spin_unlock(&lp->lock);
  617. }
  618. static irqreturn_t lance_dma_merr_int(const int irq, void *dev_id)
  619. {
  620. struct net_device *dev = dev_id;
  621. printk("%s: DMA error\n", dev->name);
  622. return IRQ_HANDLED;
  623. }
  624. static irqreturn_t lance_interrupt(const int irq, void *dev_id)
  625. {
  626. struct net_device *dev = dev_id;
  627. struct lance_private *lp = netdev_priv(dev);
  628. volatile struct lance_regs *ll = lp->ll;
  629. int csr0;
  630. writereg(&ll->rap, LE_CSR0);
  631. csr0 = ll->rdp;
  632. /* Acknowledge all the interrupt sources ASAP */
  633. writereg(&ll->rdp, csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT));
  634. if ((csr0 & LE_C0_ERR)) {
  635. /* Clear the error condition */
  636. writereg(&ll->rdp, LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
  637. LE_C0_CERR | LE_C0_MERR);
  638. }
  639. if (csr0 & LE_C0_RINT)
  640. lance_rx(dev);
  641. if (csr0 & LE_C0_TINT)
  642. lance_tx(dev);
  643. if (csr0 & LE_C0_BABL)
  644. lp->stats.tx_errors++;
  645. if (csr0 & LE_C0_MISS)
  646. lp->stats.rx_errors++;
  647. if (csr0 & LE_C0_MERR) {
  648. printk("%s: Memory error, status %04x\n", dev->name, csr0);
  649. writereg(&ll->rdp, LE_C0_STOP);
  650. lance_init_ring(dev);
  651. load_csrs(lp);
  652. init_restart_lance(lp);
  653. netif_wake_queue(dev);
  654. }
  655. writereg(&ll->rdp, LE_C0_INEA);
  656. writereg(&ll->rdp, LE_C0_INEA);
  657. return IRQ_HANDLED;
  658. }
  659. struct net_device *last_dev = 0;
  660. static int lance_open(struct net_device *dev)
  661. {
  662. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  663. struct lance_private *lp = netdev_priv(dev);
  664. volatile struct lance_regs *ll = lp->ll;
  665. int status = 0;
  666. last_dev = dev;
  667. /* Stop the Lance */
  668. writereg(&ll->rap, LE_CSR0);
  669. writereg(&ll->rdp, LE_C0_STOP);
  670. /* Set mode and clear multicast filter only at device open,
  671. * so that lance_init_ring() called at any error will not
  672. * forget multicast filters.
  673. *
  674. * BTW it is common bug in all lance drivers! --ANK
  675. */
  676. *lib_ptr(ib, mode, lp->type) = 0;
  677. *lib_ptr(ib, filter[0], lp->type) = 0;
  678. *lib_ptr(ib, filter[1], lp->type) = 0;
  679. *lib_ptr(ib, filter[2], lp->type) = 0;
  680. *lib_ptr(ib, filter[3], lp->type) = 0;
  681. lance_init_ring(dev);
  682. load_csrs(lp);
  683. netif_start_queue(dev);
  684. /* Associate IRQ with lance_interrupt */
  685. if (request_irq(dev->irq, &lance_interrupt, 0, "lance", dev)) {
  686. printk("%s: Can't get IRQ %d\n", dev->name, dev->irq);
  687. return -EAGAIN;
  688. }
  689. if (lp->dma_irq >= 0) {
  690. unsigned long flags;
  691. if (request_irq(lp->dma_irq, &lance_dma_merr_int, 0,
  692. "lance error", dev)) {
  693. free_irq(dev->irq, dev);
  694. printk("%s: Can't get DMA IRQ %d\n", dev->name,
  695. lp->dma_irq);
  696. return -EAGAIN;
  697. }
  698. spin_lock_irqsave(&ioasic_ssr_lock, flags);
  699. fast_mb();
  700. /* Enable I/O ASIC LANCE DMA. */
  701. ioasic_write(IO_REG_SSR,
  702. ioasic_read(IO_REG_SSR) | IO_SSR_LANCE_DMA_EN);
  703. fast_mb();
  704. spin_unlock_irqrestore(&ioasic_ssr_lock, flags);
  705. }
  706. status = init_restart_lance(lp);
  707. return status;
  708. }
  709. static int lance_close(struct net_device *dev)
  710. {
  711. struct lance_private *lp = netdev_priv(dev);
  712. volatile struct lance_regs *ll = lp->ll;
  713. netif_stop_queue(dev);
  714. del_timer_sync(&lp->multicast_timer);
  715. /* Stop the card */
  716. writereg(&ll->rap, LE_CSR0);
  717. writereg(&ll->rdp, LE_C0_STOP);
  718. if (lp->dma_irq >= 0) {
  719. unsigned long flags;
  720. spin_lock_irqsave(&ioasic_ssr_lock, flags);
  721. fast_mb();
  722. /* Disable I/O ASIC LANCE DMA. */
  723. ioasic_write(IO_REG_SSR,
  724. ioasic_read(IO_REG_SSR) & ~IO_SSR_LANCE_DMA_EN);
  725. fast_iob();
  726. spin_unlock_irqrestore(&ioasic_ssr_lock, flags);
  727. free_irq(lp->dma_irq, dev);
  728. }
  729. free_irq(dev->irq, dev);
  730. return 0;
  731. }
  732. static inline int lance_reset(struct net_device *dev)
  733. {
  734. struct lance_private *lp = netdev_priv(dev);
  735. volatile struct lance_regs *ll = lp->ll;
  736. int status;
  737. /* Stop the lance */
  738. writereg(&ll->rap, LE_CSR0);
  739. writereg(&ll->rdp, LE_C0_STOP);
  740. lance_init_ring(dev);
  741. load_csrs(lp);
  742. dev->trans_start = jiffies;
  743. status = init_restart_lance(lp);
  744. return status;
  745. }
  746. static void lance_tx_timeout(struct net_device *dev)
  747. {
  748. struct lance_private *lp = netdev_priv(dev);
  749. volatile struct lance_regs *ll = lp->ll;
  750. printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
  751. dev->name, ll->rdp);
  752. lance_reset(dev);
  753. netif_wake_queue(dev);
  754. }
  755. static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
  756. {
  757. struct lance_private *lp = netdev_priv(dev);
  758. volatile struct lance_regs *ll = lp->ll;
  759. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  760. int entry, len;
  761. len = skb->len;
  762. if (len < ETH_ZLEN) {
  763. if (skb_padto(skb, ETH_ZLEN))
  764. return 0;
  765. len = ETH_ZLEN;
  766. }
  767. lp->stats.tx_bytes += len;
  768. entry = lp->tx_new;
  769. *lib_ptr(ib, btx_ring[entry].length, lp->type) = (-len);
  770. *lib_ptr(ib, btx_ring[entry].misc, lp->type) = 0;
  771. cp_to_buf(lp->type, (char *)lp->tx_buf_ptr_cpu[entry], skb->data, len);
  772. /* Now, give the packet to the lance */
  773. *lib_ptr(ib, btx_ring[entry].tmd1, lp->type) =
  774. ((lp->tx_buf_ptr_lnc[entry] >> 16) & 0xff) |
  775. (LE_T1_POK | LE_T1_OWN);
  776. lp->tx_new = (entry + 1) & TX_RING_MOD_MASK;
  777. if (TX_BUFFS_AVAIL <= 0)
  778. netif_stop_queue(dev);
  779. /* Kick the lance: transmit now */
  780. writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
  781. spin_unlock_irq(&lp->lock);
  782. dev->trans_start = jiffies;
  783. dev_kfree_skb(skb);
  784. return 0;
  785. }
  786. static struct net_device_stats *lance_get_stats(struct net_device *dev)
  787. {
  788. struct lance_private *lp = netdev_priv(dev);
  789. return &lp->stats;
  790. }
  791. static void lance_load_multicast(struct net_device *dev)
  792. {
  793. struct lance_private *lp = netdev_priv(dev);
  794. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  795. struct dev_mc_list *dmi = dev->mc_list;
  796. char *addrs;
  797. int i;
  798. u32 crc;
  799. /* set all multicast bits */
  800. if (dev->flags & IFF_ALLMULTI) {
  801. *lib_ptr(ib, filter[0], lp->type) = 0xffff;
  802. *lib_ptr(ib, filter[1], lp->type) = 0xffff;
  803. *lib_ptr(ib, filter[2], lp->type) = 0xffff;
  804. *lib_ptr(ib, filter[3], lp->type) = 0xffff;
  805. return;
  806. }
  807. /* clear the multicast filter */
  808. *lib_ptr(ib, filter[0], lp->type) = 0;
  809. *lib_ptr(ib, filter[1], lp->type) = 0;
  810. *lib_ptr(ib, filter[2], lp->type) = 0;
  811. *lib_ptr(ib, filter[3], lp->type) = 0;
  812. /* Add addresses */
  813. for (i = 0; i < dev->mc_count; i++) {
  814. addrs = dmi->dmi_addr;
  815. dmi = dmi->next;
  816. /* multicast address? */
  817. if (!(*addrs & 1))
  818. continue;
  819. crc = ether_crc_le(ETH_ALEN, addrs);
  820. crc = crc >> 26;
  821. *lib_ptr(ib, filter[crc >> 4], lp->type) |= 1 << (crc & 0xf);
  822. }
  823. return;
  824. }
  825. static void lance_set_multicast(struct net_device *dev)
  826. {
  827. struct lance_private *lp = netdev_priv(dev);
  828. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  829. volatile struct lance_regs *ll = lp->ll;
  830. if (!netif_running(dev))
  831. return;
  832. if (lp->tx_old != lp->tx_new) {
  833. mod_timer(&lp->multicast_timer, jiffies + 4 * HZ/100);
  834. netif_wake_queue(dev);
  835. return;
  836. }
  837. netif_stop_queue(dev);
  838. writereg(&ll->rap, LE_CSR0);
  839. writereg(&ll->rdp, LE_C0_STOP);
  840. lance_init_ring(dev);
  841. if (dev->flags & IFF_PROMISC) {
  842. *lib_ptr(ib, mode, lp->type) |= LE_MO_PROM;
  843. } else {
  844. *lib_ptr(ib, mode, lp->type) &= ~LE_MO_PROM;
  845. lance_load_multicast(dev);
  846. }
  847. load_csrs(lp);
  848. init_restart_lance(lp);
  849. netif_wake_queue(dev);
  850. }
  851. static void lance_set_multicast_retry(unsigned long _opaque)
  852. {
  853. struct net_device *dev = (struct net_device *) _opaque;
  854. lance_set_multicast(dev);
  855. }
  856. static int __init dec_lance_probe(struct device *bdev, const int type)
  857. {
  858. static unsigned version_printed;
  859. static const char fmt[] = "declance%d";
  860. char name[10];
  861. struct net_device *dev;
  862. struct lance_private *lp;
  863. volatile struct lance_regs *ll;
  864. resource_size_t start = 0, len = 0;
  865. int i, ret;
  866. unsigned long esar_base;
  867. unsigned char *esar;
  868. if (dec_lance_debug && version_printed++ == 0)
  869. printk(version);
  870. if (bdev)
  871. snprintf(name, sizeof(name), "%s", bdev->bus_id);
  872. else {
  873. i = 0;
  874. dev = root_lance_dev;
  875. while (dev) {
  876. i++;
  877. lp = (struct lance_private *)dev->priv;
  878. dev = lp->next;
  879. }
  880. snprintf(name, sizeof(name), fmt, i);
  881. }
  882. dev = alloc_etherdev(sizeof(struct lance_private));
  883. if (!dev) {
  884. printk(KERN_ERR "%s: Unable to allocate etherdev, aborting.\n",
  885. name);
  886. ret = -ENOMEM;
  887. goto err_out;
  888. }
  889. /*
  890. * alloc_etherdev ensures the data structures used by the LANCE
  891. * are aligned.
  892. */
  893. lp = netdev_priv(dev);
  894. spin_lock_init(&lp->lock);
  895. lp->type = type;
  896. switch (type) {
  897. case ASIC_LANCE:
  898. dev->base_addr = CKSEG1ADDR(dec_kn_slot_base + IOASIC_LANCE);
  899. /* buffer space for the on-board LANCE shared memory */
  900. /*
  901. * FIXME: ugly hack!
  902. */
  903. dev->mem_start = CKSEG1ADDR(0x00020000);
  904. dev->mem_end = dev->mem_start + 0x00020000;
  905. dev->irq = dec_interrupt[DEC_IRQ_LANCE];
  906. esar_base = CKSEG1ADDR(dec_kn_slot_base + IOASIC_ESAR);
  907. /* Workaround crash with booting KN04 2.1k from Disk */
  908. memset((void *)dev->mem_start, 0,
  909. dev->mem_end - dev->mem_start);
  910. /*
  911. * setup the pointer arrays, this sucks [tm] :-(
  912. */
  913. for (i = 0; i < RX_RING_SIZE; i++) {
  914. lp->rx_buf_ptr_cpu[i] =
  915. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  916. 2 * i * RX_BUFF_SIZE);
  917. lp->rx_buf_ptr_lnc[i] =
  918. (BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  919. }
  920. for (i = 0; i < TX_RING_SIZE; i++) {
  921. lp->tx_buf_ptr_cpu[i] =
  922. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  923. 2 * RX_RING_SIZE * RX_BUFF_SIZE +
  924. 2 * i * TX_BUFF_SIZE);
  925. lp->tx_buf_ptr_lnc[i] =
  926. (BUF_OFFSET_LNC +
  927. RX_RING_SIZE * RX_BUFF_SIZE +
  928. i * TX_BUFF_SIZE);
  929. }
  930. /* Setup I/O ASIC LANCE DMA. */
  931. lp->dma_irq = dec_interrupt[DEC_IRQ_LANCE_MERR];
  932. ioasic_write(IO_REG_LANCE_DMA_P,
  933. CPHYSADDR(dev->mem_start) << 3);
  934. break;
  935. #ifdef CONFIG_TC
  936. case PMAD_LANCE:
  937. dev_set_drvdata(bdev, dev);
  938. start = to_tc_dev(bdev)->resource.start;
  939. len = to_tc_dev(bdev)->resource.end - start + 1;
  940. if (!request_mem_region(start, len, bdev->bus_id)) {
  941. printk(KERN_ERR
  942. "%s: Unable to reserve MMIO resource\n",
  943. bdev->bus_id);
  944. ret = -EBUSY;
  945. goto err_out_dev;
  946. }
  947. dev->mem_start = CKSEG1ADDR(start);
  948. dev->mem_end = dev->mem_start + 0x100000;
  949. dev->base_addr = dev->mem_start + 0x100000;
  950. dev->irq = to_tc_dev(bdev)->interrupt;
  951. esar_base = dev->mem_start + 0x1c0002;
  952. lp->dma_irq = -1;
  953. for (i = 0; i < RX_RING_SIZE; i++) {
  954. lp->rx_buf_ptr_cpu[i] =
  955. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  956. i * RX_BUFF_SIZE);
  957. lp->rx_buf_ptr_lnc[i] =
  958. (BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  959. }
  960. for (i = 0; i < TX_RING_SIZE; i++) {
  961. lp->tx_buf_ptr_cpu[i] =
  962. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  963. RX_RING_SIZE * RX_BUFF_SIZE +
  964. i * TX_BUFF_SIZE);
  965. lp->tx_buf_ptr_lnc[i] =
  966. (BUF_OFFSET_LNC +
  967. RX_RING_SIZE * RX_BUFF_SIZE +
  968. i * TX_BUFF_SIZE);
  969. }
  970. break;
  971. #endif
  972. case PMAX_LANCE:
  973. dev->irq = dec_interrupt[DEC_IRQ_LANCE];
  974. dev->base_addr = CKSEG1ADDR(KN01_SLOT_BASE + KN01_LANCE);
  975. dev->mem_start = CKSEG1ADDR(KN01_SLOT_BASE + KN01_LANCE_MEM);
  976. dev->mem_end = dev->mem_start + KN01_SLOT_SIZE;
  977. esar_base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_ESAR + 1);
  978. lp->dma_irq = -1;
  979. /*
  980. * setup the pointer arrays, this sucks [tm] :-(
  981. */
  982. for (i = 0; i < RX_RING_SIZE; i++) {
  983. lp->rx_buf_ptr_cpu[i] =
  984. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  985. 2 * i * RX_BUFF_SIZE);
  986. lp->rx_buf_ptr_lnc[i] =
  987. (BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  988. }
  989. for (i = 0; i < TX_RING_SIZE; i++) {
  990. lp->tx_buf_ptr_cpu[i] =
  991. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  992. 2 * RX_RING_SIZE * RX_BUFF_SIZE +
  993. 2 * i * TX_BUFF_SIZE);
  994. lp->tx_buf_ptr_lnc[i] =
  995. (BUF_OFFSET_LNC +
  996. RX_RING_SIZE * RX_BUFF_SIZE +
  997. i * TX_BUFF_SIZE);
  998. }
  999. break;
  1000. default:
  1001. printk(KERN_ERR "%s: declance_init called with unknown type\n",
  1002. name);
  1003. ret = -ENODEV;
  1004. goto err_out_dev;
  1005. }
  1006. ll = (struct lance_regs *) dev->base_addr;
  1007. esar = (unsigned char *) esar_base;
  1008. /* prom checks */
  1009. /* First, check for test pattern */
  1010. if (esar[0x60] != 0xff && esar[0x64] != 0x00 &&
  1011. esar[0x68] != 0x55 && esar[0x6c] != 0xaa) {
  1012. printk(KERN_ERR
  1013. "%s: Ethernet station address prom not found!\n",
  1014. name);
  1015. ret = -ENODEV;
  1016. goto err_out_resource;
  1017. }
  1018. /* Check the prom contents */
  1019. for (i = 0; i < 8; i++) {
  1020. if (esar[i * 4] != esar[0x3c - i * 4] &&
  1021. esar[i * 4] != esar[0x40 + i * 4] &&
  1022. esar[0x3c - i * 4] != esar[0x40 + i * 4]) {
  1023. printk(KERN_ERR "%s: Something is wrong with the "
  1024. "ethernet station address prom!\n", name);
  1025. ret = -ENODEV;
  1026. goto err_out_resource;
  1027. }
  1028. }
  1029. /* Copy the ethernet address to the device structure, later to the
  1030. * lance initialization block so the lance gets it every time it's
  1031. * (re)initialized.
  1032. */
  1033. switch (type) {
  1034. case ASIC_LANCE:
  1035. printk("%s: IOASIC onboard LANCE, addr = ", name);
  1036. break;
  1037. case PMAD_LANCE:
  1038. printk("%s: PMAD-AA, addr = ", name);
  1039. break;
  1040. case PMAX_LANCE:
  1041. printk("%s: PMAX onboard LANCE, addr = ", name);
  1042. break;
  1043. }
  1044. for (i = 0; i < 6; i++) {
  1045. dev->dev_addr[i] = esar[i * 4];
  1046. printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ',' : ':');
  1047. }
  1048. printk(" irq = %d\n", dev->irq);
  1049. dev->open = &lance_open;
  1050. dev->stop = &lance_close;
  1051. dev->hard_start_xmit = &lance_start_xmit;
  1052. dev->tx_timeout = &lance_tx_timeout;
  1053. dev->watchdog_timeo = 5*HZ;
  1054. dev->get_stats = &lance_get_stats;
  1055. dev->set_multicast_list = &lance_set_multicast;
  1056. /* lp->ll is the location of the registers for lance card */
  1057. lp->ll = ll;
  1058. /* busmaster_regval (CSR3) should be zero according to the PMAD-AA
  1059. * specification.
  1060. */
  1061. lp->busmaster_regval = 0;
  1062. dev->dma = 0;
  1063. /* We cannot sleep if the chip is busy during a
  1064. * multicast list update event, because such events
  1065. * can occur from interrupts (ex. IPv6). So we
  1066. * use a timer to try again later when necessary. -DaveM
  1067. */
  1068. init_timer(&lp->multicast_timer);
  1069. lp->multicast_timer.data = (unsigned long) dev;
  1070. lp->multicast_timer.function = &lance_set_multicast_retry;
  1071. ret = register_netdev(dev);
  1072. if (ret) {
  1073. printk(KERN_ERR
  1074. "%s: Unable to register netdev, aborting.\n", name);
  1075. goto err_out_resource;
  1076. }
  1077. if (!bdev) {
  1078. lp->next = root_lance_dev;
  1079. root_lance_dev = dev;
  1080. }
  1081. printk("%s: registered as %s.\n", name, dev->name);
  1082. return 0;
  1083. err_out_resource:
  1084. if (bdev)
  1085. release_mem_region(start, len);
  1086. err_out_dev:
  1087. free_netdev(dev);
  1088. err_out:
  1089. return ret;
  1090. }
  1091. static void __exit dec_lance_remove(struct device *bdev)
  1092. {
  1093. struct net_device *dev = dev_get_drvdata(bdev);
  1094. resource_size_t start, len;
  1095. unregister_netdev(dev);
  1096. start = to_tc_dev(bdev)->resource.start;
  1097. len = to_tc_dev(bdev)->resource.end - start + 1;
  1098. release_mem_region(start, len);
  1099. free_netdev(dev);
  1100. }
  1101. /* Find all the lance cards on the system and initialize them */
  1102. static int __init dec_lance_platform_probe(void)
  1103. {
  1104. int count = 0;
  1105. if (dec_interrupt[DEC_IRQ_LANCE] >= 0) {
  1106. if (dec_interrupt[DEC_IRQ_LANCE_MERR] >= 0) {
  1107. if (dec_lance_probe(NULL, ASIC_LANCE) >= 0)
  1108. count++;
  1109. } else if (!TURBOCHANNEL) {
  1110. if (dec_lance_probe(NULL, PMAX_LANCE) >= 0)
  1111. count++;
  1112. }
  1113. }
  1114. return (count > 0) ? 0 : -ENODEV;
  1115. }
  1116. static void __exit dec_lance_platform_remove(void)
  1117. {
  1118. while (root_lance_dev) {
  1119. struct net_device *dev = root_lance_dev;
  1120. struct lance_private *lp = netdev_priv(dev);
  1121. unregister_netdev(dev);
  1122. root_lance_dev = lp->next;
  1123. free_netdev(dev);
  1124. }
  1125. }
  1126. #ifdef CONFIG_TC
  1127. static int __init dec_lance_tc_probe(struct device *dev);
  1128. static int __exit dec_lance_tc_remove(struct device *dev);
  1129. static const struct tc_device_id dec_lance_tc_table[] = {
  1130. { "DEC ", "PMAD-AA " },
  1131. { }
  1132. };
  1133. MODULE_DEVICE_TABLE(tc, dec_lance_tc_table);
  1134. static struct tc_driver dec_lance_tc_driver = {
  1135. .id_table = dec_lance_tc_table,
  1136. .driver = {
  1137. .name = "declance",
  1138. .bus = &tc_bus_type,
  1139. .probe = dec_lance_tc_probe,
  1140. .remove = __exit_p(dec_lance_tc_remove),
  1141. },
  1142. };
  1143. static int __init dec_lance_tc_probe(struct device *dev)
  1144. {
  1145. int status = dec_lance_probe(dev, PMAD_LANCE);
  1146. if (!status)
  1147. get_device(dev);
  1148. return status;
  1149. }
  1150. static int __exit dec_lance_tc_remove(struct device *dev)
  1151. {
  1152. put_device(dev);
  1153. dec_lance_remove(dev);
  1154. return 0;
  1155. }
  1156. #endif
  1157. static int __init dec_lance_init(void)
  1158. {
  1159. int status;
  1160. status = tc_register_driver(&dec_lance_tc_driver);
  1161. if (!status)
  1162. dec_lance_platform_probe();
  1163. return status;
  1164. }
  1165. static void __exit dec_lance_exit(void)
  1166. {
  1167. dec_lance_platform_remove();
  1168. tc_unregister_driver(&dec_lance_tc_driver);
  1169. }
  1170. module_init(dec_lance_init);
  1171. module_exit(dec_lance_exit);