1 /* $NetBSD: atapi_base.c,v 1.17 2001/11/13 06:56:38 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 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.17 2001/11/13 06:56:38 lukem Exp $"); 42 43 #include <sys/types.h> 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/buf.h> 48 #include <sys/uio.h> 49 #include <sys/malloc.h> 50 #include <sys/errno.h> 51 #include <sys/device.h> 52 #include <sys/proc.h> 53 54 #include <dev/scsipi/scsipi_all.h> 55 #include <dev/scsipi/scsipiconf.h> 56 #include <dev/scsipi/atapiconf.h> 57 #include <dev/scsipi/scsipi_base.h> 58 59 /* 60 * Look at the returned sense and act on the error, determining 61 * the unix error number to pass back. (0 = report no error) 62 * 63 * THIS IS THE DEFAULT ERROR HANDLER 64 */ 65 int 66 atapi_interpret_sense(xs) 67 struct scsipi_xfer *xs; 68 { 69 struct scsipi_periph *periph = xs->xs_periph; 70 int key, error; 71 char *msg = NULL; 72 73 /* 74 * If the device has it's own error handler, call it first. 75 * If it returns a legit error value, return that, otherwise 76 * it wants us to continue with normal error processing. 77 */ 78 if (periph->periph_switch->psw_error != NULL) { 79 SC_DEBUG(periph, SCSIPI_DB2, 80 ("calling private err_handler()\n")); 81 error = (*periph->periph_switch->psw_error)(xs); 82 if (error != EJUSTRETURN) 83 return (error); 84 } 85 /* 86 * otherwise use the default, call the generic sense handler if we have 87 * more than the sense key 88 */ 89 if (xs->error == XS_SENSE) 90 return (scsipi_interpret_sense(xs)); 91 92 key = (xs->sense.atapi_sense & 0xf0) >> 4; 93 switch (key) { 94 case SKEY_RECOVERED_ERROR: 95 msg = "soft error (corrected)"; 96 case SKEY_NO_SENSE: 97 if (xs->resid == xs->datalen) 98 xs->resid = 0; /* not short read */ 99 error = 0; 100 break; 101 case SKEY_NOT_READY: 102 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0) 103 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 104 if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0) 105 return (0); 106 if ((xs->xs_control & XS_CTL_SILENT) != 0) 107 return (EIO); 108 msg = "not ready"; 109 error = EIO; 110 break; 111 case SKEY_MEDIUM_ERROR: /* MEDIUM ERROR */ 112 msg = "medium error"; 113 error = EIO; 114 break; 115 case SKEY_HARDWARE_ERROR: 116 msg = "non-media hardware failure"; 117 error = EIO; 118 break; 119 case SKEY_ILLEGAL_REQUEST: 120 if ((xs->xs_control & 121 XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0) 122 return (0); 123 if ((xs->xs_control & XS_CTL_SILENT) != 0) 124 return (EIO); 125 msg = "illegal request"; 126 error = EINVAL; 127 break; 128 case SKEY_UNIT_ATTENTION: 129 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0) 130 periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 131 if ((xs->xs_control & 132 XS_CTL_IGNORE_MEDIA_CHANGE) != 0 || 133 /* XXX Should reupload any transient state. */ 134 (periph->periph_flags & PERIPH_REMOVABLE) == 0) 135 return (ERESTART); 136 if ((xs->xs_control & XS_CTL_SILENT) != 0) 137 return (EIO); 138 msg = "unit attention"; 139 error = EIO; 140 break; 141 case SKEY_WRITE_PROTECT: 142 msg = "readonly device"; 143 error = EROFS; 144 break; 145 case SKEY_ABORTED_COMMAND: 146 msg = "command aborted"; 147 error = ERESTART; 148 break; 149 default: 150 error = EIO; 151 break; 152 } 153 154 if (!key) { 155 if (xs->sense.atapi_sense & 0x01) { 156 /* Illegal length indication */ 157 msg = "ATA illegal length indication"; 158 error = EIO; 159 } 160 if (xs->sense.atapi_sense & 0x02) { /* vol overflow */ 161 msg = "ATA volume overflow"; 162 error = ENOSPC; 163 } 164 if (xs->sense.atapi_sense & 0x04) { /* Aborted command */ 165 msg = "ATA command aborted"; 166 error = ERESTART; 167 } 168 } 169 if (msg) { 170 scsipi_printaddr(periph); 171 printf("%s\n", msg); 172 } else { 173 if (error) { 174 scsipi_printaddr(periph); 175 printf("unknown error code %d\n", 176 xs->sense.atapi_sense); 177 } 178 } 179 180 return (error); 181 } 182 183 /* 184 * Utility routines often used in SCSI stuff 185 */ 186 187 188 /* 189 * Print out the scsi_link structure's address info. 190 */ 191 void 192 atapi_print_addr(periph) 193 struct scsipi_periph *periph; 194 { 195 struct scsipi_channel *chan = periph->periph_channel; 196 struct scsipi_adapter *adapt = chan->chan_adapter; 197 198 printf("%s(%s:%d:%d): ", periph->periph_dev != NULL ? 199 periph->periph_dev->dv_xname : "probe", 200 adapt->adapt_dev->dv_xname, 201 chan->chan_channel, periph->periph_target); 202 } 203 204 /* 205 * ask the atapi driver to perform a command for us. 206 * tell it where to read/write the data, and how 207 * long the data is supposed to be. If we have a buf 208 * to associate with the transfer, we need that too. 209 */ 210 int 211 atapi_scsipi_cmd(periph, scsipi_cmd, cmdlen, data, datalen, 212 retries, timeout, bp, flags) 213 struct scsipi_periph *periph; 214 struct scsipi_generic *scsipi_cmd; 215 int cmdlen; 216 void *data; 217 size_t datalen; 218 int retries; 219 int timeout; 220 struct buf *bp; 221 int flags; 222 { 223 struct scsipi_xfer *xs; 224 int error, s; 225 226 SC_DEBUG(periph, SCSIPI_DB2, ("atapi_cmd\n")); 227 228 #ifdef DIAGNOSTIC 229 if (bp != NULL && (flags & XS_CTL_ASYNC) == 0) 230 panic("atapi_scsipi_cmd: buffer without async"); 231 #endif 232 233 if ((xs = scsipi_make_xs(periph, scsipi_cmd, cmdlen, data, 234 datalen, retries, timeout, bp, flags)) == NULL) { 235 if (bp != NULL) { 236 s = splbio(); 237 bp->b_flags |= B_ERROR; 238 bp->b_error = ENOMEM; 239 biodone(bp); 240 splx(s); 241 } 242 return (ENOMEM); 243 } 244 245 xs->cmdlen = (periph->periph_cap & PERIPH_CAP_CMD16) ? 16 : 12; 246 247 if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN) 248 return (0); 249 return (error); 250 } 251