1 /* $NetBSD: atapi_base.c,v 1.23 2004/09/17 23:30:22 mycroft Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace 9 * Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: atapi_base.c,v 1.23 2004/09/17 23:30:22 mycroft Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/buf.h> 47 #include <sys/uio.h> 48 #include <sys/malloc.h> 49 #include <sys/errno.h> 50 #include <sys/device.h> 51 #include <sys/proc.h> 52 53 #include <dev/scsipi/scsipi_all.h> 54 #include <dev/scsipi/scsipiconf.h> 55 #include <dev/scsipi/atapiconf.h> 56 #include <dev/scsipi/scsipi_base.h> 57 58 /* 59 * Look at the returned sense and act on the error, determining 60 * the unix error number to pass back. (0 = report no error) 61 * 62 * THIS IS THE DEFAULT ERROR HANDLER 63 */ 64 int 65 atapi_interpret_sense(struct scsipi_xfer *xs) 66 { 67 struct scsipi_periph *periph = xs->xs_periph; 68 int key, error; 69 char *msg = NULL; 70 71 /* 72 * If the device has it's own error handler, call it first. 73 * If it returns a legit error value, return that, otherwise 74 * it wants us to continue with normal error processing. 75 */ 76 if (periph->periph_switch->psw_error != NULL) { 77 SC_DEBUG(periph, SCSIPI_DB2, 78 ("calling private err_handler()\n")); 79 error = (*periph->periph_switch->psw_error)(xs); 80 if (error != EJUSTRETURN) 81 return (error); 82 } 83 /* 84 * otherwise use the default, call the generic sense handler if we have 85 * more than the sense key 86 */ 87 if (xs->error == XS_SENSE) 88 return (scsipi_interpret_sense(xs)); 89 90 key = (xs->sense.atapi_sense & 0xf0) >> 4; 91 switch (key) { 92 case SKEY_RECOVERED_ERROR: 93 msg = "soft error (corrected)"; 94 case SKEY_NO_SENSE: 95 if (xs->resid == xs->datalen) 96 xs->resid = 0; /* not short read */ 97 error = 0; 98 break; 99 case SKEY_NOT_READY: 100 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0) 101 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 102 if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0) 103 return (0); 104 if ((xs->xs_control & XS_CTL_SILENT) != 0) 105 return (EIO); 106 msg = "not ready"; 107 error = EIO; 108 break; 109 case SKEY_MEDIUM_ERROR: /* MEDIUM ERROR */ 110 msg = "medium error"; 111 error = EIO; 112 break; 113 case SKEY_HARDWARE_ERROR: 114 msg = "non-media hardware failure"; 115 error = EIO; 116 break; 117 case SKEY_ILLEGAL_REQUEST: 118 if ((xs->xs_control & 119 XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0) 120 return (0); 121 if ((xs->xs_control & XS_CTL_SILENT) != 0) 122 return (EIO); 123 msg = "illegal request"; 124 error = EINVAL; 125 break; 126 case SKEY_UNIT_ATTENTION: 127 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0) 128 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 129 if ((xs->xs_control & 130 XS_CTL_IGNORE_MEDIA_CHANGE) != 0 || 131 /* XXX Should reupload any transient state. */ 132 (periph->periph_flags & PERIPH_REMOVABLE) == 0) 133 return (ERESTART); 134 if ((xs->xs_control & XS_CTL_SILENT) != 0) 135 return (EIO); 136 msg = "unit attention"; 137 error = EIO; 138 break; 139 case SKEY_WRITE_PROTECT: 140 msg = "readonly device"; 141 error = EROFS; 142 break; 143 case SKEY_ABORTED_COMMAND: 144 msg = "command aborted"; 145 if (xs->xs_retries != 0) { 146 xs->xs_retries--; 147 error = ERESTART; 148 } else 149 error = EIO; 150 break; 151 default: 152 error = EIO; 153 break; 154 } 155 156 if (!key) { 157 if (xs->sense.atapi_sense & 0x01) { 158 /* Illegal length indication */ 159 msg = "ATA illegal length indication"; 160 error = EIO; 161 } 162 if (xs->sense.atapi_sense & 0x02) { /* vol overflow */ 163 msg = "ATA volume overflow"; 164 error = ENOSPC; 165 } 166 if (xs->sense.atapi_sense & 0x04) { /* Aborted command */ 167 msg = "ATA command aborted"; 168 if (xs->xs_retries != 0) { 169 xs->xs_retries--; 170 error = ERESTART; 171 } else 172 error = EIO; 173 } 174 } 175 if (msg) { 176 scsipi_printaddr(periph); 177 printf("%s\n", msg); 178 } else { 179 if (error) { 180 scsipi_printaddr(periph); 181 printf("unknown error code %d\n", 182 xs->sense.atapi_sense); 183 } 184 } 185 186 return (error); 187 } 188 189 /* 190 * Utility routines often used in SCSI stuff 191 */ 192 193 194 /* 195 * Print out the scsi_link structure's address info. 196 */ 197 void 198 atapi_print_addr(struct scsipi_periph *periph) 199 { 200 struct scsipi_channel *chan = periph->periph_channel; 201 struct scsipi_adapter *adapt = chan->chan_adapter; 202 203 printf("%s(%s:%d:%d): ", periph->periph_dev != NULL ? 204 periph->periph_dev->dv_xname : "probe", 205 adapt->adapt_dev->dv_xname, 206 chan->chan_channel, periph->periph_target); 207 } 208 209 /* 210 * ask the atapi driver to perform a command for us. 211 * tell it where to read/write the data, and how 212 * long the data is supposed to be. If we have a buf 213 * to associate with the transfer, we need that too. 214 */ 215 void 216 atapi_scsipi_cmd(struct scsipi_xfer *xs) 217 { 218 struct scsipi_periph *periph = xs->xs_periph; 219 220 SC_DEBUG(periph, SCSIPI_DB2, ("atapi_cmd\n")); 221 222 xs->cmdlen = (periph->periph_cap & PERIPH_CAP_CMD16) ? 16 : 12; 223 } 224