BSD 4_3 release
[unix-history] / usr / src / sys / vaxif / if_uba.c
index 0ed765c..d5fec32 100644 (file)
@@ -1,20 +1,27 @@
-/*     if_uba.c        6.1     83/07/29        */
+/*
+ * Copyright (c) 1982, 1986 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)if_uba.c    7.1 (Berkeley) 6/5/86
+ */
 
 #include "../machine/pte.h"
 
 
 #include "../machine/pte.h"
 
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/mbuf.h"
-#include "../h/map.h"
-#include "../h/buf.h"
-#include "../h/cmap.h"
-#include "../h/vmmac.h"
-#include "../h/socket.h"
+#include "param.h"
+#include "systm.h"
+#include "mbuf.h"
+#include "map.h"
+#include "buf.h"
+#include "cmap.h"
+#include "vmmac.h"
+#include "socket.h"
+#include "syslog.h"
 
 #include "../net/if.h"
 
 #include "../vax/mtpr.h"
 
 #include "../net/if.h"
 
 #include "../vax/mtpr.h"
-#include "../vaxif/if_uba.h"
+#include "if_uba.h"
 #include "../vaxuba/ubareg.h"
 #include "../vaxuba/ubavar.h"
 
 #include "../vaxuba/ubareg.h"
 #include "../vaxuba/ubavar.h"
 
  * Init UNIBUS for interface on uban whose headers of size hlen are to
  * end on a page boundary.  We allocate a UNIBUS map register for the page
  * with the header, and nmr more UNIBUS map registers for i/o on the adapter,
  * Init UNIBUS for interface on uban whose headers of size hlen are to
  * end on a page boundary.  We allocate a UNIBUS map register for the page
  * with the header, and nmr more UNIBUS map registers for i/o on the adapter,
- * doing this twice: once for reading and once for writing.  We also
+ * doing this once for each read and once for each write buffer.  We also
  * allocate page frames in the mbuffer pool for these pages.
  */
  * allocate page frames in the mbuffer pool for these pages.
  */
-if_ubainit(ifu, uban, hlen, nmr)
-       register struct ifuba *ifu;
-       int uban, hlen, nmr;
+if_ubaminit(ifu, uban, hlen, nmr, ifr, nr, ifw, nw)
+       register struct ifubinfo *ifu;
+       int uban, hlen, nmr, nr, nw;
+       register struct ifrw *ifr;
+       register struct ifxmt *ifw;
 {
 {
-       register caddr_t cp;
-       int i, ncl;
+       register caddr_t p;
+       caddr_t cp;
+       int i, ncl, off;
 
 
-       ncl = clrnd(nmr + CLSIZE) / CLSIZE;
-       if (ifu->ifu_r.ifrw_addr)
-               cp = ifu->ifu_r.ifrw_addr - (CLBYTES - hlen);
+       if (hlen)
+               off = CLBYTES - hlen;
+       else
+               off = 0;
+       ncl = clrnd(nmr) / CLSIZE;
+       if (hlen)
+               ncl++;
+       if (ifr[0].ifrw_addr)
+               cp = ifr[0].ifrw_addr - off;
        else {
        else {
-               cp = m_clalloc(2 * ncl, MPG_SPACE);
+               cp = m_clalloc((nr + nw) * ncl, MPG_SPACE, M_DONTWAIT);
                if (cp == 0)
                        return (0);
                if (cp == 0)
                        return (0);
-               ifu->ifu_r.ifrw_addr = cp + CLBYTES - hlen;
-               ifu->ifu_w.ifrw_addr = ifu->ifu_r.ifrw_addr + ncl * CLBYTES;
-               ifu->ifu_hlen = hlen;
-               ifu->ifu_uban = uban;
-               ifu->ifu_uba = uba_hd[uban].uh_uba;
+               p = cp;
+               for (i = 0; i < nr; i++) {
+                       ifr[i].ifrw_addr = p + off;
+                       p += ncl * CLBYTES;
+               }
+               for (i = 0; i < nw; i++) {
+                       ifw[i].ifw_base = p;
+                       ifw[i].ifw_addr = p + off;
+                       p += ncl * CLBYTES;
+               }
+               ifu->iff_hlen = hlen;
+               ifu->iff_uban = uban;
+               ifu->iff_uba = uba_hd[uban].uh_uba;
+       }
+       for (i = 0; i < nr; i++)
+               if (if_ubaalloc(ifu, &ifr[i], nmr) == 0) {
+                       nr = i;
+                       nw = 0;
+                       goto bad;
+               }
+       for (i = 0; i < nw; i++)
+               if (if_ubaalloc(ifu, &ifw[i].ifrw, nmr) == 0) {
+                       nw = i;
+                       goto bad;
+               }
+       while (--nw >= 0) {
+               for (i = 0; i < nmr; i++)
+                       ifw[nw].ifw_wmap[i] = ifw[nw].ifw_mr[i];
+               ifw[nw].ifw_xswapd = 0;
+               ifw[nw].ifw_flags = IFRW_W;
+               ifw[nw].ifw_nmr = nmr;
        }
        }
-       if (if_ubaalloc(ifu, &ifu->ifu_r, nmr) == 0)
-               goto bad;
-       if (if_ubaalloc(ifu, &ifu->ifu_w, nmr) == 0)
-               goto bad2;
-       for (i = 0; i < nmr; i++)
-               ifu->ifu_wmap[i] = ifu->ifu_w.ifrw_mr[i];
-       ifu->ifu_xswapd = 0;
        return (1);
        return (1);
-bad2:
-       ubarelse(ifu->ifu_uban, &ifu->ifu_r.ifrw_info);
 bad:
 bad:
-       m_pgfree(cp, 2 * ncl);
-       ifu->ifu_r.ifrw_addr = 0;
+       while (--nw >= 0)
+               ubarelse(ifu->iff_uban, &ifr[nw].ifrw_info);
+       while (--nr >= 0)
+               ubarelse(ifu->iff_uban, &ifw[nr].ifw_info);
+       m_pgfree(cp, (nr + nw) * ncl);
+       ifr[0].ifrw_addr = 0;
        return (0);
 }
 
 /*
        return (0);
 }
 
 /*
- * Setup either a ifrw structure by allocating UNIBUS map registers,
+ * Setup an ifrw structure by allocating UNIBUS map registers,
  * possibly a buffered data path, and initializing the fields of
  * the ifrw structure to minimize run-time overhead.
  */
 static
 if_ubaalloc(ifu, ifrw, nmr)
  * possibly a buffered data path, and initializing the fields of
  * the ifrw structure to minimize run-time overhead.
  */
 static
 if_ubaalloc(ifu, ifrw, nmr)
-       struct ifuba *ifu;
+       struct ifubinfo *ifu;
        register struct ifrw *ifrw;
        int nmr;
 {
        register int info;
 
        info =
        register struct ifrw *ifrw;
        int nmr;
 {
        register int info;
 
        info =
-           uballoc(ifu->ifu_uban, ifrw->ifrw_addr, nmr*NBPG + ifu->ifu_hlen,
-               ifu->ifu_flags);
+           uballoc(ifu->iff_uban, ifrw->ifrw_addr, nmr*NBPG + ifu->iff_hlen,
+               ifu->iff_flags);
        if (info == 0)
                return (0);
        ifrw->ifrw_info = info;
        ifrw->ifrw_bdp = UBAI_BDP(info);
        ifrw->ifrw_proto = UBAMR_MRV | (UBAI_BDP(info) << UBAMR_DPSHIFT);
        if (info == 0)
                return (0);
        ifrw->ifrw_info = info;
        ifrw->ifrw_bdp = UBAI_BDP(info);
        ifrw->ifrw_proto = UBAMR_MRV | (UBAI_BDP(info) << UBAMR_DPSHIFT);
-       ifrw->ifrw_mr = &ifu->ifu_uba->uba_map[UBAI_MR(info) + 1];
+       ifrw->ifrw_mr = &ifu->iff_uba->uba_map[UBAI_MR(info) + (ifu->iff_hlen?
+               1 : 0)];
        return (1);
 }
 
        return (1);
 }
 
@@ -102,64 +140,92 @@ if_ubaalloc(ifu, ifrw, nmr)
  * data into mbufs.  When full cluster sized units are present
  * on the interface on cluster boundaries we can get them more
  * easily by remapping, and take advantage of this here.
  * data into mbufs.  When full cluster sized units are present
  * on the interface on cluster boundaries we can get them more
  * easily by remapping, and take advantage of this here.
+ * Prepend a pointer to the interface structure,
+ * so that protocols can determine where incoming packets arrived.
+ * Note: we may be called to receive from a transmit buffer by some
+ * devices.  In that case, we must force normal mapping of the buffer,
+ * so that the correct data will appear (only unibus maps are 
+ * changed when remapping the transmit buffers).
  */
 struct mbuf *
  */
 struct mbuf *
-if_rubaget(ifu, totlen, off0)
-       register struct ifuba *ifu;
+if_ubaget(ifu, ifr, totlen, off0, ifp)
+       struct ifubinfo *ifu;
+       register struct ifrw *ifr;
        int totlen, off0;
        int totlen, off0;
+       struct ifnet *ifp;
 {
 {
-       struct mbuf *top, **mp, *m;
+       struct mbuf *top, **mp;
+       register struct mbuf *m;
        int off = off0, len;
        int off = off0, len;
-       register caddr_t cp = ifu->ifu_r.ifrw_addr + ifu->ifu_hlen;
-
+       register caddr_t cp = ifr->ifrw_addr + ifu->iff_hlen, pp;
 
        top = 0;
        mp = &top;
 
        top = 0;
        mp = &top;
+       if (ifr->ifrw_flags & IFRW_W)
+               rcv_xmtbuf((struct ifxmt *)ifr);
        while (totlen > 0) {
                MGET(m, M_DONTWAIT, MT_DATA);
        while (totlen > 0) {
                MGET(m, M_DONTWAIT, MT_DATA);
-               if (m == 0)
-                       goto bad;
+               if (m == 0) {
+                       m_freem(top);
+                       top = 0;
+                       goto out;
+               }
                if (off) {
                        len = totlen - off;
                if (off) {
                        len = totlen - off;
-                       cp = ifu->ifu_r.ifrw_addr + ifu->ifu_hlen + off;
+                       cp = ifr->ifrw_addr + ifu->iff_hlen + off;
                } else
                        len = totlen;
                } else
                        len = totlen;
-               if (len >= CLBYTES) {
-                       struct mbuf *p;
+               if (len >= CLBYTES/2) {
                        struct pte *cpte, *ppte;
                        int x, *ip, i;
 
                        struct pte *cpte, *ppte;
                        int x, *ip, i;
 
-                       MCLGET(p, 1);
-                       if (p == 0)
+                       /*
+                        * If doing the first mbuf and
+                        * the interface pointer hasn't been put in,
+                        * put it in a separate mbuf to preserve alignment.
+                        */
+                       if (ifp) {
+                               len = 0;
+                               goto nopage;
+                       }
+                       MCLGET(m);
+                       if (m->m_len != CLBYTES)
                                goto nopage;
                                goto nopage;
-                       len = m->m_len = CLBYTES;
-                       m->m_off = (int)p - (int)m;
+                       m->m_len = MIN(len, CLBYTES);
                        if (!claligned(cp))
                                goto copy;
 
                        /*
                        if (!claligned(cp))
                                goto copy;
 
                        /*
-                        * Switch pages mapped to UNIBUS with new page p,
+                        * Switch pages mapped to UNIBUS with new page pp,
                         * as quick form of copy.  Remap UNIBUS and invalidate.
                         */
                         * as quick form of copy.  Remap UNIBUS and invalidate.
                         */
+                       pp = mtod(m, char *);
                        cpte = &Mbmap[mtocl(cp)*CLSIZE];
                        cpte = &Mbmap[mtocl(cp)*CLSIZE];
-                       ppte = &Mbmap[mtocl(p)*CLSIZE];
-                       x = btop(cp - ifu->ifu_r.ifrw_addr);
-                       ip = (int *)&ifu->ifu_r.ifrw_mr[x];
+                       ppte = &Mbmap[mtocl(pp)*CLSIZE];
+                       x = btop(cp - ifr->ifrw_addr);
+                       ip = (int *)&ifr->ifrw_mr[x];
                        for (i = 0; i < CLSIZE; i++) {
                                struct pte t;
                                t = *ppte; *ppte++ = *cpte; *cpte = t;
                                *ip++ =
                        for (i = 0; i < CLSIZE; i++) {
                                struct pte t;
                                t = *ppte; *ppte++ = *cpte; *cpte = t;
                                *ip++ =
-                                   cpte++->pg_pfnum|ifu->ifu_r.ifrw_proto;
+                                   cpte++->pg_pfnum|ifr->ifrw_proto;
                                mtpr(TBIS, cp);
                                cp += NBPG;
                                mtpr(TBIS, cp);
                                cp += NBPG;
-                               mtpr(TBIS, (caddr_t)p);
-                               p += NBPG / sizeof (*p);
+                               mtpr(TBIS, (caddr_t)pp);
+                               pp += NBPG;
                        }
                        goto nocopy;
                }
 nopage:
                        }
                        goto nocopy;
                }
 nopage:
-               m->m_len = MIN(MLEN, len);
                m->m_off = MMINOFF;
                m->m_off = MMINOFF;
+               if (ifp) {
+                       /*
+                        * Leave room for ifp.
+                        */
+                       m->m_len = MIN(MLEN - sizeof(ifp), len);
+                       m->m_off += sizeof(ifp);
+               } else 
+                       m->m_len = MIN(MLEN, len);
 copy:
                bcopy(cp, mtod(m, caddr_t), (unsigned)m->m_len);
                cp += m->m_len;
 copy:
                bcopy(cp, mtod(m, caddr_t), (unsigned)m->m_len);
                cp += m->m_len;
@@ -170,17 +236,78 @@ nocopy:
                        /* sort of an ALGOL-W style for statement... */
                        off += m->m_len;
                        if (off == totlen) {
                        /* sort of an ALGOL-W style for statement... */
                        off += m->m_len;
                        if (off == totlen) {
-                               cp = ifu->ifu_r.ifrw_addr + ifu->ifu_hlen;
+                               cp = ifr->ifrw_addr + ifu->iff_hlen;
                                off = 0;
                                totlen = off0;
                        }
                } else
                        totlen -= m->m_len;
                                off = 0;
                                totlen = off0;
                        }
                } else
                        totlen -= m->m_len;
+               if (ifp) {
+                       /*
+                        * Prepend interface pointer to first mbuf.
+                        */
+                       m->m_len += sizeof(ifp);
+                       m->m_off -= sizeof(ifp);
+                       *(mtod(m, struct ifnet **)) = ifp;
+                       ifp = (struct ifnet *)0;
+               }
        }
        }
+out:
+       if (ifr->ifrw_flags & IFRW_W)
+               restor_xmtbuf((struct ifxmt *)ifr);
        return (top);
        return (top);
-bad:
-       m_freem(top);
-       return (0);
+}
+
+/*
+ * Change the mapping on a transmit buffer so that if_ubaget may
+ * receive from that buffer.  Copy data from any pages mapped to Unibus
+ * into the pages mapped to normal kernel virtual memory, so that
+ * they can be accessed and swapped as usual.  We take advantage
+ * of the fact that clusters are placed on the xtofree list
+ * in inverse order, finding the last one.
+ */
+static
+rcv_xmtbuf(ifw)
+       register struct ifxmt *ifw;
+{
+       register struct mbuf *m;
+       struct mbuf **mprev;
+       register i;
+       int t;
+       char *cp;
+
+       while (i = ffs((long)ifw->ifw_xswapd)) {
+               cp = ifw->ifw_base + i * CLBYTES;
+               i--;
+               ifw->ifw_xswapd &= ~(1<<i);
+               i *= CLSIZE;
+               mprev = &ifw->ifw_xtofree;
+               for (m = ifw->ifw_xtofree; m && m->m_next; m = m->m_next)
+                       mprev = &m->m_next;
+               if (m == NULL)
+                       panic("rcv_xmtbuf");
+               bcopy(mtod(m, caddr_t), cp, CLBYTES);
+               (void) m_free(m);
+               *mprev = NULL;
+               for (t = 0; t < CLSIZE; t++) {
+                       ifw->ifw_mr[i] = ifw->ifw_wmap[i];
+                       i++;
+               }
+       }
+}
+
+/*
+ * Put a transmit buffer back together after doing an if_ubaget on it,
+ * which may have swapped pages.
+ */
+static
+restor_xmtbuf(ifw)
+       register struct ifxmt *ifw;
+{
+       register i;
+
+       for (i = 0; i < ifw->ifw_nmr; i++)
+               ifw->ifw_wmap[i] = ifw->ifw_mr[i];
 }
 
 /*
 }
 
 /*
@@ -190,31 +317,33 @@ bad:
  * header which is copied to be in the mapped, aligned
  * i/o space.
  */
  * header which is copied to be in the mapped, aligned
  * i/o space.
  */
-if_wubaput(ifu, m)
-       register struct ifuba *ifu;
+if_ubaput(ifu, ifw, m)
+       struct ifubinfo *ifu;
+       register struct ifxmt *ifw;
        register struct mbuf *m;
 {
        register struct mbuf *mp;
        register caddr_t cp, dp;
        register int i;
        int xswapd = 0;
        register struct mbuf *m;
 {
        register struct mbuf *mp;
        register caddr_t cp, dp;
        register int i;
        int xswapd = 0;
-       int x, cc;
+       int x, cc, t;
 
 
-       cp = ifu->ifu_w.ifrw_addr;
+       cp = ifw->ifw_addr;
        while (m) {
                dp = mtod(m, char *);
        while (m) {
                dp = mtod(m, char *);
-               if (claligned(cp) && claligned(dp) && m->m_len == CLBYTES) {
+               if (claligned(cp) && claligned(dp) &&
+                   (m->m_len == CLBYTES || m->m_next == (struct mbuf *)0)) {
                        struct pte *pte; int *ip;
                        pte = &Mbmap[mtocl(dp)*CLSIZE];
                        struct pte *pte; int *ip;
                        pte = &Mbmap[mtocl(dp)*CLSIZE];
-                       x = btop(cp - ifu->ifu_w.ifrw_addr);
-                       ip = (int *)&ifu->ifu_w.ifrw_mr[x];
+                       x = btop(cp - ifw->ifw_addr);
+                       ip = (int *)&ifw->ifw_mr[x];
                        for (i = 0; i < CLSIZE; i++)
                                *ip++ =
                        for (i = 0; i < CLSIZE; i++)
                                *ip++ =
-                                   ifu->ifu_w.ifrw_proto | pte++->pg_pfnum;
+                                   ifw->ifw_proto | pte++->pg_pfnum;
                        xswapd |= 1 << (x>>(CLSHIFT-PGSHIFT));
                        mp = m->m_next;
                        xswapd |= 1 << (x>>(CLSHIFT-PGSHIFT));
                        mp = m->m_next;
-                       m->m_next = ifu->ifu_xtofree;
-                       ifu->ifu_xtofree = m;
+                       m->m_next = ifw->ifw_xtofree;
+                       ifw->ifw_xtofree = m;
                        cp += m->m_len;
                } else {
                        bcopy(mtod(m, caddr_t), cp, (unsigned)m->m_len);
                        cp += m->m_len;
                } else {
                        bcopy(mtod(m, caddr_t), cp, (unsigned)m->m_len);
@@ -225,27 +354,26 @@ if_wubaput(ifu, m)
        }
 
        /*
        }
 
        /*
-        * Xswapd is the set of clusters we just mapped out.  Ifu->ifu_xswapd
+        * Xswapd is the set of clusters we just mapped out.  Ifu->iff_xswapd
         * is the set of clusters mapped out from before.  We compute
         * the number of clusters involved in this operation in x.
         * Clusters mapped out before and involved in this operation
         * should be unmapped so original pages will be accessed by the device.
         */
         * is the set of clusters mapped out from before.  We compute
         * the number of clusters involved in this operation in x.
         * Clusters mapped out before and involved in this operation
         * should be unmapped so original pages will be accessed by the device.
         */
-       cc = cp - ifu->ifu_w.ifrw_addr;
-       x = ((cc - ifu->ifu_hlen) + CLBYTES - 1) >> CLSHIFT;
-       ifu->ifu_xswapd &= ~xswapd;
-       xswapd &= ~ifu->ifu_xswapd;
-       while (i = ffs(ifu->ifu_xswapd)) {
+       cc = cp - ifw->ifw_addr;
+       x = ((cc - ifu->iff_hlen) + CLBYTES - 1) >> CLSHIFT;
+       ifw->ifw_xswapd &= ~xswapd;
+       while (i = ffs((long)ifw->ifw_xswapd)) {
                i--;
                if (i >= x)
                        break;
                i--;
                if (i >= x)
                        break;
-               ifu->ifu_xswapd &= ~(1<<i);
+               ifw->ifw_xswapd &= ~(1<<i);
                i *= CLSIZE;
                i *= CLSIZE;
-               for (x = 0; x < CLSIZE; x++) {
-                       ifu->ifu_w.ifrw_mr[i] = ifu->ifu_wmap[i];
+               for (t = 0; t < CLSIZE; t++) {
+                       ifw->ifw_mr[i] = ifw->ifw_wmap[i];
                        i++;
                }
        }
                        i++;
                }
        }
-       ifu->ifu_xswapd |= xswapd;
+       ifw->ifw_xswapd |= xswapd;
        return (cc);
 }
        return (cc);
 }