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