xref: /titanic_50/usr/src/cmd/cdrw/transport.c (revision 83eefa44a30911772c048cba898c57272799c791)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54730c9c4Smikeri  * Common Development and Distribution License (the "License").
64730c9c4Smikeri  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*83eefa44Srameshc  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "transport.h"
377c478bd9Sstevel@tonic-gate #include "main.h"
387c478bd9Sstevel@tonic-gate #include "util.h"
397c478bd9Sstevel@tonic-gate #include "mmc.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate char rqbuf[RQBUFLEN];
427c478bd9Sstevel@tonic-gate uchar_t	uscsi_status, rqstatus, rqresid;
437c478bd9Sstevel@tonic-gate static struct	uscsi_cmd uscmd;
447c478bd9Sstevel@tonic-gate static char	ucdb[16];
457c478bd9Sstevel@tonic-gate static uint_t	total_retries;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate struct uscsi_cmd *
get_uscsi_cmd(void)487c478bd9Sstevel@tonic-gate get_uscsi_cmd(void)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	(void) memset(&uscmd, 0, sizeof (uscmd));
517c478bd9Sstevel@tonic-gate 	(void) memset(ucdb, 0, 16);
527c478bd9Sstevel@tonic-gate 	uscmd.uscsi_cdb = ucdb;
537c478bd9Sstevel@tonic-gate 	return (&uscmd);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate int
uscsi(int fd,struct uscsi_cmd * scmd)577c478bd9Sstevel@tonic-gate uscsi(int fd, struct uscsi_cmd *scmd)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	int ret, global_rqsense;
607c478bd9Sstevel@tonic-gate 	int retries, max_retries;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/* set up for request sense extensions */
637c478bd9Sstevel@tonic-gate 	if (!(scmd->uscsi_flags & USCSI_RQENABLE)) {
647c478bd9Sstevel@tonic-gate 		scmd->uscsi_flags |= USCSI_RQENABLE;
657c478bd9Sstevel@tonic-gate 		scmd->uscsi_rqlen = RQBUFLEN;
667c478bd9Sstevel@tonic-gate 		scmd->uscsi_rqbuf = rqbuf;
677c478bd9Sstevel@tonic-gate 		global_rqsense = 1;
687c478bd9Sstevel@tonic-gate 	} else {
697c478bd9Sstevel@tonic-gate 		global_rqsense = 0;
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * Some DVD drives may have a delay for writing or sync cache, and
747c478bd9Sstevel@tonic-gate 	 * read media info (done after syncing cache). This can take a
757c478bd9Sstevel@tonic-gate 	 * significant number of time. Such as the Pioneer A0X which will
767c478bd9Sstevel@tonic-gate 	 * generate TOC after the cache is full in the middle of writing.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if ((device_type != CD_RW) && ((scmd->uscsi_cdb[0] == WRITE_10_CMD) ||
807c478bd9Sstevel@tonic-gate 	    (scmd->uscsi_cdb[0] == READ_INFO_CMD) || (scmd->uscsi_cdb[0] ==
817c478bd9Sstevel@tonic-gate 	    SYNC_CACHE_CMD) || (scmd->uscsi_cdb[0] == CLOSE_TRACK_CMD))) {
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		max_retries = 500;
847c478bd9Sstevel@tonic-gate 	} else {
850c83a891Snakanon 		/*
860c83a891Snakanon 		 * Pioneer A08/A09 retries approx 30 times.
870c83a891Snakanon 		 */
880c83a891Snakanon 		max_retries = 40;
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * The device may be busy or slow and fail with a not ready status.
937c478bd9Sstevel@tonic-gate 	 * we'll allow a limited number of retries to give the drive time
947c478bd9Sstevel@tonic-gate 	 * to recover.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	for (retries = 0; retries < max_retries; retries++) {
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		scmd->uscsi_status = 0;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		if (global_rqsense)
1017c478bd9Sstevel@tonic-gate 			(void) memset(rqbuf, 0, RQBUFLEN);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		if (debug && verbose) {
1047c478bd9Sstevel@tonic-gate 			int i;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 			(void) printf("cmd:[");
1077c478bd9Sstevel@tonic-gate 			for (i = 0; i < scmd->uscsi_cdblen; i++)
1087c478bd9Sstevel@tonic-gate 				(void) printf("0x%02x ",
1097c478bd9Sstevel@tonic-gate 				    (uchar_t)scmd->uscsi_cdb[i]);
1107c478bd9Sstevel@tonic-gate 			(void) printf("]\n");
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		/*
1147c478bd9Sstevel@tonic-gate 		 * We need to have root privledges in order to use
1157c478bd9Sstevel@tonic-gate 		 * uscsi commands on the device.
1167c478bd9Sstevel@tonic-gate 		 */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		raise_priv();
1197c478bd9Sstevel@tonic-gate 		ret = ioctl(fd, USCSICMD, scmd);
1207c478bd9Sstevel@tonic-gate 		lower_priv();
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		/* maintain consistency in case of sgen */
1237c478bd9Sstevel@tonic-gate 		if ((ret == 0) && (scmd->uscsi_status == 2)) {
1247c478bd9Sstevel@tonic-gate 			ret = -1;
1257c478bd9Sstevel@tonic-gate 			errno = EIO;
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		/* if error and extended request sense, retrieve errors */
1297c478bd9Sstevel@tonic-gate 		if (global_rqsense && (ret < 0) && (scmd->uscsi_status == 2)) {
1307c478bd9Sstevel@tonic-gate 			/*
1317c478bd9Sstevel@tonic-gate 			 * The drive is not ready to recieve commands but
1327c478bd9Sstevel@tonic-gate 			 * may be in the process of becoming ready.
1337c478bd9Sstevel@tonic-gate 			 * sleep for a short time then retry command.
1347c478bd9Sstevel@tonic-gate 			 * SENSE/ASC = 2/4 : not ready
1357c478bd9Sstevel@tonic-gate 			 * ASCQ = 0  Not Reportable.
1367c478bd9Sstevel@tonic-gate 			 * ASCQ = 1  Becoming ready.
1377c478bd9Sstevel@tonic-gate 			 * ASCQ = 4  FORMAT in progress.
1387c478bd9Sstevel@tonic-gate 			 * ASCQ = 7  Operation in progress.
1397c478bd9Sstevel@tonic-gate 			 * ASCQ = 8  Long write in progress.
1407c478bd9Sstevel@tonic-gate 			 */
1417c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
1427c478bd9Sstevel@tonic-gate 			    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
1437c478bd9Sstevel@tonic-gate 			    (ASCQ(rqbuf) == 4)) || (ASCQ(rqbuf) == 7)) {
1447c478bd9Sstevel@tonic-gate 				total_retries++;
1457c478bd9Sstevel@tonic-gate 				(void) sleep(3);
1467c478bd9Sstevel@tonic-gate 				continue;
1477c478bd9Sstevel@tonic-gate 			}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 			/*
1507c478bd9Sstevel@tonic-gate 			 * we do not print this out under normal circumstances
1517c478bd9Sstevel@tonic-gate 			 * since we have BUFE enabled and do not want to alarm
1527c478bd9Sstevel@tonic-gate 			 * users with uneccessary messages.
1537c478bd9Sstevel@tonic-gate 			 */
1547c478bd9Sstevel@tonic-gate 			if (debug) {
1557c478bd9Sstevel@tonic-gate 				if ((SENSE_KEY(rqbuf) == 5) && (ASC(rqbuf) ==
1567c478bd9Sstevel@tonic-gate 				    0x21) && (ASCQ(rqbuf) == 2)) {
1577c478bd9Sstevel@tonic-gate 					(void) printf(gettext(
1587c478bd9Sstevel@tonic-gate 			"Buffer underrun occurred! trying to recover...\n"));
1597c478bd9Sstevel@tonic-gate 				}
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			/*
1637c478bd9Sstevel@tonic-gate 			 * long write operation in progress, ms_delay is
1647c478bd9Sstevel@tonic-gate 			 * used for some fast drives with a short drive
1657c478bd9Sstevel@tonic-gate 			 * buffer. Such as Pioneer DVD-RW drives. They will
1667c478bd9Sstevel@tonic-gate 			 * begin to generate TOC when the buffer is initially
1677c478bd9Sstevel@tonic-gate 			 * full, then resume operation a few minutes later
1687c478bd9Sstevel@tonic-gate 			 * with the buffer emptying quickly.
1697c478bd9Sstevel@tonic-gate 			 */
1707c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
1717c478bd9Sstevel@tonic-gate 			    (ASCQ(rqbuf) == 8)) {
1727c478bd9Sstevel@tonic-gate 				total_retries++;
173*83eefa44Srameshc 				/*
174*83eefa44Srameshc 				 * In Simulation write mode, we use the
175*83eefa44Srameshc 				 * READ_INFO_CMD to check if all the previous
176*83eefa44Srameshc 				 * writes completed. Sleeping 500 ms will not
177*83eefa44Srameshc 				 * be sufficient in all cases for DVDs.
178*83eefa44Srameshc 				 */
1795d465cc7Sminht 				if ((device_type != CD_RW) &&
180*83eefa44Srameshc 				    ((scmd->uscsi_cdb[0] == CLOSE_TRACK_CMD) ||
181*83eefa44Srameshc 				    ((scmd->uscsi_cdb[0] == READ_INFO_CMD) &&
182*83eefa44Srameshc 				    simulation)))
1835d465cc7Sminht 					(void) sleep(3);
1845d465cc7Sminht 				else
1857c478bd9Sstevel@tonic-gate 					ms_delay(500);
1867c478bd9Sstevel@tonic-gate 				continue;
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 			/*
1897c478bd9Sstevel@tonic-gate 			 * Device is not ready to transmit or a device reset
1907c478bd9Sstevel@tonic-gate 			 * has occurred. wait for a short period of time then
1917c478bd9Sstevel@tonic-gate 			 * retry the command.
1927c478bd9Sstevel@tonic-gate 			 */
1937c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) ||
1947c478bd9Sstevel@tonic-gate 			    (ASC(rqbuf) == 0x29))) {
1957c478bd9Sstevel@tonic-gate 				(void) sleep(3);
1967c478bd9Sstevel@tonic-gate 				total_retries++;
1977c478bd9Sstevel@tonic-gate 				continue;
1987c478bd9Sstevel@tonic-gate 			}
1994730c9c4Smikeri 
2004730c9c4Smikeri 			if ((SENSE_KEY(rqbuf) == 5) &&
2014730c9c4Smikeri 			    (device_type == DVD_PLUS ||
2024730c9c4Smikeri 			    device_type == DVD_PLUS_W)) {
2034730c9c4Smikeri 				if (scmd->uscsi_cdb[0] == MODE_SELECT_10_CMD &&
2044730c9c4Smikeri 				    ASC(rqbuf) == 0x26) {
2054730c9c4Smikeri 					ret = 1;
2064730c9c4Smikeri 					break;
2074730c9c4Smikeri 				}
2084730c9c4Smikeri 
2094730c9c4Smikeri 				if (scmd->uscsi_cdb[0] == REZERO_UNIT_CMD &&
2104730c9c4Smikeri 				    ASC(rqbuf) == 0x20) {
2114730c9c4Smikeri 					ret = 1;
2124730c9c4Smikeri 					break;
2134730c9c4Smikeri 				}
2144730c9c4Smikeri 
2154730c9c4Smikeri 			}
2167c478bd9Sstevel@tonic-gate 			/*
2177c478bd9Sstevel@tonic-gate 			 * Blank Sense, we don't know what the error is or if
2187c478bd9Sstevel@tonic-gate 			 * the command succeeded, Hope for the best. Some
2197c478bd9Sstevel@tonic-gate 			 * drives return blank sense periodically and will
2207c478bd9Sstevel@tonic-gate 			 * fail if this is removed.
2217c478bd9Sstevel@tonic-gate 			 */
2227c478bd9Sstevel@tonic-gate 			if ((SENSE_KEY(rqbuf) == 0) && (ASC(rqbuf) == 0) &&
2237c478bd9Sstevel@tonic-gate 			    (ASCQ(rqbuf) == 0)) {
2247c478bd9Sstevel@tonic-gate 				ret = 0;
2257c478bd9Sstevel@tonic-gate 				break;
2267c478bd9Sstevel@tonic-gate 			}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 			if (debug) {
2297c478bd9Sstevel@tonic-gate 				(void) printf("cmd: 0x%02x ret:%i status:%02x "
2307c478bd9Sstevel@tonic-gate 				    " sense: %02x ASC: %02x ASCQ:%02x\n",
2317c478bd9Sstevel@tonic-gate 				    (uchar_t)scmd->uscsi_cdb[0], ret,
2327c478bd9Sstevel@tonic-gate 				    scmd->uscsi_status,
2337c478bd9Sstevel@tonic-gate 				    (uchar_t)SENSE_KEY(rqbuf),
2347c478bd9Sstevel@tonic-gate 				    (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf));
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		/* no errors we'll return */
2397c478bd9Sstevel@tonic-gate 		break;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/* store the error status for later debug printing */
2437c478bd9Sstevel@tonic-gate 	if ((ret < 0) && (global_rqsense)) {
2447c478bd9Sstevel@tonic-gate 		uscsi_status = scmd->uscsi_status;
2457c478bd9Sstevel@tonic-gate 		rqstatus = scmd->uscsi_rqstatus;
2467c478bd9Sstevel@tonic-gate 		rqresid = scmd->uscsi_rqresid;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (debug && retries) {
2517c478bd9Sstevel@tonic-gate 		(void) printf("total retries: %d\n", total_retries);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	return (ret);
2557c478bd9Sstevel@tonic-gate }
256