sunqe.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /* $Id: sunqe.c,v 1.55 2002/01/15 06:48:55 davem Exp $
  2. * sunqe.c: Sparc QuadEthernet 10baseT SBUS card driver.
  3. * Once again I am out to prove that every ethernet
  4. * controller out there can be most efficiently programmed
  5. * if you make it look like a LANCE.
  6. *
  7. * Copyright (C) 1996, 1999, 2003 David S. Miller (davem@redhat.com)
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/in.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/crc32.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/bitops.h>
  27. #include <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 "3.0"
  41. #define DRV_RELDATE "8/24/03"
  42. #define DRV_AUTHOR "David S. Miller (davem@redhat.com)"
  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. qep->net_stats.tx_errors++;
  224. }
  225. if (qe_status & CREG_STAT_CLOSS) {
  226. printk(KERN_ERR "%s: Carrier lost, link down?\n", dev->name);
  227. qep->net_stats.tx_errors++;
  228. qep->net_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. qep->net_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. qep->net_stats.tx_errors++;
  238. qep->net_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. qep->net_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. qep->net_stats.tx_errors += 256;
  254. qep->net_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. qep->net_stats.tx_errors++;
  259. qep->net_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. qep->net_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. qep->net_stats.tx_errors++;
  270. qep->net_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. qep->net_stats.tx_errors++;
  276. qep->net_stats.tx_aborted_errors++;
  277. mace_hwbug_workaround = 1;
  278. }
  279. if (qe_status & CREG_STAT_RCCOFLOW) {
  280. qep->net_stats.rx_errors += 256;
  281. qep->net_stats.collisions += 256;
  282. }
  283. if (qe_status & CREG_STAT_RUOFLOW) {
  284. qep->net_stats.rx_errors += 256;
  285. qep->net_stats.rx_over_errors += 256;
  286. }
  287. if (qe_status & CREG_STAT_MCOFLOW) {
  288. qep->net_stats.rx_errors += 256;
  289. qep->net_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. qep->net_stats.rx_errors++;
  294. qep->net_stats.rx_over_errors++;
  295. }
  296. if (qe_status & CREG_STAT_RLCOLL) {
  297. printk(KERN_ERR "%s: Late receive collision.\n", dev->name);
  298. qep->net_stats.rx_errors++;
  299. qep->net_stats.collisions++;
  300. }
  301. if (qe_status & CREG_STAT_FCOFLOW) {
  302. qep->net_stats.rx_errors += 256;
  303. qep->net_stats.rx_frame_errors += 256;
  304. }
  305. if (qe_status & CREG_STAT_CECOFLOW) {
  306. qep->net_stats.rx_errors += 256;
  307. qep->net_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. qep->net_stats.rx_errors++;
  312. qep->net_stats.rx_dropped++;
  313. qep->net_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. qep->net_stats.rx_errors++;
  318. qep->net_stats.rx_length_errors++;
  319. }
  320. if (qe_status & CREG_STAT_RXLERR) {
  321. printk(KERN_ERR "%s: Receive late error.\n", dev->name);
  322. qep->net_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. qep->net_stats.rx_errors++;
  328. qep->net_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. qep->net_stats.rx_errors++;
  334. qep->net_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 qe_rxd *this;
  348. struct sunqe_buffers *qbufs = qep->buffers;
  349. __u32 qbufs_dvma = qep->buffers_dvma;
  350. int elem = qep->rx_new, drops = 0;
  351. u32 flags;
  352. this = &rxbase[elem];
  353. while (!((flags = this->rx_flags) & RXD_OWN)) {
  354. struct sk_buff *skb;
  355. unsigned char *this_qbuf =
  356. &qbufs->rx_buf[elem & (RX_RING_SIZE - 1)][0];
  357. __u32 this_qbuf_dvma = qbufs_dvma +
  358. qebuf_offset(rx_buf, (elem & (RX_RING_SIZE - 1)));
  359. struct qe_rxd *end_rxd =
  360. &rxbase[(elem+RX_RING_SIZE)&(RX_RING_MAXSIZE-1)];
  361. int len = (flags & RXD_LENGTH) - 4; /* QE adds ether FCS size to len */
  362. /* Check for errors. */
  363. if (len < ETH_ZLEN) {
  364. qep->net_stats.rx_errors++;
  365. qep->net_stats.rx_length_errors++;
  366. qep->net_stats.rx_dropped++;
  367. } else {
  368. skb = dev_alloc_skb(len + 2);
  369. if (skb == NULL) {
  370. drops++;
  371. qep->net_stats.rx_dropped++;
  372. } else {
  373. skb->dev = qep->dev;
  374. skb_reserve(skb, 2);
  375. skb_put(skb, len);
  376. eth_copy_and_sum(skb, (unsigned char *) this_qbuf,
  377. len, 0);
  378. skb->protocol = eth_type_trans(skb, qep->dev);
  379. netif_rx(skb);
  380. qep->dev->last_rx = jiffies;
  381. qep->net_stats.rx_packets++;
  382. qep->net_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, struct pt_regs *regs)
  400. {
  401. struct sunqec *qecp = (struct sunqec *) 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. memcpy(txbuf, skb->data, 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. qep->net_stats.tx_packets++;
  511. qep->net_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 struct net_device_stats *qe_get_stats(struct net_device *dev)
  526. {
  527. struct sunqe *qep = (struct sunqe *) dev->priv;
  528. return &qep->net_stats;
  529. }
  530. static void qe_set_multicast(struct net_device *dev)
  531. {
  532. struct sunqe *qep = (struct sunqe *) dev->priv;
  533. struct dev_mc_list *dmi = dev->mc_list;
  534. u8 new_mconfig = qep->mconfig;
  535. char *addrs;
  536. int i;
  537. u32 crc;
  538. /* Lock out others. */
  539. netif_stop_queue(dev);
  540. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  541. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  542. qep->mregs + MREGS_IACONFIG);
  543. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  544. barrier();
  545. for (i = 0; i < 8; i++)
  546. sbus_writeb(0xff, qep->mregs + MREGS_FILTER);
  547. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  548. } else if (dev->flags & IFF_PROMISC) {
  549. new_mconfig |= MREGS_MCONFIG_PROMISC;
  550. } else {
  551. u16 hash_table[4];
  552. u8 *hbytes = (unsigned char *) &hash_table[0];
  553. for (i = 0; i < 4; i++)
  554. hash_table[i] = 0;
  555. for (i = 0; i < dev->mc_count; i++) {
  556. addrs = dmi->dmi_addr;
  557. dmi = dmi->next;
  558. if (!(*addrs & 1))
  559. continue;
  560. crc = ether_crc_le(6, addrs);
  561. crc >>= 26;
  562. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  563. }
  564. /* Program the qe with the new filter value. */
  565. sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
  566. qep->mregs + MREGS_IACONFIG);
  567. while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
  568. barrier();
  569. for (i = 0; i < 8; i++) {
  570. u8 tmp = *hbytes++;
  571. sbus_writeb(tmp, qep->mregs + MREGS_FILTER);
  572. }
  573. sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
  574. }
  575. /* Any change of the logical address filter, the physical address,
  576. * or enabling/disabling promiscuous mode causes the MACE to disable
  577. * the receiver. So we must re-enable them here or else the MACE
  578. * refuses to listen to anything on the network. Sheesh, took
  579. * me a day or two to find this bug.
  580. */
  581. qep->mconfig = new_mconfig;
  582. sbus_writeb(qep->mconfig, qep->mregs + MREGS_MCONFIG);
  583. /* Let us get going again. */
  584. netif_wake_queue(dev);
  585. }
  586. /* Ethtool support... */
  587. static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  588. {
  589. struct sunqe *qep = dev->priv;
  590. strcpy(info->driver, "sunqe");
  591. strcpy(info->version, "3.0");
  592. sprintf(info->bus_info, "SBUS:%d",
  593. qep->qe_sdev->slot);
  594. }
  595. static u32 qe_get_link(struct net_device *dev)
  596. {
  597. struct sunqe *qep = dev->priv;
  598. void __iomem *mregs = qep->mregs;
  599. u8 phyconfig;
  600. spin_lock_irq(&qep->lock);
  601. phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
  602. spin_unlock_irq(&qep->lock);
  603. return (phyconfig & MREGS_PHYCONFIG_LSTAT);
  604. }
  605. static struct ethtool_ops qe_ethtool_ops = {
  606. .get_drvinfo = qe_get_drvinfo,
  607. .get_link = qe_get_link,
  608. };
  609. /* This is only called once at boot time for each card probed. */
  610. static inline void qec_init_once(struct sunqec *qecp, struct sbus_dev *qsdev)
  611. {
  612. u8 bsizes = qecp->qec_bursts;
  613. if (sbus_can_burst64(qsdev) && (bsizes & DMA_BURST64)) {
  614. sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
  615. } else if (bsizes & DMA_BURST32) {
  616. sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
  617. } else {
  618. sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
  619. }
  620. /* Packetsize only used in 100baseT BigMAC configurations,
  621. * set it to zero just to be on the safe side.
  622. */
  623. sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
  624. /* Set the local memsize register, divided up to one piece per QE channel. */
  625. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2),
  626. qecp->gregs + GLOB_MSIZE);
  627. /* Divide up the local QEC memory amongst the 4 QE receiver and
  628. * transmitter FIFOs. Basically it is (total / 2 / num_channels).
  629. */
  630. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
  631. qecp->gregs + GLOB_TSIZE);
  632. sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
  633. qecp->gregs + GLOB_RSIZE);
  634. }
  635. /* Four QE's per QEC card. */
  636. static int __init qec_ether_init(struct net_device *dev, struct sbus_dev *sdev)
  637. {
  638. static unsigned version_printed;
  639. struct net_device *qe_devs[4];
  640. struct sunqe *qeps[4];
  641. struct sbus_dev *qesdevs[4];
  642. struct sbus_dev *child;
  643. struct sunqec *qecp = NULL;
  644. u8 bsizes, bsizes_more;
  645. int i, j, res = -ENOMEM;
  646. for (i = 0; i < 4; i++) {
  647. qe_devs[i] = alloc_etherdev(sizeof(struct sunqe));
  648. if (!qe_devs[i])
  649. goto out;
  650. }
  651. if (version_printed++ == 0)
  652. printk(KERN_INFO "%s", version);
  653. for (i = 0; i < 4; i++) {
  654. qeps[i] = (struct sunqe *) qe_devs[i]->priv;
  655. for (j = 0; j < 6; j++)
  656. qe_devs[i]->dev_addr[j] = idprom->id_ethaddr[j];
  657. qeps[i]->channel = i;
  658. spin_lock_init(&qeps[i]->lock);
  659. }
  660. qecp = kmalloc(sizeof(struct sunqec), GFP_KERNEL);
  661. if (qecp == NULL)
  662. goto out1;
  663. qecp->qec_sdev = sdev;
  664. for (i = 0; i < 4; i++) {
  665. qecp->qes[i] = qeps[i];
  666. qeps[i]->dev = qe_devs[i];
  667. qeps[i]->parent = qecp;
  668. }
  669. res = -ENODEV;
  670. for (i = 0, child = sdev->child; i < 4; i++, child = child->next) {
  671. /* Link in channel */
  672. j = prom_getintdefault(child->prom_node, "channel#", -1);
  673. if (j == -1)
  674. goto out2;
  675. qesdevs[j] = child;
  676. }
  677. for (i = 0; i < 4; i++)
  678. qeps[i]->qe_sdev = qesdevs[i];
  679. /* Now map in the registers, QEC globals first. */
  680. qecp->gregs = sbus_ioremap(&sdev->resource[0], 0,
  681. GLOB_REG_SIZE, "QEC Global Registers");
  682. if (!qecp->gregs) {
  683. printk(KERN_ERR "QuadEther: Cannot map QEC global registers.\n");
  684. goto out2;
  685. }
  686. /* Make sure the QEC is in MACE mode. */
  687. if ((sbus_readl(qecp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_MMODE) {
  688. printk(KERN_ERR "QuadEther: AIEEE, QEC is not in MACE mode!\n");
  689. goto out3;
  690. }
  691. /* Reset the QEC. */
  692. if (qec_global_reset(qecp->gregs))
  693. goto out3;
  694. /* Find and set the burst sizes for the QEC, since it does
  695. * the actual dma for all 4 channels.
  696. */
  697. bsizes = prom_getintdefault(sdev->prom_node, "burst-sizes", 0xff);
  698. bsizes &= 0xff;
  699. bsizes_more = prom_getintdefault(sdev->bus->prom_node, "burst-sizes", 0xff);
  700. if (bsizes_more != 0xff)
  701. bsizes &= bsizes_more;
  702. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  703. (bsizes & DMA_BURST32)==0)
  704. bsizes = (DMA_BURST32 - 1);
  705. qecp->qec_bursts = bsizes;
  706. /* Perform one time QEC initialization, we never touch the QEC
  707. * globals again after this.
  708. */
  709. qec_init_once(qecp, sdev);
  710. for (i = 0; i < 4; i++) {
  711. struct sunqe *qe = qeps[i];
  712. /* Map in QEC per-channel control registers. */
  713. qe->qcregs = sbus_ioremap(&qe->qe_sdev->resource[0], 0,
  714. CREG_REG_SIZE, "QEC Channel Registers");
  715. if (!qe->qcregs) {
  716. printk(KERN_ERR "QuadEther: Cannot map QE %d's channel registers.\n", i);
  717. goto out4;
  718. }
  719. /* Map in per-channel AMD MACE registers. */
  720. qe->mregs = sbus_ioremap(&qe->qe_sdev->resource[1], 0,
  721. MREGS_REG_SIZE, "QE MACE Registers");
  722. if (!qe->mregs) {
  723. printk(KERN_ERR "QuadEther: Cannot map QE %d's MACE registers.\n", i);
  724. goto out4;
  725. }
  726. qe->qe_block = sbus_alloc_consistent(qe->qe_sdev,
  727. PAGE_SIZE,
  728. &qe->qblock_dvma);
  729. qe->buffers = sbus_alloc_consistent(qe->qe_sdev,
  730. sizeof(struct sunqe_buffers),
  731. &qe->buffers_dvma);
  732. if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
  733. qe->buffers == NULL || qe->buffers_dvma == 0) {
  734. goto out4;
  735. }
  736. /* Stop this QE. */
  737. qe_stop(qe);
  738. }
  739. for (i = 0; i < 4; i++) {
  740. SET_MODULE_OWNER(qe_devs[i]);
  741. qe_devs[i]->open = qe_open;
  742. qe_devs[i]->stop = qe_close;
  743. qe_devs[i]->hard_start_xmit = qe_start_xmit;
  744. qe_devs[i]->get_stats = qe_get_stats;
  745. qe_devs[i]->set_multicast_list = qe_set_multicast;
  746. qe_devs[i]->tx_timeout = qe_tx_timeout;
  747. qe_devs[i]->watchdog_timeo = 5*HZ;
  748. qe_devs[i]->irq = sdev->irqs[0];
  749. qe_devs[i]->dma = 0;
  750. qe_devs[i]->ethtool_ops = &qe_ethtool_ops;
  751. }
  752. /* QEC receives interrupts from each QE, then it sends the actual
  753. * IRQ to the cpu itself. Since QEC is the single point of
  754. * interrupt for all QE channels we register the IRQ handler
  755. * for it now.
  756. */
  757. if (request_irq(sdev->irqs[0], &qec_interrupt,
  758. SA_SHIRQ, "QuadEther", (void *) qecp)) {
  759. printk(KERN_ERR "QuadEther: Can't register QEC master irq handler.\n");
  760. res = -EAGAIN;
  761. goto out4;
  762. }
  763. for (i = 0; i < 4; i++) {
  764. if (register_netdev(qe_devs[i]) != 0)
  765. goto out5;
  766. }
  767. /* Report the QE channels. */
  768. for (i = 0; i < 4; i++) {
  769. printk(KERN_INFO "%s: QuadEthernet channel[%d] ", qe_devs[i]->name, i);
  770. for (j = 0; j < 6; j++)
  771. printk ("%2.2x%c",
  772. qe_devs[i]->dev_addr[j],
  773. j == 5 ? ' ': ':');
  774. printk("\n");
  775. }
  776. /* We are home free at this point, link the qe's into
  777. * the master list for later driver exit.
  778. */
  779. qecp->next_module = root_qec_dev;
  780. root_qec_dev = qecp;
  781. return 0;
  782. out5:
  783. while (i--)
  784. unregister_netdev(qe_devs[i]);
  785. free_irq(sdev->irqs[0], (void *)qecp);
  786. out4:
  787. for (i = 0; i < 4; i++) {
  788. struct sunqe *qe = (struct sunqe *)qe_devs[i]->priv;
  789. if (qe->qcregs)
  790. sbus_iounmap(qe->qcregs, CREG_REG_SIZE);
  791. if (qe->mregs)
  792. sbus_iounmap(qe->mregs, MREGS_REG_SIZE);
  793. if (qe->qe_block)
  794. sbus_free_consistent(qe->qe_sdev,
  795. PAGE_SIZE,
  796. qe->qe_block,
  797. qe->qblock_dvma);
  798. if (qe->buffers)
  799. sbus_free_consistent(qe->qe_sdev,
  800. sizeof(struct sunqe_buffers),
  801. qe->buffers,
  802. qe->buffers_dvma);
  803. }
  804. out3:
  805. sbus_iounmap(qecp->gregs, GLOB_REG_SIZE);
  806. out2:
  807. kfree(qecp);
  808. out1:
  809. i = 4;
  810. out:
  811. while (i--)
  812. free_netdev(qe_devs[i]);
  813. return res;
  814. }
  815. static int __init qec_match(struct sbus_dev *sdev)
  816. {
  817. struct sbus_dev *sibling;
  818. int i;
  819. if (strcmp(sdev->prom_name, "qec") != 0)
  820. return 0;
  821. /* QEC can be parent of either QuadEthernet or BigMAC
  822. * children. Do not confuse this with qfe/SUNW,qfe
  823. * which is a quad-happymeal card and handled by
  824. * a different driver.
  825. */
  826. sibling = sdev->child;
  827. for (i = 0; i < 4; i++) {
  828. if (sibling == NULL)
  829. return 0;
  830. if (strcmp(sibling->prom_name, "qe") != 0)
  831. return 0;
  832. sibling = sibling->next;
  833. }
  834. return 1;
  835. }
  836. static int __init qec_probe(void)
  837. {
  838. struct net_device *dev = NULL;
  839. struct sbus_bus *bus;
  840. struct sbus_dev *sdev = NULL;
  841. static int called;
  842. int cards = 0, v;
  843. root_qec_dev = NULL;
  844. if (called)
  845. return -ENODEV;
  846. called++;
  847. for_each_sbus(bus) {
  848. for_each_sbusdev(sdev, bus) {
  849. if (cards)
  850. dev = NULL;
  851. if (qec_match(sdev)) {
  852. cards++;
  853. if ((v = qec_ether_init(dev, sdev)))
  854. return v;
  855. }
  856. }
  857. }
  858. if (!cards)
  859. return -ENODEV;
  860. return 0;
  861. }
  862. static void __exit qec_cleanup(void)
  863. {
  864. struct sunqec *next_qec;
  865. int i;
  866. while (root_qec_dev) {
  867. next_qec = root_qec_dev->next_module;
  868. /* Release all four QE channels, then the QEC itself. */
  869. for (i = 0; i < 4; i++) {
  870. unregister_netdev(root_qec_dev->qes[i]->dev);
  871. sbus_iounmap(root_qec_dev->qes[i]->qcregs, CREG_REG_SIZE);
  872. sbus_iounmap(root_qec_dev->qes[i]->mregs, MREGS_REG_SIZE);
  873. sbus_free_consistent(root_qec_dev->qes[i]->qe_sdev,
  874. PAGE_SIZE,
  875. root_qec_dev->qes[i]->qe_block,
  876. root_qec_dev->qes[i]->qblock_dvma);
  877. sbus_free_consistent(root_qec_dev->qes[i]->qe_sdev,
  878. sizeof(struct sunqe_buffers),
  879. root_qec_dev->qes[i]->buffers,
  880. root_qec_dev->qes[i]->buffers_dvma);
  881. free_netdev(root_qec_dev->qes[i]->dev);
  882. }
  883. free_irq(root_qec_dev->qec_sdev->irqs[0], (void *)root_qec_dev);
  884. sbus_iounmap(root_qec_dev->gregs, GLOB_REG_SIZE);
  885. kfree(root_qec_dev);
  886. root_qec_dev = next_qec;
  887. }
  888. }
  889. module_init(qec_probe);
  890. module_exit(qec_cleanup);