/*
 * Copyright (c) 1989-1993, by Sun Microsytems, Inc.
 * Defines and structures used only within the driver, bst.c
 */

#ifndef	_BST_DEF_H
#define	_BST_DEF_H

#pragma ident	"@(#)bst_def.h	1.6	94/06/14 SMI"

#ifdef	__cplusplus
extern "C" {
#endif

#if	defined(_KERNEL) || defined(_KMEMUSER)

/*
 * This driver does not reset the SCSI bus. Instead we just give up
 * and complain if the target hangs. If the bus is really stuck, one
 * of the Sun drivers (e.g. sd) will reset it. If you decide that
 * your device is critical and you should in fact reset the bus in
 * this driver, turn on this define:
#define	SCSI_BUS_RESET
 */

/*
 * Local definitions, for clarity of code
 */
#define	BST_SLICE(d)	(getminor(d) & 0x7)	/* dev_t -> partition */
#define	BST_MINOR(i, s)	((i) << 3 | (s))	/* instance/slice -> minor */
#define	BST_DEV(i)	makedevice(0, BST_MINOR((i), 0)) /* instance -> dev_t */
#define	BST_INST(d)	(getminor(d) >> 3)	/* dev_t -> instance */

#define	BST_MUTEX(targ)	(&(targ)->targ_devp->sd_mutex)
#define	ROUTE(targ)	(&(targ)->targ_devp->sd_address)

#define	BST_UNLOCK	0		/* unlock media in drive */
#define	BST_LOCK	1		/* lock media in drive */
#define	BST_START	1		/* start disk */
#define	BST_EJECT	2		/* eject disk */

#define	SCBP(pkt)	((struct scsi_status *)(pkt)->pkt_scbp)
#define	SCBP_C(pkt)	((*(pkt)->pkt_scbp) & STATUS_MASK)
#define	CDBP(pkt)	((union scsi_cdb *)(pkt)->pkt_cdbp)
#define	BST_BP_PKT(bp)	((struct scsi_pkt *)bp->av_back)
#define	ARQP(pktp) 	((struct scsi_arq_status *)((pktp)->pkt_scbp))

#define	BST_REMOVABLE(targ)	(targ)->targ_devp->sd_inq->inq_rmb

#define	BST_CE_DEBUG1	((1 << 8) | CE_CONT)
#define	BST_CE_DEBUG2	((2 << 8) | CE_CONT)
#define	BST_CE_DEBUG3	((3 << 8) | CE_CONT)
#define	BST_CE_DEBUG4	((4 << 8) | CE_CONT)
#define	BST_LOG		if (bst_debug) bst_log
#define	BST_DEBUG_ENTER	if (bst_debug) debug_enter


/*
 * Private info for scsi targets.
 *
 * Pointed to by the un_private pointer
 * of one of the scsi_device structures.
 */
struct bst_soft {
	struct scsi_pkt	*targ_rqs;	/* ptr to request sense command pkt */
	struct scsi_pkt	*targ_pkt;	/* ptr to current command pkt */
	struct diskhd	targ_iorq;	/* i/o request queue */
	struct buf	*targ_sbufp;	/* for use in special io */
	struct dk_label	targ_label;	/* disk label */
	kcondvar_t	targ_sbuf_cv;	/* conditional variable for sbufp */
	kcondvar_t	targ_pkt_cv;	/* conditional variable for pkt */
	ksema_t		targ_semaphore;	/* open/cose serialization */
	daddr_t		targ_capacity;	/* total disk capacity */
	daddr_t		targ_ustart;	/* starting sector of OS */
	struct partition targ_part[NDKMAP]; /* partition table */
	u_char		targ_sbuf_busy;	/* Wait Variable */
	u_char		targ_excl;	/* exclusively opened */
	u_char		targ_geom_ok;	/* disk geometry is valid */
	short		targ_retry_ct;	/* retry count */
	u_char		targ_state;	/* current state */
	u_char		targ_raw_map;	/* partitions opened character */
	u_char		targ_block_map;	/* partitions opened block */
	u_int		targ_arq;	/* ARQ mode on this tgt */
	struct buf	*targ_rqbp;	/* ARQ buf pointer */
	struct scsi_device *targ_devp; /* back pointer to scsi_device */
};


#endif	/* defined(_KERNEL) || defined(_KMEMUSER) */

/*
 * Driver states
 */
#define	BST_STATE_NIL		0
#define	BST_STATE_RWAIT		1	/* waiting for resources */
#define	BST_STATE_NORESP	2	/* device is not responding */

/*
 * Parameters
 */

#define	BST_IO_TIME	30	/* default command timeout, 30sec */

/*
 * 5 seconds is what we'll wait if we get a Busy Status back
 */
#define	BST_BSY_TIMEOUT		(drv_usectohz(5 * 1000000))

/*
 * Number of times we'll retry a normal operation.
 * This includes retries due to transport failure
 */
#define	BST_RETRY_COUNT		30

/*
 * bst_callback action codes
 */
#define	COMMAND_DONE		0
#define	COMMAND_DONE_ERROR	1
#define	QUE_COMMAND		2
#define	QUE_SENSE		3
#define	JUST_RETURN		4

/*
 * Ioctl commands
 */
#define	BSTIOC		('S' << 8)
#define	BSTIOC_READY	(BSTIOC|0)	/* Send a Test Unit Ready command */
#define	BSTIOC_ERRLEV	(BSTIOC|1)	/* Set Error Reporting level */

#define	BUFLEN	256	/* useful buffer length */

#ifdef	__cplusplus
}
#endif

#endif	/* _BST_DEF_H */
