xref: /titanic_54/usr/src/cmd/cdrw/transport.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <libintl.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "transport.h"
38*7c478bd9Sstevel@tonic-gate #include "main.h"
39*7c478bd9Sstevel@tonic-gate #include "util.h"
40*7c478bd9Sstevel@tonic-gate #include "mmc.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate char rqbuf[RQBUFLEN];
43*7c478bd9Sstevel@tonic-gate uchar_t	uscsi_status, rqstatus, rqresid;
44*7c478bd9Sstevel@tonic-gate static struct	uscsi_cmd uscmd;
45*7c478bd9Sstevel@tonic-gate static char	ucdb[16];
46*7c478bd9Sstevel@tonic-gate static uint_t	total_retries;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate struct uscsi_cmd *
49*7c478bd9Sstevel@tonic-gate get_uscsi_cmd(void)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	(void) memset(&uscmd, 0, sizeof (uscmd));
52*7c478bd9Sstevel@tonic-gate 	(void) memset(ucdb, 0, 16);
53*7c478bd9Sstevel@tonic-gate 	uscmd.uscsi_cdb = ucdb;
54*7c478bd9Sstevel@tonic-gate 	return (&uscmd);
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int
58*7c478bd9Sstevel@tonic-gate uscsi(int fd, struct uscsi_cmd *scmd)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	int ret, global_rqsense;
61*7c478bd9Sstevel@tonic-gate 	int retries, max_retries;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	/* set up for request sense extensions */
64*7c478bd9Sstevel@tonic-gate 	if (!(scmd->uscsi_flags & USCSI_RQENABLE)) {
65*7c478bd9Sstevel@tonic-gate 		scmd->uscsi_flags |= USCSI_RQENABLE;
66*7c478bd9Sstevel@tonic-gate 		scmd->uscsi_rqlen = RQBUFLEN;
67*7c478bd9Sstevel@tonic-gate 		scmd->uscsi_rqbuf = rqbuf;
68*7c478bd9Sstevel@tonic-gate 		global_rqsense = 1;
69*7c478bd9Sstevel@tonic-gate 	} else {
70*7c478bd9Sstevel@tonic-gate 		global_rqsense = 0;
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	 * Some DVD drives may have a delay for writing or sync cache, and
75*7c478bd9Sstevel@tonic-gate 	 * read media info (done after syncing cache). This can take a
76*7c478bd9Sstevel@tonic-gate 	 * significant number of time. Such as the Pioneer A0X which will
77*7c478bd9Sstevel@tonic-gate 	 * generate TOC after the cache is full in the middle of writing.
78*7c478bd9Sstevel@tonic-gate 	 */
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if ((device_type != CD_RW) && ((scmd->uscsi_cdb[0] == WRITE_10_CMD) ||
81*7c478bd9Sstevel@tonic-gate 	    (scmd->uscsi_cdb[0] == READ_INFO_CMD) || (scmd->uscsi_cdb[0] ==
82*7c478bd9Sstevel@tonic-gate 	    SYNC_CACHE_CMD) || (scmd->uscsi_cdb[0] == CLOSE_TRACK_CMD))) {
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 		max_retries = 500;
85*7c478bd9Sstevel@tonic-gate 	} else {
86*7c478bd9Sstevel@tonic-gate 		max_retries = 20;
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	/*
90*7c478bd9Sstevel@tonic-gate 	 * The device may be busy or slow and fail with a not ready status.
91*7c478bd9Sstevel@tonic-gate 	 * we'll allow a limited number of retries to give the drive time
92*7c478bd9Sstevel@tonic-gate 	 * to recover.
93*7c478bd9Sstevel@tonic-gate 	 */
94*7c478bd9Sstevel@tonic-gate 	for (retries = 0; retries < max_retries; retries++) {
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 		scmd->uscsi_status = 0;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		if (global_rqsense)
99*7c478bd9Sstevel@tonic-gate 			(void) memset(rqbuf, 0, RQBUFLEN);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		if (debug && verbose) {
102*7c478bd9Sstevel@tonic-gate 			int i;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 			(void) printf("cmd:[");
105*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < scmd->uscsi_cdblen; i++)
106*7c478bd9Sstevel@tonic-gate 				(void) printf("0x%02x ",
107*7c478bd9Sstevel@tonic-gate 				    (uchar_t)scmd->uscsi_cdb[i]);
108*7c478bd9Sstevel@tonic-gate 			(void) printf("]\n");
109*7c478bd9Sstevel@tonic-gate 		}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 		/*
112*7c478bd9Sstevel@tonic-gate 		 * We need to have root privledges in order to use
113*7c478bd9Sstevel@tonic-gate 		 * uscsi commands on the device.
114*7c478bd9Sstevel@tonic-gate 		 */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 		raise_priv();
117*7c478bd9Sstevel@tonic-gate 		ret = ioctl(fd, USCSICMD, scmd);
118*7c478bd9Sstevel@tonic-gate 		lower_priv();
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 		/* maintain consistency in case of sgen */
121*7c478bd9Sstevel@tonic-gate 		if ((ret == 0) && (scmd->uscsi_status == 2)) {
122*7c478bd9Sstevel@tonic-gate 			ret = -1;
123*7c478bd9Sstevel@tonic-gate 			errno = EIO;
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		/* if error and extended request sense, retrieve errors */
127*7c478bd9Sstevel@tonic-gate 		if (global_rqsense && (ret < 0) && (scmd->uscsi_status == 2)) {
128*7c478bd9Sstevel@tonic-gate 			/*
129*7c478bd9Sstevel@tonic-gate 			 * The drive is not ready to recieve commands but
130*7c478bd9Sstevel@tonic-gate 			 * may be in the process of becoming ready.
131*7c478bd9Sstevel@tonic-gate 			 * sleep for a short time then retry command.
132*7c478bd9Sstevel@tonic-gate 			 * SENSE/ASC = 2/4 : not ready
133*7c478bd9Sstevel@tonic-gate 			 * ASCQ = 0  Not Reportable.
134*7c478bd9Sstevel@tonic-gate 			 * ASCQ = 1  Becoming ready.
135*7c478bd9Sstevel@tonic-gate 			 * ASCQ = 4  FORMAT in progress.
136*7c478bd9Sstevel@tonic-gate 			 * ASCQ = 7  Operation in progress.
137*7c478bd9Sstevel@tonic-gate 			 * ASCQ = 8  Long write in progress.
138*7c478bd9Sstevel@tonic-gate 			 */
139*7c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
140*7c478bd9Sstevel@tonic-gate 			    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
141*7c478bd9Sstevel@tonic-gate 			    (ASCQ(rqbuf) == 4)) || (ASCQ(rqbuf) == 7)) {
142*7c478bd9Sstevel@tonic-gate 				total_retries++;
143*7c478bd9Sstevel@tonic-gate 				(void) sleep(3);
144*7c478bd9Sstevel@tonic-gate 				continue;
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 			/*
148*7c478bd9Sstevel@tonic-gate 			 * we do not print this out under normal circumstances
149*7c478bd9Sstevel@tonic-gate 			 * since we have BUFE enabled and do not want to alarm
150*7c478bd9Sstevel@tonic-gate 			 * users with uneccessary messages.
151*7c478bd9Sstevel@tonic-gate 			 */
152*7c478bd9Sstevel@tonic-gate 			if (debug) {
153*7c478bd9Sstevel@tonic-gate 				if ((SENSE_KEY(rqbuf) == 5) && (ASC(rqbuf) ==
154*7c478bd9Sstevel@tonic-gate 				    0x21) && (ASCQ(rqbuf) == 2)) {
155*7c478bd9Sstevel@tonic-gate 					(void) printf(gettext(
156*7c478bd9Sstevel@tonic-gate 			"Buffer underrun occurred! trying to recover...\n"));
157*7c478bd9Sstevel@tonic-gate 				}
158*7c478bd9Sstevel@tonic-gate 			}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 			/*
161*7c478bd9Sstevel@tonic-gate 			 * long write operation in progress, ms_delay is
162*7c478bd9Sstevel@tonic-gate 			 * used for some fast drives with a short drive
163*7c478bd9Sstevel@tonic-gate 			 * buffer. Such as Pioneer DVD-RW drives. They will
164*7c478bd9Sstevel@tonic-gate 			 * begin to generate TOC when the buffer is initially
165*7c478bd9Sstevel@tonic-gate 			 * full, then resume operation a few minutes later
166*7c478bd9Sstevel@tonic-gate 			 * with the buffer emptying quickly.
167*7c478bd9Sstevel@tonic-gate 			 */
168*7c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
169*7c478bd9Sstevel@tonic-gate 			    (ASCQ(rqbuf) == 8)) {
170*7c478bd9Sstevel@tonic-gate 				total_retries++;
171*7c478bd9Sstevel@tonic-gate 				ms_delay(500);
172*7c478bd9Sstevel@tonic-gate 				continue;
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 			/*
175*7c478bd9Sstevel@tonic-gate 			 * Device is not ready to transmit or a device reset
176*7c478bd9Sstevel@tonic-gate 			 * has occurred. wait for a short period of time then
177*7c478bd9Sstevel@tonic-gate 			 * retry the command.
178*7c478bd9Sstevel@tonic-gate 			 */
179*7c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) ||
180*7c478bd9Sstevel@tonic-gate 			    (ASC(rqbuf) == 0x29))) {
181*7c478bd9Sstevel@tonic-gate 				(void) sleep(3);
182*7c478bd9Sstevel@tonic-gate 				total_retries++;
183*7c478bd9Sstevel@tonic-gate 				continue;
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 			/*
186*7c478bd9Sstevel@tonic-gate 			 * Blank Sense, we don't know what the error is or if
187*7c478bd9Sstevel@tonic-gate 			 * the command succeeded, Hope for the best. Some
188*7c478bd9Sstevel@tonic-gate 			 * drives return blank sense periodically and will
189*7c478bd9Sstevel@tonic-gate 			 * fail if this is removed.
190*7c478bd9Sstevel@tonic-gate 			 */
191*7c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 0) && (ASC(rqbuf) == 0) &&
192*7c478bd9Sstevel@tonic-gate 			    (ASCQ(rqbuf) == 0)) {
193*7c478bd9Sstevel@tonic-gate 				ret = 0;
194*7c478bd9Sstevel@tonic-gate 				break;
195*7c478bd9Sstevel@tonic-gate 			}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 			if (debug) {
198*7c478bd9Sstevel@tonic-gate 				(void) printf("cmd: 0x%02x ret:%i status:%02x "
199*7c478bd9Sstevel@tonic-gate 				    " sense: %02x ASC: %02x ASCQ:%02x\n",
200*7c478bd9Sstevel@tonic-gate 				    (uchar_t)scmd->uscsi_cdb[0], ret,
201*7c478bd9Sstevel@tonic-gate 				    scmd->uscsi_status,
202*7c478bd9Sstevel@tonic-gate 				    (uchar_t)SENSE_KEY(rqbuf),
203*7c478bd9Sstevel@tonic-gate 				    (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf));
204*7c478bd9Sstevel@tonic-gate 			}
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 		/* no errors we'll return */
208*7c478bd9Sstevel@tonic-gate 		break;
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/* store the error status for later debug printing */
212*7c478bd9Sstevel@tonic-gate 	if ((ret < 0) && (global_rqsense)) {
213*7c478bd9Sstevel@tonic-gate 		uscsi_status = scmd->uscsi_status;
214*7c478bd9Sstevel@tonic-gate 		rqstatus = scmd->uscsi_rqstatus;
215*7c478bd9Sstevel@tonic-gate 		rqresid = scmd->uscsi_rqresid;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (debug && retries) {
220*7c478bd9Sstevel@tonic-gate 		(void) printf("total retries: %d\n", total_retries);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	return (ret);
224*7c478bd9Sstevel@tonic-gate }
225