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