treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / ethernet / seeq / sgiseeq.c
blob8507ff2420143a9a6bff00336b411210ae20a22b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * sgiseeq.c: Seeq8003 ethernet driver for SGI machines.
5 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
6 */
8 #undef DEBUG
10 #include <linux/dma-mapping.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/interrupt.h>
17 #include <linux/string.h>
18 #include <linux/delay.h>
19 #include <linux/netdevice.h>
20 #include <linux/platform_device.h>
21 #include <linux/etherdevice.h>
22 #include <linux/skbuff.h>
24 #include <asm/sgi/hpc3.h>
25 #include <asm/sgi/ip22.h>
26 #include <asm/sgi/seeq.h>
28 #include "sgiseeq.h"
30 static char *sgiseeqstr = "SGI Seeq8003";
33 * If you want speed, you do something silly, it always has worked for me. So,
34 * with that in mind, I've decided to make this driver look completely like a
35 * stupid Lance from a driver architecture perspective. Only difference is that
36 * here our "ring buffer" looks and acts like a real Lance one does but is
37 * laid out like how the HPC DMA and the Seeq want it to. You'd be surprised
38 * how a stupid idea like this can pay off in performance, not to mention
39 * making this driver 2,000 times easier to write. ;-)
42 /* Tune these if we tend to run out often etc. */
43 #define SEEQ_RX_BUFFERS 16
44 #define SEEQ_TX_BUFFERS 16
46 #define PKT_BUF_SZ 1584
48 #define NEXT_RX(i) (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
49 #define NEXT_TX(i) (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
50 #define PREV_RX(i) (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
51 #define PREV_TX(i) (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
53 #define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
54 sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
55 sp->tx_old - sp->tx_new - 1)
57 #define VIRT_TO_DMA(sp, v) ((sp)->srings_dma + \
58 (dma_addr_t)((unsigned long)(v) - \
59 (unsigned long)((sp)->rx_desc)))
61 /* Copy frames shorter than rx_copybreak, otherwise pass on up in
62 * a full sized sk_buff. Value of 100 stolen from tulip.c (!alpha).
64 static int rx_copybreak = 100;
66 #define PAD_SIZE (128 - sizeof(struct hpc_dma_desc) - sizeof(void *))
68 struct sgiseeq_rx_desc {
69 volatile struct hpc_dma_desc rdma;
70 u8 padding[PAD_SIZE];
71 struct sk_buff *skb;
74 struct sgiseeq_tx_desc {
75 volatile struct hpc_dma_desc tdma;
76 u8 padding[PAD_SIZE];
77 struct sk_buff *skb;
81 * Warning: This structure is laid out in a certain way because HPC dma
82 * descriptors must be 8-byte aligned. So don't touch this without
83 * some care.
85 struct sgiseeq_init_block { /* Note the name ;-) */
86 struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
87 struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
90 struct sgiseeq_private {
91 struct sgiseeq_init_block *srings;
92 dma_addr_t srings_dma;
94 /* Ptrs to the descriptors in uncached space. */
95 struct sgiseeq_rx_desc *rx_desc;
96 struct sgiseeq_tx_desc *tx_desc;
98 char *name;
99 struct hpc3_ethregs *hregs;
100 struct sgiseeq_regs *sregs;
102 /* Ring entry counters. */
103 unsigned int rx_new, tx_new;
104 unsigned int rx_old, tx_old;
106 int is_edlc;
107 unsigned char control;
108 unsigned char mode;
110 spinlock_t tx_lock;
113 static inline void dma_sync_desc_cpu(struct net_device *dev, void *addr)
115 dma_cache_sync(dev->dev.parent, addr, sizeof(struct sgiseeq_rx_desc),
116 DMA_FROM_DEVICE);
119 static inline void dma_sync_desc_dev(struct net_device *dev, void *addr)
121 dma_cache_sync(dev->dev.parent, addr, sizeof(struct sgiseeq_rx_desc),
122 DMA_TO_DEVICE);
125 static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
127 hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
128 udelay(20);
129 hregs->reset = 0;
132 static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
133 struct sgiseeq_regs *sregs)
135 hregs->rx_ctrl = hregs->tx_ctrl = 0;
136 hpc3_eth_reset(hregs);
139 #define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
140 SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
142 static inline void seeq_go(struct sgiseeq_private *sp,
143 struct hpc3_ethregs *hregs,
144 struct sgiseeq_regs *sregs)
146 sregs->rstat = sp->mode | RSTAT_GO_BITS;
147 hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
150 static inline void __sgiseeq_set_mac_address(struct net_device *dev)
152 struct sgiseeq_private *sp = netdev_priv(dev);
153 struct sgiseeq_regs *sregs = sp->sregs;
154 int i;
156 sregs->tstat = SEEQ_TCMD_RB0;
157 for (i = 0; i < 6; i++)
158 sregs->rw.eth_addr[i] = dev->dev_addr[i];
161 static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
163 struct sgiseeq_private *sp = netdev_priv(dev);
164 struct sockaddr *sa = addr;
166 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
168 spin_lock_irq(&sp->tx_lock);
169 __sgiseeq_set_mac_address(dev);
170 spin_unlock_irq(&sp->tx_lock);
172 return 0;
175 #define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
176 #define RCNTCFG_INIT (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
177 #define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
179 static int seeq_init_ring(struct net_device *dev)
181 struct sgiseeq_private *sp = netdev_priv(dev);
182 int i;
184 netif_stop_queue(dev);
185 sp->rx_new = sp->tx_new = 0;
186 sp->rx_old = sp->tx_old = 0;
188 __sgiseeq_set_mac_address(dev);
190 /* Setup tx ring. */
191 for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
192 sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
193 dma_sync_desc_dev(dev, &sp->tx_desc[i]);
196 /* And now the rx ring. */
197 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
198 if (!sp->rx_desc[i].skb) {
199 dma_addr_t dma_addr;
200 struct sk_buff *skb = netdev_alloc_skb(dev, PKT_BUF_SZ);
202 if (skb == NULL)
203 return -ENOMEM;
204 skb_reserve(skb, 2);
205 dma_addr = dma_map_single(dev->dev.parent,
206 skb->data - 2,
207 PKT_BUF_SZ, DMA_FROM_DEVICE);
208 sp->rx_desc[i].skb = skb;
209 sp->rx_desc[i].rdma.pbuf = dma_addr;
211 sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
212 dma_sync_desc_dev(dev, &sp->rx_desc[i]);
214 sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
215 dma_sync_desc_dev(dev, &sp->rx_desc[i - 1]);
216 return 0;
219 static void seeq_purge_ring(struct net_device *dev)
221 struct sgiseeq_private *sp = netdev_priv(dev);
222 int i;
224 /* clear tx ring. */
225 for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
226 if (sp->tx_desc[i].skb) {
227 dev_kfree_skb(sp->tx_desc[i].skb);
228 sp->tx_desc[i].skb = NULL;
232 /* And now the rx ring. */
233 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
234 if (sp->rx_desc[i].skb) {
235 dev_kfree_skb(sp->rx_desc[i].skb);
236 sp->rx_desc[i].skb = NULL;
241 #ifdef DEBUG
242 static struct sgiseeq_private *gpriv;
243 static struct net_device *gdev;
245 static void sgiseeq_dump_rings(void)
247 static int once;
248 struct sgiseeq_rx_desc *r = gpriv->rx_desc;
249 struct sgiseeq_tx_desc *t = gpriv->tx_desc;
250 struct hpc3_ethregs *hregs = gpriv->hregs;
251 int i;
253 if (once)
254 return;
255 once++;
256 printk("RING DUMP:\n");
257 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
258 printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
259 i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
260 r[i].rdma.pnext);
261 i += 1;
262 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
263 i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
264 r[i].rdma.pnext);
266 for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
267 printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
268 i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
269 t[i].tdma.pnext);
270 i += 1;
271 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
272 i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
273 t[i].tdma.pnext);
275 printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
276 gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
277 printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
278 hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
279 printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
280 hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
282 #endif
284 #define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
285 #define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
287 static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
288 struct sgiseeq_regs *sregs)
290 struct hpc3_ethregs *hregs = sp->hregs;
291 int err;
293 reset_hpc3_and_seeq(hregs, sregs);
294 err = seeq_init_ring(dev);
295 if (err)
296 return err;
298 /* Setup to field the proper interrupt types. */
299 if (sp->is_edlc) {
300 sregs->tstat = TSTAT_INIT_EDLC;
301 sregs->rw.wregs.control = sp->control;
302 sregs->rw.wregs.frame_gap = 0;
303 } else {
304 sregs->tstat = TSTAT_INIT_SEEQ;
307 hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc);
308 hregs->tx_ndptr = VIRT_TO_DMA(sp, sp->tx_desc);
310 seeq_go(sp, hregs, sregs);
311 return 0;
314 static void record_rx_errors(struct net_device *dev, unsigned char status)
316 if (status & SEEQ_RSTAT_OVERF ||
317 status & SEEQ_RSTAT_SFRAME)
318 dev->stats.rx_over_errors++;
319 if (status & SEEQ_RSTAT_CERROR)
320 dev->stats.rx_crc_errors++;
321 if (status & SEEQ_RSTAT_DERROR)
322 dev->stats.rx_frame_errors++;
323 if (status & SEEQ_RSTAT_REOF)
324 dev->stats.rx_errors++;
327 static inline void rx_maybe_restart(struct sgiseeq_private *sp,
328 struct hpc3_ethregs *hregs,
329 struct sgiseeq_regs *sregs)
331 if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
332 hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc + sp->rx_new);
333 seeq_go(sp, hregs, sregs);
337 static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
338 struct hpc3_ethregs *hregs,
339 struct sgiseeq_regs *sregs)
341 struct sgiseeq_rx_desc *rd;
342 struct sk_buff *skb = NULL;
343 struct sk_buff *newskb;
344 unsigned char pkt_status;
345 int len = 0;
346 unsigned int orig_end = PREV_RX(sp->rx_new);
348 /* Service every received packet. */
349 rd = &sp->rx_desc[sp->rx_new];
350 dma_sync_desc_cpu(dev, rd);
351 while (!(rd->rdma.cntinfo & HPCDMA_OWN)) {
352 len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
353 dma_unmap_single(dev->dev.parent, rd->rdma.pbuf,
354 PKT_BUF_SZ, DMA_FROM_DEVICE);
355 pkt_status = rd->skb->data[len];
356 if (pkt_status & SEEQ_RSTAT_FIG) {
357 /* Packet is OK. */
358 /* We don't want to receive our own packets */
359 if (!ether_addr_equal(rd->skb->data + 6, dev->dev_addr)) {
360 if (len > rx_copybreak) {
361 skb = rd->skb;
362 newskb = netdev_alloc_skb(dev, PKT_BUF_SZ);
363 if (!newskb) {
364 newskb = skb;
365 skb = NULL;
366 goto memory_squeeze;
368 skb_reserve(newskb, 2);
369 } else {
370 skb = netdev_alloc_skb_ip_align(dev, len);
371 if (skb)
372 skb_copy_to_linear_data(skb, rd->skb->data, len);
374 newskb = rd->skb;
376 memory_squeeze:
377 if (skb) {
378 skb_put(skb, len);
379 skb->protocol = eth_type_trans(skb, dev);
380 netif_rx(skb);
381 dev->stats.rx_packets++;
382 dev->stats.rx_bytes += len;
383 } else {
384 dev->stats.rx_dropped++;
386 } else {
387 /* Silently drop my own packets */
388 newskb = rd->skb;
390 } else {
391 record_rx_errors(dev, pkt_status);
392 newskb = rd->skb;
394 rd->skb = newskb;
395 rd->rdma.pbuf = dma_map_single(dev->dev.parent,
396 newskb->data - 2,
397 PKT_BUF_SZ, DMA_FROM_DEVICE);
399 /* Return the entry to the ring pool. */
400 rd->rdma.cntinfo = RCNTINFO_INIT;
401 sp->rx_new = NEXT_RX(sp->rx_new);
402 dma_sync_desc_dev(dev, rd);
403 rd = &sp->rx_desc[sp->rx_new];
404 dma_sync_desc_cpu(dev, rd);
406 dma_sync_desc_cpu(dev, &sp->rx_desc[orig_end]);
407 sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
408 dma_sync_desc_dev(dev, &sp->rx_desc[orig_end]);
409 dma_sync_desc_cpu(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
410 sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
411 dma_sync_desc_dev(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
412 rx_maybe_restart(sp, hregs, sregs);
415 static inline void tx_maybe_reset_collisions(struct sgiseeq_private *sp,
416 struct sgiseeq_regs *sregs)
418 if (sp->is_edlc) {
419 sregs->rw.wregs.control = sp->control & ~(SEEQ_CTRL_XCNT);
420 sregs->rw.wregs.control = sp->control;
424 static inline void kick_tx(struct net_device *dev,
425 struct sgiseeq_private *sp,
426 struct hpc3_ethregs *hregs)
428 struct sgiseeq_tx_desc *td;
429 int i = sp->tx_old;
431 /* If the HPC aint doin nothin, and there are more packets
432 * with ETXD cleared and XIU set we must make very certain
433 * that we restart the HPC else we risk locking up the
434 * adapter. The following code is only safe iff the HPCDMA
435 * is not active!
437 td = &sp->tx_desc[i];
438 dma_sync_desc_cpu(dev, td);
439 while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
440 (HPCDMA_XIU | HPCDMA_ETXD)) {
441 i = NEXT_TX(i);
442 td = &sp->tx_desc[i];
443 dma_sync_desc_cpu(dev, td);
445 if (td->tdma.cntinfo & HPCDMA_XIU) {
446 hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
447 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
451 static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
452 struct hpc3_ethregs *hregs,
453 struct sgiseeq_regs *sregs)
455 struct sgiseeq_tx_desc *td;
456 unsigned long status = hregs->tx_ctrl;
457 int j;
459 tx_maybe_reset_collisions(sp, sregs);
461 if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
462 /* Oops, HPC detected some sort of error. */
463 if (status & SEEQ_TSTAT_R16)
464 dev->stats.tx_aborted_errors++;
465 if (status & SEEQ_TSTAT_UFLOW)
466 dev->stats.tx_fifo_errors++;
467 if (status & SEEQ_TSTAT_LCLS)
468 dev->stats.collisions++;
471 /* Ack 'em... */
472 for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
473 td = &sp->tx_desc[j];
475 dma_sync_desc_cpu(dev, td);
476 if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
477 break;
478 if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
479 if (!(status & HPC3_ETXCTRL_ACTIVE)) {
480 hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
481 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
483 break;
485 dev->stats.tx_packets++;
486 sp->tx_old = NEXT_TX(sp->tx_old);
487 td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
488 td->tdma.cntinfo |= HPCDMA_EOX;
489 if (td->skb) {
490 dev_kfree_skb_any(td->skb);
491 td->skb = NULL;
493 dma_sync_desc_dev(dev, td);
497 static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id)
499 struct net_device *dev = (struct net_device *) dev_id;
500 struct sgiseeq_private *sp = netdev_priv(dev);
501 struct hpc3_ethregs *hregs = sp->hregs;
502 struct sgiseeq_regs *sregs = sp->sregs;
504 spin_lock(&sp->tx_lock);
506 /* Ack the IRQ and set software state. */
507 hregs->reset = HPC3_ERST_CLRIRQ;
509 /* Always check for received packets. */
510 sgiseeq_rx(dev, sp, hregs, sregs);
512 /* Only check for tx acks if we have something queued. */
513 if (sp->tx_old != sp->tx_new)
514 sgiseeq_tx(dev, sp, hregs, sregs);
516 if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
517 netif_wake_queue(dev);
519 spin_unlock(&sp->tx_lock);
521 return IRQ_HANDLED;
524 static int sgiseeq_open(struct net_device *dev)
526 struct sgiseeq_private *sp = netdev_priv(dev);
527 struct sgiseeq_regs *sregs = sp->sregs;
528 unsigned int irq = dev->irq;
529 int err;
531 if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
532 printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
533 return -EAGAIN;
536 err = init_seeq(dev, sp, sregs);
537 if (err)
538 goto out_free_irq;
540 netif_start_queue(dev);
542 return 0;
544 out_free_irq:
545 free_irq(irq, dev);
547 return err;
550 static int sgiseeq_close(struct net_device *dev)
552 struct sgiseeq_private *sp = netdev_priv(dev);
553 struct sgiseeq_regs *sregs = sp->sregs;
554 unsigned int irq = dev->irq;
556 netif_stop_queue(dev);
558 /* Shutdown the Seeq. */
559 reset_hpc3_and_seeq(sp->hregs, sregs);
560 free_irq(irq, dev);
561 seeq_purge_ring(dev);
563 return 0;
566 static inline int sgiseeq_reset(struct net_device *dev)
568 struct sgiseeq_private *sp = netdev_priv(dev);
569 struct sgiseeq_regs *sregs = sp->sregs;
570 int err;
572 err = init_seeq(dev, sp, sregs);
573 if (err)
574 return err;
576 netif_trans_update(dev); /* prevent tx timeout */
577 netif_wake_queue(dev);
579 return 0;
582 static netdev_tx_t
583 sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
585 struct sgiseeq_private *sp = netdev_priv(dev);
586 struct hpc3_ethregs *hregs = sp->hregs;
587 unsigned long flags;
588 struct sgiseeq_tx_desc *td;
589 int len, entry;
591 spin_lock_irqsave(&sp->tx_lock, flags);
593 /* Setup... */
594 len = skb->len;
595 if (len < ETH_ZLEN) {
596 if (skb_padto(skb, ETH_ZLEN)) {
597 spin_unlock_irqrestore(&sp->tx_lock, flags);
598 return NETDEV_TX_OK;
600 len = ETH_ZLEN;
603 dev->stats.tx_bytes += len;
604 entry = sp->tx_new;
605 td = &sp->tx_desc[entry];
606 dma_sync_desc_cpu(dev, td);
608 /* Create entry. There are so many races with adding a new
609 * descriptor to the chain:
610 * 1) Assume that the HPC is off processing a DMA chain while
611 * we are changing all of the following.
612 * 2) Do no allow the HPC to look at a new descriptor until
613 * we have completely set up it's state. This means, do
614 * not clear HPCDMA_EOX in the current last descritptor
615 * until the one we are adding looks consistent and could
616 * be processes right now.
617 * 3) The tx interrupt code must notice when we've added a new
618 * entry and the HPC got to the end of the chain before we
619 * added this new entry and restarted it.
621 td->skb = skb;
622 td->tdma.pbuf = dma_map_single(dev->dev.parent, skb->data,
623 len, DMA_TO_DEVICE);
624 td->tdma.cntinfo = (len & HPCDMA_BCNT) |
625 HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
626 dma_sync_desc_dev(dev, td);
627 if (sp->tx_old != sp->tx_new) {
628 struct sgiseeq_tx_desc *backend;
630 backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
631 dma_sync_desc_cpu(dev, backend);
632 backend->tdma.cntinfo &= ~HPCDMA_EOX;
633 dma_sync_desc_dev(dev, backend);
635 sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
637 /* Maybe kick the HPC back into motion. */
638 if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
639 kick_tx(dev, sp, hregs);
641 if (!TX_BUFFS_AVAIL(sp))
642 netif_stop_queue(dev);
643 spin_unlock_irqrestore(&sp->tx_lock, flags);
645 return NETDEV_TX_OK;
648 static void timeout(struct net_device *dev, unsigned int txqueue)
650 printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
651 sgiseeq_reset(dev);
653 netif_trans_update(dev); /* prevent tx timeout */
654 netif_wake_queue(dev);
657 static void sgiseeq_set_multicast(struct net_device *dev)
659 struct sgiseeq_private *sp = netdev_priv(dev);
660 unsigned char oldmode = sp->mode;
662 if(dev->flags & IFF_PROMISC)
663 sp->mode = SEEQ_RCMD_RANY;
664 else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
665 sp->mode = SEEQ_RCMD_RBMCAST;
666 else
667 sp->mode = SEEQ_RCMD_RBCAST;
669 /* XXX I know this sucks, but is there a better way to reprogram
670 * XXX the receiver? At least, this shouldn't happen too often.
673 if (oldmode != sp->mode)
674 sgiseeq_reset(dev);
677 static inline void setup_tx_ring(struct net_device *dev,
678 struct sgiseeq_tx_desc *buf,
679 int nbufs)
681 struct sgiseeq_private *sp = netdev_priv(dev);
682 int i = 0;
684 while (i < (nbufs - 1)) {
685 buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
686 buf[i].tdma.pbuf = 0;
687 dma_sync_desc_dev(dev, &buf[i]);
688 i++;
690 buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf);
691 dma_sync_desc_dev(dev, &buf[i]);
694 static inline void setup_rx_ring(struct net_device *dev,
695 struct sgiseeq_rx_desc *buf,
696 int nbufs)
698 struct sgiseeq_private *sp = netdev_priv(dev);
699 int i = 0;
701 while (i < (nbufs - 1)) {
702 buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
703 buf[i].rdma.pbuf = 0;
704 dma_sync_desc_dev(dev, &buf[i]);
705 i++;
707 buf[i].rdma.pbuf = 0;
708 buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf);
709 dma_sync_desc_dev(dev, &buf[i]);
712 static const struct net_device_ops sgiseeq_netdev_ops = {
713 .ndo_open = sgiseeq_open,
714 .ndo_stop = sgiseeq_close,
715 .ndo_start_xmit = sgiseeq_start_xmit,
716 .ndo_tx_timeout = timeout,
717 .ndo_set_rx_mode = sgiseeq_set_multicast,
718 .ndo_set_mac_address = sgiseeq_set_mac_address,
719 .ndo_validate_addr = eth_validate_addr,
722 static int sgiseeq_probe(struct platform_device *pdev)
724 struct sgiseeq_platform_data *pd = dev_get_platdata(&pdev->dev);
725 struct hpc3_regs *hpcregs = pd->hpc;
726 struct sgiseeq_init_block *sr;
727 unsigned int irq = pd->irq;
728 struct sgiseeq_private *sp;
729 struct net_device *dev;
730 int err;
732 dev = alloc_etherdev(sizeof (struct sgiseeq_private));
733 if (!dev) {
734 err = -ENOMEM;
735 goto err_out;
738 platform_set_drvdata(pdev, dev);
739 SET_NETDEV_DEV(dev, &pdev->dev);
740 sp = netdev_priv(dev);
742 /* Make private data page aligned */
743 sr = dma_alloc_attrs(&pdev->dev, sizeof(*sp->srings), &sp->srings_dma,
744 GFP_KERNEL, DMA_ATTR_NON_CONSISTENT);
745 if (!sr) {
746 printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
747 err = -ENOMEM;
748 goto err_out_free_dev;
750 sp->srings = sr;
751 sp->rx_desc = sp->srings->rxvector;
752 sp->tx_desc = sp->srings->txvector;
753 spin_lock_init(&sp->tx_lock);
755 /* A couple calculations now, saves many cycles later. */
756 setup_rx_ring(dev, sp->rx_desc, SEEQ_RX_BUFFERS);
757 setup_tx_ring(dev, sp->tx_desc, SEEQ_TX_BUFFERS);
759 memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
761 #ifdef DEBUG
762 gpriv = sp;
763 gdev = dev;
764 #endif
765 sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
766 sp->hregs = &hpcregs->ethregs;
767 sp->name = sgiseeqstr;
768 sp->mode = SEEQ_RCMD_RBCAST;
770 /* Setup PIO and DMA transfer timing */
771 sp->hregs->pconfig = 0x161;
772 sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
773 HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
775 /* Setup PIO and DMA transfer timing */
776 sp->hregs->pconfig = 0x161;
777 sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
778 HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
780 /* Reset the chip. */
781 hpc3_eth_reset(sp->hregs);
783 sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
784 if (sp->is_edlc)
785 sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
786 SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
787 SEEQ_CTRL_ENCARR;
789 dev->netdev_ops = &sgiseeq_netdev_ops;
790 dev->watchdog_timeo = (200 * HZ) / 1000;
791 dev->irq = irq;
793 if (register_netdev(dev)) {
794 printk(KERN_ERR "Sgiseeq: Cannot register net device, "
795 "aborting.\n");
796 err = -ENODEV;
797 goto err_out_free_attrs;
800 printk(KERN_INFO "%s: %s %pM\n", dev->name, sgiseeqstr, dev->dev_addr);
802 return 0;
804 err_out_free_attrs:
805 dma_free_attrs(&pdev->dev, sizeof(*sp->srings), sp->srings,
806 sp->srings_dma, DMA_ATTR_NON_CONSISTENT);
807 err_out_free_dev:
808 free_netdev(dev);
810 err_out:
811 return err;
814 static int sgiseeq_remove(struct platform_device *pdev)
816 struct net_device *dev = platform_get_drvdata(pdev);
817 struct sgiseeq_private *sp = netdev_priv(dev);
819 unregister_netdev(dev);
820 dma_free_attrs(&pdev->dev, sizeof(*sp->srings), sp->srings,
821 sp->srings_dma, DMA_ATTR_NON_CONSISTENT);
822 free_netdev(dev);
824 return 0;
827 static struct platform_driver sgiseeq_driver = {
828 .probe = sgiseeq_probe,
829 .remove = sgiseeq_remove,
830 .driver = {
831 .name = "sgiseeq",
835 module_platform_driver(sgiseeq_driver);
837 MODULE_DESCRIPTION("SGI Seeq 8003 driver");
838 MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
839 MODULE_LICENSE("GPL");
840 MODULE_ALIAS("platform:sgiseeq");