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