Home | History | Annotate | Line # | Download | only in ic
isp_tpublic.h revision 1.1
      1 /* $NetBSD: isp_tpublic.h,v 1.1 2000/02/12 02:23:13 mjacob Exp $ */
      2 /*
      3  * Qlogic ISP Host Adapter Public Target Interface Structures && Routines
      4  *---------------------------------------
      5  * Copyright (c) 2000 by Matthew Jacob
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions, and the following disclaimer,
     13  *    without modification, immediately at the beginning of the file.
     14  * 2. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  * Matthew Jacob
     30  * Feral Software
     31  * mjacob (at) feral.com
     32  */
     33 
     34 /*
     35  * Required software target mode message and event handling structures.
     36  *
     37  * The message and event structures are used by the MI layer
     38  * to propagate messages and events upstream.
     39  */
     40 
     41 #ifndef	IN_MSGLEN
     42 #define	IN_MSGLEN	8
     43 #endif
     44 typedef struct {
     45 	void *		nt_hba;			/* HBA tag */
     46 	u_int64_t	nt_iid;			/* inititator id */
     47 	u_int64_t	nt_tgt;			/* target id */
     48 	u_int64_t	nt_lun;			/* logical unit */
     49 	u_int8_t	nt_bus;			/* bus */
     50 	u_int8_t	nt_tagtype;		/* tag type */
     51 	u_int16_t	nt_tagval;		/* tag value */
     52 	u_int8_t	nt_msg[IN_MSGLEN];	/* message content */
     53 } tmd_msg_t;
     54 
     55 typedef struct {
     56 	void *		ev_hba;			/* HBA tag */
     57 	u_int16_t	ev_bus;			/* bus */
     58 	u_int16_t	ev_event;		/* type of async event */
     59 } tmd_event_t;
     60 
     61 /*
     62  * Suggested software target mode command handling structure.
     63  *
     64  * The command structure is one suggested possible MD command structure,
     65  * but since the handling of thbis is entirely in the MD layer, there
     66  * is no requirement that it be used.
     67  *
     68  * The cd_private tag should be used by the MD layer to keep a free list
     69  * of these structures. Code outside of this driver can then use this
     70  * as an to identify it's own unit structures. That is, when not on the MD
     71  * layer's freelist, the MD layer should shove into it the identifier
     72  * that the outer layer has for it- passed in on an initial QIN_HBA_REG
     73  * call (see below).
     74  *
     75  * The cd_hba tag is a tag that uniquely identifies the HBA this target
     76  * mode command is coming from. The outer layer has to pass this back
     77  * unchanged to avoid chaos.
     78  *
     79  * The cd_iid, cd_tgt, cd_lun and cd_bus tags are used to identify the
     80  * id of the initiator who sent us a command, the target claim to be, the
     81  * lun on the target we claim to be, and the bus instance (for multiple
     82  * bus host adapters) that this applies to (consider it an extra Port
     83  * parameter). The iid, tgt and lun values are deliberately chosen to be
     84  * fat so that, for example, World Wide Names can be used instead of
     85  * the units that the Qlogic firmware uses (in the case where the MD
     86  * layer maintains a port database, for example).
     87  *
     88  * The cd_tagtype field specifies what kind of command tag has been
     89  * sent with the command. The cd_tagval is the tag's value.
     90  *
     91  * N.B.: when the MD layer sends this command to outside software
     92  * the outside software likely *MUST* return the same cd_tagval that
     93  * was in place because this value is likely what the Qlogic f/w uses
     94  * to identify a command.
     95  *
     96  * The cd_cdb contains storage for the passed in command descriptor block.
     97  * This is the maximum size we can get out of the Qlogic f/w. There's no
     98  * passed in length because whoever decodes the command to act upon it
     99  * will know what the appropriate length is.
    100  *
    101  * The tag cd_lflags are the flags set by the MD driver when it gets
    102  * command incoming or when it needs to inform any outside entities
    103  * that the last requested action failed.
    104  *
    105  * The tag cd_hflags should be set by any outside software to indicate
    106  * the validity of sense and status fields (defined below) and to indicate
    107  * the direction data is expected to move. It is an error to have both
    108  * CDFH_DATA_IN and CDFH_DATA_OUT set.
    109  *
    110  * If the CDFH_STSVALID flag is set, the command should be completed (after
    111  * sending any data and/or status). If CDFH_SNSVALID is set and the MD layer
    112  * can also handle sending the associated sense data (either back with an
    113  * FCP RESPONSE IU for Fibre Channel or otherwise automatically handling a
    114  * REQUEST SENSE from the initator for this target/lun), the MD layer will
    115  * set the CDFL_SENTSENSE flag on successful transmission of the sense data.
    116  * It is an error for the CDFH_SNSVALID bit to be set and CDFH_STSVALID not
    117  * to be set. It is an error for the CDFH_SNSVALID be set and the associated
    118  * SCSI status (cd_scsi_status) not be set to CHECK CONDITON.
    119  *
    120  * The tag cd_data points to a data segment to either be filled or
    121  * read from depending on the direction of data movement. The tag
    122  * is undefined if no data direction is set. The MD layer and outer
    123  * layers must agree on the meaning of cd_data.
    124  *
    125  * The tag cd_totlen is the total data amount expected to be moved
    126  * over the life of the command. It may be set by the MD layer,
    127  * for example, from the datalen field of an FCP CMND IU unit. If
    128  * it shows up in the outer layers set to zero and the CDB indicates
    129  * data should be moved, the outer layer should set it to the amount
    130  * expected to be moved.
    131  *
    132  * The tag cd_resid is the total residual of data not transferred.
    133  * That is, cd_resid is set up, possibly by the MD layer when it
    134  * reads the expected data transfer length from an FCP CMND IU,
    135  * to be the total data amount expected to be moved over the life
    136  * of the command. As data is successfully moved, this value is
    137  * decreased. At the end of a command, any residual indicates the
    138  * number of bytes requested but not moved.
    139  *
    140  * The tag cd_xfrlen is the length of the currently active data transfer.
    141  * This allows several interations between any outside software and the
    142  * MD layer to move data.
    143  *
    144  * The reason that total length and total residual have to be tracked
    145  * is that fibre channel FCP DATA IU units have to have a relative
    146  * offset field.
    147  *
    148  * N.B.: there is no necessary 1-to-1 correspondence between any one
    149  * data transfer segment and the number of CTIOs that will be generated
    150  * satisfy the current data transfer segment. It's not also possible to
    151  * predict how big a transfer can be before it will be 'too big'. Be
    152  * reasonable- a 64KB transfer is 'reasonable'. A 1MB transfer may not
    153  * be. A 32MB transfer is unreasonable. The problem here has to do with
    154  * how CTIOs can be used to map passed data pointers. In systems which
    155  * have page based scatter-gather requirements, each PAGESIZEd chunk will
    156  * consume one data segment descriptor- you get 3 or 4 of them per CTIO.
    157  * The size of the REQUEST QUEUE you drop a CTIO onto is finite (typically
    158  * it's 256, but on some systems it's even smaller, and note you have to
    159  * sure this queue with the initiator side of this driver).
    160  *
    161  * The tags cd_sense and cd_scsi_status are pretty obvious.
    162  *
    163  * The tag cd_error is to communicate between the MD layer and outer software
    164  * the current error conditions.
    165  *
    166  * The tag cd_reserved pads out the structure to 128 bytes.
    167  */
    168 
    169 #ifndef	_LP64
    170 #if	defined(__alpha__) || defined(__sparcv9cpu) || defined(__sparc_v9__)
    171 #define	_LP64
    172 #endif
    173 #endif
    174 
    175 #ifndef	_TMD_PAD_LEN
    176 #ifdef	_LP64
    177 #define	_TMD_PAD_LEN	12
    178 #else
    179 #define	_TMD_PAD_LEN	24
    180 #endif
    181 #endif
    182 #ifndef	ATIO_CDBLEN
    183 #define	ATIO_CDBLEN	26
    184 #endif
    185 #ifndef	QLTM_SENSELEN
    186 #define	QLTM_SENSELEN	18
    187 #endif
    188 typedef struct tmd_cmd {
    189 	void *			cd_private;	/* layer private data */
    190 	void *			cd_hba;		/* HBA tag */
    191 	void *			cd_data;	/* 'pointer' to data */
    192 	u_int64_t		cd_iid;		/* initiator ID */
    193 	u_int64_t		cd_tgt;		/* target id */
    194 	u_int64_t		cd_lun;		/* logical unit */
    195 	u_int8_t		cd_bus;		/* bus */
    196 	u_int8_t		cd_tagtype;	/* tag type */
    197 	u_int16_t		cd_tagval;	/* tag value */
    198 	u_int8_t		cd_cdb[ATIO_CDBLEN];	/* Command */
    199 	u_int8_t		cd_lflags;	/* flags lower level sets */
    200 	u_int8_t		cd_hflags;	/* flags higher level sets */
    201 	u_int32_t		cd_totlen;	/* total data requirement */
    202 	u_int32_t		cd_resid;	/* total data residual */
    203 	u_int32_t		cd_xfrlen;	/* current data requirement */
    204 	int32_t			cd_error;	/* current error */
    205 	u_int8_t		cd_sense[QLTM_SENSELEN];
    206 	u_int16_t		cd_scsi_status;	/* closing SCSI status */
    207 	u_int8_t		cd_reserved[_TMD_PAD_LEN];
    208 } tmd_cmd_t;
    209 
    210 #define	CDFL_BUSY	0x01		/* this command is not on a free list */
    211 #define	CDFL_NODISC	0x02		/* disconnect not allowed */
    212 #define	CDFL_SENTSENSE	0x04		/* last action sent sense data */
    213 #define	CDFL_ERROR	0x08		/* last action ended in error */
    214 #define	CDFL_PRIVATE_0	0x80		/* private layer flags */
    215 
    216 #define	CDFH_SNSVALID	0x01		/* sense data valid */
    217 #define	CDFH_STSVALID	0x02		/* status valid */
    218 #define	CDFH_NODATA	0x00		/* no data transfer expected */
    219 #define	CDFH_DATA_IN	0x04		/* target (us) -> initiator (them) */
    220 #define	CDFH_DATA_OUT	0x08		/* initiator (them) -> target (us) */
    221 #define	CDFH_DATA_MASK	0x0C		/* mask to cover data direction */
    222 #define	CDFH_PRIVATE_0	0x80		/* private layer flags */
    223 
    224 /*
    225  * Action codes set by the Qlogic MD target driver for
    226  * the external layer to figure out what to do with.
    227  */
    228 typedef enum {
    229 	QOUT_HBA_REG=0,	/* the argument is a pointer to a hba_register_t */
    230 	QOUT_TMD_START,	/* the argument is a pointer to a tmd_cmd_t */
    231 	QOUT_TMD_DONE,	/* the argument is a pointer to a tmd_cmd_t */
    232 	QOUT_TEVENT,	/* the argument is a pointer to a tmd_event_t */
    233 	QOUT_TMSG,	/* the argument is a pointer to a tmd_msg_t */
    234 	QOUT_HBA_UNREG	/* the argument is a pointer to a hba_register_t */
    235 } tact_e;
    236 
    237 /*
    238  * Action codes set by the external layer for the
    239  * MD Qlogic driver to figure out what to do with.
    240  */
    241 typedef enum {
    242 	QIN_HBA_REG=6,	/* the argument is a pointer to a hba_register_t */
    243 	QIN_TMD_CONT,	/* the argument is a pointer to a tmd_cmd_t */
    244 	QIN_TMD_FIN,	/* the argument is a pointer to a done tmd_cmd_t */
    245 	QIN_HBA_UNREG	/* the argument is a pointer to a hba_register_t */
    246 } qact_e;
    247 
    248 /*
    249  * A word about the START/CONT/DONE/FIN dance:
    250  *
    251  *	When the HBA is enabled for receiving commands, one may	show up
    252  *	without notice. When that happens, the Qlogic target mode driver
    253  *	gets a tmd_cmd_t, fills it with the info that just arrived, and
    254  *	calls the outer layer with a QIN_TMD_START code and pointer to
    255  *	the tmd_cmd_t.
    256  *
    257  *	The outer layer decodes the command, fetches data, prepares stuff,
    258  *	whatever, and starts by passing back the pointer with a QIN_TMD_CONT
    259  *	code which causes the Qlogic target mode driver to generate CTIOs to
    260  *	satisfy whatever action needs to be taken. When those CTIOs complete,
    261  *	the Qlogic target driver sends the pointer to the cmd_tmd_t back with
    262  *	a QOUT_TMD_DONE code. This repeats for as long as necessary.
    263  *
    264  *	The outer layer signals it wants to end the command by settings within
    265  *	the tmd_cmd_t itself. When the final QIN_TMD_CONT is reported completed,
    266  *	the outer layer frees the tmd_cmd_t by sending the pointer to it
    267  *	back with a QIN_TMD_FIN code.
    268  *
    269  *	The graph looks like:
    270  *
    271  *	QOUT_TMD_START -> [ QIN_TMD_CONT -> QOUT_TMD_DONE ] * -> QIN_TMD_FIN.
    272  */
    273 
    274 /*
    275  * Target handler functions.
    276  * The MD target handler function (the outer layer calls this)
    277  * should be be prototyped like:
    278  *
    279  *	void target_action(qact_e, void *arg)
    280  *
    281  * The outer layer target handler function (the MD layer calls this)
    282  * should be be prototyped like:
    283  *
    284  *	void system_action(tact_e, void *arg)
    285  */
    286 
    287 /*
    288  * This structure is used to register to other software modules the
    289  * binding of an HBA identifier, driver name and instance and the
    290  * lun width capapbilities of this target driver. It's up to each
    291  * platform to figure out how it wants to do this, but a typical
    292  * sequence would be for the MD layer to find some external module's
    293  * entry point and start by sending a QOUT_HBA_REG with info filled
    294  * in, and the external module to call back with a QIN_HBA_REG that
    295  * passes back the corresponding information.
    296  */
    297 typedef struct {
    298 	void *	r_identity;
    299 	char	r_name[8];
    300 	int	r_inst;
    301 	int	r_lunwidth;
    302 	void   (*r_action) __P((int, void *));
    303 } hba_register_t;
    304