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