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