sgiseeq.c 20 KB

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