1 /* $NetBSD: atapi_base.c,v 1.14 1999/09/30 22:57:52 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/buf.h> 44 #include <sys/uio.h> 45 #include <sys/malloc.h> 46 #include <sys/errno.h> 47 #include <sys/device.h> 48 #include <sys/proc.h> 49 50 #include <dev/scsipi/scsipi_all.h> 51 #include <dev/scsipi/atapi_all.h> 52 #include <dev/scsipi/scsipiconf.h> 53 #include <dev/scsipi/atapiconf.h> 54 #include <dev/scsipi/scsipi_base.h> 55 56 /* 57 * Look at the returned sense and act on the error, determining 58 * the unix error number to pass back. (0 = report no error) 59 * 60 * THIS IS THE DEFAULT ERROR HANDLER 61 */ 62 int 63 atapi_interpret_sense(xs) 64 struct scsipi_xfer *xs; 65 { 66 int key, error; 67 struct scsipi_link *sc_link = xs->sc_link; 68 char *msg = NULL; 69 70 /* 71 * If the device has it's own error handler, call it first. 72 * If it returns a legit error value, return that, otherwise 73 * it wants us to continue with normal error processing. 74 */ 75 if (sc_link->device->err_handler) { 76 SC_DEBUG(sc_link, SDEV_DB2, 77 ("calling private err_handler()\n")); 78 error = (*sc_link->device->err_handler) (xs); 79 if (error != SCSIRET_CONTINUE) 80 return (error); /* error >= 0 better ? */ 81 } 82 /* 83 * otherwise use the default, call the generic sense handler if we have 84 * more than the sense key 85 */ 86 if (xs->error == XS_SENSE) 87 return (scsipi_interpret_sense(xs)); 88 89 key = (xs->sense.atapi_sense & 0xf0) >> 4; 90 switch (key) { 91 case SKEY_RECOVERED_ERROR: 92 msg = "soft error (corrected)"; 93 case SKEY_NO_SENSE: 94 if (xs->resid == xs->datalen) 95 xs->resid = 0; /* not short read */ 96 error = 0; 97 break; 98 case SKEY_NOT_READY: 99 if ((sc_link->flags & SDEV_REMOVABLE) != 0) 100 sc_link->flags &= ~SDEV_MEDIA_LOADED; 101 if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0) 102 return (0); 103 if ((xs->xs_control & XS_CTL_SILENT) != 0) 104 return (EIO); 105 msg = "not ready"; 106 error = EIO; 107 break; 108 case SKEY_MEDIUM_ERROR: /* MEDIUM ERROR */ 109 msg = "medium error"; 110 error = EIO; 111 break; 112 case SKEY_HARDWARE_ERROR: 113 msg = "non-media hardware failure"; 114 error = EIO; 115 break; 116 case SKEY_ILLEGAL_REQUEST: 117 if ((xs->xs_control & 118 XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0) 119 return (0); 120 if ((xs->xs_control & XS_CTL_SILENT) != 0) 121 return (EIO); 122 msg = "illegal request"; 123 error = EINVAL; 124 break; 125 case SKEY_UNIT_ATTENTION: 126 if ((sc_link->flags & SDEV_REMOVABLE) != 0) 127 sc_link->flags &= ~SDEV_MEDIA_LOADED; 128 if ((xs->xs_control & 129 XS_CTL_IGNORE_MEDIA_CHANGE) != 0 || 130 /* XXX Should reupload any transient state. */ 131 (sc_link->flags & SDEV_REMOVABLE) == 0) 132 return (ERESTART); 133 if ((xs->xs_control & XS_CTL_SILENT) != 0) 134 return (EIO); 135 msg = "unit attention"; 136 error = EIO; 137 break; 138 case SKEY_WRITE_PROTECT: 139 msg = "readonly device"; 140 error = EROFS; 141 break; 142 case SKEY_ABORTED_COMMAND: 143 msg = "command aborted"; 144 error = ERESTART; 145 break; 146 default: 147 error = EIO; 148 break; 149 } 150 151 if (!key) { 152 if (xs->sense.atapi_sense & 0x01) { 153 /* Illegal length indication */ 154 msg = "ATA illegal length indication"; 155 error = EIO; 156 } 157 if (xs->sense.atapi_sense & 0x02) { /* vol overflow */ 158 msg = "ATA volume overflow"; 159 error = ENOSPC; 160 } 161 if (xs->sense.atapi_sense & 0x04) { /* Aborted command */ 162 msg = "ATA command aborted"; 163 error = ERESTART; 164 } 165 } 166 if (msg) { 167 sc_link->sc_print_addr(sc_link); 168 printf("%s\n", msg); 169 } else { 170 if (error) { 171 sc_link->sc_print_addr(sc_link); 172 printf("unknown error code %d\n", 173 xs->sense.atapi_sense); 174 } 175 } 176 177 return (error); 178 179 } 180 181 /* 182 * Utility routines often used in SCSI stuff 183 */ 184 185 186 /* 187 * Print out the scsi_link structure's address info. 188 */ 189 void 190 atapi_print_addr(sc_link) 191 struct scsipi_link *sc_link; 192 { 193 194 printf("%s(%s:%d:%d): ", 195 sc_link->device_softc ? 196 ((struct device *)sc_link->device_softc)->dv_xname : "probe", 197 ((struct device *)sc_link->adapter_softc)->dv_xname, 198 sc_link->scsipi_atapi.channel, sc_link->scsipi_atapi.drive); 199 } 200 201 /* 202 * ask the atapi driver to perform a command for us. 203 * tell it where to read/write the data, and how 204 * long the data is supposed to be. If we have a buf 205 * to associate with the transfer, we need that too. 206 */ 207 int 208 atapi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen, 209 retries, timeout, bp, flags) 210 struct scsipi_link *sc_link; 211 struct scsipi_generic *scsipi_cmd; 212 int cmdlen; 213 u_char *data_addr; 214 int datalen; 215 int retries; 216 int timeout; 217 struct buf *bp; 218 int flags; 219 { 220 struct scsipi_xfer *xs; 221 int error, s; 222 223 SC_DEBUG(sc_link, SDEV_DB2, ("atapi_cmd\n")); 224 225 #ifdef DIAGNOSTIC 226 if (bp != NULL && (flags & XS_CTL_ASYNC) == 0) 227 panic("atapi_scsipi_cmd: buffer without async"); 228 #endif 229 230 if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr, 231 datalen, retries, timeout, bp, flags)) == NULL) { 232 if (bp != NULL) { 233 s = splbio(); 234 bp->b_flags |= B_ERROR; 235 bp->b_error = ENOMEM; 236 biodone(bp); 237 splx(s); 238 } 239 return (ENOMEM); 240 } 241 242 xs->cmdlen = (sc_link->scsipi_atapi.cap & ACAP_LEN) ? 16 : 12; 243 244 if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN) 245 return (0); 246 247 /* 248 * we have finished with the xfer stuct, free it and 249 * check if anyone else needs to be started up. 250 */ 251 s = splbio(); 252 scsipi_free_xs(xs, flags); 253 splx(s); 254 return (error); 255 } 256 257 int 258 atapi_mode_select(l, data, len, flags, retries, timeout) 259 struct scsipi_link *l; 260 struct atapi_mode_header *data; 261 int len, flags, retries, timeout; 262 { 263 struct atapi_mode_select scsipi_cmd; 264 int error; 265 266 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 267 scsipi_cmd.opcode = ATAPI_MODE_SELECT; 268 scsipi_cmd.byte2 = AMS_PF; 269 _lto2b(len, scsipi_cmd.length); 270 271 /* length is reserved when doing mode select; zero it */ 272 _lto2l(0, data->length); 273 274 error = scsipi_command(l, (struct scsipi_generic *)&scsipi_cmd, 275 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 276 flags | XS_CTL_DATA_OUT); 277 SC_DEBUG(l, SDEV_DB2, ("atapi_mode_select: error=%d\n", error)); 278 return (error); 279 } 280 281 int 282 atapi_mode_sense(l, page, data, len, flags, retries, timeout) 283 struct scsipi_link *l; 284 int page, len, flags, retries, timeout; 285 struct atapi_mode_header *data; 286 { 287 struct atapi_mode_sense scsipi_cmd; 288 int error; 289 290 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 291 scsipi_cmd.opcode = ATAPI_MODE_SENSE; 292 scsipi_cmd.page = page; 293 _lto2b(len, scsipi_cmd.length); 294 295 error = scsipi_command(l, (struct scsipi_generic *)&scsipi_cmd, 296 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 297 flags | XS_CTL_DATA_IN); 298 SC_DEBUG(l, SDEV_DB2, ("atapi_mode_sense: error=%d\n", error)); 299 return (error); 300 } 301