xref: /freebsd/sys/dev/mpi3mr/mpi3mr.c (revision 3494f7c0)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2016-2023, Broadcom Inc. All rights reserved.
5  * Support: <fbsd-storage-driver.pdl@broadcom.com>
6  *
7  * Authors: Sumit Saxena <sumit.saxena@broadcom.com>
8  *	    Chandrakanth Patil <chandrakanth.patil@broadcom.com>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are
12  * met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation and/or other
18  *    materials provided with the distribution.
19  * 3. Neither the name of the Broadcom Inc. nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * The views and conclusions contained in the software and documentation are
36  * those of the authors and should not be interpreted as representing
37  * official policies,either expressed or implied, of the FreeBSD Project.
38  *
39  * Mail to: Broadcom Inc 1320 Ridder Park Dr, San Jose, CA 95131
40  *
41  * Broadcom Inc. (Broadcom) MPI3MR Adapter FreeBSD
42  */
43 
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50 #include <sys/conf.h>
51 #include <sys/malloc.h>
52 #include <sys/sysctl.h>
53 #include <sys/uio.h>
54 
55 #include <machine/bus.h>
56 #include <machine/resource.h>
57 #include <sys/rman.h>
58 
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pci_private.h>
62 
63 #include <cam/cam.h>
64 #include <cam/cam_ccb.h>
65 #include <cam/cam_debug.h>
66 #include <cam/cam_sim.h>
67 #include <cam/cam_xpt_sim.h>
68 #include <cam/cam_xpt_periph.h>
69 #include <cam/cam_periph.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.h>
72 #include <cam/scsi/smp_all.h>
73 #include <sys/queue.h>
74 #include <sys/kthread.h>
75 #include "mpi3mr.h"
76 #include "mpi3mr_cam.h"
77 #include "mpi3mr_app.h"
78 
79 static void mpi3mr_repost_reply_buf(struct mpi3mr_softc *sc,
80 	U64 reply_dma);
81 static int mpi3mr_complete_admin_cmd(struct mpi3mr_softc *sc);
82 static void mpi3mr_port_enable_complete(struct mpi3mr_softc *sc,
83 	struct mpi3mr_drvr_cmd *drvrcmd);
84 static void mpi3mr_flush_io(struct mpi3mr_softc *sc);
85 static int mpi3mr_issue_reset(struct mpi3mr_softc *sc, U16 reset_type,
86 	U32 reset_reason);
87 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_softc *sc, U16 handle,
88 	struct mpi3mr_drvr_cmd *cmdparam, U8 iou_rc);
89 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_softc *sc,
90 	struct mpi3mr_drvr_cmd *drv_cmd);
91 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_softc *sc,
92 	struct mpi3mr_drvr_cmd *drv_cmd);
93 static void mpi3mr_send_evt_ack(struct mpi3mr_softc *sc, U8 event,
94 	struct mpi3mr_drvr_cmd *cmdparam, U32 event_ctx);
95 static void mpi3mr_print_fault_info(struct mpi3mr_softc *sc);
96 static inline void mpi3mr_set_diagsave(struct mpi3mr_softc *sc);
97 static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code);
98 
99 void
100 mpi3mr_hexdump(void *buf, int sz, int format)
101 {
102         int i;
103         U32 *buf_loc = (U32 *)buf;
104 
105         for (i = 0; i < (sz / sizeof(U32)); i++) {
106                 if ((i % format) == 0) {
107                         if (i != 0)
108                                 printf("\n");
109                         printf("%08x: ", (i * 4));
110                 }
111                 printf("%08x ", buf_loc[i]);
112         }
113         printf("\n");
114 }
115 
116 void
117 init_completion(struct completion *completion)
118 {
119 	completion->done = 0;
120 }
121 
122 void
123 complete(struct completion *completion)
124 {
125 	completion->done = 1;
126 	wakeup(complete);
127 }
128 
129 void wait_for_completion_timeout(struct completion *completion,
130 	    U32 timeout)
131 {
132 	U32 count = timeout * 1000;
133 
134 	while ((completion->done == 0) && count) {
135                 DELAY(1000);
136 		count--;
137 	}
138 
139 	if (completion->done == 0) {
140 		printf("%s: Command is timedout\n", __func__);
141 		completion->done = 1;
142 	}
143 }
144 void wait_for_completion_timeout_tm(struct completion *completion,
145 	    U32 timeout, struct mpi3mr_softc *sc)
146 {
147 	U32 count = timeout * 1000;
148 
149 	while ((completion->done == 0) && count) {
150 		msleep(&sc->tm_chan, &sc->mpi3mr_mtx, PRIBIO,
151 		       "TM command", 1 * hz);
152 		count--;
153 	}
154 
155 	if (completion->done == 0) {
156 		printf("%s: Command is timedout\n", __func__);
157 		completion->done = 1;
158 	}
159 }
160 
161 
162 void
163 poll_for_command_completion(struct mpi3mr_softc *sc,
164        struct mpi3mr_drvr_cmd *cmd, U16 wait)
165 {
166 	int wait_time = wait * 1000;
167        while (wait_time) {
168                mpi3mr_complete_admin_cmd(sc);
169                if (cmd->state & MPI3MR_CMD_COMPLETE)
170                        break;
171 	       DELAY(1000);
172                wait_time--;
173        }
174 }
175 
176 /**
177  * mpi3mr_trigger_snapdump - triggers firmware snapdump
178  * @sc: Adapter instance reference
179  * @reason_code: reason code for the fault.
180  *
181  * This routine will trigger the snapdump and wait for it to
182  * complete or timeout before it returns.
183  * This will be called during initilaization time faults/resets/timeouts
184  * before soft reset invocation.
185  *
186  * Return:  None.
187  */
188 static void
189 mpi3mr_trigger_snapdump(struct mpi3mr_softc *sc, U32 reason_code)
190 {
191 	U32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
192 
193 	mpi3mr_dprint(sc, MPI3MR_INFO, "snapdump triggered: reason code: %s\n",
194 	    mpi3mr_reset_rc_name(reason_code));
195 
196 	mpi3mr_set_diagsave(sc);
197 	mpi3mr_issue_reset(sc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
198 			   reason_code);
199 
200 	do {
201 		host_diagnostic = mpi3mr_regread(sc, MPI3_SYSIF_HOST_DIAG_OFFSET);
202 		if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
203 			break;
204                 DELAY(100 * 1000);
205 	} while (--timeout);
206 
207 	return;
208 }
209 
210 /**
211  * mpi3mr_check_rh_fault_ioc - check reset history and fault
212  * controller
213  * @sc: Adapter instance reference
214  * @reason_code, reason code for the fault.
215  *
216  * This routine will fault the controller with
217  * the given reason code if it is not already in the fault or
218  * not asynchronosuly reset. This will be used to handle
219  * initilaization time faults/resets/timeout as in those cases
220  * immediate soft reset invocation is not required.
221  *
222  * Return:  None.
223  */
224 static void mpi3mr_check_rh_fault_ioc(struct mpi3mr_softc *sc, U32 reason_code)
225 {
226 	U32 ioc_status;
227 
228 	if (sc->unrecoverable) {
229 		mpi3mr_dprint(sc, MPI3MR_ERROR, "controller is unrecoverable\n");
230 		return;
231 	}
232 
233 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
234 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
235 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
236 		mpi3mr_print_fault_info(sc);
237 		return;
238 	}
239 
240 	mpi3mr_trigger_snapdump(sc, reason_code);
241 
242 	return;
243 }
244 
245 static void * mpi3mr_get_reply_virt_addr(struct mpi3mr_softc *sc,
246     bus_addr_t phys_addr)
247 {
248 	if (!phys_addr)
249 		return NULL;
250 	if ((phys_addr < sc->reply_buf_dma_min_address) ||
251 	    (phys_addr > sc->reply_buf_dma_max_address))
252 		return NULL;
253 
254 	return sc->reply_buf + (phys_addr - sc->reply_buf_phys);
255 }
256 
257 static void * mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_softc *sc,
258     bus_addr_t phys_addr)
259 {
260 	if (!phys_addr)
261 		return NULL;
262 	return sc->sense_buf + (phys_addr - sc->sense_buf_phys);
263 }
264 
265 static void mpi3mr_repost_reply_buf(struct mpi3mr_softc *sc,
266     U64 reply_dma)
267 {
268 	U32 old_idx = 0;
269 
270 	mtx_lock_spin(&sc->reply_free_q_lock);
271 	old_idx  =  sc->reply_free_q_host_index;
272 	sc->reply_free_q_host_index = ((sc->reply_free_q_host_index ==
273 	    (sc->reply_free_q_sz - 1)) ? 0 :
274 	    (sc->reply_free_q_host_index + 1));
275 	sc->reply_free_q[old_idx] = reply_dma;
276 	mpi3mr_regwrite(sc, MPI3_SYSIF_REPLY_FREE_HOST_INDEX_OFFSET,
277 		sc->reply_free_q_host_index);
278 	mtx_unlock_spin(&sc->reply_free_q_lock);
279 }
280 
281 static void mpi3mr_repost_sense_buf(struct mpi3mr_softc *sc,
282     U64 sense_buf_phys)
283 {
284 	U32 old_idx = 0;
285 
286 	mtx_lock_spin(&sc->sense_buf_q_lock);
287 	old_idx  =  sc->sense_buf_q_host_index;
288 	sc->sense_buf_q_host_index = ((sc->sense_buf_q_host_index ==
289 	    (sc->sense_buf_q_sz - 1)) ? 0 :
290 	    (sc->sense_buf_q_host_index + 1));
291 	sc->sense_buf_q[old_idx] = sense_buf_phys;
292 	mpi3mr_regwrite(sc, MPI3_SYSIF_SENSE_BUF_FREE_HOST_INDEX_OFFSET,
293 		sc->sense_buf_q_host_index);
294 	mtx_unlock_spin(&sc->sense_buf_q_lock);
295 
296 }
297 
298 void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_softc *sc,
299 	struct mpi3mr_throttle_group_info *tg, U8 divert_value)
300 {
301 	struct mpi3mr_target *target;
302 
303 	mtx_lock_spin(&sc->target_lock);
304 	TAILQ_FOREACH(target, &sc->cam_sc->tgt_list, tgt_next) {
305 		if (target->throttle_group == tg)
306 			target->io_divert = divert_value;
307 	}
308 	mtx_unlock_spin(&sc->target_lock);
309 }
310 
311 /**
312  * mpi3mr_submit_admin_cmd - Submit request to admin queue
313  * @mrioc: Adapter reference
314  * @admin_req: MPI3 request
315  * @admin_req_sz: Request size
316  *
317  * Post the MPI3 request into admin request queue and
318  * inform the controller, if the queue is full return
319  * appropriate error.
320  *
321  * Return: 0 on success, non-zero on failure.
322  */
323 int mpi3mr_submit_admin_cmd(struct mpi3mr_softc *sc, void *admin_req,
324     U16 admin_req_sz)
325 {
326 	U16 areq_pi = 0, areq_ci = 0, max_entries = 0;
327 	int retval = 0;
328 	U8 *areq_entry;
329 
330 	mtx_lock_spin(&sc->admin_req_lock);
331 	areq_pi = sc->admin_req_pi;
332 	areq_ci = sc->admin_req_ci;
333 	max_entries = sc->num_admin_reqs;
334 
335 	if (sc->unrecoverable)
336 		return -EFAULT;
337 
338 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
339 					   (areq_pi == (max_entries - 1)))) {
340 		printf(IOCNAME "AdminReqQ full condition detected\n",
341 		    sc->name);
342 		retval = -EAGAIN;
343 		goto out;
344 	}
345 	areq_entry = (U8 *)sc->admin_req + (areq_pi *
346 						     MPI3MR_AREQ_FRAME_SZ);
347 	memset(areq_entry, 0, MPI3MR_AREQ_FRAME_SZ);
348 	memcpy(areq_entry, (U8 *)admin_req, admin_req_sz);
349 
350 	if (++areq_pi == max_entries)
351 		areq_pi = 0;
352 	sc->admin_req_pi = areq_pi;
353 
354 	mpi3mr_regwrite(sc, MPI3_SYSIF_ADMIN_REQ_Q_PI_OFFSET, sc->admin_req_pi);
355 
356 out:
357 	mtx_unlock_spin(&sc->admin_req_lock);
358 	return retval;
359 }
360 
361 /**
362  * mpi3mr_check_req_qfull - Check request queue is full or not
363  * @op_req_q: Operational reply queue info
364  *
365  * Return: true when queue full, false otherwise.
366  */
367 static inline bool
368 mpi3mr_check_req_qfull(struct mpi3mr_op_req_queue *op_req_q)
369 {
370 	U16 pi, ci, max_entries;
371 	bool is_qfull = false;
372 
373 	pi = op_req_q->pi;
374 	ci = op_req_q->ci;
375 	max_entries = op_req_q->num_reqs;
376 
377 	if ((ci == (pi + 1)) || ((!ci) && (pi == (max_entries - 1))))
378 		is_qfull = true;
379 
380 	return is_qfull;
381 }
382 
383 /**
384  * mpi3mr_submit_io - Post IO command to firmware
385  * @sc:		      Adapter instance reference
386  * @op_req_q:	      Operational Request queue reference
387  * @req:	      MPT request data
388  *
389  * This function submits IO command to firmware.
390  *
391  * Return: Nothing
392  */
393 int mpi3mr_submit_io(struct mpi3mr_softc *sc,
394     struct mpi3mr_op_req_queue *op_req_q, U8 *req)
395 {
396 	U16 pi, max_entries;
397 	int retval = 0;
398 	U8 *req_entry;
399 	U16 req_sz = sc->facts.op_req_sz;
400 	struct mpi3mr_irq_context *irq_ctx;
401 
402 	mtx_lock_spin(&op_req_q->q_lock);
403 
404 	pi = op_req_q->pi;
405 	max_entries = op_req_q->num_reqs;
406 	if (mpi3mr_check_req_qfull(op_req_q)) {
407 		irq_ctx = &sc->irq_ctx[op_req_q->reply_qid - 1];
408 		mpi3mr_complete_io_cmd(sc, irq_ctx);
409 
410 		if (mpi3mr_check_req_qfull(op_req_q)) {
411 			printf(IOCNAME "OpReqQ full condition detected\n",
412 				sc->name);
413 			retval = -EBUSY;
414 			goto out;
415 		}
416 	}
417 
418 	req_entry = (U8 *)op_req_q->q_base + (pi * req_sz);
419 	memset(req_entry, 0, req_sz);
420 	memcpy(req_entry, req, MPI3MR_AREQ_FRAME_SZ);
421 	if (++pi == max_entries)
422 		pi = 0;
423 	op_req_q->pi = pi;
424 
425 	mpi3mr_atomic_inc(&sc->op_reply_q[op_req_q->reply_qid - 1].pend_ios);
426 
427 	mpi3mr_regwrite(sc, MPI3_SYSIF_OPER_REQ_Q_N_PI_OFFSET(op_req_q->qid), op_req_q->pi);
428 	if (sc->mpi3mr_debug & MPI3MR_TRACE) {
429 		device_printf(sc->mpi3mr_dev, "IO submission: QID:%d PI:0x%x\n", op_req_q->qid, op_req_q->pi);
430 		mpi3mr_hexdump(req_entry, MPI3MR_AREQ_FRAME_SZ, 8);
431 	}
432 
433 out:
434 	mtx_unlock_spin(&op_req_q->q_lock);
435 	return retval;
436 }
437 
438 inline void
439 mpi3mr_add_sg_single(void *paddr, U8 flags, U32 length,
440 		     bus_addr_t dma_addr)
441 {
442 	Mpi3SGESimple_t *sgel = paddr;
443 
444 	sgel->Flags = flags;
445 	sgel->Length = (length);
446 	sgel->Address = (U64)dma_addr;
447 }
448 
449 void mpi3mr_build_zero_len_sge(void *paddr)
450 {
451 	U8 sgl_flags = (MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
452 		MPI3_SGE_FLAGS_DLAS_SYSTEM | MPI3_SGE_FLAGS_END_OF_LIST);
453 
454 	mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1);
455 
456 }
457 
458 void mpi3mr_enable_interrupts(struct mpi3mr_softc *sc)
459 {
460 	sc->intr_enabled = 1;
461 }
462 
463 void mpi3mr_disable_interrupts(struct mpi3mr_softc *sc)
464 {
465 	sc->intr_enabled = 0;
466 }
467 
468 void
469 mpi3mr_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
470 {
471 	bus_addr_t *addr;
472 
473 	addr = arg;
474 	*addr = segs[0].ds_addr;
475 }
476 
477 static int mpi3mr_delete_op_reply_queue(struct mpi3mr_softc *sc, U16 qid)
478 {
479 	Mpi3DeleteReplyQueueRequest_t delq_req;
480 	struct mpi3mr_op_reply_queue *op_reply_q;
481 	int retval = 0;
482 
483 
484 	op_reply_q = &sc->op_reply_q[qid - 1];
485 
486 	if (!op_reply_q->qid)
487 	{
488 		retval = -1;
489 		printf(IOCNAME "Issue DelRepQ: called with invalid Reply QID\n",
490 		    sc->name);
491 		goto out;
492 	}
493 
494 	memset(&delq_req, 0, sizeof(delq_req));
495 
496 	mtx_lock(&sc->init_cmds.completion.lock);
497 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
498 		retval = -1;
499 		printf(IOCNAME "Issue DelRepQ: Init command is in use\n",
500 		    sc->name);
501 		mtx_unlock(&sc->init_cmds.completion.lock);
502 		goto out;
503 	}
504 
505 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
506 		retval = -1;
507 		printf(IOCNAME "Issue DelRepQ: Init command is in use\n",
508 		    sc->name);
509 		goto out;
510 	}
511 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
512 	sc->init_cmds.is_waiting = 1;
513 	sc->init_cmds.callback = NULL;
514 	delq_req.HostTag = MPI3MR_HOSTTAG_INITCMDS;
515 	delq_req.Function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
516 	delq_req.QueueID = qid;
517 
518 	init_completion(&sc->init_cmds.completion);
519 	retval = mpi3mr_submit_admin_cmd(sc, &delq_req, sizeof(delq_req));
520 	if (retval) {
521 		printf(IOCNAME "Issue DelRepQ: Admin Post failed\n",
522 		    sc->name);
523 		goto out_unlock;
524 	}
525 	wait_for_completion_timeout(&sc->init_cmds.completion,
526 	    (MPI3MR_INTADMCMD_TIMEOUT));
527 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
528 		printf(IOCNAME "Issue DelRepQ: command timed out\n",
529 		    sc->name);
530 		mpi3mr_check_rh_fault_ioc(sc,
531 		    MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
532 		sc->unrecoverable = 1;
533 
534 		retval = -1;
535 		goto out_unlock;
536 	}
537 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
538 	     != MPI3_IOCSTATUS_SUCCESS ) {
539 		printf(IOCNAME "Issue DelRepQ: Failed IOCStatus(0x%04x) "
540 		    " Loginfo(0x%08x) \n" , sc->name,
541 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
542 		    sc->init_cmds.ioc_loginfo);
543 		retval = -1;
544 		goto out_unlock;
545 	}
546 	sc->irq_ctx[qid - 1].op_reply_q = NULL;
547 
548 	if (sc->op_reply_q[qid - 1].q_base_phys != 0)
549 		bus_dmamap_unload(sc->op_reply_q[qid - 1].q_base_tag, sc->op_reply_q[qid - 1].q_base_dmamap);
550 	if (sc->op_reply_q[qid - 1].q_base != NULL)
551 		bus_dmamem_free(sc->op_reply_q[qid - 1].q_base_tag, sc->op_reply_q[qid - 1].q_base, sc->op_reply_q[qid - 1].q_base_dmamap);
552 	if (sc->op_reply_q[qid - 1].q_base_tag != NULL)
553 		bus_dma_tag_destroy(sc->op_reply_q[qid - 1].q_base_tag);
554 
555 	sc->op_reply_q[qid - 1].q_base = NULL;
556 	sc->op_reply_q[qid - 1].qid = 0;
557 out_unlock:
558 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
559 	mtx_unlock(&sc->init_cmds.completion.lock);
560 out:
561 	return retval;
562 }
563 
564 /**
565  * mpi3mr_create_op_reply_queue - create operational reply queue
566  * @sc: Adapter instance reference
567  * @qid: operational reply queue id
568  *
569  * Create operatinal reply queue by issuing MPI request
570  * through admin queue.
571  *
572  * Return:  0 on success, non-zero on failure.
573  */
574 static int mpi3mr_create_op_reply_queue(struct mpi3mr_softc *sc, U16 qid)
575 {
576 	Mpi3CreateReplyQueueRequest_t create_req;
577 	struct mpi3mr_op_reply_queue *op_reply_q;
578 	int retval = 0;
579 	char q_lock_name[32];
580 
581 	op_reply_q = &sc->op_reply_q[qid - 1];
582 
583 	if (op_reply_q->qid)
584 	{
585 		retval = -1;
586 		printf(IOCNAME "CreateRepQ: called for duplicate qid %d\n",
587 		    sc->name, op_reply_q->qid);
588 		return retval;
589 	}
590 
591 	op_reply_q->ci = 0;
592 	if (pci_get_revid(sc->mpi3mr_dev) == SAS4116_CHIP_REV_A0)
593 		op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD_A0;
594 	else
595 		op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
596 
597 	op_reply_q->qsz = op_reply_q->num_replies * sc->op_reply_sz;
598 	op_reply_q->ephase = 1;
599 
600         if (!op_reply_q->q_base) {
601 		snprintf(q_lock_name, 32, "Reply Queue Lock[%d]", qid);
602 		mtx_init(&op_reply_q->q_lock, q_lock_name, NULL, MTX_SPIN);
603 
604 		if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
605 					4, 0,			/* algnmnt, boundary */
606 					sc->dma_loaddr,		/* lowaddr */
607 					BUS_SPACE_MAXADDR,	/* highaddr */
608 					NULL, NULL,		/* filter, filterarg */
609 					op_reply_q->qsz,		/* maxsize */
610 					1,			/* nsegments */
611 					op_reply_q->qsz,		/* maxsegsize */
612 					0,			/* flags */
613 					NULL, NULL,		/* lockfunc, lockarg */
614 					&op_reply_q->q_base_tag)) {
615 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate Operational reply DMA tag\n");
616 			return (ENOMEM);
617 		}
618 
619 		if (bus_dmamem_alloc(op_reply_q->q_base_tag, (void **)&op_reply_q->q_base,
620 		    BUS_DMA_NOWAIT, &op_reply_q->q_base_dmamap)) {
621 			mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Cannot allocate replies memory\n", __func__);
622 			return (ENOMEM);
623 		}
624 		bzero(op_reply_q->q_base, op_reply_q->qsz);
625 		bus_dmamap_load(op_reply_q->q_base_tag, op_reply_q->q_base_dmamap, op_reply_q->q_base, op_reply_q->qsz,
626 		    mpi3mr_memaddr_cb, &op_reply_q->q_base_phys, BUS_DMA_NOWAIT);
627 		mpi3mr_dprint(sc, MPI3MR_XINFO, "Operational Reply queue ID: %d phys addr= %#016jx virt_addr: %pa size= %d\n",
628 		    qid, (uintmax_t)op_reply_q->q_base_phys, op_reply_q->q_base, op_reply_q->qsz);
629 
630 		if (!op_reply_q->q_base)
631 		{
632 			retval = -1;
633 			printf(IOCNAME "CreateRepQ: memory alloc failed for qid %d\n",
634 			    sc->name, qid);
635 			goto out;
636 		}
637 	}
638 
639 	memset(&create_req, 0, sizeof(create_req));
640 
641 	mtx_lock(&sc->init_cmds.completion.lock);
642 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
643 		retval = -1;
644 		printf(IOCNAME "CreateRepQ: Init command is in use\n",
645 		    sc->name);
646 		mtx_unlock(&sc->init_cmds.completion.lock);
647 		goto out;
648 	}
649 
650 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
651 	sc->init_cmds.is_waiting = 1;
652 	sc->init_cmds.callback = NULL;
653 	create_req.HostTag = MPI3MR_HOSTTAG_INITCMDS;
654 	create_req.Function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
655 	create_req.QueueID = qid;
656 	create_req.Flags = MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
657 	create_req.MSIxIndex = sc->irq_ctx[qid - 1].msix_index;
658 	create_req.BaseAddress = (U64)op_reply_q->q_base_phys;
659 	create_req.Size = op_reply_q->num_replies;
660 
661 	init_completion(&sc->init_cmds.completion);
662 	retval = mpi3mr_submit_admin_cmd(sc, &create_req,
663 	    sizeof(create_req));
664 	if (retval) {
665 		printf(IOCNAME "CreateRepQ: Admin Post failed\n",
666 		    sc->name);
667 		goto out_unlock;
668 	}
669 
670 	wait_for_completion_timeout(&sc->init_cmds.completion,
671 	  	MPI3MR_INTADMCMD_TIMEOUT);
672 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
673 		printf(IOCNAME "CreateRepQ: command timed out\n",
674 		    sc->name);
675 		mpi3mr_check_rh_fault_ioc(sc,
676 		    MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
677 		sc->unrecoverable = 1;
678 		retval = -1;
679 		goto out_unlock;
680 	}
681 
682 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
683 	     != MPI3_IOCSTATUS_SUCCESS ) {
684 		printf(IOCNAME "CreateRepQ: Failed IOCStatus(0x%04x) "
685 		    " Loginfo(0x%08x) \n" , sc->name,
686 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
687 		    sc->init_cmds.ioc_loginfo);
688 		retval = -1;
689 		goto out_unlock;
690 	}
691 	op_reply_q->qid = qid;
692 	sc->irq_ctx[qid - 1].op_reply_q = op_reply_q;
693 
694 out_unlock:
695 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
696 	mtx_unlock(&sc->init_cmds.completion.lock);
697 out:
698 	if (retval) {
699 		if (op_reply_q->q_base_phys != 0)
700 			bus_dmamap_unload(op_reply_q->q_base_tag, op_reply_q->q_base_dmamap);
701 		if (op_reply_q->q_base != NULL)
702 			bus_dmamem_free(op_reply_q->q_base_tag, op_reply_q->q_base, op_reply_q->q_base_dmamap);
703 		if (op_reply_q->q_base_tag != NULL)
704 			bus_dma_tag_destroy(op_reply_q->q_base_tag);
705 		op_reply_q->q_base = NULL;
706 		op_reply_q->qid = 0;
707 	}
708 
709 	return retval;
710 }
711 
712 /**
713  * mpi3mr_create_op_req_queue - create operational request queue
714  * @sc: Adapter instance reference
715  * @req_qid: operational request queue id
716  * @reply_qid: Reply queue ID
717  *
718  * Create operatinal request queue by issuing MPI request
719  * through admin queue.
720  *
721  * Return:  0 on success, non-zero on failure.
722  */
723 static int mpi3mr_create_op_req_queue(struct mpi3mr_softc *sc, U16 req_qid, U8 reply_qid)
724 {
725 	Mpi3CreateRequestQueueRequest_t create_req;
726 	struct mpi3mr_op_req_queue *op_req_q;
727 	int retval = 0;
728 	char q_lock_name[32];
729 
730 	op_req_q = &sc->op_req_q[req_qid - 1];
731 
732 	if (op_req_q->qid)
733 	{
734 		retval = -1;
735 		printf(IOCNAME "CreateReqQ: called for duplicate qid %d\n",
736 		    sc->name, op_req_q->qid);
737 		return retval;
738 	}
739 
740 	op_req_q->ci = 0;
741 	op_req_q->pi = 0;
742 	op_req_q->num_reqs = MPI3MR_OP_REQ_Q_QD;
743 	op_req_q->qsz = op_req_q->num_reqs * sc->facts.op_req_sz;
744 	op_req_q->reply_qid = reply_qid;
745 
746 	if (!op_req_q->q_base) {
747 		snprintf(q_lock_name, 32, "Request Queue Lock[%d]", req_qid);
748 		mtx_init(&op_req_q->q_lock, q_lock_name, NULL, MTX_SPIN);
749 
750 		if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
751 					4, 0,			/* algnmnt, boundary */
752 					sc->dma_loaddr,		/* lowaddr */
753 					BUS_SPACE_MAXADDR,	/* highaddr */
754 					NULL, NULL,		/* filter, filterarg */
755 					op_req_q->qsz,		/* maxsize */
756 					1,			/* nsegments */
757 					op_req_q->qsz,		/* maxsegsize */
758 					0,			/* flags */
759 					NULL, NULL,		/* lockfunc, lockarg */
760 					&op_req_q->q_base_tag)) {
761 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
762 			return (ENOMEM);
763 		}
764 
765 		if (bus_dmamem_alloc(op_req_q->q_base_tag, (void **)&op_req_q->q_base,
766 		    BUS_DMA_NOWAIT, &op_req_q->q_base_dmamap)) {
767 			mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Cannot allocate replies memory\n", __func__);
768 			return (ENOMEM);
769 		}
770 
771 		bzero(op_req_q->q_base, op_req_q->qsz);
772 
773 		bus_dmamap_load(op_req_q->q_base_tag, op_req_q->q_base_dmamap, op_req_q->q_base, op_req_q->qsz,
774 		    mpi3mr_memaddr_cb, &op_req_q->q_base_phys, BUS_DMA_NOWAIT);
775 
776 		mpi3mr_dprint(sc, MPI3MR_XINFO, "Operational Request QID: %d phys addr= %#016jx virt addr= %pa size= %d associated Reply QID: %d\n",
777 		    req_qid, (uintmax_t)op_req_q->q_base_phys, op_req_q->q_base, op_req_q->qsz, reply_qid);
778 
779 		if (!op_req_q->q_base) {
780 			retval = -1;
781 			printf(IOCNAME "CreateReqQ: memory alloc failed for qid %d\n",
782 			    sc->name, req_qid);
783 			goto out;
784 		}
785 	}
786 
787 	memset(&create_req, 0, sizeof(create_req));
788 
789 	mtx_lock(&sc->init_cmds.completion.lock);
790 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
791 		retval = -1;
792 		printf(IOCNAME "CreateReqQ: Init command is in use\n",
793 		    sc->name);
794 		mtx_unlock(&sc->init_cmds.completion.lock);
795 		goto out;
796 	}
797 
798 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
799 	sc->init_cmds.is_waiting = 1;
800 	sc->init_cmds.callback = NULL;
801 	create_req.HostTag = MPI3MR_HOSTTAG_INITCMDS;
802 	create_req.Function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
803 	create_req.QueueID = req_qid;
804 	create_req.Flags = 0;
805 	create_req.ReplyQueueID = reply_qid;
806 	create_req.BaseAddress = (U64)op_req_q->q_base_phys;
807 	create_req.Size = op_req_q->num_reqs;
808 
809 	init_completion(&sc->init_cmds.completion);
810 	retval = mpi3mr_submit_admin_cmd(sc, &create_req,
811 	    sizeof(create_req));
812 	if (retval) {
813 		printf(IOCNAME "CreateReqQ: Admin Post failed\n",
814 		    sc->name);
815 		goto out_unlock;
816 	}
817 
818 	wait_for_completion_timeout(&sc->init_cmds.completion,
819 	    (MPI3MR_INTADMCMD_TIMEOUT));
820 
821 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
822 		printf(IOCNAME "CreateReqQ: command timed out\n",
823 		    sc->name);
824 		mpi3mr_check_rh_fault_ioc(sc,
825 			MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
826 		sc->unrecoverable = 1;
827 		retval = -1;
828 		goto out_unlock;
829 	}
830 
831 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
832 	     != MPI3_IOCSTATUS_SUCCESS ) {
833 		printf(IOCNAME "CreateReqQ: Failed IOCStatus(0x%04x) "
834 		    " Loginfo(0x%08x) \n" , sc->name,
835 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
836 		    sc->init_cmds.ioc_loginfo);
837 		retval = -1;
838 		goto out_unlock;
839 	}
840 	op_req_q->qid = req_qid;
841 
842 out_unlock:
843 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
844 	mtx_unlock(&sc->init_cmds.completion.lock);
845 out:
846 	if (retval) {
847 		if (op_req_q->q_base_phys != 0)
848 			bus_dmamap_unload(op_req_q->q_base_tag, op_req_q->q_base_dmamap);
849 		if (op_req_q->q_base != NULL)
850 			bus_dmamem_free(op_req_q->q_base_tag, op_req_q->q_base, op_req_q->q_base_dmamap);
851 		if (op_req_q->q_base_tag != NULL)
852 			bus_dma_tag_destroy(op_req_q->q_base_tag);
853 		op_req_q->q_base = NULL;
854 		op_req_q->qid = 0;
855 	}
856 	return retval;
857 }
858 
859 /**
860  * mpi3mr_create_op_queues - create operational queues
861  * @sc: Adapter instance reference
862  *
863  * Create operatinal queues(request queues and reply queues).
864  * Return:  0 on success, non-zero on failure.
865  */
866 static int mpi3mr_create_op_queues(struct mpi3mr_softc *sc)
867 {
868 	int retval = 0;
869 	U16 num_queues = 0, i = 0, qid;
870 
871 	num_queues = min(sc->facts.max_op_reply_q,
872 	    sc->facts.max_op_req_q);
873 	num_queues = min(num_queues, sc->msix_count);
874 
875 	/*
876 	 * During reset set the num_queues to the number of queues
877 	 * that was set before the reset.
878 	 */
879 	if (sc->num_queues)
880 		num_queues = sc->num_queues;
881 
882 	mpi3mr_dprint(sc, MPI3MR_XINFO, "Trying to create %d Operational Q pairs\n",
883 	    num_queues);
884 
885 	if (!sc->op_req_q) {
886 		sc->op_req_q = malloc(sizeof(struct mpi3mr_op_req_queue) *
887 		    num_queues, M_MPI3MR, M_NOWAIT | M_ZERO);
888 
889 		if (!sc->op_req_q) {
890 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to alloc memory for Request queue info\n");
891 			retval = -1;
892 			goto out_failed;
893 		}
894 	}
895 
896 	if (!sc->op_reply_q) {
897 		sc->op_reply_q = malloc(sizeof(struct mpi3mr_op_reply_queue) * num_queues,
898 			M_MPI3MR, M_NOWAIT | M_ZERO);
899 
900 		if (!sc->op_reply_q) {
901 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to alloc memory for Reply queue info\n");
902 			retval = -1;
903 			goto out_failed;
904 		}
905 	}
906 
907 	sc->num_hosttag_op_req_q = (sc->max_host_ios + 1) / num_queues;
908 
909 	/*Operational Request and reply queue ID starts with 1*/
910 	for (i = 0; i < num_queues; i++) {
911 		qid = i + 1;
912 		if (mpi3mr_create_op_reply_queue(sc, qid)) {
913 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to create Reply queue %d\n",
914 			    qid);
915 			break;
916 		}
917 		if (mpi3mr_create_op_req_queue(sc, qid,
918 		    sc->op_reply_q[qid - 1].qid)) {
919 			mpi3mr_delete_op_reply_queue(sc, qid);
920 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to create Request queue %d\n",
921 			    qid);
922 			break;
923 		}
924 
925 	}
926 
927 	/* Not even one queue is created successfully*/
928         if (i == 0) {
929                 retval = -1;
930                 goto out_failed;
931         }
932 
933 	if (!sc->num_queues) {
934 		sc->num_queues = i;
935 	} else {
936 		if (num_queues != i) {
937 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Number of queues (%d) post reset are not same as"
938 					"queues allocated (%d) during driver init\n", i, num_queues);
939 			goto out_failed;
940 		}
941 	}
942 
943 	mpi3mr_dprint(sc, MPI3MR_INFO, "Successfully created %d Operational Queue pairs\n",
944 	    sc->num_queues);
945 	mpi3mr_dprint(sc, MPI3MR_INFO, "Request Queue QD: %d Reply queue QD: %d\n",
946 	    sc->op_req_q[0].num_reqs, sc->op_reply_q[0].num_replies);
947 
948 	return retval;
949 out_failed:
950 	if (sc->op_req_q) {
951 		free(sc->op_req_q, M_MPI3MR);
952 		sc->op_req_q = NULL;
953 	}
954 	if (sc->op_reply_q) {
955 		free(sc->op_reply_q, M_MPI3MR);
956 		sc->op_reply_q = NULL;
957 	}
958 	return retval;
959 }
960 
961 /**
962  * mpi3mr_setup_admin_qpair - Setup admin queue pairs
963  * @sc: Adapter instance reference
964  *
965  * Allocation and setup admin queues(request queues and reply queues).
966  * Return:  0 on success, non-zero on failure.
967  */
968 static int mpi3mr_setup_admin_qpair(struct mpi3mr_softc *sc)
969 {
970 	int retval = 0;
971 	U32 num_adm_entries = 0;
972 
973 	sc->admin_req_q_sz = MPI3MR_AREQQ_SIZE;
974 	sc->num_admin_reqs = sc->admin_req_q_sz / MPI3MR_AREQ_FRAME_SZ;
975 	sc->admin_req_ci = sc->admin_req_pi = 0;
976 
977 	sc->admin_reply_q_sz = MPI3MR_AREPQ_SIZE;
978 	sc->num_admin_replies = sc->admin_reply_q_sz/ MPI3MR_AREP_FRAME_SZ;
979 	sc->admin_reply_ci = 0;
980 	sc->admin_reply_ephase = 1;
981 
982 	if (!sc->admin_req) {
983 		/*
984 		 * We need to create the tag for the admin queue to get the
985 		 * iofacts to see how many bits the controller decodes.  Solve
986 		 * this chicken and egg problem by only doing lower 4GB DMA.
987 		 */
988 		if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
989 					4, 0,			/* algnmnt, boundary */
990 					BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
991 					BUS_SPACE_MAXADDR,	/* highaddr */
992 					NULL, NULL,		/* filter, filterarg */
993 					sc->admin_req_q_sz,	/* maxsize */
994 					1,			/* nsegments */
995 					sc->admin_req_q_sz,	/* maxsegsize */
996 					0,			/* flags */
997 					NULL, NULL,		/* lockfunc, lockarg */
998 					&sc->admin_req_tag)) {
999 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
1000 			return (ENOMEM);
1001 		}
1002 
1003 		if (bus_dmamem_alloc(sc->admin_req_tag, (void **)&sc->admin_req,
1004 		    BUS_DMA_NOWAIT, &sc->admin_req_dmamap)) {
1005 			mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Cannot allocate replies memory\n", __func__);
1006 			return (ENOMEM);
1007 		}
1008 		bzero(sc->admin_req, sc->admin_req_q_sz);
1009 		bus_dmamap_load(sc->admin_req_tag, sc->admin_req_dmamap, sc->admin_req, sc->admin_req_q_sz,
1010 		    mpi3mr_memaddr_cb, &sc->admin_req_phys, BUS_DMA_NOWAIT);
1011 		mpi3mr_dprint(sc, MPI3MR_XINFO, "Admin Req queue phys addr= %#016jx size= %d\n",
1012 		    (uintmax_t)sc->admin_req_phys, sc->admin_req_q_sz);
1013 
1014 		if (!sc->admin_req)
1015 		{
1016 			retval = -1;
1017 			printf(IOCNAME "Memory alloc for AdminReqQ: failed\n",
1018 			    sc->name);
1019 			goto out_failed;
1020 		}
1021 	}
1022 
1023 	if (!sc->admin_reply) {
1024 		mtx_init(&sc->admin_reply_lock, "Admin Reply Queue Lock", NULL, MTX_SPIN);
1025 
1026 		if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
1027 					4, 0,			/* algnmnt, boundary */
1028 					BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1029 					BUS_SPACE_MAXADDR,	/* highaddr */
1030 					NULL, NULL,		/* filter, filterarg */
1031 					sc->admin_reply_q_sz,	/* maxsize */
1032 					1,			/* nsegments */
1033 					sc->admin_reply_q_sz,	/* maxsegsize */
1034 					0,			/* flags */
1035 					NULL, NULL,		/* lockfunc, lockarg */
1036 					&sc->admin_reply_tag)) {
1037 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate reply DMA tag\n");
1038 			return (ENOMEM);
1039 		}
1040 
1041 		if (bus_dmamem_alloc(sc->admin_reply_tag, (void **)&sc->admin_reply,
1042 		    BUS_DMA_NOWAIT, &sc->admin_reply_dmamap)) {
1043 			mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Cannot allocate replies memory\n", __func__);
1044 			return (ENOMEM);
1045 		}
1046 		bzero(sc->admin_reply, sc->admin_reply_q_sz);
1047 		bus_dmamap_load(sc->admin_reply_tag, sc->admin_reply_dmamap, sc->admin_reply, sc->admin_reply_q_sz,
1048 		    mpi3mr_memaddr_cb, &sc->admin_reply_phys, BUS_DMA_NOWAIT);
1049 		mpi3mr_dprint(sc, MPI3MR_XINFO, "Admin Reply queue phys addr= %#016jx size= %d\n",
1050 		    (uintmax_t)sc->admin_reply_phys, sc->admin_req_q_sz);
1051 
1052 
1053 		if (!sc->admin_reply)
1054 		{
1055 			retval = -1;
1056 			printf(IOCNAME "Memory alloc for AdminRepQ: failed\n",
1057 			    sc->name);
1058 			goto out_failed;
1059 		}
1060 	}
1061 
1062 	num_adm_entries = (sc->num_admin_replies << 16) |
1063 				(sc->num_admin_reqs);
1064 	mpi3mr_regwrite(sc, MPI3_SYSIF_ADMIN_Q_NUM_ENTRIES_OFFSET, num_adm_entries);
1065 	mpi3mr_regwrite64(sc, MPI3_SYSIF_ADMIN_REQ_Q_ADDR_LOW_OFFSET, sc->admin_req_phys);
1066 	mpi3mr_regwrite64(sc, MPI3_SYSIF_ADMIN_REPLY_Q_ADDR_LOW_OFFSET, sc->admin_reply_phys);
1067 	mpi3mr_regwrite(sc, MPI3_SYSIF_ADMIN_REQ_Q_PI_OFFSET, sc->admin_req_pi);
1068 	mpi3mr_regwrite(sc, MPI3_SYSIF_ADMIN_REPLY_Q_CI_OFFSET, sc->admin_reply_ci);
1069 
1070 	return retval;
1071 
1072 out_failed:
1073 	/* Free Admin reply*/
1074 	if (sc->admin_reply_phys)
1075 		bus_dmamap_unload(sc->admin_reply_tag, sc->admin_reply_dmamap);
1076 
1077 	if (sc->admin_reply != NULL)
1078 		bus_dmamem_free(sc->admin_reply_tag, sc->admin_reply,
1079 		    sc->admin_reply_dmamap);
1080 
1081 	if (sc->admin_reply_tag != NULL)
1082 		bus_dma_tag_destroy(sc->admin_reply_tag);
1083 
1084 	/* Free Admin request*/
1085 	if (sc->admin_req_phys)
1086 		bus_dmamap_unload(sc->admin_req_tag, sc->admin_req_dmamap);
1087 
1088 	if (sc->admin_req != NULL)
1089 		bus_dmamem_free(sc->admin_req_tag, sc->admin_req,
1090 		    sc->admin_req_dmamap);
1091 
1092 	if (sc->admin_req_tag != NULL)
1093 		bus_dma_tag_destroy(sc->admin_req_tag);
1094 
1095 	return retval;
1096 }
1097 
1098 /**
1099  * mpi3mr_print_fault_info - Display fault information
1100  * @sc: Adapter instance reference
1101  *
1102  * Display the controller fault information if there is a
1103  * controller fault.
1104  *
1105  * Return: Nothing.
1106  */
1107 static void mpi3mr_print_fault_info(struct mpi3mr_softc *sc)
1108 {
1109 	U32 ioc_status, code, code1, code2, code3;
1110 
1111 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
1112 
1113 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
1114 		code = mpi3mr_regread(sc, MPI3_SYSIF_FAULT_OFFSET) &
1115 			MPI3_SYSIF_FAULT_CODE_MASK;
1116 		code1 = mpi3mr_regread(sc, MPI3_SYSIF_FAULT_INFO0_OFFSET);
1117 		code2 = mpi3mr_regread(sc, MPI3_SYSIF_FAULT_INFO1_OFFSET);
1118 		code3 = mpi3mr_regread(sc, MPI3_SYSIF_FAULT_INFO2_OFFSET);
1119 		printf(IOCNAME "fault codes 0x%04x:0x%04x:0x%04x:0x%04x\n",
1120 		    sc->name, code, code1, code2, code3);
1121 	}
1122 }
1123 
1124 enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_softc *sc)
1125 {
1126 	U32 ioc_status, ioc_control;
1127 	U8 ready, enabled;
1128 
1129 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
1130 	ioc_control = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
1131 
1132 	if(sc->unrecoverable)
1133 		return MRIOC_STATE_UNRECOVERABLE;
1134 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
1135 		return MRIOC_STATE_FAULT;
1136 
1137 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
1138 	enabled = (ioc_control & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
1139 
1140 	if (ready && enabled)
1141 		return MRIOC_STATE_READY;
1142 	if ((!ready) && (!enabled))
1143 		return MRIOC_STATE_RESET;
1144 	if ((!ready) && (enabled))
1145 		return MRIOC_STATE_BECOMING_READY;
1146 
1147 	return MRIOC_STATE_RESET_REQUESTED;
1148 }
1149 
1150 static inline void mpi3mr_clear_resethistory(struct mpi3mr_softc *sc)
1151 {
1152         U32 ioc_status;
1153 
1154 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
1155         if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1156 		mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_STATUS_OFFSET, ioc_status);
1157 
1158 }
1159 
1160 /**
1161  * mpi3mr_mur_ioc - Message unit Reset handler
1162  * @sc: Adapter instance reference
1163  * @reset_reason: Reset reason code
1164  *
1165  * Issue Message unit Reset to the controller and wait for it to
1166  * be complete.
1167  *
1168  * Return: 0 on success, -1 on failure.
1169  */
1170 static int mpi3mr_mur_ioc(struct mpi3mr_softc *sc, U32 reset_reason)
1171 {
1172         U32 ioc_config, timeout, ioc_status;
1173         int retval = -1;
1174 
1175         mpi3mr_dprint(sc, MPI3MR_INFO, "Issuing Message Unit Reset(MUR)\n");
1176         if (sc->unrecoverable) {
1177                 mpi3mr_dprint(sc, MPI3MR_ERROR, "IOC is unrecoverable MUR not issued\n");
1178                 return retval;
1179         }
1180         mpi3mr_clear_resethistory(sc);
1181 	mpi3mr_regwrite(sc, MPI3_SYSIF_SCRATCHPAD0_OFFSET, reset_reason);
1182 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
1183         ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1184 	mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET, ioc_config);
1185 
1186         timeout = MPI3MR_MUR_TIMEOUT * 10;
1187         do {
1188 		ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
1189                 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
1190                         mpi3mr_clear_resethistory(sc);
1191 			ioc_config =
1192 				mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
1193                         if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1194                             (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
1195                             (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC))) {
1196                                 retval = 0;
1197                                 break;
1198                         }
1199                 }
1200                 DELAY(100 * 1000);
1201         } while (--timeout);
1202 
1203 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
1204 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
1205 
1206         mpi3mr_dprint(sc, MPI3MR_INFO, "IOC Status/Config after %s MUR is (0x%x)/(0x%x)\n",
1207                 !retval ? "successful":"failed", ioc_status, ioc_config);
1208         return retval;
1209 }
1210 
1211 /**
1212  * mpi3mr_bring_ioc_ready - Bring controller to ready state
1213  * @sc: Adapter instance reference
1214  *
1215  * Set Enable IOC bit in IOC configuration register and wait for
1216  * the controller to become ready.
1217  *
1218  * Return: 0 on success, appropriate error on failure.
1219  */
1220 static int mpi3mr_bring_ioc_ready(struct mpi3mr_softc *sc)
1221 {
1222         U32 ioc_config, timeout;
1223         enum mpi3mr_iocstate current_state;
1224 
1225 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
1226         ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1227 	mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET, ioc_config);
1228 
1229         timeout = sc->ready_timeout * 10;
1230         do {
1231                 current_state = mpi3mr_get_iocstate(sc);
1232                 if (current_state == MRIOC_STATE_READY)
1233                         return 0;
1234                 DELAY(100 * 1000);
1235         } while (--timeout);
1236 
1237         return -1;
1238 }
1239 
1240 static const struct {
1241 	enum mpi3mr_iocstate value;
1242 	char *name;
1243 } mrioc_states[] = {
1244 	{ MRIOC_STATE_READY, "ready" },
1245 	{ MRIOC_STATE_FAULT, "fault" },
1246 	{ MRIOC_STATE_RESET, "reset" },
1247 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
1248 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
1249 	{ MRIOC_STATE_COUNT, "Count" },
1250 };
1251 
1252 static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
1253 {
1254 	int i;
1255 	char *name = NULL;
1256 
1257 	for (i = 0; i < MRIOC_STATE_COUNT; i++) {
1258 		if (mrioc_states[i].value == mrioc_state){
1259 			name = mrioc_states[i].name;
1260 			break;
1261 		}
1262 	}
1263 	return name;
1264 }
1265 
1266 /* Reset reason to name mapper structure*/
1267 static const struct {
1268 	enum mpi3mr_reset_reason value;
1269 	char *name;
1270 } mpi3mr_reset_reason_codes[] = {
1271 	{ MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
1272 	{ MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
1273 	{ MPI3MR_RESET_FROM_IOCTL, "application" },
1274 	{ MPI3MR_RESET_FROM_EH_HOS, "error handling" },
1275 	{ MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
1276 	{ MPI3MR_RESET_FROM_IOCTL_TIMEOUT, "IOCTL timeout" },
1277 	{ MPI3MR_RESET_FROM_SCSIIO_TIMEOUT, "SCSIIO timeout" },
1278 	{ MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
1279 	{ MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
1280 	{ MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
1281 	{ MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
1282 	{ MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
1283 	{ MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
1284 	{ MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
1285 	{
1286 		MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
1287 		"create request queue timeout"
1288 	},
1289 	{
1290 		MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
1291 		"create reply queue timeout"
1292 	},
1293 	{ MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
1294 	{ MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
1295 	{ MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
1296 	{ MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
1297 	{
1298 		MPI3MR_RESET_FROM_CIACTVRST_TIMER,
1299 		"component image activation timeout"
1300 	},
1301 	{
1302 		MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
1303 		"get package version timeout"
1304 	},
1305 	{
1306 		MPI3MR_RESET_FROM_PELABORT_TIMEOUT,
1307 		"persistent event log abort timeout"
1308 	},
1309 	{ MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
1310 	{ MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
1311 	{
1312 		MPI3MR_RESET_FROM_DIAG_BUFFER_POST_TIMEOUT,
1313 		"diagnostic buffer post timeout"
1314 	},
1315 	{ MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronus reset" },
1316 	{ MPI3MR_RESET_REASON_COUNT, "Reset reason count" },
1317 };
1318 
1319 /**
1320  * mpi3mr_reset_rc_name - get reset reason code name
1321  * @reason_code: reset reason code value
1322  *
1323  * Map reset reason to an NULL terminated ASCII string
1324  *
1325  * Return: Name corresponding to reset reason value or NULL.
1326  */
1327 static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
1328 {
1329 	int i;
1330 	char *name = NULL;
1331 
1332 	for (i = 0; i < MPI3MR_RESET_REASON_COUNT; i++) {
1333 		if (mpi3mr_reset_reason_codes[i].value == reason_code) {
1334 			name = mpi3mr_reset_reason_codes[i].name;
1335 			break;
1336 		}
1337 	}
1338 	return name;
1339 }
1340 
1341 #define MAX_RESET_TYPE 3
1342 /* Reset type to name mapper structure*/
1343 static const struct {
1344 	U16 reset_type;
1345 	char *name;
1346 } mpi3mr_reset_types[] = {
1347 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
1348 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
1349 	{ MAX_RESET_TYPE, "count"}
1350 };
1351 
1352 /**
1353  * mpi3mr_reset_type_name - get reset type name
1354  * @reset_type: reset type value
1355  *
1356  * Map reset type to an NULL terminated ASCII string
1357  *
1358  * Return: Name corresponding to reset type value or NULL.
1359  */
1360 static const char *mpi3mr_reset_type_name(U16 reset_type)
1361 {
1362 	int i;
1363 	char *name = NULL;
1364 
1365 	for (i = 0; i < MAX_RESET_TYPE; i++) {
1366 		if (mpi3mr_reset_types[i].reset_type == reset_type) {
1367 			name = mpi3mr_reset_types[i].name;
1368 			break;
1369 		}
1370 	}
1371 	return name;
1372 }
1373 
1374 /**
1375  * mpi3mr_soft_reset_success - Check softreset is success or not
1376  * @ioc_status: IOC status register value
1377  * @ioc_config: IOC config register value
1378  *
1379  * Check whether the soft reset is successful or not based on
1380  * IOC status and IOC config register values.
1381  *
1382  * Return: True when the soft reset is success, false otherwise.
1383  */
1384 static inline bool
1385 mpi3mr_soft_reset_success(U32 ioc_status, U32 ioc_config)
1386 {
1387 	if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1388 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
1389 	    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1390 		return true;
1391 	return false;
1392 }
1393 
1394 /**
1395  * mpi3mr_diagfault_success - Check diag fault is success or not
1396  * @sc: Adapter reference
1397  * @ioc_status: IOC status register value
1398  *
1399  * Check whether the controller hit diag reset fault code.
1400  *
1401  * Return: True when there is diag fault, false otherwise.
1402  */
1403 static inline bool mpi3mr_diagfault_success(struct mpi3mr_softc *sc,
1404 	U32 ioc_status)
1405 {
1406 	U32 fault;
1407 
1408 	if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
1409 		return false;
1410 	fault = mpi3mr_regread(sc, MPI3_SYSIF_FAULT_OFFSET) & MPI3_SYSIF_FAULT_CODE_MASK;
1411 	if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET)
1412 		return true;
1413 	return false;
1414 }
1415 
1416 /**
1417  * mpi3mr_issue_iocfacts - Send IOC Facts
1418  * @sc: Adapter instance reference
1419  * @facts_data: Cached IOC facts data
1420  *
1421  * Issue IOC Facts MPI request through admin queue and wait for
1422  * the completion of it or time out.
1423  *
1424  * Return: 0 on success, non-zero on failures.
1425  */
1426 static int mpi3mr_issue_iocfacts(struct mpi3mr_softc *sc,
1427     Mpi3IOCFactsData_t *facts_data)
1428 {
1429 	Mpi3IOCFactsRequest_t iocfacts_req;
1430 	bus_dma_tag_t data_tag = NULL;
1431 	bus_dmamap_t data_map = NULL;
1432 	bus_addr_t data_phys = 0;
1433 	void *data = NULL;
1434 	U32 data_len = sizeof(*facts_data);
1435 	int retval = 0;
1436 
1437 	U8 sgl_flags = (MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
1438                 	MPI3_SGE_FLAGS_DLAS_SYSTEM |
1439 			MPI3_SGE_FLAGS_END_OF_LIST);
1440 
1441 
1442 	/*
1443 	 * We can't use sc->dma_loaddr here.  We set those only after we get the
1444 	 * iocfacts.  So allocate in the lower 4GB.  The amount of data is tiny
1445 	 * and we don't do this that often, so any bouncing we might have to do
1446 	 * isn't a cause for concern.
1447 	 */
1448         if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
1449 				4, 0,			/* algnmnt, boundary */
1450 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1451 				BUS_SPACE_MAXADDR,	/* highaddr */
1452 				NULL, NULL,		/* filter, filterarg */
1453                                 data_len,		/* maxsize */
1454                                 1,			/* nsegments */
1455                                 data_len,		/* maxsegsize */
1456                                 0,			/* flags */
1457                                 NULL, NULL,		/* lockfunc, lockarg */
1458                                 &data_tag)) {
1459 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
1460 		return (ENOMEM);
1461         }
1462 
1463         if (bus_dmamem_alloc(data_tag, (void **)&data,
1464 	    BUS_DMA_NOWAIT, &data_map)) {
1465 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d Data  DMA mem alloc failed\n",
1466 			__func__, __LINE__);
1467 		return (ENOMEM);
1468         }
1469 
1470         bzero(data, data_len);
1471         bus_dmamap_load(data_tag, data_map, data, data_len,
1472 	    mpi3mr_memaddr_cb, &data_phys, BUS_DMA_NOWAIT);
1473 	mpi3mr_dprint(sc, MPI3MR_XINFO, "Func: %s line: %d IOCfacts data phys addr= %#016jx size= %d\n",
1474 	    __func__, __LINE__, (uintmax_t)data_phys, data_len);
1475 
1476 	if (!data)
1477 	{
1478 		retval = -1;
1479 		printf(IOCNAME "Memory alloc for IOCFactsData: failed\n",
1480 		    sc->name);
1481 		goto out;
1482 	}
1483 
1484 	mtx_lock(&sc->init_cmds.completion.lock);
1485 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
1486 
1487 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
1488 		retval = -1;
1489 		printf(IOCNAME "Issue IOCFacts: Init command is in use\n",
1490 		    sc->name);
1491 		mtx_unlock(&sc->init_cmds.completion.lock);
1492 		goto out;
1493 	}
1494 
1495 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
1496 	sc->init_cmds.is_waiting = 1;
1497 	sc->init_cmds.callback = NULL;
1498 	iocfacts_req.HostTag = (MPI3MR_HOSTTAG_INITCMDS);
1499 	iocfacts_req.Function = MPI3_FUNCTION_IOC_FACTS;
1500 
1501 	mpi3mr_add_sg_single(&iocfacts_req.SGL, sgl_flags, data_len,
1502 	    data_phys);
1503 
1504 	init_completion(&sc->init_cmds.completion);
1505 
1506 	retval = mpi3mr_submit_admin_cmd(sc, &iocfacts_req,
1507 	    sizeof(iocfacts_req));
1508 
1509 	if (retval) {
1510 		printf(IOCNAME "Issue IOCFacts: Admin Post failed\n",
1511 		    sc->name);
1512 		goto out_unlock;
1513 	}
1514 
1515 	wait_for_completion_timeout(&sc->init_cmds.completion,
1516 	    (MPI3MR_INTADMCMD_TIMEOUT));
1517 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1518 		printf(IOCNAME "Issue IOCFacts: command timed out\n",
1519 		    sc->name);
1520 		mpi3mr_check_rh_fault_ioc(sc,
1521 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
1522 		sc->unrecoverable = 1;
1523 		retval = -1;
1524 		goto out_unlock;
1525 	}
1526 
1527 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1528 	     != MPI3_IOCSTATUS_SUCCESS ) {
1529 		printf(IOCNAME "Issue IOCFacts: Failed IOCStatus(0x%04x) "
1530 		    " Loginfo(0x%08x) \n" , sc->name,
1531 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1532 		    sc->init_cmds.ioc_loginfo);
1533 		retval = -1;
1534 		goto out_unlock;
1535 	}
1536 
1537 	memcpy(facts_data, (U8 *)data, data_len);
1538 out_unlock:
1539 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1540 	mtx_unlock(&sc->init_cmds.completion.lock);
1541 
1542 out:
1543 	if (data_phys != 0)
1544 		bus_dmamap_unload(data_tag, data_map);
1545 	if (data != NULL)
1546 		bus_dmamem_free(data_tag, data, data_map);
1547 	if (data_tag != NULL)
1548 		bus_dma_tag_destroy(data_tag);
1549 	return retval;
1550 }
1551 
1552 /**
1553  * mpi3mr_process_factsdata - Process IOC facts data
1554  * @sc: Adapter instance reference
1555  * @facts_data: Cached IOC facts data
1556  *
1557  * Convert IOC facts data into cpu endianness and cache it in
1558  * the driver .
1559  *
1560  * Return: Nothing.
1561  */
1562 static int mpi3mr_process_factsdata(struct mpi3mr_softc *sc,
1563     Mpi3IOCFactsData_t *facts_data)
1564 {
1565 	int retval = 0;
1566 	U32 ioc_config, req_sz, facts_flags;
1567         struct mpi3mr_compimg_ver *fwver;
1568 
1569 	if (le16toh(facts_data->IOCFactsDataLength) !=
1570 	    (sizeof(*facts_data) / 4)) {
1571 		mpi3mr_dprint(sc, MPI3MR_INFO, "IOCFacts data length mismatch "
1572 		    " driver_sz(%ld) firmware_sz(%d) \n",
1573 		    sizeof(*facts_data),
1574 		    facts_data->IOCFactsDataLength);
1575 	}
1576 
1577 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
1578         req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
1579                   MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
1580 
1581 	if (facts_data->IOCRequestFrameSize != (req_sz/4)) {
1582 		 mpi3mr_dprint(sc, MPI3MR_INFO, "IOCFacts data reqFrameSize mismatch "
1583 		    " hw_size(%d) firmware_sz(%d) \n" , req_sz/4,
1584 		    facts_data->IOCRequestFrameSize);
1585 	}
1586 
1587 	memset(&sc->facts, 0, sizeof(sc->facts));
1588 
1589 	facts_flags = le32toh(facts_data->Flags);
1590 	sc->facts.op_req_sz = req_sz;
1591 	sc->op_reply_sz = 1 << ((ioc_config &
1592                                   MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
1593                                   MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
1594 
1595 	sc->facts.ioc_num = facts_data->IOCNumber;
1596         sc->facts.who_init = facts_data->WhoInit;
1597         sc->facts.max_msix_vectors = facts_data->MaxMSIxVectors;
1598 	sc->facts.personality = (facts_flags &
1599 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
1600 	sc->facts.dma_mask = (facts_flags &
1601 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
1602 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
1603         sc->facts.protocol_flags = facts_data->ProtocolFlags;
1604         sc->facts.mpi_version = (facts_data->MPIVersion.Word);
1605         sc->facts.max_reqs = (facts_data->MaxOutstandingRequests);
1606         sc->facts.product_id = (facts_data->ProductID);
1607 	sc->facts.reply_sz = (facts_data->ReplyFrameSize) * 4;
1608         sc->facts.exceptions = (facts_data->IOCExceptions);
1609         sc->facts.max_perids = (facts_data->MaxPersistentID);
1610         sc->facts.max_vds = (facts_data->MaxVDs);
1611         sc->facts.max_hpds = (facts_data->MaxHostPDs);
1612         sc->facts.max_advhpds = (facts_data->MaxAdvHostPDs);
1613         sc->facts.max_raidpds = (facts_data->MaxRAIDPDs);
1614         sc->facts.max_nvme = (facts_data->MaxNVMe);
1615         sc->facts.max_pcieswitches =
1616                 (facts_data->MaxPCIeSwitches);
1617         sc->facts.max_sasexpanders =
1618                 (facts_data->MaxSASExpanders);
1619         sc->facts.max_sasinitiators =
1620                 (facts_data->MaxSASInitiators);
1621         sc->facts.max_enclosures = (facts_data->MaxEnclosures);
1622         sc->facts.min_devhandle = (facts_data->MinDevHandle);
1623         sc->facts.max_devhandle = (facts_data->MaxDevHandle);
1624 	sc->facts.max_op_req_q =
1625                 (facts_data->MaxOperationalRequestQueues);
1626 	sc->facts.max_op_reply_q =
1627                 (facts_data->MaxOperationalReplyQueues);
1628         sc->facts.ioc_capabilities =
1629                 (facts_data->IOCCapabilities);
1630         sc->facts.fw_ver.build_num =
1631                 (facts_data->FWVersion.BuildNum);
1632         sc->facts.fw_ver.cust_id =
1633                 (facts_data->FWVersion.CustomerID);
1634         sc->facts.fw_ver.ph_minor = facts_data->FWVersion.PhaseMinor;
1635         sc->facts.fw_ver.ph_major = facts_data->FWVersion.PhaseMajor;
1636         sc->facts.fw_ver.gen_minor = facts_data->FWVersion.GenMinor;
1637         sc->facts.fw_ver.gen_major = facts_data->FWVersion.GenMajor;
1638         sc->max_msix_vectors = min(sc->max_msix_vectors,
1639             sc->facts.max_msix_vectors);
1640         sc->facts.sge_mod_mask = facts_data->SGEModifierMask;
1641         sc->facts.sge_mod_value = facts_data->SGEModifierValue;
1642         sc->facts.sge_mod_shift = facts_data->SGEModifierShift;
1643         sc->facts.shutdown_timeout =
1644                 (facts_data->ShutdownTimeout);
1645 	sc->facts.max_dev_per_tg = facts_data->MaxDevicesPerThrottleGroup;
1646 	sc->facts.io_throttle_data_length =
1647 	    facts_data->IOThrottleDataLength;
1648 	sc->facts.max_io_throttle_group =
1649 	    facts_data->MaxIOThrottleGroup;
1650 	sc->facts.io_throttle_low = facts_data->IOThrottleLow;
1651 	sc->facts.io_throttle_high = facts_data->IOThrottleHigh;
1652 
1653 	/*Store in 512b block count*/
1654 	if (sc->facts.io_throttle_data_length)
1655 		sc->io_throttle_data_length =
1656 		    (sc->facts.io_throttle_data_length * 2 * 4);
1657 	else
1658 		/* set the length to 1MB + 1K to disable throttle*/
1659 		sc->io_throttle_data_length = MPI3MR_MAX_SECTORS + 2;
1660 
1661 	sc->io_throttle_high = (sc->facts.io_throttle_high * 2 * 1024);
1662 	sc->io_throttle_low = (sc->facts.io_throttle_low * 2 * 1024);
1663 
1664 	fwver = &sc->facts.fw_ver;
1665 	snprintf(sc->fw_version, sizeof(sc->fw_version),
1666 	    "%d.%d.%d.%d.%05d-%05d",
1667 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
1668 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
1669 
1670 	mpi3mr_dprint(sc, MPI3MR_INFO, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),"
1671             "maxreqs(%d), mindh(%d) maxPDs(%d) maxvectors(%d) maxperids(%d)\n",
1672 	    sc->facts.ioc_num, sc->facts.max_op_req_q,
1673 	    sc->facts.max_op_reply_q, sc->facts.max_devhandle,
1674             sc->facts.max_reqs, sc->facts.min_devhandle,
1675             sc->facts.max_pds, sc->facts.max_msix_vectors,
1676             sc->facts.max_perids);
1677         mpi3mr_dprint(sc, MPI3MR_INFO, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x\n",
1678             sc->facts.sge_mod_mask, sc->facts.sge_mod_value,
1679             sc->facts.sge_mod_shift);
1680 	mpi3mr_dprint(sc, MPI3MR_INFO,
1681 	    "max_dev_per_throttle_group(%d), max_throttle_groups(%d), io_throttle_data_len(%dKiB), io_throttle_high(%dMiB), io_throttle_low(%dMiB)\n",
1682 	    sc->facts.max_dev_per_tg, sc->facts.max_io_throttle_group,
1683 	    sc->facts.io_throttle_data_length * 4,
1684 	    sc->facts.io_throttle_high, sc->facts.io_throttle_low);
1685 
1686 	sc->max_host_ios = sc->facts.max_reqs -
1687 	    (MPI3MR_INTERNALCMDS_RESVD + 1);
1688 
1689 	/*
1690 	 * Set the DMA mask for the card.  dma_mask is the number of bits that
1691 	 * can have bits set in them.  Translate this into bus_dma loaddr args.
1692 	 * Add sanity for more bits than address space or other overflow
1693 	 * situations.
1694 	 */
1695 	if (sc->facts.dma_mask == 0 ||
1696 	    (sc->facts.dma_mask >= sizeof(bus_addr_t) * 8))
1697 		sc->dma_loaddr = BUS_SPACE_MAXADDR;
1698 	else
1699 		sc->dma_loaddr = ~((1ull << sc->facts.dma_mask) - 1);
1700 	mpi3mr_dprint(sc, MPI3MR_INFO,
1701 	    "dma_mask bits: %d loaddr 0x%jx\n",
1702 	    sc->facts.dma_mask, sc->dma_loaddr);
1703 
1704 	return retval;
1705 }
1706 
1707 static inline void mpi3mr_setup_reply_free_queues(struct mpi3mr_softc *sc)
1708 {
1709 	int i;
1710 	bus_addr_t phys_addr;
1711 
1712 	/* initialize Reply buffer Queue */
1713 	for (i = 0, phys_addr = sc->reply_buf_phys;
1714 	    i < sc->num_reply_bufs; i++, phys_addr += sc->reply_sz)
1715 		sc->reply_free_q[i] = phys_addr;
1716 	sc->reply_free_q[i] = (0);
1717 
1718 	/* initialize Sense Buffer Queue */
1719 	for (i = 0, phys_addr = sc->sense_buf_phys;
1720 	    i < sc->num_sense_bufs; i++, phys_addr += MPI3MR_SENSEBUF_SZ)
1721 		sc->sense_buf_q[i] = phys_addr;
1722 	sc->sense_buf_q[i] = (0);
1723 
1724 }
1725 
1726 static int mpi3mr_reply_dma_alloc(struct mpi3mr_softc *sc)
1727 {
1728 	U32 sz;
1729 
1730 	sc->num_reply_bufs = sc->facts.max_reqs + MPI3MR_NUM_EVTREPLIES;
1731 	sc->reply_free_q_sz = sc->num_reply_bufs + 1;
1732 	sc->num_sense_bufs = sc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
1733 	sc->sense_buf_q_sz = sc->num_sense_bufs + 1;
1734 
1735 	sz = sc->num_reply_bufs * sc->reply_sz;
1736 
1737 	if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,  /* parent */
1738 				16, 0,			/* algnmnt, boundary */
1739 				sc->dma_loaddr,		/* lowaddr */
1740 				BUS_SPACE_MAXADDR,	/* highaddr */
1741 				NULL, NULL,		/* filter, filterarg */
1742                                 sz,			/* maxsize */
1743                                 1,			/* nsegments */
1744                                 sz,			/* maxsegsize */
1745                                 0,			/* flags */
1746                                 NULL, NULL,		/* lockfunc, lockarg */
1747                                 &sc->reply_buf_tag)) {
1748 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
1749 		return (ENOMEM);
1750         }
1751 
1752 	if (bus_dmamem_alloc(sc->reply_buf_tag, (void **)&sc->reply_buf,
1753 	    BUS_DMA_NOWAIT, &sc->reply_buf_dmamap)) {
1754 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d  DMA mem alloc failed\n",
1755 			__func__, __LINE__);
1756 		return (ENOMEM);
1757         }
1758 
1759 	bzero(sc->reply_buf, sz);
1760         bus_dmamap_load(sc->reply_buf_tag, sc->reply_buf_dmamap, sc->reply_buf, sz,
1761 	    mpi3mr_memaddr_cb, &sc->reply_buf_phys, BUS_DMA_NOWAIT);
1762 
1763 	sc->reply_buf_dma_min_address = sc->reply_buf_phys;
1764 	sc->reply_buf_dma_max_address = sc->reply_buf_phys + sz;
1765 	mpi3mr_dprint(sc, MPI3MR_XINFO, "reply buf (0x%p): depth(%d), frame_size(%d), "
1766 	    "pool_size(%d kB), reply_buf_dma(0x%llx)\n",
1767 	    sc->reply_buf, sc->num_reply_bufs, sc->reply_sz,
1768 	    (sz / 1024), (unsigned long long)sc->reply_buf_phys);
1769 
1770 	/* reply free queue, 8 byte align */
1771 	sz = sc->reply_free_q_sz * 8;
1772 
1773         if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
1774 				8, 0,			/* algnmnt, boundary */
1775 				sc->dma_loaddr,		/* lowaddr */
1776 				BUS_SPACE_MAXADDR,	/* highaddr */
1777 				NULL, NULL,		/* filter, filterarg */
1778                                 sz,			/* maxsize */
1779                                 1,			/* nsegments */
1780                                 sz,			/* maxsegsize */
1781                                 0,			/* flags */
1782                                 NULL, NULL,		/* lockfunc, lockarg */
1783                                 &sc->reply_free_q_tag)) {
1784 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate reply free queue DMA tag\n");
1785 		return (ENOMEM);
1786         }
1787 
1788         if (bus_dmamem_alloc(sc->reply_free_q_tag, (void **)&sc->reply_free_q,
1789 	    BUS_DMA_NOWAIT, &sc->reply_free_q_dmamap)) {
1790 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d  DMA mem alloc failed\n",
1791 			__func__, __LINE__);
1792 		return (ENOMEM);
1793         }
1794 
1795 	bzero(sc->reply_free_q, sz);
1796         bus_dmamap_load(sc->reply_free_q_tag, sc->reply_free_q_dmamap, sc->reply_free_q, sz,
1797 	    mpi3mr_memaddr_cb, &sc->reply_free_q_phys, BUS_DMA_NOWAIT);
1798 
1799 	mpi3mr_dprint(sc, MPI3MR_XINFO, "reply_free_q (0x%p): depth(%d), frame_size(%d), "
1800 	    "pool_size(%d kB), reply_free_q_dma(0x%llx)\n",
1801 	    sc->reply_free_q, sc->reply_free_q_sz, 8, (sz / 1024),
1802 	    (unsigned long long)sc->reply_free_q_phys);
1803 
1804 	/* sense buffer pool,  4 byte align */
1805 	sz = sc->num_sense_bufs * MPI3MR_SENSEBUF_SZ;
1806 
1807         if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
1808 				4, 0,			/* algnmnt, boundary */
1809 				sc->dma_loaddr,		/* lowaddr */
1810 				BUS_SPACE_MAXADDR,	/* highaddr */
1811 				NULL, NULL,		/* filter, filterarg */
1812                                 sz,			/* maxsize */
1813                                 1,			/* nsegments */
1814                                 sz,			/* maxsegsize */
1815                                 0,			/* flags */
1816                                 NULL, NULL,		/* lockfunc, lockarg */
1817                                 &sc->sense_buf_tag)) {
1818 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate Sense buffer DMA tag\n");
1819 		return (ENOMEM);
1820         }
1821 
1822 	if (bus_dmamem_alloc(sc->sense_buf_tag, (void **)&sc->sense_buf,
1823 	    BUS_DMA_NOWAIT, &sc->sense_buf_dmamap)) {
1824 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d  DMA mem alloc failed\n",
1825 			__func__, __LINE__);
1826 		return (ENOMEM);
1827         }
1828 
1829 	bzero(sc->sense_buf, sz);
1830         bus_dmamap_load(sc->sense_buf_tag, sc->sense_buf_dmamap, sc->sense_buf, sz,
1831 	    mpi3mr_memaddr_cb, &sc->sense_buf_phys, BUS_DMA_NOWAIT);
1832 
1833 	mpi3mr_dprint(sc, MPI3MR_XINFO, "sense_buf (0x%p): depth(%d), frame_size(%d), "
1834 	    "pool_size(%d kB), sense_dma(0x%llx)\n",
1835 	    sc->sense_buf, sc->num_sense_bufs, MPI3MR_SENSEBUF_SZ,
1836 	    (sz / 1024), (unsigned long long)sc->sense_buf_phys);
1837 
1838 	/* sense buffer queue, 8 byte align */
1839 	sz = sc->sense_buf_q_sz * 8;
1840 
1841         if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
1842 				8, 0,			/* algnmnt, boundary */
1843 				sc->dma_loaddr,		/* lowaddr */
1844 				BUS_SPACE_MAXADDR,	/* highaddr */
1845 				NULL, NULL,		/* filter, filterarg */
1846                                 sz,			/* maxsize */
1847                                 1,			/* nsegments */
1848                                 sz,			/* maxsegsize */
1849                                 0,			/* flags */
1850                                 NULL, NULL,		/* lockfunc, lockarg */
1851                                 &sc->sense_buf_q_tag)) {
1852 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate Sense buffer Queue DMA tag\n");
1853 		return (ENOMEM);
1854         }
1855 
1856 	if (bus_dmamem_alloc(sc->sense_buf_q_tag, (void **)&sc->sense_buf_q,
1857 	    BUS_DMA_NOWAIT, &sc->sense_buf_q_dmamap)) {
1858 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d  DMA mem alloc failed\n",
1859 			__func__, __LINE__);
1860 		return (ENOMEM);
1861         }
1862 
1863 	bzero(sc->sense_buf_q, sz);
1864         bus_dmamap_load(sc->sense_buf_q_tag, sc->sense_buf_q_dmamap, sc->sense_buf_q, sz,
1865 	    mpi3mr_memaddr_cb, &sc->sense_buf_q_phys, BUS_DMA_NOWAIT);
1866 
1867 	mpi3mr_dprint(sc, MPI3MR_XINFO, "sense_buf_q (0x%p): depth(%d), frame_size(%d), "
1868 	    "pool_size(%d kB), sense_dma(0x%llx)\n",
1869 	    sc->sense_buf_q, sc->sense_buf_q_sz, 8, (sz / 1024),
1870 	    (unsigned long long)sc->sense_buf_q_phys);
1871 
1872 	return 0;
1873 }
1874 
1875 static int mpi3mr_reply_alloc(struct mpi3mr_softc *sc)
1876 {
1877 	int retval = 0;
1878 	U32 i;
1879 
1880 	if (sc->init_cmds.reply)
1881 		goto post_reply_sbuf;
1882 
1883 	sc->init_cmds.reply = malloc(sc->reply_sz,
1884 		M_MPI3MR, M_NOWAIT | M_ZERO);
1885 
1886 	if (!sc->init_cmds.reply) {
1887 		printf(IOCNAME "Cannot allocate memory for init_cmds.reply\n",
1888 		    sc->name);
1889 		goto out_failed;
1890 	}
1891 
1892 	sc->ioctl_cmds.reply = malloc(sc->reply_sz, M_MPI3MR, M_NOWAIT | M_ZERO);
1893 	if (!sc->ioctl_cmds.reply) {
1894 		printf(IOCNAME "Cannot allocate memory for ioctl_cmds.reply\n",
1895 		    sc->name);
1896 		goto out_failed;
1897 	}
1898 
1899 	sc->host_tm_cmds.reply = malloc(sc->reply_sz, M_MPI3MR, M_NOWAIT | M_ZERO);
1900 	if (!sc->host_tm_cmds.reply) {
1901 		printf(IOCNAME "Cannot allocate memory for host_tm.reply\n",
1902 		    sc->name);
1903 		goto out_failed;
1904 	}
1905 	for (i=0; i<MPI3MR_NUM_DEVRMCMD; i++) {
1906 		sc->dev_rmhs_cmds[i].reply = malloc(sc->reply_sz,
1907 		    M_MPI3MR, M_NOWAIT | M_ZERO);
1908 		if (!sc->dev_rmhs_cmds[i].reply) {
1909 			printf(IOCNAME "Cannot allocate memory for"
1910 			    " dev_rmhs_cmd[%d].reply\n",
1911 			    sc->name, i);
1912 			goto out_failed;
1913 		}
1914 	}
1915 
1916 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
1917 		sc->evtack_cmds[i].reply = malloc(sc->reply_sz,
1918 			M_MPI3MR, M_NOWAIT | M_ZERO);
1919 		if (!sc->evtack_cmds[i].reply)
1920 			goto out_failed;
1921 	}
1922 
1923 	sc->dev_handle_bitmap_sz = MPI3MR_DIV_ROUND_UP(sc->facts.max_devhandle, 8);
1924 
1925 	sc->removepend_bitmap = malloc(sc->dev_handle_bitmap_sz,
1926 	    M_MPI3MR, M_NOWAIT | M_ZERO);
1927 	if (!sc->removepend_bitmap) {
1928 		printf(IOCNAME "Cannot alloc memory for remove pend bitmap\n",
1929 		    sc->name);
1930 		goto out_failed;
1931 	}
1932 
1933 	sc->devrem_bitmap_sz = MPI3MR_DIV_ROUND_UP(MPI3MR_NUM_DEVRMCMD, 8);
1934 	sc->devrem_bitmap = malloc(sc->devrem_bitmap_sz,
1935 	    M_MPI3MR, M_NOWAIT | M_ZERO);
1936 	if (!sc->devrem_bitmap) {
1937 		printf(IOCNAME "Cannot alloc memory for dev remove bitmap\n",
1938 		    sc->name);
1939 		goto out_failed;
1940 	}
1941 
1942 	sc->evtack_cmds_bitmap_sz = MPI3MR_DIV_ROUND_UP(MPI3MR_NUM_EVTACKCMD, 8);
1943 
1944 	sc->evtack_cmds_bitmap = malloc(sc->evtack_cmds_bitmap_sz,
1945 		M_MPI3MR, M_NOWAIT | M_ZERO);
1946 	if (!sc->evtack_cmds_bitmap)
1947 		goto out_failed;
1948 
1949 	if (mpi3mr_reply_dma_alloc(sc)) {
1950 		printf(IOCNAME "func:%s line:%d DMA memory allocation failed\n",
1951 		    sc->name, __func__, __LINE__);
1952 		goto out_failed;
1953 	}
1954 
1955 post_reply_sbuf:
1956 	mpi3mr_setup_reply_free_queues(sc);
1957 	return retval;
1958 out_failed:
1959 	mpi3mr_cleanup_interrupts(sc);
1960 	mpi3mr_free_mem(sc);
1961 	retval = -1;
1962 	return retval;
1963 }
1964 
1965 static void
1966 mpi3mr_print_fw_pkg_ver(struct mpi3mr_softc *sc)
1967 {
1968 	int retval = 0;
1969 	void *fw_pkg_ver = NULL;
1970 	bus_dma_tag_t fw_pkg_ver_tag;
1971 	bus_dmamap_t fw_pkg_ver_map;
1972 	bus_addr_t fw_pkg_ver_dma;
1973 	Mpi3CIUploadRequest_t ci_upload;
1974 	Mpi3ComponentImageHeader_t *ci_header;
1975 	U32 fw_pkg_ver_len = sizeof(*ci_header);
1976 	U8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
1977 
1978 	if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,  /* parent */
1979 				4, 0,			/* algnmnt, boundary */
1980 				sc->dma_loaddr,		/* lowaddr */
1981 				BUS_SPACE_MAXADDR,	/* highaddr */
1982 				NULL, NULL,		/* filter, filterarg */
1983 				fw_pkg_ver_len,		/* maxsize */
1984 				1,			/* nsegments */
1985 				fw_pkg_ver_len,		/* maxsegsize */
1986 				0,			/* flags */
1987 				NULL, NULL,		/* lockfunc, lockarg */
1988 				&fw_pkg_ver_tag)) {
1989 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate fw package version request DMA tag\n");
1990 		return;
1991 	}
1992 
1993 	if (bus_dmamem_alloc(fw_pkg_ver_tag, (void **)&fw_pkg_ver, BUS_DMA_NOWAIT, &fw_pkg_ver_map)) {
1994 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d fw package version DMA mem alloc failed\n",
1995 			      __func__, __LINE__);
1996 		return;
1997 	}
1998 
1999 	bzero(fw_pkg_ver, fw_pkg_ver_len);
2000 
2001 	bus_dmamap_load(fw_pkg_ver_tag, fw_pkg_ver_map, fw_pkg_ver, fw_pkg_ver_len,
2002 	    mpi3mr_memaddr_cb, &fw_pkg_ver_dma, BUS_DMA_NOWAIT);
2003 
2004 	mpi3mr_dprint(sc, MPI3MR_XINFO, "Func: %s line: %d fw package version phys addr= %#016jx size= %d\n",
2005 		      __func__, __LINE__, (uintmax_t)fw_pkg_ver_dma, fw_pkg_ver_len);
2006 
2007 	if (!fw_pkg_ver) {
2008 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Memory alloc for fw package version failed\n");
2009 		goto out;
2010 	}
2011 
2012 	memset(&ci_upload, 0, sizeof(ci_upload));
2013 	mtx_lock(&sc->init_cmds.completion.lock);
2014 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
2015 		mpi3mr_dprint(sc, MPI3MR_INFO,"Issue CI Header Upload: command is in use\n");
2016 		mtx_unlock(&sc->init_cmds.completion.lock);
2017 		goto out;
2018 	}
2019 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
2020 	sc->init_cmds.is_waiting = 1;
2021 	sc->init_cmds.callback = NULL;
2022 	ci_upload.HostTag = htole16(MPI3MR_HOSTTAG_INITCMDS);
2023 	ci_upload.Function = MPI3_FUNCTION_CI_UPLOAD;
2024 	ci_upload.MsgFlags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
2025 	ci_upload.ImageOffset = MPI3_IMAGE_HEADER_SIGNATURE0_OFFSET;
2026 	ci_upload.SegmentSize = MPI3_IMAGE_HEADER_SIZE;
2027 
2028 	mpi3mr_add_sg_single(&ci_upload.SGL, sgl_flags, fw_pkg_ver_len,
2029 	    fw_pkg_ver_dma);
2030 
2031 	init_completion(&sc->init_cmds.completion);
2032 	if ((retval = mpi3mr_submit_admin_cmd(sc, &ci_upload, sizeof(ci_upload)))) {
2033 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Issue CI Header Upload: Admin Post failed\n");
2034 		goto out_unlock;
2035 	}
2036 	wait_for_completion_timeout(&sc->init_cmds.completion,
2037 		(MPI3MR_INTADMCMD_TIMEOUT));
2038 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2039 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Issue CI Header Upload: command timed out\n");
2040 		sc->init_cmds.is_waiting = 0;
2041 		if (!(sc->init_cmds.state & MPI3MR_CMD_RESET))
2042 			mpi3mr_check_rh_fault_ioc(sc,
2043 				MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
2044 		goto out_unlock;
2045 	}
2046 	if ((GET_IOC_STATUS(sc->init_cmds.ioc_status)) != MPI3_IOCSTATUS_SUCCESS) {
2047 		mpi3mr_dprint(sc, MPI3MR_ERROR,
2048 			      "Issue CI Header Upload: Failed IOCStatus(0x%04x) Loginfo(0x%08x)\n",
2049 			      GET_IOC_STATUS(sc->init_cmds.ioc_status), sc->init_cmds.ioc_loginfo);
2050 		goto out_unlock;
2051 	}
2052 
2053 	ci_header = (Mpi3ComponentImageHeader_t *) fw_pkg_ver;
2054 	mpi3mr_dprint(sc, MPI3MR_XINFO,
2055 		      "Issue CI Header Upload:EnvVariableOffset(0x%x) \
2056 		      HeaderSize(0x%x) Signature1(0x%x)\n",
2057 		      ci_header->EnvironmentVariableOffset,
2058 		      ci_header->HeaderSize,
2059 		      ci_header->Signature1);
2060 	mpi3mr_dprint(sc, MPI3MR_INFO, "FW Package Version: %02d.%02d.%02d.%02d\n",
2061 		      ci_header->ComponentImageVersion.GenMajor,
2062 		      ci_header->ComponentImageVersion.GenMinor,
2063 		      ci_header->ComponentImageVersion.PhaseMajor,
2064 		      ci_header->ComponentImageVersion.PhaseMinor);
2065 out_unlock:
2066 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2067 	mtx_unlock(&sc->init_cmds.completion.lock);
2068 
2069 out:
2070 	if (fw_pkg_ver_dma != 0)
2071 		bus_dmamap_unload(fw_pkg_ver_tag, fw_pkg_ver_map);
2072 	if (fw_pkg_ver)
2073 		bus_dmamem_free(fw_pkg_ver_tag, fw_pkg_ver, fw_pkg_ver_map);
2074 	if (fw_pkg_ver_tag)
2075 		bus_dma_tag_destroy(fw_pkg_ver_tag);
2076 
2077 }
2078 
2079 /**
2080  * mpi3mr_issue_iocinit - Send IOC Init
2081  * @sc: Adapter instance reference
2082  *
2083  * Issue IOC Init MPI request through admin queue and wait for
2084  * the completion of it or time out.
2085  *
2086  * Return: 0 on success, non-zero on failures.
2087  */
2088 static int mpi3mr_issue_iocinit(struct mpi3mr_softc *sc)
2089 {
2090 	Mpi3IOCInitRequest_t iocinit_req;
2091 	Mpi3DriverInfoLayout_t *drvr_info = NULL;
2092 	bus_dma_tag_t drvr_info_tag;
2093 	bus_dmamap_t drvr_info_map;
2094 	bus_addr_t drvr_info_phys;
2095 	U32 drvr_info_len = sizeof(*drvr_info);
2096 	int retval = 0;
2097 	struct timeval now;
2098 	uint64_t time_in_msec;
2099 
2100 	if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,  /* parent */
2101 				4, 0,			/* algnmnt, boundary */
2102 				sc->dma_loaddr,		/* lowaddr */
2103 				BUS_SPACE_MAXADDR,	/* highaddr */
2104 				NULL, NULL,		/* filter, filterarg */
2105                                 drvr_info_len,		/* maxsize */
2106                                 1,			/* nsegments */
2107                                 drvr_info_len,		/* maxsegsize */
2108                                 0,			/* flags */
2109                                 NULL, NULL,		/* lockfunc, lockarg */
2110                                 &drvr_info_tag)) {
2111 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
2112 		return (ENOMEM);
2113         }
2114 
2115 	if (bus_dmamem_alloc(drvr_info_tag, (void **)&drvr_info,
2116 	    BUS_DMA_NOWAIT, &drvr_info_map)) {
2117 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d Data  DMA mem alloc failed\n",
2118 			__func__, __LINE__);
2119 		return (ENOMEM);
2120         }
2121 
2122 	bzero(drvr_info, drvr_info_len);
2123         bus_dmamap_load(drvr_info_tag, drvr_info_map, drvr_info, drvr_info_len,
2124 	    mpi3mr_memaddr_cb, &drvr_info_phys, BUS_DMA_NOWAIT);
2125 	mpi3mr_dprint(sc, MPI3MR_XINFO, "Func: %s line: %d IOCfacts drvr_info phys addr= %#016jx size= %d\n",
2126 	    __func__, __LINE__, (uintmax_t)drvr_info_phys, drvr_info_len);
2127 
2128 	if (!drvr_info)
2129 	{
2130 		retval = -1;
2131 		printf(IOCNAME "Memory alloc for Driver Info failed\n",
2132 		    sc->name);
2133 		goto out;
2134 	}
2135 	drvr_info->InformationLength = (drvr_info_len);
2136 	strcpy(drvr_info->DriverSignature, "Broadcom");
2137 	strcpy(drvr_info->OsName, "FreeBSD");
2138 	strcpy(drvr_info->OsVersion, fmt_os_ver);
2139 	strcpy(drvr_info->DriverName, MPI3MR_DRIVER_NAME);
2140 	strcpy(drvr_info->DriverVersion, MPI3MR_DRIVER_VERSION);
2141 	strcpy(drvr_info->DriverReleaseDate, MPI3MR_DRIVER_RELDATE);
2142 	drvr_info->DriverCapabilities = 0;
2143 	memcpy((U8 *)&sc->driver_info, (U8 *)drvr_info, sizeof(sc->driver_info));
2144 
2145 	memset(&iocinit_req, 0, sizeof(iocinit_req));
2146 	mtx_lock(&sc->init_cmds.completion.lock);
2147 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
2148 		retval = -1;
2149 		printf(IOCNAME "Issue IOCInit: Init command is in use\n",
2150 		    sc->name);
2151 		mtx_unlock(&sc->init_cmds.completion.lock);
2152 		goto out;
2153 	}
2154 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
2155 	sc->init_cmds.is_waiting = 1;
2156 	sc->init_cmds.callback = NULL;
2157         iocinit_req.HostTag = MPI3MR_HOSTTAG_INITCMDS;
2158         iocinit_req.Function = MPI3_FUNCTION_IOC_INIT;
2159         iocinit_req.MPIVersion.Struct.Dev = MPI3_VERSION_DEV;
2160         iocinit_req.MPIVersion.Struct.Unit = MPI3_VERSION_UNIT;
2161         iocinit_req.MPIVersion.Struct.Major = MPI3_VERSION_MAJOR;
2162         iocinit_req.MPIVersion.Struct.Minor = MPI3_VERSION_MINOR;
2163         iocinit_req.WhoInit = MPI3_WHOINIT_HOST_DRIVER;
2164         iocinit_req.ReplyFreeQueueDepth = sc->reply_free_q_sz;
2165         iocinit_req.ReplyFreeQueueAddress =
2166                 sc->reply_free_q_phys;
2167         iocinit_req.SenseBufferLength = MPI3MR_SENSEBUF_SZ;
2168         iocinit_req.SenseBufferFreeQueueDepth =
2169                 sc->sense_buf_q_sz;
2170         iocinit_req.SenseBufferFreeQueueAddress =
2171                 sc->sense_buf_q_phys;
2172         iocinit_req.DriverInformationAddress = drvr_info_phys;
2173 
2174 	getmicrotime(&now);
2175 	time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000);
2176 	iocinit_req.TimeStamp = htole64(time_in_msec);
2177 
2178 	init_completion(&sc->init_cmds.completion);
2179 	retval = mpi3mr_submit_admin_cmd(sc, &iocinit_req,
2180 	    sizeof(iocinit_req));
2181 
2182 	if (retval) {
2183 		printf(IOCNAME "Issue IOCInit: Admin Post failed\n",
2184 		    sc->name);
2185 		goto out_unlock;
2186 	}
2187 
2188 	wait_for_completion_timeout(&sc->init_cmds.completion,
2189 	    (MPI3MR_INTADMCMD_TIMEOUT));
2190 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2191 		printf(IOCNAME "Issue IOCInit: command timed out\n",
2192 		    sc->name);
2193 		mpi3mr_check_rh_fault_ioc(sc,
2194 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
2195 		sc->unrecoverable = 1;
2196 		retval = -1;
2197 		goto out_unlock;
2198 	}
2199 
2200 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2201 	     != MPI3_IOCSTATUS_SUCCESS ) {
2202 		printf(IOCNAME "Issue IOCInit: Failed IOCStatus(0x%04x) "
2203 		    " Loginfo(0x%08x) \n" , sc->name,
2204 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2205 		    sc->init_cmds.ioc_loginfo);
2206 		retval = -1;
2207 		goto out_unlock;
2208 	}
2209 
2210 out_unlock:
2211 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2212 	mtx_unlock(&sc->init_cmds.completion.lock);
2213 
2214 out:
2215 	if (drvr_info_phys != 0)
2216 		bus_dmamap_unload(drvr_info_tag, drvr_info_map);
2217 	if (drvr_info != NULL)
2218 		bus_dmamem_free(drvr_info_tag, drvr_info, drvr_info_map);
2219 	if (drvr_info_tag != NULL)
2220 		bus_dma_tag_destroy(drvr_info_tag);
2221 	return retval;
2222 }
2223 
2224 static void
2225 mpi3mr_display_ioc_info(struct mpi3mr_softc *sc)
2226 {
2227         int i = 0;
2228         char personality[16];
2229 
2230         switch (sc->facts.personality) {
2231         case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
2232                 strcpy(personality, "Enhanced HBA");
2233                 break;
2234         case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
2235                 strcpy(personality, "RAID");
2236                 break;
2237         default:
2238                 strcpy(personality, "Unknown");
2239                 break;
2240         }
2241 
2242 	mpi3mr_dprint(sc, MPI3MR_INFO, "Current Personality: %s\n", personality);
2243 
2244 	mpi3mr_dprint(sc, MPI3MR_INFO, "%s\n", sc->fw_version);
2245 
2246         mpi3mr_dprint(sc, MPI3MR_INFO, "Protocol=(");
2247 
2248         if (sc->facts.protocol_flags &
2249             MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2250                 printf("Initiator");
2251                 i++;
2252         }
2253 
2254         if (sc->facts.protocol_flags &
2255             MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2256                 printf("%sTarget", i ? "," : "");
2257                 i++;
2258         }
2259 
2260         if (sc->facts.protocol_flags &
2261             MPI3_IOCFACTS_PROTOCOL_NVME) {
2262                 printf("%sNVMe attachment", i ? "," : "");
2263                 i++;
2264         }
2265         i = 0;
2266         printf("), ");
2267         printf("Capabilities=(");
2268 
2269         if (sc->facts.ioc_capabilities &
2270             MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE) {
2271                 printf("RAID");
2272                 i++;
2273         }
2274 
2275         printf(")\n");
2276 }
2277 
2278 /**
2279  * mpi3mr_unmask_events - Unmask events in event mask bitmap
2280  * @sc: Adapter instance reference
2281  * @event: MPI event ID
2282  *
2283  * Un mask the specific event by resetting the event_mask
2284  * bitmap.
2285  *
2286  * Return: None.
2287  */
2288 static void mpi3mr_unmask_events(struct mpi3mr_softc *sc, U16 event)
2289 {
2290 	U32 desired_event;
2291 
2292 	if (event >= 128)
2293 		return;
2294 
2295 	desired_event = (1 << (event % 32));
2296 
2297 	if (event < 32)
2298 		sc->event_masks[0] &= ~desired_event;
2299 	else if (event < 64)
2300 		sc->event_masks[1] &= ~desired_event;
2301 	else if (event < 96)
2302 		sc->event_masks[2] &= ~desired_event;
2303 	else if (event < 128)
2304 		sc->event_masks[3] &= ~desired_event;
2305 }
2306 
2307 static void mpi3mr_set_events_mask(struct mpi3mr_softc *sc)
2308 {
2309 	int i;
2310 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2311 		sc->event_masks[i] = -1;
2312 
2313         mpi3mr_unmask_events(sc, MPI3_EVENT_DEVICE_ADDED);
2314         mpi3mr_unmask_events(sc, MPI3_EVENT_DEVICE_INFO_CHANGED);
2315         mpi3mr_unmask_events(sc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
2316 
2317         mpi3mr_unmask_events(sc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
2318 
2319         mpi3mr_unmask_events(sc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
2320         mpi3mr_unmask_events(sc, MPI3_EVENT_SAS_DISCOVERY);
2321         mpi3mr_unmask_events(sc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
2322         mpi3mr_unmask_events(sc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
2323 
2324         mpi3mr_unmask_events(sc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
2325         mpi3mr_unmask_events(sc, MPI3_EVENT_PCIE_ENUMERATION);
2326 
2327         mpi3mr_unmask_events(sc, MPI3_EVENT_PREPARE_FOR_RESET);
2328         mpi3mr_unmask_events(sc, MPI3_EVENT_CABLE_MGMT);
2329         mpi3mr_unmask_events(sc, MPI3_EVENT_ENERGY_PACK_CHANGE);
2330 }
2331 
2332 /**
2333  * mpi3mr_issue_event_notification - Send event notification
2334  * @sc: Adapter instance reference
2335  *
2336  * Issue event notification MPI request through admin queue and
2337  * wait for the completion of it or time out.
2338  *
2339  * Return: 0 on success, non-zero on failures.
2340  */
2341 int mpi3mr_issue_event_notification(struct mpi3mr_softc *sc)
2342 {
2343 	Mpi3EventNotificationRequest_t evtnotify_req;
2344 	int retval = 0;
2345 	U8 i;
2346 
2347 	memset(&evtnotify_req, 0, sizeof(evtnotify_req));
2348 	mtx_lock(&sc->init_cmds.completion.lock);
2349 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
2350 		retval = -1;
2351 		printf(IOCNAME "Issue EvtNotify: Init command is in use\n",
2352 		    sc->name);
2353 		mtx_unlock(&sc->init_cmds.completion.lock);
2354 		goto out;
2355 	}
2356 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
2357 	sc->init_cmds.is_waiting = 1;
2358 	sc->init_cmds.callback = NULL;
2359 	evtnotify_req.HostTag = (MPI3MR_HOSTTAG_INITCMDS);
2360 	evtnotify_req.Function = MPI3_FUNCTION_EVENT_NOTIFICATION;
2361 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2362 		evtnotify_req.EventMasks[i] =
2363 		    (sc->event_masks[i]);
2364 	init_completion(&sc->init_cmds.completion);
2365 	retval = mpi3mr_submit_admin_cmd(sc, &evtnotify_req,
2366 	    sizeof(evtnotify_req));
2367 	if (retval) {
2368 		printf(IOCNAME "Issue EvtNotify: Admin Post failed\n",
2369 		    sc->name);
2370 		goto out_unlock;
2371 	}
2372 
2373 	poll_for_command_completion(sc,
2374 				    &sc->init_cmds,
2375 				    (MPI3MR_INTADMCMD_TIMEOUT));
2376 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2377 		printf(IOCNAME "Issue EvtNotify: command timed out\n",
2378 		    sc->name);
2379 		mpi3mr_check_rh_fault_ioc(sc,
2380 		    MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
2381 		retval = -1;
2382 		goto out_unlock;
2383 	}
2384 
2385 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2386 	     != MPI3_IOCSTATUS_SUCCESS ) {
2387 		printf(IOCNAME "Issue EvtNotify: Failed IOCStatus(0x%04x) "
2388 		    " Loginfo(0x%08x) \n" , sc->name,
2389 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2390 		    sc->init_cmds.ioc_loginfo);
2391 		retval = -1;
2392 		goto out_unlock;
2393 	}
2394 
2395 out_unlock:
2396 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2397 	mtx_unlock(&sc->init_cmds.completion.lock);
2398 
2399 out:
2400 	return retval;
2401 }
2402 
2403 int
2404 mpi3mr_register_events(struct mpi3mr_softc *sc)
2405 {
2406 	int error;
2407 
2408 	mpi3mr_set_events_mask(sc);
2409 
2410 	error = mpi3mr_issue_event_notification(sc);
2411 
2412 	if (error) {
2413 		printf(IOCNAME "Failed to issue event notification %d\n",
2414 		    sc->name, error);
2415 	}
2416 
2417 	return error;
2418 }
2419 
2420 /**
2421  * mpi3mr_process_event_ack - Process event acknowledgment
2422  * @sc: Adapter instance reference
2423  * @event: MPI3 event ID
2424  * @event_ctx: Event context
2425  *
2426  * Send event acknowledgement through admin queue and wait for
2427  * it to complete.
2428  *
2429  * Return: 0 on success, non-zero on failures.
2430  */
2431 int mpi3mr_process_event_ack(struct mpi3mr_softc *sc, U8 event,
2432 	U32 event_ctx)
2433 {
2434 	Mpi3EventAckRequest_t evtack_req;
2435 	int retval = 0;
2436 
2437 	memset(&evtack_req, 0, sizeof(evtack_req));
2438 	mtx_lock(&sc->init_cmds.completion.lock);
2439 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
2440 		retval = -1;
2441 		printf(IOCNAME "Issue EvtAck: Init command is in use\n",
2442 		    sc->name);
2443 		mtx_unlock(&sc->init_cmds.completion.lock);
2444 		goto out;
2445 	}
2446 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
2447 	sc->init_cmds.is_waiting = 1;
2448 	sc->init_cmds.callback = NULL;
2449 	evtack_req.HostTag = htole16(MPI3MR_HOSTTAG_INITCMDS);
2450 	evtack_req.Function = MPI3_FUNCTION_EVENT_ACK;
2451 	evtack_req.Event = event;
2452 	evtack_req.EventContext = htole32(event_ctx);
2453 
2454 	init_completion(&sc->init_cmds.completion);
2455 	retval = mpi3mr_submit_admin_cmd(sc, &evtack_req,
2456 	    sizeof(evtack_req));
2457 	if (retval) {
2458 		printf(IOCNAME "Issue EvtAck: Admin Post failed\n",
2459 		    sc->name);
2460 		goto out_unlock;
2461 	}
2462 
2463 	wait_for_completion_timeout(&sc->init_cmds.completion,
2464 	    (MPI3MR_INTADMCMD_TIMEOUT));
2465 	if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2466 		printf(IOCNAME "Issue EvtAck: command timed out\n",
2467 		    sc->name);
2468 		retval = -1;
2469 		goto out_unlock;
2470 	}
2471 
2472 	if ((sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2473 	     != MPI3_IOCSTATUS_SUCCESS ) {
2474 		printf(IOCNAME "Issue EvtAck: Failed IOCStatus(0x%04x) "
2475 		    " Loginfo(0x%08x) \n" , sc->name,
2476 		    (sc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2477 		    sc->init_cmds.ioc_loginfo);
2478 		retval = -1;
2479 		goto out_unlock;
2480 	}
2481 
2482 out_unlock:
2483 	sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2484 	mtx_unlock(&sc->init_cmds.completion.lock);
2485 
2486 out:
2487 	return retval;
2488 }
2489 
2490 
2491 static int mpi3mr_alloc_chain_bufs(struct mpi3mr_softc *sc)
2492 {
2493 	int retval = 0;
2494 	U32 sz, i;
2495 	U16 num_chains;
2496 
2497 	num_chains = sc->max_host_ios;
2498 
2499 	sc->chain_buf_count = num_chains;
2500 	sz = sizeof(struct mpi3mr_chain) * num_chains;
2501 
2502 	sc->chain_sgl_list = malloc(sz, M_MPI3MR, M_NOWAIT | M_ZERO);
2503 
2504 	if (!sc->chain_sgl_list) {
2505 		printf(IOCNAME "Cannot allocate memory for chain SGL list\n",
2506 		    sc->name);
2507 		retval = -1;
2508 		goto out_failed;
2509 	}
2510 
2511 	sz = MPI3MR_CHAINSGE_SIZE;
2512 
2513         if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,  /* parent */
2514 				4096, 0,		/* algnmnt, boundary */
2515 				sc->dma_loaddr,		/* lowaddr */
2516 				BUS_SPACE_MAXADDR,	/* highaddr */
2517 				NULL, NULL,		/* filter, filterarg */
2518                                 sz,			/* maxsize */
2519                                 1,			/* nsegments */
2520                                 sz,			/* maxsegsize */
2521                                 0,			/* flags */
2522                                 NULL, NULL,		/* lockfunc, lockarg */
2523                                 &sc->chain_sgl_list_tag)) {
2524 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate Chain buffer DMA tag\n");
2525 		return (ENOMEM);
2526         }
2527 
2528 	for (i = 0; i < num_chains; i++) {
2529 		if (bus_dmamem_alloc(sc->chain_sgl_list_tag, (void **)&sc->chain_sgl_list[i].buf,
2530 		    BUS_DMA_NOWAIT, &sc->chain_sgl_list[i].buf_dmamap)) {
2531 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Func: %s line: %d  DMA mem alloc failed\n",
2532 				__func__, __LINE__);
2533 			return (ENOMEM);
2534 		}
2535 
2536 		bzero(sc->chain_sgl_list[i].buf, sz);
2537 		bus_dmamap_load(sc->chain_sgl_list_tag, sc->chain_sgl_list[i].buf_dmamap, sc->chain_sgl_list[i].buf, sz,
2538 		    mpi3mr_memaddr_cb, &sc->chain_sgl_list[i].buf_phys, BUS_DMA_NOWAIT);
2539 		mpi3mr_dprint(sc, MPI3MR_XINFO, "Func: %s line: %d phys addr= %#016jx size= %d\n",
2540 		    __func__, __LINE__, (uintmax_t)sc->chain_sgl_list[i].buf_phys, sz);
2541 	}
2542 
2543 	sc->chain_bitmap_sz = MPI3MR_DIV_ROUND_UP(num_chains, 8);
2544 
2545 	sc->chain_bitmap = malloc(sc->chain_bitmap_sz, M_MPI3MR, M_NOWAIT | M_ZERO);
2546 	if (!sc->chain_bitmap) {
2547 		mpi3mr_dprint(sc, MPI3MR_INFO, "Cannot alloc memory for chain bitmap\n");
2548 		retval = -1;
2549 		goto out_failed;
2550 	}
2551 	return retval;
2552 
2553 out_failed:
2554 	for (i = 0; i < num_chains; i++) {
2555 		if (sc->chain_sgl_list[i].buf_phys != 0)
2556 			bus_dmamap_unload(sc->chain_sgl_list_tag, sc->chain_sgl_list[i].buf_dmamap);
2557 		if (sc->chain_sgl_list[i].buf != NULL)
2558 			bus_dmamem_free(sc->chain_sgl_list_tag, sc->chain_sgl_list[i].buf, sc->chain_sgl_list[i].buf_dmamap);
2559 	}
2560 	if (sc->chain_sgl_list_tag != NULL)
2561 		bus_dma_tag_destroy(sc->chain_sgl_list_tag);
2562 	return retval;
2563 }
2564 
2565 static int mpi3mr_pel_alloc(struct mpi3mr_softc *sc)
2566 {
2567 	int retval = 0;
2568 
2569 	if (!sc->pel_cmds.reply) {
2570 		sc->pel_cmds.reply = malloc(sc->reply_sz, M_MPI3MR, M_NOWAIT | M_ZERO);
2571 		if (!sc->pel_cmds.reply) {
2572 			printf(IOCNAME "Cannot allocate memory for pel_cmds.reply\n",
2573 			    sc->name);
2574 			goto out_failed;
2575 		}
2576 	}
2577 
2578 	if (!sc->pel_abort_cmd.reply) {
2579 		sc->pel_abort_cmd.reply = malloc(sc->reply_sz, M_MPI3MR, M_NOWAIT | M_ZERO);
2580 		if (!sc->pel_abort_cmd.reply) {
2581 			printf(IOCNAME "Cannot allocate memory for pel_abort_cmd.reply\n",
2582 			    sc->name);
2583 			goto out_failed;
2584 		}
2585 	}
2586 
2587 	if (!sc->pel_seq_number) {
2588 		sc->pel_seq_number_sz = sizeof(Mpi3PELSeq_t);
2589 		if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,   /* parent */
2590 				 4, 0,                           /* alignment, boundary */
2591 				 sc->dma_loaddr,	         /* lowaddr */
2592 				 BUS_SPACE_MAXADDR,		 /* highaddr */
2593 				 NULL, NULL,                     /* filter, filterarg */
2594 				 sc->pel_seq_number_sz,		 /* maxsize */
2595 				 1,                              /* nsegments */
2596 				 sc->pel_seq_number_sz,          /* maxsegsize */
2597 				 0,                              /* flags */
2598 				 NULL, NULL,                     /* lockfunc, lockarg */
2599 				 &sc->pel_seq_num_dmatag)) {
2600 			 mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot create PEL seq number dma memory tag\n");
2601 			 retval = -ENOMEM;
2602 			 goto out_failed;
2603 		}
2604 
2605 		if (bus_dmamem_alloc(sc->pel_seq_num_dmatag, (void **)&sc->pel_seq_number,
2606 		    BUS_DMA_NOWAIT, &sc->pel_seq_num_dmamap)) {
2607 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate PEL seq number kernel buffer dma memory\n");
2608 			retval = -ENOMEM;
2609 			goto out_failed;
2610 		}
2611 
2612 		bzero(sc->pel_seq_number, sc->pel_seq_number_sz);
2613 
2614 		bus_dmamap_load(sc->pel_seq_num_dmatag, sc->pel_seq_num_dmamap, sc->pel_seq_number,
2615 		    sc->pel_seq_number_sz, mpi3mr_memaddr_cb, &sc->pel_seq_number_dma, BUS_DMA_NOWAIT);
2616 
2617 		if (!sc->pel_seq_number) {
2618 			printf(IOCNAME "%s:%d Cannot load PEL seq number dma memory for size: %d\n", sc->name,
2619 				__func__, __LINE__, sc->pel_seq_number_sz);
2620 			retval = -ENOMEM;
2621 			goto out_failed;
2622 		}
2623 	}
2624 
2625 out_failed:
2626 	return retval;
2627 }
2628 
2629 /**
2630  * mpi3mr_validate_fw_update - validate IOCFacts post adapter reset
2631  * @sc: Adapter instance reference
2632  *
2633  * Return zero if the new IOCFacts is compatible with previous values
2634  * else return appropriate error
2635  */
2636 static int
2637 mpi3mr_validate_fw_update(struct mpi3mr_softc *sc)
2638 {
2639 	U16 dev_handle_bitmap_sz;
2640 	U8 *removepend_bitmap;
2641 
2642 	if (sc->facts.reply_sz > sc->reply_sz) {
2643 		mpi3mr_dprint(sc, MPI3MR_ERROR,
2644 		    "Cannot increase reply size from %d to %d\n",
2645 		    sc->reply_sz, sc->reply_sz);
2646 		return -EPERM;
2647 	}
2648 
2649 	if (sc->num_io_throttle_group != sc->facts.max_io_throttle_group) {
2650 		mpi3mr_dprint(sc, MPI3MR_ERROR,
2651 		    "max io throttle group doesn't match old(%d), new(%d)\n",
2652 		    sc->num_io_throttle_group,
2653 		    sc->facts.max_io_throttle_group);
2654 		return -EPERM;
2655 	}
2656 
2657 	if (sc->facts.max_op_reply_q < sc->num_queues) {
2658 		mpi3mr_dprint(sc, MPI3MR_ERROR,
2659 		    "Cannot reduce number of operational reply queues from %d to %d\n",
2660 		    sc->num_queues,
2661 		    sc->facts.max_op_reply_q);
2662 		return -EPERM;
2663 	}
2664 
2665 	if (sc->facts.max_op_req_q < sc->num_queues) {
2666 		mpi3mr_dprint(sc, MPI3MR_ERROR,
2667 		    "Cannot reduce number of operational request queues from %d to %d\n",
2668 		    sc->num_queues, sc->facts.max_op_req_q);
2669 		return -EPERM;
2670 	}
2671 
2672 	dev_handle_bitmap_sz = MPI3MR_DIV_ROUND_UP(sc->facts.max_devhandle, 8);
2673 
2674 	if (dev_handle_bitmap_sz > sc->dev_handle_bitmap_sz) {
2675 		removepend_bitmap = realloc(sc->removepend_bitmap,
2676 		    dev_handle_bitmap_sz, M_MPI3MR, M_NOWAIT);
2677 
2678 		if (!removepend_bitmap) {
2679 			mpi3mr_dprint(sc, MPI3MR_ERROR,
2680 			    "failed to increase removepend_bitmap sz from: %d to %d\n",
2681 			    sc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
2682 			return -ENOMEM;
2683 		}
2684 
2685 		memset(removepend_bitmap + sc->dev_handle_bitmap_sz, 0,
2686 		    dev_handle_bitmap_sz - sc->dev_handle_bitmap_sz);
2687 		sc->removepend_bitmap = removepend_bitmap;
2688 		mpi3mr_dprint(sc, MPI3MR_INFO,
2689 		    "increased dev_handle_bitmap_sz from %d to %d\n",
2690 		    sc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
2691 		sc->dev_handle_bitmap_sz = dev_handle_bitmap_sz;
2692 	}
2693 
2694 	return 0;
2695 }
2696 
2697 /*
2698  * mpi3mr_initialize_ioc - Controller initialization
2699  * @dev: pointer to device struct
2700  *
2701  * This function allocates the controller wide resources and brings
2702  * the controller to operational state
2703  *
2704  * Return: 0 on success and proper error codes on failure
2705  */
2706 int mpi3mr_initialize_ioc(struct mpi3mr_softc *sc, U8 init_type)
2707 {
2708 	int retval = 0;
2709 	enum mpi3mr_iocstate ioc_state;
2710 	U64 ioc_info;
2711 	U32 ioc_status, ioc_control, i, timeout;
2712 	Mpi3IOCFactsData_t facts_data;
2713 	char str[32];
2714 	U32 size;
2715 
2716 	sc->cpu_count = mp_ncpus;
2717 
2718 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
2719 	ioc_control = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
2720 	ioc_info = mpi3mr_regread64(sc, MPI3_SYSIF_IOC_INFO_LOW_OFFSET);
2721 
2722 	mpi3mr_dprint(sc, MPI3MR_INFO, "SOD ioc_status: 0x%x ioc_control: 0x%x "
2723 	    "ioc_info: 0x%lx\n", ioc_status, ioc_control, ioc_info);
2724 
2725         /*The timeout value is in 2sec unit, changing it to seconds*/
2726 	sc->ready_timeout =
2727                 ((ioc_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
2728                     MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
2729 
2730 	ioc_state = mpi3mr_get_iocstate(sc);
2731 
2732 	mpi3mr_dprint(sc, MPI3MR_INFO, "IOC state: %s   IOC ready timeout: %d\n",
2733 	    mpi3mr_iocstate_name(ioc_state), sc->ready_timeout);
2734 
2735 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
2736 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
2737 		timeout = sc->ready_timeout * 10;
2738 		do {
2739 			DELAY(1000 * 100);
2740 		} while (--timeout);
2741 
2742 		ioc_state = mpi3mr_get_iocstate(sc);
2743 		mpi3mr_dprint(sc, MPI3MR_INFO,
2744 			"IOC in %s state after waiting for reset time\n",
2745 			mpi3mr_iocstate_name(ioc_state));
2746 	}
2747 
2748 	if (ioc_state == MRIOC_STATE_READY) {
2749                 retval = mpi3mr_mur_ioc(sc, MPI3MR_RESET_FROM_BRINGUP);
2750                 if (retval) {
2751                         mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to MU reset IOC, error 0x%x\n",
2752                                 retval);
2753                 }
2754                 ioc_state = mpi3mr_get_iocstate(sc);
2755         }
2756 
2757         if (ioc_state != MRIOC_STATE_RESET) {
2758                 mpi3mr_print_fault_info(sc);
2759 		 mpi3mr_dprint(sc, MPI3MR_ERROR, "issuing soft reset to bring to reset state\n");
2760                  retval = mpi3mr_issue_reset(sc,
2761                      MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
2762                      MPI3MR_RESET_FROM_BRINGUP);
2763                 if (retval) {
2764                         mpi3mr_dprint(sc, MPI3MR_ERROR,
2765                             "%s :Failed to soft reset IOC, error 0x%d\n",
2766                             __func__, retval);
2767                         goto out_failed;
2768                 }
2769         }
2770 
2771 	ioc_state = mpi3mr_get_iocstate(sc);
2772 
2773         if (ioc_state != MRIOC_STATE_RESET) {
2774 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot bring IOC to reset state\n");
2775 		goto out_failed;
2776         }
2777 
2778 	retval = mpi3mr_setup_admin_qpair(sc);
2779 	if (retval) {
2780 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to setup Admin queues, error 0x%x\n",
2781 		    retval);
2782 		goto out_failed;
2783 	}
2784 
2785 	retval = mpi3mr_bring_ioc_ready(sc);
2786 	if (retval) {
2787 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to bring IOC ready, error 0x%x\n",
2788 		    retval);
2789 		goto out_failed;
2790 	}
2791 
2792 	if (init_type == MPI3MR_INIT_TYPE_INIT) {
2793 		retval = mpi3mr_alloc_interrupts(sc, 1);
2794 		if (retval) {
2795 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to allocate interrupts, error 0x%x\n",
2796 			    retval);
2797 			goto out_failed;
2798 		}
2799 
2800 		retval = mpi3mr_setup_irqs(sc);
2801 		if (retval) {
2802 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to setup ISR, error 0x%x\n",
2803 			    retval);
2804 			goto out_failed;
2805 		}
2806 	}
2807 
2808 	mpi3mr_enable_interrupts(sc);
2809 
2810 	if (init_type == MPI3MR_INIT_TYPE_INIT) {
2811 		mtx_init(&sc->mpi3mr_mtx, "SIM lock", NULL, MTX_DEF);
2812 		mtx_init(&sc->io_lock, "IO lock", NULL, MTX_DEF);
2813 		mtx_init(&sc->admin_req_lock, "Admin Request Queue lock", NULL, MTX_SPIN);
2814 		mtx_init(&sc->reply_free_q_lock, "Reply free Queue lock", NULL, MTX_SPIN);
2815 		mtx_init(&sc->sense_buf_q_lock, "Sense buffer Queue lock", NULL, MTX_SPIN);
2816 		mtx_init(&sc->chain_buf_lock, "Chain buffer lock", NULL, MTX_SPIN);
2817 		mtx_init(&sc->cmd_pool_lock, "Command pool lock", NULL, MTX_DEF);
2818 		mtx_init(&sc->fwevt_lock, "Firmware Event lock", NULL, MTX_DEF);
2819 		mtx_init(&sc->target_lock, "Target lock", NULL, MTX_SPIN);
2820 		mtx_init(&sc->reset_mutex, "Reset lock", NULL, MTX_DEF);
2821 
2822 		mtx_init(&sc->init_cmds.completion.lock, "Init commands lock", NULL, MTX_DEF);
2823 		sc->init_cmds.reply = NULL;
2824 		sc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2825 		sc->init_cmds.dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2826 		sc->init_cmds.host_tag = MPI3MR_HOSTTAG_INITCMDS;
2827 
2828 		mtx_init(&sc->ioctl_cmds.completion.lock, "IOCTL commands lock", NULL, MTX_DEF);
2829 		sc->ioctl_cmds.reply = NULL;
2830 		sc->ioctl_cmds.state = MPI3MR_CMD_NOTUSED;
2831 		sc->ioctl_cmds.dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2832 		sc->ioctl_cmds.host_tag = MPI3MR_HOSTTAG_IOCTLCMDS;
2833 
2834 		mtx_init(&sc->pel_abort_cmd.completion.lock, "PEL Abort command lock", NULL, MTX_DEF);
2835 		sc->pel_abort_cmd.reply = NULL;
2836 		sc->pel_abort_cmd.state = MPI3MR_CMD_NOTUSED;
2837 		sc->pel_abort_cmd.dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2838 		sc->pel_abort_cmd.host_tag = MPI3MR_HOSTTAG_PELABORT;
2839 
2840 		mtx_init(&sc->host_tm_cmds.completion.lock, "TM commands lock", NULL, MTX_DEF);
2841 		sc->host_tm_cmds.reply = NULL;
2842 		sc->host_tm_cmds.state = MPI3MR_CMD_NOTUSED;
2843 		sc->host_tm_cmds.dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2844 		sc->host_tm_cmds.host_tag = MPI3MR_HOSTTAG_TMS;
2845 
2846 		TAILQ_INIT(&sc->cmd_list_head);
2847 		TAILQ_INIT(&sc->event_list);
2848 		TAILQ_INIT(&sc->delayed_rmhs_list);
2849 		TAILQ_INIT(&sc->delayed_evtack_cmds_list);
2850 
2851 		for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
2852 			snprintf(str, 32, "Dev REMHS commands lock[%d]", i);
2853 			mtx_init(&sc->dev_rmhs_cmds[i].completion.lock, str, NULL, MTX_DEF);
2854 			sc->dev_rmhs_cmds[i].reply = NULL;
2855 			sc->dev_rmhs_cmds[i].state = MPI3MR_CMD_NOTUSED;
2856 			sc->dev_rmhs_cmds[i].dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2857 			sc->dev_rmhs_cmds[i].host_tag = MPI3MR_HOSTTAG_DEVRMCMD_MIN
2858 							    + i;
2859 		}
2860 	}
2861 
2862 	retval = mpi3mr_issue_iocfacts(sc, &facts_data);
2863 	if (retval) {
2864 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to Issue IOC Facts, retval: 0x%x\n",
2865 		    retval);
2866 		goto out_failed;
2867 	}
2868 
2869 	retval = mpi3mr_process_factsdata(sc, &facts_data);
2870 	if (retval) {
2871 		mpi3mr_dprint(sc, MPI3MR_ERROR, "IOC Facts data processing failedi, retval: 0x%x\n",
2872 		    retval);
2873 		goto out_failed;
2874 	}
2875 
2876 	sc->num_io_throttle_group = sc->facts.max_io_throttle_group;
2877 	mpi3mr_atomic_set(&sc->pend_large_data_sz, 0);
2878 
2879 	if (init_type == MPI3MR_INIT_TYPE_RESET) {
2880 		retval = mpi3mr_validate_fw_update(sc);
2881 		if (retval)
2882 			goto out_failed;
2883 	} else {
2884 		sc->reply_sz = sc->facts.reply_sz;
2885 	}
2886 
2887 	mpi3mr_display_ioc_info(sc);
2888 
2889 	retval = mpi3mr_reply_alloc(sc);
2890 	if (retval) {
2891 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to allocated reply and sense buffers, retval: 0x%x\n",
2892 		    retval);
2893 		goto out_failed;
2894 	}
2895 
2896 	if (init_type == MPI3MR_INIT_TYPE_INIT) {
2897 		retval = mpi3mr_alloc_chain_bufs(sc);
2898 		if (retval) {
2899 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to allocated chain buffers, retval: 0x%x\n",
2900 			    retval);
2901 			goto out_failed;
2902 		}
2903 	}
2904 
2905 	retval = mpi3mr_issue_iocinit(sc);
2906 	if (retval) {
2907 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to Issue IOC Init, retval: 0x%x\n",
2908 		    retval);
2909 		goto out_failed;
2910 	}
2911 
2912 	mpi3mr_print_fw_pkg_ver(sc);
2913 
2914 	sc->reply_free_q_host_index = sc->num_reply_bufs;
2915 	mpi3mr_regwrite(sc, MPI3_SYSIF_REPLY_FREE_HOST_INDEX_OFFSET,
2916 		sc->reply_free_q_host_index);
2917 
2918 	sc->sense_buf_q_host_index = sc->num_sense_bufs;
2919 
2920 	mpi3mr_regwrite(sc, MPI3_SYSIF_SENSE_BUF_FREE_HOST_INDEX_OFFSET,
2921 		sc->sense_buf_q_host_index);
2922 
2923 	if (init_type == MPI3MR_INIT_TYPE_INIT) {
2924 		retval = mpi3mr_alloc_interrupts(sc, 0);
2925 		if (retval) {
2926 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to allocate interrupts, retval: 0x%x\n",
2927 			    retval);
2928 			goto out_failed;
2929 		}
2930 
2931 		retval = mpi3mr_setup_irqs(sc);
2932 		if (retval) {
2933 			printf(IOCNAME "Failed to setup ISR, error: 0x%x\n",
2934 			    sc->name, retval);
2935 			goto out_failed;
2936 		}
2937 
2938 		mpi3mr_enable_interrupts(sc);
2939 
2940 	} else
2941 		mpi3mr_enable_interrupts(sc);
2942 
2943 	retval = mpi3mr_create_op_queues(sc);
2944 
2945 	if (retval) {
2946 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to create operational queues, error: %d\n",
2947 		    retval);
2948 		goto out_failed;
2949 	}
2950 
2951 	if (!sc->throttle_groups && sc->num_io_throttle_group) {
2952 		mpi3mr_dprint(sc, MPI3MR_ERROR, "allocating memory for throttle groups\n");
2953 		size = sizeof(struct mpi3mr_throttle_group_info);
2954 		sc->throttle_groups = (struct mpi3mr_throttle_group_info *)
2955 					  malloc(sc->num_io_throttle_group *
2956 					      size, M_MPI3MR, M_NOWAIT | M_ZERO);
2957 		if (!sc->throttle_groups)
2958 			goto out_failed;
2959 	}
2960 
2961 	if (init_type == MPI3MR_INIT_TYPE_RESET) {
2962 		mpi3mr_dprint(sc, MPI3MR_INFO, "Re-register events\n");
2963 		retval = mpi3mr_register_events(sc);
2964 		if (retval) {
2965 			mpi3mr_dprint(sc, MPI3MR_INFO, "Failed to re-register events, retval: 0x%x\n",
2966 			    retval);
2967 			goto out_failed;
2968 		}
2969 
2970 		mpi3mr_dprint(sc, MPI3MR_INFO, "Issuing Port Enable\n");
2971 		retval = mpi3mr_issue_port_enable(sc, 0);
2972 		if (retval) {
2973 			mpi3mr_dprint(sc, MPI3MR_INFO, "Failed to issue port enable, retval: 0x%x\n",
2974 			    retval);
2975 			goto out_failed;
2976 		}
2977 	}
2978 	retval = mpi3mr_pel_alloc(sc);
2979 	if (retval) {
2980 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to allocate memory for PEL, retval: 0x%x\n",
2981 		    retval);
2982 		goto out_failed;
2983 	}
2984 
2985 	return retval;
2986 
2987 out_failed:
2988 	retval = -1;
2989 	return retval;
2990 }
2991 
2992 static void mpi3mr_port_enable_complete(struct mpi3mr_softc *sc,
2993     struct mpi3mr_drvr_cmd *drvrcmd)
2994 {
2995 	drvrcmd->state = MPI3MR_CMD_NOTUSED;
2996 	drvrcmd->callback = NULL;
2997 	printf(IOCNAME "Completing Port Enable Request\n", sc->name);
2998 	sc->mpi3mr_flags |= MPI3MR_FLAGS_PORT_ENABLE_DONE;
2999 	mpi3mr_startup_decrement(sc->cam_sc);
3000 }
3001 
3002 int mpi3mr_issue_port_enable(struct mpi3mr_softc *sc, U8 async)
3003 {
3004 	Mpi3PortEnableRequest_t pe_req;
3005 	int retval = 0;
3006 
3007 	memset(&pe_req, 0, sizeof(pe_req));
3008 	mtx_lock(&sc->init_cmds.completion.lock);
3009 	if (sc->init_cmds.state & MPI3MR_CMD_PENDING) {
3010 		retval = -1;
3011 		printf(IOCNAME "Issue PortEnable: Init command is in use\n", sc->name);
3012 		mtx_unlock(&sc->init_cmds.completion.lock);
3013 		goto out;
3014 	}
3015 
3016 	sc->init_cmds.state = MPI3MR_CMD_PENDING;
3017 
3018 	if (async) {
3019 		sc->init_cmds.is_waiting = 0;
3020 		sc->init_cmds.callback = mpi3mr_port_enable_complete;
3021 	} else {
3022 		sc->init_cmds.is_waiting = 1;
3023 		sc->init_cmds.callback = NULL;
3024 		init_completion(&sc->init_cmds.completion);
3025 	}
3026 	pe_req.HostTag = MPI3MR_HOSTTAG_INITCMDS;
3027 	pe_req.Function = MPI3_FUNCTION_PORT_ENABLE;
3028 
3029 	printf(IOCNAME "Sending Port Enable Request\n", sc->name);
3030 	retval = mpi3mr_submit_admin_cmd(sc, &pe_req, sizeof(pe_req));
3031 	if (retval) {
3032 		printf(IOCNAME "Issue PortEnable: Admin Post failed\n",
3033 		    sc->name);
3034 		goto out_unlock;
3035 	}
3036 
3037 	if (!async) {
3038 		wait_for_completion_timeout(&sc->init_cmds.completion,
3039 		    MPI3MR_PORTENABLE_TIMEOUT);
3040 		if (!(sc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3041 			printf(IOCNAME "Issue PortEnable: command timed out\n",
3042 			    sc->name);
3043 			retval = -1;
3044 			mpi3mr_check_rh_fault_ioc(sc, MPI3MR_RESET_FROM_PE_TIMEOUT);
3045 			goto out_unlock;
3046 		}
3047 		mpi3mr_port_enable_complete(sc, &sc->init_cmds);
3048 	}
3049 out_unlock:
3050 	mtx_unlock(&sc->init_cmds.completion.lock);
3051 
3052 out:
3053 	return retval;
3054 }
3055 
3056 void
3057 mpi3mr_watchdog_thread(void *arg)
3058 {
3059 	struct mpi3mr_softc *sc;
3060 	enum mpi3mr_iocstate ioc_state;
3061 	U32 fault, host_diagnostic, ioc_status;
3062 
3063 	sc = (struct mpi3mr_softc *)arg;
3064 
3065 	mpi3mr_dprint(sc, MPI3MR_XINFO, "%s\n", __func__);
3066 
3067 	sc->watchdog_thread_active = 1;
3068 	mtx_lock(&sc->reset_mutex);
3069 	for (;;) {
3070 		if (sc->mpi3mr_flags & MPI3MR_FLAGS_SHUTDOWN ||
3071 		    (sc->unrecoverable == 1)) {
3072 			mpi3mr_dprint(sc, MPI3MR_INFO,
3073 			    "Exit due to %s from %s\n",
3074 			   sc->mpi3mr_flags & MPI3MR_FLAGS_SHUTDOWN ? "Shutdown" :
3075 			    "Hardware critical error", __func__);
3076 			break;
3077 		}
3078 		mtx_unlock(&sc->reset_mutex);
3079 
3080 		if ((sc->prepare_for_reset) &&
3081 		    ((sc->prepare_for_reset_timeout_counter++) >=
3082 		     MPI3MR_PREPARE_FOR_RESET_TIMEOUT)) {
3083 			mpi3mr_soft_reset_handler(sc,
3084 			    MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1);
3085 			goto sleep;
3086 		}
3087 
3088 		ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
3089 
3090 		if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
3091 			mpi3mr_soft_reset_handler(sc, MPI3MR_RESET_FROM_FIRMWARE, 0);
3092 			goto sleep;
3093 		}
3094 
3095 		ioc_state = mpi3mr_get_iocstate(sc);
3096 		if (ioc_state == MRIOC_STATE_FAULT) {
3097 			fault = mpi3mr_regread(sc, MPI3_SYSIF_FAULT_OFFSET) &
3098 			    MPI3_SYSIF_FAULT_CODE_MASK;
3099 
3100 			host_diagnostic = mpi3mr_regread(sc, MPI3_SYSIF_HOST_DIAG_OFFSET);
3101 			if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
3102 				if (!sc->diagsave_timeout) {
3103 					mpi3mr_print_fault_info(sc);
3104 					mpi3mr_dprint(sc, MPI3MR_INFO,
3105 						"diag save in progress\n");
3106 				}
3107 				if ((sc->diagsave_timeout++) <= MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
3108 					goto sleep;
3109 			}
3110 			mpi3mr_print_fault_info(sc);
3111 			sc->diagsave_timeout = 0;
3112 
3113 			if ((fault == MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED) ||
3114 			    (fault == MPI3_SYSIF_FAULT_CODE_COMPLETE_RESET_NEEDED)) {
3115 				mpi3mr_dprint(sc, MPI3MR_INFO,
3116 				    "Controller requires system power cycle or complete reset is needed,"
3117 				    "fault code: 0x%x. marking controller as unrecoverable\n", fault);
3118 				sc->unrecoverable = 1;
3119 				break;
3120 			}
3121 			if ((fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET)
3122 			    || (fault == MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS)
3123 			    || (sc->reset_in_progress))
3124 				break;
3125 			if (fault == MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET)
3126 				mpi3mr_soft_reset_handler(sc,
3127 				    MPI3MR_RESET_FROM_CIACTIV_FAULT, 0);
3128 			else
3129 				mpi3mr_soft_reset_handler(sc,
3130 				    MPI3MR_RESET_FROM_FAULT_WATCH, 0);
3131 
3132 		}
3133 
3134 		if (sc->reset.type == MPI3MR_TRIGGER_SOFT_RESET) {
3135 			mpi3mr_print_fault_info(sc);
3136 			mpi3mr_soft_reset_handler(sc, sc->reset.reason, 1);
3137 		}
3138 sleep:
3139 		mtx_lock(&sc->reset_mutex);
3140 		/*
3141 		 * Sleep for 1 second if we're not exiting, then loop to top
3142 		 * to poll exit status and hardware health.
3143 		 */
3144 		if ((sc->mpi3mr_flags & MPI3MR_FLAGS_SHUTDOWN) == 0 &&
3145 		    !sc->unrecoverable) {
3146 			msleep(&sc->watchdog_chan, &sc->reset_mutex, PRIBIO,
3147 			    "mpi3mr_watchdog", 1 * hz);
3148 		}
3149 	}
3150 	mtx_unlock(&sc->reset_mutex);
3151 	sc->watchdog_thread_active = 0;
3152 	mpi3mr_kproc_exit(0);
3153 }
3154 
3155 static void mpi3mr_display_event_data(struct mpi3mr_softc *sc,
3156 	Mpi3EventNotificationReply_t *event_rep)
3157 {
3158 	char *desc = NULL;
3159 	U16 event;
3160 
3161 	event = event_rep->Event;
3162 
3163 	switch (event) {
3164 	case MPI3_EVENT_LOG_DATA:
3165 		desc = "Log Data";
3166 		break;
3167 	case MPI3_EVENT_CHANGE:
3168 		desc = "Event Change";
3169 		break;
3170 	case MPI3_EVENT_GPIO_INTERRUPT:
3171 		desc = "GPIO Interrupt";
3172 		break;
3173 	case MPI3_EVENT_CABLE_MGMT:
3174 		desc = "Cable Management";
3175 		break;
3176 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
3177 		desc = "Energy Pack Change";
3178 		break;
3179 	case MPI3_EVENT_DEVICE_ADDED:
3180 	{
3181 		Mpi3DevicePage0_t *event_data =
3182 		    (Mpi3DevicePage0_t *)event_rep->EventData;
3183 		mpi3mr_dprint(sc, MPI3MR_EVENT, "Device Added: Dev=0x%04x Form=0x%x Perst id: 0x%x\n",
3184 			event_data->DevHandle, event_data->DeviceForm, event_data->PersistentID);
3185 		return;
3186 	}
3187 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
3188 	{
3189 		Mpi3DevicePage0_t *event_data =
3190 		    (Mpi3DevicePage0_t *)event_rep->EventData;
3191 		mpi3mr_dprint(sc, MPI3MR_EVENT, "Device Info Changed: Dev=0x%04x Form=0x%x\n",
3192 			event_data->DevHandle, event_data->DeviceForm);
3193 		return;
3194 	}
3195 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
3196 	{
3197 		Mpi3EventDataDeviceStatusChange_t *event_data =
3198 		    (Mpi3EventDataDeviceStatusChange_t *)event_rep->EventData;
3199 		mpi3mr_dprint(sc, MPI3MR_EVENT, "Device Status Change: Dev=0x%04x RC=0x%x\n",
3200 			event_data->DevHandle, event_data->ReasonCode);
3201 		return;
3202 	}
3203 	case MPI3_EVENT_SAS_DISCOVERY:
3204 	{
3205 		Mpi3EventDataSasDiscovery_t *event_data =
3206 		    (Mpi3EventDataSasDiscovery_t *)event_rep->EventData;
3207 		mpi3mr_dprint(sc, MPI3MR_EVENT, "SAS Discovery: (%s)",
3208 			(event_data->ReasonCode == MPI3_EVENT_SAS_DISC_RC_STARTED) ?
3209 		    "start" : "stop");
3210 		if (event_data->DiscoveryStatus &&
3211 		    (sc->mpi3mr_debug & MPI3MR_EVENT)) {
3212 			printf("discovery_status(0x%08x)",
3213 			    event_data->DiscoveryStatus);
3214 
3215 		}
3216 
3217 		if (sc->mpi3mr_debug & MPI3MR_EVENT)
3218 			printf("\n");
3219 		return;
3220 	}
3221 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
3222 		desc = "SAS Broadcast Primitive";
3223 		break;
3224 	case MPI3_EVENT_SAS_NOTIFY_PRIMITIVE:
3225 		desc = "SAS Notify Primitive";
3226 		break;
3227 	case MPI3_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
3228 		desc = "SAS Init Device Status Change";
3229 		break;
3230 	case MPI3_EVENT_SAS_INIT_TABLE_OVERFLOW:
3231 		desc = "SAS Init Table Overflow";
3232 		break;
3233 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
3234 		desc = "SAS Topology Change List";
3235 		break;
3236 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
3237 		desc = "Enclosure Device Status Change";
3238 		break;
3239 	case MPI3_EVENT_HARD_RESET_RECEIVED:
3240 		desc = "Hard Reset Received";
3241 		break;
3242 	case MPI3_EVENT_SAS_PHY_COUNTER:
3243 		desc = "SAS PHY Counter";
3244 		break;
3245 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
3246 		desc = "SAS Device Discovery Error";
3247 		break;
3248 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
3249 		desc = "PCIE Topology Change List";
3250 		break;
3251 	case MPI3_EVENT_PCIE_ENUMERATION:
3252 	{
3253 		Mpi3EventDataPcieEnumeration_t *event_data =
3254 			(Mpi3EventDataPcieEnumeration_t *)event_rep->EventData;
3255 		mpi3mr_dprint(sc, MPI3MR_EVENT, "PCIE Enumeration: (%s)",
3256 			(event_data->ReasonCode ==
3257 			    MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" :
3258 			    "stop");
3259 		if (event_data->EnumerationStatus)
3260 			mpi3mr_dprint(sc, MPI3MR_EVENT, "enumeration_status(0x%08x)",
3261 			   event_data->EnumerationStatus);
3262 		if (sc->mpi3mr_debug & MPI3MR_EVENT)
3263 			printf("\n");
3264 		return;
3265 	}
3266 	case MPI3_EVENT_PREPARE_FOR_RESET:
3267 		desc = "Prepare For Reset";
3268 		break;
3269 	}
3270 
3271 	if (!desc)
3272 		return;
3273 
3274 	mpi3mr_dprint(sc, MPI3MR_EVENT, "%s\n", desc);
3275 }
3276 
3277 struct mpi3mr_target *
3278 mpi3mr_find_target_by_per_id(struct mpi3mr_cam_softc *cam_sc,
3279     uint16_t per_id)
3280 {
3281 	struct mpi3mr_target *target = NULL;
3282 
3283 	mtx_lock_spin(&cam_sc->sc->target_lock);
3284 	TAILQ_FOREACH(target, &cam_sc->tgt_list, tgt_next) {
3285 		if (target->per_id == per_id)
3286 			break;
3287 	}
3288 
3289 	mtx_unlock_spin(&cam_sc->sc->target_lock);
3290 	return target;
3291 }
3292 
3293 struct mpi3mr_target *
3294 mpi3mr_find_target_by_dev_handle(struct mpi3mr_cam_softc *cam_sc,
3295     uint16_t handle)
3296 {
3297 	struct mpi3mr_target *target = NULL;
3298 
3299 	mtx_lock_spin(&cam_sc->sc->target_lock);
3300 	TAILQ_FOREACH(target, &cam_sc->tgt_list, tgt_next) {
3301 		if (target->dev_handle == handle)
3302 			break;
3303 
3304 	}
3305 	mtx_unlock_spin(&cam_sc->sc->target_lock);
3306 	return target;
3307 }
3308 
3309 void mpi3mr_update_device(struct mpi3mr_softc *sc,
3310     struct mpi3mr_target *tgtdev, Mpi3DevicePage0_t *dev_pg0,
3311     bool is_added)
3312 {
3313 	U16 flags = 0;
3314 
3315 	tgtdev->per_id = (dev_pg0->PersistentID);
3316 	tgtdev->dev_handle = (dev_pg0->DevHandle);
3317 	tgtdev->dev_type = dev_pg0->DeviceForm;
3318 	tgtdev->encl_handle = (dev_pg0->EnclosureHandle);
3319 	tgtdev->parent_handle = (dev_pg0->ParentDevHandle);
3320 	tgtdev->slot = (dev_pg0->Slot);
3321 	tgtdev->qdepth = (dev_pg0->QueueDepth);
3322 	tgtdev->wwid = (dev_pg0->WWID);
3323 
3324 	flags = (dev_pg0->Flags);
3325 	tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
3326 	if (is_added == true)
3327 		tgtdev->io_throttle_enabled =
3328 		    (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
3329 
3330 	switch (dev_pg0->AccessStatus) {
3331 	case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
3332 	case MPI3_DEVICE0_ASTATUS_PREPARE:
3333 	case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
3334 	case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
3335 		break;
3336 	default:
3337 		tgtdev->is_hidden = 1;
3338 		break;
3339 	}
3340 
3341 	switch (tgtdev->dev_type) {
3342 	case MPI3_DEVICE_DEVFORM_SAS_SATA:
3343 	{
3344 		Mpi3Device0SasSataFormat_t *sasinf =
3345 		    &dev_pg0->DeviceSpecific.SasSataFormat;
3346 		U16 dev_info = (sasinf->DeviceInfo);
3347 		tgtdev->dev_spec.sassata_inf.dev_info = dev_info;
3348 		tgtdev->dev_spec.sassata_inf.sas_address =
3349 		    (sasinf->SASAddress);
3350 		if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
3351 		    MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
3352 			tgtdev->is_hidden = 1;
3353 		else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
3354 			    MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
3355 			tgtdev->is_hidden = 1;
3356 		break;
3357 	}
3358 	case MPI3_DEVICE_DEVFORM_PCIE:
3359 	{
3360 		Mpi3Device0PcieFormat_t *pcieinf =
3361 		    &dev_pg0->DeviceSpecific.PcieFormat;
3362 		U16 dev_info = (pcieinf->DeviceInfo);
3363 
3364 		tgtdev->q_depth = dev_pg0->QueueDepth;
3365 		tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
3366 		tgtdev->dev_spec.pcie_inf.capb =
3367 		    (pcieinf->Capabilities);
3368 		tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
3369 		if (dev_pg0->AccessStatus == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
3370 			tgtdev->dev_spec.pcie_inf.mdts =
3371 			    (pcieinf->MaximumDataTransferSize);
3372 			tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->PageSize;
3373 			tgtdev->dev_spec.pcie_inf.reset_to =
3374 				pcieinf->ControllerResetTO;
3375 			tgtdev->dev_spec.pcie_inf.abort_to =
3376 				pcieinf->NVMeAbortTO;
3377 		}
3378 		if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
3379 			tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
3380 
3381 		if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
3382 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
3383 		    ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
3384 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
3385 			tgtdev->is_hidden = 1;
3386 
3387 		break;
3388 	}
3389 	case MPI3_DEVICE_DEVFORM_VD:
3390 	{
3391 		Mpi3Device0VdFormat_t *vdinf =
3392 		    &dev_pg0->DeviceSpecific.VdFormat;
3393 		struct mpi3mr_throttle_group_info *tg = NULL;
3394 
3395 		tgtdev->dev_spec.vol_inf.state = vdinf->VdState;
3396 		if (vdinf->VdState == MPI3_DEVICE0_VD_STATE_OFFLINE)
3397 			tgtdev->is_hidden = 1;
3398 		tgtdev->dev_spec.vol_inf.tg_id = vdinf->IOThrottleGroup;
3399 		tgtdev->dev_spec.vol_inf.tg_high =
3400 			vdinf->IOThrottleGroupHigh * 2048;
3401 		tgtdev->dev_spec.vol_inf.tg_low =
3402 			vdinf->IOThrottleGroupLow * 2048;
3403 		if (vdinf->IOThrottleGroup < sc->num_io_throttle_group) {
3404 			tg = sc->throttle_groups + vdinf->IOThrottleGroup;
3405 			tg->id = vdinf->IOThrottleGroup;
3406 			tg->high = tgtdev->dev_spec.vol_inf.tg_high;
3407 			tg->low = tgtdev->dev_spec.vol_inf.tg_low;
3408 			if (is_added == true)
3409 				tg->fw_qd = tgtdev->q_depth;
3410 			tg->modified_qd = tgtdev->q_depth;
3411 		}
3412 		tgtdev->dev_spec.vol_inf.tg = tg;
3413 		tgtdev->throttle_group = tg;
3414 		break;
3415 	}
3416 	default:
3417 		goto out;
3418 	}
3419 
3420 out:
3421 	return;
3422 }
3423 
3424 int mpi3mr_create_device(struct mpi3mr_softc *sc,
3425     Mpi3DevicePage0_t *dev_pg0)
3426 {
3427 	int retval = 0;
3428 	struct mpi3mr_target *target = NULL;
3429 	U16 per_id = 0;
3430 
3431 	per_id = dev_pg0->PersistentID;
3432 
3433 	mtx_lock_spin(&sc->target_lock);
3434 	TAILQ_FOREACH(target, &sc->cam_sc->tgt_list, tgt_next) {
3435 		if (target->per_id == per_id) {
3436 			target->state = MPI3MR_DEV_CREATED;
3437 			break;
3438 		}
3439 	}
3440 	mtx_unlock_spin(&sc->target_lock);
3441 
3442 	if (target) {
3443 			mpi3mr_update_device(sc, target, dev_pg0, true);
3444 	} else {
3445 			target = malloc(sizeof(*target), M_MPI3MR,
3446 				 M_NOWAIT | M_ZERO);
3447 
3448 			if (target == NULL) {
3449 				retval = -1;
3450 				goto out;
3451 			}
3452 
3453 			target->exposed_to_os = 0;
3454 			mpi3mr_update_device(sc, target, dev_pg0, true);
3455 			mtx_lock_spin(&sc->target_lock);
3456 			TAILQ_INSERT_TAIL(&sc->cam_sc->tgt_list, target, tgt_next);
3457 			target->state = MPI3MR_DEV_CREATED;
3458 			mtx_unlock_spin(&sc->target_lock);
3459 	}
3460 out:
3461 	return retval;
3462 }
3463 
3464 /**
3465  * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
3466  * @sc: Adapter instance reference
3467  * @drv_cmd: Internal command tracker
3468  *
3469  * Issues a target reset TM to the firmware from the device
3470  * removal TM pend list or retry the removal handshake sequence
3471  * based on the IOU control request IOC status.
3472  *
3473  * Return: Nothing
3474  */
3475 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_softc *sc,
3476 	struct mpi3mr_drvr_cmd *drv_cmd)
3477 {
3478 	U16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
3479 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
3480 
3481 	mpi3mr_dprint(sc, MPI3MR_EVENT,
3482 	    "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
3483 	    __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
3484 	    drv_cmd->ioc_loginfo);
3485 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
3486 		if (drv_cmd->retry_count < MPI3MR_DEVRMHS_RETRYCOUNT) {
3487 			drv_cmd->retry_count++;
3488 			mpi3mr_dprint(sc, MPI3MR_EVENT,
3489 			    "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
3490 			    __func__, drv_cmd->dev_handle,
3491 			    drv_cmd->retry_count);
3492 			mpi3mr_dev_rmhs_send_tm(sc, drv_cmd->dev_handle,
3493 			    drv_cmd, drv_cmd->iou_rc);
3494 			return;
3495 		}
3496 		mpi3mr_dprint(sc, MPI3MR_ERROR,
3497 		    "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
3498 		    __func__, drv_cmd->dev_handle);
3499 	} else {
3500 		mpi3mr_dprint(sc, MPI3MR_INFO,
3501 		    "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
3502 		    __func__, drv_cmd->dev_handle);
3503 		mpi3mr_clear_bit(drv_cmd->dev_handle, sc->removepend_bitmap);
3504 	}
3505 
3506 	if (!TAILQ_EMPTY(&sc->delayed_rmhs_list)) {
3507 		delayed_dev_rmhs = TAILQ_FIRST(&sc->delayed_rmhs_list);
3508 		drv_cmd->dev_handle = delayed_dev_rmhs->handle;
3509 		drv_cmd->retry_count = 0;
3510 		drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
3511 		mpi3mr_dprint(sc, MPI3MR_EVENT,
3512 		    "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
3513 		    __func__, drv_cmd->dev_handle);
3514 		mpi3mr_dev_rmhs_send_tm(sc, drv_cmd->dev_handle, drv_cmd,
3515 		    drv_cmd->iou_rc);
3516 		TAILQ_REMOVE(&sc->delayed_rmhs_list, delayed_dev_rmhs, list);
3517 		free(delayed_dev_rmhs, M_MPI3MR);
3518 		return;
3519 	}
3520 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3521 	drv_cmd->callback = NULL;
3522 	drv_cmd->retry_count = 0;
3523 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
3524 	mpi3mr_clear_bit(cmd_idx, sc->devrem_bitmap);
3525 }
3526 
3527 /**
3528  * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
3529  * @sc: Adapter instance reference
3530  * @drv_cmd: Internal command tracker
3531  *
3532  * Issues a target reset TM to the firmware from the device
3533  * removal TM pend list or issue IO Unit control request as
3534  * part of device removal or hidden acknowledgment handshake.
3535  *
3536  * Return: Nothing
3537  */
3538 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_softc *sc,
3539 	struct mpi3mr_drvr_cmd *drv_cmd)
3540 {
3541 	Mpi3IoUnitControlRequest_t iou_ctrl;
3542 	U16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
3543 	Mpi3SCSITaskMgmtReply_t *tm_reply = NULL;
3544 	int retval;
3545 
3546 	if (drv_cmd->state & MPI3MR_CMD_REPLYVALID)
3547 		tm_reply = (Mpi3SCSITaskMgmtReply_t *)drv_cmd->reply;
3548 
3549 	if (tm_reply)
3550 		printf(IOCNAME
3551 		    "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
3552 		    sc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
3553 		    drv_cmd->ioc_loginfo,
3554 		    le32toh(tm_reply->TerminationCount));
3555 
3556 	printf(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
3557 	    sc->name, drv_cmd->dev_handle, cmd_idx);
3558 
3559 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
3560 
3561 	drv_cmd->state = MPI3MR_CMD_PENDING;
3562 	drv_cmd->is_waiting = 0;
3563 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
3564 	iou_ctrl.Operation = drv_cmd->iou_rc;
3565 	iou_ctrl.Param16[0] = htole16(drv_cmd->dev_handle);
3566 	iou_ctrl.HostTag = htole16(drv_cmd->host_tag);
3567 	iou_ctrl.Function = MPI3_FUNCTION_IO_UNIT_CONTROL;
3568 
3569 	retval = mpi3mr_submit_admin_cmd(sc, &iou_ctrl, sizeof(iou_ctrl));
3570 	if (retval) {
3571 		printf(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
3572 		    sc->name);
3573 		goto out_failed;
3574 	}
3575 
3576 	return;
3577 out_failed:
3578 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3579 	drv_cmd->callback = NULL;
3580 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
3581 	drv_cmd->retry_count = 0;
3582 	mpi3mr_clear_bit(cmd_idx, sc->devrem_bitmap);
3583 }
3584 
3585 /**
3586  * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
3587  * @sc: Adapter instance reference
3588  * @handle: Device handle
3589  * @cmdparam: Internal command tracker
3590  * @iou_rc: IO Unit reason code
3591  *
3592  * Issues a target reset TM to the firmware or add it to a pend
3593  * list as part of device removal or hidden acknowledgment
3594  * handshake.
3595  *
3596  * Return: Nothing
3597  */
3598 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_softc *sc, U16 handle,
3599 	struct mpi3mr_drvr_cmd *cmdparam, U8 iou_rc)
3600 {
3601 	Mpi3SCSITaskMgmtRequest_t tm_req;
3602 	int retval = 0;
3603 	U16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
3604 	U8 retrycount = 5;
3605 	struct mpi3mr_drvr_cmd *drv_cmd = cmdparam;
3606 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
3607 	struct mpi3mr_target *tgtdev = NULL;
3608 
3609 	mtx_lock_spin(&sc->target_lock);
3610 	TAILQ_FOREACH(tgtdev, &sc->cam_sc->tgt_list, tgt_next) {
3611 		if ((tgtdev->dev_handle == handle) &&
3612 		    (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE)) {
3613 			tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
3614 			break;
3615 		}
3616 	}
3617 	mtx_unlock_spin(&sc->target_lock);
3618 
3619 	if (drv_cmd)
3620 		goto issue_cmd;
3621 	do {
3622 		cmd_idx = mpi3mr_find_first_zero_bit(sc->devrem_bitmap,
3623 		    MPI3MR_NUM_DEVRMCMD);
3624 		if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
3625 			if (!mpi3mr_test_and_set_bit(cmd_idx, sc->devrem_bitmap))
3626 				break;
3627 			cmd_idx = MPI3MR_NUM_DEVRMCMD;
3628 		}
3629 	} while (retrycount--);
3630 
3631 	if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
3632 		delayed_dev_rmhs = malloc(sizeof(*delayed_dev_rmhs),M_MPI3MR,
3633 		     M_ZERO|M_NOWAIT);
3634 
3635 		if (!delayed_dev_rmhs)
3636 			return;
3637 		delayed_dev_rmhs->handle = handle;
3638 		delayed_dev_rmhs->iou_rc = iou_rc;
3639 		TAILQ_INSERT_TAIL(&(sc->delayed_rmhs_list), delayed_dev_rmhs, list);
3640 		mpi3mr_dprint(sc, MPI3MR_EVENT, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
3641 		    __func__, handle);
3642 
3643 
3644 		return;
3645 	}
3646 	drv_cmd = &sc->dev_rmhs_cmds[cmd_idx];
3647 
3648 issue_cmd:
3649 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
3650 	mpi3mr_dprint(sc, MPI3MR_EVENT,
3651 	    "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
3652 	    __func__, handle, cmd_idx);
3653 
3654 	memset(&tm_req, 0, sizeof(tm_req));
3655 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3656 		mpi3mr_dprint(sc, MPI3MR_EVENT, "%s :Issue TM: Command is in use\n", __func__);
3657 		goto out;
3658 	}
3659 	drv_cmd->state = MPI3MR_CMD_PENDING;
3660 	drv_cmd->is_waiting = 0;
3661 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
3662 	drv_cmd->dev_handle = handle;
3663 	drv_cmd->iou_rc = iou_rc;
3664 	tm_req.DevHandle = htole16(handle);
3665 	tm_req.TaskType = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3666 	tm_req.HostTag = htole16(drv_cmd->host_tag);
3667 	tm_req.TaskHostTag = htole16(MPI3MR_HOSTTAG_INVALID);
3668 	tm_req.Function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3669 
3670 	mpi3mr_set_bit(handle, sc->removepend_bitmap);
3671 	retval = mpi3mr_submit_admin_cmd(sc, &tm_req, sizeof(tm_req));
3672 	if (retval) {
3673 		mpi3mr_dprint(sc, MPI3MR_ERROR, "%s :Issue DevRmHsTM: Admin Post failed\n",
3674 		    __func__);
3675 		goto out_failed;
3676 	}
3677 out:
3678 	return;
3679 out_failed:
3680 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3681 	drv_cmd->callback = NULL;
3682 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
3683 	drv_cmd->retry_count = 0;
3684 	mpi3mr_clear_bit(cmd_idx, sc->devrem_bitmap);
3685 }
3686 
3687 /**
3688  * mpi3mr_complete_evt_ack - Event ack request completion
3689  * @sc: Adapter instance reference
3690  * @drv_cmd: Internal command tracker
3691  *
3692  * This is the completion handler for non blocking event
3693  * acknowledgment sent to the firmware and this will issue any
3694  * pending event acknowledgment request.
3695  *
3696  * Return: Nothing
3697  */
3698 static void mpi3mr_complete_evt_ack(struct mpi3mr_softc *sc,
3699 	struct mpi3mr_drvr_cmd *drv_cmd)
3700 {
3701 	U16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
3702 	struct delayed_evtack_node *delayed_evtack = NULL;
3703 
3704 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
3705 		mpi3mr_dprint(sc, MPI3MR_EVENT,
3706 		    "%s: Failed IOCStatus(0x%04x) Loginfo(0x%08x)\n", __func__,
3707 		    (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
3708 		    drv_cmd->ioc_loginfo);
3709 	}
3710 
3711 	if (!TAILQ_EMPTY(&sc->delayed_evtack_cmds_list)) {
3712 		delayed_evtack = TAILQ_FIRST(&sc->delayed_evtack_cmds_list);
3713 		mpi3mr_dprint(sc, MPI3MR_EVENT,
3714 		    "%s: processing delayed event ack for event %d\n",
3715 		    __func__, delayed_evtack->event);
3716 		mpi3mr_send_evt_ack(sc, delayed_evtack->event, drv_cmd,
3717 		    delayed_evtack->event_ctx);
3718 		TAILQ_REMOVE(&sc->delayed_evtack_cmds_list, delayed_evtack, list);
3719 		free(delayed_evtack, M_MPI3MR);
3720 		return;
3721 	}
3722 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3723 	drv_cmd->callback = NULL;
3724 	mpi3mr_clear_bit(cmd_idx, sc->evtack_cmds_bitmap);
3725 }
3726 
3727 /**
3728  * mpi3mr_send_evt_ack - Issue event acknwoledgment request
3729  * @sc: Adapter instance reference
3730  * @event: MPI3 event id
3731  * @cmdparam: Internal command tracker
3732  * @event_ctx: Event context
3733  *
3734  * Issues event acknowledgment request to the firmware if there
3735  * is a free command to send the event ack else it to a pend
3736  * list so that it will be processed on a completion of a prior
3737  * event acknowledgment .
3738  *
3739  * Return: Nothing
3740  */
3741 static void mpi3mr_send_evt_ack(struct mpi3mr_softc *sc, U8 event,
3742 	struct mpi3mr_drvr_cmd *cmdparam, U32 event_ctx)
3743 {
3744 	Mpi3EventAckRequest_t evtack_req;
3745 	int retval = 0;
3746 	U8 retrycount = 5;
3747 	U16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
3748 	struct mpi3mr_drvr_cmd *drv_cmd = cmdparam;
3749 	struct delayed_evtack_node *delayed_evtack = NULL;
3750 
3751 	if (drv_cmd)
3752 		goto issue_cmd;
3753 	do {
3754 		cmd_idx = mpi3mr_find_first_zero_bit(sc->evtack_cmds_bitmap,
3755 		    MPI3MR_NUM_EVTACKCMD);
3756 		if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
3757 			if (!mpi3mr_test_and_set_bit(cmd_idx,
3758 			    sc->evtack_cmds_bitmap))
3759 				break;
3760 			cmd_idx = MPI3MR_NUM_EVTACKCMD;
3761 		}
3762 	} while (retrycount--);
3763 
3764 	if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
3765 		delayed_evtack = malloc(sizeof(*delayed_evtack),M_MPI3MR,
3766 		     M_ZERO | M_NOWAIT);
3767 		if (!delayed_evtack)
3768 			return;
3769 		delayed_evtack->event = event;
3770 		delayed_evtack->event_ctx = event_ctx;
3771 		TAILQ_INSERT_TAIL(&(sc->delayed_evtack_cmds_list), delayed_evtack, list);
3772 		mpi3mr_dprint(sc, MPI3MR_EVENT, "%s : Event ack for event:%d is postponed\n",
3773 		    __func__, event);
3774 		return;
3775 	}
3776 	drv_cmd = &sc->evtack_cmds[cmd_idx];
3777 
3778 issue_cmd:
3779 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
3780 
3781 	memset(&evtack_req, 0, sizeof(evtack_req));
3782 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3783 		mpi3mr_dprint(sc, MPI3MR_EVENT, "%s: Command is in use\n", __func__);
3784 		goto out;
3785 	}
3786 	drv_cmd->state = MPI3MR_CMD_PENDING;
3787 	drv_cmd->is_waiting = 0;
3788 	drv_cmd->callback = mpi3mr_complete_evt_ack;
3789 	evtack_req.HostTag = htole16(drv_cmd->host_tag);
3790 	evtack_req.Function = MPI3_FUNCTION_EVENT_ACK;
3791 	evtack_req.Event = event;
3792 	evtack_req.EventContext = htole32(event_ctx);
3793 	retval = mpi3mr_submit_admin_cmd(sc, &evtack_req,
3794 	    sizeof(evtack_req));
3795 
3796 	if (retval) {
3797 		mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Admin Post failed\n", __func__);
3798 		goto out_failed;
3799 	}
3800 out:
3801 	return;
3802 out_failed:
3803 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3804 	drv_cmd->callback = NULL;
3805 	mpi3mr_clear_bit(cmd_idx, sc->evtack_cmds_bitmap);
3806 }
3807 
3808 /*
3809  * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
3810  * @sc: Adapter instance reference
3811  * @event_reply: Event data
3812  *
3813  * Checks for the reason code and based on that either block I/O
3814  * to device, or unblock I/O to the device, or start the device
3815  * removal handshake with reason as remove with the firmware for
3816  * PCIe devices.
3817  *
3818  * Return: Nothing
3819  */
3820 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_softc *sc,
3821 	Mpi3EventNotificationReply_t *event_reply)
3822 {
3823 	Mpi3EventDataPcieTopologyChangeList_t *topo_evt =
3824 	    (Mpi3EventDataPcieTopologyChangeList_t *) event_reply->EventData;
3825 	int i;
3826 	U16 handle;
3827 	U8 reason_code;
3828 	struct mpi3mr_target *tgtdev = NULL;
3829 
3830 	for (i = 0; i < topo_evt->NumEntries; i++) {
3831 		handle = le16toh(topo_evt->PortEntry[i].AttachedDevHandle);
3832 		if (!handle)
3833 			continue;
3834 		reason_code = topo_evt->PortEntry[i].PortStatus;
3835 		tgtdev = mpi3mr_find_target_by_dev_handle(sc->cam_sc, handle);
3836 		switch (reason_code) {
3837 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
3838 			if (tgtdev) {
3839 				tgtdev->dev_removed = 1;
3840 				tgtdev->dev_removedelay = 0;
3841 				mpi3mr_atomic_set(&tgtdev->block_io, 0);
3842 			}
3843 			mpi3mr_dev_rmhs_send_tm(sc, handle, NULL,
3844 			    MPI3_CTRL_OP_REMOVE_DEVICE);
3845 			break;
3846 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
3847 			if (tgtdev) {
3848 				tgtdev->dev_removedelay = 1;
3849 				mpi3mr_atomic_inc(&tgtdev->block_io);
3850 			}
3851 			break;
3852 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
3853 			if (tgtdev &&
3854 			    tgtdev->dev_removedelay) {
3855 				tgtdev->dev_removedelay = 0;
3856 				if (mpi3mr_atomic_read(&tgtdev->block_io) > 0)
3857 					mpi3mr_atomic_dec(&tgtdev->block_io);
3858 			}
3859 			break;
3860 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
3861 		default:
3862 			break;
3863 		}
3864 	}
3865 }
3866 
3867 /**
3868  * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
3869  * @sc: Adapter instance reference
3870  * @event_reply: Event data
3871  *
3872  * Checks for the reason code and based on that either block I/O
3873  * to device, or unblock I/O to the device, or start the device
3874  * removal handshake with reason as remove with the firmware for
3875  * SAS/SATA devices.
3876  *
3877  * Return: Nothing
3878  */
3879 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_softc *sc,
3880 	Mpi3EventNotificationReply_t *event_reply)
3881 {
3882 	Mpi3EventDataSasTopologyChangeList_t *topo_evt =
3883 	    (Mpi3EventDataSasTopologyChangeList_t *)event_reply->EventData;
3884 	int i;
3885 	U16 handle;
3886 	U8 reason_code;
3887 	struct mpi3mr_target *tgtdev = NULL;
3888 
3889 	for (i = 0; i < topo_evt->NumEntries; i++) {
3890 		handle = le16toh(topo_evt->PhyEntry[i].AttachedDevHandle);
3891 		if (!handle)
3892 			continue;
3893 		reason_code = topo_evt->PhyEntry[i].Status &
3894 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
3895 		tgtdev = mpi3mr_find_target_by_dev_handle(sc->cam_sc, handle);
3896 		switch (reason_code) {
3897 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
3898 			if (tgtdev) {
3899 				tgtdev->dev_removed = 1;
3900 				tgtdev->dev_removedelay = 0;
3901 				mpi3mr_atomic_set(&tgtdev->block_io, 0);
3902 			}
3903 			mpi3mr_dev_rmhs_send_tm(sc, handle, NULL,
3904 			    MPI3_CTRL_OP_REMOVE_DEVICE);
3905 			break;
3906 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
3907 			if (tgtdev) {
3908 				tgtdev->dev_removedelay = 1;
3909 				mpi3mr_atomic_inc(&tgtdev->block_io);
3910 			}
3911 			break;
3912 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
3913 			if (tgtdev &&
3914 			    tgtdev->dev_removedelay) {
3915 				tgtdev->dev_removedelay = 0;
3916 				if (mpi3mr_atomic_read(&tgtdev->block_io) > 0)
3917 					mpi3mr_atomic_dec(&tgtdev->block_io);
3918 			}
3919 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
3920 		default:
3921 			break;
3922 		}
3923 	}
3924 
3925 }
3926 /**
3927  * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
3928  * @sc: Adapter instance reference
3929  * @event_reply: Event data
3930  *
3931  * Checks for the reason code and based on that either block I/O
3932  * to device, or unblock I/O to the device, or start the device
3933  * removal handshake with reason as remove/hide acknowledgment
3934  * with the firmware.
3935  *
3936  * Return: Nothing
3937  */
3938 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_softc *sc,
3939 	Mpi3EventNotificationReply_t *event_reply)
3940 {
3941 	U16 dev_handle = 0;
3942 	U8 ublock = 0, block = 0, hide = 0, uhide = 0, delete = 0, remove = 0;
3943 	struct mpi3mr_target *tgtdev = NULL;
3944 	Mpi3EventDataDeviceStatusChange_t *evtdata =
3945 	    (Mpi3EventDataDeviceStatusChange_t *) event_reply->EventData;
3946 
3947 	dev_handle = le16toh(evtdata->DevHandle);
3948 
3949 	switch (evtdata->ReasonCode) {
3950 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
3951 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
3952 		block = 1;
3953 		break;
3954 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
3955 		delete = 1;
3956 		hide = 1;
3957 		break;
3958 	case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
3959 		uhide = 1;
3960 		break;
3961 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
3962 		delete = 1;
3963 		remove = 1;
3964 		break;
3965 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
3966 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
3967 		ublock = 1;
3968 		break;
3969 	default:
3970 		break;
3971 	}
3972 
3973 	tgtdev = mpi3mr_find_target_by_dev_handle(sc->cam_sc, dev_handle);
3974 
3975 	if (!tgtdev) {
3976 		mpi3mr_dprint(sc, MPI3MR_ERROR, "%s :target with dev_handle:0x%x not found\n",
3977 		    __func__, dev_handle);
3978 		return;
3979 	}
3980 
3981 	if (block)
3982 		mpi3mr_atomic_inc(&tgtdev->block_io);
3983 
3984 	if (hide)
3985 		tgtdev->is_hidden = hide;
3986 
3987 	if (uhide) {
3988 		tgtdev->is_hidden = 0;
3989 		tgtdev->dev_removed = 0;
3990 	}
3991 
3992 	if (delete)
3993 		tgtdev->dev_removed = 1;
3994 
3995 	if (ublock) {
3996 		if (mpi3mr_atomic_read(&tgtdev->block_io) > 0)
3997 			mpi3mr_atomic_dec(&tgtdev->block_io);
3998 	}
3999 
4000 	if (remove) {
4001 		mpi3mr_dev_rmhs_send_tm(sc, dev_handle, NULL,
4002 					MPI3_CTRL_OP_REMOVE_DEVICE);
4003 	}
4004 	if (hide)
4005 		mpi3mr_dev_rmhs_send_tm(sc, dev_handle, NULL,
4006 					MPI3_CTRL_OP_HIDDEN_ACK);
4007 }
4008 
4009 /**
4010  * mpi3mr_preparereset_evt_th - Prepareforreset evt tophalf
4011  * @sc: Adapter instance reference
4012  * @event_reply: Event data
4013  *
4014  * Blocks and unblocks host level I/O based on the reason code
4015  *
4016  * Return: Nothing
4017  */
4018 static void mpi3mr_preparereset_evt_th(struct mpi3mr_softc *sc,
4019 	Mpi3EventNotificationReply_t *event_reply)
4020 {
4021 	Mpi3EventDataPrepareForReset_t *evtdata =
4022 	    (Mpi3EventDataPrepareForReset_t *)event_reply->EventData;
4023 
4024 	if (evtdata->ReasonCode == MPI3_EVENT_PREPARE_RESET_RC_START) {
4025 		mpi3mr_dprint(sc, MPI3MR_EVENT, "%s :Recieved PrepForReset Event with RC=START\n",
4026 		    __func__);
4027 		if (sc->prepare_for_reset)
4028 			return;
4029 		sc->prepare_for_reset = 1;
4030 		sc->prepare_for_reset_timeout_counter = 0;
4031 	} else if (evtdata->ReasonCode == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
4032 		mpi3mr_dprint(sc, MPI3MR_EVENT, "%s :Recieved PrepForReset Event with RC=ABORT\n",
4033 		    __func__);
4034 		sc->prepare_for_reset = 0;
4035 		sc->prepare_for_reset_timeout_counter = 0;
4036 	}
4037 	if ((event_reply->MsgFlags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
4038 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
4039 		mpi3mr_send_evt_ack(sc, event_reply->Event, NULL,
4040 		    le32toh(event_reply->EventContext));
4041 }
4042 
4043 /**
4044  * mpi3mr_energypackchg_evt_th - Energypackchange evt tophalf
4045  * @sc: Adapter instance reference
4046  * @event_reply: Event data
4047  *
4048  * Identifies the new shutdown timeout value and update.
4049  *
4050  * Return: Nothing
4051  */
4052 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_softc *sc,
4053 	Mpi3EventNotificationReply_t *event_reply)
4054 {
4055 	Mpi3EventDataEnergyPackChange_t *evtdata =
4056 	    (Mpi3EventDataEnergyPackChange_t *)event_reply->EventData;
4057 	U16 shutdown_timeout = le16toh(evtdata->ShutdownTimeout);
4058 
4059 	if (shutdown_timeout <= 0) {
4060 		mpi3mr_dprint(sc, MPI3MR_ERROR,
4061 		    "%s :Invalid Shutdown Timeout received = %d\n",
4062 		    __func__, shutdown_timeout);
4063 		return;
4064 	}
4065 
4066 	mpi3mr_dprint(sc, MPI3MR_EVENT,
4067 	    "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
4068 	    __func__, sc->facts.shutdown_timeout, shutdown_timeout);
4069 	sc->facts.shutdown_timeout = shutdown_timeout;
4070 }
4071 
4072 /**
4073  * mpi3mr_cablemgmt_evt_th - Cable mgmt evt tophalf
4074  * @sc: Adapter instance reference
4075  * @event_reply: Event data
4076  *
4077  * Displays Cable manegemt event details.
4078  *
4079  * Return: Nothing
4080  */
4081 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_softc *sc,
4082 	Mpi3EventNotificationReply_t *event_reply)
4083 {
4084 	Mpi3EventDataCableManagement_t *evtdata =
4085 	    (Mpi3EventDataCableManagement_t *)event_reply->EventData;
4086 
4087 	switch (evtdata->Status) {
4088 	case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
4089 	{
4090 		mpi3mr_dprint(sc, MPI3MR_INFO, "An active cable with ReceptacleID %d cannot be powered.\n"
4091 		    "Devices connected to this cable are not detected.\n"
4092 		    "This cable requires %d mW of power.\n",
4093 		    evtdata->ReceptacleID,
4094 		    le32toh(evtdata->ActiveCablePowerRequirement));
4095 		break;
4096 	}
4097 	case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
4098 	{
4099 		mpi3mr_dprint(sc, MPI3MR_INFO, "A cable with ReceptacleID %d is not running at optimal speed\n",
4100 		    evtdata->ReceptacleID);
4101 		break;
4102 	}
4103 	default:
4104 		break;
4105 	}
4106 }
4107 
4108 /**
4109  * mpi3mr_process_events - Event's toph-half handler
4110  * @sc: Adapter instance reference
4111  * @event_reply: Event data
4112  *
4113  * Top half of event processing.
4114  *
4115  * Return: Nothing
4116  */
4117 static void mpi3mr_process_events(struct mpi3mr_softc *sc,
4118     uintptr_t data, Mpi3EventNotificationReply_t *event_reply)
4119 {
4120 	U16 evt_type;
4121 	bool ack_req = 0, process_evt_bh = 0;
4122 	struct mpi3mr_fw_event_work *fw_event;
4123 	U16 sz;
4124 
4125 	if (sc->mpi3mr_flags & MPI3MR_FLAGS_SHUTDOWN)
4126 		goto out;
4127 
4128 	if ((event_reply->MsgFlags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
4129 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
4130 		ack_req = 1;
4131 
4132 	evt_type = event_reply->Event;
4133 
4134 	switch (evt_type) {
4135 	case MPI3_EVENT_DEVICE_ADDED:
4136 	{
4137 		Mpi3DevicePage0_t *dev_pg0 =
4138 			(Mpi3DevicePage0_t *) event_reply->EventData;
4139 		if (mpi3mr_create_device(sc, dev_pg0))
4140 			mpi3mr_dprint(sc, MPI3MR_ERROR,
4141 			"%s :Failed to add device in the device add event\n",
4142 			__func__);
4143 		else
4144 			process_evt_bh = 1;
4145 		break;
4146 	}
4147 
4148 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
4149 	{
4150 		process_evt_bh = 1;
4151 		mpi3mr_devstatuschg_evt_th(sc, event_reply);
4152 		break;
4153 	}
4154 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
4155 	{
4156 		process_evt_bh = 1;
4157 		mpi3mr_sastopochg_evt_th(sc, event_reply);
4158 		break;
4159 	}
4160 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
4161 	{
4162 		process_evt_bh = 1;
4163 		mpi3mr_pcietopochg_evt_th(sc, event_reply);
4164 		break;
4165 	}
4166 	case MPI3_EVENT_PREPARE_FOR_RESET:
4167 	{
4168 		mpi3mr_preparereset_evt_th(sc, event_reply);
4169 		ack_req = 0;
4170 		break;
4171 	}
4172 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
4173 	case MPI3_EVENT_LOG_DATA:
4174 	{
4175 		process_evt_bh = 1;
4176 		break;
4177 	}
4178 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
4179 	{
4180 		mpi3mr_energypackchg_evt_th(sc, event_reply);
4181 		break;
4182 	}
4183 	case MPI3_EVENT_CABLE_MGMT:
4184 	{
4185 		mpi3mr_cablemgmt_evt_th(sc, event_reply);
4186 		break;
4187 	}
4188 
4189 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
4190 	case MPI3_EVENT_SAS_DISCOVERY:
4191 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
4192 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
4193 	case MPI3_EVENT_PCIE_ENUMERATION:
4194 		break;
4195 	default:
4196 		mpi3mr_dprint(sc, MPI3MR_INFO, "%s :Event 0x%02x is not handled by driver\n",
4197 		    __func__, evt_type);
4198 		break;
4199 	}
4200 
4201 	if (process_evt_bh || ack_req) {
4202 		fw_event = malloc(sizeof(struct mpi3mr_fw_event_work), M_MPI3MR,
4203 		     M_ZERO|M_NOWAIT);
4204 
4205 		if (!fw_event) {
4206 			printf("%s: allocate failed for fw_event\n", __func__);
4207 			return;
4208 		}
4209 
4210 		sz = le16toh(event_reply->EventDataLength) * 4;
4211 		fw_event->event_data = malloc(sz, M_MPI3MR, M_ZERO|M_NOWAIT);
4212 
4213 		if (!fw_event->event_data) {
4214 			printf("%s: allocate failed for event_data\n", __func__);
4215 			free(fw_event, M_MPI3MR);
4216 			return;
4217 		}
4218 
4219 		bcopy(event_reply->EventData, fw_event->event_data, sz);
4220 		fw_event->event = event_reply->Event;
4221 		if ((event_reply->Event == MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
4222 		    event_reply->Event == MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
4223 		    event_reply->Event == MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE ) &&
4224 		    sc->track_mapping_events)
4225 			sc->pending_map_events++;
4226 
4227 		/*
4228 		 * Events should be processed after Port enable is completed.
4229 		 */
4230 		if ((event_reply->Event == MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
4231 		    event_reply->Event == MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ) &&
4232 		    !(sc->mpi3mr_flags & MPI3MR_FLAGS_PORT_ENABLE_DONE))
4233 			mpi3mr_startup_increment(sc->cam_sc);
4234 
4235 		fw_event->send_ack = ack_req;
4236 		fw_event->event_context = le32toh(event_reply->EventContext);
4237 		fw_event->event_data_size = sz;
4238 		fw_event->process_event = process_evt_bh;
4239 
4240 		mtx_lock(&sc->fwevt_lock);
4241 		TAILQ_INSERT_TAIL(&sc->cam_sc->ev_queue, fw_event, ev_link);
4242 		taskqueue_enqueue(sc->cam_sc->ev_tq, &sc->cam_sc->ev_task);
4243 		mtx_unlock(&sc->fwevt_lock);
4244 
4245 	}
4246 out:
4247 	return;
4248 }
4249 
4250 static void mpi3mr_handle_events(struct mpi3mr_softc *sc, uintptr_t data,
4251     Mpi3DefaultReply_t *def_reply)
4252 {
4253 	Mpi3EventNotificationReply_t *event_reply =
4254 		(Mpi3EventNotificationReply_t *)def_reply;
4255 
4256 	sc->change_count = event_reply->IOCChangeCount;
4257 	mpi3mr_display_event_data(sc, event_reply);
4258 
4259 	mpi3mr_process_events(sc, data, event_reply);
4260 }
4261 
4262 static void mpi3mr_process_admin_reply_desc(struct mpi3mr_softc *sc,
4263     Mpi3DefaultReplyDescriptor_t *reply_desc, U64 *reply_dma)
4264 {
4265 	U16 reply_desc_type, host_tag = 0, idx;
4266 	U16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
4267 	U32 ioc_loginfo = 0;
4268 	Mpi3StatusReplyDescriptor_t *status_desc;
4269 	Mpi3AddressReplyDescriptor_t *addr_desc;
4270 	Mpi3SuccessReplyDescriptor_t *success_desc;
4271 	Mpi3DefaultReply_t *def_reply = NULL;
4272 	struct mpi3mr_drvr_cmd *cmdptr = NULL;
4273 	Mpi3SCSIIOReply_t *scsi_reply;
4274 	U8 *sense_buf = NULL;
4275 
4276 	*reply_dma = 0;
4277 	reply_desc_type = reply_desc->ReplyFlags &
4278 			    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
4279 	switch (reply_desc_type) {
4280 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
4281 		status_desc = (Mpi3StatusReplyDescriptor_t *)reply_desc;
4282 		host_tag = status_desc->HostTag;
4283 		ioc_status = status_desc->IOCStatus;
4284 		if (ioc_status &
4285 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
4286 			ioc_loginfo = status_desc->IOCLogInfo;
4287 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
4288 		break;
4289 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
4290 		addr_desc = (Mpi3AddressReplyDescriptor_t *)reply_desc;
4291 		*reply_dma = addr_desc->ReplyFrameAddress;
4292 		def_reply = mpi3mr_get_reply_virt_addr(sc, *reply_dma);
4293 		if (def_reply == NULL)
4294 			goto out;
4295 		host_tag = def_reply->HostTag;
4296 		ioc_status = def_reply->IOCStatus;
4297 		if (ioc_status &
4298 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
4299 			ioc_loginfo = def_reply->IOCLogInfo;
4300 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
4301 		if (def_reply->Function == MPI3_FUNCTION_SCSI_IO) {
4302 			scsi_reply = (Mpi3SCSIIOReply_t *)def_reply;
4303 			sense_buf = mpi3mr_get_sensebuf_virt_addr(sc,
4304 			    scsi_reply->SenseDataBufferAddress);
4305 		}
4306 		break;
4307 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
4308 		success_desc = (Mpi3SuccessReplyDescriptor_t *)reply_desc;
4309 		host_tag = success_desc->HostTag;
4310 		break;
4311 	default:
4312 		break;
4313 	}
4314 	switch (host_tag) {
4315 	case MPI3MR_HOSTTAG_INITCMDS:
4316 		cmdptr = &sc->init_cmds;
4317 		break;
4318 	case MPI3MR_HOSTTAG_IOCTLCMDS:
4319 		cmdptr = &sc->ioctl_cmds;
4320 		break;
4321 	case MPI3MR_HOSTTAG_TMS:
4322 		cmdptr = &sc->host_tm_cmds;
4323 		wakeup((void *)&sc->tm_chan);
4324 		break;
4325 	case MPI3MR_HOSTTAG_PELABORT:
4326 		cmdptr = &sc->pel_abort_cmd;
4327 		break;
4328 	case MPI3MR_HOSTTAG_PELWAIT:
4329 		cmdptr = &sc->pel_cmds;
4330 		break;
4331 	case MPI3MR_HOSTTAG_INVALID:
4332 		if (def_reply && def_reply->Function ==
4333 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
4334 			mpi3mr_handle_events(sc, *reply_dma ,def_reply);
4335 	default:
4336 		break;
4337 	}
4338 
4339 	if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
4340 	    host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX ) {
4341 		idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
4342 		cmdptr = &sc->dev_rmhs_cmds[idx];
4343 	}
4344 
4345 	if (host_tag >= MPI3MR_HOSTTAG_EVTACKCMD_MIN &&
4346 	    host_tag <= MPI3MR_HOSTTAG_EVTACKCMD_MAX) {
4347 		idx = host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
4348 		cmdptr = &sc->evtack_cmds[idx];
4349 	}
4350 
4351 	if (cmdptr) {
4352 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
4353 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
4354 			cmdptr->ioc_loginfo = ioc_loginfo;
4355 			cmdptr->ioc_status = ioc_status;
4356 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
4357 			if (def_reply) {
4358 				cmdptr->state |= MPI3MR_CMD_REPLYVALID;
4359 				memcpy((U8 *)cmdptr->reply, (U8 *)def_reply,
4360 				    sc->reply_sz);
4361 			}
4362 			if (sense_buf && cmdptr->sensebuf) {
4363 				cmdptr->is_senseprst = 1;
4364 				memcpy(cmdptr->sensebuf, sense_buf,
4365 				    MPI3MR_SENSEBUF_SZ);
4366 			}
4367 			if (cmdptr->is_waiting) {
4368 				complete(&cmdptr->completion);
4369 				cmdptr->is_waiting = 0;
4370 			} else if (cmdptr->callback)
4371 				cmdptr->callback(sc, cmdptr);
4372 		}
4373 	}
4374 out:
4375 	if (sense_buf != NULL)
4376 		mpi3mr_repost_sense_buf(sc,
4377 		    scsi_reply->SenseDataBufferAddress);
4378 	return;
4379 }
4380 
4381 /*
4382  * mpi3mr_complete_admin_cmd:	ISR routine for admin commands
4383  * @sc:				Adapter's soft instance
4384  *
4385  * This function processes admin command completions.
4386  */
4387 static int mpi3mr_complete_admin_cmd(struct mpi3mr_softc *sc)
4388 {
4389 	U32 exp_phase = sc->admin_reply_ephase;
4390 	U32 adm_reply_ci = sc->admin_reply_ci;
4391 	U32 num_adm_reply = 0;
4392 	U64 reply_dma = 0;
4393 	Mpi3DefaultReplyDescriptor_t *reply_desc;
4394 
4395 	mtx_lock_spin(&sc->admin_reply_lock);
4396 	if (sc->admin_in_use == false) {
4397 		sc->admin_in_use = true;
4398 		mtx_unlock_spin(&sc->admin_reply_lock);
4399 	} else {
4400 		mtx_unlock_spin(&sc->admin_reply_lock);
4401 		return 0;
4402 	}
4403 
4404 	reply_desc = (Mpi3DefaultReplyDescriptor_t *)sc->admin_reply +
4405 		adm_reply_ci;
4406 
4407 	if ((reply_desc->ReplyFlags &
4408 	     MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
4409 		mtx_lock_spin(&sc->admin_reply_lock);
4410 		sc->admin_in_use = false;
4411 		mtx_unlock_spin(&sc->admin_reply_lock);
4412 		return 0;
4413 	}
4414 
4415 	do {
4416 		sc->admin_req_ci = reply_desc->RequestQueueCI;
4417 		mpi3mr_process_admin_reply_desc(sc, reply_desc, &reply_dma);
4418 		if (reply_dma)
4419 			mpi3mr_repost_reply_buf(sc, reply_dma);
4420 		num_adm_reply++;
4421 		if (++adm_reply_ci == sc->num_admin_replies) {
4422 			adm_reply_ci = 0;
4423 			exp_phase ^= 1;
4424 		}
4425 		reply_desc =
4426 			(Mpi3DefaultReplyDescriptor_t *)sc->admin_reply +
4427 			    adm_reply_ci;
4428 		if ((reply_desc->ReplyFlags &
4429 		     MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
4430 			break;
4431 	} while (1);
4432 
4433 	mpi3mr_regwrite(sc, MPI3_SYSIF_ADMIN_REPLY_Q_CI_OFFSET, adm_reply_ci);
4434 	sc->admin_reply_ci = adm_reply_ci;
4435 	sc->admin_reply_ephase = exp_phase;
4436 	mtx_lock_spin(&sc->admin_reply_lock);
4437 	sc->admin_in_use = false;
4438 	mtx_unlock_spin(&sc->admin_reply_lock);
4439 	return num_adm_reply;
4440 }
4441 
4442 static void
4443 mpi3mr_cmd_done(struct mpi3mr_softc *sc, struct mpi3mr_cmd *cmd)
4444 {
4445 	mpi3mr_unmap_request(sc, cmd);
4446 
4447 	mtx_lock(&sc->mpi3mr_mtx);
4448 	if (cmd->callout_owner) {
4449 		callout_stop(&cmd->callout);
4450 		cmd->callout_owner = false;
4451 	}
4452 
4453 	if (sc->unrecoverable)
4454 		mpi3mr_set_ccbstatus(cmd->ccb, CAM_DEV_NOT_THERE);
4455 
4456 	xpt_done(cmd->ccb);
4457 	cmd->ccb = NULL;
4458 	mtx_unlock(&sc->mpi3mr_mtx);
4459 	mpi3mr_release_command(cmd);
4460 }
4461 
4462 void mpi3mr_process_op_reply_desc(struct mpi3mr_softc *sc,
4463     Mpi3DefaultReplyDescriptor_t *reply_desc, U64 *reply_dma)
4464 {
4465 	U16 reply_desc_type, host_tag = 0;
4466 	U16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
4467 	U32 ioc_loginfo = 0;
4468 	Mpi3StatusReplyDescriptor_t *status_desc = NULL;
4469 	Mpi3AddressReplyDescriptor_t *addr_desc = NULL;
4470 	Mpi3SuccessReplyDescriptor_t *success_desc = NULL;
4471 	Mpi3SCSIIOReply_t *scsi_reply = NULL;
4472 	U8 *sense_buf = NULL;
4473 	U8 scsi_state = 0, scsi_status = 0, sense_state = 0;
4474 	U32 xfer_count = 0, sense_count =0, resp_data = 0;
4475 	struct mpi3mr_cmd *cm = NULL;
4476 	union ccb *ccb;
4477 	struct ccb_scsiio *csio;
4478 	struct mpi3mr_cam_softc *cam_sc;
4479 	U32 target_id;
4480 	U8 *scsi_cdb;
4481 	struct mpi3mr_target *target = NULL;
4482 	U32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
4483 	struct mpi3mr_throttle_group_info *tg = NULL;
4484 	U8 throttle_enabled_dev = 0;
4485 	static int ratelimit;
4486 
4487 	*reply_dma = 0;
4488 	reply_desc_type = reply_desc->ReplyFlags &
4489 			    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
4490 	switch (reply_desc_type) {
4491 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
4492 		status_desc = (Mpi3StatusReplyDescriptor_t *)reply_desc;
4493 		host_tag = status_desc->HostTag;
4494 		ioc_status = status_desc->IOCStatus;
4495 		if (ioc_status &
4496 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
4497 			ioc_loginfo = status_desc->IOCLogInfo;
4498 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
4499 		break;
4500 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
4501 		addr_desc = (Mpi3AddressReplyDescriptor_t *)reply_desc;
4502 		*reply_dma = addr_desc->ReplyFrameAddress;
4503 		scsi_reply = mpi3mr_get_reply_virt_addr(sc,
4504 		    *reply_dma);
4505 		if (scsi_reply == NULL) {
4506 			mpi3mr_dprint(sc, MPI3MR_ERROR, "scsi_reply is NULL, "
4507 			    "this shouldn't happen, reply_desc: %p\n",
4508 			    reply_desc);
4509 			goto out;
4510 		}
4511 
4512 		host_tag = scsi_reply->HostTag;
4513 		ioc_status = scsi_reply->IOCStatus;
4514 		scsi_status = scsi_reply->SCSIStatus;
4515 		scsi_state = scsi_reply->SCSIState;
4516 		sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
4517 		xfer_count = scsi_reply->TransferCount;
4518 		sense_count = scsi_reply->SenseCount;
4519 		resp_data = scsi_reply->ResponseData;
4520 		sense_buf = mpi3mr_get_sensebuf_virt_addr(sc,
4521 		    scsi_reply->SenseDataBufferAddress);
4522 		if (ioc_status &
4523 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
4524 			ioc_loginfo = scsi_reply->IOCLogInfo;
4525 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
4526 		if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
4527 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Ran out of sense buffers\n");
4528 
4529 		break;
4530 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
4531 		success_desc = (Mpi3SuccessReplyDescriptor_t *)reply_desc;
4532 		host_tag = success_desc->HostTag;
4533 
4534 	default:
4535 		break;
4536 	}
4537 
4538 	cm = sc->cmd_list[host_tag];
4539 
4540 	if (cm->state == MPI3MR_CMD_STATE_FREE)
4541 		goto out;
4542 
4543 	cam_sc = sc->cam_sc;
4544 	ccb = cm->ccb;
4545 	csio = &ccb->csio;
4546 	target_id = csio->ccb_h.target_id;
4547 
4548 	scsi_cdb = scsiio_cdb_ptr(csio);
4549 
4550 	target = mpi3mr_find_target_by_per_id(cam_sc, target_id);
4551 	if (sc->iot_enable) {
4552 		data_len_blks = csio->dxfer_len >> 9;
4553 
4554 		if (target) {
4555 			tg = target->throttle_group;
4556 			throttle_enabled_dev =
4557 				target->io_throttle_enabled;
4558 		}
4559 
4560 		if ((data_len_blks >= sc->io_throttle_data_length) &&
4561 		     throttle_enabled_dev) {
4562 			mpi3mr_atomic_sub(&sc->pend_large_data_sz, data_len_blks);
4563 			ioc_pend_data_len = mpi3mr_atomic_read(
4564 			    &sc->pend_large_data_sz);
4565 			if (tg) {
4566 				mpi3mr_atomic_sub(&tg->pend_large_data_sz,
4567 					data_len_blks);
4568 				tg_pend_data_len = mpi3mr_atomic_read(&tg->pend_large_data_sz);
4569 				if (ratelimit % 1000) {
4570 					mpi3mr_dprint(sc, MPI3MR_IOT,
4571 						"large vd_io completion persist_id(%d), handle(0x%04x), data_len(%d),"
4572 						"ioc_pending(%d), tg_pending(%d), ioc_low(%d), tg_low(%d)\n",
4573 						    target->per_id,
4574 						    target->dev_handle,
4575 						    data_len_blks, ioc_pend_data_len,
4576 						    tg_pend_data_len,
4577 						    sc->io_throttle_low,
4578 						    tg->low);
4579 					ratelimit++;
4580 				}
4581 				if (tg->io_divert  && ((ioc_pend_data_len <=
4582 				    sc->io_throttle_low) &&
4583 				    (tg_pend_data_len <= tg->low))) {
4584 					tg->io_divert = 0;
4585 					mpi3mr_dprint(sc, MPI3MR_IOT,
4586 						"VD: Coming out of divert perst_id(%d) tg_id(%d)\n",
4587 						target->per_id, tg->id);
4588 					mpi3mr_set_io_divert_for_all_vd_in_tg(
4589 					    sc, tg, 0);
4590 				}
4591 			} else {
4592 				if (ratelimit % 1000) {
4593 					mpi3mr_dprint(sc, MPI3MR_IOT,
4594 					    "large pd_io completion persist_id(%d), handle(0x%04x), data_len(%d), ioc_pending(%d), ioc_low(%d)\n",
4595 					    target->per_id,
4596 					    target->dev_handle,
4597 					    data_len_blks, ioc_pend_data_len,
4598 					    sc->io_throttle_low);
4599 					ratelimit++;
4600 				}
4601 
4602 				if (ioc_pend_data_len <= sc->io_throttle_low) {
4603 					target->io_divert = 0;
4604 					mpi3mr_dprint(sc, MPI3MR_IOT,
4605 						"PD: Coming out of divert perst_id(%d)\n",
4606 						target->per_id);
4607 				}
4608 			}
4609 
4610 			} else if (target->io_divert) {
4611 			ioc_pend_data_len = mpi3mr_atomic_read(&sc->pend_large_data_sz);
4612 			if (!tg) {
4613 				if (ratelimit % 1000) {
4614 					mpi3mr_dprint(sc, MPI3MR_IOT,
4615 					    "pd_io completion persist_id(%d), handle(0x%04x), data_len(%d), ioc_pending(%d), ioc_low(%d)\n",
4616 					    target->per_id,
4617 					    target->dev_handle,
4618 					    data_len_blks, ioc_pend_data_len,
4619 					    sc->io_throttle_low);
4620 					ratelimit++;
4621 				}
4622 
4623 				if ( ioc_pend_data_len <= sc->io_throttle_low) {
4624 					mpi3mr_dprint(sc, MPI3MR_IOT,
4625 						"PD: Coming out of divert perst_id(%d)\n",
4626 						target->per_id);
4627 					target->io_divert = 0;
4628 				}
4629 
4630 			} else if (ioc_pend_data_len <= sc->io_throttle_low) {
4631 				tg_pend_data_len = mpi3mr_atomic_read(&tg->pend_large_data_sz);
4632 				if (ratelimit % 1000) {
4633 					mpi3mr_dprint(sc, MPI3MR_IOT,
4634 						"vd_io completion persist_id(%d), handle(0x%04x), data_len(%d),"
4635 						"ioc_pending(%d), tg_pending(%d), ioc_low(%d), tg_low(%d)\n",
4636 						    target->per_id,
4637 						    target->dev_handle,
4638 						    data_len_blks, ioc_pend_data_len,
4639 						    tg_pend_data_len,
4640 						    sc->io_throttle_low,
4641 						    tg->low);
4642 					ratelimit++;
4643 				}
4644 				if (tg->io_divert  && (tg_pend_data_len <= tg->low)) {
4645 					tg->io_divert = 0;
4646 					mpi3mr_dprint(sc, MPI3MR_IOT,
4647 						"VD: Coming out of divert perst_id(%d) tg_id(%d)\n",
4648 						target->per_id, tg->id);
4649 					mpi3mr_set_io_divert_for_all_vd_in_tg(
4650 					    sc, tg, 0);
4651 				}
4652 
4653 			}
4654 		}
4655 	}
4656 
4657 	if (success_desc) {
4658 		mpi3mr_set_ccbstatus(ccb, CAM_REQ_CMP);
4659 		goto out_success;
4660 	}
4661 
4662 	if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN
4663 	    && xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
4664 	    scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
4665 	    scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
4666 		ioc_status = MPI3_IOCSTATUS_SUCCESS;
4667 
4668 	if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count
4669 	    && sense_buf) {
4670 		int sense_len, returned_sense_len;
4671 
4672 		returned_sense_len = min(le32toh(sense_count),
4673 		    sizeof(struct scsi_sense_data));
4674 		if (returned_sense_len < csio->sense_len)
4675 			csio->sense_resid = csio->sense_len -
4676 			    returned_sense_len;
4677 		else
4678 			csio->sense_resid = 0;
4679 
4680 		sense_len = min(returned_sense_len,
4681 		    csio->sense_len - csio->sense_resid);
4682 		bzero(&csio->sense_data, sizeof(csio->sense_data));
4683 		bcopy(sense_buf, &csio->sense_data, sense_len);
4684 		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
4685 	}
4686 
4687 	switch (ioc_status) {
4688 	case MPI3_IOCSTATUS_BUSY:
4689 	case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
4690 		mpi3mr_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
4691 		break;
4692 	case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4693 		/*
4694 		 * If devinfo is 0 this will be a volume.  In that case don't
4695 		 * tell CAM that the volume is not there.  We want volumes to
4696 		 * be enumerated until they are deleted/removed, not just
4697 		 * failed.
4698 		 */
4699 		if (cm->targ->devinfo == 0)
4700 			mpi3mr_set_ccbstatus(ccb, CAM_REQ_CMP);
4701 		else
4702 			mpi3mr_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
4703 		break;
4704 	case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
4705 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
4706 	case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
4707 		mpi3mr_set_ccbstatus(ccb, CAM_SCSI_BUSY);
4708 		mpi3mr_dprint(sc, MPI3MR_TRACE,
4709 		    "func: %s line:%d tgt %u Hosttag %u loginfo %x\n",
4710 		    __func__, __LINE__,
4711 		    target_id, cm->hosttag,
4712 		    le32toh(scsi_reply->IOCLogInfo));
4713 		mpi3mr_dprint(sc, MPI3MR_TRACE,
4714 		    "SCSIStatus %x SCSIState %x xfercount %u\n",
4715 		    scsi_reply->SCSIStatus, scsi_reply->SCSIState,
4716 		    le32toh(xfer_count));
4717 		break;
4718 	case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
4719 		/* resid is ignored for this condition */
4720 		csio->resid = 0;
4721 		mpi3mr_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
4722 		break;
4723 	case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
4724 		csio->resid = cm->length - le32toh(xfer_count);
4725 	case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
4726 	case MPI3_IOCSTATUS_SUCCESS:
4727 		if ((scsi_reply->IOCStatus & MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK) ==
4728 		    MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR)
4729 			mpi3mr_dprint(sc, MPI3MR_XINFO, "func: %s line: %d recovered error\n",  __func__, __LINE__);
4730 
4731 		/* Completion failed at the transport level. */
4732 		if (scsi_reply->SCSIState & (MPI3_SCSI_STATE_NO_SCSI_STATUS |
4733 		    MPI3_SCSI_STATE_TERMINATED)) {
4734 			mpi3mr_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
4735 			break;
4736 		}
4737 
4738 		/* In a modern packetized environment, an autosense failure
4739 		 * implies that there's not much else that can be done to
4740 		 * recover the command.
4741 		 */
4742 		if (scsi_reply->SCSIState & MPI3_SCSI_STATE_SENSE_VALID) {
4743 			mpi3mr_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
4744 			break;
4745 		}
4746 
4747 		/*
4748 		 * Intentionally override the normal SCSI status reporting
4749 		 * for these two cases.  These are likely to happen in a
4750 		 * multi-initiator environment, and we want to make sure that
4751 		 * CAM retries these commands rather than fail them.
4752 		 */
4753 		if ((scsi_reply->SCSIStatus == MPI3_SCSI_STATUS_COMMAND_TERMINATED) ||
4754 		    (scsi_reply->SCSIStatus == MPI3_SCSI_STATUS_TASK_ABORTED)) {
4755 			mpi3mr_set_ccbstatus(ccb, CAM_REQ_ABORTED);
4756 			break;
4757 		}
4758 
4759 		/* Handle normal status and sense */
4760 		csio->scsi_status = scsi_reply->SCSIStatus;
4761 		if (scsi_reply->SCSIStatus == MPI3_SCSI_STATUS_GOOD)
4762 			mpi3mr_set_ccbstatus(ccb, CAM_REQ_CMP);
4763 		else
4764 			mpi3mr_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
4765 
4766 		if (scsi_reply->SCSIState & MPI3_SCSI_STATE_SENSE_VALID) {
4767 			int sense_len, returned_sense_len;
4768 
4769 			returned_sense_len = min(le32toh(scsi_reply->SenseCount),
4770 			    sizeof(struct scsi_sense_data));
4771 			if (returned_sense_len < csio->sense_len)
4772 				csio->sense_resid = csio->sense_len -
4773 				    returned_sense_len;
4774 			else
4775 				csio->sense_resid = 0;
4776 
4777 			sense_len = min(returned_sense_len,
4778 			    csio->sense_len - csio->sense_resid);
4779 			bzero(&csio->sense_data, sizeof(csio->sense_data));
4780 			bcopy(cm->sense, &csio->sense_data, sense_len);
4781 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
4782 		}
4783 
4784 		break;
4785 	case MPI3_IOCSTATUS_INVALID_SGL:
4786 		mpi3mr_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
4787 		break;
4788 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
4789 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
4790 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
4791 	case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4792 	case MPI3_IOCSTATUS_INVALID_FUNCTION:
4793 	case MPI3_IOCSTATUS_INTERNAL_ERROR:
4794 	case MPI3_IOCSTATUS_INVALID_FIELD:
4795 	case MPI3_IOCSTATUS_INVALID_STATE:
4796 	case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
4797 	case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4798 	case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
4799 	case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4800 	default:
4801 		csio->resid = cm->length;
4802 		mpi3mr_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
4803 		break;
4804 	}
4805 
4806 out_success:
4807 	if (mpi3mr_get_ccbstatus(ccb) != CAM_REQ_CMP) {
4808 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
4809 		xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
4810 	}
4811 
4812 	mpi3mr_atomic_dec(&cm->targ->outstanding);
4813 	mpi3mr_cmd_done(sc, cm);
4814 	mpi3mr_dprint(sc, MPI3MR_TRACE, "Completion IO path :"
4815 		" cdb[0]: %x targetid: 0x%x SMID: %x ioc_status: 0x%x ioc_loginfo: 0x%x scsi_status: 0x%x "
4816 		"scsi_state: 0x%x response_data: 0x%x\n", scsi_cdb[0], target_id, host_tag,
4817 		ioc_status, ioc_loginfo, scsi_status, scsi_state, resp_data);
4818 	mpi3mr_atomic_dec(&sc->fw_outstanding);
4819 out:
4820 
4821 	if (sense_buf)
4822 		mpi3mr_repost_sense_buf(sc,
4823 		    scsi_reply->SenseDataBufferAddress);
4824 	return;
4825 }
4826 
4827 /*
4828  * mpi3mr_complete_io_cmd:	ISR routine for IO commands
4829  * @sc:				Adapter's soft instance
4830  * @irq_ctx:			Driver's internal per IRQ structure
4831  *
4832  * This function processes IO command completions.
4833  */
4834 int mpi3mr_complete_io_cmd(struct mpi3mr_softc *sc,
4835     struct mpi3mr_irq_context *irq_ctx)
4836 {
4837 	struct mpi3mr_op_reply_queue *op_reply_q = irq_ctx->op_reply_q;
4838 	U32 exp_phase = op_reply_q->ephase;
4839 	U32 reply_ci = op_reply_q->ci;
4840 	U32 num_op_replies = 0;
4841 	U64 reply_dma = 0;
4842 	Mpi3DefaultReplyDescriptor_t *reply_desc;
4843 	U16 req_qid = 0;
4844 
4845 	mtx_lock_spin(&op_reply_q->q_lock);
4846 	if (op_reply_q->in_use == false) {
4847 		op_reply_q->in_use = true;
4848 		mtx_unlock_spin(&op_reply_q->q_lock);
4849 	} else {
4850 		mtx_unlock_spin(&op_reply_q->q_lock);
4851 		return 0;
4852 	}
4853 
4854 	reply_desc = (Mpi3DefaultReplyDescriptor_t *)op_reply_q->q_base + reply_ci;
4855 	mpi3mr_dprint(sc, MPI3MR_TRACE, "[QID:%d]:reply_desc: (%pa) reply_ci: %x"
4856 		" reply_desc->ReplyFlags: 0x%x\n"
4857 		"reply_q_base_phys: %#016jx reply_q_base: (%pa) exp_phase: %x\n",
4858 		op_reply_q->qid, reply_desc, reply_ci, reply_desc->ReplyFlags, op_reply_q->q_base_phys,
4859 		op_reply_q->q_base, exp_phase);
4860 
4861 	if (((reply_desc->ReplyFlags &
4862 	     MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) || !op_reply_q->qid) {
4863 		mtx_lock_spin(&op_reply_q->q_lock);
4864 		op_reply_q->in_use = false;
4865 		mtx_unlock_spin(&op_reply_q->q_lock);
4866 		return 0;
4867 	}
4868 
4869 	do {
4870 		req_qid = reply_desc->RequestQueueID;
4871 		sc->op_req_q[req_qid - 1].ci =
4872 		    reply_desc->RequestQueueCI;
4873 
4874 		mpi3mr_process_op_reply_desc(sc, reply_desc, &reply_dma);
4875 		mpi3mr_atomic_dec(&op_reply_q->pend_ios);
4876 		if (reply_dma)
4877 			mpi3mr_repost_reply_buf(sc, reply_dma);
4878 		num_op_replies++;
4879 		if (++reply_ci == op_reply_q->num_replies) {
4880 			reply_ci = 0;
4881 			exp_phase ^= 1;
4882 		}
4883 		reply_desc =
4884 		    (Mpi3DefaultReplyDescriptor_t *)op_reply_q->q_base + reply_ci;
4885 		if ((reply_desc->ReplyFlags &
4886 		     MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
4887 			break;
4888 	} while (1);
4889 
4890 
4891 	mpi3mr_regwrite(sc, MPI3_SYSIF_OPER_REPLY_Q_N_CI_OFFSET(op_reply_q->qid), reply_ci);
4892 	op_reply_q->ci = reply_ci;
4893 	op_reply_q->ephase = exp_phase;
4894 	mtx_lock_spin(&op_reply_q->q_lock);
4895 	op_reply_q->in_use = false;
4896 	mtx_unlock_spin(&op_reply_q->q_lock);
4897 	return num_op_replies;
4898 }
4899 
4900 /*
4901  * mpi3mr_isr:			Primary ISR function
4902  * privdata:			Driver's internal per IRQ structure
4903  *
4904  * This is driver's primary ISR function which is being called whenever any admin/IO
4905  * command completion.
4906  */
4907 void mpi3mr_isr(void *privdata)
4908 {
4909 	struct mpi3mr_irq_context *irq_ctx = (struct mpi3mr_irq_context *)privdata;
4910 	struct mpi3mr_softc *sc = irq_ctx->sc;
4911 	U16 msi_idx;
4912 
4913 	if (!irq_ctx)
4914 		return;
4915 
4916 	msi_idx = irq_ctx->msix_index;
4917 
4918 	if (!sc->intr_enabled)
4919 		return;
4920 
4921 	if (!msi_idx)
4922 		mpi3mr_complete_admin_cmd(sc);
4923 
4924 	if (irq_ctx->op_reply_q && irq_ctx->op_reply_q->qid) {
4925 		mpi3mr_complete_io_cmd(sc, irq_ctx);
4926 	}
4927 }
4928 
4929 /*
4930  * mpi3mr_alloc_requests - Allocates host commands
4931  * @sc: Adapter reference
4932  *
4933  * This function allocates controller supported host commands
4934  *
4935  * Return: 0 on success and proper error codes on failure
4936  */
4937 int
4938 mpi3mr_alloc_requests(struct mpi3mr_softc *sc)
4939 {
4940 	struct mpi3mr_cmd *cmd;
4941 	int i, j, nsegs, ret;
4942 
4943 	nsegs = MPI3MR_SG_DEPTH;
4944 	ret = bus_dma_tag_create( sc->mpi3mr_parent_dmat,    /* parent */
4945 				1, 0,			/* algnmnt, boundary */
4946 				sc->dma_loaddr,		/* lowaddr */
4947 				BUS_SPACE_MAXADDR,	/* highaddr */
4948 				NULL, NULL,		/* filter, filterarg */
4949 				BUS_SPACE_MAXSIZE,	/* maxsize */
4950                                 nsegs,			/* nsegments */
4951 				BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
4952                                 BUS_DMA_ALLOCNOW,	/* flags */
4953                                 busdma_lock_mutex,	/* lockfunc */
4954 				&sc->io_lock,	/* lockarg */
4955 				&sc->buffer_dmat);
4956 	if (ret) {
4957 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate buffer DMA tag ret: %d\n", ret);
4958 		return (ENOMEM);
4959         }
4960 
4961 	/*
4962 	 * sc->cmd_list is an array of struct mpi3mr_cmd pointers.
4963 	 * Allocate the dynamic array first and then allocate individual
4964 	 * commands.
4965 	 */
4966 	sc->cmd_list = malloc(sizeof(struct mpi3mr_cmd *) * sc->max_host_ios,
4967 	    M_MPI3MR, M_NOWAIT | M_ZERO);
4968 
4969 	if (!sc->cmd_list) {
4970 		device_printf(sc->mpi3mr_dev, "Cannot alloc memory for mpt_cmd_list.\n");
4971 		return (ENOMEM);
4972 	}
4973 
4974 	for (i = 0; i < sc->max_host_ios; i++) {
4975 		sc->cmd_list[i] = malloc(sizeof(struct mpi3mr_cmd),
4976 		    M_MPI3MR, M_NOWAIT | M_ZERO);
4977 		if (!sc->cmd_list[i]) {
4978 			for (j = 0; j < i; j++)
4979 				free(sc->cmd_list[j], M_MPI3MR);
4980 			free(sc->cmd_list, M_MPI3MR);
4981 			sc->cmd_list = NULL;
4982 			return (ENOMEM);
4983 		}
4984 	}
4985 
4986 	for (i = 1; i < sc->max_host_ios; i++) {
4987 		cmd = sc->cmd_list[i];
4988 		cmd->hosttag = i;
4989 		cmd->sc = sc;
4990 		cmd->state = MPI3MR_CMD_STATE_BUSY;
4991 		callout_init_mtx(&cmd->callout, &sc->mpi3mr_mtx, 0);
4992 		cmd->ccb = NULL;
4993 		TAILQ_INSERT_TAIL(&(sc->cmd_list_head), cmd, next);
4994 		if (bus_dmamap_create(sc->buffer_dmat, 0, &cmd->dmamap))
4995 			return ENOMEM;
4996 	}
4997 	return (0);
4998 }
4999 
5000 /*
5001  * mpi3mr_get_command:		Get a coomand structure from free command pool
5002  * @sc:				Adapter soft instance
5003  * Return:			MPT command reference
5004  *
5005  * This function returns an MPT command to the caller.
5006  */
5007 struct mpi3mr_cmd *
5008 mpi3mr_get_command(struct mpi3mr_softc *sc)
5009 {
5010 	struct mpi3mr_cmd *cmd = NULL;
5011 
5012 	mtx_lock(&sc->cmd_pool_lock);
5013 	if (!TAILQ_EMPTY(&sc->cmd_list_head)) {
5014 		cmd = TAILQ_FIRST(&sc->cmd_list_head);
5015 		TAILQ_REMOVE(&sc->cmd_list_head, cmd, next);
5016 	} else {
5017 		goto out;
5018 	}
5019 
5020 	mpi3mr_dprint(sc, MPI3MR_TRACE, "Get command SMID: 0x%x\n", cmd->hosttag);
5021 
5022 	memset((uint8_t *)&cmd->io_request, 0, MPI3MR_AREQ_FRAME_SZ);
5023 	cmd->data_dir = 0;
5024 	cmd->ccb = NULL;
5025 	cmd->targ = NULL;
5026 	cmd->state = MPI3MR_CMD_STATE_BUSY;
5027 	cmd->data = NULL;
5028 	cmd->length = 0;
5029 out:
5030 	mtx_unlock(&sc->cmd_pool_lock);
5031 	return cmd;
5032 }
5033 
5034 /*
5035  * mpi3mr_release_command:	Return a cmd to free command pool
5036  * input:			Command packet for return to free command pool
5037  *
5038  * This function returns an MPT command to the free command list.
5039  */
5040 void
5041 mpi3mr_release_command(struct mpi3mr_cmd *cmd)
5042 {
5043 	struct mpi3mr_softc *sc = cmd->sc;
5044 
5045 	mtx_lock(&sc->cmd_pool_lock);
5046 	TAILQ_INSERT_HEAD(&(sc->cmd_list_head), cmd, next);
5047 	cmd->state = MPI3MR_CMD_STATE_FREE;
5048 	cmd->req_qidx = 0;
5049 	mpi3mr_dprint(sc, MPI3MR_TRACE, "Release command SMID: 0x%x\n", cmd->hosttag);
5050 	mtx_unlock(&sc->cmd_pool_lock);
5051 
5052 	return;
5053 }
5054 
5055  /**
5056  * mpi3mr_free_ioctl_dma_memory - free memory for ioctl dma
5057  * @sc: Adapter instance reference
5058  *
5059  * Free the DMA memory allocated for IOCTL handling purpose.
5060  *
5061  * Return: None
5062  */
5063 static void mpi3mr_free_ioctl_dma_memory(struct mpi3mr_softc *sc)
5064 {
5065 	U16 i;
5066 	struct dma_memory_desc *mem_desc;
5067 
5068 	for (i=0; i<MPI3MR_NUM_IOCTL_SGE; i++) {
5069 		mem_desc = &sc->ioctl_sge[i];
5070 		if (mem_desc->addr && mem_desc->dma_addr) {
5071 			bus_dmamap_unload(mem_desc->tag, mem_desc->dmamap);
5072 			bus_dmamem_free(mem_desc->tag, mem_desc->addr, mem_desc->dmamap);
5073 			mem_desc->addr = NULL;
5074 			if (mem_desc->tag != NULL)
5075 				bus_dma_tag_destroy(mem_desc->tag);
5076 		}
5077 	}
5078 
5079 	mem_desc = &sc->ioctl_chain_sge;
5080 	if (mem_desc->addr && mem_desc->dma_addr) {
5081 		bus_dmamap_unload(mem_desc->tag, mem_desc->dmamap);
5082 		bus_dmamem_free(mem_desc->tag, mem_desc->addr, mem_desc->dmamap);
5083 		mem_desc->addr = NULL;
5084 		if (mem_desc->tag != NULL)
5085 			bus_dma_tag_destroy(mem_desc->tag);
5086 	}
5087 
5088 	mem_desc = &sc->ioctl_resp_sge;
5089 	if (mem_desc->addr && mem_desc->dma_addr) {
5090 		bus_dmamap_unload(mem_desc->tag, mem_desc->dmamap);
5091 		bus_dmamem_free(mem_desc->tag, mem_desc->addr, mem_desc->dmamap);
5092 		mem_desc->addr = NULL;
5093 		if (mem_desc->tag != NULL)
5094 			bus_dma_tag_destroy(mem_desc->tag);
5095 	}
5096 
5097 	sc->ioctl_sges_allocated = false;
5098 }
5099 
5100 /**
5101  * mpi3mr_alloc_ioctl_dma_memory - Alloc memory for ioctl dma
5102  * @sc: Adapter instance reference
5103  *
5104  * This function allocates dmaable memory required to handle the
5105  * application issued MPI3 IOCTL requests.
5106  *
5107  * Return: None
5108  */
5109 void mpi3mr_alloc_ioctl_dma_memory(struct mpi3mr_softc *sc)
5110 {
5111 	struct dma_memory_desc *mem_desc;
5112 	U16 i;
5113 
5114 	for (i=0; i<MPI3MR_NUM_IOCTL_SGE; i++) {
5115 		mem_desc = &sc->ioctl_sge[i];
5116 		mem_desc->size = MPI3MR_IOCTL_SGE_SIZE;
5117 
5118 		if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
5119 					4, 0,			/* algnmnt, boundary */
5120 					sc->dma_loaddr,		/* lowaddr */
5121 					BUS_SPACE_MAXADDR,	/* highaddr */
5122 					NULL, NULL,		/* filter, filterarg */
5123 					mem_desc->size,		/* maxsize */
5124 					1,			/* nsegments */
5125 					mem_desc->size,		/* maxsegsize */
5126 					0,			/* flags */
5127 					NULL, NULL,		/* lockfunc, lockarg */
5128 					&mem_desc->tag)) {
5129 			mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
5130 			goto out_failed;
5131 		}
5132 
5133 		if (bus_dmamem_alloc(mem_desc->tag, (void **)&mem_desc->addr,
5134 		    BUS_DMA_NOWAIT, &mem_desc->dmamap)) {
5135 			mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Cannot allocate replies memory\n", __func__);
5136 			goto out_failed;
5137 		}
5138 		bzero(mem_desc->addr, mem_desc->size);
5139 		bus_dmamap_load(mem_desc->tag, mem_desc->dmamap, mem_desc->addr, mem_desc->size,
5140 		    mpi3mr_memaddr_cb, &mem_desc->dma_addr, BUS_DMA_NOWAIT);
5141 
5142 		if (!mem_desc->addr)
5143 			goto out_failed;
5144 	}
5145 
5146 	mem_desc = &sc->ioctl_chain_sge;
5147 	mem_desc->size = MPI3MR_4K_PGSZ;
5148 	if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
5149 				4, 0,			/* algnmnt, boundary */
5150 				sc->dma_loaddr,		/* lowaddr */
5151 				BUS_SPACE_MAXADDR,	/* highaddr */
5152 				NULL, NULL,		/* filter, filterarg */
5153 				mem_desc->size,		/* maxsize */
5154 				1,			/* nsegments */
5155 				mem_desc->size,		/* maxsegsize */
5156 				0,			/* flags */
5157 				NULL, NULL,		/* lockfunc, lockarg */
5158 				&mem_desc->tag)) {
5159 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
5160 		goto out_failed;
5161 	}
5162 
5163 	if (bus_dmamem_alloc(mem_desc->tag, (void **)&mem_desc->addr,
5164 	    BUS_DMA_NOWAIT, &mem_desc->dmamap)) {
5165 		mpi3mr_dprint(sc, MPI3MR_ERROR, "%s: Cannot allocate replies memory\n", __func__);
5166 		goto out_failed;
5167 	}
5168 	bzero(mem_desc->addr, mem_desc->size);
5169 	bus_dmamap_load(mem_desc->tag, mem_desc->dmamap, mem_desc->addr, mem_desc->size,
5170 	    mpi3mr_memaddr_cb, &mem_desc->dma_addr, BUS_DMA_NOWAIT);
5171 
5172 	if (!mem_desc->addr)
5173 		goto out_failed;
5174 
5175 	mem_desc = &sc->ioctl_resp_sge;
5176 	mem_desc->size = MPI3MR_4K_PGSZ;
5177 	if (bus_dma_tag_create(sc->mpi3mr_parent_dmat,    /* parent */
5178 				4, 0,			/* algnmnt, boundary */
5179 				sc->dma_loaddr,		/* lowaddr */
5180 				BUS_SPACE_MAXADDR,	/* highaddr */
5181 				NULL, NULL,		/* filter, filterarg */
5182 				mem_desc->size,		/* maxsize */
5183 				1,			/* nsegments */
5184 				mem_desc->size,		/* maxsegsize */
5185 				0,			/* flags */
5186 				NULL, NULL,		/* lockfunc, lockarg */
5187 				&mem_desc->tag)) {
5188 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate request DMA tag\n");
5189 		goto out_failed;
5190 	}
5191 
5192 	if (bus_dmamem_alloc(mem_desc->tag, (void **)&mem_desc->addr,
5193 	    BUS_DMA_NOWAIT, &mem_desc->dmamap)) {
5194 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Cannot allocate replies memory\n");
5195 		goto out_failed;
5196 	}
5197 	bzero(mem_desc->addr, mem_desc->size);
5198 	bus_dmamap_load(mem_desc->tag, mem_desc->dmamap, mem_desc->addr, mem_desc->size,
5199 	    mpi3mr_memaddr_cb, &mem_desc->dma_addr, BUS_DMA_NOWAIT);
5200 
5201 	if (!mem_desc->addr)
5202 		goto out_failed;
5203 
5204 	sc->ioctl_sges_allocated = true;
5205 
5206 	return;
5207 out_failed:
5208 	printf("cannot allocate DMA memory for the mpt commands"
5209 	    "  from the applications, application interface for MPT command is disabled\n");
5210 	mpi3mr_free_ioctl_dma_memory(sc);
5211 }
5212 
5213 void
5214 mpi3mr_destory_mtx(struct mpi3mr_softc *sc)
5215 {
5216 	int i;
5217 	struct mpi3mr_op_req_queue *op_req_q;
5218 	struct mpi3mr_op_reply_queue *op_reply_q;
5219 
5220 	if (sc->admin_reply) {
5221 		if (mtx_initialized(&sc->admin_reply_lock))
5222 			mtx_destroy(&sc->admin_reply_lock);
5223 	}
5224 
5225 	if (sc->op_reply_q) {
5226 		for(i = 0; i < sc->num_queues; i++) {
5227 			op_reply_q = sc->op_reply_q + i;
5228 			if (mtx_initialized(&op_reply_q->q_lock))
5229 				mtx_destroy(&op_reply_q->q_lock);
5230 		}
5231 	}
5232 
5233 	if (sc->op_req_q) {
5234 		for(i = 0; i < sc->num_queues; i++) {
5235 			op_req_q = sc->op_req_q + i;
5236 			if (mtx_initialized(&op_req_q->q_lock))
5237 				mtx_destroy(&op_req_q->q_lock);
5238 		}
5239 	}
5240 
5241 	if (mtx_initialized(&sc->init_cmds.completion.lock))
5242 		mtx_destroy(&sc->init_cmds.completion.lock);
5243 
5244 	if (mtx_initialized(&sc->ioctl_cmds.completion.lock))
5245 		mtx_destroy(&sc->ioctl_cmds.completion.lock);
5246 
5247 	if (mtx_initialized(&sc->host_tm_cmds.completion.lock))
5248 		mtx_destroy(&sc->host_tm_cmds.completion.lock);
5249 
5250 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
5251 		if (mtx_initialized(&sc->dev_rmhs_cmds[i].completion.lock))
5252 			mtx_destroy(&sc->dev_rmhs_cmds[i].completion.lock);
5253 	}
5254 
5255 	if (mtx_initialized(&sc->reset_mutex))
5256 		mtx_destroy(&sc->reset_mutex);
5257 
5258 	if (mtx_initialized(&sc->target_lock))
5259 		mtx_destroy(&sc->target_lock);
5260 
5261 	if (mtx_initialized(&sc->fwevt_lock))
5262 		mtx_destroy(&sc->fwevt_lock);
5263 
5264 	if (mtx_initialized(&sc->cmd_pool_lock))
5265 		mtx_destroy(&sc->cmd_pool_lock);
5266 
5267 	if (mtx_initialized(&sc->reply_free_q_lock))
5268 		mtx_destroy(&sc->reply_free_q_lock);
5269 
5270 	if (mtx_initialized(&sc->sense_buf_q_lock))
5271 		mtx_destroy(&sc->sense_buf_q_lock);
5272 
5273 	if (mtx_initialized(&sc->chain_buf_lock))
5274 		mtx_destroy(&sc->chain_buf_lock);
5275 
5276 	if (mtx_initialized(&sc->admin_req_lock))
5277 		mtx_destroy(&sc->admin_req_lock);
5278 
5279 	if (mtx_initialized(&sc->mpi3mr_mtx))
5280 		mtx_destroy(&sc->mpi3mr_mtx);
5281 }
5282 
5283 /**
5284  * mpi3mr_free_mem - Freeup adapter level data structures
5285  * @sc: Adapter reference
5286  *
5287  * Return: Nothing.
5288  */
5289 void
5290 mpi3mr_free_mem(struct mpi3mr_softc *sc)
5291 {
5292 	int i;
5293 	struct mpi3mr_op_req_queue *op_req_q;
5294 	struct mpi3mr_op_reply_queue *op_reply_q;
5295 	struct mpi3mr_irq_context *irq_ctx;
5296 
5297 	if (sc->cmd_list) {
5298 		for (i = 0; i < sc->max_host_ios; i++) {
5299 			free(sc->cmd_list[i], M_MPI3MR);
5300 		}
5301 		free(sc->cmd_list, M_MPI3MR);
5302 		sc->cmd_list = NULL;
5303 	}
5304 
5305 	if (sc->pel_seq_number && sc->pel_seq_number_dma) {
5306 		bus_dmamap_unload(sc->pel_seq_num_dmatag, sc->pel_seq_num_dmamap);
5307 		bus_dmamem_free(sc->pel_seq_num_dmatag, sc->pel_seq_number, sc->pel_seq_num_dmamap);
5308 		sc->pel_seq_number = NULL;
5309 		if (sc->pel_seq_num_dmatag != NULL)
5310 			bus_dma_tag_destroy(sc->pel_seq_num_dmatag);
5311 	}
5312 
5313 	if (sc->throttle_groups) {
5314 		free(sc->throttle_groups, M_MPI3MR);
5315 		sc->throttle_groups = NULL;
5316 	}
5317 
5318 	/* Free up operational queues*/
5319 	if (sc->op_req_q) {
5320 		for (i = 0; i < sc->num_queues; i++) {
5321 			op_req_q = sc->op_req_q + i;
5322 			if (op_req_q->q_base && op_req_q->q_base_phys) {
5323 				bus_dmamap_unload(op_req_q->q_base_tag, op_req_q->q_base_dmamap);
5324 				bus_dmamem_free(op_req_q->q_base_tag, op_req_q->q_base, op_req_q->q_base_dmamap);
5325 				op_req_q->q_base = NULL;
5326 				if (op_req_q->q_base_tag != NULL)
5327 					bus_dma_tag_destroy(op_req_q->q_base_tag);
5328 			}
5329 		}
5330 		free(sc->op_req_q, M_MPI3MR);
5331 		sc->op_req_q = NULL;
5332 	}
5333 
5334 	if (sc->op_reply_q) {
5335 		for (i = 0; i < sc->num_queues; i++) {
5336 			op_reply_q = sc->op_reply_q + i;
5337 			if (op_reply_q->q_base && op_reply_q->q_base_phys) {
5338 				bus_dmamap_unload(op_reply_q->q_base_tag, op_reply_q->q_base_dmamap);
5339 				bus_dmamem_free(op_reply_q->q_base_tag, op_reply_q->q_base, op_reply_q->q_base_dmamap);
5340 				op_reply_q->q_base = NULL;
5341 				if (op_reply_q->q_base_tag != NULL)
5342 					bus_dma_tag_destroy(op_reply_q->q_base_tag);
5343 			}
5344 		}
5345 		free(sc->op_reply_q, M_MPI3MR);
5346 		sc->op_reply_q = NULL;
5347 	}
5348 
5349 	/* Free up chain buffers*/
5350 	if (sc->chain_sgl_list) {
5351 		for (i = 0; i < sc->chain_buf_count; i++) {
5352 			if (sc->chain_sgl_list[i].buf && sc->chain_sgl_list[i].buf_phys) {
5353 				bus_dmamap_unload(sc->chain_sgl_list_tag, sc->chain_sgl_list[i].buf_dmamap);
5354 				bus_dmamem_free(sc->chain_sgl_list_tag, sc->chain_sgl_list[i].buf,
5355 						sc->chain_sgl_list[i].buf_dmamap);
5356 				sc->chain_sgl_list[i].buf = NULL;
5357 			}
5358 		}
5359 		if (sc->chain_sgl_list_tag != NULL)
5360 			bus_dma_tag_destroy(sc->chain_sgl_list_tag);
5361 		free(sc->chain_sgl_list, M_MPI3MR);
5362 		sc->chain_sgl_list = NULL;
5363 	}
5364 
5365 	if (sc->chain_bitmap) {
5366 		free(sc->chain_bitmap, M_MPI3MR);
5367 		sc->chain_bitmap = NULL;
5368 	}
5369 
5370 	for (i = 0; i < sc->msix_count; i++) {
5371 		irq_ctx = sc->irq_ctx + i;
5372 		if (irq_ctx)
5373 			irq_ctx->op_reply_q = NULL;
5374 	}
5375 
5376 	/* Free reply_buf_tag */
5377 	if (sc->reply_buf && sc->reply_buf_phys) {
5378 		bus_dmamap_unload(sc->reply_buf_tag, sc->reply_buf_dmamap);
5379 		bus_dmamem_free(sc->reply_buf_tag, sc->reply_buf,
5380 				sc->reply_buf_dmamap);
5381 		sc->reply_buf = NULL;
5382 		if (sc->reply_buf_tag != NULL)
5383 			bus_dma_tag_destroy(sc->reply_buf_tag);
5384 	}
5385 
5386 	/* Free reply_free_q_tag */
5387 	if (sc->reply_free_q && sc->reply_free_q_phys) {
5388 		bus_dmamap_unload(sc->reply_free_q_tag, sc->reply_free_q_dmamap);
5389 		bus_dmamem_free(sc->reply_free_q_tag, sc->reply_free_q,
5390 				sc->reply_free_q_dmamap);
5391 		sc->reply_free_q = NULL;
5392 		if (sc->reply_free_q_tag != NULL)
5393 			bus_dma_tag_destroy(sc->reply_free_q_tag);
5394 	}
5395 
5396 	/* Free sense_buf_tag */
5397 	if (sc->sense_buf && sc->sense_buf_phys) {
5398 		bus_dmamap_unload(sc->sense_buf_tag, sc->sense_buf_dmamap);
5399 		bus_dmamem_free(sc->sense_buf_tag, sc->sense_buf,
5400 				sc->sense_buf_dmamap);
5401 		sc->sense_buf = NULL;
5402 		if (sc->sense_buf_tag != NULL)
5403 			bus_dma_tag_destroy(sc->sense_buf_tag);
5404 	}
5405 
5406 	/* Free sense_buf_q_tag */
5407 	if (sc->sense_buf_q && sc->sense_buf_q_phys) {
5408 		bus_dmamap_unload(sc->sense_buf_q_tag, sc->sense_buf_q_dmamap);
5409 		bus_dmamem_free(sc->sense_buf_q_tag, sc->sense_buf_q,
5410 				sc->sense_buf_q_dmamap);
5411 		sc->sense_buf_q = NULL;
5412 		if (sc->sense_buf_q_tag != NULL)
5413 			bus_dma_tag_destroy(sc->sense_buf_q_tag);
5414 	}
5415 
5416 	/* Free up internal(non-IO) commands*/
5417 	if (sc->init_cmds.reply) {
5418 		free(sc->init_cmds.reply, M_MPI3MR);
5419 		sc->init_cmds.reply = NULL;
5420 	}
5421 
5422 	if (sc->ioctl_cmds.reply) {
5423 		free(sc->ioctl_cmds.reply, M_MPI3MR);
5424 		sc->ioctl_cmds.reply = NULL;
5425 	}
5426 
5427 	if (sc->pel_cmds.reply) {
5428 		free(sc->pel_cmds.reply, M_MPI3MR);
5429 		sc->pel_cmds.reply = NULL;
5430 	}
5431 
5432 	if (sc->pel_abort_cmd.reply) {
5433 		free(sc->pel_abort_cmd.reply, M_MPI3MR);
5434 		sc->pel_abort_cmd.reply = NULL;
5435 	}
5436 
5437 	if (sc->host_tm_cmds.reply) {
5438 		free(sc->host_tm_cmds.reply, M_MPI3MR);
5439 		sc->host_tm_cmds.reply = NULL;
5440 	}
5441 
5442 	if (sc->log_data_buffer) {
5443 		free(sc->log_data_buffer, M_MPI3MR);
5444 		sc->log_data_buffer = NULL;
5445 	}
5446 
5447 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
5448 		if (sc->dev_rmhs_cmds[i].reply) {
5449 			free(sc->dev_rmhs_cmds[i].reply, M_MPI3MR);
5450 			sc->dev_rmhs_cmds[i].reply = NULL;
5451 		}
5452 	}
5453 
5454 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
5455 		if (sc->evtack_cmds[i].reply) {
5456 			free(sc->evtack_cmds[i].reply, M_MPI3MR);
5457 			sc->evtack_cmds[i].reply = NULL;
5458 		}
5459 	}
5460 
5461 	if (sc->removepend_bitmap) {
5462 		free(sc->removepend_bitmap, M_MPI3MR);
5463 		sc->removepend_bitmap = NULL;
5464 	}
5465 
5466 	if (sc->devrem_bitmap) {
5467 		free(sc->devrem_bitmap, M_MPI3MR);
5468 		sc->devrem_bitmap = NULL;
5469 	}
5470 
5471 	if (sc->evtack_cmds_bitmap) {
5472 		free(sc->evtack_cmds_bitmap, M_MPI3MR);
5473 		sc->evtack_cmds_bitmap = NULL;
5474 	}
5475 
5476 	/* Free Admin reply*/
5477 	if (sc->admin_reply && sc->admin_reply_phys) {
5478 		bus_dmamap_unload(sc->admin_reply_tag, sc->admin_reply_dmamap);
5479 		bus_dmamem_free(sc->admin_reply_tag, sc->admin_reply,
5480 				sc->admin_reply_dmamap);
5481 		sc->admin_reply = NULL;
5482 		if (sc->admin_reply_tag != NULL)
5483 			bus_dma_tag_destroy(sc->admin_reply_tag);
5484 	}
5485 
5486 	/* Free Admin request*/
5487 	if (sc->admin_req && sc->admin_req_phys) {
5488 		bus_dmamap_unload(sc->admin_req_tag, sc->admin_req_dmamap);
5489 		bus_dmamem_free(sc->admin_req_tag, sc->admin_req,
5490 				sc->admin_req_dmamap);
5491 		sc->admin_req = NULL;
5492 		if (sc->admin_req_tag != NULL)
5493 			bus_dma_tag_destroy(sc->admin_req_tag);
5494 	}
5495 	mpi3mr_free_ioctl_dma_memory(sc);
5496 
5497 }
5498 
5499 /**
5500  * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
5501  * @sc: Adapter instance reference
5502  * @cmdptr: Internal command tracker
5503  *
5504  * Complete an internal driver commands with state indicating it
5505  * is completed due to reset.
5506  *
5507  * Return: Nothing.
5508  */
5509 static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_softc *sc,
5510 	struct mpi3mr_drvr_cmd *cmdptr)
5511 {
5512 	if (cmdptr->state & MPI3MR_CMD_PENDING) {
5513 		cmdptr->state |= MPI3MR_CMD_RESET;
5514 		cmdptr->state &= ~MPI3MR_CMD_PENDING;
5515 		if (cmdptr->is_waiting) {
5516 			complete(&cmdptr->completion);
5517 			cmdptr->is_waiting = 0;
5518 		} else if (cmdptr->callback)
5519 			cmdptr->callback(sc, cmdptr);
5520 	}
5521 }
5522 
5523 /**
5524  * mpi3mr_flush_drv_cmds - Flush internal driver commands
5525  * @sc: Adapter instance reference
5526  *
5527  * Flush all internal driver commands post reset
5528  *
5529  * Return: Nothing.
5530  */
5531 static void mpi3mr_flush_drv_cmds(struct mpi3mr_softc *sc)
5532 {
5533 	int i = 0;
5534 	struct mpi3mr_drvr_cmd *cmdptr;
5535 
5536 	cmdptr = &sc->init_cmds;
5537 	mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5538 
5539 	cmdptr = &sc->ioctl_cmds;
5540 	mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5541 
5542 	cmdptr = &sc->host_tm_cmds;
5543 	mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5544 
5545 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
5546 		cmdptr = &sc->dev_rmhs_cmds[i];
5547 		mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5548 	}
5549 
5550 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
5551 		cmdptr = &sc->evtack_cmds[i];
5552 		mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5553 	}
5554 
5555 	cmdptr = &sc->pel_cmds;
5556 	mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5557 
5558 	cmdptr = &sc->pel_abort_cmd;
5559 	mpi3mr_drv_cmd_comp_reset(sc, cmdptr);
5560 }
5561 
5562 
5563 /**
5564  * mpi3mr_memset_buffers - memset memory for a controller
5565  * @sc: Adapter instance reference
5566  *
5567  * clear all the memory allocated for a controller, typically
5568  * called post reset to reuse the memory allocated during the
5569  * controller init.
5570  *
5571  * Return: Nothing.
5572  */
5573 static void mpi3mr_memset_buffers(struct mpi3mr_softc *sc)
5574 {
5575 	U16 i;
5576 	struct mpi3mr_throttle_group_info *tg;
5577 
5578 	memset(sc->admin_req, 0, sc->admin_req_q_sz);
5579 	memset(sc->admin_reply, 0, sc->admin_reply_q_sz);
5580 
5581 	memset(sc->init_cmds.reply, 0, sc->reply_sz);
5582 	memset(sc->ioctl_cmds.reply, 0, sc->reply_sz);
5583 	memset(sc->host_tm_cmds.reply, 0, sc->reply_sz);
5584 	memset(sc->pel_cmds.reply, 0, sc->reply_sz);
5585 	memset(sc->pel_abort_cmd.reply, 0, sc->reply_sz);
5586 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5587 		memset(sc->dev_rmhs_cmds[i].reply, 0, sc->reply_sz);
5588 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5589 		memset(sc->evtack_cmds[i].reply, 0, sc->reply_sz);
5590 	memset(sc->removepend_bitmap, 0, sc->dev_handle_bitmap_sz);
5591 	memset(sc->devrem_bitmap, 0, sc->devrem_bitmap_sz);
5592 	memset(sc->evtack_cmds_bitmap, 0, sc->evtack_cmds_bitmap_sz);
5593 
5594 	for (i = 0; i < sc->num_queues; i++) {
5595 		sc->op_reply_q[i].qid = 0;
5596 		sc->op_reply_q[i].ci = 0;
5597 		sc->op_reply_q[i].num_replies = 0;
5598 		sc->op_reply_q[i].ephase = 0;
5599 		mpi3mr_atomic_set(&sc->op_reply_q[i].pend_ios, 0);
5600 		memset(sc->op_reply_q[i].q_base, 0, sc->op_reply_q[i].qsz);
5601 
5602 		sc->op_req_q[i].ci = 0;
5603 		sc->op_req_q[i].pi = 0;
5604 		sc->op_req_q[i].num_reqs = 0;
5605 		sc->op_req_q[i].qid = 0;
5606 		sc->op_req_q[i].reply_qid = 0;
5607 		memset(sc->op_req_q[i].q_base, 0, sc->op_req_q[i].qsz);
5608 	}
5609 
5610 	mpi3mr_atomic_set(&sc->pend_large_data_sz, 0);
5611 	if (sc->throttle_groups) {
5612 		tg = sc->throttle_groups;
5613 		for (i = 0; i < sc->num_io_throttle_group; i++, tg++) {
5614 			tg->id = 0;
5615 			tg->fw_qd = 0;
5616 			tg->modified_qd = 0;
5617 			tg->io_divert= 0;
5618 			tg->high = 0;
5619 			tg->low = 0;
5620 			mpi3mr_atomic_set(&tg->pend_large_data_sz, 0);
5621 		}
5622  	}
5623 }
5624 
5625 /**
5626  * mpi3mr_invalidate_devhandles -Invalidate device handles
5627  * @sc: Adapter instance reference
5628  *
5629  * Invalidate the device handles in the target device structures
5630  * . Called post reset prior to reinitializing the controller.
5631  *
5632  * Return: Nothing.
5633  */
5634 static void mpi3mr_invalidate_devhandles(struct mpi3mr_softc *sc)
5635 {
5636 	struct mpi3mr_target *target = NULL;
5637 
5638 	mtx_lock_spin(&sc->target_lock);
5639 	TAILQ_FOREACH(target, &sc->cam_sc->tgt_list, tgt_next) {
5640 		if (target) {
5641 			target->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
5642 			target->io_throttle_enabled = 0;
5643 			target->io_divert = 0;
5644 			target->throttle_group = NULL;
5645 		}
5646 	}
5647 	mtx_unlock_spin(&sc->target_lock);
5648 }
5649 
5650 /**
5651  * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
5652  * @sc: Adapter instance reference
5653  *
5654  * This is executed post controller reset to identify any
5655  * missing devices during reset and remove from the upper layers
5656  * or expose any newly detected device to the upper layers.
5657  *
5658  * Return: Nothing.
5659  */
5660 
5661 static void mpi3mr_rfresh_tgtdevs(struct mpi3mr_softc *sc)
5662 {
5663 	struct mpi3mr_target *target = NULL;
5664 	struct mpi3mr_target *target_temp = NULL;
5665 
5666 	TAILQ_FOREACH_SAFE(target, &sc->cam_sc->tgt_list, tgt_next, target_temp) {
5667 		if (target->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
5668 			if (target->exposed_to_os)
5669 				mpi3mr_remove_device_from_os(sc, target->dev_handle);
5670 			mpi3mr_remove_device_from_list(sc, target, true);
5671 		}
5672 	}
5673 
5674 	TAILQ_FOREACH(target, &sc->cam_sc->tgt_list, tgt_next) {
5675 		if ((target->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
5676 		    !target->is_hidden && !target->exposed_to_os) {
5677 			mpi3mr_add_device(sc, target->per_id);
5678 		}
5679 	}
5680 
5681 }
5682 
5683 static void mpi3mr_flush_io(struct mpi3mr_softc *sc)
5684 {
5685 	int i;
5686 	struct mpi3mr_cmd *cmd = NULL;
5687 	union ccb *ccb = NULL;
5688 
5689 	for (i = 0; i < sc->max_host_ios; i++) {
5690 		cmd = sc->cmd_list[i];
5691 
5692 		if (cmd && cmd->ccb) {
5693 			if (cmd->callout_owner) {
5694 				ccb = (union ccb *)(cmd->ccb);
5695 				ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
5696 				mpi3mr_cmd_done(sc, cmd);
5697 			} else {
5698 				cmd->ccb = NULL;
5699 				mpi3mr_release_command(cmd);
5700 			}
5701 		}
5702 	}
5703 }
5704 /**
5705  * mpi3mr_clear_reset_history - Clear reset history
5706  * @sc: Adapter instance reference
5707  *
5708  * Write the reset history bit in IOC Status to clear the bit,
5709  * if it is already set.
5710  *
5711  * Return: Nothing.
5712  */
5713 static inline void mpi3mr_clear_reset_history(struct mpi3mr_softc *sc)
5714 {
5715 	U32 ioc_status;
5716 
5717 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
5718 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
5719 		mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_STATUS_OFFSET, ioc_status);
5720 }
5721 
5722 /**
5723  * mpi3mr_set_diagsave - Set diag save bit for snapdump
5724  * @sc: Adapter reference
5725  *
5726  * Set diag save bit in IOC configuration register to enable
5727  * snapdump.
5728  *
5729  * Return: Nothing.
5730  */
5731 static inline void mpi3mr_set_diagsave(struct mpi3mr_softc *sc)
5732 {
5733 	U32 ioc_config;
5734 
5735 	ioc_config =
5736 	    mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
5737 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
5738 	mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET, ioc_config);
5739 }
5740 
5741 /**
5742  * mpi3mr_issue_reset - Issue reset to the controller
5743  * @sc: Adapter reference
5744  * @reset_type: Reset type
5745  * @reset_reason: Reset reason code
5746  *
5747  * Unlock the host diagnostic registers and write the specific
5748  * reset type to that, wait for reset acknowledgement from the
5749  * controller, if the reset is not successful retry for the
5750  * predefined number of times.
5751  *
5752  * Return: 0 on success, non-zero on failure.
5753  */
5754 static int mpi3mr_issue_reset(struct mpi3mr_softc *sc, U16 reset_type,
5755 	U32 reset_reason)
5756 {
5757 	int retval = -1;
5758 	U8 unlock_retry_count = 0;
5759 	U32 host_diagnostic, ioc_status, ioc_config;
5760 	U32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
5761 
5762 	if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
5763 	    (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
5764 		return retval;
5765 	if (sc->unrecoverable)
5766 		return retval;
5767 
5768 	if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) {
5769 		retval = 0;
5770 		return retval;
5771 	}
5772 
5773 	mpi3mr_dprint(sc, MPI3MR_INFO, "%s reset due to %s(0x%x)\n",
5774 	    mpi3mr_reset_type_name(reset_type),
5775 	    mpi3mr_reset_rc_name(reset_reason), reset_reason);
5776 
5777 	mpi3mr_clear_reset_history(sc);
5778 	do {
5779 		mpi3mr_dprint(sc, MPI3MR_INFO,
5780 		    "Write magic sequence to unlock host diag register (retry=%d)\n",
5781 		    ++unlock_retry_count);
5782 		if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
5783 			mpi3mr_dprint(sc, MPI3MR_ERROR,
5784 			    "%s reset failed! due to host diag register unlock failure"
5785 			    "host_diagnostic(0x%08x)\n", mpi3mr_reset_type_name(reset_type),
5786 			    host_diagnostic);
5787 			sc->unrecoverable = 1;
5788 			return retval;
5789 		}
5790 
5791 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5792 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH);
5793 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5794 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST);
5795 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5796 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND);
5797 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5798 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD);
5799 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5800 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH);
5801 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5802 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH);
5803 		mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5804 			MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH);
5805 
5806 		DELAY(1000); /* delay in usec */
5807 		host_diagnostic = mpi3mr_regread(sc, MPI3_SYSIF_HOST_DIAG_OFFSET);
5808 		mpi3mr_dprint(sc, MPI3MR_INFO,
5809 		    "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
5810 		    unlock_retry_count, host_diagnostic);
5811 	} while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
5812 
5813 	mpi3mr_regwrite(sc, MPI3_SYSIF_SCRATCHPAD0_OFFSET, reset_reason);
5814 	mpi3mr_regwrite(sc, MPI3_SYSIF_HOST_DIAG_OFFSET, host_diagnostic | reset_type);
5815 
5816 	if (reset_type == MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) {
5817 		do {
5818 			ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
5819 			if (ioc_status &
5820 			    MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
5821 				ioc_config =
5822 				    mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
5823 				if (mpi3mr_soft_reset_success(ioc_status,
5824 				    ioc_config)) {
5825 					mpi3mr_clear_reset_history(sc);
5826 					retval = 0;
5827 					break;
5828 				}
5829 			}
5830 			DELAY(100 * 1000);
5831 		} while (--timeout);
5832 	} else if (reset_type == MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT) {
5833 		do {
5834 			ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
5835 			if (mpi3mr_diagfault_success(sc, ioc_status)) {
5836 				retval = 0;
5837 				break;
5838 			}
5839 			DELAY(100 * 1000);
5840 		} while (--timeout);
5841 	}
5842 
5843 	mpi3mr_regwrite(sc, MPI3_SYSIF_WRITE_SEQUENCE_OFFSET,
5844 		MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND);
5845 
5846 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
5847 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
5848 
5849 	mpi3mr_dprint(sc, MPI3MR_INFO,
5850 	    "IOC Status/Config after %s reset is (0x%x)/(0x%x)\n",
5851 	    !retval ? "successful":"failed", ioc_status,
5852 	    ioc_config);
5853 
5854 	if (retval)
5855 		sc->unrecoverable = 1;
5856 
5857 	return retval;
5858 }
5859 
5860 inline void mpi3mr_cleanup_event_taskq(struct mpi3mr_softc *sc)
5861 {
5862 	/*
5863 	 * Block the taskqueue before draining.  This means any new tasks won't
5864 	 * be queued to the taskqueue worker thread.  But it doesn't stop the
5865 	 * current workers that are running.  taskqueue_drain waits for those
5866 	 * correctly in the case of thread backed taskqueues.  The while loop
5867 	 * ensures that all taskqueue threads have finished their current tasks.
5868 	 */
5869 	taskqueue_block(sc->cam_sc->ev_tq);
5870 	while (taskqueue_cancel(sc->cam_sc->ev_tq, &sc->cam_sc->ev_task, NULL) != 0) {
5871 		taskqueue_drain(sc->cam_sc->ev_tq, &sc->cam_sc->ev_task);
5872 	}
5873 }
5874 
5875 /**
5876  * mpi3mr_soft_reset_handler - Reset the controller
5877  * @sc: Adapter instance reference
5878  * @reset_reason: Reset reason code
5879  * @snapdump: snapdump enable/disbale bit
5880  *
5881  * This is an handler for recovering controller by issuing soft
5882  * reset or diag fault reset. This is a blocking function and
5883  * when one reset is executed if any other resets they will be
5884  * blocked. All IOCTLs/IO will be blocked during the reset. If
5885  * controller reset is successful then the controller will be
5886  * reinitalized, otherwise the controller will be marked as not
5887  * recoverable
5888  *
5889  * Return: 0 on success, non-zero on failure.
5890  */
5891 int mpi3mr_soft_reset_handler(struct mpi3mr_softc *sc,
5892 	U32 reset_reason, bool snapdump)
5893 {
5894 	int retval = 0, i = 0;
5895 	enum mpi3mr_iocstate ioc_state;
5896 
5897 	mpi3mr_dprint(sc, MPI3MR_INFO, "soft reset invoked: reason code: %s\n",
5898 	    mpi3mr_reset_rc_name(reset_reason));
5899 
5900 	if ((reset_reason == MPI3MR_RESET_FROM_IOCTL) &&
5901 	     (sc->reset.ioctl_reset_snapdump != true))
5902 		snapdump = false;
5903 
5904 	mpi3mr_dprint(sc, MPI3MR_INFO,
5905 	    "soft_reset_handler: wait if diag save is in progress\n");
5906 	while (sc->diagsave_timeout)
5907 		DELAY(1000 * 1000);
5908 
5909 	ioc_state = mpi3mr_get_iocstate(sc);
5910 	if (ioc_state == MRIOC_STATE_UNRECOVERABLE) {
5911 		mpi3mr_dprint(sc, MPI3MR_ERROR, "controller is in unrecoverable state, exit\n");
5912 		sc->reset.type = MPI3MR_NO_RESET;
5913 		sc->reset.reason = MPI3MR_DEFAULT_RESET_REASON;
5914 		sc->reset.status = -1;
5915 		sc->reset.ioctl_reset_snapdump = false;
5916 		return -1;
5917 	}
5918 
5919 	if (sc->reset_in_progress) {
5920 		mpi3mr_dprint(sc, MPI3MR_INFO, "reset is already in progress, exit\n");
5921 		return -1;
5922 	}
5923 
5924 	/* Pause IOs, drain and block the event taskqueue */
5925 	xpt_freeze_simq(sc->cam_sc->sim, 1);
5926 
5927 	mpi3mr_cleanup_event_taskq(sc);
5928 
5929 	sc->reset_in_progress = 1;
5930 	sc->block_ioctls = 1;
5931 
5932 	while (mpi3mr_atomic_read(&sc->pend_ioctls) && (i < PEND_IOCTLS_COMP_WAIT_TIME)) {
5933 		ioc_state = mpi3mr_get_iocstate(sc);
5934 		if (ioc_state == MRIOC_STATE_FAULT)
5935 			break;
5936 		i++;
5937 		if (!(i % 5)) {
5938 			mpi3mr_dprint(sc, MPI3MR_INFO,
5939 			    "[%2ds]waiting for IOCTL to be finished from %s\n", i, __func__);
5940 		}
5941 		DELAY(1000 * 1000);
5942 	}
5943 
5944 	if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
5945 	    (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) &&
5946 	    (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
5947 
5948 		mpi3mr_dprint(sc, MPI3MR_INFO, "Turn off events prior to reset\n");
5949 
5950 		for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
5951 			sc->event_masks[i] = -1;
5952 		mpi3mr_issue_event_notification(sc);
5953 	}
5954 
5955 	mpi3mr_disable_interrupts(sc);
5956 
5957 	if (snapdump)
5958 		mpi3mr_trigger_snapdump(sc, reset_reason);
5959 
5960 	retval = mpi3mr_issue_reset(sc,
5961 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
5962 	if (retval) {
5963 		mpi3mr_dprint(sc, MPI3MR_ERROR, "Failed to issue soft reset to the ioc\n");
5964 		goto out;
5965 	}
5966 
5967 	mpi3mr_flush_drv_cmds(sc);
5968 	mpi3mr_flush_io(sc);
5969 	mpi3mr_invalidate_devhandles(sc);
5970 	mpi3mr_memset_buffers(sc);
5971 
5972 	if (sc->prepare_for_reset) {
5973 		sc->prepare_for_reset = 0;
5974 		sc->prepare_for_reset_timeout_counter = 0;
5975 	}
5976 
5977 	retval = mpi3mr_initialize_ioc(sc, MPI3MR_INIT_TYPE_RESET);
5978 	if (retval) {
5979 		mpi3mr_dprint(sc, MPI3MR_ERROR, "reinit after soft reset failed: reason %d\n",
5980 		    reset_reason);
5981 		goto out;
5982 	}
5983 
5984 	DELAY((1000 * 1000) * 10);
5985 out:
5986 	if (!retval) {
5987 		sc->diagsave_timeout = 0;
5988 		sc->reset_in_progress = 0;
5989 		mpi3mr_rfresh_tgtdevs(sc);
5990 		sc->ts_update_counter = 0;
5991 		sc->block_ioctls = 0;
5992 		sc->pel_abort_requested = 0;
5993 		if (sc->pel_wait_pend) {
5994 			sc->pel_cmds.retry_count = 0;
5995 			mpi3mr_issue_pel_wait(sc, &sc->pel_cmds);
5996 			mpi3mr_app_send_aen(sc);
5997 		}
5998 	} else {
5999 		mpi3mr_issue_reset(sc,
6000 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
6001 		sc->unrecoverable = 1;
6002 		sc->reset_in_progress = 0;
6003 	}
6004 
6005 	mpi3mr_dprint(sc, MPI3MR_INFO, "Soft Reset: %s\n", ((retval == 0) ? "SUCCESS" : "FAILED"));
6006 
6007 	taskqueue_unblock(sc->cam_sc->ev_tq);
6008 	xpt_release_simq(sc->cam_sc->sim, 1);
6009 
6010 	sc->reset.type = MPI3MR_NO_RESET;
6011 	sc->reset.reason = MPI3MR_DEFAULT_RESET_REASON;
6012 	sc->reset.status = retval;
6013 	sc->reset.ioctl_reset_snapdump = false;
6014 
6015 	return retval;
6016 }
6017 
6018 /**
6019  * mpi3mr_issue_ioc_shutdown - shutdown controller
6020  * @sc: Adapter instance reference
6021  *
6022  * Send shutodwn notification to the controller and wait for the
6023  * shutdown_timeout for it to be completed.
6024  *
6025  * Return: Nothing.
6026  */
6027 static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_softc *sc)
6028 {
6029 	U32 ioc_config, ioc_status;
6030 	U8 retval = 1, retry = 0;
6031 	U32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
6032 
6033 	mpi3mr_dprint(sc, MPI3MR_INFO, "sending shutdown notification\n");
6034 	if (sc->unrecoverable) {
6035 		mpi3mr_dprint(sc, MPI3MR_ERROR,
6036 		    "controller is unrecoverable, shutdown not issued\n");
6037 		return;
6038 	}
6039 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
6040 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
6041 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
6042 		mpi3mr_dprint(sc, MPI3MR_ERROR, "shutdown already in progress\n");
6043 		return;
6044 	}
6045 
6046 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
6047 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
6048 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
6049 
6050 	mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET, ioc_config);
6051 
6052 	if (sc->facts.shutdown_timeout)
6053 		timeout = sc->facts.shutdown_timeout * 10;
6054 
6055 	do {
6056 		ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
6057 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
6058 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
6059 			retval = 0;
6060 			break;
6061 		}
6062 
6063 		if (sc->unrecoverable)
6064 			break;
6065 
6066 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
6067 			mpi3mr_print_fault_info(sc);
6068 
6069 			if (retry >= MPI3MR_MAX_SHUTDOWN_RETRY_COUNT)
6070 				break;
6071 
6072 			if (mpi3mr_issue_reset(sc,
6073 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
6074 			    MPI3MR_RESET_FROM_CTLR_CLEANUP))
6075 				break;
6076 
6077 			ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
6078 			ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
6079 			ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
6080 
6081 			mpi3mr_regwrite(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET, ioc_config);
6082 
6083 			if (sc->facts.shutdown_timeout)
6084 				timeout = sc->facts.shutdown_timeout * 10;
6085 
6086 			retry++;
6087 		}
6088 
6089                 DELAY(100 * 1000);
6090 
6091 	} while (--timeout);
6092 
6093 	ioc_status = mpi3mr_regread(sc, MPI3_SYSIF_IOC_STATUS_OFFSET);
6094 	ioc_config = mpi3mr_regread(sc, MPI3_SYSIF_IOC_CONFIG_OFFSET);
6095 
6096 	if (retval) {
6097 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
6098 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
6099 			mpi3mr_dprint(sc, MPI3MR_ERROR,
6100 			    "shutdown still in progress after timeout\n");
6101 	}
6102 
6103 	mpi3mr_dprint(sc, MPI3MR_INFO,
6104 	    "ioc_status/ioc_config after %s shutdown is (0x%x)/(0x%x)\n",
6105 	    (!retval)?"successful":"failed", ioc_status,
6106 	    ioc_config);
6107 }
6108 
6109 /**
6110  * mpi3mr_cleanup_ioc - Cleanup controller
6111  * @sc: Adapter instance reference
6112 
6113  * controller cleanup handler, Message unit reset or soft reset
6114  * and shutdown notification is issued to the controller.
6115  *
6116  * Return: Nothing.
6117  */
6118 void mpi3mr_cleanup_ioc(struct mpi3mr_softc *sc)
6119 {
6120 	enum mpi3mr_iocstate ioc_state;
6121 
6122 	mpi3mr_dprint(sc, MPI3MR_INFO, "cleaning up the controller\n");
6123 	mpi3mr_disable_interrupts(sc);
6124 
6125 	ioc_state = mpi3mr_get_iocstate(sc);
6126 
6127 	if ((!sc->unrecoverable) && (!sc->reset_in_progress) &&
6128 	    (ioc_state == MRIOC_STATE_READY)) {
6129 		if (mpi3mr_mur_ioc(sc,
6130 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
6131 			mpi3mr_issue_reset(sc,
6132 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
6133 			    MPI3MR_RESET_FROM_MUR_FAILURE);
6134 		mpi3mr_issue_ioc_shutdown(sc);
6135 	}
6136 
6137 	mpi3mr_dprint(sc, MPI3MR_INFO, "controller cleanup completed\n");
6138 }
6139