sunqe.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. dev->trans_start = jiffies;
  509. sbus_writel(CREG_CTRL_TWAKEUP, qep->qcregs + CREG_CTRL);
  510. dev->stats.tx_packets++;
  511. dev->stats.tx_bytes += len;
  512. if (TX_BUFFS_AVAIL(qep) <= 0) {
  513. /* Halt the net queue and enable tx interrupts.
  514. * When the tx queue empties the tx irq handler
  515. * will wake up the queue and return us back to
  516. * the lazy tx reclaim scheme.
  517. */
  518. netif_stop_queue(dev);
  519. sbus_writel(0, qep->qcregs + CREG_TIMASK);
  520. }
  521. spin_unlock_irq(&qep->lock);
  522. dev_kfree_skb(skb);
  523. return 0;
  524. }
  525. static void qe_set_multicast(struct net_device *dev)
  526. {
  527. struct sunqe *qep = netdev_priv(dev);
  528. struct dev_mc_list *dmi = dev->mc_list;
  529. u8 new_mconfig = qep->mconfig;
  530. char *addrs;
  531. int i;
  532. u32 crc;
  533. /* Lock out others. */
  534. netif_stop_queue(dev);
  535. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  536. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  537. qep->mregs + MREGS_IACONFIG);
  538. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  539. barrier();
  540. for (i = 0; i < 8; i++)
  541. sbus_writeb(0xff, qep->mregs + MREGS_FILTER);
  542. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  543. } else if (dev->flags & IFF_PROMISC) {
  544. new_mconfig |= MREGS_MCONFIG_PROMISC;
  545. } else {
  546. u16 hash_table[4];
  547. u8 *hbytes = (unsigned char *) &hash_table[0];
  548. for (i = 0; i < 4; i++)
  549. hash_table[i] = 0;
  550. for (i = 0; i < dev->mc_count; i++) {
  551. addrs = dmi->dmi_addr;
  552. dmi = dmi->next;
  553. if (!(*addrs & 1))
  554. continue;
  555. crc = ether_crc_le(6, addrs);
  556. crc >>= 26;
  557. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  558. }
  559. /* Program the qe with the new filter value. */
  560. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  561. qep->mregs + MREGS_IACONFIG);
  562. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  563. barrier();
  564. for (i = 0; i < 8; i++) {
  565. u8 tmp = *hbytes++;
  566. sbus_writeb(tmp, qep->mregs + MREGS_FILTER);
  567. }
  568. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  569. }
  570. /* Any change of the logical address filter, the physical address,
  571. * or enabling/disabling promiscuous mode causes the MACE to disable
  572. * the receiver. So we must re-enable them here or else the MACE
  573. * refuses to listen to anything on the network. Sheesh, took
  574. * me a day or two to find this bug.
  575. */
  576. qep->mconfig = new_mconfig;
  577. sbus_writeb(qep->mconfig, qep->mregs + MREGS_MCONFIG);
  578. /* Let us get going again. */
  579. netif_wake_queue(dev);
  580. }
  581. /* Ethtool support... */
  582. static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  583. {
  584. const struct linux_prom_registers *regs;
  585. struct sunqe *qep = netdev_priv(dev);
  586. struct of_device *op;
  587. strcpy(info->driver, "sunqe");
  588. strcpy(info->version, "3.0");
  589. op = qep->op;
  590. regs = of_get_property(op->node, "reg", NULL);
  591. if (regs)
  592. sprintf(info->bus_info, "SBUS:%d", regs->which_io);
  593. }
  594. static u32 qe_get_link(struct net_device *dev)
  595. {
  596. struct sunqe *qep = netdev_priv(dev);
  597. void __iomem *mregs = qep->mregs;
  598. u8 phyconfig;
  599. spin_lock_irq(&qep->lock);
  600. phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
  601. spin_unlock_irq(&qep->lock);
  602. return (phyconfig & MREGS_PHYCONFIG_LSTAT);
  603. }
  604. static const struct ethtool_ops qe_ethtool_ops = {
  605. .get_drvinfo = qe_get_drvinfo,
  606. .get_link = qe_get_link,
  607. };
  608. /* This is only called once at boot time for each card probed. */
  609. static void qec_init_once(struct sunqec *qecp, struct of_device *op)
  610. {
  611. u8 bsizes = qecp->qec_bursts;
  612. if (sbus_can_burst64() && (bsizes & DMA_BURST64)) {
  613. sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
  614. } else if (bsizes & DMA_BURST32) {
  615. sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
  616. } else {
  617. sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
  618. }
  619. /* Packetsize only used in 100baseT BigMAC configurations,
  620. * set it to zero just to be on the safe side.
  621. */
  622. sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
  623. /* Set the local memsize register, divided up to one piece per QE channel. */
  624. sbus_writel((resource_size(&op->resource[1]) >> 2),
  625. qecp->gregs + GLOB_MSIZE);
  626. /* Divide up the local QEC memory amongst the 4 QE receiver and
  627. * transmitter FIFOs. Basically it is (total / 2 / num_channels).
  628. */
  629. sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
  630. qecp->gregs + GLOB_TSIZE);
  631. sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
  632. qecp->gregs + GLOB_RSIZE);
  633. }
  634. static u8 __devinit qec_get_burst(struct device_node *dp)
  635. {
  636. u8 bsizes, bsizes_more;
  637. /* Find and set the burst sizes for the QEC, since it
  638. * does the actual dma for all 4 channels.
  639. */
  640. bsizes = of_getintprop_default(dp, "burst-sizes", 0xff);
  641. bsizes &= 0xff;
  642. bsizes_more = of_getintprop_default(dp->parent, "burst-sizes", 0xff);
  643. if (bsizes_more != 0xff)
  644. bsizes &= bsizes_more;
  645. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  646. (bsizes & DMA_BURST32)==0)
  647. bsizes = (DMA_BURST32 - 1);
  648. return bsizes;
  649. }
  650. static struct sunqec * __devinit get_qec(struct of_device *child)
  651. {
  652. struct of_device *op = to_of_device(child->dev.parent);
  653. struct sunqec *qecp;
  654. qecp = dev_get_drvdata(&op->dev);
  655. if (!qecp) {
  656. qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
  657. if (qecp) {
  658. u32 ctrl;
  659. qecp->op = op;
  660. qecp->gregs = of_ioremap(&op->resource[0], 0,
  661. GLOB_REG_SIZE,
  662. "QEC Global Registers");
  663. if (!qecp->gregs)
  664. goto fail;
  665. /* Make sure the QEC is in MACE mode. */
  666. ctrl = sbus_readl(qecp->gregs + GLOB_CTRL);
  667. ctrl &= 0xf0000000;
  668. if (ctrl != GLOB_CTRL_MMODE) {
  669. printk(KERN_ERR "qec: Not in MACE mode!\n");
  670. goto fail;
  671. }
  672. if (qec_global_reset(qecp->gregs))
  673. goto fail;
  674. qecp->qec_bursts = qec_get_burst(op->node);
  675. qec_init_once(qecp, op);
  676. if (request_irq(op->irqs[0], &qec_interrupt,
  677. IRQF_SHARED, "qec", (void *) qecp)) {
  678. printk(KERN_ERR "qec: Can't register irq.\n");
  679. goto fail;
  680. }
  681. dev_set_drvdata(&op->dev, qecp);
  682. qecp->next_module = root_qec_dev;
  683. root_qec_dev = qecp;
  684. }
  685. }
  686. return qecp;
  687. fail:
  688. if (qecp->gregs)
  689. of_iounmap(&op->resource[0], qecp->gregs, GLOB_REG_SIZE);
  690. kfree(qecp);
  691. return NULL;
  692. }
  693. static int __devinit qec_ether_init(struct of_device *op)
  694. {
  695. static unsigned version_printed;
  696. struct net_device *dev;
  697. struct sunqec *qecp;
  698. struct sunqe *qe;
  699. int i, res;
  700. if (version_printed++ == 0)
  701. printk(KERN_INFO "%s", version);
  702. dev = alloc_etherdev(sizeof(struct sunqe));
  703. if (!dev)
  704. return -ENOMEM;
  705. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  706. qe = netdev_priv(dev);
  707. res = -ENODEV;
  708. i = of_getintprop_default(op->node, "channel#", -1);
  709. if (i == -1)
  710. goto fail;
  711. qe->channel = i;
  712. spin_lock_init(&qe->lock);
  713. qecp = get_qec(op);
  714. if (!qecp)
  715. goto fail;
  716. qecp->qes[qe->channel] = qe;
  717. qe->dev = dev;
  718. qe->parent = qecp;
  719. qe->op = op;
  720. res = -ENOMEM;
  721. qe->qcregs = of_ioremap(&op->resource[0], 0,
  722. CREG_REG_SIZE, "QEC Channel Registers");
  723. if (!qe->qcregs) {
  724. printk(KERN_ERR "qe: Cannot map channel registers.\n");
  725. goto fail;
  726. }
  727. qe->mregs = of_ioremap(&op->resource[1], 0,
  728. MREGS_REG_SIZE, "QE MACE Registers");
  729. if (!qe->mregs) {
  730. printk(KERN_ERR "qe: Cannot map MACE registers.\n");
  731. goto fail;
  732. }
  733. qe->qe_block = dma_alloc_coherent(&op->dev, PAGE_SIZE,
  734. &qe->qblock_dvma, GFP_ATOMIC);
  735. qe->buffers = dma_alloc_coherent(&op->dev, sizeof(struct sunqe_buffers),
  736. &qe->buffers_dvma, GFP_ATOMIC);
  737. if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
  738. qe->buffers == NULL || qe->buffers_dvma == 0)
  739. goto fail;
  740. /* Stop this QE. */
  741. qe_stop(qe);
  742. SET_NETDEV_DEV(dev, &op->dev);
  743. dev->open = qe_open;
  744. dev->stop = qe_close;
  745. dev->hard_start_xmit = qe_start_xmit;
  746. dev->set_multicast_list = qe_set_multicast;
  747. dev->tx_timeout = qe_tx_timeout;
  748. dev->watchdog_timeo = 5*HZ;
  749. dev->irq = op->irqs[0];
  750. dev->dma = 0;
  751. dev->ethtool_ops = &qe_ethtool_ops;
  752. res = register_netdev(dev);
  753. if (res)
  754. goto fail;
  755. dev_set_drvdata(&op->dev, qe);
  756. printk(KERN_INFO "%s: qe channel[%d] ", dev->name, qe->channel);
  757. for (i = 0; i < 6; i++)
  758. printk ("%2.2x%c",
  759. dev->dev_addr[i],
  760. i == 5 ? ' ': ':');
  761. printk("\n");
  762. return 0;
  763. fail:
  764. if (qe->qcregs)
  765. of_iounmap(&op->resource[0], qe->qcregs, CREG_REG_SIZE);
  766. if (qe->mregs)
  767. of_iounmap(&op->resource[1], qe->mregs, MREGS_REG_SIZE);
  768. if (qe->qe_block)
  769. dma_free_coherent(&op->dev, PAGE_SIZE,
  770. qe->qe_block, qe->qblock_dvma);
  771. if (qe->buffers)
  772. dma_free_coherent(&op->dev,
  773. sizeof(struct sunqe_buffers),
  774. qe->buffers,
  775. qe->buffers_dvma);
  776. free_netdev(dev);
  777. return res;
  778. }
  779. static int __devinit qec_sbus_probe(struct of_device *op, const struct of_device_id *match)
  780. {
  781. return qec_ether_init(op);
  782. }
  783. static int __devexit qec_sbus_remove(struct of_device *op)
  784. {
  785. struct sunqe *qp = dev_get_drvdata(&op->dev);
  786. struct net_device *net_dev = qp->dev;
  787. unregister_netdev(net_dev);
  788. of_iounmap(&op->resource[0], qp->qcregs, CREG_REG_SIZE);
  789. of_iounmap(&op->resource[1], qp->mregs, MREGS_REG_SIZE);
  790. dma_free_coherent(&op->dev, PAGE_SIZE,
  791. qp->qe_block, qp->qblock_dvma);
  792. dma_free_coherent(&op->dev, sizeof(struct sunqe_buffers),
  793. qp->buffers, qp->buffers_dvma);
  794. free_netdev(net_dev);
  795. dev_set_drvdata(&op->dev, NULL);
  796. return 0;
  797. }
  798. static const struct of_device_id qec_sbus_match[] = {
  799. {
  800. .name = "qe",
  801. },
  802. {},
  803. };
  804. MODULE_DEVICE_TABLE(of, qec_sbus_match);
  805. static struct of_platform_driver qec_sbus_driver = {
  806. .name = "qec",
  807. .match_table = qec_sbus_match,
  808. .probe = qec_sbus_probe,
  809. .remove = __devexit_p(qec_sbus_remove),
  810. };
  811. static int __init qec_init(void)
  812. {
  813. return of_register_driver(&qec_sbus_driver, &of_bus_type);
  814. }
  815. static void __exit qec_exit(void)
  816. {
  817. of_unregister_driver(&qec_sbus_driver);
  818. while (root_qec_dev) {
  819. struct sunqec *next = root_qec_dev->next_module;
  820. struct of_device *op = root_qec_dev->op;
  821. free_irq(op->irqs[0], (void *) root_qec_dev);
  822. of_iounmap(&op->resource[0], root_qec_dev->gregs,
  823. GLOB_REG_SIZE);
  824. kfree(root_qec_dev);
  825. root_qec_dev = next;
  826. }
  827. }
  828. module_init(qec_init);
  829. module_exit(qec_exit);