1 /* $NetBSD: atapi_base.c,v 1.11 1998/11/17 14:45:39 bouyer 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->flags & SCSI_IGNORE_NOT_READY) != 0) 102 return (0); 103 if ((xs->flags & SCSI_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->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0) 118 return (0); 119 if ((xs->flags & SCSI_SILENT) != 0) 120 return (EIO); 121 msg = "illegal request"; 122 error = EINVAL; 123 break; 124 case SKEY_UNIT_ATTENTION: 125 if ((sc_link->flags & SDEV_REMOVABLE) != 0) 126 sc_link->flags &= ~SDEV_MEDIA_LOADED; 127 if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 || 128 /* XXX Should reupload any transient state. */ 129 (sc_link->flags & SDEV_REMOVABLE) == 0) 130 return (ERESTART); 131 if ((xs->flags & SCSI_SILENT) != 0) 132 return (EIO); 133 msg = "unit attention"; 134 error = EIO; 135 break; 136 case SKEY_WRITE_PROTECT: 137 msg = "readonly device"; 138 error = EROFS; 139 break; 140 case SKEY_ABORTED_COMMAND: 141 msg = "command aborted"; 142 error = ERESTART; 143 break; 144 default: 145 error = EIO; 146 break; 147 } 148 149 if (!key) { 150 if (xs->sense.atapi_sense & 0x01) { 151 /* Illegal length indication */ 152 msg = "ATA illegal length indication"; 153 error = EIO; 154 } 155 if (xs->sense.atapi_sense & 0x02) { /* vol overflow */ 156 msg = "ATA volume overflow"; 157 error = ENOSPC; 158 } 159 if (xs->sense.atapi_sense & 0x04) { /* Aborted command */ 160 msg = "ATA command aborted"; 161 error = ERESTART; 162 } 163 } 164 if (msg) { 165 sc_link->sc_print_addr(sc_link); 166 printf("%s\n", msg); 167 } else { 168 if (error) { 169 sc_link->sc_print_addr(sc_link); 170 printf("unknown error code %d\n", 171 xs->sense.atapi_sense); 172 } 173 } 174 175 return (error); 176 177 } 178 179 /* 180 * Utility routines often used in SCSI stuff 181 */ 182 183 184 /* 185 * Print out the scsi_link structure's address info. 186 */ 187 void 188 atapi_print_addr(sc_link) 189 struct scsipi_link *sc_link; 190 { 191 192 printf("%s(%s:%d:%d): ", 193 sc_link->device_softc ? 194 ((struct device *)sc_link->device_softc)->dv_xname : "probe", 195 ((struct device *)sc_link->adapter_softc)->dv_xname, 196 sc_link->scsipi_atapi.channel, sc_link->scsipi_atapi.drive); 197 } 198 199 /* 200 * ask the atapi driver to perform a command for us. 201 * tell it where to read/write the data, and how 202 * long the data is supposed to be. If we have a buf 203 * to associate with the transfer, we need that too. 204 */ 205 int 206 atapi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen, 207 retries, timeout, bp, flags) 208 struct scsipi_link *sc_link; 209 struct scsipi_generic *scsipi_cmd; 210 int cmdlen; 211 u_char *data_addr; 212 int datalen; 213 int retries; 214 int timeout; 215 struct buf *bp; 216 int flags; 217 { 218 struct scsipi_xfer *xs; 219 int error; 220 221 SC_DEBUG(sc_link, SDEV_DB2, ("atapi_cmd\n")); 222 223 #ifdef DIAGNOSTIC 224 if (bp != 0 && (flags & SCSI_NOSLEEP) == 0) 225 panic("atapi_scsipi_cmd: buffer without nosleep"); 226 #endif 227 228 if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr, 229 datalen, retries, timeout, bp, flags)) == NULL) 230 return (ENOMEM); 231 232 xs->cmdlen = (sc_link->scsipi_atapi.cap & ACAP_LEN) ? 16 : 12; 233 234 if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN) 235 return (0); 236 237 /* 238 * we have finished with the xfer stuct, free it and 239 * check if anyone else needs to be started up. 240 */ 241 scsipi_free_xs(xs, flags); 242 return (error); 243 } 244 245 int 246 atapi_mode_select(l, data, len, flags, retries, timeout) 247 struct scsipi_link *l; 248 struct atapi_mode_header *data; 249 int len, flags, retries, timeout; 250 { 251 struct atapi_mode_select scsipi_cmd; 252 int error; 253 254 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 255 scsipi_cmd.opcode = ATAPI_MODE_SELECT; 256 scsipi_cmd.byte2 = AMS_PF; 257 _lto2b(len, scsipi_cmd.length); 258 259 /* length is reserved when doing mode select; zero it */ 260 _lto2l(0, data->length); 261 262 error = scsipi_command(l, (struct scsipi_generic *)&scsipi_cmd, 263 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 264 flags | SCSI_DATA_OUT); 265 SC_DEBUG(l, SDEV_DB2, ("atapi_mode_select: error=%d\n", error)); 266 return (error); 267 } 268 269 int 270 atapi_mode_sense(l, page, data, len, flags, retries, timeout) 271 struct scsipi_link *l; 272 int page, len, flags, retries, timeout; 273 struct atapi_mode_header *data; 274 { 275 struct atapi_mode_sense scsipi_cmd; 276 int error; 277 278 bzero(&scsipi_cmd, sizeof(scsipi_cmd)); 279 scsipi_cmd.opcode = ATAPI_MODE_SENSE; 280 scsipi_cmd.page = page; 281 _lto2b(len, scsipi_cmd.length); 282 283 error = scsipi_command(l, (struct scsipi_generic *)&scsipi_cmd, 284 sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL, 285 flags | SCSI_DATA_IN); 286 SC_DEBUG(l, SDEV_DB2, ("atapi_mode_sense: error=%d\n", error)); 287 return (error); 288 } 289