declance.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. unsigned short busmaster_regval;
  220. struct timer_list multicast_timer;
  221. /* Pointers to the ring buffers as seen from the CPU */
  222. char *rx_buf_ptr_cpu[RX_RING_SIZE];
  223. char *tx_buf_ptr_cpu[TX_RING_SIZE];
  224. /* Pointers to the ring buffers as seen from the LANCE */
  225. uint rx_buf_ptr_lnc[RX_RING_SIZE];
  226. uint tx_buf_ptr_lnc[TX_RING_SIZE];
  227. };
  228. #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
  229. lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
  230. lp->tx_old - lp->tx_new-1)
  231. /* The lance control ports are at an absolute address, machine and tc-slot
  232. * dependent.
  233. * DECstations do only 32-bit access and the LANCE uses 16 bit addresses,
  234. * so we have to give the structure an extra member making rap pointing
  235. * at the right address
  236. */
  237. struct lance_regs {
  238. volatile unsigned short rdp; /* register data port */
  239. unsigned short pad;
  240. volatile unsigned short rap; /* register address port */
  241. };
  242. int dec_lance_debug = 2;
  243. static struct tc_driver dec_lance_tc_driver;
  244. static struct net_device *root_lance_dev;
  245. static inline void writereg(volatile unsigned short *regptr, short value)
  246. {
  247. *regptr = value;
  248. iob();
  249. }
  250. /* Load the CSR registers */
  251. static void load_csrs(struct lance_private *lp)
  252. {
  253. volatile struct lance_regs *ll = lp->ll;
  254. uint leptr;
  255. /* The address space as seen from the LANCE
  256. * begins at address 0. HK
  257. */
  258. leptr = 0;
  259. writereg(&ll->rap, LE_CSR1);
  260. writereg(&ll->rdp, (leptr & 0xFFFF));
  261. writereg(&ll->rap, LE_CSR2);
  262. writereg(&ll->rdp, leptr >> 16);
  263. writereg(&ll->rap, LE_CSR3);
  264. writereg(&ll->rdp, lp->busmaster_regval);
  265. /* Point back to csr0 */
  266. writereg(&ll->rap, LE_CSR0);
  267. }
  268. /*
  269. * Our specialized copy routines
  270. *
  271. */
  272. static void cp_to_buf(const int type, void *to, const void *from, int len)
  273. {
  274. unsigned short *tp, *fp, clen;
  275. unsigned char *rtp, *rfp;
  276. if (type == PMAD_LANCE) {
  277. memcpy(to, from, len);
  278. } else if (type == PMAX_LANCE) {
  279. clen = len >> 1;
  280. tp = (unsigned short *) to;
  281. fp = (unsigned short *) from;
  282. while (clen--) {
  283. *tp++ = *fp++;
  284. tp++;
  285. }
  286. clen = len & 1;
  287. rtp = (unsigned char *) tp;
  288. rfp = (unsigned char *) fp;
  289. while (clen--) {
  290. *rtp++ = *rfp++;
  291. }
  292. } else {
  293. /*
  294. * copy 16 Byte chunks
  295. */
  296. clen = len >> 4;
  297. tp = (unsigned short *) to;
  298. fp = (unsigned short *) from;
  299. while (clen--) {
  300. *tp++ = *fp++;
  301. *tp++ = *fp++;
  302. *tp++ = *fp++;
  303. *tp++ = *fp++;
  304. *tp++ = *fp++;
  305. *tp++ = *fp++;
  306. *tp++ = *fp++;
  307. *tp++ = *fp++;
  308. tp += 8;
  309. }
  310. /*
  311. * do the rest, if any.
  312. */
  313. clen = len & 15;
  314. rtp = (unsigned char *) tp;
  315. rfp = (unsigned char *) fp;
  316. while (clen--) {
  317. *rtp++ = *rfp++;
  318. }
  319. }
  320. iob();
  321. }
  322. static void cp_from_buf(const int type, void *to, const void *from, int len)
  323. {
  324. unsigned short *tp, *fp, clen;
  325. unsigned char *rtp, *rfp;
  326. if (type == PMAD_LANCE) {
  327. memcpy(to, from, len);
  328. } else if (type == PMAX_LANCE) {
  329. clen = len >> 1;
  330. tp = (unsigned short *) to;
  331. fp = (unsigned short *) from;
  332. while (clen--) {
  333. *tp++ = *fp++;
  334. fp++;
  335. }
  336. clen = len & 1;
  337. rtp = (unsigned char *) tp;
  338. rfp = (unsigned char *) fp;
  339. while (clen--) {
  340. *rtp++ = *rfp++;
  341. }
  342. } else {
  343. /*
  344. * copy 16 Byte chunks
  345. */
  346. clen = len >> 4;
  347. tp = (unsigned short *) to;
  348. fp = (unsigned short *) from;
  349. while (clen--) {
  350. *tp++ = *fp++;
  351. *tp++ = *fp++;
  352. *tp++ = *fp++;
  353. *tp++ = *fp++;
  354. *tp++ = *fp++;
  355. *tp++ = *fp++;
  356. *tp++ = *fp++;
  357. *tp++ = *fp++;
  358. fp += 8;
  359. }
  360. /*
  361. * do the rest, if any.
  362. */
  363. clen = len & 15;
  364. rtp = (unsigned char *) tp;
  365. rfp = (unsigned char *) fp;
  366. while (clen--) {
  367. *rtp++ = *rfp++;
  368. }
  369. }
  370. }
  371. /* Setup the Lance Rx and Tx rings */
  372. static void lance_init_ring(struct net_device *dev)
  373. {
  374. struct lance_private *lp = netdev_priv(dev);
  375. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  376. uint leptr;
  377. int i;
  378. /* Lock out other processes while setting up hardware */
  379. netif_stop_queue(dev);
  380. lp->rx_new = lp->tx_new = 0;
  381. lp->rx_old = lp->tx_old = 0;
  382. /* Copy the ethernet address to the lance init block.
  383. * XXX bit 0 of the physical address registers has to be zero
  384. */
  385. *lib_ptr(ib, phys_addr[0], lp->type) = (dev->dev_addr[1] << 8) |
  386. dev->dev_addr[0];
  387. *lib_ptr(ib, phys_addr[1], lp->type) = (dev->dev_addr[3] << 8) |
  388. dev->dev_addr[2];
  389. *lib_ptr(ib, phys_addr[2], lp->type) = (dev->dev_addr[5] << 8) |
  390. dev->dev_addr[4];
  391. /* Setup the initialization block */
  392. /* Setup rx descriptor pointer */
  393. leptr = offsetof(struct lance_init_block, brx_ring);
  394. *lib_ptr(ib, rx_len, lp->type) = (LANCE_LOG_RX_BUFFERS << 13) |
  395. (leptr >> 16);
  396. *lib_ptr(ib, rx_ptr, lp->type) = leptr;
  397. if (ZERO)
  398. printk("RX ptr: %8.8x(%8.8x)\n",
  399. leptr, lib_off(brx_ring, lp->type));
  400. /* Setup tx descriptor pointer */
  401. leptr = offsetof(struct lance_init_block, btx_ring);
  402. *lib_ptr(ib, tx_len, lp->type) = (LANCE_LOG_TX_BUFFERS << 13) |
  403. (leptr >> 16);
  404. *lib_ptr(ib, tx_ptr, lp->type) = leptr;
  405. if (ZERO)
  406. printk("TX ptr: %8.8x(%8.8x)\n",
  407. leptr, lib_off(btx_ring, lp->type));
  408. if (ZERO)
  409. printk("TX rings:\n");
  410. /* Setup the Tx ring entries */
  411. for (i = 0; i < TX_RING_SIZE; i++) {
  412. leptr = lp->tx_buf_ptr_lnc[i];
  413. *lib_ptr(ib, btx_ring[i].tmd0, lp->type) = leptr;
  414. *lib_ptr(ib, btx_ring[i].tmd1, lp->type) = (leptr >> 16) &
  415. 0xff;
  416. *lib_ptr(ib, btx_ring[i].length, lp->type) = 0xf000;
  417. /* The ones required by tmd2 */
  418. *lib_ptr(ib, btx_ring[i].misc, lp->type) = 0;
  419. if (i < 3 && ZERO)
  420. printk("%d: 0x%8.8x(0x%8.8x)\n",
  421. i, leptr, (uint)lp->tx_buf_ptr_cpu[i]);
  422. }
  423. /* Setup the Rx ring entries */
  424. if (ZERO)
  425. printk("RX rings:\n");
  426. for (i = 0; i < RX_RING_SIZE; i++) {
  427. leptr = lp->rx_buf_ptr_lnc[i];
  428. *lib_ptr(ib, brx_ring[i].rmd0, lp->type) = leptr;
  429. *lib_ptr(ib, brx_ring[i].rmd1, lp->type) = ((leptr >> 16) &
  430. 0xff) |
  431. LE_R1_OWN;
  432. *lib_ptr(ib, brx_ring[i].length, lp->type) = -RX_BUFF_SIZE |
  433. 0xf000;
  434. *lib_ptr(ib, brx_ring[i].mblength, lp->type) = 0;
  435. if (i < 3 && ZERO)
  436. printk("%d: 0x%8.8x(0x%8.8x)\n",
  437. i, leptr, (uint)lp->rx_buf_ptr_cpu[i]);
  438. }
  439. iob();
  440. }
  441. static int init_restart_lance(struct lance_private *lp)
  442. {
  443. volatile struct lance_regs *ll = lp->ll;
  444. int i;
  445. writereg(&ll->rap, LE_CSR0);
  446. writereg(&ll->rdp, LE_C0_INIT);
  447. /* Wait for the lance to complete initialization */
  448. for (i = 0; (i < 100) && !(ll->rdp & LE_C0_IDON); i++) {
  449. udelay(10);
  450. }
  451. if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
  452. printk("LANCE unopened after %d ticks, csr0=%4.4x.\n",
  453. i, ll->rdp);
  454. return -1;
  455. }
  456. if ((ll->rdp & LE_C0_ERR)) {
  457. printk("LANCE unopened after %d ticks, csr0=%4.4x.\n",
  458. i, ll->rdp);
  459. return -1;
  460. }
  461. writereg(&ll->rdp, LE_C0_IDON);
  462. writereg(&ll->rdp, LE_C0_STRT);
  463. writereg(&ll->rdp, LE_C0_INEA);
  464. return 0;
  465. }
  466. static int lance_rx(struct net_device *dev)
  467. {
  468. struct lance_private *lp = netdev_priv(dev);
  469. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  470. volatile u16 *rd;
  471. unsigned short bits;
  472. int entry, len;
  473. struct sk_buff *skb;
  474. #ifdef TEST_HITS
  475. {
  476. int i;
  477. printk("[");
  478. for (i = 0; i < RX_RING_SIZE; i++) {
  479. if (i == lp->rx_new)
  480. printk("%s", *lib_ptr(ib, brx_ring[i].rmd1,
  481. lp->type) &
  482. LE_R1_OWN ? "_" : "X");
  483. else
  484. printk("%s", *lib_ptr(ib, brx_ring[i].rmd1,
  485. lp->type) &
  486. LE_R1_OWN ? "." : "1");
  487. }
  488. printk("]");
  489. }
  490. #endif
  491. for (rd = lib_ptr(ib, brx_ring[lp->rx_new], lp->type);
  492. !((bits = *rds_ptr(rd, rmd1, lp->type)) & LE_R1_OWN);
  493. rd = lib_ptr(ib, brx_ring[lp->rx_new], lp->type)) {
  494. entry = lp->rx_new;
  495. /* We got an incomplete frame? */
  496. if ((bits & LE_R1_POK) != LE_R1_POK) {
  497. dev->stats.rx_over_errors++;
  498. dev->stats.rx_errors++;
  499. } else if (bits & LE_R1_ERR) {
  500. /* Count only the end frame as a rx error,
  501. * not the beginning
  502. */
  503. if (bits & LE_R1_BUF)
  504. dev->stats.rx_fifo_errors++;
  505. if (bits & LE_R1_CRC)
  506. dev->stats.rx_crc_errors++;
  507. if (bits & LE_R1_OFL)
  508. dev->stats.rx_over_errors++;
  509. if (bits & LE_R1_FRA)
  510. dev->stats.rx_frame_errors++;
  511. if (bits & LE_R1_EOP)
  512. dev->stats.rx_errors++;
  513. } else {
  514. len = (*rds_ptr(rd, mblength, lp->type) & 0xfff) - 4;
  515. skb = dev_alloc_skb(len + 2);
  516. if (skb == 0) {
  517. printk("%s: Memory squeeze, deferring packet.\n",
  518. dev->name);
  519. dev->stats.rx_dropped++;
  520. *rds_ptr(rd, mblength, lp->type) = 0;
  521. *rds_ptr(rd, rmd1, lp->type) =
  522. ((lp->rx_buf_ptr_lnc[entry] >> 16) &
  523. 0xff) | LE_R1_OWN;
  524. lp->rx_new = (entry + 1) & RX_RING_MOD_MASK;
  525. return 0;
  526. }
  527. dev->stats.rx_bytes += len;
  528. skb_reserve(skb, 2); /* 16 byte align */
  529. skb_put(skb, len); /* make room */
  530. cp_from_buf(lp->type, skb->data,
  531. (char *)lp->rx_buf_ptr_cpu[entry], len);
  532. skb->protocol = eth_type_trans(skb, dev);
  533. netif_rx(skb);
  534. dev->stats.rx_packets++;
  535. }
  536. /* Return the packet to the pool */
  537. *rds_ptr(rd, mblength, lp->type) = 0;
  538. *rds_ptr(rd, length, lp->type) = -RX_BUFF_SIZE | 0xf000;
  539. *rds_ptr(rd, rmd1, lp->type) =
  540. ((lp->rx_buf_ptr_lnc[entry] >> 16) & 0xff) | LE_R1_OWN;
  541. lp->rx_new = (entry + 1) & RX_RING_MOD_MASK;
  542. }
  543. return 0;
  544. }
  545. static void lance_tx(struct net_device *dev)
  546. {
  547. struct lance_private *lp = netdev_priv(dev);
  548. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  549. volatile struct lance_regs *ll = lp->ll;
  550. volatile u16 *td;
  551. int i, j;
  552. int status;
  553. j = lp->tx_old;
  554. spin_lock(&lp->lock);
  555. for (i = j; i != lp->tx_new; i = j) {
  556. td = lib_ptr(ib, btx_ring[i], lp->type);
  557. /* If we hit a packet not owned by us, stop */
  558. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_OWN)
  559. break;
  560. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_ERR) {
  561. status = *tds_ptr(td, misc, lp->type);
  562. dev->stats.tx_errors++;
  563. if (status & LE_T3_RTY)
  564. dev->stats.tx_aborted_errors++;
  565. if (status & LE_T3_LCOL)
  566. dev->stats.tx_window_errors++;
  567. if (status & LE_T3_CLOS) {
  568. dev->stats.tx_carrier_errors++;
  569. printk("%s: Carrier Lost\n", dev->name);
  570. /* Stop the lance */
  571. writereg(&ll->rap, LE_CSR0);
  572. writereg(&ll->rdp, LE_C0_STOP);
  573. lance_init_ring(dev);
  574. load_csrs(lp);
  575. init_restart_lance(lp);
  576. goto out;
  577. }
  578. /* Buffer errors and underflows turn off the
  579. * transmitter, restart the adapter.
  580. */
  581. if (status & (LE_T3_BUF | LE_T3_UFL)) {
  582. dev->stats.tx_fifo_errors++;
  583. printk("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
  584. dev->name);
  585. /* Stop the lance */
  586. writereg(&ll->rap, LE_CSR0);
  587. writereg(&ll->rdp, LE_C0_STOP);
  588. lance_init_ring(dev);
  589. load_csrs(lp);
  590. init_restart_lance(lp);
  591. goto out;
  592. }
  593. } else if ((*tds_ptr(td, tmd1, lp->type) & LE_T1_POK) ==
  594. LE_T1_POK) {
  595. /*
  596. * So we don't count the packet more than once.
  597. */
  598. *tds_ptr(td, tmd1, lp->type) &= ~(LE_T1_POK);
  599. /* One collision before packet was sent. */
  600. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_EONE)
  601. dev->stats.collisions++;
  602. /* More than one collision, be optimistic. */
  603. if (*tds_ptr(td, tmd1, lp->type) & LE_T1_EMORE)
  604. dev->stats.collisions += 2;
  605. dev->stats.tx_packets++;
  606. }
  607. j = (j + 1) & TX_RING_MOD_MASK;
  608. }
  609. lp->tx_old = j;
  610. out:
  611. if (netif_queue_stopped(dev) &&
  612. TX_BUFFS_AVAIL > 0)
  613. netif_wake_queue(dev);
  614. spin_unlock(&lp->lock);
  615. }
  616. static irqreturn_t lance_dma_merr_int(int irq, void *dev_id)
  617. {
  618. struct net_device *dev = dev_id;
  619. printk(KERN_ERR "%s: DMA error\n", dev->name);
  620. return IRQ_HANDLED;
  621. }
  622. static irqreturn_t lance_interrupt(int irq, void *dev_id)
  623. {
  624. struct net_device *dev = dev_id;
  625. struct lance_private *lp = netdev_priv(dev);
  626. volatile struct lance_regs *ll = lp->ll;
  627. int csr0;
  628. writereg(&ll->rap, LE_CSR0);
  629. csr0 = ll->rdp;
  630. /* Acknowledge all the interrupt sources ASAP */
  631. writereg(&ll->rdp, csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT));
  632. if ((csr0 & LE_C0_ERR)) {
  633. /* Clear the error condition */
  634. writereg(&ll->rdp, LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
  635. LE_C0_CERR | LE_C0_MERR);
  636. }
  637. if (csr0 & LE_C0_RINT)
  638. lance_rx(dev);
  639. if (csr0 & LE_C0_TINT)
  640. lance_tx(dev);
  641. if (csr0 & LE_C0_BABL)
  642. dev->stats.tx_errors++;
  643. if (csr0 & LE_C0_MISS)
  644. dev->stats.rx_errors++;
  645. if (csr0 & LE_C0_MERR) {
  646. printk("%s: Memory error, status %04x\n", dev->name, csr0);
  647. writereg(&ll->rdp, LE_C0_STOP);
  648. lance_init_ring(dev);
  649. load_csrs(lp);
  650. init_restart_lance(lp);
  651. netif_wake_queue(dev);
  652. }
  653. writereg(&ll->rdp, LE_C0_INEA);
  654. writereg(&ll->rdp, LE_C0_INEA);
  655. return IRQ_HANDLED;
  656. }
  657. static int lance_open(struct net_device *dev)
  658. {
  659. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  660. struct lance_private *lp = netdev_priv(dev);
  661. volatile struct lance_regs *ll = lp->ll;
  662. int status = 0;
  663. /* Stop the Lance */
  664. writereg(&ll->rap, LE_CSR0);
  665. writereg(&ll->rdp, LE_C0_STOP);
  666. /* Set mode and clear multicast filter only at device open,
  667. * so that lance_init_ring() called at any error will not
  668. * forget multicast filters.
  669. *
  670. * BTW it is common bug in all lance drivers! --ANK
  671. */
  672. *lib_ptr(ib, mode, lp->type) = 0;
  673. *lib_ptr(ib, filter[0], lp->type) = 0;
  674. *lib_ptr(ib, filter[1], lp->type) = 0;
  675. *lib_ptr(ib, filter[2], lp->type) = 0;
  676. *lib_ptr(ib, filter[3], lp->type) = 0;
  677. lance_init_ring(dev);
  678. load_csrs(lp);
  679. netif_start_queue(dev);
  680. /* Associate IRQ with lance_interrupt */
  681. if (request_irq(dev->irq, lance_interrupt, 0, "lance", dev)) {
  682. printk("%s: Can't get IRQ %d\n", dev->name, dev->irq);
  683. return -EAGAIN;
  684. }
  685. if (lp->dma_irq >= 0) {
  686. unsigned long flags;
  687. if (request_irq(lp->dma_irq, lance_dma_merr_int, 0,
  688. "lance error", dev)) {
  689. free_irq(dev->irq, dev);
  690. printk("%s: Can't get DMA IRQ %d\n", dev->name,
  691. lp->dma_irq);
  692. return -EAGAIN;
  693. }
  694. spin_lock_irqsave(&ioasic_ssr_lock, flags);
  695. fast_mb();
  696. /* Enable I/O ASIC LANCE DMA. */
  697. ioasic_write(IO_REG_SSR,
  698. ioasic_read(IO_REG_SSR) | IO_SSR_LANCE_DMA_EN);
  699. fast_mb();
  700. spin_unlock_irqrestore(&ioasic_ssr_lock, flags);
  701. }
  702. status = init_restart_lance(lp);
  703. return status;
  704. }
  705. static int lance_close(struct net_device *dev)
  706. {
  707. struct lance_private *lp = netdev_priv(dev);
  708. volatile struct lance_regs *ll = lp->ll;
  709. netif_stop_queue(dev);
  710. del_timer_sync(&lp->multicast_timer);
  711. /* Stop the card */
  712. writereg(&ll->rap, LE_CSR0);
  713. writereg(&ll->rdp, LE_C0_STOP);
  714. if (lp->dma_irq >= 0) {
  715. unsigned long flags;
  716. spin_lock_irqsave(&ioasic_ssr_lock, flags);
  717. fast_mb();
  718. /* Disable I/O ASIC LANCE DMA. */
  719. ioasic_write(IO_REG_SSR,
  720. ioasic_read(IO_REG_SSR) & ~IO_SSR_LANCE_DMA_EN);
  721. fast_iob();
  722. spin_unlock_irqrestore(&ioasic_ssr_lock, flags);
  723. free_irq(lp->dma_irq, dev);
  724. }
  725. free_irq(dev->irq, dev);
  726. return 0;
  727. }
  728. static inline int lance_reset(struct net_device *dev)
  729. {
  730. struct lance_private *lp = netdev_priv(dev);
  731. volatile struct lance_regs *ll = lp->ll;
  732. int status;
  733. /* Stop the lance */
  734. writereg(&ll->rap, LE_CSR0);
  735. writereg(&ll->rdp, LE_C0_STOP);
  736. lance_init_ring(dev);
  737. load_csrs(lp);
  738. dev->trans_start = jiffies; /* prevent tx timeout */
  739. status = init_restart_lance(lp);
  740. return status;
  741. }
  742. static void lance_tx_timeout(struct net_device *dev)
  743. {
  744. struct lance_private *lp = netdev_priv(dev);
  745. volatile struct lance_regs *ll = lp->ll;
  746. printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
  747. dev->name, ll->rdp);
  748. lance_reset(dev);
  749. netif_wake_queue(dev);
  750. }
  751. static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
  752. {
  753. struct lance_private *lp = netdev_priv(dev);
  754. volatile struct lance_regs *ll = lp->ll;
  755. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  756. unsigned long flags;
  757. int entry, len;
  758. len = skb->len;
  759. if (len < ETH_ZLEN) {
  760. if (skb_padto(skb, ETH_ZLEN))
  761. return NETDEV_TX_OK;
  762. len = ETH_ZLEN;
  763. }
  764. dev->stats.tx_bytes += len;
  765. spin_lock_irqsave(&lp->lock, flags);
  766. entry = lp->tx_new;
  767. *lib_ptr(ib, btx_ring[entry].length, lp->type) = (-len);
  768. *lib_ptr(ib, btx_ring[entry].misc, lp->type) = 0;
  769. cp_to_buf(lp->type, (char *)lp->tx_buf_ptr_cpu[entry], skb->data, len);
  770. /* Now, give the packet to the lance */
  771. *lib_ptr(ib, btx_ring[entry].tmd1, lp->type) =
  772. ((lp->tx_buf_ptr_lnc[entry] >> 16) & 0xff) |
  773. (LE_T1_POK | LE_T1_OWN);
  774. lp->tx_new = (entry + 1) & TX_RING_MOD_MASK;
  775. if (TX_BUFFS_AVAIL <= 0)
  776. netif_stop_queue(dev);
  777. /* Kick the lance: transmit now */
  778. writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
  779. spin_unlock_irqrestore(&lp->lock, flags);
  780. dev_kfree_skb(skb);
  781. return NETDEV_TX_OK;
  782. }
  783. static void lance_load_multicast(struct net_device *dev)
  784. {
  785. struct lance_private *lp = netdev_priv(dev);
  786. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  787. struct netdev_hw_addr *ha;
  788. char *addrs;
  789. u32 crc;
  790. /* set all multicast bits */
  791. if (dev->flags & IFF_ALLMULTI) {
  792. *lib_ptr(ib, filter[0], lp->type) = 0xffff;
  793. *lib_ptr(ib, filter[1], lp->type) = 0xffff;
  794. *lib_ptr(ib, filter[2], lp->type) = 0xffff;
  795. *lib_ptr(ib, filter[3], lp->type) = 0xffff;
  796. return;
  797. }
  798. /* clear the multicast filter */
  799. *lib_ptr(ib, filter[0], lp->type) = 0;
  800. *lib_ptr(ib, filter[1], lp->type) = 0;
  801. *lib_ptr(ib, filter[2], lp->type) = 0;
  802. *lib_ptr(ib, filter[3], lp->type) = 0;
  803. /* Add addresses */
  804. netdev_for_each_mc_addr(ha, dev) {
  805. addrs = ha->addr;
  806. /* multicast address? */
  807. if (!(*addrs & 1))
  808. continue;
  809. crc = ether_crc_le(ETH_ALEN, addrs);
  810. crc = crc >> 26;
  811. *lib_ptr(ib, filter[crc >> 4], lp->type) |= 1 << (crc & 0xf);
  812. }
  813. }
  814. static void lance_set_multicast(struct net_device *dev)
  815. {
  816. struct lance_private *lp = netdev_priv(dev);
  817. volatile u16 *ib = (volatile u16 *)dev->mem_start;
  818. volatile struct lance_regs *ll = lp->ll;
  819. if (!netif_running(dev))
  820. return;
  821. if (lp->tx_old != lp->tx_new) {
  822. mod_timer(&lp->multicast_timer, jiffies + 4 * HZ/100);
  823. netif_wake_queue(dev);
  824. return;
  825. }
  826. netif_stop_queue(dev);
  827. writereg(&ll->rap, LE_CSR0);
  828. writereg(&ll->rdp, LE_C0_STOP);
  829. lance_init_ring(dev);
  830. if (dev->flags & IFF_PROMISC) {
  831. *lib_ptr(ib, mode, lp->type) |= LE_MO_PROM;
  832. } else {
  833. *lib_ptr(ib, mode, lp->type) &= ~LE_MO_PROM;
  834. lance_load_multicast(dev);
  835. }
  836. load_csrs(lp);
  837. init_restart_lance(lp);
  838. netif_wake_queue(dev);
  839. }
  840. static void lance_set_multicast_retry(unsigned long _opaque)
  841. {
  842. struct net_device *dev = (struct net_device *) _opaque;
  843. lance_set_multicast(dev);
  844. }
  845. static const struct net_device_ops lance_netdev_ops = {
  846. .ndo_open = lance_open,
  847. .ndo_stop = lance_close,
  848. .ndo_start_xmit = lance_start_xmit,
  849. .ndo_tx_timeout = lance_tx_timeout,
  850. .ndo_set_multicast_list = lance_set_multicast,
  851. .ndo_change_mtu = eth_change_mtu,
  852. .ndo_validate_addr = eth_validate_addr,
  853. .ndo_set_mac_address = eth_mac_addr,
  854. };
  855. static int __devinit dec_lance_probe(struct device *bdev, const int type)
  856. {
  857. static unsigned version_printed;
  858. static const char fmt[] = "declance%d";
  859. char name[10];
  860. struct net_device *dev;
  861. struct lance_private *lp;
  862. volatile struct lance_regs *ll;
  863. resource_size_t start = 0, len = 0;
  864. int i, ret;
  865. unsigned long esar_base;
  866. unsigned char *esar;
  867. if (dec_lance_debug && version_printed++ == 0)
  868. printk(version);
  869. if (bdev)
  870. snprintf(name, sizeof(name), "%s", dev_name(bdev));
  871. else {
  872. i = 0;
  873. dev = root_lance_dev;
  874. while (dev) {
  875. i++;
  876. lp = netdev_priv(dev);
  877. dev = lp->next;
  878. }
  879. snprintf(name, sizeof(name), fmt, i);
  880. }
  881. dev = alloc_etherdev(sizeof(struct lance_private));
  882. if (!dev) {
  883. printk(KERN_ERR "%s: Unable to allocate etherdev, aborting.\n",
  884. name);
  885. ret = -ENOMEM;
  886. goto err_out;
  887. }
  888. /*
  889. * alloc_etherdev ensures the data structures used by the LANCE
  890. * are aligned.
  891. */
  892. lp = netdev_priv(dev);
  893. spin_lock_init(&lp->lock);
  894. lp->type = type;
  895. switch (type) {
  896. case ASIC_LANCE:
  897. dev->base_addr = CKSEG1ADDR(dec_kn_slot_base + IOASIC_LANCE);
  898. /* buffer space for the on-board LANCE shared memory */
  899. /*
  900. * FIXME: ugly hack!
  901. */
  902. dev->mem_start = CKSEG1ADDR(0x00020000);
  903. dev->mem_end = dev->mem_start + 0x00020000;
  904. dev->irq = dec_interrupt[DEC_IRQ_LANCE];
  905. esar_base = CKSEG1ADDR(dec_kn_slot_base + IOASIC_ESAR);
  906. /* Workaround crash with booting KN04 2.1k from Disk */
  907. memset((void *)dev->mem_start, 0,
  908. dev->mem_end - dev->mem_start);
  909. /*
  910. * setup the pointer arrays, this sucks [tm] :-(
  911. */
  912. for (i = 0; i < RX_RING_SIZE; i++) {
  913. lp->rx_buf_ptr_cpu[i] =
  914. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  915. 2 * i * RX_BUFF_SIZE);
  916. lp->rx_buf_ptr_lnc[i] =
  917. (BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  918. }
  919. for (i = 0; i < TX_RING_SIZE; i++) {
  920. lp->tx_buf_ptr_cpu[i] =
  921. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  922. 2 * RX_RING_SIZE * RX_BUFF_SIZE +
  923. 2 * i * TX_BUFF_SIZE);
  924. lp->tx_buf_ptr_lnc[i] =
  925. (BUF_OFFSET_LNC +
  926. RX_RING_SIZE * RX_BUFF_SIZE +
  927. i * TX_BUFF_SIZE);
  928. }
  929. /* Setup I/O ASIC LANCE DMA. */
  930. lp->dma_irq = dec_interrupt[DEC_IRQ_LANCE_MERR];
  931. ioasic_write(IO_REG_LANCE_DMA_P,
  932. CPHYSADDR(dev->mem_start) << 3);
  933. break;
  934. #ifdef CONFIG_TC
  935. case PMAD_LANCE:
  936. dev_set_drvdata(bdev, dev);
  937. start = to_tc_dev(bdev)->resource.start;
  938. len = to_tc_dev(bdev)->resource.end - start + 1;
  939. if (!request_mem_region(start, len, dev_name(bdev))) {
  940. printk(KERN_ERR
  941. "%s: Unable to reserve MMIO resource\n",
  942. dev_name(bdev));
  943. ret = -EBUSY;
  944. goto err_out_dev;
  945. }
  946. dev->mem_start = CKSEG1ADDR(start);
  947. dev->mem_end = dev->mem_start + 0x100000;
  948. dev->base_addr = dev->mem_start + 0x100000;
  949. dev->irq = to_tc_dev(bdev)->interrupt;
  950. esar_base = dev->mem_start + 0x1c0002;
  951. lp->dma_irq = -1;
  952. for (i = 0; i < RX_RING_SIZE; i++) {
  953. lp->rx_buf_ptr_cpu[i] =
  954. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  955. i * RX_BUFF_SIZE);
  956. lp->rx_buf_ptr_lnc[i] =
  957. (BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  958. }
  959. for (i = 0; i < TX_RING_SIZE; i++) {
  960. lp->tx_buf_ptr_cpu[i] =
  961. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  962. RX_RING_SIZE * RX_BUFF_SIZE +
  963. i * TX_BUFF_SIZE);
  964. lp->tx_buf_ptr_lnc[i] =
  965. (BUF_OFFSET_LNC +
  966. RX_RING_SIZE * RX_BUFF_SIZE +
  967. i * TX_BUFF_SIZE);
  968. }
  969. break;
  970. #endif
  971. case PMAX_LANCE:
  972. dev->irq = dec_interrupt[DEC_IRQ_LANCE];
  973. dev->base_addr = CKSEG1ADDR(KN01_SLOT_BASE + KN01_LANCE);
  974. dev->mem_start = CKSEG1ADDR(KN01_SLOT_BASE + KN01_LANCE_MEM);
  975. dev->mem_end = dev->mem_start + KN01_SLOT_SIZE;
  976. esar_base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_ESAR + 1);
  977. lp->dma_irq = -1;
  978. /*
  979. * setup the pointer arrays, this sucks [tm] :-(
  980. */
  981. for (i = 0; i < RX_RING_SIZE; i++) {
  982. lp->rx_buf_ptr_cpu[i] =
  983. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  984. 2 * i * RX_BUFF_SIZE);
  985. lp->rx_buf_ptr_lnc[i] =
  986. (BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  987. }
  988. for (i = 0; i < TX_RING_SIZE; i++) {
  989. lp->tx_buf_ptr_cpu[i] =
  990. (char *)(dev->mem_start + 2 * BUF_OFFSET_CPU +
  991. 2 * RX_RING_SIZE * RX_BUFF_SIZE +
  992. 2 * i * TX_BUFF_SIZE);
  993. lp->tx_buf_ptr_lnc[i] =
  994. (BUF_OFFSET_LNC +
  995. RX_RING_SIZE * RX_BUFF_SIZE +
  996. i * TX_BUFF_SIZE);
  997. }
  998. break;
  999. default:
  1000. printk(KERN_ERR "%s: declance_init called with unknown type\n",
  1001. name);
  1002. ret = -ENODEV;
  1003. goto err_out_dev;
  1004. }
  1005. ll = (struct lance_regs *) dev->base_addr;
  1006. esar = (unsigned char *) esar_base;
  1007. /* prom checks */
  1008. /* First, check for test pattern */
  1009. if (esar[0x60] != 0xff && esar[0x64] != 0x00 &&
  1010. esar[0x68] != 0x55 && esar[0x6c] != 0xaa) {
  1011. printk(KERN_ERR
  1012. "%s: Ethernet station address prom not found!\n",
  1013. name);
  1014. ret = -ENODEV;
  1015. goto err_out_resource;
  1016. }
  1017. /* Check the prom contents */
  1018. for (i = 0; i < 8; i++) {
  1019. if (esar[i * 4] != esar[0x3c - i * 4] &&
  1020. esar[i * 4] != esar[0x40 + i * 4] &&
  1021. esar[0x3c - i * 4] != esar[0x40 + i * 4]) {
  1022. printk(KERN_ERR "%s: Something is wrong with the "
  1023. "ethernet station address prom!\n", name);
  1024. ret = -ENODEV;
  1025. goto err_out_resource;
  1026. }
  1027. }
  1028. /* Copy the ethernet address to the device structure, later to the
  1029. * lance initialization block so the lance gets it every time it's
  1030. * (re)initialized.
  1031. */
  1032. switch (type) {
  1033. case ASIC_LANCE:
  1034. printk("%s: IOASIC onboard LANCE", name);
  1035. break;
  1036. case PMAD_LANCE:
  1037. printk("%s: PMAD-AA", name);
  1038. break;
  1039. case PMAX_LANCE:
  1040. printk("%s: PMAX onboard LANCE", name);
  1041. break;
  1042. }
  1043. for (i = 0; i < 6; i++)
  1044. dev->dev_addr[i] = esar[i * 4];
  1045. printk(", addr = %pM, irq = %d\n", dev->dev_addr, dev->irq);
  1046. dev->netdev_ops = &lance_netdev_ops;
  1047. dev->watchdog_timeo = 5*HZ;
  1048. /* lp->ll is the location of the registers for lance card */
  1049. lp->ll = ll;
  1050. /* busmaster_regval (CSR3) should be zero according to the PMAD-AA
  1051. * specification.
  1052. */
  1053. lp->busmaster_regval = 0;
  1054. dev->dma = 0;
  1055. /* We cannot sleep if the chip is busy during a
  1056. * multicast list update event, because such events
  1057. * can occur from interrupts (ex. IPv6). So we
  1058. * use a timer to try again later when necessary. -DaveM
  1059. */
  1060. init_timer(&lp->multicast_timer);
  1061. lp->multicast_timer.data = (unsigned long) dev;
  1062. lp->multicast_timer.function = lance_set_multicast_retry;
  1063. ret = register_netdev(dev);
  1064. if (ret) {
  1065. printk(KERN_ERR
  1066. "%s: Unable to register netdev, aborting.\n", name);
  1067. goto err_out_resource;
  1068. }
  1069. if (!bdev) {
  1070. lp->next = root_lance_dev;
  1071. root_lance_dev = dev;
  1072. }
  1073. printk("%s: registered as %s.\n", name, dev->name);
  1074. return 0;
  1075. err_out_resource:
  1076. if (bdev)
  1077. release_mem_region(start, len);
  1078. err_out_dev:
  1079. free_netdev(dev);
  1080. err_out:
  1081. return ret;
  1082. }
  1083. static void __exit dec_lance_remove(struct device *bdev)
  1084. {
  1085. struct net_device *dev = dev_get_drvdata(bdev);
  1086. resource_size_t start, len;
  1087. unregister_netdev(dev);
  1088. start = to_tc_dev(bdev)->resource.start;
  1089. len = to_tc_dev(bdev)->resource.end - start + 1;
  1090. release_mem_region(start, len);
  1091. free_netdev(dev);
  1092. }
  1093. /* Find all the lance cards on the system and initialize them */
  1094. static int __init dec_lance_platform_probe(void)
  1095. {
  1096. int count = 0;
  1097. if (dec_interrupt[DEC_IRQ_LANCE] >= 0) {
  1098. if (dec_interrupt[DEC_IRQ_LANCE_MERR] >= 0) {
  1099. if (dec_lance_probe(NULL, ASIC_LANCE) >= 0)
  1100. count++;
  1101. } else if (!TURBOCHANNEL) {
  1102. if (dec_lance_probe(NULL, PMAX_LANCE) >= 0)
  1103. count++;
  1104. }
  1105. }
  1106. return (count > 0) ? 0 : -ENODEV;
  1107. }
  1108. static void __exit dec_lance_platform_remove(void)
  1109. {
  1110. while (root_lance_dev) {
  1111. struct net_device *dev = root_lance_dev;
  1112. struct lance_private *lp = netdev_priv(dev);
  1113. unregister_netdev(dev);
  1114. root_lance_dev = lp->next;
  1115. free_netdev(dev);
  1116. }
  1117. }
  1118. #ifdef CONFIG_TC
  1119. static int __devinit dec_lance_tc_probe(struct device *dev);
  1120. static int __exit dec_lance_tc_remove(struct device *dev);
  1121. static const struct tc_device_id dec_lance_tc_table[] = {
  1122. { "DEC ", "PMAD-AA " },
  1123. { }
  1124. };
  1125. MODULE_DEVICE_TABLE(tc, dec_lance_tc_table);
  1126. static struct tc_driver dec_lance_tc_driver = {
  1127. .id_table = dec_lance_tc_table,
  1128. .driver = {
  1129. .name = "declance",
  1130. .bus = &tc_bus_type,
  1131. .probe = dec_lance_tc_probe,
  1132. .remove = __exit_p(dec_lance_tc_remove),
  1133. },
  1134. };
  1135. static int __devinit dec_lance_tc_probe(struct device *dev)
  1136. {
  1137. int status = dec_lance_probe(dev, PMAD_LANCE);
  1138. if (!status)
  1139. get_device(dev);
  1140. return status;
  1141. }
  1142. static int __exit dec_lance_tc_remove(struct device *dev)
  1143. {
  1144. put_device(dev);
  1145. dec_lance_remove(dev);
  1146. return 0;
  1147. }
  1148. #endif
  1149. static int __init dec_lance_init(void)
  1150. {
  1151. int status;
  1152. status = tc_register_driver(&dec_lance_tc_driver);
  1153. if (!status)
  1154. dec_lance_platform_probe();
  1155. return status;
  1156. }
  1157. static void __exit dec_lance_exit(void)
  1158. {
  1159. dec_lance_platform_remove();
  1160. tc_unregister_driver(&dec_lance_tc_driver);
  1161. }
  1162. module_init(dec_lance_init);
  1163. module_exit(dec_lance_exit);