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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*0c83a891Snakanon * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <errno.h> 357c478bd9Sstevel@tonic-gate #include <libintl.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include "transport.h" 387c478bd9Sstevel@tonic-gate #include "main.h" 397c478bd9Sstevel@tonic-gate #include "util.h" 407c478bd9Sstevel@tonic-gate #include "mmc.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate char rqbuf[RQBUFLEN]; 437c478bd9Sstevel@tonic-gate uchar_t uscsi_status, rqstatus, rqresid; 447c478bd9Sstevel@tonic-gate static struct uscsi_cmd uscmd; 457c478bd9Sstevel@tonic-gate static char ucdb[16]; 467c478bd9Sstevel@tonic-gate static uint_t total_retries; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate struct uscsi_cmd * 497c478bd9Sstevel@tonic-gate get_uscsi_cmd(void) 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate (void) memset(&uscmd, 0, sizeof (uscmd)); 527c478bd9Sstevel@tonic-gate (void) memset(ucdb, 0, 16); 537c478bd9Sstevel@tonic-gate uscmd.uscsi_cdb = ucdb; 547c478bd9Sstevel@tonic-gate return (&uscmd); 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate int 587c478bd9Sstevel@tonic-gate uscsi(int fd, struct uscsi_cmd *scmd) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate int ret, global_rqsense; 617c478bd9Sstevel@tonic-gate int retries, max_retries; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* set up for request sense extensions */ 647c478bd9Sstevel@tonic-gate if (!(scmd->uscsi_flags & USCSI_RQENABLE)) { 657c478bd9Sstevel@tonic-gate scmd->uscsi_flags |= USCSI_RQENABLE; 667c478bd9Sstevel@tonic-gate scmd->uscsi_rqlen = RQBUFLEN; 677c478bd9Sstevel@tonic-gate scmd->uscsi_rqbuf = rqbuf; 687c478bd9Sstevel@tonic-gate global_rqsense = 1; 697c478bd9Sstevel@tonic-gate } else { 707c478bd9Sstevel@tonic-gate global_rqsense = 0; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * Some DVD drives may have a delay for writing or sync cache, and 757c478bd9Sstevel@tonic-gate * read media info (done after syncing cache). This can take a 767c478bd9Sstevel@tonic-gate * significant number of time. Such as the Pioneer A0X which will 777c478bd9Sstevel@tonic-gate * generate TOC after the cache is full in the middle of writing. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate if ((device_type != CD_RW) && ((scmd->uscsi_cdb[0] == WRITE_10_CMD) || 817c478bd9Sstevel@tonic-gate (scmd->uscsi_cdb[0] == READ_INFO_CMD) || (scmd->uscsi_cdb[0] == 827c478bd9Sstevel@tonic-gate SYNC_CACHE_CMD) || (scmd->uscsi_cdb[0] == CLOSE_TRACK_CMD))) { 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate max_retries = 500; 857c478bd9Sstevel@tonic-gate } else { 86*0c83a891Snakanon /* 87*0c83a891Snakanon * Pioneer A08/A09 retries approx 30 times. 88*0c83a891Snakanon */ 89*0c83a891Snakanon max_retries = 40; 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate /* 937c478bd9Sstevel@tonic-gate * The device may be busy or slow and fail with a not ready status. 947c478bd9Sstevel@tonic-gate * we'll allow a limited number of retries to give the drive time 957c478bd9Sstevel@tonic-gate * to recover. 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate for (retries = 0; retries < max_retries; retries++) { 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate scmd->uscsi_status = 0; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (global_rqsense) 1027c478bd9Sstevel@tonic-gate (void) memset(rqbuf, 0, RQBUFLEN); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate if (debug && verbose) { 1057c478bd9Sstevel@tonic-gate int i; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate (void) printf("cmd:["); 1087c478bd9Sstevel@tonic-gate for (i = 0; i < scmd->uscsi_cdblen; i++) 1097c478bd9Sstevel@tonic-gate (void) printf("0x%02x ", 1107c478bd9Sstevel@tonic-gate (uchar_t)scmd->uscsi_cdb[i]); 1117c478bd9Sstevel@tonic-gate (void) printf("]\n"); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* 1157c478bd9Sstevel@tonic-gate * We need to have root privledges in order to use 1167c478bd9Sstevel@tonic-gate * uscsi commands on the device. 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate raise_priv(); 1207c478bd9Sstevel@tonic-gate ret = ioctl(fd, USCSICMD, scmd); 1217c478bd9Sstevel@tonic-gate lower_priv(); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* maintain consistency in case of sgen */ 1247c478bd9Sstevel@tonic-gate if ((ret == 0) && (scmd->uscsi_status == 2)) { 1257c478bd9Sstevel@tonic-gate ret = -1; 1267c478bd9Sstevel@tonic-gate errno = EIO; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* if error and extended request sense, retrieve errors */ 1307c478bd9Sstevel@tonic-gate if (global_rqsense && (ret < 0) && (scmd->uscsi_status == 2)) { 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * The drive is not ready to recieve commands but 1337c478bd9Sstevel@tonic-gate * may be in the process of becoming ready. 1347c478bd9Sstevel@tonic-gate * sleep for a short time then retry command. 1357c478bd9Sstevel@tonic-gate * SENSE/ASC = 2/4 : not ready 1367c478bd9Sstevel@tonic-gate * ASCQ = 0 Not Reportable. 1377c478bd9Sstevel@tonic-gate * ASCQ = 1 Becoming ready. 1387c478bd9Sstevel@tonic-gate * ASCQ = 4 FORMAT in progress. 1397c478bd9Sstevel@tonic-gate * ASCQ = 7 Operation in progress. 1407c478bd9Sstevel@tonic-gate * ASCQ = 8 Long write in progress. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) && 1437c478bd9Sstevel@tonic-gate ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) || 1447c478bd9Sstevel@tonic-gate (ASCQ(rqbuf) == 4)) || (ASCQ(rqbuf) == 7)) { 1457c478bd9Sstevel@tonic-gate total_retries++; 1467c478bd9Sstevel@tonic-gate (void) sleep(3); 1477c478bd9Sstevel@tonic-gate continue; 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * we do not print this out under normal circumstances 1527c478bd9Sstevel@tonic-gate * since we have BUFE enabled and do not want to alarm 1537c478bd9Sstevel@tonic-gate * users with uneccessary messages. 1547c478bd9Sstevel@tonic-gate */ 1557c478bd9Sstevel@tonic-gate if (debug) { 1567c478bd9Sstevel@tonic-gate if ((SENSE_KEY(rqbuf) == 5) && (ASC(rqbuf) == 1577c478bd9Sstevel@tonic-gate 0x21) && (ASCQ(rqbuf) == 2)) { 1587c478bd9Sstevel@tonic-gate (void) printf(gettext( 1597c478bd9Sstevel@tonic-gate "Buffer underrun occurred! trying to recover...\n")); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* 1647c478bd9Sstevel@tonic-gate * long write operation in progress, ms_delay is 1657c478bd9Sstevel@tonic-gate * used for some fast drives with a short drive 1667c478bd9Sstevel@tonic-gate * buffer. Such as Pioneer DVD-RW drives. They will 1677c478bd9Sstevel@tonic-gate * begin to generate TOC when the buffer is initially 1687c478bd9Sstevel@tonic-gate * full, then resume operation a few minutes later 1697c478bd9Sstevel@tonic-gate * with the buffer emptying quickly. 1707c478bd9Sstevel@tonic-gate */ 1717c478bd9Sstevel@tonic-gate if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) && 1727c478bd9Sstevel@tonic-gate (ASCQ(rqbuf) == 8)) { 1737c478bd9Sstevel@tonic-gate total_retries++; 1747c478bd9Sstevel@tonic-gate ms_delay(500); 1757c478bd9Sstevel@tonic-gate continue; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * Device is not ready to transmit or a device reset 1797c478bd9Sstevel@tonic-gate * has occurred. wait for a short period of time then 1807c478bd9Sstevel@tonic-gate * retry the command. 1817c478bd9Sstevel@tonic-gate */ 1827c478bd9Sstevel@tonic-gate if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) || 1837c478bd9Sstevel@tonic-gate (ASC(rqbuf) == 0x29))) { 1847c478bd9Sstevel@tonic-gate (void) sleep(3); 1857c478bd9Sstevel@tonic-gate total_retries++; 1867c478bd9Sstevel@tonic-gate continue; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate /* 1897c478bd9Sstevel@tonic-gate * Blank Sense, we don't know what the error is or if 1907c478bd9Sstevel@tonic-gate * the command succeeded, Hope for the best. Some 1917c478bd9Sstevel@tonic-gate * drives return blank sense periodically and will 1927c478bd9Sstevel@tonic-gate * fail if this is removed. 1937c478bd9Sstevel@tonic-gate */ 1947c478bd9Sstevel@tonic-gate if ((SENSE_KEY(rqbuf) == 0) && (ASC(rqbuf) == 0) && 1957c478bd9Sstevel@tonic-gate (ASCQ(rqbuf) == 0)) { 1967c478bd9Sstevel@tonic-gate ret = 0; 1977c478bd9Sstevel@tonic-gate break; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate if (debug) { 2017c478bd9Sstevel@tonic-gate (void) printf("cmd: 0x%02x ret:%i status:%02x " 2027c478bd9Sstevel@tonic-gate " sense: %02x ASC: %02x ASCQ:%02x\n", 2037c478bd9Sstevel@tonic-gate (uchar_t)scmd->uscsi_cdb[0], ret, 2047c478bd9Sstevel@tonic-gate scmd->uscsi_status, 2057c478bd9Sstevel@tonic-gate (uchar_t)SENSE_KEY(rqbuf), 2067c478bd9Sstevel@tonic-gate (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf)); 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* no errors we'll return */ 2117c478bd9Sstevel@tonic-gate break; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* store the error status for later debug printing */ 2157c478bd9Sstevel@tonic-gate if ((ret < 0) && (global_rqsense)) { 2167c478bd9Sstevel@tonic-gate uscsi_status = scmd->uscsi_status; 2177c478bd9Sstevel@tonic-gate rqstatus = scmd->uscsi_rqstatus; 2187c478bd9Sstevel@tonic-gate rqresid = scmd->uscsi_rqresid; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if (debug && retries) { 2237c478bd9Sstevel@tonic-gate (void) printf("total retries: %d\n", total_retries); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate return (ret); 2277c478bd9Sstevel@tonic-gate } 228