xref: /illumos-gate/usr/src/uts/common/io/scsi/adapters/mpt_sas/mptsas_impl.c (revision 0a586cea3ceec7e5e50e7e54c745082a7a333ac2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2000 to 2010, LSI Corporation.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms of all code within
32  * this file that is exclusively owned by LSI, with or without
33  * modification, is permitted provided that, in addition to the CDDL 1.0
34  * License requirements, the following conditions are met:
35  *
36  *    Neither the name of the author nor the names of its contributors may be
37  *    used to endorse or promote products derived from this software without
38  *    specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
43  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
44  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
45  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
46  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
47  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
48  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51  * DAMAGE.
52  */
53 
54 /*
55  * mptsas_impl - This file contains all the basic functions for communicating
56  * to MPT based hardware.
57  */
58 
59 #if defined(lint) || defined(DEBUG)
60 #define	MPTSAS_DEBUG
61 #endif
62 
63 /*
64  * standard header files
65  */
66 #include <sys/note.h>
67 #include <sys/scsi/scsi.h>
68 #include <sys/pci.h>
69 
70 #pragma pack(1)
71 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_type.h>
72 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2.h>
73 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_cnfg.h>
74 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_init.h>
75 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_ioc.h>
76 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_sas.h>
77 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_tool.h>
78 #pragma pack()
79 
80 /*
81  * private header files.
82  */
83 #include <sys/scsi/adapters/mpt_sas/mptsas_var.h>
84 
85 /*
86  * FMA header files.
87  */
88 #include <sys/fm/io/ddi.h>
89 
90 #if defined(MPTSAS_DEBUG)
91 extern uint32_t mptsas_debug_flags;
92 #endif
93 
94 /*
95  *  prototypes
96  */
97 static void mptsas_ioc_event_cmdq_add(mptsas_t *mpt, m_event_struct_t *cmd);
98 static void mptsas_ioc_event_cmdq_delete(mptsas_t *mpt, m_event_struct_t *cmd);
99 static m_event_struct_t *mptsas_ioc_event_find_by_cmd(mptsas_t *mpt,
100     struct mptsas_cmd *cmd);
101 
102 /*
103  * add ioc evnet cmd into the queue
104  */
105 static void
106 mptsas_ioc_event_cmdq_add(mptsas_t *mpt, m_event_struct_t *cmd)
107 {
108 	if ((cmd->m_event_linkp = mpt->m_ioc_event_cmdq) == NULL) {
109 		mpt->m_ioc_event_cmdtail = &cmd->m_event_linkp;
110 		mpt->m_ioc_event_cmdq = cmd;
111 	} else {
112 		cmd->m_event_linkp = NULL;
113 		*(mpt->m_ioc_event_cmdtail) = cmd;
114 		mpt->m_ioc_event_cmdtail = &cmd->m_event_linkp;
115 	}
116 }
117 
118 /*
119  * remove specified cmd from the ioc event queue
120  */
121 static void
122 mptsas_ioc_event_cmdq_delete(mptsas_t *mpt, m_event_struct_t *cmd)
123 {
124 	m_event_struct_t	*prev = mpt->m_ioc_event_cmdq;
125 	if (prev == cmd) {
126 		if ((mpt->m_ioc_event_cmdq = cmd->m_event_linkp) == NULL) {
127 			mpt->m_ioc_event_cmdtail = &mpt->m_ioc_event_cmdq;
128 		}
129 		cmd->m_event_linkp = NULL;
130 		return;
131 	}
132 	while (prev != NULL) {
133 		if (prev->m_event_linkp == cmd) {
134 			prev->m_event_linkp = cmd->m_event_linkp;
135 			if (cmd->m_event_linkp == NULL) {
136 				mpt->m_ioc_event_cmdtail = &prev->m_event_linkp;
137 			}
138 
139 			cmd->m_event_linkp = NULL;
140 			return;
141 		}
142 		prev = prev->m_event_linkp;
143 	}
144 }
145 
146 static m_event_struct_t *
147 mptsas_ioc_event_find_by_cmd(mptsas_t *mpt, struct mptsas_cmd *cmd)
148 {
149 	m_event_struct_t	*ioc_cmd = NULL;
150 
151 	ioc_cmd = mpt->m_ioc_event_cmdq;
152 	while (ioc_cmd != NULL) {
153 		if (&(ioc_cmd->m_event_cmd) == cmd) {
154 			return (ioc_cmd);
155 		}
156 		ioc_cmd = ioc_cmd->m_event_linkp;
157 	}
158 	ioc_cmd = NULL;
159 	return (ioc_cmd);
160 }
161 
162 void
163 mptsas_destroy_ioc_event_cmd(mptsas_t *mpt)
164 {
165 	m_event_struct_t	*ioc_cmd = NULL;
166 	m_event_struct_t	*ioc_cmd_tmp = NULL;
167 	ioc_cmd = mpt->m_ioc_event_cmdq;
168 
169 	/*
170 	 * because the IOC event queue is resource of per instance for driver,
171 	 * it's not only ACK event commands used it, but also some others used
172 	 * it. We need destroy all ACK event commands when IOC reset, but can't
173 	 * disturb others.So we use filter to clear the ACK event cmd in ioc
174 	 * event queue, and other requests should be reserved, and they would
175 	 * be free by its owner.
176 	 */
177 	while (ioc_cmd != NULL) {
178 		if (ioc_cmd->m_event_cmd.cmd_flags & CFLAG_CMDACK) {
179 			NDBG20(("destroy!! remove Ack Flag ioc_cmd\n"));
180 			if ((mpt->m_ioc_event_cmdq =
181 			    ioc_cmd->m_event_linkp) == NULL)
182 				mpt->m_ioc_event_cmdtail =
183 				    &mpt->m_ioc_event_cmdq;
184 			ioc_cmd_tmp = ioc_cmd;
185 			ioc_cmd = ioc_cmd->m_event_linkp;
186 			kmem_free(ioc_cmd_tmp, M_EVENT_STRUCT_SIZE);
187 		} else {
188 			/*
189 			 * it's not ack cmd, so continue to check next one
190 			 */
191 
192 			NDBG20(("destroy!! it's not Ack Flag, continue\n"));
193 			ioc_cmd = ioc_cmd->m_event_linkp;
194 		}
195 
196 	}
197 }
198 
199 void
200 mptsas_start_config_page_access(mptsas_t *mpt, mptsas_cmd_t *cmd)
201 {
202 	pMpi2ConfigRequest_t	request;
203 	pMpi2SGESimple64_t	sge;
204 	struct scsi_pkt		*pkt = cmd->cmd_pkt;
205 	mptsas_config_request_t	*config = pkt->pkt_ha_private;
206 	uint8_t			direction;
207 	uint32_t		length, flagslength, request_desc_low;
208 
209 	ASSERT(mutex_owned(&mpt->m_mutex));
210 
211 	/*
212 	 * Point to the correct message and clear it as well as the global
213 	 * config page memory.
214 	 */
215 	request = (pMpi2ConfigRequest_t)(mpt->m_req_frame +
216 	    (mpt->m_req_frame_size * cmd->cmd_slot));
217 	bzero(request, mpt->m_req_frame_size);
218 
219 	/*
220 	 * Form the request message.
221 	 */
222 	ddi_put8(mpt->m_acc_req_frame_hdl, &request->Function,
223 	    MPI2_FUNCTION_CONFIG);
224 	ddi_put8(mpt->m_acc_req_frame_hdl, &request->Action, config->action);
225 	direction = MPI2_SGE_FLAGS_IOC_TO_HOST;
226 	length = 0;
227 	sge = (pMpi2SGESimple64_t)&request->PageBufferSGE;
228 	if (config->action == MPI2_CONFIG_ACTION_PAGE_HEADER) {
229 		if (config->page_type > MPI2_CONFIG_PAGETYPE_MASK) {
230 			ddi_put8(mpt->m_acc_req_frame_hdl,
231 			    &request->Header.PageType,
232 			    MPI2_CONFIG_PAGETYPE_EXTENDED);
233 			ddi_put8(mpt->m_acc_req_frame_hdl,
234 			    &request->ExtPageType, config->page_type);
235 		} else {
236 			ddi_put8(mpt->m_acc_req_frame_hdl,
237 			    &request->Header.PageType, config->page_type);
238 		}
239 	} else {
240 		ddi_put8(mpt->m_acc_req_frame_hdl, &request->ExtPageType,
241 		    config->ext_page_type);
242 		ddi_put16(mpt->m_acc_req_frame_hdl, &request->ExtPageLength,
243 		    config->ext_page_length);
244 		ddi_put8(mpt->m_acc_req_frame_hdl, &request->Header.PageType,
245 		    config->page_type);
246 		ddi_put8(mpt->m_acc_req_frame_hdl, &request->Header.PageLength,
247 		    config->page_length);
248 		ddi_put8(mpt->m_acc_req_frame_hdl,
249 		    &request->Header.PageVersion, config->page_version);
250 		if ((config->page_type & MPI2_CONFIG_PAGETYPE_MASK) ==
251 		    MPI2_CONFIG_PAGETYPE_EXTENDED) {
252 			length = config->ext_page_length * 4;
253 		} else {
254 			length = config->page_length * 4;
255 		}
256 
257 		if (config->action == MPI2_CONFIG_ACTION_PAGE_WRITE_NVRAM) {
258 			direction = MPI2_SGE_FLAGS_HOST_TO_IOC;
259 		}
260 		ddi_put32(mpt->m_acc_req_frame_hdl, &sge->Address.Low,
261 		    (uint32_t)cmd->cmd_dma_addr);
262 		ddi_put32(mpt->m_acc_req_frame_hdl, &sge->Address.High,
263 		    (uint32_t)(cmd->cmd_dma_addr >> 32));
264 	}
265 	ddi_put8(mpt->m_acc_req_frame_hdl, &request->Header.PageNumber,
266 	    config->page_number);
267 	ddi_put32(mpt->m_acc_req_frame_hdl, &request->PageAddress,
268 	    config->page_address);
269 	flagslength = ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT |
270 	    MPI2_SGE_FLAGS_END_OF_BUFFER |
271 	    MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
272 	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
273 	    MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
274 	    direction |
275 	    MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
276 	flagslength |= length;
277 	ddi_put32(mpt->m_acc_req_frame_hdl, &sge->FlagsLength, flagslength);
278 
279 	(void) ddi_dma_sync(mpt->m_dma_req_frame_hdl, 0, 0,
280 	    DDI_DMA_SYNC_FORDEV);
281 	request_desc_low = (cmd->cmd_slot << 16) +
282 	    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
283 	cmd->cmd_rfm = NULL;
284 	MPTSAS_START_CMD(mpt, request_desc_low, 0);
285 	if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
286 	    DDI_SUCCESS) ||
287 	    (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
288 	    DDI_SUCCESS)) {
289 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
290 	}
291 }
292 
293 int
294 mptsas_access_config_page(mptsas_t *mpt, uint8_t action, uint8_t page_type,
295     uint8_t page_number, uint32_t page_address, int (*callback) (mptsas_t *,
296     caddr_t, ddi_acc_handle_t, uint16_t, uint32_t, va_list), ...)
297 {
298 	va_list			ap;
299 	ddi_dma_attr_t		attrs;
300 	uint_t			ncookie;
301 	ddi_dma_cookie_t	cookie;
302 	ddi_acc_handle_t	accessp;
303 	size_t			len = 0, alloc_len;
304 	mptsas_config_request_t	config;
305 	int			rval = DDI_SUCCESS, config_flags = 0;
306 	mptsas_cmd_t		*cmd;
307 	struct scsi_pkt		*pkt;
308 	pMpi2ConfigReply_t	reply;
309 	uint16_t		iocstatus = 0;
310 	uint32_t		iocloginfo;
311 	caddr_t			page_memp;
312 
313 	va_start(ap, callback);
314 	ASSERT(mutex_owned(&mpt->m_mutex));
315 
316 	/*
317 	 * Get a command from the pool.
318 	 */
319 	if ((rval = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
320 		mptsas_log(mpt, CE_NOTE, "command pool is full for config "
321 		    "page request");
322 		rval = DDI_FAILURE;
323 		goto page_done;
324 	}
325 	config_flags |= MPTSAS_REQUEST_POOL_CMD;
326 
327 	bzero((caddr_t)cmd, sizeof (*cmd));
328 	bzero((caddr_t)pkt, scsi_pkt_size());
329 	bzero((caddr_t)&config, sizeof (config));
330 
331 	/*
332 	 * Save the data for this request to be used in the call to start the
333 	 * config header request.
334 	 */
335 	config.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
336 	config.page_type = page_type;
337 	config.page_number = page_number;
338 	config.page_address = page_address;
339 
340 	/*
341 	 * Form a blank cmd/pkt to store the acknowledgement message
342 	 */
343 	pkt->pkt_ha_private	= (opaque_t)&config;
344 	pkt->pkt_flags		= FLAG_HEAD;
345 	pkt->pkt_time		= 60;
346 	cmd->cmd_pkt		= pkt;
347 	cmd->cmd_flags		= CFLAG_CMDIOC | CFLAG_CONFIG;
348 
349 	/*
350 	 * Save the config header request message in a slot.
351 	 */
352 	if (mptsas_save_cmd(mpt, cmd) == TRUE) {
353 		cmd->cmd_flags |= CFLAG_PREPARED;
354 		mptsas_start_config_page_access(mpt, cmd);
355 	} else {
356 		mptsas_waitq_add(mpt, cmd);
357 	}
358 
359 	/*
360 	 * If this is a request for a RAID info page, or any page called during
361 	 * the RAID info page request, poll because these config page requests
362 	 * are nested.  Poll to avoid data corruption due to one page's data
363 	 * overwriting the outer page request's data.  This can happen when
364 	 * the mutex is released in cv_wait.
365 	 */
366 	if ((page_type == MPI2_CONFIG_EXTPAGETYPE_RAID_CONFIG) ||
367 	    (page_type == MPI2_CONFIG_PAGETYPE_RAID_VOLUME) ||
368 	    (page_type == MPI2_CONFIG_PAGETYPE_RAID_PHYSDISK)) {
369 		(void) mptsas_poll(mpt, cmd, pkt->pkt_time * 1000);
370 	} else {
371 		while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
372 			cv_wait(&mpt->m_config_cv, &mpt->m_mutex);
373 		}
374 	}
375 
376 	/*
377 	 * Check if the header request completed without timing out
378 	 */
379 	if (cmd->cmd_flags & CFLAG_TIMEOUT) {
380 		mptsas_log(mpt, CE_WARN, "config header request timeout");
381 		rval = DDI_FAILURE;
382 		goto page_done;
383 	}
384 
385 	/*
386 	 * cmd_rfm points to the reply message if a reply was given.  Check the
387 	 * IOCStatus to make sure everything went OK with the header request.
388 	 */
389 	if (cmd->cmd_rfm) {
390 		config_flags |= MPTSAS_ADDRESS_REPLY;
391 		(void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
392 		    DDI_DMA_SYNC_FORCPU);
393 		reply = (pMpi2ConfigReply_t)(mpt->m_reply_frame + (cmd->cmd_rfm
394 		    - mpt->m_reply_frame_dma_addr));
395 		config.page_type = ddi_get8(mpt->m_acc_reply_frame_hdl,
396 		    &reply->Header.PageType);
397 		config.page_number = ddi_get8(mpt->m_acc_reply_frame_hdl,
398 		    &reply->Header.PageNumber);
399 		config.page_length = ddi_get8(mpt->m_acc_reply_frame_hdl,
400 		    &reply->Header.PageLength);
401 		config.page_version = ddi_get8(mpt->m_acc_reply_frame_hdl,
402 		    &reply->Header.PageVersion);
403 		config.ext_page_type = ddi_get8(mpt->m_acc_reply_frame_hdl,
404 		    &reply->ExtPageType);
405 		config.ext_page_length = ddi_get16(mpt->m_acc_reply_frame_hdl,
406 		    &reply->ExtPageLength);
407 
408 		iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
409 		    &reply->IOCStatus);
410 		iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
411 		    &reply->IOCLogInfo);
412 
413 		if (iocstatus) {
414 			NDBG13(("mptsas_access_config_page header: "
415 			    "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus,
416 			    iocloginfo));
417 			rval = DDI_FAILURE;
418 			goto page_done;
419 		}
420 
421 		if ((config.page_type & MPI2_CONFIG_PAGETYPE_MASK) ==
422 		    MPI2_CONFIG_PAGETYPE_EXTENDED)
423 			len = (config.ext_page_length * 4);
424 		else
425 			len = (config.page_length * 4);
426 
427 	}
428 
429 	if (pkt->pkt_reason == CMD_RESET) {
430 		mptsas_log(mpt, CE_WARN, "ioc reset abort config header "
431 		    "request");
432 		rval = DDI_FAILURE;
433 		goto page_done;
434 	}
435 
436 	/*
437 	 * Put the reply frame back on the free queue, increment the free
438 	 * index, and write the new index to the free index register.  But only
439 	 * if this reply is an ADDRESS reply.
440 	 */
441 	if (config_flags & MPTSAS_ADDRESS_REPLY) {
442 		ddi_put32(mpt->m_acc_free_queue_hdl,
443 		    &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
444 		    cmd->cmd_rfm);
445 		(void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
446 		    DDI_DMA_SYNC_FORDEV);
447 		if (++mpt->m_free_index == mpt->m_free_queue_depth) {
448 			mpt->m_free_index = 0;
449 		}
450 		ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
451 		    mpt->m_free_index);
452 		config_flags &= (~MPTSAS_ADDRESS_REPLY);
453 	}
454 
455 	/*
456 	 * Allocate DMA buffer here.  Store the info regarding this buffer in
457 	 * the cmd struct so that it can be used for this specific command and
458 	 * de-allocated after the command completes.  The size of the reply
459 	 * will not be larger than the reply frame size.
460 	 */
461 	attrs = mpt->m_msg_dma_attr;
462 	attrs.dma_attr_sgllen = 1;
463 	attrs.dma_attr_granular = (uint32_t)len;
464 
465 	if (ddi_dma_alloc_handle(mpt->m_dip, &attrs,
466 	    DDI_DMA_SLEEP, NULL, &cmd->cmd_dmahandle) != DDI_SUCCESS) {
467 		mptsas_log(mpt, CE_WARN, "unable to allocate dma handle for "
468 		    "config page.");
469 		rval = DDI_FAILURE;
470 		goto page_done;
471 	}
472 	if (ddi_dma_mem_alloc(cmd->cmd_dmahandle, len,
473 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
474 	    &page_memp, &alloc_len, &accessp) != DDI_SUCCESS) {
475 		ddi_dma_free_handle(&cmd->cmd_dmahandle);
476 		cmd->cmd_dmahandle = NULL;
477 		mptsas_log(mpt, CE_WARN, "unable to allocate config page "
478 		    "structure.");
479 		rval = DDI_FAILURE;
480 		goto page_done;
481 	}
482 
483 	if (ddi_dma_addr_bind_handle(cmd->cmd_dmahandle, NULL, page_memp,
484 	    alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
485 	    &cookie, &ncookie) != DDI_DMA_MAPPED) {
486 		(void) ddi_dma_mem_free(&accessp);
487 		ddi_dma_free_handle(&cmd->cmd_dmahandle);
488 		cmd->cmd_dmahandle = NULL;
489 		mptsas_log(mpt, CE_WARN, "unable to bind DMA resources for "
490 		    "config page.");
491 		rval = DDI_FAILURE;
492 		goto page_done;
493 	}
494 	cmd->cmd_dma_addr = cookie.dmac_laddress;
495 	bzero(page_memp, len);
496 
497 	/*
498 	 * Save the data for this request to be used in the call to start the
499 	 * config page read
500 	 */
501 	config.action = action;
502 	config.page_address = page_address;
503 
504 	/*
505 	 * Re-use the cmd that was used to get the header.  Reset some of the
506 	 * values.
507 	 */
508 	bzero((caddr_t)pkt, scsi_pkt_size());
509 	pkt->pkt_ha_private	= (opaque_t)&config;
510 	pkt->pkt_flags		= FLAG_HEAD;
511 	pkt->pkt_time		= 60;
512 	cmd->cmd_flags		= CFLAG_PREPARED | CFLAG_CMDIOC | CFLAG_CONFIG;
513 
514 	/*
515 	 * Send the config page request.  cmd is re-used from header request.
516 	 */
517 	mptsas_start_config_page_access(mpt, cmd);
518 
519 	/*
520 	 * If this is a request for a RAID info page, or any page called during
521 	 * the RAID info page request, poll because these config page requests
522 	 * are nested.  Poll to avoid data corruption due to one page's data
523 	 * overwriting the outer page request's data.  This can happen when
524 	 * the mutex is released in cv_wait.
525 	 */
526 	if ((page_type == MPI2_CONFIG_EXTPAGETYPE_RAID_CONFIG) ||
527 	    (page_type == MPI2_CONFIG_PAGETYPE_RAID_VOLUME) ||
528 	    (page_type == MPI2_CONFIG_PAGETYPE_RAID_PHYSDISK)) {
529 		(void) mptsas_poll(mpt, cmd, pkt->pkt_time * 1000);
530 	} else {
531 		while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
532 			cv_wait(&mpt->m_config_cv, &mpt->m_mutex);
533 		}
534 	}
535 
536 	/*
537 	 * Check if the request completed without timing out
538 	 */
539 	if (cmd->cmd_flags & CFLAG_TIMEOUT) {
540 		mptsas_log(mpt, CE_WARN, "config page request timeout");
541 		rval = DDI_FAILURE;
542 		goto page_done;
543 	}
544 
545 	/*
546 	 * cmd_rfm points to the reply message if a reply was given.  The reply
547 	 * frame and the config page are returned from this function in the
548 	 * param list.
549 	 */
550 	if (cmd->cmd_rfm) {
551 		config_flags |= MPTSAS_ADDRESS_REPLY;
552 		(void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
553 		    DDI_DMA_SYNC_FORCPU);
554 		(void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
555 		    DDI_DMA_SYNC_FORCPU);
556 		reply = (pMpi2ConfigReply_t)(mpt->m_reply_frame + (cmd->cmd_rfm
557 		    - mpt->m_reply_frame_dma_addr));
558 		iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
559 		    &reply->IOCStatus);
560 		iocstatus = MPTSAS_IOCSTATUS(iocstatus);
561 		iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
562 		    &reply->IOCLogInfo);
563 	}
564 
565 	if (callback(mpt, page_memp, accessp, iocstatus, iocloginfo, ap)) {
566 		rval = DDI_FAILURE;
567 		goto page_done;
568 	}
569 
570 	mptsas_fma_check(mpt, cmd);
571 	/*
572 	 * Check the DMA/ACC handles and then free the DMA buffer.
573 	 */
574 	if ((mptsas_check_dma_handle(cmd->cmd_dmahandle) != DDI_SUCCESS) ||
575 	    (mptsas_check_acc_handle(accessp) != DDI_SUCCESS)) {
576 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
577 		rval = DDI_FAILURE;
578 	}
579 
580 	if (pkt->pkt_reason == CMD_TRAN_ERR) {
581 		mptsas_log(mpt, CE_WARN, "config fma error");
582 		rval = DDI_FAILURE;
583 		goto page_done;
584 	}
585 	if (pkt->pkt_reason == CMD_RESET) {
586 		mptsas_log(mpt, CE_WARN, "ioc reset abort config request");
587 		rval = DDI_FAILURE;
588 		goto page_done;
589 	}
590 
591 page_done:
592 	va_end(ap);
593 	/*
594 	 * Put the reply frame back on the free queue, increment the free
595 	 * index, and write the new index to the free index register.  But only
596 	 * if this reply is an ADDRESS reply.
597 	 */
598 	if (config_flags & MPTSAS_ADDRESS_REPLY) {
599 		ddi_put32(mpt->m_acc_free_queue_hdl,
600 		    &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
601 		    cmd->cmd_rfm);
602 		(void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
603 		    DDI_DMA_SYNC_FORDEV);
604 		if (++mpt->m_free_index == mpt->m_free_queue_depth) {
605 			mpt->m_free_index = 0;
606 		}
607 		ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
608 		    mpt->m_free_index);
609 	}
610 
611 	if (cmd->cmd_dmahandle != NULL) {
612 		(void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
613 		(void) ddi_dma_mem_free(&accessp);
614 		ddi_dma_free_handle(&cmd->cmd_dmahandle);
615 	}
616 
617 	if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
618 		mptsas_remove_cmd(mpt, cmd);
619 		config_flags &= (~MPTSAS_REQUEST_POOL_CMD);
620 	}
621 	if (config_flags & MPTSAS_REQUEST_POOL_CMD)
622 		mptsas_return_to_pool(mpt, cmd);
623 
624 	if (config_flags & MPTSAS_CMD_TIMEOUT) {
625 		if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
626 			mptsas_log(mpt, CE_WARN, "mptsas_restart_ioc failed");
627 		}
628 	}
629 
630 	return (rval);
631 }
632 
633 int
634 mptsas_send_config_request_msg(mptsas_t *mpt, uint8_t action, uint8_t pagetype,
635 	uint32_t pageaddress, uint8_t pagenumber, uint8_t pageversion,
636 	uint8_t pagelength, uint32_t SGEflagslength, uint32_t SGEaddress32)
637 {
638 	pMpi2ConfigRequest_t	config;
639 	int			send_numbytes;
640 
641 	bzero(mpt->m_hshk_memp, sizeof (MPI2_CONFIG_REQUEST));
642 	config = (pMpi2ConfigRequest_t)mpt->m_hshk_memp;
643 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Function, MPI2_FUNCTION_CONFIG);
644 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Action, action);
645 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageNumber, pagenumber);
646 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageType, pagetype);
647 	ddi_put32(mpt->m_hshk_acc_hdl, &config->PageAddress, pageaddress);
648 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageVersion, pageversion);
649 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageLength, pagelength);
650 	ddi_put32(mpt->m_hshk_acc_hdl,
651 	    &config->PageBufferSGE.MpiSimple.FlagsLength, SGEflagslength);
652 	ddi_put32(mpt->m_hshk_acc_hdl,
653 	    &config->PageBufferSGE.MpiSimple.u.Address32, SGEaddress32);
654 	send_numbytes = sizeof (MPI2_CONFIG_REQUEST);
655 
656 	/*
657 	 * Post message via handshake
658 	 */
659 	if (mptsas_send_handshake_msg(mpt, (caddr_t)config, send_numbytes,
660 	    mpt->m_hshk_acc_hdl)) {
661 		return (-1);
662 	}
663 	return (0);
664 }
665 
666 int
667 mptsas_send_extended_config_request_msg(mptsas_t *mpt, uint8_t action,
668 	uint8_t extpagetype, uint32_t pageaddress, uint8_t pagenumber,
669 	uint8_t pageversion, uint16_t extpagelength,
670 	uint32_t SGEflagslength, uint32_t SGEaddress32)
671 {
672 	pMpi2ConfigRequest_t	config;
673 	int			send_numbytes;
674 
675 	bzero(mpt->m_hshk_memp, sizeof (MPI2_CONFIG_REQUEST));
676 	config = (pMpi2ConfigRequest_t)mpt->m_hshk_memp;
677 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Function, MPI2_FUNCTION_CONFIG);
678 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Action, action);
679 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageNumber, pagenumber);
680 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageType,
681 	    MPI2_CONFIG_PAGETYPE_EXTENDED);
682 	ddi_put8(mpt->m_hshk_acc_hdl, &config->ExtPageType, extpagetype);
683 	ddi_put32(mpt->m_hshk_acc_hdl, &config->PageAddress, pageaddress);
684 	ddi_put8(mpt->m_hshk_acc_hdl, &config->Header.PageVersion, pageversion);
685 	ddi_put16(mpt->m_hshk_acc_hdl, &config->ExtPageLength, extpagelength);
686 	ddi_put32(mpt->m_hshk_acc_hdl,
687 	    &config->PageBufferSGE.MpiSimple.FlagsLength, SGEflagslength);
688 	ddi_put32(mpt->m_hshk_acc_hdl,
689 	    &config->PageBufferSGE.MpiSimple.u.Address32, SGEaddress32);
690 	send_numbytes = sizeof (MPI2_CONFIG_REQUEST);
691 
692 	/*
693 	 * Post message via handshake
694 	 */
695 	if (mptsas_send_handshake_msg(mpt, (caddr_t)config, send_numbytes,
696 	    mpt->m_hshk_acc_hdl)) {
697 		return (-1);
698 	}
699 	return (0);
700 }
701 
702 int
703 mptsas_ioc_wait_for_response(mptsas_t *mpt)
704 {
705 	int	polls = 0;
706 
707 	while ((ddi_get32(mpt->m_datap,
708 	    &mpt->m_reg->HostInterruptStatus) & MPI2_HIS_IOP_DOORBELL_STATUS)) {
709 		drv_usecwait(1000);
710 		if (polls++ > 60000) {
711 			return (-1);
712 		}
713 	}
714 	return (0);
715 }
716 
717 int
718 mptsas_ioc_wait_for_doorbell(mptsas_t *mpt)
719 {
720 	int	polls = 0;
721 
722 	while ((ddi_get32(mpt->m_datap,
723 	    &mpt->m_reg->HostInterruptStatus) & MPI2_HIM_DIM) == 0) {
724 		drv_usecwait(1000);
725 		if (polls++ > 300000) {
726 			return (-1);
727 		}
728 	}
729 	return (0);
730 }
731 
732 int
733 mptsas_send_handshake_msg(mptsas_t *mpt, caddr_t memp, int numbytes,
734 	ddi_acc_handle_t accessp)
735 {
736 	int	i;
737 
738 	/*
739 	 * clean pending doorbells
740 	 */
741 	ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptStatus, 0);
742 	ddi_put32(mpt->m_datap, &mpt->m_reg->Doorbell,
743 	    ((MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) |
744 	    ((numbytes / 4) << MPI2_DOORBELL_ADD_DWORDS_SHIFT)));
745 
746 	if (mptsas_ioc_wait_for_doorbell(mpt)) {
747 		NDBG19(("mptsas_send_handshake failed.  Doorbell not ready\n"));
748 		return (-1);
749 	}
750 
751 	/*
752 	 * clean pending doorbells again
753 	 */
754 	ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptStatus, 0);
755 
756 	if (mptsas_ioc_wait_for_response(mpt)) {
757 		NDBG19(("mptsas_send_handshake failed.  Doorbell not "
758 		    "cleared\n"));
759 		return (-1);
760 	}
761 
762 	/*
763 	 * post handshake message
764 	 */
765 	for (i = 0; (i < numbytes / 4); i++, memp += 4) {
766 		ddi_put32(mpt->m_datap, &mpt->m_reg->Doorbell,
767 		    ddi_get32(accessp, (uint32_t *)((void *)(memp))));
768 		if (mptsas_ioc_wait_for_response(mpt)) {
769 			NDBG19(("mptsas_send_handshake failed posting "
770 			    "message\n"));
771 			return (-1);
772 		}
773 	}
774 
775 	if (mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) {
776 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
777 		ddi_fm_acc_err_clear(mpt->m_datap, DDI_FME_VER0);
778 		return (-1);
779 	}
780 
781 	return (0);
782 }
783 
784 int
785 mptsas_get_handshake_msg(mptsas_t *mpt, caddr_t memp, int numbytes,
786 	ddi_acc_handle_t accessp)
787 {
788 	int		i, totalbytes, bytesleft;
789 	uint16_t	val;
790 
791 	/*
792 	 * wait for doorbell
793 	 */
794 	if (mptsas_ioc_wait_for_doorbell(mpt)) {
795 		NDBG19(("mptsas_get_handshake failed.  Doorbell not ready\n"));
796 		return (-1);
797 	}
798 
799 	/*
800 	 * get first 2 bytes of handshake message to determine how much
801 	 * data we will be getting
802 	 */
803 	for (i = 0; i < 2; i++, memp += 2) {
804 		val = (ddi_get32(mpt->m_datap,
805 		    &mpt->m_reg->Doorbell) & MPI2_DOORBELL_DATA_MASK);
806 		ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptStatus, 0);
807 		if (mptsas_ioc_wait_for_doorbell(mpt)) {
808 			NDBG19(("mptsas_get_handshake failure getting initial"
809 			    " data\n"));
810 			return (-1);
811 		}
812 		ddi_put16(accessp, (uint16_t *)((void *)(memp)), val);
813 		if (i == 1) {
814 			totalbytes = (val & 0xFF) * 2;
815 		}
816 	}
817 
818 	/*
819 	 * If we are expecting less bytes than the message wants to send
820 	 * we simply save as much as we expected and then throw out the rest
821 	 * later
822 	 */
823 	if (totalbytes > (numbytes / 2)) {
824 		bytesleft = ((numbytes / 2) - 2);
825 	} else {
826 		bytesleft = (totalbytes - 2);
827 	}
828 
829 	/*
830 	 * Get the rest of the data
831 	 */
832 	for (i = 0; i < bytesleft; i++, memp += 2) {
833 		val = (ddi_get32(mpt->m_datap,
834 		    &mpt->m_reg->Doorbell) & MPI2_DOORBELL_DATA_MASK);
835 		ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptStatus, 0);
836 		if (mptsas_ioc_wait_for_doorbell(mpt)) {
837 			NDBG19(("mptsas_get_handshake failure getting"
838 			    " main data\n"));
839 			return (-1);
840 		}
841 		ddi_put16(accessp, (uint16_t *)((void *)(memp)), val);
842 	}
843 
844 	/*
845 	 * Sometimes the device will send more data than is expected
846 	 * This data is not used by us but needs to be cleared from
847 	 * ioc doorbell.  So we just read the values and throw
848 	 * them out.
849 	 */
850 	if (totalbytes > (numbytes / 2)) {
851 		for (i = (numbytes / 2); i < totalbytes; i++) {
852 			val = (ddi_get32(mpt->m_datap,
853 			    &mpt->m_reg->Doorbell) &
854 			    MPI2_DOORBELL_DATA_MASK);
855 			ddi_put32(mpt->m_datap,
856 			    &mpt->m_reg->HostInterruptStatus, 0);
857 			if (mptsas_ioc_wait_for_doorbell(mpt)) {
858 				NDBG19(("mptsas_get_handshake failure getting "
859 				    "extra garbage data\n"));
860 				return (-1);
861 			}
862 		}
863 	}
864 
865 	ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptStatus, 0);
866 
867 	if (mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) {
868 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
869 		ddi_fm_acc_err_clear(mpt->m_datap, DDI_FME_VER0);
870 		return (-1);
871 	}
872 
873 	return (0);
874 }
875 
876 int
877 mptsas_kick_start(mptsas_t *mpt)
878 {
879 	int		polls = 0;
880 	uint32_t	diag_reg, ioc_state, saved_HCB_size;
881 
882 	/*
883 	 * Start a hard reset.  Write magic number and wait 900 uSeconds.
884 	 */
885 	MPTSAS_ENABLE_DRWE(mpt);
886 	drv_usecwait(900);
887 
888 	/*
889 	 * Read the current Diag Reg and save the Host Controlled Boot size.
890 	 */
891 	diag_reg = ddi_get32(mpt->m_datap, &mpt->m_reg->HostDiagnostic);
892 	saved_HCB_size = ddi_get32(mpt->m_datap, &mpt->m_reg->HCBSize);
893 
894 	/*
895 	 * Set Reset Adapter bit and wait 50 mSeconds.
896 	 */
897 	diag_reg |= MPI2_DIAG_RESET_ADAPTER;
898 	ddi_put32(mpt->m_datap, &mpt->m_reg->HostDiagnostic, diag_reg);
899 	drv_usecwait(50000);
900 
901 	/*
902 	 * Poll, waiting for Reset Adapter bit to clear.  300 Seconds max
903 	 * (600000 * 500 = 300,000,000 uSeconds, 300 seconds).
904 	 * If no more adapter (all FF's), just return failure.
905 	 */
906 	for (polls = 0; polls < 600000; polls++) {
907 		diag_reg = ddi_get32(mpt->m_datap,
908 		    &mpt->m_reg->HostDiagnostic);
909 		if (diag_reg == 0xFFFFFFFF) {
910 			mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
911 			ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
912 			return (DDI_FAILURE);
913 		}
914 		if (!(diag_reg & MPI2_DIAG_RESET_ADAPTER)) {
915 			break;
916 		}
917 		drv_usecwait(500);
918 	}
919 	if (polls == 600000) {
920 		mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
921 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
922 		return (DDI_FAILURE);
923 	}
924 
925 	/*
926 	 * Check if adapter is in Host Boot Mode.  If so, restart adapter
927 	 * assuming the HCB points to good FW.
928 	 * Set BootDeviceSel to HCDW (Host Code and Data Window).
929 	 */
930 	if (diag_reg & MPI2_DIAG_HCB_MODE) {
931 		diag_reg &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
932 		diag_reg |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
933 		ddi_put32(mpt->m_datap, &mpt->m_reg->HostDiagnostic, diag_reg);
934 
935 		/*
936 		 * Re-enable the HCDW.
937 		 */
938 		ddi_put32(mpt->m_datap, &mpt->m_reg->HCBSize,
939 		    (saved_HCB_size | MPI2_HCB_SIZE_HCB_ENABLE));
940 	}
941 
942 	/*
943 	 * Restart the adapter.
944 	 */
945 	diag_reg &= ~MPI2_DIAG_HOLD_IOC_RESET;
946 	ddi_put32(mpt->m_datap, &mpt->m_reg->HostDiagnostic, diag_reg);
947 
948 	/*
949 	 * Disable writes to the Host Diag register.
950 	 */
951 	ddi_put32(mpt->m_datap, &mpt->m_reg->WriteSequence,
952 	    MPI2_WRSEQ_FLUSH_KEY_VALUE);
953 
954 	/*
955 	 * Wait 60 seconds max for FW to come to ready state.
956 	 */
957 	for (polls = 0; polls < 60000; polls++) {
958 		ioc_state = ddi_get32(mpt->m_datap, &mpt->m_reg->Doorbell);
959 		if (ioc_state == 0xFFFFFFFF) {
960 			mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
961 			ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
962 			return (DDI_FAILURE);
963 		}
964 		if ((ioc_state & MPI2_IOC_STATE_MASK) ==
965 		    MPI2_IOC_STATE_READY) {
966 			break;
967 		}
968 		drv_usecwait(1000);
969 	}
970 	if (polls == 60000) {
971 		mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
972 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
973 		return (DDI_FAILURE);
974 	}
975 
976 	/*
977 	 * Clear the ioc ack events queue.
978 	 */
979 	mptsas_destroy_ioc_event_cmd(mpt);
980 
981 	return (DDI_SUCCESS);
982 }
983 
984 int
985 mptsas_ioc_reset(mptsas_t *mpt)
986 {
987 #ifdef SLM
988 	int		polls = 0;
989 	uint32_t	reset_msg;
990 
991 #endif
992 	uint32_t	ioc_state;
993 	ioc_state = ddi_get32(mpt->m_datap, &mpt->m_reg->Doorbell);
994 	/*
995 	 * If chip is already in ready state then there is nothing to do.
996 	 */
997 	if (ioc_state == MPI2_IOC_STATE_READY) {
998 		return (MPTSAS_NO_RESET);
999 	}
1000 
1001 /*
1002  * SLM-test; skip MUR for now
1003  */
1004 #ifdef SLM
1005 	/*
1006 	 * If the chip is already operational, we just need to send
1007 	 * it a message unit reset to put it back in the ready state
1008 	 */
1009 	if (ioc_state & MPI2_IOC_STATE_OPERATIONAL) {
1010 		reset_msg = MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET;
1011 		ddi_put32(mpt->m_datap, &mpt->m_reg->Doorbell,
1012 		    (reset_msg << MPI2_DOORBELL_FUNCTION_SHIFT));
1013 		if (mptsas_ioc_wait_for_response(mpt)) {
1014 			NDBG19(("mptsas_ioc_reset failure sending "
1015 			    "message_unit_reset\n"));
1016 			goto hard_reset;
1017 		}
1018 
1019 		/*
1020 		 * Wait no more than 60 seconds for chip to become ready.
1021 		 */
1022 		while ((ddi_get32(mpt->m_datap, &mpt->m_reg->Doorbell) &
1023 		    MPI2_IOC_STATE_READY) == 0x0) {
1024 			drv_usecwait(1000);
1025 			if (polls++ > 60000) {
1026 				goto hard_reset;
1027 			}
1028 		}
1029 		/*
1030 		 * the message unit reset would do reset operations
1031 		 * clear reply and request queue, so we should clear
1032 		 * ACK event cmd.
1033 		 */
1034 		mptsas_destroy_ioc_event_cmd(mpt);
1035 		return (MPTSAS_NO_RESET);
1036 	}
1037 
1038 hard_reset:
1039 #endif
1040 	if (mptsas_kick_start(mpt) == DDI_FAILURE) {
1041 		mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
1042 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
1043 		return (MPTSAS_RESET_FAIL);
1044 	}
1045 	return (MPTSAS_SUCCESS_HARDRESET);
1046 }
1047 
1048 
1049 int
1050 mptsas_request_from_pool(mptsas_t *mpt, mptsas_cmd_t **cmd,
1051     struct scsi_pkt **pkt)
1052 {
1053 	m_event_struct_t	*ioc_cmd = NULL;
1054 
1055 	ioc_cmd = kmem_zalloc(M_EVENT_STRUCT_SIZE, KM_SLEEP);
1056 	if (ioc_cmd == NULL) {
1057 		return (DDI_FAILURE);
1058 	}
1059 	ioc_cmd->m_event_linkp = NULL;
1060 	mptsas_ioc_event_cmdq_add(mpt, ioc_cmd);
1061 	*cmd = &(ioc_cmd->m_event_cmd);
1062 	*pkt = &(ioc_cmd->m_event_pkt);
1063 
1064 	return (DDI_SUCCESS);
1065 }
1066 
1067 void
1068 mptsas_return_to_pool(mptsas_t *mpt, mptsas_cmd_t *cmd)
1069 {
1070 	m_event_struct_t	*ioc_cmd = NULL;
1071 
1072 	ioc_cmd = mptsas_ioc_event_find_by_cmd(mpt, cmd);
1073 	if (ioc_cmd == NULL) {
1074 		return;
1075 	}
1076 
1077 	mptsas_ioc_event_cmdq_delete(mpt, ioc_cmd);
1078 	kmem_free(ioc_cmd, M_EVENT_STRUCT_SIZE);
1079 	ioc_cmd = NULL;
1080 }
1081 
1082 /*
1083  * NOTE: We should be able to queue TM requests in the controller to make this
1084  * a lot faster.  If resetting all targets, for example, we can load the hi
1085  * priority queue with its limit and the controller will reply as they are
1086  * completed.  This way, we don't have to poll for one reply at a time.
1087  * Think about enhancing this later.
1088  */
1089 int
1090 mptsas_ioc_task_management(mptsas_t *mpt, int task_type, uint16_t dev_handle,
1091 	int lun, uint8_t *reply, uint32_t reply_size, int mode)
1092 {
1093 	/*
1094 	 * In order to avoid allocating variables on the stack,
1095 	 * we make use of the pre-existing mptsas_cmd_t and
1096 	 * scsi_pkt which are included in the mptsas_t which
1097 	 * is passed to this routine.
1098 	 */
1099 
1100 	pMpi2SCSITaskManagementRequest_t	task;
1101 	int					rval = FALSE;
1102 	mptsas_cmd_t				*cmd;
1103 	struct scsi_pkt				*pkt;
1104 	mptsas_slots_t				*slots = mpt->m_active;
1105 	uint32_t				request_desc_low, i;
1106 	pMPI2DefaultReply_t			reply_msg;
1107 
1108 	/*
1109 	 * Can't start another task management routine.
1110 	 */
1111 	if (slots->m_slot[MPTSAS_TM_SLOT(mpt)] != NULL) {
1112 		mptsas_log(mpt, CE_WARN, "Can only start 1 task management"
1113 		    " command at a time\n");
1114 		return (FALSE);
1115 	}
1116 
1117 	cmd = &(mpt->m_event_task_mgmt.m_event_cmd);
1118 	pkt = &(mpt->m_event_task_mgmt.m_event_pkt);
1119 
1120 	bzero((caddr_t)cmd, sizeof (*cmd));
1121 	bzero((caddr_t)pkt, scsi_pkt_size());
1122 
1123 	pkt->pkt_cdbp		= (opaque_t)&cmd->cmd_cdb[0];
1124 	pkt->pkt_scbp		= (opaque_t)&cmd->cmd_scb;
1125 	pkt->pkt_ha_private	= (opaque_t)cmd;
1126 	pkt->pkt_flags		= (FLAG_NOINTR | FLAG_HEAD);
1127 	pkt->pkt_time		= 60;
1128 	pkt->pkt_address.a_target = dev_handle;
1129 	pkt->pkt_address.a_lun = (uchar_t)lun;
1130 	cmd->cmd_pkt		= pkt;
1131 	cmd->cmd_scblen		= 1;
1132 	cmd->cmd_flags		= CFLAG_TM_CMD;
1133 	cmd->cmd_slot		= MPTSAS_TM_SLOT(mpt);
1134 
1135 	slots->m_slot[MPTSAS_TM_SLOT(mpt)] = cmd;
1136 
1137 	/*
1138 	 * Store the TM message in memory location corresponding to the TM slot
1139 	 * number.
1140 	 */
1141 	task = (pMpi2SCSITaskManagementRequest_t)(mpt->m_req_frame +
1142 	    (mpt->m_req_frame_size * cmd->cmd_slot));
1143 	bzero(task, mpt->m_req_frame_size);
1144 
1145 	/*
1146 	 * form message for requested task
1147 	 */
1148 	mptsas_init_std_hdr(mpt->m_acc_req_frame_hdl, task, dev_handle, lun, 0,
1149 	    MPI2_FUNCTION_SCSI_TASK_MGMT);
1150 
1151 	/*
1152 	 * Set the task type
1153 	 */
1154 	ddi_put8(mpt->m_acc_req_frame_hdl, &task->TaskType, task_type);
1155 
1156 	/*
1157 	 * Send TM request using High Priority Queue.
1158 	 */
1159 	(void) ddi_dma_sync(mpt->m_dma_req_frame_hdl, 0, 0,
1160 	    DDI_DMA_SYNC_FORDEV);
1161 	request_desc_low = (cmd->cmd_slot << 16) +
1162 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1163 	MPTSAS_START_CMD(mpt, request_desc_low, 0);
1164 	rval = mptsas_poll(mpt, cmd, MPTSAS_POLL_TIME);
1165 
1166 	if (pkt->pkt_reason == CMD_INCOMPLETE)
1167 		rval = FALSE;
1168 
1169 	/*
1170 	 * If a reply frame was used and there is a reply buffer to copy the
1171 	 * reply data into, copy it.  If this fails, log a message, but don't
1172 	 * fail the TM request.
1173 	 */
1174 	if (cmd->cmd_rfm && reply) {
1175 		(void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
1176 		    DDI_DMA_SYNC_FORCPU);
1177 		reply_msg = (pMPI2DefaultReply_t)
1178 		    (mpt->m_reply_frame + (cmd->cmd_rfm -
1179 		    mpt->m_reply_frame_dma_addr));
1180 		if (reply_size > sizeof (MPI2_SCSI_TASK_MANAGE_REPLY)) {
1181 			reply_size = sizeof (MPI2_SCSI_TASK_MANAGE_REPLY);
1182 		}
1183 		mutex_exit(&mpt->m_mutex);
1184 		for (i = 0; i < reply_size; i++) {
1185 			if (ddi_copyout((uint8_t *)reply_msg + i, reply + i, 1,
1186 			    mode)) {
1187 				mptsas_log(mpt, CE_WARN, "failed to copy out "
1188 				    "reply data for TM request");
1189 				break;
1190 			}
1191 		}
1192 		mutex_enter(&mpt->m_mutex);
1193 	}
1194 
1195 	/*
1196 	 * clear the TM slot before returning
1197 	 */
1198 	slots->m_slot[MPTSAS_TM_SLOT(mpt)] = NULL;
1199 
1200 	/*
1201 	 * If we lost our task management command
1202 	 * we need to reset the ioc
1203 	 */
1204 	if (rval == FALSE) {
1205 		mptsas_log(mpt, CE_WARN, "mptsas_ioc_task_management failed "
1206 		    "try to reset ioc to recovery!");
1207 		if (mptsas_restart_ioc(mpt)) {
1208 			mptsas_log(mpt, CE_WARN, "mptsas_restart_ioc failed");
1209 			rval = FAILED;
1210 		}
1211 	}
1212 
1213 	return (rval);
1214 }
1215 
1216 int
1217 mptsas_update_flash(mptsas_t *mpt, caddr_t ptrbuffer, uint32_t size,
1218     uint8_t type, int mode)
1219 {
1220 
1221 	/*
1222 	 * In order to avoid allocating variables on the stack,
1223 	 * we make use of the pre-existing mptsas_cmd_t and
1224 	 * scsi_pkt which are included in the mptsas_t which
1225 	 * is passed to this routine.
1226 	 */
1227 
1228 	ddi_dma_attr_t		flsh_dma_attrs;
1229 	uint_t			flsh_ncookie;
1230 	ddi_dma_cookie_t	flsh_cookie;
1231 	ddi_dma_handle_t	flsh_dma_handle;
1232 	ddi_acc_handle_t	flsh_accessp;
1233 	size_t			flsh_alloc_len;
1234 	caddr_t			memp, flsh_memp;
1235 	uint32_t		flagslength;
1236 	pMpi2FWDownloadRequest	fwdownload;
1237 	pMpi2FWDownloadTCSGE_t	tcsge;
1238 	pMpi2SGESimple64_t	sge;
1239 	mptsas_cmd_t		*cmd;
1240 	struct scsi_pkt		*pkt;
1241 	int			i;
1242 	int			rvalue = 0;
1243 	uint32_t		request_desc_low;
1244 
1245 	if ((rvalue = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
1246 		mptsas_log(mpt, CE_WARN, "mptsas_update_flash(): allocation "
1247 		    "failed. event ack command pool is full\n");
1248 		return (rvalue);
1249 	}
1250 
1251 	bzero((caddr_t)cmd, sizeof (*cmd));
1252 	bzero((caddr_t)pkt, scsi_pkt_size());
1253 	cmd->ioc_cmd_slot = (uint32_t)rvalue;
1254 
1255 	/*
1256 	 * dynamically create a customized dma attribute structure
1257 	 * that describes the flash file.
1258 	 */
1259 	flsh_dma_attrs = mpt->m_msg_dma_attr;
1260 	flsh_dma_attrs.dma_attr_sgllen = 1;
1261 
1262 	if (ddi_dma_alloc_handle(mpt->m_dip, &flsh_dma_attrs,
1263 	    DDI_DMA_SLEEP, NULL, &flsh_dma_handle) != DDI_SUCCESS) {
1264 		mptsas_log(mpt, CE_WARN,
1265 		    "(unable to allocate dma handle.");
1266 		mptsas_return_to_pool(mpt, cmd);
1267 		return (-1);
1268 	}
1269 
1270 	if (ddi_dma_mem_alloc(flsh_dma_handle, size,
1271 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1272 	    &flsh_memp, &flsh_alloc_len, &flsh_accessp) != DDI_SUCCESS) {
1273 		ddi_dma_free_handle(&flsh_dma_handle);
1274 		mptsas_log(mpt, CE_WARN,
1275 		    "unable to allocate flash structure.");
1276 		mptsas_return_to_pool(mpt, cmd);
1277 		return (-1);
1278 	}
1279 
1280 	if (ddi_dma_addr_bind_handle(flsh_dma_handle, NULL, flsh_memp,
1281 	    flsh_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1282 	    NULL, &flsh_cookie, &flsh_ncookie) != DDI_DMA_MAPPED) {
1283 		(void) ddi_dma_mem_free(&flsh_accessp);
1284 		ddi_dma_free_handle(&flsh_dma_handle);
1285 		mptsas_log(mpt, CE_WARN, "unable to bind DMA resources.");
1286 		mptsas_return_to_pool(mpt, cmd);
1287 		return (-1);
1288 	}
1289 	bzero(flsh_memp, size);
1290 
1291 	for (i = 0; i < size; i++) {
1292 		(void) ddi_copyin(ptrbuffer + i, flsh_memp + i, 1, mode);
1293 	}
1294 	(void) ddi_dma_sync(flsh_dma_handle, 0, 0, DDI_DMA_SYNC_FORDEV);
1295 
1296 	/*
1297 	 * form a cmd/pkt to store the fw download message
1298 	 */
1299 	pkt->pkt_cdbp		= (opaque_t)&cmd->cmd_cdb[0];
1300 	pkt->pkt_scbp		= (opaque_t)&cmd->cmd_scb;
1301 	pkt->pkt_ha_private	= (opaque_t)cmd;
1302 	pkt->pkt_flags		= FLAG_HEAD;
1303 	pkt->pkt_time		= 60;
1304 	cmd->cmd_pkt		= pkt;
1305 	cmd->cmd_scblen		= 1;
1306 	cmd->cmd_flags		= CFLAG_CMDIOC | CFLAG_FW_CMD;
1307 
1308 	/*
1309 	 * Save the command in a slot
1310 	 */
1311 	if (mptsas_save_cmd(mpt, cmd) == FALSE) {
1312 		(void) ddi_dma_unbind_handle(flsh_dma_handle);
1313 		(void) ddi_dma_mem_free(&flsh_accessp);
1314 		ddi_dma_free_handle(&flsh_dma_handle);
1315 		mptsas_return_to_pool(mpt, cmd);
1316 		return (-1);
1317 	}
1318 
1319 	/*
1320 	 * Fill in fw download message
1321 	 */
1322 	ASSERT(cmd->cmd_slot != 0);
1323 	memp = mpt->m_req_frame + (mpt->m_req_frame_size * cmd->cmd_slot);
1324 	bzero(memp, mpt->m_req_frame_size);
1325 	fwdownload = (void *)memp;
1326 	ddi_put8(mpt->m_acc_req_frame_hdl, &fwdownload->Function,
1327 	    MPI2_FUNCTION_FW_DOWNLOAD);
1328 	ddi_put8(mpt->m_acc_req_frame_hdl, &fwdownload->ImageType, type);
1329 	ddi_put8(mpt->m_acc_req_frame_hdl, &fwdownload->MsgFlags,
1330 	    MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT);
1331 	ddi_put32(mpt->m_acc_req_frame_hdl, &fwdownload->TotalImageSize, size);
1332 
1333 	tcsge = (pMpi2FWDownloadTCSGE_t)&fwdownload->SGL;
1334 	ddi_put8(mpt->m_acc_req_frame_hdl, &tcsge->ContextSize, 0);
1335 	ddi_put8(mpt->m_acc_req_frame_hdl, &tcsge->DetailsLength, 12);
1336 	ddi_put8(mpt->m_acc_req_frame_hdl, &tcsge->Flags, 0);
1337 	ddi_put32(mpt->m_acc_req_frame_hdl, &tcsge->ImageOffset, 0);
1338 	ddi_put32(mpt->m_acc_req_frame_hdl, &tcsge->ImageSize, size);
1339 
1340 	sge = (pMpi2SGESimple64_t)(tcsge + 1);
1341 	flagslength = size;
1342 	flagslength |= ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT |
1343 	    MPI2_SGE_FLAGS_END_OF_BUFFER |
1344 	    MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1345 	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
1346 	    MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1347 	    MPI2_SGE_FLAGS_HOST_TO_IOC |
1348 	    MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
1349 	ddi_put32(mpt->m_acc_req_frame_hdl, &sge->FlagsLength, flagslength);
1350 	ddi_put32(mpt->m_acc_req_frame_hdl, &sge->Address.Low,
1351 	    flsh_cookie.dmac_address);
1352 	ddi_put32(mpt->m_acc_req_frame_hdl, &sge->Address.High,
1353 	    (uint32_t)(flsh_cookie.dmac_laddress >> 32));
1354 
1355 	/*
1356 	 * Start command
1357 	 */
1358 	(void) ddi_dma_sync(mpt->m_dma_req_frame_hdl, 0, 0,
1359 	    DDI_DMA_SYNC_FORDEV);
1360 	request_desc_low = (cmd->cmd_slot << 16) +
1361 	    MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1362 	cmd->cmd_rfm = NULL;
1363 	MPTSAS_START_CMD(mpt, request_desc_low, 0);
1364 
1365 	rvalue = 0;
1366 	(void) cv_reltimedwait(&mpt->m_fw_cv, &mpt->m_mutex,
1367 	    drv_usectohz(60 * MICROSEC), TR_CLOCK_TICK);
1368 	if (!(cmd->cmd_flags & CFLAG_FINISHED)) {
1369 		if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
1370 			mptsas_log(mpt, CE_WARN, "mptsas_restart_ioc failed");
1371 		}
1372 		rvalue = -1;
1373 	}
1374 	mptsas_remove_cmd(mpt, cmd);
1375 
1376 	(void) ddi_dma_unbind_handle(flsh_dma_handle);
1377 	(void) ddi_dma_mem_free(&flsh_accessp);
1378 	ddi_dma_free_handle(&flsh_dma_handle);
1379 
1380 	return (rvalue);
1381 }
1382 
1383 static int
1384 mptsas_sasdevpage_0_cb(mptsas_t *mpt, caddr_t page_memp,
1385     ddi_acc_handle_t accessp, uint16_t iocstatus, uint32_t iocloginfo,
1386     va_list ap)
1387 {
1388 #ifndef __lock_lint
1389 	_NOTE(ARGUNUSED(ap))
1390 #endif
1391 	pMpi2SasDevicePage0_t	sasdevpage;
1392 	int			rval = DDI_SUCCESS, i;
1393 	uint8_t			*sas_addr = NULL;
1394 	uint8_t			tmp_sas_wwn[SAS_WWN_BYTE_SIZE];
1395 	uint16_t		*devhdl;
1396 	uint64_t		*sas_wwn;
1397 	uint32_t		*dev_info;
1398 	uint8_t			*physport, *phynum;
1399 	uint32_t		page_address;
1400 
1401 	if ((iocstatus != MPI2_IOCSTATUS_SUCCESS) &&
1402 	    (iocstatus != MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)) {
1403 		mptsas_log(mpt, CE_WARN, "mptsas_get_sas_device_page0 "
1404 		    "header: IOCStatus=0x%x, IOCLogInfo=0x%x",
1405 		    iocstatus, iocloginfo);
1406 		rval = DDI_FAILURE;
1407 		return (rval);
1408 	}
1409 	page_address = va_arg(ap, uint32_t);
1410 	/*
1411 	 * The INVALID_PAGE status is normal if using GET_NEXT_HANDLE and there
1412 	 * are no more pages.  If everything is OK up to this point but the
1413 	 * status is INVALID_PAGE, change rval to FAILURE and quit.  Also,
1414 	 * signal that device traversal is complete.
1415 	 */
1416 	if (iocstatus == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE) {
1417 		if ((page_address & MPI2_SAS_DEVICE_PGAD_FORM_MASK) ==
1418 		    MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE) {
1419 			mpt->m_done_traverse_dev = 1;
1420 		}
1421 		rval = DDI_FAILURE;
1422 		return (rval);
1423 	}
1424 	devhdl = va_arg(ap, uint16_t *);
1425 	sas_wwn = va_arg(ap, uint64_t *);
1426 	dev_info = va_arg(ap, uint32_t *);
1427 	physport = va_arg(ap, uint8_t *);
1428 	phynum = va_arg(ap, uint8_t *);
1429 
1430 	sasdevpage = (pMpi2SasDevicePage0_t)page_memp;
1431 
1432 	*dev_info = ddi_get32(accessp, &sasdevpage->DeviceInfo);
1433 	*devhdl = ddi_get16(accessp, &sasdevpage->DevHandle);
1434 	sas_addr = (uint8_t *)(&sasdevpage->SASAddress);
1435 	for (i = 0; i < SAS_WWN_BYTE_SIZE; i++) {
1436 		tmp_sas_wwn[i] = ddi_get8(accessp, sas_addr + i);
1437 	}
1438 	bcopy(tmp_sas_wwn, sas_wwn, SAS_WWN_BYTE_SIZE);
1439 	*sas_wwn = LE_64(*sas_wwn);
1440 	*physport = ddi_get8(accessp, &sasdevpage->PhysicalPort);
1441 	*phynum = ddi_get8(accessp, &sasdevpage->PhyNum);
1442 	return (rval);
1443 }
1444 
1445 /*
1446  * Request MPI configuration page SAS device page 0 to get DevHandle, device
1447  * info and SAS address.
1448  */
1449 int
1450 mptsas_get_sas_device_page0(mptsas_t *mpt, uint32_t page_address,
1451     uint16_t *dev_handle, uint64_t *sas_wwn, uint32_t *dev_info,
1452     uint8_t *physport, uint8_t *phynum)
1453 {
1454 	int rval = DDI_SUCCESS;
1455 
1456 	ASSERT(mutex_owned(&mpt->m_mutex));
1457 
1458 	/*
1459 	 * Get the header and config page.  reply contains the reply frame,
1460 	 * which holds status info for the request.
1461 	 */
1462 	rval = mptsas_access_config_page(mpt,
1463 	    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT,
1464 	    MPI2_CONFIG_EXTPAGETYPE_SAS_DEVICE, 0, page_address,
1465 	    mptsas_sasdevpage_0_cb, page_address, dev_handle, sas_wwn,
1466 	    dev_info, physport, phynum);
1467 
1468 	return (rval);
1469 }
1470 
1471 static int
1472 mptsas_sasexpdpage_0_cb(mptsas_t *mpt, caddr_t page_memp,
1473     ddi_acc_handle_t accessp, uint16_t iocstatus, uint32_t iocloginfo,
1474     va_list ap)
1475 {
1476 #ifndef __lock_lint
1477 	_NOTE(ARGUNUSED(ap))
1478 #endif
1479 	pMpi2ExpanderPage0_t	expddevpage;
1480 	int			rval = DDI_SUCCESS, i;
1481 	uint8_t			*sas_addr = NULL;
1482 	uint8_t			tmp_sas_wwn[SAS_WWN_BYTE_SIZE];
1483 	uint16_t		*devhdl;
1484 	uint64_t		*sas_wwn;
1485 	uint8_t			physport, *phymask;
1486 	uint32_t		page_address;
1487 
1488 	if ((iocstatus != MPI2_IOCSTATUS_SUCCESS) &&
1489 	    (iocstatus != MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)) {
1490 		mptsas_log(mpt, CE_WARN, "mptsas_get_sas_expander_page0 "
1491 		    "config: IOCStatus=0x%x, IOCLogInfo=0x%x",
1492 		    iocstatus, iocloginfo);
1493 		rval = DDI_FAILURE;
1494 		return (rval);
1495 	}
1496 	page_address = va_arg(ap, uint32_t);
1497 	/*
1498 	 * The INVALID_PAGE status is normal if using GET_NEXT_HANDLE and there
1499 	 * are no more pages.  If everything is OK up to this point but the
1500 	 * status is INVALID_PAGE, change rval to FAILURE and quit.  Also,
1501 	 * signal that device traversal is complete.
1502 	 */
1503 	if (iocstatus == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE) {
1504 		if ((page_address & MPI2_SAS_EXPAND_PGAD_FORM_MASK) ==
1505 		    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL) {
1506 			mpt->m_done_traverse_smp = 1;
1507 		}
1508 		rval = DDI_FAILURE;
1509 		return (rval);
1510 	}
1511 	devhdl = va_arg(ap, uint16_t *);
1512 	sas_wwn = va_arg(ap, uint64_t *);
1513 	phymask = va_arg(ap, uint8_t *);
1514 
1515 	expddevpage = (pMpi2ExpanderPage0_t)page_memp;
1516 
1517 	*devhdl = ddi_get16(accessp, &expddevpage->DevHandle);
1518 	physport = ddi_get8(accessp, &expddevpage->PhysicalPort);
1519 	*phymask = mptsas_physport_to_phymask(mpt, physport);
1520 	sas_addr = (uint8_t *)(&expddevpage->SASAddress);
1521 	for (i = 0; i < SAS_WWN_BYTE_SIZE; i++) {
1522 		tmp_sas_wwn[i] = ddi_get8(accessp, sas_addr + i);
1523 	}
1524 	bcopy(tmp_sas_wwn, sas_wwn, SAS_WWN_BYTE_SIZE);
1525 	*sas_wwn = LE_64(*sas_wwn);
1526 	return (rval);
1527 }
1528 
1529 /*
1530  * Request MPI configuration page SAS device page 0 to get DevHandle, phymask
1531  * and SAS address.
1532  */
1533 int
1534 mptsas_get_sas_expander_page0(mptsas_t *mpt, uint32_t page_address,
1535     mptsas_smp_t *info)
1536 {
1537 	int			rval = DDI_SUCCESS;
1538 
1539 	ASSERT(mutex_owned(&mpt->m_mutex));
1540 
1541 	/*
1542 	 * Get the header and config page.  reply contains the reply frame,
1543 	 * which holds status info for the request.
1544 	 */
1545 	rval = mptsas_access_config_page(mpt,
1546 	    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT,
1547 	    MPI2_CONFIG_EXTPAGETYPE_SAS_EXPANDER, 0, page_address,
1548 	    mptsas_sasexpdpage_0_cb, page_address, &info->m_devhdl,
1549 	    &info->m_sasaddr, &info->m_phymask);
1550 
1551 	return (rval);
1552 }
1553 
1554 static int
1555 mptsas_sasportpage_0_cb(mptsas_t *mpt, caddr_t page_memp,
1556     ddi_acc_handle_t accessp, uint16_t iocstatus, uint32_t iocloginfo,
1557     va_list ap)
1558 {
1559 #ifndef __lock_lint
1560 	_NOTE(ARGUNUSED(ap))
1561 #endif
1562 	int	rval = DDI_SUCCESS, i;
1563 	uint8_t	*sas_addr = NULL;
1564 	uint64_t *sas_wwn;
1565 	uint8_t	tmp_sas_wwn[SAS_WWN_BYTE_SIZE];
1566 	uint8_t *portwidth;
1567 	pMpi2SasPortPage0_t sasportpage;
1568 
1569 	if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
1570 		mptsas_log(mpt, CE_WARN, "mptsas_get_sas_port_page0 "
1571 		    "config: IOCStatus=0x%x, IOCLogInfo=0x%x",
1572 		    iocstatus, iocloginfo);
1573 		rval = DDI_FAILURE;
1574 		return (rval);
1575 	}
1576 	sas_wwn = va_arg(ap, uint64_t *);
1577 	portwidth = va_arg(ap, uint8_t *);
1578 
1579 	sasportpage = (pMpi2SasPortPage0_t)page_memp;
1580 	sas_addr = (uint8_t *)(&sasportpage->SASAddress);
1581 	for (i = 0; i < SAS_WWN_BYTE_SIZE; i++) {
1582 		tmp_sas_wwn[i] = ddi_get8(accessp, sas_addr + i);
1583 	}
1584 	bcopy(tmp_sas_wwn, sas_wwn, SAS_WWN_BYTE_SIZE);
1585 	*sas_wwn = LE_64(*sas_wwn);
1586 	*portwidth = ddi_get8(accessp, &sasportpage->PortWidth);
1587 	return (rval);
1588 }
1589 
1590 /*
1591  * Request MPI configuration page SAS port page 0 to get initiator SAS address
1592  * and port width.
1593  */
1594 int
1595 mptsas_get_sas_port_page0(mptsas_t *mpt, uint32_t page_address,
1596     uint64_t *sas_wwn, uint8_t *portwidth)
1597 {
1598 	int rval = DDI_SUCCESS;
1599 
1600 	ASSERT(mutex_owned(&mpt->m_mutex));
1601 
1602 	/*
1603 	 * Get the header and config page.  reply contains the reply frame,
1604 	 * which holds status info for the request.
1605 	 */
1606 	rval = mptsas_access_config_page(mpt,
1607 	    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT,
1608 	    MPI2_CONFIG_EXTPAGETYPE_SAS_PORT, 0, page_address,
1609 	    mptsas_sasportpage_0_cb, sas_wwn, portwidth);
1610 
1611 	return (rval);
1612 }
1613 
1614 static int
1615 mptsas_sasiou_page_0_cb(mptsas_t *mpt, caddr_t page_memp,
1616     ddi_acc_handle_t accessp, uint16_t iocstatus, uint32_t iocloginfo,
1617     va_list ap)
1618 {
1619 #ifndef __lock_lint
1620 	_NOTE(ARGUNUSED(ap))
1621 #endif
1622 	int rval = DDI_SUCCESS;
1623 	pMpi2SasIOUnitPage0_t sasioupage0;
1624 	int i, num_phys;
1625 	uint32_t cpdi[8], *retrypage0, *readpage1;
1626 	uint8_t port_flags;
1627 
1628 	if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
1629 		mptsas_log(mpt, CE_WARN, "mptsas_get_sas_io_unit_page0 "
1630 		    "config: IOCStatus=0x%x, IOCLogInfo=0x%x",
1631 		    iocstatus, iocloginfo);
1632 		rval = DDI_FAILURE;
1633 		return (rval);
1634 	}
1635 	readpage1 = va_arg(ap, uint32_t *);
1636 	retrypage0 = va_arg(ap, uint32_t *);
1637 
1638 	sasioupage0 = (pMpi2SasIOUnitPage0_t)page_memp;
1639 
1640 	num_phys = ddi_get8(accessp, &sasioupage0->NumPhys);
1641 	for (i = 0; i < num_phys; i++) {
1642 		cpdi[i] = ddi_get32(accessp,
1643 		    &sasioupage0->PhyData[i].
1644 		    ControllerPhyDeviceInfo);
1645 		port_flags = ddi_get8(accessp,
1646 		    &sasioupage0->PhyData[i].PortFlags);
1647 		mpt->m_phy_info[i].port_num =
1648 		    ddi_get8(accessp,
1649 		    &sasioupage0->PhyData[i].Port);
1650 		mpt->m_phy_info[i].ctrl_devhdl =
1651 		    ddi_get16(accessp, &sasioupage0->
1652 		    PhyData[i].ControllerDevHandle);
1653 		mpt->m_phy_info[i].attached_devhdl =
1654 		    ddi_get16(accessp, &sasioupage0->
1655 		    PhyData[i].AttachedDevHandle);
1656 		mpt->m_phy_info[i].phy_device_type = cpdi[i];
1657 		mpt->m_phy_info[i].port_flags = port_flags;
1658 
1659 		if (port_flags & DISCOVERY_IN_PROGRESS) {
1660 			*retrypage0 = *retrypage0 + 1;
1661 			break;
1662 		} else {
1663 			*retrypage0 = 0;
1664 		}
1665 		if (!(port_flags & AUTO_PORT_CONFIGURATION)) {
1666 			/*
1667 			 * some PHY configuration described in
1668 			 * SAS IO Unit Page1
1669 			 */
1670 			*readpage1 = 1;
1671 		}
1672 	}
1673 
1674 	return (rval);
1675 }
1676 
1677 static int
1678 mptsas_sasiou_page_1_cb(mptsas_t *mpt, caddr_t page_memp,
1679     ddi_acc_handle_t accessp, uint16_t iocstatus, uint32_t iocloginfo,
1680     va_list ap)
1681 {
1682 #ifndef __lock_lint
1683 	_NOTE(ARGUNUSED(ap))
1684 #endif
1685 	int rval = DDI_SUCCESS;
1686 	pMpi2SasIOUnitPage1_t sasioupage1;
1687 	int i, num_phys;
1688 	uint32_t cpdi[8];
1689 	uint8_t port_flags;
1690 
1691 	if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
1692 		mptsas_log(mpt, CE_WARN, "mptsas_get_sas_io_unit_page1 "
1693 		    "config: IOCStatus=0x%x, IOCLogInfo=0x%x",
1694 		    iocstatus, iocloginfo);
1695 		rval = DDI_FAILURE;
1696 		return (rval);
1697 	}
1698 
1699 	sasioupage1 = (pMpi2SasIOUnitPage1_t)page_memp;
1700 	num_phys = ddi_get8(accessp, &sasioupage1->NumPhys);
1701 	for (i = 0; i < num_phys; i++) {
1702 		cpdi[i] = ddi_get32(accessp, &sasioupage1->PhyData[i].
1703 		    ControllerPhyDeviceInfo);
1704 		port_flags = ddi_get8(accessp,
1705 		    &sasioupage1->PhyData[i].PortFlags);
1706 		mpt->m_phy_info[i].port_num =
1707 		    ddi_get8(accessp,
1708 		    &sasioupage1->PhyData[i].Port);
1709 		mpt->m_phy_info[i].port_flags = port_flags;
1710 		mpt->m_phy_info[i].phy_device_type = cpdi[i];
1711 
1712 	}
1713 	return (rval);
1714 }
1715 
1716 /*
1717  * Read IO unit page 0 to get information for each PHY. If needed, Read IO Unit
1718  * page1 to update the PHY information.  This is the message passing method of
1719  * this function which should be called except during initialization.
1720  */
1721 int
1722 mptsas_get_sas_io_unit_page(mptsas_t *mpt)
1723 {
1724 	int rval = DDI_SUCCESS, state;
1725 	uint32_t readpage1 = 0, retrypage0 = 0;
1726 
1727 	ASSERT(mutex_owned(&mpt->m_mutex));
1728 
1729 	/*
1730 	 * Now we cycle through the state machine.  Here's what happens:
1731 	 * 1. Read IO unit page 0 and set phy information
1732 	 * 2. See if Read IO unit page1 is needed because of port configuration
1733 	 * 3. Read IO unit page 1 and update phy information.
1734 	 */
1735 	state = IOUC_READ_PAGE0;
1736 	while (state != IOUC_DONE) {
1737 		if (state == IOUC_READ_PAGE0) {
1738 			rval = mptsas_access_config_page(mpt,
1739 			    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT,
1740 			    MPI2_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0, 0,
1741 			    mptsas_sasiou_page_0_cb, &readpage1,
1742 			    &retrypage0);
1743 		} else if (state == IOUC_READ_PAGE1) {
1744 			rval = mptsas_access_config_page(mpt,
1745 			    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT,
1746 			    MPI2_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 1, 0,
1747 			    mptsas_sasiou_page_1_cb);
1748 		}
1749 
1750 		if (rval == DDI_SUCCESS) {
1751 			switch (state) {
1752 			case IOUC_READ_PAGE0:
1753 				/*
1754 				 * retry 30 times if discovery is in process
1755 				 */
1756 				if (retrypage0 && (retrypage0 < 30)) {
1757 					drv_usecwait(1000 * 100);
1758 					state = IOUC_READ_PAGE0;
1759 					break;
1760 				} else if (retrypage0 == 30) {
1761 					mptsas_log(mpt, CE_WARN,
1762 					    "!Discovery in progress, can't "
1763 					    "verify IO unit config, then "
1764 					    "after 30 times retry, give "
1765 					    "up!");
1766 					state = IOUC_DONE;
1767 					rval = DDI_FAILURE;
1768 					break;
1769 				}
1770 
1771 				if (readpage1 == 0) {
1772 					state = IOUC_DONE;
1773 					rval = DDI_SUCCESS;
1774 					break;
1775 				}
1776 
1777 				state = IOUC_READ_PAGE1;
1778 				break;
1779 
1780 			case IOUC_READ_PAGE1:
1781 				state = IOUC_DONE;
1782 				rval = DDI_SUCCESS;
1783 				break;
1784 			}
1785 		} else {
1786 			return (rval);
1787 		}
1788 	}
1789 
1790 	return (rval);
1791 }
1792 
1793 static int
1794 mptsas_biospage_3_cb(mptsas_t *mpt, caddr_t page_memp,
1795     ddi_acc_handle_t accessp, uint16_t iocstatus, uint32_t iocloginfo,
1796     va_list ap)
1797 {
1798 #ifndef __lock_lint
1799 	_NOTE(ARGUNUSED(ap))
1800 #endif
1801 	pMpi2BiosPage3_t	sasbiospage;
1802 	int			rval = DDI_SUCCESS;
1803 	uint32_t		*bios_version;
1804 
1805 	if ((iocstatus != MPI2_IOCSTATUS_SUCCESS) &&
1806 	    (iocstatus != MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)) {
1807 		mptsas_log(mpt, CE_WARN, "mptsas_get_bios_page3 header: "
1808 		    "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus, iocloginfo);
1809 		rval = DDI_FAILURE;
1810 		return (rval);
1811 	}
1812 	bios_version = va_arg(ap, uint32_t *);
1813 	sasbiospage = (pMpi2BiosPage3_t)page_memp;
1814 	*bios_version = ddi_get32(accessp, &sasbiospage->BiosVersion);
1815 
1816 	return (rval);
1817 }
1818 
1819 /*
1820  * Request MPI configuration page BIOS page 3 to get BIOS version.  Since all
1821  * other information in this page is not needed, just ignore it.
1822  */
1823 int
1824 mptsas_get_bios_page3(mptsas_t *mpt, uint32_t *bios_version)
1825 {
1826 	int rval = DDI_SUCCESS;
1827 
1828 	ASSERT(mutex_owned(&mpt->m_mutex));
1829 
1830 	/*
1831 	 * Get the header and config page.  reply contains the reply frame,
1832 	 * which holds status info for the request.
1833 	 */
1834 	rval = mptsas_access_config_page(mpt,
1835 	    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT, MPI2_CONFIG_PAGETYPE_BIOS, 3,
1836 	    0, mptsas_biospage_3_cb, bios_version);
1837 
1838 	return (rval);
1839 }
1840 
1841 /*
1842  * Read IO unit page 0 to get information for each PHY. If needed, Read IO Unit
1843  * page1 to update the PHY information.  This is the handshaking version of
1844  * this function, which should be called during initialization only.
1845  */
1846 int
1847 mptsas_get_sas_io_unit_page_hndshk(mptsas_t *mpt)
1848 {
1849 	ddi_dma_attr_t		recv_dma_attrs, page_dma_attrs;
1850 	uint_t			recv_ncookie, page_ncookie;
1851 	ddi_dma_cookie_t	recv_cookie, page_cookie;
1852 	ddi_dma_handle_t	recv_dma_handle, page_dma_handle;
1853 	ddi_acc_handle_t	recv_accessp, page_accessp;
1854 	size_t			recv_alloc_len, page_alloc_len;
1855 	pMpi2ConfigReply_t	configreply;
1856 	pMpi2SasIOUnitPage0_t	sasioupage0;
1857 	pMpi2SasIOUnitPage1_t	sasioupage1;
1858 	int			recv_numbytes;
1859 	caddr_t			recv_memp, page_memp;
1860 	int			recv_dmastate = 0;
1861 	int			page_dmastate = 0;
1862 	int			i, num_phys;
1863 	int			page0_size =
1864 	    sizeof (MPI2_CONFIG_PAGE_SASIOUNIT_0) +
1865 	    (sizeof (MPI2_SAS_IO_UNIT0_PHY_DATA) * 7);
1866 	int			page1_size =
1867 	    sizeof (MPI2_CONFIG_PAGE_SASIOUNIT_1) +
1868 	    (sizeof (MPI2_SAS_IO_UNIT1_PHY_DATA) * 7);
1869 	uint32_t		flags_length;
1870 	uint32_t		cpdi[8], readpage1 = 0, retrypage0 = 0;
1871 	uint16_t		iocstatus;
1872 	uint8_t			port_flags, page_number, action;
1873 	uint32_t		reply_size = 256; /* Big enough for any page */
1874 	uint_t			state;
1875 	int			rval = DDI_FAILURE;
1876 
1877 	/*
1878 	 * Initialize our "state machine".  This is a bit convoluted,
1879 	 * but it keeps us from having to do the ddi allocations numerous
1880 	 * times.
1881 	 */
1882 
1883 	NDBG20(("mptsas_get_sas_io_unit_page_hndshk enter"));
1884 	ASSERT(mutex_owned(&mpt->m_mutex));
1885 	state = IOUC_READ_PAGE0;
1886 
1887 	/*
1888 	 * dynamically create a customized dma attribute structure
1889 	 * that describes mpt's config reply page request structure.
1890 	 */
1891 	recv_dma_attrs = mpt->m_msg_dma_attr;
1892 	recv_dma_attrs.dma_attr_sgllen = 1;
1893 	recv_dma_attrs.dma_attr_granular = (sizeof (MPI2_CONFIG_REPLY));
1894 
1895 	if (ddi_dma_alloc_handle(mpt->m_dip, &recv_dma_attrs,
1896 	    DDI_DMA_SLEEP, NULL, &recv_dma_handle) != DDI_SUCCESS) {
1897 		goto cleanup;
1898 	}
1899 
1900 	recv_dmastate |= MPTSAS_DMA_HANDLE_ALLOCD;
1901 
1902 	if (ddi_dma_mem_alloc(recv_dma_handle,
1903 	    (sizeof (MPI2_CONFIG_REPLY)),
1904 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1905 	    &recv_memp, &recv_alloc_len, &recv_accessp) != DDI_SUCCESS) {
1906 		goto cleanup;
1907 	}
1908 
1909 	recv_dmastate |= MPTSAS_DMA_MEMORY_ALLOCD;
1910 
1911 	if (ddi_dma_addr_bind_handle(recv_dma_handle, NULL, recv_memp,
1912 	    recv_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1913 	    NULL, &recv_cookie, &recv_ncookie) != DDI_DMA_MAPPED) {
1914 		goto cleanup;
1915 	}
1916 
1917 	recv_dmastate |= MPTSAS_DMA_HANDLE_BOUND;
1918 
1919 	page_dma_attrs = mpt->m_msg_dma_attr;
1920 	page_dma_attrs.dma_attr_sgllen = 1;
1921 	page_dma_attrs.dma_attr_granular = reply_size;
1922 
1923 	if (ddi_dma_alloc_handle(mpt->m_dip, &page_dma_attrs,
1924 	    DDI_DMA_SLEEP, NULL, &page_dma_handle) != DDI_SUCCESS) {
1925 		goto cleanup;
1926 	}
1927 
1928 	page_dmastate |= MPTSAS_DMA_HANDLE_ALLOCD;
1929 
1930 	/*
1931 	 * Page 0 size is larger, so just use that for both.
1932 	 */
1933 
1934 	if (ddi_dma_mem_alloc(page_dma_handle, reply_size,
1935 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1936 	    &page_memp, &page_alloc_len, &page_accessp) != DDI_SUCCESS) {
1937 		goto cleanup;
1938 	}
1939 
1940 	page_dmastate |= MPTSAS_DMA_MEMORY_ALLOCD;
1941 
1942 	if (ddi_dma_addr_bind_handle(page_dma_handle, NULL, page_memp,
1943 	    page_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1944 	    NULL, &page_cookie, &page_ncookie) != DDI_DMA_MAPPED) {
1945 		goto cleanup;
1946 	}
1947 
1948 	page_dmastate |= MPTSAS_DMA_HANDLE_BOUND;
1949 
1950 	/*
1951 	 * Now we cycle through the state machine.  Here's what happens:
1952 	 * 1. Read IO unit page 0 and set phy information
1953 	 * 2. See if Read IO unit page1 is needed because of port configuration
1954 	 * 3. Read IO unit page 1 and update phy information.
1955 	 */
1956 
1957 	sasioupage0 = (pMpi2SasIOUnitPage0_t)page_memp;
1958 	sasioupage1 = (pMpi2SasIOUnitPage1_t)page_memp;
1959 
1960 	while (state != IOUC_DONE) {
1961 		switch (state) {
1962 		case IOUC_READ_PAGE0:
1963 			page_number = 0;
1964 			action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
1965 			flags_length = (uint32_t)page0_size;
1966 			flags_length |= ((uint32_t)(
1967 			    MPI2_SGE_FLAGS_LAST_ELEMENT |
1968 			    MPI2_SGE_FLAGS_END_OF_BUFFER |
1969 			    MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1970 			    MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
1971 			    MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1972 			    MPI2_SGE_FLAGS_IOC_TO_HOST |
1973 			    MPI2_SGE_FLAGS_END_OF_LIST) <<
1974 			    MPI2_SGE_FLAGS_SHIFT);
1975 
1976 			break;
1977 
1978 		case IOUC_READ_PAGE1:
1979 			page_number = 1;
1980 			action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
1981 			flags_length = (uint32_t)page1_size;
1982 			flags_length |= ((uint32_t)(
1983 			    MPI2_SGE_FLAGS_LAST_ELEMENT |
1984 			    MPI2_SGE_FLAGS_END_OF_BUFFER |
1985 			    MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1986 			    MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
1987 			    MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1988 			    MPI2_SGE_FLAGS_IOC_TO_HOST |
1989 			    MPI2_SGE_FLAGS_END_OF_LIST) <<
1990 			    MPI2_SGE_FLAGS_SHIFT);
1991 
1992 			break;
1993 		default:
1994 			break;
1995 		}
1996 
1997 		bzero(recv_memp, sizeof (MPI2_CONFIG_REPLY));
1998 		configreply = (pMpi2ConfigReply_t)recv_memp;
1999 		recv_numbytes = sizeof (MPI2_CONFIG_REPLY);
2000 
2001 		if (mptsas_send_extended_config_request_msg(mpt,
2002 		    MPI2_CONFIG_ACTION_PAGE_HEADER,
2003 		    MPI2_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
2004 		    0, page_number, 0, 0, 0, 0)) {
2005 			goto cleanup;
2006 		}
2007 
2008 		if (mptsas_get_handshake_msg(mpt, recv_memp, recv_numbytes,
2009 		    recv_accessp)) {
2010 			goto cleanup;
2011 		}
2012 
2013 		iocstatus = ddi_get16(recv_accessp, &configreply->IOCStatus);
2014 		iocstatus = MPTSAS_IOCSTATUS(iocstatus);
2015 
2016 		if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
2017 			mptsas_log(mpt, CE_WARN,
2018 			    "mptsas_get_sas_io_unit_page_hndshk: read page "
2019 			    "header iocstatus = 0x%x", iocstatus);
2020 			goto cleanup;
2021 		}
2022 
2023 		if (action != MPI2_CONFIG_ACTION_PAGE_WRITE_NVRAM) {
2024 			bzero(page_memp, reply_size);
2025 		}
2026 
2027 		if (mptsas_send_extended_config_request_msg(mpt, action,
2028 		    MPI2_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0, page_number,
2029 		    ddi_get8(recv_accessp, &configreply->Header.PageVersion),
2030 		    ddi_get16(recv_accessp, &configreply->ExtPageLength),
2031 		    flags_length, page_cookie.dmac_address)) {
2032 			goto cleanup;
2033 		}
2034 
2035 		if (mptsas_get_handshake_msg(mpt, recv_memp, recv_numbytes,
2036 		    recv_accessp)) {
2037 			goto cleanup;
2038 		}
2039 
2040 		iocstatus = ddi_get16(recv_accessp, &configreply->IOCStatus);
2041 		iocstatus = MPTSAS_IOCSTATUS(iocstatus);
2042 
2043 		if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
2044 			mptsas_log(mpt, CE_WARN,
2045 			    "mptsas_get_sas_io_unit_page_hndshk: IO unit "
2046 			    "config failed for action %d, iocstatus = 0x%x",
2047 			    action, iocstatus);
2048 			goto cleanup;
2049 		}
2050 
2051 		switch (state) {
2052 		case IOUC_READ_PAGE0:
2053 			if ((ddi_dma_sync(page_dma_handle, 0, 0,
2054 			    DDI_DMA_SYNC_FORCPU)) != DDI_SUCCESS) {
2055 				goto cleanup;
2056 			}
2057 
2058 			num_phys = ddi_get8(page_accessp,
2059 			    &sasioupage0->NumPhys);
2060 			for (i = 0; i < num_phys; i++) {
2061 				cpdi[i] = ddi_get32(page_accessp,
2062 				    &sasioupage0->PhyData[i].
2063 				    ControllerPhyDeviceInfo);
2064 				port_flags = ddi_get8(page_accessp,
2065 				    &sasioupage0->PhyData[i].PortFlags);
2066 
2067 				mpt->m_phy_info[i].port_num =
2068 				    ddi_get8(page_accessp,
2069 				    &sasioupage0->PhyData[i].Port);
2070 				mpt->m_phy_info[i].ctrl_devhdl =
2071 				    ddi_get16(page_accessp, &sasioupage0->
2072 				    PhyData[i].ControllerDevHandle);
2073 				mpt->m_phy_info[i].attached_devhdl =
2074 				    ddi_get16(page_accessp, &sasioupage0->
2075 				    PhyData[i].AttachedDevHandle);
2076 				mpt->m_phy_info[i].phy_device_type = cpdi[i];
2077 				mpt->m_phy_info[i].port_flags = port_flags;
2078 
2079 				if (port_flags & DISCOVERY_IN_PROGRESS) {
2080 					retrypage0++;
2081 					NDBG20(("Discovery in progress, can't "
2082 					    "verify IO unit config, then NO.%d"
2083 					    " times retry", retrypage0));
2084 					break;
2085 				} else {
2086 					retrypage0 = 0;
2087 				}
2088 				if (!(port_flags & AUTO_PORT_CONFIGURATION)) {
2089 					/*
2090 					 * some PHY configuration described in
2091 					 * SAS IO Unit Page1
2092 					 */
2093 					readpage1 = 1;
2094 				}
2095 			}
2096 
2097 			/*
2098 			 * retry 30 times if discovery is in process
2099 			 */
2100 			if (retrypage0 && (retrypage0 < 30)) {
2101 				drv_usecwait(1000 * 100);
2102 				state = IOUC_READ_PAGE0;
2103 				break;
2104 			} else if (retrypage0 == 30) {
2105 				mptsas_log(mpt, CE_WARN,
2106 				    "!Discovery in progress, can't "
2107 				    "verify IO unit config, then after"
2108 				    " 30 times retry, give up!");
2109 				state = IOUC_DONE;
2110 				rval = DDI_FAILURE;
2111 				break;
2112 			}
2113 
2114 			if (readpage1 == 0) {
2115 				state = IOUC_DONE;
2116 				rval = DDI_SUCCESS;
2117 				break;
2118 			}
2119 
2120 			state = IOUC_READ_PAGE1;
2121 			break;
2122 
2123 		case IOUC_READ_PAGE1:
2124 			if ((ddi_dma_sync(page_dma_handle, 0, 0,
2125 			    DDI_DMA_SYNC_FORCPU)) != DDI_SUCCESS) {
2126 				goto cleanup;
2127 			}
2128 
2129 			num_phys = ddi_get8(page_accessp,
2130 			    &sasioupage1->NumPhys);
2131 
2132 			for (i = 0; i < num_phys; i++) {
2133 				cpdi[i] = ddi_get32(page_accessp,
2134 				    &sasioupage1->PhyData[i].
2135 				    ControllerPhyDeviceInfo);
2136 				port_flags = ddi_get8(page_accessp,
2137 				    &sasioupage1->PhyData[i].PortFlags);
2138 				mpt->m_phy_info[i].port_num =
2139 				    ddi_get8(page_accessp,
2140 				    &sasioupage1->PhyData[i].Port);
2141 				mpt->m_phy_info[i].port_flags = port_flags;
2142 				mpt->m_phy_info[i].phy_device_type = cpdi[i];
2143 
2144 			}
2145 
2146 			state = IOUC_DONE;
2147 			rval = DDI_SUCCESS;
2148 			break;
2149 		}
2150 	}
2151 	if ((mptsas_check_dma_handle(recv_dma_handle) != DDI_SUCCESS) ||
2152 	    (mptsas_check_dma_handle(page_dma_handle) != DDI_SUCCESS)) {
2153 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2154 		rval = DDI_FAILURE;
2155 		goto cleanup;
2156 	}
2157 	if ((mptsas_check_acc_handle(recv_accessp) != DDI_SUCCESS) ||
2158 	    (mptsas_check_acc_handle(page_accessp) != DDI_SUCCESS)) {
2159 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2160 		rval = DDI_FAILURE;
2161 		goto cleanup;
2162 	}
2163 
2164 cleanup:
2165 	if (recv_dmastate & MPTSAS_DMA_HANDLE_BOUND)
2166 		(void) ddi_dma_unbind_handle(recv_dma_handle);
2167 	if (page_dmastate & MPTSAS_DMA_HANDLE_BOUND)
2168 		(void) ddi_dma_unbind_handle(page_dma_handle);
2169 	if (recv_dmastate & MPTSAS_DMA_MEMORY_ALLOCD)
2170 		(void) ddi_dma_mem_free(&recv_accessp);
2171 	if (page_dmastate & MPTSAS_DMA_MEMORY_ALLOCD)
2172 		(void) ddi_dma_mem_free(&page_accessp);
2173 	if (recv_dmastate & MPTSAS_DMA_HANDLE_ALLOCD)
2174 		ddi_dma_free_handle(&recv_dma_handle);
2175 	if (page_dmastate & MPTSAS_DMA_HANDLE_ALLOCD)
2176 		ddi_dma_free_handle(&page_dma_handle);
2177 
2178 	if (rval != DDI_SUCCESS) {
2179 		mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
2180 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
2181 	}
2182 	return (rval);
2183 }
2184 
2185 /*
2186  * Check if the PHYs are currently in target mode. If they are not, we don't
2187  * need to change anything.  Otherwise, we need to modify the appropriate bits
2188  * and write them to IO unit page 1.  Once that is done, an IO unit reset is
2189  * necessary to begin operating in initiator mode.  Since this function is only
2190  * called during the initialization process, use handshaking.
2191  */
2192 int
2193 mptsas_set_initiator_mode(mptsas_t *mpt)
2194 {
2195 	ddi_dma_attr_t		recv_dma_attrs, page_dma_attrs;
2196 	uint_t			recv_ncookie, page_ncookie;
2197 	ddi_dma_cookie_t	recv_cookie, page_cookie;
2198 	ddi_dma_handle_t	recv_dma_handle, page_dma_handle;
2199 	ddi_acc_handle_t	recv_accessp, page_accessp;
2200 	size_t			recv_alloc_len, page_alloc_len;
2201 	pMpi2ConfigReply_t	configreply;
2202 	pMpi2SasIOUnitPage1_t	sasioupage1;
2203 	int			recv_numbytes;
2204 	caddr_t			recv_memp, page_memp;
2205 	int			recv_dmastate = 0;
2206 	int			page_dmastate = 0;
2207 	int			i;
2208 	int			page1_size =
2209 	    sizeof (MPI2_CONFIG_PAGE_SASIOUNIT_1) +
2210 	    (sizeof (MPI2_SAS_IO_UNIT0_PHY_DATA) * 7);
2211 	uint32_t		flags_length;
2212 	uint32_t		cpdi[8], reprogram = 0;
2213 	uint16_t		iocstatus;
2214 	uint8_t			port_flags, page_number, action;
2215 	uint32_t		reply_size = 256; /* Big enough for any page */
2216 	uint_t			state;
2217 	int			rval = DDI_FAILURE;
2218 
2219 	ASSERT(mutex_owned(&mpt->m_mutex));
2220 	/*
2221 	 * get each PHY informations from SAS IO Unit Pages.  Use handshakiing
2222 	 * to get SAS IO Unit Page information since this is during init.
2223 	 */
2224 	rval = mptsas_get_sas_io_unit_page_hndshk(mpt);
2225 	if (rval != DDI_SUCCESS)
2226 		return (rval);
2227 
2228 	for (i = 0; i < mpt->m_num_phys; i++) {
2229 		if (mpt->m_phy_info[i].phy_device_type &
2230 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2231 			reprogram = 1;
2232 			break;
2233 		}
2234 	}
2235 	if (reprogram == 0)
2236 		return (DDI_SUCCESS);
2237 
2238 	/*
2239 	 * Initialize our "state machine".  This is a bit convoluted,
2240 	 * but it keeps us from having to do the ddi allocations numerous
2241 	 * times.
2242 	 */
2243 
2244 	state = IOUC_READ_PAGE1;
2245 
2246 	/*
2247 	 * dynamically create a customized dma attribute structure
2248 	 * that describes mpt's config reply page request structure.
2249 	 */
2250 	recv_dma_attrs = mpt->m_msg_dma_attr;
2251 	recv_dma_attrs.dma_attr_sgllen = 1;
2252 	recv_dma_attrs.dma_attr_granular = (sizeof (MPI2_CONFIG_REPLY));
2253 
2254 	if (ddi_dma_alloc_handle(mpt->m_dip, &recv_dma_attrs,
2255 	    DDI_DMA_SLEEP, NULL, &recv_dma_handle) != DDI_SUCCESS) {
2256 		goto cleanup;
2257 	}
2258 
2259 	recv_dmastate |= MPTSAS_DMA_HANDLE_ALLOCD;
2260 
2261 	if (ddi_dma_mem_alloc(recv_dma_handle,
2262 	    (sizeof (MPI2_CONFIG_REPLY)),
2263 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
2264 	    &recv_memp, &recv_alloc_len, &recv_accessp) != DDI_SUCCESS) {
2265 		goto cleanup;
2266 	}
2267 
2268 	recv_dmastate |= MPTSAS_DMA_MEMORY_ALLOCD;
2269 
2270 	if (ddi_dma_addr_bind_handle(recv_dma_handle, NULL, recv_memp,
2271 	    recv_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
2272 	    NULL, &recv_cookie, &recv_ncookie) != DDI_DMA_MAPPED) {
2273 		goto cleanup;
2274 	}
2275 
2276 	recv_dmastate |= MPTSAS_DMA_HANDLE_BOUND;
2277 
2278 	page_dma_attrs = mpt->m_msg_dma_attr;
2279 	page_dma_attrs.dma_attr_sgllen = 1;
2280 	page_dma_attrs.dma_attr_granular = reply_size;
2281 
2282 	if (ddi_dma_alloc_handle(mpt->m_dip, &page_dma_attrs,
2283 	    DDI_DMA_SLEEP, NULL, &page_dma_handle) != DDI_SUCCESS) {
2284 		goto cleanup;
2285 	}
2286 
2287 	page_dmastate |= MPTSAS_DMA_HANDLE_ALLOCD;
2288 
2289 	if (ddi_dma_mem_alloc(page_dma_handle, reply_size,
2290 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
2291 	    &page_memp, &page_alloc_len, &page_accessp) != DDI_SUCCESS) {
2292 		goto cleanup;
2293 	}
2294 
2295 	page_dmastate |= MPTSAS_DMA_MEMORY_ALLOCD;
2296 
2297 	if (ddi_dma_addr_bind_handle(page_dma_handle, NULL, page_memp,
2298 	    page_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
2299 	    NULL, &page_cookie, &page_ncookie) != DDI_DMA_MAPPED) {
2300 		goto cleanup;
2301 	}
2302 
2303 	page_dmastate |= MPTSAS_DMA_HANDLE_BOUND;
2304 
2305 	/*
2306 	 * Now we cycle through the state machine.  Here's what happens:
2307 	 * 1. Read IO unit page 1.
2308 	 * 2. Change the appropriate bits
2309 	 * 3. Write the updated settings to IO unit page 1.
2310 	 * 4. Reset the IO unit.
2311 	 */
2312 
2313 	sasioupage1 = (pMpi2SasIOUnitPage1_t)page_memp;
2314 
2315 	while (state != IOUC_DONE) {
2316 		switch (state) {
2317 		case IOUC_READ_PAGE1:
2318 			page_number = 1;
2319 			action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
2320 			flags_length = (uint32_t)page1_size;
2321 			flags_length |= ((uint32_t)(
2322 			    MPI2_SGE_FLAGS_LAST_ELEMENT |
2323 			    MPI2_SGE_FLAGS_END_OF_BUFFER |
2324 			    MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2325 			    MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
2326 			    MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
2327 			    MPI2_SGE_FLAGS_IOC_TO_HOST |
2328 			    MPI2_SGE_FLAGS_END_OF_LIST) <<
2329 			    MPI2_SGE_FLAGS_SHIFT);
2330 
2331 			break;
2332 
2333 		case IOUC_WRITE_PAGE1:
2334 			page_number = 1;
2335 			action = MPI2_CONFIG_ACTION_PAGE_WRITE_NVRAM;
2336 			flags_length = (uint32_t)page1_size;
2337 			flags_length |= ((uint32_t)(
2338 			    MPI2_SGE_FLAGS_LAST_ELEMENT |
2339 			    MPI2_SGE_FLAGS_END_OF_BUFFER |
2340 			    MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2341 			    MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
2342 			    MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
2343 			    MPI2_SGE_FLAGS_HOST_TO_IOC |
2344 			    MPI2_SGE_FLAGS_END_OF_LIST) <<
2345 			    MPI2_SGE_FLAGS_SHIFT);
2346 
2347 			break;
2348 		}
2349 
2350 		bzero(recv_memp, sizeof (MPI2_CONFIG_REPLY));
2351 		configreply = (pMpi2ConfigReply_t)recv_memp;
2352 		recv_numbytes = sizeof (MPI2_CONFIG_REPLY);
2353 
2354 		if (mptsas_send_extended_config_request_msg(mpt,
2355 		    MPI2_CONFIG_ACTION_PAGE_HEADER,
2356 		    MPI2_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
2357 		    0, page_number, 0, 0, 0, 0)) {
2358 			goto cleanup;
2359 		}
2360 
2361 		if (mptsas_get_handshake_msg(mpt, recv_memp, recv_numbytes,
2362 		    recv_accessp)) {
2363 			goto cleanup;
2364 		}
2365 
2366 		iocstatus = ddi_get16(recv_accessp, &configreply->IOCStatus);
2367 		iocstatus = MPTSAS_IOCSTATUS(iocstatus);
2368 
2369 		if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
2370 			mptsas_log(mpt, CE_WARN,
2371 			    "mptsas_set_initiator_mode: read page hdr iocstatus"
2372 			    ": 0x%x", iocstatus);
2373 			goto cleanup;
2374 		}
2375 
2376 		if (action != MPI2_CONFIG_ACTION_PAGE_WRITE_NVRAM) {
2377 			bzero(page_memp, reply_size);
2378 		}
2379 
2380 		if (mptsas_send_extended_config_request_msg(mpt, action,
2381 		    MPI2_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0, page_number,
2382 		    ddi_get8(recv_accessp, &configreply->Header.PageVersion),
2383 		    ddi_get16(recv_accessp, &configreply->ExtPageLength),
2384 		    flags_length, page_cookie.dmac_address)) {
2385 			goto cleanup;
2386 		}
2387 
2388 		if (mptsas_get_handshake_msg(mpt, recv_memp, recv_numbytes,
2389 		    recv_accessp)) {
2390 			goto cleanup;
2391 		}
2392 
2393 		iocstatus = ddi_get16(recv_accessp, &configreply->IOCStatus);
2394 		iocstatus = MPTSAS_IOCSTATUS(iocstatus);
2395 
2396 		if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
2397 			mptsas_log(mpt, CE_WARN,
2398 			    "mptsas_set_initiator_mode: IO unit config failed "
2399 			    "for action %d, iocstatus = 0x%x", action,
2400 			    iocstatus);
2401 			goto cleanup;
2402 		}
2403 
2404 		switch (state) {
2405 		case IOUC_READ_PAGE1:
2406 			if ((ddi_dma_sync(page_dma_handle, 0, 0,
2407 			    DDI_DMA_SYNC_FORCPU)) != DDI_SUCCESS) {
2408 				goto cleanup;
2409 			}
2410 
2411 			/*
2412 			 * All the PHYs should have the same settings, so we
2413 			 * really only need to read 1 and use its config for
2414 			 * every PHY.
2415 			 */
2416 
2417 			cpdi[0] = ddi_get32(page_accessp,
2418 			    &sasioupage1->PhyData[0].ControllerPhyDeviceInfo);
2419 			port_flags = ddi_get8(page_accessp,
2420 			    &sasioupage1->PhyData[0].PortFlags);
2421 			port_flags |=
2422 			    MPI2_SASIOUNIT1_PORT_FLAGS_AUTO_PORT_CONFIG;
2423 
2424 			/*
2425 			 * Write the configuration to SAS I/O unit page 1
2426 			 */
2427 
2428 			mptsas_log(mpt, CE_NOTE,
2429 			    "?IO unit in target mode, changing to initiator");
2430 
2431 			/*
2432 			 * Modify the PHY settings for initiator mode
2433 			 */
2434 
2435 			cpdi[0] &= ~MPI2_SAS_DEVICE_INFO_SSP_TARGET;
2436 			cpdi[0] |= (MPI2_SAS_DEVICE_INFO_SSP_INITIATOR |
2437 			    MPI2_SAS_DEVICE_INFO_STP_INITIATOR |
2438 			    MPI2_SAS_DEVICE_INFO_SMP_INITIATOR);
2439 
2440 			for (i = 0; i < mpt->m_num_phys; i++) {
2441 				ddi_put32(page_accessp,
2442 				    &sasioupage1->PhyData[i].
2443 				    ControllerPhyDeviceInfo, cpdi[0]);
2444 				ddi_put8(page_accessp,
2445 				    &sasioupage1->PhyData[i].
2446 				    PortFlags, port_flags);
2447 				/*
2448 				 * update phy information
2449 				 */
2450 				mpt->m_phy_info[i].phy_device_type = cpdi[0];
2451 				mpt->m_phy_info[i].port_flags = port_flags;
2452 			}
2453 
2454 			if ((ddi_dma_sync(page_dma_handle, 0, 0,
2455 			    DDI_DMA_SYNC_FORDEV)) != DDI_SUCCESS) {
2456 				goto cleanup;
2457 			}
2458 
2459 			state = IOUC_WRITE_PAGE1;
2460 
2461 			break;
2462 
2463 		case IOUC_WRITE_PAGE1:
2464 			/*
2465 			 * If we're here, we wrote IO unit page 1 succesfully.
2466 			 */
2467 			state = IOUC_DONE;
2468 
2469 			rval = DDI_SUCCESS;
2470 			break;
2471 		}
2472 	}
2473 
2474 	/*
2475 	 * We need to do a Message Unit Reset in order to activate the changes.
2476 	 */
2477 	mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
2478 	rval = mptsas_init_chip(mpt, FALSE);
2479 
2480 cleanup:
2481 	if (recv_dmastate & MPTSAS_DMA_HANDLE_BOUND)
2482 		(void) ddi_dma_unbind_handle(recv_dma_handle);
2483 	if (page_dmastate & MPTSAS_DMA_HANDLE_BOUND)
2484 		(void) ddi_dma_unbind_handle(page_dma_handle);
2485 	if (recv_dmastate & MPTSAS_DMA_MEMORY_ALLOCD)
2486 		(void) ddi_dma_mem_free(&recv_accessp);
2487 	if (page_dmastate & MPTSAS_DMA_MEMORY_ALLOCD)
2488 		(void) ddi_dma_mem_free(&page_accessp);
2489 	if (recv_dmastate & MPTSAS_DMA_HANDLE_ALLOCD)
2490 		ddi_dma_free_handle(&recv_dma_handle);
2491 	if (page_dmastate & MPTSAS_DMA_HANDLE_ALLOCD)
2492 		ddi_dma_free_handle(&page_dma_handle);
2493 
2494 	return (rval);
2495 }
2496 
2497 /*
2498  * mptsas_get_manufacture_page5
2499  *
2500  * This function will retrieve the base WWID from the adapter.  Since this
2501  * function is only called during the initialization process, use handshaking.
2502  */
2503 int
2504 mptsas_get_manufacture_page5(mptsas_t *mpt)
2505 {
2506 	ddi_dma_attr_t			recv_dma_attrs, page_dma_attrs;
2507 	ddi_dma_cookie_t		recv_cookie, page_cookie;
2508 	ddi_dma_handle_t		recv_dma_handle, page_dma_handle;
2509 	ddi_acc_handle_t		recv_accessp, page_accessp;
2510 	size_t				recv_alloc_len, page_alloc_len;
2511 	pMpi2ConfigReply_t		configreply;
2512 	uint_t				recv_ncookie, page_ncookie;
2513 	caddr_t				recv_memp, page_memp;
2514 	int				recv_numbytes;
2515 	pMpi2ManufacturingPage5_t	m5;
2516 	int				recv_dmastate = 0;
2517 	int				page_dmastate = 0;
2518 	uint32_t			flagslength;
2519 	int				rval = DDI_SUCCESS;
2520 	uint_t				iocstatus;
2521 
2522 	MPTSAS_DISABLE_INTR(mpt);
2523 
2524 	if (mptsas_send_config_request_msg(mpt, MPI2_CONFIG_ACTION_PAGE_HEADER,
2525 	    MPI2_CONFIG_PAGETYPE_MANUFACTURING, 0, 5, 0, 0, 0, 0)) {
2526 		rval = DDI_FAILURE;
2527 		goto done;
2528 	}
2529 
2530 	/*
2531 	 * dynamically create a customized dma attribute structure
2532 	 * that describes the MPT's config reply page request structure.
2533 	 */
2534 	recv_dma_attrs = mpt->m_msg_dma_attr;
2535 	recv_dma_attrs.dma_attr_sgllen = 1;
2536 	recv_dma_attrs.dma_attr_granular = (sizeof (MPI2_CONFIG_REPLY));
2537 
2538 	if (ddi_dma_alloc_handle(mpt->m_dip, &recv_dma_attrs,
2539 	    DDI_DMA_SLEEP, NULL, &recv_dma_handle) != DDI_SUCCESS) {
2540 		mptsas_log(mpt, CE_WARN,
2541 		    "(unable to allocate dma handle.");
2542 		rval = DDI_FAILURE;
2543 		goto done;
2544 	}
2545 	recv_dmastate |= MPTSAS_DMA_HANDLE_ALLOCD;
2546 
2547 	if (ddi_dma_mem_alloc(recv_dma_handle,
2548 	    (sizeof (MPI2_CONFIG_REPLY)),
2549 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
2550 	    &recv_memp, &recv_alloc_len, &recv_accessp) != DDI_SUCCESS) {
2551 		mptsas_log(mpt, CE_WARN,
2552 		    "unable to allocate config_reply structure.");
2553 		rval = DDI_FAILURE;
2554 		goto done;
2555 	}
2556 	recv_dmastate |= MPTSAS_DMA_MEMORY_ALLOCD;
2557 
2558 	if (ddi_dma_addr_bind_handle(recv_dma_handle, NULL, recv_memp,
2559 	    recv_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
2560 	    NULL, &recv_cookie, &recv_ncookie) != DDI_DMA_MAPPED) {
2561 		mptsas_log(mpt, CE_WARN, "unable to bind DMA resources.");
2562 		rval = DDI_FAILURE;
2563 		goto done;
2564 	}
2565 	recv_dmastate |= MPTSAS_DMA_HANDLE_BOUND;
2566 	bzero(recv_memp, sizeof (MPI2_CONFIG_REPLY));
2567 	configreply = (pMpi2ConfigReply_t)recv_memp;
2568 	recv_numbytes = sizeof (MPI2_CONFIG_REPLY);
2569 
2570 	/*
2571 	 * get config reply message
2572 	 */
2573 	if (mptsas_get_handshake_msg(mpt, recv_memp, recv_numbytes,
2574 	    recv_accessp)) {
2575 		rval = DDI_FAILURE;
2576 		goto done;
2577 	}
2578 
2579 	if (iocstatus = ddi_get16(recv_accessp, &configreply->IOCStatus)) {
2580 		mptsas_log(mpt, CE_WARN, "mptsas_get_manufacture_page5 update: "
2581 		    "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus,
2582 		    ddi_get32(recv_accessp, &configreply->IOCLogInfo));
2583 		goto done;
2584 	}
2585 
2586 	/*
2587 	 * dynamically create a customized dma attribute structure
2588 	 * that describes the MPT's config page structure.
2589 	 */
2590 	page_dma_attrs = mpt->m_msg_dma_attr;
2591 	page_dma_attrs.dma_attr_sgllen = 1;
2592 	page_dma_attrs.dma_attr_granular = (sizeof (MPI2_CONFIG_PAGE_MAN_5));
2593 
2594 	if (ddi_dma_alloc_handle(mpt->m_dip, &page_dma_attrs,
2595 	    DDI_DMA_SLEEP, NULL, &page_dma_handle) != DDI_SUCCESS) {
2596 		mptsas_log(mpt, CE_WARN,
2597 		    "(unable to allocate dma handle.");
2598 		rval = DDI_FAILURE;
2599 		goto done;
2600 	}
2601 	page_dmastate |= MPTSAS_DMA_HANDLE_ALLOCD;
2602 
2603 	if (ddi_dma_mem_alloc(page_dma_handle,
2604 	    (sizeof (MPI2_CONFIG_PAGE_MAN_5)),
2605 	    &mpt->m_dev_acc_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
2606 	    &page_memp, &page_alloc_len, &page_accessp) != DDI_SUCCESS) {
2607 		mptsas_log(mpt, CE_WARN,
2608 		    "unable to allocate manufacturing page structure.");
2609 		rval = DDI_FAILURE;
2610 		goto done;
2611 	}
2612 	page_dmastate |= MPTSAS_DMA_MEMORY_ALLOCD;
2613 
2614 	if (ddi_dma_addr_bind_handle(page_dma_handle, NULL, page_memp,
2615 	    page_alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
2616 	    NULL, &page_cookie, &page_ncookie) != DDI_DMA_MAPPED) {
2617 		mptsas_log(mpt, CE_WARN, "unable to bind DMA resources.");
2618 		rval = DDI_FAILURE;
2619 		goto done;
2620 	}
2621 	page_dmastate |= MPTSAS_DMA_HANDLE_BOUND;
2622 	bzero(page_memp, sizeof (MPI2_CONFIG_PAGE_MAN_5));
2623 	m5 = (pMpi2ManufacturingPage5_t)page_memp;
2624 
2625 	/*
2626 	 * Give reply address to IOC to store config page in and send
2627 	 * config request out.
2628 	 */
2629 
2630 	flagslength = sizeof (MPI2_CONFIG_PAGE_MAN_5);
2631 	flagslength |= ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT |
2632 	    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2633 	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS | MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
2634 	    MPI2_SGE_FLAGS_IOC_TO_HOST |
2635 	    MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
2636 
2637 	if (mptsas_send_config_request_msg(mpt,
2638 	    MPI2_CONFIG_ACTION_PAGE_READ_CURRENT,
2639 	    MPI2_CONFIG_PAGETYPE_MANUFACTURING, 0, 5,
2640 	    ddi_get8(recv_accessp, &configreply->Header.PageVersion),
2641 	    ddi_get8(recv_accessp, &configreply->Header.PageLength),
2642 	    flagslength, page_cookie.dmac_address)) {
2643 		rval = DDI_FAILURE;
2644 		goto done;
2645 	}
2646 
2647 	/*
2648 	 * get reply view handshake
2649 	 */
2650 	if (mptsas_get_handshake_msg(mpt, recv_memp, recv_numbytes,
2651 	    recv_accessp)) {
2652 		rval = DDI_FAILURE;
2653 		goto done;
2654 	}
2655 
2656 	if (iocstatus = ddi_get16(recv_accessp, &configreply->IOCStatus)) {
2657 		mptsas_log(mpt, CE_WARN, "mptsas_get_manufacture_page5 config: "
2658 		    "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus,
2659 		    ddi_get32(recv_accessp, &configreply->IOCLogInfo));
2660 		goto done;
2661 	}
2662 
2663 	(void) ddi_dma_sync(page_dma_handle, 0, 0, DDI_DMA_SYNC_FORCPU);
2664 
2665 	/*
2666 	 * Fusion-MPT stores fields in little-endian format.  This is
2667 	 * why the low-order 32 bits are stored first.
2668 	 */
2669 	mpt->un.sasaddr.m_base_wwid_lo =
2670 	    ddi_get32(page_accessp, (uint32_t *)(void *)&m5->Phy[0].WWID);
2671 	mpt->un.sasaddr.m_base_wwid_hi =
2672 	    ddi_get32(page_accessp, (uint32_t *)(void *)&m5->Phy[0].WWID + 1);
2673 
2674 	if (ddi_prop_update_int64(DDI_DEV_T_NONE, mpt->m_dip,
2675 	    "base-wwid", mpt->un.m_base_wwid) != DDI_PROP_SUCCESS) {
2676 		NDBG2(("%s%d: failed to create base-wwid property",
2677 		    ddi_driver_name(mpt->m_dip), ddi_get_instance(mpt->m_dip)));
2678 	}
2679 
2680 	/*
2681 	 * Set the number of PHYs present.
2682 	 */
2683 	mpt->m_num_phys = ddi_get8(page_accessp, (uint8_t *)&m5->NumPhys);
2684 
2685 	if (ddi_prop_update_int(DDI_DEV_T_NONE, mpt->m_dip,
2686 	    "num-phys", mpt->m_num_phys) != DDI_PROP_SUCCESS) {
2687 		NDBG2(("%s%d: failed to create num-phys property",
2688 		    ddi_driver_name(mpt->m_dip), ddi_get_instance(mpt->m_dip)));
2689 	}
2690 
2691 	mptsas_log(mpt, CE_NOTE, "!mpt%d: Initiator WWNs: 0x%016llx-0x%016llx",
2692 	    mpt->m_instance, (unsigned long long)mpt->un.m_base_wwid,
2693 	    (unsigned long long)mpt->un.m_base_wwid + mpt->m_num_phys - 1);
2694 
2695 	if ((mptsas_check_dma_handle(recv_dma_handle) != DDI_SUCCESS) ||
2696 	    (mptsas_check_dma_handle(page_dma_handle) != DDI_SUCCESS)) {
2697 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2698 		rval = DDI_FAILURE;
2699 		goto done;
2700 	}
2701 	if ((mptsas_check_acc_handle(recv_accessp) != DDI_SUCCESS) ||
2702 	    (mptsas_check_acc_handle(page_accessp) != DDI_SUCCESS)) {
2703 		ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2704 		rval = DDI_FAILURE;
2705 	}
2706 done:
2707 	/*
2708 	 * free up memory
2709 	 */
2710 	if (recv_dmastate & MPTSAS_DMA_HANDLE_BOUND)
2711 		(void) ddi_dma_unbind_handle(recv_dma_handle);
2712 	if (page_dmastate & MPTSAS_DMA_HANDLE_BOUND)
2713 		(void) ddi_dma_unbind_handle(page_dma_handle);
2714 	if (recv_dmastate & MPTSAS_DMA_MEMORY_ALLOCD)
2715 		(void) ddi_dma_mem_free(&recv_accessp);
2716 	if (page_dmastate & MPTSAS_DMA_MEMORY_ALLOCD)
2717 		(void) ddi_dma_mem_free(&page_accessp);
2718 	if (recv_dmastate & MPTSAS_DMA_HANDLE_ALLOCD)
2719 		ddi_dma_free_handle(&recv_dma_handle);
2720 	if (page_dmastate & MPTSAS_DMA_HANDLE_ALLOCD)
2721 		ddi_dma_free_handle(&page_dma_handle);
2722 
2723 	MPTSAS_ENABLE_INTR(mpt);
2724 
2725 	return (rval);
2726 }
2727