Home | History | Annotate | Line # | Download | only in scsipi
atapi_base.c revision 1.15
      1 /*	$NetBSD: atapi_base.c,v 1.15 2001/04/25 17:53:38 bouyer 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/types.h>
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/buf.h>
     45 #include <sys/uio.h>
     46 #include <sys/malloc.h>
     47 #include <sys/errno.h>
     48 #include <sys/device.h>
     49 #include <sys/proc.h>
     50 
     51 #include <dev/scsipi/scsipi_all.h>
     52 #include <dev/scsipi/atapi_all.h>
     53 #include <dev/scsipi/scsipiconf.h>
     54 #include <dev/scsipi/atapiconf.h>
     55 #include <dev/scsipi/scsipi_base.h>
     56 
     57 /*
     58  * Look at the returned sense and act on the error, determining
     59  * the unix error number to pass back.  (0 = report no error)
     60  *
     61  * THIS IS THE DEFAULT ERROR HANDLER
     62  */
     63 int
     64 atapi_interpret_sense(xs)
     65 	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 			error = ERESTART;
    146 			break;
    147 		default:
    148 			error = EIO;
    149 			break;
    150 	}
    151 
    152 	if (!key) {
    153 		if (xs->sense.atapi_sense & 0x01) {
    154 			/* Illegal length indication */
    155 			msg = "ATA illegal length indication";
    156 			error = EIO;
    157 		}
    158 		if (xs->sense.atapi_sense & 0x02) { /* vol overflow */
    159 			msg = "ATA volume overflow";
    160 			error = ENOSPC;
    161 		}
    162 		if (xs->sense.atapi_sense & 0x04) { /* Aborted command */
    163 			msg = "ATA command aborted";
    164 			error = ERESTART;
    165 		}
    166 	}
    167 	if (msg) {
    168 		scsipi_printaddr(periph);
    169 		printf("%s\n", msg);
    170 	} else {
    171 		if (error) {
    172 			scsipi_printaddr(periph);
    173 			printf("unknown error code %d\n",
    174 			    xs->sense.atapi_sense);
    175 		}
    176 	}
    177 
    178 	return (error);
    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(periph)
    191 	struct scsipi_periph *periph;
    192 {
    193 	struct scsipi_channel *chan = periph->periph_channel;
    194 	struct scsipi_adapter *adapt = chan->chan_adapter;
    195 
    196 	printf("%s(%s:%d:%d): ", periph->periph_dev != NULL ?
    197 	    periph->periph_dev->dv_xname : "probe",
    198 	    adapt->adapt_dev->dv_xname,
    199 	    chan->chan_channel, periph->periph_target);
    200 }
    201 
    202 /*
    203  * ask the atapi driver to perform a command for us.
    204  * tell it where to read/write the data, and how
    205  * long the data is supposed to be. If we have  a buf
    206  * to associate with the transfer, we need that too.
    207  */
    208 int
    209 atapi_scsipi_cmd(periph, scsipi_cmd, cmdlen, data, datalen,
    210     retries, timeout, bp, flags)
    211 	struct scsipi_periph *periph;
    212 	struct scsipi_generic *scsipi_cmd;
    213 	int cmdlen;
    214 	void *data;
    215 	size_t datalen;
    216 	int retries;
    217 	int timeout;
    218 	struct buf *bp;
    219 	int flags;
    220 {
    221 	struct scsipi_xfer *xs;
    222 	int error, s;
    223 
    224 	SC_DEBUG(periph, SCSIPI_DB2, ("atapi_cmd\n"));
    225 
    226 #ifdef DIAGNOSTIC
    227 	if (bp != NULL && (flags & XS_CTL_ASYNC) == 0)
    228 		panic("atapi_scsipi_cmd: buffer without async");
    229 #endif
    230 
    231 	if ((xs = scsipi_make_xs(periph, scsipi_cmd, cmdlen, data,
    232 	    datalen, retries, timeout, bp, flags)) == NULL) {
    233 		if (bp != NULL) {
    234 			s = splbio();
    235 			bp->b_flags |= B_ERROR;
    236 			bp->b_error = ENOMEM;
    237 			biodone(bp);
    238 			splx(s);
    239 		}
    240 		return (ENOMEM);
    241 	}
    242 
    243 	xs->cmdlen = (periph->periph_cap & PERIPH_CAP_CMD16) ? 16 : 12;
    244 
    245 	if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
    246 		return (0);
    247 	return (error);
    248 }
    249 
    250 int
    251 atapi_mode_select(periph, data, len, flags, retries, timeout)
    252 	struct scsipi_periph *periph;
    253 	struct atapi_mode_header *data;
    254 	int len, flags, retries, timeout;
    255 {
    256 	struct atapi_mode_select scsipi_cmd;
    257 	int error;
    258 
    259 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    260 	scsipi_cmd.opcode = ATAPI_MODE_SELECT;
    261 	scsipi_cmd.byte2 = AMS_PF;
    262 	_lto2b(len, scsipi_cmd.length);
    263 
    264 	/* length is reserved when doing mode select; zero it */
    265 	_lto2l(0, data->length);
    266 
    267 	error = scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd,
    268 	    sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL,
    269 	    flags | XS_CTL_DATA_OUT);
    270 	SC_DEBUG(periph, SCSIPI_DB2, ("atapi_mode_select: error=%d\n", error));
    271 	return (error);
    272 }
    273 
    274 int
    275 atapi_mode_sense(periph, page, data, len, flags, retries, timeout)
    276 	struct scsipi_periph *periph;
    277 	int page, len, flags, retries, timeout;
    278 	struct atapi_mode_header *data;
    279 {
    280 	struct atapi_mode_sense scsipi_cmd;
    281 	int error;
    282 
    283 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    284 	scsipi_cmd.opcode = ATAPI_MODE_SENSE;
    285 	scsipi_cmd.page = page;
    286 	_lto2b(len, scsipi_cmd.length);
    287 
    288 	error = scsipi_command(periph, (struct scsipi_generic *)&scsipi_cmd,
    289 	    sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL,
    290 	    flags | XS_CTL_DATA_IN);
    291 	SC_DEBUG(periph, SCSIPI_DB2, ("atapi_mode_sense: error=%d\n", error));
    292 	return (error);
    293 }
    294