Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

sunqe.c

Go to the documentation of this file.
00001 /* sunqe.c: Sparc QuadEthernet 10baseT SBUS card driver.
00002  *          Once again I am out to prove that every ethernet
00003  *          controller out there can be most efficiently programmed
00004  *          if you make it look like a LANCE.
00005  *
00006  * Copyright (C) 1996, 1999 David S. Miller (davem@redhat.com)
00007  */
00008 
00009 static char *version =
00010         "sunqe.c:v2.0 9/9/99 David S. Miller (davem@redhat.com)\n";
00011 
00012 #include <linux/module.h>
00013 
00014 #include <linux/kernel.h>
00015 #include <linux/sched.h>
00016 #include <linux/types.h>
00017 #include <linux/fcntl.h>
00018 #include <linux/interrupt.h>
00019 #include <linux/ptrace.h>
00020 #include <linux/ioport.h>
00021 #include <linux/in.h>
00022 #include <linux/malloc.h>
00023 #include <linux/string.h>
00024 #include <linux/delay.h>
00025 #include <linux/init.h>
00026 
00027 #include <asm/system.h>
00028 #include <asm/bitops.h>
00029 #include <asm/io.h>
00030 #include <asm/dma.h>
00031 #include <linux/errno.h>
00032 #include <asm/byteorder.h>
00033 
00034 #include <asm/idprom.h>
00035 #include <asm/sbus.h>
00036 #include <asm/openprom.h>
00037 #include <asm/oplib.h>
00038 #include <asm/auxio.h>
00039 #include <asm/pgtable.h>
00040 #include <asm/irq.h>
00041 
00042 #include <linux/netdevice.h>
00043 #include <linux/etherdevice.h>
00044 #include <linux/skbuff.h>
00045 
00046 #include "sunqe.h"
00047 
00048 #ifdef MODULE
00049 static struct sunqec *root_qec_dev = NULL;
00050 #endif
00051 
00052 static void qe_set_multicast(struct device *dev);
00053 
00054 #define QEC_RESET_TRIES 200
00055 
00056 static inline int qec_global_reset(struct qe_globreg *gregs)
00057 {
00058         int tries = QEC_RESET_TRIES;
00059 
00060         gregs->ctrl = GLOB_CTRL_RESET;
00061         while(--tries) {
00062                 if(gregs->ctrl & GLOB_CTRL_RESET) {
00063                         udelay(20);
00064                         continue;
00065                 }
00066                 break;
00067         }
00068         if(tries)
00069                 return 0;
00070         printk(KERN_ERR "QuadEther: AIEEE cannot reset the QEC!\n");
00071         return -1;
00072 }
00073 
00074 #define MACE_RESET_RETRIES 200
00075 #define QE_RESET_RETRIES   200
00076 
00077 static inline int qe_stop(struct sunqe *qep)
00078 {
00079         struct qe_creg *cregs = qep->qcregs;
00080         struct qe_mregs *mregs = qep->mregs;
00081         int tries;
00082 
00083         /* Reset the MACE, then the QEC channel. */
00084         mregs->bconfig = MREGS_BCONFIG_RESET;
00085         tries = MACE_RESET_RETRIES;
00086         while(--tries) {
00087                 if(mregs->bconfig & MREGS_BCONFIG_RESET) {
00088                         udelay(20);
00089                         continue;
00090                 }
00091                 break;
00092         }
00093         if(!tries) {
00094                 printk(KERN_ERR "QuadEther: AIEEE cannot reset the MACE!\n");
00095                 return -1;
00096         }
00097 
00098         cregs->ctrl = CREG_CTRL_RESET;
00099         tries = QE_RESET_RETRIES;
00100         while(--tries) {
00101                 if(cregs->ctrl & CREG_CTRL_RESET) {
00102                         udelay(20);
00103                         continue;
00104                 }
00105                 break;
00106         }
00107         if(!tries) {
00108                 printk(KERN_ERR "QuadEther: Cannot reset QE channel!\n");
00109                 return -1;
00110         }
00111         return 0;
00112 }
00113 
00114 static void qe_init_rings(struct sunqe *qep)
00115 {
00116         struct qe_init_block *qb = qep->qe_block;
00117         struct sunqe_buffers *qbufs = qep->buffers;
00118         __u32 qbufs_dvma = qep->buffers_dvma;
00119         int i;
00120 
00121         qep->rx_new = qep->rx_old = qep->tx_new = qep->tx_old = 0;
00122         memset(qb, 0, sizeof(struct qe_init_block));
00123         memset(qbufs, 0, sizeof(struct sunqe_buffers));
00124         for(i = 0; i < RX_RING_SIZE; i++) {
00125                 qb->qe_rxd[i].rx_addr = qbufs_dvma + qebuf_offset(rx_buf, i);
00126                 qb->qe_rxd[i].rx_flags =
00127                         (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
00128         }
00129 }
00130 
00131 static int qe_init(struct sunqe *qep, int from_irq)
00132 {
00133         struct sunqec *qecp = qep->parent;
00134         struct qe_creg *cregs = qep->qcregs;
00135         struct qe_mregs *mregs = qep->mregs;
00136         struct qe_globreg *gregs = qecp->gregs;
00137         unsigned char *e = &qep->dev->dev_addr[0];
00138         volatile unsigned char garbage;
00139         int i;
00140 
00141         /* Shut it up. */
00142         if(qe_stop(qep))
00143                 return -EAGAIN;
00144 
00145         /* Setup initial rx/tx init block pointers. */
00146         cregs->rxds = qep->qblock_dvma + qib_offset(qe_rxd, 0);
00147         cregs->txds = qep->qblock_dvma + qib_offset(qe_txd, 0);
00148 
00149         /* Enable/mask the various irq's. */
00150         cregs->rimask = 0;
00151         cregs->timask = 1;
00152 
00153         cregs->qmask = 0;
00154         cregs->mmask = CREG_MMASK_RXCOLL;
00155 
00156         /* Setup the FIFO pointers into QEC local memory. */
00157         cregs->rxwbufptr = cregs->rxrbufptr = qep->channel * gregs->msize;
00158         cregs->txwbufptr = cregs->txrbufptr = cregs->rxrbufptr + gregs->rsize;
00159 
00160         /* Clear the channel collision counter. */
00161         cregs->ccnt = 0;
00162 
00163         /* For 10baseT, inter frame space nor throttle seems to be necessary. */
00164         cregs->pipg = 0;
00165 
00166         /* Now dork with the AMD MACE. */
00167         mregs->phyconfig = MREGS_PHYCONFIG_AUTO;
00168         mregs->txfcntl = MREGS_TXFCNTL_AUTOPAD; /* Save us some tx work. */
00169         mregs->rxfcntl = 0;
00170 
00171         /* The QEC dma's the rx'd packets from local memory out to main memory,
00172          * and therefore it interrupts when the packet reception is "complete".
00173          * So don't listen for the MACE talking about it.
00174          */
00175         mregs->imask = (MREGS_IMASK_COLL | MREGS_IMASK_RXIRQ);
00176 
00177         mregs->bconfig = (MREGS_BCONFIG_BSWAP | MREGS_BCONFIG_64TS);
00178         mregs->fconfig = (MREGS_FCONFIG_TXF16 | MREGS_FCONFIG_RXF32 |
00179                           MREGS_FCONFIG_RFWU | MREGS_FCONFIG_TFWU);
00180 
00181         /* Only usable interface on QuadEther is twisted pair. */
00182         mregs->plsconfig = (MREGS_PLSCONFIG_TP);
00183 
00184         /* Tell MACE we are changing the ether address. */
00185         mregs->iaconfig = (MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_PARESET);
00186         while ((mregs->iaconfig & MREGS_IACONFIG_ACHNGE) != 0)
00187                 barrier();
00188         mregs->ethaddr = e[0];
00189         mregs->ethaddr = e[1];
00190         mregs->ethaddr = e[2];
00191         mregs->ethaddr = e[3];
00192         mregs->ethaddr = e[4];
00193         mregs->ethaddr = e[5];
00194 
00195         /* Clear out the address filter. */
00196         mregs->iaconfig = (MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET);
00197         while ((mregs->iaconfig & MREGS_IACONFIG_ACHNGE) != 0)
00198                 barrier();
00199         for(i = 0; i < 8; i++)
00200                 mregs->filter = 0;
00201 
00202         /* Address changes are now complete. */
00203         mregs->iaconfig = 0;
00204 
00205         qe_init_rings(qep);
00206 
00207         /* Wait a little bit for the link to come up... */
00208         mdelay(5);
00209         if(!(mregs->phyconfig & MREGS_PHYCONFIG_LTESTDIS)) {
00210                 int tries = 50;
00211 
00212                 while (tries--) {
00213                         mdelay(5);
00214                         barrier();
00215                         if((mregs->phyconfig & MREGS_PHYCONFIG_LSTAT) != 0)
00216                                 break;
00217                 }
00218                 if (tries == 0)
00219                         printk(KERN_NOTICE "%s: Warning, link state is down.\n", qep->dev->name);
00220         }
00221 
00222         /* Missed packet counter is cleared on a read. */
00223         garbage = mregs->mpcnt;
00224 
00225         /* Reload multicast information, this will enable the receiver
00226          * and transmitter.  But set the base mconfig value right now.
00227          */
00228         qe_set_multicast(qep->dev);
00229 
00230         /* QEC should now start to show interrupts. */
00231         return 0;
00232 }
00233 
00234 /* Grrr, certain error conditions completely lock up the AMD MACE,
00235  * so when we get these we _must_ reset the chip.
00236  */
00237 static int qe_is_bolixed(struct sunqe *qep, unsigned int qe_status)
00238 {
00239         struct device *dev = qep->dev;
00240         int mace_hwbug_workaround = 0;
00241 
00242         if(qe_status & CREG_STAT_EDEFER) {
00243                 printk(KERN_ERR "%s: Excessive transmit defers.\n", dev->name);
00244                 qep->net_stats.tx_errors++;
00245         }
00246 
00247         if(qe_status & CREG_STAT_CLOSS) {
00248                 printk(KERN_ERR "%s: Carrier lost, link down?\n", dev->name);
00249                 qep->net_stats.tx_errors++;
00250                 qep->net_stats.tx_carrier_errors++;
00251         }
00252 
00253         if(qe_status & CREG_STAT_ERETRIES) {
00254                 printk(KERN_ERR "%s: Excessive transmit retries (more than 16).\n", dev->name);
00255                 qep->net_stats.tx_errors++;
00256                 mace_hwbug_workaround = 1;
00257         }
00258 
00259         if(qe_status & CREG_STAT_LCOLL) {
00260                 printk(KERN_ERR "%s: Late transmit collision.\n", dev->name);
00261                 qep->net_stats.tx_errors++;
00262                 qep->net_stats.collisions++;
00263                 mace_hwbug_workaround = 1;
00264         }
00265 
00266         if(qe_status & CREG_STAT_FUFLOW) {
00267                 printk(KERN_ERR "%s: Transmit fifo underflow, driver bug.\n", dev->name);
00268                 qep->net_stats.tx_errors++;
00269                 mace_hwbug_workaround = 1;
00270         }
00271 
00272         if(qe_status & CREG_STAT_JERROR) {
00273                 printk(KERN_ERR "%s: Jabber error.\n", dev->name);
00274         }
00275 
00276         if(qe_status & CREG_STAT_BERROR) {
00277                 printk(KERN_ERR "%s: Babble error.\n", dev->name);
00278         }
00279 
00280         if(qe_status & CREG_STAT_CCOFLOW) {
00281                 qep->net_stats.tx_errors += 256;
00282                 qep->net_stats.collisions += 256;
00283         }
00284 
00285         if(qe_status & CREG_STAT_TXDERROR) {
00286                 printk(KERN_ERR "%s: Transmit descriptor is bogus, driver bug.\n", dev->name);
00287                 qep->net_stats.tx_errors++;
00288                 qep->net_stats.tx_aborted_errors++;
00289                 mace_hwbug_workaround = 1;
00290         }
00291 
00292         if(qe_status & CREG_STAT_TXLERR) {
00293                 printk(KERN_ERR "%s: Transmit late error.\n", dev->name);
00294                 qep->net_stats.tx_errors++;
00295                 mace_hwbug_workaround = 1;
00296         }
00297 
00298         if(qe_status & CREG_STAT_TXPERR) {
00299                 printk(KERN_ERR "%s: Transmit DMA parity error.\n", dev->name);
00300                 qep->net_stats.tx_errors++;
00301                 qep->net_stats.tx_aborted_errors++;
00302                 mace_hwbug_workaround = 1;
00303         }
00304 
00305         if(qe_status & CREG_STAT_TXSERR) {
00306                 printk(KERN_ERR "%s: Transmit DMA sbus error ack.\n", dev->name);
00307                 qep->net_stats.tx_errors++;
00308                 qep->net_stats.tx_aborted_errors++;
00309                 mace_hwbug_workaround = 1;
00310         }
00311 
00312         if(qe_status & CREG_STAT_RCCOFLOW) {
00313                 qep->net_stats.rx_errors += 256;
00314                 qep->net_stats.collisions += 256;
00315         }
00316 
00317         if(qe_status & CREG_STAT_RUOFLOW) {
00318                 qep->net_stats.rx_errors += 256;
00319                 qep->net_stats.rx_over_errors += 256;
00320         }
00321 
00322         if(qe_status & CREG_STAT_MCOFLOW) {
00323                 qep->net_stats.rx_errors += 256;
00324                 qep->net_stats.rx_missed_errors += 256;
00325         }
00326 
00327         if(qe_status & CREG_STAT_RXFOFLOW) {
00328                 printk(KERN_ERR "%s: Receive fifo overflow.\n", dev->name);
00329                 qep->net_stats.rx_errors++;
00330                 qep->net_stats.rx_over_errors++;
00331         }
00332 
00333         if(qe_status & CREG_STAT_RLCOLL) {
00334                 printk(KERN_ERR "%s: Late receive collision.\n", dev->name);
00335                 qep->net_stats.rx_errors++;
00336                 qep->net_stats.collisions++;
00337         }
00338 
00339         if(qe_status & CREG_STAT_FCOFLOW) {
00340                 qep->net_stats.rx_errors += 256;
00341                 qep->net_stats.rx_frame_errors += 256;
00342         }
00343 
00344         if(qe_status & CREG_STAT_CECOFLOW) {
00345                 qep->net_stats.rx_errors += 256;
00346                 qep->net_stats.rx_crc_errors += 256;
00347         }
00348 
00349         if(qe_status & CREG_STAT_RXDROP) {
00350                 printk(KERN_INFO "%s: Receive packet dropped.\n", dev->name);
00351                 qep->net_stats.rx_errors++;
00352                 qep->net_stats.rx_dropped++;
00353                 qep->net_stats.rx_missed_errors++;
00354         }
00355 
00356         if(qe_status & CREG_STAT_RXSMALL) {
00357                 printk(KERN_ERR "%s: Receive buffer too small, driver bug.\n", dev->name);
00358                 qep->net_stats.rx_errors++;
00359                 qep->net_stats.rx_length_errors++;
00360         }
00361 
00362         if(qe_status & CREG_STAT_RXLERR) {
00363                 printk(KERN_ERR "%s: Receive late error.\n", dev->name);
00364                 qep->net_stats.rx_errors++;
00365                 mace_hwbug_workaround = 1;
00366         }
00367 
00368         if(qe_status & CREG_STAT_RXPERR) {
00369                 printk(KERN_ERR "%s: Receive DMA parity error.\n", dev->name);
00370                 qep->net_stats.rx_errors++;
00371                 qep->net_stats.rx_missed_errors++;
00372                 mace_hwbug_workaround = 1;
00373         }
00374 
00375         if(qe_status & CREG_STAT_RXSERR) {
00376                 printk(KERN_ERR "%s: Receive DMA sbus error ack.\n", dev->name);
00377                 qep->net_stats.rx_errors++;
00378                 qep->net_stats.rx_missed_errors++;
00379                 mace_hwbug_workaround = 1;
00380         }
00381 
00382         if(mace_hwbug_workaround)
00383                 qe_init(qep, 1);
00384         return mace_hwbug_workaround;
00385 }
00386 
00387 /* Per-QE receive interrupt service routine.  Just like on the happy meal
00388  * we receive directly into skb's with a small packet copy water mark.
00389  */
00390 static void qe_rx(struct sunqe *qep)
00391 {
00392         struct qe_rxd *rxbase = &qep->qe_block->qe_rxd[0];
00393         struct qe_rxd *this;
00394         struct sunqe_buffers *qbufs = qep->buffers;
00395         __u32 qbufs_dvma = qep->buffers_dvma;
00396         int elem = qep->rx_new, drops = 0;
00397         unsigned int flags;
00398 
00399         this = &rxbase[elem];
00400         while(!((flags = this->rx_flags) & RXD_OWN)) {
00401                 struct sk_buff *skb;
00402                 unsigned char *this_qbuf =
00403                         &qbufs->rx_buf[elem & (RX_RING_SIZE - 1)][0];
00404                 __u32 this_qbuf_dvma = qbufs_dvma +
00405                         qebuf_offset(rx_buf, (elem & (RX_RING_SIZE - 1)));
00406                 struct qe_rxd *end_rxd =
00407                         &rxbase[(elem+RX_RING_SIZE)&(RX_RING_MAXSIZE-1)];
00408                 int len = (flags & RXD_LENGTH) - 4;  /* QE adds ether FCS size to len */
00409 
00410                 /* Check for errors. */
00411                 if(len < ETH_ZLEN) {
00412                         qep->net_stats.rx_errors++;
00413                         qep->net_stats.rx_length_errors++;
00414                         qep->net_stats.rx_dropped++;
00415                 } else {
00416                         skb = dev_alloc_skb(len + 2);
00417                         if(skb == 0) {
00418                                 drops++;
00419                                 qep->net_stats.rx_dropped++;
00420                         } else {
00421                                 skb->dev = qep->dev;
00422                                 skb_reserve(skb, 2);
00423                                 skb_put(skb, len);
00424                                 eth_copy_and_sum(skb, (unsigned char *)this_qbuf,
00425                                                  len, 0);
00426                                 skb->protocol = eth_type_trans(skb, qep->dev);
00427                                 netif_rx(skb);
00428                                 qep->net_stats.rx_packets++;
00429                                 qep->net_stats.rx_bytes+=len;
00430                         }
00431                 }
00432                 end_rxd->rx_addr = this_qbuf_dvma;
00433                 end_rxd->rx_flags = (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
00434                 
00435                 elem = NEXT_RX(elem);
00436                 this = &rxbase[elem];
00437         }
00438         qep->rx_new = elem;
00439         if(drops)
00440                 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", qep->dev->name);
00441 }
00442 
00443 /* Interrupts for all QE's get filtered out via the QEC master controller,
00444  * so we just run through each qe and check to see who is signaling
00445  * and thus needs to be serviced.
00446  */
00447 static void qec_interrupt(int irq, void *dev_id, struct pt_regs *regs)
00448 {
00449         struct sunqec *qecp = (struct sunqec *) dev_id;
00450         unsigned int qec_status;
00451         int channel = 0;
00452 
00453         /* Latch the status now. */
00454         qec_status = qecp->gregs->stat;
00455         while(channel < 4) {
00456                 if(qec_status & 0xf) {
00457                         struct sunqe *qep = qecp->qes[channel];
00458                         struct device *dev = qep->dev;
00459                         unsigned int qe_status;
00460 
00461                         dev->interrupt = 1;
00462 
00463                         qe_status = qep->qcregs->stat;
00464                         if(qe_status & CREG_STAT_ERRORS)
00465                                 if(qe_is_bolixed(qep, qe_status))
00466                                         goto next;
00467 
00468                         if(qe_status & CREG_STAT_RXIRQ)
00469                                 qe_rx(qep);
00470         next:
00471                         dev->interrupt = 0;
00472                 }
00473                 qec_status >>= 4;
00474                 channel++;
00475         }
00476 }
00477 
00478 static int qe_open(struct device *dev)
00479 {
00480         struct sunqe *qep = (struct sunqe *) dev->priv;
00481         int res;
00482 
00483         qep->mconfig = (MREGS_MCONFIG_TXENAB |
00484                         MREGS_MCONFIG_RXENAB |
00485                         MREGS_MCONFIG_MBAENAB);
00486         res = qe_init(qep, 0);
00487         if(!res)
00488                 MOD_INC_USE_COUNT;
00489 
00490         return res;
00491 }
00492 
00493 static int qe_close(struct device *dev)
00494 {
00495         struct sunqe *qep = (struct sunqe *) dev->priv;
00496 
00497         qe_stop(qep);
00498         MOD_DEC_USE_COUNT;
00499         return 0;
00500 }
00501 
00502 /* Reclaim TX'd frames from the ring. */
00503 static void qe_tx_reclaim(struct sunqe *qep)
00504 {
00505         struct qe_txd *txbase = &qep->qe_block->qe_txd[0];
00506         struct device *dev = qep->dev;
00507         int elem = qep->tx_old;
00508 
00509         while(elem != qep->tx_new) {
00510                 unsigned int flags = txbase[elem].tx_flags;
00511 
00512                 if (flags & TXD_OWN)
00513                         break;
00514                 qep->net_stats.tx_packets++;
00515                 qep->net_stats.tx_bytes+=(flags & TXD_LENGTH);
00516                 elem = NEXT_TX(elem);
00517         }
00518         qep->tx_old = elem;
00519 
00520         if(dev->tbusy && (TX_BUFFS_AVAIL(qep) > 0)) {
00521                 dev->tbusy = 0;
00522                 mark_bh(NET_BH);
00523         }
00524 }
00525 
00526 /* Get a packet queued to go onto the wire. */
00527 static int qe_start_xmit(struct sk_buff *skb, struct device *dev)
00528 {
00529         struct sunqe *qep = (struct sunqe *) dev->priv;
00530         struct sunqe_buffers *qbufs = qep->buffers;
00531         __u32 txbuf_dvma, qbufs_dvma = qep->buffers_dvma;
00532         unsigned char *txbuf;
00533         int len, entry;
00534 
00535         qe_tx_reclaim(qep);
00536 
00537         if(test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
00538                 long tickssofar = jiffies - dev->trans_start;
00539 
00540                 if (tickssofar >= 40) {
00541                         printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
00542                         qe_init(qep, 1);
00543                         dev->tbusy = 0;
00544                         dev->trans_start = jiffies;
00545                 }
00546                 return 1;
00547         }
00548 
00549         if(!TX_BUFFS_AVAIL(qep))
00550                 return 1;
00551 
00552         len = skb->len;
00553         entry = qep->tx_new;
00554 
00555         txbuf = &qbufs->tx_buf[entry & (TX_RING_SIZE - 1)][0];
00556         txbuf_dvma = qbufs_dvma +
00557                 qebuf_offset(tx_buf, (entry & (TX_RING_SIZE - 1)));
00558 
00559         /* Avoid a race... */
00560         qep->qe_block->qe_txd[entry].tx_flags = TXD_UPDATE;
00561 
00562         memcpy(txbuf, skb->data, len);
00563 
00564         qep->qe_block->qe_txd[entry].tx_addr = txbuf_dvma;
00565         qep->qe_block->qe_txd[entry].tx_flags =
00566                 (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
00567         qep->tx_new = NEXT_TX(entry);
00568 
00569         /* Get it going. */
00570         dev->trans_start = jiffies;
00571         qep->qcregs->ctrl = CREG_CTRL_TWAKEUP;
00572 
00573         dev_kfree_skb(skb);
00574 
00575         if(TX_BUFFS_AVAIL(qep))
00576                 dev->tbusy = 0;
00577 
00578         return 0;
00579 }
00580 
00581 static struct net_device_stats *qe_get_stats(struct device *dev)
00582 {
00583         struct sunqe *qep = (struct sunqe *) dev->priv;
00584 
00585         return &qep->net_stats;
00586 }
00587 
00588 #define CRC_POLYNOMIAL_BE 0x04c11db7UL  /* Ethernet CRC, big endian */
00589 #define CRC_POLYNOMIAL_LE 0xedb88320UL  /* Ethernet CRC, little endian */
00590 
00591 static void qe_set_multicast(struct device *dev)
00592 {
00593         struct sunqe *qep = (struct sunqe *) dev->priv;
00594         struct dev_mc_list *dmi = dev->mc_list;
00595         unsigned char new_mconfig = qep->mconfig;
00596         char *addrs;
00597         int i, j, bit, byte;
00598         u32 crc, poly = CRC_POLYNOMIAL_LE;
00599 
00600         /* Lock out others. */
00601         set_bit(0, (void *) &dev->tbusy);
00602 
00603         if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
00604                 qep->mregs->iaconfig = MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET;
00605                 while ((qep->mregs->iaconfig & MREGS_IACONFIG_ACHNGE) != 0)
00606                         barrier();
00607                 for(i = 0; i < 8; i++)
00608                         qep->mregs->filter = 0xff;
00609                 qep->mregs->iaconfig = 0;
00610         } else if(dev->flags & IFF_PROMISC) {
00611                 new_mconfig |= MREGS_MCONFIG_PROMISC;
00612         } else {
00613                 u16 hash_table[4];
00614                 unsigned char *hbytes = (unsigned char *) &hash_table[0];
00615 
00616                 for(i = 0; i < 4; i++)
00617                         hash_table[i] = 0;
00618 
00619                 for(i = 0; i < dev->mc_count; i++) {
00620                         addrs = dmi->dmi_addr;
00621                         dmi = dmi->next;
00622 
00623                         if(!(*addrs & 1))
00624                                 continue;
00625 
00626                         crc = 0xffffffffU;
00627                         for(byte = 0; byte < 6; byte++) {
00628                                 for(bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
00629                                         int test;
00630 
00631                                         test = ((bit ^ crc) & 0x01);
00632                                         crc >>= 1;
00633                                         if(test)
00634                                                 crc = crc ^ poly;
00635                                 }
00636                         }
00637                         crc >>= 26;
00638                         hash_table[crc >> 4] |= 1 << (crc & 0xf);
00639                 }
00640                 /* Program the qe with the new filter value. */
00641                 qep->mregs->iaconfig = MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET;
00642                 while ((qep->mregs->iaconfig & MREGS_IACONFIG_ACHNGE) != 0)
00643                         barrier();
00644                 for(i = 0; i < 8; i++)
00645                         qep->mregs->filter = *hbytes++;
00646                 qep->mregs->iaconfig = 0;
00647         }
00648 
00649         /* Any change of the logical address filter, the physical address,
00650          * or enabling/disabling promiscuous mode causes the MACE to disable
00651          * the receiver.  So we must re-enable them here or else the MACE
00652          * refuses to listen to anything on the network.  Sheesh, took
00653          * me a day or two to find this bug.
00654          */
00655         qep->mconfig = new_mconfig;
00656         qep->mregs->mconfig = qep->mconfig;
00657 
00658         /* Let us get going again. */
00659         dev->tbusy = 0;
00660 }
00661 
00662 /* This is only called once at boot time for each card probed. */
00663 static inline void qec_init_once(struct sunqec *qecp, struct linux_sbus_device *qsdev)
00664 {
00665         unsigned char bsizes = qecp->qec_bursts;
00666 
00667 #ifdef __sparc_v9__
00668         if (bsizes & DMA_BURST64) {
00669                 qecp->gregs->ctrl = GLOB_CTRL_B64;
00670         } else
00671 #endif
00672         if(bsizes & DMA_BURST32) {
00673                 qecp->gregs->ctrl = GLOB_CTRL_B32;
00674         } else {
00675                 qecp->gregs->ctrl = GLOB_CTRL_B16;
00676         }
00677 
00678         /* Packetsize only used in 100baseT BigMAC configurations,
00679          * set it to zero just to be on the safe side.
00680          */
00681         qecp->gregs->psize = 0;
00682 
00683         /* Set the local memsize register, divided up to one piece per QE channel. */
00684         qecp->gregs->msize = (qsdev->reg_addrs[1].reg_size >> 2);
00685 
00686         /* Divide up the local QEC memory amongst the 4 QE receiver and
00687          * transmitter FIFOs.  Basically it is (total / 2 / num_channels).
00688          */
00689         qecp->gregs->rsize = qecp->gregs->tsize =
00690                 (qsdev->reg_addrs[1].reg_size >> 2) >> 1;
00691 
00692 }
00693 
00694 /* Four QE's per QEC card. */
00695 static inline int qec_ether_init(struct device *dev, struct linux_sbus_device *sdev)
00696 {
00697         static unsigned version_printed = 0;
00698         struct device *qe_devs[4];
00699         struct sunqe *qeps[4];
00700         struct linux_sbus_device *qesdevs[4];
00701         struct sunqec *qecp;
00702         struct linux_prom_ranges qranges[8];
00703         unsigned char bsizes, bsizes_more, num_qranges;
00704         int i, j, res = ENOMEM;
00705 
00706         dev = init_etherdev(0, sizeof(struct sunqe));
00707         qe_devs[0] = dev;
00708         qeps[0] = (struct sunqe *) dev->priv;
00709         qeps[0]->channel = 0;
00710         for(j = 0; j < 6; j++)
00711                 qe_devs[0]->dev_addr[j] = idprom->id_ethaddr[j];
00712 
00713         if(version_printed++ == 0)
00714                 printk(KERN_INFO "%s", version);
00715 
00716         qe_devs[1] = qe_devs[2] = qe_devs[3] = NULL;
00717         for(i = 1; i < 4; i++) {
00718                 qe_devs[i] = init_etherdev(0, sizeof(struct sunqe));
00719                 if(qe_devs[i] == NULL || qe_devs[i]->priv == NULL)
00720                         goto qec_free_devs;
00721                 qeps[i] = (struct sunqe *) qe_devs[i]->priv;
00722                 for(j = 0; j < 6; j++)
00723                         qe_devs[i]->dev_addr[j] = idprom->id_ethaddr[j];
00724                 qeps[i]->channel = i;
00725         }
00726         qecp = kmalloc(sizeof(struct sunqec), GFP_KERNEL);
00727         if(qecp == NULL)
00728                 goto qec_free_devs;
00729         qecp->qec_sbus_dev = sdev;
00730 
00731         for(i = 0; i < 4; i++) {
00732                 qecp->qes[i] = qeps[i];
00733                 qeps[i]->dev = qe_devs[i];
00734                 qeps[i]->parent = qecp;
00735         }
00736 
00737         /* Link in channel 0. */
00738         i = prom_getintdefault(sdev->child->prom_node, "channel#", -1);
00739         if(i == -1) { res=ENODEV; goto qec_free_devs; }
00740         qesdevs[i] = sdev->child;
00741         qe_devs[i]->base_addr = (long) qesdevs[i];
00742 
00743         /* Link in channel 1. */
00744         i = prom_getintdefault(sdev->child->next->prom_node, "channel#", -1);
00745         if(i == -1) { res=ENODEV; goto qec_free_devs; }
00746         qesdevs[i] = sdev->child->next;
00747         qe_devs[i]->base_addr = (long) qesdevs[i];
00748 
00749         /* Link in channel 2. */
00750         i = prom_getintdefault(sdev->child->next->next->prom_node, "channel#", -1);
00751         if(i == -1) { res=ENODEV; goto qec_free_devs; }
00752         qesdevs[i] = sdev->child->next->next;
00753         qe_devs[i]->base_addr = (long) qesdevs[i];
00754 
00755         /* Link in channel 3. */
00756         i = prom_getintdefault(sdev->child->next->next->next->prom_node, "channel#", -1);
00757         if(i == -1) { res=ENODEV; goto qec_free_devs; }
00758         qesdevs[i] = sdev->child->next->next->next;
00759         qe_devs[i]->base_addr = (long) qesdevs[i];
00760 
00761         for(i = 0; i < 4; i++)
00762                 qeps[i]->qe_sbusdev = qesdevs[i];
00763 
00764         /* This is a bit of fun, get QEC ranges. */
00765         i = prom_getproperty(sdev->prom_node, "ranges",
00766                              (char *) &qranges[0], sizeof(qranges));
00767         num_qranges = (i / sizeof(struct linux_prom_ranges));
00768 
00769         /* Now, apply all the ranges, QEC ranges then the SBUS ones for each QE. */
00770         if (sdev->ranges_applied == 0) {
00771                 for(i = 0; i < 4; i++) {
00772                         for(j = 0; j < 2; j++) {
00773                                 int k;
00774 
00775                                 for(k = 0; k < num_qranges; k++)
00776                                         if(qesdevs[i]->reg_addrs[j].which_io ==
00777                                            qranges[k].ot_child_space)
00778                                                 break;
00779                                 if(k >= num_qranges)
00780                                         printk(KERN_ERR "QuadEther: Aieee, bogus QEC range for "
00781                                                "space %08x\n",qesdevs[i]->reg_addrs[j].which_io);
00782                                 qesdevs[i]->reg_addrs[j].which_io = qranges[k].ot_parent_space;
00783                                 qesdevs[i]->reg_addrs[j].phys_addr += qranges[k].ot_parent_base;
00784                         }
00785 
00786                         prom_apply_sbus_ranges(qesdevs[i]->my_bus, &qesdevs[i]->reg_addrs[0],
00787                                                2, qesdevs[i]);
00788                 }
00789                 prom_apply_sbus_ranges(sdev->my_bus, &sdev->reg_addrs[0],
00790                                        sdev->num_registers, sdev);
00791         }
00792 
00793         /* Now map in the registers, QEC globals first. */
00794         qecp->gregs = sparc_alloc_io(sdev->reg_addrs[0].phys_addr, 0,
00795                                      sizeof(struct qe_globreg),
00796                                      "QEC Global Registers",
00797                                      sdev->reg_addrs[0].which_io, 0);
00798         if(!qecp->gregs) {
00799                 printk(KERN_ERR "QuadEther: Cannot map QEC global registers.\n");
00800                 res = ENODEV;
00801                 goto qec_free_devs;
00802         }
00803 
00804         /* Make sure the QEC is in MACE mode. */
00805         if((qecp->gregs->ctrl & 0xf0000000) != GLOB_CTRL_MMODE) {
00806                 printk(KERN_ERR "QuadEther: AIEEE, QEC is not in MACE mode!\n");
00807                 res = ENODEV;
00808                 goto qec_free_devs;
00809         }
00810 
00811         /* Reset the QEC. */
00812         if(qec_global_reset(qecp->gregs)) {
00813                 res = ENODEV;
00814                 goto qec_free_devs;
00815         }
00816 
00817         /* Find and set the burst sizes for the QEC, since it does
00818          * the actual dma for all 4 channels.
00819          */
00820         bsizes = prom_getintdefault(sdev->prom_node, "burst-sizes", 0xff);
00821         bsizes &= 0xff;
00822         bsizes_more = prom_getintdefault(sdev->my_bus->prom_node, "burst-sizes", 0xff);
00823 
00824         if(bsizes_more != 0xff)
00825                 bsizes &= bsizes_more;
00826         if(bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
00827            (bsizes & DMA_BURST32)==0)
00828                 bsizes = (DMA_BURST32 - 1);
00829 
00830         qecp->qec_bursts = bsizes;
00831 
00832         /* Perform one time QEC initialization, we never touch the QEC
00833          * globals again after this.
00834          */
00835         qec_init_once(qecp, sdev);
00836 
00837         for(i = 0; i < 4; i++) {
00838                 /* Map in QEC per-channel control registers. */
00839                 qeps[i]->qcregs = sparc_alloc_io(qesdevs[i]->reg_addrs[0].phys_addr, 0,
00840                                                  sizeof(struct qe_creg),
00841                                                  "QEC Per-Channel Registers",
00842                                                  qesdevs[i]->reg_addrs[0].which_io, 0);
00843                 if(!qeps[i]->qcregs) {
00844                         printk(KERN_ERR "QuadEther: Cannot map QE %d's channel registers.\n", i);
00845                         res = ENODEV;
00846                         goto qec_free_devs;
00847                 }
00848 
00849                 /* Map in per-channel AMD MACE registers. */
00850                 qeps[i]->mregs = sparc_alloc_io(qesdevs[i]->reg_addrs[1].phys_addr, 0,
00851                                                 sizeof(struct qe_mregs),
00852                                                 "QE MACE Registers",
00853                                                 qesdevs[i]->reg_addrs[1].which_io, 0);
00854                 if(!qeps[i]->mregs) {
00855                         printk(KERN_ERR "QuadEther: Cannot map QE %d's MACE registers.\n", i);
00856                         res = ENODEV;
00857                         goto qec_free_devs;
00858                 }
00859 
00860                 qeps[i]->qe_block = (struct qe_init_block *)
00861                         sparc_dvma_malloc(PAGE_SIZE, "QE Init Block",
00862                                           &qeps[i]->qblock_dvma);
00863 
00864                 qeps[i]->buffers = (struct sunqe_buffers *)
00865                         sparc_dvma_malloc(sizeof(struct sunqe_buffers),
00866                                           "QE RX/TX Buffers",
00867                                           &qeps[i]->buffers_dvma);
00868 
00869                 /* Stop this QE. */
00870                 qe_stop(qeps[i]);
00871         }
00872 
00873         for(i = 0; i < 4; i++) {
00874                 qe_devs[i]->open = qe_open;
00875                 qe_devs[i]->stop = qe_close;
00876                 qe_devs[i]->hard_start_xmit = qe_start_xmit;
00877                 qe_devs[i]->get_stats = qe_get_stats;
00878                 qe_devs[i]->set_multicast_list = qe_set_multicast;
00879                 qe_devs[i]->irq = sdev->irqs[0];
00880                 qe_devs[i]->dma = 0;
00881                 ether_setup(qe_devs[i]);
00882         }
00883 
00884         /* QEC receives interrupts from each QE, then it sends the actual
00885          * IRQ to the cpu itself.  Since QEC is the single point of
00886          * interrupt for all QE channels we register the IRQ handler
00887          * for it now.
00888          */
00889         if(request_irq(sdev->irqs[0], &qec_interrupt,
00890                        SA_SHIRQ, "QuadEther", (void *) qecp)) {
00891                 printk(KERN_ERR "QuadEther: Can't register QEC master irq handler.\n");
00892                 res = EAGAIN;
00893                 goto qec_free_devs;
00894         }
00895 
00896         /* Report the QE channels. */
00897         for(i = 0; i < 4; i++) {
00898                 printk(KERN_INFO "%s: QuadEthernet channel[%d] ", qe_devs[i]->name, i);
00899                 for(j = 0; j < 6; j++)
00900                         printk ("%2.2x%c",
00901                                 qe_devs[i]->dev_addr[j],
00902                                 j == 5 ? ' ': ':');
00903                 printk("\n");
00904         }
00905 
00906 #ifdef MODULE
00907         /* We are home free at this point, link the qe's into
00908          * the master list for later module unloading.
00909          */
00910         for(i = 0; i < 4; i++)
00911                 qe_devs[i]->ifindex = dev_new_index();
00912         qecp->next_module = root_qec_dev;
00913         root_qec_dev = qecp;
00914 #endif
00915 
00916         return 0;
00917 
00918 qec_free_devs:
00919         for(i = 0; i < 4; i++) {
00920                 if(qe_devs[i]) {
00921                         if(qe_devs[i]->priv)
00922                                 kfree(qe_devs[i]->priv);
00923                         kfree(qe_devs[i]);
00924                 }
00925         }
00926         return res;
00927 }
00928 
00929 __initfunc(int qec_probe(struct device *dev))
00930 {
00931         struct linux_sbus *bus;
00932         struct linux_sbus_device *sdev = 0;
00933         static int called = 0;
00934         int cards = 0, v;
00935 
00936         if(called)
00937                 return ENODEV;
00938         called++;
00939 
00940         for_each_sbus(bus) {
00941                 for_each_sbusdev(sdev, bus) {
00942                         if(cards) dev = NULL;
00943 
00944                         /* QEC can be parent of either QuadEthernet or BigMAC
00945                          * children.
00946                          */
00947                         if(!strcmp(sdev->prom_name, "qec") && sdev->child &&
00948                            !strcmp(sdev->child->prom_name, "qe") &&
00949                            sdev->child->next &&
00950                            !strcmp(sdev->child->next->prom_name, "qe") &&
00951                            sdev->child->next->next &&
00952                            !strcmp(sdev->child->next->next->prom_name, "qe") &&
00953                            sdev->child->next->next->next &&
00954                            !strcmp(sdev->child->next->next->next->prom_name, "qe")) {
00955                                 cards++;
00956                                 if((v = qec_ether_init(dev, sdev)))
00957                                         return v;
00958                         }
00959                 }
00960         }
00961         if(!cards)
00962                 return ENODEV;
00963         return 0;
00964 }
00965 
00966 #ifdef MODULE
00967 
00968 int
00969 init_module(void)
00970 {
00971         root_qec_dev = NULL;
00972         return qec_probe(NULL);
00973 }
00974 
00975 void
00976 cleanup_module(void)
00977 {
00978         struct sunqec *next_qec;
00979         int i;
00980 
00981         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
00982         while (root_qec_dev) {
00983                 next_qec = root_qec_dev->next_module;
00984 
00985                 /* Release all four QE channels, then the QEC itself. */
00986                 for(i = 0; i < 4; i++) {
00987                         unregister_netdev(root_qec_dev->qes[i]->dev);
00988                         sparc_free_io(root_qec_dev->qes[i]->qcregs, sizeof(struct qe_creg));
00989                         sparc_free_io(root_qec_dev->qes[i]->mregs, sizeof(struct qe_mregs));
00990                         kfree(root_qec_dev->qes[i]->dev);
00991                 }
00992                 free_irq(root_qec_dev->qec_sbus_dev->irqs[0], (void *)root_qec_dev);
00993                 sparc_free_io(root_qec_dev->gregs, sizeof(struct qe_globreg));
00994                 kfree(root_qec_dev);
00995                 root_qec_dev = next_qec;
00996         }
00997 }
00998 
00999 #endif /* MODULE */