sgiseeq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * sgiseeq.c: Seeq8003 ethernet driver for SGI machines.
  3. *
  4. * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
  5. */
  6. #undef DEBUG
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/skbuff.h>
  20. #include <asm/sgi/hpc3.h>
  21. #include <asm/sgi/ip22.h>
  22. #include <asm/sgi/seeq.h>
  23. #include "sgiseeq.h"
  24. static char *sgiseeqstr = "SGI Seeq8003";
  25. /*
  26. * If you want speed, you do something silly, it always has worked for me. So,
  27. * with that in mind, I've decided to make this driver look completely like a
  28. * stupid Lance from a driver architecture perspective. Only difference is that
  29. * here our "ring buffer" looks and acts like a real Lance one does but is
  30. * laid out like how the HPC DMA and the Seeq want it to. You'd be surprised
  31. * how a stupid idea like this can pay off in performance, not to mention
  32. * making this driver 2,000 times easier to write. ;-)
  33. */
  34. /* Tune these if we tend to run out often etc. */
  35. #define SEEQ_RX_BUFFERS 16
  36. #define SEEQ_TX_BUFFERS 16
  37. #define PKT_BUF_SZ 1584
  38. #define NEXT_RX(i) (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
  39. #define NEXT_TX(i) (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
  40. #define PREV_RX(i) (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
  41. #define PREV_TX(i) (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
  42. #define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
  43. sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
  44. sp->tx_old - sp->tx_new - 1)
  45. #define VIRT_TO_DMA(sp, v) ((sp)->srings_dma + \
  46. (dma_addr_t)((unsigned long)(v) - \
  47. (unsigned long)((sp)->rx_desc)))
  48. /* Copy frames shorter than rx_copybreak, otherwise pass on up in
  49. * a full sized sk_buff. Value of 100 stolen from tulip.c (!alpha).
  50. */
  51. static int rx_copybreak = 100;
  52. #define PAD_SIZE (128 - sizeof(struct hpc_dma_desc) - sizeof(void *))
  53. struct sgiseeq_rx_desc {
  54. volatile struct hpc_dma_desc rdma;
  55. u8 padding[PAD_SIZE];
  56. struct sk_buff *skb;
  57. };
  58. struct sgiseeq_tx_desc {
  59. volatile struct hpc_dma_desc tdma;
  60. u8 padding[PAD_SIZE];
  61. struct sk_buff *skb;
  62. };
  63. /*
  64. * Warning: This structure is laid out in a certain way because HPC dma
  65. * descriptors must be 8-byte aligned. So don't touch this without
  66. * some care.
  67. */
  68. struct sgiseeq_init_block { /* Note the name ;-) */
  69. struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
  70. struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
  71. };
  72. struct sgiseeq_private {
  73. struct sgiseeq_init_block *srings;
  74. dma_addr_t srings_dma;
  75. /* Ptrs to the descriptors in uncached space. */
  76. struct sgiseeq_rx_desc *rx_desc;
  77. struct sgiseeq_tx_desc *tx_desc;
  78. char *name;
  79. struct hpc3_ethregs *hregs;
  80. struct sgiseeq_regs *sregs;
  81. /* Ring entry counters. */
  82. unsigned int rx_new, tx_new;
  83. unsigned int rx_old, tx_old;
  84. int is_edlc;
  85. unsigned char control;
  86. unsigned char mode;
  87. spinlock_t tx_lock;
  88. };
  89. static inline void dma_sync_desc_cpu(struct net_device *dev, void *addr)
  90. {
  91. dma_cache_sync(dev->dev.parent, addr, sizeof(struct sgiseeq_rx_desc),
  92. DMA_FROM_DEVICE);
  93. }
  94. static inline void dma_sync_desc_dev(struct net_device *dev, void *addr)
  95. {
  96. dma_cache_sync(dev->dev.parent, addr, sizeof(struct sgiseeq_rx_desc),
  97. DMA_TO_DEVICE);
  98. }
  99. static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
  100. {
  101. hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
  102. udelay(20);
  103. hregs->reset = 0;
  104. }
  105. static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
  106. struct sgiseeq_regs *sregs)
  107. {
  108. hregs->rx_ctrl = hregs->tx_ctrl = 0;
  109. hpc3_eth_reset(hregs);
  110. }
  111. #define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
  112. SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
  113. static inline void seeq_go(struct sgiseeq_private *sp,
  114. struct hpc3_ethregs *hregs,
  115. struct sgiseeq_regs *sregs)
  116. {
  117. sregs->rstat = sp->mode | RSTAT_GO_BITS;
  118. hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
  119. }
  120. static inline void __sgiseeq_set_mac_address(struct net_device *dev)
  121. {
  122. struct sgiseeq_private *sp = netdev_priv(dev);
  123. struct sgiseeq_regs *sregs = sp->sregs;
  124. int i;
  125. sregs->tstat = SEEQ_TCMD_RB0;
  126. for (i = 0; i < 6; i++)
  127. sregs->rw.eth_addr[i] = dev->dev_addr[i];
  128. }
  129. static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
  130. {
  131. struct sgiseeq_private *sp = netdev_priv(dev);
  132. struct sockaddr *sa = addr;
  133. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  134. spin_lock_irq(&sp->tx_lock);
  135. __sgiseeq_set_mac_address(dev);
  136. spin_unlock_irq(&sp->tx_lock);
  137. return 0;
  138. }
  139. #define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
  140. #define RCNTCFG_INIT (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
  141. #define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
  142. static int seeq_init_ring(struct net_device *dev)
  143. {
  144. struct sgiseeq_private *sp = netdev_priv(dev);
  145. int i;
  146. netif_stop_queue(dev);
  147. sp->rx_new = sp->tx_new = 0;
  148. sp->rx_old = sp->tx_old = 0;
  149. __sgiseeq_set_mac_address(dev);
  150. /* Setup tx ring. */
  151. for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
  152. sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
  153. dma_sync_desc_dev(dev, &sp->tx_desc[i]);
  154. }
  155. /* And now the rx ring. */
  156. for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
  157. if (!sp->rx_desc[i].skb) {
  158. dma_addr_t dma_addr;
  159. struct sk_buff *skb = netdev_alloc_skb(dev, PKT_BUF_SZ);
  160. if (skb == NULL)
  161. return -ENOMEM;
  162. skb_reserve(skb, 2);
  163. dma_addr = dma_map_single(dev->dev.parent,
  164. skb->data - 2,
  165. PKT_BUF_SZ, DMA_FROM_DEVICE);
  166. sp->rx_desc[i].skb = skb;
  167. sp->rx_desc[i].rdma.pbuf = dma_addr;
  168. }
  169. sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
  170. dma_sync_desc_dev(dev, &sp->rx_desc[i]);
  171. }
  172. sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
  173. dma_sync_desc_dev(dev, &sp->rx_desc[i - 1]);
  174. return 0;
  175. }
  176. static void seeq_purge_ring(struct net_device *dev)
  177. {
  178. struct sgiseeq_private *sp = netdev_priv(dev);
  179. int i;
  180. /* clear tx ring. */
  181. for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
  182. if (sp->tx_desc[i].skb) {
  183. dev_kfree_skb(sp->tx_desc[i].skb);
  184. sp->tx_desc[i].skb = NULL;
  185. }
  186. }
  187. /* And now the rx ring. */
  188. for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
  189. if (sp->rx_desc[i].skb) {
  190. dev_kfree_skb(sp->rx_desc[i].skb);
  191. sp->rx_desc[i].skb = NULL;
  192. }
  193. }
  194. }
  195. #ifdef DEBUG
  196. static struct sgiseeq_private *gpriv;
  197. static struct net_device *gdev;
  198. static void sgiseeq_dump_rings(void)
  199. {
  200. static int once;
  201. struct sgiseeq_rx_desc *r = gpriv->rx_desc;
  202. struct sgiseeq_tx_desc *t = gpriv->tx_desc;
  203. struct hpc3_ethregs *hregs = gpriv->hregs;
  204. int i;
  205. if (once)
  206. return;
  207. once++;
  208. printk("RING DUMP:\n");
  209. for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
  210. printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
  211. i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
  212. r[i].rdma.pnext);
  213. i += 1;
  214. printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
  215. i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
  216. r[i].rdma.pnext);
  217. }
  218. for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
  219. printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
  220. i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
  221. t[i].tdma.pnext);
  222. i += 1;
  223. printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
  224. i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
  225. t[i].tdma.pnext);
  226. }
  227. printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
  228. gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
  229. printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
  230. hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
  231. printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
  232. hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
  233. }
  234. #endif
  235. #define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
  236. #define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
  237. static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
  238. struct sgiseeq_regs *sregs)
  239. {
  240. struct hpc3_ethregs *hregs = sp->hregs;
  241. int err;
  242. reset_hpc3_and_seeq(hregs, sregs);
  243. err = seeq_init_ring(dev);
  244. if (err)
  245. return err;
  246. /* Setup to field the proper interrupt types. */
  247. if (sp->is_edlc) {
  248. sregs->tstat = TSTAT_INIT_EDLC;
  249. sregs->rw.wregs.control = sp->control;
  250. sregs->rw.wregs.frame_gap = 0;
  251. } else {
  252. sregs->tstat = TSTAT_INIT_SEEQ;
  253. }
  254. hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc);
  255. hregs->tx_ndptr = VIRT_TO_DMA(sp, sp->tx_desc);
  256. seeq_go(sp, hregs, sregs);
  257. return 0;
  258. }
  259. static void record_rx_errors(struct net_device *dev, unsigned char status)
  260. {
  261. if (status & SEEQ_RSTAT_OVERF ||
  262. status & SEEQ_RSTAT_SFRAME)
  263. dev->stats.rx_over_errors++;
  264. if (status & SEEQ_RSTAT_CERROR)
  265. dev->stats.rx_crc_errors++;
  266. if (status & SEEQ_RSTAT_DERROR)
  267. dev->stats.rx_frame_errors++;
  268. if (status & SEEQ_RSTAT_REOF)
  269. dev->stats.rx_errors++;
  270. }
  271. static inline void rx_maybe_restart(struct sgiseeq_private *sp,
  272. struct hpc3_ethregs *hregs,
  273. struct sgiseeq_regs *sregs)
  274. {
  275. if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
  276. hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc + sp->rx_new);
  277. seeq_go(sp, hregs, sregs);
  278. }
  279. }
  280. static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
  281. struct hpc3_ethregs *hregs,
  282. struct sgiseeq_regs *sregs)
  283. {
  284. struct sgiseeq_rx_desc *rd;
  285. struct sk_buff *skb = NULL;
  286. struct sk_buff *newskb;
  287. unsigned char pkt_status;
  288. int len = 0;
  289. unsigned int orig_end = PREV_RX(sp->rx_new);
  290. /* Service every received packet. */
  291. rd = &sp->rx_desc[sp->rx_new];
  292. dma_sync_desc_cpu(dev, rd);
  293. while (!(rd->rdma.cntinfo & HPCDMA_OWN)) {
  294. len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
  295. dma_unmap_single(dev->dev.parent, rd->rdma.pbuf,
  296. PKT_BUF_SZ, DMA_FROM_DEVICE);
  297. pkt_status = rd->skb->data[len];
  298. if (pkt_status & SEEQ_RSTAT_FIG) {
  299. /* Packet is OK. */
  300. /* We don't want to receive our own packets */
  301. if (memcmp(rd->skb->data + 6, dev->dev_addr, ETH_ALEN)) {
  302. if (len > rx_copybreak) {
  303. skb = rd->skb;
  304. newskb = netdev_alloc_skb(dev, PKT_BUF_SZ);
  305. if (!newskb) {
  306. newskb = skb;
  307. skb = NULL;
  308. goto memory_squeeze;
  309. }
  310. skb_reserve(newskb, 2);
  311. } else {
  312. skb = netdev_alloc_skb_ip_align(dev, len);
  313. if (skb)
  314. skb_copy_to_linear_data(skb, rd->skb->data, len);
  315. newskb = rd->skb;
  316. }
  317. memory_squeeze:
  318. if (skb) {
  319. skb_put(skb, len);
  320. skb->protocol = eth_type_trans(skb, dev);
  321. netif_rx(skb);
  322. dev->stats.rx_packets++;
  323. dev->stats.rx_bytes += len;
  324. } else {
  325. printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n",
  326. dev->name);
  327. dev->stats.rx_dropped++;
  328. }
  329. } else {
  330. /* Silently drop my own packets */
  331. newskb = rd->skb;
  332. }
  333. } else {
  334. record_rx_errors(dev, pkt_status);
  335. newskb = rd->skb;
  336. }
  337. rd->skb = newskb;
  338. rd->rdma.pbuf = dma_map_single(dev->dev.parent,
  339. newskb->data - 2,
  340. PKT_BUF_SZ, DMA_FROM_DEVICE);
  341. /* Return the entry to the ring pool. */
  342. rd->rdma.cntinfo = RCNTINFO_INIT;
  343. sp->rx_new = NEXT_RX(sp->rx_new);
  344. dma_sync_desc_dev(dev, rd);
  345. rd = &sp->rx_desc[sp->rx_new];
  346. dma_sync_desc_cpu(dev, rd);
  347. }
  348. dma_sync_desc_cpu(dev, &sp->rx_desc[orig_end]);
  349. sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
  350. dma_sync_desc_dev(dev, &sp->rx_desc[orig_end]);
  351. dma_sync_desc_cpu(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
  352. sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
  353. dma_sync_desc_dev(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
  354. rx_maybe_restart(sp, hregs, sregs);
  355. }
  356. static inline void tx_maybe_reset_collisions(struct sgiseeq_private *sp,
  357. struct sgiseeq_regs *sregs)
  358. {
  359. if (sp->is_edlc) {
  360. sregs->rw.wregs.control = sp->control & ~(SEEQ_CTRL_XCNT);
  361. sregs->rw.wregs.control = sp->control;
  362. }
  363. }
  364. static inline void kick_tx(struct net_device *dev,
  365. struct sgiseeq_private *sp,
  366. struct hpc3_ethregs *hregs)
  367. {
  368. struct sgiseeq_tx_desc *td;
  369. int i = sp->tx_old;
  370. /* If the HPC aint doin nothin, and there are more packets
  371. * with ETXD cleared and XIU set we must make very certain
  372. * that we restart the HPC else we risk locking up the
  373. * adapter. The following code is only safe iff the HPCDMA
  374. * is not active!
  375. */
  376. td = &sp->tx_desc[i];
  377. dma_sync_desc_cpu(dev, td);
  378. while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
  379. (HPCDMA_XIU | HPCDMA_ETXD)) {
  380. i = NEXT_TX(i);
  381. td = &sp->tx_desc[i];
  382. dma_sync_desc_cpu(dev, td);
  383. }
  384. if (td->tdma.cntinfo & HPCDMA_XIU) {
  385. hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
  386. hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
  387. }
  388. }
  389. static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
  390. struct hpc3_ethregs *hregs,
  391. struct sgiseeq_regs *sregs)
  392. {
  393. struct sgiseeq_tx_desc *td;
  394. unsigned long status = hregs->tx_ctrl;
  395. int j;
  396. tx_maybe_reset_collisions(sp, sregs);
  397. if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
  398. /* Oops, HPC detected some sort of error. */
  399. if (status & SEEQ_TSTAT_R16)
  400. dev->stats.tx_aborted_errors++;
  401. if (status & SEEQ_TSTAT_UFLOW)
  402. dev->stats.tx_fifo_errors++;
  403. if (status & SEEQ_TSTAT_LCLS)
  404. dev->stats.collisions++;
  405. }
  406. /* Ack 'em... */
  407. for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
  408. td = &sp->tx_desc[j];
  409. dma_sync_desc_cpu(dev, td);
  410. if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
  411. break;
  412. if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
  413. if (!(status & HPC3_ETXCTRL_ACTIVE)) {
  414. hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
  415. hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
  416. }
  417. break;
  418. }
  419. dev->stats.tx_packets++;
  420. sp->tx_old = NEXT_TX(sp->tx_old);
  421. td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
  422. td->tdma.cntinfo |= HPCDMA_EOX;
  423. if (td->skb) {
  424. dev_kfree_skb_any(td->skb);
  425. td->skb = NULL;
  426. }
  427. dma_sync_desc_dev(dev, td);
  428. }
  429. }
  430. static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id)
  431. {
  432. struct net_device *dev = (struct net_device *) dev_id;
  433. struct sgiseeq_private *sp = netdev_priv(dev);
  434. struct hpc3_ethregs *hregs = sp->hregs;
  435. struct sgiseeq_regs *sregs = sp->sregs;
  436. spin_lock(&sp->tx_lock);
  437. /* Ack the IRQ and set software state. */
  438. hregs->reset = HPC3_ERST_CLRIRQ;
  439. /* Always check for received packets. */
  440. sgiseeq_rx(dev, sp, hregs, sregs);
  441. /* Only check for tx acks if we have something queued. */
  442. if (sp->tx_old != sp->tx_new)
  443. sgiseeq_tx(dev, sp, hregs, sregs);
  444. if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
  445. netif_wake_queue(dev);
  446. }
  447. spin_unlock(&sp->tx_lock);
  448. return IRQ_HANDLED;
  449. }
  450. static int sgiseeq_open(struct net_device *dev)
  451. {
  452. struct sgiseeq_private *sp = netdev_priv(dev);
  453. struct sgiseeq_regs *sregs = sp->sregs;
  454. unsigned int irq = dev->irq;
  455. int err;
  456. if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
  457. printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
  458. return -EAGAIN;
  459. }
  460. err = init_seeq(dev, sp, sregs);
  461. if (err)
  462. goto out_free_irq;
  463. netif_start_queue(dev);
  464. return 0;
  465. out_free_irq:
  466. free_irq(irq, dev);
  467. return err;
  468. }
  469. static int sgiseeq_close(struct net_device *dev)
  470. {
  471. struct sgiseeq_private *sp = netdev_priv(dev);
  472. struct sgiseeq_regs *sregs = sp->sregs;
  473. unsigned int irq = dev->irq;
  474. netif_stop_queue(dev);
  475. /* Shutdown the Seeq. */
  476. reset_hpc3_and_seeq(sp->hregs, sregs);
  477. free_irq(irq, dev);
  478. seeq_purge_ring(dev);
  479. return 0;
  480. }
  481. static inline int sgiseeq_reset(struct net_device *dev)
  482. {
  483. struct sgiseeq_private *sp = netdev_priv(dev);
  484. struct sgiseeq_regs *sregs = sp->sregs;
  485. int err;
  486. err = init_seeq(dev, sp, sregs);
  487. if (err)
  488. return err;
  489. dev->trans_start = jiffies; /* prevent tx timeout */
  490. netif_wake_queue(dev);
  491. return 0;
  492. }
  493. static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
  494. {
  495. struct sgiseeq_private *sp = netdev_priv(dev);
  496. struct hpc3_ethregs *hregs = sp->hregs;
  497. unsigned long flags;
  498. struct sgiseeq_tx_desc *td;
  499. int len, entry;
  500. spin_lock_irqsave(&sp->tx_lock, flags);
  501. /* Setup... */
  502. len = skb->len;
  503. if (len < ETH_ZLEN) {
  504. if (skb_padto(skb, ETH_ZLEN)) {
  505. spin_unlock_irqrestore(&sp->tx_lock, flags);
  506. return NETDEV_TX_OK;
  507. }
  508. len = ETH_ZLEN;
  509. }
  510. dev->stats.tx_bytes += len;
  511. entry = sp->tx_new;
  512. td = &sp->tx_desc[entry];
  513. dma_sync_desc_cpu(dev, td);
  514. /* Create entry. There are so many races with adding a new
  515. * descriptor to the chain:
  516. * 1) Assume that the HPC is off processing a DMA chain while
  517. * we are changing all of the following.
  518. * 2) Do no allow the HPC to look at a new descriptor until
  519. * we have completely set up it's state. This means, do
  520. * not clear HPCDMA_EOX in the current last descritptor
  521. * until the one we are adding looks consistent and could
  522. * be processes right now.
  523. * 3) The tx interrupt code must notice when we've added a new
  524. * entry and the HPC got to the end of the chain before we
  525. * added this new entry and restarted it.
  526. */
  527. td->skb = skb;
  528. td->tdma.pbuf = dma_map_single(dev->dev.parent, skb->data,
  529. len, DMA_TO_DEVICE);
  530. td->tdma.cntinfo = (len & HPCDMA_BCNT) |
  531. HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
  532. dma_sync_desc_dev(dev, td);
  533. if (sp->tx_old != sp->tx_new) {
  534. struct sgiseeq_tx_desc *backend;
  535. backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
  536. dma_sync_desc_cpu(dev, backend);
  537. backend->tdma.cntinfo &= ~HPCDMA_EOX;
  538. dma_sync_desc_dev(dev, backend);
  539. }
  540. sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
  541. /* Maybe kick the HPC back into motion. */
  542. if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
  543. kick_tx(dev, sp, hregs);
  544. if (!TX_BUFFS_AVAIL(sp))
  545. netif_stop_queue(dev);
  546. spin_unlock_irqrestore(&sp->tx_lock, flags);
  547. return NETDEV_TX_OK;
  548. }
  549. static void timeout(struct net_device *dev)
  550. {
  551. printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
  552. sgiseeq_reset(dev);
  553. dev->trans_start = jiffies; /* prevent tx timeout */
  554. netif_wake_queue(dev);
  555. }
  556. static void sgiseeq_set_multicast(struct net_device *dev)
  557. {
  558. struct sgiseeq_private *sp = netdev_priv(dev);
  559. unsigned char oldmode = sp->mode;
  560. if(dev->flags & IFF_PROMISC)
  561. sp->mode = SEEQ_RCMD_RANY;
  562. else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
  563. sp->mode = SEEQ_RCMD_RBMCAST;
  564. else
  565. sp->mode = SEEQ_RCMD_RBCAST;
  566. /* XXX I know this sucks, but is there a better way to reprogram
  567. * XXX the receiver? At least, this shouldn't happen too often.
  568. */
  569. if (oldmode != sp->mode)
  570. sgiseeq_reset(dev);
  571. }
  572. static inline void setup_tx_ring(struct net_device *dev,
  573. struct sgiseeq_tx_desc *buf,
  574. int nbufs)
  575. {
  576. struct sgiseeq_private *sp = netdev_priv(dev);
  577. int i = 0;
  578. while (i < (nbufs - 1)) {
  579. buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
  580. buf[i].tdma.pbuf = 0;
  581. dma_sync_desc_dev(dev, &buf[i]);
  582. i++;
  583. }
  584. buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf);
  585. dma_sync_desc_dev(dev, &buf[i]);
  586. }
  587. static inline void setup_rx_ring(struct net_device *dev,
  588. struct sgiseeq_rx_desc *buf,
  589. int nbufs)
  590. {
  591. struct sgiseeq_private *sp = netdev_priv(dev);
  592. int i = 0;
  593. while (i < (nbufs - 1)) {
  594. buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
  595. buf[i].rdma.pbuf = 0;
  596. dma_sync_desc_dev(dev, &buf[i]);
  597. i++;
  598. }
  599. buf[i].rdma.pbuf = 0;
  600. buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf);
  601. dma_sync_desc_dev(dev, &buf[i]);
  602. }
  603. static const struct net_device_ops sgiseeq_netdev_ops = {
  604. .ndo_open = sgiseeq_open,
  605. .ndo_stop = sgiseeq_close,
  606. .ndo_start_xmit = sgiseeq_start_xmit,
  607. .ndo_tx_timeout = timeout,
  608. .ndo_set_multicast_list = sgiseeq_set_multicast,
  609. .ndo_set_mac_address = sgiseeq_set_mac_address,
  610. .ndo_change_mtu = eth_change_mtu,
  611. .ndo_validate_addr = eth_validate_addr,
  612. };
  613. static int __devinit sgiseeq_probe(struct platform_device *pdev)
  614. {
  615. struct sgiseeq_platform_data *pd = pdev->dev.platform_data;
  616. struct hpc3_regs *hpcregs = pd->hpc;
  617. struct sgiseeq_init_block *sr;
  618. unsigned int irq = pd->irq;
  619. struct sgiseeq_private *sp;
  620. struct net_device *dev;
  621. int err;
  622. dev = alloc_etherdev(sizeof (struct sgiseeq_private));
  623. if (!dev) {
  624. printk(KERN_ERR "Sgiseeq: Etherdev alloc failed, aborting.\n");
  625. err = -ENOMEM;
  626. goto err_out;
  627. }
  628. platform_set_drvdata(pdev, dev);
  629. sp = netdev_priv(dev);
  630. /* Make private data page aligned */
  631. sr = dma_alloc_noncoherent(&pdev->dev, sizeof(*sp->srings),
  632. &sp->srings_dma, GFP_KERNEL);
  633. if (!sr) {
  634. printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
  635. err = -ENOMEM;
  636. goto err_out_free_dev;
  637. }
  638. sp->srings = sr;
  639. sp->rx_desc = sp->srings->rxvector;
  640. sp->tx_desc = sp->srings->txvector;
  641. /* A couple calculations now, saves many cycles later. */
  642. setup_rx_ring(dev, sp->rx_desc, SEEQ_RX_BUFFERS);
  643. setup_tx_ring(dev, sp->tx_desc, SEEQ_TX_BUFFERS);
  644. memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
  645. #ifdef DEBUG
  646. gpriv = sp;
  647. gdev = dev;
  648. #endif
  649. sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
  650. sp->hregs = &hpcregs->ethregs;
  651. sp->name = sgiseeqstr;
  652. sp->mode = SEEQ_RCMD_RBCAST;
  653. /* Setup PIO and DMA transfer timing */
  654. sp->hregs->pconfig = 0x161;
  655. sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
  656. HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
  657. /* Setup PIO and DMA transfer timing */
  658. sp->hregs->pconfig = 0x161;
  659. sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
  660. HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
  661. /* Reset the chip. */
  662. hpc3_eth_reset(sp->hregs);
  663. sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
  664. if (sp->is_edlc)
  665. sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
  666. SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
  667. SEEQ_CTRL_ENCARR;
  668. dev->netdev_ops = &sgiseeq_netdev_ops;
  669. dev->watchdog_timeo = (200 * HZ) / 1000;
  670. dev->irq = irq;
  671. if (register_netdev(dev)) {
  672. printk(KERN_ERR "Sgiseeq: Cannot register net device, "
  673. "aborting.\n");
  674. err = -ENODEV;
  675. goto err_out_free_page;
  676. }
  677. printk(KERN_INFO "%s: %s %pM\n", dev->name, sgiseeqstr, dev->dev_addr);
  678. return 0;
  679. err_out_free_page:
  680. free_page((unsigned long) sp->srings);
  681. err_out_free_dev:
  682. free_netdev(dev);
  683. err_out:
  684. return err;
  685. }
  686. static int __exit sgiseeq_remove(struct platform_device *pdev)
  687. {
  688. struct net_device *dev = platform_get_drvdata(pdev);
  689. struct sgiseeq_private *sp = netdev_priv(dev);
  690. unregister_netdev(dev);
  691. dma_free_noncoherent(&pdev->dev, sizeof(*sp->srings), sp->srings,
  692. sp->srings_dma);
  693. free_netdev(dev);
  694. platform_set_drvdata(pdev, NULL);
  695. return 0;
  696. }
  697. static struct platform_driver sgiseeq_driver = {
  698. .probe = sgiseeq_probe,
  699. .remove = __exit_p(sgiseeq_remove),
  700. .driver = {
  701. .name = "sgiseeq",
  702. .owner = THIS_MODULE,
  703. }
  704. };
  705. static int __init sgiseeq_module_init(void)
  706. {
  707. if (platform_driver_register(&sgiseeq_driver)) {
  708. printk(KERN_ERR "Driver registration failed\n");
  709. return -ENODEV;
  710. }
  711. return 0;
  712. }
  713. static void __exit sgiseeq_module_exit(void)
  714. {
  715. platform_driver_unregister(&sgiseeq_driver);
  716. }
  717. module_init(sgiseeq_module_init);
  718. module_exit(sgiseeq_module_exit);
  719. MODULE_DESCRIPTION("SGI Seeq 8003 driver");
  720. MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
  721. MODULE_LICENSE("GPL");
  722. MODULE_ALIAS("platform:sgiseeq");