Merge /pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.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
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_reserve(skb, 2);
322                                 skb_put(skb, len);
323
324                                 /* Copy out of kseg1 to avoid silly cache flush. */
325                                 eth_copy_and_sum(skb, pkt_pointer + 2, len, 0);
326                                 skb->protocol = eth_type_trans(skb, dev);
327
328                                 /* We don't want to receive our own packets */
329                                 if (memcmp(eth_hdr(skb)->h_source, dev->dev_addr, ETH_ALEN)) {
330                                         netif_rx(skb);
331                                         dev->last_rx = jiffies;
332                                         sp->stats.rx_packets++;
333                                         sp->stats.rx_bytes += len;
334                                 } else {
335                                         /* Silently drop my own packets */
336                                         dev_kfree_skb_irq(skb);
337                                 }
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) CKSEG1ADDR(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)
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->reset = HPC3_ERST_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         unsigned int irq = dev->irq;
481
482         netif_stop_queue(dev);
483
484         /* Shutdown the Seeq. */
485         reset_hpc3_and_seeq(sp->hregs, sregs);
486         free_irq(irq, dev);
487
488         return 0;
489 }
490
491 static inline int sgiseeq_reset(struct net_device *dev)
492 {
493         struct sgiseeq_private *sp = netdev_priv(dev);
494         struct sgiseeq_regs *sregs = sp->sregs;
495         int err;
496
497         err = init_seeq(dev, sp, sregs);
498         if (err)
499                 return err;
500
501         dev->trans_start = jiffies;
502         netif_wake_queue(dev);
503
504         return 0;
505 }
506
507 static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
508 {
509         struct sgiseeq_private *sp = netdev_priv(dev);
510         struct hpc3_ethregs *hregs = sp->hregs;
511         unsigned long flags;
512         struct sgiseeq_tx_desc *td;
513         int skblen, len, entry;
514
515         spin_lock_irqsave(&sp->tx_lock, flags);
516
517         /* Setup... */
518         skblen = skb->len;
519         len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
520         sp->stats.tx_bytes += len;
521         entry = sp->tx_new;
522         td = &sp->tx_desc[entry];
523
524         /* Create entry.  There are so many races with adding a new
525          * descriptor to the chain:
526          * 1) Assume that the HPC is off processing a DMA chain while
527          *    we are changing all of the following.
528          * 2) Do no allow the HPC to look at a new descriptor until
529          *    we have completely set up it's state.  This means, do
530          *    not clear HPCDMA_EOX in the current last descritptor
531          *    until the one we are adding looks consistent and could
532          *    be processes right now.
533          * 3) The tx interrupt code must notice when we've added a new
534          *    entry and the HPC got to the end of the chain before we
535          *    added this new entry and restarted it.
536          */
537         skb_copy_from_linear_data(skb, (char *)(long)td->buf_vaddr, skblen);
538         if (len != skblen)
539                 memset((char *)(long)td->buf_vaddr + skb->len, 0, len-skblen);
540         td->tdma.cntinfo = (len & HPCDMA_BCNT) |
541                            HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
542         if (sp->tx_old != sp->tx_new) {
543                 struct sgiseeq_tx_desc *backend;
544
545                 backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
546                 backend->tdma.cntinfo &= ~HPCDMA_EOX;
547         }
548         sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
549
550         /* Maybe kick the HPC back into motion. */
551         if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
552                 kick_tx(&sp->tx_desc[sp->tx_old], hregs);
553
554         dev->trans_start = jiffies;
555         dev_kfree_skb(skb);
556
557         if (!TX_BUFFS_AVAIL(sp))
558                 netif_stop_queue(dev);
559         spin_unlock_irqrestore(&sp->tx_lock, flags);
560
561         return 0;
562 }
563
564 static void timeout(struct net_device *dev)
565 {
566         printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
567         sgiseeq_reset(dev);
568
569         dev->trans_start = jiffies;
570         netif_wake_queue(dev);
571 }
572
573 static struct net_device_stats *sgiseeq_get_stats(struct net_device *dev)
574 {
575         struct sgiseeq_private *sp = netdev_priv(dev);
576
577         return &sp->stats;
578 }
579
580 static void sgiseeq_set_multicast(struct net_device *dev)
581 {
582         struct sgiseeq_private *sp = (struct sgiseeq_private *) dev->priv;
583         unsigned char oldmode = sp->mode;
584
585         if(dev->flags & IFF_PROMISC)
586                 sp->mode = SEEQ_RCMD_RANY;
587         else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count)
588                 sp->mode = SEEQ_RCMD_RBMCAST;
589         else
590                 sp->mode = SEEQ_RCMD_RBCAST;
591
592         /* XXX I know this sucks, but is there a better way to reprogram
593          * XXX the receiver? At least, this shouldn't happen too often.
594          */
595
596         if (oldmode != sp->mode)
597                 sgiseeq_reset(dev);
598 }
599
600 static inline void setup_tx_ring(struct sgiseeq_tx_desc *buf, int nbufs)
601 {
602         int i = 0;
603
604         while (i < (nbufs - 1)) {
605                 buf[i].tdma.pnext = CPHYSADDR(buf + i + 1);
606                 buf[i].tdma.pbuf = 0;
607                 i++;
608         }
609         buf[i].tdma.pnext = CPHYSADDR(buf);
610 }
611
612 static inline void setup_rx_ring(struct sgiseeq_rx_desc *buf, int nbufs)
613 {
614         int i = 0;
615
616         while (i < (nbufs - 1)) {
617                 buf[i].rdma.pnext = CPHYSADDR(buf + i + 1);
618                 buf[i].rdma.pbuf = 0;
619                 i++;
620         }
621         buf[i].rdma.pbuf = 0;
622         buf[i].rdma.pnext = CPHYSADDR(buf);
623 }
624
625 #define ALIGNED(x)  ((((unsigned long)(x)) + 0xf) & ~(0xf))
626
627 static int sgiseeq_init(struct hpc3_regs* hpcregs, int irq, int has_eeprom)
628 {
629         struct sgiseeq_init_block *sr;
630         struct sgiseeq_private *sp;
631         struct net_device *dev;
632         int err, i;
633
634         dev = alloc_etherdev(sizeof (struct sgiseeq_private));
635         if (!dev) {
636                 printk(KERN_ERR "Sgiseeq: Etherdev alloc failed, aborting.\n");
637                 err = -ENOMEM;
638                 goto err_out;
639         }
640         sp = netdev_priv(dev);
641
642         /* Make private data page aligned */
643         sr = (struct sgiseeq_init_block *) get_zeroed_page(GFP_KERNEL);
644         if (!sr) {
645                 printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
646                 err = -ENOMEM;
647                 goto err_out_free_dev;
648         }
649         sp->srings = sr;
650
651 #define EADDR_NVOFS     250
652         for (i = 0; i < 3; i++) {
653                 unsigned short tmp = has_eeprom ?
654                         ip22_eeprom_read(&hpcregs->eeprom, EADDR_NVOFS / 2+i) :
655                         ip22_nvram_read(EADDR_NVOFS / 2+i);
656
657                 dev->dev_addr[2 * i]     = tmp >> 8;
658                 dev->dev_addr[2 * i + 1] = tmp & 0xff;
659         }
660
661 #ifdef DEBUG
662         gpriv = sp;
663         gdev = dev;
664 #endif
665         sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
666         sp->hregs = &hpcregs->ethregs;
667         sp->name = sgiseeqstr;
668         sp->mode = SEEQ_RCMD_RBCAST;
669
670         sp->rx_desc = (struct sgiseeq_rx_desc *)
671                       CKSEG1ADDR(ALIGNED(&sp->srings->rxvector[0]));
672         dma_cache_wback_inv((unsigned long)&sp->srings->rxvector,
673                             sizeof(sp->srings->rxvector));
674         sp->tx_desc = (struct sgiseeq_tx_desc *)
675                       CKSEG1ADDR(ALIGNED(&sp->srings->txvector[0]));
676         dma_cache_wback_inv((unsigned long)&sp->srings->txvector,
677                             sizeof(sp->srings->txvector));
678
679         /* A couple calculations now, saves many cycles later. */
680         setup_rx_ring(sp->rx_desc, SEEQ_RX_BUFFERS);
681         setup_tx_ring(sp->tx_desc, SEEQ_TX_BUFFERS);
682
683         /* Setup PIO and DMA transfer timing */
684         sp->hregs->pconfig = 0x161;
685         sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
686                              HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
687
688         /* Setup PIO and DMA transfer timing */
689         sp->hregs->pconfig = 0x161;
690         sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
691                              HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
692
693         /* Reset the chip. */
694         hpc3_eth_reset(sp->hregs);
695
696         sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
697         if (sp->is_edlc)
698                 sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
699                               SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
700                               SEEQ_CTRL_ENCARR;
701
702         dev->open               = sgiseeq_open;
703         dev->stop               = sgiseeq_close;
704         dev->hard_start_xmit    = sgiseeq_start_xmit;
705         dev->tx_timeout         = timeout;
706         dev->watchdog_timeo     = (200 * HZ) / 1000;
707         dev->get_stats          = sgiseeq_get_stats;
708         dev->set_multicast_list = sgiseeq_set_multicast;
709         dev->set_mac_address    = sgiseeq_set_mac_address;
710         dev->irq                = irq;
711
712         if (register_netdev(dev)) {
713                 printk(KERN_ERR "Sgiseeq: Cannot register net device, "
714                        "aborting.\n");
715                 err = -ENODEV;
716                 goto err_out_free_page;
717         }
718
719         printk(KERN_INFO "%s: %s ", dev->name, sgiseeqstr);
720         for (i = 0; i < 6; i++)
721                 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
722
723         sp->next_module = root_sgiseeq_dev;
724         root_sgiseeq_dev = dev;
725
726         return 0;
727
728 err_out_free_page:
729         free_page((unsigned long) sp->srings);
730 err_out_free_dev:
731         kfree(dev);
732
733 err_out:
734         return err;
735 }
736
737 static int __init sgiseeq_probe(void)
738 {
739         unsigned int tmp, ret1, ret2 = 0;
740
741         /* On board adapter on 1st HPC is always present */
742         ret1 = sgiseeq_init(hpc3c0, SGI_ENET_IRQ, 0);
743         /* Let's see if second HPC is there */
744         if (!(ip22_is_fullhouse()) &&
745             get_dbe(tmp, (unsigned int *)&hpc3c1->pbdma[1]) == 0) {
746                 sgimc->giopar |= SGIMC_GIOPAR_MASTEREXP1 |
747                                  SGIMC_GIOPAR_EXP164 |
748                                  SGIMC_GIOPAR_HPC264;
749                 hpc3c1->pbus_piocfg[0][0] = 0x3ffff;
750                 /* interrupt/config register on Challenge S Mezz board */
751                 hpc3c1->pbus_extregs[0][0] = 0x30;
752                 ret2 = sgiseeq_init(hpc3c1, SGI_GIO_0_IRQ, 1);
753         }
754
755         return (ret1 & ret2) ? ret1 : 0;
756 }
757
758 static void __exit sgiseeq_exit(void)
759 {
760         struct net_device *next, *dev;
761         struct sgiseeq_private *sp;
762
763         for (dev = root_sgiseeq_dev; dev; dev = next) {
764                 sp = (struct sgiseeq_private *) netdev_priv(dev);
765                 next = sp->next_module;
766                 unregister_netdev(dev);
767                 free_page((unsigned long) sp->srings);
768                 free_netdev(dev);
769         }
770 }
771
772 module_init(sgiseeq_probe);
773 module_exit(sgiseeq_exit);
774
775 MODULE_DESCRIPTION("SGI Seeq 8003 driver");
776 MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
777 MODULE_LICENSE("GPL");