xref: /csrg-svn/sys/luna68k/stand/scsi.c (revision 63199)
157098Sakito /*
257098Sakito  * Copyright (c) 1992 OMRON Corporation.
3*63199Sbostic  * Copyright (c) 1992, 1993
4*63199Sbostic  *	The Regents of the University of California.  All rights reserved.
557098Sakito  *
657098Sakito  * This code is derived from software contributed to Berkeley by
757098Sakito  * OMRON Corporation.
857098Sakito  *
957098Sakito  * %sccs.include.redist.c%
1057098Sakito  *
11*63199Sbostic  *	@(#)scsi.c	8.1 (Berkeley) 06/10/93
1257098Sakito  */
1357098Sakito 
1457098Sakito /*
1557098Sakito  * scsi.c -- front end of SCSI test commands
1657098Sakito  * by A.Fujita, FEB-09-1992
1757098Sakito  */
1857098Sakito 
1957098Sakito #include <sys/param.h>
2057098Sakito #include <luna68k/dev/scsireg.h>
2157098Sakito #include <luna68k/stand/status.h>
2257098Sakito 
2357098Sakito 
2457098Sakito int scsi_device = 6;
2557098Sakito 
26*63199Sbostic #define SENSBUFF 8					/* 06/10/93P%$%98.1i%$%P$G%;%s%9%G!<%? */
2757098Sakito 							/* $ND9$5$r#8/usr/home/csrg/sccs/sys/luna68k/stand/SCCS/s.scsi.c$%H0JFb$K8GDj$7$F */
2857098Sakito u_char	sensbuff[SENSBUFF];				/* #80J>e$OL50UL#$G$"$k!#         */
2957098Sakito 
3057098Sakito static struct scsi_inquiry inquirybuf;
3157098Sakito static struct scsi_fmt_cdb inquiry = {
3257098Sakito 	6,
3357098Sakito 	CMD_INQUIRY, 0, 0, 0, sizeof(inquirybuf), 0
3457098Sakito };
3557098Sakito 
3657098Sakito static u_long capacitybuf[2];
3757098Sakito struct scsi_fmt_cdb capacity = {
3857098Sakito 	10,
3957098Sakito 	CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0
4057098Sakito };
4157098Sakito 
4257098Sakito 
4357098Sakito int
scsi(argc,argv)4457098Sakito scsi(argc, argv)
4557098Sakito 	int   argc;
4657098Sakito 	char *argv[];
4757098Sakito {
4857098Sakito 	register char *p;
4957098Sakito 	register int i, status;
5057098Sakito 
5157098Sakito 	if (argc < 2) {
5257098Sakito 		printf("This command is required subcommand !!\n");
5357098Sakito 		return(ST_ERROR);
5457098Sakito 	}
5557098Sakito 
5657098Sakito 	if (!strcmp(argv[1], "device")) {
5757098Sakito 		if (argc > 2) {
5857098Sakito 			i = 0;
5957098Sakito 			for (p = argv[2]; *p != NULL; p++) {
6057098Sakito 				i = i * 10 + *p - '0';
6157098Sakito 			}
6257098Sakito 			if ( i < 8 && i >= 0) {
6357098Sakito 				scsi_device = i;
6457098Sakito 			}
6557098Sakito 		}
6657098Sakito 		printf("Current Device ID: %d\n", scsi_device);
6757098Sakito 	} else if (!strcmp(argv[1], "test_unit_rdy")) {
6857098Sakito 				/* CTLR  SLAVE  LUN */
6957098Sakito 		scsi_test_unit_rdy(   0, scsi_device,   0);
7057098Sakito 	} else if (!strcmp(argv[1], "request_sense")) {
7157098Sakito 			        /* CTLR  SLAVE  LUN */
7257098Sakito 		scsi_request_sense(   0, scsi_device,   0, sensbuff, SENSBUFF);
7357098Sakito 	} else if (!strcmp(argv[1], "inquiry")) {
7457098Sakito 		if (scsi_immed_command(   0, scsi_device,   0, &inquiry,
7557098Sakito 				       (u_char *) &inquirybuf, sizeof(inquirybuf)) == 0) {
7657098Sakito 			printf("Type:\t0x%x\n",		inquirybuf.type);
7757098Sakito 			printf("Qualifier:\t0x%x\n",	inquirybuf.qual);
7857098Sakito 			printf("Version:\t0x%x\n",	inquirybuf.version);
7957098Sakito 			printf("RDF:\t0x%x\n",		inquirybuf.rsvd);
8057098Sakito 
8157098Sakito 			printf("Vender ID:\t");
8257098Sakito 			for (i = 0; i < 8; i++)
8357098Sakito 				printf("%c", inquirybuf.vendor_id[i]);
8457098Sakito 			printf("\n");
8557098Sakito 
8657098Sakito 			printf("Product ID:\t");
8757098Sakito 			for (i = 0; i < 16; i++)
8857098Sakito 				printf("%c", inquirybuf.product_id[i]);
8957098Sakito 			printf("\n");
9057098Sakito 
9157098Sakito 			printf("Revision:\t");
9257098Sakito 			for (i = 0; i < 4; i++)
9357098Sakito 				printf("%c", inquirybuf.rev[i]);
9457098Sakito 			printf("\n");
9557098Sakito 		}
9657098Sakito 	} else if (!strcmp(argv[1], "read_capacity")) {
9757098Sakito 		if (scsi_immed_command(   0, scsi_device,   0, &capacity,
9857098Sakito 				       (u_char *) &capacitybuf, sizeof(capacitybuf)) == 0) {
9957098Sakito 			printf("Logical Block Address:\t%d (0x%x)\n",
10057098Sakito 			       capacitybuf[0], capacitybuf[0]);
10157098Sakito 			printf("Block Length:\t\t%d (0x%x)\n",
10257098Sakito 			       capacitybuf[1], capacitybuf[1]);
10357098Sakito 		}
10457098Sakito 	} else if (!strcmp(argv[1], "trace")) {
10557098Sakito 		for (i = 0; i < 7; i++) {
10657098Sakito 			printf("SCSI ID %d .... ", i);
10757098Sakito 			status = scsi_test_unit_rdy( 0, i, 0);
10857098Sakito 			if (status >= 0)
10957098Sakito 				printf("found.\n");
11057098Sakito 			else
11157098Sakito 				printf("no.\n");
11257098Sakito 		}
11357098Sakito 	} else if (!strcmp(argv[1], "format_unit")) {
11457098Sakito 		i = 0;
11557098Sakito 		while (i == 0) {
11657098Sakito 			printf("Do you really want to format SCSI %d device ? [y/n]: ",
11757098Sakito 			       scsi_device);
11857098Sakito 			i = cngetc();
11957098Sakito 			printf("\n");
12057098Sakito 			if ((i != 'y') && (i != 'Y') && (i != 'n') && (i != 'N'))
12157098Sakito 				i = 0;
12257098Sakito 		}
12357098Sakito 
12457098Sakito 		if ((i == 'y') || (i == 'Y'))
12557098Sakito 			status = scsi_format_unit( 0, scsi_device, 0);
12657098Sakito 	}
12757098Sakito 
12857098Sakito 	return(ST_NORMAL);
12957098Sakito }
13057098Sakito 
13157098Sakito static struct scsi_fmt_cdb scsi_cdb = {
13257098Sakito 	10,
13357098Sakito 	0,  0, 0, 0, 0, 0, 0, 0, 0, 0
13457098Sakito };
13557098Sakito 
13657098Sakito int
scsi_read_raw(target,blk,nblk,buff,len)13757098Sakito scsi_read_raw(target, blk, nblk, buff, len)
13857098Sakito 	u_int   target;
13957098Sakito 	u_int   blk;
14057098Sakito 	u_int   nblk;
14157098Sakito 	u_char *buff;
14257098Sakito 	u_int   len;
14357098Sakito {
14457098Sakito 	register struct scsi_fmt_cdb *cdb = &scsi_cdb;
14557098Sakito 
14657098Sakito 	cdb->cdb[0] = CMD_READ_EXT;
14757098Sakito 
14857098Sakito 	cdb->cdb[2] = (blk & 0xff000000) >> 24;
14957098Sakito 	cdb->cdb[3] = (blk & 0x00ff0000) >> 16;
15057098Sakito 	cdb->cdb[4] = (blk & 0x0000ff00) >>  8;
15157098Sakito 	cdb->cdb[5] = (blk & 0x000000ff);
15257098Sakito 
15357098Sakito 	cdb->cdb[7] = (nblk & 0xff00) >> 8;
15457098Sakito 	cdb->cdb[8] = (nblk & 0x00ff);
15557098Sakito 
15657098Sakito 	if (scsi_immed_command(0, target, 0, cdb, buff, len) == 0)
15757098Sakito 		return(1);
15857098Sakito 	else
15957098Sakito 		return(0);
16057098Sakito }
16157098Sakito 
16257098Sakito int
scsi_read(blk,buff,len)16357098Sakito scsi_read(blk, buff, len)
16457098Sakito 	u_int   blk;
16557098Sakito 	u_char *buff;
16657098Sakito 	u_int   len;
16757098Sakito {
16857098Sakito 	u_int   nblk = len >> DEV_BSHIFT;
16957098Sakito 
17057098Sakito 	return(scsi_read_raw(scsi_device, blk, nblk, buff, len));
17157098Sakito }
17257098Sakito 
17357098Sakito int
scsi_write(blk,buff,len)17457098Sakito scsi_write(blk, buff, len)
17557098Sakito 	u_int   blk;
17657098Sakito 	u_char *buff;
17757098Sakito 	u_int   len;
17857098Sakito {
17957098Sakito 	register struct scsi_fmt_cdb *cdb = &scsi_cdb;
18057098Sakito 
18157098Sakito 	cdb->cdb[0] = CMD_WRITE_EXT;
18257098Sakito 
18357098Sakito 	cdb->cdb[2] = (blk & 0xff000000) >> 24;
18457098Sakito 	cdb->cdb[3] = (blk & 0x00ff0000) >> 16;
18557098Sakito 	cdb->cdb[4] = (blk & 0x0000ff00) >>  8;
18657098Sakito 	cdb->cdb[5] = (blk & 0x000000ff);
18757098Sakito 
18857098Sakito 	cdb->cdb[7] = ((len >> DEV_BSHIFT) & 0xff00) >> 8;
18957098Sakito 	cdb->cdb[8] = ((len >> DEV_BSHIFT) & 0x00ff);
19057098Sakito 
19157098Sakito 	if (scsi_immed_command(0, scsi_device, 0, cdb, buff, len) == 0)
19257098Sakito 		return(1);
19357098Sakito 	else
19457098Sakito 		return(0);
19557098Sakito }
196