declance.c 35 KB

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