Index: c/src/lib/libbsp/m68k/uC5282/ChangeLog
===================================================================
--- c/src/lib/libbsp/m68k/uC5282/ChangeLog	(revision 6fdd3b1070219ff1fb7430d10de0638093d9d607)
+++ c/src/lib/libbsp/m68k/uC5282/ChangeLog	(revision f077cc96df127bef807d45827337bd449a195a68)
@@ -1,2 +1,7 @@
+2005-06-20  Eric Norum <norume@aps.anl.gov>
+
+	* network/network.c: Clean up transmit buffer realignment.  Many thanks to
+                         Mr. Kolja Waschk for identifying the problem.
+
 2005-05-26	Ralf Corsepius <ralf.corsepius@rtems.org>
 
Index: c/src/lib/libbsp/m68k/uC5282/README
===================================================================
--- c/src/lib/libbsp/m68k/uC5282/README	(revision 6fdd3b1070219ff1fb7430d10de0638093d9d607)
+++ c/src/lib/libbsp/m68k/uC5282/README	(revision f077cc96df127bef807d45827337bd449a195a68)
@@ -79,14 +79,14 @@
 2) (first time only)
    Set the uC5282 board Internet configuration:
-        setenv IPADDR0 www.xxx.yyy.zzz      (Your board's address)
-        setenv NETMASK ppp.qqq.rrr.sss      (Your local network address mask)
-        setenv HOSTNAME somename            (Your board's name)
+        setenv IPADDR0 www.xxx.yyy.zzz  (Your board's address)
+        setenv NETMASK ppp.qqq.rrr.sss  (Your local network address mask)
+        setenv HOSTNAME somename        (Your board's name)
 
 3) Type 'tftp<CR>'
 
 4) Run 'tftp' on your host machine:
-            tftp> binary
-            tftp> connect www.xxx.yyy.zzz      (Your ucDIMM's address)
-            tftp> put someFile.exe
+        tftp> binary
+        tftp> connect www.xxx.yyy.zzz   (Your ucDIMM's address)
+        tftp> put someFile.exe          (someFile.boot for the EPICS build system)
 
 5) When the file has downloaded press the <ESC> key to terminate
Index: c/src/lib/libbsp/m68k/uC5282/network/network.c
===================================================================
--- c/src/lib/libbsp/m68k/uC5282/network/network.c	(revision 6fdd3b1070219ff1fb7430d10de0638093d9d607)
+++ c/src/lib/libbsp/m68k/uC5282/network/network.c	(revision f077cc96df127bef807d45827337bd449a195a68)
@@ -1,9 +1,4 @@
 /*
  * RTEMS/TCPIP driver for MCF5282 Fast Ethernet Controller
- *
- * TO DO: Check network stack code -- Is it possible force longword alignment
- *                                    of all tx mbufs?  If so, the stupid 
- *                                    realignment code in the output routine
- *                                    could be removed.
  */
 
@@ -104,5 +99,5 @@
     unsigned long   txRawWait;
     unsigned long   txRealign;
-    unsigned long   txRealignBytes;
+    unsigned long   txRealignDrop;
 };
 static struct mcf5282_enet_struct enet_driver[NIFACES];
@@ -321,9 +316,12 @@
 {
     struct mbuf *m, *n;
+    uint16_t status;
 
     while ((sc->txBdActiveCount != 0)
-        && ((sc->txBdBase[sc->txBdTail].status & MCF5282_FEC_TxBD_R) == 0)) {
-        m = sc->txMbuf[sc->txBdTail];
-        MFREE(m, n);
+        && (((status = sc->txBdBase[sc->txBdTail].status) & MCF5282_FEC_TxBD_R) == 0)) {
+        if ((status & MCF5282_FEC_TxBD_TO1) == 0) {
+            m = sc->txMbuf[sc->txBdTail];
+            MFREE(m, n);
+        }
         if (++sc->txBdTail == sc->txBdCount)
             sc->txBdTail = 0;
@@ -338,5 +336,5 @@
     struct ifnet *ifp = (struct ifnet* )&sc->arpcom.ac_if;
     struct mbuf *m;
-    volatile uint16_t status;
+    uint16_t status;
     volatile mcf5282BufferDescriptor_t *rxBd;
     int rxBdIndex;
@@ -455,4 +453,5 @@
     volatile mcf5282BufferDescriptor_t *firstTxBd, *txBd;
     uint16_t status;
+    int offset;
     int nAdded;
 
@@ -472,5 +471,5 @@
     firstTxBd = sc->txBdBase + sc->txBdHead;
     
-    for (;;) {
+    while (m != NULL) {
         /* 
          * Wait for buffer descriptor to become available
@@ -518,25 +517,35 @@
         if (m->m_len) {
             char *p = mtod(m, char *);
-            /*
-             * Stupid FEC can't handle misaligned data!
-             * Given the way that mbuf's are layed out it should be
-             * safe to shuffle the data down like this.....
-             * Perhaps this code could be improved with a "Duff's Device".
-             */
-            if ((int)p & 0x3) {
-                int l = m->m_len;
-                char *dest = p - ((int)p & 0x3);
-                uint16_t *o = (uint16_t *)dest, *i = (uint16_t *)p;
-                while (l > 0) {
-                    *o++ = *i++;
-                    l -= sizeof(uint16_t);
+            if ((offset = (int)p & 0x3) == 0) {
+                txBd->buffer = p;
+                txBd->length = m->m_len;
+                sc->txMbuf[sc->txBdHead] = m;
+                m = m->m_next;
+            }
+            else {
+                /*
+                 * Stupid FEC can't handle misaligned data!
+                 * Move offending bytes to a local buffer.
+                 * Use buffer descriptor TO1 bit to indicate this.
+                 */
+                int nmove = 4 - offset;
+                char *d = (char *)&sc->txMbuf[sc->txBdHead];
+                status |= MCF5282_FEC_TxBD_TO1;
+                sc->txRealign++;
+                if (nmove > m->m_len)
+                    nmove = m->m_len;
+                m->m_data += nmove;
+                m->m_len -= nmove;
+                txBd->buffer = d;
+                txBd->length = nmove;
+                while (nmove--)
+                    *d++ = *p++;
+                if (m->m_len == 0) {
+                    struct mbuf *n;
+                    sc->txRealignDrop++;
+                    MFREE(m, n);
+                    m = n;
                 }
-                p = dest;
-                sc->txRealign++;
-                sc->txRealignBytes += m->m_len;
             }
-            txBd->buffer = p;
-            txBd->length = m->m_len;
-            sc->txMbuf[sc->txBdHead] = m;
             nAdded++;
             if (++sc->txBdHead == sc->txBdCount) {
@@ -544,9 +553,9 @@
                 sc->txBdHead = 0;
             }
-            m = m->m_next;
+            txBd->status = status;
         }
         else {
             /*
-             * Just toss empty mbufs
+             * Toss empty mbufs.
              */
             struct mbuf *n;
@@ -554,18 +563,14 @@
             m = n;
         }
-        if (m == NULL) {
-          if (nAdded) {
-            txBd->status = status | MCF5282_FEC_TxBD_R
-                                  | MCF5282_FEC_TxBD_L
-                                  | MCF5282_FEC_TxBD_TC;
-            if (nAdded > 1)
-                firstTxBd->status |= MCF5282_FEC_TxBD_R;
-            MCF5282_FEC_TDAR = 0;
-            sc->txBdActiveCount += nAdded;
-          }
-          break;
-        }
-        txBd->status = status;
-    }
+    }
+    if (nAdded) {
+        txBd->status = status | MCF5282_FEC_TxBD_R
+                              | MCF5282_FEC_TxBD_L
+                              | MCF5282_FEC_TxBD_TC;
+        if (nAdded > 1)
+            firstTxBd->status |= MCF5282_FEC_TxBD_R;
+        MCF5282_FEC_TDAR = 0;
+        sc->txBdActiveCount += nAdded;
+      }
 }
 
@@ -700,35 +705,35 @@
     printf("  Tx Interrupts:%-10lu",   sc->txInterrupts);
     printf("Tx Output Waits:%-10lu",   sc->txRawWait);
-    printf("Tx Realignments:%-10lu\n",   sc->txRealign);
-    printf(" Tx RealignByte:%-10lu", sc->txRealignBytes);
-    printf(" Tx Unaccounted:%-10lu", MCF5282_FEC_RMON_T_DROP);
-    printf("Tx Packet Count:%-10lu\n",   MCF5282_FEC_RMON_T_PACKETS);
+    printf("Tx mbuf realign:%-10lu\n", sc->txRealign);
+    printf("Tx realign drop:%-10lu",   sc->txRealignDrop);
+    printf(" Tx Unaccounted:%-10lu",   MCF5282_FEC_RMON_T_DROP);
+    printf("Tx Packet Count:%-10lu\n", MCF5282_FEC_RMON_T_PACKETS);
     printf("   Tx Broadcast:%-10lu",   MCF5282_FEC_RMON_T_BC_PKT);
-    printf("   Tx Multicast:%-10lu", MCF5282_FEC_RMON_T_MC_PKT);
-    printf("CRC/Align error:%-10lu\n",   MCF5282_FEC_RMON_T_CRC_ALIGN);
+    printf("   Tx Multicast:%-10lu",   MCF5282_FEC_RMON_T_MC_PKT);
+    printf("CRC/Align error:%-10lu\n", MCF5282_FEC_RMON_T_CRC_ALIGN);
     printf("   Tx Undersize:%-10lu",   MCF5282_FEC_RMON_T_UNDERSIZE);
-    printf("    Tx Oversize:%-10lu", MCF5282_FEC_RMON_T_OVERSIZE);
-    printf("    Tx Fragment:%-10lu\n",   MCF5282_FEC_RMON_T_FRAG);
+    printf("    Tx Oversize:%-10lu",   MCF5282_FEC_RMON_T_OVERSIZE);
+    printf("    Tx Fragment:%-10lu\n", MCF5282_FEC_RMON_T_FRAG);
     printf("      Tx Jabber:%-10lu",   MCF5282_FEC_RMON_T_JAB);
-    printf("  Tx Collisions:%-10lu", MCF5282_FEC_RMON_T_COL);
-    printf("          Tx 64:%-10lu\n",   MCF5282_FEC_RMON_T_P64);
+    printf("  Tx Collisions:%-10lu",   MCF5282_FEC_RMON_T_COL);
+    printf("          Tx 64:%-10lu\n", MCF5282_FEC_RMON_T_P64);
     printf("      Tx 65-127:%-10lu",   MCF5282_FEC_RMON_T_P65TO127);
-    printf("     Tx 128-255:%-10lu", MCF5282_FEC_RMON_T_P128TO255);
-    printf("     Tx 256-511:%-10lu\n",   MCF5282_FEC_RMON_T_P256TO511);
+    printf("     Tx 128-255:%-10lu",   MCF5282_FEC_RMON_T_P128TO255);
+    printf("     Tx 256-511:%-10lu\n", MCF5282_FEC_RMON_T_P256TO511);
     printf("    Tx 511-1023:%-10lu",   MCF5282_FEC_RMON_T_P512TO1023);
-    printf("   Tx 1024-2047:%-10lu", MCF5282_FEC_RMON_T_P1024TO2047);
-    printf("      Tx >=2048:%-10lu\n",   MCF5282_FEC_RMON_T_P_GTE2048);
+    printf("   Tx 1024-2047:%-10lu",   MCF5282_FEC_RMON_T_P1024TO2047);
+    printf("      Tx >=2048:%-10lu\n", MCF5282_FEC_RMON_T_P_GTE2048);
     printf("      Tx Octets:%-10lu",   MCF5282_FEC_RMON_T_OCTETS);
-    printf("     Tx Dropped:%-10lu", MCF5282_FEC_IEEE_T_DROP);
-    printf("    Tx Frame OK:%-10lu\n",   MCF5282_FEC_IEEE_T_FRAME_OK);
+    printf("     Tx Dropped:%-10lu",   MCF5282_FEC_IEEE_T_DROP);
+    printf("    Tx Frame OK:%-10lu\n", MCF5282_FEC_IEEE_T_FRAME_OK);
     printf(" Tx 1 Collision:%-10lu",   MCF5282_FEC_IEEE_T_1COL);
-    printf("Tx >1 Collision:%-10lu", MCF5282_FEC_IEEE_T_MCOL);
-    printf("    Tx Deferred:%-10lu\n",   MCF5282_FEC_IEEE_T_DEF);
+    printf("Tx >1 Collision:%-10lu",   MCF5282_FEC_IEEE_T_MCOL);
+    printf("    Tx Deferred:%-10lu\n", MCF5282_FEC_IEEE_T_DEF);
     printf(" Late Collision:%-10lu",   MCF5282_FEC_IEEE_T_LCOL);
-    printf(" Excessive Coll:%-10lu", MCF5282_FEC_IEEE_T_EXCOL);
-    printf("  FIFO Underrun:%-10lu\n",   MCF5282_FEC_IEEE_T_MACERR);
+    printf(" Excessive Coll:%-10lu",   MCF5282_FEC_IEEE_T_EXCOL);
+    printf("  FIFO Underrun:%-10lu\n", MCF5282_FEC_IEEE_T_MACERR);
     printf("  Carrier Error:%-10lu",   MCF5282_FEC_IEEE_T_CSERR);
-    printf("   Tx SQE Error:%-10lu", MCF5282_FEC_IEEE_T_SQE);
-    printf("Tx Pause Frames:%-10lu\n",   MCF5282_FEC_IEEE_T_FDXFC);
+    printf("   Tx SQE Error:%-10lu",   MCF5282_FEC_IEEE_T_SQE);
+    printf("Tx Pause Frames:%-10lu\n", MCF5282_FEC_IEEE_T_FDXFC);
     printf("   Tx Octets OK:%-10lu\n", MCF5282_FEC_IEEE_T_OCTETS_OK);
     printf(" EIR:%8.8lx  ",  MCF5282_FEC_EIR);
@@ -747,5 +752,5 @@
          * but this diagnostic is more for when things have backed up.
          */
-        printf("Transmit Buffer Descriptors (Tail %d, Head %d, Active %d):\n",
+        printf("Transmit Buffer Descriptors (Tail %d, Head %d, Unretired %d):\n",
                                                     sc->txBdTail,
                                                     sc->txBdHead,
