sunqe.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* sunqe.c: Sparc QuadEthernet 10baseT SBUS card driver.
  3. * Once again I am out to prove that every ethernet
  4. * controller out there can be most efficiently programmed
  5. * if you make it look like a LANCE.
  6. *
  7. * Copyright (C) 1996, 1999, 2003, 2006, 2008 David S. Miller (davem@davemloft.net)
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/in.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/crc32.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/bitops.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <asm/io.h>
  31. #include <asm/dma.h>
  32. #include <asm/byteorder.h>
  33. #include <asm/idprom.h>
  34. #include <asm/openprom.h>
  35. #include <asm/oplib.h>
  36. #include <asm/auxio.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/irq.h>
  39. #include "sunqe.h"
  40. #define DRV_NAME "sunqe"
  41. #define DRV_VERSION "4.1"
  42. #define DRV_RELDATE "August 27, 2008"
  43. #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
  44. static char version[] =
  45. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  46. MODULE_VERSION(DRV_VERSION);
  47. MODULE_AUTHOR(DRV_AUTHOR);
  48. MODULE_DESCRIPTION("Sun QuadEthernet 10baseT SBUS card driver");
  49. MODULE_LICENSE("GPL");
  50. static struct sunqec *root_qec_dev;
  51. static void qe_set_multicast(struct net_device *dev);
  52. #define QEC_RESET_TRIES 200
  53. static inline int qec_global_reset(void __iomem *gregs)
  54. {
  55. int tries = QEC_RESET_TRIES;
  56. sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
  57. while (--tries) {
  58. u32 tmp = sbus_readl(gregs + GLOB_CTRL);
  59. if (tmp & GLOB_CTRL_RESET) {
  60. udelay(20);
  61. continue;
  62. }
  63. break;
  64. }
  65. if (tries)
  66. return 0;
  67. printk(KERN_ERR "QuadEther: AIEEE cannot reset the QEC!\n");
  68. return -1;
  69. }
  70. #define MACE_RESET_RETRIES 200
  71. #define QE_RESET_RETRIES 200
  72. static inline int qe_stop(struct sunqe *qep)
  73. {
  74. void __iomem *cregs = qep->qcregs;
  75. void __iomem *mregs = qep->mregs;
  76. int tries;
  77. /* Reset the MACE, then the QEC channel. */
  78. sbus_writeb(MREGS_BCONFIG_RESET, mregs + MREGS_BCONFIG);
  79. tries = MACE_RESET_RETRIES;
  80. while (--tries) {
  81. u8 tmp = sbus_readb(mregs + MREGS_BCONFIG);
  82. if (tmp & MREGS_BCONFIG_RESET) {
  83. udelay(20);
  84. continue;
  85. }
  86. break;
  87. }
  88. if (!tries) {
  89. printk(KERN_ERR "QuadEther: AIEEE cannot reset the MACE!\n");
  90. return -1;
  91. }
  92. sbus_writel(CREG_CTRL_RESET, cregs + CREG_CTRL);
  93. tries = QE_RESET_RETRIES;
  94. while (--tries) {
  95. u32 tmp = sbus_readl(cregs + CREG_CTRL);
  96. if (tmp & CREG_CTRL_RESET) {
  97. udelay(20);
  98. continue;
  99. }
  100. break;
  101. }
  102. if (!tries) {
  103. printk(KERN_ERR "QuadEther: Cannot reset QE channel!\n");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. static void qe_init_rings(struct sunqe *qep)
  109. {
  110. struct qe_init_block *qb = qep->qe_block;
  111. struct sunqe_buffers *qbufs = qep->buffers;
  112. __u32 qbufs_dvma = (__u32)qep->buffers_dvma;
  113. int i;
  114. qep->rx_new = qep->rx_old = qep->tx_new = qep->tx_old = 0;
  115. memset(qb, 0, sizeof(struct qe_init_block));
  116. memset(qbufs, 0, sizeof(struct sunqe_buffers));
  117. for (i = 0; i < RX_RING_SIZE; i++) {
  118. qb->qe_rxd[i].rx_addr = qbufs_dvma + qebuf_offset(rx_buf, i);
  119. qb->qe_rxd[i].rx_flags =
  120. (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
  121. }
  122. }
  123. static int qe_init(struct sunqe *qep, int from_irq)
  124. {
  125. struct sunqec *qecp = qep->parent;
  126. void __iomem *cregs = qep->qcregs;
  127. void __iomem *mregs = qep->mregs;
  128. void __iomem *gregs = qecp->gregs;
  129. unsigned char *e = &qep->dev->dev_addr[0];
  130. __u32 qblk_dvma = (__u32)qep->qblock_dvma;
  131. u32 tmp;
  132. int i;
  133. /* Shut it up. */
  134. if (qe_stop(qep))
  135. return -EAGAIN;
  136. /* Setup initial rx/tx init block pointers. */
  137. sbus_writel(qblk_dvma + qib_offset(qe_rxd, 0), cregs + CREG_RXDS);
  138. sbus_writel(qblk_dvma + qib_offset(qe_txd, 0), cregs + CREG_TXDS);
  139. /* Enable/mask the various irq's. */
  140. sbus_writel(0, cregs + CREG_RIMASK);
  141. sbus_writel(1, cregs + CREG_TIMASK);
  142. sbus_writel(0, cregs + CREG_QMASK);
  143. sbus_writel(CREG_MMASK_RXCOLL, cregs + CREG_MMASK);
  144. /* Setup the FIFO pointers into QEC local memory. */
  145. tmp = qep->channel * sbus_readl(gregs + GLOB_MSIZE);
  146. sbus_writel(tmp, cregs + CREG_RXRBUFPTR);
  147. sbus_writel(tmp, cregs + CREG_RXWBUFPTR);
  148. tmp = sbus_readl(cregs + CREG_RXRBUFPTR) +
  149. sbus_readl(gregs + GLOB_RSIZE);
  150. sbus_writel(tmp, cregs + CREG_TXRBUFPTR);
  151. sbus_writel(tmp, cregs + CREG_TXWBUFPTR);
  152. /* Clear the channel collision counter. */
  153. sbus_writel(0, cregs + CREG_CCNT);
  154. /* For 10baseT, inter frame space nor throttle seems to be necessary. */
  155. sbus_writel(0, cregs + CREG_PIPG);
  156. /* Now dork with the AMD MACE. */
  157. sbus_writeb(MREGS_PHYCONFIG_AUTO, mregs + MREGS_PHYCONFIG);
  158. sbus_writeb(MREGS_TXFCNTL_AUTOPAD, mregs + MREGS_TXFCNTL);
  159. sbus_writeb(0, mregs + MREGS_RXFCNTL);
  160. /* The QEC dma's the rx'd packets from local memory out to main memory,
  161. * and therefore it interrupts when the packet reception is "complete".
  162. * So don't listen for the MACE talking about it.
  163. */
  164. sbus_writeb(MREGS_IMASK_COLL | MREGS_IMASK_RXIRQ, mregs + MREGS_IMASK);
  165. sbus_writeb(MREGS_BCONFIG_BSWAP | MREGS_BCONFIG_64TS, mregs + MREGS_BCONFIG);
  166. sbus_writeb((MREGS_FCONFIG_TXF16 | MREGS_FCONFIG_RXF32 |
  167. MREGS_FCONFIG_RFWU | MREGS_FCONFIG_TFWU),
  168. mregs + MREGS_FCONFIG);
  169. /* Only usable interface on QuadEther is twisted pair. */
  170. sbus_writeb(MREGS_PLSCONFIG_TP, mregs + MREGS_PLSCONFIG);
  171. /* Tell MACE we are changing the ether address. */
  172. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_PARESET,
  173. mregs + MREGS_IACONFIG);
  174. while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  175. barrier();
  176. sbus_writeb(e[0], mregs + MREGS_ETHADDR);
  177. sbus_writeb(e[1], mregs + MREGS_ETHADDR);
  178. sbus_writeb(e[2], mregs + MREGS_ETHADDR);
  179. sbus_writeb(e[3], mregs + MREGS_ETHADDR);
  180. sbus_writeb(e[4], mregs + MREGS_ETHADDR);
  181. sbus_writeb(e[5], mregs + MREGS_ETHADDR);
  182. /* Clear out the address filter. */
  183. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  184. mregs + MREGS_IACONFIG);
  185. while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  186. barrier();
  187. for (i = 0; i < 8; i++)
  188. sbus_writeb(0, mregs + MREGS_FILTER);
  189. /* Address changes are now complete. */
  190. sbus_writeb(0, mregs + MREGS_IACONFIG);
  191. qe_init_rings(qep);
  192. /* Wait a little bit for the link to come up... */
  193. mdelay(5);
  194. if (!(sbus_readb(mregs + MREGS_PHYCONFIG) & MREGS_PHYCONFIG_LTESTDIS)) {
  195. int tries = 50;
  196. while (--tries) {
  197. u8 tmp;
  198. mdelay(5);
  199. barrier();
  200. tmp = sbus_readb(mregs + MREGS_PHYCONFIG);
  201. if ((tmp & MREGS_PHYCONFIG_LSTAT) != 0)
  202. break;
  203. }
  204. if (tries == 0)
  205. printk(KERN_NOTICE "%s: Warning, link state is down.\n", qep->dev->name);
  206. }
  207. /* Missed packet counter is cleared on a read. */
  208. sbus_readb(mregs + MREGS_MPCNT);
  209. /* Reload multicast information, this will enable the receiver
  210. * and transmitter.
  211. */
  212. qe_set_multicast(qep->dev);
  213. /* QEC should now start to show interrupts. */
  214. return 0;
  215. }
  216. /* Grrr, certain error conditions completely lock up the AMD MACE,
  217. * so when we get these we _must_ reset the chip.
  218. */
  219. static int qe_is_bolixed(struct sunqe *qep, u32 qe_status)
  220. {
  221. struct net_device *dev = qep->dev;
  222. int mace_hwbug_workaround = 0;
  223. if (qe_status & CREG_STAT_EDEFER) {
  224. printk(KERN_ERR "%s: Excessive transmit defers.\n", dev->name);
  225. dev->stats.tx_errors++;
  226. }
  227. if (qe_status & CREG_STAT_CLOSS) {
  228. printk(KERN_ERR "%s: Carrier lost, link down?\n", dev->name);
  229. dev->stats.tx_errors++;
  230. dev->stats.tx_carrier_errors++;
  231. }
  232. if (qe_status & CREG_STAT_ERETRIES) {
  233. printk(KERN_ERR "%s: Excessive transmit retries (more than 16).\n", dev->name);
  234. dev->stats.tx_errors++;
  235. mace_hwbug_workaround = 1;
  236. }
  237. if (qe_status & CREG_STAT_LCOLL) {
  238. printk(KERN_ERR "%s: Late transmit collision.\n", dev->name);
  239. dev->stats.tx_errors++;
  240. dev->stats.collisions++;
  241. mace_hwbug_workaround = 1;
  242. }
  243. if (qe_status & CREG_STAT_FUFLOW) {
  244. printk(KERN_ERR "%s: Transmit fifo underflow, driver bug.\n", dev->name);
  245. dev->stats.tx_errors++;
  246. mace_hwbug_workaround = 1;
  247. }
  248. if (qe_status & CREG_STAT_JERROR) {
  249. printk(KERN_ERR "%s: Jabber error.\n", dev->name);
  250. }
  251. if (qe_status & CREG_STAT_BERROR) {
  252. printk(KERN_ERR "%s: Babble error.\n", dev->name);
  253. }
  254. if (qe_status & CREG_STAT_CCOFLOW) {
  255. dev->stats.tx_errors += 256;
  256. dev->stats.collisions += 256;
  257. }
  258. if (qe_status & CREG_STAT_TXDERROR) {
  259. printk(KERN_ERR "%s: Transmit descriptor is bogus, driver bug.\n", dev->name);
  260. dev->stats.tx_errors++;
  261. dev->stats.tx_aborted_errors++;
  262. mace_hwbug_workaround = 1;
  263. }
  264. if (qe_status & CREG_STAT_TXLERR) {
  265. printk(KERN_ERR "%s: Transmit late error.\n", dev->name);
  266. dev->stats.tx_errors++;
  267. mace_hwbug_workaround = 1;
  268. }
  269. if (qe_status & CREG_STAT_TXPERR) {
  270. printk(KERN_ERR "%s: Transmit DMA parity error.\n", dev->name);
  271. dev->stats.tx_errors++;
  272. dev->stats.tx_aborted_errors++;
  273. mace_hwbug_workaround = 1;
  274. }
  275. if (qe_status & CREG_STAT_TXSERR) {
  276. printk(KERN_ERR "%s: Transmit DMA sbus error ack.\n", dev->name);
  277. dev->stats.tx_errors++;
  278. dev->stats.tx_aborted_errors++;
  279. mace_hwbug_workaround = 1;
  280. }
  281. if (qe_status & CREG_STAT_RCCOFLOW) {
  282. dev->stats.rx_errors += 256;
  283. dev->stats.collisions += 256;
  284. }
  285. if (qe_status & CREG_STAT_RUOFLOW) {
  286. dev->stats.rx_errors += 256;
  287. dev->stats.rx_over_errors += 256;
  288. }
  289. if (qe_status & CREG_STAT_MCOFLOW) {
  290. dev->stats.rx_errors += 256;
  291. dev->stats.rx_missed_errors += 256;
  292. }
  293. if (qe_status & CREG_STAT_RXFOFLOW) {
  294. printk(KERN_ERR "%s: Receive fifo overflow.\n", dev->name);
  295. dev->stats.rx_errors++;
  296. dev->stats.rx_over_errors++;
  297. }
  298. if (qe_status & CREG_STAT_RLCOLL) {
  299. printk(KERN_ERR "%s: Late receive collision.\n", dev->name);
  300. dev->stats.rx_errors++;
  301. dev->stats.collisions++;
  302. }
  303. if (qe_status & CREG_STAT_FCOFLOW) {
  304. dev->stats.rx_errors += 256;
  305. dev->stats.rx_frame_errors += 256;
  306. }
  307. if (qe_status & CREG_STAT_CECOFLOW) {
  308. dev->stats.rx_errors += 256;
  309. dev->stats.rx_crc_errors += 256;
  310. }
  311. if (qe_status & CREG_STAT_RXDROP) {
  312. printk(KERN_ERR "%s: Receive packet dropped.\n", dev->name);
  313. dev->stats.rx_errors++;
  314. dev->stats.rx_dropped++;
  315. dev->stats.rx_missed_errors++;
  316. }
  317. if (qe_status & CREG_STAT_RXSMALL) {
  318. printk(KERN_ERR "%s: Receive buffer too small, driver bug.\n", dev->name);
  319. dev->stats.rx_errors++;
  320. dev->stats.rx_length_errors++;
  321. }
  322. if (qe_status & CREG_STAT_RXLERR) {
  323. printk(KERN_ERR "%s: Receive late error.\n", dev->name);
  324. dev->stats.rx_errors++;
  325. mace_hwbug_workaround = 1;
  326. }
  327. if (qe_status & CREG_STAT_RXPERR) {
  328. printk(KERN_ERR "%s: Receive DMA parity error.\n", dev->name);
  329. dev->stats.rx_errors++;
  330. dev->stats.rx_missed_errors++;
  331. mace_hwbug_workaround = 1;
  332. }
  333. if (qe_status & CREG_STAT_RXSERR) {
  334. printk(KERN_ERR "%s: Receive DMA sbus error ack.\n", dev->name);
  335. dev->stats.rx_errors++;
  336. dev->stats.rx_missed_errors++;
  337. mace_hwbug_workaround = 1;
  338. }
  339. if (mace_hwbug_workaround)
  340. qe_init(qep, 1);
  341. return mace_hwbug_workaround;
  342. }
  343. /* Per-QE receive interrupt service routine. Just like on the happy meal
  344. * we receive directly into skb's with a small packet copy water mark.
  345. */
  346. static void qe_rx(struct sunqe *qep)
  347. {
  348. struct qe_rxd *rxbase = &qep->qe_block->qe_rxd[0];
  349. struct net_device *dev = qep->dev;
  350. struct qe_rxd *this;
  351. struct sunqe_buffers *qbufs = qep->buffers;
  352. __u32 qbufs_dvma = (__u32)qep->buffers_dvma;
  353. int elem = qep->rx_new;
  354. u32 flags;
  355. this = &rxbase[elem];
  356. while (!((flags = this->rx_flags) & RXD_OWN)) {
  357. struct sk_buff *skb;
  358. unsigned char *this_qbuf =
  359. &qbufs->rx_buf[elem & (RX_RING_SIZE - 1)][0];
  360. __u32 this_qbuf_dvma = qbufs_dvma +
  361. qebuf_offset(rx_buf, (elem & (RX_RING_SIZE - 1)));
  362. struct qe_rxd *end_rxd =
  363. &rxbase[(elem+RX_RING_SIZE)&(RX_RING_MAXSIZE-1)];
  364. int len = (flags & RXD_LENGTH) - 4; /* QE adds ether FCS size to len */
  365. /* Check for errors. */
  366. if (len < ETH_ZLEN) {
  367. dev->stats.rx_errors++;
  368. dev->stats.rx_length_errors++;
  369. dev->stats.rx_dropped++;
  370. } else {
  371. skb = netdev_alloc_skb(dev, len + 2);
  372. if (skb == NULL) {
  373. dev->stats.rx_dropped++;
  374. } else {
  375. skb_reserve(skb, 2);
  376. skb_put(skb, len);
  377. skb_copy_to_linear_data(skb, this_qbuf,
  378. len);
  379. skb->protocol = eth_type_trans(skb, qep->dev);
  380. netif_rx(skb);
  381. dev->stats.rx_packets++;
  382. dev->stats.rx_bytes += len;
  383. }
  384. }
  385. end_rxd->rx_addr = this_qbuf_dvma;
  386. end_rxd->rx_flags = (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
  387. elem = NEXT_RX(elem);
  388. this = &rxbase[elem];
  389. }
  390. qep->rx_new = elem;
  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 = netdev_priv(dev);
  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 = netdev_priv(dev);
  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 = netdev_priv(dev);
  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 netdev_tx_t qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
  485. {
  486. struct sunqe *qep = netdev_priv(dev);
  487. struct sunqe_buffers *qbufs = qep->buffers;
  488. __u32 txbuf_dvma, qbufs_dvma = (__u32)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. sbus_writel(CREG_CTRL_TWAKEUP, qep->qcregs + CREG_CTRL);
  507. dev->stats.tx_packets++;
  508. dev->stats.tx_bytes += len;
  509. if (TX_BUFFS_AVAIL(qep) <= 0) {
  510. /* Halt the net queue and enable tx interrupts.
  511. * When the tx queue empties the tx irq handler
  512. * will wake up the queue and return us back to
  513. * the lazy tx reclaim scheme.
  514. */
  515. netif_stop_queue(dev);
  516. sbus_writel(0, qep->qcregs + CREG_TIMASK);
  517. }
  518. spin_unlock_irq(&qep->lock);
  519. dev_kfree_skb(skb);
  520. return NETDEV_TX_OK;
  521. }
  522. static void qe_set_multicast(struct net_device *dev)
  523. {
  524. struct sunqe *qep = netdev_priv(dev);
  525. struct netdev_hw_addr *ha;
  526. u8 new_mconfig = qep->mconfig;
  527. int i;
  528. u32 crc;
  529. /* Lock out others. */
  530. netif_stop_queue(dev);
  531. if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
  532. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  533. qep->mregs + MREGS_IACONFIG);
  534. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  535. barrier();
  536. for (i = 0; i < 8; i++)
  537. sbus_writeb(0xff, qep->mregs + MREGS_FILTER);
  538. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  539. } else if (dev->flags & IFF_PROMISC) {
  540. new_mconfig |= MREGS_MCONFIG_PROMISC;
  541. } else {
  542. u16 hash_table[4];
  543. u8 *hbytes = (unsigned char *) &hash_table[0];
  544. memset(hash_table, 0, sizeof(hash_table));
  545. netdev_for_each_mc_addr(ha, dev) {
  546. crc = ether_crc_le(6, ha->addr);
  547. crc >>= 26;
  548. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  549. }
  550. /* Program the qe with the new filter value. */
  551. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  552. qep->mregs + MREGS_IACONFIG);
  553. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  554. barrier();
  555. for (i = 0; i < 8; i++) {
  556. u8 tmp = *hbytes++;
  557. sbus_writeb(tmp, qep->mregs + MREGS_FILTER);
  558. }
  559. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  560. }
  561. /* Any change of the logical address filter, the physical address,
  562. * or enabling/disabling promiscuous mode causes the MACE to disable
  563. * the receiver. So we must re-enable them here or else the MACE
  564. * refuses to listen to anything on the network. Sheesh, took
  565. * me a day or two to find this bug.
  566. */
  567. qep->mconfig = new_mconfig;
  568. sbus_writeb(qep->mconfig, qep->mregs + MREGS_MCONFIG);
  569. /* Let us get going again. */
  570. netif_wake_queue(dev);
  571. }
  572. /* Ethtool support... */
  573. static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  574. {
  575. const struct linux_prom_registers *regs;
  576. struct sunqe *qep = netdev_priv(dev);
  577. struct platform_device *op;
  578. strlcpy(info->driver, "sunqe", sizeof(info->driver));
  579. strlcpy(info->version, "3.0", sizeof(info->version));
  580. op = qep->op;
  581. regs = of_get_property(op->dev.of_node, "reg", NULL);
  582. if (regs)
  583. snprintf(info->bus_info, sizeof(info->bus_info), "SBUS:%d",
  584. regs->which_io);
  585. }
  586. static u32 qe_get_link(struct net_device *dev)
  587. {
  588. struct sunqe *qep = netdev_priv(dev);
  589. void __iomem *mregs = qep->mregs;
  590. u8 phyconfig;
  591. spin_lock_irq(&qep->lock);
  592. phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
  593. spin_unlock_irq(&qep->lock);
  594. return phyconfig & MREGS_PHYCONFIG_LSTAT;
  595. }
  596. static const struct ethtool_ops qe_ethtool_ops = {
  597. .get_drvinfo = qe_get_drvinfo,
  598. .get_link = qe_get_link,
  599. };
  600. /* This is only called once at boot time for each card probed. */
  601. static void qec_init_once(struct sunqec *qecp, struct platform_device *op)
  602. {
  603. u8 bsizes = qecp->qec_bursts;
  604. if (sbus_can_burst64() && (bsizes & DMA_BURST64)) {
  605. sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
  606. } else if (bsizes & DMA_BURST32) {
  607. sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
  608. } else {
  609. sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
  610. }
  611. /* Packetsize only used in 100baseT BigMAC configurations,
  612. * set it to zero just to be on the safe side.
  613. */
  614. sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
  615. /* Set the local memsize register, divided up to one piece per QE channel. */
  616. sbus_writel((resource_size(&op->resource[1]) >> 2),
  617. qecp->gregs + GLOB_MSIZE);
  618. /* Divide up the local QEC memory amongst the 4 QE receiver and
  619. * transmitter FIFOs. Basically it is (total / 2 / num_channels).
  620. */
  621. sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
  622. qecp->gregs + GLOB_TSIZE);
  623. sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
  624. qecp->gregs + GLOB_RSIZE);
  625. }
  626. static u8 qec_get_burst(struct device_node *dp)
  627. {
  628. u8 bsizes, bsizes_more;
  629. /* Find and set the burst sizes for the QEC, since it
  630. * does the actual dma for all 4 channels.
  631. */
  632. bsizes = of_getintprop_default(dp, "burst-sizes", 0xff);
  633. bsizes &= 0xff;
  634. bsizes_more = of_getintprop_default(dp->parent, "burst-sizes", 0xff);
  635. if (bsizes_more != 0xff)
  636. bsizes &= bsizes_more;
  637. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  638. (bsizes & DMA_BURST32)==0)
  639. bsizes = (DMA_BURST32 - 1);
  640. return bsizes;
  641. }
  642. static struct sunqec *get_qec(struct platform_device *child)
  643. {
  644. struct platform_device *op = to_platform_device(child->dev.parent);
  645. struct sunqec *qecp;
  646. qecp = platform_get_drvdata(op);
  647. if (!qecp) {
  648. qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
  649. if (qecp) {
  650. u32 ctrl;
  651. qecp->op = op;
  652. qecp->gregs = of_ioremap(&op->resource[0], 0,
  653. GLOB_REG_SIZE,
  654. "QEC Global Registers");
  655. if (!qecp->gregs)
  656. goto fail;
  657. /* Make sure the QEC is in MACE mode. */
  658. ctrl = sbus_readl(qecp->gregs + GLOB_CTRL);
  659. ctrl &= 0xf0000000;
  660. if (ctrl != GLOB_CTRL_MMODE) {
  661. printk(KERN_ERR "qec: Not in MACE mode!\n");
  662. goto fail;
  663. }
  664. if (qec_global_reset(qecp->gregs))
  665. goto fail;
  666. qecp->qec_bursts = qec_get_burst(op->dev.of_node);
  667. qec_init_once(qecp, op);
  668. if (request_irq(op->archdata.irqs[0], qec_interrupt,
  669. IRQF_SHARED, "qec", (void *) qecp)) {
  670. printk(KERN_ERR "qec: Can't register irq.\n");
  671. goto fail;
  672. }
  673. platform_set_drvdata(op, qecp);
  674. qecp->next_module = root_qec_dev;
  675. root_qec_dev = qecp;
  676. }
  677. }
  678. return qecp;
  679. fail:
  680. if (qecp->gregs)
  681. of_iounmap(&op->resource[0], qecp->gregs, GLOB_REG_SIZE);
  682. kfree(qecp);
  683. return NULL;
  684. }
  685. static const struct net_device_ops qec_ops = {
  686. .ndo_open = qe_open,
  687. .ndo_stop = qe_close,
  688. .ndo_start_xmit = qe_start_xmit,
  689. .ndo_set_rx_mode = qe_set_multicast,
  690. .ndo_tx_timeout = qe_tx_timeout,
  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);