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