sonic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * sonic.c
  3. *
  4. * (C) 2005 Finn Thain
  5. *
  6. * Converted to DMA API, added zero-copy buffer handling, and
  7. * (from the mac68k project) introduced dhd's support for 16-bit cards.
  8. *
  9. * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
  10. *
  11. * This driver is based on work from Andreas Busse, but most of
  12. * the code is rewritten.
  13. *
  14. * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
  15. *
  16. * Core code included by system sonic drivers
  17. *
  18. * And... partially rewritten again by David Huggins-Daines in order
  19. * to cope with screwed up Macintosh NICs that may or may not use
  20. * 16-bit DMA.
  21. *
  22. * (C) 1999 David Huggins-Daines <dhd@debian.org>
  23. *
  24. */
  25. /*
  26. * Sources: Olivetti M700-10 Risc Personal Computer hardware handbook,
  27. * National Semiconductors data sheet for the DP83932B Sonic Ethernet
  28. * controller, and the files "8390.c" and "skeleton.c" in this directory.
  29. *
  30. * Additional sources: Nat Semi data sheet for the DP83932C and Nat Semi
  31. * Application Note AN-746, the files "lance.c" and "ibmlana.c". See also
  32. * the NetBSD file "sys/arch/mac68k/dev/if_sn.c".
  33. */
  34. /*
  35. * Open/initialize the SONIC controller.
  36. *
  37. * This routine should set everything up anew at each open, even
  38. * registers that "should" only need to be set once at boot, so that
  39. * there is non-reboot way to recover if something goes wrong.
  40. */
  41. static int sonic_open(struct net_device *dev)
  42. {
  43. struct sonic_local *lp = netdev_priv(dev);
  44. int i;
  45. if (sonic_debug > 2)
  46. printk("sonic_open: initializing sonic driver.\n");
  47. /*
  48. * We don't need to deal with auto-irq stuff since we
  49. * hardwire the sonic interrupt.
  50. */
  51. /*
  52. * XXX Horrible work around: We install sonic_interrupt as fast interrupt.
  53. * This means that during execution of the handler interrupt are disabled
  54. * covering another bug otherwise corrupting data. This doesn't mean
  55. * this glue works ok under all situations.
  56. *
  57. * Note (dhd): this also appears to prevent lockups on the Macintrash
  58. * when more than one Ethernet card is installed (knock on wood)
  59. *
  60. * Note (fthain): whether the above is still true is anyones guess. Certainly
  61. * the buffer handling algorithms will not tolerate re-entrance without some
  62. * mutual exclusion added. Anyway, the memcpy has now been eliminated from the
  63. * rx code to make this a faster "fast interrupt".
  64. */
  65. if (request_irq(dev->irq, &sonic_interrupt, SONIC_IRQ_FLAG, "sonic", dev)) {
  66. printk(KERN_ERR "\n%s: unable to get IRQ %d .\n", dev->name, dev->irq);
  67. return -EAGAIN;
  68. }
  69. for (i = 0; i < SONIC_NUM_RRS; i++) {
  70. struct sk_buff *skb = dev_alloc_skb(SONIC_RBSIZE + 2);
  71. if (skb == NULL) {
  72. while(i > 0) { /* free any that were allocated successfully */
  73. i--;
  74. dev_kfree_skb(lp->rx_skb[i]);
  75. lp->rx_skb[i] = NULL;
  76. }
  77. printk(KERN_ERR "%s: couldn't allocate receive buffers\n",
  78. dev->name);
  79. return -ENOMEM;
  80. }
  81. skb->dev = dev;
  82. /* align IP header unless DMA requires otherwise */
  83. if (SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
  84. skb_reserve(skb, 2);
  85. lp->rx_skb[i] = skb;
  86. }
  87. for (i = 0; i < SONIC_NUM_RRS; i++) {
  88. dma_addr_t laddr = dma_map_single(lp->device, skb_put(lp->rx_skb[i], SONIC_RBSIZE),
  89. SONIC_RBSIZE, DMA_FROM_DEVICE);
  90. if (!laddr) {
  91. while(i > 0) { /* free any that were mapped successfully */
  92. i--;
  93. dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE);
  94. lp->rx_laddr[i] = (dma_addr_t)0;
  95. }
  96. for (i = 0; i < SONIC_NUM_RRS; i++) {
  97. dev_kfree_skb(lp->rx_skb[i]);
  98. lp->rx_skb[i] = NULL;
  99. }
  100. printk(KERN_ERR "%s: couldn't map rx DMA buffers\n",
  101. dev->name);
  102. return -ENOMEM;
  103. }
  104. lp->rx_laddr[i] = laddr;
  105. }
  106. /*
  107. * Initialize the SONIC
  108. */
  109. sonic_init(dev);
  110. netif_start_queue(dev);
  111. if (sonic_debug > 2)
  112. printk("sonic_open: Initialization done.\n");
  113. return 0;
  114. }
  115. /*
  116. * Close the SONIC device
  117. */
  118. static int sonic_close(struct net_device *dev)
  119. {
  120. struct sonic_local *lp = netdev_priv(dev);
  121. int i;
  122. if (sonic_debug > 2)
  123. printk("sonic_close\n");
  124. netif_stop_queue(dev);
  125. /*
  126. * stop the SONIC, disable interrupts
  127. */
  128. SONIC_WRITE(SONIC_IMR, 0);
  129. SONIC_WRITE(SONIC_ISR, 0x7fff);
  130. SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
  131. /* unmap and free skbs that haven't been transmitted */
  132. for (i = 0; i < SONIC_NUM_TDS; i++) {
  133. if(lp->tx_laddr[i]) {
  134. dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
  135. lp->tx_laddr[i] = (dma_addr_t)0;
  136. }
  137. if(lp->tx_skb[i]) {
  138. dev_kfree_skb(lp->tx_skb[i]);
  139. lp->tx_skb[i] = NULL;
  140. }
  141. }
  142. /* unmap and free the receive buffers */
  143. for (i = 0; i < SONIC_NUM_RRS; i++) {
  144. if(lp->rx_laddr[i]) {
  145. dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE);
  146. lp->rx_laddr[i] = (dma_addr_t)0;
  147. }
  148. if(lp->rx_skb[i]) {
  149. dev_kfree_skb(lp->rx_skb[i]);
  150. lp->rx_skb[i] = NULL;
  151. }
  152. }
  153. free_irq(dev->irq, dev); /* release the IRQ */
  154. return 0;
  155. }
  156. static void sonic_tx_timeout(struct net_device *dev)
  157. {
  158. struct sonic_local *lp = netdev_priv(dev);
  159. int i;
  160. /* Stop the interrupts for this */
  161. SONIC_WRITE(SONIC_IMR, 0);
  162. /* We could resend the original skbs. Easier to re-initialise. */
  163. for (i = 0; i < SONIC_NUM_TDS; i++) {
  164. if(lp->tx_laddr[i]) {
  165. dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
  166. lp->tx_laddr[i] = (dma_addr_t)0;
  167. }
  168. if(lp->tx_skb[i]) {
  169. dev_kfree_skb(lp->tx_skb[i]);
  170. lp->tx_skb[i] = NULL;
  171. }
  172. }
  173. /* Try to restart the adaptor. */
  174. sonic_init(dev);
  175. lp->stats.tx_errors++;
  176. dev->trans_start = jiffies;
  177. netif_wake_queue(dev);
  178. }
  179. /*
  180. * transmit packet
  181. *
  182. * Appends new TD during transmission thus avoiding any TX interrupts
  183. * until we run out of TDs.
  184. * This routine interacts closely with the ISR in that it may,
  185. * set tx_skb[i]
  186. * reset the status flags of the new TD
  187. * set and reset EOL flags
  188. * stop the tx queue
  189. * The ISR interacts with this routine in various ways. It may,
  190. * reset tx_skb[i]
  191. * test the EOL and status flags of the TDs
  192. * wake the tx queue
  193. * Concurrently with all of this, the SONIC is potentially writing to
  194. * the status flags of the TDs.
  195. * Until some mutual exclusion is added, this code will not work with SMP. However,
  196. * MIPS Jazz machines and m68k Macs were all uni-processor machines.
  197. */
  198. static int sonic_send_packet(struct sk_buff *skb, struct net_device *dev)
  199. {
  200. struct sonic_local *lp = netdev_priv(dev);
  201. dma_addr_t laddr;
  202. int length;
  203. int entry = lp->next_tx;
  204. if (sonic_debug > 2)
  205. printk("sonic_send_packet: skb=%p, dev=%p\n", skb, dev);
  206. length = skb->len;
  207. if (length < ETH_ZLEN) {
  208. skb = skb_padto(skb, ETH_ZLEN);
  209. if (skb == NULL)
  210. return 0;
  211. length = ETH_ZLEN;
  212. }
  213. /*
  214. * Map the packet data into the logical DMA address space
  215. */
  216. laddr = dma_map_single(lp->device, skb->data, length, DMA_TO_DEVICE);
  217. if (!laddr) {
  218. printk(KERN_ERR "%s: failed to map tx DMA buffer.\n", dev->name);
  219. dev_kfree_skb(skb);
  220. return 1;
  221. }
  222. sonic_tda_put(dev, entry, SONIC_TD_STATUS, 0); /* clear status */
  223. sonic_tda_put(dev, entry, SONIC_TD_FRAG_COUNT, 1); /* single fragment */
  224. sonic_tda_put(dev, entry, SONIC_TD_PKTSIZE, length); /* length of packet */
  225. sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_L, laddr & 0xffff);
  226. sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_H, laddr >> 16);
  227. sonic_tda_put(dev, entry, SONIC_TD_FRAG_SIZE, length);
  228. sonic_tda_put(dev, entry, SONIC_TD_LINK,
  229. sonic_tda_get(dev, entry, SONIC_TD_LINK) | SONIC_EOL);
  230. /*
  231. * Must set tx_skb[entry] only after clearing status, and
  232. * before clearing EOL and before stopping queue
  233. */
  234. wmb();
  235. lp->tx_len[entry] = length;
  236. lp->tx_laddr[entry] = laddr;
  237. lp->tx_skb[entry] = skb;
  238. wmb();
  239. sonic_tda_put(dev, lp->eol_tx, SONIC_TD_LINK,
  240. sonic_tda_get(dev, lp->eol_tx, SONIC_TD_LINK) & ~SONIC_EOL);
  241. lp->eol_tx = entry;
  242. lp->next_tx = (entry + 1) & SONIC_TDS_MASK;
  243. if (lp->tx_skb[lp->next_tx] != NULL) {
  244. /* The ring is full, the ISR has yet to process the next TD. */
  245. if (sonic_debug > 3)
  246. printk("%s: stopping queue\n", dev->name);
  247. netif_stop_queue(dev);
  248. /* after this packet, wait for ISR to free up some TDAs */
  249. } else netif_start_queue(dev);
  250. if (sonic_debug > 2)
  251. printk("sonic_send_packet: issuing Tx command\n");
  252. SONIC_WRITE(SONIC_CMD, SONIC_CR_TXP);
  253. dev->trans_start = jiffies;
  254. return 0;
  255. }
  256. /*
  257. * The typical workload of the driver:
  258. * Handle the network interface interrupts.
  259. */
  260. static irqreturn_t sonic_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  261. {
  262. struct net_device *dev = (struct net_device *) dev_id;
  263. struct sonic_local *lp = netdev_priv(dev);
  264. int status;
  265. if (dev == NULL) {
  266. printk(KERN_ERR "sonic_interrupt: irq %d for unknown device.\n", irq);
  267. return IRQ_NONE;
  268. }
  269. if (!(status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT))
  270. return IRQ_NONE;
  271. do {
  272. if (status & SONIC_INT_PKTRX) {
  273. if (sonic_debug > 2)
  274. printk("%s: packet rx\n", dev->name);
  275. sonic_rx(dev); /* got packet(s) */
  276. SONIC_WRITE(SONIC_ISR, SONIC_INT_PKTRX); /* clear the interrupt */
  277. }
  278. if (status & SONIC_INT_TXDN) {
  279. int entry = lp->cur_tx;
  280. int td_status;
  281. int freed_some = 0;
  282. /* At this point, cur_tx is the index of a TD that is one of:
  283. * unallocated/freed (status set & tx_skb[entry] clear)
  284. * allocated and sent (status set & tx_skb[entry] set )
  285. * allocated and not yet sent (status clear & tx_skb[entry] set )
  286. * still being allocated by sonic_send_packet (status clear & tx_skb[entry] clear)
  287. */
  288. if (sonic_debug > 2)
  289. printk("%s: tx done\n", dev->name);
  290. while (lp->tx_skb[entry] != NULL) {
  291. if ((td_status = sonic_tda_get(dev, entry, SONIC_TD_STATUS)) == 0)
  292. break;
  293. if (td_status & 0x0001) {
  294. lp->stats.tx_packets++;
  295. lp->stats.tx_bytes += sonic_tda_get(dev, entry, SONIC_TD_PKTSIZE);
  296. } else {
  297. lp->stats.tx_errors++;
  298. if (td_status & 0x0642)
  299. lp->stats.tx_aborted_errors++;
  300. if (td_status & 0x0180)
  301. lp->stats.tx_carrier_errors++;
  302. if (td_status & 0x0020)
  303. lp->stats.tx_window_errors++;
  304. if (td_status & 0x0004)
  305. lp->stats.tx_fifo_errors++;
  306. }
  307. /* We must free the original skb */
  308. dev_kfree_skb_irq(lp->tx_skb[entry]);
  309. lp->tx_skb[entry] = NULL;
  310. /* and unmap DMA buffer */
  311. dma_unmap_single(lp->device, lp->tx_laddr[entry], lp->tx_len[entry], DMA_TO_DEVICE);
  312. lp->tx_laddr[entry] = (dma_addr_t)0;
  313. freed_some = 1;
  314. if (sonic_tda_get(dev, entry, SONIC_TD_LINK) & SONIC_EOL) {
  315. entry = (entry + 1) & SONIC_TDS_MASK;
  316. break;
  317. }
  318. entry = (entry + 1) & SONIC_TDS_MASK;
  319. }
  320. if (freed_some || lp->tx_skb[entry] == NULL)
  321. netif_wake_queue(dev); /* The ring is no longer full */
  322. lp->cur_tx = entry;
  323. SONIC_WRITE(SONIC_ISR, SONIC_INT_TXDN); /* clear the interrupt */
  324. }
  325. /*
  326. * check error conditions
  327. */
  328. if (status & SONIC_INT_RFO) {
  329. if (sonic_debug > 1)
  330. printk("%s: rx fifo overrun\n", dev->name);
  331. lp->stats.rx_fifo_errors++;
  332. SONIC_WRITE(SONIC_ISR, SONIC_INT_RFO); /* clear the interrupt */
  333. }
  334. if (status & SONIC_INT_RDE) {
  335. if (sonic_debug > 1)
  336. printk("%s: rx descriptors exhausted\n", dev->name);
  337. lp->stats.rx_dropped++;
  338. SONIC_WRITE(SONIC_ISR, SONIC_INT_RDE); /* clear the interrupt */
  339. }
  340. if (status & SONIC_INT_RBAE) {
  341. if (sonic_debug > 1)
  342. printk("%s: rx buffer area exceeded\n", dev->name);
  343. lp->stats.rx_dropped++;
  344. SONIC_WRITE(SONIC_ISR, SONIC_INT_RBAE); /* clear the interrupt */
  345. }
  346. /* counter overruns; all counters are 16bit wide */
  347. if (status & SONIC_INT_FAE) {
  348. lp->stats.rx_frame_errors += 65536;
  349. SONIC_WRITE(SONIC_ISR, SONIC_INT_FAE); /* clear the interrupt */
  350. }
  351. if (status & SONIC_INT_CRC) {
  352. lp->stats.rx_crc_errors += 65536;
  353. SONIC_WRITE(SONIC_ISR, SONIC_INT_CRC); /* clear the interrupt */
  354. }
  355. if (status & SONIC_INT_MP) {
  356. lp->stats.rx_missed_errors += 65536;
  357. SONIC_WRITE(SONIC_ISR, SONIC_INT_MP); /* clear the interrupt */
  358. }
  359. /* transmit error */
  360. if (status & SONIC_INT_TXER) {
  361. if ((SONIC_READ(SONIC_TCR) & SONIC_TCR_FU) && (sonic_debug > 2))
  362. printk(KERN_ERR "%s: tx fifo underrun\n", dev->name);
  363. SONIC_WRITE(SONIC_ISR, SONIC_INT_TXER); /* clear the interrupt */
  364. }
  365. /* bus retry */
  366. if (status & SONIC_INT_BR) {
  367. printk(KERN_ERR "%s: Bus retry occurred! Device interrupt disabled.\n",
  368. dev->name);
  369. /* ... to help debug DMA problems causing endless interrupts. */
  370. /* Bounce the eth interface to turn on the interrupt again. */
  371. SONIC_WRITE(SONIC_IMR, 0);
  372. SONIC_WRITE(SONIC_ISR, SONIC_INT_BR); /* clear the interrupt */
  373. }
  374. /* load CAM done */
  375. if (status & SONIC_INT_LCD)
  376. SONIC_WRITE(SONIC_ISR, SONIC_INT_LCD); /* clear the interrupt */
  377. } while((status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT));
  378. return IRQ_HANDLED;
  379. }
  380. /*
  381. * We have a good packet(s), pass it/them up the network stack.
  382. */
  383. static void sonic_rx(struct net_device *dev)
  384. {
  385. struct sonic_local *lp = netdev_priv(dev);
  386. int status;
  387. int entry = lp->cur_rx;
  388. while (sonic_rda_get(dev, entry, SONIC_RD_IN_USE) == 0) {
  389. struct sk_buff *used_skb;
  390. struct sk_buff *new_skb;
  391. dma_addr_t new_laddr;
  392. u16 bufadr_l;
  393. u16 bufadr_h;
  394. int pkt_len;
  395. status = sonic_rda_get(dev, entry, SONIC_RD_STATUS);
  396. if (status & SONIC_RCR_PRX) {
  397. /* Malloc up new buffer. */
  398. new_skb = dev_alloc_skb(SONIC_RBSIZE + 2);
  399. if (new_skb == NULL) {
  400. printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n", dev->name);
  401. lp->stats.rx_dropped++;
  402. break;
  403. }
  404. new_skb->dev = dev;
  405. /* provide 16 byte IP header alignment unless DMA requires otherwise */
  406. if(SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
  407. skb_reserve(new_skb, 2);
  408. new_laddr = dma_map_single(lp->device, skb_put(new_skb, SONIC_RBSIZE),
  409. SONIC_RBSIZE, DMA_FROM_DEVICE);
  410. if (!new_laddr) {
  411. dev_kfree_skb(new_skb);
  412. printk(KERN_ERR "%s: Failed to map rx buffer, dropping packet.\n", dev->name);
  413. lp->stats.rx_dropped++;
  414. break;
  415. }
  416. /* now we have a new skb to replace it, pass the used one up the stack */
  417. dma_unmap_single(lp->device, lp->rx_laddr[entry], SONIC_RBSIZE, DMA_FROM_DEVICE);
  418. used_skb = lp->rx_skb[entry];
  419. pkt_len = sonic_rda_get(dev, entry, SONIC_RD_PKTLEN);
  420. skb_trim(used_skb, pkt_len);
  421. used_skb->protocol = eth_type_trans(used_skb, dev);
  422. netif_rx(used_skb);
  423. dev->last_rx = jiffies;
  424. lp->stats.rx_packets++;
  425. lp->stats.rx_bytes += pkt_len;
  426. /* and insert the new skb */
  427. lp->rx_laddr[entry] = new_laddr;
  428. lp->rx_skb[entry] = new_skb;
  429. bufadr_l = (unsigned long)new_laddr & 0xffff;
  430. bufadr_h = (unsigned long)new_laddr >> 16;
  431. sonic_rra_put(dev, entry, SONIC_RR_BUFADR_L, bufadr_l);
  432. sonic_rra_put(dev, entry, SONIC_RR_BUFADR_H, bufadr_h);
  433. } else {
  434. /* This should only happen, if we enable accepting broken packets. */
  435. lp->stats.rx_errors++;
  436. if (status & SONIC_RCR_FAER)
  437. lp->stats.rx_frame_errors++;
  438. if (status & SONIC_RCR_CRCR)
  439. lp->stats.rx_crc_errors++;
  440. }
  441. if (status & SONIC_RCR_LPKT) {
  442. /*
  443. * this was the last packet out of the current receive buffer
  444. * give the buffer back to the SONIC
  445. */
  446. lp->cur_rwp += SIZEOF_SONIC_RR * SONIC_BUS_SCALE(lp->dma_bitmode);
  447. if (lp->cur_rwp >= lp->rra_end) lp->cur_rwp = lp->rra_laddr & 0xffff;
  448. SONIC_WRITE(SONIC_RWP, lp->cur_rwp);
  449. if (SONIC_READ(SONIC_ISR) & SONIC_INT_RBE) {
  450. if (sonic_debug > 2)
  451. printk("%s: rx buffer exhausted\n", dev->name);
  452. SONIC_WRITE(SONIC_ISR, SONIC_INT_RBE); /* clear the flag */
  453. }
  454. } else
  455. printk(KERN_ERR "%s: rx desc without RCR_LPKT. Shouldn't happen !?\n",
  456. dev->name);
  457. /*
  458. * give back the descriptor
  459. */
  460. sonic_rda_put(dev, entry, SONIC_RD_LINK,
  461. sonic_rda_get(dev, entry, SONIC_RD_LINK) | SONIC_EOL);
  462. sonic_rda_put(dev, entry, SONIC_RD_IN_USE, 1);
  463. sonic_rda_put(dev, lp->eol_rx, SONIC_RD_LINK,
  464. sonic_rda_get(dev, lp->eol_rx, SONIC_RD_LINK) & ~SONIC_EOL);
  465. lp->eol_rx = entry;
  466. lp->cur_rx = entry = (entry + 1) & SONIC_RDS_MASK;
  467. }
  468. /*
  469. * If any worth-while packets have been received, netif_rx()
  470. * has done a mark_bh(NET_BH) for us and will work on them
  471. * when we get to the bottom-half routine.
  472. */
  473. }
  474. /*
  475. * Get the current statistics.
  476. * This may be called with the device open or closed.
  477. */
  478. static struct net_device_stats *sonic_get_stats(struct net_device *dev)
  479. {
  480. struct sonic_local *lp = netdev_priv(dev);
  481. /* read the tally counter from the SONIC and reset them */
  482. lp->stats.rx_crc_errors += SONIC_READ(SONIC_CRCT);
  483. SONIC_WRITE(SONIC_CRCT, 0xffff);
  484. lp->stats.rx_frame_errors += SONIC_READ(SONIC_FAET);
  485. SONIC_WRITE(SONIC_FAET, 0xffff);
  486. lp->stats.rx_missed_errors += SONIC_READ(SONIC_MPT);
  487. SONIC_WRITE(SONIC_MPT, 0xffff);
  488. return &lp->stats;
  489. }
  490. /*
  491. * Set or clear the multicast filter for this adaptor.
  492. */
  493. static void sonic_multicast_list(struct net_device *dev)
  494. {
  495. struct sonic_local *lp = netdev_priv(dev);
  496. unsigned int rcr;
  497. struct dev_mc_list *dmi = dev->mc_list;
  498. unsigned char *addr;
  499. int i;
  500. rcr = SONIC_READ(SONIC_RCR) & ~(SONIC_RCR_PRO | SONIC_RCR_AMC);
  501. rcr |= SONIC_RCR_BRD; /* accept broadcast packets */
  502. if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
  503. rcr |= SONIC_RCR_PRO;
  504. } else {
  505. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
  506. rcr |= SONIC_RCR_AMC;
  507. } else {
  508. if (sonic_debug > 2)
  509. printk("sonic_multicast_list: mc_count %d\n", dev->mc_count);
  510. sonic_set_cam_enable(dev, 1); /* always enable our own address */
  511. for (i = 1; i <= dev->mc_count; i++) {
  512. addr = dmi->dmi_addr;
  513. dmi = dmi->next;
  514. sonic_cda_put(dev, i, SONIC_CD_CAP0, addr[1] << 8 | addr[0]);
  515. sonic_cda_put(dev, i, SONIC_CD_CAP1, addr[3] << 8 | addr[2]);
  516. sonic_cda_put(dev, i, SONIC_CD_CAP2, addr[5] << 8 | addr[4]);
  517. sonic_set_cam_enable(dev, sonic_get_cam_enable(dev) | (1 << i));
  518. }
  519. SONIC_WRITE(SONIC_CDC, 16);
  520. /* issue Load CAM command */
  521. SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
  522. SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
  523. }
  524. }
  525. if (sonic_debug > 2)
  526. printk("sonic_multicast_list: setting RCR=%x\n", rcr);
  527. SONIC_WRITE(SONIC_RCR, rcr);
  528. }
  529. /*
  530. * Initialize the SONIC ethernet controller.
  531. */
  532. static int sonic_init(struct net_device *dev)
  533. {
  534. unsigned int cmd;
  535. struct sonic_local *lp = netdev_priv(dev);
  536. int i;
  537. /*
  538. * put the Sonic into software-reset mode and
  539. * disable all interrupts
  540. */
  541. SONIC_WRITE(SONIC_IMR, 0);
  542. SONIC_WRITE(SONIC_ISR, 0x7fff);
  543. SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
  544. /*
  545. * clear software reset flag, disable receiver, clear and
  546. * enable interrupts, then completely initialize the SONIC
  547. */
  548. SONIC_WRITE(SONIC_CMD, 0);
  549. SONIC_WRITE(SONIC_CMD, SONIC_CR_RXDIS);
  550. /*
  551. * initialize the receive resource area
  552. */
  553. if (sonic_debug > 2)
  554. printk("sonic_init: initialize receive resource area\n");
  555. for (i = 0; i < SONIC_NUM_RRS; i++) {
  556. u16 bufadr_l = (unsigned long)lp->rx_laddr[i] & 0xffff;
  557. u16 bufadr_h = (unsigned long)lp->rx_laddr[i] >> 16;
  558. sonic_rra_put(dev, i, SONIC_RR_BUFADR_L, bufadr_l);
  559. sonic_rra_put(dev, i, SONIC_RR_BUFADR_H, bufadr_h);
  560. sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_L, SONIC_RBSIZE >> 1);
  561. sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_H, 0);
  562. }
  563. /* initialize all RRA registers */
  564. lp->rra_end = (lp->rra_laddr + SONIC_NUM_RRS * SIZEOF_SONIC_RR *
  565. SONIC_BUS_SCALE(lp->dma_bitmode)) & 0xffff;
  566. lp->cur_rwp = (lp->rra_laddr + (SONIC_NUM_RRS - 1) * SIZEOF_SONIC_RR *
  567. SONIC_BUS_SCALE(lp->dma_bitmode)) & 0xffff;
  568. SONIC_WRITE(SONIC_RSA, lp->rra_laddr & 0xffff);
  569. SONIC_WRITE(SONIC_REA, lp->rra_end);
  570. SONIC_WRITE(SONIC_RRP, lp->rra_laddr & 0xffff);
  571. SONIC_WRITE(SONIC_RWP, lp->cur_rwp);
  572. SONIC_WRITE(SONIC_URRA, lp->rra_laddr >> 16);
  573. SONIC_WRITE(SONIC_EOBC, (SONIC_RBSIZE >> 1) - (lp->dma_bitmode ? 2 : 1));
  574. /* load the resource pointers */
  575. if (sonic_debug > 3)
  576. printk("sonic_init: issuing RRRA command\n");
  577. SONIC_WRITE(SONIC_CMD, SONIC_CR_RRRA);
  578. i = 0;
  579. while (i++ < 100) {
  580. if (SONIC_READ(SONIC_CMD) & SONIC_CR_RRRA)
  581. break;
  582. }
  583. if (sonic_debug > 2)
  584. printk("sonic_init: status=%x i=%d\n", SONIC_READ(SONIC_CMD), i);
  585. /*
  586. * Initialize the receive descriptors so that they
  587. * become a circular linked list, ie. let the last
  588. * descriptor point to the first again.
  589. */
  590. if (sonic_debug > 2)
  591. printk("sonic_init: initialize receive descriptors\n");
  592. for (i=0; i<SONIC_NUM_RDS; i++) {
  593. sonic_rda_put(dev, i, SONIC_RD_STATUS, 0);
  594. sonic_rda_put(dev, i, SONIC_RD_PKTLEN, 0);
  595. sonic_rda_put(dev, i, SONIC_RD_PKTPTR_L, 0);
  596. sonic_rda_put(dev, i, SONIC_RD_PKTPTR_H, 0);
  597. sonic_rda_put(dev, i, SONIC_RD_SEQNO, 0);
  598. sonic_rda_put(dev, i, SONIC_RD_IN_USE, 1);
  599. sonic_rda_put(dev, i, SONIC_RD_LINK,
  600. lp->rda_laddr +
  601. ((i+1) * SIZEOF_SONIC_RD * SONIC_BUS_SCALE(lp->dma_bitmode)));
  602. }
  603. /* fix last descriptor */
  604. sonic_rda_put(dev, SONIC_NUM_RDS - 1, SONIC_RD_LINK,
  605. (lp->rda_laddr & 0xffff) | SONIC_EOL);
  606. lp->eol_rx = SONIC_NUM_RDS - 1;
  607. lp->cur_rx = 0;
  608. SONIC_WRITE(SONIC_URDA, lp->rda_laddr >> 16);
  609. SONIC_WRITE(SONIC_CRDA, lp->rda_laddr & 0xffff);
  610. /*
  611. * initialize transmit descriptors
  612. */
  613. if (sonic_debug > 2)
  614. printk("sonic_init: initialize transmit descriptors\n");
  615. for (i = 0; i < SONIC_NUM_TDS; i++) {
  616. sonic_tda_put(dev, i, SONIC_TD_STATUS, 0);
  617. sonic_tda_put(dev, i, SONIC_TD_CONFIG, 0);
  618. sonic_tda_put(dev, i, SONIC_TD_PKTSIZE, 0);
  619. sonic_tda_put(dev, i, SONIC_TD_FRAG_COUNT, 0);
  620. sonic_tda_put(dev, i, SONIC_TD_LINK,
  621. (lp->tda_laddr & 0xffff) +
  622. (i + 1) * SIZEOF_SONIC_TD * SONIC_BUS_SCALE(lp->dma_bitmode));
  623. lp->tx_skb[i] = NULL;
  624. }
  625. /* fix last descriptor */
  626. sonic_tda_put(dev, SONIC_NUM_TDS - 1, SONIC_TD_LINK,
  627. (lp->tda_laddr & 0xffff));
  628. SONIC_WRITE(SONIC_UTDA, lp->tda_laddr >> 16);
  629. SONIC_WRITE(SONIC_CTDA, lp->tda_laddr & 0xffff);
  630. lp->cur_tx = lp->next_tx = 0;
  631. lp->eol_tx = SONIC_NUM_TDS - 1;
  632. /*
  633. * put our own address to CAM desc[0]
  634. */
  635. sonic_cda_put(dev, 0, SONIC_CD_CAP0, dev->dev_addr[1] << 8 | dev->dev_addr[0]);
  636. sonic_cda_put(dev, 0, SONIC_CD_CAP1, dev->dev_addr[3] << 8 | dev->dev_addr[2]);
  637. sonic_cda_put(dev, 0, SONIC_CD_CAP2, dev->dev_addr[5] << 8 | dev->dev_addr[4]);
  638. sonic_set_cam_enable(dev, 1);
  639. for (i = 0; i < 16; i++)
  640. sonic_cda_put(dev, i, SONIC_CD_ENTRY_POINTER, i);
  641. /*
  642. * initialize CAM registers
  643. */
  644. SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
  645. SONIC_WRITE(SONIC_CDC, 16);
  646. /*
  647. * load the CAM
  648. */
  649. SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
  650. i = 0;
  651. while (i++ < 100) {
  652. if (SONIC_READ(SONIC_ISR) & SONIC_INT_LCD)
  653. break;
  654. }
  655. if (sonic_debug > 2) {
  656. printk("sonic_init: CMD=%x, ISR=%x\n, i=%d",
  657. SONIC_READ(SONIC_CMD), SONIC_READ(SONIC_ISR), i);
  658. }
  659. /*
  660. * enable receiver, disable loopback
  661. * and enable all interrupts
  662. */
  663. SONIC_WRITE(SONIC_CMD, SONIC_CR_RXEN | SONIC_CR_STP);
  664. SONIC_WRITE(SONIC_RCR, SONIC_RCR_DEFAULT);
  665. SONIC_WRITE(SONIC_TCR, SONIC_TCR_DEFAULT);
  666. SONIC_WRITE(SONIC_ISR, 0x7fff);
  667. SONIC_WRITE(SONIC_IMR, SONIC_IMR_DEFAULT);
  668. cmd = SONIC_READ(SONIC_CMD);
  669. if ((cmd & SONIC_CR_RXEN) == 0 || (cmd & SONIC_CR_STP) == 0)
  670. printk(KERN_ERR "sonic_init: failed, status=%x\n", cmd);
  671. if (sonic_debug > 2)
  672. printk("sonic_init: new status=%x\n",
  673. SONIC_READ(SONIC_CMD));
  674. return 0;
  675. }
  676. MODULE_LICENSE("GPL");