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