sunqe.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 const struct net_device_ops qec_ops = {
  694. .ndo_open = qe_open,
  695. .ndo_stop = qe_close,
  696. .ndo_start_xmit = qe_start_xmit,
  697. .ndo_set_multicast_list = qe_set_multicast,
  698. .ndo_tx_timeout = qe_tx_timeout,
  699. .ndo_change_mtu = eth_change_mtu,
  700. .ndo_set_mac_address = eth_mac_addr,
  701. .ndo_validate_addr = eth_validate_addr,
  702. };
  703. static int __devinit qec_ether_init(struct of_device *op)
  704. {
  705. static unsigned version_printed;
  706. struct net_device *dev;
  707. struct sunqec *qecp;
  708. struct sunqe *qe;
  709. int i, res;
  710. if (version_printed++ == 0)
  711. printk(KERN_INFO "%s", version);
  712. dev = alloc_etherdev(sizeof(struct sunqe));
  713. if (!dev)
  714. return -ENOMEM;
  715. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  716. qe = netdev_priv(dev);
  717. res = -ENODEV;
  718. i = of_getintprop_default(op->node, "channel#", -1);
  719. if (i == -1)
  720. goto fail;
  721. qe->channel = i;
  722. spin_lock_init(&qe->lock);
  723. qecp = get_qec(op);
  724. if (!qecp)
  725. goto fail;
  726. qecp->qes[qe->channel] = qe;
  727. qe->dev = dev;
  728. qe->parent = qecp;
  729. qe->op = op;
  730. res = -ENOMEM;
  731. qe->qcregs = of_ioremap(&op->resource[0], 0,
  732. CREG_REG_SIZE, "QEC Channel Registers");
  733. if (!qe->qcregs) {
  734. printk(KERN_ERR "qe: Cannot map channel registers.\n");
  735. goto fail;
  736. }
  737. qe->mregs = of_ioremap(&op->resource[1], 0,
  738. MREGS_REG_SIZE, "QE MACE Registers");
  739. if (!qe->mregs) {
  740. printk(KERN_ERR "qe: Cannot map MACE registers.\n");
  741. goto fail;
  742. }
  743. qe->qe_block = dma_alloc_coherent(&op->dev, PAGE_SIZE,
  744. &qe->qblock_dvma, GFP_ATOMIC);
  745. qe->buffers = dma_alloc_coherent(&op->dev, sizeof(struct sunqe_buffers),
  746. &qe->buffers_dvma, GFP_ATOMIC);
  747. if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
  748. qe->buffers == NULL || qe->buffers_dvma == 0)
  749. goto fail;
  750. /* Stop this QE. */
  751. qe_stop(qe);
  752. SET_NETDEV_DEV(dev, &op->dev);
  753. dev->watchdog_timeo = 5*HZ;
  754. dev->irq = op->irqs[0];
  755. dev->dma = 0;
  756. dev->ethtool_ops = &qe_ethtool_ops;
  757. dev->netdev_ops = &qec_ops;
  758. res = register_netdev(dev);
  759. if (res)
  760. goto fail;
  761. dev_set_drvdata(&op->dev, qe);
  762. printk(KERN_INFO "%s: qe channel[%d] ", dev->name, qe->channel);
  763. for (i = 0; i < 6; i++)
  764. printk ("%2.2x%c",
  765. dev->dev_addr[i],
  766. i == 5 ? ' ': ':');
  767. printk("\n");
  768. return 0;
  769. fail:
  770. if (qe->qcregs)
  771. of_iounmap(&op->resource[0], qe->qcregs, CREG_REG_SIZE);
  772. if (qe->mregs)
  773. of_iounmap(&op->resource[1], qe->mregs, MREGS_REG_SIZE);
  774. if (qe->qe_block)
  775. dma_free_coherent(&op->dev, PAGE_SIZE,
  776. qe->qe_block, qe->qblock_dvma);
  777. if (qe->buffers)
  778. dma_free_coherent(&op->dev,
  779. sizeof(struct sunqe_buffers),
  780. qe->buffers,
  781. qe->buffers_dvma);
  782. free_netdev(dev);
  783. return res;
  784. }
  785. static int __devinit qec_sbus_probe(struct of_device *op, const struct of_device_id *match)
  786. {
  787. return qec_ether_init(op);
  788. }
  789. static int __devexit qec_sbus_remove(struct of_device *op)
  790. {
  791. struct sunqe *qp = dev_get_drvdata(&op->dev);
  792. struct net_device *net_dev = qp->dev;
  793. unregister_netdev(net_dev);
  794. of_iounmap(&op->resource[0], qp->qcregs, CREG_REG_SIZE);
  795. of_iounmap(&op->resource[1], qp->mregs, MREGS_REG_SIZE);
  796. dma_free_coherent(&op->dev, PAGE_SIZE,
  797. qp->qe_block, qp->qblock_dvma);
  798. dma_free_coherent(&op->dev, sizeof(struct sunqe_buffers),
  799. qp->buffers, qp->buffers_dvma);
  800. free_netdev(net_dev);
  801. dev_set_drvdata(&op->dev, NULL);
  802. return 0;
  803. }
  804. static const struct of_device_id qec_sbus_match[] = {
  805. {
  806. .name = "qe",
  807. },
  808. {},
  809. };
  810. MODULE_DEVICE_TABLE(of, qec_sbus_match);
  811. static struct of_platform_driver qec_sbus_driver = {
  812. .name = "qec",
  813. .match_table = qec_sbus_match,
  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);