xref: /onnv-gate/usr/src/uts/common/io/mr_sas/mr_sas.c (revision 12757:e25e7e1b3c65)
19440SSusan.Scheufele@Sun.COM /*
29440SSusan.Scheufele@Sun.COM  * mr_sas.c: source for mr_sas driver
39440SSusan.Scheufele@Sun.COM  *
49440SSusan.Scheufele@Sun.COM  * MegaRAID device driver for SAS2.0 controllers
511856SYu.Wu@Sun.COM  * Copyright (c) 2008-2010, LSI Logic Corporation.
69440SSusan.Scheufele@Sun.COM  * All rights reserved.
79440SSusan.Scheufele@Sun.COM  *
89440SSusan.Scheufele@Sun.COM  * Version:
99440SSusan.Scheufele@Sun.COM  * Author:
109440SSusan.Scheufele@Sun.COM  *		Arun Chandrashekhar
119440SSusan.Scheufele@Sun.COM  *		Manju R
1212244SYu.Wu@Sun.COM  *		Rajesh Prabhakaran
1312244SYu.Wu@Sun.COM  * 		Seokmann Ju
149440SSusan.Scheufele@Sun.COM  *
159440SSusan.Scheufele@Sun.COM  * Redistribution and use in source and binary forms, with or without
169440SSusan.Scheufele@Sun.COM  * modification, are permitted provided that the following conditions are met:
179440SSusan.Scheufele@Sun.COM  *
189440SSusan.Scheufele@Sun.COM  * 1. Redistributions of source code must retain the above copyright notice,
199440SSusan.Scheufele@Sun.COM  *    this list of conditions and the following disclaimer.
209440SSusan.Scheufele@Sun.COM  *
219440SSusan.Scheufele@Sun.COM  * 2. Redistributions in binary form must reproduce the above copyright notice,
229440SSusan.Scheufele@Sun.COM  *    this list of conditions and the following disclaimer in the documentation
239440SSusan.Scheufele@Sun.COM  *    and/or other materials provided with the distribution.
249440SSusan.Scheufele@Sun.COM  *
259440SSusan.Scheufele@Sun.COM  * 3. Neither the name of the author nor the names of its contributors may be
269440SSusan.Scheufele@Sun.COM  *    used to endorse or promote products derived from this software without
279440SSusan.Scheufele@Sun.COM  *    specific prior written permission.
289440SSusan.Scheufele@Sun.COM  *
299440SSusan.Scheufele@Sun.COM  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
309440SSusan.Scheufele@Sun.COM  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
319440SSusan.Scheufele@Sun.COM  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
329440SSusan.Scheufele@Sun.COM  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
339440SSusan.Scheufele@Sun.COM  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
349440SSusan.Scheufele@Sun.COM  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
359440SSusan.Scheufele@Sun.COM  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
369440SSusan.Scheufele@Sun.COM  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
379440SSusan.Scheufele@Sun.COM  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
389440SSusan.Scheufele@Sun.COM  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
399440SSusan.Scheufele@Sun.COM  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
409440SSusan.Scheufele@Sun.COM  * DAMAGE.
419440SSusan.Scheufele@Sun.COM  */
429440SSusan.Scheufele@Sun.COM 
439440SSusan.Scheufele@Sun.COM /*
4412244SYu.Wu@Sun.COM  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
459440SSusan.Scheufele@Sun.COM  */
469440SSusan.Scheufele@Sun.COM 
479440SSusan.Scheufele@Sun.COM #include <sys/types.h>
489440SSusan.Scheufele@Sun.COM #include <sys/param.h>
499440SSusan.Scheufele@Sun.COM #include <sys/file.h>
509440SSusan.Scheufele@Sun.COM #include <sys/errno.h>
519440SSusan.Scheufele@Sun.COM #include <sys/open.h>
529440SSusan.Scheufele@Sun.COM #include <sys/cred.h>
539440SSusan.Scheufele@Sun.COM #include <sys/modctl.h>
549440SSusan.Scheufele@Sun.COM #include <sys/conf.h>
559440SSusan.Scheufele@Sun.COM #include <sys/devops.h>
569440SSusan.Scheufele@Sun.COM #include <sys/cmn_err.h>
579440SSusan.Scheufele@Sun.COM #include <sys/kmem.h>
589440SSusan.Scheufele@Sun.COM #include <sys/stat.h>
599440SSusan.Scheufele@Sun.COM #include <sys/mkdev.h>
609440SSusan.Scheufele@Sun.COM #include <sys/pci.h>
619440SSusan.Scheufele@Sun.COM #include <sys/scsi/scsi.h>
629440SSusan.Scheufele@Sun.COM #include <sys/ddi.h>
639440SSusan.Scheufele@Sun.COM #include <sys/sunddi.h>
649440SSusan.Scheufele@Sun.COM #include <sys/atomic.h>
659440SSusan.Scheufele@Sun.COM #include <sys/signal.h>
669795SYu.Wu@Sun.COM #include <sys/byteorder.h>
6710820SSusan.Scheufele@Sun.COM #include <sys/sdt.h>
689440SSusan.Scheufele@Sun.COM #include <sys/fs/dv_node.h>	/* devfs_clean */
699440SSusan.Scheufele@Sun.COM 
709440SSusan.Scheufele@Sun.COM #include "mr_sas.h"
719440SSusan.Scheufele@Sun.COM 
729440SSusan.Scheufele@Sun.COM /*
739440SSusan.Scheufele@Sun.COM  * FMA header files
749440SSusan.Scheufele@Sun.COM  */
759440SSusan.Scheufele@Sun.COM #include <sys/ddifm.h>
769440SSusan.Scheufele@Sun.COM #include <sys/fm/protocol.h>
779440SSusan.Scheufele@Sun.COM #include <sys/fm/util.h>
789440SSusan.Scheufele@Sun.COM #include <sys/fm/io/ddi.h>
799440SSusan.Scheufele@Sun.COM 
809440SSusan.Scheufele@Sun.COM /*
819440SSusan.Scheufele@Sun.COM  * Local static data
829440SSusan.Scheufele@Sun.COM  */
839440SSusan.Scheufele@Sun.COM static void	*mrsas_state = NULL;
8410600SYu.Wu@Sun.COM static volatile boolean_t	mrsas_relaxed_ordering = B_TRUE;
8510600SYu.Wu@Sun.COM static volatile int 	debug_level_g = CL_NONE;
8610600SYu.Wu@Sun.COM static volatile int 	msi_enable = 1;
8711856SYu.Wu@Sun.COM static volatile int 	ctio_enable = 1;
889440SSusan.Scheufele@Sun.COM 
8912244SYu.Wu@Sun.COM /* Default Timeout value to issue online controller reset */
90*12757SYu.Wu@Sun.COM static volatile int  debug_timeout_g  = 0xB4;
9112244SYu.Wu@Sun.COM /* Simulate consecutive firmware fault */
9212244SYu.Wu@Sun.COM static volatile int  debug_fw_faults_after_ocr_g  = 0;
9312244SYu.Wu@Sun.COM 
9412244SYu.Wu@Sun.COM #ifdef OCRDEBUG
9512244SYu.Wu@Sun.COM /* Simulate three consecutive timeout for an IO */
9612244SYu.Wu@Sun.COM static volatile int  debug_consecutive_timeout_after_ocr_g  = 0;
9712244SYu.Wu@Sun.COM #endif
9812244SYu.Wu@Sun.COM 
999440SSusan.Scheufele@Sun.COM #pragma weak scsi_hba_open
1009440SSusan.Scheufele@Sun.COM #pragma weak scsi_hba_close
1019440SSusan.Scheufele@Sun.COM #pragma weak scsi_hba_ioctl
1029440SSusan.Scheufele@Sun.COM 
1039440SSusan.Scheufele@Sun.COM static ddi_dma_attr_t mrsas_generic_dma_attr = {
1049440SSusan.Scheufele@Sun.COM 	DMA_ATTR_V0,		/* dma_attr_version */
1059440SSusan.Scheufele@Sun.COM 	0,			/* low DMA address range */
1069440SSusan.Scheufele@Sun.COM 	0xFFFFFFFFU,		/* high DMA address range */
1079440SSusan.Scheufele@Sun.COM 	0xFFFFFFFFU,		/* DMA counter register  */
1089440SSusan.Scheufele@Sun.COM 	8,			/* DMA address alignment */
1099440SSusan.Scheufele@Sun.COM 	0x07,			/* DMA burstsizes  */
1109440SSusan.Scheufele@Sun.COM 	1,			/* min DMA size */
1119440SSusan.Scheufele@Sun.COM 	0xFFFFFFFFU,		/* max DMA size */
1129440SSusan.Scheufele@Sun.COM 	0xFFFFFFFFU,		/* segment boundary */
1139440SSusan.Scheufele@Sun.COM 	MRSAS_MAX_SGE_CNT,	/* dma_attr_sglen */
1149440SSusan.Scheufele@Sun.COM 	512,			/* granularity of device */
1159440SSusan.Scheufele@Sun.COM 	0			/* bus specific DMA flags */
1169440SSusan.Scheufele@Sun.COM };
1179440SSusan.Scheufele@Sun.COM 
1189440SSusan.Scheufele@Sun.COM int32_t mrsas_max_cap_maxxfer = 0x1000000;
1199440SSusan.Scheufele@Sun.COM 
1209440SSusan.Scheufele@Sun.COM /*
1219440SSusan.Scheufele@Sun.COM  * cb_ops contains base level routines
1229440SSusan.Scheufele@Sun.COM  */
1239440SSusan.Scheufele@Sun.COM static struct cb_ops mrsas_cb_ops = {
1249440SSusan.Scheufele@Sun.COM 	mrsas_open,		/* open */
1259440SSusan.Scheufele@Sun.COM 	mrsas_close,		/* close */
1269440SSusan.Scheufele@Sun.COM 	nodev,			/* strategy */
1279440SSusan.Scheufele@Sun.COM 	nodev,			/* print */
1289440SSusan.Scheufele@Sun.COM 	nodev,			/* dump */
1299440SSusan.Scheufele@Sun.COM 	nodev,			/* read */
1309440SSusan.Scheufele@Sun.COM 	nodev,			/* write */
1319440SSusan.Scheufele@Sun.COM 	mrsas_ioctl,		/* ioctl */
1329440SSusan.Scheufele@Sun.COM 	nodev,			/* devmap */
1339440SSusan.Scheufele@Sun.COM 	nodev,			/* mmap */
1349440SSusan.Scheufele@Sun.COM 	nodev,			/* segmap */
1359440SSusan.Scheufele@Sun.COM 	nochpoll,		/* poll */
1369440SSusan.Scheufele@Sun.COM 	nodev,			/* cb_prop_op */
1379440SSusan.Scheufele@Sun.COM 	0,			/* streamtab  */
1389440SSusan.Scheufele@Sun.COM 	D_NEW | D_HOTPLUG,	/* cb_flag */
1399440SSusan.Scheufele@Sun.COM 	CB_REV,			/* cb_rev */
1409440SSusan.Scheufele@Sun.COM 	nodev,			/* cb_aread */
1419440SSusan.Scheufele@Sun.COM 	nodev			/* cb_awrite */
1429440SSusan.Scheufele@Sun.COM };
1439440SSusan.Scheufele@Sun.COM 
1449440SSusan.Scheufele@Sun.COM /*
1459440SSusan.Scheufele@Sun.COM  * dev_ops contains configuration routines
1469440SSusan.Scheufele@Sun.COM  */
1479440SSusan.Scheufele@Sun.COM static struct dev_ops mrsas_ops = {
1489440SSusan.Scheufele@Sun.COM 	DEVO_REV,		/* rev, */
1499440SSusan.Scheufele@Sun.COM 	0,			/* refcnt */
1509440SSusan.Scheufele@Sun.COM 	mrsas_getinfo,		/* getinfo */
1519440SSusan.Scheufele@Sun.COM 	nulldev,		/* identify */
1529440SSusan.Scheufele@Sun.COM 	nulldev,		/* probe */
1539440SSusan.Scheufele@Sun.COM 	mrsas_attach,		/* attach */
1549440SSusan.Scheufele@Sun.COM 	mrsas_detach,		/* detach */
15512356SYu.Wu@Sun.COM #ifdef  __sparc
1569440SSusan.Scheufele@Sun.COM 	mrsas_reset,		/* reset */
15712356SYu.Wu@Sun.COM #else	/* __sparc */
15812356SYu.Wu@Sun.COM 	nodev,
15912356SYu.Wu@Sun.COM #endif  /* __sparc */
1609440SSusan.Scheufele@Sun.COM 	&mrsas_cb_ops,		/* char/block ops */
1619440SSusan.Scheufele@Sun.COM 	NULL,			/* bus ops */
1629440SSusan.Scheufele@Sun.COM 	NULL,			/* power */
16312356SYu.Wu@Sun.COM #ifdef	__sparc
16412356SYu.Wu@Sun.COM 	ddi_quiesce_not_needed
16512356SYu.Wu@Sun.COM #else	/* __sparc */
16612356SYu.Wu@Sun.COM 	mrsas_quiesce		/* quiesce */
16712356SYu.Wu@Sun.COM #endif	/* __sparc */
1689440SSusan.Scheufele@Sun.COM };
1699440SSusan.Scheufele@Sun.COM 
1709440SSusan.Scheufele@Sun.COM char _depends_on[] = "misc/scsi";
1719440SSusan.Scheufele@Sun.COM 
1729440SSusan.Scheufele@Sun.COM static struct modldrv modldrv = {
1739440SSusan.Scheufele@Sun.COM 	&mod_driverops,		/* module type - driver */
1749440SSusan.Scheufele@Sun.COM 	MRSAS_VERSION,
1759440SSusan.Scheufele@Sun.COM 	&mrsas_ops,		/* driver ops */
1769440SSusan.Scheufele@Sun.COM };
1779440SSusan.Scheufele@Sun.COM 
1789440SSusan.Scheufele@Sun.COM static struct modlinkage modlinkage = {
1799440SSusan.Scheufele@Sun.COM 	MODREV_1,	/* ml_rev - must be MODREV_1 */
1809440SSusan.Scheufele@Sun.COM 	&modldrv,	/* ml_linkage */
1819440SSusan.Scheufele@Sun.COM 	NULL		/* end of driver linkage */
1829440SSusan.Scheufele@Sun.COM };
1839440SSusan.Scheufele@Sun.COM 
1849440SSusan.Scheufele@Sun.COM static struct ddi_device_acc_attr endian_attr = {
18511236SStephen.Hanson@Sun.COM 	DDI_DEVICE_ATTR_V1,
1869440SSusan.Scheufele@Sun.COM 	DDI_STRUCTURE_LE_ACC,
18711236SStephen.Hanson@Sun.COM 	DDI_STRICTORDER_ACC,
18811236SStephen.Hanson@Sun.COM 	DDI_DEFAULT_ACC
1899440SSusan.Scheufele@Sun.COM };
1909440SSusan.Scheufele@Sun.COM 
1919440SSusan.Scheufele@Sun.COM 
1929440SSusan.Scheufele@Sun.COM /*
1939440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
1949440SSusan.Scheufele@Sun.COM  *                                                                            *
1959440SSusan.Scheufele@Sun.COM  *         common entry points - for loadable kernel modules                  *
1969440SSusan.Scheufele@Sun.COM  *                                                                            *
1979440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
1989440SSusan.Scheufele@Sun.COM  */
1999440SSusan.Scheufele@Sun.COM 
2009440SSusan.Scheufele@Sun.COM int
_init(void)2019440SSusan.Scheufele@Sun.COM _init(void)
2029440SSusan.Scheufele@Sun.COM {
2039440SSusan.Scheufele@Sun.COM 	int ret;
2049440SSusan.Scheufele@Sun.COM 
2059440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2069440SSusan.Scheufele@Sun.COM 
2079440SSusan.Scheufele@Sun.COM 	ret = ddi_soft_state_init(&mrsas_state,
2089440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_instance), 0);
2099440SSusan.Scheufele@Sun.COM 
2109440SSusan.Scheufele@Sun.COM 	if (ret != DDI_SUCCESS) {
2119440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas: could not init state"));
2129440SSusan.Scheufele@Sun.COM 		return (ret);
2139440SSusan.Scheufele@Sun.COM 	}
2149440SSusan.Scheufele@Sun.COM 
2159440SSusan.Scheufele@Sun.COM 	if ((ret = scsi_hba_init(&modlinkage)) != DDI_SUCCESS) {
2169440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas: could not init scsi hba"));
2179440SSusan.Scheufele@Sun.COM 		ddi_soft_state_fini(&mrsas_state);
2189440SSusan.Scheufele@Sun.COM 		return (ret);
2199440SSusan.Scheufele@Sun.COM 	}
2209440SSusan.Scheufele@Sun.COM 
2219440SSusan.Scheufele@Sun.COM 	ret = mod_install(&modlinkage);
2229440SSusan.Scheufele@Sun.COM 
2239440SSusan.Scheufele@Sun.COM 	if (ret != DDI_SUCCESS) {
2249440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas: mod_install failed"));
2259440SSusan.Scheufele@Sun.COM 		scsi_hba_fini(&modlinkage);
2269440SSusan.Scheufele@Sun.COM 		ddi_soft_state_fini(&mrsas_state);
2279440SSusan.Scheufele@Sun.COM 	}
2289440SSusan.Scheufele@Sun.COM 
2299440SSusan.Scheufele@Sun.COM 	return (ret);
2309440SSusan.Scheufele@Sun.COM }
2319440SSusan.Scheufele@Sun.COM 
2329440SSusan.Scheufele@Sun.COM int
_info(struct modinfo * modinfop)2339440SSusan.Scheufele@Sun.COM _info(struct modinfo *modinfop)
2349440SSusan.Scheufele@Sun.COM {
2359440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2369440SSusan.Scheufele@Sun.COM 
2379440SSusan.Scheufele@Sun.COM 	return (mod_info(&modlinkage, modinfop));
2389440SSusan.Scheufele@Sun.COM }
2399440SSusan.Scheufele@Sun.COM 
2409440SSusan.Scheufele@Sun.COM int
_fini(void)2419440SSusan.Scheufele@Sun.COM _fini(void)
2429440SSusan.Scheufele@Sun.COM {
2439440SSusan.Scheufele@Sun.COM 	int ret;
2449440SSusan.Scheufele@Sun.COM 
2459440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2469440SSusan.Scheufele@Sun.COM 
2479440SSusan.Scheufele@Sun.COM 	if ((ret = mod_remove(&modlinkage)) != DDI_SUCCESS)
2489440SSusan.Scheufele@Sun.COM 		return (ret);
2499440SSusan.Scheufele@Sun.COM 
2509440SSusan.Scheufele@Sun.COM 	scsi_hba_fini(&modlinkage);
2519440SSusan.Scheufele@Sun.COM 
2529440SSusan.Scheufele@Sun.COM 	ddi_soft_state_fini(&mrsas_state);
2539440SSusan.Scheufele@Sun.COM 
2549440SSusan.Scheufele@Sun.COM 	return (ret);
2559440SSusan.Scheufele@Sun.COM }
2569440SSusan.Scheufele@Sun.COM 
2579440SSusan.Scheufele@Sun.COM 
2589440SSusan.Scheufele@Sun.COM /*
2599440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
2609440SSusan.Scheufele@Sun.COM  *                                                                            *
2619440SSusan.Scheufele@Sun.COM  *               common entry points - for autoconfiguration                  *
2629440SSusan.Scheufele@Sun.COM  *                                                                            *
2639440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
2649440SSusan.Scheufele@Sun.COM  */
2659440SSusan.Scheufele@Sun.COM 
2669440SSusan.Scheufele@Sun.COM static int
mrsas_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2679440SSusan.Scheufele@Sun.COM mrsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2689440SSusan.Scheufele@Sun.COM {
2699440SSusan.Scheufele@Sun.COM 	int		instance_no;
2709440SSusan.Scheufele@Sun.COM 	int		nregs;
2719440SSusan.Scheufele@Sun.COM 	uint8_t		added_isr_f = 0;
2729440SSusan.Scheufele@Sun.COM 	uint8_t		added_soft_isr_f = 0;
2739440SSusan.Scheufele@Sun.COM 	uint8_t		create_devctl_node_f = 0;
2749440SSusan.Scheufele@Sun.COM 	uint8_t		create_scsi_node_f = 0;
2759440SSusan.Scheufele@Sun.COM 	uint8_t		create_ioc_node_f = 0;
2769440SSusan.Scheufele@Sun.COM 	uint8_t		tran_alloc_f = 0;
2779440SSusan.Scheufele@Sun.COM 	uint8_t 	irq;
2789440SSusan.Scheufele@Sun.COM 	uint16_t	vendor_id;
2799440SSusan.Scheufele@Sun.COM 	uint16_t	device_id;
2809440SSusan.Scheufele@Sun.COM 	uint16_t	subsysvid;
2819440SSusan.Scheufele@Sun.COM 	uint16_t	subsysid;
2829440SSusan.Scheufele@Sun.COM 	uint16_t	command;
2839440SSusan.Scheufele@Sun.COM 	off_t		reglength = 0;
2849440SSusan.Scheufele@Sun.COM 	int		intr_types = 0;
2859440SSusan.Scheufele@Sun.COM 	char		*data;
2869440SSusan.Scheufele@Sun.COM 
2879440SSusan.Scheufele@Sun.COM 	scsi_hba_tran_t		*tran;
2889440SSusan.Scheufele@Sun.COM 	ddi_dma_attr_t  tran_dma_attr;
2899440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance;
2909440SSusan.Scheufele@Sun.COM 
2919440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2929440SSusan.Scheufele@Sun.COM 
2939440SSusan.Scheufele@Sun.COM 	/* CONSTCOND */
2949440SSusan.Scheufele@Sun.COM 	ASSERT(NO_COMPETING_THREADS);
2959440SSusan.Scheufele@Sun.COM 
2969440SSusan.Scheufele@Sun.COM 	instance_no = ddi_get_instance(dip);
2979440SSusan.Scheufele@Sun.COM 
2989440SSusan.Scheufele@Sun.COM 	/*
2999440SSusan.Scheufele@Sun.COM 	 * check to see whether this device is in a DMA-capable slot.
3009440SSusan.Scheufele@Sun.COM 	 */
3019440SSusan.Scheufele@Sun.COM 	if (ddi_slaveonly(dip) == DDI_SUCCESS) {
3029440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
3039440SSusan.Scheufele@Sun.COM 		    "mr_sas%d: Device in slave-only slot, unused",
3049440SSusan.Scheufele@Sun.COM 		    instance_no));
3059440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
3069440SSusan.Scheufele@Sun.COM 	}
3079440SSusan.Scheufele@Sun.COM 
3089440SSusan.Scheufele@Sun.COM 	switch (cmd) {
3099440SSusan.Scheufele@Sun.COM 		case DDI_ATTACH:
3109440SSusan.Scheufele@Sun.COM 			con_log(CL_DLEVEL1, (CE_NOTE, "mr_sas: DDI_ATTACH"));
3119440SSusan.Scheufele@Sun.COM 			/* allocate the soft state for the instance */
3129440SSusan.Scheufele@Sun.COM 			if (ddi_soft_state_zalloc(mrsas_state, instance_no)
3139440SSusan.Scheufele@Sun.COM 			    != DDI_SUCCESS) {
3149440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
3159440SSusan.Scheufele@Sun.COM 				    "mr_sas%d: Failed to allocate soft state",
3169440SSusan.Scheufele@Sun.COM 				    instance_no));
3179440SSusan.Scheufele@Sun.COM 
3189440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
3199440SSusan.Scheufele@Sun.COM 			}
3209440SSusan.Scheufele@Sun.COM 
3219440SSusan.Scheufele@Sun.COM 			instance = (struct mrsas_instance *)ddi_get_soft_state
3229440SSusan.Scheufele@Sun.COM 			    (mrsas_state, instance_no);
3239440SSusan.Scheufele@Sun.COM 
3249440SSusan.Scheufele@Sun.COM 			if (instance == NULL) {
3259440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
3269440SSusan.Scheufele@Sun.COM 				    "mr_sas%d: Bad soft state", instance_no));
3279440SSusan.Scheufele@Sun.COM 
3289440SSusan.Scheufele@Sun.COM 				ddi_soft_state_free(mrsas_state, instance_no);
3299440SSusan.Scheufele@Sun.COM 
3309440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
3319440SSusan.Scheufele@Sun.COM 			}
3329440SSusan.Scheufele@Sun.COM 
3339440SSusan.Scheufele@Sun.COM 			bzero((caddr_t)instance,
3349440SSusan.Scheufele@Sun.COM 			    sizeof (struct mrsas_instance));
3359440SSusan.Scheufele@Sun.COM 
3369440SSusan.Scheufele@Sun.COM 			instance->func_ptr = kmem_zalloc(
3379440SSusan.Scheufele@Sun.COM 			    sizeof (struct mrsas_func_ptr), KM_SLEEP);
3389440SSusan.Scheufele@Sun.COM 			ASSERT(instance->func_ptr);
3399440SSusan.Scheufele@Sun.COM 
3409440SSusan.Scheufele@Sun.COM 			/* Setup the PCI configuration space handles */
3419440SSusan.Scheufele@Sun.COM 			if (pci_config_setup(dip, &instance->pci_handle) !=
3429440SSusan.Scheufele@Sun.COM 			    DDI_SUCCESS) {
3439440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
3449440SSusan.Scheufele@Sun.COM 				    "mr_sas%d: pci config setup failed ",
3459440SSusan.Scheufele@Sun.COM 				    instance_no));
3469440SSusan.Scheufele@Sun.COM 
3479440SSusan.Scheufele@Sun.COM 				kmem_free(instance->func_ptr,
3489440SSusan.Scheufele@Sun.COM 				    sizeof (struct mrsas_func_ptr));
3499440SSusan.Scheufele@Sun.COM 				ddi_soft_state_free(mrsas_state, instance_no);
3509440SSusan.Scheufele@Sun.COM 
3519440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
3529440SSusan.Scheufele@Sun.COM 			}
3539440SSusan.Scheufele@Sun.COM 
3549440SSusan.Scheufele@Sun.COM 			if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) {
3559440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
3569440SSusan.Scheufele@Sun.COM 				    "mr_sas: failed to get registers."));
3579440SSusan.Scheufele@Sun.COM 
3589440SSusan.Scheufele@Sun.COM 				pci_config_teardown(&instance->pci_handle);
3599440SSusan.Scheufele@Sun.COM 				kmem_free(instance->func_ptr,
3609440SSusan.Scheufele@Sun.COM 				    sizeof (struct mrsas_func_ptr));
3619440SSusan.Scheufele@Sun.COM 				ddi_soft_state_free(mrsas_state, instance_no);
3629440SSusan.Scheufele@Sun.COM 
3639440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
3649440SSusan.Scheufele@Sun.COM 			}
3659440SSusan.Scheufele@Sun.COM 
3669440SSusan.Scheufele@Sun.COM 			vendor_id = pci_config_get16(instance->pci_handle,
3679440SSusan.Scheufele@Sun.COM 			    PCI_CONF_VENID);
3689440SSusan.Scheufele@Sun.COM 			device_id = pci_config_get16(instance->pci_handle,
3699440SSusan.Scheufele@Sun.COM 			    PCI_CONF_DEVID);
3709440SSusan.Scheufele@Sun.COM 
3719440SSusan.Scheufele@Sun.COM 			subsysvid = pci_config_get16(instance->pci_handle,
3729440SSusan.Scheufele@Sun.COM 			    PCI_CONF_SUBVENID);
3739440SSusan.Scheufele@Sun.COM 			subsysid = pci_config_get16(instance->pci_handle,
3749440SSusan.Scheufele@Sun.COM 			    PCI_CONF_SUBSYSID);
3759440SSusan.Scheufele@Sun.COM 
3769440SSusan.Scheufele@Sun.COM 			pci_config_put16(instance->pci_handle, PCI_CONF_COMM,
3779440SSusan.Scheufele@Sun.COM 			    (pci_config_get16(instance->pci_handle,
3789440SSusan.Scheufele@Sun.COM 			    PCI_CONF_COMM) | PCI_COMM_ME));
3799440SSusan.Scheufele@Sun.COM 			irq = pci_config_get8(instance->pci_handle,
3809440SSusan.Scheufele@Sun.COM 			    PCI_CONF_ILINE);
3819440SSusan.Scheufele@Sun.COM 
3829440SSusan.Scheufele@Sun.COM 			con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: "
3839440SSusan.Scheufele@Sun.COM 			    "0x%x:0x%x 0x%x:0x%x, irq:%d drv-ver:%s",
3849440SSusan.Scheufele@Sun.COM 			    instance_no, vendor_id, device_id, subsysvid,
3859440SSusan.Scheufele@Sun.COM 			    subsysid, irq, MRSAS_VERSION));
3869440SSusan.Scheufele@Sun.COM 
3879440SSusan.Scheufele@Sun.COM 			/* enable bus-mastering */
3889440SSusan.Scheufele@Sun.COM 			command = pci_config_get16(instance->pci_handle,
3899440SSusan.Scheufele@Sun.COM 			    PCI_CONF_COMM);
3909440SSusan.Scheufele@Sun.COM 
3919440SSusan.Scheufele@Sun.COM 			if (!(command & PCI_COMM_ME)) {
3929440SSusan.Scheufele@Sun.COM 				command |= PCI_COMM_ME;
3939440SSusan.Scheufele@Sun.COM 
3949440SSusan.Scheufele@Sun.COM 				pci_config_put16(instance->pci_handle,
3959440SSusan.Scheufele@Sun.COM 				    PCI_CONF_COMM, command);
3969440SSusan.Scheufele@Sun.COM 
3979440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_CONT, "mr_sas%d: "
3989440SSusan.Scheufele@Sun.COM 				    "enable bus-mastering", instance_no));
3999440SSusan.Scheufele@Sun.COM 			} else {
4009440SSusan.Scheufele@Sun.COM 				con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: "
4019440SSusan.Scheufele@Sun.COM 				"bus-mastering already set", instance_no));
4029440SSusan.Scheufele@Sun.COM 			}
4039440SSusan.Scheufele@Sun.COM 
4049440SSusan.Scheufele@Sun.COM 			/* initialize function pointers */
4059440SSusan.Scheufele@Sun.COM 			if ((device_id == PCI_DEVICE_ID_LSI_2108VDE) ||
4069440SSusan.Scheufele@Sun.COM 			    (device_id == PCI_DEVICE_ID_LSI_2108V)) {
4079440SSusan.Scheufele@Sun.COM 				con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: "
4089440SSusan.Scheufele@Sun.COM 				    "2108V/DE detected", instance_no));
4099440SSusan.Scheufele@Sun.COM 				instance->func_ptr->read_fw_status_reg =
4109440SSusan.Scheufele@Sun.COM 				    read_fw_status_reg_ppc;
4119440SSusan.Scheufele@Sun.COM 				instance->func_ptr->issue_cmd = issue_cmd_ppc;
4129440SSusan.Scheufele@Sun.COM 				instance->func_ptr->issue_cmd_in_sync_mode =
4139440SSusan.Scheufele@Sun.COM 				    issue_cmd_in_sync_mode_ppc;
4149440SSusan.Scheufele@Sun.COM 				instance->func_ptr->issue_cmd_in_poll_mode =
4159440SSusan.Scheufele@Sun.COM 				    issue_cmd_in_poll_mode_ppc;
4169440SSusan.Scheufele@Sun.COM 				instance->func_ptr->enable_intr =
4179440SSusan.Scheufele@Sun.COM 				    enable_intr_ppc;
4189440SSusan.Scheufele@Sun.COM 				instance->func_ptr->disable_intr =
4199440SSusan.Scheufele@Sun.COM 				    disable_intr_ppc;
4209440SSusan.Scheufele@Sun.COM 				instance->func_ptr->intr_ack = intr_ack_ppc;
4219440SSusan.Scheufele@Sun.COM 			} else {
4229440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
4239440SSusan.Scheufele@Sun.COM 				    "mr_sas: Invalid device detected"));
4249440SSusan.Scheufele@Sun.COM 
4259440SSusan.Scheufele@Sun.COM 				pci_config_teardown(&instance->pci_handle);
4269440SSusan.Scheufele@Sun.COM 				kmem_free(instance->func_ptr,
4279440SSusan.Scheufele@Sun.COM 				    sizeof (struct mrsas_func_ptr));
4289440SSusan.Scheufele@Sun.COM 				ddi_soft_state_free(mrsas_state, instance_no);
4299440SSusan.Scheufele@Sun.COM 
4309440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
4319440SSusan.Scheufele@Sun.COM 			}
4329440SSusan.Scheufele@Sun.COM 
4339440SSusan.Scheufele@Sun.COM 			instance->baseaddress = pci_config_get32(
4349440SSusan.Scheufele@Sun.COM 			    instance->pci_handle, PCI_CONF_BASE0);
4359440SSusan.Scheufele@Sun.COM 			instance->baseaddress &= 0x0fffc;
4369440SSusan.Scheufele@Sun.COM 
4379440SSusan.Scheufele@Sun.COM 			instance->dip		= dip;
4389440SSusan.Scheufele@Sun.COM 			instance->vendor_id	= vendor_id;
4399440SSusan.Scheufele@Sun.COM 			instance->device_id	= device_id;
4409440SSusan.Scheufele@Sun.COM 			instance->subsysvid	= subsysvid;
4419440SSusan.Scheufele@Sun.COM 			instance->subsysid	= subsysid;
4429440SSusan.Scheufele@Sun.COM 			instance->instance	= instance_no;
4439440SSusan.Scheufele@Sun.COM 
4449440SSusan.Scheufele@Sun.COM 			/* Initialize FMA */
4459440SSusan.Scheufele@Sun.COM 			instance->fm_capabilities = ddi_prop_get_int(
4469440SSusan.Scheufele@Sun.COM 			    DDI_DEV_T_ANY, instance->dip, DDI_PROP_DONTPASS,
4479440SSusan.Scheufele@Sun.COM 			    "fm-capable", DDI_FM_EREPORT_CAPABLE |
4489440SSusan.Scheufele@Sun.COM 			    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE
4499440SSusan.Scheufele@Sun.COM 			    | DDI_FM_ERRCB_CAPABLE);
4509440SSusan.Scheufele@Sun.COM 
4519440SSusan.Scheufele@Sun.COM 			mrsas_fm_init(instance);
4529440SSusan.Scheufele@Sun.COM 
4539440SSusan.Scheufele@Sun.COM 			/* Initialize Interrupts */
4549440SSusan.Scheufele@Sun.COM 			if ((ddi_dev_regsize(instance->dip,
4559440SSusan.Scheufele@Sun.COM 			    REGISTER_SET_IO_2108, &reglength) != DDI_SUCCESS) ||
4569440SSusan.Scheufele@Sun.COM 			    reglength < MINIMUM_MFI_MEM_SZ) {
4579440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
4589440SSusan.Scheufele@Sun.COM 			}
4599440SSusan.Scheufele@Sun.COM 			if (reglength > DEFAULT_MFI_MEM_SZ) {
4609440SSusan.Scheufele@Sun.COM 				reglength = DEFAULT_MFI_MEM_SZ;
4619440SSusan.Scheufele@Sun.COM 				con_log(CL_DLEVEL1, (CE_NOTE,
4629440SSusan.Scheufele@Sun.COM 				    "mr_sas: register length to map is "
4639440SSusan.Scheufele@Sun.COM 				    "0x%lx bytes", reglength));
4649440SSusan.Scheufele@Sun.COM 			}
4659440SSusan.Scheufele@Sun.COM 			if (ddi_regs_map_setup(instance->dip,
4669440SSusan.Scheufele@Sun.COM 			    REGISTER_SET_IO_2108, &instance->regmap, 0,
4679440SSusan.Scheufele@Sun.COM 			    reglength, &endian_attr, &instance->regmap_handle)
4689440SSusan.Scheufele@Sun.COM 			    != DDI_SUCCESS) {
4699440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_NOTE,
4709440SSusan.Scheufele@Sun.COM 				    "mr_sas: couldn't map control registers"));
4719440SSusan.Scheufele@Sun.COM 				goto fail_attach;
4729440SSusan.Scheufele@Sun.COM 			}
4739440SSusan.Scheufele@Sun.COM 
4749440SSusan.Scheufele@Sun.COM 			/*
4759440SSusan.Scheufele@Sun.COM 			 * Disable Interrupt Now.
4769440SSusan.Scheufele@Sun.COM 			 * Setup Software interrupt
4779440SSusan.Scheufele@Sun.COM 			 */
4789440SSusan.Scheufele@Sun.COM 			instance->func_ptr->disable_intr(instance);
4799440SSusan.Scheufele@Sun.COM 
4809440SSusan.Scheufele@Sun.COM 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
4819440SSusan.Scheufele@Sun.COM 			    "mrsas-enable-msi", &data) == DDI_SUCCESS) {
48210600SYu.Wu@Sun.COM 				if (strncmp(data, "no", 3) == 0) {
48310600SYu.Wu@Sun.COM 					msi_enable = 0;
48410600SYu.Wu@Sun.COM 					con_log(CL_ANN1, (CE_WARN,
48510600SYu.Wu@Sun.COM 					    "msi_enable = %d disabled",
4869440SSusan.Scheufele@Sun.COM 					    msi_enable));
4879440SSusan.Scheufele@Sun.COM 				}
4889440SSusan.Scheufele@Sun.COM 				ddi_prop_free(data);
4899440SSusan.Scheufele@Sun.COM 			}
4909440SSusan.Scheufele@Sun.COM 
4919440SSusan.Scheufele@Sun.COM 			con_log(CL_DLEVEL1, (CE_WARN, "msi_enable = %d",
4929440SSusan.Scheufele@Sun.COM 			    msi_enable));
4939440SSusan.Scheufele@Sun.COM 
4949440SSusan.Scheufele@Sun.COM 			/* Check for all supported interrupt types */
4959440SSusan.Scheufele@Sun.COM 			if (ddi_intr_get_supported_types(
4969440SSusan.Scheufele@Sun.COM 			    dip, &intr_types) != DDI_SUCCESS) {
4979440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
4989440SSusan.Scheufele@Sun.COM 				    "ddi_intr_get_supported_types() failed"));
4999440SSusan.Scheufele@Sun.COM 				goto fail_attach;
5009440SSusan.Scheufele@Sun.COM 			}
5019440SSusan.Scheufele@Sun.COM 
5029440SSusan.Scheufele@Sun.COM 			con_log(CL_DLEVEL1, (CE_NOTE,
5039440SSusan.Scheufele@Sun.COM 			    "ddi_intr_get_supported_types() ret: 0x%x",
5049440SSusan.Scheufele@Sun.COM 			    intr_types));
5059440SSusan.Scheufele@Sun.COM 
5069440SSusan.Scheufele@Sun.COM 			/* Initialize and Setup Interrupt handler */
5079440SSusan.Scheufele@Sun.COM 			if (msi_enable && (intr_types & DDI_INTR_TYPE_MSIX)) {
5089440SSusan.Scheufele@Sun.COM 				if (mrsas_add_intrs(instance,
5099440SSusan.Scheufele@Sun.COM 				    DDI_INTR_TYPE_MSIX) != DDI_SUCCESS) {
5109440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
5119440SSusan.Scheufele@Sun.COM 					    "MSIX interrupt query failed"));
5129440SSusan.Scheufele@Sun.COM 					goto fail_attach;
5139440SSusan.Scheufele@Sun.COM 				}
5149440SSusan.Scheufele@Sun.COM 				instance->intr_type = DDI_INTR_TYPE_MSIX;
5159440SSusan.Scheufele@Sun.COM 			} else if (msi_enable && (intr_types &
5169440SSusan.Scheufele@Sun.COM 			    DDI_INTR_TYPE_MSI)) {
5179440SSusan.Scheufele@Sun.COM 				if (mrsas_add_intrs(instance,
5189440SSusan.Scheufele@Sun.COM 				    DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
5199440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
5209440SSusan.Scheufele@Sun.COM 					    "MSI interrupt query failed"));
5219440SSusan.Scheufele@Sun.COM 					goto fail_attach;
5229440SSusan.Scheufele@Sun.COM 				}
5239440SSusan.Scheufele@Sun.COM 				instance->intr_type = DDI_INTR_TYPE_MSI;
5249440SSusan.Scheufele@Sun.COM 			} else if (intr_types & DDI_INTR_TYPE_FIXED) {
5259440SSusan.Scheufele@Sun.COM 				msi_enable = 0;
5269440SSusan.Scheufele@Sun.COM 				if (mrsas_add_intrs(instance,
5279440SSusan.Scheufele@Sun.COM 				    DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
5289440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
5299440SSusan.Scheufele@Sun.COM 					    "FIXED interrupt query failed"));
5309440SSusan.Scheufele@Sun.COM 					goto fail_attach;
5319440SSusan.Scheufele@Sun.COM 				}
5329440SSusan.Scheufele@Sun.COM 				instance->intr_type = DDI_INTR_TYPE_FIXED;
5339440SSusan.Scheufele@Sun.COM 			} else {
5349440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "Device cannot "
5359440SSusan.Scheufele@Sun.COM 				    "suppport either FIXED or MSI/X "
5369440SSusan.Scheufele@Sun.COM 				    "interrupts"));
5379440SSusan.Scheufele@Sun.COM 				goto fail_attach;
5389440SSusan.Scheufele@Sun.COM 			}
5399440SSusan.Scheufele@Sun.COM 
5409440SSusan.Scheufele@Sun.COM 			added_isr_f = 1;
5419440SSusan.Scheufele@Sun.COM 
54211856SYu.Wu@Sun.COM 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
54311856SYu.Wu@Sun.COM 			    "mrsas-enable-ctio", &data) == DDI_SUCCESS) {
54411856SYu.Wu@Sun.COM 				if (strncmp(data, "no", 3) == 0) {
54511856SYu.Wu@Sun.COM 					ctio_enable = 0;
54611856SYu.Wu@Sun.COM 					con_log(CL_ANN1, (CE_WARN,
54711856SYu.Wu@Sun.COM 					    "ctio_enable = %d disabled",
54811856SYu.Wu@Sun.COM 					    ctio_enable));
54911856SYu.Wu@Sun.COM 				}
55011856SYu.Wu@Sun.COM 				ddi_prop_free(data);
55111856SYu.Wu@Sun.COM 			}
55211856SYu.Wu@Sun.COM 
55311856SYu.Wu@Sun.COM 			con_log(CL_DLEVEL1, (CE_WARN, "ctio_enable = %d",
55411856SYu.Wu@Sun.COM 			    ctio_enable));
55511856SYu.Wu@Sun.COM 
5569440SSusan.Scheufele@Sun.COM 			/* setup the mfi based low level driver */
5579440SSusan.Scheufele@Sun.COM 			if (init_mfi(instance) != DDI_SUCCESS) {
5589440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "mr_sas: "
5599440SSusan.Scheufele@Sun.COM 				"could not initialize the low level driver"));
5609440SSusan.Scheufele@Sun.COM 
5619440SSusan.Scheufele@Sun.COM 				goto fail_attach;
5629440SSusan.Scheufele@Sun.COM 			}
5639440SSusan.Scheufele@Sun.COM 
5649440SSusan.Scheufele@Sun.COM 			/* Initialize all Mutex */
5659440SSusan.Scheufele@Sun.COM 			INIT_LIST_HEAD(&instance->completed_pool_list);
5669440SSusan.Scheufele@Sun.COM 			mutex_init(&instance->completed_pool_mtx,
5679440SSusan.Scheufele@Sun.COM 			    "completed_pool_mtx", MUTEX_DRIVER,
5689440SSusan.Scheufele@Sun.COM 			    DDI_INTR_PRI(instance->intr_pri));
5699440SSusan.Scheufele@Sun.COM 
57012244SYu.Wu@Sun.COM 			mutex_init(&instance->app_cmd_pool_mtx,
57112244SYu.Wu@Sun.COM 			    "app_cmd_pool_mtx",	MUTEX_DRIVER,
57212244SYu.Wu@Sun.COM 			    DDI_INTR_PRI(instance->intr_pri));
57312244SYu.Wu@Sun.COM 
57412244SYu.Wu@Sun.COM 			mutex_init(&instance->cmd_pend_mtx, "cmd_pend_mtx",
57512244SYu.Wu@Sun.COM 			    MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
57612244SYu.Wu@Sun.COM 
57712244SYu.Wu@Sun.COM 			mutex_init(&instance->ocr_flags_mtx, "ocr_flags_mtx",
57812244SYu.Wu@Sun.COM 			    MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
57912244SYu.Wu@Sun.COM 
5809440SSusan.Scheufele@Sun.COM 			mutex_init(&instance->int_cmd_mtx, "int_cmd_mtx",
5819440SSusan.Scheufele@Sun.COM 			    MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
5829440SSusan.Scheufele@Sun.COM 			cv_init(&instance->int_cmd_cv, NULL, CV_DRIVER, NULL);
5839440SSusan.Scheufele@Sun.COM 
5849440SSusan.Scheufele@Sun.COM 			mutex_init(&instance->cmd_pool_mtx, "cmd_pool_mtx",
5859440SSusan.Scheufele@Sun.COM 			    MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
5869440SSusan.Scheufele@Sun.COM 
58712244SYu.Wu@Sun.COM 			instance->timeout_id = (timeout_id_t)-1;
58812244SYu.Wu@Sun.COM 
5899440SSusan.Scheufele@Sun.COM 			/* Register our soft-isr for highlevel interrupts. */
5909440SSusan.Scheufele@Sun.COM 			instance->isr_level = instance->intr_pri;
5919440SSusan.Scheufele@Sun.COM 			if (instance->isr_level == HIGH_LEVEL_INTR) {
5929440SSusan.Scheufele@Sun.COM 				if (ddi_add_softintr(dip, DDI_SOFTINT_HIGH,
5939440SSusan.Scheufele@Sun.COM 				    &instance->soft_intr_id, NULL, NULL,
5949440SSusan.Scheufele@Sun.COM 				    mrsas_softintr, (caddr_t)instance) !=
5959440SSusan.Scheufele@Sun.COM 				    DDI_SUCCESS) {
5969440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
5979440SSusan.Scheufele@Sun.COM 					    " Software ISR did not register"));
5989440SSusan.Scheufele@Sun.COM 
5999440SSusan.Scheufele@Sun.COM 					goto fail_attach;
6009440SSusan.Scheufele@Sun.COM 				}
6019440SSusan.Scheufele@Sun.COM 
6029440SSusan.Scheufele@Sun.COM 				added_soft_isr_f = 1;
6039440SSusan.Scheufele@Sun.COM 			}
6049440SSusan.Scheufele@Sun.COM 
6059440SSusan.Scheufele@Sun.COM 			/* Allocate a transport structure */
6069440SSusan.Scheufele@Sun.COM 			tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
6079440SSusan.Scheufele@Sun.COM 
6089440SSusan.Scheufele@Sun.COM 			if (tran == NULL) {
6099440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
6109440SSusan.Scheufele@Sun.COM 				    "scsi_hba_tran_alloc failed"));
6119440SSusan.Scheufele@Sun.COM 				goto fail_attach;
6129440SSusan.Scheufele@Sun.COM 			}
6139440SSusan.Scheufele@Sun.COM 
6149440SSusan.Scheufele@Sun.COM 			tran_alloc_f = 1;
6159440SSusan.Scheufele@Sun.COM 
6169440SSusan.Scheufele@Sun.COM 			instance->tran = tran;
6179440SSusan.Scheufele@Sun.COM 
6189440SSusan.Scheufele@Sun.COM 			tran->tran_hba_private	= instance;
6199440SSusan.Scheufele@Sun.COM 			tran->tran_tgt_init	= mrsas_tran_tgt_init;
6209440SSusan.Scheufele@Sun.COM 			tran->tran_tgt_probe	= scsi_hba_probe;
6219440SSusan.Scheufele@Sun.COM 			tran->tran_tgt_free	= mrsas_tran_tgt_free;
6229440SSusan.Scheufele@Sun.COM 			tran->tran_init_pkt	= mrsas_tran_init_pkt;
6239440SSusan.Scheufele@Sun.COM 			tran->tran_start	= mrsas_tran_start;
6249440SSusan.Scheufele@Sun.COM 			tran->tran_abort	= mrsas_tran_abort;
6259440SSusan.Scheufele@Sun.COM 			tran->tran_reset	= mrsas_tran_reset;
6269440SSusan.Scheufele@Sun.COM 			tran->tran_getcap	= mrsas_tran_getcap;
6279440SSusan.Scheufele@Sun.COM 			tran->tran_setcap	= mrsas_tran_setcap;
6289440SSusan.Scheufele@Sun.COM 			tran->tran_destroy_pkt	= mrsas_tran_destroy_pkt;
6299440SSusan.Scheufele@Sun.COM 			tran->tran_dmafree	= mrsas_tran_dmafree;
6309440SSusan.Scheufele@Sun.COM 			tran->tran_sync_pkt	= mrsas_tran_sync_pkt;
6319440SSusan.Scheufele@Sun.COM 			tran->tran_bus_config	= mrsas_tran_bus_config;
6329440SSusan.Scheufele@Sun.COM 
6339795SYu.Wu@Sun.COM 			if (mrsas_relaxed_ordering)
6349795SYu.Wu@Sun.COM 				mrsas_generic_dma_attr.dma_attr_flags |=
6359795SYu.Wu@Sun.COM 				    DDI_DMA_RELAXED_ORDERING;
6369795SYu.Wu@Sun.COM 
6379795SYu.Wu@Sun.COM 
6389440SSusan.Scheufele@Sun.COM 			tran_dma_attr = mrsas_generic_dma_attr;
6399440SSusan.Scheufele@Sun.COM 			tran_dma_attr.dma_attr_sgllen = instance->max_num_sge;
6409440SSusan.Scheufele@Sun.COM 
6419440SSusan.Scheufele@Sun.COM 			/* Attach this instance of the hba */
6429440SSusan.Scheufele@Sun.COM 			if (scsi_hba_attach_setup(dip, &tran_dma_attr, tran, 0)
6439440SSusan.Scheufele@Sun.COM 			    != DDI_SUCCESS) {
6449440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
6459440SSusan.Scheufele@Sun.COM 				    "scsi_hba_attach failed"));
6469440SSusan.Scheufele@Sun.COM 
6479440SSusan.Scheufele@Sun.COM 				goto fail_attach;
6489440SSusan.Scheufele@Sun.COM 			}
6499440SSusan.Scheufele@Sun.COM 
6509440SSusan.Scheufele@Sun.COM 			/* create devctl node for cfgadm command */
6519440SSusan.Scheufele@Sun.COM 			if (ddi_create_minor_node(dip, "devctl",
6529440SSusan.Scheufele@Sun.COM 			    S_IFCHR, INST2DEVCTL(instance_no),
6539440SSusan.Scheufele@Sun.COM 			    DDI_NT_SCSI_NEXUS, 0) == DDI_FAILURE) {
6549440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
6559440SSusan.Scheufele@Sun.COM 				    "mr_sas: failed to create devctl node."));
6569440SSusan.Scheufele@Sun.COM 
6579440SSusan.Scheufele@Sun.COM 				goto fail_attach;
6589440SSusan.Scheufele@Sun.COM 			}
6599440SSusan.Scheufele@Sun.COM 
6609440SSusan.Scheufele@Sun.COM 			create_devctl_node_f = 1;
6619440SSusan.Scheufele@Sun.COM 
6629440SSusan.Scheufele@Sun.COM 			/* create scsi node for cfgadm command */
6639440SSusan.Scheufele@Sun.COM 			if (ddi_create_minor_node(dip, "scsi", S_IFCHR,
6649440SSusan.Scheufele@Sun.COM 			    INST2SCSI(instance_no),
6659440SSusan.Scheufele@Sun.COM 			    DDI_NT_SCSI_ATTACHMENT_POINT, 0) ==
6669440SSusan.Scheufele@Sun.COM 			    DDI_FAILURE) {
6679440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
6689440SSusan.Scheufele@Sun.COM 				    "mr_sas: failed to create scsi node."));
6699440SSusan.Scheufele@Sun.COM 
6709440SSusan.Scheufele@Sun.COM 				goto fail_attach;
6719440SSusan.Scheufele@Sun.COM 			}
6729440SSusan.Scheufele@Sun.COM 
6739440SSusan.Scheufele@Sun.COM 			create_scsi_node_f = 1;
6749440SSusan.Scheufele@Sun.COM 
6759440SSusan.Scheufele@Sun.COM 			(void) sprintf(instance->iocnode, "%d:lsirdctl",
6769440SSusan.Scheufele@Sun.COM 			    instance_no);
6779440SSusan.Scheufele@Sun.COM 
6789440SSusan.Scheufele@Sun.COM 			/*
6799440SSusan.Scheufele@Sun.COM 			 * Create a node for applications
6809440SSusan.Scheufele@Sun.COM 			 * for issuing ioctl to the driver.
6819440SSusan.Scheufele@Sun.COM 			 */
6829440SSusan.Scheufele@Sun.COM 			if (ddi_create_minor_node(dip, instance->iocnode,
6839440SSusan.Scheufele@Sun.COM 			    S_IFCHR, INST2LSIRDCTL(instance_no),
6849440SSusan.Scheufele@Sun.COM 			    DDI_PSEUDO, 0) == DDI_FAILURE) {
6859440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
6869440SSusan.Scheufele@Sun.COM 				    "mr_sas: failed to create ioctl node."));
6879440SSusan.Scheufele@Sun.COM 
6889440SSusan.Scheufele@Sun.COM 				goto fail_attach;
6899440SSusan.Scheufele@Sun.COM 			}
6909440SSusan.Scheufele@Sun.COM 
6919440SSusan.Scheufele@Sun.COM 			create_ioc_node_f = 1;
6929440SSusan.Scheufele@Sun.COM 
6939440SSusan.Scheufele@Sun.COM 			/* Create a taskq to handle dr events */
6949440SSusan.Scheufele@Sun.COM 			if ((instance->taskq = ddi_taskq_create(dip,
6959440SSusan.Scheufele@Sun.COM 			    "mrsas_dr_taskq", 1,
6969440SSusan.Scheufele@Sun.COM 			    TASKQ_DEFAULTPRI, 0)) == NULL) {
6979440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
6989440SSusan.Scheufele@Sun.COM 				    "mr_sas: failed to create taskq "));
6999440SSusan.Scheufele@Sun.COM 				instance->taskq = NULL;
7009440SSusan.Scheufele@Sun.COM 				goto fail_attach;
7019440SSusan.Scheufele@Sun.COM 			}
7029440SSusan.Scheufele@Sun.COM 
7039440SSusan.Scheufele@Sun.COM 			/* enable interrupt */
7049440SSusan.Scheufele@Sun.COM 			instance->func_ptr->enable_intr(instance);
7059440SSusan.Scheufele@Sun.COM 
7069440SSusan.Scheufele@Sun.COM 			/* initiate AEN */
7079440SSusan.Scheufele@Sun.COM 			if (start_mfi_aen(instance)) {
7089440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
7099440SSusan.Scheufele@Sun.COM 				    "mr_sas: failed to initiate AEN."));
7109440SSusan.Scheufele@Sun.COM 				goto fail_initiate_aen;
7119440SSusan.Scheufele@Sun.COM 			}
7129440SSusan.Scheufele@Sun.COM 
7139440SSusan.Scheufele@Sun.COM 			con_log(CL_DLEVEL1, (CE_NOTE,
7149440SSusan.Scheufele@Sun.COM 			    "AEN started for instance %d.", instance_no));
7159440SSusan.Scheufele@Sun.COM 
7169440SSusan.Scheufele@Sun.COM 			/* Finally! We are on the air.  */
7179440SSusan.Scheufele@Sun.COM 			ddi_report_dev(dip);
7189440SSusan.Scheufele@Sun.COM 
7199440SSusan.Scheufele@Sun.COM 			if (mrsas_check_acc_handle(instance->regmap_handle) !=
7209440SSusan.Scheufele@Sun.COM 			    DDI_SUCCESS) {
7219440SSusan.Scheufele@Sun.COM 				goto fail_attach;
7229440SSusan.Scheufele@Sun.COM 			}
7239440SSusan.Scheufele@Sun.COM 			if (mrsas_check_acc_handle(instance->pci_handle) !=
7249440SSusan.Scheufele@Sun.COM 			    DDI_SUCCESS) {
7259440SSusan.Scheufele@Sun.COM 				goto fail_attach;
7269440SSusan.Scheufele@Sun.COM 			}
7279440SSusan.Scheufele@Sun.COM 			instance->mr_ld_list =
7289440SSusan.Scheufele@Sun.COM 			    kmem_zalloc(MRDRV_MAX_LD * sizeof (struct mrsas_ld),
7299440SSusan.Scheufele@Sun.COM 			    KM_SLEEP);
7309440SSusan.Scheufele@Sun.COM 			break;
7319440SSusan.Scheufele@Sun.COM 		case DDI_PM_RESUME:
7329440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_NOTE,
7339440SSusan.Scheufele@Sun.COM 			    "mr_sas: DDI_PM_RESUME"));
7349440SSusan.Scheufele@Sun.COM 			break;
7359440SSusan.Scheufele@Sun.COM 		case DDI_RESUME:
7369440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_NOTE,
7379440SSusan.Scheufele@Sun.COM 			    "mr_sas: DDI_RESUME"));
7389440SSusan.Scheufele@Sun.COM 			break;
7399440SSusan.Scheufele@Sun.COM 		default:
7409440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN,
7419440SSusan.Scheufele@Sun.COM 			    "mr_sas: invalid attach cmd=%x", cmd));
7429440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
7439440SSusan.Scheufele@Sun.COM 	}
7449440SSusan.Scheufele@Sun.COM 
7459440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
7469440SSusan.Scheufele@Sun.COM 
7479440SSusan.Scheufele@Sun.COM fail_initiate_aen:
7489440SSusan.Scheufele@Sun.COM fail_attach:
7499440SSusan.Scheufele@Sun.COM 	if (create_devctl_node_f) {
7509440SSusan.Scheufele@Sun.COM 		ddi_remove_minor_node(dip, "devctl");
7519440SSusan.Scheufele@Sun.COM 	}
7529440SSusan.Scheufele@Sun.COM 
7539440SSusan.Scheufele@Sun.COM 	if (create_scsi_node_f) {
7549440SSusan.Scheufele@Sun.COM 		ddi_remove_minor_node(dip, "scsi");
7559440SSusan.Scheufele@Sun.COM 	}
7569440SSusan.Scheufele@Sun.COM 
7579440SSusan.Scheufele@Sun.COM 	if (create_ioc_node_f) {
7589440SSusan.Scheufele@Sun.COM 		ddi_remove_minor_node(dip, instance->iocnode);
7599440SSusan.Scheufele@Sun.COM 	}
7609440SSusan.Scheufele@Sun.COM 
7619440SSusan.Scheufele@Sun.COM 	if (tran_alloc_f) {
7629440SSusan.Scheufele@Sun.COM 		scsi_hba_tran_free(tran);
7639440SSusan.Scheufele@Sun.COM 	}
7649440SSusan.Scheufele@Sun.COM 
7659440SSusan.Scheufele@Sun.COM 
7669440SSusan.Scheufele@Sun.COM 	if (added_soft_isr_f) {
7679440SSusan.Scheufele@Sun.COM 		ddi_remove_softintr(instance->soft_intr_id);
7689440SSusan.Scheufele@Sun.COM 	}
7699440SSusan.Scheufele@Sun.COM 
7709440SSusan.Scheufele@Sun.COM 	if (added_isr_f) {
7719440SSusan.Scheufele@Sun.COM 		mrsas_rem_intrs(instance);
7729440SSusan.Scheufele@Sun.COM 	}
7739440SSusan.Scheufele@Sun.COM 
7749440SSusan.Scheufele@Sun.COM 	if (instance && instance->taskq) {
7759440SSusan.Scheufele@Sun.COM 		ddi_taskq_destroy(instance->taskq);
7769440SSusan.Scheufele@Sun.COM 	}
7779440SSusan.Scheufele@Sun.COM 
7789440SSusan.Scheufele@Sun.COM 	mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
7799440SSusan.Scheufele@Sun.COM 	ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
7809440SSusan.Scheufele@Sun.COM 
7819440SSusan.Scheufele@Sun.COM 	mrsas_fm_fini(instance);
7829440SSusan.Scheufele@Sun.COM 
7839440SSusan.Scheufele@Sun.COM 	pci_config_teardown(&instance->pci_handle);
7849440SSusan.Scheufele@Sun.COM 
7859440SSusan.Scheufele@Sun.COM 	ddi_soft_state_free(mrsas_state, instance_no);
7869440SSusan.Scheufele@Sun.COM 
7879440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN, (CE_NOTE,
7889440SSusan.Scheufele@Sun.COM 	    "mr_sas: return failure from mrsas_attach"));
7899440SSusan.Scheufele@Sun.COM 
7909440SSusan.Scheufele@Sun.COM 	return (DDI_FAILURE);
7919440SSusan.Scheufele@Sun.COM }
7929440SSusan.Scheufele@Sun.COM 
7939440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
7949440SSusan.Scheufele@Sun.COM static int
mrsas_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** resultp)7959440SSusan.Scheufele@Sun.COM mrsas_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
7969440SSusan.Scheufele@Sun.COM {
7979440SSusan.Scheufele@Sun.COM 	int	rval;
7989440SSusan.Scheufele@Sun.COM 	int	mrsas_minor = getminor((dev_t)arg);
7999440SSusan.Scheufele@Sun.COM 
8009440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance;
8019440SSusan.Scheufele@Sun.COM 
8029440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
8039440SSusan.Scheufele@Sun.COM 
8049440SSusan.Scheufele@Sun.COM 	switch (cmd) {
8059440SSusan.Scheufele@Sun.COM 		case DDI_INFO_DEVT2DEVINFO:
8069440SSusan.Scheufele@Sun.COM 			instance = (struct mrsas_instance *)
8079440SSusan.Scheufele@Sun.COM 			    ddi_get_soft_state(mrsas_state,
8089440SSusan.Scheufele@Sun.COM 			    MINOR2INST(mrsas_minor));
8099440SSusan.Scheufele@Sun.COM 
8109440SSusan.Scheufele@Sun.COM 			if (instance == NULL) {
8119440SSusan.Scheufele@Sun.COM 				*resultp = NULL;
8129440SSusan.Scheufele@Sun.COM 				rval = DDI_FAILURE;
8139440SSusan.Scheufele@Sun.COM 			} else {
8149440SSusan.Scheufele@Sun.COM 				*resultp = instance->dip;
8159440SSusan.Scheufele@Sun.COM 				rval = DDI_SUCCESS;
8169440SSusan.Scheufele@Sun.COM 			}
8179440SSusan.Scheufele@Sun.COM 			break;
8189440SSusan.Scheufele@Sun.COM 		case DDI_INFO_DEVT2INSTANCE:
81910600SYu.Wu@Sun.COM 			*resultp = (void *)(intptr_t)
82010600SYu.Wu@Sun.COM 			    (MINOR2INST(getminor((dev_t)arg)));
8219440SSusan.Scheufele@Sun.COM 			rval = DDI_SUCCESS;
8229440SSusan.Scheufele@Sun.COM 			break;
8239440SSusan.Scheufele@Sun.COM 		default:
8249440SSusan.Scheufele@Sun.COM 			*resultp = NULL;
8259440SSusan.Scheufele@Sun.COM 			rval = DDI_FAILURE;
8269440SSusan.Scheufele@Sun.COM 	}
8279440SSusan.Scheufele@Sun.COM 
8289440SSusan.Scheufele@Sun.COM 	return (rval);
8299440SSusan.Scheufele@Sun.COM }
8309440SSusan.Scheufele@Sun.COM 
8319440SSusan.Scheufele@Sun.COM static int
mrsas_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)8329440SSusan.Scheufele@Sun.COM mrsas_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8339440SSusan.Scheufele@Sun.COM {
8349440SSusan.Scheufele@Sun.COM 	int	instance_no;
8359440SSusan.Scheufele@Sun.COM 
8369440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance;
8379440SSusan.Scheufele@Sun.COM 
8389440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
8399440SSusan.Scheufele@Sun.COM 
8409440SSusan.Scheufele@Sun.COM 	/* CONSTCOND */
8419440SSusan.Scheufele@Sun.COM 	ASSERT(NO_COMPETING_THREADS);
8429440SSusan.Scheufele@Sun.COM 
8439440SSusan.Scheufele@Sun.COM 	instance_no = ddi_get_instance(dip);
8449440SSusan.Scheufele@Sun.COM 
8459440SSusan.Scheufele@Sun.COM 	instance = (struct mrsas_instance *)ddi_get_soft_state(mrsas_state,
8469440SSusan.Scheufele@Sun.COM 	    instance_no);
8479440SSusan.Scheufele@Sun.COM 
8489440SSusan.Scheufele@Sun.COM 	if (!instance) {
8499440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
8509440SSusan.Scheufele@Sun.COM 		    "mr_sas:%d could not get instance in detach",
8519440SSusan.Scheufele@Sun.COM 		    instance_no));
8529440SSusan.Scheufele@Sun.COM 
8539440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
8549440SSusan.Scheufele@Sun.COM 	}
8559440SSusan.Scheufele@Sun.COM 
8569440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN, (CE_NOTE,
8579440SSusan.Scheufele@Sun.COM 	    "mr_sas%d: detaching device 0x%4x:0x%4x:0x%4x:0x%4x",
8589440SSusan.Scheufele@Sun.COM 	    instance_no, instance->vendor_id, instance->device_id,
8599440SSusan.Scheufele@Sun.COM 	    instance->subsysvid, instance->subsysid));
8609440SSusan.Scheufele@Sun.COM 
8619440SSusan.Scheufele@Sun.COM 	switch (cmd) {
8629440SSusan.Scheufele@Sun.COM 	case DDI_DETACH:
8639440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE,
8649440SSusan.Scheufele@Sun.COM 		    "mrsas_detach: DDI_DETACH"));
8659440SSusan.Scheufele@Sun.COM 
8669440SSusan.Scheufele@Sun.COM 		if (scsi_hba_detach(dip) != DDI_SUCCESS) {
8679440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN,
8689440SSusan.Scheufele@Sun.COM 			    "mr_sas:%d failed to detach",
8699440SSusan.Scheufele@Sun.COM 			    instance_no));
8709440SSusan.Scheufele@Sun.COM 
8719440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
8729440SSusan.Scheufele@Sun.COM 		}
8739440SSusan.Scheufele@Sun.COM 
8749440SSusan.Scheufele@Sun.COM 		scsi_hba_tran_free(instance->tran);
8759440SSusan.Scheufele@Sun.COM 
8769440SSusan.Scheufele@Sun.COM 		flush_cache(instance);
8779440SSusan.Scheufele@Sun.COM 
8789440SSusan.Scheufele@Sun.COM 		if (abort_aen_cmd(instance, instance->aen_cmd)) {
8799440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "mrsas_detach: "
8809440SSusan.Scheufele@Sun.COM 			    "failed to abort prevous AEN command"));
8819440SSusan.Scheufele@Sun.COM 
8829440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
8839440SSusan.Scheufele@Sun.COM 		}
8849440SSusan.Scheufele@Sun.COM 
8859440SSusan.Scheufele@Sun.COM 		instance->func_ptr->disable_intr(instance);
8869440SSusan.Scheufele@Sun.COM 
8879440SSusan.Scheufele@Sun.COM 		if (instance->isr_level == HIGH_LEVEL_INTR) {
8889440SSusan.Scheufele@Sun.COM 			ddi_remove_softintr(instance->soft_intr_id);
8899440SSusan.Scheufele@Sun.COM 		}
8909440SSusan.Scheufele@Sun.COM 
8919440SSusan.Scheufele@Sun.COM 		mrsas_rem_intrs(instance);
8929440SSusan.Scheufele@Sun.COM 
8939440SSusan.Scheufele@Sun.COM 		if (instance->taskq) {
8949440SSusan.Scheufele@Sun.COM 			ddi_taskq_destroy(instance->taskq);
8959440SSusan.Scheufele@Sun.COM 		}
8969440SSusan.Scheufele@Sun.COM 		kmem_free(instance->mr_ld_list, MRDRV_MAX_LD
8979440SSusan.Scheufele@Sun.COM 		    * sizeof (struct mrsas_ld));
8989440SSusan.Scheufele@Sun.COM 		free_space_for_mfi(instance);
8999440SSusan.Scheufele@Sun.COM 
9009440SSusan.Scheufele@Sun.COM 		mrsas_fm_fini(instance);
9019440SSusan.Scheufele@Sun.COM 
9029440SSusan.Scheufele@Sun.COM 		pci_config_teardown(&instance->pci_handle);
9039440SSusan.Scheufele@Sun.COM 
9049440SSusan.Scheufele@Sun.COM 		kmem_free(instance->func_ptr,
9059440SSusan.Scheufele@Sun.COM 		    sizeof (struct mrsas_func_ptr));
9069440SSusan.Scheufele@Sun.COM 
90712244SYu.Wu@Sun.COM 		if (instance->timeout_id != (timeout_id_t)-1) {
90812244SYu.Wu@Sun.COM 			(void) untimeout(instance->timeout_id);
90912244SYu.Wu@Sun.COM 			instance->timeout_id = (timeout_id_t)-1;
91012244SYu.Wu@Sun.COM 		}
9119440SSusan.Scheufele@Sun.COM 		ddi_soft_state_free(mrsas_state, instance_no);
9129440SSusan.Scheufele@Sun.COM 		break;
9139440SSusan.Scheufele@Sun.COM 	case DDI_PM_SUSPEND:
9149440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE,
9159440SSusan.Scheufele@Sun.COM 		    "mrsas_detach: DDI_PM_SUSPEND"));
9169440SSusan.Scheufele@Sun.COM 
9179440SSusan.Scheufele@Sun.COM 		break;
9189440SSusan.Scheufele@Sun.COM 	case DDI_SUSPEND:
9199440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE,
9209440SSusan.Scheufele@Sun.COM 		    "mrsas_detach: DDI_SUSPEND"));
9219440SSusan.Scheufele@Sun.COM 
9229440SSusan.Scheufele@Sun.COM 		break;
9239440SSusan.Scheufele@Sun.COM 	default:
9249440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
9259440SSusan.Scheufele@Sun.COM 		    "invalid detach command:0x%x", cmd));
9269440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
9279440SSusan.Scheufele@Sun.COM 	}
9289440SSusan.Scheufele@Sun.COM 
9299440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
9309440SSusan.Scheufele@Sun.COM }
9319440SSusan.Scheufele@Sun.COM 
9329440SSusan.Scheufele@Sun.COM /*
9339440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
9349440SSusan.Scheufele@Sun.COM  *                                                                            *
9359440SSusan.Scheufele@Sun.COM  *             common entry points - for character driver types               *
9369440SSusan.Scheufele@Sun.COM  *                                                                            *
9379440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
9389440SSusan.Scheufele@Sun.COM  */
9399440SSusan.Scheufele@Sun.COM static  int
mrsas_open(dev_t * dev,int openflags,int otyp,cred_t * credp)9409440SSusan.Scheufele@Sun.COM mrsas_open(dev_t *dev, int openflags, int otyp, cred_t *credp)
9419440SSusan.Scheufele@Sun.COM {
9429440SSusan.Scheufele@Sun.COM 	int	rval = 0;
9439440SSusan.Scheufele@Sun.COM 
9449440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
9459440SSusan.Scheufele@Sun.COM 
9469440SSusan.Scheufele@Sun.COM 	/* Check root permissions */
9479440SSusan.Scheufele@Sun.COM 	if (drv_priv(credp) != 0) {
9489440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
9499440SSusan.Scheufele@Sun.COM 		    "mr_sas: Non-root ioctl access denied!"));
9509440SSusan.Scheufele@Sun.COM 		return (EPERM);
9519440SSusan.Scheufele@Sun.COM 	}
9529440SSusan.Scheufele@Sun.COM 
9539440SSusan.Scheufele@Sun.COM 	/* Verify we are being opened as a character device */
9549440SSusan.Scheufele@Sun.COM 	if (otyp != OTYP_CHR) {
9559440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
9569440SSusan.Scheufele@Sun.COM 		    "mr_sas: ioctl node must be a char node"));
9579440SSusan.Scheufele@Sun.COM 		return (EINVAL);
9589440SSusan.Scheufele@Sun.COM 	}
9599440SSusan.Scheufele@Sun.COM 
9609440SSusan.Scheufele@Sun.COM 	if (ddi_get_soft_state(mrsas_state, MINOR2INST(getminor(*dev)))
9619440SSusan.Scheufele@Sun.COM 	    == NULL) {
9629440SSusan.Scheufele@Sun.COM 		return (ENXIO);
9639440SSusan.Scheufele@Sun.COM 	}
9649440SSusan.Scheufele@Sun.COM 
9659440SSusan.Scheufele@Sun.COM 	if (scsi_hba_open) {
9669440SSusan.Scheufele@Sun.COM 		rval = scsi_hba_open(dev, openflags, otyp, credp);
9679440SSusan.Scheufele@Sun.COM 	}
9689440SSusan.Scheufele@Sun.COM 
9699440SSusan.Scheufele@Sun.COM 	return (rval);
9709440SSusan.Scheufele@Sun.COM }
9719440SSusan.Scheufele@Sun.COM 
9729440SSusan.Scheufele@Sun.COM static  int
mrsas_close(dev_t dev,int openflags,int otyp,cred_t * credp)9739440SSusan.Scheufele@Sun.COM mrsas_close(dev_t dev, int openflags, int otyp, cred_t *credp)
9749440SSusan.Scheufele@Sun.COM {
9759440SSusan.Scheufele@Sun.COM 	int	rval = 0;
9769440SSusan.Scheufele@Sun.COM 
9779440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
9789440SSusan.Scheufele@Sun.COM 
9799440SSusan.Scheufele@Sun.COM 	/* no need for locks! */
9809440SSusan.Scheufele@Sun.COM 
9819440SSusan.Scheufele@Sun.COM 	if (scsi_hba_close) {
9829440SSusan.Scheufele@Sun.COM 		rval = scsi_hba_close(dev, openflags, otyp, credp);
9839440SSusan.Scheufele@Sun.COM 	}
9849440SSusan.Scheufele@Sun.COM 
9859440SSusan.Scheufele@Sun.COM 	return (rval);
9869440SSusan.Scheufele@Sun.COM }
9879440SSusan.Scheufele@Sun.COM 
9889440SSusan.Scheufele@Sun.COM static int
mrsas_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)9899440SSusan.Scheufele@Sun.COM mrsas_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
9909440SSusan.Scheufele@Sun.COM     int *rvalp)
9919440SSusan.Scheufele@Sun.COM {
9929440SSusan.Scheufele@Sun.COM 	int	rval = 0;
9939440SSusan.Scheufele@Sun.COM 
9949440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance;
9959440SSusan.Scheufele@Sun.COM 	struct mrsas_ioctl	*ioctl;
9969440SSusan.Scheufele@Sun.COM 	struct mrsas_aen	aen;
9979440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
9989440SSusan.Scheufele@Sun.COM 
9999440SSusan.Scheufele@Sun.COM 	instance = ddi_get_soft_state(mrsas_state, MINOR2INST(getminor(dev)));
10009440SSusan.Scheufele@Sun.COM 
10019440SSusan.Scheufele@Sun.COM 	if (instance == NULL) {
10029440SSusan.Scheufele@Sun.COM 		/* invalid minor number */
10039440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas: adapter not found."));
10049440SSusan.Scheufele@Sun.COM 		return (ENXIO);
10059440SSusan.Scheufele@Sun.COM 	}
10069440SSusan.Scheufele@Sun.COM 
10079440SSusan.Scheufele@Sun.COM 	ioctl = (struct mrsas_ioctl *)kmem_zalloc(sizeof (struct mrsas_ioctl),
10089440SSusan.Scheufele@Sun.COM 	    KM_SLEEP);
10099440SSusan.Scheufele@Sun.COM 	ASSERT(ioctl);
10109440SSusan.Scheufele@Sun.COM 
10119440SSusan.Scheufele@Sun.COM 	switch ((uint_t)cmd) {
10129440SSusan.Scheufele@Sun.COM 		case MRSAS_IOCTL_FIRMWARE:
10139795SYu.Wu@Sun.COM 			if (ddi_copyin((void *)arg, ioctl,
10149795SYu.Wu@Sun.COM 			    sizeof (struct mrsas_ioctl), mode)) {
10159795SYu.Wu@Sun.COM 				con_log(CL_ANN, (CE_WARN, "mrsas_ioctl: "
10169795SYu.Wu@Sun.COM 				    "ERROR IOCTL copyin"));
10179795SYu.Wu@Sun.COM 				kmem_free(ioctl, sizeof (struct mrsas_ioctl));
10189795SYu.Wu@Sun.COM 				return (EFAULT);
10199440SSusan.Scheufele@Sun.COM 			}
10209795SYu.Wu@Sun.COM 
10219440SSusan.Scheufele@Sun.COM 			if (ioctl->control_code == MRSAS_DRIVER_IOCTL_COMMON) {
10229440SSusan.Scheufele@Sun.COM 				rval = handle_drv_ioctl(instance, ioctl, mode);
10239440SSusan.Scheufele@Sun.COM 			} else {
10249440SSusan.Scheufele@Sun.COM 				rval = handle_mfi_ioctl(instance, ioctl, mode);
10259440SSusan.Scheufele@Sun.COM 			}
10269795SYu.Wu@Sun.COM 
10279795SYu.Wu@Sun.COM 			if (ddi_copyout((void *)ioctl, (void *)arg,
10289795SYu.Wu@Sun.COM 			    (sizeof (struct mrsas_ioctl) - 1), mode)) {
10299795SYu.Wu@Sun.COM 				con_log(CL_ANN, (CE_WARN,
10309795SYu.Wu@Sun.COM 				    "mrsas_ioctl: copy_to_user failed"));
10319795SYu.Wu@Sun.COM 				rval = 1;
10329440SSusan.Scheufele@Sun.COM 			}
10339440SSusan.Scheufele@Sun.COM 
10349440SSusan.Scheufele@Sun.COM 			break;
10359440SSusan.Scheufele@Sun.COM 		case MRSAS_IOCTL_AEN:
10369795SYu.Wu@Sun.COM 			if (ddi_copyin((void *) arg, &aen,
10379795SYu.Wu@Sun.COM 			    sizeof (struct mrsas_aen), mode)) {
10389795SYu.Wu@Sun.COM 				con_log(CL_ANN, (CE_WARN,
10399795SYu.Wu@Sun.COM 				    "mrsas_ioctl: ERROR AEN copyin"));
10409795SYu.Wu@Sun.COM 				kmem_free(ioctl, sizeof (struct mrsas_ioctl));
10419795SYu.Wu@Sun.COM 				return (EFAULT);
10429440SSusan.Scheufele@Sun.COM 			}
10439440SSusan.Scheufele@Sun.COM 
10449440SSusan.Scheufele@Sun.COM 			rval = handle_mfi_aen(instance, &aen);
10459795SYu.Wu@Sun.COM 
10469795SYu.Wu@Sun.COM 			if (ddi_copyout((void *) &aen, (void *)arg,
10479795SYu.Wu@Sun.COM 			    sizeof (struct mrsas_aen), mode)) {
10489795SYu.Wu@Sun.COM 				con_log(CL_ANN, (CE_WARN,
10499795SYu.Wu@Sun.COM 				    "mrsas_ioctl: copy_to_user failed"));
10509795SYu.Wu@Sun.COM 				rval = 1;
10519440SSusan.Scheufele@Sun.COM 			}
10529440SSusan.Scheufele@Sun.COM 
10539440SSusan.Scheufele@Sun.COM 			break;
10549440SSusan.Scheufele@Sun.COM 		default:
10559440SSusan.Scheufele@Sun.COM 			rval = scsi_hba_ioctl(dev, cmd, arg,
10569440SSusan.Scheufele@Sun.COM 			    mode, credp, rvalp);
10579440SSusan.Scheufele@Sun.COM 
10589440SSusan.Scheufele@Sun.COM 			con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_ioctl: "
10599440SSusan.Scheufele@Sun.COM 			    "scsi_hba_ioctl called, ret = %x.", rval));
10609440SSusan.Scheufele@Sun.COM 	}
10619440SSusan.Scheufele@Sun.COM 
10629440SSusan.Scheufele@Sun.COM 	kmem_free(ioctl, sizeof (struct mrsas_ioctl));
10639440SSusan.Scheufele@Sun.COM 	return (rval);
10649440SSusan.Scheufele@Sun.COM }
10659440SSusan.Scheufele@Sun.COM 
10669440SSusan.Scheufele@Sun.COM /*
10679440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
10689440SSusan.Scheufele@Sun.COM  *                                                                            *
10699440SSusan.Scheufele@Sun.COM  *               common entry points - for block driver types                 *
10709440SSusan.Scheufele@Sun.COM  *                                                                            *
10719440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
10729440SSusan.Scheufele@Sun.COM  */
107312356SYu.Wu@Sun.COM #ifdef	__sparc
10749440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
10759440SSusan.Scheufele@Sun.COM static int
mrsas_reset(dev_info_t * dip,ddi_reset_cmd_t cmd)10769440SSusan.Scheufele@Sun.COM mrsas_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
10779440SSusan.Scheufele@Sun.COM {
10789440SSusan.Scheufele@Sun.COM 	int	instance_no;
10799440SSusan.Scheufele@Sun.COM 
10809440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance;
10819440SSusan.Scheufele@Sun.COM 
10829440SSusan.Scheufele@Sun.COM 	instance_no = ddi_get_instance(dip);
10839440SSusan.Scheufele@Sun.COM 	instance = (struct mrsas_instance *)ddi_get_soft_state
10849440SSusan.Scheufele@Sun.COM 	    (mrsas_state, instance_no);
10859440SSusan.Scheufele@Sun.COM 
10869440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
10879440SSusan.Scheufele@Sun.COM 
10889440SSusan.Scheufele@Sun.COM 	if (!instance) {
10899440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas:%d could not get adapter "
10909440SSusan.Scheufele@Sun.COM 		    "in reset", instance_no));
10919440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
10929440SSusan.Scheufele@Sun.COM 	}
10939440SSusan.Scheufele@Sun.COM 
10949440SSusan.Scheufele@Sun.COM 	instance->func_ptr->disable_intr(instance);
10959440SSusan.Scheufele@Sun.COM 
10969440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "flushing cache for instance %d",
10979440SSusan.Scheufele@Sun.COM 	    instance_no));
10989440SSusan.Scheufele@Sun.COM 
10999440SSusan.Scheufele@Sun.COM 	flush_cache(instance);
11009440SSusan.Scheufele@Sun.COM 
11019440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
11029440SSusan.Scheufele@Sun.COM }
110312356SYu.Wu@Sun.COM #else /* __sparc */
110412356SYu.Wu@Sun.COM /*ARGSUSED*/
110512356SYu.Wu@Sun.COM static int
mrsas_quiesce(dev_info_t * dip)110612356SYu.Wu@Sun.COM mrsas_quiesce(dev_info_t *dip)
110712356SYu.Wu@Sun.COM {
110812356SYu.Wu@Sun.COM 	int	instance_no;
110912356SYu.Wu@Sun.COM 
111012356SYu.Wu@Sun.COM 	struct mrsas_instance	*instance;
111112356SYu.Wu@Sun.COM 
111212356SYu.Wu@Sun.COM 	instance_no = ddi_get_instance(dip);
111312356SYu.Wu@Sun.COM 	instance = (struct mrsas_instance *)ddi_get_soft_state
111412356SYu.Wu@Sun.COM 	    (mrsas_state, instance_no);
111512356SYu.Wu@Sun.COM 
111612356SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
111712356SYu.Wu@Sun.COM 
111812356SYu.Wu@Sun.COM 	if (!instance) {
111912356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "mr_sas:%d could not get adapter "
112012356SYu.Wu@Sun.COM 		    "in quiesce", instance_no));
112112356SYu.Wu@Sun.COM 		return (DDI_FAILURE);
112212356SYu.Wu@Sun.COM 	}
112312356SYu.Wu@Sun.COM 	if (instance->deadadapter || instance->adapterresetinprogress) {
112412356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "mr_sas:%d adapter is not in "
112512356SYu.Wu@Sun.COM 		    "healthy state", instance_no));
112612356SYu.Wu@Sun.COM 		return (DDI_FAILURE);
112712356SYu.Wu@Sun.COM 	}
112812356SYu.Wu@Sun.COM 
112912356SYu.Wu@Sun.COM 	if (abort_aen_cmd(instance, instance->aen_cmd)) {
113012356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "mrsas_quiesce: "
113112356SYu.Wu@Sun.COM 		    "failed to abort prevous AEN command QUIESCE"));
113212356SYu.Wu@Sun.COM 	}
113312356SYu.Wu@Sun.COM 
113412356SYu.Wu@Sun.COM 	instance->func_ptr->disable_intr(instance);
113512356SYu.Wu@Sun.COM 
113612356SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "flushing cache for instance %d",
113712356SYu.Wu@Sun.COM 	    instance_no));
113812356SYu.Wu@Sun.COM 
113912356SYu.Wu@Sun.COM 	flush_cache(instance);
114012356SYu.Wu@Sun.COM 
114112356SYu.Wu@Sun.COM 	if (wait_for_outstanding(instance)) {
114212356SYu.Wu@Sun.COM 		return (DDI_FAILURE);
114312356SYu.Wu@Sun.COM 	}
114412356SYu.Wu@Sun.COM 	return (DDI_SUCCESS);
114512356SYu.Wu@Sun.COM }
114612356SYu.Wu@Sun.COM #endif	/* __sparc */
11479440SSusan.Scheufele@Sun.COM 
11489440SSusan.Scheufele@Sun.COM /*
11499440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
11509440SSusan.Scheufele@Sun.COM  *                                                                            *
11519440SSusan.Scheufele@Sun.COM  *                          entry points (SCSI HBA)                           *
11529440SSusan.Scheufele@Sun.COM  *                                                                            *
11539440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
11549440SSusan.Scheufele@Sun.COM  */
11559440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
11569440SSusan.Scheufele@Sun.COM static int
mrsas_tran_tgt_init(dev_info_t * hba_dip,dev_info_t * tgt_dip,scsi_hba_tran_t * tran,struct scsi_device * sd)11579440SSusan.Scheufele@Sun.COM mrsas_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
11589440SSusan.Scheufele@Sun.COM 		scsi_hba_tran_t *tran, struct scsi_device *sd)
11599440SSusan.Scheufele@Sun.COM {
11609440SSusan.Scheufele@Sun.COM 	struct mrsas_instance *instance;
11619440SSusan.Scheufele@Sun.COM 	uint16_t tgt = sd->sd_address.a_target;
11629440SSusan.Scheufele@Sun.COM 	uint8_t lun = sd->sd_address.a_lun;
11639440SSusan.Scheufele@Sun.COM 
11649440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_tgt_init target %d lun %d",
11659440SSusan.Scheufele@Sun.COM 	    tgt, lun));
11669440SSusan.Scheufele@Sun.COM 
11679440SSusan.Scheufele@Sun.COM 	instance = ADDR2MR(&sd->sd_address);
11689440SSusan.Scheufele@Sun.COM 
11699440SSusan.Scheufele@Sun.COM 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
11709440SSusan.Scheufele@Sun.COM 		(void) ndi_merge_node(tgt_dip, mrsas_name_node);
11719440SSusan.Scheufele@Sun.COM 		ddi_set_name_addr(tgt_dip, NULL);
11729440SSusan.Scheufele@Sun.COM 
11739440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "mrsas_tgt_init in "
11749440SSusan.Scheufele@Sun.COM 		    "ndi_dev_is_persistent_node DDI_FAILURE t = %d l = %d",
11759440SSusan.Scheufele@Sun.COM 		    tgt, lun));
11769440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
11779440SSusan.Scheufele@Sun.COM 	}
11789440SSusan.Scheufele@Sun.COM 
11799440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_tgt_init dev_dip %p tgt_dip %p",
11809440SSusan.Scheufele@Sun.COM 	    (void *)instance->mr_ld_list[tgt].dip, (void *)tgt_dip));
11819440SSusan.Scheufele@Sun.COM 
11829440SSusan.Scheufele@Sun.COM 	if (tgt < MRDRV_MAX_LD && lun == 0) {
11839440SSusan.Scheufele@Sun.COM 		if (instance->mr_ld_list[tgt].dip == NULL &&
11849440SSusan.Scheufele@Sun.COM 		    strcmp(ddi_driver_name(sd->sd_dev), "sd") == 0) {
11859440SSusan.Scheufele@Sun.COM 			instance->mr_ld_list[tgt].dip = tgt_dip;
11869440SSusan.Scheufele@Sun.COM 			instance->mr_ld_list[tgt].lun_type = MRSAS_LD_LUN;
11879440SSusan.Scheufele@Sun.COM 		}
11889440SSusan.Scheufele@Sun.COM 	}
11899440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
11909440SSusan.Scheufele@Sun.COM }
11919440SSusan.Scheufele@Sun.COM 
11929440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
11939440SSusan.Scheufele@Sun.COM static void
mrsas_tran_tgt_free(dev_info_t * hba_dip,dev_info_t * tgt_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)11949440SSusan.Scheufele@Sun.COM mrsas_tran_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
11959440SSusan.Scheufele@Sun.COM     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
11969440SSusan.Scheufele@Sun.COM {
11979440SSusan.Scheufele@Sun.COM 	struct mrsas_instance *instance;
11989440SSusan.Scheufele@Sun.COM 	int tgt = sd->sd_address.a_target;
11999440SSusan.Scheufele@Sun.COM 	int lun = sd->sd_address.a_lun;
12009440SSusan.Scheufele@Sun.COM 
12019440SSusan.Scheufele@Sun.COM 	instance = ADDR2MR(&sd->sd_address);
12029440SSusan.Scheufele@Sun.COM 
12039440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "tgt_free t = %d l = %d", tgt, lun));
12049440SSusan.Scheufele@Sun.COM 
12059440SSusan.Scheufele@Sun.COM 	if (tgt < MRDRV_MAX_LD && lun == 0) {
12069440SSusan.Scheufele@Sun.COM 		if (instance->mr_ld_list[tgt].dip == tgt_dip) {
12079440SSusan.Scheufele@Sun.COM 			instance->mr_ld_list[tgt].dip = NULL;
12089440SSusan.Scheufele@Sun.COM 		}
12099440SSusan.Scheufele@Sun.COM 	}
12109440SSusan.Scheufele@Sun.COM }
12119440SSusan.Scheufele@Sun.COM 
12129440SSusan.Scheufele@Sun.COM static dev_info_t *
mrsas_find_child(struct mrsas_instance * instance,uint16_t tgt,uint8_t lun)12139440SSusan.Scheufele@Sun.COM mrsas_find_child(struct mrsas_instance *instance, uint16_t tgt, uint8_t lun)
12149440SSusan.Scheufele@Sun.COM {
12159440SSusan.Scheufele@Sun.COM 	dev_info_t *child = NULL;
12169440SSusan.Scheufele@Sun.COM 	char addr[SCSI_MAXNAMELEN];
12179440SSusan.Scheufele@Sun.COM 	char tmp[MAXNAMELEN];
12189440SSusan.Scheufele@Sun.COM 
12199440SSusan.Scheufele@Sun.COM 	(void) sprintf(addr, "%x,%x", tgt, lun);
12209440SSusan.Scheufele@Sun.COM 	for (child = ddi_get_child(instance->dip); child;
12219440SSusan.Scheufele@Sun.COM 	    child = ddi_get_next_sibling(child)) {
12229440SSusan.Scheufele@Sun.COM 
12239440SSusan.Scheufele@Sun.COM 		if (mrsas_name_node(child, tmp, MAXNAMELEN) !=
12249440SSusan.Scheufele@Sun.COM 		    DDI_SUCCESS) {
12259440SSusan.Scheufele@Sun.COM 			continue;
12269440SSusan.Scheufele@Sun.COM 		}
12279440SSusan.Scheufele@Sun.COM 
12289440SSusan.Scheufele@Sun.COM 		if (strcmp(addr, tmp) == 0) {
12299440SSusan.Scheufele@Sun.COM 			break;
12309440SSusan.Scheufele@Sun.COM 		}
12319440SSusan.Scheufele@Sun.COM 	}
12329440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_find_child: return child = %p",
12339440SSusan.Scheufele@Sun.COM 	    (void *)child));
12349440SSusan.Scheufele@Sun.COM 	return (child);
12359440SSusan.Scheufele@Sun.COM }
12369440SSusan.Scheufele@Sun.COM 
12379440SSusan.Scheufele@Sun.COM static int
mrsas_name_node(dev_info_t * dip,char * name,int len)12389440SSusan.Scheufele@Sun.COM mrsas_name_node(dev_info_t *dip, char *name, int len)
12399440SSusan.Scheufele@Sun.COM {
12409440SSusan.Scheufele@Sun.COM 	int tgt, lun;
12419440SSusan.Scheufele@Sun.COM 
12429440SSusan.Scheufele@Sun.COM 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
12439440SSusan.Scheufele@Sun.COM 	    DDI_PROP_DONTPASS, "target", -1);
12449440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
12459440SSusan.Scheufele@Sun.COM 	    "mrsas_name_node: dip %p tgt %d", (void *)dip, tgt));
12469440SSusan.Scheufele@Sun.COM 	if (tgt == -1) {
12479440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
12489440SSusan.Scheufele@Sun.COM 	}
12499440SSusan.Scheufele@Sun.COM 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
12509440SSusan.Scheufele@Sun.COM 	    "lun", -1);
12519440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1,
12529440SSusan.Scheufele@Sun.COM 	    (CE_NOTE, "mrsas_name_node: tgt %d lun %d", tgt, lun));
12539440SSusan.Scheufele@Sun.COM 	if (lun == -1) {
12549440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
12559440SSusan.Scheufele@Sun.COM 	}
12569440SSusan.Scheufele@Sun.COM 	(void) snprintf(name, len, "%x,%x", tgt, lun);
12579440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
12589440SSusan.Scheufele@Sun.COM }
12599440SSusan.Scheufele@Sun.COM 
12609440SSusan.Scheufele@Sun.COM static struct scsi_pkt *
mrsas_tran_init_pkt(struct scsi_address * ap,register struct scsi_pkt * pkt,struct buf * bp,int cmdlen,int statuslen,int tgtlen,int flags,int (* callback)(),caddr_t arg)12619440SSusan.Scheufele@Sun.COM mrsas_tran_init_pkt(struct scsi_address *ap, register struct scsi_pkt *pkt,
12629440SSusan.Scheufele@Sun.COM 	struct buf *bp, int cmdlen, int statuslen, int tgtlen,
12639440SSusan.Scheufele@Sun.COM 	int flags, int (*callback)(), caddr_t arg)
12649440SSusan.Scheufele@Sun.COM {
12659440SSusan.Scheufele@Sun.COM 	struct scsa_cmd	*acmd;
12669440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance;
12679440SSusan.Scheufele@Sun.COM 	struct scsi_pkt	*new_pkt;
12689440SSusan.Scheufele@Sun.COM 
12699440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
12709440SSusan.Scheufele@Sun.COM 
12719440SSusan.Scheufele@Sun.COM 	instance = ADDR2MR(ap);
12729440SSusan.Scheufele@Sun.COM 
12739440SSusan.Scheufele@Sun.COM 	/* step #1 : pkt allocation */
12749440SSusan.Scheufele@Sun.COM 	if (pkt == NULL) {
12759440SSusan.Scheufele@Sun.COM 		pkt = scsi_hba_pkt_alloc(instance->dip, ap, cmdlen, statuslen,
12769440SSusan.Scheufele@Sun.COM 		    tgtlen, sizeof (struct scsa_cmd), callback, arg);
12779440SSusan.Scheufele@Sun.COM 		if (pkt == NULL) {
12789440SSusan.Scheufele@Sun.COM 			return (NULL);
12799440SSusan.Scheufele@Sun.COM 		}
12809440SSusan.Scheufele@Sun.COM 
12819440SSusan.Scheufele@Sun.COM 		acmd = PKT2CMD(pkt);
12829440SSusan.Scheufele@Sun.COM 
12839440SSusan.Scheufele@Sun.COM 		/*
12849440SSusan.Scheufele@Sun.COM 		 * Initialize the new pkt - we redundantly initialize
12859440SSusan.Scheufele@Sun.COM 		 * all the fields for illustrative purposes.
12869440SSusan.Scheufele@Sun.COM 		 */
12879440SSusan.Scheufele@Sun.COM 		acmd->cmd_pkt		= pkt;
12889440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags		= 0;
12899440SSusan.Scheufele@Sun.COM 		acmd->cmd_scblen	= statuslen;
12909440SSusan.Scheufele@Sun.COM 		acmd->cmd_cdblen	= cmdlen;
12919440SSusan.Scheufele@Sun.COM 		acmd->cmd_dmahandle	= NULL;
12929440SSusan.Scheufele@Sun.COM 		acmd->cmd_ncookies	= 0;
12939440SSusan.Scheufele@Sun.COM 		acmd->cmd_cookie	= 0;
12949440SSusan.Scheufele@Sun.COM 		acmd->cmd_cookiecnt	= 0;
12959440SSusan.Scheufele@Sun.COM 		acmd->cmd_nwin		= 0;
12969440SSusan.Scheufele@Sun.COM 
12979440SSusan.Scheufele@Sun.COM 		pkt->pkt_address	= *ap;
12989440SSusan.Scheufele@Sun.COM 		pkt->pkt_comp		= (void (*)())NULL;
12999440SSusan.Scheufele@Sun.COM 		pkt->pkt_flags		= 0;
13009440SSusan.Scheufele@Sun.COM 		pkt->pkt_time		= 0;
13019440SSusan.Scheufele@Sun.COM 		pkt->pkt_resid		= 0;
13029440SSusan.Scheufele@Sun.COM 		pkt->pkt_state		= 0;
13039440SSusan.Scheufele@Sun.COM 		pkt->pkt_statistics	= 0;
13049440SSusan.Scheufele@Sun.COM 		pkt->pkt_reason		= 0;
13059440SSusan.Scheufele@Sun.COM 		new_pkt			= pkt;
13069440SSusan.Scheufele@Sun.COM 	} else {
13079440SSusan.Scheufele@Sun.COM 		acmd = PKT2CMD(pkt);
13089440SSusan.Scheufele@Sun.COM 		new_pkt = NULL;
13099440SSusan.Scheufele@Sun.COM 	}
13109440SSusan.Scheufele@Sun.COM 
13119440SSusan.Scheufele@Sun.COM 	/* step #2 : dma allocation/move */
13129440SSusan.Scheufele@Sun.COM 	if (bp && bp->b_bcount != 0) {
13139440SSusan.Scheufele@Sun.COM 		if (acmd->cmd_dmahandle == NULL) {
13149440SSusan.Scheufele@Sun.COM 			if (mrsas_dma_alloc(instance, pkt, bp, flags,
13159440SSusan.Scheufele@Sun.COM 			    callback) == DDI_FAILURE) {
13169440SSusan.Scheufele@Sun.COM 				if (new_pkt) {
13179440SSusan.Scheufele@Sun.COM 					scsi_hba_pkt_free(ap, new_pkt);
13189440SSusan.Scheufele@Sun.COM 				}
13199440SSusan.Scheufele@Sun.COM 				return ((struct scsi_pkt *)NULL);
13209440SSusan.Scheufele@Sun.COM 			}
13219440SSusan.Scheufele@Sun.COM 		} else {
13229440SSusan.Scheufele@Sun.COM 			if (mrsas_dma_move(instance, pkt, bp) == DDI_FAILURE) {
13239440SSusan.Scheufele@Sun.COM 				return ((struct scsi_pkt *)NULL);
13249440SSusan.Scheufele@Sun.COM 			}
13259440SSusan.Scheufele@Sun.COM 		}
13269440SSusan.Scheufele@Sun.COM 	}
13279440SSusan.Scheufele@Sun.COM 
13289440SSusan.Scheufele@Sun.COM 	return (pkt);
13299440SSusan.Scheufele@Sun.COM }
13309440SSusan.Scheufele@Sun.COM 
13319440SSusan.Scheufele@Sun.COM static int
mrsas_tran_start(struct scsi_address * ap,register struct scsi_pkt * pkt)13329440SSusan.Scheufele@Sun.COM mrsas_tran_start(struct scsi_address *ap, register struct scsi_pkt *pkt)
13339440SSusan.Scheufele@Sun.COM {
13349440SSusan.Scheufele@Sun.COM 	uchar_t 	cmd_done = 0;
13359440SSusan.Scheufele@Sun.COM 
13369440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance = ADDR2MR(ap);
13379440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
13389440SSusan.Scheufele@Sun.COM 
133912244SYu.Wu@Sun.COM 	if (instance->deadadapter == 1) {
134012244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
134112244SYu.Wu@Sun.COM 		    "mrsas_tran_start: return TRAN_FATAL_ERROR "
134212244SYu.Wu@Sun.COM 		    "for IO, as the HBA doesnt take any more IOs"));
134312244SYu.Wu@Sun.COM 		if (pkt) {
134412244SYu.Wu@Sun.COM 			pkt->pkt_reason		= CMD_DEV_GONE;
134512244SYu.Wu@Sun.COM 			pkt->pkt_statistics	= STAT_DISCON;
134612244SYu.Wu@Sun.COM 		}
134712244SYu.Wu@Sun.COM 		return (TRAN_FATAL_ERROR);
134812244SYu.Wu@Sun.COM 	}
134912244SYu.Wu@Sun.COM 
135012244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress) {
135112244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
135212244SYu.Wu@Sun.COM 		    "returning mfi_pkt and setting TRAN_BUSY\n"));
135312244SYu.Wu@Sun.COM 		return (TRAN_BUSY);
135412244SYu.Wu@Sun.COM 	}
135512244SYu.Wu@Sun.COM 
135612244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d:SCSI CDB[0]=0x%x time:%x",
135712244SYu.Wu@Sun.COM 	    __func__, __LINE__, pkt->pkt_cdbp[0], pkt->pkt_time));
13589440SSusan.Scheufele@Sun.COM 
13599440SSusan.Scheufele@Sun.COM 	pkt->pkt_reason	= CMD_CMPLT;
13609440SSusan.Scheufele@Sun.COM 	*pkt->pkt_scbp = STATUS_GOOD; /* clear arq scsi_status */
13619440SSusan.Scheufele@Sun.COM 
13629440SSusan.Scheufele@Sun.COM 	cmd = build_cmd(instance, ap, pkt, &cmd_done);
13639440SSusan.Scheufele@Sun.COM 
13649440SSusan.Scheufele@Sun.COM 	/*
13659440SSusan.Scheufele@Sun.COM 	 * Check if the command is already completed by the mrsas_build_cmd()
13669440SSusan.Scheufele@Sun.COM 	 * routine. In which case the busy_flag would be clear and scb will be
13679440SSusan.Scheufele@Sun.COM 	 * NULL and appropriate reason provided in pkt_reason field
13689440SSusan.Scheufele@Sun.COM 	 */
13699440SSusan.Scheufele@Sun.COM 	if (cmd_done) {
13709440SSusan.Scheufele@Sun.COM 		pkt->pkt_reason = CMD_CMPLT;
13719440SSusan.Scheufele@Sun.COM 		pkt->pkt_scbp[0] = STATUS_GOOD;
13729440SSusan.Scheufele@Sun.COM 		pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET
13739440SSusan.Scheufele@Sun.COM 		    | STATE_SENT_CMD;
13749440SSusan.Scheufele@Sun.COM 		if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && pkt->pkt_comp) {
13759440SSusan.Scheufele@Sun.COM 			(*pkt->pkt_comp)(pkt);
13769440SSusan.Scheufele@Sun.COM 		}
13779440SSusan.Scheufele@Sun.COM 
13789440SSusan.Scheufele@Sun.COM 		return (TRAN_ACCEPT);
13799440SSusan.Scheufele@Sun.COM 	}
13809440SSusan.Scheufele@Sun.COM 
13819440SSusan.Scheufele@Sun.COM 	if (cmd == NULL) {
13829440SSusan.Scheufele@Sun.COM 		return (TRAN_BUSY);
13839440SSusan.Scheufele@Sun.COM 	}
13849440SSusan.Scheufele@Sun.COM 
13859440SSusan.Scheufele@Sun.COM 	if ((pkt->pkt_flags & FLAG_NOINTR) == 0) {
13869440SSusan.Scheufele@Sun.COM 		if (instance->fw_outstanding > instance->max_fw_cmds) {
13879440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_CONT, "mr_sas:Firmware busy"));
138810820SSusan.Scheufele@Sun.COM 			DTRACE_PROBE2(start_tran_err,
138910820SSusan.Scheufele@Sun.COM 			    uint16_t, instance->fw_outstanding,
139010820SSusan.Scheufele@Sun.COM 			    uint16_t, instance->max_fw_cmds);
13919440SSusan.Scheufele@Sun.COM 			return_mfi_pkt(instance, cmd);
13929440SSusan.Scheufele@Sun.COM 			return (TRAN_BUSY);
13939440SSusan.Scheufele@Sun.COM 		}
13949440SSusan.Scheufele@Sun.COM 
13959440SSusan.Scheufele@Sun.COM 		/* Synchronize the Cmd frame for the controller */
13969440SSusan.Scheufele@Sun.COM 		(void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 0, 0,
13979440SSusan.Scheufele@Sun.COM 		    DDI_DMA_SYNC_FORDEV);
139812244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "Push SCSI CDB[0]=0x%x"
139912244SYu.Wu@Sun.COM 		    "cmd->index:%x\n", pkt->pkt_cdbp[0], cmd->index));
14009440SSusan.Scheufele@Sun.COM 		instance->func_ptr->issue_cmd(cmd, instance);
14019440SSusan.Scheufele@Sun.COM 
14029440SSusan.Scheufele@Sun.COM 	} else {
14039440SSusan.Scheufele@Sun.COM 		struct mrsas_header *hdr = &cmd->frame->hdr;
14049440SSusan.Scheufele@Sun.COM 
14059440SSusan.Scheufele@Sun.COM 		cmd->sync_cmd = MRSAS_TRUE;
14069440SSusan.Scheufele@Sun.COM 
14079440SSusan.Scheufele@Sun.COM 		instance->func_ptr-> issue_cmd_in_poll_mode(instance, cmd);
14089440SSusan.Scheufele@Sun.COM 
14099440SSusan.Scheufele@Sun.COM 		pkt->pkt_reason		= CMD_CMPLT;
14109440SSusan.Scheufele@Sun.COM 		pkt->pkt_statistics	= 0;
14119440SSusan.Scheufele@Sun.COM 		pkt->pkt_state |= STATE_XFERRED_DATA | STATE_GOT_STATUS;
14129440SSusan.Scheufele@Sun.COM 
14139440SSusan.Scheufele@Sun.COM 		switch (ddi_get8(cmd->frame_dma_obj.acc_handle,
14149440SSusan.Scheufele@Sun.COM 		    &hdr->cmd_status)) {
14159440SSusan.Scheufele@Sun.COM 		case MFI_STAT_OK:
14169440SSusan.Scheufele@Sun.COM 			pkt->pkt_scbp[0] = STATUS_GOOD;
14179440SSusan.Scheufele@Sun.COM 			break;
14189440SSusan.Scheufele@Sun.COM 
14199440SSusan.Scheufele@Sun.COM 		case MFI_STAT_SCSI_DONE_WITH_ERROR:
14209440SSusan.Scheufele@Sun.COM 
14219440SSusan.Scheufele@Sun.COM 			pkt->pkt_reason	= CMD_CMPLT;
14229440SSusan.Scheufele@Sun.COM 			pkt->pkt_statistics = 0;
14239440SSusan.Scheufele@Sun.COM 
14249440SSusan.Scheufele@Sun.COM 			((struct scsi_status *)pkt->pkt_scbp)->sts_chk = 1;
14259440SSusan.Scheufele@Sun.COM 			break;
14269440SSusan.Scheufele@Sun.COM 
14279440SSusan.Scheufele@Sun.COM 		case MFI_STAT_DEVICE_NOT_FOUND:
14289440SSusan.Scheufele@Sun.COM 			pkt->pkt_reason		= CMD_DEV_GONE;
14299440SSusan.Scheufele@Sun.COM 			pkt->pkt_statistics	= STAT_DISCON;
14309440SSusan.Scheufele@Sun.COM 			break;
14319440SSusan.Scheufele@Sun.COM 
14329440SSusan.Scheufele@Sun.COM 		default:
14339440SSusan.Scheufele@Sun.COM 			((struct scsi_status *)pkt->pkt_scbp)->sts_busy = 1;
14349440SSusan.Scheufele@Sun.COM 		}
14359440SSusan.Scheufele@Sun.COM 
143610600SYu.Wu@Sun.COM 		(void) mrsas_common_check(instance, cmd);
143710820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(start_nointr_done, uint8_t, hdr->cmd,
143810820SSusan.Scheufele@Sun.COM 		    uint8_t, hdr->cmd_status);
14399440SSusan.Scheufele@Sun.COM 		return_mfi_pkt(instance, cmd);
14409440SSusan.Scheufele@Sun.COM 
14419440SSusan.Scheufele@Sun.COM 		if (pkt->pkt_comp) {
14429440SSusan.Scheufele@Sun.COM 			(*pkt->pkt_comp)(pkt);
14439440SSusan.Scheufele@Sun.COM 		}
14449440SSusan.Scheufele@Sun.COM 
14459440SSusan.Scheufele@Sun.COM 	}
14469440SSusan.Scheufele@Sun.COM 
14479440SSusan.Scheufele@Sun.COM 	return (TRAN_ACCEPT);
14489440SSusan.Scheufele@Sun.COM }
14499440SSusan.Scheufele@Sun.COM 
14509440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
14519440SSusan.Scheufele@Sun.COM static int
mrsas_tran_abort(struct scsi_address * ap,struct scsi_pkt * pkt)14529440SSusan.Scheufele@Sun.COM mrsas_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
14539440SSusan.Scheufele@Sun.COM {
14549440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
14559440SSusan.Scheufele@Sun.COM 
14569440SSusan.Scheufele@Sun.COM 	/* abort command not supported by H/W */
14579440SSusan.Scheufele@Sun.COM 
14589440SSusan.Scheufele@Sun.COM 	return (DDI_FAILURE);
14599440SSusan.Scheufele@Sun.COM }
14609440SSusan.Scheufele@Sun.COM 
14619440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
14629440SSusan.Scheufele@Sun.COM static int
mrsas_tran_reset(struct scsi_address * ap,int level)14639440SSusan.Scheufele@Sun.COM mrsas_tran_reset(struct scsi_address *ap, int level)
14649440SSusan.Scheufele@Sun.COM {
14659440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
14669440SSusan.Scheufele@Sun.COM 
14679440SSusan.Scheufele@Sun.COM 	/* reset command not supported by H/W */
14689440SSusan.Scheufele@Sun.COM 
14699440SSusan.Scheufele@Sun.COM 	return (DDI_FAILURE);
14709440SSusan.Scheufele@Sun.COM 
14719440SSusan.Scheufele@Sun.COM }
14729440SSusan.Scheufele@Sun.COM 
14739440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
14749440SSusan.Scheufele@Sun.COM static int
mrsas_tran_getcap(struct scsi_address * ap,char * cap,int whom)14759440SSusan.Scheufele@Sun.COM mrsas_tran_getcap(struct scsi_address *ap, char *cap, int whom)
14769440SSusan.Scheufele@Sun.COM {
14779440SSusan.Scheufele@Sun.COM 	int	rval = 0;
14789440SSusan.Scheufele@Sun.COM 
14799440SSusan.Scheufele@Sun.COM 	struct mrsas_instance	*instance = ADDR2MR(ap);
14809440SSusan.Scheufele@Sun.COM 
14819440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
14829440SSusan.Scheufele@Sun.COM 
14839440SSusan.Scheufele@Sun.COM 	/* we do allow inquiring about capabilities for other targets */
14849440SSusan.Scheufele@Sun.COM 	if (cap == NULL) {
14859440SSusan.Scheufele@Sun.COM 		return (-1);
14869440SSusan.Scheufele@Sun.COM 	}
14879440SSusan.Scheufele@Sun.COM 
14889440SSusan.Scheufele@Sun.COM 	switch (scsi_hba_lookup_capstr(cap)) {
14899440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_DMA_MAX:
14909440SSusan.Scheufele@Sun.COM 		/* Limit to 16MB max transfer */
14919440SSusan.Scheufele@Sun.COM 		rval = mrsas_max_cap_maxxfer;
14929440SSusan.Scheufele@Sun.COM 		break;
14939440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_MSG_OUT:
14949440SSusan.Scheufele@Sun.COM 		rval = 1;
14959440SSusan.Scheufele@Sun.COM 		break;
14969440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_DISCONNECT:
14979440SSusan.Scheufele@Sun.COM 		rval = 0;
14989440SSusan.Scheufele@Sun.COM 		break;
14999440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_SYNCHRONOUS:
15009440SSusan.Scheufele@Sun.COM 		rval = 0;
15019440SSusan.Scheufele@Sun.COM 		break;
15029440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_WIDE_XFER:
15039440SSusan.Scheufele@Sun.COM 		rval = 1;
15049440SSusan.Scheufele@Sun.COM 		break;
15059440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_TAGGED_QING:
15069440SSusan.Scheufele@Sun.COM 		rval = 1;
15079440SSusan.Scheufele@Sun.COM 		break;
15089440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_UNTAGGED_QING:
15099440SSusan.Scheufele@Sun.COM 		rval = 1;
15109440SSusan.Scheufele@Sun.COM 		break;
15119440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_PARITY:
15129440SSusan.Scheufele@Sun.COM 		rval = 1;
15139440SSusan.Scheufele@Sun.COM 		break;
15149440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_INITIATOR_ID:
15159440SSusan.Scheufele@Sun.COM 		rval = instance->init_id;
15169440SSusan.Scheufele@Sun.COM 		break;
15179440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_ARQ:
15189440SSusan.Scheufele@Sun.COM 		rval = 1;
15199440SSusan.Scheufele@Sun.COM 		break;
15209440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_LINKED_CMDS:
15219440SSusan.Scheufele@Sun.COM 		rval = 0;
15229440SSusan.Scheufele@Sun.COM 		break;
15239440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_RESET_NOTIFICATION:
15249440SSusan.Scheufele@Sun.COM 		rval = 1;
15259440SSusan.Scheufele@Sun.COM 		break;
15269440SSusan.Scheufele@Sun.COM 	case SCSI_CAP_GEOMETRY:
15279440SSusan.Scheufele@Sun.COM 		rval = -1;
15289440SSusan.Scheufele@Sun.COM 
15299440SSusan.Scheufele@Sun.COM 		break;
15309440SSusan.Scheufele@Sun.COM 	default:
15319440SSusan.Scheufele@Sun.COM 		con_log(CL_DLEVEL2, (CE_NOTE, "Default cap coming 0x%x",
15329440SSusan.Scheufele@Sun.COM 		    scsi_hba_lookup_capstr(cap)));
15339440SSusan.Scheufele@Sun.COM 		rval = -1;
15349440SSusan.Scheufele@Sun.COM 		break;
15359440SSusan.Scheufele@Sun.COM 	}
15369440SSusan.Scheufele@Sun.COM 
15379440SSusan.Scheufele@Sun.COM 	return (rval);
15389440SSusan.Scheufele@Sun.COM }
15399440SSusan.Scheufele@Sun.COM 
15409440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
15419440SSusan.Scheufele@Sun.COM static int
mrsas_tran_setcap(struct scsi_address * ap,char * cap,int value,int whom)15429440SSusan.Scheufele@Sun.COM mrsas_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom)
15439440SSusan.Scheufele@Sun.COM {
15449440SSusan.Scheufele@Sun.COM 	int		rval = 1;
15459440SSusan.Scheufele@Sun.COM 
15469440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
15479440SSusan.Scheufele@Sun.COM 
15489440SSusan.Scheufele@Sun.COM 	/* We don't allow setting capabilities for other targets */
15499440SSusan.Scheufele@Sun.COM 	if (cap == NULL || whom == 0) {
15509440SSusan.Scheufele@Sun.COM 		return (-1);
15519440SSusan.Scheufele@Sun.COM 	}
15529440SSusan.Scheufele@Sun.COM 
15539440SSusan.Scheufele@Sun.COM 	switch (scsi_hba_lookup_capstr(cap)) {
15549440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_DMA_MAX:
15559440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_MSG_OUT:
15569440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_PARITY:
15579440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_LINKED_CMDS:
15589440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_RESET_NOTIFICATION:
15599440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_DISCONNECT:
15609440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_SYNCHRONOUS:
15619440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_UNTAGGED_QING:
15629440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_WIDE_XFER:
15639440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_INITIATOR_ID:
15649440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_ARQ:
15659440SSusan.Scheufele@Sun.COM 			/*
15669440SSusan.Scheufele@Sun.COM 			 * None of these are settable via
15679440SSusan.Scheufele@Sun.COM 			 * the capability interface.
15689440SSusan.Scheufele@Sun.COM 			 */
15699440SSusan.Scheufele@Sun.COM 			break;
15709440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_TAGGED_QING:
15719440SSusan.Scheufele@Sun.COM 			rval = 1;
15729440SSusan.Scheufele@Sun.COM 			break;
15739440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_SECTOR_SIZE:
15749440SSusan.Scheufele@Sun.COM 			rval = 1;
15759440SSusan.Scheufele@Sun.COM 			break;
15769440SSusan.Scheufele@Sun.COM 
15779440SSusan.Scheufele@Sun.COM 		case SCSI_CAP_TOTAL_SECTORS:
15789440SSusan.Scheufele@Sun.COM 			rval = 1;
15799440SSusan.Scheufele@Sun.COM 			break;
15809440SSusan.Scheufele@Sun.COM 		default:
15819440SSusan.Scheufele@Sun.COM 			rval = -1;
15829440SSusan.Scheufele@Sun.COM 			break;
15839440SSusan.Scheufele@Sun.COM 	}
15849440SSusan.Scheufele@Sun.COM 
15859440SSusan.Scheufele@Sun.COM 	return (rval);
15869440SSusan.Scheufele@Sun.COM }
15879440SSusan.Scheufele@Sun.COM 
15889440SSusan.Scheufele@Sun.COM static void
mrsas_tran_destroy_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)15899440SSusan.Scheufele@Sun.COM mrsas_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
15909440SSusan.Scheufele@Sun.COM {
15919440SSusan.Scheufele@Sun.COM 	struct scsa_cmd *acmd = PKT2CMD(pkt);
15929440SSusan.Scheufele@Sun.COM 
15939440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
15949440SSusan.Scheufele@Sun.COM 
15959440SSusan.Scheufele@Sun.COM 	if (acmd->cmd_flags & CFLAG_DMAVALID) {
15969440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags &= ~CFLAG_DMAVALID;
15979440SSusan.Scheufele@Sun.COM 
15989440SSusan.Scheufele@Sun.COM 		(void) ddi_dma_unbind_handle(acmd->cmd_dmahandle);
15999440SSusan.Scheufele@Sun.COM 
16009440SSusan.Scheufele@Sun.COM 		ddi_dma_free_handle(&acmd->cmd_dmahandle);
16019440SSusan.Scheufele@Sun.COM 
16029440SSusan.Scheufele@Sun.COM 		acmd->cmd_dmahandle = NULL;
16039440SSusan.Scheufele@Sun.COM 	}
16049440SSusan.Scheufele@Sun.COM 
16059440SSusan.Scheufele@Sun.COM 	/* free the pkt */
16069440SSusan.Scheufele@Sun.COM 	scsi_hba_pkt_free(ap, pkt);
16079440SSusan.Scheufele@Sun.COM }
16089440SSusan.Scheufele@Sun.COM 
16099440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
16109440SSusan.Scheufele@Sun.COM static void
mrsas_tran_dmafree(struct scsi_address * ap,struct scsi_pkt * pkt)16119440SSusan.Scheufele@Sun.COM mrsas_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
16129440SSusan.Scheufele@Sun.COM {
16139440SSusan.Scheufele@Sun.COM 	register struct scsa_cmd *acmd = PKT2CMD(pkt);
16149440SSusan.Scheufele@Sun.COM 
16159440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
16169440SSusan.Scheufele@Sun.COM 
16179440SSusan.Scheufele@Sun.COM 	if (acmd->cmd_flags & CFLAG_DMAVALID) {
16189440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags &= ~CFLAG_DMAVALID;
16199440SSusan.Scheufele@Sun.COM 
16209440SSusan.Scheufele@Sun.COM 		(void) ddi_dma_unbind_handle(acmd->cmd_dmahandle);
16219440SSusan.Scheufele@Sun.COM 
16229440SSusan.Scheufele@Sun.COM 		ddi_dma_free_handle(&acmd->cmd_dmahandle);
16239440SSusan.Scheufele@Sun.COM 
16249440SSusan.Scheufele@Sun.COM 		acmd->cmd_dmahandle = NULL;
16259440SSusan.Scheufele@Sun.COM 	}
16269440SSusan.Scheufele@Sun.COM }
16279440SSusan.Scheufele@Sun.COM 
16289440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
16299440SSusan.Scheufele@Sun.COM static void
mrsas_tran_sync_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)16309440SSusan.Scheufele@Sun.COM mrsas_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
16319440SSusan.Scheufele@Sun.COM {
16329440SSusan.Scheufele@Sun.COM 	register struct scsa_cmd	*acmd = PKT2CMD(pkt);
16339440SSusan.Scheufele@Sun.COM 
16349440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
16359440SSusan.Scheufele@Sun.COM 
16369440SSusan.Scheufele@Sun.COM 	if (acmd->cmd_flags & CFLAG_DMAVALID) {
16379440SSusan.Scheufele@Sun.COM 		(void) ddi_dma_sync(acmd->cmd_dmahandle, acmd->cmd_dma_offset,
16389440SSusan.Scheufele@Sun.COM 		    acmd->cmd_dma_len, (acmd->cmd_flags & CFLAG_DMASEND) ?
16399440SSusan.Scheufele@Sun.COM 		    DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
16409440SSusan.Scheufele@Sun.COM 	}
16419440SSusan.Scheufele@Sun.COM }
16429440SSusan.Scheufele@Sun.COM 
16439440SSusan.Scheufele@Sun.COM /*
16449440SSusan.Scheufele@Sun.COM  * mrsas_isr(caddr_t)
16459440SSusan.Scheufele@Sun.COM  *
16469440SSusan.Scheufele@Sun.COM  * The Interrupt Service Routine
16479440SSusan.Scheufele@Sun.COM  *
16489440SSusan.Scheufele@Sun.COM  * Collect status for all completed commands and do callback
16499440SSusan.Scheufele@Sun.COM  *
16509440SSusan.Scheufele@Sun.COM  */
16519440SSusan.Scheufele@Sun.COM static uint_t
mrsas_isr(struct mrsas_instance * instance)16529440SSusan.Scheufele@Sun.COM mrsas_isr(struct mrsas_instance *instance)
16539440SSusan.Scheufele@Sun.COM {
16549440SSusan.Scheufele@Sun.COM 	int		need_softintr;
16559440SSusan.Scheufele@Sun.COM 	uint32_t	producer;
16569440SSusan.Scheufele@Sun.COM 	uint32_t	consumer;
16579440SSusan.Scheufele@Sun.COM 	uint32_t	context;
16589440SSusan.Scheufele@Sun.COM 
16599440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
166012244SYu.Wu@Sun.COM 	struct mrsas_header	*hdr;
166112244SYu.Wu@Sun.COM 	struct scsi_pkt		*pkt;
16629440SSusan.Scheufele@Sun.COM 
16639440SSusan.Scheufele@Sun.COM 	ASSERT(instance);
16649440SSusan.Scheufele@Sun.COM 	if ((instance->intr_type == DDI_INTR_TYPE_FIXED) &&
16659440SSusan.Scheufele@Sun.COM 	    !instance->func_ptr->intr_ack(instance)) {
16669440SSusan.Scheufele@Sun.COM 		return (DDI_INTR_UNCLAIMED);
16679440SSusan.Scheufele@Sun.COM 	}
16689440SSusan.Scheufele@Sun.COM 
16699440SSusan.Scheufele@Sun.COM 	(void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle,
16709440SSusan.Scheufele@Sun.COM 	    0, 0, DDI_DMA_SYNC_FORCPU);
16719440SSusan.Scheufele@Sun.COM 
16729440SSusan.Scheufele@Sun.COM 	if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle)
16739440SSusan.Scheufele@Sun.COM 	    != DDI_SUCCESS) {
16749440SSusan.Scheufele@Sun.COM 		mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
16759440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
167612244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
167712244SYu.Wu@Sun.COM 		    "mr_sas_isr(): FMA check, returning DDI_INTR_UNCLAIMED"));
16789795SYu.Wu@Sun.COM 		return (DDI_INTR_CLAIMED);
16799440SSusan.Scheufele@Sun.COM 	}
168012244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
16819440SSusan.Scheufele@Sun.COM 
168212244SYu.Wu@Sun.COM #ifdef OCRDEBUG
168312244SYu.Wu@Sun.COM 	if (debug_consecutive_timeout_after_ocr_g == 1) {
168412244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
168512244SYu.Wu@Sun.COM 		"simulating consecutive timeout after ocr"));
168612244SYu.Wu@Sun.COM 		return (DDI_INTR_CLAIMED);
168712244SYu.Wu@Sun.COM 	}
168812244SYu.Wu@Sun.COM #endif
168912244SYu.Wu@Sun.COM 
16909440SSusan.Scheufele@Sun.COM 	mutex_enter(&instance->completed_pool_mtx);
169112244SYu.Wu@Sun.COM 	mutex_enter(&instance->cmd_pend_mtx);
16929440SSusan.Scheufele@Sun.COM 
169312638SSusan.Scheufele@Sun.COM 	producer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
169412638SSusan.Scheufele@Sun.COM 	    instance->producer);
169512638SSusan.Scheufele@Sun.COM 	consumer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
169612638SSusan.Scheufele@Sun.COM 	    instance->consumer);
169712638SSusan.Scheufele@Sun.COM 
169812638SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, " producer %x consumer %x ",
169912638SSusan.Scheufele@Sun.COM 	    producer, consumer));
170012638SSusan.Scheufele@Sun.COM 	if (producer == consumer) {
170112638SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "producer =  consumer case"));
170212638SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(isr_pc_err, uint32_t, producer,
170312638SSusan.Scheufele@Sun.COM 		    uint32_t, consumer);
1704*12757SYu.Wu@Sun.COM 		mutex_exit(&instance->cmd_pend_mtx);
170512638SSusan.Scheufele@Sun.COM 		mutex_exit(&instance->completed_pool_mtx);
170612638SSusan.Scheufele@Sun.COM 		return (DDI_INTR_CLAIMED);
170712638SSusan.Scheufele@Sun.COM 	}
170812638SSusan.Scheufele@Sun.COM 
17099440SSusan.Scheufele@Sun.COM 	while (consumer != producer) {
17109440SSusan.Scheufele@Sun.COM 		context = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
17119440SSusan.Scheufele@Sun.COM 		    &instance->reply_queue[consumer]);
17129440SSusan.Scheufele@Sun.COM 		cmd = instance->cmd_list[context];
171312244SYu.Wu@Sun.COM 
171412244SYu.Wu@Sun.COM 		if (cmd->sync_cmd == MRSAS_TRUE) {
171512244SYu.Wu@Sun.COM 		hdr = (struct mrsas_header *)&cmd->frame->hdr;
171612244SYu.Wu@Sun.COM 		if (hdr) {
171712244SYu.Wu@Sun.COM 			mlist_del_init(&cmd->list);
171812244SYu.Wu@Sun.COM 		}
171912244SYu.Wu@Sun.COM 		} else {
172012244SYu.Wu@Sun.COM 			pkt = cmd->pkt;
172112244SYu.Wu@Sun.COM 			if (pkt) {
172212244SYu.Wu@Sun.COM 				mlist_del_init(&cmd->list);
172312244SYu.Wu@Sun.COM 			}
172412244SYu.Wu@Sun.COM 		}
172512244SYu.Wu@Sun.COM 
17269440SSusan.Scheufele@Sun.COM 		mlist_add_tail(&cmd->list, &instance->completed_pool_list);
17279440SSusan.Scheufele@Sun.COM 
17289440SSusan.Scheufele@Sun.COM 		consumer++;
17299440SSusan.Scheufele@Sun.COM 		if (consumer == (instance->max_fw_cmds + 1)) {
17309440SSusan.Scheufele@Sun.COM 			consumer = 0;
17319440SSusan.Scheufele@Sun.COM 		}
17329440SSusan.Scheufele@Sun.COM 	}
173312638SSusan.Scheufele@Sun.COM 	ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
173412638SSusan.Scheufele@Sun.COM 	    instance->consumer, consumer);
173512244SYu.Wu@Sun.COM 	mutex_exit(&instance->cmd_pend_mtx);
17369440SSusan.Scheufele@Sun.COM 	mutex_exit(&instance->completed_pool_mtx);
17379440SSusan.Scheufele@Sun.COM 
17389440SSusan.Scheufele@Sun.COM 	(void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle,
17399440SSusan.Scheufele@Sun.COM 	    0, 0, DDI_DMA_SYNC_FORDEV);
17409440SSusan.Scheufele@Sun.COM 
17419440SSusan.Scheufele@Sun.COM 	if (instance->softint_running) {
17429440SSusan.Scheufele@Sun.COM 		need_softintr = 0;
17439440SSusan.Scheufele@Sun.COM 	} else {
17449440SSusan.Scheufele@Sun.COM 		need_softintr = 1;
17459440SSusan.Scheufele@Sun.COM 	}
17469440SSusan.Scheufele@Sun.COM 
17479440SSusan.Scheufele@Sun.COM 	if (instance->isr_level == HIGH_LEVEL_INTR) {
17489440SSusan.Scheufele@Sun.COM 		if (need_softintr) {
17499440SSusan.Scheufele@Sun.COM 			ddi_trigger_softintr(instance->soft_intr_id);
17509440SSusan.Scheufele@Sun.COM 		}
17519440SSusan.Scheufele@Sun.COM 	} else {
17529440SSusan.Scheufele@Sun.COM 		/*
17539440SSusan.Scheufele@Sun.COM 		 * Not a high-level interrupt, therefore call the soft level
17549440SSusan.Scheufele@Sun.COM 		 * interrupt explicitly
17559440SSusan.Scheufele@Sun.COM 		 */
17569440SSusan.Scheufele@Sun.COM 		(void) mrsas_softintr(instance);
17579440SSusan.Scheufele@Sun.COM 	}
17589440SSusan.Scheufele@Sun.COM 
17599440SSusan.Scheufele@Sun.COM 	return (DDI_INTR_CLAIMED);
17609440SSusan.Scheufele@Sun.COM }
17619440SSusan.Scheufele@Sun.COM 
17629440SSusan.Scheufele@Sun.COM 
17639440SSusan.Scheufele@Sun.COM /*
17649440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
17659440SSusan.Scheufele@Sun.COM  *                                                                            *
17669440SSusan.Scheufele@Sun.COM  *                                  libraries                                 *
17679440SSusan.Scheufele@Sun.COM  *                                                                            *
17689440SSusan.Scheufele@Sun.COM  * ************************************************************************** *
17699440SSusan.Scheufele@Sun.COM  */
17709440SSusan.Scheufele@Sun.COM /*
17719440SSusan.Scheufele@Sun.COM  * get_mfi_pkt : Get a command from the free pool
17729440SSusan.Scheufele@Sun.COM  * After successful allocation, the caller of this routine
17739440SSusan.Scheufele@Sun.COM  * must clear the frame buffer (memset to zero) before
17749440SSusan.Scheufele@Sun.COM  * using the packet further.
17759440SSusan.Scheufele@Sun.COM  *
17769440SSusan.Scheufele@Sun.COM  * ***** Note *****
17779440SSusan.Scheufele@Sun.COM  * After clearing the frame buffer the context id of the
17789440SSusan.Scheufele@Sun.COM  * frame buffer SHOULD be restored back.
17799440SSusan.Scheufele@Sun.COM  */
17809440SSusan.Scheufele@Sun.COM static struct mrsas_cmd *
get_mfi_pkt(struct mrsas_instance * instance)17819440SSusan.Scheufele@Sun.COM get_mfi_pkt(struct mrsas_instance *instance)
17829440SSusan.Scheufele@Sun.COM {
17839440SSusan.Scheufele@Sun.COM 	mlist_t 		*head = &instance->cmd_pool_list;
17849440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd = NULL;
17859440SSusan.Scheufele@Sun.COM 
17869440SSusan.Scheufele@Sun.COM 	mutex_enter(&instance->cmd_pool_mtx);
17879440SSusan.Scheufele@Sun.COM 	ASSERT(mutex_owned(&instance->cmd_pool_mtx));
17889440SSusan.Scheufele@Sun.COM 
17899440SSusan.Scheufele@Sun.COM 	if (!mlist_empty(head)) {
17909440SSusan.Scheufele@Sun.COM 		cmd = mlist_entry(head->next, struct mrsas_cmd, list);
17919440SSusan.Scheufele@Sun.COM 		mlist_del_init(head->next);
17929440SSusan.Scheufele@Sun.COM 	}
179312244SYu.Wu@Sun.COM 	if (cmd != NULL) {
17949440SSusan.Scheufele@Sun.COM 		cmd->pkt = NULL;
179512244SYu.Wu@Sun.COM 		cmd->retry_count_for_ocr = 0;
179612244SYu.Wu@Sun.COM 		cmd->drv_pkt_time = 0;
179712244SYu.Wu@Sun.COM 	}
17989440SSusan.Scheufele@Sun.COM 	mutex_exit(&instance->cmd_pool_mtx);
17999440SSusan.Scheufele@Sun.COM 
18009440SSusan.Scheufele@Sun.COM 	return (cmd);
18019440SSusan.Scheufele@Sun.COM }
18029440SSusan.Scheufele@Sun.COM 
180312244SYu.Wu@Sun.COM static struct mrsas_cmd *
get_mfi_app_pkt(struct mrsas_instance * instance)180412244SYu.Wu@Sun.COM get_mfi_app_pkt(struct mrsas_instance *instance)
180512244SYu.Wu@Sun.COM {
180612244SYu.Wu@Sun.COM 	mlist_t				*head = &instance->app_cmd_pool_list;
180712244SYu.Wu@Sun.COM 	struct mrsas_cmd	*cmd = NULL;
180812244SYu.Wu@Sun.COM 
180912244SYu.Wu@Sun.COM 	mutex_enter(&instance->app_cmd_pool_mtx);
181012244SYu.Wu@Sun.COM 	ASSERT(mutex_owned(&instance->app_cmd_pool_mtx));
181112244SYu.Wu@Sun.COM 
181212244SYu.Wu@Sun.COM 	if (!mlist_empty(head)) {
181312244SYu.Wu@Sun.COM 		cmd = mlist_entry(head->next, struct mrsas_cmd, list);
181412244SYu.Wu@Sun.COM 		mlist_del_init(head->next);
181512244SYu.Wu@Sun.COM 	}
181612244SYu.Wu@Sun.COM 	if (cmd != NULL)
181712244SYu.Wu@Sun.COM 		cmd->pkt = NULL;
181812244SYu.Wu@Sun.COM 	mutex_exit(&instance->app_cmd_pool_mtx);
181912244SYu.Wu@Sun.COM 
182012244SYu.Wu@Sun.COM 	return (cmd);
182112244SYu.Wu@Sun.COM }
18229440SSusan.Scheufele@Sun.COM /*
18239440SSusan.Scheufele@Sun.COM  * return_mfi_pkt : Return a cmd to free command pool
18249440SSusan.Scheufele@Sun.COM  */
18259440SSusan.Scheufele@Sun.COM static void
return_mfi_pkt(struct mrsas_instance * instance,struct mrsas_cmd * cmd)18269440SSusan.Scheufele@Sun.COM return_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
18279440SSusan.Scheufele@Sun.COM {
18289440SSusan.Scheufele@Sun.COM 	mutex_enter(&instance->cmd_pool_mtx);
18299440SSusan.Scheufele@Sun.COM 	ASSERT(mutex_owned(&instance->cmd_pool_mtx));
1830*12757SYu.Wu@Sun.COM 	/* use mlist_add_tail for debug assistance */
1831*12757SYu.Wu@Sun.COM 	mlist_add_tail(&cmd->list, &instance->cmd_pool_list);
18329440SSusan.Scheufele@Sun.COM 
18339440SSusan.Scheufele@Sun.COM 	mutex_exit(&instance->cmd_pool_mtx);
18349440SSusan.Scheufele@Sun.COM }
18359440SSusan.Scheufele@Sun.COM 
183612244SYu.Wu@Sun.COM static void
return_mfi_app_pkt(struct mrsas_instance * instance,struct mrsas_cmd * cmd)183712244SYu.Wu@Sun.COM return_mfi_app_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
183812244SYu.Wu@Sun.COM {
183912244SYu.Wu@Sun.COM 	mutex_enter(&instance->app_cmd_pool_mtx);
184012244SYu.Wu@Sun.COM 	ASSERT(mutex_owned(&instance->app_cmd_pool_mtx));
184112244SYu.Wu@Sun.COM 
184212244SYu.Wu@Sun.COM 	mlist_add(&cmd->list, &instance->app_cmd_pool_list);
184312244SYu.Wu@Sun.COM 
184412244SYu.Wu@Sun.COM 	mutex_exit(&instance->app_cmd_pool_mtx);
184512244SYu.Wu@Sun.COM }
184612244SYu.Wu@Sun.COM static void
push_pending_mfi_pkt(struct mrsas_instance * instance,struct mrsas_cmd * cmd)184712244SYu.Wu@Sun.COM push_pending_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
184812244SYu.Wu@Sun.COM {
184912244SYu.Wu@Sun.COM 	struct scsi_pkt *pkt;
185012244SYu.Wu@Sun.COM 	struct mrsas_header	*hdr;
185112244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "push_pending_pkt(): Called\n"));
185212244SYu.Wu@Sun.COM 	mutex_enter(&instance->cmd_pend_mtx);
185312244SYu.Wu@Sun.COM 	ASSERT(mutex_owned(&instance->cmd_pend_mtx));
185412244SYu.Wu@Sun.COM 	mlist_del_init(&cmd->list);
185512244SYu.Wu@Sun.COM 	mlist_add_tail(&cmd->list, &instance->cmd_pend_list);
185612244SYu.Wu@Sun.COM 	if (cmd->sync_cmd == MRSAS_TRUE) {
185712244SYu.Wu@Sun.COM 		hdr = (struct mrsas_header *)&cmd->frame->hdr;
185812244SYu.Wu@Sun.COM 		if (hdr) {
185912244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_CONT,
186012244SYu.Wu@Sun.COM 			    "push_pending_mfi_pkt: "
186112244SYu.Wu@Sun.COM 			    "cmd %p index %x "
186212244SYu.Wu@Sun.COM 			    "time %llx",
186312244SYu.Wu@Sun.COM 			    (void *)cmd, cmd->index,
186412244SYu.Wu@Sun.COM 			    gethrtime()));
186512244SYu.Wu@Sun.COM 			/* Wait for specified interval  */
1866*12757SYu.Wu@Sun.COM 			cmd->drv_pkt_time = ddi_get16(
1867*12757SYu.Wu@Sun.COM 			    cmd->frame_dma_obj.acc_handle, &hdr->timeout);
1868*12757SYu.Wu@Sun.COM 			if (cmd->drv_pkt_time < debug_timeout_g)
1869*12757SYu.Wu@Sun.COM 				cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
187012244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_CONT,
187112244SYu.Wu@Sun.COM 			    "push_pending_pkt(): "
187212244SYu.Wu@Sun.COM 			    "Called IO Timeout Value %x\n",
1873*12757SYu.Wu@Sun.COM 			    cmd->drv_pkt_time));
187412244SYu.Wu@Sun.COM 		}
187512244SYu.Wu@Sun.COM 		if (hdr && instance->timeout_id == (timeout_id_t)-1) {
187612244SYu.Wu@Sun.COM 			instance->timeout_id = timeout(io_timeout_checker,
187712244SYu.Wu@Sun.COM 			    (void *) instance, drv_usectohz(MRSAS_1_SECOND));
187812244SYu.Wu@Sun.COM 		}
187912244SYu.Wu@Sun.COM 	} else {
188012244SYu.Wu@Sun.COM 		pkt = cmd->pkt;
188112244SYu.Wu@Sun.COM 		if (pkt) {
188212244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_CONT,
188312244SYu.Wu@Sun.COM 			    "push_pending_mfi_pkt: "
188412244SYu.Wu@Sun.COM 			    "cmd %p index %x pkt %p, "
188512244SYu.Wu@Sun.COM 			    "time %llx",
188612244SYu.Wu@Sun.COM 			    (void *)cmd, cmd->index, (void *)pkt,
188712244SYu.Wu@Sun.COM 			    gethrtime()));
188812244SYu.Wu@Sun.COM 			cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
188912244SYu.Wu@Sun.COM 		}
189012244SYu.Wu@Sun.COM 		if (pkt && instance->timeout_id == (timeout_id_t)-1) {
189112244SYu.Wu@Sun.COM 			instance->timeout_id = timeout(io_timeout_checker,
189212244SYu.Wu@Sun.COM 			    (void *) instance, drv_usectohz(MRSAS_1_SECOND));
189312244SYu.Wu@Sun.COM 		}
189412244SYu.Wu@Sun.COM 	}
189512244SYu.Wu@Sun.COM 
189612244SYu.Wu@Sun.COM 	mutex_exit(&instance->cmd_pend_mtx);
189712244SYu.Wu@Sun.COM }
189812244SYu.Wu@Sun.COM 
189912244SYu.Wu@Sun.COM static int
mrsas_print_pending_cmds(struct mrsas_instance * instance)190012244SYu.Wu@Sun.COM mrsas_print_pending_cmds(struct mrsas_instance *instance)
190112244SYu.Wu@Sun.COM {
190212244SYu.Wu@Sun.COM 	mlist_t *head = &instance->cmd_pend_list;
190312244SYu.Wu@Sun.COM 	mlist_t *tmp = head;
190412244SYu.Wu@Sun.COM 	struct mrsas_cmd *cmd = NULL;
190512244SYu.Wu@Sun.COM 	struct mrsas_header	*hdr;
190612244SYu.Wu@Sun.COM 	unsigned int		flag = 1;
190712244SYu.Wu@Sun.COM 
190812244SYu.Wu@Sun.COM 	struct scsi_pkt *pkt;
190912244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
191012244SYu.Wu@Sun.COM 	    "mrsas_print_pending_cmds(): Called"));
191112244SYu.Wu@Sun.COM 	while (flag) {
191212244SYu.Wu@Sun.COM 		mutex_enter(&instance->cmd_pend_mtx);
191312244SYu.Wu@Sun.COM 		tmp	=	tmp->next;
191412244SYu.Wu@Sun.COM 		if (tmp == head) {
191512244SYu.Wu@Sun.COM 			mutex_exit(&instance->cmd_pend_mtx);
191612244SYu.Wu@Sun.COM 			flag = 0;
191712244SYu.Wu@Sun.COM 			break;
191812244SYu.Wu@Sun.COM 		} else {
191912244SYu.Wu@Sun.COM 			cmd = mlist_entry(tmp, struct mrsas_cmd, list);
192012244SYu.Wu@Sun.COM 			mutex_exit(&instance->cmd_pend_mtx);
192112244SYu.Wu@Sun.COM 			if (cmd) {
192212244SYu.Wu@Sun.COM 				if (cmd->sync_cmd == MRSAS_TRUE) {
192312244SYu.Wu@Sun.COM 				hdr = (struct mrsas_header *)&cmd->frame->hdr;
192412244SYu.Wu@Sun.COM 					if (hdr) {
192512244SYu.Wu@Sun.COM 					con_log(CL_ANN1, (CE_CONT,
192612244SYu.Wu@Sun.COM 					    "print: cmd %p index %x hdr %p",
192712244SYu.Wu@Sun.COM 					    (void *)cmd, cmd->index,
192812244SYu.Wu@Sun.COM 					    (void *)hdr));
192912244SYu.Wu@Sun.COM 					}
193012244SYu.Wu@Sun.COM 				} else {
193112244SYu.Wu@Sun.COM 					pkt = cmd->pkt;
193212244SYu.Wu@Sun.COM 					if (pkt) {
193312244SYu.Wu@Sun.COM 					con_log(CL_ANN1, (CE_CONT,
193412244SYu.Wu@Sun.COM 					    "print: cmd %p index %x "
193512244SYu.Wu@Sun.COM 					    "pkt %p", (void *)cmd, cmd->index,
193612244SYu.Wu@Sun.COM 					    (void *)pkt));
193712244SYu.Wu@Sun.COM 					}
193812244SYu.Wu@Sun.COM 				}
193912244SYu.Wu@Sun.COM 			}
194012244SYu.Wu@Sun.COM 		}
194112244SYu.Wu@Sun.COM 	}
194212244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_print_pending_cmds(): Done\n"));
194312244SYu.Wu@Sun.COM 	return (DDI_SUCCESS);
194412244SYu.Wu@Sun.COM }
194512244SYu.Wu@Sun.COM 
194612244SYu.Wu@Sun.COM 
194712244SYu.Wu@Sun.COM static int
mrsas_complete_pending_cmds(struct mrsas_instance * instance)194812244SYu.Wu@Sun.COM mrsas_complete_pending_cmds(struct mrsas_instance *instance)
194912244SYu.Wu@Sun.COM {
195012244SYu.Wu@Sun.COM 
195112244SYu.Wu@Sun.COM 	struct mrsas_cmd *cmd = NULL;
195212244SYu.Wu@Sun.COM 	struct scsi_pkt *pkt;
195312244SYu.Wu@Sun.COM 	struct mrsas_header *hdr;
195412244SYu.Wu@Sun.COM 
195512244SYu.Wu@Sun.COM 	struct mlist_head		*pos, *next;
195612244SYu.Wu@Sun.COM 
195712244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
195812244SYu.Wu@Sun.COM 	    "mrsas_complete_pending_cmds(): Called"));
195912244SYu.Wu@Sun.COM 
196012244SYu.Wu@Sun.COM 	mutex_enter(&instance->cmd_pend_mtx);
196112244SYu.Wu@Sun.COM 	mlist_for_each_safe(pos, next, &instance->cmd_pend_list) {
196212244SYu.Wu@Sun.COM 		cmd = mlist_entry(pos, struct mrsas_cmd, list);
196312244SYu.Wu@Sun.COM 		if (cmd) {
196412244SYu.Wu@Sun.COM 			pkt = cmd->pkt;
1965*12757SYu.Wu@Sun.COM 			if (pkt) { /* for IO */
196612244SYu.Wu@Sun.COM 				if (((pkt->pkt_flags & FLAG_NOINTR)
196712244SYu.Wu@Sun.COM 				    == 0) && pkt->pkt_comp) {
196812244SYu.Wu@Sun.COM 					pkt->pkt_reason
196912244SYu.Wu@Sun.COM 					    = CMD_DEV_GONE;
197012244SYu.Wu@Sun.COM 					pkt->pkt_statistics
197112244SYu.Wu@Sun.COM 					    = STAT_DISCON;
197212244SYu.Wu@Sun.COM 					con_log(CL_ANN1, (CE_NOTE,
197312244SYu.Wu@Sun.COM 					    "fail and posting to scsa "
197412244SYu.Wu@Sun.COM 					    "cmd %p index %x"
197512244SYu.Wu@Sun.COM 					    " pkt %p "
197612244SYu.Wu@Sun.COM 					    "time : %llx",
197712244SYu.Wu@Sun.COM 					    (void *)cmd, cmd->index,
197812244SYu.Wu@Sun.COM 					    (void *)pkt, gethrtime()));
197912244SYu.Wu@Sun.COM 					(*pkt->pkt_comp)(pkt);
198012244SYu.Wu@Sun.COM 				}
198112244SYu.Wu@Sun.COM 			} else { /* for DCMDS */
198212244SYu.Wu@Sun.COM 				if (cmd->sync_cmd == MRSAS_TRUE) {
198312244SYu.Wu@Sun.COM 				hdr = (struct mrsas_header *)&cmd->frame->hdr;
198412244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_NOTE,
198512244SYu.Wu@Sun.COM 				    "posting invalid status to application "
198612244SYu.Wu@Sun.COM 				    "cmd %p index %x"
198712244SYu.Wu@Sun.COM 				    " hdr %p "
198812244SYu.Wu@Sun.COM 				    "time : %llx",
198912244SYu.Wu@Sun.COM 				    (void *)cmd, cmd->index,
199012244SYu.Wu@Sun.COM 				    (void *)hdr, gethrtime()));
199112244SYu.Wu@Sun.COM 				hdr->cmd_status = MFI_STAT_INVALID_STATUS;
199212244SYu.Wu@Sun.COM 				complete_cmd_in_sync_mode(instance, cmd);
199312244SYu.Wu@Sun.COM 				}
199412244SYu.Wu@Sun.COM 			}
199512244SYu.Wu@Sun.COM 			mlist_del_init(&cmd->list);
199612244SYu.Wu@Sun.COM 		} else {
199712244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
199812244SYu.Wu@Sun.COM 			    "mrsas_complete_pending_cmds:"
199912244SYu.Wu@Sun.COM 			    "NULL command\n"));
200012244SYu.Wu@Sun.COM 		}
200112244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
200212244SYu.Wu@Sun.COM 		    "mrsas_complete_pending_cmds:"
200312244SYu.Wu@Sun.COM 		    "looping for more commands\n"));
200412244SYu.Wu@Sun.COM 	}
200512244SYu.Wu@Sun.COM 	mutex_exit(&instance->cmd_pend_mtx);
200612244SYu.Wu@Sun.COM 
200712244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_complete_pending_cmds(): DONE\n"));
200812244SYu.Wu@Sun.COM 	return (DDI_SUCCESS);
200912244SYu.Wu@Sun.COM }
201012244SYu.Wu@Sun.COM 
201112244SYu.Wu@Sun.COM 
201212244SYu.Wu@Sun.COM static int
mrsas_issue_pending_cmds(struct mrsas_instance * instance)201312244SYu.Wu@Sun.COM mrsas_issue_pending_cmds(struct mrsas_instance *instance)
201412244SYu.Wu@Sun.COM {
201512244SYu.Wu@Sun.COM 	mlist_t *head	=	&instance->cmd_pend_list;
201612244SYu.Wu@Sun.COM 	mlist_t *tmp	=	head->next;
201712244SYu.Wu@Sun.COM 	struct mrsas_cmd *cmd = NULL;
201812244SYu.Wu@Sun.COM 	struct scsi_pkt *pkt;
201912244SYu.Wu@Sun.COM 
202012244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_pending_cmds(): Called"));
202112244SYu.Wu@Sun.COM 	while (tmp != head) {
202212244SYu.Wu@Sun.COM 		mutex_enter(&instance->cmd_pend_mtx);
202312244SYu.Wu@Sun.COM 		cmd = mlist_entry(tmp, struct mrsas_cmd, list);
202412244SYu.Wu@Sun.COM 		tmp = tmp->next;
202512244SYu.Wu@Sun.COM 		mutex_exit(&instance->cmd_pend_mtx);
202612244SYu.Wu@Sun.COM 		if (cmd) {
202712244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
202812244SYu.Wu@Sun.COM 			    "mrsas_issue_pending_cmds(): "
202912244SYu.Wu@Sun.COM 			    "Got a cmd: cmd:%p\n", (void *)cmd));
203012244SYu.Wu@Sun.COM 			cmd->retry_count_for_ocr++;
203112244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
203212244SYu.Wu@Sun.COM 			    "mrsas_issue_pending_cmds(): "
203312244SYu.Wu@Sun.COM 			    "cmd retry count = %d\n",
203412244SYu.Wu@Sun.COM 			    cmd->retry_count_for_ocr));
203512244SYu.Wu@Sun.COM 			if (cmd->retry_count_for_ocr > IO_RETRY_COUNT) {
203612244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_NOTE,
203712244SYu.Wu@Sun.COM 				    "mrsas_issue_pending_cmds():"
203812244SYu.Wu@Sun.COM 				    "Calling Kill Adapter\n"));
203912244SYu.Wu@Sun.COM 				(void) mrsas_kill_adapter(instance);
204012244SYu.Wu@Sun.COM 				return (DDI_FAILURE);
204112244SYu.Wu@Sun.COM 			}
204212244SYu.Wu@Sun.COM 			pkt = cmd->pkt;
204312244SYu.Wu@Sun.COM 			if (pkt) {
204412244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_NOTE,
204512244SYu.Wu@Sun.COM 				    "PENDING ISSUE: cmd %p index %x "
204612244SYu.Wu@Sun.COM 				    "pkt %p time %llx",
204712244SYu.Wu@Sun.COM 				    (void *)cmd, cmd->index,
204812244SYu.Wu@Sun.COM 				    (void *)pkt,
204912244SYu.Wu@Sun.COM 				    gethrtime()));
205012244SYu.Wu@Sun.COM 
205112244SYu.Wu@Sun.COM 			}
205212244SYu.Wu@Sun.COM 			if (cmd->sync_cmd == MRSAS_TRUE) {
205312244SYu.Wu@Sun.COM 				instance->func_ptr->issue_cmd_in_sync_mode(
205412244SYu.Wu@Sun.COM 				    instance, cmd);
205512244SYu.Wu@Sun.COM 			} else {
205612244SYu.Wu@Sun.COM 				instance->func_ptr->issue_cmd(cmd, instance);
205712244SYu.Wu@Sun.COM 			}
205812244SYu.Wu@Sun.COM 		} else {
205912244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
206012244SYu.Wu@Sun.COM 			    "mrsas_issue_pending_cmds: NULL command\n"));
206112244SYu.Wu@Sun.COM 		}
206212244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
206312244SYu.Wu@Sun.COM 		    "mrsas_issue_pending_cmds:"
206412244SYu.Wu@Sun.COM 		    "looping for more commands"));
206512244SYu.Wu@Sun.COM 	}
206612244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_pending_cmds(): DONE\n"));
206712244SYu.Wu@Sun.COM 	return (DDI_SUCCESS);
206812244SYu.Wu@Sun.COM }
206912244SYu.Wu@Sun.COM 
20709440SSusan.Scheufele@Sun.COM /*
20719440SSusan.Scheufele@Sun.COM  * destroy_mfi_frame_pool
20729440SSusan.Scheufele@Sun.COM  */
20739440SSusan.Scheufele@Sun.COM static void
destroy_mfi_frame_pool(struct mrsas_instance * instance)20749440SSusan.Scheufele@Sun.COM destroy_mfi_frame_pool(struct mrsas_instance *instance)
20759440SSusan.Scheufele@Sun.COM {
20769440SSusan.Scheufele@Sun.COM 	int		i;
20779440SSusan.Scheufele@Sun.COM 	uint32_t	max_cmd = instance->max_fw_cmds;
20789440SSusan.Scheufele@Sun.COM 
20799440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
20809440SSusan.Scheufele@Sun.COM 
20819440SSusan.Scheufele@Sun.COM 	/* return all frames to pool */
20829440SSusan.Scheufele@Sun.COM 	for (i = 0; i < max_cmd+1; i++) {
20839440SSusan.Scheufele@Sun.COM 
20849440SSusan.Scheufele@Sun.COM 		cmd = instance->cmd_list[i];
20859440SSusan.Scheufele@Sun.COM 
20869440SSusan.Scheufele@Sun.COM 		if (cmd->frame_dma_obj_status == DMA_OBJ_ALLOCATED)
20879440SSusan.Scheufele@Sun.COM 			(void) mrsas_free_dma_obj(instance, cmd->frame_dma_obj);
20889440SSusan.Scheufele@Sun.COM 
20899440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj_status  = DMA_OBJ_FREED;
20909440SSusan.Scheufele@Sun.COM 	}
20919440SSusan.Scheufele@Sun.COM 
20929440SSusan.Scheufele@Sun.COM }
20939440SSusan.Scheufele@Sun.COM 
20949440SSusan.Scheufele@Sun.COM /*
20959440SSusan.Scheufele@Sun.COM  * create_mfi_frame_pool
20969440SSusan.Scheufele@Sun.COM  */
20979440SSusan.Scheufele@Sun.COM static int
create_mfi_frame_pool(struct mrsas_instance * instance)20989440SSusan.Scheufele@Sun.COM create_mfi_frame_pool(struct mrsas_instance *instance)
20999440SSusan.Scheufele@Sun.COM {
21009440SSusan.Scheufele@Sun.COM 	int		i = 0;
21019440SSusan.Scheufele@Sun.COM 	int		cookie_cnt;
21029440SSusan.Scheufele@Sun.COM 	uint16_t	max_cmd;
21039440SSusan.Scheufele@Sun.COM 	uint16_t	sge_sz;
21049440SSusan.Scheufele@Sun.COM 	uint32_t	sgl_sz;
21059440SSusan.Scheufele@Sun.COM 	uint32_t	tot_frame_size;
21069440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
21079440SSusan.Scheufele@Sun.COM 
21089440SSusan.Scheufele@Sun.COM 	max_cmd = instance->max_fw_cmds;
21099440SSusan.Scheufele@Sun.COM 
211011856SYu.Wu@Sun.COM 	sge_sz	= sizeof (struct mrsas_sge_ieee);
21119440SSusan.Scheufele@Sun.COM 
21129440SSusan.Scheufele@Sun.COM 	/* calculated the number of 64byte frames required for SGL */
21139440SSusan.Scheufele@Sun.COM 	sgl_sz		= sge_sz * instance->max_num_sge;
21149440SSusan.Scheufele@Sun.COM 	tot_frame_size	= sgl_sz + MRMFI_FRAME_SIZE + SENSE_LENGTH;
21159440SSusan.Scheufele@Sun.COM 
21169440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL3, (CE_NOTE, "create_mfi_frame_pool: "
21179440SSusan.Scheufele@Sun.COM 	    "sgl_sz %x tot_frame_size %x", sgl_sz, tot_frame_size));
21189440SSusan.Scheufele@Sun.COM 
21199440SSusan.Scheufele@Sun.COM 	while (i < max_cmd+1) {
21209440SSusan.Scheufele@Sun.COM 		cmd = instance->cmd_list[i];
21219440SSusan.Scheufele@Sun.COM 
21229440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj.size	= tot_frame_size;
21239440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj.dma_attr = mrsas_generic_dma_attr;
21249440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
21259440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
21269440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj.dma_attr.dma_attr_sgllen = 1;
21279440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj.dma_attr.dma_attr_align = 64;
21289440SSusan.Scheufele@Sun.COM 
21299440SSusan.Scheufele@Sun.COM 
21309440SSusan.Scheufele@Sun.COM 		cookie_cnt = mrsas_alloc_dma_obj(instance, &cmd->frame_dma_obj,
21319440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC);
21329440SSusan.Scheufele@Sun.COM 
21339440SSusan.Scheufele@Sun.COM 		if (cookie_cnt == -1 || cookie_cnt > 1) {
21349440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN,
21359440SSusan.Scheufele@Sun.COM 			    "create_mfi_frame_pool: could not alloc."));
21369440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
21379440SSusan.Scheufele@Sun.COM 		}
21389440SSusan.Scheufele@Sun.COM 
21399440SSusan.Scheufele@Sun.COM 		bzero(cmd->frame_dma_obj.buffer, tot_frame_size);
21409440SSusan.Scheufele@Sun.COM 
21419440SSusan.Scheufele@Sun.COM 		cmd->frame_dma_obj_status = DMA_OBJ_ALLOCATED;
21429440SSusan.Scheufele@Sun.COM 		cmd->frame = (union mrsas_frame *)cmd->frame_dma_obj.buffer;
21439440SSusan.Scheufele@Sun.COM 		cmd->frame_phys_addr =
21449440SSusan.Scheufele@Sun.COM 		    cmd->frame_dma_obj.dma_cookie[0].dmac_address;
21459440SSusan.Scheufele@Sun.COM 
21469440SSusan.Scheufele@Sun.COM 		cmd->sense = (uint8_t *)(((unsigned long)
21479440SSusan.Scheufele@Sun.COM 		    cmd->frame_dma_obj.buffer) +
21489440SSusan.Scheufele@Sun.COM 		    tot_frame_size - SENSE_LENGTH);
21499440SSusan.Scheufele@Sun.COM 		cmd->sense_phys_addr =
21509440SSusan.Scheufele@Sun.COM 		    cmd->frame_dma_obj.dma_cookie[0].dmac_address +
21519440SSusan.Scheufele@Sun.COM 		    tot_frame_size - SENSE_LENGTH;
21529440SSusan.Scheufele@Sun.COM 
21539440SSusan.Scheufele@Sun.COM 		if (!cmd->frame || !cmd->sense) {
21549440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_NOTE,
21559440SSusan.Scheufele@Sun.COM 			    "mr_sas: pci_pool_alloc failed"));
21569440SSusan.Scheufele@Sun.COM 
21579440SSusan.Scheufele@Sun.COM 			return (ENOMEM);
21589440SSusan.Scheufele@Sun.COM 		}
21599440SSusan.Scheufele@Sun.COM 
21609440SSusan.Scheufele@Sun.COM 		ddi_put32(cmd->frame_dma_obj.acc_handle,
21619440SSusan.Scheufele@Sun.COM 		    &cmd->frame->io.context, cmd->index);
21629440SSusan.Scheufele@Sun.COM 		i++;
21639440SSusan.Scheufele@Sun.COM 
21649440SSusan.Scheufele@Sun.COM 		con_log(CL_DLEVEL3, (CE_NOTE, "[%x]-%x",
21659440SSusan.Scheufele@Sun.COM 		    cmd->index, cmd->frame_phys_addr));
21669440SSusan.Scheufele@Sun.COM 	}
21679440SSusan.Scheufele@Sun.COM 
21689440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
21699440SSusan.Scheufele@Sun.COM }
21709440SSusan.Scheufele@Sun.COM 
21719440SSusan.Scheufele@Sun.COM /*
21729440SSusan.Scheufele@Sun.COM  * free_additional_dma_buffer
21739440SSusan.Scheufele@Sun.COM  */
21749440SSusan.Scheufele@Sun.COM static void
free_additional_dma_buffer(struct mrsas_instance * instance)21759440SSusan.Scheufele@Sun.COM free_additional_dma_buffer(struct mrsas_instance *instance)
21769440SSusan.Scheufele@Sun.COM {
21779440SSusan.Scheufele@Sun.COM 	if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) {
21789440SSusan.Scheufele@Sun.COM 		(void) mrsas_free_dma_obj(instance,
21799440SSusan.Scheufele@Sun.COM 		    instance->mfi_internal_dma_obj);
21809440SSusan.Scheufele@Sun.COM 		instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED;
21819440SSusan.Scheufele@Sun.COM 	}
21829440SSusan.Scheufele@Sun.COM 
21839440SSusan.Scheufele@Sun.COM 	if (instance->mfi_evt_detail_obj.status == DMA_OBJ_ALLOCATED) {
21849440SSusan.Scheufele@Sun.COM 		(void) mrsas_free_dma_obj(instance,
21859440SSusan.Scheufele@Sun.COM 		    instance->mfi_evt_detail_obj);
21869440SSusan.Scheufele@Sun.COM 		instance->mfi_evt_detail_obj.status = DMA_OBJ_FREED;
21879440SSusan.Scheufele@Sun.COM 	}
21889440SSusan.Scheufele@Sun.COM }
21899440SSusan.Scheufele@Sun.COM 
21909440SSusan.Scheufele@Sun.COM /*
21919440SSusan.Scheufele@Sun.COM  * alloc_additional_dma_buffer
21929440SSusan.Scheufele@Sun.COM  */
21939440SSusan.Scheufele@Sun.COM static int
alloc_additional_dma_buffer(struct mrsas_instance * instance)21949440SSusan.Scheufele@Sun.COM alloc_additional_dma_buffer(struct mrsas_instance *instance)
21959440SSusan.Scheufele@Sun.COM {
21969440SSusan.Scheufele@Sun.COM 	uint32_t	reply_q_sz;
21979440SSusan.Scheufele@Sun.COM 	uint32_t	internal_buf_size = PAGESIZE*2;
21989440SSusan.Scheufele@Sun.COM 
21999440SSusan.Scheufele@Sun.COM 	/* max cmds plus 1 + producer & consumer */
22009440SSusan.Scheufele@Sun.COM 	reply_q_sz = sizeof (uint32_t) * (instance->max_fw_cmds + 1 + 2);
22019440SSusan.Scheufele@Sun.COM 
22029440SSusan.Scheufele@Sun.COM 	instance->mfi_internal_dma_obj.size = internal_buf_size;
22039440SSusan.Scheufele@Sun.COM 	instance->mfi_internal_dma_obj.dma_attr	= mrsas_generic_dma_attr;
22049440SSusan.Scheufele@Sun.COM 	instance->mfi_internal_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
22059440SSusan.Scheufele@Sun.COM 	instance->mfi_internal_dma_obj.dma_attr.dma_attr_count_max =
22069440SSusan.Scheufele@Sun.COM 	    0xFFFFFFFFU;
22079440SSusan.Scheufele@Sun.COM 	instance->mfi_internal_dma_obj.dma_attr.dma_attr_sgllen	= 1;
22089440SSusan.Scheufele@Sun.COM 
22099440SSusan.Scheufele@Sun.COM 	if (mrsas_alloc_dma_obj(instance, &instance->mfi_internal_dma_obj,
22109440SSusan.Scheufele@Sun.COM 	    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
22119440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
22129440SSusan.Scheufele@Sun.COM 		    "mr_sas: could not alloc reply queue"));
22139440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
22149440SSusan.Scheufele@Sun.COM 	}
22159440SSusan.Scheufele@Sun.COM 
22169440SSusan.Scheufele@Sun.COM 	bzero(instance->mfi_internal_dma_obj.buffer, internal_buf_size);
22179440SSusan.Scheufele@Sun.COM 
22189440SSusan.Scheufele@Sun.COM 	instance->mfi_internal_dma_obj.status |= DMA_OBJ_ALLOCATED;
22199440SSusan.Scheufele@Sun.COM 
22209440SSusan.Scheufele@Sun.COM 	instance->producer = (uint32_t *)((unsigned long)
22219440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.buffer);
22229440SSusan.Scheufele@Sun.COM 	instance->consumer = (uint32_t *)((unsigned long)
22239440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.buffer + 4);
22249440SSusan.Scheufele@Sun.COM 	instance->reply_queue = (uint32_t *)((unsigned long)
22259440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.buffer + 8);
22269440SSusan.Scheufele@Sun.COM 	instance->internal_buf = (caddr_t)(((unsigned long)
22279440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.buffer) + reply_q_sz + 8);
22289440SSusan.Scheufele@Sun.COM 	instance->internal_buf_dmac_add =
22299440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address +
22309440SSusan.Scheufele@Sun.COM 	    (reply_q_sz + 8);
22319440SSusan.Scheufele@Sun.COM 	instance->internal_buf_size = internal_buf_size -
22329440SSusan.Scheufele@Sun.COM 	    (reply_q_sz + 8);
22339440SSusan.Scheufele@Sun.COM 
22349440SSusan.Scheufele@Sun.COM 	/* allocate evt_detail */
22359440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.size = sizeof (struct mrsas_evt_detail);
22369440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.dma_attr = mrsas_generic_dma_attr;
22379440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
22389440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
22399440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.dma_attr.dma_attr_sgllen = 1;
22409440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.dma_attr.dma_attr_align = 1;
22419440SSusan.Scheufele@Sun.COM 
22429440SSusan.Scheufele@Sun.COM 	if (mrsas_alloc_dma_obj(instance, &instance->mfi_evt_detail_obj,
22439440SSusan.Scheufele@Sun.COM 	    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
22449440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "alloc_additional_dma_buffer: "
22459440SSusan.Scheufele@Sun.COM 		    "could not allocate data transfer buffer."));
22469440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
22479440SSusan.Scheufele@Sun.COM 	}
22489440SSusan.Scheufele@Sun.COM 
22499440SSusan.Scheufele@Sun.COM 	bzero(instance->mfi_evt_detail_obj.buffer,
22509440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_detail));
22519440SSusan.Scheufele@Sun.COM 
22529440SSusan.Scheufele@Sun.COM 	instance->mfi_evt_detail_obj.status |= DMA_OBJ_ALLOCATED;
22539440SSusan.Scheufele@Sun.COM 
22549440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
22559440SSusan.Scheufele@Sun.COM }
22569440SSusan.Scheufele@Sun.COM 
22579440SSusan.Scheufele@Sun.COM /*
22589440SSusan.Scheufele@Sun.COM  * free_space_for_mfi
22599440SSusan.Scheufele@Sun.COM  */
22609440SSusan.Scheufele@Sun.COM static void
free_space_for_mfi(struct mrsas_instance * instance)22619440SSusan.Scheufele@Sun.COM free_space_for_mfi(struct mrsas_instance *instance)
22629440SSusan.Scheufele@Sun.COM {
22639440SSusan.Scheufele@Sun.COM 	int		i;
22649440SSusan.Scheufele@Sun.COM 	uint32_t	max_cmd = instance->max_fw_cmds;
22659440SSusan.Scheufele@Sun.COM 
22669440SSusan.Scheufele@Sun.COM 	/* already freed */
22679440SSusan.Scheufele@Sun.COM 	if (instance->cmd_list == NULL) {
22689440SSusan.Scheufele@Sun.COM 		return;
22699440SSusan.Scheufele@Sun.COM 	}
22709440SSusan.Scheufele@Sun.COM 
22719440SSusan.Scheufele@Sun.COM 	free_additional_dma_buffer(instance);
22729440SSusan.Scheufele@Sun.COM 
22739440SSusan.Scheufele@Sun.COM 	/* first free the MFI frame pool */
22749440SSusan.Scheufele@Sun.COM 	destroy_mfi_frame_pool(instance);
22759440SSusan.Scheufele@Sun.COM 
22769440SSusan.Scheufele@Sun.COM 	/* free all the commands in the cmd_list */
22779440SSusan.Scheufele@Sun.COM 	for (i = 0; i < instance->max_fw_cmds+1; i++) {
22789440SSusan.Scheufele@Sun.COM 		kmem_free(instance->cmd_list[i],
22799440SSusan.Scheufele@Sun.COM 		    sizeof (struct mrsas_cmd));
22809440SSusan.Scheufele@Sun.COM 
22819440SSusan.Scheufele@Sun.COM 		instance->cmd_list[i] = NULL;
22829440SSusan.Scheufele@Sun.COM 	}
22839440SSusan.Scheufele@Sun.COM 
22849440SSusan.Scheufele@Sun.COM 	/* free the cmd_list buffer itself */
22859440SSusan.Scheufele@Sun.COM 	kmem_free(instance->cmd_list,
22869440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_cmd *) * (max_cmd+1));
22879440SSusan.Scheufele@Sun.COM 
22889440SSusan.Scheufele@Sun.COM 	instance->cmd_list = NULL;
22899440SSusan.Scheufele@Sun.COM 
22909440SSusan.Scheufele@Sun.COM 	INIT_LIST_HEAD(&instance->cmd_pool_list);
229112244SYu.Wu@Sun.COM 	INIT_LIST_HEAD(&instance->app_cmd_pool_list);
229212244SYu.Wu@Sun.COM 	INIT_LIST_HEAD(&instance->cmd_pend_list);
22939440SSusan.Scheufele@Sun.COM }
22949440SSusan.Scheufele@Sun.COM 
22959440SSusan.Scheufele@Sun.COM /*
22969440SSusan.Scheufele@Sun.COM  * alloc_space_for_mfi
22979440SSusan.Scheufele@Sun.COM  */
22989440SSusan.Scheufele@Sun.COM static int
alloc_space_for_mfi(struct mrsas_instance * instance)22999440SSusan.Scheufele@Sun.COM alloc_space_for_mfi(struct mrsas_instance *instance)
23009440SSusan.Scheufele@Sun.COM {
23019440SSusan.Scheufele@Sun.COM 	int		i;
23029440SSusan.Scheufele@Sun.COM 	uint32_t	max_cmd;
230312244SYu.Wu@Sun.COM 	uint32_t	reserve_cmd;
23049440SSusan.Scheufele@Sun.COM 	size_t		sz;
23059440SSusan.Scheufele@Sun.COM 
23069440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
23079440SSusan.Scheufele@Sun.COM 
23089440SSusan.Scheufele@Sun.COM 	max_cmd = instance->max_fw_cmds;
23099440SSusan.Scheufele@Sun.COM 
23109440SSusan.Scheufele@Sun.COM 	/* reserve 1 more slot for flush_cache */
23119440SSusan.Scheufele@Sun.COM 	sz = sizeof (struct mrsas_cmd *) * (max_cmd+1);
23129440SSusan.Scheufele@Sun.COM 
23139440SSusan.Scheufele@Sun.COM 	/*
23149440SSusan.Scheufele@Sun.COM 	 * instance->cmd_list is an array of struct mrsas_cmd pointers.
23159440SSusan.Scheufele@Sun.COM 	 * Allocate the dynamic array first and then allocate individual
23169440SSusan.Scheufele@Sun.COM 	 * commands.
23179440SSusan.Scheufele@Sun.COM 	 */
23189440SSusan.Scheufele@Sun.COM 	instance->cmd_list = kmem_zalloc(sz, KM_SLEEP);
23199440SSusan.Scheufele@Sun.COM 	ASSERT(instance->cmd_list);
23209440SSusan.Scheufele@Sun.COM 
23219440SSusan.Scheufele@Sun.COM 	for (i = 0; i < max_cmd+1; i++) {
23229440SSusan.Scheufele@Sun.COM 		instance->cmd_list[i] = kmem_zalloc(sizeof (struct mrsas_cmd),
23239440SSusan.Scheufele@Sun.COM 		    KM_SLEEP);
23249440SSusan.Scheufele@Sun.COM 		ASSERT(instance->cmd_list[i]);
23259440SSusan.Scheufele@Sun.COM 	}
23269440SSusan.Scheufele@Sun.COM 
23279440SSusan.Scheufele@Sun.COM 	INIT_LIST_HEAD(&instance->cmd_pool_list);
232812244SYu.Wu@Sun.COM 	INIT_LIST_HEAD(&instance->cmd_pend_list);
23299440SSusan.Scheufele@Sun.COM 	/* add all the commands to command pool (instance->cmd_pool) */
233012244SYu.Wu@Sun.COM 	reserve_cmd	=	APP_RESERVE_CMDS;
233112244SYu.Wu@Sun.COM 	INIT_LIST_HEAD(&instance->app_cmd_pool_list);
233212356SYu.Wu@Sun.COM 	for (i = 0; i < reserve_cmd-1; i++) {
233312244SYu.Wu@Sun.COM 		cmd	= instance->cmd_list[i];
23349440SSusan.Scheufele@Sun.COM 		cmd->index	= i;
233512244SYu.Wu@Sun.COM 		mlist_add_tail(&cmd->list, &instance->app_cmd_pool_list);
233612244SYu.Wu@Sun.COM 	}
233712356SYu.Wu@Sun.COM 	/*
233812356SYu.Wu@Sun.COM 	 * reserve slot instance->cmd_list[APP_RESERVE_CMDS-1]
233912356SYu.Wu@Sun.COM 	 * for abort_aen_cmd
234012356SYu.Wu@Sun.COM 	 */
234112244SYu.Wu@Sun.COM 	for (i = reserve_cmd; i < max_cmd; i++) {
234212244SYu.Wu@Sun.COM 		cmd			= instance->cmd_list[i];
234312244SYu.Wu@Sun.COM 		cmd->index	= i;
23449440SSusan.Scheufele@Sun.COM 		mlist_add_tail(&cmd->list, &instance->cmd_pool_list);
23459440SSusan.Scheufele@Sun.COM 	}
23469440SSusan.Scheufele@Sun.COM 
23479440SSusan.Scheufele@Sun.COM 	/* single slot for flush_cache won't be added in command pool */
23489440SSusan.Scheufele@Sun.COM 	cmd		= instance->cmd_list[max_cmd];
23499440SSusan.Scheufele@Sun.COM 	cmd->index	= i;
23509440SSusan.Scheufele@Sun.COM 
23519440SSusan.Scheufele@Sun.COM 	/* create a frame pool and assign one frame to each cmd */
23529440SSusan.Scheufele@Sun.COM 	if (create_mfi_frame_pool(instance)) {
23539440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "error creating frame DMA pool"));
23549440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
23559440SSusan.Scheufele@Sun.COM 	}
23569440SSusan.Scheufele@Sun.COM 
23579440SSusan.Scheufele@Sun.COM 	/* create a frame pool and assign one frame to each cmd */
23589440SSusan.Scheufele@Sun.COM 	if (alloc_additional_dma_buffer(instance)) {
23599440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "error creating frame DMA pool"));
23609440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
23619440SSusan.Scheufele@Sun.COM 	}
23629440SSusan.Scheufele@Sun.COM 
23639440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
23649440SSusan.Scheufele@Sun.COM }
23659440SSusan.Scheufele@Sun.COM 
236612244SYu.Wu@Sun.COM 
23679440SSusan.Scheufele@Sun.COM /*
23689440SSusan.Scheufele@Sun.COM  * get_ctrl_info
23699440SSusan.Scheufele@Sun.COM  */
23709440SSusan.Scheufele@Sun.COM static int
get_ctrl_info(struct mrsas_instance * instance,struct mrsas_ctrl_info * ctrl_info)23719440SSusan.Scheufele@Sun.COM get_ctrl_info(struct mrsas_instance *instance,
23729440SSusan.Scheufele@Sun.COM     struct mrsas_ctrl_info *ctrl_info)
23739440SSusan.Scheufele@Sun.COM {
23749440SSusan.Scheufele@Sun.COM 	int	ret = 0;
23759440SSusan.Scheufele@Sun.COM 
23769440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd		*cmd;
23779440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame	*dcmd;
23789440SSusan.Scheufele@Sun.COM 	struct mrsas_ctrl_info	*ci;
23799440SSusan.Scheufele@Sun.COM 
23809440SSusan.Scheufele@Sun.COM 	cmd = get_mfi_pkt(instance);
23819440SSusan.Scheufele@Sun.COM 
23829440SSusan.Scheufele@Sun.COM 	if (!cmd) {
23839440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
23849440SSusan.Scheufele@Sun.COM 		    "Failed to get a cmd for ctrl info"));
238510820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(info_mfi_err, uint16_t, instance->fw_outstanding,
238610820SSusan.Scheufele@Sun.COM 		    uint16_t, instance->max_fw_cmds);
23879440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
23889440SSusan.Scheufele@Sun.COM 	}
238912244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
23909440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
23919440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
23929440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
23939440SSusan.Scheufele@Sun.COM 	    cmd->index);
23949440SSusan.Scheufele@Sun.COM 
23959440SSusan.Scheufele@Sun.COM 	dcmd = &cmd->frame->dcmd;
23969440SSusan.Scheufele@Sun.COM 
23979440SSusan.Scheufele@Sun.COM 	ci = (struct mrsas_ctrl_info *)instance->internal_buf;
23989440SSusan.Scheufele@Sun.COM 
23999440SSusan.Scheufele@Sun.COM 	if (!ci) {
24009440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
24019440SSusan.Scheufele@Sun.COM 		    "Failed to alloc mem for ctrl info"));
24029440SSusan.Scheufele@Sun.COM 		return_mfi_pkt(instance, cmd);
24039440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
24049440SSusan.Scheufele@Sun.COM 	}
24059440SSusan.Scheufele@Sun.COM 
24069440SSusan.Scheufele@Sun.COM 	(void) memset(ci, 0, sizeof (struct mrsas_ctrl_info));
24079440SSusan.Scheufele@Sun.COM 
24089440SSusan.Scheufele@Sun.COM 	/* for( i = 0; i < DCMD_MBOX_SZ; i++ ) dcmd->mbox.b[i] = 0; */
24099440SSusan.Scheufele@Sun.COM 	(void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
24109440SSusan.Scheufele@Sun.COM 
24119440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
24129440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status,
24139440SSusan.Scheufele@Sun.COM 	    MFI_CMD_STATUS_POLL_MODE);
24149440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
24159440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
24169440SSusan.Scheufele@Sun.COM 	    MFI_FRAME_DIR_READ);
24179440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
24189440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
24199440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_ctrl_info));
24209440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
24219440SSusan.Scheufele@Sun.COM 	    MR_DCMD_CTRL_GET_INFO);
24229440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
24239440SSusan.Scheufele@Sun.COM 	    instance->internal_buf_dmac_add);
24249440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
24259440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_ctrl_info));
24269440SSusan.Scheufele@Sun.COM 
24279440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
24289440SSusan.Scheufele@Sun.COM 
24299440SSusan.Scheufele@Sun.COM 	if (!instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
243012244SYu.Wu@Sun.COM 	ret = 0;
243112244SYu.Wu@Sun.COM 
243212244SYu.Wu@Sun.COM 	ctrl_info->max_request_size = ddi_get32(
243312244SYu.Wu@Sun.COM 	    cmd->frame_dma_obj.acc_handle, &ci->max_request_size);
243412244SYu.Wu@Sun.COM 
243512244SYu.Wu@Sun.COM 	ctrl_info->ld_present_count = ddi_get16(
243612244SYu.Wu@Sun.COM 	    cmd->frame_dma_obj.acc_handle, &ci->ld_present_count);
243712244SYu.Wu@Sun.COM 
2438*12757SYu.Wu@Sun.COM 	ctrl_info->properties.on_off_properties =
2439*12757SYu.Wu@Sun.COM 	    ddi_get32(cmd->frame_dma_obj.acc_handle,
2440*12757SYu.Wu@Sun.COM 	    &ci->properties.on_off_properties);
2441*12757SYu.Wu@Sun.COM 
244212244SYu.Wu@Sun.COM 	ddi_rep_get8(cmd->frame_dma_obj.acc_handle,
244312244SYu.Wu@Sun.COM 	    (uint8_t *)(ctrl_info->product_name),
244412244SYu.Wu@Sun.COM 	    (uint8_t *)(ci->product_name), 80 * sizeof (char),
244512244SYu.Wu@Sun.COM 	    DDI_DEV_AUTOINCR);
244612244SYu.Wu@Sun.COM 	/* should get more members of ci with ddi_get when needed */
24479440SSusan.Scheufele@Sun.COM 	} else {
24489440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "get_ctrl_info: Ctrl info failed"));
24499440SSusan.Scheufele@Sun.COM 		ret = -1;
24509440SSusan.Scheufele@Sun.COM 	}
24519440SSusan.Scheufele@Sun.COM 
24529440SSusan.Scheufele@Sun.COM 	if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
24539440SSusan.Scheufele@Sun.COM 		ret = -1;
24549440SSusan.Scheufele@Sun.COM 	}
245510820SSusan.Scheufele@Sun.COM 	return_mfi_pkt(instance, cmd);
24569440SSusan.Scheufele@Sun.COM 
24579440SSusan.Scheufele@Sun.COM 	return (ret);
24589440SSusan.Scheufele@Sun.COM }
24599440SSusan.Scheufele@Sun.COM 
24609440SSusan.Scheufele@Sun.COM /*
24619440SSusan.Scheufele@Sun.COM  * abort_aen_cmd
24629440SSusan.Scheufele@Sun.COM  */
24639440SSusan.Scheufele@Sun.COM static int
abort_aen_cmd(struct mrsas_instance * instance,struct mrsas_cmd * cmd_to_abort)24649440SSusan.Scheufele@Sun.COM abort_aen_cmd(struct mrsas_instance *instance,
24659440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd_to_abort)
24669440SSusan.Scheufele@Sun.COM {
24679440SSusan.Scheufele@Sun.COM 	int	ret = 0;
24689440SSusan.Scheufele@Sun.COM 
24699440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd		*cmd;
24709440SSusan.Scheufele@Sun.COM 	struct mrsas_abort_frame	*abort_fr;
24719440SSusan.Scheufele@Sun.COM 
247212356SYu.Wu@Sun.COM 	cmd = instance->cmd_list[APP_RESERVE_CMDS-1];
24739440SSusan.Scheufele@Sun.COM 
24749440SSusan.Scheufele@Sun.COM 	if (!cmd) {
247512356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
247612356SYu.Wu@Sun.COM 		    "abort_aen_cmd():Failed to get a cmd for abort_aen_cmd"));
247710820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(abort_mfi_err, uint16_t, instance->fw_outstanding,
247810820SSusan.Scheufele@Sun.COM 		    uint16_t, instance->max_fw_cmds);
24799440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
24809440SSusan.Scheufele@Sun.COM 	}
248112244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
24829440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
24839440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
24849440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
24859440SSusan.Scheufele@Sun.COM 	    cmd->index);
24869440SSusan.Scheufele@Sun.COM 
24879440SSusan.Scheufele@Sun.COM 	abort_fr = &cmd->frame->abort;
24889440SSusan.Scheufele@Sun.COM 
24899440SSusan.Scheufele@Sun.COM 	/* prepare and issue the abort frame */
24909440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle,
24919440SSusan.Scheufele@Sun.COM 	    &abort_fr->cmd, MFI_CMD_OP_ABORT);
24929440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &abort_fr->cmd_status,
24939440SSusan.Scheufele@Sun.COM 	    MFI_CMD_STATUS_SYNC_MODE);
24949440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &abort_fr->flags, 0);
24959440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &abort_fr->abort_context,
24969440SSusan.Scheufele@Sun.COM 	    cmd_to_abort->index);
24979440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
24989440SSusan.Scheufele@Sun.COM 	    &abort_fr->abort_mfi_phys_addr_lo, cmd_to_abort->frame_phys_addr);
24999440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
25009440SSusan.Scheufele@Sun.COM 	    &abort_fr->abort_mfi_phys_addr_hi, 0);
25019440SSusan.Scheufele@Sun.COM 
25029440SSusan.Scheufele@Sun.COM 	instance->aen_cmd->abort_aen = 1;
25039440SSusan.Scheufele@Sun.COM 
25049440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_TRUE;
25059440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
25069440SSusan.Scheufele@Sun.COM 
250712356SYu.Wu@Sun.COM 	if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
250812356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
250912356SYu.Wu@Sun.COM 		    "abort_aen_cmd: issue_cmd_in_poll_mode failed"));
25109440SSusan.Scheufele@Sun.COM 		ret = -1;
25119440SSusan.Scheufele@Sun.COM 	} else {
25129440SSusan.Scheufele@Sun.COM 		ret = 0;
25139440SSusan.Scheufele@Sun.COM 	}
25149440SSusan.Scheufele@Sun.COM 
25159440SSusan.Scheufele@Sun.COM 	instance->aen_cmd->abort_aen = 1;
25169440SSusan.Scheufele@Sun.COM 	instance->aen_cmd = 0;
25179440SSusan.Scheufele@Sun.COM 
251812356SYu.Wu@Sun.COM 	atomic_add_16(&instance->fw_outstanding, (-1));
25199440SSusan.Scheufele@Sun.COM 
25209440SSusan.Scheufele@Sun.COM 	return (ret);
25219440SSusan.Scheufele@Sun.COM }
25229440SSusan.Scheufele@Sun.COM 
252312244SYu.Wu@Sun.COM 
25249440SSusan.Scheufele@Sun.COM /*
25259440SSusan.Scheufele@Sun.COM  * init_mfi
25269440SSusan.Scheufele@Sun.COM  */
25279440SSusan.Scheufele@Sun.COM static int
init_mfi(struct mrsas_instance * instance)25289440SSusan.Scheufele@Sun.COM init_mfi(struct mrsas_instance *instance)
25299440SSusan.Scheufele@Sun.COM {
25309440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd		*cmd;
25319440SSusan.Scheufele@Sun.COM 	struct mrsas_ctrl_info		ctrl_info;
25329440SSusan.Scheufele@Sun.COM 	struct mrsas_init_frame		*init_frame;
25339440SSusan.Scheufele@Sun.COM 	struct mrsas_init_queue_info	*initq_info;
25349440SSusan.Scheufele@Sun.COM 
25359440SSusan.Scheufele@Sun.COM 	/* we expect the FW state to be READY */
25369440SSusan.Scheufele@Sun.COM 	if (mfi_state_transition_to_ready(instance)) {
25379440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas: F/W is not ready"));
25389440SSusan.Scheufele@Sun.COM 		goto fail_ready_state;
25399440SSusan.Scheufele@Sun.COM 	}
25409440SSusan.Scheufele@Sun.COM 
25419440SSusan.Scheufele@Sun.COM 	/* get various operational parameters from status register */
25429440SSusan.Scheufele@Sun.COM 	instance->max_num_sge =
25439440SSusan.Scheufele@Sun.COM 	    (instance->func_ptr->read_fw_status_reg(instance) &
25449440SSusan.Scheufele@Sun.COM 	    0xFF0000) >> 0x10;
25459440SSusan.Scheufele@Sun.COM 	/*
25469440SSusan.Scheufele@Sun.COM 	 * Reduce the max supported cmds by 1. This is to ensure that the
25479440SSusan.Scheufele@Sun.COM 	 * reply_q_sz (1 more than the max cmd that driver may send)
25489440SSusan.Scheufele@Sun.COM 	 * does not exceed max cmds that the FW can support
25499440SSusan.Scheufele@Sun.COM 	 */
25509440SSusan.Scheufele@Sun.COM 	instance->max_fw_cmds =
25519440SSusan.Scheufele@Sun.COM 	    instance->func_ptr->read_fw_status_reg(instance) & 0xFFFF;
25529440SSusan.Scheufele@Sun.COM 	instance->max_fw_cmds = instance->max_fw_cmds - 1;
25539440SSusan.Scheufele@Sun.COM 
25549440SSusan.Scheufele@Sun.COM 	instance->max_num_sge =
25559440SSusan.Scheufele@Sun.COM 	    (instance->max_num_sge > MRSAS_MAX_SGE_CNT) ?
25569440SSusan.Scheufele@Sun.COM 	    MRSAS_MAX_SGE_CNT : instance->max_num_sge;
25579440SSusan.Scheufele@Sun.COM 
25589440SSusan.Scheufele@Sun.COM 	/* create a pool of commands */
25599440SSusan.Scheufele@Sun.COM 	if (alloc_space_for_mfi(instance) != DDI_SUCCESS)
25609440SSusan.Scheufele@Sun.COM 		goto fail_alloc_fw_space;
25619440SSusan.Scheufele@Sun.COM 
25629440SSusan.Scheufele@Sun.COM 	/*
25639440SSusan.Scheufele@Sun.COM 	 * Prepare a init frame. Note the init frame points to queue info
25649440SSusan.Scheufele@Sun.COM 	 * structure. Each frame has SGL allocated after first 64 bytes. For
25659440SSusan.Scheufele@Sun.COM 	 * this frame - since we don't need any SGL - we use SGL's space as
25669440SSusan.Scheufele@Sun.COM 	 * queue info structure
25679440SSusan.Scheufele@Sun.COM 	 */
25689440SSusan.Scheufele@Sun.COM 	cmd = get_mfi_pkt(instance);
256912244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
257012244SYu.Wu@Sun.COM 
25719440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
25729440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
25739440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
25749440SSusan.Scheufele@Sun.COM 	    cmd->index);
25759440SSusan.Scheufele@Sun.COM 
25769440SSusan.Scheufele@Sun.COM 	init_frame = (struct mrsas_init_frame *)cmd->frame;
25779440SSusan.Scheufele@Sun.COM 	initq_info = (struct mrsas_init_queue_info *)
25789440SSusan.Scheufele@Sun.COM 	    ((unsigned long)init_frame + 64);
25799440SSusan.Scheufele@Sun.COM 
25809440SSusan.Scheufele@Sun.COM 	(void) memset(init_frame, 0, MRMFI_FRAME_SIZE);
25819440SSusan.Scheufele@Sun.COM 	(void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info));
25829440SSusan.Scheufele@Sun.COM 
25839440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0);
25849440SSusan.Scheufele@Sun.COM 
25859440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
25869440SSusan.Scheufele@Sun.COM 	    &initq_info->reply_queue_entries, instance->max_fw_cmds + 1);
25879440SSusan.Scheufele@Sun.COM 
25889440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
25899440SSusan.Scheufele@Sun.COM 	    &initq_info->producer_index_phys_addr_hi, 0);
25909440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
25919440SSusan.Scheufele@Sun.COM 	    &initq_info->producer_index_phys_addr_lo,
25929440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address);
25939440SSusan.Scheufele@Sun.COM 
25949440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
25959440SSusan.Scheufele@Sun.COM 	    &initq_info->consumer_index_phys_addr_hi, 0);
25969440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
25979440SSusan.Scheufele@Sun.COM 	    &initq_info->consumer_index_phys_addr_lo,
25989440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4);
25999440SSusan.Scheufele@Sun.COM 
26009440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
26019440SSusan.Scheufele@Sun.COM 	    &initq_info->reply_queue_start_phys_addr_hi, 0);
26029440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
26039440SSusan.Scheufele@Sun.COM 	    &initq_info->reply_queue_start_phys_addr_lo,
26049440SSusan.Scheufele@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8);
26059440SSusan.Scheufele@Sun.COM 
26069440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle,
26079440SSusan.Scheufele@Sun.COM 	    &init_frame->cmd, MFI_CMD_OP_INIT);
26089440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &init_frame->cmd_status,
26099440SSusan.Scheufele@Sun.COM 	    MFI_CMD_STATUS_POLL_MODE);
26109440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0);
26119440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
26129440SSusan.Scheufele@Sun.COM 	    &init_frame->queue_info_new_phys_addr_lo,
26139440SSusan.Scheufele@Sun.COM 	    cmd->frame_phys_addr + 64);
26149440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
26159440SSusan.Scheufele@Sun.COM 	    &init_frame->queue_info_new_phys_addr_hi, 0);
26169440SSusan.Scheufele@Sun.COM 
26179440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len,
26189440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_init_queue_info));
26199440SSusan.Scheufele@Sun.COM 
26209440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
26219440SSusan.Scheufele@Sun.COM 
26229440SSusan.Scheufele@Sun.COM 	/* issue the init frame in polled mode */
26239440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
26249440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "failed to init firmware"));
262510600SYu.Wu@Sun.COM 		return_mfi_pkt(instance, cmd);
262610600SYu.Wu@Sun.COM 		goto fail_fw_init;
262710600SYu.Wu@Sun.COM 	}
262810600SYu.Wu@Sun.COM 
262910600SYu.Wu@Sun.COM 	if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
263010600SYu.Wu@Sun.COM 		return_mfi_pkt(instance, cmd);
26319440SSusan.Scheufele@Sun.COM 		goto fail_fw_init;
26329440SSusan.Scheufele@Sun.COM 	}
26339440SSusan.Scheufele@Sun.COM 	return_mfi_pkt(instance, cmd);
26349440SSusan.Scheufele@Sun.COM 
263511856SYu.Wu@Sun.COM 	if (ctio_enable &&
263611856SYu.Wu@Sun.COM 	    (instance->func_ptr->read_fw_status_reg(instance) & 0x04000000)) {
263711856SYu.Wu@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "mr_sas: IEEE SGL's supported"));
263811856SYu.Wu@Sun.COM 		instance->flag_ieee = 1;
263911856SYu.Wu@Sun.COM 	} else {
264011856SYu.Wu@Sun.COM 		instance->flag_ieee = 0;
264111856SYu.Wu@Sun.COM 	}
264211856SYu.Wu@Sun.COM 
264312244SYu.Wu@Sun.COM 	instance->disable_online_ctrl_reset = 0;
26449440SSusan.Scheufele@Sun.COM 	/* gather misc FW related information */
26459440SSusan.Scheufele@Sun.COM 	if (!get_ctrl_info(instance, &ctrl_info)) {
26469440SSusan.Scheufele@Sun.COM 		instance->max_sectors_per_req = ctrl_info.max_request_size;
264712244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
264812244SYu.Wu@Sun.COM 		    "product name %s ld present %d",
26499440SSusan.Scheufele@Sun.COM 		    ctrl_info.product_name, ctrl_info.ld_present_count));
26509440SSusan.Scheufele@Sun.COM 	} else {
26519440SSusan.Scheufele@Sun.COM 		instance->max_sectors_per_req = instance->max_num_sge *
26529440SSusan.Scheufele@Sun.COM 		    PAGESIZE / 512;
26539440SSusan.Scheufele@Sun.COM 	}
2654*12757SYu.Wu@Sun.COM 
2655*12757SYu.Wu@Sun.COM 	if (ctrl_info.properties.on_off_properties & DISABLE_OCR_PROP_FLAG)
2656*12757SYu.Wu@Sun.COM 		instance->disable_online_ctrl_reset = 1;
2657*12757SYu.Wu@Sun.COM 
26589440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
26599440SSusan.Scheufele@Sun.COM 
26609440SSusan.Scheufele@Sun.COM fail_fw_init:
26619440SSusan.Scheufele@Sun.COM fail_alloc_fw_space:
26629440SSusan.Scheufele@Sun.COM 
26639440SSusan.Scheufele@Sun.COM 	free_space_for_mfi(instance);
26649440SSusan.Scheufele@Sun.COM 
26659440SSusan.Scheufele@Sun.COM fail_ready_state:
26669440SSusan.Scheufele@Sun.COM 	ddi_regs_map_free(&instance->regmap_handle);
26679440SSusan.Scheufele@Sun.COM 
26689440SSusan.Scheufele@Sun.COM fail_mfi_reg_setup:
26699440SSusan.Scheufele@Sun.COM 	return (DDI_FAILURE);
26709440SSusan.Scheufele@Sun.COM }
26719440SSusan.Scheufele@Sun.COM 
267212244SYu.Wu@Sun.COM 
267312244SYu.Wu@Sun.COM 
267412244SYu.Wu@Sun.COM 
267512244SYu.Wu@Sun.COM 
267612244SYu.Wu@Sun.COM 
267712244SYu.Wu@Sun.COM static int
mrsas_issue_init_mfi(struct mrsas_instance * instance)267812244SYu.Wu@Sun.COM mrsas_issue_init_mfi(struct mrsas_instance *instance)
267912244SYu.Wu@Sun.COM {
268012244SYu.Wu@Sun.COM 	struct mrsas_cmd		*cmd;
268112244SYu.Wu@Sun.COM 	struct mrsas_init_frame		*init_frame;
268212244SYu.Wu@Sun.COM 	struct mrsas_init_queue_info	*initq_info;
268312244SYu.Wu@Sun.COM 
268412244SYu.Wu@Sun.COM /*
268512244SYu.Wu@Sun.COM  * Prepare a init frame. Note the init frame points to queue info
268612244SYu.Wu@Sun.COM  * structure. Each frame has SGL allocated after first 64 bytes. For
268712244SYu.Wu@Sun.COM  * this frame - since we don't need any SGL - we use SGL's space as
268812244SYu.Wu@Sun.COM  * queue info structure
268912244SYu.Wu@Sun.COM  */
269012244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
269112244SYu.Wu@Sun.COM 	    "mrsas_issue_init_mfi: entry\n"));
269212244SYu.Wu@Sun.COM 	cmd = get_mfi_app_pkt(instance);
269312244SYu.Wu@Sun.COM 
269412244SYu.Wu@Sun.COM 	if (!cmd) {
269512244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
269612244SYu.Wu@Sun.COM 		    "mrsas_issue_init_mfi: get_pkt failed\n"));
269712244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
269812244SYu.Wu@Sun.COM 	}
269912244SYu.Wu@Sun.COM 
270012244SYu.Wu@Sun.COM 	/* Clear the frame buffer and assign back the context id */
270112244SYu.Wu@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
270212244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
270312244SYu.Wu@Sun.COM 	    cmd->index);
270412244SYu.Wu@Sun.COM 
270512244SYu.Wu@Sun.COM 	init_frame = (struct mrsas_init_frame *)cmd->frame;
270612244SYu.Wu@Sun.COM 	initq_info = (struct mrsas_init_queue_info *)
270712244SYu.Wu@Sun.COM 	    ((unsigned long)init_frame + 64);
270812244SYu.Wu@Sun.COM 
270912244SYu.Wu@Sun.COM 	(void) memset(init_frame, 0, MRMFI_FRAME_SIZE);
271012244SYu.Wu@Sun.COM 	(void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info));
271112244SYu.Wu@Sun.COM 
271212244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0);
271312244SYu.Wu@Sun.COM 
271412244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
271512244SYu.Wu@Sun.COM 	    &initq_info->reply_queue_entries, instance->max_fw_cmds + 1);
271612244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
271712244SYu.Wu@Sun.COM 	    &initq_info->producer_index_phys_addr_hi, 0);
271812244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
271912244SYu.Wu@Sun.COM 	    &initq_info->producer_index_phys_addr_lo,
272012244SYu.Wu@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address);
272112244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
272212244SYu.Wu@Sun.COM 	    &initq_info->consumer_index_phys_addr_hi, 0);
272312244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
272412244SYu.Wu@Sun.COM 	    &initq_info->consumer_index_phys_addr_lo,
272512244SYu.Wu@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4);
272612244SYu.Wu@Sun.COM 
272712244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
272812244SYu.Wu@Sun.COM 	    &initq_info->reply_queue_start_phys_addr_hi, 0);
272912244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
273012244SYu.Wu@Sun.COM 	    &initq_info->reply_queue_start_phys_addr_lo,
273112244SYu.Wu@Sun.COM 	    instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8);
273212244SYu.Wu@Sun.COM 
273312244SYu.Wu@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle,
273412244SYu.Wu@Sun.COM 	    &init_frame->cmd, MFI_CMD_OP_INIT);
273512244SYu.Wu@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &init_frame->cmd_status,
273612244SYu.Wu@Sun.COM 	    MFI_CMD_STATUS_POLL_MODE);
273712244SYu.Wu@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0);
273812244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
273912244SYu.Wu@Sun.COM 	    &init_frame->queue_info_new_phys_addr_lo,
274012244SYu.Wu@Sun.COM 	    cmd->frame_phys_addr + 64);
274112244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle,
274212244SYu.Wu@Sun.COM 	    &init_frame->queue_info_new_phys_addr_hi, 0);
274312244SYu.Wu@Sun.COM 
274412244SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len,
274512244SYu.Wu@Sun.COM 	    sizeof (struct mrsas_init_queue_info));
274612244SYu.Wu@Sun.COM 
274712244SYu.Wu@Sun.COM 	cmd->frame_count = 1;
274812244SYu.Wu@Sun.COM 
274912244SYu.Wu@Sun.COM 	/* issue the init frame in polled mode */
275012244SYu.Wu@Sun.COM 	if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
275112244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
275212244SYu.Wu@Sun.COM 		    "mrsas_issue_init_mfi():failed to "
275312244SYu.Wu@Sun.COM 		    "init firmware"));
275412244SYu.Wu@Sun.COM 		return_mfi_app_pkt(instance, cmd);
275512244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
275612244SYu.Wu@Sun.COM 	}
275712244SYu.Wu@Sun.COM 	return_mfi_app_pkt(instance, cmd);
275812244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_init_mfi: Done"));
275912244SYu.Wu@Sun.COM 	return (DDI_SUCCESS);
276012244SYu.Wu@Sun.COM }
27619440SSusan.Scheufele@Sun.COM /*
27629440SSusan.Scheufele@Sun.COM  * mfi_state_transition_to_ready	: Move the FW to READY state
27639440SSusan.Scheufele@Sun.COM  *
27649440SSusan.Scheufele@Sun.COM  * @reg_set			: MFI register set
27659440SSusan.Scheufele@Sun.COM  */
27669440SSusan.Scheufele@Sun.COM static int
mfi_state_transition_to_ready(struct mrsas_instance * instance)27679440SSusan.Scheufele@Sun.COM mfi_state_transition_to_ready(struct mrsas_instance *instance)
27689440SSusan.Scheufele@Sun.COM {
27699440SSusan.Scheufele@Sun.COM 	int		i;
27709440SSusan.Scheufele@Sun.COM 	uint8_t		max_wait;
27719440SSusan.Scheufele@Sun.COM 	uint32_t	fw_ctrl;
27729440SSusan.Scheufele@Sun.COM 	uint32_t	fw_state;
27739440SSusan.Scheufele@Sun.COM 	uint32_t	cur_state;
277412244SYu.Wu@Sun.COM 	uint32_t	cur_abs_reg_val;
277512244SYu.Wu@Sun.COM 	uint32_t	prev_abs_reg_val;
277612244SYu.Wu@Sun.COM 
277712244SYu.Wu@Sun.COM 	cur_abs_reg_val =
277812244SYu.Wu@Sun.COM 	    instance->func_ptr->read_fw_status_reg(instance);
27799440SSusan.Scheufele@Sun.COM 	fw_state =
278012244SYu.Wu@Sun.COM 	    cur_abs_reg_val & MFI_STATE_MASK;
27819440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
27829440SSusan.Scheufele@Sun.COM 	    "mfi_state_transition_to_ready:FW state = 0x%x", fw_state));
27839440SSusan.Scheufele@Sun.COM 
27849440SSusan.Scheufele@Sun.COM 	while (fw_state != MFI_STATE_READY) {
27859440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE,
27869440SSusan.Scheufele@Sun.COM 		    "mfi_state_transition_to_ready:FW state%x", fw_state));
27879440SSusan.Scheufele@Sun.COM 
27889440SSusan.Scheufele@Sun.COM 		switch (fw_state) {
27899440SSusan.Scheufele@Sun.COM 		case MFI_STATE_FAULT:
279012244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
27919440SSusan.Scheufele@Sun.COM 			    "mr_sas: FW in FAULT state!!"));
27929440SSusan.Scheufele@Sun.COM 
27939440SSusan.Scheufele@Sun.COM 			return (ENODEV);
27949440SSusan.Scheufele@Sun.COM 		case MFI_STATE_WAIT_HANDSHAKE:
27959440SSusan.Scheufele@Sun.COM 			/* set the CLR bit in IMR0 */
279612244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
27979440SSusan.Scheufele@Sun.COM 			    "mr_sas: FW waiting for HANDSHAKE"));
27989440SSusan.Scheufele@Sun.COM 			/*
27999440SSusan.Scheufele@Sun.COM 			 * PCI_Hot Plug: MFI F/W requires
28009440SSusan.Scheufele@Sun.COM 			 * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG)
28019440SSusan.Scheufele@Sun.COM 			 * to be set
28029440SSusan.Scheufele@Sun.COM 			 */
28039440SSusan.Scheufele@Sun.COM 			/* WR_IB_MSG_0(MFI_INIT_CLEAR_HANDSHAKE, instance); */
28049440SSusan.Scheufele@Sun.COM 			WR_IB_DOORBELL(MFI_INIT_CLEAR_HANDSHAKE |
28059440SSusan.Scheufele@Sun.COM 			    MFI_INIT_HOTPLUG, instance);
28069440SSusan.Scheufele@Sun.COM 
28079440SSusan.Scheufele@Sun.COM 			max_wait	= 2;
28089440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_WAIT_HANDSHAKE;
28099440SSusan.Scheufele@Sun.COM 			break;
28109440SSusan.Scheufele@Sun.COM 		case MFI_STATE_BOOT_MESSAGE_PENDING:
28119440SSusan.Scheufele@Sun.COM 			/* set the CLR bit in IMR0 */
281212244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
28139440SSusan.Scheufele@Sun.COM 			    "mr_sas: FW state boot message pending"));
28149440SSusan.Scheufele@Sun.COM 			/*
28159440SSusan.Scheufele@Sun.COM 			 * PCI_Hot Plug: MFI F/W requires
28169440SSusan.Scheufele@Sun.COM 			 * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG)
28179440SSusan.Scheufele@Sun.COM 			 * to be set
28189440SSusan.Scheufele@Sun.COM 			 */
28199440SSusan.Scheufele@Sun.COM 			WR_IB_DOORBELL(MFI_INIT_HOTPLUG, instance);
28209440SSusan.Scheufele@Sun.COM 
28219440SSusan.Scheufele@Sun.COM 			max_wait	= 10;
28229440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_BOOT_MESSAGE_PENDING;
28239440SSusan.Scheufele@Sun.COM 			break;
28249440SSusan.Scheufele@Sun.COM 		case MFI_STATE_OPERATIONAL:
28259440SSusan.Scheufele@Sun.COM 			/* bring it to READY state; assuming max wait 2 secs */
28269440SSusan.Scheufele@Sun.COM 			instance->func_ptr->disable_intr(instance);
28279440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
28289440SSusan.Scheufele@Sun.COM 			    "mr_sas: FW in OPERATIONAL state"));
28299440SSusan.Scheufele@Sun.COM 			/*
28309440SSusan.Scheufele@Sun.COM 			 * PCI_Hot Plug: MFI F/W requires
28319440SSusan.Scheufele@Sun.COM 			 * (MFI_INIT_READY | MFI_INIT_MFIMODE | MFI_INIT_ABORT)
28329440SSusan.Scheufele@Sun.COM 			 * to be set
28339440SSusan.Scheufele@Sun.COM 			 */
28349440SSusan.Scheufele@Sun.COM 			/* WR_IB_DOORBELL(MFI_INIT_READY, instance); */
28359440SSusan.Scheufele@Sun.COM 			WR_IB_DOORBELL(MFI_RESET_FLAGS, instance);
28369440SSusan.Scheufele@Sun.COM 
28379440SSusan.Scheufele@Sun.COM 			max_wait	= 10;
28389440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_OPERATIONAL;
28399440SSusan.Scheufele@Sun.COM 			break;
28409440SSusan.Scheufele@Sun.COM 		case MFI_STATE_UNDEFINED:
28419440SSusan.Scheufele@Sun.COM 			/* this state should not last for more than 2 seconds */
284212244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE, "FW state undefined"));
28439440SSusan.Scheufele@Sun.COM 
28449440SSusan.Scheufele@Sun.COM 			max_wait	= 2;
28459440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_UNDEFINED;
28469440SSusan.Scheufele@Sun.COM 			break;
28479440SSusan.Scheufele@Sun.COM 		case MFI_STATE_BB_INIT:
28489440SSusan.Scheufele@Sun.COM 			max_wait	= 2;
28499440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_BB_INIT;
28509440SSusan.Scheufele@Sun.COM 			break;
28519440SSusan.Scheufele@Sun.COM 		case MFI_STATE_FW_INIT:
28529440SSusan.Scheufele@Sun.COM 			max_wait	= 2;
28539440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_FW_INIT;
28549440SSusan.Scheufele@Sun.COM 			break;
28559440SSusan.Scheufele@Sun.COM 		case MFI_STATE_DEVICE_SCAN:
285612244SYu.Wu@Sun.COM 			max_wait	= 180;
28579440SSusan.Scheufele@Sun.COM 			cur_state	= MFI_STATE_DEVICE_SCAN;
285812244SYu.Wu@Sun.COM 			prev_abs_reg_val = cur_abs_reg_val;
285912244SYu.Wu@Sun.COM 			con_log(CL_NONE, (CE_NOTE,
286012244SYu.Wu@Sun.COM 			    "Device scan in progress ...\n"));
28619440SSusan.Scheufele@Sun.COM 			break;
28629440SSusan.Scheufele@Sun.COM 		default:
286312244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
28649440SSusan.Scheufele@Sun.COM 			    "mr_sas: Unknown state 0x%x", fw_state));
28659440SSusan.Scheufele@Sun.COM 			return (ENODEV);
28669440SSusan.Scheufele@Sun.COM 		}
28679440SSusan.Scheufele@Sun.COM 
28689440SSusan.Scheufele@Sun.COM 		/* the cur_state should not last for more than max_wait secs */
28699440SSusan.Scheufele@Sun.COM 		for (i = 0; i < (max_wait * MILLISEC); i++) {
28709440SSusan.Scheufele@Sun.COM 			/* fw_state = RD_OB_MSG_0(instance) & MFI_STATE_MASK; */
287112244SYu.Wu@Sun.COM 			cur_abs_reg_val =
287212244SYu.Wu@Sun.COM 			    instance->func_ptr->read_fw_status_reg(instance);
287312244SYu.Wu@Sun.COM 			fw_state = cur_abs_reg_val & MFI_STATE_MASK;
28749440SSusan.Scheufele@Sun.COM 
28759440SSusan.Scheufele@Sun.COM 			if (fw_state == cur_state) {
28769440SSusan.Scheufele@Sun.COM 				delay(1 * drv_usectohz(MILLISEC));
28779440SSusan.Scheufele@Sun.COM 			} else {
28789440SSusan.Scheufele@Sun.COM 				break;
28799440SSusan.Scheufele@Sun.COM 			}
28809440SSusan.Scheufele@Sun.COM 		}
288112244SYu.Wu@Sun.COM 		if (fw_state == MFI_STATE_DEVICE_SCAN) {
288212244SYu.Wu@Sun.COM 			if (prev_abs_reg_val != cur_abs_reg_val) {
288312244SYu.Wu@Sun.COM 				continue;
288412244SYu.Wu@Sun.COM 			}
288512244SYu.Wu@Sun.COM 		}
28869440SSusan.Scheufele@Sun.COM 
28879440SSusan.Scheufele@Sun.COM 		/* return error if fw_state hasn't changed after max_wait */
28889440SSusan.Scheufele@Sun.COM 		if (fw_state == cur_state) {
288912244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
28909440SSusan.Scheufele@Sun.COM 			    "FW state hasn't changed in %d secs", max_wait));
28919440SSusan.Scheufele@Sun.COM 			return (ENODEV);
28929440SSusan.Scheufele@Sun.COM 		}
28939440SSusan.Scheufele@Sun.COM 	};
28949440SSusan.Scheufele@Sun.COM 
28959440SSusan.Scheufele@Sun.COM 	fw_ctrl = RD_IB_DOORBELL(instance);
28969440SSusan.Scheufele@Sun.COM 
28979440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
28989440SSusan.Scheufele@Sun.COM 	    "mfi_state_transition_to_ready:FW ctrl = 0x%x", fw_ctrl));
28999440SSusan.Scheufele@Sun.COM 
29009440SSusan.Scheufele@Sun.COM 	/*
29019440SSusan.Scheufele@Sun.COM 	 * Write 0xF to the doorbell register to do the following.
29029440SSusan.Scheufele@Sun.COM 	 * - Abort all outstanding commands (bit 0).
29039440SSusan.Scheufele@Sun.COM 	 * - Transition from OPERATIONAL to READY state (bit 1).
29049440SSusan.Scheufele@Sun.COM 	 * - Discard (possible) low MFA posted in 64-bit mode (bit-2).
29059440SSusan.Scheufele@Sun.COM 	 * - Set to release FW to continue running (i.e. BIOS handshake
29069440SSusan.Scheufele@Sun.COM 	 *   (bit 3).
29079440SSusan.Scheufele@Sun.COM 	 */
29089440SSusan.Scheufele@Sun.COM 	WR_IB_DOORBELL(0xF, instance);
29099440SSusan.Scheufele@Sun.COM 
29109440SSusan.Scheufele@Sun.COM 	if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
29119440SSusan.Scheufele@Sun.COM 		return (ENODEV);
29129440SSusan.Scheufele@Sun.COM 	}
29139440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
29149440SSusan.Scheufele@Sun.COM }
29159440SSusan.Scheufele@Sun.COM 
29169440SSusan.Scheufele@Sun.COM /*
29179440SSusan.Scheufele@Sun.COM  * get_seq_num
29189440SSusan.Scheufele@Sun.COM  */
29199440SSusan.Scheufele@Sun.COM static int
get_seq_num(struct mrsas_instance * instance,struct mrsas_evt_log_info * eli)29209440SSusan.Scheufele@Sun.COM get_seq_num(struct mrsas_instance *instance,
29219440SSusan.Scheufele@Sun.COM     struct mrsas_evt_log_info *eli)
29229440SSusan.Scheufele@Sun.COM {
29239440SSusan.Scheufele@Sun.COM 	int	ret = DDI_SUCCESS;
29249440SSusan.Scheufele@Sun.COM 
29259440SSusan.Scheufele@Sun.COM 	dma_obj_t			dcmd_dma_obj;
29269440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd		*cmd;
29279440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame		*dcmd;
29289795SYu.Wu@Sun.COM 	struct mrsas_evt_log_info *eli_tmp;
29299440SSusan.Scheufele@Sun.COM 	cmd = get_mfi_pkt(instance);
29309440SSusan.Scheufele@Sun.COM 
29319440SSusan.Scheufele@Sun.COM 	if (!cmd) {
29329440SSusan.Scheufele@Sun.COM 		cmn_err(CE_WARN, "mr_sas: failed to get a cmd");
293310820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(seq_num_mfi_err, uint16_t,
293410820SSusan.Scheufele@Sun.COM 		    instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
29359440SSusan.Scheufele@Sun.COM 		return (ENOMEM);
29369440SSusan.Scheufele@Sun.COM 	}
293712244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
29389440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
29399440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
29409440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
29419440SSusan.Scheufele@Sun.COM 	    cmd->index);
29429440SSusan.Scheufele@Sun.COM 
29439440SSusan.Scheufele@Sun.COM 	dcmd	= &cmd->frame->dcmd;
29449440SSusan.Scheufele@Sun.COM 
29459440SSusan.Scheufele@Sun.COM 	/* allocate the data transfer buffer */
29469440SSusan.Scheufele@Sun.COM 	dcmd_dma_obj.size = sizeof (struct mrsas_evt_log_info);
29479440SSusan.Scheufele@Sun.COM 	dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr;
29489440SSusan.Scheufele@Sun.COM 	dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
29499440SSusan.Scheufele@Sun.COM 	dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
29509440SSusan.Scheufele@Sun.COM 	dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1;
29519440SSusan.Scheufele@Sun.COM 	dcmd_dma_obj.dma_attr.dma_attr_align = 1;
29529440SSusan.Scheufele@Sun.COM 
29539440SSusan.Scheufele@Sun.COM 	if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj,
29549440SSusan.Scheufele@Sun.COM 	    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
29559440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
29569440SSusan.Scheufele@Sun.COM 		    "get_seq_num: could not allocate data transfer buffer."));
29579440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
29589440SSusan.Scheufele@Sun.COM 	}
29599440SSusan.Scheufele@Sun.COM 
29609440SSusan.Scheufele@Sun.COM 	(void) memset(dcmd_dma_obj.buffer, 0,
29619440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_log_info));
29629440SSusan.Scheufele@Sun.COM 
29639440SSusan.Scheufele@Sun.COM 	(void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
29649440SSusan.Scheufele@Sun.COM 
29659440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
29669440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0);
29679440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
29689440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
29699440SSusan.Scheufele@Sun.COM 	    MFI_FRAME_DIR_READ);
29709440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
29719440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
29729440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_log_info));
29739440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
29749440SSusan.Scheufele@Sun.COM 	    MR_DCMD_CTRL_EVENT_GET_INFO);
29759440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
29769440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_log_info));
29779440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
29789440SSusan.Scheufele@Sun.COM 	    dcmd_dma_obj.dma_cookie[0].dmac_address);
29799440SSusan.Scheufele@Sun.COM 
29809440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_TRUE;
29819440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
29829440SSusan.Scheufele@Sun.COM 
29839440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
29849440SSusan.Scheufele@Sun.COM 		cmn_err(CE_WARN, "get_seq_num: "
29859440SSusan.Scheufele@Sun.COM 		    "failed to issue MRSAS_DCMD_CTRL_EVENT_GET_INFO");
29869440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
29879440SSusan.Scheufele@Sun.COM 	} else {
29889795SYu.Wu@Sun.COM 		eli_tmp = (struct mrsas_evt_log_info *)dcmd_dma_obj.buffer;
29899795SYu.Wu@Sun.COM 		eli->newest_seq_num = ddi_get32(cmd->frame_dma_obj.acc_handle,
29909795SYu.Wu@Sun.COM 		    &eli_tmp->newest_seq_num);
29919440SSusan.Scheufele@Sun.COM 		ret = DDI_SUCCESS;
29929440SSusan.Scheufele@Sun.COM 	}
29939440SSusan.Scheufele@Sun.COM 
29949440SSusan.Scheufele@Sun.COM 	if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS)
29959440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
29969440SSusan.Scheufele@Sun.COM 
29979440SSusan.Scheufele@Sun.COM 	if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
29989440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
29999440SSusan.Scheufele@Sun.COM 	}
300010600SYu.Wu@Sun.COM 
300110600SYu.Wu@Sun.COM 	return_mfi_pkt(instance, cmd);
300210600SYu.Wu@Sun.COM 
30039440SSusan.Scheufele@Sun.COM 	return (ret);
30049440SSusan.Scheufele@Sun.COM }
30059440SSusan.Scheufele@Sun.COM 
30069440SSusan.Scheufele@Sun.COM /*
30079440SSusan.Scheufele@Sun.COM  * start_mfi_aen
30089440SSusan.Scheufele@Sun.COM  */
30099440SSusan.Scheufele@Sun.COM static int
start_mfi_aen(struct mrsas_instance * instance)30109440SSusan.Scheufele@Sun.COM start_mfi_aen(struct mrsas_instance *instance)
30119440SSusan.Scheufele@Sun.COM {
30129440SSusan.Scheufele@Sun.COM 	int	ret = 0;
30139440SSusan.Scheufele@Sun.COM 
30149440SSusan.Scheufele@Sun.COM 	struct mrsas_evt_log_info	eli;
30159440SSusan.Scheufele@Sun.COM 	union mrsas_evt_class_locale	class_locale;
30169440SSusan.Scheufele@Sun.COM 
30179440SSusan.Scheufele@Sun.COM 	/* get the latest sequence number from FW */
30189440SSusan.Scheufele@Sun.COM 	(void) memset(&eli, 0, sizeof (struct mrsas_evt_log_info));
30199440SSusan.Scheufele@Sun.COM 
30209440SSusan.Scheufele@Sun.COM 	if (get_seq_num(instance, &eli)) {
30219440SSusan.Scheufele@Sun.COM 		cmn_err(CE_WARN, "start_mfi_aen: failed to get seq num");
30229440SSusan.Scheufele@Sun.COM 		return (-1);
30239440SSusan.Scheufele@Sun.COM 	}
30249440SSusan.Scheufele@Sun.COM 
30259440SSusan.Scheufele@Sun.COM 	/* register AEN with FW for latest sequence number plus 1 */
30269440SSusan.Scheufele@Sun.COM 	class_locale.members.reserved	= 0;
30279795SYu.Wu@Sun.COM 	class_locale.members.locale	= LE_16(MR_EVT_LOCALE_ALL);
30289440SSusan.Scheufele@Sun.COM 	class_locale.members.class	= MR_EVT_CLASS_INFO;
30299795SYu.Wu@Sun.COM 	class_locale.word	= LE_32(class_locale.word);
30309440SSusan.Scheufele@Sun.COM 	ret = register_mfi_aen(instance, eli.newest_seq_num + 1,
30319440SSusan.Scheufele@Sun.COM 	    class_locale.word);
30329440SSusan.Scheufele@Sun.COM 
30339440SSusan.Scheufele@Sun.COM 	if (ret) {
30349440SSusan.Scheufele@Sun.COM 		cmn_err(CE_WARN, "start_mfi_aen: aen registration failed");
30359440SSusan.Scheufele@Sun.COM 		return (-1);
30369440SSusan.Scheufele@Sun.COM 	}
30379440SSusan.Scheufele@Sun.COM 
30389440SSusan.Scheufele@Sun.COM 	return (ret);
30399440SSusan.Scheufele@Sun.COM }
30409440SSusan.Scheufele@Sun.COM 
30419440SSusan.Scheufele@Sun.COM /*
30429440SSusan.Scheufele@Sun.COM  * flush_cache
30439440SSusan.Scheufele@Sun.COM  */
30449440SSusan.Scheufele@Sun.COM static void
flush_cache(struct mrsas_instance * instance)30459440SSusan.Scheufele@Sun.COM flush_cache(struct mrsas_instance *instance)
30469440SSusan.Scheufele@Sun.COM {
30479440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd		*cmd = NULL;
30489440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame		*dcmd;
30499440SSusan.Scheufele@Sun.COM 	uint32_t	max_cmd = instance->max_fw_cmds;
30509440SSusan.Scheufele@Sun.COM 
30519440SSusan.Scheufele@Sun.COM 	cmd = instance->cmd_list[max_cmd];
30529440SSusan.Scheufele@Sun.COM 
305312356SYu.Wu@Sun.COM 	if (!cmd) {
305412356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
305512356SYu.Wu@Sun.COM 		    "flush_cache():Failed to get a cmd for flush_cache"));
305612356SYu.Wu@Sun.COM 		DTRACE_PROBE2(flush_cache_err, uint16_t,
305712356SYu.Wu@Sun.COM 		    instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
30589440SSusan.Scheufele@Sun.COM 		return;
305912356SYu.Wu@Sun.COM 	}
3060*12757SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
3061*12757SYu.Wu@Sun.COM 	/* Clear the frame buffer and assign back the context id */
3062*12757SYu.Wu@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3063*12757SYu.Wu@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
3064*12757SYu.Wu@Sun.COM 	    cmd->index);
3065*12757SYu.Wu@Sun.COM 
30669440SSusan.Scheufele@Sun.COM 	dcmd = &cmd->frame->dcmd;
30679440SSusan.Scheufele@Sun.COM 
30689440SSusan.Scheufele@Sun.COM 	(void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
30699440SSusan.Scheufele@Sun.COM 
30709440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
30719440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0);
30729440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 0);
30739440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
30749440SSusan.Scheufele@Sun.COM 	    MFI_FRAME_DIR_NONE);
30759440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
30769440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 0);
30779440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
30789440SSusan.Scheufele@Sun.COM 	    MR_DCMD_CTRL_CACHE_FLUSH);
30799440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.b[0],
30809440SSusan.Scheufele@Sun.COM 	    MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE);
30819440SSusan.Scheufele@Sun.COM 
30829440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
30839440SSusan.Scheufele@Sun.COM 
30849440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
30859440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_WARN,
30869440SSusan.Scheufele@Sun.COM 	    "flush_cache: failed to issue MFI_DCMD_CTRL_CACHE_FLUSH"));
30879440SSusan.Scheufele@Sun.COM 	}
308812244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "flush_cache done"));
30899440SSusan.Scheufele@Sun.COM }
30909440SSusan.Scheufele@Sun.COM 
30919440SSusan.Scheufele@Sun.COM /*
30929440SSusan.Scheufele@Sun.COM  * service_mfi_aen-	Completes an AEN command
30939440SSusan.Scheufele@Sun.COM  * @instance:			Adapter soft state
30949440SSusan.Scheufele@Sun.COM  * @cmd:			Command to be completed
30959440SSusan.Scheufele@Sun.COM  *
30969440SSusan.Scheufele@Sun.COM  */
30979440SSusan.Scheufele@Sun.COM static void
service_mfi_aen(struct mrsas_instance * instance,struct mrsas_cmd * cmd)30989440SSusan.Scheufele@Sun.COM service_mfi_aen(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
30999440SSusan.Scheufele@Sun.COM {
31009440SSusan.Scheufele@Sun.COM 	uint32_t	seq_num;
31019440SSusan.Scheufele@Sun.COM 	struct mrsas_evt_detail *evt_detail =
31029440SSusan.Scheufele@Sun.COM 	    (struct mrsas_evt_detail *)instance->mfi_evt_detail_obj.buffer;
31039440SSusan.Scheufele@Sun.COM 	int		rval = 0;
31049440SSusan.Scheufele@Sun.COM 	int		tgt = 0;
31059440SSusan.Scheufele@Sun.COM 	ddi_acc_handle_t		acc_handle;
31069440SSusan.Scheufele@Sun.COM 
31079440SSusan.Scheufele@Sun.COM 	acc_handle = cmd->frame_dma_obj.acc_handle;
31089440SSusan.Scheufele@Sun.COM 
31099440SSusan.Scheufele@Sun.COM 	cmd->cmd_status = ddi_get8(acc_handle, &cmd->frame->io.cmd_status);
31109440SSusan.Scheufele@Sun.COM 
31119440SSusan.Scheufele@Sun.COM 	if (cmd->cmd_status == ENODATA) {
31129440SSusan.Scheufele@Sun.COM 		cmd->cmd_status = 0;
31139440SSusan.Scheufele@Sun.COM 	}
31149440SSusan.Scheufele@Sun.COM 
31159440SSusan.Scheufele@Sun.COM 	/*
31169440SSusan.Scheufele@Sun.COM 	 * log the MFI AEN event to the sysevent queue so that
31179440SSusan.Scheufele@Sun.COM 	 * application will get noticed
31189440SSusan.Scheufele@Sun.COM 	 */
31199440SSusan.Scheufele@Sun.COM 	if (ddi_log_sysevent(instance->dip, DDI_VENDOR_LSI, "LSIMEGA", "SAS",
31209440SSusan.Scheufele@Sun.COM 	    NULL, NULL, DDI_NOSLEEP) != DDI_SUCCESS) {
31219440SSusan.Scheufele@Sun.COM 		int	instance_no = ddi_get_instance(instance->dip);
31229440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
31239440SSusan.Scheufele@Sun.COM 		    "mr_sas%d: Failed to log AEN event", instance_no));
31249440SSusan.Scheufele@Sun.COM 	}
31259440SSusan.Scheufele@Sun.COM 	/*
31269440SSusan.Scheufele@Sun.COM 	 * Check for any ld devices that has changed state. i.e. online
31279440SSusan.Scheufele@Sun.COM 	 * or offline.
31289440SSusan.Scheufele@Sun.COM 	 */
31299440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
31309440SSusan.Scheufele@Sun.COM 	    "AEN: code = %x class = %x locale = %x args = %x",
31319440SSusan.Scheufele@Sun.COM 	    ddi_get32(acc_handle, &evt_detail->code),
31329440SSusan.Scheufele@Sun.COM 	    evt_detail->cl.members.class,
31339440SSusan.Scheufele@Sun.COM 	    ddi_get16(acc_handle, &evt_detail->cl.members.locale),
31349440SSusan.Scheufele@Sun.COM 	    ddi_get8(acc_handle, &evt_detail->arg_type)));
31359440SSusan.Scheufele@Sun.COM 
31369440SSusan.Scheufele@Sun.COM 	switch (ddi_get32(acc_handle, &evt_detail->code)) {
31379440SSusan.Scheufele@Sun.COM 	case MR_EVT_CFG_CLEARED: {
31389440SSusan.Scheufele@Sun.COM 		for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) {
31399440SSusan.Scheufele@Sun.COM 			if (instance->mr_ld_list[tgt].dip != NULL) {
31409440SSusan.Scheufele@Sun.COM 				rval = mrsas_service_evt(instance, tgt, 0,
31419440SSusan.Scheufele@Sun.COM 				    MRSAS_EVT_UNCONFIG_TGT, NULL);
31429440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN1, (CE_WARN,
31439440SSusan.Scheufele@Sun.COM 				    "mr_sas: CFG CLEARED AEN rval = %d "
31449440SSusan.Scheufele@Sun.COM 				    "tgt id = %d", rval, tgt));
31459440SSusan.Scheufele@Sun.COM 			}
31469440SSusan.Scheufele@Sun.COM 		}
31479440SSusan.Scheufele@Sun.COM 		break;
31489440SSusan.Scheufele@Sun.COM 	}
31499440SSusan.Scheufele@Sun.COM 
31509440SSusan.Scheufele@Sun.COM 	case MR_EVT_LD_DELETED: {
31519440SSusan.Scheufele@Sun.COM 		rval = mrsas_service_evt(instance,
31529440SSusan.Scheufele@Sun.COM 		    ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0,
31539440SSusan.Scheufele@Sun.COM 		    MRSAS_EVT_UNCONFIG_TGT, NULL);
31549440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "mr_sas: LD DELETED AEN rval = %d "
31559440SSusan.Scheufele@Sun.COM 		    "tgt id = %d index = %d", rval,
31569440SSusan.Scheufele@Sun.COM 		    ddi_get16(acc_handle, &evt_detail->args.ld.target_id),
31579440SSusan.Scheufele@Sun.COM 		    ddi_get8(acc_handle, &evt_detail->args.ld.ld_index)));
31589440SSusan.Scheufele@Sun.COM 		break;
31599440SSusan.Scheufele@Sun.COM 	} /* End of MR_EVT_LD_DELETED */
31609440SSusan.Scheufele@Sun.COM 
31619440SSusan.Scheufele@Sun.COM 	case MR_EVT_LD_CREATED: {
31629440SSusan.Scheufele@Sun.COM 		rval = mrsas_service_evt(instance,
31639440SSusan.Scheufele@Sun.COM 		    ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0,
31649440SSusan.Scheufele@Sun.COM 		    MRSAS_EVT_CONFIG_TGT, NULL);
31659440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "mr_sas: LD CREATED AEN rval = %d "
31669440SSusan.Scheufele@Sun.COM 		    "tgt id = %d index = %d", rval,
31679440SSusan.Scheufele@Sun.COM 		    ddi_get16(acc_handle, &evt_detail->args.ld.target_id),
31689440SSusan.Scheufele@Sun.COM 		    ddi_get8(acc_handle, &evt_detail->args.ld.ld_index)));
31699440SSusan.Scheufele@Sun.COM 		break;
31709440SSusan.Scheufele@Sun.COM 	} /* End of MR_EVT_LD_CREATED */
31719440SSusan.Scheufele@Sun.COM 	} /* End of Main Switch */
31729440SSusan.Scheufele@Sun.COM 
31739440SSusan.Scheufele@Sun.COM 	/* get copy of seq_num and class/locale for re-registration */
31749440SSusan.Scheufele@Sun.COM 	seq_num = ddi_get32(acc_handle, &evt_detail->seq_num);
31759440SSusan.Scheufele@Sun.COM 	seq_num++;
31769440SSusan.Scheufele@Sun.COM 	(void) memset(instance->mfi_evt_detail_obj.buffer, 0,
31779440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_detail));
31789440SSusan.Scheufele@Sun.COM 
31799440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &cmd->frame->dcmd.cmd_status, 0x0);
31809440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &cmd->frame->dcmd.mbox.w[0], seq_num);
31819440SSusan.Scheufele@Sun.COM 
31829440SSusan.Scheufele@Sun.COM 	instance->aen_seq_num = seq_num;
31839440SSusan.Scheufele@Sun.COM 
31849440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
31859440SSusan.Scheufele@Sun.COM 
31869440SSusan.Scheufele@Sun.COM 	/* Issue the aen registration frame */
31879440SSusan.Scheufele@Sun.COM 	instance->func_ptr->issue_cmd(cmd, instance);
31889440SSusan.Scheufele@Sun.COM }
31899440SSusan.Scheufele@Sun.COM 
31909440SSusan.Scheufele@Sun.COM /*
31919440SSusan.Scheufele@Sun.COM  * complete_cmd_in_sync_mode -	Completes an internal command
31929440SSusan.Scheufele@Sun.COM  * @instance:			Adapter soft state
31939440SSusan.Scheufele@Sun.COM  * @cmd:			Command to be completed
31949440SSusan.Scheufele@Sun.COM  *
31959440SSusan.Scheufele@Sun.COM  * The issue_cmd_in_sync_mode() function waits for a command to complete
31969440SSusan.Scheufele@Sun.COM  * after it issues a command. This function wakes up that waiting routine by
31979440SSusan.Scheufele@Sun.COM  * calling wake_up() on the wait queue.
31989440SSusan.Scheufele@Sun.COM  */
31999440SSusan.Scheufele@Sun.COM static void
complete_cmd_in_sync_mode(struct mrsas_instance * instance,struct mrsas_cmd * cmd)32009440SSusan.Scheufele@Sun.COM complete_cmd_in_sync_mode(struct mrsas_instance *instance,
32019440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd)
32029440SSusan.Scheufele@Sun.COM {
32039440SSusan.Scheufele@Sun.COM 	cmd->cmd_status = ddi_get8(cmd->frame_dma_obj.acc_handle,
32049440SSusan.Scheufele@Sun.COM 	    &cmd->frame->io.cmd_status);
32059440SSusan.Scheufele@Sun.COM 
32069440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_FALSE;
32079440SSusan.Scheufele@Sun.COM 
32089440SSusan.Scheufele@Sun.COM 	if (cmd->cmd_status == ENODATA) {
32099440SSusan.Scheufele@Sun.COM 		cmd->cmd_status = 0;
32109440SSusan.Scheufele@Sun.COM 	}
32119440SSusan.Scheufele@Sun.COM 
321212244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "complete_cmd_in_sync_mode called %p \n",
321312244SYu.Wu@Sun.COM 	    (void *)cmd));
321412244SYu.Wu@Sun.COM 
32159440SSusan.Scheufele@Sun.COM 	cv_broadcast(&instance->int_cmd_cv);
32169440SSusan.Scheufele@Sun.COM }
32179440SSusan.Scheufele@Sun.COM 
32189440SSusan.Scheufele@Sun.COM /*
321912244SYu.Wu@Sun.COM  * Call this function inside mrsas_softintr.
322012244SYu.Wu@Sun.COM  * mrsas_initiate_ocr_if_fw_is_faulty  - Initiates OCR if FW status is faulty
322112244SYu.Wu@Sun.COM  * @instance:			Adapter soft state
322212244SYu.Wu@Sun.COM  */
322312244SYu.Wu@Sun.COM 
322412244SYu.Wu@Sun.COM static uint32_t
mrsas_initiate_ocr_if_fw_is_faulty(struct mrsas_instance * instance)322512244SYu.Wu@Sun.COM mrsas_initiate_ocr_if_fw_is_faulty(struct mrsas_instance *instance)
322612244SYu.Wu@Sun.COM {
322712244SYu.Wu@Sun.COM 	uint32_t	cur_abs_reg_val;
322812244SYu.Wu@Sun.COM 	uint32_t	fw_state;
322912244SYu.Wu@Sun.COM 
323012244SYu.Wu@Sun.COM 	cur_abs_reg_val =  instance->func_ptr->read_fw_status_reg(instance);
323112244SYu.Wu@Sun.COM 	fw_state = cur_abs_reg_val & MFI_STATE_MASK;
323212244SYu.Wu@Sun.COM 	if (fw_state == MFI_STATE_FAULT) {
323312244SYu.Wu@Sun.COM 
323412244SYu.Wu@Sun.COM 		if (instance->disable_online_ctrl_reset == 1) {
323512244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
323612244SYu.Wu@Sun.COM 		    "mrsas_initiate_ocr_if_fw_is_faulty: "
323712244SYu.Wu@Sun.COM 		    "FW in Fault state, detected in ISR: "
323812244SYu.Wu@Sun.COM 		    "FW doesn't support ocr "));
323912244SYu.Wu@Sun.COM 		return (ADAPTER_RESET_NOT_REQUIRED);
324012244SYu.Wu@Sun.COM 		} else {
324112244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
324212244SYu.Wu@Sun.COM 		    "mrsas_initiate_ocr_if_fw_is_faulty: "
324312244SYu.Wu@Sun.COM 		    "FW in Fault state, detected in ISR: FW supports ocr "));
324412244SYu.Wu@Sun.COM 			return (ADAPTER_RESET_REQUIRED);
324512244SYu.Wu@Sun.COM 		}
324612244SYu.Wu@Sun.COM 	}
324712244SYu.Wu@Sun.COM 	return (ADAPTER_RESET_NOT_REQUIRED);
324812244SYu.Wu@Sun.COM }
324912244SYu.Wu@Sun.COM 
325012244SYu.Wu@Sun.COM /*
32519440SSusan.Scheufele@Sun.COM  * mrsas_softintr - The Software ISR
32529440SSusan.Scheufele@Sun.COM  * @param arg	: HBA soft state
32539440SSusan.Scheufele@Sun.COM  *
32549440SSusan.Scheufele@Sun.COM  * called from high-level interrupt if hi-level interrupt are not there,
32559440SSusan.Scheufele@Sun.COM  * otherwise triggered as a soft interrupt
32569440SSusan.Scheufele@Sun.COM  */
32579440SSusan.Scheufele@Sun.COM static uint_t
mrsas_softintr(struct mrsas_instance * instance)32589440SSusan.Scheufele@Sun.COM mrsas_softintr(struct mrsas_instance *instance)
32599440SSusan.Scheufele@Sun.COM {
32609440SSusan.Scheufele@Sun.COM 	struct scsi_pkt		*pkt;
32619440SSusan.Scheufele@Sun.COM 	struct scsa_cmd		*acmd;
32629440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
32639440SSusan.Scheufele@Sun.COM 	struct mlist_head	*pos, *next;
32649440SSusan.Scheufele@Sun.COM 	mlist_t			process_list;
32659440SSusan.Scheufele@Sun.COM 	struct mrsas_header	*hdr;
32669440SSusan.Scheufele@Sun.COM 	struct scsi_arq_status	*arqstat;
32679440SSusan.Scheufele@Sun.COM 
32689440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_CONT, "mrsas_softintr called"));
32699440SSusan.Scheufele@Sun.COM 
32709440SSusan.Scheufele@Sun.COM 	ASSERT(instance);
327112244SYu.Wu@Sun.COM 
32729440SSusan.Scheufele@Sun.COM 	mutex_enter(&instance->completed_pool_mtx);
32739440SSusan.Scheufele@Sun.COM 
32749440SSusan.Scheufele@Sun.COM 	if (mlist_empty(&instance->completed_pool_list)) {
32759440SSusan.Scheufele@Sun.COM 		mutex_exit(&instance->completed_pool_mtx);
32769795SYu.Wu@Sun.COM 		return (DDI_INTR_CLAIMED);
32779440SSusan.Scheufele@Sun.COM 	}
32789440SSusan.Scheufele@Sun.COM 
32799440SSusan.Scheufele@Sun.COM 	instance->softint_running = 1;
32809440SSusan.Scheufele@Sun.COM 
32819440SSusan.Scheufele@Sun.COM 	INIT_LIST_HEAD(&process_list);
32829440SSusan.Scheufele@Sun.COM 	mlist_splice(&instance->completed_pool_list, &process_list);
32839440SSusan.Scheufele@Sun.COM 	INIT_LIST_HEAD(&instance->completed_pool_list);
32849440SSusan.Scheufele@Sun.COM 
32859440SSusan.Scheufele@Sun.COM 	mutex_exit(&instance->completed_pool_mtx);
32869440SSusan.Scheufele@Sun.COM 
32879440SSusan.Scheufele@Sun.COM 	/* perform all callbacks first, before releasing the SCBs */
32889440SSusan.Scheufele@Sun.COM 	mlist_for_each_safe(pos, next, &process_list) {
32899440SSusan.Scheufele@Sun.COM 		cmd = mlist_entry(pos, struct mrsas_cmd, list);
32909440SSusan.Scheufele@Sun.COM 
32919440SSusan.Scheufele@Sun.COM 		/* syncronize the Cmd frame for the controller */
32929440SSusan.Scheufele@Sun.COM 		(void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle,
32939440SSusan.Scheufele@Sun.COM 		    0, 0, DDI_DMA_SYNC_FORCPU);
32949440SSusan.Scheufele@Sun.COM 
32959440SSusan.Scheufele@Sun.COM 		if (mrsas_check_dma_handle(cmd->frame_dma_obj.dma_handle) !=
32969440SSusan.Scheufele@Sun.COM 		    DDI_SUCCESS) {
32979440SSusan.Scheufele@Sun.COM 			mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
32989440SSusan.Scheufele@Sun.COM 			ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
329912244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_WARN,
330012244SYu.Wu@Sun.COM 			    "mrsas_softintr: "
330112244SYu.Wu@Sun.COM 			    "FMA check reports DMA handle failure"));
33029795SYu.Wu@Sun.COM 			return (DDI_INTR_CLAIMED);
33039440SSusan.Scheufele@Sun.COM 		}
33049440SSusan.Scheufele@Sun.COM 
33059440SSusan.Scheufele@Sun.COM 		hdr = &cmd->frame->hdr;
33069440SSusan.Scheufele@Sun.COM 
33079440SSusan.Scheufele@Sun.COM 		/* remove the internal command from the process list */
33089440SSusan.Scheufele@Sun.COM 		mlist_del_init(&cmd->list);
33099440SSusan.Scheufele@Sun.COM 
33109440SSusan.Scheufele@Sun.COM 		switch (ddi_get8(cmd->frame_dma_obj.acc_handle, &hdr->cmd)) {
33119440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_PD_SCSI:
33129440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_LD_SCSI:
33139440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_LD_READ:
33149440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_LD_WRITE:
33159440SSusan.Scheufele@Sun.COM 			/*
33169440SSusan.Scheufele@Sun.COM 			 * MFI_CMD_OP_PD_SCSI and MFI_CMD_OP_LD_SCSI
33179440SSusan.Scheufele@Sun.COM 			 * could have been issued either through an
33189440SSusan.Scheufele@Sun.COM 			 * IO path or an IOCTL path. If it was via IOCTL,
33199440SSusan.Scheufele@Sun.COM 			 * we will send it to internal completion.
33209440SSusan.Scheufele@Sun.COM 			 */
33219440SSusan.Scheufele@Sun.COM 			if (cmd->sync_cmd == MRSAS_TRUE) {
33229440SSusan.Scheufele@Sun.COM 				complete_cmd_in_sync_mode(instance, cmd);
33239440SSusan.Scheufele@Sun.COM 				break;
33249440SSusan.Scheufele@Sun.COM 			}
33259440SSusan.Scheufele@Sun.COM 
33269440SSusan.Scheufele@Sun.COM 			/* regular commands */
33279440SSusan.Scheufele@Sun.COM 			acmd =	cmd->cmd;
33289440SSusan.Scheufele@Sun.COM 			pkt =	CMD2PKT(acmd);
33299440SSusan.Scheufele@Sun.COM 
33309440SSusan.Scheufele@Sun.COM 			if (acmd->cmd_flags & CFLAG_DMAVALID) {
33319440SSusan.Scheufele@Sun.COM 				if (acmd->cmd_flags & CFLAG_CONSISTENT) {
33329440SSusan.Scheufele@Sun.COM 					(void) ddi_dma_sync(acmd->cmd_dmahandle,
33339440SSusan.Scheufele@Sun.COM 					    acmd->cmd_dma_offset,
33349440SSusan.Scheufele@Sun.COM 					    acmd->cmd_dma_len,
33359440SSusan.Scheufele@Sun.COM 					    DDI_DMA_SYNC_FORCPU);
33369440SSusan.Scheufele@Sun.COM 				}
33379440SSusan.Scheufele@Sun.COM 			}
33389440SSusan.Scheufele@Sun.COM 
33399440SSusan.Scheufele@Sun.COM 			pkt->pkt_reason		= CMD_CMPLT;
33409440SSusan.Scheufele@Sun.COM 			pkt->pkt_statistics	= 0;
33419440SSusan.Scheufele@Sun.COM 			pkt->pkt_state = STATE_GOT_BUS
33429440SSusan.Scheufele@Sun.COM 			    | STATE_GOT_TARGET | STATE_SENT_CMD
33439440SSusan.Scheufele@Sun.COM 			    | STATE_XFERRED_DATA | STATE_GOT_STATUS;
33449440SSusan.Scheufele@Sun.COM 
33459440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_CONT,
33469440SSusan.Scheufele@Sun.COM 			    "CDB[0] = %x completed for %s: size %lx context %x",
33479440SSusan.Scheufele@Sun.COM 			    pkt->pkt_cdbp[0], ((acmd->islogical) ? "LD" : "PD"),
33489440SSusan.Scheufele@Sun.COM 			    acmd->cmd_dmacount, hdr->context));
334910820SSusan.Scheufele@Sun.COM 			DTRACE_PROBE3(softintr_cdb, uint8_t, pkt->pkt_cdbp[0],
335010820SSusan.Scheufele@Sun.COM 			    uint_t, acmd->cmd_cdblen, ulong_t,
335110820SSusan.Scheufele@Sun.COM 			    acmd->cmd_dmacount);
33529440SSusan.Scheufele@Sun.COM 
33539440SSusan.Scheufele@Sun.COM 			if (pkt->pkt_cdbp[0] == SCMD_INQUIRY) {
33549440SSusan.Scheufele@Sun.COM 				struct scsi_inquiry	*inq;
33559440SSusan.Scheufele@Sun.COM 
33569440SSusan.Scheufele@Sun.COM 				if (acmd->cmd_dmacount != 0) {
33579440SSusan.Scheufele@Sun.COM 					bp_mapin(acmd->cmd_buf);
33589440SSusan.Scheufele@Sun.COM 					inq = (struct scsi_inquiry *)
33599440SSusan.Scheufele@Sun.COM 					    acmd->cmd_buf->b_un.b_addr;
33609440SSusan.Scheufele@Sun.COM 
33619440SSusan.Scheufele@Sun.COM 					/* don't expose physical drives to OS */
33629440SSusan.Scheufele@Sun.COM 					if (acmd->islogical &&
33639440SSusan.Scheufele@Sun.COM 					    (hdr->cmd_status == MFI_STAT_OK)) {
33649440SSusan.Scheufele@Sun.COM 						display_scsi_inquiry(
33659440SSusan.Scheufele@Sun.COM 						    (caddr_t)inq);
33669440SSusan.Scheufele@Sun.COM 					} else if ((hdr->cmd_status ==
33679440SSusan.Scheufele@Sun.COM 					    MFI_STAT_OK) && inq->inq_dtype ==
33689440SSusan.Scheufele@Sun.COM 					    DTYPE_DIRECT) {
33699440SSusan.Scheufele@Sun.COM 
33709440SSusan.Scheufele@Sun.COM 						display_scsi_inquiry(
33719440SSusan.Scheufele@Sun.COM 						    (caddr_t)inq);
33729440SSusan.Scheufele@Sun.COM 
33739440SSusan.Scheufele@Sun.COM 						/* for physical disk */
33749440SSusan.Scheufele@Sun.COM 						hdr->cmd_status =
33759440SSusan.Scheufele@Sun.COM 						    MFI_STAT_DEVICE_NOT_FOUND;
33769440SSusan.Scheufele@Sun.COM 					}
33779440SSusan.Scheufele@Sun.COM 				}
33789440SSusan.Scheufele@Sun.COM 			}
33799440SSusan.Scheufele@Sun.COM 
338010820SSusan.Scheufele@Sun.COM 			DTRACE_PROBE2(softintr_done, uint8_t, hdr->cmd,
338110820SSusan.Scheufele@Sun.COM 			    uint8_t, hdr->cmd_status);
338210820SSusan.Scheufele@Sun.COM 
33839440SSusan.Scheufele@Sun.COM 			switch (hdr->cmd_status) {
33849440SSusan.Scheufele@Sun.COM 			case MFI_STAT_OK:
33859440SSusan.Scheufele@Sun.COM 				pkt->pkt_scbp[0] = STATUS_GOOD;
33869440SSusan.Scheufele@Sun.COM 				break;
33879440SSusan.Scheufele@Sun.COM 			case MFI_STAT_LD_CC_IN_PROGRESS:
33889440SSusan.Scheufele@Sun.COM 			case MFI_STAT_LD_RECON_IN_PROGRESS:
33899440SSusan.Scheufele@Sun.COM 				pkt->pkt_scbp[0] = STATUS_GOOD;
33909440SSusan.Scheufele@Sun.COM 				break;
33919440SSusan.Scheufele@Sun.COM 			case MFI_STAT_LD_INIT_IN_PROGRESS:
33929440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN,
33939440SSusan.Scheufele@Sun.COM 				    (CE_WARN, "Initialization in Progress"));
33949440SSusan.Scheufele@Sun.COM 				pkt->pkt_reason	= CMD_TRAN_ERR;
33959440SSusan.Scheufele@Sun.COM 
33969440SSusan.Scheufele@Sun.COM 				break;
33979440SSusan.Scheufele@Sun.COM 			case MFI_STAT_SCSI_DONE_WITH_ERROR:
33989440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN1, (CE_CONT, "scsi_done error"));
33999440SSusan.Scheufele@Sun.COM 
34009440SSusan.Scheufele@Sun.COM 				pkt->pkt_reason	= CMD_CMPLT;
34019440SSusan.Scheufele@Sun.COM 				((struct scsi_status *)
34029440SSusan.Scheufele@Sun.COM 				    pkt->pkt_scbp)->sts_chk = 1;
34039440SSusan.Scheufele@Sun.COM 
34049440SSusan.Scheufele@Sun.COM 				if (pkt->pkt_cdbp[0] == SCMD_TEST_UNIT_READY) {
34059440SSusan.Scheufele@Sun.COM 
34069440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN,
34079440SSusan.Scheufele@Sun.COM 					    (CE_WARN, "TEST_UNIT_READY fail"));
34089440SSusan.Scheufele@Sun.COM 
34099440SSusan.Scheufele@Sun.COM 				} else {
34109440SSusan.Scheufele@Sun.COM 					pkt->pkt_state |= STATE_ARQ_DONE;
34119440SSusan.Scheufele@Sun.COM 					arqstat = (void *)(pkt->pkt_scbp);
34129440SSusan.Scheufele@Sun.COM 					arqstat->sts_rqpkt_reason = CMD_CMPLT;
34139440SSusan.Scheufele@Sun.COM 					arqstat->sts_rqpkt_resid = 0;
34149440SSusan.Scheufele@Sun.COM 					arqstat->sts_rqpkt_state |=
34159440SSusan.Scheufele@Sun.COM 					    STATE_GOT_BUS | STATE_GOT_TARGET
34169440SSusan.Scheufele@Sun.COM 					    | STATE_SENT_CMD
34179440SSusan.Scheufele@Sun.COM 					    | STATE_XFERRED_DATA;
34189440SSusan.Scheufele@Sun.COM 					*(uint8_t *)&arqstat->sts_rqpkt_status =
34199440SSusan.Scheufele@Sun.COM 					    STATUS_GOOD;
34209440SSusan.Scheufele@Sun.COM 					ddi_rep_get8(
34219440SSusan.Scheufele@Sun.COM 					    cmd->frame_dma_obj.acc_handle,
34229440SSusan.Scheufele@Sun.COM 					    (uint8_t *)
34239440SSusan.Scheufele@Sun.COM 					    &(arqstat->sts_sensedata),
34249440SSusan.Scheufele@Sun.COM 					    cmd->sense,
34259440SSusan.Scheufele@Sun.COM 					    acmd->cmd_scblen -
34269440SSusan.Scheufele@Sun.COM 					    offsetof(struct scsi_arq_status,
34279440SSusan.Scheufele@Sun.COM 					    sts_sensedata), DDI_DEV_AUTOINCR);
34289795SYu.Wu@Sun.COM 			}
34299440SSusan.Scheufele@Sun.COM 				break;
34309440SSusan.Scheufele@Sun.COM 			case MFI_STAT_LD_OFFLINE:
34319440SSusan.Scheufele@Sun.COM 			case MFI_STAT_DEVICE_NOT_FOUND:
34329440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN1, (CE_CONT,
343312244SYu.Wu@Sun.COM 				"mrsas_softintr:device not found error"));
34349440SSusan.Scheufele@Sun.COM 				pkt->pkt_reason	= CMD_DEV_GONE;
34359440SSusan.Scheufele@Sun.COM 				pkt->pkt_statistics  = STAT_DISCON;
34369440SSusan.Scheufele@Sun.COM 				break;
34379440SSusan.Scheufele@Sun.COM 			case MFI_STAT_LD_LBA_OUT_OF_RANGE:
34389440SSusan.Scheufele@Sun.COM 				pkt->pkt_state |= STATE_ARQ_DONE;
34399440SSusan.Scheufele@Sun.COM 				pkt->pkt_reason	= CMD_CMPLT;
34409440SSusan.Scheufele@Sun.COM 				((struct scsi_status *)
34419440SSusan.Scheufele@Sun.COM 				    pkt->pkt_scbp)->sts_chk = 1;
34429440SSusan.Scheufele@Sun.COM 
34439440SSusan.Scheufele@Sun.COM 				arqstat = (void *)(pkt->pkt_scbp);
34449440SSusan.Scheufele@Sun.COM 				arqstat->sts_rqpkt_reason = CMD_CMPLT;
34459440SSusan.Scheufele@Sun.COM 				arqstat->sts_rqpkt_resid = 0;
34469440SSusan.Scheufele@Sun.COM 				arqstat->sts_rqpkt_state |= STATE_GOT_BUS
34479440SSusan.Scheufele@Sun.COM 				    | STATE_GOT_TARGET | STATE_SENT_CMD
34489440SSusan.Scheufele@Sun.COM 				    | STATE_XFERRED_DATA;
34499440SSusan.Scheufele@Sun.COM 				*(uint8_t *)&arqstat->sts_rqpkt_status =
34509440SSusan.Scheufele@Sun.COM 				    STATUS_GOOD;
34519440SSusan.Scheufele@Sun.COM 
34529440SSusan.Scheufele@Sun.COM 				arqstat->sts_sensedata.es_valid = 1;
34539440SSusan.Scheufele@Sun.COM 				arqstat->sts_sensedata.es_key =
34549440SSusan.Scheufele@Sun.COM 				    KEY_ILLEGAL_REQUEST;
34559440SSusan.Scheufele@Sun.COM 				arqstat->sts_sensedata.es_class =
34569440SSusan.Scheufele@Sun.COM 				    CLASS_EXTENDED_SENSE;
34579440SSusan.Scheufele@Sun.COM 
34589440SSusan.Scheufele@Sun.COM 				/*
34599440SSusan.Scheufele@Sun.COM 				 * LOGICAL BLOCK ADDRESS OUT OF RANGE:
34609440SSusan.Scheufele@Sun.COM 				 * ASC: 0x21h; ASCQ: 0x00h;
34619440SSusan.Scheufele@Sun.COM 				 */
34629440SSusan.Scheufele@Sun.COM 				arqstat->sts_sensedata.es_add_code = 0x21;
34639440SSusan.Scheufele@Sun.COM 				arqstat->sts_sensedata.es_qual_code = 0x00;
34649440SSusan.Scheufele@Sun.COM 
34659440SSusan.Scheufele@Sun.COM 				break;
34669440SSusan.Scheufele@Sun.COM 
34679440SSusan.Scheufele@Sun.COM 			default:
34689440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_CONT, "Unknown status!"));
34699440SSusan.Scheufele@Sun.COM 				pkt->pkt_reason	= CMD_TRAN_ERR;
34709440SSusan.Scheufele@Sun.COM 
34719440SSusan.Scheufele@Sun.COM 				break;
34729440SSusan.Scheufele@Sun.COM 			}
34739440SSusan.Scheufele@Sun.COM 
34749440SSusan.Scheufele@Sun.COM 			atomic_add_16(&instance->fw_outstanding, (-1));
34759440SSusan.Scheufele@Sun.COM 
347610600SYu.Wu@Sun.COM 			(void) mrsas_common_check(instance, cmd);
347710600SYu.Wu@Sun.COM 
34789440SSusan.Scheufele@Sun.COM 			if (acmd->cmd_dmahandle) {
34799440SSusan.Scheufele@Sun.COM 				if (mrsas_check_dma_handle(
34809440SSusan.Scheufele@Sun.COM 				    acmd->cmd_dmahandle) != DDI_SUCCESS) {
34819440SSusan.Scheufele@Sun.COM 					ddi_fm_service_impact(instance->dip,
34829440SSusan.Scheufele@Sun.COM 					    DDI_SERVICE_UNAFFECTED);
34839440SSusan.Scheufele@Sun.COM 					pkt->pkt_reason = CMD_TRAN_ERR;
34849440SSusan.Scheufele@Sun.COM 					pkt->pkt_statistics = 0;
34859440SSusan.Scheufele@Sun.COM 				}
34869440SSusan.Scheufele@Sun.COM 			}
34879440SSusan.Scheufele@Sun.COM 
34889440SSusan.Scheufele@Sun.COM 			/* Call the callback routine */
34899440SSusan.Scheufele@Sun.COM 			if (((pkt->pkt_flags & FLAG_NOINTR) == 0) &&
34909440SSusan.Scheufele@Sun.COM 			    pkt->pkt_comp) {
349112244SYu.Wu@Sun.COM 
349212244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_NOTE, "mrsas_softintr: "
349312244SYu.Wu@Sun.COM 				    "posting to scsa cmd %p index %x pkt %p "
349412244SYu.Wu@Sun.COM 				    "time %llx", (void *)cmd, cmd->index,
349512244SYu.Wu@Sun.COM 				    (void *)pkt, gethrtime()));
34969440SSusan.Scheufele@Sun.COM 				(*pkt->pkt_comp)(pkt);
349712244SYu.Wu@Sun.COM 
34989440SSusan.Scheufele@Sun.COM 			}
349912244SYu.Wu@Sun.COM 			return_mfi_pkt(instance, cmd);
35009440SSusan.Scheufele@Sun.COM 			break;
35019440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_SMP:
35029440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_STP:
35039440SSusan.Scheufele@Sun.COM 			complete_cmd_in_sync_mode(instance, cmd);
35049440SSusan.Scheufele@Sun.COM 			break;
35059440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_DCMD:
35069440SSusan.Scheufele@Sun.COM 			/* see if got an event notification */
35079440SSusan.Scheufele@Sun.COM 			if (ddi_get32(cmd->frame_dma_obj.acc_handle,
35089440SSusan.Scheufele@Sun.COM 			    &cmd->frame->dcmd.opcode) ==
35099440SSusan.Scheufele@Sun.COM 			    MR_DCMD_CTRL_EVENT_WAIT) {
35109440SSusan.Scheufele@Sun.COM 				if ((instance->aen_cmd == cmd) &&
35119440SSusan.Scheufele@Sun.COM 				    (instance->aen_cmd->abort_aen)) {
35129440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
35139440SSusan.Scheufele@Sun.COM 					    "mrsas_softintr: "
35149440SSusan.Scheufele@Sun.COM 					    "aborted_aen returned"));
35159440SSusan.Scheufele@Sun.COM 				} else {
35169440SSusan.Scheufele@Sun.COM 					atomic_add_16(&instance->fw_outstanding,
35179440SSusan.Scheufele@Sun.COM 					    (-1));
35189440SSusan.Scheufele@Sun.COM 					service_mfi_aen(instance, cmd);
35199440SSusan.Scheufele@Sun.COM 				}
35209440SSusan.Scheufele@Sun.COM 			} else {
35219440SSusan.Scheufele@Sun.COM 				complete_cmd_in_sync_mode(instance, cmd);
35229440SSusan.Scheufele@Sun.COM 			}
35239440SSusan.Scheufele@Sun.COM 
35249440SSusan.Scheufele@Sun.COM 			break;
35259440SSusan.Scheufele@Sun.COM 		case MFI_CMD_OP_ABORT:
35269440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "MFI_CMD_OP_ABORT complete"));
35279440SSusan.Scheufele@Sun.COM 			/*
35289440SSusan.Scheufele@Sun.COM 			 * MFI_CMD_OP_ABORT successfully completed
35299440SSusan.Scheufele@Sun.COM 			 * in the synchronous mode
35309440SSusan.Scheufele@Sun.COM 			 */
35319440SSusan.Scheufele@Sun.COM 			complete_cmd_in_sync_mode(instance, cmd);
35329440SSusan.Scheufele@Sun.COM 			break;
35339440SSusan.Scheufele@Sun.COM 		default:
35349440SSusan.Scheufele@Sun.COM 			mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
35359440SSusan.Scheufele@Sun.COM 			ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
35369440SSusan.Scheufele@Sun.COM 
35379440SSusan.Scheufele@Sun.COM 			if (cmd->pkt != NULL) {
35389440SSusan.Scheufele@Sun.COM 				pkt = cmd->pkt;
35399440SSusan.Scheufele@Sun.COM 				if (((pkt->pkt_flags & FLAG_NOINTR) == 0) &&
35409440SSusan.Scheufele@Sun.COM 				    pkt->pkt_comp) {
354112244SYu.Wu@Sun.COM 
354212244SYu.Wu@Sun.COM 					con_log(CL_ANN1, (CE_CONT, "posting to "
354312244SYu.Wu@Sun.COM 					    "scsa cmd %p index %x pkt %p"
354412244SYu.Wu@Sun.COM 					    "time %llx, default ", (void *)cmd,
354512244SYu.Wu@Sun.COM 					    cmd->index, (void *)pkt,
354612244SYu.Wu@Sun.COM 					    gethrtime()));
354712244SYu.Wu@Sun.COM 
35489440SSusan.Scheufele@Sun.COM 					(*pkt->pkt_comp)(pkt);
354912244SYu.Wu@Sun.COM 
35509440SSusan.Scheufele@Sun.COM 				}
35519440SSusan.Scheufele@Sun.COM 			}
35529440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "Cmd type unknown !"));
35539440SSusan.Scheufele@Sun.COM 			break;
35549440SSusan.Scheufele@Sun.COM 		}
35559440SSusan.Scheufele@Sun.COM 	}
35569440SSusan.Scheufele@Sun.COM 
35579440SSusan.Scheufele@Sun.COM 	instance->softint_running = 0;
35589440SSusan.Scheufele@Sun.COM 
35599440SSusan.Scheufele@Sun.COM 	return (DDI_INTR_CLAIMED);
35609440SSusan.Scheufele@Sun.COM }
35619440SSusan.Scheufele@Sun.COM 
35629440SSusan.Scheufele@Sun.COM /*
35639440SSusan.Scheufele@Sun.COM  * mrsas_alloc_dma_obj
35649440SSusan.Scheufele@Sun.COM  *
35659440SSusan.Scheufele@Sun.COM  * Allocate the memory and other resources for an dma object.
35669440SSusan.Scheufele@Sun.COM  */
35679440SSusan.Scheufele@Sun.COM static int
mrsas_alloc_dma_obj(struct mrsas_instance * instance,dma_obj_t * obj,uchar_t endian_flags)35689440SSusan.Scheufele@Sun.COM mrsas_alloc_dma_obj(struct mrsas_instance *instance, dma_obj_t *obj,
35699440SSusan.Scheufele@Sun.COM     uchar_t endian_flags)
35709440SSusan.Scheufele@Sun.COM {
35719440SSusan.Scheufele@Sun.COM 	int	i;
35729440SSusan.Scheufele@Sun.COM 	size_t	alen = 0;
35739440SSusan.Scheufele@Sun.COM 	uint_t	cookie_cnt;
35749440SSusan.Scheufele@Sun.COM 	struct ddi_device_acc_attr tmp_endian_attr;
35759440SSusan.Scheufele@Sun.COM 
35769440SSusan.Scheufele@Sun.COM 	tmp_endian_attr = endian_attr;
35779440SSusan.Scheufele@Sun.COM 	tmp_endian_attr.devacc_attr_endian_flags = endian_flags;
357811236SStephen.Hanson@Sun.COM 	tmp_endian_attr.devacc_attr_access = DDI_DEFAULT_ACC;
35799440SSusan.Scheufele@Sun.COM 
35809440SSusan.Scheufele@Sun.COM 	i = ddi_dma_alloc_handle(instance->dip, &obj->dma_attr,
35819440SSusan.Scheufele@Sun.COM 	    DDI_DMA_SLEEP, NULL, &obj->dma_handle);
35829440SSusan.Scheufele@Sun.COM 	if (i != DDI_SUCCESS) {
35839440SSusan.Scheufele@Sun.COM 
35849440SSusan.Scheufele@Sun.COM 		switch (i) {
35859440SSusan.Scheufele@Sun.COM 			case DDI_DMA_BADATTR :
35869440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
35879440SSusan.Scheufele@Sun.COM 				"Failed ddi_dma_alloc_handle- Bad attribute"));
35889440SSusan.Scheufele@Sun.COM 				break;
35899440SSusan.Scheufele@Sun.COM 			case DDI_DMA_NORESOURCES :
35909440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
35919440SSusan.Scheufele@Sun.COM 				"Failed ddi_dma_alloc_handle- No Resources"));
35929440SSusan.Scheufele@Sun.COM 				break;
35939440SSusan.Scheufele@Sun.COM 			default :
35949440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
35959440SSusan.Scheufele@Sun.COM 				"Failed ddi_dma_alloc_handle: "
35969440SSusan.Scheufele@Sun.COM 				"unknown status %d", i));
35979440SSusan.Scheufele@Sun.COM 				break;
35989440SSusan.Scheufele@Sun.COM 		}
35999440SSusan.Scheufele@Sun.COM 
36009440SSusan.Scheufele@Sun.COM 		return (-1);
36019440SSusan.Scheufele@Sun.COM 	}
36029440SSusan.Scheufele@Sun.COM 
36039440SSusan.Scheufele@Sun.COM 	if ((ddi_dma_mem_alloc(obj->dma_handle, obj->size, &tmp_endian_attr,
36049440SSusan.Scheufele@Sun.COM 	    DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL,
36059440SSusan.Scheufele@Sun.COM 	    &obj->buffer, &alen, &obj->acc_handle) != DDI_SUCCESS) ||
36069440SSusan.Scheufele@Sun.COM 	    alen < obj->size) {
36079440SSusan.Scheufele@Sun.COM 
36089440SSusan.Scheufele@Sun.COM 		ddi_dma_free_handle(&obj->dma_handle);
36099440SSusan.Scheufele@Sun.COM 
36109440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "Failed : ddi_dma_mem_alloc"));
36119440SSusan.Scheufele@Sun.COM 
36129440SSusan.Scheufele@Sun.COM 		return (-1);
36139440SSusan.Scheufele@Sun.COM 	}
36149440SSusan.Scheufele@Sun.COM 
36159440SSusan.Scheufele@Sun.COM 	if (ddi_dma_addr_bind_handle(obj->dma_handle, NULL, obj->buffer,
36169440SSusan.Scheufele@Sun.COM 	    obj->size, DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP,
36179440SSusan.Scheufele@Sun.COM 	    NULL, &obj->dma_cookie[0], &cookie_cnt) != DDI_SUCCESS) {
36189440SSusan.Scheufele@Sun.COM 
36199440SSusan.Scheufele@Sun.COM 		ddi_dma_mem_free(&obj->acc_handle);
36209440SSusan.Scheufele@Sun.COM 		ddi_dma_free_handle(&obj->dma_handle);
36219440SSusan.Scheufele@Sun.COM 
36229440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "Failed : ddi_dma_addr_bind_handle"));
36239440SSusan.Scheufele@Sun.COM 
36249440SSusan.Scheufele@Sun.COM 		return (-1);
36259440SSusan.Scheufele@Sun.COM 	}
36269440SSusan.Scheufele@Sun.COM 
36279440SSusan.Scheufele@Sun.COM 	if (mrsas_check_dma_handle(obj->dma_handle) != DDI_SUCCESS) {
36289440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
36299440SSusan.Scheufele@Sun.COM 		return (-1);
36309440SSusan.Scheufele@Sun.COM 	}
36319440SSusan.Scheufele@Sun.COM 
36329440SSusan.Scheufele@Sun.COM 	if (mrsas_check_acc_handle(obj->acc_handle) != DDI_SUCCESS) {
36339440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
36349440SSusan.Scheufele@Sun.COM 		return (-1);
36359440SSusan.Scheufele@Sun.COM 	}
36369440SSusan.Scheufele@Sun.COM 
36379440SSusan.Scheufele@Sun.COM 	return (cookie_cnt);
36389440SSusan.Scheufele@Sun.COM }
36399440SSusan.Scheufele@Sun.COM 
36409440SSusan.Scheufele@Sun.COM /*
36419440SSusan.Scheufele@Sun.COM  * mrsas_free_dma_obj(struct mrsas_instance *, dma_obj_t)
36429440SSusan.Scheufele@Sun.COM  *
36439440SSusan.Scheufele@Sun.COM  * De-allocate the memory and other resources for an dma object, which must
36449440SSusan.Scheufele@Sun.COM  * have been alloated by a previous call to mrsas_alloc_dma_obj()
36459440SSusan.Scheufele@Sun.COM  */
36469440SSusan.Scheufele@Sun.COM static int
mrsas_free_dma_obj(struct mrsas_instance * instance,dma_obj_t obj)36479440SSusan.Scheufele@Sun.COM mrsas_free_dma_obj(struct mrsas_instance *instance, dma_obj_t obj)
36489440SSusan.Scheufele@Sun.COM {
36499440SSusan.Scheufele@Sun.COM 
36509440SSusan.Scheufele@Sun.COM 	if (mrsas_check_dma_handle(obj.dma_handle) != DDI_SUCCESS) {
36519440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
36529440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
36539440SSusan.Scheufele@Sun.COM 	}
36549440SSusan.Scheufele@Sun.COM 
36559440SSusan.Scheufele@Sun.COM 	if (mrsas_check_acc_handle(obj.acc_handle) != DDI_SUCCESS) {
36569440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
36579440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
36589440SSusan.Scheufele@Sun.COM 	}
36599440SSusan.Scheufele@Sun.COM 
36609440SSusan.Scheufele@Sun.COM 	(void) ddi_dma_unbind_handle(obj.dma_handle);
36619440SSusan.Scheufele@Sun.COM 	ddi_dma_mem_free(&obj.acc_handle);
36629440SSusan.Scheufele@Sun.COM 	ddi_dma_free_handle(&obj.dma_handle);
36639440SSusan.Scheufele@Sun.COM 
36649440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
36659440SSusan.Scheufele@Sun.COM }
36669440SSusan.Scheufele@Sun.COM 
36679440SSusan.Scheufele@Sun.COM /*
36689440SSusan.Scheufele@Sun.COM  * mrsas_dma_alloc(instance_t *, struct scsi_pkt *, struct buf *,
36699440SSusan.Scheufele@Sun.COM  * int, int (*)())
36709440SSusan.Scheufele@Sun.COM  *
36719440SSusan.Scheufele@Sun.COM  * Allocate dma resources for a new scsi command
36729440SSusan.Scheufele@Sun.COM  */
36739440SSusan.Scheufele@Sun.COM static int
mrsas_dma_alloc(struct mrsas_instance * instance,struct scsi_pkt * pkt,struct buf * bp,int flags,int (* callback)())36749440SSusan.Scheufele@Sun.COM mrsas_dma_alloc(struct mrsas_instance *instance, struct scsi_pkt *pkt,
36759440SSusan.Scheufele@Sun.COM     struct buf *bp, int flags, int (*callback)())
36769440SSusan.Scheufele@Sun.COM {
36779440SSusan.Scheufele@Sun.COM 	int	dma_flags;
36789440SSusan.Scheufele@Sun.COM 	int	(*cb)(caddr_t);
36799440SSusan.Scheufele@Sun.COM 	int	i;
36809440SSusan.Scheufele@Sun.COM 
36819440SSusan.Scheufele@Sun.COM 	ddi_dma_attr_t	tmp_dma_attr = mrsas_generic_dma_attr;
36829440SSusan.Scheufele@Sun.COM 	struct scsa_cmd	*acmd = PKT2CMD(pkt);
36839440SSusan.Scheufele@Sun.COM 
36849440SSusan.Scheufele@Sun.COM 	acmd->cmd_buf = bp;
36859440SSusan.Scheufele@Sun.COM 
36869440SSusan.Scheufele@Sun.COM 	if (bp->b_flags & B_READ) {
36879440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags &= ~CFLAG_DMASEND;
36889440SSusan.Scheufele@Sun.COM 		dma_flags = DDI_DMA_READ;
36899440SSusan.Scheufele@Sun.COM 	} else {
36909440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags |= CFLAG_DMASEND;
36919440SSusan.Scheufele@Sun.COM 		dma_flags = DDI_DMA_WRITE;
36929440SSusan.Scheufele@Sun.COM 	}
36939440SSusan.Scheufele@Sun.COM 
36949440SSusan.Scheufele@Sun.COM 	if (flags & PKT_CONSISTENT) {
36959440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags |= CFLAG_CONSISTENT;
36969440SSusan.Scheufele@Sun.COM 		dma_flags |= DDI_DMA_CONSISTENT;
36979440SSusan.Scheufele@Sun.COM 	}
36989440SSusan.Scheufele@Sun.COM 
36999440SSusan.Scheufele@Sun.COM 	if (flags & PKT_DMA_PARTIAL) {
37009440SSusan.Scheufele@Sun.COM 		dma_flags |= DDI_DMA_PARTIAL;
37019440SSusan.Scheufele@Sun.COM 	}
37029440SSusan.Scheufele@Sun.COM 
37039440SSusan.Scheufele@Sun.COM 	dma_flags |= DDI_DMA_REDZONE;
37049440SSusan.Scheufele@Sun.COM 
37059440SSusan.Scheufele@Sun.COM 	cb = (callback == NULL_FUNC) ? DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
37069440SSusan.Scheufele@Sun.COM 
37079440SSusan.Scheufele@Sun.COM 	tmp_dma_attr.dma_attr_sgllen = instance->max_num_sge;
37089440SSusan.Scheufele@Sun.COM 	tmp_dma_attr.dma_attr_addr_hi = 0xffffffffffffffffull;
37099440SSusan.Scheufele@Sun.COM 
37109440SSusan.Scheufele@Sun.COM 	if ((i = ddi_dma_alloc_handle(instance->dip, &tmp_dma_attr,
37119440SSusan.Scheufele@Sun.COM 	    cb, 0, &acmd->cmd_dmahandle)) != DDI_SUCCESS) {
37129440SSusan.Scheufele@Sun.COM 		switch (i) {
37139440SSusan.Scheufele@Sun.COM 		case DDI_DMA_BADATTR:
37149440SSusan.Scheufele@Sun.COM 			bioerror(bp, EFAULT);
37159440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
37169440SSusan.Scheufele@Sun.COM 
37179440SSusan.Scheufele@Sun.COM 		case DDI_DMA_NORESOURCES:
37189440SSusan.Scheufele@Sun.COM 			bioerror(bp, 0);
37199440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
37209440SSusan.Scheufele@Sun.COM 
37219440SSusan.Scheufele@Sun.COM 		default:
37229440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_PANIC, "ddi_dma_alloc_handle: "
37239440SSusan.Scheufele@Sun.COM 			    "impossible result (0x%x)", i));
37249440SSusan.Scheufele@Sun.COM 			bioerror(bp, EFAULT);
37259440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
37269440SSusan.Scheufele@Sun.COM 		}
37279440SSusan.Scheufele@Sun.COM 	}
37289440SSusan.Scheufele@Sun.COM 
37299440SSusan.Scheufele@Sun.COM 	i = ddi_dma_buf_bind_handle(acmd->cmd_dmahandle, bp, dma_flags,
37309440SSusan.Scheufele@Sun.COM 	    cb, 0, &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies);
37319440SSusan.Scheufele@Sun.COM 
37329440SSusan.Scheufele@Sun.COM 	switch (i) {
37339440SSusan.Scheufele@Sun.COM 	case DDI_DMA_PARTIAL_MAP:
37349440SSusan.Scheufele@Sun.COM 		if ((dma_flags & DDI_DMA_PARTIAL) == 0) {
37359440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: "
37369440SSusan.Scheufele@Sun.COM 			    "DDI_DMA_PARTIAL_MAP impossible"));
37379440SSusan.Scheufele@Sun.COM 			goto no_dma_cookies;
37389440SSusan.Scheufele@Sun.COM 		}
37399440SSusan.Scheufele@Sun.COM 
37409440SSusan.Scheufele@Sun.COM 		if (ddi_dma_numwin(acmd->cmd_dmahandle, &acmd->cmd_nwin) ==
37419440SSusan.Scheufele@Sun.COM 		    DDI_FAILURE) {
37429440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_PANIC, "ddi_dma_numwin failed"));
37439440SSusan.Scheufele@Sun.COM 			goto no_dma_cookies;
37449440SSusan.Scheufele@Sun.COM 		}
37459440SSusan.Scheufele@Sun.COM 
37469440SSusan.Scheufele@Sun.COM 		if (ddi_dma_getwin(acmd->cmd_dmahandle, acmd->cmd_curwin,
37479440SSusan.Scheufele@Sun.COM 		    &acmd->cmd_dma_offset, &acmd->cmd_dma_len,
37489440SSusan.Scheufele@Sun.COM 		    &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies) ==
37499440SSusan.Scheufele@Sun.COM 		    DDI_FAILURE) {
37509440SSusan.Scheufele@Sun.COM 
37519440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_PANIC, "ddi_dma_getwin failed"));
37529440SSusan.Scheufele@Sun.COM 			goto no_dma_cookies;
37539440SSusan.Scheufele@Sun.COM 		}
37549440SSusan.Scheufele@Sun.COM 
37559440SSusan.Scheufele@Sun.COM 		goto get_dma_cookies;
37569440SSusan.Scheufele@Sun.COM 	case DDI_DMA_MAPPED:
37579440SSusan.Scheufele@Sun.COM 		acmd->cmd_nwin = 1;
37589440SSusan.Scheufele@Sun.COM 		acmd->cmd_dma_len = 0;
37599440SSusan.Scheufele@Sun.COM 		acmd->cmd_dma_offset = 0;
37609440SSusan.Scheufele@Sun.COM 
37619440SSusan.Scheufele@Sun.COM get_dma_cookies:
37629440SSusan.Scheufele@Sun.COM 		i = 0;
37639440SSusan.Scheufele@Sun.COM 		acmd->cmd_dmacount = 0;
37649440SSusan.Scheufele@Sun.COM 		for (;;) {
37659440SSusan.Scheufele@Sun.COM 			acmd->cmd_dmacount +=
37669440SSusan.Scheufele@Sun.COM 			    acmd->cmd_dmacookies[i++].dmac_size;
37679440SSusan.Scheufele@Sun.COM 
37689440SSusan.Scheufele@Sun.COM 			if (i == instance->max_num_sge ||
37699440SSusan.Scheufele@Sun.COM 			    i == acmd->cmd_ncookies)
37709440SSusan.Scheufele@Sun.COM 				break;
37719440SSusan.Scheufele@Sun.COM 
37729440SSusan.Scheufele@Sun.COM 			ddi_dma_nextcookie(acmd->cmd_dmahandle,
37739440SSusan.Scheufele@Sun.COM 			    &acmd->cmd_dmacookies[i]);
37749440SSusan.Scheufele@Sun.COM 		}
37759440SSusan.Scheufele@Sun.COM 
37769440SSusan.Scheufele@Sun.COM 		acmd->cmd_cookie = i;
37779440SSusan.Scheufele@Sun.COM 		acmd->cmd_cookiecnt = i;
37789440SSusan.Scheufele@Sun.COM 
37799440SSusan.Scheufele@Sun.COM 		acmd->cmd_flags |= CFLAG_DMAVALID;
37809440SSusan.Scheufele@Sun.COM 
37819440SSusan.Scheufele@Sun.COM 		if (bp->b_bcount >= acmd->cmd_dmacount) {
37829440SSusan.Scheufele@Sun.COM 			pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount;
37839440SSusan.Scheufele@Sun.COM 		} else {
37849440SSusan.Scheufele@Sun.COM 			pkt->pkt_resid = 0;
37859440SSusan.Scheufele@Sun.COM 		}
37869440SSusan.Scheufele@Sun.COM 
37879440SSusan.Scheufele@Sun.COM 		return (DDI_SUCCESS);
37889440SSusan.Scheufele@Sun.COM 	case DDI_DMA_NORESOURCES:
37899440SSusan.Scheufele@Sun.COM 		bioerror(bp, 0);
37909440SSusan.Scheufele@Sun.COM 		break;
37919440SSusan.Scheufele@Sun.COM 	case DDI_DMA_NOMAPPING:
37929440SSusan.Scheufele@Sun.COM 		bioerror(bp, EFAULT);
37939440SSusan.Scheufele@Sun.COM 		break;
37949440SSusan.Scheufele@Sun.COM 	case DDI_DMA_TOOBIG:
37959440SSusan.Scheufele@Sun.COM 		bioerror(bp, EINVAL);
37969440SSusan.Scheufele@Sun.COM 		break;
37979440SSusan.Scheufele@Sun.COM 	case DDI_DMA_INUSE:
37989440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle:"
37999440SSusan.Scheufele@Sun.COM 		    " DDI_DMA_INUSE impossible"));
38009440SSusan.Scheufele@Sun.COM 		break;
38019440SSusan.Scheufele@Sun.COM 	default:
38029440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: "
38039440SSusan.Scheufele@Sun.COM 		    "impossible result (0x%x)", i));
38049440SSusan.Scheufele@Sun.COM 		break;
38059440SSusan.Scheufele@Sun.COM 	}
38069440SSusan.Scheufele@Sun.COM 
38079440SSusan.Scheufele@Sun.COM no_dma_cookies:
38089440SSusan.Scheufele@Sun.COM 	ddi_dma_free_handle(&acmd->cmd_dmahandle);
38099440SSusan.Scheufele@Sun.COM 	acmd->cmd_dmahandle = NULL;
38109440SSusan.Scheufele@Sun.COM 	acmd->cmd_flags &= ~CFLAG_DMAVALID;
38119440SSusan.Scheufele@Sun.COM 	return (DDI_FAILURE);
38129440SSusan.Scheufele@Sun.COM }
38139440SSusan.Scheufele@Sun.COM 
38149440SSusan.Scheufele@Sun.COM /*
38159440SSusan.Scheufele@Sun.COM  * mrsas_dma_move(struct mrsas_instance *, struct scsi_pkt *, struct buf *)
38169440SSusan.Scheufele@Sun.COM  *
38179440SSusan.Scheufele@Sun.COM  * move dma resources to next dma window
38189440SSusan.Scheufele@Sun.COM  *
38199440SSusan.Scheufele@Sun.COM  */
38209440SSusan.Scheufele@Sun.COM static int
mrsas_dma_move(struct mrsas_instance * instance,struct scsi_pkt * pkt,struct buf * bp)38219440SSusan.Scheufele@Sun.COM mrsas_dma_move(struct mrsas_instance *instance, struct scsi_pkt *pkt,
38229440SSusan.Scheufele@Sun.COM     struct buf *bp)
38239440SSusan.Scheufele@Sun.COM {
38249440SSusan.Scheufele@Sun.COM 	int	i = 0;
38259440SSusan.Scheufele@Sun.COM 
38269440SSusan.Scheufele@Sun.COM 	struct scsa_cmd	*acmd = PKT2CMD(pkt);
38279440SSusan.Scheufele@Sun.COM 
38289440SSusan.Scheufele@Sun.COM 	/*
38299440SSusan.Scheufele@Sun.COM 	 * If there are no more cookies remaining in this window,
38309440SSusan.Scheufele@Sun.COM 	 * must move to the next window first.
38319440SSusan.Scheufele@Sun.COM 	 */
38329440SSusan.Scheufele@Sun.COM 	if (acmd->cmd_cookie == acmd->cmd_ncookies) {
38339440SSusan.Scheufele@Sun.COM 		if (acmd->cmd_curwin == acmd->cmd_nwin && acmd->cmd_nwin == 1) {
38349440SSusan.Scheufele@Sun.COM 			return (DDI_SUCCESS);
38359440SSusan.Scheufele@Sun.COM 		}
38369440SSusan.Scheufele@Sun.COM 
38379440SSusan.Scheufele@Sun.COM 		/* at last window, cannot move */
38389440SSusan.Scheufele@Sun.COM 		if (++acmd->cmd_curwin >= acmd->cmd_nwin) {
38399440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
38409440SSusan.Scheufele@Sun.COM 		}
38419440SSusan.Scheufele@Sun.COM 
38429440SSusan.Scheufele@Sun.COM 		if (ddi_dma_getwin(acmd->cmd_dmahandle, acmd->cmd_curwin,
38439440SSusan.Scheufele@Sun.COM 		    &acmd->cmd_dma_offset, &acmd->cmd_dma_len,
38449440SSusan.Scheufele@Sun.COM 		    &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies) ==
38459440SSusan.Scheufele@Sun.COM 		    DDI_FAILURE) {
38469440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
38479440SSusan.Scheufele@Sun.COM 		}
38489440SSusan.Scheufele@Sun.COM 
38499440SSusan.Scheufele@Sun.COM 		acmd->cmd_cookie = 0;
38509440SSusan.Scheufele@Sun.COM 	} else {
38519440SSusan.Scheufele@Sun.COM 		/* still more cookies in this window - get the next one */
38529440SSusan.Scheufele@Sun.COM 		ddi_dma_nextcookie(acmd->cmd_dmahandle,
38539440SSusan.Scheufele@Sun.COM 		    &acmd->cmd_dmacookies[0]);
38549440SSusan.Scheufele@Sun.COM 	}
38559440SSusan.Scheufele@Sun.COM 
38569440SSusan.Scheufele@Sun.COM 	/* get remaining cookies in this window, up to our maximum */
38579440SSusan.Scheufele@Sun.COM 	for (;;) {
38589440SSusan.Scheufele@Sun.COM 		acmd->cmd_dmacount += acmd->cmd_dmacookies[i++].dmac_size;
38599440SSusan.Scheufele@Sun.COM 		acmd->cmd_cookie++;
38609440SSusan.Scheufele@Sun.COM 
38619440SSusan.Scheufele@Sun.COM 		if (i == instance->max_num_sge ||
38629440SSusan.Scheufele@Sun.COM 		    acmd->cmd_cookie == acmd->cmd_ncookies) {
38639440SSusan.Scheufele@Sun.COM 			break;
38649440SSusan.Scheufele@Sun.COM 		}
38659440SSusan.Scheufele@Sun.COM 
38669440SSusan.Scheufele@Sun.COM 		ddi_dma_nextcookie(acmd->cmd_dmahandle,
38679440SSusan.Scheufele@Sun.COM 		    &acmd->cmd_dmacookies[i]);
38689440SSusan.Scheufele@Sun.COM 	}
38699440SSusan.Scheufele@Sun.COM 
38709440SSusan.Scheufele@Sun.COM 	acmd->cmd_cookiecnt = i;
38719440SSusan.Scheufele@Sun.COM 
38729440SSusan.Scheufele@Sun.COM 	if (bp->b_bcount >= acmd->cmd_dmacount) {
38739440SSusan.Scheufele@Sun.COM 		pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount;
38749440SSusan.Scheufele@Sun.COM 	} else {
38759440SSusan.Scheufele@Sun.COM 		pkt->pkt_resid = 0;
38769440SSusan.Scheufele@Sun.COM 	}
38779440SSusan.Scheufele@Sun.COM 
38789440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
38799440SSusan.Scheufele@Sun.COM }
38809440SSusan.Scheufele@Sun.COM 
38819440SSusan.Scheufele@Sun.COM /*
38829440SSusan.Scheufele@Sun.COM  * build_cmd
38839440SSusan.Scheufele@Sun.COM  */
38849440SSusan.Scheufele@Sun.COM static struct mrsas_cmd *
build_cmd(struct mrsas_instance * instance,struct scsi_address * ap,struct scsi_pkt * pkt,uchar_t * cmd_done)38859440SSusan.Scheufele@Sun.COM build_cmd(struct mrsas_instance *instance, struct scsi_address *ap,
38869440SSusan.Scheufele@Sun.COM     struct scsi_pkt *pkt, uchar_t *cmd_done)
38879440SSusan.Scheufele@Sun.COM {
38889440SSusan.Scheufele@Sun.COM 	uint16_t	flags = 0;
38899440SSusan.Scheufele@Sun.COM 	uint32_t	i;
38909440SSusan.Scheufele@Sun.COM 	uint32_t 	context;
38919440SSusan.Scheufele@Sun.COM 	uint32_t	sge_bytes;
38929440SSusan.Scheufele@Sun.COM 	ddi_acc_handle_t acc_handle;
38939440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd		*cmd;
38949440SSusan.Scheufele@Sun.COM 	struct mrsas_sge64		*mfi_sgl;
389511856SYu.Wu@Sun.COM 	struct mrsas_sge_ieee		*mfi_sgl_ieee;
38969440SSusan.Scheufele@Sun.COM 	struct scsa_cmd			*acmd = PKT2CMD(pkt);
38979440SSusan.Scheufele@Sun.COM 	struct mrsas_pthru_frame 	*pthru;
38989440SSusan.Scheufele@Sun.COM 	struct mrsas_io_frame		*ldio;
38999440SSusan.Scheufele@Sun.COM 
39009440SSusan.Scheufele@Sun.COM 	/* find out if this is logical or physical drive command.  */
39019440SSusan.Scheufele@Sun.COM 	acmd->islogical = MRDRV_IS_LOGICAL(ap);
39029440SSusan.Scheufele@Sun.COM 	acmd->device_id = MAP_DEVICE_ID(instance, ap);
39039440SSusan.Scheufele@Sun.COM 	*cmd_done = 0;
39049440SSusan.Scheufele@Sun.COM 
39059440SSusan.Scheufele@Sun.COM 	/* get the command packet */
39069440SSusan.Scheufele@Sun.COM 	if (!(cmd = get_mfi_pkt(instance))) {
390710820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(build_cmd_mfi_err, uint16_t,
390810820SSusan.Scheufele@Sun.COM 		    instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
39099440SSusan.Scheufele@Sun.COM 		return (NULL);
39109440SSusan.Scheufele@Sun.COM 	}
39119440SSusan.Scheufele@Sun.COM 
391212244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
391312244SYu.Wu@Sun.COM 
39149440SSusan.Scheufele@Sun.COM 	acc_handle = cmd->frame_dma_obj.acc_handle;
39159440SSusan.Scheufele@Sun.COM 
39169440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
39179440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
39189440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &cmd->frame->hdr.context, cmd->index);
39199440SSusan.Scheufele@Sun.COM 
39209440SSusan.Scheufele@Sun.COM 	cmd->pkt = pkt;
39219440SSusan.Scheufele@Sun.COM 	cmd->cmd = acmd;
392210820SSusan.Scheufele@Sun.COM 	DTRACE_PROBE3(build_cmds, uint8_t, pkt->pkt_cdbp[0],
392310820SSusan.Scheufele@Sun.COM 	    ulong_t, acmd->cmd_dmacount, ulong_t, acmd->cmd_dma_len);
39249440SSusan.Scheufele@Sun.COM 
39259440SSusan.Scheufele@Sun.COM 	/* lets get the command directions */
39269440SSusan.Scheufele@Sun.COM 	if (acmd->cmd_flags & CFLAG_DMASEND) {
39279440SSusan.Scheufele@Sun.COM 		flags = MFI_FRAME_DIR_WRITE;
39289440SSusan.Scheufele@Sun.COM 
39299440SSusan.Scheufele@Sun.COM 		if (acmd->cmd_flags & CFLAG_CONSISTENT) {
39309440SSusan.Scheufele@Sun.COM 			(void) ddi_dma_sync(acmd->cmd_dmahandle,
39319440SSusan.Scheufele@Sun.COM 			    acmd->cmd_dma_offset, acmd->cmd_dma_len,
39329440SSusan.Scheufele@Sun.COM 			    DDI_DMA_SYNC_FORDEV);
39339440SSusan.Scheufele@Sun.COM 		}
39349440SSusan.Scheufele@Sun.COM 	} else if (acmd->cmd_flags & ~CFLAG_DMASEND) {
39359440SSusan.Scheufele@Sun.COM 		flags = MFI_FRAME_DIR_READ;
39369440SSusan.Scheufele@Sun.COM 
39379440SSusan.Scheufele@Sun.COM 		if (acmd->cmd_flags & CFLAG_CONSISTENT) {
39389440SSusan.Scheufele@Sun.COM 			(void) ddi_dma_sync(acmd->cmd_dmahandle,
39399440SSusan.Scheufele@Sun.COM 			    acmd->cmd_dma_offset, acmd->cmd_dma_len,
39409440SSusan.Scheufele@Sun.COM 			    DDI_DMA_SYNC_FORCPU);
39419440SSusan.Scheufele@Sun.COM 		}
39429440SSusan.Scheufele@Sun.COM 	} else {
39439440SSusan.Scheufele@Sun.COM 		flags = MFI_FRAME_DIR_NONE;
39449440SSusan.Scheufele@Sun.COM 	}
39459440SSusan.Scheufele@Sun.COM 
394611856SYu.Wu@Sun.COM 	if (instance->flag_ieee) {
394711856SYu.Wu@Sun.COM 		flags |= MFI_FRAME_IEEE;
394811856SYu.Wu@Sun.COM 	}
39499440SSusan.Scheufele@Sun.COM 	flags |= MFI_FRAME_SGL64;
39509440SSusan.Scheufele@Sun.COM 
39519440SSusan.Scheufele@Sun.COM 	switch (pkt->pkt_cdbp[0]) {
39529440SSusan.Scheufele@Sun.COM 
39539440SSusan.Scheufele@Sun.COM 	/*
39549440SSusan.Scheufele@Sun.COM 	 * case SCMD_SYNCHRONIZE_CACHE:
39559440SSusan.Scheufele@Sun.COM 	 * 	flush_cache(instance);
39569440SSusan.Scheufele@Sun.COM 	 *	return_mfi_pkt(instance, cmd);
39579440SSusan.Scheufele@Sun.COM 	 *	*cmd_done = 1;
39589440SSusan.Scheufele@Sun.COM 	 *
39599440SSusan.Scheufele@Sun.COM 	 *	return (NULL);
39609440SSusan.Scheufele@Sun.COM 	 */
39619440SSusan.Scheufele@Sun.COM 
39629440SSusan.Scheufele@Sun.COM 	case SCMD_READ:
39639440SSusan.Scheufele@Sun.COM 	case SCMD_WRITE:
39649440SSusan.Scheufele@Sun.COM 	case SCMD_READ_G1:
39659440SSusan.Scheufele@Sun.COM 	case SCMD_WRITE_G1:
39669440SSusan.Scheufele@Sun.COM 		if (acmd->islogical) {
39679440SSusan.Scheufele@Sun.COM 			ldio = (struct mrsas_io_frame *)cmd->frame;
39689440SSusan.Scheufele@Sun.COM 
39699440SSusan.Scheufele@Sun.COM 			/*
39709440SSusan.Scheufele@Sun.COM 			 * preare the Logical IO frame:
39719440SSusan.Scheufele@Sun.COM 			 * 2nd bit is zero for all read cmds
39729440SSusan.Scheufele@Sun.COM 			 */
39739440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->cmd,
39749440SSusan.Scheufele@Sun.COM 			    (pkt->pkt_cdbp[0] & 0x02) ? MFI_CMD_OP_LD_WRITE
39759440SSusan.Scheufele@Sun.COM 			    : MFI_CMD_OP_LD_READ);
39769440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->cmd_status, 0x0);
39779440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->scsi_status, 0x0);
39789440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->target_id, acmd->device_id);
39799440SSusan.Scheufele@Sun.COM 			ddi_put16(acc_handle, &ldio->timeout, 0);
39809440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->reserved_0, 0);
39819440SSusan.Scheufele@Sun.COM 			ddi_put16(acc_handle, &ldio->pad_0, 0);
39829440SSusan.Scheufele@Sun.COM 			ddi_put16(acc_handle, &ldio->flags, flags);
39839440SSusan.Scheufele@Sun.COM 
39849440SSusan.Scheufele@Sun.COM 			/* Initialize sense Information */
39859440SSusan.Scheufele@Sun.COM 			bzero(cmd->sense, SENSE_LENGTH);
39869440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->sense_len, SENSE_LENGTH);
39879440SSusan.Scheufele@Sun.COM 			ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_hi, 0);
39889440SSusan.Scheufele@Sun.COM 			ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_lo,
39899440SSusan.Scheufele@Sun.COM 			    cmd->sense_phys_addr);
39909440SSusan.Scheufele@Sun.COM 			ddi_put32(acc_handle, &ldio->start_lba_hi, 0);
39919440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->access_byte,
39929440SSusan.Scheufele@Sun.COM 			    (acmd->cmd_cdblen != 6) ? pkt->pkt_cdbp[1] : 0);
39939440SSusan.Scheufele@Sun.COM 			ddi_put8(acc_handle, &ldio->sge_count,
39949440SSusan.Scheufele@Sun.COM 			    acmd->cmd_cookiecnt);
399511856SYu.Wu@Sun.COM 			if (instance->flag_ieee) {
399611856SYu.Wu@Sun.COM 				mfi_sgl_ieee =
399711856SYu.Wu@Sun.COM 				    (struct mrsas_sge_ieee *)&ldio->sgl;
399811856SYu.Wu@Sun.COM 			} else {
399911856SYu.Wu@Sun.COM 				mfi_sgl = (struct mrsas_sge64	*)&ldio->sgl;
400011856SYu.Wu@Sun.COM 			}
40019440SSusan.Scheufele@Sun.COM 
40029440SSusan.Scheufele@Sun.COM 			context = ddi_get32(acc_handle, &ldio->context);
40039440SSusan.Scheufele@Sun.COM 
40049440SSusan.Scheufele@Sun.COM 			if (acmd->cmd_cdblen == CDB_GROUP0) {
40059440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->lba_count, (
40069440SSusan.Scheufele@Sun.COM 				    (uint16_t)(pkt->pkt_cdbp[4])));
40079440SSusan.Scheufele@Sun.COM 
40089440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->start_lba_lo, (
40099440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[3])) |
40109440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[2]) << 8) |
40119440SSusan.Scheufele@Sun.COM 				    ((uint32_t)((pkt->pkt_cdbp[1]) & 0x1F)
40129440SSusan.Scheufele@Sun.COM 				    << 16)));
40139440SSusan.Scheufele@Sun.COM 			} else if (acmd->cmd_cdblen == CDB_GROUP1) {
40149440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->lba_count, (
40159440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[8])) |
40169440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[7]) << 8)));
40179440SSusan.Scheufele@Sun.COM 
40189440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->start_lba_lo, (
40199440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[5])) |
40209440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
40219440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
40229440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
40239440SSusan.Scheufele@Sun.COM 			} else if (acmd->cmd_cdblen == CDB_GROUP2) {
40249440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->lba_count, (
40259440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[9])) |
40269440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[8]) << 8) |
40279440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[7]) << 16) |
40289440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[6]) << 24)));
40299440SSusan.Scheufele@Sun.COM 
40309440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->start_lba_lo, (
40319440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[5])) |
40329440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
40339440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
40349440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
40359440SSusan.Scheufele@Sun.COM 			} else if (acmd->cmd_cdblen == CDB_GROUP3) {
40369440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->lba_count, (
40379440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[13])) |
40389440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[12]) << 8) |
40399440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[11]) << 16) |
40409440SSusan.Scheufele@Sun.COM 				    ((uint16_t)(pkt->pkt_cdbp[10]) << 24)));
40419440SSusan.Scheufele@Sun.COM 
40429440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->start_lba_lo, (
40439440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[9])) |
40449440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[8]) << 8) |
40459440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[7]) << 16) |
40469440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[6]) << 24)));
40479440SSusan.Scheufele@Sun.COM 
40489440SSusan.Scheufele@Sun.COM 				ddi_put32(acc_handle, &ldio->start_lba_lo, (
40499440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[5])) |
40509440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
40519440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
40529440SSusan.Scheufele@Sun.COM 				    ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
40539440SSusan.Scheufele@Sun.COM 			}
40549440SSusan.Scheufele@Sun.COM 
40559440SSusan.Scheufele@Sun.COM 			break;
40569440SSusan.Scheufele@Sun.COM 		}
40579440SSusan.Scheufele@Sun.COM 		/* fall through For all non-rd/wr cmds */
40589440SSusan.Scheufele@Sun.COM 	default:
40599440SSusan.Scheufele@Sun.COM 
40609440SSusan.Scheufele@Sun.COM 		switch (pkt->pkt_cdbp[0]) {
40619440SSusan.Scheufele@Sun.COM 		case SCMD_MODE_SENSE:
40629440SSusan.Scheufele@Sun.COM 		case SCMD_MODE_SENSE_G1: {
40639440SSusan.Scheufele@Sun.COM 			union scsi_cdb	*cdbp;
40649440SSusan.Scheufele@Sun.COM 			uint16_t	page_code;
40659440SSusan.Scheufele@Sun.COM 
40669440SSusan.Scheufele@Sun.COM 			cdbp = (void *)pkt->pkt_cdbp;
40679440SSusan.Scheufele@Sun.COM 			page_code = (uint16_t)cdbp->cdb_un.sg.scsi[0];
40689440SSusan.Scheufele@Sun.COM 			switch (page_code) {
40699440SSusan.Scheufele@Sun.COM 			case 0x3:
40709440SSusan.Scheufele@Sun.COM 			case 0x4:
40719440SSusan.Scheufele@Sun.COM 				(void) mrsas_mode_sense_build(pkt);
40729440SSusan.Scheufele@Sun.COM 				return_mfi_pkt(instance, cmd);
40739440SSusan.Scheufele@Sun.COM 				*cmd_done = 1;
40749440SSusan.Scheufele@Sun.COM 				return (NULL);
40759440SSusan.Scheufele@Sun.COM 			}
40769440SSusan.Scheufele@Sun.COM 			break;
40779440SSusan.Scheufele@Sun.COM 		}
40789440SSusan.Scheufele@Sun.COM 		default:
40799440SSusan.Scheufele@Sun.COM 			break;
40809440SSusan.Scheufele@Sun.COM 		}
40819440SSusan.Scheufele@Sun.COM 
40829440SSusan.Scheufele@Sun.COM 		pthru	= (struct mrsas_pthru_frame *)cmd->frame;
40839440SSusan.Scheufele@Sun.COM 
40849440SSusan.Scheufele@Sun.COM 		/* prepare the DCDB frame */
40859440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->cmd, (acmd->islogical) ?
40869440SSusan.Scheufele@Sun.COM 		    MFI_CMD_OP_LD_SCSI : MFI_CMD_OP_PD_SCSI);
40879440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->cmd_status, 0x0);
40889440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->scsi_status, 0x0);
40899440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->target_id, acmd->device_id);
40909440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->lun, 0);
40919440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->cdb_len, acmd->cmd_cdblen);
40929440SSusan.Scheufele@Sun.COM 		ddi_put16(acc_handle, &pthru->timeout, 0);
40939440SSusan.Scheufele@Sun.COM 		ddi_put16(acc_handle, &pthru->flags, flags);
40949440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &pthru->data_xfer_len,
40959440SSusan.Scheufele@Sun.COM 		    acmd->cmd_dmacount);
40969440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->sge_count, acmd->cmd_cookiecnt);
409711856SYu.Wu@Sun.COM 		if (instance->flag_ieee) {
409811856SYu.Wu@Sun.COM 			mfi_sgl_ieee = (struct mrsas_sge_ieee *)&pthru->sgl;
409911856SYu.Wu@Sun.COM 		} else {
410011856SYu.Wu@Sun.COM 			mfi_sgl	= (struct mrsas_sge64 *)&pthru->sgl;
410111856SYu.Wu@Sun.COM 		}
41029440SSusan.Scheufele@Sun.COM 
41039440SSusan.Scheufele@Sun.COM 		bzero(cmd->sense, SENSE_LENGTH);
41049440SSusan.Scheufele@Sun.COM 		ddi_put8(acc_handle, &pthru->sense_len, SENSE_LENGTH);
41059440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0);
41069440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo,
41079440SSusan.Scheufele@Sun.COM 		    cmd->sense_phys_addr);
41089440SSusan.Scheufele@Sun.COM 
41099440SSusan.Scheufele@Sun.COM 		context = ddi_get32(acc_handle, &pthru->context);
41109440SSusan.Scheufele@Sun.COM 		ddi_rep_put8(acc_handle, (uint8_t *)pkt->pkt_cdbp,
41119440SSusan.Scheufele@Sun.COM 		    (uint8_t *)pthru->cdb, acmd->cmd_cdblen, DDI_DEV_AUTOINCR);
41129440SSusan.Scheufele@Sun.COM 
41139440SSusan.Scheufele@Sun.COM 		break;
41149440SSusan.Scheufele@Sun.COM 	}
41159440SSusan.Scheufele@Sun.COM #ifdef lint
41169440SSusan.Scheufele@Sun.COM 	context = context;
41179440SSusan.Scheufele@Sun.COM #endif
41189440SSusan.Scheufele@Sun.COM 	/* prepare the scatter-gather list for the firmware */
411911856SYu.Wu@Sun.COM 	if (instance->flag_ieee) {
412011856SYu.Wu@Sun.COM 		for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl_ieee++) {
412111856SYu.Wu@Sun.COM 			ddi_put64(acc_handle, &mfi_sgl_ieee->phys_addr,
412211856SYu.Wu@Sun.COM 			    acmd->cmd_dmacookies[i].dmac_laddress);
412311856SYu.Wu@Sun.COM 			ddi_put32(acc_handle, &mfi_sgl_ieee->length,
412411856SYu.Wu@Sun.COM 			    acmd->cmd_dmacookies[i].dmac_size);
412511856SYu.Wu@Sun.COM 		}
412611856SYu.Wu@Sun.COM 		sge_bytes = sizeof (struct mrsas_sge_ieee)*acmd->cmd_cookiecnt;
412711856SYu.Wu@Sun.COM 	} else {
412811856SYu.Wu@Sun.COM 		for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl++) {
412911856SYu.Wu@Sun.COM 			ddi_put64(acc_handle, &mfi_sgl->phys_addr,
413011856SYu.Wu@Sun.COM 			    acmd->cmd_dmacookies[i].dmac_laddress);
413111856SYu.Wu@Sun.COM 			ddi_put32(acc_handle, &mfi_sgl->length,
413211856SYu.Wu@Sun.COM 			    acmd->cmd_dmacookies[i].dmac_size);
413311856SYu.Wu@Sun.COM 		}
413411856SYu.Wu@Sun.COM 		sge_bytes = sizeof (struct mrsas_sge64)*acmd->cmd_cookiecnt;
413511856SYu.Wu@Sun.COM 	}
41369440SSusan.Scheufele@Sun.COM 
41379440SSusan.Scheufele@Sun.COM 	cmd->frame_count = (sge_bytes / MRMFI_FRAME_SIZE) +
41389440SSusan.Scheufele@Sun.COM 	    ((sge_bytes % MRMFI_FRAME_SIZE) ? 1 : 0) + 1;
41399440SSusan.Scheufele@Sun.COM 
41409440SSusan.Scheufele@Sun.COM 	if (cmd->frame_count >= 8) {
41419440SSusan.Scheufele@Sun.COM 		cmd->frame_count = 8;
41429440SSusan.Scheufele@Sun.COM 	}
41439440SSusan.Scheufele@Sun.COM 
41449440SSusan.Scheufele@Sun.COM 	return (cmd);
41459440SSusan.Scheufele@Sun.COM }
414612356SYu.Wu@Sun.COM #ifndef __sparc
414712356SYu.Wu@Sun.COM static int
wait_for_outstanding(struct mrsas_instance * instance)414812356SYu.Wu@Sun.COM wait_for_outstanding(struct mrsas_instance *instance)
414912356SYu.Wu@Sun.COM {
415012356SYu.Wu@Sun.COM 	int		i;
415112356SYu.Wu@Sun.COM 	uint32_t	wait_time = 90;
415212356SYu.Wu@Sun.COM 
415312356SYu.Wu@Sun.COM 	for (i = 0; i < wait_time; i++) {
415412356SYu.Wu@Sun.COM 		if (!instance->fw_outstanding) {
415512356SYu.Wu@Sun.COM 			break;
415612356SYu.Wu@Sun.COM 		}
415712356SYu.Wu@Sun.COM 		drv_usecwait(MILLISEC); /* wait for 1000 usecs */;
415812356SYu.Wu@Sun.COM 	}
415912356SYu.Wu@Sun.COM 
416012356SYu.Wu@Sun.COM 	if (instance->fw_outstanding) {
416112356SYu.Wu@Sun.COM 		return (1);
416212356SYu.Wu@Sun.COM 	}
416312356SYu.Wu@Sun.COM 
416412356SYu.Wu@Sun.COM 	return (0);
416512356SYu.Wu@Sun.COM }
416612356SYu.Wu@Sun.COM #endif  /* __sparc */
41679440SSusan.Scheufele@Sun.COM /*
41689440SSusan.Scheufele@Sun.COM  * issue_mfi_pthru
41699440SSusan.Scheufele@Sun.COM  */
41709440SSusan.Scheufele@Sun.COM static int
issue_mfi_pthru(struct mrsas_instance * instance,struct mrsas_ioctl * ioctl,struct mrsas_cmd * cmd,int mode)41719440SSusan.Scheufele@Sun.COM issue_mfi_pthru(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
41729440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd, int mode)
41739440SSusan.Scheufele@Sun.COM {
41749440SSusan.Scheufele@Sun.COM 	void		*ubuf;
41759440SSusan.Scheufele@Sun.COM 	uint32_t	kphys_addr = 0;
41769440SSusan.Scheufele@Sun.COM 	uint32_t	xferlen = 0;
41779440SSusan.Scheufele@Sun.COM 	uint_t		model;
41789440SSusan.Scheufele@Sun.COM 	ddi_acc_handle_t	acc_handle = cmd->frame_dma_obj.acc_handle;
41799440SSusan.Scheufele@Sun.COM 	dma_obj_t			pthru_dma_obj;
41809440SSusan.Scheufele@Sun.COM 	struct mrsas_pthru_frame	*kpthru;
41819440SSusan.Scheufele@Sun.COM 	struct mrsas_pthru_frame	*pthru;
41829440SSusan.Scheufele@Sun.COM 	int i;
41839440SSusan.Scheufele@Sun.COM 	pthru = &cmd->frame->pthru;
41849440SSusan.Scheufele@Sun.COM 	kpthru = (struct mrsas_pthru_frame *)&ioctl->frame[0];
41859440SSusan.Scheufele@Sun.COM 
418612244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress) {
418712244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: Reset flag set, "
418812244SYu.Wu@Sun.COM 		"returning mfi_pkt and setting TRAN_BUSY\n"));
418912244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
419012244SYu.Wu@Sun.COM 	}
41919440SSusan.Scheufele@Sun.COM 	model = ddi_model_convert_from(mode & FMODELS);
41929440SSusan.Scheufele@Sun.COM 	if (model == DDI_MODEL_ILP32) {
41939440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP32"));
41949440SSusan.Scheufele@Sun.COM 
41959440SSusan.Scheufele@Sun.COM 		xferlen	= kpthru->sgl.sge32[0].length;
41969440SSusan.Scheufele@Sun.COM 
41979440SSusan.Scheufele@Sun.COM 		ubuf	= (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr;
41989440SSusan.Scheufele@Sun.COM 	} else {
41999440SSusan.Scheufele@Sun.COM #ifdef _ILP32
42009440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP32"));
42019440SSusan.Scheufele@Sun.COM 		xferlen	= kpthru->sgl.sge32[0].length;
42029440SSusan.Scheufele@Sun.COM 		ubuf	= (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr;
42039440SSusan.Scheufele@Sun.COM #else
42049440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP64"));
42059440SSusan.Scheufele@Sun.COM 		xferlen	= kpthru->sgl.sge64[0].length;
42069440SSusan.Scheufele@Sun.COM 		ubuf	= (void *)(ulong_t)kpthru->sgl.sge64[0].phys_addr;
42079440SSusan.Scheufele@Sun.COM #endif
42089440SSusan.Scheufele@Sun.COM 	}
42099440SSusan.Scheufele@Sun.COM 
42109440SSusan.Scheufele@Sun.COM 	if (xferlen) {
42119440SSusan.Scheufele@Sun.COM 		/* means IOCTL requires DMA */
42129440SSusan.Scheufele@Sun.COM 		/* allocate the data transfer buffer */
42139440SSusan.Scheufele@Sun.COM 		pthru_dma_obj.size = xferlen;
42149440SSusan.Scheufele@Sun.COM 		pthru_dma_obj.dma_attr = mrsas_generic_dma_attr;
42159440SSusan.Scheufele@Sun.COM 		pthru_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
42169440SSusan.Scheufele@Sun.COM 		pthru_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
42179440SSusan.Scheufele@Sun.COM 		pthru_dma_obj.dma_attr.dma_attr_sgllen = 1;
42189440SSusan.Scheufele@Sun.COM 		pthru_dma_obj.dma_attr.dma_attr_align = 1;
42199440SSusan.Scheufele@Sun.COM 
42209440SSusan.Scheufele@Sun.COM 		/* allocate kernel buffer for DMA */
42219440SSusan.Scheufele@Sun.COM 		if (mrsas_alloc_dma_obj(instance, &pthru_dma_obj,
42229440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
42239440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: "
42249440SSusan.Scheufele@Sun.COM 			    "could not allocate data transfer buffer."));
42259440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
42269440SSusan.Scheufele@Sun.COM 		}
422710600SYu.Wu@Sun.COM 		(void) memset(pthru_dma_obj.buffer, 0, xferlen);
42289440SSusan.Scheufele@Sun.COM 
42299440SSusan.Scheufele@Sun.COM 		/* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
42309440SSusan.Scheufele@Sun.COM 		if (kpthru->flags & MFI_FRAME_DIR_WRITE) {
42319440SSusan.Scheufele@Sun.COM 			for (i = 0; i < xferlen; i++) {
42329440SSusan.Scheufele@Sun.COM 				if (ddi_copyin((uint8_t *)ubuf+i,
42339440SSusan.Scheufele@Sun.COM 				    (uint8_t *)pthru_dma_obj.buffer+i,
42349440SSusan.Scheufele@Sun.COM 				    1, mode)) {
42359440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
42369440SSusan.Scheufele@Sun.COM 					    "issue_mfi_pthru : "
42379440SSusan.Scheufele@Sun.COM 					    "copy from user space failed"));
42389440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
42399440SSusan.Scheufele@Sun.COM 				}
42409440SSusan.Scheufele@Sun.COM 			}
42419440SSusan.Scheufele@Sun.COM 		}
42429440SSusan.Scheufele@Sun.COM 
42439440SSusan.Scheufele@Sun.COM 		kphys_addr = pthru_dma_obj.dma_cookie[0].dmac_address;
42449440SSusan.Scheufele@Sun.COM 	}
42459440SSusan.Scheufele@Sun.COM 
42469440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->cmd, kpthru->cmd);
424710600SYu.Wu@Sun.COM 	ddi_put8(acc_handle, &pthru->sense_len, 0);
42489440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->cmd_status, 0);
42499440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->scsi_status, 0);
42509440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->target_id, kpthru->target_id);
42519440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->lun, kpthru->lun);
42529440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->cdb_len, kpthru->cdb_len);
42539440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &pthru->sge_count, kpthru->sge_count);
42549440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &pthru->timeout, kpthru->timeout);
42559440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &pthru->data_xfer_len, kpthru->data_xfer_len);
42569440SSusan.Scheufele@Sun.COM 
42579440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0);
42589440SSusan.Scheufele@Sun.COM 	/* pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; */
42599440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo, 0);
42609440SSusan.Scheufele@Sun.COM 
42619440SSusan.Scheufele@Sun.COM 	ddi_rep_put8(acc_handle, (uint8_t *)kpthru->cdb, (uint8_t *)pthru->cdb,
42629440SSusan.Scheufele@Sun.COM 	    pthru->cdb_len, DDI_DEV_AUTOINCR);
42639440SSusan.Scheufele@Sun.COM 
42649440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &pthru->flags, kpthru->flags & ~MFI_FRAME_SGL64);
42659440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &pthru->sgl.sge32[0].length, xferlen);
42669440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &pthru->sgl.sge32[0].phys_addr, kphys_addr);
42679440SSusan.Scheufele@Sun.COM 
42689440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_TRUE;
42699440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
42709440SSusan.Scheufele@Sun.COM 
42719440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
42729440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
42739440SSusan.Scheufele@Sun.COM 		    "issue_mfi_pthru: fw_ioctl failed"));
42749440SSusan.Scheufele@Sun.COM 	} else {
42759440SSusan.Scheufele@Sun.COM 		if (xferlen && kpthru->flags & MFI_FRAME_DIR_READ) {
42769440SSusan.Scheufele@Sun.COM 			for (i = 0; i < xferlen; i++) {
42779440SSusan.Scheufele@Sun.COM 				if (ddi_copyout(
42789440SSusan.Scheufele@Sun.COM 				    (uint8_t *)pthru_dma_obj.buffer+i,
42799440SSusan.Scheufele@Sun.COM 				    (uint8_t *)ubuf+i, 1, mode)) {
42809440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
42819440SSusan.Scheufele@Sun.COM 					    "issue_mfi_pthru : "
42829440SSusan.Scheufele@Sun.COM 					    "copy to user space failed"));
42839440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
42849440SSusan.Scheufele@Sun.COM 				}
42859440SSusan.Scheufele@Sun.COM 			}
42869440SSusan.Scheufele@Sun.COM 		}
42879440SSusan.Scheufele@Sun.COM 	}
42889440SSusan.Scheufele@Sun.COM 
42899440SSusan.Scheufele@Sun.COM 	kpthru->cmd_status = ddi_get8(acc_handle, &pthru->cmd_status);
42909440SSusan.Scheufele@Sun.COM 	kpthru->scsi_status = ddi_get8(acc_handle, &pthru->scsi_status);
42919440SSusan.Scheufele@Sun.COM 
42929440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN, (CE_NOTE, "issue_mfi_pthru: cmd_status %x, "
42939440SSusan.Scheufele@Sun.COM 	    "scsi_status %x", kpthru->cmd_status, kpthru->scsi_status));
429410820SSusan.Scheufele@Sun.COM 	DTRACE_PROBE3(issue_pthru, uint8_t, kpthru->cmd, uint8_t,
429510820SSusan.Scheufele@Sun.COM 	    kpthru->cmd_status, uint8_t, kpthru->scsi_status);
42969440SSusan.Scheufele@Sun.COM 
42979440SSusan.Scheufele@Sun.COM 	if (xferlen) {
42989440SSusan.Scheufele@Sun.COM 		/* free kernel buffer */
42999440SSusan.Scheufele@Sun.COM 		if (mrsas_free_dma_obj(instance, pthru_dma_obj) != DDI_SUCCESS)
43009440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
43019440SSusan.Scheufele@Sun.COM 	}
43029440SSusan.Scheufele@Sun.COM 
43039440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
43049440SSusan.Scheufele@Sun.COM }
43059440SSusan.Scheufele@Sun.COM 
43069440SSusan.Scheufele@Sun.COM /*
43079440SSusan.Scheufele@Sun.COM  * issue_mfi_dcmd
43089440SSusan.Scheufele@Sun.COM  */
43099440SSusan.Scheufele@Sun.COM static int
issue_mfi_dcmd(struct mrsas_instance * instance,struct mrsas_ioctl * ioctl,struct mrsas_cmd * cmd,int mode)43109440SSusan.Scheufele@Sun.COM issue_mfi_dcmd(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
43119440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd, int mode)
43129440SSusan.Scheufele@Sun.COM {
43139440SSusan.Scheufele@Sun.COM 	void		*ubuf;
43149440SSusan.Scheufele@Sun.COM 	uint32_t	kphys_addr = 0;
43159440SSusan.Scheufele@Sun.COM 	uint32_t	xferlen = 0;
43169440SSusan.Scheufele@Sun.COM 	uint32_t	model;
43179440SSusan.Scheufele@Sun.COM 	dma_obj_t	dcmd_dma_obj;
43189440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame	*kdcmd;
43199440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame	*dcmd;
43209440SSusan.Scheufele@Sun.COM 	ddi_acc_handle_t	acc_handle = cmd->frame_dma_obj.acc_handle;
43219440SSusan.Scheufele@Sun.COM 	int i;
43229440SSusan.Scheufele@Sun.COM 	dcmd = &cmd->frame->dcmd;
43239440SSusan.Scheufele@Sun.COM 	kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0];
432412244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress) {
432512244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
432612244SYu.Wu@Sun.COM 		"returning mfi_pkt and setting TRAN_BUSY\n"));
432712244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
432812244SYu.Wu@Sun.COM 	}
43299440SSusan.Scheufele@Sun.COM 	model = ddi_model_convert_from(mode & FMODELS);
43309440SSusan.Scheufele@Sun.COM 	if (model == DDI_MODEL_ILP32) {
43319440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_ILP32"));
43329440SSusan.Scheufele@Sun.COM 
43339440SSusan.Scheufele@Sun.COM 		xferlen	= kdcmd->sgl.sge32[0].length;
43349440SSusan.Scheufele@Sun.COM 
43359440SSusan.Scheufele@Sun.COM 		ubuf	= (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
43369440SSusan.Scheufele@Sun.COM 	} else {
43379440SSusan.Scheufele@Sun.COM #ifdef _ILP32
43389440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_ILP32"));
43399440SSusan.Scheufele@Sun.COM 		xferlen	= kdcmd->sgl.sge32[0].length;
43409440SSusan.Scheufele@Sun.COM 		ubuf	= (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
43419440SSusan.Scheufele@Sun.COM #else
43429440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_LP64"));
43439440SSusan.Scheufele@Sun.COM 		xferlen	= kdcmd->sgl.sge64[0].length;
43449440SSusan.Scheufele@Sun.COM 		ubuf	= (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr;
43459440SSusan.Scheufele@Sun.COM #endif
43469440SSusan.Scheufele@Sun.COM 	}
43479440SSusan.Scheufele@Sun.COM 	if (xferlen) {
43489440SSusan.Scheufele@Sun.COM 		/* means IOCTL requires DMA */
43499440SSusan.Scheufele@Sun.COM 		/* allocate the data transfer buffer */
43509440SSusan.Scheufele@Sun.COM 		dcmd_dma_obj.size = xferlen;
43519440SSusan.Scheufele@Sun.COM 		dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr;
43529440SSusan.Scheufele@Sun.COM 		dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
43539440SSusan.Scheufele@Sun.COM 		dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
43549440SSusan.Scheufele@Sun.COM 		dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1;
43559440SSusan.Scheufele@Sun.COM 		dcmd_dma_obj.dma_attr.dma_attr_align = 1;
43569440SSusan.Scheufele@Sun.COM 
43579440SSusan.Scheufele@Sun.COM 		/* allocate kernel buffer for DMA */
43589440SSusan.Scheufele@Sun.COM 		if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj,
43599440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
43609440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: "
43619440SSusan.Scheufele@Sun.COM 			    "could not allocate data transfer buffer."));
43629440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
43639440SSusan.Scheufele@Sun.COM 		}
436410600SYu.Wu@Sun.COM 		(void) memset(dcmd_dma_obj.buffer, 0, xferlen);
43659440SSusan.Scheufele@Sun.COM 
43669440SSusan.Scheufele@Sun.COM 		/* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
43679440SSusan.Scheufele@Sun.COM 		if (kdcmd->flags & MFI_FRAME_DIR_WRITE) {
43689440SSusan.Scheufele@Sun.COM 			for (i = 0; i < xferlen; i++) {
43699440SSusan.Scheufele@Sun.COM 				if (ddi_copyin((uint8_t *)ubuf + i,
43709440SSusan.Scheufele@Sun.COM 				    (uint8_t *)dcmd_dma_obj.buffer + i,
43719440SSusan.Scheufele@Sun.COM 				    1, mode)) {
43729440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
43739440SSusan.Scheufele@Sun.COM 					    "issue_mfi_dcmd : "
43749440SSusan.Scheufele@Sun.COM 					    "copy from user space failed"));
43759440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
43769440SSusan.Scheufele@Sun.COM 				}
43779440SSusan.Scheufele@Sun.COM 			}
43789440SSusan.Scheufele@Sun.COM 		}
43799440SSusan.Scheufele@Sun.COM 
43809440SSusan.Scheufele@Sun.COM 		kphys_addr = dcmd_dma_obj.dma_cookie[0].dmac_address;
43819440SSusan.Scheufele@Sun.COM 	}
43829440SSusan.Scheufele@Sun.COM 
43839440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &dcmd->cmd, kdcmd->cmd);
43849440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &dcmd->cmd_status, 0);
43859440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &dcmd->sge_count, kdcmd->sge_count);
43869440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &dcmd->timeout, kdcmd->timeout);
43879440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &dcmd->data_xfer_len, kdcmd->data_xfer_len);
43889440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &dcmd->opcode, kdcmd->opcode);
43899440SSusan.Scheufele@Sun.COM 
43909440SSusan.Scheufele@Sun.COM 	ddi_rep_put8(acc_handle, (uint8_t *)kdcmd->mbox.b,
43919440SSusan.Scheufele@Sun.COM 	    (uint8_t *)dcmd->mbox.b, DCMD_MBOX_SZ, DDI_DEV_AUTOINCR);
43929440SSusan.Scheufele@Sun.COM 
43939440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &dcmd->flags, kdcmd->flags & ~MFI_FRAME_SGL64);
43949440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &dcmd->sgl.sge32[0].length, xferlen);
43959440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &dcmd->sgl.sge32[0].phys_addr, kphys_addr);
43969440SSusan.Scheufele@Sun.COM 
43979440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_TRUE;
43989440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
43999440SSusan.Scheufele@Sun.COM 
44009440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
44019440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: fw_ioctl failed"));
44029440SSusan.Scheufele@Sun.COM 	} else {
44039440SSusan.Scheufele@Sun.COM 		if (xferlen && (kdcmd->flags & MFI_FRAME_DIR_READ)) {
44049440SSusan.Scheufele@Sun.COM 			for (i = 0; i < xferlen; i++) {
44059440SSusan.Scheufele@Sun.COM 				if (ddi_copyout(
44069440SSusan.Scheufele@Sun.COM 				    (uint8_t *)dcmd_dma_obj.buffer + i,
44079440SSusan.Scheufele@Sun.COM 				    (uint8_t *)ubuf + i,
44089440SSusan.Scheufele@Sun.COM 				    1, mode)) {
44099440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
44109440SSusan.Scheufele@Sun.COM 					    "issue_mfi_dcmd : "
44119440SSusan.Scheufele@Sun.COM 					    "copy to user space failed"));
44129440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
44139440SSusan.Scheufele@Sun.COM 				}
44149440SSusan.Scheufele@Sun.COM 			}
44159440SSusan.Scheufele@Sun.COM 		}
44169440SSusan.Scheufele@Sun.COM 	}
44179440SSusan.Scheufele@Sun.COM 
44189440SSusan.Scheufele@Sun.COM 	kdcmd->cmd_status = ddi_get8(acc_handle, &dcmd->cmd_status);
441910820SSusan.Scheufele@Sun.COM 	DTRACE_PROBE3(issue_dcmd, uint32_t, kdcmd->opcode, uint8_t,
442010820SSusan.Scheufele@Sun.COM 	    kdcmd->cmd, uint8_t, kdcmd->cmd_status);
44219440SSusan.Scheufele@Sun.COM 
44229440SSusan.Scheufele@Sun.COM 	if (xferlen) {
44239440SSusan.Scheufele@Sun.COM 		/* free kernel buffer */
44249440SSusan.Scheufele@Sun.COM 		if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS)
44259440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
44269440SSusan.Scheufele@Sun.COM 	}
44279440SSusan.Scheufele@Sun.COM 
44289440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
44299440SSusan.Scheufele@Sun.COM }
44309440SSusan.Scheufele@Sun.COM 
44319440SSusan.Scheufele@Sun.COM /*
44329440SSusan.Scheufele@Sun.COM  * issue_mfi_smp
44339440SSusan.Scheufele@Sun.COM  */
44349440SSusan.Scheufele@Sun.COM static int
issue_mfi_smp(struct mrsas_instance * instance,struct mrsas_ioctl * ioctl,struct mrsas_cmd * cmd,int mode)44359440SSusan.Scheufele@Sun.COM issue_mfi_smp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
44369440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd, int mode)
44379440SSusan.Scheufele@Sun.COM {
44389440SSusan.Scheufele@Sun.COM 	void		*request_ubuf;
44399440SSusan.Scheufele@Sun.COM 	void		*response_ubuf;
44409440SSusan.Scheufele@Sun.COM 	uint32_t	request_xferlen = 0;
44419440SSusan.Scheufele@Sun.COM 	uint32_t	response_xferlen = 0;
44429440SSusan.Scheufele@Sun.COM 	uint_t		model;
44439440SSusan.Scheufele@Sun.COM 	dma_obj_t			request_dma_obj;
44449440SSusan.Scheufele@Sun.COM 	dma_obj_t			response_dma_obj;
44459440SSusan.Scheufele@Sun.COM 	ddi_acc_handle_t	acc_handle = cmd->frame_dma_obj.acc_handle;
44469440SSusan.Scheufele@Sun.COM 	struct mrsas_smp_frame		*ksmp;
44479440SSusan.Scheufele@Sun.COM 	struct mrsas_smp_frame		*smp;
44489440SSusan.Scheufele@Sun.COM 	struct mrsas_sge32		*sge32;
44499440SSusan.Scheufele@Sun.COM #ifndef _ILP32
44509440SSusan.Scheufele@Sun.COM 	struct mrsas_sge64		*sge64;
44519440SSusan.Scheufele@Sun.COM #endif
44529440SSusan.Scheufele@Sun.COM 	int i;
44539440SSusan.Scheufele@Sun.COM 	uint64_t			tmp_sas_addr;
44549440SSusan.Scheufele@Sun.COM 
44559440SSusan.Scheufele@Sun.COM 	smp = &cmd->frame->smp;
44569440SSusan.Scheufele@Sun.COM 	ksmp = (struct mrsas_smp_frame *)&ioctl->frame[0];
44579440SSusan.Scheufele@Sun.COM 
445812244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress) {
445912244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
446012244SYu.Wu@Sun.COM 		"returning mfi_pkt and setting TRAN_BUSY\n"));
446112244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
446212244SYu.Wu@Sun.COM 	}
44639440SSusan.Scheufele@Sun.COM 	model = ddi_model_convert_from(mode & FMODELS);
44649440SSusan.Scheufele@Sun.COM 	if (model == DDI_MODEL_ILP32) {
44659440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_ILP32"));
44669440SSusan.Scheufele@Sun.COM 
44679440SSusan.Scheufele@Sun.COM 		sge32			= &ksmp->sgl[0].sge32[0];
44689440SSusan.Scheufele@Sun.COM 		response_xferlen	= sge32[0].length;
44699440SSusan.Scheufele@Sun.COM 		request_xferlen		= sge32[1].length;
44709440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "issue_mfi_smp: "
44719440SSusan.Scheufele@Sun.COM 		    "response_xferlen = %x, request_xferlen = %x",
44729440SSusan.Scheufele@Sun.COM 		    response_xferlen, request_xferlen));
44739440SSusan.Scheufele@Sun.COM 
44749440SSusan.Scheufele@Sun.COM 		response_ubuf	= (void *)(ulong_t)sge32[0].phys_addr;
44759440SSusan.Scheufele@Sun.COM 		request_ubuf	= (void *)(ulong_t)sge32[1].phys_addr;
44769440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: "
44779440SSusan.Scheufele@Sun.COM 		    "response_ubuf = %p, request_ubuf = %p",
44789440SSusan.Scheufele@Sun.COM 		    response_ubuf, request_ubuf));
44799440SSusan.Scheufele@Sun.COM 	} else {
44809440SSusan.Scheufele@Sun.COM #ifdef _ILP32
44819440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_ILP32"));
44829440SSusan.Scheufele@Sun.COM 
44839440SSusan.Scheufele@Sun.COM 		sge32			= &ksmp->sgl[0].sge32[0];
44849440SSusan.Scheufele@Sun.COM 		response_xferlen	= sge32[0].length;
44859440SSusan.Scheufele@Sun.COM 		request_xferlen		= sge32[1].length;
44869440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "issue_mfi_smp: "
44879440SSusan.Scheufele@Sun.COM 		    "response_xferlen = %x, request_xferlen = %x",
44889440SSusan.Scheufele@Sun.COM 		    response_xferlen, request_xferlen));
44899440SSusan.Scheufele@Sun.COM 
44909440SSusan.Scheufele@Sun.COM 		response_ubuf	= (void *)(ulong_t)sge32[0].phys_addr;
44919440SSusan.Scheufele@Sun.COM 		request_ubuf	= (void *)(ulong_t)sge32[1].phys_addr;
44929440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: "
44939440SSusan.Scheufele@Sun.COM 		    "response_ubuf = %p, request_ubuf = %p",
44949440SSusan.Scheufele@Sun.COM 		    response_ubuf, request_ubuf));
44959440SSusan.Scheufele@Sun.COM #else
44969440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_LP64"));
44979440SSusan.Scheufele@Sun.COM 
44989440SSusan.Scheufele@Sun.COM 		sge64			= &ksmp->sgl[0].sge64[0];
44999440SSusan.Scheufele@Sun.COM 		response_xferlen	= sge64[0].length;
45009440SSusan.Scheufele@Sun.COM 		request_xferlen		= sge64[1].length;
45019440SSusan.Scheufele@Sun.COM 
45029440SSusan.Scheufele@Sun.COM 		response_ubuf	= (void *)(ulong_t)sge64[0].phys_addr;
45039440SSusan.Scheufele@Sun.COM 		request_ubuf	= (void *)(ulong_t)sge64[1].phys_addr;
45049440SSusan.Scheufele@Sun.COM #endif
45059440SSusan.Scheufele@Sun.COM 	}
45069440SSusan.Scheufele@Sun.COM 	if (request_xferlen) {
45079440SSusan.Scheufele@Sun.COM 		/* means IOCTL requires DMA */
45089440SSusan.Scheufele@Sun.COM 		/* allocate the data transfer buffer */
45099440SSusan.Scheufele@Sun.COM 		request_dma_obj.size = request_xferlen;
45109440SSusan.Scheufele@Sun.COM 		request_dma_obj.dma_attr = mrsas_generic_dma_attr;
45119440SSusan.Scheufele@Sun.COM 		request_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
45129440SSusan.Scheufele@Sun.COM 		request_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
45139440SSusan.Scheufele@Sun.COM 		request_dma_obj.dma_attr.dma_attr_sgllen = 1;
45149440SSusan.Scheufele@Sun.COM 		request_dma_obj.dma_attr.dma_attr_align = 1;
45159440SSusan.Scheufele@Sun.COM 
45169440SSusan.Scheufele@Sun.COM 		/* allocate kernel buffer for DMA */
45179440SSusan.Scheufele@Sun.COM 		if (mrsas_alloc_dma_obj(instance, &request_dma_obj,
45189440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
45199440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
45209440SSusan.Scheufele@Sun.COM 			    "could not allocate data transfer buffer."));
45219440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
45229440SSusan.Scheufele@Sun.COM 		}
452310600SYu.Wu@Sun.COM 		(void) memset(request_dma_obj.buffer, 0, request_xferlen);
45249440SSusan.Scheufele@Sun.COM 
45259440SSusan.Scheufele@Sun.COM 		/* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
45269440SSusan.Scheufele@Sun.COM 		for (i = 0; i < request_xferlen; i++) {
45279440SSusan.Scheufele@Sun.COM 			if (ddi_copyin((uint8_t *)request_ubuf + i,
45289440SSusan.Scheufele@Sun.COM 			    (uint8_t *)request_dma_obj.buffer + i,
45299440SSusan.Scheufele@Sun.COM 			    1, mode)) {
45309440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
45319440SSusan.Scheufele@Sun.COM 				    "copy from user space failed"));
45329440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
45339440SSusan.Scheufele@Sun.COM 			}
45349440SSusan.Scheufele@Sun.COM 		}
45359440SSusan.Scheufele@Sun.COM 	}
45369440SSusan.Scheufele@Sun.COM 
45379440SSusan.Scheufele@Sun.COM 	if (response_xferlen) {
45389440SSusan.Scheufele@Sun.COM 		/* means IOCTL requires DMA */
45399440SSusan.Scheufele@Sun.COM 		/* allocate the data transfer buffer */
45409440SSusan.Scheufele@Sun.COM 		response_dma_obj.size = response_xferlen;
45419440SSusan.Scheufele@Sun.COM 		response_dma_obj.dma_attr = mrsas_generic_dma_attr;
45429440SSusan.Scheufele@Sun.COM 		response_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
45439440SSusan.Scheufele@Sun.COM 		response_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
45449440SSusan.Scheufele@Sun.COM 		response_dma_obj.dma_attr.dma_attr_sgllen = 1;
45459440SSusan.Scheufele@Sun.COM 		response_dma_obj.dma_attr.dma_attr_align = 1;
45469440SSusan.Scheufele@Sun.COM 
45479440SSusan.Scheufele@Sun.COM 		/* allocate kernel buffer for DMA */
45489440SSusan.Scheufele@Sun.COM 		if (mrsas_alloc_dma_obj(instance, &response_dma_obj,
45499440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
45509440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
45519440SSusan.Scheufele@Sun.COM 			    "could not allocate data transfer buffer."));
45529440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
45539440SSusan.Scheufele@Sun.COM 		}
455410600SYu.Wu@Sun.COM 		(void) memset(response_dma_obj.buffer, 0, response_xferlen);
45559440SSusan.Scheufele@Sun.COM 
45569440SSusan.Scheufele@Sun.COM 		/* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
45579440SSusan.Scheufele@Sun.COM 		for (i = 0; i < response_xferlen; i++) {
45589440SSusan.Scheufele@Sun.COM 			if (ddi_copyin((uint8_t *)response_ubuf + i,
45599440SSusan.Scheufele@Sun.COM 			    (uint8_t *)response_dma_obj.buffer + i,
45609440SSusan.Scheufele@Sun.COM 			    1, mode)) {
45619440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
45629440SSusan.Scheufele@Sun.COM 				    "copy from user space failed"));
45639440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
45649440SSusan.Scheufele@Sun.COM 			}
45659440SSusan.Scheufele@Sun.COM 		}
45669440SSusan.Scheufele@Sun.COM 	}
45679440SSusan.Scheufele@Sun.COM 
45689440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &smp->cmd, ksmp->cmd);
45699440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &smp->cmd_status, 0);
45709440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &smp->connection_status, 0);
45719440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &smp->sge_count, ksmp->sge_count);
45729440SSusan.Scheufele@Sun.COM 	/* smp->context		= ksmp->context; */
45739440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &smp->timeout, ksmp->timeout);
45749440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &smp->data_xfer_len, ksmp->data_xfer_len);
45759440SSusan.Scheufele@Sun.COM 
45769440SSusan.Scheufele@Sun.COM 	bcopy((void *)&ksmp->sas_addr, (void *)&tmp_sas_addr,
45779440SSusan.Scheufele@Sun.COM 	    sizeof (uint64_t));
45789440SSusan.Scheufele@Sun.COM 	ddi_put64(acc_handle, &smp->sas_addr, tmp_sas_addr);
45799440SSusan.Scheufele@Sun.COM 
45809440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &smp->flags, ksmp->flags & ~MFI_FRAME_SGL64);
45819440SSusan.Scheufele@Sun.COM 
45829440SSusan.Scheufele@Sun.COM 	model = ddi_model_convert_from(mode & FMODELS);
45839440SSusan.Scheufele@Sun.COM 	if (model == DDI_MODEL_ILP32) {
45849440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
45859795SYu.Wu@Sun.COM 		    "issue_mfi_smp: DDI_MODEL_ILP32"));
45869440SSusan.Scheufele@Sun.COM 
45879440SSusan.Scheufele@Sun.COM 		sge32 = &smp->sgl[0].sge32[0];
45889440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[0].length, response_xferlen);
45899440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[0].phys_addr,
45909440SSusan.Scheufele@Sun.COM 		    response_dma_obj.dma_cookie[0].dmac_address);
45919440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[1].length, request_xferlen);
45929440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[1].phys_addr,
45939440SSusan.Scheufele@Sun.COM 		    request_dma_obj.dma_cookie[0].dmac_address);
45949440SSusan.Scheufele@Sun.COM 	} else {
45959440SSusan.Scheufele@Sun.COM #ifdef _ILP32
45969440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
45979795SYu.Wu@Sun.COM 		    "issue_mfi_smp: DDI_MODEL_ILP32"));
45989440SSusan.Scheufele@Sun.COM 		sge32 = &smp->sgl[0].sge32[0];
45999440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[0].length, response_xferlen);
46009440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[0].phys_addr,
46019440SSusan.Scheufele@Sun.COM 		    response_dma_obj.dma_cookie[0].dmac_address);
46029440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[1].length, request_xferlen);
46039440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge32[1].phys_addr,
46049440SSusan.Scheufele@Sun.COM 		    request_dma_obj.dma_cookie[0].dmac_address);
46059440SSusan.Scheufele@Sun.COM #else
46069440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
46079440SSusan.Scheufele@Sun.COM 		    "issue_mfi_smp: DDI_MODEL_LP64"));
46089440SSusan.Scheufele@Sun.COM 		sge64 = &smp->sgl[0].sge64[0];
46099440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge64[0].length, response_xferlen);
46109440SSusan.Scheufele@Sun.COM 		ddi_put64(acc_handle, &sge64[0].phys_addr,
46119440SSusan.Scheufele@Sun.COM 		    response_dma_obj.dma_cookie[0].dmac_address);
46129440SSusan.Scheufele@Sun.COM 		ddi_put32(acc_handle, &sge64[1].length, request_xferlen);
46139440SSusan.Scheufele@Sun.COM 		ddi_put64(acc_handle, &sge64[1].phys_addr,
46149440SSusan.Scheufele@Sun.COM 		    request_dma_obj.dma_cookie[0].dmac_address);
46159440SSusan.Scheufele@Sun.COM #endif
46169440SSusan.Scheufele@Sun.COM 	}
46179440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp : "
46189440SSusan.Scheufele@Sun.COM 	    "smp->response_xferlen = %d, smp->request_xferlen = %d "
46199440SSusan.Scheufele@Sun.COM 	    "smp->data_xfer_len = %d", ddi_get32(acc_handle, &sge32[0].length),
46209440SSusan.Scheufele@Sun.COM 	    ddi_get32(acc_handle, &sge32[1].length),
46219440SSusan.Scheufele@Sun.COM 	    ddi_get32(acc_handle, &smp->data_xfer_len)));
46229440SSusan.Scheufele@Sun.COM 
46239440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_TRUE;
46249440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
46259440SSusan.Scheufele@Sun.COM 
46269440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
46279440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN,
46289440SSusan.Scheufele@Sun.COM 		    "issue_mfi_smp: fw_ioctl failed"));
46299440SSusan.Scheufele@Sun.COM 	} else {
46309440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
46319440SSusan.Scheufele@Sun.COM 		    "issue_mfi_smp: copy to user space"));
46329440SSusan.Scheufele@Sun.COM 
46339440SSusan.Scheufele@Sun.COM 		if (request_xferlen) {
46349440SSusan.Scheufele@Sun.COM 			for (i = 0; i < request_xferlen; i++) {
46359440SSusan.Scheufele@Sun.COM 				if (ddi_copyout(
46369440SSusan.Scheufele@Sun.COM 				    (uint8_t *)request_dma_obj.buffer +
46379440SSusan.Scheufele@Sun.COM 				    i, (uint8_t *)request_ubuf + i,
46389440SSusan.Scheufele@Sun.COM 				    1, mode)) {
46399440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
46409440SSusan.Scheufele@Sun.COM 					    "issue_mfi_smp : copy to user space"
46419440SSusan.Scheufele@Sun.COM 					    " failed"));
46429440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
46439440SSusan.Scheufele@Sun.COM 				}
46449440SSusan.Scheufele@Sun.COM 			}
46459440SSusan.Scheufele@Sun.COM 		}
46469440SSusan.Scheufele@Sun.COM 
46479440SSusan.Scheufele@Sun.COM 		if (response_xferlen) {
46489440SSusan.Scheufele@Sun.COM 			for (i = 0; i < response_xferlen; i++) {
46499440SSusan.Scheufele@Sun.COM 				if (ddi_copyout(
46509440SSusan.Scheufele@Sun.COM 				    (uint8_t *)response_dma_obj.buffer
46519440SSusan.Scheufele@Sun.COM 				    + i, (uint8_t *)response_ubuf
46529440SSusan.Scheufele@Sun.COM 				    + i, 1, mode)) {
46539440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
46549440SSusan.Scheufele@Sun.COM 					    "issue_mfi_smp : copy to "
46559440SSusan.Scheufele@Sun.COM 					    "user space failed"));
46569440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
46579440SSusan.Scheufele@Sun.COM 				}
46589440SSusan.Scheufele@Sun.COM 			}
46599440SSusan.Scheufele@Sun.COM 		}
46609440SSusan.Scheufele@Sun.COM 	}
46619440SSusan.Scheufele@Sun.COM 
46629440SSusan.Scheufele@Sun.COM 	ksmp->cmd_status = ddi_get8(acc_handle, &smp->cmd_status);
46639440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: smp->cmd_status = %d",
46649440SSusan.Scheufele@Sun.COM 	    ddi_get8(acc_handle, &smp->cmd_status)));
466510820SSusan.Scheufele@Sun.COM 	DTRACE_PROBE2(issue_smp, uint8_t, ksmp->cmd, uint8_t, ksmp->cmd_status);
46669440SSusan.Scheufele@Sun.COM 
46679440SSusan.Scheufele@Sun.COM 	if (request_xferlen) {
46689440SSusan.Scheufele@Sun.COM 		/* free kernel buffer */
46699440SSusan.Scheufele@Sun.COM 		if (mrsas_free_dma_obj(instance, request_dma_obj) !=
46709440SSusan.Scheufele@Sun.COM 		    DDI_SUCCESS)
46719440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
46729440SSusan.Scheufele@Sun.COM 	}
46739440SSusan.Scheufele@Sun.COM 
46749440SSusan.Scheufele@Sun.COM 	if (response_xferlen) {
46759440SSusan.Scheufele@Sun.COM 		/* free kernel buffer */
46769440SSusan.Scheufele@Sun.COM 		if (mrsas_free_dma_obj(instance, response_dma_obj) !=
46779440SSusan.Scheufele@Sun.COM 		    DDI_SUCCESS)
46789440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
46799440SSusan.Scheufele@Sun.COM 	}
46809440SSusan.Scheufele@Sun.COM 
46819440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
46829440SSusan.Scheufele@Sun.COM }
46839440SSusan.Scheufele@Sun.COM 
46849440SSusan.Scheufele@Sun.COM /*
46859440SSusan.Scheufele@Sun.COM  * issue_mfi_stp
46869440SSusan.Scheufele@Sun.COM  */
46879440SSusan.Scheufele@Sun.COM static int
issue_mfi_stp(struct mrsas_instance * instance,struct mrsas_ioctl * ioctl,struct mrsas_cmd * cmd,int mode)46889440SSusan.Scheufele@Sun.COM issue_mfi_stp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
46899440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd, int mode)
46909440SSusan.Scheufele@Sun.COM {
46919440SSusan.Scheufele@Sun.COM 	void		*fis_ubuf;
46929440SSusan.Scheufele@Sun.COM 	void		*data_ubuf;
46939440SSusan.Scheufele@Sun.COM 	uint32_t	fis_xferlen = 0;
46949440SSusan.Scheufele@Sun.COM 	uint32_t	data_xferlen = 0;
46959440SSusan.Scheufele@Sun.COM 	uint_t		model;
46969440SSusan.Scheufele@Sun.COM 	dma_obj_t	fis_dma_obj;
46979440SSusan.Scheufele@Sun.COM 	dma_obj_t	data_dma_obj;
46989440SSusan.Scheufele@Sun.COM 	struct mrsas_stp_frame	*kstp;
46999440SSusan.Scheufele@Sun.COM 	struct mrsas_stp_frame	*stp;
47009440SSusan.Scheufele@Sun.COM 	ddi_acc_handle_t	acc_handle = cmd->frame_dma_obj.acc_handle;
47019440SSusan.Scheufele@Sun.COM 	int i;
47029440SSusan.Scheufele@Sun.COM 
47039440SSusan.Scheufele@Sun.COM 	stp = &cmd->frame->stp;
47049440SSusan.Scheufele@Sun.COM 	kstp = (struct mrsas_stp_frame *)&ioctl->frame[0];
47059440SSusan.Scheufele@Sun.COM 
470612244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress) {
470712244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
470812244SYu.Wu@Sun.COM 		"returning mfi_pkt and setting TRAN_BUSY\n"));
470912244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
471012244SYu.Wu@Sun.COM 	}
47119440SSusan.Scheufele@Sun.COM 	model = ddi_model_convert_from(mode & FMODELS);
47129440SSusan.Scheufele@Sun.COM 	if (model == DDI_MODEL_ILP32) {
47139440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_ILP32"));
47149440SSusan.Scheufele@Sun.COM 
47159440SSusan.Scheufele@Sun.COM 		fis_xferlen	= kstp->sgl.sge32[0].length;
47169440SSusan.Scheufele@Sun.COM 		data_xferlen	= kstp->sgl.sge32[1].length;
47179440SSusan.Scheufele@Sun.COM 
47189440SSusan.Scheufele@Sun.COM 		fis_ubuf	= (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr;
47199440SSusan.Scheufele@Sun.COM 		data_ubuf	= (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr;
47209440SSusan.Scheufele@Sun.COM 	}
47219440SSusan.Scheufele@Sun.COM 	else
47229440SSusan.Scheufele@Sun.COM 	{
47239440SSusan.Scheufele@Sun.COM #ifdef _ILP32
47249440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_ILP32"));
47259440SSusan.Scheufele@Sun.COM 
47269440SSusan.Scheufele@Sun.COM 		fis_xferlen	= kstp->sgl.sge32[0].length;
47279440SSusan.Scheufele@Sun.COM 		data_xferlen	= kstp->sgl.sge32[1].length;
47289440SSusan.Scheufele@Sun.COM 
47299440SSusan.Scheufele@Sun.COM 		fis_ubuf	= (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr;
47309440SSusan.Scheufele@Sun.COM 		data_ubuf	= (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr;
47319440SSusan.Scheufele@Sun.COM #else
47329440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_LP64"));
47339440SSusan.Scheufele@Sun.COM 
47349440SSusan.Scheufele@Sun.COM 		fis_xferlen	= kstp->sgl.sge64[0].length;
47359440SSusan.Scheufele@Sun.COM 		data_xferlen	= kstp->sgl.sge64[1].length;
47369440SSusan.Scheufele@Sun.COM 
47379440SSusan.Scheufele@Sun.COM 		fis_ubuf	= (void *)(ulong_t)kstp->sgl.sge64[0].phys_addr;
47389440SSusan.Scheufele@Sun.COM 		data_ubuf	= (void *)(ulong_t)kstp->sgl.sge64[1].phys_addr;
47399440SSusan.Scheufele@Sun.COM #endif
47409440SSusan.Scheufele@Sun.COM 	}
47419440SSusan.Scheufele@Sun.COM 
47429440SSusan.Scheufele@Sun.COM 
47439440SSusan.Scheufele@Sun.COM 	if (fis_xferlen) {
47449440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "issue_mfi_stp: "
47459440SSusan.Scheufele@Sun.COM 		    "fis_ubuf = %p fis_xferlen = %x", fis_ubuf, fis_xferlen));
47469440SSusan.Scheufele@Sun.COM 
47479440SSusan.Scheufele@Sun.COM 		/* means IOCTL requires DMA */
47489440SSusan.Scheufele@Sun.COM 		/* allocate the data transfer buffer */
47499440SSusan.Scheufele@Sun.COM 		fis_dma_obj.size = fis_xferlen;
47509440SSusan.Scheufele@Sun.COM 		fis_dma_obj.dma_attr = mrsas_generic_dma_attr;
47519440SSusan.Scheufele@Sun.COM 		fis_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
47529440SSusan.Scheufele@Sun.COM 		fis_dma_obj.dma_attr.dma_attr_count_max	= 0xFFFFFFFFU;
47539440SSusan.Scheufele@Sun.COM 		fis_dma_obj.dma_attr.dma_attr_sgllen = 1;
47549440SSusan.Scheufele@Sun.COM 		fis_dma_obj.dma_attr.dma_attr_align = 1;
47559440SSusan.Scheufele@Sun.COM 
47569440SSusan.Scheufele@Sun.COM 		/* allocate kernel buffer for DMA */
47579440SSusan.Scheufele@Sun.COM 		if (mrsas_alloc_dma_obj(instance, &fis_dma_obj,
47589440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
47599440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "issue_mfi_stp : "
47609440SSusan.Scheufele@Sun.COM 			    "could not allocate data transfer buffer."));
47619440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
47629440SSusan.Scheufele@Sun.COM 		}
476310600SYu.Wu@Sun.COM 		(void) memset(fis_dma_obj.buffer, 0, fis_xferlen);
47649440SSusan.Scheufele@Sun.COM 
47659440SSusan.Scheufele@Sun.COM 		/* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
47669440SSusan.Scheufele@Sun.COM 		for (i = 0; i < fis_xferlen; i++) {
47679440SSusan.Scheufele@Sun.COM 			if (ddi_copyin((uint8_t *)fis_ubuf + i,
47689440SSusan.Scheufele@Sun.COM 			    (uint8_t *)fis_dma_obj.buffer + i, 1, mode)) {
47699440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
47709440SSusan.Scheufele@Sun.COM 				    "copy from user space failed"));
47719440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
47729440SSusan.Scheufele@Sun.COM 			}
47739440SSusan.Scheufele@Sun.COM 		}
47749440SSusan.Scheufele@Sun.COM 	}
47759440SSusan.Scheufele@Sun.COM 
47769440SSusan.Scheufele@Sun.COM 	if (data_xferlen) {
47779440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, "issue_mfi_stp: data_ubuf = %p "
47789440SSusan.Scheufele@Sun.COM 		    "data_xferlen = %x", data_ubuf, data_xferlen));
47799440SSusan.Scheufele@Sun.COM 
47809440SSusan.Scheufele@Sun.COM 		/* means IOCTL requires DMA */
47819440SSusan.Scheufele@Sun.COM 		/* allocate the data transfer buffer */
47829440SSusan.Scheufele@Sun.COM 		data_dma_obj.size = data_xferlen;
47839440SSusan.Scheufele@Sun.COM 		data_dma_obj.dma_attr = mrsas_generic_dma_attr;
47849440SSusan.Scheufele@Sun.COM 		data_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
47859440SSusan.Scheufele@Sun.COM 		data_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
47869440SSusan.Scheufele@Sun.COM 		data_dma_obj.dma_attr.dma_attr_sgllen = 1;
47879440SSusan.Scheufele@Sun.COM 		data_dma_obj.dma_attr.dma_attr_align = 1;
47889440SSusan.Scheufele@Sun.COM 
47899440SSusan.Scheufele@Sun.COM /* allocate kernel buffer for DMA */
47909440SSusan.Scheufele@Sun.COM 		if (mrsas_alloc_dma_obj(instance, &data_dma_obj,
47919440SSusan.Scheufele@Sun.COM 		    (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
47929440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
47939440SSusan.Scheufele@Sun.COM 			    "could not allocate data transfer buffer."));
47949440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
47959440SSusan.Scheufele@Sun.COM 		}
479610600SYu.Wu@Sun.COM 		(void) memset(data_dma_obj.buffer, 0, data_xferlen);
47979440SSusan.Scheufele@Sun.COM 
47989440SSusan.Scheufele@Sun.COM 		/* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
47999440SSusan.Scheufele@Sun.COM 		for (i = 0; i < data_xferlen; i++) {
48009440SSusan.Scheufele@Sun.COM 			if (ddi_copyin((uint8_t *)data_ubuf + i,
48019440SSusan.Scheufele@Sun.COM 			    (uint8_t *)data_dma_obj.buffer + i, 1, mode)) {
48029440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
48039440SSusan.Scheufele@Sun.COM 				    "copy from user space failed"));
48049440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
48059440SSusan.Scheufele@Sun.COM 			}
48069440SSusan.Scheufele@Sun.COM 		}
48079440SSusan.Scheufele@Sun.COM 	}
48089440SSusan.Scheufele@Sun.COM 
48099440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &stp->cmd, kstp->cmd);
48109440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &stp->cmd_status, 0);
48119440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &stp->connection_status, 0);
48129440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &stp->target_id, kstp->target_id);
48139440SSusan.Scheufele@Sun.COM 	ddi_put8(acc_handle, &stp->sge_count, kstp->sge_count);
48149440SSusan.Scheufele@Sun.COM 
48159440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &stp->timeout, kstp->timeout);
48169440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &stp->data_xfer_len, kstp->data_xfer_len);
48179440SSusan.Scheufele@Sun.COM 
48189440SSusan.Scheufele@Sun.COM 	ddi_rep_put8(acc_handle, (uint8_t *)kstp->fis, (uint8_t *)stp->fis, 10,
48199440SSusan.Scheufele@Sun.COM 	    DDI_DEV_AUTOINCR);
48209440SSusan.Scheufele@Sun.COM 
48219440SSusan.Scheufele@Sun.COM 	ddi_put16(acc_handle, &stp->flags, kstp->flags & ~MFI_FRAME_SGL64);
48229440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &stp->stp_flags, kstp->stp_flags);
48239440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &stp->sgl.sge32[0].length, fis_xferlen);
48249440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &stp->sgl.sge32[0].phys_addr,
48259440SSusan.Scheufele@Sun.COM 	    fis_dma_obj.dma_cookie[0].dmac_address);
48269440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &stp->sgl.sge32[1].length, data_xferlen);
48279440SSusan.Scheufele@Sun.COM 	ddi_put32(acc_handle, &stp->sgl.sge32[1].phys_addr,
48289440SSusan.Scheufele@Sun.COM 	    data_dma_obj.dma_cookie[0].dmac_address);
48299440SSusan.Scheufele@Sun.COM 
48309440SSusan.Scheufele@Sun.COM 	cmd->sync_cmd = MRSAS_TRUE;
48319440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
48329440SSusan.Scheufele@Sun.COM 
48339440SSusan.Scheufele@Sun.COM 	if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
48349440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: fw_ioctl failed"));
48359440SSusan.Scheufele@Sun.COM 	} else {
48369440SSusan.Scheufele@Sun.COM 
48379440SSusan.Scheufele@Sun.COM 		if (fis_xferlen) {
48389440SSusan.Scheufele@Sun.COM 			for (i = 0; i < fis_xferlen; i++) {
48399440SSusan.Scheufele@Sun.COM 				if (ddi_copyout(
48409440SSusan.Scheufele@Sun.COM 				    (uint8_t *)fis_dma_obj.buffer + i,
48419440SSusan.Scheufele@Sun.COM 				    (uint8_t *)fis_ubuf + i, 1, mode)) {
48429440SSusan.Scheufele@Sun.COM 					con_log(CL_ANN, (CE_WARN,
48439440SSusan.Scheufele@Sun.COM 					    "issue_mfi_stp : copy to "
48449440SSusan.Scheufele@Sun.COM 					    "user space failed"));
48459440SSusan.Scheufele@Sun.COM 					return (DDI_FAILURE);
48469440SSusan.Scheufele@Sun.COM 				}
48479440SSusan.Scheufele@Sun.COM 			}
48489440SSusan.Scheufele@Sun.COM 		}
48499440SSusan.Scheufele@Sun.COM 	}
48509440SSusan.Scheufele@Sun.COM 	if (data_xferlen) {
48519440SSusan.Scheufele@Sun.COM 		for (i = 0; i < data_xferlen; i++) {
48529440SSusan.Scheufele@Sun.COM 			if (ddi_copyout(
48539440SSusan.Scheufele@Sun.COM 			    (uint8_t *)data_dma_obj.buffer + i,
48549440SSusan.Scheufele@Sun.COM 			    (uint8_t *)data_ubuf + i, 1, mode)) {
48559440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN,
48569440SSusan.Scheufele@Sun.COM 				    "issue_mfi_stp : copy to"
48579440SSusan.Scheufele@Sun.COM 				    " user space failed"));
48589440SSusan.Scheufele@Sun.COM 				return (DDI_FAILURE);
48599440SSusan.Scheufele@Sun.COM 			}
48609440SSusan.Scheufele@Sun.COM 		}
48619440SSusan.Scheufele@Sun.COM 	}
48629440SSusan.Scheufele@Sun.COM 
48639440SSusan.Scheufele@Sun.COM 	kstp->cmd_status = ddi_get8(acc_handle, &stp->cmd_status);
486410820SSusan.Scheufele@Sun.COM 	DTRACE_PROBE2(issue_stp, uint8_t, kstp->cmd, uint8_t, kstp->cmd_status);
48659440SSusan.Scheufele@Sun.COM 
48669440SSusan.Scheufele@Sun.COM 	if (fis_xferlen) {
48679440SSusan.Scheufele@Sun.COM 		/* free kernel buffer */
48689440SSusan.Scheufele@Sun.COM 		if (mrsas_free_dma_obj(instance, fis_dma_obj) != DDI_SUCCESS)
48699440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
48709440SSusan.Scheufele@Sun.COM 	}
48719440SSusan.Scheufele@Sun.COM 
48729440SSusan.Scheufele@Sun.COM 	if (data_xferlen) {
48739440SSusan.Scheufele@Sun.COM 		/* free kernel buffer */
48749440SSusan.Scheufele@Sun.COM 		if (mrsas_free_dma_obj(instance, data_dma_obj) != DDI_SUCCESS)
48759440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
48769440SSusan.Scheufele@Sun.COM 	}
48779440SSusan.Scheufele@Sun.COM 
48789440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
48799440SSusan.Scheufele@Sun.COM }
48809440SSusan.Scheufele@Sun.COM 
48819440SSusan.Scheufele@Sun.COM /*
48829440SSusan.Scheufele@Sun.COM  * fill_up_drv_ver
48839440SSusan.Scheufele@Sun.COM  */
48849440SSusan.Scheufele@Sun.COM static void
fill_up_drv_ver(struct mrsas_drv_ver * dv)48859440SSusan.Scheufele@Sun.COM fill_up_drv_ver(struct mrsas_drv_ver *dv)
48869440SSusan.Scheufele@Sun.COM {
48879440SSusan.Scheufele@Sun.COM 	(void) memset(dv, 0, sizeof (struct mrsas_drv_ver));
48889440SSusan.Scheufele@Sun.COM 
48899440SSusan.Scheufele@Sun.COM 	(void) memcpy(dv->signature, "$LSI LOGIC$", strlen("$LSI LOGIC$"));
48909440SSusan.Scheufele@Sun.COM 	(void) memcpy(dv->os_name, "Solaris", strlen("Solaris"));
48919440SSusan.Scheufele@Sun.COM 	(void) memcpy(dv->drv_name, "mr_sas", strlen("mr_sas"));
48929440SSusan.Scheufele@Sun.COM 	(void) memcpy(dv->drv_ver, MRSAS_VERSION, strlen(MRSAS_VERSION));
48939440SSusan.Scheufele@Sun.COM 	(void) memcpy(dv->drv_rel_date, MRSAS_RELDATE,
48949440SSusan.Scheufele@Sun.COM 	    strlen(MRSAS_RELDATE));
48959440SSusan.Scheufele@Sun.COM }
48969440SSusan.Scheufele@Sun.COM 
48979440SSusan.Scheufele@Sun.COM /*
48989440SSusan.Scheufele@Sun.COM  * handle_drv_ioctl
48999440SSusan.Scheufele@Sun.COM  */
49009440SSusan.Scheufele@Sun.COM static int
handle_drv_ioctl(struct mrsas_instance * instance,struct mrsas_ioctl * ioctl,int mode)49019440SSusan.Scheufele@Sun.COM handle_drv_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
49029440SSusan.Scheufele@Sun.COM     int mode)
49039440SSusan.Scheufele@Sun.COM {
49049440SSusan.Scheufele@Sun.COM 	int	i;
49059440SSusan.Scheufele@Sun.COM 	int	rval = DDI_SUCCESS;
49069440SSusan.Scheufele@Sun.COM 	int	*props = NULL;
49079440SSusan.Scheufele@Sun.COM 	void	*ubuf;
49089440SSusan.Scheufele@Sun.COM 
49099440SSusan.Scheufele@Sun.COM 	uint8_t		*pci_conf_buf;
49109440SSusan.Scheufele@Sun.COM 	uint32_t	xferlen;
49119440SSusan.Scheufele@Sun.COM 	uint32_t	num_props;
49129440SSusan.Scheufele@Sun.COM 	uint_t		model;
49139440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame	*kdcmd;
49149440SSusan.Scheufele@Sun.COM 	struct mrsas_drv_ver	dv;
49159440SSusan.Scheufele@Sun.COM 	struct mrsas_pci_information pi;
49169440SSusan.Scheufele@Sun.COM 
49179440SSusan.Scheufele@Sun.COM 	kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0];
49189440SSusan.Scheufele@Sun.COM 
49199440SSusan.Scheufele@Sun.COM 	model = ddi_model_convert_from(mode & FMODELS);
49209440SSusan.Scheufele@Sun.COM 	if (model == DDI_MODEL_ILP32) {
49219440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
49229440SSusan.Scheufele@Sun.COM 		    "handle_drv_ioctl: DDI_MODEL_ILP32"));
49239440SSusan.Scheufele@Sun.COM 
49249440SSusan.Scheufele@Sun.COM 		xferlen	= kdcmd->sgl.sge32[0].length;
49259440SSusan.Scheufele@Sun.COM 
49269440SSusan.Scheufele@Sun.COM 		ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
49279440SSusan.Scheufele@Sun.COM 	} else {
49289440SSusan.Scheufele@Sun.COM #ifdef _ILP32
49299440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
49309440SSusan.Scheufele@Sun.COM 		    "handle_drv_ioctl: DDI_MODEL_ILP32"));
49319440SSusan.Scheufele@Sun.COM 		xferlen	= kdcmd->sgl.sge32[0].length;
49329440SSusan.Scheufele@Sun.COM 		ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
49339440SSusan.Scheufele@Sun.COM #else
49349440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
49359440SSusan.Scheufele@Sun.COM 		    "handle_drv_ioctl: DDI_MODEL_LP64"));
49369440SSusan.Scheufele@Sun.COM 		xferlen	= kdcmd->sgl.sge64[0].length;
49379440SSusan.Scheufele@Sun.COM 		ubuf = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr;
49389440SSusan.Scheufele@Sun.COM #endif
49399440SSusan.Scheufele@Sun.COM 	}
49409440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
49419440SSusan.Scheufele@Sun.COM 	    "dataBuf=%p size=%d bytes", ubuf, xferlen));
49429440SSusan.Scheufele@Sun.COM 
49439440SSusan.Scheufele@Sun.COM 	switch (kdcmd->opcode) {
49449440SSusan.Scheufele@Sun.COM 	case MRSAS_DRIVER_IOCTL_DRIVER_VERSION:
49459440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
49469440SSusan.Scheufele@Sun.COM 		    "MRSAS_DRIVER_IOCTL_DRIVER_VERSION"));
49479440SSusan.Scheufele@Sun.COM 
49489440SSusan.Scheufele@Sun.COM 		fill_up_drv_ver(&dv);
49499795SYu.Wu@Sun.COM 
49509795SYu.Wu@Sun.COM 		if (ddi_copyout(&dv, ubuf, xferlen, mode)) {
49519795SYu.Wu@Sun.COM 			con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: "
49529795SYu.Wu@Sun.COM 			    "MRSAS_DRIVER_IOCTL_DRIVER_VERSION : "
49539795SYu.Wu@Sun.COM 			    "copy to user space failed"));
49549795SYu.Wu@Sun.COM 			kdcmd->cmd_status = 1;
49559795SYu.Wu@Sun.COM 			rval = 1;
49569795SYu.Wu@Sun.COM 		} else {
49579795SYu.Wu@Sun.COM 			kdcmd->cmd_status = 0;
49589440SSusan.Scheufele@Sun.COM 		}
49599440SSusan.Scheufele@Sun.COM 		break;
49609440SSusan.Scheufele@Sun.COM 	case MRSAS_DRIVER_IOCTL_PCI_INFORMATION:
49619440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
49629440SSusan.Scheufele@Sun.COM 		    "MRSAS_DRIVER_IOCTL_PCI_INFORMAITON"));
49639440SSusan.Scheufele@Sun.COM 
49649440SSusan.Scheufele@Sun.COM 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, instance->dip,
49659440SSusan.Scheufele@Sun.COM 		    0, "reg", &props, &num_props)) {
49669440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: "
49679440SSusan.Scheufele@Sun.COM 			    "MRSAS_DRIVER_IOCTL_PCI_INFORMATION : "
49689440SSusan.Scheufele@Sun.COM 			    "ddi_prop_look_int_array failed"));
49699440SSusan.Scheufele@Sun.COM 			rval = DDI_FAILURE;
49709440SSusan.Scheufele@Sun.COM 		} else {
49719440SSusan.Scheufele@Sun.COM 
49729440SSusan.Scheufele@Sun.COM 			pi.busNumber = (props[0] >> 16) & 0xFF;
49739440SSusan.Scheufele@Sun.COM 			pi.deviceNumber = (props[0] >> 11) & 0x1f;
49749440SSusan.Scheufele@Sun.COM 			pi.functionNumber = (props[0] >> 8) & 0x7;
49759440SSusan.Scheufele@Sun.COM 			ddi_prop_free((void *)props);
49769440SSusan.Scheufele@Sun.COM 		}
49779440SSusan.Scheufele@Sun.COM 
49789440SSusan.Scheufele@Sun.COM 		pci_conf_buf = (uint8_t *)&pi.pciHeaderInfo;
49799440SSusan.Scheufele@Sun.COM 
49809440SSusan.Scheufele@Sun.COM 		for (i = 0; i < (sizeof (struct mrsas_pci_information) -
49819440SSusan.Scheufele@Sun.COM 		    offsetof(struct mrsas_pci_information, pciHeaderInfo));
49829440SSusan.Scheufele@Sun.COM 		    i++) {
49839440SSusan.Scheufele@Sun.COM 			pci_conf_buf[i] =
49849440SSusan.Scheufele@Sun.COM 			    pci_config_get8(instance->pci_handle, i);
49859440SSusan.Scheufele@Sun.COM 		}
49869795SYu.Wu@Sun.COM 
49879795SYu.Wu@Sun.COM 		if (ddi_copyout(&pi, ubuf, xferlen, mode)) {
49889795SYu.Wu@Sun.COM 			con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: "
49899795SYu.Wu@Sun.COM 			    "MRSAS_DRIVER_IOCTL_PCI_INFORMATION : "
49909795SYu.Wu@Sun.COM 			    "copy to user space failed"));
49919795SYu.Wu@Sun.COM 			kdcmd->cmd_status = 1;
49929795SYu.Wu@Sun.COM 			rval = 1;
49939795SYu.Wu@Sun.COM 		} else {
49949795SYu.Wu@Sun.COM 			kdcmd->cmd_status = 0;
49959440SSusan.Scheufele@Sun.COM 		}
49969440SSusan.Scheufele@Sun.COM 		break;
49979440SSusan.Scheufele@Sun.COM 	default:
49989440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: "
49999440SSusan.Scheufele@Sun.COM 		    "invalid driver specific IOCTL opcode = 0x%x",
50009440SSusan.Scheufele@Sun.COM 		    kdcmd->opcode));
50019440SSusan.Scheufele@Sun.COM 		kdcmd->cmd_status = 1;
50029440SSusan.Scheufele@Sun.COM 		rval = DDI_FAILURE;
50039440SSusan.Scheufele@Sun.COM 		break;
50049440SSusan.Scheufele@Sun.COM 	}
50059440SSusan.Scheufele@Sun.COM 
50069440SSusan.Scheufele@Sun.COM 	return (rval);
50079440SSusan.Scheufele@Sun.COM }
50089440SSusan.Scheufele@Sun.COM 
50099440SSusan.Scheufele@Sun.COM /*
50109440SSusan.Scheufele@Sun.COM  * handle_mfi_ioctl
50119440SSusan.Scheufele@Sun.COM  */
50129440SSusan.Scheufele@Sun.COM static int
handle_mfi_ioctl(struct mrsas_instance * instance,struct mrsas_ioctl * ioctl,int mode)50139440SSusan.Scheufele@Sun.COM handle_mfi_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
50149440SSusan.Scheufele@Sun.COM     int mode)
50159440SSusan.Scheufele@Sun.COM {
50169440SSusan.Scheufele@Sun.COM 	int	rval = DDI_SUCCESS;
50179440SSusan.Scheufele@Sun.COM 
50189440SSusan.Scheufele@Sun.COM 	struct mrsas_header	*hdr;
50199440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd;
50209440SSusan.Scheufele@Sun.COM 
50219440SSusan.Scheufele@Sun.COM 	cmd = get_mfi_pkt(instance);
50229440SSusan.Scheufele@Sun.COM 
50239440SSusan.Scheufele@Sun.COM 	if (!cmd) {
50249440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mr_sas: "
50259440SSusan.Scheufele@Sun.COM 		    "failed to get a cmd packet"));
502610820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(mfi_ioctl_err, uint16_t,
502710820SSusan.Scheufele@Sun.COM 		    instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
50289440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
50299440SSusan.Scheufele@Sun.COM 	}
503012244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
50319440SSusan.Scheufele@Sun.COM 
50329440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
50339440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
50349440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
50359440SSusan.Scheufele@Sun.COM 	    cmd->index);
50369440SSusan.Scheufele@Sun.COM 
50379440SSusan.Scheufele@Sun.COM 	hdr = (struct mrsas_header *)&ioctl->frame[0];
50389440SSusan.Scheufele@Sun.COM 
50399795SYu.Wu@Sun.COM 	switch (ddi_get8(cmd->frame_dma_obj.acc_handle, &hdr->cmd)) {
50409440SSusan.Scheufele@Sun.COM 	case MFI_CMD_OP_DCMD:
50419440SSusan.Scheufele@Sun.COM 		rval = issue_mfi_dcmd(instance, ioctl, cmd, mode);
50429440SSusan.Scheufele@Sun.COM 		break;
50439440SSusan.Scheufele@Sun.COM 	case MFI_CMD_OP_SMP:
50449440SSusan.Scheufele@Sun.COM 		rval = issue_mfi_smp(instance, ioctl, cmd, mode);
50459440SSusan.Scheufele@Sun.COM 		break;
50469440SSusan.Scheufele@Sun.COM 	case MFI_CMD_OP_STP:
50479440SSusan.Scheufele@Sun.COM 		rval = issue_mfi_stp(instance, ioctl, cmd, mode);
50489440SSusan.Scheufele@Sun.COM 		break;
50499440SSusan.Scheufele@Sun.COM 	case MFI_CMD_OP_LD_SCSI:
50509440SSusan.Scheufele@Sun.COM 	case MFI_CMD_OP_PD_SCSI:
50519440SSusan.Scheufele@Sun.COM 		rval = issue_mfi_pthru(instance, ioctl, cmd, mode);
50529440SSusan.Scheufele@Sun.COM 		break;
50539440SSusan.Scheufele@Sun.COM 	default:
50549440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "handle_mfi_ioctl: "
50559440SSusan.Scheufele@Sun.COM 		    "invalid mfi ioctl hdr->cmd = %d", hdr->cmd));
50569440SSusan.Scheufele@Sun.COM 		rval = DDI_FAILURE;
50579440SSusan.Scheufele@Sun.COM 		break;
50589440SSusan.Scheufele@Sun.COM 	}
50599440SSusan.Scheufele@Sun.COM 
506010600SYu.Wu@Sun.COM 	if (mrsas_common_check(instance, cmd) != DDI_SUCCESS)
506110600SYu.Wu@Sun.COM 		rval = DDI_FAILURE;
50629440SSusan.Scheufele@Sun.COM 
50639440SSusan.Scheufele@Sun.COM 	return_mfi_pkt(instance, cmd);
506410600SYu.Wu@Sun.COM 
50659440SSusan.Scheufele@Sun.COM 	return (rval);
50669440SSusan.Scheufele@Sun.COM }
50679440SSusan.Scheufele@Sun.COM 
50689440SSusan.Scheufele@Sun.COM /*
50699440SSusan.Scheufele@Sun.COM  * AEN
50709440SSusan.Scheufele@Sun.COM  */
50719440SSusan.Scheufele@Sun.COM static int
handle_mfi_aen(struct mrsas_instance * instance,struct mrsas_aen * aen)50729440SSusan.Scheufele@Sun.COM handle_mfi_aen(struct mrsas_instance *instance, struct mrsas_aen *aen)
50739440SSusan.Scheufele@Sun.COM {
50749440SSusan.Scheufele@Sun.COM 	int	rval = 0;
50759440SSusan.Scheufele@Sun.COM 
50769440SSusan.Scheufele@Sun.COM 	rval = register_mfi_aen(instance, instance->aen_seq_num,
50779440SSusan.Scheufele@Sun.COM 	    aen->class_locale_word);
50789440SSusan.Scheufele@Sun.COM 
50799440SSusan.Scheufele@Sun.COM 	aen->cmd_status = (uint8_t)rval;
50809440SSusan.Scheufele@Sun.COM 
50819440SSusan.Scheufele@Sun.COM 	return (rval);
50829440SSusan.Scheufele@Sun.COM }
50839440SSusan.Scheufele@Sun.COM 
50849440SSusan.Scheufele@Sun.COM static int
register_mfi_aen(struct mrsas_instance * instance,uint32_t seq_num,uint32_t class_locale_word)50859440SSusan.Scheufele@Sun.COM register_mfi_aen(struct mrsas_instance *instance, uint32_t seq_num,
50869440SSusan.Scheufele@Sun.COM     uint32_t class_locale_word)
50879440SSusan.Scheufele@Sun.COM {
50889440SSusan.Scheufele@Sun.COM 	int	ret_val;
50899440SSusan.Scheufele@Sun.COM 
50909440SSusan.Scheufele@Sun.COM 	struct mrsas_cmd	*cmd, *aen_cmd;
50919440SSusan.Scheufele@Sun.COM 	struct mrsas_dcmd_frame	*dcmd;
50929440SSusan.Scheufele@Sun.COM 	union mrsas_evt_class_locale	curr_aen;
50939440SSusan.Scheufele@Sun.COM 	union mrsas_evt_class_locale	prev_aen;
50949440SSusan.Scheufele@Sun.COM 
50959440SSusan.Scheufele@Sun.COM 	/*
50969440SSusan.Scheufele@Sun.COM 	 * If there an AEN pending already (aen_cmd), check if the
50979440SSusan.Scheufele@Sun.COM 	 * class_locale of that pending AEN is inclusive of the new
50989440SSusan.Scheufele@Sun.COM 	 * AEN request we currently have. If it is, then we don't have
50999440SSusan.Scheufele@Sun.COM 	 * to do anything. In other words, whichever events the current
51009440SSusan.Scheufele@Sun.COM 	 * AEN request is subscribing to, have already been subscribed
51019440SSusan.Scheufele@Sun.COM 	 * to.
51029440SSusan.Scheufele@Sun.COM 	 *
51039440SSusan.Scheufele@Sun.COM 	 * If the old_cmd is _not_ inclusive, then we have to abort
51049440SSusan.Scheufele@Sun.COM 	 * that command, form a class_locale that is superset of both
51059440SSusan.Scheufele@Sun.COM 	 * old and current and re-issue to the FW
51069440SSusan.Scheufele@Sun.COM 	 */
51079440SSusan.Scheufele@Sun.COM 
51089795SYu.Wu@Sun.COM 	curr_aen.word = LE_32(class_locale_word);
51099795SYu.Wu@Sun.COM 	curr_aen.members.locale = LE_16(curr_aen.members.locale);
51109440SSusan.Scheufele@Sun.COM 	aen_cmd = instance->aen_cmd;
51119440SSusan.Scheufele@Sun.COM 	if (aen_cmd) {
51129440SSusan.Scheufele@Sun.COM 		prev_aen.word = ddi_get32(aen_cmd->frame_dma_obj.acc_handle,
51139440SSusan.Scheufele@Sun.COM 		    &aen_cmd->frame->dcmd.mbox.w[1]);
51149795SYu.Wu@Sun.COM 		prev_aen.word = LE_32(prev_aen.word);
51159795SYu.Wu@Sun.COM 		prev_aen.members.locale = LE_16(prev_aen.members.locale);
51169440SSusan.Scheufele@Sun.COM 		/*
51179440SSusan.Scheufele@Sun.COM 		 * A class whose enum value is smaller is inclusive of all
51189440SSusan.Scheufele@Sun.COM 		 * higher values. If a PROGRESS (= -1) was previously
51199440SSusan.Scheufele@Sun.COM 		 * registered, then a new registration requests for higher
51209440SSusan.Scheufele@Sun.COM 		 * classes need not be sent to FW. They are automatically
51219440SSusan.Scheufele@Sun.COM 		 * included.
51229440SSusan.Scheufele@Sun.COM 		 *
51239440SSusan.Scheufele@Sun.COM 		 * Locale numbers don't have such hierarchy. They are bitmap
51249440SSusan.Scheufele@Sun.COM 		 * values
51259440SSusan.Scheufele@Sun.COM 		 */
51269440SSusan.Scheufele@Sun.COM 		if ((prev_aen.members.class <= curr_aen.members.class) &&
51279440SSusan.Scheufele@Sun.COM 		    !((prev_aen.members.locale & curr_aen.members.locale) ^
51289440SSusan.Scheufele@Sun.COM 		    curr_aen.members.locale)) {
51299440SSusan.Scheufele@Sun.COM 			/*
51309440SSusan.Scheufele@Sun.COM 			 * Previously issued event registration includes
51319440SSusan.Scheufele@Sun.COM 			 * current request. Nothing to do.
51329440SSusan.Scheufele@Sun.COM 			 */
51339440SSusan.Scheufele@Sun.COM 
51349440SSusan.Scheufele@Sun.COM 			return (0);
51359440SSusan.Scheufele@Sun.COM 		} else {
51369440SSusan.Scheufele@Sun.COM 			curr_aen.members.locale |= prev_aen.members.locale;
51379440SSusan.Scheufele@Sun.COM 
51389440SSusan.Scheufele@Sun.COM 			if (prev_aen.members.class < curr_aen.members.class)
51399440SSusan.Scheufele@Sun.COM 				curr_aen.members.class = prev_aen.members.class;
51409440SSusan.Scheufele@Sun.COM 
51419440SSusan.Scheufele@Sun.COM 			ret_val = abort_aen_cmd(instance, aen_cmd);
51429440SSusan.Scheufele@Sun.COM 
51439440SSusan.Scheufele@Sun.COM 			if (ret_val) {
51449440SSusan.Scheufele@Sun.COM 				con_log(CL_ANN, (CE_WARN, "register_mfi_aen: "
51459440SSusan.Scheufele@Sun.COM 				    "failed to abort prevous AEN command"));
51469440SSusan.Scheufele@Sun.COM 
51479440SSusan.Scheufele@Sun.COM 				return (ret_val);
51489440SSusan.Scheufele@Sun.COM 			}
51499440SSusan.Scheufele@Sun.COM 		}
51509440SSusan.Scheufele@Sun.COM 	} else {
515110600SYu.Wu@Sun.COM 		curr_aen.word = LE_32(class_locale_word);
515210600SYu.Wu@Sun.COM 		curr_aen.members.locale = LE_16(curr_aen.members.locale);
51539440SSusan.Scheufele@Sun.COM 	}
51549440SSusan.Scheufele@Sun.COM 
51559440SSusan.Scheufele@Sun.COM 	cmd = get_mfi_pkt(instance);
51569440SSusan.Scheufele@Sun.COM 
515710820SSusan.Scheufele@Sun.COM 	if (!cmd) {
515810820SSusan.Scheufele@Sun.COM 		DTRACE_PROBE2(mfi_aen_err, uint16_t, instance->fw_outstanding,
515910820SSusan.Scheufele@Sun.COM 		    uint16_t, instance->max_fw_cmds);
51609440SSusan.Scheufele@Sun.COM 		return (ENOMEM);
516110820SSusan.Scheufele@Sun.COM 	}
516212244SYu.Wu@Sun.COM 	cmd->retry_count_for_ocr = 0;
51639440SSusan.Scheufele@Sun.COM 	/* Clear the frame buffer and assign back the context id */
51649440SSusan.Scheufele@Sun.COM 	(void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
51659440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
51669440SSusan.Scheufele@Sun.COM 	    cmd->index);
51679440SSusan.Scheufele@Sun.COM 
51689440SSusan.Scheufele@Sun.COM 	dcmd = &cmd->frame->dcmd;
51699440SSusan.Scheufele@Sun.COM 
51709440SSusan.Scheufele@Sun.COM 	/* for(i = 0; i < DCMD_MBOX_SZ; i++) dcmd->mbox.b[i] = 0; */
51719440SSusan.Scheufele@Sun.COM 	(void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
51729440SSusan.Scheufele@Sun.COM 
51739440SSusan.Scheufele@Sun.COM 	(void) memset(instance->mfi_evt_detail_obj.buffer, 0,
51749440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_detail));
51759440SSusan.Scheufele@Sun.COM 
51769440SSusan.Scheufele@Sun.COM 	/* Prepare DCMD for aen registration */
51779440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
51789440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0);
51799440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
51809440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
51819440SSusan.Scheufele@Sun.COM 	    MFI_FRAME_DIR_READ);
51829440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
51839440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
51849440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_detail));
51859440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
51869440SSusan.Scheufele@Sun.COM 	    MR_DCMD_CTRL_EVENT_WAIT);
51879440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.w[0], seq_num);
51889795SYu.Wu@Sun.COM 	curr_aen.members.locale = LE_16(curr_aen.members.locale);
51899795SYu.Wu@Sun.COM 	curr_aen.word = LE_32(curr_aen.word);
51909440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.w[1],
51919440SSusan.Scheufele@Sun.COM 	    curr_aen.word);
51929440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
51939440SSusan.Scheufele@Sun.COM 	    instance->mfi_evt_detail_obj.dma_cookie[0].dmac_address);
51949440SSusan.Scheufele@Sun.COM 	ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
51959440SSusan.Scheufele@Sun.COM 	    sizeof (struct mrsas_evt_detail));
51969440SSusan.Scheufele@Sun.COM 
51979440SSusan.Scheufele@Sun.COM 	instance->aen_seq_num = seq_num;
51989440SSusan.Scheufele@Sun.COM 
51999440SSusan.Scheufele@Sun.COM 
52009440SSusan.Scheufele@Sun.COM 	/*
52019440SSusan.Scheufele@Sun.COM 	 * Store reference to the cmd used to register for AEN. When an
52029440SSusan.Scheufele@Sun.COM 	 * application wants us to register for AEN, we have to abort this
52039440SSusan.Scheufele@Sun.COM 	 * cmd and re-register with a new EVENT LOCALE supplied by that app
52049440SSusan.Scheufele@Sun.COM 	 */
52059440SSusan.Scheufele@Sun.COM 	instance->aen_cmd = cmd;
52069440SSusan.Scheufele@Sun.COM 
52079440SSusan.Scheufele@Sun.COM 	cmd->frame_count = 1;
52089440SSusan.Scheufele@Sun.COM 
52099440SSusan.Scheufele@Sun.COM 	/* Issue the aen registration frame */
52109440SSusan.Scheufele@Sun.COM 	/* atomic_add_16 (&instance->fw_outstanding, 1); */
52119440SSusan.Scheufele@Sun.COM 	instance->func_ptr->issue_cmd(cmd, instance);
52129440SSusan.Scheufele@Sun.COM 
52139440SSusan.Scheufele@Sun.COM 	return (0);
52149440SSusan.Scheufele@Sun.COM }
52159440SSusan.Scheufele@Sun.COM 
52169440SSusan.Scheufele@Sun.COM static void
display_scsi_inquiry(caddr_t scsi_inq)52179440SSusan.Scheufele@Sun.COM display_scsi_inquiry(caddr_t scsi_inq)
52189440SSusan.Scheufele@Sun.COM {
52199440SSusan.Scheufele@Sun.COM #define	MAX_SCSI_DEVICE_CODE	14
52209440SSusan.Scheufele@Sun.COM 	int		i;
52219440SSusan.Scheufele@Sun.COM 	char		inquiry_buf[256] = {0};
52229440SSusan.Scheufele@Sun.COM 	int		len;
52239440SSusan.Scheufele@Sun.COM 	const char	*const scsi_device_types[] = {
52249440SSusan.Scheufele@Sun.COM 		"Direct-Access    ",
52259440SSusan.Scheufele@Sun.COM 		"Sequential-Access",
52269440SSusan.Scheufele@Sun.COM 		"Printer          ",
52279440SSusan.Scheufele@Sun.COM 		"Processor        ",
52289440SSusan.Scheufele@Sun.COM 		"WORM             ",
52299440SSusan.Scheufele@Sun.COM 		"CD-ROM           ",
52309440SSusan.Scheufele@Sun.COM 		"Scanner          ",
52319440SSusan.Scheufele@Sun.COM 		"Optical Device   ",
52329440SSusan.Scheufele@Sun.COM 		"Medium Changer   ",
52339440SSusan.Scheufele@Sun.COM 		"Communications   ",
52349440SSusan.Scheufele@Sun.COM 		"Unknown          ",
52359440SSusan.Scheufele@Sun.COM 		"Unknown          ",
52369440SSusan.Scheufele@Sun.COM 		"Unknown          ",
52379440SSusan.Scheufele@Sun.COM 		"Enclosure        ",
52389440SSusan.Scheufele@Sun.COM 	};
52399440SSusan.Scheufele@Sun.COM 
52409440SSusan.Scheufele@Sun.COM 	len = 0;
52419440SSusan.Scheufele@Sun.COM 
52429440SSusan.Scheufele@Sun.COM 	len += snprintf(inquiry_buf + len, 265 - len, "  Vendor: ");
52439440SSusan.Scheufele@Sun.COM 	for (i = 8; i < 16; i++) {
52449440SSusan.Scheufele@Sun.COM 		len += snprintf(inquiry_buf + len, 265 - len, "%c",
52459440SSusan.Scheufele@Sun.COM 		    scsi_inq[i]);
52469440SSusan.Scheufele@Sun.COM 	}
52479440SSusan.Scheufele@Sun.COM 
52489440SSusan.Scheufele@Sun.COM 	len += snprintf(inquiry_buf + len, 265 - len, "  Model: ");
52499440SSusan.Scheufele@Sun.COM 
52509440SSusan.Scheufele@Sun.COM 	for (i = 16; i < 32; i++) {
52519440SSusan.Scheufele@Sun.COM 		len += snprintf(inquiry_buf + len, 265 - len, "%c",
52529440SSusan.Scheufele@Sun.COM 		    scsi_inq[i]);
52539440SSusan.Scheufele@Sun.COM 	}
52549440SSusan.Scheufele@Sun.COM 
52559440SSusan.Scheufele@Sun.COM 	len += snprintf(inquiry_buf + len, 265 - len, "  Rev: ");
52569440SSusan.Scheufele@Sun.COM 
52579440SSusan.Scheufele@Sun.COM 	for (i = 32; i < 36; i++) {
52589440SSusan.Scheufele@Sun.COM 		len += snprintf(inquiry_buf + len, 265 - len, "%c",
52599440SSusan.Scheufele@Sun.COM 		    scsi_inq[i]);
52609440SSusan.Scheufele@Sun.COM 	}
52619440SSusan.Scheufele@Sun.COM 
52629440SSusan.Scheufele@Sun.COM 	len += snprintf(inquiry_buf + len, 265 - len, "\n");
52639440SSusan.Scheufele@Sun.COM 
52649440SSusan.Scheufele@Sun.COM 
52659440SSusan.Scheufele@Sun.COM 	i = scsi_inq[0] & 0x1f;
52669440SSusan.Scheufele@Sun.COM 
52679440SSusan.Scheufele@Sun.COM 
52689440SSusan.Scheufele@Sun.COM 	len += snprintf(inquiry_buf + len, 265 - len, "  Type:   %s ",
52699440SSusan.Scheufele@Sun.COM 	    i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
52709440SSusan.Scheufele@Sun.COM 	    "Unknown          ");
52719440SSusan.Scheufele@Sun.COM 
52729440SSusan.Scheufele@Sun.COM 
52739440SSusan.Scheufele@Sun.COM 	len += snprintf(inquiry_buf + len, 265 - len,
52749440SSusan.Scheufele@Sun.COM 	    "                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
52759440SSusan.Scheufele@Sun.COM 
52769440SSusan.Scheufele@Sun.COM 	if ((scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1) {
52779440SSusan.Scheufele@Sun.COM 		len += snprintf(inquiry_buf + len, 265 - len, " CCS\n");
52789440SSusan.Scheufele@Sun.COM 	} else {
52799440SSusan.Scheufele@Sun.COM 		len += snprintf(inquiry_buf + len, 265 - len, "\n");
52809440SSusan.Scheufele@Sun.COM 	}
52819440SSusan.Scheufele@Sun.COM 
52829440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_CONT, inquiry_buf));
52839440SSusan.Scheufele@Sun.COM }
52849440SSusan.Scheufele@Sun.COM 
528512244SYu.Wu@Sun.COM static void
io_timeout_checker(void * arg)528612244SYu.Wu@Sun.COM io_timeout_checker(void *arg)
528712244SYu.Wu@Sun.COM {
528812244SYu.Wu@Sun.COM 	struct scsi_pkt *pkt;
528912244SYu.Wu@Sun.COM 	struct mrsas_instance *instance = arg;
529012244SYu.Wu@Sun.COM 	struct mrsas_cmd	*cmd = NULL;
529112244SYu.Wu@Sun.COM 	struct mrsas_header	*hdr;
529212244SYu.Wu@Sun.COM 	int time = 0;
529312244SYu.Wu@Sun.COM 	int counter = 0;
529412244SYu.Wu@Sun.COM 	struct mlist_head	*pos, *next;
529512244SYu.Wu@Sun.COM 	mlist_t			process_list;
529612244SYu.Wu@Sun.COM 
529712244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress == 1) {
529812244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "io_timeout_checker"
529912244SYu.Wu@Sun.COM 		    " reset in progress"));
530012244SYu.Wu@Sun.COM 		instance->timeout_id = timeout(io_timeout_checker,
530112244SYu.Wu@Sun.COM 		    (void *) instance, drv_usectohz(MRSAS_1_SECOND));
530212244SYu.Wu@Sun.COM 		return;
530312244SYu.Wu@Sun.COM 	}
530412244SYu.Wu@Sun.COM 
530512244SYu.Wu@Sun.COM 	/* See if this check needs to be in the beginning or last in ISR */
530612244SYu.Wu@Sun.COM 	if (mrsas_initiate_ocr_if_fw_is_faulty(instance) ==  1) {
530712244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
530812244SYu.Wu@Sun.COM 		    "Fw Fault state Handling in io_timeout_checker"));
530912244SYu.Wu@Sun.COM 		if (instance->adapterresetinprogress == 0) {
531012244SYu.Wu@Sun.COM 			(void) mrsas_reset_ppc(instance);
531112244SYu.Wu@Sun.COM 		}
531212244SYu.Wu@Sun.COM 		instance->timeout_id = timeout(io_timeout_checker,
531312244SYu.Wu@Sun.COM 		    (void *) instance, drv_usectohz(MRSAS_1_SECOND));
531412244SYu.Wu@Sun.COM 		return;
531512244SYu.Wu@Sun.COM 	}
531612244SYu.Wu@Sun.COM 
531712244SYu.Wu@Sun.COM 	INIT_LIST_HEAD(&process_list);
531812244SYu.Wu@Sun.COM 
531912244SYu.Wu@Sun.COM 	mutex_enter(&instance->cmd_pend_mtx);
532012244SYu.Wu@Sun.COM 	mlist_for_each_safe(pos, next, &instance->cmd_pend_list) {
5321*12757SYu.Wu@Sun.COM 		cmd = mlist_entry(pos, struct mrsas_cmd, list);
5322*12757SYu.Wu@Sun.COM 
5323*12757SYu.Wu@Sun.COM 		if (cmd == NULL) {
532412244SYu.Wu@Sun.COM 			continue;
532512244SYu.Wu@Sun.COM 		}
5326*12757SYu.Wu@Sun.COM 
5327*12757SYu.Wu@Sun.COM 		if (cmd->sync_cmd == MRSAS_TRUE) {
5328*12757SYu.Wu@Sun.COM 			hdr = (struct mrsas_header *)&cmd->frame->hdr;
5329*12757SYu.Wu@Sun.COM 			if (hdr == NULL) {
5330*12757SYu.Wu@Sun.COM 				continue;
5331*12757SYu.Wu@Sun.COM 			}
5332*12757SYu.Wu@Sun.COM 			time = --cmd->drv_pkt_time;
533312244SYu.Wu@Sun.COM 		} else {
533412244SYu.Wu@Sun.COM 			pkt = cmd->pkt;
533512244SYu.Wu@Sun.COM 			if (pkt == NULL) {
533612244SYu.Wu@Sun.COM 				continue;
533712244SYu.Wu@Sun.COM 			}
533812244SYu.Wu@Sun.COM 			time = --cmd->drv_pkt_time;
533912244SYu.Wu@Sun.COM 		}
534012244SYu.Wu@Sun.COM 		if (time <= 0) {
534112244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE, "%llx: "
534212244SYu.Wu@Sun.COM 			    "io_timeout_checker: TIMING OUT: pkt "
534312244SYu.Wu@Sun.COM 			    ": %p, cmd %p", gethrtime(), (void *)pkt,
534412244SYu.Wu@Sun.COM 			    (void *)cmd));
534512244SYu.Wu@Sun.COM 			counter++;
534612244SYu.Wu@Sun.COM 			break;
534712244SYu.Wu@Sun.COM 		}
534812244SYu.Wu@Sun.COM 	}
534912244SYu.Wu@Sun.COM 	mutex_exit(&instance->cmd_pend_mtx);
535012244SYu.Wu@Sun.COM 
535112244SYu.Wu@Sun.COM 	if (counter) {
535212244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
535312244SYu.Wu@Sun.COM 		    "io_timeout_checker "
535412244SYu.Wu@Sun.COM 		    "cmd->retrycount_for_ocr %d, "
535512244SYu.Wu@Sun.COM 		    "cmd index %d , cmd address %p ",
535612244SYu.Wu@Sun.COM 		    cmd->retry_count_for_ocr+1, cmd->index, (void *)cmd));
535712244SYu.Wu@Sun.COM 
535812244SYu.Wu@Sun.COM 		if (instance->disable_online_ctrl_reset == 1) {
535912244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE, "mrsas: "
536012244SYu.Wu@Sun.COM 			    "OCR is not supported by the Firmware "
536112244SYu.Wu@Sun.COM 			    "Failing all the queued packets \n"));
536212244SYu.Wu@Sun.COM 
536312244SYu.Wu@Sun.COM 			(void) mrsas_kill_adapter(instance);
5364*12757SYu.Wu@Sun.COM 			return;
536512244SYu.Wu@Sun.COM 		} else {
536612244SYu.Wu@Sun.COM 			if (cmd->retry_count_for_ocr <=  IO_RETRY_COUNT) {
536712244SYu.Wu@Sun.COM 				if (instance->adapterresetinprogress == 0) {
536812244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_NOTE, "mrsas: "
536912244SYu.Wu@Sun.COM 				    "OCR is supported by FW "
537012244SYu.Wu@Sun.COM 				    "triggering  mrsas_reset_ppc"));
537112244SYu.Wu@Sun.COM 				(void) mrsas_reset_ppc(instance);
537212244SYu.Wu@Sun.COM 				}
537312244SYu.Wu@Sun.COM 			} else {
537412244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_NOTE,
537512244SYu.Wu@Sun.COM 				    "io_timeout_checker:"
537612244SYu.Wu@Sun.COM 				    " cmdindex: %d,cmd address: %p "
537712244SYu.Wu@Sun.COM 				    "timed out even after 3 resets: "
537812244SYu.Wu@Sun.COM 				    "so kill adapter", cmd->index,
537912244SYu.Wu@Sun.COM 				    (void *)cmd));
538012244SYu.Wu@Sun.COM 				(void) mrsas_kill_adapter(instance);
538112244SYu.Wu@Sun.COM 				return;
538212244SYu.Wu@Sun.COM 			}
538312244SYu.Wu@Sun.COM 		}
538412244SYu.Wu@Sun.COM 	}
538512244SYu.Wu@Sun.COM 
538612244SYu.Wu@Sun.COM 
5387*12757SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas: "
5388*12757SYu.Wu@Sun.COM 	    "schedule next timeout check: "
5389*12757SYu.Wu@Sun.COM 	    "do timeout \n"));
5390*12757SYu.Wu@Sun.COM 	instance->timeout_id =
5391*12757SYu.Wu@Sun.COM 	    timeout(io_timeout_checker, (void *)instance,
5392*12757SYu.Wu@Sun.COM 	    drv_usectohz(MRSAS_1_SECOND));
539312244SYu.Wu@Sun.COM }
53949440SSusan.Scheufele@Sun.COM static int
read_fw_status_reg_ppc(struct mrsas_instance * instance)53959440SSusan.Scheufele@Sun.COM read_fw_status_reg_ppc(struct mrsas_instance *instance)
53969440SSusan.Scheufele@Sun.COM {
53979440SSusan.Scheufele@Sun.COM 	return ((int)RD_OB_SCRATCH_PAD_0(instance));
53989440SSusan.Scheufele@Sun.COM }
53999440SSusan.Scheufele@Sun.COM 
54009440SSusan.Scheufele@Sun.COM static void
issue_cmd_ppc(struct mrsas_cmd * cmd,struct mrsas_instance * instance)54019440SSusan.Scheufele@Sun.COM issue_cmd_ppc(struct mrsas_cmd *cmd, struct mrsas_instance *instance)
54029440SSusan.Scheufele@Sun.COM {
540312244SYu.Wu@Sun.COM 	struct scsi_pkt *pkt;
54049440SSusan.Scheufele@Sun.COM 	atomic_add_16(&instance->fw_outstanding, 1);
54059440SSusan.Scheufele@Sun.COM 
540612244SYu.Wu@Sun.COM 	pkt = cmd->pkt;
540712244SYu.Wu@Sun.COM 	if (pkt) {
540812244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_CONT, "%llx : issue_cmd_ppc:"
540912244SYu.Wu@Sun.COM 		    "ISSUED CMD TO FW : called : cmd:"
541012244SYu.Wu@Sun.COM 		    ": %p instance : %p pkt : %p pkt_time : %x\n",
541112244SYu.Wu@Sun.COM 		    gethrtime(), (void *)cmd, (void *)instance,
541212244SYu.Wu@Sun.COM 		    (void *)pkt, cmd->drv_pkt_time));
541312244SYu.Wu@Sun.COM 		if (instance->adapterresetinprogress) {
541412244SYu.Wu@Sun.COM 			cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
541512244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE, "Reset the scsi_pkt timer"));
541612244SYu.Wu@Sun.COM 		} else {
541712244SYu.Wu@Sun.COM 			push_pending_mfi_pkt(instance, cmd);
541812244SYu.Wu@Sun.COM 		}
541912244SYu.Wu@Sun.COM 
542012244SYu.Wu@Sun.COM 	} else {
542112244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_CONT, "%llx : issue_cmd_ppc:"
542212244SYu.Wu@Sun.COM 		    "ISSUED CMD TO FW : called : cmd : %p, instance: %p"
542312244SYu.Wu@Sun.COM 		    "(NO PKT)\n", gethrtime(), (void *)cmd, (void *)instance));
542412244SYu.Wu@Sun.COM 	}
54259440SSusan.Scheufele@Sun.COM 	/* Issue the command to the FW */
54269440SSusan.Scheufele@Sun.COM 	WR_IB_QPORT((cmd->frame_phys_addr) |
54279440SSusan.Scheufele@Sun.COM 	    (((cmd->frame_count - 1) << 1) | 1), instance);
54289440SSusan.Scheufele@Sun.COM }
54299440SSusan.Scheufele@Sun.COM 
54309440SSusan.Scheufele@Sun.COM /*
54319440SSusan.Scheufele@Sun.COM  * issue_cmd_in_sync_mode
54329440SSusan.Scheufele@Sun.COM  */
54339440SSusan.Scheufele@Sun.COM static int
issue_cmd_in_sync_mode_ppc(struct mrsas_instance * instance,struct mrsas_cmd * cmd)54349440SSusan.Scheufele@Sun.COM issue_cmd_in_sync_mode_ppc(struct mrsas_instance *instance,
543512244SYu.Wu@Sun.COM struct mrsas_cmd *cmd)
54369440SSusan.Scheufele@Sun.COM {
543712244SYu.Wu@Sun.COM 	int	i;
54389440SSusan.Scheufele@Sun.COM 	uint32_t	msecs = MFI_POLL_TIMEOUT_SECS * (10 * MILLISEC);
5439*12757SYu.Wu@Sun.COM 	struct mrsas_header *hdr = &cmd->frame->hdr;
54409440SSusan.Scheufele@Sun.COM 
54419440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: called"));
54429440SSusan.Scheufele@Sun.COM 
544312244SYu.Wu@Sun.COM 	if (instance->adapterresetinprogress) {
5444*12757SYu.Wu@Sun.COM 		cmd->drv_pkt_time = ddi_get16(
5445*12757SYu.Wu@Sun.COM 		    cmd->frame_dma_obj.acc_handle, &hdr->timeout);
5446*12757SYu.Wu@Sun.COM 		if (cmd->drv_pkt_time < debug_timeout_g)
5447*12757SYu.Wu@Sun.COM 			cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
544812244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: "
544912244SYu.Wu@Sun.COM 		    "issue and return in reset case\n"));
545012244SYu.Wu@Sun.COM 		WR_IB_QPORT((cmd->frame_phys_addr) |
545112244SYu.Wu@Sun.COM 		    (((cmd->frame_count - 1) << 1) | 1), instance);
545212244SYu.Wu@Sun.COM 		return (DDI_SUCCESS);
545312244SYu.Wu@Sun.COM 	} else {
545412244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: pushing the pkt\n"));
545512244SYu.Wu@Sun.COM 		push_pending_mfi_pkt(instance, cmd);
545612244SYu.Wu@Sun.COM 	}
545712244SYu.Wu@Sun.COM 
54589440SSusan.Scheufele@Sun.COM 	cmd->cmd_status	= ENODATA;
54599440SSusan.Scheufele@Sun.COM 
54609440SSusan.Scheufele@Sun.COM 	WR_IB_QPORT((cmd->frame_phys_addr) |
54619440SSusan.Scheufele@Sun.COM 	    (((cmd->frame_count - 1) << 1) | 1), instance);
54629440SSusan.Scheufele@Sun.COM 
54639440SSusan.Scheufele@Sun.COM 	mutex_enter(&instance->int_cmd_mtx);
54649440SSusan.Scheufele@Sun.COM 
54659440SSusan.Scheufele@Sun.COM 	for (i = 0; i < msecs && (cmd->cmd_status == ENODATA); i++) {
54669440SSusan.Scheufele@Sun.COM 		cv_wait(&instance->int_cmd_cv, &instance->int_cmd_mtx);
54679440SSusan.Scheufele@Sun.COM 	}
54689440SSusan.Scheufele@Sun.COM 
54699440SSusan.Scheufele@Sun.COM 	mutex_exit(&instance->int_cmd_mtx);
54709440SSusan.Scheufele@Sun.COM 
54719440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: done"));
54729440SSusan.Scheufele@Sun.COM 
54739440SSusan.Scheufele@Sun.COM 	if (i < (msecs -1)) {
54749440SSusan.Scheufele@Sun.COM 		return (DDI_SUCCESS);
54759440SSusan.Scheufele@Sun.COM 	} else {
54769440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
54779440SSusan.Scheufele@Sun.COM 	}
54789440SSusan.Scheufele@Sun.COM }
54799440SSusan.Scheufele@Sun.COM 
54809440SSusan.Scheufele@Sun.COM /*
54819440SSusan.Scheufele@Sun.COM  * issue_cmd_in_poll_mode
54829440SSusan.Scheufele@Sun.COM  */
54839440SSusan.Scheufele@Sun.COM static int
issue_cmd_in_poll_mode_ppc(struct mrsas_instance * instance,struct mrsas_cmd * cmd)54849440SSusan.Scheufele@Sun.COM issue_cmd_in_poll_mode_ppc(struct mrsas_instance *instance,
54859440SSusan.Scheufele@Sun.COM     struct mrsas_cmd *cmd)
54869440SSusan.Scheufele@Sun.COM {
54879440SSusan.Scheufele@Sun.COM 	int		i;
54889440SSusan.Scheufele@Sun.COM 	uint16_t	flags;
54899440SSusan.Scheufele@Sun.COM 	uint32_t	msecs = MFI_POLL_TIMEOUT_SECS * MILLISEC;
54909440SSusan.Scheufele@Sun.COM 	struct mrsas_header *frame_hdr;
54919440SSusan.Scheufele@Sun.COM 
54929440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_poll_mode_ppc: called"));
54939440SSusan.Scheufele@Sun.COM 
54949440SSusan.Scheufele@Sun.COM 	frame_hdr = (struct mrsas_header *)cmd->frame;
54959440SSusan.Scheufele@Sun.COM 	ddi_put8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status,
54969440SSusan.Scheufele@Sun.COM 	    MFI_CMD_STATUS_POLL_MODE);
54979440SSusan.Scheufele@Sun.COM 	flags = ddi_get16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags);
54989440SSusan.Scheufele@Sun.COM 	flags 	|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
54999440SSusan.Scheufele@Sun.COM 
55009440SSusan.Scheufele@Sun.COM 	ddi_put16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags, flags);
55019440SSusan.Scheufele@Sun.COM 
55029440SSusan.Scheufele@Sun.COM 	/* issue the frame using inbound queue port */
55039440SSusan.Scheufele@Sun.COM 	WR_IB_QPORT((cmd->frame_phys_addr) |
55049440SSusan.Scheufele@Sun.COM 	    (((cmd->frame_count - 1) << 1) | 1), instance);
55059440SSusan.Scheufele@Sun.COM 
55069440SSusan.Scheufele@Sun.COM 	/* wait for cmd_status to change from 0xFF */
55079440SSusan.Scheufele@Sun.COM 	for (i = 0; i < msecs && (
55089440SSusan.Scheufele@Sun.COM 	    ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status)
55099440SSusan.Scheufele@Sun.COM 	    == MFI_CMD_STATUS_POLL_MODE); i++) {
55109440SSusan.Scheufele@Sun.COM 		drv_usecwait(MILLISEC); /* wait for 1000 usecs */
55119440SSusan.Scheufele@Sun.COM 	}
55129440SSusan.Scheufele@Sun.COM 
55139440SSusan.Scheufele@Sun.COM 	if (ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status)
55149440SSusan.Scheufele@Sun.COM 	    == MFI_CMD_STATUS_POLL_MODE) {
551512356SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_poll_mode: "
55169440SSusan.Scheufele@Sun.COM 		    "cmd polling timed out"));
55179440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
55189440SSusan.Scheufele@Sun.COM 	}
55199440SSusan.Scheufele@Sun.COM 
55209440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
55219440SSusan.Scheufele@Sun.COM }
55229440SSusan.Scheufele@Sun.COM 
55239440SSusan.Scheufele@Sun.COM static void
enable_intr_ppc(struct mrsas_instance * instance)55249440SSusan.Scheufele@Sun.COM enable_intr_ppc(struct mrsas_instance *instance)
55259440SSusan.Scheufele@Sun.COM {
55269440SSusan.Scheufele@Sun.COM 	uint32_t	mask;
55279440SSusan.Scheufele@Sun.COM 
55289440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: called"));
55299440SSusan.Scheufele@Sun.COM 
55309440SSusan.Scheufele@Sun.COM 	/* WR_OB_DOORBELL_CLEAR(0xFFFFFFFF, instance); */
55319440SSusan.Scheufele@Sun.COM 	WR_OB_DOORBELL_CLEAR(OB_DOORBELL_CLEAR_MASK, instance);
55329440SSusan.Scheufele@Sun.COM 
55339440SSusan.Scheufele@Sun.COM 	/* WR_OB_INTR_MASK(~0x80000000, instance); */
55349440SSusan.Scheufele@Sun.COM 	WR_OB_INTR_MASK(~(MFI_REPLY_2108_MESSAGE_INTR_MASK), instance);
55359440SSusan.Scheufele@Sun.COM 
55369440SSusan.Scheufele@Sun.COM 	/* dummy read to force PCI flush */
55379440SSusan.Scheufele@Sun.COM 	mask = RD_OB_INTR_MASK(instance);
55389440SSusan.Scheufele@Sun.COM 
55399440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: "
55409440SSusan.Scheufele@Sun.COM 	    "outbound_intr_mask = 0x%x", mask));
55419440SSusan.Scheufele@Sun.COM }
55429440SSusan.Scheufele@Sun.COM 
55439440SSusan.Scheufele@Sun.COM static void
disable_intr_ppc(struct mrsas_instance * instance)55449440SSusan.Scheufele@Sun.COM disable_intr_ppc(struct mrsas_instance *instance)
55459440SSusan.Scheufele@Sun.COM {
55469440SSusan.Scheufele@Sun.COM 	uint32_t	mask;
55479440SSusan.Scheufele@Sun.COM 
55489440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: called"));
55499440SSusan.Scheufele@Sun.COM 
55509440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: before : "
55519440SSusan.Scheufele@Sun.COM 	    "outbound_intr_mask = 0x%x", RD_OB_INTR_MASK(instance)));
55529440SSusan.Scheufele@Sun.COM 
55539440SSusan.Scheufele@Sun.COM 	/* WR_OB_INTR_MASK(0xFFFFFFFF, instance); */
55549440SSusan.Scheufele@Sun.COM 	WR_OB_INTR_MASK(OB_INTR_MASK, instance);
55559440SSusan.Scheufele@Sun.COM 
55569440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: after : "
55579440SSusan.Scheufele@Sun.COM 	    "outbound_intr_mask = 0x%x", RD_OB_INTR_MASK(instance)));
55589440SSusan.Scheufele@Sun.COM 
55599440SSusan.Scheufele@Sun.COM 	/* dummy read to force PCI flush */
55609440SSusan.Scheufele@Sun.COM 	mask = RD_OB_INTR_MASK(instance);
55619440SSusan.Scheufele@Sun.COM #ifdef lint
55629440SSusan.Scheufele@Sun.COM 	mask = mask;
55639440SSusan.Scheufele@Sun.COM #endif
55649440SSusan.Scheufele@Sun.COM }
55659440SSusan.Scheufele@Sun.COM 
55669440SSusan.Scheufele@Sun.COM static int
intr_ack_ppc(struct mrsas_instance * instance)55679440SSusan.Scheufele@Sun.COM intr_ack_ppc(struct mrsas_instance *instance)
55689440SSusan.Scheufele@Sun.COM {
55699440SSusan.Scheufele@Sun.COM 	uint32_t	status;
55709795SYu.Wu@Sun.COM 	int ret = DDI_INTR_CLAIMED;
55719440SSusan.Scheufele@Sun.COM 
55729440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: called"));
55739440SSusan.Scheufele@Sun.COM 
55749440SSusan.Scheufele@Sun.COM 	/* check if it is our interrupt */
55759440SSusan.Scheufele@Sun.COM 	status = RD_OB_INTR_STATUS(instance);
55769440SSusan.Scheufele@Sun.COM 
55779440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: status = 0x%x", status));
55789440SSusan.Scheufele@Sun.COM 
55799440SSusan.Scheufele@Sun.COM 	if (!(status & MFI_REPLY_2108_MESSAGE_INTR)) {
55809795SYu.Wu@Sun.COM 		ret = DDI_INTR_UNCLAIMED;
55819795SYu.Wu@Sun.COM 	}
55829795SYu.Wu@Sun.COM 
55839795SYu.Wu@Sun.COM 	if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
558410600SYu.Wu@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
55859795SYu.Wu@Sun.COM 		ret = DDI_INTR_UNCLAIMED;
55869795SYu.Wu@Sun.COM 	}
55879795SYu.Wu@Sun.COM 
55889795SYu.Wu@Sun.COM 	if (ret == DDI_INTR_UNCLAIMED) {
55899795SYu.Wu@Sun.COM 		return (ret);
55909795SYu.Wu@Sun.COM 	}
55919440SSusan.Scheufele@Sun.COM 	/* clear the interrupt by writing back the same value */
55929440SSusan.Scheufele@Sun.COM 	WR_OB_DOORBELL_CLEAR(status, instance);
55939440SSusan.Scheufele@Sun.COM 
55949440SSusan.Scheufele@Sun.COM 	/* dummy READ */
55959440SSusan.Scheufele@Sun.COM 	status = RD_OB_INTR_STATUS(instance);
55969440SSusan.Scheufele@Sun.COM 
55979440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: interrupt cleared"));
55989440SSusan.Scheufele@Sun.COM 
55999795SYu.Wu@Sun.COM 	return (ret);
56009440SSusan.Scheufele@Sun.COM }
56019440SSusan.Scheufele@Sun.COM 
560212244SYu.Wu@Sun.COM /*
560312244SYu.Wu@Sun.COM  * Marks HBA as bad. This will be called either when an
560412244SYu.Wu@Sun.COM  * IO packet times out even after 3 FW resets
560512244SYu.Wu@Sun.COM  * or FW is found to be fault even after 3 continuous resets.
560612244SYu.Wu@Sun.COM  */
560712244SYu.Wu@Sun.COM 
560812244SYu.Wu@Sun.COM static int
mrsas_kill_adapter(struct mrsas_instance * instance)560912244SYu.Wu@Sun.COM mrsas_kill_adapter(struct mrsas_instance *instance)
561012244SYu.Wu@Sun.COM {
561112244SYu.Wu@Sun.COM 		if (instance->deadadapter == 1)
561212244SYu.Wu@Sun.COM 			return (DDI_FAILURE);
561312244SYu.Wu@Sun.COM 
561412244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "mrsas_kill_adapter: "
561512244SYu.Wu@Sun.COM 		    "Writing to doorbell with MFI_STOP_ADP "));
561612244SYu.Wu@Sun.COM 		mutex_enter(&instance->ocr_flags_mtx);
561712244SYu.Wu@Sun.COM 		instance->deadadapter = 1;
561812244SYu.Wu@Sun.COM 		mutex_exit(&instance->ocr_flags_mtx);
561912244SYu.Wu@Sun.COM 		instance->func_ptr->disable_intr(instance);
562012244SYu.Wu@Sun.COM 		WR_IB_DOORBELL(MFI_STOP_ADP, instance);
562112244SYu.Wu@Sun.COM 		(void) mrsas_complete_pending_cmds(instance);
562212244SYu.Wu@Sun.COM 		return (DDI_SUCCESS);
562312244SYu.Wu@Sun.COM }
562412244SYu.Wu@Sun.COM 
562512244SYu.Wu@Sun.COM 
562612244SYu.Wu@Sun.COM static int
mrsas_reset_ppc(struct mrsas_instance * instance)562712244SYu.Wu@Sun.COM mrsas_reset_ppc(struct mrsas_instance *instance)
562812244SYu.Wu@Sun.COM {
562912244SYu.Wu@Sun.COM 	uint32_t status;
563012244SYu.Wu@Sun.COM 	uint32_t retry = 0;
563112244SYu.Wu@Sun.COM 	uint32_t cur_abs_reg_val;
563212244SYu.Wu@Sun.COM 	uint32_t fw_state;
563312244SYu.Wu@Sun.COM 
563412244SYu.Wu@Sun.COM 	if (instance->deadadapter == 1) {
563512244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
563612244SYu.Wu@Sun.COM 		    "no more resets as HBA has been marked dead "));
563712244SYu.Wu@Sun.COM 		return (DDI_FAILURE);
563812244SYu.Wu@Sun.COM 	}
563912244SYu.Wu@Sun.COM 	mutex_enter(&instance->ocr_flags_mtx);
564012244SYu.Wu@Sun.COM 	instance->adapterresetinprogress = 1;
564112244SYu.Wu@Sun.COM 	mutex_exit(&instance->ocr_flags_mtx);
564212244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: adpterresetinprogress "
564312244SYu.Wu@Sun.COM 	    "flag set, time %llx", gethrtime()));
564412244SYu.Wu@Sun.COM 	instance->func_ptr->disable_intr(instance);
564512244SYu.Wu@Sun.COM retry_reset:
564612244SYu.Wu@Sun.COM 	WR_IB_WRITE_SEQ(0, instance);
564712244SYu.Wu@Sun.COM 	WR_IB_WRITE_SEQ(4, instance);
564812244SYu.Wu@Sun.COM 	WR_IB_WRITE_SEQ(0xb, instance);
564912244SYu.Wu@Sun.COM 	WR_IB_WRITE_SEQ(2, instance);
565012244SYu.Wu@Sun.COM 	WR_IB_WRITE_SEQ(7, instance);
565112244SYu.Wu@Sun.COM 	WR_IB_WRITE_SEQ(0xd, instance);
565212244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: magic number written "
565312244SYu.Wu@Sun.COM 	    "to write sequence register\n"));
565412244SYu.Wu@Sun.COM 	delay(100 * drv_usectohz(MILLISEC));
565512244SYu.Wu@Sun.COM 	status = RD_OB_DRWE(instance);
565612244SYu.Wu@Sun.COM 
565712244SYu.Wu@Sun.COM 	while (!(status & DIAG_WRITE_ENABLE)) {
565812244SYu.Wu@Sun.COM 		delay(100 * drv_usectohz(MILLISEC));
565912244SYu.Wu@Sun.COM 		status = RD_OB_DRWE(instance);
566012244SYu.Wu@Sun.COM 		if (retry++ == 100) {
566112244SYu.Wu@Sun.COM 			con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: DRWE bit "
566212244SYu.Wu@Sun.COM 			    "check retry count %d\n", retry));
566312244SYu.Wu@Sun.COM 			return (DDI_FAILURE);
566412244SYu.Wu@Sun.COM 		}
566512244SYu.Wu@Sun.COM 	}
566612244SYu.Wu@Sun.COM 	WR_IB_DRWE(status | DIAG_RESET_ADAPTER, instance);
566712244SYu.Wu@Sun.COM 	delay(100 * drv_usectohz(MILLISEC));
566812244SYu.Wu@Sun.COM 	status = RD_OB_DRWE(instance);
566912244SYu.Wu@Sun.COM 	while (status & DIAG_RESET_ADAPTER) {
567012244SYu.Wu@Sun.COM 		delay(100 * drv_usectohz(MILLISEC));
567112244SYu.Wu@Sun.COM 		status = RD_OB_DRWE(instance);
567212244SYu.Wu@Sun.COM 		if (retry++ == 100) {
567312244SYu.Wu@Sun.COM 			(void) mrsas_kill_adapter(instance);
567412244SYu.Wu@Sun.COM 			return (DDI_FAILURE);
567512244SYu.Wu@Sun.COM 		}
567612244SYu.Wu@Sun.COM 	}
567712244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: Adapter reset complete"));
567812244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
567912244SYu.Wu@Sun.COM 	    "Calling mfi_state_transition_to_ready"));
568012244SYu.Wu@Sun.COM 
568112244SYu.Wu@Sun.COM 	/* Mark HBA as bad, if FW is fault after 3 continuous resets */
568212244SYu.Wu@Sun.COM 	if (mfi_state_transition_to_ready(instance) ||
568312244SYu.Wu@Sun.COM 	    debug_fw_faults_after_ocr_g == 1) {
568412244SYu.Wu@Sun.COM 		cur_abs_reg_val =
568512244SYu.Wu@Sun.COM 		    instance->func_ptr->read_fw_status_reg(instance);
568612244SYu.Wu@Sun.COM 		fw_state	= cur_abs_reg_val & MFI_STATE_MASK;
568712244SYu.Wu@Sun.COM 
568812244SYu.Wu@Sun.COM #ifdef OCRDEBUG
568912244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
569012244SYu.Wu@Sun.COM 		    "mrsas_reset_ppc :before fake: FW is not ready "
569112244SYu.Wu@Sun.COM 		    "FW state = 0x%x", fw_state));
569212244SYu.Wu@Sun.COM 		if (debug_fw_faults_after_ocr_g == 1)
569312244SYu.Wu@Sun.COM 			fw_state = MFI_STATE_FAULT;
569412244SYu.Wu@Sun.COM #endif
569512244SYu.Wu@Sun.COM 
569612244SYu.Wu@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,  "mrsas_reset_ppc : FW is not ready "
569712244SYu.Wu@Sun.COM 		    "FW state = 0x%x", fw_state));
569812244SYu.Wu@Sun.COM 
569912244SYu.Wu@Sun.COM 		if (fw_state == MFI_STATE_FAULT) {
5700*12757SYu.Wu@Sun.COM 			/* increment the count */
570112244SYu.Wu@Sun.COM 			instance->fw_fault_count_after_ocr++;
570212244SYu.Wu@Sun.COM 			if (instance->fw_fault_count_after_ocr
570312244SYu.Wu@Sun.COM 			    < MAX_FW_RESET_COUNT) {
570412244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_WARN, "mrsas_reset_ppc: "
570512244SYu.Wu@Sun.COM 				    "FW is in fault after OCR count %d ",
570612244SYu.Wu@Sun.COM 				    instance->fw_fault_count_after_ocr));
570712244SYu.Wu@Sun.COM 				goto retry_reset;
570812244SYu.Wu@Sun.COM 
570912244SYu.Wu@Sun.COM 			} else {
571012244SYu.Wu@Sun.COM 				con_log(CL_ANN1, (CE_WARN, "mrsas_reset_ppc: "
571112244SYu.Wu@Sun.COM 				    "Max Reset Count exceeded "
571212244SYu.Wu@Sun.COM 				    "Mark HBA as bad"));
571312244SYu.Wu@Sun.COM 				(void) mrsas_kill_adapter(instance);
571412244SYu.Wu@Sun.COM 				return (DDI_FAILURE);
571512244SYu.Wu@Sun.COM 			}
571612244SYu.Wu@Sun.COM 		}
571712244SYu.Wu@Sun.COM 	}
5718*12757SYu.Wu@Sun.COM 	/* reset the counter as FW is up after OCR */
571912244SYu.Wu@Sun.COM 	instance->fw_fault_count_after_ocr = 0;
572012244SYu.Wu@Sun.COM 
572112244SYu.Wu@Sun.COM 
572212244SYu.Wu@Sun.COM 	ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
572312244SYu.Wu@Sun.COM 	    instance->producer, 0);
572412244SYu.Wu@Sun.COM 
572512244SYu.Wu@Sun.COM 	ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
572612244SYu.Wu@Sun.COM 	    instance->consumer, 0);
572712244SYu.Wu@Sun.COM 
572812244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
572912244SYu.Wu@Sun.COM 	    " after resetting produconsumer chck indexs:"
573012244SYu.Wu@Sun.COM 	    "producer %x consumer %x", *instance->producer,
573112244SYu.Wu@Sun.COM 	    *instance->consumer));
573212244SYu.Wu@Sun.COM 
573312244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
573412244SYu.Wu@Sun.COM 	    "Calling mrsas_issue_init_mfi"));
573512244SYu.Wu@Sun.COM 	(void) mrsas_issue_init_mfi(instance);
573612244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
573712244SYu.Wu@Sun.COM 	    "mrsas_issue_init_mfi Done"));
573812244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
573912244SYu.Wu@Sun.COM 	    "Calling mrsas_print_pending_cmd\n"));
574012244SYu.Wu@Sun.COM 	(void) mrsas_print_pending_cmds(instance);
574112244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
574212244SYu.Wu@Sun.COM 	    "mrsas_print_pending_cmd done\n"));
574312244SYu.Wu@Sun.COM 	instance->func_ptr->enable_intr(instance);
574412244SYu.Wu@Sun.COM 	instance->fw_outstanding = 0;
574512244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
574612244SYu.Wu@Sun.COM 	    "Calling mrsas_issue_pending_cmds"));
574712244SYu.Wu@Sun.COM 	(void) mrsas_issue_pending_cmds(instance);
574812244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
574912244SYu.Wu@Sun.COM 	"Complete"));
575012244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
575112244SYu.Wu@Sun.COM 	    "Calling aen registration"));
575212244SYu.Wu@Sun.COM 	instance->func_ptr->issue_cmd(instance->aen_cmd, instance);
575312244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "Unsetting adpresetinprogress flag.\n"));
575412244SYu.Wu@Sun.COM 	mutex_enter(&instance->ocr_flags_mtx);
575512244SYu.Wu@Sun.COM 	instance->adapterresetinprogress = 0;
575612244SYu.Wu@Sun.COM 	mutex_exit(&instance->ocr_flags_mtx);
575712244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
575812244SYu.Wu@Sun.COM 	    "adpterresetinprogress flag unset"));
575912244SYu.Wu@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc done\n"));
576012244SYu.Wu@Sun.COM 	return (DDI_SUCCESS);
576112244SYu.Wu@Sun.COM }
57629440SSusan.Scheufele@Sun.COM static int
mrsas_common_check(struct mrsas_instance * instance,struct mrsas_cmd * cmd)57639440SSusan.Scheufele@Sun.COM mrsas_common_check(struct mrsas_instance *instance,
57649440SSusan.Scheufele@Sun.COM     struct  mrsas_cmd *cmd)
57659440SSusan.Scheufele@Sun.COM {
57669440SSusan.Scheufele@Sun.COM 	int ret = DDI_SUCCESS;
57679440SSusan.Scheufele@Sun.COM 
57689440SSusan.Scheufele@Sun.COM 	if (mrsas_check_dma_handle(cmd->frame_dma_obj.dma_handle) !=
57699440SSusan.Scheufele@Sun.COM 	    DDI_SUCCESS) {
57709440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
57719440SSusan.Scheufele@Sun.COM 		if (cmd->pkt != NULL) {
57729440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_reason = CMD_TRAN_ERR;
57739440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_statistics = 0;
57749440SSusan.Scheufele@Sun.COM 		}
57759440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
57769440SSusan.Scheufele@Sun.COM 	}
57779440SSusan.Scheufele@Sun.COM 	if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle)
57789440SSusan.Scheufele@Sun.COM 	    != DDI_SUCCESS) {
57799440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
57809440SSusan.Scheufele@Sun.COM 		if (cmd->pkt != NULL) {
57819440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_reason = CMD_TRAN_ERR;
57829440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_statistics = 0;
57839440SSusan.Scheufele@Sun.COM 		}
57849440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
57859440SSusan.Scheufele@Sun.COM 	}
57869440SSusan.Scheufele@Sun.COM 	if (mrsas_check_dma_handle(instance->mfi_evt_detail_obj.dma_handle) !=
57879440SSusan.Scheufele@Sun.COM 	    DDI_SUCCESS) {
57889440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
57899440SSusan.Scheufele@Sun.COM 		if (cmd->pkt != NULL) {
57909440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_reason = CMD_TRAN_ERR;
57919440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_statistics = 0;
57929440SSusan.Scheufele@Sun.COM 		}
57939440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
57949440SSusan.Scheufele@Sun.COM 	}
57959440SSusan.Scheufele@Sun.COM 	if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
57969440SSusan.Scheufele@Sun.COM 		ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
57979440SSusan.Scheufele@Sun.COM 
57989440SSusan.Scheufele@Sun.COM 		ddi_fm_acc_err_clear(instance->regmap_handle, DDI_FME_VER0);
57999440SSusan.Scheufele@Sun.COM 
58009440SSusan.Scheufele@Sun.COM 		if (cmd->pkt != NULL) {
58019440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_reason = CMD_TRAN_ERR;
58029440SSusan.Scheufele@Sun.COM 			cmd->pkt->pkt_statistics = 0;
58039440SSusan.Scheufele@Sun.COM 		}
58049440SSusan.Scheufele@Sun.COM 		ret = DDI_FAILURE;
58059440SSusan.Scheufele@Sun.COM 	}
58069440SSusan.Scheufele@Sun.COM 
58079440SSusan.Scheufele@Sun.COM 	return (ret);
58089440SSusan.Scheufele@Sun.COM }
58099440SSusan.Scheufele@Sun.COM 
58109440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
58119440SSusan.Scheufele@Sun.COM static int
mrsas_fm_error_cb(dev_info_t * dip,ddi_fm_error_t * err,const void * impl_data)58129440SSusan.Scheufele@Sun.COM mrsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
58139440SSusan.Scheufele@Sun.COM {
58149440SSusan.Scheufele@Sun.COM 	/*
58159440SSusan.Scheufele@Sun.COM 	 * as the driver can always deal with an error in any dma or
58169440SSusan.Scheufele@Sun.COM 	 * access handle, we can just return the fme_status value.
58179440SSusan.Scheufele@Sun.COM 	 */
58189440SSusan.Scheufele@Sun.COM 	pci_ereport_post(dip, err, NULL);
58199440SSusan.Scheufele@Sun.COM 	return (err->fme_status);
58209440SSusan.Scheufele@Sun.COM }
58219440SSusan.Scheufele@Sun.COM 
58229440SSusan.Scheufele@Sun.COM static void
mrsas_fm_init(struct mrsas_instance * instance)58239440SSusan.Scheufele@Sun.COM mrsas_fm_init(struct mrsas_instance *instance)
58249440SSusan.Scheufele@Sun.COM {
58259440SSusan.Scheufele@Sun.COM 	/* Need to change iblock to priority for new MSI intr */
58269440SSusan.Scheufele@Sun.COM 	ddi_iblock_cookie_t fm_ibc;
58279440SSusan.Scheufele@Sun.COM 
58289440SSusan.Scheufele@Sun.COM 	/* Only register with IO Fault Services if we have some capability */
58299440SSusan.Scheufele@Sun.COM 	if (instance->fm_capabilities) {
58309440SSusan.Scheufele@Sun.COM 		/* Adjust access and dma attributes for FMA */
58319440SSusan.Scheufele@Sun.COM 		endian_attr.devacc_attr_access = DDI_FLAGERR_ACC;
58329440SSusan.Scheufele@Sun.COM 		mrsas_generic_dma_attr.dma_attr_flags = DDI_DMA_FLAGERR;
58339440SSusan.Scheufele@Sun.COM 
58349440SSusan.Scheufele@Sun.COM 		/*
58359440SSusan.Scheufele@Sun.COM 		 * Register capabilities with IO Fault Services.
58369440SSusan.Scheufele@Sun.COM 		 * fm_capabilities will be updated to indicate
58379440SSusan.Scheufele@Sun.COM 		 * capabilities actually supported (not requested.)
58389440SSusan.Scheufele@Sun.COM 		 */
58399440SSusan.Scheufele@Sun.COM 
58409440SSusan.Scheufele@Sun.COM 		ddi_fm_init(instance->dip, &instance->fm_capabilities, &fm_ibc);
58419440SSusan.Scheufele@Sun.COM 
58429440SSusan.Scheufele@Sun.COM 		/*
58439440SSusan.Scheufele@Sun.COM 		 * Initialize pci ereport capabilities if ereport
58449440SSusan.Scheufele@Sun.COM 		 * capable (should always be.)
58459440SSusan.Scheufele@Sun.COM 		 */
58469440SSusan.Scheufele@Sun.COM 
58479440SSusan.Scheufele@Sun.COM 		if (DDI_FM_EREPORT_CAP(instance->fm_capabilities) ||
58489440SSusan.Scheufele@Sun.COM 		    DDI_FM_ERRCB_CAP(instance->fm_capabilities)) {
58499440SSusan.Scheufele@Sun.COM 			pci_ereport_setup(instance->dip);
58509440SSusan.Scheufele@Sun.COM 		}
58519440SSusan.Scheufele@Sun.COM 
58529440SSusan.Scheufele@Sun.COM 		/*
58539440SSusan.Scheufele@Sun.COM 		 * Register error callback if error callback capable.
58549440SSusan.Scheufele@Sun.COM 		 */
58559440SSusan.Scheufele@Sun.COM 		if (DDI_FM_ERRCB_CAP(instance->fm_capabilities)) {
58569440SSusan.Scheufele@Sun.COM 			ddi_fm_handler_register(instance->dip,
58579440SSusan.Scheufele@Sun.COM 			    mrsas_fm_error_cb, (void*) instance);
58589440SSusan.Scheufele@Sun.COM 		}
58599440SSusan.Scheufele@Sun.COM 	} else {
58609440SSusan.Scheufele@Sun.COM 		endian_attr.devacc_attr_access = DDI_DEFAULT_ACC;
58619440SSusan.Scheufele@Sun.COM 		mrsas_generic_dma_attr.dma_attr_flags = 0;
58629440SSusan.Scheufele@Sun.COM 	}
58639440SSusan.Scheufele@Sun.COM }
58649440SSusan.Scheufele@Sun.COM 
58659440SSusan.Scheufele@Sun.COM static void
mrsas_fm_fini(struct mrsas_instance * instance)58669440SSusan.Scheufele@Sun.COM mrsas_fm_fini(struct mrsas_instance *instance)
58679440SSusan.Scheufele@Sun.COM {
58689440SSusan.Scheufele@Sun.COM 	/* Only unregister FMA capabilities if registered */
58699440SSusan.Scheufele@Sun.COM 	if (instance->fm_capabilities) {
58709440SSusan.Scheufele@Sun.COM 		/*
58719440SSusan.Scheufele@Sun.COM 		 * Un-register error callback if error callback capable.
58729440SSusan.Scheufele@Sun.COM 		 */
58739440SSusan.Scheufele@Sun.COM 		if (DDI_FM_ERRCB_CAP(instance->fm_capabilities)) {
58749440SSusan.Scheufele@Sun.COM 			ddi_fm_handler_unregister(instance->dip);
58759440SSusan.Scheufele@Sun.COM 		}
58769440SSusan.Scheufele@Sun.COM 
58779440SSusan.Scheufele@Sun.COM 		/*
58789440SSusan.Scheufele@Sun.COM 		 * Release any resources allocated by pci_ereport_setup()
58799440SSusan.Scheufele@Sun.COM 		 */
58809440SSusan.Scheufele@Sun.COM 		if (DDI_FM_EREPORT_CAP(instance->fm_capabilities) ||
58819440SSusan.Scheufele@Sun.COM 		    DDI_FM_ERRCB_CAP(instance->fm_capabilities)) {
58829440SSusan.Scheufele@Sun.COM 			pci_ereport_teardown(instance->dip);
58839440SSusan.Scheufele@Sun.COM 		}
58849440SSusan.Scheufele@Sun.COM 
58859440SSusan.Scheufele@Sun.COM 		/* Unregister from IO Fault Services */
58869440SSusan.Scheufele@Sun.COM 		ddi_fm_fini(instance->dip);
58879440SSusan.Scheufele@Sun.COM 
58889440SSusan.Scheufele@Sun.COM 		/* Adjust access and dma attributes for FMA */
58899440SSusan.Scheufele@Sun.COM 		endian_attr.devacc_attr_access = DDI_DEFAULT_ACC;
58909440SSusan.Scheufele@Sun.COM 		mrsas_generic_dma_attr.dma_attr_flags = 0;
58919440SSusan.Scheufele@Sun.COM 	}
58929440SSusan.Scheufele@Sun.COM }
58939440SSusan.Scheufele@Sun.COM 
58949440SSusan.Scheufele@Sun.COM int
mrsas_check_acc_handle(ddi_acc_handle_t handle)58959440SSusan.Scheufele@Sun.COM mrsas_check_acc_handle(ddi_acc_handle_t handle)
58969440SSusan.Scheufele@Sun.COM {
58979440SSusan.Scheufele@Sun.COM 	ddi_fm_error_t de;
58989440SSusan.Scheufele@Sun.COM 
58999440SSusan.Scheufele@Sun.COM 	if (handle == NULL) {
59009440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
59019440SSusan.Scheufele@Sun.COM 	}
59029440SSusan.Scheufele@Sun.COM 
59039440SSusan.Scheufele@Sun.COM 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
59049440SSusan.Scheufele@Sun.COM 
59059440SSusan.Scheufele@Sun.COM 	return (de.fme_status);
59069440SSusan.Scheufele@Sun.COM }
59079440SSusan.Scheufele@Sun.COM 
59089440SSusan.Scheufele@Sun.COM int
mrsas_check_dma_handle(ddi_dma_handle_t handle)59099440SSusan.Scheufele@Sun.COM mrsas_check_dma_handle(ddi_dma_handle_t handle)
59109440SSusan.Scheufele@Sun.COM {
59119440SSusan.Scheufele@Sun.COM 	ddi_fm_error_t de;
59129440SSusan.Scheufele@Sun.COM 
59139440SSusan.Scheufele@Sun.COM 	if (handle == NULL) {
59149440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
59159440SSusan.Scheufele@Sun.COM 	}
59169440SSusan.Scheufele@Sun.COM 
59179440SSusan.Scheufele@Sun.COM 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
59189440SSusan.Scheufele@Sun.COM 
59199440SSusan.Scheufele@Sun.COM 	return (de.fme_status);
59209440SSusan.Scheufele@Sun.COM }
59219440SSusan.Scheufele@Sun.COM 
59229440SSusan.Scheufele@Sun.COM void
mrsas_fm_ereport(struct mrsas_instance * instance,char * detail)59239440SSusan.Scheufele@Sun.COM mrsas_fm_ereport(struct mrsas_instance *instance, char *detail)
59249440SSusan.Scheufele@Sun.COM {
59259440SSusan.Scheufele@Sun.COM 	uint64_t ena;
59269440SSusan.Scheufele@Sun.COM 	char buf[FM_MAX_CLASS];
59279440SSusan.Scheufele@Sun.COM 
59289440SSusan.Scheufele@Sun.COM 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
59299440SSusan.Scheufele@Sun.COM 	ena = fm_ena_generate(0, FM_ENA_FMT1);
59309440SSusan.Scheufele@Sun.COM 	if (DDI_FM_EREPORT_CAP(instance->fm_capabilities)) {
59319440SSusan.Scheufele@Sun.COM 		ddi_fm_ereport_post(instance->dip, buf, ena, DDI_NOSLEEP,
59329440SSusan.Scheufele@Sun.COM 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERSION, NULL);
59339440SSusan.Scheufele@Sun.COM 	}
59349440SSusan.Scheufele@Sun.COM }
59359440SSusan.Scheufele@Sun.COM 
59369440SSusan.Scheufele@Sun.COM static int
mrsas_add_intrs(struct mrsas_instance * instance,int intr_type)59379440SSusan.Scheufele@Sun.COM mrsas_add_intrs(struct mrsas_instance *instance, int intr_type)
59389440SSusan.Scheufele@Sun.COM {
59399440SSusan.Scheufele@Sun.COM 
59409440SSusan.Scheufele@Sun.COM 	dev_info_t *dip = instance->dip;
59419440SSusan.Scheufele@Sun.COM 	int	avail, actual, count;
59429440SSusan.Scheufele@Sun.COM 	int	i, flag, ret;
59439440SSusan.Scheufele@Sun.COM 
59449440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL1, (CE_WARN, "mrsas_add_intrs: intr_type = %x",
59459440SSusan.Scheufele@Sun.COM 	    intr_type));
59469440SSusan.Scheufele@Sun.COM 
59479440SSusan.Scheufele@Sun.COM 	/* Get number of interrupts */
59489440SSusan.Scheufele@Sun.COM 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
59499440SSusan.Scheufele@Sun.COM 	if ((ret != DDI_SUCCESS) || (count == 0)) {
59509440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "ddi_intr_get_nintrs() failed:"
59519440SSusan.Scheufele@Sun.COM 		    "ret %d count %d", ret, count));
59529440SSusan.Scheufele@Sun.COM 
59539440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
59549440SSusan.Scheufele@Sun.COM 	}
59559440SSusan.Scheufele@Sun.COM 
59569440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL1, (CE_WARN, "mrsas_add_intrs: count = %d ", count));
59579440SSusan.Scheufele@Sun.COM 
59589440SSusan.Scheufele@Sun.COM 	/* Get number of available interrupts */
59599440SSusan.Scheufele@Sun.COM 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
59609440SSusan.Scheufele@Sun.COM 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
59619440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "ddi_intr_get_navail() failed:"
59629440SSusan.Scheufele@Sun.COM 		    "ret %d avail %d", ret, avail));
59639440SSusan.Scheufele@Sun.COM 
59649440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
59659440SSusan.Scheufele@Sun.COM 	}
59669440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL1, (CE_WARN, "mrsas_add_intrs: avail = %d ", avail));
59679440SSusan.Scheufele@Sun.COM 
59689440SSusan.Scheufele@Sun.COM 	/* Only one interrupt routine. So limit the count to 1 */
59699440SSusan.Scheufele@Sun.COM 	if (count > 1) {
59709440SSusan.Scheufele@Sun.COM 		count = 1;
59719440SSusan.Scheufele@Sun.COM 	}
59729440SSusan.Scheufele@Sun.COM 
59739440SSusan.Scheufele@Sun.COM 	/*
59749440SSusan.Scheufele@Sun.COM 	 * Allocate an array of interrupt handlers. Currently we support
59759440SSusan.Scheufele@Sun.COM 	 * only one interrupt. The framework can be extended later.
59769440SSusan.Scheufele@Sun.COM 	 */
59779440SSusan.Scheufele@Sun.COM 	instance->intr_size = count * sizeof (ddi_intr_handle_t);
59789440SSusan.Scheufele@Sun.COM 	instance->intr_htable = kmem_zalloc(instance->intr_size, KM_SLEEP);
59799440SSusan.Scheufele@Sun.COM 	ASSERT(instance->intr_htable);
59809440SSusan.Scheufele@Sun.COM 
59819440SSusan.Scheufele@Sun.COM 	flag = ((intr_type == DDI_INTR_TYPE_MSI) || (intr_type ==
59829440SSusan.Scheufele@Sun.COM 	    DDI_INTR_TYPE_MSIX)) ? DDI_INTR_ALLOC_STRICT:DDI_INTR_ALLOC_NORMAL;
59839440SSusan.Scheufele@Sun.COM 
59849440SSusan.Scheufele@Sun.COM 	/* Allocate interrupt */
59859440SSusan.Scheufele@Sun.COM 	ret = ddi_intr_alloc(dip, instance->intr_htable, intr_type, 0,
59869440SSusan.Scheufele@Sun.COM 	    count, &actual, flag);
59879440SSusan.Scheufele@Sun.COM 
59889440SSusan.Scheufele@Sun.COM 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
59899440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
59909440SSusan.Scheufele@Sun.COM 		    "avail = %d", avail));
59919440SSusan.Scheufele@Sun.COM 		kmem_free(instance->intr_htable, instance->intr_size);
59929440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
59939440SSusan.Scheufele@Sun.COM 	}
59949440SSusan.Scheufele@Sun.COM 	if (actual < count) {
59959440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
59969440SSusan.Scheufele@Sun.COM 		    "Requested = %d  Received = %d", count, actual));
59979440SSusan.Scheufele@Sun.COM 	}
59989440SSusan.Scheufele@Sun.COM 	instance->intr_cnt = actual;
59999440SSusan.Scheufele@Sun.COM 
60009440SSusan.Scheufele@Sun.COM 	/*
60019440SSusan.Scheufele@Sun.COM 	 * Get the priority of the interrupt allocated.
60029440SSusan.Scheufele@Sun.COM 	 */
60039440SSusan.Scheufele@Sun.COM 	if ((ret = ddi_intr_get_pri(instance->intr_htable[0],
60049440SSusan.Scheufele@Sun.COM 	    &instance->intr_pri)) != DDI_SUCCESS) {
60059440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
60069440SSusan.Scheufele@Sun.COM 		    "get priority call failed"));
60079440SSusan.Scheufele@Sun.COM 
60089440SSusan.Scheufele@Sun.COM 		for (i = 0; i < actual; i++) {
60099440SSusan.Scheufele@Sun.COM 			(void) ddi_intr_free(instance->intr_htable[i]);
60109440SSusan.Scheufele@Sun.COM 		}
60119440SSusan.Scheufele@Sun.COM 		kmem_free(instance->intr_htable, instance->intr_size);
60129440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
60139440SSusan.Scheufele@Sun.COM 	}
60149440SSusan.Scheufele@Sun.COM 
60159440SSusan.Scheufele@Sun.COM 	/*
60169440SSusan.Scheufele@Sun.COM 	 * Test for high level mutex. we don't support them.
60179440SSusan.Scheufele@Sun.COM 	 */
60189440SSusan.Scheufele@Sun.COM 	if (instance->intr_pri >= ddi_intr_get_hilevel_pri()) {
60199440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
60209440SSusan.Scheufele@Sun.COM 		    "High level interrupts not supported."));
60219440SSusan.Scheufele@Sun.COM 
60229440SSusan.Scheufele@Sun.COM 		for (i = 0; i < actual; i++) {
60239440SSusan.Scheufele@Sun.COM 			(void) ddi_intr_free(instance->intr_htable[i]);
60249440SSusan.Scheufele@Sun.COM 		}
60259440SSusan.Scheufele@Sun.COM 		kmem_free(instance->intr_htable, instance->intr_size);
60269440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
60279440SSusan.Scheufele@Sun.COM 	}
60289440SSusan.Scheufele@Sun.COM 
60299440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: intr_pri = 0x%x ",
60309440SSusan.Scheufele@Sun.COM 	    instance->intr_pri));
60319440SSusan.Scheufele@Sun.COM 
60329440SSusan.Scheufele@Sun.COM 	/* Call ddi_intr_add_handler() */
60339440SSusan.Scheufele@Sun.COM 	for (i = 0; i < actual; i++) {
60349440SSusan.Scheufele@Sun.COM 		ret = ddi_intr_add_handler(instance->intr_htable[i],
60359440SSusan.Scheufele@Sun.COM 		    (ddi_intr_handler_t *)mrsas_isr, (caddr_t)instance,
60369440SSusan.Scheufele@Sun.COM 		    (caddr_t)(uintptr_t)i);
60379440SSusan.Scheufele@Sun.COM 
60389440SSusan.Scheufele@Sun.COM 		if (ret != DDI_SUCCESS) {
60399440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs:"
60409440SSusan.Scheufele@Sun.COM 			    "failed %d", ret));
60419440SSusan.Scheufele@Sun.COM 
60429440SSusan.Scheufele@Sun.COM 			for (i = 0; i < actual; i++) {
60439440SSusan.Scheufele@Sun.COM 				(void) ddi_intr_free(instance->intr_htable[i]);
60449440SSusan.Scheufele@Sun.COM 			}
60459440SSusan.Scheufele@Sun.COM 			kmem_free(instance->intr_htable, instance->intr_size);
60469440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
60479440SSusan.Scheufele@Sun.COM 		}
60489440SSusan.Scheufele@Sun.COM 
60499440SSusan.Scheufele@Sun.COM 	}
60509440SSusan.Scheufele@Sun.COM 
60519440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL1, (CE_WARN, " ddi_intr_add_handler done"));
60529440SSusan.Scheufele@Sun.COM 
60539440SSusan.Scheufele@Sun.COM 	if ((ret = ddi_intr_get_cap(instance->intr_htable[0],
60549440SSusan.Scheufele@Sun.COM 	    &instance->intr_cap)) != DDI_SUCCESS) {
60559440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "ddi_intr_get_cap() failed %d",
60569440SSusan.Scheufele@Sun.COM 		    ret));
60579440SSusan.Scheufele@Sun.COM 
60589440SSusan.Scheufele@Sun.COM 		/* Free already allocated intr */
60599440SSusan.Scheufele@Sun.COM 		for (i = 0; i < actual; i++) {
60609440SSusan.Scheufele@Sun.COM 			(void) ddi_intr_remove_handler(
60619440SSusan.Scheufele@Sun.COM 			    instance->intr_htable[i]);
60629440SSusan.Scheufele@Sun.COM 			(void) ddi_intr_free(instance->intr_htable[i]);
60639440SSusan.Scheufele@Sun.COM 		}
60649440SSusan.Scheufele@Sun.COM 		kmem_free(instance->intr_htable, instance->intr_size);
60659440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
60669440SSusan.Scheufele@Sun.COM 	}
60679440SSusan.Scheufele@Sun.COM 
60689440SSusan.Scheufele@Sun.COM 	if (instance->intr_cap &  DDI_INTR_FLAG_BLOCK) {
60699440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_WARN, "Calling ddi_intr_block _enable"));
60709440SSusan.Scheufele@Sun.COM 
60719440SSusan.Scheufele@Sun.COM 		(void) ddi_intr_block_enable(instance->intr_htable,
60729440SSusan.Scheufele@Sun.COM 		    instance->intr_cnt);
60739440SSusan.Scheufele@Sun.COM 	} else {
60749440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN, (CE_NOTE, " calling ddi_intr_enable"));
60759440SSusan.Scheufele@Sun.COM 
60769440SSusan.Scheufele@Sun.COM 		for (i = 0; i < instance->intr_cnt; i++) {
60779440SSusan.Scheufele@Sun.COM 			(void) ddi_intr_enable(instance->intr_htable[i]);
60789440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN, (CE_NOTE, "ddi intr enable returns "
60799440SSusan.Scheufele@Sun.COM 			    "%d", i));
60809440SSusan.Scheufele@Sun.COM 		}
60819440SSusan.Scheufele@Sun.COM 	}
60829440SSusan.Scheufele@Sun.COM 
60839440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
60849440SSusan.Scheufele@Sun.COM 
60859440SSusan.Scheufele@Sun.COM }
60869440SSusan.Scheufele@Sun.COM 
60879440SSusan.Scheufele@Sun.COM 
60889440SSusan.Scheufele@Sun.COM static void
mrsas_rem_intrs(struct mrsas_instance * instance)60899440SSusan.Scheufele@Sun.COM mrsas_rem_intrs(struct mrsas_instance *instance)
60909440SSusan.Scheufele@Sun.COM {
60919440SSusan.Scheufele@Sun.COM 	int i;
60929440SSusan.Scheufele@Sun.COM 
60939440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN, (CE_NOTE, "mrsas_rem_intrs called"));
60949440SSusan.Scheufele@Sun.COM 
60959440SSusan.Scheufele@Sun.COM 	/* Disable all interrupts first */
60969440SSusan.Scheufele@Sun.COM 	if (instance->intr_cap & DDI_INTR_FLAG_BLOCK) {
60979440SSusan.Scheufele@Sun.COM 		(void) ddi_intr_block_disable(instance->intr_htable,
60989440SSusan.Scheufele@Sun.COM 		    instance->intr_cnt);
60999440SSusan.Scheufele@Sun.COM 	} else {
61009440SSusan.Scheufele@Sun.COM 		for (i = 0; i < instance->intr_cnt; i++) {
61019440SSusan.Scheufele@Sun.COM 			(void) ddi_intr_disable(instance->intr_htable[i]);
61029440SSusan.Scheufele@Sun.COM 		}
61039440SSusan.Scheufele@Sun.COM 	}
61049440SSusan.Scheufele@Sun.COM 
61059440SSusan.Scheufele@Sun.COM 	/* Remove all the handlers */
61069440SSusan.Scheufele@Sun.COM 
61079440SSusan.Scheufele@Sun.COM 	for (i = 0; i < instance->intr_cnt; i++) {
61089440SSusan.Scheufele@Sun.COM 		(void) ddi_intr_remove_handler(instance->intr_htable[i]);
61099440SSusan.Scheufele@Sun.COM 		(void) ddi_intr_free(instance->intr_htable[i]);
61109440SSusan.Scheufele@Sun.COM 	}
61119440SSusan.Scheufele@Sun.COM 
61129440SSusan.Scheufele@Sun.COM 	kmem_free(instance->intr_htable, instance->intr_size);
61139440SSusan.Scheufele@Sun.COM }
61149440SSusan.Scheufele@Sun.COM 
61159440SSusan.Scheufele@Sun.COM static int
mrsas_tran_bus_config(dev_info_t * parent,uint_t flags,ddi_bus_config_op_t op,void * arg,dev_info_t ** childp)61169440SSusan.Scheufele@Sun.COM mrsas_tran_bus_config(dev_info_t *parent, uint_t flags,
61179440SSusan.Scheufele@Sun.COM     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
61189440SSusan.Scheufele@Sun.COM {
61199440SSusan.Scheufele@Sun.COM 	struct mrsas_instance *instance;
61209440SSusan.Scheufele@Sun.COM 	int config;
61219440SSusan.Scheufele@Sun.COM 	int rval;
61229440SSusan.Scheufele@Sun.COM 
61239440SSusan.Scheufele@Sun.COM 	char *ptr = NULL;
61249440SSusan.Scheufele@Sun.COM 	int tgt, lun;
61259440SSusan.Scheufele@Sun.COM 
61269440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "Bus config called for op = %x", op));
61279440SSusan.Scheufele@Sun.COM 
61289440SSusan.Scheufele@Sun.COM 	if ((instance = ddi_get_soft_state(mrsas_state,
61299440SSusan.Scheufele@Sun.COM 	    ddi_get_instance(parent))) == NULL) {
61309440SSusan.Scheufele@Sun.COM 		return (NDI_FAILURE);
61319440SSusan.Scheufele@Sun.COM 	}
61329440SSusan.Scheufele@Sun.COM 
61339440SSusan.Scheufele@Sun.COM 	/* Hold nexus during bus_config */
61349440SSusan.Scheufele@Sun.COM 	ndi_devi_enter(parent, &config);
61359440SSusan.Scheufele@Sun.COM 	switch (op) {
61369440SSusan.Scheufele@Sun.COM 	case BUS_CONFIG_ONE: {
61379440SSusan.Scheufele@Sun.COM 
61389440SSusan.Scheufele@Sun.COM 		/* parse wwid/target name out of name given */
61399440SSusan.Scheufele@Sun.COM 		if ((ptr = strchr((char *)arg, '@')) == NULL) {
61409440SSusan.Scheufele@Sun.COM 			rval = NDI_FAILURE;
61419440SSusan.Scheufele@Sun.COM 			break;
61429440SSusan.Scheufele@Sun.COM 		}
61439440SSusan.Scheufele@Sun.COM 		ptr++;
61449440SSusan.Scheufele@Sun.COM 
61459440SSusan.Scheufele@Sun.COM 		if (mrsas_parse_devname(arg, &tgt, &lun) != 0) {
61469440SSusan.Scheufele@Sun.COM 			rval = NDI_FAILURE;
61479440SSusan.Scheufele@Sun.COM 			break;
61489440SSusan.Scheufele@Sun.COM 		}
61499440SSusan.Scheufele@Sun.COM 
61509440SSusan.Scheufele@Sun.COM 		if (lun == 0) {
61519440SSusan.Scheufele@Sun.COM 			rval = mrsas_config_ld(instance, tgt, lun, childp);
61529440SSusan.Scheufele@Sun.COM 		} else {
61539440SSusan.Scheufele@Sun.COM 			rval = NDI_FAILURE;
61549440SSusan.Scheufele@Sun.COM 		}
61559440SSusan.Scheufele@Sun.COM 
61569440SSusan.Scheufele@Sun.COM 		break;
61579440SSusan.Scheufele@Sun.COM 	}
61589440SSusan.Scheufele@Sun.COM 	case BUS_CONFIG_DRIVER:
61599440SSusan.Scheufele@Sun.COM 	case BUS_CONFIG_ALL: {
61609440SSusan.Scheufele@Sun.COM 
61619440SSusan.Scheufele@Sun.COM 		rval = mrsas_config_all_devices(instance);
61629440SSusan.Scheufele@Sun.COM 
61639440SSusan.Scheufele@Sun.COM 		rval = NDI_SUCCESS;
61649440SSusan.Scheufele@Sun.COM 		break;
61659440SSusan.Scheufele@Sun.COM 	}
61669440SSusan.Scheufele@Sun.COM 	}
61679440SSusan.Scheufele@Sun.COM 
61689440SSusan.Scheufele@Sun.COM 	if (rval == NDI_SUCCESS) {
61699440SSusan.Scheufele@Sun.COM 		rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0);
61709440SSusan.Scheufele@Sun.COM 
61719440SSusan.Scheufele@Sun.COM 	}
61729440SSusan.Scheufele@Sun.COM 	ndi_devi_exit(parent, config);
61739440SSusan.Scheufele@Sun.COM 
61749440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_tran_bus_config: rval = %x",
61759440SSusan.Scheufele@Sun.COM 	    rval));
61769440SSusan.Scheufele@Sun.COM 	return (rval);
61779440SSusan.Scheufele@Sun.COM }
61789440SSusan.Scheufele@Sun.COM 
61799440SSusan.Scheufele@Sun.COM static int
mrsas_config_all_devices(struct mrsas_instance * instance)61809440SSusan.Scheufele@Sun.COM mrsas_config_all_devices(struct mrsas_instance *instance)
61819440SSusan.Scheufele@Sun.COM {
61829440SSusan.Scheufele@Sun.COM 	int rval, tgt;
61839440SSusan.Scheufele@Sun.COM 
61849440SSusan.Scheufele@Sun.COM 	for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) {
61859440SSusan.Scheufele@Sun.COM 		(void) mrsas_config_ld(instance, tgt, 0, NULL);
61869440SSusan.Scheufele@Sun.COM 
61879440SSusan.Scheufele@Sun.COM 	}
61889440SSusan.Scheufele@Sun.COM 
61899440SSusan.Scheufele@Sun.COM 	rval = NDI_SUCCESS;
61909440SSusan.Scheufele@Sun.COM 	return (rval);
61919440SSusan.Scheufele@Sun.COM }
61929440SSusan.Scheufele@Sun.COM 
61939440SSusan.Scheufele@Sun.COM static int
mrsas_parse_devname(char * devnm,int * tgt,int * lun)61949440SSusan.Scheufele@Sun.COM mrsas_parse_devname(char *devnm, int *tgt, int *lun)
61959440SSusan.Scheufele@Sun.COM {
61969440SSusan.Scheufele@Sun.COM 	char devbuf[SCSI_MAXNAMELEN];
61979440SSusan.Scheufele@Sun.COM 	char *addr;
61989440SSusan.Scheufele@Sun.COM 	char *p,  *tp, *lp;
61999440SSusan.Scheufele@Sun.COM 	long num;
62009440SSusan.Scheufele@Sun.COM 
62019440SSusan.Scheufele@Sun.COM 	/* Parse dev name and address */
62029440SSusan.Scheufele@Sun.COM 	(void) strcpy(devbuf, devnm);
62039440SSusan.Scheufele@Sun.COM 	addr = "";
62049440SSusan.Scheufele@Sun.COM 	for (p = devbuf; *p != '\0'; p++) {
62059440SSusan.Scheufele@Sun.COM 		if (*p == '@') {
62069440SSusan.Scheufele@Sun.COM 			addr = p + 1;
62079440SSusan.Scheufele@Sun.COM 			*p = '\0';
62089440SSusan.Scheufele@Sun.COM 		} else if (*p == ':') {
62099440SSusan.Scheufele@Sun.COM 			*p = '\0';
62109440SSusan.Scheufele@Sun.COM 			break;
62119440SSusan.Scheufele@Sun.COM 		}
62129440SSusan.Scheufele@Sun.COM 	}
62139440SSusan.Scheufele@Sun.COM 
62149440SSusan.Scheufele@Sun.COM 	/* Parse target and lun */
62159440SSusan.Scheufele@Sun.COM 	for (p = tp = addr, lp = NULL; *p != '\0'; p++) {
62169440SSusan.Scheufele@Sun.COM 		if (*p == ',') {
62179440SSusan.Scheufele@Sun.COM 			lp = p + 1;
62189440SSusan.Scheufele@Sun.COM 			*p = '\0';
62199440SSusan.Scheufele@Sun.COM 			break;
62209440SSusan.Scheufele@Sun.COM 		}
62219440SSusan.Scheufele@Sun.COM 	}
62229440SSusan.Scheufele@Sun.COM 	if (tgt && tp) {
62239440SSusan.Scheufele@Sun.COM 		if (ddi_strtol(tp, NULL, 0x10, &num)) {
62249440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE); /* Can declare this as constant */
62259440SSusan.Scheufele@Sun.COM 		}
62269440SSusan.Scheufele@Sun.COM 			*tgt = (int)num;
62279440SSusan.Scheufele@Sun.COM 	}
62289440SSusan.Scheufele@Sun.COM 	if (lun && lp) {
62299440SSusan.Scheufele@Sun.COM 		if (ddi_strtol(lp, NULL, 0x10, &num)) {
62309440SSusan.Scheufele@Sun.COM 			return (DDI_FAILURE);
62319440SSusan.Scheufele@Sun.COM 		}
62329440SSusan.Scheufele@Sun.COM 			*lun = (int)num;
62339440SSusan.Scheufele@Sun.COM 	}
62349440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);  /* Success case */
62359440SSusan.Scheufele@Sun.COM }
62369440SSusan.Scheufele@Sun.COM 
62379440SSusan.Scheufele@Sun.COM static int
mrsas_config_ld(struct mrsas_instance * instance,uint16_t tgt,uint8_t lun,dev_info_t ** ldip)62389440SSusan.Scheufele@Sun.COM mrsas_config_ld(struct mrsas_instance *instance, uint16_t tgt,
62399440SSusan.Scheufele@Sun.COM     uint8_t lun, dev_info_t **ldip)
62409440SSusan.Scheufele@Sun.COM {
62419440SSusan.Scheufele@Sun.COM 	struct scsi_device *sd;
62429440SSusan.Scheufele@Sun.COM 	dev_info_t *child;
62439440SSusan.Scheufele@Sun.COM 	int rval;
62449440SSusan.Scheufele@Sun.COM 
62459440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_config_ld: t = %d l = %d",
62469440SSusan.Scheufele@Sun.COM 	    tgt, lun));
62479440SSusan.Scheufele@Sun.COM 
62489440SSusan.Scheufele@Sun.COM 	if ((child = mrsas_find_child(instance, tgt, lun)) != NULL) {
62499440SSusan.Scheufele@Sun.COM 		if (ldip) {
62509440SSusan.Scheufele@Sun.COM 			*ldip = child;
62519440SSusan.Scheufele@Sun.COM 		}
62529440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
62539440SSusan.Scheufele@Sun.COM 		    "mrsas_config_ld: Child = %p found t = %d l = %d",
62549440SSusan.Scheufele@Sun.COM 		    (void *)child, tgt, lun));
62559440SSusan.Scheufele@Sun.COM 		return (NDI_SUCCESS);
62569440SSusan.Scheufele@Sun.COM 	}
62579440SSusan.Scheufele@Sun.COM 
62589440SSusan.Scheufele@Sun.COM 	sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
62599440SSusan.Scheufele@Sun.COM 	sd->sd_address.a_hba_tran = instance->tran;
62609440SSusan.Scheufele@Sun.COM 	sd->sd_address.a_target = (uint16_t)tgt;
62619440SSusan.Scheufele@Sun.COM 	sd->sd_address.a_lun = (uint8_t)lun;
62629440SSusan.Scheufele@Sun.COM 
62639440SSusan.Scheufele@Sun.COM 	if (scsi_hba_probe(sd, NULL) == SCSIPROBE_EXISTS)
62649440SSusan.Scheufele@Sun.COM 		rval = mrsas_config_scsi_device(instance, sd, ldip);
62659440SSusan.Scheufele@Sun.COM 	else
62669440SSusan.Scheufele@Sun.COM 		rval = NDI_FAILURE;
62679440SSusan.Scheufele@Sun.COM 
62689440SSusan.Scheufele@Sun.COM 	/* sd_unprobe is blank now. Free buffer manually */
62699440SSusan.Scheufele@Sun.COM 	if (sd->sd_inq) {
62709440SSusan.Scheufele@Sun.COM 		kmem_free(sd->sd_inq, SUN_INQSIZE);
62719440SSusan.Scheufele@Sun.COM 		sd->sd_inq = (struct scsi_inquiry *)NULL;
62729440SSusan.Scheufele@Sun.COM 	}
62739440SSusan.Scheufele@Sun.COM 
62749440SSusan.Scheufele@Sun.COM 	kmem_free(sd, sizeof (struct scsi_device));
62759440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_config_ld: return rval = %d",
62769440SSusan.Scheufele@Sun.COM 	    rval));
62779440SSusan.Scheufele@Sun.COM 	return (rval);
62789440SSusan.Scheufele@Sun.COM }
62799440SSusan.Scheufele@Sun.COM 
62809440SSusan.Scheufele@Sun.COM static int
mrsas_config_scsi_device(struct mrsas_instance * instance,struct scsi_device * sd,dev_info_t ** dipp)62819440SSusan.Scheufele@Sun.COM mrsas_config_scsi_device(struct mrsas_instance *instance,
62829440SSusan.Scheufele@Sun.COM     struct scsi_device *sd, dev_info_t **dipp)
62839440SSusan.Scheufele@Sun.COM {
62849440SSusan.Scheufele@Sun.COM 	char *nodename = NULL;
62859440SSusan.Scheufele@Sun.COM 	char **compatible = NULL;
62869440SSusan.Scheufele@Sun.COM 	int ncompatible = 0;
62879440SSusan.Scheufele@Sun.COM 	char *childname;
62889440SSusan.Scheufele@Sun.COM 	dev_info_t *ldip = NULL;
62899440SSusan.Scheufele@Sun.COM 	int tgt = sd->sd_address.a_target;
62909440SSusan.Scheufele@Sun.COM 	int lun = sd->sd_address.a_lun;
62919440SSusan.Scheufele@Sun.COM 	int dtype = sd->sd_inq->inq_dtype & DTYPE_MASK;
62929440SSusan.Scheufele@Sun.COM 	int rval;
62939440SSusan.Scheufele@Sun.COM 
62949440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_WARN, "mr_sas: scsi_device t%dL%d", tgt, lun));
62959440SSusan.Scheufele@Sun.COM 	scsi_hba_nodename_compatible_get(sd->sd_inq, NULL, dtype,
62969440SSusan.Scheufele@Sun.COM 	    NULL, &nodename, &compatible, &ncompatible);
62979440SSusan.Scheufele@Sun.COM 
62989440SSusan.Scheufele@Sun.COM 	if (nodename == NULL) {
62999440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "mr_sas: Found no compatible driver "
63009440SSusan.Scheufele@Sun.COM 		    "for t%dL%d", tgt, lun));
63019440SSusan.Scheufele@Sun.COM 		rval = NDI_FAILURE;
63029440SSusan.Scheufele@Sun.COM 		goto finish;
63039440SSusan.Scheufele@Sun.COM 	}
63049440SSusan.Scheufele@Sun.COM 
63059440SSusan.Scheufele@Sun.COM 	childname = (dtype == DTYPE_DIRECT) ? "sd" : nodename;
63069440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_WARN,
63079440SSusan.Scheufele@Sun.COM 	    "mr_sas: Childname = %2s nodename = %s", childname, nodename));
63089440SSusan.Scheufele@Sun.COM 
63099440SSusan.Scheufele@Sun.COM 	/* Create a dev node */
63109440SSusan.Scheufele@Sun.COM 	rval = ndi_devi_alloc(instance->dip, childname, DEVI_SID_NODEID, &ldip);
63119440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_WARN,
63129440SSusan.Scheufele@Sun.COM 	    "mr_sas_config_scsi_device: ndi_devi_alloc rval = %x", rval));
63139440SSusan.Scheufele@Sun.COM 	if (rval == NDI_SUCCESS) {
63149440SSusan.Scheufele@Sun.COM 		if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "target", tgt) !=
63159440SSusan.Scheufele@Sun.COM 		    DDI_PROP_SUCCESS) {
63169440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
63179440SSusan.Scheufele@Sun.COM 			    "property for t%dl%d target", tgt, lun));
63189440SSusan.Scheufele@Sun.COM 			rval = NDI_FAILURE;
63199440SSusan.Scheufele@Sun.COM 			goto finish;
63209440SSusan.Scheufele@Sun.COM 		}
63219440SSusan.Scheufele@Sun.COM 		if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "lun", lun) !=
63229440SSusan.Scheufele@Sun.COM 		    DDI_PROP_SUCCESS) {
63239440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
63249440SSusan.Scheufele@Sun.COM 			    "property for t%dl%d lun", tgt, lun));
63259440SSusan.Scheufele@Sun.COM 			rval = NDI_FAILURE;
63269440SSusan.Scheufele@Sun.COM 			goto finish;
63279440SSusan.Scheufele@Sun.COM 		}
63289440SSusan.Scheufele@Sun.COM 
63299440SSusan.Scheufele@Sun.COM 		if (ndi_prop_update_string_array(DDI_DEV_T_NONE, ldip,
63309440SSusan.Scheufele@Sun.COM 		    "compatible", compatible, ncompatible) !=
63319440SSusan.Scheufele@Sun.COM 		    DDI_PROP_SUCCESS) {
63329440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
63339440SSusan.Scheufele@Sun.COM 			    "property for t%dl%d compatible", tgt, lun));
63349440SSusan.Scheufele@Sun.COM 			rval = NDI_FAILURE;
63359440SSusan.Scheufele@Sun.COM 			goto finish;
63369440SSusan.Scheufele@Sun.COM 		}
63379440SSusan.Scheufele@Sun.COM 
63389440SSusan.Scheufele@Sun.COM 		rval = ndi_devi_online(ldip, NDI_ONLINE_ATTACH);
63399440SSusan.Scheufele@Sun.COM 		if (rval != NDI_SUCCESS) {
63409440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to online "
63419440SSusan.Scheufele@Sun.COM 			    "t%dl%d", tgt, lun));
63429440SSusan.Scheufele@Sun.COM 			ndi_prop_remove_all(ldip);
63439440SSusan.Scheufele@Sun.COM 			(void) ndi_devi_free(ldip);
63449440SSusan.Scheufele@Sun.COM 		} else {
63459440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_WARN, "mr_sas: online Done :"
63469440SSusan.Scheufele@Sun.COM 			    "0 t%dl%d", tgt, lun));
63479440SSusan.Scheufele@Sun.COM 		}
63489440SSusan.Scheufele@Sun.COM 
63499440SSusan.Scheufele@Sun.COM 	}
63509440SSusan.Scheufele@Sun.COM finish:
63519440SSusan.Scheufele@Sun.COM 	if (dipp) {
63529440SSusan.Scheufele@Sun.COM 		*dipp = ldip;
63539440SSusan.Scheufele@Sun.COM 	}
63549440SSusan.Scheufele@Sun.COM 
63559440SSusan.Scheufele@Sun.COM 	con_log(CL_DLEVEL1, (CE_WARN,
63569440SSusan.Scheufele@Sun.COM 	    "mr_sas: config_scsi_device rval = %d t%dL%d",
63579440SSusan.Scheufele@Sun.COM 	    rval, tgt, lun));
63589440SSusan.Scheufele@Sun.COM 	scsi_hba_nodename_compatible_free(nodename, compatible);
63599440SSusan.Scheufele@Sun.COM 	return (rval);
63609440SSusan.Scheufele@Sun.COM }
63619440SSusan.Scheufele@Sun.COM 
63629440SSusan.Scheufele@Sun.COM /*ARGSUSED*/
63639440SSusan.Scheufele@Sun.COM static int
mrsas_service_evt(struct mrsas_instance * instance,int tgt,int lun,int event,uint64_t wwn)63649440SSusan.Scheufele@Sun.COM mrsas_service_evt(struct mrsas_instance *instance, int tgt, int lun, int event,
63659440SSusan.Scheufele@Sun.COM     uint64_t wwn)
63669440SSusan.Scheufele@Sun.COM {
63679440SSusan.Scheufele@Sun.COM 	struct mrsas_eventinfo *mrevt = NULL;
63689440SSusan.Scheufele@Sun.COM 
63699440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE,
63709440SSusan.Scheufele@Sun.COM 	    "mrsas_service_evt called for t%dl%d event = %d",
63719440SSusan.Scheufele@Sun.COM 	    tgt, lun, event));
63729440SSusan.Scheufele@Sun.COM 
63739440SSusan.Scheufele@Sun.COM 	if ((instance->taskq == NULL) || (mrevt =
63749440SSusan.Scheufele@Sun.COM 	    kmem_zalloc(sizeof (struct mrsas_eventinfo), KM_NOSLEEP)) == NULL) {
63759440SSusan.Scheufele@Sun.COM 		return (ENOMEM);
63769440SSusan.Scheufele@Sun.COM 	}
63779440SSusan.Scheufele@Sun.COM 
63789440SSusan.Scheufele@Sun.COM 	mrevt->instance = instance;
63799440SSusan.Scheufele@Sun.COM 	mrevt->tgt = tgt;
63809440SSusan.Scheufele@Sun.COM 	mrevt->lun = lun;
63819440SSusan.Scheufele@Sun.COM 	mrevt->event = event;
63829440SSusan.Scheufele@Sun.COM 
63839440SSusan.Scheufele@Sun.COM 	if ((ddi_taskq_dispatch(instance->taskq,
63849440SSusan.Scheufele@Sun.COM 	    (void (*)(void *))mrsas_issue_evt_taskq, mrevt, DDI_NOSLEEP)) !=
63859440SSusan.Scheufele@Sun.COM 	    DDI_SUCCESS) {
63869440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_NOTE,
63879440SSusan.Scheufele@Sun.COM 		    "mr_sas: Event task failed for t%dl%d event = %d",
63889440SSusan.Scheufele@Sun.COM 		    tgt, lun, event));
63899440SSusan.Scheufele@Sun.COM 		kmem_free(mrevt, sizeof (struct mrsas_eventinfo));
63909440SSusan.Scheufele@Sun.COM 		return (DDI_FAILURE);
63919440SSusan.Scheufele@Sun.COM 	}
639210820SSusan.Scheufele@Sun.COM 	DTRACE_PROBE3(service_evt, int, tgt, int, lun, int, event);
63939440SSusan.Scheufele@Sun.COM 	return (DDI_SUCCESS);
63949440SSusan.Scheufele@Sun.COM }
63959440SSusan.Scheufele@Sun.COM 
63969440SSusan.Scheufele@Sun.COM static void
mrsas_issue_evt_taskq(struct mrsas_eventinfo * mrevt)63979440SSusan.Scheufele@Sun.COM mrsas_issue_evt_taskq(struct mrsas_eventinfo *mrevt)
63989440SSusan.Scheufele@Sun.COM {
63999440SSusan.Scheufele@Sun.COM 	struct mrsas_instance *instance = mrevt->instance;
64009440SSusan.Scheufele@Sun.COM 	dev_info_t *dip, *pdip;
64019440SSusan.Scheufele@Sun.COM 	int circ1 = 0;
64029440SSusan.Scheufele@Sun.COM 	char *devname;
64039440SSusan.Scheufele@Sun.COM 
64049440SSusan.Scheufele@Sun.COM 	con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_evt_taskq: called for"
64059440SSusan.Scheufele@Sun.COM 	    " tgt %d lun %d event %d",
64069440SSusan.Scheufele@Sun.COM 	    mrevt->tgt, mrevt->lun, mrevt->event));
64079440SSusan.Scheufele@Sun.COM 
64089440SSusan.Scheufele@Sun.COM 	if (mrevt->tgt < MRDRV_MAX_LD && mrevt->lun == 0) {
64099440SSusan.Scheufele@Sun.COM 		dip = instance->mr_ld_list[mrevt->tgt].dip;
64109440SSusan.Scheufele@Sun.COM 	} else {
64119440SSusan.Scheufele@Sun.COM 		return;
64129440SSusan.Scheufele@Sun.COM 	}
64139440SSusan.Scheufele@Sun.COM 
64149440SSusan.Scheufele@Sun.COM 	ndi_devi_enter(instance->dip, &circ1);
64159440SSusan.Scheufele@Sun.COM 	switch (mrevt->event) {
64169440SSusan.Scheufele@Sun.COM 	case MRSAS_EVT_CONFIG_TGT:
64179440SSusan.Scheufele@Sun.COM 		if (dip == NULL) {
64189440SSusan.Scheufele@Sun.COM 
64199440SSusan.Scheufele@Sun.COM 			if (mrevt->lun == 0) {
64209440SSusan.Scheufele@Sun.COM 				(void) mrsas_config_ld(instance, mrevt->tgt,
64219440SSusan.Scheufele@Sun.COM 				    0, NULL);
64229440SSusan.Scheufele@Sun.COM 			}
64239440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
64249440SSusan.Scheufele@Sun.COM 			    "mr_sas: EVT_CONFIG_TGT called:"
64259440SSusan.Scheufele@Sun.COM 			    " for tgt %d lun %d event %d",
64269440SSusan.Scheufele@Sun.COM 			    mrevt->tgt, mrevt->lun, mrevt->event));
64279440SSusan.Scheufele@Sun.COM 
64289440SSusan.Scheufele@Sun.COM 		} else {
64299440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
64309440SSusan.Scheufele@Sun.COM 			    "mr_sas: EVT_CONFIG_TGT dip != NULL:"
64319440SSusan.Scheufele@Sun.COM 			    " for tgt %d lun %d event %d",
64329440SSusan.Scheufele@Sun.COM 			    mrevt->tgt, mrevt->lun, mrevt->event));
64339440SSusan.Scheufele@Sun.COM 		}
64349440SSusan.Scheufele@Sun.COM 		break;
64359440SSusan.Scheufele@Sun.COM 	case MRSAS_EVT_UNCONFIG_TGT:
64369440SSusan.Scheufele@Sun.COM 		if (dip) {
64379440SSusan.Scheufele@Sun.COM 			if (i_ddi_devi_attached(dip)) {
64389440SSusan.Scheufele@Sun.COM 
64399440SSusan.Scheufele@Sun.COM 				pdip = ddi_get_parent(dip);
64409440SSusan.Scheufele@Sun.COM 
64419440SSusan.Scheufele@Sun.COM 				devname = kmem_zalloc(MAXNAMELEN + 1, KM_SLEEP);
64429440SSusan.Scheufele@Sun.COM 				(void) ddi_deviname(dip, devname);
64439440SSusan.Scheufele@Sun.COM 
64449440SSusan.Scheufele@Sun.COM 				(void) devfs_clean(pdip, devname + 1,
64459440SSusan.Scheufele@Sun.COM 				    DV_CLEAN_FORCE);
64469440SSusan.Scheufele@Sun.COM 				kmem_free(devname, MAXNAMELEN + 1);
64479440SSusan.Scheufele@Sun.COM 			}
64489440SSusan.Scheufele@Sun.COM 			(void) ndi_devi_offline(dip, NDI_DEVI_REMOVE);
64499440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
64509440SSusan.Scheufele@Sun.COM 			    "mr_sas: EVT_UNCONFIG_TGT called:"
64519440SSusan.Scheufele@Sun.COM 			    " for tgt %d lun %d event %d",
64529440SSusan.Scheufele@Sun.COM 			    mrevt->tgt, mrevt->lun, mrevt->event));
64539440SSusan.Scheufele@Sun.COM 		} else {
64549440SSusan.Scheufele@Sun.COM 			con_log(CL_ANN1, (CE_NOTE,
64559440SSusan.Scheufele@Sun.COM 			    "mr_sas: EVT_UNCONFIG_TGT dip == NULL:"
64569440SSusan.Scheufele@Sun.COM 			    " for tgt %d lun %d event %d",
64579440SSusan.Scheufele@Sun.COM 			    mrevt->tgt, mrevt->lun, mrevt->event));
64589440SSusan.Scheufele@Sun.COM 		}
64599440SSusan.Scheufele@Sun.COM 		break;
64609440SSusan.Scheufele@Sun.COM 	}
64619440SSusan.Scheufele@Sun.COM 	kmem_free(mrevt, sizeof (struct mrsas_eventinfo));
64629440SSusan.Scheufele@Sun.COM 	ndi_devi_exit(instance->dip, circ1);
64639440SSusan.Scheufele@Sun.COM }
64649440SSusan.Scheufele@Sun.COM 
64659440SSusan.Scheufele@Sun.COM static int
mrsas_mode_sense_build(struct scsi_pkt * pkt)64669440SSusan.Scheufele@Sun.COM mrsas_mode_sense_build(struct scsi_pkt *pkt)
64679440SSusan.Scheufele@Sun.COM {
64689440SSusan.Scheufele@Sun.COM 	union scsi_cdb		*cdbp;
64699440SSusan.Scheufele@Sun.COM 	uint16_t 		page_code;
64709440SSusan.Scheufele@Sun.COM 	struct scsa_cmd		*acmd;
64719440SSusan.Scheufele@Sun.COM 	struct buf		*bp;
64729440SSusan.Scheufele@Sun.COM 	struct mode_header	*modehdrp;
64739440SSusan.Scheufele@Sun.COM 
64749440SSusan.Scheufele@Sun.COM 	cdbp = (void *)pkt->pkt_cdbp;
64759440SSusan.Scheufele@Sun.COM 	page_code = cdbp->cdb_un.sg.scsi[0];
64769440SSusan.Scheufele@Sun.COM 	acmd = PKT2CMD(pkt);
64779440SSusan.Scheufele@Sun.COM 	bp = acmd->cmd_buf;
64789440SSusan.Scheufele@Sun.COM 	if ((!bp) && bp->b_un.b_addr && bp->b_bcount && acmd->cmd_dmacount) {
64799440SSusan.Scheufele@Sun.COM 		con_log(CL_ANN1, (CE_WARN, "Failing MODESENSE Command"));
64809440SSusan.Scheufele@Sun.COM 		/* ADD pkt statistics as Command failed. */
64819440SSusan.Scheufele@Sun.COM 		return (NULL);
64829440SSusan.Scheufele@Sun.COM 	}
64839440SSusan.Scheufele@Sun.COM 
64849440SSusan.Scheufele@Sun.COM 	bp_mapin(bp);
64859440SSusan.Scheufele@Sun.COM 	bzero(bp->b_un.b_addr, bp->b_bcount);
64869440SSusan.Scheufele@Sun.COM 
64879440SSusan.Scheufele@Sun.COM 	switch (page_code) {
64889440SSusan.Scheufele@Sun.COM 		case 0x3: {
64899440SSusan.Scheufele@Sun.COM 			struct mode_format *page3p = NULL;
64909440SSusan.Scheufele@Sun.COM 			modehdrp = (struct mode_header *)(bp->b_un.b_addr);
64919440SSusan.Scheufele@Sun.COM 			modehdrp->bdesc_length = MODE_BLK_DESC_LENGTH;
64929440SSusan.Scheufele@Sun.COM 
64939440SSusan.Scheufele@Sun.COM 			page3p = (void *)((caddr_t)modehdrp +
64949440SSusan.Scheufele@Sun.COM 			    MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH);
64959440SSusan.Scheufele@Sun.COM 			page3p->mode_page.code = 0x3;
64969440SSusan.Scheufele@Sun.COM 			page3p->mode_page.length =
64979440SSusan.Scheufele@Sun.COM 			    (uchar_t)(sizeof (struct mode_format));
64989440SSusan.Scheufele@Sun.COM 			page3p->data_bytes_sect = 512;
64999440SSusan.Scheufele@Sun.COM 			page3p->sect_track = 63;
65009440SSusan.Scheufele@Sun.COM 			break;
65019440SSusan.Scheufele@Sun.COM 		}
65029440SSusan.Scheufele@Sun.COM 		case 0x4: {
65039440SSusan.Scheufele@Sun.COM 			struct mode_geometry *page4p = NULL;
65049440SSusan.Scheufele@Sun.COM 			modehdrp = (struct mode_header *)(bp->b_un.b_addr);
65059440SSusan.Scheufele@Sun.COM 			modehdrp->bdesc_length = MODE_BLK_DESC_LENGTH;
65069440SSusan.Scheufele@Sun.COM 
65079440SSusan.Scheufele@Sun.COM 			page4p = (void *)((caddr_t)modehdrp +
65089440SSusan.Scheufele@Sun.COM 			    MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH);
65099440SSusan.Scheufele@Sun.COM 			page4p->mode_page.code = 0x4;
65109440SSusan.Scheufele@Sun.COM 			page4p->mode_page.length =
65119440SSusan.Scheufele@Sun.COM 			    (uchar_t)(sizeof (struct mode_geometry));
65129440SSusan.Scheufele@Sun.COM 			page4p->heads = 255;
65139440SSusan.Scheufele@Sun.COM 			page4p->rpm = 10000;
65149440SSusan.Scheufele@Sun.COM 			break;
65159440SSusan.Scheufele@Sun.COM 		}
65169440SSusan.Scheufele@Sun.COM 		default:
65179440SSusan.Scheufele@Sun.COM 			break;
65189440SSusan.Scheufele@Sun.COM 	}
65199440SSusan.Scheufele@Sun.COM 	return (NULL);
65209440SSusan.Scheufele@Sun.COM }
6521