sunqe.c 25 KB

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