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