Added connection to ?
[unix-history] / usr / src / sys / vax / mba / mba.c
CommitLineData
71236e46 1/* mba.c 4.8 81/02/21 */
b81fd3e8
BJ
2
3/*
4 * Massbus driver; arbitrates massbusses through device driver routines
5 * and provides common functions.
6 */
7int mbadebug = 0;
8#define dprintf if (mbadebug) printf
b5ad10c3
BJ
9
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"
b81fd3e8 19#include "../h/pte.h"
b5ad10c3
BJ
20#include "../h/mba.h"
21#include "../h/mtpr.h"
22#include "../h/vm.h"
23
24/*
b81fd3e8
BJ
25 * Start activity on a massbus device.
26 * We are given the device's mba_info structure and activate
27 * the device via the unit start routine. The unit start
28 * routine may indicate that it is finished (e.g. if the operation
29 * was a ``sense'' on a tape drive), that the (multi-ported) unit
30 * is busy (we will get an interrupt later), that it started the
31 * unit (e.g. for a non-data transfer operation), or that it has
32 * set up a data transfer operation and we should start the massbus adaptor.
b5ad10c3 33 */
b81fd3e8
BJ
34mbustart(mi)
35 register struct mba_info *mi;
36{
37 register struct mba_drv *mdp; /* drive registers */
38 register struct buf *bp; /* i/o operation at head of queue */
39 register struct mba_hd *mhp; /* header for mba device is on */
40
41 dprintf("enter mbustart\n");
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;
49 mdp = mi->mi_drv;
50 /*
51 * Since we clear attentions on the drive when we are
52 * finished processing it, the fact that an attention
53 * status shows indicated confusion in the hardware or our logic.
54 */
55 if (mdp->mbd_as & (1 << mi->mi_drive)) {
56 printf("mbustart: ata on for %d\n", mi->mi_drive);
57 mdp->mbd_as = 1 << mi->mi_drive;
58 }
59 /*
60 * Let the drivers unit start routine have at it
61 * and then process the request further, per its instructions.
62 */
63 switch ((*mi->mi_driver->md_ustart)(mi)) {
64
65 case MBU_NEXT: /* request is complete (e.g. ``sense'') */
66 dprintf("mbu_next\n");
67 mi->mi_tab.b_active = 0;
68 mi->mi_tab.b_actf = bp->av_forw;
69 iodone(bp);
70 goto loop;
71
72 case MBU_DODATA: /* all ready to do data transfer */
73 dprintf("mbu_dodata\n");
74 /*
75 * Queue the device mba_info structure on the massbus
76 * mba_hd structure for processing as soon as the
77 * data path is available.
78 */
79 mhp = mi->mi_hd;
80 mi->mi_forw = NULL;
81 if (mhp->mh_actf == NULL)
82 mhp->mh_actf = mi;
83 else
84 mhp->mh_actl->mi_forw = mi;
85 mhp->mh_actl = mi;
86 /*
87 * If data path is idle, start transfer now.
88 * In any case the device is ``active'' waiting for the
89 * data to transfer.
90 */
91 if (mhp->mh_active == 0)
92 mbstart(mhp);
93 mi->mi_tab.b_active = 1;
94 return;
95
96 case MBU_STARTED: /* driver started a non-data transfer */
97 dprintf("mbu_started\n");
98 /*
99 * Mark device busy during non-data transfer
100 * and count this as a ``seek'' on the device.
101 */
102 if (mi->mi_dk >= 0)
103 dk_seek[mi->mi_dk]++;
104 mi->mi_tab.b_active = 1;
105 return;
106
107 case MBU_BUSY: /* dual port drive busy */
108 dprintf("mbu_busy\n");
109 /*
110 * We mark the device structure so that when an
111 * interrupt occurs we will know to restart the unit.
112 */
113 mi->mi_tab.b_flags |= B_BUSY;
114 return;
115
116 default:
117 panic("mbustart");
118 }
e1e57888 119}
b81fd3e8
BJ
120
121/*
122 * Start an i/o operation on the massbus specified by the argument.
123 * We peel the first operation off its queue and insure that the drive
124 * is present and on-line. We then use the drivers start routine
125 * (if any) to prepare the drive, setup the massbus map for the transfer
126 * and start the transfer.
127 */
128mbstart(mhp)
129 register struct mba_hd *mhp;
130{
131 register struct mba_info *mi;
132 struct buf *bp;
b81fd3e8
BJ
133 register struct mba_regs *mbp;
134
135 dprintf("mbstart\n");
136loop:
137 /*
138 * Look for an operation at the front of the queue.
139 */
140 if ((mi = mhp->mh_actf) == NULL) {
141 dprintf("nothing to do\n");
142 return;
143 }
144 if ((bp = mi->mi_tab.b_actf) == NULL) {
145 dprintf("nothing on actf\n");
146 mhp->mh_actf = mi->mi_forw;
147 goto loop;
148 }
149 /*
150 * If this device isn't present and on-line, then
151 * we screwed up, and can't really do the operation.
152 */
153 if ((mi->mi_drv->mbd_ds & (MBD_DPR|MBD_MOL)) != (MBD_DPR|MBD_MOL)) {
154 dprintf("not on line ds %x\n", mi->mi_drv->mbd_ds);
155 mi->mi_tab.b_actf = bp->av_forw;
156 bp->b_flags |= B_ERROR;
157 iodone(bp);
158 goto loop;
159 }
160 /*
161 * We can do the operation; mark the massbus active
162 * and let the device start routine setup any necessary
163 * device state for the transfer (e.g. desired cylinder, etc
164 * on disks).
165 */
166 mhp->mh_active = 1;
167 if (mi->mi_driver->md_start) {
168 dprintf("md_start\n");
169 (*mi->mi_driver->md_start)(mi);
170 }
171
172 /*
173 * Setup the massbus control and map registers and start
174 * the transfer.
175 */
176 dprintf("start mba\n");
177 mbp = mi->mi_mba;
178 mbp->mba_sr = -1; /* conservative */
179 mbp->mba_var = mbasetup(mi);
180 mbp->mba_bcr = -bp->b_bcount;
181 mi->mi_drv->mbd_cs1 =
182 (bp->b_flags & B_READ) ? MBD_RCOM|MBD_GO : MBD_WCOM|MBD_GO;
183 if (mi->mi_dk >= 0) {
184 dk_busy |= 1 << mi->mi_dk;
185 dk_xfer[mi->mi_dk]++;
186 dk_wds[mi->mi_dk] += bp->b_bcount >> 6;
187 }
188}
b5ad10c3 189
b81fd3e8
BJ
190/*
191 * Take an interrupt off of massbus mbanum,
192 * and dispatch to drivers as appropriate.
193 */
194mbintr(mbanum)
195 int mbanum;
196{
197 register struct mba_hd *mhp = &mba_hd[mbanum];
198 register struct mba_regs *mbp = mhp->mh_mba;
199 register struct mba_info *mi;
80e7c811 200 register struct buf *bp;
b81fd3e8
BJ
201 register int drive;
202 int mbastat, as;
203
204 /*
205 * Read out the massbus status register
206 * and attention status register and clear
207 * the bits in same by writing them back.
208 */
209 mbastat = mbp->mba_sr;
210 mbp->mba_sr = mbastat;
211 /* note: the mbd_as register is shared between drives */
212 as = mbp->mba_drv[0].mbd_as;
213 mbp->mba_drv[0].mbd_as = as;
214 dprintf("mbintr mbastat %x as %x\n", mbastat, as);
215
216 /*
217 * Disable interrupts from the massbus adapter
218 * for the duration of the operation of the massbus
219 * driver, so that spurious interrupts won't be generated.
220 */
221 mbp->mba_cr &= ~MBAIE;
222
223 /*
224 * If the mba was active, process the data transfer
225 * complete interrupt; otherwise just process units which
226 * are now finished.
227 */
228 if (mhp->mh_active) {
229 if ((mbastat & MBS_DTCMP) == 0) {
230 printf("mbintr(%d),b_active,no DTCMP!\n", mbanum);
231 goto doattn;
e1e57888 232 }
b81fd3e8
BJ
233 /*
234 * Clear attention status for drive whose data
235 * transfer completed, and give the dtint driver
236 * routine a chance to say what is next.
237 */
238 mi = mhp->mh_actf;
239 as &= ~(1 << mi->mi_drive);
240 dk_busy &= ~(1 << mi->mi_dk);
241 bp = mi->mi_tab.b_actf;
242 switch((*mi->mi_driver->md_dtint)(mi, mbastat)) {
243
244 case MBD_DONE: /* all done, for better or worse */
245 dprintf("mbd_done\n");
246 /*
247 * Flush request from drive queue.
248 */
249 mi->mi_tab.b_errcnt = 0;
250 mi->mi_tab.b_actf = bp->av_forw;
251 iodone(bp);
252 /* fall into... */
253 case MBD_RETRY: /* attempt the operation again */
254 dprintf("mbd_retry\n");
255 /*
256 * Dequeue data transfer from massbus queue;
257 * if there is still a i/o request on the device
258 * queue then start the next operation on the device.
259 * (Common code for DONE and RETRY).
260 */
261 mhp->mh_active = 0;
262 mi->mi_tab.b_active = 0;
263 mhp->mh_actf = mi->mi_forw;
264 if (mi->mi_tab.b_actf)
265 mbustart(mi);
266 break;
267
268 case MBD_RESTARTED: /* driver restarted op (ecc, e.g.)
269 dprintf("mbd_restarted\n");
270 /*
271 * Note that mp->b_active is still on.
272 */
273 break;
274
275 default:
276 panic("mbaintr");
277 }
278 } else {
279 dprintf("!dtcmp\n");
280 if (mbastat & MBS_DTCMP)
281 printf("mbaintr,DTCMP,!b_active\n");
282 }
283doattn:
284 /*
285 * Service drives which require attention
286 * after non-data-transfer operations.
287 */
288 for (drive = 0; as && drive < 8; drive++)
289 if (as & (1 << drive)) {
290 dprintf("service as %d\n", drive);
291 as &= ~(1 << drive);
292 /*
293 * Consistency check the implied attention,
294 * to make sure the drive should have interrupted.
295 */
296 mi = mhp->mh_mbip[drive];
297 if (mi == NULL)
298 goto random; /* no such drive */
299 if (mi->mi_tab.b_active == 0 &&
300 (mi->mi_tab.b_flags&B_BUSY) == 0)
301 goto random; /* not active */
302 if ((bp = mi->mi_tab.b_actf) == NULL) {
303 /* nothing doing */
304random:
305 printf("random mbaintr %d %d\n",mbanum,drive);
306 continue;
307 }
308 /*
309 * If this interrupt wasn't a notification that
310 * a dual ported drive is available, and if the
311 * driver has a handler for non-data transfer
312 * interrupts, give it a chance to tell us that
313 * the operation needs to be redone
314 */
315 if ((mi->mi_tab.b_flags&B_BUSY) == 0 &&
316 mi->mi_driver->md_ndint) {
317 mi->mi_tab.b_active = 0;
318 switch((*mi->mi_driver->md_ndint)(mi)) {
319
320 case MBN_DONE:
321 dprintf("mbn_done\n");
322 /*
323 * Non-data transfer interrupt
324 * completed i/o request's processing.
325 */
326 mi->mi_tab.b_errcnt = 0;
327 mi->mi_tab.b_actf = bp->av_forw;
328 iodone(bp);
329 /* fall into... */
330 case MBN_RETRY:
331 dprintf("mbn_retry\n");
332 if (mi->mi_tab.b_actf)
333 mbustart(mi);
334 break;
335
336 default:
337 panic("mbintr ndint");
338 }
339 } else
340 mbustart(mi);
341 }
342 /*
343 * If there is an operation available and
344 * the massbus isn't active, get it going.
345 */
346 if (mhp->mh_actf && !mhp->mh_active)
347 mbstart(mhp);
348 mbp->mba_cr |= MBAIE;
349}
350
351/*
352 * Setup the mapping registers for a transfer.
353 */
354mbasetup(mi)
355 register struct mba_info *mi;
b5ad10c3 356{
b81fd3e8
BJ
357 register struct mba_regs *mbap = mi->mi_mba;
358 struct buf *bp = mi->mi_tab.b_actf;
b5ad10c3
BJ
359 register int i;
360 int npf;
361 unsigned v;
362 register struct pte *pte, *io;
363 int o;
364 int vaddr;
b5ad10c3 365 struct proc *rp;
b5ad10c3 366
f9b6e695
BJ
367 io = mbap->mba_map;
368 v = btop(bp->b_un.b_addr);
369 o = (int)bp->b_un.b_addr & PGOFSET;
370 npf = btoc(bp->b_bcount + o);
371 rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
372 vaddr = o;
373 if (bp->b_flags & B_UAREA) {
374 for (i = 0; i < UPAGES; i++) {
375 if (rp->p_addr[i].pg_pfnum == 0)
376 panic("mba: zero upage");
377 *(int *)io++ = rp->p_addr[i].pg_pfnum | PG_V;
378 }
379 } else if ((bp->b_flags & B_PHYS) == 0) {
380 pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
381 while (--npf >= 0)
382 *(int *)io++ = pte++->pg_pfnum | PG_V;
383 } else {
384 if (bp->b_flags & B_PAGET)
385 pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
386 else
387 pte = vtopte(rp, v);
388 while (--npf >= 0) {
389 if (pte->pg_pfnum == 0)
390 panic("mba, zero entry");
391 *(int *)io++ = pte++->pg_pfnum | PG_V;
b5ad10c3
BJ
392 }
393 }
f9b6e695 394 *(int *)io++ = 0;
b81fd3e8 395 return (vaddr);
b5ad10c3 396}