VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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         volatile struct hpc_dma_desc rdma;
68         volatile signed int buf_vaddr;
69 };
70
71 struct sgiseeq_tx_desc {
72         volatile struct hpc_dma_desc tdma;
73         volatile 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         struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
83         struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
84 };
85
86 struct sgiseeq_private {
87         struct sgiseeq_init_block *srings;
88
89         /* Ptrs to the descriptors in uncached space. */
90         struct sgiseeq_rx_desc *rx_desc;
91         struct sgiseeq_tx_desc *tx_desc;
92
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 = netdev_priv(dev);
156         int i;
157
158         netif_stop_queue(dev);
159         sp->rx_new = sp->tx_new = 0;
160         sp->rx_old = sp->tx_old = 0;
161
162         seeq_load_eaddr(dev, sp->sregs);
163
164         /* XXX for now just accept packets directly to us
165          * XXX and ether-broadcast.  Will do multicast and
166          * XXX promiscuous mode later. -davem
167          */
168         sp->mode = SEEQ_RCMD_RBCAST;
169
170         /* Setup tx ring. */
171         for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
172                 if (!sp->tx_desc[i].tdma.pbuf) {
173                         unsigned long buffer;
174
175                         buffer = (unsigned long) kmalloc(PKT_BUF_SZ, GFP_KERNEL);
176                         if (!buffer)
177                                 return -ENOMEM;
178                         sp->tx_desc[i].buf_vaddr = KSEG1ADDR(buffer);
179                         sp->tx_desc[i].tdma.pbuf = CPHYSADDR(buffer);
180                 }
181                 sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
182         }
183
184         /* And now the rx ring. */
185         for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
186                 if (!sp->rx_desc[i].rdma.pbuf) {
187                         unsigned long buffer;
188
189                         buffer = (unsigned long) kmalloc(PKT_BUF_SZ, GFP_KERNEL);
190                         if (!buffer)
191                                 return -ENOMEM;
192                         sp->rx_desc[i].buf_vaddr = KSEG1ADDR(buffer);
193                         sp->rx_desc[i].rdma.pbuf = CPHYSADDR(buffer);
194                 }
195                 sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
196         }
197         sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
198         return 0;
199 }
200
201 #ifdef DEBUG
202 static struct sgiseeq_private *gpriv;
203 static struct net_device *gdev;
204
205 void sgiseeq_dump_rings(void)
206 {
207         static int once;
208         struct sgiseeq_rx_desc *r = gpriv->rx_desc;
209         struct sgiseeq_tx_desc *t = gpriv->tx_desc;
210         struct hpc3_ethregs *hregs = gpriv->hregs;
211         int i;
212
213         if (once)
214                 return;
215         once++;
216         printk("RING DUMP:\n");
217         for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
218                 printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
219                        i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
220                        r[i].rdma.pnext);
221                 i += 1;
222                 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
223                        i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
224                        r[i].rdma.pnext);
225         }
226         for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
227                 printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
228                        i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
229                        t[i].tdma.pnext);
230                 i += 1;
231                 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
232                        i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
233                        t[i].tdma.pnext);
234         }
235         printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
236                gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
237         printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
238                hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
239         printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
240                hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
241 }
242 #endif
243
244 #define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
245 #define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
246 #define RDMACFG_INIT    (HPC3_ERXDCFG_FRXDC | HPC3_ERXDCFG_FEOP | HPC3_ERXDCFG_FIRQ)
247
248 static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
249                      struct sgiseeq_regs *sregs)
250 {
251         struct hpc3_ethregs *hregs = sp->hregs;
252         int err;
253
254         reset_hpc3_and_seeq(hregs, sregs);
255         err = seeq_init_ring(dev);
256         if (err)
257                 return err;
258
259         /* Setup to field the proper interrupt types. */
260         if (sp->is_edlc) {
261                 sregs->tstat = TSTAT_INIT_EDLC;
262                 sregs->rw.wregs.control = sp->control;
263                 sregs->rw.wregs.frame_gap = 0;
264         } else {
265                 sregs->tstat = TSTAT_INIT_SEEQ;
266         }
267
268         hregs->rx_dconfig |= RDMACFG_INIT;
269
270         hregs->rx_ndptr = CPHYSADDR(sp->rx_desc);
271         hregs->tx_ndptr = CPHYSADDR(sp->tx_desc);
272
273         seeq_go(sp, hregs, sregs);
274         return 0;
275 }
276
277 static inline void record_rx_errors(struct sgiseeq_private *sp,
278                                     unsigned char status)
279 {
280         if (status & SEEQ_RSTAT_OVERF ||
281             status & SEEQ_RSTAT_SFRAME)
282                 sp->stats.rx_over_errors++;
283         if (status & SEEQ_RSTAT_CERROR)
284                 sp->stats.rx_crc_errors++;
285         if (status & SEEQ_RSTAT_DERROR)
286                 sp->stats.rx_frame_errors++;
287         if (status & SEEQ_RSTAT_REOF)
288                 sp->stats.rx_errors++;
289 }
290
291 static inline void rx_maybe_restart(struct sgiseeq_private *sp,
292                                     struct hpc3_ethregs *hregs,
293                                     struct sgiseeq_regs *sregs)
294 {
295         if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
296                 hregs->rx_ndptr = CPHYSADDR(sp->rx_desc + sp->rx_new);
297                 seeq_go(sp, hregs, sregs);
298         }
299 }
300
301 #define for_each_rx(rd, sp) for((rd) = &(sp)->rx_desc[(sp)->rx_new]; \
302                                 !((rd)->rdma.cntinfo & HPCDMA_OWN); \
303                                 (rd) = &(sp)->rx_desc[(sp)->rx_new])
304
305 static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
306                               struct hpc3_ethregs *hregs,
307                               struct sgiseeq_regs *sregs)
308 {
309         struct sgiseeq_rx_desc *rd;
310         struct sk_buff *skb = 0;
311         unsigned char pkt_status;
312         unsigned char *pkt_pointer = 0;
313         int len = 0;
314         unsigned int orig_end = PREV_RX(sp->rx_new);
315
316         /* Service every received packet. */
317         for_each_rx(rd, sp) {
318                 len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
319                 pkt_pointer = (unsigned char *)(long)rd->buf_vaddr;
320                 pkt_status = pkt_pointer[len + 2];
321
322                 if (pkt_status & SEEQ_RSTAT_FIG) {
323                         /* Packet is OK. */
324                         skb = dev_alloc_skb(len + 2);
325
326                         if (skb) {
327                                 skb->dev = dev;
328                                 skb_reserve(skb, 2);
329                                 skb_put(skb, len);
330
331                                 /* Copy out of kseg1 to avoid silly cache flush. */
332                                 eth_copy_and_sum(skb, pkt_pointer + 2, len, 0);
333                                 skb->protocol = eth_type_trans(skb, dev);
334                                 netif_rx(skb);
335                                 dev->last_rx = jiffies;
336                                 sp->stats.rx_packets++;
337                                 sp->stats.rx_bytes += len;
338                         } else {
339                                 printk (KERN_NOTICE "%s: Memory squeeze, deferring packet.\n",
340                                         dev->name);
341                                 sp->stats.rx_dropped++;
342                         }
343                 } else {
344                         record_rx_errors(sp, pkt_status);
345                 }
346
347                 /* Return the entry to the ring pool. */
348                 rd->rdma.cntinfo = RCNTINFO_INIT;
349                 sp->rx_new = NEXT_RX(sp->rx_new);
350         }
351         sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
352         sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
353         rx_maybe_restart(sp, hregs, sregs);
354 }
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
365 static inline void kick_tx(struct sgiseeq_tx_desc *td,
366                            struct hpc3_ethregs *hregs)
367 {
368         /* If the HPC aint doin nothin, and there are more packets
369          * with ETXD cleared and XIU set we must make very certain
370          * that we restart the HPC else we risk locking up the
371          * adapter.  The following code is only safe iff the HPCDMA
372          * is not active!
373          */
374         while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
375               (HPCDMA_XIU | HPCDMA_ETXD))
376                 td = (struct sgiseeq_tx_desc *)(long) KSEG1ADDR(td->tdma.pnext);
377         if (td->tdma.cntinfo & HPCDMA_XIU) {
378                 hregs->tx_ndptr = CPHYSADDR(td);
379                 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
380         }
381 }
382
383 static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
384                               struct hpc3_ethregs *hregs,
385                               struct sgiseeq_regs *sregs)
386 {
387         struct sgiseeq_tx_desc *td;
388         unsigned long status = hregs->tx_ctrl;
389         int j;
390
391         tx_maybe_reset_collisions(sp, sregs);
392
393         if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
394                 /* Oops, HPC detected some sort of error. */
395                 if (status & SEEQ_TSTAT_R16)
396                         sp->stats.tx_aborted_errors++;
397                 if (status & SEEQ_TSTAT_UFLOW)
398                         sp->stats.tx_fifo_errors++;
399                 if (status & SEEQ_TSTAT_LCLS)
400                         sp->stats.collisions++;
401         }
402
403         /* Ack 'em... */
404         for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
405                 td = &sp->tx_desc[j];
406
407                 if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
408                         break;
409                 if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
410                         if (!(status & HPC3_ETXCTRL_ACTIVE)) {
411                                 hregs->tx_ndptr = CPHYSADDR(td);
412                                 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
413                         }
414                         break;
415                 }
416                 sp->stats.tx_packets++;
417                 sp->tx_old = NEXT_TX(sp->tx_old);
418                 td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
419                 td->tdma.cntinfo |= HPCDMA_EOX;
420         }
421 }
422
423 static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id, struct pt_regs *regs)
424 {
425         struct net_device *dev = (struct net_device *) dev_id;
426         struct sgiseeq_private *sp = netdev_priv(dev);
427         struct hpc3_ethregs *hregs = sp->hregs;
428         struct sgiseeq_regs *sregs = sp->sregs;
429
430         spin_lock(&sp->tx_lock);
431
432         /* Ack the IRQ and set software state. */
433         hregs->rx_reset = HPC3_ERXRST_CLRIRQ;
434
435         /* Always check for received packets. */
436         sgiseeq_rx(dev, sp, hregs, sregs);
437
438         /* Only check for tx acks if we have something queued. */
439         if (sp->tx_old != sp->tx_new)
440                 sgiseeq_tx(dev, sp, hregs, sregs);
441
442         if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
443                 netif_wake_queue(dev);
444         }
445         spin_unlock(&sp->tx_lock);
446
447         return IRQ_HANDLED;
448 }
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
457         if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
458                 printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
459                 err = -EAGAIN;
460         }
461
462         err = init_seeq(dev, sp, sregs);
463         if (err)
464                 goto out_free_irq;
465
466         netif_start_queue(dev);
467
468         return 0;
469
470 out_free_irq:
471         free_irq(irq, dev);
472
473         return err;
474 }
475
476 static int sgiseeq_close(struct net_device *dev)
477 {
478         struct sgiseeq_private *sp = netdev_priv(dev);
479         struct sgiseeq_regs *sregs = sp->sregs;
480
481         netif_stop_queue(dev);
482
483         /* Shutdown the Seeq. */
484         reset_hpc3_and_seeq(sp->hregs, sregs);
485
486         return 0;
487 }
488
489 static inline int sgiseeq_reset(struct net_device *dev)
490 {
491         struct sgiseeq_private *sp = netdev_priv(dev);
492         struct sgiseeq_regs *sregs = sp->sregs;
493         int err;
494
495         err = init_seeq(dev, sp, sregs);
496         if (err)
497                 return err;
498
499         dev->trans_start = jiffies;
500         netif_wake_queue(dev);
501
502         return 0;
503 }
504
505 void sgiseeq_my_reset(void)
506 {
507         printk("RESET!\n");
508         sgiseeq_reset(gdev);
509 }
510
511 static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
512 {
513         struct sgiseeq_private *sp = netdev_priv(dev);
514         struct hpc3_ethregs *hregs = sp->hregs;
515         unsigned long flags;
516         struct sgiseeq_tx_desc *td;
517         int skblen, len, entry;
518
519         spin_lock_irqsave(&sp->tx_lock, flags);
520
521         /* Setup... */
522         skblen = skb->len;
523         len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
524         sp->stats.tx_bytes += len;
525         entry = sp->tx_new;
526         td = &sp->tx_desc[entry];
527
528         /* Create entry.  There are so many races with adding a new
529          * descriptor to the chain:
530          * 1) Assume that the HPC is off processing a DMA chain while
531          *    we are changing all of the following.
532          * 2) Do no allow the HPC to look at a new descriptor until
533          *    we have completely set up it's state.  This means, do
534          *    not clear HPCDMA_EOX in the current last descritptor
535          *    until the one we are adding looks consistent and could
536          *    be processes right now.
537          * 3) The tx interrupt code must notice when we've added a new
538          *    entry and the HPC got to the end of the chain before we
539          *    added this new entry and restarted it.
540          */
541         memcpy((char *)(long)td->buf_vaddr, skb->data, skblen);
542         if (len != skblen)
543                 memset((char *)(long)td->buf_vaddr + skb->len, 0, len-skblen);
544         td->tdma.cntinfo = (len & HPCDMA_BCNT) |
545                            HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
546         if (sp->tx_old != sp->tx_new) {
547                 struct sgiseeq_tx_desc *backend;
548
549                 backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
550                 backend->tdma.cntinfo &= ~HPCDMA_EOX;
551         }
552         sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
553
554         /* Maybe kick the HPC back into motion. */
555         if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
556                 kick_tx(&sp->tx_desc[sp->tx_old], hregs);
557
558         dev->trans_start = jiffies;
559         dev_kfree_skb(skb);
560
561         if (!TX_BUFFS_AVAIL(sp))
562                 netif_stop_queue(dev);
563         spin_unlock_irqrestore(&sp->tx_lock, flags);
564
565         return 0;
566 }
567
568 static void timeout(struct net_device *dev)
569 {
570         printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
571         sgiseeq_reset(dev);
572
573         dev->trans_start = jiffies;
574         netif_wake_queue(dev);
575 }
576
577 static struct net_device_stats *sgiseeq_get_stats(struct net_device *dev)
578 {
579         struct sgiseeq_private *sp = netdev_priv(dev);
580
581         return &sp->stats;
582 }
583
584 static void sgiseeq_set_multicast(struct net_device *dev)
585 {
586 }
587
588 static inline void setup_tx_ring(struct sgiseeq_tx_desc *buf, int nbufs)
589 {
590         int i = 0;
591
592         while (i < (nbufs - 1)) {
593                 buf[i].tdma.pnext = CPHYSADDR(buf + i + 1);
594                 buf[i].tdma.pbuf = 0;
595                 i++;
596         }
597         buf[i].tdma.pnext = CPHYSADDR(buf);
598 }
599
600 static inline void setup_rx_ring(struct sgiseeq_rx_desc *buf, int nbufs)
601 {
602         int i = 0;
603
604         while (i < (nbufs - 1)) {
605                 buf[i].rdma.pnext = CPHYSADDR(buf + i + 1);
606                 buf[i].rdma.pbuf = 0;
607                 i++;
608         }
609         buf[i].rdma.pbuf = 0;
610         buf[i].rdma.pnext = CPHYSADDR(buf);
611 }
612
613 #define ALIGNED(x)  ((((unsigned long)(x)) + 0xf) & ~(0xf))
614
615 static int sgiseeq_init(struct hpc3_regs* regs, int irq)
616 {
617         struct sgiseeq_init_block *sr;
618         struct sgiseeq_private *sp;
619         struct net_device *dev;
620         int err, i;
621
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         sp = netdev_priv(dev);
629
630         /* Make private data page aligned */
631         sr = (struct sgiseeq_init_block *) get_zeroed_page(GFP_KERNEL);
632         if (!sr) {
633                 printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
634                 err = -ENOMEM;
635                 goto err_out_free_dev;
636         }
637         sp->srings = sr;
638
639 #define EADDR_NVOFS     250
640         for (i = 0; i < 3; i++) {
641                 unsigned short tmp = ip22_nvram_read(EADDR_NVOFS / 2 + i);
642
643                 dev->dev_addr[2 * i]     = tmp >> 8;
644                 dev->dev_addr[2 * i + 1] = tmp & 0xff;
645         }
646
647 #ifdef DEBUG
648         gpriv = sp;
649         gdev = dev;
650 #endif
651         sp->sregs = (struct sgiseeq_regs *) &hpc3c0->eth_ext[0];
652         sp->hregs = &hpc3c0->ethregs;
653         sp->name = sgiseeqstr;
654
655         sp->rx_desc = (struct sgiseeq_rx_desc *)
656                       KSEG1ADDR(ALIGNED(&sp->srings->rxvector[0]));
657         dma_cache_wback_inv((unsigned long)&sp->srings->rxvector,
658                             sizeof(sp->srings->rxvector));
659         sp->tx_desc = (struct sgiseeq_tx_desc *)
660                       KSEG1ADDR(ALIGNED(&sp->srings->txvector[0]));
661         dma_cache_wback_inv((unsigned long)&sp->srings->txvector,
662                             sizeof(sp->srings->txvector));
663
664         /* A couple calculations now, saves many cycles later. */
665         setup_rx_ring(sp->rx_desc, SEEQ_RX_BUFFERS);
666         setup_tx_ring(sp->tx_desc, SEEQ_TX_BUFFERS);
667
668         /* Reset the chip. */
669         hpc3_eth_reset(sp->hregs);
670
671         sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
672         if (sp->is_edlc)
673                 sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
674                               SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
675                               SEEQ_CTRL_ENCARR;
676
677         dev->open               = sgiseeq_open;
678         dev->stop               = sgiseeq_close;
679         dev->hard_start_xmit    = sgiseeq_start_xmit;
680         dev->tx_timeout         = timeout;
681         dev->watchdog_timeo     = (200 * HZ) / 1000;
682         dev->get_stats          = sgiseeq_get_stats;
683         dev->set_multicast_list = sgiseeq_set_multicast;
684         dev->irq                = irq;
685
686         if (register_netdev(dev)) {
687                 printk(KERN_ERR "Sgiseeq: Cannot register net device, "
688                        "aborting.\n");
689                 err = -ENODEV;
690                 goto err_out_free_page;
691         }
692
693         printk(KERN_INFO "%s: SGI Seeq8003 ", dev->name);
694         for (i = 0; i < 6; i++)
695                 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
696
697         sp->next_module = root_sgiseeq_dev;
698         root_sgiseeq_dev = dev;
699
700         return 0;
701
702 err_out_free_page:
703         free_page((unsigned long) sp);
704 err_out_free_dev:
705         kfree(dev);
706
707 err_out:
708         return err;
709 }
710
711 static int __init sgiseeq_probe(void)
712 {
713         printk(version);
714
715         /* On board adapter on 1st HPC is always present */
716         return sgiseeq_init(hpc3c0, SGI_ENET_IRQ);
717 }
718
719 static void __exit sgiseeq_exit(void)
720 {
721         struct net_device *next, *dev;
722         struct sgiseeq_private *sp;
723         int irq;
724
725         for (dev = root_sgiseeq_dev; dev; dev = next) {
726                 sp = (struct sgiseeq_private *) netdev_priv(dev);
727                 next = sp->next_module;
728                 irq = dev->irq;
729                 unregister_netdev(dev);
730                 free_irq(irq, dev);
731                 free_page((unsigned long) sp);
732                 free_netdev(dev);
733         }
734 }
735
736 module_init(sgiseeq_probe);
737 module_exit(sgiseeq_exit);
738
739 MODULE_LICENSE("GPL");