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