Home | History | Annotate | Line # | Download | only in scsipi
atapi_base.c revision 1.1.2.1
      1 /*	$NetBSD: atapi_base.c,v 1.1.2.1 1997/07/01 16:46:56 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/buf.h>
     41 #include <sys/uio.h>
     42 #include <sys/malloc.h>
     43 #include <sys/errno.h>
     44 #include <sys/device.h>
     45 #include <sys/proc.h>
     46 
     47 #include <dev/scsipi/scsipi_all.h>
     48 #include <dev/scsipi/scsi_all.h>
     49 #include <dev/scsipi/scsipi_disk.h>
     50 #include <dev/scsipi/atapiconf.h>
     51 #include <dev/scsipi/scsipi_base.h>
     52 
     53 /*
     54  * Look at the returned sense and act on the error, determining
     55  * the unix error number to pass back.  (0 = report no error)
     56  *
     57  * THIS IS THE DEFAULT ERROR HANDLER
     58  */
     59 int
     60 atapi_interpret_sense(xs)
     61 	struct scsipi_xfer *xs;
     62 {
     63 	int key, error;
     64 	struct scsipi_link *sc_link = xs->sc_link;
     65 	char *msg = NULL;
     66 
     67 	key = (xs->sense.atapi_sense & 0xf0) >> 4;
     68 	/*
     69 	 * If the device has it's own error handler, call it first.
     70 	 * If it returns a legit error value, return that, otherwise
     71 	 * it wants us to continue with normal error processing.
     72 	 */
     73 	if (sc_link->device->err_handler) {
     74 		SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
     75 		error = (*sc_link->device->err_handler) (xs);
     76 		if (error != -1)
     77 			return error;		/* error >= 0  better ? */
     78 	}
     79 	/* otherwise use the default */
     80 	switch (key) {
     81 		case 0x1: /* RECOVERED ERROR */
     82 			msg = "soft error (corrected)";
     83 		case 0x0: /* NO SENSE */
     84 			if (xs->resid == xs->datalen)
     85 				xs->resid = 0;  /* not short read */
     86 		error = 0;
     87 		break;
     88 		case 0x2:	/* NOT READY */
     89 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
     90 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
     91 			if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
     92 				return 0;
     93 			if ((xs->flags & SCSI_SILENT) != 0)
     94 				return EIO;
     95 			msg = "not ready";
     96 			error = EIO;
     97 			break;
     98 		case 0x03: /* MEDIUM ERROR */
     99 			msg = "medium error";
    100 			error = EIO;
    101 			break;
    102 		case 0x04:
    103 			msg = "non-media hardware failure";
    104 			error = EIO;
    105 			break;
    106 		case 0x5:	/* ILLEGAL REQUEST */
    107 			if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
    108 				return 0;
    109 			if ((xs->flags & SCSI_SILENT) != 0)
    110 				return EIO;
    111 			msg = "illegal request";
    112 			error = EINVAL;
    113 			break;
    114 		case 0x6:	/* UNIT ATTENTION */
    115 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    116 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    117 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    118 			    /* XXX Should reupload any transient state. */
    119 			    (sc_link->flags & SDEV_REMOVABLE) == 0)
    120 				return ERESTART;
    121 			if ((xs->flags & SCSI_SILENT) != 0)
    122 				return EIO;
    123 			msg = "unit attention";
    124 			error = EIO;
    125 			break;
    126 		case 0x7:	/* DATA PROTECT */
    127 			msg = "readonly device";
    128 			error = EACCES;
    129 			break;
    130 		case 0xb:	/* COMMAND ABORTED */
    131 			msg = "command aborted";
    132 			error = ERESTART;
    133 			break;
    134 		default:
    135 			error = EIO;
    136 			break;
    137 	}
    138 
    139 	if (!key) {
    140 		if (xs->sense.atapi_sense & 0x01) { /* Illegal length indication */
    141 			msg = "ATA illegal length indication";
    142 			error = EIO;
    143 		}
    144 		if (xs->sense.atapi_sense & 0x02) { /* vol overflow */
    145 			msg = "ATA volume overflow";
    146 			error = ENOSPC;
    147 		}
    148 		if (xs->sense.atapi_sense & 0x04) { /* Aborted command */
    149 			msg = "ATA command aborted";
    150 			error = ERESTART;
    151 		}
    152 	}
    153 	if (msg) {
    154 		sc_link->sc_print_addr(sc_link);
    155 		printf("%s\n", msg);
    156 	} else {
    157 		if (error) {
    158 			sc_link->sc_print_addr(sc_link);
    159 			printf("unknown error code %d\n", xs->sense.atapi_sense);
    160 		}
    161 	}
    162 
    163 	return error;
    164 
    165 }
    166 
    167 /*
    168  * Utility routines often used in SCSI stuff
    169  */
    170 
    171 
    172 /*
    173  * Print out the scsi_link structure's address info.
    174  */
    175 void
    176 atapi_print_addr(sc_link)
    177 	struct scsipi_link *sc_link;
    178 {
    179 
    180 	printf("%s(%s:%d): ",
    181 	    sc_link->device_softc ?
    182 	    ((struct device *)sc_link->device_softc)->dv_xname : "probe",
    183 	    ((struct device *)sc_link->adapter_softc)->dv_xname,
    184 	    sc_link->scsipi_atapi.drive);
    185 }
    186 
    187 /*
    188  * ask the atapi driver to perform a command for us.
    189  * tell it where to read/write the data, and how
    190  * long the data is supposed to be. If we have  a buf
    191  * to associate with the transfer, we need that too.
    192  */
    193 int
    194 atapi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
    195     retries, timeout, bp, flags)
    196 	struct scsipi_link *sc_link;
    197 	struct scsipi_generic *scsipi_cmd;
    198 	int cmdlen;
    199 	u_char *data_addr;
    200 	int datalen;
    201 	int retries;
    202 	int timeout;
    203 	struct buf *bp;
    204 	int flags;
    205 {
    206 	struct scsipi_xfer *xs;
    207 	int error;
    208 
    209 	SC_DEBUG(sc_link, SDEV_DB2, ("atapi_cmd\n"));
    210 
    211 #ifdef DIAGNOSTIC
    212 	if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
    213 		panic("atapi_scsipi_cmd: buffer without nosleep");
    214 #endif
    215 
    216 	if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
    217 	    retries, timeout, bp, flags)) == NULL)
    218 		return ENOMEM;
    219 
    220 	xs->cmdlen = (sc_link->scsipi_atapi.cap & ACAP_LEN) ? 16 : 12;
    221 
    222 	if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
    223 		return 0;
    224 
    225 	/*
    226 	 * we have finished with the xfer stuct, free it and
    227 	 * check if anyone else needs to be started up.
    228 	 */
    229 	scsipi_free_xs(xs, flags);
    230 	return error;
    231 }
    232