sun merge
[unix-history] / usr / src / sys / vax / mba / mba.c
CommitLineData
961945a8 1/* mba.c 4.30 82/12/17 */
b81fd3e8 2
443c8066
BJ
3#include "mba.h"
4#if NMBA > 0
b81fd3e8 5/*
d565635a 6 * Massbus driver, arbitrates a massbus among attached devices.
b81fd3e8 7 */
961945a8
SL
8#include "../machine/pte.h"
9
b5ad10c3 10#include "../h/param.h"
b81fd3e8
BJ
11#include "../h/systm.h"
12#include "../h/dk.h"
b5ad10c3
BJ
13#include "../h/buf.h"
14#include "../h/conf.h"
b5ad10c3
BJ
15#include "../h/dir.h"
16#include "../h/user.h"
17#include "../h/proc.h"
b5ad10c3 18#include "../h/map.h"
c895c266 19#include "../vax/mtpr.h"
b5ad10c3
BJ
20#include "../h/vm.h"
21
c895c266
BJ
22#include "../vaxmba/mbareg.h"
23#include "../vaxmba/mbavar.h"
24
d565635a 25char mbsr_bits[] = MBSR_BITS;
b5ad10c3 26/*
b81fd3e8 27 * Start activity on a massbus device.
89bd2f01 28 * We are given the device's mba_device structure and activate
b81fd3e8
BJ
29 * the device via the unit start routine. The unit start
30 * routine may indicate that it is finished (e.g. if the operation
31 * was a ``sense'' on a tape drive), that the (multi-ported) unit
32 * is busy (we will get an interrupt later), that it started the
33 * unit (e.g. for a non-data transfer operation), or that it has
34 * set up a data transfer operation and we should start the massbus adaptor.
b5ad10c3 35 */
b81fd3e8 36mbustart(mi)
89bd2f01 37 register struct mba_device *mi;
b81fd3e8 38{
b81fd3e8
BJ
39 register struct buf *bp; /* i/o operation at head of queue */
40 register struct mba_hd *mhp; /* header for mba device is on */
41
b81fd3e8
BJ
42loop:
43 /*
44 * Get the first thing to do off device queue.
45 */
46 bp = mi->mi_tab.b_actf;
47 if (bp == NULL)
48 return;
0d7a9a27
SL
49 /*
50 * Make sure the drive is still there before starting it up.
51 */
52 if ((mi->mi_drv->mbd_dt & MBDT_TYPE) == 0) {
53 printf("%s%d: nonexistent\n", mi->mi_driver->md_dname,
54 dkunit(bp));
55 mi->mi_alive = 0;
56 mi->mi_tab.b_actf = bp->av_forw;
57 mi->mi_tab.b_active = 0;
58 mi->mi_tab.b_errcnt = 0;
59 bp->b_flags |= B_ERROR;
60 iodone(bp);
61 goto loop;
62 }
b81fd3e8
BJ
63 /*
64 * Let the drivers unit start routine have at it
65 * and then process the request further, per its instructions.
66 */
67 switch ((*mi->mi_driver->md_ustart)(mi)) {
68
69 case MBU_NEXT: /* request is complete (e.g. ``sense'') */
b81fd3e8 70 mi->mi_tab.b_active = 0;
cda4cdd4 71 mi->mi_tab.b_errcnt = 0;
b81fd3e8
BJ
72 mi->mi_tab.b_actf = bp->av_forw;
73 iodone(bp);
74 goto loop;
75
76 case MBU_DODATA: /* all ready to do data transfer */
b81fd3e8 77 /*
89bd2f01 78 * Queue the device mba_device structure on the massbus
b81fd3e8
BJ
79 * mba_hd structure for processing as soon as the
80 * data path is available.
81 */
82 mhp = mi->mi_hd;
83 mi->mi_forw = NULL;
84 if (mhp->mh_actf == NULL)
85 mhp->mh_actf = mi;
86 else
87 mhp->mh_actl->mi_forw = mi;
88 mhp->mh_actl = mi;
89 /*
90 * If data path is idle, start transfer now.
91 * In any case the device is ``active'' waiting for the
92 * data to transfer.
93 */
3dbaa9da 94 mi->mi_tab.b_active = 1;
b81fd3e8
BJ
95 if (mhp->mh_active == 0)
96 mbstart(mhp);
b81fd3e8
BJ
97 return;
98
99 case MBU_STARTED: /* driver started a non-data transfer */
b81fd3e8
BJ
100 /*
101 * Mark device busy during non-data transfer
102 * and count this as a ``seek'' on the device.
103 */
fc4d0a69 104 if (mi->mi_dk >= 0) {
b81fd3e8 105 dk_seek[mi->mi_dk]++;
fc4d0a69
BJ
106 dk_busy |= (1 << mi->mi_dk);
107 }
b81fd3e8
BJ
108 mi->mi_tab.b_active = 1;
109 return;
110
111 case MBU_BUSY: /* dual port drive busy */
b81fd3e8
BJ
112 /*
113 * We mark the device structure so that when an
114 * interrupt occurs we will know to restart the unit.
115 */
116 mi->mi_tab.b_flags |= B_BUSY;
117 return;
118
119 default:
120 panic("mbustart");
121 }
e1e57888 122}
b81fd3e8
BJ
123
124/*
125 * Start an i/o operation on the massbus specified by the argument.
126 * We peel the first operation off its queue and insure that the drive
127 * is present and on-line. We then use the drivers start routine
128 * (if any) to prepare the drive, setup the massbus map for the transfer
129 * and start the transfer.
130 */
131mbstart(mhp)
132 register struct mba_hd *mhp;
133{
89bd2f01 134 register struct mba_device *mi;
b81fd3e8 135 struct buf *bp;
b81fd3e8 136 register struct mba_regs *mbp;
7bd5c7e4 137 register int com;
b81fd3e8 138
b81fd3e8
BJ
139loop:
140 /*
141 * Look for an operation at the front of the queue.
142 */
cda4cdd4 143 if ((mi = mhp->mh_actf) == NULL) {
b81fd3e8 144 return;
cda4cdd4 145 }
b81fd3e8 146 if ((bp = mi->mi_tab.b_actf) == NULL) {
b81fd3e8
BJ
147 mhp->mh_actf = mi->mi_forw;
148 goto loop;
149 }
150 /*
151 * If this device isn't present and on-line, then
152 * we screwed up, and can't really do the operation.
64614526
BJ
153 * Only check for non-tapes because tape drivers check
154 * ONLINE themselves and because TU78 registers are
155 * different.
b81fd3e8 156 */
0d7a9a27 157 if (((com = mi->mi_drv->mbd_dt) & MBDT_TAP) == 0)
d565635a 158 if ((mi->mi_drv->mbd_ds & MBDS_DREADY) != MBDS_DREADY) {
0d7a9a27
SL
159 if ((com & MBDT_TYPE) == 0) {
160 mi->mi_alive = 0;
161 printf("%s%d: nonexistent\n", mi->mi_driver->md_dname,
162 dkunit(bp));
163 } else
164 printf("%s%d: not ready\n", mi->mi_driver->md_dname,
165 dkunit(bp));
b81fd3e8 166 mi->mi_tab.b_actf = bp->av_forw;
3dbaa9da
BJ
167 mi->mi_tab.b_errcnt = 0;
168 mi->mi_tab.b_active = 0;
b81fd3e8
BJ
169 bp->b_flags |= B_ERROR;
170 iodone(bp);
171 goto loop;
172 }
173 /*
174 * We can do the operation; mark the massbus active
175 * and let the device start routine setup any necessary
176 * device state for the transfer (e.g. desired cylinder, etc
177 * on disks).
178 */
179 mhp->mh_active = 1;
7bd5c7e4
BJ
180 if (mi->mi_driver->md_start) {
181 if ((com = (*mi->mi_driver->md_start)(mi)) == 0)
182 com = (bp->b_flags & B_READ) ?
183 MB_RCOM|MB_GO : MB_WCOM|MB_GO;
184 } else
185 com = (bp->b_flags & B_READ) ? MB_RCOM|MB_GO : MB_WCOM|MB_GO;
b81fd3e8
BJ
186
187 /*
188 * Setup the massbus control and map registers and start
189 * the transfer.
190 */
b81fd3e8
BJ
191 mbp = mi->mi_mba;
192 mbp->mba_sr = -1; /* conservative */
193 mbp->mba_var = mbasetup(mi);
194 mbp->mba_bcr = -bp->b_bcount;
7bd5c7e4 195 mi->mi_drv->mbd_cs1 = com;
b81fd3e8
BJ
196 if (mi->mi_dk >= 0) {
197 dk_busy |= 1 << mi->mi_dk;
198 dk_xfer[mi->mi_dk]++;
199 dk_wds[mi->mi_dk] += bp->b_bcount >> 6;
200 }
201}
b5ad10c3 202
b81fd3e8
BJ
203/*
204 * Take an interrupt off of massbus mbanum,
205 * and dispatch to drivers as appropriate.
206 */
207mbintr(mbanum)
208 int mbanum;
209{
210 register struct mba_hd *mhp = &mba_hd[mbanum];
211 register struct mba_regs *mbp = mhp->mh_mba;
89bd2f01 212 register struct mba_device *mi;
80e7c811 213 register struct buf *bp;
b81fd3e8 214 register int drive;
cda4cdd4 215 int mbasr, as;
0d7a9a27 216 extern struct mba_device *mbaconfig();
b81fd3e8
BJ
217
218 /*
219 * Read out the massbus status register
220 * and attention status register and clear
221 * the bits in same by writing them back.
222 */
cda4cdd4
BJ
223 mbasr = mbp->mba_sr;
224 mbp->mba_sr = mbasr;
c9e9b65b 225#if VAX750
d565635a 226 if (mbasr&MBSR_CBHUNG) {
16e4017f
BJ
227 printf("mba%d: control bus hung\n", mbanum);
228 panic("cbhung");
229 }
c9e9b65b 230#endif
b81fd3e8 231 /* note: the mbd_as register is shared between drives */
cda4cdd4 232 as = mbp->mba_drv[0].mbd_as & 0xff;
b81fd3e8 233 mbp->mba_drv[0].mbd_as = as;
b81fd3e8 234
b81fd3e8
BJ
235 /*
236 * If the mba was active, process the data transfer
237 * complete interrupt; otherwise just process units which
238 * are now finished.
239 */
240 if (mhp->mh_active) {
b81fd3e8
BJ
241 /*
242 * Clear attention status for drive whose data
d565635a
BJ
243 * transfer related operation completed,
244 * and give the dtint driver
b81fd3e8
BJ
245 * routine a chance to say what is next.
246 */
247 mi = mhp->mh_actf;
248 as &= ~(1 << mi->mi_drive);
249 dk_busy &= ~(1 << mi->mi_dk);
250 bp = mi->mi_tab.b_actf;
d565635a 251 switch ((*mi->mi_driver->md_dtint)(mi, mbasr)) {
b81fd3e8
BJ
252
253 case MBD_DONE: /* all done, for better or worse */
b81fd3e8
BJ
254 /*
255 * Flush request from drive queue.
256 */
257 mi->mi_tab.b_errcnt = 0;
258 mi->mi_tab.b_actf = bp->av_forw;
259 iodone(bp);
260 /* fall into... */
261 case MBD_RETRY: /* attempt the operation again */
b81fd3e8
BJ
262 /*
263 * Dequeue data transfer from massbus queue;
264 * if there is still a i/o request on the device
265 * queue then start the next operation on the device.
266 * (Common code for DONE and RETRY).
267 */
268 mhp->mh_active = 0;
269 mi->mi_tab.b_active = 0;
270 mhp->mh_actf = mi->mi_forw;
271 if (mi->mi_tab.b_actf)
272 mbustart(mi);
273 break;
274
275 case MBD_RESTARTED: /* driver restarted op (ecc, e.g.)
b81fd3e8 276 /*
3dbaa9da 277 * Note that mhp->mh_active is still on.
b81fd3e8
BJ
278 */
279 break;
280
281 default:
c9e9b65b 282 panic("mbintr");
b81fd3e8 283 }
b81fd3e8 284 }
b81fd3e8
BJ
285 /*
286 * Service drives which require attention
287 * after non-data-transfer operations.
288 */
cda4cdd4
BJ
289 while (drive = ffs(as)) {
290 drive--; /* was 1 origin */
291 as &= ~(1 << drive);
89bd2f01 292 mi = mhp->mh_mbip[drive];
0d7a9a27
SL
293 if (mi == NULL || mi->mi_alive == 0) {
294 struct mba_device fnd;
295 struct mba_slave *ms;
296 struct mba_drv *mbd = &mhp->mh_mba->mba_drv[drive];
297 int dt = mbd->mbd_dt & 0xffff;
298
299 if (dt == 0 || dt == MBDT_MOH)
300 continue;
301 fnd.mi_mba = mhp->mh_mba;
302 fnd.mi_mbanum = mbanum;
303 fnd.mi_drive = drive;
304 if ((mi = mbaconfig(&fnd, dt)) == NULL)
305 continue;
306 if (dt & MBDT_TAP) {
307 for (ms = mbsinit; ms->ms_driver; ms++)
308 if (ms->ms_driver == mi->mi_driver &&
309 ms->ms_alive == 0 &&
310 (ms->ms_ctlr == mi->mi_unit ||
311 ms->ms_ctlr == '?')) {
312 if ((*ms->ms_driver->md_slave)(mi, ms)) {
313 printf("%s%d at %s%d slave %d\n",
314 ms->ms_driver->md_sname,
315 ms->ms_unit,
316 mi->mi_driver->md_dname,
317 mi->mi_unit,
318 ms->ms_slave);
319 ms->ms_alive = 1;
320 ms->ms_ctlr = mi->mi_unit;
321 }
322 }
323 }
324 }
cda4cdd4 325 /*
89bd2f01 326 * If driver has a handler for non-data transfer
d565635a 327 * interrupts, give it a chance to tell us what to do.
cda4cdd4 328 */
cda4cdd4 329 if (mi->mi_driver->md_ndint) {
cda4cdd4 330 switch ((*mi->mi_driver->md_ndint)(mi)) {
b81fd3e8 331
d565635a 332 case MBN_DONE: /* operation completed */
0d7a9a27 333 mi->mi_tab.b_active = 0;
cda4cdd4 334 mi->mi_tab.b_errcnt = 0;
89bd2f01 335 bp = mi->mi_tab.b_actf;
cda4cdd4
BJ
336 mi->mi_tab.b_actf = bp->av_forw;
337 iodone(bp);
d565635a
BJ
338 /* fall into common code */
339 case MBN_RETRY: /* operation continues */
cda4cdd4
BJ
340 if (mi->mi_tab.b_actf)
341 mbustart(mi);
342 break;
d565635a 343 case MBN_SKIP: /* ignore unsol. interrupt */
89bd2f01 344 break;
cda4cdd4
BJ
345 default:
346 panic("mbintr");
347 }
348 } else
d565635a
BJ
349 /*
350 * If there is no non-data transfer interrupt
351 * routine, then we should just
352 * restart the unit, leading to a mbstart() soon.
353 */
cda4cdd4
BJ
354 mbustart(mi);
355 }
b81fd3e8
BJ
356 /*
357 * If there is an operation available and
358 * the massbus isn't active, get it going.
359 */
360 if (mhp->mh_actf && !mhp->mh_active)
361 mbstart(mhp);
d565635a 362 /* THHHHATS all folks... */
b81fd3e8
BJ
363}
364
365/*
366 * Setup the mapping registers for a transfer.
367 */
368mbasetup(mi)
89bd2f01 369 register struct mba_device *mi;
b5ad10c3 370{
b81fd3e8
BJ
371 register struct mba_regs *mbap = mi->mi_mba;
372 struct buf *bp = mi->mi_tab.b_actf;
309cfbf4 373 register int npf;
b5ad10c3
BJ
374 unsigned v;
375 register struct pte *pte, *io;
376 int o;
b5ad10c3 377 struct proc *rp;
b5ad10c3 378
f9b6e695
BJ
379 v = btop(bp->b_un.b_addr);
380 o = (int)bp->b_un.b_addr & PGOFSET;
381 npf = btoc(bp->b_bcount + o);
382 rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
309cfbf4 383 if ((bp->b_flags & B_PHYS) == 0)
f9b6e695 384 pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
309cfbf4
BJ
385 else if (bp->b_flags & B_UAREA)
386 pte = &rp->p_addr[v];
387 else if (bp->b_flags & B_PAGET)
388 pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
389 else
390 pte = vtopte(rp, v);
391 io = mbap->mba_map;
392 while (--npf >= 0) {
393 if (pte->pg_pfnum == 0)
394 panic("mba, zero entry");
395 *(int *)io++ = pte++->pg_pfnum | PG_V;
b5ad10c3 396 }
f9b6e695 397 *(int *)io++ = 0;
309cfbf4 398 return (o);
b5ad10c3 399}
16e4017f 400
a42d8a6b 401#if notdef
d565635a
BJ
402/*
403 * Init and interrupt enable a massbus adapter.
404 */
16e4017f
BJ
405mbainit(mp)
406 struct mba_regs *mp;
407{
408
d565635a
BJ
409 mp->mba_cr = MBCR_INIT;
410 mp->mba_cr = MBCR_IE;
16e4017f 411}
443c8066 412#endif
2752c877 413#endif