declance.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:31k
源码类别:

嵌入式Linux

开发平台:

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.  *      additional sources:
  9.  *      - PMAD-AA TURBOchannel Ethernet Module Functional Specification,
  10.  *        Revision 1.2
  11.  *
  12.  *      History:
  13.  *
  14.  *      v0.001: The kernel accepts the code and it shows the hardware address.
  15.  *
  16.  *      v0.002: Removed most sparc stuff, left only some module and dma stuff.
  17.  *
  18.  *      v0.003: Enhanced base address calculation from proposals by
  19.  *      Harald Koerfgen and Thomas Riemer.
  20.  *
  21.  *      v0.004: lance-regs is pointing at the right addresses, added prom
  22.  *      check. First start of address mapping and DMA.
  23.  *
  24.  *      v0.005: started to play around with LANCE-DMA. This driver will not work
  25.  *      for non IOASIC lances. HK
  26.  *
  27.  *      v0.006: added pointer arrays to lance_private and setup routine for them
  28.  *      in dec_lance_init. HK
  29.  *
  30.  *      v0.007: Big shit. The LANCE seems to use a different DMA mechanism to access
  31.  *      the init block. This looks like one (short) word at a time, but the smallest
  32.  *      amount the IOASIC can transfer is a (long) word. So we have a 2-2 padding here.
  33.  *      Changed lance_init_block accordingly. The 16-16 padding for the buffers
  34.  *      seems to be correct. HK
  35.  *
  36.  *     v0.008 - mods to make PMAX_LANCE work. 01/09/1999 triemer
  37.  */
  38. #undef DEBUG_DRIVER
  39. static char *version =
  40. "declance.c: v0.008 by Linux Mips DECstation task forcen";
  41. static char *lancestr = "LANCE";
  42. /*
  43.  * card types
  44.  */
  45. #define ASIC_LANCE 1
  46. #define PMAD_LANCE 2
  47. #define PMAX_LANCE 3
  48. #include <linux/init.h>
  49. #include <linux/kernel.h>
  50. #include <linux/netdevice.h>
  51. #include <asm/dec/interrupts.h>
  52. #include <asm/dec/ioasic_ints.h>
  53. #include <asm/dec/ioasic_addrs.h>
  54. #include <asm/dec/machtype.h>
  55. #include <asm/dec/tc.h>
  56. #include <asm/dec/kn01.h>
  57. #include <asm/wbflush.h>
  58. #include <asm/addrspace.h>
  59. #include <linux/config.h>
  60. #include <linux/errno.h>
  61. #include <linux/hdreg.h>
  62. #include <linux/ioport.h>
  63. #include <linux/sched.h>
  64. #include <linux/mm.h>
  65. #include <linux/stddef.h>
  66. #include <linux/string.h>
  67. #include <linux/unistd.h>
  68. #include <linux/ptrace.h>
  69. #include <linux/slab.h>
  70. #include <linux/user.h>
  71. #include <linux/utsname.h>
  72. #include <linux/a.out.h>
  73. #include <linux/tty.h>
  74. #include <linux/delay.h>
  75. #include <asm/io.h>
  76. #include <linux/etherdevice.h>
  77. #ifndef CONFIG_TC
  78. unsigned long system_base;
  79. unsigned long dmaptr;
  80. #endif
  81. static int type;
  82. #define CRC_POLYNOMIAL_BE 0x04c11db7UL /* Ethernet CRC, big endian */
  83. #define CRC_POLYNOMIAL_LE 0xedb88320UL /* Ethernet CRC, little endian */
  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       0x80 /* Who owns the entry */
  110. #define LE_R1_ERR       0x40 /* Error: if FRA, OFL, CRC or BUF is set */
  111. #define LE_R1_FRA       0x20 /* FRA: Frame error */
  112. #define LE_R1_OFL       0x10 /* OFL: Frame overflow */
  113. #define LE_R1_CRC       0x08 /* CRC error */
  114. #define LE_R1_BUF       0x04 /* BUF: Buffer error */
  115. #define LE_R1_SOP       0x02 /* Start of packet */
  116. #define LE_R1_EOP       0x01 /* End of packet */
  117. #define LE_R1_POK       0x03 /* Packet is complete: SOP + EOP */
  118. #define LE_T1_OWN       0x80 /* Lance owns the packet */
  119. #define LE_T1_ERR       0x40 /* Error summary */
  120. #define LE_T1_EMORE     0x10 /* Error: more than one retry needed */
  121. #define LE_T1_EONE      0x08 /* Error: one retry needed */
  122. #define LE_T1_EDEF      0x04 /* Error: deferred */
  123. #define LE_T1_SOP       0x02 /* Start of packet */
  124. #define LE_T1_EOP       0x01 /* End of packet */
  125. #define LE_T1_POK 0x03 /* 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 DEBUG_DRIVER 1
  146. #define ZERO 0
  147. /* The DS2000/3000 have a linear 64 KB buffer.
  148.  * The PMAD-AA has 128 kb buffer on-board. 
  149.  *
  150.  * The IOASIC LANCE devices use a shared memory region. This region as seen 
  151.  * from the CPU is (max) 128 KB long and has to be on an 128 KB boundary.
  152.  * The LANCE sees this as a 64 KB long continuous memory region.
  153.  *
  154.  * The LANCE's DMA address is used as an index in this buffer and DMA takes
  155.  * place in bursts of eight 16-Bit words which are packed into four 32-Bit words
  156.  * by the IOASIC. This leads to a strange padding: 16 bytes of valid data followed
  157.  * by a 16 byte gap :-(.
  158.  */
  159. struct lance_rx_desc {
  160. unsigned short rmd0; /* low address of packet */
  161. short gap0;
  162. unsigned char rmd1_hadr; /* high address of packet */
  163. unsigned char rmd1_bits; /* descriptor bits */
  164. short gap1;
  165. short length; /* This length is 2s complement (negative)!
  166.    * Buffer length
  167.  */
  168. short gap2;
  169. unsigned short mblength; /* This is the actual number of bytes received */
  170. short gap3;
  171. };
  172. struct lance_tx_desc {
  173. unsigned short tmd0; /* low address of packet */
  174. short gap0;
  175. unsigned char tmd1_hadr; /* high address of packet */
  176. unsigned char tmd1_bits; /* descriptor bits */
  177. short gap1;
  178. short length; /* Length is 2s complement (negative)! */
  179. short gap2;
  180. unsigned short misc;
  181. short gap3;
  182. };
  183. /* First part of the LANCE initialization block, described in databook. */
  184. struct lance_init_block {
  185. unsigned short mode; /* Pre-set mode (reg. 15) */
  186. short gap0;
  187. unsigned char phys_addr[12]; /* Physical ethernet address
  188.    * only 0, 1, 4, 5, 8, 9 are valid
  189.    * 2, 3, 6, 7, 10, 11 are gaps
  190.  */
  191. unsigned short filter[8]; /* Multicast filter.
  192.    * only 0, 2, 4, 6 are valid
  193.    * 1, 3, 5, 7 are gaps
  194.  */
  195. /* Receive and transmit ring base, along with extra bits. */
  196. unsigned short rx_ptr; /* receive descriptor addr */
  197. short gap1;
  198. unsigned short rx_len; /* receive len and high addr */
  199. short gap2;
  200. unsigned short tx_ptr; /* transmit descriptor addr */
  201. short gap3;
  202. unsigned short tx_len; /* transmit len and high addr */
  203. short gap4;
  204. char gap5[16];
  205. /* The buffer descriptors */
  206. struct lance_rx_desc brx_ring[RX_RING_SIZE];
  207. struct lance_tx_desc btx_ring[TX_RING_SIZE];
  208. };
  209. #define BUF_OFFSET_CPU sizeof(struct lance_init_block)
  210. #define BUF_OFFSET_LNC (sizeof(struct lance_init_block)>>1)
  211. #define libdesc_offset(rt, elem) 
  212. ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
  213. /*
  214.  * This works *only* for the ring descriptors
  215.  */
  216. #define LANCE_ADDR(x) (PHYSADDR(x) >> 1)
  217. struct lance_private {
  218. char *name;
  219. volatile struct lance_regs *ll;
  220. volatile struct lance_init_block *init_block;
  221. volatile unsigned long *dma_ptr_reg;
  222. spinlock_t lock;
  223. int rx_new, tx_new;
  224. int rx_old, tx_old;
  225. struct net_device_stats stats;
  226. unsigned short busmaster_regval;
  227. struct net_device *dev; /* Backpointer        */
  228. struct lance_private *next_module;
  229. struct timer_list       multicast_timer;
  230. /* Pointers to the ring buffers as seen from the CPU */
  231. char *rx_buf_ptr_cpu[RX_RING_SIZE];
  232. char *tx_buf_ptr_cpu[TX_RING_SIZE];
  233. /* Pointers to the ring buffers as seen from the LANCE */
  234. char *rx_buf_ptr_lnc[RX_RING_SIZE];
  235. char *tx_buf_ptr_lnc[TX_RING_SIZE];
  236. };
  237. #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?
  238. lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:
  239. lp->tx_old - lp->tx_new-1)
  240. /* The lance control ports are at an absolute address, machine and tc-slot
  241.  * dependant.
  242.  * DECstations do only 32-bit access and the LANCE uses 16 bit addresses,
  243.  * so we have to give the structure an extra member making rap pointing
  244.  * at the right address
  245.  */
  246. struct lance_regs {
  247. volatile unsigned short rdp; /* register data port */
  248. unsigned short pad;
  249. volatile unsigned short rap; /* register address port */
  250. };
  251. int dec_lance_debug = 2;
  252. /*
  253.    #ifdef MODULE
  254.    static struct lance_private *root_lance_dev = NULL;
  255.    #endif
  256.  */
  257. static inline void writereg(volatile unsigned short *regptr, short value)
  258. {
  259. *regptr = value;
  260. wbflush();
  261. }
  262. /* Load the CSR registers */
  263. static void load_csrs(struct lance_private *lp)
  264. {
  265. volatile struct lance_regs *ll = lp->ll;
  266. int leptr;
  267. /* The address space as seen from the LANCE
  268.  * begins at address 0. HK
  269.  */
  270. leptr = 0;
  271. writereg(&ll->rap, LE_CSR1);
  272. writereg(&ll->rdp, (leptr & 0xFFFF));
  273. writereg(&ll->rap, LE_CSR2);
  274. writereg(&ll->rdp, leptr >> 16);
  275. writereg(&ll->rap, LE_CSR3);
  276. writereg(&ll->rdp, lp->busmaster_regval);
  277. /* Point back to csr0 */
  278. writereg(&ll->rap, LE_CSR0);
  279. }
  280. /*
  281.  * Our specialized copy routines
  282.  *
  283.  */
  284. void cp_to_buf(void *to, const void *from, __kernel_size_t len)
  285. {
  286. unsigned short *tp, *fp, clen;
  287. unsigned char *rtp, *rfp;
  288. if (type == PMAX_LANCE) {
  289. clen = len >> 1;
  290. tp = (unsigned short *) to;
  291. fp = (unsigned short *) from;
  292. while (clen--) {
  293. *tp++ = *fp++;
  294. tp++;
  295. }
  296. clen = len & 1;
  297. rtp = (unsigned char *) tp;
  298. rfp = (unsigned char *) fp;
  299. while (clen--) {
  300. *rtp++ = *rfp++;
  301. }
  302. } else {
  303. /*
  304.  * copy 16 Byte chunks
  305.  */
  306. clen = len >> 4;
  307. tp = (unsigned short *) to;
  308. fp = (unsigned short *) from;
  309. while (clen--) {
  310. *tp++ = *fp++;
  311. *tp++ = *fp++;
  312. *tp++ = *fp++;
  313. *tp++ = *fp++;
  314. *tp++ = *fp++;
  315. *tp++ = *fp++;
  316. *tp++ = *fp++;
  317. *tp++ = *fp++;
  318. tp += 8;
  319. }
  320. /*
  321.  * do the rest, if any.
  322.  */
  323. clen = len & 15;
  324. rtp = (unsigned char *) tp;
  325. rfp = (unsigned char *) fp;
  326. while (clen--) {
  327. *rtp++ = *rfp++;
  328. }
  329. }
  330. wbflush();
  331. }
  332. void cp_from_buf(void *to, unsigned char *from, int len)
  333. {
  334. unsigned short *tp, *fp, clen;
  335. unsigned char *rtp, *rfp;
  336. if (type == PMAX_LANCE) {
  337. clen = len >> 1;
  338. tp = (unsigned short *) to;
  339. fp = (unsigned short *) from;
  340. while (clen--) {
  341. *tp++ = *fp++;
  342. fp++;
  343. }
  344. clen = len & 1;
  345. rtp = (unsigned char *) tp;
  346. rfp = (unsigned char *) fp;
  347. while (clen--) {
  348. *rtp++ = *rfp++;
  349. }
  350. } else {
  351. /*
  352.  * copy 16 Byte chunks
  353.  */
  354. clen = len >> 4;
  355. tp = (unsigned short *) to;
  356. fp = (unsigned short *) from;
  357. while (clen--) {
  358. *tp++ = *fp++;
  359. *tp++ = *fp++;
  360. *tp++ = *fp++;
  361. *tp++ = *fp++;
  362. *tp++ = *fp++;
  363. *tp++ = *fp++;
  364. *tp++ = *fp++;
  365. *tp++ = *fp++;
  366. fp += 8;
  367. }
  368. /*
  369.  * do the rest, if any.
  370.  */
  371. clen = len & 15;
  372. rtp = (unsigned char *) tp;
  373. rfp = (unsigned char *) fp;
  374. while (clen--) {
  375. *rtp++ = *rfp++;
  376. }
  377. }
  378. }
  379. /* Setup the Lance Rx and Tx rings */
  380. static void lance_init_ring(struct net_device *dev)
  381. {
  382. struct lance_private *lp = (struct lance_private *) dev->priv;
  383. volatile struct lance_init_block *ib;
  384. int leptr;
  385. int i;
  386. ib = (struct lance_init_block *) (dev->mem_start);
  387. /* Lock out other processes while setting up hardware */
  388. netif_stop_queue(dev);
  389. lp->rx_new = lp->tx_new = 0;
  390. lp->rx_old = lp->tx_old = 0;
  391. /* Copy the ethernet address to the lance init block.
  392.  * XXX bit 0 of the physical address registers has to be zero
  393.  */
  394. ib->phys_addr[0] = dev->dev_addr[0];
  395. ib->phys_addr[1] = dev->dev_addr[1];
  396. ib->phys_addr[4] = dev->dev_addr[2];
  397. ib->phys_addr[5] = dev->dev_addr[3];
  398. ib->phys_addr[8] = dev->dev_addr[4];
  399. ib->phys_addr[9] = dev->dev_addr[5];
  400. /* Setup the initialization block */
  401. /* Setup rx descriptor pointer */
  402. leptr = LANCE_ADDR(libdesc_offset(brx_ring, 0));
  403. ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
  404. ib->rx_ptr = leptr;
  405. if (ZERO)
  406. printk("RX ptr: %8.8x(%8.8x)n", leptr, libdesc_offset(brx_ring, 0));
  407. /* Setup tx descriptor pointer */
  408. leptr = LANCE_ADDR(libdesc_offset(btx_ring, 0));
  409. ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
  410. ib->tx_ptr = leptr;
  411. if (ZERO)
  412. printk("TX ptr: %8.8x(%8.8x)n", leptr, libdesc_offset(btx_ring, 0));
  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 = (int) lp->tx_buf_ptr_lnc[i];
  418. ib->btx_ring[i].tmd0 = leptr;
  419. ib->btx_ring[i].tmd1_hadr = leptr >> 16;
  420. ib->btx_ring[i].tmd1_bits = 0;
  421. ib->btx_ring[i].length = 0xf000; /* The ones required by tmd2 */
  422. ib->btx_ring[i].misc = 0;
  423. if (i < 3 && ZERO)
  424. printk("%d: 0x%8.8x(0x%8.8x)n", i, leptr, (int) lp->tx_buf_ptr_cpu[i]);
  425. }
  426. /* Setup the Rx ring entries */
  427. if (ZERO)
  428. printk("RX rings:n");
  429. for (i = 0; i < RX_RING_SIZE; i++) {
  430. leptr = (int) lp->rx_buf_ptr_lnc[i];
  431. ib->brx_ring[i].rmd0 = leptr;
  432. ib->brx_ring[i].rmd1_hadr = leptr >> 16;
  433. ib->brx_ring[i].rmd1_bits = LE_R1_OWN;
  434. ib->brx_ring[i].length = -RX_BUFF_SIZE | 0xf000;
  435. ib->brx_ring[i].mblength = 0;
  436. if (i < 3 && ZERO)
  437. printk("%d: 0x%8.8x(0x%8.8x)n", i, leptr, (int) lp->rx_buf_ptr_cpu[i]);
  438. }
  439. wbflush();
  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", i, ll->rdp);
  453. return -1;
  454. }
  455. if ((ll->rdp & LE_C0_ERR)) {
  456. printk("LANCE unopened after %d ticks, csr0=%4.4x.n", i, ll->rdp);
  457. return -1;
  458. }
  459. writereg(&ll->rdp, LE_C0_IDON);
  460. writereg(&ll->rdp, LE_C0_STRT);
  461. writereg(&ll->rdp, LE_C0_INEA);
  462. return 0;
  463. }
  464. static int lance_rx(struct net_device *dev)
  465. {
  466. struct lance_private *lp = (struct lance_private *) dev->priv;
  467. volatile struct lance_init_block *ib;
  468. volatile struct lance_rx_desc *rd = 0;
  469. unsigned char bits;
  470. int len = 0;
  471. struct sk_buff *skb = 0;
  472. ib = (struct lance_init_block *) (dev->mem_start);
  473. #ifdef TEST_HITS
  474. {
  475. int i;
  476. printk("[");
  477. for (i = 0; i < RX_RING_SIZE; i++) {
  478. if (i == lp->rx_new)
  479. printk("%s",
  480.        ib->brx_ring[i].rmd1_bits & LE_R1_OWN ? "_" : "X");
  481. else
  482. printk("%s",
  483.        ib->brx_ring[i].rmd1_bits & LE_R1_OWN ? "." : "1");
  484. }
  485. printk("]");
  486. }
  487. #endif
  488. for (rd = &ib->brx_ring[lp->rx_new];
  489.      !((bits = rd->rmd1_bits) & LE_R1_OWN);
  490.      rd = &ib->brx_ring[lp->rx_new]) {
  491. /* We got an incomplete frame? */
  492. if ((bits & LE_R1_POK) != LE_R1_POK) {
  493. lp->stats.rx_over_errors++;
  494. lp->stats.rx_errors++;
  495. } else if (bits & LE_R1_ERR) {
  496. /* Count only the end frame as a rx error,
  497.  * not the beginning
  498.  */
  499. if (bits & LE_R1_BUF)
  500. lp->stats.rx_fifo_errors++;
  501. if (bits & LE_R1_CRC)
  502. lp->stats.rx_crc_errors++;
  503. if (bits & LE_R1_OFL)
  504. lp->stats.rx_over_errors++;
  505. if (bits & LE_R1_FRA)
  506. lp->stats.rx_frame_errors++;
  507. if (bits & LE_R1_EOP)
  508. lp->stats.rx_errors++;
  509. } else {
  510. len = (rd->mblength & 0xfff) - 4;
  511. skb = dev_alloc_skb(len + 2);
  512. if (skb == 0) {
  513. printk("%s: Memory squeeze, deferring packet.n",
  514.        dev->name);
  515. lp->stats.rx_dropped++;
  516. rd->mblength = 0;
  517. rd->rmd1_bits = LE_R1_OWN;
  518. lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
  519. return 0;
  520. }
  521. lp->stats.rx_bytes += len;
  522. skb->dev = dev;
  523. skb_reserve(skb, 2); /* 16 byte align */
  524. skb_put(skb, len); /* make room */
  525. cp_from_buf(skb->data,
  526.  (char *) lp->rx_buf_ptr_cpu[lp->rx_new],
  527.     len);
  528. skb->protocol = eth_type_trans(skb, dev);
  529. netif_rx(skb);
  530. dev->last_rx = jiffies;
  531. lp->stats.rx_packets++;
  532. }
  533. /* Return the packet to the pool */
  534. rd->mblength = 0;
  535. rd->length = -RX_BUFF_SIZE | 0xf000;
  536. rd->rmd1_bits = LE_R1_OWN;
  537. lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
  538. }
  539. return 0;
  540. }
  541. static void lance_tx(struct net_device *dev)
  542. {
  543. struct lance_private *lp = (struct lance_private *) dev->priv;
  544. volatile struct lance_init_block *ib;
  545. volatile struct lance_regs *ll = lp->ll;
  546. volatile struct lance_tx_desc *td;
  547. int i, j;
  548. int status;
  549. ib = (struct lance_init_block *) (dev->mem_start);
  550. j = lp->tx_old;
  551. spin_lock(&lp->lock);
  552. for (i = j; i != lp->tx_new; i = j) {
  553. td = &ib->btx_ring[i];
  554. /* If we hit a packet not owned by us, stop */
  555. if (td->tmd1_bits & LE_T1_OWN)
  556. break;
  557. if (td->tmd1_bits & LE_T1_ERR) {
  558. status = td->misc;
  559. lp->stats.tx_errors++;
  560. if (status & LE_T3_RTY)
  561. lp->stats.tx_aborted_errors++;
  562. if (status & LE_T3_LCOL)
  563. lp->stats.tx_window_errors++;
  564. if (status & LE_T3_CLOS) {
  565. lp->stats.tx_carrier_errors++;
  566. printk("%s: Carrier Lostn", dev->name);
  567. /* Stop the lance */
  568. writereg(&ll->rap, LE_CSR0);
  569. writereg(&ll->rdp, LE_C0_STOP);
  570. lance_init_ring(dev);
  571. load_csrs(lp);
  572. init_restart_lance(lp);
  573. goto out;
  574. }
  575. /* Buffer errors and underflows turn off the
  576.  * transmitter, restart the adapter.
  577.  */
  578. if (status & (LE_T3_BUF | LE_T3_UFL)) {
  579. lp->stats.tx_fifo_errors++;
  580. printk("%s: Tx: ERR_BUF|ERR_UFL, restartingn",
  581.        dev->name);
  582. /* Stop the lance */
  583. writereg(&ll->rap, LE_CSR0);
  584. writereg(&ll->rdp, LE_C0_STOP);
  585. lance_init_ring(dev);
  586. load_csrs(lp);
  587. init_restart_lance(lp);
  588. goto out;
  589. }
  590. } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
  591. /*
  592.  * So we don't count the packet more than once.
  593.  */
  594. td->tmd1_bits &= ~(LE_T1_POK);
  595. /* One collision before packet was sent. */
  596. if (td->tmd1_bits & LE_T1_EONE)
  597. lp->stats.collisions++;
  598. /* More than one collision, be optimistic. */
  599. if (td->tmd1_bits & LE_T1_EMORE)
  600. lp->stats.collisions += 2;
  601. lp->stats.tx_packets++;
  602. }
  603. j = (j + 1) & TX_RING_MOD_MASK;
  604. }
  605. lp->tx_old = j;
  606. out:
  607. if (netif_queue_stopped(dev) &&
  608.     TX_BUFFS_AVAIL > 0)
  609. netif_wake_queue(dev);
  610. spin_unlock(&lp->lock);
  611. }
  612. static void lance_interrupt(const int irq, void *dev_id, struct pt_regs *regs)
  613. {
  614. struct net_device *dev = (struct net_device *) dev_id;
  615. struct lance_private *lp = (struct lance_private *) dev->priv;
  616. volatile struct lance_regs *ll = lp->ll;
  617. int csr0;
  618. writereg(&ll->rap, LE_CSR0);
  619. csr0 = ll->rdp;
  620. /* Acknowledge all the interrupt sources ASAP */
  621. writereg(&ll->rdp, csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT));
  622. if ((csr0 & LE_C0_ERR)) {
  623. /* Clear the error condition */
  624. writereg(&ll->rdp, LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
  625.  LE_C0_CERR | LE_C0_MERR);
  626. }
  627. if (csr0 & LE_C0_RINT)
  628. lance_rx(dev);
  629. if (csr0 & LE_C0_TINT)
  630. lance_tx(dev);
  631. if (csr0 & LE_C0_BABL)
  632. lp->stats.tx_errors++;
  633. if (csr0 & LE_C0_MISS)
  634. lp->stats.rx_errors++;
  635. if (csr0 & LE_C0_MERR) {
  636. volatile unsigned long int_stat = *(unsigned long *) (system_base + IOCTL + SIR);
  637. printk("%s: Memory error, status %04xn", dev->name, csr0);
  638. if (int_stat & LANCE_DMA_MEMRDERR) {
  639. printk("%s: DMA errorn", dev->name);
  640. int_stat |= LANCE_DMA_MEMRDERR;
  641. /*
  642.  * re-enable LANCE DMA
  643.  */
  644. *(unsigned long *) (system_base + IOCTL + SSR) |= (1 << 16);
  645. wbflush();
  646. }
  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. }
  656. struct net_device *last_dev = 0;
  657. static int lance_open(struct net_device *dev)
  658. {
  659. volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
  660. struct lance_private *lp = (struct lance_private *) dev->priv;
  661. volatile struct lance_regs *ll = lp->ll;
  662. int status = 0;
  663. last_dev = dev;
  664. /* Stop the Lance */
  665. writereg(&ll->rap, LE_CSR0);
  666. writereg(&ll->rdp, LE_C0_STOP);
  667. /* Set mode and clear multicast filter only at device open,
  668.  * so that lance_init_ring() called at any error will not
  669.  * forget multicast filters.
  670.  *
  671.  * BTW it is common bug in all lance drivers! --ANK
  672.  */
  673. ib->mode = 0;
  674. ib->filter [0] = 0;
  675. ib->filter [2] = 0;
  676. lance_init_ring(dev);
  677. load_csrs(lp);
  678. netif_start_queue(dev);
  679. /* Associate IRQ with lance_interrupt */
  680. if (request_irq(dev->irq, &lance_interrupt, 0, lp->name, dev)) {
  681. printk("Lance: Can't get irq %dn", dev->irq);
  682. return -EAGAIN;
  683. }
  684. status = init_restart_lance(lp);
  685. /*
  686.  * if (!status)
  687.  *      MOD_INC_USE_COUNT;
  688.  */
  689. return status;
  690. }
  691. static int lance_close(struct net_device *dev)
  692. {
  693. struct lance_private *lp = (struct lance_private *) dev->priv;
  694. volatile struct lance_regs *ll = lp->ll;
  695. netif_stop_queue(dev);
  696. del_timer_sync(&lp->multicast_timer);
  697. /* Stop the card */
  698. writereg(&ll->rap, LE_CSR0);
  699. writereg(&ll->rdp, LE_C0_STOP);
  700. free_irq(dev->irq, (void *) dev);
  701. /*
  702.    MOD_DEC_USE_COUNT;
  703.  */
  704. return 0;
  705. }
  706. static inline int lance_reset(struct net_device *dev)
  707. {
  708. struct lance_private *lp = (struct lance_private *) dev->priv;
  709. volatile struct lance_regs *ll = lp->ll;
  710. int status;
  711. /* Stop the lance */
  712. writereg(&ll->rap, LE_CSR0);
  713. writereg(&ll->rdp, LE_C0_STOP);
  714. lance_init_ring(dev);
  715. load_csrs(lp);
  716. dev->trans_start = jiffies;
  717. status = init_restart_lance(lp);
  718. return status;
  719. }
  720. static void lance_tx_timeout(struct net_device *dev)
  721. {
  722. struct lance_private *lp = (struct lance_private *) dev->priv;
  723. volatile struct lance_regs *ll = lp->ll;
  724. printk(KERN_ERR "%s: transmit timed out, status %04x, resetn",
  725.        dev->name, ll->rdp);
  726. lance_reset(dev);
  727. netif_wake_queue(dev);
  728. }
  729. static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
  730. {
  731. struct lance_private *lp = (struct lance_private *) dev->priv;
  732. volatile struct lance_regs *ll = lp->ll;
  733. volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
  734. int entry, skblen, len;
  735. skblen = skb->len;
  736. len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
  737. lp->stats.tx_bytes += len;
  738. entry = lp->tx_new & TX_RING_MOD_MASK;
  739. ib->btx_ring[entry].length = (-len);
  740. ib->btx_ring[entry].misc = 0;
  741. cp_to_buf((char *) lp->tx_buf_ptr_cpu[entry], skb->data, skblen);
  742. /* Clear the slack of the packet, do I need this? */
  743. /* For a firewall its a good idea - AC */
  744. /*
  745.    if (len != skblen)
  746.    memset ((char *) &ib->tx_buf [entry][skblen], 0, (len - skblen) << 1);
  747.  */
  748. /* Now, give the packet to the lance */
  749. ib->btx_ring[entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
  750. lp->tx_new = (lp->tx_new + 1) & TX_RING_MOD_MASK;
  751. if (TX_BUFFS_AVAIL <= 0)
  752. netif_stop_queue(dev);
  753. /* Kick the lance: transmit now */
  754. writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
  755. spin_unlock_irq(&lp->lock);
  756. dev->trans_start = jiffies;
  757. dev_kfree_skb(skb);
  758.   return 0;
  759. }
  760. static struct net_device_stats *lance_get_stats(struct net_device *dev)
  761. {
  762. struct lance_private *lp = (struct lance_private *) dev->priv;
  763. return &lp->stats;
  764. }
  765. static void lance_load_multicast(struct net_device *dev)
  766. {
  767. volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
  768. volatile u16 *mcast_table = (u16 *) & ib->filter;
  769. struct dev_mc_list *dmi = dev->mc_list;
  770. char *addrs;
  771. int i, j, bit, byte;
  772. u32 crc, poly = CRC_POLYNOMIAL_BE;
  773. /* set all multicast bits */
  774. if (dev->flags & IFF_ALLMULTI) {
  775. ib->filter[0] = 0xffff;
  776. ib->filter[2] = 0xffff;
  777. ib->filter[4] = 0xffff;
  778. ib->filter[6] = 0xffff;
  779. return;
  780. }
  781. /* clear the multicast filter */
  782. ib->filter[0] = 0;
  783. ib->filter[2] = 0;
  784. ib->filter[4] = 0;
  785. ib->filter[6] = 0;
  786. /* Add addresses */
  787. for (i = 0; i < dev->mc_count; i++) {
  788. addrs = dmi->dmi_addr;
  789. dmi = dmi->next;
  790. /* multicast address? */
  791. if (!(*addrs & 1))
  792. continue;
  793. crc = 0xffffffff;
  794. for (byte = 0; byte < 6; byte++)
  795. for (bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
  796. int test;
  797. test = ((bit ^ crc) & 0x01);
  798. crc >>= 1;
  799. if (test) {
  800. crc = crc ^ poly;
  801. }
  802. }
  803. crc = crc >> 26;
  804. mcast_table[crc >> 3] |= 1 << (crc & 0xf);
  805. }
  806. return;
  807. }
  808. static void lance_set_multicast(struct net_device *dev)
  809. {
  810. struct lance_private *lp = (struct lance_private *) dev->priv;
  811. volatile struct lance_init_block *ib;
  812. volatile struct lance_regs *ll = lp->ll;
  813. ib = (struct lance_init_block *) (dev->mem_start);
  814. if (!netif_running(dev))
  815. return;
  816. if (lp->tx_old != lp->tx_new) {
  817. mod_timer(&lp->multicast_timer, jiffies + 4);
  818. netif_wake_queue(dev);
  819. return;
  820. }
  821. netif_stop_queue(dev);
  822. writereg(&ll->rap, LE_CSR0);
  823. writereg(&ll->rdp, LE_C0_STOP);
  824. lance_init_ring(dev);
  825. if (dev->flags & IFF_PROMISC) {
  826. ib->mode |= LE_MO_PROM;
  827. } else {
  828. ib->mode &= ~LE_MO_PROM;
  829. lance_load_multicast(dev);
  830. }
  831. load_csrs(lp);
  832. init_restart_lance(lp);
  833. netif_wake_queue(dev);
  834. }
  835. static void lance_set_multicast_retry(unsigned long _opaque)
  836. {
  837. struct net_device *dev = (struct net_device *) _opaque;
  838. lance_set_multicast(dev);
  839. }
  840. static int __init dec_lance_init(struct net_device *dev, const int type)
  841. {
  842. static unsigned version_printed;
  843. struct net_device *dev;
  844. struct lance_private *lp;
  845. volatile struct lance_regs *ll;
  846. int i, ret;
  847. unsigned long esar_base;
  848. unsigned char *esar;
  849. #ifndef CONFIG_TC
  850. system_base = KN01_LANCE_BASE;
  851. #else
  852. int slot;
  853. #endif
  854. if (dec_lance_debug && version_printed++ == 0)
  855. printk(version);
  856. dev = init_etherdev(0, sizeof(struct lance_private));
  857. if (!dev)
  858. return -ENOMEM;
  859. /* init_etherdev ensures the data structures used by the LANCE are aligned. */
  860. lp = (struct lance_private *) dev->priv;
  861. spin_lock_init(&lp->lock);
  862. switch (type) {
  863. #ifdef CONFIG_TC
  864. case ASIC_LANCE:
  865. dev->base_addr = system_base + LANCE;
  866. /* buffer space for the on-board LANCE shared memory */
  867. /*
  868.  * FIXME: ugly hack!
  869.  */
  870. dev->mem_start = KSEG1ADDR(0x00020000);
  871. dev->mem_end = dev->mem_start + 0x00020000;
  872. dev->irq = ETHER;
  873. esar_base = system_base + ESAR;
  874. /* Workaround crash with booting KN04 2.1k from Disk */
  875. memset(dev->mem_start, 0, 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] = (char *) (dev->mem_start + BUF_OFFSET_CPU
  881.  + 2 * i * RX_BUFF_SIZE);
  882. lp->rx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
  883.      + i * RX_BUFF_SIZE);
  884. }
  885. for (i = 0; i < TX_RING_SIZE; i++) {
  886. lp->tx_buf_ptr_cpu[i] = (char *) (dev->mem_start + BUF_OFFSET_CPU
  887. + 2 * RX_RING_SIZE * RX_BUFF_SIZE
  888.  + 2 * i * TX_BUFF_SIZE);
  889. lp->tx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
  890.     + RX_RING_SIZE * RX_BUFF_SIZE
  891.      + i * TX_BUFF_SIZE);
  892. }
  893. /*
  894.  * setup and enable IOASIC LANCE DMA
  895.  */
  896. lp->dma_ptr_reg = (unsigned long *) (system_base + IOCTL + LANCE_DMA_P);
  897. *(lp->dma_ptr_reg) = PHYSADDR(dev->mem_start) << 3;
  898. *(unsigned long *) (system_base + IOCTL + SSR) |= (1 << 16);
  899. wbflush();
  900. break;
  901. case PMAD_LANCE:
  902. slot = search_tc_card("PMAD-AA");
  903. claim_tc_card(slot);
  904. dev->mem_start = get_tc_base_addr(slot);
  905. dev->base_addr = dev->mem_start + 0x100000;
  906. dev->irq = get_tc_irq_nr(slot);
  907. esar_base = dev->mem_start + 0x1c0002;
  908. break;
  909. #endif
  910. case PMAX_LANCE:
  911. dev->irq = ETHER;
  912. dev->base_addr = KN01_LANCE_BASE;
  913. dev->mem_start = KN01_LANCE_BASE + 0x01000000;
  914. esar_base = KN01_RTC_BASE + 1;
  915. /*
  916.  * setup the pointer arrays, this sucks [tm] :-(
  917.  */
  918. for (i = 0; i < RX_RING_SIZE; i++) {
  919. lp->rx_buf_ptr_cpu[i] =
  920.     (char *) (dev->mem_start + BUF_OFFSET_CPU
  921.       + 2 * i * RX_BUFF_SIZE);
  922. lp->rx_buf_ptr_lnc[i] =
  923.     (char *) (BUF_OFFSET_LNC
  924.       + i * RX_BUFF_SIZE);
  925. }
  926. for (i = 0; i < TX_RING_SIZE; i++) {
  927. lp->tx_buf_ptr_cpu[i] =
  928.     (char *) (dev->mem_start + BUF_OFFSET_CPU
  929.       + 2 * RX_RING_SIZE * RX_BUFF_SIZE
  930.       + 2 * i * TX_BUFF_SIZE);
  931. lp->tx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
  932.     + RX_RING_SIZE * RX_BUFF_SIZE
  933.      + i * TX_BUFF_SIZE);
  934. }
  935. break;
  936. default:
  937. printk("declance_init called with unknown typen");
  938. ret = -ENODEV;
  939. goto err_out;
  940. }
  941. ll = (struct lance_regs *) dev->base_addr;
  942. esar = (unsigned char *) esar_base;
  943. /* prom checks */
  944. /* First, check for test pattern */
  945. if (esar[0x60] != 0xff && esar[0x64] != 0x00 &&
  946.     esar[0x68] != 0x55 && esar[0x6c] != 0xaa) {
  947. printk("Ethernet station address prom not found!n");
  948. ret = -ENODEV;
  949. goto err_out;
  950. }
  951. /* Check the prom contents */
  952. for (i = 0; i < 8; i++) {
  953. if (esar[i * 4] != esar[0x3c - i * 4] &&
  954.     esar[i * 4] != esar[0x40 + i * 4] &&
  955.     esar[0x3c - i * 4] != esar[0x40 + i * 4]) {
  956. printk("Something is wrong with the ethernet "
  957.        "station address prom!n");
  958. ret = -ENODEV;
  959. goto err_out;
  960. }
  961. }
  962. /* Copy the ethernet address to the device structure, later to the
  963.  * lance initialization block so the lance gets it every time it's
  964.  * (re)initialized.
  965.  */
  966. switch (type) {
  967. case ASIC_LANCE:
  968. printk("%s: IOASIC onboard LANCE, addr = ", dev->name);
  969. break;
  970. case PMAD_LANCE:
  971. printk("%s: PMAD-AA, addr = ", dev->name);
  972. break;
  973. case PMAX_LANCE:
  974. printk("%s: PMAX onboard LANCE, addr = ", dev->name);
  975. break;
  976. }
  977. for (i = 0; i < 6; i++) {
  978. dev->dev_addr[i] = esar[i * 4];
  979. printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ',' : ':');
  980. }
  981. printk(" irq = %dn", dev->irq);
  982. lp->dev = dev;
  983. dev->open = &lance_open;
  984. dev->stop = &lance_close;
  985. dev->hard_start_xmit = &lance_start_xmit;
  986. dev->tx_timeout = &lance_tx_timeout;
  987. dev->watchdog_timeo = 5*HZ;
  988. dev->get_stats = &lance_get_stats;
  989. dev->set_multicast_list = &lance_set_multicast;
  990. /* lp->ll is the location of the registers for lance card */
  991. lp->ll = ll;
  992. lp->name = lancestr;
  993. /* busmaster_regval (CSR3) should be zero according to the PMAD-AA
  994.  * specification.
  995.  */
  996. lp->busmaster_regval = 0;
  997. dev->dma = 0;
  998. ether_setup(dev);
  999. /* We cannot sleep if the chip is busy during a
  1000.  * multicast list update event, because such events
  1001.  * can occur from interrupts (ex. IPv6).  So we
  1002.  * use a timer to try again later when necessary. -DaveM
  1003.  */
  1004. init_timer(&lp->multicast_timer);
  1005. lp->multicast_timer.data = (unsigned long) dev;
  1006. lp->multicast_timer.function = &lance_set_multicast_retry;
  1007. #ifdef MODULE
  1008. dev->ifindex = dev_new_index();
  1009. lp->next_module = root_lance_dev;
  1010. root_lance_dev = lp;
  1011. #endif
  1012. return 0;
  1013. err_out:
  1014. unregister_netdev(dev);
  1015. kfree(dev);
  1016. return ret;
  1017. }
  1018. /* Find all the lance cards on the system and initialize them */
  1019. static int __init dec_lance_probe(void)
  1020. {
  1021. struct net_device *dev = NULL;
  1022. static int called;
  1023. #ifdef MODULE
  1024. root_lance_dev = NULL;
  1025. #endif
  1026. #ifdef CONFIG_TC
  1027. int slot = -1;
  1028. if (TURBOCHANNEL) {
  1029. if (IOASIC && !called) {
  1030. called = 1;
  1031. type = ASIC_LANCE;
  1032. } else {
  1033. if ((slot = search_tc_card("PMAD-AA")) >= 0) {
  1034. type = PMAD_LANCE;
  1035. } else {
  1036. return -ENODEV;
  1037. }
  1038. }
  1039. } else {
  1040. if (!called) {
  1041. called = 1;
  1042. type = PMAX_LANCE;
  1043. } else {
  1044. return -ENODEV;
  1045. }
  1046. }
  1047. #else
  1048. if (!called && !TURBOCHANNEL) {
  1049. called = 1;
  1050. type = PMAX_LANCE;
  1051. } else {
  1052. return -ENODEV;
  1053. }
  1054. #endif
  1055. return dec_lance_init(dev, type);
  1056. }
  1057. static void __exit dec_lance_cleanup(void)
  1058. {
  1059. #ifdef MODULE
  1060.    struct lance_private *lp;
  1061.    while (root_lance_dev) {
  1062.    lp = root_lance_dev->next_module;
  1063.    unregister_netdev(root_lance_dev->dev);
  1064.    kfree(root_lance_dev->dev);
  1065.    root_lance_dev = lp;
  1066.    }
  1067. #endif /* MODULE */
  1068. }
  1069. module_init(dec_lance_probe);
  1070. module_exit(dec_lance_cleanup);