xref: /netbsd-src/sys/dev/pci/mpii.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: mpii.c,v 1.4 2013/10/17 21:06:15 christos Exp $ */
2 /*	OpenBSD: mpii.c,v 1.51 2012/04/11 13:29:14 naddy Exp 	*/
3 /*
4  * Copyright (c) 2010 Mike Belopuhov <mkb@crypt.org.ru>
5  * Copyright (c) 2009 James Giannoules
6  * Copyright (c) 2005 - 2010 David Gwynne <dlg@openbsd.org>
7  * Copyright (c) 2005 - 2010 Marco Peereboom <marco@openbsd.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <sys/cdefs.h>
23 __KERNEL_RCSID(0, "$NetBSD: mpii.c,v 1.4 2013/10/17 21:06:15 christos Exp $");
24 
25 #include "bio.h"
26 
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/buf.h>
30 #include <sys/device.h>
31 #include <sys/ioctl.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/mutex.h>
35 #include <sys/condvar.h>
36 #include <sys/dkio.h>
37 #include <sys/tree.h>
38 
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcidevs.h>
42 
43 #include <dev/scsipi/scsipi_all.h>
44 #include <dev/scsipi/scsi_all.h>
45 #include <dev/scsipi/scsiconf.h>
46 
47 #include <dev/biovar.h>
48 #include <dev/sysmon/sysmonvar.h>
49 #include <sys/envsys.h>
50 
51 #define MPII_DOORBELL			(0x00)
52 /* doorbell read bits */
53 #define MPII_DOORBELL_STATE		(0xf<<28) /* ioc state */
54 #define  MPII_DOORBELL_STATE_RESET	(0x0<<28)
55 #define  MPII_DOORBELL_STATE_READY	(0x1<<28)
56 #define  MPII_DOORBELL_STATE_OPER	(0x2<<28)
57 #define  MPII_DOORBELL_STATE_FAULT	(0x4<<28)
58 #define  MPII_DOORBELL_INUSE		(0x1<<27) /* doorbell used */
59 #define MPII_DOORBELL_WHOINIT		(0x7<<24) /* last to reset ioc */
60 #define  MPII_DOORBELL_WHOINIT_NOONE	(0x0<<24) /* not initialized */
61 #define  MPII_DOORBELL_WHOINIT_SYSBIOS	(0x1<<24) /* system bios */
62 #define  MPII_DOORBELL_WHOINIT_ROMBIOS	(0x2<<24) /* rom bios */
63 #define  MPII_DOORBELL_WHOINIT_PCIPEER	(0x3<<24) /* pci peer */
64 #define  MPII_DOORBELL_WHOINIT_DRIVER	(0x4<<24) /* host driver */
65 #define  MPII_DOORBELL_WHOINIT_MANUFACT	(0x5<<24) /* manufacturing */
66 #define MPII_DOORBELL_FAULT		(0xffff<<0) /* fault code */
67 /* doorbell write bits */
68 #define MPII_DOORBELL_FUNCTION_SHIFT	(24)
69 #define MPII_DOORBELL_FUNCTION_MASK	(0xff << MPII_DOORBELL_FUNCTION_SHIFT)
70 #define MPII_DOORBELL_FUNCTION(x)	\
71     (((x) << MPII_DOORBELL_FUNCTION_SHIFT) & MPII_DOORBELL_FUNCTION_MASK)
72 #define MPII_DOORBELL_DWORDS_SHIFT	16
73 #define MPII_DOORBELL_DWORDS_MASK	(0xff << MPII_DOORBELL_DWORDS_SHIFT)
74 #define MPII_DOORBELL_DWORDS(x)		\
75     (((x) << MPII_DOORBELL_DWORDS_SHIFT) & MPII_DOORBELL_DWORDS_MASK)
76 #define MPII_DOORBELL_DATA_MASK		(0xffff)
77 
78 #define MPII_WRITESEQ			(0x04)
79 #define  MPII_WRITESEQ_KEY_VALUE_MASK	(0x0000000f) /* key value */
80 #define  MPII_WRITESEQ_FLUSH		(0x00)
81 #define  MPII_WRITESEQ_1		(0x0f)
82 #define  MPII_WRITESEQ_2		(0x04)
83 #define  MPII_WRITESEQ_3		(0x0b)
84 #define  MPII_WRITESEQ_4		(0x02)
85 #define  MPII_WRITESEQ_5		(0x07)
86 #define  MPII_WRITESEQ_6		(0x0d)
87 
88 #define MPII_HOSTDIAG			(0x08)
89 #define  MPII_HOSTDIAG_BDS_MASK		(0x00001800) /* boot device select */
90 #define   MPII_HOSTDIAG_BDS_DEFAULT 	(0<<11)	/* default address map, flash */
91 #define   MPII_HOSTDIAG_BDS_HCDW	(1<<11)	/* host code and data window */
92 #define  MPII_HOSTDIAG_CLEARFBS		(1<<10) /* clear flash bad sig */
93 #define  MPII_HOSTDIAG_FORCE_HCB_ONBOOT (1<<9)	/* force host controlled boot */
94 #define  MPII_HOSTDIAG_HCB_MODE		(1<<8)	/* host controlled boot mode */
95 #define  MPII_HOSTDIAG_DWRE		(1<<7) 	/* diag reg write enabled */
96 #define  MPII_HOSTDIAG_FBS		(1<<6) 	/* flash bad sig */
97 #define  MPII_HOSTDIAG_RESET_HIST	(1<<5) 	/* reset history */
98 #define  MPII_HOSTDIAG_DIAGWR_EN	(1<<4) 	/* diagnostic write enabled */
99 #define  MPII_HOSTDIAG_RESET_ADAPTER	(1<<2) 	/* reset adapter */
100 #define  MPII_HOSTDIAG_HOLD_IOC_RESET	(1<<1) 	/* hold ioc in reset */
101 #define  MPII_HOSTDIAG_DIAGMEM_EN	(1<<0) 	/* diag mem enable */
102 
103 #define MPII_DIAGRWDATA			(0x10)
104 
105 #define MPII_DIAGRWADDRLOW		(0x14)
106 
107 #define MPII_DIAGRWADDRHIGH		(0x18)
108 
109 #define MPII_INTR_STATUS		(0x30)
110 #define  MPII_INTR_STATUS_SYS2IOCDB	(1<<31) /* ioc written to by host */
111 #define  MPII_INTR_STATUS_RESET		(1<<30) /* physical ioc reset */
112 #define  MPII_INTR_STATUS_REPLY		(1<<3)	/* reply message interrupt */
113 #define  MPII_INTR_STATUS_IOC2SYSDB	(1<<0) 	/* ioc write to doorbell */
114 
115 #define MPII_INTR_MASK			(0x34)
116 #define  MPII_INTR_MASK_RESET		(1<<30) /* ioc reset intr mask */
117 #define  MPII_INTR_MASK_REPLY		(1<<3) 	/* reply message intr mask */
118 #define  MPII_INTR_MASK_DOORBELL	(1<<0) 	/* doorbell interrupt mask */
119 
120 #define MPII_DCR_DATA			(0x38)
121 
122 #define MPII_DCR_ADDRESS		(0x3c)
123 
124 #define MPII_REPLY_FREE_HOST_INDEX	(0x48)
125 
126 #define MPII_REPLY_POST_HOST_INDEX	(0x6c)
127 
128 #define MPII_HCB_SIZE			(0x74)
129 
130 #define MPII_HCB_ADDRESS_LOW		(0x78)
131 #define MPII_HCB_ADDRESS_HIGH		(0x7c)
132 
133 #define MPII_REQ_DESCR_POST_LOW		(0xc0)
134 #define MPII_REQ_DESCR_POST_HIGH	(0xc4)
135 
136 /*
137  * Scatter Gather Lists
138  */
139 
140 #define MPII_SGE_FL_LAST		(0x1<<31) /* last element in segment */
141 #define MPII_SGE_FL_EOB			(0x1<<30) /* last element of buffer */
142 #define MPII_SGE_FL_TYPE		(0x3<<28) /* element type */
143  #define MPII_SGE_FL_TYPE_SIMPLE	(0x1<<28) /* simple element */
144  #define MPII_SGE_FL_TYPE_CHAIN		(0x3<<28) /* chain element */
145  #define MPII_SGE_FL_TYPE_XACTCTX	(0x0<<28) /* transaction context */
146 #define MPII_SGE_FL_LOCAL		(0x1<<27) /* local address */
147 #define MPII_SGE_FL_DIR			(0x1<<26) /* direction */
148  #define MPII_SGE_FL_DIR_OUT		(0x1<<26)
149  #define MPII_SGE_FL_DIR_IN		(0x0<<26)
150 #define MPII_SGE_FL_SIZE		(0x1<<25) /* address size */
151  #define MPII_SGE_FL_SIZE_32		(0x0<<25)
152  #define MPII_SGE_FL_SIZE_64		(0x1<<25)
153 #define MPII_SGE_FL_EOL			(0x1<<24) /* end of list */
154 
155 struct mpii_sge {
156 	u_int32_t		sg_hdr;
157 	u_int32_t		sg_lo_addr;
158 	u_int32_t		sg_hi_addr;
159 } __packed;
160 
161 struct mpii_fw_tce {
162 	u_int8_t		reserved1;
163 	u_int8_t		context_size;
164 	u_int8_t		details_length;
165 	u_int8_t		flags;
166 
167 	u_int32_t		reserved2;
168 
169 	u_int32_t		image_offset;
170 
171 	u_int32_t		image_size;
172 } __packed;
173 
174 /*
175  * Messages
176  */
177 
178 /* functions */
179 #define MPII_FUNCTION_SCSI_IO_REQUEST			(0x00)
180 #define MPII_FUNCTION_SCSI_TASK_MGMT			(0x01)
181 #define MPII_FUNCTION_IOC_INIT				(0x02)
182 #define MPII_FUNCTION_IOC_FACTS				(0x03)
183 #define MPII_FUNCTION_CONFIG				(0x04)
184 #define MPII_FUNCTION_PORT_FACTS			(0x05)
185 #define MPII_FUNCTION_PORT_ENABLE			(0x06)
186 #define MPII_FUNCTION_EVENT_NOTIFICATION		(0x07)
187 #define MPII_FUNCTION_EVENT_ACK				(0x08)
188 #define MPII_FUNCTION_FW_DOWNLOAD			(0x09)
189 #define MPII_FUNCTION_TARGET_CMD_BUFFER_POST		(0x0a)
190 #define MPII_FUNCTION_TARGET_ASSIST			(0x0b)
191 #define MPII_FUNCTION_TARGET_STATUS_SEND		(0x0c)
192 #define MPII_FUNCTION_TARGET_MODE_ABORT			(0x0d)
193 #define MPII_FUNCTION_FW_UPLOAD				(0x12)
194 
195 #define MPII_FUNCTION_RAID_ACTION			(0x15)
196 #define MPII_FUNCTION_RAID_SCSI_IO_PASSTHROUGH		(0x16)
197 
198 #define MPII_FUNCTION_TOOLBOX				(0x17)
199 
200 #define MPII_FUNCTION_SCSI_ENCLOSURE_PROCESSOR		(0x18)
201 
202 #define MPII_FUNCTION_SMP_PASSTHROUGH			(0x1a)
203 #define MPII_FUNCTION_SAS_IO_UNIT_CONTROL		(0x1b)
204 #define MPII_FUNCTION_SATA_PASSTHROUGH			(0x1c)
205 
206 #define MPII_FUNCTION_DIAG_BUFFER_POST			(0x1d)
207 #define MPII_FUNCTION_DIAG_RELEASE			(0x1e)
208 
209 #define MPII_FUNCTION_TARGET_CMD_BUF_BASE_POST		(0x24)
210 #define MPII_FUNCTION_TARGET_CMD_BUF_LIST_POST		(0x25)
211 
212 #define MPII_FUNCTION_IOC_MESSAGE_UNIT_RESET		(0x40)
213 #define MPII_FUNCTION_IO_UNIT_RESET			(0x41)
214 #define MPII_FUNCTION_HANDSHAKE				(0x42)
215 
216 /* Common IOCStatus values for all replies */
217 #define MPII_IOCSTATUS_MASK				(0x7fff)
218 #define  MPII_IOCSTATUS_SUCCESS				(0x0000)
219 #define  MPII_IOCSTATUS_INVALID_FUNCTION		(0x0001)
220 #define  MPII_IOCSTATUS_BUSY				(0x0002)
221 #define  MPII_IOCSTATUS_INVALID_SGL			(0x0003)
222 #define  MPII_IOCSTATUS_INTERNAL_ERROR			(0x0004)
223 #define  MPII_IOCSTATUS_INVALID_VPID			(0x0005)
224 #define  MPII_IOCSTATUS_INSUFFICIENT_RESOURCES		(0x0006)
225 #define  MPII_IOCSTATUS_INVALID_FIELD			(0x0007)
226 #define  MPII_IOCSTATUS_INVALID_STATE			(0x0008)
227 #define  MPII_IOCSTATUS_OP_STATE_NOT_SUPPORTED		(0x0009)
228 /* Config IOCStatus values */
229 #define  MPII_IOCSTATUS_CONFIG_INVALID_ACTION		(0x0020)
230 #define  MPII_IOCSTATUS_CONFIG_INVALID_TYPE		(0x0021)
231 #define  MPII_IOCSTATUS_CONFIG_INVALID_PAGE		(0x0022)
232 #define  MPII_IOCSTATUS_CONFIG_INVALID_DATA		(0x0023)
233 #define  MPII_IOCSTATUS_CONFIG_NO_DEFAULTS		(0x0024)
234 #define  MPII_IOCSTATUS_CONFIG_CANT_COMMIT		(0x0025)
235 /* SCSIIO Reply initiator values */
236 #define  MPII_IOCSTATUS_SCSI_RECOVERED_ERROR		(0x0040)
237 #define  MPII_IOCSTATUS_SCSI_INVALID_DEVHANDLE		(0x0042)
238 #define  MPII_IOCSTATUS_SCSI_DEVICE_NOT_THERE		(0x0043)
239 #define  MPII_IOCSTATUS_SCSI_DATA_OVERRUN		(0x0044)
240 #define  MPII_IOCSTATUS_SCSI_DATA_UNDERRUN		(0x0045)
241 #define  MPII_IOCSTATUS_SCSI_IO_DATA_ERROR		(0x0046)
242 #define  MPII_IOCSTATUS_SCSI_PROTOCOL_ERROR		(0x0047)
243 #define  MPII_IOCSTATUS_SCSI_TASK_TERMINATED		(0x0048)
244 #define  MPII_IOCSTATUS_SCSI_RESIDUAL_MISMATCH		(0x0049)
245 #define  MPII_IOCSTATUS_SCSI_TASK_MGMT_FAILED		(0x004a)
246 #define  MPII_IOCSTATUS_SCSI_IOC_TERMINATED		(0x004b)
247 #define  MPII_IOCSTATUS_SCSI_EXT_TERMINATED		(0x004c)
248 /* For use by SCSI Initiator and SCSI Target end-to-end data protection */
249 #define  MPII_IOCSTATUS_EEDP_GUARD_ERROR		(0x004d)
250 #define  MPII_IOCSTATUS_EEDP_REF_TAG_ERROR		(0x004e)
251 #define  MPII_IOCSTATUS_EEDP_APP_TAG_ERROR		(0x004f)
252 /* SCSI (SPI & FCP) target values */
253 #define  MPII_IOCSTATUS_TARGET_INVALID_IO_INDEX		(0x0062)
254 #define  MPII_IOCSTATUS_TARGET_ABORTED			(0x0063)
255 #define  MPII_IOCSTATUS_TARGET_NO_CONN_RETRYABLE	(0x0064)
256 #define  MPII_IOCSTATUS_TARGET_NO_CONNECTION		(0x0065)
257 #define  MPII_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH	(0x006a)
258 #define  MPII_IOCSTATUS_TARGET_DATA_OFFSET_ERROR	(0x006d)
259 #define  MPII_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA	(0x006e)
260 #define  MPII_IOCSTATUS_TARGET_IU_TOO_SHORT		(0x006f)
261 #define  MPII_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT		(0x0070)
262 #define  MPII_IOCSTATUS_TARGET_NAK_RECEIVED		(0x0071)
263 /* Serial Attached SCSI values */
264 #define  MPII_IOCSTATUS_SAS_SMP_REQUEST_FAILED		(0x0090)
265 #define  MPII_IOCSTATUS_SAS_SMP_DATA_OVERRUN		(0x0091)
266 /* Diagnostic Tools values */
267 #define  MPII_IOCSTATUS_DIAGNOSTIC_RELEASED		(0x00a0)
268 
269 #define MPII_REP_IOCLOGINFO_TYPE			(0xf<<28)
270 #define MPII_REP_IOCLOGINFO_TYPE_NONE			(0x0<<28)
271 #define MPII_REP_IOCLOGINFO_TYPE_SCSI			(0x1<<28)
272 #define MPII_REP_IOCLOGINFO_TYPE_FC			(0x2<<28)
273 #define MPII_REP_IOCLOGINFO_TYPE_SAS			(0x3<<28)
274 #define MPII_REP_IOCLOGINFO_TYPE_ISCSI			(0x4<<28)
275 #define MPII_REP_IOCLOGINFO_DATA			(0x0fffffff)
276 
277 /* event notification types */
278 #define MPII_EVENT_NONE					(0x00)
279 #define MPII_EVENT_LOG_DATA				(0x01)
280 #define MPII_EVENT_STATE_CHANGE				(0x02)
281 #define MPII_EVENT_HARD_RESET_RECEIVED			(0x05)
282 #define MPII_EVENT_EVENT_CHANGE				(0x0a)
283 #define MPII_EVENT_TASK_SET_FULL			(0x0e)
284 #define MPII_EVENT_SAS_DEVICE_STATUS_CHANGE		(0x0f)
285 #define MPII_EVENT_IR_OPERATION_STATUS			(0x14)
286 #define MPII_EVENT_SAS_DISCOVERY			(0x16)
287 #define MPII_EVENT_SAS_BROADCAST_PRIMITIVE		(0x17)
288 #define MPII_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE	(0x18)
289 #define MPII_EVENT_SAS_INIT_TABLE_OVERFLOW		(0x19)
290 #define MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST		(0x1c)
291 #define MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE	(0x1d)
292 #define MPII_EVENT_IR_VOLUME				(0x1e)
293 #define MPII_EVENT_IR_PHYSICAL_DISK			(0x1f)
294 #define MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST		(0x20)
295 #define MPII_EVENT_LOG_ENTRY_ADDED			(0x21)
296 
297 /* messages */
298 
299 #define MPII_WHOINIT_NOONE				(0x00)
300 #define MPII_WHOINIT_SYSTEM_BIOS			(0x01)
301 #define MPII_WHOINIT_ROM_BIOS				(0x02)
302 #define MPII_WHOINIT_PCI_PEER				(0x03)
303 #define MPII_WHOINIT_HOST_DRIVER			(0x04)
304 #define MPII_WHOINIT_MANUFACTURER			(0x05)
305 
306 /* default messages */
307 
308 struct mpii_msg_request {
309 	u_int8_t		reserved1;
310 	u_int8_t		reserved2;
311 	u_int8_t		chain_offset;
312 	u_int8_t		function;
313 
314 	u_int8_t		reserved3;
315 	u_int8_t		reserved4;
316 	u_int8_t		reserved5;
317 	u_int8_t		msg_flags;
318 
319 	u_int8_t		vp_id;
320 	u_int8_t		vf_id;
321 	u_int16_t		reserved6;
322 } __packed;
323 
324 struct mpii_msg_reply {
325 	u_int16_t		reserved1;
326 	u_int8_t		msg_length;
327 	u_int8_t		function;
328 
329 	u_int16_t		reserved2;
330 	u_int8_t		reserved3;
331 	u_int8_t		msg_flags;
332 
333 	u_int8_t		vp_id;
334 	u_int8_t		vf_if;
335 	u_int16_t		reserved4;
336 
337 	u_int16_t		reserved5;
338 	u_int16_t		ioc_status;
339 
340 	u_int32_t		ioc_loginfo;
341 } __packed;
342 
343 /* ioc init */
344 
345 struct mpii_msg_iocinit_request {
346 	u_int8_t		whoinit;
347 	u_int8_t		reserved1;
348 	u_int8_t		chain_offset;
349 	u_int8_t		function;
350 
351 	u_int16_t		reserved2;
352 	u_int8_t		reserved3;
353 	u_int8_t		msg_flags;
354 
355 	u_int8_t		vp_id;
356 	u_int8_t		vf_id;
357 	u_int16_t		reserved4;
358 
359 	u_int8_t		msg_version_min;
360 	u_int8_t		msg_version_maj;
361 	u_int8_t		hdr_version_unit;
362 	u_int8_t		hdr_version_dev;
363 
364 	u_int32_t		reserved5;
365 
366 	u_int32_t		reserved6;
367 
368 	u_int16_t		reserved7;
369 	u_int16_t		system_request_frame_size;
370 
371 	u_int16_t		reply_descriptor_post_queue_depth;
372 	u_int16_t		reply_free_queue_depth;
373 
374 	u_int32_t		sense_buffer_address_high;
375 
376 	u_int32_t		system_reply_address_high;
377 
378 	u_int64_t		system_request_frame_base_address;
379 
380 	u_int64_t		reply_descriptor_post_queue_address;
381 
382 	u_int64_t		reply_free_queue_address;
383 
384 	u_int64_t		timestamp;
385 } __packed;
386 
387 struct mpii_msg_iocinit_reply {
388 	u_int8_t		whoinit;
389 	u_int8_t		reserved1;
390 	u_int8_t		msg_length;
391 	u_int8_t		function;
392 
393 	u_int16_t		reserved2;
394 	u_int8_t		reserved3;
395 	u_int8_t		msg_flags;
396 
397 	u_int8_t		vp_id;
398 	u_int8_t		vf_id;
399 	u_int16_t		reserved4;
400 
401 	u_int16_t		reserved5;
402 	u_int16_t		ioc_status;
403 
404 	u_int32_t		ioc_loginfo;
405 } __packed;
406 
407 struct mpii_msg_iocfacts_request {
408 	u_int16_t		reserved1;
409 	u_int8_t		chain_offset;
410 	u_int8_t		function;
411 
412 	u_int16_t		reserved2;
413 	u_int8_t		reserved3;
414 	u_int8_t		msg_flags;
415 
416 	u_int8_t		vp_id;
417 	u_int8_t		vf_id;
418 	u_int16_t		reserved4;
419 } __packed;
420 
421 struct mpii_msg_iocfacts_reply {
422 	u_int8_t		msg_version_min;
423 	u_int8_t		msg_version_maj;
424 	u_int8_t		msg_length;
425 	u_int8_t		function;
426 
427 	u_int8_t		header_version_dev;
428 	u_int8_t		header_version_unit;
429 	u_int8_t		ioc_number;
430 	u_int8_t		msg_flags;
431 
432 	u_int8_t		vp_id;
433 	u_int8_t		vf_id;
434 	u_int16_t		reserved1;
435 
436 	u_int16_t		ioc_exceptions;
437 #define MPII_IOCFACTS_EXCEPT_CONFIG_CHECKSUM_FAIL	(1<<0)
438 #define MPII_IOCFACTS_EXCEPT_RAID_CONFIG_INVALID	(1<<1)
439 #define MPII_IOCFACTS_EXCEPT_FW_CHECKSUM_FAIL		(1<<2)
440 #define MPII_IOCFACTS_EXCEPT_MANUFACT_CHECKSUM_FAIL	(1<<3)
441 #define MPII_IOCFACTS_EXCEPT_METADATA_UNSUPPORTED	(1<<4)
442 #define MPII_IOCFACTS_EXCEPT_IR_FOREIGN_CONFIG_MAC	(1<<8)
443 	/* XXX JPG BOOT_STATUS in bits[7:5] */
444 	/* XXX JPG all these #defines need to be fixed up */
445 	u_int16_t		ioc_status;
446 
447 	u_int32_t		ioc_loginfo;
448 
449 	u_int8_t		max_chain_depth;
450 	u_int8_t		whoinit;
451 	u_int8_t		number_of_ports;
452 	u_int8_t		reserved2;
453 
454 	u_int16_t		request_credit;
455 	u_int16_t		product_id;
456 
457 	u_int32_t		ioc_capabilities;
458 #define MPII_IOCFACTS_CAPABILITY_EVENT_REPLAY           (1<<13)
459 #define MPII_IOCFACTS_CAPABILITY_INTEGRATED_RAID        (1<<12)
460 #define MPII_IOCFACTS_CAPABILITY_TLR                    (1<<11)
461 #define MPII_IOCFACTS_CAPABILITY_MULTICAST              (1<<8)
462 #define MPII_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET   (1<<7)
463 #define MPII_IOCFACTS_CAPABILITY_EEDP                   (1<<6)
464 #define MPII_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER        (1<<4)
465 #define MPII_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER      (1<<3)
466 #define MPII_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING (1<<2)
467 
468 	u_int8_t		fw_version_dev;
469 	u_int8_t		fw_version_unit;
470 	u_int8_t		fw_version_min;
471 	u_int8_t		fw_version_maj;
472 
473 	u_int16_t		ioc_request_frame_size;
474 	u_int16_t		reserved3;
475 
476 	u_int16_t		max_initiators;
477 	u_int16_t		max_targets;
478 
479 	u_int16_t		max_sas_expanders;
480 	u_int16_t		max_enclosures;
481 
482 	u_int16_t		protocol_flags;
483 	u_int16_t		high_priority_credit;
484 
485 	u_int16_t		max_reply_descriptor_post_queue_depth;
486 	u_int8_t		reply_frame_size;
487 	u_int8_t		max_volumes;
488 
489 	u_int16_t		max_dev_handle;
490 	u_int16_t		max_persistent_entries;
491 
492 	u_int32_t		reserved4;
493 } __packed;
494 
495 struct mpii_msg_portfacts_request {
496 	u_int16_t		reserved1;
497 	u_int8_t		chain_offset;
498 	u_int8_t		function;
499 
500 	u_int16_t		reserved2;
501 	u_int8_t		port_number;
502 	u_int8_t		msg_flags;
503 
504 	u_int8_t		vp_id;
505 	u_int8_t		vf_id;
506 	u_int16_t		reserved3;
507 } __packed;
508 
509 struct mpii_msg_portfacts_reply {
510 	u_int16_t		reserved1;
511 	u_int8_t		msg_length;
512 	u_int8_t		function;
513 
514 	u_int16_t		reserved2;
515 	u_int8_t		port_number;
516 	u_int8_t		msg_flags;
517 
518 	u_int8_t		vp_id;
519 	u_int8_t		vf_id;
520 	u_int16_t		reserved3;
521 
522 	u_int16_t		reserved4;
523 	u_int16_t		ioc_status;
524 
525 	u_int32_t		ioc_loginfo;
526 
527 	u_int8_t		reserved5;
528 	u_int8_t		port_type;
529 #define MPII_PORTFACTS_PORTTYPE_INACTIVE		(0x00)
530 #define MPII_PORTFACTS_PORTTYPE_FC			(0x10)
531 #define MPII_PORTFACTS_PORTTYPE_ISCSI			(0x20)
532 #define MPII_PORTFACTS_PORTTYPE_SAS_PHYSICAL		(0x30)
533 #define MPII_PORTFACTS_PORTTYPE_SAS_VIRTUAL		(0x31)
534 	u_int16_t		reserved6;
535 
536 	u_int16_t		max_posted_cmd_buffers;
537 	u_int16_t		reserved7;
538 } __packed;
539 
540 struct mpii_msg_portenable_request {
541 	u_int16_t		reserved1;
542 	u_int8_t		chain_offset;
543 	u_int8_t		function;
544 
545 	u_int8_t		reserved2;
546 	u_int8_t		port_flags;
547 	u_int8_t		reserved3;
548 	u_int8_t		msg_flags;
549 
550 	u_int8_t		vp_id;
551 	u_int8_t		vf_id;
552 	u_int16_t		reserved4;
553 } __packed;
554 
555 struct mpii_msg_portenable_reply {
556 	u_int16_t		reserved1;
557 	u_int8_t		msg_length;
558 	u_int8_t		function;
559 
560 	u_int8_t		reserved2;
561 	u_int8_t		port_flags;
562 	u_int8_t		reserved3;
563 	u_int8_t		msg_flags;
564 
565 	u_int8_t		vp_id;
566 	u_int8_t		vf_id;
567 	u_int16_t		reserved4;
568 
569 	u_int16_t		reserved5;
570 	u_int16_t		ioc_status;
571 
572 	u_int32_t		ioc_loginfo;
573 } __packed;
574 
575 struct mpii_msg_event_request {
576 	u_int16_t		reserved1;
577 	u_int8_t		chain_offset;
578 	u_int8_t		function;
579 
580 	u_int16_t		reserved2;
581 	u_int8_t		reserved3;
582 	u_int8_t		msg_flags;
583 
584 	u_int8_t		vp_id;
585 	u_int8_t		vf_id;
586 	u_int16_t		reserved4;
587 
588 	u_int32_t		reserved5;
589 
590 	u_int32_t		reserved6;
591 
592 	u_int32_t		event_masks[4];
593 
594 	u_int16_t		sas_broadcase_primitive_masks;
595 	u_int16_t		reserved7;
596 
597 	u_int32_t		reserved8;
598 } __packed;
599 
600 struct mpii_msg_event_reply {
601 	u_int16_t		event_data_length;
602 	u_int8_t		msg_length;
603 	u_int8_t		function;
604 
605 	u_int16_t		reserved1;
606 	u_int8_t		ack_required;
607 #define MPII_EVENT_ACK_REQUIRED				(0x01)
608 	u_int8_t		msg_flags;
609 #define MPII_EVENT_FLAGS_REPLY_KEPT			(1<<7)
610 
611 	u_int8_t		vp_id;
612 	u_int8_t		vf_id;
613 	u_int16_t		reserved2;
614 
615 	u_int16_t		reserved3;
616 	u_int16_t		ioc_status;
617 
618 	u_int32_t		ioc_loginfo;
619 
620 	u_int16_t		event;
621 	u_int16_t		reserved4;
622 
623 	u_int32_t		event_context;
624 
625 	/* event data follows */
626 } __packed;
627 
628 struct mpii_msg_eventack_request {
629 	u_int16_t		reserved1;
630 	u_int8_t		chain_offset;
631 	u_int8_t		function;
632 
633 	u_int8_t		reserved2[3];
634 	u_int8_t		msg_flags;
635 
636 	u_int8_t		vp_id;
637 	u_int8_t		vf_id;
638 	u_int16_t		reserved3;
639 
640 	u_int16_t		event;
641 	u_int16_t		reserved4;
642 
643 	u_int32_t		event_context;
644 } __packed;
645 
646 struct mpii_msg_eventack_reply {
647 	u_int16_t		reserved1;
648 	u_int8_t		msg_length;
649 	u_int8_t		function;
650 
651 	u_int8_t		reserved2[3];
652 	u_int8_t		msg_flags;
653 
654 	u_int8_t		vp_id;
655 	u_int8_t		vf_id;
656 	u_int16_t		reserved3;
657 
658 	u_int16_t		reserved4;
659 	u_int16_t		ioc_status;
660 
661 	u_int32_t		ioc_loginfo;
662 } __packed;
663 
664 struct mpii_msg_fwupload_request {
665 	u_int8_t		image_type;
666 #define MPII_FWUPLOAD_IMAGETYPE_IOC_FW			(0x00)
667 #define MPII_FWUPLOAD_IMAGETYPE_NV_FW			(0x01)
668 #define MPII_FWUPLOAD_IMAGETYPE_NV_BACKUP		(0x05)
669 #define MPII_FWUPLOAD_IMAGETYPE_NV_MANUFACTURING	(0x06)
670 #define MPII_FWUPLOAD_IMAGETYPE_NV_CONFIG_1		(0x07)
671 #define MPII_FWUPLOAD_IMAGETYPE_NV_CONFIG_2		(0x08)
672 #define MPII_FWUPLOAD_IMAGETYPE_NV_MEGARAID		(0x09)
673 #define MPII_FWUPLOAD_IMAGETYPE_NV_COMPLETE		(0x0a)
674 #define MPII_FWUPLOAD_IMAGETYPE_COMMON_BOOT_BLOCK	(0x0b)
675 	u_int8_t		reserved1;
676 	u_int8_t		chain_offset;
677 	u_int8_t		function;
678 
679 	u_int8_t		reserved2[3];
680 	u_int8_t		msg_flags;
681 
682 	u_int8_t		vp_id;
683 	u_int8_t		vf_id;
684 	u_int16_t		reserved3;
685 
686 	u_int32_t		reserved4;
687 
688 	u_int32_t		reserved5;
689 
690 	struct mpii_fw_tce	tce;
691 
692 	/* followed by an sgl */
693 } __packed;
694 
695 struct mpii_msg_fwupload_reply {
696 	u_int8_t		image_type;
697 	u_int8_t		reserved1;
698 	u_int8_t		msg_length;
699 	u_int8_t		function;
700 
701 	u_int8_t		reserved2[3];
702 	u_int8_t		msg_flags;
703 
704 	u_int8_t		vp_id;
705 	u_int8_t		vf_id;
706 	u_int16_t		reserved3;
707 
708 	u_int16_t		reserved4;
709 	u_int16_t		ioc_status;
710 
711 	u_int32_t		ioc_loginfo;
712 
713 	u_int32_t		actual_image_size;
714 } __packed;
715 
716 struct mpii_msg_scsi_io {
717 	u_int16_t		dev_handle;
718 	u_int8_t		chain_offset;
719 	u_int8_t		function;
720 
721 	u_int16_t		reserved1;
722 	u_int8_t		reserved2;
723 	u_int8_t		msg_flags;
724 
725 	u_int8_t		vp_id;
726 	u_int8_t		vf_id;
727 	u_int16_t		reserved3;
728 
729 	u_int32_t		sense_buffer_low_address;
730 
731 	u_int16_t		sgl_flags;
732 	u_int8_t		sense_buffer_length;
733 	u_int8_t		reserved4;
734 
735 	u_int8_t		sgl_offset0;
736 	u_int8_t		sgl_offset1;
737 	u_int8_t		sgl_offset2;
738 	u_int8_t		sgl_offset3;
739 
740 	u_int32_t		skip_count;
741 
742 	u_int32_t		data_length;
743 
744 	u_int32_t		bidirectional_data_length;
745 
746 	u_int16_t		io_flags;
747 	u_int16_t		eedp_flags;
748 
749 	u_int32_t		eedp_block_size;
750 
751 	u_int32_t		secondary_reference_tag;
752 
753 	u_int16_t		secondary_application_tag;
754 	u_int16_t		application_tag_translation_mask;
755 
756 	u_int16_t		lun[4];
757 
758 /* the following 16 bits are defined in MPI2 as the control field */
759 	u_int8_t		reserved5;
760 	u_int8_t		tagging;
761 #define MPII_SCSIIO_ATTR_SIMPLE_Q			(0x0)
762 #define MPII_SCSIIO_ATTR_HEAD_OF_Q			(0x1)
763 #define MPII_SCSIIO_ATTR_ORDERED_Q			(0x2)
764 #define MPII_SCSIIO_ATTR_ACA_Q				(0x4)
765 #define MPII_SCSIIO_ATTR_UNTAGGED			(0x5)
766 #define MPII_SCSIIO_ATTR_NO_DISCONNECT			(0x7)
767 	u_int8_t		reserved6;
768 	u_int8_t		direction;
769 #define MPII_SCSIIO_DIR_NONE				(0x0)
770 #define MPII_SCSIIO_DIR_WRITE				(0x1)
771 #define MPII_SCSIIO_DIR_READ				(0x2)
772 
773 #define	MPII_CDB_LEN					(32)
774 	u_int8_t		cdb[MPII_CDB_LEN];
775 
776 	/* followed by an sgl */
777 } __packed;
778 
779 struct mpii_msg_scsi_io_error {
780 	u_int16_t		dev_handle;
781 	u_int8_t		msg_length;
782 	u_int8_t		function;
783 
784 	u_int16_t		reserved1;
785 	u_int8_t		reserved2;
786 	u_int8_t		msg_flags;
787 
788 	u_int8_t		vp_id;
789 	u_int8_t		vf_id;
790 	u_int16_t		reserved3;
791 
792 	u_int8_t		scsi_status;
793 
794 #define MPII_SCSIIO_ERR_STATUS_SUCCESS			(0x00)
795 #define MPII_SCSIIO_ERR_STATUS_CHECK_COND		(0x02)
796 #define MPII_SCSIIO_ERR_STATUS_BUSY			(0x04)
797 #define MPII_SCSIIO_ERR_STATUS_INTERMEDIATE		(0x08)
798 #define MPII_SCSIIO_ERR_STATUS_INTERMEDIATE_CONDMET	(0x10)
799 #define MPII_SCSIIO_ERR_STATUS_RESERVATION_CONFLICT	(0x14)
800 #define MPII_SCSIIO_ERR_STATUS_CMD_TERM			(0x22)
801 #define MPII_SCSIIO_ERR_STATUS_TASK_SET_FULL		(0x28)
802 #define MPII_SCSIIO_ERR_STATUS_ACA_ACTIVE		(0x30)
803 #define MPII_SCSIIO_ERR_STATUS_TASK_ABORTED		(0x40)
804 
805 	u_int8_t		scsi_state;
806 #define MPII_SCSIIO_ERR_STATE_AUTOSENSE_VALID		(1<<0)
807 #define MPII_SCSIIO_ERR_STATE_AUTOSENSE_FAILED		(1<<1)
808 #define MPII_SCSIIO_ERR_STATE_NO_SCSI_STATUS		(1<<2)
809 #define MPII_SCSIIO_ERR_STATE_TERMINATED		(1<<3)
810 #define MPII_SCSIIO_ERR_STATE_RESPONSE_INFO_VALID	(1<<4)
811 #define MPII_SCSIIO_ERR_STATE_QUEUE_TAG_REJECTED	(0xffff)
812 	u_int16_t		ioc_status;
813 
814 	u_int32_t		ioc_loginfo;
815 
816 	u_int32_t		transfer_count;
817 
818 	u_int32_t		sense_count;
819 
820 	u_int32_t		response_info;
821 
822 	u_int16_t		task_tag;
823 	u_int16_t		reserved4;
824 
825 	u_int32_t		bidirectional_transfer_count;
826 
827 	u_int32_t		reserved5;
828 
829 	u_int32_t		reserved6;
830 } __packed;
831 
832 struct mpii_request_descr {
833 	u_int8_t		request_flags;
834 #define MPII_REQ_DESCR_TYPE_MASK			(0x0e)
835 #define MPII_REQ_DESCR_SCSI_IO				(0x00)
836 #define MPII_REQ_DESCR_SCSI_TARGET			(0x02)
837 #define MPII_REQ_DESCR_HIGH_PRIORITY			(0x06)
838 #define MPII_REQ_DESCR_DEFAULT				(0x08)
839 	u_int8_t		vf_id;
840 	u_int16_t		smid;
841 
842 	u_int16_t		lmid;
843 	u_int16_t		dev_handle;
844 } __packed;
845 
846 struct mpii_reply_descr {
847 	u_int8_t		reply_flags;
848 #define MPII_REPLY_DESCR_TYPE_MASK               	(0x0f)
849 #define MPII_REPLY_DESCR_SCSI_IO_SUCCESS         	(0x00)
850 #define MPII_REPLY_DESCR_ADDRESS_REPLY           	(0x01)
851 #define MPII_REPLY_DESCR_TARGET_ASSIST_SUCCESS    	(0x02)
852 #define MPII_REPLY_DESCR_TARGET_COMMAND_BUFFER   	(0x03)
853 #define MPII_REPLY_DESCR_UNUSED                  	(0x0f)
854 	u_int8_t		vf_id;
855 	u_int16_t		smid;
856 
857 	union {
858 		u_int32_t	data;
859 		u_int32_t	frame_addr;	/* Address Reply */
860 	};
861 } __packed;
862 
863 struct mpii_request_header {
864 	u_int16_t		function_dependent1;
865 	u_int8_t		chain_offset;
866 	u_int8_t		function;
867 
868 	u_int16_t		function_dependent2;
869 	u_int8_t		function_dependent3;
870 	u_int8_t		message_flags;
871 
872 	u_int8_t		vp_id;
873 	u_int8_t		vf_id;
874 	u_int16_t		reserved;
875 } __packed;
876 
877 struct mpii_msg_scsi_task_request {
878 	u_int16_t		dev_handle;
879 	u_int8_t		chain_offset;
880 	u_int8_t		function;
881 
882 	u_int8_t		reserved1;
883 	u_int8_t		task_type;
884 #define MPII_SCSI_TASK_ABORT_TASK			(0x01)
885 #define MPII_SCSI_TASK_ABRT_TASK_SET			(0x02)
886 #define MPII_SCSI_TASK_TARGET_RESET			(0x03)
887 #define MPII_SCSI_TASK_RESET_BUS			(0x04)
888 #define MPII_SCSI_TASK_LOGICAL_UNIT_RESET		(0x05)
889 	u_int8_t		reserved2;
890 	u_int8_t		msg_flags;
891 
892 	u_int8_t		vp_id;
893 	u_int8_t		vf_id;
894 	u_int16_t		reserved3;
895 
896 	u_int16_t		lun[4];
897 
898 	u_int32_t		reserved4[7];
899 
900 	u_int16_t		task_mid;
901 	u_int16_t		reserved5;
902 } __packed;
903 
904 struct mpii_msg_scsi_task_reply {
905 	u_int16_t		dev_handle;
906 	u_int8_t		msg_length;
907 	u_int8_t		function;
908 
909 	u_int8_t		response_code;
910 	u_int8_t		task_type;
911 	u_int8_t		reserved1;
912 	u_int8_t		msg_flags;
913 
914 	u_int8_t		vp_id;
915 	u_int8_t		vf_id;
916 	u_int16_t		reserved2;
917 
918 	u_int16_t		reserved3;
919 	u_int16_t		ioc_status;
920 
921 	u_int32_t		ioc_loginfo;
922 
923 	u_int32_t		termination_count;
924 } __packed;
925 
926 struct mpii_msg_sas_oper_request {
927 	u_int8_t		operation;
928 #define MPII_SAS_OP_CLEAR_PERSISTENT		(0x02)
929 #define MPII_SAS_OP_PHY_LINK_RESET		(0x06)
930 #define MPII_SAS_OP_PHY_HARD_RESET		(0x07)
931 #define MPII_SAS_OP_PHY_CLEAR_ERROR_LOG		(0x08)
932 #define MPII_SAS_OP_SEND_PRIMITIVE		(0x0a)
933 #define MPII_SAS_OP_FORCE_FULL_DISCOVERY	(0x0b)
934 #define MPII_SAS_OP_TRANSMIT_PORT_SELECT	(0x0c)
935 #define MPII_SAS_OP_REMOVE_DEVICE		(0x0d)
936 #define MPII_SAS_OP_LOOKUP_MAPPING		(0x0e)
937 #define MPII_SAS_OP_SET_IOC_PARAM		(0x0f)
938 	u_int8_t		reserved1;
939 	u_int8_t		chain_offset;
940 	u_int8_t		function;
941 
942 	u_int16_t		dev_handle;
943 	u_int8_t		ioc_param;
944 	u_int8_t		msg_flags;
945 
946 	u_int8_t		vp_id;
947 	u_int8_t		vf_id;
948 	u_int16_t		reserved2;
949 
950 	u_int16_t		reserved3;
951 	u_int8_t		phy_num;
952 	u_int8_t		prim_flags;
953 
954 	u_int32_t		primitive;
955 
956 	u_int8_t		lookup_method;
957 #define MPII_SAS_LOOKUP_METHOD_SAS_ADDR		(0x01)
958 #define MPII_SAS_LOOKUP_METHOD_SAS_ENCL		(0x02)
959 #define MPII_SAS_LOOKUP_METHOD_SAS_DEVNAME	(0x03)
960 	u_int8_t		reserved4;
961 	u_int16_t		slot_num;
962 
963 	u_int64_t		lookup_addr;
964 
965 	u_int32_t		ioc_param_value;
966 
967 	u_int64_t		reserved5;
968 } __packed;
969 
970 struct mpii_msg_sas_oper_reply {
971 	u_int8_t		operation;
972 	u_int8_t		reserved1;
973 	u_int8_t		chain_offset;
974 	u_int8_t		function;
975 
976 	u_int16_t		dev_handle;
977 	u_int8_t		ioc_param;
978 	u_int8_t		msg_flags;
979 
980 	u_int8_t		vp_id;
981 	u_int8_t		vf_id;
982 	u_int16_t		reserved2;
983 
984 	u_int16_t		reserved3;
985 	u_int16_t		ioc_status;
986 
987 	u_int32_t		ioc_loginfo;
988 } __packed;
989 
990 struct mpii_msg_raid_action_request {
991 	u_int8_t	action;
992 #define MPII_RAID_ACTION_CHANGE_VOL_WRITE_CACHE	(0x17)
993 	u_int8_t	reserved1;
994 	u_int8_t	chain_offset;
995 	u_int8_t	function;
996 
997 	u_int16_t	vol_dev_handle;
998 	u_int8_t	phys_disk_num;
999 	u_int8_t	msg_flags;
1000 
1001 	u_int8_t	vp_id;
1002 	u_int8_t	vf_if;
1003 	u_int16_t	reserved2;
1004 
1005 	u_int32_t	reserved3;
1006 
1007 	u_int32_t	action_data;
1008 #define MPII_RAID_VOL_WRITE_CACHE_MASK			(0x03)
1009 #define MPII_RAID_VOL_WRITE_CACHE_DISABLE		(0x01)
1010 #define MPII_RAID_VOL_WRITE_CACHE_ENABLE		(0x02)
1011 
1012 	struct mpii_sge	action_sge;
1013 } __packed;
1014 
1015 struct mpii_msg_raid_action_reply {
1016 	u_int8_t	action;
1017 	u_int8_t	reserved1;
1018 	u_int8_t	chain_offset;
1019 	u_int8_t	function;
1020 
1021 	u_int16_t	vol_dev_handle;
1022 	u_int8_t	phys_disk_num;
1023 	u_int8_t	msg_flags;
1024 
1025 	u_int8_t	vp_id;
1026 	u_int8_t	vf_if;
1027 	u_int16_t	reserved2;
1028 
1029 	u_int16_t	reserved3;
1030 	u_int16_t	ioc_status;
1031 
1032 	u_int32_t	action_data[5];
1033 } __packed;
1034 
1035 struct mpii_cfg_hdr {
1036 	u_int8_t		page_version;
1037 	u_int8_t		page_length;
1038 	u_int8_t		page_number;
1039 	u_int8_t		page_type;
1040 #define MPII_CONFIG_REQ_PAGE_TYPE_ATTRIBUTE		(0xf0)
1041 #define MPI2_CONFIG_PAGEATTR_READ_ONLY              	(0x00)
1042 #define MPI2_CONFIG_PAGEATTR_CHANGEABLE             	(0x10)
1043 #define MPI2_CONFIG_PAGEATTR_PERSISTENT             	(0x20)
1044 
1045 #define MPII_CONFIG_REQ_PAGE_TYPE_MASK			(0x0f)
1046 #define MPII_CONFIG_REQ_PAGE_TYPE_IO_UNIT		(0x00)
1047 #define MPII_CONFIG_REQ_PAGE_TYPE_IOC			(0x01)
1048 #define MPII_CONFIG_REQ_PAGE_TYPE_BIOS			(0x02)
1049 #define MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL		(0x08)
1050 #define MPII_CONFIG_REQ_PAGE_TYPE_MANUFACTURING		(0x09)
1051 #define MPII_CONFIG_REQ_PAGE_TYPE_RAID_PD		(0x0a)
1052 #define MPII_CONFIG_REQ_PAGE_TYPE_EXTENDED		(0x0f)
1053 } __packed;
1054 
1055 struct mpii_ecfg_hdr {
1056 	u_int8_t		page_version;
1057 	u_int8_t		reserved1;
1058 	u_int8_t		page_number;
1059 	u_int8_t		page_type;
1060 
1061 	u_int16_t		ext_page_length;
1062 	u_int8_t		ext_page_type;
1063 #define MPII_CONFIG_REQ_PAGE_TYPE_SAS_DEVICE		(0x12)
1064 #define MPII_CONFIG_REQ_PAGE_TYPE_RAID_CONFIG		(0x16)
1065 #define MPII_CONFIG_REQ_PAGE_TYPE_DRIVER_MAPPING	(0x17)
1066 	u_int8_t		reserved2;
1067 } __packed;
1068 
1069 struct mpii_msg_config_request {
1070 	u_int8_t		action;
1071 #define MPII_CONFIG_REQ_ACTION_PAGE_HEADER		(0x00)
1072 #define MPII_CONFIG_REQ_ACTION_PAGE_READ_CURRENT	(0x01)
1073 #define MPII_CONFIG_REQ_ACTION_PAGE_WRITE_CURRENT	(0x02)
1074 #define MPII_CONFIG_REQ_ACTION_PAGE_DEFAULT		(0x03)
1075 #define MPII_CONFIG_REQ_ACTION_PAGE_WRITE_NVRAM		(0x04)
1076 #define MPII_CONFIG_REQ_ACTION_PAGE_READ_DEFAULT	(0x05)
1077 #define MPII_CONFIG_REQ_ACTION_PAGE_READ_NVRAM		(0x06)
1078 	u_int8_t		sgl_flags;
1079 	u_int8_t		chain_offset;
1080 	u_int8_t		function;
1081 
1082 	u_int16_t		ext_page_len;
1083 	u_int8_t		ext_page_type;
1084 #define MPII_CONFIG_REQ_EXTPAGE_TYPE_SAS_IO_UNIT	(0x10)
1085 #define MPII_CONFIG_REQ_EXTPAGE_TYPE_SAS_EXPANDER	(0x11)
1086 #define MPII_CONFIG_REQ_EXTPAGE_TYPE_SAS_DEVICE		(0x12)
1087 #define MPII_CONFIG_REQ_EXTPAGE_TYPE_SAS_PHY		(0x13)
1088 #define MPII_CONFIG_REQ_EXTPAGE_TYPE_LOG		(0x14)
1089 #define MPI2_CONFIG_EXTPAGETYPE_ENCLOSURE            	(0x15)
1090 #define MPI2_CONFIG_EXTPAGETYPE_RAID_CONFIG         	(0x16)
1091 #define MPI2_CONFIG_EXTPAGETYPE_DRIVER_MAPPING      	(0x17)
1092 #define MPI2_CONFIG_EXTPAGETYPE_SAS_PORT            	(0x18)
1093 	u_int8_t		msg_flags;
1094 
1095 	u_int8_t		vp_id;
1096 	u_int8_t		vf_id;
1097 	u_int16_t		reserved1;
1098 
1099 	u_int32_t		reserved2[2];
1100 
1101 	struct mpii_cfg_hdr	config_header;
1102 
1103 	u_int32_t		page_address;
1104 /* XXX lots of defns here */
1105 
1106 	struct mpii_sge		page_buffer;
1107 } __packed;
1108 
1109 struct mpii_msg_config_reply {
1110 	u_int8_t		action;
1111 	u_int8_t		sgl_flags;
1112 	u_int8_t		msg_length;
1113 	u_int8_t		function;
1114 
1115 	u_int16_t		ext_page_length;
1116 	u_int8_t		ext_page_type;
1117 	u_int8_t		msg_flags;
1118 
1119 	u_int8_t		vp_id;
1120 	u_int8_t		vf_id;
1121 	u_int16_t		reserved1;
1122 
1123 	u_int16_t		reserved2;
1124 	u_int16_t		ioc_status;
1125 
1126 	u_int32_t		ioc_loginfo;
1127 
1128 	struct mpii_cfg_hdr	config_header;
1129 } __packed;
1130 
1131 struct mpii_cfg_manufacturing_pg0 {
1132 	struct mpii_cfg_hdr	config_header;
1133 
1134 	char			chip_name[16];
1135 	char			chip_revision[8];
1136 	char			board_name[16];
1137 	char			board_assembly[16];
1138 	char			board_tracer_number[16];
1139 } __packed;
1140 
1141 struct mpii_cfg_ioc_pg1 {
1142 	struct mpii_cfg_hdr     config_header;
1143 
1144 	u_int32_t       flags;
1145 
1146 	u_int32_t       coalescing_timeout;
1147 #define	MPII_CFG_IOC_1_REPLY_COALESCING			(1<<0)
1148 
1149 	u_int8_t        coalescing_depth;
1150 	u_int8_t        pci_slot_num;
1151 	u_int8_t        pci_bus_num;
1152 	u_int8_t        pci_domain_segment;
1153 
1154 	u_int32_t       reserved1;
1155 
1156 	u_int32_t       reserved2;
1157 } __packed;
1158 
1159 struct mpii_cfg_ioc_pg3 {
1160 	struct mpii_cfg_hdr	config_header;
1161 
1162 	u_int8_t		no_phys_disks;
1163 	u_int8_t		reserved[3];
1164 
1165 	/* followed by a list of mpii_cfg_raid_physdisk structs */
1166 } __packed;
1167 
1168 struct mpii_cfg_ioc_pg8 {
1169 	struct mpii_cfg_hdr	config_header;
1170 
1171 	u_int8_t		num_devs_per_enclosure;
1172 	u_int8_t		reserved1;
1173 	u_int16_t		reserved2;
1174 
1175 	u_int16_t		max_persistent_entries;
1176 	u_int16_t		max_num_physical_mapped_ids;
1177 
1178 	u_int16_t		flags;
1179 #define	MPII_IOC_PG8_FLAGS_DA_START_SLOT_1		(1<<5)
1180 #define MPII_IOC_PG8_FLAGS_RESERVED_TARGETID_0		(1<<4)
1181 #define MPII_IOC_PG8_FLAGS_MAPPING_MODE_MASK		(0x0000000e)
1182 #define MPII_IOC_PG8_FLAGS_DEVICE_PERSISTENCE_MAPPING	(0<<1)
1183 #define MPII_IOC_PG8_FLAGS_ENCLOSURE_SLOT_MAPPING	(1<<1)
1184 #define MPII_IOC_PG8_FLAGS_DISABLE_PERSISTENT_MAPPING	(1<<0)
1185 #define	MPII_IOC_PG8_FLAGS_ENABLE_PERSISTENT_MAPPING	(0<<0)
1186 	u_int16_t		reserved3;
1187 
1188 	u_int16_t		ir_volume_mapping_flags;
1189 #define	MPII_IOC_PG8_IRFLAGS_VOLUME_MAPPING_MODE_MASK	(0x00000003)
1190 #define	MPII_IOC_PG8_IRFLAGS_LOW_VOLUME_MAPPING		(0<<0)
1191 #define	MPII_IOC_PG8_IRFLAGS_HIGH_VOLUME_MAPPING	(1<<0)
1192 	u_int16_t		reserved4;
1193 
1194 	u_int32_t		reserved5;
1195 } __packed;
1196 
1197 struct mpii_cfg_raid_physdisk {
1198 	u_int8_t		phys_disk_id;
1199 	u_int8_t		phys_disk_bus;
1200 	u_int8_t		phys_disk_ioc;
1201 	u_int8_t		phys_disk_num;
1202 } __packed;
1203 
1204 struct mpii_cfg_fc_port_pg0 {
1205 	struct mpii_cfg_hdr	config_header;
1206 
1207 	u_int32_t		flags;
1208 
1209 	u_int8_t		mpii_port_nr;
1210 	u_int8_t		link_type;
1211 	u_int8_t		port_state;
1212 	u_int8_t		reserved1;
1213 
1214 	u_int32_t		port_id;
1215 
1216 	u_int64_t		wwnn;
1217 
1218 	u_int64_t		wwpn;
1219 
1220 	u_int32_t		supported_service_class;
1221 
1222 	u_int32_t		supported_speeds;
1223 
1224 	u_int32_t		current_speed;
1225 
1226 	u_int32_t		max_frame_size;
1227 
1228 	u_int64_t		fabric_wwnn;
1229 
1230 	u_int64_t		fabric_wwpn;
1231 
1232 	u_int32_t		discovered_port_count;
1233 
1234 	u_int32_t		max_initiators;
1235 
1236 	u_int8_t		max_aliases_supported;
1237 	u_int8_t		max_hard_aliases_supported;
1238 	u_int8_t		num_current_aliases;
1239 	u_int8_t		reserved2;
1240 } __packed;
1241 
1242 struct mpii_cfg_fc_port_pg1 {
1243 	struct mpii_cfg_hdr	config_header;
1244 
1245 	u_int32_t		flags;
1246 
1247 	u_int64_t		noseepromwwnn;
1248 
1249 	u_int64_t		noseepromwwpn;
1250 
1251 	u_int8_t		hard_alpa;
1252 	u_int8_t		link_config;
1253 	u_int8_t		topology_config;
1254 	u_int8_t		alt_connector;
1255 
1256 	u_int8_t		num_req_aliases;
1257 	u_int8_t		rr_tov;
1258 	u_int8_t		initiator_dev_to;
1259 	u_int8_t		initiator_lo_pend_to;
1260 } __packed;
1261 
1262 struct mpii_cfg_fc_device_pg0 {
1263 	struct mpii_cfg_hdr	config_header;
1264 
1265 	u_int64_t		wwnn;
1266 
1267 	u_int64_t		wwpn;
1268 
1269 	u_int32_t		port_id;
1270 
1271 	u_int8_t		protocol;
1272 	u_int8_t		flags;
1273 	u_int16_t		bb_credit;
1274 
1275 	u_int16_t		max_rx_frame_size;
1276 	u_int8_t		adisc_hard_alpa;
1277 	u_int8_t		port_nr;
1278 
1279 	u_int8_t		fc_ph_low_version;
1280 	u_int8_t		fc_ph_high_version;
1281 	u_int8_t		current_target_id;
1282 	u_int8_t		current_bus;
1283 } __packed;
1284 
1285 #define MPII_CFG_RAID_VOL_ADDR_HANDLE		(1<<28)
1286 
1287 struct mpii_cfg_raid_vol_pg0 {
1288 	struct mpii_cfg_hdr	config_header;
1289 
1290 	u_int16_t		volume_handle;
1291 	u_int8_t		volume_state;
1292 #define MPII_CFG_RAID_VOL_0_STATE_MISSING		(0x00)
1293 #define MPII_CFG_RAID_VOL_0_STATE_FAILED		(0x01)
1294 #define MPII_CFG_RAID_VOL_0_STATE_INITIALIZING		(0x02)
1295 #define MPII_CFG_RAID_VOL_0_STATE_ONLINE		(0x03)
1296 #define MPII_CFG_RAID_VOL_0_STATE_DEGRADED		(0x04)
1297 #define MPII_CFG_RAID_VOL_0_STATE_OPTIMAL		(0x05)
1298 	u_int8_t		volume_type;
1299 #define MPII_CFG_RAID_VOL_0_TYPE_RAID0			(0x00)
1300 #define MPII_CFG_RAID_VOL_0_TYPE_RAID1E			(0x01)
1301 #define MPII_CFG_RAID_VOL_0_TYPE_RAID1			(0x02)
1302 #define MPII_CFG_RAID_VOL_0_TYPE_RAID10			(0x05)
1303 #define MPII_CFG_RAID_VOL_0_TYPE_UNKNOWN		(0xff)
1304 
1305 	u_int32_t		volume_status;
1306 #define MPII_CFG_RAID_VOL_0_STATUS_SCRUB		(1<<20)
1307 #define MPII_CFG_RAID_VOL_0_STATUS_RESYNC		(1<<16)
1308 
1309 	u_int16_t		volume_settings;
1310 #define MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_MASK		(0x3<<0)
1311 #define MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_UNCHANGED	(0x0<<0)
1312 #define MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_DISABLED	(0x1<<0)
1313 #define MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_ENABLED	(0x2<<0)
1314 
1315 	u_int8_t		hot_spare_pool;
1316 	u_int8_t		reserved1;
1317 
1318 	u_int64_t		max_lba;
1319 
1320 	u_int32_t		stripe_size;
1321 
1322 	u_int16_t		block_size;
1323 	u_int16_t		reserved2;
1324 
1325 	u_int8_t		phys_disk_types;
1326 	u_int8_t		resync_rate;
1327 	u_int16_t		data_scrub_rate;
1328 
1329 	u_int8_t		num_phys_disks;
1330 	u_int16_t		reserved3;
1331 	u_int8_t		inactive_status;
1332 #define MPII_CFG_RAID_VOL_0_INACTIVE_UNKNOWN		(0x00)
1333 #define MPII_CFG_RAID_VOL_0_INACTIVE_STALE_META		(0x01)
1334 #define MPII_CFG_RAID_VOL_0_INACTIVE_FOREIGN_VOL	(0x02)
1335 #define MPII_CFG_RAID_VOL_0_INACTIVE_NO_RESOURCES	(0x03)
1336 #define MPII_CFG_RAID_VOL_0_INACTIVE_CLONED_VOL		(0x04)
1337 #define MPII_CFG_RAID_VOL_0_INACTIVE_INSUF_META		(0x05)
1338 
1339 	/* followed by a list of mpii_cfg_raid_vol_pg0_physdisk structs */
1340 } __packed;
1341 
1342 struct mpii_cfg_raid_vol_pg0_physdisk {
1343 	u_int8_t		raid_set_num;
1344 	u_int8_t		phys_disk_map;
1345 	u_int8_t		phys_disk_num;
1346 	u_int8_t		reserved;
1347 } __packed;
1348 
1349 struct mpii_cfg_raid_vol_pg1 {
1350 	struct mpii_cfg_hdr	config_header;
1351 
1352 	u_int8_t		volume_id;
1353 	u_int8_t		volume_bus;
1354 	u_int8_t		volume_ioc;
1355 	u_int8_t		reserved1;
1356 
1357 	u_int8_t		guid[24];
1358 
1359 	u_int8_t		name[32];
1360 
1361 	u_int64_t		wwid;
1362 
1363 	u_int32_t		reserved2;
1364 
1365 	u_int32_t		reserved3;
1366 } __packed;
1367 
1368 #define MPII_CFG_RAID_PHYS_DISK_ADDR_NUMBER		(1<<28)
1369 
1370 struct mpii_cfg_raid_physdisk_pg0 {
1371 	struct mpii_cfg_hdr	config_header;
1372 
1373 	u_int16_t		dev_handle;
1374 	u_int8_t		reserved1;
1375 	u_int8_t		phys_disk_num;
1376 
1377 	u_int8_t		enc_id;
1378 	u_int8_t		enc_bus;
1379 	u_int8_t		hot_spare_pool;
1380 	u_int8_t		enc_type;
1381 #define MPII_CFG_RAID_PHYDISK_0_ENCTYPE_NONE		(0x0)
1382 #define MPII_CFG_RAID_PHYDISK_0_ENCTYPE_SAFTE		(0x1)
1383 #define MPII_CFG_RAID_PHYDISK_0_ENCTYPE_SES		(0x2)
1384 
1385 	u_int32_t		reserved2;
1386 
1387 	u_int8_t		vendor_id[8];
1388 
1389 	u_int8_t		product_id[16];
1390 
1391 	u_int8_t		product_rev[4];
1392 
1393 	u_int8_t		serial[32];
1394 
1395 	u_int32_t		reserved3;
1396 
1397 	u_int8_t		phys_disk_state;
1398 #define MPII_CFG_RAID_PHYDISK_0_STATE_NOTCONFIGURED	(0x00)
1399 #define MPII_CFG_RAID_PHYDISK_0_STATE_NOTCOMPATIBLE	(0x01)
1400 #define MPII_CFG_RAID_PHYDISK_0_STATE_OFFLINE		(0x02)
1401 #define MPII_CFG_RAID_PHYDISK_0_STATE_ONLINE		(0x03)
1402 #define MPII_CFG_RAID_PHYDISK_0_STATE_HOTSPARE		(0x04)
1403 #define MPII_CFG_RAID_PHYDISK_0_STATE_DEGRADED		(0x05)
1404 #define MPII_CFG_RAID_PHYDISK_0_STATE_REBUILDING	(0x06)
1405 #define MPII_CFG_RAID_PHYDISK_0_STATE_OPTIMAL		(0x07)
1406 	u_int8_t		offline_reason;
1407 #define MPII_CFG_RAID_PHYDISK_0_OFFLINE_MISSING		(0x01)
1408 #define MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILED		(0x03)
1409 #define MPII_CFG_RAID_PHYDISK_0_OFFLINE_INITIALIZING	(0x04)
1410 #define MPII_CFG_RAID_PHYDISK_0_OFFLINE_REQUESTED	(0x05)
1411 #define MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILEDREQ	(0x06)
1412 #define MPII_CFG_RAID_PHYDISK_0_OFFLINE_OTHER		(0xff)
1413 
1414 	u_int8_t		incompat_reason;
1415 	u_int8_t		phys_disk_attrs;
1416 
1417 	u_int32_t		phys_disk_status;
1418 #define MPII_CFG_RAID_PHYDISK_0_STATUS_OUTOFSYNC	(1<<0)
1419 #define MPII_CFG_RAID_PHYDISK_0_STATUS_QUIESCED		(1<<1)
1420 
1421 	u_int64_t		dev_max_lba;
1422 
1423 	u_int64_t		host_max_lba;
1424 
1425 	u_int64_t		coerced_max_lba;
1426 
1427 	u_int16_t		block_size;
1428 	u_int16_t		reserved4;
1429 
1430 	u_int32_t		reserved5;
1431 } __packed;
1432 
1433 struct mpii_cfg_raid_physdisk_pg1 {
1434 	struct mpii_cfg_hdr	config_header;
1435 
1436 	u_int8_t		num_phys_disk_paths;
1437 	u_int8_t		phys_disk_num;
1438 	u_int16_t		reserved1;
1439 
1440 	u_int32_t		reserved2;
1441 
1442 	/* followed by mpii_cfg_raid_physdisk_path structs */
1443 } __packed;
1444 
1445 struct mpii_cfg_raid_physdisk_path {
1446 	u_int8_t		phys_disk_id;
1447 	u_int8_t		phys_disk_bus;
1448 	u_int16_t		reserved1;
1449 
1450 	u_int64_t		wwwid;
1451 
1452 	u_int64_t		owner_wwid;
1453 
1454 	u_int8_t		ownder_id;
1455 	u_int8_t		reserved2;
1456 	u_int16_t		flags;
1457 #define MPII_CFG_RAID_PHYDISK_PATH_INVALID	(1<<0)
1458 #define MPII_CFG_RAID_PHYDISK_PATH_BROKEN	(1<<1)
1459 } __packed;
1460 
1461 #define MPII_CFG_SAS_DEV_ADDR_NEXT		(0<<28)
1462 #define MPII_CFG_SAS_DEV_ADDR_BUS		(1<<28)
1463 #define MPII_CFG_SAS_DEV_ADDR_HANDLE		(2<<28)
1464 
1465 struct mpii_cfg_sas_dev_pg0 {
1466 	struct mpii_ecfg_hdr	config_header;
1467 
1468 	u_int16_t		slot;
1469 	u_int16_t		enc_handle;
1470 
1471 	u_int64_t		sas_addr;
1472 
1473 	u_int16_t		parent_dev_handle;
1474 	u_int8_t		phy_num;
1475 	u_int8_t		access_status;
1476 
1477 	u_int16_t		dev_handle;
1478 	u_int8_t		target;
1479 	u_int8_t		bus;
1480 
1481 	u_int32_t		device_info;
1482 #define MPII_CFG_SAS_DEV_0_DEVINFO_TYPE			(0x7)
1483 #define MPII_CFG_SAS_DEV_0_DEVINFO_TYPE_NONE		(0x0)
1484 #define MPII_CFG_SAS_DEV_0_DEVINFO_TYPE_END		(0x1)
1485 #define MPII_CFG_SAS_DEV_0_DEVINFO_TYPE_EDGE_EXPANDER	(0x2)
1486 #define MPII_CFG_SAS_DEV_0_DEVINFO_TYPE_FANOUT_EXPANDER	(0x3)
1487 #define MPII_CFG_SAS_DEV_0_DEVINFO_SATA_HOST		(1<<3)
1488 #define MPII_CFG_SAS_DEV_0_DEVINFO_SMP_INITIATOR	(1<<4)
1489 #define MPII_CFG_SAS_DEV_0_DEVINFO_STP_INITIATOR	(1<<5)
1490 #define MPII_CFG_SAS_DEV_0_DEVINFO_SSP_INITIATOR	(1<<6)
1491 #define MPII_CFG_SAS_DEV_0_DEVINFO_SATA_DEVICE		(1<<7)
1492 #define MPII_CFG_SAS_DEV_0_DEVINFO_SMP_TARGET		(1<<8)
1493 #define MPII_CFG_SAS_DEV_0_DEVINFO_STP_TARGET		(1<<9)
1494 #define MPII_CFG_SAS_DEV_0_DEVINFO_SSP_TARGET		(1<<10)
1495 #define MPII_CFG_SAS_DEV_0_DEVINFO_DIRECT_ATTACHED	(1<<11)
1496 #define MPII_CFG_SAS_DEV_0_DEVINFO_LSI_DEVICE		(1<<12)
1497 #define MPII_CFG_SAS_DEV_0_DEVINFO_ATAPI_DEVICE		(1<<13)
1498 #define MPII_CFG_SAS_DEV_0_DEVINFO_SEP_DEVICE		(1<<14)
1499 
1500 	u_int16_t		flags;
1501 #define MPII_CFG_SAS_DEV_0_FLAGS_DEV_PRESENT		(1<<0)
1502 #define MPII_CFG_SAS_DEV_0_FLAGS_DEV_MAPPED		(1<<1)
1503 #define MPII_CFG_SAS_DEV_0_FLAGS_DEV_MAPPED_PERSISTENT	(1<<2)
1504 #define MPII_CFG_SAS_DEV_0_FLAGS_SATA_PORT_SELECTOR	(1<<3)
1505 #define MPII_CFG_SAS_DEV_0_FLAGS_SATA_FUA		(1<<4)
1506 #define MPII_CFG_SAS_DEV_0_FLAGS_SATA_NCQ		(1<<5)
1507 #define MPII_CFG_SAS_DEV_0_FLAGS_SATA_SMART		(1<<6)
1508 #define MPII_CFG_SAS_DEV_0_FLAGS_SATA_LBA48		(1<<7)
1509 #define MPII_CFG_SAS_DEV_0_FLAGS_UNSUPPORTED		(1<<8)
1510 #define MPII_CFG_SAS_DEV_0_FLAGS_SATA_SETTINGS		(1<<9)
1511 	u_int8_t		physical_port;
1512 	u_int8_t		max_port_conn;
1513 
1514 	u_int64_t		device_name;
1515 
1516 	u_int8_t		port_groups;
1517 	u_int8_t		dma_group;
1518 	u_int8_t		ctrl_group;
1519 	u_int8_t		reserved1;
1520 
1521 	u_int64_t		reserved2;
1522 } __packed;
1523 
1524 #define MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG		(2<<28)
1525 
1526 struct mpii_cfg_raid_config_pg0 {
1527 	struct	mpii_ecfg_hdr	config_header;
1528 
1529 	u_int8_t		num_hot_spares;
1530 	u_int8_t		num_phys_disks;
1531 	u_int8_t		num_volumes;
1532 	u_int8_t		config_num;
1533 
1534 	u_int32_t		flags;
1535 #define MPII_CFG_RAID_CONFIG_0_FLAGS_NATIVE		(0<<0)
1536 #define MPII_CFG_RAID_CONFIG_0_FLAGS_FOREIGN		(1<<0)
1537 
1538 	u_int32_t		config_guid[6];
1539 
1540 	u_int32_t		reserved1;
1541 
1542 	u_int8_t		num_elements;
1543 	u_int8_t		reserved2[3];
1544 
1545 	/* followed by struct mpii_raid_config_element structs */
1546 } __packed;
1547 
1548 struct mpii_raid_config_element {
1549 	u_int16_t		element_flags;
1550 #define MPII_RAID_CONFIG_ELEMENT_FLAG_VOLUME		(0x0)
1551 #define MPII_RAID_CONFIG_ELEMENT_FLAG_VOLUME_PHYS_DISK	(0x1)
1552 #define	MPII_RAID_CONFIG_ELEMENT_FLAG_HSP_PHYS_DISK	(0x2)
1553 #define MPII_RAID_CONFIG_ELEMENT_ONLINE_CE_PHYS_DISK	(0x3)
1554 	u_int16_t		vol_dev_handle;
1555 
1556 	u_int8_t		hot_spare_pool;
1557 	u_int8_t		phys_disk_num;
1558 	u_int16_t		phys_disk_dev_handle;
1559 } __packed;
1560 
1561 struct mpii_cfg_dpm_pg0 {
1562 	struct mpii_ecfg_hdr	config_header;
1563 #define MPII_DPM_ADDRESS_FORM_MASK			(0xf0000000)
1564 #define MPII_DPM_ADDRESS_FORM_ENTRY_RANGE		(0x00000000)
1565 #define MPII_DPM_ADDRESS_ENTRY_COUNT_MASK		(0x0fff0000)
1566 #define MPII_DPM_ADDRESS_ENTRY_COUNT_SHIFT		(16)
1567 #define MPII_DPM_ADDRESS_START_ENTRY_MASK		(0x0000ffff)
1568 
1569 	/* followed by struct mpii_dpm_entry structs */
1570 } __packed;
1571 
1572 struct mpii_dpm_entry {
1573 	u_int64_t		physical_identifier;
1574 
1575 	u_int16_t		mapping_information;
1576 	u_int16_t		device_index;
1577 
1578 	u_int32_t		physical_bits_mapping;
1579 
1580 	u_int32_t		reserved1;
1581 } __packed;
1582 
1583 struct mpii_evt_sas_discovery {
1584 	u_int8_t		flags;
1585 #define	MPII_EVENT_SAS_DISC_FLAGS_DEV_CHANGE_MASK	(1<<1)
1586 #define MPII_EVENT_SAS_DISC_FLAGS_DEV_CHANGE_NO_CHANGE	(0<<1)
1587 #define MPII_EVENT_SAS_DISC_FLAGS_DEV_CHANGE_CHANGE	(1<<1)
1588 #define MPII_EVENT_SAS_DISC_FLAGS_DISC_IN_PROG_MASK	(1<<0)
1589 #define MPII_EVENT_SAS_DISC_FLAGS_DISC_NOT_IN_PROGRESS	(1<<0)
1590 #define MPII_EVENT_SAS_DISC_FLAGS_DISC_IN_PROGRESS	(0<<0)
1591 	u_int8_t		reason_code;
1592 #define MPII_EVENT_SAS_DISC_REASON_CODE_STARTED		(0x01)
1593 #define	MPII_EVENT_SAS_DISC_REASON_CODE_COMPLETED	(0x02)
1594 	u_int8_t		physical_port;
1595 	u_int8_t		reserved1;
1596 
1597 	u_int32_t		discovery_status;
1598 } __packed;
1599 
1600 struct mpii_evt_ir_status {
1601 	u_int16_t		vol_dev_handle;
1602 	u_int16_t		reserved1;
1603 
1604 	u_int8_t		operation;
1605 #define MPII_EVENT_IR_RAIDOP_RESYNC			(0x00)
1606 #define MPII_EVENT_IR_RAIDOP_OCE			(0x01)
1607 #define MPII_EVENT_IR_RAIDOP_CONS_CHECK			(0x02)
1608 #define MPII_EVENT_IR_RAIDOP_BG_INIT			(0x03)
1609 #define MPII_EVENT_IR_RAIDOP_MAKE_CONS			(0x04)
1610 	u_int8_t		percent;
1611 	u_int16_t		reserved2;
1612 
1613 	u_int32_t		reserved3;
1614 };
1615 
1616 struct mpii_evt_ir_volume {
1617 	u_int16_t		vol_dev_handle;
1618 	u_int8_t		reason_code;
1619 #define MPII_EVENT_IR_VOL_RC_SETTINGS_CHANGED		(0x01)
1620 #define MPII_EVENT_IR_VOL_RC_STATUS_CHANGED		(0x02)
1621 #define MPII_EVENT_IR_VOL_RC_STATE_CHANGED		(0x03)
1622 	u_int8_t		reserved1;
1623 
1624 	u_int32_t		new_value;
1625 	u_int32_t		prev_value;
1626 } __packed;
1627 
1628 struct mpii_evt_ir_physical_disk {
1629 	u_int16_t		reserved1;
1630 	u_int8_t		reason_code;
1631 #define MPII_EVENT_IR_PD_RC_SETTINGS_CHANGED		(0x01)
1632 #define MPII_EVENT_IR_PD_RC_STATUS_FLAGS_CHANGED	(0x02)
1633 #define MPII_EVENT_IR_PD_RC_STATUS_CHANGED		(0x03)
1634 	u_int8_t		phys_disk_num;
1635 
1636 	u_int16_t		phys_disk_dev_handle;
1637 	u_int16_t		reserved2;
1638 
1639 	u_int16_t		slot;
1640 	u_int16_t		enclosure_handle;
1641 
1642 	u_int32_t		new_value;
1643 	u_int32_t		previous_value;
1644 } __packed;
1645 
1646 struct mpii_evt_sas_tcl {
1647 	u_int16_t		enclosure_handle;
1648 	u_int16_t		expander_handle;
1649 
1650 	u_int8_t		num_phys;
1651 	u_int8_t		reserved1[3];
1652 
1653 	u_int8_t		num_entries;
1654 	u_int8_t		start_phy_num;
1655 	u_int8_t		expn_status;
1656 #define	MPII_EVENT_SAS_TOPO_ES_ADDED			(0x01)
1657 #define MPII_EVENT_SAS_TOPO_ES_NOT_RESPONDING		(0x02)
1658 #define MPII_EVENT_SAS_TOPO_ES_RESPONDING		(0x03)
1659 #define MPII_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING	(0x04)
1660 	u_int8_t		physical_port;
1661 
1662 	/* followed by num_entries number of struct mpii_evt_phy_entry */
1663 } __packed;
1664 
1665 struct mpii_evt_phy_entry {
1666 	u_int16_t		dev_handle;
1667 	u_int8_t		link_rate;
1668 	u_int8_t		phy_status;
1669 #define MPII_EVENT_SAS_TOPO_PS_RC_MASK			(0x0f)
1670 #define MPII_EVENT_SAS_TOPO_PS_RC_ADDED			(0x01)
1671 #define MPII_EVENT_SAS_TOPO_PS_RC_MISSING		(0x02)
1672 } __packed;
1673 
1674 struct mpii_evt_ir_cfg_change_list {
1675 	u_int8_t		num_elements;
1676 	u_int16_t		reserved;
1677 	u_int8_t		config_num;
1678 
1679 	u_int32_t		flags;
1680 #define MPII_EVT_IR_CFG_CHANGE_LIST_FOREIGN		(0x1)
1681 
1682 	/* followed by num_elements struct mpii_evt_ir_cfg_elements */
1683 } __packed;
1684 
1685 struct mpii_evt_ir_cfg_element {
1686 	u_int16_t		element_flags;
1687 #define MPII_EVT_IR_CFG_ELEMENT_TYPE_MASK		(0xf)
1688 #define MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME		(0x0)
1689 #define MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME_DISK	(0x1)
1690 #define MPII_EVT_IR_CFG_ELEMENT_TYPE_HOT_SPARE		(0x2)
1691 	u_int16_t		vol_dev_handle;
1692 
1693 	u_int8_t		reason_code;
1694 #define MPII_EVT_IR_CFG_ELEMENT_RC_ADDED		(0x01)
1695 #define MPII_EVT_IR_CFG_ELEMENT_RC_REMOVED		(0x02)
1696 #define MPII_EVT_IR_CFG_ELEMENT_RC_NO_CHANGE		(0x03)
1697 #define MPII_EVT_IR_CFG_ELEMENT_RC_HIDE			(0x04)
1698 #define MPII_EVT_IR_CFG_ELEMENT_RC_UNHIDE		(0x05)
1699 #define MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_CREATED	(0x06)
1700 #define MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_DELETED	(0x07)
1701 #define MPII_EVT_IR_CFG_ELEMENT_RC_PD_CREATED		(0x08)
1702 #define MPII_EVT_IR_CFG_ELEMENT_RC_PD_DELETED		(0x09)
1703 	u_int8_t		phys_disk_num;
1704 	u_int16_t		phys_disk_dev_handle;
1705 } __packed;
1706 
1707 /* #define MPII_DEBUG */
1708 #ifdef MPII_DEBUG
1709 #define DPRINTF(x...)		do { if (mpii_debug) printf(x); } while(0)
1710 #define DNPRINTF(n,x...)	do { if (mpii_debug & (n)) printf(x); } while(0)
1711 #define	MPII_D_CMD		(0x0001)
1712 #define	MPII_D_INTR		(0x0002)
1713 #define	MPII_D_MISC		(0x0004)
1714 #define	MPII_D_DMA		(0x0008)
1715 #define	MPII_D_IOCTL		(0x0010)
1716 #define	MPII_D_RW		(0x0020)
1717 #define	MPII_D_MEM		(0x0040)
1718 #define	MPII_D_CCB		(0x0080)
1719 #define	MPII_D_PPR		(0x0100)
1720 #define	MPII_D_RAID		(0x0200)
1721 #define	MPII_D_EVT		(0x0400)
1722 #define MPII_D_CFG		(0x0800)
1723 #define MPII_D_MAP		(0x1000)
1724 
1725 #if 0
1726 u_int32_t  mpii_debug = 0
1727 		| MPII_D_CMD
1728 		| MPII_D_INTR
1729 		| MPII_D_MISC
1730 		| MPII_D_DMA
1731 		| MPII_D_IOCTL
1732 		| MPII_D_RW
1733 		| MPII_D_MEM
1734 		| MPII_D_CCB
1735 		| MPII_D_PPR
1736 		| MPII_D_RAID
1737 		| MPII_D_EVT
1738 		| MPII_D_CFG
1739 		| MPII_D_MAP
1740 	;
1741 #endif
1742 u_int32_t  mpii_debug = MPII_D_MISC;
1743 #else
1744 #define DPRINTF(x...)
1745 #define DNPRINTF(n,x...)
1746 #endif
1747 
1748 #define MPII_REQUEST_SIZE	(512)
1749 #define MPII_REPLY_SIZE		(128)
1750 #define MPII_REPLY_COUNT	PAGE_SIZE / MPII_REPLY_SIZE
1751 
1752 /*
1753  * this is the max number of sge's we can stuff in a request frame:
1754  * sizeof(scsi_io) + sizeof(sense) + sizeof(sge) * 32 = MPII_REQUEST_SIZE
1755  */
1756 #define MPII_MAX_SGL			(32)
1757 
1758 #define MPII_MAX_REQUEST_CREDIT		(128)
1759 
1760 #define MPII_MAXFER MAXPHYS /* XXX bogus */
1761 
1762 struct mpii_dmamem {
1763 	bus_dmamap_t		mdm_map;
1764 	bus_dma_segment_t	mdm_seg;
1765 	size_t			mdm_size;
1766 	void 			*mdm_kva;
1767 };
1768 #define MPII_DMA_MAP(_mdm)	(_mdm)->mdm_map
1769 #define MPII_DMA_DVA(_mdm)	(_mdm)->mdm_map->dm_segs[0].ds_addr
1770 #define MPII_DMA_KVA(_mdm)	(void *)(_mdm)->mdm_kva
1771 
1772 struct mpii_ccb_bundle {
1773 	struct mpii_msg_scsi_io	mcb_io; /* sgl must follow */
1774 	struct mpii_sge		mcb_sgl[MPII_MAX_SGL];
1775 	struct scsi_sense_data	mcb_sense;
1776 } __packed;
1777 
1778 struct mpii_softc;
1779 
1780 struct mpii_rcb {
1781 	union {
1782 		struct work	rcb_wk; /* has to be first in struct */
1783 		SIMPLEQ_ENTRY(mpii_rcb)	rcb_link;
1784 	} u;
1785 	void			*rcb_reply;
1786 	u_int32_t		rcb_reply_dva;
1787 };
1788 
1789 SIMPLEQ_HEAD(mpii_rcb_list, mpii_rcb);
1790 
1791 struct mpii_device {
1792 	int			flags;
1793 #define MPII_DF_ATTACH		(0x0001)
1794 #define MPII_DF_DETACH		(0x0002)
1795 #define MPII_DF_HIDDEN		(0x0004)
1796 #define MPII_DF_UNUSED		(0x0008)
1797 #define MPII_DF_VOLUME		(0x0010)
1798 #define MPII_DF_VOLUME_DISK	(0x0020)
1799 #define MPII_DF_HOT_SPARE	(0x0040)
1800 	short			slot;
1801 	short			percent;
1802 	u_int16_t		dev_handle;
1803 	u_int16_t		enclosure;
1804 	u_int16_t		expander;
1805 	u_int8_t		phy_num;
1806 	u_int8_t		physical_port;
1807 };
1808 
1809 struct mpii_ccb {
1810 	union {
1811 		struct work	ccb_wk; /* has to be first in struct */
1812 		SIMPLEQ_ENTRY(mpii_ccb)	ccb_link;
1813 	} u;
1814 	struct mpii_softc	*ccb_sc;
1815 	int			ccb_smid;
1816 
1817 	void *			ccb_cookie;
1818 	bus_dmamap_t		ccb_dmamap;
1819 
1820 	bus_addr_t		ccb_offset;
1821 	void			*ccb_cmd;
1822 	bus_addr_t		ccb_cmd_dva;
1823 	u_int16_t		ccb_dev_handle;
1824 
1825 	volatile enum {
1826 		MPII_CCB_FREE,
1827 		MPII_CCB_READY,
1828 		MPII_CCB_QUEUED,
1829 		MPII_CCB_TIMEOUT
1830 	}			ccb_state;
1831 
1832 	void			(*ccb_done)(struct mpii_ccb *);
1833 	struct mpii_rcb		*ccb_rcb;
1834 
1835 };
1836 
1837 struct mpii_ccb_wait {
1838 	kmutex_t	mpii_ccbw_mtx;
1839 	kcondvar_t	mpii_ccbw_cv;
1840 };
1841 
1842 SIMPLEQ_HEAD(mpii_ccb_list, mpii_ccb);
1843 
1844 struct mpii_softc {
1845 	device_t		sc_dev;
1846 
1847 	pci_chipset_tag_t	sc_pc;
1848 	pcitag_t		sc_tag;
1849 
1850 	void			*sc_ih;
1851 
1852 	int			sc_flags;
1853 #define MPII_F_RAID		(1<<1)
1854 
1855 	struct scsipi_adapter	sc_adapt;
1856 	struct scsipi_channel	sc_chan;
1857 	device_t		sc_child; /* our scsibus */
1858 
1859 	struct mpii_device	**sc_devs;
1860 
1861 	bus_space_tag_t		sc_iot;
1862 	bus_space_handle_t	sc_ioh;
1863 	bus_size_t		sc_ios;
1864 	bus_dma_tag_t		sc_dmat;
1865 
1866 	kmutex_t		sc_req_mtx;
1867 	kmutex_t		sc_rep_mtx;
1868 
1869 	u_int8_t		sc_porttype;
1870 	int			sc_request_depth;
1871 	int			sc_num_reply_frames;
1872 	int			sc_reply_free_qdepth;
1873 	int			sc_reply_post_qdepth;
1874 	int			sc_maxchdepth;
1875 	int			sc_first_sgl_len;
1876 	int			sc_chain_len;
1877 	int			sc_max_sgl_len;
1878 
1879 	u_int8_t		sc_ioc_event_replay;
1880 	u_int16_t		sc_max_enclosures;
1881 	u_int16_t		sc_max_expanders;
1882 	u_int8_t		sc_max_volumes;
1883 	u_int16_t		sc_max_devices;
1884 	u_int16_t		sc_max_dpm_entries;
1885 	u_int16_t		sc_vd_count;
1886 	u_int16_t		sc_vd_id_low;
1887 	u_int16_t		sc_pd_id_start;
1888 	u_int8_t		sc_num_channels;
1889 	int			sc_ioc_number;
1890 	u_int8_t		sc_vf_id;
1891 	u_int8_t		sc_num_ports;
1892 
1893 	struct mpii_ccb		*sc_ccbs;
1894 	struct mpii_ccb_list	sc_ccb_free;
1895 	kmutex_t		sc_ccb_free_mtx;
1896 	kcondvar_t		sc_ccb_free_cv;
1897 
1898 	kmutex_t		sc_ccb_mtx;
1899 				/*
1900 				 * this protects the ccb state and list entry
1901 				 * between mpii_scsi_cmd and scsidone.
1902 				 */
1903 
1904 	struct workqueue	*sc_ssb_tmowk;
1905 
1906 	struct mpii_dmamem	*sc_requests;
1907 
1908 	struct mpii_dmamem	*sc_replies;
1909 	struct mpii_rcb		*sc_rcbs;
1910 
1911 	struct mpii_dmamem	*sc_reply_postq;
1912 	struct mpii_reply_descr	*sc_reply_postq_kva;
1913 	int			sc_reply_post_host_index;
1914 
1915 	struct mpii_dmamem	*sc_reply_freeq;
1916 	int			sc_reply_free_host_index;
1917 
1918 	struct workqueue	*sc_ssb_evt_ackwk;
1919 
1920 	struct sysmon_envsys	*sc_sme;
1921 	envsys_data_t		*sc_sensors;
1922 };
1923 
1924 static int	mpii_match(device_t, cfdata_t, void *);
1925 static void	mpii_attach(device_t, device_t, void *);
1926 static int	mpii_detach(device_t, int);
1927 static void	mpii_childdetached(device_t, device_t);
1928 static int	mpii_rescan(device_t, const char *, const int *);
1929 
1930 static int	mpii_intr(void *);
1931 
1932 CFATTACH_DECL3_NEW(mpii, sizeof(struct mpii_softc),
1933     mpii_match, mpii_attach, mpii_detach, NULL, mpii_rescan,
1934     mpii_childdetached, DVF_DETACH_SHUTDOWN);
1935 
1936 #define PREAD(s, r)	pci_conf_read((s)->sc_pc, (s)->sc_tag, (r))
1937 #define PWRITE(s, r, v)	pci_conf_write((s)->sc_pc, (s)->sc_tag, (r), (v))
1938 
1939 static void	mpii_scsipi_request(struct scsipi_channel *,
1940 		    scsipi_adapter_req_t, void *);
1941 static void	mpii_scsi_cmd_done(struct mpii_ccb *);
1942 static void	mpii_minphys(struct buf *bp);
1943 
1944 static struct mpii_dmamem *mpii_dmamem_alloc(struct mpii_softc *, size_t);
1945 static void	mpii_dmamem_free(struct mpii_softc *, struct mpii_dmamem *);
1946 static int	mpii_alloc_ccbs(struct mpii_softc *);
1947 static struct mpii_ccb *mpii_get_ccb(struct mpii_softc *, int);
1948 #define MPII_NOSLEEP 0x0001
1949 static void	mpii_put_ccb(struct mpii_softc *, struct mpii_ccb *);
1950 static int	mpii_alloc_replies(struct mpii_softc *);
1951 static int	mpii_alloc_queues(struct mpii_softc *);
1952 static void	mpii_push_reply(struct mpii_softc *, struct mpii_rcb *);
1953 static void	mpii_push_replies(struct mpii_softc *);
1954 
1955 static void	mpii_scsi_cmd_tmo(void *);
1956 static void	mpii_scsi_cmd_tmo_handler(struct work *, void *);
1957 static void	mpii_scsi_cmd_tmo_done(struct mpii_ccb *);
1958 
1959 static int	mpii_alloc_dev(struct mpii_softc *);
1960 static int	mpii_insert_dev(struct mpii_softc *, struct mpii_device *);
1961 static int	mpii_remove_dev(struct mpii_softc *, struct mpii_device *);
1962 static struct mpii_device *mpii_find_dev(struct mpii_softc *, u_int16_t);
1963 
1964 static void	mpii_start(struct mpii_softc *, struct mpii_ccb *);
1965 static int	mpii_poll(struct mpii_softc *, struct mpii_ccb *);
1966 static void	mpii_poll_done(struct mpii_ccb *);
1967 static struct mpii_rcb *mpii_reply(struct mpii_softc *,
1968 			struct mpii_reply_descr *);
1969 
1970 static void	mpii_wait(struct mpii_softc *, struct mpii_ccb *);
1971 static void	mpii_wait_done(struct mpii_ccb *);
1972 
1973 static void	mpii_init_queues(struct mpii_softc *);
1974 
1975 static int	mpii_load_xs(struct mpii_ccb *);
1976 
1977 static u_int32_t mpii_read(struct mpii_softc *, bus_size_t);
1978 static void	mpii_write(struct mpii_softc *, bus_size_t, u_int32_t);
1979 static int	mpii_wait_eq(struct mpii_softc *, bus_size_t, u_int32_t,
1980 		    u_int32_t);
1981 static int	mpii_wait_ne(struct mpii_softc *, bus_size_t, u_int32_t,
1982 		    u_int32_t);
1983 
1984 static int	mpii_init(struct mpii_softc *);
1985 static int	mpii_reset_soft(struct mpii_softc *);
1986 static int	mpii_reset_hard(struct mpii_softc *);
1987 
1988 static int	mpii_handshake_send(struct mpii_softc *, void *, size_t);
1989 static int	mpii_handshake_recv_dword(struct mpii_softc *,
1990 		    u_int32_t *);
1991 static int	mpii_handshake_recv(struct mpii_softc *, void *, size_t);
1992 
1993 static void	mpii_empty_done(struct mpii_ccb *);
1994 
1995 static int	mpii_iocinit(struct mpii_softc *);
1996 static int	mpii_iocfacts(struct mpii_softc *);
1997 static int	mpii_portfacts(struct mpii_softc *);
1998 static int	mpii_portenable(struct mpii_softc *);
1999 static int	mpii_cfg_coalescing(struct mpii_softc *);
2000 
2001 static int	mpii_eventnotify(struct mpii_softc *);
2002 static void	mpii_eventnotify_done(struct mpii_ccb *);
2003 static void	mpii_eventack(struct work *, void *);
2004 static void	mpii_eventack_done(struct mpii_ccb *);
2005 static void	mpii_event_process(struct mpii_softc *, struct mpii_rcb *);
2006 static void	mpii_event_sas(struct mpii_softc *,
2007 		    struct mpii_msg_event_reply *);
2008 static void	mpii_event_raid(struct mpii_softc *,
2009 		    struct mpii_msg_event_reply *);
2010 static void	mpii_event_defer(void *, void *);
2011 
2012 static void	mpii_sas_remove_device(struct mpii_softc *, u_int16_t);
2013 
2014 static int	mpii_req_cfg_header(struct mpii_softc *, u_int8_t,
2015 		    u_int8_t, u_int32_t, int, void *);
2016 static int	mpii_req_cfg_page(struct mpii_softc *, u_int32_t, int,
2017 		    void *, int, void *, size_t);
2018 
2019 static int	mpii_get_ioc_pg8(struct mpii_softc *);
2020 
2021 #if 0
2022 static int	mpii_ioctl_cache(struct scsi_link *, u_long, struct dk_cache *);
2023 #endif
2024 static int	mpii_cache_enable(struct mpii_softc *, struct mpii_device *);
2025 
2026 #if NBIO > 0
2027 static int	mpii_ioctl(device_t, u_long, void *);
2028 static int	mpii_ioctl_inq(struct mpii_softc *, struct bioc_inq *);
2029 static int	mpii_ioctl_vol(struct mpii_softc *, struct bioc_vol *);
2030 static int	mpii_ioctl_disk(struct mpii_softc *, struct bioc_disk *);
2031 static int	mpii_bio_hs(struct mpii_softc *, struct bioc_disk *, int,
2032 		    int, int *);
2033 static int	mpii_bio_disk(struct mpii_softc *, struct bioc_disk *,
2034 		    u_int8_t);
2035 static struct mpii_device *mpii_find_vol(struct mpii_softc *, int);
2036 static int	mpii_bio_volstate(struct mpii_softc *, struct bioc_vol *);
2037 static int	mpii_create_sensors(struct mpii_softc *);
2038 static int	mpii_destroy_sensors(struct mpii_softc *);
2039 static void	mpii_refresh_sensors(struct sysmon_envsys *, envsys_data_t *);
2040 #endif /* NBIO > 0 */
2041 
2042 #define DEVNAME(_s)		(device_xname((_s)->sc_dev))
2043 
2044 #define dwordsof(s)		(sizeof(s) / sizeof(u_int32_t))
2045 #define dwordn(p, n)		(((u_int32_t *)(p))[(n)])
2046 
2047 #define mpii_read_db(s)		mpii_read((s), MPII_DOORBELL)
2048 #define mpii_write_db(s, v)	mpii_write((s), MPII_DOORBELL, (v))
2049 #define mpii_read_intr(s)	mpii_read((s), MPII_INTR_STATUS)
2050 #define mpii_write_intr(s, v)	mpii_write((s), MPII_INTR_STATUS, (v))
2051 #define mpii_reply_waiting(s)	((mpii_read_intr((s)) & MPII_INTR_STATUS_REPLY)\
2052 				    == MPII_INTR_STATUS_REPLY)
2053 
2054 #define mpii_read_reply_free(s)		mpii_read((s), \
2055 						MPII_REPLY_FREE_HOST_INDEX)
2056 #define mpii_write_reply_free(s, v)	mpii_write((s), \
2057 						MPII_REPLY_FREE_HOST_INDEX, (v))
2058 #define mpii_read_reply_post(s)		mpii_read((s), \
2059 						MPII_REPLY_POST_HOST_INDEX)
2060 #define mpii_write_reply_post(s, v)	mpii_write((s), \
2061 						MPII_REPLY_POST_HOST_INDEX, (v))
2062 
2063 #define mpii_wait_db_int(s)	mpii_wait_ne((s), MPII_INTR_STATUS, \
2064 				    MPII_INTR_STATUS_IOC2SYSDB, 0)
2065 #define mpii_wait_db_ack(s)	mpii_wait_eq((s), MPII_INTR_STATUS, \
2066 				    MPII_INTR_STATUS_SYS2IOCDB, 0)
2067 
2068 #define MPII_PG_EXTENDED	(1<<0)
2069 #define MPII_PG_POLL		(1<<1)
2070 #define MPII_PG_FMT		"\020" "\002POLL" "\001EXTENDED"
2071 
2072 #define mpii_cfg_header(_s, _t, _n, _a, _h) \
2073 	mpii_req_cfg_header((_s), (_t), (_n), (_a), \
2074 	    MPII_PG_POLL, (_h))
2075 #define mpii_ecfg_header(_s, _t, _n, _a, _h) \
2076 	mpii_req_cfg_header((_s), (_t), (_n), (_a), \
2077 	    MPII_PG_POLL|MPII_PG_EXTENDED, (_h))
2078 
2079 #define mpii_cfg_page(_s, _a, _h, _r, _p, _l) \
2080 	mpii_req_cfg_page((_s), (_a), MPII_PG_POLL, \
2081 	    (_h), (_r), (_p), (_l))
2082 #define mpii_ecfg_page(_s, _a, _h, _r, _p, _l) \
2083 	mpii_req_cfg_page((_s), (_a), MPII_PG_POLL|MPII_PG_EXTENDED, \
2084 	    (_h), (_r), (_p), (_l))
2085 
2086 
2087 static const struct mpii_pci_product {
2088 	pci_vendor_id_t         mpii_vendor;
2089 	pci_product_id_t        mpii_product;
2090 } mpii_devices[] = {
2091 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2004 },
2092 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2008 },
2093 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_3 },
2094 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_4 },
2095 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_5 },
2096 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2116_1 },
2097 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2116_2 },
2098 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_1 },
2099 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_2 },
2100 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_3 },
2101 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_4 },
2102 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_5 },
2103 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2208_6 },
2104 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2308_1 },
2105 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2308_2 },
2106 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2308_3 },
2107 	{ 0,	0 }
2108 };
2109 
2110 static int
2111 mpii_match(device_t parent, cfdata_t match, void *aux)
2112 {
2113 	struct pci_attach_args *pa = aux;
2114 	const struct mpii_pci_product *mpii;
2115 
2116 	for (mpii = mpii_devices; mpii->mpii_vendor != 0; mpii++) {
2117 		if (PCI_VENDOR(pa->pa_id) == mpii->mpii_vendor &&
2118 		    PCI_PRODUCT(pa->pa_id) == mpii->mpii_product)
2119 			return (1);
2120 	}
2121 	return (0);
2122 }
2123 
2124 static void
2125 mpii_attach(device_t parent, device_t self, void *aux)
2126 {
2127 	struct mpii_softc		*sc = device_private(self);
2128 	struct pci_attach_args		*pa = aux;
2129 	pcireg_t			memtype;
2130 	int				r;
2131 	pci_intr_handle_t		ih;
2132 	const char			*intrstr;
2133 	struct mpii_ccb			*ccb;
2134 	struct scsipi_adapter *adapt = &sc->sc_adapt;
2135 	struct scsipi_channel *chan = &sc->sc_chan;
2136 	char wkname[15];
2137 
2138 	pci_aprint_devinfo(pa, NULL);
2139 
2140 	sc->sc_pc = pa->pa_pc;
2141 	sc->sc_tag = pa->pa_tag;
2142 	sc->sc_dmat = pa->pa_dmat;
2143 	sc->sc_dev = self;
2144 
2145 	mutex_init(&sc->sc_req_mtx, MUTEX_DEFAULT, IPL_BIO);
2146 	mutex_init(&sc->sc_rep_mtx, MUTEX_DEFAULT, IPL_BIO);
2147 	mutex_init(&sc->sc_ccb_free_mtx, MUTEX_DEFAULT, IPL_BIO);
2148 	cv_init(&sc->sc_ccb_free_cv, "mpii_ccbs");
2149 	mutex_init(&sc->sc_ccb_mtx, MUTEX_DEFAULT, IPL_BIO);
2150 
2151 	snprintf(wkname, sizeof(wkname), "%s_tmo", DEVNAME(sc));
2152 	if (workqueue_create(&sc->sc_ssb_tmowk, wkname,
2153 	    mpii_scsi_cmd_tmo_handler, sc, PRI_NONE, IPL_BIO, WQ_MPSAFE) != 0) {
2154 		aprint_error_dev(self, "can't create %s workqueue\n", wkname);
2155 		return;
2156 	}
2157 
2158 	snprintf(wkname, sizeof(wkname), "%s_evt", DEVNAME(sc));
2159 	if (workqueue_create(&sc->sc_ssb_evt_ackwk, wkname,
2160 	    mpii_eventack, sc, PRI_NONE, IPL_BIO, WQ_MPSAFE) != 0) {
2161 		aprint_error_dev(self, "can't create %s workqueue\n", wkname);
2162 		return;
2163 	}
2164 
2165 	/* find the appropriate memory base */
2166 	for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
2167 		memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, r);
2168 		if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM)
2169 			break;
2170 	}
2171 	if (r >= PCI_MAPREG_END) {
2172 		aprint_error_dev(self,
2173 		    "unable to locate system interface registers\n");
2174 		return;
2175 	}
2176 
2177 	if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
2178 	    NULL, &sc->sc_ios) != 0) {
2179 		aprint_error_dev(self,
2180 		    "unable to map system interface registers\n");
2181 		return;
2182 	}
2183 
2184 	/* disable the expansion rom */
2185 	PWRITE(sc, PCI_MAPREG_ROM,
2186 	    PREAD(sc, PCI_MAPREG_ROM) & ~PCI_MAPREG_ROM_ENABLE);
2187 
2188 	/* disable interrupts */
2189 	mpii_write(sc, MPII_INTR_MASK,
2190 	    MPII_INTR_MASK_RESET | MPII_INTR_MASK_REPLY |
2191 	    MPII_INTR_MASK_DOORBELL);
2192 
2193 	/* hook up the interrupt */
2194 	if (pci_intr_map(pa, &ih) != 0) {
2195 		aprint_error_dev(self, "unable to map interrupt\n");
2196 		goto unmap;
2197 	}
2198 	intrstr = pci_intr_string(pa->pa_pc, ih);
2199 
2200 	if (mpii_init(sc) != 0) {
2201 		aprint_error_dev(self, "unable to initialize ioc\n");
2202 		goto unmap;
2203 	}
2204 
2205 	if (mpii_iocfacts(sc) != 0) {
2206 		aprint_error_dev(self, "unable to get iocfacts\n");
2207 		goto unmap;
2208 	}
2209 
2210 	if (mpii_alloc_ccbs(sc) != 0) {
2211 		/* error already printed */
2212 		goto unmap;
2213 	}
2214 
2215 	if (mpii_alloc_replies(sc) != 0) {
2216 		aprint_error_dev(self, "unable to allocated reply space\n");
2217 		goto free_ccbs;
2218 	}
2219 
2220 	if (mpii_alloc_queues(sc) != 0) {
2221 		aprint_error_dev(self, "unable to allocate reply queues\n");
2222 		goto free_replies;
2223 	}
2224 
2225 	if (mpii_iocinit(sc) != 0) {
2226 		aprint_error_dev(self, "unable to send iocinit\n");
2227 		goto free_queues;
2228 	}
2229 
2230 	if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
2231 	    MPII_DOORBELL_STATE_OPER) != 0) {
2232 		aprint_error_dev(self, "state: 0x%08x\n",
2233 			mpii_read_db(sc) & MPII_DOORBELL_STATE);
2234 		aprint_error_dev(self, "operational state timeout\n");
2235 		goto free_queues;
2236 	}
2237 
2238 	mpii_push_replies(sc);
2239 	mpii_init_queues(sc);
2240 
2241 	if (mpii_portfacts(sc) != 0) {
2242 		aprint_error_dev(self, "unable to get portfacts\n");
2243 		goto free_queues;
2244 	}
2245 
2246 	if (mpii_get_ioc_pg8(sc) != 0) {
2247 		aprint_error_dev(self, "unable to get ioc page 8\n");
2248 		goto free_queues;
2249 	}
2250 
2251 	if (mpii_cfg_coalescing(sc) != 0) {
2252 		aprint_error_dev(self, "unable to configure coalescing\n");
2253 		goto free_queues;
2254 	}
2255 
2256 	/* XXX bail on unsupported porttype? */
2257 	if ((sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_SAS_PHYSICAL) ||
2258 	    (sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_SAS_VIRTUAL)) {
2259 		if (mpii_eventnotify(sc) != 0) {
2260 			aprint_error_dev(self, "unable to enable events\n");
2261 			goto free_queues;
2262 		}
2263 	}
2264 
2265 	if (mpii_alloc_dev(sc) != 0) {
2266 		aprint_error_dev(self,
2267 		    "unable to allocate memory for mpii_device\n");
2268 		goto free_queues;
2269 	}
2270 
2271 	if (mpii_portenable(sc) != 0) {
2272 		aprint_error_dev(self, "unable to enable port\n");
2273 		goto free_dev;
2274 	}
2275 
2276 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_BIO,
2277 	    mpii_intr, sc);
2278 	if (sc->sc_ih == NULL) {
2279 		aprint_error_dev(self, "can't establish interrupt");
2280 		if (intrstr)
2281 			aprint_error(" at %s", intrstr);
2282 		aprint_error("\n");
2283 		goto free_dev;
2284 	}
2285 
2286 	memset(adapt, 0, sizeof(*adapt));
2287 	adapt->adapt_dev = sc->sc_dev;
2288 	adapt->adapt_nchannels = 1;
2289 	adapt->adapt_openings = sc->sc_request_depth - 1;
2290 	adapt->adapt_max_periph = adapt->adapt_openings;
2291 	adapt->adapt_request = mpii_scsipi_request;
2292 	adapt->adapt_minphys = mpii_minphys;
2293 
2294 	memset(chan, 0, sizeof(*chan));
2295 	chan->chan_adapter = adapt;
2296 	chan->chan_bustype = &scsi_sas_bustype;
2297 	chan->chan_channel = 0;
2298 	chan->chan_flags = 0;
2299 	chan->chan_nluns = 8;
2300 	chan->chan_ntargets = sc->sc_max_devices;
2301 	chan->chan_id = -1;
2302 
2303 	mpii_rescan(self, "scsi", NULL);
2304 
2305 	/* enable interrupts */
2306 	mpii_write(sc, MPII_INTR_MASK, MPII_INTR_MASK_DOORBELL
2307 	    | MPII_INTR_MASK_RESET);
2308 
2309 #if NBIO > 0
2310 	if (ISSET(sc->sc_flags, MPII_F_RAID)) {
2311 		if (bio_register(sc->sc_dev, mpii_ioctl) != 0)
2312 			panic("%s: controller registration failed",
2313 			    DEVNAME(sc));
2314 
2315 		if (mpii_create_sensors(sc) != 0)
2316 			aprint_error_dev(self, "unable to create sensors\n");
2317 	}
2318 #endif
2319 
2320 	return;
2321 
2322 free_dev:
2323 	if (sc->sc_devs)
2324 		free(sc->sc_devs, M_DEVBUF);
2325 
2326 free_queues:
2327 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_freeq),
2328      	    0, sc->sc_reply_free_qdepth * 4, BUS_DMASYNC_POSTREAD);
2329 	mpii_dmamem_free(sc, sc->sc_reply_freeq);
2330 
2331 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
2332 	    0, sc->sc_reply_post_qdepth * 8, BUS_DMASYNC_POSTREAD);
2333 	mpii_dmamem_free(sc, sc->sc_reply_postq);
2334 
2335 free_replies:
2336 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
2337 		0, PAGE_SIZE, BUS_DMASYNC_POSTREAD);
2338 	mpii_dmamem_free(sc, sc->sc_replies);
2339 
2340 free_ccbs:
2341 	while ((ccb = mpii_get_ccb(sc, MPII_NOSLEEP)) != NULL)
2342 		bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
2343 	mpii_dmamem_free(sc, sc->sc_requests);
2344 	free(sc->sc_ccbs, M_DEVBUF);
2345 
2346 unmap:
2347 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
2348 	sc->sc_ios = 0;
2349 }
2350 
2351 static int
2352 mpii_detach(device_t self, int flags)
2353 {
2354 	struct mpii_softc		*sc = device_private(self);
2355 	int error;
2356 	struct mpii_ccb *ccb;
2357 
2358 	if ((error = config_detach_children(sc->sc_dev, flags)) != 0)
2359 		return error;
2360 
2361 #if NBIO > 0
2362 	mpii_destroy_sensors(sc);
2363 	bio_unregister(sc->sc_dev);
2364 #endif /* NBIO > 0 */
2365 
2366 	if (sc->sc_ih != NULL) {
2367 		if (sc->sc_devs)
2368 			free(sc->sc_devs, M_DEVBUF);
2369 
2370 		bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_freeq),
2371 		    0, sc->sc_reply_free_qdepth * 4, BUS_DMASYNC_POSTREAD);
2372 		mpii_dmamem_free(sc, sc->sc_reply_freeq);
2373 
2374 		bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
2375 		    0, sc->sc_reply_post_qdepth * 8, BUS_DMASYNC_POSTREAD);
2376 		mpii_dmamem_free(sc, sc->sc_reply_postq);
2377 
2378 		bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
2379 			0, PAGE_SIZE, BUS_DMASYNC_POSTREAD);
2380 		mpii_dmamem_free(sc, sc->sc_replies);
2381 
2382 		while ((ccb = mpii_get_ccb(sc, MPII_NOSLEEP)) != NULL)
2383 			bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
2384 		mpii_dmamem_free(sc, sc->sc_requests);
2385 		free(sc->sc_ccbs, M_DEVBUF);
2386 
2387 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
2388 		sc->sc_ih = NULL;
2389 	}
2390 	if (sc->sc_ios != 0) {
2391 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
2392 		sc->sc_ios = 0;
2393 	}
2394 
2395 	return (0);
2396 }
2397 
2398 static int
2399 mpii_rescan(device_t self, const char *ifattr, const int *locators)
2400 {
2401 	struct mpii_softc *sc = device_private(self);
2402 
2403 	if (sc->sc_child != NULL)
2404 		return 0;
2405 
2406 	sc->sc_child = config_found_sm_loc(self, ifattr, locators, &sc->sc_chan,
2407 	    scsiprint, NULL);
2408 
2409 	return 0;
2410 }
2411 
2412 static void
2413 mpii_childdetached(device_t self, device_t child)
2414 {
2415         struct mpii_softc *sc = device_private(self);
2416 
2417         KASSERT(self == sc->sc_dev);
2418         KASSERT(child == sc->sc_child);
2419 
2420         if (child == sc->sc_child)
2421                 sc->sc_child = NULL;
2422 }
2423 
2424 static int
2425 mpii_intr(void *arg)
2426 {
2427 	struct mpii_rcb_list		evts = SIMPLEQ_HEAD_INITIALIZER(evts);
2428 	struct mpii_ccb_list		ccbs = SIMPLEQ_HEAD_INITIALIZER(ccbs);
2429 	struct mpii_softc		*sc = arg;
2430 	struct mpii_reply_descr		*postq = sc->sc_reply_postq_kva, *rdp;
2431 	struct mpii_ccb			*ccb;
2432 	struct mpii_rcb			*rcb;
2433 	int				smid;
2434 	int				rv = 0;
2435 
2436 	mutex_enter(&sc->sc_rep_mtx);
2437 	bus_dmamap_sync(sc->sc_dmat,
2438 	    MPII_DMA_MAP(sc->sc_reply_postq),
2439 	    0, 8 * sc->sc_reply_post_qdepth,
2440 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2441 
2442 	for (;;) {
2443 		rdp = &postq[sc->sc_reply_post_host_index];
2444 		if ((rdp->reply_flags & MPII_REPLY_DESCR_TYPE_MASK) ==
2445 		    MPII_REPLY_DESCR_UNUSED)
2446 			break;
2447 		if (rdp->data == 0xffffffff) {
2448 			/*
2449 			 * ioc is still writing to the reply post queue
2450 			 * race condition - bail!
2451 			 */
2452 			break;
2453 		}
2454 
2455 		smid = le16toh(rdp->smid);
2456 		rcb = mpii_reply(sc, rdp);
2457 
2458 		if (smid) {
2459 			ccb = &sc->sc_ccbs[smid - 1];
2460 			ccb->ccb_state = MPII_CCB_READY;
2461 			ccb->ccb_rcb = rcb;
2462 			SIMPLEQ_INSERT_TAIL(&ccbs, ccb, u.ccb_link);
2463 		} else
2464 			SIMPLEQ_INSERT_TAIL(&evts, rcb, u.rcb_link);
2465 
2466 		sc->sc_reply_post_host_index++;
2467 		sc->sc_reply_post_host_index %= sc->sc_reply_post_qdepth;
2468 		rv = 1;
2469 	}
2470 
2471 	bus_dmamap_sync(sc->sc_dmat,
2472 	    MPII_DMA_MAP(sc->sc_reply_postq),
2473 	    0, 8 * sc->sc_reply_post_qdepth,
2474 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2475 
2476 	if (rv)
2477 		mpii_write_reply_post(sc, sc->sc_reply_post_host_index);
2478 
2479 	mutex_exit(&sc->sc_rep_mtx);
2480 
2481 	if (rv == 0)
2482 		return (0);
2483 
2484 	while ((ccb = SIMPLEQ_FIRST(&ccbs)) != NULL) {
2485 		SIMPLEQ_REMOVE_HEAD(&ccbs, u.ccb_link);
2486 		ccb->ccb_done(ccb);
2487 	}
2488 	while ((rcb = SIMPLEQ_FIRST(&evts)) != NULL) {
2489 		SIMPLEQ_REMOVE_HEAD(&evts, u.rcb_link);
2490 		mpii_event_process(sc, rcb);
2491 	}
2492 
2493 	return (1);
2494 }
2495 
2496 static int
2497 mpii_load_xs(struct mpii_ccb *ccb)
2498 {
2499 	struct mpii_softc	*sc = ccb->ccb_sc;
2500 	struct scsipi_xfer	*xs = ccb->ccb_cookie;
2501 	struct mpii_ccb_bundle	*mcb = ccb->ccb_cmd;
2502 	struct mpii_msg_scsi_io	*io = &mcb->mcb_io;
2503 	struct mpii_sge		*sge = NULL, *nsge = &mcb->mcb_sgl[0];
2504 	struct mpii_sge		*ce = NULL, *nce = NULL;
2505 	u_int64_t		ce_dva;
2506 	bus_dmamap_t		dmap = ccb->ccb_dmamap;
2507 	u_int32_t		addr, flags;
2508 	int			i, error;
2509 
2510 	/* zero length transfer still requires an SGE */
2511 	if (xs->datalen == 0) {
2512 		nsge->sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
2513 		    MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL);
2514 		return (0);
2515 	}
2516 
2517 	error = bus_dmamap_load(sc->sc_dmat, dmap,
2518 	    xs->data, xs->datalen, NULL, (xs->xs_control & XS_CTL_NOSLEEP) ?
2519 	      BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
2520 	if (error) {
2521 		aprint_error_dev(sc->sc_dev, "error %d loading dmamap\n",
2522 		    error);
2523 		return (1);
2524 	}
2525 
2526 	/* safe default staring flags */
2527 	flags = MPII_SGE_FL_TYPE_SIMPLE | MPII_SGE_FL_SIZE_64;
2528 	/* if data out */
2529 	if (xs->xs_control & XS_CTL_DATA_OUT)
2530 		flags |= MPII_SGE_FL_DIR_OUT;
2531 
2532 	/* we will have to exceed the SGEs we can cram into the request frame */
2533 	if (dmap->dm_nsegs > sc->sc_first_sgl_len) {
2534 		ce = &mcb->mcb_sgl[sc->sc_first_sgl_len - 1];
2535 		io->chain_offset = ((u_int8_t *)ce - (u_int8_t *)io) / 4;
2536 	}
2537 
2538 	for (i = 0; i < dmap->dm_nsegs; i++) {
2539 		if (nsge == ce) {
2540 			nsge++;
2541 			sge->sg_hdr |= htole32(MPII_SGE_FL_LAST);
2542 
2543 			DNPRINTF(MPII_D_DMA, "%s:   - 0x%08x 0x%08x 0x%08x\n",
2544 			    DEVNAME(sc), sge->sg_hdr,
2545 			    sge->sg_hi_addr, sge->sg_lo_addr);
2546 
2547 			if ((dmap->dm_nsegs - i) > sc->sc_chain_len) {
2548 				nce = &nsge[sc->sc_chain_len - 1];
2549 				addr = ((u_int8_t *)nce - (u_int8_t *)nsge) / 4;
2550 				addr = addr << 16 |
2551 				    sizeof(struct mpii_sge) * sc->sc_chain_len;
2552 			} else {
2553 				nce = NULL;
2554 				addr = sizeof(struct mpii_sge) *
2555 				    (dmap->dm_nsegs - i);
2556 			}
2557 
2558 			ce->sg_hdr = htole32(MPII_SGE_FL_TYPE_CHAIN |
2559 			    MPII_SGE_FL_SIZE_64 | addr);
2560 
2561 			ce_dva = ccb->ccb_cmd_dva +
2562 			    ((u_int8_t *)nsge - (u_int8_t *)mcb);
2563 
2564 			addr = (u_int32_t)(ce_dva >> 32);
2565 			ce->sg_hi_addr = htole32(addr);
2566 			addr = (u_int32_t)ce_dva;
2567 			ce->sg_lo_addr = htole32(addr);
2568 
2569 			DNPRINTF(MPII_D_DMA, "%s:  ce: 0x%08x 0x%08x 0x%08x\n",
2570 			    DEVNAME(sc), ce->sg_hdr, ce->sg_hi_addr,
2571 			    ce->sg_lo_addr);
2572 
2573 			ce = nce;
2574 		}
2575 
2576 		DNPRINTF(MPII_D_DMA, "%s:  %d: %" PRId64 " 0x%016" PRIx64 "\n",
2577 		    DEVNAME(sc), i, (int64_t)dmap->dm_segs[i].ds_len,
2578 		    (u_int64_t)dmap->dm_segs[i].ds_addr);
2579 
2580 		sge = nsge;
2581 
2582 		sge->sg_hdr = htole32(flags | dmap->dm_segs[i].ds_len);
2583 		addr = (u_int32_t)((u_int64_t)dmap->dm_segs[i].ds_addr >> 32);
2584 		sge->sg_hi_addr = htole32(addr);
2585 		addr = (u_int32_t)dmap->dm_segs[i].ds_addr;
2586 		sge->sg_lo_addr = htole32(addr);
2587 
2588 		DNPRINTF(MPII_D_DMA, "%s:  %d: 0x%08x 0x%08x 0x%08x\n",
2589 		    DEVNAME(sc), i, sge->sg_hdr, sge->sg_hi_addr,
2590 		    sge->sg_lo_addr);
2591 
2592 		nsge = sge + 1;
2593 	}
2594 
2595 	/* terminate list */
2596 	sge->sg_hdr |= htole32(MPII_SGE_FL_LAST | MPII_SGE_FL_EOB |
2597 	    MPII_SGE_FL_EOL);
2598 
2599 	bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
2600 	    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
2601 	    BUS_DMASYNC_PREWRITE);
2602 
2603 	return (0);
2604 }
2605 
2606 static u_int32_t
2607 mpii_read(struct mpii_softc *sc, bus_size_t r)
2608 {
2609 	u_int32_t			rv;
2610 
2611 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2612 	    BUS_SPACE_BARRIER_READ);
2613 	rv = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
2614 
2615 	DNPRINTF(MPII_D_RW, "%s: mpii_read %#" PRIx64 " %#x\n", DEVNAME(sc),
2616 	    (uint64_t)r, rv);
2617 
2618 	return (rv);
2619 }
2620 
2621 static void
2622 mpii_write(struct mpii_softc *sc, bus_size_t r, u_int32_t v)
2623 {
2624 	DNPRINTF(MPII_D_RW, "%s: mpii_write %#" PRIx64 " %#x\n", DEVNAME(sc),
2625 	    (uint64_t)r, v);
2626 
2627 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
2628 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
2629 	    BUS_SPACE_BARRIER_WRITE);
2630 }
2631 
2632 
2633 static int
2634 mpii_wait_eq(struct mpii_softc *sc, bus_size_t r, u_int32_t mask,
2635     u_int32_t target)
2636 {
2637 	int			i;
2638 
2639 	DNPRINTF(MPII_D_RW, "%s: mpii_wait_eq %#" PRIx64 " %#x %#x\n",
2640 	    DEVNAME(sc), (uint64_t)r, mask, target);
2641 
2642 	for (i = 0; i < 15000; i++) {
2643 		if ((mpii_read(sc, r) & mask) == target)
2644 			return (0);
2645 		delay(1000);
2646 	}
2647 
2648 	return (1);
2649 }
2650 
2651 static int
2652 mpii_wait_ne(struct mpii_softc *sc, bus_size_t r, u_int32_t mask,
2653     u_int32_t target)
2654 {
2655 	int			i;
2656 
2657 	DNPRINTF(MPII_D_RW, "%s: mpii_wait_ne %#" PRIx64 " %#x %#x\n",
2658 	    DEVNAME(sc), (uint64_t)r, mask, target);
2659 
2660 	for (i = 0; i < 15000; i++) {
2661 		if ((mpii_read(sc, r) & mask) != target)
2662 			return (0);
2663 		delay(1000);
2664 	}
2665 
2666 	return (1);
2667 }
2668 
2669 
2670 static int
2671 mpii_init(struct mpii_softc *sc)
2672 {
2673 	u_int32_t		db;
2674 	int			i;
2675 
2676 	/* spin until the ioc leaves the reset state */
2677 	if (mpii_wait_ne(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
2678 	    MPII_DOORBELL_STATE_RESET) != 0) {
2679 		DNPRINTF(MPII_D_MISC, "%s: mpii_init timeout waiting to leave "
2680 		    "reset state\n", DEVNAME(sc));
2681 		return (1);
2682 	}
2683 
2684 	/* check current ownership */
2685 	db = mpii_read_db(sc);
2686 	if ((db & MPII_DOORBELL_WHOINIT) == MPII_DOORBELL_WHOINIT_PCIPEER) {
2687 		DNPRINTF(MPII_D_MISC, "%s: mpii_init initialised by pci peer\n",
2688 		    DEVNAME(sc));
2689 		return (0);
2690 	}
2691 
2692 	for (i = 0; i < 5; i++) {
2693 		switch (db & MPII_DOORBELL_STATE) {
2694 		case MPII_DOORBELL_STATE_READY:
2695 			DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is ready\n",
2696 			    DEVNAME(sc));
2697 			return (0);
2698 
2699 		case MPII_DOORBELL_STATE_OPER:
2700 			DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is oper\n",
2701 			    DEVNAME(sc));
2702 			if (sc->sc_ioc_event_replay)
2703 				mpii_reset_soft(sc);
2704 			else
2705 				mpii_reset_hard(sc);
2706 			break;
2707 
2708 		case MPII_DOORBELL_STATE_FAULT:
2709 			DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is being "
2710 			    "reset hard\n" , DEVNAME(sc));
2711 			mpii_reset_hard(sc);
2712 			break;
2713 
2714 		case MPII_DOORBELL_STATE_RESET:
2715 			DNPRINTF(MPII_D_MISC, "%s: mpii_init waiting to come "
2716 			    "out of reset\n", DEVNAME(sc));
2717 			if (mpii_wait_ne(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
2718 			    MPII_DOORBELL_STATE_RESET) != 0)
2719 				return (1);
2720 			break;
2721 		}
2722 		db = mpii_read_db(sc);
2723 	}
2724 
2725 	return (1);
2726 }
2727 
2728 static int
2729 mpii_reset_soft(struct mpii_softc *sc)
2730 {
2731 	DNPRINTF(MPII_D_MISC, "%s: mpii_reset_soft\n", DEVNAME(sc));
2732 
2733 	if (mpii_read_db(sc) & MPII_DOORBELL_INUSE) {
2734 		return (1);
2735 	}
2736 
2737 	mpii_write_db(sc,
2738 	    MPII_DOORBELL_FUNCTION(MPII_FUNCTION_IOC_MESSAGE_UNIT_RESET));
2739 
2740 	/* XXX LSI waits 15 sec */
2741 	if (mpii_wait_db_ack(sc) != 0)
2742 		return (1);
2743 
2744 	/* XXX LSI waits 15 sec */
2745 	if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
2746 	    MPII_DOORBELL_STATE_READY) != 0)
2747 		return (1);
2748 
2749 	/* XXX wait for Sys2IOCDB bit to clear in HIS?? */
2750 
2751 	return (0);
2752 }
2753 
2754 static int
2755 mpii_reset_hard(struct mpii_softc *sc)
2756 {
2757 	u_int16_t		i;
2758 
2759 	DNPRINTF(MPII_D_MISC, "%s: mpii_reset_hard\n", DEVNAME(sc));
2760 
2761 	mpii_write_intr(sc, 0);
2762 
2763 	/* enable diagnostic register */
2764 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_FLUSH);
2765 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_1);
2766 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_2);
2767 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_3);
2768 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_4);
2769 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_5);
2770 	mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_6);
2771 
2772 	delay(100);
2773 
2774 	if ((mpii_read(sc, MPII_HOSTDIAG) & MPII_HOSTDIAG_DWRE) == 0) {
2775 		DNPRINTF(MPII_D_MISC, "%s: mpii_reset_hard failure to enable "
2776 		    "diagnostic read/write\n", DEVNAME(sc));
2777 		return(1);
2778 	}
2779 
2780 	/* reset ioc */
2781 	mpii_write(sc, MPII_HOSTDIAG, MPII_HOSTDIAG_RESET_ADAPTER);
2782 
2783 	/* 240 milliseconds */
2784 	delay(240000);
2785 
2786 
2787 	/* XXX this whole function should be more robust */
2788 
2789 	/* XXX  read the host diagnostic reg until reset adapter bit clears ? */
2790 	for (i = 0; i < 30000; i++) {
2791 		if ((mpii_read(sc, MPII_HOSTDIAG) &
2792 		    MPII_HOSTDIAG_RESET_ADAPTER) == 0)
2793 			break;
2794 		delay(10000);
2795 	}
2796 
2797 	/* disable diagnostic register */
2798 	mpii_write(sc, MPII_WRITESEQ, 0xff);
2799 
2800 	/* XXX what else? */
2801 
2802 	DNPRINTF(MPII_D_MISC, "%s: done with mpii_reset_hard\n", DEVNAME(sc));
2803 
2804 	return(0);
2805 }
2806 
2807 static int
2808 mpii_handshake_send(struct mpii_softc *sc, void *buf, size_t dwords)
2809 {
2810 	u_int32_t		*query = buf;
2811 	int			i;
2812 
2813 	/* make sure the doorbell is not in use. */
2814 	if (mpii_read_db(sc) & MPII_DOORBELL_INUSE)
2815 		return (1);
2816 
2817 	/* clear pending doorbell interrupts */
2818 	if (mpii_read_intr(sc) & MPII_INTR_STATUS_IOC2SYSDB)
2819 		mpii_write_intr(sc, 0);
2820 
2821 	/*
2822 	 * first write the doorbell with the handshake function and the
2823 	 * dword count.
2824 	 */
2825 	mpii_write_db(sc, MPII_DOORBELL_FUNCTION(MPII_FUNCTION_HANDSHAKE) |
2826 	    MPII_DOORBELL_DWORDS(dwords));
2827 
2828 	/*
2829 	 * the doorbell used bit will be set because a doorbell function has
2830 	 * started. wait for the interrupt and then ack it.
2831 	 */
2832 	if (mpii_wait_db_int(sc) != 0)
2833 		return (1);
2834 	mpii_write_intr(sc, 0);
2835 
2836 	/* poll for the acknowledgement. */
2837 	if (mpii_wait_db_ack(sc) != 0)
2838 		return (1);
2839 
2840 	/* write the query through the doorbell. */
2841 	for (i = 0; i < dwords; i++) {
2842 		mpii_write_db(sc, htole32(query[i]));
2843 		if (mpii_wait_db_ack(sc) != 0)
2844 			return (1);
2845 	}
2846 
2847 	return (0);
2848 }
2849 
2850 static int
2851 mpii_handshake_recv_dword(struct mpii_softc *sc, u_int32_t *dword)
2852 {
2853 	u_int16_t		*words = (u_int16_t *)dword;
2854 	int			i;
2855 
2856 	for (i = 0; i < 2; i++) {
2857 		if (mpii_wait_db_int(sc) != 0)
2858 			return (1);
2859 		words[i] = le16toh(mpii_read_db(sc) & MPII_DOORBELL_DATA_MASK);
2860 		mpii_write_intr(sc, 0);
2861 	}
2862 
2863 	return (0);
2864 }
2865 
2866 static int
2867 mpii_handshake_recv(struct mpii_softc *sc, void *buf, size_t dwords)
2868 {
2869 	struct mpii_msg_reply	*reply = buf;
2870 	u_int32_t		*dbuf = buf, dummy;
2871 	int			i;
2872 
2873 	/* get the first dword so we can read the length out of the header. */
2874 	if (mpii_handshake_recv_dword(sc, &dbuf[0]) != 0)
2875 		return (1);
2876 
2877 	DNPRINTF(MPII_D_CMD, "%s: mpii_handshake_recv dwords: %zd reply: %d\n",
2878 	    DEVNAME(sc), dwords, reply->msg_length);
2879 
2880 	/*
2881 	 * the total length, in dwords, is in the message length field of the
2882 	 * reply header.
2883 	 */
2884 	for (i = 1; i < MIN(dwords, reply->msg_length); i++) {
2885 		if (mpii_handshake_recv_dword(sc, &dbuf[i]) != 0)
2886 			return (1);
2887 	}
2888 
2889 	/* if there's extra stuff to come off the ioc, discard it */
2890 	while (i++ < reply->msg_length) {
2891 		if (mpii_handshake_recv_dword(sc, &dummy) != 0)
2892 			return (1);
2893 		DNPRINTF(MPII_D_CMD, "%s: mpii_handshake_recv dummy read: "
2894 		    "0x%08x\n", DEVNAME(sc), dummy);
2895 	}
2896 
2897 	/* wait for the doorbell used bit to be reset and clear the intr */
2898 	if (mpii_wait_db_int(sc) != 0)
2899 		return (1);
2900 
2901 	if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_INUSE, 0) != 0)
2902 		return (1);
2903 
2904 	mpii_write_intr(sc, 0);
2905 
2906 	return (0);
2907 }
2908 
2909 static void
2910 mpii_empty_done(struct mpii_ccb *ccb)
2911 {
2912 	/* nothing to do */
2913 }
2914 
2915 static int
2916 mpii_iocfacts(struct mpii_softc *sc)
2917 {
2918 	struct mpii_msg_iocfacts_request	ifq;
2919 	struct mpii_msg_iocfacts_reply		ifp;
2920 
2921 	DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts\n", DEVNAME(sc));
2922 
2923 	bzero(&ifq, sizeof(ifq));
2924 	bzero(&ifp, sizeof(ifp));
2925 
2926 	ifq.function = MPII_FUNCTION_IOC_FACTS;
2927 
2928 	if (mpii_handshake_send(sc, &ifq, dwordsof(ifq)) != 0) {
2929 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts send failed\n",
2930 		    DEVNAME(sc));
2931 		return (1);
2932 	}
2933 
2934 	if (mpii_handshake_recv(sc, &ifp, dwordsof(ifp)) != 0) {
2935 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts recv failed\n",
2936 		    DEVNAME(sc));
2937 		return (1);
2938 	}
2939 
2940 	DNPRINTF(MPII_D_MISC, "%s:  func: 0x%02x length: %d msgver: %d.%d\n",
2941 	    DEVNAME(sc), ifp.function, ifp.msg_length,
2942 	    ifp.msg_version_maj, ifp.msg_version_min);
2943 	DNPRINTF(MPII_D_MISC, "%s:  msgflags: 0x%02x iocnumber: 0x%02x "
2944 	    "headerver: %d.%d\n", DEVNAME(sc), ifp.msg_flags,
2945 	    ifp.ioc_number, ifp.header_version_unit,
2946 	    ifp.header_version_dev);
2947 	DNPRINTF(MPII_D_MISC, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
2948 	    ifp.vp_id, ifp.vf_id);
2949 	DNPRINTF(MPII_D_MISC, "%s:  iocstatus: 0x%04x ioexceptions: 0x%04x\n",
2950 	    DEVNAME(sc), le16toh(ifp.ioc_status),
2951 	    le16toh(ifp.ioc_exceptions));
2952 	DNPRINTF(MPII_D_MISC, "%s:  iocloginfo: 0x%08x\n", DEVNAME(sc),
2953 	    le32toh(ifp.ioc_loginfo));
2954 	DNPRINTF(MPII_D_MISC, "%s:  numberofports: 0x%02x whoinit: 0x%02x "
2955 	    "maxchaindepth: %d\n", DEVNAME(sc), ifp.number_of_ports,
2956 	    ifp.whoinit, ifp.max_chain_depth);
2957 	DNPRINTF(MPII_D_MISC, "%s:  productid: 0x%04x requestcredit: 0x%04x\n",
2958 	    DEVNAME(sc), le16toh(ifp.product_id), le16toh(ifp.request_credit));
2959 	DNPRINTF(MPII_D_MISC, "%s:  ioc_capabilities: 0x%08x\n", DEVNAME(sc),
2960 	    le32toh(ifp.ioc_capabilities));
2961 	DNPRINTF(MPII_D_MISC, "%s:  fw_version: %d.%d fw_version_unit: 0x%02x "
2962 	    "fw_version_dev: 0x%02x\n", DEVNAME(sc),
2963 	    ifp.fw_version_maj, ifp.fw_version_min,
2964 	    ifp.fw_version_unit, ifp.fw_version_dev);
2965 	DNPRINTF(MPII_D_MISC, "%s:  iocrequestframesize: 0x%04x\n",
2966 	    DEVNAME(sc), le16toh(ifp.ioc_request_frame_size));
2967 	DNPRINTF(MPII_D_MISC, "%s:  maxtargets: 0x%04x "
2968 	    "maxinitiators: 0x%04x\n", DEVNAME(sc),
2969 	    le16toh(ifp.max_targets), le16toh(ifp.max_initiators));
2970 	DNPRINTF(MPII_D_MISC, "%s:  maxenclosures: 0x%04x "
2971 	    "maxsasexpanders: 0x%04x\n", DEVNAME(sc),
2972 	    le16toh(ifp.max_enclosures), le16toh(ifp.max_sas_expanders));
2973 	DNPRINTF(MPII_D_MISC, "%s:  highprioritycredit: 0x%04x "
2974 	    "protocolflags: 0x%02x\n", DEVNAME(sc),
2975 	    le16toh(ifp.high_priority_credit), le16toh(ifp.protocol_flags));
2976 	DNPRINTF(MPII_D_MISC, "%s:  maxvolumes: 0x%02x replyframesize: 0x%02x "
2977 	    "mrdpqd: 0x%04x\n", DEVNAME(sc), ifp.max_volumes,
2978 	    ifp.reply_frame_size,
2979 	    le16toh(ifp.max_reply_descriptor_post_queue_depth));
2980 	DNPRINTF(MPII_D_MISC, "%s:  maxpersistententries: 0x%04x "
2981 	    "maxdevhandle: 0x%02x\n", DEVNAME(sc),
2982 	    le16toh(ifp.max_persistent_entries), le16toh(ifp.max_dev_handle));
2983 
2984 	sc->sc_maxchdepth = ifp.max_chain_depth;
2985 	sc->sc_ioc_number = ifp.ioc_number;
2986 	sc->sc_vf_id = ifp.vf_id;
2987 
2988 	sc->sc_num_ports = ifp.number_of_ports;
2989 	sc->sc_ioc_event_replay = (le32toh(ifp.ioc_capabilities) &
2990 	    MPII_IOCFACTS_CAPABILITY_EVENT_REPLAY) ? 1 : 0;
2991 	sc->sc_max_enclosures = le16toh(ifp.max_enclosures);
2992 	sc->sc_max_expanders = le16toh(ifp.max_sas_expanders);
2993 	sc->sc_max_volumes = ifp.max_volumes;
2994 	sc->sc_max_devices = ifp.max_volumes + le16toh(ifp.max_targets);
2995 	sc->sc_num_channels = 1;
2996 
2997 	if (ISSET(le32toh(ifp.ioc_capabilities),
2998 	    MPII_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
2999 		SET(sc->sc_flags, MPII_F_RAID);
3000 
3001 	sc->sc_request_depth = MIN(le16toh(ifp.request_credit),
3002 	    MPII_MAX_REQUEST_CREDIT);
3003 
3004 	/* should not be multiple of 16 */
3005 	sc->sc_num_reply_frames = sc->sc_request_depth + 32;
3006 	if (!(sc->sc_num_reply_frames % 16))
3007 		sc->sc_num_reply_frames--;
3008 
3009 	/* must be multiple of 16 */
3010 	sc->sc_reply_free_qdepth = sc->sc_num_reply_frames +
3011 	    (16 - (sc->sc_num_reply_frames % 16));
3012 	sc->sc_reply_post_qdepth = ((sc->sc_request_depth +
3013 	    sc->sc_num_reply_frames + 1 + 15) / 16) * 16;
3014 
3015 	if (sc->sc_reply_post_qdepth >
3016 	    ifp.max_reply_descriptor_post_queue_depth)
3017 		sc->sc_reply_post_qdepth =
3018 		    ifp.max_reply_descriptor_post_queue_depth;
3019 
3020 	DNPRINTF(MPII_D_MISC, "%s: sc_request_depth: %d "
3021 	    "sc_num_reply_frames: %d sc_reply_free_qdepth: %d "
3022 	    "sc_reply_post_qdepth: %d\n", DEVNAME(sc), sc->sc_request_depth,
3023 	    sc->sc_num_reply_frames, sc->sc_reply_free_qdepth,
3024 	    sc->sc_reply_post_qdepth);
3025 
3026 	/*
3027 	 * you can fit sg elements on the end of the io cmd if they fit in the
3028 	 * request frame size.
3029 	 */
3030 
3031 	sc->sc_first_sgl_len = ((le16toh(ifp.ioc_request_frame_size) * 4) -
3032 	    sizeof(struct mpii_msg_scsi_io)) / sizeof(struct mpii_sge);
3033 	DNPRINTF(MPII_D_MISC, "%s:   first sgl len: %d\n", DEVNAME(sc),
3034 	    sc->sc_first_sgl_len);
3035 
3036 	sc->sc_chain_len = (le16toh(ifp.ioc_request_frame_size) * 4) /
3037 	    sizeof(struct mpii_sge);
3038 	DNPRINTF(MPII_D_MISC, "%s:   chain len: %d\n", DEVNAME(sc),
3039 	    sc->sc_chain_len);
3040 
3041 	/* the sgl tailing the io cmd loses an entry to the chain element. */
3042 	sc->sc_max_sgl_len = MPII_MAX_SGL - 1;
3043 	/* the sgl chains lose an entry for each chain element */
3044 	sc->sc_max_sgl_len -= (MPII_MAX_SGL - sc->sc_first_sgl_len) /
3045 	    sc->sc_chain_len;
3046 	DNPRINTF(MPII_D_MISC, "%s:   max sgl len: %d\n", DEVNAME(sc),
3047 	    sc->sc_max_sgl_len);
3048 
3049 	/* XXX we're ignoring the max chain depth */
3050 
3051 	return(0);
3052 
3053 }
3054 
3055 static int
3056 mpii_iocinit(struct mpii_softc *sc)
3057 {
3058 	struct mpii_msg_iocinit_request		iiq;
3059 	struct mpii_msg_iocinit_reply		iip;
3060 	u_int32_t				hi_addr;
3061 
3062 	DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit\n", DEVNAME(sc));
3063 
3064 	bzero(&iiq, sizeof(iiq));
3065 	bzero(&iip, sizeof(iip));
3066 
3067 	iiq.function = MPII_FUNCTION_IOC_INIT;
3068 	iiq.whoinit = MPII_WHOINIT_HOST_DRIVER;
3069 
3070 	/* XXX JPG do something about vf_id */
3071 	iiq.vf_id = 0;
3072 
3073 	iiq.msg_version_maj = 0x02;
3074 	iiq.msg_version_min = 0x00;
3075 
3076 	/* XXX JPG ensure compliance with some level and hard-code? */
3077 	iiq.hdr_version_unit = 0x00;
3078 	iiq.hdr_version_dev = 0x00;
3079 
3080 	iiq.system_request_frame_size = htole16(MPII_REQUEST_SIZE / 4);
3081 
3082 	iiq.reply_descriptor_post_queue_depth =
3083 	    htole16(sc->sc_reply_post_qdepth);
3084 
3085 	iiq.reply_free_queue_depth = htole16(sc->sc_reply_free_qdepth);
3086 
3087 	hi_addr = (u_int32_t)((u_int64_t)MPII_DMA_DVA(sc->sc_requests) >> 32);
3088 	iiq.sense_buffer_address_high = htole32(hi_addr);
3089 
3090 	hi_addr = (u_int32_t)
3091 	    ((u_int64_t)MPII_DMA_DVA(sc->sc_replies) >> 32);
3092 	iiq.system_reply_address_high = htole32(hi_addr);
3093 
3094 	iiq.system_request_frame_base_address =
3095 	    (u_int64_t)MPII_DMA_DVA(sc->sc_requests);
3096 
3097 	iiq.reply_descriptor_post_queue_address =
3098 	    (u_int64_t)MPII_DMA_DVA(sc->sc_reply_postq);
3099 
3100 	iiq.reply_free_queue_address =
3101 	    (u_int64_t)MPII_DMA_DVA(sc->sc_reply_freeq);
3102 
3103 	if (mpii_handshake_send(sc, &iiq, dwordsof(iiq)) != 0) {
3104 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit send failed\n",
3105 		    DEVNAME(sc));
3106 		return (1);
3107 	}
3108 
3109 	if (mpii_handshake_recv(sc, &iip, dwordsof(iip)) != 0) {
3110 		DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit recv failed\n",
3111 		    DEVNAME(sc));
3112 		return (1);
3113 	}
3114 
3115 	DNPRINTF(MPII_D_MISC, "%s:  function: 0x%02x msg_length: %d "
3116 	    "whoinit: 0x%02x\n", DEVNAME(sc), iip.function,
3117 	    iip.msg_length, iip.whoinit);
3118 	DNPRINTF(MPII_D_MISC, "%s:  msg_flags: 0x%02x\n", DEVNAME(sc),
3119 	    iip.msg_flags);
3120 	DNPRINTF(MPII_D_MISC, "%s:  vf_id: 0x%02x vp_id: 0x%02x\n", DEVNAME(sc),
3121 	    iip.vf_id, iip.vp_id);
3122 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
3123 	    le16toh(iip.ioc_status));
3124 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
3125 	    le32toh(iip.ioc_loginfo));
3126 
3127 	if ((iip.ioc_status != MPII_IOCSTATUS_SUCCESS) || (iip.ioc_loginfo))
3128 		return (1);
3129 
3130 	return (0);
3131 }
3132 
3133 static void
3134 mpii_push_reply(struct mpii_softc *sc, struct mpii_rcb *rcb)
3135 {
3136 	u_int32_t		*rfp;
3137 
3138 	if (rcb == NULL)
3139 		return;
3140 
3141 	rfp = MPII_DMA_KVA(sc->sc_reply_freeq);
3142 	rfp[sc->sc_reply_free_host_index] = rcb->rcb_reply_dva;
3143 
3144 	sc->sc_reply_free_host_index = (sc->sc_reply_free_host_index + 1) %
3145 	    sc->sc_reply_free_qdepth;
3146 
3147 	mpii_write_reply_free(sc, sc->sc_reply_free_host_index);
3148 }
3149 
3150 static int
3151 mpii_portfacts(struct mpii_softc *sc)
3152 {
3153 	struct mpii_msg_portfacts_request	*pfq;
3154 	struct mpii_msg_portfacts_reply		*pfp;
3155 	struct mpii_ccb				*ccb;
3156 	int					rv = 1;
3157 
3158 	DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts\n", DEVNAME(sc));
3159 
3160 	ccb = mpii_get_ccb(sc, 0);
3161 	if (ccb == NULL) {
3162 		DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts mpii_get_ccb fail\n",
3163 		    DEVNAME(sc));
3164 		return (rv);
3165 	}
3166 
3167 	ccb->ccb_done = mpii_empty_done;
3168 	pfq = ccb->ccb_cmd;
3169 
3170 	bzero(pfq, sizeof(*pfq));
3171 
3172 	pfq->function = MPII_FUNCTION_PORT_FACTS;
3173 	pfq->chain_offset = 0;
3174 	pfq->msg_flags = 0;
3175 	pfq->port_number = 0;
3176 	pfq->vp_id = 0;
3177 	pfq->vf_id = 0;
3178 
3179 	if (mpii_poll(sc, ccb) != 0) {
3180 		DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts poll\n",
3181 		    DEVNAME(sc));
3182 		goto err;
3183 	}
3184 
3185 	if (ccb->ccb_rcb == NULL) {
3186 		DNPRINTF(MPII_D_MISC, "%s: empty portfacts reply\n",
3187 		    DEVNAME(sc));
3188 		goto err;
3189 	}
3190 
3191 	pfp = ccb->ccb_rcb->rcb_reply;
3192 	DNPRINTF(MPII_D_MISC, "%s   pfp: %p\n", DEVNAME(sc), pfp);
3193 
3194 	DNPRINTF(MPII_D_MISC, "%s:  function: 0x%02x msg_length: %d\n",
3195 	    DEVNAME(sc), pfp->function, pfp->msg_length);
3196 	DNPRINTF(MPII_D_MISC, "%s:  msg_flags: 0x%02x port_number: %d\n",
3197 	    DEVNAME(sc), pfp->msg_flags, pfp->port_number);
3198 	DNPRINTF(MPII_D_MISC, "%s:  vf_id: 0x%02x vp_id: 0x%02x\n",
3199 	    DEVNAME(sc), pfp->vf_id, pfp->vp_id);
3200 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
3201 	    le16toh(pfp->ioc_status));
3202 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
3203 	    le32toh(pfp->ioc_loginfo));
3204 	DNPRINTF(MPII_D_MISC, "%s:  port_type: 0x%02x\n", DEVNAME(sc),
3205 	    pfp->port_type);
3206 	DNPRINTF(MPII_D_MISC, "%s:  max_posted_cmd_buffers: %d\n", DEVNAME(sc),
3207 	    le16toh(pfp->max_posted_cmd_buffers));
3208 
3209 	sc->sc_porttype = pfp->port_type;
3210 
3211 	mpii_push_reply(sc, ccb->ccb_rcb);
3212 	rv = 0;
3213 err:
3214 	mpii_put_ccb(sc, ccb);
3215 
3216 	return (rv);
3217 }
3218 
3219 static void
3220 mpii_eventack(struct work *wk, void *cookie)
3221 {
3222 	struct mpii_softc			*sc = cookie;
3223 	struct mpii_ccb				*ccb;
3224 	struct mpii_rcb				*rcb = (void *)wk;
3225 	struct mpii_msg_event_reply		*enp;
3226 	struct mpii_msg_eventack_request	*eaq;
3227 
3228 	ccb = mpii_get_ccb(sc, 0);
3229 
3230 	enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
3231 
3232 	ccb->ccb_done = mpii_eventack_done;
3233 	eaq = ccb->ccb_cmd;
3234 
3235 	eaq->function = MPII_FUNCTION_EVENT_ACK;
3236 
3237 	eaq->event = enp->event;
3238 	eaq->event_context = enp->event_context;
3239 
3240 	mpii_push_reply(sc, rcb);
3241 
3242 	mpii_start(sc, ccb);
3243 
3244 }
3245 
3246 static void
3247 mpii_eventack_done(struct mpii_ccb *ccb)
3248 {
3249 	struct mpii_softc			*sc = ccb->ccb_sc;
3250 
3251 	DNPRINTF(MPII_D_EVT, "%s: event ack done\n", DEVNAME(sc));
3252 
3253 	mpii_push_reply(sc, ccb->ccb_rcb);
3254 	mpii_put_ccb(sc, ccb);
3255 }
3256 
3257 static int
3258 mpii_portenable(struct mpii_softc *sc)
3259 {
3260 	struct mpii_msg_portenable_request	*peq;
3261 	struct mpii_ccb				*ccb;
3262 
3263 	DNPRINTF(MPII_D_MISC, "%s: mpii_portenable\n", DEVNAME(sc));
3264 
3265 	ccb = mpii_get_ccb(sc, 0);
3266 	if (ccb == NULL) {
3267 		DNPRINTF(MPII_D_MISC, "%s: mpii_portenable ccb_get\n",
3268 		    DEVNAME(sc));
3269 		return (1);
3270 	}
3271 
3272 	ccb->ccb_done = mpii_empty_done;
3273 	peq = ccb->ccb_cmd;
3274 
3275 	peq->function = MPII_FUNCTION_PORT_ENABLE;
3276 	peq->vf_id = sc->sc_vf_id;
3277 
3278 	if (mpii_poll(sc, ccb) != 0) {
3279 		DNPRINTF(MPII_D_MISC, "%s: mpii_portenable poll\n",
3280 		    DEVNAME(sc));
3281 		return (1);
3282 	}
3283 
3284 	if (ccb->ccb_rcb == NULL) {
3285 		DNPRINTF(MPII_D_MISC, "%s: empty portenable reply\n",
3286 		    DEVNAME(sc));
3287 		return (1);
3288 	}
3289 
3290 	mpii_push_reply(sc, ccb->ccb_rcb);
3291 	mpii_put_ccb(sc, ccb);
3292 
3293 	return (0);
3294 }
3295 
3296 static int
3297 mpii_cfg_coalescing(struct mpii_softc *sc)
3298 {
3299 	struct mpii_cfg_hdr		hdr;
3300 	struct mpii_cfg_ioc_pg1		pg;
3301 
3302 	if (mpii_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_IOC, 1, 0,
3303 	    &hdr) != 0) {
3304 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch IOC page 1 "
3305 		    "header\n", DEVNAME(sc));
3306 		return (1);
3307 	}
3308 
3309 	if (mpii_cfg_page(sc, 0, &hdr, 1, &pg, sizeof(pg)) != 0) {
3310 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch IOC page 1\n"
3311 		    "page 1\n", DEVNAME(sc));
3312 		return (1);
3313 	}
3314 
3315 	DNPRINTF(MPII_D_MISC, "%s: IOC page 1\n", DEVNAME(sc));
3316 	DNPRINTF(MPII_D_MISC, "%s:  flags: 0x08%x\n", DEVNAME(sc),
3317 	    le32toh(pg.flags));
3318 	DNPRINTF(MPII_D_MISC, "%s:  coalescing_timeout: %d\n", DEVNAME(sc),
3319 	    le32toh(pg.coalescing_timeout));
3320 	DNPRINTF(MPII_D_MISC, "%s:  coalescing_depth: %d pci_slot_num: %d\n",
3321 	    DEVNAME(sc), pg.coalescing_timeout, pg.pci_slot_num);
3322 
3323 	if (!ISSET(le32toh(pg.flags), MPII_CFG_IOC_1_REPLY_COALESCING))
3324 		return (0);
3325 
3326 	CLR(pg.flags, htole32(MPII_CFG_IOC_1_REPLY_COALESCING));
3327 	if (mpii_cfg_page(sc, 0, &hdr, 0, &pg, sizeof(pg)) != 0) {
3328 		DNPRINTF(MPII_D_MISC, "%s: unable to clear coalescing\n",
3329 		    DEVNAME(sc));
3330 		return (1);
3331 	}
3332 
3333 	return (0);
3334 }
3335 
3336 #define MPII_EVENT_MASKALL(enq)		do {			\
3337 		enq->event_masks[0] = 0xffffffff;		\
3338 		enq->event_masks[1] = 0xffffffff;		\
3339 		enq->event_masks[2] = 0xffffffff;		\
3340 		enq->event_masks[3] = 0xffffffff;		\
3341 	} while (0)
3342 
3343 #define MPII_EVENT_UNMASK(enq, evt)	do {			\
3344 		enq->event_masks[evt / 32] &=			\
3345 		    htole32(~(1 << (evt % 32)));		\
3346 	} while (0)
3347 
3348 static int
3349 mpii_eventnotify(struct mpii_softc *sc)
3350 {
3351 	struct mpii_msg_event_request		*enq;
3352 	struct mpii_ccb				*ccb;
3353 
3354 	ccb = mpii_get_ccb(sc, 0);
3355 	if (ccb == NULL) {
3356 		DNPRINTF(MPII_D_MISC, "%s: mpii_eventnotify ccb_get\n",
3357 		    DEVNAME(sc));
3358 		return (1);
3359 	}
3360 
3361 	ccb->ccb_done = mpii_eventnotify_done;
3362 	enq = ccb->ccb_cmd;
3363 
3364 	enq->function = MPII_FUNCTION_EVENT_NOTIFICATION;
3365 
3366 	/*
3367 	 * Enable reporting of the following events:
3368 	 *
3369 	 * MPII_EVENT_SAS_DISCOVERY
3370 	 * MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST
3371 	 * MPII_EVENT_SAS_DEVICE_STATUS_CHANGE
3372 	 * MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE
3373 	 * MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST
3374 	 * MPII_EVENT_IR_VOLUME
3375 	 * MPII_EVENT_IR_PHYSICAL_DISK
3376 	 * MPII_EVENT_IR_OPERATION_STATUS
3377 	 */
3378 
3379 	MPII_EVENT_MASKALL(enq);
3380 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_DISCOVERY);
3381 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3382 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_DEVICE_STATUS_CHANGE);
3383 	MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
3384 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST);
3385 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_VOLUME);
3386 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_PHYSICAL_DISK);
3387 	MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_OPERATION_STATUS);
3388 
3389 	mpii_start(sc, ccb);
3390 
3391 	return (0);
3392 }
3393 
3394 static void
3395 mpii_eventnotify_done(struct mpii_ccb *ccb)
3396 {
3397 	struct mpii_softc			*sc = ccb->ccb_sc;
3398 	struct mpii_rcb				*rcb = ccb->ccb_rcb;
3399 
3400 	DNPRINTF(MPII_D_EVT, "%s: mpii_eventnotify_done\n", DEVNAME(sc));
3401 
3402 	mpii_put_ccb(sc, ccb);
3403 	mpii_event_process(sc, rcb);
3404 }
3405 
3406 static void
3407 mpii_event_raid(struct mpii_softc *sc, struct mpii_msg_event_reply *enp)
3408 {
3409 	struct mpii_evt_ir_cfg_change_list	*ccl;
3410 	struct mpii_evt_ir_cfg_element		*ce;
3411 	struct mpii_device			*dev;
3412 	u_int16_t				type;
3413 	int					i;
3414 
3415 	ccl = (struct mpii_evt_ir_cfg_change_list *)(enp + 1);
3416 
3417 	if (ccl->num_elements == 0)
3418 		return;
3419 	if (ISSET(le32toh(ccl->flags), MPII_EVT_IR_CFG_CHANGE_LIST_FOREIGN))
3420 		/* bail on foreign configurations */
3421 		return;
3422 
3423 	ce = (struct mpii_evt_ir_cfg_element *)(ccl + 1);
3424 
3425 	for (i = 0; i < ccl->num_elements; i++, ce++) {
3426 		type = (le16toh(ce->element_flags) &
3427 		    MPII_EVT_IR_CFG_ELEMENT_TYPE_MASK);
3428 
3429 		switch (type) {
3430 		case MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME:
3431 			switch (ce->reason_code) {
3432 			case MPII_EVT_IR_CFG_ELEMENT_RC_ADDED:
3433 			case MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_CREATED:
3434 				if (mpii_find_dev(sc,
3435 				    le16toh(ce->vol_dev_handle))) {
3436 					printf("%s: device %#x is already "
3437 					    "configured\n", DEVNAME(sc),
3438 					    le16toh(ce->vol_dev_handle));
3439 					break;
3440 				}
3441 				dev = malloc(sizeof(*dev), M_DEVBUF,
3442 				    M_NOWAIT | M_ZERO);
3443 				if (!dev) {
3444 					printf("%s: failed to allocate a "
3445 				    	    "device structure\n", DEVNAME(sc));
3446 					break;
3447 				}
3448 				SET(dev->flags, MPII_DF_VOLUME);
3449 				dev->slot = sc->sc_vd_id_low;
3450 				dev->dev_handle = le16toh(ce->vol_dev_handle);
3451 				if (mpii_insert_dev(sc, dev)) {
3452 					free(dev, M_DEVBUF);
3453 					break;
3454 				}
3455 				mpii_cache_enable(sc, dev);
3456 				sc->sc_vd_count++;
3457 				break;
3458 			case MPII_EVT_IR_CFG_ELEMENT_RC_REMOVED:
3459 			case MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_DELETED:
3460 				if (!(dev = mpii_find_dev(sc,
3461 				    le16toh(ce->vol_dev_handle))))
3462 					break;
3463 				mpii_remove_dev(sc, dev);
3464 				sc->sc_vd_count--;
3465 				break;
3466 			}
3467 			break;
3468 		case MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME_DISK:
3469 			if (ce->reason_code ==
3470 			    MPII_EVT_IR_CFG_ELEMENT_RC_PD_CREATED ||
3471 			    ce->reason_code ==
3472 			    MPII_EVT_IR_CFG_ELEMENT_RC_HIDE) {
3473 				/* there should be an underlying sas drive */
3474 				if (!(dev = mpii_find_dev(sc,
3475 				    le16toh(ce->phys_disk_dev_handle))))
3476 					break;
3477 				/* promoted from a hot spare? */
3478 				CLR(dev->flags, MPII_DF_HOT_SPARE);
3479 				SET(dev->flags, MPII_DF_VOLUME_DISK |
3480 				    MPII_DF_HIDDEN);
3481 			}
3482 			break;
3483 		case MPII_EVT_IR_CFG_ELEMENT_TYPE_HOT_SPARE:
3484 			if (ce->reason_code ==
3485 			    MPII_EVT_IR_CFG_ELEMENT_RC_HIDE) {
3486 				/* there should be an underlying sas drive */
3487 				if (!(dev = mpii_find_dev(sc,
3488 				    le16toh(ce->phys_disk_dev_handle))))
3489 					break;
3490 				SET(dev->flags, MPII_DF_HOT_SPARE |
3491 				    MPII_DF_HIDDEN);
3492 			}
3493 			break;
3494 		}
3495 	}
3496 }
3497 
3498 static void
3499 mpii_event_sas(struct mpii_softc *sc, struct mpii_msg_event_reply *enp)
3500 {
3501 	struct mpii_evt_sas_tcl		*tcl;
3502 	struct mpii_evt_phy_entry	*pe;
3503 	struct mpii_device		*dev;
3504 	int				i;
3505 
3506 	tcl = (struct mpii_evt_sas_tcl *)(enp + 1);
3507 
3508 	if (tcl->num_entries == 0)
3509 		return;
3510 
3511 	pe = (struct mpii_evt_phy_entry *)(tcl + 1);
3512 
3513 	for (i = 0; i < tcl->num_entries; i++, pe++) {
3514 		switch (pe->phy_status & MPII_EVENT_SAS_TOPO_PS_RC_MASK) {
3515 		case MPII_EVENT_SAS_TOPO_PS_RC_ADDED:
3516 			if (mpii_find_dev(sc, le16toh(pe->dev_handle))) {
3517 				printf("%s: device %#x is already "
3518 				    "configured\n", DEVNAME(sc),
3519 				    le16toh(pe->dev_handle));
3520 				break;
3521 			}
3522 			dev = malloc(sizeof(*dev), M_DEVBUF, M_NOWAIT | M_ZERO);
3523 			if (!dev) {
3524 				printf("%s: failed to allocate a "
3525 				    "device structure\n", DEVNAME(sc));
3526 				break;
3527 			}
3528 			dev->slot = sc->sc_pd_id_start + tcl->start_phy_num + i;
3529 			dev->dev_handle = le16toh(pe->dev_handle);
3530 			dev->phy_num = tcl->start_phy_num + i;
3531 			if (tcl->enclosure_handle)
3532 				dev->physical_port = tcl->physical_port;
3533 			dev->enclosure = le16toh(tcl->enclosure_handle);
3534 			dev->expander = le16toh(tcl->expander_handle);
3535 			if (mpii_insert_dev(sc, dev)) {
3536 				free(dev, M_DEVBUF);
3537 				break;
3538 			}
3539 			break;
3540 		case MPII_EVENT_SAS_TOPO_PS_RC_MISSING:
3541 			if (!(dev = mpii_find_dev(sc,
3542 			    le16toh(pe->dev_handle))))
3543 				break;
3544 			mpii_remove_dev(sc, dev);
3545 #if 0
3546 			if (sc->sc_scsibus) {
3547 				SET(dev->flags, MPII_DF_DETACH);
3548 				scsi_activate(sc->sc_scsibus, dev->slot, -1,
3549 				    DVACT_DEACTIVATE);
3550 				if (scsi_task(mpii_event_defer, sc,
3551 				    dev, 0) != 0)
3552 					printf("%s: unable to run device "
3553 					    "detachment routine\n",
3554 					    DEVNAME(sc));
3555 			}
3556 #else
3557 			mpii_event_defer(sc, dev);
3558 #endif /* XXX */
3559 			break;
3560 		}
3561 	}
3562 }
3563 
3564 static void
3565 mpii_event_process(struct mpii_softc *sc, struct mpii_rcb *rcb)
3566 {
3567 	struct mpii_msg_event_reply		*enp;
3568 
3569 	enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
3570 
3571 	DNPRINTF(MPII_D_EVT, "%s: mpii_event_process: %#x\n", DEVNAME(sc),
3572 	    le32toh(enp->event));
3573 
3574 	switch (le32toh(enp->event)) {
3575 	case MPII_EVENT_EVENT_CHANGE:
3576 		/* should be properly ignored */
3577 		break;
3578 	case MPII_EVENT_SAS_DISCOVERY: {
3579 		struct mpii_evt_sas_discovery	*esd =
3580 		    (struct mpii_evt_sas_discovery *)(enp + 1);
3581 
3582 		if (esd->reason_code ==
3583 		    MPII_EVENT_SAS_DISC_REASON_CODE_COMPLETED &&
3584 		    esd->discovery_status != 0)
3585 			printf("%s: sas discovery completed with status %#x\n",
3586 			    DEVNAME(sc), esd->discovery_status);
3587 		}
3588 		break;
3589 	case MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
3590 		mpii_event_sas(sc, enp);
3591 		break;
3592 	case MPII_EVENT_SAS_DEVICE_STATUS_CHANGE:
3593 		break;
3594 	case MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
3595 		break;
3596 	case MPII_EVENT_IR_VOLUME: {
3597 		struct mpii_evt_ir_volume	*evd =
3598 		    (struct mpii_evt_ir_volume *)(enp + 1);
3599 		struct mpii_device		*dev;
3600 #if NBIO > 0
3601 		const char *vol_states[] = {
3602 			BIOC_SVINVALID_S,
3603 			BIOC_SVOFFLINE_S,
3604 			BIOC_SVBUILDING_S,
3605 			BIOC_SVONLINE_S,
3606 			BIOC_SVDEGRADED_S,
3607 			BIOC_SVONLINE_S,
3608 		};
3609 #endif
3610 
3611 		if (cold)
3612 			break;
3613 		if (!(dev = mpii_find_dev(sc, le16toh(evd->vol_dev_handle))))
3614 			break;
3615 #if NBIO > 0
3616 		if (evd->reason_code == MPII_EVENT_IR_VOL_RC_STATE_CHANGED)
3617 			printf("%s: volume %d state changed from %s to %s\n",
3618 			    DEVNAME(sc), dev->slot - sc->sc_vd_id_low,
3619 			    vol_states[evd->prev_value],
3620 			    vol_states[evd->new_value]);
3621 #endif
3622 		if (evd->reason_code == MPII_EVENT_IR_VOL_RC_STATUS_CHANGED &&
3623 		    ISSET(evd->new_value, MPII_CFG_RAID_VOL_0_STATUS_RESYNC) &&
3624 		    !ISSET(evd->prev_value, MPII_CFG_RAID_VOL_0_STATUS_RESYNC))
3625 			printf("%s: started resync on a volume %d\n",
3626 			    DEVNAME(sc), dev->slot - sc->sc_vd_id_low);
3627 		}
3628 		break;
3629 	case MPII_EVENT_IR_PHYSICAL_DISK:
3630 		break;
3631 	case MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST:
3632 		mpii_event_raid(sc, enp);
3633 		break;
3634 	case MPII_EVENT_IR_OPERATION_STATUS: {
3635 		struct mpii_evt_ir_status	*evs =
3636 		    (struct mpii_evt_ir_status *)(enp + 1);
3637 		struct mpii_device		*dev;
3638 
3639 		if (!(dev = mpii_find_dev(sc, le16toh(evs->vol_dev_handle))))
3640 			break;
3641 		if (evs->operation == MPII_EVENT_IR_RAIDOP_RESYNC)
3642 			dev->percent = evs->percent;
3643 		break;
3644 		}
3645 	default:
3646 		DNPRINTF(MPII_D_EVT, "%s:  unhandled event 0x%02x\n",
3647 		    DEVNAME(sc), le32toh(enp->event));
3648 	}
3649 
3650 	if (enp->ack_required)
3651 		workqueue_enqueue(sc->sc_ssb_evt_ackwk, &rcb->u.rcb_wk, NULL);
3652 	else
3653 		mpii_push_reply(sc, rcb);
3654 }
3655 
3656 static void
3657 mpii_event_defer(void *xsc, void *arg)
3658 {
3659 	struct mpii_softc	*sc = xsc;
3660 	struct mpii_device	*dev = arg;
3661 
3662 	if (ISSET(dev->flags, MPII_DF_DETACH)) {
3663 		mpii_sas_remove_device(sc, dev->dev_handle);
3664 #if 0
3665 		if (!ISSET(dev->flags, MPII_DF_HIDDEN)) {
3666 			scsi_detach_target(sc->sc_scsibus, dev->slot,
3667 			    DETACH_FORCE);
3668 		}
3669 #endif /* XXX */
3670 		free(dev, M_DEVBUF);
3671 
3672 	} else if (ISSET(dev->flags, MPII_DF_ATTACH)) {
3673 		CLR(dev->flags, MPII_DF_ATTACH);
3674 #if 0
3675 		if (!ISSET(dev->flags, MPII_DF_HIDDEN))
3676 			scsi_probe_target(sc->sc_scsibus, dev->slot);
3677 #endif /* XXX */
3678 	}
3679 }
3680 
3681 static void
3682 mpii_sas_remove_device(struct mpii_softc *sc, u_int16_t handle)
3683 {
3684  	struct mpii_msg_scsi_task_request	*stq;
3685 	struct mpii_msg_sas_oper_request	*soq;
3686 	struct mpii_ccb				*ccb;
3687 
3688 	ccb = mpii_get_ccb(sc, 0);
3689 	if (ccb == NULL)
3690 		return;
3691 
3692 	stq = ccb->ccb_cmd;
3693 	stq->function = MPII_FUNCTION_SCSI_TASK_MGMT;
3694 	stq->task_type = MPII_SCSI_TASK_TARGET_RESET;
3695 	stq->dev_handle = htole16(handle);
3696 
3697 	ccb->ccb_done = mpii_empty_done;
3698 	mpii_wait(sc, ccb);
3699 
3700 	if (ccb->ccb_rcb != NULL)
3701 		mpii_push_reply(sc, ccb->ccb_rcb);
3702 
3703 	/* reuse a ccb */
3704 	ccb->ccb_state = MPII_CCB_READY;
3705 	ccb->ccb_rcb = NULL;
3706 
3707 	soq = ccb->ccb_cmd;
3708 	bzero(soq, sizeof(*soq));
3709 	soq->function = MPII_FUNCTION_SAS_IO_UNIT_CONTROL;
3710 	soq->operation = MPII_SAS_OP_REMOVE_DEVICE;
3711 	soq->dev_handle = htole16(handle);
3712 
3713 	ccb->ccb_done = mpii_empty_done;
3714 	mpii_wait(sc, ccb);
3715 	if (ccb->ccb_rcb != NULL)
3716 		mpii_push_reply(sc, ccb->ccb_rcb);
3717 }
3718 
3719 static int
3720 mpii_get_ioc_pg8(struct mpii_softc *sc)
3721 {
3722 	struct mpii_cfg_hdr	hdr;
3723 	struct mpii_cfg_ioc_pg8	*page;
3724 	size_t			pagelen;
3725 	u_int16_t		flags;
3726 	int			pad = 0, rv = 0;
3727 
3728 	DNPRINTF(MPII_D_RAID, "%s: mpii_get_ioc_pg8\n", DEVNAME(sc));
3729 
3730 	if (mpii_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_IOC, 8, 0,
3731 	    &hdr) != 0) {
3732 		DNPRINTF(MPII_D_CFG, "%s: mpii_get_ioc_pg8 unable to fetch "
3733 		    "header for IOC page 8\n", DEVNAME(sc));
3734 		return (1);
3735 	}
3736 
3737 	pagelen = hdr.page_length * 4; /* dwords to bytes */
3738 
3739 	page = malloc(pagelen, M_TEMP, M_NOWAIT);
3740 	if (page == NULL) {
3741 		DNPRINTF(MPII_D_CFG, "%s: mpii_get_ioc_pg8 unable to allocate "
3742 		    "space for ioc config page 8\n", DEVNAME(sc));
3743 		return (1);
3744 	}
3745 
3746 	if (mpii_cfg_page(sc, 0, &hdr, 1, page, pagelen) != 0) {
3747 		DNPRINTF(MPII_D_CFG, "%s: mpii_get_raid unable to fetch IOC "
3748 		    "page 8\n", DEVNAME(sc));
3749 		rv = 1;
3750 		goto out;
3751 	}
3752 
3753 	DNPRINTF(MPII_D_CFG, "%s:  numdevsperenclosure: 0x%02x\n", DEVNAME(sc),
3754 	    page->num_devs_per_enclosure);
3755 	DNPRINTF(MPII_D_CFG, "%s:  maxpersistententries: 0x%04x "
3756 	    "maxnumphysicalmappedids: 0x%04x\n", DEVNAME(sc),
3757 	    le16toh(page->max_persistent_entries),
3758 	    le16toh(page->max_num_physical_mapped_ids));
3759 	DNPRINTF(MPII_D_CFG, "%s:  flags: 0x%04x\n", DEVNAME(sc),
3760 	    le16toh(page->flags));
3761 	DNPRINTF(MPII_D_CFG, "%s:  irvolumemappingflags: 0x%04x\n",
3762 	    DEVNAME(sc), le16toh(page->ir_volume_mapping_flags));
3763 
3764 	if (page->flags & MPII_IOC_PG8_FLAGS_RESERVED_TARGETID_0)
3765 		pad = 1;
3766 
3767 	flags = page->ir_volume_mapping_flags &
3768 	    MPII_IOC_PG8_IRFLAGS_VOLUME_MAPPING_MODE_MASK;
3769 	if (ISSET(sc->sc_flags, MPII_F_RAID)) {
3770 		if (flags == MPII_IOC_PG8_IRFLAGS_LOW_VOLUME_MAPPING) {
3771 			sc->sc_vd_id_low += pad;
3772 			pad = sc->sc_max_volumes; /* for sc_pd_id_start */
3773 		} else
3774 			sc->sc_vd_id_low = sc->sc_max_devices -
3775 			    sc->sc_max_volumes;
3776 	}
3777 
3778 	sc->sc_pd_id_start += pad;
3779 
3780 	DNPRINTF(MPII_D_MAP, "%s: mpii_get_ioc_pg8 mapping: sc_pd_id_start: %d "
3781 	    "sc_vd_id_low: %d sc_max_volumes: %d\n", DEVNAME(sc),
3782 	    sc->sc_pd_id_start, sc->sc_vd_id_low, sc->sc_max_volumes);
3783 
3784 out:
3785 	free(page, M_TEMP);
3786 
3787 	return(rv);
3788 }
3789 
3790 static int
3791 mpii_req_cfg_header(struct mpii_softc *sc, u_int8_t type, u_int8_t number,
3792     u_int32_t address, int flags, void *p)
3793 {
3794 	struct mpii_msg_config_request		*cq;
3795 	struct mpii_msg_config_reply		*cp;
3796 	struct mpii_cfg_hdr	*hdr = p;
3797 	struct mpii_ccb		*ccb;
3798 	struct mpii_ecfg_hdr	*ehdr = p;
3799 	int			etype = 0;
3800 	int			rv = 0;
3801 
3802 	DNPRINTF(MPII_D_MISC, "%s: mpii_req_cfg_header type: %#x number: %x "
3803 	    "address: 0x%08x flags: 0x%x\n", DEVNAME(sc), type, number,
3804 	    address, flags);
3805 
3806 	ccb = mpii_get_ccb(sc, ISSET(flags, MPII_PG_POLL) ? MPII_NOSLEEP : 0);
3807 	if (ccb == NULL) {
3808 		DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header ccb_get\n",
3809 		    DEVNAME(sc));
3810 		return (1);
3811 	}
3812 
3813 	if (ISSET(flags, MPII_PG_EXTENDED)) {
3814 		etype = type;
3815 		type = MPII_CONFIG_REQ_PAGE_TYPE_EXTENDED;
3816 	}
3817 
3818 	cq = ccb->ccb_cmd;
3819 
3820 	cq->function = MPII_FUNCTION_CONFIG;
3821 
3822 	cq->action = MPII_CONFIG_REQ_ACTION_PAGE_HEADER;
3823 
3824 	cq->config_header.page_number = number;
3825 	cq->config_header.page_type = type;
3826 	cq->ext_page_type = etype;
3827 	cq->page_address = htole32(address);
3828 	cq->page_buffer.sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
3829 	    MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL);
3830 
3831 	ccb->ccb_done = mpii_empty_done;
3832 	if (ISSET(flags, MPII_PG_POLL)) {
3833 		if (mpii_poll(sc, ccb) != 0) {
3834 			DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header poll\n",
3835 			    DEVNAME(sc));
3836 			return (1);
3837 		}
3838 	} else
3839 		mpii_wait(sc, ccb);
3840 
3841 	if (ccb->ccb_rcb == NULL) {
3842 		mpii_put_ccb(sc, ccb);
3843 		return (1);
3844 	}
3845 	cp = ccb->ccb_rcb->rcb_reply;
3846 
3847 	DNPRINTF(MPII_D_MISC, "%s:  action: 0x%02x sgl_flags: 0x%02x "
3848 	    "msg_length: %d function: 0x%02x\n", DEVNAME(sc), cp->action,
3849 	    cp->sgl_flags, cp->msg_length, cp->function);
3850 	DNPRINTF(MPII_D_MISC, "%s:  ext_page_length: %d ext_page_type: 0x%02x "
3851 	    "msg_flags: 0x%02x\n", DEVNAME(sc),
3852 	    le16toh(cp->ext_page_length), cp->ext_page_type,
3853 	    cp->msg_flags);
3854 	DNPRINTF(MPII_D_MISC, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
3855 	    cp->vp_id, cp->vf_id);
3856 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
3857 	    le16toh(cp->ioc_status));
3858 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
3859 	    le32toh(cp->ioc_loginfo));
3860 	DNPRINTF(MPII_D_MISC, "%s:  page_version: 0x%02x page_length: %d "
3861 	    "page_number: 0x%02x page_type: 0x%02x\n", DEVNAME(sc),
3862 	    cp->config_header.page_version,
3863 	    cp->config_header.page_length,
3864 	    cp->config_header.page_number,
3865 	    cp->config_header.page_type);
3866 
3867 	if (le16toh(cp->ioc_status) != MPII_IOCSTATUS_SUCCESS)
3868 		rv = 1;
3869 	else if (ISSET(flags, MPII_PG_EXTENDED)) {
3870 		bzero(ehdr, sizeof(*ehdr));
3871 		ehdr->page_version = cp->config_header.page_version;
3872 		ehdr->page_number = cp->config_header.page_number;
3873 		ehdr->page_type = cp->config_header.page_type;
3874 		ehdr->ext_page_length = cp->ext_page_length;
3875 		ehdr->ext_page_type = cp->ext_page_type;
3876 	} else
3877 		*hdr = cp->config_header;
3878 
3879 	mpii_push_reply(sc, ccb->ccb_rcb);
3880 	mpii_put_ccb(sc, ccb);
3881 
3882 	return (rv);
3883 }
3884 
3885 static int
3886 mpii_req_cfg_page(struct mpii_softc *sc, u_int32_t address, int flags,
3887     void *p, int read, void *page, size_t len)
3888 {
3889 	struct mpii_msg_config_request		*cq;
3890 	struct mpii_msg_config_reply		*cp;
3891 	struct mpii_cfg_hdr	*hdr = p;
3892 	struct mpii_ccb		*ccb;
3893 	struct mpii_ecfg_hdr	*ehdr = p;
3894 	u_int64_t		dva;
3895 	char			*kva;
3896 	int			page_length;
3897 	int			rv = 0;
3898 
3899 	DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_page address: %d read: %d "
3900 	    "type: %x\n", DEVNAME(sc), address, read, hdr->page_type);
3901 
3902 	page_length = ISSET(flags, MPII_PG_EXTENDED) ?
3903 	    le16toh(ehdr->ext_page_length) : hdr->page_length;
3904 
3905 	if (len > MPII_REQUEST_SIZE - sizeof(struct mpii_msg_config_request) ||
3906     	    len < page_length * 4)
3907 		return (1);
3908 
3909 	ccb = mpii_get_ccb(sc,
3910 	    ISSET(flags, MPII_PG_POLL) ? MPII_NOSLEEP : 0);
3911 	if (ccb == NULL) {
3912 		DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_page ccb_get\n",
3913 		    DEVNAME(sc));
3914 		return (1);
3915 	}
3916 
3917 	cq = ccb->ccb_cmd;
3918 
3919 	cq->function = MPII_FUNCTION_CONFIG;
3920 
3921 	cq->action = (read ? MPII_CONFIG_REQ_ACTION_PAGE_READ_CURRENT :
3922 	    MPII_CONFIG_REQ_ACTION_PAGE_WRITE_CURRENT);
3923 
3924 	if (ISSET(flags, MPII_PG_EXTENDED)) {
3925 		cq->config_header.page_version = ehdr->page_version;
3926 		cq->config_header.page_number = ehdr->page_number;
3927 		cq->config_header.page_type = ehdr->page_type;
3928 		cq->ext_page_len = ehdr->ext_page_length;
3929 		cq->ext_page_type = ehdr->ext_page_type;
3930 	} else
3931 		cq->config_header = *hdr;
3932 	cq->config_header.page_type &= MPII_CONFIG_REQ_PAGE_TYPE_MASK;
3933 	cq->page_address = htole32(address);
3934 	cq->page_buffer.sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
3935 	    MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL |
3936 	    MPII_SGE_FL_SIZE_64 | (page_length * 4) |
3937 	    (read ? MPII_SGE_FL_DIR_IN : MPII_SGE_FL_DIR_OUT));
3938 
3939 	/* bounce the page via the request space to avoid more bus_dma games */
3940 	dva = ccb->ccb_cmd_dva + sizeof(struct mpii_msg_config_request);
3941 
3942 	cq->page_buffer.sg_hi_addr = htole32((u_int32_t)(dva >> 32));
3943 	cq->page_buffer.sg_lo_addr = htole32((u_int32_t)dva);
3944 
3945 	kva = ccb->ccb_cmd;
3946 	kva += sizeof(struct mpii_msg_config_request);
3947 
3948 	if (!read)
3949 		bcopy(page, kva, len);
3950 
3951 	ccb->ccb_done = mpii_empty_done;
3952 	if (ISSET(flags, MPII_PG_POLL)) {
3953 		if (mpii_poll(sc, ccb) != 0) {
3954 			DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header poll\n",
3955 			    DEVNAME(sc));
3956 			return (1);
3957 		}
3958 	} else
3959 		mpii_wait(sc, ccb);
3960 
3961 	if (ccb->ccb_rcb == NULL) {
3962 		mpii_put_ccb(sc, ccb);
3963 		return (1);
3964 	}
3965 	cp = ccb->ccb_rcb->rcb_reply;
3966 
3967 	DNPRINTF(MPII_D_MISC, "%s:  action: 0x%02x "
3968 	    "msg_length: %d function: 0x%02x\n", DEVNAME(sc), cp->action,
3969 	    cp->msg_length, cp->function);
3970 	DNPRINTF(MPII_D_MISC, "%s:  ext_page_length: %d ext_page_type: 0x%02x "
3971 	    "msg_flags: 0x%02x\n", DEVNAME(sc),
3972 	    le16toh(cp->ext_page_length), cp->ext_page_type,
3973 	    cp->msg_flags);
3974 	DNPRINTF(MPII_D_MISC, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
3975 	    cp->vp_id, cp->vf_id);
3976 	DNPRINTF(MPII_D_MISC, "%s:  ioc_status: 0x%04x\n", DEVNAME(sc),
3977 	    le16toh(cp->ioc_status));
3978 	DNPRINTF(MPII_D_MISC, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
3979 	    le32toh(cp->ioc_loginfo));
3980 	DNPRINTF(MPII_D_MISC, "%s:  page_version: 0x%02x page_length: %d "
3981 	    "page_number: 0x%02x page_type: 0x%02x\n", DEVNAME(sc),
3982 	    cp->config_header.page_version,
3983 	    cp->config_header.page_length,
3984 	    cp->config_header.page_number,
3985 	    cp->config_header.page_type);
3986 
3987 	if (le16toh(cp->ioc_status) != MPII_IOCSTATUS_SUCCESS)
3988 		rv = 1;
3989 	else if (read)
3990 		bcopy(kva, page, len);
3991 
3992 	mpii_push_reply(sc, ccb->ccb_rcb);
3993 	mpii_put_ccb(sc, ccb);
3994 
3995 	return (rv);
3996 }
3997 
3998 static struct mpii_rcb *
3999 mpii_reply(struct mpii_softc *sc, struct mpii_reply_descr *rdp)
4000 {
4001 	struct mpii_rcb		*rcb = NULL;
4002 	u_int32_t		rfid;
4003 
4004 	DNPRINTF(MPII_D_INTR, "%s: mpii_reply\n", DEVNAME(sc));
4005 
4006 	if ((rdp->reply_flags & MPII_REPLY_DESCR_TYPE_MASK) ==
4007 	    MPII_REPLY_DESCR_ADDRESS_REPLY) {
4008 		rfid = (le32toh(rdp->frame_addr) -
4009 		    (u_int32_t)MPII_DMA_DVA(sc->sc_replies)) / MPII_REPLY_SIZE;
4010 
4011 		bus_dmamap_sync(sc->sc_dmat,
4012 		    MPII_DMA_MAP(sc->sc_replies), MPII_REPLY_SIZE * rfid,
4013 		    MPII_REPLY_SIZE, BUS_DMASYNC_POSTREAD);
4014 
4015 		rcb = &sc->sc_rcbs[rfid];
4016 	}
4017 
4018 	memset(rdp, 0xff, sizeof(*rdp));
4019 
4020 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
4021 	    8 * sc->sc_reply_post_host_index, 8,
4022 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4023 
4024 	return (rcb);
4025 }
4026 
4027 static struct mpii_dmamem *
4028 mpii_dmamem_alloc(struct mpii_softc *sc, size_t size)
4029 {
4030 	struct mpii_dmamem	*mdm;
4031 	int			nsegs;
4032 
4033 	mdm = malloc(sizeof(*mdm), M_DEVBUF, M_NOWAIT | M_ZERO);
4034 	if (mdm == NULL)
4035 	return (NULL);
4036 
4037 	mdm->mdm_size = size;
4038 
4039 	if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
4040 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &mdm->mdm_map) != 0)
4041 		goto mdmfree;
4042 
4043 	if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &mdm->mdm_seg,
4044 	    1, &nsegs, BUS_DMA_NOWAIT) != 0) goto destroy;
4045 
4046 	if (bus_dmamem_map(sc->sc_dmat, &mdm->mdm_seg, nsegs, size,
4047 	    &mdm->mdm_kva, BUS_DMA_NOWAIT) != 0)
4048 		goto free;
4049 
4050 	if (bus_dmamap_load(sc->sc_dmat, mdm->mdm_map, mdm->mdm_kva, size,
4051 	    NULL, BUS_DMA_NOWAIT) != 0)
4052 		goto unmap;
4053 
4054 	DNPRINTF(MPII_D_MEM,
4055 	    "  kva: %p  dva: 0x%" PRIx64 "  map: %p  size: %" PRId64 "\n",
4056 	    mdm->mdm_kva, (uint64_t)mdm->mdm_map->dm_segs[0].ds_addr,
4057 	    mdm->mdm_map, (uint64_t)size);
4058 
4059 	bzero(mdm->mdm_kva, size);
4060 
4061 	return (mdm);
4062 
4063 unmap:
4064 	bus_dmamem_unmap(sc->sc_dmat, mdm->mdm_kva, size);
4065 free:
4066 	bus_dmamem_free(sc->sc_dmat, &mdm->mdm_seg, 1);
4067 destroy:
4068 	bus_dmamap_destroy(sc->sc_dmat, mdm->mdm_map);
4069 mdmfree:
4070 	free(mdm, M_DEVBUF);
4071 
4072 	return (NULL);
4073 }
4074 
4075 static void
4076 mpii_dmamem_free(struct mpii_softc *sc, struct mpii_dmamem *mdm)
4077 {
4078 	DNPRINTF(MPII_D_MEM, "%s: mpii_dmamem_free %p\n", DEVNAME(sc), mdm);
4079 
4080 	bus_dmamap_unload(sc->sc_dmat, mdm->mdm_map);
4081 	bus_dmamem_unmap(sc->sc_dmat, mdm->mdm_kva, mdm->mdm_size);
4082 	bus_dmamem_free(sc->sc_dmat, &mdm->mdm_seg, 1);
4083 	bus_dmamap_destroy(sc->sc_dmat, mdm->mdm_map);
4084 	free(mdm, M_DEVBUF);
4085 }
4086 
4087 static int
4088 mpii_alloc_dev(struct mpii_softc *sc)
4089 {
4090 	sc->sc_devs = malloc(sc->sc_max_devices *
4091 	    sizeof(struct mpii_device *), M_DEVBUF, M_NOWAIT | M_ZERO);
4092 	if (sc->sc_devs == NULL)
4093 		return (1);
4094 	return (0);
4095 }
4096 
4097 static int
4098 mpii_insert_dev(struct mpii_softc *sc, struct mpii_device *dev)
4099 {
4100 	int slot = dev->slot; 	/* initial hint */
4101 
4102 	if (!dev || slot < 0)
4103 		return (1);
4104 	while (slot < sc->sc_max_devices && sc->sc_devs[slot] != NULL)
4105 		slot++;
4106 	if (slot >= sc->sc_max_devices)
4107 		return (1);
4108 	dev->slot = slot;
4109 	sc->sc_devs[slot] = dev;
4110 	return (0);
4111 }
4112 
4113 static int
4114 mpii_remove_dev(struct mpii_softc *sc, struct mpii_device *dev)
4115 {
4116 	int			i;
4117 
4118 	if (!dev)
4119 		return (1);
4120 	for (i = 0; i < sc->sc_max_devices;  i++)
4121 		if (sc->sc_devs[i] &&
4122 		    sc->sc_devs[i]->dev_handle == dev->dev_handle) {
4123 			sc->sc_devs[i] = NULL;
4124 			return (0);
4125 		}
4126 	return (1);
4127 }
4128 
4129 static struct mpii_device *
4130 mpii_find_dev(struct mpii_softc *sc, u_int16_t handle)
4131 {
4132 	int			i;
4133 
4134 	for (i = 0; i < sc->sc_max_devices;  i++)
4135 		if (sc->sc_devs[i] && sc->sc_devs[i]->dev_handle == handle)
4136 			return (sc->sc_devs[i]);
4137 	return (NULL);
4138 }
4139 
4140 static int
4141 mpii_alloc_ccbs(struct mpii_softc *sc)
4142 {
4143 	struct mpii_ccb		*ccb;
4144 	u_int8_t		*cmd;
4145 	int			i;
4146 
4147 	SIMPLEQ_INIT(&sc->sc_ccb_free);
4148 
4149 	sc->sc_ccbs = malloc(sizeof(*ccb) * (sc->sc_request_depth-1),
4150 	    M_DEVBUF, M_NOWAIT | M_ZERO);
4151 	if (sc->sc_ccbs == NULL) {
4152 		printf("%s: unable to allocate ccbs\n", DEVNAME(sc));
4153 		return (1);
4154 	}
4155 
4156 	sc->sc_requests = mpii_dmamem_alloc(sc,
4157 	    MPII_REQUEST_SIZE * sc->sc_request_depth);
4158 	if (sc->sc_requests == NULL) {
4159 		printf("%s: unable to allocate ccb dmamem\n", DEVNAME(sc));
4160 		goto free_ccbs;
4161 	}
4162 	cmd = MPII_DMA_KVA(sc->sc_requests);
4163 	bzero(cmd, MPII_REQUEST_SIZE * sc->sc_request_depth);
4164 
4165 	/*
4166 	 * we have sc->sc_request_depth system request message
4167 	 * frames, but smid zero cannot be used. so we then
4168 	 * have (sc->sc_request_depth - 1) number of ccbs
4169 	 */
4170 	for (i = 1; i < sc->sc_request_depth; i++) {
4171 		ccb = &sc->sc_ccbs[i - 1];
4172 
4173 		if (bus_dmamap_create(sc->sc_dmat, MAXPHYS,
4174 		    sc->sc_max_sgl_len, MAXPHYS, 0,
4175 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
4176 		    &ccb->ccb_dmamap) != 0) {
4177 			printf("%s: unable to create dma map\n", DEVNAME(sc));
4178 			goto free_maps;
4179 		}
4180 
4181 		ccb->ccb_sc = sc;
4182 		ccb->ccb_smid = i;
4183 		ccb->ccb_offset = MPII_REQUEST_SIZE * i;
4184 
4185 		ccb->ccb_cmd = &cmd[ccb->ccb_offset];
4186 		ccb->ccb_cmd_dva = (u_int32_t)MPII_DMA_DVA(sc->sc_requests) +
4187 		    ccb->ccb_offset;
4188 
4189 		DNPRINTF(MPII_D_CCB, "%s: mpii_alloc_ccbs(%d) ccb: %p map: %p "
4190 		    "sc: %p smid: %#x offs: %#" PRIx64 " cmd: %#" PRIx64 " dva: %#" PRIx64 "\n",
4191 		    DEVNAME(sc), i, ccb, ccb->ccb_dmamap, ccb->ccb_sc,
4192 		    ccb->ccb_smid, (uint64_t)ccb->ccb_offset,
4193 		    (uint64_t)ccb->ccb_cmd, (uint64_t)ccb->ccb_cmd_dva);
4194 
4195 		mpii_put_ccb(sc, ccb);
4196 	}
4197 
4198 	return (0);
4199 
4200 free_maps:
4201 	while ((ccb = mpii_get_ccb(sc, MPII_NOSLEEP)) != NULL)
4202 		bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
4203 
4204 	mpii_dmamem_free(sc, sc->sc_requests);
4205 free_ccbs:
4206 	free(sc->sc_ccbs, M_DEVBUF);
4207 
4208 	return (1);
4209 }
4210 
4211 static void
4212 mpii_put_ccb(struct mpii_softc *sc, struct mpii_ccb *ccb)
4213 {
4214 	KASSERT(ccb->ccb_sc == sc);
4215 	DNPRINTF(MPII_D_CCB, "%s: mpii_put_ccb %p\n", DEVNAME(sc), ccb);
4216 
4217 	ccb->ccb_state = MPII_CCB_FREE;
4218 	ccb->ccb_cookie = NULL;
4219 	ccb->ccb_done = NULL;
4220 	ccb->ccb_rcb = NULL;
4221 	bzero(ccb->ccb_cmd, MPII_REQUEST_SIZE);
4222 
4223 	mutex_enter(&sc->sc_ccb_free_mtx);
4224 	SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, u.ccb_link);
4225 	cv_signal(&sc->sc_ccb_free_cv);
4226 	mutex_exit(&sc->sc_ccb_free_mtx);
4227 }
4228 
4229 static struct mpii_ccb *
4230 mpii_get_ccb(struct mpii_softc *sc, int flags)
4231 {
4232 	struct mpii_ccb		*ccb;
4233 
4234 	mutex_enter(&sc->sc_ccb_free_mtx);
4235 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) == NULL) {
4236 		if (flags & MPII_NOSLEEP)
4237 			break;
4238 		cv_wait(&sc->sc_ccb_free_cv, &sc->sc_ccb_free_mtx);
4239 	}
4240 
4241 	if (ccb != NULL) {
4242 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, u.ccb_link);
4243 		ccb->ccb_state = MPII_CCB_READY;
4244 		KASSERT(ccb->ccb_sc == sc);
4245 	}
4246 	mutex_exit(&sc->sc_ccb_free_mtx);
4247 
4248 	DNPRINTF(MPII_D_CCB, "%s: mpii_get_ccb %p\n", DEVNAME(sc), ccb);
4249 
4250 	return (ccb);
4251 }
4252 
4253 static int
4254 mpii_alloc_replies(struct mpii_softc *sc)
4255 {
4256 	DNPRINTF(MPII_D_MISC, "%s: mpii_alloc_replies\n", DEVNAME(sc));
4257 
4258 	sc->sc_rcbs = malloc(sc->sc_num_reply_frames * sizeof(struct mpii_rcb),
4259 	    M_DEVBUF, M_NOWAIT);
4260 	if (sc->sc_rcbs == NULL)
4261 		return (1);
4262 
4263 	sc->sc_replies = mpii_dmamem_alloc(sc, MPII_REPLY_SIZE *
4264 	    sc->sc_num_reply_frames);
4265 	if (sc->sc_replies == NULL) {
4266 		free(sc->sc_rcbs, M_DEVBUF);
4267 		return (1);
4268 	}
4269 
4270 	return (0);
4271 }
4272 
4273 static void
4274 mpii_push_replies(struct mpii_softc *sc)
4275 {
4276 	struct mpii_rcb		*rcb;
4277 	char			*kva = MPII_DMA_KVA(sc->sc_replies);
4278 	int			i;
4279 
4280 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
4281 	    0, MPII_REPLY_SIZE * sc->sc_num_reply_frames, BUS_DMASYNC_PREREAD);
4282 
4283 	for (i = 0; i < sc->sc_num_reply_frames; i++) {
4284 		rcb = &sc->sc_rcbs[i];
4285 
4286 		rcb->rcb_reply = kva + MPII_REPLY_SIZE * i;
4287 		rcb->rcb_reply_dva = (u_int32_t)MPII_DMA_DVA(sc->sc_replies) +
4288 		    MPII_REPLY_SIZE * i;
4289 		mpii_push_reply(sc, rcb);
4290 	}
4291 }
4292 
4293 static void
4294 mpii_start(struct mpii_softc *sc, struct mpii_ccb *ccb)
4295 {
4296 	struct mpii_request_header	*rhp;
4297 	struct mpii_request_descr	descr;
4298 	u_int32_t			*rdp = (u_int32_t *)&descr;
4299 
4300 	DNPRINTF(MPII_D_RW, "%s: mpii_start %#" PRIx64 "\n", DEVNAME(sc),
4301 	    (uint64_t)ccb->ccb_cmd_dva);
4302 
4303 	rhp = ccb->ccb_cmd;
4304 
4305 	bzero(&descr, sizeof(descr));
4306 
4307 	switch (rhp->function) {
4308 	case MPII_FUNCTION_SCSI_IO_REQUEST:
4309 		descr.request_flags = MPII_REQ_DESCR_SCSI_IO;
4310 		descr.dev_handle = htole16(ccb->ccb_dev_handle);
4311 		break;
4312 	case MPII_FUNCTION_SCSI_TASK_MGMT:
4313 		descr.request_flags = MPII_REQ_DESCR_HIGH_PRIORITY;
4314 		break;
4315 	default:
4316 		descr.request_flags = MPII_REQ_DESCR_DEFAULT;
4317 	}
4318 
4319 	descr.vf_id = sc->sc_vf_id;
4320 	descr.smid = htole16(ccb->ccb_smid);
4321 
4322 	bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_requests),
4323 	    ccb->ccb_offset, MPII_REQUEST_SIZE,
4324 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4325 
4326 	ccb->ccb_state = MPII_CCB_QUEUED;
4327 
4328 	DNPRINTF(MPII_D_RW, "%s:   MPII_REQ_DESCR_POST_LOW (0x%08x) write "
4329 	    "0x%08x\n", DEVNAME(sc), MPII_REQ_DESCR_POST_LOW, *rdp);
4330 
4331 	DNPRINTF(MPII_D_RW, "%s:   MPII_REQ_DESCR_POST_HIGH (0x%08x) write "
4332 	    "0x%08x\n", DEVNAME(sc), MPII_REQ_DESCR_POST_HIGH, *(rdp+1));
4333 
4334 	mutex_enter(&sc->sc_req_mtx);
4335 	mpii_write(sc, MPII_REQ_DESCR_POST_LOW, htole32(*rdp));
4336 	mpii_write(sc, MPII_REQ_DESCR_POST_HIGH, htole32(*(rdp+1)));
4337 	mutex_exit(&sc->sc_req_mtx);
4338 }
4339 
4340 static int
4341 mpii_poll(struct mpii_softc *sc, struct mpii_ccb *ccb)
4342 {
4343 	void				(*done)(struct mpii_ccb *);
4344 	void				*cookie;
4345 	int				rv = 1;
4346 
4347 	DNPRINTF(MPII_D_INTR, "%s: mpii_complete\n", DEVNAME(sc));
4348 
4349 	done = ccb->ccb_done;
4350 	cookie = ccb->ccb_cookie;
4351 
4352 	ccb->ccb_done = mpii_poll_done;
4353 	ccb->ccb_cookie = &rv;
4354 
4355 	mpii_start(sc, ccb);
4356 
4357 	while (rv == 1) {
4358 		/* avoid excessive polling */
4359 		if (mpii_reply_waiting(sc))
4360 			mpii_intr(sc);
4361 		else
4362 			delay(10);
4363 	}
4364 
4365 	ccb->ccb_cookie = cookie;
4366 	done(ccb);
4367 
4368 	return (0);
4369 }
4370 
4371 static void
4372 mpii_poll_done(struct mpii_ccb *ccb)
4373 {
4374 	int				*rv = ccb->ccb_cookie;
4375 
4376 	*rv = 0;
4377 }
4378 
4379 static int
4380 mpii_alloc_queues(struct mpii_softc *sc)
4381 {
4382 	u_int32_t		*kva;
4383 	u_int64_t		*kva64;
4384 	int			i;
4385 
4386 	DNPRINTF(MPII_D_MISC, "%s: mpii_alloc_queues\n", DEVNAME(sc));
4387 
4388 	sc->sc_reply_freeq = mpii_dmamem_alloc(sc,
4389 	    sc->sc_reply_free_qdepth * 4);
4390 	if (sc->sc_reply_freeq == NULL)
4391 		return (1);
4392 
4393 	kva = MPII_DMA_KVA(sc->sc_reply_freeq);
4394 	for (i = 0; i < sc->sc_num_reply_frames; i++) {
4395 		kva[i] = (u_int32_t)MPII_DMA_DVA(sc->sc_replies) +
4396 		    MPII_REPLY_SIZE * i;
4397 
4398 		DNPRINTF(MPII_D_MISC, "%s:   %d:  %p = 0x%08x\n",
4399 		    DEVNAME(sc), i,
4400 		    &kva[i], (u_int)MPII_DMA_DVA(sc->sc_replies) +
4401 		    MPII_REPLY_SIZE * i);
4402 	}
4403 
4404 	sc->sc_reply_postq =
4405 	    mpii_dmamem_alloc(sc, sc->sc_reply_post_qdepth * 8);
4406 	if (sc->sc_reply_postq == NULL)
4407 		goto free_reply_freeq;
4408 	sc->sc_reply_postq_kva = MPII_DMA_KVA(sc->sc_reply_postq);
4409 
4410 	DNPRINTF(MPII_D_MISC, "%s:  populating reply post descriptor queue\n",
4411 	    DEVNAME(sc));
4412 	kva64 = (u_int64_t *)MPII_DMA_KVA(sc->sc_reply_postq);
4413 	for (i = 0; i < sc->sc_reply_post_qdepth; i++) {
4414 		kva64[i] = 0xffffffffffffffffllu;
4415 		DNPRINTF(MPII_D_MISC, "%s:    %d:  %p = 0x%" PRIx64 "\n",
4416 		    DEVNAME(sc), i, &kva64[i], kva64[i]);
4417 	}
4418 
4419 	return (0);
4420 
4421 free_reply_freeq:
4422 
4423 	mpii_dmamem_free(sc, sc->sc_reply_freeq);
4424 	return (1);
4425 }
4426 
4427 static void
4428 mpii_init_queues(struct mpii_softc *sc)
4429 {
4430 	DNPRINTF(MPII_D_MISC, "%s:  mpii_init_queues\n", DEVNAME(sc));
4431 
4432 	sc->sc_reply_free_host_index = sc->sc_reply_free_qdepth - 1;
4433 	sc->sc_reply_post_host_index = 0;
4434 	mpii_write_reply_free(sc, sc->sc_reply_free_host_index);
4435 	mpii_write_reply_post(sc, sc->sc_reply_post_host_index);
4436 }
4437 
4438 static void
4439 mpii_wait(struct mpii_softc *sc, struct mpii_ccb *ccb)
4440 {
4441 	struct mpii_ccb_wait	mpii_ccb_wait;
4442 	void			(*done)(struct mpii_ccb *);
4443 	void			*cookie;
4444 
4445 	done = ccb->ccb_done;
4446 	cookie = ccb->ccb_cookie;
4447 
4448 	ccb->ccb_done = mpii_wait_done;
4449 	ccb->ccb_cookie = &mpii_ccb_wait;
4450 
4451 	mutex_init(&mpii_ccb_wait.mpii_ccbw_mtx, MUTEX_DEFAULT, IPL_BIO);
4452 	cv_init(&mpii_ccb_wait.mpii_ccbw_cv, "mpii_wait");
4453 
4454 	/* XXX this will wait forever for the ccb to complete */
4455 
4456 	mpii_start(sc, ccb);
4457 
4458 	mutex_enter(&mpii_ccb_wait.mpii_ccbw_mtx);
4459 	while (ccb->ccb_cookie != NULL) {
4460 		cv_wait(&mpii_ccb_wait.mpii_ccbw_cv,
4461 		    &mpii_ccb_wait.mpii_ccbw_mtx);
4462 	}
4463 	mutex_exit(&mpii_ccb_wait.mpii_ccbw_mtx);
4464 	mutex_destroy(&mpii_ccb_wait.mpii_ccbw_mtx);
4465 	cv_destroy(&mpii_ccb_wait.mpii_ccbw_cv);
4466 
4467 	ccb->ccb_cookie = cookie;
4468 	done(ccb);
4469 }
4470 
4471 static void
4472 mpii_wait_done(struct mpii_ccb *ccb)
4473 {
4474 	struct mpii_ccb_wait	*mpii_ccb_waitp = ccb->ccb_cookie;
4475 
4476 	mutex_enter(&mpii_ccb_waitp->mpii_ccbw_mtx);
4477 	ccb->ccb_cookie = NULL;
4478 	cv_signal(&mpii_ccb_waitp->mpii_ccbw_cv);
4479 	mutex_exit(&mpii_ccb_waitp->mpii_ccbw_mtx);
4480 }
4481 
4482 static void
4483 mpii_minphys(struct buf *bp)
4484 {
4485 	DNPRINTF(MPII_D_MISC, "mpii_minphys: %d\n", bp->b_bcount);
4486 
4487 	/* XXX currently using MPII_MAXFER = MAXPHYS */
4488 	if (bp->b_bcount > MPII_MAXFER) {
4489 		bp->b_bcount = MPII_MAXFER;
4490 		minphys(bp);
4491 	}
4492 }
4493 
4494 static void
4495 mpii_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
4496     void *arg)
4497 {
4498 	struct scsipi_periph	*periph;
4499 	struct scsipi_xfer	*xs;
4500 	struct scsipi_adapter	*adapt = chan->chan_adapter;
4501 	struct mpii_softc	*sc = device_private(adapt->adapt_dev);
4502 	struct mpii_ccb		*ccb;
4503 	struct mpii_ccb_bundle	*mcb;
4504 	struct mpii_msg_scsi_io	*io;
4505 	struct mpii_device	*dev;
4506 	int			target;
4507 	int timeout;
4508 
4509 	DNPRINTF(MPII_D_CMD, "%s: mpii_scsipi_request\n", DEVNAME(sc));
4510 	switch (req) {
4511 	case ADAPTER_REQ_GROW_RESOURCES:
4512 		/* Not supported. */
4513 		return;
4514 	case ADAPTER_REQ_SET_XFER_MODE:
4515 	{
4516 		struct scsipi_xfer_mode *xm = arg;
4517 		xm->xm_mode = PERIPH_CAP_TQING;
4518 		xm->xm_period = 0;
4519 		xm->xm_offset = 0;
4520 		scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, xm);
4521 		return;
4522 	}
4523 	case ADAPTER_REQ_RUN_XFER:
4524 		break;
4525 	}
4526 
4527 	xs = arg;
4528 	periph = xs->xs_periph;
4529 	target = periph->periph_target;
4530 
4531 	if (xs->cmdlen > MPII_CDB_LEN) {
4532 		DNPRINTF(MPII_D_CMD, "%s: CBD too big %d\n",
4533 		    DEVNAME(sc), xs->cmdlen);
4534 		bzero(&xs->sense, sizeof(xs->sense));
4535 		xs->sense.scsi_sense.response_code =
4536 		    SSD_RCODE_VALID | SSD_RCODE_CURRENT;
4537 		xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
4538 		xs->sense.scsi_sense.asc = 0x20;
4539 		xs->error = XS_SENSE;
4540 		scsipi_done(xs);
4541 		return;
4542 	}
4543 
4544 	if ((dev = sc->sc_devs[target]) == NULL) {
4545 		/* device no longer exists */
4546 		xs->error = XS_SELTIMEOUT;
4547 		scsipi_done(xs);
4548 		return;
4549 	}
4550 
4551 	ccb = mpii_get_ccb(sc, MPII_NOSLEEP);
4552 	if (ccb == NULL) {
4553 		xs->error = XS_RESOURCE_SHORTAGE;
4554 		scsipi_done(xs);
4555 		return;
4556 	}
4557 
4558 	DNPRINTF(MPII_D_CMD, "%s: ccb_smid: %d xs->xs_control: 0x%x\n",
4559 	    DEVNAME(sc), ccb->ccb_smid, xs->xs_control);
4560 
4561 	ccb->ccb_cookie = xs;
4562 	ccb->ccb_done = mpii_scsi_cmd_done;
4563 	ccb->ccb_dev_handle = dev->dev_handle;
4564 
4565 	mcb = ccb->ccb_cmd;
4566 	io = &mcb->mcb_io;
4567 
4568 	io->function = MPII_FUNCTION_SCSI_IO_REQUEST;
4569 	io->sense_buffer_length = sizeof(xs->sense);
4570 	io->sgl_offset0 = 24; /* XXX fix this */
4571 	io->io_flags = htole16(xs->cmdlen);
4572 	io->dev_handle = htole16(ccb->ccb_dev_handle);
4573 	io->lun[0] = htobe16(periph->periph_lun);
4574 
4575 	switch (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
4576 	case XS_CTL_DATA_IN:
4577 		io->direction = MPII_SCSIIO_DIR_READ;
4578 		break;
4579 	case XS_CTL_DATA_OUT:
4580 		io->direction = MPII_SCSIIO_DIR_WRITE;
4581 		break;
4582 	default:
4583 		io->direction = MPII_SCSIIO_DIR_NONE;
4584 	}
4585 
4586 	io->tagging = MPII_SCSIIO_ATTR_SIMPLE_Q;
4587 
4588 	memcpy(io->cdb, xs->cmd, xs->cmdlen);
4589 
4590 	io->data_length = htole32(xs->datalen);
4591 
4592 	io->sense_buffer_low_address = htole32(ccb->ccb_cmd_dva +
4593 	    ((u_int8_t *)&mcb->mcb_sense - (u_int8_t *)mcb));
4594 
4595 	if (mpii_load_xs(ccb) != 0) {
4596 		xs->error = XS_DRIVER_STUFFUP;
4597 		mpii_put_ccb(sc, ccb);
4598 		scsipi_done(xs);
4599 		return;
4600 	}
4601 
4602 	DNPRINTF(MPII_D_CMD, "%s:  sizeof(mpii_msg_scsi_io): %ld "
4603 	    "sizeof(mpii_ccb_bundle): %ld sge offset: 0x%02lx\n",
4604 	    DEVNAME(sc), sizeof(struct mpii_msg_scsi_io),
4605 	    sizeof(struct mpii_ccb_bundle),
4606 	    (u_int8_t *)&mcb->mcb_sgl[0] - (u_int8_t *)mcb);
4607 
4608 	DNPRINTF(MPII_D_CMD, "%s   sgl[0]: 0x%04x 0%04x 0x%04x\n",
4609 	    DEVNAME(sc), mcb->mcb_sgl[0].sg_hdr, mcb->mcb_sgl[0].sg_lo_addr,
4610 	    mcb->mcb_sgl[0].sg_hi_addr);
4611 
4612 	DNPRINTF(MPII_D_CMD, "%s:  Offset0: 0x%02x\n", DEVNAME(sc),
4613 	    io->sgl_offset0);
4614 
4615 	if (xs->xs_control & XS_CTL_POLL) {
4616 		if (mpii_poll(sc, ccb) != 0) {
4617 			xs->error = XS_DRIVER_STUFFUP;
4618 			mpii_put_ccb(sc, ccb);
4619 			scsipi_done(xs);
4620 		}
4621 		return;
4622 	}
4623 	timeout = mstohz(xs->timeout);
4624 	if (timeout == 0)
4625 		timeout = 1;
4626 	callout_reset(&xs->xs_callout, timeout, mpii_scsi_cmd_tmo, ccb);
4627 
4628 	DNPRINTF(MPII_D_CMD, "%s:    mpii_scsipi_request(): opcode: %02x "
4629 	    "datalen: %d\n", DEVNAME(sc), xs->cmd->opcode, xs->datalen);
4630 
4631 	mpii_start(sc, ccb);
4632 }
4633 
4634 static void
4635 mpii_scsi_cmd_tmo(void *xccb)
4636 {
4637 	struct mpii_ccb		*ccb = xccb;
4638 	struct mpii_softc	*sc = ccb->ccb_sc;
4639 
4640 	printf("%s: mpii_scsi_cmd_tmo\n", DEVNAME(sc));
4641 
4642 	mutex_enter(&sc->sc_ccb_mtx);
4643 	if (ccb->ccb_state == MPII_CCB_QUEUED) {
4644 		ccb->ccb_state = MPII_CCB_TIMEOUT;
4645 		workqueue_enqueue(sc->sc_ssb_tmowk, &ccb->u.ccb_wk, NULL);
4646 	}
4647 	mutex_exit(&sc->sc_ccb_mtx);
4648 }
4649 
4650 static void
4651 mpii_scsi_cmd_tmo_handler(struct work *wk, void *cookie)
4652 {
4653 	struct mpii_softc			*sc = cookie;
4654 	struct mpii_ccb				*tccb;
4655 	struct mpii_ccb				*ccb;
4656 	struct mpii_msg_scsi_task_request	*stq;
4657 
4658 	ccb = (void *)wk;
4659 	tccb = mpii_get_ccb(sc, 0);
4660 
4661 	mutex_enter(&sc->sc_ccb_mtx);
4662 	if (ccb->ccb_state != MPII_CCB_TIMEOUT) {
4663 		mpii_put_ccb(sc, tccb);
4664 	}
4665 	/* should remove any other ccbs for the same dev handle */
4666 	mutex_exit(&sc->sc_ccb_mtx);
4667 
4668 	stq = tccb->ccb_cmd;
4669 	stq->function = MPII_FUNCTION_SCSI_TASK_MGMT;
4670 	stq->task_type = MPII_SCSI_TASK_TARGET_RESET;
4671 	stq->dev_handle = htole16(ccb->ccb_dev_handle);
4672 
4673 	tccb->ccb_done = mpii_scsi_cmd_tmo_done;
4674 	mpii_start(sc, tccb);
4675 }
4676 
4677 static void
4678 mpii_scsi_cmd_tmo_done(struct mpii_ccb *tccb)
4679 {
4680         mpii_put_ccb(tccb->ccb_sc, tccb);
4681 }
4682 
4683 static u_int8_t
4684 map_scsi_status(u_int8_t mpii_scsi_status)
4685 {
4686 	u_int8_t scsi_status;
4687 
4688 	switch (mpii_scsi_status)
4689 	{
4690 	case MPII_SCSIIO_ERR_STATUS_SUCCESS:
4691 		scsi_status = SCSI_OK;
4692 		break;
4693 
4694 	case MPII_SCSIIO_ERR_STATUS_CHECK_COND:
4695 		scsi_status = SCSI_CHECK;
4696 		break;
4697 
4698 	case MPII_SCSIIO_ERR_STATUS_BUSY:
4699 		scsi_status = SCSI_BUSY;
4700 		break;
4701 
4702 	case MPII_SCSIIO_ERR_STATUS_INTERMEDIATE:
4703 		scsi_status = SCSI_INTERM;
4704 		break;
4705 
4706 	case MPII_SCSIIO_ERR_STATUS_INTERMEDIATE_CONDMET:
4707 		scsi_status = SCSI_INTERM;
4708 		break;
4709 
4710 	case MPII_SCSIIO_ERR_STATUS_RESERVATION_CONFLICT:
4711 		scsi_status = SCSI_RESV_CONFLICT;
4712 		break;
4713 
4714 	case MPII_SCSIIO_ERR_STATUS_CMD_TERM:
4715 	case MPII_SCSIIO_ERR_STATUS_TASK_ABORTED:
4716 		scsi_status = SCSI_TERMINATED;
4717 		break;
4718 
4719 	case MPII_SCSIIO_ERR_STATUS_TASK_SET_FULL:
4720 		scsi_status = SCSI_QUEUE_FULL;
4721 		break;
4722 
4723 	case MPII_SCSIIO_ERR_STATUS_ACA_ACTIVE:
4724 		scsi_status = SCSI_ACA_ACTIVE;
4725 		break;
4726 
4727 	default:
4728 		/* XXX: for the lack of anything better and other than OK */
4729 		scsi_status = 0xFF;
4730 		break;
4731 	}
4732 
4733 	return scsi_status;
4734 }
4735 
4736 static void
4737 mpii_scsi_cmd_done(struct mpii_ccb *ccb)
4738 {
4739 	struct mpii_msg_scsi_io_error	*sie;
4740 	struct mpii_softc	*sc = ccb->ccb_sc;
4741 	struct scsipi_xfer	*xs = ccb->ccb_cookie;
4742 	struct mpii_ccb_bundle	*mcb = ccb->ccb_cmd;
4743 	bus_dmamap_t		dmap = ccb->ccb_dmamap;
4744 	bool			timeout = 0;
4745 
4746 	callout_stop(&xs->xs_callout);
4747 	mutex_enter(&sc->sc_ccb_mtx);
4748 	if (ccb->ccb_state == MPII_CCB_TIMEOUT)
4749 		timeout = 1;
4750 	ccb->ccb_state = MPII_CCB_READY;
4751 	mutex_exit(&sc->sc_ccb_mtx);
4752 
4753 	if (xs->datalen != 0) {
4754 		bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
4755 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
4756 		    BUS_DMASYNC_POSTWRITE);
4757 
4758 		bus_dmamap_unload(sc->sc_dmat, dmap);
4759 	}
4760 
4761 	xs->error = XS_NOERROR;
4762 	xs->resid = 0;
4763 
4764 	if (ccb->ccb_rcb == NULL) {
4765 		/* no scsi error, we're ok so drop out early */
4766 		xs->status = SCSI_OK;
4767 		mpii_put_ccb(sc, ccb);
4768 		scsipi_done(xs);
4769 		return;
4770 	}
4771 
4772 	sie = ccb->ccb_rcb->rcb_reply;
4773 
4774 	DNPRINTF(MPII_D_CMD, "%s: mpii_scsi_cmd_done xs cmd: 0x%02x len: %d "
4775 	    "xs_control 0x%x\n", DEVNAME(sc), xs->cmd->opcode, xs->datalen,
4776 	    xs->xs_control);
4777 	DNPRINTF(MPII_D_CMD, "%s:  dev_handle: %d msg_length: %d "
4778 	    "function: 0x%02x\n", DEVNAME(sc), le16toh(sie->dev_handle),
4779 	    sie->msg_length, sie->function);
4780 	DNPRINTF(MPII_D_CMD, "%s:  vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
4781 	    sie->vp_id, sie->vf_id);
4782 	DNPRINTF(MPII_D_CMD, "%s:  scsi_status: 0x%02x scsi_state: 0x%02x "
4783 	    "ioc_status: 0x%04x\n", DEVNAME(sc), sie->scsi_status,
4784 	    sie->scsi_state, le16toh(sie->ioc_status));
4785 	DNPRINTF(MPII_D_CMD, "%s:  ioc_loginfo: 0x%08x\n", DEVNAME(sc),
4786 	    le32toh(sie->ioc_loginfo));
4787 	DNPRINTF(MPII_D_CMD, "%s:  transfer_count: %d\n", DEVNAME(sc),
4788 	    le32toh(sie->transfer_count));
4789 	DNPRINTF(MPII_D_CMD, "%s:  sense_count: %d\n", DEVNAME(sc),
4790 	    le32toh(sie->sense_count));
4791 	DNPRINTF(MPII_D_CMD, "%s:  response_info: 0x%08x\n", DEVNAME(sc),
4792 	    le32toh(sie->response_info));
4793 	DNPRINTF(MPII_D_CMD, "%s:  task_tag: 0x%04x\n", DEVNAME(sc),
4794 	    le16toh(sie->task_tag));
4795 	DNPRINTF(MPII_D_CMD, "%s:  bidirectional_transfer_count: 0x%08x\n",
4796 	    DEVNAME(sc), le32toh(sie->bidirectional_transfer_count));
4797 
4798 	xs->status = map_scsi_status(sie->scsi_status);
4799 
4800 	switch (le16toh(sie->ioc_status) & MPII_IOCSTATUS_MASK) {
4801 	case MPII_IOCSTATUS_SCSI_DATA_UNDERRUN:
4802 		switch (sie->scsi_status) {
4803 		case MPII_SCSIIO_ERR_STATUS_CHECK_COND:
4804 			xs->error = XS_SENSE;
4805 			/*FALLTHROUGH*/
4806 		case MPII_SCSIIO_ERR_STATUS_SUCCESS:
4807 			xs->resid = xs->datalen - le32toh(sie->transfer_count);
4808 			break;
4809 
4810 		default:
4811 			xs->error = XS_DRIVER_STUFFUP;
4812 			break;
4813 		}
4814 		break;
4815 
4816 	case MPII_IOCSTATUS_SUCCESS:
4817 	case MPII_IOCSTATUS_SCSI_RECOVERED_ERROR:
4818 		switch (sie->scsi_status) {
4819 		case MPII_SCSIIO_ERR_STATUS_SUCCESS:
4820 			/*
4821 			 * xs->resid = 0; - already set above
4822 			 *
4823 			 * XXX: check whether UNDERUN strategy
4824 			 * would be appropriate here too.
4825 			 * that would allow joining these cases.
4826 			 */
4827 			break;
4828 
4829 		case MPII_SCSIIO_ERR_STATUS_CHECK_COND:
4830 			xs->error = XS_SENSE;
4831 			break;
4832 
4833 		case MPII_SCSIIO_ERR_STATUS_BUSY:
4834 		case MPII_SCSIIO_ERR_STATUS_TASK_SET_FULL:
4835 			xs->error = XS_BUSY;
4836 			break;
4837 
4838 		default:
4839 			xs->error = XS_DRIVER_STUFFUP;
4840 		}
4841 		break;
4842 
4843 	case MPII_IOCSTATUS_BUSY:
4844 	case MPII_IOCSTATUS_INSUFFICIENT_RESOURCES:
4845 		xs->error = XS_BUSY;
4846 		break;
4847 
4848 	case MPII_IOCSTATUS_SCSI_IOC_TERMINATED:
4849 	case MPII_IOCSTATUS_SCSI_TASK_TERMINATED:
4850 		xs->error = timeout ? XS_TIMEOUT : XS_RESET;
4851 		break;
4852 
4853 	case MPII_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4854 	case MPII_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4855 		xs->error = XS_SELTIMEOUT;
4856 		break;
4857 
4858 	default:
4859 		xs->error = XS_DRIVER_STUFFUP;
4860 		break;
4861 	}
4862 
4863 	if (sie->scsi_state & MPII_SCSIIO_ERR_STATE_AUTOSENSE_VALID)
4864 		memcpy(&xs->sense, &mcb->mcb_sense, sizeof(xs->sense));
4865 
4866 	DNPRINTF(MPII_D_CMD, "%s:  xs err: %d status: %#x\n", DEVNAME(sc),
4867 	    xs->error, xs->status);
4868 
4869 	mpii_push_reply(sc, ccb->ccb_rcb);
4870 	mpii_put_ccb(sc, ccb);
4871 	scsipi_done(xs);
4872 }
4873 
4874 #if 0
4875 static int
4876 mpii_ioctl_cache(struct scsi_link *link, u_long cmd, struct dk_cache *dc)
4877 {
4878 	struct mpii_softc *sc = (struct mpii_softc *)link->adapter_softc;
4879 	struct mpii_device *dev = sc->sc_devs[link->target];
4880 	struct mpii_cfg_raid_vol_pg0 *vpg;
4881 	struct mpii_msg_raid_action_request *req;
4882  	struct mpii_msg_raid_action_reply *rep;
4883 	struct mpii_cfg_hdr hdr;
4884 	struct mpii_ccb	*ccb;
4885 	u_int32_t addr = MPII_CFG_RAID_VOL_ADDR_HANDLE | dev->dev_handle;
4886 	size_t pagelen;
4887 	int rv = 0;
4888 	int enabled;
4889 
4890 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
4891 	    addr, MPII_PG_POLL, &hdr) != 0)
4892 		return (EINVAL);
4893 
4894 	pagelen = hdr.page_length * 4;
4895 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_CANFAIL | M_ZERO);
4896 	if (vpg == NULL)
4897 		return (ENOMEM);
4898 
4899 	if (mpii_req_cfg_page(sc, addr, MPII_PG_POLL, &hdr, 1,
4900 	    vpg, pagelen) != 0) {
4901 		rv = EINVAL;
4902 		goto done;
4903 		free(vpg, M_TEMP);
4904 		return (EINVAL);
4905 	}
4906 
4907 	enabled = ((le16toh(vpg->volume_settings) &
4908 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_MASK) ==
4909 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_ENABLED) ? 1 : 0;
4910 
4911 	if (cmd == DIOCGCACHE) {
4912 		dc->wrcache = enabled;
4913 		dc->rdcache = 0;
4914 		goto done;
4915 	} /* else DIOCSCACHE */
4916 
4917 	if (dc->rdcache) {
4918 		rv = EOPNOTSUPP;
4919 		goto done;
4920 	}
4921 
4922 	if (((dc->wrcache) ? 1 : 0) == enabled)
4923 		goto done;
4924 
4925 	ccb = mpii_get_ccb(sc, MPII_NOSLEEP);
4926 	if (ccb == NULL) {
4927 		rv = ENOMEM;
4928 		goto done;
4929 	}
4930 
4931 	ccb->ccb_done = mpii_empty_done;
4932 
4933 	req = ccb->ccb_cmd;
4934 	bzero(req, sizeof(*req));
4935 	req->function = MPII_FUNCTION_RAID_ACTION;
4936 	req->action = MPII_RAID_ACTION_CHANGE_VOL_WRITE_CACHE;
4937 	req->vol_dev_handle = htole16(dev->dev_handle);
4938 	req->action_data = htole32(dc->wrcache ?
4939 	    MPII_RAID_VOL_WRITE_CACHE_ENABLE :
4940 	    MPII_RAID_VOL_WRITE_CACHE_DISABLE);
4941 
4942 	if (mpii_poll(sc, ccb) != 0) {
4943 		rv = EIO;
4944 		goto done;
4945 	}
4946 
4947 	if (ccb->ccb_rcb != NULL) {
4948 		rep = ccb->ccb_rcb->rcb_reply;
4949 		if ((rep->ioc_status != MPII_IOCSTATUS_SUCCESS) ||
4950 		    ((rep->action_data[0] &
4951 		     MPII_RAID_VOL_WRITE_CACHE_MASK) !=
4952 		    (dc->wrcache ? MPII_RAID_VOL_WRITE_CACHE_ENABLE :
4953 		     MPII_RAID_VOL_WRITE_CACHE_DISABLE)))
4954 			rv = EINVAL;
4955 		mpii_push_reply(sc, ccb->ccb_rcb);
4956 	}
4957 
4958 	mpii_put_ccb(sc, ccb);
4959 
4960 done:
4961 	free(vpg, M_TEMP);
4962 	return (rv);
4963 }
4964 #endif
4965 static int
4966 mpii_cache_enable(struct mpii_softc *sc, struct mpii_device *dev)
4967 {
4968 	struct mpii_cfg_raid_vol_pg0 *vpg;
4969 	struct mpii_msg_raid_action_request *req;
4970  	struct mpii_msg_raid_action_reply *rep;
4971 	struct mpii_cfg_hdr hdr;
4972 	struct mpii_ccb	*ccb;
4973 	u_int32_t addr = MPII_CFG_RAID_VOL_ADDR_HANDLE | dev->dev_handle;
4974 	size_t pagelen;
4975 	int rv = 0;
4976 	int enabled;
4977 
4978 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
4979 	    addr, MPII_PG_POLL, &hdr) != 0)
4980 		return (EINVAL);
4981 
4982 	pagelen = hdr.page_length * 4;
4983 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_CANFAIL | M_ZERO);
4984 	if (vpg == NULL)
4985 		return (ENOMEM);
4986 
4987 	if (mpii_req_cfg_page(sc, addr, MPII_PG_POLL, &hdr, 1,
4988 	    vpg, pagelen) != 0) {
4989 		rv = EINVAL;
4990 		goto done;
4991 		free(vpg, M_TEMP);
4992 		return (EINVAL);
4993 	}
4994 
4995 	enabled = ((le16toh(vpg->volume_settings) &
4996 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_MASK) ==
4997 	    MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_ENABLED) ? 1 : 0;
4998 	aprint_normal_dev(sc->sc_dev, "target %d cache %s", dev->slot,
4999 	    enabled ? "enabled" : "disabled, enabling");
5000 	aprint_normal("\n");
5001 
5002 	if (enabled == 0)
5003 		goto done;
5004 
5005 	ccb = mpii_get_ccb(sc, MPII_NOSLEEP);
5006 	if (ccb == NULL) {
5007 		rv = ENOMEM;
5008 		goto done;
5009 	}
5010 
5011 	ccb->ccb_done = mpii_empty_done;
5012 
5013 	req = ccb->ccb_cmd;
5014 	bzero(req, sizeof(*req));
5015 	req->function = MPII_FUNCTION_RAID_ACTION;
5016 	req->action = MPII_RAID_ACTION_CHANGE_VOL_WRITE_CACHE;
5017 	req->vol_dev_handle = htole16(dev->dev_handle);
5018 	req->action_data = htole32(
5019 	    MPII_RAID_VOL_WRITE_CACHE_ENABLE);
5020 
5021 	if (mpii_poll(sc, ccb) != 0) {
5022 		rv = EIO;
5023 		goto done;
5024 	}
5025 
5026 	if (ccb->ccb_rcb != NULL) {
5027 		rep = ccb->ccb_rcb->rcb_reply;
5028 		if ((rep->ioc_status != MPII_IOCSTATUS_SUCCESS) ||
5029 		    ((rep->action_data[0] &
5030 		     MPII_RAID_VOL_WRITE_CACHE_MASK) !=
5031 		     MPII_RAID_VOL_WRITE_CACHE_ENABLE))
5032 			rv = EINVAL;
5033 		mpii_push_reply(sc, ccb->ccb_rcb);
5034 	}
5035 
5036 	mpii_put_ccb(sc, ccb);
5037 
5038 done:
5039 	free(vpg, M_TEMP);
5040 	if (rv) {
5041 		aprint_error_dev(sc->sc_dev,
5042 		    "enabling cache on target %d failed (%d)\n",
5043 		    dev->slot, rv);
5044 	}
5045 	return (rv);
5046 }
5047 
5048 #if NBIO > 0
5049 static int
5050 mpii_ioctl(device_t dev, u_long cmd, void *addr)
5051 {
5052 	struct mpii_softc	*sc = device_private(dev);
5053 	int			s, error = 0;
5054 
5055 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl ", DEVNAME(sc));
5056 	KERNEL_LOCK(1, curlwp);
5057 	s = splbio();
5058 
5059 	switch (cmd) {
5060 	case BIOCINQ:
5061 		DNPRINTF(MPII_D_IOCTL, "inq\n");
5062 		error = mpii_ioctl_inq(sc, (struct bioc_inq *)addr);
5063 		break;
5064 	case BIOCVOL:
5065 		DNPRINTF(MPII_D_IOCTL, "vol\n");
5066 		error = mpii_ioctl_vol(sc, (struct bioc_vol *)addr);
5067 		break;
5068 	case BIOCDISK:
5069 		DNPRINTF(MPII_D_IOCTL, "disk\n");
5070 		error = mpii_ioctl_disk(sc, (struct bioc_disk *)addr);
5071 		break;
5072 	default:
5073 		DNPRINTF(MPII_D_IOCTL, " invalid ioctl\n");
5074 		error = EINVAL;
5075 	}
5076 
5077 	splx(s);
5078 	KERNEL_UNLOCK_ONE(curlwp);
5079 	return (error);
5080 }
5081 
5082 static int
5083 mpii_ioctl_inq(struct mpii_softc *sc, struct bioc_inq *bi)
5084 {
5085 	int			i;
5086 
5087 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_inq\n", DEVNAME(sc));
5088 
5089 	strlcpy(bi->bi_dev, DEVNAME(sc), sizeof(bi->bi_dev));
5090 	for (i = 0; i < sc->sc_max_devices; i++)
5091 		if (sc->sc_devs[i] &&
5092 		    ISSET(sc->sc_devs[i]->flags, MPII_DF_VOLUME))
5093 			bi->bi_novol++;
5094 	return (0);
5095 }
5096 
5097 static int
5098 mpii_ioctl_vol(struct mpii_softc *sc, struct bioc_vol *bv)
5099 {
5100 	struct mpii_cfg_raid_vol_pg0	*vpg;
5101 	struct mpii_cfg_hdr		hdr;
5102 	struct mpii_device		*dev;
5103 	struct scsipi_periph 		*periph;
5104 	size_t				pagelen;
5105 	u_int16_t			volh;
5106 	int				rv, hcnt = 0;
5107 
5108 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_vol %d\n",
5109 	    DEVNAME(sc), bv->bv_volid);
5110 
5111 	if ((dev = mpii_find_vol(sc, bv->bv_volid)) == NULL)
5112 		return (ENODEV);
5113 	volh = dev->dev_handle;
5114 
5115 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
5116 	    MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0, &hdr) != 0) {
5117 		printf("%s: unable to fetch header for raid volume page 0\n",
5118 		    DEVNAME(sc));
5119 		return (EINVAL);
5120 	}
5121 
5122 	pagelen = hdr.page_length * 4;
5123 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_CANFAIL | M_ZERO);
5124 	if (vpg == NULL) {
5125 		printf("%s: unable to allocate space for raid "
5126 		    "volume page 0\n", DEVNAME(sc));
5127 		return (ENOMEM);
5128 	}
5129 
5130 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0,
5131 	    &hdr, 1, vpg, pagelen) != 0) {
5132 		printf("%s: unable to fetch raid volume page 0\n",
5133 		    DEVNAME(sc));
5134 		free(vpg, M_TEMP);
5135 		return (EINVAL);
5136 	}
5137 
5138 	switch (vpg->volume_state) {
5139 	case MPII_CFG_RAID_VOL_0_STATE_ONLINE:
5140 	case MPII_CFG_RAID_VOL_0_STATE_OPTIMAL:
5141 		bv->bv_status = BIOC_SVONLINE;
5142 		break;
5143 	case MPII_CFG_RAID_VOL_0_STATE_DEGRADED:
5144 		if (ISSET(le32toh(vpg->volume_status),
5145 		    MPII_CFG_RAID_VOL_0_STATUS_RESYNC)) {
5146 			bv->bv_status = BIOC_SVREBUILD;
5147 			bv->bv_percent = dev->percent;
5148 		} else
5149 			bv->bv_status = BIOC_SVDEGRADED;
5150 		break;
5151 	case MPII_CFG_RAID_VOL_0_STATE_FAILED:
5152 		bv->bv_status = BIOC_SVOFFLINE;
5153 		break;
5154 	case MPII_CFG_RAID_VOL_0_STATE_INITIALIZING:
5155 		bv->bv_status = BIOC_SVBUILDING;
5156 		break;
5157 	case MPII_CFG_RAID_VOL_0_STATE_MISSING:
5158 	default:
5159 		bv->bv_status = BIOC_SVINVALID;
5160 		break;
5161 	}
5162 
5163 	switch (vpg->volume_type) {
5164 	case MPII_CFG_RAID_VOL_0_TYPE_RAID0:
5165 		bv->bv_level = 0;
5166 		break;
5167 	case MPII_CFG_RAID_VOL_0_TYPE_RAID1:
5168 		bv->bv_level = 1;
5169 		break;
5170 	case MPII_CFG_RAID_VOL_0_TYPE_RAID1E:
5171 	case MPII_CFG_RAID_VOL_0_TYPE_RAID10:
5172 		bv->bv_level = 10;
5173 		break;
5174 	default:
5175 		bv->bv_level = -1;
5176 	}
5177 
5178 	if ((rv = mpii_bio_hs(sc, NULL, 0, vpg->hot_spare_pool, &hcnt)) != 0) {
5179 		free(vpg, M_TEMP);
5180 		return (rv);
5181 	}
5182 
5183 	bv->bv_nodisk = vpg->num_phys_disks + hcnt;
5184 
5185 	bv->bv_size = le64toh(vpg->max_lba) * le16toh(vpg->block_size);
5186 
5187 	periph = scsipi_lookup_periph(&sc->sc_chan, dev->slot, 0);
5188 	if (periph != NULL) {
5189 		if (periph->periph_dev == NULL) {
5190 			snprintf(bv->bv_dev, sizeof(bv->bv_dev), "%s:%d",
5191 			    DEVNAME(sc), dev->slot);
5192 		} else {
5193 			strlcpy(bv->bv_dev, device_xname(periph->periph_dev),
5194 			    sizeof(bv->bv_dev));
5195 		}
5196 	}
5197 
5198 	free(vpg, M_TEMP);
5199 	return (0);
5200 }
5201 
5202 static int
5203 mpii_ioctl_disk(struct mpii_softc *sc, struct bioc_disk *bd)
5204 {
5205 	struct mpii_cfg_raid_vol_pg0		*vpg;
5206 	struct mpii_cfg_raid_vol_pg0_physdisk	*pd;
5207 	struct mpii_cfg_hdr			hdr;
5208 	struct mpii_device			*dev;
5209 	size_t					pagelen;
5210 	u_int16_t				volh;
5211 	u_int8_t				dn;
5212 
5213 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_disk %d/%d\n",
5214 	    DEVNAME(sc), bd->bd_volid, bd->bd_diskid);
5215 
5216 	if ((dev = mpii_find_vol(sc, bd->bd_volid)) == NULL)
5217 		return (ENODEV);
5218 	volh = dev->dev_handle;
5219 
5220 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
5221 	    MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0, &hdr) != 0) {
5222 		printf("%s: unable to fetch header for raid volume page 0\n",
5223 		    DEVNAME(sc));
5224 		return (EINVAL);
5225 	}
5226 
5227 	pagelen = hdr.page_length * 4;
5228 	vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_CANFAIL | M_ZERO);
5229 	if (vpg == NULL) {
5230 		printf("%s: unable to allocate space for raid "
5231 		    "volume page 0\n", DEVNAME(sc));
5232 		return (ENOMEM);
5233 	}
5234 
5235 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0,
5236 	    &hdr, 1, vpg, pagelen) != 0) {
5237 		printf("%s: unable to fetch raid volume page 0\n",
5238 		    DEVNAME(sc));
5239 		free(vpg, M_TEMP);
5240 		return (EINVAL);
5241 	}
5242 
5243 	if (bd->bd_diskid >= vpg->num_phys_disks) {
5244 		int		nvdsk = vpg->num_phys_disks;
5245 		int		hsmap = vpg->hot_spare_pool;
5246 
5247 		free(vpg, M_TEMP);
5248 		return (mpii_bio_hs(sc, bd, nvdsk, hsmap, NULL));
5249 	}
5250 
5251 	pd = (struct mpii_cfg_raid_vol_pg0_physdisk *)(vpg + 1) +
5252 	    bd->bd_diskid;
5253 	dn = pd->phys_disk_num;
5254 
5255 	free(vpg, M_TEMP);
5256 	return (mpii_bio_disk(sc, bd, dn));
5257 }
5258 
5259 static int
5260 mpii_bio_hs(struct mpii_softc *sc, struct bioc_disk *bd, int nvdsk,
5261      int hsmap, int *hscnt)
5262 {
5263 	struct mpii_cfg_raid_config_pg0	*cpg;
5264 	struct mpii_raid_config_element	*el;
5265 	struct mpii_ecfg_hdr		ehdr;
5266 	size_t				pagelen;
5267 	int				i, nhs = 0;
5268 
5269 	if (bd) {
5270 		DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_hs %d\n", DEVNAME(sc),
5271 		    bd->bd_diskid - nvdsk);
5272 	} else {
5273 		DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_hs\n", DEVNAME(sc));
5274 	}
5275 
5276 	if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_CONFIG,
5277 	    0, MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG, MPII_PG_EXTENDED,
5278 	    &ehdr) != 0) {
5279 		printf("%s: unable to fetch header for raid config page 0\n",
5280 		    DEVNAME(sc));
5281 		return (EINVAL);
5282 	}
5283 
5284 	pagelen = le16toh(ehdr.ext_page_length) * 4;
5285 	cpg = malloc(pagelen, M_TEMP, M_WAITOK | M_CANFAIL | M_ZERO);
5286 	if (cpg == NULL) {
5287 		printf("%s: unable to allocate space for raid config page 0\n",
5288 		    DEVNAME(sc));
5289 		return (ENOMEM);
5290 	}
5291 
5292 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG,
5293 	    MPII_PG_EXTENDED, &ehdr, 1, cpg, pagelen) != 0) {
5294 		printf("%s: unable to fetch raid config page 0\n",
5295 		    DEVNAME(sc));
5296 		free(cpg, M_TEMP);
5297 		return (EINVAL);
5298 	}
5299 
5300 	el = (struct mpii_raid_config_element *)(cpg + 1);
5301 	for (i = 0; i < cpg->num_elements; i++, el++) {
5302 		if (ISSET(le16toh(el->element_flags),
5303 		    MPII_RAID_CONFIG_ELEMENT_FLAG_HSP_PHYS_DISK) &&
5304 		    el->hot_spare_pool == hsmap) {
5305 			/*
5306 			 * diskid comparison is based on the idea that all
5307 			 * disks are counted by the bio(4) in sequence, thus
5308 			 * substracting the number of disks in the volume
5309 			 * from the diskid yields us a "relative" hotspare
5310 			 * number, which is good enough for us.
5311 			 */
5312 			if (bd != NULL && bd->bd_diskid == nhs + nvdsk) {
5313 				u_int8_t dn = el->phys_disk_num;
5314 
5315 				free(cpg, M_TEMP);
5316 				return (mpii_bio_disk(sc, bd, dn));
5317 			}
5318 			nhs++;
5319 		}
5320 	}
5321 
5322 	if (hscnt)
5323 		*hscnt = nhs;
5324 
5325 	free(cpg, M_TEMP);
5326 	return (0);
5327 }
5328 
5329 static int
5330 mpii_bio_disk(struct mpii_softc *sc, struct bioc_disk *bd, u_int8_t dn)
5331 {
5332 	struct mpii_cfg_raid_physdisk_pg0	*ppg;
5333 	struct mpii_cfg_hdr			hdr;
5334 	struct mpii_device			*dev;
5335 	int					len;
5336 
5337 	DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_disk %d\n", DEVNAME(sc),
5338 	    bd->bd_diskid);
5339 
5340 	ppg = malloc(sizeof(*ppg), M_TEMP, M_WAITOK | M_CANFAIL | M_ZERO);
5341 	if (ppg == NULL) {
5342 		printf("%s: unable to allocate space for raid physical disk "
5343 		    "page 0\n", DEVNAME(sc));
5344 		return (ENOMEM);
5345 	}
5346 
5347 	hdr.page_version = 0;
5348 	hdr.page_length = sizeof(*ppg) / 4;
5349 	hdr.page_number = 0;
5350 	hdr.page_type = MPII_CONFIG_REQ_PAGE_TYPE_RAID_PD;
5351 
5352 	if (mpii_req_cfg_page(sc, MPII_CFG_RAID_PHYS_DISK_ADDR_NUMBER | dn, 0,
5353 	    &hdr, 1, ppg, sizeof(*ppg)) != 0) {
5354 		printf("%s: unable to fetch raid drive page 0\n",
5355 		    DEVNAME(sc));
5356 		free(ppg, M_TEMP);
5357 		return (EINVAL);
5358 	}
5359 
5360 	bd->bd_target = ppg->phys_disk_num;
5361 
5362 	if ((dev = mpii_find_dev(sc, le16toh(ppg->dev_handle))) == NULL) {
5363 		bd->bd_status = BIOC_SDINVALID;
5364 		free(ppg, M_TEMP);
5365 		return (0);
5366 	}
5367 
5368 	switch (ppg->phys_disk_state) {
5369 	case MPII_CFG_RAID_PHYDISK_0_STATE_ONLINE:
5370 	case MPII_CFG_RAID_PHYDISK_0_STATE_OPTIMAL:
5371 		bd->bd_status = BIOC_SDONLINE;
5372 		break;
5373 	case MPII_CFG_RAID_PHYDISK_0_STATE_OFFLINE:
5374 		if (ppg->offline_reason ==
5375 		    MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILED ||
5376 		    ppg->offline_reason ==
5377 		    MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILEDREQ)
5378 			bd->bd_status = BIOC_SDFAILED;
5379 		else
5380 			bd->bd_status = BIOC_SDOFFLINE;
5381 		break;
5382 	case MPII_CFG_RAID_PHYDISK_0_STATE_DEGRADED:
5383 		bd->bd_status = BIOC_SDFAILED;
5384 		break;
5385 	case MPII_CFG_RAID_PHYDISK_0_STATE_REBUILDING:
5386 		bd->bd_status = BIOC_SDREBUILD;
5387 		break;
5388 	case MPII_CFG_RAID_PHYDISK_0_STATE_HOTSPARE:
5389 		bd->bd_status = BIOC_SDHOTSPARE;
5390 		break;
5391 	case MPII_CFG_RAID_PHYDISK_0_STATE_NOTCONFIGURED:
5392 		bd->bd_status = BIOC_SDUNUSED;
5393 		break;
5394 	case MPII_CFG_RAID_PHYDISK_0_STATE_NOTCOMPATIBLE:
5395 	default:
5396 		bd->bd_status = BIOC_SDINVALID;
5397 		break;
5398 	}
5399 
5400 	bd->bd_size = le64toh(ppg->dev_max_lba) * le16toh(ppg->block_size);
5401 
5402 	scsipi_strvis(bd->bd_vendor, sizeof(bd->bd_vendor),
5403 	    ppg->vendor_id, sizeof(ppg->vendor_id));
5404 	len = strlen(bd->bd_vendor);
5405 	bd->bd_vendor[len] = ' ';
5406 	scsipi_strvis(&bd->bd_vendor[len + 1], sizeof(ppg->vendor_id) - len - 1,
5407 	    ppg->product_id, sizeof(ppg->product_id));
5408 	scsipi_strvis(bd->bd_serial, sizeof(bd->bd_serial),
5409 	    ppg->serial, sizeof(ppg->serial));
5410 
5411 	free(ppg, M_TEMP);
5412 	return (0);
5413 }
5414 
5415 static struct mpii_device *
5416 mpii_find_vol(struct mpii_softc *sc, int volid)
5417 {
5418 	struct mpii_device	*dev = NULL;
5419 
5420 	if (sc->sc_vd_id_low + volid >= sc->sc_max_devices)
5421 		return (NULL);
5422 	dev = sc->sc_devs[sc->sc_vd_id_low + volid];
5423 	if (dev && ISSET(dev->flags, MPII_DF_VOLUME))
5424 		return (dev);
5425 	return (NULL);
5426 }
5427 
5428 /*
5429  * Non-sleeping lightweight version of the mpii_ioctl_vol
5430  */
5431 static int
5432 mpii_bio_volstate(struct mpii_softc *sc, struct bioc_vol *bv)
5433 {
5434 	struct mpii_cfg_raid_vol_pg0	*vpg;
5435 	struct mpii_cfg_hdr		hdr;
5436 	struct mpii_device		*dev = NULL;
5437 	size_t				pagelen;
5438 	u_int16_t			volh;
5439 
5440 	if ((dev = mpii_find_vol(sc, bv->bv_volid)) == NULL)
5441 		return (ENODEV);
5442 	volh = dev->dev_handle;
5443 
5444 	if (mpii_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
5445 	    MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, &hdr) != 0) {
5446 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch header for raid "
5447 		    "volume page 0\n", DEVNAME(sc));
5448 		return (EINVAL);
5449 	}
5450 
5451 	pagelen = hdr.page_length * 4;
5452 	vpg = malloc(pagelen, M_TEMP, M_NOWAIT | M_ZERO);
5453 	if (vpg == NULL) {
5454 		DNPRINTF(MPII_D_MISC, "%s: unable to allocate space for raid "
5455 		    "volume page 0\n", DEVNAME(sc));
5456 		return (ENOMEM);
5457 	}
5458 
5459 	if (mpii_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh,
5460 	    &hdr, 1, vpg, pagelen) != 0) {
5461 		DNPRINTF(MPII_D_MISC, "%s: unable to fetch raid volume "
5462 		    "page 0\n", DEVNAME(sc));
5463 		free(vpg, M_TEMP);
5464 		return (EINVAL);
5465 	}
5466 
5467 	switch (vpg->volume_state) {
5468 	case MPII_CFG_RAID_VOL_0_STATE_ONLINE:
5469 	case MPII_CFG_RAID_VOL_0_STATE_OPTIMAL:
5470 		bv->bv_status = BIOC_SVONLINE;
5471 		break;
5472 	case MPII_CFG_RAID_VOL_0_STATE_DEGRADED:
5473 		if (ISSET(le32toh(vpg->volume_status),
5474 		    MPII_CFG_RAID_VOL_0_STATUS_RESYNC))
5475 			bv->bv_status = BIOC_SVREBUILD;
5476 		else
5477 			bv->bv_status = BIOC_SVDEGRADED;
5478 		break;
5479 	case MPII_CFG_RAID_VOL_0_STATE_FAILED:
5480 		bv->bv_status = BIOC_SVOFFLINE;
5481 		break;
5482 	case MPII_CFG_RAID_VOL_0_STATE_INITIALIZING:
5483 		bv->bv_status = BIOC_SVBUILDING;
5484 		break;
5485 	case MPII_CFG_RAID_VOL_0_STATE_MISSING:
5486 	default:
5487 		bv->bv_status = BIOC_SVINVALID;
5488 		break;
5489 	}
5490 
5491 	free(vpg, M_TEMP);
5492 	return (0);
5493 }
5494 
5495 static int
5496 mpii_create_sensors(struct mpii_softc *sc)
5497 {
5498 	int			i, rv;
5499 
5500 	sc->sc_sme = sysmon_envsys_create();
5501 	sc->sc_sensors = malloc(sizeof(envsys_data_t) * sc->sc_vd_count,
5502 	    M_DEVBUF, M_NOWAIT | M_ZERO);
5503 	if (sc->sc_sensors == NULL) {
5504 		aprint_error_dev(sc->sc_dev,
5505 		    "can't allocate envsys_data_t\n");
5506 		return (1);
5507 	}
5508 
5509 	for (i = 0; i < sc->sc_vd_count; i++) {
5510 		sc->sc_sensors[i].units = ENVSYS_DRIVE;
5511 		sc->sc_sensors[i].state = ENVSYS_SINVALID;
5512 		sc->sc_sensors[i].value_cur = ENVSYS_DRIVE_EMPTY;
5513 		/* Enable monitoring for drive state changes */
5514 		sc->sc_sensors[i].flags |= ENVSYS_FMONSTCHANGED;
5515 
5516 		/* logical drives */
5517 		snprintf(sc->sc_sensors[i].desc,
5518 		    sizeof(sc->sc_sensors[i].desc), "%s:%d",
5519 		    DEVNAME(sc), i);
5520 		if ((rv = sysmon_envsys_sensor_attach(sc->sc_sme,
5521 		    &sc->sc_sensors[i])) != 0) {
5522 			aprint_error_dev(sc->sc_dev,
5523 			    "unable to attach sensor (rv = %d)\n", rv);
5524 			goto out;
5525 		}
5526 	}
5527 	sc->sc_sme->sme_name =  DEVNAME(sc);
5528 	sc->sc_sme->sme_cookie = sc;
5529 	sc->sc_sme->sme_refresh = mpii_refresh_sensors;
5530 
5531 	rv = sysmon_envsys_register(sc->sc_sme);
5532 
5533 	if (rv != 0) {
5534 		aprint_error_dev(sc->sc_dev,
5535 		    "unable to register with sysmon (rv = %d)\n", rv);
5536 		goto out;
5537 	}
5538 	return 0;
5539 
5540 out:
5541 	free(sc->sc_sensors, M_DEVBUF);
5542 	sysmon_envsys_destroy(sc->sc_sme);
5543 	sc->sc_sme = NULL;
5544 	return EINVAL;
5545 }
5546 
5547 static int
5548 mpii_destroy_sensors(struct mpii_softc *sc)
5549 {
5550 	if (sc->sc_sme == NULL)
5551 		return 0;
5552 	sysmon_envsys_unregister(sc->sc_sme);
5553 	sc->sc_sme = NULL;
5554 	free(sc->sc_sensors, M_DEVBUF);
5555 	return 0;
5556 }
5557 
5558 static void
5559 mpii_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata)
5560 {
5561 	struct mpii_softc	*sc = sc = sme->sme_cookie;
5562 	struct bioc_vol		bv;
5563 	int			s, error;
5564 
5565 	bzero(&bv, sizeof(bv));
5566 	bv.bv_volid = edata->sensor;
5567 	KERNEL_LOCK(1, curlwp);
5568 	s = splbio();
5569 	error = mpii_bio_volstate(sc, &bv);
5570 	splx(s);
5571 	KERNEL_UNLOCK_ONE(curlwp);
5572 	if (error)
5573 		return;
5574 	switch(bv.bv_status) {
5575 	case BIOC_SVOFFLINE:
5576 		edata->value_cur = ENVSYS_DRIVE_FAIL;
5577 		edata->state = ENVSYS_SCRITICAL;
5578 		break;
5579 	case BIOC_SVDEGRADED:
5580 		edata->value_cur = ENVSYS_DRIVE_PFAIL;
5581 		edata->state = ENVSYS_SCRITICAL;
5582 		break;
5583 	case BIOC_SVREBUILD:
5584 		edata->value_cur = ENVSYS_DRIVE_REBUILD;
5585 		edata->state = ENVSYS_SVALID;
5586 		break;
5587 	case BIOC_SVONLINE:
5588 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
5589 		edata->state = ENVSYS_SVALID;
5590 		break;
5591 	case BIOC_SVINVALID:
5592 		/* FALLTHROUGH */
5593 	default:
5594 		edata->value_cur = 0; /* unknown */
5595 		edata->state = ENVSYS_SINVALID;
5596 	}
5597 }
5598 #endif /* NBIO > 0 */
5599