xref: /original-bsd/sys/pmax/dev/rz.c (revision 0997b878)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)rz.c	8.3 (Berkeley) 11/30/94
11  */
12 
13 /*
14  * SCSI CCS (Command Command Set) disk driver.
15  * NOTE: The name was changed from "sd" to "rz" for DEC naming compatibility.
16  * I guess I can't avoid confusion someplace.
17  */
18 #include "rz.h"
19 #if NRZ > 0
20 
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/buf.h>
24 #include <sys/errno.h>
25 #include <sys/fcntl.h>
26 #include <sys/ioctl.h>
27 #include <sys/dkstat.h>
28 #include <sys/disklabel.h>
29 #include <sys/malloc.h>
30 #include <sys/proc.h>
31 #include <sys/uio.h>
32 #include <sys/stat.h>
33 #include <sys/syslog.h>
34 
35 #include <ufs/ffs/fs.h>
36 
37 #include <pmax/dev/device.h>
38 #include <pmax/dev/scsi.h>
39 
40 extern int splbio();
41 extern void splx();
42 extern int physio();
43 
44 int	rzprobe();
45 void	rzstrategy(), rzstart(), rzdone();
46 
47 struct	driver rzdriver = {
48 	"rz", rzprobe, rzstart, rzdone,
49 };
50 
51 struct	size {
52 	u_long	strtblk;
53 	u_long	nblocks;
54 };
55 
56 /*
57  * Since the SCSI standard tends to hide the disk structure, we define
58  * partitions in terms of DEV_BSIZE blocks.  The default partition table
59  * (for an unlabeled disk) reserves 8K for a boot area, has an 8 meg
60  * root and 32 meg of swap.  The rest of the space on the drive goes in
61  * the G partition.  As usual, the C partition covers the entire disk
62  * (including the boot area).
63  */
64 static struct size rzdefaultpart[MAXPARTITIONS] = {
65 	        0,   16384,	/* A */
66 	    16384,   65536,	/* B */
67 	        0,       0,	/* C */
68 	    17408,       0,	/* D */
69 	   115712,       0,	/* E */
70 	   218112,       0,	/* F */
71 	    81920,       0,	/* G */
72 	   115712,       0,	/* H */
73 };
74 
75 #define	RAWPART		2	/* 'c' partition */	/* XXX */
76 
77 struct rzstats {
78 	long	rzresets;
79 	long	rztransfers;
80 	long	rzpartials;
81 };
82 
83 struct	rz_softc {
84 	struct	scsi_device *sc_sd;	/* physical unit info */
85 	pid_t	sc_format_pid;		/* process using "format" mode */
86 	u_long	sc_openpart;		/* partitions open */
87 	u_long	sc_bopenpart;		/* block partitions open */
88 	u_long	sc_copenpart;		/* character partitions open */
89 	short	sc_flags;		/* see below */
90 	short	sc_type;		/* drive type from INQUIRY cmd */
91 	u_int	sc_blks;		/* number of blocks on device */
92 	int	sc_blksize;		/* device block size in bytes */
93 	int	sc_bshift;		/* convert device blocks to DEV_BSIZE */
94 	u_int	sc_wpms;		/* average xfer rate in 16bit wds/sec */
95 	struct	disklabel sc_label;	/* disk label for this disk */
96 	struct	rzstats sc_stats;	/* statisic counts */
97 	struct	buf sc_tab;		/* queue of pending operations */
98 	struct	buf sc_buf;		/* buf for doing I/O */
99 	struct	buf sc_errbuf;		/* buf for doing REQUEST_SENSE */
100 	struct	ScsiCmd sc_cmd;		/* command for controller */
101 	ScsiGroup1Cmd sc_rwcmd;		/* SCSI cmd if not in "format" mode */
102 	struct	scsi_fmt_cdb sc_cdb;	/* SCSI cmd if in "format" mode */
103 	struct	scsi_fmt_sense sc_sense;	/* sense data from last cmd */
104 	u_char	sc_capbuf[8];		/* buffer for SCSI_READ_CAPACITY */
105 } rz_softc[NRZ];
106 
107 /* sc_flags values */
108 #define	RZF_ALIVE		0x0001	/* drive found and ready */
109 #define	RZF_SENSEINPROGRESS	0x0002	/* REQUEST_SENSE command in progress */
110 #define	RZF_ALTCMD		0x0004	/* alternate command in progress */
111 #define	RZF_HAVELABEL		0x0008	/* valid label found on disk */
112 #define	RZF_WLABEL		0x0010	/* label is writeable */
113 #define	RZF_WAIT		0x0020	/* waiting for sc_tab to drain */
114 #define	RZF_REMOVEABLE		0x0040	/* disk is removable */
115 #define	RZF_TRYSYNC		0x0080	/* try synchronous operation */
116 #define	RZF_NOERR		0x0100	/* don't print error messages */
117 
118 #ifdef DEBUG
119 int	rzdebug = 3;
120 #define RZB_ERROR	0x01
121 #define RZB_PARTIAL	0x02
122 #define RZB_PRLABEL	0x04
123 #endif
124 
125 #define	rzunit(x)	(minor(x) >> 3)
126 #define rzpart(x)	(minor(x) & 0x7)
127 #define	b_cylin		b_resid
128 
129 /*
130  * Table of scsi commands users are allowed to access via "format" mode.
131  *  0 means not legal.
132  *  1 means legal.
133  */
134 static char legal_cmds[256] = {
135 /*****  0   1   2   3   4   5   6   7     8   9   A   B   C   D   E   F */
136 /*00*/	0,  0,  0,  0,  1,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
137 /*10*/	0,  0,  1,  0,  0,  1,  0,  0,    0,  0,  1,  0,  0,  0,  0,  0,
138 /*20*/	0,  0,  0,  0,  0,  1,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
139 /*30*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
140 /*40*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
141 /*50*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
142 /*60*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
143 /*70*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
144 /*80*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
145 /*90*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
146 /*a0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
147 /*b0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
148 /*c0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
149 /*d0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
150 /*e0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
151 /*f0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
152 };
153 
154 /*
155  * Test to see if the unit is ready and if not, try to make it ready.
156  * Also, find the drive capacity.
157  */
158 static int
159 rzready(sc)
160 	register struct rz_softc *sc;
161 {
162 	register int tries, i;
163 	ScsiClass7Sense *sp;
164 
165 	/* don't print SCSI errors */
166 	sc->sc_flags |= RZF_NOERR;
167 
168 	/* see if the device is ready */
169 	for (tries = 10; ; ) {
170 		sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
171 		scsiGroup0Cmd(SCSI_TEST_UNIT_READY, sc->sc_rwcmd.unitNumber,
172 			0, 0, (ScsiGroup0Cmd *)sc->sc_cdb.cdb);
173 		sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
174 		sc->sc_buf.b_bcount = 0;
175 		sc->sc_buf.b_un.b_addr = (caddr_t)0;
176 		sc->sc_buf.b_actf = (struct buf *)0;
177 		sc->sc_tab.b_actf = &sc->sc_buf;
178 
179 		sc->sc_cmd.cmd = sc->sc_cdb.cdb;
180 		sc->sc_cmd.cmdlen = sc->sc_cdb.len;
181 		sc->sc_cmd.buf = (caddr_t)0;
182 		sc->sc_cmd.buflen = 0;
183 		/* setup synchronous data transfers if the device supports it */
184 		if (tries == 10 && (sc->sc_flags & RZF_TRYSYNC))
185 			sc->sc_cmd.flags = SCSICMD_USE_SYNC;
186 		else
187 			sc->sc_cmd.flags = 0;
188 
189 		(*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd);
190 		if (!biowait(&sc->sc_buf))
191 			break;
192 		if (--tries < 0)
193 			return (0);
194 		if (!(sc->sc_sense.status & SCSI_STATUS_CHECKCOND))
195 			goto again;
196 		sp = (ScsiClass7Sense *)sc->sc_sense.sense;
197 		if (sp->error7 != 0x70)
198 			goto again;
199 		if (sp->key == SCSI_CLASS7_UNIT_ATTN && tries != 9) {
200 			/* drive recalibrating, give it a while */
201 			DELAY(1000000);
202 			continue;
203 		}
204 		if (sp->key == SCSI_CLASS7_NOT_READY) {
205 			ScsiStartStopCmd *cp;
206 
207 			/* try to spin-up disk with start/stop command */
208 			sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
209 			cp = (ScsiStartStopCmd *)sc->sc_cdb.cdb;
210 			cp->command = SCSI_START_STOP;
211 			cp->unitNumber = sc->sc_rwcmd.unitNumber;
212 			cp->immed = 0;
213 			cp->loadEject = 0;
214 			cp->start = 1;
215 			cp->pad1 = 0;
216 			cp->pad2 = 0;
217 			cp->pad3 = 0;
218 			cp->pad4 = 0;
219 			cp->control = 0;
220 			sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
221 			sc->sc_buf.b_bcount = 0;
222 			sc->sc_buf.b_un.b_addr = (caddr_t)0;
223 			sc->sc_buf.b_actf = (struct buf *)0;
224 			sc->sc_tab.b_actf = &sc->sc_buf;
225 			rzstart(sc->sc_cmd.unit);
226 			if (biowait(&sc->sc_buf))
227 				return (0);
228 			continue;
229 		}
230 	again:
231 		DELAY(1000);
232 	}
233 
234 	/* print SCSI errors */
235 	sc->sc_flags &= ~RZF_NOERR;
236 
237 	/* find out how big a disk this is */
238 	sc->sc_cdb.len = sizeof(ScsiGroup1Cmd);
239 	scsiGroup1Cmd(SCSI_READ_CAPACITY, sc->sc_rwcmd.unitNumber, 0, 0,
240 		(ScsiGroup1Cmd *)sc->sc_cdb.cdb);
241 	sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
242 	sc->sc_buf.b_bcount = sizeof(sc->sc_capbuf);
243 	sc->sc_buf.b_un.b_addr = (caddr_t)sc->sc_capbuf;
244 	sc->sc_buf.b_actf = (struct buf *)0;
245 	sc->sc_tab.b_actf = &sc->sc_buf;
246 	sc->sc_flags |= RZF_ALTCMD;
247 	rzstart(sc->sc_cmd.unit);
248 	sc->sc_flags &= ~RZF_ALTCMD;
249 	if (biowait(&sc->sc_buf) || sc->sc_buf.b_resid != 0)
250 		return (0);
251 	sc->sc_blks = ((sc->sc_capbuf[0] << 24) | (sc->sc_capbuf[1] << 16) |
252 		(sc->sc_capbuf[2] << 8) | sc->sc_capbuf[3]) + 1;
253 	sc->sc_blksize = (sc->sc_capbuf[4] << 24) | (sc->sc_capbuf[5] << 16) |
254 		(sc->sc_capbuf[6] << 8) | sc->sc_capbuf[7];
255 
256 	sc->sc_bshift = 0;
257 	for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
258 		++sc->sc_bshift;
259 	sc->sc_blks <<= sc->sc_bshift;
260 
261 	return (1);
262 }
263 
264 /*
265  * Test to see if device is present.
266  * Return true if found and initialized ok.
267  */
268 rzprobe(sd)
269 	register struct scsi_device *sd;
270 {
271 	register struct rz_softc *sc = &rz_softc[sd->sd_unit];
272 	register int i;
273 	ScsiInquiryData inqbuf;
274 	ScsiClass7Sense *sp;
275 
276 	/* init some parameters that don't change */
277 	sc->sc_sd = sd;
278 	sc->sc_cmd.sd = sd;
279 	sc->sc_cmd.unit = sd->sd_unit;
280 	sc->sc_rwcmd.unitNumber = sd->sd_slave;
281 
282 	/* try to find out what type of device this is */
283 	sc->sc_format_pid = 1;		/* force use of sc_cdb */
284 	sc->sc_flags = RZF_NOERR;	/* don't print SCSI errors */
285 	sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
286 	scsiGroup0Cmd(SCSI_INQUIRY, sd->sd_slave, 0, sizeof(inqbuf),
287 		(ScsiGroup0Cmd *)sc->sc_cdb.cdb);
288 	sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
289 	sc->sc_buf.b_bcount = sizeof(inqbuf);
290 	sc->sc_buf.b_un.b_addr = (caddr_t)&inqbuf;
291 	sc->sc_buf.b_actf = (struct buf *)0;
292 	sc->sc_tab.b_actf = &sc->sc_buf;
293 	rzstart(sd->sd_unit);
294 	if (biowait(&sc->sc_buf) ||
295 	    (i = sizeof(inqbuf) - sc->sc_buf.b_resid) < 5)
296 		goto bad;
297 	switch (inqbuf.type) {
298 	case SCSI_DISK_TYPE:		/* disk */
299 	case SCSI_WORM_TYPE:		/* WORM */
300 	case SCSI_ROM_TYPE:		/* CD-ROM */
301 	case SCSI_OPTICAL_MEM_TYPE:	/* Magneto-optical */
302 		break;
303 
304 	default:			/* not a disk */
305 		goto bad;
306 	}
307 	sc->sc_type = inqbuf.type;
308 	if (inqbuf.flags & SCSI_SYNC)
309 		sc->sc_flags |= RZF_TRYSYNC;
310 
311 	if (!inqbuf.rmb) {
312 		if (!rzready(sc))
313 			goto bad;
314 	}
315 
316 	printf("rz%d at %s%d drive %d slave %d", sd->sd_unit,
317 		sd->sd_cdriver->d_name, sd->sd_ctlr, sd->sd_drive,
318 		sd->sd_slave);
319 	if (inqbuf.version > 2 || i < 36)
320 		printf(" type 0x%x, qual 0x%x, ver %d",
321 			inqbuf.type, inqbuf.qualifier, inqbuf.version);
322 	else {
323 		char vid[9], pid[17], revl[5];
324 
325 		bcopy((caddr_t)inqbuf.vendorID, (caddr_t)vid, 8);
326 		bcopy((caddr_t)inqbuf.productID, (caddr_t)pid, 16);
327 		bcopy((caddr_t)inqbuf.revLevel, (caddr_t)revl, 4);
328 		for (i = 8; --i > 0; )
329 			if (vid[i] != ' ')
330 				break;
331 		vid[i+1] = 0;
332 		for (i = 16; --i > 0; )
333 			if (pid[i] != ' ')
334 				break;
335 		pid[i+1] = 0;
336 		for (i = 4; --i > 0; )
337 			if (revl[i] != ' ')
338 				break;
339 		revl[i+1] = 0;
340 		printf(" %s %s rev %s", vid, pid, revl);
341 	}
342 	printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize);
343 	if (!inqbuf.rmb && sc->sc_blksize != DEV_BSIZE) {
344 		if (sc->sc_blksize < DEV_BSIZE) {
345 			printf("rz%d: need %d byte blocks - drive ignored\n",
346 				sd->sd_unit, DEV_BSIZE);
347 			goto bad;
348 		}
349 	}
350 	sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
351 	sc->sc_format_pid = 0;
352 	sc->sc_flags |= RZF_ALIVE;
353 	if (inqbuf.rmb)
354 		sc->sc_flags |= RZF_REMOVEABLE;
355 	sc->sc_buf.b_flags = 0;
356 	return (1);
357 
358 bad:
359 	/* doesn't exist or not a CCS device */
360 	sc->sc_format_pid = 0;
361 	sc->sc_buf.b_flags = 0;
362 	return (0);
363 }
364 
365 /*
366  * This routine is called for partial block transfers and non-aligned
367  * transfers (the latter only being possible on devices with a block size
368  * larger than DEV_BSIZE).  The operation is performed in three steps
369  * using a locally allocated buffer:
370  *	1. transfer any initial partial block
371  *	2. transfer full blocks
372  *	3. transfer any final partial block
373  */
374 static void
375 rzlblkstrat(bp, bsize)
376 	register struct buf *bp;
377 	register int bsize;
378 {
379 	register struct buf *cbp;
380 	caddr_t cbuf;
381 	register int bn, resid;
382 	register caddr_t addr;
383 
384 	cbp = (struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK);
385 	cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
386 	bzero((caddr_t)cbp, sizeof(*cbp));
387 	cbp->b_proc = curproc;
388 	cbp->b_dev = bp->b_dev;
389 	bn = bp->b_blkno;
390 	resid = bp->b_bcount;
391 	addr = bp->b_un.b_addr;
392 #ifdef DEBUG
393 	if (rzdebug & RZB_PARTIAL)
394 		printf("rzlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
395 		       bp, bp->b_flags, bn, resid, addr);
396 #endif
397 
398 	while (resid > 0) {
399 		register int boff = dbtob(bn) & (bsize - 1);
400 		register int count;
401 
402 		if (boff || resid < bsize) {
403 			rz_softc[rzunit(bp->b_dev)].sc_stats.rzpartials++;
404 			count = min(resid, bsize - boff);
405 			cbp->b_flags = B_BUSY | B_PHYS | B_READ;
406 			cbp->b_blkno = bn - btodb(boff);
407 			cbp->b_un.b_addr = cbuf;
408 			cbp->b_bcount = bsize;
409 #ifdef DEBUG
410 			if (rzdebug & RZB_PARTIAL)
411 				printf(" readahead: bn %x cnt %x off %x addr %x\n",
412 				       cbp->b_blkno, count, boff, addr);
413 #endif
414 			rzstrategy(cbp);
415 			biowait(cbp);
416 			if (cbp->b_flags & B_ERROR) {
417 				bp->b_flags |= B_ERROR;
418 				bp->b_error = cbp->b_error;
419 				break;
420 			}
421 			if (bp->b_flags & B_READ) {
422 				bcopy(&cbuf[boff], addr, count);
423 				goto done;
424 			}
425 			bcopy(addr, &cbuf[boff], count);
426 #ifdef DEBUG
427 			if (rzdebug & RZB_PARTIAL)
428 				printf(" writeback: bn %x cnt %x off %x addr %x\n",
429 				       cbp->b_blkno, count, boff, addr);
430 #endif
431 		} else {
432 			count = resid & ~(bsize - 1);
433 			cbp->b_blkno = bn;
434 			cbp->b_un.b_addr = addr;
435 			cbp->b_bcount = count;
436 #ifdef DEBUG
437 			if (rzdebug & RZB_PARTIAL)
438 				printf(" fulltrans: bn %x cnt %x addr %x\n",
439 				       cbp->b_blkno, count, addr);
440 #endif
441 		}
442 		cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
443 		rzstrategy(cbp);
444 		biowait(cbp);
445 		if (cbp->b_flags & B_ERROR) {
446 			bp->b_flags |= B_ERROR;
447 			bp->b_error = cbp->b_error;
448 			break;
449 		}
450 done:
451 		bn += btodb(count);
452 		resid -= count;
453 		addr += count;
454 #ifdef DEBUG
455 		if (rzdebug & RZB_PARTIAL)
456 			printf(" done: bn %x resid %x addr %x\n",
457 			       bn, resid, addr);
458 #endif
459 	}
460 	free(cbuf, M_DEVBUF);
461 	free(cbp, M_DEVBUF);
462 }
463 
464 void
465 rzstrategy(bp)
466 	register struct buf *bp;
467 {
468 	register int unit = rzunit(bp->b_dev);
469 	register int part = rzpart(bp->b_dev);
470 	register struct rz_softc *sc = &rz_softc[unit];
471 	register struct partition *pp = &sc->sc_label.d_partitions[part];
472 	register daddr_t bn;
473 	register long sz, s;
474 
475 	if (sc->sc_format_pid) {
476 		if (sc->sc_format_pid != curproc->p_pid) {
477 			bp->b_error = EPERM;
478 			goto bad;
479 		}
480 		bp->b_cylin = 0;
481 	} else {
482 		bn = bp->b_blkno;
483 		sz = howmany(bp->b_bcount, DEV_BSIZE);
484 		if ((unsigned)bn + sz > pp->p_size) {
485 			sz = pp->p_size - bn;
486 			/* if exactly at end of disk, return an EOF */
487 			if (sz == 0) {
488 				bp->b_resid = bp->b_bcount;
489 				goto done;
490 			}
491 			/* if none of it fits, error */
492 			if (sz < 0) {
493 				bp->b_error = EINVAL;
494 				goto bad;
495 			}
496 			/* otherwise, truncate */
497 			bp->b_bcount = dbtob(sz);
498 		}
499 		/* check for write to write protected label */
500 		if (bn + pp->p_offset <= LABELSECTOR &&
501 #if LABELSECTOR != 0
502 		    bn + pp->p_offset + sz > LABELSECTOR &&
503 #endif
504 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RZF_WLABEL)) {
505 			bp->b_error = EROFS;
506 			goto bad;
507 		}
508 		/*
509 		 * Non-aligned or partial-block transfers handled specially.
510 		 */
511 		s = sc->sc_blksize - 1;
512 		if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
513 			rzlblkstrat(bp, sc->sc_blksize);
514 			goto done;
515 		}
516 		bp->b_cylin = (bn + pp->p_offset) >> sc->sc_bshift;
517 	}
518 	/* don't let disksort() see sc_errbuf */
519 	while (sc->sc_flags & RZF_SENSEINPROGRESS)
520 		printf("SENSE\n"); /* XXX */
521 	s = splbio();
522 	disksort(&sc->sc_tab, bp);
523 	if (sc->sc_tab.b_active == 0) {
524 		sc->sc_tab.b_active = 1;
525 		rzstart(unit);
526 	}
527 	splx(s);
528 	return;
529 bad:
530 	bp->b_flags |= B_ERROR;
531 done:
532 	biodone(bp);
533 }
534 
535 void
536 rzstart(unit)
537 	int unit;
538 {
539 	register struct rz_softc *sc = &rz_softc[unit];
540 	register struct buf *bp = sc->sc_tab.b_actf;
541 	register int n;
542 
543 	sc->sc_cmd.buf = bp->b_un.b_addr;
544 	sc->sc_cmd.buflen = bp->b_bcount;
545 
546 	if (sc->sc_format_pid ||
547 	    (sc->sc_flags & (RZF_SENSEINPROGRESS | RZF_ALTCMD))) {
548 		sc->sc_cmd.flags = !(bp->b_flags & B_READ) ?
549 			SCSICMD_DATA_TO_DEVICE : 0;
550 		sc->sc_cmd.cmd = sc->sc_cdb.cdb;
551 		sc->sc_cmd.cmdlen = sc->sc_cdb.len;
552 	} else {
553 		if (bp->b_flags & B_READ) {
554 			sc->sc_cmd.flags = 0;
555 			sc->sc_rwcmd.command = SCSI_READ_EXT;
556 		} else {
557 			sc->sc_cmd.flags = SCSICMD_DATA_TO_DEVICE;
558 			sc->sc_rwcmd.command = SCSI_WRITE_EXT;
559 		}
560 		sc->sc_cmd.cmd = (u_char *)&sc->sc_rwcmd;
561 		sc->sc_cmd.cmdlen = sizeof(sc->sc_rwcmd);
562 		n = bp->b_cylin;
563 		sc->sc_rwcmd.highAddr = n >> 24;
564 		sc->sc_rwcmd.midHighAddr = n >> 16;
565 		sc->sc_rwcmd.midLowAddr = n >> 8;
566 		sc->sc_rwcmd.lowAddr = n;
567 		n = howmany(bp->b_bcount, sc->sc_blksize);
568 		sc->sc_rwcmd.highBlockCount = n >> 8;
569 		sc->sc_rwcmd.lowBlockCount = n;
570 #ifdef DEBUG
571 		if ((bp->b_bcount & (sc->sc_blksize - 1)) != 0)
572 			printf("rz%d: partial block xfer -- %x bytes\n",
573 				unit, bp->b_bcount);
574 #endif
575 		sc->sc_stats.rztransfers++;
576 		if ((n = sc->sc_sd->sd_dk) >= 0) {
577 			dk_busy |= 1 << n;
578 			++dk_seek[n];
579 			++dk_xfer[n];
580 			dk_wds[n] += bp->b_bcount >> 6;
581 		}
582 	}
583 
584 	/* tell controller to start this command */
585 	(*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd);
586 }
587 
588 /*
589  * This is called by the controller driver when the command is done.
590  */
591 void
592 rzdone(unit, error, resid, status)
593 	register int unit;
594 	int error;		/* error number from errno.h */
595 	int resid;		/* amount not transfered */
596 	int status;		/* SCSI status byte */
597 {
598 	register struct rz_softc *sc = &rz_softc[unit];
599 	register struct buf *bp = sc->sc_tab.b_actf;
600 	register struct scsi_device *sd = sc->sc_sd;
601 
602 	if (bp == NULL) {
603 		printf("rz%d: bp == NULL\n", unit);
604 		return;
605 	}
606 	if (sd->sd_dk >= 0)
607 		dk_busy &= ~(1 << sd->sd_dk);
608 	if (sc->sc_flags & RZF_SENSEINPROGRESS) {
609 		sc->sc_flags &= ~RZF_SENSEINPROGRESS;
610 		sc->sc_tab.b_actf = bp = bp->b_actf;	/* remove sc_errbuf */
611 
612 		if (error || (status & SCSI_STATUS_CHECKCOND)) {
613 #ifdef DEBUG
614 			if (rzdebug & RZB_ERROR)
615 				printf("rz%d: error reading sense data: error %d scsi status 0x%x\n",
616 					unit, error, status);
617 #endif
618 			/*
619 			 * We got an error during the REQUEST_SENSE,
620 			 * fill in no sense for data.
621 			 */
622 			sc->sc_sense.sense[0] = 0x70;
623 			sc->sc_sense.sense[2] = SCSI_CLASS7_NO_SENSE;
624 		} else if (!(sc->sc_flags & RZF_NOERR)) {
625 			printf("rz%d: ", unit);
626 			scsiPrintSense((ScsiClass7Sense *)sc->sc_sense.sense,
627 				sizeof(sc->sc_sense.sense) - resid);
628 		}
629 	} else if (error || (status & SCSI_STATUS_CHECKCOND)) {
630 #ifdef DEBUG
631 		if (!(sc->sc_flags & RZF_NOERR) && (rzdebug & RZB_ERROR))
632 			printf("rz%d: error %d scsi status 0x%x\n",
633 				unit, error, status);
634 #endif
635 		/* save error info */
636 		sc->sc_sense.status = status;
637 		bp->b_flags |= B_ERROR;
638 		bp->b_error = error;
639 		bp->b_resid = resid;
640 
641 		if (status & SCSI_STATUS_CHECKCOND) {
642 			/*
643 			 * Start a REQUEST_SENSE command.
644 			 * Since we are called at interrupt time, we can't
645 			 * wait for the command to finish; that's why we use
646 			 * the sc_flags field.
647 			 */
648 			sc->sc_flags |= RZF_SENSEINPROGRESS;
649 			sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
650 			scsiGroup0Cmd(SCSI_REQUEST_SENSE, sd->sd_slave, 0,
651 				sizeof(sc->sc_sense.sense),
652 				(ScsiGroup0Cmd *)sc->sc_cdb.cdb);
653 			sc->sc_errbuf.b_flags = B_BUSY | B_PHYS | B_READ;
654 			sc->sc_errbuf.b_bcount = sizeof(sc->sc_sense.sense);
655 			sc->sc_errbuf.b_un.b_addr = (caddr_t)sc->sc_sense.sense;
656 			sc->sc_errbuf.b_actf = bp;
657 			sc->sc_tab.b_actf = &sc->sc_errbuf;
658 			rzstart(unit);
659 			return;
660 		}
661 	} else {
662 		sc->sc_sense.status = status;
663 		bp->b_resid = resid;
664 	}
665 
666 	sc->sc_tab.b_actf = bp->b_actf;
667 	biodone(bp);
668 	if (sc->sc_tab.b_actf)
669 		rzstart(unit);
670 	else {
671 		sc->sc_tab.b_active = 0;
672 		/* finish close protocol */
673 		if (sc->sc_openpart == 0)
674 			wakeup((caddr_t)&sc->sc_tab);
675 	}
676 }
677 
678 /*
679  * Read or constuct a disklabel
680  */
681 void
682 rzgetinfo(dev)
683 	dev_t dev;
684 {
685 	register int unit = rzunit(dev);
686 	register struct rz_softc *sc = &rz_softc[unit];
687 	register struct disklabel *lp = &sc->sc_label;
688 	register int i;
689 	char *msg;
690 	int part;
691 	extern char *readdisklabel();
692 
693 	part = rzpart(dev);
694 	sc->sc_flags |= RZF_HAVELABEL;
695 
696 	if (sc->sc_type == SCSI_ROM_TYPE) {
697 		lp->d_type = DTYPE_SCSI;
698 		lp->d_secsize = sc->sc_blksize;
699 		lp->d_nsectors = 100;
700 		lp->d_ntracks = 1;
701 		lp->d_ncylinders = (sc->sc_blks / 100) + 1;
702 		lp->d_secpercyl	= 100;
703 		lp->d_secperunit = sc->sc_blks;
704 		lp->d_rpm = 300;
705 		lp->d_interleave = 1;
706 		lp->d_flags = D_REMOVABLE;
707 		lp->d_npartitions = 1;
708 		lp->d_partitions[0].p_offset = 0;
709 		lp->d_partitions[0].p_size = sc->sc_blks;
710 		lp->d_partitions[0].p_fstype = FS_ISO9660;
711 		lp->d_magic = DISKMAGIC;
712 		lp->d_magic2 = DISKMAGIC;
713 		lp->d_checksum = dkcksum(lp);
714 		return;
715 	}
716 
717 	lp->d_type = DTYPE_SCSI;
718 	lp->d_secsize = DEV_BSIZE;
719 	lp->d_secpercyl = 1 << sc->sc_bshift;
720 	lp->d_npartitions = MAXPARTITIONS;
721 	lp->d_partitions[part].p_offset = 0;
722 	lp->d_partitions[part].p_size = sc->sc_blks;
723 
724 	/*
725 	 * Now try to read the disklabel
726 	 */
727 	msg = readdisklabel(dev, rzstrategy, lp);
728 	if (msg == NULL)
729 		return;
730 
731 	printf("rz%d: WARNING: %s\n", unit, msg);
732 	lp->d_magic = DISKMAGIC;
733 	lp->d_magic2 = DISKMAGIC;
734 	lp->d_type = DTYPE_SCSI;
735 	lp->d_subtype = 0;
736 	lp->d_typename[0] = '\0';
737 	lp->d_secsize = DEV_BSIZE;
738 	lp->d_secperunit = sc->sc_blks;
739 	lp->d_npartitions = MAXPARTITIONS;
740 	lp->d_bbsize = BBSIZE;
741 	lp->d_sbsize = SBSIZE;
742 	for (i = 0; i < MAXPARTITIONS; i++) {
743 		lp->d_partitions[i].p_size = rzdefaultpart[i].nblocks;
744 		lp->d_partitions[i].p_offset = rzdefaultpart[i].strtblk;
745 	}
746 	lp->d_partitions[RAWPART].p_size = sc->sc_blks;
747 }
748 
749 int
750 rzopen(dev, flags, mode, p)
751 	dev_t dev;
752 	int flags, mode;
753 	struct proc *p;
754 {
755 	register int unit = rzunit(dev);
756 	register struct rz_softc *sc = &rz_softc[unit];
757 	register struct disklabel *lp;
758 	register int i;
759 	int part;
760 	u_long mask;
761 
762 	if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE))
763 		return (ENXIO);
764 
765 	/* make sure disk is ready */
766 	if (sc->sc_flags & RZF_REMOVEABLE) {
767 		if (!rzready(sc))
768 			return (ENXIO);
769 	}
770 
771 	/* try to read disk label and partition table information */
772 	part = rzpart(dev);
773 	if (!(sc->sc_flags & RZF_HAVELABEL))
774 		rzgetinfo(dev);
775 
776 	lp = &sc->sc_label;
777 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0)
778 		return (ENXIO);
779 	/*
780 	 * Warn if a partition is opened that overlaps another
781 	 * already open, unless either is the `raw' partition
782 	 * (whole disk).
783 	 */
784 	mask = 1 << part;
785 	if ((sc->sc_openpart & mask) == 0 && part != RAWPART) {
786 		register struct partition *pp;
787 		u_long start, end;
788 
789 		pp = &lp->d_partitions[part];
790 		start = pp->p_offset;
791 		end = pp->p_offset + pp->p_size;
792 		for (pp = lp->d_partitions, i = 0;
793 		     i < lp->d_npartitions; pp++, i++) {
794 			if (pp->p_offset + pp->p_size <= start ||
795 			    pp->p_offset >= end || i == RAWPART)
796 				continue;
797 			if (sc->sc_openpart & (1 << i))
798 				log(LOG_WARNING,
799 				    "rz%d%c: overlaps open partition (%c)\n",
800 				    unit, part + 'a', i + 'a');
801 		}
802 	}
803 	switch (mode) {
804 	case S_IFCHR:
805 		sc->sc_copenpart |= mask;
806 		break;
807 	case S_IFBLK:
808 		sc->sc_bopenpart |= mask;
809 		break;
810 	}
811 	sc->sc_openpart |= mask;
812 	if (sc->sc_sd->sd_dk >= 0)
813 		dk_wpms[sc->sc_sd->sd_dk] = sc->sc_wpms;
814 	return (0);
815 }
816 
817 rzclose(dev, flags, mode)
818 	dev_t dev;
819 	int flags, mode;
820 {
821 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
822 	u_long mask = (1 << rzpart(dev));
823 	int s;
824 
825 	switch (mode) {
826 	case S_IFCHR:
827 		sc->sc_copenpart &= ~mask;
828 		break;
829 	case S_IFBLK:
830 		sc->sc_bopenpart &= ~mask;
831 		break;
832 	}
833 	sc->sc_openpart = sc->sc_copenpart | sc->sc_bopenpart;
834 
835 	/*
836 	 * Should wait for I/O to complete on this partition even if
837 	 * others are open, but wait for work on blkflush().
838 	 */
839 	if (sc->sc_openpart == 0) {
840 		s = splbio();
841 		while (sc->sc_tab.b_actf)
842 			sleep((caddr_t)&sc->sc_tab, PZERO - 1);
843 		splx(s);
844 		sc->sc_flags &= ~RZF_WLABEL;
845 	}
846 	return (0);
847 }
848 
849 int
850 rzread(dev, uio)
851 	dev_t dev;
852 	struct uio *uio;
853 {
854 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
855 
856 	if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid)
857 		return (EPERM);
858 
859 	return (physio(rzstrategy, (struct buf *)0, dev,
860 		B_READ, minphys, uio));
861 }
862 
863 int
864 rzwrite(dev, uio)
865 	dev_t dev;
866 	struct uio *uio;
867 {
868 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
869 
870 	if (sc->sc_type == SCSI_ROM_TYPE)
871 		return (EROFS);
872 
873 	if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid)
874 		return (EPERM);
875 
876 	return (physio(rzstrategy, (struct buf *)0, dev,
877 		B_WRITE, minphys, uio));
878 }
879 
880 int
881 rzioctl(dev, cmd, data, flag, p)
882 	dev_t dev;
883 	int cmd;
884 	caddr_t data;
885 	int flag;
886 	struct proc *p;
887 {
888 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
889 	int error;
890 	int flags;
891 
892 	switch (cmd) {
893 	default:
894 		return (EINVAL);
895 
896 	case SDIOCSFORMAT:
897 		/* take this device into or out of "format" mode */
898 		if (suser(p->p_ucred, &p->p_acflag))
899 			return (EPERM);
900 
901 		if (*(int *)data) {
902 			if (sc->sc_format_pid)
903 				return (EPERM);
904 			sc->sc_format_pid = p->p_pid;
905 		} else
906 			sc->sc_format_pid = 0;
907 		return (0);
908 
909 	case SDIOCGFORMAT:
910 		/* find out who has the device in format mode */
911 		*(int *)data = sc->sc_format_pid;
912 		return (0);
913 
914 	case SDIOCSCSICOMMAND:
915 		/*
916 		 * Save what user gave us as SCSI cdb to use with next
917 		 * read or write to the char device.
918 		 */
919 		if (sc->sc_format_pid != p->p_pid)
920 			return (EPERM);
921 		if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
922 			return (EINVAL);
923 		bcopy(data, (caddr_t)&sc->sc_cdb, sizeof(sc->sc_cdb));
924 		return (0);
925 
926 	case SDIOCSENSE:
927 		/*
928 		 * return the SCSI sense data saved after the last
929 		 * operation that completed with "check condition" status.
930 		 */
931 		bcopy((caddr_t)&sc->sc_sense, data, sizeof(sc->sc_sense));
932 		return (0);
933 
934 	case DIOCGDINFO:
935 		/* get the current disk label */
936 		*(struct disklabel *)data = sc->sc_label;
937 		return (0);
938 
939 	case DIOCSDINFO:
940 		/* set the current disk label */
941 		if (!(flag & FWRITE))
942 			return (EBADF);
943 		error = setdisklabel(&sc->sc_label,
944 			(struct disklabel *)data,
945 			(sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart);
946 		return (error);
947 
948 	case DIOCGPART:
949 		/* return the disk partition data */
950 		((struct partinfo *)data)->disklab = &sc->sc_label;
951 		((struct partinfo *)data)->part =
952 			&sc->sc_label.d_partitions[rzpart(dev)];
953 		return (0);
954 
955 	case DIOCWLABEL:
956 		if (!(flag & FWRITE))
957 			return (EBADF);
958 		if (*(int *)data)
959 			sc->sc_flags |= RZF_WLABEL;
960 		else
961 			sc->sc_flags &= ~RZF_WLABEL;
962 		return (0);
963 
964 	case DIOCWDINFO:
965 		/* write the disk label to disk */
966 		if (!(flag & FWRITE))
967 			return (EBADF);
968 		error = setdisklabel(&sc->sc_label,
969 			(struct disklabel *)data,
970 			(sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart);
971 		if (error)
972 			return (error);
973 
974 		/* simulate opening partition 0 so write succeeds */
975 		flags = sc->sc_flags;
976 		sc->sc_flags = RZF_ALIVE | RZF_WLABEL;
977 		error = writedisklabel(dev, rzstrategy, &sc->sc_label);
978 		sc->sc_flags = flags;
979 		return (error);
980 	}
981 	/*NOTREACHED*/
982 }
983 
984 int
985 rzsize(dev)
986 	dev_t dev;
987 {
988 	register int unit = rzunit(dev);
989 	register int part = rzpart(dev);
990 	register struct rz_softc *sc = &rz_softc[unit];
991 
992 	if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE))
993 		return (-1);
994 
995 	/*
996 	 * We get called very early on (via swapconf)
997 	 * without the device being open so we need to
998 	 * read the disklabel here.
999 	 */
1000 	if (!(sc->sc_flags & RZF_HAVELABEL))
1001 		rzgetinfo(dev);
1002 
1003 	if (part >= sc->sc_label.d_npartitions)
1004 		return (-1);
1005 	return (sc->sc_label.d_partitions[part].p_size);
1006 }
1007 
1008 /*
1009  * Non-interrupt driven, non-dma dump routine.
1010  */
1011 int
1012 rzdump(dev)
1013 	dev_t dev;
1014 {
1015 #ifdef notdef
1016 	int part = rzpart(dev);
1017 	int unit = rzunit(dev);
1018 	register struct rz_softc *sc = &rz_softc[unit];
1019 	register struct scsi_device *sd = sc->sc_hd;
1020 	register daddr_t baddr;
1021 	register int maddr;
1022 	register int pages, i;
1023 	int stat;
1024 	extern int lowram;
1025 
1026 	/*
1027 	 * Hmm... all vax drivers dump maxfree pages which is physmem minus
1028 	 * the message buffer.  Is there a reason for not dumping the
1029 	 * message buffer?  Savecore expects to read 'dumpsize' pages of
1030 	 * dump, where dumpsys() sets dumpsize to physmem!
1031 	 */
1032 	pages = physmem;
1033 
1034 	/* is drive ok? */
1035 	if (unit >= NRZ || (sc->sc_flags & RZF_ALIVE) == 0)
1036 		return (ENXIO);
1037 	/* dump parameters in range? */
1038 	if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks)
1039 		return (EINVAL);
1040 	if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks)
1041 		pages = dtoc(sc->sc_info.part[part].nblocks - dumplo);
1042 	maddr = lowram;
1043 	baddr = dumplo + sc->sc_info.part[part].strtblk;
1044 	/* scsi bus idle? */
1045 	if (!scsireq(&sc->sc_dq)) {
1046 		scsireset(sd->sd_ctlr);
1047 		sc->sc_stats.rzresets++;
1048 		printf("[ drive %d reset ] ", unit);
1049 	}
1050 	for (i = 0; i < pages; i++) {
1051 #define NPGMB	(1024*1024/NBPG)
1052 		/* print out how many Mbs we have dumped */
1053 		if (i && (i % NPGMB) == 0)
1054 			printf("%d ", i / NPGMB);
1055 #undef NPBMG
1056 		mapin(mmap, (u_int)vmmap, btop(maddr), PG_URKR|PG_CI|PG_V);
1057 		stat = scsi_tt_write(sd->sd_ctlr, sd->sd_drive, sd->sd_slave,
1058 				     vmmap, NBPG, baddr, sc->sc_bshift);
1059 		if (stat) {
1060 			printf("rzdump: scsi write error 0x%x\n", stat);
1061 			return (EIO);
1062 		}
1063 		maddr += NBPG;
1064 		baddr += ctod(1);
1065 	}
1066 	return (0);
1067 #else /* notdef */
1068 	return (ENXIO);
1069 #endif /* notdef */
1070 }
1071 #endif
1072