xref: /original-bsd/sys/hp300/dev/sd.c (revision 13ec26c3)
1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Van Jacobson of Lawrence Berkeley Laboratory.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)sd.c	7.13 (Berkeley) 06/05/92
11  */
12 
13 /*
14  * SCSI CCS (Command Command Set) disk driver.
15  */
16 #include "sd.h"
17 #if NSD > 0
18 
19 #ifndef lint
20 static char rcsid[] = "$Header: /usr/src/sys/hp300/dev/RCS/sd.c,v 1.2 92/04/10 20:48:35 mike Exp $";
21 #endif
22 
23 #include "sys/param.h"
24 #include "sys/systm.h"
25 #include "sys/buf.h"
26 #include "sys/dkstat.h"
27 #include "sys/disklabel.h"
28 #include "sys/malloc.h"
29 #include "sys/proc.h"
30 
31 #include "hp/dev/device.h"
32 #include "scsireg.h"
33 #include "vm/vm_param.h"
34 #include "vm/lock.h"
35 #include "vm/vm_prot.h"
36 #include "vm/pmap.h"
37 
38 extern int scsi_test_unit_rdy();
39 extern int scsi_request_sense();
40 extern int scsi_inquiry();
41 extern int scsi_read_capacity();
42 extern int scsi_tt_write();
43 extern int scsireq();
44 extern int scsiustart();
45 extern int scsigo();
46 extern void scsifree();
47 extern void scsireset();
48 extern void scsi_delay();
49 
50 extern void disksort();
51 extern void biodone();
52 extern int physio();
53 extern void TBIS();
54 
55 int	sdinit();
56 void	sdstrategy(), sdstart(), sdustart(), sdgo(), sdintr();
57 
58 struct	driver sddriver = {
59 	sdinit, "sd", (int (*)())sdstart, (int (*)())sdgo, (int (*)())sdintr,
60 };
61 
62 struct	size {
63 	u_long	strtblk;
64 	u_long	endblk;
65 	int	nblocks;
66 };
67 
68 struct sdinfo {
69 	struct	size part[8];
70 };
71 
72 /*
73  * since the SCSI standard tends to hide the disk structure, we define
74  * partitions in terms of DEV_BSIZE blocks.  The default partition table
75  * (for an unlabeled disk) reserves 512K for a boot area, has an 8 meg
76  * root and 32 meg of swap.  The rest of the space on the drive goes in
77  * the G partition.  As usual, the C partition covers the entire disk
78  * (including the boot area).
79  */
80 struct sdinfo sddefaultpart = {
81 	     1024,   17408,   16384   ,	/* A */
82 	    17408,   82944,   65536   ,	/* B */
83 	        0,       0,       0   ,	/* C */
84 	    17408,  115712,   98304   ,	/* D */
85 	   115712,  218112,  102400   ,	/* E */
86 	   218112,       0,       0   ,	/* F */
87 	    82944,       0,       0   ,	/* G */
88 	   115712,       0,       0   ,	/* H */
89 };
90 
91 struct	sd_softc {
92 	struct	hp_device *sc_hd;
93 	struct	devqueue sc_dq;
94 	int	sc_format_pid;	/* process using "format" mode */
95 	short	sc_flags;
96 	short	sc_type;	/* drive type */
97 	short	sc_punit;	/* physical unit (scsi lun) */
98 	u_short	sc_bshift;	/* convert device blocks to DEV_BSIZE blks */
99 	u_int	sc_blks;	/* number of blocks on device */
100 	int	sc_blksize;	/* device block size in bytes */
101 	u_int	sc_wpms;	/* average xfer rate in 16 bit wds/sec. */
102 	struct	sdinfo sc_info;	/* drive partition table & label info */
103 } sd_softc[NSD];
104 
105 /* sc_flags values */
106 #define	SDF_ALIVE	0x1
107 #define SDF_WANTED	0x2
108 #define SDF_RMEDIA	0x4
109 
110 #ifdef DEBUG
111 int sddebug = 1;
112 #define SDB_ERROR	0x01
113 #define SDB_PARTIAL	0x02
114 #endif
115 
116 struct sdstats {
117 	long	sdresets;
118 	long	sdtransfers;
119 	long	sdpartials;
120 } sdstats[NSD];
121 
122 struct	buf sdtab[NSD];
123 struct	scsi_fmt_cdb sdcmd[NSD];
124 struct	scsi_fmt_sense sdsense[NSD];
125 
126 static struct scsi_fmt_cdb sd_read_cmd = { 10, CMD_READ_EXT };
127 static struct scsi_fmt_cdb sd_write_cmd = { 10, CMD_WRITE_EXT };
128 
129 #define	sdunit(x)	(minor(x) >> 3)
130 #define sdpart(x)	(minor(x) & 0x7)
131 #define	sdpunit(x)	((x) & 7)
132 #define	b_cylin		b_resid
133 
134 #define	SDRETRY		2
135 
136 /*
137  * Table of scsi commands users are allowed to access via "format"
138  * mode.  0 means not legal.  1 means "immediate" (doesn't need dma).
139  * -1 means needs dma and/or wait for intr.
140  */
141 static char legal_cmds[256] = {
142 /*****  0   1   2   3   4   5   6   7     8   9   A   B   C   D   E   F */
143 /*00*/	0,  0,  0,  0, -1,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
144 /*10*/	0,  0,  1,  0,  0,  1,  0,  0,    0,  0,  1,  0,  0,  0,  0,  0,
145 /*20*/	0,  0,  0,  0,  0,  1,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
146 /*30*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
147 /*40*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
148 /*50*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
149 /*60*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
150 /*70*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
151 /*80*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
152 /*90*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
153 /*a0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
154 /*b0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
155 /*c0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
156 /*d0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
157 /*e0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
158 /*f0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
159 };
160 
161 static struct scsi_inquiry inqbuf;
162 static struct scsi_fmt_cdb inq = {
163 	6,
164 	CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0
165 };
166 
167 static u_char capbuf[8];
168 struct scsi_fmt_cdb cap = {
169 	10,
170 	CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0
171 };
172 
173 static int
174 sdident(sc, hd)
175 	struct sd_softc *sc;
176 	struct hp_device *hd;
177 {
178 	int unit;
179 	register int ctlr, slave;
180 	register int i;
181 	register int tries = 10;
182 	char idstr[32];
183 	int ismo = 0;
184 
185 	ctlr = hd->hp_ctlr;
186 	slave = hd->hp_slave;
187 	unit = sc->sc_punit;
188 	scsi_delay(-1);
189 
190 	/*
191 	 * See if unit exists and is a disk then read block size & nblocks.
192 	 */
193 	while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) {
194 		if (i == -1 || --tries < 0) {
195 			if (ismo)
196 				break;
197 			/* doesn't exist or not a CCS device */
198 			goto failed;
199 		}
200 		if (i == STS_CHECKCOND) {
201 			u_char sensebuf[128];
202 			struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf;
203 
204 			scsi_request_sense(ctlr, slave, unit, sensebuf,
205 					   sizeof(sensebuf));
206 			if (sp->class == 7)
207 				switch (sp->key) {
208 				/* not ready -- might be MO with no media */
209 				case 2:
210 					if (sp->len == 12 &&
211 					    sensebuf[12] == 10)	/* XXX */
212 						ismo = 1;
213 					break;
214 				/* drive doing an RTZ -- give it a while */
215 				case 6:
216 					DELAY(1000000);
217 					break;
218 				default:
219 					break;
220 				}
221 		}
222 		DELAY(1000);
223 	}
224 	/*
225 	 * Find out about device
226 	 */
227 	if (scsi_immed_command(ctlr, slave, unit, &inq,
228 			       (u_char *)&inqbuf, sizeof(inqbuf), B_READ))
229 		goto failed;
230 	switch (inqbuf.type) {
231 	case 0:		/* disk */
232 	case 4:		/* WORM */
233 	case 5:		/* CD-ROM */
234 	case 7:		/* Magneto-optical */
235 		break;
236 	default:	/* not a disk */
237 		goto failed;
238 	}
239 	/*
240 	 * Get a usable id string
241 	 */
242 	if (inqbuf.version != 1) {
243 		bcopy("UNKNOWN", &idstr[0], 8);
244 		bcopy("DRIVE TYPE", &idstr[8], 11);
245 	} else {
246 		bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28);
247 		for (i = 27; i > 23; --i)
248 			if (idstr[i] != ' ')
249 				break;
250 		idstr[i+1] = 0;
251 		for (i = 23; i > 7; --i)
252 			if (idstr[i] != ' ')
253 				break;
254 		idstr[i+1] = 0;
255 		for (i = 7; i >= 0; --i)
256 			if (idstr[i] != ' ')
257 				break;
258 		idstr[i+1] = 0;
259 	}
260 	i = scsi_immed_command(ctlr, slave, unit, &cap,
261 			       (u_char *)&capbuf, sizeof(capbuf), B_READ);
262 	if (i) {
263 		if (i != STS_CHECKCOND ||
264 		    bcmp(&idstr[0], "HP", 3) ||
265 		    bcmp(&idstr[8], "S6300.650A", 11))
266 			goto failed;
267 		/* XXX unformatted or non-existant MO media; fake it */
268 		sc->sc_blks = 318664;
269 		sc->sc_blksize = 1024;
270 	} else {
271 		sc->sc_blks = *(u_int *)&capbuf[0];
272 		sc->sc_blksize = *(int *)&capbuf[4];
273 	}
274 	/* return value of read capacity is last valid block number */
275 	sc->sc_blks++;
276 
277 	if (inqbuf.version != 1)
278 		printf("sd%d: type 0x%x, qual 0x%x, ver %d", hd->hp_unit,
279 			inqbuf.type, inqbuf.qual, inqbuf.version);
280 	else
281 		printf("sd%d: %s %s rev %s", hd->hp_unit, idstr, &idstr[8],
282 			&idstr[24]);
283 	printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize);
284 	if (inqbuf.qual & 0x80)
285 		sc->sc_flags |= SDF_RMEDIA;
286 	if (sc->sc_blksize != DEV_BSIZE) {
287 		if (sc->sc_blksize < DEV_BSIZE) {
288 			printf("sd%d: need %d byte blocks - drive ignored\n",
289 				unit, DEV_BSIZE);
290 			goto failed;
291 		}
292 		for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
293 			++sc->sc_bshift;
294 		sc->sc_blks <<= sc->sc_bshift;
295 	}
296 	sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
297 	scsi_delay(0);
298 	return(inqbuf.type);
299 failed:
300 	scsi_delay(0);
301 	return(-1);
302 }
303 
304 int
305 sdinit(hd)
306 	register struct hp_device *hd;
307 {
308 	register struct sd_softc *sc = &sd_softc[hd->hp_unit];
309 
310 	sc->sc_hd = hd;
311 	sc->sc_flags = 0;
312 	sc->sc_punit = sdpunit(hd->hp_flags);
313 	sc->sc_type = sdident(sc, hd);
314 	if (sc->sc_type < 0)
315 		return(0);
316 	sc->sc_dq.dq_ctlr = hd->hp_ctlr;
317 	sc->sc_dq.dq_unit = hd->hp_unit;
318 	sc->sc_dq.dq_slave = hd->hp_slave;
319 	sc->sc_dq.dq_driver = &sddriver;
320 
321 	/*
322 	 * If we don't have a disk label, build a default partition
323 	 * table with 'standard' size root & swap and everything else
324 	 * in the G partition.
325 	 */
326 	sc->sc_info = sddefaultpart;
327 	/* C gets everything */
328 	sc->sc_info.part[2].nblocks = sc->sc_blks;
329 	sc->sc_info.part[2].endblk = sc->sc_blks;
330 	/* G gets from end of B to end of disk */
331 	sc->sc_info.part[6].nblocks = sc->sc_blks - sc->sc_info.part[1].endblk;
332 	sc->sc_info.part[6].endblk = sc->sc_blks;
333 	/*
334 	 * We also define the D, E and F paritions as an alternative to
335 	 * B and G.  D is 48Mb, starts after A and is intended for swapping.
336 	 * E is 50Mb, starts after D and is intended for /usr. F starts
337 	 * after E and is what ever is left.
338 	 */
339 	if (sc->sc_blks >= sc->sc_info.part[4].endblk) {
340 		sc->sc_info.part[5].nblocks =
341 			sc->sc_blks - sc->sc_info.part[4].endblk;
342 		sc->sc_info.part[5].endblk = sc->sc_blks;
343 	} else {
344 		sc->sc_info.part[5].strtblk = 0;
345 		sc->sc_info.part[3] = sc->sc_info.part[5];
346 		sc->sc_info.part[4] = sc->sc_info.part[5];
347 	}
348 	/*
349 	 * H is a single partition alternative to E and F.
350 	 */
351 	if (sc->sc_blks >= sc->sc_info.part[3].endblk) {
352 		sc->sc_info.part[7].nblocks =
353 			sc->sc_blks - sc->sc_info.part[3].endblk;
354 		sc->sc_info.part[7].endblk = sc->sc_blks;
355 	} else {
356 		sc->sc_info.part[7].strtblk = 0;
357 	}
358 
359 	sc->sc_flags |= SDF_ALIVE;
360 	return(1);
361 }
362 
363 void
364 sdreset(sc, hd)
365 	register struct sd_softc *sc;
366 	register struct hp_device *hd;
367 {
368 	sdstats[hd->hp_unit].sdresets++;
369 }
370 
371 int
372 sdopen(dev, flags, mode, p)
373 	dev_t dev;
374 	int flags, mode;
375 	struct proc *p;
376 {
377 	register int unit = sdunit(dev);
378 	register struct sd_softc *sc = &sd_softc[unit];
379 
380 	if (unit >= NSD)
381 		return(ENXIO);
382 	if ((sc->sc_flags & SDF_ALIVE) == 0 && suser(p->p_ucred, &p->p_acflag))
383 		return(ENXIO);
384 
385 	if (sc->sc_hd->hp_dk >= 0)
386 		dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms;
387 	return(0);
388 }
389 
390 int
391 sdclose(dev, flag, mode, p)
392 	dev_t dev;
393 	int flag, mode;
394 	struct proc *p;
395 {
396 	int unit = sdunit(dev);
397 	register struct sd_softc *sc = &sd_softc[unit];
398 	int s;
399 
400 	/*
401 	 * XXX we should really do this for all drives.
402 	 */
403 	if (sc->sc_flags & SDF_RMEDIA) {
404 		s = splbio();
405 		while (sdtab[unit].b_active) {
406 			sc->sc_flags |= SDF_WANTED;
407 			sleep((caddr_t)&sdtab[unit], PRIBIO);
408 		}
409 		splx(s);
410 	}
411 	sc->sc_format_pid = 0;
412 }
413 
414 /*
415  * This routine is called for partial block transfers and non-aligned
416  * transfers (the latter only being possible on devices with a block size
417  * larger than DEV_BSIZE).  The operation is performed in three steps
418  * using a locally allocated buffer:
419  *	1. transfer any initial partial block
420  *	2. transfer full blocks
421  *	3. transfer any final partial block
422  */
423 static void
424 sdlblkstrat(bp, bsize)
425 	register struct buf *bp;
426 	register int bsize;
427 {
428 	register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf),
429 							M_DEVBUF, M_WAITOK);
430 	caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
431 	register int bn, resid;
432 	register caddr_t addr;
433 
434 	bzero((caddr_t)cbp, sizeof(*cbp));
435 	cbp->b_proc = curproc;		/* XXX */
436 	cbp->b_dev = bp->b_dev;
437 	bn = bp->b_blkno;
438 	resid = bp->b_bcount;
439 	addr = bp->b_un.b_addr;
440 #ifdef DEBUG
441 	if (sddebug & SDB_PARTIAL)
442 		printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
443 		       bp, bp->b_flags, bn, resid, addr);
444 #endif
445 
446 	while (resid > 0) {
447 		register int boff = dbtob(bn) & (bsize - 1);
448 		register int count;
449 
450 		if (boff || resid < bsize) {
451 			sdstats[sdunit(bp->b_dev)].sdpartials++;
452 			count = MIN(resid, bsize - boff);
453 			cbp->b_flags = B_BUSY | B_PHYS | B_READ;
454 			cbp->b_blkno = bn - btodb(boff);
455 			cbp->b_un.b_addr = cbuf;
456 			cbp->b_bcount = bsize;
457 #ifdef DEBUG
458 			if (sddebug & SDB_PARTIAL)
459 				printf(" readahead: bn %x cnt %x off %x addr %x\n",
460 				       cbp->b_blkno, count, boff, addr);
461 #endif
462 			sdstrategy(cbp);
463 			biowait(cbp);
464 			if (cbp->b_flags & B_ERROR) {
465 				bp->b_flags |= B_ERROR;
466 				bp->b_error = cbp->b_error;
467 				break;
468 			}
469 			if (bp->b_flags & B_READ) {
470 				bcopy(&cbuf[boff], addr, count);
471 				goto done;
472 			}
473 			bcopy(addr, &cbuf[boff], count);
474 #ifdef DEBUG
475 			if (sddebug & SDB_PARTIAL)
476 				printf(" writeback: bn %x cnt %x off %x addr %x\n",
477 				       cbp->b_blkno, count, boff, addr);
478 #endif
479 		} else {
480 			count = resid & ~(bsize - 1);
481 			cbp->b_blkno = bn;
482 			cbp->b_un.b_addr = addr;
483 			cbp->b_bcount = count;
484 #ifdef DEBUG
485 			if (sddebug & SDB_PARTIAL)
486 				printf(" fulltrans: bn %x cnt %x addr %x\n",
487 				       cbp->b_blkno, count, addr);
488 #endif
489 		}
490 		cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
491 		sdstrategy(cbp);
492 		biowait(cbp);
493 		if (cbp->b_flags & B_ERROR) {
494 			bp->b_flags |= B_ERROR;
495 			bp->b_error = cbp->b_error;
496 			break;
497 		}
498 done:
499 		bn += btodb(count);
500 		resid -= count;
501 		addr += count;
502 #ifdef DEBUG
503 		if (sddebug & SDB_PARTIAL)
504 			printf(" done: bn %x resid %x addr %x\n",
505 			       bn, resid, addr);
506 #endif
507 	}
508 	free(cbuf, M_DEVBUF);
509 	free(cbp, M_DEVBUF);
510 }
511 
512 void
513 sdstrategy(bp)
514 	register struct buf *bp;
515 {
516 	register int unit = sdunit(bp->b_dev);
517 	register struct sd_softc *sc = &sd_softc[unit];
518 	register struct size *pinfo = &sc->sc_info.part[sdpart(bp->b_dev)];
519 	register struct buf *dp = &sdtab[unit];
520 	register daddr_t bn;
521 	register int sz, s;
522 
523 	if (sc->sc_format_pid) {
524 		if (sc->sc_format_pid != curproc->p_pid) {	/* XXX */
525 			bp->b_error = EPERM;
526 			bp->b_flags |= B_ERROR;
527 			goto done;
528 		}
529 		bp->b_cylin = 0;
530 	} else {
531 		bn = bp->b_blkno;
532 		sz = howmany(bp->b_bcount, DEV_BSIZE);
533 		if (bn < 0 || bn + sz > pinfo->nblocks) {
534 			sz = pinfo->nblocks - bn;
535 			if (sz == 0) {
536 				bp->b_resid = bp->b_bcount;
537 				goto done;
538 			}
539 			if (sz < 0) {
540 				bp->b_error = EINVAL;
541 				bp->b_flags |= B_ERROR;
542 				goto done;
543 			}
544 			bp->b_bcount = dbtob(sz);
545 		}
546 		/*
547 		 * Non-aligned or partial-block transfers handled specially.
548 		 */
549 		s = sc->sc_blksize - 1;
550 		if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
551 			sdlblkstrat(bp, sc->sc_blksize);
552 			goto done;
553 		}
554 		bp->b_cylin = (bn + pinfo->strtblk) >> sc->sc_bshift;
555 	}
556 	s = splbio();
557 	disksort(dp, bp);
558 	if (dp->b_active == 0) {
559 		dp->b_active = 1;
560 		sdustart(unit);
561 	}
562 	splx(s);
563 	return;
564 done:
565 	biodone(bp);
566 }
567 
568 void
569 sdustart(unit)
570 	register int unit;
571 {
572 	if (scsireq(&sd_softc[unit].sc_dq))
573 		sdstart(unit);
574 }
575 
576 /*
577  * Return:
578  *	0	if not really an error
579  *	<0	if we should do a retry
580  *	>0	if a fatal error
581  */
582 static int
583 sderror(unit, sc, hp, stat)
584 	int unit, stat;
585 	register struct sd_softc *sc;
586 	register struct hp_device *hp;
587 {
588 	int cond = 1;
589 
590 	sdsense[unit].status = stat;
591 	if (stat & STS_CHECKCOND) {
592 		struct scsi_xsense *sp;
593 
594 		scsi_request_sense(hp->hp_ctlr, hp->hp_slave,
595 				   sc->sc_punit, sdsense[unit].sense,
596 				   sizeof(sdsense[unit].sense));
597 		sp = (struct scsi_xsense *)sdsense[unit].sense;
598 		printf("sd%d: scsi sense class %d, code %d", unit,
599 			sp->class, sp->code);
600 		if (sp->class == 7) {
601 			printf(", key %d", sp->key);
602 			if (sp->valid)
603 				printf(", blk %d", *(int *)&sp->info1);
604 			switch (sp->key) {
605 			/* no sense, try again */
606 			case 0:
607 				cond = -1;
608 				break;
609 			/* recovered error, not a problem */
610 			case 1:
611 				cond = 0;
612 				break;
613 			}
614 		}
615 		printf("\n");
616 	}
617 	return(cond);
618 }
619 
620 static void
621 sdfinish(unit, sc, bp)
622 	int unit;
623 	register struct sd_softc *sc;
624 	register struct buf *bp;
625 {
626 	register struct buf *dp = &sdtab[unit];
627 
628 	dp->b_errcnt = 0;
629 	dp->b_actf = bp->b_actf;
630 	bp->b_resid = 0;
631 	biodone(bp);
632 	scsifree(&sc->sc_dq);
633 	if (dp->b_actf)
634 		sdustart(unit);
635 	else {
636 		dp->b_active = 0;
637 		if (sc->sc_flags & SDF_WANTED) {
638 			sc->sc_flags &= ~SDF_WANTED;
639 			wakeup((caddr_t)dp);
640 		}
641 	}
642 }
643 
644 void
645 sdstart(unit)
646 	register int unit;
647 {
648 	register struct sd_softc *sc = &sd_softc[unit];
649 	register struct hp_device *hp = sc->sc_hd;
650 
651 	/*
652 	 * we have the SCSI bus -- in format mode, we may or may not need dma
653 	 * so check now.
654 	 */
655 	if (sc->sc_format_pid && legal_cmds[sdcmd[unit].cdb[0]] > 0) {
656 		register struct buf *bp = sdtab[unit].b_actf;
657 		register int sts;
658 
659 		sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave,
660 					 sc->sc_punit, &sdcmd[unit],
661 					 bp->b_un.b_addr, bp->b_bcount,
662 					 bp->b_flags & B_READ);
663 		sdsense[unit].status = sts;
664 		if (sts & 0xfe) {
665 			(void) sderror(unit, sc, hp, sts);
666 			bp->b_flags |= B_ERROR;
667 			bp->b_error = EIO;
668 		}
669 		sdfinish(unit, sc, bp);
670 
671 	} else if (scsiustart(hp->hp_ctlr))
672 		sdgo(unit);
673 }
674 
675 void
676 sdgo(unit)
677 	register int unit;
678 {
679 	register struct sd_softc *sc = &sd_softc[unit];
680 	register struct hp_device *hp = sc->sc_hd;
681 	register struct buf *bp = sdtab[unit].b_actf;
682 	register int pad;
683 	register struct scsi_fmt_cdb *cmd;
684 
685 	if (sc->sc_format_pid) {
686 		cmd = &sdcmd[unit];
687 		pad = 0;
688 	} else {
689 		cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd;
690 		*(int *)(&cmd->cdb[2]) = bp->b_cylin;
691 		pad = howmany(bp->b_bcount, sc->sc_blksize);
692 		*(u_short *)(&cmd->cdb[7]) = pad;
693 		pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0;
694 #ifdef DEBUG
695 		if (pad)
696 			printf("sd%d: partial block xfer -- %x bytes\n",
697 			       unit, bp->b_bcount);
698 #endif
699 		sdstats[unit].sdtransfers++;
700 	}
701 	if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) {
702 		if (hp->hp_dk >= 0) {
703 			dk_busy |= 1 << hp->hp_dk;
704 			++dk_seek[hp->hp_dk];
705 			++dk_xfer[hp->hp_dk];
706 			dk_wds[hp->hp_dk] += bp->b_bcount >> 6;
707 		}
708 		return;
709 	}
710 #ifdef DEBUG
711 	if (sddebug & SDB_ERROR)
712 		printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n",
713 		       unit, bp->b_flags & B_READ? "read" : "write",
714 		       bp->b_un.b_addr, bp->b_cylin, bp->b_bcount,
715 		       sdtab[unit].b_errcnt);
716 #endif
717 	bp->b_flags |= B_ERROR;
718 	bp->b_error = EIO;
719 	sdfinish(unit, sc, bp);
720 }
721 
722 void
723 sdintr(unit, stat)
724 	register int unit;
725 	int stat;
726 {
727 	register struct sd_softc *sc = &sd_softc[unit];
728 	register struct buf *bp = sdtab[unit].b_actf;
729 	register struct hp_device *hp = sc->sc_hd;
730 	int cond;
731 
732 	if (bp == NULL) {
733 		printf("sd%d: bp == NULL\n", unit);
734 		return;
735 	}
736 	if (hp->hp_dk >= 0)
737 		dk_busy &=~ (1 << hp->hp_dk);
738 	if (stat) {
739 #ifdef DEBUG
740 		if (sddebug & SDB_ERROR)
741 			printf("sd%d: sdintr: bad scsi status 0x%x\n",
742 				unit, stat);
743 #endif
744 		cond = sderror(unit, sc, hp, stat);
745 		if (cond) {
746 			if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) {
747 #ifdef DEBUG
748 				if (sddebug & SDB_ERROR)
749 					printf("sd%d: retry #%d\n",
750 					       unit, sdtab[unit].b_errcnt);
751 #endif
752 				sdstart(unit);
753 				return;
754 			}
755 			bp->b_flags |= B_ERROR;
756 			bp->b_error = EIO;
757 		}
758 	}
759 	sdfinish(unit, sc, bp);
760 }
761 
762 int
763 sdread(dev, uio, flags)
764 	dev_t dev;
765 	struct uio *uio;
766 	int flags;
767 {
768 	register int unit = sdunit(dev);
769 	register int pid;
770 
771 	if ((pid = sd_softc[unit].sc_format_pid) &&
772 	    pid != uio->uio_procp->p_pid)
773 		return (EPERM);
774 
775 	return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio));
776 }
777 
778 int
779 sdwrite(dev, uio, flags)
780 	dev_t dev;
781 	struct uio *uio;
782 	int flags;
783 {
784 	register int unit = sdunit(dev);
785 	register int pid;
786 
787 	if ((pid = sd_softc[unit].sc_format_pid) &&
788 	    pid != uio->uio_procp->p_pid)
789 		return (EPERM);
790 
791 	return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio));
792 }
793 
794 int
795 sdioctl(dev, cmd, data, flag, p)
796 	dev_t dev;
797 	int cmd;
798 	caddr_t data;
799 	int flag;
800 	struct proc *p;
801 {
802 	register int unit = sdunit(dev);
803 	register struct sd_softc *sc = &sd_softc[unit];
804 
805 	switch (cmd) {
806 	default:
807 		return (EINVAL);
808 
809 	case SDIOCSFORMAT:
810 		/* take this device into or out of "format" mode */
811 		if (suser(p->p_ucred, &p->p_acflag))
812 			return(EPERM);
813 
814 		if (*(int *)data) {
815 			if (sc->sc_format_pid)
816 				return (EPERM);
817 			sc->sc_format_pid = p->p_pid;
818 		} else
819 			sc->sc_format_pid = 0;
820 		return (0);
821 
822 	case SDIOCGFORMAT:
823 		/* find out who has the device in format mode */
824 		*(int *)data = sc->sc_format_pid;
825 		return (0);
826 
827 	case SDIOCSCSICOMMAND:
828 		/*
829 		 * Save what user gave us as SCSI cdb to use with next
830 		 * read or write to the char device.
831 		 */
832 		if (sc->sc_format_pid != p->p_pid)
833 			return (EPERM);
834 		if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
835 			return (EINVAL);
836 		bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0]));
837 		return (0);
838 
839 	case SDIOCSENSE:
840 		/*
841 		 * return the SCSI sense data saved after the last
842 		 * operation that completed with "check condition" status.
843 		 */
844 		bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0]));
845 		return (0);
846 
847 	}
848 	/*NOTREACHED*/
849 }
850 
851 int
852 sdsize(dev)
853 	dev_t dev;
854 {
855 	register int unit = sdunit(dev);
856 	register struct sd_softc *sc = &sd_softc[unit];
857 
858 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
859 		return(-1);
860 
861 	return(sc->sc_info.part[sdpart(dev)].nblocks);
862 }
863 
864 /*
865  * Non-interrupt driven, non-dma dump routine.
866  */
867 int
868 sddump(dev)
869 	dev_t dev;
870 {
871 	int part = sdpart(dev);
872 	int unit = sdunit(dev);
873 	register struct sd_softc *sc = &sd_softc[unit];
874 	register struct hp_device *hp = sc->sc_hd;
875 	register daddr_t baddr;
876 	register int maddr;
877 	register int pages, i;
878 	int stat;
879 	extern int lowram;
880 
881 	/*
882 	 * Hmm... all vax drivers dump maxfree pages which is physmem minus
883 	 * the message buffer.  Is there a reason for not dumping the
884 	 * message buffer?  Savecore expects to read 'dumpsize' pages of
885 	 * dump, where dumpsys() sets dumpsize to physmem!
886 	 */
887 	pages = physmem;
888 
889 	/* is drive ok? */
890 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
891 		return (ENXIO);
892 	/* dump parameters in range? */
893 	if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks)
894 		return (EINVAL);
895 	if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks)
896 		pages = dtoc(sc->sc_info.part[part].nblocks - dumplo);
897 	maddr = lowram;
898 	baddr = dumplo + sc->sc_info.part[part].strtblk;
899 	/* scsi bus idle? */
900 	if (!scsireq(&sc->sc_dq)) {
901 		scsireset(hp->hp_ctlr);
902 		sdreset(sc, sc->sc_hd);
903 		printf("[ drive %d reset ] ", unit);
904 	}
905 	for (i = 0; i < pages; i++) {
906 #define NPGMB	(1024*1024/NBPG)
907 		/* print out how many Mbs we have dumped */
908 		if (i && (i % NPGMB) == 0)
909 			printf("%d ", i / NPGMB);
910 #undef NPBMG
911 		pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
912 		    VM_PROT_READ, TRUE);
913 		stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit,
914 				     vmmap, NBPG, baddr, sc->sc_bshift);
915 		if (stat) {
916 			printf("sddump: scsi write error 0x%x\n", stat);
917 			return (EIO);
918 		}
919 		maddr += NBPG;
920 		baddr += ctod(1);
921 	}
922 	return (0);
923 }
924 #endif
925