Home | History | Annotate | Line # | Download | only in scsipi
scsiconf.h revision 1.29
      1 /*	$NetBSD: scsiconf.h,v 1.29 1996/03/19 03:05:15 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994, 1995 Charles 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 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) tfs.com)
     34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  *
     36  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  * Mellon University, makes this software available to CMU to distribute
     38  * or use in any manner that they see fit as long as this message is kept with
     39  * the software. For this reason TFS also grants any other persons or
     40  * organisations permission to use or modify this software.
     41  *
     42  * TFS supplies this software to be publicly redistributed
     43  * on the understanding that TFS is not responsible for the correct
     44  * functioning of this software in any circumstances.
     45  *
     46  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     47  */
     48 
     49 #ifndef	SCSI_SCSICONF_H
     50 #define SCSI_SCSICONF_H 1
     51 
     52 typedef	int			boolean;
     53 
     54 #include <sys/queue.h>
     55 #include <machine/cpu.h>
     56 #include <scsi/scsi_debug.h>
     57 
     58 /*
     59  * The following documentation tries to describe the relationship between the
     60  * various structures defined in this file:
     61  *
     62  * each adapter type has a scsi_adapter struct. This describes the adapter and
     63  *    identifies routines that can be called to use the adapter.
     64  * each device type has a scsi_device struct. This describes the device and
     65  *    identifies routines that can be called to use the device.
     66  * each existing device position (scsibus + target + lun)
     67  *    can be described by a scsi_link struct.
     68  *    Only scsi positions that actually have devices, have a scsi_link
     69  *    structure assigned. so in effect each device has scsi_link struct.
     70  *    The scsi_link structure contains information identifying both the
     71  *    device driver and the adapter driver for that position on that scsi bus,
     72  *    and can be said to 'link' the two.
     73  * each individual scsi bus has an array that points to all the scsi_link
     74  *    structs associated with that scsi bus. Slots with no device have
     75  *    a NULL pointer.
     76  * each individual device also knows the address of it's own scsi_link
     77  *    structure.
     78  *
     79  *				-------------
     80  *
     81  * The key to all this is the scsi_link structure which associates all the
     82  * other structures with each other in the correct configuration.  The
     83  * scsi_link is the connecting information that allows each part of the
     84  * scsi system to find the associated other parts.
     85  */
     86 
     87 struct buf;
     88 struct scsi_xfer;
     89 
     90 /*
     91  * These entrypoints are called by the high-end drivers to get services from
     92  * whatever low-end drivers they are attached to each adapter type has one of
     93  * these statically allocated.
     94  */
     95 struct scsi_adapter {
     96 	int		(*scsi_cmd) __P((struct scsi_xfer *));
     97 	void		(*scsi_minphys) __P((struct buf *));
     98 	int		(*open_target_lu) __P((void));
     99 	int		(*close_target_lu) __P((void));
    100 };
    101 
    102 /*
    103  * return values for scsi_cmd()
    104  */
    105 #define SUCCESSFULLY_QUEUED	0
    106 #define TRY_AGAIN_LATER		1
    107 #define	COMPLETE		2
    108 #define	ESCAPE_NOT_SUPPORTED	3
    109 
    110 /*
    111  * These entry points are called by the low-end drivers to get services from
    112  * whatever high-end drivers they are attached to.  Each device type has one
    113  * of these statically allocated.
    114  */
    115 struct scsi_device {
    116 	int	(*err_handler) __P((struct scsi_xfer *));
    117 			/* returns -1 to say err processing done */
    118 	void	(*start) __P((void *));
    119 
    120 	int	(*async) __P((void));
    121 	/*
    122 	 * When called with `0' as the second argument, we expect status
    123 	 * back from the upper-level driver.  When called with a `1',
    124 	 * we're simply notifying the upper-level driver that the command
    125 	 * is complete and expect no status back.
    126 	 */
    127 	int	(*done)  __P((struct scsi_xfer *, int));
    128 };
    129 
    130 /*
    131  * This structure describes the connection between an adapter driver and
    132  * a device driver, and is used by each to call services provided by
    133  * the other, and to allow generic scsi glue code to call these services
    134  * as well.
    135  */
    136 struct scsi_link {
    137 	u_int8_t scsibus;		/* the Nth scsibus */
    138 	u_int8_t target;		/* targ of this dev */
    139 	u_int8_t lun;			/* lun of this dev */
    140 	u_int8_t adapter_target;	/* what are we on the scsi bus */
    141 	u_int8_t openings;		/* available operations */
    142 	u_int8_t active;		/* operations in progress */
    143 	u_int8_t flags;			/* flags that all devices have */
    144 #define	SDEV_REMOVABLE	 	0x01	/* media is removable */
    145 #define	SDEV_MEDIA_LOADED 	0x02	/* device figures are still valid */
    146 #define	SDEV_WAITING	 	0x04	/* a process is waiting for this */
    147 #define	SDEV_OPEN	 	0x08	/* at least 1 open session */
    148 #define	SDEV_DBX		0xf0	/* debuging flags (scsi_debug.h) */
    149 	u_int8_t quirks;		/* per-device oddities */
    150 #define	SDEV_AUTOSAVE		0x01	/* do implicit SAVEDATAPOINTER on disconnect */
    151 #define	SDEV_NOSYNCWIDE		0x02	/* does not grok SDTR or WDTR */
    152 #define	SDEV_NOLUNS		0x04	/* does not grok LUNs */
    153 #define	SDEV_FORCELUNS		0x08	/* prehistoric drive/ctlr groks LUNs */
    154 	struct	scsi_device *device;	/* device entry points etc. */
    155 	void	*device_softc;		/* needed for call to foo_start */
    156 	struct	scsi_adapter *adapter;	/* adapter entry points etc. */
    157 	void	*adapter_softc;		/* needed for call to foo_scsi_cmd */
    158 };
    159 
    160 /*
    161  * This describes matching information for scsi_inqmatch().  The more things
    162  * match, the higher the configuration priority.
    163  */
    164 struct scsi_inquiry_pattern {
    165 	u_int8_t type;
    166 	boolean removable;
    167 	char *vendor;
    168 	char *product;
    169 	char *revision;
    170 };
    171 
    172 /*
    173  * One of these is allocated and filled in for each scsi bus.
    174  * it holds pointers to allow the scsi bus to get to the driver
    175  * That is running each LUN on the bus
    176  * it also has a template entry which is the prototype struct
    177  * supplied by the adapter driver, this is used to initialise
    178  * the others, before they have the rest of the fields filled in
    179  */
    180 struct scsibus_softc {
    181 	struct device sc_dev;
    182 	struct scsi_link *adapter_link;		/* prototype supplied by adapter */
    183 	struct scsi_link *sc_link[8][8];
    184 	u_int8_t moreluns;
    185 };
    186 
    187 /*
    188  * This is used to pass information from the high-level configuration code
    189  * to the device-specific drivers.
    190  */
    191 struct scsibus_attach_args {
    192 	struct scsi_link *sa_sc_link;
    193 	struct scsi_inquiry_data *sa_inqbuf;
    194 };
    195 
    196 /*
    197  * Each scsi transaction is fully described by one of these structures
    198  * It includes information about the source of the command and also the
    199  * device and adapter for which the command is destined.
    200  * (via the scsi_link structure)
    201  */
    202 struct scsi_xfer {
    203 	LIST_ENTRY(scsi_xfer) free_list;
    204 	int	flags;
    205 	struct	scsi_link *sc_link;	/* all about our device and adapter */
    206 	int	retries;		/* the number of times to retry */
    207 	int	timeout;		/* in milliseconds */
    208 	struct	scsi_generic *cmd;	/* The scsi command to execute */
    209 	int	cmdlen;			/* how long it is */
    210 	u_char	*data;			/* dma address OR a uio address */
    211 	int	datalen;		/* data len (blank if uio)    */
    212 	int	resid;			/* how much buffer was not touched */
    213 	int	error;			/* an error value	*/
    214 	struct	buf *bp;		/* If we need to associate with a buf */
    215 	struct	scsi_sense_data	sense; /* 32 bytes*/
    216 	/*
    217 	 * Believe it or not, Some targets fall on the ground with
    218 	 * anything but a certain sense length.
    219 	 */
    220 	int	req_sense_length;	/* Explicit request sense length */
    221 	u_int8_t status;		/* SCSI status */
    222 	struct	scsi_generic cmdstore;	/* stash the command in here */
    223 };
    224 
    225 /*
    226  * Per-request Flag values
    227  */
    228 #define	SCSI_NOSLEEP	0x0001	/* don't sleep */
    229 #define	SCSI_POLL	0x0002	/* poll for completion */
    230 #define	SCSI_AUTOCONF	0x0003	/* shorthand for SCSI_POLL | SCSI_NOSLEEP */
    231 #define	SCSI_USER	0x0004	/* Is a user cmd, call scsi_user_done	*/
    232 #define	ITSDONE		0x0008	/* the transfer is as done as it gets	*/
    233 #define	INUSE		0x0010	/* The scsi_xfer block is in use	*/
    234 #define	SCSI_SILENT	0x0020	/* don't announce NOT READY or MEDIA CHANGE */
    235 #define	SCSI_IGNORE_NOT_READY		0x0040	/* ignore NOT READY */
    236 #define	SCSI_IGNORE_MEDIA_CHANGE	0x0080	/* ignore MEDIA CHANGE */
    237 #define	SCSI_IGNORE_ILLEGAL_REQUEST	0x0100	/* ignore ILLEGAL REQUEST */
    238 #define	SCSI_RESET	0x0200	/* Reset the device in question		*/
    239 #define	SCSI_DATA_UIO	0x0400	/* The data address refers to a UIO	*/
    240 #define	SCSI_DATA_IN	0x0800	/* expect data to come INTO memory	*/
    241 #define	SCSI_DATA_OUT	0x1000	/* expect data to flow OUT of memory	*/
    242 #define	SCSI_TARGET	0x2000	/* This defines a TARGET mode op.	*/
    243 #define	SCSI_ESCAPE	0x4000	/* Escape operation			*/
    244 
    245 /*
    246  * Escape op codes.  This provides an extensible setup for operations
    247  * that are not scsi commands.  They are intended for modal operations.
    248  */
    249 
    250 #define SCSI_OP_TARGET	0x0001
    251 #define	SCSI_OP_RESET	0x0002
    252 #define	SCSI_OP_BDINFO	0x0003
    253 
    254 /*
    255  * Error values an adapter driver may return
    256  */
    257 #define XS_NOERROR	0	/* there is no error, (sense is invalid)  */
    258 #define XS_SENSE	1	/* Check the returned sense for the error */
    259 #define	XS_DRIVER_STUFFUP 2	/* Driver failed to perform operation	  */
    260 #define XS_SELTIMEOUT	3	/* The device timed out.. turned off?	  */
    261 #define XS_TIMEOUT	4	/* The Timeout reported was caught by SW  */
    262 #define XS_BUSY		5	/* The device busy, try again later?	  */
    263 
    264 caddr_t scsi_inqmatch __P((struct scsi_inquiry_data *, caddr_t, int, int, int *));
    265 
    266 struct scsi_xfer *scsi_get_xs __P((struct scsi_link *, int));
    267 void scsi_free_xs __P((struct scsi_xfer *, int));
    268 int scsi_execute_xs __P((struct scsi_xfer *));
    269 u_long scsi_size __P((struct scsi_link *, int));
    270 int scsi_test_unit_ready __P((struct scsi_link *, int));
    271 int scsi_change_def __P((struct scsi_link *, int));
    272 int scsi_inquire __P((struct scsi_link *, struct scsi_inquiry_data *, int));
    273 int scsi_prevent __P((struct scsi_link *, int, int));
    274 int scsi_start __P((struct scsi_link *, int, int));
    275 void scsi_done __P((struct scsi_xfer *));
    276 void scsi_user_done __P((struct scsi_xfer *));
    277 int scsi_scsi_cmd __P((struct scsi_link *, struct scsi_generic *,
    278 			int cmdlen, u_char *data_addr,
    279 			int datalen, int retries,
    280 			int timeout, struct buf *bp,
    281 			int flags));
    282 int scsi_do_ioctl __P((struct scsi_link *, dev_t, u_long, caddr_t, int, struct proc *));
    283 void sc_print_addr __P((struct scsi_link *));
    284 
    285 void show_scsi_xs __P((struct scsi_xfer *));
    286 void show_scsi_cmd __P((struct scsi_xfer *));
    287 void show_mem __P((u_char *, int));
    288 int scsi_probe_busses __P((int, int, int));
    289 void scsi_strvis __P((u_char *, u_char *, int));
    290 
    291 static __inline void _lto2b __P((u_int32_t val, u_int8_t *bytes));
    292 static __inline void _lto3b __P((u_int32_t val, u_int8_t *bytes));
    293 static __inline void _lto4b __P((u_int32_t val, u_int8_t *bytes));
    294 static __inline u_int32_t _2btol __P((u_int8_t *bytes));
    295 static __inline u_int32_t _3btol __P((u_int8_t *bytes));
    296 static __inline u_int32_t _4btol __P((u_int8_t *bytes));
    297 
    298 static __inline void _lto2l __P((u_int32_t val, u_int8_t *bytes));
    299 static __inline void _lto3l __P((u_int32_t val, u_int8_t *bytes));
    300 static __inline void _lto4l __P((u_int32_t val, u_int8_t *bytes));
    301 static __inline u_int32_t _2ltol __P((u_int8_t *bytes));
    302 static __inline u_int32_t _3ltol __P((u_int8_t *bytes));
    303 static __inline u_int32_t _4ltol __P((u_int8_t *bytes));
    304 
    305 static __inline void
    306 _lto2b(val, bytes)
    307 	u_int32_t val;
    308 	u_int8_t *bytes;
    309 {
    310 
    311 	bytes[0] = (val >> 8) & 0xff;
    312 	bytes[1] = val & 0xff;
    313 }
    314 
    315 static __inline void
    316 _lto3b(val, bytes)
    317 	u_int32_t val;
    318 	u_int8_t *bytes;
    319 {
    320 
    321 	bytes[0] = (val >> 16) & 0xff;
    322 	bytes[1] = (val >> 8) & 0xff;
    323 	bytes[2] = val & 0xff;
    324 }
    325 
    326 static __inline void
    327 _lto4b(val, bytes)
    328 	u_int32_t val;
    329 	u_int8_t *bytes;
    330 {
    331 
    332 	bytes[0] = (val >> 24) & 0xff;
    333 	bytes[1] = (val >> 16) & 0xff;
    334 	bytes[2] = (val >> 8) & 0xff;
    335 	bytes[3] = val & 0xff;
    336 }
    337 
    338 static __inline u_int32_t
    339 _2btol(bytes)
    340 	u_int8_t *bytes;
    341 {
    342 	register u_int32_t rv;
    343 
    344 	rv = (bytes[0] << 8) |
    345 	     bytes[1];
    346 	return (rv);
    347 }
    348 
    349 static __inline u_int32_t
    350 _3btol(bytes)
    351 	u_int8_t *bytes;
    352 {
    353 	register u_int32_t rv;
    354 
    355 	rv = (bytes[0] << 16) |
    356 	     (bytes[1] << 8) |
    357 	     bytes[2];
    358 	return (rv);
    359 }
    360 
    361 static __inline u_int32_t
    362 _4btol(bytes)
    363 	u_int8_t *bytes;
    364 {
    365 	register u_int32_t rv;
    366 
    367 	rv = (bytes[0] << 24) |
    368 	     (bytes[1] << 16) |
    369 	     (bytes[2] << 8) |
    370 	     bytes[3];
    371 	return (rv);
    372 }
    373 
    374 static __inline void
    375 _lto2l(val, bytes)
    376 	u_int32_t val;
    377 	u_int8_t *bytes;
    378 {
    379 
    380 	bytes[0] = val & 0xff;
    381 	bytes[1] = (val >> 8) & 0xff;
    382 }
    383 
    384 static __inline void
    385 _lto3l(val, bytes)
    386 	u_int32_t val;
    387 	u_int8_t *bytes;
    388 {
    389 
    390 	bytes[0] = val & 0xff;
    391 	bytes[1] = (val >> 8) & 0xff;
    392 	bytes[2] = (val >> 16) & 0xff;
    393 }
    394 
    395 static __inline void
    396 _lto4l(val, bytes)
    397 	u_int32_t val;
    398 	u_int8_t *bytes;
    399 {
    400 
    401 	bytes[0] = val & 0xff;
    402 	bytes[1] = (val >> 8) & 0xff;
    403 	bytes[2] = (val >> 16) & 0xff;
    404 	bytes[3] = (val >> 24) & 0xff;
    405 }
    406 
    407 static __inline u_int32_t
    408 _2ltol(bytes)
    409 	u_int8_t *bytes;
    410 {
    411 	register u_int32_t rv;
    412 
    413 	rv = bytes[0] |
    414 	     (bytes[1] << 8);
    415 	return (rv);
    416 }
    417 
    418 static __inline u_int32_t
    419 _3ltol(bytes)
    420 	u_int8_t *bytes;
    421 {
    422 	register u_int32_t rv;
    423 
    424 	rv = bytes[0] |
    425 	     (bytes[1] << 8) |
    426 	     (bytes[2] << 16);
    427 	return (rv);
    428 }
    429 
    430 static __inline u_int32_t
    431 _4ltol(bytes)
    432 	u_int8_t *bytes;
    433 {
    434 	register u_int32_t rv;
    435 
    436 	rv = bytes[0] |
    437 	     (bytes[1] << 8) |
    438 	     (bytes[2] << 16) |
    439 	     (bytes[3] << 24);
    440 	return (rv);
    441 }
    442 
    443 #endif /* SCSI_SCSICONF_H */
    444