xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_fw.c (revision 824a1566)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Broadcom MPI3 Storage Controllers
4  *
5  * Copyright (C) 2017-2021 Broadcom Inc.
6  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7  *
8  */
9 
10 #include "mpi3mr.h"
11 #include <linux/io-64-nonatomic-lo-hi.h>
12 
13 #if defined(writeq) && defined(CONFIG_64BIT)
14 static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
15 {
16 	writeq(b, addr);
17 }
18 #else
19 static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
20 {
21 	__u64 data_out = b;
22 
23 	writel((u32)(data_out), addr);
24 	writel((u32)(data_out >> 32), (addr + 4));
25 }
26 #endif
27 
28 static void mpi3mr_sync_irqs(struct mpi3mr_ioc *mrioc)
29 {
30 	u16 i, max_vectors;
31 
32 	max_vectors = mrioc->intr_info_count;
33 
34 	for (i = 0; i < max_vectors; i++)
35 		synchronize_irq(pci_irq_vector(mrioc->pdev, i));
36 }
37 
38 void mpi3mr_ioc_disable_intr(struct mpi3mr_ioc *mrioc)
39 {
40 	mrioc->intr_enabled = 0;
41 	mpi3mr_sync_irqs(mrioc);
42 }
43 
44 void mpi3mr_ioc_enable_intr(struct mpi3mr_ioc *mrioc)
45 {
46 	mrioc->intr_enabled = 1;
47 }
48 
49 static void mpi3mr_cleanup_isr(struct mpi3mr_ioc *mrioc)
50 {
51 	u16 i;
52 
53 	mpi3mr_ioc_disable_intr(mrioc);
54 
55 	if (!mrioc->intr_info)
56 		return;
57 
58 	for (i = 0; i < mrioc->intr_info_count; i++)
59 		free_irq(pci_irq_vector(mrioc->pdev, i),
60 		    (mrioc->intr_info + i));
61 
62 	kfree(mrioc->intr_info);
63 	mrioc->intr_info = NULL;
64 	mrioc->intr_info_count = 0;
65 	pci_free_irq_vectors(mrioc->pdev);
66 }
67 
68 void mpi3mr_add_sg_single(void *paddr, u8 flags, u32 length,
69 	dma_addr_t dma_addr)
70 {
71 	struct mpi3_sge_common *sgel = paddr;
72 
73 	sgel->flags = flags;
74 	sgel->length = cpu_to_le32(length);
75 	sgel->address = cpu_to_le64(dma_addr);
76 }
77 
78 void mpi3mr_build_zero_len_sge(void *paddr)
79 {
80 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
81 
82 	mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1);
83 }
84 
85 void *mpi3mr_get_reply_virt_addr(struct mpi3mr_ioc *mrioc,
86 	dma_addr_t phys_addr)
87 {
88 	if (!phys_addr)
89 		return NULL;
90 
91 	if ((phys_addr < mrioc->reply_buf_dma) ||
92 	    (phys_addr > mrioc->reply_buf_dma_max_address))
93 		return NULL;
94 
95 	return mrioc->reply_buf + (phys_addr - mrioc->reply_buf_dma);
96 }
97 
98 void *mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_ioc *mrioc,
99 	dma_addr_t phys_addr)
100 {
101 	if (!phys_addr)
102 		return NULL;
103 
104 	return mrioc->sense_buf + (phys_addr - mrioc->sense_buf_dma);
105 }
106 
107 static void mpi3mr_repost_reply_buf(struct mpi3mr_ioc *mrioc,
108 	u64 reply_dma)
109 {
110 	u32 old_idx = 0;
111 
112 	spin_lock(&mrioc->reply_free_queue_lock);
113 	old_idx  =  mrioc->reply_free_queue_host_index;
114 	mrioc->reply_free_queue_host_index = (
115 	    (mrioc->reply_free_queue_host_index ==
116 	    (mrioc->reply_free_qsz - 1)) ? 0 :
117 	    (mrioc->reply_free_queue_host_index + 1));
118 	mrioc->reply_free_q[old_idx] = cpu_to_le64(reply_dma);
119 	writel(mrioc->reply_free_queue_host_index,
120 	    &mrioc->sysif_regs->reply_free_host_index);
121 	spin_unlock(&mrioc->reply_free_queue_lock);
122 }
123 
124 void mpi3mr_repost_sense_buf(struct mpi3mr_ioc *mrioc,
125 	u64 sense_buf_dma)
126 {
127 	u32 old_idx = 0;
128 
129 	spin_lock(&mrioc->sbq_lock);
130 	old_idx  =  mrioc->sbq_host_index;
131 	mrioc->sbq_host_index = ((mrioc->sbq_host_index ==
132 	    (mrioc->sense_buf_q_sz - 1)) ? 0 :
133 	    (mrioc->sbq_host_index + 1));
134 	mrioc->sense_buf_q[old_idx] = cpu_to_le64(sense_buf_dma);
135 	writel(mrioc->sbq_host_index,
136 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
137 	spin_unlock(&mrioc->sbq_lock);
138 }
139 
140 static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc,
141 	struct mpi3_default_reply *def_reply)
142 {
143 	struct mpi3_event_notification_reply *event_reply =
144 	    (struct mpi3_event_notification_reply *)def_reply;
145 
146 	mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count);
147 }
148 
149 static struct mpi3mr_drv_cmd *
150 mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag,
151 	struct mpi3_default_reply *def_reply)
152 {
153 	switch (host_tag) {
154 	case MPI3MR_HOSTTAG_INITCMDS:
155 		return &mrioc->init_cmds;
156 	case MPI3MR_HOSTTAG_INVALID:
157 		if (def_reply && def_reply->function ==
158 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
159 			mpi3mr_handle_events(mrioc, def_reply);
160 		return NULL;
161 	default:
162 		break;
163 	}
164 
165 	return NULL;
166 }
167 
168 static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
169 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
170 {
171 	u16 reply_desc_type, host_tag = 0;
172 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
173 	u32 ioc_loginfo = 0;
174 	struct mpi3_status_reply_descriptor *status_desc;
175 	struct mpi3_address_reply_descriptor *addr_desc;
176 	struct mpi3_success_reply_descriptor *success_desc;
177 	struct mpi3_default_reply *def_reply = NULL;
178 	struct mpi3mr_drv_cmd *cmdptr = NULL;
179 	struct mpi3_scsi_io_reply *scsi_reply;
180 	u8 *sense_buf = NULL;
181 
182 	*reply_dma = 0;
183 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
184 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
185 	switch (reply_desc_type) {
186 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
187 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
188 		host_tag = le16_to_cpu(status_desc->host_tag);
189 		ioc_status = le16_to_cpu(status_desc->ioc_status);
190 		if (ioc_status &
191 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
192 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
193 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
194 		break;
195 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
196 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
197 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
198 		def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
199 		if (!def_reply)
200 			goto out;
201 		host_tag = le16_to_cpu(def_reply->host_tag);
202 		ioc_status = le16_to_cpu(def_reply->ioc_status);
203 		if (ioc_status &
204 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
205 			ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
206 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
207 		if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
208 			scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
209 			sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
210 			    le64_to_cpu(scsi_reply->sense_data_buffer_address));
211 		}
212 		break;
213 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
214 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
215 		host_tag = le16_to_cpu(success_desc->host_tag);
216 		break;
217 	default:
218 		break;
219 	}
220 
221 	cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
222 	if (cmdptr) {
223 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
224 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
225 			cmdptr->ioc_loginfo = ioc_loginfo;
226 			cmdptr->ioc_status = ioc_status;
227 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
228 			if (def_reply) {
229 				cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
230 				memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
231 				    mrioc->facts.reply_sz);
232 			}
233 			if (cmdptr->is_waiting) {
234 				complete(&cmdptr->done);
235 				cmdptr->is_waiting = 0;
236 			} else if (cmdptr->callback)
237 				cmdptr->callback(mrioc, cmdptr);
238 		}
239 	}
240 out:
241 	if (sense_buf)
242 		mpi3mr_repost_sense_buf(mrioc,
243 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
244 }
245 
246 static int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
247 {
248 	u32 exp_phase = mrioc->admin_reply_ephase;
249 	u32 admin_reply_ci = mrioc->admin_reply_ci;
250 	u32 num_admin_replies = 0;
251 	u64 reply_dma = 0;
252 	struct mpi3_default_reply_descriptor *reply_desc;
253 
254 	reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
255 	    admin_reply_ci;
256 
257 	if ((le16_to_cpu(reply_desc->reply_flags) &
258 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
259 		return 0;
260 
261 	do {
262 		mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
263 		mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
264 		if (reply_dma)
265 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
266 		num_admin_replies++;
267 		if (++admin_reply_ci == mrioc->num_admin_replies) {
268 			admin_reply_ci = 0;
269 			exp_phase ^= 1;
270 		}
271 		reply_desc =
272 		    (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
273 		    admin_reply_ci;
274 		if ((le16_to_cpu(reply_desc->reply_flags) &
275 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
276 			break;
277 	} while (1);
278 
279 	writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
280 	mrioc->admin_reply_ci = admin_reply_ci;
281 	mrioc->admin_reply_ephase = exp_phase;
282 
283 	return num_admin_replies;
284 }
285 
286 static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
287 {
288 	struct mpi3mr_intr_info *intr_info = privdata;
289 	struct mpi3mr_ioc *mrioc;
290 	u16 midx;
291 	u32 num_admin_replies = 0;
292 
293 	if (!intr_info)
294 		return IRQ_NONE;
295 
296 	mrioc = intr_info->mrioc;
297 
298 	if (!mrioc->intr_enabled)
299 		return IRQ_NONE;
300 
301 	midx = intr_info->msix_index;
302 
303 	if (!midx)
304 		num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
305 
306 	if (num_admin_replies)
307 		return IRQ_HANDLED;
308 	else
309 		return IRQ_NONE;
310 }
311 
312 static irqreturn_t mpi3mr_isr(int irq, void *privdata)
313 {
314 	struct mpi3mr_intr_info *intr_info = privdata;
315 	int ret;
316 
317 	if (!intr_info)
318 		return IRQ_NONE;
319 
320 	/* Call primary ISR routine */
321 	ret = mpi3mr_isr_primary(irq, privdata);
322 
323 	return ret;
324 }
325 
326 /**
327  * mpi3mr_isr_poll - Reply queue polling routine
328  * @irq: IRQ
329  * @privdata: Interrupt info
330  *
331  * poll for pending I/O completions in a loop until pending I/Os
332  * present or controller queue depth I/Os are processed.
333  *
334  * Return: IRQ_NONE or IRQ_HANDLED
335  */
336 static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
337 {
338 	return IRQ_HANDLED;
339 }
340 
341 /**
342  * mpi3mr_request_irq - Request IRQ and register ISR
343  * @mrioc: Adapter instance reference
344  * @index: IRQ vector index
345  *
346  * Request threaded ISR with primary ISR and secondary
347  *
348  * Return: 0 on success and non zero on failures.
349  */
350 static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
351 {
352 	struct pci_dev *pdev = mrioc->pdev;
353 	struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
354 	int retval = 0;
355 
356 	intr_info->mrioc = mrioc;
357 	intr_info->msix_index = index;
358 	intr_info->op_reply_q = NULL;
359 
360 	snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
361 	    mrioc->driver_name, mrioc->id, index);
362 
363 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
364 	    mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
365 	if (retval) {
366 		ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
367 		    intr_info->name, pci_irq_vector(pdev, index));
368 		return retval;
369 	}
370 
371 	return retval;
372 }
373 
374 /**
375  * mpi3mr_setup_isr - Setup ISR for the controller
376  * @mrioc: Adapter instance reference
377  * @setup_one: Request one IRQ or more
378  *
379  * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
380  *
381  * Return: 0 on success and non zero on failures.
382  */
383 static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
384 {
385 	unsigned int irq_flags = PCI_IRQ_MSIX;
386 	u16 max_vectors = 0, i;
387 	int retval = 0;
388 	struct irq_affinity desc = { .pre_vectors =  1};
389 
390 	mpi3mr_cleanup_isr(mrioc);
391 
392 	if (setup_one || reset_devices)
393 		max_vectors = 1;
394 	else {
395 		max_vectors =
396 		    min_t(int, mrioc->cpu_count + 1, mrioc->msix_count);
397 
398 		ioc_info(mrioc,
399 		    "MSI-X vectors supported: %d, no of cores: %d,",
400 		    mrioc->msix_count, mrioc->cpu_count);
401 		ioc_info(mrioc,
402 		    "MSI-x vectors requested: %d\n", max_vectors);
403 	}
404 
405 	irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
406 
407 	i = pci_alloc_irq_vectors_affinity(mrioc->pdev,
408 	    1, max_vectors, irq_flags, &desc);
409 	if (i <= 0) {
410 		ioc_err(mrioc, "Cannot alloc irq vectors\n");
411 		goto out_failed;
412 	}
413 	if (i != max_vectors) {
414 		ioc_info(mrioc,
415 		    "allocated vectors (%d) are less than configured (%d)\n",
416 		    i, max_vectors);
417 
418 		max_vectors = i;
419 	}
420 	mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
421 	    GFP_KERNEL);
422 	if (!mrioc->intr_info) {
423 		retval = -1;
424 		pci_free_irq_vectors(mrioc->pdev);
425 		goto out_failed;
426 	}
427 	for (i = 0; i < max_vectors; i++) {
428 		retval = mpi3mr_request_irq(mrioc, i);
429 		if (retval) {
430 			mrioc->intr_info_count = i;
431 			goto out_failed;
432 		}
433 	}
434 	mrioc->intr_info_count = max_vectors;
435 	mpi3mr_ioc_enable_intr(mrioc);
436 	return retval;
437 out_failed:
438 	mpi3mr_cleanup_isr(mrioc);
439 
440 	return retval;
441 }
442 
443 static const struct {
444 	enum mpi3mr_iocstate value;
445 	char *name;
446 } mrioc_states[] = {
447 	{ MRIOC_STATE_READY, "ready" },
448 	{ MRIOC_STATE_FAULT, "fault" },
449 	{ MRIOC_STATE_RESET, "reset" },
450 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
451 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
452 	{ MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
453 };
454 
455 static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
456 {
457 	int i;
458 	char *name = NULL;
459 
460 	for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
461 		if (mrioc_states[i].value == mrioc_state) {
462 			name = mrioc_states[i].name;
463 			break;
464 		}
465 	}
466 	return name;
467 }
468 
469 /**
470  * mpi3mr_print_fault_info - Display fault information
471  * @mrioc: Adapter instance reference
472  *
473  * Display the controller fault information if there is a
474  * controller fault.
475  *
476  * Return: Nothing.
477  */
478 static void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
479 {
480 	u32 ioc_status, code, code1, code2, code3;
481 
482 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
483 
484 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
485 		code = readl(&mrioc->sysif_regs->fault);
486 		code1 = readl(&mrioc->sysif_regs->fault_info[0]);
487 		code2 = readl(&mrioc->sysif_regs->fault_info[1]);
488 		code3 = readl(&mrioc->sysif_regs->fault_info[2]);
489 
490 		ioc_info(mrioc,
491 		    "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
492 		    code, code1, code2, code3);
493 	}
494 }
495 
496 /**
497  * mpi3mr_get_iocstate - Get IOC State
498  * @mrioc: Adapter instance reference
499  *
500  * Return a proper IOC state enum based on the IOC status and
501  * IOC configuration and unrcoverable state of the controller.
502  *
503  * Return: Current IOC state.
504  */
505 enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
506 {
507 	u32 ioc_status, ioc_config;
508 	u8 ready, enabled;
509 
510 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
511 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
512 
513 	if (mrioc->unrecoverable)
514 		return MRIOC_STATE_UNRECOVERABLE;
515 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
516 		return MRIOC_STATE_FAULT;
517 
518 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
519 	enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
520 
521 	if (ready && enabled)
522 		return MRIOC_STATE_READY;
523 	if ((!ready) && (!enabled))
524 		return MRIOC_STATE_RESET;
525 	if ((!ready) && (enabled))
526 		return MRIOC_STATE_BECOMING_READY;
527 
528 	return MRIOC_STATE_RESET_REQUESTED;
529 }
530 
531 /**
532  * mpi3mr_clear_reset_history - clear reset history
533  * @mrioc: Adapter instance reference
534  *
535  * Write the reset history bit in IOC status to clear the bit,
536  * if it is already set.
537  *
538  * Return: Nothing.
539  */
540 static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
541 {
542 	u32 ioc_status;
543 
544 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
545 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
546 		writel(ioc_status, &mrioc->sysif_regs->ioc_status);
547 }
548 
549 /**
550  * mpi3mr_issue_and_process_mur - Message unit Reset handler
551  * @mrioc: Adapter instance reference
552  * @reset_reason: Reset reason code
553  *
554  * Issue Message unit Reset to the controller and wait for it to
555  * be complete.
556  *
557  * Return: 0 on success, -1 on failure.
558  */
559 static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
560 	u32 reset_reason)
561 {
562 	u32 ioc_config, timeout, ioc_status;
563 	int retval = -1;
564 
565 	ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
566 	if (mrioc->unrecoverable) {
567 		ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
568 		return retval;
569 	}
570 	mpi3mr_clear_reset_history(mrioc);
571 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
572 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
573 	ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
574 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
575 
576 	timeout = mrioc->ready_timeout * 10;
577 	do {
578 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
579 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
580 			mpi3mr_clear_reset_history(mrioc);
581 			ioc_config =
582 			    readl(&mrioc->sysif_regs->ioc_configuration);
583 			if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
584 			      (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
585 			    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC))) {
586 				retval = 0;
587 				break;
588 			}
589 		}
590 		msleep(100);
591 	} while (--timeout);
592 
593 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
594 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
595 
596 	ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
597 	    (!retval) ? "successful" : "failed", ioc_status, ioc_config);
598 	return retval;
599 }
600 
601 /**
602  * mpi3mr_bring_ioc_ready - Bring controller to ready state
603  * @mrioc: Adapter instance reference
604  *
605  * Set Enable IOC bit in IOC configuration register and wait for
606  * the controller to become ready.
607  *
608  * Return: 0 on success, -1 on failure.
609  */
610 static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
611 {
612 	u32 ioc_config, timeout;
613 	enum mpi3mr_iocstate current_state;
614 
615 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
616 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
617 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
618 
619 	timeout = mrioc->ready_timeout * 10;
620 	do {
621 		current_state = mpi3mr_get_iocstate(mrioc);
622 		if (current_state == MRIOC_STATE_READY)
623 			return 0;
624 		msleep(100);
625 	} while (--timeout);
626 
627 	return -1;
628 }
629 
630 /**
631  * mpi3mr_set_diagsave - Set diag save bit for snapdump
632  * @mrioc: Adapter reference
633  *
634  * Set diag save bit in IOC configuration register to enable
635  * snapdump.
636  *
637  * Return: Nothing.
638  */
639 static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
640 {
641 	u32 ioc_config;
642 
643 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
644 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
645 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
646 }
647 
648 /**
649  * mpi3mr_issue_reset - Issue reset to the controller
650  * @mrioc: Adapter reference
651  * @reset_type: Reset type
652  * @reset_reason: Reset reason code
653  *
654  * TBD
655  *
656  * Return: 0 on success, non-zero on failure.
657  */
658 static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
659 	u32 reset_reason)
660 {
661 	return 0;
662 }
663 
664 /**
665  * mpi3mr_admin_request_post - Post request to admin queue
666  * @mrioc: Adapter reference
667  * @admin_req: MPI3 request
668  * @admin_req_sz: Request size
669  * @ignore_reset: Ignore reset in process
670  *
671  * Post the MPI3 request into admin request queue and
672  * inform the controller, if the queue is full return
673  * appropriate error.
674  *
675  * Return: 0 on success, non-zero on failure.
676  */
677 int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
678 	u16 admin_req_sz, u8 ignore_reset)
679 {
680 	u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
681 	int retval = 0;
682 	unsigned long flags;
683 	u8 *areq_entry;
684 
685 	if (mrioc->unrecoverable) {
686 		ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
687 		return -EFAULT;
688 	}
689 
690 	spin_lock_irqsave(&mrioc->admin_req_lock, flags);
691 	areq_pi = mrioc->admin_req_pi;
692 	areq_ci = mrioc->admin_req_ci;
693 	max_entries = mrioc->num_admin_req;
694 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
695 	    (areq_pi == (max_entries - 1)))) {
696 		ioc_err(mrioc, "AdminReqQ full condition detected\n");
697 		retval = -EAGAIN;
698 		goto out;
699 	}
700 	if (!ignore_reset && mrioc->reset_in_progress) {
701 		ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
702 		retval = -EAGAIN;
703 		goto out;
704 	}
705 	areq_entry = (u8 *)mrioc->admin_req_base +
706 	    (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
707 	memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
708 	memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
709 
710 	if (++areq_pi == max_entries)
711 		areq_pi = 0;
712 	mrioc->admin_req_pi = areq_pi;
713 
714 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
715 
716 out:
717 	spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
718 
719 	return retval;
720 }
721 
722 /**
723  * mpi3mr_setup_admin_qpair - Setup admin queue pair
724  * @mrioc: Adapter instance reference
725  *
726  * Allocate memory for admin queue pair if required and register
727  * the admin queue with the controller.
728  *
729  * Return: 0 on success, non-zero on failures.
730  */
731 static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
732 {
733 	int retval = 0;
734 	u32 num_admin_entries = 0;
735 
736 	mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
737 	mrioc->num_admin_req = mrioc->admin_req_q_sz /
738 	    MPI3MR_ADMIN_REQ_FRAME_SZ;
739 	mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
740 	mrioc->admin_req_base = NULL;
741 
742 	mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
743 	mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
744 	    MPI3MR_ADMIN_REPLY_FRAME_SZ;
745 	mrioc->admin_reply_ci = 0;
746 	mrioc->admin_reply_ephase = 1;
747 	mrioc->admin_reply_base = NULL;
748 
749 	if (!mrioc->admin_req_base) {
750 		mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
751 		    mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
752 
753 		if (!mrioc->admin_req_base) {
754 			retval = -1;
755 			goto out_failed;
756 		}
757 
758 		mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
759 		    mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
760 		    GFP_KERNEL);
761 
762 		if (!mrioc->admin_reply_base) {
763 			retval = -1;
764 			goto out_failed;
765 		}
766 	}
767 
768 	num_admin_entries = (mrioc->num_admin_replies << 16) |
769 	    (mrioc->num_admin_req);
770 	writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
771 	mpi3mr_writeq(mrioc->admin_req_dma,
772 	    &mrioc->sysif_regs->admin_request_queue_address);
773 	mpi3mr_writeq(mrioc->admin_reply_dma,
774 	    &mrioc->sysif_regs->admin_reply_queue_address);
775 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
776 	writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
777 	return retval;
778 
779 out_failed:
780 
781 	if (mrioc->admin_reply_base) {
782 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
783 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
784 		mrioc->admin_reply_base = NULL;
785 	}
786 	if (mrioc->admin_req_base) {
787 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
788 		    mrioc->admin_req_base, mrioc->admin_req_dma);
789 		mrioc->admin_req_base = NULL;
790 	}
791 	return retval;
792 }
793 
794 /**
795  * mpi3mr_issue_iocfacts - Send IOC Facts
796  * @mrioc: Adapter instance reference
797  * @facts_data: Cached IOC facts data
798  *
799  * Issue IOC Facts MPI request through admin queue and wait for
800  * the completion of it or time out.
801  *
802  * Return: 0 on success, non-zero on failures.
803  */
804 static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
805 	struct mpi3_ioc_facts_data *facts_data)
806 {
807 	struct mpi3_ioc_facts_request iocfacts_req;
808 	void *data = NULL;
809 	dma_addr_t data_dma;
810 	u32 data_len = sizeof(*facts_data);
811 	int retval = 0;
812 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
813 
814 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
815 	    GFP_KERNEL);
816 
817 	if (!data) {
818 		retval = -1;
819 		goto out;
820 	}
821 
822 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
823 	mutex_lock(&mrioc->init_cmds.mutex);
824 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
825 		retval = -1;
826 		ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
827 		mutex_unlock(&mrioc->init_cmds.mutex);
828 		goto out;
829 	}
830 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
831 	mrioc->init_cmds.is_waiting = 1;
832 	mrioc->init_cmds.callback = NULL;
833 	iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
834 	iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
835 
836 	mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
837 	    data_dma);
838 
839 	init_completion(&mrioc->init_cmds.done);
840 	retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
841 	    sizeof(iocfacts_req), 1);
842 	if (retval) {
843 		ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
844 		goto out_unlock;
845 	}
846 	wait_for_completion_timeout(&mrioc->init_cmds.done,
847 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
848 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
849 		ioc_err(mrioc, "Issue IOCFacts: command timed out\n");
850 		mpi3mr_set_diagsave(mrioc);
851 		mpi3mr_issue_reset(mrioc,
852 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
853 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
854 		mrioc->unrecoverable = 1;
855 		retval = -1;
856 		goto out_unlock;
857 	}
858 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
859 	    != MPI3_IOCSTATUS_SUCCESS) {
860 		ioc_err(mrioc,
861 		    "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
862 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
863 		    mrioc->init_cmds.ioc_loginfo);
864 		retval = -1;
865 		goto out_unlock;
866 	}
867 	memcpy(facts_data, (u8 *)data, data_len);
868 out_unlock:
869 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
870 	mutex_unlock(&mrioc->init_cmds.mutex);
871 
872 out:
873 	if (data)
874 		dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
875 
876 	return retval;
877 }
878 
879 /**
880  * mpi3mr_check_reset_dma_mask - Process IOC facts data
881  * @mrioc: Adapter instance reference
882  *
883  * Check whether the new DMA mask requested through IOCFacts by
884  * firmware needs to be set, if so set it .
885  *
886  * Return: 0 on success, non-zero on failure.
887  */
888 static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
889 {
890 	struct pci_dev *pdev = mrioc->pdev;
891 	int r;
892 	u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
893 
894 	if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
895 		return 0;
896 
897 	ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
898 	    mrioc->dma_mask, facts_dma_mask);
899 
900 	r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
901 	if (r) {
902 		ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
903 		    facts_dma_mask, r);
904 		return r;
905 	}
906 	mrioc->dma_mask = facts_dma_mask;
907 	return r;
908 }
909 
910 /**
911  * mpi3mr_process_factsdata - Process IOC facts data
912  * @mrioc: Adapter instance reference
913  * @facts_data: Cached IOC facts data
914  *
915  * Convert IOC facts data into cpu endianness and cache it in
916  * the driver .
917  *
918  * Return: Nothing.
919  */
920 static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
921 	struct mpi3_ioc_facts_data *facts_data)
922 {
923 	u32 ioc_config, req_sz, facts_flags;
924 
925 	if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
926 	    (sizeof(*facts_data) / 4)) {
927 		ioc_warn(mrioc,
928 		    "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
929 		    sizeof(*facts_data),
930 		    le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
931 	}
932 
933 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
934 	req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
935 	    MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
936 	if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
937 		ioc_err(mrioc,
938 		    "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
939 		    req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
940 	}
941 
942 	memset(&mrioc->facts, 0, sizeof(mrioc->facts));
943 
944 	facts_flags = le32_to_cpu(facts_data->flags);
945 	mrioc->facts.op_req_sz = req_sz;
946 	mrioc->op_reply_desc_sz = 1 << ((ioc_config &
947 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
948 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
949 
950 	mrioc->facts.ioc_num = facts_data->ioc_number;
951 	mrioc->facts.who_init = facts_data->who_init;
952 	mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
953 	mrioc->facts.personality = (facts_flags &
954 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
955 	mrioc->facts.dma_mask = (facts_flags &
956 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
957 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
958 	mrioc->facts.protocol_flags = facts_data->protocol_flags;
959 	mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
960 	mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_request);
961 	mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
962 	mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
963 	mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
964 	mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
965 	mrioc->facts.max_pds = le16_to_cpu(facts_data->max_pds);
966 	mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
967 	mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
968 	mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_advanced_host_pds);
969 	mrioc->facts.max_raidpds = le16_to_cpu(facts_data->max_raid_pds);
970 	mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
971 	mrioc->facts.max_pcie_switches =
972 	    le16_to_cpu(facts_data->max_pc_ie_switches);
973 	mrioc->facts.max_sasexpanders =
974 	    le16_to_cpu(facts_data->max_sas_expanders);
975 	mrioc->facts.max_sasinitiators =
976 	    le16_to_cpu(facts_data->max_sas_initiators);
977 	mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
978 	mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
979 	mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
980 	mrioc->facts.max_op_req_q =
981 	    le16_to_cpu(facts_data->max_operational_request_queues);
982 	mrioc->facts.max_op_reply_q =
983 	    le16_to_cpu(facts_data->max_operational_reply_queues);
984 	mrioc->facts.ioc_capabilities =
985 	    le32_to_cpu(facts_data->ioc_capabilities);
986 	mrioc->facts.fw_ver.build_num =
987 	    le16_to_cpu(facts_data->fw_version.build_num);
988 	mrioc->facts.fw_ver.cust_id =
989 	    le16_to_cpu(facts_data->fw_version.customer_id);
990 	mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
991 	mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
992 	mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
993 	mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
994 	mrioc->msix_count = min_t(int, mrioc->msix_count,
995 	    mrioc->facts.max_msix_vectors);
996 	mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
997 	mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
998 	mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
999 	mrioc->facts.shutdown_timeout =
1000 	    le16_to_cpu(facts_data->shutdown_timeout);
1001 
1002 	ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
1003 	    mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
1004 	    mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
1005 	ioc_info(mrioc,
1006 	    "maxreqs(%d), mindh(%d) maxPDs(%d) maxvectors(%d) maxperids(%d)\n",
1007 	    mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
1008 	    mrioc->facts.max_pds, mrioc->facts.max_msix_vectors,
1009 	    mrioc->facts.max_perids);
1010 	ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
1011 	    mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
1012 	    mrioc->facts.sge_mod_shift);
1013 	ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
1014 	    mrioc->facts.dma_mask, (facts_flags &
1015 	    MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
1016 
1017 	mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
1018 
1019 	if (reset_devices)
1020 		mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
1021 		    MPI3MR_HOST_IOS_KDUMP);
1022 }
1023 
1024 /**
1025  * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
1026  * @mrioc: Adapter instance reference
1027  *
1028  * Allocate and initialize the reply free buffers, sense
1029  * buffers, reply free queue and sense buffer queue.
1030  *
1031  * Return: 0 on success, non-zero on failures.
1032  */
1033 static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
1034 {
1035 	int retval = 0;
1036 	u32 sz, i;
1037 	dma_addr_t phy_addr;
1038 
1039 	if (mrioc->init_cmds.reply)
1040 		goto post_reply_sbuf;
1041 
1042 	mrioc->init_cmds.reply = kzalloc(mrioc->facts.reply_sz, GFP_KERNEL);
1043 	if (!mrioc->init_cmds.reply)
1044 		goto out_failed;
1045 
1046 	mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
1047 	mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
1048 	mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
1049 	mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
1050 
1051 	/* reply buffer pool, 16 byte align */
1052 	sz = mrioc->num_reply_bufs * mrioc->facts.reply_sz;
1053 	mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
1054 	    &mrioc->pdev->dev, sz, 16, 0);
1055 	if (!mrioc->reply_buf_pool) {
1056 		ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
1057 		goto out_failed;
1058 	}
1059 
1060 	mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
1061 	    &mrioc->reply_buf_dma);
1062 	if (!mrioc->reply_buf)
1063 		goto out_failed;
1064 
1065 	mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
1066 
1067 	/* reply free queue, 8 byte align */
1068 	sz = mrioc->reply_free_qsz * 8;
1069 	mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
1070 	    &mrioc->pdev->dev, sz, 8, 0);
1071 	if (!mrioc->reply_free_q_pool) {
1072 		ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
1073 		goto out_failed;
1074 	}
1075 	mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
1076 	    GFP_KERNEL, &mrioc->reply_free_q_dma);
1077 	if (!mrioc->reply_free_q)
1078 		goto out_failed;
1079 
1080 	/* sense buffer pool,  4 byte align */
1081 	sz = mrioc->num_sense_bufs * MPI3MR_SENSEBUF_SZ;
1082 	mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
1083 	    &mrioc->pdev->dev, sz, 4, 0);
1084 	if (!mrioc->sense_buf_pool) {
1085 		ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
1086 		goto out_failed;
1087 	}
1088 	mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
1089 	    &mrioc->sense_buf_dma);
1090 	if (!mrioc->sense_buf)
1091 		goto out_failed;
1092 
1093 	/* sense buffer queue, 8 byte align */
1094 	sz = mrioc->sense_buf_q_sz * 8;
1095 	mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
1096 	    &mrioc->pdev->dev, sz, 8, 0);
1097 	if (!mrioc->sense_buf_q_pool) {
1098 		ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
1099 		goto out_failed;
1100 	}
1101 	mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
1102 	    GFP_KERNEL, &mrioc->sense_buf_q_dma);
1103 	if (!mrioc->sense_buf_q)
1104 		goto out_failed;
1105 
1106 post_reply_sbuf:
1107 	sz = mrioc->num_reply_bufs * mrioc->facts.reply_sz;
1108 	ioc_info(mrioc,
1109 	    "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
1110 	    mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->facts.reply_sz,
1111 	    (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
1112 	sz = mrioc->reply_free_qsz * 8;
1113 	ioc_info(mrioc,
1114 	    "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
1115 	    mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
1116 	    (unsigned long long)mrioc->reply_free_q_dma);
1117 	sz = mrioc->num_sense_bufs * MPI3MR_SENSEBUF_SZ;
1118 	ioc_info(mrioc,
1119 	    "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
1120 	    mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSEBUF_SZ,
1121 	    (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
1122 	sz = mrioc->sense_buf_q_sz * 8;
1123 	ioc_info(mrioc,
1124 	    "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
1125 	    mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
1126 	    (unsigned long long)mrioc->sense_buf_q_dma);
1127 
1128 	/* initialize Reply buffer Queue */
1129 	for (i = 0, phy_addr = mrioc->reply_buf_dma;
1130 	    i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->facts.reply_sz)
1131 		mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
1132 	mrioc->reply_free_q[i] = cpu_to_le64(0);
1133 
1134 	/* initialize Sense Buffer Queue */
1135 	for (i = 0, phy_addr = mrioc->sense_buf_dma;
1136 	    i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSEBUF_SZ)
1137 		mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
1138 	mrioc->sense_buf_q[i] = cpu_to_le64(0);
1139 	return retval;
1140 
1141 out_failed:
1142 	retval = -1;
1143 	return retval;
1144 }
1145 
1146 /**
1147  * mpi3mr_issue_iocinit - Send IOC Init
1148  * @mrioc: Adapter instance reference
1149  *
1150  * Issue IOC Init MPI request through admin queue and wait for
1151  * the completion of it or time out.
1152  *
1153  * Return: 0 on success, non-zero on failures.
1154  */
1155 static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
1156 {
1157 	struct mpi3_ioc_init_request iocinit_req;
1158 	struct mpi3_driver_info_layout *drv_info;
1159 	dma_addr_t data_dma;
1160 	u32 data_len = sizeof(*drv_info);
1161 	int retval = 0;
1162 	ktime_t current_time;
1163 
1164 	drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
1165 	    GFP_KERNEL);
1166 	if (!drv_info) {
1167 		retval = -1;
1168 		goto out;
1169 	}
1170 	drv_info->information_length = cpu_to_le32(data_len);
1171 	strncpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
1172 	strncpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
1173 	drv_info->os_name[sizeof(drv_info->os_name) - 1] = 0;
1174 	strncpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
1175 	drv_info->os_version[sizeof(drv_info->os_version) - 1] = 0;
1176 	strncpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
1177 	strncpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
1178 	strncpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE, sizeof(drv_info->driver_release_date));
1179 	drv_info->driver_capabilities = 0;
1180 	memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
1181 	    sizeof(mrioc->driver_info));
1182 
1183 	memset(&iocinit_req, 0, sizeof(iocinit_req));
1184 	mutex_lock(&mrioc->init_cmds.mutex);
1185 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1186 		retval = -1;
1187 		ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
1188 		mutex_unlock(&mrioc->init_cmds.mutex);
1189 		goto out;
1190 	}
1191 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1192 	mrioc->init_cmds.is_waiting = 1;
1193 	mrioc->init_cmds.callback = NULL;
1194 	iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1195 	iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
1196 	iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
1197 	iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
1198 	iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
1199 	iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
1200 	iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
1201 	iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
1202 	iocinit_req.reply_free_queue_address =
1203 	    cpu_to_le64(mrioc->reply_free_q_dma);
1204 	iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSEBUF_SZ);
1205 	iocinit_req.sense_buffer_free_queue_depth =
1206 	    cpu_to_le16(mrioc->sense_buf_q_sz);
1207 	iocinit_req.sense_buffer_free_queue_address =
1208 	    cpu_to_le64(mrioc->sense_buf_q_dma);
1209 	iocinit_req.driver_information_address = cpu_to_le64(data_dma);
1210 
1211 	current_time = ktime_get_real();
1212 	iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
1213 
1214 	init_completion(&mrioc->init_cmds.done);
1215 	retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
1216 	    sizeof(iocinit_req), 1);
1217 	if (retval) {
1218 		ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
1219 		goto out_unlock;
1220 	}
1221 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1222 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1223 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1224 		mpi3mr_set_diagsave(mrioc);
1225 		mpi3mr_issue_reset(mrioc,
1226 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
1227 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
1228 		mrioc->unrecoverable = 1;
1229 		ioc_err(mrioc, "Issue IOCInit: command timed out\n");
1230 		retval = -1;
1231 		goto out_unlock;
1232 	}
1233 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1234 	    != MPI3_IOCSTATUS_SUCCESS) {
1235 		ioc_err(mrioc,
1236 		    "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1237 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1238 		    mrioc->init_cmds.ioc_loginfo);
1239 		retval = -1;
1240 		goto out_unlock;
1241 	}
1242 
1243 out_unlock:
1244 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1245 	mutex_unlock(&mrioc->init_cmds.mutex);
1246 
1247 out:
1248 	if (drv_info)
1249 		dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
1250 		    data_dma);
1251 
1252 	return retval;
1253 }
1254 
1255 /**
1256  * mpi3mr_alloc_chain_bufs - Allocate chain buffers
1257  * @mrioc: Adapter instance reference
1258  *
1259  * Allocate chain buffers and set a bitmap to indicate free
1260  * chain buffers. Chain buffers are used to pass the SGE
1261  * information along with MPI3 SCSI IO requests for host I/O.
1262  *
1263  * Return: 0 on success, non-zero on failure
1264  */
1265 static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
1266 {
1267 	int retval = 0;
1268 	u32 sz, i;
1269 	u16 num_chains;
1270 
1271 	num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
1272 
1273 	mrioc->chain_buf_count = num_chains;
1274 	sz = sizeof(struct chain_element) * num_chains;
1275 	mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
1276 	if (!mrioc->chain_sgl_list)
1277 		goto out_failed;
1278 
1279 	sz = MPI3MR_PAGE_SIZE_4K;
1280 	mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
1281 	    &mrioc->pdev->dev, sz, 16, 0);
1282 	if (!mrioc->chain_buf_pool) {
1283 		ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
1284 		goto out_failed;
1285 	}
1286 
1287 	for (i = 0; i < num_chains; i++) {
1288 		mrioc->chain_sgl_list[i].addr =
1289 		    dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
1290 		    &mrioc->chain_sgl_list[i].dma_addr);
1291 
1292 		if (!mrioc->chain_sgl_list[i].addr)
1293 			goto out_failed;
1294 	}
1295 	mrioc->chain_bitmap_sz = num_chains / 8;
1296 	if (num_chains % 8)
1297 		mrioc->chain_bitmap_sz++;
1298 	mrioc->chain_bitmap = kzalloc(mrioc->chain_bitmap_sz, GFP_KERNEL);
1299 	if (!mrioc->chain_bitmap)
1300 		goto out_failed;
1301 	return retval;
1302 out_failed:
1303 	retval = -1;
1304 	return retval;
1305 }
1306 
1307 /**
1308  * mpi3mr_cleanup_resources - Free PCI resources
1309  * @mrioc: Adapter instance reference
1310  *
1311  * Unmap PCI device memory and disable PCI device.
1312  *
1313  * Return: 0 on success and non-zero on failure.
1314  */
1315 void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
1316 {
1317 	struct pci_dev *pdev = mrioc->pdev;
1318 
1319 	mpi3mr_cleanup_isr(mrioc);
1320 
1321 	if (mrioc->sysif_regs) {
1322 		iounmap((void __iomem *)mrioc->sysif_regs);
1323 		mrioc->sysif_regs = NULL;
1324 	}
1325 
1326 	if (pci_is_enabled(pdev)) {
1327 		if (mrioc->bars)
1328 			pci_release_selected_regions(pdev, mrioc->bars);
1329 		pci_disable_device(pdev);
1330 	}
1331 }
1332 
1333 /**
1334  * mpi3mr_setup_resources - Enable PCI resources
1335  * @mrioc: Adapter instance reference
1336  *
1337  * Enable PCI device memory, MSI-x registers and set DMA mask.
1338  *
1339  * Return: 0 on success and non-zero on failure.
1340  */
1341 int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
1342 {
1343 	struct pci_dev *pdev = mrioc->pdev;
1344 	u32 memap_sz = 0;
1345 	int i, retval = 0, capb = 0;
1346 	u16 message_control;
1347 	u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
1348 	    (((dma_get_required_mask(&pdev->dev) > DMA_BIT_MASK(32)) &&
1349 	    (sizeof(dma_addr_t) > 4)) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
1350 
1351 	if (pci_enable_device_mem(pdev)) {
1352 		ioc_err(mrioc, "pci_enable_device_mem: failed\n");
1353 		retval = -ENODEV;
1354 		goto out_failed;
1355 	}
1356 
1357 	capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
1358 	if (!capb) {
1359 		ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
1360 		retval = -ENODEV;
1361 		goto out_failed;
1362 	}
1363 	mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1364 
1365 	if (pci_request_selected_regions(pdev, mrioc->bars,
1366 	    mrioc->driver_name)) {
1367 		ioc_err(mrioc, "pci_request_selected_regions: failed\n");
1368 		retval = -ENODEV;
1369 		goto out_failed;
1370 	}
1371 
1372 	for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
1373 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1374 			mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
1375 			memap_sz = pci_resource_len(pdev, i);
1376 			mrioc->sysif_regs =
1377 			    ioremap(mrioc->sysif_regs_phys, memap_sz);
1378 			break;
1379 		}
1380 	}
1381 
1382 	pci_set_master(pdev);
1383 
1384 	retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
1385 	if (retval) {
1386 		if (dma_mask != DMA_BIT_MASK(32)) {
1387 			ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
1388 			dma_mask = DMA_BIT_MASK(32);
1389 			retval = dma_set_mask_and_coherent(&pdev->dev,
1390 			    dma_mask);
1391 		}
1392 		if (retval) {
1393 			mrioc->dma_mask = 0;
1394 			ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
1395 			goto out_failed;
1396 		}
1397 	}
1398 	mrioc->dma_mask = dma_mask;
1399 
1400 	if (!mrioc->sysif_regs) {
1401 		ioc_err(mrioc,
1402 		    "Unable to map adapter memory or resource not found\n");
1403 		retval = -EINVAL;
1404 		goto out_failed;
1405 	}
1406 
1407 	pci_read_config_word(pdev, capb + 2, &message_control);
1408 	mrioc->msix_count = (message_control & 0x3FF) + 1;
1409 
1410 	pci_save_state(pdev);
1411 
1412 	pci_set_drvdata(pdev, mrioc->shost);
1413 
1414 	mpi3mr_ioc_disable_intr(mrioc);
1415 
1416 	ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1417 	    (unsigned long long)mrioc->sysif_regs_phys,
1418 	    mrioc->sysif_regs, memap_sz);
1419 	ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
1420 	    mrioc->msix_count);
1421 	return retval;
1422 
1423 out_failed:
1424 	mpi3mr_cleanup_resources(mrioc);
1425 	return retval;
1426 }
1427 
1428 /**
1429  * mpi3mr_init_ioc - Initialize the controller
1430  * @mrioc: Adapter instance reference
1431  *
1432  * This the controller initialization routine, executed either
1433  * after soft reset or from pci probe callback.
1434  * Setup the required resources, memory map the controller
1435  * registers, create admin and operational reply queue pairs,
1436  * allocate required memory for reply pool, sense buffer pool,
1437  * issue IOC init request to the firmware, unmask the events and
1438  * issue port enable to discover SAS/SATA/NVMe devies and RAID
1439  * volumes.
1440  *
1441  * Return: 0 on success and non-zero on failure.
1442  */
1443 int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc)
1444 {
1445 	int retval = 0;
1446 	enum mpi3mr_iocstate ioc_state;
1447 	u64 base_info;
1448 	u32 timeout;
1449 	u32 ioc_status, ioc_config;
1450 	struct mpi3_ioc_facts_data facts_data;
1451 
1452 	mrioc->change_count = 0;
1453 	mrioc->cpu_count = num_online_cpus();
1454 	retval = mpi3mr_setup_resources(mrioc);
1455 	if (retval) {
1456 		ioc_err(mrioc, "Failed to setup resources:error %d\n",
1457 		    retval);
1458 		goto out_nocleanup;
1459 	}
1460 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1461 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1462 
1463 	ioc_info(mrioc, "SOD status %x configuration %x\n",
1464 	    ioc_status, ioc_config);
1465 
1466 	base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
1467 	ioc_info(mrioc, "SOD base_info %llx\n",	base_info);
1468 
1469 	/*The timeout value is in 2sec unit, changing it to seconds*/
1470 	mrioc->ready_timeout =
1471 	    ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
1472 	    MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
1473 
1474 	ioc_info(mrioc, "IOC ready timeout %d\n", mrioc->ready_timeout);
1475 
1476 	ioc_state = mpi3mr_get_iocstate(mrioc);
1477 	ioc_info(mrioc, "IOC in %s state during detection\n",
1478 	    mpi3mr_iocstate_name(ioc_state));
1479 
1480 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
1481 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
1482 		timeout = mrioc->ready_timeout * 10;
1483 		do {
1484 			msleep(100);
1485 		} while (--timeout);
1486 
1487 		ioc_state = mpi3mr_get_iocstate(mrioc);
1488 		ioc_info(mrioc,
1489 		    "IOC in %s state after waiting for reset time\n",
1490 		    mpi3mr_iocstate_name(ioc_state));
1491 	}
1492 
1493 	if (ioc_state == MRIOC_STATE_READY) {
1494 		retval = mpi3mr_issue_and_process_mur(mrioc,
1495 		    MPI3MR_RESET_FROM_BRINGUP);
1496 		if (retval) {
1497 			ioc_err(mrioc, "Failed to MU reset IOC error %d\n",
1498 			    retval);
1499 		}
1500 		ioc_state = mpi3mr_get_iocstate(mrioc);
1501 	}
1502 	if (ioc_state != MRIOC_STATE_RESET) {
1503 		mpi3mr_print_fault_info(mrioc);
1504 		retval = mpi3mr_issue_reset(mrioc,
1505 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
1506 		    MPI3MR_RESET_FROM_BRINGUP);
1507 		if (retval) {
1508 			ioc_err(mrioc,
1509 			    "%s :Failed to soft reset IOC error %d\n",
1510 			    __func__, retval);
1511 			goto out_failed;
1512 		}
1513 	}
1514 	ioc_state = mpi3mr_get_iocstate(mrioc);
1515 	if (ioc_state != MRIOC_STATE_RESET) {
1516 		ioc_err(mrioc, "Cannot bring IOC to reset state\n");
1517 		goto out_failed;
1518 	}
1519 
1520 	retval = mpi3mr_setup_admin_qpair(mrioc);
1521 	if (retval) {
1522 		ioc_err(mrioc, "Failed to setup admin Qs: error %d\n",
1523 		    retval);
1524 		goto out_failed;
1525 	}
1526 
1527 	retval = mpi3mr_bring_ioc_ready(mrioc);
1528 	if (retval) {
1529 		ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
1530 		    retval);
1531 		goto out_failed;
1532 	}
1533 
1534 	retval = mpi3mr_setup_isr(mrioc, 1);
1535 	if (retval) {
1536 		ioc_err(mrioc, "Failed to setup ISR error %d\n",
1537 		    retval);
1538 		goto out_failed;
1539 	}
1540 
1541 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
1542 	if (retval) {
1543 		ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
1544 		    retval);
1545 		goto out_failed;
1546 	}
1547 
1548 	mpi3mr_process_factsdata(mrioc, &facts_data);
1549 	retval = mpi3mr_check_reset_dma_mask(mrioc);
1550 	if (retval) {
1551 		ioc_err(mrioc, "Resetting dma mask failed %d\n",
1552 		    retval);
1553 		goto out_failed;
1554 	}
1555 
1556 	retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
1557 	if (retval) {
1558 		ioc_err(mrioc,
1559 		    "%s :Failed to allocated reply sense buffers %d\n",
1560 		    __func__, retval);
1561 		goto out_failed;
1562 	}
1563 
1564 	retval = mpi3mr_alloc_chain_bufs(mrioc);
1565 	if (retval) {
1566 		ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
1567 		    retval);
1568 		goto out_failed;
1569 	}
1570 
1571 	retval = mpi3mr_issue_iocinit(mrioc);
1572 	if (retval) {
1573 		ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
1574 		    retval);
1575 		goto out_failed;
1576 	}
1577 	mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
1578 	writel(mrioc->reply_free_queue_host_index,
1579 	    &mrioc->sysif_regs->reply_free_host_index);
1580 
1581 	mrioc->sbq_host_index = mrioc->num_sense_bufs;
1582 	writel(mrioc->sbq_host_index,
1583 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
1584 
1585 	retval = mpi3mr_setup_isr(mrioc, 0);
1586 	if (retval) {
1587 		ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
1588 		    retval);
1589 		goto out_failed;
1590 	}
1591 
1592 	return retval;
1593 
1594 out_failed:
1595 	mpi3mr_cleanup_ioc(mrioc);
1596 out_nocleanup:
1597 	return retval;
1598 }
1599 
1600 /**
1601  * mpi3mr_free_mem - Free memory allocated for a controller
1602  * @mrioc: Adapter instance reference
1603  *
1604  * Free all the memory allocated for a controller.
1605  *
1606  * Return: Nothing.
1607  */
1608 static void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
1609 {
1610 	u16 i;
1611 	struct mpi3mr_intr_info *intr_info;
1612 
1613 	if (mrioc->sense_buf_pool) {
1614 		if (mrioc->sense_buf)
1615 			dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
1616 			    mrioc->sense_buf_dma);
1617 		dma_pool_destroy(mrioc->sense_buf_pool);
1618 		mrioc->sense_buf = NULL;
1619 		mrioc->sense_buf_pool = NULL;
1620 	}
1621 	if (mrioc->sense_buf_q_pool) {
1622 		if (mrioc->sense_buf_q)
1623 			dma_pool_free(mrioc->sense_buf_q_pool,
1624 			    mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
1625 		dma_pool_destroy(mrioc->sense_buf_q_pool);
1626 		mrioc->sense_buf_q = NULL;
1627 		mrioc->sense_buf_q_pool = NULL;
1628 	}
1629 
1630 	if (mrioc->reply_buf_pool) {
1631 		if (mrioc->reply_buf)
1632 			dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
1633 			    mrioc->reply_buf_dma);
1634 		dma_pool_destroy(mrioc->reply_buf_pool);
1635 		mrioc->reply_buf = NULL;
1636 		mrioc->reply_buf_pool = NULL;
1637 	}
1638 	if (mrioc->reply_free_q_pool) {
1639 		if (mrioc->reply_free_q)
1640 			dma_pool_free(mrioc->reply_free_q_pool,
1641 			    mrioc->reply_free_q, mrioc->reply_free_q_dma);
1642 		dma_pool_destroy(mrioc->reply_free_q_pool);
1643 		mrioc->reply_free_q = NULL;
1644 		mrioc->reply_free_q_pool = NULL;
1645 	}
1646 
1647 	for (i = 0; i < mrioc->intr_info_count; i++) {
1648 		intr_info = mrioc->intr_info + i;
1649 		if (intr_info)
1650 			intr_info->op_reply_q = NULL;
1651 	}
1652 
1653 	kfree(mrioc->req_qinfo);
1654 	mrioc->req_qinfo = NULL;
1655 	mrioc->num_op_req_q = 0;
1656 
1657 	kfree(mrioc->op_reply_qinfo);
1658 	mrioc->op_reply_qinfo = NULL;
1659 	mrioc->num_op_reply_q = 0;
1660 
1661 	kfree(mrioc->init_cmds.reply);
1662 	mrioc->init_cmds.reply = NULL;
1663 
1664 	kfree(mrioc->chain_bitmap);
1665 	mrioc->chain_bitmap = NULL;
1666 
1667 	if (mrioc->chain_buf_pool) {
1668 		for (i = 0; i < mrioc->chain_buf_count; i++) {
1669 			if (mrioc->chain_sgl_list[i].addr) {
1670 				dma_pool_free(mrioc->chain_buf_pool,
1671 				    mrioc->chain_sgl_list[i].addr,
1672 				    mrioc->chain_sgl_list[i].dma_addr);
1673 				mrioc->chain_sgl_list[i].addr = NULL;
1674 			}
1675 		}
1676 		dma_pool_destroy(mrioc->chain_buf_pool);
1677 		mrioc->chain_buf_pool = NULL;
1678 	}
1679 
1680 	kfree(mrioc->chain_sgl_list);
1681 	mrioc->chain_sgl_list = NULL;
1682 
1683 	if (mrioc->admin_reply_base) {
1684 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
1685 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
1686 		mrioc->admin_reply_base = NULL;
1687 	}
1688 	if (mrioc->admin_req_base) {
1689 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
1690 		    mrioc->admin_req_base, mrioc->admin_req_dma);
1691 		mrioc->admin_req_base = NULL;
1692 	}
1693 }
1694 
1695 /**
1696  * mpi3mr_issue_ioc_shutdown - shutdown controller
1697  * @mrioc: Adapter instance reference
1698  *
1699  * Send shutodwn notification to the controller and wait for the
1700  * shutdown_timeout for it to be completed.
1701  *
1702  * Return: Nothing.
1703  */
1704 static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
1705 {
1706 	u32 ioc_config, ioc_status;
1707 	u8 retval = 1;
1708 	u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
1709 
1710 	ioc_info(mrioc, "Issuing shutdown Notification\n");
1711 	if (mrioc->unrecoverable) {
1712 		ioc_warn(mrioc,
1713 		    "IOC is unrecoverable shutdown is not issued\n");
1714 		return;
1715 	}
1716 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1717 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
1718 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
1719 		ioc_info(mrioc, "shutdown already in progress\n");
1720 		return;
1721 	}
1722 
1723 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1724 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
1725 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN;
1726 
1727 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1728 
1729 	if (mrioc->facts.shutdown_timeout)
1730 		timeout = mrioc->facts.shutdown_timeout * 10;
1731 
1732 	do {
1733 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1734 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
1735 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
1736 			retval = 0;
1737 			break;
1738 		}
1739 		msleep(100);
1740 	} while (--timeout);
1741 
1742 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1743 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1744 
1745 	if (retval) {
1746 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
1747 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
1748 			ioc_warn(mrioc,
1749 			    "shutdown still in progress after timeout\n");
1750 	}
1751 
1752 	ioc_info(mrioc,
1753 	    "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
1754 	    (!retval) ? "successful" : "failed", ioc_status,
1755 	    ioc_config);
1756 }
1757 
1758 /**
1759  * mpi3mr_cleanup_ioc - Cleanup controller
1760  * @mrioc: Adapter instance reference
1761  *
1762  * controller cleanup handler, Message unit reset or soft reset
1763  * and shutdown notification is issued to the controller and the
1764  * associated memory resources are freed.
1765  *
1766  * Return: Nothing.
1767  */
1768 void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)
1769 {
1770 	enum mpi3mr_iocstate ioc_state;
1771 
1772 	mpi3mr_ioc_disable_intr(mrioc);
1773 
1774 	ioc_state = mpi3mr_get_iocstate(mrioc);
1775 
1776 	if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
1777 	    (ioc_state == MRIOC_STATE_READY)) {
1778 		if (mpi3mr_issue_and_process_mur(mrioc,
1779 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
1780 			mpi3mr_issue_reset(mrioc,
1781 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
1782 			    MPI3MR_RESET_FROM_MUR_FAILURE);
1783 
1784 		 mpi3mr_issue_ioc_shutdown(mrioc);
1785 	}
1786 
1787 	mpi3mr_free_mem(mrioc);
1788 	mpi3mr_cleanup_resources(mrioc);
1789 }
1790 
1791 /**
1792  * mpi3mr_soft_reset_handler - Reset the controller
1793  * @mrioc: Adapter instance reference
1794  * @reset_reason: Reset reason code
1795  * @snapdump: Flag to generate snapdump in firmware or not
1796  *
1797  * TBD
1798  *
1799  * Return: 0 on success, non-zero on failure.
1800  */
1801 int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
1802 	u32 reset_reason, u8 snapdump)
1803 {
1804 	return 0;
1805 }
1806