Index: c/src/lib/libbsp/powerpc/dmv177/sonic/sonic.c
===================================================================
--- c/src/lib/libbsp/powerpc/dmv177/sonic/sonic.c	(revision 07d880f4bccd3d5f26fb729ca3fca5f6bed081d3)
+++ c/src/lib/libbsp/powerpc/dmv177/sonic/sonic.c	(revision 7dbab7204fc0c9fcb47826bacaf11b0be663f257)
@@ -64,11 +64,29 @@
 #define SONIC_DEBUG_DESCRIPTORS      0x0010
 #define SONIC_DEBUG_ERRORS           0x0020
-
-#define SONIC_DEBUG (SONIC_DEBUG_ERRORS)
+#define SONIC_DEBUG_DUMP_TX_MBUFS    0x0040
+#define SONIC_DEBUG_DUMP_RX_MBUFS    0x0080
+
+#define SONIC_DEBUG_DUMP_MBUFS \
+  (SONIC_DEBUG_DUMP_TX_MBUFS|SONIC_DEBUG_DUMP_RX_MBUFS)
+
+#define SONIC_DEBUG_MEDIUM \
+  ((SONIC_DEBUG_ALL) & ~(SONIC_DEBUG_PRINT_REGISTERS|SONIC_DEBUG_DUMP_MBUFS))
+  /*
+  ((SONIC_DEBUG_ALL) & ~(SONIC_DEBUG_DUMP_MBUFS))
+*/
+
+#define SONIC_DEBUG  SONIC_DEBUG_ALL
+
+/* ((SONIC_DEBUG_ALL) & ~SONIC_DEBUG_PRINT_REGISTERS)  */
+  /* (SONIC_DEBUG_ALL) */
+
+/* (SONIC_DEBUG_ALL) */
+/* (SONIC_DEBUG_ERRORS) */
 
 /* (SONIC_DEBUG_MEMORY|SONIC_DEBUG_DESCRIPTORS) */
 
-/* SONIC_DEBUG_ALL */
-
+#if (SONIC_DEBUG & SONIC_DEBUG_DUMP_MBUFS)
+#include <rtems/dumpbuf.h>
+#endif
 
 /*
@@ -140,5 +158,5 @@
  */
 #define RDA_COUNT     20
-#define TDA_COUNT     100
+#define TDA_COUNT     10
 
 /*
@@ -196,4 +214,11 @@
    */
   void                             *sonic;
+
+  /*
+   *  Tables to map the mbufs from chip to stack
+   */
+
+  struct mbuf             **rxMbuf;
+  struct mbuf             **txMbuf;
 
   /*
@@ -306,7 +331,7 @@
      */
     p = malloc( nbytes, M_MBUF, M_NOWAIT );
-    memset (p, '\0', nbytes);
     if (p == NULL)
       rtems_panic ("No memory!");
+    memset (p, '\0', nbytes);
     a1 = (unsigned long)p;
     a2 = a1 + nbytes - 1;
@@ -373,4 +398,5 @@
 {
   struct sonic_softc *sc = sonic_softc;
+  unsigned32 isr, imr;
   void *rp;
 
@@ -394,14 +420,13 @@
   sc->Interrupts++;
 
+  isr = sonic_read_register( rp, SONIC_REG_ISR );
+  imr = sonic_read_register( rp, SONIC_REG_IMR );
+
   /*
    * Packet received or receive buffer area exceeded?
    */
-  if ((sonic_read_register( rp, SONIC_REG_IMR ) & (IMR_PRXEN | IMR_RBAEEN)) &&
-      (sonic_read_register( rp, SONIC_REG_ISR ) & (ISR_PKTRX | ISR_RBAE))) {
-    sonic_write_register(
-       rp,
-       SONIC_REG_IMR,
-       sonic_read_register( rp, SONIC_REG_IMR) & ~(IMR_PRXEN | IMR_RBAEEN)
-    );
+  if ((imr & (IMR_PRXEN | IMR_RBAEEN)) &&
+      (isr & (ISR_PKTRX | ISR_RBAE))) {
+    imr &= ~(IMR_PRXEN | IMR_RBAEEN);
     sc->rxInterrupts++;
     rtems_event_send (sc->rxDaemonTid, INTERRUPT_EVENT);
@@ -411,15 +436,12 @@
    * Packet started, transmitter done or transmitter error?
    */
-  if ((sonic_read_register( rp, SONIC_REG_IMR ) & (IMR_PINTEN | IMR_PTXEN | IMR_TXEREN))
-   && (sonic_read_register( rp, SONIC_REG_ISR ) & (ISR_PINT | ISR_TXDN | ISR_TXER))) {
-    sonic_write_register(
-       rp,
-       SONIC_REG_IMR,
-       sonic_read_register( rp, SONIC_REG_IMR) &
-                  ~(IMR_PINTEN | IMR_PTXEN | IMR_TXEREN)
-    );
+  if ((imr & (IMR_PINTEN | IMR_PTXEN | IMR_TXEREN)) &&
+      (isr & (ISR_PINT | ISR_TXDN | ISR_TXER))) {
+    imr &= ~(IMR_PINTEN | IMR_PTXEN | IMR_TXEREN);
     sc->txInterrupts++;
     rtems_event_send (sc->txDaemonTid, INTERRUPT_EVENT);
   }
+
+  sonic_write_register( rp, SONIC_REG_IMR, imr );
 }
 
@@ -602,10 +624,16 @@
       fp->frag_size = m->m_len;
       packetSize += m->m_len;
+#if (SONIC_DEBUG & SONIC_DEBUG_FRAGMENTS)
+      printf( "fp %p 0x%04x%04x %d=%d .. %d\n",
+        fp, fp->frag_msw, fp->frag_lsw, fp->frag_size, m->m_len, packetSize );
+#endif
+#if (SONIC_DEBUG & SONIC_DEBUG_DUMP_TX_MBUFS)
+      Dump_Buffer(
+        p,
+        (fp->frag_size > MAXIMUM_FRAME_SIZE) ? MAXIMUM_FRAME_SIZE : fp->frag_size
+      );
+#endif
       l = m;
       m = m->m_next;
-#if (SONIC_DEBUG & SONIC_DEBUG_FRAGMENTS)
-      printf( "fp %p 0x%04x%04x %d\n",
-                    fp, fp->frag_msw, fp->frag_lsw, fp->frag_size );
-#endif
     }
     else {
@@ -658,5 +686,6 @@
   sc->tdaHead = tdp;
 
-  sonic_enable_interrupts( rp, (IMR_PINTEN | IMR_PTXEN | IMR_TXEREN) );
+/* XXX */
+/*   sonic_enable_interrupts( rp, (IMR_PINTEN | IMR_PTXEN | IMR_TXEREN) ); */
   sonic_write_register( rp, SONIC_REG_CR, CR_TXP );
 }
@@ -859,4 +888,5 @@
   ReceiveResourcePointer_t rwp, rea;
   rtems_unsigned16 newMissedTally, oldMissedTally;
+  unsigned32 rxMbufIndex;
 
   rwp = sc->rsa;
@@ -868,10 +898,9 @@
    */
   oldMissedTally = sonic_read_register( rp, SONIC_REG_MPT );
-  sonic_write_register( rp, SONIC_REG_CR, CR_RRRA );
-  sonic_write_register( rp, SONIC_REG_CR, CR_RXEN );
 
   /*
    * Input packet handling loop
    */
+  rxMbufIndex = 0;
   for (;;) {
     /*
@@ -891,14 +920,6 @@
     status = rdp->status;
     if (status & RDA_STATUS_PRX) {
-      struct mbuf **mp;
       struct ether_header *eh;
       void *p;
-
-      /*
-       * Get the mbuf pointer
-       */
-      p = PTR(rdp->pkt_msw, rdp->pkt_lsw);
-      mp = (struct mbuf **)p - 1;
-      m = *mp;
 
       /*
@@ -910,4 +931,5 @@
        * Invalidate cache entries for this memory.
        */
+      m = rdp->mbufp;
       m->m_len = m->m_pkthdr.len = rdp->byte_count -
                           sizeof(rtems_unsigned32) -
@@ -915,4 +937,10 @@
       eh = mtod (m, struct ether_header *);
       m->m_data += sizeof(struct ether_header);
+  
+#if (SONIC_DEBUG & SONIC_DEBUG_DUMP_RX_MBUFS)
+      Dump_Buffer( (void *) eh, sizeof(struct ether_header) );
+      Dump_Buffer( (void *) m, 96 /* m->m_len*/ );
+#endif
+
       ether_input (ifp, eh, m);
 
@@ -934,10 +962,10 @@
        * Allocate a new mbuf.
        */
+
+      m= (void *)0xA0000000; /* hope for a fault :) */
       MGETHDR (m, M_WAIT, MT_DATA);
       MCLGET (m, M_WAIT);
       m->m_pkthdr.rcvif = ifp;
-      mp = mtod (m, struct mbuf **);
-      m->m_data += sizeof *mp;
-      *mp = m;
+      rdp->mbufp = m;
       p = mtod (m, void *);
 
@@ -945,7 +973,11 @@
        * Reuse Receive Resource.
        */
+
       rwp->buff_ptr_lsw = LSW(p);
       rwp->buff_ptr_msw = MSW(p);
+      rwp->buff_wc_lsw = RBUF_WC;
+      rwp->buff_wc_msw = 0;
       rwp++;
+
       if (rwp == rea) {
 #if (SONIC_DEBUG & SONIC_DEBUG_MEMORY)
@@ -983,7 +1015,9 @@
      * Move to next receive descriptor
      */
+
     rdp->in_use = RDA_FREE;
     rdp = rdp->next;
     rdp->link &= ~RDA_LINK_EOL;
+
   }
 }
@@ -1026,4 +1060,17 @@
   
   /*
+   *  Allocate memory so we can figure out from the descriptor which
+   *  mbuf to send to the stack.
+   */
+
+  sc->txMbuf = malloc (sc->tdaCount * sizeof *sc->txMbuf, M_MBUF, M_NOWAIT);
+  if (!sc->txMbuf)
+     rtems_panic ("No memory for TX mbuf pointers");
+
+  sc->rxMbuf = malloc (sc->rdaCount * sizeof *sc->rxMbuf, M_MBUF, M_NOWAIT);
+  if (!sc->rxMbuf)
+     rtems_panic ("No memory for RX mbuf pointers");
+
+  /*
    *  Set up circular linked list in Transmit Descriptor Area.
    *  Use the PINT bit in the transmit configuration field to
@@ -1040,4 +1087,9 @@
   tdp = sc->tdaTail;
   for (i = 0 ; i < sc->tdaCount ; i++) {
+    /*
+     *  Start off with the table of outstanding mbuf's 
+     */
+    sc->txMbuf[i] = NULL;
+
     /*
      *  status, pkt_config, pkt_size, and all fragment fields 
@@ -1120,5 +1172,4 @@
   rwp = sc->rsa;
   for (i = 0 ; i < (sc->rdaCount + RRA_EXTRA_COUNT) ; i++, rwp++) {
-    struct mbuf **mp;
 
     /*
@@ -1132,8 +1183,7 @@
     MCLGET (m, M_WAIT);
     m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
-    mp = mtod (m, struct mbuf **);
-
-    m->m_data += sizeof *mp;
-    *mp = m;
+    sc->rxMbuf[i] = m;
+    sc->rda[i].mbufp = m;
+
     p = mtod (m, void *);
 
@@ -1168,5 +1218,5 @@
    * Mask all interrupts
    */
-  sonic_write_register( rp, SONIC_REG_IMR, 0x3fff );
+  sonic_write_register( rp, SONIC_REG_IMR, 0x0 ); /* XXX was backwards */
 
   /*
@@ -1293,10 +1343,12 @@
   }
 
-  sonic_write_register(rp, SONIC_REG_CR, CR_TXP | CR_RXEN | CR_STP);
+  sonic_write_register(rp, SONIC_REG_CR, /* CR_TXP | */CR_RXEN | CR_STP);
 
   /*
    * Attach SONIC interrupt handler
    */
+/* XXX
   sonic_write_register( rp, SONIC_REG_IMR, 0 );
+*/
   old_handler = set_vector(sonic_interrupt_handler, sc->vector, 0);
 
@@ -1362,5 +1414,8 @@
    * Enable receiver and transmitter
    */
-  sonic_write_register(rp, SONIC_REG_CR, CR_TXP | CR_RXEN);
+  /* sonic_write_register( rp, SONIC_REG_IMR, 0 ); */
+  sonic_enable_interrupts( rp, (IMR_PRXEN | IMR_RBAEEN) );
+
+  sonic_write_register(rp, SONIC_REG_CR, /* CR_TXP | */ CR_RXEN);
 }
 
@@ -1472,4 +1527,7 @@
     sc->tdaCount = TDA_COUNT;
   sc->acceptBroadcast = !config->ignore_broadcast;
+
+  sc->sonic = (void *) SONIC_BASE_ADDRESS;
+  sc->vector = SONIC_VECTOR;
 
   /*
@@ -1573,4 +1631,8 @@
 {
   volatile unsigned32 *p = base;
+{
+  volatile unsigned32 *C = (void *)0x34CDF0;
+  if ( *C ) printf( "W. *C = 0x%x\n", *C );
+}
 
 #if (SONIC_DEBUG & SONIC_DEBUG_PRINT_REGISTERS)
@@ -1589,4 +1651,9 @@
   volatile unsigned32 *p = base;
   unsigned32           value;
+
+{
+  volatile unsigned32 *C = (void *)0x34CDF0;
+  if ( *C ) printf( "R. *C = 0x%x\n", *C );
+}
 
   value = p[regno];
Index: c/src/lib/libbsp/powerpc/dmv177/sonic/sonic.h
===================================================================
--- c/src/lib/libbsp/powerpc/dmv177/sonic/sonic.h	(revision 07d880f4bccd3d5f26fb729ca3fca5f6bed081d3)
+++ c/src/lib/libbsp/powerpc/dmv177/sonic/sonic.h	(revision 7dbab7204fc0c9fcb47826bacaf11b0be663f257)
@@ -330,4 +330,5 @@
    */
   volatile struct ReceiveDescriptor  *next;  /* Circularly-linked list */
+  struct mbuf                        *mbufp; /* First mbuf in packet */
 };
 typedef struct ReceiveDescriptor ReceiveDescriptor_t;
