sonic.c 22 KB

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