xref: /illumos-gate/usr/src/uts/common/io/aac/aac.c (revision c5866e1dd55ab5acc06a0e2fb2d896f7fddc9695)
1 /*
2  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright 2005-08 Adaptec, Inc.
8  * Copyright (c) 2005-08 Adaptec Inc., Achim Leubner
9  * Copyright (c) 2000 Michael Smith
10  * Copyright (c) 2001 Scott Long
11  * Copyright (c) 2000 BSDi
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <sys/modctl.h>
36 #include <sys/conf.h>
37 #include <sys/cmn_err.h>
38 #include <sys/ddi.h>
39 #include <sys/devops.h>
40 #include <sys/pci.h>
41 #include <sys/types.h>
42 #include <sys/ddidmareq.h>
43 #include <sys/scsi/scsi.h>
44 #include <sys/ksynch.h>
45 #include <sys/sunddi.h>
46 #include <sys/byteorder.h>
47 #include "aac_regs.h"
48 #include "aac.h"
49 
50 /*
51  * FMA header files
52  */
53 #include <sys/ddifm.h>
54 #include <sys/fm/protocol.h>
55 #include <sys/fm/util.h>
56 #include <sys/fm/io/ddi.h>
57 
58 /*
59  * For minor nodes created by the SCSA framework, minor numbers are
60  * formed by left-shifting instance by INST_MINOR_SHIFT and OR in a
61  * number less than 64.
62  *
63  * To support cfgadm, need to confirm the SCSA framework by creating
64  * devctl/scsi and driver specific minor nodes under SCSA format,
65  * and calling scsi_hba_xxx() functions aacordingly.
66  */
67 
68 #define	AAC_MINOR		32
69 #define	INST2AAC(x)		(((x) << INST_MINOR_SHIFT) | AAC_MINOR)
70 #define	AAC_SCSA_MINOR(x)	((x) & TRAN_MINOR_MASK)
71 #define	AAC_IS_SCSA_NODE(x)	((x) == DEVCTL_MINOR || (x) == SCSI_MINOR)
72 
73 #define	SD2TRAN(sd)		((sd)->sd_address.a_hba_tran)
74 #define	AAC_TRAN2SOFTS(tran) ((struct aac_softstate *)(tran)->tran_hba_private)
75 #define	AAC_DIP2TRAN(dip)	((scsi_hba_tran_t *)ddi_get_driver_private(dip))
76 #define	AAC_DIP2SOFTS(dip)	(AAC_TRAN2SOFTS(AAC_DIP2TRAN(dip)))
77 #define	SD2AAC(sd)		(AAC_TRAN2SOFTS(SD2TRAN(sd)))
78 #define	AAC_PD(t)		((t) - AAC_MAX_LD)
79 #define	AAC_DEV(softs, t)	(((t) < AAC_MAX_LD) ? \
80 				&(softs)->containers[(t)].dev : \
81 				((t) < AAC_MAX_DEV(softs)) ? \
82 				&(softs)->nondasds[AAC_PD(t)].dev : NULL)
83 #define	AAC_DEVCFG_BEGIN(softs, tgt) \
84 				aac_devcfg((softs), (tgt), 1)
85 #define	AAC_DEVCFG_END(softs, tgt) \
86 				aac_devcfg((softs), (tgt), 0)
87 #define	PKT2AC(pkt)		((struct aac_cmd *)(pkt)->pkt_ha_private)
88 #define	AAC_BUSYWAIT(cond, timeout /* in millisecond */) { \
89 		if (!(cond)) { \
90 			int count = (timeout) * 10; \
91 			while (count) { \
92 				drv_usecwait(100); \
93 				if (cond) \
94 					break; \
95 				count--; \
96 			} \
97 			(timeout) = (count + 9) / 10; \
98 		} \
99 	}
100 
101 #define	AAC_SENSE_DATA_DESCR_LEN \
102 	(sizeof (struct scsi_descr_sense_hdr) + \
103 	sizeof (struct scsi_information_sense_descr))
104 #define	AAC_ARQ64_LENGTH \
105 	(sizeof (struct scsi_arq_status) + \
106 	AAC_SENSE_DATA_DESCR_LEN - SENSE_LENGTH)
107 
108 /* NOTE: GETG4ADDRTL(cdbp) is int32_t */
109 #define	AAC_GETGXADDR(cmdlen, cdbp) \
110 	((cmdlen == 6) ? GETG0ADDR(cdbp) : \
111 	(cmdlen == 10) ? (uint32_t)GETG1ADDR(cdbp) : \
112 	((uint64_t)GETG4ADDR(cdbp) << 32) | (uint32_t)GETG4ADDRTL(cdbp))
113 
114 #define	AAC_CDB_INQUIRY_CMDDT	0x02
115 #define	AAC_CDB_INQUIRY_EVPD	0x01
116 #define	AAC_VPD_PAGE_CODE	1
117 #define	AAC_VPD_PAGE_LENGTH	3
118 #define	AAC_VPD_PAGE_DATA	4
119 #define	AAC_VPD_ID_CODESET	0
120 #define	AAC_VPD_ID_TYPE		1
121 #define	AAC_VPD_ID_LENGTH	3
122 #define	AAC_VPD_ID_DATA		4
123 
124 #define	AAC_SCSI_RPTLUNS_HEAD_SIZE			0x08
125 #define	AAC_SCSI_RPTLUNS_ADDR_SIZE			0x08
126 #define	AAC_SCSI_RPTLUNS_ADDR_MASK			0xC0
127 /* 00b - peripheral device addressing method */
128 #define	AAC_SCSI_RPTLUNS_ADDR_PERIPHERAL		0x00
129 /* 01b - flat space addressing method */
130 #define	AAC_SCSI_RPTLUNS_ADDR_FLAT_SPACE		0x40
131 /* 10b - logical unit addressing method */
132 #define	AAC_SCSI_RPTLUNS_ADDR_LOGICAL_UNIT		0x80
133 
134 /* Return the size of FIB with data part type data_type */
135 #define	AAC_FIB_SIZEOF(data_type) \
136 	(sizeof (struct aac_fib_header) + sizeof (data_type))
137 /* Return the container size defined in mir */
138 #define	AAC_MIR_SIZE(softs, acc, mir) \
139 	(((softs)->flags & AAC_FLAGS_LBA_64BIT) ? \
140 	(uint64_t)ddi_get32((acc), &(mir)->MntObj.Capacity) + \
141 	((uint64_t)ddi_get32((acc), &(mir)->MntObj.CapacityHigh) << 32) : \
142 	(uint64_t)ddi_get32((acc), &(mir)->MntObj.Capacity))
143 
144 /* The last entry of aac_cards[] is for unknown cards */
145 #define	AAC_UNKNOWN_CARD \
146 	(sizeof (aac_cards) / sizeof (struct aac_card_type) - 1)
147 #define	CARD_IS_UNKNOWN(i)	(i == AAC_UNKNOWN_CARD)
148 #define	BUF_IS_READ(bp)		((bp)->b_flags & B_READ)
149 #define	AAC_IS_Q_EMPTY(q)	((q)->q_head == NULL)
150 #define	AAC_CMDQ(acp)		(!((acp)->flags & AAC_CMD_SYNC))
151 
152 #define	PCI_MEM_GET32(softs, off) \
153 	ddi_get32((softs)->pci_mem_handle, \
154 	    (void *)((softs)->pci_mem_base_vaddr + (off)))
155 #define	PCI_MEM_PUT32(softs, off, val) \
156 	ddi_put32((softs)->pci_mem_handle, \
157 	    (void *)((softs)->pci_mem_base_vaddr + (off)), \
158 	    (uint32_t)(val))
159 #define	PCI_MEM_GET16(softs, off) \
160 	ddi_get16((softs)->pci_mem_handle, \
161 	(void *)((softs)->pci_mem_base_vaddr + (off)))
162 #define	PCI_MEM_PUT16(softs, off, val) \
163 	ddi_put16((softs)->pci_mem_handle, \
164 	(void *)((softs)->pci_mem_base_vaddr + (off)), (uint16_t)(val))
165 /* Write host data at valp to device mem[off] repeatedly count times */
166 #define	PCI_MEM_REP_PUT8(softs, off, valp, count) \
167 	ddi_rep_put8((softs)->pci_mem_handle, (uint8_t *)(valp), \
168 	    (uint8_t *)((softs)->pci_mem_base_vaddr + (off)), \
169 	    count, DDI_DEV_AUTOINCR)
170 /* Read device data at mem[off] to host addr valp repeatedly count times */
171 #define	PCI_MEM_REP_GET8(softs, off, valp, count) \
172 	ddi_rep_get8((softs)->pci_mem_handle, (uint8_t *)(valp), \
173 	    (uint8_t *)((softs)->pci_mem_base_vaddr + (off)), \
174 	    count, DDI_DEV_AUTOINCR)
175 #define	AAC_GET_FIELD8(acc, d, s, field) \
176 	(d)->field = ddi_get8(acc, (uint8_t *)&(s)->field)
177 #define	AAC_GET_FIELD32(acc, d, s, field) \
178 	(d)->field = ddi_get32(acc, (uint32_t *)&(s)->field)
179 #define	AAC_GET_FIELD64(acc, d, s, field) \
180 	(d)->field = ddi_get64(acc, (uint64_t *)&(s)->field)
181 #define	AAC_REP_GET_FIELD8(acc, d, s, field, r) \
182 	ddi_rep_get8((acc), (uint8_t *)&(d)->field, \
183 	    (uint8_t *)&(s)->field, (r), DDI_DEV_AUTOINCR)
184 #define	AAC_REP_GET_FIELD32(acc, d, s, field, r) \
185 	ddi_rep_get32((acc), (uint32_t *)&(d)->field, \
186 	    (uint32_t *)&(s)->field, (r), DDI_DEV_AUTOINCR)
187 
188 #define	AAC_ENABLE_INTR(softs) { \
189 		if (softs->flags & AAC_FLAGS_NEW_COMM) \
190 			PCI_MEM_PUT32(softs, AAC_OIMR, ~AAC_DB_INTR_NEW); \
191 		else \
192 			PCI_MEM_PUT32(softs, AAC_OIMR, ~AAC_DB_INTR_BITS); \
193 		softs->state |= AAC_STATE_INTR; \
194 	}
195 
196 #define	AAC_DISABLE_INTR(softs)	{ \
197 		PCI_MEM_PUT32(softs, AAC_OIMR, ~0); \
198 		softs->state &= ~AAC_STATE_INTR; \
199 	}
200 #define	AAC_STATUS_CLR(softs, mask)	PCI_MEM_PUT32(softs, AAC_ODBR, mask)
201 #define	AAC_STATUS_GET(softs)		PCI_MEM_GET32(softs, AAC_ODBR)
202 #define	AAC_NOTIFY(softs, val)		PCI_MEM_PUT32(softs, AAC_IDBR, val)
203 #define	AAC_OUTB_GET(softs)		PCI_MEM_GET32(softs, AAC_OQUE)
204 #define	AAC_OUTB_SET(softs, val)	PCI_MEM_PUT32(softs, AAC_OQUE, val)
205 #define	AAC_FWSTATUS_GET(softs)	\
206 	((softs)->aac_if.aif_get_fwstatus(softs))
207 #define	AAC_MAILBOX_GET(softs, mb) \
208 	((softs)->aac_if.aif_get_mailbox((softs), (mb)))
209 #define	AAC_MAILBOX_SET(softs, cmd, arg0, arg1, arg2, arg3) \
210 	((softs)->aac_if.aif_set_mailbox((softs), (cmd), \
211 	    (arg0), (arg1), (arg2), (arg3)))
212 
213 #define	AAC_MGT_SLOT_NUM	2
214 #define	AAC_THROTTLE_DRAIN	-1
215 
216 #define	AAC_QUIESCE_TICK	1	/* 1 second */
217 #define	AAC_QUIESCE_TIMEOUT	180	/* 180 seconds */
218 #define	AAC_DEFAULT_TICK	10	/* 10 seconds */
219 #define	AAC_SYNC_TICK		(30*60)	/* 30 minutes */
220 
221 /* Poll time for aac_do_poll_io() */
222 #define	AAC_POLL_TIME		60	/* 60 seconds */
223 
224 /* IOP reset */
225 #define	AAC_IOP_RESET_SUCCEED		0	/* IOP reset succeed */
226 #define	AAC_IOP_RESET_FAILED		-1	/* IOP reset failed */
227 #define	AAC_IOP_RESET_ABNORMAL		-2	/* Reset operation abnormal */
228 
229 /*
230  * Hardware access functions
231  */
232 static int aac_rx_get_fwstatus(struct aac_softstate *);
233 static int aac_rx_get_mailbox(struct aac_softstate *, int);
234 static void aac_rx_set_mailbox(struct aac_softstate *, uint32_t, uint32_t,
235     uint32_t, uint32_t, uint32_t);
236 static int aac_rkt_get_fwstatus(struct aac_softstate *);
237 static int aac_rkt_get_mailbox(struct aac_softstate *, int);
238 static void aac_rkt_set_mailbox(struct aac_softstate *, uint32_t, uint32_t,
239     uint32_t, uint32_t, uint32_t);
240 
241 /*
242  * SCSA function prototypes
243  */
244 static int aac_attach(dev_info_t *, ddi_attach_cmd_t);
245 static int aac_detach(dev_info_t *, ddi_detach_cmd_t);
246 static int aac_reset(dev_info_t *, ddi_reset_cmd_t);
247 static int aac_quiesce(dev_info_t *);
248 static int aac_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
249 
250 /*
251  * Interrupt handler functions
252  */
253 static int aac_query_intrs(struct aac_softstate *, int);
254 static int aac_add_intrs(struct aac_softstate *);
255 static void aac_remove_intrs(struct aac_softstate *);
256 static int aac_enable_intrs(struct aac_softstate *);
257 static int aac_disable_intrs(struct aac_softstate *);
258 static uint_t aac_intr_old(caddr_t);
259 static uint_t aac_intr_new(caddr_t);
260 static uint_t aac_softintr(caddr_t);
261 
262 /*
263  * Internal functions in attach
264  */
265 static int aac_check_card_type(struct aac_softstate *);
266 static int aac_check_firmware(struct aac_softstate *);
267 static int aac_common_attach(struct aac_softstate *);
268 static void aac_common_detach(struct aac_softstate *);
269 static int aac_probe_containers(struct aac_softstate *);
270 static int aac_alloc_comm_space(struct aac_softstate *);
271 static int aac_setup_comm_space(struct aac_softstate *);
272 static void aac_free_comm_space(struct aac_softstate *);
273 static int aac_hba_setup(struct aac_softstate *);
274 
275 /*
276  * Sync FIB operation functions
277  */
278 int aac_sync_mbcommand(struct aac_softstate *, uint32_t, uint32_t,
279     uint32_t, uint32_t, uint32_t, uint32_t *);
280 static int aac_sync_fib(struct aac_softstate *, uint16_t, uint16_t);
281 
282 /*
283  * Command queue operation functions
284  */
285 static void aac_cmd_initq(struct aac_cmd_queue *);
286 static void aac_cmd_enqueue(struct aac_cmd_queue *, struct aac_cmd *);
287 static struct aac_cmd *aac_cmd_dequeue(struct aac_cmd_queue *);
288 static void aac_cmd_delete(struct aac_cmd_queue *, struct aac_cmd *);
289 
290 /*
291  * FIB queue operation functions
292  */
293 static int aac_fib_enqueue(struct aac_softstate *, int, uint32_t, uint32_t);
294 static int aac_fib_dequeue(struct aac_softstate *, int, int *);
295 
296 /*
297  * Slot operation functions
298  */
299 static int aac_create_slots(struct aac_softstate *);
300 static void aac_destroy_slots(struct aac_softstate *);
301 static void aac_alloc_fibs(struct aac_softstate *);
302 static void aac_destroy_fibs(struct aac_softstate *);
303 static struct aac_slot *aac_get_slot(struct aac_softstate *);
304 static void aac_release_slot(struct aac_softstate *, struct aac_slot *);
305 static int aac_alloc_fib(struct aac_softstate *, struct aac_slot *);
306 static void aac_free_fib(struct aac_slot *);
307 
308 /*
309  * Internal functions
310  */
311 static void aac_cmd_fib_header(struct aac_softstate *, struct aac_cmd *,
312     uint16_t);
313 static void aac_cmd_fib_rawio(struct aac_softstate *, struct aac_cmd *);
314 static void aac_cmd_fib_brw64(struct aac_softstate *, struct aac_cmd *);
315 static void aac_cmd_fib_brw(struct aac_softstate *, struct aac_cmd *);
316 static void aac_cmd_fib_sync(struct aac_softstate *, struct aac_cmd *);
317 static void aac_cmd_fib_scsi32(struct aac_softstate *, struct aac_cmd *);
318 static void aac_cmd_fib_scsi64(struct aac_softstate *, struct aac_cmd *);
319 static void aac_cmd_fib_startstop(struct aac_softstate *, struct aac_cmd *);
320 static void aac_start_waiting_io(struct aac_softstate *);
321 static void aac_drain_comp_q(struct aac_softstate *);
322 int aac_do_io(struct aac_softstate *, struct aac_cmd *);
323 static int aac_sync_fib_slot_bind(struct aac_softstate *, struct aac_cmd *);
324 static void aac_sync_fib_slot_release(struct aac_softstate *, struct aac_cmd *);
325 static void aac_start_io(struct aac_softstate *, struct aac_cmd *);
326 static int aac_do_poll_io(struct aac_softstate *, struct aac_cmd *);
327 static int aac_do_sync_io(struct aac_softstate *, struct aac_cmd *);
328 static int aac_send_command(struct aac_softstate *, struct aac_slot *);
329 static void aac_cmd_timeout(struct aac_softstate *, struct aac_cmd *);
330 static int aac_dma_sync_ac(struct aac_cmd *);
331 static int aac_shutdown(struct aac_softstate *);
332 static int aac_reset_adapter(struct aac_softstate *);
333 static int aac_do_quiesce(struct aac_softstate *softs);
334 static int aac_do_unquiesce(struct aac_softstate *softs);
335 static void aac_unhold_bus(struct aac_softstate *, int);
336 static void aac_set_throttle(struct aac_softstate *, struct aac_device *,
337     int, int);
338 
339 /*
340  * Adapter Initiated FIB handling function
341  */
342 static void aac_save_aif(struct aac_softstate *, ddi_acc_handle_t,
343     struct aac_fib *, int);
344 static int aac_handle_aif(struct aac_softstate *, struct aac_aif_command *);
345 
346 /*
347  * Event handling related functions
348  */
349 static void aac_timer(void *);
350 static void aac_event_thread(struct aac_softstate *);
351 static void aac_event_disp(struct aac_softstate *, int);
352 
353 /*
354  * IOCTL interface related functions
355  */
356 static int aac_open(dev_t *, int, int, cred_t *);
357 static int aac_close(dev_t, int, int, cred_t *);
358 static int aac_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
359 extern int aac_do_ioctl(struct aac_softstate *, dev_t, int, intptr_t, int);
360 
361 /*
362  * FMA Prototypes
363  */
364 static void aac_fm_init(struct aac_softstate *);
365 static void aac_fm_fini(struct aac_softstate *);
366 static int aac_fm_error_cb(dev_info_t *, ddi_fm_error_t *, const void *);
367 int aac_check_acc_handle(ddi_acc_handle_t);
368 int aac_check_dma_handle(ddi_dma_handle_t);
369 void aac_fm_ereport(struct aac_softstate *, char *);
370 
371 /*
372  * Auto enumeration functions
373  */
374 static dev_info_t *aac_find_child(struct aac_softstate *, uint16_t, uint8_t);
375 static int aac_tran_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t,
376     void *, dev_info_t **);
377 static int aac_handle_dr(struct aac_softstate *, int, int, int);
378 
379 extern pri_t minclsyspri;
380 
381 #ifdef DEBUG
382 /*
383  * UART	debug output support
384  */
385 
386 #define	AAC_PRINT_BUFFER_SIZE		512
387 #define	AAC_PRINT_TIMEOUT		250	/* 1/4 sec. = 250 msec. */
388 
389 #define	AAC_FW_DBG_STRLEN_OFFSET	0x00
390 #define	AAC_FW_DBG_FLAGS_OFFSET		0x04
391 #define	AAC_FW_DBG_BLED_OFFSET		0x08
392 
393 static int aac_get_fw_debug_buffer(struct aac_softstate *);
394 static void aac_print_scmd(struct aac_softstate *, struct aac_cmd *);
395 static void aac_print_aif(struct aac_softstate *, struct aac_aif_command *);
396 
397 static char aac_prt_buf[AAC_PRINT_BUFFER_SIZE];
398 static char aac_fmt[] = " %s";
399 static char aac_fmt_header[] = " %s.%d: %s";
400 static kmutex_t aac_prt_mutex;
401 
402 /*
403  * Debug flags to be put into the softstate flags field
404  * when initialized
405  */
406 uint32_t aac_debug_flags =
407 /*    AACDB_FLAGS_KERNEL_PRINT | */
408 /*    AACDB_FLAGS_FW_PRINT |	*/
409 /*    AACDB_FLAGS_MISC |	*/
410 /*    AACDB_FLAGS_FUNC1 |	*/
411 /*    AACDB_FLAGS_FUNC2 |	*/
412 /*    AACDB_FLAGS_SCMD |	*/
413 /*    AACDB_FLAGS_AIF |		*/
414 /*    AACDB_FLAGS_FIB |		*/
415 /*    AACDB_FLAGS_IOCTL |	*/
416 0;
417 uint32_t aac_debug_fib_flags =
418 /*    AACDB_FLAGS_FIB_RW |	*/
419 /*    AACDB_FLAGS_FIB_IOCTL |	*/
420 /*    AACDB_FLAGS_FIB_SRB |	*/
421 /*    AACDB_FLAGS_FIB_SYNC |	*/
422 /*    AACDB_FLAGS_FIB_HEADER |	*/
423 /*    AACDB_FLAGS_FIB_TIMEOUT |	*/
424 0;
425 
426 #endif /* DEBUG */
427 
428 static struct cb_ops aac_cb_ops = {
429 	aac_open,	/* open */
430 	aac_close,	/* close */
431 	nodev,		/* strategy */
432 	nodev,		/* print */
433 	nodev,		/* dump */
434 	nodev,		/* read */
435 	nodev,		/* write */
436 	aac_ioctl,	/* ioctl */
437 	nodev,		/* devmap */
438 	nodev,		/* mmap */
439 	nodev,		/* segmap */
440 	nochpoll,	/* poll */
441 	ddi_prop_op,	/* cb_prop_op */
442 	NULL,		/* streamtab */
443 	D_64BIT | D_NEW | D_MP | D_HOTPLUG,	/* cb_flag */
444 	CB_REV,		/* cb_rev */
445 	nodev,		/* async I/O read entry point */
446 	nodev		/* async I/O write entry point */
447 };
448 
449 static struct dev_ops aac_dev_ops = {
450 	DEVO_REV,
451 	0,
452 	aac_getinfo,
453 	nulldev,
454 	nulldev,
455 	aac_attach,
456 	aac_detach,
457 	aac_reset,
458 	&aac_cb_ops,
459 	NULL,
460 	NULL,
461 	aac_quiesce,
462 };
463 
464 static struct modldrv aac_modldrv = {
465 	&mod_driverops,
466 	"AAC Driver " AAC_DRIVER_VERSION,
467 	&aac_dev_ops,
468 };
469 
470 static struct modlinkage aac_modlinkage = {
471 	MODREV_1,
472 	&aac_modldrv,
473 	NULL
474 };
475 
476 static struct aac_softstate  *aac_softstatep;
477 
478 /*
479  * Supported card list
480  * ordered in vendor id, subvendor id, subdevice id, and device id
481  */
482 static struct aac_card_type aac_cards[] = {
483 	{0x1028, 0x1, 0x1028, 0x1, AAC_HWIF_I960RX,
484 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
485 	    "Dell", "PERC 3/Di"},
486 	{0x1028, 0x2, 0x1028, 0x2, AAC_HWIF_I960RX,
487 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
488 	    "Dell", "PERC 3/Di"},
489 	{0x1028, 0x3, 0x1028, 0x3, AAC_HWIF_I960RX,
490 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
491 	    "Dell", "PERC 3/Si"},
492 	{0x1028, 0x8, 0x1028, 0xcf, AAC_HWIF_I960RX,
493 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
494 	    "Dell", "PERC 3/Di"},
495 	{0x1028, 0x4, 0x1028, 0xd0, AAC_HWIF_I960RX,
496 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
497 	    "Dell", "PERC 3/Si"},
498 	{0x1028, 0x2, 0x1028, 0xd1, AAC_HWIF_I960RX,
499 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
500 	    "Dell", "PERC 3/Di"},
501 	{0x1028, 0x2, 0x1028, 0xd9, AAC_HWIF_I960RX,
502 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
503 	    "Dell", "PERC 3/Di"},
504 	{0x1028, 0xa, 0x1028, 0x106, AAC_HWIF_I960RX,
505 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
506 	    "Dell", "PERC 3/Di"},
507 	{0x1028, 0xa, 0x1028, 0x11b, AAC_HWIF_I960RX,
508 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
509 	    "Dell", "PERC 3/Di"},
510 	{0x1028, 0xa, 0x1028, 0x121, AAC_HWIF_I960RX,
511 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG, AAC_TYPE_SCSI,
512 	    "Dell", "PERC 3/Di"},
513 	{0x9005, 0x285, 0x1028, 0x287, AAC_HWIF_I960RX,
514 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG | AAC_FLAGS_256FIBS, AAC_TYPE_SCSI,
515 	    "Dell", "PERC 320/DC"},
516 	{0x9005, 0x285, 0x1028, 0x291, AAC_HWIF_I960RX,
517 	    AAC_FLAGS_17SG, AAC_TYPE_SATA, "Dell", "CERC SR2"},
518 
519 	{0x9005, 0x285, 0x1014, 0x2f2, AAC_HWIF_I960RX,
520 	    0, AAC_TYPE_SCSI, "IBM", "ServeRAID 8i"},
521 	{0x9005, 0x285, 0x1014, 0x34d, AAC_HWIF_I960RX,
522 	    0, AAC_TYPE_SAS, "IBM", "ServeRAID 8s"},
523 	{0x9005, 0x286, 0x1014, 0x9580, AAC_HWIF_RKT,
524 	    0, AAC_TYPE_SAS, "IBM", "ServeRAID 8k"},
525 
526 	{0x9005, 0x285, 0x103c, 0x3227, AAC_HWIF_I960RX,
527 	    AAC_FLAGS_17SG, AAC_TYPE_SATA, "Adaptec", "2610SA"},
528 	{0x9005, 0x285, 0xe11, 0x295, AAC_HWIF_I960RX,
529 	    AAC_FLAGS_17SG, AAC_TYPE_SATA, "Adaptec", "2610SA"},
530 
531 	{0x9005, 0x285, 0x9005, 0x285, AAC_HWIF_I960RX,
532 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG | AAC_FLAGS_256FIBS, AAC_TYPE_SCSI,
533 	    "Adaptec", "2200S"},
534 	{0x9005, 0x285, 0x9005, 0x286, AAC_HWIF_I960RX,
535 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG | AAC_FLAGS_256FIBS, AAC_TYPE_SCSI,
536 	    "Adaptec", "2120S"},
537 	{0x9005, 0x285, 0x9005, 0x287, AAC_HWIF_I960RX,
538 	    AAC_FLAGS_NO4GB | AAC_FLAGS_34SG | AAC_FLAGS_256FIBS, AAC_TYPE_SCSI,
539 	    "Adaptec", "2200S"},
540 	{0x9005, 0x285, 0x9005, 0x288, AAC_HWIF_I960RX,
541 	    0, AAC_TYPE_SCSI, "Adaptec", "3230S"},
542 	{0x9005, 0x285, 0x9005, 0x289, AAC_HWIF_I960RX,
543 	    0, AAC_TYPE_SCSI, "Adaptec", "3240S"},
544 	{0x9005, 0x285, 0x9005, 0x28a, AAC_HWIF_I960RX,
545 	    0, AAC_TYPE_SCSI, "Adaptec", "2020ZCR"},
546 	{0x9005, 0x285, 0x9005, 0x28b, AAC_HWIF_I960RX,
547 	    0, AAC_TYPE_SCSI, "Adaptec", "2025ZCR"},
548 	{0x9005, 0x286, 0x9005, 0x28c, AAC_HWIF_RKT,
549 	    0, AAC_TYPE_SCSI, "Adaptec", "2230S"},
550 	{0x9005, 0x286, 0x9005, 0x28d, AAC_HWIF_RKT,
551 	    0, AAC_TYPE_SCSI, "Adaptec", "2130S"},
552 	{0x9005, 0x285, 0x9005, 0x28e, AAC_HWIF_I960RX,
553 	    0, AAC_TYPE_SATA, "Adaptec", "2020SA"},
554 	{0x9005, 0x285, 0x9005, 0x28f, AAC_HWIF_I960RX,
555 	    0, AAC_TYPE_SATA, "Adaptec", "2025SA"},
556 	{0x9005, 0x285, 0x9005, 0x290, AAC_HWIF_I960RX,
557 	    AAC_FLAGS_17SG, AAC_TYPE_SATA, "Adaptec", "2410SA"},
558 	{0x9005, 0x285, 0x9005, 0x292, AAC_HWIF_I960RX,
559 	    AAC_FLAGS_17SG, AAC_TYPE_SATA, "Adaptec", "2810SA"},
560 	{0x9005, 0x285, 0x9005, 0x293, AAC_HWIF_I960RX,
561 	    AAC_FLAGS_17SG, AAC_TYPE_SATA, "Adaptec", "21610SA"},
562 	{0x9005, 0x285, 0x9005, 0x294, AAC_HWIF_I960RX,
563 	    0, AAC_TYPE_SATA, "Adaptec", "2026ZCR"},
564 	{0x9005, 0x285, 0x9005, 0x296, AAC_HWIF_I960RX,
565 	    0, AAC_TYPE_SCSI, "Adaptec", "2240S"},
566 	{0x9005, 0x285, 0x9005, 0x297, AAC_HWIF_I960RX,
567 	    0, AAC_TYPE_SAS, "Adaptec", "4005SAS"},
568 	{0x9005, 0x285, 0x9005, 0x298, AAC_HWIF_I960RX,
569 	    0, AAC_TYPE_SAS, "Adaptec", "RAID 4000"},
570 	{0x9005, 0x285, 0x9005, 0x299, AAC_HWIF_I960RX,
571 	    0, AAC_TYPE_SAS, "Adaptec", "4800SAS"},
572 	{0x9005, 0x285, 0x9005, 0x29a, AAC_HWIF_I960RX,
573 	    0, AAC_TYPE_SAS, "Adaptec", "4805SAS"},
574 	{0x9005, 0x286, 0x9005, 0x29b, AAC_HWIF_RKT,
575 	    0, AAC_TYPE_SATA, "Adaptec", "2820SA"},
576 	{0x9005, 0x286, 0x9005, 0x29c, AAC_HWIF_RKT,
577 	    0, AAC_TYPE_SATA, "Adaptec", "2620SA"},
578 	{0x9005, 0x286, 0x9005, 0x29d, AAC_HWIF_RKT,
579 	    0, AAC_TYPE_SATA, "Adaptec", "2420SA"},
580 	{0x9005, 0x286, 0x9005, 0x29e, AAC_HWIF_RKT,
581 	    0, AAC_TYPE_SATA, "ICP", "9024RO"},
582 	{0x9005, 0x286, 0x9005, 0x29f, AAC_HWIF_RKT,
583 	    0, AAC_TYPE_SATA, "ICP", "9014RO"},
584 	{0x9005, 0x286, 0x9005, 0x2a0, AAC_HWIF_RKT,
585 	    0, AAC_TYPE_SATA, "ICP", "9047MA"},
586 	{0x9005, 0x286, 0x9005, 0x2a1, AAC_HWIF_RKT,
587 	    0, AAC_TYPE_SATA, "ICP", "9087MA"},
588 	{0x9005, 0x285, 0x9005, 0x2a4, AAC_HWIF_I960RX,
589 	    0, AAC_TYPE_SAS, "ICP", "9085LI"},
590 	{0x9005, 0x285, 0x9005, 0x2a5, AAC_HWIF_I960RX,
591 	    0, AAC_TYPE_SAS, "ICP", "5085BR"},
592 	{0x9005, 0x286, 0x9005, 0x2a6, AAC_HWIF_RKT,
593 	    0, AAC_TYPE_SATA, "ICP", "9067MA"},
594 	{0x9005, 0x285, 0x9005, 0x2b5, AAC_HWIF_I960RX,
595 	    0, AAC_TYPE_SAS, "Adaptec", "RAID 5445"},
596 	{0x9005, 0x285, 0x9005, 0x2b6, AAC_HWIF_I960RX,
597 	    0, AAC_TYPE_SAS, "Adaptec", "RAID 5805"},
598 	{0x9005, 0x285, 0x9005, 0x2b7, AAC_HWIF_I960RX,
599 	    0, AAC_TYPE_SAS, "Adaptec", "RAID 5085"},
600 	{0x9005, 0x285, 0x9005, 0x2b8, AAC_HWIF_I960RX,
601 	    0, AAC_TYPE_SAS, "ICP", "RAID ICP5445SL"},
602 	{0x9005, 0x285, 0x9005, 0x2b9, AAC_HWIF_I960RX,
603 	    0, AAC_TYPE_SAS, "ICP", "RAID ICP5085SL"},
604 	{0x9005, 0x285, 0x9005, 0x2ba, AAC_HWIF_I960RX,
605 	    0, AAC_TYPE_SAS, "ICP", "RAID ICP5805SL"},
606 
607 	{0, 0, 0, 0, AAC_HWIF_UNKNOWN,
608 	    0, AAC_TYPE_UNKNOWN, "Unknown", "AAC card"},
609 };
610 
611 /*
612  * Hardware access functions for i960 based cards
613  */
614 static struct aac_interface aac_rx_interface = {
615 	aac_rx_get_fwstatus,
616 	aac_rx_get_mailbox,
617 	aac_rx_set_mailbox
618 };
619 
620 /*
621  * Hardware access functions for Rocket based cards
622  */
623 static struct aac_interface aac_rkt_interface = {
624 	aac_rkt_get_fwstatus,
625 	aac_rkt_get_mailbox,
626 	aac_rkt_set_mailbox
627 };
628 
629 ddi_device_acc_attr_t aac_acc_attr = {
630 	DDI_DEVICE_ATTR_V1,
631 	DDI_STRUCTURE_LE_ACC,
632 	DDI_STRICTORDER_ACC,
633 	DDI_DEFAULT_ACC
634 };
635 
636 static struct {
637 	int	size;
638 	int	notify;
639 } aac_qinfo[] = {
640 	{AAC_HOST_NORM_CMD_ENTRIES, AAC_DB_COMMAND_NOT_FULL},
641 	{AAC_HOST_HIGH_CMD_ENTRIES, 0},
642 	{AAC_ADAP_NORM_CMD_ENTRIES, AAC_DB_COMMAND_READY},
643 	{AAC_ADAP_HIGH_CMD_ENTRIES, 0},
644 	{AAC_HOST_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_NOT_FULL},
645 	{AAC_HOST_HIGH_RESP_ENTRIES, 0},
646 	{AAC_ADAP_NORM_RESP_ENTRIES, AAC_DB_RESPONSE_READY},
647 	{AAC_ADAP_HIGH_RESP_ENTRIES, 0}
648 };
649 
650 /*
651  * Default aac dma attributes
652  */
653 static ddi_dma_attr_t aac_dma_attr = {
654 	DMA_ATTR_V0,
655 	0,		/* lowest usable address */
656 	0xffffffffull,	/* high DMA address range */
657 	0xffffffffull,	/* DMA counter register */
658 	AAC_DMA_ALIGN,	/* DMA address alignment */
659 	1,		/* DMA burstsizes */
660 	1,		/* min effective DMA size */
661 	0xffffffffull,	/* max DMA xfer size */
662 	0xffffffffull,	/* segment boundary */
663 	1,		/* s/g list length */
664 	AAC_BLK_SIZE,	/* granularity of device */
665 	0		/* DMA transfer flags */
666 };
667 
668 static int aac_tick = AAC_DEFAULT_TICK;	/* tick for the internal timer */
669 static uint32_t aac_timebase = 0;	/* internal timer in seconds */
670 
671 /*
672  * Warlock directives
673  *
674  * Different variables with the same types have to be protected by the
675  * same mutex; otherwise, warlock will complain with "variables don't
676  * seem to be protected consistently". For example,
677  * aac_softstate::{q_wait, q_comp} are type of aac_cmd_queue, and protected
678  * by aac_softstate::{io_lock, q_comp_mutex} respectively. We have to
679  * declare them as protected explictly at aac_cmd_dequeue().
680  */
681 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_pkt scsi_cdb scsi_status \
682     scsi_arq_status scsi_descr_sense_hdr scsi_information_sense_descr \
683     mode_format mode_geometry mode_header aac_cmd))
684 _NOTE(SCHEME_PROTECTS_DATA("unique per aac_cmd", aac_fib ddi_dma_cookie_t \
685     aac_sge))
686 _NOTE(SCHEME_PROTECTS_DATA("unique per aac_fib", aac_blockread aac_blockwrite \
687     aac_blockread64 aac_raw_io aac_sg_entry aac_sg_entry64 aac_sg_entryraw \
688     aac_sg_table aac_srb))
689 _NOTE(SCHEME_PROTECTS_DATA("unique to sync fib and cdb", scsi_inquiry))
690 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device scsi_address))
691 _NOTE(SCHEME_PROTECTS_DATA("unique to scsi_transport", buf))
692 
693 int
694 _init(void)
695 {
696 	int rval = 0;
697 
698 #ifdef DEBUG
699 	mutex_init(&aac_prt_mutex, NULL, MUTEX_DRIVER, NULL);
700 #endif
701 	DBCALLED(NULL, 1);
702 
703 	if ((rval = ddi_soft_state_init((void *)&aac_softstatep,
704 	    sizeof (struct aac_softstate), 0)) != 0)
705 		goto error;
706 
707 	if ((rval = scsi_hba_init(&aac_modlinkage)) != 0) {
708 		ddi_soft_state_fini((void *)&aac_softstatep);
709 		goto error;
710 	}
711 
712 	if ((rval = mod_install(&aac_modlinkage)) != 0) {
713 		ddi_soft_state_fini((void *)&aac_softstatep);
714 		scsi_hba_fini(&aac_modlinkage);
715 		goto error;
716 	}
717 	return (rval);
718 
719 error:
720 	AACDB_PRINT(NULL, CE_WARN, "Mod init error!");
721 #ifdef DEBUG
722 	mutex_destroy(&aac_prt_mutex);
723 #endif
724 	return (rval);
725 }
726 
727 int
728 _info(struct modinfo *modinfop)
729 {
730 	DBCALLED(NULL, 1);
731 	return (mod_info(&aac_modlinkage, modinfop));
732 }
733 
734 /*
735  * An HBA driver cannot be unload unless you reboot,
736  * so this function will be of no use.
737  */
738 int
739 _fini(void)
740 {
741 	int rval;
742 
743 	DBCALLED(NULL, 1);
744 
745 	if ((rval = mod_remove(&aac_modlinkage)) != 0)
746 		goto error;
747 
748 	scsi_hba_fini(&aac_modlinkage);
749 	ddi_soft_state_fini((void *)&aac_softstatep);
750 #ifdef DEBUG
751 	mutex_destroy(&aac_prt_mutex);
752 #endif
753 	return (0);
754 
755 error:
756 	AACDB_PRINT(NULL, CE_WARN, "AAC is busy, cannot unload!");
757 	return (rval);
758 }
759 
760 static int
761 aac_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
762 {
763 	int instance, i;
764 	struct aac_softstate *softs = NULL;
765 	int attach_state = 0;
766 	char *data;
767 
768 	DBCALLED(NULL, 1);
769 
770 	switch (cmd) {
771 	case DDI_ATTACH:
772 		break;
773 	case DDI_RESUME:
774 		return (DDI_FAILURE);
775 	default:
776 		return (DDI_FAILURE);
777 	}
778 
779 	instance = ddi_get_instance(dip);
780 
781 	/* Get soft state */
782 	if (ddi_soft_state_zalloc(aac_softstatep, instance) != DDI_SUCCESS) {
783 		AACDB_PRINT(softs, CE_WARN, "Cannot alloc soft state");
784 		goto error;
785 	}
786 	softs = ddi_get_soft_state(aac_softstatep, instance);
787 	attach_state |= AAC_ATTACH_SOFTSTATE_ALLOCED;
788 
789 	softs->instance = instance;
790 	softs->devinfo_p = dip;
791 	softs->buf_dma_attr = softs->addr_dma_attr = aac_dma_attr;
792 	softs->addr_dma_attr.dma_attr_granular = 1;
793 	softs->acc_attr = aac_acc_attr;
794 	softs->reg_attr = aac_acc_attr;
795 	softs->card = AAC_UNKNOWN_CARD;
796 #ifdef DEBUG
797 	softs->debug_flags = aac_debug_flags;
798 	softs->debug_fib_flags = aac_debug_fib_flags;
799 #endif
800 
801 	/* Initialize FMA */
802 	aac_fm_init(softs);
803 
804 	/* Check the card type */
805 	if (aac_check_card_type(softs) == AACERR) {
806 		AACDB_PRINT(softs, CE_WARN, "Card not supported");
807 		goto error;
808 	}
809 	/* We have found the right card and everything is OK */
810 	attach_state |= AAC_ATTACH_CARD_DETECTED;
811 
812 	/* Map PCI mem space */
813 	if (ddi_regs_map_setup(dip, 1,
814 	    (caddr_t *)&softs->pci_mem_base_vaddr, 0,
815 	    softs->map_size_min, &softs->reg_attr,
816 	    &softs->pci_mem_handle) != DDI_SUCCESS)
817 		goto error;
818 
819 	softs->map_size = softs->map_size_min;
820 	attach_state |= AAC_ATTACH_PCI_MEM_MAPPED;
821 
822 	AAC_DISABLE_INTR(softs);
823 
824 	/* Init mutexes and condvars */
825 	mutex_init(&softs->io_lock, NULL, MUTEX_DRIVER,
826 	    DDI_INTR_PRI(softs->intr_pri));
827 	mutex_init(&softs->q_comp_mutex, NULL, MUTEX_DRIVER,
828 	    DDI_INTR_PRI(softs->intr_pri));
829 	mutex_init(&softs->time_mutex, NULL, MUTEX_DRIVER,
830 	    DDI_INTR_PRI(softs->intr_pri));
831 	mutex_init(&softs->ev_lock, NULL, MUTEX_DRIVER,
832 	    DDI_INTR_PRI(softs->intr_pri));
833 	mutex_init(&softs->aifq_mutex, NULL,
834 	    MUTEX_DRIVER, DDI_INTR_PRI(softs->intr_pri));
835 	cv_init(&softs->event, NULL, CV_DRIVER, NULL);
836 	cv_init(&softs->sync_fib_cv, NULL, CV_DRIVER, NULL);
837 	cv_init(&softs->drain_cv, NULL, CV_DRIVER, NULL);
838 	cv_init(&softs->event_wait_cv, NULL, CV_DRIVER, NULL);
839 	cv_init(&softs->event_disp_cv, NULL, CV_DRIVER, NULL);
840 	cv_init(&softs->aifq_cv, NULL, CV_DRIVER, NULL);
841 	attach_state |= AAC_ATTACH_KMUTEX_INITED;
842 
843 	/* Init the cmd queues */
844 	for (i = 0; i < AAC_CMDQ_NUM; i++)
845 		aac_cmd_initq(&softs->q_wait[i]);
846 	aac_cmd_initq(&softs->q_busy);
847 	aac_cmd_initq(&softs->q_comp);
848 
849 	/* Check for legacy device naming support */
850 	softs->legacy = 1; /* default to use legacy name */
851 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
852 	    "legacy-name-enable", &data) == DDI_SUCCESS)) {
853 		if (strcmp(data, "no") == 0) {
854 			AACDB_PRINT(softs, CE_NOTE, "legacy-name disabled");
855 			softs->legacy = 0;
856 		}
857 		ddi_prop_free(data);
858 	}
859 
860 	/*
861 	 * Everything has been set up till now,
862 	 * we will do some common attach.
863 	 */
864 	mutex_enter(&softs->io_lock);
865 	if (aac_common_attach(softs) == AACERR) {
866 		mutex_exit(&softs->io_lock);
867 		goto error;
868 	}
869 	mutex_exit(&softs->io_lock);
870 	attach_state |= AAC_ATTACH_COMM_SPACE_SETUP;
871 
872 	/* Check for buf breakup support */
873 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
874 	    "breakup-enable", &data) == DDI_SUCCESS)) {
875 		if (strcmp(data, "yes") == 0) {
876 			AACDB_PRINT(softs, CE_NOTE, "buf breakup enabled");
877 			softs->flags |= AAC_FLAGS_BRKUP;
878 		}
879 		ddi_prop_free(data);
880 	}
881 	softs->dma_max = softs->buf_dma_attr.dma_attr_maxxfer;
882 	if (softs->flags & AAC_FLAGS_BRKUP) {
883 		softs->dma_max = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
884 		    DDI_PROP_DONTPASS, "dma-max", softs->dma_max);
885 	}
886 
887 	if (aac_hba_setup(softs) != AACOK)
888 		goto error;
889 	attach_state |= AAC_ATTACH_SCSI_TRAN_SETUP;
890 
891 	/* Create devctl/scsi nodes for cfgadm */
892 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
893 	    INST2DEVCTL(instance), DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
894 		AACDB_PRINT(softs, CE_WARN, "failed to create devctl node");
895 		goto error;
896 	}
897 	attach_state |= AAC_ATTACH_CREATE_DEVCTL;
898 
899 	if (ddi_create_minor_node(dip, "scsi", S_IFCHR, INST2SCSI(instance),
900 	    DDI_NT_SCSI_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
901 		AACDB_PRINT(softs, CE_WARN, "failed to create scsi node");
902 		goto error;
903 	}
904 	attach_state |= AAC_ATTACH_CREATE_SCSI;
905 
906 	/* Create aac node for app. to issue ioctls */
907 	if (ddi_create_minor_node(dip, "aac", S_IFCHR, INST2AAC(instance),
908 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
909 		AACDB_PRINT(softs, CE_WARN, "failed to create aac node");
910 		goto error;
911 	}
912 
913 	/* Common attach is OK, so we are attached! */
914 	softs->state |= AAC_STATE_RUN;
915 
916 	/* Create event thread */
917 	softs->fibctx_p = &softs->aifctx;
918 	if ((softs->event_thread = thread_create(NULL, 0, aac_event_thread,
919 	    softs, 0, &p0, TS_RUN, minclsyspri)) == NULL) {
920 		AACDB_PRINT(softs, CE_WARN, "aif thread create failed");
921 		softs->state &= ~AAC_STATE_RUN;
922 		goto error;
923 	}
924 
925 	aac_unhold_bus(softs, AAC_IOCMD_SYNC | AAC_IOCMD_ASYNC);
926 
927 	/* Create a thread for command timeout */
928 	softs->timeout_id = timeout(aac_timer, (void *)softs,
929 	    (aac_tick * drv_usectohz(1000000)));
930 
931 	/* Common attach is OK, so we are attached! */
932 	ddi_report_dev(dip);
933 	AACDB_PRINT(softs, CE_NOTE, "aac attached ok");
934 	return (DDI_SUCCESS);
935 
936 error:
937 	if (attach_state & AAC_ATTACH_CREATE_SCSI)
938 		ddi_remove_minor_node(dip, "scsi");
939 	if (attach_state & AAC_ATTACH_CREATE_DEVCTL)
940 		ddi_remove_minor_node(dip, "devctl");
941 	if (attach_state & AAC_ATTACH_COMM_SPACE_SETUP)
942 		aac_common_detach(softs);
943 	if (attach_state & AAC_ATTACH_SCSI_TRAN_SETUP) {
944 		(void) scsi_hba_detach(dip);
945 		scsi_hba_tran_free(AAC_DIP2TRAN(dip));
946 	}
947 	if (attach_state & AAC_ATTACH_KMUTEX_INITED) {
948 		mutex_destroy(&softs->io_lock);
949 		mutex_destroy(&softs->q_comp_mutex);
950 		mutex_destroy(&softs->time_mutex);
951 		mutex_destroy(&softs->ev_lock);
952 		mutex_destroy(&softs->aifq_mutex);
953 		cv_destroy(&softs->event);
954 		cv_destroy(&softs->sync_fib_cv);
955 		cv_destroy(&softs->drain_cv);
956 		cv_destroy(&softs->event_wait_cv);
957 		cv_destroy(&softs->event_disp_cv);
958 		cv_destroy(&softs->aifq_cv);
959 	}
960 	if (attach_state & AAC_ATTACH_PCI_MEM_MAPPED)
961 		ddi_regs_map_free(&softs->pci_mem_handle);
962 	aac_fm_fini(softs);
963 	if (attach_state & AAC_ATTACH_CARD_DETECTED)
964 		softs->card = AACERR;
965 	if (attach_state & AAC_ATTACH_SOFTSTATE_ALLOCED)
966 		ddi_soft_state_free(aac_softstatep, instance);
967 	return (DDI_FAILURE);
968 }
969 
970 static int
971 aac_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
972 {
973 	scsi_hba_tran_t *tran = AAC_DIP2TRAN(dip);
974 	struct aac_softstate *softs = AAC_TRAN2SOFTS(tran);
975 
976 	DBCALLED(softs, 1);
977 
978 	switch (cmd) {
979 	case DDI_DETACH:
980 		break;
981 	case DDI_SUSPEND:
982 		return (DDI_FAILURE);
983 	default:
984 		return (DDI_FAILURE);
985 	}
986 
987 	mutex_enter(&softs->io_lock);
988 	AAC_DISABLE_INTR(softs);
989 	softs->state = AAC_STATE_STOPPED;
990 
991 	ddi_remove_minor_node(dip, "aac");
992 	ddi_remove_minor_node(dip, "scsi");
993 	ddi_remove_minor_node(dip, "devctl");
994 	mutex_exit(&softs->io_lock);
995 
996 	aac_common_detach(softs);
997 
998 	mutex_enter(&softs->io_lock);
999 	(void) scsi_hba_detach(dip);
1000 	scsi_hba_tran_free(tran);
1001 	mutex_exit(&softs->io_lock);
1002 
1003 	/* Stop timer */
1004 	mutex_enter(&softs->time_mutex);
1005 	if (softs->timeout_id) {
1006 		timeout_id_t tid = softs->timeout_id;
1007 		softs->timeout_id = 0;
1008 
1009 		mutex_exit(&softs->time_mutex);
1010 		(void) untimeout(tid);
1011 		mutex_enter(&softs->time_mutex);
1012 	}
1013 	mutex_exit(&softs->time_mutex);
1014 
1015 	/* Destroy event thread */
1016 	mutex_enter(&softs->ev_lock);
1017 	cv_signal(&softs->event_disp_cv);
1018 	cv_wait(&softs->event_wait_cv, &softs->ev_lock);
1019 	mutex_exit(&softs->ev_lock);
1020 
1021 	cv_destroy(&softs->aifq_cv);
1022 	cv_destroy(&softs->event_disp_cv);
1023 	cv_destroy(&softs->event_wait_cv);
1024 	cv_destroy(&softs->drain_cv);
1025 	cv_destroy(&softs->sync_fib_cv);
1026 	cv_destroy(&softs->event);
1027 	mutex_destroy(&softs->aifq_mutex);
1028 	mutex_destroy(&softs->ev_lock);
1029 	mutex_destroy(&softs->time_mutex);
1030 	mutex_destroy(&softs->q_comp_mutex);
1031 	mutex_destroy(&softs->io_lock);
1032 
1033 	ddi_regs_map_free(&softs->pci_mem_handle);
1034 	aac_fm_fini(softs);
1035 	softs->hwif = AAC_HWIF_UNKNOWN;
1036 	softs->card = AAC_UNKNOWN_CARD;
1037 	ddi_soft_state_free(aac_softstatep, ddi_get_instance(dip));
1038 
1039 	return (DDI_SUCCESS);
1040 }
1041 
1042 /*ARGSUSED*/
1043 static int
1044 aac_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
1045 {
1046 	struct aac_softstate *softs = AAC_DIP2SOFTS(dip);
1047 
1048 	DBCALLED(softs, 1);
1049 
1050 	mutex_enter(&softs->io_lock);
1051 	AAC_DISABLE_INTR(softs);
1052 	(void) aac_shutdown(softs);
1053 	mutex_exit(&softs->io_lock);
1054 
1055 	return (DDI_SUCCESS);
1056 }
1057 
1058 /*
1059  * quiesce(9E) entry point.
1060  *
1061  * This function is called when the system is single-threaded at high
1062  * PIL with preemption disabled. Therefore, this function must not be
1063  * blocked.
1064  *
1065  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
1066  * DDI_FAILURE indicates an error condition and should almost never happen.
1067  */
1068 static int
1069 aac_quiesce(dev_info_t *dip)
1070 {
1071 	struct aac_softstate *softs = AAC_DIP2SOFTS(dip);
1072 
1073 	if (softs == NULL)
1074 		return (DDI_FAILURE);
1075 
1076 	_NOTE(ASSUMING_PROTECTED(softs->state))
1077 	AAC_DISABLE_INTR(softs);
1078 
1079 	return (DDI_SUCCESS);
1080 }
1081 
1082 /* ARGSUSED */
1083 static int
1084 aac_getinfo(dev_info_t *self, ddi_info_cmd_t infocmd, void *arg,
1085     void **result)
1086 {
1087 	int error = DDI_SUCCESS;
1088 
1089 	switch (infocmd) {
1090 	case DDI_INFO_DEVT2INSTANCE:
1091 		*result = (void *)(intptr_t)(MINOR2INST(getminor((dev_t)arg)));
1092 		break;
1093 	default:
1094 		error = DDI_FAILURE;
1095 	}
1096 	return (error);
1097 }
1098 
1099 /*
1100  * Bring the controller down to a dormant state and detach all child devices.
1101  * This function is called before detach or system shutdown.
1102  * Note: we can assume that the q_wait on the controller is empty, as we
1103  * won't allow shutdown if any device is open.
1104  */
1105 static int
1106 aac_shutdown(struct aac_softstate *softs)
1107 {
1108 	ddi_acc_handle_t acc;
1109 	struct aac_close_command *cc;
1110 	int rval;
1111 
1112 	(void) aac_sync_fib_slot_bind(softs, &softs->sync_ac);
1113 	acc = softs->sync_ac.slotp->fib_acc_handle;
1114 
1115 	cc = (struct aac_close_command *)&softs->sync_ac.slotp->fibp->data[0];
1116 
1117 	ddi_put32(acc, &cc->Command, VM_CloseAll);
1118 	ddi_put32(acc, &cc->ContainerId, 0xfffffffful);
1119 
1120 	/* Flush all caches, set FW to write through mode */
1121 	rval = aac_sync_fib(softs, ContainerCommand,
1122 	    AAC_FIB_SIZEOF(struct aac_close_command));
1123 	aac_sync_fib_slot_release(softs, &softs->sync_ac);
1124 
1125 	AACDB_PRINT(softs, CE_NOTE,
1126 	    "shutting down aac %s", (rval == AACOK) ? "ok" : "fail");
1127 	return (rval);
1128 }
1129 
1130 static uint_t
1131 aac_softintr(caddr_t arg)
1132 {
1133 	struct aac_softstate *softs = (void *)arg;
1134 
1135 	if (!AAC_IS_Q_EMPTY(&softs->q_comp)) {
1136 		aac_drain_comp_q(softs);
1137 	}
1138 	return (DDI_INTR_CLAIMED);
1139 }
1140 
1141 /*
1142  * Setup auto sense data for pkt
1143  */
1144 static void
1145 aac_set_arq_data(struct scsi_pkt *pkt, uchar_t key,
1146     uchar_t add_code, uchar_t qual_code, uint64_t info)
1147 {
1148 	struct scsi_arq_status *arqstat = (void *)(pkt->pkt_scbp);
1149 
1150 	*pkt->pkt_scbp = STATUS_CHECK; /* CHECK CONDITION */
1151 	pkt->pkt_state |= STATE_ARQ_DONE;
1152 
1153 	*(uint8_t *)&arqstat->sts_rqpkt_status = STATUS_GOOD;
1154 	arqstat->sts_rqpkt_reason = CMD_CMPLT;
1155 	arqstat->sts_rqpkt_resid = 0;
1156 	arqstat->sts_rqpkt_state =
1157 	    STATE_GOT_BUS |
1158 	    STATE_GOT_TARGET |
1159 	    STATE_SENT_CMD |
1160 	    STATE_XFERRED_DATA;
1161 	arqstat->sts_rqpkt_statistics = 0;
1162 
1163 	if (info <= 0xfffffffful) {
1164 		arqstat->sts_sensedata.es_valid = 1;
1165 		arqstat->sts_sensedata.es_class = CLASS_EXTENDED_SENSE;
1166 		arqstat->sts_sensedata.es_code = CODE_FMT_FIXED_CURRENT;
1167 		arqstat->sts_sensedata.es_key = key;
1168 		arqstat->sts_sensedata.es_add_code = add_code;
1169 		arqstat->sts_sensedata.es_qual_code = qual_code;
1170 
1171 		arqstat->sts_sensedata.es_info_1 = (info >> 24) & 0xFF;
1172 		arqstat->sts_sensedata.es_info_2 = (info >> 16) & 0xFF;
1173 		arqstat->sts_sensedata.es_info_3 = (info >>  8) & 0xFF;
1174 		arqstat->sts_sensedata.es_info_4 = info & 0xFF;
1175 	} else { /* 64-bit LBA */
1176 		struct scsi_descr_sense_hdr *dsp;
1177 		struct scsi_information_sense_descr *isd;
1178 
1179 		dsp = (struct scsi_descr_sense_hdr *)&arqstat->sts_sensedata;
1180 		dsp->ds_class = CLASS_EXTENDED_SENSE;
1181 		dsp->ds_code = CODE_FMT_DESCR_CURRENT;
1182 		dsp->ds_key = key;
1183 		dsp->ds_add_code = add_code;
1184 		dsp->ds_qual_code = qual_code;
1185 		dsp->ds_addl_sense_length =
1186 		    sizeof (struct scsi_information_sense_descr);
1187 
1188 		isd = (struct scsi_information_sense_descr *)(dsp+1);
1189 		isd->isd_descr_type = DESCR_INFORMATION;
1190 		isd->isd_valid = 1;
1191 		isd->isd_information[0] = (info >> 56) & 0xFF;
1192 		isd->isd_information[1] = (info >> 48) & 0xFF;
1193 		isd->isd_information[2] = (info >> 40) & 0xFF;
1194 		isd->isd_information[3] = (info >> 32) & 0xFF;
1195 		isd->isd_information[4] = (info >> 24) & 0xFF;
1196 		isd->isd_information[5] = (info >> 16) & 0xFF;
1197 		isd->isd_information[6] = (info >>  8) & 0xFF;
1198 		isd->isd_information[7] = (info) & 0xFF;
1199 	}
1200 }
1201 
1202 /*
1203  * Setup auto sense data for HARDWARE ERROR
1204  */
1205 static void
1206 aac_set_arq_data_hwerr(struct aac_cmd *acp)
1207 {
1208 	union scsi_cdb *cdbp;
1209 	uint64_t err_blkno;
1210 
1211 	cdbp = (void *)acp->pkt->pkt_cdbp;
1212 	err_blkno = AAC_GETGXADDR(acp->cmdlen, cdbp);
1213 	aac_set_arq_data(acp->pkt, KEY_HARDWARE_ERROR, 0x00, 0x00, err_blkno);
1214 }
1215 
1216 /*
1217  * Send a command to the adapter in New Comm. interface
1218  */
1219 static int
1220 aac_send_command(struct aac_softstate *softs, struct aac_slot *slotp)
1221 {
1222 	uint32_t index, device;
1223 
1224 	index = PCI_MEM_GET32(softs, AAC_IQUE);
1225 	if (index == 0xffffffffUL) {
1226 		index = PCI_MEM_GET32(softs, AAC_IQUE);
1227 		if (index == 0xffffffffUL)
1228 			return (AACERR);
1229 	}
1230 
1231 	device = index;
1232 	PCI_MEM_PUT32(softs, device,
1233 	    (uint32_t)(slotp->fib_phyaddr & 0xfffffffful));
1234 	device += 4;
1235 	PCI_MEM_PUT32(softs, device, (uint32_t)(slotp->fib_phyaddr >> 32));
1236 	device += 4;
1237 	PCI_MEM_PUT32(softs, device, slotp->acp->fib_size);
1238 	PCI_MEM_PUT32(softs, AAC_IQUE, index);
1239 	return (AACOK);
1240 }
1241 
1242 static void
1243 aac_end_io(struct aac_softstate *softs, struct aac_cmd *acp)
1244 {
1245 	struct aac_device *dvp = acp->dvp;
1246 	int q = AAC_CMDQ(acp);
1247 
1248 	if (acp->slotp) { /* outstanding cmd */
1249 		if (!(acp->flags & AAC_CMD_IN_SYNC_SLOT)) {
1250 			aac_release_slot(softs, acp->slotp);
1251 			acp->slotp = NULL;
1252 		}
1253 		if (dvp) {
1254 			dvp->ncmds[q]--;
1255 			if (dvp->throttle[q] == AAC_THROTTLE_DRAIN &&
1256 			    dvp->ncmds[q] == 0 && q == AAC_CMDQ_ASYNC)
1257 				aac_set_throttle(softs, dvp, q,
1258 				    softs->total_slots);
1259 			/*
1260 			 * Setup auto sense data for UNIT ATTENTION
1261 			 * Each lun should generate a unit attention
1262 			 * condition when reset.
1263 			 * Phys. drives are treated as logical ones
1264 			 * during error recovery.
1265 			 */
1266 			if (dvp->type == AAC_DEV_LD) {
1267 				struct aac_container *ctp =
1268 				    (struct aac_container *)dvp;
1269 				if (ctp->reset == 0)
1270 					goto noreset;
1271 
1272 				AACDB_PRINT(softs, CE_NOTE,
1273 				    "Unit attention: reset");
1274 				ctp->reset = 0;
1275 				aac_set_arq_data(acp->pkt, KEY_UNIT_ATTENTION,
1276 				    0x29, 0x02, 0);
1277 			}
1278 		}
1279 noreset:
1280 		softs->bus_ncmds[q]--;
1281 		aac_cmd_delete(&softs->q_busy, acp);
1282 	} else { /* cmd in waiting queue */
1283 		aac_cmd_delete(&softs->q_wait[q], acp);
1284 	}
1285 
1286 	if (!(acp->flags & (AAC_CMD_NO_CB | AAC_CMD_NO_INTR))) { /* async IO */
1287 		mutex_enter(&softs->q_comp_mutex);
1288 		aac_cmd_enqueue(&softs->q_comp, acp);
1289 		mutex_exit(&softs->q_comp_mutex);
1290 	} else if (acp->flags & AAC_CMD_NO_CB) { /* sync IO */
1291 		cv_broadcast(&softs->event);
1292 	}
1293 }
1294 
1295 static void
1296 aac_handle_io(struct aac_softstate *softs, int index)
1297 {
1298 	struct aac_slot *slotp;
1299 	struct aac_cmd *acp;
1300 	uint32_t fast;
1301 
1302 	fast = index & AAC_SENDERADDR_MASK_FAST_RESPONSE;
1303 	index >>= 2;
1304 
1305 	/* Make sure firmware reported index is valid */
1306 	ASSERT(index >= 0 && index < softs->total_slots);
1307 	slotp = &softs->io_slot[index];
1308 	ASSERT(slotp->index == index);
1309 	acp = slotp->acp;
1310 
1311 	if (acp == NULL || acp->slotp != slotp) {
1312 		cmn_err(CE_WARN,
1313 		    "Firmware error: invalid slot index received from FW");
1314 		return;
1315 	}
1316 
1317 	acp->flags |= AAC_CMD_CMPLT;
1318 	(void) ddi_dma_sync(slotp->fib_dma_handle, 0, 0, DDI_DMA_SYNC_FORCPU);
1319 
1320 	if (aac_check_dma_handle(slotp->fib_dma_handle) == DDI_SUCCESS) {
1321 		/*
1322 		 * For fast response IO, the firmware do not return any FIB
1323 		 * data, so we need to fill in the FIB status and state so that
1324 		 * FIB users can handle it correctly.
1325 		 */
1326 		if (fast) {
1327 			uint32_t state;
1328 
1329 			state = ddi_get32(slotp->fib_acc_handle,
1330 			    &slotp->fibp->Header.XferState);
1331 			/*
1332 			 * Update state for CPU not for device, no DMA sync
1333 			 * needed
1334 			 */
1335 			ddi_put32(slotp->fib_acc_handle,
1336 			    &slotp->fibp->Header.XferState,
1337 			    state | AAC_FIBSTATE_DONEADAP);
1338 			ddi_put32(slotp->fib_acc_handle,
1339 			    (void *)&slotp->fibp->data[0], ST_OK);
1340 		}
1341 
1342 		/* Handle completed ac */
1343 		acp->ac_comp(softs, acp);
1344 	} else {
1345 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_UNAFFECTED);
1346 		acp->flags |= AAC_CMD_ERR;
1347 		if (acp->pkt) {
1348 			acp->pkt->pkt_reason = CMD_TRAN_ERR;
1349 			acp->pkt->pkt_statistics = 0;
1350 		}
1351 	}
1352 	aac_end_io(softs, acp);
1353 }
1354 
1355 /*
1356  * Interrupt handler for New Comm. interface
1357  * New Comm. interface use a different mechanism for interrupt. No explict
1358  * message queues, and driver need only accesses the mapped PCI mem space to
1359  * find the completed FIB or AIF.
1360  */
1361 static int
1362 aac_process_intr_new(struct aac_softstate *softs)
1363 {
1364 	uint32_t index;
1365 
1366 	index = AAC_OUTB_GET(softs);
1367 	if (index == 0xfffffffful)
1368 		index = AAC_OUTB_GET(softs);
1369 	if (aac_check_acc_handle(softs->pci_mem_handle) != DDI_SUCCESS) {
1370 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_UNAFFECTED);
1371 		return (0);
1372 	}
1373 	if (index != 0xfffffffful) {
1374 		do {
1375 			if ((index & AAC_SENDERADDR_MASK_AIF) == 0) {
1376 				aac_handle_io(softs, index);
1377 			} else if (index != 0xfffffffeul) {
1378 				struct aac_fib *fibp;	/* FIB in AIF queue */
1379 				uint16_t fib_size;
1380 
1381 				/*
1382 				 * 0xfffffffe means that the controller wants
1383 				 * more work, ignore it for now. Otherwise,
1384 				 * AIF received.
1385 				 */
1386 				index &= ~2;
1387 
1388 				fibp = (struct aac_fib *)(softs-> \
1389 				    pci_mem_base_vaddr + index);
1390 				fib_size = PCI_MEM_GET16(softs, index + \
1391 				    offsetof(struct aac_fib, Header.Size));
1392 
1393 				aac_save_aif(softs, softs->pci_mem_handle,
1394 				    fibp, fib_size);
1395 
1396 				/*
1397 				 * AIF memory is owned by the adapter, so let it
1398 				 * know that we are done with it.
1399 				 */
1400 				AAC_OUTB_SET(softs, index);
1401 				AAC_STATUS_CLR(softs, AAC_DB_RESPONSE_READY);
1402 			}
1403 
1404 			index = AAC_OUTB_GET(softs);
1405 		} while (index != 0xfffffffful);
1406 
1407 		/*
1408 		 * Process waiting cmds before start new ones to
1409 		 * ensure first IOs are serviced first.
1410 		 */
1411 		aac_start_waiting_io(softs);
1412 		return (AAC_DB_COMMAND_READY);
1413 	} else {
1414 		return (0);
1415 	}
1416 }
1417 
1418 static uint_t
1419 aac_intr_new(caddr_t arg)
1420 {
1421 	struct aac_softstate *softs = (void *)arg;
1422 	uint_t rval;
1423 
1424 	mutex_enter(&softs->io_lock);
1425 	if (aac_process_intr_new(softs))
1426 		rval = DDI_INTR_CLAIMED;
1427 	else
1428 		rval = DDI_INTR_UNCLAIMED;
1429 	mutex_exit(&softs->io_lock);
1430 
1431 	aac_drain_comp_q(softs);
1432 	return (rval);
1433 }
1434 
1435 /*
1436  * Interrupt handler for old interface
1437  * Explicit message queues are used to send FIB to and get completed FIB from
1438  * the adapter. Driver and adapter maitain the queues in the producer/consumer
1439  * manner. The driver has to query the queues to find the completed FIB.
1440  */
1441 static int
1442 aac_process_intr_old(struct aac_softstate *softs)
1443 {
1444 	uint16_t status;
1445 
1446 	status = AAC_STATUS_GET(softs);
1447 	if (aac_check_acc_handle(softs->pci_mem_handle) != DDI_SUCCESS) {
1448 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_UNAFFECTED);
1449 		return (DDI_INTR_UNCLAIMED);
1450 	}
1451 	if (status & AAC_DB_RESPONSE_READY) {
1452 		int slot_idx;
1453 
1454 		/* ACK the intr */
1455 		AAC_STATUS_CLR(softs, AAC_DB_RESPONSE_READY);
1456 		(void) AAC_STATUS_GET(softs);
1457 		while (aac_fib_dequeue(softs, AAC_HOST_NORM_RESP_Q,
1458 		    &slot_idx) == AACOK)
1459 			aac_handle_io(softs, slot_idx);
1460 
1461 		/*
1462 		 * Process waiting cmds before start new ones to
1463 		 * ensure first IOs are serviced first.
1464 		 */
1465 		aac_start_waiting_io(softs);
1466 		return (AAC_DB_RESPONSE_READY);
1467 	} else if (status & AAC_DB_COMMAND_READY) {
1468 		int aif_idx;
1469 
1470 		AAC_STATUS_CLR(softs, AAC_DB_COMMAND_READY);
1471 		(void) AAC_STATUS_GET(softs);
1472 		if (aac_fib_dequeue(softs, AAC_HOST_NORM_CMD_Q, &aif_idx) ==
1473 		    AACOK) {
1474 			ddi_acc_handle_t acc = softs->comm_space_acc_handle;
1475 			struct aac_fib *fibp;	/* FIB in communication space */
1476 			uint16_t fib_size;
1477 			uint32_t fib_xfer_state;
1478 			uint32_t addr, size;
1479 
1480 			ASSERT((aif_idx >= 0) && (aif_idx < AAC_ADAPTER_FIBS));
1481 
1482 #define	AAC_SYNC_AIF(softs, aif_idx, type) \
1483 	{ (void) ddi_dma_sync((softs)->comm_space_dma_handle, \
1484 	    offsetof(struct aac_comm_space, \
1485 	    adapter_fibs[(aif_idx)]), AAC_FIB_SIZE, \
1486 	    (type)); }
1487 
1488 			/* Copy AIF from adapter to the empty AIF slot */
1489 			AAC_SYNC_AIF(softs, aif_idx, DDI_DMA_SYNC_FORCPU);
1490 			fibp = &softs->comm_space->adapter_fibs[aif_idx];
1491 			fib_size = ddi_get16(acc, &fibp->Header.Size);
1492 
1493 			aac_save_aif(softs, acc, fibp, fib_size);
1494 
1495 			/* Complete AIF back to adapter with good status */
1496 			fib_xfer_state = LE_32(fibp->Header.XferState);
1497 			if (fib_xfer_state & AAC_FIBSTATE_FROMADAP) {
1498 				ddi_put32(acc, &fibp->Header.XferState,
1499 				    fib_xfer_state | AAC_FIBSTATE_DONEHOST);
1500 				ddi_put32(acc, (void *)&fibp->data[0], ST_OK);
1501 				if (fib_size > AAC_FIB_SIZE)
1502 					ddi_put16(acc, &fibp->Header.Size,
1503 					    AAC_FIB_SIZE);
1504 				AAC_SYNC_AIF(softs, aif_idx,
1505 				    DDI_DMA_SYNC_FORDEV);
1506 			}
1507 
1508 			/* Put the AIF response on the response queue */
1509 			addr = ddi_get32(acc,
1510 			    &softs->comm_space->adapter_fibs[aif_idx]. \
1511 			    Header.SenderFibAddress);
1512 			size = (uint32_t)ddi_get16(acc,
1513 			    &softs->comm_space->adapter_fibs[aif_idx]. \
1514 			    Header.Size);
1515 			ddi_put32(acc,
1516 			    &softs->comm_space->adapter_fibs[aif_idx]. \
1517 			    Header.ReceiverFibAddress, addr);
1518 			if (aac_fib_enqueue(softs, AAC_ADAP_NORM_RESP_Q,
1519 			    addr, size) == AACERR)
1520 				cmn_err(CE_NOTE, "!AIF ack failed");
1521 		}
1522 		return (AAC_DB_COMMAND_READY);
1523 	} else if (status & AAC_DB_PRINTF_READY) {
1524 		/* ACK the intr */
1525 		AAC_STATUS_CLR(softs, AAC_DB_PRINTF_READY);
1526 		(void) AAC_STATUS_GET(softs);
1527 		(void) ddi_dma_sync(softs->comm_space_dma_handle,
1528 		    offsetof(struct aac_comm_space, adapter_print_buf),
1529 		    AAC_ADAPTER_PRINT_BUFSIZE, DDI_DMA_SYNC_FORCPU);
1530 		if (aac_check_dma_handle(softs->comm_space_dma_handle) ==
1531 		    DDI_SUCCESS)
1532 			cmn_err(CE_NOTE, "MSG From Adapter: %s",
1533 			    softs->comm_space->adapter_print_buf);
1534 		else
1535 			ddi_fm_service_impact(softs->devinfo_p,
1536 			    DDI_SERVICE_UNAFFECTED);
1537 		AAC_NOTIFY(softs, AAC_DB_PRINTF_READY);
1538 		return (AAC_DB_PRINTF_READY);
1539 	} else if (status & AAC_DB_COMMAND_NOT_FULL) {
1540 		/*
1541 		 * Without these two condition statements, the OS could hang
1542 		 * after a while, especially if there are a lot of AIF's to
1543 		 * handle, for instance if a drive is pulled from an array
1544 		 * under heavy load.
1545 		 */
1546 		AAC_STATUS_CLR(softs, AAC_DB_COMMAND_NOT_FULL);
1547 		return (AAC_DB_COMMAND_NOT_FULL);
1548 	} else if (status & AAC_DB_RESPONSE_NOT_FULL) {
1549 		AAC_STATUS_CLR(softs, AAC_DB_COMMAND_NOT_FULL);
1550 		AAC_STATUS_CLR(softs, AAC_DB_RESPONSE_NOT_FULL);
1551 		return (AAC_DB_RESPONSE_NOT_FULL);
1552 	} else {
1553 		return (0);
1554 	}
1555 }
1556 
1557 static uint_t
1558 aac_intr_old(caddr_t arg)
1559 {
1560 	struct aac_softstate *softs = (void *)arg;
1561 	int rval;
1562 
1563 	mutex_enter(&softs->io_lock);
1564 	if (aac_process_intr_old(softs))
1565 		rval = DDI_INTR_CLAIMED;
1566 	else
1567 		rval = DDI_INTR_UNCLAIMED;
1568 	mutex_exit(&softs->io_lock);
1569 
1570 	aac_drain_comp_q(softs);
1571 	return (rval);
1572 }
1573 
1574 /*
1575  * Query FIXED or MSI interrupts
1576  */
1577 static int
1578 aac_query_intrs(struct aac_softstate *softs, int intr_type)
1579 {
1580 	dev_info_t *dip = softs->devinfo_p;
1581 	int avail, actual, count;
1582 	int i, flag, ret;
1583 
1584 	AACDB_PRINT(softs, CE_NOTE,
1585 	    "aac_query_intrs:interrupt type 0x%x", intr_type);
1586 
1587 	/* Get number of interrupts */
1588 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
1589 	if ((ret != DDI_SUCCESS) || (count == 0)) {
1590 		AACDB_PRINT(softs, CE_WARN,
1591 		    "ddi_intr_get_nintrs() failed, ret %d count %d",
1592 		    ret, count);
1593 		return (DDI_FAILURE);
1594 	}
1595 
1596 	/* Get number of available interrupts */
1597 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
1598 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
1599 		AACDB_PRINT(softs, CE_WARN,
1600 		    "ddi_intr_get_navail() failed, ret %d avail %d",
1601 		    ret, avail);
1602 		return (DDI_FAILURE);
1603 	}
1604 
1605 	AACDB_PRINT(softs, CE_NOTE,
1606 	    "ddi_intr_get_nvail returned %d, navail() returned %d",
1607 	    count, avail);
1608 
1609 	/* Allocate an array of interrupt handles */
1610 	softs->intr_size = count * sizeof (ddi_intr_handle_t);
1611 	softs->htable = kmem_alloc(softs->intr_size, KM_SLEEP);
1612 
1613 	if (intr_type == DDI_INTR_TYPE_MSI) {
1614 		count = 1; /* only one vector needed by now */
1615 		flag = DDI_INTR_ALLOC_STRICT;
1616 	} else { /* must be DDI_INTR_TYPE_FIXED */
1617 		flag = DDI_INTR_ALLOC_NORMAL;
1618 	}
1619 
1620 	/* Call ddi_intr_alloc() */
1621 	ret = ddi_intr_alloc(dip, softs->htable, intr_type, 0,
1622 	    count, &actual, flag);
1623 
1624 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
1625 		AACDB_PRINT(softs, CE_WARN,
1626 		    "ddi_intr_alloc() failed, ret = %d", ret);
1627 		actual = 0;
1628 		goto error;
1629 	}
1630 
1631 	if (actual < count) {
1632 		AACDB_PRINT(softs, CE_NOTE,
1633 		    "Requested: %d, Received: %d", count, actual);
1634 		goto error;
1635 	}
1636 
1637 	softs->intr_cnt = actual;
1638 
1639 	/* Get priority for first msi, assume remaining are all the same */
1640 	if ((ret = ddi_intr_get_pri(softs->htable[0],
1641 	    &softs->intr_pri)) != DDI_SUCCESS) {
1642 		AACDB_PRINT(softs, CE_WARN,
1643 		    "ddi_intr_get_pri() failed, ret = %d", ret);
1644 		goto error;
1645 	}
1646 
1647 	/* Test for high level mutex */
1648 	if (softs->intr_pri >= ddi_intr_get_hilevel_pri()) {
1649 		AACDB_PRINT(softs, CE_WARN,
1650 		    "aac_query_intrs: Hi level interrupt not supported");
1651 		goto error;
1652 	}
1653 
1654 	return (DDI_SUCCESS);
1655 
1656 error:
1657 	/* Free already allocated intr */
1658 	for (i = 0; i < actual; i++)
1659 		(void) ddi_intr_free(softs->htable[i]);
1660 
1661 	kmem_free(softs->htable, softs->intr_size);
1662 	return (DDI_FAILURE);
1663 }
1664 
1665 
1666 /*
1667  * Register FIXED or MSI interrupts, and enable them
1668  */
1669 static int
1670 aac_add_intrs(struct aac_softstate *softs)
1671 {
1672 	int i, ret;
1673 	int actual;
1674 	ddi_intr_handler_t *aac_intr;
1675 
1676 	actual = softs->intr_cnt;
1677 	aac_intr = (ddi_intr_handler_t *)((softs->flags & AAC_FLAGS_NEW_COMM) ?
1678 	    aac_intr_new : aac_intr_old);
1679 
1680 	/* Call ddi_intr_add_handler() */
1681 	for (i = 0; i < actual; i++) {
1682 		if ((ret = ddi_intr_add_handler(softs->htable[i],
1683 		    aac_intr, (caddr_t)softs, NULL)) != DDI_SUCCESS) {
1684 			cmn_err(CE_WARN,
1685 			    "ddi_intr_add_handler() failed ret = %d", ret);
1686 
1687 			/* Free already allocated intr */
1688 			for (i = 0; i < actual; i++)
1689 				(void) ddi_intr_free(softs->htable[i]);
1690 
1691 			kmem_free(softs->htable, softs->intr_size);
1692 			return (DDI_FAILURE);
1693 		}
1694 	}
1695 
1696 	if ((ret = ddi_intr_get_cap(softs->htable[0], &softs->intr_cap))
1697 	    != DDI_SUCCESS) {
1698 		cmn_err(CE_WARN, "ddi_intr_get_cap() failed, ret = %d", ret);
1699 
1700 		/* Free already allocated intr */
1701 		for (i = 0; i < actual; i++)
1702 			(void) ddi_intr_free(softs->htable[i]);
1703 
1704 		kmem_free(softs->htable, softs->intr_size);
1705 		return (DDI_FAILURE);
1706 	}
1707 
1708 	return (DDI_SUCCESS);
1709 }
1710 
1711 /*
1712  * Unregister FIXED or MSI interrupts
1713  */
1714 static void
1715 aac_remove_intrs(struct aac_softstate *softs)
1716 {
1717 	int i;
1718 
1719 	/* Disable all interrupts */
1720 	(void) aac_disable_intrs(softs);
1721 	/* Call ddi_intr_remove_handler() */
1722 	for (i = 0; i < softs->intr_cnt; i++) {
1723 		(void) ddi_intr_remove_handler(softs->htable[i]);
1724 		(void) ddi_intr_free(softs->htable[i]);
1725 	}
1726 
1727 	kmem_free(softs->htable, softs->intr_size);
1728 }
1729 
1730 static int
1731 aac_enable_intrs(struct aac_softstate *softs)
1732 {
1733 	int rval = AACOK;
1734 
1735 	if (softs->intr_cap & DDI_INTR_FLAG_BLOCK) {
1736 		/* for MSI block enable */
1737 		if (ddi_intr_block_enable(softs->htable, softs->intr_cnt) !=
1738 		    DDI_SUCCESS)
1739 			rval = AACERR;
1740 	} else {
1741 		int i;
1742 
1743 		/* Call ddi_intr_enable() for legacy/MSI non block enable */
1744 		for (i = 0; i < softs->intr_cnt; i++) {
1745 			if (ddi_intr_enable(softs->htable[i]) != DDI_SUCCESS)
1746 				rval = AACERR;
1747 		}
1748 	}
1749 	return (rval);
1750 }
1751 
1752 static int
1753 aac_disable_intrs(struct aac_softstate *softs)
1754 {
1755 	int rval = AACOK;
1756 
1757 	if (softs->intr_cap & DDI_INTR_FLAG_BLOCK) {
1758 		/* Call ddi_intr_block_disable() */
1759 		if (ddi_intr_block_disable(softs->htable, softs->intr_cnt) !=
1760 		    DDI_SUCCESS)
1761 			rval = AACERR;
1762 	} else {
1763 		int i;
1764 
1765 		for (i = 0; i < softs->intr_cnt; i++) {
1766 			if (ddi_intr_disable(softs->htable[i]) != DDI_SUCCESS)
1767 				rval = AACERR;
1768 		}
1769 	}
1770 	return (rval);
1771 }
1772 
1773 /*
1774  * Set pkt_reason and OR in pkt_statistics flag
1775  */
1776 static void
1777 aac_set_pkt_reason(struct aac_softstate *softs, struct aac_cmd *acp,
1778     uchar_t reason, uint_t stat)
1779 {
1780 #ifndef __lock_lint
1781 	_NOTE(ARGUNUSED(softs))
1782 #endif
1783 	if (acp->pkt->pkt_reason == CMD_CMPLT)
1784 		acp->pkt->pkt_reason = reason;
1785 	acp->pkt->pkt_statistics |= stat;
1786 }
1787 
1788 /*
1789  * Handle a finished pkt of soft SCMD
1790  */
1791 static void
1792 aac_soft_callback(struct aac_softstate *softs, struct aac_cmd *acp)
1793 {
1794 	ASSERT(acp->pkt);
1795 
1796 	acp->flags |= AAC_CMD_CMPLT;
1797 
1798 	acp->pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET | \
1799 	    STATE_SENT_CMD | STATE_GOT_STATUS;
1800 	if (acp->pkt->pkt_state & STATE_XFERRED_DATA)
1801 		acp->pkt->pkt_resid = 0;
1802 
1803 	/* AAC_CMD_NO_INTR means no complete callback */
1804 	if (!(acp->flags & AAC_CMD_NO_INTR)) {
1805 		mutex_enter(&softs->q_comp_mutex);
1806 		aac_cmd_enqueue(&softs->q_comp, acp);
1807 		mutex_exit(&softs->q_comp_mutex);
1808 		ddi_trigger_softintr(softs->softint_id);
1809 	}
1810 }
1811 
1812 /*
1813  * Handlers for completed IOs, common to aac_intr_new() and aac_intr_old()
1814  */
1815 
1816 /*
1817  * Handle completed logical device IO command
1818  */
1819 /*ARGSUSED*/
1820 static void
1821 aac_ld_complete(struct aac_softstate *softs, struct aac_cmd *acp)
1822 {
1823 	struct aac_slot *slotp = acp->slotp;
1824 	struct aac_blockread_response *resp;
1825 	uint32_t status;
1826 
1827 	ASSERT(!(acp->flags & AAC_CMD_SYNC));
1828 	ASSERT(!(acp->flags & AAC_CMD_NO_CB));
1829 
1830 	acp->pkt->pkt_state |= STATE_GOT_STATUS;
1831 
1832 	/*
1833 	 * block_read/write has a similar response header, use blockread
1834 	 * response for both.
1835 	 */
1836 	resp = (struct aac_blockread_response *)&slotp->fibp->data[0];
1837 	status = ddi_get32(slotp->fib_acc_handle, &resp->Status);
1838 	if (status == ST_OK) {
1839 		acp->pkt->pkt_resid = 0;
1840 		acp->pkt->pkt_state |= STATE_XFERRED_DATA;
1841 	} else {
1842 		aac_set_arq_data_hwerr(acp);
1843 	}
1844 }
1845 
1846 /*
1847  * Handle completed phys. device IO command
1848  */
1849 static void
1850 aac_pd_complete(struct aac_softstate *softs, struct aac_cmd *acp)
1851 {
1852 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
1853 	struct aac_fib *fibp = acp->slotp->fibp;
1854 	struct scsi_pkt *pkt = acp->pkt;
1855 	struct aac_srb_reply *resp;
1856 	uint32_t resp_status;
1857 
1858 	ASSERT(!(acp->flags & AAC_CMD_SYNC));
1859 	ASSERT(!(acp->flags & AAC_CMD_NO_CB));
1860 
1861 	resp = (struct aac_srb_reply *)&fibp->data[0];
1862 	resp_status = ddi_get32(acc, &resp->status);
1863 
1864 	/* First check FIB status */
1865 	if (resp_status == ST_OK) {
1866 		uint32_t scsi_status;
1867 		uint32_t srb_status;
1868 		uint32_t data_xfer_length;
1869 
1870 		scsi_status = ddi_get32(acc, &resp->scsi_status);
1871 		srb_status = ddi_get32(acc, &resp->srb_status);
1872 		data_xfer_length = ddi_get32(acc, &resp->data_xfer_length);
1873 
1874 		*pkt->pkt_scbp = (uint8_t)scsi_status;
1875 		pkt->pkt_state |= STATE_GOT_STATUS;
1876 		if (scsi_status == STATUS_GOOD) {
1877 			uchar_t cmd = ((union scsi_cdb *)(void *)
1878 			    (pkt->pkt_cdbp))->scc_cmd;
1879 
1880 			/* Next check SRB status */
1881 			switch (srb_status & 0x3f) {
1882 			case SRB_STATUS_DATA_OVERRUN:
1883 				AACDB_PRINT(softs, CE_NOTE, "DATA_OVERRUN: " \
1884 				    "scmd=%d, xfer=%d, buflen=%d",
1885 				    (uint32_t)cmd, data_xfer_length,
1886 				    acp->bcount);
1887 
1888 				switch (cmd) {
1889 				case SCMD_READ:
1890 				case SCMD_WRITE:
1891 				case SCMD_READ_G1:
1892 				case SCMD_WRITE_G1:
1893 				case SCMD_READ_G4:
1894 				case SCMD_WRITE_G4:
1895 				case SCMD_READ_G5:
1896 				case SCMD_WRITE_G5:
1897 					aac_set_pkt_reason(softs, acp,
1898 					    CMD_DATA_OVR, 0);
1899 					break;
1900 				}
1901 				/*FALLTHRU*/
1902 			case SRB_STATUS_ERROR_RECOVERY:
1903 			case SRB_STATUS_PENDING:
1904 			case SRB_STATUS_SUCCESS:
1905 				/*
1906 				 * pkt_resid should only be calculated if the
1907 				 * status is ERROR_RECOVERY/PENDING/SUCCESS/
1908 				 * OVERRUN/UNDERRUN
1909 				 */
1910 				if (data_xfer_length) {
1911 					pkt->pkt_state |= STATE_XFERRED_DATA;
1912 					pkt->pkt_resid = acp->bcount - \
1913 					    data_xfer_length;
1914 					ASSERT(pkt->pkt_resid >= 0);
1915 				}
1916 				break;
1917 			case SRB_STATUS_ABORTED:
1918 				AACDB_PRINT(softs, CE_NOTE,
1919 				    "SRB_STATUS_ABORTED, xfer=%d, resid=%d",
1920 				    data_xfer_length, pkt->pkt_resid);
1921 				aac_set_pkt_reason(softs, acp, CMD_ABORTED,
1922 				    STAT_ABORTED);
1923 				break;
1924 			case SRB_STATUS_ABORT_FAILED:
1925 				AACDB_PRINT(softs, CE_NOTE,
1926 				    "SRB_STATUS_ABORT_FAILED, xfer=%d, " \
1927 				    "resid=%d", data_xfer_length,
1928 				    pkt->pkt_resid);
1929 				aac_set_pkt_reason(softs, acp, CMD_ABORT_FAIL,
1930 				    0);
1931 				break;
1932 			case SRB_STATUS_PARITY_ERROR:
1933 				AACDB_PRINT(softs, CE_NOTE,
1934 				    "SRB_STATUS_PARITY_ERROR, xfer=%d, " \
1935 				    "resid=%d", data_xfer_length,
1936 				    pkt->pkt_resid);
1937 				aac_set_pkt_reason(softs, acp, CMD_PER_FAIL, 0);
1938 				break;
1939 			case SRB_STATUS_NO_DEVICE:
1940 			case SRB_STATUS_INVALID_PATH_ID:
1941 			case SRB_STATUS_INVALID_TARGET_ID:
1942 			case SRB_STATUS_INVALID_LUN:
1943 			case SRB_STATUS_SELECTION_TIMEOUT:
1944 #ifdef DEBUG
1945 				if (AAC_DEV_IS_VALID(acp->dvp)) {
1946 					AACDB_PRINT(softs, CE_NOTE,
1947 					    "SRB_STATUS_NO_DEVICE(%d), " \
1948 					    "xfer=%d, resid=%d ",
1949 					    srb_status & 0x3f,
1950 					    data_xfer_length, pkt->pkt_resid);
1951 				}
1952 #endif
1953 				aac_set_pkt_reason(softs, acp, CMD_DEV_GONE, 0);
1954 				break;
1955 			case SRB_STATUS_COMMAND_TIMEOUT:
1956 			case SRB_STATUS_TIMEOUT:
1957 				AACDB_PRINT(softs, CE_NOTE,
1958 				    "SRB_STATUS_COMMAND_TIMEOUT, xfer=%d, " \
1959 				    "resid=%d", data_xfer_length,
1960 				    pkt->pkt_resid);
1961 				aac_set_pkt_reason(softs, acp, CMD_TIMEOUT,
1962 				    STAT_TIMEOUT);
1963 				break;
1964 			case SRB_STATUS_BUS_RESET:
1965 				AACDB_PRINT(softs, CE_NOTE,
1966 				    "SRB_STATUS_BUS_RESET, xfer=%d, " \
1967 				    "resid=%d", data_xfer_length,
1968 				    pkt->pkt_resid);
1969 				aac_set_pkt_reason(softs, acp, CMD_RESET,
1970 				    STAT_BUS_RESET);
1971 				break;
1972 			default:
1973 				AACDB_PRINT(softs, CE_NOTE, "srb_status=%d, " \
1974 				    "xfer=%d, resid=%d", srb_status & 0x3f,
1975 				    data_xfer_length, pkt->pkt_resid);
1976 				aac_set_pkt_reason(softs, acp, CMD_TRAN_ERR, 0);
1977 				break;
1978 			}
1979 		} else if (scsi_status == STATUS_CHECK) {
1980 			/* CHECK CONDITION */
1981 			struct scsi_arq_status *arqstat =
1982 			    (void *)(pkt->pkt_scbp);
1983 			uint32_t sense_data_size;
1984 
1985 			pkt->pkt_state |= STATE_ARQ_DONE;
1986 
1987 			*(uint8_t *)&arqstat->sts_rqpkt_status = STATUS_GOOD;
1988 			arqstat->sts_rqpkt_reason = CMD_CMPLT;
1989 			arqstat->sts_rqpkt_resid = 0;
1990 			arqstat->sts_rqpkt_state =
1991 			    STATE_GOT_BUS |
1992 			    STATE_GOT_TARGET |
1993 			    STATE_SENT_CMD |
1994 			    STATE_XFERRED_DATA;
1995 			arqstat->sts_rqpkt_statistics = 0;
1996 
1997 			sense_data_size = ddi_get32(acc,
1998 			    &resp->sense_data_size);
1999 			ASSERT(sense_data_size <= AAC_SENSE_BUFFERSIZE);
2000 			AACDB_PRINT(softs, CE_NOTE,
2001 			    "CHECK CONDITION: sense len=%d, xfer len=%d",
2002 			    sense_data_size, data_xfer_length);
2003 
2004 			if (sense_data_size > SENSE_LENGTH)
2005 				sense_data_size = SENSE_LENGTH;
2006 			ddi_rep_get8(acc, (uint8_t *)&arqstat->sts_sensedata,
2007 			    (uint8_t *)resp->sense_data, sense_data_size,
2008 			    DDI_DEV_AUTOINCR);
2009 		} else {
2010 			AACDB_PRINT(softs, CE_WARN, "invaild scsi status: " \
2011 			    "scsi_status=%d, srb_status=%d",
2012 			    scsi_status, srb_status);
2013 			aac_set_pkt_reason(softs, acp, CMD_TRAN_ERR, 0);
2014 		}
2015 	} else {
2016 		AACDB_PRINT(softs, CE_NOTE, "SRB failed: fib status %d",
2017 		    resp_status);
2018 		aac_set_pkt_reason(softs, acp, CMD_TRAN_ERR, 0);
2019 	}
2020 }
2021 
2022 /*
2023  * Handle completed IOCTL command
2024  */
2025 /*ARGSUSED*/
2026 void
2027 aac_ioctl_complete(struct aac_softstate *softs, struct aac_cmd *acp)
2028 {
2029 	struct aac_slot *slotp = acp->slotp;
2030 
2031 	/*
2032 	 * NOTE: Both aac_ioctl_send_fib() and aac_send_raw_srb()
2033 	 * may wait on softs->event, so use cv_broadcast() instead
2034 	 * of cv_signal().
2035 	 */
2036 	ASSERT(acp->flags & AAC_CMD_SYNC);
2037 	ASSERT(acp->flags & AAC_CMD_NO_CB);
2038 
2039 	/* Get the size of the response FIB from its FIB.Header.Size field */
2040 	acp->fib_size = ddi_get16(slotp->fib_acc_handle,
2041 	    &slotp->fibp->Header.Size);
2042 
2043 	ASSERT(acp->fib_size <= softs->aac_max_fib_size);
2044 	ddi_rep_get8(slotp->fib_acc_handle, (uint8_t *)acp->fibp,
2045 	    (uint8_t *)slotp->fibp, acp->fib_size, DDI_DEV_AUTOINCR);
2046 }
2047 
2048 /*
2049  * Handle completed sync fib command
2050  */
2051 /*ARGSUSED*/
2052 void
2053 aac_sync_complete(struct aac_softstate *softs, struct aac_cmd *acp)
2054 {
2055 }
2056 
2057 /*
2058  * Handle completed Flush command
2059  */
2060 /*ARGSUSED*/
2061 static void
2062 aac_synccache_complete(struct aac_softstate *softs, struct aac_cmd *acp)
2063 {
2064 	struct aac_slot *slotp = acp->slotp;
2065 	ddi_acc_handle_t acc = slotp->fib_acc_handle;
2066 	struct aac_synchronize_reply *resp;
2067 	uint32_t status;
2068 
2069 	ASSERT(!(acp->flags & AAC_CMD_SYNC));
2070 
2071 	acp->pkt->pkt_state |= STATE_GOT_STATUS;
2072 
2073 	resp = (struct aac_synchronize_reply *)&slotp->fibp->data[0];
2074 	status = ddi_get32(acc, &resp->Status);
2075 	if (status != CT_OK)
2076 		aac_set_arq_data_hwerr(acp);
2077 }
2078 
2079 /*ARGSUSED*/
2080 static void
2081 aac_startstop_complete(struct aac_softstate *softs, struct aac_cmd *acp)
2082 {
2083 	struct aac_slot *slotp = acp->slotp;
2084 	ddi_acc_handle_t acc = slotp->fib_acc_handle;
2085 	struct aac_Container_resp *resp;
2086 	uint32_t status;
2087 
2088 	ASSERT(!(acp->flags & AAC_CMD_SYNC));
2089 
2090 	acp->pkt->pkt_state |= STATE_GOT_STATUS;
2091 
2092 	resp = (struct aac_Container_resp *)&slotp->fibp->data[0];
2093 	status = ddi_get32(acc, &resp->Status);
2094 	if (status != 0) {
2095 		AACDB_PRINT(softs, CE_WARN, "Cannot start/stop a unit");
2096 		aac_set_arq_data_hwerr(acp);
2097 	}
2098 }
2099 
2100 /*
2101  * Access PCI space to see if the driver can support the card
2102  */
2103 static int
2104 aac_check_card_type(struct aac_softstate *softs)
2105 {
2106 	ddi_acc_handle_t pci_config_handle;
2107 	int card_index;
2108 	uint32_t pci_cmd;
2109 
2110 	/* Map pci configuration space */
2111 	if ((pci_config_setup(softs->devinfo_p, &pci_config_handle)) !=
2112 	    DDI_SUCCESS) {
2113 		AACDB_PRINT(softs, CE_WARN, "Cannot setup pci config space");
2114 		return (AACERR);
2115 	}
2116 
2117 	softs->vendid = pci_config_get16(pci_config_handle, PCI_CONF_VENID);
2118 	softs->devid = pci_config_get16(pci_config_handle, PCI_CONF_DEVID);
2119 	softs->subvendid = pci_config_get16(pci_config_handle,
2120 	    PCI_CONF_SUBVENID);
2121 	softs->subsysid = pci_config_get16(pci_config_handle,
2122 	    PCI_CONF_SUBSYSID);
2123 
2124 	card_index = 0;
2125 	while (!CARD_IS_UNKNOWN(card_index)) {
2126 		if ((aac_cards[card_index].vendor == softs->vendid) &&
2127 		    (aac_cards[card_index].device == softs->devid) &&
2128 		    (aac_cards[card_index].subvendor == softs->subvendid) &&
2129 		    (aac_cards[card_index].subsys == softs->subsysid)) {
2130 			break;
2131 		}
2132 		card_index++;
2133 	}
2134 
2135 	softs->card = card_index;
2136 	softs->hwif = aac_cards[card_index].hwif;
2137 
2138 	/*
2139 	 * Unknown aac card
2140 	 * do a generic match based on the VendorID and DeviceID to
2141 	 * support the new cards in the aac family
2142 	 */
2143 	if (CARD_IS_UNKNOWN(card_index)) {
2144 		if (softs->vendid != 0x9005) {
2145 			AACDB_PRINT(softs, CE_WARN,
2146 			    "Unknown vendor 0x%x", softs->vendid);
2147 			goto error;
2148 		}
2149 		switch (softs->devid) {
2150 		case 0x285:
2151 			softs->hwif = AAC_HWIF_I960RX;
2152 			break;
2153 		case 0x286:
2154 			softs->hwif = AAC_HWIF_RKT;
2155 			break;
2156 		default:
2157 			AACDB_PRINT(softs, CE_WARN,
2158 			    "Unknown device \"pci9005,%x\"", softs->devid);
2159 			goto error;
2160 		}
2161 	}
2162 
2163 	/* Set hardware dependent interface */
2164 	switch (softs->hwif) {
2165 	case AAC_HWIF_I960RX:
2166 		softs->aac_if = aac_rx_interface;
2167 		softs->map_size_min = AAC_MAP_SIZE_MIN_RX;
2168 		break;
2169 	case AAC_HWIF_RKT:
2170 		softs->aac_if = aac_rkt_interface;
2171 		softs->map_size_min = AAC_MAP_SIZE_MIN_RKT;
2172 		break;
2173 	default:
2174 		AACDB_PRINT(softs, CE_WARN,
2175 		    "Unknown hardware interface %d", softs->hwif);
2176 		goto error;
2177 	}
2178 
2179 	/* Set card names */
2180 	(void *)strncpy(softs->vendor_name, aac_cards[card_index].vid,
2181 	    AAC_VENDOR_LEN);
2182 	(void *)strncpy(softs->product_name, aac_cards[card_index].desc,
2183 	    AAC_PRODUCT_LEN);
2184 
2185 	/* Set up quirks */
2186 	softs->flags = aac_cards[card_index].quirks;
2187 
2188 	/* Force the busmaster enable bit on */
2189 	pci_cmd = pci_config_get16(pci_config_handle, PCI_CONF_COMM);
2190 	if ((pci_cmd & PCI_COMM_ME) == 0) {
2191 		pci_cmd |= PCI_COMM_ME;
2192 		pci_config_put16(pci_config_handle, PCI_CONF_COMM, pci_cmd);
2193 		pci_cmd = pci_config_get16(pci_config_handle, PCI_CONF_COMM);
2194 		if ((pci_cmd & PCI_COMM_ME) == 0) {
2195 			cmn_err(CE_CONT, "?Cannot enable busmaster bit");
2196 			goto error;
2197 		}
2198 	}
2199 
2200 	/* Set memory base to map */
2201 	softs->pci_mem_base_paddr = 0xfffffff0UL & \
2202 	    pci_config_get32(pci_config_handle, PCI_CONF_BASE0);
2203 
2204 	pci_config_teardown(&pci_config_handle);
2205 
2206 	return (AACOK); /* card type detected */
2207 error:
2208 	pci_config_teardown(&pci_config_handle);
2209 	return (AACERR); /* no matched card found */
2210 }
2211 
2212 /*
2213  * Do the usual interrupt handler setup stuff.
2214  */
2215 static int
2216 aac_register_intrs(struct aac_softstate *softs)
2217 {
2218 	dev_info_t *dip;
2219 	int intr_types;
2220 
2221 	ASSERT(softs->devinfo_p);
2222 	dip = softs->devinfo_p;
2223 
2224 	/* Get the type of device intrrupts */
2225 	if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
2226 		AACDB_PRINT(softs, CE_WARN,
2227 		    "ddi_intr_get_supported_types() failed");
2228 		return (AACERR);
2229 	}
2230 	AACDB_PRINT(softs, CE_NOTE,
2231 	    "ddi_intr_get_supported_types() ret: 0x%x", intr_types);
2232 
2233 	/* Query interrupt, and alloc/init all needed struct */
2234 	if (intr_types & DDI_INTR_TYPE_MSI) {
2235 		if (aac_query_intrs(softs, DDI_INTR_TYPE_MSI)
2236 		    != DDI_SUCCESS) {
2237 			AACDB_PRINT(softs, CE_WARN,
2238 			    "MSI interrupt query failed");
2239 			return (AACERR);
2240 		}
2241 		softs->intr_type = DDI_INTR_TYPE_MSI;
2242 	} else if (intr_types & DDI_INTR_TYPE_FIXED) {
2243 		if (aac_query_intrs(softs, DDI_INTR_TYPE_FIXED)
2244 		    != DDI_SUCCESS) {
2245 			AACDB_PRINT(softs, CE_WARN,
2246 			    "FIXED interrupt query failed");
2247 			return (AACERR);
2248 		}
2249 		softs->intr_type = DDI_INTR_TYPE_FIXED;
2250 	} else {
2251 		AACDB_PRINT(softs, CE_WARN,
2252 		    "Device cannot suppport both FIXED and MSI interrupts");
2253 		return (AACERR);
2254 	}
2255 
2256 	/* Connect interrupt handlers */
2257 	if (aac_add_intrs(softs) != DDI_SUCCESS) {
2258 		AACDB_PRINT(softs, CE_WARN,
2259 		    "Interrupt registration failed, intr type: %s",
2260 		    softs->intr_type == DDI_INTR_TYPE_MSI ? "MSI" : "FIXED");
2261 		return (AACERR);
2262 	}
2263 	(void) aac_enable_intrs(softs);
2264 
2265 	if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &softs->softint_id,
2266 	    NULL, NULL, aac_softintr, (caddr_t)softs) != DDI_SUCCESS) {
2267 		AACDB_PRINT(softs, CE_WARN,
2268 		    "Can not setup soft interrupt handler!");
2269 		aac_remove_intrs(softs);
2270 		return (AACERR);
2271 	}
2272 
2273 	return (AACOK);
2274 }
2275 
2276 static void
2277 aac_unregister_intrs(struct aac_softstate *softs)
2278 {
2279 	aac_remove_intrs(softs);
2280 	ddi_remove_softintr(softs->softint_id);
2281 }
2282 
2283 /*
2284  * Check the firmware to determine the features to support and the FIB
2285  * parameters to use.
2286  */
2287 static int
2288 aac_check_firmware(struct aac_softstate *softs)
2289 {
2290 	uint32_t options;
2291 	uint32_t atu_size;
2292 	ddi_acc_handle_t pci_handle;
2293 	uint8_t *data;
2294 	uint32_t max_fibs;
2295 	uint32_t max_fib_size;
2296 	uint32_t sg_tablesize;
2297 	uint32_t max_sectors;
2298 	uint32_t status;
2299 
2300 	/* Get supported options */
2301 	if ((aac_sync_mbcommand(softs, AAC_MONKER_GETINFO, 0, 0, 0, 0,
2302 	    &status)) != AACOK) {
2303 		if (status != SRB_STATUS_INVALID_REQUEST) {
2304 			cmn_err(CE_CONT,
2305 			    "?Fatal error: request adapter info error");
2306 			return (AACERR);
2307 		}
2308 		options = 0;
2309 		atu_size = 0;
2310 	} else {
2311 		options = AAC_MAILBOX_GET(softs, 1);
2312 		atu_size = AAC_MAILBOX_GET(softs, 2);
2313 	}
2314 
2315 	if (softs->state & AAC_STATE_RESET) {
2316 		if ((softs->support_opt == options) &&
2317 		    (softs->atu_size == atu_size))
2318 			return (AACOK);
2319 
2320 		cmn_err(CE_WARN,
2321 		    "?Fatal error: firmware changed, system needs reboot");
2322 		return (AACERR);
2323 	}
2324 
2325 	/*
2326 	 * The following critical settings are initialized only once during
2327 	 * driver attachment.
2328 	 */
2329 	softs->support_opt = options;
2330 	softs->atu_size = atu_size;
2331 
2332 	/* Process supported options */
2333 	if ((options & AAC_SUPPORTED_4GB_WINDOW) != 0 &&
2334 	    (softs->flags & AAC_FLAGS_NO4GB) == 0) {
2335 		AACDB_PRINT(softs, CE_NOTE, "!Enable FIB map 4GB window");
2336 		softs->flags |= AAC_FLAGS_4GB_WINDOW;
2337 	} else {
2338 		/*
2339 		 * Quirk AAC_FLAGS_NO4GB is for FIB address and thus comm space
2340 		 * only. IO is handled by the DMA engine which does not suffer
2341 		 * from the ATU window programming workarounds necessary for
2342 		 * CPU copy operations.
2343 		 */
2344 		softs->addr_dma_attr.dma_attr_addr_lo = 0x2000ull;
2345 		softs->addr_dma_attr.dma_attr_addr_hi = 0x7fffffffull;
2346 	}
2347 
2348 	if ((options & AAC_SUPPORTED_SGMAP_HOST64) != 0) {
2349 		AACDB_PRINT(softs, CE_NOTE, "!Enable SG map 64-bit address");
2350 		softs->buf_dma_attr.dma_attr_addr_hi = 0xffffffffffffffffull;
2351 		softs->buf_dma_attr.dma_attr_seg = 0xffffffffffffffffull;
2352 		softs->flags |= AAC_FLAGS_SG_64BIT;
2353 	}
2354 
2355 	if (options & AAC_SUPPORTED_64BIT_ARRAYSIZE) {
2356 		softs->flags |= AAC_FLAGS_ARRAY_64BIT;
2357 		AACDB_PRINT(softs, CE_NOTE, "!Enable 64-bit array size");
2358 	}
2359 
2360 	if (options & AAC_SUPPORTED_NONDASD) {
2361 		if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, softs->devinfo_p, 0,
2362 		    "nondasd-enable", (char **)&data) == DDI_SUCCESS)) {
2363 			if (strcmp((char *)data, "yes") == 0) {
2364 				AACDB_PRINT(softs, CE_NOTE,
2365 				    "!Enable Non-DASD access");
2366 				softs->flags |= AAC_FLAGS_NONDASD;
2367 			}
2368 			ddi_prop_free(data);
2369 		}
2370 	}
2371 
2372 	/* Read preferred settings */
2373 	max_fib_size = 0;
2374 	if ((aac_sync_mbcommand(softs, AAC_MONKER_GETCOMMPREF,
2375 	    0, 0, 0, 0, NULL)) == AACOK) {
2376 		options = AAC_MAILBOX_GET(softs, 1);
2377 		max_fib_size = (options & 0xffff);
2378 		max_sectors = (options >> 16) << 1;
2379 		options = AAC_MAILBOX_GET(softs, 2);
2380 		sg_tablesize = (options >> 16);
2381 		options = AAC_MAILBOX_GET(softs, 3);
2382 		max_fibs = (options & 0xffff);
2383 	}
2384 
2385 	/* Enable new comm. and rawio at the same time */
2386 	if ((softs->support_opt & AAC_SUPPORTED_NEW_COMM) &&
2387 	    (max_fib_size != 0)) {
2388 		/* read out and save PCI MBR */
2389 		if ((atu_size > softs->map_size) &&
2390 		    (ddi_regs_map_setup(softs->devinfo_p, 1,
2391 		    (caddr_t *)&data, 0, atu_size, &softs->reg_attr,
2392 		    &pci_handle) == DDI_SUCCESS)) {
2393 			ddi_regs_map_free(&softs->pci_mem_handle);
2394 			softs->pci_mem_handle = pci_handle;
2395 			softs->pci_mem_base_vaddr = data;
2396 			softs->map_size = atu_size;
2397 		}
2398 		if (atu_size == softs->map_size) {
2399 			softs->flags |= AAC_FLAGS_NEW_COMM;
2400 			AACDB_PRINT(softs, CE_NOTE,
2401 			    "!Enable New Comm. interface");
2402 		}
2403 	}
2404 
2405 	/* Set FIB parameters */
2406 	if (softs->flags & AAC_FLAGS_NEW_COMM) {
2407 		softs->aac_max_fibs = max_fibs;
2408 		softs->aac_max_fib_size = max_fib_size;
2409 		softs->aac_max_sectors = max_sectors;
2410 		softs->aac_sg_tablesize = sg_tablesize;
2411 
2412 		softs->flags |= AAC_FLAGS_RAW_IO;
2413 		AACDB_PRINT(softs, CE_NOTE, "!Enable RawIO");
2414 	} else {
2415 		softs->aac_max_fibs =
2416 		    (softs->flags & AAC_FLAGS_256FIBS) ? 256 : 512;
2417 		softs->aac_max_fib_size = AAC_FIB_SIZE;
2418 		softs->aac_max_sectors = 128;	/* 64K */
2419 		if (softs->flags & AAC_FLAGS_17SG)
2420 			softs->aac_sg_tablesize = 17;
2421 		else if (softs->flags & AAC_FLAGS_34SG)
2422 			softs->aac_sg_tablesize = 34;
2423 		else if (softs->flags & AAC_FLAGS_SG_64BIT)
2424 			softs->aac_sg_tablesize = (AAC_FIB_DATASIZE -
2425 			    sizeof (struct aac_blockwrite64) +
2426 			    sizeof (struct aac_sg_entry64)) /
2427 			    sizeof (struct aac_sg_entry64);
2428 		else
2429 			softs->aac_sg_tablesize = (AAC_FIB_DATASIZE -
2430 			    sizeof (struct aac_blockwrite) +
2431 			    sizeof (struct aac_sg_entry)) /
2432 			    sizeof (struct aac_sg_entry);
2433 	}
2434 
2435 	if ((softs->flags & AAC_FLAGS_RAW_IO) &&
2436 	    (softs->flags & AAC_FLAGS_ARRAY_64BIT)) {
2437 		softs->flags |= AAC_FLAGS_LBA_64BIT;
2438 		AACDB_PRINT(softs, CE_NOTE, "!Enable 64-bit array");
2439 	}
2440 	softs->buf_dma_attr.dma_attr_sgllen = softs->aac_sg_tablesize;
2441 	softs->buf_dma_attr.dma_attr_maxxfer = softs->aac_max_sectors << 9;
2442 	/*
2443 	 * 64K maximum segment size in scatter gather list is controlled by
2444 	 * the NEW_COMM bit in the adapter information. If not set, the card
2445 	 * can only accept a maximum of 64K. It is not recommended to permit
2446 	 * more than 128KB of total transfer size to the adapters because
2447 	 * performance is negatively impacted.
2448 	 *
2449 	 * For new comm, segment size equals max xfer size. For old comm,
2450 	 * we use 64K for both.
2451 	 */
2452 	softs->buf_dma_attr.dma_attr_count_max =
2453 	    softs->buf_dma_attr.dma_attr_maxxfer - 1;
2454 
2455 	/* Setup FIB operations */
2456 	if (softs->flags & AAC_FLAGS_RAW_IO)
2457 		softs->aac_cmd_fib = aac_cmd_fib_rawio;
2458 	else if (softs->flags & AAC_FLAGS_SG_64BIT)
2459 		softs->aac_cmd_fib = aac_cmd_fib_brw64;
2460 	else
2461 		softs->aac_cmd_fib = aac_cmd_fib_brw;
2462 	softs->aac_cmd_fib_scsi = (softs->flags & AAC_FLAGS_SG_64BIT) ? \
2463 	    aac_cmd_fib_scsi64 : aac_cmd_fib_scsi32;
2464 
2465 	/* 64-bit LBA needs descriptor format sense data */
2466 	softs->slen = sizeof (struct scsi_arq_status);
2467 	if ((softs->flags & AAC_FLAGS_LBA_64BIT) &&
2468 	    softs->slen < AAC_ARQ64_LENGTH)
2469 		softs->slen = AAC_ARQ64_LENGTH;
2470 
2471 	AACDB_PRINT(softs, CE_NOTE,
2472 	    "!max_fibs %d max_fibsize 0x%x max_sectors %d max_sg %d",
2473 	    softs->aac_max_fibs, softs->aac_max_fib_size,
2474 	    softs->aac_max_sectors, softs->aac_sg_tablesize);
2475 
2476 	return (AACOK);
2477 }
2478 
2479 static void
2480 aac_fsa_rev(struct aac_softstate *softs, struct FsaRev *fsarev0,
2481     struct FsaRev *fsarev1)
2482 {
2483 	ddi_acc_handle_t acc = softs->sync_ac.slotp->fib_acc_handle;
2484 
2485 	AAC_GET_FIELD8(acc, fsarev1, fsarev0, external.comp.dash);
2486 	AAC_GET_FIELD8(acc, fsarev1, fsarev0, external.comp.type);
2487 	AAC_GET_FIELD8(acc, fsarev1, fsarev0, external.comp.minor);
2488 	AAC_GET_FIELD8(acc, fsarev1, fsarev0, external.comp.major);
2489 	AAC_GET_FIELD32(acc, fsarev1, fsarev0, buildNumber);
2490 }
2491 
2492 /*
2493  * The following function comes from Adaptec:
2494  *
2495  * Query adapter information and supplement adapter information
2496  */
2497 static int
2498 aac_get_adapter_info(struct aac_softstate *softs,
2499     struct aac_adapter_info *ainfr, struct aac_supplement_adapter_info *sinfr)
2500 {
2501 	struct aac_cmd *acp = &softs->sync_ac;
2502 	ddi_acc_handle_t acc;
2503 	struct aac_fib *fibp;
2504 	struct aac_adapter_info *ainfp;
2505 	struct aac_supplement_adapter_info *sinfp;
2506 	int rval;
2507 
2508 	(void) aac_sync_fib_slot_bind(softs, acp);
2509 	acc = acp->slotp->fib_acc_handle;
2510 	fibp = acp->slotp->fibp;
2511 
2512 	ddi_put8(acc, &fibp->data[0], 0);
2513 	if (aac_sync_fib(softs, RequestAdapterInfo,
2514 	    sizeof (struct aac_fib_header)) != AACOK) {
2515 		AACDB_PRINT(softs, CE_WARN, "RequestAdapterInfo failed");
2516 		rval = AACERR;
2517 		goto finish;
2518 	}
2519 	ainfp = (struct aac_adapter_info *)fibp->data;
2520 	if (ainfr) {
2521 		AAC_GET_FIELD32(acc, ainfr, ainfp, SupportedOptions);
2522 		AAC_GET_FIELD32(acc, ainfr, ainfp, PlatformBase);
2523 		AAC_GET_FIELD32(acc, ainfr, ainfp, CpuArchitecture);
2524 		AAC_GET_FIELD32(acc, ainfr, ainfp, CpuVariant);
2525 		AAC_GET_FIELD32(acc, ainfr, ainfp, ClockSpeed);
2526 		AAC_GET_FIELD32(acc, ainfr, ainfp, ExecutionMem);
2527 		AAC_GET_FIELD32(acc, ainfr, ainfp, BufferMem);
2528 		AAC_GET_FIELD32(acc, ainfr, ainfp, TotalMem);
2529 		aac_fsa_rev(softs, &ainfp->KernelRevision,
2530 		    &ainfr->KernelRevision);
2531 		aac_fsa_rev(softs, &ainfp->MonitorRevision,
2532 		    &ainfr->MonitorRevision);
2533 		aac_fsa_rev(softs, &ainfp->HardwareRevision,
2534 		    &ainfr->HardwareRevision);
2535 		aac_fsa_rev(softs, &ainfp->BIOSRevision,
2536 		    &ainfr->BIOSRevision);
2537 		AAC_GET_FIELD32(acc, ainfr, ainfp, ClusteringEnabled);
2538 		AAC_GET_FIELD32(acc, ainfr, ainfp, ClusterChannelMask);
2539 		AAC_GET_FIELD64(acc, ainfr, ainfp, SerialNumber);
2540 		AAC_GET_FIELD32(acc, ainfr, ainfp, batteryPlatform);
2541 		AAC_GET_FIELD32(acc, ainfr, ainfp, SupportedOptions);
2542 		AAC_GET_FIELD32(acc, ainfr, ainfp, OemVariant);
2543 	}
2544 	if (sinfr) {
2545 		if (!(softs->support_opt &
2546 		    AAC_SUPPORTED_SUPPLEMENT_ADAPTER_INFO)) {
2547 			AACDB_PRINT(softs, CE_WARN,
2548 			    "SupplementAdapterInfo not supported");
2549 			rval = AACERR;
2550 			goto finish;
2551 		}
2552 		ddi_put8(acc, &fibp->data[0], 0);
2553 		if (aac_sync_fib(softs, RequestSupplementAdapterInfo,
2554 		    sizeof (struct aac_fib_header)) != AACOK) {
2555 			AACDB_PRINT(softs, CE_WARN,
2556 			    "RequestSupplementAdapterInfo failed");
2557 			rval = AACERR;
2558 			goto finish;
2559 		}
2560 		sinfp = (struct aac_supplement_adapter_info *)fibp->data;
2561 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, AdapterTypeText[0], 17+1);
2562 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, Pad[0], 2);
2563 		AAC_GET_FIELD32(acc, sinfr, sinfp, FlashMemoryByteSize);
2564 		AAC_GET_FIELD32(acc, sinfr, sinfp, FlashImageId);
2565 		AAC_GET_FIELD32(acc, sinfr, sinfp, MaxNumberPorts);
2566 		AAC_GET_FIELD32(acc, sinfr, sinfp, Version);
2567 		AAC_GET_FIELD32(acc, sinfr, sinfp, FeatureBits);
2568 		AAC_GET_FIELD8(acc, sinfr, sinfp, SlotNumber);
2569 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, ReservedPad0[0], 3);
2570 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, BuildDate[0], 12);
2571 		AAC_GET_FIELD32(acc, sinfr, sinfp, CurrentNumberPorts);
2572 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, VpdInfo,
2573 		    sizeof (struct vpd_info));
2574 		aac_fsa_rev(softs, &sinfp->FlashFirmwareRevision,
2575 		    &sinfr->FlashFirmwareRevision);
2576 		AAC_GET_FIELD32(acc, sinfr, sinfp, RaidTypeMorphOptions);
2577 		aac_fsa_rev(softs, &sinfp->FlashFirmwareBootRevision,
2578 		    &sinfr->FlashFirmwareBootRevision);
2579 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, MfgPcbaSerialNo,
2580 		    MFG_PCBA_SERIAL_NUMBER_WIDTH);
2581 		AAC_REP_GET_FIELD8(acc, sinfr, sinfp, MfgWWNName[0],
2582 		    MFG_WWN_WIDTH);
2583 		AAC_GET_FIELD32(acc, sinfr, sinfp, SupportedOptions2);
2584 		AAC_GET_FIELD32(acc, sinfr, sinfp, ExpansionFlag);
2585 		if (sinfr->ExpansionFlag == 1) {
2586 			AAC_GET_FIELD32(acc, sinfr, sinfp, FeatureBits3);
2587 			AAC_GET_FIELD32(acc, sinfr, sinfp,
2588 			    SupportedPerformanceMode);
2589 			AAC_REP_GET_FIELD32(acc, sinfr, sinfp,
2590 			    ReservedGrowth[0], 80);
2591 		}
2592 	}
2593 	rval = AACOK;
2594 finish:
2595 	aac_sync_fib_slot_release(softs, acp);
2596 	return (rval);
2597 }
2598 
2599 static int
2600 aac_get_bus_info(struct aac_softstate *softs, uint32_t *bus_max,
2601     uint32_t *tgt_max)
2602 {
2603 	struct aac_cmd *acp = &softs->sync_ac;
2604 	ddi_acc_handle_t acc;
2605 	struct aac_fib *fibp;
2606 	struct aac_ctcfg *c_cmd;
2607 	struct aac_ctcfg_resp *c_resp;
2608 	uint32_t scsi_method_id;
2609 	struct aac_bus_info *cmd;
2610 	struct aac_bus_info_response *resp;
2611 	int rval;
2612 
2613 	(void) aac_sync_fib_slot_bind(softs, acp);
2614 	acc = acp->slotp->fib_acc_handle;
2615 	fibp = acp->slotp->fibp;
2616 
2617 	/* Detect MethodId */
2618 	c_cmd = (struct aac_ctcfg *)&fibp->data[0];
2619 	ddi_put32(acc, &c_cmd->Command, VM_ContainerConfig);
2620 	ddi_put32(acc, &c_cmd->cmd, CT_GET_SCSI_METHOD);
2621 	ddi_put32(acc, &c_cmd->param, 0);
2622 	rval = aac_sync_fib(softs, ContainerCommand,
2623 	    AAC_FIB_SIZEOF(struct aac_ctcfg));
2624 	c_resp = (struct aac_ctcfg_resp *)&fibp->data[0];
2625 	if (rval != AACOK || ddi_get32(acc, &c_resp->Status) != 0) {
2626 		AACDB_PRINT(softs, CE_WARN,
2627 		    "VM_ContainerConfig command fail");
2628 		rval = AACERR;
2629 		goto finish;
2630 	}
2631 	scsi_method_id = ddi_get32(acc, &c_resp->param);
2632 
2633 	/* Detect phys. bus count and max. target id first */
2634 	cmd = (struct aac_bus_info *)&fibp->data[0];
2635 	ddi_put32(acc, &cmd->Command, VM_Ioctl);
2636 	ddi_put32(acc, &cmd->ObjType, FT_DRIVE); /* physical drive */
2637 	ddi_put32(acc, &cmd->MethodId, scsi_method_id);
2638 	ddi_put32(acc, &cmd->ObjectId, 0);
2639 	ddi_put32(acc, &cmd->CtlCmd, GetBusInfo);
2640 	/*
2641 	 * For VM_Ioctl, the firmware uses the Header.Size filled from the
2642 	 * driver as the size to be returned. Therefore the driver has to use
2643 	 * sizeof (struct aac_bus_info_response) because it is greater than
2644 	 * sizeof (struct aac_bus_info).
2645 	 */
2646 	rval = aac_sync_fib(softs, ContainerCommand,
2647 	    AAC_FIB_SIZEOF(struct aac_bus_info_response));
2648 	resp = (struct aac_bus_info_response *)cmd;
2649 
2650 	/* Scan all coordinates with INQUIRY */
2651 	if ((rval != AACOK) || (ddi_get32(acc, &resp->Status) != 0)) {
2652 		AACDB_PRINT(softs, CE_WARN, "GetBusInfo command fail");
2653 		rval = AACERR;
2654 		goto finish;
2655 	}
2656 	*bus_max = ddi_get32(acc, &resp->BusCount);
2657 	*tgt_max = ddi_get32(acc, &resp->TargetsPerBus);
2658 
2659 finish:
2660 	aac_sync_fib_slot_release(softs, acp);
2661 	return (AACOK);
2662 }
2663 
2664 /*
2665  * The following function comes from Adaptec:
2666  *
2667  * Routine to be called during initialization of communications with
2668  * the adapter to handle possible adapter configuration issues. When
2669  * the adapter first boots up, it examines attached drives, etc, and
2670  * potentially comes up with a new or revised configuration (relative to
2671  * what's stored in it's NVRAM). Additionally it may discover problems
2672  * that make the current physical configuration unworkable (currently
2673  * applicable only to cluster configuration issues).
2674  *
2675  * If there are no configuration issues or the issues are considered
2676  * trival by the adapter, it will set it's configuration status to
2677  * "FSACT_CONTINUE" and execute the "commit confiuguration" action
2678  * automatically on it's own.
2679  *
2680  * However, if there are non-trivial issues, the adapter will set it's
2681  * internal configuration status to "FSACT_PAUSE" or "FASCT_ABORT"
2682  * and wait for some agent on the host to issue the "\ContainerCommand
2683  * \VM_ContainerConfig\CT_COMMIT_CONFIG" FIB command to cause the
2684  * adapter to commit the new/updated configuration and enable
2685  * un-inhibited operation.  The host agent should first issue the
2686  * "\ContainerCommand\VM_ContainerConfig\CT_GET_CONFIG_STATUS" FIB
2687  * command to obtain information about config issues detected by
2688  * the adapter.
2689  *
2690  * Normally the adapter's PC BIOS will execute on the host following
2691  * adapter poweron and reset and will be responsible for querring the
2692  * adapter with CT_GET_CONFIG_STATUS and issuing the CT_COMMIT_CONFIG
2693  * command if appropriate.
2694  *
2695  * However, with the introduction of IOP reset support, the adapter may
2696  * boot up without the benefit of the adapter's PC BIOS host agent.
2697  * This routine is intended to take care of these issues in situations
2698  * where BIOS doesn't execute following adapter poweron or reset.  The
2699  * CT_COMMIT_CONFIG command is a no-op if it's already been issued, so
2700  * there is no harm in doing this when it's already been done.
2701  */
2702 static int
2703 aac_handle_adapter_config_issues(struct aac_softstate *softs)
2704 {
2705 	struct aac_cmd *acp = &softs->sync_ac;
2706 	ddi_acc_handle_t acc;
2707 	struct aac_fib *fibp;
2708 	struct aac_Container *cmd;
2709 	struct aac_Container_resp *resp;
2710 	struct aac_cf_status_header *cfg_sts_hdr;
2711 	uint32_t resp_status;
2712 	uint32_t ct_status;
2713 	uint32_t cfg_stat_action;
2714 	int rval;
2715 
2716 	(void) aac_sync_fib_slot_bind(softs, acp);
2717 	acc = acp->slotp->fib_acc_handle;
2718 	fibp = acp->slotp->fibp;
2719 
2720 	/* Get adapter config status */
2721 	cmd = (struct aac_Container *)&fibp->data[0];
2722 
2723 	bzero(cmd, sizeof (*cmd) - CT_PACKET_SIZE);
2724 	ddi_put32(acc, &cmd->Command, VM_ContainerConfig);
2725 	ddi_put32(acc, &cmd->CTCommand.command, CT_GET_CONFIG_STATUS);
2726 	ddi_put32(acc, &cmd->CTCommand.param[CNT_SIZE],
2727 	    sizeof (struct aac_cf_status_header));
2728 	rval = aac_sync_fib(softs, ContainerCommand,
2729 	    AAC_FIB_SIZEOF(struct aac_Container));
2730 	resp = (struct aac_Container_resp *)cmd;
2731 	cfg_sts_hdr = (struct aac_cf_status_header *)resp->CTResponse.data;
2732 
2733 	resp_status = ddi_get32(acc, &resp->Status);
2734 	ct_status = ddi_get32(acc, &resp->CTResponse.param[0]);
2735 	if ((rval == AACOK) && (resp_status == 0) && (ct_status == CT_OK)) {
2736 		cfg_stat_action = ddi_get32(acc, &cfg_sts_hdr->action);
2737 
2738 		/* Commit configuration if it's reasonable to do so. */
2739 		if (cfg_stat_action <= CFACT_PAUSE) {
2740 			bzero(cmd, sizeof (*cmd) - CT_PACKET_SIZE);
2741 			ddi_put32(acc, &cmd->Command, VM_ContainerConfig);
2742 			ddi_put32(acc, &cmd->CTCommand.command,
2743 			    CT_COMMIT_CONFIG);
2744 			rval = aac_sync_fib(softs, ContainerCommand,
2745 			    AAC_FIB_SIZEOF(struct aac_Container));
2746 
2747 			resp_status = ddi_get32(acc, &resp->Status);
2748 			ct_status = ddi_get32(acc, &resp->CTResponse.param[0]);
2749 			if ((rval == AACOK) && (resp_status == 0) &&
2750 			    (ct_status == CT_OK))
2751 				/* Successful completion */
2752 				rval = AACMPE_OK;
2753 			else
2754 				/* Auto-commit aborted due to error(s). */
2755 				rval = AACMPE_COMMIT_CONFIG;
2756 		} else {
2757 			/*
2758 			 * Auto-commit aborted due to adapter indicating
2759 			 * configuration issue(s) too dangerous to auto-commit.
2760 			 */
2761 			rval = AACMPE_CONFIG_STATUS;
2762 		}
2763 	} else {
2764 		cmn_err(CE_WARN, "!Configuration issue, auto-commit aborted");
2765 		rval = AACMPE_CONFIG_STATUS;
2766 	}
2767 
2768 	aac_sync_fib_slot_release(softs, acp);
2769 	return (rval);
2770 }
2771 
2772 /*
2773  * Hardware initialization and resource allocation
2774  */
2775 static int
2776 aac_common_attach(struct aac_softstate *softs)
2777 {
2778 	uint32_t status;
2779 	int i;
2780 	struct aac_supplement_adapter_info sinf;
2781 
2782 	DBCALLED(softs, 1);
2783 
2784 	/*
2785 	 * Do a little check here to make sure there aren't any outstanding
2786 	 * FIBs in the message queue. At this point there should not be and
2787 	 * if there are they are probably left over from another instance of
2788 	 * the driver like when the system crashes and the crash dump driver
2789 	 * gets loaded.
2790 	 */
2791 	while (AAC_OUTB_GET(softs) != 0xfffffffful)
2792 		;
2793 
2794 	/*
2795 	 * Wait the card to complete booting up before do anything that
2796 	 * attempts to communicate with it.
2797 	 */
2798 	status = AAC_FWSTATUS_GET(softs);
2799 	if (status == AAC_SELF_TEST_FAILED || status == AAC_KERNEL_PANIC)
2800 		goto error;
2801 	i = AAC_FWUP_TIMEOUT * 1000; /* set timeout */
2802 	AAC_BUSYWAIT(AAC_FWSTATUS_GET(softs) & AAC_KERNEL_UP_AND_RUNNING, i);
2803 	if (i == 0) {
2804 		cmn_err(CE_CONT, "?Fatal error: controller not ready");
2805 		aac_fm_ereport(softs, DDI_FM_DEVICE_NO_RESPONSE);
2806 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
2807 		goto error;
2808 	}
2809 
2810 	/* Read and set card supported options and settings */
2811 	if (aac_check_firmware(softs) == AACERR) {
2812 		aac_fm_ereport(softs, DDI_FM_DEVICE_NO_RESPONSE);
2813 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
2814 		goto error;
2815 	}
2816 
2817 	/* Add interrupt handlers */
2818 	if (aac_register_intrs(softs) == AACERR) {
2819 		cmn_err(CE_CONT,
2820 		    "?Fatal error: interrupts register failed");
2821 		goto error;
2822 	}
2823 
2824 	/* Setup communication space with the card */
2825 	if (softs->comm_space_dma_handle == NULL) {
2826 		if (aac_alloc_comm_space(softs) != AACOK)
2827 			goto error;
2828 	}
2829 	if (aac_setup_comm_space(softs) != AACOK) {
2830 		cmn_err(CE_CONT, "?Setup communication space failed");
2831 		aac_fm_ereport(softs, DDI_FM_DEVICE_NO_RESPONSE);
2832 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
2833 		goto error;
2834 	}
2835 
2836 #ifdef DEBUG
2837 	if (aac_get_fw_debug_buffer(softs) != AACOK)
2838 		cmn_err(CE_CONT, "?firmware UART trace not supported");
2839 #endif
2840 
2841 	/* Allocate slots */
2842 	if ((softs->total_slots == 0) && (aac_create_slots(softs) != AACOK)) {
2843 		cmn_err(CE_CONT, "?Fatal error: slots allocate failed");
2844 		goto error;
2845 	}
2846 	AACDB_PRINT(softs, CE_NOTE, "%d slots allocated", softs->total_slots);
2847 
2848 	/* Allocate FIBs */
2849 	if (softs->total_fibs < softs->total_slots) {
2850 		aac_alloc_fibs(softs);
2851 		if (softs->total_fibs == 0)
2852 			goto error;
2853 		AACDB_PRINT(softs, CE_NOTE, "%d fibs allocated",
2854 		    softs->total_fibs);
2855 	}
2856 
2857 	AAC_STATUS_CLR(softs, ~0); /* Clear out all interrupts */
2858 	AAC_ENABLE_INTR(softs); /* Enable the interrupts we can handle */
2859 
2860 	if (aac_get_adapter_info(softs, NULL, &sinf) == AACOK) {
2861 		/* Get adapter names */
2862 		if (CARD_IS_UNKNOWN(softs->card)) {
2863 
2864 			softs->feature_bits = sinf.FeatureBits;
2865 			softs->support_opt2 = sinf.SupportedOptions2;
2866 
2867 			char *p, *p0, *p1;
2868 
2869 			/*
2870 			 * Now find the controller name in supp_adapter_info->
2871 			 * AdapterTypeText. Use the first word as the vendor
2872 			 * and the other words as the product name.
2873 			 */
2874 			AACDB_PRINT(softs, CE_NOTE, "sinf.AdapterTypeText = "
2875 			    "\"%s\"", sinf.AdapterTypeText);
2876 			p = sinf.AdapterTypeText;
2877 			p0 = p1 = NULL;
2878 			/* Skip heading spaces */
2879 			while (*p && (*p == ' ' || *p == '\t'))
2880 				p++;
2881 			p0 = p;
2882 			while (*p && (*p != ' ' && *p != '\t'))
2883 				p++;
2884 			/* Remove middle spaces */
2885 			while (*p && (*p == ' ' || *p == '\t'))
2886 				*p++ = 0;
2887 			p1 = p;
2888 			/* Remove trailing spaces */
2889 			p = p1 + strlen(p1) - 1;
2890 			while (p > p1 && (*p == ' ' || *p == '\t'))
2891 				*p-- = 0;
2892 			if (*p0 && *p1) {
2893 				(void *)strncpy(softs->vendor_name, p0,
2894 				    AAC_VENDOR_LEN);
2895 				(void *)strncpy(softs->product_name, p1,
2896 				    AAC_PRODUCT_LEN);
2897 			} else {
2898 				cmn_err(CE_WARN,
2899 				    "?adapter name mis-formatted\n");
2900 				if (*p0)
2901 					(void *)strncpy(softs->product_name,
2902 					    p0, AAC_PRODUCT_LEN);
2903 			}
2904 		}
2905 	} else {
2906 		cmn_err(CE_CONT, "?Query adapter information failed");
2907 	}
2908 
2909 
2910 	cmn_err(CE_NOTE,
2911 	    "!aac driver %d.%02d.%02d-%d, found card: " \
2912 	    "%s %s(pci0x%x.%x.%x.%x) at 0x%x",
2913 	    AAC_DRIVER_MAJOR_VERSION,
2914 	    AAC_DRIVER_MINOR_VERSION,
2915 	    AAC_DRIVER_BUGFIX_LEVEL,
2916 	    AAC_DRIVER_BUILD,
2917 	    softs->vendor_name, softs->product_name,
2918 	    softs->vendid, softs->devid, softs->subvendid, softs->subsysid,
2919 	    softs->pci_mem_base_paddr);
2920 
2921 	/* Perform acceptance of adapter-detected config changes if possible */
2922 	if (aac_handle_adapter_config_issues(softs) != AACMPE_OK) {
2923 		cmn_err(CE_CONT, "?Handle adapter config issues failed");
2924 		aac_fm_ereport(softs, DDI_FM_DEVICE_NO_RESPONSE);
2925 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
2926 		goto error;
2927 	}
2928 
2929 	/* Setup containers (logical devices) */
2930 	if (aac_probe_containers(softs) != AACOK) {
2931 		cmn_err(CE_CONT, "?Fatal error: get container info error");
2932 		goto error;
2933 	}
2934 
2935 	/* Setup phys. devices */
2936 	if (softs->flags & AAC_FLAGS_NONDASD) {
2937 		uint32_t bus_max, tgt_max;
2938 		uint32_t bus, tgt;
2939 		int index;
2940 
2941 		if (aac_get_bus_info(softs, &bus_max, &tgt_max) != AACOK) {
2942 			cmn_err(CE_CONT, "?Fatal error: get bus info error");
2943 			goto error;
2944 		}
2945 		AACDB_PRINT(softs, CE_NOTE, "bus_max=%d, tgt_max=%d",
2946 		    bus_max, tgt_max);
2947 		if (bus_max != softs->bus_max || tgt_max != softs->tgt_max) {
2948 			if (softs->state & AAC_STATE_RESET) {
2949 				cmn_err(CE_WARN,
2950 				    "?Fatal error: bus map changed");
2951 				goto error;
2952 			}
2953 			softs->bus_max = bus_max;
2954 			softs->tgt_max = tgt_max;
2955 			if (softs->nondasds) {
2956 				kmem_free(softs->nondasds, AAC_MAX_PD(softs) * \
2957 				    sizeof (struct aac_nondasd));
2958 			}
2959 			softs->nondasds = kmem_zalloc(AAC_MAX_PD(softs) * \
2960 			    sizeof (struct aac_nondasd), KM_SLEEP);
2961 
2962 			index = 0;
2963 			for (bus = 0; bus < softs->bus_max; bus++) {
2964 				for (tgt = 0; tgt < softs->tgt_max; tgt++) {
2965 					struct aac_nondasd *dvp =
2966 					    &softs->nondasds[index++];
2967 					dvp->dev.type = AAC_DEV_PD;
2968 					dvp->bus = bus;
2969 					dvp->tid = tgt;
2970 				}
2971 			}
2972 		}
2973 	}
2974 
2975 	/* Check dma & acc handles allocated in attach */
2976 	if (aac_check_dma_handle(softs->comm_space_dma_handle) != DDI_SUCCESS) {
2977 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
2978 		goto error;
2979 	}
2980 
2981 	if (aac_check_acc_handle(softs->pci_mem_handle) != DDI_SUCCESS) {
2982 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
2983 		goto error;
2984 	}
2985 
2986 	for (i = 0; i < softs->total_slots; i++) {
2987 		if (aac_check_dma_handle(softs->io_slot[i].fib_dma_handle) !=
2988 		    DDI_SUCCESS) {
2989 			ddi_fm_service_impact(softs->devinfo_p,
2990 			    DDI_SERVICE_LOST);
2991 			goto error;
2992 		}
2993 	}
2994 
2995 	return (AACOK);
2996 error:
2997 	if (softs->state & AAC_STATE_RESET)
2998 		return (AACERR);
2999 	if (softs->nondasds) {
3000 		kmem_free(softs->nondasds, AAC_MAX_PD(softs) * \
3001 		    sizeof (struct aac_nondasd));
3002 		softs->nondasds = NULL;
3003 	}
3004 	if (softs->total_fibs > 0)
3005 		aac_destroy_fibs(softs);
3006 	if (softs->total_slots > 0)
3007 		aac_destroy_slots(softs);
3008 	if (softs->comm_space_dma_handle)
3009 		aac_free_comm_space(softs);
3010 	return (AACERR);
3011 }
3012 
3013 /*
3014  * Hardware shutdown and resource release
3015  */
3016 static void
3017 aac_common_detach(struct aac_softstate *softs)
3018 {
3019 	DBCALLED(softs, 1);
3020 
3021 	aac_unregister_intrs(softs);
3022 
3023 	mutex_enter(&softs->io_lock);
3024 	(void) aac_shutdown(softs);
3025 
3026 	if (softs->nondasds) {
3027 		kmem_free(softs->nondasds, AAC_MAX_PD(softs) * \
3028 		    sizeof (struct aac_nondasd));
3029 		softs->nondasds = NULL;
3030 	}
3031 	aac_destroy_fibs(softs);
3032 	aac_destroy_slots(softs);
3033 	aac_free_comm_space(softs);
3034 	mutex_exit(&softs->io_lock);
3035 }
3036 
3037 /*
3038  * Send a synchronous command to the controller and wait for a result.
3039  * Indicate if the controller completed the command with an error status.
3040  */
3041 int
3042 aac_sync_mbcommand(struct aac_softstate *softs, uint32_t cmd,
3043     uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3,
3044     uint32_t *statusp)
3045 {
3046 	int timeout;
3047 	uint32_t status;
3048 
3049 	if (statusp != NULL)
3050 		*statusp = SRB_STATUS_SUCCESS;
3051 
3052 	/* Fill in mailbox */
3053 	AAC_MAILBOX_SET(softs, cmd, arg0, arg1, arg2, arg3);
3054 
3055 	/* Ensure the sync command doorbell flag is cleared */
3056 	AAC_STATUS_CLR(softs, AAC_DB_SYNC_COMMAND);
3057 
3058 	/* Then set it to signal the adapter */
3059 	AAC_NOTIFY(softs, AAC_DB_SYNC_COMMAND);
3060 
3061 	/* Spin waiting for the command to complete */
3062 	timeout = AAC_IMMEDIATE_TIMEOUT * 1000;
3063 	AAC_BUSYWAIT(AAC_STATUS_GET(softs) & AAC_DB_SYNC_COMMAND, timeout);
3064 	if (!timeout) {
3065 		AACDB_PRINT(softs, CE_WARN,
3066 		    "Sync command timed out after %d seconds (0x%x)!",
3067 		    AAC_IMMEDIATE_TIMEOUT, AAC_FWSTATUS_GET(softs));
3068 		return (AACERR);
3069 	}
3070 
3071 	/* Clear the completion flag */
3072 	AAC_STATUS_CLR(softs, AAC_DB_SYNC_COMMAND);
3073 
3074 	/* Get the command status */
3075 	status = AAC_MAILBOX_GET(softs, 0);
3076 	if (statusp != NULL)
3077 		*statusp = status;
3078 	if (status != SRB_STATUS_SUCCESS) {
3079 		AACDB_PRINT(softs, CE_WARN,
3080 		    "Sync command fail: status = 0x%x", status);
3081 		return (AACERR);
3082 	}
3083 
3084 	return (AACOK);
3085 }
3086 
3087 /*
3088  * Send a synchronous FIB to the adapter and wait for its completion
3089  */
3090 static int
3091 aac_sync_fib(struct aac_softstate *softs, uint16_t cmd, uint16_t fibsize)
3092 {
3093 	struct aac_cmd *acp = &softs->sync_ac;
3094 
3095 	acp->flags = AAC_CMD_SYNC | AAC_CMD_IN_SYNC_SLOT;
3096 	if (softs->state & AAC_STATE_INTR)
3097 		acp->flags |= AAC_CMD_NO_CB;
3098 	else
3099 		acp->flags |= AAC_CMD_NO_INTR;
3100 
3101 	acp->ac_comp = aac_sync_complete;
3102 	acp->timeout = AAC_SYNC_TIMEOUT;
3103 	acp->fib_size = fibsize;
3104 
3105 	/*
3106 	 * Only need to setup sync fib header, caller should have init
3107 	 * fib data
3108 	 */
3109 	aac_cmd_fib_header(softs, acp, cmd);
3110 
3111 	(void) ddi_dma_sync(acp->slotp->fib_dma_handle, 0, fibsize,
3112 	    DDI_DMA_SYNC_FORDEV);
3113 
3114 	aac_start_io(softs, acp);
3115 
3116 	if (softs->state & AAC_STATE_INTR)
3117 		return (aac_do_sync_io(softs, acp));
3118 	else
3119 		return (aac_do_poll_io(softs, acp));
3120 }
3121 
3122 static void
3123 aac_cmd_initq(struct aac_cmd_queue *q)
3124 {
3125 	q->q_head = NULL;
3126 	q->q_tail = (struct aac_cmd *)&q->q_head;
3127 }
3128 
3129 /*
3130  * Remove a cmd from the head of q
3131  */
3132 static struct aac_cmd *
3133 aac_cmd_dequeue(struct aac_cmd_queue *q)
3134 {
3135 	struct aac_cmd *acp;
3136 
3137 	_NOTE(ASSUMING_PROTECTED(*q))
3138 
3139 	if ((acp = q->q_head) != NULL) {
3140 		if ((q->q_head = acp->next) != NULL)
3141 			acp->next = NULL;
3142 		else
3143 			q->q_tail = (struct aac_cmd *)&q->q_head;
3144 		acp->prev = NULL;
3145 	}
3146 	return (acp);
3147 }
3148 
3149 /*
3150  * Add a cmd to the tail of q
3151  */
3152 static void
3153 aac_cmd_enqueue(struct aac_cmd_queue *q, struct aac_cmd *acp)
3154 {
3155 	ASSERT(acp->next == NULL);
3156 	acp->prev = q->q_tail;
3157 	q->q_tail->next = acp;
3158 	q->q_tail = acp;
3159 }
3160 
3161 /*
3162  * Remove the cmd ac from q
3163  */
3164 static void
3165 aac_cmd_delete(struct aac_cmd_queue *q, struct aac_cmd *acp)
3166 {
3167 	if (acp->prev) {
3168 		if ((acp->prev->next = acp->next) != NULL) {
3169 			acp->next->prev = acp->prev;
3170 			acp->next = NULL;
3171 		} else {
3172 			q->q_tail = acp->prev;
3173 		}
3174 		acp->prev = NULL;
3175 	}
3176 	/* ac is not in the queue */
3177 }
3178 
3179 /*
3180  * Atomically insert an entry into the nominated queue, returns 0 on success or
3181  * AACERR if the queue is full.
3182  *
3183  * Note: it would be more efficient to defer notifying the controller in
3184  *	 the case where we may be inserting several entries in rapid succession,
3185  *	 but implementing this usefully may be difficult (it would involve a
3186  *	 separate queue/notify interface).
3187  */
3188 static int
3189 aac_fib_enqueue(struct aac_softstate *softs, int queue, uint32_t fib_addr,
3190     uint32_t fib_size)
3191 {
3192 	ddi_dma_handle_t dma = softs->comm_space_dma_handle;
3193 	ddi_acc_handle_t acc = softs->comm_space_acc_handle;
3194 	uint32_t pi, ci;
3195 
3196 	DBCALLED(softs, 2);
3197 
3198 	ASSERT(queue == AAC_ADAP_NORM_CMD_Q || queue == AAC_ADAP_NORM_RESP_Q);
3199 
3200 	/* Get the producer/consumer indices */
3201 	(void) ddi_dma_sync(dma, (uintptr_t)softs->qtablep->qt_qindex[queue] - \
3202 	    (uintptr_t)softs->comm_space, sizeof (uint32_t) * 2,
3203 	    DDI_DMA_SYNC_FORCPU);
3204 	if (aac_check_dma_handle(dma) != DDI_SUCCESS) {
3205 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_UNAFFECTED);
3206 		return (AACERR);
3207 	}
3208 
3209 	pi = ddi_get32(acc,
3210 	    &softs->qtablep->qt_qindex[queue][AAC_PRODUCER_INDEX]);
3211 	ci = ddi_get32(acc,
3212 	    &softs->qtablep->qt_qindex[queue][AAC_CONSUMER_INDEX]);
3213 
3214 	/*
3215 	 * Wrap the queue first before we check the queue to see
3216 	 * if it is full
3217 	 */
3218 	if (pi >= aac_qinfo[queue].size)
3219 		pi = 0;
3220 
3221 	/* XXX queue full */
3222 	if ((pi + 1) == ci)
3223 		return (AACERR);
3224 
3225 	/* Fill in queue entry */
3226 	ddi_put32(acc, &((softs->qentries[queue] + pi)->aq_fib_size), fib_size);
3227 	ddi_put32(acc, &((softs->qentries[queue] + pi)->aq_fib_addr), fib_addr);
3228 	(void) ddi_dma_sync(dma, (uintptr_t)(softs->qentries[queue] + pi) - \
3229 	    (uintptr_t)softs->comm_space, sizeof (struct aac_queue_entry),
3230 	    DDI_DMA_SYNC_FORDEV);
3231 
3232 	/* Update producer index */
3233 	ddi_put32(acc, &softs->qtablep->qt_qindex[queue][AAC_PRODUCER_INDEX],
3234 	    pi + 1);
3235 	(void) ddi_dma_sync(dma,
3236 	    (uintptr_t)&softs->qtablep->qt_qindex[queue][AAC_PRODUCER_INDEX] - \
3237 	    (uintptr_t)softs->comm_space, sizeof (uint32_t),
3238 	    DDI_DMA_SYNC_FORDEV);
3239 
3240 	if (aac_qinfo[queue].notify != 0)
3241 		AAC_NOTIFY(softs, aac_qinfo[queue].notify);
3242 	return (AACOK);
3243 }
3244 
3245 /*
3246  * Atomically remove one entry from the nominated queue, returns 0 on
3247  * success or AACERR if the queue is empty.
3248  */
3249 static int
3250 aac_fib_dequeue(struct aac_softstate *softs, int queue, int *idxp)
3251 {
3252 	ddi_acc_handle_t acc = softs->comm_space_acc_handle;
3253 	ddi_dma_handle_t dma = softs->comm_space_dma_handle;
3254 	uint32_t pi, ci;
3255 	int unfull = 0;
3256 
3257 	DBCALLED(softs, 2);
3258 
3259 	ASSERT(idxp);
3260 
3261 	/* Get the producer/consumer indices */
3262 	(void) ddi_dma_sync(dma, (uintptr_t)softs->qtablep->qt_qindex[queue] - \
3263 	    (uintptr_t)softs->comm_space, sizeof (uint32_t) * 2,
3264 	    DDI_DMA_SYNC_FORCPU);
3265 	pi = ddi_get32(acc,
3266 	    &softs->qtablep->qt_qindex[queue][AAC_PRODUCER_INDEX]);
3267 	ci = ddi_get32(acc,
3268 	    &softs->qtablep->qt_qindex[queue][AAC_CONSUMER_INDEX]);
3269 
3270 	/* Check for queue empty */
3271 	if (ci == pi)
3272 		return (AACERR);
3273 
3274 	if (pi >= aac_qinfo[queue].size)
3275 		pi = 0;
3276 
3277 	/* Check for queue full */
3278 	if (ci == pi + 1)
3279 		unfull = 1;
3280 
3281 	/*
3282 	 * The controller does not wrap the queue,
3283 	 * so we have to do it by ourselves
3284 	 */
3285 	if (ci >= aac_qinfo[queue].size)
3286 		ci = 0;
3287 
3288 	/* Fetch the entry */
3289 	(void) ddi_dma_sync(dma, (uintptr_t)(softs->qentries[queue] + pi) - \
3290 	    (uintptr_t)softs->comm_space, sizeof (struct aac_queue_entry),
3291 	    DDI_DMA_SYNC_FORCPU);
3292 	if (aac_check_dma_handle(dma) != DDI_SUCCESS) {
3293 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_UNAFFECTED);
3294 		return (AACERR);
3295 	}
3296 
3297 	switch (queue) {
3298 	case AAC_HOST_NORM_RESP_Q:
3299 	case AAC_HOST_HIGH_RESP_Q:
3300 		*idxp = ddi_get32(acc,
3301 		    &(softs->qentries[queue] + ci)->aq_fib_addr);
3302 		break;
3303 
3304 	case AAC_HOST_NORM_CMD_Q:
3305 	case AAC_HOST_HIGH_CMD_Q:
3306 		*idxp = ddi_get32(acc,
3307 		    &(softs->qentries[queue] + ci)->aq_fib_addr) / AAC_FIB_SIZE;
3308 		break;
3309 
3310 	default:
3311 		cmn_err(CE_NOTE, "!Invalid queue in aac_fib_dequeue()");
3312 		return (AACERR);
3313 	}
3314 
3315 	/* Update consumer index */
3316 	ddi_put32(acc, &softs->qtablep->qt_qindex[queue][AAC_CONSUMER_INDEX],
3317 	    ci + 1);
3318 	(void) ddi_dma_sync(dma,
3319 	    (uintptr_t)&softs->qtablep->qt_qindex[queue][AAC_CONSUMER_INDEX] - \
3320 	    (uintptr_t)softs->comm_space, sizeof (uint32_t),
3321 	    DDI_DMA_SYNC_FORDEV);
3322 
3323 	if (unfull && aac_qinfo[queue].notify != 0)
3324 		AAC_NOTIFY(softs, aac_qinfo[queue].notify);
3325 	return (AACOK);
3326 }
3327 
3328 static struct aac_mntinforesp *
3329 aac_get_mntinfo(struct aac_softstate *softs, int cid)
3330 {
3331 	ddi_acc_handle_t acc = softs->sync_ac.slotp->fib_acc_handle;
3332 	struct aac_fib *fibp = softs->sync_ac.slotp->fibp;
3333 	struct aac_mntinfo *mi = (struct aac_mntinfo *)&fibp->data[0];
3334 	struct aac_mntinforesp *mir;
3335 
3336 	ddi_put32(acc, &mi->Command, /* Use 64-bit LBA if enabled */
3337 	    (softs->flags & AAC_FLAGS_LBA_64BIT) ?
3338 	    VM_NameServe64 : VM_NameServe);
3339 	ddi_put32(acc, &mi->MntType, FT_FILESYS);
3340 	ddi_put32(acc, &mi->MntCount, cid);
3341 
3342 	if (aac_sync_fib(softs, ContainerCommand,
3343 	    AAC_FIB_SIZEOF(struct aac_mntinfo)) == AACERR) {
3344 		AACDB_PRINT(softs, CE_WARN, "Error probe container %d", cid);
3345 		return (NULL);
3346 	}
3347 
3348 	mir = (struct aac_mntinforesp *)&fibp->data[0];
3349 	if (ddi_get32(acc, &mir->Status) == ST_OK)
3350 		return (mir);
3351 	return (NULL);
3352 }
3353 
3354 static int
3355 aac_get_container_count(struct aac_softstate *softs, int *count)
3356 {
3357 	ddi_acc_handle_t acc;
3358 	struct aac_mntinforesp *mir;
3359 	int rval;
3360 
3361 	(void) aac_sync_fib_slot_bind(softs, &softs->sync_ac);
3362 	acc = softs->sync_ac.slotp->fib_acc_handle;
3363 
3364 	if ((mir = aac_get_mntinfo(softs, 0)) == NULL) {
3365 		rval = AACERR;
3366 		goto finish;
3367 	}
3368 	*count = ddi_get32(acc, &mir->MntRespCount);
3369 	if (*count > AAC_MAX_LD) {
3370 		AACDB_PRINT(softs, CE_CONT,
3371 		    "container count(%d) > AAC_MAX_LD", *count);
3372 		rval = AACERR;
3373 		goto finish;
3374 	}
3375 	rval = AACOK;
3376 
3377 finish:
3378 	aac_sync_fib_slot_release(softs, &softs->sync_ac);
3379 	return (rval);
3380 }
3381 
3382 static int
3383 aac_get_container_uid(struct aac_softstate *softs, uint32_t cid, uint32_t *uid)
3384 {
3385 	ddi_acc_handle_t acc = softs->sync_ac.slotp->fib_acc_handle;
3386 	struct aac_Container *ct = (struct aac_Container *) \
3387 	    &softs->sync_ac.slotp->fibp->data[0];
3388 
3389 	bzero(ct, sizeof (*ct) - CT_PACKET_SIZE);
3390 	ddi_put32(acc, &ct->Command, VM_ContainerConfig);
3391 	ddi_put32(acc, &ct->CTCommand.command, CT_CID_TO_32BITS_UID);
3392 	ddi_put32(acc, &ct->CTCommand.param[0], cid);
3393 
3394 	if (aac_sync_fib(softs, ContainerCommand,
3395 	    AAC_FIB_SIZEOF(struct aac_Container)) == AACERR)
3396 		return (AACERR);
3397 	if (ddi_get32(acc, &ct->CTCommand.param[0]) != CT_OK)
3398 		return (AACERR);
3399 
3400 	*uid = ddi_get32(acc, &ct->CTCommand.param[1]);
3401 	return (AACOK);
3402 }
3403 
3404 /*
3405  * Request information of the container cid
3406  */
3407 static struct aac_mntinforesp *
3408 aac_get_container_info(struct aac_softstate *softs, int cid)
3409 {
3410 	ddi_acc_handle_t acc = softs->sync_ac.slotp->fib_acc_handle;
3411 	struct aac_mntinforesp *mir;
3412 	int rval_uid;
3413 	uint32_t uid;
3414 
3415 	/* Get container UID first so that it will not overwrite mntinfo */
3416 	rval_uid = aac_get_container_uid(softs, cid, &uid);
3417 
3418 	/* Get container basic info */
3419 	if ((mir = aac_get_mntinfo(softs, cid)) == NULL) {
3420 		AACDB_PRINT(softs, CE_CONT,
3421 		    "query container %d info failed", cid);
3422 		return (NULL);
3423 	}
3424 	if (ddi_get32(acc, &mir->MntObj.VolType) == CT_NONE)
3425 		return (mir);
3426 	if (rval_uid != AACOK) {
3427 		AACDB_PRINT(softs, CE_CONT,
3428 		    "query container %d uid failed", cid);
3429 		return (NULL);
3430 	}
3431 
3432 	ddi_put32(acc, &mir->Status, uid);
3433 	return (mir);
3434 }
3435 
3436 static enum aac_cfg_event
3437 aac_probe_container(struct aac_softstate *softs, uint32_t cid)
3438 {
3439 	enum aac_cfg_event event = AAC_CFG_NULL_NOEXIST;
3440 	struct aac_container *dvp = &softs->containers[cid];
3441 	struct aac_mntinforesp *mir;
3442 	ddi_acc_handle_t acc;
3443 
3444 	(void) aac_sync_fib_slot_bind(softs, &softs->sync_ac);
3445 	acc = softs->sync_ac.slotp->fib_acc_handle;
3446 
3447 	/* Get container basic info */
3448 	if ((mir = aac_get_container_info(softs, cid)) == NULL) {
3449 		/* AAC_CFG_NULL_NOEXIST */
3450 		goto finish;
3451 	}
3452 
3453 	if (ddi_get32(acc, &mir->MntObj.VolType) == CT_NONE) {
3454 		if (AAC_DEV_IS_VALID(&dvp->dev)) {
3455 			AACDB_PRINT(softs, CE_NOTE,
3456 			    ">>> Container %d deleted", cid);
3457 			dvp->dev.flags &= ~AAC_DFLAG_VALID;
3458 			event = AAC_CFG_DELETE;
3459 		}
3460 		/* AAC_CFG_NULL_NOEXIST */
3461 	} else {
3462 		uint64_t size;
3463 		uint32_t uid;
3464 
3465 		event = AAC_CFG_NULL_EXIST;
3466 
3467 		size = AAC_MIR_SIZE(softs, acc, mir);
3468 		uid = ddi_get32(acc, &mir->Status);
3469 		if (AAC_DEV_IS_VALID(&dvp->dev)) {
3470 			if (dvp->uid != uid) {
3471 				AACDB_PRINT(softs, CE_WARN,
3472 				    ">>> Container %u uid changed to %d",
3473 				    cid, uid);
3474 				dvp->uid = uid;
3475 				event = AAC_CFG_CHANGE;
3476 			}
3477 			if (dvp->size != size) {
3478 				AACDB_PRINT(softs, CE_NOTE,
3479 				    ">>> Container %u size changed to %"PRIu64,
3480 				    cid, size);
3481 				dvp->size = size;
3482 				event = AAC_CFG_CHANGE;
3483 			}
3484 		} else { /* Init new container */
3485 			AACDB_PRINT(softs, CE_NOTE,
3486 			    ">>> Container %d added: " \
3487 			    "size=0x%x.%08x, type=%d, name=%s",
3488 			    cid,
3489 			    ddi_get32(acc, &mir->MntObj.CapacityHigh),
3490 			    ddi_get32(acc, &mir->MntObj.Capacity),
3491 			    ddi_get32(acc, &mir->MntObj.VolType),
3492 			    mir->MntObj.FileSystemName);
3493 			dvp->dev.flags |= AAC_DFLAG_VALID;
3494 			dvp->dev.type = AAC_DEV_LD;
3495 
3496 			dvp->cid = cid;
3497 			dvp->uid = uid;
3498 			dvp->size = size;
3499 			dvp->locked = 0;
3500 			dvp->deleted = 0;
3501 
3502 			event = AAC_CFG_ADD;
3503 		}
3504 	}
3505 
3506 finish:
3507 	aac_sync_fib_slot_release(softs, &softs->sync_ac);
3508 	return (event);
3509 }
3510 
3511 /*
3512  * Do a rescan of all the possible containers and update the container list
3513  * with newly online/offline containers, and prepare for autoconfiguration.
3514  */
3515 static int
3516 aac_probe_containers(struct aac_softstate *softs)
3517 {
3518 	int i, count, total;
3519 
3520 	/* Loop over possible containers */
3521 	count = softs->container_count;
3522 	if (aac_get_container_count(softs, &count) == AACERR)
3523 		return (AACERR);
3524 
3525 	for (i = total = 0; i < count; i++) {
3526 		enum aac_cfg_event event = aac_probe_container(softs, i);
3527 		if ((event != AAC_CFG_NULL_NOEXIST) &&
3528 		    (event != AAC_CFG_NULL_EXIST)) {
3529 			(void) aac_handle_dr(softs, i, -1, event);
3530 			total++;
3531 		}
3532 	}
3533 
3534 	if (count < softs->container_count) {
3535 		struct aac_container *dvp;
3536 
3537 		for (dvp = &softs->containers[count];
3538 		    dvp < &softs->containers[softs->container_count]; dvp++) {
3539 			if (!AAC_DEV_IS_VALID(&dvp->dev))
3540 				continue;
3541 			AACDB_PRINT(softs, CE_NOTE, ">>> Container %d deleted",
3542 			    dvp->cid);
3543 			dvp->dev.flags &= ~AAC_DFLAG_VALID;
3544 			(void) aac_handle_dr(softs, dvp->cid, -1,
3545 			    AAC_CFG_DELETE);
3546 		}
3547 	}
3548 
3549 	softs->container_count = count;
3550 	AACDB_PRINT(softs, CE_CONT, "?Total %d container(s) found", total);
3551 	return (AACOK);
3552 }
3553 
3554 static int
3555 aac_alloc_comm_space(struct aac_softstate *softs)
3556 {
3557 	size_t rlen;
3558 	ddi_dma_cookie_t cookie;
3559 	uint_t cookien;
3560 
3561 	/* Allocate DMA for comm. space */
3562 	if (ddi_dma_alloc_handle(
3563 	    softs->devinfo_p,
3564 	    &softs->addr_dma_attr,
3565 	    DDI_DMA_SLEEP,
3566 	    NULL,
3567 	    &softs->comm_space_dma_handle) != DDI_SUCCESS) {
3568 		AACDB_PRINT(softs, CE_WARN,
3569 		    "Cannot alloc dma handle for communication area");
3570 		goto error;
3571 	}
3572 	if (ddi_dma_mem_alloc(
3573 	    softs->comm_space_dma_handle,
3574 	    sizeof (struct aac_comm_space),
3575 	    &softs->acc_attr,
3576 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
3577 	    DDI_DMA_SLEEP,
3578 	    NULL,
3579 	    (caddr_t *)&softs->comm_space,
3580 	    &rlen,
3581 	    &softs->comm_space_acc_handle) != DDI_SUCCESS) {
3582 		AACDB_PRINT(softs, CE_WARN,
3583 		    "Cannot alloc mem for communication area");
3584 		goto error;
3585 	}
3586 	if (ddi_dma_addr_bind_handle(
3587 	    softs->comm_space_dma_handle,
3588 	    NULL,
3589 	    (caddr_t)softs->comm_space,
3590 	    sizeof (struct aac_comm_space),
3591 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
3592 	    DDI_DMA_SLEEP,
3593 	    NULL,
3594 	    &cookie,
3595 	    &cookien) != DDI_DMA_MAPPED) {
3596 		AACDB_PRINT(softs, CE_WARN,
3597 		    "DMA bind failed for communication area");
3598 		goto error;
3599 	}
3600 	softs->comm_space_phyaddr = cookie.dmac_address;
3601 
3602 	return (AACOK);
3603 error:
3604 	if (softs->comm_space_acc_handle) {
3605 		ddi_dma_mem_free(&softs->comm_space_acc_handle);
3606 		softs->comm_space_acc_handle = NULL;
3607 	}
3608 	if (softs->comm_space_dma_handle) {
3609 		ddi_dma_free_handle(&softs->comm_space_dma_handle);
3610 		softs->comm_space_dma_handle = NULL;
3611 	}
3612 	return (AACERR);
3613 }
3614 
3615 static void
3616 aac_free_comm_space(struct aac_softstate *softs)
3617 {
3618 
3619 	(void) ddi_dma_unbind_handle(softs->comm_space_dma_handle);
3620 	ddi_dma_mem_free(&softs->comm_space_acc_handle);
3621 	softs->comm_space_acc_handle = NULL;
3622 	ddi_dma_free_handle(&softs->comm_space_dma_handle);
3623 	softs->comm_space_dma_handle = NULL;
3624 	softs->comm_space_phyaddr = NULL;
3625 }
3626 
3627 /*
3628  * Initialize the data structures that are required for the communication
3629  * interface to operate
3630  */
3631 static int
3632 aac_setup_comm_space(struct aac_softstate *softs)
3633 {
3634 	ddi_dma_handle_t dma = softs->comm_space_dma_handle;
3635 	ddi_acc_handle_t acc = softs->comm_space_acc_handle;
3636 	uint32_t comm_space_phyaddr;
3637 	struct aac_adapter_init *initp;
3638 	int qoffset;
3639 
3640 	comm_space_phyaddr = softs->comm_space_phyaddr;
3641 
3642 	/* Setup adapter init struct */
3643 	initp = &softs->comm_space->init_data;
3644 	bzero(initp, sizeof (struct aac_adapter_init));
3645 
3646 	ddi_put32(acc, &initp->InitStructRevision, AAC_INIT_STRUCT_REVISION);
3647 	ddi_put32(acc, &initp->HostElapsedSeconds, ddi_get_time());
3648 
3649 	/* Setup new/old comm. specific data */
3650 	if (softs->flags & AAC_FLAGS_RAW_IO) {
3651 		uint32_t init_flags = 0;
3652 
3653 		if (softs->flags & AAC_FLAGS_NEW_COMM)
3654 			init_flags |= AAC_INIT_FLAGS_NEW_COMM_SUPPORTED;
3655 		/* AAC_SUPPORTED_POWER_MANAGEMENT */
3656 		init_flags |= AAC_INIT_FLAGS_DRIVER_SUPPORTS_PM;
3657 		init_flags |= AAC_INIT_FLAGS_DRIVER_USES_UTC_TIME;
3658 
3659 		ddi_put32(acc, &initp->InitStructRevision,
3660 		    AAC_INIT_STRUCT_REVISION_4);
3661 		ddi_put32(acc, &initp->InitFlags, init_flags);
3662 		/* Setup the preferred settings */
3663 		ddi_put32(acc, &initp->MaxIoCommands, softs->aac_max_fibs);
3664 		ddi_put32(acc, &initp->MaxIoSize,
3665 		    (softs->aac_max_sectors << 9));
3666 		ddi_put32(acc, &initp->MaxFibSize, softs->aac_max_fib_size);
3667 	} else {
3668 		/*
3669 		 * Tells the adapter about the physical location of various
3670 		 * important shared data structures
3671 		 */
3672 		ddi_put32(acc, &initp->AdapterFibsPhysicalAddress,
3673 		    comm_space_phyaddr + \
3674 		    offsetof(struct aac_comm_space, adapter_fibs));
3675 		ddi_put32(acc, &initp->AdapterFibsVirtualAddress, 0);
3676 		ddi_put32(acc, &initp->AdapterFibAlign, AAC_FIB_SIZE);
3677 		ddi_put32(acc, &initp->AdapterFibsSize,
3678 		    AAC_ADAPTER_FIBS * AAC_FIB_SIZE);
3679 		ddi_put32(acc, &initp->PrintfBufferAddress,
3680 		    comm_space_phyaddr + \
3681 		    offsetof(struct aac_comm_space, adapter_print_buf));
3682 		ddi_put32(acc, &initp->PrintfBufferSize,
3683 		    AAC_ADAPTER_PRINT_BUFSIZE);
3684 		ddi_put32(acc, &initp->MiniPortRevision,
3685 		    AAC_INIT_STRUCT_MINIPORT_REVISION);
3686 		ddi_put32(acc, &initp->HostPhysMemPages, AAC_MAX_PFN);
3687 
3688 		qoffset = (comm_space_phyaddr + \
3689 		    offsetof(struct aac_comm_space, qtable)) % \
3690 		    AAC_QUEUE_ALIGN;
3691 		if (qoffset)
3692 			qoffset = AAC_QUEUE_ALIGN - qoffset;
3693 		softs->qtablep = (struct aac_queue_table *) \
3694 		    ((char *)&softs->comm_space->qtable + qoffset);
3695 		ddi_put32(acc, &initp->CommHeaderAddress, comm_space_phyaddr + \
3696 		    offsetof(struct aac_comm_space, qtable) + qoffset);
3697 
3698 		/* Init queue table */
3699 		ddi_put32(acc, &softs->qtablep-> \
3700 		    qt_qindex[AAC_HOST_NORM_CMD_Q][AAC_PRODUCER_INDEX],
3701 		    AAC_HOST_NORM_CMD_ENTRIES);
3702 		ddi_put32(acc, &softs->qtablep-> \
3703 		    qt_qindex[AAC_HOST_NORM_CMD_Q][AAC_CONSUMER_INDEX],
3704 		    AAC_HOST_NORM_CMD_ENTRIES);
3705 		ddi_put32(acc, &softs->qtablep-> \
3706 		    qt_qindex[AAC_HOST_HIGH_CMD_Q][AAC_PRODUCER_INDEX],
3707 		    AAC_HOST_HIGH_CMD_ENTRIES);
3708 		ddi_put32(acc, &softs->qtablep-> \
3709 		    qt_qindex[AAC_HOST_HIGH_CMD_Q][AAC_CONSUMER_INDEX],
3710 		    AAC_HOST_HIGH_CMD_ENTRIES);
3711 		ddi_put32(acc, &softs->qtablep-> \
3712 		    qt_qindex[AAC_ADAP_NORM_CMD_Q][AAC_PRODUCER_INDEX],
3713 		    AAC_ADAP_NORM_CMD_ENTRIES);
3714 		ddi_put32(acc, &softs->qtablep-> \
3715 		    qt_qindex[AAC_ADAP_NORM_CMD_Q][AAC_CONSUMER_INDEX],
3716 		    AAC_ADAP_NORM_CMD_ENTRIES);
3717 		ddi_put32(acc, &softs->qtablep-> \
3718 		    qt_qindex[AAC_ADAP_HIGH_CMD_Q][AAC_PRODUCER_INDEX],
3719 		    AAC_ADAP_HIGH_CMD_ENTRIES);
3720 		ddi_put32(acc, &softs->qtablep-> \
3721 		    qt_qindex[AAC_ADAP_HIGH_CMD_Q][AAC_CONSUMER_INDEX],
3722 		    AAC_ADAP_HIGH_CMD_ENTRIES);
3723 		ddi_put32(acc, &softs->qtablep-> \
3724 		    qt_qindex[AAC_HOST_NORM_RESP_Q][AAC_PRODUCER_INDEX],
3725 		    AAC_HOST_NORM_RESP_ENTRIES);
3726 		ddi_put32(acc, &softs->qtablep-> \
3727 		    qt_qindex[AAC_HOST_NORM_RESP_Q][AAC_CONSUMER_INDEX],
3728 		    AAC_HOST_NORM_RESP_ENTRIES);
3729 		ddi_put32(acc, &softs->qtablep-> \
3730 		    qt_qindex[AAC_HOST_HIGH_RESP_Q][AAC_PRODUCER_INDEX],
3731 		    AAC_HOST_HIGH_RESP_ENTRIES);
3732 		ddi_put32(acc, &softs->qtablep-> \
3733 		    qt_qindex[AAC_HOST_HIGH_RESP_Q][AAC_CONSUMER_INDEX],
3734 		    AAC_HOST_HIGH_RESP_ENTRIES);
3735 		ddi_put32(acc, &softs->qtablep-> \
3736 		    qt_qindex[AAC_ADAP_NORM_RESP_Q][AAC_PRODUCER_INDEX],
3737 		    AAC_ADAP_NORM_RESP_ENTRIES);
3738 		ddi_put32(acc, &softs->qtablep-> \
3739 		    qt_qindex[AAC_ADAP_NORM_RESP_Q][AAC_CONSUMER_INDEX],
3740 		    AAC_ADAP_NORM_RESP_ENTRIES);
3741 		ddi_put32(acc, &softs->qtablep-> \
3742 		    qt_qindex[AAC_ADAP_HIGH_RESP_Q][AAC_PRODUCER_INDEX],
3743 		    AAC_ADAP_HIGH_RESP_ENTRIES);
3744 		ddi_put32(acc, &softs->qtablep-> \
3745 		    qt_qindex[AAC_ADAP_HIGH_RESP_Q][AAC_CONSUMER_INDEX],
3746 		    AAC_ADAP_HIGH_RESP_ENTRIES);
3747 
3748 		/* Init queue entries */
3749 		softs->qentries[AAC_HOST_NORM_CMD_Q] =
3750 		    &softs->qtablep->qt_HostNormCmdQueue[0];
3751 		softs->qentries[AAC_HOST_HIGH_CMD_Q] =
3752 		    &softs->qtablep->qt_HostHighCmdQueue[0];
3753 		softs->qentries[AAC_ADAP_NORM_CMD_Q] =
3754 		    &softs->qtablep->qt_AdapNormCmdQueue[0];
3755 		softs->qentries[AAC_ADAP_HIGH_CMD_Q] =
3756 		    &softs->qtablep->qt_AdapHighCmdQueue[0];
3757 		softs->qentries[AAC_HOST_NORM_RESP_Q] =
3758 		    &softs->qtablep->qt_HostNormRespQueue[0];
3759 		softs->qentries[AAC_HOST_HIGH_RESP_Q] =
3760 		    &softs->qtablep->qt_HostHighRespQueue[0];
3761 		softs->qentries[AAC_ADAP_NORM_RESP_Q] =
3762 		    &softs->qtablep->qt_AdapNormRespQueue[0];
3763 		softs->qentries[AAC_ADAP_HIGH_RESP_Q] =
3764 		    &softs->qtablep->qt_AdapHighRespQueue[0];
3765 	}
3766 	(void) ddi_dma_sync(dma, 0, 0, DDI_DMA_SYNC_FORDEV);
3767 
3768 	/* Send init structure to the card */
3769 	if (aac_sync_mbcommand(softs, AAC_MONKER_INITSTRUCT,
3770 	    comm_space_phyaddr + \
3771 	    offsetof(struct aac_comm_space, init_data),
3772 	    0, 0, 0, NULL) == AACERR) {
3773 		AACDB_PRINT(softs, CE_WARN,
3774 		    "Cannot send init structure to adapter");
3775 		return (AACERR);
3776 	}
3777 
3778 	return (AACOK);
3779 }
3780 
3781 static uchar_t *
3782 aac_vendor_id(struct aac_softstate *softs, uchar_t *buf)
3783 {
3784 	(void) memset(buf, ' ', AAC_VENDOR_LEN);
3785 	bcopy(softs->vendor_name, buf, strlen(softs->vendor_name));
3786 	return (buf + AAC_VENDOR_LEN);
3787 }
3788 
3789 static uchar_t *
3790 aac_product_id(struct aac_softstate *softs, uchar_t *buf)
3791 {
3792 	(void) memset(buf, ' ', AAC_PRODUCT_LEN);
3793 	bcopy(softs->product_name, buf, strlen(softs->product_name));
3794 	return (buf + AAC_PRODUCT_LEN);
3795 }
3796 
3797 /*
3798  * Construct unit serial number from container uid
3799  */
3800 static uchar_t *
3801 aac_lun_serialno(struct aac_softstate *softs, int tgt, uchar_t *buf)
3802 {
3803 	int i, d;
3804 	uint32_t uid;
3805 
3806 	ASSERT(tgt >= 0 && tgt < AAC_MAX_LD);
3807 
3808 	uid = softs->containers[tgt].uid;
3809 	for (i = 7; i >= 0; i--) {
3810 		d = uid & 0xf;
3811 		buf[i] = d > 9 ? 'A' + (d - 0xa) : '0' + d;
3812 		uid >>= 4;
3813 	}
3814 	return (buf + 8);
3815 }
3816 
3817 /*
3818  * SPC-3 7.5 INQUIRY command implementation
3819  */
3820 static void
3821 aac_inquiry(struct aac_softstate *softs, struct scsi_pkt *pkt,
3822     union scsi_cdb *cdbp, struct buf *bp)
3823 {
3824 	int tgt = pkt->pkt_address.a_target;
3825 	char *b_addr = NULL;
3826 	uchar_t page = cdbp->cdb_opaque[2];
3827 
3828 	if (cdbp->cdb_opaque[1] & AAC_CDB_INQUIRY_CMDDT) {
3829 		/* Command Support Data is not supported */
3830 		aac_set_arq_data(pkt, KEY_ILLEGAL_REQUEST, 0x24, 0x00, 0);
3831 		return;
3832 	}
3833 
3834 	if (bp && bp->b_un.b_addr && bp->b_bcount) {
3835 		if (bp->b_flags & (B_PHYS | B_PAGEIO))
3836 			bp_mapin(bp);
3837 		b_addr = bp->b_un.b_addr;
3838 	}
3839 
3840 	if (cdbp->cdb_opaque[1] & AAC_CDB_INQUIRY_EVPD) {
3841 		uchar_t *vpdp = (uchar_t *)b_addr;
3842 		uchar_t *idp, *sp;
3843 
3844 		/* SPC-3 8.4 Vital product data parameters */
3845 		switch (page) {
3846 		case 0x00:
3847 			/* Supported VPD pages */
3848 			if (vpdp == NULL ||
3849 			    bp->b_bcount < (AAC_VPD_PAGE_DATA + 3))
3850 				return;
3851 			bzero(vpdp, AAC_VPD_PAGE_LENGTH);
3852 			vpdp[AAC_VPD_PAGE_CODE] = 0x00;
3853 			vpdp[AAC_VPD_PAGE_LENGTH] = 3;
3854 
3855 			vpdp[AAC_VPD_PAGE_DATA] = 0x00;
3856 			vpdp[AAC_VPD_PAGE_DATA + 1] = 0x80;
3857 			vpdp[AAC_VPD_PAGE_DATA + 2] = 0x83;
3858 
3859 			pkt->pkt_state |= STATE_XFERRED_DATA;
3860 			break;
3861 
3862 		case 0x80:
3863 			/* Unit serial number page */
3864 			if (vpdp == NULL ||
3865 			    bp->b_bcount < (AAC_VPD_PAGE_DATA + 8))
3866 				return;
3867 			bzero(vpdp, AAC_VPD_PAGE_LENGTH);
3868 			vpdp[AAC_VPD_PAGE_CODE] = 0x80;
3869 			vpdp[AAC_VPD_PAGE_LENGTH] = 8;
3870 
3871 			sp = &vpdp[AAC_VPD_PAGE_DATA];
3872 			(void) aac_lun_serialno(softs, tgt, sp);
3873 
3874 			pkt->pkt_state |= STATE_XFERRED_DATA;
3875 			break;
3876 
3877 		case 0x83:
3878 			/* Device identification page */
3879 			if (vpdp == NULL ||
3880 			    bp->b_bcount < (AAC_VPD_PAGE_DATA + 32))
3881 				return;
3882 			bzero(vpdp, AAC_VPD_PAGE_LENGTH);
3883 			vpdp[AAC_VPD_PAGE_CODE] = 0x83;
3884 
3885 			idp = &vpdp[AAC_VPD_PAGE_DATA];
3886 			bzero(idp, AAC_VPD_ID_LENGTH);
3887 			idp[AAC_VPD_ID_CODESET] = 0x02;
3888 			idp[AAC_VPD_ID_TYPE] = 0x01;
3889 
3890 			/*
3891 			 * SPC-3 Table 111 - Identifier type
3892 			 * One recommanded method of constructing the remainder
3893 			 * of identifier field is to concatenate the product
3894 			 * identification field from the standard INQUIRY data
3895 			 * field and the product serial number field from the
3896 			 * unit serial number page.
3897 			 */
3898 			sp = &idp[AAC_VPD_ID_DATA];
3899 			sp = aac_vendor_id(softs, sp);
3900 			sp = aac_product_id(softs, sp);
3901 			sp = aac_lun_serialno(softs, tgt, sp);
3902 			idp[AAC_VPD_ID_LENGTH] = (uintptr_t)sp - \
3903 			    (uintptr_t)&idp[AAC_VPD_ID_DATA];
3904 
3905 			vpdp[AAC_VPD_PAGE_LENGTH] = (uintptr_t)sp - \
3906 			    (uintptr_t)&vpdp[AAC_VPD_PAGE_DATA];
3907 			pkt->pkt_state |= STATE_XFERRED_DATA;
3908 			break;
3909 
3910 		default:
3911 			aac_set_arq_data(pkt, KEY_ILLEGAL_REQUEST,
3912 			    0x24, 0x00, 0);
3913 			break;
3914 		}
3915 	} else {
3916 		struct scsi_inquiry *inqp = (struct scsi_inquiry *)b_addr;
3917 		size_t len = sizeof (struct scsi_inquiry);
3918 
3919 		if (page != 0) {
3920 			aac_set_arq_data(pkt, KEY_ILLEGAL_REQUEST,
3921 			    0x24, 0x00, 0);
3922 			return;
3923 		}
3924 		if (inqp == NULL || bp->b_bcount < len)
3925 			return;
3926 
3927 		bzero(inqp, len);
3928 		inqp->inq_len = AAC_ADDITIONAL_LEN;
3929 		inqp->inq_ansi = AAC_ANSI_VER;
3930 		inqp->inq_rdf = AAC_RESP_DATA_FORMAT;
3931 		(void) aac_vendor_id(softs, (uchar_t *)inqp->inq_vid);
3932 		(void) aac_product_id(softs, (uchar_t *)inqp->inq_pid);
3933 		bcopy("V1.0", inqp->inq_revision, 4);
3934 		inqp->inq_cmdque = 1; /* enable tagged-queuing */
3935 		/*
3936 		 * For "sd-max-xfer-size" property which may impact performance
3937 		 * when IO threads increase.
3938 		 */
3939 		inqp->inq_wbus32 = 1;
3940 
3941 		pkt->pkt_state |= STATE_XFERRED_DATA;
3942 	}
3943 }
3944 
3945 /*
3946  * SPC-3 7.10 MODE SENSE command implementation
3947  */
3948 static void
3949 aac_mode_sense(struct aac_softstate *softs, struct scsi_pkt *pkt,
3950     union scsi_cdb *cdbp, struct buf *bp, int capacity)
3951 {
3952 	uchar_t pagecode;
3953 	struct mode_header *headerp;
3954 	struct mode_header_g1 *g1_headerp;
3955 	unsigned int ncyl;
3956 	caddr_t sense_data;
3957 	caddr_t next_page;
3958 	size_t sdata_size;
3959 	size_t pages_size;
3960 	int unsupport_page = 0;
3961 
3962 	ASSERT(cdbp->scc_cmd == SCMD_MODE_SENSE ||
3963 	    cdbp->scc_cmd == SCMD_MODE_SENSE_G1);
3964 
3965 	if (!(bp && bp->b_un.b_addr && bp->b_bcount))
3966 		return;
3967 
3968 	if (bp->b_flags & (B_PHYS | B_PAGEIO))
3969 		bp_mapin(bp);
3970 	pkt->pkt_state |= STATE_XFERRED_DATA;
3971 	pagecode = cdbp->cdb_un.sg.scsi[0] & 0x3F;
3972 
3973 	/* calculate the size of needed buffer */
3974 	if (cdbp->scc_cmd == SCMD_MODE_SENSE)
3975 		sdata_size = MODE_HEADER_LENGTH;
3976 	else /* must be SCMD_MODE_SENSE_G1 */
3977 		sdata_size = MODE_HEADER_LENGTH_G1;
3978 
3979 	pages_size = 0;
3980 	switch (pagecode) {
3981 	case SD_MODE_SENSE_PAGE3_CODE:
3982 		pages_size += sizeof (struct mode_format);
3983 		break;
3984 
3985 	case SD_MODE_SENSE_PAGE4_CODE:
3986 		pages_size += sizeof (struct mode_geometry);
3987 		break;
3988 
3989 	case MODEPAGE_CTRL_MODE:
3990 		if (softs->flags & AAC_FLAGS_LBA_64BIT) {
3991 			pages_size += sizeof (struct mode_control_scsi3);
3992 		} else {
3993 			unsupport_page = 1;
3994 		}
3995 		break;
3996 
3997 	case MODEPAGE_ALLPAGES:
3998 		if (softs->flags & AAC_FLAGS_LBA_64BIT) {
3999 			pages_size += sizeof (struct mode_format) +
4000 			    sizeof (struct mode_geometry) +
4001 			    sizeof (struct mode_control_scsi3);
4002 		} else {
4003 			pages_size += sizeof (struct mode_format) +
4004 			    sizeof (struct mode_geometry);
4005 		}
4006 		break;
4007 
4008 	default:
4009 		/* unsupported pages */
4010 		unsupport_page = 1;
4011 	}
4012 
4013 	/* allocate buffer to fill the send data */
4014 	sdata_size += pages_size;
4015 	sense_data = kmem_zalloc(sdata_size, KM_SLEEP);
4016 
4017 	if (cdbp->scc_cmd == SCMD_MODE_SENSE) {
4018 		headerp = (struct mode_header *)sense_data;
4019 		headerp->length = MODE_HEADER_LENGTH + pages_size -
4020 		    sizeof (headerp->length);
4021 		headerp->bdesc_length = 0;
4022 		next_page = sense_data + sizeof (struct mode_header);
4023 	} else {
4024 		g1_headerp = (void *)sense_data;
4025 		g1_headerp->length = BE_16(MODE_HEADER_LENGTH_G1 + pages_size -
4026 		    sizeof (g1_headerp->length));
4027 		g1_headerp->bdesc_length = 0;
4028 		next_page = sense_data + sizeof (struct mode_header_g1);
4029 	}
4030 
4031 	if (unsupport_page)
4032 		goto finish;
4033 
4034 	if (pagecode == SD_MODE_SENSE_PAGE3_CODE ||
4035 	    pagecode == MODEPAGE_ALLPAGES) {
4036 		/* SBC-3 7.1.3.3 Format device page */
4037 		struct mode_format *page3p;
4038 
4039 		page3p = (void *)next_page;
4040 		page3p->mode_page.code = SD_MODE_SENSE_PAGE3_CODE;
4041 		page3p->mode_page.length = sizeof (struct mode_format);
4042 		page3p->data_bytes_sect = BE_16(AAC_SECTOR_SIZE);
4043 		page3p->sect_track = BE_16(AAC_SECTORS_PER_TRACK);
4044 
4045 		next_page += sizeof (struct mode_format);
4046 	}
4047 
4048 	if (pagecode == SD_MODE_SENSE_PAGE4_CODE ||
4049 	    pagecode == MODEPAGE_ALLPAGES) {
4050 		/* SBC-3 7.1.3.8 Rigid disk device geometry page */
4051 		struct mode_geometry *page4p;
4052 
4053 		page4p = (void *)next_page;
4054 		page4p->mode_page.code = SD_MODE_SENSE_PAGE4_CODE;
4055 		page4p->mode_page.length = sizeof (struct mode_geometry);
4056 		page4p->heads = AAC_NUMBER_OF_HEADS;
4057 		page4p->rpm = BE_16(AAC_ROTATION_SPEED);
4058 		ncyl = capacity / (AAC_NUMBER_OF_HEADS * AAC_SECTORS_PER_TRACK);
4059 		page4p->cyl_lb = ncyl & 0xff;
4060 		page4p->cyl_mb = (ncyl >> 8) & 0xff;
4061 		page4p->cyl_ub = (ncyl >> 16) & 0xff;
4062 
4063 		next_page += sizeof (struct mode_geometry);
4064 	}
4065 
4066 	if ((pagecode == MODEPAGE_CTRL_MODE || pagecode == MODEPAGE_ALLPAGES) &&
4067 	    softs->flags & AAC_FLAGS_LBA_64BIT) {
4068 		/* 64-bit LBA need large sense data */
4069 		struct mode_control_scsi3 *mctl;
4070 
4071 		mctl = (void *)next_page;
4072 		mctl->mode_page.code = MODEPAGE_CTRL_MODE;
4073 		mctl->mode_page.length =
4074 		    sizeof (struct mode_control_scsi3) -
4075 		    sizeof (struct mode_page);
4076 		mctl->d_sense = 1;
4077 	}
4078 
4079 finish:
4080 	/* copyout the valid data. */
4081 	bcopy(sense_data, bp->b_un.b_addr, min(sdata_size, bp->b_bcount));
4082 	kmem_free(sense_data, sdata_size);
4083 }
4084 
4085 static int
4086 aac_name_node(dev_info_t *dip, char *name, int len)
4087 {
4088 	int tgt, lun;
4089 
4090 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
4091 	    DDI_PROP_DONTPASS, "target", -1);
4092 	if (tgt == -1)
4093 		return (DDI_FAILURE);
4094 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
4095 	    DDI_PROP_DONTPASS, "lun", -1);
4096 	if (lun == -1)
4097 		return (DDI_FAILURE);
4098 
4099 	(void) snprintf(name, len, "%x,%x", tgt, lun);
4100 	return (DDI_SUCCESS);
4101 }
4102 
4103 /*ARGSUSED*/
4104 static int
4105 aac_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
4106     scsi_hba_tran_t *tran, struct scsi_device *sd)
4107 {
4108 	struct aac_softstate *softs = AAC_TRAN2SOFTS(tran);
4109 #if defined(DEBUG) || defined(__lock_lint)
4110 	int ctl = ddi_get_instance(softs->devinfo_p);
4111 #endif
4112 	uint16_t tgt = sd->sd_address.a_target;
4113 	uint8_t lun = sd->sd_address.a_lun;
4114 	struct aac_device *dvp;
4115 
4116 	DBCALLED(softs, 2);
4117 
4118 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
4119 		/*
4120 		 * If no persistent node exist, we don't allow .conf node
4121 		 * to be created.
4122 		 */
4123 		if (aac_find_child(softs, tgt, lun) != NULL) {
4124 			if (ndi_merge_node(tgt_dip, aac_name_node) !=
4125 			    DDI_SUCCESS)
4126 				/* Create this .conf node */
4127 				return (DDI_SUCCESS);
4128 		}
4129 		return (DDI_FAILURE);
4130 	}
4131 
4132 	/*
4133 	 * Only support container/phys. device that has been
4134 	 * detected and valid
4135 	 */
4136 	mutex_enter(&softs->io_lock);
4137 	if (tgt >= AAC_MAX_DEV(softs)) {
4138 		AACDB_PRINT_TRAN(softs,
4139 		    "aac_tran_tgt_init: c%dt%dL%d out", ctl, tgt, lun);
4140 		mutex_exit(&softs->io_lock);
4141 		return (DDI_FAILURE);
4142 	}
4143 
4144 	if (tgt < AAC_MAX_LD) {
4145 		dvp = (struct aac_device *)&softs->containers[tgt];
4146 		if (lun != 0 || !AAC_DEV_IS_VALID(dvp)) {
4147 			AACDB_PRINT_TRAN(softs, "aac_tran_tgt_init: c%dt%dL%d",
4148 			    ctl, tgt, lun);
4149 			mutex_exit(&softs->io_lock);
4150 			return (DDI_FAILURE);
4151 		}
4152 		/*
4153 		 * Save the tgt_dip for the given target if one doesn't exist
4154 		 * already. Dip's for non-existance tgt's will be cleared in
4155 		 * tgt_free.
4156 		 */
4157 		if (softs->containers[tgt].dev.dip == NULL &&
4158 		    strcmp(ddi_driver_name(sd->sd_dev), "sd") == 0)
4159 			softs->containers[tgt].dev.dip = tgt_dip;
4160 	} else {
4161 		dvp = (struct aac_device *)&softs->nondasds[AAC_PD(tgt)];
4162 	}
4163 
4164 	if (softs->flags & AAC_FLAGS_BRKUP) {
4165 		if (ndi_prop_update_int(DDI_DEV_T_NONE, tgt_dip,
4166 		    "buf_break", 1) != DDI_PROP_SUCCESS) {
4167 			cmn_err(CE_CONT, "unable to create "
4168 			    "property for t%dL%d (buf_break)", tgt, lun);
4169 		}
4170 	}
4171 
4172 	AACDB_PRINT(softs, CE_NOTE,
4173 	    "aac_tran_tgt_init: c%dt%dL%d ok (%s)", ctl, tgt, lun,
4174 	    (dvp->type == AAC_DEV_PD) ? "pd" : "ld");
4175 	mutex_exit(&softs->io_lock);
4176 	return (DDI_SUCCESS);
4177 }
4178 
4179 static void
4180 aac_tran_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
4181     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
4182 {
4183 #ifndef __lock_lint
4184 	_NOTE(ARGUNUSED(hba_dip, tgt_dip, hba_tran))
4185 #endif
4186 
4187 	struct aac_softstate *softs = SD2AAC(sd);
4188 	int tgt = sd->sd_address.a_target;
4189 
4190 	mutex_enter(&softs->io_lock);
4191 	if (tgt < AAC_MAX_LD) {
4192 		if (softs->containers[tgt].dev.dip == tgt_dip)
4193 			softs->containers[tgt].dev.dip = NULL;
4194 	} else {
4195 		softs->nondasds[AAC_PD(tgt)].dev.flags &= ~AAC_DFLAG_VALID;
4196 	}
4197 	mutex_exit(&softs->io_lock);
4198 }
4199 
4200 /*
4201  * Check if the firmware is Up And Running. If it is in the Kernel Panic
4202  * state, (BlinkLED code + 1) is returned.
4203  *    0 -- firmware up and running
4204  *   -1 -- firmware dead
4205  *   >0 -- firmware kernel panic
4206  */
4207 static int
4208 aac_check_adapter_health(struct aac_softstate *softs)
4209 {
4210 	int rval;
4211 
4212 	rval = PCI_MEM_GET32(softs, AAC_OMR0);
4213 
4214 	if (rval & AAC_KERNEL_UP_AND_RUNNING) {
4215 		rval = 0;
4216 	} else if (rval & AAC_KERNEL_PANIC) {
4217 		cmn_err(CE_WARN, "firmware panic");
4218 		rval = ((rval >> 16) & 0xff) + 1; /* avoid 0 as return value */
4219 	} else {
4220 		cmn_err(CE_WARN, "firmware dead");
4221 		rval = -1;
4222 	}
4223 	return (rval);
4224 }
4225 
4226 static void
4227 aac_abort_iocmd(struct aac_softstate *softs, struct aac_cmd *acp,
4228     uchar_t reason)
4229 {
4230 	acp->flags |= AAC_CMD_ABORT;
4231 
4232 	if (acp->pkt) {
4233 		if (acp->slotp) { /* outstanding cmd */
4234 			acp->pkt->pkt_state |= STATE_GOT_STATUS;
4235 		}
4236 
4237 		switch (reason) {
4238 		case CMD_TIMEOUT:
4239 			AACDB_PRINT(softs, CE_NOTE, "CMD_TIMEOUT: acp=0x%p",
4240 			    acp);
4241 			aac_set_pkt_reason(softs, acp, CMD_TIMEOUT,
4242 			    STAT_TIMEOUT | STAT_BUS_RESET);
4243 			break;
4244 		case CMD_RESET:
4245 			/* aac support only RESET_ALL */
4246 			AACDB_PRINT(softs, CE_NOTE, "CMD_RESET: acp=0x%p", acp);
4247 			aac_set_pkt_reason(softs, acp, CMD_RESET,
4248 			    STAT_BUS_RESET);
4249 			break;
4250 		case CMD_ABORTED:
4251 			AACDB_PRINT(softs, CE_NOTE, "CMD_ABORTED: acp=0x%p",
4252 			    acp);
4253 			aac_set_pkt_reason(softs, acp, CMD_ABORTED,
4254 			    STAT_ABORTED);
4255 			break;
4256 		}
4257 	}
4258 	aac_end_io(softs, acp);
4259 }
4260 
4261 /*
4262  * Abort all the pending commands of type iocmd or just the command pkt
4263  * corresponding to pkt
4264  */
4265 static void
4266 aac_abort_iocmds(struct aac_softstate *softs, int iocmd, struct scsi_pkt *pkt,
4267     int reason)
4268 {
4269 	struct aac_cmd *ac_arg, *acp;
4270 	int i;
4271 
4272 	if (pkt == NULL) {
4273 		ac_arg = NULL;
4274 	} else {
4275 		ac_arg = PKT2AC(pkt);
4276 		iocmd = (ac_arg->flags & AAC_CMD_SYNC) ?
4277 		    AAC_IOCMD_SYNC : AAC_IOCMD_ASYNC;
4278 	}
4279 
4280 	/*
4281 	 * a) outstanding commands on the controller
4282 	 * Note: should abort outstanding commands only after one
4283 	 * IOP reset has been done.
4284 	 */
4285 	if (iocmd & AAC_IOCMD_OUTSTANDING) {
4286 		struct aac_cmd *acp;
4287 
4288 		for (i = 0; i < AAC_MAX_LD; i++) {
4289 			if (AAC_DEV_IS_VALID(&softs->containers[i].dev))
4290 				softs->containers[i].reset = 1;
4291 		}
4292 		while ((acp = softs->q_busy.q_head) != NULL)
4293 			aac_abort_iocmd(softs, acp, reason);
4294 	}
4295 
4296 	/* b) commands in the waiting queues */
4297 	for (i = 0; i < AAC_CMDQ_NUM; i++) {
4298 		if (iocmd & (1 << i)) {
4299 			if (ac_arg) {
4300 				aac_abort_iocmd(softs, ac_arg, reason);
4301 			} else {
4302 				while ((acp = softs->q_wait[i].q_head) != NULL)
4303 					aac_abort_iocmd(softs, acp, reason);
4304 			}
4305 		}
4306 	}
4307 }
4308 
4309 /*
4310  * The draining thread is shared among quiesce threads. It terminates
4311  * when the adapter is quiesced or stopped by aac_stop_drain().
4312  */
4313 static void
4314 aac_check_drain(void *arg)
4315 {
4316 	struct aac_softstate *softs = arg;
4317 
4318 	mutex_enter(&softs->io_lock);
4319 	if (softs->ndrains) {
4320 		softs->drain_timeid = 0;
4321 		/*
4322 		 * If both ASYNC and SYNC bus throttle are held,
4323 		 * wake up threads only when both are drained out.
4324 		 */
4325 		if ((softs->bus_throttle[AAC_CMDQ_ASYNC] > 0 ||
4326 		    softs->bus_ncmds[AAC_CMDQ_ASYNC] == 0) &&
4327 		    (softs->bus_throttle[AAC_CMDQ_SYNC] > 0 ||
4328 		    softs->bus_ncmds[AAC_CMDQ_SYNC] == 0))
4329 			cv_broadcast(&softs->drain_cv);
4330 		else
4331 			softs->drain_timeid = timeout(aac_check_drain, softs,
4332 			    AAC_QUIESCE_TICK * drv_usectohz(1000000));
4333 	}
4334 	mutex_exit(&softs->io_lock);
4335 }
4336 
4337 /*
4338  * If not draining the outstanding cmds, drain them. Otherwise,
4339  * only update ndrains.
4340  */
4341 static void
4342 aac_start_drain(struct aac_softstate *softs)
4343 {
4344 	if (softs->ndrains == 0) {
4345 		ASSERT(softs->drain_timeid == 0);
4346 		softs->drain_timeid = timeout(aac_check_drain, softs,
4347 		    AAC_QUIESCE_TICK * drv_usectohz(1000000));
4348 	}
4349 	softs->ndrains++;
4350 }
4351 
4352 /*
4353  * Stop the draining thread when no other threads use it any longer.
4354  * Side effect: io_lock may be released in the middle.
4355  */
4356 static void
4357 aac_stop_drain(struct aac_softstate *softs)
4358 {
4359 	softs->ndrains--;
4360 	if (softs->ndrains == 0) {
4361 		if (softs->drain_timeid != 0) {
4362 			timeout_id_t tid = softs->drain_timeid;
4363 
4364 			softs->drain_timeid = 0;
4365 			mutex_exit(&softs->io_lock);
4366 			(void) untimeout(tid);
4367 			mutex_enter(&softs->io_lock);
4368 		}
4369 	}
4370 }
4371 
4372 /*
4373  * The following function comes from Adaptec:
4374  *
4375  * Once do an IOP reset, basically the driver have to re-initialize the card
4376  * as if up from a cold boot, and the driver is responsible for any IO that
4377  * is outstanding to the adapter at the time of the IOP RESET. And prepare
4378  * for IOP RESET by making the init code modular with the ability to call it
4379  * from multiple places.
4380  */
4381 static int
4382 aac_reset_adapter(struct aac_softstate *softs)
4383 {
4384 	int health;
4385 	uint32_t status;
4386 	int rval = AAC_IOP_RESET_FAILED;
4387 
4388 	DBCALLED(softs, 1);
4389 
4390 	ASSERT(softs->state & AAC_STATE_RESET);
4391 
4392 	ddi_fm_acc_err_clear(softs->pci_mem_handle, DDI_FME_VER0);
4393 	/* Disable interrupt */
4394 	AAC_DISABLE_INTR(softs);
4395 
4396 	health = aac_check_adapter_health(softs);
4397 	if (health == -1) {
4398 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
4399 		goto finish;
4400 	}
4401 	if (health == 0) /* flush drives if possible */
4402 		(void) aac_shutdown(softs);
4403 
4404 	/* Execute IOP reset */
4405 	if ((aac_sync_mbcommand(softs, AAC_IOP_RESET, 0, 0, 0, 0,
4406 	    &status)) != AACOK) {
4407 		ddi_acc_handle_t acc;
4408 		struct aac_fib *fibp;
4409 		struct aac_pause_command *pc;
4410 
4411 		if ((status & 0xf) == 0xf) {
4412 			uint32_t wait_count;
4413 
4414 			/*
4415 			 * Sunrise Lake has dual cores and we must drag the
4416 			 * other core with us to reset simultaneously. There
4417 			 * are 2 bits in the Inbound Reset Control and Status
4418 			 * Register (offset 0x38) of the Sunrise Lake to reset
4419 			 * the chip without clearing out the PCI configuration
4420 			 * info (COMMAND & BARS).
4421 			 */
4422 			PCI_MEM_PUT32(softs, AAC_IRCSR, AAC_IRCSR_CORES_RST);
4423 
4424 			/*
4425 			 * We need to wait for 5 seconds before accessing the MU
4426 			 * again 10000 * 100us = 1000,000us = 1000ms = 1s
4427 			 */
4428 			wait_count = 5 * 10000;
4429 			while (wait_count) {
4430 				drv_usecwait(100); /* delay 100 microseconds */
4431 				wait_count--;
4432 			}
4433 		} else {
4434 			if (status == SRB_STATUS_INVALID_REQUEST)
4435 				cmn_err(CE_WARN, "!IOP_RESET not supported");
4436 			else /* probably timeout */
4437 				cmn_err(CE_WARN, "!IOP_RESET failed");
4438 
4439 			/* Unwind aac_shutdown() */
4440 			(void) aac_sync_fib_slot_bind(softs, &softs->sync_ac);
4441 			acc = softs->sync_ac.slotp->fib_acc_handle;
4442 
4443 			fibp = softs->sync_ac.slotp->fibp;
4444 			pc = (struct aac_pause_command *)&fibp->data[0];
4445 
4446 			bzero(pc, sizeof (*pc));
4447 			ddi_put32(acc, &pc->Command, VM_ContainerConfig);
4448 			ddi_put32(acc, &pc->Type, CT_PAUSE_IO);
4449 			ddi_put32(acc, &pc->Timeout, 1);
4450 			ddi_put32(acc, &pc->Min, 1);
4451 			ddi_put32(acc, &pc->NoRescan, 1);
4452 
4453 			(void) aac_sync_fib(softs, ContainerCommand,
4454 			    AAC_FIB_SIZEOF(struct aac_pause_command));
4455 			aac_sync_fib_slot_release(softs, &softs->sync_ac);
4456 
4457 			if (aac_check_adapter_health(softs) != 0)
4458 				ddi_fm_service_impact(softs->devinfo_p,
4459 				    DDI_SERVICE_LOST);
4460 			else
4461 				/*
4462 				 * IOP reset not supported or IOP not reseted
4463 				 */
4464 				rval = AAC_IOP_RESET_ABNORMAL;
4465 			goto finish;
4466 		}
4467 	}
4468 
4469 	/*
4470 	 * Re-read and renegotiate the FIB parameters, as one of the actions
4471 	 * that can result from an IOP reset is the running of a new firmware
4472 	 * image.
4473 	 */
4474 	if (aac_common_attach(softs) != AACOK)
4475 		goto finish;
4476 
4477 	rval = AAC_IOP_RESET_SUCCEED;
4478 
4479 finish:
4480 	AAC_ENABLE_INTR(softs);
4481 	return (rval);
4482 }
4483 
4484 static void
4485 aac_set_throttle(struct aac_softstate *softs, struct aac_device *dvp, int q,
4486     int throttle)
4487 {
4488 	/*
4489 	 * If the bus is draining/quiesced, no changes to the throttles
4490 	 * are allowed. All throttles should have been set to 0.
4491 	 */
4492 	if ((softs->state & AAC_STATE_QUIESCED) || softs->ndrains)
4493 		return;
4494 	dvp->throttle[q] = throttle;
4495 }
4496 
4497 static void
4498 aac_hold_bus(struct aac_softstate *softs, int iocmds)
4499 {
4500 	int i, q;
4501 
4502 	/* Hold bus by holding every device on the bus */
4503 	for (q = 0; q < AAC_CMDQ_NUM; q++) {
4504 		if (iocmds & (1 << q)) {
4505 			softs->bus_throttle[q] = 0;
4506 			for (i = 0; i < AAC_MAX_LD; i++)
4507 				aac_set_throttle(softs,
4508 				    &softs->containers[i].dev, q, 0);
4509 			for (i = 0; i < AAC_MAX_PD(softs); i++)
4510 				aac_set_throttle(softs,
4511 				    &softs->nondasds[i].dev, q, 0);
4512 		}
4513 	}
4514 }
4515 
4516 static void
4517 aac_unhold_bus(struct aac_softstate *softs, int iocmds)
4518 {
4519 	int i, q, max_throttle;
4520 
4521 	for (q = 0; q < AAC_CMDQ_NUM; q++) {
4522 		if (iocmds & (1 << q)) {
4523 			/*
4524 			 * Should not unhold AAC_IOCMD_ASYNC bus, if it has been
4525 			 * quiesced or being drained by possibly some quiesce
4526 			 * threads.
4527 			 */
4528 			if (q == AAC_CMDQ_ASYNC && ((softs->state &
4529 			    AAC_STATE_QUIESCED) || softs->ndrains))
4530 				continue;
4531 			if (q == AAC_CMDQ_ASYNC)
4532 				max_throttle = softs->total_slots -
4533 				    AAC_MGT_SLOT_NUM;
4534 			else
4535 				max_throttle = softs->total_slots - 1;
4536 			softs->bus_throttle[q] = max_throttle;
4537 			for (i = 0; i < AAC_MAX_LD; i++)
4538 				aac_set_throttle(softs,
4539 				    &softs->containers[i].dev,
4540 				    q, max_throttle);
4541 			for (i = 0; i < AAC_MAX_PD(softs); i++)
4542 				aac_set_throttle(softs, &softs->nondasds[i].dev,
4543 				    q, max_throttle);
4544 		}
4545 	}
4546 }
4547 
4548 static int
4549 aac_do_reset(struct aac_softstate *softs)
4550 {
4551 	int health;
4552 	int rval;
4553 
4554 	softs->state |= AAC_STATE_RESET;
4555 	health = aac_check_adapter_health(softs);
4556 
4557 	/*
4558 	 * Hold off new io commands and wait all outstanding io
4559 	 * commands to complete.
4560 	 */
4561 	if (health == 0) {
4562 		int sync_cmds = softs->bus_ncmds[AAC_CMDQ_SYNC];
4563 		int async_cmds = softs->bus_ncmds[AAC_CMDQ_ASYNC];
4564 
4565 		if (sync_cmds == 0 && async_cmds == 0) {
4566 			rval = AAC_IOP_RESET_SUCCEED;
4567 			goto finish;
4568 		}
4569 		/*
4570 		 * Give the adapter up to AAC_QUIESCE_TIMEOUT more seconds
4571 		 * to complete the outstanding io commands
4572 		 */
4573 		int timeout = AAC_QUIESCE_TIMEOUT * 1000 * 10;
4574 		int (*intr_handler)(struct aac_softstate *);
4575 
4576 		aac_hold_bus(softs, AAC_IOCMD_SYNC | AAC_IOCMD_ASYNC);
4577 		/*
4578 		 * Poll the adapter by ourselves in case interrupt is disabled
4579 		 * and to avoid releasing the io_lock.
4580 		 */
4581 		intr_handler = (softs->flags & AAC_FLAGS_NEW_COMM) ?
4582 		    aac_process_intr_new : aac_process_intr_old;
4583 		while ((softs->bus_ncmds[AAC_CMDQ_SYNC] ||
4584 		    softs->bus_ncmds[AAC_CMDQ_ASYNC]) && timeout) {
4585 			drv_usecwait(100);
4586 			(void) intr_handler(softs);
4587 			timeout--;
4588 		}
4589 		aac_unhold_bus(softs, AAC_IOCMD_SYNC | AAC_IOCMD_ASYNC);
4590 
4591 		if (softs->bus_ncmds[AAC_CMDQ_SYNC] == 0 &&
4592 		    softs->bus_ncmds[AAC_CMDQ_ASYNC] == 0) {
4593 			/* Cmds drained out */
4594 			rval = AAC_IOP_RESET_SUCCEED;
4595 			goto finish;
4596 		} else if (softs->bus_ncmds[AAC_CMDQ_SYNC] < sync_cmds ||
4597 		    softs->bus_ncmds[AAC_CMDQ_ASYNC] < async_cmds) {
4598 			/* Cmds not drained out, adapter overloaded */
4599 			rval = AAC_IOP_RESET_ABNORMAL;
4600 			goto finish;
4601 		}
4602 	}
4603 
4604 	/*
4605 	 * If a longer waiting time still can't drain any outstanding io
4606 	 * commands, do IOP reset.
4607 	 */
4608 	if ((rval = aac_reset_adapter(softs)) == AAC_IOP_RESET_FAILED)
4609 		softs->state |= AAC_STATE_DEAD;
4610 
4611 finish:
4612 	softs->state &= ~AAC_STATE_RESET;
4613 	return (rval);
4614 }
4615 
4616 static int
4617 aac_tran_reset(struct scsi_address *ap, int level)
4618 {
4619 	struct aac_softstate *softs = AAC_TRAN2SOFTS(ap->a_hba_tran);
4620 	int rval;
4621 
4622 	DBCALLED(softs, 1);
4623 
4624 	if (level != RESET_ALL) {
4625 		cmn_err(CE_NOTE, "!reset target/lun not supported");
4626 		return (0);
4627 	}
4628 
4629 	mutex_enter(&softs->io_lock);
4630 	switch (rval = aac_do_reset(softs)) {
4631 	case AAC_IOP_RESET_SUCCEED:
4632 		aac_abort_iocmds(softs, AAC_IOCMD_OUTSTANDING | AAC_IOCMD_ASYNC,
4633 		    NULL, CMD_RESET);
4634 		aac_start_waiting_io(softs);
4635 		break;
4636 	case AAC_IOP_RESET_FAILED:
4637 		/* Abort IOCTL cmds when adapter is dead */
4638 		aac_abort_iocmds(softs, AAC_IOCMD_ALL, NULL, CMD_RESET);
4639 		break;
4640 	case AAC_IOP_RESET_ABNORMAL:
4641 		aac_start_waiting_io(softs);
4642 	}
4643 	mutex_exit(&softs->io_lock);
4644 
4645 	aac_drain_comp_q(softs);
4646 	return (rval == 0);
4647 }
4648 
4649 static int
4650 aac_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
4651 {
4652 	struct aac_softstate *softs = AAC_TRAN2SOFTS(ap->a_hba_tran);
4653 
4654 	DBCALLED(softs, 1);
4655 
4656 	mutex_enter(&softs->io_lock);
4657 	aac_abort_iocmds(softs, 0, pkt, CMD_ABORTED);
4658 	mutex_exit(&softs->io_lock);
4659 
4660 	aac_drain_comp_q(softs);
4661 	return (1);
4662 }
4663 
4664 void
4665 aac_free_dmamap(struct aac_cmd *acp)
4666 {
4667 	/* Free dma mapping */
4668 	if (acp->flags & AAC_CMD_DMA_VALID) {
4669 		ASSERT(acp->buf_dma_handle);
4670 		(void) ddi_dma_unbind_handle(acp->buf_dma_handle);
4671 		acp->flags &= ~AAC_CMD_DMA_VALID;
4672 	}
4673 
4674 	if (acp->abp != NULL) { /* free non-aligned buf DMA */
4675 		ASSERT(acp->buf_dma_handle);
4676 		if ((acp->flags & AAC_CMD_BUF_WRITE) == 0 && acp->bp)
4677 			ddi_rep_get8(acp->abh, (uint8_t *)acp->bp->b_un.b_addr,
4678 			    (uint8_t *)acp->abp, acp->bp->b_bcount,
4679 			    DDI_DEV_AUTOINCR);
4680 		ddi_dma_mem_free(&acp->abh);
4681 		acp->abp = NULL;
4682 	}
4683 
4684 	if (acp->buf_dma_handle) {
4685 		ddi_dma_free_handle(&acp->buf_dma_handle);
4686 		acp->buf_dma_handle = NULL;
4687 	}
4688 }
4689 
4690 static void
4691 aac_unknown_scmd(struct aac_softstate *softs, struct aac_cmd *acp)
4692 {
4693 	AACDB_PRINT(softs, CE_CONT, "SCMD 0x%x not supported",
4694 	    ((union scsi_cdb *)(void *)acp->pkt->pkt_cdbp)->scc_cmd);
4695 	aac_free_dmamap(acp);
4696 	aac_set_arq_data(acp->pkt, KEY_ILLEGAL_REQUEST, 0x20, 0x00, 0);
4697 	aac_soft_callback(softs, acp);
4698 }
4699 
4700 /*
4701  * Handle command to logical device
4702  */
4703 static int
4704 aac_tran_start_ld(struct aac_softstate *softs, struct aac_cmd *acp)
4705 {
4706 	struct aac_container *dvp;
4707 	struct scsi_pkt *pkt;
4708 	union scsi_cdb *cdbp;
4709 	struct buf *bp;
4710 	int rval;
4711 
4712 	dvp = (struct aac_container *)acp->dvp;
4713 	pkt = acp->pkt;
4714 	cdbp = (void *)pkt->pkt_cdbp;
4715 	bp = acp->bp;
4716 
4717 	switch (cdbp->scc_cmd) {
4718 	case SCMD_INQUIRY: /* inquiry */
4719 		aac_free_dmamap(acp);
4720 		aac_inquiry(softs, pkt, cdbp, bp);
4721 		aac_soft_callback(softs, acp);
4722 		rval = TRAN_ACCEPT;
4723 		break;
4724 
4725 	case SCMD_READ_CAPACITY: /* read capacity */
4726 		if (bp && bp->b_un.b_addr && bp->b_bcount) {
4727 			struct scsi_capacity cap;
4728 			uint64_t last_lba;
4729 
4730 			/* check 64-bit LBA */
4731 			last_lba = dvp->size - 1;
4732 			if (last_lba > 0xffffffffull) {
4733 				cap.capacity = 0xfffffffful;
4734 			} else {
4735 				cap.capacity = BE_32(last_lba);
4736 			}
4737 			cap.lbasize = BE_32(AAC_SECTOR_SIZE);
4738 
4739 			aac_free_dmamap(acp);
4740 			if (bp->b_flags & (B_PHYS|B_PAGEIO))
4741 				bp_mapin(bp);
4742 			bcopy(&cap, bp->b_un.b_addr, min(bp->b_bcount, 8));
4743 			pkt->pkt_state |= STATE_XFERRED_DATA;
4744 		}
4745 		aac_soft_callback(softs, acp);
4746 		rval = TRAN_ACCEPT;
4747 		break;
4748 
4749 	case SCMD_SVC_ACTION_IN_G4: /* read capacity 16 */
4750 		/* Check if containers need 64-bit LBA support */
4751 		if (cdbp->cdb_opaque[1] == SSVC_ACTION_READ_CAPACITY_G4) {
4752 			if (bp && bp->b_un.b_addr && bp->b_bcount) {
4753 				struct scsi_capacity_16 cap16;
4754 				int cap_len = sizeof (struct scsi_capacity_16);
4755 
4756 				bzero(&cap16, cap_len);
4757 				cap16.sc_capacity = BE_64(dvp->size - 1);
4758 				cap16.sc_lbasize = BE_32(AAC_SECTOR_SIZE);
4759 
4760 				aac_free_dmamap(acp);
4761 				if (bp->b_flags & (B_PHYS | B_PAGEIO))
4762 					bp_mapin(bp);
4763 				bcopy(&cap16, bp->b_un.b_addr,
4764 				    min(bp->b_bcount, cap_len));
4765 				pkt->pkt_state |= STATE_XFERRED_DATA;
4766 			}
4767 			aac_soft_callback(softs, acp);
4768 		} else {
4769 			aac_unknown_scmd(softs, acp);
4770 		}
4771 		rval = TRAN_ACCEPT;
4772 		break;
4773 
4774 	case SCMD_READ_G4: /* read_16 */
4775 	case SCMD_WRITE_G4: /* write_16 */
4776 		if (softs->flags & AAC_FLAGS_RAW_IO) {
4777 			/* NOTE: GETG4ADDRTL(cdbp) is int32_t */
4778 			acp->blkno = ((uint64_t) \
4779 			    GETG4ADDR(cdbp) << 32) | \
4780 			    (uint32_t)GETG4ADDRTL(cdbp);
4781 			goto do_io;
4782 		}
4783 		AACDB_PRINT(softs, CE_WARN, "64-bit LBA not supported");
4784 		aac_unknown_scmd(softs, acp);
4785 		rval = TRAN_ACCEPT;
4786 		break;
4787 
4788 	case SCMD_READ: /* read_6 */
4789 	case SCMD_WRITE: /* write_6 */
4790 		acp->blkno = GETG0ADDR(cdbp);
4791 		goto do_io;
4792 
4793 	case SCMD_READ_G5: /* read_12 */
4794 	case SCMD_WRITE_G5: /* write_12 */
4795 		acp->blkno = GETG5ADDR(cdbp);
4796 		goto do_io;
4797 
4798 	case SCMD_READ_G1: /* read_10 */
4799 	case SCMD_WRITE_G1: /* write_10 */
4800 		acp->blkno = (uint32_t)GETG1ADDR(cdbp);
4801 do_io:
4802 		if (acp->flags & AAC_CMD_DMA_VALID) {
4803 			uint64_t cnt_size = dvp->size;
4804 
4805 			/*
4806 			 * If LBA > array size AND rawio, the
4807 			 * adapter may hang. So check it before
4808 			 * sending.
4809 			 * NOTE: (blkno + blkcnt) may overflow
4810 			 */
4811 			if ((acp->blkno < cnt_size) &&
4812 			    ((acp->blkno + acp->bcount /
4813 			    AAC_BLK_SIZE) <= cnt_size)) {
4814 				rval = aac_do_io(softs, acp);
4815 			} else {
4816 			/*
4817 			 * Request exceeds the capacity of disk,
4818 			 * set error block number to last LBA
4819 			 * + 1.
4820 			 */
4821 				aac_set_arq_data(pkt,
4822 				    KEY_ILLEGAL_REQUEST, 0x21,
4823 				    0x00, cnt_size);
4824 				aac_soft_callback(softs, acp);
4825 				rval = TRAN_ACCEPT;
4826 			}
4827 		} else if (acp->bcount == 0) {
4828 			/* For 0 length IO, just return ok */
4829 			aac_soft_callback(softs, acp);
4830 			rval = TRAN_ACCEPT;
4831 		} else {
4832 			rval = TRAN_BADPKT;
4833 		}
4834 		break;
4835 
4836 	case SCMD_MODE_SENSE: /* mode_sense_6 */
4837 	case SCMD_MODE_SENSE_G1: { /* mode_sense_10 */
4838 		int capacity;
4839 
4840 		aac_free_dmamap(acp);
4841 		if (dvp->size > 0xffffffffull)
4842 			capacity = 0xfffffffful; /* 64-bit LBA */
4843 		else
4844 			capacity = dvp->size;
4845 		aac_mode_sense(softs, pkt, cdbp, bp, capacity);
4846 		aac_soft_callback(softs, acp);
4847 		rval = TRAN_ACCEPT;
4848 		break;
4849 	}
4850 
4851 	case SCMD_START_STOP:
4852 		if (softs->support_opt2 & AAC_SUPPORTED_POWER_MANAGEMENT) {
4853 			acp->aac_cmd_fib = aac_cmd_fib_startstop;
4854 			acp->ac_comp = aac_startstop_complete;
4855 			rval = aac_do_io(softs, acp);
4856 			break;
4857 		}
4858 	/* FALLTHRU */
4859 	case SCMD_TEST_UNIT_READY:
4860 	case SCMD_REQUEST_SENSE:
4861 	case SCMD_FORMAT:
4862 		aac_free_dmamap(acp);
4863 		if (bp && bp->b_un.b_addr && bp->b_bcount) {
4864 			if (acp->flags & AAC_CMD_BUF_READ) {
4865 				if (bp->b_flags & (B_PHYS|B_PAGEIO))
4866 					bp_mapin(bp);
4867 				bzero(bp->b_un.b_addr, bp->b_bcount);
4868 			}
4869 			pkt->pkt_state |= STATE_XFERRED_DATA;
4870 		}
4871 		aac_soft_callback(softs, acp);
4872 		rval = TRAN_ACCEPT;
4873 		break;
4874 
4875 	case SCMD_SYNCHRONIZE_CACHE:
4876 		acp->flags |= AAC_CMD_NTAG;
4877 		acp->aac_cmd_fib = aac_cmd_fib_sync;
4878 		acp->ac_comp = aac_synccache_complete;
4879 		rval = aac_do_io(softs, acp);
4880 		break;
4881 
4882 	case SCMD_DOORLOCK:
4883 		aac_free_dmamap(acp);
4884 		dvp->locked = (pkt->pkt_cdbp[4] & 0x01) ? 1 : 0;
4885 		aac_soft_callback(softs, acp);
4886 		rval = TRAN_ACCEPT;
4887 		break;
4888 
4889 	default: /* unknown command */
4890 		aac_unknown_scmd(softs, acp);
4891 		rval = TRAN_ACCEPT;
4892 		break;
4893 	}
4894 
4895 	return (rval);
4896 }
4897 
4898 static int
4899 aac_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt)
4900 {
4901 	struct aac_softstate *softs = AAC_TRAN2SOFTS(ap->a_hba_tran);
4902 	struct aac_cmd *acp = PKT2AC(pkt);
4903 	struct aac_device *dvp = acp->dvp;
4904 	int rval;
4905 
4906 	DBCALLED(softs, 2);
4907 
4908 	/*
4909 	 * Reinitialize some fields of ac and pkt; the packet may
4910 	 * have been resubmitted
4911 	 */
4912 	acp->flags &= AAC_CMD_CONSISTENT | AAC_CMD_DMA_PARTIAL | \
4913 	    AAC_CMD_BUF_READ | AAC_CMD_BUF_WRITE | AAC_CMD_DMA_VALID;
4914 	acp->timeout = acp->pkt->pkt_time;
4915 	if (pkt->pkt_flags & FLAG_NOINTR)
4916 		acp->flags |= AAC_CMD_NO_INTR;
4917 #ifdef DEBUG
4918 	acp->fib_flags = AACDB_FLAGS_FIB_SCMD;
4919 #endif
4920 	pkt->pkt_reason = CMD_CMPLT;
4921 	pkt->pkt_state = 0;
4922 	pkt->pkt_statistics = 0;
4923 	*pkt->pkt_scbp = STATUS_GOOD; /* clear arq scsi_status */
4924 
4925 	if (acp->flags & AAC_CMD_DMA_VALID) {
4926 		pkt->pkt_resid = acp->bcount;
4927 		/* Consistent packets need to be sync'ed first */
4928 		if ((acp->flags & AAC_CMD_CONSISTENT) &&
4929 		    (acp->flags & AAC_CMD_BUF_WRITE))
4930 			if (aac_dma_sync_ac(acp) != AACOK) {
4931 				ddi_fm_service_impact(softs->devinfo_p,
4932 				    DDI_SERVICE_UNAFFECTED);
4933 				return (TRAN_BADPKT);
4934 			}
4935 	} else {
4936 		pkt->pkt_resid = 0;
4937 	}
4938 
4939 	mutex_enter(&softs->io_lock);
4940 	AACDB_PRINT_SCMD(softs, acp);
4941 	if ((dvp->flags & (AAC_DFLAG_VALID | AAC_DFLAG_CONFIGURING)) &&
4942 	    !(softs->state & AAC_STATE_DEAD)) {
4943 		if (dvp->type == AAC_DEV_LD) {
4944 			if (ap->a_lun == 0)
4945 				rval = aac_tran_start_ld(softs, acp);
4946 			else
4947 				goto error;
4948 		} else {
4949 			rval = aac_do_io(softs, acp);
4950 		}
4951 	} else {
4952 error:
4953 #ifdef DEBUG
4954 		if (!(softs->state & AAC_STATE_DEAD)) {
4955 			AACDB_PRINT_TRAN(softs,
4956 			    "Cannot send cmd to target t%dL%d: %s",
4957 			    ap->a_target, ap->a_lun,
4958 			    "target invalid");
4959 		} else {
4960 			AACDB_PRINT(softs, CE_WARN,
4961 			    "Cannot send cmd to target t%dL%d: %s",
4962 			    ap->a_target, ap->a_lun,
4963 			    "adapter dead");
4964 		}
4965 #endif
4966 		rval = TRAN_FATAL_ERROR;
4967 	}
4968 	mutex_exit(&softs->io_lock);
4969 	return (rval);
4970 }
4971 
4972 static int
4973 aac_tran_getcap(struct scsi_address *ap, char *cap, int whom)
4974 {
4975 	struct aac_softstate *softs = AAC_TRAN2SOFTS(ap->a_hba_tran);
4976 	struct aac_device *dvp;
4977 	int rval;
4978 
4979 	DBCALLED(softs, 2);
4980 
4981 	/* We don't allow inquiring about capabilities for other targets */
4982 	if (cap == NULL || whom == 0) {
4983 		AACDB_PRINT(softs, CE_WARN,
4984 		    "GetCap> %s not supported: whom=%d", cap, whom);
4985 		return (-1);
4986 	}
4987 
4988 	mutex_enter(&softs->io_lock);
4989 	dvp = AAC_DEV(softs, ap->a_target);
4990 	if (dvp == NULL || !AAC_DEV_IS_VALID(dvp)) {
4991 		mutex_exit(&softs->io_lock);
4992 		AACDB_PRINT_TRAN(softs, "Bad target t%dL%d to getcap",
4993 		    ap->a_target, ap->a_lun);
4994 		return (-1);
4995 	}
4996 
4997 	switch (scsi_hba_lookup_capstr(cap)) {
4998 	case SCSI_CAP_ARQ: /* auto request sense */
4999 		rval = 1;
5000 		break;
5001 	case SCSI_CAP_UNTAGGED_QING:
5002 	case SCSI_CAP_TAGGED_QING:
5003 		rval = 1;
5004 		break;
5005 	case SCSI_CAP_DMA_MAX:
5006 		rval = softs->dma_max;
5007 		break;
5008 	default:
5009 		rval = -1;
5010 		break;
5011 	}
5012 	mutex_exit(&softs->io_lock);
5013 
5014 	AACDB_PRINT_TRAN(softs, "GetCap> %s t%dL%d: rval=%d",
5015 	    cap, ap->a_target, ap->a_lun, rval);
5016 	return (rval);
5017 }
5018 
5019 /*ARGSUSED*/
5020 static int
5021 aac_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom)
5022 {
5023 	struct aac_softstate *softs = AAC_TRAN2SOFTS(ap->a_hba_tran);
5024 	struct aac_device *dvp;
5025 	int rval;
5026 
5027 	DBCALLED(softs, 2);
5028 
5029 	/* We don't allow inquiring about capabilities for other targets */
5030 	if (cap == NULL || whom == 0) {
5031 		AACDB_PRINT(softs, CE_WARN,
5032 		    "SetCap> %s not supported: whom=%d", cap, whom);
5033 		return (-1);
5034 	}
5035 
5036 	mutex_enter(&softs->io_lock);
5037 	dvp = AAC_DEV(softs, ap->a_target);
5038 	if (dvp == NULL || !AAC_DEV_IS_VALID(dvp)) {
5039 		mutex_exit(&softs->io_lock);
5040 		AACDB_PRINT_TRAN(softs, "Bad target t%dL%d to setcap",
5041 		    ap->a_target, ap->a_lun);
5042 		return (-1);
5043 	}
5044 
5045 	switch (scsi_hba_lookup_capstr(cap)) {
5046 	case SCSI_CAP_ARQ:
5047 		/* Force auto request sense */
5048 		rval = (value == 1) ? 1 : 0;
5049 		break;
5050 	case SCSI_CAP_UNTAGGED_QING:
5051 	case SCSI_CAP_TAGGED_QING:
5052 		rval = (value == 1) ? 1 : 0;
5053 		break;
5054 	default:
5055 		rval = -1;
5056 		break;
5057 	}
5058 	mutex_exit(&softs->io_lock);
5059 
5060 	AACDB_PRINT_TRAN(softs, "SetCap> %s t%dL%d val=%d: rval=%d",
5061 	    cap, ap->a_target, ap->a_lun, value, rval);
5062 	return (rval);
5063 }
5064 
5065 static void
5066 aac_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
5067 {
5068 	struct aac_cmd *acp = PKT2AC(pkt);
5069 
5070 	DBCALLED(NULL, 2);
5071 
5072 	if (acp->sgt) {
5073 		kmem_free(acp->sgt, sizeof (struct aac_sge) * \
5074 		    acp->left_cookien);
5075 	}
5076 	aac_free_dmamap(acp);
5077 	ASSERT(acp->slotp == NULL);
5078 	scsi_hba_pkt_free(ap, pkt);
5079 }
5080 
5081 int
5082 aac_cmd_dma_alloc(struct aac_softstate *softs, struct aac_cmd *acp,
5083     struct buf *bp, int flags, int (*cb)(), caddr_t arg)
5084 {
5085 	int kf = (cb == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
5086 	uint_t oldcookiec;
5087 	int bioerr;
5088 	int rval;
5089 
5090 	oldcookiec = acp->left_cookien;
5091 
5092 	/* Move window to build s/g map */
5093 	if (acp->total_nwin > 0) {
5094 		if (++acp->cur_win < acp->total_nwin) {
5095 			off_t off;
5096 			size_t len;
5097 
5098 			rval = ddi_dma_getwin(acp->buf_dma_handle, acp->cur_win,
5099 			    &off, &len, &acp->cookie, &acp->left_cookien);
5100 			if (rval == DDI_SUCCESS)
5101 				goto get_dma_cookies;
5102 			AACDB_PRINT(softs, CE_WARN,
5103 			    "ddi_dma_getwin() fail %d", rval);
5104 			return (AACERR);
5105 		}
5106 		AACDB_PRINT(softs, CE_WARN, "Nothing to transfer");
5107 		return (AACERR);
5108 	}
5109 
5110 	/* We need to transfer data, so we alloc DMA resources for this pkt */
5111 	if (bp && bp->b_bcount != 0 && !(acp->flags & AAC_CMD_DMA_VALID)) {
5112 		uint_t dma_flags = 0;
5113 		struct aac_sge *sge;
5114 
5115 		/*
5116 		 * We will still use this point to fake some
5117 		 * infomation in tran_start
5118 		 */
5119 		acp->bp = bp;
5120 
5121 		/* Set dma flags */
5122 		if (BUF_IS_READ(bp)) {
5123 			dma_flags |= DDI_DMA_READ;
5124 			acp->flags |= AAC_CMD_BUF_READ;
5125 		} else {
5126 			dma_flags |= DDI_DMA_WRITE;
5127 			acp->flags |= AAC_CMD_BUF_WRITE;
5128 		}
5129 		if (flags & PKT_CONSISTENT)
5130 			dma_flags |= DDI_DMA_CONSISTENT;
5131 		if (flags & PKT_DMA_PARTIAL)
5132 			dma_flags |= DDI_DMA_PARTIAL;
5133 
5134 		/* Alloc buf dma handle */
5135 		if (!acp->buf_dma_handle) {
5136 			rval = ddi_dma_alloc_handle(softs->devinfo_p,
5137 			    &softs->buf_dma_attr, cb, arg,
5138 			    &acp->buf_dma_handle);
5139 			if (rval != DDI_SUCCESS) {
5140 				AACDB_PRINT(softs, CE_WARN,
5141 				    "Can't allocate DMA handle, errno=%d",
5142 				    rval);
5143 				goto error_out;
5144 			}
5145 		}
5146 
5147 		/* Bind buf */
5148 		if (((uintptr_t)bp->b_un.b_addr & AAC_DMA_ALIGN_MASK) == 0) {
5149 			rval = ddi_dma_buf_bind_handle(acp->buf_dma_handle,
5150 			    bp, dma_flags, cb, arg, &acp->cookie,
5151 			    &acp->left_cookien);
5152 		} else {
5153 			size_t bufsz;
5154 
5155 			AACDB_PRINT_TRAN(softs,
5156 			    "non-aligned buffer: addr=0x%p, cnt=%lu",
5157 			    (void *)bp->b_un.b_addr, bp->b_bcount);
5158 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
5159 				bp_mapin(bp);
5160 
5161 			rval = ddi_dma_mem_alloc(acp->buf_dma_handle,
5162 			    AAC_ROUNDUP(bp->b_bcount, AAC_DMA_ALIGN),
5163 			    &softs->acc_attr, DDI_DMA_STREAMING,
5164 			    cb, arg, &acp->abp, &bufsz, &acp->abh);
5165 
5166 			if (rval != DDI_SUCCESS) {
5167 				AACDB_PRINT(softs, CE_NOTE,
5168 				    "Cannot alloc DMA to non-aligned buf");
5169 				bioerr = 0;
5170 				goto error_out;
5171 			}
5172 
5173 			if (acp->flags & AAC_CMD_BUF_WRITE)
5174 				ddi_rep_put8(acp->abh,
5175 				    (uint8_t *)bp->b_un.b_addr,
5176 				    (uint8_t *)acp->abp, bp->b_bcount,
5177 				    DDI_DEV_AUTOINCR);
5178 
5179 			rval = ddi_dma_addr_bind_handle(acp->buf_dma_handle,
5180 			    NULL, acp->abp, bufsz, dma_flags, cb, arg,
5181 			    &acp->cookie, &acp->left_cookien);
5182 		}
5183 
5184 		switch (rval) {
5185 		case DDI_DMA_PARTIAL_MAP:
5186 			if (ddi_dma_numwin(acp->buf_dma_handle,
5187 			    &acp->total_nwin) == DDI_FAILURE) {
5188 				AACDB_PRINT(softs, CE_WARN,
5189 				    "Cannot get number of DMA windows");
5190 				bioerr = 0;
5191 				goto error_out;
5192 			}
5193 			AACDB_PRINT_TRAN(softs, "buf bind, %d seg(s)",
5194 			    acp->left_cookien);
5195 			acp->cur_win = 0;
5196 			break;
5197 
5198 		case DDI_DMA_MAPPED:
5199 			AACDB_PRINT_TRAN(softs, "buf bind, %d seg(s)",
5200 			    acp->left_cookien);
5201 			acp->cur_win = 0;
5202 			acp->total_nwin = 1;
5203 			break;
5204 
5205 		case DDI_DMA_NORESOURCES:
5206 			bioerr = 0;
5207 			AACDB_PRINT(softs, CE_WARN,
5208 			    "Cannot bind buf for DMA: DDI_DMA_NORESOURCES");
5209 			goto error_out;
5210 		case DDI_DMA_BADATTR:
5211 		case DDI_DMA_NOMAPPING:
5212 			bioerr = EFAULT;
5213 			AACDB_PRINT(softs, CE_WARN,
5214 			    "Cannot bind buf for DMA: DDI_DMA_NOMAPPING");
5215 			goto error_out;
5216 		case DDI_DMA_TOOBIG:
5217 			bioerr = EINVAL;
5218 			AACDB_PRINT(softs, CE_WARN,
5219 			    "Cannot bind buf for DMA: DDI_DMA_TOOBIG(%d)",
5220 			    bp->b_bcount);
5221 			goto error_out;
5222 		default:
5223 			bioerr = EINVAL;
5224 			AACDB_PRINT(softs, CE_WARN,
5225 			    "Cannot bind buf for DMA: %d", rval);
5226 			goto error_out;
5227 		}
5228 		acp->flags |= AAC_CMD_DMA_VALID;
5229 
5230 get_dma_cookies:
5231 		ASSERT(acp->left_cookien > 0);
5232 		if (acp->left_cookien > softs->aac_sg_tablesize) {
5233 			AACDB_PRINT(softs, CE_NOTE, "large cookiec received %d",
5234 			    acp->left_cookien);
5235 			bioerr = EINVAL;
5236 			goto error_out;
5237 		}
5238 		if (oldcookiec != acp->left_cookien && acp->sgt != NULL) {
5239 			kmem_free(acp->sgt, sizeof (struct aac_sge) * \
5240 			    oldcookiec);
5241 			acp->sgt = NULL;
5242 		}
5243 		if (acp->sgt == NULL) {
5244 			acp->sgt = kmem_alloc(sizeof (struct aac_sge) * \
5245 			    acp->left_cookien, kf);
5246 			if (acp->sgt == NULL) {
5247 				AACDB_PRINT(softs, CE_WARN,
5248 				    "sgt kmem_alloc fail");
5249 				bioerr = ENOMEM;
5250 				goto error_out;
5251 			}
5252 		}
5253 
5254 		sge = &acp->sgt[0];
5255 		sge->bcount = acp->cookie.dmac_size;
5256 		sge->addr.ad64.lo = AAC_LS32(acp->cookie.dmac_laddress);
5257 		sge->addr.ad64.hi = AAC_MS32(acp->cookie.dmac_laddress);
5258 		acp->bcount = acp->cookie.dmac_size;
5259 		for (sge++; sge < &acp->sgt[acp->left_cookien]; sge++) {
5260 			ddi_dma_nextcookie(acp->buf_dma_handle, &acp->cookie);
5261 			sge->bcount = acp->cookie.dmac_size;
5262 			sge->addr.ad64.lo = AAC_LS32(acp->cookie.dmac_laddress);
5263 			sge->addr.ad64.hi = AAC_MS32(acp->cookie.dmac_laddress);
5264 			acp->bcount += acp->cookie.dmac_size;
5265 		}
5266 
5267 		/*
5268 		 * Note: The old DMA engine do not correctly handle
5269 		 * dma_attr_maxxfer attribute. So we have to ensure
5270 		 * it by ourself.
5271 		 */
5272 		if (acp->bcount > softs->buf_dma_attr.dma_attr_maxxfer) {
5273 			AACDB_PRINT(softs, CE_NOTE,
5274 			    "large xfer size received %d\n", acp->bcount);
5275 			bioerr = EINVAL;
5276 			goto error_out;
5277 		}
5278 
5279 		acp->total_xfer += acp->bcount;
5280 
5281 		if (acp->pkt) {
5282 			/* Return remaining byte count */
5283 			if (acp->total_xfer <= bp->b_bcount) {
5284 				acp->pkt->pkt_resid = bp->b_bcount - \
5285 				    acp->total_xfer;
5286 			} else {
5287 				/*
5288 				 * Allocated DMA size is greater than the buf
5289 				 * size of bp. This is caused by devices like
5290 				 * tape. we have extra bytes allocated, but
5291 				 * the packet residual has to stay correct.
5292 				 */
5293 				acp->pkt->pkt_resid = 0;
5294 			}
5295 			AACDB_PRINT_TRAN(softs,
5296 			    "bp=0x%p, xfered=%d/%d, resid=%d",
5297 			    (void *)bp->b_un.b_addr, (int)acp->total_xfer,
5298 			    (int)bp->b_bcount, (int)acp->pkt->pkt_resid);
5299 		}
5300 	}
5301 	return (AACOK);
5302 
5303 error_out:
5304 	bioerror(bp, bioerr);
5305 	return (AACERR);
5306 }
5307 
5308 static struct scsi_pkt *
5309 aac_tran_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
5310     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
5311     int (*callback)(), caddr_t arg)
5312 {
5313 	struct aac_softstate *softs = AAC_TRAN2SOFTS(ap->a_hba_tran);
5314 	struct aac_cmd *acp, *new_acp;
5315 
5316 	DBCALLED(softs, 2);
5317 
5318 	/* Allocate pkt */
5319 	if (pkt == NULL) {
5320 		int slen;
5321 
5322 		/* Force auto request sense */
5323 		slen = (statuslen > softs->slen) ? statuslen : softs->slen;
5324 		pkt = scsi_hba_pkt_alloc(softs->devinfo_p, ap, cmdlen,
5325 		    slen, tgtlen, sizeof (struct aac_cmd), callback, arg);
5326 		if (pkt == NULL) {
5327 			AACDB_PRINT(softs, CE_WARN, "Alloc scsi pkt failed");
5328 			return (NULL);
5329 		}
5330 		acp = new_acp = PKT2AC(pkt);
5331 		acp->pkt = pkt;
5332 		acp->cmdlen = cmdlen;
5333 
5334 		if (ap->a_target < AAC_MAX_LD) {
5335 			acp->dvp = &softs->containers[ap->a_target].dev;
5336 			acp->aac_cmd_fib = softs->aac_cmd_fib;
5337 			acp->ac_comp = aac_ld_complete;
5338 		} else {
5339 			_NOTE(ASSUMING_PROTECTED(softs->nondasds))
5340 
5341 			acp->dvp = &softs->nondasds[AAC_PD(ap->a_target)].dev;
5342 			acp->aac_cmd_fib = softs->aac_cmd_fib_scsi;
5343 			acp->ac_comp = aac_pd_complete;
5344 		}
5345 	} else {
5346 		acp = PKT2AC(pkt);
5347 		new_acp = NULL;
5348 	}
5349 
5350 	if (aac_cmd_dma_alloc(softs, acp, bp, flags, callback, arg) == AACOK)
5351 		return (pkt);
5352 
5353 	if (new_acp)
5354 		aac_tran_destroy_pkt(ap, pkt);
5355 	return (NULL);
5356 }
5357 
5358 /*
5359  * tran_sync_pkt(9E) - explicit DMA synchronization
5360  */
5361 /*ARGSUSED*/
5362 static void
5363 aac_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
5364 {
5365 	struct aac_cmd *acp = PKT2AC(pkt);
5366 
5367 	DBCALLED(NULL, 2);
5368 
5369 	if (aac_dma_sync_ac(acp) != AACOK)
5370 		ddi_fm_service_impact(
5371 		    (AAC_TRAN2SOFTS(ap->a_hba_tran))->devinfo_p,
5372 		    DDI_SERVICE_UNAFFECTED);
5373 }
5374 
5375 /*
5376  * tran_dmafree(9E) - deallocate DMA resources allocated for command
5377  */
5378 /*ARGSUSED*/
5379 static void
5380 aac_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
5381 {
5382 	struct aac_cmd *acp = PKT2AC(pkt);
5383 
5384 	DBCALLED(NULL, 2);
5385 
5386 	aac_free_dmamap(acp);
5387 }
5388 
5389 static int
5390 aac_do_quiesce(struct aac_softstate *softs)
5391 {
5392 	aac_hold_bus(softs, AAC_IOCMD_ASYNC);
5393 	if (softs->bus_ncmds[AAC_CMDQ_ASYNC]) {
5394 		aac_start_drain(softs);
5395 		do {
5396 			if (cv_wait_sig(&softs->drain_cv,
5397 			    &softs->io_lock) == 0) {
5398 				/* Quiesce has been interrupted */
5399 				aac_stop_drain(softs);
5400 				aac_unhold_bus(softs, AAC_IOCMD_ASYNC);
5401 				aac_start_waiting_io(softs);
5402 				return (AACERR);
5403 			}
5404 		} while (softs->bus_ncmds[AAC_CMDQ_ASYNC]);
5405 		aac_stop_drain(softs);
5406 	}
5407 
5408 	softs->state |= AAC_STATE_QUIESCED;
5409 	return (AACOK);
5410 }
5411 
5412 static int
5413 aac_tran_quiesce(dev_info_t *dip)
5414 {
5415 	struct aac_softstate *softs = AAC_DIP2SOFTS(dip);
5416 	int rval;
5417 
5418 	DBCALLED(softs, 1);
5419 
5420 	mutex_enter(&softs->io_lock);
5421 	if (aac_do_quiesce(softs) == AACOK)
5422 		rval = 0;
5423 	else
5424 		rval = 1;
5425 	mutex_exit(&softs->io_lock);
5426 	return (rval);
5427 }
5428 
5429 static int
5430 aac_do_unquiesce(struct aac_softstate *softs)
5431 {
5432 	softs->state &= ~AAC_STATE_QUIESCED;
5433 	aac_unhold_bus(softs, AAC_IOCMD_ASYNC);
5434 
5435 	aac_start_waiting_io(softs);
5436 	return (AACOK);
5437 }
5438 
5439 static int
5440 aac_tran_unquiesce(dev_info_t *dip)
5441 {
5442 	struct aac_softstate *softs = AAC_DIP2SOFTS(dip);
5443 	int rval;
5444 
5445 	DBCALLED(softs, 1);
5446 
5447 	mutex_enter(&softs->io_lock);
5448 	if (aac_do_unquiesce(softs) == AACOK)
5449 		rval = 0;
5450 	else
5451 		rval = 1;
5452 	mutex_exit(&softs->io_lock);
5453 	return (rval);
5454 }
5455 
5456 static int
5457 aac_hba_setup(struct aac_softstate *softs)
5458 {
5459 	scsi_hba_tran_t *hba_tran;
5460 	int rval;
5461 
5462 	hba_tran = scsi_hba_tran_alloc(softs->devinfo_p, SCSI_HBA_CANSLEEP);
5463 	if (hba_tran == NULL)
5464 		return (AACERR);
5465 	hba_tran->tran_hba_private = softs;
5466 	hba_tran->tran_tgt_init = aac_tran_tgt_init;
5467 	hba_tran->tran_tgt_free = aac_tran_tgt_free;
5468 	hba_tran->tran_tgt_probe = scsi_hba_probe;
5469 	hba_tran->tran_start = aac_tran_start;
5470 	hba_tran->tran_getcap = aac_tran_getcap;
5471 	hba_tran->tran_setcap = aac_tran_setcap;
5472 	hba_tran->tran_init_pkt = aac_tran_init_pkt;
5473 	hba_tran->tran_destroy_pkt = aac_tran_destroy_pkt;
5474 	hba_tran->tran_reset = aac_tran_reset;
5475 	hba_tran->tran_abort = aac_tran_abort;
5476 	hba_tran->tran_sync_pkt = aac_tran_sync_pkt;
5477 	hba_tran->tran_dmafree = aac_tran_dmafree;
5478 	hba_tran->tran_quiesce = aac_tran_quiesce;
5479 	hba_tran->tran_unquiesce = aac_tran_unquiesce;
5480 	hba_tran->tran_bus_config = aac_tran_bus_config;
5481 	rval = scsi_hba_attach_setup(softs->devinfo_p, &softs->buf_dma_attr,
5482 	    hba_tran, 0);
5483 	if (rval != DDI_SUCCESS) {
5484 		scsi_hba_tran_free(hba_tran);
5485 		AACDB_PRINT(softs, CE_WARN, "aac_hba_setup failed");
5486 		return (AACERR);
5487 	}
5488 
5489 	softs->hba_tran = hba_tran;
5490 	return (AACOK);
5491 }
5492 
5493 /*
5494  * FIB setup operations
5495  */
5496 
5497 /*
5498  * Init FIB header
5499  */
5500 static void
5501 aac_cmd_fib_header(struct aac_softstate *softs, struct aac_cmd *acp,
5502     uint16_t cmd)
5503 {
5504 	struct aac_slot *slotp = acp->slotp;
5505 	ddi_acc_handle_t acc = slotp->fib_acc_handle;
5506 	struct aac_fib *fibp = slotp->fibp;
5507 	uint32_t xfer_state;
5508 
5509 	xfer_state =
5510 	    AAC_FIBSTATE_HOSTOWNED |
5511 	    AAC_FIBSTATE_INITIALISED |
5512 	    AAC_FIBSTATE_EMPTY |
5513 	    AAC_FIBSTATE_FAST_RESPONSE | /* enable fast io */
5514 	    AAC_FIBSTATE_FROMHOST |
5515 	    AAC_FIBSTATE_REXPECTED |
5516 	    AAC_FIBSTATE_NORM;
5517 
5518 	if (!(acp->flags & AAC_CMD_SYNC))
5519 		xfer_state |= AAC_FIBSTATE_ASYNC;
5520 
5521 	ddi_put32(acc, &fibp->Header.XferState, xfer_state);
5522 	ddi_put16(acc, &fibp->Header.Command, cmd);
5523 	ddi_put8(acc, &fibp->Header.StructType, AAC_FIBTYPE_TFIB);
5524 	ddi_put8(acc, &fibp->Header.Flags, 0); /* don't care */
5525 	ddi_put16(acc, &fibp->Header.Size, acp->fib_size);
5526 	ddi_put16(acc, &fibp->Header.SenderSize, softs->aac_max_fib_size);
5527 	ddi_put32(acc, &fibp->Header.SenderFibAddress, (slotp->index << 2));
5528 	ddi_put32(acc, &fibp->Header.ReceiverFibAddress, slotp->fib_phyaddr);
5529 	ddi_put32(acc, &fibp->Header.SenderData, 0); /* don't care */
5530 }
5531 
5532 /*
5533  * Init FIB for raw IO command
5534  */
5535 static void
5536 aac_cmd_fib_rawio(struct aac_softstate *softs, struct aac_cmd *acp)
5537 {
5538 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5539 	struct aac_raw_io *io = (struct aac_raw_io *)&acp->slotp->fibp->data[0];
5540 	struct aac_sg_entryraw *sgp;
5541 	struct aac_sge *sge;
5542 
5543 	/* Calculate FIB size */
5544 	acp->fib_size = sizeof (struct aac_fib_header) + \
5545 	    sizeof (struct aac_raw_io) + (acp->left_cookien - 1) * \
5546 	    sizeof (struct aac_sg_entryraw);
5547 
5548 	aac_cmd_fib_header(softs, acp, RawIo);
5549 
5550 	ddi_put16(acc, &io->Flags, (acp->flags & AAC_CMD_BUF_READ) ? 1 : 0);
5551 	ddi_put16(acc, &io->BpTotal, 0);
5552 	ddi_put16(acc, &io->BpComplete, 0);
5553 
5554 	ddi_put32(acc, AAC_LO32(&io->BlockNumber), AAC_LS32(acp->blkno));
5555 	ddi_put32(acc, AAC_HI32(&io->BlockNumber), AAC_MS32(acp->blkno));
5556 	ddi_put16(acc, &io->ContainerId,
5557 	    ((struct aac_container *)acp->dvp)->cid);
5558 
5559 	/* Fill SG table */
5560 	ddi_put32(acc, &io->SgMapRaw.SgCount, acp->left_cookien);
5561 	ddi_put32(acc, &io->ByteCount, acp->bcount);
5562 
5563 	for (sge = &acp->sgt[0], sgp = &io->SgMapRaw.SgEntryRaw[0];
5564 	    sge < &acp->sgt[acp->left_cookien]; sge++, sgp++) {
5565 		ddi_put32(acc, AAC_LO32(&sgp->SgAddress), sge->addr.ad64.lo);
5566 		ddi_put32(acc, AAC_HI32(&sgp->SgAddress), sge->addr.ad64.hi);
5567 		ddi_put32(acc, &sgp->SgByteCount, sge->bcount);
5568 		sgp->Next = 0;
5569 		sgp->Prev = 0;
5570 		sgp->Flags = 0;
5571 	}
5572 }
5573 
5574 /* Init FIB for 64-bit block IO command */
5575 static void
5576 aac_cmd_fib_brw64(struct aac_softstate *softs, struct aac_cmd *acp)
5577 {
5578 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5579 	struct aac_blockread64 *br = (struct aac_blockread64 *) \
5580 	    &acp->slotp->fibp->data[0];
5581 	struct aac_sg_entry64 *sgp;
5582 	struct aac_sge *sge;
5583 
5584 	acp->fib_size = sizeof (struct aac_fib_header) + \
5585 	    sizeof (struct aac_blockread64) + (acp->left_cookien - 1) * \
5586 	    sizeof (struct aac_sg_entry64);
5587 
5588 	aac_cmd_fib_header(softs, acp, ContainerCommand64);
5589 
5590 	/*
5591 	 * The definitions for aac_blockread64 and aac_blockwrite64
5592 	 * are the same.
5593 	 */
5594 	ddi_put32(acc, &br->BlockNumber, (uint32_t)acp->blkno);
5595 	ddi_put16(acc, &br->ContainerId,
5596 	    ((struct aac_container *)acp->dvp)->cid);
5597 	ddi_put32(acc, &br->Command, (acp->flags & AAC_CMD_BUF_READ) ?
5598 	    VM_CtHostRead64 : VM_CtHostWrite64);
5599 	ddi_put16(acc, &br->Pad, 0);
5600 	ddi_put16(acc, &br->Flags, 0);
5601 
5602 	/* Fill SG table */
5603 	ddi_put32(acc, &br->SgMap64.SgCount, acp->left_cookien);
5604 	ddi_put16(acc, &br->SectorCount, acp->bcount / AAC_BLK_SIZE);
5605 
5606 	for (sge = &acp->sgt[0], sgp = &br->SgMap64.SgEntry64[0];
5607 	    sge < &acp->sgt[acp->left_cookien]; sge++, sgp++) {
5608 		ddi_put32(acc, AAC_LO32(&sgp->SgAddress), sge->addr.ad64.lo);
5609 		ddi_put32(acc, AAC_HI32(&sgp->SgAddress), sge->addr.ad64.hi);
5610 		ddi_put32(acc, &sgp->SgByteCount, sge->bcount);
5611 	}
5612 }
5613 
5614 /* Init FIB for block IO command */
5615 static void
5616 aac_cmd_fib_brw(struct aac_softstate *softs, struct aac_cmd *acp)
5617 {
5618 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5619 	struct aac_blockread *br = (struct aac_blockread *) \
5620 	    &acp->slotp->fibp->data[0];
5621 	struct aac_sg_entry *sgp;
5622 	struct aac_sge *sge = &acp->sgt[0];
5623 
5624 	if (acp->flags & AAC_CMD_BUF_READ) {
5625 		acp->fib_size = sizeof (struct aac_fib_header) + \
5626 		    sizeof (struct aac_blockread) + (acp->left_cookien - 1) * \
5627 		    sizeof (struct aac_sg_entry);
5628 
5629 		ddi_put32(acc, &br->Command, VM_CtBlockRead);
5630 		ddi_put32(acc, &br->SgMap.SgCount, acp->left_cookien);
5631 		sgp = &br->SgMap.SgEntry[0];
5632 	} else {
5633 		struct aac_blockwrite *bw = (struct aac_blockwrite *)br;
5634 
5635 		acp->fib_size = sizeof (struct aac_fib_header) + \
5636 		    sizeof (struct aac_blockwrite) + (acp->left_cookien - 1) * \
5637 		    sizeof (struct aac_sg_entry);
5638 
5639 		ddi_put32(acc, &bw->Command, VM_CtBlockWrite);
5640 		ddi_put32(acc, &bw->Stable, CUNSTABLE);
5641 		ddi_put32(acc, &bw->SgMap.SgCount, acp->left_cookien);
5642 		sgp = &bw->SgMap.SgEntry[0];
5643 	}
5644 	aac_cmd_fib_header(softs, acp, ContainerCommand);
5645 
5646 	/*
5647 	 * aac_blockread and aac_blockwrite have the similar
5648 	 * structure head, so use br for bw here
5649 	 */
5650 	ddi_put32(acc, &br->BlockNumber, (uint32_t)acp->blkno);
5651 	ddi_put32(acc, &br->ContainerId,
5652 	    ((struct aac_container *)acp->dvp)->cid);
5653 	ddi_put32(acc, &br->ByteCount, acp->bcount);
5654 
5655 	/* Fill SG table */
5656 	for (sge = &acp->sgt[0];
5657 	    sge < &acp->sgt[acp->left_cookien]; sge++, sgp++) {
5658 		ddi_put32(acc, &sgp->SgAddress, sge->addr.ad32);
5659 		ddi_put32(acc, &sgp->SgByteCount, sge->bcount);
5660 	}
5661 }
5662 
5663 /*ARGSUSED*/
5664 void
5665 aac_cmd_fib_copy(struct aac_softstate *softs, struct aac_cmd *acp)
5666 {
5667 	struct aac_slot *slotp = acp->slotp;
5668 	struct aac_fib *fibp = slotp->fibp;
5669 	ddi_acc_handle_t acc = slotp->fib_acc_handle;
5670 
5671 	ddi_rep_put8(acc, (uint8_t *)acp->fibp, (uint8_t *)fibp,
5672 	    acp->fib_size,   /* only copy data of needed length */
5673 	    DDI_DEV_AUTOINCR);
5674 	ddi_put32(acc, &fibp->Header.ReceiverFibAddress, slotp->fib_phyaddr);
5675 	ddi_put32(acc, &fibp->Header.SenderFibAddress, slotp->index << 2);
5676 }
5677 
5678 static void
5679 aac_cmd_fib_sync(struct aac_softstate *softs, struct aac_cmd *acp)
5680 {
5681 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5682 	struct aac_synchronize_command *sync =
5683 	    (struct aac_synchronize_command *)&acp->slotp->fibp->data[0];
5684 
5685 	acp->fib_size = AAC_FIB_SIZEOF(struct aac_synchronize_command);
5686 
5687 	aac_cmd_fib_header(softs, acp, ContainerCommand);
5688 	ddi_put32(acc, &sync->Command, VM_ContainerConfig);
5689 	ddi_put32(acc, &sync->Type, (uint32_t)CT_FLUSH_CACHE);
5690 	ddi_put32(acc, &sync->Cid, ((struct aac_container *)acp->dvp)->cid);
5691 	ddi_put32(acc, &sync->Count,
5692 	    sizeof (((struct aac_synchronize_reply *)0)->Data));
5693 }
5694 
5695 /*
5696  * Start/Stop unit (Power Management)
5697  */
5698 static void
5699 aac_cmd_fib_startstop(struct aac_softstate *softs, struct aac_cmd *acp)
5700 {
5701 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5702 	struct aac_Container *cmd =
5703 	    (struct aac_Container *)&acp->slotp->fibp->data[0];
5704 	union scsi_cdb *cdbp = (void *)acp->pkt->pkt_cdbp;
5705 
5706 	acp->fib_size = AAC_FIB_SIZEOF(struct aac_Container);
5707 
5708 	aac_cmd_fib_header(softs, acp, ContainerCommand);
5709 	bzero(cmd, sizeof (*cmd) - CT_PACKET_SIZE);
5710 	ddi_put32(acc, &cmd->Command, VM_ContainerConfig);
5711 	ddi_put32(acc, &cmd->CTCommand.command, CT_PM_DRIVER_SUPPORT);
5712 	ddi_put32(acc, &cmd->CTCommand.param[0], cdbp->cdb_opaque[4] & 1 ? \
5713 	    AAC_PM_DRIVERSUP_START_UNIT : AAC_PM_DRIVERSUP_STOP_UNIT);
5714 	ddi_put32(acc, &cmd->CTCommand.param[1],
5715 	    ((struct aac_container *)acp->dvp)->cid);
5716 	ddi_put32(acc, &cmd->CTCommand.param[2], cdbp->cdb_opaque[1] & 1);
5717 }
5718 
5719 /*
5720  * Init FIB for pass-through SCMD
5721  */
5722 static void
5723 aac_cmd_fib_srb(struct aac_cmd *acp)
5724 {
5725 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5726 	struct aac_srb *srb = (struct aac_srb *)&acp->slotp->fibp->data[0];
5727 	uint8_t *cdb;
5728 
5729 	ddi_put32(acc, &srb->function, SRBF_ExecuteScsi);
5730 	ddi_put32(acc, &srb->retry_limit, 0);
5731 	ddi_put32(acc, &srb->cdb_size, acp->cmdlen);
5732 	ddi_put32(acc, &srb->timeout, 0); /* use driver timeout */
5733 	if (acp->fibp == NULL) {
5734 		if (acp->flags & AAC_CMD_BUF_READ)
5735 			ddi_put32(acc, &srb->flags, SRB_DataIn);
5736 		else if (acp->flags & AAC_CMD_BUF_WRITE)
5737 			ddi_put32(acc, &srb->flags, SRB_DataOut);
5738 		ddi_put32(acc, &srb->channel,
5739 		    ((struct aac_nondasd *)acp->dvp)->bus);
5740 		ddi_put32(acc, &srb->id, ((struct aac_nondasd *)acp->dvp)->tid);
5741 		ddi_put32(acc, &srb->lun, 0);
5742 		cdb = acp->pkt->pkt_cdbp;
5743 	} else {
5744 		struct aac_srb *srb0 = (struct aac_srb *)&acp->fibp->data[0];
5745 
5746 		ddi_put32(acc, &srb->flags, srb0->flags);
5747 		ddi_put32(acc, &srb->channel, srb0->channel);
5748 		ddi_put32(acc, &srb->id, srb0->id);
5749 		ddi_put32(acc, &srb->lun, srb0->lun);
5750 		cdb = srb0->cdb;
5751 	}
5752 	ddi_rep_put8(acc, cdb, srb->cdb, acp->cmdlen, DDI_DEV_AUTOINCR);
5753 }
5754 
5755 static void
5756 aac_cmd_fib_scsi32(struct aac_softstate *softs, struct aac_cmd *acp)
5757 {
5758 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5759 	struct aac_srb *srb = (struct aac_srb *)&acp->slotp->fibp->data[0];
5760 	struct aac_sg_entry *sgp;
5761 	struct aac_sge *sge;
5762 
5763 	acp->fib_size = sizeof (struct aac_fib_header) + \
5764 	    sizeof (struct aac_srb) - sizeof (struct aac_sg_entry) + \
5765 	    acp->left_cookien * sizeof (struct aac_sg_entry);
5766 
5767 	/* Fill FIB and SRB headers, and copy cdb */
5768 	aac_cmd_fib_header(softs, acp, ScsiPortCommand);
5769 	aac_cmd_fib_srb(acp);
5770 
5771 	/* Fill SG table */
5772 	ddi_put32(acc, &srb->sg.SgCount, acp->left_cookien);
5773 	ddi_put32(acc, &srb->count, acp->bcount);
5774 
5775 	for (sge = &acp->sgt[0], sgp = &srb->sg.SgEntry[0];
5776 	    sge < &acp->sgt[acp->left_cookien]; sge++, sgp++) {
5777 		ddi_put32(acc, &sgp->SgAddress, sge->addr.ad32);
5778 		ddi_put32(acc, &sgp->SgByteCount, sge->bcount);
5779 	}
5780 }
5781 
5782 static void
5783 aac_cmd_fib_scsi64(struct aac_softstate *softs, struct aac_cmd *acp)
5784 {
5785 	ddi_acc_handle_t acc = acp->slotp->fib_acc_handle;
5786 	struct aac_srb *srb = (struct aac_srb *)&acp->slotp->fibp->data[0];
5787 	struct aac_sg_entry64 *sgp;
5788 	struct aac_sge *sge;
5789 
5790 	acp->fib_size = sizeof (struct aac_fib_header) + \
5791 	    sizeof (struct aac_srb) - sizeof (struct aac_sg_entry) + \
5792 	    acp->left_cookien * sizeof (struct aac_sg_entry64);
5793 
5794 	/* Fill FIB and SRB headers, and copy cdb */
5795 	aac_cmd_fib_header(softs, acp, ScsiPortCommandU64);
5796 	aac_cmd_fib_srb(acp);
5797 
5798 	/* Fill SG table */
5799 	ddi_put32(acc, &srb->sg.SgCount, acp->left_cookien);
5800 	ddi_put32(acc, &srb->count, acp->bcount);
5801 
5802 	for (sge = &acp->sgt[0],
5803 	    sgp = &((struct aac_sg_table64 *)&srb->sg)->SgEntry64[0];
5804 	    sge < &acp->sgt[acp->left_cookien]; sge++, sgp++) {
5805 		ddi_put32(acc, AAC_LO32(&sgp->SgAddress), sge->addr.ad64.lo);
5806 		ddi_put32(acc, AAC_HI32(&sgp->SgAddress), sge->addr.ad64.hi);
5807 		ddi_put32(acc, &sgp->SgByteCount, sge->bcount);
5808 	}
5809 }
5810 
5811 static int
5812 aac_cmd_slot_bind(struct aac_softstate *softs, struct aac_cmd *acp)
5813 {
5814 	struct aac_slot *slotp;
5815 
5816 	if (slotp = aac_get_slot(softs)) {
5817 		acp->slotp = slotp;
5818 		slotp->acp = acp;
5819 		acp->aac_cmd_fib(softs, acp);
5820 		(void) ddi_dma_sync(slotp->fib_dma_handle, 0, 0,
5821 		    DDI_DMA_SYNC_FORDEV);
5822 		return (AACOK);
5823 	}
5824 	return (AACERR);
5825 }
5826 
5827 static int
5828 aac_bind_io(struct aac_softstate *softs, struct aac_cmd *acp)
5829 {
5830 	struct aac_device *dvp = acp->dvp;
5831 	int q = AAC_CMDQ(acp);
5832 
5833 	if (softs->bus_ncmds[q] < softs->bus_throttle[q]) {
5834 		if (dvp) {
5835 			if (dvp->ncmds[q] < dvp->throttle[q]) {
5836 				if (!(acp->flags & AAC_CMD_NTAG) ||
5837 				    dvp->ncmds[q] == 0) {
5838 					return (aac_cmd_slot_bind(softs, acp));
5839 				}
5840 				ASSERT(q == AAC_CMDQ_ASYNC);
5841 				aac_set_throttle(softs, dvp, AAC_CMDQ_ASYNC,
5842 				    AAC_THROTTLE_DRAIN);
5843 			}
5844 		} else {
5845 			return (aac_cmd_slot_bind(softs, acp));
5846 		}
5847 	}
5848 	return (AACERR);
5849 }
5850 
5851 static int
5852 aac_sync_fib_slot_bind(struct aac_softstate *softs, struct aac_cmd *acp)
5853 {
5854 	struct aac_slot *slotp;
5855 
5856 	while (softs->sync_ac.slotp)
5857 		cv_wait(&softs->sync_fib_cv, &softs->io_lock);
5858 
5859 	if (slotp = aac_get_slot(softs)) {
5860 		ASSERT(acp->slotp == NULL);
5861 
5862 		acp->slotp = slotp;
5863 		slotp->acp = acp;
5864 		return (AACOK);
5865 	}
5866 	return (AACERR);
5867 }
5868 
5869 static void
5870 aac_sync_fib_slot_release(struct aac_softstate *softs, struct aac_cmd *acp)
5871 {
5872 	ASSERT(acp->slotp);
5873 
5874 	aac_release_slot(softs, acp->slotp);
5875 	acp->slotp->acp = NULL;
5876 	acp->slotp = NULL;
5877 
5878 	cv_signal(&softs->sync_fib_cv);
5879 }
5880 
5881 static void
5882 aac_start_io(struct aac_softstate *softs, struct aac_cmd *acp)
5883 {
5884 	struct aac_slot *slotp = acp->slotp;
5885 	int q = AAC_CMDQ(acp);
5886 	int rval;
5887 
5888 	/* Set ac and pkt */
5889 	if (acp->pkt) { /* ac from ioctl has no pkt */
5890 		acp->pkt->pkt_state |=
5891 		    STATE_GOT_BUS | STATE_GOT_TARGET | STATE_SENT_CMD;
5892 	}
5893 	if (acp->timeout) /* 0 indicates no timeout */
5894 		acp->timeout += aac_timebase + aac_tick;
5895 
5896 	if (acp->dvp)
5897 		acp->dvp->ncmds[q]++;
5898 	softs->bus_ncmds[q]++;
5899 	aac_cmd_enqueue(&softs->q_busy, acp);
5900 
5901 	AACDB_PRINT_FIB(softs, slotp);
5902 
5903 	if (softs->flags & AAC_FLAGS_NEW_COMM) {
5904 		rval = aac_send_command(softs, slotp);
5905 	} else {
5906 		/*
5907 		 * If fib can not be enqueued, the adapter is in an abnormal
5908 		 * state, there will be no interrupt to us.
5909 		 */
5910 		rval = aac_fib_enqueue(softs, AAC_ADAP_NORM_CMD_Q,
5911 		    slotp->fib_phyaddr, acp->fib_size);
5912 	}
5913 
5914 	if (aac_check_dma_handle(slotp->fib_dma_handle) != DDI_SUCCESS)
5915 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_UNAFFECTED);
5916 
5917 	/*
5918 	 * NOTE: We send command only when slots availabe, so should never
5919 	 * reach here.
5920 	 */
5921 	if (rval != AACOK) {
5922 		AACDB_PRINT(softs, CE_NOTE, "SCMD send failed");
5923 		if (acp->pkt) {
5924 			acp->pkt->pkt_state &= ~STATE_SENT_CMD;
5925 			aac_set_pkt_reason(softs, acp, CMD_INCOMPLETE, 0);
5926 		}
5927 		aac_end_io(softs, acp);
5928 		if (!(acp->flags & (AAC_CMD_NO_INTR | AAC_CMD_NO_CB)))
5929 			ddi_trigger_softintr(softs->softint_id);
5930 	}
5931 }
5932 
5933 static void
5934 aac_start_waitq(struct aac_softstate *softs, struct aac_cmd_queue *q)
5935 {
5936 	struct aac_cmd *acp, *next_acp;
5937 
5938 	/* Serve as many waiting io's as possible */
5939 	for (acp = q->q_head; acp; acp = next_acp) {
5940 		next_acp = acp->next;
5941 		if (aac_bind_io(softs, acp) == AACOK) {
5942 			aac_cmd_delete(q, acp);
5943 			aac_start_io(softs, acp);
5944 		}
5945 		if (softs->free_io_slot_head == NULL)
5946 			break;
5947 	}
5948 }
5949 
5950 static void
5951 aac_start_waiting_io(struct aac_softstate *softs)
5952 {
5953 	/*
5954 	 * Sync FIB io is served before async FIB io so that io requests
5955 	 * sent by interactive userland commands get responded asap.
5956 	 */
5957 	if (softs->q_wait[AAC_CMDQ_SYNC].q_head)
5958 		aac_start_waitq(softs, &softs->q_wait[AAC_CMDQ_SYNC]);
5959 	if (softs->q_wait[AAC_CMDQ_ASYNC].q_head)
5960 		aac_start_waitq(softs, &softs->q_wait[AAC_CMDQ_ASYNC]);
5961 }
5962 
5963 static void
5964 aac_drain_comp_q(struct aac_softstate *softs)
5965 {
5966 	struct aac_cmd *acp;
5967 	struct scsi_pkt *pkt;
5968 
5969 	/*CONSTCOND*/
5970 	while (1) {
5971 		mutex_enter(&softs->q_comp_mutex);
5972 		acp = aac_cmd_dequeue(&softs->q_comp);
5973 		mutex_exit(&softs->q_comp_mutex);
5974 		if (acp != NULL) {
5975 			ASSERT(acp->pkt != NULL);
5976 			pkt = acp->pkt;
5977 
5978 			if (pkt->pkt_reason == CMD_CMPLT) {
5979 				/*
5980 				 * Consistent packets need to be sync'ed first
5981 				 */
5982 				if ((acp->flags & AAC_CMD_CONSISTENT) &&
5983 				    (acp->flags & AAC_CMD_BUF_READ)) {
5984 					if (aac_dma_sync_ac(acp) != AACOK) {
5985 						ddi_fm_service_impact(
5986 						    softs->devinfo_p,
5987 						    DDI_SERVICE_UNAFFECTED);
5988 						pkt->pkt_reason = CMD_TRAN_ERR;
5989 						pkt->pkt_statistics = 0;
5990 					}
5991 				}
5992 				if ((aac_check_acc_handle(softs-> \
5993 				    comm_space_acc_handle) != DDI_SUCCESS) ||
5994 				    (aac_check_acc_handle(softs-> \
5995 				    pci_mem_handle) != DDI_SUCCESS)) {
5996 					ddi_fm_service_impact(softs->devinfo_p,
5997 					    DDI_SERVICE_UNAFFECTED);
5998 					ddi_fm_acc_err_clear(softs-> \
5999 					    pci_mem_handle, DDI_FME_VER0);
6000 					pkt->pkt_reason = CMD_TRAN_ERR;
6001 					pkt->pkt_statistics = 0;
6002 				}
6003 				if (aac_check_dma_handle(softs-> \
6004 				    comm_space_dma_handle) != DDI_SUCCESS) {
6005 					ddi_fm_service_impact(softs->devinfo_p,
6006 					    DDI_SERVICE_UNAFFECTED);
6007 					pkt->pkt_reason = CMD_TRAN_ERR;
6008 					pkt->pkt_statistics = 0;
6009 				}
6010 			}
6011 			scsi_hba_pkt_comp(pkt);
6012 		} else {
6013 			break;
6014 		}
6015 	}
6016 }
6017 
6018 static int
6019 aac_alloc_fib(struct aac_softstate *softs, struct aac_slot *slotp)
6020 {
6021 	size_t rlen;
6022 	ddi_dma_cookie_t cookie;
6023 	uint_t cookien;
6024 
6025 	/* Allocate FIB dma resource */
6026 	if (ddi_dma_alloc_handle(
6027 	    softs->devinfo_p,
6028 	    &softs->addr_dma_attr,
6029 	    DDI_DMA_SLEEP,
6030 	    NULL,
6031 	    &slotp->fib_dma_handle) != DDI_SUCCESS) {
6032 		AACDB_PRINT(softs, CE_WARN,
6033 		    "Cannot alloc dma handle for slot fib area");
6034 		goto error;
6035 	}
6036 	if (ddi_dma_mem_alloc(
6037 	    slotp->fib_dma_handle,
6038 	    softs->aac_max_fib_size,
6039 	    &softs->acc_attr,
6040 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
6041 	    DDI_DMA_SLEEP,
6042 	    NULL,
6043 	    (caddr_t *)&slotp->fibp,
6044 	    &rlen,
6045 	    &slotp->fib_acc_handle) != DDI_SUCCESS) {
6046 		AACDB_PRINT(softs, CE_WARN,
6047 		    "Cannot alloc mem for slot fib area");
6048 		goto error;
6049 	}
6050 	if (ddi_dma_addr_bind_handle(
6051 	    slotp->fib_dma_handle,
6052 	    NULL,
6053 	    (caddr_t)slotp->fibp,
6054 	    softs->aac_max_fib_size,
6055 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
6056 	    DDI_DMA_SLEEP,
6057 	    NULL,
6058 	    &cookie,
6059 	    &cookien) != DDI_DMA_MAPPED) {
6060 		AACDB_PRINT(softs, CE_WARN,
6061 		    "dma bind failed for slot fib area");
6062 		goto error;
6063 	}
6064 
6065 	/* Check dma handles allocated in fib attach */
6066 	if (aac_check_dma_handle(slotp->fib_dma_handle) != DDI_SUCCESS) {
6067 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
6068 		goto error;
6069 	}
6070 
6071 	/* Check acc handles allocated in fib attach */
6072 	if (aac_check_acc_handle(slotp->fib_acc_handle) != DDI_SUCCESS) {
6073 		ddi_fm_service_impact(softs->devinfo_p, DDI_SERVICE_LOST);
6074 		goto error;
6075 	}
6076 
6077 	slotp->fib_phyaddr = cookie.dmac_laddress;
6078 	return (AACOK);
6079 
6080 error:
6081 	if (slotp->fib_acc_handle) {
6082 		ddi_dma_mem_free(&slotp->fib_acc_handle);
6083 		slotp->fib_acc_handle = NULL;
6084 	}
6085 	if (slotp->fib_dma_handle) {
6086 		ddi_dma_free_handle(&slotp->fib_dma_handle);
6087 		slotp->fib_dma_handle = NULL;
6088 	}
6089 	return (AACERR);
6090 }
6091 
6092 static void
6093 aac_free_fib(struct aac_slot *slotp)
6094 {
6095 	(void) ddi_dma_unbind_handle(slotp->fib_dma_handle);
6096 	ddi_dma_mem_free(&slotp->fib_acc_handle);
6097 	slotp->fib_acc_handle = NULL;
6098 	ddi_dma_free_handle(&slotp->fib_dma_handle);
6099 	slotp->fib_dma_handle = NULL;
6100 	slotp->fib_phyaddr = 0;
6101 }
6102 
6103 static void
6104 aac_alloc_fibs(struct aac_softstate *softs)
6105 {
6106 	int i;
6107 	struct aac_slot *slotp;
6108 
6109 	for (i = 0; i < softs->total_slots &&
6110 	    softs->total_fibs < softs->total_slots; i++) {
6111 		slotp = &(softs->io_slot[i]);
6112 		if (slotp->fib_phyaddr)
6113 			continue;
6114 		if (aac_alloc_fib(softs, slotp) != AACOK)
6115 			break;
6116 
6117 		/* Insert the slot to the free slot list */
6118 		aac_release_slot(softs, slotp);
6119 		softs->total_fibs++;
6120 	}
6121 }
6122 
6123 static void
6124 aac_destroy_fibs(struct aac_softstate *softs)
6125 {
6126 	struct aac_slot *slotp;
6127 
6128 	while ((slotp = softs->free_io_slot_head) != NULL) {
6129 		ASSERT(slotp->fib_phyaddr);
6130 		softs->free_io_slot_head = slotp->next;
6131 		aac_free_fib(slotp);
6132 		ASSERT(slotp->index == (slotp - softs->io_slot));
6133 		softs->total_fibs--;
6134 	}
6135 	ASSERT(softs->total_fibs == 0);
6136 }
6137 
6138 static int
6139 aac_create_slots(struct aac_softstate *softs)
6140 {
6141 	int i;
6142 
6143 	softs->total_slots = softs->aac_max_fibs;
6144 	softs->io_slot = kmem_zalloc(sizeof (struct aac_slot) * \
6145 	    softs->total_slots, KM_SLEEP);
6146 	if (softs->io_slot == NULL) {
6147 		AACDB_PRINT(softs, CE_WARN, "Cannot allocate slot");
6148 		return (AACERR);
6149 	}
6150 	for (i = 0; i < softs->total_slots; i++)
6151 		softs->io_slot[i].index = i;
6152 	softs->free_io_slot_head = NULL;
6153 	softs->total_fibs = 0;
6154 	return (AACOK);
6155 }
6156 
6157 static void
6158 aac_destroy_slots(struct aac_softstate *softs)
6159 {
6160 	ASSERT(softs->free_io_slot_head == NULL);
6161 
6162 	kmem_free(softs->io_slot, sizeof (struct aac_slot) * \
6163 	    softs->total_slots);
6164 	softs->io_slot = NULL;
6165 	softs->total_slots = 0;
6166 }
6167 
6168 struct aac_slot *
6169 aac_get_slot(struct aac_softstate *softs)
6170 {
6171 	struct aac_slot *slotp;
6172 
6173 	if ((slotp = softs->free_io_slot_head) != NULL) {
6174 		softs->free_io_slot_head = slotp->next;
6175 		slotp->next = NULL;
6176 	}
6177 	return (slotp);
6178 }
6179 
6180 static void
6181 aac_release_slot(struct aac_softstate *softs, struct aac_slot *slotp)
6182 {
6183 	ASSERT((slotp->index >= 0) && (slotp->index < softs->total_slots));
6184 	ASSERT(slotp == &softs->io_slot[slotp->index]);
6185 
6186 	slotp->acp = NULL;
6187 	slotp->next = softs->free_io_slot_head;
6188 	softs->free_io_slot_head = slotp;
6189 }
6190 
6191 int
6192 aac_do_io(struct aac_softstate *softs, struct aac_cmd *acp)
6193 {
6194 	if (aac_bind_io(softs, acp) == AACOK)
6195 		aac_start_io(softs, acp);
6196 	else
6197 		aac_cmd_enqueue(&softs->q_wait[AAC_CMDQ(acp)], acp);
6198 
6199 	if (!(acp->flags & (AAC_CMD_NO_CB | AAC_CMD_NO_INTR)))
6200 		return (TRAN_ACCEPT);
6201 	/*
6202 	 * Because sync FIB is always 512 bytes and used for critical
6203 	 * functions, async FIB is used for poll IO.
6204 	 */
6205 	if (acp->flags & AAC_CMD_NO_INTR) {
6206 		if (aac_do_poll_io(softs, acp) == AACOK)
6207 			return (TRAN_ACCEPT);
6208 	} else {
6209 		if (aac_do_sync_io(softs, acp) == AACOK)
6210 			return (TRAN_ACCEPT);
6211 	}
6212 	return (TRAN_BADPKT);
6213 }
6214 
6215 static int
6216 aac_do_poll_io(struct aac_softstate *softs, struct aac_cmd *acp)
6217 {
6218 	int (*intr_handler)(struct aac_softstate *);
6219 
6220 	/*
6221 	 * Interrupt is disabled, we have to poll the adapter by ourselves.
6222 	 */
6223 	intr_handler = (softs->flags & AAC_FLAGS_NEW_COMM) ?
6224 	    aac_process_intr_new : aac_process_intr_old;
6225 	while (!(acp->flags & (AAC_CMD_CMPLT | AAC_CMD_ABORT))) {
6226 		int i = AAC_POLL_TIME * 1000;
6227 
6228 		AAC_BUSYWAIT((intr_handler(softs) != AAC_DB_RESPONSE_READY), i);
6229 		if (i == 0)
6230 			aac_cmd_timeout(softs, acp);
6231 	}
6232 
6233 	ddi_trigger_softintr(softs->softint_id);
6234 
6235 	if ((acp->flags & AAC_CMD_CMPLT) && !(acp->flags & AAC_CMD_ERR))
6236 		return (AACOK);
6237 	return (AACERR);
6238 }
6239 
6240 static int
6241 aac_do_sync_io(struct aac_softstate *softs, struct aac_cmd *acp)
6242 {
6243 	ASSERT(softs && acp);
6244 
6245 	while (!(acp->flags & (AAC_CMD_CMPLT | AAC_CMD_ABORT)))
6246 		cv_wait(&softs->event, &softs->io_lock);
6247 
6248 	if (acp->flags & AAC_CMD_CMPLT)
6249 		return (AACOK);
6250 	return (AACERR);
6251 }
6252 
6253 static int
6254 aac_dma_sync_ac(struct aac_cmd *acp)
6255 {
6256 	if (acp->buf_dma_handle) {
6257 		if (acp->flags & AAC_CMD_BUF_WRITE) {
6258 			if (acp->abp != NULL)
6259 				ddi_rep_put8(acp->abh,
6260 				    (uint8_t *)acp->bp->b_un.b_addr,
6261 				    (uint8_t *)acp->abp, acp->bp->b_bcount,
6262 				    DDI_DEV_AUTOINCR);
6263 			(void) ddi_dma_sync(acp->buf_dma_handle, 0, 0,
6264 			    DDI_DMA_SYNC_FORDEV);
6265 		} else {
6266 			(void) ddi_dma_sync(acp->buf_dma_handle, 0, 0,
6267 			    DDI_DMA_SYNC_FORCPU);
6268 			if (aac_check_dma_handle(acp->buf_dma_handle) !=
6269 			    DDI_SUCCESS)
6270 				return (AACERR);
6271 			if (acp->abp != NULL)
6272 				ddi_rep_get8(acp->abh,
6273 				    (uint8_t *)acp->bp->b_un.b_addr,
6274 				    (uint8_t *)acp->abp, acp->bp->b_bcount,
6275 				    DDI_DEV_AUTOINCR);
6276 		}
6277 	}
6278 	return (AACOK);
6279 }
6280 
6281 /*
6282  * Copy AIF from adapter to the empty AIF slot and inform AIF threads
6283  */
6284 static void
6285 aac_save_aif(struct aac_softstate *softs, ddi_acc_handle_t acc,
6286     struct aac_fib *fibp0, int fib_size0)
6287 {
6288 	struct aac_fib *fibp;	/* FIB in AIF queue */
6289 	int fib_size;
6290 	uint16_t fib_command;
6291 	int current, next;
6292 
6293 	/* Ignore non AIF messages */
6294 	fib_command = ddi_get16(acc, &fibp0->Header.Command);
6295 	if (fib_command != AifRequest) {
6296 		cmn_err(CE_WARN, "!Unknown command from controller");
6297 		return;
6298 	}
6299 
6300 	mutex_enter(&softs->aifq_mutex);
6301 
6302 	/* Save AIF */
6303 	fibp = &softs->aifq[softs->aifq_idx].d;
6304 	fib_size = (fib_size0 > AAC_FIB_SIZE) ? AAC_FIB_SIZE : fib_size0;
6305 	ddi_rep_get8(acc, (uint8_t *)fibp, (uint8_t *)fibp0, fib_size,
6306 	    DDI_DEV_AUTOINCR);
6307 
6308 	if (aac_check_acc_handle(softs->pci_mem_handle) != DDI_SUCCESS) {
6309 		ddi_fm_service_impact(softs->devinfo_p,
6310 		    DDI_SERVICE_UNAFFECTED);
6311 		mutex_exit(&softs->aifq_mutex);
6312 		return;
6313 	}
6314 
6315 	AACDB_PRINT_AIF(softs, (struct aac_aif_command *)&fibp->data[0]);
6316 
6317 	/* Modify AIF contexts */
6318 	current = softs->aifq_idx;
6319 	next = (current + 1) % AAC_AIFQ_LENGTH;
6320 	if (next == 0) {
6321 		struct aac_fib_context *ctx_p;
6322 
6323 		softs->aifq_wrap = 1;
6324 		for (ctx_p = softs->fibctx_p; ctx_p; ctx_p = ctx_p->next) {
6325 			if (next == ctx_p->ctx_idx) {
6326 				ctx_p->ctx_flags |= AAC_CTXFLAG_FILLED;
6327 			} else if (current == ctx_p->ctx_idx &&
6328 			    (ctx_p->ctx_flags & AAC_CTXFLAG_FILLED)) {
6329 				ctx_p->ctx_idx = next;
6330 				ctx_p->ctx_overrun++;
6331 			}
6332 		}
6333 	}
6334 	softs->aifq_idx = next;
6335 
6336 	/* Wakeup AIF threads */
6337 	cv_broadcast(&softs->aifq_cv);
6338 	mutex_exit(&softs->aifq_mutex);
6339 
6340 	/* Wakeup event thread to handle aif */
6341 	aac_event_disp(softs, AAC_EVENT_AIF);
6342 }
6343 
6344 static int
6345 aac_return_aif_common(struct aac_softstate *softs, struct aac_fib_context *ctx,
6346     struct aac_fib **fibpp)
6347 {
6348 	int current;
6349 
6350 	current = ctx->ctx_idx;
6351 	if (current == softs->aifq_idx &&
6352 	    !(ctx->ctx_flags & AAC_CTXFLAG_FILLED))
6353 		return (EAGAIN); /* Empty */
6354 
6355 	*fibpp = &softs->aifq[current].d;
6356 
6357 	ctx->ctx_flags &= ~AAC_CTXFLAG_FILLED;
6358 	ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
6359 	return (0);
6360 }
6361 
6362 int
6363 aac_return_aif(struct aac_softstate *softs, struct aac_fib_context *ctx,
6364     struct aac_fib **fibpp)
6365 {
6366 	int rval;
6367 
6368 	mutex_enter(&softs->aifq_mutex);
6369 	rval = aac_return_aif_common(softs, ctx, fibpp);
6370 	mutex_exit(&softs->aifq_mutex);
6371 	return (rval);
6372 }
6373 
6374 int
6375 aac_return_aif_wait(struct aac_softstate *softs, struct aac_fib_context *ctx,
6376     struct aac_fib **fibpp)
6377 {
6378 	int rval;
6379 
6380 	mutex_enter(&softs->aifq_mutex);
6381 	rval = aac_return_aif_common(softs, ctx, fibpp);
6382 	if (rval == EAGAIN) {
6383 		AACDB_PRINT(softs, CE_NOTE, "Waiting for AIF");
6384 		rval = cv_wait_sig(&softs->aifq_cv, &softs->aifq_mutex);
6385 	}
6386 	mutex_exit(&softs->aifq_mutex);
6387 	return ((rval > 0) ? 0 : EINTR);
6388 }
6389 
6390 /*
6391  * The following function comes from Adaptec:
6392  *
6393  * When driver sees a particular event that means containers are changed, it
6394  * will rescan containers. However a change may not be complete until some
6395  * other event is received. For example, creating or deleting an array will
6396  * incur as many as six AifEnConfigChange events which would generate six
6397  * container rescans. To diminish rescans, driver set a flag to wait for
6398  * another particular event. When sees that events come in, it will do rescan.
6399  */
6400 static int
6401 aac_handle_aif(struct aac_softstate *softs, struct aac_aif_command *aif)
6402 {
6403 	ddi_acc_handle_t acc = softs->comm_space_acc_handle;
6404 	int en_type;
6405 	int devcfg_needed;
6406 
6407 	devcfg_needed = 0;
6408 	en_type = LE_32((uint32_t)aif->data.EN.type);
6409 
6410 	switch (LE_32((uint32_t)aif->command)) {
6411 	case AifCmdDriverNotify: {
6412 		int cid = LE_32(aif->data.EN.data.ECC.container[0]);
6413 
6414 		switch (en_type) {
6415 		case AifDenMorphComplete:
6416 		case AifDenVolumeExtendComplete:
6417 			if (AAC_DEV_IS_VALID(&softs->containers[cid].dev))
6418 				softs->devcfg_wait_on = AifEnConfigChange;
6419 			break;
6420 		}
6421 		if (softs->devcfg_wait_on == en_type)
6422 			devcfg_needed = 1;
6423 		break;
6424 	}
6425 
6426 	case AifCmdEventNotify:
6427 		switch (en_type) {
6428 		case AifEnAddContainer:
6429 		case AifEnDeleteContainer:
6430 			softs->devcfg_wait_on = AifEnConfigChange;
6431 			break;
6432 		case AifEnContainerChange:
6433 			if (!softs->devcfg_wait_on)
6434 				softs->devcfg_wait_on = AifEnConfigChange;
6435 			break;
6436 		case AifEnContainerEvent:
6437 			if (ddi_get32(acc, &aif-> \
6438 			    data.EN.data.ECE.eventType) == CT_PUP_MISSING_DRIVE)
6439 				devcfg_needed = 1;
6440 			break;
6441 		}
6442 		if (softs->devcfg_wait_on == en_type)
6443 			devcfg_needed = 1;
6444 		break;
6445 
6446 	case AifCmdJobProgress:
6447 		if (LE_32((uint32_t)aif->data.PR[0].jd.type) == AifJobCtrZero) {
6448 			int pr_status;
6449 			uint32_t pr_ftick, pr_ctick;
6450 
6451 			pr_status = LE_32((uint32_t)aif->data.PR[0].status);
6452 			pr_ctick = LE_32(aif->data.PR[0].currentTick);
6453 			pr_ftick = LE_32(aif->data.PR[0].finalTick);
6454 
6455 			if ((pr_ctick == pr_ftick) ||
6456 			    (pr_status == AifJobStsSuccess))
6457 				softs->devcfg_wait_on = AifEnContainerChange;
6458 			else if ((pr_ctick == 0) &&
6459 			    (pr_status == AifJobStsRunning))
6460 				softs->devcfg_wait_on = AifEnContainerChange;
6461 		}
6462 		break;
6463 	}
6464 
6465 	if (devcfg_needed) {
6466 		softs->devcfg_wait_on = 0;
6467 		(void) aac_probe_containers(softs);
6468 	}
6469 
6470 	return (AACOK);
6471 }
6472 
6473 
6474 /*
6475  * Check and handle AIF events
6476  */
6477 static void
6478 aac_aif_event(struct aac_softstate *softs)
6479 {
6480 	struct aac_fib *fibp;
6481 
6482 	/*CONSTCOND*/
6483 	while (1) {
6484 		if (aac_return_aif(softs, &softs->aifctx, &fibp) != 0)
6485 			break; /* No more AIFs to handle, end loop */
6486 
6487 		/* AIF overrun, array create/delete may missed. */
6488 		if (softs->aifctx.ctx_overrun) {
6489 			softs->aifctx.ctx_overrun = 0;
6490 		}
6491 
6492 		/* AIF received, handle it */
6493 		struct aac_aif_command *aifp =
6494 		    (struct aac_aif_command *)&fibp->data[0];
6495 		uint32_t aif_command = LE_32((uint32_t)aifp->command);
6496 
6497 		if (aif_command == AifCmdDriverNotify ||
6498 		    aif_command == AifCmdEventNotify ||
6499 		    aif_command == AifCmdJobProgress)
6500 			(void) aac_handle_aif(softs, aifp);
6501 	}
6502 }
6503 
6504 /*
6505  * Timeout recovery
6506  */
6507 /*ARGSUSED*/
6508 static void
6509 aac_cmd_timeout(struct aac_softstate *softs, struct aac_cmd *acp)
6510 {
6511 #ifdef DEBUG
6512 	acp->fib_flags |= AACDB_FLAGS_FIB_TIMEOUT;
6513 	AACDB_PRINT(softs, CE_WARN, "acp %p timed out", acp);
6514 	AACDB_PRINT_FIB(softs, acp->slotp);
6515 #endif
6516 
6517 	/*
6518 	 * Besides the firmware in unhealthy state, an overloaded
6519 	 * adapter may also incur pkt timeout.
6520 	 * There is a chance for an adapter with a slower IOP to take
6521 	 * longer than 60 seconds to process the commands, such as when
6522 	 * to perform IOs. So the adapter is doing a build on a RAID-5
6523 	 * while being required longer completion times should be
6524 	 * tolerated.
6525 	 */
6526 	switch (aac_do_reset(softs)) {
6527 	case AAC_IOP_RESET_SUCCEED:
6528 		aac_abort_iocmds(softs, AAC_IOCMD_OUTSTANDING, NULL, CMD_RESET);
6529 		aac_start_waiting_io(softs);
6530 		break;
6531 	case AAC_IOP_RESET_FAILED:
6532 		/* Abort all waiting cmds when adapter is dead */
6533 		aac_abort_iocmds(softs, AAC_IOCMD_ALL, NULL, CMD_TIMEOUT);
6534 		break;
6535 	case AAC_IOP_RESET_ABNORMAL:
6536 		aac_start_waiting_io(softs);
6537 	}
6538 }
6539 
6540 /*
6541  * The following function comes from Adaptec:
6542  *
6543  * Time sync. command added to synchronize time with firmware every 30
6544  * minutes (required for correct AIF timestamps etc.)
6545  */
6546 static void
6547 aac_sync_tick(struct aac_softstate *softs)
6548 {
6549 	ddi_acc_handle_t acc;
6550 	int rval;
6551 
6552 	mutex_enter(&softs->time_mutex);
6553 	ASSERT(softs->time_sync <= softs->timebase);
6554 	softs->time_sync = 0;
6555 	mutex_exit(&softs->time_mutex);
6556 
6557 	/* Time sync. with firmware every AAC_SYNC_TICK */
6558 	(void) aac_sync_fib_slot_bind(softs, &softs->sync_ac);
6559 	acc = softs->sync_ac.slotp->fib_acc_handle;
6560 
6561 	ddi_put32(acc, (void *)&softs->sync_ac.slotp->fibp->data[0],
6562 	    ddi_get_time());
6563 	rval = aac_sync_fib(softs, SendHostTime, AAC_FIB_SIZEOF(uint32_t));
6564 	aac_sync_fib_slot_release(softs, &softs->sync_ac);
6565 
6566 	mutex_enter(&softs->time_mutex);
6567 	softs->time_sync = softs->timebase;
6568 	if (rval != AACOK)
6569 		/* retry shortly */
6570 		softs->time_sync += aac_tick << 1;
6571 	else
6572 		softs->time_sync += AAC_SYNC_TICK;
6573 	mutex_exit(&softs->time_mutex);
6574 }
6575 
6576 /*
6577  * Timeout checking and handling
6578  */
6579 static void
6580 aac_daemon(struct aac_softstate *softs)
6581 {
6582 	int time_out; /* set if timeout happened */
6583 	int time_adjust;
6584 	uint32_t softs_timebase;
6585 
6586 	mutex_enter(&softs->time_mutex);
6587 	ASSERT(softs->time_out <= softs->timebase);
6588 	softs->time_out = 0;
6589 	softs_timebase = softs->timebase;
6590 	mutex_exit(&softs->time_mutex);
6591 
6592 	/* Check slots for timeout pkts */
6593 	time_adjust = 0;
6594 	do {
6595 		struct aac_cmd *acp;
6596 
6597 		time_out = 0;
6598 		for (acp = softs->q_busy.q_head; acp; acp = acp->next) {
6599 			if (acp->timeout == 0)
6600 				continue;
6601 
6602 			/*
6603 			 * If timeout happened, update outstanding cmds
6604 			 * to be checked later again.
6605 			 */
6606 			if (time_adjust) {
6607 				acp->timeout += time_adjust;
6608 				continue;
6609 			}
6610 
6611 			if (acp->timeout <= softs_timebase) {
6612 				aac_cmd_timeout(softs, acp);
6613 				time_out = 1;
6614 				time_adjust = aac_tick * drv_usectohz(1000000);
6615 				break; /* timeout happened */
6616 			} else {
6617 				break; /* no timeout */
6618 			}
6619 		}
6620 	} while (time_out);
6621 
6622 	mutex_enter(&softs->time_mutex);
6623 	softs->time_out = softs->timebase + aac_tick;
6624 	mutex_exit(&softs->time_mutex);
6625 }
6626 
6627 /*
6628  * The event thread handles various tasks serially for the other parts of
6629  * the driver, so that they can run fast.
6630  */
6631 static void
6632 aac_event_thread(struct aac_softstate *softs)
6633 {
6634 	int run = 1;
6635 
6636 	DBCALLED(softs, 1);
6637 
6638 	mutex_enter(&softs->ev_lock);
6639 	while (run) {
6640 		int events;
6641 
6642 		if ((events = softs->events) == 0) {
6643 			cv_wait(&softs->event_disp_cv, &softs->ev_lock);
6644 			events = softs->events;
6645 		}
6646 		softs->events = 0;
6647 		mutex_exit(&softs->ev_lock);
6648 
6649 		mutex_enter(&softs->io_lock);
6650 		if ((softs->state & AAC_STATE_RUN) &&
6651 		    (softs->state & AAC_STATE_DEAD) == 0) {
6652 			if (events & AAC_EVENT_TIMEOUT)
6653 				aac_daemon(softs);
6654 			if (events & AAC_EVENT_SYNCTICK)
6655 				aac_sync_tick(softs);
6656 			if (events & AAC_EVENT_AIF)
6657 				aac_aif_event(softs);
6658 		} else {
6659 			run = 0;
6660 		}
6661 		mutex_exit(&softs->io_lock);
6662 
6663 		mutex_enter(&softs->ev_lock);
6664 	}
6665 
6666 	cv_signal(&softs->event_wait_cv);
6667 	mutex_exit(&softs->ev_lock);
6668 }
6669 
6670 /*
6671  * Internal timer. It is only responsbile for time counting and report time
6672  * related events. Events handling is done by aac_event_thread(), so that
6673  * the timer itself could be as precise as possible.
6674  */
6675 static void
6676 aac_timer(void *arg)
6677 {
6678 	struct aac_softstate *softs = arg;
6679 	int events = 0;
6680 
6681 	mutex_enter(&softs->time_mutex);
6682 
6683 	/* If timer is being stopped, exit */
6684 	if (softs->timeout_id) {
6685 		softs->timeout_id = timeout(aac_timer, (void *)softs,
6686 		    (aac_tick * drv_usectohz(1000000)));
6687 	} else {
6688 		mutex_exit(&softs->time_mutex);
6689 		return;
6690 	}
6691 
6692 	/* Time counting */
6693 	softs->timebase += aac_tick;
6694 
6695 	/* Check time related events */
6696 	if (softs->time_out && softs->time_out <= softs->timebase)
6697 		events |= AAC_EVENT_TIMEOUT;
6698 	if (softs->time_sync && softs->time_sync <= softs->timebase)
6699 		events |= AAC_EVENT_SYNCTICK;
6700 
6701 	mutex_exit(&softs->time_mutex);
6702 
6703 	if (events)
6704 		aac_event_disp(softs, events);
6705 }
6706 
6707 /*
6708  * Dispatch events to daemon thread for handling
6709  */
6710 static void
6711 aac_event_disp(struct aac_softstate *softs, int events)
6712 {
6713 	mutex_enter(&softs->ev_lock);
6714 	softs->events |= events;
6715 	cv_broadcast(&softs->event_disp_cv);
6716 	mutex_exit(&softs->ev_lock);
6717 }
6718 
6719 /*
6720  * Architecture dependent functions
6721  */
6722 static int
6723 aac_rx_get_fwstatus(struct aac_softstate *softs)
6724 {
6725 	return (PCI_MEM_GET32(softs, AAC_OMR0));
6726 }
6727 
6728 static int
6729 aac_rx_get_mailbox(struct aac_softstate *softs, int mb)
6730 {
6731 	return (PCI_MEM_GET32(softs, AAC_RX_MAILBOX + mb * 4));
6732 }
6733 
6734 static void
6735 aac_rx_set_mailbox(struct aac_softstate *softs, uint32_t cmd,
6736     uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
6737 {
6738 	PCI_MEM_PUT32(softs, AAC_RX_MAILBOX, cmd);
6739 	PCI_MEM_PUT32(softs, AAC_RX_MAILBOX + 4, arg0);
6740 	PCI_MEM_PUT32(softs, AAC_RX_MAILBOX + 8, arg1);
6741 	PCI_MEM_PUT32(softs, AAC_RX_MAILBOX + 12, arg2);
6742 	PCI_MEM_PUT32(softs, AAC_RX_MAILBOX + 16, arg3);
6743 }
6744 
6745 static int
6746 aac_rkt_get_fwstatus(struct aac_softstate *softs)
6747 {
6748 	return (PCI_MEM_GET32(softs, AAC_OMR0));
6749 }
6750 
6751 static int
6752 aac_rkt_get_mailbox(struct aac_softstate *softs, int mb)
6753 {
6754 	return (PCI_MEM_GET32(softs, AAC_RKT_MAILBOX + mb *4));
6755 }
6756 
6757 static void
6758 aac_rkt_set_mailbox(struct aac_softstate *softs, uint32_t cmd,
6759     uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
6760 {
6761 	PCI_MEM_PUT32(softs, AAC_RKT_MAILBOX, cmd);
6762 	PCI_MEM_PUT32(softs, AAC_RKT_MAILBOX + 4, arg0);
6763 	PCI_MEM_PUT32(softs, AAC_RKT_MAILBOX + 8, arg1);
6764 	PCI_MEM_PUT32(softs, AAC_RKT_MAILBOX + 12, arg2);
6765 	PCI_MEM_PUT32(softs, AAC_RKT_MAILBOX + 16, arg3);
6766 }
6767 
6768 /*
6769  * cb_ops functions
6770  */
6771 static int
6772 aac_open(dev_t *devp, int flag, int otyp, cred_t *cred)
6773 {
6774 	struct aac_softstate *softs;
6775 	int minor0, minor;
6776 	int instance;
6777 
6778 	DBCALLED(NULL, 2);
6779 
6780 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
6781 		return (EINVAL);
6782 
6783 	minor0 = getminor(*devp);
6784 	minor = AAC_SCSA_MINOR(minor0);
6785 
6786 	if (AAC_IS_SCSA_NODE(minor))
6787 		return (scsi_hba_open(devp, flag, otyp, cred));
6788 
6789 	instance = MINOR2INST(minor0);
6790 	if (instance >= AAC_MAX_ADAPTERS)
6791 		return (ENXIO);
6792 
6793 	softs = ddi_get_soft_state(aac_softstatep, instance);
6794 	if (softs == NULL)
6795 		return (ENXIO);
6796 
6797 	return (0);
6798 }
6799 
6800 /*ARGSUSED*/
6801 static int
6802 aac_close(dev_t dev, int flag, int otyp, cred_t *cred)
6803 {
6804 	int minor0, minor;
6805 	int instance;
6806 
6807 	DBCALLED(NULL, 2);
6808 
6809 	if (otyp != OTYP_BLK && otyp != OTYP_CHR)
6810 		return (EINVAL);
6811 
6812 	minor0 = getminor(dev);
6813 	minor = AAC_SCSA_MINOR(minor0);
6814 
6815 	if (AAC_IS_SCSA_NODE(minor))
6816 		return (scsi_hba_close(dev, flag, otyp, cred));
6817 
6818 	instance = MINOR2INST(minor0);
6819 	if (instance >= AAC_MAX_ADAPTERS)
6820 		return (ENXIO);
6821 
6822 	return (0);
6823 }
6824 
6825 static int
6826 aac_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p,
6827     int *rval_p)
6828 {
6829 	struct aac_softstate *softs;
6830 	int minor0, minor;
6831 	int instance;
6832 
6833 	DBCALLED(NULL, 2);
6834 
6835 	if (drv_priv(cred_p) != 0)
6836 		return (EPERM);
6837 
6838 	minor0 = getminor(dev);
6839 	minor = AAC_SCSA_MINOR(minor0);
6840 
6841 	if (AAC_IS_SCSA_NODE(minor))
6842 		return (scsi_hba_ioctl(dev, cmd, arg, flag, cred_p, rval_p));
6843 
6844 	instance = MINOR2INST(minor0);
6845 	if (instance < AAC_MAX_ADAPTERS) {
6846 		softs = ddi_get_soft_state(aac_softstatep, instance);
6847 		return (aac_do_ioctl(softs, dev, cmd, arg, flag));
6848 	}
6849 	return (ENXIO);
6850 }
6851 
6852 /*
6853  * The IO fault service error handling callback function
6854  */
6855 /*ARGSUSED*/
6856 static int
6857 aac_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
6858 {
6859 	/*
6860 	 * as the driver can always deal with an error in any dma or
6861 	 * access handle, we can just return the fme_status value.
6862 	 */
6863 	pci_ereport_post(dip, err, NULL);
6864 	return (err->fme_status);
6865 }
6866 
6867 /*
6868  * aac_fm_init - initialize fma capabilities and register with IO
6869  *               fault services.
6870  */
6871 static void
6872 aac_fm_init(struct aac_softstate *softs)
6873 {
6874 	/*
6875 	 * Need to change iblock to priority for new MSI intr
6876 	 */
6877 	ddi_iblock_cookie_t fm_ibc;
6878 
6879 	softs->fm_capabilities = ddi_getprop(DDI_DEV_T_ANY, softs->devinfo_p,
6880 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
6881 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
6882 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
6883 
6884 	/* Only register with IO Fault Services if we have some capability */
6885 	if (softs->fm_capabilities) {
6886 		/* Adjust access and dma attributes for FMA */
6887 		softs->reg_attr.devacc_attr_access = DDI_FLAGERR_ACC;
6888 		softs->addr_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
6889 		softs->buf_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
6890 
6891 		/*
6892 		 * Register capabilities with IO Fault Services.
6893 		 * fm_capabilities will be updated to indicate
6894 		 * capabilities actually supported (not requested.)
6895 		 */
6896 		ddi_fm_init(softs->devinfo_p, &softs->fm_capabilities, &fm_ibc);
6897 
6898 		/*
6899 		 * Initialize pci ereport capabilities if ereport
6900 		 * capable (should always be.)
6901 		 */
6902 		if (DDI_FM_EREPORT_CAP(softs->fm_capabilities) ||
6903 		    DDI_FM_ERRCB_CAP(softs->fm_capabilities)) {
6904 			pci_ereport_setup(softs->devinfo_p);
6905 		}
6906 
6907 		/*
6908 		 * Register error callback if error callback capable.
6909 		 */
6910 		if (DDI_FM_ERRCB_CAP(softs->fm_capabilities)) {
6911 			ddi_fm_handler_register(softs->devinfo_p,
6912 			    aac_fm_error_cb, (void *) softs);
6913 		}
6914 	}
6915 }
6916 
6917 /*
6918  * aac_fm_fini - Releases fma capabilities and un-registers with IO
6919  *               fault services.
6920  */
6921 static void
6922 aac_fm_fini(struct aac_softstate *softs)
6923 {
6924 	/* Only unregister FMA capabilities if registered */
6925 	if (softs->fm_capabilities) {
6926 		/*
6927 		 * Un-register error callback if error callback capable.
6928 		 */
6929 		if (DDI_FM_ERRCB_CAP(softs->fm_capabilities)) {
6930 			ddi_fm_handler_unregister(softs->devinfo_p);
6931 		}
6932 
6933 		/*
6934 		 * Release any resources allocated by pci_ereport_setup()
6935 		 */
6936 		if (DDI_FM_EREPORT_CAP(softs->fm_capabilities) ||
6937 		    DDI_FM_ERRCB_CAP(softs->fm_capabilities)) {
6938 			pci_ereport_teardown(softs->devinfo_p);
6939 		}
6940 
6941 		/* Unregister from IO Fault Services */
6942 		ddi_fm_fini(softs->devinfo_p);
6943 
6944 		/* Adjust access and dma attributes for FMA */
6945 		softs->reg_attr.devacc_attr_access = DDI_DEFAULT_ACC;
6946 		softs->addr_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
6947 		softs->buf_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
6948 	}
6949 }
6950 
6951 int
6952 aac_check_acc_handle(ddi_acc_handle_t handle)
6953 {
6954 	ddi_fm_error_t de;
6955 
6956 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
6957 	return (de.fme_status);
6958 }
6959 
6960 int
6961 aac_check_dma_handle(ddi_dma_handle_t handle)
6962 {
6963 	ddi_fm_error_t de;
6964 
6965 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
6966 	return (de.fme_status);
6967 }
6968 
6969 void
6970 aac_fm_ereport(struct aac_softstate *softs, char *detail)
6971 {
6972 	uint64_t ena;
6973 	char buf[FM_MAX_CLASS];
6974 
6975 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
6976 	ena = fm_ena_generate(0, FM_ENA_FMT1);
6977 	if (DDI_FM_EREPORT_CAP(softs->fm_capabilities)) {
6978 		ddi_fm_ereport_post(softs->devinfo_p, buf, ena, DDI_NOSLEEP,
6979 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERSION, NULL);
6980 	}
6981 }
6982 
6983 /*
6984  * Autoconfiguration support
6985  */
6986 static int
6987 aac_parse_devname(char *devnm, int *tgt, int *lun)
6988 {
6989 	char devbuf[SCSI_MAXNAMELEN];
6990 	char *addr;
6991 	char *p,  *tp, *lp;
6992 	long num;
6993 
6994 	/* Parse dev name and address */
6995 	(void) strcpy(devbuf, devnm);
6996 	addr = "";
6997 	for (p = devbuf; *p != '\0'; p++) {
6998 		if (*p == '@') {
6999 			addr = p + 1;
7000 			*p = '\0';
7001 		} else if (*p == ':') {
7002 			*p = '\0';
7003 			break;
7004 		}
7005 	}
7006 
7007 	/* Parse taget and lun */
7008 	for (p = tp = addr, lp = NULL; *p != '\0'; p++) {
7009 		if (*p == ',') {
7010 			lp = p + 1;
7011 			*p = '\0';
7012 			break;
7013 		}
7014 	}
7015 	if (tgt && tp) {
7016 		if (ddi_strtol(tp, NULL, 0x10, &num))
7017 			return (AACERR);
7018 		*tgt = (int)num;
7019 	}
7020 	if (lun && lp) {
7021 		if (ddi_strtol(lp, NULL, 0x10, &num))
7022 			return (AACERR);
7023 		*lun = (int)num;
7024 	}
7025 	return (AACOK);
7026 }
7027 
7028 static dev_info_t *
7029 aac_find_child(struct aac_softstate *softs, uint16_t tgt, uint8_t lun)
7030 {
7031 	dev_info_t *child = NULL;
7032 	char addr[SCSI_MAXNAMELEN];
7033 	char tmp[MAXNAMELEN];
7034 
7035 	if (tgt < AAC_MAX_LD) {
7036 		if (lun == 0) {
7037 			struct aac_device *dvp = &softs->containers[tgt].dev;
7038 
7039 			child = dvp->dip;
7040 		}
7041 	} else {
7042 		(void) sprintf(addr, "%x,%x", tgt, lun);
7043 		for (child = ddi_get_child(softs->devinfo_p);
7044 		    child; child = ddi_get_next_sibling(child)) {
7045 			/* We don't care about non-persistent node */
7046 			if (ndi_dev_is_persistent_node(child) == 0)
7047 				continue;
7048 
7049 			if (aac_name_node(child, tmp, MAXNAMELEN) !=
7050 			    DDI_SUCCESS)
7051 				continue;
7052 			if (strcmp(addr, tmp) == 0)
7053 				break;
7054 		}
7055 	}
7056 	return (child);
7057 }
7058 
7059 static int
7060 aac_config_child(struct aac_softstate *softs, struct scsi_device *sd,
7061     dev_info_t **dipp)
7062 {
7063 	char *nodename = NULL;
7064 	char **compatible = NULL;
7065 	int ncompatible = 0;
7066 	char *childname;
7067 	dev_info_t *ldip = NULL;
7068 	int tgt = sd->sd_address.a_target;
7069 	int lun = sd->sd_address.a_lun;
7070 	int dtype = sd->sd_inq->inq_dtype & DTYPE_MASK;
7071 	int rval;
7072 
7073 	DBCALLED(softs, 2);
7074 
7075 	scsi_hba_nodename_compatible_get(sd->sd_inq, NULL, dtype,
7076 	    NULL, &nodename, &compatible, &ncompatible);
7077 	if (nodename == NULL) {
7078 		AACDB_PRINT(softs, CE_WARN,
7079 		    "found no comptible driver for t%dL%d", tgt, lun);
7080 		rval = NDI_FAILURE;
7081 		goto finish;
7082 	}
7083 	childname = (softs->legacy && dtype == DTYPE_DIRECT) ? "sd" : nodename;
7084 
7085 	/* Create dev node */
7086 	rval = ndi_devi_alloc(softs->devinfo_p, childname, DEVI_SID_NODEID,
7087 	    &ldip);
7088 	if (rval == NDI_SUCCESS) {
7089 		if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "target", tgt)
7090 		    != DDI_PROP_SUCCESS) {
7091 			AACDB_PRINT(softs, CE_WARN, "unable to create "
7092 			    "property for t%dL%d (target)", tgt, lun);
7093 			rval = NDI_FAILURE;
7094 			goto finish;
7095 		}
7096 		if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "lun", lun)
7097 		    != DDI_PROP_SUCCESS) {
7098 			AACDB_PRINT(softs, CE_WARN, "unable to create "
7099 			    "property for t%dL%d (lun)", tgt, lun);
7100 			rval = NDI_FAILURE;
7101 			goto finish;
7102 		}
7103 		if (ndi_prop_update_string_array(DDI_DEV_T_NONE, ldip,
7104 		    "compatible", compatible, ncompatible)
7105 		    != DDI_PROP_SUCCESS) {
7106 			AACDB_PRINT(softs, CE_WARN, "unable to create "
7107 			    "property for t%dL%d (compatible)", tgt, lun);
7108 			rval = NDI_FAILURE;
7109 			goto finish;
7110 		}
7111 
7112 		rval = ndi_devi_online(ldip, NDI_ONLINE_ATTACH);
7113 		if (rval != NDI_SUCCESS) {
7114 			AACDB_PRINT(softs, CE_WARN, "unable to online t%dL%d",
7115 			    tgt, lun);
7116 			ndi_prop_remove_all(ldip);
7117 			(void) ndi_devi_free(ldip);
7118 		}
7119 	}
7120 finish:
7121 	if (dipp)
7122 		*dipp = ldip;
7123 
7124 	scsi_hba_nodename_compatible_free(nodename, compatible);
7125 	return (rval);
7126 }
7127 
7128 /*ARGSUSED*/
7129 static int
7130 aac_probe_lun(struct aac_softstate *softs, struct scsi_device *sd)
7131 {
7132 	int tgt = sd->sd_address.a_target;
7133 	int lun = sd->sd_address.a_lun;
7134 
7135 	DBCALLED(softs, 2);
7136 
7137 	if (tgt < AAC_MAX_LD) {
7138 		enum aac_cfg_event event;
7139 
7140 		if (lun == 0) {
7141 			mutex_enter(&softs->io_lock);
7142 			event = aac_probe_container(softs, tgt);
7143 			mutex_exit(&softs->io_lock);
7144 			if ((event != AAC_CFG_NULL_NOEXIST) &&
7145 			    (event != AAC_CFG_DELETE)) {
7146 				if (scsi_hba_probe(sd, NULL) ==
7147 				    SCSIPROBE_EXISTS)
7148 					return (NDI_SUCCESS);
7149 			}
7150 		}
7151 		return (NDI_FAILURE);
7152 	} else {
7153 		int dtype;
7154 
7155 		if (scsi_hba_probe(sd, NULL) != SCSIPROBE_EXISTS)
7156 			return (NDI_FAILURE);
7157 
7158 		dtype = sd->sd_inq->inq_dtype & DTYPE_MASK;
7159 
7160 		AACDB_PRINT(softs, CE_NOTE,
7161 		    "Phys. device found: tgt %d dtype %d: %s",
7162 		    tgt, dtype, sd->sd_inq->inq_vid);
7163 
7164 		/* Only non-DASD exposed */
7165 		if (dtype != DTYPE_RODIRECT /* CDROM */ &&
7166 		    dtype != DTYPE_SEQUENTIAL /* TAPE */ &&
7167 		    dtype != DTYPE_ESI /* SES */)
7168 			return (NDI_FAILURE);
7169 
7170 		AACDB_PRINT(softs, CE_NOTE, "non-DASD %d found", tgt);
7171 		mutex_enter(&softs->io_lock);
7172 		softs->nondasds[AAC_PD(tgt)].dev.flags |= AAC_DFLAG_VALID;
7173 		mutex_exit(&softs->io_lock);
7174 		return (NDI_SUCCESS);
7175 	}
7176 }
7177 
7178 static int
7179 aac_config_lun(struct aac_softstate *softs, uint16_t tgt, uint8_t lun,
7180     dev_info_t **ldip)
7181 {
7182 	struct scsi_device sd;
7183 	dev_info_t *child;
7184 	int rval;
7185 
7186 	DBCALLED(softs, 2);
7187 
7188 	if ((child = aac_find_child(softs, tgt, lun)) != NULL) {
7189 		if (ldip)
7190 			*ldip = child;
7191 		return (NDI_SUCCESS);
7192 	}
7193 
7194 	bzero(&sd, sizeof (struct scsi_device));
7195 	sd.sd_address.a_hba_tran = softs->hba_tran;
7196 	sd.sd_address.a_target = (uint16_t)tgt;
7197 	sd.sd_address.a_lun = (uint8_t)lun;
7198 	if ((rval = aac_probe_lun(softs, &sd)) == NDI_SUCCESS)
7199 		rval = aac_config_child(softs, &sd, ldip);
7200 	/* scsi_unprobe is blank now. Free buffer manually */
7201 	if (sd.sd_inq) {
7202 		kmem_free(sd.sd_inq, SUN_INQSIZE);
7203 		sd.sd_inq = (struct scsi_inquiry *)NULL;
7204 	}
7205 	return (rval);
7206 }
7207 
7208 static int
7209 aac_config_tgt(struct aac_softstate *softs, int tgt)
7210 {
7211 	struct scsi_address ap;
7212 	struct buf *bp = NULL;
7213 	int buf_len = AAC_SCSI_RPTLUNS_HEAD_SIZE + AAC_SCSI_RPTLUNS_ADDR_SIZE;
7214 	int list_len = 0;
7215 	int lun_total = 0;
7216 	dev_info_t *ldip;
7217 	int i;
7218 
7219 	ap.a_hba_tran = softs->hba_tran;
7220 	ap.a_target = (uint16_t)tgt;
7221 	ap.a_lun = 0;
7222 
7223 	for (i = 0; i < 2; i++) {
7224 		struct scsi_pkt *pkt;
7225 		uchar_t *cdb;
7226 		uchar_t *p;
7227 		uint32_t data;
7228 
7229 		if (bp == NULL) {
7230 			if ((bp = scsi_alloc_consistent_buf(&ap, NULL,
7231 			    buf_len, B_READ, NULL_FUNC, NULL)) == NULL)
7232 			return (AACERR);
7233 		}
7234 		if ((pkt = scsi_init_pkt(&ap, NULL, bp, CDB_GROUP5,
7235 		    sizeof (struct scsi_arq_status), 0, PKT_CONSISTENT,
7236 		    NULL, NULL)) == NULL) {
7237 			scsi_free_consistent_buf(bp);
7238 			return (AACERR);
7239 		}
7240 		cdb = pkt->pkt_cdbp;
7241 		bzero(cdb, CDB_GROUP5);
7242 		cdb[0] = SCMD_REPORT_LUNS;
7243 
7244 		/* Convert buffer len from local to LE_32 */
7245 		data = buf_len;
7246 		for (p = &cdb[9]; p > &cdb[5]; p--) {
7247 			*p = data & 0xff;
7248 			data >>= 8;
7249 		}
7250 
7251 		if (scsi_poll(pkt) < 0 ||
7252 		    ((struct scsi_status *)pkt->pkt_scbp)->sts_chk) {
7253 			scsi_destroy_pkt(pkt);
7254 			break;
7255 		}
7256 
7257 		/* Convert list_len from LE_32 to local */
7258 		for (p = (uchar_t *)bp->b_un.b_addr;
7259 		    p < (uchar_t *)bp->b_un.b_addr + 4; p++) {
7260 			data <<= 8;
7261 			data |= *p;
7262 		}
7263 		list_len = data;
7264 		if (buf_len < list_len + AAC_SCSI_RPTLUNS_HEAD_SIZE) {
7265 			scsi_free_consistent_buf(bp);
7266 			bp = NULL;
7267 			buf_len = list_len + AAC_SCSI_RPTLUNS_HEAD_SIZE;
7268 		}
7269 		scsi_destroy_pkt(pkt);
7270 	}
7271 	if (i >= 2) {
7272 		uint8_t *buf = (uint8_t *)(bp->b_un.b_addr +
7273 		    AAC_SCSI_RPTLUNS_HEAD_SIZE);
7274 
7275 		for (i = 0; i < (list_len / AAC_SCSI_RPTLUNS_ADDR_SIZE); i++) {
7276 			uint16_t lun;
7277 
7278 			/* Determine report luns addressing type */
7279 			switch (buf[0] & AAC_SCSI_RPTLUNS_ADDR_MASK) {
7280 			/*
7281 			 * Vendors in the field have been found to be
7282 			 * concatenating bus/target/lun to equal the
7283 			 * complete lun value instead of switching to
7284 			 * flat space addressing
7285 			 */
7286 			case AAC_SCSI_RPTLUNS_ADDR_PERIPHERAL:
7287 			case AAC_SCSI_RPTLUNS_ADDR_LOGICAL_UNIT:
7288 			case AAC_SCSI_RPTLUNS_ADDR_FLAT_SPACE:
7289 				lun = ((buf[0] & 0x3f) << 8) | buf[1];
7290 				if (lun > UINT8_MAX) {
7291 					AACDB_PRINT(softs, CE_WARN,
7292 					    "abnormal lun number: %d", lun);
7293 					break;
7294 				}
7295 				if (aac_config_lun(softs, tgt, lun, &ldip) ==
7296 				    NDI_SUCCESS)
7297 					lun_total++;
7298 				break;
7299 			}
7300 
7301 			buf += AAC_SCSI_RPTLUNS_ADDR_SIZE;
7302 		}
7303 	} else {
7304 		/* The target may do not support SCMD_REPORT_LUNS. */
7305 		if (aac_config_lun(softs, tgt, 0, &ldip) == NDI_SUCCESS)
7306 			lun_total++;
7307 	}
7308 	scsi_free_consistent_buf(bp);
7309 	return (lun_total);
7310 }
7311 
7312 static void
7313 aac_devcfg(struct aac_softstate *softs, int tgt, int en)
7314 {
7315 	struct aac_device *dvp;
7316 
7317 	mutex_enter(&softs->io_lock);
7318 	dvp = AAC_DEV(softs, tgt);
7319 	if (en)
7320 		dvp->flags |= AAC_DFLAG_CONFIGURING;
7321 	else
7322 		dvp->flags &= ~AAC_DFLAG_CONFIGURING;
7323 	mutex_exit(&softs->io_lock);
7324 }
7325 
7326 static int
7327 aac_tran_bus_config(dev_info_t *parent, uint_t flags, ddi_bus_config_op_t op,
7328     void *arg, dev_info_t **childp)
7329 {
7330 	struct aac_softstate *softs;
7331 	int circ = 0;
7332 	int rval;
7333 
7334 	if ((softs = ddi_get_soft_state(aac_softstatep,
7335 	    ddi_get_instance(parent))) == NULL)
7336 		return (NDI_FAILURE);
7337 
7338 	/* Commands for bus config should be blocked as the bus is quiesced */
7339 	mutex_enter(&softs->io_lock);
7340 	if (softs->state & AAC_STATE_QUIESCED) {
7341 		AACDB_PRINT(softs, CE_NOTE,
7342 		    "bus_config abroted because bus is quiesced");
7343 		mutex_exit(&softs->io_lock);
7344 		return (NDI_FAILURE);
7345 	}
7346 	mutex_exit(&softs->io_lock);
7347 
7348 	DBCALLED(softs, 1);
7349 
7350 	/* Hold the nexus across the bus_config */
7351 	ndi_devi_enter(parent, &circ);
7352 	switch (op) {
7353 	case BUS_CONFIG_ONE: {
7354 		int tgt, lun;
7355 
7356 		if (aac_parse_devname(arg, &tgt, &lun) != AACOK) {
7357 			rval = NDI_FAILURE;
7358 			break;
7359 		}
7360 
7361 		AAC_DEVCFG_BEGIN(softs, tgt);
7362 		rval = aac_config_lun(softs, tgt, lun, childp);
7363 		AAC_DEVCFG_END(softs, tgt);
7364 		break;
7365 	}
7366 
7367 	case BUS_CONFIG_DRIVER:
7368 	case BUS_CONFIG_ALL: {
7369 		uint32_t bus, tgt;
7370 		int index, total;
7371 
7372 		for (tgt = 0; tgt < AAC_MAX_LD; tgt++) {
7373 			AAC_DEVCFG_BEGIN(softs, tgt);
7374 			(void) aac_config_lun(softs, tgt, 0, NULL);
7375 			AAC_DEVCFG_END(softs, tgt);
7376 		}
7377 
7378 		/* Config the non-DASD devices connected to the card */
7379 		total = 0;
7380 		index = AAC_MAX_LD;
7381 		for (bus = 0; bus < softs->bus_max; bus++) {
7382 			AACDB_PRINT(softs, CE_NOTE, "bus %d:", bus);
7383 			for (tgt = 0; tgt < softs->tgt_max; tgt++, index++) {
7384 				AAC_DEVCFG_BEGIN(softs, index);
7385 				if (aac_config_tgt(softs, index))
7386 					total++;
7387 				AAC_DEVCFG_END(softs, index);
7388 			}
7389 		}
7390 		AACDB_PRINT(softs, CE_CONT,
7391 		    "?Total %d phys. device(s) found", total);
7392 		rval = NDI_SUCCESS;
7393 		break;
7394 	}
7395 	}
7396 
7397 	if (rval == NDI_SUCCESS)
7398 		rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0);
7399 	ndi_devi_exit(parent, circ);
7400 	return (rval);
7401 }
7402 
7403 /*ARGSUSED*/
7404 static int
7405 aac_handle_dr(struct aac_softstate *softs, int tgt, int lun, int event)
7406 {
7407 	struct aac_device *dvp;
7408 	dev_info_t *dip;
7409 	int valid;
7410 	int circ1 = 0;
7411 
7412 	DBCALLED(softs, 1);
7413 
7414 	/* Hold the nexus across the bus_config */
7415 	dvp = AAC_DEV(softs, tgt);
7416 	valid = AAC_DEV_IS_VALID(dvp);
7417 	dip = dvp->dip;
7418 	if (!(softs->state & AAC_STATE_RUN))
7419 		return (AACERR);
7420 	mutex_exit(&softs->io_lock);
7421 
7422 	switch (event) {
7423 	case AAC_CFG_ADD:
7424 	case AAC_CFG_DELETE:
7425 		/* Device onlined */
7426 		if (dip == NULL && valid) {
7427 			ndi_devi_enter(softs->devinfo_p, &circ1);
7428 			(void) aac_config_lun(softs, tgt, 0, NULL);
7429 			AACDB_PRINT(softs, CE_NOTE, "c%dt%dL%d onlined",
7430 			    softs->instance, tgt, lun);
7431 			ndi_devi_exit(softs->devinfo_p, circ1);
7432 		}
7433 		/* Device offlined */
7434 		if (dip && !valid) {
7435 			mutex_enter(&softs->io_lock);
7436 			(void) aac_do_reset(softs);
7437 			mutex_exit(&softs->io_lock);
7438 
7439 			(void) ndi_devi_offline(dip, NDI_DEVI_REMOVE);
7440 			AACDB_PRINT(softs, CE_NOTE, "c%dt%dL%d offlined",
7441 			    softs->instance, tgt, lun);
7442 		}
7443 		break;
7444 	}
7445 
7446 	mutex_enter(&softs->io_lock);
7447 	return (AACOK);
7448 }
7449 
7450 #ifdef DEBUG
7451 
7452 /* -------------------------debug aid functions-------------------------- */
7453 
7454 #define	AAC_FIB_CMD_KEY_STRINGS \
7455 	TestCommandResponse, "TestCommandResponse", \
7456 	TestAdapterCommand, "TestAdapterCommand", \
7457 	LastTestCommand, "LastTestCommand", \
7458 	ReinitHostNormCommandQueue, "ReinitHostNormCommandQueue", \
7459 	ReinitHostHighCommandQueue, "ReinitHostHighCommandQueue", \
7460 	ReinitHostHighRespQueue, "ReinitHostHighRespQueue", \
7461 	ReinitHostNormRespQueue, "ReinitHostNormRespQueue", \
7462 	ReinitAdapNormCommandQueue, "ReinitAdapNormCommandQueue", \
7463 	ReinitAdapHighCommandQueue, "ReinitAdapHighCommandQueue", \
7464 	ReinitAdapHighRespQueue, "ReinitAdapHighRespQueue", \
7465 	ReinitAdapNormRespQueue, "ReinitAdapNormRespQueue", \
7466 	InterfaceShutdown, "InterfaceShutdown", \
7467 	DmaCommandFib, "DmaCommandFib", \
7468 	StartProfile, "StartProfile", \
7469 	TermProfile, "TermProfile", \
7470 	SpeedTest, "SpeedTest", \
7471 	TakeABreakPt, "TakeABreakPt", \
7472 	RequestPerfData, "RequestPerfData", \
7473 	SetInterruptDefTimer, "SetInterruptDefTimer", \
7474 	SetInterruptDefCount, "SetInterruptDefCount", \
7475 	GetInterruptDefStatus, "GetInterruptDefStatus", \
7476 	LastCommCommand, "LastCommCommand", \
7477 	NuFileSystem, "NuFileSystem", \
7478 	UFS, "UFS", \
7479 	HostFileSystem, "HostFileSystem", \
7480 	LastFileSystemCommand, "LastFileSystemCommand", \
7481 	ContainerCommand, "ContainerCommand", \
7482 	ContainerCommand64, "ContainerCommand64", \
7483 	ClusterCommand, "ClusterCommand", \
7484 	ScsiPortCommand, "ScsiPortCommand", \
7485 	ScsiPortCommandU64, "ScsiPortCommandU64", \
7486 	AifRequest, "AifRequest", \
7487 	CheckRevision, "CheckRevision", \
7488 	FsaHostShutdown, "FsaHostShutdown", \
7489 	RequestAdapterInfo, "RequestAdapterInfo", \
7490 	IsAdapterPaused, "IsAdapterPaused", \
7491 	SendHostTime, "SendHostTime", \
7492 	LastMiscCommand, "LastMiscCommand"
7493 
7494 #define	AAC_CTVM_SUBCMD_KEY_STRINGS \
7495 	VM_Null, "VM_Null", \
7496 	VM_NameServe, "VM_NameServe", \
7497 	VM_ContainerConfig, "VM_ContainerConfig", \
7498 	VM_Ioctl, "VM_Ioctl", \
7499 	VM_FilesystemIoctl, "VM_FilesystemIoctl", \
7500 	VM_CloseAll, "VM_CloseAll", \
7501 	VM_CtBlockRead, "VM_CtBlockRead", \
7502 	VM_CtBlockWrite, "VM_CtBlockWrite", \
7503 	VM_SliceBlockRead, "VM_SliceBlockRead", \
7504 	VM_SliceBlockWrite, "VM_SliceBlockWrite", \
7505 	VM_DriveBlockRead, "VM_DriveBlockRead", \
7506 	VM_DriveBlockWrite, "VM_DriveBlockWrite", \
7507 	VM_EnclosureMgt, "VM_EnclosureMgt", \
7508 	VM_Unused, "VM_Unused", \
7509 	VM_CtBlockVerify, "VM_CtBlockVerify", \
7510 	VM_CtPerf, "VM_CtPerf", \
7511 	VM_CtBlockRead64, "VM_CtBlockRead64", \
7512 	VM_CtBlockWrite64, "VM_CtBlockWrite64", \
7513 	VM_CtBlockVerify64, "VM_CtBlockVerify64", \
7514 	VM_CtHostRead64, "VM_CtHostRead64", \
7515 	VM_CtHostWrite64, "VM_CtHostWrite64", \
7516 	VM_NameServe64, "VM_NameServe64"
7517 
7518 #define	AAC_CT_SUBCMD_KEY_STRINGS \
7519 	CT_Null, "CT_Null", \
7520 	CT_GET_SLICE_COUNT, "CT_GET_SLICE_COUNT", \
7521 	CT_GET_PARTITION_COUNT, "CT_GET_PARTITION_COUNT", \
7522 	CT_GET_PARTITION_INFO, "CT_GET_PARTITION_INFO", \
7523 	CT_GET_CONTAINER_COUNT, "CT_GET_CONTAINER_COUNT", \
7524 	CT_GET_CONTAINER_INFO_OLD, "CT_GET_CONTAINER_INFO_OLD", \
7525 	CT_WRITE_MBR, "CT_WRITE_MBR", \
7526 	CT_WRITE_PARTITION, "CT_WRITE_PARTITION", \
7527 	CT_UPDATE_PARTITION, "CT_UPDATE_PARTITION", \
7528 	CT_UNLOAD_CONTAINER, "CT_UNLOAD_CONTAINER", \
7529 	CT_CONFIG_SINGLE_PRIMARY, "CT_CONFIG_SINGLE_PRIMARY", \
7530 	CT_READ_CONFIG_AGE, "CT_READ_CONFIG_AGE", \
7531 	CT_WRITE_CONFIG_AGE, "CT_WRITE_CONFIG_AGE", \
7532 	CT_READ_SERIAL_NUMBER, "CT_READ_SERIAL_NUMBER", \
7533 	CT_ZERO_PAR_ENTRY, "CT_ZERO_PAR_ENTRY", \
7534 	CT_READ_MBR, "CT_READ_MBR", \
7535 	CT_READ_PARTITION, "CT_READ_PARTITION", \
7536 	CT_DESTROY_CONTAINER, "CT_DESTROY_CONTAINER", \
7537 	CT_DESTROY2_CONTAINER, "CT_DESTROY2_CONTAINER", \
7538 	CT_SLICE_SIZE, "CT_SLICE_SIZE", \
7539 	CT_CHECK_CONFLICTS, "CT_CHECK_CONFLICTS", \
7540 	CT_MOVE_CONTAINER, "CT_MOVE_CONTAINER", \
7541 	CT_READ_LAST_DRIVE, "CT_READ_LAST_DRIVE", \
7542 	CT_WRITE_LAST_DRIVE, "CT_WRITE_LAST_DRIVE", \
7543 	CT_UNMIRROR, "CT_UNMIRROR", \
7544 	CT_MIRROR_DELAY, "CT_MIRROR_DELAY", \
7545 	CT_GEN_MIRROR, "CT_GEN_MIRROR", \
7546 	CT_GEN_MIRROR2, "CT_GEN_MIRROR2", \
7547 	CT_TEST_CONTAINER, "CT_TEST_CONTAINER", \
7548 	CT_MOVE2, "CT_MOVE2", \
7549 	CT_SPLIT, "CT_SPLIT", \
7550 	CT_SPLIT2, "CT_SPLIT2", \
7551 	CT_SPLIT_BROKEN, "CT_SPLIT_BROKEN", \
7552 	CT_SPLIT_BROKEN2, "CT_SPLIT_BROKEN2", \
7553 	CT_RECONFIG, "CT_RECONFIG", \
7554 	CT_BREAK2, "CT_BREAK2", \
7555 	CT_BREAK, "CT_BREAK", \
7556 	CT_MERGE2, "CT_MERGE2", \
7557 	CT_MERGE, "CT_MERGE", \
7558 	CT_FORCE_ERROR, "CT_FORCE_ERROR", \
7559 	CT_CLEAR_ERROR, "CT_CLEAR_ERROR", \
7560 	CT_ASSIGN_FAILOVER, "CT_ASSIGN_FAILOVER", \
7561 	CT_CLEAR_FAILOVER, "CT_CLEAR_FAILOVER", \
7562 	CT_GET_FAILOVER_DATA, "CT_GET_FAILOVER_DATA", \
7563 	CT_VOLUME_ADD, "CT_VOLUME_ADD", \
7564 	CT_VOLUME_ADD2, "CT_VOLUME_ADD2", \
7565 	CT_MIRROR_STATUS, "CT_MIRROR_STATUS", \
7566 	CT_COPY_STATUS, "CT_COPY_STATUS", \
7567 	CT_COPY, "CT_COPY", \
7568 	CT_UNLOCK_CONTAINER, "CT_UNLOCK_CONTAINER", \
7569 	CT_LOCK_CONTAINER, "CT_LOCK_CONTAINER", \
7570 	CT_MAKE_READ_ONLY, "CT_MAKE_READ_ONLY", \
7571 	CT_MAKE_READ_WRITE, "CT_MAKE_READ_WRITE", \
7572 	CT_CLEAN_DEAD, "CT_CLEAN_DEAD", \
7573 	CT_ABORT_MIRROR_COMMAND, "CT_ABORT_MIRROR_COMMAND", \
7574 	CT_SET, "CT_SET", \
7575 	CT_GET, "CT_GET", \
7576 	CT_GET_NVLOG_ENTRY, "CT_GET_NVLOG_ENTRY", \
7577 	CT_GET_DELAY, "CT_GET_DELAY", \
7578 	CT_ZERO_CONTAINER_SPACE, "CT_ZERO_CONTAINER_SPACE", \
7579 	CT_GET_ZERO_STATUS, "CT_GET_ZERO_STATUS", \
7580 	CT_SCRUB, "CT_SCRUB", \
7581 	CT_GET_SCRUB_STATUS, "CT_GET_SCRUB_STATUS", \
7582 	CT_GET_SLICE_INFO, "CT_GET_SLICE_INFO", \
7583 	CT_GET_SCSI_METHOD, "CT_GET_SCSI_METHOD", \
7584 	CT_PAUSE_IO, "CT_PAUSE_IO", \
7585 	CT_RELEASE_IO, "CT_RELEASE_IO", \
7586 	CT_SCRUB2, "CT_SCRUB2", \
7587 	CT_MCHECK, "CT_MCHECK", \
7588 	CT_CORRUPT, "CT_CORRUPT", \
7589 	CT_GET_TASK_COUNT, "CT_GET_TASK_COUNT", \
7590 	CT_PROMOTE, "CT_PROMOTE", \
7591 	CT_SET_DEAD, "CT_SET_DEAD", \
7592 	CT_CONTAINER_OPTIONS, "CT_CONTAINER_OPTIONS", \
7593 	CT_GET_NV_PARAM, "CT_GET_NV_PARAM", \
7594 	CT_GET_PARAM, "CT_GET_PARAM", \
7595 	CT_NV_PARAM_SIZE, "CT_NV_PARAM_SIZE", \
7596 	CT_COMMON_PARAM_SIZE, "CT_COMMON_PARAM_SIZE", \
7597 	CT_PLATFORM_PARAM_SIZE, "CT_PLATFORM_PARAM_SIZE", \
7598 	CT_SET_NV_PARAM, "CT_SET_NV_PARAM", \
7599 	CT_ABORT_SCRUB, "CT_ABORT_SCRUB", \
7600 	CT_GET_SCRUB_ERROR, "CT_GET_SCRUB_ERROR", \
7601 	CT_LABEL_CONTAINER, "CT_LABEL_CONTAINER", \
7602 	CT_CONTINUE_DATA, "CT_CONTINUE_DATA", \
7603 	CT_STOP_DATA, "CT_STOP_DATA", \
7604 	CT_GET_PARTITION_TABLE, "CT_GET_PARTITION_TABLE", \
7605 	CT_GET_DISK_PARTITIONS, "CT_GET_DISK_PARTITIONS", \
7606 	CT_GET_MISC_STATUS, "CT_GET_MISC_STATUS", \
7607 	CT_GET_CONTAINER_PERF_INFO, "CT_GET_CONTAINER_PERF_INFO", \
7608 	CT_GET_TIME, "CT_GET_TIME", \
7609 	CT_READ_DATA, "CT_READ_DATA", \
7610 	CT_CTR, "CT_CTR", \
7611 	CT_CTL, "CT_CTL", \
7612 	CT_DRAINIO, "CT_DRAINIO", \
7613 	CT_RELEASEIO, "CT_RELEASEIO", \
7614 	CT_GET_NVRAM, "CT_GET_NVRAM", \
7615 	CT_GET_MEMORY, "CT_GET_MEMORY", \
7616 	CT_PRINT_CT_LOG, "CT_PRINT_CT_LOG", \
7617 	CT_ADD_LEVEL, "CT_ADD_LEVEL", \
7618 	CT_NV_ZERO, "CT_NV_ZERO", \
7619 	CT_READ_SIGNATURE, "CT_READ_SIGNATURE", \
7620 	CT_THROTTLE_ON, "CT_THROTTLE_ON", \
7621 	CT_THROTTLE_OFF, "CT_THROTTLE_OFF", \
7622 	CT_GET_THROTTLE_STATS, "CT_GET_THROTTLE_STATS", \
7623 	CT_MAKE_SNAPSHOT, "CT_MAKE_SNAPSHOT", \
7624 	CT_REMOVE_SNAPSHOT, "CT_REMOVE_SNAPSHOT", \
7625 	CT_WRITE_USER_FLAGS, "CT_WRITE_USER_FLAGS", \
7626 	CT_READ_USER_FLAGS, "CT_READ_USER_FLAGS", \
7627 	CT_MONITOR, "CT_MONITOR", \
7628 	CT_GEN_MORPH, "CT_GEN_MORPH", \
7629 	CT_GET_SNAPSHOT_INFO, "CT_GET_SNAPSHOT_INFO", \
7630 	CT_CACHE_SET, "CT_CACHE_SET", \
7631 	CT_CACHE_STAT, "CT_CACHE_STAT", \
7632 	CT_TRACE_START, "CT_TRACE_START", \
7633 	CT_TRACE_STOP, "CT_TRACE_STOP", \
7634 	CT_TRACE_ENABLE, "CT_TRACE_ENABLE", \
7635 	CT_TRACE_DISABLE, "CT_TRACE_DISABLE", \
7636 	CT_FORCE_CORE_DUMP, "CT_FORCE_CORE_DUMP", \
7637 	CT_SET_SERIAL_NUMBER, "CT_SET_SERIAL_NUMBER", \
7638 	CT_RESET_SERIAL_NUMBER, "CT_RESET_SERIAL_NUMBER", \
7639 	CT_ENABLE_RAID5, "CT_ENABLE_RAID5", \
7640 	CT_CLEAR_VALID_DUMP_FLAG, "CT_CLEAR_VALID_DUMP_FLAG", \
7641 	CT_GET_MEM_STATS, "CT_GET_MEM_STATS", \
7642 	CT_GET_CORE_SIZE, "CT_GET_CORE_SIZE", \
7643 	CT_CREATE_CONTAINER_OLD, "CT_CREATE_CONTAINER_OLD", \
7644 	CT_STOP_DUMPS, "CT_STOP_DUMPS", \
7645 	CT_PANIC_ON_TAKE_A_BREAK, "CT_PANIC_ON_TAKE_A_BREAK", \
7646 	CT_GET_CACHE_STATS, "CT_GET_CACHE_STATS", \
7647 	CT_MOVE_PARTITION, "CT_MOVE_PARTITION", \
7648 	CT_FLUSH_CACHE, "CT_FLUSH_CACHE", \
7649 	CT_READ_NAME, "CT_READ_NAME", \
7650 	CT_WRITE_NAME, "CT_WRITE_NAME", \
7651 	CT_TOSS_CACHE, "CT_TOSS_CACHE", \
7652 	CT_LOCK_DRAINIO, "CT_LOCK_DRAINIO", \
7653 	CT_CONTAINER_OFFLINE, "CT_CONTAINER_OFFLINE", \
7654 	CT_SET_CACHE_SIZE, "CT_SET_CACHE_SIZE", \
7655 	CT_CLEAN_SHUTDOWN_STATUS, "CT_CLEAN_SHUTDOWN_STATUS", \
7656 	CT_CLEAR_DISKLOG_ON_DISK, "CT_CLEAR_DISKLOG_ON_DISK", \
7657 	CT_CLEAR_ALL_DISKLOG, "CT_CLEAR_ALL_DISKLOG", \
7658 	CT_CACHE_FAVOR, "CT_CACHE_FAVOR", \
7659 	CT_READ_PASSTHRU_MBR, "CT_READ_PASSTHRU_MBR", \
7660 	CT_SCRUB_NOFIX, "CT_SCRUB_NOFIX", \
7661 	CT_SCRUB2_NOFIX, "CT_SCRUB2_NOFIX", \
7662 	CT_FLUSH, "CT_FLUSH", \
7663 	CT_REBUILD, "CT_REBUILD", \
7664 	CT_FLUSH_CONTAINER, "CT_FLUSH_CONTAINER", \
7665 	CT_RESTART, "CT_RESTART", \
7666 	CT_GET_CONFIG_STATUS, "CT_GET_CONFIG_STATUS", \
7667 	CT_TRACE_FLAG, "CT_TRACE_FLAG", \
7668 	CT_RESTART_MORPH, "CT_RESTART_MORPH", \
7669 	CT_GET_TRACE_INFO, "CT_GET_TRACE_INFO", \
7670 	CT_GET_TRACE_ITEM, "CT_GET_TRACE_ITEM", \
7671 	CT_COMMIT_CONFIG, "CT_COMMIT_CONFIG", \
7672 	CT_CONTAINER_EXISTS, "CT_CONTAINER_EXISTS", \
7673 	CT_GET_SLICE_FROM_DEVT, "CT_GET_SLICE_FROM_DEVT", \
7674 	CT_OPEN_READ_WRITE, "CT_OPEN_READ_WRITE", \
7675 	CT_WRITE_MEMORY_BLOCK, "CT_WRITE_MEMORY_BLOCK", \
7676 	CT_GET_CACHE_PARAMS, "CT_GET_CACHE_PARAMS", \
7677 	CT_CRAZY_CACHE, "CT_CRAZY_CACHE", \
7678 	CT_GET_PROFILE_STRUCT, "CT_GET_PROFILE_STRUCT", \
7679 	CT_SET_IO_TRACE_FLAG, "CT_SET_IO_TRACE_FLAG", \
7680 	CT_GET_IO_TRACE_STRUCT, "CT_GET_IO_TRACE_STRUCT", \
7681 	CT_CID_TO_64BITS_UID, "CT_CID_TO_64BITS_UID", \
7682 	CT_64BITS_UID_TO_CID, "CT_64BITS_UID_TO_CID", \
7683 	CT_PAR_TO_64BITS_UID, "CT_PAR_TO_64BITS_UID", \
7684 	CT_CID_TO_32BITS_UID, "CT_CID_TO_32BITS_UID", \
7685 	CT_32BITS_UID_TO_CID, "CT_32BITS_UID_TO_CID", \
7686 	CT_PAR_TO_32BITS_UID, "CT_PAR_TO_32BITS_UID", \
7687 	CT_SET_FAILOVER_OPTION, "CT_SET_FAILOVER_OPTION", \
7688 	CT_GET_FAILOVER_OPTION, "CT_GET_FAILOVER_OPTION", \
7689 	CT_STRIPE_ADD2, "CT_STRIPE_ADD2", \
7690 	CT_CREATE_VOLUME_SET, "CT_CREATE_VOLUME_SET", \
7691 	CT_CREATE_STRIPE_SET, "CT_CREATE_STRIPE_SET", \
7692 	CT_VERIFY_CONTAINER, "CT_VERIFY_CONTAINER", \
7693 	CT_IS_CONTAINER_DEAD, "CT_IS_CONTAINER_DEAD", \
7694 	CT_GET_CONTAINER_OPTION, "CT_GET_CONTAINER_OPTION", \
7695 	CT_GET_SNAPSHOT_UNUSED_STRUCT, "CT_GET_SNAPSHOT_UNUSED_STRUCT", \
7696 	CT_CLEAR_SNAPSHOT_UNUSED_STRUCT, "CT_CLEAR_SNAPSHOT_UNUSED_STRUCT", \
7697 	CT_GET_CONTAINER_INFO, "CT_GET_CONTAINER_INFO", \
7698 	CT_CREATE_CONTAINER, "CT_CREATE_CONTAINER", \
7699 	CT_CHANGE_CREATIONINFO, "CT_CHANGE_CREATIONINFO", \
7700 	CT_CHECK_CONFLICT_UID, "CT_CHECK_CONFLICT_UID", \
7701 	CT_CONTAINER_UID_CHECK, "CT_CONTAINER_UID_CHECK", \
7702 	CT_IS_CONTAINER_MEATADATA_STANDARD, \
7703 	    "CT_IS_CONTAINER_MEATADATA_STANDARD", \
7704 	CT_IS_SLICE_METADATA_STANDARD, "CT_IS_SLICE_METADATA_STANDARD", \
7705 	CT_GET_IMPORT_COUNT, "CT_GET_IMPORT_COUNT", \
7706 	CT_CANCEL_ALL_IMPORTS, "CT_CANCEL_ALL_IMPORTS", \
7707 	CT_GET_IMPORT_INFO, "CT_GET_IMPORT_INFO", \
7708 	CT_IMPORT_ARRAY, "CT_IMPORT_ARRAY", \
7709 	CT_GET_LOG_SIZE, "CT_GET_LOG_SIZE", \
7710 	CT_ALARM_GET_STATE, "CT_ALARM_GET_STATE", \
7711 	CT_ALARM_SET_STATE, "CT_ALARM_SET_STATE", \
7712 	CT_ALARM_ON_OFF, "CT_ALARM_ON_OFF", \
7713 	CT_GET_EE_OEM_ID, "CT_GET_EE_OEM_ID", \
7714 	CT_GET_PPI_HEADERS, "CT_GET_PPI_HEADERS", \
7715 	CT_GET_PPI_DATA, "CT_GET_PPI_DATA", \
7716 	CT_GET_PPI_ENTRIES, "CT_GET_PPI_ENTRIES", \
7717 	CT_DELETE_PPI_BUNDLE, "CT_DELETE_PPI_BUNDLE", \
7718 	CT_GET_PARTITION_TABLE_2, "CT_GET_PARTITION_TABLE_2", \
7719 	CT_GET_PARTITION_INFO_2, "CT_GET_PARTITION_INFO_2", \
7720 	CT_GET_DISK_PARTITIONS_2, "CT_GET_DISK_PARTITIONS_2", \
7721 	CT_QUIESCE_ADAPTER, "CT_QUIESCE_ADAPTER", \
7722 	CT_CLEAR_PPI_TABLE, "CT_CLEAR_PPI_TABLE"
7723 
7724 #define	AAC_CL_SUBCMD_KEY_STRINGS \
7725 	CL_NULL, "CL_NULL", \
7726 	DS_INIT, "DS_INIT", \
7727 	DS_RESCAN, "DS_RESCAN", \
7728 	DS_CREATE, "DS_CREATE", \
7729 	DS_DELETE, "DS_DELETE", \
7730 	DS_ADD_DISK, "DS_ADD_DISK", \
7731 	DS_REMOVE_DISK, "DS_REMOVE_DISK", \
7732 	DS_MOVE_DISK, "DS_MOVE_DISK", \
7733 	DS_TAKE_OWNERSHIP, "DS_TAKE_OWNERSHIP", \
7734 	DS_RELEASE_OWNERSHIP, "DS_RELEASE_OWNERSHIP", \
7735 	DS_FORCE_OWNERSHIP, "DS_FORCE_OWNERSHIP", \
7736 	DS_GET_DISK_SET_PARAM, "DS_GET_DISK_SET_PARAM", \
7737 	DS_GET_DRIVE_PARAM, "DS_GET_DRIVE_PARAM", \
7738 	DS_GET_SLICE_PARAM, "DS_GET_SLICE_PARAM", \
7739 	DS_GET_DISK_SETS, "DS_GET_DISK_SETS", \
7740 	DS_GET_DRIVES, "DS_GET_DRIVES", \
7741 	DS_SET_DISK_SET_PARAM, "DS_SET_DISK_SET_PARAM", \
7742 	DS_ONLINE, "DS_ONLINE", \
7743 	DS_OFFLINE, "DS_OFFLINE", \
7744 	DS_ONLINE_CONTAINERS, "DS_ONLINE_CONTAINERS", \
7745 	DS_FSAPRINT, "DS_FSAPRINT", \
7746 	CL_CFG_SET_HOST_IDS, "CL_CFG_SET_HOST_IDS", \
7747 	CL_CFG_SET_PARTNER_HOST_IDS, "CL_CFG_SET_PARTNER_HOST_IDS", \
7748 	CL_CFG_GET_CLUSTER_CONFIG, "CL_CFG_GET_CLUSTER_CONFIG", \
7749 	CC_CLI_CLEAR_MESSAGE_BUFFER, "CC_CLI_CLEAR_MESSAGE_BUFFER", \
7750 	CC_SRV_CLEAR_MESSAGE_BUFFER, "CC_SRV_CLEAR_MESSAGE_BUFFER", \
7751 	CC_CLI_SHOW_MESSAGE_BUFFER, "CC_CLI_SHOW_MESSAGE_BUFFER", \
7752 	CC_SRV_SHOW_MESSAGE_BUFFER, "CC_SRV_SHOW_MESSAGE_BUFFER", \
7753 	CC_CLI_SEND_MESSAGE, "CC_CLI_SEND_MESSAGE", \
7754 	CC_SRV_SEND_MESSAGE, "CC_SRV_SEND_MESSAGE", \
7755 	CC_CLI_GET_MESSAGE, "CC_CLI_GET_MESSAGE", \
7756 	CC_SRV_GET_MESSAGE, "CC_SRV_GET_MESSAGE", \
7757 	CC_SEND_TEST_MESSAGE, "CC_SEND_TEST_MESSAGE", \
7758 	CC_GET_BUSINFO, "CC_GET_BUSINFO", \
7759 	CC_GET_PORTINFO, "CC_GET_PORTINFO", \
7760 	CC_GET_NAMEINFO, "CC_GET_NAMEINFO", \
7761 	CC_GET_CONFIGINFO, "CC_GET_CONFIGINFO", \
7762 	CQ_QUORUM_OP, "CQ_QUORUM_OP"
7763 
7764 #define	AAC_AIF_SUBCMD_KEY_STRINGS \
7765 	AifCmdEventNotify, "AifCmdEventNotify", \
7766 	AifCmdJobProgress, "AifCmdJobProgress", \
7767 	AifCmdAPIReport, "AifCmdAPIReport", \
7768 	AifCmdDriverNotify, "AifCmdDriverNotify", \
7769 	AifReqJobList, "AifReqJobList", \
7770 	AifReqJobsForCtr, "AifReqJobsForCtr", \
7771 	AifReqJobsForScsi, "AifReqJobsForScsi", \
7772 	AifReqJobReport, "AifReqJobReport", \
7773 	AifReqTerminateJob, "AifReqTerminateJob", \
7774 	AifReqSuspendJob, "AifReqSuspendJob", \
7775 	AifReqResumeJob, "AifReqResumeJob", \
7776 	AifReqSendAPIReport, "AifReqSendAPIReport", \
7777 	AifReqAPIJobStart, "AifReqAPIJobStart", \
7778 	AifReqAPIJobUpdate, "AifReqAPIJobUpdate", \
7779 	AifReqAPIJobFinish, "AifReqAPIJobFinish"
7780 
7781 #define	AAC_IOCTL_SUBCMD_KEY_STRINGS \
7782 	Reserved_IOCTL, "Reserved_IOCTL", \
7783 	GetDeviceHandle, "GetDeviceHandle", \
7784 	BusTargetLun_to_DeviceHandle, "BusTargetLun_to_DeviceHandle", \
7785 	DeviceHandle_to_BusTargetLun, "DeviceHandle_to_BusTargetLun", \
7786 	RescanBus, "RescanBus", \
7787 	GetDeviceProbeInfo, "GetDeviceProbeInfo", \
7788 	GetDeviceCapacity, "GetDeviceCapacity", \
7789 	GetContainerProbeInfo, "GetContainerProbeInfo", \
7790 	GetRequestedMemorySize, "GetRequestedMemorySize", \
7791 	GetBusInfo, "GetBusInfo", \
7792 	GetVendorSpecific, "GetVendorSpecific", \
7793 	EnhancedGetDeviceProbeInfo, "EnhancedGetDeviceProbeInfo", \
7794 	EnhancedGetBusInfo, "EnhancedGetBusInfo", \
7795 	SetupExtendedCounters, "SetupExtendedCounters", \
7796 	GetPerformanceCounters, "GetPerformanceCounters", \
7797 	ResetPerformanceCounters, "ResetPerformanceCounters", \
7798 	ReadModePage, "ReadModePage", \
7799 	WriteModePage, "WriteModePage", \
7800 	ReadDriveParameter, "ReadDriveParameter", \
7801 	WriteDriveParameter, "WriteDriveParameter", \
7802 	ResetAdapter, "ResetAdapter", \
7803 	ResetBus, "ResetBus", \
7804 	ResetBusDevice, "ResetBusDevice", \
7805 	ExecuteSrb, "ExecuteSrb", \
7806 	Create_IO_Task, "Create_IO_Task", \
7807 	Delete_IO_Task, "Delete_IO_Task", \
7808 	Get_IO_Task_Info, "Get_IO_Task_Info", \
7809 	Check_Task_Progress, "Check_Task_Progress", \
7810 	InjectError, "InjectError", \
7811 	GetDeviceDefectCounts, "GetDeviceDefectCounts", \
7812 	GetDeviceDefectInfo, "GetDeviceDefectInfo", \
7813 	GetDeviceStatus, "GetDeviceStatus", \
7814 	ClearDeviceStatus, "ClearDeviceStatus", \
7815 	DiskSpinControl, "DiskSpinControl", \
7816 	DiskSmartControl, "DiskSmartControl", \
7817 	WriteSame, "WriteSame", \
7818 	ReadWriteLong, "ReadWriteLong", \
7819 	FormatUnit, "FormatUnit", \
7820 	TargetDeviceControl, "TargetDeviceControl", \
7821 	TargetChannelControl, "TargetChannelControl", \
7822 	FlashNewCode, "FlashNewCode", \
7823 	DiskCheck, "DiskCheck", \
7824 	RequestSense, "RequestSense", \
7825 	DiskPERControl, "DiskPERControl", \
7826 	Read10, "Read10", \
7827 	Write10, "Write10"
7828 
7829 #define	AAC_AIFEN_KEY_STRINGS \
7830 	AifEnGeneric, "Generic", \
7831 	AifEnTaskComplete, "TaskComplete", \
7832 	AifEnConfigChange, "Config change", \
7833 	AifEnContainerChange, "Container change", \
7834 	AifEnDeviceFailure, "device failed", \
7835 	AifEnMirrorFailover, "Mirror failover", \
7836 	AifEnContainerEvent, "container event", \
7837 	AifEnFileSystemChange, "File system changed", \
7838 	AifEnConfigPause, "Container pause event", \
7839 	AifEnConfigResume, "Container resume event", \
7840 	AifEnFailoverChange, "Failover space assignment changed", \
7841 	AifEnRAID5RebuildDone, "RAID5 rebuild finished", \
7842 	AifEnEnclosureManagement, "Enclosure management event", \
7843 	AifEnBatteryEvent, "battery event", \
7844 	AifEnAddContainer, "Add container", \
7845 	AifEnDeleteContainer, "Delete container", \
7846 	AifEnSMARTEvent, "SMART Event", \
7847 	AifEnBatteryNeedsRecond, "battery needs reconditioning", \
7848 	AifEnClusterEvent, "cluster event", \
7849 	AifEnDiskSetEvent, "disk set event occured", \
7850 	AifDenMorphComplete, "morph operation completed", \
7851 	AifDenVolumeExtendComplete, "VolumeExtendComplete"
7852 
7853 struct aac_key_strings {
7854 	int key;
7855 	char *message;
7856 };
7857 
7858 extern struct scsi_key_strings scsi_cmds[];
7859 
7860 static struct aac_key_strings aac_fib_cmds[] = {
7861 	AAC_FIB_CMD_KEY_STRINGS,
7862 	-1,			NULL
7863 };
7864 
7865 static struct aac_key_strings aac_ctvm_subcmds[] = {
7866 	AAC_CTVM_SUBCMD_KEY_STRINGS,
7867 	-1,			NULL
7868 };
7869 
7870 static struct aac_key_strings aac_ct_subcmds[] = {
7871 	AAC_CT_SUBCMD_KEY_STRINGS,
7872 	-1,			NULL
7873 };
7874 
7875 static struct aac_key_strings aac_cl_subcmds[] = {
7876 	AAC_CL_SUBCMD_KEY_STRINGS,
7877 	-1,			NULL
7878 };
7879 
7880 static struct aac_key_strings aac_aif_subcmds[] = {
7881 	AAC_AIF_SUBCMD_KEY_STRINGS,
7882 	-1,			NULL
7883 };
7884 
7885 static struct aac_key_strings aac_ioctl_subcmds[] = {
7886 	AAC_IOCTL_SUBCMD_KEY_STRINGS,
7887 	-1,			NULL
7888 };
7889 
7890 static struct aac_key_strings aac_aifens[] = {
7891 	AAC_AIFEN_KEY_STRINGS,
7892 	-1,			NULL
7893 };
7894 
7895 /*
7896  * The following function comes from Adaptec:
7897  *
7898  * Get the firmware print buffer parameters from the firmware,
7899  * if the command was successful map in the address.
7900  */
7901 static int
7902 aac_get_fw_debug_buffer(struct aac_softstate *softs)
7903 {
7904 	if (aac_sync_mbcommand(softs, AAC_MONKER_GETDRVPROP,
7905 	    0, 0, 0, 0, NULL) == AACOK) {
7906 		uint32_t mondrv_buf_paddrl = AAC_MAILBOX_GET(softs, 1);
7907 		uint32_t mondrv_buf_paddrh = AAC_MAILBOX_GET(softs, 2);
7908 		uint32_t mondrv_buf_size = AAC_MAILBOX_GET(softs, 3);
7909 		uint32_t mondrv_hdr_size = AAC_MAILBOX_GET(softs, 4);
7910 
7911 		if (mondrv_buf_size) {
7912 			uint32_t offset = mondrv_buf_paddrl - \
7913 			    softs->pci_mem_base_paddr;
7914 
7915 			/*
7916 			 * See if the address is already mapped in, and
7917 			 * if so set it up from the base address
7918 			 */
7919 			if ((mondrv_buf_paddrh == 0) &&
7920 			    (offset + mondrv_buf_size < softs->map_size)) {
7921 				mutex_enter(&aac_prt_mutex);
7922 				softs->debug_buf_offset = offset;
7923 				softs->debug_header_size = mondrv_hdr_size;
7924 				softs->debug_buf_size = mondrv_buf_size;
7925 				softs->debug_fw_flags = 0;
7926 				softs->debug_flags &= ~AACDB_FLAGS_FW_PRINT;
7927 				mutex_exit(&aac_prt_mutex);
7928 
7929 				return (AACOK);
7930 			}
7931 		}
7932 	}
7933 	return (AACERR);
7934 }
7935 
7936 int
7937 aac_dbflag_on(struct aac_softstate *softs, int flag)
7938 {
7939 	int debug_flags = softs ? softs->debug_flags : aac_debug_flags;
7940 
7941 	return ((debug_flags & (AACDB_FLAGS_FW_PRINT | \
7942 	    AACDB_FLAGS_KERNEL_PRINT)) && (debug_flags & flag));
7943 }
7944 
7945 static void
7946 aac_cmn_err(struct aac_softstate *softs, uint_t lev, char sl, int noheader)
7947 {
7948 	if (noheader) {
7949 		if (sl) {
7950 			aac_fmt[0] = sl;
7951 			cmn_err(lev, aac_fmt, aac_prt_buf);
7952 		} else {
7953 			cmn_err(lev, &aac_fmt[1], aac_prt_buf);
7954 		}
7955 	} else {
7956 		if (sl) {
7957 			aac_fmt_header[0] = sl;
7958 			cmn_err(lev, aac_fmt_header,
7959 			    softs->vendor_name, softs->instance,
7960 			    aac_prt_buf);
7961 		} else {
7962 			cmn_err(lev, &aac_fmt_header[1],
7963 			    softs->vendor_name, softs->instance,
7964 			    aac_prt_buf);
7965 		}
7966 	}
7967 }
7968 
7969 /*
7970  * The following function comes from Adaptec:
7971  *
7972  * Format and print out the data passed in to UART or console
7973  * as specified by debug flags.
7974  */
7975 void
7976 aac_printf(struct aac_softstate *softs, uint_t lev, const char *fmt, ...)
7977 {
7978 	va_list args;
7979 	char sl; /* system log character */
7980 
7981 	mutex_enter(&aac_prt_mutex);
7982 	/* Set up parameters and call sprintf function to format the data */
7983 	if (strchr("^!?", fmt[0]) == NULL) {
7984 		sl = 0;
7985 	} else {
7986 		sl = fmt[0];
7987 		fmt++;
7988 	}
7989 	va_start(args, fmt);
7990 	(void) vsprintf(aac_prt_buf, fmt, args);
7991 	va_end(args);
7992 
7993 	/* Make sure the softs structure has been passed in for this section */
7994 	if (softs) {
7995 		if ((softs->debug_flags & AACDB_FLAGS_FW_PRINT) &&
7996 		    /* If we are set up for a Firmware print */
7997 		    (softs->debug_buf_size)) {
7998 			uint32_t count, i;
7999 
8000 			/* Make sure the string size is within boundaries */
8001 			count = strlen(aac_prt_buf);
8002 			if (count > softs->debug_buf_size)
8003 				count = (uint16_t)softs->debug_buf_size;
8004 
8005 			/*
8006 			 * Wait for no more than AAC_PRINT_TIMEOUT for the
8007 			 * previous message length to clear (the handshake).
8008 			 */
8009 			for (i = 0; i < AAC_PRINT_TIMEOUT; i++) {
8010 				if (!PCI_MEM_GET32(softs,
8011 				    softs->debug_buf_offset + \
8012 				    AAC_FW_DBG_STRLEN_OFFSET))
8013 					break;
8014 
8015 				drv_usecwait(1000);
8016 			}
8017 
8018 			/*
8019 			 * If the length is clear, copy over the message, the
8020 			 * flags, and the length. Make sure the length is the
8021 			 * last because that is the signal for the Firmware to
8022 			 * pick it up.
8023 			 */
8024 			if (!PCI_MEM_GET32(softs, softs->debug_buf_offset + \
8025 			    AAC_FW_DBG_STRLEN_OFFSET)) {
8026 				PCI_MEM_REP_PUT8(softs,
8027 				    softs->debug_buf_offset + \
8028 				    softs->debug_header_size,
8029 				    aac_prt_buf, count);
8030 				PCI_MEM_PUT32(softs,
8031 				    softs->debug_buf_offset + \
8032 				    AAC_FW_DBG_FLAGS_OFFSET,
8033 				    softs->debug_fw_flags);
8034 				PCI_MEM_PUT32(softs,
8035 				    softs->debug_buf_offset + \
8036 				    AAC_FW_DBG_STRLEN_OFFSET, count);
8037 			} else {
8038 				cmn_err(CE_WARN, "UART output fail");
8039 				softs->debug_flags &= ~AACDB_FLAGS_FW_PRINT;
8040 			}
8041 		}
8042 
8043 		/*
8044 		 * If the Kernel Debug Print flag is set, send it off
8045 		 * to the Kernel Debugger
8046 		 */
8047 		if (softs->debug_flags & AACDB_FLAGS_KERNEL_PRINT)
8048 			aac_cmn_err(softs, lev, sl,
8049 			    (softs->debug_flags & AACDB_FLAGS_NO_HEADERS));
8050 	} else {
8051 		/* Driver not initialized yet, no firmware or header output */
8052 		if (aac_debug_flags & AACDB_FLAGS_KERNEL_PRINT)
8053 			aac_cmn_err(softs, lev, sl, 1);
8054 	}
8055 	mutex_exit(&aac_prt_mutex);
8056 }
8057 
8058 /*
8059  * Translate command number to description string
8060  */
8061 static char *
8062 aac_cmd_name(int cmd, struct aac_key_strings *cmdlist)
8063 {
8064 	int i;
8065 
8066 	for (i = 0; cmdlist[i].key != -1; i++) {
8067 		if (cmd == cmdlist[i].key)
8068 			return (cmdlist[i].message);
8069 	}
8070 	return (NULL);
8071 }
8072 
8073 static void
8074 aac_print_scmd(struct aac_softstate *softs, struct aac_cmd *acp)
8075 {
8076 	struct scsi_pkt *pkt = acp->pkt;
8077 	struct scsi_address *ap = &pkt->pkt_address;
8078 	int is_pd = 0;
8079 	int ctl = ddi_get_instance(softs->devinfo_p);
8080 	int tgt = ap->a_target;
8081 	int lun = ap->a_lun;
8082 	union scsi_cdb *cdbp = (void *)pkt->pkt_cdbp;
8083 	uchar_t cmd = cdbp->scc_cmd;
8084 	char *desc;
8085 
8086 	if (tgt >= AAC_MAX_LD) {
8087 		is_pd = 1;
8088 		ctl = ((struct aac_nondasd *)acp->dvp)->bus;
8089 		tgt = ((struct aac_nondasd *)acp->dvp)->tid;
8090 		lun = 0;
8091 	}
8092 
8093 	if ((desc = aac_cmd_name(cmd,
8094 	    (struct aac_key_strings *)scsi_cmds)) == NULL) {
8095 		aac_printf(softs, CE_NOTE,
8096 		    "SCMD> Unknown(0x%2x) --> c%dt%dL%d %s",
8097 		    cmd, ctl, tgt, lun, is_pd ? "(pd)" : "");
8098 		return;
8099 	}
8100 
8101 	switch (cmd) {
8102 	case SCMD_READ:
8103 	case SCMD_WRITE:
8104 		aac_printf(softs, CE_NOTE,
8105 		    "SCMD> %s 0x%x[%d] %s --> c%dt%dL%d %s",
8106 		    desc, GETG0ADDR(cdbp), GETG0COUNT(cdbp),
8107 		    (acp->flags & AAC_CMD_NO_INTR) ? "poll" : "intr",
8108 		    ctl, tgt, lun, is_pd ? "(pd)" : "");
8109 		break;
8110 	case SCMD_READ_G1:
8111 	case SCMD_WRITE_G1:
8112 		aac_printf(softs, CE_NOTE,
8113 		    "SCMD> %s 0x%x[%d] %s --> c%dt%dL%d %s",
8114 		    desc, GETG1ADDR(cdbp), GETG1COUNT(cdbp),
8115 		    (acp->flags & AAC_CMD_NO_INTR) ? "poll" : "intr",
8116 		    ctl, tgt, lun, is_pd ? "(pd)" : "");
8117 		break;
8118 	case SCMD_READ_G4:
8119 	case SCMD_WRITE_G4:
8120 		aac_printf(softs, CE_NOTE,
8121 		    "SCMD> %s 0x%x.%08x[%d] %s --> c%dt%dL%d %s",
8122 		    desc, GETG4ADDR(cdbp), GETG4ADDRTL(cdbp),
8123 		    GETG4COUNT(cdbp),
8124 		    (acp->flags & AAC_CMD_NO_INTR) ? "poll" : "intr",
8125 		    ctl, tgt, lun, is_pd ? "(pd)" : "");
8126 		break;
8127 	case SCMD_READ_G5:
8128 	case SCMD_WRITE_G5:
8129 		aac_printf(softs, CE_NOTE,
8130 		    "SCMD> %s 0x%x[%d] %s --> c%dt%dL%d %s",
8131 		    desc, GETG5ADDR(cdbp), GETG5COUNT(cdbp),
8132 		    (acp->flags & AAC_CMD_NO_INTR) ? "poll" : "intr",
8133 		    ctl, tgt, lun, is_pd ? "(pd)" : "");
8134 		break;
8135 	default:
8136 		aac_printf(softs, CE_NOTE, "SCMD> %s --> c%dt%dL%d %s",
8137 		    desc, ctl, tgt, lun, is_pd ? "(pd)" : "");
8138 	}
8139 }
8140 
8141 void
8142 aac_print_fib(struct aac_softstate *softs, struct aac_slot *slotp)
8143 {
8144 	struct aac_cmd *acp = slotp->acp;
8145 	struct aac_fib *fibp = slotp->fibp;
8146 	ddi_acc_handle_t acc = slotp->fib_acc_handle;
8147 	uint16_t fib_size;
8148 	uint32_t fib_cmd, sub_cmd;
8149 	char *cmdstr, *subcmdstr;
8150 	char *caller;
8151 	int i;
8152 
8153 	if (acp) {
8154 		if (!(softs->debug_fib_flags & acp->fib_flags))
8155 			return;
8156 		if (acp->fib_flags & AACDB_FLAGS_FIB_SCMD)
8157 			caller = "SCMD";
8158 		else if (acp->fib_flags & AACDB_FLAGS_FIB_IOCTL)
8159 			caller = "IOCTL";
8160 		else if (acp->fib_flags & AACDB_FLAGS_FIB_SRB)
8161 			caller = "SRB";
8162 		else
8163 			return;
8164 	} else {
8165 		if (!(softs->debug_fib_flags & AACDB_FLAGS_FIB_SYNC))
8166 			return;
8167 		caller = "SYNC";
8168 	}
8169 
8170 	fib_cmd = ddi_get16(acc, &fibp->Header.Command);
8171 	cmdstr = aac_cmd_name(fib_cmd, aac_fib_cmds);
8172 	sub_cmd = (uint32_t)-1;
8173 	subcmdstr = NULL;
8174 
8175 	/* Print FIB header */
8176 	if (softs->debug_fib_flags & AACDB_FLAGS_FIB_HEADER) {
8177 		aac_printf(softs, CE_NOTE, "FIB> from %s", caller);
8178 		aac_printf(softs, CE_NOTE, "     XferState  %d",
8179 		    ddi_get32(acc, &fibp->Header.XferState));
8180 		aac_printf(softs, CE_NOTE, "     Command    %d",
8181 		    ddi_get16(acc, &fibp->Header.Command));
8182 		aac_printf(softs, CE_NOTE, "     StructType %d",
8183 		    ddi_get8(acc, &fibp->Header.StructType));
8184 		aac_printf(softs, CE_NOTE, "     Flags      0x%x",
8185 		    ddi_get8(acc, &fibp->Header.Flags));
8186 		aac_printf(softs, CE_NOTE, "     Size       %d",
8187 		    ddi_get16(acc, &fibp->Header.Size));
8188 		aac_printf(softs, CE_NOTE, "     SenderSize %d",
8189 		    ddi_get16(acc, &fibp->Header.SenderSize));
8190 		aac_printf(softs, CE_NOTE, "     SenderAddr 0x%x",
8191 		    ddi_get32(acc, &fibp->Header.SenderFibAddress));
8192 		aac_printf(softs, CE_NOTE, "     RcvrAddr   0x%x",
8193 		    ddi_get32(acc, &fibp->Header.ReceiverFibAddress));
8194 		aac_printf(softs, CE_NOTE, "     SenderData 0x%x",
8195 		    ddi_get32(acc, &fibp->Header.SenderData));
8196 	}
8197 
8198 	/* Print FIB data */
8199 	switch (fib_cmd) {
8200 	case ContainerCommand:
8201 		sub_cmd = ddi_get32(acc,
8202 		    (void *)&(((uint32_t *)(void *)&fibp->data[0])[0]));
8203 		subcmdstr = aac_cmd_name(sub_cmd, aac_ctvm_subcmds);
8204 		if (subcmdstr == NULL)
8205 			break;
8206 
8207 		switch (sub_cmd) {
8208 		case VM_ContainerConfig: {
8209 			struct aac_Container *pContainer =
8210 			    (struct aac_Container *)fibp->data;
8211 
8212 			fib_cmd = sub_cmd;
8213 			cmdstr = subcmdstr;
8214 			sub_cmd = (uint32_t)-1;
8215 			subcmdstr = NULL;
8216 
8217 			sub_cmd = ddi_get32(acc,
8218 			    &pContainer->CTCommand.command);
8219 			subcmdstr = aac_cmd_name(sub_cmd, aac_ct_subcmds);
8220 			if (subcmdstr == NULL)
8221 				break;
8222 			aac_printf(softs, CE_NOTE, "FIB> %s (0x%x, 0x%x, 0x%x)",
8223 			    subcmdstr,
8224 			    ddi_get32(acc, &pContainer->CTCommand.param[0]),
8225 			    ddi_get32(acc, &pContainer->CTCommand.param[1]),
8226 			    ddi_get32(acc, &pContainer->CTCommand.param[2]));
8227 			return;
8228 		}
8229 
8230 		case VM_Ioctl:
8231 			fib_cmd = sub_cmd;
8232 			cmdstr = subcmdstr;
8233 			sub_cmd = (uint32_t)-1;
8234 			subcmdstr = NULL;
8235 
8236 			sub_cmd = ddi_get32(acc,
8237 			    (void *)&(((uint32_t *)(void *)&fibp->data[0])[4]));
8238 			subcmdstr = aac_cmd_name(sub_cmd, aac_ioctl_subcmds);
8239 			break;
8240 
8241 		case VM_CtBlockRead:
8242 		case VM_CtBlockWrite: {
8243 			struct aac_blockread *br =
8244 			    (struct aac_blockread *)fibp->data;
8245 			struct aac_sg_table *sg = &br->SgMap;
8246 			uint32_t sgcount = ddi_get32(acc, &sg->SgCount);
8247 
8248 			aac_printf(softs, CE_NOTE,
8249 			    "FIB> %s Container %d  0x%x/%d", subcmdstr,
8250 			    ddi_get32(acc, &br->ContainerId),
8251 			    ddi_get32(acc, &br->BlockNumber),
8252 			    ddi_get32(acc, &br->ByteCount));
8253 			for (i = 0; i < sgcount; i++)
8254 				aac_printf(softs, CE_NOTE,
8255 				    "     %d: 0x%08x/%d", i,
8256 				    ddi_get32(acc, &sg->SgEntry[i].SgAddress),
8257 				    ddi_get32(acc, &sg->SgEntry[i]. \
8258 				    SgByteCount));
8259 			return;
8260 		}
8261 		}
8262 		break;
8263 
8264 	case ContainerCommand64: {
8265 		struct aac_blockread64 *br =
8266 		    (struct aac_blockread64 *)fibp->data;
8267 		struct aac_sg_table64 *sg = &br->SgMap64;
8268 		uint32_t sgcount = ddi_get32(acc, &sg->SgCount);
8269 		uint64_t sgaddr;
8270 
8271 		sub_cmd = br->Command;
8272 		subcmdstr = NULL;
8273 		if (sub_cmd == VM_CtHostRead64)
8274 			subcmdstr = "VM_CtHostRead64";
8275 		else if (sub_cmd == VM_CtHostWrite64)
8276 			subcmdstr = "VM_CtHostWrite64";
8277 		else
8278 			break;
8279 
8280 		aac_printf(softs, CE_NOTE,
8281 		    "FIB> %s Container %d  0x%x/%d", subcmdstr,
8282 		    ddi_get16(acc, &br->ContainerId),
8283 		    ddi_get32(acc, &br->BlockNumber),
8284 		    ddi_get16(acc, &br->SectorCount));
8285 		for (i = 0; i < sgcount; i++) {
8286 			sgaddr = ddi_get64(acc,
8287 			    &sg->SgEntry64[i].SgAddress);
8288 			aac_printf(softs, CE_NOTE,
8289 			    "     %d: 0x%08x.%08x/%d", i,
8290 			    AAC_MS32(sgaddr), AAC_LS32(sgaddr),
8291 			    ddi_get32(acc, &sg->SgEntry64[i]. \
8292 			    SgByteCount));
8293 		}
8294 		return;
8295 	}
8296 
8297 	case RawIo: {
8298 		struct aac_raw_io *io = (struct aac_raw_io *)fibp->data;
8299 		struct aac_sg_tableraw *sg = &io->SgMapRaw;
8300 		uint32_t sgcount = ddi_get32(acc, &sg->SgCount);
8301 		uint64_t sgaddr;
8302 
8303 		aac_printf(softs, CE_NOTE,
8304 		    "FIB> RawIo Container %d  0x%llx/%d 0x%x",
8305 		    ddi_get16(acc, &io->ContainerId),
8306 		    ddi_get64(acc, &io->BlockNumber),
8307 		    ddi_get32(acc, &io->ByteCount),
8308 		    ddi_get16(acc, &io->Flags));
8309 		for (i = 0; i < sgcount; i++) {
8310 			sgaddr = ddi_get64(acc, &sg->SgEntryRaw[i].SgAddress);
8311 			aac_printf(softs, CE_NOTE, "     %d: 0x%08x.%08x/%d", i,
8312 			    AAC_MS32(sgaddr), AAC_LS32(sgaddr),
8313 			    ddi_get32(acc, &sg->SgEntryRaw[i].SgByteCount));
8314 		}
8315 		return;
8316 	}
8317 
8318 	case ClusterCommand:
8319 		sub_cmd = ddi_get32(acc,
8320 		    (void *)&(((uint32_t *)(void *)fibp->data)[0]));
8321 		subcmdstr = aac_cmd_name(sub_cmd, aac_cl_subcmds);
8322 		break;
8323 
8324 	case AifRequest:
8325 		sub_cmd = ddi_get32(acc,
8326 		    (void *)&(((uint32_t *)(void *)fibp->data)[0]));
8327 		subcmdstr = aac_cmd_name(sub_cmd, aac_aif_subcmds);
8328 		break;
8329 
8330 	default:
8331 		break;
8332 	}
8333 
8334 	fib_size = ddi_get16(acc, &(fibp->Header.Size));
8335 	if (subcmdstr)
8336 		aac_printf(softs, CE_NOTE, "FIB> %s, sz=%d",
8337 		    subcmdstr, fib_size);
8338 	else if (cmdstr && sub_cmd == (uint32_t)-1)
8339 		aac_printf(softs, CE_NOTE, "FIB> %s, sz=%d",
8340 		    cmdstr, fib_size);
8341 	else if (cmdstr)
8342 		aac_printf(softs, CE_NOTE, "FIB> %s: Unknown(0x%x), sz=%d",
8343 		    cmdstr, sub_cmd, fib_size);
8344 	else
8345 		aac_printf(softs, CE_NOTE, "FIB> Unknown(0x%x), sz=%d",
8346 		    fib_cmd, fib_size);
8347 }
8348 
8349 static void
8350 aac_print_aif(struct aac_softstate *softs, struct aac_aif_command *aif)
8351 {
8352 	int aif_command;
8353 	uint32_t aif_seqnumber;
8354 	int aif_en_type;
8355 	char *str;
8356 
8357 	aif_command = LE_32(aif->command);
8358 	aif_seqnumber = LE_32(aif->seqNumber);
8359 	aif_en_type = LE_32(aif->data.EN.type);
8360 
8361 	switch (aif_command) {
8362 	case AifCmdEventNotify:
8363 		str = aac_cmd_name(aif_en_type, aac_aifens);
8364 		if (str)
8365 			aac_printf(softs, CE_NOTE, "AIF! %s", str);
8366 		else
8367 			aac_printf(softs, CE_NOTE, "AIF! Unknown(0x%x)",
8368 			    aif_en_type);
8369 		break;
8370 
8371 	case AifCmdJobProgress:
8372 		switch (LE_32(aif->data.PR[0].status)) {
8373 		case AifJobStsSuccess:
8374 			str = "success"; break;
8375 		case AifJobStsFinished:
8376 			str = "finished"; break;
8377 		case AifJobStsAborted:
8378 			str = "aborted"; break;
8379 		case AifJobStsFailed:
8380 			str = "failed"; break;
8381 		case AifJobStsSuspended:
8382 			str = "suspended"; break;
8383 		case AifJobStsRunning:
8384 			str = "running"; break;
8385 		default:
8386 			str = "unknown"; break;
8387 		}
8388 		aac_printf(softs, CE_NOTE,
8389 		    "AIF! JobProgress (%d) - %s (%d, %d)",
8390 		    aif_seqnumber, str,
8391 		    LE_32(aif->data.PR[0].currentTick),
8392 		    LE_32(aif->data.PR[0].finalTick));
8393 		break;
8394 
8395 	case AifCmdAPIReport:
8396 		aac_printf(softs, CE_NOTE, "AIF! APIReport (%d)",
8397 		    aif_seqnumber);
8398 		break;
8399 
8400 	case AifCmdDriverNotify:
8401 		aac_printf(softs, CE_NOTE, "AIF! DriverNotify (%d)",
8402 		    aif_seqnumber);
8403 		break;
8404 
8405 	default:
8406 		aac_printf(softs, CE_NOTE, "AIF! AIF %d (%d)",
8407 		    aif_command, aif_seqnumber);
8408 		break;
8409 	}
8410 }
8411 
8412 #endif /* DEBUG */
8413