sunqe.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /* sunqe.c: Sparc QuadEthernet 10baseT SBUS card driver.
  2. * Once again I am out to prove that every ethernet
  3. * controller out there can be most efficiently programmed
  4. * if you make it look like a LANCE.
  5. *
  6. * Copyright (C) 1996, 1999, 2003, 2006 David S. Miller (davem@davemloft.net)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/ioport.h>
  15. #include <linux/in.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/crc32.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/bitops.h>
  26. #include <asm/system.h>
  27. #include <asm/io.h>
  28. #include <asm/dma.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/idprom.h>
  31. #include <asm/sbus.h>
  32. #include <asm/openprom.h>
  33. #include <asm/oplib.h>
  34. #include <asm/auxio.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/irq.h>
  37. #include "sunqe.h"
  38. #define DRV_NAME "sunqe"
  39. #define DRV_VERSION "4.0"
  40. #define DRV_RELDATE "June 23, 2006"
  41. #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
  42. static char version[] =
  43. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  44. MODULE_VERSION(DRV_VERSION);
  45. MODULE_AUTHOR(DRV_AUTHOR);
  46. MODULE_DESCRIPTION("Sun QuadEthernet 10baseT SBUS card driver");
  47. MODULE_LICENSE("GPL");
  48. static struct sunqec *root_qec_dev;
  49. static void qe_set_multicast(struct net_device *dev);
  50. #define QEC_RESET_TRIES 200
  51. static inline int qec_global_reset(void __iomem *gregs)
  52. {
  53. int tries = QEC_RESET_TRIES;
  54. sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
  55. while (--tries) {
  56. u32 tmp = sbus_readl(gregs + GLOB_CTRL);
  57. if (tmp & GLOB_CTRL_RESET) {
  58. udelay(20);
  59. continue;
  60. }
  61. break;
  62. }
  63. if (tries)
  64. return 0;
  65. printk(KERN_ERR "QuadEther: AIEEE cannot reset the QEC!\n");
  66. return -1;
  67. }
  68. #define MACE_RESET_RETRIES 200
  69. #define QE_RESET_RETRIES 200
  70. static inline int qe_stop(struct sunqe *qep)
  71. {
  72. void __iomem *cregs = qep->qcregs;
  73. void __iomem *mregs = qep->mregs;
  74. int tries;
  75. /* Reset the MACE, then the QEC channel. */
  76. sbus_writeb(MREGS_BCONFIG_RESET, mregs + MREGS_BCONFIG);
  77. tries = MACE_RESET_RETRIES;
  78. while (--tries) {
  79. u8 tmp = sbus_readb(mregs + MREGS_BCONFIG);
  80. if (tmp & MREGS_BCONFIG_RESET) {
  81. udelay(20);
  82. continue;
  83. }
  84. break;
  85. }
  86. if (!tries) {
  87. printk(KERN_ERR "QuadEther: AIEEE cannot reset the MACE!\n");
  88. return -1;
  89. }
  90. sbus_writel(CREG_CTRL_RESET, cregs + CREG_CTRL);
  91. tries = QE_RESET_RETRIES;
  92. while (--tries) {
  93. u32 tmp = sbus_readl(cregs + CREG_CTRL);
  94. if (tmp & CREG_CTRL_RESET) {
  95. udelay(20);
  96. continue;
  97. }
  98. break;
  99. }
  100. if (!tries) {
  101. printk(KERN_ERR "QuadEther: Cannot reset QE channel!\n");
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. static void qe_init_rings(struct sunqe *qep)
  107. {
  108. struct qe_init_block *qb = qep->qe_block;
  109. struct sunqe_buffers *qbufs = qep->buffers;
  110. __u32 qbufs_dvma = qep->buffers_dvma;
  111. int i;
  112. qep->rx_new = qep->rx_old = qep->tx_new = qep->tx_old = 0;
  113. memset(qb, 0, sizeof(struct qe_init_block));
  114. memset(qbufs, 0, sizeof(struct sunqe_buffers));
  115. for (i = 0; i < RX_RING_SIZE; i++) {
  116. qb->qe_rxd[i].rx_addr = qbufs_dvma + qebuf_offset(rx_buf, i);
  117. qb->qe_rxd[i].rx_flags =
  118. (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
  119. }
  120. }
  121. static int qe_init(struct sunqe *qep, int from_irq)
  122. {
  123. struct sunqec *qecp = qep->parent;
  124. void __iomem *cregs = qep->qcregs;
  125. void __iomem *mregs = qep->mregs;
  126. void __iomem *gregs = qecp->gregs;
  127. unsigned char *e = &qep->dev->dev_addr[0];
  128. u32 tmp;
  129. int i;
  130. /* Shut it up. */
  131. if (qe_stop(qep))
  132. return -EAGAIN;
  133. /* Setup initial rx/tx init block pointers. */
  134. sbus_writel(qep->qblock_dvma + qib_offset(qe_rxd, 0), cregs + CREG_RXDS);
  135. sbus_writel(qep->qblock_dvma + qib_offset(qe_txd, 0), cregs + CREG_TXDS);
  136. /* Enable/mask the various irq's. */
  137. sbus_writel(0, cregs + CREG_RIMASK);
  138. sbus_writel(1, cregs + CREG_TIMASK);
  139. sbus_writel(0, cregs + CREG_QMASK);
  140. sbus_writel(CREG_MMASK_RXCOLL, cregs + CREG_MMASK);
  141. /* Setup the FIFO pointers into QEC local memory. */
  142. tmp = qep->channel * sbus_readl(gregs + GLOB_MSIZE);
  143. sbus_writel(tmp, cregs + CREG_RXRBUFPTR);
  144. sbus_writel(tmp, cregs + CREG_RXWBUFPTR);
  145. tmp = sbus_readl(cregs + CREG_RXRBUFPTR) +
  146. sbus_readl(gregs + GLOB_RSIZE);
  147. sbus_writel(tmp, cregs + CREG_TXRBUFPTR);
  148. sbus_writel(tmp, cregs + CREG_TXWBUFPTR);
  149. /* Clear the channel collision counter. */
  150. sbus_writel(0, cregs + CREG_CCNT);
  151. /* For 10baseT, inter frame space nor throttle seems to be necessary. */
  152. sbus_writel(0, cregs + CREG_PIPG);
  153. /* Now dork with the AMD MACE. */
  154. sbus_writeb(MREGS_PHYCONFIG_AUTO, mregs + MREGS_PHYCONFIG);
  155. sbus_writeb(MREGS_TXFCNTL_AUTOPAD, mregs + MREGS_TXFCNTL);
  156. sbus_writeb(0, mregs + MREGS_RXFCNTL);
  157. /* The QEC dma's the rx'd packets from local memory out to main memory,
  158. * and therefore it interrupts when the packet reception is "complete".
  159. * So don't listen for the MACE talking about it.
  160. */
  161. sbus_writeb(MREGS_IMASK_COLL | MREGS_IMASK_RXIRQ, mregs + MREGS_IMASK);
  162. sbus_writeb(MREGS_BCONFIG_BSWAP | MREGS_BCONFIG_64TS, mregs + MREGS_BCONFIG);
  163. sbus_writeb((MREGS_FCONFIG_TXF16 | MREGS_FCONFIG_RXF32 |
  164. MREGS_FCONFIG_RFWU | MREGS_FCONFIG_TFWU),
  165. mregs + MREGS_FCONFIG);
  166. /* Only usable interface on QuadEther is twisted pair. */
  167. sbus_writeb(MREGS_PLSCONFIG_TP, mregs + MREGS_PLSCONFIG);
  168. /* Tell MACE we are changing the ether address. */
  169. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_PARESET,
  170. mregs + MREGS_IACONFIG);
  171. while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  172. barrier();
  173. sbus_writeb(e[0], mregs + MREGS_ETHADDR);
  174. sbus_writeb(e[1], mregs + MREGS_ETHADDR);
  175. sbus_writeb(e[2], mregs + MREGS_ETHADDR);
  176. sbus_writeb(e[3], mregs + MREGS_ETHADDR);
  177. sbus_writeb(e[4], mregs + MREGS_ETHADDR);
  178. sbus_writeb(e[5], mregs + MREGS_ETHADDR);
  179. /* Clear out the address filter. */
  180. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  181. mregs + MREGS_IACONFIG);
  182. while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  183. barrier();
  184. for (i = 0; i < 8; i++)
  185. sbus_writeb(0, mregs + MREGS_FILTER);
  186. /* Address changes are now complete. */
  187. sbus_writeb(0, mregs + MREGS_IACONFIG);
  188. qe_init_rings(qep);
  189. /* Wait a little bit for the link to come up... */
  190. mdelay(5);
  191. if (!(sbus_readb(mregs + MREGS_PHYCONFIG) & MREGS_PHYCONFIG_LTESTDIS)) {
  192. int tries = 50;
  193. while (tries--) {
  194. u8 tmp;
  195. mdelay(5);
  196. barrier();
  197. tmp = sbus_readb(mregs + MREGS_PHYCONFIG);
  198. if ((tmp & MREGS_PHYCONFIG_LSTAT) != 0)
  199. break;
  200. }
  201. if (tries == 0)
  202. printk(KERN_NOTICE "%s: Warning, link state is down.\n", qep->dev->name);
  203. }
  204. /* Missed packet counter is cleared on a read. */
  205. sbus_readb(mregs + MREGS_MPCNT);
  206. /* Reload multicast information, this will enable the receiver
  207. * and transmitter.
  208. */
  209. qe_set_multicast(qep->dev);
  210. /* QEC should now start to show interrupts. */
  211. return 0;
  212. }
  213. /* Grrr, certain error conditions completely lock up the AMD MACE,
  214. * so when we get these we _must_ reset the chip.
  215. */
  216. static int qe_is_bolixed(struct sunqe *qep, u32 qe_status)
  217. {
  218. struct net_device *dev = qep->dev;
  219. int mace_hwbug_workaround = 0;
  220. if (qe_status & CREG_STAT_EDEFER) {
  221. printk(KERN_ERR "%s: Excessive transmit defers.\n", dev->name);
  222. qep->net_stats.tx_errors++;
  223. }
  224. if (qe_status & CREG_STAT_CLOSS) {
  225. printk(KERN_ERR "%s: Carrier lost, link down?\n", dev->name);
  226. qep->net_stats.tx_errors++;
  227. qep->net_stats.tx_carrier_errors++;
  228. }
  229. if (qe_status & CREG_STAT_ERETRIES) {
  230. printk(KERN_ERR "%s: Excessive transmit retries (more than 16).\n", dev->name);
  231. qep->net_stats.tx_errors++;
  232. mace_hwbug_workaround = 1;
  233. }
  234. if (qe_status & CREG_STAT_LCOLL) {
  235. printk(KERN_ERR "%s: Late transmit collision.\n", dev->name);
  236. qep->net_stats.tx_errors++;
  237. qep->net_stats.collisions++;
  238. mace_hwbug_workaround = 1;
  239. }
  240. if (qe_status & CREG_STAT_FUFLOW) {
  241. printk(KERN_ERR "%s: Transmit fifo underflow, driver bug.\n", dev->name);
  242. qep->net_stats.tx_errors++;
  243. mace_hwbug_workaround = 1;
  244. }
  245. if (qe_status & CREG_STAT_JERROR) {
  246. printk(KERN_ERR "%s: Jabber error.\n", dev->name);
  247. }
  248. if (qe_status & CREG_STAT_BERROR) {
  249. printk(KERN_ERR "%s: Babble error.\n", dev->name);
  250. }
  251. if (qe_status & CREG_STAT_CCOFLOW) {
  252. qep->net_stats.tx_errors += 256;
  253. qep->net_stats.collisions += 256;
  254. }
  255. if (qe_status & CREG_STAT_TXDERROR) {
  256. printk(KERN_ERR "%s: Transmit descriptor is bogus, driver bug.\n", dev->name);
  257. qep->net_stats.tx_errors++;
  258. qep->net_stats.tx_aborted_errors++;
  259. mace_hwbug_workaround = 1;
  260. }
  261. if (qe_status & CREG_STAT_TXLERR) {
  262. printk(KERN_ERR "%s: Transmit late error.\n", dev->name);
  263. qep->net_stats.tx_errors++;
  264. mace_hwbug_workaround = 1;
  265. }
  266. if (qe_status & CREG_STAT_TXPERR) {
  267. printk(KERN_ERR "%s: Transmit DMA parity error.\n", dev->name);
  268. qep->net_stats.tx_errors++;
  269. qep->net_stats.tx_aborted_errors++;
  270. mace_hwbug_workaround = 1;
  271. }
  272. if (qe_status & CREG_STAT_TXSERR) {
  273. printk(KERN_ERR "%s: Transmit DMA sbus error ack.\n", dev->name);
  274. qep->net_stats.tx_errors++;
  275. qep->net_stats.tx_aborted_errors++;
  276. mace_hwbug_workaround = 1;
  277. }
  278. if (qe_status & CREG_STAT_RCCOFLOW) {
  279. qep->net_stats.rx_errors += 256;
  280. qep->net_stats.collisions += 256;
  281. }
  282. if (qe_status & CREG_STAT_RUOFLOW) {
  283. qep->net_stats.rx_errors += 256;
  284. qep->net_stats.rx_over_errors += 256;
  285. }
  286. if (qe_status & CREG_STAT_MCOFLOW) {
  287. qep->net_stats.rx_errors += 256;
  288. qep->net_stats.rx_missed_errors += 256;
  289. }
  290. if (qe_status & CREG_STAT_RXFOFLOW) {
  291. printk(KERN_ERR "%s: Receive fifo overflow.\n", dev->name);
  292. qep->net_stats.rx_errors++;
  293. qep->net_stats.rx_over_errors++;
  294. }
  295. if (qe_status & CREG_STAT_RLCOLL) {
  296. printk(KERN_ERR "%s: Late receive collision.\n", dev->name);
  297. qep->net_stats.rx_errors++;
  298. qep->net_stats.collisions++;
  299. }
  300. if (qe_status & CREG_STAT_FCOFLOW) {
  301. qep->net_stats.rx_errors += 256;
  302. qep->net_stats.rx_frame_errors += 256;
  303. }
  304. if (qe_status & CREG_STAT_CECOFLOW) {
  305. qep->net_stats.rx_errors += 256;
  306. qep->net_stats.rx_crc_errors += 256;
  307. }
  308. if (qe_status & CREG_STAT_RXDROP) {
  309. printk(KERN_ERR "%s: Receive packet dropped.\n", dev->name);
  310. qep->net_stats.rx_errors++;
  311. qep->net_stats.rx_dropped++;
  312. qep->net_stats.rx_missed_errors++;
  313. }
  314. if (qe_status & CREG_STAT_RXSMALL) {
  315. printk(KERN_ERR "%s: Receive buffer too small, driver bug.\n", dev->name);
  316. qep->net_stats.rx_errors++;
  317. qep->net_stats.rx_length_errors++;
  318. }
  319. if (qe_status & CREG_STAT_RXLERR) {
  320. printk(KERN_ERR "%s: Receive late error.\n", dev->name);
  321. qep->net_stats.rx_errors++;
  322. mace_hwbug_workaround = 1;
  323. }
  324. if (qe_status & CREG_STAT_RXPERR) {
  325. printk(KERN_ERR "%s: Receive DMA parity error.\n", dev->name);
  326. qep->net_stats.rx_errors++;
  327. qep->net_stats.rx_missed_errors++;
  328. mace_hwbug_workaround = 1;
  329. }
  330. if (qe_status & CREG_STAT_RXSERR) {
  331. printk(KERN_ERR "%s: Receive DMA sbus error ack.\n", dev->name);
  332. qep->net_stats.rx_errors++;
  333. qep->net_stats.rx_missed_errors++;
  334. mace_hwbug_workaround = 1;
  335. }
  336. if (mace_hwbug_workaround)
  337. qe_init(qep, 1);
  338. return mace_hwbug_workaround;
  339. }
  340. /* Per-QE receive interrupt service routine. Just like on the happy meal
  341. * we receive directly into skb's with a small packet copy water mark.
  342. */
  343. static void qe_rx(struct sunqe *qep)
  344. {
  345. struct qe_rxd *rxbase = &qep->qe_block->qe_rxd[0];
  346. struct qe_rxd *this;
  347. struct sunqe_buffers *qbufs = qep->buffers;
  348. __u32 qbufs_dvma = qep->buffers_dvma;
  349. int elem = qep->rx_new, drops = 0;
  350. u32 flags;
  351. this = &rxbase[elem];
  352. while (!((flags = this->rx_flags) & RXD_OWN)) {
  353. struct sk_buff *skb;
  354. unsigned char *this_qbuf =
  355. &qbufs->rx_buf[elem & (RX_RING_SIZE - 1)][0];
  356. __u32 this_qbuf_dvma = qbufs_dvma +
  357. qebuf_offset(rx_buf, (elem & (RX_RING_SIZE - 1)));
  358. struct qe_rxd *end_rxd =
  359. &rxbase[(elem+RX_RING_SIZE)&(RX_RING_MAXSIZE-1)];
  360. int len = (flags & RXD_LENGTH) - 4; /* QE adds ether FCS size to len */
  361. /* Check for errors. */
  362. if (len < ETH_ZLEN) {
  363. qep->net_stats.rx_errors++;
  364. qep->net_stats.rx_length_errors++;
  365. qep->net_stats.rx_dropped++;
  366. } else {
  367. skb = dev_alloc_skb(len + 2);
  368. if (skb == NULL) {
  369. drops++;
  370. qep->net_stats.rx_dropped++;
  371. } else {
  372. skb_reserve(skb, 2);
  373. skb_put(skb, len);
  374. eth_copy_and_sum(skb, (unsigned char *) this_qbuf,
  375. len, 0);
  376. skb->protocol = eth_type_trans(skb, qep->dev);
  377. netif_rx(skb);
  378. qep->dev->last_rx = jiffies;
  379. qep->net_stats.rx_packets++;
  380. qep->net_stats.rx_bytes += len;
  381. }
  382. }
  383. end_rxd->rx_addr = this_qbuf_dvma;
  384. end_rxd->rx_flags = (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
  385. elem = NEXT_RX(elem);
  386. this = &rxbase[elem];
  387. }
  388. qep->rx_new = elem;
  389. if (drops)
  390. printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", qep->dev->name);
  391. }
  392. static void qe_tx_reclaim(struct sunqe *qep);
  393. /* Interrupts for all QE's get filtered out via the QEC master controller,
  394. * so we just run through each qe and check to see who is signaling
  395. * and thus needs to be serviced.
  396. */
  397. static irqreturn_t qec_interrupt(int irq, void *dev_id)
  398. {
  399. struct sunqec *qecp = dev_id;
  400. u32 qec_status;
  401. int channel = 0;
  402. /* Latch the status now. */
  403. qec_status = sbus_readl(qecp->gregs + GLOB_STAT);
  404. while (channel < 4) {
  405. if (qec_status & 0xf) {
  406. struct sunqe *qep = qecp->qes[channel];
  407. u32 qe_status;
  408. qe_status = sbus_readl(qep->qcregs + CREG_STAT);
  409. if (qe_status & CREG_STAT_ERRORS) {
  410. if (qe_is_bolixed(qep, qe_status))
  411. goto next;
  412. }
  413. if (qe_status & CREG_STAT_RXIRQ)
  414. qe_rx(qep);
  415. if (netif_queue_stopped(qep->dev) &&
  416. (qe_status & CREG_STAT_TXIRQ)) {
  417. spin_lock(&qep->lock);
  418. qe_tx_reclaim(qep);
  419. if (TX_BUFFS_AVAIL(qep) > 0) {
  420. /* Wake net queue and return to
  421. * lazy tx reclaim.
  422. */
  423. netif_wake_queue(qep->dev);
  424. sbus_writel(1, qep->qcregs + CREG_TIMASK);
  425. }
  426. spin_unlock(&qep->lock);
  427. }
  428. next:
  429. ;
  430. }
  431. qec_status >>= 4;
  432. channel++;
  433. }
  434. return IRQ_HANDLED;
  435. }
  436. static int qe_open(struct net_device *dev)
  437. {
  438. struct sunqe *qep = (struct sunqe *) dev->priv;
  439. qep->mconfig = (MREGS_MCONFIG_TXENAB |
  440. MREGS_MCONFIG_RXENAB |
  441. MREGS_MCONFIG_MBAENAB);
  442. return qe_init(qep, 0);
  443. }
  444. static int qe_close(struct net_device *dev)
  445. {
  446. struct sunqe *qep = (struct sunqe *) dev->priv;
  447. qe_stop(qep);
  448. return 0;
  449. }
  450. /* Reclaim TX'd frames from the ring. This must always run under
  451. * the IRQ protected qep->lock.
  452. */
  453. static void qe_tx_reclaim(struct sunqe *qep)
  454. {
  455. struct qe_txd *txbase = &qep->qe_block->qe_txd[0];
  456. int elem = qep->tx_old;
  457. while (elem != qep->tx_new) {
  458. u32 flags = txbase[elem].tx_flags;
  459. if (flags & TXD_OWN)
  460. break;
  461. elem = NEXT_TX(elem);
  462. }
  463. qep->tx_old = elem;
  464. }
  465. static void qe_tx_timeout(struct net_device *dev)
  466. {
  467. struct sunqe *qep = (struct sunqe *) dev->priv;
  468. int tx_full;
  469. spin_lock_irq(&qep->lock);
  470. /* Try to reclaim, if that frees up some tx
  471. * entries, we're fine.
  472. */
  473. qe_tx_reclaim(qep);
  474. tx_full = TX_BUFFS_AVAIL(qep) <= 0;
  475. spin_unlock_irq(&qep->lock);
  476. if (! tx_full)
  477. goto out;
  478. printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  479. qe_init(qep, 1);
  480. out:
  481. netif_wake_queue(dev);
  482. }
  483. /* Get a packet queued to go onto the wire. */
  484. static int qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
  485. {
  486. struct sunqe *qep = (struct sunqe *) dev->priv;
  487. struct sunqe_buffers *qbufs = qep->buffers;
  488. __u32 txbuf_dvma, qbufs_dvma = qep->buffers_dvma;
  489. unsigned char *txbuf;
  490. int len, entry;
  491. spin_lock_irq(&qep->lock);
  492. qe_tx_reclaim(qep);
  493. len = skb->len;
  494. entry = qep->tx_new;
  495. txbuf = &qbufs->tx_buf[entry & (TX_RING_SIZE - 1)][0];
  496. txbuf_dvma = qbufs_dvma +
  497. qebuf_offset(tx_buf, (entry & (TX_RING_SIZE - 1)));
  498. /* Avoid a race... */
  499. qep->qe_block->qe_txd[entry].tx_flags = TXD_UPDATE;
  500. skb_copy_from_linear_data(skb, txbuf, len);
  501. qep->qe_block->qe_txd[entry].tx_addr = txbuf_dvma;
  502. qep->qe_block->qe_txd[entry].tx_flags =
  503. (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
  504. qep->tx_new = NEXT_TX(entry);
  505. /* Get it going. */
  506. dev->trans_start = jiffies;
  507. sbus_writel(CREG_CTRL_TWAKEUP, qep->qcregs + CREG_CTRL);
  508. qep->net_stats.tx_packets++;
  509. qep->net_stats.tx_bytes += len;
  510. if (TX_BUFFS_AVAIL(qep) <= 0) {
  511. /* Halt the net queue and enable tx interrupts.
  512. * When the tx queue empties the tx irq handler
  513. * will wake up the queue and return us back to
  514. * the lazy tx reclaim scheme.
  515. */
  516. netif_stop_queue(dev);
  517. sbus_writel(0, qep->qcregs + CREG_TIMASK);
  518. }
  519. spin_unlock_irq(&qep->lock);
  520. dev_kfree_skb(skb);
  521. return 0;
  522. }
  523. static struct net_device_stats *qe_get_stats(struct net_device *dev)
  524. {
  525. struct sunqe *qep = (struct sunqe *) dev->priv;
  526. return &qep->net_stats;
  527. }
  528. static void qe_set_multicast(struct net_device *dev)
  529. {
  530. struct sunqe *qep = (struct sunqe *) dev->priv;
  531. struct dev_mc_list *dmi = dev->mc_list;
  532. u8 new_mconfig = qep->mconfig;
  533. char *addrs;
  534. int i;
  535. u32 crc;
  536. /* Lock out others. */
  537. netif_stop_queue(dev);
  538. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  539. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  540. qep->mregs + MREGS_IACONFIG);
  541. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  542. barrier();
  543. for (i = 0; i < 8; i++)
  544. sbus_writeb(0xff, qep->mregs + MREGS_FILTER);
  545. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  546. } else if (dev->flags & IFF_PROMISC) {
  547. new_mconfig |= MREGS_MCONFIG_PROMISC;
  548. } else {
  549. u16 hash_table[4];
  550. u8 *hbytes = (unsigned char *) &hash_table[0];
  551. for (i = 0; i < 4; i++)
  552. hash_table[i] = 0;
  553. for (i = 0; i < dev->mc_count; i++) {
  554. addrs = dmi->dmi_addr;
  555. dmi = dmi->next;
  556. if (!(*addrs & 1))
  557. continue;
  558. crc = ether_crc_le(6, addrs);
  559. crc >>= 26;
  560. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  561. }
  562. /* Program the qe with the new filter value. */
  563. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  564. qep->mregs + MREGS_IACONFIG);
  565. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  566. barrier();
  567. for (i = 0; i < 8; i++) {
  568. u8 tmp = *hbytes++;
  569. sbus_writeb(tmp, qep->mregs + MREGS_FILTER);
  570. }
  571. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  572. }
  573. /* Any change of the logical address filter, the physical address,
  574. * or enabling/disabling promiscuous mode causes the MACE to disable
  575. * the receiver. So we must re-enable them here or else the MACE
  576. * refuses to listen to anything on the network. Sheesh, took
  577. * me a day or two to find this bug.
  578. */
  579. qep->mconfig = new_mconfig;
  580. sbus_writeb(qep->mconfig, qep->mregs + MREGS_MCONFIG);
  581. /* Let us get going again. */
  582. netif_wake_queue(dev);
  583. }
  584. /* Ethtool support... */
  585. static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  586. {
  587. struct sunqe *qep = dev->priv;
  588. strcpy(info->driver, "sunqe");
  589. strcpy(info->version, "3.0");
  590. sprintf(info->bus_info, "SBUS:%d",
  591. qep->qe_sdev->slot);
  592. }
  593. static u32 qe_get_link(struct net_device *dev)
  594. {
  595. struct sunqe *qep = dev->priv;
  596. void __iomem *mregs = qep->mregs;
  597. u8 phyconfig;
  598. spin_lock_irq(&qep->lock);
  599. phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
  600. spin_unlock_irq(&qep->lock);
  601. return (phyconfig & MREGS_PHYCONFIG_LSTAT);
  602. }
  603. static const struct ethtool_ops qe_ethtool_ops = {
  604. .get_drvinfo = qe_get_drvinfo,
  605. .get_link = qe_get_link,
  606. };
  607. /* This is only called once at boot time for each card probed. */
  608. static inline void qec_init_once(struct sunqec *qecp, struct sbus_dev *qsdev)
  609. {
  610. u8 bsizes = qecp->qec_bursts;
  611. if (sbus_can_burst64(qsdev) && (bsizes & DMA_BURST64)) {
  612. sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
  613. } else if (bsizes & DMA_BURST32) {
  614. sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
  615. } else {
  616. sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
  617. }
  618. /* Packetsize only used in 100baseT BigMAC configurations,
  619. * set it to zero just to be on the safe side.
  620. */
  621. sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
  622. /* Set the local memsize register, divided up to one piece per QE channel. */
  623. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2),
  624. qecp->gregs + GLOB_MSIZE);
  625. /* Divide up the local QEC memory amongst the 4 QE receiver and
  626. * transmitter FIFOs. Basically it is (total / 2 / num_channels).
  627. */
  628. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
  629. qecp->gregs + GLOB_TSIZE);
  630. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
  631. qecp->gregs + GLOB_RSIZE);
  632. }
  633. static u8 __init qec_get_burst(struct device_node *dp)
  634. {
  635. u8 bsizes, bsizes_more;
  636. /* Find and set the burst sizes for the QEC, since it
  637. * does the actual dma for all 4 channels.
  638. */
  639. bsizes = of_getintprop_default(dp, "burst-sizes", 0xff);
  640. bsizes &= 0xff;
  641. bsizes_more = of_getintprop_default(dp->parent, "burst-sizes", 0xff);
  642. if (bsizes_more != 0xff)
  643. bsizes &= bsizes_more;
  644. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  645. (bsizes & DMA_BURST32)==0)
  646. bsizes = (DMA_BURST32 - 1);
  647. return bsizes;
  648. }
  649. static struct sunqec * __init get_qec(struct sbus_dev *child_sdev)
  650. {
  651. struct sbus_dev *qec_sdev = child_sdev->parent;
  652. struct sunqec *qecp;
  653. for (qecp = root_qec_dev; qecp; qecp = qecp->next_module) {
  654. if (qecp->qec_sdev == qec_sdev)
  655. break;
  656. }
  657. if (!qecp) {
  658. qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
  659. if (qecp) {
  660. u32 ctrl;
  661. qecp->qec_sdev = qec_sdev;
  662. qecp->gregs = sbus_ioremap(&qec_sdev->resource[0], 0,
  663. GLOB_REG_SIZE,
  664. "QEC Global Registers");
  665. if (!qecp->gregs)
  666. goto fail;
  667. /* Make sure the QEC is in MACE mode. */
  668. ctrl = sbus_readl(qecp->gregs + GLOB_CTRL);
  669. ctrl &= 0xf0000000;
  670. if (ctrl != GLOB_CTRL_MMODE) {
  671. printk(KERN_ERR "qec: Not in MACE mode!\n");
  672. goto fail;
  673. }
  674. if (qec_global_reset(qecp->gregs))
  675. goto fail;
  676. qecp->qec_bursts = qec_get_burst(qec_sdev->ofdev.node);
  677. qec_init_once(qecp, qec_sdev);
  678. if (request_irq(qec_sdev->irqs[0], &qec_interrupt,
  679. IRQF_SHARED, "qec", (void *) qecp)) {
  680. printk(KERN_ERR "qec: Can't register irq.\n");
  681. goto fail;
  682. }
  683. qecp->next_module = root_qec_dev;
  684. root_qec_dev = qecp;
  685. }
  686. }
  687. return qecp;
  688. fail:
  689. if (qecp->gregs)
  690. sbus_iounmap(qecp->gregs, GLOB_REG_SIZE);
  691. kfree(qecp);
  692. return NULL;
  693. }
  694. static int __init qec_ether_init(struct sbus_dev *sdev)
  695. {
  696. static unsigned version_printed;
  697. struct net_device *dev;
  698. struct sunqe *qe;
  699. struct sunqec *qecp;
  700. int i, res;
  701. if (version_printed++ == 0)
  702. printk(KERN_INFO "%s", version);
  703. dev = alloc_etherdev(sizeof(struct sunqe));
  704. if (!dev)
  705. return -ENOMEM;
  706. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  707. qe = netdev_priv(dev);
  708. i = of_getintprop_default(sdev->ofdev.node, "channel#", -1);
  709. if (i == -1) {
  710. struct sbus_dev *td = sdev->parent->child;
  711. i = 0;
  712. while (td != sdev) {
  713. td = td->next;
  714. i++;
  715. }
  716. }
  717. qe->channel = i;
  718. spin_lock_init(&qe->lock);
  719. res = -ENODEV;
  720. qecp = get_qec(sdev);
  721. if (!qecp)
  722. goto fail;
  723. qecp->qes[qe->channel] = qe;
  724. qe->dev = dev;
  725. qe->parent = qecp;
  726. qe->qe_sdev = sdev;
  727. res = -ENOMEM;
  728. qe->qcregs = sbus_ioremap(&qe->qe_sdev->resource[0], 0,
  729. CREG_REG_SIZE, "QEC Channel Registers");
  730. if (!qe->qcregs) {
  731. printk(KERN_ERR "qe: Cannot map channel registers.\n");
  732. goto fail;
  733. }
  734. qe->mregs = sbus_ioremap(&qe->qe_sdev->resource[1], 0,
  735. MREGS_REG_SIZE, "QE MACE Registers");
  736. if (!qe->mregs) {
  737. printk(KERN_ERR "qe: Cannot map MACE registers.\n");
  738. goto fail;
  739. }
  740. qe->qe_block = sbus_alloc_consistent(qe->qe_sdev,
  741. PAGE_SIZE,
  742. &qe->qblock_dvma);
  743. qe->buffers = sbus_alloc_consistent(qe->qe_sdev,
  744. sizeof(struct sunqe_buffers),
  745. &qe->buffers_dvma);
  746. if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
  747. qe->buffers == NULL || qe->buffers_dvma == 0)
  748. goto fail;
  749. /* Stop this QE. */
  750. qe_stop(qe);
  751. SET_MODULE_OWNER(dev);
  752. SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
  753. dev->open = qe_open;
  754. dev->stop = qe_close;
  755. dev->hard_start_xmit = qe_start_xmit;
  756. dev->get_stats = qe_get_stats;
  757. dev->set_multicast_list = qe_set_multicast;
  758. dev->tx_timeout = qe_tx_timeout;
  759. dev->watchdog_timeo = 5*HZ;
  760. dev->irq = sdev->irqs[0];
  761. dev->dma = 0;
  762. dev->ethtool_ops = &qe_ethtool_ops;
  763. res = register_netdev(dev);
  764. if (res)
  765. goto fail;
  766. dev_set_drvdata(&sdev->ofdev.dev, qe);
  767. printk(KERN_INFO "%s: qe channel[%d] ", dev->name, qe->channel);
  768. for (i = 0; i < 6; i++)
  769. printk ("%2.2x%c",
  770. dev->dev_addr[i],
  771. i == 5 ? ' ': ':');
  772. printk("\n");
  773. return 0;
  774. fail:
  775. if (qe->qcregs)
  776. sbus_iounmap(qe->qcregs, CREG_REG_SIZE);
  777. if (qe->mregs)
  778. sbus_iounmap(qe->mregs, MREGS_REG_SIZE);
  779. if (qe->qe_block)
  780. sbus_free_consistent(qe->qe_sdev,
  781. PAGE_SIZE,
  782. qe->qe_block,
  783. qe->qblock_dvma);
  784. if (qe->buffers)
  785. sbus_free_consistent(qe->qe_sdev,
  786. sizeof(struct sunqe_buffers),
  787. qe->buffers,
  788. qe->buffers_dvma);
  789. free_netdev(dev);
  790. return res;
  791. }
  792. static int __devinit qec_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  793. {
  794. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  795. return qec_ether_init(sdev);
  796. }
  797. static int __devexit qec_sbus_remove(struct of_device *dev)
  798. {
  799. struct sunqe *qp = dev_get_drvdata(&dev->dev);
  800. struct net_device *net_dev = qp->dev;
  801. unregister_netdev(net_dev);
  802. sbus_iounmap(qp->qcregs, CREG_REG_SIZE);
  803. sbus_iounmap(qp->mregs, MREGS_REG_SIZE);
  804. sbus_free_consistent(qp->qe_sdev,
  805. PAGE_SIZE,
  806. qp->qe_block,
  807. qp->qblock_dvma);
  808. sbus_free_consistent(qp->qe_sdev,
  809. sizeof(struct sunqe_buffers),
  810. qp->buffers,
  811. qp->buffers_dvma);
  812. free_netdev(net_dev);
  813. dev_set_drvdata(&dev->dev, NULL);
  814. return 0;
  815. }
  816. static struct of_device_id qec_sbus_match[] = {
  817. {
  818. .name = "qe",
  819. },
  820. {},
  821. };
  822. MODULE_DEVICE_TABLE(of, qec_sbus_match);
  823. static struct of_platform_driver qec_sbus_driver = {
  824. .name = "qec",
  825. .match_table = qec_sbus_match,
  826. .probe = qec_sbus_probe,
  827. .remove = __devexit_p(qec_sbus_remove),
  828. };
  829. static int __init qec_init(void)
  830. {
  831. return of_register_driver(&qec_sbus_driver, &sbus_bus_type);
  832. }
  833. static void __exit qec_exit(void)
  834. {
  835. of_unregister_driver(&qec_sbus_driver);
  836. while (root_qec_dev) {
  837. struct sunqec *next = root_qec_dev->next_module;
  838. free_irq(root_qec_dev->qec_sdev->irqs[0],
  839. (void *) root_qec_dev);
  840. sbus_iounmap(root_qec_dev->gregs, GLOB_REG_SIZE);
  841. kfree(root_qec_dev);
  842. root_qec_dev = next;
  843. }
  844. }
  845. module_init(qec_init);
  846. module_exit(qec_exit);