xref: /titanic_41/usr/src/cmd/cdrw/transport.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
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 2006 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 		/*
87 		 * Pioneer A08/A09 retries approx 30 times.
88 		 */
89 		max_retries = 40;
90 	}
91 
92 	/*
93 	 * The device may be busy or slow and fail with a not ready status.
94 	 * we'll allow a limited number of retries to give the drive time
95 	 * to recover.
96 	 */
97 	for (retries = 0; retries < max_retries; retries++) {
98 
99 		scmd->uscsi_status = 0;
100 
101 		if (global_rqsense)
102 			(void) memset(rqbuf, 0, RQBUFLEN);
103 
104 		if (debug && verbose) {
105 			int i;
106 
107 			(void) printf("cmd:[");
108 			for (i = 0; i < scmd->uscsi_cdblen; i++)
109 				(void) printf("0x%02x ",
110 				    (uchar_t)scmd->uscsi_cdb[i]);
111 			(void) printf("]\n");
112 		}
113 
114 		/*
115 		 * We need to have root privledges in order to use
116 		 * uscsi commands on the device.
117 		 */
118 
119 		raise_priv();
120 		ret = ioctl(fd, USCSICMD, scmd);
121 		lower_priv();
122 
123 		/* maintain consistency in case of sgen */
124 		if ((ret == 0) && (scmd->uscsi_status == 2)) {
125 			ret = -1;
126 			errno = EIO;
127 		}
128 
129 		/* if error and extended request sense, retrieve errors */
130 		if (global_rqsense && (ret < 0) && (scmd->uscsi_status == 2)) {
131 			/*
132 			 * The drive is not ready to recieve commands but
133 			 * may be in the process of becoming ready.
134 			 * sleep for a short time then retry command.
135 			 * SENSE/ASC = 2/4 : not ready
136 			 * ASCQ = 0  Not Reportable.
137 			 * ASCQ = 1  Becoming ready.
138 			 * ASCQ = 4  FORMAT in progress.
139 			 * ASCQ = 7  Operation in progress.
140 			 * ASCQ = 8  Long write in progress.
141 			 */
142 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
143 			    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
144 			    (ASCQ(rqbuf) == 4)) || (ASCQ(rqbuf) == 7)) {
145 				total_retries++;
146 				(void) sleep(3);
147 				continue;
148 			}
149 
150 			/*
151 			 * we do not print this out under normal circumstances
152 			 * since we have BUFE enabled and do not want to alarm
153 			 * users with uneccessary messages.
154 			 */
155 			if (debug) {
156 				if ((SENSE_KEY(rqbuf) == 5) && (ASC(rqbuf) ==
157 				    0x21) && (ASCQ(rqbuf) == 2)) {
158 					(void) printf(gettext(
159 			"Buffer underrun occurred! trying to recover...\n"));
160 				}
161 			}
162 
163 			/*
164 			 * long write operation in progress, ms_delay is
165 			 * used for some fast drives with a short drive
166 			 * buffer. Such as Pioneer DVD-RW drives. They will
167 			 * begin to generate TOC when the buffer is initially
168 			 * full, then resume operation a few minutes later
169 			 * with the buffer emptying quickly.
170 			 */
171 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
172 			    (ASCQ(rqbuf) == 8)) {
173 				total_retries++;
174 				if ((device_type != CD_RW) &&
175 				    (scmd->uscsi_cdb[0] == CLOSE_TRACK_CMD))
176 					(void) sleep(3);
177 				else
178 					ms_delay(500);
179 				continue;
180 			}
181 			/*
182 			 * Device is not ready to transmit or a device reset
183 			 * has occurred. wait for a short period of time then
184 			 * retry the command.
185 			 */
186 			if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) ||
187 			    (ASC(rqbuf) == 0x29))) {
188 				(void) sleep(3);
189 				total_retries++;
190 				continue;
191 			}
192 			/*
193 			 * Blank Sense, we don't know what the error is or if
194 			 * the command succeeded, Hope for the best. Some
195 			 * drives return blank sense periodically and will
196 			 * fail if this is removed.
197 			 */
198 			if ((SENSE_KEY(rqbuf) == 0) && (ASC(rqbuf) == 0) &&
199 			    (ASCQ(rqbuf) == 0)) {
200 				ret = 0;
201 				break;
202 			}
203 
204 			if (debug) {
205 				(void) printf("cmd: 0x%02x ret:%i status:%02x "
206 				    " sense: %02x ASC: %02x ASCQ:%02x\n",
207 				    (uchar_t)scmd->uscsi_cdb[0], ret,
208 				    scmd->uscsi_status,
209 				    (uchar_t)SENSE_KEY(rqbuf),
210 				    (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf));
211 			}
212 		}
213 
214 		/* no errors we'll return */
215 		break;
216 	}
217 
218 	/* store the error status for later debug printing */
219 	if ((ret < 0) && (global_rqsense)) {
220 		uscsi_status = scmd->uscsi_status;
221 		rqstatus = scmd->uscsi_rqstatus;
222 		rqresid = scmd->uscsi_rqresid;
223 
224 	}
225 
226 	if (debug && retries) {
227 		(void) printf("total retries: %d\n", total_retries);
228 	}
229 
230 	return (ret);
231 }
232