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