xref: /csrg-svn/sys/pmax/dev/rz.c (revision 69799)
152130Smckusick /*
264089Sbostic  * Copyright (c) 1992, 1993
364089Sbostic  *	The Regents of the University of California.  All rights reserved.
452130Smckusick  *
552130Smckusick  * This code is derived from software contributed to Berkeley by
652130Smckusick  * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
752130Smckusick  *
852130Smckusick  * %sccs.include.redist.c%
952130Smckusick  *
10*69799Sralph  *	@(#)rz.c	8.4 (Berkeley) 06/02/95
1152130Smckusick  */
1252130Smckusick 
1352130Smckusick /*
1452130Smckusick  * SCSI CCS (Command Command Set) disk driver.
1553202Sralph  * NOTE: The name was changed from "sd" to "rz" for DEC naming compatibility.
1652130Smckusick  * I guess I can't avoid confusion someplace.
1752130Smckusick  */
1852130Smckusick #include "rz.h"
1952130Smckusick #if NRZ > 0
2052130Smckusick 
2156522Sbostic #include <sys/param.h>
2256522Sbostic #include <sys/systm.h>
2356522Sbostic #include <sys/buf.h>
2456522Sbostic #include <sys/errno.h>
2556522Sbostic #include <sys/fcntl.h>
2656522Sbostic #include <sys/ioctl.h>
2756522Sbostic #include <sys/dkstat.h>
2856522Sbostic #include <sys/disklabel.h>
2956522Sbostic #include <sys/malloc.h>
3056522Sbostic #include <sys/proc.h>
3156522Sbostic #include <sys/uio.h>
3256522Sbostic #include <sys/stat.h>
3356522Sbostic #include <sys/syslog.h>
3452130Smckusick 
3556525Sbostic #include <pmax/dev/device.h>
3656525Sbostic #include <pmax/dev/scsi.h>
3756522Sbostic 
3852130Smckusick extern int splbio();
3952130Smckusick extern void splx();
4052130Smckusick extern int physio();
4152130Smckusick 
4252130Smckusick int	rzprobe();
4352130Smckusick void	rzstrategy(), rzstart(), rzdone();
4452130Smckusick 
4552130Smckusick struct	driver rzdriver = {
4652130Smckusick 	"rz", rzprobe, rzstart, rzdone,
4752130Smckusick };
4852130Smckusick 
4952130Smckusick struct	size {
5052130Smckusick 	u_long	strtblk;
5153202Sralph 	u_long	nblocks;
5252130Smckusick };
5352130Smckusick 
5452130Smckusick /*
5552130Smckusick  * Since the SCSI standard tends to hide the disk structure, we define
5652130Smckusick  * partitions in terms of DEV_BSIZE blocks.  The default partition table
5753202Sralph  * (for an unlabeled disk) reserves 8K for a boot area, has an 8 meg
5852130Smckusick  * root and 32 meg of swap.  The rest of the space on the drive goes in
5952130Smckusick  * the G partition.  As usual, the C partition covers the entire disk
6052130Smckusick  * (including the boot area).
6152130Smckusick  */
6256629Sralph static struct size rzdefaultpart[MAXPARTITIONS] = {
6353202Sralph 	        0,   16384,	/* A */
6453202Sralph 	    16384,   65536,	/* B */
6553202Sralph 	        0,       0,	/* C */
6653202Sralph 	    17408,       0,	/* D */
6753202Sralph 	   115712,       0,	/* E */
6853202Sralph 	   218112,       0,	/* F */
6953202Sralph 	    81920,       0,	/* G */
7053202Sralph 	   115712,       0,	/* H */
7152130Smckusick };
7252130Smckusick 
7353202Sralph #define	RAWPART		2	/* 'c' partition */	/* XXX */
7453202Sralph 
7552130Smckusick struct rzstats {
7652130Smckusick 	long	rzresets;
7752130Smckusick 	long	rztransfers;
7852130Smckusick 	long	rzpartials;
7952130Smckusick };
8052130Smckusick 
8152130Smckusick struct	rz_softc {
8252130Smckusick 	struct	scsi_device *sc_sd;	/* physical unit info */
8353202Sralph 	pid_t	sc_format_pid;		/* process using "format" mode */
8453202Sralph 	u_long	sc_openpart;		/* partitions open */
8553202Sralph 	u_long	sc_bopenpart;		/* block partitions open */
8653202Sralph 	u_long	sc_copenpart;		/* character partitions open */
8752130Smckusick 	short	sc_flags;		/* see below */
8852130Smckusick 	short	sc_type;		/* drive type from INQUIRY cmd */
8952130Smckusick 	u_int	sc_blks;		/* number of blocks on device */
9052130Smckusick 	int	sc_blksize;		/* device block size in bytes */
9152130Smckusick 	int	sc_bshift;		/* convert device blocks to DEV_BSIZE */
9252130Smckusick 	u_int	sc_wpms;		/* average xfer rate in 16bit wds/sec */
9353202Sralph 	struct	disklabel sc_label;	/* disk label for this disk */
9452130Smckusick 	struct	rzstats sc_stats;	/* statisic counts */
9552130Smckusick 	struct	buf sc_tab;		/* queue of pending operations */
9652130Smckusick 	struct	buf sc_buf;		/* buf for doing I/O */
9752130Smckusick 	struct	buf sc_errbuf;		/* buf for doing REQUEST_SENSE */
9852130Smckusick 	struct	ScsiCmd sc_cmd;		/* command for controller */
9952130Smckusick 	ScsiGroup1Cmd sc_rwcmd;		/* SCSI cmd if not in "format" mode */
10052130Smckusick 	struct	scsi_fmt_cdb sc_cdb;	/* SCSI cmd if in "format" mode */
10152130Smckusick 	struct	scsi_fmt_sense sc_sense;	/* sense data from last cmd */
10268012Smckusick 	u_char	sc_capbuf[8];		/* buffer for SCSI_READ_CAPACITY */
10352130Smckusick } rz_softc[NRZ];
10452130Smckusick 
10552130Smckusick /* sc_flags values */
10668012Smckusick #define	RZF_ALIVE		0x0001	/* drive found and ready */
10768012Smckusick #define	RZF_SENSEINPROGRESS	0x0002	/* REQUEST_SENSE command in progress */
10868012Smckusick #define	RZF_ALTCMD		0x0004	/* alternate command in progress */
10968012Smckusick #define	RZF_HAVELABEL		0x0008	/* valid label found on disk */
11068012Smckusick #define	RZF_WLABEL		0x0010	/* label is writeable */
11168012Smckusick #define	RZF_WAIT		0x0020	/* waiting for sc_tab to drain */
11268012Smckusick #define	RZF_REMOVEABLE		0x0040	/* disk is removable */
11368012Smckusick #define	RZF_TRYSYNC		0x0080	/* try synchronous operation */
11468012Smckusick #define	RZF_NOERR		0x0100	/* don't print error messages */
11552130Smckusick 
11652130Smckusick #ifdef DEBUG
11752130Smckusick int	rzdebug = 3;
11852130Smckusick #define RZB_ERROR	0x01
11952130Smckusick #define RZB_PARTIAL	0x02
12052130Smckusick #define RZB_PRLABEL	0x04
12152130Smckusick #endif
12252130Smckusick 
12353202Sralph #define	rzunit(x)	(minor(x) >> 3)
12452130Smckusick #define rzpart(x)	(minor(x) & 0x7)
12552130Smckusick #define	b_cylin		b_resid
12652130Smckusick 
12752130Smckusick /*
12852130Smckusick  * Table of scsi commands users are allowed to access via "format" mode.
12952130Smckusick  *  0 means not legal.
13052130Smckusick  *  1 means legal.
13152130Smckusick  */
13252130Smckusick static char legal_cmds[256] = {
13352130Smckusick /*****  0   1   2   3   4   5   6   7     8   9   A   B   C   D   E   F */
13452130Smckusick /*00*/	0,  0,  0,  0,  1,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
13552130Smckusick /*10*/	0,  0,  1,  0,  0,  1,  0,  0,    0,  0,  1,  0,  0,  0,  0,  0,
13652130Smckusick /*20*/	0,  0,  0,  0,  0,  1,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
13752130Smckusick /*30*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
13852130Smckusick /*40*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
13952130Smckusick /*50*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14052130Smckusick /*60*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14152130Smckusick /*70*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14252130Smckusick /*80*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14352130Smckusick /*90*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14452130Smckusick /*a0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14552130Smckusick /*b0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14652130Smckusick /*c0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14752130Smckusick /*d0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14852130Smckusick /*e0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
14952130Smckusick /*f0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
15052130Smckusick };
15152130Smckusick 
15252130Smckusick /*
15368012Smckusick  * Test to see if the unit is ready and if not, try to make it ready.
15468012Smckusick  * Also, find the drive capacity.
15552130Smckusick  */
15668012Smckusick static int
rzready(sc)15768012Smckusick rzready(sc)
15868012Smckusick 	register struct rz_softc *sc;
15952130Smckusick {
16052130Smckusick 	register int tries, i;
16152130Smckusick 	ScsiClass7Sense *sp;
16252130Smckusick 
16368012Smckusick 	/* don't print SCSI errors */
164*69799Sralph 	sc->sc_flags |= RZF_NOERR | RZF_ALTCMD;
16552130Smckusick 
16668012Smckusick 	/* see if the device is ready */
16752130Smckusick 	for (tries = 10; ; ) {
16852130Smckusick 		sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
16968012Smckusick 		scsiGroup0Cmd(SCSI_TEST_UNIT_READY, sc->sc_rwcmd.unitNumber,
17068012Smckusick 			0, 0, (ScsiGroup0Cmd *)sc->sc_cdb.cdb);
17152130Smckusick 		sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
17252130Smckusick 		sc->sc_buf.b_bcount = 0;
17352130Smckusick 		sc->sc_buf.b_un.b_addr = (caddr_t)0;
17456629Sralph 		sc->sc_buf.b_actf = (struct buf *)0;
17556629Sralph 		sc->sc_tab.b_actf = &sc->sc_buf;
17652130Smckusick 
17752130Smckusick 		sc->sc_cmd.cmd = sc->sc_cdb.cdb;
17852130Smckusick 		sc->sc_cmd.cmdlen = sc->sc_cdb.len;
17952130Smckusick 		sc->sc_cmd.buf = (caddr_t)0;
18052130Smckusick 		sc->sc_cmd.buflen = 0;
18152130Smckusick 		/* setup synchronous data transfers if the device supports it */
18268012Smckusick 		if (tries == 10 && (sc->sc_flags & RZF_TRYSYNC))
18352130Smckusick 			sc->sc_cmd.flags = SCSICMD_USE_SYNC;
18452130Smckusick 		else
18552130Smckusick 			sc->sc_cmd.flags = 0;
18652130Smckusick 
18752130Smckusick 		(*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd);
18852130Smckusick 		if (!biowait(&sc->sc_buf))
18952130Smckusick 			break;
19052130Smckusick 		if (--tries < 0)
19168012Smckusick 			return (0);
19252130Smckusick 		if (!(sc->sc_sense.status & SCSI_STATUS_CHECKCOND))
19352130Smckusick 			goto again;
19452130Smckusick 		sp = (ScsiClass7Sense *)sc->sc_sense.sense;
19552130Smckusick 		if (sp->error7 != 0x70)
19652130Smckusick 			goto again;
19752130Smckusick 		if (sp->key == SCSI_CLASS7_UNIT_ATTN && tries != 9) {
19852130Smckusick 			/* drive recalibrating, give it a while */
19952130Smckusick 			DELAY(1000000);
20052130Smckusick 			continue;
20152130Smckusick 		}
20252130Smckusick 		if (sp->key == SCSI_CLASS7_NOT_READY) {
20352130Smckusick 			ScsiStartStopCmd *cp;
20452130Smckusick 
20552130Smckusick 			/* try to spin-up disk with start/stop command */
20652130Smckusick 			sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
20752130Smckusick 			cp = (ScsiStartStopCmd *)sc->sc_cdb.cdb;
20852130Smckusick 			cp->command = SCSI_START_STOP;
20968012Smckusick 			cp->unitNumber = sc->sc_rwcmd.unitNumber;
21052130Smckusick 			cp->immed = 0;
21152130Smckusick 			cp->loadEject = 0;
21252130Smckusick 			cp->start = 1;
21352130Smckusick 			cp->pad1 = 0;
21452130Smckusick 			cp->pad2 = 0;
21552130Smckusick 			cp->pad3 = 0;
21652130Smckusick 			cp->pad4 = 0;
21752130Smckusick 			cp->control = 0;
21852130Smckusick 			sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
21952130Smckusick 			sc->sc_buf.b_bcount = 0;
22052130Smckusick 			sc->sc_buf.b_un.b_addr = (caddr_t)0;
22156629Sralph 			sc->sc_buf.b_actf = (struct buf *)0;
22256629Sralph 			sc->sc_tab.b_actf = &sc->sc_buf;
22368012Smckusick 			rzstart(sc->sc_cmd.unit);
22452130Smckusick 			if (biowait(&sc->sc_buf))
22568012Smckusick 				return (0);
22652130Smckusick 			continue;
22752130Smckusick 		}
22852130Smckusick 	again:
22952130Smckusick 		DELAY(1000);
23052130Smckusick 	}
23152130Smckusick 
23268012Smckusick 	/* print SCSI errors */
233*69799Sralph 	sc->sc_flags &= ~(RZF_NOERR | RZF_ALTCMD);
23468012Smckusick 
23552130Smckusick 	/* find out how big a disk this is */
23652130Smckusick 	sc->sc_cdb.len = sizeof(ScsiGroup1Cmd);
23768012Smckusick 	scsiGroup1Cmd(SCSI_READ_CAPACITY, sc->sc_rwcmd.unitNumber, 0, 0,
23852130Smckusick 		(ScsiGroup1Cmd *)sc->sc_cdb.cdb);
23952130Smckusick 	sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
24068012Smckusick 	sc->sc_buf.b_bcount = sizeof(sc->sc_capbuf);
24168012Smckusick 	sc->sc_buf.b_un.b_addr = (caddr_t)sc->sc_capbuf;
24256629Sralph 	sc->sc_buf.b_actf = (struct buf *)0;
24356629Sralph 	sc->sc_tab.b_actf = &sc->sc_buf;
24468012Smckusick 	sc->sc_flags |= RZF_ALTCMD;
24568012Smckusick 	rzstart(sc->sc_cmd.unit);
24668012Smckusick 	sc->sc_flags &= ~RZF_ALTCMD;
24768012Smckusick 	if (biowait(&sc->sc_buf) || sc->sc_buf.b_resid != 0)
24868012Smckusick 		return (0);
24968012Smckusick 	sc->sc_blks = ((sc->sc_capbuf[0] << 24) | (sc->sc_capbuf[1] << 16) |
25068012Smckusick 		(sc->sc_capbuf[2] << 8) | sc->sc_capbuf[3]) + 1;
25168012Smckusick 	sc->sc_blksize = (sc->sc_capbuf[4] << 24) | (sc->sc_capbuf[5] << 16) |
25268012Smckusick 		(sc->sc_capbuf[6] << 8) | sc->sc_capbuf[7];
25368012Smckusick 
25468012Smckusick 	sc->sc_bshift = 0;
25568012Smckusick 	for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
25668012Smckusick 		++sc->sc_bshift;
25768012Smckusick 	sc->sc_blks <<= sc->sc_bshift;
25868012Smckusick 
25968012Smckusick 	return (1);
26068012Smckusick }
26168012Smckusick 
26268012Smckusick /*
26368012Smckusick  * Test to see if device is present.
26468012Smckusick  * Return true if found and initialized ok.
26568012Smckusick  */
rzprobe(sd)26668012Smckusick rzprobe(sd)
26768012Smckusick 	register struct scsi_device *sd;
26868012Smckusick {
26968012Smckusick 	register struct rz_softc *sc = &rz_softc[sd->sd_unit];
27068012Smckusick 	register int i;
27168012Smckusick 	ScsiInquiryData inqbuf;
27268012Smckusick 	ScsiClass7Sense *sp;
27368012Smckusick 
27468012Smckusick 	/* init some parameters that don't change */
27568012Smckusick 	sc->sc_sd = sd;
27668012Smckusick 	sc->sc_cmd.sd = sd;
27768012Smckusick 	sc->sc_cmd.unit = sd->sd_unit;
27868012Smckusick 	sc->sc_rwcmd.unitNumber = sd->sd_slave;
27968012Smckusick 
28068012Smckusick 	/* try to find out what type of device this is */
28168012Smckusick 	sc->sc_format_pid = 1;		/* force use of sc_cdb */
28268012Smckusick 	sc->sc_flags = RZF_NOERR;	/* don't print SCSI errors */
28368012Smckusick 	sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
28468012Smckusick 	scsiGroup0Cmd(SCSI_INQUIRY, sd->sd_slave, 0, sizeof(inqbuf),
28568012Smckusick 		(ScsiGroup0Cmd *)sc->sc_cdb.cdb);
28668012Smckusick 	sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
28768012Smckusick 	sc->sc_buf.b_bcount = sizeof(inqbuf);
28868012Smckusick 	sc->sc_buf.b_un.b_addr = (caddr_t)&inqbuf;
28968012Smckusick 	sc->sc_buf.b_actf = (struct buf *)0;
29068012Smckusick 	sc->sc_tab.b_actf = &sc->sc_buf;
29152130Smckusick 	rzstart(sd->sd_unit);
29268012Smckusick 	if (biowait(&sc->sc_buf) ||
29368012Smckusick 	    (i = sizeof(inqbuf) - sc->sc_buf.b_resid) < 5)
29452130Smckusick 		goto bad;
29568012Smckusick 	switch (inqbuf.type) {
29668012Smckusick 	case SCSI_DISK_TYPE:		/* disk */
29768012Smckusick 	case SCSI_WORM_TYPE:		/* WORM */
29868012Smckusick 	case SCSI_ROM_TYPE:		/* CD-ROM */
29968012Smckusick 	case SCSI_OPTICAL_MEM_TYPE:	/* Magneto-optical */
30068012Smckusick 		break;
30152130Smckusick 
30268012Smckusick 	default:			/* not a disk */
30368012Smckusick 		goto bad;
30468012Smckusick 	}
30568012Smckusick 	sc->sc_type = inqbuf.type;
30668012Smckusick 	if (inqbuf.flags & SCSI_SYNC)
30768012Smckusick 		sc->sc_flags |= RZF_TRYSYNC;
30868012Smckusick 
30968012Smckusick 	if (!inqbuf.rmb) {
31068012Smckusick 		if (!rzready(sc))
31168012Smckusick 			goto bad;
31268012Smckusick 	}
31368012Smckusick 
31452130Smckusick 	printf("rz%d at %s%d drive %d slave %d", sd->sd_unit,
31552130Smckusick 		sd->sd_cdriver->d_name, sd->sd_ctlr, sd->sd_drive,
31652130Smckusick 		sd->sd_slave);
31767478Smckusick 	if (inqbuf.version > 2 || i < 36)
31852130Smckusick 		printf(" type 0x%x, qual 0x%x, ver %d",
31952130Smckusick 			inqbuf.type, inqbuf.qualifier, inqbuf.version);
32052130Smckusick 	else {
32152130Smckusick 		char vid[9], pid[17], revl[5];
32252130Smckusick 
32352130Smckusick 		bcopy((caddr_t)inqbuf.vendorID, (caddr_t)vid, 8);
32452130Smckusick 		bcopy((caddr_t)inqbuf.productID, (caddr_t)pid, 16);
32552130Smckusick 		bcopy((caddr_t)inqbuf.revLevel, (caddr_t)revl, 4);
32652130Smckusick 		for (i = 8; --i > 0; )
32752130Smckusick 			if (vid[i] != ' ')
32852130Smckusick 				break;
32952130Smckusick 		vid[i+1] = 0;
33052130Smckusick 		for (i = 16; --i > 0; )
33152130Smckusick 			if (pid[i] != ' ')
33252130Smckusick 				break;
33352130Smckusick 		pid[i+1] = 0;
33452130Smckusick 		for (i = 4; --i > 0; )
33552130Smckusick 			if (revl[i] != ' ')
33652130Smckusick 				break;
33752130Smckusick 		revl[i+1] = 0;
33852130Smckusick 		printf(" %s %s rev %s", vid, pid, revl);
33952130Smckusick 	}
34052130Smckusick 	printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize);
34168012Smckusick 	if (!inqbuf.rmb && sc->sc_blksize != DEV_BSIZE) {
34252130Smckusick 		if (sc->sc_blksize < DEV_BSIZE) {
34352130Smckusick 			printf("rz%d: need %d byte blocks - drive ignored\n",
34452130Smckusick 				sd->sd_unit, DEV_BSIZE);
34552130Smckusick 			goto bad;
34652130Smckusick 		}
34752130Smckusick 	}
34852130Smckusick 	sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
34952130Smckusick 	sc->sc_format_pid = 0;
35068012Smckusick 	sc->sc_flags |= RZF_ALIVE;
35168012Smckusick 	if (inqbuf.rmb)
35268012Smckusick 		sc->sc_flags |= RZF_REMOVEABLE;
35352130Smckusick 	sc->sc_buf.b_flags = 0;
35452130Smckusick 	return (1);
35552130Smckusick 
35652130Smckusick bad:
35752130Smckusick 	/* doesn't exist or not a CCS device */
35852130Smckusick 	sc->sc_format_pid = 0;
35952130Smckusick 	sc->sc_buf.b_flags = 0;
36052130Smckusick 	return (0);
36152130Smckusick }
36252130Smckusick 
36352130Smckusick /*
36452130Smckusick  * This routine is called for partial block transfers and non-aligned
36552130Smckusick  * transfers (the latter only being possible on devices with a block size
36652130Smckusick  * larger than DEV_BSIZE).  The operation is performed in three steps
36752130Smckusick  * using a locally allocated buffer:
36852130Smckusick  *	1. transfer any initial partial block
36952130Smckusick  *	2. transfer full blocks
37052130Smckusick  *	3. transfer any final partial block
37152130Smckusick  */
37252130Smckusick static void
rzlblkstrat(bp,bsize)37352130Smckusick rzlblkstrat(bp, bsize)
37452130Smckusick 	register struct buf *bp;
37552130Smckusick 	register int bsize;
37652130Smckusick {
37752130Smckusick 	register struct buf *cbp;
37852130Smckusick 	caddr_t cbuf;
37952130Smckusick 	register int bn, resid;
38052130Smckusick 	register caddr_t addr;
38152130Smckusick 
38252130Smckusick 	cbp = (struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK);
38352130Smckusick 	cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
38452130Smckusick 	bzero((caddr_t)cbp, sizeof(*cbp));
38552130Smckusick 	cbp->b_proc = curproc;
38652130Smckusick 	cbp->b_dev = bp->b_dev;
38752130Smckusick 	bn = bp->b_blkno;
38852130Smckusick 	resid = bp->b_bcount;
38952130Smckusick 	addr = bp->b_un.b_addr;
39052130Smckusick #ifdef DEBUG
39152130Smckusick 	if (rzdebug & RZB_PARTIAL)
39252130Smckusick 		printf("rzlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
39352130Smckusick 		       bp, bp->b_flags, bn, resid, addr);
39452130Smckusick #endif
39552130Smckusick 
39652130Smckusick 	while (resid > 0) {
39752130Smckusick 		register int boff = dbtob(bn) & (bsize - 1);
39852130Smckusick 		register int count;
39952130Smckusick 
40052130Smckusick 		if (boff || resid < bsize) {
40152130Smckusick 			rz_softc[rzunit(bp->b_dev)].sc_stats.rzpartials++;
40255746Sralph 			count = min(resid, bsize - boff);
40352130Smckusick 			cbp->b_flags = B_BUSY | B_PHYS | B_READ;
40452130Smckusick 			cbp->b_blkno = bn - btodb(boff);
40552130Smckusick 			cbp->b_un.b_addr = cbuf;
40652130Smckusick 			cbp->b_bcount = bsize;
40752130Smckusick #ifdef DEBUG
40852130Smckusick 			if (rzdebug & RZB_PARTIAL)
40952130Smckusick 				printf(" readahead: bn %x cnt %x off %x addr %x\n",
41052130Smckusick 				       cbp->b_blkno, count, boff, addr);
41152130Smckusick #endif
41252130Smckusick 			rzstrategy(cbp);
41352130Smckusick 			biowait(cbp);
41452130Smckusick 			if (cbp->b_flags & B_ERROR) {
41552130Smckusick 				bp->b_flags |= B_ERROR;
41652130Smckusick 				bp->b_error = cbp->b_error;
41752130Smckusick 				break;
41852130Smckusick 			}
41952130Smckusick 			if (bp->b_flags & B_READ) {
42052130Smckusick 				bcopy(&cbuf[boff], addr, count);
42152130Smckusick 				goto done;
42252130Smckusick 			}
42352130Smckusick 			bcopy(addr, &cbuf[boff], count);
42452130Smckusick #ifdef DEBUG
42552130Smckusick 			if (rzdebug & RZB_PARTIAL)
42652130Smckusick 				printf(" writeback: bn %x cnt %x off %x addr %x\n",
42752130Smckusick 				       cbp->b_blkno, count, boff, addr);
42852130Smckusick #endif
42952130Smckusick 		} else {
43052130Smckusick 			count = resid & ~(bsize - 1);
43152130Smckusick 			cbp->b_blkno = bn;
43252130Smckusick 			cbp->b_un.b_addr = addr;
43352130Smckusick 			cbp->b_bcount = count;
43452130Smckusick #ifdef DEBUG
43552130Smckusick 			if (rzdebug & RZB_PARTIAL)
43652130Smckusick 				printf(" fulltrans: bn %x cnt %x addr %x\n",
43752130Smckusick 				       cbp->b_blkno, count, addr);
43852130Smckusick #endif
43952130Smckusick 		}
44052130Smckusick 		cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
44152130Smckusick 		rzstrategy(cbp);
44252130Smckusick 		biowait(cbp);
44352130Smckusick 		if (cbp->b_flags & B_ERROR) {
44452130Smckusick 			bp->b_flags |= B_ERROR;
44552130Smckusick 			bp->b_error = cbp->b_error;
44652130Smckusick 			break;
44752130Smckusick 		}
44852130Smckusick done:
44952130Smckusick 		bn += btodb(count);
45052130Smckusick 		resid -= count;
45152130Smckusick 		addr += count;
45252130Smckusick #ifdef DEBUG
45352130Smckusick 		if (rzdebug & RZB_PARTIAL)
45452130Smckusick 			printf(" done: bn %x resid %x addr %x\n",
45552130Smckusick 			       bn, resid, addr);
45652130Smckusick #endif
45752130Smckusick 	}
45852130Smckusick 	free(cbuf, M_DEVBUF);
45952130Smckusick 	free(cbp, M_DEVBUF);
46052130Smckusick }
46152130Smckusick 
46252130Smckusick void
rzstrategy(bp)46352130Smckusick rzstrategy(bp)
46452130Smckusick 	register struct buf *bp;
46552130Smckusick {
46652130Smckusick 	register int unit = rzunit(bp->b_dev);
46752130Smckusick 	register int part = rzpart(bp->b_dev);
46852130Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
46953202Sralph 	register struct partition *pp = &sc->sc_label.d_partitions[part];
47056629Sralph 	register daddr_t bn;
47156629Sralph 	register long sz, s;
47252130Smckusick 
47352130Smckusick 	if (sc->sc_format_pid) {
47452130Smckusick 		if (sc->sc_format_pid != curproc->p_pid) {
47552130Smckusick 			bp->b_error = EPERM;
47652130Smckusick 			goto bad;
47752130Smckusick 		}
47852130Smckusick 		bp->b_cylin = 0;
47952130Smckusick 	} else {
48052130Smckusick 		bn = bp->b_blkno;
48156629Sralph 		sz = howmany(bp->b_bcount, DEV_BSIZE);
48256629Sralph 		if ((unsigned)bn + sz > pp->p_size) {
48356629Sralph 			sz = pp->p_size - bn;
48453202Sralph 			/* if exactly at end of disk, return an EOF */
48556629Sralph 			if (sz == 0) {
48652130Smckusick 				bp->b_resid = bp->b_bcount;
48752130Smckusick 				goto done;
48852130Smckusick 			}
48953202Sralph 			/* if none of it fits, error */
49056629Sralph 			if (sz < 0) {
49153202Sralph 				bp->b_error = EINVAL;
49253202Sralph 				goto bad;
49353202Sralph 			}
49453202Sralph 			/* otherwise, truncate */
49556629Sralph 			bp->b_bcount = dbtob(sz);
49653202Sralph 		}
49753202Sralph 		/* check for write to write protected label */
49853202Sralph 		if (bn + pp->p_offset <= LABELSECTOR &&
49953202Sralph #if LABELSECTOR != 0
50053202Sralph 		    bn + pp->p_offset + sz > LABELSECTOR &&
50153202Sralph #endif
50253202Sralph 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RZF_WLABEL)) {
50353202Sralph 			bp->b_error = EROFS;
50452130Smckusick 			goto bad;
50552130Smckusick 		}
50652130Smckusick 		/*
50752130Smckusick 		 * Non-aligned or partial-block transfers handled specially.
50852130Smckusick 		 */
50952130Smckusick 		s = sc->sc_blksize - 1;
51052130Smckusick 		if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
51152130Smckusick 			rzlblkstrat(bp, sc->sc_blksize);
51252130Smckusick 			goto done;
51352130Smckusick 		}
51453202Sralph 		bp->b_cylin = (bn + pp->p_offset) >> sc->sc_bshift;
51552130Smckusick 	}
51652130Smckusick 	/* don't let disksort() see sc_errbuf */
51752130Smckusick 	while (sc->sc_flags & RZF_SENSEINPROGRESS)
51852130Smckusick 		printf("SENSE\n"); /* XXX */
51952130Smckusick 	s = splbio();
52052130Smckusick 	disksort(&sc->sc_tab, bp);
52152130Smckusick 	if (sc->sc_tab.b_active == 0) {
52252130Smckusick 		sc->sc_tab.b_active = 1;
52352130Smckusick 		rzstart(unit);
52452130Smckusick 	}
52552130Smckusick 	splx(s);
52652130Smckusick 	return;
52752130Smckusick bad:
52852130Smckusick 	bp->b_flags |= B_ERROR;
52952130Smckusick done:
53052130Smckusick 	biodone(bp);
53152130Smckusick }
53252130Smckusick 
53352130Smckusick void
rzstart(unit)53452130Smckusick rzstart(unit)
53552130Smckusick 	int unit;
53652130Smckusick {
53752130Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
53852130Smckusick 	register struct buf *bp = sc->sc_tab.b_actf;
53952130Smckusick 	register int n;
54052130Smckusick 
54152130Smckusick 	sc->sc_cmd.buf = bp->b_un.b_addr;
54252130Smckusick 	sc->sc_cmd.buflen = bp->b_bcount;
54352130Smckusick 
54468012Smckusick 	if (sc->sc_format_pid ||
54568012Smckusick 	    (sc->sc_flags & (RZF_SENSEINPROGRESS | RZF_ALTCMD))) {
54652130Smckusick 		sc->sc_cmd.flags = !(bp->b_flags & B_READ) ?
54752130Smckusick 			SCSICMD_DATA_TO_DEVICE : 0;
54852130Smckusick 		sc->sc_cmd.cmd = sc->sc_cdb.cdb;
54952130Smckusick 		sc->sc_cmd.cmdlen = sc->sc_cdb.len;
55052130Smckusick 	} else {
55152130Smckusick 		if (bp->b_flags & B_READ) {
55252130Smckusick 			sc->sc_cmd.flags = 0;
55352130Smckusick 			sc->sc_rwcmd.command = SCSI_READ_EXT;
55452130Smckusick 		} else {
55552130Smckusick 			sc->sc_cmd.flags = SCSICMD_DATA_TO_DEVICE;
55652130Smckusick 			sc->sc_rwcmd.command = SCSI_WRITE_EXT;
55752130Smckusick 		}
55852130Smckusick 		sc->sc_cmd.cmd = (u_char *)&sc->sc_rwcmd;
55952130Smckusick 		sc->sc_cmd.cmdlen = sizeof(sc->sc_rwcmd);
56052130Smckusick 		n = bp->b_cylin;
56152130Smckusick 		sc->sc_rwcmd.highAddr = n >> 24;
56252130Smckusick 		sc->sc_rwcmd.midHighAddr = n >> 16;
56352130Smckusick 		sc->sc_rwcmd.midLowAddr = n >> 8;
56452130Smckusick 		sc->sc_rwcmd.lowAddr = n;
56552130Smckusick 		n = howmany(bp->b_bcount, sc->sc_blksize);
56652130Smckusick 		sc->sc_rwcmd.highBlockCount = n >> 8;
56752130Smckusick 		sc->sc_rwcmd.lowBlockCount = n;
56852130Smckusick #ifdef DEBUG
56952130Smckusick 		if ((bp->b_bcount & (sc->sc_blksize - 1)) != 0)
57052130Smckusick 			printf("rz%d: partial block xfer -- %x bytes\n",
57152130Smckusick 				unit, bp->b_bcount);
57252130Smckusick #endif
57352130Smckusick 		sc->sc_stats.rztransfers++;
57452130Smckusick 		if ((n = sc->sc_sd->sd_dk) >= 0) {
57552130Smckusick 			dk_busy |= 1 << n;
57652130Smckusick 			++dk_seek[n];
57752130Smckusick 			++dk_xfer[n];
57852130Smckusick 			dk_wds[n] += bp->b_bcount >> 6;
57952130Smckusick 		}
58052130Smckusick 	}
58152130Smckusick 
58252130Smckusick 	/* tell controller to start this command */
58352130Smckusick 	(*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd);
58452130Smckusick }
58552130Smckusick 
58652130Smckusick /*
58752130Smckusick  * This is called by the controller driver when the command is done.
58852130Smckusick  */
58952130Smckusick void
rzdone(unit,error,resid,status)59052130Smckusick rzdone(unit, error, resid, status)
59152130Smckusick 	register int unit;
59252130Smckusick 	int error;		/* error number from errno.h */
59352130Smckusick 	int resid;		/* amount not transfered */
59452130Smckusick 	int status;		/* SCSI status byte */
59552130Smckusick {
59652130Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
59752130Smckusick 	register struct buf *bp = sc->sc_tab.b_actf;
59852130Smckusick 	register struct scsi_device *sd = sc->sc_sd;
59952130Smckusick 
60052130Smckusick 	if (bp == NULL) {
60152130Smckusick 		printf("rz%d: bp == NULL\n", unit);
60252130Smckusick 		return;
60352130Smckusick 	}
60452130Smckusick 	if (sd->sd_dk >= 0)
60552130Smckusick 		dk_busy &= ~(1 << sd->sd_dk);
60652130Smckusick 	if (sc->sc_flags & RZF_SENSEINPROGRESS) {
60752130Smckusick 		sc->sc_flags &= ~RZF_SENSEINPROGRESS;
60856629Sralph 		sc->sc_tab.b_actf = bp = bp->b_actf;	/* remove sc_errbuf */
60952130Smckusick 
61052130Smckusick 		if (error || (status & SCSI_STATUS_CHECKCOND)) {
61152130Smckusick #ifdef DEBUG
61252130Smckusick 			if (rzdebug & RZB_ERROR)
61352130Smckusick 				printf("rz%d: error reading sense data: error %d scsi status 0x%x\n",
61452130Smckusick 					unit, error, status);
61552130Smckusick #endif
61652130Smckusick 			/*
61752130Smckusick 			 * We got an error during the REQUEST_SENSE,
61852130Smckusick 			 * fill in no sense for data.
61952130Smckusick 			 */
62052130Smckusick 			sc->sc_sense.sense[0] = 0x70;
62152130Smckusick 			sc->sc_sense.sense[2] = SCSI_CLASS7_NO_SENSE;
62268012Smckusick 		} else if (!(sc->sc_flags & RZF_NOERR)) {
62352130Smckusick 			printf("rz%d: ", unit);
62452130Smckusick 			scsiPrintSense((ScsiClass7Sense *)sc->sc_sense.sense,
62552130Smckusick 				sizeof(sc->sc_sense.sense) - resid);
62652130Smckusick 		}
62752130Smckusick 	} else if (error || (status & SCSI_STATUS_CHECKCOND)) {
62852130Smckusick #ifdef DEBUG
62968012Smckusick 		if (!(sc->sc_flags & RZF_NOERR) && (rzdebug & RZB_ERROR))
63052130Smckusick 			printf("rz%d: error %d scsi status 0x%x\n",
63152130Smckusick 				unit, error, status);
63252130Smckusick #endif
63352130Smckusick 		/* save error info */
63452130Smckusick 		sc->sc_sense.status = status;
63552130Smckusick 		bp->b_flags |= B_ERROR;
63652130Smckusick 		bp->b_error = error;
63752130Smckusick 		bp->b_resid = resid;
63852130Smckusick 
63952130Smckusick 		if (status & SCSI_STATUS_CHECKCOND) {
64052130Smckusick 			/*
64152130Smckusick 			 * Start a REQUEST_SENSE command.
64252130Smckusick 			 * Since we are called at interrupt time, we can't
64352130Smckusick 			 * wait for the command to finish; that's why we use
64452130Smckusick 			 * the sc_flags field.
64552130Smckusick 			 */
64652130Smckusick 			sc->sc_flags |= RZF_SENSEINPROGRESS;
64752130Smckusick 			sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
64852130Smckusick 			scsiGroup0Cmd(SCSI_REQUEST_SENSE, sd->sd_slave, 0,
64952130Smckusick 				sizeof(sc->sc_sense.sense),
65052130Smckusick 				(ScsiGroup0Cmd *)sc->sc_cdb.cdb);
65152130Smckusick 			sc->sc_errbuf.b_flags = B_BUSY | B_PHYS | B_READ;
65252130Smckusick 			sc->sc_errbuf.b_bcount = sizeof(sc->sc_sense.sense);
65352130Smckusick 			sc->sc_errbuf.b_un.b_addr = (caddr_t)sc->sc_sense.sense;
65456629Sralph 			sc->sc_errbuf.b_actf = bp;
65552130Smckusick 			sc->sc_tab.b_actf = &sc->sc_errbuf;
65652130Smckusick 			rzstart(unit);
65752130Smckusick 			return;
65852130Smckusick 		}
65952130Smckusick 	} else {
66052130Smckusick 		sc->sc_sense.status = status;
66152130Smckusick 		bp->b_resid = resid;
66252130Smckusick 	}
66352130Smckusick 
66456629Sralph 	sc->sc_tab.b_actf = bp->b_actf;
66552130Smckusick 	biodone(bp);
66652130Smckusick 	if (sc->sc_tab.b_actf)
66752130Smckusick 		rzstart(unit);
66853202Sralph 	else {
66952130Smckusick 		sc->sc_tab.b_active = 0;
67053202Sralph 		/* finish close protocol */
67153202Sralph 		if (sc->sc_openpart == 0)
67253202Sralph 			wakeup((caddr_t)&sc->sc_tab);
67353202Sralph 	}
67452130Smckusick }
67552130Smckusick 
67664088Smckusick /*
67764088Smckusick  * Read or constuct a disklabel
67864088Smckusick  */
67964088Smckusick void
rzgetinfo(dev)68064088Smckusick rzgetinfo(dev)
68164088Smckusick 	dev_t dev;
68264088Smckusick {
68364088Smckusick 	register int unit = rzunit(dev);
68464088Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
68564088Smckusick 	register struct disklabel *lp = &sc->sc_label;
68664088Smckusick 	register int i;
68764088Smckusick 	char *msg;
68864088Smckusick 	int part;
68964088Smckusick 	extern char *readdisklabel();
69064088Smckusick 
69164088Smckusick 	part = rzpart(dev);
69264088Smckusick 	sc->sc_flags |= RZF_HAVELABEL;
69364088Smckusick 
69468012Smckusick 	if (sc->sc_type == SCSI_ROM_TYPE) {
69568012Smckusick 		lp->d_type = DTYPE_SCSI;
69668012Smckusick 		lp->d_secsize = sc->sc_blksize;
69768012Smckusick 		lp->d_nsectors = 100;
69868012Smckusick 		lp->d_ntracks = 1;
69968012Smckusick 		lp->d_ncylinders = (sc->sc_blks / 100) + 1;
70068012Smckusick 		lp->d_secpercyl	= 100;
70168012Smckusick 		lp->d_secperunit = sc->sc_blks;
70268012Smckusick 		lp->d_rpm = 300;
70368012Smckusick 		lp->d_interleave = 1;
70468012Smckusick 		lp->d_flags = D_REMOVABLE;
70568012Smckusick 		lp->d_npartitions = 1;
70668012Smckusick 		lp->d_partitions[0].p_offset = 0;
70768012Smckusick 		lp->d_partitions[0].p_size = sc->sc_blks;
70868012Smckusick 		lp->d_partitions[0].p_fstype = FS_ISO9660;
70968012Smckusick 		lp->d_magic = DISKMAGIC;
71068012Smckusick 		lp->d_magic2 = DISKMAGIC;
71168012Smckusick 		lp->d_checksum = dkcksum(lp);
71268012Smckusick 		return;
71368012Smckusick 	}
71468012Smckusick 
71564088Smckusick 	lp->d_type = DTYPE_SCSI;
71664088Smckusick 	lp->d_secsize = DEV_BSIZE;
71764088Smckusick 	lp->d_secpercyl = 1 << sc->sc_bshift;
71864088Smckusick 	lp->d_npartitions = MAXPARTITIONS;
71964088Smckusick 	lp->d_partitions[part].p_offset = 0;
72064088Smckusick 	lp->d_partitions[part].p_size = sc->sc_blks;
72164088Smckusick 
72264088Smckusick 	/*
72364088Smckusick 	 * Now try to read the disklabel
72464088Smckusick 	 */
72564088Smckusick 	msg = readdisklabel(dev, rzstrategy, lp);
72664088Smckusick 	if (msg == NULL)
72764088Smckusick 		return;
72864088Smckusick 
72964088Smckusick 	printf("rz%d: WARNING: %s\n", unit, msg);
73068012Smckusick 	lp->d_magic = DISKMAGIC;
73168012Smckusick 	lp->d_magic2 = DISKMAGIC;
73268012Smckusick 	lp->d_type = DTYPE_SCSI;
73368012Smckusick 	lp->d_subtype = 0;
73468012Smckusick 	lp->d_typename[0] = '\0';
73568012Smckusick 	lp->d_secsize = DEV_BSIZE;
73668012Smckusick 	lp->d_secperunit = sc->sc_blks;
73768012Smckusick 	lp->d_npartitions = MAXPARTITIONS;
73864088Smckusick 	for (i = 0; i < MAXPARTITIONS; i++) {
73968012Smckusick 		lp->d_partitions[i].p_size = rzdefaultpart[i].nblocks;
74068012Smckusick 		lp->d_partitions[i].p_offset = rzdefaultpart[i].strtblk;
74164088Smckusick 	}
74268012Smckusick 	lp->d_partitions[RAWPART].p_size = sc->sc_blks;
74364088Smckusick }
74464088Smckusick 
74552130Smckusick int
rzopen(dev,flags,mode,p)74652130Smckusick rzopen(dev, flags, mode, p)
74752130Smckusick 	dev_t dev;
74852130Smckusick 	int flags, mode;
74952130Smckusick 	struct proc *p;
75052130Smckusick {
75152130Smckusick 	register int unit = rzunit(dev);
75252130Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
75353202Sralph 	register struct disklabel *lp;
75453202Sralph 	register int i;
75553202Sralph 	int part;
75653202Sralph 	u_long mask;
75752130Smckusick 
75853202Sralph 	if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE))
75952130Smckusick 		return (ENXIO);
76053202Sralph 
76168012Smckusick 	/* make sure disk is ready */
76268012Smckusick 	if (sc->sc_flags & RZF_REMOVEABLE) {
76368012Smckusick 		if (!rzready(sc))
76468012Smckusick 			return (ENXIO);
76568012Smckusick 	}
76668012Smckusick 
76753202Sralph 	/* try to read disk label and partition table information */
76853202Sralph 	part = rzpart(dev);
76964088Smckusick 	if (!(sc->sc_flags & RZF_HAVELABEL))
77064088Smckusick 		rzgetinfo(dev);
77164088Smckusick 
77253202Sralph 	lp = &sc->sc_label;
77353202Sralph 	if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0)
77452130Smckusick 		return (ENXIO);
77553202Sralph 	/*
77653202Sralph 	 * Warn if a partition is opened that overlaps another
77753202Sralph 	 * already open, unless either is the `raw' partition
77853202Sralph 	 * (whole disk).
77953202Sralph 	 */
78053202Sralph 	mask = 1 << part;
78153202Sralph 	if ((sc->sc_openpart & mask) == 0 && part != RAWPART) {
78253202Sralph 		register struct partition *pp;
78353202Sralph 		u_long start, end;
78452130Smckusick 
78553202Sralph 		pp = &lp->d_partitions[part];
78653202Sralph 		start = pp->p_offset;
78753202Sralph 		end = pp->p_offset + pp->p_size;
78853202Sralph 		for (pp = lp->d_partitions, i = 0;
78953202Sralph 		     i < lp->d_npartitions; pp++, i++) {
79053202Sralph 			if (pp->p_offset + pp->p_size <= start ||
79153202Sralph 			    pp->p_offset >= end || i == RAWPART)
79253202Sralph 				continue;
79353202Sralph 			if (sc->sc_openpart & (1 << i))
79453202Sralph 				log(LOG_WARNING,
79553202Sralph 				    "rz%d%c: overlaps open partition (%c)\n",
79653202Sralph 				    unit, part + 'a', i + 'a');
79753202Sralph 		}
79853202Sralph 	}
79953202Sralph 	switch (mode) {
80053202Sralph 	case S_IFCHR:
80153202Sralph 		sc->sc_copenpart |= mask;
80253202Sralph 		break;
80353202Sralph 	case S_IFBLK:
80453202Sralph 		sc->sc_bopenpart |= mask;
80553202Sralph 		break;
80653202Sralph 	}
80753202Sralph 	sc->sc_openpart |= mask;
80852130Smckusick 	if (sc->sc_sd->sd_dk >= 0)
80952130Smckusick 		dk_wpms[sc->sc_sd->sd_dk] = sc->sc_wpms;
81052130Smckusick 	return (0);
81152130Smckusick }
81252130Smckusick 
rzclose(dev,flags,mode)81353202Sralph rzclose(dev, flags, mode)
81452130Smckusick 	dev_t dev;
81553202Sralph 	int flags, mode;
81652130Smckusick {
81753202Sralph 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
81853202Sralph 	u_long mask = (1 << rzpart(dev));
81953202Sralph 	int s;
82053202Sralph 
82153202Sralph 	switch (mode) {
82253202Sralph 	case S_IFCHR:
82353202Sralph 		sc->sc_copenpart &= ~mask;
82453202Sralph 		break;
82553202Sralph 	case S_IFBLK:
82653202Sralph 		sc->sc_bopenpart &= ~mask;
82753202Sralph 		break;
82853202Sralph 	}
82953202Sralph 	sc->sc_openpart = sc->sc_copenpart | sc->sc_bopenpart;
83053202Sralph 
83153202Sralph 	/*
83253202Sralph 	 * Should wait for I/O to complete on this partition even if
83353202Sralph 	 * others are open, but wait for work on blkflush().
83453202Sralph 	 */
83553202Sralph 	if (sc->sc_openpart == 0) {
83653202Sralph 		s = splbio();
83753202Sralph 		while (sc->sc_tab.b_actf)
83853202Sralph 			sleep((caddr_t)&sc->sc_tab, PZERO - 1);
83953202Sralph 		splx(s);
84053202Sralph 		sc->sc_flags &= ~RZF_WLABEL;
84153202Sralph 	}
84252130Smckusick 	return (0);
84352130Smckusick }
84452130Smckusick 
84552130Smckusick int
rzread(dev,uio)84652130Smckusick rzread(dev, uio)
84752130Smckusick 	dev_t dev;
84852130Smckusick 	struct uio *uio;
84952130Smckusick {
85052130Smckusick 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
85152130Smckusick 
85252130Smckusick 	if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid)
85352130Smckusick 		return (EPERM);
85452130Smckusick 
85552130Smckusick 	return (physio(rzstrategy, (struct buf *)0, dev,
85652130Smckusick 		B_READ, minphys, uio));
85752130Smckusick }
85852130Smckusick 
85952130Smckusick int
rzwrite(dev,uio)86052130Smckusick rzwrite(dev, uio)
86152130Smckusick 	dev_t dev;
86252130Smckusick 	struct uio *uio;
86352130Smckusick {
86452130Smckusick 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
86552130Smckusick 
86668012Smckusick 	if (sc->sc_type == SCSI_ROM_TYPE)
86768012Smckusick 		return (EROFS);
86868012Smckusick 
86952130Smckusick 	if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid)
87052130Smckusick 		return (EPERM);
87152130Smckusick 
87252130Smckusick 	return (physio(rzstrategy, (struct buf *)0, dev,
87352130Smckusick 		B_WRITE, minphys, uio));
87452130Smckusick }
87552130Smckusick 
87652130Smckusick int
rzioctl(dev,cmd,data,flag,p)87752130Smckusick rzioctl(dev, cmd, data, flag, p)
87852130Smckusick 	dev_t dev;
879*69799Sralph 	u_long cmd;
88052130Smckusick 	caddr_t data;
88152130Smckusick 	int flag;
88252130Smckusick 	struct proc *p;
88352130Smckusick {
88452130Smckusick 	register struct rz_softc *sc = &rz_softc[rzunit(dev)];
88553202Sralph 	int error;
88653202Sralph 	int flags;
88752130Smckusick 
88852130Smckusick 	switch (cmd) {
88952130Smckusick 	default:
89052130Smckusick 		return (EINVAL);
89152130Smckusick 
89252130Smckusick 	case SDIOCSFORMAT:
89352130Smckusick 		/* take this device into or out of "format" mode */
89452130Smckusick 		if (suser(p->p_ucred, &p->p_acflag))
89552130Smckusick 			return (EPERM);
89652130Smckusick 
89752130Smckusick 		if (*(int *)data) {
89852130Smckusick 			if (sc->sc_format_pid)
89952130Smckusick 				return (EPERM);
90052130Smckusick 			sc->sc_format_pid = p->p_pid;
90152130Smckusick 		} else
90252130Smckusick 			sc->sc_format_pid = 0;
90352130Smckusick 		return (0);
90452130Smckusick 
90552130Smckusick 	case SDIOCGFORMAT:
90652130Smckusick 		/* find out who has the device in format mode */
90752130Smckusick 		*(int *)data = sc->sc_format_pid;
90852130Smckusick 		return (0);
90952130Smckusick 
91052130Smckusick 	case SDIOCSCSICOMMAND:
91152130Smckusick 		/*
91252130Smckusick 		 * Save what user gave us as SCSI cdb to use with next
91352130Smckusick 		 * read or write to the char device.
91452130Smckusick 		 */
91552130Smckusick 		if (sc->sc_format_pid != p->p_pid)
91652130Smckusick 			return (EPERM);
91752130Smckusick 		if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
91852130Smckusick 			return (EINVAL);
91952130Smckusick 		bcopy(data, (caddr_t)&sc->sc_cdb, sizeof(sc->sc_cdb));
92052130Smckusick 		return (0);
92152130Smckusick 
92252130Smckusick 	case SDIOCSENSE:
92352130Smckusick 		/*
92452130Smckusick 		 * return the SCSI sense data saved after the last
92552130Smckusick 		 * operation that completed with "check condition" status.
92652130Smckusick 		 */
92752130Smckusick 		bcopy((caddr_t)&sc->sc_sense, data, sizeof(sc->sc_sense));
92852130Smckusick 		return (0);
92952130Smckusick 
93053202Sralph 	case DIOCGDINFO:
93153202Sralph 		/* get the current disk label */
93253202Sralph 		*(struct disklabel *)data = sc->sc_label;
93353202Sralph 		return (0);
93453202Sralph 
93553202Sralph 	case DIOCSDINFO:
93653202Sralph 		/* set the current disk label */
93753202Sralph 		if (!(flag & FWRITE))
93853202Sralph 			return (EBADF);
93956629Sralph 		error = setdisklabel(&sc->sc_label,
94053202Sralph 			(struct disklabel *)data,
94156629Sralph 			(sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart);
94256629Sralph 		return (error);
94353202Sralph 
94453202Sralph 	case DIOCGPART:
94553202Sralph 		/* return the disk partition data */
94653202Sralph 		((struct partinfo *)data)->disklab = &sc->sc_label;
94753202Sralph 		((struct partinfo *)data)->part =
94853202Sralph 			&sc->sc_label.d_partitions[rzpart(dev)];
94953202Sralph 		return (0);
95053202Sralph 
95153202Sralph 	case DIOCWLABEL:
95253202Sralph 		if (!(flag & FWRITE))
95353202Sralph 			return (EBADF);
95453202Sralph 		if (*(int *)data)
95553202Sralph 			sc->sc_flags |= RZF_WLABEL;
95653202Sralph 		else
95753202Sralph 			sc->sc_flags &= ~RZF_WLABEL;
95853202Sralph 		return (0);
95953202Sralph 
96053202Sralph 	case DIOCWDINFO:
96153202Sralph 		/* write the disk label to disk */
96253202Sralph 		if (!(flag & FWRITE))
96353202Sralph 			return (EBADF);
96453202Sralph 		error = setdisklabel(&sc->sc_label,
96553202Sralph 			(struct disklabel *)data,
96653202Sralph 			(sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart);
96753202Sralph 		if (error)
96853202Sralph 			return (error);
96953202Sralph 
97053202Sralph 		/* simulate opening partition 0 so write succeeds */
97153202Sralph 		flags = sc->sc_flags;
97253202Sralph 		sc->sc_flags = RZF_ALIVE | RZF_WLABEL;
97353202Sralph 		error = writedisklabel(dev, rzstrategy, &sc->sc_label);
97453202Sralph 		sc->sc_flags = flags;
97553202Sralph 		return (error);
97652130Smckusick 	}
97752130Smckusick 	/*NOTREACHED*/
97852130Smckusick }
97952130Smckusick 
98052130Smckusick int
rzsize(dev)98152130Smckusick rzsize(dev)
98252130Smckusick 	dev_t dev;
98352130Smckusick {
98452130Smckusick 	register int unit = rzunit(dev);
98553202Sralph 	register int part = rzpart(dev);
98652130Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
98752130Smckusick 
98864088Smckusick 	if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE))
98952130Smckusick 		return (-1);
99052130Smckusick 
99164088Smckusick 	/*
99264088Smckusick 	 * We get called very early on (via swapconf)
99364088Smckusick 	 * without the device being open so we need to
99464088Smckusick 	 * read the disklabel here.
99564088Smckusick 	 */
99664088Smckusick 	if (!(sc->sc_flags & RZF_HAVELABEL))
99764088Smckusick 		rzgetinfo(dev);
99864088Smckusick 
99964088Smckusick 	if (part >= sc->sc_label.d_npartitions)
100064088Smckusick 		return (-1);
100153202Sralph 	return (sc->sc_label.d_partitions[part].p_size);
100252130Smckusick }
100352130Smckusick 
100452130Smckusick /*
100552130Smckusick  * Non-interrupt driven, non-dma dump routine.
100652130Smckusick  */
100752130Smckusick int
rzdump(dev)100852130Smckusick rzdump(dev)
100952130Smckusick 	dev_t dev;
101052130Smckusick {
101152130Smckusick #ifdef notdef
101252130Smckusick 	int part = rzpart(dev);
101352130Smckusick 	int unit = rzunit(dev);
101452130Smckusick 	register struct rz_softc *sc = &rz_softc[unit];
101552130Smckusick 	register struct scsi_device *sd = sc->sc_hd;
101652130Smckusick 	register daddr_t baddr;
101752130Smckusick 	register int maddr;
101852130Smckusick 	register int pages, i;
101952130Smckusick 	int stat;
102052130Smckusick 	extern int lowram;
102152130Smckusick 
102252130Smckusick 	/*
102352130Smckusick 	 * Hmm... all vax drivers dump maxfree pages which is physmem minus
102452130Smckusick 	 * the message buffer.  Is there a reason for not dumping the
102552130Smckusick 	 * message buffer?  Savecore expects to read 'dumpsize' pages of
102652130Smckusick 	 * dump, where dumpsys() sets dumpsize to physmem!
102752130Smckusick 	 */
102852130Smckusick 	pages = physmem;
102952130Smckusick 
103052130Smckusick 	/* is drive ok? */
103152130Smckusick 	if (unit >= NRZ || (sc->sc_flags & RZF_ALIVE) == 0)
103252130Smckusick 		return (ENXIO);
103352130Smckusick 	/* dump parameters in range? */
103452130Smckusick 	if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks)
103552130Smckusick 		return (EINVAL);
103652130Smckusick 	if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks)
103752130Smckusick 		pages = dtoc(sc->sc_info.part[part].nblocks - dumplo);
103852130Smckusick 	maddr = lowram;
103952130Smckusick 	baddr = dumplo + sc->sc_info.part[part].strtblk;
104052130Smckusick 	/* scsi bus idle? */
104152130Smckusick 	if (!scsireq(&sc->sc_dq)) {
104252130Smckusick 		scsireset(sd->sd_ctlr);
104352130Smckusick 		sc->sc_stats.rzresets++;
104452130Smckusick 		printf("[ drive %d reset ] ", unit);
104552130Smckusick 	}
104652130Smckusick 	for (i = 0; i < pages; i++) {
104752130Smckusick #define NPGMB	(1024*1024/NBPG)
104852130Smckusick 		/* print out how many Mbs we have dumped */
104952130Smckusick 		if (i && (i % NPGMB) == 0)
105052130Smckusick 			printf("%d ", i / NPGMB);
105152130Smckusick #undef NPBMG
105252130Smckusick 		mapin(mmap, (u_int)vmmap, btop(maddr), PG_URKR|PG_CI|PG_V);
105352130Smckusick 		stat = scsi_tt_write(sd->sd_ctlr, sd->sd_drive, sd->sd_slave,
105452130Smckusick 				     vmmap, NBPG, baddr, sc->sc_bshift);
105552130Smckusick 		if (stat) {
105652130Smckusick 			printf("rzdump: scsi write error 0x%x\n", stat);
105752130Smckusick 			return (EIO);
105852130Smckusick 		}
105952130Smckusick 		maddr += NBPG;
106052130Smckusick 		baddr += ctod(1);
106152130Smckusick 	}
106252130Smckusick 	return (0);
106359823Sralph #else /* notdef */
106452130Smckusick 	return (ENXIO);
106559823Sralph #endif /* notdef */
106652130Smckusick }
106752130Smckusick #endif
1068