Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.16
      1  1.16  thorpej /*	$NetBSD: scsipi_base.c,v 1.16 1998/12/08 00:26:22 thorpej Exp $	*/
      2   1.2   bouyer 
      3   1.8  mycroft /*-
      4   1.8  mycroft  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.8  mycroft  * All rights reserved.
      6   1.8  mycroft  *
      7   1.8  mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8   1.8  mycroft  * by Charles M. Hannum.
      9   1.2   bouyer  *
     10   1.2   bouyer  * Redistribution and use in source and binary forms, with or without
     11   1.2   bouyer  * modification, are permitted provided that the following conditions
     12   1.2   bouyer  * are met:
     13   1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     14   1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     15   1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     17   1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     18   1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     19   1.2   bouyer  *    must display the following acknowledgement:
     20   1.8  mycroft  *        This product includes software developed by the NetBSD
     21   1.8  mycroft  *        Foundation, Inc. and its contributors.
     22   1.8  mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.8  mycroft  *    contributors may be used to endorse or promote products derived
     24   1.8  mycroft  *    from this software without specific prior written permission.
     25   1.2   bouyer  *
     26   1.8  mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.8  mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.8  mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.8  mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.8  mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.8  mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.8  mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.8  mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.8  mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.8  mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.8  mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37   1.2   bouyer  */
     38   1.2   bouyer 
     39  1.13   bouyer #include "opt_scsi.h"
     40  1.13   bouyer 
     41   1.2   bouyer #include <sys/types.h>
     42   1.2   bouyer #include <sys/param.h>
     43   1.2   bouyer #include <sys/systm.h>
     44   1.2   bouyer #include <sys/kernel.h>
     45   1.2   bouyer #include <sys/buf.h>
     46   1.2   bouyer #include <sys/uio.h>
     47   1.2   bouyer #include <sys/malloc.h>
     48   1.6  thorpej #include <sys/pool.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.2   bouyer #include <dev/scsipi/scsipi_disk.h>
     55   1.2   bouyer #include <dev/scsipi/scsipiconf.h>
     56   1.2   bouyer #include <dev/scsipi/scsipi_base.h>
     57   1.2   bouyer 
     58   1.6  thorpej struct pool scsipi_xfer_pool;
     59   1.6  thorpej 
     60   1.3    enami int	sc_err1 __P((struct scsipi_xfer *, int));
     61   1.2   bouyer 
     62   1.2   bouyer /*
     63   1.6  thorpej  * Called when a scsibus is attached to initialize global data.
     64   1.6  thorpej  */
     65   1.6  thorpej void
     66   1.6  thorpej scsipi_init()
     67   1.6  thorpej {
     68   1.6  thorpej 	static int scsipi_init_done;
     69   1.6  thorpej 
     70   1.6  thorpej 	if (scsipi_init_done)
     71   1.6  thorpej 		return;
     72   1.6  thorpej 	scsipi_init_done = 1;
     73   1.6  thorpej 
     74   1.6  thorpej 	/* Initialize the scsipi_xfer pool. */
     75   1.6  thorpej 	pool_init(&scsipi_xfer_pool, sizeof(struct scsipi_xfer), 0,
     76   1.6  thorpej 	    0, 0, "scxspl", 0, NULL, NULL, M_DEVBUF);
     77   1.6  thorpej }
     78   1.6  thorpej 
     79   1.6  thorpej /*
     80   1.2   bouyer  * Get a scsipi transfer structure for the caller. Charge the structure
     81   1.3    enami  * to the device that is referenced by the sc_link structure. If the
     82   1.2   bouyer  * sc_link structure has no 'credits' then the device already has the
     83   1.2   bouyer  * maximum number or outstanding operations under way. In this stage,
     84   1.2   bouyer  * wait on the structure so that when one is freed, we are awoken again
     85   1.2   bouyer  * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
     86   1.2   bouyer  * a NULL pointer, signifying that no slots were available
     87   1.2   bouyer  * Note in the link structure, that we are waiting on it.
     88   1.2   bouyer  */
     89   1.2   bouyer 
     90   1.2   bouyer struct scsipi_xfer *
     91   1.2   bouyer scsipi_get_xs(sc_link, flags)
     92   1.2   bouyer 	struct scsipi_link *sc_link;	/* who to charge the xs to */
     93   1.2   bouyer 	int flags;			/* if this call can sleep */
     94   1.2   bouyer {
     95   1.2   bouyer 	struct scsipi_xfer *xs;
     96   1.2   bouyer 	int s;
     97   1.2   bouyer 
     98   1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_get_xs\n"));
     99   1.6  thorpej 
    100   1.2   bouyer 	s = splbio();
    101   1.2   bouyer 	while (sc_link->openings <= 0) {
    102   1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
    103   1.2   bouyer 		if ((flags & SCSI_NOSLEEP) != 0) {
    104   1.2   bouyer 			splx(s);
    105   1.3    enami 			return (0);
    106   1.2   bouyer 		}
    107   1.2   bouyer 		sc_link->flags |= SDEV_WAITING;
    108   1.3    enami 		(void)tsleep(sc_link, PRIBIO, "getxs", 0);
    109   1.2   bouyer 	}
    110   1.6  thorpej 	SC_DEBUG(sc_link, SDEV_DB3, ("calling pool_get\n"));
    111   1.6  thorpej 	xs = pool_get(&scsipi_xfer_pool,
    112   1.6  thorpej 	    ((flags & SCSI_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    113   1.6  thorpej 	if (xs != NULL)
    114   1.6  thorpej 		sc_link->openings--;
    115   1.6  thorpej 	else {
    116   1.6  thorpej 		(*sc_link->sc_print_addr)(sc_link);
    117   1.6  thorpej 		printf("cannot allocate scsipi xs\n");
    118   1.2   bouyer 	}
    119   1.6  thorpej 	splx(s);
    120   1.2   bouyer 
    121   1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    122   1.6  thorpej 
    123   1.3    enami 	/*
    124   1.7   scottr 	 * zeroes out the command, as ATAPI may use longer commands
    125   1.3    enami 	 * than SCSI
    126   1.3    enami 	 */
    127   1.7   scottr 	if (xs != NULL) {
    128   1.7   scottr 		xs->flags = INUSE | flags;
    129  1.15  thorpej 		TAILQ_INSERT_TAIL(&sc_link->pending_xfers, xs, device_q);
    130   1.7   scottr 		bzero(&xs->cmdstore, sizeof(xs->cmdstore));
    131   1.7   scottr 	}
    132   1.3    enami 	return (xs);
    133   1.2   bouyer }
    134   1.2   bouyer 
    135   1.2   bouyer /*
    136   1.2   bouyer  * Given a scsipi_xfer struct, and a device (referenced through sc_link)
    137   1.2   bouyer  * return the struct to the free pool and credit the device with it
    138   1.2   bouyer  * If another process is waiting for an xs, do a wakeup, let it proceed
    139   1.6  thorpej  *
    140   1.6  thorpej  * MUST BE CALLED AT splbio()!!
    141   1.2   bouyer  */
    142   1.3    enami void
    143   1.2   bouyer scsipi_free_xs(xs, flags)
    144   1.2   bouyer 	struct scsipi_xfer *xs;
    145   1.2   bouyer 	int flags;
    146   1.2   bouyer {
    147   1.2   bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    148   1.2   bouyer 
    149  1.15  thorpej 	TAILQ_REMOVE(&sc_link->pending_xfers, xs, device_q);
    150  1.15  thorpej 	if (TAILQ_FIRST(&sc_link->pending_xfers) == NULL &&
    151  1.16  thorpej 	    (sc_link->flags & SDEV_WAITDRAIN) != 0) {
    152  1.16  thorpej 		sc_link->flags &= ~SDEV_WAITDRAIN;
    153  1.15  thorpej 		wakeup(&sc_link->pending_xfers);
    154  1.16  thorpej 	}
    155   1.2   bouyer 	xs->flags &= ~INUSE;
    156   1.6  thorpej 	pool_put(&scsipi_xfer_pool, xs);
    157   1.2   bouyer 
    158   1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
    159   1.2   bouyer 	/* if was 0 and someone waits, wake them up */
    160   1.2   bouyer 	sc_link->openings++;
    161   1.2   bouyer 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    162   1.2   bouyer 		sc_link->flags &= ~SDEV_WAITING;
    163   1.2   bouyer 		wakeup(sc_link);
    164   1.2   bouyer 	} else {
    165   1.2   bouyer 		if (sc_link->device->start) {
    166   1.3    enami 			SC_DEBUG(sc_link, SDEV_DB2,
    167   1.3    enami 			    ("calling private start()\n"));
    168   1.3    enami 			(*(sc_link->device->start))(sc_link->device_softc);
    169   1.2   bouyer 		}
    170   1.2   bouyer 	}
    171  1.15  thorpej }
    172  1.15  thorpej 
    173  1.15  thorpej /*
    174  1.15  thorpej  * Wait for a scsipi_link's pending xfers to drain.
    175  1.15  thorpej  */
    176  1.15  thorpej void
    177  1.15  thorpej scsipi_wait_drain(sc_link)
    178  1.15  thorpej 	struct scsipi_link *sc_link;
    179  1.15  thorpej {
    180  1.15  thorpej 	int s;
    181  1.15  thorpej 
    182  1.15  thorpej 	s = splbio();
    183  1.16  thorpej 	while (TAILQ_FIRST(&sc_link->pending_xfers) != NULL) {
    184  1.15  thorpej 		sc_link->flags |= SDEV_WAITDRAIN;
    185  1.15  thorpej 		(void) tsleep(&sc_link->pending_xfers, PRIBIO, "sxdrn", 0);
    186  1.15  thorpej 	}
    187  1.15  thorpej 	splx(s);
    188   1.2   bouyer }
    189   1.2   bouyer 
    190   1.2   bouyer /*
    191  1.13   bouyer  * Look at the returned sense and act on the error, determining
    192  1.13   bouyer  * the unix error number to pass back.  (0 = report no error)
    193  1.13   bouyer  *
    194  1.13   bouyer  * THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES
    195  1.13   bouyer  */
    196  1.13   bouyer int
    197  1.13   bouyer scsipi_interpret_sense(xs)
    198  1.13   bouyer 	struct scsipi_xfer *xs;
    199  1.13   bouyer {
    200  1.13   bouyer 	struct scsipi_sense_data *sense;
    201  1.13   bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    202  1.13   bouyer 	u_int8_t key;
    203  1.13   bouyer 	u_int32_t info;
    204  1.13   bouyer 	int error;
    205  1.13   bouyer #ifndef	SCSIVERBOSE
    206  1.13   bouyer 	static char *error_mes[] = {
    207  1.13   bouyer 		"soft error (corrected)",
    208  1.13   bouyer 		"not ready", "medium error",
    209  1.13   bouyer 		"non-media hardware failure", "illegal request",
    210  1.13   bouyer 		"unit attention", "readonly device",
    211  1.13   bouyer 		"no data found", "vendor unique",
    212  1.13   bouyer 		"copy aborted", "command aborted",
    213  1.13   bouyer 		"search returned equal", "volume overflow",
    214  1.13   bouyer 		"verify miscompare", "unknown error key"
    215  1.13   bouyer 	};
    216  1.13   bouyer #endif
    217  1.13   bouyer 
    218  1.13   bouyer 	sense = &xs->sense.scsi_sense;
    219  1.13   bouyer #ifdef	SCSIDEBUG
    220  1.13   bouyer 	if ((sc_link->flags & SDEV_DB1) != 0) {
    221  1.13   bouyer 		int count;
    222  1.13   bouyer 		printf("code 0x%x valid 0x%x ",
    223  1.13   bouyer 			sense->error_code & SSD_ERRCODE,
    224  1.13   bouyer 			sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    225  1.13   bouyer 		printf("seg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
    226  1.13   bouyer 			sense->segment,
    227  1.13   bouyer 			sense->flags & SSD_KEY,
    228  1.13   bouyer 			sense->flags & SSD_ILI ? 1 : 0,
    229  1.13   bouyer 			sense->flags & SSD_EOM ? 1 : 0,
    230  1.13   bouyer 			sense->flags & SSD_FILEMARK ? 1 : 0);
    231  1.13   bouyer 		printf("info: 0x%x 0x%x 0x%x 0x%x followed by %d extra bytes\n",
    232  1.13   bouyer 			sense->info[0],
    233  1.13   bouyer 			sense->info[1],
    234  1.13   bouyer 			sense->info[2],
    235  1.13   bouyer 			sense->info[3],
    236  1.13   bouyer 			sense->extra_len);
    237  1.13   bouyer 		printf("extra: ");
    238  1.13   bouyer 		for (count = 0; count < ADD_BYTES_LIM(sense); count++)
    239  1.13   bouyer 			printf("0x%x ", sense->cmd_spec_info[count]);
    240  1.13   bouyer 		printf("\n");
    241  1.13   bouyer 	}
    242  1.13   bouyer #endif	/* SCSIDEBUG */
    243  1.13   bouyer 	/*
    244  1.13   bouyer 	 * If the device has it's own error handler, call it first.
    245  1.13   bouyer 	 * If it returns a legit error value, return that, otherwise
    246  1.13   bouyer 	 * it wants us to continue with normal error processing.
    247  1.13   bouyer 	 */
    248  1.13   bouyer 	if (sc_link->device->err_handler) {
    249  1.13   bouyer 		SC_DEBUG(sc_link, SDEV_DB2,
    250  1.13   bouyer 		    ("calling private err_handler()\n"));
    251  1.13   bouyer 		error = (*sc_link->device->err_handler)(xs);
    252  1.13   bouyer 		if (error != SCSIRET_CONTINUE)
    253  1.13   bouyer 			return (error);		/* error >= 0  better ? */
    254  1.13   bouyer 	}
    255  1.13   bouyer 	/* otherwise use the default */
    256  1.13   bouyer 	switch (sense->error_code & SSD_ERRCODE) {
    257  1.13   bouyer 		/*
    258  1.13   bouyer 		 * If it's code 70, use the extended stuff and
    259  1.13   bouyer 		 * interpret the key
    260  1.13   bouyer 		 */
    261  1.13   bouyer 	case 0x71:		/* delayed error */
    262  1.13   bouyer 		sc_link->sc_print_addr(sc_link);
    263  1.13   bouyer 		key = sense->flags & SSD_KEY;
    264  1.13   bouyer 		printf(" DEFERRED ERROR, key = 0x%x\n", key);
    265  1.13   bouyer 		/* FALLTHROUGH */
    266  1.13   bouyer 	case 0x70:
    267  1.13   bouyer 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
    268  1.13   bouyer 			info = _4btol(sense->info);
    269  1.13   bouyer 		else
    270  1.13   bouyer 			info = 0;
    271  1.13   bouyer 		key = sense->flags & SSD_KEY;
    272  1.13   bouyer 
    273  1.13   bouyer 		switch (key) {
    274  1.13   bouyer 		case SKEY_NO_SENSE:
    275  1.13   bouyer 		case SKEY_RECOVERED_ERROR:
    276  1.13   bouyer 			if (xs->resid == xs->datalen && xs->datalen) {
    277  1.13   bouyer 				/*
    278  1.13   bouyer 				 * Why is this here?
    279  1.13   bouyer 				 */
    280  1.13   bouyer 				xs->resid = 0;	/* not short read */
    281  1.13   bouyer 			}
    282  1.13   bouyer 		case SKEY_EQUAL:
    283  1.13   bouyer 			error = 0;
    284  1.13   bouyer 			break;
    285  1.13   bouyer 		case SKEY_NOT_READY:
    286  1.13   bouyer 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    287  1.13   bouyer 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    288  1.13   bouyer 			if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
    289  1.13   bouyer 				return (0);
    290  1.13   bouyer 			if ((xs->flags & SCSI_SILENT) != 0)
    291  1.13   bouyer 				return (EIO);
    292  1.13   bouyer 			error = EIO;
    293  1.13   bouyer 			break;
    294  1.13   bouyer 		case SKEY_ILLEGAL_REQUEST:
    295  1.13   bouyer 			if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
    296  1.13   bouyer 				return (0);
    297  1.13   bouyer 			if ((xs->flags & SCSI_SILENT) != 0)
    298  1.13   bouyer 				return (EIO);
    299  1.13   bouyer 			error = EINVAL;
    300  1.13   bouyer 			break;
    301  1.13   bouyer 		case SKEY_UNIT_ATTENTION:
    302  1.13   bouyer 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    303  1.13   bouyer 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    304  1.13   bouyer 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    305  1.13   bouyer 				/* XXX Should reupload any transient state. */
    306  1.13   bouyer 				(sc_link->flags & SDEV_REMOVABLE) == 0)
    307  1.13   bouyer 				return (ERESTART);
    308  1.13   bouyer 			if ((xs->flags & SCSI_SILENT) != 0)
    309  1.13   bouyer 				return (EIO);
    310  1.13   bouyer 			error = EIO;
    311  1.13   bouyer 			break;
    312  1.13   bouyer 		case SKEY_WRITE_PROTECT:
    313  1.13   bouyer 			error = EROFS;
    314  1.13   bouyer 			break;
    315  1.13   bouyer 		case SKEY_BLANK_CHECK:
    316  1.13   bouyer 			error = 0;
    317  1.13   bouyer 			break;
    318  1.13   bouyer 		case SKEY_ABORTED_COMMAND:
    319  1.13   bouyer 			error = ERESTART;
    320  1.13   bouyer 			break;
    321  1.13   bouyer 		case SKEY_VOLUME_OVERFLOW:
    322  1.13   bouyer 			error = ENOSPC;
    323  1.13   bouyer 			break;
    324  1.13   bouyer 		default:
    325  1.13   bouyer 			error = EIO;
    326  1.13   bouyer 			break;
    327  1.13   bouyer 		}
    328  1.13   bouyer 
    329  1.13   bouyer #ifdef SCSIVERBOSE
    330  1.13   bouyer 		if ((xs->flags & SCSI_SILENT) == 0)
    331  1.13   bouyer 			scsipi_print_sense(xs, 0);
    332  1.13   bouyer #else
    333  1.13   bouyer 		if (key) {
    334  1.13   bouyer 			sc_link->sc_print_addr(sc_link);
    335  1.13   bouyer 			printf("%s", error_mes[key - 1]);
    336  1.13   bouyer 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    337  1.13   bouyer 				switch (key) {
    338  1.13   bouyer 				case SKEY_NOT_READY:
    339  1.13   bouyer 				case SKEY_ILLEGAL_REQUEST:
    340  1.13   bouyer 				case SKEY_UNIT_ATTENTION:
    341  1.13   bouyer 				case SKEY_WRITE_PROTECT:
    342  1.13   bouyer 					break;
    343  1.13   bouyer 				case SKEY_BLANK_CHECK:
    344  1.13   bouyer 					printf(", requested size: %d (decimal)",
    345  1.13   bouyer 					    info);
    346  1.13   bouyer 					break;
    347  1.13   bouyer 				case SKEY_ABORTED_COMMAND:
    348  1.13   bouyer 					if (xs->retries)
    349  1.13   bouyer 						printf(", retrying");
    350  1.13   bouyer 					printf(", cmd 0x%x, info 0x%x",
    351  1.13   bouyer 					    xs->cmd->opcode, info);
    352  1.13   bouyer 					break;
    353  1.13   bouyer 				default:
    354  1.13   bouyer 					printf(", info = %d (decimal)", info);
    355  1.13   bouyer 				}
    356  1.13   bouyer 			}
    357  1.13   bouyer 			if (sense->extra_len != 0) {
    358  1.13   bouyer 				int n;
    359  1.13   bouyer 				printf(", data =");
    360  1.13   bouyer 				for (n = 0; n < sense->extra_len; n++)
    361  1.13   bouyer 					printf(" %02x",
    362  1.13   bouyer 					    sense->cmd_spec_info[n]);
    363  1.13   bouyer 			}
    364  1.13   bouyer 			printf("\n");
    365  1.13   bouyer 		}
    366  1.13   bouyer #endif
    367  1.13   bouyer 		return (error);
    368  1.13   bouyer 
    369  1.13   bouyer 	/*
    370  1.13   bouyer 	 * Not code 70, just report it
    371  1.13   bouyer 	 */
    372  1.13   bouyer 	default:
    373  1.13   bouyer 		sc_link->sc_print_addr(sc_link);
    374  1.13   bouyer 		printf("error code %d", sense->error_code & SSD_ERRCODE);
    375  1.13   bouyer 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    376  1.13   bouyer 			struct scsipi_sense_data_unextended *usense =
    377  1.13   bouyer 			    (struct scsipi_sense_data_unextended *)sense;
    378  1.13   bouyer 			printf(" at block no. %d (decimal)",
    379  1.13   bouyer 			    _3btol(usense->block));
    380  1.13   bouyer 		}
    381  1.13   bouyer 		printf("\n");
    382  1.13   bouyer 		return (EIO);
    383  1.13   bouyer 	}
    384  1.13   bouyer }
    385  1.13   bouyer 
    386  1.13   bouyer /*
    387   1.2   bouyer  * Find out from the device what its capacity is.
    388   1.2   bouyer  */
    389   1.2   bouyer u_long
    390   1.2   bouyer scsipi_size(sc_link, flags)
    391   1.2   bouyer 	struct scsipi_link *sc_link;
    392   1.2   bouyer 	int flags;
    393   1.2   bouyer {
    394   1.2   bouyer 	struct scsipi_read_cap_data rdcap;
    395   1.2   bouyer 	struct scsipi_read_capacity scsipi_cmd;
    396   1.2   bouyer 
    397   1.2   bouyer 	/*
    398   1.2   bouyer 	 * make up a scsipi command and ask the scsipi driver to do
    399   1.2   bouyer 	 * it for you.
    400   1.2   bouyer 	 */
    401   1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    402   1.2   bouyer 	scsipi_cmd.opcode = READ_CAPACITY;
    403   1.2   bouyer 
    404   1.2   bouyer 	/*
    405   1.2   bouyer 	 * If the command works, interpret the result as a 4 byte
    406   1.2   bouyer 	 * number of blocks
    407   1.2   bouyer 	 */
    408   1.4  thorpej 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    409   1.3    enami 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    410   1.3    enami 	    2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    411   1.2   bouyer 		sc_link->sc_print_addr(sc_link);
    412   1.2   bouyer 		printf("could not get size\n");
    413   1.3    enami 		return (0);
    414   1.2   bouyer 	}
    415   1.2   bouyer 
    416   1.3    enami 	return (_4btol(rdcap.addr) + 1);
    417   1.2   bouyer }
    418   1.2   bouyer 
    419   1.2   bouyer /*
    420   1.2   bouyer  * Get scsipi driver to send a "are you ready?" command
    421   1.2   bouyer  */
    422   1.3    enami int
    423   1.2   bouyer scsipi_test_unit_ready(sc_link, flags)
    424   1.2   bouyer 	struct scsipi_link *sc_link;
    425   1.2   bouyer 	int flags;
    426   1.2   bouyer {
    427   1.2   bouyer 	struct scsipi_test_unit_ready scsipi_cmd;
    428   1.2   bouyer 
    429   1.2   bouyer 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    430   1.2   bouyer 	if (sc_link->quirks & ADEV_NOTUR)
    431   1.3    enami 		return (0);
    432   1.2   bouyer 
    433   1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    434   1.2   bouyer 	scsipi_cmd.opcode = TEST_UNIT_READY;
    435   1.2   bouyer 
    436   1.4  thorpej 	return (scsipi_command(sc_link,
    437   1.3    enami 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    438   1.3    enami 	    0, 0, 2, 10000, NULL, flags));
    439   1.2   bouyer }
    440   1.2   bouyer 
    441   1.2   bouyer /*
    442   1.2   bouyer  * Do a scsipi operation asking a device what it is
    443   1.2   bouyer  * Use the scsipi_cmd routine in the switch table.
    444   1.2   bouyer  * XXX actually this is only used for scsi devices, because I have the feeling
    445   1.2   bouyer  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    446   1.2   bouyer  * in the atapi specs.
    447   1.2   bouyer  */
    448   1.3    enami int
    449   1.2   bouyer scsipi_inquire(sc_link, inqbuf, flags)
    450   1.2   bouyer 	struct scsipi_link *sc_link;
    451   1.2   bouyer 	struct scsipi_inquiry_data *inqbuf;
    452   1.2   bouyer 	int flags;
    453   1.2   bouyer {
    454   1.2   bouyer 	struct scsipi_inquiry scsipi_cmd;
    455   1.2   bouyer 
    456   1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    457   1.2   bouyer 	scsipi_cmd.opcode = INQUIRY;
    458   1.2   bouyer 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    459   1.2   bouyer 
    460   1.4  thorpej 	return (scsipi_command(sc_link,
    461   1.3    enami 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    462   1.3    enami 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    463   1.3    enami 	    2, 10000, NULL, SCSI_DATA_IN | flags));
    464   1.2   bouyer }
    465   1.2   bouyer 
    466   1.2   bouyer /*
    467   1.2   bouyer  * Prevent or allow the user to remove the media
    468   1.2   bouyer  */
    469   1.3    enami int
    470   1.2   bouyer scsipi_prevent(sc_link, type, flags)
    471   1.2   bouyer 	struct scsipi_link *sc_link;
    472   1.2   bouyer 	int type, flags;
    473   1.2   bouyer {
    474   1.2   bouyer 	struct scsipi_prevent scsipi_cmd;
    475   1.2   bouyer 
    476   1.2   bouyer 	if (sc_link->quirks & ADEV_NODOORLOCK)
    477   1.3    enami 		return (0);
    478   1.2   bouyer 
    479   1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    480   1.2   bouyer 	scsipi_cmd.opcode = PREVENT_ALLOW;
    481   1.2   bouyer 	scsipi_cmd.how = type;
    482   1.4  thorpej 	return (scsipi_command(sc_link,
    483   1.3    enami 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    484   1.3    enami 	    0, 0, 2, 5000, NULL, flags));
    485   1.2   bouyer }
    486   1.2   bouyer 
    487   1.2   bouyer /*
    488   1.2   bouyer  * Get scsipi driver to send a "start up" command
    489   1.2   bouyer  */
    490   1.3    enami int
    491   1.2   bouyer scsipi_start(sc_link, type, flags)
    492   1.2   bouyer 	struct scsipi_link *sc_link;
    493   1.2   bouyer 	int type, flags;
    494   1.2   bouyer {
    495   1.2   bouyer 	struct scsipi_start_stop scsipi_cmd;
    496   1.2   bouyer 
    497   1.2   bouyer 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    498   1.2   bouyer 	scsipi_cmd.opcode = START_STOP;
    499   1.2   bouyer 	scsipi_cmd.byte2 = 0x00;
    500   1.2   bouyer 	scsipi_cmd.how = type;
    501   1.4  thorpej 	return (scsipi_command(sc_link,
    502   1.3    enami 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    503   1.3    enami 	    0, 0, 2, type == SSS_START ? 30000 : 10000, NULL, flags));
    504   1.2   bouyer }
    505   1.2   bouyer 
    506   1.2   bouyer /*
    507   1.3    enami  * This routine is called by the scsipi interrupt when the transfer is
    508   1.3    enami  * complete.
    509   1.2   bouyer  */
    510   1.3    enami void
    511   1.2   bouyer scsipi_done(xs)
    512   1.2   bouyer 	struct scsipi_xfer *xs;
    513   1.2   bouyer {
    514   1.2   bouyer 	struct scsipi_link *sc_link = xs->sc_link;
    515   1.2   bouyer 	struct buf *bp;
    516   1.2   bouyer 	int error;
    517   1.2   bouyer 
    518   1.2   bouyer 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    519   1.2   bouyer #ifdef	SCSIDEBUG
    520   1.2   bouyer 	if ((sc_link->flags & SDEV_DB1) != 0)
    521   1.2   bouyer 		show_scsipi_cmd(xs);
    522   1.2   bouyer #endif /* SCSIDEBUG */
    523   1.2   bouyer 
    524   1.2   bouyer 	/*
    525   1.3    enami 	 * If it's a user level request, bypass all usual completion
    526   1.3    enami 	 * processing, let the user work it out..  We take
    527   1.3    enami 	 * reponsibility for freeing the xs when the user returns.
    528   1.3    enami 	 * (and restarting the device's queue).
    529   1.3    enami 	 */
    530   1.2   bouyer 	if ((xs->flags & SCSI_USER) != 0) {
    531   1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    532   1.2   bouyer 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    533   1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    534   1.2   bouyer 
    535   1.9   scottr 		/*
    536   1.9   scottr 		 * If this was an asynchronous operation (i.e. adapter
    537   1.9   scottr 		 * returned SUCCESSFULLY_QUEUED when the command was
    538   1.9   scottr 		 * submitted), we need to free the scsipi_xfer here.
    539   1.9   scottr 		 */
    540  1.11   scottr 		if (SCSIPI_XFER_ASYNC(xs))
    541   1.9   scottr 			scsipi_free_xs(xs, SCSI_NOSLEEP);
    542   1.2   bouyer 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    543   1.2   bouyer 		return;
    544   1.2   bouyer 	}
    545   1.2   bouyer 
    546  1.11   scottr 	if (!SCSIPI_XFER_ASYNC(xs)) {
    547   1.2   bouyer 		/*
    548   1.2   bouyer 		 * if it's a normal upper level request, then ask
    549   1.2   bouyer 		 * the upper level code to handle error checking
    550   1.2   bouyer 		 * rather than doing it here at interrupt time
    551   1.2   bouyer 		 */
    552   1.2   bouyer 		wakeup(xs);
    553   1.2   bouyer 		return;
    554   1.2   bouyer 	}
    555   1.2   bouyer 
    556   1.2   bouyer 	/*
    557   1.2   bouyer 	 * Go and handle errors now.
    558   1.2   bouyer 	 * If it returns ERESTART then we should RETRY
    559   1.2   bouyer 	 */
    560   1.2   bouyer retry:
    561   1.2   bouyer 	error = sc_err1(xs, 1);
    562   1.3    enami 	if (error == ERESTART)
    563   1.4  thorpej 		switch (scsipi_command_direct(xs)) {
    564   1.2   bouyer 		case SUCCESSFULLY_QUEUED:
    565   1.2   bouyer 			return;
    566   1.2   bouyer 
    567   1.2   bouyer 		case TRY_AGAIN_LATER:
    568   1.2   bouyer 			xs->error = XS_BUSY;
    569   1.2   bouyer 		case COMPLETE:
    570   1.2   bouyer 			goto retry;
    571   1.2   bouyer 		}
    572   1.2   bouyer 
    573   1.2   bouyer 	bp = xs->bp;
    574   1.2   bouyer 	if (bp) {
    575   1.2   bouyer 		if (error) {
    576   1.2   bouyer 			bp->b_error = error;
    577   1.2   bouyer 			bp->b_flags |= B_ERROR;
    578   1.2   bouyer 			bp->b_resid = bp->b_bcount;
    579   1.2   bouyer 		} else {
    580   1.2   bouyer 			bp->b_error = 0;
    581   1.2   bouyer 			bp->b_resid = xs->resid;
    582   1.2   bouyer 		}
    583   1.2   bouyer 	}
    584   1.2   bouyer 	if (sc_link->device->done) {
    585   1.2   bouyer 		/*
    586   1.2   bouyer 		 * Tell the device the operation is actually complete.
    587   1.2   bouyer 		 * No more will happen with this xfer.  This for
    588   1.2   bouyer 		 * notification of the upper-level driver only; they
    589   1.2   bouyer 		 * won't be returning any meaningful information to us.
    590   1.2   bouyer 		 */
    591   1.2   bouyer 		(*sc_link->device->done)(xs);
    592   1.2   bouyer 	}
    593   1.9   scottr 	/*
    594   1.9   scottr 	 * If this was an asynchronous operation (i.e. adapter
    595   1.9   scottr 	 * returned SUCCESSFULLY_QUEUED when the command was
    596   1.9   scottr 	 * submitted), we need to free the scsipi_xfer here.
    597   1.9   scottr 	 */
    598  1.11   scottr 	if (SCSIPI_XFER_ASYNC(xs))
    599   1.9   scottr 		scsipi_free_xs(xs, SCSI_NOSLEEP);
    600   1.2   bouyer 	if (bp)
    601   1.2   bouyer 		biodone(bp);
    602   1.2   bouyer }
    603   1.2   bouyer 
    604   1.2   bouyer int
    605   1.2   bouyer scsipi_execute_xs(xs)
    606   1.2   bouyer 	struct scsipi_xfer *xs;
    607   1.2   bouyer {
    608  1.11   scottr 	int async;
    609   1.2   bouyer 	int error;
    610   1.2   bouyer 	int s;
    611   1.2   bouyer 
    612  1.11   scottr 	xs->flags &= ~ITSDONE;
    613   1.2   bouyer 	xs->error = XS_NOERROR;
    614   1.2   bouyer 	xs->resid = xs->datalen;
    615   1.5  thorpej 	xs->status = 0;
    616   1.2   bouyer 
    617   1.2   bouyer retry:
    618   1.2   bouyer 	/*
    619   1.2   bouyer 	 * Do the transfer. If we are polling we will return:
    620   1.2   bouyer 	 * COMPLETE,  Was poll, and scsipi_done has been called
    621   1.2   bouyer 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    622   1.3    enami 	 *
    623   1.2   bouyer 	 * if under full steam (interrupts) it will return:
    624   1.2   bouyer 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    625   1.2   bouyer 	 * TRY_AGAIN_LATER, (as for polling)
    626   1.2   bouyer 	 * After the wakeup, we must still check if it succeeded
    627   1.3    enami 	 *
    628   1.2   bouyer 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    629   1.2   bouyer 	 * we just return.  All the error proccessing and the buffer
    630   1.2   bouyer 	 * code both expect us to return straight to them, so as soon
    631   1.2   bouyer 	 * as the command is queued, return.
    632   1.2   bouyer 	 */
    633   1.2   bouyer #ifdef SCSIDEBUG
    634   1.2   bouyer 	if (xs->sc_link->flags & SDEV_DB3) {
    635   1.2   bouyer 		printf("scsipi_exec_cmd: ");
    636   1.2   bouyer 		show_scsipi_xs(xs);
    637   1.2   bouyer 		printf("\n");
    638   1.2   bouyer 	}
    639   1.2   bouyer #endif
    640  1.11   scottr 	async = SCSIPI_XFER_ASYNC(xs);
    641   1.4  thorpej 	switch (scsipi_command_direct(xs)) {
    642   1.2   bouyer 	case SUCCESSFULLY_QUEUED:
    643  1.11   scottr 		if (async) {
    644  1.11   scottr 			/* scsipi_done() will free the scsipi_xfer. */
    645   1.3    enami 			return (EJUSTRETURN);
    646   1.9   scottr 		}
    647   1.2   bouyer #ifdef DIAGNOSTIC
    648   1.2   bouyer 		if (xs->flags & SCSI_NOSLEEP)
    649   1.2   bouyer 			panic("scsipi_execute_xs: NOSLEEP and POLL");
    650   1.2   bouyer #endif
    651  1.11   scottr 		s = splbio();
    652   1.2   bouyer 		while ((xs->flags & ITSDONE) == 0)
    653   1.2   bouyer 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    654   1.2   bouyer 		splx(s);
    655   1.2   bouyer 	case COMPLETE:		/* Polling command completed ok */
    656   1.2   bouyer 		if (xs->bp)
    657   1.9   scottr 			return (0);
    658   1.2   bouyer 	doit:
    659   1.2   bouyer 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    660   1.2   bouyer 		if ((error = sc_err1(xs, 0)) != ERESTART)
    661   1.3    enami 			return (error);
    662   1.2   bouyer 		goto retry;
    663   1.2   bouyer 
    664   1.2   bouyer 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    665   1.2   bouyer 		xs->error = XS_BUSY;
    666   1.2   bouyer 		goto doit;
    667   1.2   bouyer 
    668   1.2   bouyer 	default:
    669   1.2   bouyer 		panic("scsipi_execute_xs: invalid return code");
    670   1.2   bouyer 	}
    671   1.2   bouyer 
    672   1.2   bouyer #ifdef DIAGNOSTIC
    673   1.2   bouyer 	panic("scsipi_execute_xs: impossible");
    674   1.2   bouyer #endif
    675   1.3    enami 	return (EINVAL);
    676   1.2   bouyer }
    677   1.2   bouyer 
    678   1.3    enami int
    679   1.2   bouyer sc_err1(xs, async)
    680   1.2   bouyer 	struct scsipi_xfer *xs;
    681   1.2   bouyer 	int async;
    682   1.2   bouyer {
    683   1.2   bouyer 	int error;
    684   1.2   bouyer 
    685   1.2   bouyer 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    686   1.2   bouyer 
    687   1.2   bouyer 	/*
    688   1.2   bouyer 	 * If it has a buf, we might be working with
    689   1.2   bouyer 	 * a request from the buffer cache or some other
    690   1.2   bouyer 	 * piece of code that requires us to process
    691   1.2   bouyer 	 * errors at inetrrupt time. We have probably
    692   1.2   bouyer 	 * been called by scsipi_done()
    693   1.2   bouyer 	 */
    694   1.2   bouyer 	switch (xs->error) {
    695   1.2   bouyer 	case XS_NOERROR:	/* nearly always hit this one */
    696   1.2   bouyer 		error = 0;
    697   1.2   bouyer 		break;
    698   1.2   bouyer 
    699   1.2   bouyer 	case XS_SENSE:
    700  1.13   bouyer 	case XS_SHORTSENSE:
    701   1.3    enami 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    702   1.3    enami 		    ERESTART)
    703   1.2   bouyer 			goto retry;
    704   1.2   bouyer 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    705   1.2   bouyer 		    ("scsipi_interpret_sense returned %d\n", error));
    706   1.2   bouyer 		break;
    707   1.2   bouyer 
    708   1.2   bouyer 	case XS_BUSY:
    709   1.2   bouyer 		if (xs->retries) {
    710   1.2   bouyer 			if ((xs->flags & SCSI_POLL) != 0)
    711   1.2   bouyer 				delay(1000000);
    712   1.2   bouyer 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    713   1.2   bouyer 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    714   1.2   bouyer 			else
    715   1.2   bouyer #if 0
    716   1.2   bouyer 				timeout(scsipi_requeue, xs, hz);
    717   1.2   bouyer #else
    718   1.2   bouyer 				goto lose;
    719   1.2   bouyer #endif
    720   1.2   bouyer 		}
    721   1.2   bouyer 	case XS_TIMEOUT:
    722   1.2   bouyer 	retry:
    723  1.12  thorpej 		if (xs->retries) {
    724  1.12  thorpej 			xs->retries--;
    725   1.2   bouyer 			xs->error = XS_NOERROR;
    726   1.2   bouyer 			xs->flags &= ~ITSDONE;
    727   1.3    enami 			return (ERESTART);
    728   1.2   bouyer 		}
    729   1.2   bouyer 	case XS_DRIVER_STUFFUP:
    730   1.2   bouyer 	lose:
    731   1.2   bouyer 		error = EIO;
    732   1.2   bouyer 		break;
    733   1.2   bouyer 
    734   1.2   bouyer 	case XS_SELTIMEOUT:
    735   1.2   bouyer 		/* XXX Disable device? */
    736  1.12  thorpej 		error = EIO;
    737  1.12  thorpej 		break;
    738  1.12  thorpej 
    739  1.12  thorpej 	case XS_RESET:
    740  1.12  thorpej 		if (xs->retries) {
    741  1.12  thorpej 			SC_DEBUG(xs->sc_link, SDEV_DB3,
    742  1.12  thorpej 			    ("restarting command destroyed by reset\n"));
    743  1.12  thorpej 			goto retry;
    744  1.12  thorpej 		}
    745   1.2   bouyer 		error = EIO;
    746   1.2   bouyer 		break;
    747   1.2   bouyer 
    748   1.2   bouyer 	default:
    749   1.3    enami 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    750   1.2   bouyer 		printf("unknown error category from scsipi driver\n");
    751   1.2   bouyer 		error = EIO;
    752   1.2   bouyer 		break;
    753   1.2   bouyer 	}
    754   1.2   bouyer 
    755   1.3    enami 	return (error);
    756   1.2   bouyer }
    757   1.2   bouyer 
    758  1.14  thorpej /*
    759  1.14  thorpej  * Add a reference to the adapter pointed to by the provided
    760  1.14  thorpej  * link, enabling the adapter if necessary.
    761  1.14  thorpej  */
    762  1.14  thorpej int
    763  1.14  thorpej scsipi_adapter_addref(link)
    764  1.14  thorpej 	struct scsipi_link *link;
    765  1.14  thorpej {
    766  1.14  thorpej 	struct scsipi_adapter *adapter = link->adapter;
    767  1.14  thorpej 	int s, error = 0;
    768  1.14  thorpej 
    769  1.14  thorpej 	s = splbio();
    770  1.14  thorpej 	if (adapter->scsipi_refcnt++ == 0 &&
    771  1.14  thorpej 	    adapter->scsipi_enable != NULL) {
    772  1.14  thorpej 		error = (*adapter->scsipi_enable)(link->adapter_softc, 1);
    773  1.14  thorpej 		if (error)
    774  1.14  thorpej 			adapter->scsipi_refcnt--;
    775  1.14  thorpej 	}
    776  1.14  thorpej 	splx(s);
    777  1.14  thorpej 	return (error);
    778  1.14  thorpej }
    779  1.14  thorpej 
    780  1.14  thorpej /*
    781  1.14  thorpej  * Delete a reference to the adapter pointed to by the provided
    782  1.14  thorpej  * link, disabling the adapter if possible.
    783  1.14  thorpej  */
    784  1.14  thorpej void
    785  1.14  thorpej scsipi_adapter_delref(link)
    786  1.14  thorpej 	struct scsipi_link *link;
    787  1.14  thorpej {
    788  1.14  thorpej 	struct scsipi_adapter *adapter = link->adapter;
    789  1.14  thorpej 	int s;
    790  1.14  thorpej 
    791  1.14  thorpej 	s = splbio();
    792  1.14  thorpej 	if (adapter->scsipi_refcnt-- == 1 &&
    793  1.14  thorpej 	    adapter->scsipi_enable != NULL)
    794  1.14  thorpej 		(void) (*adapter->scsipi_enable)(link->adapter_softc, 0);
    795  1.14  thorpej 	splx(s);
    796  1.14  thorpej }
    797  1.14  thorpej 
    798   1.2   bouyer #ifdef	SCSIDEBUG
    799   1.2   bouyer /*
    800   1.2   bouyer  * Given a scsipi_xfer, dump the request, in all it's glory
    801   1.2   bouyer  */
    802   1.2   bouyer void
    803   1.2   bouyer show_scsipi_xs(xs)
    804   1.2   bouyer 	struct scsipi_xfer *xs;
    805   1.2   bouyer {
    806   1.3    enami 
    807   1.2   bouyer 	printf("xs(%p): ", xs);
    808   1.2   bouyer 	printf("flg(0x%x)", xs->flags);
    809   1.2   bouyer 	printf("sc_link(%p)", xs->sc_link);
    810   1.2   bouyer 	printf("retr(0x%x)", xs->retries);
    811   1.2   bouyer 	printf("timo(0x%x)", xs->timeout);
    812   1.2   bouyer 	printf("cmd(%p)", xs->cmd);
    813   1.2   bouyer 	printf("len(0x%x)", xs->cmdlen);
    814   1.2   bouyer 	printf("data(%p)", xs->data);
    815   1.2   bouyer 	printf("len(0x%x)", xs->datalen);
    816   1.2   bouyer 	printf("res(0x%x)", xs->resid);
    817   1.2   bouyer 	printf("err(0x%x)", xs->error);
    818   1.2   bouyer 	printf("bp(%p)", xs->bp);
    819   1.2   bouyer 	show_scsipi_cmd(xs);
    820   1.2   bouyer }
    821   1.2   bouyer 
    822   1.2   bouyer void
    823   1.2   bouyer show_scsipi_cmd(xs)
    824   1.2   bouyer 	struct scsipi_xfer *xs;
    825   1.2   bouyer {
    826   1.2   bouyer 	u_char *b = (u_char *) xs->cmd;
    827   1.3    enami 	int i = 0;
    828   1.2   bouyer 
    829   1.3    enami 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    830   1.2   bouyer 	printf("command: ");
    831   1.2   bouyer 
    832   1.2   bouyer 	if ((xs->flags & SCSI_RESET) == 0) {
    833   1.2   bouyer 		while (i < xs->cmdlen) {
    834   1.2   bouyer 			if (i)
    835   1.2   bouyer 				printf(",");
    836   1.2   bouyer 			printf("0x%x", b[i++]);
    837   1.2   bouyer 		}
    838   1.2   bouyer 		printf("-[%d bytes]\n", xs->datalen);
    839   1.2   bouyer 		if (xs->datalen)
    840   1.2   bouyer 			show_mem(xs->data, min(64, xs->datalen));
    841   1.2   bouyer 	} else
    842   1.2   bouyer 		printf("-RESET-\n");
    843   1.2   bouyer }
    844   1.2   bouyer 
    845   1.2   bouyer void
    846   1.2   bouyer show_mem(address, num)
    847   1.2   bouyer 	u_char *address;
    848   1.2   bouyer 	int num;
    849   1.2   bouyer {
    850   1.2   bouyer 	int x;
    851   1.2   bouyer 
    852   1.2   bouyer 	printf("------------------------------");
    853   1.2   bouyer 	for (x = 0; x < num; x++) {
    854   1.2   bouyer 		if ((x % 16) == 0)
    855   1.2   bouyer 			printf("\n%03d: ", x);
    856   1.2   bouyer 		printf("%02x ", *address++);
    857   1.2   bouyer 	}
    858   1.2   bouyer 	printf("\n------------------------------\n");
    859   1.2   bouyer }
    860   1.2   bouyer #endif /*SCSIDEBUG */
    861