sunqe.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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/system.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 = 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 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(qep->qblock_dvma + qib_offset(qe_rxd, 0), cregs + CREG_RXDS);
  137. sbus_writel(qep->qblock_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 = qep->buffers_dvma;
  352. int elem = qep->rx_new, drops = 0;
  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 = dev_alloc_skb(len + 2);
  371. if (skb == NULL) {
  372. drops++;
  373. dev->stats.rx_dropped++;
  374. } else {
  375. skb_reserve(skb, 2);
  376. skb_put(skb, len);
  377. skb_copy_to_linear_data(skb, (unsigned char *) 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. if (drops)
  392. printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", qep->dev->name);
  393. }
  394. static void qe_tx_reclaim(struct sunqe *qep);
  395. /* Interrupts for all QE's get filtered out via the QEC master controller,
  396. * so we just run through each qe and check to see who is signaling
  397. * and thus needs to be serviced.
  398. */
  399. static irqreturn_t qec_interrupt(int irq, void *dev_id)
  400. {
  401. struct sunqec *qecp = dev_id;
  402. u32 qec_status;
  403. int channel = 0;
  404. /* Latch the status now. */
  405. qec_status = sbus_readl(qecp->gregs + GLOB_STAT);
  406. while (channel < 4) {
  407. if (qec_status & 0xf) {
  408. struct sunqe *qep = qecp->qes[channel];
  409. u32 qe_status;
  410. qe_status = sbus_readl(qep->qcregs + CREG_STAT);
  411. if (qe_status & CREG_STAT_ERRORS) {
  412. if (qe_is_bolixed(qep, qe_status))
  413. goto next;
  414. }
  415. if (qe_status & CREG_STAT_RXIRQ)
  416. qe_rx(qep);
  417. if (netif_queue_stopped(qep->dev) &&
  418. (qe_status & CREG_STAT_TXIRQ)) {
  419. spin_lock(&qep->lock);
  420. qe_tx_reclaim(qep);
  421. if (TX_BUFFS_AVAIL(qep) > 0) {
  422. /* Wake net queue and return to
  423. * lazy tx reclaim.
  424. */
  425. netif_wake_queue(qep->dev);
  426. sbus_writel(1, qep->qcregs + CREG_TIMASK);
  427. }
  428. spin_unlock(&qep->lock);
  429. }
  430. next:
  431. ;
  432. }
  433. qec_status >>= 4;
  434. channel++;
  435. }
  436. return IRQ_HANDLED;
  437. }
  438. static int qe_open(struct net_device *dev)
  439. {
  440. struct sunqe *qep = netdev_priv(dev);
  441. qep->mconfig = (MREGS_MCONFIG_TXENAB |
  442. MREGS_MCONFIG_RXENAB |
  443. MREGS_MCONFIG_MBAENAB);
  444. return qe_init(qep, 0);
  445. }
  446. static int qe_close(struct net_device *dev)
  447. {
  448. struct sunqe *qep = netdev_priv(dev);
  449. qe_stop(qep);
  450. return 0;
  451. }
  452. /* Reclaim TX'd frames from the ring. This must always run under
  453. * the IRQ protected qep->lock.
  454. */
  455. static void qe_tx_reclaim(struct sunqe *qep)
  456. {
  457. struct qe_txd *txbase = &qep->qe_block->qe_txd[0];
  458. int elem = qep->tx_old;
  459. while (elem != qep->tx_new) {
  460. u32 flags = txbase[elem].tx_flags;
  461. if (flags & TXD_OWN)
  462. break;
  463. elem = NEXT_TX(elem);
  464. }
  465. qep->tx_old = elem;
  466. }
  467. static void qe_tx_timeout(struct net_device *dev)
  468. {
  469. struct sunqe *qep = netdev_priv(dev);
  470. int tx_full;
  471. spin_lock_irq(&qep->lock);
  472. /* Try to reclaim, if that frees up some tx
  473. * entries, we're fine.
  474. */
  475. qe_tx_reclaim(qep);
  476. tx_full = TX_BUFFS_AVAIL(qep) <= 0;
  477. spin_unlock_irq(&qep->lock);
  478. if (! tx_full)
  479. goto out;
  480. printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  481. qe_init(qep, 1);
  482. out:
  483. netif_wake_queue(dev);
  484. }
  485. /* Get a packet queued to go onto the wire. */
  486. static int qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
  487. {
  488. struct sunqe *qep = netdev_priv(dev);
  489. struct sunqe_buffers *qbufs = qep->buffers;
  490. __u32 txbuf_dvma, qbufs_dvma = qep->buffers_dvma;
  491. unsigned char *txbuf;
  492. int len, entry;
  493. spin_lock_irq(&qep->lock);
  494. qe_tx_reclaim(qep);
  495. len = skb->len;
  496. entry = qep->tx_new;
  497. txbuf = &qbufs->tx_buf[entry & (TX_RING_SIZE - 1)][0];
  498. txbuf_dvma = qbufs_dvma +
  499. qebuf_offset(tx_buf, (entry & (TX_RING_SIZE - 1)));
  500. /* Avoid a race... */
  501. qep->qe_block->qe_txd[entry].tx_flags = TXD_UPDATE;
  502. skb_copy_from_linear_data(skb, txbuf, len);
  503. qep->qe_block->qe_txd[entry].tx_addr = txbuf_dvma;
  504. qep->qe_block->qe_txd[entry].tx_flags =
  505. (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
  506. qep->tx_new = NEXT_TX(entry);
  507. /* Get it going. */
  508. sbus_writel(CREG_CTRL_TWAKEUP, qep->qcregs + CREG_CTRL);
  509. dev->stats.tx_packets++;
  510. dev->stats.tx_bytes += len;
  511. if (TX_BUFFS_AVAIL(qep) <= 0) {
  512. /* Halt the net queue and enable tx interrupts.
  513. * When the tx queue empties the tx irq handler
  514. * will wake up the queue and return us back to
  515. * the lazy tx reclaim scheme.
  516. */
  517. netif_stop_queue(dev);
  518. sbus_writel(0, qep->qcregs + CREG_TIMASK);
  519. }
  520. spin_unlock_irq(&qep->lock);
  521. dev_kfree_skb(skb);
  522. return NETDEV_TX_OK;
  523. }
  524. static void qe_set_multicast(struct net_device *dev)
  525. {
  526. struct sunqe *qep = netdev_priv(dev);
  527. struct netdev_hw_addr *ha;
  528. u8 new_mconfig = qep->mconfig;
  529. char *addrs;
  530. int i;
  531. u32 crc;
  532. /* Lock out others. */
  533. netif_stop_queue(dev);
  534. if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
  535. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  536. qep->mregs + MREGS_IACONFIG);
  537. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  538. barrier();
  539. for (i = 0; i < 8; i++)
  540. sbus_writeb(0xff, qep->mregs + MREGS_FILTER);
  541. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  542. } else if (dev->flags & IFF_PROMISC) {
  543. new_mconfig |= MREGS_MCONFIG_PROMISC;
  544. } else {
  545. u16 hash_table[4];
  546. u8 *hbytes = (unsigned char *) &hash_table[0];
  547. memset(hash_table, 0, sizeof(hash_table));
  548. netdev_for_each_mc_addr(ha, dev) {
  549. addrs = ha->addr;
  550. if (!(*addrs & 1))
  551. continue;
  552. crc = ether_crc_le(6, addrs);
  553. crc >>= 26;
  554. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  555. }
  556. /* Program the qe with the new filter value. */
  557. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  558. qep->mregs + MREGS_IACONFIG);
  559. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  560. barrier();
  561. for (i = 0; i < 8; i++) {
  562. u8 tmp = *hbytes++;
  563. sbus_writeb(tmp, qep->mregs + MREGS_FILTER);
  564. }
  565. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  566. }
  567. /* Any change of the logical address filter, the physical address,
  568. * or enabling/disabling promiscuous mode causes the MACE to disable
  569. * the receiver. So we must re-enable them here or else the MACE
  570. * refuses to listen to anything on the network. Sheesh, took
  571. * me a day or two to find this bug.
  572. */
  573. qep->mconfig = new_mconfig;
  574. sbus_writeb(qep->mconfig, qep->mregs + MREGS_MCONFIG);
  575. /* Let us get going again. */
  576. netif_wake_queue(dev);
  577. }
  578. /* Ethtool support... */
  579. static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  580. {
  581. const struct linux_prom_registers *regs;
  582. struct sunqe *qep = netdev_priv(dev);
  583. struct of_device *op;
  584. strcpy(info->driver, "sunqe");
  585. strcpy(info->version, "3.0");
  586. op = qep->op;
  587. regs = of_get_property(op->dev.of_node, "reg", NULL);
  588. if (regs)
  589. sprintf(info->bus_info, "SBUS:%d", regs->which_io);
  590. }
  591. static u32 qe_get_link(struct net_device *dev)
  592. {
  593. struct sunqe *qep = netdev_priv(dev);
  594. void __iomem *mregs = qep->mregs;
  595. u8 phyconfig;
  596. spin_lock_irq(&qep->lock);
  597. phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
  598. spin_unlock_irq(&qep->lock);
  599. return (phyconfig & MREGS_PHYCONFIG_LSTAT);
  600. }
  601. static const struct ethtool_ops qe_ethtool_ops = {
  602. .get_drvinfo = qe_get_drvinfo,
  603. .get_link = qe_get_link,
  604. };
  605. /* This is only called once at boot time for each card probed. */
  606. static void qec_init_once(struct sunqec *qecp, struct of_device *op)
  607. {
  608. u8 bsizes = qecp->qec_bursts;
  609. if (sbus_can_burst64() && (bsizes & DMA_BURST64)) {
  610. sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
  611. } else if (bsizes & DMA_BURST32) {
  612. sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
  613. } else {
  614. sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
  615. }
  616. /* Packetsize only used in 100baseT BigMAC configurations,
  617. * set it to zero just to be on the safe side.
  618. */
  619. sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
  620. /* Set the local memsize register, divided up to one piece per QE channel. */
  621. sbus_writel((resource_size(&op->resource[1]) >> 2),
  622. qecp->gregs + GLOB_MSIZE);
  623. /* Divide up the local QEC memory amongst the 4 QE receiver and
  624. * transmitter FIFOs. Basically it is (total / 2 / num_channels).
  625. */
  626. sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
  627. qecp->gregs + GLOB_TSIZE);
  628. sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
  629. qecp->gregs + GLOB_RSIZE);
  630. }
  631. static u8 __devinit qec_get_burst(struct device_node *dp)
  632. {
  633. u8 bsizes, bsizes_more;
  634. /* Find and set the burst sizes for the QEC, since it
  635. * does the actual dma for all 4 channels.
  636. */
  637. bsizes = of_getintprop_default(dp, "burst-sizes", 0xff);
  638. bsizes &= 0xff;
  639. bsizes_more = of_getintprop_default(dp->parent, "burst-sizes", 0xff);
  640. if (bsizes_more != 0xff)
  641. bsizes &= bsizes_more;
  642. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  643. (bsizes & DMA_BURST32)==0)
  644. bsizes = (DMA_BURST32 - 1);
  645. return bsizes;
  646. }
  647. static struct sunqec * __devinit get_qec(struct of_device *child)
  648. {
  649. struct of_device *op = to_of_device(child->dev.parent);
  650. struct sunqec *qecp;
  651. qecp = dev_get_drvdata(&op->dev);
  652. if (!qecp) {
  653. qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
  654. if (qecp) {
  655. u32 ctrl;
  656. qecp->op = op;
  657. qecp->gregs = of_ioremap(&op->resource[0], 0,
  658. GLOB_REG_SIZE,
  659. "QEC Global Registers");
  660. if (!qecp->gregs)
  661. goto fail;
  662. /* Make sure the QEC is in MACE mode. */
  663. ctrl = sbus_readl(qecp->gregs + GLOB_CTRL);
  664. ctrl &= 0xf0000000;
  665. if (ctrl != GLOB_CTRL_MMODE) {
  666. printk(KERN_ERR "qec: Not in MACE mode!\n");
  667. goto fail;
  668. }
  669. if (qec_global_reset(qecp->gregs))
  670. goto fail;
  671. qecp->qec_bursts = qec_get_burst(op->dev.of_node);
  672. qec_init_once(qecp, op);
  673. if (request_irq(op->irqs[0], qec_interrupt,
  674. IRQF_SHARED, "qec", (void *) qecp)) {
  675. printk(KERN_ERR "qec: Can't register irq.\n");
  676. goto fail;
  677. }
  678. dev_set_drvdata(&op->dev, qecp);
  679. qecp->next_module = root_qec_dev;
  680. root_qec_dev = qecp;
  681. }
  682. }
  683. return qecp;
  684. fail:
  685. if (qecp->gregs)
  686. of_iounmap(&op->resource[0], qecp->gregs, GLOB_REG_SIZE);
  687. kfree(qecp);
  688. return NULL;
  689. }
  690. static const struct net_device_ops qec_ops = {
  691. .ndo_open = qe_open,
  692. .ndo_stop = qe_close,
  693. .ndo_start_xmit = qe_start_xmit,
  694. .ndo_set_multicast_list = qe_set_multicast,
  695. .ndo_tx_timeout = qe_tx_timeout,
  696. .ndo_change_mtu = eth_change_mtu,
  697. .ndo_set_mac_address = eth_mac_addr,
  698. .ndo_validate_addr = eth_validate_addr,
  699. };
  700. static int __devinit qec_ether_init(struct of_device *op)
  701. {
  702. static unsigned version_printed;
  703. struct net_device *dev;
  704. struct sunqec *qecp;
  705. struct sunqe *qe;
  706. int i, res;
  707. if (version_printed++ == 0)
  708. printk(KERN_INFO "%s", version);
  709. dev = alloc_etherdev(sizeof(struct sunqe));
  710. if (!dev)
  711. return -ENOMEM;
  712. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  713. qe = netdev_priv(dev);
  714. res = -ENODEV;
  715. i = of_getintprop_default(op->dev.of_node, "channel#", -1);
  716. if (i == -1)
  717. goto fail;
  718. qe->channel = i;
  719. spin_lock_init(&qe->lock);
  720. qecp = get_qec(op);
  721. if (!qecp)
  722. goto fail;
  723. qecp->qes[qe->channel] = qe;
  724. qe->dev = dev;
  725. qe->parent = qecp;
  726. qe->op = op;
  727. res = -ENOMEM;
  728. qe->qcregs = of_ioremap(&op->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 = of_ioremap(&op->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 = dma_alloc_coherent(&op->dev, PAGE_SIZE,
  741. &qe->qblock_dvma, GFP_ATOMIC);
  742. qe->buffers = dma_alloc_coherent(&op->dev, sizeof(struct sunqe_buffers),
  743. &qe->buffers_dvma, GFP_ATOMIC);
  744. if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
  745. qe->buffers == NULL || qe->buffers_dvma == 0)
  746. goto fail;
  747. /* Stop this QE. */
  748. qe_stop(qe);
  749. SET_NETDEV_DEV(dev, &op->dev);
  750. dev->watchdog_timeo = 5*HZ;
  751. dev->irq = op->irqs[0];
  752. dev->dma = 0;
  753. dev->ethtool_ops = &qe_ethtool_ops;
  754. dev->netdev_ops = &qec_ops;
  755. res = register_netdev(dev);
  756. if (res)
  757. goto fail;
  758. dev_set_drvdata(&op->dev, qe);
  759. printk(KERN_INFO "%s: qe channel[%d] ", dev->name, qe->channel);
  760. for (i = 0; i < 6; i++)
  761. printk ("%2.2x%c",
  762. dev->dev_addr[i],
  763. i == 5 ? ' ': ':');
  764. printk("\n");
  765. return 0;
  766. fail:
  767. if (qe->qcregs)
  768. of_iounmap(&op->resource[0], qe->qcregs, CREG_REG_SIZE);
  769. if (qe->mregs)
  770. of_iounmap(&op->resource[1], qe->mregs, MREGS_REG_SIZE);
  771. if (qe->qe_block)
  772. dma_free_coherent(&op->dev, PAGE_SIZE,
  773. qe->qe_block, qe->qblock_dvma);
  774. if (qe->buffers)
  775. dma_free_coherent(&op->dev,
  776. sizeof(struct sunqe_buffers),
  777. qe->buffers,
  778. qe->buffers_dvma);
  779. free_netdev(dev);
  780. return res;
  781. }
  782. static int __devinit qec_sbus_probe(struct of_device *op, const struct of_device_id *match)
  783. {
  784. return qec_ether_init(op);
  785. }
  786. static int __devexit qec_sbus_remove(struct of_device *op)
  787. {
  788. struct sunqe *qp = dev_get_drvdata(&op->dev);
  789. struct net_device *net_dev = qp->dev;
  790. unregister_netdev(net_dev);
  791. of_iounmap(&op->resource[0], qp->qcregs, CREG_REG_SIZE);
  792. of_iounmap(&op->resource[1], qp->mregs, MREGS_REG_SIZE);
  793. dma_free_coherent(&op->dev, PAGE_SIZE,
  794. qp->qe_block, qp->qblock_dvma);
  795. dma_free_coherent(&op->dev, sizeof(struct sunqe_buffers),
  796. qp->buffers, qp->buffers_dvma);
  797. free_netdev(net_dev);
  798. dev_set_drvdata(&op->dev, NULL);
  799. return 0;
  800. }
  801. static const struct of_device_id qec_sbus_match[] = {
  802. {
  803. .name = "qe",
  804. },
  805. {},
  806. };
  807. MODULE_DEVICE_TABLE(of, qec_sbus_match);
  808. static struct of_platform_driver qec_sbus_driver = {
  809. .driver = {
  810. .name = "qec",
  811. .owner = THIS_MODULE,
  812. .of_match_table = qec_sbus_match,
  813. },
  814. .probe = qec_sbus_probe,
  815. .remove = __devexit_p(qec_sbus_remove),
  816. };
  817. static int __init qec_init(void)
  818. {
  819. return of_register_driver(&qec_sbus_driver, &of_bus_type);
  820. }
  821. static void __exit qec_exit(void)
  822. {
  823. of_unregister_driver(&qec_sbus_driver);
  824. while (root_qec_dev) {
  825. struct sunqec *next = root_qec_dev->next_module;
  826. struct of_device *op = root_qec_dev->op;
  827. free_irq(op->irqs[0], (void *) root_qec_dev);
  828. of_iounmap(&op->resource[0], root_qec_dev->gregs,
  829. GLOB_REG_SIZE);
  830. kfree(root_qec_dev);
  831. root_qec_dev = next;
  832. }
  833. }
  834. module_init(qec_init);
  835. module_exit(qec_exit);