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