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