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