155431Sbostic /*-
255431Sbostic  * Copyright (c) 1992 The Regents of the University of California.
355431Sbostic  * 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*61561Sbostic  *	@(#)scsiformat.c	5.4 (Berkeley) 06/05/93
1255431Sbostic  */
1355431Sbostic 
14*61561Sbostic #ifndef lint
15*61561Sbostic char copyright[] =
16*61561Sbostic "@(#) Copyright (c) 1993 The Regents of the University of California.\n\
17*61561Sbostic  All rights reserved.\n";
18*61561Sbostic #endif /* not lint */
19*61561Sbostic 
20*61561Sbostic #ifndef lint
21*61561Sbostic static char sccsid[] = "@(#)scsiformat.c	5.4 (Berkeley) 06/05/93";
22*61561Sbostic #endif /* not lint */
23*61561Sbostic 
2455431Sbostic #include <sys/param.h>
2555431Sbostic #include <sys/ioctl.h>
2655431Sbostic 
27*61561Sbostic #include <dev/scsi/scsi.h>
28*61561Sbostic #include <dev/scsi/disk.h>
29*61561Sbostic #include <dev/scsi/disktape.h>
30*61561Sbostic #include <dev/scsi/scsi_ioctl.h>
31*61561Sbostic 
32*61561Sbostic #define COMPAT_HPSCSI
33*61561Sbostic 
34*61561Sbostic #include <errno.h>
3555431Sbostic #include <fcntl.h>
36*61561Sbostic #include <stdio.h>
3755431Sbostic #include <stdlib.h>
3855431Sbostic #include <string.h>
39*61561Sbostic #include <unistd.h>
4055431Sbostic 
4155431Sbostic int fd;
4255431Sbostic char *device;
4355431Sbostic 
44*61561Sbostic void	scsi_str __P((char *, char *, int));
45*61561Sbostic 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));
49*61561Sbostic 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 
54*61561Sbostic #define	N2(c, d)	(((c) << 8) | (d))
55*61561Sbostic #define	N3(b, c, d)	(((b) << 16) | N2(c, d))
56*61561Sbostic #define	N4(a, b, c, d)	(((a) << 24) | N3(b, c, d))
57*61561Sbostic 
58*61561Sbostic int	sense_pctl;
59*61561Sbostic 
6055431Sbostic int
6161549Sbostic main(argc, argv)
6255431Sbostic 	int argc;
6355431Sbostic 	char *argv[];
6455431Sbostic {
65*61561Sbostic 	extern char *optarg;
66*61561Sbostic 	int ch, readonly;
6755431Sbostic 
68*61561Sbostic 	readonly = 0;
69*61561Sbostic 	sense_pctl = SCSI_MSENSE_PCTL_CUR;
70*61561Sbostic 	while ((ch = getopt(argc, argv, "rp:")) != EOF) {
7155431Sbostic 		switch(ch) {
72*61561Sbostic 		case 'r':
73*61561Sbostic 			readonly = 1;
74*61561Sbostic 			break;
75*61561Sbostic 		case 'p':		/* mode sense page control */
76*61561Sbostic 			switch (*optarg) {
77*61561Sbostic 			case 'c':
78*61561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_CUR;
79*61561Sbostic 				break;
80*61561Sbostic 			case 'd':
81*61561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_DFLT;
82*61561Sbostic 				break;
83*61561Sbostic 			case 's':
84*61561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_SAVED;
85*61561Sbostic 				break;
86*61561Sbostic 			case 'v':
87*61561Sbostic 				(void)printf(
88*61561Sbostic 	"*** note: for variable parameters, 1-bit means ``can write here''\n");
89*61561Sbostic 				sense_pctl = SCSI_MSENSE_PCTL_VAR;
90*61561Sbostic 				break;
91*61561Sbostic 			}
92*61561Sbostic 			/* FALLTHROUGH */
9355431Sbostic 		case '?':
9455431Sbostic 		default:
9555431Sbostic 			usage();
9655431Sbostic 		}
97*61561Sbostic 	}
9855431Sbostic 	argc -= optind;
9955431Sbostic 	argv += optind;
10055431Sbostic 
10155431Sbostic 	if (argc != 1)
10255431Sbostic 		usage();
10355431Sbostic 
10455448Sbostic 	device = *argv;
105*61561Sbostic 	fd = open(device, readonly ? O_RDONLY : O_RDWR, 0);
106*61561Sbostic 	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 
115*61561Sbostic 	if (!readonly)
116*61561Sbostic 		do_format();
11755431Sbostic 	exit(0);
11855431Sbostic }
11955431Sbostic 
120*61561Sbostic /*
121*61561Sbostic  * Copy a counted string, trimming trailing blanks, and turning the
122*61561Sbostic  * result into a C-style string.
123*61561Sbostic  */
12455431Sbostic void
125*61561Sbostic scsi_str(src, dst, len)
126*61561Sbostic 	register char *src, *dst;
127*61561Sbostic 	register int len;
128*61561Sbostic {
129*61561Sbostic 
130*61561Sbostic 	while (src[len - 1] == ' ') {
131*61561Sbostic 		if (--len == 0) {
132*61561Sbostic 			*dst = 0;
133*61561Sbostic 			return;
134*61561Sbostic 		}
135*61561Sbostic 	}
136*61561Sbostic 	bcopy(src, dst, len);
137*61561Sbostic 	dst[len] = 0;
138*61561Sbostic }
139*61561Sbostic 
140*61561Sbostic void
14155431Sbostic print_inquiry()
14255431Sbostic {
143*61561Sbostic 	register struct scsi_inq_ansi *si;
144*61561Sbostic 	int ver;
145*61561Sbostic 	struct scsi_inquiry inqbuf;
146*61561Sbostic 	char vendor[10], product[17], rev[5];
147*61561Sbostic 	static struct scsi_cdb inq = {
148*61561Sbostic 		CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0
149*61561Sbostic 	};
15055431Sbostic 
151*61561Sbostic 	do_command(fd, &inq, &inqbuf, sizeof(inqbuf));
152*61561Sbostic 	(void)printf("%s: ", device);
15355431Sbostic 
154*61561Sbostic 	ver = (inqbuf.si_version >> VER_ANSI_SHIFT) & VER_ANSI_MASK;
155*61561Sbostic 	if (ver != 1 && ver != 2) {
156*61561Sbostic 		(void)printf("type 0x%x, qual 0x%x, ver 0x%x (ansi %d)\n",
157*61561Sbostic 		    inqbuf.si_type, inqbuf.si_qual, inqbuf.si_version, ver);
15855431Sbostic 		return;
15955431Sbostic 	}
160*61561Sbostic 	si = (struct scsi_inq_ansi *)&inqbuf;
161*61561Sbostic 	switch (si->si_type & TYPE_TYPE_MASK) {
162*61561Sbostic 
163*61561Sbostic 	case TYPE_DAD:
164*61561Sbostic 		(void)printf("(disk)");
165*61561Sbostic 		break;
166*61561Sbostic 
167*61561Sbostic 	case TYPE_WORM:
168*61561Sbostic 		(void)printf("(WORM)");
169*61561Sbostic 		break;
170*61561Sbostic 
171*61561Sbostic 	case TYPE_ROM:
172*61561Sbostic 		(void)printf("(CD-ROM)");
173*61561Sbostic 		break;
174*61561Sbostic 
175*61561Sbostic 	case TYPE_MO:
176*61561Sbostic 		(void)printf("(MO-DISK)");
177*61561Sbostic 		break;
178*61561Sbostic 
179*61561Sbostic 	case TYPE_JUKEBOX:
180*61561Sbostic 		(void)printf("(jukebox)");
181*61561Sbostic 		break;
182*61561Sbostic 
183*61561Sbostic 	default:
184*61561Sbostic 		(void)printf("(??)");
185*61561Sbostic 		break;
18655431Sbostic 	}
187*61561Sbostic 	scsi_str(si->si_vendor, vendor, sizeof(si->si_vendor));
188*61561Sbostic 	scsi_str(si->si_product, product, sizeof(si->si_product));
189*61561Sbostic 	scsi_str(si->si_rev, rev, sizeof(si->si_rev));
190*61561Sbostic 	(void)printf(" %s %s rev %s:", vendor, product, rev);
19155431Sbostic }
19255431Sbostic 
19355431Sbostic void
19455431Sbostic print_capacity()
19555431Sbostic {
196*61561Sbostic 	struct scsi_rc rc;		/* for READ CAPACITY */
197*61561Sbostic 	static struct scsi_cdb cap = { CMD_READ_CAPACITY };
198*61561Sbostic 
199*61561Sbostic 	do_command(fd, &cap, &rc, sizeof(rc));
200*61561Sbostic 	(void)printf(" %d blocks of %d bytes each\n",
201*61561Sbostic 	    N4(rc.rc_lbah, rc.rc_lbahm, rc.rc_lbalm, rc.rc_lbal) + 1,
202*61561Sbostic 	    N4(rc.rc_blh, rc.rc_blhm, rc.rc_bllm, rc.rc_bll));
20355431Sbostic }
20455431Sbostic 
20555448Sbostic void
20655448Sbostic print_mode_sense()
20755448Sbostic {
208*61561Sbostic 	register u_char *cp, *ep;
209*61561Sbostic 	register struct scsi_ms_bd *bd;
210*61561Sbostic 	register int n, i, l, len, bdlen;
211*61561Sbostic #ifdef TEN_BYTE_SENSE
212*61561Sbostic 	struct {
213*61561Sbostic 		struct	scsi_ms10 ms;
214*61561Sbostic 		u_char	p[1023 - sizeof(struct scsi_ms10)];
215*61561Sbostic 	} msbuf;
216*61561Sbostic 	static struct scsi_cdb modesense = {
217*61561Sbostic 		CMD_MODE_SENSE10, SCSI_MSENSE_DBD, 0, 0, 0, 0, 0,
218*61561Sbostic 		sizeof(msbuf) >> 8, sizeof (msbuf), 0
219*61561Sbostic 	};
22055448Sbostic 
221*61561Sbostic 	CDB10(&modesense)->cdb_lbam = sense_pctl | SCSI_MS_PC_ALL;
222*61561Sbostic 	do_command(fd, &modesense, &msbuf, sizeof(msbuf));
223*61561Sbostic 	len = N2(msbuf.ms.ms_lenh, msbuf.ms.ms_lenl);
224*61561Sbostic 	bdlen = N2(msbuf.ms.ms_bdlh, msbuf.ms.ms_bdll);
225*61561Sbostic #else
226*61561Sbostic 	struct {
227*61561Sbostic 		struct	scsi_ms6 ms;
228*61561Sbostic 		u_char	p[255 - sizeof(struct scsi_ms6)];
229*61561Sbostic 	} msbuf;
230*61561Sbostic 	static struct scsi_cdb modesense = {
231*61561Sbostic 		CMD_MODE_SENSE6, 0, 0, 0, sizeof(msbuf), 0
232*61561Sbostic 	};
23355448Sbostic 
234*61561Sbostic 	CDB6(&modesense)->cdb_lbam = sense_pctl | SCSI_MS_PC_ALL;
235*61561Sbostic 	do_command(fd, &modesense, &msbuf, sizeof(msbuf));
236*61561Sbostic 	len = msbuf.ms.ms_len;
237*61561Sbostic 	bdlen = msbuf.ms.ms_bdl;
238*61561Sbostic #endif
239*61561Sbostic 	(void)printf("\n%d bytes of mode sense data. ", len);
240*61561Sbostic 	(void)printf("medium type 0x%x, %swrite protected\n",
241*61561Sbostic 	    msbuf.ms.ms_mt, msbuf.ms.ms_dsp & SCSI_MS_DSP_WP ? "" : "not ");
242*61561Sbostic 	if ((n = bdlen) != 0) {
243*61561Sbostic 		bd = (struct scsi_ms_bd *)msbuf.p;
244*61561Sbostic 		for (n /= sizeof(*bd); --n >= 0; bd++) {
245*61561Sbostic 			(void)printf("\tdensity code 0x%x, ", bd->bd_dc);
246*61561Sbostic 			i = N3(bd->bd_nbh, bd->bd_nbm, bd->bd_nbl);
247*61561Sbostic 			l = N3(bd->bd_blh, bd->bd_blm, bd->bd_bll);
248*61561Sbostic 			if (i)
249*61561Sbostic 				(void)printf("%d blocks of length %d\n", i, l);
250*61561Sbostic 			else
251*61561Sbostic 				(void)printf("all blocks of length %d\n", l);
252*61561Sbostic 		}
253*61561Sbostic 	}
254*61561Sbostic 	/*
255*61561Sbostic 	 * Sense header lengths includes the sense header, while mode page
256*61561Sbostic 	 * lengths do not ... let's hear it for consistency!
257*61561Sbostic 	 */
258*61561Sbostic 	cp = msbuf.p + bdlen;
259*61561Sbostic 	ep = msbuf.p + len - sizeof(msbuf.ms);
26055448Sbostic 	while (cp < ep)
26155448Sbostic 		cp = print_mode_page(cp);
26255448Sbostic }
26355448Sbostic 
264*61561Sbostic void
265*61561Sbostic prflags(v, cp)
266*61561Sbostic 	int v;
267*61561Sbostic 	register const char *cp;
268*61561Sbostic {
269*61561Sbostic 	register const char *np;
270*61561Sbostic 	char f, sep;
271*61561Sbostic 
272*61561Sbostic 	for (sep = '<'; (f = *cp++) != 0; cp = np) {
273*61561Sbostic 		for (np = cp; *np >= ' ';)
274*61561Sbostic 			np++;
275*61561Sbostic 		if ((v & (1 << (f - 1))) == 0)
276*61561Sbostic 			continue;
277*61561Sbostic 		printf("%c%.*s", sep, np - cp, cp);
278*61561Sbostic 		sep = ',';
279*61561Sbostic 	}
280*61561Sbostic 	if (sep != '<')
281*61561Sbostic 		putchar('>');
282*61561Sbostic }
283*61561Sbostic 
284*61561Sbostic static char *
285*61561Sbostic cache_policy(x)
286*61561Sbostic 	int x;
287*61561Sbostic {
288*61561Sbostic 	static char rsvd[30];
289*61561Sbostic 
290*61561Sbostic 	switch (x) {
291*61561Sbostic 
292*61561Sbostic 	case SCSI_CACHE_DEFAULT:
293*61561Sbostic 		return ("default");
294*61561Sbostic 
295*61561Sbostic 	case SCSI_CACHE_KEEPPF:
296*61561Sbostic 		return ("toss cmd data, save prefetch");
297*61561Sbostic 
298*61561Sbostic 	case SCSI_CACHE_KEEPCMD:
299*61561Sbostic 		return ("toss prefetch data, save cmd");
300*61561Sbostic 
301*61561Sbostic 	default:
302*61561Sbostic 		(void)sprintf(rsvd, "reserved %d", x);
303*61561Sbostic 		return (rsvd);
304*61561Sbostic 	}
305*61561Sbostic 	/* NOTREACHED */
306*61561Sbostic }
307*61561Sbostic 
30855431Sbostic u_char *
30955431Sbostic print_mode_page(cp)
31055431Sbostic 	u_char *cp;
31155431Sbostic {
312*61561Sbostic 	register struct scsi_ms_page_hdr *mp;
313*61561Sbostic 	int len, code, i;
314*61561Sbostic 	u_char *tp;
315*61561Sbostic 	const char *s;
31655431Sbostic 
317*61561Sbostic 	mp = (struct scsi_ms_page_hdr *)cp;
318*61561Sbostic 	code = mp->mp_psc & SCSI_MS_PC_MASK;
319*61561Sbostic 	len = mp->mp_len;
320*61561Sbostic 	(void)printf("\npage type %d%s (%d bytes): ",
321*61561Sbostic 	    code, mp->mp_psc & SCSI_MS_MP_SAVEABLE ? " (saveable)" : "", len);
322*61561Sbostic 	switch (code) {
32355431Sbostic 
324*61561Sbostic 	case SCSI_MS_PC_RWERRREC:
325*61561Sbostic #define	rw ((struct scsi_page_rwerrrec *)(mp + 1))
326*61561Sbostic 		(void)printf("Read/Write Error Recovery parameters.\n");
327*61561Sbostic 		(void)printf("\tflags = 0x%x", rw->rw_flags);
328*61561Sbostic 		prflags(rw->rw_flags,
329*61561Sbostic 		    "\10AWRE\7ARRE\6TB\5RC\4EER\3PER\2DTE\1DCR");
330*61561Sbostic 		(void)printf(",\n\t%d read retries, %d correction span bits,\n",
331*61561Sbostic 		    rw->rw_read_retry, rw->rw_corr_span);
332*61561Sbostic 		(void)printf("\t%d head offsets, %d data strobe offsets%s\n",
333*61561Sbostic 		    rw->rw_hd_off, rw->rw_ds_off, len > 6 ? "," : ".");
334*61561Sbostic 		if (len <= 6)
335*61561Sbostic 			break;
336*61561Sbostic 		(void)printf("\t%d write retries, ", rw->rw_write_retry);
337*61561Sbostic 		i = N2(rw->rw_rtlh, rw->rw_rtll);
338*61561Sbostic 		if (i != 0xffff)
339*61561Sbostic 			(void)printf("%d", i);
34055431Sbostic 		else
341*61561Sbostic 			(void)printf("no");
342*61561Sbostic 		(void)printf(" recovery time limit.\n");
34355431Sbostic 		break;
344*61561Sbostic #undef rw
34555431Sbostic 
346*61561Sbostic 	case SCSI_MS_PC_DR:
347*61561Sbostic #define	dr ((struct scsi_page_dr *)(mp + 1))
348*61561Sbostic 		(void)printf("Disconnect/Reconnect control.\n");
349*61561Sbostic 		(void)printf("\tbuffer full ratio %d, buffer empty ratio %d,\n",
350*61561Sbostic 		    dr->dr_full, dr->dr_empty);
351*61561Sbostic 		(void)printf("\ttime limits: %d bus inactivity, ",
352*61561Sbostic 		    N2(dr->dr_inacth, dr->dr_inactl));
353*61561Sbostic 		(void)printf("%d disconnect, %d connect.\n",
354*61561Sbostic 		    N2(dr->dr_disconh, dr->dr_disconl),
355*61561Sbostic 		    N2(dr->dr_conh, dr->dr_conl));
356*61561Sbostic 		(void)printf("\tmaximum burst size %d,\n",
357*61561Sbostic 		    N2(dr->dr_bursth, dr->dr_burstl));
358*61561Sbostic 		switch (dr->dr_dtdc & SCSI_DR_DTDC_MASK) {
359*61561Sbostic 		case SCSI_DR_DTDC_NONE:
360*61561Sbostic 			s = "never";
361*61561Sbostic 			break;
362*61561Sbostic 		case SCSI_DR_DTDC_NOTDATA:
363*61561Sbostic 			s = "during data transfer";
364*61561Sbostic 			break;
365*61561Sbostic 		case SCSI_DR_DTDC_RSVD:
366*61561Sbostic 			s = "???";
367*61561Sbostic 			break;
368*61561Sbostic 		case SCSI_DR_DTDC_NOTD2:
369*61561Sbostic 			s = "during and after data transfer";
370*61561Sbostic 			break;
371*61561Sbostic 		}
372*61561Sbostic 		(void)printf("\tsuppress disconnect %s.\n", s);
37355431Sbostic 		break;
374*61561Sbostic #undef dr
37555431Sbostic 
376*61561Sbostic 	case SCSI_MS_PC_FMT:
377*61561Sbostic #define	fmt ((struct scsi_page_fmt *)(mp + 1))
378*61561Sbostic 		(void)printf("Format parameters.\n");
379*61561Sbostic 		(void)printf("\t%d tracks/zone, %d alt.sect./zone, ",
380*61561Sbostic 		    N2(fmt->fmt_tpzh, fmt->fmt_tpzl),
381*61561Sbostic 		    N2(fmt->fmt_aspzh, fmt->fmt_aspzl));
382*61561Sbostic 		(void)printf("%d alt.tracks/zone,\n\t%d alt.tracks/vol., ",
383*61561Sbostic 		    N2(fmt->fmt_atpzh, fmt->fmt_atpzl),
384*61561Sbostic 		    N2(fmt->fmt_atpvh, fmt->fmt_atpvl));
385*61561Sbostic 		(void)printf("%d sectors/track, %d bytes/phys.sector,\n",
386*61561Sbostic 		    N2(fmt->fmt_spth, fmt->fmt_sptl),
387*61561Sbostic 		    N2(fmt->fmt_dbppsh, fmt->fmt_dbppsl));
388*61561Sbostic 		(void)printf("\tinterleave %d, track skew %d, cyl.skew %d,\n",
389*61561Sbostic 		    N2(fmt->fmt_ilh, fmt->fmt_ill),
390*61561Sbostic 		    N2(fmt->fmt_tsfh, fmt->fmt_tsfl),
391*61561Sbostic 		    N2(fmt->fmt_csfh, fmt->fmt_csfl));
392*61561Sbostic 		(void)printf("\tdrive flags 0x%x", fmt->fmt_flags);
393*61561Sbostic 		prflags(fmt->fmt_flags, "\10SSEC\7HSEC\6RMB\5SURF");
394*61561Sbostic 		(void)printf(".\n");
395*61561Sbostic 		break;
396*61561Sbostic #undef fmt
397*61561Sbostic 
398*61561Sbostic 	case SCSI_MS_PC_RDGEOM:
399*61561Sbostic #define rd ((struct scsi_page_rdgeom *)(mp + 1))
400*61561Sbostic 		(void)printf("Disk Geometry parameters.\n");
401*61561Sbostic 		(void)printf("\t%d cylinders, %d heads,\n",
402*61561Sbostic 		    N3(rd->rd_ncylh, rd->rd_ncylm, rd->rd_ncyll),
403*61561Sbostic 		    rd->rd_nheads);
404*61561Sbostic 		(void)printf("\tstart write precompensation at cyl %d,\n",
405*61561Sbostic 		    N3(rd->rd_wpcylh, rd->rd_wpcylm, rd->rd_wpcyll));
406*61561Sbostic 		(void)printf("\tstart reduced write current at cyl %d,\n",
407*61561Sbostic 		    N3(rd->rd_rwcylh, rd->rd_rwcylm, rd->rd_rwcyll));
408*61561Sbostic 		(void)printf("\tseek step rate %f us, landing zone cyl %d,\n",
409*61561Sbostic 		    N2(rd->rd_steph, rd->rd_stepl) * 0.1,
410*61561Sbostic 		    N3(rd->rd_lcylh, rd->rd_lcylm, rd->rd_lcyll));
411*61561Sbostic 		switch (rd->rd_rpl & SCSI_RD_RPL_MASK) {
412*61561Sbostic 		case SCSI_RD_RPL_NONE:
413*61561Sbostic 			s = "disabled or unsupported";
414*61561Sbostic 			break;
415*61561Sbostic 		case SCSI_RD_RPL_SLAVE:
416*61561Sbostic 			s = "slave";
417*61561Sbostic 			break;
418*61561Sbostic 		case SCSI_RD_RPL_MASTER:
419*61561Sbostic 			s = "master";
420*61561Sbostic 			break;
421*61561Sbostic 		case SCSI_RD_RPL_MCONTROL:
422*61561Sbostic 			s = "master control";
423*61561Sbostic 			break;
42455431Sbostic 		}
425*61561Sbostic 		(void)printf("\trotational synch %s, offset %d/256%s\n",
426*61561Sbostic 		    s, rd->rd_roff, len > 18 ? "," : ".");
427*61561Sbostic 		if (len > 18)
428*61561Sbostic 			(void)printf("\trotation %d rpm.\n",
429*61561Sbostic 			    N2(rd->rd_rpmh, rd->rd_rpml));
43055431Sbostic 		break;
431*61561Sbostic #undef rd
43255431Sbostic 
433*61561Sbostic 	case SCSI_MS_PC_VERRREC:
434*61561Sbostic #define	v ((struct scsi_page_verrrec *)(mp + 1))
435*61561Sbostic 		(void)printf("Verify Error Recovery parameters.\n");
436*61561Sbostic 		(void)printf("\tflags = 0x%x", v->v_flags);
437*61561Sbostic 		prflags(v->v_flags, "\4EER\3PER\2DTE\1DCR");
438*61561Sbostic 		(void)printf(",\n\t%d verify retries, %d %s span bits,\n\t",
439*61561Sbostic 		    v->v_verify_retry, v->v_corr_span, "correction");
440*61561Sbostic 		(void)printf("%d recovery time limit.\n",
441*61561Sbostic 		    N2(v->v_rtlh, v->v_rtll));
44255431Sbostic 		break;
443*61561Sbostic #undef v
44455431Sbostic 
445*61561Sbostic 	case SCSI_MS_PC_CACHE:
446*61561Sbostic #define cache ((struct scsi_page_cache *)(mp + 1))
447*61561Sbostic 		(void)printf("Caching Page.\n");
448*61561Sbostic 		(void)printf("\tflags = 0x%x", cache->cache_flags);
449*61561Sbostic 		prflags(cache->cache_flags, "\3WCE\2MF\1RCD");
450*61561Sbostic 		(void)printf(
451*61561Sbostic 		    ",\n\tread retention = %s, write retention = %s,\n",
452*61561Sbostic 		    cache_policy(SCSI_CACHE_RDPOLICY(cache->cache_reten)),
453*61561Sbostic 		    cache_policy(SCSI_CACHE_WRPOLICY(cache->cache_reten)));
454*61561Sbostic 		(void)printf("\tdisable prefetch transfer length = %d,\n",
455*61561Sbostic 		    N2(cache->cache_dptlh, cache->cache_dptll));
456*61561Sbostic 		(void)printf("\tmin prefetch = %d, max prefetch = %d, ",
457*61561Sbostic 		    N2(cache->cache_minpfh, cache->cache_minpfl),
458*61561Sbostic 		    N2(cache->cache_maxpfh, cache->cache_maxpfl));
459*61561Sbostic 		(void)printf("max prefetch ceiling = %d.\n",
460*61561Sbostic 		    N2(cache->cache_mpch, cache->cache_mpcl));
461*61561Sbostic 		break;
462*61561Sbostic #undef cache
463*61561Sbostic 
464*61561Sbostic 	case SCSI_MS_PC_CTLMODE:
465*61561Sbostic #define	cm ((struct scsi_page_ctlmode *)(mp + 1))
466*61561Sbostic 		(void)printf("Control Mode Page.\n");
467*61561Sbostic 		(void)printf("\t%s report log-activity error conditions,\n",
468*61561Sbostic 		    cm->cm_rlec & SCSI_CM_RLEC ? "do" : "do not");
469*61561Sbostic 		(void)printf("\tqueue algorithm modifier = %d, flags = 0x%x",
470*61561Sbostic 		    SCSI_CM_QMOD(cm->cm_qctl),
471*61561Sbostic 		    cm->cm_qctl & (SCSI_CM_QERR|SCSI_CM_DQUE));
472*61561Sbostic 		prflags(cm->cm_qctl, "\2QERR\1DQUE");
473*61561Sbostic 		(void)printf(",\n\tECA/AEN flags = 0x%x", cm->cm_ecaaen);
474*61561Sbostic 		prflags(cm->cm_ecaaen, "\10ECA\3RAENP\2UUAENP\1EAENP");
475*61561Sbostic 		(void)printf(", AEN holdoff period = %d ms.\n",
476*61561Sbostic 		    N2(cm->cm_aenholdh, cm->cm_aenholdl));
477*61561Sbostic 		break;
478*61561Sbostic #undef cm
479*61561Sbostic 
480*61561Sbostic 	/*
481*61561Sbostic 	 * Vendor Unique, but what the heck.
482*61561Sbostic 	 */
483*61561Sbostic 	case SCSI_MS_PC_CDCCACHECTL:
484*61561Sbostic #define	ccm ((struct scsi_page_CDCcachectlmode *)(mp + 1))
485*61561Sbostic 		(void)printf("CDC-specific Cache Control Mode Page.\n");
486*61561Sbostic 		(void)printf("\tflags = 0x%x", ccm->ccm_flags);
487*61561Sbostic 		prflags(ccm->ccm_flags, "\7WIE\5ENABLE");
488*61561Sbostic 		(void)printf(", table size = %d, prefetch threshold = %d\n",
489*61561Sbostic 		    SCSI_CDC_CCM_TBLSZ(ccm->ccm_flags),
490*61561Sbostic 		    ccm->ccm_pfthresh);
491*61561Sbostic 		(void)printf("\tmaximum %s = %d, maximum %s = %d,\n",
492*61561Sbostic 		    "threshold", ccm->ccm_maxthresh,
493*61561Sbostic 		    "prefetch multiplier", ccm->ccm_maxpfmult);
494*61561Sbostic 		(void)printf("\tminimum %s = %d, minimum %s = %d.\n",
495*61561Sbostic 		    "threshold", ccm->ccm_minthresh,
496*61561Sbostic 		    "prefetch multiplier", ccm->ccm_minpfmult);
497*61561Sbostic 		break;
498*61561Sbostic #undef ccm
499*61561Sbostic 
50055431Sbostic 	default:
501*61561Sbostic 		(void)printf("Unknown page type.");
502*61561Sbostic 		for (tp = cp + sizeof(*mp), i = 0; i < len; ++i) {
50355431Sbostic 			if ((i & 7) == 0)
504*61561Sbostic 				(void)printf("\n\t%2d: ", i);
505*61561Sbostic 			(void)printf(" %02x", *tp++);
50655431Sbostic 		}
507*61561Sbostic 		(void)printf(".\n");
50855431Sbostic 		break;
50955431Sbostic 	}
510*61561Sbostic 	return (cp + sizeof(*mp) + len);
51155431Sbostic }
51255431Sbostic 
51355431Sbostic void
51461549Sbostic pr_sense(fd)
51561549Sbostic 	int fd;
51661549Sbostic {
51761549Sbostic 	static struct scsi_fmt_sense s;
518*61561Sbostic 	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) {
526*61561Sbostic 		sn = (struct scsi_sense *)s.sense;
52761549Sbostic 
528*61561Sbostic 		(void)printf(" sense class %d, code %d",
529*61561Sbostic 		    SENSE_ECLASS(sn), SENSE_ECODE(sn));
530*61561Sbostic 		if (SENSE_ISXSENSE(sn)) {
531*61561Sbostic 			(void)printf(", key %d", XSENSE_KEY(sn));
532*61561Sbostic 			if (XSENSE_IVALID(sn))
533*61561Sbostic 				(void)printf(", blk %d", XSENSE_INFO(sn));
53461549Sbostic 		}
53561549Sbostic 	}
53661549Sbostic 	(void)printf("\n");
53761549Sbostic }
53861549Sbostic 
53961549Sbostic void
54061549Sbostic do_format()
54161549Sbostic {
542*61561Sbostic 	struct {
543*61561Sbostic 		struct scsi_ms6 ms;		/* mode select header */
544*61561Sbostic 		struct scsi_ms_bd bd;		/* block descriptor */
545*61561Sbostic 		struct scsi_ms_page_hdr mp;	/* ctl mode page hdr */
546*61561Sbostic 		struct scsi_page_ctlmode cm;	/* ctl mode page */
547*61561Sbostic 		u_char pad[4];			/* ??? */
548*61561Sbostic 	} msel;
549*61561Sbostic 	u_char fmtbuf[128];
550*61561Sbostic 	static struct scsi_cdb modeselect = {
551*61561Sbostic 		CMD_MODE_SELECT6,
552*61561Sbostic 		SCSI_MSEL_SCSI2_DATA | SCSI_MSEL_SAVEPAGES, 0, 0,
553*61561Sbostic 		sizeof(msel), 0
554*61561Sbostic 	};
555*61561Sbostic 	static struct scsi_cdb format = { CMD_FORMAT_UNIT };
55661549Sbostic 
557*61561Sbostic 	/* want mostly 0s; set them all zero here */
558*61561Sbostic 	bzero(&msel, sizeof(msel));
55961549Sbostic 
560*61561Sbostic 	/* one block descriptor */
561*61561Sbostic 	msel.ms.ms_bdl = sizeof(struct scsi_ms_bd);
562*61561Sbostic 
563*61561Sbostic 	/* block length = 512 bytes */
564*61561Sbostic 	msel.bd.bd_blm = 512 / 256;
565*61561Sbostic 	msel.bd.bd_bll = 512 % 256;
566*61561Sbostic 
567*61561Sbostic 	/*
568*61561Sbostic 	 * In the following, the mystery pad region is copied from
569*61561Sbostic 	 * the original driver.  I have no idea what it is for.
570*61561Sbostic 	 * (Anyone got SCSI-2 documents?)
571*61561Sbostic 	 */
572*61561Sbostic 
573*61561Sbostic 	/* mode page parameters: report log-activity exception conditions */
574*61561Sbostic 	msel.mp.mp_psc = SCSI_MS_PC_CTLMODE;
575*61561Sbostic 	msel.mp.mp_len = sizeof(msel.cm) + sizeof(msel.pad);
576*61561Sbostic 	msel.cm.cm_rlec = SCSI_CM_RLEC;
577*61561Sbostic 
578*61561Sbostic 	do_command(fd, &modeselect, &msel, sizeof(msel));
579*61561Sbostic 
580*61561Sbostic 	bzero(fmtbuf, sizeof(fmtbuf));
581*61561Sbostic 	do_command(fd, &format, fmtbuf, sizeof(fmtbuf));
58261549Sbostic }
58361549Sbostic 
58461549Sbostic void
58555448Sbostic do_command(fd, cdb, buf, len)
58655448Sbostic 	int fd;
587*61561Sbostic 	struct scsi_cdb *cdb;
588*61561Sbostic 	void *buf;
58955448Sbostic 	int len;
59055431Sbostic {
591*61561Sbostic 	static int on = 1, off = 0;
592*61561Sbostic 	int user, ret;
59355431Sbostic 
594*61561Sbostic 	bzero(buf, len);
59555448Sbostic 	if (ioctl(fd, SDIOCSFORMAT, &on) < 0) {
59655448Sbostic 		(void)fprintf(stderr,
59755448Sbostic 		    "scsiformat: SDIOCSFORMAT (on): %s\n", strerror(errno));
598*61561Sbostic 		if (ioctl(fd, SDIOCGFORMAT, &user) == 0 && user != 0)
599*61561Sbostic 			(void)fprintf(stderr, "scsiformat: pid %d has it\n",
600*61561Sbostic 			    user);
60155448Sbostic 		return;
60255448Sbostic 	}
603*61561Sbostic 	ret = ioctl(fd, SDIOCSCSICOMMAND, cdb);
604*61561Sbostic #ifdef COMPAT_HPSCSI
605*61561Sbostic 	if (ret < 0) {
606*61561Sbostic 		static const char scsicmdlen[8] = { 6, 10, 0, 0, 0, 12, 0, 0 };
607*61561Sbostic #define	SCSICMDLEN(cmd)	scsicmdlen[(cmd) >> 5]
608*61561Sbostic 		struct scsi_fmt_cdb {
609*61561Sbostic 			int	len;
610*61561Sbostic 			u_char	cdb[28];
611*61561Sbostic 		} sc;
612*61561Sbostic #define OSDIOCSCSICOMMAND _IOW('S', 0x3, struct scsi_fmt_cdb)
613*61561Sbostic 
614*61561Sbostic 		sc.len = SCSICMDLEN(cdb->cdb_bytes[0]);
615*61561Sbostic 		bcopy(cdb->cdb_bytes, sc.cdb, sc.len);
616*61561Sbostic 		ret = ioctl(fd, OSDIOCSCSICOMMAND, &sc);
617*61561Sbostic 	}
618*61561Sbostic #endif
619*61561Sbostic 	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
63455431Sbostic usage()
63555431Sbostic {
636*61561Sbostic 	(void)fprintf(stderr, "usage: scsiformat [-r] [-p c|d|s|v] device\n");
63755431Sbostic 	exit(1);
63855431Sbostic }
639