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