declance.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:32k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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  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. #include <linux/config.h>
  45. #include <linux/crc32.h>
  46. #include <linux/delay.h>
  47. #include <linux/errno.h>
  48. #include <linux/if_ether.h>
  49. #include <linux/init.h>
  50. #include <linux/kernel.h>
  51. #include <linux/module.h>
  52. #include <linux/netdevice.h>
  53. #include <linux/etherdevice.h>
  54. #include <linux/stddef.h>
  55. #include <linux/string.h>
  56. #include <asm/addrspace.h>
  57. #include <asm/dec/interrupts.h>
  58. #include <asm/dec/ioasic.h>
  59. #include <asm/dec/ioasic_addrs.h>
  60. #include <asm/dec/kn01.h>
  61. #include <asm/dec/machtype.h>
  62. #include <asm/dec/tc.h>
  63. #include <asm/system.h>
  64. static char version[] __devinitdata =
  65. "declance.c: v0.009 by Linux MIPS DECstation task forcen";
  66. MODULE_AUTHOR("Linux MIPS DECstation task force");
  67. MODULE_DESCRIPTION("DEC LANCE (DECstation onboard, PMAD-xx) driver");
  68. MODULE_LICENSE("GPL");
  69. /*
  70.  * card types
  71.  */
  72. #define ASIC_LANCE 1
  73. #define PMAD_LANCE 2
  74. #define PMAX_LANCE 3
  75. #ifndef CONFIG_TC
  76. unsigned long system_base;
  77. unsigned long dmaptr;
  78. #endif
  79. #define LE_CSR0 0
  80. #define LE_CSR1 1
  81. #define LE_CSR2 2
  82. #define LE_CSR3 3
  83. #define LE_MO_PROM      0x8000 /* Enable promiscuous mode */
  84. #define LE_C0_ERR 0x8000 /* Error: set if BAB, SQE, MISS or ME is set */
  85. #define LE_C0_BABL 0x4000 /* BAB:  Babble: tx timeout. */
  86. #define LE_C0_CERR 0x2000 /* SQE:  Signal quality error */
  87. #define LE_C0_MISS 0x1000 /* MISS: Missed a packet */
  88. #define LE_C0_MERR 0x0800 /* ME:   Memory error */
  89. #define LE_C0_RINT 0x0400 /* Received interrupt */
  90. #define LE_C0_TINT 0x0200 /* Transmitter Interrupt */
  91. #define LE_C0_IDON 0x0100 /* IFIN: Init finished. */
  92. #define LE_C0_INTR 0x0080 /* Interrupt or error */
  93. #define LE_C0_INEA 0x0040 /* Interrupt enable */
  94. #define LE_C0_RXON 0x0020 /* Receiver on */
  95. #define LE_C0_TXON 0x0010 /* Transmitter on */
  96. #define LE_C0_TDMD 0x0008 /* Transmitter demand */
  97. #define LE_C0_STOP 0x0004 /* Stop the card */
  98. #define LE_C0_STRT 0x0002 /* Start the card */
  99. #define LE_C0_INIT 0x0001 /* Init the card */
  100. #define LE_C3_BSWP 0x4 /* SWAP */
  101. #define LE_C3_ACON 0x2 /* ALE Control */
  102. #define LE_C3_BCON 0x1 /* Byte control */
  103. /* Receive message descriptor 1 */
  104. #define LE_R1_OWN       0x80 /* Who owns the entry */
  105. #define LE_R1_ERR       0x40 /* Error: if FRA, OFL, CRC or BUF is set */
  106. #define LE_R1_FRA       0x20 /* FRA: Frame error */
  107. #define LE_R1_OFL       0x10 /* OFL: Frame overflow */
  108. #define LE_R1_CRC       0x08 /* CRC error */
  109. #define LE_R1_BUF       0x04 /* BUF: Buffer error */
  110. #define LE_R1_SOP       0x02 /* Start of packet */
  111. #define LE_R1_EOP       0x01 /* End of packet */
  112. #define LE_R1_POK       0x03 /* Packet is complete: SOP + EOP */
  113. #define LE_T1_OWN       0x80 /* Lance owns the packet */
  114. #define LE_T1_ERR       0x40 /* Error summary */
  115. #define LE_T1_EMORE     0x10 /* Error: more than one retry needed */
  116. #define LE_T1_EONE      0x08 /* Error: one retry needed */
  117. #define LE_T1_EDEF      0x04 /* Error: deferred */
  118. #define LE_T1_SOP       0x02 /* Start of packet */
  119. #define LE_T1_EOP       0x01 /* End of packet */
  120. #define LE_T1_POK 0x03 /* Packet is complete: SOP + EOP */
  121. #define LE_T3_BUF       0x8000 /* Buffer error */
  122. #define LE_T3_UFL       0x4000 /* Error underflow */
  123. #define LE_T3_LCOL      0x1000 /* Error late collision */
  124. #define LE_T3_CLOS      0x0800 /* Error carrier loss */
  125. #define LE_T3_RTY       0x0400 /* Error retry */
  126. #define LE_T3_TDR       0x03ff /* Time Domain Reflectometry counter */
  127. /* Define: 2^4 Tx buffers and 2^4 Rx buffers */
  128. #ifndef LANCE_LOG_TX_BUFFERS
  129. #define LANCE_LOG_TX_BUFFERS 4
  130. #define LANCE_LOG_RX_BUFFERS 4
  131. #endif
  132. #define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
  133. #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
  134. #define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
  135. #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
  136. #define PKT_BUF_SZ 1536
  137. #define RX_BUFF_SIZE            PKT_BUF_SZ
  138. #define TX_BUFF_SIZE            PKT_BUF_SZ
  139. #undef TEST_HITS
  140. #define ZERO 0
  141. /* The DS2000/3000 have a linear 64 KB buffer.
  142.  * The PMAD-AA has 128 kb buffer on-board. 
  143.  *
  144.  * The IOASIC LANCE devices use a shared memory region. This region as seen 
  145.  * from the CPU is (max) 128 KB long and has to be on an 128 KB boundary.
  146.  * The LANCE sees this as a 64 KB long continuous memory region.
  147.  *
  148.  * The LANCE's DMA address is used as an index in this buffer and DMA takes
  149.  * place in bursts of eight 16-Bit words which are packed into four 32-Bit words
  150.  * by the IOASIC. This leads to a strange padding: 16 bytes of valid data followed
  151.  * by a 16 byte gap :-(.
  152.  */
  153. struct lance_rx_desc {
  154. unsigned short rmd0; /* low address of packet */
  155. short gap0;
  156. unsigned char rmd1_hadr; /* high address of packet */
  157. unsigned char rmd1_bits; /* descriptor bits */
  158. short gap1;
  159. short length; /* 2s complement (negative!)
  160.    of buffer length */
  161. short gap2;
  162. unsigned short mblength; /* actual number of bytes received */
  163. short gap3;
  164. };
  165. struct lance_tx_desc {
  166. unsigned short tmd0; /* low address of packet */
  167. short gap0;
  168. unsigned char tmd1_hadr; /* high address of packet */
  169. unsigned char tmd1_bits; /* descriptor bits */
  170. short gap1;
  171. short length; /* 2s complement (negative!)
  172.    of buffer length */
  173. short gap2;
  174. unsigned short misc;
  175. short gap3;
  176. };
  177. /* First part of the LANCE initialization block, described in databook. */
  178. struct lance_init_block {
  179. unsigned short mode; /* pre-set mode (reg. 15) */
  180. short gap0;
  181. unsigned char phys_addr[12]; /* physical ethernet address
  182.    only 0, 1, 4, 5, 8, 9 are valid
  183.    2, 3, 6, 7, 10, 11 are gaps */
  184. unsigned short filter[8]; /* multicast filter
  185.    only 0, 2, 4, 6 are valid
  186.    1, 3, 5, 7 are gaps */
  187. /* Receive and transmit ring base, along with extra bits. */
  188. unsigned short rx_ptr; /* receive descriptor addr */
  189. short gap1;
  190. unsigned short rx_len; /* receive len and high addr */
  191. short gap2;
  192. unsigned short tx_ptr; /* transmit descriptor addr */
  193. short gap3;
  194. unsigned short tx_len; /* transmit len and high addr */
  195. short gap4;
  196. short gap5[8];
  197. /* The buffer descriptors */
  198. struct lance_rx_desc brx_ring[RX_RING_SIZE];
  199. struct lance_tx_desc btx_ring[TX_RING_SIZE];
  200. };
  201. #define BUF_OFFSET_CPU sizeof(struct lance_init_block)
  202. #define BUF_OFFSET_LNC (sizeof(struct lance_init_block)>>1)
  203. #define libdesc_offset(rt, elem) 
  204. ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
  205. /*
  206.  * This works *only* for the ring descriptors
  207.  */
  208. #define LANCE_ADDR(x) (PHYSADDR(x) >> 1)
  209. struct lance_private {
  210. struct net_device *next;
  211. int type;
  212. int slot;
  213. int dma_irq;
  214. volatile struct lance_regs *ll;
  215. volatile struct lance_init_block *init_block;
  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. char *rx_buf_ptr_lnc[RX_RING_SIZE];
  227. char *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.  * dependant.
  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 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. int 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. 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 == PMAX_LANCE) {
  277. clen = len >> 1;
  278. tp = (unsigned short *) to;
  279. fp = (unsigned short *) from;
  280. while (clen--) {
  281. *tp++ = *fp++;
  282. tp++;
  283. }
  284. clen = len & 1;
  285. rtp = (unsigned char *) tp;
  286. rfp = (unsigned char *) fp;
  287. while (clen--) {
  288. *rtp++ = *rfp++;
  289. }
  290. } else {
  291. /*
  292.  * copy 16 Byte chunks
  293.  */
  294. clen = len >> 4;
  295. tp = (unsigned short *) to;
  296. fp = (unsigned short *) from;
  297. while (clen--) {
  298. *tp++ = *fp++;
  299. *tp++ = *fp++;
  300. *tp++ = *fp++;
  301. *tp++ = *fp++;
  302. *tp++ = *fp++;
  303. *tp++ = *fp++;
  304. *tp++ = *fp++;
  305. *tp++ = *fp++;
  306. tp += 8;
  307. }
  308. /*
  309.  * do the rest, if any.
  310.  */
  311. clen = len & 15;
  312. rtp = (unsigned char *) tp;
  313. rfp = (unsigned char *) fp;
  314. while (clen--) {
  315. *rtp++ = *rfp++;
  316. }
  317. }
  318. iob();
  319. }
  320. void cp_from_buf(const int type, void *to, const void *from, int len)
  321. {
  322. unsigned short *tp, *fp, clen;
  323. unsigned char *rtp, *rfp;
  324. if (type == PMAX_LANCE) {
  325. clen = len >> 1;
  326. tp = (unsigned short *) to;
  327. fp = (unsigned short *) from;
  328. while (clen--) {
  329. *tp++ = *fp++;
  330. fp++;
  331. }
  332. clen = len & 1;
  333. rtp = (unsigned char *) tp;
  334. rfp = (unsigned char *) fp;
  335. while (clen--) {
  336. *rtp++ = *rfp++;
  337. }
  338. } else {
  339. /*
  340.  * copy 16 Byte chunks
  341.  */
  342. clen = len >> 4;
  343. tp = (unsigned short *) to;
  344. fp = (unsigned short *) from;
  345. while (clen--) {
  346. *tp++ = *fp++;
  347. *tp++ = *fp++;
  348. *tp++ = *fp++;
  349. *tp++ = *fp++;
  350. *tp++ = *fp++;
  351. *tp++ = *fp++;
  352. *tp++ = *fp++;
  353. *tp++ = *fp++;
  354. fp += 8;
  355. }
  356. /*
  357.  * do the rest, if any.
  358.  */
  359. clen = len & 15;
  360. rtp = (unsigned char *) tp;
  361. rfp = (unsigned char *) fp;
  362. while (clen--) {
  363. *rtp++ = *rfp++;
  364. }
  365. }
  366. }
  367. /* Setup the Lance Rx and Tx rings */
  368. static void lance_init_ring(struct net_device *dev)
  369. {
  370. struct lance_private *lp = (struct lance_private *) dev->priv;
  371. volatile struct lance_init_block *ib;
  372. int leptr;
  373. int i;
  374. ib = (struct lance_init_block *) (dev->mem_start);
  375. /* Lock out other processes while setting up hardware */
  376. netif_stop_queue(dev);
  377. lp->rx_new = lp->tx_new = 0;
  378. lp->rx_old = lp->tx_old = 0;
  379. /* Copy the ethernet address to the lance init block.
  380.  * XXX bit 0 of the physical address registers has to be zero
  381.  */
  382. ib->phys_addr[0] = dev->dev_addr[0];
  383. ib->phys_addr[1] = dev->dev_addr[1];
  384. ib->phys_addr[4] = dev->dev_addr[2];
  385. ib->phys_addr[5] = dev->dev_addr[3];
  386. ib->phys_addr[8] = dev->dev_addr[4];
  387. ib->phys_addr[9] = dev->dev_addr[5];
  388. /* Setup the initialization block */
  389. /* Setup rx descriptor pointer */
  390. leptr = LANCE_ADDR(libdesc_offset(brx_ring, 0));
  391. ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
  392. ib->rx_ptr = leptr;
  393. if (ZERO)
  394. printk("RX ptr: %8.8x(%8.8x)n", leptr, libdesc_offset(brx_ring, 0));
  395. /* Setup tx descriptor pointer */
  396. leptr = LANCE_ADDR(libdesc_offset(btx_ring, 0));
  397. ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
  398. ib->tx_ptr = leptr;
  399. if (ZERO)
  400. printk("TX ptr: %8.8x(%8.8x)n", leptr, libdesc_offset(btx_ring, 0));
  401. if (ZERO)
  402. printk("TX rings:n");
  403. /* Setup the Tx ring entries */
  404. for (i = 0; i < TX_RING_SIZE; i++) {
  405. leptr = (int) lp->tx_buf_ptr_lnc[i];
  406. ib->btx_ring[i].tmd0 = leptr;
  407. ib->btx_ring[i].tmd1_hadr = leptr >> 16;
  408. ib->btx_ring[i].tmd1_bits = 0;
  409. ib->btx_ring[i].length = 0xf000; /* The ones required by tmd2 */
  410. ib->btx_ring[i].misc = 0;
  411. if (i < 3 && ZERO)
  412. printk("%d: 0x%8.8x(0x%8.8x)n", i, leptr, (int) lp->tx_buf_ptr_cpu[i]);
  413. }
  414. /* Setup the Rx ring entries */
  415. if (ZERO)
  416. printk("RX rings:n");
  417. for (i = 0; i < RX_RING_SIZE; i++) {
  418. leptr = (int) lp->rx_buf_ptr_lnc[i];
  419. ib->brx_ring[i].rmd0 = leptr;
  420. ib->brx_ring[i].rmd1_hadr = leptr >> 16;
  421. ib->brx_ring[i].rmd1_bits = LE_R1_OWN;
  422. ib->brx_ring[i].length = -RX_BUFF_SIZE | 0xf000;
  423. ib->brx_ring[i].mblength = 0;
  424. if (i < 3 && ZERO)
  425. printk("%d: 0x%8.8x(0x%8.8x)n", i, leptr, (int) lp->rx_buf_ptr_cpu[i]);
  426. }
  427. iob();
  428. }
  429. static int init_restart_lance(struct lance_private *lp)
  430. {
  431. volatile struct lance_regs *ll = lp->ll;
  432. int i;
  433. writereg(&ll->rap, LE_CSR0);
  434. writereg(&ll->rdp, LE_C0_INIT);
  435. /* Wait for the lance to complete initialization */
  436. for (i = 0; (i < 100) && !(ll->rdp & LE_C0_IDON); i++) {
  437. udelay(10);
  438. }
  439. if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
  440. printk("LANCE unopened after %d ticks, csr0=%4.4x.n", i, ll->rdp);
  441. return -1;
  442. }
  443. if ((ll->rdp & LE_C0_ERR)) {
  444. printk("LANCE unopened after %d ticks, csr0=%4.4x.n", i, ll->rdp);
  445. return -1;
  446. }
  447. writereg(&ll->rdp, LE_C0_IDON);
  448. writereg(&ll->rdp, LE_C0_STRT);
  449. writereg(&ll->rdp, LE_C0_INEA);
  450. return 0;
  451. }
  452. static int lance_rx(struct net_device *dev)
  453. {
  454. struct lance_private *lp = (struct lance_private *) dev->priv;
  455. volatile struct lance_init_block *ib;
  456. volatile struct lance_rx_desc *rd = 0;
  457. unsigned char bits;
  458. int len = 0;
  459. struct sk_buff *skb = 0;
  460. ib = (struct lance_init_block *) (dev->mem_start);
  461. #ifdef TEST_HITS
  462. {
  463. int i;
  464. printk("[");
  465. for (i = 0; i < RX_RING_SIZE; i++) {
  466. if (i == lp->rx_new)
  467. printk("%s", ib->brx_ring[i].rmd1_bits &
  468.      LE_R1_OWN ? "_" : "X");
  469. else
  470. printk("%s", ib->brx_ring[i].rmd1_bits &
  471.      LE_R1_OWN ? "." : "1");
  472. }
  473. printk("]");
  474. }
  475. #endif
  476. for (rd = &ib->brx_ring[lp->rx_new];
  477.      !((bits = rd->rmd1_bits) & LE_R1_OWN);
  478.      rd = &ib->brx_ring[lp->rx_new]) {
  479. /* We got an incomplete frame? */
  480. if ((bits & LE_R1_POK) != LE_R1_POK) {
  481. lp->stats.rx_over_errors++;
  482. lp->stats.rx_errors++;
  483. } else if (bits & LE_R1_ERR) {
  484. /* Count only the end frame as a rx error,
  485.  * not the beginning
  486.  */
  487. if (bits & LE_R1_BUF)
  488. lp->stats.rx_fifo_errors++;
  489. if (bits & LE_R1_CRC)
  490. lp->stats.rx_crc_errors++;
  491. if (bits & LE_R1_OFL)
  492. lp->stats.rx_over_errors++;
  493. if (bits & LE_R1_FRA)
  494. lp->stats.rx_frame_errors++;
  495. if (bits & LE_R1_EOP)
  496. lp->stats.rx_errors++;
  497. } else {
  498. len = (rd->mblength & 0xfff) - 4;
  499. skb = dev_alloc_skb(len + 2);
  500. if (skb == 0) {
  501. printk("%s: Memory squeeze, deferring packet.n",
  502.        dev->name);
  503. lp->stats.rx_dropped++;
  504. rd->mblength = 0;
  505. rd->rmd1_bits = LE_R1_OWN;
  506. lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
  507. return 0;
  508. }
  509. lp->stats.rx_bytes += len;
  510. skb->dev = dev;
  511. skb_reserve(skb, 2); /* 16 byte align */
  512. skb_put(skb, len); /* make room */
  513. cp_from_buf(lp->type, skb->data,
  514.     (char *)lp->rx_buf_ptr_cpu[lp->rx_new],
  515.     len);
  516. skb->protocol = eth_type_trans(skb, dev);
  517. netif_rx(skb);
  518. dev->last_rx = jiffies;
  519. lp->stats.rx_packets++;
  520. }
  521. /* Return the packet to the pool */
  522. rd->mblength = 0;
  523. rd->length = -RX_BUFF_SIZE | 0xf000;
  524. rd->rmd1_bits = LE_R1_OWN;
  525. lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
  526. }
  527. return 0;
  528. }
  529. static void lance_tx(struct net_device *dev)
  530. {
  531. struct lance_private *lp = (struct lance_private *) dev->priv;
  532. volatile struct lance_init_block *ib;
  533. volatile struct lance_regs *ll = lp->ll;
  534. volatile struct lance_tx_desc *td;
  535. int i, j;
  536. int status;
  537. ib = (struct lance_init_block *) (dev->mem_start);
  538. j = lp->tx_old;
  539. spin_lock(&lp->lock);
  540. for (i = j; i != lp->tx_new; i = j) {
  541. td = &ib->btx_ring[i];
  542. /* If we hit a packet not owned by us, stop */
  543. if (td->tmd1_bits & LE_T1_OWN)
  544. break;
  545. if (td->tmd1_bits & LE_T1_ERR) {
  546. status = td->misc;
  547. lp->stats.tx_errors++;
  548. if (status & LE_T3_RTY)
  549. lp->stats.tx_aborted_errors++;
  550. if (status & LE_T3_LCOL)
  551. lp->stats.tx_window_errors++;
  552. if (status & LE_T3_CLOS) {
  553. lp->stats.tx_carrier_errors++;
  554. printk("%s: Carrier Lostn", dev->name);
  555. /* Stop the lance */
  556. writereg(&ll->rap, LE_CSR0);
  557. writereg(&ll->rdp, LE_C0_STOP);
  558. lance_init_ring(dev);
  559. load_csrs(lp);
  560. init_restart_lance(lp);
  561. goto out;
  562. }
  563. /* Buffer errors and underflows turn off the
  564.  * transmitter, restart the adapter.
  565.  */
  566. if (status & (LE_T3_BUF | LE_T3_UFL)) {
  567. lp->stats.tx_fifo_errors++;
  568. printk("%s: Tx: ERR_BUF|ERR_UFL, restartingn",
  569.        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. } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
  579. /*
  580.  * So we don't count the packet more than once.
  581.  */
  582. td->tmd1_bits &= ~(LE_T1_POK);
  583. /* One collision before packet was sent. */
  584. if (td->tmd1_bits & LE_T1_EONE)
  585. lp->stats.collisions++;
  586. /* More than one collision, be optimistic. */
  587. if (td->tmd1_bits & LE_T1_EMORE)
  588. lp->stats.collisions += 2;
  589. lp->stats.tx_packets++;
  590. }
  591. j = (j + 1) & TX_RING_MOD_MASK;
  592. }
  593. lp->tx_old = j;
  594. out:
  595. if (netif_queue_stopped(dev) &&
  596.     TX_BUFFS_AVAIL > 0)
  597. netif_wake_queue(dev);
  598. spin_unlock(&lp->lock);
  599. }
  600. static void lance_dma_merr_int(const int irq, void *dev_id,
  601. struct pt_regs *regs)
  602. {
  603. struct net_device *dev = (struct net_device *) dev_id;
  604. printk("%s: DMA errorn", dev->name);
  605. }
  606. static void lance_interrupt(const int irq, void *dev_id, struct pt_regs *regs)
  607. {
  608. struct net_device *dev = (struct net_device *) dev_id;
  609. struct lance_private *lp = (struct lance_private *) dev->priv;
  610. volatile struct lance_regs *ll = lp->ll;
  611. int csr0;
  612. writereg(&ll->rap, LE_CSR0);
  613. csr0 = ll->rdp;
  614. /* Acknowledge all the interrupt sources ASAP */
  615. writereg(&ll->rdp, csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT));
  616. if ((csr0 & LE_C0_ERR)) {
  617. /* Clear the error condition */
  618. writereg(&ll->rdp, LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
  619.  LE_C0_CERR | LE_C0_MERR);
  620. }
  621. if (csr0 & LE_C0_RINT)
  622. lance_rx(dev);
  623. if (csr0 & LE_C0_TINT)
  624. lance_tx(dev);
  625. if (csr0 & LE_C0_BABL)
  626. lp->stats.tx_errors++;
  627. if (csr0 & LE_C0_MISS)
  628. lp->stats.rx_errors++;
  629. if (csr0 & LE_C0_MERR) {
  630. printk("%s: Memory error, status %04xn", dev->name, csr0);
  631. writereg(&ll->rdp, LE_C0_STOP);
  632. lance_init_ring(dev);
  633. load_csrs(lp);
  634. init_restart_lance(lp);
  635. netif_wake_queue(dev);
  636. }
  637. writereg(&ll->rdp, LE_C0_INEA);
  638. writereg(&ll->rdp, LE_C0_INEA);
  639. }
  640. struct net_device *last_dev = 0;
  641. static int lance_open(struct net_device *dev)
  642. {
  643. volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
  644. struct lance_private *lp = (struct lance_private *) dev->priv;
  645. volatile struct lance_regs *ll = lp->ll;
  646. int status = 0;
  647. last_dev = dev;
  648. /* Stop the Lance */
  649. writereg(&ll->rap, LE_CSR0);
  650. writereg(&ll->rdp, LE_C0_STOP);
  651. /* Set mode and clear multicast filter only at device open,
  652.  * so that lance_init_ring() called at any error will not
  653.  * forget multicast filters.
  654.  *
  655.  * BTW it is common bug in all lance drivers! --ANK
  656.  */
  657. ib->mode = 0;
  658. ib->filter [0] = 0;
  659. ib->filter [2] = 0;
  660. ib->filter [4] = 0;
  661. ib->filter [6] = 0;
  662. lance_init_ring(dev);
  663. load_csrs(lp);
  664. netif_start_queue(dev);
  665. /* Associate IRQ with lance_interrupt */
  666. if (request_irq(dev->irq, &lance_interrupt, 0, "lance", dev)) {
  667. printk("lance: Can't get IRQ %dn", dev->irq);
  668. return -EAGAIN;
  669. }
  670. if (lp->dma_irq >= 0) {
  671. if (request_irq(lp->dma_irq, &lance_dma_merr_int, 0,
  672. "lance error", dev)) {
  673. free_irq(dev->irq, dev);
  674. printk("lance: Can't get DMA IRQ %dn", lp->dma_irq);
  675. return -EAGAIN;
  676. }
  677. /* Enable I/O ASIC LANCE DMA.  */
  678. fast_wmb();
  679. ioasic_write(SSR, ioasic_read(SSR) | LANCE_DMA_EN);
  680. }
  681. status = init_restart_lance(lp);
  682. /*
  683.  * if (!status)
  684.  *      MOD_INC_USE_COUNT;
  685.  */
  686. return status;
  687. }
  688. static int lance_close(struct net_device *dev)
  689. {
  690. struct lance_private *lp = (struct lance_private *) dev->priv;
  691. volatile struct lance_regs *ll = lp->ll;
  692. netif_stop_queue(dev);
  693. del_timer_sync(&lp->multicast_timer);
  694. /* Stop the card */
  695. writereg(&ll->rap, LE_CSR0);
  696. writereg(&ll->rdp, LE_C0_STOP);
  697. if (lp->dma_irq >= 0) {
  698. /* Disable I/O ASIC LANCE DMA.  */
  699. ioasic_write(SSR, ioasic_read(SSR) & ~LANCE_DMA_EN);
  700. fast_iob();
  701. free_irq(lp->dma_irq, dev);
  702. }
  703. free_irq(dev->irq, dev);
  704. /*
  705.    MOD_DEC_USE_COUNT;
  706.  */
  707. return 0;
  708. }
  709. static inline int lance_reset(struct net_device *dev)
  710. {
  711. struct lance_private *lp = (struct lance_private *) dev->priv;
  712. volatile struct lance_regs *ll = lp->ll;
  713. int status;
  714. /* Stop the lance */
  715. writereg(&ll->rap, LE_CSR0);
  716. writereg(&ll->rdp, LE_C0_STOP);
  717. lance_init_ring(dev);
  718. load_csrs(lp);
  719. dev->trans_start = jiffies;
  720. status = init_restart_lance(lp);
  721. return status;
  722. }
  723. static void lance_tx_timeout(struct net_device *dev)
  724. {
  725. struct lance_private *lp = (struct lance_private *) dev->priv;
  726. volatile struct lance_regs *ll = lp->ll;
  727. printk(KERN_ERR "%s: transmit timed out, status %04x, resetn",
  728.        dev->name, ll->rdp);
  729. lance_reset(dev);
  730. netif_wake_queue(dev);
  731. }
  732. static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
  733. {
  734. struct lance_private *lp = (struct lance_private *) dev->priv;
  735. volatile struct lance_regs *ll = lp->ll;
  736. volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
  737. int entry, skblen, len;
  738. skblen = skb->len;
  739. len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
  740. lp->stats.tx_bytes += len;
  741. entry = lp->tx_new & TX_RING_MOD_MASK;
  742. ib->btx_ring[entry].length = (-len);
  743. ib->btx_ring[entry].misc = 0;
  744. cp_to_buf(lp->type, (char *)lp->tx_buf_ptr_cpu[entry], skb->data,
  745.   skblen);
  746. /* Clear the slack of the packet, do I need this? */
  747. /* For a firewall its a good idea - AC */
  748. /*
  749.    if (len != skblen)
  750.    memset ((char *) &ib->tx_buf [entry][skblen], 0, (len - skblen) << 1);
  751.  */
  752. /* Now, give the packet to the lance */
  753. ib->btx_ring[entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
  754. lp->tx_new = (lp->tx_new + 1) & TX_RING_MOD_MASK;
  755. if (TX_BUFFS_AVAIL <= 0)
  756. netif_stop_queue(dev);
  757. /* Kick the lance: transmit now */
  758. writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
  759. spin_unlock_irq(&lp->lock);
  760. dev->trans_start = jiffies;
  761. dev_kfree_skb(skb);
  762.   return 0;
  763. }
  764. static struct net_device_stats *lance_get_stats(struct net_device *dev)
  765. {
  766. struct lance_private *lp = (struct lance_private *) dev->priv;
  767. return &lp->stats;
  768. }
  769. static void lance_load_multicast(struct net_device *dev)
  770. {
  771. volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
  772. volatile u16 *mcast_table = (u16 *) & ib->filter;
  773. struct dev_mc_list *dmi = dev->mc_list;
  774. char *addrs;
  775. int i, j, bit, byte;
  776. u32 crc;
  777. /* set all multicast bits */
  778. if (dev->flags & IFF_ALLMULTI) {
  779. ib->filter[0] = 0xffff;
  780. ib->filter[2] = 0xffff;
  781. ib->filter[4] = 0xffff;
  782. ib->filter[6] = 0xffff;
  783. return;
  784. }
  785. /* clear the multicast filter */
  786. ib->filter[0] = 0;
  787. ib->filter[2] = 0;
  788. ib->filter[4] = 0;
  789. ib->filter[6] = 0;
  790. /* Add addresses */
  791. for (i = 0; i < dev->mc_count; i++) {
  792. addrs = dmi->dmi_addr;
  793. dmi = dmi->next;
  794. /* multicast address? */
  795. if (!(*addrs & 1))
  796. continue;
  797. crc = ether_crc_le(ETH_ALEN, addrs);
  798. crc = crc >> 26;
  799. mcast_table[2 * (crc >> 4)] |= 1 << (crc & 0xf);
  800. }
  801. return;
  802. }
  803. static void lance_set_multicast(struct net_device *dev)
  804. {
  805. struct lance_private *lp = (struct lance_private *) dev->priv;
  806. volatile struct lance_init_block *ib;
  807. volatile struct lance_regs *ll = lp->ll;
  808. ib = (struct lance_init_block *) (dev->mem_start);
  809. if (!netif_running(dev))
  810. return;
  811. if (lp->tx_old != lp->tx_new) {
  812. mod_timer(&lp->multicast_timer, jiffies + 4);
  813. netif_wake_queue(dev);
  814. return;
  815. }
  816. netif_stop_queue(dev);
  817. writereg(&ll->rap, LE_CSR0);
  818. writereg(&ll->rdp, LE_C0_STOP);
  819. lance_init_ring(dev);
  820. if (dev->flags & IFF_PROMISC) {
  821. ib->mode |= LE_MO_PROM;
  822. } else {
  823. ib->mode &= ~LE_MO_PROM;
  824. lance_load_multicast(dev);
  825. }
  826. load_csrs(lp);
  827. init_restart_lance(lp);
  828. netif_wake_queue(dev);
  829. }
  830. static void lance_set_multicast_retry(unsigned long _opaque)
  831. {
  832. struct net_device *dev = (struct net_device *) _opaque;
  833. lance_set_multicast(dev);
  834. }
  835. static int __init dec_lance_init(const int type, const int slot)
  836. {
  837. static unsigned version_printed;
  838. struct net_device *dev;
  839. struct lance_private *lp;
  840. volatile struct lance_regs *ll;
  841. int i, ret;
  842. unsigned long esar_base;
  843. unsigned char *esar;
  844. #ifndef CONFIG_TC
  845. system_base = KN01_LANCE_BASE;
  846. #endif
  847. if (dec_lance_debug && version_printed++ == 0)
  848. printk(version);
  849. dev = init_etherdev(NULL, sizeof(struct lance_private));
  850. if (!dev)
  851. return -ENOMEM;
  852. SET_MODULE_OWNER(dev);
  853. /*
  854.  * init_etherdev ensures the data structures used by the LANCE
  855.  * are aligned.
  856.  */
  857. lp = (struct lance_private *) dev->priv;
  858. spin_lock_init(&lp->lock);
  859. lp->type = type;
  860. lp->slot = slot;
  861. switch (type) {
  862. #ifdef CONFIG_TC
  863. case ASIC_LANCE:
  864. dev->base_addr = system_base + LANCE;
  865. /* buffer space for the on-board LANCE shared memory */
  866. /*
  867.  * FIXME: ugly hack!
  868.  */
  869. dev->mem_start = KSEG1ADDR(0x00020000);
  870. dev->mem_end = dev->mem_start + 0x00020000;
  871. dev->irq = dec_interrupt[DEC_IRQ_LANCE];
  872. esar_base = system_base + ESAR;
  873. /* Workaround crash with booting KN04 2.1k from Disk */
  874. memset((void *)dev->mem_start, 0,
  875.        dev->mem_end - dev->mem_start);
  876. /*
  877.  * setup the pointer arrays, this sucks [tm] :-(
  878.  */
  879. for (i = 0; i < RX_RING_SIZE; i++) {
  880. lp->rx_buf_ptr_cpu[i] =
  881. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  882.  2 * i * RX_BUFF_SIZE);
  883. lp->rx_buf_ptr_lnc[i] =
  884. (char *)(BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  885. }
  886. for (i = 0; i < TX_RING_SIZE; i++) {
  887. lp->tx_buf_ptr_cpu[i] =
  888. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  889.  2 * RX_RING_SIZE * RX_BUFF_SIZE +
  890.  2 * i * TX_BUFF_SIZE);
  891. lp->tx_buf_ptr_lnc[i] =
  892. (char *)(BUF_OFFSET_LNC +
  893.  RX_RING_SIZE * RX_BUFF_SIZE +
  894.  i * TX_BUFF_SIZE);
  895. }
  896. /* Setup I/O ASIC LANCE DMA.  */
  897. lp->dma_irq = dec_interrupt[DEC_IRQ_LANCE_MERR];
  898. ioasic_write(LANCE_DMA_P, PHYSADDR(dev->mem_start) << 3);
  899. break;
  900. case PMAD_LANCE:
  901. claim_tc_card(slot);
  902. dev->mem_start = get_tc_base_addr(slot);
  903. dev->base_addr = dev->mem_start + 0x100000;
  904. dev->irq = get_tc_irq_nr(slot);
  905. esar_base = dev->mem_start + 0x1c0002;
  906. lp->dma_irq = -1;
  907. for (i = 0; i < RX_RING_SIZE; i++) {
  908. lp->rx_buf_ptr_cpu[i] =
  909. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  910.  i * RX_BUFF_SIZE);
  911. lp->rx_buf_ptr_lnc[i] =
  912. (char *)(BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  913. }
  914. for (i = 0; i < TX_RING_SIZE; i++) {
  915. lp->tx_buf_ptr_cpu[i] =
  916. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  917.  RX_RING_SIZE * RX_BUFF_SIZE +
  918.  i * TX_BUFF_SIZE);
  919. lp->tx_buf_ptr_lnc[i] =
  920. (char *)(BUF_OFFSET_LNC +
  921.  RX_RING_SIZE * RX_BUFF_SIZE +
  922.  i * TX_BUFF_SIZE);
  923. }
  924. break;
  925. #endif
  926. case PMAX_LANCE:
  927. dev->irq = dec_interrupt[DEC_IRQ_LANCE];
  928. dev->base_addr = KN01_LANCE_BASE;
  929. dev->mem_start = KN01_LANCE_BASE + 0x01000000;
  930. esar_base = KN01_RTC_BASE + 1;
  931. lp->dma_irq = -1;
  932. /*
  933.  * setup the pointer arrays, this sucks [tm] :-(
  934.  */
  935. for (i = 0; i < RX_RING_SIZE; i++) {
  936. lp->rx_buf_ptr_cpu[i] =
  937. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  938.  2 * i * RX_BUFF_SIZE);
  939. lp->rx_buf_ptr_lnc[i] =
  940. (char *)(BUF_OFFSET_LNC + i * RX_BUFF_SIZE);
  941. }
  942. for (i = 0; i < TX_RING_SIZE; i++) {
  943. lp->tx_buf_ptr_cpu[i] =
  944. (char *)(dev->mem_start + BUF_OFFSET_CPU +
  945.  2 * RX_RING_SIZE * RX_BUFF_SIZE +
  946.  2 * i * TX_BUFF_SIZE);
  947. lp->tx_buf_ptr_lnc[i] =
  948. (char *)(BUF_OFFSET_LNC +
  949.  RX_RING_SIZE * RX_BUFF_SIZE +
  950.  i * TX_BUFF_SIZE);
  951. }
  952. break;
  953. default:
  954. printk("declance_init called with unknown typen");
  955. ret = -ENODEV;
  956. goto err_out;
  957. }
  958. ll = (struct lance_regs *) dev->base_addr;
  959. esar = (unsigned char *) esar_base;
  960. /* prom checks */
  961. /* First, check for test pattern */
  962. if (esar[0x60] != 0xff && esar[0x64] != 0x00 &&
  963.     esar[0x68] != 0x55 && esar[0x6c] != 0xaa) {
  964. printk("Ethernet station address prom not found!n");
  965. ret = -ENODEV;
  966. goto err_out;
  967. }
  968. /* Check the prom contents */
  969. for (i = 0; i < 8; i++) {
  970. if (esar[i * 4] != esar[0x3c - i * 4] &&
  971.     esar[i * 4] != esar[0x40 + i * 4] &&
  972.     esar[0x3c - i * 4] != esar[0x40 + i * 4]) {
  973. printk("Something is wrong with the ethernet "
  974.        "station address prom!n");
  975. ret = -ENODEV;
  976. goto err_out;
  977. }
  978. }
  979. lp->next = root_lance_dev;
  980. root_lance_dev = dev;
  981. /* Copy the ethernet address to the device structure, later to the
  982.  * lance initialization block so the lance gets it every time it's
  983.  * (re)initialized.
  984.  */
  985. switch (type) {
  986. case ASIC_LANCE:
  987. printk("%s: IOASIC onboard LANCE, addr = ", dev->name);
  988. break;
  989. case PMAD_LANCE:
  990. printk("%s: PMAD-AA, addr = ", dev->name);
  991. break;
  992. case PMAX_LANCE:
  993. printk("%s: PMAX onboard LANCE, addr = ", dev->name);
  994. break;
  995. }
  996. for (i = 0; i < 6; i++) {
  997. dev->dev_addr[i] = esar[i * 4];
  998. printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ',' : ':');
  999. }
  1000. printk(" irq = %dn", dev->irq);
  1001. dev->open = &lance_open;
  1002. dev->stop = &lance_close;
  1003. dev->hard_start_xmit = &lance_start_xmit;
  1004. dev->tx_timeout = &lance_tx_timeout;
  1005. dev->watchdog_timeo = 5*HZ;
  1006. dev->get_stats = &lance_get_stats;
  1007. dev->set_multicast_list = &lance_set_multicast;
  1008. /* lp->ll is the location of the registers for lance card */
  1009. lp->ll = ll;
  1010. /* busmaster_regval (CSR3) should be zero according to the PMAD-AA
  1011.  * specification.
  1012.  */
  1013. lp->busmaster_regval = 0;
  1014. dev->dma = 0;
  1015. /* We cannot sleep if the chip is busy during a
  1016.  * multicast list update event, because such events
  1017.  * can occur from interrupts (ex. IPv6).  So we
  1018.  * use a timer to try again later when necessary. -DaveM
  1019.  */
  1020. init_timer(&lp->multicast_timer);
  1021. lp->multicast_timer.data = (unsigned long) dev;
  1022. lp->multicast_timer.function = &lance_set_multicast_retry;
  1023. return 0;
  1024. err_out:
  1025. unregister_netdev(dev);
  1026. kfree(dev);
  1027. return ret;
  1028. }
  1029. /* Find all the lance cards on the system and initialize them */
  1030. static int __init dec_lance_probe(void)
  1031. {
  1032. int count = 0;
  1033. /* Scan slots for PMAD-AA cards first. */
  1034. #ifdef CONFIG_TC
  1035. if (TURBOCHANNEL) {
  1036. int slot;
  1037. while ((slot = search_tc_card("PMAD-AA")) >= 0) {
  1038. if (dec_lance_init(PMAD_LANCE, slot) < 0)
  1039. break;
  1040. count++;
  1041. }
  1042. }
  1043. #endif
  1044. /* Then handle onboard devices. */
  1045. if (dec_interrupt[DEC_IRQ_LANCE] >= 0) {
  1046. if (dec_interrupt[DEC_IRQ_LANCE_MERR] >= 0) {
  1047. #ifdef CONFIG_TC
  1048. if (dec_lance_init(ASIC_LANCE, -1) >= 0)
  1049. count++;
  1050. #endif
  1051. } else if (!TURBOCHANNEL) {
  1052. if (dec_lance_init(PMAX_LANCE, -1) >= 0)
  1053. count++;
  1054. }
  1055. }
  1056. return (count > 0) ? 0 : -ENODEV;
  1057. }
  1058. static void __exit dec_lance_cleanup(void)
  1059. {
  1060. while (root_lance_dev) {
  1061. struct net_device *dev = root_lance_dev;
  1062. struct lance_private *lp = (struct lance_private *)dev->priv;
  1063. #ifdef CONFIG_TC
  1064. if (lp->slot >= 0)
  1065. release_tc_card(lp->slot);
  1066. #endif
  1067. root_lance_dev = lp->next;
  1068. unregister_netdev(dev);
  1069. kfree(dev);
  1070. }
  1071. }
  1072. module_init(dec_lance_probe);
  1073. module_exit(dec_lance_cleanup);