155431Sbostic /*-
2*66640Sbostic  * Copyright (c) 1992, 1993
3*66640Sbostic  *	The Regents of the University of California.  All rights reserved.
455431Sbostic  *
555431Sbostic  * This software was developed by the Computer Systems Engineering group
655431Sbostic  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
755431Sbostic  * contributed to Berkeley.
855431Sbostic  *
955431Sbostic  * %sccs.include.redist.c%
1055431Sbostic  *
11*66640Sbostic  *	@(#)scsiformat.c	5.5 (Berkeley) 04/02/94
1255431Sbostic  */
1355431Sbostic 
1461561Sbostic #ifndef lint
1561561Sbostic char copyright[] =
16*66640Sbostic "@(#) Copyright (c) 1992, 1993\n\
17*66640Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1861561Sbostic #endif /* not lint */
1961561Sbostic 
2061561Sbostic #ifndef lint
21*66640Sbostic static char sccsid[] = "@(#)scsiformat.c	5.5 (Berkeley) 04/02/94";
2261561Sbostic #endif /* not lint */
2361561Sbostic 
2455431Sbostic #include <sys/param.h>
2555431Sbostic #include <sys/ioctl.h>
2655431Sbostic 
2761561Sbostic #include <dev/scsi/scsi.h>
2861561Sbostic #include <dev/scsi/disk.h>
2961561Sbostic #include <dev/scsi/disktape.h>
3061561Sbostic #include <dev/scsi/scsi_ioctl.h>
3161561Sbostic 
3261561Sbostic #define COMPAT_HPSCSI
3361561Sbostic 
3461561Sbostic #include <errno.h>
3555431Sbostic #include <fcntl.h>
3661561Sbostic #include <stdio.h>
3755431Sbostic #include <stdlib.h>
3855431Sbostic #include <string.h>
3961561Sbostic #include <unistd.h>
4055431Sbostic 
4155431Sbostic int fd;
4255431Sbostic char *device;
4355431Sbostic 
4461561Sbostic void	scsi_str __P((char *, char *, int));
4561561Sbostic void	do_command __P((int, struct scsi_cdb *, void *, int));
4661549Sbostic void	do_format __P((void));
4755431Sbostic void	print_capacity __P((void));
4855431Sbostic void	print_inquiry __P((void));
4961561Sbostic void	prflags __P((int, const char *));
5055431Sbostic u_char *print_mode_page __P((u_char *));
5155431Sbostic void	print_mode_sense __P((void));
5255431Sbostic void	usage __P((void));
5355431Sbostic 
5461561Sbostic #define	N2(c, d)	(((c) << 8) | (d))
5561561Sbostic #define	N3(b, c, d)	(((b) << 16) | N2(c, d))
5661561Sbostic #define	N4(a, b, c, d)	(((a) << 24) | N3(b, c, d))
5761561Sbostic 
5861561Sbostic int	sense_pctl;
5961561Sbostic 
6055431Sbostic int
main(argc,argv)6161549Sbostic main(argc, argv)
6255431Sbostic 	int argc;
6355431Sbostic 	char *argv[];
6455431Sbostic {
6561561Sbostic 	extern char *optarg;
6661561Sbostic 	int ch, readonly;
6755431Sbostic 
6861561Sbostic 	readonly = 0;
6961561Sbostic 	sense_pctl = SCSI_MSENSE_PCTL_CUR;
7061561Sbostic 	while ((ch = getopt(argc, argv, "rp:")) != EOF) {
7155431Sbostic 		switch(ch) {
7261561Sbostic 		case 'r':
7361561Sbostic 			readonly = 1;
7461561Sbostic 			break;
7561561Sbostic 		case 'p':		/* mode sense page control */
7661561Sbostic 			switch (*optarg) {
7761561Sbostic 			case 'c':
7861561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_CUR;
7961561Sbostic 				break;
8061561Sbostic 			case 'd':
8161561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_DFLT;
8261561Sbostic 				break;
8361561Sbostic 			case 's':
8461561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_SAVED;
8561561Sbostic 				break;
8661561Sbostic 			case 'v':
8761561Sbostic 				(void)printf(
8861561Sbostic 	"*** note: for variable parameters, 1-bit means ``can write here''\n");
8961561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_VAR;
9061561Sbostic 				break;
9161561Sbostic 			}
9261561Sbostic 			/* FALLTHROUGH */
9355431Sbostic 		case '?':
9455431Sbostic 		default:
9555431Sbostic 			usage();
9655431Sbostic 		}
9761561Sbostic 	}
9855431Sbostic 	argc -= optind;
9955431Sbostic 	argv += optind;
10055431Sbostic 
10155431Sbostic 	if (argc != 1)
10255431Sbostic 		usage();
10355431Sbostic 
10455448Sbostic 	device = *argv;
10561561Sbostic 	fd = open(device, readonly ? O_RDONLY : O_RDWR, 0);
10661561Sbostic 	if (fd < 0) {
10755431Sbostic 		(void)fprintf(stderr,
10855431Sbostic 		    "scsiformat: %s: %s\n", device, strerror(errno));
10955431Sbostic 		exit(1);
11055431Sbostic 	}
11155431Sbostic 	print_inquiry();
11255431Sbostic 	print_capacity();
11355431Sbostic 	print_mode_sense();
11461549Sbostic 
11561561Sbostic 	if (!readonly)
11661561Sbostic 		do_format();
11755431Sbostic 	exit(0);
11855431Sbostic }
11955431Sbostic 
12061561Sbostic /*
12161561Sbostic  * Copy a counted string, trimming trailing blanks, and turning the
12261561Sbostic  * result into a C-style string.
12361561Sbostic  */
12455431Sbostic void
scsi_str(src,dst,len)12561561Sbostic scsi_str(src, dst, len)
12661561Sbostic 	register char *src, *dst;
12761561Sbostic 	register int len;
12861561Sbostic {
12961561Sbostic 
13061561Sbostic 	while (src[len - 1] == ' ') {
13161561Sbostic 		if (--len == 0) {
13261561Sbostic 			*dst = 0;
13361561Sbostic 			return;
13461561Sbostic 		}
13561561Sbostic 	}
13661561Sbostic 	bcopy(src, dst, len);
13761561Sbostic 	dst[len] = 0;
13861561Sbostic }
13961561Sbostic 
14061561Sbostic void
print_inquiry()14155431Sbostic print_inquiry()
14255431Sbostic {
14361561Sbostic 	register struct scsi_inq_ansi *si;
14461561Sbostic 	int ver;
14561561Sbostic 	struct scsi_inquiry inqbuf;
14661561Sbostic 	char vendor[10], product[17], rev[5];
14761561Sbostic 	static struct scsi_cdb inq = {
14861561Sbostic 		CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0
14961561Sbostic 	};
15055431Sbostic 
15161561Sbostic 	do_command(fd, &inq, &inqbuf, sizeof(inqbuf));
15261561Sbostic 	(void)printf("%s: ", device);
15355431Sbostic 
15461561Sbostic 	ver = (inqbuf.si_version >> VER_ANSI_SHIFT) & VER_ANSI_MASK;
15561561Sbostic 	if (ver != 1 && ver != 2) {
15661561Sbostic 		(void)printf("type 0x%x, qual 0x%x, ver 0x%x (ansi %d)\n",
15761561Sbostic 		    inqbuf.si_type, inqbuf.si_qual, inqbuf.si_version, ver);
15855431Sbostic 		return;
15955431Sbostic 	}
16061561Sbostic 	si = (struct scsi_inq_ansi *)&inqbuf;
16161561Sbostic 	switch (si->si_type & TYPE_TYPE_MASK) {
16261561Sbostic 
16361561Sbostic 	case TYPE_DAD:
16461561Sbostic 		(void)printf("(disk)");
16561561Sbostic 		break;
16661561Sbostic 
16761561Sbostic 	case TYPE_WORM:
16861561Sbostic 		(void)printf("(WORM)");
16961561Sbostic 		break;
17061561Sbostic 
17161561Sbostic 	case TYPE_ROM:
17261561Sbostic 		(void)printf("(CD-ROM)");
17361561Sbostic 		break;
17461561Sbostic 
17561561Sbostic 	case TYPE_MO:
17661561Sbostic 		(void)printf("(MO-DISK)");
17761561Sbostic 		break;
17861561Sbostic 
17961561Sbostic 	case TYPE_JUKEBOX:
18061561Sbostic 		(void)printf("(jukebox)");
18161561Sbostic 		break;
18261561Sbostic 
18361561Sbostic 	default:
18461561Sbostic 		(void)printf("(??)");
18561561Sbostic 		break;
18655431Sbostic 	}
18761561Sbostic 	scsi_str(si->si_vendor, vendor, sizeof(si->si_vendor));
18861561Sbostic 	scsi_str(si->si_product, product, sizeof(si->si_product));
18961561Sbostic 	scsi_str(si->si_rev, rev, sizeof(si->si_rev));
19061561Sbostic 	(void)printf(" %s %s rev %s:", vendor, product, rev);
19155431Sbostic }
19255431Sbostic 
19355431Sbostic void
print_capacity()19455431Sbostic print_capacity()
19555431Sbostic {
19661561Sbostic 	struct scsi_rc rc;		/* for READ CAPACITY */
19761561Sbostic 	static struct scsi_cdb cap = { CMD_READ_CAPACITY };
19861561Sbostic 
19961561Sbostic 	do_command(fd, &cap, &rc, sizeof(rc));
20061561Sbostic 	(void)printf(" %d blocks of %d bytes each\n",
20161561Sbostic 	    N4(rc.rc_lbah, rc.rc_lbahm, rc.rc_lbalm, rc.rc_lbal) + 1,
20261561Sbostic 	    N4(rc.rc_blh, rc.rc_blhm, rc.rc_bllm, rc.rc_bll));
20355431Sbostic }
20455431Sbostic 
20555448Sbostic void
print_mode_sense()20655448Sbostic print_mode_sense()
20755448Sbostic {
20861561Sbostic 	register u_char *cp, *ep;
20961561Sbostic 	register struct scsi_ms_bd *bd;
21061561Sbostic 	register int n, i, l, len, bdlen;
21161561Sbostic #ifdef TEN_BYTE_SENSE
21261561Sbostic 	struct {
21361561Sbostic 		struct	scsi_ms10 ms;
21461561Sbostic 		u_char	p[1023 - sizeof(struct scsi_ms10)];
21561561Sbostic 	} msbuf;
21661561Sbostic 	static struct scsi_cdb modesense = {
21761561Sbostic 		CMD_MODE_SENSE10, SCSI_MSENSE_DBD, 0, 0, 0, 0, 0,
21861561Sbostic 		sizeof(msbuf) >> 8, sizeof (msbuf), 0
21961561Sbostic 	};
22055448Sbostic 
22161561Sbostic 	CDB10(&modesense)->cdb_lbam = sense_pctl | SCSI_MS_PC_ALL;
22261561Sbostic 	do_command(fd, &modesense, &msbuf, sizeof(msbuf));
22361561Sbostic 	len = N2(msbuf.ms.ms_lenh, msbuf.ms.ms_lenl);
22461561Sbostic 	bdlen = N2(msbuf.ms.ms_bdlh, msbuf.ms.ms_bdll);
22561561Sbostic #else
22661561Sbostic 	struct {
22761561Sbostic 		struct	scsi_ms6 ms;
22861561Sbostic 		u_char	p[255 - sizeof(struct scsi_ms6)];
22961561Sbostic 	} msbuf;
23061561Sbostic 	static struct scsi_cdb modesense = {
23161561Sbostic 		CMD_MODE_SENSE6, 0, 0, 0, sizeof(msbuf), 0
23261561Sbostic 	};
23355448Sbostic 
23461561Sbostic 	CDB6(&modesense)->cdb_lbam = sense_pctl | SCSI_MS_PC_ALL;
23561561Sbostic 	do_command(fd, &modesense, &msbuf, sizeof(msbuf));
23661561Sbostic 	len = msbuf.ms.ms_len;
23761561Sbostic 	bdlen = msbuf.ms.ms_bdl;
23861561Sbostic #endif
23961561Sbostic 	(void)printf("\n%d bytes of mode sense data. ", len);
24061561Sbostic 	(void)printf("medium type 0x%x, %swrite protected\n",
24161561Sbostic 	    msbuf.ms.ms_mt, msbuf.ms.ms_dsp & SCSI_MS_DSP_WP ? "" : "not ");
24261561Sbostic 	if ((n = bdlen) != 0) {
24361561Sbostic 		bd = (struct scsi_ms_bd *)msbuf.p;
24461561Sbostic 		for (n /= sizeof(*bd); --n >= 0; bd++) {
24561561Sbostic 			(void)printf("\tdensity code 0x%x, ", bd->bd_dc);
24661561Sbostic 			i = N3(bd->bd_nbh, bd->bd_nbm, bd->bd_nbl);
24761561Sbostic 			l = N3(bd->bd_blh, bd->bd_blm, bd->bd_bll);
24861561Sbostic 			if (i)
24961561Sbostic 				(void)printf("%d blocks of length %d\n", i, l);
25061561Sbostic 			else
25161561Sbostic 				(void)printf("all blocks of length %d\n", l);
25261561Sbostic 		}
25361561Sbostic 	}
25461561Sbostic 	/*
25561561Sbostic 	 * Sense header lengths includes the sense header, while mode page
25661561Sbostic 	 * lengths do not ... let's hear it for consistency!
25761561Sbostic 	 */
25861561Sbostic 	cp = msbuf.p + bdlen;
25961561Sbostic 	ep = msbuf.p + len - sizeof(msbuf.ms);
26055448Sbostic 	while (cp < ep)
26155448Sbostic 		cp = print_mode_page(cp);
26255448Sbostic }
26355448Sbostic 
26461561Sbostic void
prflags(v,cp)26561561Sbostic prflags(v, cp)
26661561Sbostic 	int v;
26761561Sbostic 	register const char *cp;
26861561Sbostic {
26961561Sbostic 	register const char *np;
27061561Sbostic 	char f, sep;
27161561Sbostic 
27261561Sbostic 	for (sep = '<'; (f = *cp++) != 0; cp = np) {
27361561Sbostic 		for (np = cp; *np >= ' ';)
27461561Sbostic 			np++;
27561561Sbostic 		if ((v & (1 << (f - 1))) == 0)
27661561Sbostic 			continue;
27761561Sbostic 		printf("%c%.*s", sep, np - cp, cp);
27861561Sbostic 		sep = ',';
27961561Sbostic 	}
28061561Sbostic 	if (sep != '<')
28161561Sbostic 		putchar('>');
28261561Sbostic }
28361561Sbostic 
28461561Sbostic static char *
cache_policy(x)28561561Sbostic cache_policy(x)
28661561Sbostic 	int x;
28761561Sbostic {
28861561Sbostic 	static char rsvd[30];
28961561Sbostic 
29061561Sbostic 	switch (x) {
29161561Sbostic 
29261561Sbostic 	case SCSI_CACHE_DEFAULT:
29361561Sbostic 		return ("default");
29461561Sbostic 
29561561Sbostic 	case SCSI_CACHE_KEEPPF:
29661561Sbostic 		return ("toss cmd data, save prefetch");
29761561Sbostic 
29861561Sbostic 	case SCSI_CACHE_KEEPCMD:
29961561Sbostic 		return ("toss prefetch data, save cmd");
30061561Sbostic 
30161561Sbostic 	default:
30261561Sbostic 		(void)sprintf(rsvd, "reserved %d", x);
30361561Sbostic 		return (rsvd);
30461561Sbostic 	}
30561561Sbostic 	/* NOTREACHED */
30661561Sbostic }
30761561Sbostic 
30855431Sbostic u_char *
print_mode_page(cp)30955431Sbostic print_mode_page(cp)
31055431Sbostic 	u_char *cp;
31155431Sbostic {
31261561Sbostic 	register struct scsi_ms_page_hdr *mp;
31361561Sbostic 	int len, code, i;
31461561Sbostic 	u_char *tp;
31561561Sbostic 	const char *s;
31655431Sbostic 
31761561Sbostic 	mp = (struct scsi_ms_page_hdr *)cp;
31861561Sbostic 	code = mp->mp_psc & SCSI_MS_PC_MASK;
31961561Sbostic 	len = mp->mp_len;
32061561Sbostic 	(void)printf("\npage type %d%s (%d bytes): ",
32161561Sbostic 	    code, mp->mp_psc & SCSI_MS_MP_SAVEABLE ? " (saveable)" : "", len);
32261561Sbostic 	switch (code) {
32355431Sbostic 
32461561Sbostic 	case SCSI_MS_PC_RWERRREC:
32561561Sbostic #define	rw ((struct scsi_page_rwerrrec *)(mp + 1))
32661561Sbostic 		(void)printf("Read/Write Error Recovery parameters.\n");
32761561Sbostic 		(void)printf("\tflags = 0x%x", rw->rw_flags);
32861561Sbostic 		prflags(rw->rw_flags,
32961561Sbostic 		    "\10AWRE\7ARRE\6TB\5RC\4EER\3PER\2DTE\1DCR");
33061561Sbostic 		(void)printf(",\n\t%d read retries, %d correction span bits,\n",
33161561Sbostic 		    rw->rw_read_retry, rw->rw_corr_span);
33261561Sbostic 		(void)printf("\t%d head offsets, %d data strobe offsets%s\n",
33361561Sbostic 		    rw->rw_hd_off, rw->rw_ds_off, len > 6 ? "," : ".");
33461561Sbostic 		if (len <= 6)
33561561Sbostic 			break;
33661561Sbostic 		(void)printf("\t%d write retries, ", rw->rw_write_retry);
33761561Sbostic 		i = N2(rw->rw_rtlh, rw->rw_rtll);
33861561Sbostic 		if (i != 0xffff)
33961561Sbostic 			(void)printf("%d", i);
34055431Sbostic 		else
34161561Sbostic 			(void)printf("no");
34261561Sbostic 		(void)printf(" recovery time limit.\n");
34355431Sbostic 		break;
34461561Sbostic #undef rw
34555431Sbostic 
34661561Sbostic 	case SCSI_MS_PC_DR:
34761561Sbostic #define	dr ((struct scsi_page_dr *)(mp + 1))
34861561Sbostic 		(void)printf("Disconnect/Reconnect control.\n");
34961561Sbostic 		(void)printf("\tbuffer full ratio %d, buffer empty ratio %d,\n",
35061561Sbostic 		    dr->dr_full, dr->dr_empty);
35161561Sbostic 		(void)printf("\ttime limits: %d bus inactivity, ",
35261561Sbostic 		    N2(dr->dr_inacth, dr->dr_inactl));
35361561Sbostic 		(void)printf("%d disconnect, %d connect.\n",
35461561Sbostic 		    N2(dr->dr_disconh, dr->dr_disconl),
35561561Sbostic 		    N2(dr->dr_conh, dr->dr_conl));
35661561Sbostic 		(void)printf("\tmaximum burst size %d,\n",
35761561Sbostic 		    N2(dr->dr_bursth, dr->dr_burstl));
35861561Sbostic 		switch (dr->dr_dtdc & SCSI_DR_DTDC_MASK) {
35961561Sbostic 		case SCSI_DR_DTDC_NONE:
36061561Sbostic 			s = "never";
36161561Sbostic 			break;
36261561Sbostic 		case SCSI_DR_DTDC_NOTDATA:
36361561Sbostic 			s = "during data transfer";
36461561Sbostic 			break;
36561561Sbostic 		case SCSI_DR_DTDC_RSVD:
36661561Sbostic 			s = "???";
36761561Sbostic 			break;
36861561Sbostic 		case SCSI_DR_DTDC_NOTD2:
36961561Sbostic 			s = "during and after data transfer";
37061561Sbostic 			break;
37161561Sbostic 		}
37261561Sbostic 		(void)printf("\tsuppress disconnect %s.\n", s);
37355431Sbostic 		break;
37461561Sbostic #undef dr
37555431Sbostic 
37661561Sbostic 	case SCSI_MS_PC_FMT:
37761561Sbostic #define	fmt ((struct scsi_page_fmt *)(mp + 1))
37861561Sbostic 		(void)printf("Format parameters.\n");
37961561Sbostic 		(void)printf("\t%d tracks/zone, %d alt.sect./zone, ",
38061561Sbostic 		    N2(fmt->fmt_tpzh, fmt->fmt_tpzl),
38161561Sbostic 		    N2(fmt->fmt_aspzh, fmt->fmt_aspzl));
38261561Sbostic 		(void)printf("%d alt.tracks/zone,\n\t%d alt.tracks/vol., ",
38361561Sbostic 		    N2(fmt->fmt_atpzh, fmt->fmt_atpzl),
38461561Sbostic 		    N2(fmt->fmt_atpvh, fmt->fmt_atpvl));
38561561Sbostic 		(void)printf("%d sectors/track, %d bytes/phys.sector,\n",
38661561Sbostic 		    N2(fmt->fmt_spth, fmt->fmt_sptl),
38761561Sbostic 		    N2(fmt->fmt_dbppsh, fmt->fmt_dbppsl));
38861561Sbostic 		(void)printf("\tinterleave %d, track skew %d, cyl.skew %d,\n",
38961561Sbostic 		    N2(fmt->fmt_ilh, fmt->fmt_ill),
39061561Sbostic 		    N2(fmt->fmt_tsfh, fmt->fmt_tsfl),
39161561Sbostic 		    N2(fmt->fmt_csfh, fmt->fmt_csfl));
39261561Sbostic 		(void)printf("\tdrive flags 0x%x", fmt->fmt_flags);
39361561Sbostic 		prflags(fmt->fmt_flags, "\10SSEC\7HSEC\6RMB\5SURF");
39461561Sbostic 		(void)printf(".\n");
39561561Sbostic 		break;
39661561Sbostic #undef fmt
39761561Sbostic 
39861561Sbostic 	case SCSI_MS_PC_RDGEOM:
39961561Sbostic #define rd ((struct scsi_page_rdgeom *)(mp + 1))
40061561Sbostic 		(void)printf("Disk Geometry parameters.\n");
40161561Sbostic 		(void)printf("\t%d cylinders, %d heads,\n",
40261561Sbostic 		    N3(rd->rd_ncylh, rd->rd_ncylm, rd->rd_ncyll),
40361561Sbostic 		    rd->rd_nheads);
40461561Sbostic 		(void)printf("\tstart write precompensation at cyl %d,\n",
40561561Sbostic 		    N3(rd->rd_wpcylh, rd->rd_wpcylm, rd->rd_wpcyll));
40661561Sbostic 		(void)printf("\tstart reduced write current at cyl %d,\n",
40761561Sbostic 		    N3(rd->rd_rwcylh, rd->rd_rwcylm, rd->rd_rwcyll));
40861561Sbostic 		(void)printf("\tseek step rate %f us, landing zone cyl %d,\n",
40961561Sbostic 		    N2(rd->rd_steph, rd->rd_stepl) * 0.1,
41061561Sbostic 		    N3(rd->rd_lcylh, rd->rd_lcylm, rd->rd_lcyll));
41161561Sbostic 		switch (rd->rd_rpl & SCSI_RD_RPL_MASK) {
41261561Sbostic 		case SCSI_RD_RPL_NONE:
41361561Sbostic 			s = "disabled or unsupported";
41461561Sbostic 			break;
41561561Sbostic 		case SCSI_RD_RPL_SLAVE:
41661561Sbostic 			s = "slave";
41761561Sbostic 			break;
41861561Sbostic 		case SCSI_RD_RPL_MASTER:
41961561Sbostic 			s = "master";
42061561Sbostic 			break;
42161561Sbostic 		case SCSI_RD_RPL_MCONTROL:
42261561Sbostic 			s = "master control";
42361561Sbostic 			break;
42455431Sbostic 		}
42561561Sbostic 		(void)printf("\trotational synch %s, offset %d/256%s\n",
42661561Sbostic 		    s, rd->rd_roff, len > 18 ? "," : ".");
42761561Sbostic 		if (len > 18)
42861561Sbostic 			(void)printf("\trotation %d rpm.\n",
42961561Sbostic 			    N2(rd->rd_rpmh, rd->rd_rpml));
43055431Sbostic 		break;
43161561Sbostic #undef rd
43255431Sbostic 
43361561Sbostic 	case SCSI_MS_PC_VERRREC:
43461561Sbostic #define	v ((struct scsi_page_verrrec *)(mp + 1))
43561561Sbostic 		(void)printf("Verify Error Recovery parameters.\n");
43661561Sbostic 		(void)printf("\tflags = 0x%x", v->v_flags);
43761561Sbostic 		prflags(v->v_flags, "\4EER\3PER\2DTE\1DCR");
43861561Sbostic 		(void)printf(",\n\t%d verify retries, %d %s span bits,\n\t",
43961561Sbostic 		    v->v_verify_retry, v->v_corr_span, "correction");
44061561Sbostic 		(void)printf("%d recovery time limit.\n",
44161561Sbostic 		    N2(v->v_rtlh, v->v_rtll));
44255431Sbostic 		break;
44361561Sbostic #undef v
44455431Sbostic 
44561561Sbostic 	case SCSI_MS_PC_CACHE:
44661561Sbostic #define cache ((struct scsi_page_cache *)(mp + 1))
44761561Sbostic 		(void)printf("Caching Page.\n");
44861561Sbostic 		(void)printf("\tflags = 0x%x", cache->cache_flags);
44961561Sbostic 		prflags(cache->cache_flags, "\3WCE\2MF\1RCD");
45061561Sbostic 		(void)printf(
45161561Sbostic 		    ",\n\tread retention = %s, write retention = %s,\n",
45261561Sbostic 		    cache_policy(SCSI_CACHE_RDPOLICY(cache->cache_reten)),
45361561Sbostic 		    cache_policy(SCSI_CACHE_WRPOLICY(cache->cache_reten)));
45461561Sbostic 		(void)printf("\tdisable prefetch transfer length = %d,\n",
45561561Sbostic 		    N2(cache->cache_dptlh, cache->cache_dptll));
45661561Sbostic 		(void)printf("\tmin prefetch = %d, max prefetch = %d, ",
45761561Sbostic 		    N2(cache->cache_minpfh, cache->cache_minpfl),
45861561Sbostic 		    N2(cache->cache_maxpfh, cache->cache_maxpfl));
45961561Sbostic 		(void)printf("max prefetch ceiling = %d.\n",
46061561Sbostic 		    N2(cache->cache_mpch, cache->cache_mpcl));
46161561Sbostic 		break;
46261561Sbostic #undef cache
46361561Sbostic 
46461561Sbostic 	case SCSI_MS_PC_CTLMODE:
46561561Sbostic #define	cm ((struct scsi_page_ctlmode *)(mp + 1))
46661561Sbostic 		(void)printf("Control Mode Page.\n");
46761561Sbostic 		(void)printf("\t%s report log-activity error conditions,\n",
46861561Sbostic 		    cm->cm_rlec & SCSI_CM_RLEC ? "do" : "do not");
46961561Sbostic 		(void)printf("\tqueue algorithm modifier = %d, flags = 0x%x",
47061561Sbostic 		    SCSI_CM_QMOD(cm->cm_qctl),
47161561Sbostic 		    cm->cm_qctl & (SCSI_CM_QERR|SCSI_CM_DQUE));
47261561Sbostic 		prflags(cm->cm_qctl, "\2QERR\1DQUE");
47361561Sbostic 		(void)printf(",\n\tECA/AEN flags = 0x%x", cm->cm_ecaaen);
47461561Sbostic 		prflags(cm->cm_ecaaen, "\10ECA\3RAENP\2UUAENP\1EAENP");
47561561Sbostic 		(void)printf(", AEN holdoff period = %d ms.\n",
47661561Sbostic 		    N2(cm->cm_aenholdh, cm->cm_aenholdl));
47761561Sbostic 		break;
47861561Sbostic #undef cm
47961561Sbostic 
48061561Sbostic 	/*
48161561Sbostic 	 * Vendor Unique, but what the heck.
48261561Sbostic 	 */
48361561Sbostic 	case SCSI_MS_PC_CDCCACHECTL:
48461561Sbostic #define	ccm ((struct scsi_page_CDCcachectlmode *)(mp + 1))
48561561Sbostic 		(void)printf("CDC-specific Cache Control Mode Page.\n");
48661561Sbostic 		(void)printf("\tflags = 0x%x", ccm->ccm_flags);
48761561Sbostic 		prflags(ccm->ccm_flags, "\7WIE\5ENABLE");
48861561Sbostic 		(void)printf(", table size = %d, prefetch threshold = %d\n",
48961561Sbostic 		    SCSI_CDC_CCM_TBLSZ(ccm->ccm_flags),
49061561Sbostic 		    ccm->ccm_pfthresh);
49161561Sbostic 		(void)printf("\tmaximum %s = %d, maximum %s = %d,\n",
49261561Sbostic 		    "threshold", ccm->ccm_maxthresh,
49361561Sbostic 		    "prefetch multiplier", ccm->ccm_maxpfmult);
49461561Sbostic 		(void)printf("\tminimum %s = %d, minimum %s = %d.\n",
49561561Sbostic 		    "threshold", ccm->ccm_minthresh,
49661561Sbostic 		    "prefetch multiplier", ccm->ccm_minpfmult);
49761561Sbostic 		break;
49861561Sbostic #undef ccm
49961561Sbostic 
50055431Sbostic 	default:
50161561Sbostic 		(void)printf("Unknown page type.");
50261561Sbostic 		for (tp = cp + sizeof(*mp), i = 0; i < len; ++i) {
50355431Sbostic 			if ((i & 7) == 0)
50461561Sbostic 				(void)printf("\n\t%2d: ", i);
50561561Sbostic 			(void)printf(" %02x", *tp++);
50655431Sbostic 		}
50761561Sbostic 		(void)printf(".\n");
50855431Sbostic 		break;
50955431Sbostic 	}
51061561Sbostic 	return (cp + sizeof(*mp) + len);
51155431Sbostic }
51255431Sbostic 
51355431Sbostic void
pr_sense(fd)51461549Sbostic pr_sense(fd)
51561549Sbostic 	int fd;
51661549Sbostic {
51761549Sbostic 	static struct scsi_fmt_sense s;
51861561Sbostic 	register struct scsi_sense *sn;
51961549Sbostic 
52061549Sbostic 	if (ioctl(fd, SDIOCSENSE, &s) < 0)
52161549Sbostic 		(void)fprintf(stderr,
52261549Sbostic 		    "scsiformat: SDIOCSENSE: %s\n", strerror(errno));
52361549Sbostic 
52461549Sbostic 	(void)printf("scsi status 0x%x", s.status);
52561549Sbostic 	if (s.status & STS_CHECKCOND) {
52661561Sbostic 		sn = (struct scsi_sense *)s.sense;
52761549Sbostic 
52861561Sbostic 		(void)printf(" sense class %d, code %d",
52961561Sbostic 		    SENSE_ECLASS(sn), SENSE_ECODE(sn));
53061561Sbostic 		if (SENSE_ISXSENSE(sn)) {
53161561Sbostic 			(void)printf(", key %d", XSENSE_KEY(sn));
53261561Sbostic 			if (XSENSE_IVALID(sn))
53361561Sbostic 				(void)printf(", blk %d", XSENSE_INFO(sn));
53461549Sbostic 		}
53561549Sbostic 	}
53661549Sbostic 	(void)printf("\n");
53761549Sbostic }
53861549Sbostic 
53961549Sbostic void
do_format()54061549Sbostic do_format()
54161549Sbostic {
54261561Sbostic 	struct {
54361561Sbostic 		struct scsi_ms6 ms;		/* mode select header */
54461561Sbostic 		struct scsi_ms_bd bd;		/* block descriptor */
54561561Sbostic 		struct scsi_ms_page_hdr mp;	/* ctl mode page hdr */
54661561Sbostic 		struct scsi_page_ctlmode cm;	/* ctl mode page */
54761561Sbostic 		u_char pad[4];			/* ??? */
54861561Sbostic 	} msel;
54961561Sbostic 	u_char fmtbuf[128];
55061561Sbostic 	static struct scsi_cdb modeselect = {
55161561Sbostic 		CMD_MODE_SELECT6,
55261561Sbostic 		SCSI_MSEL_SCSI2_DATA | SCSI_MSEL_SAVEPAGES, 0, 0,
55361561Sbostic 		sizeof(msel), 0
55461561Sbostic 	};
55561561Sbostic 	static struct scsi_cdb format = { CMD_FORMAT_UNIT };
55661549Sbostic 
55761561Sbostic 	/* want mostly 0s; set them all zero here */
55861561Sbostic 	bzero(&msel, sizeof(msel));
55961549Sbostic 
56061561Sbostic 	/* one block descriptor */
56161561Sbostic 	msel.ms.ms_bdl = sizeof(struct scsi_ms_bd);
56261561Sbostic 
56361561Sbostic 	/* block length = 512 bytes */
56461561Sbostic 	msel.bd.bd_blm = 512 / 256;
56561561Sbostic 	msel.bd.bd_bll = 512 % 256;
56661561Sbostic 
56761561Sbostic 	/*
56861561Sbostic 	 * In the following, the mystery pad region is copied from
56961561Sbostic 	 * the original driver.  I have no idea what it is for.
57061561Sbostic 	 * (Anyone got SCSI-2 documents?)
57161561Sbostic 	 */
57261561Sbostic 
57361561Sbostic 	/* mode page parameters: report log-activity exception conditions */
57461561Sbostic 	msel.mp.mp_psc = SCSI_MS_PC_CTLMODE;
57561561Sbostic 	msel.mp.mp_len = sizeof(msel.cm) + sizeof(msel.pad);
57661561Sbostic 	msel.cm.cm_rlec = SCSI_CM_RLEC;
57761561Sbostic 
57861561Sbostic 	do_command(fd, &modeselect, &msel, sizeof(msel));
57961561Sbostic 
58061561Sbostic 	bzero(fmtbuf, sizeof(fmtbuf));
58161561Sbostic 	do_command(fd, &format, fmtbuf, sizeof(fmtbuf));
58261549Sbostic }
58361549Sbostic 
58461549Sbostic void
do_command(fd,cdb,buf,len)58555448Sbostic do_command(fd, cdb, buf, len)
58655448Sbostic 	int fd;
58761561Sbostic 	struct scsi_cdb *cdb;
58861561Sbostic 	void *buf;
58955448Sbostic 	int len;
59055431Sbostic {
59161561Sbostic 	static int on = 1, off = 0;
59261561Sbostic 	int user, ret;
59355431Sbostic 
59461561Sbostic 	bzero(buf, len);
59555448Sbostic 	if (ioctl(fd, SDIOCSFORMAT, &on) < 0) {
59655448Sbostic 		(void)fprintf(stderr,
59755448Sbostic 		    "scsiformat: SDIOCSFORMAT (on): %s\n", strerror(errno));
59861561Sbostic 		if (ioctl(fd, SDIOCGFORMAT, &user) == 0 && user != 0)
59961561Sbostic 			(void)fprintf(stderr, "scsiformat: pid %d has it\n",
60061561Sbostic 			    user);
60155448Sbostic 		return;
60255448Sbostic 	}
60361561Sbostic 	ret = ioctl(fd, SDIOCSCSICOMMAND, cdb);
60461561Sbostic #ifdef COMPAT_HPSCSI
60561561Sbostic 	if (ret < 0) {
60661561Sbostic 		static const char scsicmdlen[8] = { 6, 10, 0, 0, 0, 12, 0, 0 };
60761561Sbostic #define	SCSICMDLEN(cmd)	scsicmdlen[(cmd) >> 5]
60861561Sbostic 		struct scsi_fmt_cdb {
60961561Sbostic 			int	len;
61061561Sbostic 			u_char	cdb[28];
61161561Sbostic 		} sc;
61261561Sbostic #define OSDIOCSCSICOMMAND _IOW('S', 0x3, struct scsi_fmt_cdb)
61361561Sbostic 
61461561Sbostic 		sc.len = SCSICMDLEN(cdb->cdb_bytes[0]);
61561561Sbostic 		bcopy(cdb->cdb_bytes, sc.cdb, sc.len);
61661561Sbostic 		ret = ioctl(fd, OSDIOCSCSICOMMAND, &sc);
61761561Sbostic 	}
61861561Sbostic #endif
61961561Sbostic 	if (ret < 0)
62055448Sbostic 		(void)fprintf(stderr,
62155448Sbostic 		    "scsiformat: SDIOCSCSICOMMAND: %s\n", strerror(errno));
62261549Sbostic 	else if (read(fd, buf, len) < 0) {
62355448Sbostic 		(void)fprintf(stderr,
62455448Sbostic 		    "scsiformat: read: %s\n", strerror(errno));
62561549Sbostic 		pr_sense(fd);
62661549Sbostic 	}
62755431Sbostic 
62855448Sbostic 	if (ioctl(fd, SDIOCSFORMAT, &off) < 0)
62955448Sbostic 		(void)fprintf(stderr,
63055448Sbostic 		    "scsiformat: SDIOCSFORMAT (off): %s\n", strerror(errno));
63155431Sbostic }
63255431Sbostic 
63355431Sbostic void
usage()63455431Sbostic usage()
63555431Sbostic {
63661561Sbostic 	(void)fprintf(stderr, "usage: scsiformat [-r] [-p c|d|s|v] device\n");
63755431Sbostic 	exit(1);
63855431Sbostic }
639