sunqe.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 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 <asm/system.h>
  28. #include <asm/io.h>
  29. #include <asm/dma.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/idprom.h>
  32. #include <asm/sbus.h>
  33. #include <asm/openprom.h>
  34. #include <asm/oplib.h>
  35. #include <asm/auxio.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/irq.h>
  38. #include "sunqe.h"
  39. #define DRV_NAME "sunqe"
  40. #define DRV_VERSION "4.0"
  41. #define DRV_RELDATE "June 23, 2006"
  42. #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
  43. static char version[] =
  44. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  45. MODULE_VERSION(DRV_VERSION);
  46. MODULE_AUTHOR(DRV_AUTHOR);
  47. MODULE_DESCRIPTION("Sun QuadEthernet 10baseT SBUS card driver");
  48. MODULE_LICENSE("GPL");
  49. static struct sunqec *root_qec_dev;
  50. static void qe_set_multicast(struct net_device *dev);
  51. #define QEC_RESET_TRIES 200
  52. static inline int qec_global_reset(void __iomem *gregs)
  53. {
  54. int tries = QEC_RESET_TRIES;
  55. sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
  56. while (--tries) {
  57. u32 tmp = sbus_readl(gregs + GLOB_CTRL);
  58. if (tmp & GLOB_CTRL_RESET) {
  59. udelay(20);
  60. continue;
  61. }
  62. break;
  63. }
  64. if (tries)
  65. return 0;
  66. printk(KERN_ERR "QuadEther: AIEEE cannot reset the QEC!\n");
  67. return -1;
  68. }
  69. #define MACE_RESET_RETRIES 200
  70. #define QE_RESET_RETRIES 200
  71. static inline int qe_stop(struct sunqe *qep)
  72. {
  73. void __iomem *cregs = qep->qcregs;
  74. void __iomem *mregs = qep->mregs;
  75. int tries;
  76. /* Reset the MACE, then the QEC channel. */
  77. sbus_writeb(MREGS_BCONFIG_RESET, mregs + MREGS_BCONFIG);
  78. tries = MACE_RESET_RETRIES;
  79. while (--tries) {
  80. u8 tmp = sbus_readb(mregs + MREGS_BCONFIG);
  81. if (tmp & MREGS_BCONFIG_RESET) {
  82. udelay(20);
  83. continue;
  84. }
  85. break;
  86. }
  87. if (!tries) {
  88. printk(KERN_ERR "QuadEther: AIEEE cannot reset the MACE!\n");
  89. return -1;
  90. }
  91. sbus_writel(CREG_CTRL_RESET, cregs + CREG_CTRL);
  92. tries = QE_RESET_RETRIES;
  93. while (--tries) {
  94. u32 tmp = sbus_readl(cregs + CREG_CTRL);
  95. if (tmp & CREG_CTRL_RESET) {
  96. udelay(20);
  97. continue;
  98. }
  99. break;
  100. }
  101. if (!tries) {
  102. printk(KERN_ERR "QuadEther: Cannot reset QE channel!\n");
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. static void qe_init_rings(struct sunqe *qep)
  108. {
  109. struct qe_init_block *qb = qep->qe_block;
  110. struct sunqe_buffers *qbufs = qep->buffers;
  111. __u32 qbufs_dvma = qep->buffers_dvma;
  112. int i;
  113. qep->rx_new = qep->rx_old = qep->tx_new = qep->tx_old = 0;
  114. memset(qb, 0, sizeof(struct qe_init_block));
  115. memset(qbufs, 0, sizeof(struct sunqe_buffers));
  116. for (i = 0; i < RX_RING_SIZE; i++) {
  117. qb->qe_rxd[i].rx_addr = qbufs_dvma + qebuf_offset(rx_buf, i);
  118. qb->qe_rxd[i].rx_flags =
  119. (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
  120. }
  121. }
  122. static int qe_init(struct sunqe *qep, int from_irq)
  123. {
  124. struct sunqec *qecp = qep->parent;
  125. void __iomem *cregs = qep->qcregs;
  126. void __iomem *mregs = qep->mregs;
  127. void __iomem *gregs = qecp->gregs;
  128. unsigned char *e = &qep->dev->dev_addr[0];
  129. u32 tmp;
  130. int i;
  131. /* Shut it up. */
  132. if (qe_stop(qep))
  133. return -EAGAIN;
  134. /* Setup initial rx/tx init block pointers. */
  135. sbus_writel(qep->qblock_dvma + qib_offset(qe_rxd, 0), cregs + CREG_RXDS);
  136. sbus_writel(qep->qblock_dvma + qib_offset(qe_txd, 0), cregs + CREG_TXDS);
  137. /* Enable/mask the various irq's. */
  138. sbus_writel(0, cregs + CREG_RIMASK);
  139. sbus_writel(1, cregs + CREG_TIMASK);
  140. sbus_writel(0, cregs + CREG_QMASK);
  141. sbus_writel(CREG_MMASK_RXCOLL, cregs + CREG_MMASK);
  142. /* Setup the FIFO pointers into QEC local memory. */
  143. tmp = qep->channel * sbus_readl(gregs + GLOB_MSIZE);
  144. sbus_writel(tmp, cregs + CREG_RXRBUFPTR);
  145. sbus_writel(tmp, cregs + CREG_RXWBUFPTR);
  146. tmp = sbus_readl(cregs + CREG_RXRBUFPTR) +
  147. sbus_readl(gregs + GLOB_RSIZE);
  148. sbus_writel(tmp, cregs + CREG_TXRBUFPTR);
  149. sbus_writel(tmp, cregs + CREG_TXWBUFPTR);
  150. /* Clear the channel collision counter. */
  151. sbus_writel(0, cregs + CREG_CCNT);
  152. /* For 10baseT, inter frame space nor throttle seems to be necessary. */
  153. sbus_writel(0, cregs + CREG_PIPG);
  154. /* Now dork with the AMD MACE. */
  155. sbus_writeb(MREGS_PHYCONFIG_AUTO, mregs + MREGS_PHYCONFIG);
  156. sbus_writeb(MREGS_TXFCNTL_AUTOPAD, mregs + MREGS_TXFCNTL);
  157. sbus_writeb(0, mregs + MREGS_RXFCNTL);
  158. /* The QEC dma's the rx'd packets from local memory out to main memory,
  159. * and therefore it interrupts when the packet reception is "complete".
  160. * So don't listen for the MACE talking about it.
  161. */
  162. sbus_writeb(MREGS_IMASK_COLL | MREGS_IMASK_RXIRQ, mregs + MREGS_IMASK);
  163. sbus_writeb(MREGS_BCONFIG_BSWAP | MREGS_BCONFIG_64TS, mregs + MREGS_BCONFIG);
  164. sbus_writeb((MREGS_FCONFIG_TXF16 | MREGS_FCONFIG_RXF32 |
  165. MREGS_FCONFIG_RFWU | MREGS_FCONFIG_TFWU),
  166. mregs + MREGS_FCONFIG);
  167. /* Only usable interface on QuadEther is twisted pair. */
  168. sbus_writeb(MREGS_PLSCONFIG_TP, mregs + MREGS_PLSCONFIG);
  169. /* Tell MACE we are changing the ether address. */
  170. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_PARESET,
  171. mregs + MREGS_IACONFIG);
  172. while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  173. barrier();
  174. sbus_writeb(e[0], mregs + MREGS_ETHADDR);
  175. sbus_writeb(e[1], mregs + MREGS_ETHADDR);
  176. sbus_writeb(e[2], mregs + MREGS_ETHADDR);
  177. sbus_writeb(e[3], mregs + MREGS_ETHADDR);
  178. sbus_writeb(e[4], mregs + MREGS_ETHADDR);
  179. sbus_writeb(e[5], mregs + MREGS_ETHADDR);
  180. /* Clear out the address filter. */
  181. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  182. mregs + MREGS_IACONFIG);
  183. while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  184. barrier();
  185. for (i = 0; i < 8; i++)
  186. sbus_writeb(0, mregs + MREGS_FILTER);
  187. /* Address changes are now complete. */
  188. sbus_writeb(0, mregs + MREGS_IACONFIG);
  189. qe_init_rings(qep);
  190. /* Wait a little bit for the link to come up... */
  191. mdelay(5);
  192. if (!(sbus_readb(mregs + MREGS_PHYCONFIG) & MREGS_PHYCONFIG_LTESTDIS)) {
  193. int tries = 50;
  194. while (tries--) {
  195. u8 tmp;
  196. mdelay(5);
  197. barrier();
  198. tmp = sbus_readb(mregs + MREGS_PHYCONFIG);
  199. if ((tmp & MREGS_PHYCONFIG_LSTAT) != 0)
  200. break;
  201. }
  202. if (tries == 0)
  203. printk(KERN_NOTICE "%s: Warning, link state is down.\n", qep->dev->name);
  204. }
  205. /* Missed packet counter is cleared on a read. */
  206. sbus_readb(mregs + MREGS_MPCNT);
  207. /* Reload multicast information, this will enable the receiver
  208. * and transmitter.
  209. */
  210. qe_set_multicast(qep->dev);
  211. /* QEC should now start to show interrupts. */
  212. return 0;
  213. }
  214. /* Grrr, certain error conditions completely lock up the AMD MACE,
  215. * so when we get these we _must_ reset the chip.
  216. */
  217. static int qe_is_bolixed(struct sunqe *qep, u32 qe_status)
  218. {
  219. struct net_device *dev = qep->dev;
  220. int mace_hwbug_workaround = 0;
  221. if (qe_status & CREG_STAT_EDEFER) {
  222. printk(KERN_ERR "%s: Excessive transmit defers.\n", dev->name);
  223. dev->stats.tx_errors++;
  224. }
  225. if (qe_status & CREG_STAT_CLOSS) {
  226. printk(KERN_ERR "%s: Carrier lost, link down?\n", dev->name);
  227. dev->stats.tx_errors++;
  228. dev->stats.tx_carrier_errors++;
  229. }
  230. if (qe_status & CREG_STAT_ERETRIES) {
  231. printk(KERN_ERR "%s: Excessive transmit retries (more than 16).\n", dev->name);
  232. dev->stats.tx_errors++;
  233. mace_hwbug_workaround = 1;
  234. }
  235. if (qe_status & CREG_STAT_LCOLL) {
  236. printk(KERN_ERR "%s: Late transmit collision.\n", dev->name);
  237. dev->stats.tx_errors++;
  238. dev->stats.collisions++;
  239. mace_hwbug_workaround = 1;
  240. }
  241. if (qe_status & CREG_STAT_FUFLOW) {
  242. printk(KERN_ERR "%s: Transmit fifo underflow, driver bug.\n", dev->name);
  243. dev->stats.tx_errors++;
  244. mace_hwbug_workaround = 1;
  245. }
  246. if (qe_status & CREG_STAT_JERROR) {
  247. printk(KERN_ERR "%s: Jabber error.\n", dev->name);
  248. }
  249. if (qe_status & CREG_STAT_BERROR) {
  250. printk(KERN_ERR "%s: Babble error.\n", dev->name);
  251. }
  252. if (qe_status & CREG_STAT_CCOFLOW) {
  253. dev->stats.tx_errors += 256;
  254. dev->stats.collisions += 256;
  255. }
  256. if (qe_status & CREG_STAT_TXDERROR) {
  257. printk(KERN_ERR "%s: Transmit descriptor is bogus, driver bug.\n", dev->name);
  258. dev->stats.tx_errors++;
  259. dev->stats.tx_aborted_errors++;
  260. mace_hwbug_workaround = 1;
  261. }
  262. if (qe_status & CREG_STAT_TXLERR) {
  263. printk(KERN_ERR "%s: Transmit late error.\n", dev->name);
  264. dev->stats.tx_errors++;
  265. mace_hwbug_workaround = 1;
  266. }
  267. if (qe_status & CREG_STAT_TXPERR) {
  268. printk(KERN_ERR "%s: Transmit DMA parity error.\n", dev->name);
  269. dev->stats.tx_errors++;
  270. dev->stats.tx_aborted_errors++;
  271. mace_hwbug_workaround = 1;
  272. }
  273. if (qe_status & CREG_STAT_TXSERR) {
  274. printk(KERN_ERR "%s: Transmit DMA sbus error ack.\n", dev->name);
  275. dev->stats.tx_errors++;
  276. dev->stats.tx_aborted_errors++;
  277. mace_hwbug_workaround = 1;
  278. }
  279. if (qe_status & CREG_STAT_RCCOFLOW) {
  280. dev->stats.rx_errors += 256;
  281. dev->stats.collisions += 256;
  282. }
  283. if (qe_status & CREG_STAT_RUOFLOW) {
  284. dev->stats.rx_errors += 256;
  285. dev->stats.rx_over_errors += 256;
  286. }
  287. if (qe_status & CREG_STAT_MCOFLOW) {
  288. dev->stats.rx_errors += 256;
  289. dev->stats.rx_missed_errors += 256;
  290. }
  291. if (qe_status & CREG_STAT_RXFOFLOW) {
  292. printk(KERN_ERR "%s: Receive fifo overflow.\n", dev->name);
  293. dev->stats.rx_errors++;
  294. dev->stats.rx_over_errors++;
  295. }
  296. if (qe_status & CREG_STAT_RLCOLL) {
  297. printk(KERN_ERR "%s: Late receive collision.\n", dev->name);
  298. dev->stats.rx_errors++;
  299. dev->stats.collisions++;
  300. }
  301. if (qe_status & CREG_STAT_FCOFLOW) {
  302. dev->stats.rx_errors += 256;
  303. dev->stats.rx_frame_errors += 256;
  304. }
  305. if (qe_status & CREG_STAT_CECOFLOW) {
  306. dev->stats.rx_errors += 256;
  307. dev->stats.rx_crc_errors += 256;
  308. }
  309. if (qe_status & CREG_STAT_RXDROP) {
  310. printk(KERN_ERR "%s: Receive packet dropped.\n", dev->name);
  311. dev->stats.rx_errors++;
  312. dev->stats.rx_dropped++;
  313. dev->stats.rx_missed_errors++;
  314. }
  315. if (qe_status & CREG_STAT_RXSMALL) {
  316. printk(KERN_ERR "%s: Receive buffer too small, driver bug.\n", dev->name);
  317. dev->stats.rx_errors++;
  318. dev->stats.rx_length_errors++;
  319. }
  320. if (qe_status & CREG_STAT_RXLERR) {
  321. printk(KERN_ERR "%s: Receive late error.\n", dev->name);
  322. dev->stats.rx_errors++;
  323. mace_hwbug_workaround = 1;
  324. }
  325. if (qe_status & CREG_STAT_RXPERR) {
  326. printk(KERN_ERR "%s: Receive DMA parity error.\n", dev->name);
  327. dev->stats.rx_errors++;
  328. dev->stats.rx_missed_errors++;
  329. mace_hwbug_workaround = 1;
  330. }
  331. if (qe_status & CREG_STAT_RXSERR) {
  332. printk(KERN_ERR "%s: Receive DMA sbus error ack.\n", dev->name);
  333. dev->stats.rx_errors++;
  334. dev->stats.rx_missed_errors++;
  335. mace_hwbug_workaround = 1;
  336. }
  337. if (mace_hwbug_workaround)
  338. qe_init(qep, 1);
  339. return mace_hwbug_workaround;
  340. }
  341. /* Per-QE receive interrupt service routine. Just like on the happy meal
  342. * we receive directly into skb's with a small packet copy water mark.
  343. */
  344. static void qe_rx(struct sunqe *qep)
  345. {
  346. struct qe_rxd *rxbase = &qep->qe_block->qe_rxd[0];
  347. struct net_device *dev = qep->dev;
  348. struct qe_rxd *this;
  349. struct sunqe_buffers *qbufs = qep->buffers;
  350. __u32 qbufs_dvma = qep->buffers_dvma;
  351. int elem = qep->rx_new, drops = 0;
  352. u32 flags;
  353. this = &rxbase[elem];
  354. while (!((flags = this->rx_flags) & RXD_OWN)) {
  355. struct sk_buff *skb;
  356. unsigned char *this_qbuf =
  357. &qbufs->rx_buf[elem & (RX_RING_SIZE - 1)][0];
  358. __u32 this_qbuf_dvma = qbufs_dvma +
  359. qebuf_offset(rx_buf, (elem & (RX_RING_SIZE - 1)));
  360. struct qe_rxd *end_rxd =
  361. &rxbase[(elem+RX_RING_SIZE)&(RX_RING_MAXSIZE-1)];
  362. int len = (flags & RXD_LENGTH) - 4; /* QE adds ether FCS size to len */
  363. /* Check for errors. */
  364. if (len < ETH_ZLEN) {
  365. dev->stats.rx_errors++;
  366. dev->stats.rx_length_errors++;
  367. dev->stats.rx_dropped++;
  368. } else {
  369. skb = dev_alloc_skb(len + 2);
  370. if (skb == NULL) {
  371. drops++;
  372. dev->stats.rx_dropped++;
  373. } else {
  374. skb_reserve(skb, 2);
  375. skb_put(skb, len);
  376. skb_copy_to_linear_data(skb, (unsigned char *) this_qbuf,
  377. len);
  378. skb->protocol = eth_type_trans(skb, qep->dev);
  379. netif_rx(skb);
  380. qep->dev->last_rx = jiffies;
  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 = (struct sunqe *) dev->priv;
  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 = (struct sunqe *) dev->priv;
  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 = (struct sunqe *) dev->priv;
  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 = (struct sunqe *) dev->priv;
  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 = (struct sunqe *) dev->priv;
  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. struct sunqe *qep = dev->priv;
  585. strcpy(info->driver, "sunqe");
  586. strcpy(info->version, "3.0");
  587. sprintf(info->bus_info, "SBUS:%d",
  588. qep->qe_sdev->slot);
  589. }
  590. static u32 qe_get_link(struct net_device *dev)
  591. {
  592. struct sunqe *qep = dev->priv;
  593. void __iomem *mregs = qep->mregs;
  594. u8 phyconfig;
  595. spin_lock_irq(&qep->lock);
  596. phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
  597. spin_unlock_irq(&qep->lock);
  598. return (phyconfig & MREGS_PHYCONFIG_LSTAT);
  599. }
  600. static const struct ethtool_ops qe_ethtool_ops = {
  601. .get_drvinfo = qe_get_drvinfo,
  602. .get_link = qe_get_link,
  603. };
  604. /* This is only called once at boot time for each card probed. */
  605. static inline void qec_init_once(struct sunqec *qecp, struct sbus_dev *qsdev)
  606. {
  607. u8 bsizes = qecp->qec_bursts;
  608. if (sbus_can_burst64(qsdev) && (bsizes & DMA_BURST64)) {
  609. sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
  610. } else if (bsizes & DMA_BURST32) {
  611. sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
  612. } else {
  613. sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
  614. }
  615. /* Packetsize only used in 100baseT BigMAC configurations,
  616. * set it to zero just to be on the safe side.
  617. */
  618. sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
  619. /* Set the local memsize register, divided up to one piece per QE channel. */
  620. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2),
  621. qecp->gregs + GLOB_MSIZE);
  622. /* Divide up the local QEC memory amongst the 4 QE receiver and
  623. * transmitter FIFOs. Basically it is (total / 2 / num_channels).
  624. */
  625. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
  626. qecp->gregs + GLOB_TSIZE);
  627. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
  628. qecp->gregs + GLOB_RSIZE);
  629. }
  630. static u8 __devinit qec_get_burst(struct device_node *dp)
  631. {
  632. u8 bsizes, bsizes_more;
  633. /* Find and set the burst sizes for the QEC, since it
  634. * does the actual dma for all 4 channels.
  635. */
  636. bsizes = of_getintprop_default(dp, "burst-sizes", 0xff);
  637. bsizes &= 0xff;
  638. bsizes_more = of_getintprop_default(dp->parent, "burst-sizes", 0xff);
  639. if (bsizes_more != 0xff)
  640. bsizes &= bsizes_more;
  641. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  642. (bsizes & DMA_BURST32)==0)
  643. bsizes = (DMA_BURST32 - 1);
  644. return bsizes;
  645. }
  646. static struct sunqec * __devinit get_qec(struct sbus_dev *child_sdev)
  647. {
  648. struct sbus_dev *qec_sdev = child_sdev->parent;
  649. struct sunqec *qecp;
  650. for (qecp = root_qec_dev; qecp; qecp = qecp->next_module) {
  651. if (qecp->qec_sdev == qec_sdev)
  652. break;
  653. }
  654. if (!qecp) {
  655. qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
  656. if (qecp) {
  657. u32 ctrl;
  658. qecp->qec_sdev = qec_sdev;
  659. qecp->gregs = sbus_ioremap(&qec_sdev->resource[0], 0,
  660. GLOB_REG_SIZE,
  661. "QEC Global Registers");
  662. if (!qecp->gregs)
  663. goto fail;
  664. /* Make sure the QEC is in MACE mode. */
  665. ctrl = sbus_readl(qecp->gregs + GLOB_CTRL);
  666. ctrl &= 0xf0000000;
  667. if (ctrl != GLOB_CTRL_MMODE) {
  668. printk(KERN_ERR "qec: Not in MACE mode!\n");
  669. goto fail;
  670. }
  671. if (qec_global_reset(qecp->gregs))
  672. goto fail;
  673. qecp->qec_bursts = qec_get_burst(qec_sdev->ofdev.node);
  674. qec_init_once(qecp, qec_sdev);
  675. if (request_irq(qec_sdev->irqs[0], &qec_interrupt,
  676. IRQF_SHARED, "qec", (void *) qecp)) {
  677. printk(KERN_ERR "qec: Can't register irq.\n");
  678. goto fail;
  679. }
  680. qecp->next_module = root_qec_dev;
  681. root_qec_dev = qecp;
  682. }
  683. }
  684. return qecp;
  685. fail:
  686. if (qecp->gregs)
  687. sbus_iounmap(qecp->gregs, GLOB_REG_SIZE);
  688. kfree(qecp);
  689. return NULL;
  690. }
  691. static int __devinit qec_ether_init(struct sbus_dev *sdev)
  692. {
  693. static unsigned version_printed;
  694. struct net_device *dev;
  695. struct sunqe *qe;
  696. struct sunqec *qecp;
  697. int i, res;
  698. if (version_printed++ == 0)
  699. printk(KERN_INFO "%s", version);
  700. dev = alloc_etherdev(sizeof(struct sunqe));
  701. if (!dev)
  702. return -ENOMEM;
  703. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  704. qe = netdev_priv(dev);
  705. i = of_getintprop_default(sdev->ofdev.node, "channel#", -1);
  706. if (i == -1) {
  707. struct sbus_dev *td = sdev->parent->child;
  708. i = 0;
  709. while (td != sdev) {
  710. td = td->next;
  711. i++;
  712. }
  713. }
  714. qe->channel = i;
  715. spin_lock_init(&qe->lock);
  716. res = -ENODEV;
  717. qecp = get_qec(sdev);
  718. if (!qecp)
  719. goto fail;
  720. qecp->qes[qe->channel] = qe;
  721. qe->dev = dev;
  722. qe->parent = qecp;
  723. qe->qe_sdev = sdev;
  724. res = -ENOMEM;
  725. qe->qcregs = sbus_ioremap(&qe->qe_sdev->resource[0], 0,
  726. CREG_REG_SIZE, "QEC Channel Registers");
  727. if (!qe->qcregs) {
  728. printk(KERN_ERR "qe: Cannot map channel registers.\n");
  729. goto fail;
  730. }
  731. qe->mregs = sbus_ioremap(&qe->qe_sdev->resource[1], 0,
  732. MREGS_REG_SIZE, "QE MACE Registers");
  733. if (!qe->mregs) {
  734. printk(KERN_ERR "qe: Cannot map MACE registers.\n");
  735. goto fail;
  736. }
  737. qe->qe_block = dma_alloc_coherent(&qe->qe_sdev->ofdev.dev,
  738. PAGE_SIZE,
  739. &qe->qblock_dvma, GFP_ATOMIC);
  740. qe->buffers = dma_alloc_coherent(&qe->qe_sdev->ofdev.dev,
  741. sizeof(struct sunqe_buffers),
  742. &qe->buffers_dvma, GFP_ATOMIC);
  743. if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
  744. qe->buffers == NULL || qe->buffers_dvma == 0)
  745. goto fail;
  746. /* Stop this QE. */
  747. qe_stop(qe);
  748. SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
  749. dev->open = qe_open;
  750. dev->stop = qe_close;
  751. dev->hard_start_xmit = qe_start_xmit;
  752. dev->set_multicast_list = qe_set_multicast;
  753. dev->tx_timeout = qe_tx_timeout;
  754. dev->watchdog_timeo = 5*HZ;
  755. dev->irq = sdev->irqs[0];
  756. dev->dma = 0;
  757. dev->ethtool_ops = &qe_ethtool_ops;
  758. res = register_netdev(dev);
  759. if (res)
  760. goto fail;
  761. dev_set_drvdata(&sdev->ofdev.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. sbus_iounmap(qe->qcregs, CREG_REG_SIZE);
  772. if (qe->mregs)
  773. sbus_iounmap(qe->mregs, MREGS_REG_SIZE);
  774. if (qe->qe_block)
  775. dma_free_coherent(&qe->qe_sdev->ofdev.dev,
  776. PAGE_SIZE,
  777. qe->qe_block,
  778. qe->qblock_dvma);
  779. if (qe->buffers)
  780. dma_free_coherent(&qe->qe_sdev->ofdev.dev,
  781. sizeof(struct sunqe_buffers),
  782. qe->buffers,
  783. qe->buffers_dvma);
  784. free_netdev(dev);
  785. return res;
  786. }
  787. static int __devinit qec_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  788. {
  789. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  790. return qec_ether_init(sdev);
  791. }
  792. static int __devexit qec_sbus_remove(struct of_device *dev)
  793. {
  794. struct sunqe *qp = dev_get_drvdata(&dev->dev);
  795. struct net_device *net_dev = qp->dev;
  796. unregister_netdev(net_dev);
  797. sbus_iounmap(qp->qcregs, CREG_REG_SIZE);
  798. sbus_iounmap(qp->mregs, MREGS_REG_SIZE);
  799. dma_free_coherent(&qp->qe_sdev->ofdev.dev,
  800. PAGE_SIZE,
  801. qp->qe_block,
  802. qp->qblock_dvma);
  803. dma_free_coherent(&qp->qe_sdev->ofdev.dev,
  804. sizeof(struct sunqe_buffers),
  805. qp->buffers,
  806. qp->buffers_dvma);
  807. free_netdev(net_dev);
  808. dev_set_drvdata(&dev->dev, NULL);
  809. return 0;
  810. }
  811. static struct of_device_id qec_sbus_match[] = {
  812. {
  813. .name = "qe",
  814. },
  815. {},
  816. };
  817. MODULE_DEVICE_TABLE(of, qec_sbus_match);
  818. static struct of_platform_driver qec_sbus_driver = {
  819. .name = "qec",
  820. .match_table = qec_sbus_match,
  821. .probe = qec_sbus_probe,
  822. .remove = __devexit_p(qec_sbus_remove),
  823. };
  824. static int __init qec_init(void)
  825. {
  826. return of_register_driver(&qec_sbus_driver, &sbus_bus_type);
  827. }
  828. static void __exit qec_exit(void)
  829. {
  830. of_unregister_driver(&qec_sbus_driver);
  831. while (root_qec_dev) {
  832. struct sunqec *next = root_qec_dev->next_module;
  833. free_irq(root_qec_dev->qec_sdev->irqs[0],
  834. (void *) root_qec_dev);
  835. sbus_iounmap(root_qec_dev->gregs, GLOB_REG_SIZE);
  836. kfree(root_qec_dev);
  837. root_qec_dev = next;
  838. }
  839. }
  840. module_init(qec_init);
  841. module_exit(qec_exit);