sgiseeq: Don't include unnecessary headerfiles.
[safe/jmp/linux-2.6] / 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
7 #undef DEBUG
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/interrupt.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
21
22 #include <asm/sgi/hpc3.h>
23 #include <asm/sgi/ip22.h>
24
25 #include "sgiseeq.h"
26
27 static char *sgiseeqstr = "SGI Seeq8003";
28
29 /*
30  * If you want speed, you do something silly, it always has worked for me.  So,
31  * with that in mind, I've decided to make this driver look completely like a
32  * stupid Lance from a driver architecture perspective.  Only difference is that
33  * here our "ring buffer" looks and acts like a real Lance one does but is
34  * layed out like how the HPC DMA and the Seeq want it to.  You'd be surprised
35  * how a stupid idea like this can pay off in performance, not to mention
36  * making this driver 2,000 times easier to write. ;-)
37  */
38
39 /* Tune these if we tend to run out often etc. */
40 #define SEEQ_RX_BUFFERS  16
41 #define SEEQ_TX_BUFFERS  16
42
43 #define PKT_BUF_SZ       1584
44
45 #define NEXT_RX(i)  (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
46 #define NEXT_TX(i)  (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
47 #define PREV_RX(i)  (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
48 #define PREV_TX(i)  (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
49
50 #define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
51                             sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
52                             sp->tx_old - sp->tx_new - 1)
53
54 struct sgiseeq_rx_desc {
55         volatile struct hpc_dma_desc rdma;
56         volatile signed int buf_vaddr;
57 };
58
59 struct sgiseeq_tx_desc {
60         volatile struct hpc_dma_desc tdma;
61         volatile signed int buf_vaddr;
62 };
63
64 /*
65  * Warning: This structure is layed out in a certain way because HPC dma
66  *          descriptors must be 8-byte aligned.  So don't touch this without
67  *          some care.
68  */
69 struct sgiseeq_init_block { /* Note the name ;-) */
70         struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
71         struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
72 };
73
74 struct sgiseeq_private {
75         struct sgiseeq_init_block *srings;
76
77         /* Ptrs to the descriptors in uncached space. */
78         struct sgiseeq_rx_desc *rx_desc;
79         struct sgiseeq_tx_desc *tx_desc;
80
81         char *name;
82         struct hpc3_ethregs *hregs;
83         struct sgiseeq_regs *sregs;
84
85         /* Ring entry counters. */
86         unsigned int rx_new, tx_new;
87         unsigned int rx_old, tx_old;
88
89         int is_edlc;
90         unsigned char control;
91         unsigned char mode;
92
93         struct net_device_stats stats;
94
95         struct net_device *next_module;
96         spinlock_t tx_lock;
97 };
98
99 /* A list of all installed seeq devices, for removing the driver module. */
100 static struct net_device *root_sgiseeq_dev;
101
102 static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
103 {
104         hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
105         udelay(20);
106         hregs->reset = 0;
107 }
108
109 static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
110                                        struct sgiseeq_regs *sregs)
111 {
112         hregs->rx_ctrl = hregs->tx_ctrl = 0;
113         hpc3_eth_reset(hregs);
114 }
115
116 #define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
117                        SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
118
119 static inline void seeq_go(struct sgiseeq_private *sp,
120                            struct hpc3_ethregs *hregs,
121                            struct sgiseeq_regs *sregs)
122 {
123         sregs->rstat = sp->mode | RSTAT_GO_BITS;
124         hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
125 }
126
127 static inline void __sgiseeq_set_mac_address(struct net_device *dev)
128 {
129         struct sgiseeq_private *sp = netdev_priv(dev);
130         struct sgiseeq_regs *sregs = sp->sregs;
131         int i;
132
133         sregs->tstat = SEEQ_TCMD_RB0;
134         for (i = 0; i < 6; i++)
135                 sregs->rw.eth_addr[i] = dev->dev_addr[i];
136 }
137
138 static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
139 {
140         struct sgiseeq_private *sp = netdev_priv(dev);
141         struct sockaddr *sa = addr;
142
143         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
144
145         spin_lock_irq(&sp->tx_lock);
146         __sgiseeq_set_mac_address(dev);
147         spin_unlock_irq(&sp->tx_lock);
148
149         return 0;
150 }
151
152 #define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
153 #define RCNTCFG_INIT  (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
154 #define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
155
156 static int seeq_init_ring(struct net_device *dev)
157 {
158         struct sgiseeq_private *sp = netdev_priv(dev);
159         int i;
160
161         netif_stop_queue(dev);
162         sp->rx_new = sp->tx_new = 0;
163         sp->rx_old = sp->tx_old = 0;
164
165         __sgiseeq_set_mac_address(dev);
166
167         /* Setup tx ring. */
168         for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
169                 if (!sp->tx_desc[i].tdma.pbuf) {
170                         unsigned long buffer;
171
172                         buffer = (unsigned long) kmalloc(PKT_BUF_SZ, GFP_KERNEL);
173                         if (!buffer)
174                                 return -ENOMEM;
175                         sp->tx_desc[i].buf_vaddr = CKSEG1ADDR(buffer);
176                         sp->tx_desc[i].tdma.pbuf = CPHYSADDR(buffer);
177                 }
178                 sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
179         }
180
181         /* And now the rx ring. */
182         for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
183                 if (!sp->rx_desc[i].rdma.pbuf) {
184                         unsigned long buffer;
185
186                         buffer = (unsigned long) kmalloc(PKT_BUF_SZ, GFP_KERNEL);
187                         if (!buffer)
188                                 return -ENOMEM;
189                         sp->rx_desc[i].buf_vaddr = CKSEG1ADDR(buffer);
190                         sp->rx_desc[i].rdma.pbuf = CPHYSADDR(buffer);
191                 }
192                 sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
193         }
194         sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
195         return 0;
196 }
197
198 #ifdef DEBUG
199 static struct sgiseeq_private *gpriv;
200 static struct net_device *gdev;
201
202 static void sgiseeq_dump_rings(void)
203 {
204         static int once;
205         struct sgiseeq_rx_desc *r = gpriv->rx_desc;
206         struct sgiseeq_tx_desc *t = gpriv->tx_desc;
207         struct hpc3_ethregs *hregs = gpriv->hregs;
208         int i;
209
210         if (once)
211                 return;
212         once++;
213         printk("RING DUMP:\n");
214         for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
215                 printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
216                        i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
217                        r[i].rdma.pnext);
218                 i += 1;
219                 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
220                        i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
221                        r[i].rdma.pnext);
222         }
223         for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
224                 printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
225                        i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
226                        t[i].tdma.pnext);
227                 i += 1;
228                 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
229                        i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
230                        t[i].tdma.pnext);
231         }
232         printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
233                gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
234         printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
235                hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
236         printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
237                hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
238 }
239 #endif
240
241 #define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
242 #define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
243
244 static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
245                      struct sgiseeq_regs *sregs)
246 {
247         struct hpc3_ethregs *hregs = sp->hregs;
248         int err;
249
250         reset_hpc3_and_seeq(hregs, sregs);
251         err = seeq_init_ring(dev);
252         if (err)
253                 return err;
254
255         /* Setup to field the proper interrupt types. */
256         if (sp->is_edlc) {
257                 sregs->tstat = TSTAT_INIT_EDLC;
258                 sregs->rw.wregs.control = sp->control;
259                 sregs->rw.wregs.frame_gap = 0;
260         } else {
261                 sregs->tstat = TSTAT_INIT_SEEQ;
262         }
263
264         hregs->rx_ndptr = CPHYSADDR(sp->rx_desc);
265         hregs->tx_ndptr = CPHYSADDR(sp->tx_desc);
266
267         seeq_go(sp, hregs, sregs);
268         return 0;
269 }
270
271 static inline void record_rx_errors(struct sgiseeq_private *sp,
272                                     unsigned char status)
273 {
274         if (status & SEEQ_RSTAT_OVERF ||
275             status & SEEQ_RSTAT_SFRAME)
276                 sp->stats.rx_over_errors++;
277         if (status & SEEQ_RSTAT_CERROR)
278                 sp->stats.rx_crc_errors++;
279         if (status & SEEQ_RSTAT_DERROR)
280                 sp->stats.rx_frame_errors++;
281         if (status & SEEQ_RSTAT_REOF)
282                 sp->stats.rx_errors++;
283 }
284
285 static inline void rx_maybe_restart(struct sgiseeq_private *sp,
286                                     struct hpc3_ethregs *hregs,
287                                     struct sgiseeq_regs *sregs)
288 {
289         if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
290                 hregs->rx_ndptr = CPHYSADDR(sp->rx_desc + sp->rx_new);
291                 seeq_go(sp, hregs, sregs);
292         }
293 }
294
295 #define for_each_rx(rd, sp) for((rd) = &(sp)->rx_desc[(sp)->rx_new]; \
296                                 !((rd)->rdma.cntinfo & HPCDMA_OWN); \
297                                 (rd) = &(sp)->rx_desc[(sp)->rx_new])
298
299 static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
300                               struct hpc3_ethregs *hregs,
301                               struct sgiseeq_regs *sregs)
302 {
303         struct sgiseeq_rx_desc *rd;
304         struct sk_buff *skb = NULL;
305         unsigned char pkt_status;
306         unsigned char *pkt_pointer = NULL;
307         int len = 0;
308         unsigned int orig_end = PREV_RX(sp->rx_new);
309
310         /* Service every received packet. */
311         for_each_rx(rd, sp) {
312                 len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
313                 pkt_pointer = (unsigned char *)(long)rd->buf_vaddr;
314                 pkt_status = pkt_pointer[len + 2];
315
316                 if (pkt_status & SEEQ_RSTAT_FIG) {
317                         /* Packet is OK. */
318                         skb = dev_alloc_skb(len + 2);
319
320                         if (skb) {
321                                 skb->dev = dev;
322                                 skb_reserve(skb, 2);
323                                 skb_put(skb, len);
324
325                                 /* Copy out of kseg1 to avoid silly cache flush. */
326                                 eth_copy_and_sum(skb, pkt_pointer + 2, len, 0);
327                                 skb->protocol = eth_type_trans(skb, dev);
328
329                                 /* We don't want to receive our own packets */
330                                 if (memcmp(eth_hdr(skb)->h_source, dev->dev_addr, ETH_ALEN)) {
331                                         netif_rx(skb);
332                                         dev->last_rx = jiffies;
333                                         sp->stats.rx_packets++;
334                                         sp->stats.rx_bytes += len;
335                                 } else {
336                                         /* Silently drop my own packets */
337                                         dev_kfree_skb_irq(skb);
338                                 }
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->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
353         sp->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) CKSEG1ADDR(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->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)
425 {
426         struct net_device *dev = (struct net_device *) dev_id;
427         struct sgiseeq_private *sp = netdev_priv(dev);
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->reset = HPC3_ERST_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 = netdev_priv(dev);
454         struct sgiseeq_regs *sregs = sp->sregs;
455         unsigned int irq = dev->irq;
456         int err;
457
458         if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
459                 printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
460                 err = -EAGAIN;
461         }
462
463         err = init_seeq(dev, sp, sregs);
464         if (err)
465                 goto out_free_irq;
466
467         netif_start_queue(dev);
468
469         return 0;
470
471 out_free_irq:
472         free_irq(irq, dev);
473
474         return err;
475 }
476
477 static int sgiseeq_close(struct net_device *dev)
478 {
479         struct sgiseeq_private *sp = netdev_priv(dev);
480         struct sgiseeq_regs *sregs = sp->sregs;
481         unsigned int irq = dev->irq;
482
483         netif_stop_queue(dev);
484
485         /* Shutdown the Seeq. */
486         reset_hpc3_and_seeq(sp->hregs, sregs);
487         free_irq(irq, dev);
488
489         return 0;
490 }
491
492 static inline int sgiseeq_reset(struct net_device *dev)
493 {
494         struct sgiseeq_private *sp = netdev_priv(dev);
495         struct sgiseeq_regs *sregs = sp->sregs;
496         int err;
497
498         err = init_seeq(dev, sp, sregs);
499         if (err)
500                 return err;
501
502         dev->trans_start = jiffies;
503         netif_wake_queue(dev);
504
505         return 0;
506 }
507
508 static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
509 {
510         struct sgiseeq_private *sp = netdev_priv(dev);
511         struct hpc3_ethregs *hregs = sp->hregs;
512         unsigned long flags;
513         struct sgiseeq_tx_desc *td;
514         int skblen, len, entry;
515
516         spin_lock_irqsave(&sp->tx_lock, flags);
517
518         /* Setup... */
519         skblen = skb->len;
520         len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
521         sp->stats.tx_bytes += len;
522         entry = sp->tx_new;
523         td = &sp->tx_desc[entry];
524
525         /* Create entry.  There are so many races with adding a new
526          * descriptor to the chain:
527          * 1) Assume that the HPC is off processing a DMA chain while
528          *    we are changing all of the following.
529          * 2) Do no allow the HPC to look at a new descriptor until
530          *    we have completely set up it's state.  This means, do
531          *    not clear HPCDMA_EOX in the current last descritptor
532          *    until the one we are adding looks consistent and could
533          *    be processes right now.
534          * 3) The tx interrupt code must notice when we've added a new
535          *    entry and the HPC got to the end of the chain before we
536          *    added this new entry and restarted it.
537          */
538         memcpy((char *)(long)td->buf_vaddr, skb->data, skblen);
539         if (len != skblen)
540                 memset((char *)(long)td->buf_vaddr + skb->len, 0, len-skblen);
541         td->tdma.cntinfo = (len & HPCDMA_BCNT) |
542                            HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
543         if (sp->tx_old != sp->tx_new) {
544                 struct sgiseeq_tx_desc *backend;
545
546                 backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
547                 backend->tdma.cntinfo &= ~HPCDMA_EOX;
548         }
549         sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
550
551         /* Maybe kick the HPC back into motion. */
552         if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
553                 kick_tx(&sp->tx_desc[sp->tx_old], hregs);
554
555         dev->trans_start = jiffies;
556         dev_kfree_skb(skb);
557
558         if (!TX_BUFFS_AVAIL(sp))
559                 netif_stop_queue(dev);
560         spin_unlock_irqrestore(&sp->tx_lock, flags);
561
562         return 0;
563 }
564
565 static void timeout(struct net_device *dev)
566 {
567         printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
568         sgiseeq_reset(dev);
569
570         dev->trans_start = jiffies;
571         netif_wake_queue(dev);
572 }
573
574 static struct net_device_stats *sgiseeq_get_stats(struct net_device *dev)
575 {
576         struct sgiseeq_private *sp = netdev_priv(dev);
577
578         return &sp->stats;
579 }
580
581 static void sgiseeq_set_multicast(struct net_device *dev)
582 {
583         struct sgiseeq_private *sp = (struct sgiseeq_private *) dev->priv;
584         unsigned char oldmode = sp->mode;
585
586         if(dev->flags & IFF_PROMISC)
587                 sp->mode = SEEQ_RCMD_RANY;
588         else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count)
589                 sp->mode = SEEQ_RCMD_RBMCAST;
590         else
591                 sp->mode = SEEQ_RCMD_RBCAST;
592
593         /* XXX I know this sucks, but is there a better way to reprogram
594          * XXX the receiver? At least, this shouldn't happen too often.
595          */
596
597         if (oldmode != sp->mode)
598                 sgiseeq_reset(dev);
599 }
600
601 static inline void setup_tx_ring(struct sgiseeq_tx_desc *buf, int nbufs)
602 {
603         int i = 0;
604
605         while (i < (nbufs - 1)) {
606                 buf[i].tdma.pnext = CPHYSADDR(buf + i + 1);
607                 buf[i].tdma.pbuf = 0;
608                 i++;
609         }
610         buf[i].tdma.pnext = CPHYSADDR(buf);
611 }
612
613 static inline void setup_rx_ring(struct sgiseeq_rx_desc *buf, int nbufs)
614 {
615         int i = 0;
616
617         while (i < (nbufs - 1)) {
618                 buf[i].rdma.pnext = CPHYSADDR(buf + i + 1);
619                 buf[i].rdma.pbuf = 0;
620                 i++;
621         }
622         buf[i].rdma.pbuf = 0;
623         buf[i].rdma.pnext = CPHYSADDR(buf);
624 }
625
626 #define ALIGNED(x)  ((((unsigned long)(x)) + 0xf) & ~(0xf))
627
628 static int sgiseeq_init(struct hpc3_regs* hpcregs, int irq)
629 {
630         struct sgiseeq_init_block *sr;
631         struct sgiseeq_private *sp;
632         struct net_device *dev;
633         int err, i;
634
635         dev = alloc_etherdev(sizeof (struct sgiseeq_private));
636         if (!dev) {
637                 printk(KERN_ERR "Sgiseeq: Etherdev alloc failed, aborting.\n");
638                 err = -ENOMEM;
639                 goto err_out;
640         }
641         sp = netdev_priv(dev);
642
643         /* Make private data page aligned */
644         sr = (struct sgiseeq_init_block *) get_zeroed_page(GFP_KERNEL);
645         if (!sr) {
646                 printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
647                 err = -ENOMEM;
648                 goto err_out_free_dev;
649         }
650         sp->srings = sr;
651
652 #define EADDR_NVOFS     250
653         for (i = 0; i < 3; i++) {
654                 unsigned short tmp = ip22_nvram_read(EADDR_NVOFS / 2 + i);
655
656                 dev->dev_addr[2 * i]     = tmp >> 8;
657                 dev->dev_addr[2 * i + 1] = tmp & 0xff;
658         }
659
660 #ifdef DEBUG
661         gpriv = sp;
662         gdev = dev;
663 #endif
664         sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
665         sp->hregs = &hpcregs->ethregs;
666         sp->name = sgiseeqstr;
667         sp->mode = SEEQ_RCMD_RBCAST;
668
669         sp->rx_desc = (struct sgiseeq_rx_desc *)
670                       CKSEG1ADDR(ALIGNED(&sp->srings->rxvector[0]));
671         dma_cache_wback_inv((unsigned long)&sp->srings->rxvector,
672                             sizeof(sp->srings->rxvector));
673         sp->tx_desc = (struct sgiseeq_tx_desc *)
674                       CKSEG1ADDR(ALIGNED(&sp->srings->txvector[0]));
675         dma_cache_wback_inv((unsigned long)&sp->srings->txvector,
676                             sizeof(sp->srings->txvector));
677
678         /* A couple calculations now, saves many cycles later. */
679         setup_rx_ring(sp->rx_desc, SEEQ_RX_BUFFERS);
680         setup_tx_ring(sp->tx_desc, SEEQ_TX_BUFFERS);
681
682         /* Setup PIO and DMA transfer timing */
683         sp->hregs->pconfig = 0x161;
684         sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
685                              HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
686
687         /* Reset the chip. */
688         hpc3_eth_reset(sp->hregs);
689
690         sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
691         if (sp->is_edlc)
692                 sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
693                               SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
694                               SEEQ_CTRL_ENCARR;
695
696         dev->open               = sgiseeq_open;
697         dev->stop               = sgiseeq_close;
698         dev->hard_start_xmit    = sgiseeq_start_xmit;
699         dev->tx_timeout         = timeout;
700         dev->watchdog_timeo     = (200 * HZ) / 1000;
701         dev->get_stats          = sgiseeq_get_stats;
702         dev->set_multicast_list = sgiseeq_set_multicast;
703         dev->set_mac_address    = sgiseeq_set_mac_address;
704         dev->irq                = irq;
705
706         if (register_netdev(dev)) {
707                 printk(KERN_ERR "Sgiseeq: Cannot register net device, "
708                        "aborting.\n");
709                 err = -ENODEV;
710                 goto err_out_free_page;
711         }
712
713         printk(KERN_INFO "%s: %s ", dev->name, sgiseeqstr);
714         for (i = 0; i < 6; i++)
715                 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
716
717         sp->next_module = root_sgiseeq_dev;
718         root_sgiseeq_dev = dev;
719
720         return 0;
721
722 err_out_free_page:
723         free_page((unsigned long) sp->srings);
724 err_out_free_dev:
725         kfree(dev);
726
727 err_out:
728         return err;
729 }
730
731 static int __init sgiseeq_probe(void)
732 {
733         /* On board adapter on 1st HPC is always present */
734         return sgiseeq_init(hpc3c0, SGI_ENET_IRQ);
735 }
736
737 static void __exit sgiseeq_exit(void)
738 {
739         struct net_device *next, *dev;
740         struct sgiseeq_private *sp;
741
742         for (dev = root_sgiseeq_dev; dev; dev = next) {
743                 sp = (struct sgiseeq_private *) netdev_priv(dev);
744                 next = sp->next_module;
745                 unregister_netdev(dev);
746                 free_page((unsigned long) sp->srings);
747                 free_netdev(dev);
748         }
749 }
750
751 module_init(sgiseeq_probe);
752 module_exit(sgiseeq_exit);
753
754 MODULE_DESCRIPTION("SGI Seeq 8003 driver");
755 MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
756 MODULE_LICENSE("GPL");