sonic.c 22 KB

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