Home | History | Annotate | Download | only in adapters
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 
     27 /*
     28  * esp - Emulex SCSI Processor host adapter driver with FAS101/236,
     29  *	tagged and non-tagged queuing support
     30  */
     31 #if defined(lint) && !defined(DEBUG)
     32 #define	DEBUG	1
     33 #define	ESP_CHECK
     34 #endif
     35 
     36 #include <sys/note.h>
     37 
     38 #include <sys/modctl.h>
     39 #include <sys/scsi/scsi.h>
     40 
     41 /*
     42  * these are non-ddi compliant:
     43  */
     44 #include <sys/varargs.h>
     45 #include <sys/var.h>
     46 #include <sys/proc.h>
     47 #include <sys/thread.h>
     48 #include <sys/utsname.h>
     49 #include <sys/kstat.h>
     50 #include <sys/vtrace.h>
     51 #include <sys/kmem.h>
     52 #include <sys/callb.h>
     53 
     54 /*
     55  * private
     56  */
     57 #include <sys/scsi/adapters/espvar.h>
     58 #include <sys/scsi/adapters/espcmd.h>
     59 #include <sys/scsi/impl/scsi_reset_notify.h>
     60 
     61 /*
     62  * External references
     63  */
     64 extern uchar_t	scsi_cdb_size[];
     65 
     66 /*
     67  * tunables
     68  */
     69 static int esp_burst_sizes_limit = 0xff; /* patch in case of hw problems */
     70 static int esp_selection_timeout = 250; /* 250 milliseconds */
     71 
     72 #ifdef ESP_KSTATS
     73 static int esp_do_kstats = 1;
     74 static int esp_do_bus_kstats = 1;
     75 #endif
     76 
     77 #ifdef	ESPDEBUG
     78 static int espdebug = 0;
     79 static int esp_no_sync_backoff = 0;
     80 static void esp_stat_int_print(struct esp *esp);
     81 static int esp_test_stop;
     82 #endif	/* ESPDEBUG */
     83 
     84 /*
     85  * Local static data
     86  * the global mutex protects some of these esp driver variables
     87  */
     88 static kmutex_t esp_global_mutex;
     89 static int esp_watchdog_running = 0;
     90 static int esp_scsi_watchdog_tick;	/* in sec */
     91 static clock_t esp_tick;		/* esp_watch() interval in Hz */
     92 static timeout_id_t esp_reset_watch;
     93 static timeout_id_t esp_timeout_id = 0;
     94 static int esp_timeout_initted = 0;
     95 static int esp_n_esps = 0;
     96 static void *esp_state;
     97 static kmutex_t esp_log_mutex;
     98 static char esp_log_buf[256];
     99 
    100 /*
    101  * readers/writer lock to protect the integrity of the softc structure
    102  * linked list while being traversed (or updated).
    103  */
    104 static krwlock_t esp_global_rwlock;
    105 static struct esp *esp_softc = (struct esp *)0;
    106 static struct esp *esp_tail;
    107 
    108 /*
    109  * variables & prototypes for torture testing
    110  */
    111 #ifdef ESP_TEST_RQSENSE
    112 static int esp_test_rqsense;
    113 #endif /* ESP_TEST_RQSENSE */
    114 
    115 #ifdef	ESP_TEST_PARITY
    116 static int esp_ptest_emsgin;
    117 static int esp_ptest_msgin;
    118 static int esp_ptest_msg = -1;
    119 static int esp_ptest_status;
    120 static int esp_ptest_data_in;
    121 #endif	/* ESP_TEST_PARITY */
    122 
    123 #ifdef ESP_TEST_ABORT
    124 static int esp_atest;
    125 static int esp_atest_disc;
    126 static int esp_atest_reconn;
    127 static void esp_test_abort(struct esp *esp, int slot);
    128 #endif /* ESP_TEST_ABORT */
    129 
    130 #ifdef ESP_TEST_RESET
    131 static int esp_rtest;
    132 static int esp_rtest_type;
    133 static void esp_test_reset(struct esp *esp, int slot);
    134 #endif /* ESP_TEST_RESET */
    135 
    136 #ifdef ESP_TEST_TIMEOUT
    137 static int esp_force_timeout;
    138 #endif /* ESP_TEST_TIMEOUT */
    139 
    140 #ifdef ESP_TEST_BUS_RESET
    141 static int esp_btest;
    142 #endif /* ESP_TEST_BUS_RESET */
    143 
    144 #ifdef ESP_TEST_UNTAGGED
    145 static int esp_test_untagged;
    146 static int esp_enable_untagged;
    147 static int esp_test_stop;
    148 #endif /* ESP_TEST_UNTAGGED */
    149 #ifdef ESP_PERF
    150 _NOTE(SCHEME_PROTECTS_DATA("Stable Data", esp_request_count))
    151 _NOTE(SCHEME_PROTECTS_DATA("Stable Data", esp_sample_time))
    152 _NOTE(SCHEME_PROTECTS_DATA("Stable Data", esp_intr_count))
    153 _NOTE(SCHEME_PROTECTS_DATA("Stable Data", esp_ncmds))
    154 _NOTE(SCHEME_PROTECTS_DATA("Stable Data", esp_ndisc))
    155 _NOTE(SCHEME_PROTECTS_DATA("Stable Data", esp_ncmds_per_esp))
    156 
    157 /*
    158  * these should really be protected but it is not really important
    159  * to be very accurate
    160  */
    161 static int esp_request_count;
    162 static int esp_sample_time = 0;
    163 static int esp_intr_count;
    164 static int esp_ncmds;
    165 static int esp_ndisc;
    166 #define	MAX_ESPS	80	/* should be enough */
    167 static int esp_ncmds_per_esp[MAX_ESPS];
    168 #endif
    169 
    170 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", \
    171 	scsi_pkt esp_cmd buf scsi_cdb scsi_status))
    172 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_address scsi_device))
    173 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", esp_watchdog_running))
    174 _NOTE(DATA_READABLE_WITHOUT_LOCK(esp_scsi_watchdog_tick))
    175 _NOTE(DATA_READABLE_WITHOUT_LOCK(espdebug))
    176 _NOTE(DATA_READABLE_WITHOUT_LOCK(dmaga))
    177 
    178 /*
    179  * function prototypes
    180  *
    181  * scsa functions are exported by means of the transport table
    182  */
    183 static int esp_scsi_tgt_probe(struct scsi_device *sd,
    184     int (*waitfunc)(void));
    185 static int esp_scsi_tgt_init(dev_info_t *, dev_info_t *,
    186     scsi_hba_tran_t *, struct scsi_device *);
    187 static int esp_start(struct scsi_address *ap, struct scsi_pkt *pkt);
    188 static int esp_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
    189 static int esp_reset(struct scsi_address *ap, int level);
    190 static int esp_commoncap(struct scsi_address *ap, char *cap, int val,
    191     int tgtonly, int doset);
    192 static int esp_getcap(struct scsi_address *ap, char *cap, int whom);
    193 static int esp_setcap(struct scsi_address *ap, char *cap, int value, int whom);
    194 static struct scsi_pkt *esp_scsi_init_pkt(struct scsi_address *ap,
    195     struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
    196     int tgtlen, int flags, int (*callback)(), caddr_t arg);
    197 static void esp_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt);
    198 static void esp_scsi_dmafree(struct scsi_address *ap,
    199     struct scsi_pkt *pkt);
    200 static void esp_scsi_sync_pkt(struct scsi_address *ap,
    201     struct scsi_pkt *pkt);
    202 
    203 /*
    204  * internal functions
    205  */
    206 static int esp_ustart(struct esp *esp, short start_slot, short flag);
    207 static int esp_startcmd(struct esp *esp, struct esp_cmd *sp);
    208 static int esp_finish(struct esp *esp);
    209 static void esp_handle_qfull(struct esp *esp, struct esp_cmd *sp, int slot);
    210 static void esp_restart_cmd(void *);
    211 static int esp_dopoll(struct esp *esp, int timeout);
    212 static uint_t esp_intr(caddr_t arg);
    213 static void espsvc(struct esp *esp);
    214 static int esp_phasemanage(struct esp *esp);
    215 static int esp_handle_unknown(struct esp *esp);
    216 static int esp_handle_cmd_start(struct esp *esp);
    217 static int esp_handle_cmd_done(struct esp *esp);
    218 static int esp_handle_msg_out(struct esp *esp);
    219 static int esp_handle_msg_out_done(struct esp *esp);
    220 static int esp_handle_clearing(struct esp *esp);
    221 static int esp_handle_data(struct esp *esp);
    222 static int esp_handle_data_done(struct esp *esp);
    223 static int esp_handle_c_cmplt(struct esp *esp);
    224 static int esp_handle_msg_in(struct esp *esp);
    225 static int esp_handle_more_msgin(struct esp *esp);
    226 static int esp_handle_msg_in_done(struct esp *esp);
    227 static int esp_onebyte_msg(struct esp *esp);
    228 static int esp_twobyte_msg(struct esp *esp);
    229 static int esp_multibyte_msg(struct esp *esp);
    230 static int esp_finish_select(struct esp *esp);
    231 static int esp_reconnect(struct esp *esp);
    232 static int esp_istart(struct esp *esp);
    233 static void esp_runpoll(struct esp *esp, short slot, struct esp_cmd *sp);
    234 static int esp_reset_bus(struct esp *esp);
    235 static int esp_reset_recovery(struct esp *esp);
    236 static int esp_handle_selection(struct esp *esp);
    237 static void esp_makeproxy_cmd(struct esp_cmd *sp,
    238     struct scsi_address *ap, int nmsg, ...);
    239 static void esp_make_sdtr(struct esp *esp, int msgout_offset,
    240     int period, int offset);
    241 static void esp_watch(void *);
    242 static void esp_watchsubr(struct esp *esp);
    243 static void esp_cmd_timeout(struct esp *esp, struct esp_cmd *sp, int slot);
    244 static int esp_abort_curcmd(struct esp *esp);
    245 static int esp_abort_cmd(struct esp *esp, struct esp_cmd *sp, int slot);
    246 static int esp_abort_allcmds(struct esp *esp);
    247 static void esp_internal_reset(struct esp *esp, int reset_action);
    248 static void esp_sync_backoff(struct esp *esp, struct esp_cmd *sp, int slot);
    249 static void esp_hw_reset(struct esp *esp, int action);
    250 /*PRINTFLIKE3*/
    251 static void esplog(struct esp *esp, int level, const char *fmt, ...)
    252 	__KPRINTFLIKE(3);
    253 /*PRINTFLIKE2*/
    254 static void eprintf(struct esp *esp, const char *fmt, ...)
    255 	__KPRINTFLIKE(2);
    256 static void esp_printstate(struct esp *esp, char *msg);
    257 static void esp_dump_cmd(struct esp_cmd *sp);
    258 static void esp_dump_state(struct esp *esp);
    259 static char *esp_state_name(ushort_t state);
    260 static void esp_update_props(struct esp *esp, int tgt);
    261 static int _esp_start(struct esp *esp, struct esp_cmd *sp, int flag);
    262 static int _esp_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
    263 static int _esp_reset(struct scsi_address *ap, int level);
    264 static int esp_alloc_tag(struct esp *esp, struct esp_cmd *sp);
    265 static int esp_remove_readyQ(struct esp *esp, struct esp_cmd *sp, int slot);
    266 static void esp_flush_readyQ(struct esp *esp, int slot);
    267 static void esp_flush_tagQ(struct esp *esp, int slot);
    268 static void esp_flush_cmd(struct esp *esp, struct esp_cmd *sp,
    269     uchar_t reason, uint_t stat);
    270 static int esp_abort_connected_cmd(struct esp *esp, struct esp_cmd *sp,
    271     uchar_t msg);
    272 static int esp_abort_disconnected_cmd(struct esp *esp, struct scsi_address *ap,
    273     struct esp_cmd *sp, uchar_t msg, int slot);
    274 static void esp_mark_packets(struct esp *esp, int slot, uchar_t reason,
    275     uint_t stat);
    276 static int esp_reset_connected_cmd(struct esp *esp, struct scsi_address *ap,
    277     int slot);
    278 static int esp_reset_disconnected_cmd(struct esp *esp, struct scsi_address *ap,
    279     int slot);
    280 static int esp_create_arq_pkt(struct esp *esp, struct scsi_address *ap,
    281     int size);
    282 static int esp_start_arq_pkt(struct esp *esp, struct esp_cmd *sp);
    283 static void esp_complete_arq_pkt(struct esp *esp, struct esp_cmd *sp,
    284     int slot);
    285 static void esp_determine_chip_type(struct esp *esp);
    286 static void esp_create_callback_thread(struct esp *esp);
    287 static void esp_destroy_callback_thread(struct esp *);
    288 static void esp_callback(struct esp *esp);
    289 static void esp_call_pkt_comp(struct esp *esp, struct esp_cmd *sp);
    290 static int esp_set_new_window(struct esp *esp, struct esp_cmd *sp);
    291 static int esp_restore_pointers(struct esp *esp, struct esp_cmd *sp);
    292 static int esp_next_window(struct esp *esp, struct esp_cmd *sp);
    293 static void esp_start_watch_reset_delay(struct esp *);
    294 static void esp_watch_reset_delay(void *arg);
    295 static int esp_watch_reset_delay_subr(struct esp *esp);
    296 void esp_wakeup_callback_thread(struct callback_info *cb_info);
    297 static void esp_update_TQ_props(struct esp *esp, int tgt, int value);
    298 static int esp_check_dma_error(struct esp *esp);
    299 static void esp_reset_cleanup(struct esp *esp, int slot);
    300 static int esp_scsi_reset_notify(struct scsi_address *ap, int flag,
    301     void (*callback)(caddr_t), caddr_t arg);
    302 static void esp_set_throttles(struct esp *esp, int slot,
    303     int n, int what);
    304 static void esp_set_all_lun_throttles(struct esp *esp, int slot, int what);
    305 static void esp_save_throttles(struct esp *esp, int slot, int n,
    306     short *throttle);
    307 static void esp_restore_throttles(struct esp *esp, int slot, int n,
    308     short *throttle);
    309 static int esp_do_proxy_cmd(struct esp *esp, struct esp_cmd *sp,
    310     struct scsi_address *ap, int slot, char *what);
    311 static void esp_remove_tagged_cmd(struct esp *esp, struct esp_cmd *sp,
    312     int slot, int timeout);
    313 static void esp_decrement_ncmds(struct esp *esp, struct esp_cmd *sp);
    314 static int esp_pkt_alloc_extern(struct esp *esp, struct esp_cmd *sp,
    315     int cmdlen, int tgtlen, int statuslen, int kf);
    316 static void esp_pkt_destroy_extern(struct esp *esp, struct esp_cmd *sp);
    317 static int esp_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags);
    318 static void esp_kmem_cache_destructor(void *buf, void *cdrarg);
    319 
    320 static void esp_flush_fifo(struct esp *esp);
    321 static void esp_empty_startQ(struct esp *esp);
    322 
    323 #ifdef ESP_CHECK
    324 static void esp_check_in_transport(struct esp *esp, struct esp_cmd *sp);
    325 #else
    326 #define	esp_check_in_transport(esp, sp)
    327 #endif
    328 
    329 /*
    330  * esp DMA attr for all supported dma engines:
    331  */
    332 static ddi_dma_attr_t dma1_espattr = {
    333 	DMA_ATTR_V0, (unsigned long long)0,
    334 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    335 	1, DEFAULT_BURSTSIZE, 1,
    336 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    337 	1, 512, 0
    338 };
    339 
    340 /*
    341  * ESC1 esp dma attr
    342  */
    343 static ddi_dma_attr_t esc1_espattr = {
    344 	DMA_ATTR_V0, (unsigned long long)0,
    345 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    346 	1, DEFAULT_BURSTSIZE | BURST32, 4,
    347 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    348 	1, 512, 0
    349 };
    350 
    351 /*
    352  * DMA2 esp dma attr
    353  */
    354 static ddi_dma_attr_t dma2_espattr = {
    355 	DMA_ATTR_V0, (unsigned long long)0,
    356 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    357 	1, DEFAULT_BURSTSIZE, 1,
    358 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    359 	1, 512, 0
    360 };
    361 
    362 /*
    363  * DMA3 esp dma attr
    364  */
    365 static ddi_dma_attr_t dma3_espattr = {
    366 	DMA_ATTR_V0, (unsigned long long)0x0,
    367 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    368 	1, DEFAULT_BURSTSIZE | BURST32, 1,
    369 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
    370 	1, 512, 0
    371 };
    372 
    373 /*
    374  * autoconfiguration routines.
    375  */
    376 static int esp_attach(dev_info_t *dev, ddi_attach_cmd_t cmd);
    377 static int esp_detach(dev_info_t *dev, ddi_detach_cmd_t cmd);
    378 static int esp_dr_detach(dev_info_t *dev);
    379 
    380 static struct dev_ops esp_ops = {
    381 	DEVO_REV,		/* devo_rev, */
    382 	0,			/* refcnt  */
    383 	ddi_no_info,		/* info */
    384 	nulldev,		/* identify */
    385 	nulldev,		/* probe */
    386 	esp_attach,		/* attach */
    387 	esp_detach,		/* detach */
    388 	nodev,			/* reset */
    389 	NULL,			/* cb ops */
    390 	NULL,			/* bus operations */
    391 	ddi_power,		/* power */
    392 	ddi_quiesce_not_supported,	/* devo_quiesce */
    393 };
    394 
    395 char _depends_on[] = "misc/scsi";
    396 
    397 static struct modldrv modldrv = {
    398 	&mod_driverops, /* Type of module. This one is a driver */
    399 	"ESP SCSI HBA Driver", /* Name of the module. */
    400 	&esp_ops,	/* driver ops */
    401 };
    402 
    403 static struct modlinkage modlinkage = {
    404 	MODREV_1, (void *)&modldrv, NULL
    405 };
    406 
    407 int
    408 _init(void)
    409 {
    410 	int	i;
    411 
    412 	/* CONSTCOND */
    413 	ASSERT(NO_COMPETING_THREADS);
    414 
    415 	i = ddi_soft_state_init(&esp_state, sizeof (struct esp),
    416 	    ESP_INIT_SOFT_STATE);
    417 	if (i != 0)
    418 		return (i);
    419 	if ((i = scsi_hba_init(&modlinkage)) != 0) {
    420 		ddi_soft_state_fini(&esp_state);
    421 		return (i);
    422 	}
    423 
    424 	mutex_init(&esp_global_mutex, NULL, MUTEX_DRIVER, NULL);
    425 	rw_init(&esp_global_rwlock, NULL, RW_DRIVER, NULL);
    426 
    427 	mutex_init(&esp_log_mutex, NULL, MUTEX_DRIVER, NULL);
    428 
    429 	if ((i = mod_install(&modlinkage)) != 0) {
    430 		mutex_destroy(&esp_log_mutex);
    431 		rw_destroy(&esp_global_rwlock);
    432 		mutex_destroy(&esp_global_mutex);
    433 		ddi_soft_state_fini(&esp_state);
    434 		scsi_hba_fini(&modlinkage);
    435 		return (i);
    436 	}
    437 
    438 	return (i);
    439 }
    440 
    441 int
    442 _fini(void)
    443 {
    444 	int	i;
    445 
    446 	/* CONSTCOND */
    447 	ASSERT(NO_COMPETING_THREADS);
    448 	if ((i = mod_remove(&modlinkage)) == 0) {
    449 		mutex_destroy(&esp_log_mutex);
    450 		scsi_hba_fini(&modlinkage);
    451 		rw_destroy(&esp_global_rwlock);
    452 		mutex_destroy(&esp_global_mutex);
    453 		ddi_soft_state_fini(&esp_state);
    454 	}
    455 	return (i);
    456 }
    457 
    458 int
    459 _info(struct modinfo *modinfop)
    460 {
    461 	return (mod_info(&modlinkage, modinfop));
    462 }
    463 
    464 static int
    465 esp_scsi_tgt_probe(struct scsi_device *sd,
    466     int (*waitfunc)(void))
    467 {
    468 	dev_info_t *dip = ddi_get_parent(sd->sd_dev);
    469 	int rval = SCSIPROBE_FAILURE;
    470 	scsi_hba_tran_t *tran;
    471 	struct esp *esp;
    472 	int tgt = sd->sd_address.a_target;
    473 
    474 	tran = ddi_get_driver_private(dip);
    475 	ASSERT(tran != NULL);
    476 	esp = TRAN2ESP(tran);
    477 
    478 	/*
    479 	 * force renegotiation since Inquiry cmds do not cause
    480 	 * check conditions
    481 	 */
    482 	mutex_enter(ESP_MUTEX);
    483 	esp->e_sync_known &= ~(1 << tgt);
    484 	mutex_exit(ESP_MUTEX);
    485 
    486 	rval = scsi_hba_probe(sd, waitfunc);
    487 
    488 	/*
    489 	 * the scsi-options precedence is:
    490 	 *	target-scsi-options		highest
    491 	 *	device-type-scsi-options
    492 	 *	per bus scsi-options
    493 	 *	global scsi-options		lowest
    494 	 */
    495 	mutex_enter(ESP_MUTEX);
    496 	if ((rval == SCSIPROBE_EXISTS) &&
    497 	    ((esp->e_target_scsi_options_defined & (1 << tgt)) == 0)) {
    498 		int options;
    499 
    500 		options = scsi_get_device_type_scsi_options(dip, sd, -1);
    501 		if (options != -1) {
    502 			esp->e_target_scsi_options[tgt] = options;
    503 			esplog(esp, CE_NOTE,
    504 			    "?target%x-scsi-options = 0x%x\n", tgt,
    505 			    esp->e_target_scsi_options[tgt]);
    506 
    507 			if (options & SCSI_OPTIONS_FAST) {
    508 				esp->e_default_period[tgt] = (uchar_t)
    509 				    MIN_SYNC_PERIOD(esp);
    510 			} else {
    511 				esp->e_default_period[tgt] = (uchar_t)
    512 				    CONVERT_PERIOD(DEFAULT_SYNC_PERIOD);
    513 			}
    514 			esp->e_neg_period[tgt] = 0;
    515 			esp->e_sync_known &= ~(1 << tgt);
    516 		}
    517 	}
    518 	mutex_exit(ESP_MUTEX);
    519 
    520 	IPRINTF2("target%x-scsi-options = 0x%x\n",
    521 	    tgt, esp->e_target_scsi_options[tgt]);
    522 
    523 	return (rval);
    524 }
    525 
    526 
    527 /*ARGSUSED*/
    528 static int
    529 esp_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
    530     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
    531 {
    532 	return (((sd->sd_address.a_target < NTARGETS) &&
    533 	    (sd->sd_address.a_lun < NLUNS_PER_TARGET)) ?
    534 	    DDI_SUCCESS : DDI_FAILURE);
    535 }
    536 
    537 static char *prop_cfreq = "clock-frequency";
    538 
    539 /*ARGSUSED*/
    540 static int
    541 esp_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
    542 {
    543 	struct esp *esp;
    544 	volatile struct dmaga *dmar = NULL;
    545 	volatile struct espreg *ep;
    546 	ddi_dma_attr_t	*esp_dma_attr;
    547 	scsi_hba_tran_t *tran = NULL;
    548 	ddi_device_acc_attr_t dev_attr;
    549 
    550 	int		instance, i;
    551 	char		buf[64];
    552 	int		mutex_initialized = 0;
    553 	int		add_intr_done = 0;
    554 	int		bound_handle = 0;
    555 	uint_t		count;
    556 	size_t		rlen;
    557 	char		*prop_template = "target%d-scsi-options";
    558 	char		prop_str[32];
    559 
    560 	switch (cmd) {
    561 	case DDI_ATTACH:
    562 		break;
    563 
    564 	case DDI_RESUME:
    565 	case DDI_PM_RESUME:
    566 		if ((tran = ddi_get_driver_private(dip)) == NULL)
    567 			return (DDI_FAILURE);
    568 
    569 		esp = TRAN2ESP(tran);
    570 		if (!esp) {
    571 			return (DDI_FAILURE);
    572 		}
    573 		mutex_enter(ESP_MUTEX);
    574 
    575 		/*
    576 		 * Reset hardware and softc to "no outstanding commands"
    577 		 * Note that a check condition can result on first command
    578 		 * to a target.
    579 		 */
    580 		esp_internal_reset(esp,
    581 		    ESP_RESET_SOFTC|ESP_RESET_ESP|ESP_RESET_DMA);
    582 		(void) esp_reset_bus(esp);
    583 
    584 		/*
    585 		 * esp_watchdog_running was reset at checkpoint time,
    586 		 * enable it at resume time
    587 		 */
    588 		esp_watchdog_running = 1;
    589 
    590 		esp->e_suspended = 0;
    591 
    592 		mutex_enter(&esp_global_mutex);
    593 		if (esp_timeout_id == 0) {
    594 			esp_timeout_id = timeout(esp_watch, NULL, esp_tick);
    595 			esp_timeout_initted = 1;
    596 		}
    597 		mutex_exit(&esp_global_mutex);
    598 
    599 		/* make sure that things get started */
    600 		(void) esp_istart(esp);
    601 		ESP_CHECK_STARTQ_AND_ESP_MUTEX_EXIT(esp);
    602 		return (DDI_SUCCESS);
    603 
    604 	default:
    605 		return (DDI_FAILURE);
    606 	}
    607 
    608 	instance = ddi_get_instance(dip);
    609 
    610 	/*
    611 	 * Since we know that some instantiations of this device can
    612 	 * be plugged into slave-only SBus slots, check to see whether
    613 	 * this is one such.
    614 	 */
    615 	if (ddi_slaveonly(dip) == DDI_SUCCESS) {
    616 		cmn_err(CE_WARN,
    617 		    "esp%d: device in slave-only slot", instance);
    618 		return (DDI_FAILURE);
    619 	}
    620 
    621 	if (ddi_intr_hilevel(dip, 0)) {
    622 		/*
    623 		 * Interrupt number '0' is a high-level interrupt.
    624 		 * At this point you either add a special interrupt
    625 		 * handler that triggers a soft interrupt at a lower level,
    626 		 * or - more simply and appropriately here - you just
    627 		 * fail the attach.
    628 		 */
    629 		cmn_err(CE_WARN,
    630 		    "esp%d: Device is using a hilevel intr", instance);
    631 		return (DDI_FAILURE);
    632 	}
    633 
    634 	/*
    635 	 * Allocate softc information.
    636 	 */
    637 	if (ddi_soft_state_zalloc(esp_state, instance) != DDI_SUCCESS) {
    638 		cmn_err(CE_WARN,
    639 		    "esp%d: cannot allocate soft state", instance);
    640 		return (DDI_FAILURE);
    641 	}
    642 
    643 	esp = (struct esp *)ddi_get_soft_state(esp_state, instance);
    644 
    645 	if (esp == NULL) {
    646 		return (DDI_FAILURE);
    647 	}
    648 
    649 	/*
    650 	 * map in device registers
    651 	 */
    652 	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
    653 	dev_attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
    654 	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
    655 
    656 	if (ddi_regs_map_setup(dip, (uint_t)0, (caddr_t *)&ep,
    657 	    (off_t)0, (off_t)sizeof (struct espreg),
    658 	    &dev_attr, &esp->e_regs_acc_handle) != DDI_SUCCESS) {
    659 		cmn_err(CE_WARN, "esp%d: unable to map registers", instance);
    660 		goto exit;
    661 	}
    662 
    663 	dmar = dma_alloc(dip);
    664 	if (dmar == NULL) {
    665 		cmn_err(CE_WARN,
    666 		    "esp%d: cannot find dma controller", instance);
    667 		goto unmap;
    668 	}
    669 
    670 	/*
    671 	 * Initialize state of DMA gate array.
    672 	 * Must clear DMAGA_RESET on the ESC before accessing the esp.
    673 	 */
    674 	switch (DMAGA_REV(dmar)) {
    675 	case DMA_REV2:
    676 		esp_dma_attr = &dma2_espattr;
    677 		break;
    678 	case ESC1_REV1:
    679 		dmar->dmaga_csr &= ~DMAGA_RESET;
    680 		esp_dma_attr = &esc1_espattr;
    681 		break;
    682 	case DMA_REV3:
    683 		esp_dma_attr = &dma3_espattr;
    684 		break;
    685 	case DMA_REV1:
    686 	default:
    687 		esp_dma_attr = &dma1_espattr;
    688 		break;
    689 	}
    690 
    691 	dmar->dmaga_csr &= ~DMAGA_WRITE;
    692 
    693 	if (ddi_dma_alloc_handle(dip, esp_dma_attr,
    694 	    DDI_DMA_SLEEP, NULL, &esp->e_dmahandle) != DDI_SUCCESS) {
    695 		cmn_err(CE_WARN,
    696 		    "esp%d: cannot alloc dma handle", instance);
    697 		goto fail;
    698 	}
    699 
    700 	if (ddi_dma_mem_alloc(esp->e_dmahandle, (uint_t)FIFOSIZE,
    701 	    &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
    702 	    NULL, (caddr_t *)&esp->e_cmdarea, &rlen,
    703 	    &esp->e_cmdarea_acc_handle) != DDI_SUCCESS) {
    704 		cmn_err(CE_WARN,
    705 		    "esp%d: cannot alloc cmd area", instance);
    706 		goto fail;
    707 	}
    708 	ASSERT(rlen >= FIFOSIZE);
    709 
    710 	if (ddi_dma_addr_bind_handle(esp->e_dmahandle,
    711 	    NULL, (caddr_t)esp->e_cmdarea,
    712 	    rlen, DDI_DMA_RDWR|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
    713 	    &esp->e_dmacookie, &count) != DDI_DMA_MAPPED) {
    714 		cmn_err(CE_WARN,
    715 		    "esp%d: cannot bind cmdarea", instance);
    716 		goto fail;
    717 	}
    718 	bound_handle++;
    719 	ASSERT(count == 1);
    720 
    721 	/*
    722 	 * Allocate a transport structure
    723 	 */
    724 	tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
    725 
    726 	/* Indicate that we are 'sizeof (scsi_*(9S))' clean. */
    727 	scsi_size_clean(dip);		/* SCSI_SIZE_CLEAN_VERIFY ok */
    728 
    729 	/*
    730 	 * the ESC has a rerun bug and the workaround is
    731 	 * to round up the ESC count; rather than
    732 	 * doing this on each xfer we do it once here
    733 	 * for the cmd area read xfers
    734 	 */
    735 	esp->e_dma_rev = DMAGA_REV(dmar);
    736 	if (esp->e_dma_rev == ESC1_REV1) {
    737 		uint32_t addr1 = esp->e_dmacookie.dmac_address;
    738 		uint32_t addr2 = roundup(addr1 + FIFOSIZE, ptob(1));
    739 		esp->e_esc_read_count = (uint32_t)(addr2 - addr1);
    740 	}
    741 
    742 	/*
    743 	 * By default we assume embedded devices and save time
    744 	 * checking for timeouts in esp_watch() by skipping the rest of luns
    745 	 * If we're talking to any non-embedded devices, we can't cheat
    746 	 * and skip over non-zero luns anymore in esp_watch().
    747 	 */
    748 	esp->e_dslot = NLUNS_PER_TARGET;
    749 
    750 #ifdef	ESPDEBUG
    751 	/*
    752 	 * Initialize last state log.
    753 	 */
    754 	for (i = 0; i < NPHASE; i++) {
    755 		esp->e_phase[i].e_save_state = STATE_FREE;
    756 		esp->e_phase[i].e_save_stat = -1;
    757 		esp->e_phase[i].e_val1 = -1;
    758 		esp->e_phase[i].e_val2 = -1;
    759 	}
    760 	esp->e_phase_index = 0;
    761 	esp->e_xfer = 0;
    762 #endif	/* ESPDEBUG */
    763 
    764 	/*
    765 	 * Initialize throttles.
    766 	 */
    767 	esp_set_throttles(esp, 0, N_SLOTS, CLEAR_THROTTLE);
    768 
    769 	/*
    770 	 * initialize transport structure
    771 	 */
    772 	esp->e_tran			= tran;
    773 	esp->e_dev			= dip;
    774 
    775 	tran->tran_hba_private		= esp;
    776 	tran->tran_tgt_private		= NULL;
    777 
    778 	tran->tran_tgt_init		= esp_scsi_tgt_init;
    779 	tran->tran_tgt_probe		= esp_scsi_tgt_probe;
    780 	tran->tran_tgt_free		= NULL;
    781 
    782 	tran->tran_start		= esp_start;
    783 	tran->tran_abort		= esp_abort;
    784 	tran->tran_reset		= esp_reset;
    785 	tran->tran_getcap		= esp_getcap;
    786 	tran->tran_setcap		= esp_setcap;
    787 	tran->tran_init_pkt		= esp_scsi_init_pkt;
    788 	tran->tran_destroy_pkt		= esp_scsi_destroy_pkt;
    789 	tran->tran_dmafree		= esp_scsi_dmafree;
    790 	tran->tran_sync_pkt		= esp_scsi_sync_pkt;
    791 	tran->tran_reset_notify		= esp_scsi_reset_notify;
    792 	tran->tran_get_bus_addr		= NULL;
    793 	tran->tran_get_name		= NULL;
    794 	tran->tran_add_eventcall	= NULL;
    795 	tran->tran_get_eventcookie	= NULL;
    796 	tran->tran_post_event		= NULL;
    797 	tran->tran_remove_eventcall	= NULL;
    798 
    799 	/* XXX need tran_quiesce and tran_unquiesce for hotplugging */
    800 	tran->tran_bus_reset		= NULL;
    801 	tran->tran_quiesce		= NULL;
    802 	tran->tran_unquiesce		= NULL;
    803 
    804 	esp->e_espconf = DEFAULT_HOSTID;
    805 	i = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0, "initiator-id", -1);
    806 	if (i == -1) {
    807 		i = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
    808 		    "scsi-initiator-id", -1);
    809 	}
    810 	if (i != DEFAULT_HOSTID && i >= 0 && i < NTARGETS) {
    811 		esplog(esp, CE_NOTE, "initiator SCSI ID now %d\n", i);
    812 		esp->e_espconf = (uchar_t)i;
    813 	}
    814 
    815 	for (i = 0; i < NTARGETS; i++) {
    816 		esp->e_qfull_retries[i] = QFULL_RETRIES;
    817 		esp->e_qfull_retry_interval[i] =
    818 		    drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
    819 	}
    820 
    821 	esp->e_reg = ep;
    822 	esp->e_dma = dmar;
    823 	esp->e_last_slot = esp->e_cur_slot = UNDEFINED;
    824 
    825 	IPRINTF1("DMA Rev: 0x%x\n", ESP_DMAGA_REV(esp));
    826 
    827 	esp->e_dma_attr = esp_dma_attr;
    828 	IPRINTF1("esp_dma_attr burstsize=%x\n",
    829 	    esp_dma_attr->dma_attr_burstsizes);
    830 
    831 	/*
    832 	 * Attach this instance of the hba
    833 	 */
    834 	if (scsi_hba_attach_setup(dip, esp->e_dma_attr, tran, 0) !=
    835 	    DDI_SUCCESS) {
    836 		cmn_err(CE_WARN, "esp: scsi_hba_attach failed\n");
    837 		goto fail;
    838 	}
    839 
    840 	/*
    841 	 * if scsi-options property exists, use it;
    842 	 * otherwise use the global variable
    843 	 */
    844 	esp->e_scsi_options = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
    845 	    "scsi-options", SCSI_OPTIONS_DR);
    846 
    847 	/* we don't support wide */
    848 	if (esp->e_scsi_options & SCSI_OPTIONS_WIDE) {
    849 		esp->e_scsi_options &= ~SCSI_OPTIONS_WIDE;
    850 		(void) ddi_prop_update_int(DDI_MAJOR_T_UNKNOWN, dip,
    851 		    "scsi-options", esp->e_scsi_options);
    852 	}
    853 
    854 	if ((esp->e_scsi_options & SCSI_OPTIONS_SYNC) == 0) {
    855 		esp->e_weak = 0xff;
    856 	}
    857 
    858 	/*
    859 	 * if scsi-selection-timeout property exists, use it
    860 	 */
    861 	esp_selection_timeout = ddi_prop_get_int(DDI_DEV_T_ANY,
    862 	    dip, 0, "scsi-selection-timeout", SCSI_DEFAULT_SELECTION_TIMEOUT);
    863 
    864 #ifdef ESPDEBUG
    865 	if ((esp->e_scsi_options & SCSI_DEBUG_HA) && (espdebug == 0)) {
    866 		espdebug = 1;
    867 	}
    868 #endif
    869 
    870 	/*
    871 	 * if target<n>-scsi-options property exists, use it;
    872 	 * otherwise use the e_scsi_options
    873 	 */
    874 	for (i = 0; i < NTARGETS; i++) {
    875 		(void) sprintf(prop_str, prop_template, i);
    876 		esp->e_target_scsi_options[i] = ddi_prop_get_int(
    877 		    DDI_DEV_T_ANY, dip, 0, prop_str, -1);
    878 		if (esp->e_target_scsi_options[i] != -1) {
    879 			esplog(esp, CE_NOTE,
    880 			    "?target%d_scsi_options=0x%x\n",
    881 			    i, esp->e_target_scsi_options[i]);
    882 			esp->e_target_scsi_options_defined |= 1 << i;
    883 		} else {
    884 			esp->e_target_scsi_options[i] = esp->e_scsi_options;
    885 		}
    886 
    887 		if (((esp->e_target_scsi_options[i] & SCSI_OPTIONS_DR) == 0) &&
    888 		    (esp->e_target_scsi_options[i] & SCSI_OPTIONS_TAG)) {
    889 			esp->e_target_scsi_options[i] &= ~SCSI_OPTIONS_TAG;
    890 			esplog(esp, CE_WARN,
    891 			    "Disabled TQ since disconnects are disabled\n");
    892 		}
    893 	}
    894 
    895 	esp->e_scsi_tag_age_limit =
    896 	    ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0, "scsi-tag-age-limit",
    897 	    scsi_tag_age_limit);
    898 	IPRINTF2("esp tag age limit=%d, global=%d\n",
    899 	    esp->e_scsi_tag_age_limit, scsi_tag_age_limit);
    900 	if (esp->e_scsi_tag_age_limit != scsi_tag_age_limit) {
    901 		esplog(esp, CE_NOTE, "scsi-tag-age-limit=%d\n",
    902 		    esp->e_scsi_tag_age_limit);
    903 	}
    904 
    905 	esp->e_scsi_reset_delay = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
    906 	    "scsi-reset-delay", scsi_reset_delay);
    907 	IPRINTF2("esp scsi_reset_delay=%x, global=%x\n",
    908 	    esp->e_scsi_reset_delay, scsi_reset_delay);
    909 	if (esp->e_scsi_reset_delay == 0) {
    910 		esplog(esp, CE_NOTE,
    911 		    "scsi_reset_delay of 0 is not recommended,"
    912 		    " resetting to SCSI_DEFAULT_RESET_DELAY\n");
    913 		esp->e_scsi_reset_delay = SCSI_DEFAULT_RESET_DELAY;
    914 	}
    915 	if (esp->e_scsi_reset_delay != scsi_reset_delay) {
    916 		esplog(esp, CE_NOTE, "scsi-reset-delay=%d\n",
    917 		    esp->e_scsi_reset_delay);
    918 	}
    919 
    920 	esp->e_force_async = 0;
    921 	/*
    922 	 * disable tagged queuing for all targets
    923 	 * (will be enabled by target driver if necessary)
    924 	 */
    925 	esp->e_notag = 0xff;
    926 
    927 	/*
    928 	 * get iblock cookie and initialize mutexes
    929 	 */
    930 	if (ddi_get_iblock_cookie(dip, (uint_t)0, &esp->e_iblock)
    931 	    != DDI_SUCCESS) {
    932 		cmn_err(CE_WARN, "esp_attach: cannot get iblock cookie");
    933 		goto fail;
    934 	}
    935 
    936 	mutex_init(ESP_MUTEX, NULL, MUTEX_DRIVER, esp->e_iblock);
    937 
    938 	/*
    939 	 * initialize mutex for startQ
    940 	 */
    941 	mutex_init(&esp->e_startQ_mutex, NULL, MUTEX_DRIVER, esp->e_iblock);
    942 
    943 	/*
    944 	 * add this esp to the linked list of esp's
    945 	 */
    946 	rw_enter(&esp_global_rwlock, RW_WRITER);
    947 	if (esp_softc == (struct esp *)NULL) {
    948 		esp_softc = esp;
    949 	} else {
    950 		esp_tail->e_next = esp;
    951 	}
    952 	esp_tail = esp;		/* point to last esp in list */
    953 	rw_exit(&esp_global_rwlock);
    954 	mutex_initialized++;
    955 
    956 	/*
    957 	 * kstat_intr support
    958 	 */
    959 	(void) sprintf(buf, "esp%d", instance);
    960 	esp->e_intr_kstat = kstat_create("esp", instance, buf, "controller", \
    961 	    KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
    962 	if (esp->e_intr_kstat)
    963 		kstat_install(esp->e_intr_kstat);
    964 
    965 	if (ddi_add_intr(dip, (uint_t)0, &esp->e_iblock, NULL, esp_intr,
    966 	    (caddr_t)esp)) {
    967 		cmn_err(CE_WARN, "esp: cannot add intr");
    968 		goto fail;
    969 	}
    970 	add_intr_done++;
    971 
    972 	/*
    973 	 * finally, find out what kind of ESP/FAS chip we have here
    974 	 * now we are ready to take the reset interrupt
    975 	 */
    976 	esp_determine_chip_type(esp);
    977 
    978 	/*
    979 	 * start off one watchdog for all esp's now we are fully initialized
    980 	 */
    981 	if (esp_softc == esp) {
    982 		esp_scsi_watchdog_tick =
    983 		    ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
    984 		    "scsi-watchdog-tick", scsi_watchdog_tick);
    985 		if (esp_scsi_watchdog_tick != scsi_watchdog_tick) {
    986 			esplog(esp, CE_NOTE, "scsi-watchdog-tick=%d\n",
    987 			    esp_scsi_watchdog_tick);
    988 		}
    989 		esp_tick = drv_usectohz((clock_t)
    990 		    esp_scsi_watchdog_tick * 1000000);
    991 		IPRINTF2("esp scsi watchdog tick=%x, esp_tick=%lx\n",
    992 		    esp_scsi_watchdog_tick, esp_tick);
    993 		mutex_enter(&esp_global_mutex);
    994 		if (esp_timeout_id == 0) {
    995 			esp_timeout_id = timeout(esp_watch, NULL, esp_tick);
    996 			esp_timeout_initted = 1;
    997 		}
    998 		mutex_exit(&esp_global_mutex);
    999 	}
   1000 
   1001 	/*
   1002 	 * Initialize power management bookkeeping; components are
   1003 	 * created idle
   1004 	 */
   1005 
   1006 	/*
   1007 	 * Since as of now, there is no power management done in
   1008 	 * scsi-HBA drivers, there is no need to create a pm_component.
   1009 	 * BUT esp is a special case with GYPSY. In gypsy, the
   1010 	 * PM_SUSPEND/PM_RESUME is used. So, the following few lines
   1011 	 * of code will be there until Gypsy machines are supported.
   1012 	 */
   1013 
   1014 	if (pm_create_components(dip, 1) == DDI_SUCCESS) {
   1015 		pm_set_normal_power(dip, 0, 1);
   1016 	} else {
   1017 		goto fail;
   1018 	}
   1019 
   1020 #ifdef ESP_KSTATS
   1021 	/*
   1022 	 * kstats to measure scsi bus busy time
   1023 	 */
   1024 	if (esp_do_bus_kstats) {
   1025 		if ((esp->e_scsi_bus_stats = kstat_create("esp-scsi-bus",
   1026 		    instance, NULL, "disk", KSTAT_TYPE_IO, 1,
   1027 		    KSTAT_FLAG_PERSISTENT)) != NULL) {
   1028 			esp->e_scsi_bus_stats->ks_lock = ESP_MUTEX;
   1029 			kstat_install(esp->e_scsi_bus_stats);
   1030 		}
   1031 	}
   1032 #endif /* ESP_KSTATS */
   1033 
   1034 	/*
   1035 	 * create a possibly shared callback thread which will empty the
   1036 	 * callback queue
   1037 	 */
   1038 	mutex_enter(&esp_global_mutex);
   1039 	esp_create_callback_thread(esp);
   1040 	mutex_exit(&esp_global_mutex);
   1041 
   1042 	/*
   1043 	 * create kmem cache for packets
   1044 	 */
   1045 	(void) sprintf(buf, "esp%d_cache", instance);
   1046 	esp->e_kmem_cache = kmem_cache_create(buf,
   1047 	    ESP_CMD_SIZE, 8,
   1048 	    esp_kmem_cache_constructor, esp_kmem_cache_destructor,
   1049 	    NULL, (void *)esp, NULL, 0);
   1050 	if (esp->e_kmem_cache == NULL) {
   1051 		cmn_err(CE_WARN, "esp: cannot create kmem_cache");
   1052 		goto fail;
   1053 	}
   1054 
   1055 	ddi_report_dev(dip);
   1056 
   1057 	return (DDI_SUCCESS);
   1058 
   1059 fail:
   1060 	cmn_err(CE_WARN, "esp%d: cannot attach", instance);
   1061 	if (esp) {
   1062 		struct esp *next, *prev;
   1063 
   1064 		/* remove this esp from the linked list */
   1065 		rw_enter(&esp_global_rwlock, RW_WRITER);
   1066 		for (prev = NULL,  next = esp_softc; next != NULL;
   1067 		    prev = next, next = next->e_next) {
   1068 			if (next == esp) {
   1069 				if (next == esp_softc) {
   1070 					esp_softc = esp->e_next;
   1071 				} else {
   1072 					prev->e_next = esp->e_next;
   1073 				}
   1074 				if (esp_tail == esp) {
   1075 					esp_tail = prev;
   1076 				}
   1077 				break;
   1078 			}
   1079 		}
   1080 		rw_exit(&esp_global_rwlock);
   1081 
   1082 		if (mutex_initialized) {
   1083 			mutex_destroy(&esp->e_startQ_mutex);
   1084 			mutex_destroy(ESP_MUTEX);
   1085 		}
   1086 		if (esp->e_intr_kstat) {
   1087 			kstat_delete(esp->e_intr_kstat);
   1088 		}
   1089 		if (add_intr_done) {
   1090 			ddi_remove_intr(dip, (uint_t)0, esp->e_iblock);
   1091 		}
   1092 		if (tran) {
   1093 			scsi_hba_tran_free(tran);
   1094 		}
   1095 		if (esp->e_kmem_cache) {
   1096 			kmem_cache_destroy(esp->e_kmem_cache);
   1097 		}
   1098 		if (esp->e_cmdarea) {
   1099 			if (bound_handle) {
   1100 				(void) ddi_dma_unbind_handle(esp->e_dmahandle);
   1101 			}
   1102 			ddi_dma_mem_free(&esp->e_cmdarea_acc_handle);
   1103 		}
   1104 		if (esp->e_dmahandle) {
   1105 			ddi_dma_free_handle(&esp->e_dmahandle);
   1106 		}
   1107 	}
   1108 
   1109 	if (dmar)
   1110 		dma_free((struct dmaga *)dmar);
   1111 unmap:
   1112 	if (esp->e_regs_acc_handle)
   1113 		ddi_regs_map_free(&esp->e_regs_acc_handle);
   1114 
   1115 exit:
   1116 	if (esp) {
   1117 		ddi_soft_state_free(esp_state, instance);
   1118 	}
   1119 
   1120 	return (DDI_FAILURE);
   1121 }
   1122 
   1123 /*ARGSUSED*/
   1124 static int
   1125 esp_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
   1126 {
   1127 	struct esp	*esp, *nesp;
   1128 	scsi_hba_tran_t		*tran;
   1129 
   1130 	switch (cmd) {
   1131 	case DDI_DETACH:
   1132 		return (esp_dr_detach(dip));
   1133 
   1134 	case DDI_SUSPEND:
   1135 	case DDI_PM_SUSPEND:
   1136 		if ((tran = ddi_get_driver_private(dip)) == NULL)
   1137 			return (DDI_FAILURE);
   1138 
   1139 		esp = TRAN2ESP(tran);
   1140 		if (!esp) {
   1141 			return (DDI_FAILURE);
   1142 		}
   1143 		mutex_enter(ESP_MUTEX);
   1144 
   1145 		esp->e_suspended = 1;
   1146 		esp_watchdog_running = 0;
   1147 
   1148 		if (esp->e_ncmds) {
   1149 			(void) esp_reset_bus(esp);
   1150 			(void) esp_dopoll(esp, SHORT_POLL_TIMEOUT);
   1151 		}
   1152 		/*
   1153 		 * In the current implementation of esp power management, the
   1154 		 * SCSI active terminators are turned off and so the bus
   1155 		 * signals can wander everywhere - including generating false
   1156 		 * interrupts, so they need to be disabled.  This should also
   1157 		 * be done for a full SUSPEND in theory, but since CPR writes
   1158 		 * out the state file....
   1159 		 */
   1160 		if (cmd == DDI_PM_SUSPEND) {
   1161 			esp->e_dmaga_csr &= ~DMAGA_INTEN;
   1162 			esp->e_dma->dmaga_csr = esp->e_dmaga_csr;
   1163 		}
   1164 		mutex_exit(ESP_MUTEX);
   1165 
   1166 		if (esp->e_restart_cmd_timeid) {
   1167 			(void) untimeout(esp->e_restart_cmd_timeid);
   1168 			esp->e_restart_cmd_timeid = 0;
   1169 		}
   1170 
   1171 		/* Last esp? */
   1172 		rw_enter(&esp_global_rwlock, RW_WRITER);
   1173 		for (nesp = esp_softc; nesp; nesp = nesp->e_next) {
   1174 			if (!nesp->e_suspended) {
   1175 				rw_exit(&esp_global_rwlock);
   1176 				return (DDI_SUCCESS);
   1177 			}
   1178 		}
   1179 		rw_exit(&esp_global_rwlock);
   1180 
   1181 		mutex_enter(&esp_global_mutex);
   1182 		if (esp_timeout_initted) {
   1183 			timeout_id_t tid = esp_timeout_id;
   1184 			esp_timeout_initted = 0;
   1185 			esp_timeout_id = 0;		/* don't resched */
   1186 			mutex_exit(&esp_global_mutex);
   1187 			(void) untimeout(tid);
   1188 			mutex_enter(&esp_global_mutex);
   1189 		}
   1190 
   1191 		if (esp_reset_watch) {
   1192 			mutex_exit(&esp_global_mutex);
   1193 			(void) untimeout(esp_reset_watch);
   1194 			mutex_enter(&esp_global_mutex);
   1195 			esp_reset_watch = 0;
   1196 		}
   1197 		mutex_exit(&esp_global_mutex);
   1198 
   1199 		return (DDI_SUCCESS);
   1200 
   1201 	default:
   1202 		return (DDI_FAILURE);
   1203 	}
   1204 	_NOTE(NOT_REACHED)
   1205 	/* NOTREACHED */
   1206 }
   1207 
   1208 static int
   1209 esp_dr_detach(dev_info_t *dev)
   1210 {
   1211 	struct esp	*esp, *e;
   1212 	scsi_hba_tran_t		*tran;
   1213 	int			i, j;
   1214 
   1215 	if ((tran = ddi_get_driver_private(dev)) == NULL)
   1216 		return (DDI_FAILURE);
   1217 
   1218 	esp = TRAN2ESP(tran);
   1219 	if (!esp) {
   1220 		return (DDI_FAILURE);
   1221 	}
   1222 
   1223 	/*
   1224 	 * Force interrupts OFF
   1225 	 */
   1226 	esp->e_dmaga_csr &= ~DMAGA_INTEN;
   1227 	esp->e_dma->dmaga_csr = esp->e_dmaga_csr;
   1228 	ddi_remove_intr(dev, (uint_t)0, esp->e_iblock);
   1229 
   1230 #ifdef ESP_KSTATS
   1231 	/*
   1232 	 * Remove kstats if any i.e., if pointer non-NULL.
   1233 	 * Note: pointer NOT explicitly NULL'ed. But buffer zalloc'd
   1234 	 */
   1235 	if (esp->e_scsi_bus_stats != (struct kstat *)NULL) {
   1236 		kmutex_t *lp = esp->e_scsi_bus_stats->ks_lock;
   1237 		if ((lp != (kmutex_t *)NULL) && !MUTEX_HELD(lp))
   1238 			kstat_delete(esp->e_scsi_bus_stats);
   1239 	}
   1240 #endif /* ESP_KSTATS */
   1241 
   1242 	/*
   1243 	 * deallocate reset notify callback list
   1244 	 */
   1245 	scsi_hba_reset_notify_tear_down(esp->e_reset_notify_listf);
   1246 
   1247 	/*
   1248 	 * Remove device instance from the global linked list
   1249 	 */
   1250 	rw_enter(&esp_global_rwlock, RW_WRITER);
   1251 
   1252 	if (esp_softc == esp) {
   1253 		e = esp_softc = esp->e_next;
   1254 	} else {
   1255 		for (e = esp_softc; e != (struct esp *)NULL; e = e->e_next) {
   1256 			if (e->e_next == esp) {
   1257 				e->e_next = esp->e_next;
   1258 				break;
   1259 			}
   1260 		}
   1261 		if (e == (struct esp *)NULL) {
   1262 			/*
   1263 			 * Instance not in softc list. Since the
   1264 			 * instance is not there in softc list, don't
   1265 			 * enable interrupts, the instance is effectively
   1266 			 * unusable.
   1267 			 */
   1268 			cmn_err(CE_WARN, "esp_dr_detach: esp instance not"
   1269 			    " in softc list!");
   1270 			rw_exit(&esp_global_rwlock);
   1271 			return (DDI_FAILURE);
   1272 		}
   1273 	}
   1274 
   1275 	if (esp_tail == esp)
   1276 		esp_tail = e;
   1277 
   1278 	rw_exit(&esp_global_rwlock);
   1279 
   1280 	if (esp->e_intr_kstat)
   1281 		kstat_delete(esp->e_intr_kstat);
   1282 
   1283 	/*
   1284 	 * disallow timeout thread rescheduling
   1285 	 */
   1286 	mutex_enter(&esp_global_mutex);
   1287 	esp->e_flags |= ESP_FLG_NOTIMEOUTS;
   1288 	mutex_exit(&esp_global_mutex);
   1289 
   1290 	/*
   1291 	 * last esp? ... if active, CANCEL watch threads.
   1292 	 */
   1293 	if (esp_softc == (struct esp *)NULL) {
   1294 		mutex_enter(&esp_global_mutex);
   1295 		if (esp_timeout_initted) {
   1296 			timeout_id_t tid = esp_timeout_id;
   1297 			esp_timeout_initted = 0;
   1298 			esp_timeout_id = 0;		/* don't resched */
   1299 			mutex_exit(&esp_global_mutex);
   1300 			(void) untimeout(tid);
   1301 			mutex_enter(&esp_global_mutex);
   1302 		}
   1303 
   1304 		if (esp_reset_watch) {
   1305 			mutex_exit(&esp_global_mutex);
   1306 			(void) untimeout(esp_reset_watch);
   1307 			mutex_enter(&esp_global_mutex);
   1308 			esp_reset_watch = 0;
   1309 		}
   1310 		mutex_exit(&esp_global_mutex);
   1311 	}
   1312 
   1313 	if (esp->e_restart_cmd_timeid) {
   1314 		(void) untimeout(esp->e_restart_cmd_timeid);
   1315 		esp->e_restart_cmd_timeid = 0;
   1316 	}
   1317 
   1318 	/*
   1319 	 * destroy outstanding ARQ pkts
   1320 	 */
   1321 	for (i = 0; i < NTARGETS; i++) {
   1322 		for (j = 0; j < NLUNS_PER_TARGET; j++) {
   1323 			int slot = i * NLUNS_PER_TARGET | j;
   1324 			if (esp->e_arq_pkt[slot]) {
   1325 				struct scsi_address	sa;
   1326 				sa.a_hba_tran = NULL;	/* not used */
   1327 				sa.a_target = (ushort_t)i;
   1328 				sa.a_lun = (uchar_t)j;
   1329 				(void) esp_create_arq_pkt(esp, &sa, 0);
   1330 			}
   1331 		}
   1332 	}
   1333 
   1334 	/*
   1335 	 * destroy any outstanding tagged command info
   1336 	 */
   1337 	for (i = 0; i < N_SLOTS; i++) {
   1338 		struct t_slots *active = esp->e_tagQ[i];
   1339 		if (active) {
   1340 			for (j = 0; j < NTAGS; j++) {
   1341 				struct esp_cmd *sp = active->t_slot[j];
   1342 				if (sp) {
   1343 					struct scsi_pkt *pkt = &sp->cmd_pkt;
   1344 					if (pkt) {
   1345 						esp_scsi_destroy_pkt(
   1346 						    &pkt->pkt_address, pkt);
   1347 					}
   1348 					/* sp freed in esp_scsi_destroy_pkt */
   1349 					active->t_slot[j] = NULL;
   1350 				}
   1351 			}
   1352 			kmem_free(active, sizeof (struct t_slots));
   1353 			esp->e_tagQ[i] = NULL;
   1354 		}
   1355 		ASSERT(esp->e_tcmds[i] == 0);
   1356 	}
   1357 
   1358 	/*
   1359 	 * Remove device MT locks
   1360 	 */
   1361 	mutex_destroy(&esp->e_startQ_mutex);
   1362 	mutex_destroy(ESP_MUTEX);
   1363 
   1364 	/*
   1365 	 * Release miscellaneous device resources
   1366 	 */
   1367 	if (esp->e_kmem_cache) {
   1368 		kmem_cache_destroy(esp->e_kmem_cache);
   1369 	}
   1370 
   1371 	if (esp->e_cmdarea != (uchar_t *)NULL) {
   1372 		(void) ddi_dma_unbind_handle(esp->e_dmahandle);
   1373 		ddi_dma_mem_free(&esp->e_cmdarea_acc_handle);
   1374 	}
   1375 
   1376 	if (esp->e_dmahandle != NULL)
   1377 		ddi_dma_free_handle(&esp->e_dmahandle);
   1378 
   1379 	if (esp->e_dma != (struct dmaga *)NULL)
   1380 		dma_free((struct dmaga *)esp->e_dma);
   1381 
   1382 	ddi_regs_map_free(&esp->e_regs_acc_handle);
   1383 
   1384 	esp_destroy_callback_thread(esp);
   1385 
   1386 	/*
   1387 	 * Process shared callback resources, as required.
   1388 	 * Update callback_thread bookkeeping.
   1389 	 */
   1390 	ddi_soft_state_free(esp_state, ddi_get_instance(dev));
   1391 
   1392 	/*
   1393 	 * Remove properties created during attach()
   1394 	 */
   1395 	ddi_prop_remove_all(dev);
   1396 
   1397 	/*
   1398 	 * Delete the DMA limits, transport vectors and remove the device
   1399 	 * links to the scsi_transport layer.
   1400 	 *	-- ddi_set_driver_private(dip, NULL)
   1401 	 */
   1402 	(void) scsi_hba_detach(dev);
   1403 
   1404 	/*
   1405 	 * Free the scsi_transport structure for this device.
   1406 	 */
   1407 	scsi_hba_tran_free(tran);
   1408 
   1409 	return (DDI_SUCCESS);
   1410 }
   1411 
   1412 /*
   1413  * Hardware and Software internal reset routines
   1414  */
   1415 static void
   1416 esp_determine_chip_type(struct esp *esp)
   1417 {
   1418 	int i;
   1419 	uchar_t clock_conv;
   1420 	clock_t ticks;
   1421 	volatile struct espreg *ep = esp->e_reg;
   1422 
   1423 	if (esp->e_scsi_options & SCSI_OPTIONS_PARITY)
   1424 		esp->e_espconf |= ESP_CONF_PAREN;
   1425 
   1426 	/*
   1427 	 * Determine clock frequency of attached ESP chip.
   1428 	 */
   1429 	i = ddi_prop_get_int(DDI_DEV_T_ANY, esp->e_dev, 0, prop_cfreq, -1);
   1430 
   1431 	/*
   1432 	 * Valid clock freqs. are between 10 and 40 MHz.  Otherwise
   1433 	 * presume 20 MHz. and complain.  (Notice, that we wrap to
   1434 	 * zero at 40 MHz.  Ick!)  This test should NEVER fail!
   1435 	 *
   1436 	 *	freq (MHz)	clock conversion factor
   1437 	 *	10		2
   1438 	 *	10.01-15	3
   1439 	 *	15.01-20	4
   1440 	 *	20.01-25	5
   1441 	 *	25.01-30	6
   1442 	 *	30.01-35	7
   1443 	 *	35.01-40	8 (0)
   1444 	 */
   1445 	if (i > FIVE_MEG) {
   1446 		clock_conv = (i + FIVE_MEG - 1)/ FIVE_MEG;
   1447 	} else {
   1448 		clock_conv = 0;
   1449 	}
   1450 	if (clock_conv < CLOCK_10MHZ || clock_conv > CLOCK_40MHZ) {
   1451 		esplog(esp, CE_WARN,
   1452 		    "Bad clock frequency- setting 20mhz, asynchronous mode");
   1453 		esp->e_weak = 0xff;
   1454 		clock_conv = CLOCK_20MHZ;
   1455 		i = TWENTY_MEG;
   1456 	}
   1457 
   1458 	esp->e_clock_conv = clock_conv;
   1459 	esp->e_clock_cycle = CLOCK_PERIOD(i);
   1460 	ticks = ESP_CLOCK_TICK(esp);
   1461 	esp->e_stval = ESP_CLOCK_TIMEOUT(ticks, esp_selection_timeout);
   1462 
   1463 	IPRINTF5("%d mhz, clock_conv %d, clock_cycle %d, ticks %ld, stval %d\n",
   1464 	    i, esp->e_clock_conv, esp->e_clock_cycle,
   1465 	    ticks, esp->e_stval);
   1466 
   1467 	ep->esp_conf2 = 0;
   1468 	ep->esp_conf2 = 0xa;
   1469 	if ((ep->esp_conf2 & 0xf) == 0xa) {
   1470 		esp->e_espconf2 = (uchar_t)ESP_CONF2_SCSI2;
   1471 		ep->esp_conf3 = 0;
   1472 		ep->esp_conf3 = 5;
   1473 		if (ep->esp_conf3 == 0x5) {
   1474 			for (i = 0; i < NTARGETS; i++) {
   1475 				esp->e_espconf3[i] = 0;
   1476 			}
   1477 			if (clock_conv > CLOCK_25MHZ) {
   1478 				/*
   1479 				 * do not enable FENABLE when using
   1480 				 * stacked cmds
   1481 				 * esp->e_espconf2 |= ESP_CONF2_FENABLE;
   1482 				 */
   1483 				ep->esp_conf2 = esp->e_espconf2;
   1484 				esp->e_type = FAST;
   1485 				IPRINTF("found FAST\n");
   1486 			} else {
   1487 				ep->esp_conf2 = esp->e_espconf2;
   1488 				esp->e_type = ESP236;
   1489 			}
   1490 			ep->esp_conf3 = 0;
   1491 		} else {
   1492 			ep->esp_conf2 = esp->e_espconf2;
   1493 			esp->e_type = ESP100A;
   1494 		}
   1495 	} else {
   1496 		esp->e_type = ESP100;
   1497 	}
   1498 
   1499 	for (i = 0; i < NTARGETS; i++) {
   1500 		if (esp->e_target_scsi_options[i] & SCSI_OPTIONS_FAST) {
   1501 			esp->e_default_period[i] = (uchar_t)
   1502 			    MIN_SYNC_PERIOD(esp);
   1503 		} else {
   1504 			esp->e_default_period[i] =
   1505 			    (uchar_t)CONVERT_PERIOD(DEFAULT_SYNC_PERIOD);
   1506 		}
   1507 	}
   1508 
   1509 	New_state(esp, ACTS_RESET);
   1510 
   1511 	/*
   1512 	 * Avoid resetting the scsi bus since this causes a few seconds
   1513 	 * delay per esp in boot and also causes busy conditions in some
   1514 	 * tape devices.
   1515 	 * we assume that with FAS devices, we probably have OBP 2.0 or
   1516 	 * higher which resets the bus before booting.
   1517 	 * worst case, we hang during the first probe and reset then
   1518 	 */
   1519 	if ((esp->e_type == FAST) && (esp->e_weak == 0)) {
   1520 		esp_internal_reset(esp,
   1521 		    ESP_RESET_SOFTC|ESP_RESET_ESP|ESP_RESET_DMA);
   1522 	} else {
   1523 		esp_internal_reset(esp, ESP_RESET_ALL);
   1524 	}
   1525 }
   1526 
   1527 static void
   1528 esp_flush_fifo(struct esp *esp)
   1529 {
   1530 	Esp_cmd(esp, CMD_FLUSH);
   1531 
   1532 	if (esp->e_options & ESP_OPT_SLOW_FIFO_FLUSH) {
   1533 		int i;
   1534 		for (i = 0; i < 1000; i++) {
   1535 			if (FIFO_CNT(esp->e_reg) == 0) {
   1536 				break;
   1537 			}
   1538 			drv_usecwait(1);
   1539 		}
   1540 		if (i >= 1000) {
   1541 			esplog(esp, CE_WARN, "fifo didn't flush\n");
   1542 		}
   1543 	}
   1544 }
   1545 
   1546 
   1547 static void
   1548 esp_internal_reset(struct esp *esp, int reset_action)
   1549 {
   1550 	if (reset_action & ESP_RESET_HW) {
   1551 		esp_hw_reset(esp, reset_action);
   1552 	}
   1553 
   1554 	if (reset_action & ESP_RESET_SOFTC) {
   1555 		esp->e_last_slot = esp->e_cur_slot;
   1556 		esp->e_cur_slot = UNDEFINED;
   1557 		bzero(esp->e_slots, (sizeof (struct esp_cmd *)) * N_SLOTS);
   1558 		bzero(esp->e_offset, NTARGETS * (sizeof (uchar_t)));
   1559 		bzero(esp->e_period, NTARGETS * (sizeof (uchar_t)));
   1560 		esp->e_sync_known = esp->e_omsglen = 0;
   1561 		esp->e_cur_msgout[0] = esp->e_last_msgout =
   1562 		    esp->e_last_msgin = INVALID_MSG;
   1563 		esp->e_espconf3_last = esp->e_offset_last =
   1564 		    esp->e_period_last = (uchar_t)-1;
   1565 
   1566 		/*
   1567 		 * esp->e_weak && esp->e_nodisc && ncmds && ndiscs  are
   1568 		 * preserved across softc resets.
   1569 		 */
   1570 		New_state(esp, STATE_FREE);
   1571 	}
   1572 	LOG_STATE(esp, ACTS_RESET, esp->e_stat, -1, reset_action);
   1573 }
   1574 
   1575 static void
   1576 esp_hw_reset(struct esp *esp, int action)
   1577 {
   1578 	volatile struct espreg *ep = esp->e_reg;
   1579 	volatile struct dmaga *dmar = esp->e_dma;
   1580 	uchar_t junk, i;
   1581 	int sbus_reruns;
   1582 
   1583 	/*
   1584 	 * never reset the dmaga while a request pending; this
   1585 	 * may cause a hang in xbox if there was a rerun pending
   1586 	 */
   1587 	if (action & ESP_RESET_SCSIBUS) {
   1588 		Esp_cmd(esp, CMD_RESET_SCSI);
   1589 		if (esp_watchdog_running && !panicstr) {
   1590 			int i;
   1591 
   1592 			esp_set_throttles(esp, 0, N_SLOTS, HOLD_THROTTLE);
   1593 			for (i = 0; i < NTARGETS; i++) {
   1594 				esp->e_reset_delay[i] =
   1595 				    esp->e_scsi_reset_delay;
   1596 			}
   1597 			esp_start_watch_reset_delay(esp);
   1598 		} else {
   1599 			drv_usecwait(esp->e_scsi_reset_delay * 1000);
   1600 		}
   1601 		ESP_FLUSH_DMA(esp);
   1602 	}
   1603 
   1604 	if (action & ESP_RESET_DMA) {
   1605 		int burstsizes = esp->e_dma_attr->dma_attr_burstsizes;
   1606 		burstsizes &= (ddi_dma_burstsizes(esp->e_dmahandle) &
   1607 		    esp_burst_sizes_limit);
   1608 
   1609 		ESP_FLUSH_DMA(esp);
   1610 		dmar->dmaga_csr = DMAGA_RESET;
   1611 		dmar->dmaga_csr &= ~DMAGA_RESET; /* clear it */
   1612 
   1613 		switch (ESP_DMAGA_REV(esp)) {
   1614 		case ESC1_REV1:
   1615 			sbus_reruns =
   1616 			    ddi_prop_exists(DDI_DEV_T_ANY, esp->e_dev, 0,
   1617 			    "reruns");
   1618 			if (sbus_reruns) {
   1619 				esp->e_options |= ESP_OPT_SBUS_RERUNS;
   1620 			}
   1621 			IPRINTF2("DMA Rev: 0x%x with %s\n", ESP_DMAGA_REV(esp),
   1622 			    sbus_reruns ? "SBus Reruns" : "No SBus Reruns");
   1623 
   1624 			if (!(burstsizes & BURST32)) {
   1625 				IPRINTF("16 byte burstsize\n");
   1626 				DMAESC_SETBURST16(dmar);
   1627 			}
   1628 			dmar->dmaga_csr |= DMAESC_EN_ADD;
   1629 			break;
   1630 
   1631 		case DMA_REV2:
   1632 			if (esp->e_type != ESP100)
   1633 				dmar->dmaga_csr |= DMAGA_TURBO;
   1634 			break;
   1635 
   1636 		case DMA_REV3:
   1637 			dmar->dmaga_csr &= ~DMAGA_TURBO;
   1638 			dmar->dmaga_csr |= DMAGA_TWO_CYCLE;
   1639 
   1640 			if (burstsizes & BURST32) {
   1641 				IPRINTF("32 byte burstsize\n");
   1642 				DMA2_SETBURST32(dmar);
   1643 			}
   1644 			break;
   1645 
   1646 		default:
   1647 			break;
   1648 		}
   1649 	}
   1650 
   1651 	dmar->dmaga_csr = esp->e_dmaga_csr = dmar->dmaga_csr | DMAGA_INTEN;
   1652 
   1653 	if (action & ESP_RESET_ESP) {
   1654 		/*
   1655 		 * according to Emulex, 2 NOPs with DMA are required here
   1656 		 * (essential for FAS101; id_code is unreliable if we don't
   1657 		 * do this)
   1658 		 */
   1659 		ESP_FLUSH_DMA(esp);
   1660 		Esp_cmd(esp, CMD_RESET_ESP);	/* hard-reset ESP chip */
   1661 		Esp_cmd(esp, CMD_NOP | CMD_DMA);
   1662 		Esp_cmd(esp, CMD_NOP | CMD_DMA);
   1663 
   1664 		/*
   1665 		 * Re-load chip configurations
   1666 		 */
   1667 		ep->esp_clock_conv = esp->e_clock_conv & CLOCK_MASK;
   1668 		ep->esp_timeout = esp->e_stval;
   1669 		ep->esp_sync_period = 0;
   1670 		ep->esp_sync_offset = 0;
   1671 
   1672 		/*
   1673 		 * enable default configurations
   1674 		 */
   1675 		if (esp->e_type == FAST) {
   1676 			uchar_t fcode;
   1677 
   1678 			esp->e_idcode = ep->esp_id_code;
   1679 			fcode =
   1680 			    (uchar_t)(ep->esp_id_code & ESP_FCODE_MASK)>>
   1681 			    (uchar_t)3;
   1682 			if (fcode == ESP_FAS236) {
   1683 				esp->e_type = FAS236;
   1684 			} else {
   1685 				esp->e_type = FAS100A;
   1686 			}
   1687 			IPRINTF2("Family code %d, revision %d\n",
   1688 			    fcode, (esp->e_idcode & ESP_REV_MASK));
   1689 		}
   1690 
   1691 		ep->esp_conf = esp->e_espconf;
   1692 		switch (esp->e_type) {
   1693 		case FAS236:
   1694 			/*
   1695 			 * used on DSBE, FSBE, galaxies
   1696 			 */
   1697 			IPRINTF("type is FAS236\n");
   1698 			for (i = 0; i < NTARGETS; i++) {
   1699 				esp->e_espconf3[i] |= ESP_CONF3_236_FASTCLK;
   1700 			}
   1701 			ep->esp_conf3 = esp->e_espconf3[0];
   1702 			esp->e_espconf3_fastscsi = ESP_CONF3_236_FASTSCSI;
   1703 			ep->esp_conf2 = esp->e_espconf2;
   1704 
   1705 			/*
   1706 			 * check if differential scsi bus; if so then no
   1707 			 * req/ack delay desired
   1708 			 */
   1709 			if (ddi_prop_get_int(DDI_DEV_T_ANY, esp->e_dev,
   1710 			    DDI_PROP_DONTPASS, "differential", 0)) {
   1711 				IPRINTF("differential scsibus\n");
   1712 				esp->e_req_ack_delay = 0;
   1713 				esp->e_options |= ESP_OPT_DIFFERENTIAL;
   1714 			} else {
   1715 				esp->e_req_ack_delay =
   1716 				    DEFAULT_REQ_ACK_DELAY_236;
   1717 			}
   1718 			if ((uchar_t)(ep->esp_id_code & ESP_REV_MASK)
   1719 			    > (uchar_t)2) {
   1720 				IPRINTF1("FAS236 rev=%x Stack_cmds DISABLED\n",
   1721 				    (uchar_t)(ep->esp_id_code & ESP_REV_MASK));
   1722 				esp->e_options |= ESP_OPT_DMA_OUT_TAG
   1723 				    | ESP_OPT_FAS;
   1724 			} else {
   1725 				IPRINTF1("FAS236 rev=%x Stack_cmds ENABLED\n",
   1726 				    (uchar_t)(ep->esp_id_code & ESP_REV_MASK));
   1727 				esp->e_options |= ESP_OPT_DMA_OUT_TAG
   1728 				    | ESP_OPT_FAS | ESP_OPT_STACKED_CMDS;
   1729 			}
   1730 			break;
   1731 
   1732 		case FAS100A:
   1733 			/*
   1734 			 * used on all desktop sun4m machines (macio)
   1735 			 */
   1736 			IPRINTF("type is FAS100A or 101A\n");
   1737 			for (i = 0; i < NTARGETS; i++) {
   1738 				esp->e_espconf3[i] |= ESP_CONF3_100A_FASTCLK;
   1739 			}
   1740 			ep->esp_conf3 = esp->e_espconf3[0];
   1741 			esp->e_espconf3_fastscsi = ESP_CONF3_100A_FASTSCSI;
   1742 			ep->esp_conf2 = esp->e_espconf2;
   1743 			esp->e_req_ack_delay = DEFAULT_REQ_ACK_DELAY_101;
   1744 			esp->e_options |= ESP_OPT_DMA_OUT_TAG | ESP_OPT_FAS |
   1745 			    ESP_OPT_ACCEPT_STEP567;
   1746 			break;
   1747 
   1748 		case ESP236:
   1749 			/*
   1750 			 * used on galaxies, SBE
   1751 			 */
   1752 			IPRINTF("type is ESP236\n");
   1753 			ep->esp_conf2 = esp->e_espconf2;
   1754 			ep->esp_conf3 = esp->e_espconf3[0];
   1755 			esp->e_options |= ESP_OPT_DMA_OUT_TAG |
   1756 			    ESP_OPT_SLOW_FIFO_FLUSH;
   1757 			break;
   1758 
   1759 		case ESP100A:
   1760 			/*
   1761 			 * used on SS2, IPX, sport8
   1762 			 */
   1763 			IPRINTF("type is ESP100A\n");
   1764 			ep->esp_conf2 = esp->e_espconf2;
   1765 			esp->e_options |= ESP_OPT_DMA_OUT_TAG |
   1766 			    ESP_OPT_MASK_OFF_STAT |
   1767 			    ESP_OPT_ACCEPT_STEP567;
   1768 			break;
   1769 
   1770 		case ESP100:
   1771 			/*
   1772 			 * used on SS1, SS1+, IPC
   1773 			 */
   1774 			IPRINTF("type is ESP100\n");
   1775 			IPRINTF("disable sync mode\n");
   1776 			esp->e_weak = 0xff;
   1777 			esp->e_options |= ESP_OPT_MASK_OFF_STAT;
   1778 			break;
   1779 
   1780 		default:
   1781 			IPRINTF("type is ???\n");
   1782 			break;
   1783 		}
   1784 
   1785 		/*
   1786 		 * look up esp-options property
   1787 		 */
   1788 		esp->e_options = ddi_prop_get_int(DDI_DEV_T_ANY,
   1789 		    esp->e_dev, 0, "esp-options", esp->e_options);
   1790 
   1791 		esplog(esp, CE_NOTE, "?esp-options=0x%x\n", esp->e_options);
   1792 
   1793 		/*
   1794 		 * Just in case...
   1795 		 * clear interrupt
   1796 		 */
   1797 		junk = ep->esp_intr;
   1798 
   1799 		IPRINTF1("clock conversion = %x\n", esp->e_clock_conv);
   1800 		IPRINTF2("conf = %x (%x)\n", esp->e_espconf, ep->esp_conf);
   1801 		if (esp->e_type > ESP100) {
   1802 			IPRINTF2("conf2 = %x (%x)\n",
   1803 			    esp->e_espconf2, ep->esp_conf2);
   1804 		}
   1805 		if (esp->e_type > ESP100A) {
   1806 			EPRINTF1("conf3=%x (read back)\n", ep->esp_conf3);
   1807 			EPRINTF4("conf3 (for target 0 - 3) = %x %x %x %x\n",
   1808 			    esp->e_espconf3[0], esp->e_espconf3[1],
   1809 			    esp->e_espconf3[2], esp->e_espconf3[3]);
   1810 			EPRINTF3("conf3 (for target 4 - 6) = %x %x %x\n",
   1811 			    esp->e_espconf3[4],
   1812 			    esp->e_espconf3[5], esp->e_espconf3[6]);
   1813 			EPRINTF2("req_ack_delay (0x%p) = %x\n",
   1814 			    (void *)&esp->e_req_ack_delay,
   1815 			    esp->e_req_ack_delay);
   1816 
   1817 		}
   1818 	}
   1819 
   1820 #ifdef	lint
   1821 	junk = junk;
   1822 #endif	/* lint */
   1823 }
   1824 
   1825 /*
   1826  * create a thread that performs the callbacks and init associated cv and mutex
   1827  *
   1828  * callback tunables:
   1829  */
   1830 static int esp_n_esps_per_callback_thread = 4;
   1831 static uchar_t esp_max_spawn = 2;	/* max of 2 extra threads for 4 esps */
   1832 static uchar_t esp_cb_now_qlen = 5;
   1833 static int esp_hi_cb_load = 50;		/* high watermark */
   1834 static int esp_lo_cb_load = 2;		/* low watermark */
   1835 static int esp_cb_load_count = 25;
   1836 
   1837 static int esp_n_callback_threads = 0;
   1838 static struct callback_info  *last_esp_callback_info;
   1839 
   1840 static void
   1841 esp_create_callback_thread(struct esp *esp)
   1842 {
   1843 	ASSERT(mutex_owned(&esp_global_mutex));
   1844 	ASSERT(esp->e_callback_info == NULL);
   1845 
   1846 	if ((esp_n_esps++ % esp_n_esps_per_callback_thread) == 0) {
   1847 		kthread_t *t;
   1848 		struct callback_info  *cb_info;
   1849 
   1850 		/*
   1851 		 * create another thread
   1852 		 */
   1853 		IPRINTF1("create callback thread %d\n", esp_n_callback_threads);
   1854 
   1855 		cb_info = kmem_zalloc(sizeof (struct callback_info), KM_SLEEP);
   1856 
   1857 		cv_init(&cb_info->c_cv, NULL, CV_DRIVER, NULL);
   1858 		cv_init(&cb_info->c_cvd, NULL, CV_DRIVER, NULL);
   1859 
   1860 		mutex_init(&cb_info->c_mutex, NULL, MUTEX_DRIVER,
   1861 		    esp->e_iblock);
   1862 
   1863 		cb_info->c_id = esp_n_callback_threads++;
   1864 		if (last_esp_callback_info) {
   1865 			last_esp_callback_info->c_next = cb_info;
   1866 		}
   1867 		last_esp_callback_info = esp->e_callback_info = cb_info;
   1868 
   1869 		t = thread_create(NULL, 0, esp_callback, esp, 0, &p0,
   1870 		    TS_RUN, v.v_maxsyspri - 2);
   1871 
   1872 		cb_info->c_thread = t;
   1873 		cb_info->c_spawned = esp_max_spawn;
   1874 		cb_info->c_cb_now_qlen = esp_cb_now_qlen;
   1875 	} else {
   1876 		ASSERT(last_esp_callback_info != NULL);
   1877 		IPRINTF1("sharing callback thread %d\n",
   1878 		    last_esp_callback_info->c_id);
   1879 		esp->e_callback_info = last_esp_callback_info;
   1880 	}
   1881 }
   1882 
   1883 static void
   1884 esp_destroy_callback_thread(struct esp *esp)
   1885 {
   1886 	struct esp	*e;
   1887 
   1888 	ASSERT(esp->e_callback_info != NULL);
   1889 
   1890 	/*
   1891 	 * Remove callback
   1892 	 *
   1893 	 * We have to see if we are the last one using this cb thread
   1894 	 * before deleting it.
   1895 	 * Check the list for others referencing this cb.  We are off
   1896 	 * the list, so finding one other reference indicates shared.
   1897 	 */
   1898 	rw_enter(&esp_global_rwlock, RW_READER);
   1899 	for (e = esp_softc; e != (struct esp *)NULL; e = e->e_next) {
   1900 		if (e->e_callback_info == esp->e_callback_info) {
   1901 			break;
   1902 		}
   1903 	}
   1904 	rw_exit(&esp_global_rwlock);
   1905 
   1906 	/*
   1907 	 * we couldn't find another esp sharing this cb
   1908 	 */
   1909 	if (!e) {
   1910 		struct callback_info	*ci = esp->e_callback_info;
   1911 		struct callback_info	*tci, **pci;
   1912 
   1913 	IPRINTF2("esp_destroy_callback_thread: "
   1914 	    "killing callback 0x%p thread %d\n",
   1915 	    (void *)esp->e_callback_info, ci->c_id);
   1916 		mutex_enter(&ci->c_mutex);
   1917 		ci->c_exit = 1;			/* die */
   1918 
   1919 		IPRINTF2("esp_destroy_callback_thread: spawned %d max %d\n",
   1920 		    ci->c_spawned, esp_max_spawn);
   1921 		while (ci->c_spawned <= (uchar_t)esp_max_spawn) {
   1922 			IPRINTF1("esp_destroy_callback_thread:%p wakeup\n",
   1923 			    (void *)ci);
   1924 			cv_broadcast(&ci->c_cv);	/* might be snoozing */
   1925 			cv_wait(&ci->c_cvd, &ci->c_mutex);
   1926 		}
   1927 
   1928 		mutex_exit(&ci->c_mutex);
   1929 		IPRINTF("esp_destroy_callback_thread: all threads killed\n");
   1930 
   1931 		mutex_enter(&esp_global_mutex);
   1932 		for (pci = &last_esp_callback_info;
   1933 		    (tci = *pci) != NULL; pci = &tci->c_next) {
   1934 			if (tci == ci) {
   1935 				/* take it out of list */
   1936 				*pci = tci->c_next;
   1937 				/* destroy it */
   1938 				cv_destroy(&ci->c_cv);
   1939 				cv_destroy(&ci->c_cvd);
   1940 				mutex_destroy(&ci->c_mutex);
   1941 				kmem_free(ci, sizeof (struct callback_info));
   1942 				IPRINTF1("esp_destroy_callback_thread:%p"
   1943 				    " freed\n", (void *)ci);
   1944 				esp->e_callback_info = NULL;
   1945 				break;
   1946 			}
   1947 		}
   1948 	} else {
   1949 		mutex_enter(&esp_global_mutex);
   1950 		IPRINTF1("esp_destroy_callback_thread: callback 0x%p shared\n",
   1951 		    (void *)esp->e_callback_info);
   1952 	}
   1953 
   1954 	esp_n_esps--;
   1955 	mutex_exit(&esp_global_mutex);
   1956 }
   1957 
   1958 /*
   1959  * this is the function executed by the callback thread; it
   1960  * empties the callback queue by calling the completion function of each
   1961  * packet; note the release of the mutex before
   1962  * calling the completion function
   1963  * the cv_wait is at the end of the loop because by the time this thread
   1964  * comes alive, there is already work to do.
   1965  */
   1966 void
   1967 esp_wakeup_callback_thread(struct callback_info *cb_info)
   1968 {
   1969 	struct esp_cmd *sp;
   1970 
   1971 	mutex_enter(&cb_info->c_mutex);
   1972 	if (cb_info->c_qlen) {
   1973 		/*
   1974 		 * callback now?
   1975 		 */
   1976 		if ((cb_info->c_qlen < cb_info->c_cb_now_qlen) || panicstr) {
   1977 			while (cb_info->c_qf) {
   1978 				sp = cb_info->c_qf;
   1979 				cb_info->c_qf = sp->cmd_forw;
   1980 				if (cb_info->c_qb == sp) {
   1981 					cb_info->c_qb = NULL;
   1982 				}
   1983 				cb_info->c_qlen--;
   1984 				mutex_exit(&cb_info->c_mutex);
   1985 				(*sp->cmd_pkt.pkt_comp)(&sp->cmd_pkt);
   1986 				mutex_enter(&cb_info->c_mutex);
   1987 			}
   1988 		/*
   1989 		 * if the queue is too long then do
   1990 		 * a wakeup for *all* callback threads
   1991 		 */
   1992 		} else if (cb_info->c_signal_needed) {
   1993 			cv_broadcast(&cb_info->c_cv);
   1994 		}
   1995 	}
   1996 	cb_info->c_signal_needed = 0;
   1997 	mutex_exit(&cb_info->c_mutex);
   1998 }
   1999 
   2000 /*
   2001  * Warlock has a problem when we use different locks
   2002  * on the same type of structure in different contexts.
   2003  * We use callb_cpr_t in both scsi_watch and esp_callback threads.
   2004  * we use different mutex's in different threads. And
   2005  * this is not acceptable to warlock. To avoid this
   2006  * problem we use the same name for the mutex in
   2007  * both scsi_watch & esp_callback. when __lock_lint is not defined
   2008  * esp_callback uses the mutex on the stack and in scsi_watch
   2009  * a static variable. But when __lock_lint is defined
   2010  * we make a mutex which is global in esp_callback and
   2011  * a external mutex for scsi_watch.
   2012  */
   2013 #ifdef __lock_lint
   2014 kmutex_t cpr_mutex;
   2015 #endif
   2016 
   2017 static void
   2018 esp_callback(struct esp *esp)
   2019 {
   2020 	struct esp_cmd *sp;
   2021 	struct callback_info *cb_info = esp->e_callback_info;
   2022 	int serviced = 0;
   2023 	int wakeups = 0;
   2024 	int id, load;
   2025 	int hiload = 0;
   2026 	int loload = 0;
   2027 	callb_cpr_t cpr_info;
   2028 #ifndef	__lock_lint
   2029 	kmutex_t cpr_mutex;
   2030 #endif
   2031 	int n = 0;
   2032 
   2033 	_NOTE(MUTEX_PROTECTS_DATA(cpr_mutex, cpr_info))
   2034 	_NOTE(NO_COMPETING_THREADS_NOW);
   2035 	mutex_init(&cpr_mutex, NULL, MUTEX_DRIVER, esp->e_iblock);
   2036 	CALLB_CPR_INIT(&cpr_info,
   2037 	    &cpr_mutex, callb_generic_cpr, "esp_callback");
   2038 #ifndef lint
   2039 	_NOTE(COMPETING_THREADS_NOW);
   2040 #endif
   2041 
   2042 	mutex_enter(&cb_info->c_mutex);
   2043 
   2044 	id = cb_info->c_count++;
   2045 #ifdef ESP_PERF
   2046 	cmn_err(CE_CONT,
   2047 	    "esp cb%d.%d thread starting\n", cb_info->c_id, id);
   2048 #endif
   2049 
   2050 	for (;;) {
   2051 		TRACE_0(TR_FAC_SCSI, TR_ESP_CALLBACK_START,
   2052 		    "esp_callback_start");
   2053 		while (cb_info->c_qf) {
   2054 			sp = cb_info->c_qf;
   2055 			cb_info->c_qf = sp->cmd_forw;
   2056 			if (cb_info->c_qb == sp) {
   2057 				cb_info->c_qb = NULL;
   2058 			}
   2059 			cb_info->c_qlen--;
   2060 			ASSERT(sp->cmd_pkt.pkt_comp != 0);
   2061 			serviced++;
   2062 			mutex_exit(&cb_info->c_mutex);
   2063 			(*sp->cmd_pkt.pkt_comp)(&sp->cmd_pkt);
   2064 			mutex_enter(&cb_info->c_mutex);
   2065 			n++;
   2066 		}
   2067 
   2068 		/*
   2069 		 * check load
   2070 		 * if the load is consistently too high, create another
   2071 		 * thread to help out
   2072 		 * if the load is consistently too low, exit thread
   2073 		 * If the load is so high that we never exit the
   2074 		 * above while loop then esp_n_esps_per_callback_thread is
   2075 		 * too high; we are not going to deal with that condition
   2076 		 * here
   2077 		 */
   2078 		if (wakeups) {
   2079 			load  = (serviced + wakeups - 1)/wakeups;
   2080 		} else {
   2081 			load = 0;
   2082 		}
   2083 
   2084 		if (cb_info->c_exit) {
   2085 			EPRINTF2("esp_callback: thread %d 0x%p exit set\n",
   2086 			    cb_info->c_id, (void *)cb_info);
   2087 			cb_info->c_spawned++;
   2088 			cv_broadcast(&cb_info->c_cvd);
   2089 			mutex_exit(&cb_info->c_mutex);
   2090 			break;
   2091 		} else if (load > esp_hi_cb_load) {
   2092 			/*
   2093 			 * load is too high
   2094 			 */
   2095 			if ((hiload++ > esp_cb_load_count) &&
   2096 			    (cb_info->c_spawned > 0)) {
   2097 				/*
   2098 				 * create another thread
   2099 				 */
   2100 				(void) thread_create(NULL, 0, esp_callback, esp,
   2101 				    0, &p0, TS_RUN, v.v_maxsyspri - 2);
   2102 				serviced = wakeups = 0;
   2103 				cb_info->c_spawned--;
   2104 				/*
   2105 				 * from now on do not allow immediate
   2106 				 * callback
   2107 				 */
   2108 				cb_info->c_cb_now_qlen = 0;
   2109 				hiload = loload = 0;
   2110 			}
   2111 		} else if (load < esp_lo_cb_load) {
   2112 			/*
   2113 			 * load is too low
   2114 			 */
   2115 			if (loload++ > esp_cb_load_count) {
   2116 				/*
   2117 				 * if this is not the first thread, exit
   2118 				 */
   2119 				if (id != 0) {
   2120 					cb_info->c_spawned++;
   2121 					mutex_exit(&cb_info->c_mutex);
   2122 					/*
   2123 					 * exit while loop and esp_callback
   2124 					 * function which destroys the
   2125 					 * thread
   2126 					 */
   2127 					break;
   2128 				} else {
   2129 					/*
   2130 					 * if only 1 thread left then set
   2131 					 * back cb_now_qlen
   2132 					 */
   2133 					if (cb_info->c_spawned ==
   2134 					    esp_max_spawn) {
   2135 						cb_info->c_cb_now_qlen =
   2136 						    esp_cb_now_qlen;
   2137 					}
   2138 				}
   2139 				hiload = loload = 0;
   2140 			}
   2141 		} else {
   2142 			/*
   2143 			 * always use deferred callback from now on
   2144 			 */
   2145 			cb_info->c_cb_now_qlen = 0;
   2146 			hiload = loload = 0;
   2147 		}
   2148 
   2149 		TRACE_1(TR_FAC_SCSI, TR_ESP_CALLBACK_END,
   2150 		    "esp_callback_end: (%d)", serviced);
   2151 
   2152 		/*
   2153 		 * reset serviced and wakeups; if these numbers get too high
   2154 		 * then we don't adjust to bursts very well
   2155 		 */
   2156 		if (serviced >= 20000) {
   2157 #ifdef ESP_PERF
   2158 			cmn_err(CE_CONT,
   2159 	    "esp cb%d.%d: svced=%d, wkup=%d, ld=%d, spwn=%d, now_qlen=%d\n",
   2160 			    cb_info->c_id, id, serviced, wakeups,
   2161 			    load, cb_info->c_spawned,
   2162 			    cb_info->c_cb_now_qlen);
   2163 #endif
   2164 			serviced = 0;
   2165 			wakeups = 0;
   2166 		}
   2167 
   2168 		mutex_enter(&cpr_mutex);
   2169 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
   2170 		mutex_exit(&cpr_mutex);
   2171 
   2172 		cv_wait(&cb_info->c_cv, &cb_info->c_mutex);
   2173 
   2174 		mutex_exit(&cb_info->c_mutex);
   2175 		mutex_enter(&cpr_mutex);
   2176 		CALLB_CPR_SAFE_END(&cpr_info, &cpr_mutex);
   2177 		mutex_exit(&cpr_mutex);
   2178 		mutex_enter(&cb_info->c_mutex);
   2179 
   2180 		cb_info->c_signal_needed = 0;
   2181 		wakeups++;
   2182 	}
   2183 
   2184 #ifdef ESP_PERF
   2185 	cmn_err(CE_CONT, "esp cb%d.%d exits\n", cb_info->c_id, id);
   2186 #endif
   2187 	TRACE_1(TR_FAC_SCSI, TR_ESP_CALLBACK_END,
   2188 	    "esp_callback_end:  (%d)", n);
   2189 #ifndef __lock_lint
   2190 	mutex_enter(&cpr_mutex);
   2191 	CALLB_CPR_EXIT(&cpr_info);
   2192 #endif
   2193 	mutex_destroy(&cpr_mutex);
   2194 	thread_exit();
   2195 }
   2196 
   2197 /*
   2198  * Interface functions
   2199  *
   2200  * Visible to the external world via the transport structure.
   2201  *
   2202  * These functions have been grouped together to reduce cache misses.
   2203  *
   2204  */
   2205 /*ARGSUSED*/
   2206 static void
   2207 esp_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
   2208 {
   2209 	struct esp_cmd *cmd = (struct esp_cmd *)pkt->pkt_ha_private;
   2210 
   2211 	TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_DMAFREE_START,
   2212 	    "esp_scsi_dmafree_start");
   2213 
   2214 	if (cmd->cmd_flags & CFLAG_DMAVALID) {
   2215 		/*
   2216 		 * Free the mapping.
   2217 		 */
   2218 		(void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
   2219 		cmd->cmd_flags ^= CFLAG_DMAVALID;
   2220 	}
   2221 	TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_DMAFREE_END,
   2222 	    "esp_scsi_dmafree_end");
   2223 }
   2224 
   2225 
   2226 /*ARGSUSED*/
   2227 static void
   2228 esp_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
   2229 {
   2230 	int i;
   2231 	struct esp_cmd *sp = (struct esp_cmd *)pkt->pkt_ha_private;
   2232 
   2233 	if (sp->cmd_flags & CFLAG_DMAVALID) {
   2234 		i = ddi_dma_sync(sp->cmd_dmahandle, 0, 0,
   2235 		    (sp->cmd_flags & CFLAG_DMASEND) ?
   2236 		    DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
   2237 		if (i != DDI_SUCCESS) {
   2238 			cmn_err(CE_WARN, "esp: sync pkt failed");
   2239 		}
   2240 	}
   2241 }
   2242 
   2243 
   2244 static struct scsi_pkt *
   2245 esp_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
   2246 	struct buf *bp, int cmdlen, int statuslen, int tgtlen,
   2247 	int flags, int (*callback)(), caddr_t arg)
   2248 {
   2249 	int kf;
   2250 	int failure = 0;
   2251 	struct esp_cmd *cmd, *new_cmd;
   2252 	struct esp *esp = ADDR2ESP(ap);
   2253 	int rval;
   2254 
   2255 /* #define	ESP_TEST_EXTRN_ALLOC */
   2256 #ifdef ESP_TEST_EXTRN_ALLOC
   2257 	cmdlen *= 4; statuslen *= 4; tgtlen *= 4;
   2258 #endif
   2259 	/*
   2260 	 * If we've already allocated a pkt once,
   2261 	 * this request is for dma allocation only.
   2262 	 */
   2263 	if (pkt == NULL) {
   2264 		/*
   2265 		 * First step of esp_scsi_init_pkt:  pkt allocation
   2266 		 */
   2267 		TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_PKTALLOC_START,
   2268 		    "esp_scsi_pktalloc_start");
   2269 
   2270 		failure = 0;
   2271 		kf = (callback == SLEEP_FUNC)? KM_SLEEP: KM_NOSLEEP;
   2272 
   2273 		cmd = kmem_cache_alloc(esp->e_kmem_cache, kf);
   2274 
   2275 		if (cmd) {
   2276 			ddi_dma_handle_t save_dma_handle;
   2277 
   2278 			save_dma_handle = cmd->cmd_dmahandle;
   2279 			bzero(cmd, ESP_CMD_SIZE);
   2280 			cmd->cmd_dmahandle = save_dma_handle;
   2281 
   2282 			cmd->cmd_pkt.pkt_scbp = (opaque_t)cmd->cmd_scb;
   2283 			cmd->cmd_cdblen_alloc = cmd->cmd_cdblen =
   2284 			    (uchar_t)cmdlen;
   2285 			cmd->cmd_scblen		= statuslen;
   2286 			cmd->cmd_privlen	= tgtlen;
   2287 			cmd->cmd_pkt.pkt_address = *ap;
   2288 
   2289 			cmd->cmd_pkt.pkt_cdbp = (opaque_t)&cmd->cmd_cdb;
   2290 			cmd->cmd_pkt.pkt_private = cmd->cmd_pkt_private;
   2291 			cmd->cmd_pkt.pkt_ha_private = (opaque_t)cmd;
   2292 		} else {
   2293 			failure++;
   2294 		}
   2295 
   2296 		if (failure || (cmdlen > sizeof (cmd->cmd_cdb)) ||
   2297 		    (tgtlen > PKT_PRIV_LEN) ||
   2298 		    (statuslen > EXTCMDS_STATUS_SIZE)) {
   2299 			if (failure == 0) {
   2300 				failure = esp_pkt_alloc_extern(esp, cmd,
   2301 				    cmdlen, tgtlen, statuslen, kf);
   2302 			}
   2303 			if (failure) {
   2304 				TRACE_0(TR_FAC_SCSI,
   2305 				    TR_ESP_SCSI_IMPL_PKTALLOC_END,
   2306 				    "esp_scsi_pktalloc_end");
   2307 				return (NULL);
   2308 			}
   2309 		}
   2310 
   2311 		new_cmd = cmd;
   2312 
   2313 		TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_PKTALLOC_END,
   2314 		    "esp_scsi_pktalloc_end");
   2315 	} else {
   2316 		cmd = (struct esp_cmd *)pkt->pkt_ha_private;
   2317 		new_cmd = NULL;
   2318 	}
   2319 
   2320 
   2321 	/*
   2322 	 * Second step of esp_scsi_init_pkt:  dma allocation
   2323 	 * Set up dma info
   2324 	 */
   2325 	if (bp && bp->b_bcount) {
   2326 		uint_t cmd_flags, dma_flags;
   2327 		uint_t dmacookie_count;
   2328 
   2329 		TRACE_0(TR_FAC_SCSI, TR_SCSI_IMPL_DMAGET_START,
   2330 		    "esp_scsi_dmaget_start");
   2331 
   2332 		cmd_flags = cmd->cmd_flags;
   2333 
   2334 		if (bp->b_flags & B_READ) {
   2335 			cmd_flags &= ~CFLAG_DMASEND;
   2336 			dma_flags = DDI_DMA_READ | DDI_DMA_PARTIAL;
   2337 		} else {
   2338 			cmd_flags |= CFLAG_DMASEND;
   2339 			dma_flags = DDI_DMA_WRITE | DDI_DMA_PARTIAL;
   2340 		}
   2341 		if (flags & PKT_CONSISTENT) {
   2342 			cmd_flags |= CFLAG_CMDIOPB;
   2343 			dma_flags |= DDI_DMA_CONSISTENT;
   2344 		}
   2345 
   2346 		ASSERT(cmd->cmd_dmahandle != NULL);
   2347 
   2348 		rval = ddi_dma_buf_bind_handle(cmd->cmd_dmahandle, bp,
   2349 		    dma_flags, callback, arg, &cmd->cmd_dmacookie,
   2350 		    &dmacookie_count);
   2351 dma_failure:
   2352 		if (rval && rval != DDI_DMA_PARTIAL_MAP) {
   2353 			switch (rval) {
   2354 			case DDI_DMA_NORESOURCES:
   2355 				bioerror(bp, 0);
   2356 				break;
   2357 			case DDI_DMA_BADATTR:
   2358 			case DDI_DMA_NOMAPPING:
   2359 				bioerror(bp, EFAULT);
   2360 				break;
   2361 			case DDI_DMA_TOOBIG:
   2362 			default:
   2363 				bioerror(bp, EINVAL);
   2364 				break;
   2365 			}
   2366 			cmd->cmd_flags = cmd_flags & ~CFLAG_DMAVALID;
   2367 			if (new_cmd) {
   2368 				esp_scsi_destroy_pkt(ap, &new_cmd->cmd_pkt);
   2369 			}
   2370 			TRACE_0(TR_FAC_SCSI, TR_SCSI_IMPL_DMAGET_END,
   2371 			    "esp_scsi_dmaget_end");
   2372 			return ((struct scsi_pkt *)NULL);
   2373 		}
   2374 		ASSERT(dmacookie_count == 1);
   2375 		cmd->cmd_dmacount = bp->b_bcount;
   2376 		cmd->cmd_flags = cmd_flags | CFLAG_DMAVALID;
   2377 
   2378 		ASSERT(cmd->cmd_dmahandle != NULL);
   2379 		TRACE_0(TR_FAC_SCSI, TR_SCSI_IMPL_DMAGET_END,
   2380 		    "esp_scsi_dmaget_end");
   2381 	}
   2382 
   2383 	return (&cmd->cmd_pkt);
   2384 }
   2385 
   2386 /*
   2387  * allocate and deallocate external space (ie. not part of esp_cmd) for
   2388  * non-standard length cdb, pkt_private, status areas
   2389  */
   2390 /* ARGSUSED */
   2391 static int
   2392 esp_pkt_alloc_extern(struct esp *esp, struct esp_cmd *sp,
   2393     int cmdlen, int tgtlen, int statuslen, int kf)
   2394 {
   2395 	caddr_t cdbp, scbp, tgt;
   2396 	int failure = 0;
   2397 
   2398 	tgt = cdbp = scbp = NULL;
   2399 	if (cmdlen > sizeof (sp->cmd_cdb)) {
   2400 		if ((cdbp = kmem_zalloc((size_t)cmdlen, kf)) == NULL) {
   2401 			failure++;
   2402 		} else {
   2403 			sp->cmd_pkt.pkt_cdbp = (opaque_t)cdbp;
   2404 			sp->cmd_flags |= CFLAG_CDBEXTERN;
   2405 		}
   2406 	}
   2407 	if (tgtlen > PKT_PRIV_LEN) {
   2408 		if ((tgt = kmem_zalloc(tgtlen, kf)) == NULL) {
   2409 			failure++;
   2410 		} else {
   2411 			sp->cmd_flags |= CFLAG_PRIVEXTERN;
   2412 			sp->cmd_pkt.pkt_private = tgt;
   2413 		}
   2414 	}
   2415 	if (statuslen > EXTCMDS_STATUS_SIZE) {
   2416 		if ((scbp = kmem_zalloc((size_t)statuslen, kf)) == NULL) {
   2417 			failure++;
   2418 		} else {
   2419 			sp->cmd_flags |= CFLAG_SCBEXTERN;
   2420 			sp->cmd_pkt.pkt_scbp = (opaque_t)scbp;
   2421 		}
   2422 	}
   2423 	if (failure) {
   2424 		esp_pkt_destroy_extern(esp, sp);
   2425 	}
   2426 	return (failure);
   2427 }
   2428 
   2429 /* ARGSUSED */
   2430 static void
   2431 esp_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
   2432 {
   2433 	struct esp_cmd *sp = (struct esp_cmd *)pkt->pkt_ha_private;
   2434 	struct esp *esp = ADDR2ESP(ap);
   2435 
   2436 	/*
   2437 	 * esp_scsi_dmafree inline to speed things up
   2438 	 */
   2439 	TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_DMAFREE_START,
   2440 	    "esp_scsi_dmafree_start");
   2441 
   2442 	if (sp->cmd_flags & CFLAG_DMAVALID) {
   2443 		/*
   2444 		 * Free the mapping.
   2445 		 */
   2446 		(void) ddi_dma_unbind_handle(sp->cmd_dmahandle);
   2447 		sp->cmd_flags ^= CFLAG_DMAVALID;
   2448 	}
   2449 	TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_DMAFREE_END,
   2450 	    "esp_scsi_dmafree_end");
   2451 
   2452 	TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_PKTFREE_START,
   2453 	    "esp_scsi_pktfree_start");
   2454 
   2455 	/*
   2456 	 * first test the most common case
   2457 	 */
   2458 	if ((sp->cmd_flags &
   2459 	    (CFLAG_FREE | CFLAG_CDBEXTERN | CFLAG_PRIVEXTERN |
   2460 	    CFLAG_SCBEXTERN)) == 0) {
   2461 		sp->cmd_flags = CFLAG_FREE;
   2462 		kmem_cache_free(esp->e_kmem_cache, (void *)sp);
   2463 	} else {
   2464 		esp_pkt_destroy_extern(esp, sp);
   2465 	}
   2466 
   2467 	TRACE_0(TR_FAC_SCSI, TR_ESP_SCSI_IMPL_PKTFREE_END,
   2468 	    "esp_scsi_pktfree_end");
   2469 }
   2470 
   2471 /* ARGSUSED */
   2472 static void
   2473 esp_pkt_destroy_extern(struct esp *esp, struct esp_cmd *sp)
   2474 {
   2475 	if (sp->cmd_flags & CFLAG_FREE) {
   2476 		panic("esp_pkt_destroy(_extern): freeing free packet");
   2477 		_NOTE(NOT_REACHED)
   2478 		/*NOTREACHED*/
   2479 	}
   2480 	if (sp->cmd_flags & CFLAG_CDBEXTERN) {
   2481 		kmem_free((caddr_t)sp->cmd_pkt.pkt_cdbp,
   2482 		    (size_t)sp->cmd_cdblen_alloc);
   2483 	}
   2484 	if (sp->cmd_flags & CFLAG_SCBEXTERN) {
   2485 		kmem_free((caddr_t)sp->cmd_pkt.pkt_scbp,
   2486 		    (size_t)sp->cmd_scblen);
   2487 	}
   2488 	if (sp->cmd_flags & CFLAG_PRIVEXTERN) {
   2489 		kmem_free((caddr_t)sp->cmd_pkt.pkt_private,
   2490 		    (size_t)sp->cmd_privlen);
   2491 	}
   2492 	sp->cmd_flags = CFLAG_FREE;
   2493 	kmem_cache_free(esp->e_kmem_cache, (void *)sp);
   2494 }
   2495 
   2496 /*
   2497  * kmem cache constructor and destructor.
   2498  * When constructing, we bzero the cmd and allocate a handle
   2499  * When destructing, just free the dma handle
   2500  */
   2501 static int
   2502 esp_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags)
   2503 {
   2504 	struct esp_cmd *cmd = buf;
   2505 	struct esp *esp = cdrarg;
   2506 	int  (*callback)(caddr_t) = (kmflags == KM_SLEEP) ? DDI_DMA_SLEEP:
   2507 	    DDI_DMA_DONTWAIT;
   2508 
   2509 	bzero(cmd, ESP_CMD_SIZE);
   2510 
   2511 	if (ddi_dma_alloc_handle(esp->e_dev,
   2512 	    esp->e_dma_attr, callback, NULL,
   2513 	    &cmd->cmd_dmahandle) != 0) {
   2514 		return (-1);
   2515 	}
   2516 	return (0);
   2517 }
   2518 
   2519 /* ARGSUSED */
   2520 static void
   2521 esp_kmem_cache_destructor(void *buf, void *cdrarg)
   2522 {
   2523 	struct esp_cmd *cmd = buf;
   2524 	if (cmd->cmd_dmahandle) {
   2525 		ddi_dma_free_handle(&cmd->cmd_dmahandle);
   2526 	}
   2527 }
   2528 
   2529 /*
   2530  * esp_prepare_pkt():
   2531  * initialize the packet and do some sanity checks
   2532  * before taking the lock
   2533  */
   2534 static int
   2535 esp_prepare_pkt(struct esp *esp, struct esp_cmd *sp)
   2536 {
   2537 	int size, cmdlen;
   2538 
   2539 #ifdef ESPDEBUG
   2540 	if (sp->cmd_flags & CFLAG_DMAVALID) {
   2541 		uint32_t maxdma;
   2542 		switch (ESP_DMAGA_REV(esp)) {
   2543 		default:
   2544 		case DMA_REV1:
   2545 		case DMA_REV2:
   2546 		case ESC1_REV1:
   2547 			maxdma = 1 << 24;
   2548 			break;
   2549 		case DMA_REV3:
   2550 			maxdma = 1 << 30; /* be reasonable - 2gb is enuff */
   2551 			break;
   2552 		}
   2553 		if (sp->cmd_dmacount >= maxdma) {
   2554 			IPRINTF("prepare pkt: dma count too high\n");
   2555 			return (TRAN_BADPKT);
   2556 		}
   2557 	}
   2558 	ASSERT((sp->cmd_flags & CFLAG_IN_TRANSPORT) == 0);
   2559 #endif
   2560 
   2561 	/*
   2562 	 * Reinitialize some fields that need it; the packet may
   2563 	 * have been resubmitted
   2564 	 */
   2565 	sp->cmd_pkt.pkt_reason = CMD_CMPLT;
   2566 	sp->cmd_pkt.pkt_state = 0;
   2567 	sp->cmd_pkt.pkt_statistics = 0;
   2568 	sp->cmd_pkt.pkt_resid = 0;
   2569 	sp->cmd_age = 0;
   2570 
   2571 	/*
   2572 	 * Copy the cdb and scb pointers to the esp_cmd area as we
   2573 	 * modify these parameters.
   2574 	 */
   2575 	sp->cmd_cdbp = sp->cmd_pkt.pkt_cdbp;
   2576 	sp->cmd_scbp = sp->cmd_pkt.pkt_scbp;
   2577 	*(sp->cmd_scbp) = 0;
   2578 	sp->cmd_flags &= ~CFLAG_TRANFLAG;
   2579 	sp->cmd_flags |= CFLAG_IN_TRANSPORT;
   2580 
   2581 	if (sp->cmd_pkt.pkt_time != 0) {
   2582 		sp->cmd_flags |= CFLAG_WATCH;
   2583 	}
   2584 	sp->cmd_timeout = sp->cmd_pkt.pkt_time; /* Set timeout */
   2585 
   2586 	if (sp->cmd_flags & CFLAG_DMAVALID) {
   2587 		sp->cmd_pkt.pkt_resid = sp->cmd_dmacount;
   2588 
   2589 		/*
   2590 		 * if the pkt was resubmitted then the
   2591 		 * window may be at the wrong number
   2592 		 */
   2593 		if (sp->cmd_cur_win) {
   2594 			sp->cmd_cur_win = 0;
   2595 			if (esp_set_new_window(esp, sp)) {
   2596 				IPRINTF("cannot reset window\n");
   2597 				return (TRAN_BADPKT);
   2598 			}
   2599 		}
   2600 		sp->cmd_saved_cur_addr =
   2601 		    sp->cmd_cur_addr = sp->cmd_dmacookie.dmac_address;
   2602 
   2603 		/*
   2604 		 * the common case is just one window, we worry
   2605 		 * about multiple windows when we run out of the
   2606 		 * current window
   2607 		 */
   2608 		sp->cmd_nwin = sp->cmd_saved_win = 0;
   2609 		sp->cmd_data_count = sp->cmd_saved_data_count = 0;
   2610 
   2611 		if ((sp->cmd_flags & (CFLAG_CMDIOPB | CFLAG_DMASEND)) ==
   2612 		    (CFLAG_CMDIOPB | CFLAG_DMASEND)) {
   2613 			(void) ddi_dma_sync(sp->cmd_dmahandle, 0, (uint_t)-1,
   2614 			    DDI_DMA_SYNC_FORDEV);
   2615 		}
   2616 	}
   2617 
   2618 	/*
   2619 	 * The ESP chip only will automatically send 6, 10 or 12 byte
   2620 	 * cdb's.  Setting cmd_cdblen to a non-zero value signals this.
   2621 	 * Otherwise, we have to do it manually and send them out one at
   2622 	 * a time.  Setting cmd_cdblen to zero signals this condition.
   2623 	 * For non-group{0,1,2,5} cmds we use the cmdlen specified by
   2624 	 * the target driver if it is 6, 10, or 12.
   2625 	 */
   2626 	size = scsi_cdb_size[CDB_GROUPID(sp->cmd_cdbp[0])];
   2627 	cmdlen = sp->cmd_cdblen;
   2628 	if (size == 0 && (cmdlen != CDB_GROUP0 &&
   2629 	    cmdlen != CDB_GROUP1 && cmdlen != CDB_GROUP5)) {
   2630 		sp->cmd_cdblen = 0;
   2631 		IPRINTF("cdblen = 0\n");
   2632 	} else if (size != 0) {
   2633 		sp->cmd_cdblen = (uchar_t)size;
   2634 	}
   2635 
   2636 
   2637 #ifdef ESP_TEST_UNTAGGED
   2638 #ifndef __lock_lint
   2639 	if (esp_test_untagged > 0) {
   2640 		if (TAGGED(Tgt(sp))) {
   2641 			int slot = Tgt(sp) * NLUNS_PER_TARGET | Lun(sp);
   2642 			sp->cmd_pkt.pkt_flags &= ~FLAG_TAGMASK;
   2643 			sp->cmd_pkt.pkt_flags &= ~FLAG_NODISCON;
   2644 			sp->cmd_pkt.pkt_flags |= 0x80000000;
   2645 			esplog(esp, CE_NOTE,
   2646 			    "starting untagged cmd, target=%d,"
   2647 			    " tcmds=%d, sp=0x%p, throttle=%d\n",
   2648 			    Tgt(sp), esp->e_tcmds[slot], (void *)sp,
   2649 			    esp->e_throttle[slot]);
   2650 			esp_test_untagged = -10;
   2651 		}
   2652 	}
   2653 #endif
   2654 #endif
   2655 
   2656 
   2657 #ifdef ESPDEBUG
   2658 	if (NOTAG(Tgt(sp)) && (sp->cmd_pkt.pkt_flags & FLAG_TAGMASK)) {
   2659 		IPRINTF2("tagged packet for non-tagged target %d.%d\n",
   2660 		    Tgt(sp), Lun(sp));
   2661 		sp->cmd_pkt.pkt_flags &= ~FLAG_TAGMASK;
   2662 	}
   2663 
   2664 	/*
   2665 	 * the scsa spec states that it is an error to have no
   2666 	 * completion function when FLAG_NOINTR is not set
   2667 	 */
   2668 	if ((sp->cmd_pkt.pkt_comp == NULL) &&
   2669 	    ((sp->cmd_pkt.pkt_flags & FLAG_NOINTR) == 0)) {
   2670 		IPRINTF("intr packet with pkt_comp == 0\n");
   2671 		sp->cmd_flags &= ~CFLAG_IN_TRANSPORT;
   2672 		TRACE_0(TR_FAC_SCSI, TR_ESP_PREPARE_PKT_TRAN_BADPKT_END,
   2673 		    "esp_prepare_pkt_end (tran_badpkt)");
   2674 		return (TRAN_BADPKT);
   2675 	}
   2676 #endif /* ESPDEBUG */
   2677 
   2678 	if (((esp->e_target_scsi_options[Tgt(sp)] & SCSI_OPTIONS_DR) == 0) ||
   2679 	    (esp->e_nodisc & (Tgt(sp) << 1)))  {
   2680 		/*
   2681 		 * no need to reset tag bits since tag queuing will
   2682 		 * not be enabled if disconnects are disabled
   2683 		 */
   2684 		sp->cmd_pkt.pkt_flags |= FLAG_NODISCON;
   2685 	}
   2686 
   2687 	sp->cmd_flags |= CFLAG_PREPARED;
   2688 
   2689 	ASSERT(sp->cmd_flags & CFLAG_IN_TRANSPORT);
   2690 
   2691 	TRACE_0(TR_FAC_SCSI, TR_ESP_PREPARE_PKT_TRAN_ACCEPT_END,
   2692 	    "esp_prepare_pkt_end (tran_accept)");
   2693 	return (TRAN_ACCEPT);
   2694 }
   2695 
   2696 /*
   2697  * when the startQ is emptied, we cannot tolerate TRAN_BUSY.
   2698  * if the queue is not empty when the next request comes in esp_start
   2699  * the order of requests is not preserved
   2700  * if a transport busy condition occurs, we queue up startQ pkts in the ready
   2701  * queue; the disadvantage is that the target driver has initially
   2702  * the wrong value (too high) for the target queue but eventually
   2703  * it should get it right; there is not really a big performance hit here
   2704  */
   2705 static void
   2706 esp_empty_startQ(struct esp *esp)
   2707 {
   2708 	struct esp_cmd *sp;
   2709 	int rval;
   2710 
   2711 	ASSERT(mutex_owned(&esp->e_startQ_mutex));
   2712 
   2713 	TRACE_0(TR_FAC_SCSI, TR_ESP_EMPTY_STARTQ_START,
   2714 	    "esp_empty_startQ_start");
   2715 	while (esp->e_startf) {
   2716 		sp = esp->e_startf;
   2717 		esp->e_startf = sp->cmd_forw;
   2718 		if (esp->e_startb == sp) {
   2719 			esp->e_startb = NULL;
   2720 		}
   2721 		mutex_exit(&esp->e_startQ_mutex);
   2722 		rval = _esp_start(esp, sp, NO_TRAN_BUSY);
   2723 
   2724 		/*
   2725 		 * the request should have been accepted but if not,
   2726 		 * put it back on the head of startQ
   2727 		 * If the  packet was rejected for other reasons then
   2728 		 * complete it here
   2729 		 */
   2730 		if (rval != TRAN_ACCEPT) {
   2731 			if (rval != TRAN_BUSY) {
   2732 				if (sp->cmd_pkt.pkt_reason == CMD_CMPLT) {
   2733 					sp->cmd_pkt.pkt_reason = CMD_TRAN_ERR;
   2734 				}
   2735 				if (sp->cmd_pkt.pkt_comp) {
   2736 					mutex_exit(ESP_MUTEX);
   2737 					(*sp->cmd_pkt.pkt_comp)(&sp->cmd_pkt);
   2738 					mutex_enter(ESP_MUTEX);
   2739 				}
   2740 				mutex_enter(&esp->e_startQ_mutex);
   2741 				continue;
   2742 			}
   2743 			mutex_enter(&esp->e_startQ_mutex);
   2744 			if (esp->e_startf == NULL) {
   2745 				esp->e_startb = esp->e_startf = sp;
   2746 				sp->cmd_forw = NULL;
   2747 			} else {
   2748 				sp->cmd_forw = esp->e_startf;
   2749 				esp->e_startf = sp;
   2750 			}
   2751 			break;
   2752 		}
   2753 		mutex_enter(&esp->e_startQ_mutex);
   2754 	}
   2755 	TRACE_0(TR_FAC_SCSI, TR_ESP_EMPTY_STARTQ_END,
   2756 	    "esp_empty_startQ_end");
   2757 }
   2758 
   2759 /*
   2760  * emptying the startQ just before releasing ESP_MUTEX is
   2761  * tricky; there is a small window where we checked the
   2762  * startQ and emptied it but possibly due to a
   2763  * a kernel preemption, we don't release the ESP_MUTEX soon enough and
   2764  * esp_start() will not be able to get the ESP_MUTEX and exit
   2765  * The next cmd coming in or the next interrupt or esp_watch() would eventually
   2766  * empty the startQ, though
   2767  * Therefore, by releasing the ESP_MUTEX before releasing the startQ mutex,
   2768  * we prevent that esp_start() fills the startQ and then cannot get the
   2769  * ESP_MUTEX for emptying the startQ
   2770  *
   2771  * esp_start() - accept a esp_cmd
   2772  */
   2773 static int
   2774 esp_start(struct scsi_address *ap, struct scsi_pkt *pkt)
   2775 {
   2776 	struct esp_cmd *sp = (struct esp_cmd *)pkt->pkt_ha_private;
   2777 	struct esp *esp = ADDR2ESP(ap);
   2778 	int rval;
   2779 
   2780 	TRACE_0(TR_FAC_SCSI, TR_ESP_START_START, "esp_start_start");
   2781 
   2782 #ifdef ESP_PERF
   2783 	esp_ncmds_per_esp[CNUM]++;
   2784 #endif
   2785 #ifdef ESP_CHECK
   2786 	mutex_enter(ESP_MUTEX);
   2787 	esp_check_in_transport(esp, sp);
   2788 	mutex_exit(ESP_MUTEX);
   2789 #endif
   2790 
   2791 	/*
   2792 	 * prepare packet before taking the mutex
   2793 	 */
   2794 	rval = esp_prepare_pkt(esp, sp);
   2795 	if (rval != TRAN_ACCEPT) {
   2796 		TRACE_0(TR_FAC_SCSI, TR_ESP_START_PREPARE_PKT_END,
   2797 		    "esp_start_end (prepare_pkt)");
   2798 		return (rval);
   2799 	}
   2800 
   2801 	/*
   2802 	 * esp mutex can be held for a long time; therefore, if mutex is
   2803 	 * held, we queue the packet in a startQ; we now need to check
   2804 	 * the startQ on every mutex_exit(ESP_MUTEX);
   2805 	 * Don't put NOINTR cmds in startQ! Proxy cmds go directly
   2806 	 * to _esp_start
   2807 	 */
   2808 	if (sp->cmd_pkt.pkt_flags & FLAG_NOINTR) {
   2809 		mutex_enter(ESP_MUTEX);
   2810 	} else {
   2811 		mutex_enter(&esp->e_startQ_mutex);
   2812 		if (esp->e_startf || (mutex_tryenter(ESP_MUTEX) == 0)) {
   2813 			if (esp->e_startf == NULL) {
   2814 				esp->e_startb = esp->e_startf = sp;
   2815 				sp->cmd_forw = NULL;
   2816 			} else {
   2817 				struct esp_cmd *dp = esp->e_startb;
   2818 				dp->cmd_forw = esp->e_startb = sp;
   2819 				sp->cmd_forw = NULL;
   2820 			}
   2821 			/*
   2822 			 * check again the ESP_MUTEX
   2823 			 */
   2824 			if (mutex_tryenter(ESP_MUTEX)) {
   2825 				esp_empty_startQ(esp);
   2826 				mutex_exit(ESP_MUTEX);
   2827 			}
   2828 			mutex_exit(&esp->e_startQ_mutex);
   2829 			goto done;
   2830 		}
   2831 		mutex_exit(&esp->e_startQ_mutex);
   2832 	}
   2833 
   2834 	rval = _esp_start(esp, sp, TRAN_BUSY_OK);
   2835 	ESP_CHECK_STARTQ_AND_ESP_MUTEX_EXIT(esp);
   2836 	ESP_WAKEUP_CALLBACK_THREAD(esp);
   2837 done:
   2838 	TRACE_1(TR_FAC_SCSI, TR_ESP_START_END, "esp_start_end: esp 0x%p",
   2839 	    (void *)esp);
   2840 	return (rval);
   2841 }
   2842 
   2843 /*
   2844  * _esp_start()
   2845  * the flag argument is to force _esp_start to accept the pkt; pkts that were
   2846  * on startQ cannot be bounced back with TRAN_BUSY
   2847  */
   2848 static int
   2849 _esp_start(struct esp *esp, struct esp_cmd *sp, int flag)
   2850 {
   2851 	short slot;
   2852 	int target = Tgt(sp);
   2853 	int lun = Lun(sp);
   2854 	int rval = TRAN_ACCEPT;
   2855 
   2856 	TRACE_0(TR_FAC_SCSI, TR__ESP_START_START, "_esp_start_start");
   2857 	slot =	(target * NLUNS_PER_TARGET) | lun;
   2858 	ASSERT(mutex_owned(ESP_MUTEX));
   2859 	ASSERT(esp->e_ncmds >= esp->e_ndisc);
   2860 	ASSERT(esp->e_ncmds >= 0 && esp->e_ndisc >= 0);
   2861 
   2862 	if (lun) {
   2863 		EPRINTF("_esp_start: switching target and lun slot scan\n");
   2864 		esp->e_dslot = 1;
   2865 	}
   2866 
   2867 	esp_check_in_transport(esp, sp);
   2868 
   2869 	/*
   2870 	 * prepare (init) packet if this hasn't been done yet and do some checks
   2871 	 */
   2872 	if ((sp->cmd_flags & CFLAG_PREPARED) == 0) {
   2873 		rval = esp_prepare_pkt(esp, sp);
   2874 		if (rval != TRAN_ACCEPT) {
   2875 			IPRINTF1("prepare pkt failed, slot=%x\n", slot);
   2876 #ifdef ESPDEBUG
   2877 			sp->cmd_flags &= ~CFLAG_IN_TRANSPORT;
   2878 #endif
   2879 			goto done;
   2880 		}
   2881 	}
   2882 
   2883 	/*
   2884 	 * At this point we are not going to reject the packet.
   2885 	 * we let proxy packets go thru because these packets don't call a
   2886 	 * target driver completion routine
   2887 	 */
   2888 
   2889 #ifdef ESP_KSTATS
   2890 	/*
   2891 	 * create kstats if not done already
   2892 	 */
   2893 	if (esp_do_kstats) {
   2894 		int slot = (Tgt(sp) * NLUNS_PER_TARGET) | Lun(sp);
   2895 
   2896 		/*
   2897 		 * don't create e_slot_stats if this is an NOINTR cmd; this
   2898 		 * may be just a probing
   2899 		 */
   2900 		if ((esp->e_slot_stats[slot] == NULL) &&
   2901 		    ((sp->cmd_pkt.pkt_flags & FLAG_NOINTR) == 0)) {
   2902 			char buf[32];
   2903 
   2904 			(void) sprintf(buf, "esp%dt%dd", CNUM, target);
   2905 			if ((esp->e_slot_stats[slot] = kstat_create(
   2906 			    buf, lun, NULL, "disk",
   2907 			    KSTAT_TYPE_IO, 1,
   2908 			    KSTAT_FLAG_PERSISTENT)) != NULL) {
   2909 				esp->e_slot_stats[slot]->ks_lock = ESP_MUTEX;
   2910 				kstat_install(esp->e_slot_stats[slot]);
   2911 			}
   2912 		}
   2913 		if (esp->e_slot_stats[slot]) {
   2914 			kstat_waitq_enter(IOSP(slot));
   2915 		}
   2916 	}
   2917 #endif /* ESP_KSTATS */
   2918 
   2919 #ifdef ESP_PERF
   2920 	esp_request_count++;
   2921 #endif
   2922 
   2923 	/*
   2924 	 * we accepted the command; increment the count
   2925 	 * (we may reject later if TRAN_BUSY!; we test this later because
   2926 	 * we don't want to incur the extra overhead here)
   2927 	 */
   2928 	esp->e_ncmds++;
   2929 
   2930 	/*
   2931 	 * if it is a nointr packet, start it now
   2932 	 * (NO_INTR pkts are not queued in the startQ)
   2933 	 */
   2934 	if (sp->cmd_pkt.pkt_flags & FLAG_NOINTR) {
   2935 		EPRINTF("starting a nointr cmd\n");
   2936 		esp_runpoll(esp, slot, sp);
   2937 #ifdef ESPDEBUG
   2938 		sp->cmd_flags &= ~CFLAG_IN_TRANSPORT;
   2939 #endif
   2940 		goto done;
   2941 	}
   2942 
   2943 	/*
   2944 	 * accept the command:
   2945 	 * If no ready que and free slot, run cmd immediately.
   2946 	 * If FLAG_HEAD mode set, run cmd as soon as free slot
   2947 	 * available. if first cmd in ready Q is request sense then insert
   2948 	 * after this cmd (there shouldn't be more than one request sense).
   2949 	 * Queue up the command in the ready queue if this queue is non-empty
   2950 	 * or if we had a queue full condition
   2951 	 */
   2952 	if (esp->e_readyf[slot]) {
   2953 		if (sp->cmd_pkt.pkt_flags & FLAG_HEAD) {
   2954 			struct esp_cmd *ssp = esp->e_readyf[slot];
   2955 			EPRINTF("que head\n");
   2956 			if (*(ssp->cmd_pkt.pkt_cdbp) != SCMD_REQUEST_SENSE) {
   2957 				sp->cmd_forw = ssp;
   2958 				esp->e_readyf[slot] = sp;
   2959 			} else {
   2960 				struct esp_cmd *dp = ssp->cmd_forw;
   2961 				ssp->cmd_forw = sp;
   2962 				sp->cmd_forw = dp;
   2963 				if (esp->e_readyb[slot] == ssp) {
   2964 					esp->e_readyb[slot] = sp;
   2965 				}
   2966 			}
   2967 		} else if ((esp->e_tcmds[slot] >= esp->e_throttle[slot]) &&
   2968 		    (esp->e_throttle[slot] > HOLD_THROTTLE) &&
   2969 		    (flag == TRAN_BUSY_OK)) {
   2970 			IPRINTF2(
   2971 			    "transport busy, slot=%x, ncmds=%x\n",
   2972 			    slot, esp->e_ncmds);
   2973 			rval = TRAN_BUSY;
   2974 			esp->e_ncmds--;
   2975 			sp->cmd_flags &= ~CFLAG_PREPARED;
   2976 #ifdef ESPDEBUG
   2977 			sp->cmd_flags &= ~CFLAG_IN_TRANSPORT;
   2978 #endif
   2979 #ifdef ESP_PERF
   2980 			esp_request_count--;
   2981 #endif
   2982 			goto done;
   2983 		} else {
   2984 			struct esp_cmd *dp = esp->e_readyb[slot];
   2985 
   2986 			EPRINTF("que tail\n");
   2987 			ASSERT(dp != 0);
   2988 			esp->e_readyb[slot] = sp;
   2989 			sp->cmd_forw = NULL;
   2990 			dp->cmd_forw = sp;
   2991 		}
   2992 
   2993 		if ((esp->e_throttle[slot] == DRAIN_THROTTLE) &&
   2994 		    (esp->e_tcmds[slot] == 0)) {
   2995 			esp->e_throttle[slot] = CLEAR_THROTTLE;
   2996 		}
   2997 
   2998 		/*
   2999 		 * just in case that the bus is free and we haven't
   3000 		 * been able to restart for some reason
   3001 		 * XXX this shouldn't really be necessary
   3002 		 */
   3003 		if (esp->e_state == STATE_FREE) {
   3004 			(void) esp_ustart(esp, slot, NEW_CMD);
   3005 		}
   3006 	} else {
   3007 		/*
   3008 		 * for tagged targets with no cmds outstanding and currently
   3009 		 * draining, reset throttle now
   3010 		 * for non-tagged targets and currently draining, always reset
   3011 		 * throttle now (t_cmds is always zero for non-tagged)
   3012 		 */
   3013 		if ((esp->e_tcmds[slot] == 0) && (esp->e_throttle[slot] ==
   3014 		    DRAIN_THROTTLE)) {
   3015 			IPRINTF("reset throttle\n");
   3016 			esp->e_throttle[slot] = CLEAR_THROTTLE;
   3017 		}
   3018 		if ((esp->e_state == STATE_FREE) &&
   3019 		    (esp->e_slots[slot] == NULL) &&
   3020 		    (esp->e_tcmds[slot] < esp->e_throttle[slot])) {
   3021 			EPRINTF("start cmd (maybe)\n");
   3022 			esp->e_cur_slot = slot;
   3023 			esp->e_slots[slot] = sp;
   3024 			(void) esp_startcmd(esp, sp);
   3025 		} else {
   3026 			EPRINTF2(
   3027 			    "cmd not started: e_slot=0x%p, throttle=%x\n",
   3028 			    (void *)esp->e_slots[slot], esp->e_throttle[slot]);
   3029 			esp->e_readyf[slot] = esp->e_readyb[slot] = sp;
   3030 			sp->cmd_forw = NULL;
   3031 		}
   3032 	}
   3033 done:
   3034 	ASSERT(mutex_owned(ESP_MUTEX));
   3035 	TRACE_0(TR_FAC_SCSI, TR__ESP_START_END, "_esp_start_end");
   3036 	return (rval);
   3037 }
   3038 
   3039 static char esp_tag_lookup[] =
   3040 	{0, MSG_HEAD_QTAG, MSG_ORDERED_QTAG, 0, MSG_SIMPLE_QTAG};
   3041 
   3042 static int
   3043 esp_alloc_tag(struct esp *esp, struct esp_cmd *sp)
   3044 {
   3045 	struct t_slots *tag_slots;
   3046 	uchar_t tag;
   3047 	int rval = 0;
   3048 	int target = Tgt(sp);
   3049 	int lun = Lun(sp);
   3050 	short slot = (target * NLUNS_PER_TARGET) | lun;
   3051 
   3052 	TRACE_0(TR_FAC_SCSI, TR_ESP_ALLOC_TAG_START,
   3053 	    "esp_alloc_tag_start");
   3054 	ASSERT(mutex_owned(ESP_MUTEX));
   3055 
   3056 alloc:
   3057 	/*
   3058 	 * allocate tag
   3059 	 * Optimize for the common case, ie. success
   3060 	 */
   3061 	tag_slots = esp->e_tagQ[slot];
   3062 	if (tag_slots != NULL) {
   3063 		tag = (esp->e_tagQ[slot]->e_tags)++;
   3064 		EPRINTF1("tagged cmd, tag = %d\n", tag);
   3065 
   3066 		/* Validate tag, should never fail. */
   3067 		if (tag_slots->t_slot[tag] == 0) {
   3068 			/*
   3069 			 * Store assigned tag and tag queue type.
   3070 			 * Note, in case
   3071 			 * of multiple choice, default to simple queue.
   3072 			 */
   3073 			sp->cmd_tag[1] = tag;
   3074 			sp->cmd_tag[0] = esp_tag_lookup[
   3075 			    ((sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) >> 12)];
   3076 			EPRINTF1("tag= %d\n", tag);
   3077 			tag_slots->t_slot[tag] =  sp;
   3078 			(esp->e_tcmds[slot])++;
   3079 done:
   3080 			ASSERT(mutex_owned(ESP_MUTEX));
   3081 			TRACE_0(TR_FAC_SCSI, TR_ESP_ALLOC_TAG_END,
   3082 			    "esp_alloc_tag_end");
   3083 			return (rval);
   3084 		} else {
   3085 			int age, i;
   3086 
   3087 			/*
   3088 			 * Check tag age.  If timeouts enabled and
   3089 			 * tag age greater than 1, print warning msg.
   3090 			 * If timeouts enabled and tag age greater than
   3091 			 * age limit, begin draining tag que to check for
   3092 			 * lost tag cmd.
   3093 			 */
   3094 			age = tag_slots->t_slot[tag]->cmd_age++;
   3095 			if (age >= esp->e_scsi_tag_age_limit &&
   3096 			    tag_slots->t_slot[tag]->cmd_pkt.pkt_time) {
   3097 				IPRINTF2("tag %d in use, age= %d\n", tag, age);
   3098 				IPRINTF("draining tag queue\n");
   3099 				if (esp->e_reset_delay[Tgt(sp)] == 0) {
   3100 					esp->e_throttle[slot] = DRAIN_THROTTLE;
   3101 				}
   3102 			}
   3103 
   3104 			/* If tag in use, scan until a free one is found. */
   3105 			for (i = 0; i < NTAGS; i++) {
   3106 				tag = esp->e_tagQ[slot]->e_tags;
   3107 				if (!tag_slots->t_slot[tag]) {
   3108 					EPRINTF1("found free tag %d\n", tag);
   3109 					break;
   3110 				}
   3111 				++(esp->e_tagQ[slot]->e_tags);
   3112 				EPRINTF1("found in use tag %d\n", tag);
   3113 			}
   3114 
   3115 			/* If no free tags, we're in serious trouble. */
   3116 			if (tag_slots->t_slot[tag]) {
   3117 				esplog(esp, CE_WARN,
   3118 				    "slot %x: All tags in use!!!\n", slot);
   3119 				rval = -1;
   3120 				goto done;
   3121 			}
   3122 			goto alloc;
   3123 		}
   3124 	} else {
   3125 		EPRINTF2("Target %d.%d allocating tag que\n",
   3126 		    target, Lun(sp));
   3127 		tag_slots = kmem_zalloc(sizeof (struct t_slots), KM_NOSLEEP);
   3128 		if (tag_slots == NULL) {
   3129 			/*
   3130 			 * Couldn't get space for tagged que.  Complain
   3131 			 * and disable tagged queuing.	 It beats
   3132 			 * dying...Seriously, this should not
   3133 			 * happen.
   3134 			 */
   3135 			esplog(esp, CE_WARN,
   3136 			    "Target %d.%d cannot alloc tag queue\n",
   3137 			    target, Lun(sp));
   3138 			esp->e_notag |= 1<<target;
   3139 			sp->cmd_pkt.pkt_flags &=  ~FLAG_TAGMASK;
   3140 			goto done;
   3141 		}
   3142 		esp->e_tagQ[slot] = tag_slots;
   3143 		goto alloc;
   3144 	}
   3145 	_NOTE(NOT_REACHED)
   3146 	/* NOTREACHED */
   3147 }
   3148 
   3149 /*
   3150  * Internal Search Routine.
   3151  *
   3152  * Search for a command to start.
   3153  */
   3154 static int
   3155 esp_istart(struct esp *esp)
   3156 {
   3157 	TRACE_0(TR_FAC_SCSI, TR_ESP_ISTART_START,
   3158 	    "esp_istart_start");
   3159 	EPRINTF("esp_istart:\n");
   3160 
   3161 	if (esp->e_state == STATE_FREE && esp->e_ncmds > esp->e_ndisc) {
   3162 		(void) esp_ustart(esp, esp->e_last_slot, NEW_CMD);
   3163 	}
   3164 	TRACE_0(TR_FAC_SCSI, TR_ESP_ISTART_END,
   3165 	    "esp_istart_end");
   3166 	return (ACTION_RETURN);
   3167 }
   3168 
   3169 static int
   3170 esp_ustart(struct esp *esp, short start_slot, short flag)
   3171 {
   3172 	struct esp_cmd *sp;
   3173 	short slot;
   3174 
   3175 	TRACE_0(TR_FAC_SCSI, TR_ESP_USTART_START, "esp_ustart_start");
   3176 	EPRINTF2("esp_ustart: start_slot=%x, flag=%x\n", start_slot, flag);
   3177 
   3178 
   3179 	switch (flag) {
   3180 	case NEW_CMD:
   3181 	{
   3182 		int found = 0;
   3183 		short dslot = esp->e_dslot;
   3184 
   3185 		slot = start_slot = esp->e_next_slot;
   3186 
   3187 #ifdef ESPDEBUG
   3188 		ASSERT(dslot != 0);
   3189 		if (dslot == NLUNS_PER_TARGET) {
   3190 			ASSERT((slot % NLUNS_PER_TARGET) == 0);
   3191 		}
   3192 #endif /* ESPDEBUG */
   3193 
   3194 		/*
   3195 		 * check each std slot; if it is empty (ie. target not currently
   3196 		 * connected), then check the ready queue for packets
   3197 		 */
   3198 		do {
   3199 			sp = esp->e_slots[slot];
   3200 			if ((sp == 0) && esp->e_readyf[slot] &&
   3201 			    (esp->e_throttle[slot] >  esp->e_tcmds[slot])) {
   3202 				sp = esp->e_readyf[slot];
   3203 				esp->e_readyf[slot] = sp->cmd_forw;
   3204 				if (sp->cmd_forw == NULL) {
   3205 					esp->e_readyb[slot] = NULL;
   3206 				}
   3207 				found++;
   3208 			} else {
   3209 				slot = NEXTSLOT(slot, dslot);
   3210 			}
   3211 		} while (found == 0 && slot != start_slot);
   3212 
   3213 		if (!found) {
   3214 			EPRINTF("esp_ustart: no cmds to start\n");
   3215 			TRACE_0(TR_FAC_SCSI, TR_ESP_USTART_NOT_FOUND_END,
   3216 			    "esp_ustart_end (not_found)");
   3217 			return (FALSE);
   3218 		}
   3219 		esp->e_cur_slot = slot;
   3220 		esp->e_slots[slot] = sp;
   3221 		ASSERT((sp->cmd_pkt.pkt_flags & FLAG_NOINTR) == 0);
   3222 		break;
   3223 	}
   3224 	case SAME_CMD:
   3225 		ASSERT(start_slot != UNDEFINED);
   3226 		slot = esp->e_cur_slot = start_slot;
   3227 		sp = esp->e_slots[start_slot];
   3228 		break;
   3229 	default:
   3230 		TRACE_0(TR_FAC_SCSI, TR_ESP_USTART_DEFAULT_END,
   3231 		    "esp_ustart_end (default)");
   3232 		return (FALSE);
   3233 	}
   3234 
   3235 	esp->e_next_slot = NEXTSLOT(slot, esp->e_dslot);
   3236 
   3237 	TRACE_0(TR_FAC_SCSI, TR_ESP_USTART_END, "esp_ustart_end");
   3238 	return (esp_startcmd(esp, sp));
   3239 }
   3240 
   3241 
   3242 /*
   3243  * Start a command off
   3244  */
   3245 #ifdef ESPDEBUG
   3246 static int esp_cmd_len;
   3247 #endif
   3248 
   3249 static int
   3250 esp_startcmd(struct esp *esp, struct esp_cmd *sp)
   3251 {
   3252 	volatile struct espreg *ep = esp->e_reg;
   3253 	int cmd_len, i, nstate;
   3254 	uchar_t cmd, tshift, target, lun;
   3255 	volatile caddr_t tp = (caddr_t)esp->e_cmdarea;
   3256 	uchar_t offset, period, conf3;
   3257 
   3258 	ASSERT(esp->e_slots[esp->e_cur_slot]->cmd_flags & CFLAG_IN_TRANSPORT);
   3259 	ASSERT(sp == esp->e_slots[esp->e_cur_slot]);
   3260 
   3261 #define	LOAD_CMDP	*(tp++)
   3262 
   3263 	target = Tgt(sp);
   3264 	lun    = Lun(sp);
   3265 	TRACE_0(TR_FAC_SCSI, TR_ESP_STARTCMD_START, "esp_startcmd_start");
   3266 
   3267 	EPRINTF2("esp_startcmd: sp=0x%p flags=%x\n",
   3268 	    (void *)sp, sp->cmd_pkt.pkt_flags);
   3269 
   3270 #ifdef ESPDEBUG
   3271 	if (esp->e_cur_slot != ((target * NLUNS_PER_TARGET) | lun)) {
   3272 		eprintf(esp, "cur_slot=%x, target=%x, lun=%x, sp=0x%p\n",
   3273 		    esp->e_cur_slot, target, lun, (void *)sp);
   3274 		debug_enter("esp_startcmd");
   3275 	}
   3276 	ASSERT((sp->cmd_flags & CFLAG_FREE) == 0);
   3277 	ASSERT(esp->e_reset_delay[Tgt(sp)] == 0);
   3278 #endif
   3279 
   3280 	/*
   3281 	 * if a non-tagged cmd is submitted to an active tagged target
   3282 	 * then drain before submitting this cmd; SCSI-2 allows RQSENSE
   3283 	 * to be untagged
   3284 	 */
   3285 	if (((sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) == 0) &&
   3286 	    TAGGED(Tgt(sp)) && esp->e_tcmds[esp->e_cur_slot] &&
   3287 	    ((sp->cmd_flags & CFLAG_CMDPROXY) == 0) &&
   3288 	    (*(sp->cmd_pkt.pkt_cdbp) != SCMD_REQUEST_SENSE)) {
   3289 		esp->e_slots[esp->e_cur_slot] = NULL;
   3290 		if ((sp->cmd_pkt.pkt_flags & FLAG_NOINTR) == 0) {
   3291 			struct esp_cmd *dp;
   3292 			int slot = esp->e_cur_slot;
   3293 
   3294 			IPRINTF("untagged cmd, start draining\n");
   3295 
   3296 			if (esp->e_reset_delay[Tgt(sp)] == 0) {
   3297 				esp->e_throttle[slot] = DRAIN_THROTTLE;
   3298 			}
   3299 			dp = esp->e_readyf[slot];
   3300 			esp->e_readyf[slot] = sp;
   3301 			sp->cmd_forw = dp;
   3302 			if (esp->e_readyb[slot] == NULL) {
   3303 				esp->e_readyb[slot] = sp;
   3304 			}
   3305 		}
   3306 		return (FALSE);
   3307 	}
   3308 
   3309 	/*
   3310 	 * The only reason that this should happen
   3311 	 * is if we have a re-selection attempt starting.
   3312 	 */
   3313 	if (INTPENDING(esp)) {
   3314 		int slot;
   3315 		struct esp_cmd *dp;
   3316 
   3317 		slot = esp->e_cur_slot;
   3318 		ESP_PREEMPT(esp);
   3319 		LOG_STATE(esp, ACTS_PREEMPTED, esp->e_stat, Tgt(sp), lun);
   3320 		TRACE_0(TR_FAC_SCSI, TR_ESP_STARTCMD_PREEMPT_CALL,
   3321 		    "esp_startcmd_preempt_call");
   3322 		/*
   3323 		 * put request back in the ready queue
   3324 		 * runpoll will retry NOINTR cmds so no need to put
   3325 		 * those on ready Q
   3326 		 */
   3327 		if ((sp->cmd_pkt.pkt_flags & FLAG_NOINTR) == 0) {
   3328 			esp->e_slots[slot] = (struct esp_cmd *)NULL;
   3329 			dp = esp->e_readyf[slot];
   3330 			esp->e_readyf[slot] = sp;
   3331 			sp->cmd_forw = dp;
   3332 			if (esp->e_readyb[slot] == NULL) {
   3333 				esp->e_readyb[slot] = sp;
   3334 			}
   3335 			esp->e_polled_intr = 1;
   3336 			espsvc(esp);
   3337 		}
   3338 		TRACE_0(TR_FAC_SCSI, TR_ESP_STARTCMD_RE_SELECTION_END,
   3339 		    "esp_startcmd_end (re_selection)");
   3340 		return (FALSE);
   3341 	}
   3342 
   3343 	/*
   3344 	 * allocate a tag; if no tag available then put request back
   3345 	 * on the ready queue and return; eventually a cmd completes and we
   3346 	 * get going again
   3347 	 */
   3348 	if (sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) {
   3349 		if ((esp_alloc_tag(esp, sp))) {
   3350 			int slot;
   3351 			struct esp_cmd *dp;
   3352 
   3353 			slot = esp->e_cur_slot;
   3354 			dp = esp->e_readyf[slot];
   3355 			esp->e_readyf[slot] = sp;
   3356 			sp->cmd_forw = dp;
   3357 			if (esp->e_readyb[slot] == NULL) {
   3358 				esp->e_readyb[slot] = sp;
   3359 			}
   3360 			esp->e_slots[slot] = NULL;
   3361 			esp->e_last_slot = esp->e_cur_slot;
   3362 			esp->e_cur_slot = UNDEFINED;
   3363 			TRACE_0(TR_FAC_SCSI, TR_ESP_STARTCMD_ALLOC_TAG2_END,
   3364 			    "esp_startcmd_end (alloc_tag2)");
   3365 			return (FALSE);
   3366 		}
   3367 	} else {
   3368 		if (TAGGED(target)) {
   3369 			if (*(sp->cmd_pkt.pkt_cdbp) != SCMD_REQUEST_SENSE) {
   3370 				esp->e_throttle[esp->e_cur_slot] = 1;
   3371 			}
   3372 		}
   3373 	}
   3374 	esp->e_sdtr = esp->e_omsglen = 0;
   3375 	tshift = 1<<target;
   3376 
   3377 #ifdef	ESPDEBUG
   3378 	esp->e_xfer = sp->cmd_dmacount;
   3379 #endif	/* ESPDEBUG */
   3380 
   3381 	/*
   3382 	 * The ESP chip will only automatically
   3383 	 * send 6, 10 or 12 byte SCSI cmds.
   3384 	 * NOTE: if cmd_len is 0, we xfer cmd bytes one at the time
   3385 	 * Also note that the "SELECT with ATN and STOP" stops with ATN
   3386 	 * asserted; if no msg is available, we send a NOP. Some targets
   3387 	 * may not like this.
   3388 	 */
   3389 	cmd_len = sp->cmd_cdblen;
   3390 
   3391 #ifdef ESPDEBUG
   3392 	if (esp_cmd_len)
   3393 		cmd_len = 0;
   3394 #endif
   3395 
   3396 	if ((sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) &&
   3397 	    (esp->e_sync_known & tshift ||
   3398 	    (esp->e_target_scsi_options[target] & SCSI_OPTIONS_SYNC) == 0)) {
   3399 		EPRINTF("tag cmd\n");
   3400 
   3401 		ASSERT((sp->cmd_pkt.pkt_flags & FLAG_NODISCON) == 0);
   3402 		LOAD_CMDP = esp->e_last_msgout = MSG_DR_IDENTIFY | lun;
   3403 
   3404 		if (cmd_len) {
   3405 			LOAD_CMDP = sp->cmd_tag[0];
   3406 			LOAD_CMDP = sp->cmd_tag[1];
   3407 
   3408 			nstate = STATE_SELECT_NORMAL;
   3409 			cmd = CMD_SEL_ATN3 | CMD_DMA;
   3410 
   3411 		} else {
   3412 			esp->e_cur_msgout[0] = sp->cmd_tag[0];
   3413 			esp->e_cur_msgout[1] = sp->cmd_tag[1];
   3414 			esp->e_omsglen = 2;
   3415 			EPRINTF2("tag %d, omsglen=%x\n",
   3416 			    sp->cmd_tag[1], esp->e_omsglen);
   3417 
   3418 			cmd_len = 0;
   3419 			nstate = STATE_SELECT_N_TAG;
   3420 			cmd = CMD_SEL_STOP | CMD_DMA;
   3421 		}
   3422 		LOG_STATE(esp, ACTS_SELECT, target, lun, -1);
   3423 		LOG_STATE(esp, ACTS_TAG, sp->cmd_tag[0], sp->cmd_tag[1], -1);
   3424 
   3425 	} else if (sp->cmd_flags & CFLAG_CMDPROXY) {
   3426 
   3427 		IPRINTF2("proxy cmd, len=%x, msg=%x\n",
   3428 		    sp->cmd_cdb[ESP_PROXY_DATA],
   3429 		    sp->cmd_cdb[ESP_PROXY_DATA+1]);
   3430 
   3431 		/*
   3432 		 * This is a proxy command. It will have
   3433 		 * a message to send as part of post-selection
   3434 		 * (e.g, MSG_ABORT or MSG_DEVICE_RESET)
   3435 		 * XXX: We should check to make sure that
   3436 		 * this is a valid PROXY command, i.e,
   3437 		 * a  valid message length.
   3438 		 */
   3439 		LOAD_CMDP = esp->e_last_msgout = MSG_IDENTIFY | lun;
   3440 		esp->e_omsglen = sp->cmd_cdb[ESP_PROXY_DATA];
   3441 		for (i = 0; i < (uint_t)esp->e_omsglen; i++) {
   3442 			esp->e_cur_msgout[i] =
   3443 			    sp->cmd_cdb[ESP_PROXY_DATA+1+i];
   3444 		}
   3445 		sp->cmd_cdb[ESP_PROXY_RESULT] = FALSE;
   3446 		nstate = STATE_SELECT_N_SENDMSG;
   3447 		cmd = CMD_SEL_STOP | CMD_DMA;
   3448 		cmd_len = 0;
   3449 		LOG_STATE(esp, ACTS_PROXY, esp->e_stat,
   3450 		    esp->e_cur_msgout[0], nstate);
   3451 
   3452 	} else if (((esp->e_sync_known & tshift) == 0) &&
   3453 	    (esp->e_target_scsi_options[target] & SCSI_OPTIONS_SYNC)) {
   3454 
   3455 		if (sp->cmd_pkt.pkt_flags & FLAG_NODISCON) {
   3456 			LOAD_CMDP = esp->e_last_msgout = MSG_IDENTIFY | lun;
   3457 			ASSERT((sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) == 0);
   3458 		} else {
   3459 			LOAD_CMDP = esp->e_last_msgout =
   3460 			    MSG_DR_IDENTIFY | lun;
   3461 		}
   3462 
   3463 		/*
   3464 		 * Set up to send synch. negotiating message.  This is getting
   3465 		 * a bit tricky as we dma out the identify message and
   3466 		 * send the other messages via the fifo buffer.
   3467 		 */
   3468 		EPRINTF1("cmd with sdtr msg, tag=%x\n", sp->cmd_tag[1]);
   3469 		LOG_STATE(esp, ACTS_SELECT, target, lun, -1);
   3470 
   3471 		/* First the tag message bytes */
   3472 		i = 0;
   3473 		if (sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) {
   3474 			esp->e_cur_msgout[i++] = sp->cmd_tag[0];
   3475 			esp->e_cur_msgout[i++] = sp->cmd_tag[1];
   3476 			LOG_STATE(esp, ACTS_TAG,
   3477 			    sp->cmd_tag[0], sp->cmd_tag[1], -1);
   3478 		}
   3479 
   3480 		if (esp->e_weak & tshift) {
   3481 			nstate = STATE_SELECT_NORMAL;
   3482 			cmd = CMD_SEL_ATN | CMD_DMA;
   3483 		} else {
   3484 			int period = esp->e_default_period[target];
   3485 			int offset = DEFAULT_OFFSET;
   3486 
   3487 			if (esp->e_force_async & (1<<target)) {
   3488 				offset = 0;
   3489 			}
   3490 			if (esp->e_backoff[target] == 1) {
   3491 				period = esp->e_neg_period[target];
   3492 			} else if (esp->e_backoff[target] > 1) {
   3493 				period = esp->e_neg_period[target];
   3494 				offset = 0;
   3495 			}
   3496 			esp_make_sdtr(esp, i, (int)period,
   3497 			    (int)offset);
   3498 			LOG_STATE(esp, ACTS_SYNCHOUT, esp->e_stat,
   3499 			    period, offset);
   3500 			cmd_len = 0;
   3501 			cmd = CMD_SEL_STOP | CMD_DMA;
   3502 			nstate = STATE_SELECT_N_SENDMSG;
   3503 		}
   3504 		/*
   3505 		 * XXX: Set sync known here because the Sony CDrom
   3506 		 * ignores the synch negotiation msg. Net effect
   3507 		 * is we negotiate on every I/O request forever.
   3508 		 */
   3509 		esp->e_sync_known |= (1<<target);
   3510 
   3511 	} else	{
   3512 
   3513 		ASSERT((sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) == 0);
   3514 
   3515 		if (sp->cmd_pkt.pkt_flags & FLAG_NODISCON) {
   3516 			LOAD_CMDP = esp->e_last_msgout = MSG_IDENTIFY | lun;
   3517 		} else {
   3518 			LOAD_CMDP = esp->e_last_msgout =
   3519 			    MSG_DR_IDENTIFY | lun;
   3520 		}
   3521 
   3522 		/* Send cmd. */
   3523 		if (cmd_len) {
   3524 			EPRINTF("std. cmd\n");
   3525 			nstate = STATE_SELECT_NORMAL;
   3526 			cmd = CMD_SEL_ATN | CMD_DMA;
   3527 		/*
   3528 		 * XXX: Things get a bit complicated for cdb's the esp
   3529 		 *	chip doesn't understand.  We have to send them out
   3530 		 *	one byte at a time.  This is not a fast process!
   3531 		 */
   3532 		} else {
   3533 			IPRINTF("sending special cmd\n");
   3534 			cmd = CMD_SEL_STOP | CMD_DMA;
   3535 			nstate = STATE_SELECT_N_STOP;
   3536 		}
   3537 		LOG_STATE(esp, ACTS_SELECT, target, lun, -1);
   3538 	}
   3539 
   3540 	/*
   3541 	 * Now load cdb (if any)
   3542 	 */
   3543 	for (i = 0; i < cmd_len; i++) {
   3544 		LOAD_CMDP = sp->cmd_cdbp[i];
   3545 	}
   3546 	if (cmd_len) {
   3547 		LOG_STATE(esp, ACTS_CMD_START, esp->e_stat, sp->cmd_cdbp[0],
   3548 		    nstate);
   3549 	}
   3550 
   3551 	/*
   3552 	 * calculate total dma amount:
   3553 	 */
   3554 	esp->e_lastcount = (uintptr_t)tp - (uintptr_t)esp->e_cmdarea;
   3555 
   3556 	/*
   3557 	 * load rest of chip registers, if needed
   3558 	 */
   3559 	ep->esp_busid = target;
   3560 
   3561 	period =  esp->e_period[target] & SYNC_PERIOD_MASK;
   3562 	offset = esp->e_offset[target];
   3563 	conf3 = esp->e_espconf3[target];
   3564 	if ((esp->e_period_last != period) ||
   3565 	    (esp->e_offset_last != offset) ||
   3566 	    (esp->e_espconf3_last != conf3)) {
   3567 		esp->e_period_last = ep->esp_sync_period = period;
   3568 		esp->e_offset_last = ep->esp_sync_offset = offset;
   3569 		esp->e_espconf3_last = ep->esp_conf3 = conf3;
   3570 	}
   3571 
   3572 	if ((esp->e_target_scsi_options[target] & SCSI_OPTIONS_PARITY) &&
   3573 	    (sp->cmd_pkt.pkt_flags & FLAG_NOPARITY)) {
   3574 		ep->esp_conf = esp->e_espconf & ~ESP_CONF_PAREN;
   3575 	}
   3576 	SET_DMAESC_COUNT(esp->e_dma, esp->e_esc_read_count);
   3577 	ESP_DMA_READ(esp, esp->e_lastcount, esp->e_dmacookie.dmac_address);
   3578 
   3579 	Esp_cmd(esp, (int)cmd);
   3580 	New_state(esp, (int)nstate);
   3581 	LOG_STATE(esp, nstate, esp->e_stat, target, lun);
   3582 
   3583 #ifdef ESPDEBUG
   3584 	if (DEBUGGING) {
   3585 		auto char buf[256];
   3586 		buf[0] = '\0';
   3587 		(void) sprintf(&buf[strlen(buf)], "sel %d.%d cmd[ ",
   3588 		    target, lun);
   3589 		for (i = 0; i < (int)sp->cmd_cdblen; i++) {
   3590 			(void) sprintf(&buf[strlen(buf)],
   3591 			    "0x%x ", sp->cmd_cdbp[i] & 0xff);
   3592 		}
   3593 		(void) sprintf(&buf[strlen(buf)], "]\n\tstate=%s\n",
   3594 		    esp_state_name(esp->e_state));
   3595 		eprintf(esp, "%s", buf);
   3596 	}
   3597 #endif /* ESPDEBUG */
   3598 
   3599 	/*
   3600 	 * set up timeout here; there is a risk of preemption in which
   3601 	 * case we don't adjust the timeout. So, we hope that this
   3602 	 * cmd gets started fairly quickly after a preemption.
   3603 	 */
   3604 	if (sp->cmd_pkt.pkt_flags & FLAG_TAGMASK) {
   3605 		short slot = esp->e_cur_slot;
   3606 		struct t_slots *tag_slots = esp->e_tagQ[slot];
   3607 
   3608 		i = sp->cmd_pkt.pkt_time - tag_slots->e_timebase;
   3609 
   3610 		if (i == 0) {
   3611 			EPRINTF("dup timeout\n");
   3612 			(tag_slots->e_dups)++;
   3613 			tag_slots->e_timeout = tag_slots->e_timebase;
   3614 		} else if (i > 0) {
   3615 			EPRINTF("new timeout\n");
   3616 			tag_slots->e_timeout = tag_slots->e_timebase =
   3617 			    sp->cmd_pkt.pkt_time;
   3618 			tag_slots->e_dups = 1;
   3619 		}
   3620 	}
   3621 
   3622 #ifdef ESP_KSTATS
   3623 	if (esp_do_kstats && esp->e_slot_stats[esp->e_cur_slot]) {
   3624 		kstat_waitq_to_runq(IOSP(esp->e_cur_slot));
   3625 	}
   3626 #endif /* ESP_KSTATS */
   3627 	TRACE_0(TR_FAC_SCSI, TR_ESP_STARTCMD_END, "esp_startcmd_end");
   3628 
   3629 	return (TRUE);
   3630 }
   3631 
   3632 /*
   3633  * Autovector Interrupt Entry Point.
   3634  *
   3635  */
   3636 
   3637 static uint_t
   3638 esp_intr(caddr_t arg)
   3639 {
   3640 	struct esp *esp = (struct esp *)arg;
   3641 	int kstat_updated = 0;
   3642 	int rval = DDI_INTR_UNCLAIMED;
   3643 
   3644 #ifdef ESP_PERF
   3645 	esp_intr_count++;
   3646 #endif
   3647 	do {
   3648 		mutex_enter(ESP_MUTEX);
   3649 		while (INTPENDING(esp)) {
   3650 			espsvc(esp);
   3651 			rval = DDI_INTR_CLAIMED;
   3652 		}
   3653 
   3654 		if (esp->e_polled_intr) {
   3655 			rval = DDI_INTR_CLAIMED;
   3656 			esp->e_polled_intr = 0;
   3657 		}
   3658 		if (!kstat_updated && esp->e_intr_kstat &&
   3659 		    rval == DDI_INTR_CLAIMED) {
   3660 			ESP_KSTAT_INTR(esp);
   3661 			kstat_updated++;
   3662 		}
   3663 
   3664 		/*
   3665 		 * check and empty the startQ
   3666 		 */
   3667 		ESP_CHECK_STARTQ_AND_ESP_MUTEX_EXIT(esp);
   3668 		ESP_WAKEUP_CALLBACK_THREAD(esp);
   3669 	} while (INTPENDING(esp));
   3670 
   3671 	return (rval);
   3672 }
   3673 
   3674 /*
   3675  * General interrupt service routine.
   3676  */
   3677 static char *dmaga_bits = DMAGA_BITS;
   3678 
   3679 static void
   3680 espsvc(struct esp *esp)
   3681 {
   3682 	static int (*evec[])(struct esp *esp) = {
   3683 		esp_finish_select,
   3684 		esp_reconnect,
   3685 		esp_phasemanage,
   3686 		esp_finish,
   3687 		esp_reset_recovery,
   3688 		esp_istart,
   3689 		esp_abort_curcmd,
   3690 		esp_abort_allcmds,
   3691 		esp_reset_bus,
   3692 		esp_handle_selection
   3693 	};
   3694 	int action;
   3695 	uchar_t intr;
   3696 	volatile struct espreg *ep = esp->e_reg;
   3697 	int i = 0;
   3698 
   3699 	TRACE_0(TR_FAC_SCSI, TR_ESPSVC_START, "espsvc_start");
   3700 
   3701 	/*
   3702 	 * A read of ESP interrupt register clears interrupt,
   3703 	 * so any other volatile information needs to be latched
   3704 	 * up prior to reading the interrupt register.
   3705 	 */
   3706 	esp->e_stat = ep->esp_stat;
   3707 
   3708 	/*
   3709 	 * unclear what could cause a gross error;
   3710 	 * most of the time we get a data overrun after this.
   3711 	 */
   3712 	if (esp->e_stat & ESP_STAT_GERR) {
   3713 		esplog(esp, CE_WARN,
   3714 		    "gross error in esp status (%x)", esp->e_stat);
   3715 		IPRINTF5("esp_cmd=%x, stat=%x, intr=%x, step=%x, fifoflag=%x\n",
   3716 		    ep->esp_cmd, esp->e_stat, esp->e_intr, ep->esp_step,
   3717 		    ep->esp_fifo_flag);
   3718 		if (esp->e_cur_slot != UNDEFINED) {
   3719 			struct esp_cmd *sp = CURRENT_CMD(esp);
   3720 			if (sp->cmd_pkt.pkt_reason == CMD_CMPLT) {
   3721 				sp->cmd_pkt.pkt_reason = CMD_TRAN_ERR;
   3722 			}
   3723 		} else {
   3724 			action = ACTION_ABORT_ALLCMDS;
   3725 			goto start_action;
   3726 		}
   3727 	}
   3728 
   3729 #ifdef ESPDEBUG
   3730 	if (esp_check_dma_error(esp)) {
   3731 		action = ACTION_RESET;
   3732 		goto start_action;
   3733 	}
   3734 #endif
   3735 
   3736 	/*
   3737 	 * the esp may post an interrupt even though we have just reset
   3738 	 * the bus and blown away the targets; therefore, check on
   3739 	 * reset state first and deal with reset recovery immediately
   3740 	 */
   3741 	if (esp->e_state == ACTS_RESET) {
   3742 		action = ACTION_FINRST;
   3743 		goto start_action;
   3744 	}
   3745 
   3746 
   3747 	/*
   3748 	 * While some documentation claims that the
   3749 	 * ESP100A's msb in the stat register is an
   3750 	 * INTERRUPT PENDING bit, an errata sheet
   3751 	 * warned that you shouldn't depend on that
   3752 	 * being so (unless you're an ESP-236)
   3753 	 */
   3754 	if (esp->e_options & ESP_OPT_MASK_OFF_STAT) {
   3755 		esp->e_stat &= ~ESP_STAT_RES;
   3756 	} else	if ((esp->e_stat & ESP_STAT_IPEND) == 0) {
   3757 		esp->e_intr = intr = ep->esp_intr;
   3758 		if (esp->e_intr & ESP_INT_RESET) {
   3759 			action = ACTION_FINRST;
   3760 			goto start_action;
   3761 		}
   3762 
   3763 		if (esp_check_dma_error(esp)) {
   3764 			action = ACTION_RESET;
   3765 			goto start_action;
   3766 		}
   3767 
   3768 		esplog(esp, CE_WARN, "Spurious interrupt");
   3769 		action = ACTION_RETURN;
   3770 		goto exit;
   3771 	}
   3772 
   3773 	/*
   3774 	 * now it is finally safe to read the interrupt register
   3775 	 */
   3776 	esp->e_intr = intr = ep->esp_intr;
   3777 
   3778 #ifdef	ESPDEBUG
   3779 	if (DEBUGGING) {
   3780 		eprintf(esp, "espsvc: dma csr 0x%b addr 0x%x\n",
   3781 		    esp->e_dma->dmaga_csr, dmaga_bits, esp->e_dma->dmaga_addr);
   3782 		esp_stat_int_print(esp);
   3783 		eprintf(esp, "\tState %s Laststate %s\n",
   3784 		    esp_state_name(esp->e_state),
   3785 		    esp_state_name(esp->e_laststate));
   3786 	}
   3787 #endif	/* ESPDEBUG */
   3788 
   3789 	/*
   3790 	 * Based upon the current state of the host adapter driver
   3791 	 * we should be able to figure out what to do with an interrupt.
   3792 	 * We have several possible interrupt sources, some of them
   3793 	 * modified by various status conditions.
   3794 	 *
   3795 	 * Basically, we'll get an interrupt through the dma gate array
   3796 	 * for one or more of the following three conditions:
   3797 	 *
   3798 	 *	1. The ESP is asserting an interrupt request.
   3799 	 *
   3800 	 *	2. There has been a memory exception of some kind.
   3801 	 *
   3802 	 * In the latter case we are either in one of the SCSI
   3803 	 * DATA phases or are using dma in sending a command to a
   3804 	 * target. We will let the various handlers for these kind
   3805 	 * of states decode any error conditions in the gate array.
   3806 	 *
   3807 	 * The ESP asserts an interrupt with one or more of 8 possible
   3808 	 * bits set in its interrupt register. These conditions are
   3809 	 * SCSI bus reset detected, an illegal command fed to the ESP,
   3810 	 * one of DISCONNECT, BUS SERVICE, FUNCTION COMPLETE conditions
   3811 	 * for the ESP, a Reselection interrupt, or one of Selection
   3812 	 * or Selection with Attention.
   3813 	 *
   3814 	 * Of these possible interrupts, we can deal with some right
   3815 	 * here and now, irrespective of the current state of the driver.
   3816 	 *
   3817 	 * take care of the most likely interrupts first and call the action
   3818 	 * immediately
   3819 	 */
   3820 	if ((intr & (ESP_INT_RESET|ESP_INT_ILLEGAL|ESP_INT_SEL|ESP_INT_SELATN|
   3821 	    ESP_INT_RESEL)) == 0) {
   3822 		/*
   3823 		 * The rest of the reasons for an interrupt, including
   3824 		 * interrupts just from the dma gate array itself, can
   3825 		 * be handled based purely on the state that the driver
   3826 		 * is currently in now.
   3827 		 */
   3828 		if (esp->e_state & STATE_SELECTING) {
   3829 			action = esp_finish_select(esp);
   3830 
   3831 		} else if (esp->e_state & STATE_ITPHASES) {
   3832 			action = esp_phasemanage(esp);
   3833 
   3834 		} else {
   3835 #ifdef	ESPDEBUG
   3836 			esp_printstate(esp, "spurious interrupt");
   3837 #endif	/* ESPDEBUG */
   3838 			esplog(esp, CE_WARN, "spurious interrupt");
   3839 			action = ACTION_RETURN;
   3840 		}
   3841 
   3842 	} else if ((intr & ESP_INT_RESEL) && ((intr &
   3843 	    (ESP_INT_RESET|ESP_INT_ILLEGAL|ESP_INT_SEL|ESP_INT_SELATN)) == 0)) {
   3844 
   3845 		if (esp->e_state & STATE_SELECTING) {
   3846 			action = esp_finish_select(esp);
   3847 
   3848 		} else if (esp->e_state != STATE_FREE) {
   3849 			/*
   3850 			 * this 'cannot happen'.
   3851 			 */
   3852 			esp_printstate(esp, "illegal reselection");
   3853 			action = ACTION_RESET;
   3854 		} else {
   3855 			action = esp_reconnect(esp);
   3856 		}
   3857 
   3858 
   3859 	} else if (intr & ESP_INT_RESET) {
   3860 		/*
   3861 		 * If we detect a SCSI reset, we blow away the current
   3862 		 * command (if there is one) and all disconnected commands
   3863 		 * because we now don't know the state of them at all.
   3864 		 */
   3865 		action = ACTION_FINRST;
   3866 
   3867 	} else if (intr & ESP_INT_ILLEGAL) {
   3868 		/*
   3869 		 * This should not happen. The one situation where
   3870 		 * we can get an ILLEGAL COMMAND interrupt is due to
   3871 		 * a bug in the ESP100 during reselection which we
   3872 		 * should be handling in esp_reconnect().
   3873 		 */
   3874 		IPRINTF1("lastcmd=%x\n", esp->e_reg->esp_cmd);
   3875 		esp_printstate(esp, "ILLEGAL bit set");
   3876 		action = ACTION_RESET;
   3877 
   3878 	} else if (intr & (ESP_INT_SEL|ESP_INT_SELATN)) {
   3879 		action = ACTION_SELECT;
   3880 	}
   3881 
   3882 start_action:
   3883 	while (action != ACTION_RETURN) {
   3884 		ASSERT((action >= 0) && (action <= ACTION_SELECT));
   3885 		TRACE_3(TR_FAC_SCSI, TR_ESPSVC_ACTION_CALL,
   3886 		    "espsvc call: esp 0x%p, action %d (%d)",
   3887 		    (void *)esp, action, i);
   3888 		i++;
   3889 		action = (*evec[action])(esp);
   3890 	}
   3891