BSD 4_4 release
[unix-history] / usr / src / sys / vax / if / if_uba.c
index 1246b71..f8f2cf6 100644 (file)
@@ -1,29 +1,56 @@
 /*
 /*
- * 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.
+ * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
+ * All rights reserved.
  *
  *
- *     @(#)if_uba.c    7.2 (Berkeley) %G%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)if_uba.c    7.16 (Berkeley) 12/16/90
  */
 
  */
 
-#include "../machine/pte.h"
+#include "sys/param.h"
+#include "sys/systm.h"
+#include "sys/malloc.h"
+#include "sys/mbuf.h"
+#include "sys/map.h"
+#include "sys/buf.h"
+#include "sys/cmap.h"
+#include "sys/vmmac.h"
+#include "sys/socket.h"
+#include "sys/syslog.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 "../net/if.h"
-
-#include "../vax/mtpr.h"
+#include "../include/pte.h"
+#include "../include/mtpr.h"
 #include "if_uba.h"
 #include "if_uba.h"
-#include "../vaxuba/ubareg.h"
-#include "../vaxuba/ubavar.h"
+#include "../uba/ubareg.h"
+#include "../uba/ubavar.h"
 
 /*
  * Routines supporting UNIBUS network interfaces.
 
 /*
  * Routines supporting UNIBUS network interfaces.
@@ -47,34 +74,36 @@ if_ubaminit(ifu, uban, hlen, nmr, ifr, nr, ifw, nw)
 {
        register caddr_t p;
        caddr_t cp;
 {
        register caddr_t p;
        caddr_t cp;
-       int i, ncl, off;
+       int i, nclbytes, off;
 
        if (hlen)
 
        if (hlen)
-               off = CLBYTES - hlen;
+               off = MCLBYTES - hlen;
        else
                off = 0;
        else
                off = 0;
-       ncl = clrnd(nmr) / CLSIZE;
+       nclbytes = roundup(nmr * NBPG, MCLBYTES);
        if (hlen)
        if (hlen)
-               ncl++;
+               nclbytes += MCLBYTES;
        if (ifr[0].ifrw_addr)
                cp = ifr[0].ifrw_addr - off;
        else {
        if (ifr[0].ifrw_addr)
                cp = ifr[0].ifrw_addr - off;
        else {
-               cp = m_clalloc((nr + nw) * ncl, MPG_SPACE, M_DONTWAIT);
+               cp = (caddr_t)malloc((u_long)((nr + nw) * nclbytes), M_DEVBUF,
+                   M_NOWAIT);
                if (cp == 0)
                        return (0);
                p = cp;
                for (i = 0; i < nr; i++) {
                        ifr[i].ifrw_addr = p + off;
                if (cp == 0)
                        return (0);
                p = cp;
                for (i = 0; i < nr; i++) {
                        ifr[i].ifrw_addr = p + off;
-                       p += ncl * CLBYTES;
+                       p += nclbytes;
                }
                for (i = 0; i < nw; i++) {
                        ifw[i].ifw_base = p;
                        ifw[i].ifw_addr = p + off;
                }
                for (i = 0; i < nw; i++) {
                        ifw[i].ifw_base = p;
                        ifw[i].ifw_addr = p + off;
-                       p += ncl * CLBYTES;
+                       p += nclbytes;
                }
                ifu->iff_hlen = hlen;
                ifu->iff_uban = uban;
                ifu->iff_uba = uba_hd[uban].uh_uba;
                }
                ifu->iff_hlen = hlen;
                ifu->iff_uban = uban;
                ifu->iff_uba = uba_hd[uban].uh_uba;
+               ifu->iff_ubamr = uba_hd[uban].uh_mr;
        }
        for (i = 0; i < nr; i++)
                if (if_ubaalloc(ifu, &ifr[i], nmr) == 0) {
        }
        for (i = 0; i < nr; i++)
                if (if_ubaalloc(ifu, &ifr[i], nmr) == 0) {
@@ -97,10 +126,10 @@ if_ubaminit(ifu, uban, hlen, nmr, ifr, nr, ifw, nw)
        return (1);
 bad:
        while (--nw >= 0)
        return (1);
 bad:
        while (--nw >= 0)
-               ubarelse(ifu->iff_uban, &ifr[nw].ifrw_info);
+               ubarelse(ifu->iff_uban, &ifw[nw].ifw_info);
        while (--nr >= 0)
        while (--nr >= 0)
-               ubarelse(ifu->iff_uban, &ifw[nr].ifw_info);
-       m_pgfree(cp, (nr + nw) * ncl);
+               ubarelse(ifu->iff_uban, &ifr[nr].ifrw_info);
+       free(cp, M_DEVBUF);
        ifr[0].ifrw_addr = 0;
        return (0);
 }
        ifr[0].ifrw_addr = 0;
        return (0);
 }
@@ -126,21 +155,20 @@ if_ubaalloc(ifu, ifrw, nmr)
        ifrw->ifrw_info = info;
        ifrw->ifrw_bdp = UBAI_BDP(info);
        ifrw->ifrw_proto = UBAMR_MRV | (UBAI_BDP(info) << UBAMR_DPSHIFT);
        ifrw->ifrw_info = info;
        ifrw->ifrw_bdp = UBAI_BDP(info);
        ifrw->ifrw_proto = UBAMR_MRV | (UBAI_BDP(info) << UBAMR_DPSHIFT);
-       ifrw->ifrw_mr = &ifu->iff_uba->uba_map[UBAI_MR(info) + (ifu->iff_hlen?
-               1 : 0)];
+       ifrw->ifrw_mr = &ifu->iff_ubamr[UBAI_MR(info) + (ifu->iff_hlen? 1 : 0)];
        return (1);
 }
 
 /*
  * Pull read data off a interface.
        return (1);
 }
 
 /*
  * Pull read data off a interface.
- * Len is length of data, with local net header stripped.
+ * Totlen is length of data, with local net header stripped.
  * Off is non-zero if a trailer protocol was used, and
  * gives the offset of the trailer information.
  * Off is non-zero if a trailer protocol was used, and
  * gives the offset of the trailer information.
- * We copy the trailer information and then all the normal
+ * We copy the header from the trailer and then all the normal
  * 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,
+ * Save a pointer to the interface structure and the total length,
  * 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 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,
@@ -148,50 +176,59 @@ if_ubaalloc(ifu, ifrw, nmr)
  * changed when remapping the transmit buffers).
  */
 struct mbuf *
  * changed when remapping the transmit buffers).
  */
 struct mbuf *
-if_ubaget(ifu, ifr, totlen, off0, ifp)
+if_ubaget(ifu, ifr, totlen, off, ifp)
        struct ifubinfo *ifu;
        register struct ifrw *ifr;
        struct ifubinfo *ifu;
        register struct ifrw *ifr;
-       int totlen, off0;
+       register int totlen;
+       int off;
        struct ifnet *ifp;
 {
        struct mbuf *top, **mp;
        register struct mbuf *m;
        struct ifnet *ifp;
 {
        struct mbuf *top, **mp;
        register struct mbuf *m;
-       int off = off0, len;
        register caddr_t cp = ifr->ifrw_addr + ifu->iff_hlen, pp;
        register caddr_t cp = ifr->ifrw_addr + ifu->iff_hlen, pp;
+       register int len;
+       caddr_t epkt = cp + totlen;
 
        top = 0;
        mp = &top;
 
        top = 0;
        mp = &top;
+       /*
+        * Skip the trailer header (type and trailer length).
+        */
+       if (off) {
+               off += 2 * sizeof(u_short);
+               totlen -= 2 * sizeof(u_short);
+               cp += off;
+       }
+       MGETHDR(m, M_DONTWAIT, MT_DATA);
+       if (m == 0)
+               return ((struct mbuf *)NULL);
+       m->m_pkthdr.rcvif = ifp;
+       m->m_pkthdr.len = totlen;
+       m->m_len = MHLEN;
+
        if (ifr->ifrw_flags & IFRW_W)
                rcv_xmtbuf((struct ifxmt *)ifr);
        if (ifr->ifrw_flags & IFRW_W)
                rcv_xmtbuf((struct ifxmt *)ifr);
+
        while (totlen > 0) {
        while (totlen > 0) {
-               MGET(m, M_DONTWAIT, MT_DATA);
-               if (m == 0) {
-                       m_freem(top);
-                       top = 0;
-                       goto out;
+               if (top) {
+                       MGET(m, M_DONTWAIT, MT_DATA);
+                       if (m == 0) {
+                               m_freem(top);
+                               top = 0;
+                               goto out;
+                       }
+                       m->m_len = MLEN;
                }
                }
-               if (off) {
-                       len = totlen - off;
-                       cp = ifr->ifrw_addr + ifu->iff_hlen + off;
-               } else
-                       len = totlen;
-               if (len >= CLBYTES/2) {
+               len = min(totlen, epkt - cp);
+               if (len >= MINCLSIZE) {
                        struct pte *cpte, *ppte;
                        int x, *ip, i;
 
                        struct pte *cpte, *ppte;
                        int x, *ip, i;
 
-                       /*
-                        * 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)
+                       MCLGET(m, M_DONTWAIT);
+                       if ((m->m_flags & M_EXT) == 0)
                                goto nopage;
                                goto nopage;
-                       m->m_len = MIN(len, CLBYTES);
+                       len = min(len, MCLBYTES);
+                       m->m_len = len;
                        if (!claligned(cp))
                                goto copy;
 
                        if (!claligned(cp))
                                goto copy;
 
@@ -200,15 +237,14 @@ if_ubaget(ifu, ifr, totlen, off0, ifp)
                         * as quick form of copy.  Remap UNIBUS and invalidate.
                         */
                        pp = mtod(m, char *);
                         * as quick form of copy.  Remap UNIBUS and invalidate.
                         */
                        pp = mtod(m, char *);
-                       cpte = &Mbmap[mtocl(cp)*CLSIZE];
-                       ppte = &Mbmap[mtocl(pp)*CLSIZE];
+                       cpte = kvtopte(cp);
+                       ppte = kvtopte(pp);
                        x = btop(cp - ifr->ifrw_addr);
                        ip = (int *)&ifr->ifrw_mr[x];
                        x = btop(cp - ifr->ifrw_addr);
                        ip = (int *)&ifr->ifrw_mr[x];
-                       for (i = 0; i < CLSIZE; i++) {
+                       for (i = 0; i < MCLBYTES/NBPG; i++) {
                                struct pte t;
                                t = *ppte; *ppte++ = *cpte; *cpte = t;
                                struct pte t;
                                t = *ppte; *ppte++ = *cpte; *cpte = t;
-                               *ip++ =
-                                   cpte++->pg_pfnum|ifr->ifrw_proto;
+                               *ip++ = cpte++->pg_pfnum|ifr->ifrw_proto;
                                mtpr(TBIS, cp);
                                cp += NBPG;
                                mtpr(TBIS, (caddr_t)pp);
                                mtpr(TBIS, cp);
                                cp += NBPG;
                                mtpr(TBIS, (caddr_t)pp);
@@ -217,40 +253,24 @@ if_ubaget(ifu, ifr, totlen, off0, ifp)
                        goto nocopy;
                }
 nopage:
                        goto nocopy;
                }
 nopage:
-               m->m_off = MMINOFF;
-               if (ifp) {
+               if (len < m->m_len) {
                        /*
                        /*
-                        * Leave room for ifp.
+                        * Place initial small packet/header at end of mbuf.
                         */
                         */
-                       m->m_len = MIN(MLEN - sizeof(ifp), len);
-                       m->m_off += sizeof(ifp);
-               } else 
-                       m->m_len = MIN(MLEN, len);
+                       if (top == 0 && len + max_linkhdr <= m->m_len)
+                               m->m_data += max_linkhdr;
+                       m->m_len = len;
+               } else
+                       len = m->m_len;
 copy:
 copy:
-               bcopy(cp, mtod(m, caddr_t), (unsigned)m->m_len);
-               cp += m->m_len;
+               bcopy(cp, mtod(m, caddr_t), (unsigned)len);
+               cp += len;
 nocopy:
                *mp = m;
                mp = &m->m_next;
 nocopy:
                *mp = m;
                mp = &m->m_next;
-               if (off) {
-                       /* sort of an ALGOL-W style for statement... */
-                       off += m->m_len;
-                       if (off == totlen) {
-                               cp = ifr->ifrw_addr + ifu->iff_hlen;
-                               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;
-               }
+               totlen -= len;
+               if (cp == epkt)
+                       cp = ifr->ifrw_addr + ifu->iff_hlen;
        }
 out:
        if (ifr->ifrw_flags & IFRW_W)
        }
 out:
        if (ifr->ifrw_flags & IFRW_W)
@@ -273,27 +293,24 @@ rcv_xmtbuf(ifw)
        register struct mbuf *m;
        struct mbuf **mprev;
        register i;
        register struct mbuf *m;
        struct mbuf **mprev;
        register i;
-       int t;
        char *cp;
 
        while (i = ffs((long)ifw->ifw_xswapd)) {
        char *cp;
 
        while (i = ffs((long)ifw->ifw_xswapd)) {
-               cp = ifw->ifw_base + i * CLBYTES;
+               cp = ifw->ifw_base + i * MCLBYTES;
                i--;
                ifw->ifw_xswapd &= ~(1<<i);
                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)
                mprev = &ifw->ifw_xtofree;
                for (m = ifw->ifw_xtofree; m && m->m_next; m = m->m_next)
                        mprev = &m->m_next;
                if (m == NULL)
-                       return;
-               bcopy(mtod(m, caddr_t), cp, CLBYTES);
+                       break;
+               bcopy(mtod(m, caddr_t), cp, MCLBYTES);
                (void) m_free(m);
                *mprev = NULL;
                (void) m_free(m);
                *mprev = NULL;
-               for (t = 0; t < CLSIZE; t++) {
-                       ifw->ifw_mr[i] = ifw->ifw_wmap[i];
-                       i++;
-               }
        }
        }
+       ifw->ifw_xswapd = 0;
+       for (i = 0; i < ifw->ifw_nmr; i++)
+               ifw->ifw_mr[i] = ifw->ifw_wmap[i];
 }
 
 /*
 }
 
 /*
@@ -332,15 +349,16 @@ if_ubaput(ifu, ifw, m)
        while (m) {
                dp = mtod(m, char *);
                if (claligned(cp) && claligned(dp) &&
        while (m) {
                dp = mtod(m, char *);
                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];
+                   (m->m_len == MCLBYTES || m->m_next == (struct mbuf *)0)) {
+                       struct pte *pte;
+                       int *ip;
+
+                       pte = kvtopte(dp);
                        x = btop(cp - ifw->ifw_addr);
                        ip = (int *)&ifw->ifw_mr[x];
                        x = btop(cp - ifw->ifw_addr);
                        ip = (int *)&ifw->ifw_mr[x];
-                       for (i = 0; i < CLSIZE; i++)
-                               *ip++ =
-                                   ifw->ifw_proto | pte++->pg_pfnum;
-                       xswapd |= 1 << (x>>(CLSHIFT-PGSHIFT));
+                       for (i = 0; i < MCLBYTES/NBPG; i++)
+                               *ip++ = ifw->ifw_proto | pte++->pg_pfnum;
+                       xswapd |= 1 << (x>>(MCLSHIFT-PGSHIFT));
                        mp = m->m_next;
                        m->m_next = ifw->ifw_xtofree;
                        ifw->ifw_xtofree = m;
                        mp = m->m_next;
                        m->m_next = ifw->ifw_xtofree;
                        ifw->ifw_xtofree = m;
@@ -361,15 +379,15 @@ if_ubaput(ifu, ifw, m)
         * should be unmapped so original pages will be accessed by the device.
         */
        cc = cp - ifw->ifw_addr;
         * should be unmapped so original pages will be accessed by the device.
         */
        cc = cp - ifw->ifw_addr;
-       x = ((cc - ifu->iff_hlen) + CLBYTES - 1) >> CLSHIFT;
+       x = ((cc - ifu->iff_hlen) + MCLBYTES - 1) >> MCLSHIFT;
        ifw->ifw_xswapd &= ~xswapd;
        while (i = ffs((long)ifw->ifw_xswapd)) {
                i--;
                if (i >= x)
                        break;
                ifw->ifw_xswapd &= ~(1<<i);
        ifw->ifw_xswapd &= ~xswapd;
        while (i = ffs((long)ifw->ifw_xswapd)) {
                i--;
                if (i >= x)
                        break;
                ifw->ifw_xswapd &= ~(1<<i);
-               i *= CLSIZE;
-               for (t = 0; t < CLSIZE; t++) {
+               i *= MCLBYTES/NBPG;
+               for (t = 0; t < MCLBYTES/NBPG; t++) {
                        ifw->ifw_mr[i] = ifw->ifw_wmap[i];
                        i++;
                }
                        ifw->ifw_mr[i] = ifw->ifw_wmap[i];
                        i++;
                }