fix includes
[unix-history] / usr / src / sys / vax / if / if_uba.c
CommitLineData
d2cc167c 1/* if_uba.c 4.13 82/10/09 */
0468e5fa
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/mbuf.h"
6#include "../h/map.h"
7#include "../h/pte.h"
8a13b737 8#include "../h/buf.h"
0468e5fa
BJ
9#include "../h/ubareg.h"
10#include "../h/ubavar.h"
11#include "../h/cmap.h"
12#include "../h/mtpr.h"
8a13b737 13#include "../h/vmmac.h"
ee787340 14#include "../h/socket.h"
8a13b737 15#include "../net/if.h"
d2cc167c 16#include "../vaxif/if_uba.h"
0468e5fa
BJ
17
18/*
19 * Routines supporting UNIBUS network interfaces.
20 *
21 * TODO:
22 * Support interfaces using only one BDP statically.
23 */
24
25/*
26 * Init UNIBUS for interface on uban whose headers of size hlen are to
27 * end on a page boundary. We allocate a UNIBUS map register for the page
28 * with the header, and nmr more UNIBUS map registers for i/o on the adapter,
29 * doing this twice: once for reading and once for writing. We also
30 * allocate page frames in the mbuffer pool for these pages.
31 */
32if_ubainit(ifu, uban, hlen, nmr)
33 register struct ifuba *ifu;
34 int uban, hlen, nmr;
35{
97da2a42
BJ
36 register caddr_t cp;
37 int i, ncl;
0468e5fa 38
97da2a42 39 ncl = clrnd(nmr + CLSIZE) / CLSIZE;
176c481f
BJ
40 if (ifu->ifu_r.ifrw_addr)
41 cp = ifu->ifu_r.ifrw_addr - (CLBYTES - hlen);
42 else {
43 cp = m_clalloc(2 * ncl, MPG_SPACE);
44 if (cp == 0)
45 return (0);
46 ifu->ifu_r.ifrw_addr = cp + CLBYTES - hlen;
47 ifu->ifu_w.ifrw_addr = ifu->ifu_r.ifrw_addr + ncl * CLBYTES;
48 ifu->ifu_hlen = hlen;
49 ifu->ifu_uban = uban;
50 ifu->ifu_uba = uba_hd[uban].uh_uba;
51 }
b454c3ea 52 if (if_ubaalloc(ifu, &ifu->ifu_r, nmr) == 0)
0468e5fa 53 goto bad;
b454c3ea 54 if (if_ubaalloc(ifu, &ifu->ifu_w, nmr) == 0)
0468e5fa 55 goto bad2;
b454c3ea 56 for (i = 0; i < nmr; i++)
97da2a42 57 ifu->ifu_wmap[i] = ifu->ifu_w.ifrw_mr[i];
8a13b737 58 ifu->ifu_xswapd = 0;
0468e5fa
BJ
59 return (1);
60bad2:
8a13b737 61 ubarelse(ifu->ifu_uban, &ifu->ifu_r.ifrw_info);
0468e5fa 62bad:
97da2a42 63 m_pgfree(cp, 2 * ncl);
176c481f 64 ifu->ifu_r.ifrw_addr = 0;
0468e5fa
BJ
65 return (0);
66}
67
68/*
69 * Setup either a ifrw structure by allocating UNIBUS map registers,
d6391cba
SL
70 * possibly a buffered data path, and initializing the fields of
71 * the ifrw structure to minimize run-time overhead.
0468e5fa
BJ
72 */
73static
b454c3ea 74if_ubaalloc(ifu, ifrw, nmr)
0468e5fa
BJ
75 struct ifuba *ifu;
76 register struct ifrw *ifrw;
b454c3ea 77 int nmr;
0468e5fa
BJ
78{
79 register int info;
80
81 info =
b454c3ea 82 uballoc(ifu->ifu_uban, ifrw->ifrw_addr, nmr*NBPG + ifu->ifu_hlen,
d6391cba 83 ifu->ifu_flags);
0468e5fa 84 if (info == 0)
8a13b737 85 return (0);
0468e5fa
BJ
86 ifrw->ifrw_info = info;
87 ifrw->ifrw_bdp = UBAI_BDP(info);
97da2a42 88 ifrw->ifrw_proto = UBAMR_MRV | (UBAI_BDP(info) << UBAMR_DPSHIFT);
8a13b737
BJ
89 ifrw->ifrw_mr = &ifu->ifu_uba->uba_map[UBAI_MR(info) + 1];
90 return (1);
0468e5fa
BJ
91}
92
93/*
f1b2fa5b
BJ
94 * Pull read data off a interface.
95 * Len is length of data, with local net header stripped.
96 * Off is non-zero if a trailer protocol was used, and
97 * gives the offset of the trailer information.
98 * We copy the trailer information and then all the normal
99 * data into mbufs. When full cluster sized units are present
100 * on the interface on cluster boundaries we can get them more
101 * easily by remapping, and take advantage of this here.
0468e5fa
BJ
102 */
103struct mbuf *
f1b2fa5b 104if_rubaget(ifu, totlen, off0)
0468e5fa 105 register struct ifuba *ifu;
f1b2fa5b 106 int totlen, off0;
0468e5fa 107{
b454c3ea
BJ
108 struct mbuf *top, **mp, *m;
109 int off = off0, len;
97da2a42 110 register caddr_t cp = ifu->ifu_r.ifrw_addr + ifu->ifu_hlen;
0468e5fa 111
0468e5fa 112
f1b2fa5b
BJ
113 top = 0;
114 mp = &top;
115 while (totlen > 0) {
0468e5fa
BJ
116 MGET(m, 0);
117 if (m == 0)
8a13b737 118 goto bad;
f1b2fa5b
BJ
119 if (off) {
120 len = totlen - off;
121 cp = ifu->ifu_r.ifrw_addr + ifu->ifu_hlen + off;
122 } else
123 len = totlen;
97da2a42 124 if (len >= CLBYTES) {
b454c3ea 125 struct mbuf *p;
0468e5fa 126 struct pte *cpte, *ppte;
b454c3ea 127 int x, *ip, i;
0468e5fa
BJ
128
129 MCLGET(p, 1);
130 if (p == 0)
131 goto nopage;
97da2a42 132 len = m->m_len = CLBYTES;
0468e5fa 133 m->m_off = (int)p - (int)m;
b454c3ea 134 if (!claligned(cp))
0468e5fa
BJ
135 goto copy;
136
137 /*
b454c3ea
BJ
138 * Switch pages mapped to UNIBUS with new page p,
139 * as quick form of copy. Remap UNIBUS and invalidate.
0468e5fa 140 */
b454c3ea
BJ
141 cpte = &Mbmap[mtocl(cp)*CLSIZE];
142 ppte = &Mbmap[mtocl(p)*CLSIZE];
8a13b737 143 x = btop(cp - ifu->ifu_r.ifrw_addr);
97da2a42 144 ip = (int *)&ifu->ifu_r.ifrw_mr[x];
0468e5fa
BJ
145 for (i = 0; i < CLSIZE; i++) {
146 struct pte t;
b454c3ea 147 t = *ppte; *ppte++ = *cpte; *cpte = t;
0468e5fa 148 *ip++ =
8a13b737 149 cpte++->pg_pfnum|ifu->ifu_r.ifrw_proto;
0468e5fa 150 mtpr(TBIS, cp);
8a13b737 151 cp += NBPG;
0468e5fa 152 mtpr(TBIS, (caddr_t)p);
8a13b737 153 p += NBPG / sizeof (*p);
0468e5fa
BJ
154 }
155 goto nocopy;
156 }
157nopage:
158 m->m_len = MIN(MLEN, len);
159 m->m_off = MMINOFF;
160copy:
161 bcopy(cp, mtod(m, caddr_t), (unsigned)m->m_len);
162 cp += m->m_len;
163nocopy:
f1b2fa5b
BJ
164 *mp = m;
165 mp = &m->m_next;
166 if (off) {
b454c3ea 167 /* sort of an ALGOL-W style for statement... */
f1b2fa5b
BJ
168 off += m->m_len;
169 if (off == totlen) {
170 cp = ifu->ifu_r.ifrw_addr + ifu->ifu_hlen;
171 off = 0;
97da2a42 172 totlen = off0;
f1b2fa5b 173 }
97da2a42 174 } else
c7a4fdbf 175 totlen -= m->m_len;
0468e5fa
BJ
176 }
177 return (top);
178bad:
179 m_freem(top);
180 return (0);
181}
182
183/*
184 * Map a chain of mbufs onto a network interface
185 * in preparation for an i/o operation.
186 * The argument chain of mbufs includes the local network
187 * header which is copied to be in the mapped, aligned
188 * i/o space.
189 */
190if_wubaput(ifu, m)
191 register struct ifuba *ifu;
192 register struct mbuf *m;
193{
194 register struct mbuf *mp;
195 register caddr_t cp, dp;
196 register int i;
b454c3ea
BJ
197 int xswapd = 0;
198 int x, cc;
0468e5fa 199
0468e5fa
BJ
200 cp = ifu->ifu_w.ifrw_addr;
201 while (m) {
202 dp = mtod(m, char *);
97da2a42 203 if (claligned(cp) && claligned(dp) && m->m_len == CLBYTES) {
0468e5fa 204 struct pte *pte; int *ip;
b454c3ea 205 pte = &Mbmap[mtocl(dp)*CLSIZE];
0468e5fa 206 x = btop(cp - ifu->ifu_w.ifrw_addr);
97da2a42 207 ip = (int *)&ifu->ifu_w.ifrw_mr[x];
0468e5fa
BJ
208 for (i = 0; i < CLSIZE; i++)
209 *ip++ =
210 ifu->ifu_w.ifrw_proto | pte++->pg_pfnum;
97da2a42 211 xswapd |= 1 << (x>>(CLSHIFT-PGSHIFT));
b454c3ea
BJ
212 mp = m->m_next;
213 m->m_next = ifu->ifu_xtofree;
214 ifu->ifu_xtofree = m;
215 cp += m->m_len;
216 } else {
0468e5fa 217 bcopy(mtod(m, caddr_t), cp, (unsigned)m->m_len);
b454c3ea
BJ
218 cp += m->m_len;
219 MFREE(m, mp);
220 }
0468e5fa
BJ
221 m = mp;
222 }
b454c3ea
BJ
223
224 /*
225 * Xswapd is the set of clusters we just mapped out. Ifu->ifu_xswapd
226 * is the set of clusters mapped out from before. We compute
227 * the number of clusters involved in this operation in x.
228 * Clusters mapped out before and involved in this operation
229 * should be unmapped so original pages will be accessed by the device.
230 */
231 cc = cp - ifu->ifu_w.ifrw_addr;
232 x = ((cc - ifu->ifu_hlen) + CLBYTES - 1) >> CLSHIFT;
97da2a42 233 ifu->ifu_xswapd &= ~xswapd;
0468e5fa 234 xswapd &= ~ifu->ifu_xswapd;
97da2a42
BJ
235 while (i = ffs(ifu->ifu_xswapd)) {
236 i--;
237 if (i >= x)
238 break;
239 ifu->ifu_xswapd &= ~(1<<i);
240 i *= CLSIZE;
241 for (x = 0; x < CLSIZE; x++) {
242 ifu->ifu_w.ifrw_mr[i] = ifu->ifu_wmap[i];
243 i++;
0468e5fa 244 }
97da2a42 245 }
b454c3ea
BJ
246 ifu->ifu_xswapd |= xswapd;
247 return (cc);
0468e5fa 248}