xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_fw.c (revision a6856cc4)
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 inline bool
29 mpi3mr_check_req_qfull(struct op_req_qinfo *op_req_q)
30 {
31 	u16 pi, ci, max_entries;
32 	bool is_qfull = false;
33 
34 	pi = op_req_q->pi;
35 	ci = READ_ONCE(op_req_q->ci);
36 	max_entries = op_req_q->num_requests;
37 
38 	if ((ci == (pi + 1)) || ((!ci) && (pi == (max_entries - 1))))
39 		is_qfull = true;
40 
41 	return is_qfull;
42 }
43 
44 static void mpi3mr_sync_irqs(struct mpi3mr_ioc *mrioc)
45 {
46 	u16 i, max_vectors;
47 
48 	max_vectors = mrioc->intr_info_count;
49 
50 	for (i = 0; i < max_vectors; i++)
51 		synchronize_irq(pci_irq_vector(mrioc->pdev, i));
52 }
53 
54 void mpi3mr_ioc_disable_intr(struct mpi3mr_ioc *mrioc)
55 {
56 	mrioc->intr_enabled = 0;
57 	mpi3mr_sync_irqs(mrioc);
58 }
59 
60 void mpi3mr_ioc_enable_intr(struct mpi3mr_ioc *mrioc)
61 {
62 	mrioc->intr_enabled = 1;
63 }
64 
65 static void mpi3mr_cleanup_isr(struct mpi3mr_ioc *mrioc)
66 {
67 	u16 i;
68 
69 	mpi3mr_ioc_disable_intr(mrioc);
70 
71 	if (!mrioc->intr_info)
72 		return;
73 
74 	for (i = 0; i < mrioc->intr_info_count; i++)
75 		free_irq(pci_irq_vector(mrioc->pdev, i),
76 		    (mrioc->intr_info + i));
77 
78 	kfree(mrioc->intr_info);
79 	mrioc->intr_info = NULL;
80 	mrioc->intr_info_count = 0;
81 	pci_free_irq_vectors(mrioc->pdev);
82 }
83 
84 void mpi3mr_add_sg_single(void *paddr, u8 flags, u32 length,
85 	dma_addr_t dma_addr)
86 {
87 	struct mpi3_sge_common *sgel = paddr;
88 
89 	sgel->flags = flags;
90 	sgel->length = cpu_to_le32(length);
91 	sgel->address = cpu_to_le64(dma_addr);
92 }
93 
94 void mpi3mr_build_zero_len_sge(void *paddr)
95 {
96 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
97 
98 	mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1);
99 }
100 
101 void *mpi3mr_get_reply_virt_addr(struct mpi3mr_ioc *mrioc,
102 	dma_addr_t phys_addr)
103 {
104 	if (!phys_addr)
105 		return NULL;
106 
107 	if ((phys_addr < mrioc->reply_buf_dma) ||
108 	    (phys_addr > mrioc->reply_buf_dma_max_address))
109 		return NULL;
110 
111 	return mrioc->reply_buf + (phys_addr - mrioc->reply_buf_dma);
112 }
113 
114 void *mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_ioc *mrioc,
115 	dma_addr_t phys_addr)
116 {
117 	if (!phys_addr)
118 		return NULL;
119 
120 	return mrioc->sense_buf + (phys_addr - mrioc->sense_buf_dma);
121 }
122 
123 static void mpi3mr_repost_reply_buf(struct mpi3mr_ioc *mrioc,
124 	u64 reply_dma)
125 {
126 	u32 old_idx = 0;
127 	unsigned long flags;
128 
129 	spin_lock_irqsave(&mrioc->reply_free_queue_lock, flags);
130 	old_idx  =  mrioc->reply_free_queue_host_index;
131 	mrioc->reply_free_queue_host_index = (
132 	    (mrioc->reply_free_queue_host_index ==
133 	    (mrioc->reply_free_qsz - 1)) ? 0 :
134 	    (mrioc->reply_free_queue_host_index + 1));
135 	mrioc->reply_free_q[old_idx] = cpu_to_le64(reply_dma);
136 	writel(mrioc->reply_free_queue_host_index,
137 	    &mrioc->sysif_regs->reply_free_host_index);
138 	spin_unlock_irqrestore(&mrioc->reply_free_queue_lock, flags);
139 }
140 
141 void mpi3mr_repost_sense_buf(struct mpi3mr_ioc *mrioc,
142 	u64 sense_buf_dma)
143 {
144 	u32 old_idx = 0;
145 	unsigned long flags;
146 
147 	spin_lock_irqsave(&mrioc->sbq_lock, flags);
148 	old_idx  =  mrioc->sbq_host_index;
149 	mrioc->sbq_host_index = ((mrioc->sbq_host_index ==
150 	    (mrioc->sense_buf_q_sz - 1)) ? 0 :
151 	    (mrioc->sbq_host_index + 1));
152 	mrioc->sense_buf_q[old_idx] = cpu_to_le64(sense_buf_dma);
153 	writel(mrioc->sbq_host_index,
154 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
155 	spin_unlock_irqrestore(&mrioc->sbq_lock, flags);
156 }
157 
158 static void mpi3mr_print_event_data(struct mpi3mr_ioc *mrioc,
159 	struct mpi3_event_notification_reply *event_reply)
160 {
161 	char *desc = NULL;
162 	u16 event;
163 
164 	event = event_reply->event;
165 
166 	switch (event) {
167 	case MPI3_EVENT_LOG_DATA:
168 		desc = "Log Data";
169 		break;
170 	case MPI3_EVENT_CHANGE:
171 		desc = "Event Change";
172 		break;
173 	case MPI3_EVENT_GPIO_INTERRUPT:
174 		desc = "GPIO Interrupt";
175 		break;
176 	case MPI3_EVENT_TEMP_THRESHOLD:
177 		desc = "Temperature Threshold";
178 		break;
179 	case MPI3_EVENT_CABLE_MGMT:
180 		desc = "Cable Management";
181 		break;
182 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
183 		desc = "Energy Pack Change";
184 		break;
185 	case MPI3_EVENT_DEVICE_ADDED:
186 	{
187 		struct mpi3_device_page0 *event_data =
188 		    (struct mpi3_device_page0 *)event_reply->event_data;
189 		ioc_info(mrioc, "Device Added: dev=0x%04x Form=0x%x\n",
190 		    event_data->dev_handle, event_data->device_form);
191 		return;
192 	}
193 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
194 	{
195 		struct mpi3_device_page0 *event_data =
196 		    (struct mpi3_device_page0 *)event_reply->event_data;
197 		ioc_info(mrioc, "Device Info Changed: dev=0x%04x Form=0x%x\n",
198 		    event_data->dev_handle, event_data->device_form);
199 		return;
200 	}
201 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
202 	{
203 		struct mpi3_event_data_device_status_change *event_data =
204 		    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
205 		ioc_info(mrioc, "Device status Change: dev=0x%04x RC=0x%x\n",
206 		    event_data->dev_handle, event_data->reason_code);
207 		return;
208 	}
209 	case MPI3_EVENT_SAS_DISCOVERY:
210 	{
211 		struct mpi3_event_data_sas_discovery *event_data =
212 		    (struct mpi3_event_data_sas_discovery *)event_reply->event_data;
213 		ioc_info(mrioc, "SAS Discovery: (%s) status (0x%08x)\n",
214 		    (event_data->reason_code == MPI3_EVENT_SAS_DISC_RC_STARTED) ?
215 		    "start" : "stop",
216 		    le32_to_cpu(event_data->discovery_status));
217 		return;
218 	}
219 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
220 		desc = "SAS Broadcast Primitive";
221 		break;
222 	case MPI3_EVENT_SAS_NOTIFY_PRIMITIVE:
223 		desc = "SAS Notify Primitive";
224 		break;
225 	case MPI3_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
226 		desc = "SAS Init Device Status Change";
227 		break;
228 	case MPI3_EVENT_SAS_INIT_TABLE_OVERFLOW:
229 		desc = "SAS Init Table Overflow";
230 		break;
231 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
232 		desc = "SAS Topology Change List";
233 		break;
234 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
235 		desc = "Enclosure Device Status Change";
236 		break;
237 	case MPI3_EVENT_HARD_RESET_RECEIVED:
238 		desc = "Hard Reset Received";
239 		break;
240 	case MPI3_EVENT_SAS_PHY_COUNTER:
241 		desc = "SAS PHY Counter";
242 		break;
243 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
244 		desc = "SAS Device Discovery Error";
245 		break;
246 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
247 		desc = "PCIE Topology Change List";
248 		break;
249 	case MPI3_EVENT_PCIE_ENUMERATION:
250 	{
251 		struct mpi3_event_data_pcie_enumeration *event_data =
252 		    (struct mpi3_event_data_pcie_enumeration *)event_reply->event_data;
253 		ioc_info(mrioc, "PCIE Enumeration: (%s)",
254 		    (event_data->reason_code ==
255 		    MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" : "stop");
256 		if (event_data->enumeration_status)
257 			ioc_info(mrioc, "enumeration_status(0x%08x)\n",
258 			    le32_to_cpu(event_data->enumeration_status));
259 		return;
260 	}
261 	case MPI3_EVENT_PREPARE_FOR_RESET:
262 		desc = "Prepare For Reset";
263 		break;
264 	}
265 
266 	if (!desc)
267 		return;
268 
269 	ioc_info(mrioc, "%s\n", desc);
270 }
271 
272 static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc,
273 	struct mpi3_default_reply *def_reply)
274 {
275 	struct mpi3_event_notification_reply *event_reply =
276 	    (struct mpi3_event_notification_reply *)def_reply;
277 
278 	mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count);
279 	mpi3mr_print_event_data(mrioc, event_reply);
280 	mpi3mr_os_handle_events(mrioc, event_reply);
281 }
282 
283 static struct mpi3mr_drv_cmd *
284 mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag,
285 	struct mpi3_default_reply *def_reply)
286 {
287 	u16 idx;
288 
289 	switch (host_tag) {
290 	case MPI3MR_HOSTTAG_INITCMDS:
291 		return &mrioc->init_cmds;
292 	case MPI3MR_HOSTTAG_BLK_TMS:
293 		return &mrioc->host_tm_cmds;
294 	case MPI3MR_HOSTTAG_INVALID:
295 		if (def_reply && def_reply->function ==
296 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
297 			mpi3mr_handle_events(mrioc, def_reply);
298 		return NULL;
299 	default:
300 		break;
301 	}
302 	if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
303 	    host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) {
304 		idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
305 		return &mrioc->dev_rmhs_cmds[idx];
306 	}
307 
308 	return NULL;
309 }
310 
311 static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
312 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
313 {
314 	u16 reply_desc_type, host_tag = 0;
315 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
316 	u32 ioc_loginfo = 0;
317 	struct mpi3_status_reply_descriptor *status_desc;
318 	struct mpi3_address_reply_descriptor *addr_desc;
319 	struct mpi3_success_reply_descriptor *success_desc;
320 	struct mpi3_default_reply *def_reply = NULL;
321 	struct mpi3mr_drv_cmd *cmdptr = NULL;
322 	struct mpi3_scsi_io_reply *scsi_reply;
323 	u8 *sense_buf = NULL;
324 
325 	*reply_dma = 0;
326 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
327 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
328 	switch (reply_desc_type) {
329 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
330 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
331 		host_tag = le16_to_cpu(status_desc->host_tag);
332 		ioc_status = le16_to_cpu(status_desc->ioc_status);
333 		if (ioc_status &
334 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
335 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
336 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
337 		break;
338 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
339 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
340 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
341 		def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
342 		if (!def_reply)
343 			goto out;
344 		host_tag = le16_to_cpu(def_reply->host_tag);
345 		ioc_status = le16_to_cpu(def_reply->ioc_status);
346 		if (ioc_status &
347 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
348 			ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
349 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
350 		if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
351 			scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
352 			sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
353 			    le64_to_cpu(scsi_reply->sense_data_buffer_address));
354 		}
355 		break;
356 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
357 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
358 		host_tag = le16_to_cpu(success_desc->host_tag);
359 		break;
360 	default:
361 		break;
362 	}
363 
364 	cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
365 	if (cmdptr) {
366 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
367 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
368 			cmdptr->ioc_loginfo = ioc_loginfo;
369 			cmdptr->ioc_status = ioc_status;
370 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
371 			if (def_reply) {
372 				cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
373 				memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
374 				    mrioc->facts.reply_sz);
375 			}
376 			if (cmdptr->is_waiting) {
377 				complete(&cmdptr->done);
378 				cmdptr->is_waiting = 0;
379 			} else if (cmdptr->callback)
380 				cmdptr->callback(mrioc, cmdptr);
381 		}
382 	}
383 out:
384 	if (sense_buf)
385 		mpi3mr_repost_sense_buf(mrioc,
386 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
387 }
388 
389 static int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
390 {
391 	u32 exp_phase = mrioc->admin_reply_ephase;
392 	u32 admin_reply_ci = mrioc->admin_reply_ci;
393 	u32 num_admin_replies = 0;
394 	u64 reply_dma = 0;
395 	struct mpi3_default_reply_descriptor *reply_desc;
396 
397 	reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
398 	    admin_reply_ci;
399 
400 	if ((le16_to_cpu(reply_desc->reply_flags) &
401 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
402 		return 0;
403 
404 	do {
405 		mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
406 		mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
407 		if (reply_dma)
408 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
409 		num_admin_replies++;
410 		if (++admin_reply_ci == mrioc->num_admin_replies) {
411 			admin_reply_ci = 0;
412 			exp_phase ^= 1;
413 		}
414 		reply_desc =
415 		    (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
416 		    admin_reply_ci;
417 		if ((le16_to_cpu(reply_desc->reply_flags) &
418 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
419 			break;
420 	} while (1);
421 
422 	writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
423 	mrioc->admin_reply_ci = admin_reply_ci;
424 	mrioc->admin_reply_ephase = exp_phase;
425 
426 	return num_admin_replies;
427 }
428 
429 /**
430  * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to
431  *	queue's consumer index from operational reply descriptor queue.
432  * @op_reply_q: op_reply_qinfo object
433  * @reply_ci: operational reply descriptor's queue consumer index
434  *
435  * Returns reply descriptor frame address
436  */
437 static inline struct mpi3_default_reply_descriptor *
438 mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci)
439 {
440 	void *segment_base_addr;
441 	struct segments *segments = op_reply_q->q_segments;
442 	struct mpi3_default_reply_descriptor *reply_desc = NULL;
443 
444 	segment_base_addr =
445 	    segments[reply_ci / op_reply_q->segment_qd].segment;
446 	reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr +
447 	    (reply_ci % op_reply_q->segment_qd);
448 	return reply_desc;
449 }
450 
451 static int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc,
452 	struct mpi3mr_intr_info *intr_info)
453 {
454 	struct op_reply_qinfo *op_reply_q = intr_info->op_reply_q;
455 	struct op_req_qinfo *op_req_q;
456 	u32 exp_phase;
457 	u32 reply_ci;
458 	u32 num_op_reply = 0;
459 	u64 reply_dma = 0;
460 	struct mpi3_default_reply_descriptor *reply_desc;
461 	u16 req_q_idx = 0, reply_qidx;
462 
463 	reply_qidx = op_reply_q->qid - 1;
464 
465 	if (!atomic_add_unless(&op_reply_q->in_use, 1, 1))
466 		return 0;
467 
468 	exp_phase = op_reply_q->ephase;
469 	reply_ci = op_reply_q->ci;
470 
471 	reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
472 	if ((le16_to_cpu(reply_desc->reply_flags) &
473 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
474 		atomic_dec(&op_reply_q->in_use);
475 		return 0;
476 	}
477 
478 	do {
479 		req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1;
480 		op_req_q = &mrioc->req_qinfo[req_q_idx];
481 
482 		WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci));
483 		mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma,
484 		    reply_qidx);
485 		atomic_dec(&op_reply_q->pend_ios);
486 		if (reply_dma)
487 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
488 		num_op_reply++;
489 
490 		if (++reply_ci == op_reply_q->num_replies) {
491 			reply_ci = 0;
492 			exp_phase ^= 1;
493 		}
494 
495 		reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
496 
497 		if ((le16_to_cpu(reply_desc->reply_flags) &
498 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
499 			break;
500 		/*
501 		 * Exit completion loop to avoid CPU lockup
502 		 * Ensure remaining completion happens from threaded ISR.
503 		 */
504 		if (num_op_reply > mrioc->max_host_ios) {
505 			intr_info->op_reply_q->enable_irq_poll = true;
506 			break;
507 		}
508 
509 	} while (1);
510 
511 	writel(reply_ci,
512 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index);
513 	op_reply_q->ci = reply_ci;
514 	op_reply_q->ephase = exp_phase;
515 
516 	atomic_dec(&op_reply_q->in_use);
517 	return num_op_reply;
518 }
519 
520 static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
521 {
522 	struct mpi3mr_intr_info *intr_info = privdata;
523 	struct mpi3mr_ioc *mrioc;
524 	u16 midx;
525 	u32 num_admin_replies = 0, num_op_reply = 0;
526 
527 	if (!intr_info)
528 		return IRQ_NONE;
529 
530 	mrioc = intr_info->mrioc;
531 
532 	if (!mrioc->intr_enabled)
533 		return IRQ_NONE;
534 
535 	midx = intr_info->msix_index;
536 
537 	if (!midx)
538 		num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
539 	if (intr_info->op_reply_q)
540 		num_op_reply = mpi3mr_process_op_reply_q(mrioc, intr_info);
541 
542 	if (num_admin_replies || num_op_reply)
543 		return IRQ_HANDLED;
544 	else
545 		return IRQ_NONE;
546 }
547 
548 static irqreturn_t mpi3mr_isr(int irq, void *privdata)
549 {
550 	struct mpi3mr_intr_info *intr_info = privdata;
551 	struct mpi3mr_ioc *mrioc;
552 	u16 midx;
553 	int ret;
554 
555 	if (!intr_info)
556 		return IRQ_NONE;
557 
558 	mrioc = intr_info->mrioc;
559 	midx = intr_info->msix_index;
560 	/* Call primary ISR routine */
561 	ret = mpi3mr_isr_primary(irq, privdata);
562 
563 	/*
564 	 * If more IOs are expected, schedule IRQ polling thread.
565 	 * Otherwise exit from ISR.
566 	 */
567 	if (!intr_info->op_reply_q)
568 		return ret;
569 
570 	if (!intr_info->op_reply_q->enable_irq_poll ||
571 	    !atomic_read(&intr_info->op_reply_q->pend_ios))
572 		return ret;
573 
574 	disable_irq_nosync(pci_irq_vector(mrioc->pdev, midx));
575 
576 	return IRQ_WAKE_THREAD;
577 }
578 
579 /**
580  * mpi3mr_isr_poll - Reply queue polling routine
581  * @irq: IRQ
582  * @privdata: Interrupt info
583  *
584  * poll for pending I/O completions in a loop until pending I/Os
585  * present or controller queue depth I/Os are processed.
586  *
587  * Return: IRQ_NONE or IRQ_HANDLED
588  */
589 static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
590 {
591 	struct mpi3mr_intr_info *intr_info = privdata;
592 	struct mpi3mr_ioc *mrioc;
593 	u16 midx;
594 	u32 num_op_reply = 0;
595 
596 	if (!intr_info || !intr_info->op_reply_q)
597 		return IRQ_NONE;
598 
599 	mrioc = intr_info->mrioc;
600 	midx = intr_info->msix_index;
601 
602 	/* Poll for pending IOs completions */
603 	do {
604 		if (!mrioc->intr_enabled)
605 			break;
606 
607 		if (!midx)
608 			mpi3mr_process_admin_reply_q(mrioc);
609 		if (intr_info->op_reply_q)
610 			num_op_reply +=
611 			    mpi3mr_process_op_reply_q(mrioc, intr_info);
612 
613 		usleep_range(mrioc->irqpoll_sleep, 10 * mrioc->irqpoll_sleep);
614 
615 	} while (atomic_read(&intr_info->op_reply_q->pend_ios) &&
616 	    (num_op_reply < mrioc->max_host_ios));
617 
618 	intr_info->op_reply_q->enable_irq_poll = false;
619 	enable_irq(pci_irq_vector(mrioc->pdev, midx));
620 
621 	return IRQ_HANDLED;
622 }
623 
624 /**
625  * mpi3mr_request_irq - Request IRQ and register ISR
626  * @mrioc: Adapter instance reference
627  * @index: IRQ vector index
628  *
629  * Request threaded ISR with primary ISR and secondary
630  *
631  * Return: 0 on success and non zero on failures.
632  */
633 static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
634 {
635 	struct pci_dev *pdev = mrioc->pdev;
636 	struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
637 	int retval = 0;
638 
639 	intr_info->mrioc = mrioc;
640 	intr_info->msix_index = index;
641 	intr_info->op_reply_q = NULL;
642 
643 	snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
644 	    mrioc->driver_name, mrioc->id, index);
645 
646 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
647 	    mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
648 	if (retval) {
649 		ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
650 		    intr_info->name, pci_irq_vector(pdev, index));
651 		return retval;
652 	}
653 
654 	return retval;
655 }
656 
657 /**
658  * mpi3mr_setup_isr - Setup ISR for the controller
659  * @mrioc: Adapter instance reference
660  * @setup_one: Request one IRQ or more
661  *
662  * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
663  *
664  * Return: 0 on success and non zero on failures.
665  */
666 static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
667 {
668 	unsigned int irq_flags = PCI_IRQ_MSIX;
669 	int max_vectors;
670 	int retval;
671 	int i;
672 	struct irq_affinity desc = { .pre_vectors =  1};
673 
674 	mpi3mr_cleanup_isr(mrioc);
675 
676 	if (setup_one || reset_devices)
677 		max_vectors = 1;
678 	else {
679 		max_vectors =
680 		    min_t(int, mrioc->cpu_count + 1, mrioc->msix_count);
681 
682 		ioc_info(mrioc,
683 		    "MSI-X vectors supported: %d, no of cores: %d,",
684 		    mrioc->msix_count, mrioc->cpu_count);
685 		ioc_info(mrioc,
686 		    "MSI-x vectors requested: %d\n", max_vectors);
687 	}
688 
689 	irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
690 
691 	mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0;
692 	retval = pci_alloc_irq_vectors_affinity(mrioc->pdev,
693 				1, max_vectors, irq_flags, &desc);
694 	if (retval < 0) {
695 		ioc_err(mrioc, "Cannot alloc irq vectors\n");
696 		goto out_failed;
697 	}
698 	if (retval != max_vectors) {
699 		ioc_info(mrioc,
700 		    "allocated vectors (%d) are less than configured (%d)\n",
701 		    retval, max_vectors);
702 		/*
703 		 * If only one MSI-x is allocated, then MSI-x 0 will be shared
704 		 * between Admin queue and operational queue
705 		 */
706 		if (retval == 1)
707 			mrioc->op_reply_q_offset = 0;
708 
709 		max_vectors = retval;
710 	}
711 	mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
712 	    GFP_KERNEL);
713 	if (!mrioc->intr_info) {
714 		retval = -ENOMEM;
715 		pci_free_irq_vectors(mrioc->pdev);
716 		goto out_failed;
717 	}
718 	for (i = 0; i < max_vectors; i++) {
719 		retval = mpi3mr_request_irq(mrioc, i);
720 		if (retval) {
721 			mrioc->intr_info_count = i;
722 			goto out_failed;
723 		}
724 	}
725 	mrioc->intr_info_count = max_vectors;
726 	mpi3mr_ioc_enable_intr(mrioc);
727 	return 0;
728 
729 out_failed:
730 	mpi3mr_cleanup_isr(mrioc);
731 
732 	return retval;
733 }
734 
735 static const struct {
736 	enum mpi3mr_iocstate value;
737 	char *name;
738 } mrioc_states[] = {
739 	{ MRIOC_STATE_READY, "ready" },
740 	{ MRIOC_STATE_FAULT, "fault" },
741 	{ MRIOC_STATE_RESET, "reset" },
742 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
743 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
744 	{ MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
745 };
746 
747 static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
748 {
749 	int i;
750 	char *name = NULL;
751 
752 	for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
753 		if (mrioc_states[i].value == mrioc_state) {
754 			name = mrioc_states[i].name;
755 			break;
756 		}
757 	}
758 	return name;
759 }
760 
761 /* Reset reason to name mapper structure*/
762 static const struct {
763 	enum mpi3mr_reset_reason value;
764 	char *name;
765 } mpi3mr_reset_reason_codes[] = {
766 	{ MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
767 	{ MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
768 	{ MPI3MR_RESET_FROM_IOCTL, "application invocation" },
769 	{ MPI3MR_RESET_FROM_EH_HOS, "error handling" },
770 	{ MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
771 	{ MPI3MR_RESET_FROM_IOCTL_TIMEOUT, "IOCTL timeout" },
772 	{ MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
773 	{ MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
774 	{ MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
775 	{ MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
776 	{ MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
777 	{ MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
778 	{ MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
779 	{
780 		MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
781 		"create request queue timeout"
782 	},
783 	{
784 		MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
785 		"create reply queue timeout"
786 	},
787 	{ MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
788 	{ MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
789 	{ MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
790 	{ MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
791 	{
792 		MPI3MR_RESET_FROM_CIACTVRST_TIMER,
793 		"component image activation timeout"
794 	},
795 	{
796 		MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
797 		"get package version timeout"
798 	},
799 	{ MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
800 	{ MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
801 };
802 
803 /**
804  * mpi3mr_reset_rc_name - get reset reason code name
805  * @reason_code: reset reason code value
806  *
807  * Map reset reason to an NULL terminated ASCII string
808  *
809  * Return: name corresponding to reset reason value or NULL.
810  */
811 static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
812 {
813 	int i;
814 	char *name = NULL;
815 
816 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) {
817 		if (mpi3mr_reset_reason_codes[i].value == reason_code) {
818 			name = mpi3mr_reset_reason_codes[i].name;
819 			break;
820 		}
821 	}
822 	return name;
823 }
824 
825 /* Reset type to name mapper structure*/
826 static const struct {
827 	u16 reset_type;
828 	char *name;
829 } mpi3mr_reset_types[] = {
830 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
831 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
832 };
833 
834 /**
835  * mpi3mr_reset_type_name - get reset type name
836  * @reset_type: reset type value
837  *
838  * Map reset type to an NULL terminated ASCII string
839  *
840  * Return: name corresponding to reset type value or NULL.
841  */
842 static const char *mpi3mr_reset_type_name(u16 reset_type)
843 {
844 	int i;
845 	char *name = NULL;
846 
847 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) {
848 		if (mpi3mr_reset_types[i].reset_type == reset_type) {
849 			name = mpi3mr_reset_types[i].name;
850 			break;
851 		}
852 	}
853 	return name;
854 }
855 
856 /**
857  * mpi3mr_print_fault_info - Display fault information
858  * @mrioc: Adapter instance reference
859  *
860  * Display the controller fault information if there is a
861  * controller fault.
862  *
863  * Return: Nothing.
864  */
865 static void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
866 {
867 	u32 ioc_status, code, code1, code2, code3;
868 
869 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
870 
871 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
872 		code = readl(&mrioc->sysif_regs->fault);
873 		code1 = readl(&mrioc->sysif_regs->fault_info[0]);
874 		code2 = readl(&mrioc->sysif_regs->fault_info[1]);
875 		code3 = readl(&mrioc->sysif_regs->fault_info[2]);
876 
877 		ioc_info(mrioc,
878 		    "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
879 		    code, code1, code2, code3);
880 	}
881 }
882 
883 /**
884  * mpi3mr_get_iocstate - Get IOC State
885  * @mrioc: Adapter instance reference
886  *
887  * Return a proper IOC state enum based on the IOC status and
888  * IOC configuration and unrcoverable state of the controller.
889  *
890  * Return: Current IOC state.
891  */
892 enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
893 {
894 	u32 ioc_status, ioc_config;
895 	u8 ready, enabled;
896 
897 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
898 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
899 
900 	if (mrioc->unrecoverable)
901 		return MRIOC_STATE_UNRECOVERABLE;
902 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
903 		return MRIOC_STATE_FAULT;
904 
905 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
906 	enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
907 
908 	if (ready && enabled)
909 		return MRIOC_STATE_READY;
910 	if ((!ready) && (!enabled))
911 		return MRIOC_STATE_RESET;
912 	if ((!ready) && (enabled))
913 		return MRIOC_STATE_BECOMING_READY;
914 
915 	return MRIOC_STATE_RESET_REQUESTED;
916 }
917 
918 /**
919  * mpi3mr_clear_reset_history - clear reset history
920  * @mrioc: Adapter instance reference
921  *
922  * Write the reset history bit in IOC status to clear the bit,
923  * if it is already set.
924  *
925  * Return: Nothing.
926  */
927 static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
928 {
929 	u32 ioc_status;
930 
931 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
932 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
933 		writel(ioc_status, &mrioc->sysif_regs->ioc_status);
934 }
935 
936 /**
937  * mpi3mr_issue_and_process_mur - Message unit Reset handler
938  * @mrioc: Adapter instance reference
939  * @reset_reason: Reset reason code
940  *
941  * Issue Message unit Reset to the controller and wait for it to
942  * be complete.
943  *
944  * Return: 0 on success, -1 on failure.
945  */
946 static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
947 	u32 reset_reason)
948 {
949 	u32 ioc_config, timeout, ioc_status;
950 	int retval = -1;
951 
952 	ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
953 	if (mrioc->unrecoverable) {
954 		ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
955 		return retval;
956 	}
957 	mpi3mr_clear_reset_history(mrioc);
958 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
959 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
960 	ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
961 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
962 
963 	timeout = mrioc->ready_timeout * 10;
964 	do {
965 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
966 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
967 			mpi3mr_clear_reset_history(mrioc);
968 			ioc_config =
969 			    readl(&mrioc->sysif_regs->ioc_configuration);
970 			if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
971 			      (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
972 			    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC))) {
973 				retval = 0;
974 				break;
975 			}
976 		}
977 		msleep(100);
978 	} while (--timeout);
979 
980 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
981 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
982 
983 	ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
984 	    (!retval) ? "successful" : "failed", ioc_status, ioc_config);
985 	return retval;
986 }
987 
988 /**
989  * mpi3mr_bring_ioc_ready - Bring controller to ready state
990  * @mrioc: Adapter instance reference
991  *
992  * Set Enable IOC bit in IOC configuration register and wait for
993  * the controller to become ready.
994  *
995  * Return: 0 on success, -1 on failure.
996  */
997 static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
998 {
999 	u32 ioc_config, timeout;
1000 	enum mpi3mr_iocstate current_state;
1001 
1002 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1003 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1004 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1005 
1006 	timeout = mrioc->ready_timeout * 10;
1007 	do {
1008 		current_state = mpi3mr_get_iocstate(mrioc);
1009 		if (current_state == MRIOC_STATE_READY)
1010 			return 0;
1011 		msleep(100);
1012 	} while (--timeout);
1013 
1014 	return -1;
1015 }
1016 
1017 /**
1018  * mpi3mr_soft_reset_success - Check softreset is success or not
1019  * @ioc_status: IOC status register value
1020  * @ioc_config: IOC config register value
1021  *
1022  * Check whether the soft reset is successful or not based on
1023  * IOC status and IOC config register values.
1024  *
1025  * Return: True when the soft reset is success, false otherwise.
1026  */
1027 static inline bool
1028 mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config)
1029 {
1030 	if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1031 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
1032 	    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1033 		return true;
1034 	return false;
1035 }
1036 
1037 /**
1038  * mpi3mr_diagfault_success - Check diag fault is success or not
1039  * @mrioc: Adapter reference
1040  * @ioc_status: IOC status register value
1041  *
1042  * Check whether the controller hit diag reset fault code.
1043  *
1044  * Return: True when there is diag fault, false otherwise.
1045  */
1046 static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc,
1047 	u32 ioc_status)
1048 {
1049 	u32 fault;
1050 
1051 	if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
1052 		return false;
1053 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
1054 	if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET)
1055 		return true;
1056 	return false;
1057 }
1058 
1059 /**
1060  * mpi3mr_set_diagsave - Set diag save bit for snapdump
1061  * @mrioc: Adapter reference
1062  *
1063  * Set diag save bit in IOC configuration register to enable
1064  * snapdump.
1065  *
1066  * Return: Nothing.
1067  */
1068 static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
1069 {
1070 	u32 ioc_config;
1071 
1072 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1073 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
1074 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1075 }
1076 
1077 /**
1078  * mpi3mr_issue_reset - Issue reset to the controller
1079  * @mrioc: Adapter reference
1080  * @reset_type: Reset type
1081  * @reset_reason: Reset reason code
1082  *
1083  * Unlock the host diagnostic registers and write the specific
1084  * reset type to that, wait for reset acknowledgment from the
1085  * controller, if the reset is not successful retry for the
1086  * predefined number of times.
1087  *
1088  * Return: 0 on success, non-zero on failure.
1089  */
1090 static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
1091 	u32 reset_reason)
1092 {
1093 	int retval = -1;
1094 	u8 unlock_retry_count, reset_retry_count = 0;
1095 	u32 host_diagnostic, timeout, ioc_status, ioc_config;
1096 
1097 	pci_cfg_access_lock(mrioc->pdev);
1098 	if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
1099 	    (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
1100 		goto out;
1101 	if (mrioc->unrecoverable)
1102 		goto out;
1103 retry_reset:
1104 	unlock_retry_count = 0;
1105 	mpi3mr_clear_reset_history(mrioc);
1106 	do {
1107 		ioc_info(mrioc,
1108 		    "Write magic sequence to unlock host diag register (retry=%d)\n",
1109 		    ++unlock_retry_count);
1110 		if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
1111 			writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1112 			mrioc->unrecoverable = 1;
1113 			goto out;
1114 		}
1115 
1116 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH,
1117 		    &mrioc->sysif_regs->write_sequence);
1118 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST,
1119 		    &mrioc->sysif_regs->write_sequence);
1120 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1121 		    &mrioc->sysif_regs->write_sequence);
1122 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD,
1123 		    &mrioc->sysif_regs->write_sequence);
1124 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH,
1125 		    &mrioc->sysif_regs->write_sequence);
1126 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH,
1127 		    &mrioc->sysif_regs->write_sequence);
1128 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH,
1129 		    &mrioc->sysif_regs->write_sequence);
1130 		usleep_range(1000, 1100);
1131 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
1132 		ioc_info(mrioc,
1133 		    "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
1134 		    unlock_retry_count, host_diagnostic);
1135 	} while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
1136 
1137 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1138 	ioc_info(mrioc, "%s reset due to %s(0x%x)\n",
1139 	    mpi3mr_reset_type_name(reset_type),
1140 	    mpi3mr_reset_rc_name(reset_reason), reset_reason);
1141 	writel(host_diagnostic | reset_type,
1142 	    &mrioc->sysif_regs->host_diagnostic);
1143 	timeout = mrioc->ready_timeout * 10;
1144 	if (reset_type == MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) {
1145 		do {
1146 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1147 			if (ioc_status &
1148 			    MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
1149 				mpi3mr_clear_reset_history(mrioc);
1150 				ioc_config =
1151 				    readl(&mrioc->sysif_regs->ioc_configuration);
1152 				if (mpi3mr_soft_reset_success(ioc_status,
1153 				    ioc_config)) {
1154 					retval = 0;
1155 					break;
1156 				}
1157 			}
1158 			msleep(100);
1159 		} while (--timeout);
1160 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1161 		    &mrioc->sysif_regs->write_sequence);
1162 	} else if (reset_type == MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT) {
1163 		do {
1164 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1165 			if (mpi3mr_diagfault_success(mrioc, ioc_status)) {
1166 				retval = 0;
1167 				break;
1168 			}
1169 			msleep(100);
1170 		} while (--timeout);
1171 		mpi3mr_clear_reset_history(mrioc);
1172 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1173 		    &mrioc->sysif_regs->write_sequence);
1174 	}
1175 	if (retval && ((++reset_retry_count) < MPI3MR_MAX_RESET_RETRY_COUNT)) {
1176 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1177 		ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1178 		ioc_info(mrioc,
1179 		    "Base IOC Sts/Config after reset try %d is (0x%x)/(0x%x)\n",
1180 		    reset_retry_count, ioc_status, ioc_config);
1181 		goto retry_reset;
1182 	}
1183 
1184 out:
1185 	pci_cfg_access_unlock(mrioc->pdev);
1186 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1187 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1188 
1189 	ioc_info(mrioc,
1190 	    "Base IOC Sts/Config after %s reset is (0x%x)/(0x%x)\n",
1191 	    (!retval) ? "successful" : "failed", ioc_status,
1192 	    ioc_config);
1193 	return retval;
1194 }
1195 
1196 /**
1197  * mpi3mr_admin_request_post - Post request to admin queue
1198  * @mrioc: Adapter reference
1199  * @admin_req: MPI3 request
1200  * @admin_req_sz: Request size
1201  * @ignore_reset: Ignore reset in process
1202  *
1203  * Post the MPI3 request into admin request queue and
1204  * inform the controller, if the queue is full return
1205  * appropriate error.
1206  *
1207  * Return: 0 on success, non-zero on failure.
1208  */
1209 int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
1210 	u16 admin_req_sz, u8 ignore_reset)
1211 {
1212 	u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
1213 	int retval = 0;
1214 	unsigned long flags;
1215 	u8 *areq_entry;
1216 
1217 	if (mrioc->unrecoverable) {
1218 		ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
1219 		return -EFAULT;
1220 	}
1221 
1222 	spin_lock_irqsave(&mrioc->admin_req_lock, flags);
1223 	areq_pi = mrioc->admin_req_pi;
1224 	areq_ci = mrioc->admin_req_ci;
1225 	max_entries = mrioc->num_admin_req;
1226 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
1227 	    (areq_pi == (max_entries - 1)))) {
1228 		ioc_err(mrioc, "AdminReqQ full condition detected\n");
1229 		retval = -EAGAIN;
1230 		goto out;
1231 	}
1232 	if (!ignore_reset && mrioc->reset_in_progress) {
1233 		ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
1234 		retval = -EAGAIN;
1235 		goto out;
1236 	}
1237 	areq_entry = (u8 *)mrioc->admin_req_base +
1238 	    (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
1239 	memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
1240 	memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
1241 
1242 	if (++areq_pi == max_entries)
1243 		areq_pi = 0;
1244 	mrioc->admin_req_pi = areq_pi;
1245 
1246 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
1247 
1248 out:
1249 	spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
1250 
1251 	return retval;
1252 }
1253 
1254 /**
1255  * mpi3mr_free_op_req_q_segments - free request memory segments
1256  * @mrioc: Adapter instance reference
1257  * @q_idx: operational request queue index
1258  *
1259  * Free memory segments allocated for operational request queue
1260  *
1261  * Return: Nothing.
1262  */
1263 static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1264 {
1265 	u16 j;
1266 	int size;
1267 	struct segments *segments;
1268 
1269 	segments = mrioc->req_qinfo[q_idx].q_segments;
1270 	if (!segments)
1271 		return;
1272 
1273 	if (mrioc->enable_segqueue) {
1274 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1275 		if (mrioc->req_qinfo[q_idx].q_segment_list) {
1276 			dma_free_coherent(&mrioc->pdev->dev,
1277 			    MPI3MR_MAX_SEG_LIST_SIZE,
1278 			    mrioc->req_qinfo[q_idx].q_segment_list,
1279 			    mrioc->req_qinfo[q_idx].q_segment_list_dma);
1280 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1281 		}
1282 	} else
1283 		size = mrioc->req_qinfo[q_idx].num_requests *
1284 		    mrioc->facts.op_req_sz;
1285 
1286 	for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) {
1287 		if (!segments[j].segment)
1288 			continue;
1289 		dma_free_coherent(&mrioc->pdev->dev,
1290 		    size, segments[j].segment, segments[j].segment_dma);
1291 		segments[j].segment = NULL;
1292 	}
1293 	kfree(mrioc->req_qinfo[q_idx].q_segments);
1294 	mrioc->req_qinfo[q_idx].q_segments = NULL;
1295 	mrioc->req_qinfo[q_idx].qid = 0;
1296 }
1297 
1298 /**
1299  * mpi3mr_free_op_reply_q_segments - free reply memory segments
1300  * @mrioc: Adapter instance reference
1301  * @q_idx: operational reply queue index
1302  *
1303  * Free memory segments allocated for operational reply queue
1304  *
1305  * Return: Nothing.
1306  */
1307 static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1308 {
1309 	u16 j;
1310 	int size;
1311 	struct segments *segments;
1312 
1313 	segments = mrioc->op_reply_qinfo[q_idx].q_segments;
1314 	if (!segments)
1315 		return;
1316 
1317 	if (mrioc->enable_segqueue) {
1318 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1319 		if (mrioc->op_reply_qinfo[q_idx].q_segment_list) {
1320 			dma_free_coherent(&mrioc->pdev->dev,
1321 			    MPI3MR_MAX_SEG_LIST_SIZE,
1322 			    mrioc->op_reply_qinfo[q_idx].q_segment_list,
1323 			    mrioc->op_reply_qinfo[q_idx].q_segment_list_dma);
1324 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1325 		}
1326 	} else
1327 		size = mrioc->op_reply_qinfo[q_idx].segment_qd *
1328 		    mrioc->op_reply_desc_sz;
1329 
1330 	for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) {
1331 		if (!segments[j].segment)
1332 			continue;
1333 		dma_free_coherent(&mrioc->pdev->dev,
1334 		    size, segments[j].segment, segments[j].segment_dma);
1335 		segments[j].segment = NULL;
1336 	}
1337 
1338 	kfree(mrioc->op_reply_qinfo[q_idx].q_segments);
1339 	mrioc->op_reply_qinfo[q_idx].q_segments = NULL;
1340 	mrioc->op_reply_qinfo[q_idx].qid = 0;
1341 }
1342 
1343 /**
1344  * mpi3mr_delete_op_reply_q - delete operational reply queue
1345  * @mrioc: Adapter instance reference
1346  * @qidx: operational reply queue index
1347  *
1348  * Delete operatinal reply queue by issuing MPI request
1349  * through admin queue.
1350  *
1351  * Return:  0 on success, non-zero on failure.
1352  */
1353 static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1354 {
1355 	struct mpi3_delete_reply_queue_request delq_req;
1356 	int retval = 0;
1357 	u16 reply_qid = 0, midx;
1358 
1359 	reply_qid = mrioc->op_reply_qinfo[qidx].qid;
1360 
1361 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1362 
1363 	if (!reply_qid)	{
1364 		retval = -1;
1365 		ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n");
1366 		goto out;
1367 	}
1368 
1369 	memset(&delq_req, 0, sizeof(delq_req));
1370 	mutex_lock(&mrioc->init_cmds.mutex);
1371 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1372 		retval = -1;
1373 		ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n");
1374 		mutex_unlock(&mrioc->init_cmds.mutex);
1375 		goto out;
1376 	}
1377 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1378 	mrioc->init_cmds.is_waiting = 1;
1379 	mrioc->init_cmds.callback = NULL;
1380 	delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1381 	delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
1382 	delq_req.queue_id = cpu_to_le16(reply_qid);
1383 
1384 	init_completion(&mrioc->init_cmds.done);
1385 	retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req),
1386 	    1);
1387 	if (retval) {
1388 		ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n");
1389 		goto out_unlock;
1390 	}
1391 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1392 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1393 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1394 		ioc_err(mrioc, "delete reply queue timed out\n");
1395 		mpi3mr_check_rh_fault_ioc(mrioc,
1396 		    MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
1397 		retval = -1;
1398 		goto out_unlock;
1399 	}
1400 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1401 	    != MPI3_IOCSTATUS_SUCCESS) {
1402 		ioc_err(mrioc,
1403 		    "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1404 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1405 		    mrioc->init_cmds.ioc_loginfo);
1406 		retval = -1;
1407 		goto out_unlock;
1408 	}
1409 	mrioc->intr_info[midx].op_reply_q = NULL;
1410 
1411 	mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1412 out_unlock:
1413 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1414 	mutex_unlock(&mrioc->init_cmds.mutex);
1415 out:
1416 
1417 	return retval;
1418 }
1419 
1420 /**
1421  * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool
1422  * @mrioc: Adapter instance reference
1423  * @qidx: request queue index
1424  *
1425  * Allocate segmented memory pools for operational reply
1426  * queue.
1427  *
1428  * Return: 0 on success, non-zero on failure.
1429  */
1430 static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1431 {
1432 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1433 	int i, size;
1434 	u64 *q_segment_list_entry = NULL;
1435 	struct segments *segments;
1436 
1437 	if (mrioc->enable_segqueue) {
1438 		op_reply_q->segment_qd =
1439 		    MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz;
1440 
1441 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1442 
1443 		op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1444 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma,
1445 		    GFP_KERNEL);
1446 		if (!op_reply_q->q_segment_list)
1447 			return -ENOMEM;
1448 		q_segment_list_entry = (u64 *)op_reply_q->q_segment_list;
1449 	} else {
1450 		op_reply_q->segment_qd = op_reply_q->num_replies;
1451 		size = op_reply_q->num_replies * mrioc->op_reply_desc_sz;
1452 	}
1453 
1454 	op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies,
1455 	    op_reply_q->segment_qd);
1456 
1457 	op_reply_q->q_segments = kcalloc(op_reply_q->num_segments,
1458 	    sizeof(struct segments), GFP_KERNEL);
1459 	if (!op_reply_q->q_segments)
1460 		return -ENOMEM;
1461 
1462 	segments = op_reply_q->q_segments;
1463 	for (i = 0; i < op_reply_q->num_segments; i++) {
1464 		segments[i].segment =
1465 		    dma_alloc_coherent(&mrioc->pdev->dev,
1466 		    size, &segments[i].segment_dma, GFP_KERNEL);
1467 		if (!segments[i].segment)
1468 			return -ENOMEM;
1469 		if (mrioc->enable_segqueue)
1470 			q_segment_list_entry[i] =
1471 			    (unsigned long)segments[i].segment_dma;
1472 	}
1473 
1474 	return 0;
1475 }
1476 
1477 /**
1478  * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool.
1479  * @mrioc: Adapter instance reference
1480  * @qidx: request queue index
1481  *
1482  * Allocate segmented memory pools for operational request
1483  * queue.
1484  *
1485  * Return: 0 on success, non-zero on failure.
1486  */
1487 static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1488 {
1489 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
1490 	int i, size;
1491 	u64 *q_segment_list_entry = NULL;
1492 	struct segments *segments;
1493 
1494 	if (mrioc->enable_segqueue) {
1495 		op_req_q->segment_qd =
1496 		    MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz;
1497 
1498 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1499 
1500 		op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1501 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma,
1502 		    GFP_KERNEL);
1503 		if (!op_req_q->q_segment_list)
1504 			return -ENOMEM;
1505 		q_segment_list_entry = (u64 *)op_req_q->q_segment_list;
1506 
1507 	} else {
1508 		op_req_q->segment_qd = op_req_q->num_requests;
1509 		size = op_req_q->num_requests * mrioc->facts.op_req_sz;
1510 	}
1511 
1512 	op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests,
1513 	    op_req_q->segment_qd);
1514 
1515 	op_req_q->q_segments = kcalloc(op_req_q->num_segments,
1516 	    sizeof(struct segments), GFP_KERNEL);
1517 	if (!op_req_q->q_segments)
1518 		return -ENOMEM;
1519 
1520 	segments = op_req_q->q_segments;
1521 	for (i = 0; i < op_req_q->num_segments; i++) {
1522 		segments[i].segment =
1523 		    dma_alloc_coherent(&mrioc->pdev->dev,
1524 		    size, &segments[i].segment_dma, GFP_KERNEL);
1525 		if (!segments[i].segment)
1526 			return -ENOMEM;
1527 		if (mrioc->enable_segqueue)
1528 			q_segment_list_entry[i] =
1529 			    (unsigned long)segments[i].segment_dma;
1530 	}
1531 
1532 	return 0;
1533 }
1534 
1535 /**
1536  * mpi3mr_create_op_reply_q - create operational reply queue
1537  * @mrioc: Adapter instance reference
1538  * @qidx: operational reply queue index
1539  *
1540  * Create operatinal reply queue by issuing MPI request
1541  * through admin queue.
1542  *
1543  * Return:  0 on success, non-zero on failure.
1544  */
1545 static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1546 {
1547 	struct mpi3_create_reply_queue_request create_req;
1548 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1549 	int retval = 0;
1550 	u16 reply_qid = 0, midx;
1551 
1552 	reply_qid = op_reply_q->qid;
1553 
1554 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1555 
1556 	if (reply_qid) {
1557 		retval = -1;
1558 		ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n",
1559 		    reply_qid);
1560 
1561 		return retval;
1562 	}
1563 
1564 	reply_qid = qidx + 1;
1565 	op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
1566 	op_reply_q->ci = 0;
1567 	op_reply_q->ephase = 1;
1568 	atomic_set(&op_reply_q->pend_ios, 0);
1569 	atomic_set(&op_reply_q->in_use, 0);
1570 	op_reply_q->enable_irq_poll = false;
1571 
1572 	if (!op_reply_q->q_segments) {
1573 		retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx);
1574 		if (retval) {
1575 			mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1576 			goto out;
1577 		}
1578 	}
1579 
1580 	memset(&create_req, 0, sizeof(create_req));
1581 	mutex_lock(&mrioc->init_cmds.mutex);
1582 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1583 		retval = -1;
1584 		ioc_err(mrioc, "CreateRepQ: Init command is in use\n");
1585 		goto out_unlock;
1586 	}
1587 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1588 	mrioc->init_cmds.is_waiting = 1;
1589 	mrioc->init_cmds.callback = NULL;
1590 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1591 	create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
1592 	create_req.queue_id = cpu_to_le16(reply_qid);
1593 	create_req.flags = MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
1594 	create_req.msix_index = cpu_to_le16(mrioc->intr_info[midx].msix_index);
1595 	if (mrioc->enable_segqueue) {
1596 		create_req.flags |=
1597 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1598 		create_req.base_address = cpu_to_le64(
1599 		    op_reply_q->q_segment_list_dma);
1600 	} else
1601 		create_req.base_address = cpu_to_le64(
1602 		    op_reply_q->q_segments[0].segment_dma);
1603 
1604 	create_req.size = cpu_to_le16(op_reply_q->num_replies);
1605 
1606 	init_completion(&mrioc->init_cmds.done);
1607 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1608 	    sizeof(create_req), 1);
1609 	if (retval) {
1610 		ioc_err(mrioc, "CreateRepQ: Admin Post failed\n");
1611 		goto out_unlock;
1612 	}
1613 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1614 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1615 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1616 		ioc_err(mrioc, "create reply queue timed out\n");
1617 		mpi3mr_check_rh_fault_ioc(mrioc,
1618 		    MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
1619 		retval = -1;
1620 		goto out_unlock;
1621 	}
1622 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1623 	    != MPI3_IOCSTATUS_SUCCESS) {
1624 		ioc_err(mrioc,
1625 		    "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1626 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1627 		    mrioc->init_cmds.ioc_loginfo);
1628 		retval = -1;
1629 		goto out_unlock;
1630 	}
1631 	op_reply_q->qid = reply_qid;
1632 	mrioc->intr_info[midx].op_reply_q = op_reply_q;
1633 
1634 out_unlock:
1635 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1636 	mutex_unlock(&mrioc->init_cmds.mutex);
1637 out:
1638 
1639 	return retval;
1640 }
1641 
1642 /**
1643  * mpi3mr_create_op_req_q - create operational request queue
1644  * @mrioc: Adapter instance reference
1645  * @idx: operational request queue index
1646  * @reply_qid: Reply queue ID
1647  *
1648  * Create operatinal request queue by issuing MPI request
1649  * through admin queue.
1650  *
1651  * Return:  0 on success, non-zero on failure.
1652  */
1653 static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx,
1654 	u16 reply_qid)
1655 {
1656 	struct mpi3_create_request_queue_request create_req;
1657 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx;
1658 	int retval = 0;
1659 	u16 req_qid = 0;
1660 
1661 	req_qid = op_req_q->qid;
1662 
1663 	if (req_qid) {
1664 		retval = -1;
1665 		ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n",
1666 		    req_qid);
1667 
1668 		return retval;
1669 	}
1670 	req_qid = idx + 1;
1671 
1672 	op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD;
1673 	op_req_q->ci = 0;
1674 	op_req_q->pi = 0;
1675 	op_req_q->reply_qid = reply_qid;
1676 	spin_lock_init(&op_req_q->q_lock);
1677 
1678 	if (!op_req_q->q_segments) {
1679 		retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx);
1680 		if (retval) {
1681 			mpi3mr_free_op_req_q_segments(mrioc, idx);
1682 			goto out;
1683 		}
1684 	}
1685 
1686 	memset(&create_req, 0, sizeof(create_req));
1687 	mutex_lock(&mrioc->init_cmds.mutex);
1688 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1689 		retval = -1;
1690 		ioc_err(mrioc, "CreateReqQ: Init command is in use\n");
1691 		goto out_unlock;
1692 	}
1693 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1694 	mrioc->init_cmds.is_waiting = 1;
1695 	mrioc->init_cmds.callback = NULL;
1696 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1697 	create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
1698 	create_req.queue_id = cpu_to_le16(req_qid);
1699 	if (mrioc->enable_segqueue) {
1700 		create_req.flags =
1701 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1702 		create_req.base_address = cpu_to_le64(
1703 		    op_req_q->q_segment_list_dma);
1704 	} else
1705 		create_req.base_address = cpu_to_le64(
1706 		    op_req_q->q_segments[0].segment_dma);
1707 	create_req.reply_queue_id = cpu_to_le16(reply_qid);
1708 	create_req.size = cpu_to_le16(op_req_q->num_requests);
1709 
1710 	init_completion(&mrioc->init_cmds.done);
1711 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1712 	    sizeof(create_req), 1);
1713 	if (retval) {
1714 		ioc_err(mrioc, "CreateReqQ: Admin Post failed\n");
1715 		goto out_unlock;
1716 	}
1717 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1718 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1719 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1720 		ioc_err(mrioc, "create request queue timed out\n");
1721 		mpi3mr_check_rh_fault_ioc(mrioc,
1722 		    MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
1723 		retval = -1;
1724 		goto out_unlock;
1725 	}
1726 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1727 	    != MPI3_IOCSTATUS_SUCCESS) {
1728 		ioc_err(mrioc,
1729 		    "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1730 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1731 		    mrioc->init_cmds.ioc_loginfo);
1732 		retval = -1;
1733 		goto out_unlock;
1734 	}
1735 	op_req_q->qid = req_qid;
1736 
1737 out_unlock:
1738 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1739 	mutex_unlock(&mrioc->init_cmds.mutex);
1740 out:
1741 
1742 	return retval;
1743 }
1744 
1745 /**
1746  * mpi3mr_create_op_queues - create operational queue pairs
1747  * @mrioc: Adapter instance reference
1748  *
1749  * Allocate memory for operational queue meta data and call
1750  * create request and reply queue functions.
1751  *
1752  * Return: 0 on success, non-zero on failures.
1753  */
1754 static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc)
1755 {
1756 	int retval = 0;
1757 	u16 num_queues = 0, i = 0, msix_count_op_q = 1;
1758 
1759 	num_queues = min_t(int, mrioc->facts.max_op_reply_q,
1760 	    mrioc->facts.max_op_req_q);
1761 
1762 	msix_count_op_q =
1763 	    mrioc->intr_info_count - mrioc->op_reply_q_offset;
1764 	if (!mrioc->num_queues)
1765 		mrioc->num_queues = min_t(int, num_queues, msix_count_op_q);
1766 	num_queues = mrioc->num_queues;
1767 	ioc_info(mrioc, "Trying to create %d Operational Q pairs\n",
1768 	    num_queues);
1769 
1770 	if (!mrioc->req_qinfo) {
1771 		mrioc->req_qinfo = kcalloc(num_queues,
1772 		    sizeof(struct op_req_qinfo), GFP_KERNEL);
1773 		if (!mrioc->req_qinfo) {
1774 			retval = -1;
1775 			goto out_failed;
1776 		}
1777 
1778 		mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) *
1779 		    num_queues, GFP_KERNEL);
1780 		if (!mrioc->op_reply_qinfo) {
1781 			retval = -1;
1782 			goto out_failed;
1783 		}
1784 	}
1785 
1786 	if (mrioc->enable_segqueue)
1787 		ioc_info(mrioc,
1788 		    "allocating operational queues through segmented queues\n");
1789 
1790 	for (i = 0; i < num_queues; i++) {
1791 		if (mpi3mr_create_op_reply_q(mrioc, i)) {
1792 			ioc_err(mrioc, "Cannot create OP RepQ %d\n", i);
1793 			break;
1794 		}
1795 		if (mpi3mr_create_op_req_q(mrioc, i,
1796 		    mrioc->op_reply_qinfo[i].qid)) {
1797 			ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i);
1798 			mpi3mr_delete_op_reply_q(mrioc, i);
1799 			break;
1800 		}
1801 	}
1802 
1803 	if (i == 0) {
1804 		/* Not even one queue is created successfully*/
1805 		retval = -1;
1806 		goto out_failed;
1807 	}
1808 	mrioc->num_op_reply_q = mrioc->num_op_req_q = i;
1809 	ioc_info(mrioc, "Successfully created %d Operational Q pairs\n",
1810 	    mrioc->num_op_reply_q);
1811 
1812 	return retval;
1813 out_failed:
1814 	kfree(mrioc->req_qinfo);
1815 	mrioc->req_qinfo = NULL;
1816 
1817 	kfree(mrioc->op_reply_qinfo);
1818 	mrioc->op_reply_qinfo = NULL;
1819 
1820 	return retval;
1821 }
1822 
1823 /**
1824  * mpi3mr_op_request_post - Post request to operational queue
1825  * @mrioc: Adapter reference
1826  * @op_req_q: Operational request queue info
1827  * @req: MPI3 request
1828  *
1829  * Post the MPI3 request into operational request queue and
1830  * inform the controller, if the queue is full return
1831  * appropriate error.
1832  *
1833  * Return: 0 on success, non-zero on failure.
1834  */
1835 int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc,
1836 	struct op_req_qinfo *op_req_q, u8 *req)
1837 {
1838 	u16 pi = 0, max_entries, reply_qidx = 0, midx;
1839 	int retval = 0;
1840 	unsigned long flags;
1841 	u8 *req_entry;
1842 	void *segment_base_addr;
1843 	u16 req_sz = mrioc->facts.op_req_sz;
1844 	struct segments *segments = op_req_q->q_segments;
1845 
1846 	reply_qidx = op_req_q->reply_qid - 1;
1847 
1848 	if (mrioc->unrecoverable)
1849 		return -EFAULT;
1850 
1851 	spin_lock_irqsave(&op_req_q->q_lock, flags);
1852 	pi = op_req_q->pi;
1853 	max_entries = op_req_q->num_requests;
1854 
1855 	if (mpi3mr_check_req_qfull(op_req_q)) {
1856 		midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(
1857 		    reply_qidx, mrioc->op_reply_q_offset);
1858 		mpi3mr_process_op_reply_q(mrioc, &mrioc->intr_info[midx]);
1859 
1860 		if (mpi3mr_check_req_qfull(op_req_q)) {
1861 			retval = -EAGAIN;
1862 			goto out;
1863 		}
1864 	}
1865 
1866 	if (mrioc->reset_in_progress) {
1867 		ioc_err(mrioc, "OpReqQ submit reset in progress\n");
1868 		retval = -EAGAIN;
1869 		goto out;
1870 	}
1871 
1872 	segment_base_addr = segments[pi / op_req_q->segment_qd].segment;
1873 	req_entry = (u8 *)segment_base_addr +
1874 	    ((pi % op_req_q->segment_qd) * req_sz);
1875 
1876 	memset(req_entry, 0, req_sz);
1877 	memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ);
1878 
1879 	if (++pi == max_entries)
1880 		pi = 0;
1881 	op_req_q->pi = pi;
1882 
1883 	if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios)
1884 	    > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT)
1885 		mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true;
1886 
1887 	writel(op_req_q->pi,
1888 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index);
1889 
1890 out:
1891 	spin_unlock_irqrestore(&op_req_q->q_lock, flags);
1892 	return retval;
1893 }
1894 
1895 /**
1896  * mpi3mr_check_rh_fault_ioc - check reset history and fault
1897  * controller
1898  * @mrioc: Adapter instance reference
1899  * @reason_code, reason code for the fault.
1900  *
1901  * This routine will save snapdump and fault the controller with
1902  * the given reason code if it is not already in the fault or
1903  * not asynchronosuly reset. This will be used to handle
1904  * initilaization time faults/resets/timeout as in those cases
1905  * immediate soft reset invocation is not required.
1906  *
1907  * Return:  None.
1908  */
1909 void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code)
1910 {
1911 	u32 ioc_status, host_diagnostic, timeout;
1912 
1913 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1914 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
1915 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
1916 		mpi3mr_print_fault_info(mrioc);
1917 		return;
1918 	}
1919 	mpi3mr_set_diagsave(mrioc);
1920 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
1921 	    reason_code);
1922 	timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
1923 	do {
1924 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
1925 		if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
1926 			break;
1927 		msleep(100);
1928 	} while (--timeout);
1929 }
1930 
1931 /**
1932  * mpi3mr_sync_timestamp - Issue time stamp sync request
1933  * @mrioc: Adapter reference
1934  *
1935  * Issue IO unit control MPI request to synchornize firmware
1936  * timestamp with host time.
1937  *
1938  * Return: 0 on success, non-zero on failure.
1939  */
1940 static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc)
1941 {
1942 	ktime_t current_time;
1943 	struct mpi3_iounit_control_request iou_ctrl;
1944 	int retval = 0;
1945 
1946 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
1947 	mutex_lock(&mrioc->init_cmds.mutex);
1948 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1949 		retval = -1;
1950 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n");
1951 		mutex_unlock(&mrioc->init_cmds.mutex);
1952 		goto out;
1953 	}
1954 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1955 	mrioc->init_cmds.is_waiting = 1;
1956 	mrioc->init_cmds.callback = NULL;
1957 	iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1958 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
1959 	iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP;
1960 	current_time = ktime_get_real();
1961 	iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time));
1962 
1963 	init_completion(&mrioc->init_cmds.done);
1964 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl,
1965 	    sizeof(iou_ctrl), 0);
1966 	if (retval) {
1967 		ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n");
1968 		goto out_unlock;
1969 	}
1970 
1971 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1972 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1973 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1974 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n");
1975 		mrioc->init_cmds.is_waiting = 0;
1976 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
1977 			mpi3mr_soft_reset_handler(mrioc,
1978 			    MPI3MR_RESET_FROM_TSU_TIMEOUT, 1);
1979 		retval = -1;
1980 		goto out_unlock;
1981 	}
1982 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1983 	    != MPI3_IOCSTATUS_SUCCESS) {
1984 		ioc_err(mrioc,
1985 		    "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1986 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1987 		    mrioc->init_cmds.ioc_loginfo);
1988 		retval = -1;
1989 		goto out_unlock;
1990 	}
1991 
1992 out_unlock:
1993 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1994 	mutex_unlock(&mrioc->init_cmds.mutex);
1995 
1996 out:
1997 	return retval;
1998 }
1999 
2000 /**
2001  * mpi3mr_print_pkg_ver - display controller fw package version
2002  * @mrioc: Adapter reference
2003  *
2004  * Retrieve firmware package version from the component image
2005  * header of the controller flash and display it.
2006  *
2007  * Return: 0 on success and non-zero on failure.
2008  */
2009 static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc)
2010 {
2011 	struct mpi3_ci_upload_request ci_upload;
2012 	int retval = -1;
2013 	void *data = NULL;
2014 	dma_addr_t data_dma;
2015 	struct mpi3_ci_manifest_mpi *manifest;
2016 	u32 data_len = sizeof(struct mpi3_ci_manifest_mpi);
2017 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
2018 
2019 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2020 	    GFP_KERNEL);
2021 	if (!data)
2022 		return -ENOMEM;
2023 
2024 	memset(&ci_upload, 0, sizeof(ci_upload));
2025 	mutex_lock(&mrioc->init_cmds.mutex);
2026 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2027 		ioc_err(mrioc, "sending get package version failed due to command in use\n");
2028 		mutex_unlock(&mrioc->init_cmds.mutex);
2029 		goto out;
2030 	}
2031 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2032 	mrioc->init_cmds.is_waiting = 1;
2033 	mrioc->init_cmds.callback = NULL;
2034 	ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2035 	ci_upload.function = MPI3_FUNCTION_CI_UPLOAD;
2036 	ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
2037 	ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST);
2038 	ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE);
2039 	ci_upload.segment_size = cpu_to_le32(data_len);
2040 
2041 	mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len,
2042 	    data_dma);
2043 	init_completion(&mrioc->init_cmds.done);
2044 	retval = mpi3mr_admin_request_post(mrioc, &ci_upload,
2045 	    sizeof(ci_upload), 1);
2046 	if (retval) {
2047 		ioc_err(mrioc, "posting get package version failed\n");
2048 		goto out_unlock;
2049 	}
2050 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2051 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2052 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2053 		ioc_err(mrioc, "get package version timed out\n");
2054 		mpi3mr_check_rh_fault_ioc(mrioc,
2055 		    MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
2056 		retval = -1;
2057 		goto out_unlock;
2058 	}
2059 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2060 	    == MPI3_IOCSTATUS_SUCCESS) {
2061 		manifest = (struct mpi3_ci_manifest_mpi *) data;
2062 		if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) {
2063 			ioc_info(mrioc,
2064 			    "firmware package version(%d.%d.%d.%d.%05d-%05d)\n",
2065 			    manifest->package_version.gen_major,
2066 			    manifest->package_version.gen_minor,
2067 			    manifest->package_version.phase_major,
2068 			    manifest->package_version.phase_minor,
2069 			    manifest->package_version.customer_id,
2070 			    manifest->package_version.build_num);
2071 		}
2072 	}
2073 	retval = 0;
2074 out_unlock:
2075 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2076 	mutex_unlock(&mrioc->init_cmds.mutex);
2077 
2078 out:
2079 	if (data)
2080 		dma_free_coherent(&mrioc->pdev->dev, data_len, data,
2081 		    data_dma);
2082 	return retval;
2083 }
2084 
2085 /**
2086  * mpi3mr_watchdog_work - watchdog thread to monitor faults
2087  * @work: work struct
2088  *
2089  * Watch dog work periodically executed (1 second interval) to
2090  * monitor firmware fault and to issue periodic timer sync to
2091  * the firmware.
2092  *
2093  * Return: Nothing.
2094  */
2095 static void mpi3mr_watchdog_work(struct work_struct *work)
2096 {
2097 	struct mpi3mr_ioc *mrioc =
2098 	    container_of(work, struct mpi3mr_ioc, watchdog_work.work);
2099 	unsigned long flags;
2100 	enum mpi3mr_iocstate ioc_state;
2101 	u32 fault, host_diagnostic;
2102 
2103 	if (mrioc->ts_update_counter++ >= MPI3MR_TSUPDATE_INTERVAL) {
2104 		mrioc->ts_update_counter = 0;
2105 		mpi3mr_sync_timestamp(mrioc);
2106 	}
2107 
2108 	/*Check for fault state every one second and issue Soft reset*/
2109 	ioc_state = mpi3mr_get_iocstate(mrioc);
2110 	if (ioc_state == MRIOC_STATE_FAULT) {
2111 		fault = readl(&mrioc->sysif_regs->fault) &
2112 		    MPI3_SYSIF_FAULT_CODE_MASK;
2113 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2114 		if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
2115 			if (!mrioc->diagsave_timeout) {
2116 				mpi3mr_print_fault_info(mrioc);
2117 				ioc_warn(mrioc, "Diag save in progress\n");
2118 			}
2119 			if ((mrioc->diagsave_timeout++) <=
2120 			    MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
2121 				goto schedule_work;
2122 		} else
2123 			mpi3mr_print_fault_info(mrioc);
2124 		mrioc->diagsave_timeout = 0;
2125 
2126 		if (fault == MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED) {
2127 			ioc_info(mrioc,
2128 			    "Factory Reset fault occurred marking controller as unrecoverable"
2129 			    );
2130 			mrioc->unrecoverable = 1;
2131 			goto out;
2132 		}
2133 
2134 		if ((fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) ||
2135 		    (fault == MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS) ||
2136 		    (mrioc->reset_in_progress))
2137 			goto out;
2138 		if (fault == MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET)
2139 			mpi3mr_soft_reset_handler(mrioc,
2140 			    MPI3MR_RESET_FROM_CIACTIV_FAULT, 0);
2141 		else
2142 			mpi3mr_soft_reset_handler(mrioc,
2143 			    MPI3MR_RESET_FROM_FAULT_WATCH, 0);
2144 	}
2145 
2146 schedule_work:
2147 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2148 	if (mrioc->watchdog_work_q)
2149 		queue_delayed_work(mrioc->watchdog_work_q,
2150 		    &mrioc->watchdog_work,
2151 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2152 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2153 out:
2154 	return;
2155 }
2156 
2157 /**
2158  * mpi3mr_start_watchdog - Start watchdog
2159  * @mrioc: Adapter instance reference
2160  *
2161  * Create and start the watchdog thread to monitor controller
2162  * faults.
2163  *
2164  * Return: Nothing.
2165  */
2166 void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc)
2167 {
2168 	if (mrioc->watchdog_work_q)
2169 		return;
2170 
2171 	INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work);
2172 	snprintf(mrioc->watchdog_work_q_name,
2173 	    sizeof(mrioc->watchdog_work_q_name), "watchdog_%s%d", mrioc->name,
2174 	    mrioc->id);
2175 	mrioc->watchdog_work_q =
2176 	    create_singlethread_workqueue(mrioc->watchdog_work_q_name);
2177 	if (!mrioc->watchdog_work_q) {
2178 		ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__);
2179 		return;
2180 	}
2181 
2182 	if (mrioc->watchdog_work_q)
2183 		queue_delayed_work(mrioc->watchdog_work_q,
2184 		    &mrioc->watchdog_work,
2185 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2186 }
2187 
2188 /**
2189  * mpi3mr_stop_watchdog - Stop watchdog
2190  * @mrioc: Adapter instance reference
2191  *
2192  * Stop the watchdog thread created to monitor controller
2193  * faults.
2194  *
2195  * Return: Nothing.
2196  */
2197 void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc)
2198 {
2199 	unsigned long flags;
2200 	struct workqueue_struct *wq;
2201 
2202 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2203 	wq = mrioc->watchdog_work_q;
2204 	mrioc->watchdog_work_q = NULL;
2205 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2206 	if (wq) {
2207 		if (!cancel_delayed_work_sync(&mrioc->watchdog_work))
2208 			flush_workqueue(wq);
2209 		destroy_workqueue(wq);
2210 	}
2211 }
2212 
2213 /**
2214  * mpi3mr_kill_ioc - Kill the controller
2215  * @mrioc: Adapter instance reference
2216  * @reason: reason for the failure.
2217  *
2218  * If fault debug is enabled, display the fault info else issue
2219  * diag fault and freeze the system for controller debug
2220  * purpose.
2221  *
2222  * Return: Nothing.
2223  */
2224 static void mpi3mr_kill_ioc(struct mpi3mr_ioc *mrioc, u32 reason)
2225 {
2226 	enum mpi3mr_iocstate ioc_state;
2227 
2228 	if (!mrioc->fault_dbg)
2229 		return;
2230 
2231 	dump_stack();
2232 
2233 	ioc_state = mpi3mr_get_iocstate(mrioc);
2234 	if (ioc_state == MRIOC_STATE_FAULT)
2235 		mpi3mr_print_fault_info(mrioc);
2236 	else {
2237 		ioc_err(mrioc, "Firmware is halted due to the reason %d\n",
2238 		    reason);
2239 		mpi3mr_diagfault_reset_handler(mrioc, reason);
2240 	}
2241 	if (mrioc->fault_dbg == 2)
2242 		for (;;)
2243 			;
2244 	else
2245 		panic("panic in %s\n", __func__);
2246 }
2247 
2248 /**
2249  * mpi3mr_setup_admin_qpair - Setup admin queue pair
2250  * @mrioc: Adapter instance reference
2251  *
2252  * Allocate memory for admin queue pair if required and register
2253  * the admin queue with the controller.
2254  *
2255  * Return: 0 on success, non-zero on failures.
2256  */
2257 static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
2258 {
2259 	int retval = 0;
2260 	u32 num_admin_entries = 0;
2261 
2262 	mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
2263 	mrioc->num_admin_req = mrioc->admin_req_q_sz /
2264 	    MPI3MR_ADMIN_REQ_FRAME_SZ;
2265 	mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
2266 	mrioc->admin_req_base = NULL;
2267 
2268 	mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
2269 	mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
2270 	    MPI3MR_ADMIN_REPLY_FRAME_SZ;
2271 	mrioc->admin_reply_ci = 0;
2272 	mrioc->admin_reply_ephase = 1;
2273 	mrioc->admin_reply_base = NULL;
2274 
2275 	if (!mrioc->admin_req_base) {
2276 		mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
2277 		    mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
2278 
2279 		if (!mrioc->admin_req_base) {
2280 			retval = -1;
2281 			goto out_failed;
2282 		}
2283 
2284 		mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
2285 		    mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
2286 		    GFP_KERNEL);
2287 
2288 		if (!mrioc->admin_reply_base) {
2289 			retval = -1;
2290 			goto out_failed;
2291 		}
2292 	}
2293 
2294 	num_admin_entries = (mrioc->num_admin_replies << 16) |
2295 	    (mrioc->num_admin_req);
2296 	writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
2297 	mpi3mr_writeq(mrioc->admin_req_dma,
2298 	    &mrioc->sysif_regs->admin_request_queue_address);
2299 	mpi3mr_writeq(mrioc->admin_reply_dma,
2300 	    &mrioc->sysif_regs->admin_reply_queue_address);
2301 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
2302 	writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
2303 	return retval;
2304 
2305 out_failed:
2306 
2307 	if (mrioc->admin_reply_base) {
2308 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
2309 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
2310 		mrioc->admin_reply_base = NULL;
2311 	}
2312 	if (mrioc->admin_req_base) {
2313 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
2314 		    mrioc->admin_req_base, mrioc->admin_req_dma);
2315 		mrioc->admin_req_base = NULL;
2316 	}
2317 	return retval;
2318 }
2319 
2320 /**
2321  * mpi3mr_issue_iocfacts - Send IOC Facts
2322  * @mrioc: Adapter instance reference
2323  * @facts_data: Cached IOC facts data
2324  *
2325  * Issue IOC Facts MPI request through admin queue and wait for
2326  * the completion of it or time out.
2327  *
2328  * Return: 0 on success, non-zero on failures.
2329  */
2330 static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
2331 	struct mpi3_ioc_facts_data *facts_data)
2332 {
2333 	struct mpi3_ioc_facts_request iocfacts_req;
2334 	void *data = NULL;
2335 	dma_addr_t data_dma;
2336 	u32 data_len = sizeof(*facts_data);
2337 	int retval = 0;
2338 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
2339 
2340 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2341 	    GFP_KERNEL);
2342 
2343 	if (!data) {
2344 		retval = -1;
2345 		goto out;
2346 	}
2347 
2348 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
2349 	mutex_lock(&mrioc->init_cmds.mutex);
2350 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2351 		retval = -1;
2352 		ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
2353 		mutex_unlock(&mrioc->init_cmds.mutex);
2354 		goto out;
2355 	}
2356 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2357 	mrioc->init_cmds.is_waiting = 1;
2358 	mrioc->init_cmds.callback = NULL;
2359 	iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2360 	iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
2361 
2362 	mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
2363 	    data_dma);
2364 
2365 	init_completion(&mrioc->init_cmds.done);
2366 	retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
2367 	    sizeof(iocfacts_req), 1);
2368 	if (retval) {
2369 		ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
2370 		goto out_unlock;
2371 	}
2372 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2373 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2374 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2375 		ioc_err(mrioc, "ioc_facts timed out\n");
2376 		mpi3mr_check_rh_fault_ioc(mrioc,
2377 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
2378 		retval = -1;
2379 		goto out_unlock;
2380 	}
2381 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2382 	    != MPI3_IOCSTATUS_SUCCESS) {
2383 		ioc_err(mrioc,
2384 		    "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2385 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2386 		    mrioc->init_cmds.ioc_loginfo);
2387 		retval = -1;
2388 		goto out_unlock;
2389 	}
2390 	memcpy(facts_data, (u8 *)data, data_len);
2391 out_unlock:
2392 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2393 	mutex_unlock(&mrioc->init_cmds.mutex);
2394 
2395 out:
2396 	if (data)
2397 		dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
2398 
2399 	return retval;
2400 }
2401 
2402 /**
2403  * mpi3mr_check_reset_dma_mask - Process IOC facts data
2404  * @mrioc: Adapter instance reference
2405  *
2406  * Check whether the new DMA mask requested through IOCFacts by
2407  * firmware needs to be set, if so set it .
2408  *
2409  * Return: 0 on success, non-zero on failure.
2410  */
2411 static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
2412 {
2413 	struct pci_dev *pdev = mrioc->pdev;
2414 	int r;
2415 	u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
2416 
2417 	if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
2418 		return 0;
2419 
2420 	ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
2421 	    mrioc->dma_mask, facts_dma_mask);
2422 
2423 	r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
2424 	if (r) {
2425 		ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
2426 		    facts_dma_mask, r);
2427 		return r;
2428 	}
2429 	mrioc->dma_mask = facts_dma_mask;
2430 	return r;
2431 }
2432 
2433 /**
2434  * mpi3mr_process_factsdata - Process IOC facts data
2435  * @mrioc: Adapter instance reference
2436  * @facts_data: Cached IOC facts data
2437  *
2438  * Convert IOC facts data into cpu endianness and cache it in
2439  * the driver .
2440  *
2441  * Return: Nothing.
2442  */
2443 static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
2444 	struct mpi3_ioc_facts_data *facts_data)
2445 {
2446 	u32 ioc_config, req_sz, facts_flags;
2447 
2448 	if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
2449 	    (sizeof(*facts_data) / 4)) {
2450 		ioc_warn(mrioc,
2451 		    "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
2452 		    sizeof(*facts_data),
2453 		    le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
2454 	}
2455 
2456 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
2457 	req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
2458 	    MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
2459 	if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
2460 		ioc_err(mrioc,
2461 		    "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
2462 		    req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
2463 	}
2464 
2465 	memset(&mrioc->facts, 0, sizeof(mrioc->facts));
2466 
2467 	facts_flags = le32_to_cpu(facts_data->flags);
2468 	mrioc->facts.op_req_sz = req_sz;
2469 	mrioc->op_reply_desc_sz = 1 << ((ioc_config &
2470 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
2471 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
2472 
2473 	mrioc->facts.ioc_num = facts_data->ioc_number;
2474 	mrioc->facts.who_init = facts_data->who_init;
2475 	mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
2476 	mrioc->facts.personality = (facts_flags &
2477 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
2478 	mrioc->facts.dma_mask = (facts_flags &
2479 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
2480 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
2481 	mrioc->facts.protocol_flags = facts_data->protocol_flags;
2482 	mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
2483 	mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_request);
2484 	mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
2485 	mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
2486 	mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
2487 	mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
2488 	mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
2489 	mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
2490 	mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds);
2491 	mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds);
2492 	mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
2493 	mrioc->facts.max_pcie_switches =
2494 	    le16_to_cpu(facts_data->max_pcie_switches);
2495 	mrioc->facts.max_sasexpanders =
2496 	    le16_to_cpu(facts_data->max_sas_expanders);
2497 	mrioc->facts.max_sasinitiators =
2498 	    le16_to_cpu(facts_data->max_sas_initiators);
2499 	mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
2500 	mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
2501 	mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
2502 	mrioc->facts.max_op_req_q =
2503 	    le16_to_cpu(facts_data->max_operational_request_queues);
2504 	mrioc->facts.max_op_reply_q =
2505 	    le16_to_cpu(facts_data->max_operational_reply_queues);
2506 	mrioc->facts.ioc_capabilities =
2507 	    le32_to_cpu(facts_data->ioc_capabilities);
2508 	mrioc->facts.fw_ver.build_num =
2509 	    le16_to_cpu(facts_data->fw_version.build_num);
2510 	mrioc->facts.fw_ver.cust_id =
2511 	    le16_to_cpu(facts_data->fw_version.customer_id);
2512 	mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
2513 	mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
2514 	mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
2515 	mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
2516 	mrioc->msix_count = min_t(int, mrioc->msix_count,
2517 	    mrioc->facts.max_msix_vectors);
2518 	mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
2519 	mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
2520 	mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
2521 	mrioc->facts.shutdown_timeout =
2522 	    le16_to_cpu(facts_data->shutdown_timeout);
2523 
2524 	ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
2525 	    mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
2526 	    mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
2527 	ioc_info(mrioc,
2528 	    "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n",
2529 	    mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
2530 	    mrioc->facts.max_msix_vectors, mrioc->facts.max_perids);
2531 	ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
2532 	    mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
2533 	    mrioc->facts.sge_mod_shift);
2534 	ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
2535 	    mrioc->facts.dma_mask, (facts_flags &
2536 	    MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
2537 
2538 	mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
2539 
2540 	if (reset_devices)
2541 		mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
2542 		    MPI3MR_HOST_IOS_KDUMP);
2543 }
2544 
2545 /**
2546  * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
2547  * @mrioc: Adapter instance reference
2548  *
2549  * Allocate and initialize the reply free buffers, sense
2550  * buffers, reply free queue and sense buffer queue.
2551  *
2552  * Return: 0 on success, non-zero on failures.
2553  */
2554 static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
2555 {
2556 	int retval = 0;
2557 	u32 sz, i;
2558 	dma_addr_t phy_addr;
2559 
2560 	if (mrioc->init_cmds.reply)
2561 		goto post_reply_sbuf;
2562 
2563 	mrioc->init_cmds.reply = kzalloc(mrioc->facts.reply_sz, GFP_KERNEL);
2564 	if (!mrioc->init_cmds.reply)
2565 		goto out_failed;
2566 
2567 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
2568 		mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->facts.reply_sz,
2569 		    GFP_KERNEL);
2570 		if (!mrioc->dev_rmhs_cmds[i].reply)
2571 			goto out_failed;
2572 	}
2573 
2574 	mrioc->host_tm_cmds.reply = kzalloc(mrioc->facts.reply_sz, GFP_KERNEL);
2575 	if (!mrioc->host_tm_cmds.reply)
2576 		goto out_failed;
2577 
2578 	mrioc->dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
2579 	if (mrioc->facts.max_devhandle % 8)
2580 		mrioc->dev_handle_bitmap_sz++;
2581 	mrioc->removepend_bitmap = kzalloc(mrioc->dev_handle_bitmap_sz,
2582 	    GFP_KERNEL);
2583 	if (!mrioc->removepend_bitmap)
2584 		goto out_failed;
2585 
2586 	mrioc->devrem_bitmap_sz = MPI3MR_NUM_DEVRMCMD / 8;
2587 	if (MPI3MR_NUM_DEVRMCMD % 8)
2588 		mrioc->devrem_bitmap_sz++;
2589 	mrioc->devrem_bitmap = kzalloc(mrioc->devrem_bitmap_sz,
2590 	    GFP_KERNEL);
2591 	if (!mrioc->devrem_bitmap)
2592 		goto out_failed;
2593 
2594 	mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
2595 	mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
2596 	mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
2597 	mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
2598 
2599 	/* reply buffer pool, 16 byte align */
2600 	sz = mrioc->num_reply_bufs * mrioc->facts.reply_sz;
2601 	mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
2602 	    &mrioc->pdev->dev, sz, 16, 0);
2603 	if (!mrioc->reply_buf_pool) {
2604 		ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
2605 		goto out_failed;
2606 	}
2607 
2608 	mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
2609 	    &mrioc->reply_buf_dma);
2610 	if (!mrioc->reply_buf)
2611 		goto out_failed;
2612 
2613 	mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
2614 
2615 	/* reply free queue, 8 byte align */
2616 	sz = mrioc->reply_free_qsz * 8;
2617 	mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
2618 	    &mrioc->pdev->dev, sz, 8, 0);
2619 	if (!mrioc->reply_free_q_pool) {
2620 		ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
2621 		goto out_failed;
2622 	}
2623 	mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
2624 	    GFP_KERNEL, &mrioc->reply_free_q_dma);
2625 	if (!mrioc->reply_free_q)
2626 		goto out_failed;
2627 
2628 	/* sense buffer pool,  4 byte align */
2629 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2630 	mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
2631 	    &mrioc->pdev->dev, sz, 4, 0);
2632 	if (!mrioc->sense_buf_pool) {
2633 		ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
2634 		goto out_failed;
2635 	}
2636 	mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
2637 	    &mrioc->sense_buf_dma);
2638 	if (!mrioc->sense_buf)
2639 		goto out_failed;
2640 
2641 	/* sense buffer queue, 8 byte align */
2642 	sz = mrioc->sense_buf_q_sz * 8;
2643 	mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
2644 	    &mrioc->pdev->dev, sz, 8, 0);
2645 	if (!mrioc->sense_buf_q_pool) {
2646 		ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
2647 		goto out_failed;
2648 	}
2649 	mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
2650 	    GFP_KERNEL, &mrioc->sense_buf_q_dma);
2651 	if (!mrioc->sense_buf_q)
2652 		goto out_failed;
2653 
2654 post_reply_sbuf:
2655 	sz = mrioc->num_reply_bufs * mrioc->facts.reply_sz;
2656 	ioc_info(mrioc,
2657 	    "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2658 	    mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->facts.reply_sz,
2659 	    (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
2660 	sz = mrioc->reply_free_qsz * 8;
2661 	ioc_info(mrioc,
2662 	    "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2663 	    mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
2664 	    (unsigned long long)mrioc->reply_free_q_dma);
2665 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2666 	ioc_info(mrioc,
2667 	    "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
2668 	    mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ,
2669 	    (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
2670 	sz = mrioc->sense_buf_q_sz * 8;
2671 	ioc_info(mrioc,
2672 	    "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
2673 	    mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
2674 	    (unsigned long long)mrioc->sense_buf_q_dma);
2675 
2676 	/* initialize Reply buffer Queue */
2677 	for (i = 0, phy_addr = mrioc->reply_buf_dma;
2678 	    i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->facts.reply_sz)
2679 		mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
2680 	mrioc->reply_free_q[i] = cpu_to_le64(0);
2681 
2682 	/* initialize Sense Buffer Queue */
2683 	for (i = 0, phy_addr = mrioc->sense_buf_dma;
2684 	    i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ)
2685 		mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
2686 	mrioc->sense_buf_q[i] = cpu_to_le64(0);
2687 	return retval;
2688 
2689 out_failed:
2690 	retval = -1;
2691 	return retval;
2692 }
2693 
2694 /**
2695  * mpi3mr_issue_iocinit - Send IOC Init
2696  * @mrioc: Adapter instance reference
2697  *
2698  * Issue IOC Init MPI request through admin queue and wait for
2699  * the completion of it or time out.
2700  *
2701  * Return: 0 on success, non-zero on failures.
2702  */
2703 static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
2704 {
2705 	struct mpi3_ioc_init_request iocinit_req;
2706 	struct mpi3_driver_info_layout *drv_info;
2707 	dma_addr_t data_dma;
2708 	u32 data_len = sizeof(*drv_info);
2709 	int retval = 0;
2710 	ktime_t current_time;
2711 
2712 	drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2713 	    GFP_KERNEL);
2714 	if (!drv_info) {
2715 		retval = -1;
2716 		goto out;
2717 	}
2718 	drv_info->information_length = cpu_to_le32(data_len);
2719 	strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
2720 	strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
2721 	strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
2722 	strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
2723 	strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
2724 	strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE,
2725 	    sizeof(drv_info->driver_release_date));
2726 	drv_info->driver_capabilities = 0;
2727 	memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
2728 	    sizeof(mrioc->driver_info));
2729 
2730 	memset(&iocinit_req, 0, sizeof(iocinit_req));
2731 	mutex_lock(&mrioc->init_cmds.mutex);
2732 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2733 		retval = -1;
2734 		ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
2735 		mutex_unlock(&mrioc->init_cmds.mutex);
2736 		goto out;
2737 	}
2738 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2739 	mrioc->init_cmds.is_waiting = 1;
2740 	mrioc->init_cmds.callback = NULL;
2741 	iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2742 	iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
2743 	iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
2744 	iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
2745 	iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
2746 	iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
2747 	iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
2748 	iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
2749 	iocinit_req.reply_free_queue_address =
2750 	    cpu_to_le64(mrioc->reply_free_q_dma);
2751 	iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ);
2752 	iocinit_req.sense_buffer_free_queue_depth =
2753 	    cpu_to_le16(mrioc->sense_buf_q_sz);
2754 	iocinit_req.sense_buffer_free_queue_address =
2755 	    cpu_to_le64(mrioc->sense_buf_q_dma);
2756 	iocinit_req.driver_information_address = cpu_to_le64(data_dma);
2757 
2758 	current_time = ktime_get_real();
2759 	iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
2760 
2761 	init_completion(&mrioc->init_cmds.done);
2762 	retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
2763 	    sizeof(iocinit_req), 1);
2764 	if (retval) {
2765 		ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
2766 		goto out_unlock;
2767 	}
2768 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2769 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2770 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2771 		mpi3mr_check_rh_fault_ioc(mrioc,
2772 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
2773 		ioc_err(mrioc, "ioc_init timed out\n");
2774 		retval = -1;
2775 		goto out_unlock;
2776 	}
2777 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2778 	    != MPI3_IOCSTATUS_SUCCESS) {
2779 		ioc_err(mrioc,
2780 		    "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2781 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2782 		    mrioc->init_cmds.ioc_loginfo);
2783 		retval = -1;
2784 		goto out_unlock;
2785 	}
2786 
2787 out_unlock:
2788 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2789 	mutex_unlock(&mrioc->init_cmds.mutex);
2790 
2791 out:
2792 	if (drv_info)
2793 		dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
2794 		    data_dma);
2795 
2796 	return retval;
2797 }
2798 
2799 /**
2800  * mpi3mr_unmask_events - Unmask events in event mask bitmap
2801  * @mrioc: Adapter instance reference
2802  * @event: MPI event ID
2803  *
2804  * Un mask the specific event by resetting the event_mask
2805  * bitmap.
2806  *
2807  * Return: 0 on success, non-zero on failures.
2808  */
2809 static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event)
2810 {
2811 	u32 desired_event;
2812 	u8 word;
2813 
2814 	if (event >= 128)
2815 		return;
2816 
2817 	desired_event = (1 << (event % 32));
2818 	word = event / 32;
2819 
2820 	mrioc->event_masks[word] &= ~desired_event;
2821 }
2822 
2823 /**
2824  * mpi3mr_issue_event_notification - Send event notification
2825  * @mrioc: Adapter instance reference
2826  *
2827  * Issue event notification MPI request through admin queue and
2828  * wait for the completion of it or time out.
2829  *
2830  * Return: 0 on success, non-zero on failures.
2831  */
2832 static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc)
2833 {
2834 	struct mpi3_event_notification_request evtnotify_req;
2835 	int retval = 0;
2836 	u8 i;
2837 
2838 	memset(&evtnotify_req, 0, sizeof(evtnotify_req));
2839 	mutex_lock(&mrioc->init_cmds.mutex);
2840 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2841 		retval = -1;
2842 		ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n");
2843 		mutex_unlock(&mrioc->init_cmds.mutex);
2844 		goto out;
2845 	}
2846 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2847 	mrioc->init_cmds.is_waiting = 1;
2848 	mrioc->init_cmds.callback = NULL;
2849 	evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2850 	evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION;
2851 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2852 		evtnotify_req.event_masks[i] =
2853 		    cpu_to_le32(mrioc->event_masks[i]);
2854 	init_completion(&mrioc->init_cmds.done);
2855 	retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req,
2856 	    sizeof(evtnotify_req), 1);
2857 	if (retval) {
2858 		ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n");
2859 		goto out_unlock;
2860 	}
2861 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2862 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2863 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2864 		ioc_err(mrioc, "event notification timed out\n");
2865 		mpi3mr_check_rh_fault_ioc(mrioc,
2866 		    MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
2867 		retval = -1;
2868 		goto out_unlock;
2869 	}
2870 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2871 	    != MPI3_IOCSTATUS_SUCCESS) {
2872 		ioc_err(mrioc,
2873 		    "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2874 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2875 		    mrioc->init_cmds.ioc_loginfo);
2876 		retval = -1;
2877 		goto out_unlock;
2878 	}
2879 
2880 out_unlock:
2881 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2882 	mutex_unlock(&mrioc->init_cmds.mutex);
2883 out:
2884 	return retval;
2885 }
2886 
2887 /**
2888  * mpi3mr_send_event_ack - Send event acknowledgment
2889  * @mrioc: Adapter instance reference
2890  * @event: MPI3 event ID
2891  * @event_ctx: Event context
2892  *
2893  * Send event acknowledgment through admin queue and wait for
2894  * it to complete.
2895  *
2896  * Return: 0 on success, non-zero on failures.
2897  */
2898 int mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2899 	u32 event_ctx)
2900 {
2901 	struct mpi3_event_ack_request evtack_req;
2902 	int retval = 0;
2903 
2904 	memset(&evtack_req, 0, sizeof(evtack_req));
2905 	mutex_lock(&mrioc->init_cmds.mutex);
2906 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2907 		retval = -1;
2908 		ioc_err(mrioc, "Send EvtAck: Init command is in use\n");
2909 		mutex_unlock(&mrioc->init_cmds.mutex);
2910 		goto out;
2911 	}
2912 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2913 	mrioc->init_cmds.is_waiting = 1;
2914 	mrioc->init_cmds.callback = NULL;
2915 	evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2916 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2917 	evtack_req.event = event;
2918 	evtack_req.event_context = cpu_to_le32(event_ctx);
2919 
2920 	init_completion(&mrioc->init_cmds.done);
2921 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2922 	    sizeof(evtack_req), 1);
2923 	if (retval) {
2924 		ioc_err(mrioc, "Send EvtAck: Admin Post failed\n");
2925 		goto out_unlock;
2926 	}
2927 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2928 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2929 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2930 		ioc_err(mrioc, "Issue EvtNotify: command timed out\n");
2931 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
2932 			mpi3mr_soft_reset_handler(mrioc,
2933 			    MPI3MR_RESET_FROM_EVTACK_TIMEOUT, 1);
2934 		retval = -1;
2935 		goto out_unlock;
2936 	}
2937 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2938 	    != MPI3_IOCSTATUS_SUCCESS) {
2939 		ioc_err(mrioc,
2940 		    "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2941 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2942 		    mrioc->init_cmds.ioc_loginfo);
2943 		retval = -1;
2944 		goto out_unlock;
2945 	}
2946 
2947 out_unlock:
2948 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2949 	mutex_unlock(&mrioc->init_cmds.mutex);
2950 out:
2951 	return retval;
2952 }
2953 
2954 /**
2955  * mpi3mr_alloc_chain_bufs - Allocate chain buffers
2956  * @mrioc: Adapter instance reference
2957  *
2958  * Allocate chain buffers and set a bitmap to indicate free
2959  * chain buffers. Chain buffers are used to pass the SGE
2960  * information along with MPI3 SCSI IO requests for host I/O.
2961  *
2962  * Return: 0 on success, non-zero on failure
2963  */
2964 static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
2965 {
2966 	int retval = 0;
2967 	u32 sz, i;
2968 	u16 num_chains;
2969 
2970 	num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
2971 
2972 	if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION
2973 	    | SHOST_DIX_TYPE1_PROTECTION
2974 	    | SHOST_DIX_TYPE2_PROTECTION
2975 	    | SHOST_DIX_TYPE3_PROTECTION))
2976 		num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR);
2977 
2978 	mrioc->chain_buf_count = num_chains;
2979 	sz = sizeof(struct chain_element) * num_chains;
2980 	mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
2981 	if (!mrioc->chain_sgl_list)
2982 		goto out_failed;
2983 
2984 	sz = MPI3MR_PAGE_SIZE_4K;
2985 	mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
2986 	    &mrioc->pdev->dev, sz, 16, 0);
2987 	if (!mrioc->chain_buf_pool) {
2988 		ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
2989 		goto out_failed;
2990 	}
2991 
2992 	for (i = 0; i < num_chains; i++) {
2993 		mrioc->chain_sgl_list[i].addr =
2994 		    dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
2995 		    &mrioc->chain_sgl_list[i].dma_addr);
2996 
2997 		if (!mrioc->chain_sgl_list[i].addr)
2998 			goto out_failed;
2999 	}
3000 	mrioc->chain_bitmap_sz = num_chains / 8;
3001 	if (num_chains % 8)
3002 		mrioc->chain_bitmap_sz++;
3003 	mrioc->chain_bitmap = kzalloc(mrioc->chain_bitmap_sz, GFP_KERNEL);
3004 	if (!mrioc->chain_bitmap)
3005 		goto out_failed;
3006 	return retval;
3007 out_failed:
3008 	retval = -1;
3009 	return retval;
3010 }
3011 
3012 /**
3013  * mpi3mr_port_enable_complete - Mark port enable complete
3014  * @mrioc: Adapter instance reference
3015  * @drv_cmd: Internal command tracker
3016  *
3017  * Call back for asynchronous port enable request sets the
3018  * driver command to indicate port enable request is complete.
3019  *
3020  * Return: Nothing
3021  */
3022 static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc,
3023 	struct mpi3mr_drv_cmd *drv_cmd)
3024 {
3025 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3026 	drv_cmd->callback = NULL;
3027 	mrioc->scan_failed = drv_cmd->ioc_status;
3028 	mrioc->scan_started = 0;
3029 }
3030 
3031 /**
3032  * mpi3mr_issue_port_enable - Issue Port Enable
3033  * @mrioc: Adapter instance reference
3034  * @async: Flag to wait for completion or not
3035  *
3036  * Issue Port Enable MPI request through admin queue and if the
3037  * async flag is not set wait for the completion of the port
3038  * enable or time out.
3039  *
3040  * Return: 0 on success, non-zero on failures.
3041  */
3042 int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async)
3043 {
3044 	struct mpi3_port_enable_request pe_req;
3045 	int retval = 0;
3046 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3047 
3048 	memset(&pe_req, 0, sizeof(pe_req));
3049 	mutex_lock(&mrioc->init_cmds.mutex);
3050 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3051 		retval = -1;
3052 		ioc_err(mrioc, "Issue PortEnable: Init command is in use\n");
3053 		mutex_unlock(&mrioc->init_cmds.mutex);
3054 		goto out;
3055 	}
3056 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3057 	if (async) {
3058 		mrioc->init_cmds.is_waiting = 0;
3059 		mrioc->init_cmds.callback = mpi3mr_port_enable_complete;
3060 	} else {
3061 		mrioc->init_cmds.is_waiting = 1;
3062 		mrioc->init_cmds.callback = NULL;
3063 		init_completion(&mrioc->init_cmds.done);
3064 	}
3065 	pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3066 	pe_req.function = MPI3_FUNCTION_PORT_ENABLE;
3067 
3068 	retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1);
3069 	if (retval) {
3070 		ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n");
3071 		goto out_unlock;
3072 	}
3073 	if (async) {
3074 		mutex_unlock(&mrioc->init_cmds.mutex);
3075 		goto out;
3076 	}
3077 
3078 	wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ));
3079 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3080 		ioc_err(mrioc, "port enable timed out\n");
3081 		retval = -1;
3082 		mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT);
3083 		goto out_unlock;
3084 	}
3085 	mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds);
3086 
3087 out_unlock:
3088 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3089 	mutex_unlock(&mrioc->init_cmds.mutex);
3090 out:
3091 	return retval;
3092 }
3093 
3094 /* Protocol type to name mapper structure */
3095 static const struct {
3096 	u8 protocol;
3097 	char *name;
3098 } mpi3mr_protocols[] = {
3099 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" },
3100 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" },
3101 	{ MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" },
3102 };
3103 
3104 /* Capability to name mapper structure*/
3105 static const struct {
3106 	u32 capability;
3107 	char *name;
3108 } mpi3mr_capabilities[] = {
3109 	{ MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE, "RAID" },
3110 };
3111 
3112 /**
3113  * mpi3mr_print_ioc_info - Display controller information
3114  * @mrioc: Adapter instance reference
3115  *
3116  * Display controller personalit, capability, supported
3117  * protocols etc.
3118  *
3119  * Return: Nothing
3120  */
3121 static void
3122 mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc)
3123 {
3124 	int i = 0, bytes_written = 0;
3125 	char personality[16];
3126 	char protocol[50] = {0};
3127 	char capabilities[100] = {0};
3128 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
3129 
3130 	switch (mrioc->facts.personality) {
3131 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
3132 		strncpy(personality, "Enhanced HBA", sizeof(personality));
3133 		break;
3134 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
3135 		strncpy(personality, "RAID", sizeof(personality));
3136 		break;
3137 	default:
3138 		strncpy(personality, "Unknown", sizeof(personality));
3139 		break;
3140 	}
3141 
3142 	ioc_info(mrioc, "Running in %s Personality", personality);
3143 
3144 	ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n",
3145 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
3146 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
3147 
3148 	for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) {
3149 		if (mrioc->facts.protocol_flags &
3150 		    mpi3mr_protocols[i].protocol) {
3151 			bytes_written += scnprintf(protocol + bytes_written,
3152 				    sizeof(protocol) - bytes_written, "%s%s",
3153 				    bytes_written ? "," : "",
3154 				    mpi3mr_protocols[i].name);
3155 		}
3156 	}
3157 
3158 	bytes_written = 0;
3159 	for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) {
3160 		if (mrioc->facts.protocol_flags &
3161 		    mpi3mr_capabilities[i].capability) {
3162 			bytes_written += scnprintf(capabilities + bytes_written,
3163 				    sizeof(capabilities) - bytes_written, "%s%s",
3164 				    bytes_written ? "," : "",
3165 				    mpi3mr_capabilities[i].name);
3166 		}
3167 	}
3168 
3169 	ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n",
3170 		 protocol, capabilities);
3171 }
3172 
3173 /**
3174  * mpi3mr_cleanup_resources - Free PCI resources
3175  * @mrioc: Adapter instance reference
3176  *
3177  * Unmap PCI device memory and disable PCI device.
3178  *
3179  * Return: 0 on success and non-zero on failure.
3180  */
3181 void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
3182 {
3183 	struct pci_dev *pdev = mrioc->pdev;
3184 
3185 	mpi3mr_cleanup_isr(mrioc);
3186 
3187 	if (mrioc->sysif_regs) {
3188 		iounmap((void __iomem *)mrioc->sysif_regs);
3189 		mrioc->sysif_regs = NULL;
3190 	}
3191 
3192 	if (pci_is_enabled(pdev)) {
3193 		if (mrioc->bars)
3194 			pci_release_selected_regions(pdev, mrioc->bars);
3195 		pci_disable_device(pdev);
3196 	}
3197 }
3198 
3199 /**
3200  * mpi3mr_setup_resources - Enable PCI resources
3201  * @mrioc: Adapter instance reference
3202  *
3203  * Enable PCI device memory, MSI-x registers and set DMA mask.
3204  *
3205  * Return: 0 on success and non-zero on failure.
3206  */
3207 int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
3208 {
3209 	struct pci_dev *pdev = mrioc->pdev;
3210 	u32 memap_sz = 0;
3211 	int i, retval = 0, capb = 0;
3212 	u16 message_control;
3213 	u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
3214 	    (((dma_get_required_mask(&pdev->dev) > DMA_BIT_MASK(32)) &&
3215 	    (sizeof(dma_addr_t) > 4)) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
3216 
3217 	if (pci_enable_device_mem(pdev)) {
3218 		ioc_err(mrioc, "pci_enable_device_mem: failed\n");
3219 		retval = -ENODEV;
3220 		goto out_failed;
3221 	}
3222 
3223 	capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3224 	if (!capb) {
3225 		ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
3226 		retval = -ENODEV;
3227 		goto out_failed;
3228 	}
3229 	mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3230 
3231 	if (pci_request_selected_regions(pdev, mrioc->bars,
3232 	    mrioc->driver_name)) {
3233 		ioc_err(mrioc, "pci_request_selected_regions: failed\n");
3234 		retval = -ENODEV;
3235 		goto out_failed;
3236 	}
3237 
3238 	for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
3239 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3240 			mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
3241 			memap_sz = pci_resource_len(pdev, i);
3242 			mrioc->sysif_regs =
3243 			    ioremap(mrioc->sysif_regs_phys, memap_sz);
3244 			break;
3245 		}
3246 	}
3247 
3248 	pci_set_master(pdev);
3249 
3250 	retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
3251 	if (retval) {
3252 		if (dma_mask != DMA_BIT_MASK(32)) {
3253 			ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
3254 			dma_mask = DMA_BIT_MASK(32);
3255 			retval = dma_set_mask_and_coherent(&pdev->dev,
3256 			    dma_mask);
3257 		}
3258 		if (retval) {
3259 			mrioc->dma_mask = 0;
3260 			ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
3261 			goto out_failed;
3262 		}
3263 	}
3264 	mrioc->dma_mask = dma_mask;
3265 
3266 	if (!mrioc->sysif_regs) {
3267 		ioc_err(mrioc,
3268 		    "Unable to map adapter memory or resource not found\n");
3269 		retval = -EINVAL;
3270 		goto out_failed;
3271 	}
3272 
3273 	pci_read_config_word(pdev, capb + 2, &message_control);
3274 	mrioc->msix_count = (message_control & 0x3FF) + 1;
3275 
3276 	pci_save_state(pdev);
3277 
3278 	pci_set_drvdata(pdev, mrioc->shost);
3279 
3280 	mpi3mr_ioc_disable_intr(mrioc);
3281 
3282 	ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
3283 	    (unsigned long long)mrioc->sysif_regs_phys,
3284 	    mrioc->sysif_regs, memap_sz);
3285 	ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
3286 	    mrioc->msix_count);
3287 	return retval;
3288 
3289 out_failed:
3290 	mpi3mr_cleanup_resources(mrioc);
3291 	return retval;
3292 }
3293 
3294 /**
3295  * mpi3mr_init_ioc - Initialize the controller
3296  * @mrioc: Adapter instance reference
3297  * @init_type: Flag to indicate is the init_type
3298  *
3299  * This the controller initialization routine, executed either
3300  * after soft reset or from pci probe callback.
3301  * Setup the required resources, memory map the controller
3302  * registers, create admin and operational reply queue pairs,
3303  * allocate required memory for reply pool, sense buffer pool,
3304  * issue IOC init request to the firmware, unmask the events and
3305  * issue port enable to discover SAS/SATA/NVMe devies and RAID
3306  * volumes.
3307  *
3308  * Return: 0 on success and non-zero on failure.
3309  */
3310 int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc, u8 init_type)
3311 {
3312 	int retval = 0;
3313 	enum mpi3mr_iocstate ioc_state;
3314 	u64 base_info;
3315 	u32 timeout;
3316 	u32 ioc_status, ioc_config, i;
3317 	struct mpi3_ioc_facts_data facts_data;
3318 
3319 	mrioc->irqpoll_sleep = MPI3MR_IRQ_POLL_SLEEP;
3320 	mrioc->change_count = 0;
3321 	if (init_type == MPI3MR_IT_INIT) {
3322 		mrioc->cpu_count = num_online_cpus();
3323 		retval = mpi3mr_setup_resources(mrioc);
3324 		if (retval) {
3325 			ioc_err(mrioc, "Failed to setup resources:error %d\n",
3326 			    retval);
3327 			goto out_nocleanup;
3328 		}
3329 	}
3330 
3331 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3332 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
3333 
3334 	ioc_info(mrioc, "SOD status %x configuration %x\n",
3335 	    ioc_status, ioc_config);
3336 
3337 	base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
3338 	ioc_info(mrioc, "SOD base_info %llx\n",	base_info);
3339 
3340 	/*The timeout value is in 2sec unit, changing it to seconds*/
3341 	mrioc->ready_timeout =
3342 	    ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
3343 	    MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
3344 
3345 	ioc_info(mrioc, "IOC ready timeout %d\n", mrioc->ready_timeout);
3346 
3347 	ioc_state = mpi3mr_get_iocstate(mrioc);
3348 	ioc_info(mrioc, "IOC in %s state during detection\n",
3349 	    mpi3mr_iocstate_name(ioc_state));
3350 
3351 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
3352 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
3353 		timeout = mrioc->ready_timeout * 10;
3354 		do {
3355 			msleep(100);
3356 		} while (--timeout);
3357 
3358 		ioc_state = mpi3mr_get_iocstate(mrioc);
3359 		ioc_info(mrioc,
3360 		    "IOC in %s state after waiting for reset time\n",
3361 		    mpi3mr_iocstate_name(ioc_state));
3362 	}
3363 
3364 	if (ioc_state == MRIOC_STATE_READY) {
3365 		retval = mpi3mr_issue_and_process_mur(mrioc,
3366 		    MPI3MR_RESET_FROM_BRINGUP);
3367 		if (retval) {
3368 			ioc_err(mrioc, "Failed to MU reset IOC error %d\n",
3369 			    retval);
3370 		}
3371 		ioc_state = mpi3mr_get_iocstate(mrioc);
3372 	}
3373 	if (ioc_state != MRIOC_STATE_RESET) {
3374 		mpi3mr_print_fault_info(mrioc);
3375 		retval = mpi3mr_issue_reset(mrioc,
3376 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
3377 		    MPI3MR_RESET_FROM_BRINGUP);
3378 		if (retval) {
3379 			ioc_err(mrioc,
3380 			    "%s :Failed to soft reset IOC error %d\n",
3381 			    __func__, retval);
3382 			goto out_failed;
3383 		}
3384 	}
3385 	ioc_state = mpi3mr_get_iocstate(mrioc);
3386 	if (ioc_state != MRIOC_STATE_RESET) {
3387 		retval = -1;
3388 		ioc_err(mrioc, "Cannot bring IOC to reset state\n");
3389 		goto out_failed;
3390 	}
3391 
3392 	retval = mpi3mr_setup_admin_qpair(mrioc);
3393 	if (retval) {
3394 		ioc_err(mrioc, "Failed to setup admin Qs: error %d\n",
3395 		    retval);
3396 		goto out_failed;
3397 	}
3398 
3399 	retval = mpi3mr_bring_ioc_ready(mrioc);
3400 	if (retval) {
3401 		ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
3402 		    retval);
3403 		goto out_failed;
3404 	}
3405 
3406 	if (init_type != MPI3MR_IT_RESET) {
3407 		retval = mpi3mr_setup_isr(mrioc, 1);
3408 		if (retval) {
3409 			ioc_err(mrioc, "Failed to setup ISR error %d\n",
3410 			    retval);
3411 			goto out_failed;
3412 		}
3413 	} else
3414 		mpi3mr_ioc_enable_intr(mrioc);
3415 
3416 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3417 	if (retval) {
3418 		ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
3419 		    retval);
3420 		goto out_failed;
3421 	}
3422 
3423 	mpi3mr_process_factsdata(mrioc, &facts_data);
3424 	if (init_type == MPI3MR_IT_INIT) {
3425 		retval = mpi3mr_check_reset_dma_mask(mrioc);
3426 		if (retval) {
3427 			ioc_err(mrioc, "Resetting dma mask failed %d\n",
3428 			    retval);
3429 			goto out_failed;
3430 		}
3431 	}
3432 
3433 	mpi3mr_print_ioc_info(mrioc);
3434 
3435 	retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
3436 	if (retval) {
3437 		ioc_err(mrioc,
3438 		    "%s :Failed to allocated reply sense buffers %d\n",
3439 		    __func__, retval);
3440 		goto out_failed;
3441 	}
3442 
3443 	if (init_type == MPI3MR_IT_INIT) {
3444 		retval = mpi3mr_alloc_chain_bufs(mrioc);
3445 		if (retval) {
3446 			ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
3447 			    retval);
3448 			goto out_failed;
3449 		}
3450 	}
3451 
3452 	retval = mpi3mr_issue_iocinit(mrioc);
3453 	if (retval) {
3454 		ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
3455 		    retval);
3456 		goto out_failed;
3457 	}
3458 	mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
3459 	writel(mrioc->reply_free_queue_host_index,
3460 	    &mrioc->sysif_regs->reply_free_host_index);
3461 
3462 	mrioc->sbq_host_index = mrioc->num_sense_bufs;
3463 	writel(mrioc->sbq_host_index,
3464 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
3465 
3466 	retval = mpi3mr_print_pkg_ver(mrioc);
3467 	if (retval) {
3468 		ioc_err(mrioc, "failed to get package version\n");
3469 		goto out_failed;
3470 	}
3471 
3472 	if (init_type != MPI3MR_IT_RESET) {
3473 		retval = mpi3mr_setup_isr(mrioc, 0);
3474 		if (retval) {
3475 			ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
3476 			    retval);
3477 			goto out_failed;
3478 		}
3479 	}
3480 
3481 	retval = mpi3mr_create_op_queues(mrioc);
3482 	if (retval) {
3483 		ioc_err(mrioc, "Failed to create OpQueues error %d\n",
3484 		    retval);
3485 		goto out_failed;
3486 	}
3487 
3488 	if ((init_type != MPI3MR_IT_INIT) &&
3489 	    (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q)) {
3490 		retval = -1;
3491 		ioc_err(mrioc,
3492 		    "Cannot create minimum number of OpQueues expected:%d created:%d\n",
3493 		    mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q);
3494 		goto out_failed;
3495 	}
3496 
3497 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3498 		mrioc->event_masks[i] = -1;
3499 
3500 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED);
3501 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED);
3502 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
3503 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
3504 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3505 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY);
3506 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
3507 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
3508 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
3509 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION);
3510 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT);
3511 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE);
3512 
3513 	retval = mpi3mr_issue_event_notification(mrioc);
3514 	if (retval) {
3515 		ioc_err(mrioc, "Failed to issue event notification %d\n",
3516 		    retval);
3517 		goto out_failed;
3518 	}
3519 
3520 	if (init_type != MPI3MR_IT_INIT) {
3521 		ioc_info(mrioc, "Issuing Port Enable\n");
3522 		retval = mpi3mr_issue_port_enable(mrioc, 0);
3523 		if (retval) {
3524 			ioc_err(mrioc, "Failed to issue port enable %d\n",
3525 			    retval);
3526 			goto out_failed;
3527 		}
3528 	}
3529 	return retval;
3530 
3531 out_failed:
3532 	if (init_type == MPI3MR_IT_INIT)
3533 		mpi3mr_cleanup_ioc(mrioc, MPI3MR_COMPLETE_CLEANUP);
3534 	else
3535 		mpi3mr_cleanup_ioc(mrioc, MPI3MR_REINIT_FAILURE);
3536 out_nocleanup:
3537 	return retval;
3538 }
3539 
3540 /**
3541  * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's
3542  *					segments
3543  * @mrioc: Adapter instance reference
3544  * @qidx: Operational reply queue index
3545  *
3546  * Return: Nothing.
3547  */
3548 static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3549 {
3550 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
3551 	struct segments *segments;
3552 	int i, size;
3553 
3554 	if (!op_reply_q->q_segments)
3555 		return;
3556 
3557 	size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz;
3558 	segments = op_reply_q->q_segments;
3559 	for (i = 0; i < op_reply_q->num_segments; i++)
3560 		memset(segments[i].segment, 0, size);
3561 }
3562 
3563 /**
3564  * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's
3565  *					segments
3566  * @mrioc: Adapter instance reference
3567  * @qidx: Operational request queue index
3568  *
3569  * Return: Nothing.
3570  */
3571 static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3572 {
3573 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
3574 	struct segments *segments;
3575 	int i, size;
3576 
3577 	if (!op_req_q->q_segments)
3578 		return;
3579 
3580 	size = op_req_q->segment_qd * mrioc->facts.op_req_sz;
3581 	segments = op_req_q->q_segments;
3582 	for (i = 0; i < op_req_q->num_segments; i++)
3583 		memset(segments[i].segment, 0, size);
3584 }
3585 
3586 /**
3587  * mpi3mr_memset_buffers - memset memory for a controller
3588  * @mrioc: Adapter instance reference
3589  *
3590  * clear all the memory allocated for a controller, typically
3591  * called post reset to reuse the memory allocated during the
3592  * controller init.
3593  *
3594  * Return: Nothing.
3595  */
3596 void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)
3597 {
3598 	u16 i;
3599 
3600 	memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz);
3601 	memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz);
3602 
3603 	memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply));
3604 	memset(mrioc->host_tm_cmds.reply, 0,
3605 	    sizeof(*mrioc->host_tm_cmds.reply));
3606 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
3607 		memset(mrioc->dev_rmhs_cmds[i].reply, 0,
3608 		    sizeof(*mrioc->dev_rmhs_cmds[i].reply));
3609 	memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
3610 	memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
3611 
3612 	for (i = 0; i < mrioc->num_queues; i++) {
3613 		mrioc->op_reply_qinfo[i].qid = 0;
3614 		mrioc->op_reply_qinfo[i].ci = 0;
3615 		mrioc->op_reply_qinfo[i].num_replies = 0;
3616 		mrioc->op_reply_qinfo[i].ephase = 0;
3617 		atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
3618 		atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0);
3619 		mpi3mr_memset_op_reply_q_buffers(mrioc, i);
3620 
3621 		mrioc->req_qinfo[i].ci = 0;
3622 		mrioc->req_qinfo[i].pi = 0;
3623 		mrioc->req_qinfo[i].num_requests = 0;
3624 		mrioc->req_qinfo[i].qid = 0;
3625 		mrioc->req_qinfo[i].reply_qid = 0;
3626 		spin_lock_init(&mrioc->req_qinfo[i].q_lock);
3627 		mpi3mr_memset_op_req_q_buffers(mrioc, i);
3628 	}
3629 }
3630 
3631 /**
3632  * mpi3mr_free_mem - Free memory allocated for a controller
3633  * @mrioc: Adapter instance reference
3634  *
3635  * Free all the memory allocated for a controller.
3636  *
3637  * Return: Nothing.
3638  */
3639 static void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
3640 {
3641 	u16 i;
3642 	struct mpi3mr_intr_info *intr_info;
3643 
3644 	if (mrioc->sense_buf_pool) {
3645 		if (mrioc->sense_buf)
3646 			dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
3647 			    mrioc->sense_buf_dma);
3648 		dma_pool_destroy(mrioc->sense_buf_pool);
3649 		mrioc->sense_buf = NULL;
3650 		mrioc->sense_buf_pool = NULL;
3651 	}
3652 	if (mrioc->sense_buf_q_pool) {
3653 		if (mrioc->sense_buf_q)
3654 			dma_pool_free(mrioc->sense_buf_q_pool,
3655 			    mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
3656 		dma_pool_destroy(mrioc->sense_buf_q_pool);
3657 		mrioc->sense_buf_q = NULL;
3658 		mrioc->sense_buf_q_pool = NULL;
3659 	}
3660 
3661 	if (mrioc->reply_buf_pool) {
3662 		if (mrioc->reply_buf)
3663 			dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
3664 			    mrioc->reply_buf_dma);
3665 		dma_pool_destroy(mrioc->reply_buf_pool);
3666 		mrioc->reply_buf = NULL;
3667 		mrioc->reply_buf_pool = NULL;
3668 	}
3669 	if (mrioc->reply_free_q_pool) {
3670 		if (mrioc->reply_free_q)
3671 			dma_pool_free(mrioc->reply_free_q_pool,
3672 			    mrioc->reply_free_q, mrioc->reply_free_q_dma);
3673 		dma_pool_destroy(mrioc->reply_free_q_pool);
3674 		mrioc->reply_free_q = NULL;
3675 		mrioc->reply_free_q_pool = NULL;
3676 	}
3677 
3678 	for (i = 0; i < mrioc->num_op_req_q; i++)
3679 		mpi3mr_free_op_req_q_segments(mrioc, i);
3680 
3681 	for (i = 0; i < mrioc->num_op_reply_q; i++)
3682 		mpi3mr_free_op_reply_q_segments(mrioc, i);
3683 
3684 	for (i = 0; i < mrioc->intr_info_count; i++) {
3685 		intr_info = mrioc->intr_info + i;
3686 		intr_info->op_reply_q = NULL;
3687 	}
3688 
3689 	kfree(mrioc->req_qinfo);
3690 	mrioc->req_qinfo = NULL;
3691 	mrioc->num_op_req_q = 0;
3692 
3693 	kfree(mrioc->op_reply_qinfo);
3694 	mrioc->op_reply_qinfo = NULL;
3695 	mrioc->num_op_reply_q = 0;
3696 
3697 	kfree(mrioc->init_cmds.reply);
3698 	mrioc->init_cmds.reply = NULL;
3699 
3700 	kfree(mrioc->host_tm_cmds.reply);
3701 	mrioc->host_tm_cmds.reply = NULL;
3702 
3703 	kfree(mrioc->removepend_bitmap);
3704 	mrioc->removepend_bitmap = NULL;
3705 
3706 	kfree(mrioc->devrem_bitmap);
3707 	mrioc->devrem_bitmap = NULL;
3708 
3709 	kfree(mrioc->chain_bitmap);
3710 	mrioc->chain_bitmap = NULL;
3711 
3712 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
3713 		kfree(mrioc->dev_rmhs_cmds[i].reply);
3714 		mrioc->dev_rmhs_cmds[i].reply = NULL;
3715 	}
3716 
3717 	if (mrioc->chain_buf_pool) {
3718 		for (i = 0; i < mrioc->chain_buf_count; i++) {
3719 			if (mrioc->chain_sgl_list[i].addr) {
3720 				dma_pool_free(mrioc->chain_buf_pool,
3721 				    mrioc->chain_sgl_list[i].addr,
3722 				    mrioc->chain_sgl_list[i].dma_addr);
3723 				mrioc->chain_sgl_list[i].addr = NULL;
3724 			}
3725 		}
3726 		dma_pool_destroy(mrioc->chain_buf_pool);
3727 		mrioc->chain_buf_pool = NULL;
3728 	}
3729 
3730 	kfree(mrioc->chain_sgl_list);
3731 	mrioc->chain_sgl_list = NULL;
3732 
3733 	if (mrioc->admin_reply_base) {
3734 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
3735 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
3736 		mrioc->admin_reply_base = NULL;
3737 	}
3738 	if (mrioc->admin_req_base) {
3739 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
3740 		    mrioc->admin_req_base, mrioc->admin_req_dma);
3741 		mrioc->admin_req_base = NULL;
3742 	}
3743 }
3744 
3745 /**
3746  * mpi3mr_issue_ioc_shutdown - shutdown controller
3747  * @mrioc: Adapter instance reference
3748  *
3749  * Send shutodwn notification to the controller and wait for the
3750  * shutdown_timeout for it to be completed.
3751  *
3752  * Return: Nothing.
3753  */
3754 static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
3755 {
3756 	u32 ioc_config, ioc_status;
3757 	u8 retval = 1;
3758 	u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
3759 
3760 	ioc_info(mrioc, "Issuing shutdown Notification\n");
3761 	if (mrioc->unrecoverable) {
3762 		ioc_warn(mrioc,
3763 		    "IOC is unrecoverable shutdown is not issued\n");
3764 		return;
3765 	}
3766 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3767 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
3768 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
3769 		ioc_info(mrioc, "shutdown already in progress\n");
3770 		return;
3771 	}
3772 
3773 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
3774 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
3775 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
3776 
3777 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
3778 
3779 	if (mrioc->facts.shutdown_timeout)
3780 		timeout = mrioc->facts.shutdown_timeout * 10;
3781 
3782 	do {
3783 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3784 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
3785 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
3786 			retval = 0;
3787 			break;
3788 		}
3789 		msleep(100);
3790 	} while (--timeout);
3791 
3792 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3793 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
3794 
3795 	if (retval) {
3796 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
3797 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
3798 			ioc_warn(mrioc,
3799 			    "shutdown still in progress after timeout\n");
3800 	}
3801 
3802 	ioc_info(mrioc,
3803 	    "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
3804 	    (!retval) ? "successful" : "failed", ioc_status,
3805 	    ioc_config);
3806 }
3807 
3808 /**
3809  * mpi3mr_cleanup_ioc - Cleanup controller
3810  * @mrioc: Adapter instance reference
3811  * @reason: Cleanup reason
3812  *
3813  * controller cleanup handler, Message unit reset or soft reset
3814  * and shutdown notification is issued to the controller and the
3815  * associated memory resources are freed.
3816  *
3817  * Return: Nothing.
3818  */
3819 void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc, u8 reason)
3820 {
3821 	enum mpi3mr_iocstate ioc_state;
3822 
3823 	if (reason == MPI3MR_COMPLETE_CLEANUP)
3824 		mpi3mr_stop_watchdog(mrioc);
3825 
3826 	mpi3mr_ioc_disable_intr(mrioc);
3827 
3828 	ioc_state = mpi3mr_get_iocstate(mrioc);
3829 
3830 	if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
3831 	    (ioc_state == MRIOC_STATE_READY)) {
3832 		if (mpi3mr_issue_and_process_mur(mrioc,
3833 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
3834 			mpi3mr_issue_reset(mrioc,
3835 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
3836 			    MPI3MR_RESET_FROM_MUR_FAILURE);
3837 
3838 		if (reason != MPI3MR_REINIT_FAILURE)
3839 			mpi3mr_issue_ioc_shutdown(mrioc);
3840 	}
3841 
3842 	if (reason == MPI3MR_COMPLETE_CLEANUP) {
3843 		mpi3mr_free_mem(mrioc);
3844 		mpi3mr_cleanup_resources(mrioc);
3845 	}
3846 }
3847 
3848 /**
3849  * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
3850  * @mrioc: Adapter instance reference
3851  * @cmdptr: Internal command tracker
3852  *
3853  * Complete an internal driver commands with state indicating it
3854  * is completed due to reset.
3855  *
3856  * Return: Nothing.
3857  */
3858 static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc,
3859 	struct mpi3mr_drv_cmd *cmdptr)
3860 {
3861 	if (cmdptr->state & MPI3MR_CMD_PENDING) {
3862 		cmdptr->state |= MPI3MR_CMD_RESET;
3863 		cmdptr->state &= ~MPI3MR_CMD_PENDING;
3864 		if (cmdptr->is_waiting) {
3865 			complete(&cmdptr->done);
3866 			cmdptr->is_waiting = 0;
3867 		} else if (cmdptr->callback)
3868 			cmdptr->callback(mrioc, cmdptr);
3869 	}
3870 }
3871 
3872 /**
3873  * mpi3mr_flush_drv_cmds - Flush internaldriver commands
3874  * @mrioc: Adapter instance reference
3875  *
3876  * Flush all internal driver commands post reset
3877  *
3878  * Return: Nothing.
3879  */
3880 static void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc)
3881 {
3882 	struct mpi3mr_drv_cmd *cmdptr;
3883 	u8 i;
3884 
3885 	cmdptr = &mrioc->init_cmds;
3886 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
3887 	cmdptr = &mrioc->host_tm_cmds;
3888 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
3889 
3890 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
3891 		cmdptr = &mrioc->dev_rmhs_cmds[i];
3892 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
3893 	}
3894 }
3895 
3896 /**
3897  * mpi3mr_diagfault_reset_handler - Diag fault reset handler
3898  * @mrioc: Adapter instance reference
3899  * @reset_reason: Reset reason code
3900  *
3901  * This is an handler for issuing diag fault reset from the
3902  * applications through IOCTL path to stop the execution of the
3903  * controller
3904  *
3905  * Return: 0 on success, non-zero on failure.
3906  */
3907 int mpi3mr_diagfault_reset_handler(struct mpi3mr_ioc *mrioc,
3908 	u32 reset_reason)
3909 {
3910 	int retval = 0;
3911 
3912 	ioc_info(mrioc, "Entry: reason code: %s\n",
3913 	    mpi3mr_reset_rc_name(reset_reason));
3914 	mrioc->reset_in_progress = 1;
3915 
3916 	mpi3mr_ioc_disable_intr(mrioc);
3917 
3918 	retval = mpi3mr_issue_reset(mrioc,
3919 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
3920 
3921 	if (retval) {
3922 		ioc_err(mrioc, "The diag fault reset failed: reason %d\n",
3923 		    reset_reason);
3924 		mpi3mr_ioc_enable_intr(mrioc);
3925 	}
3926 	ioc_info(mrioc, "%s\n", ((retval == 0) ? "SUCCESS" : "FAILED"));
3927 	mrioc->reset_in_progress = 0;
3928 	return retval;
3929 }
3930 
3931 /**
3932  * mpi3mr_soft_reset_handler - Reset the controller
3933  * @mrioc: Adapter instance reference
3934  * @reset_reason: Reset reason code
3935  * @snapdump: Flag to generate snapdump in firmware or not
3936  *
3937  * This is an handler for recovering controller by issuing soft
3938  * reset are diag fault reset.  This is a blocking function and
3939  * when one reset is executed if any other resets they will be
3940  * blocked. All IOCTLs/IO will be blocked during the reset. If
3941  * controller reset is successful then the controller will be
3942  * reinitalized, otherwise the controller will be marked as not
3943  * recoverable
3944  *
3945  * In snapdump bit is set, the controller is issued with diag
3946  * fault reset so that the firmware can create a snap dump and
3947  * post that the firmware will result in F000 fault and the
3948  * driver will issue soft reset to recover from that.
3949  *
3950  * Return: 0 on success, non-zero on failure.
3951  */
3952 int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
3953 	u32 reset_reason, u8 snapdump)
3954 {
3955 	int retval = 0, i;
3956 	unsigned long flags;
3957 	u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
3958 
3959 	if (mrioc->fault_dbg) {
3960 		if (snapdump)
3961 			mpi3mr_set_diagsave(mrioc);
3962 		mpi3mr_kill_ioc(mrioc, reset_reason);
3963 	}
3964 
3965 	/*
3966 	 * Block new resets until the currently executing one is finished and
3967 	 * return the status of the existing reset for all blocked resets
3968 	 */
3969 	if (!mutex_trylock(&mrioc->reset_mutex)) {
3970 		ioc_info(mrioc, "Another reset in progress\n");
3971 		return -1;
3972 	}
3973 	mrioc->reset_in_progress = 1;
3974 
3975 	if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
3976 	    (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
3977 		for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3978 			mrioc->event_masks[i] = -1;
3979 
3980 		retval = mpi3mr_issue_event_notification(mrioc);
3981 
3982 		if (retval) {
3983 			ioc_err(mrioc,
3984 			    "Failed to turn off events prior to reset %d\n",
3985 			    retval);
3986 		}
3987 	}
3988 
3989 	mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT);
3990 
3991 	mpi3mr_ioc_disable_intr(mrioc);
3992 
3993 	if (snapdump) {
3994 		mpi3mr_set_diagsave(mrioc);
3995 		retval = mpi3mr_issue_reset(mrioc,
3996 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
3997 		if (!retval) {
3998 			do {
3999 				host_diagnostic =
4000 				    readl(&mrioc->sysif_regs->host_diagnostic);
4001 				if (!(host_diagnostic &
4002 				    MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
4003 					break;
4004 				msleep(100);
4005 			} while (--timeout);
4006 		}
4007 	}
4008 
4009 	retval = mpi3mr_issue_reset(mrioc,
4010 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
4011 	if (retval) {
4012 		ioc_err(mrioc, "Failed to issue soft reset to the ioc\n");
4013 		goto out;
4014 	}
4015 
4016 	mpi3mr_flush_delayed_rmhs_list(mrioc);
4017 	mpi3mr_flush_drv_cmds(mrioc);
4018 	memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
4019 	memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
4020 	mpi3mr_cleanup_fwevt_list(mrioc);
4021 	mpi3mr_flush_host_io(mrioc);
4022 	mpi3mr_invalidate_devhandles(mrioc);
4023 	mpi3mr_memset_buffers(mrioc);
4024 	retval = mpi3mr_init_ioc(mrioc, MPI3MR_IT_RESET);
4025 	if (retval) {
4026 		pr_err(IOCNAME "reinit after soft reset failed: reason %d\n",
4027 		    mrioc->name, reset_reason);
4028 		goto out;
4029 	}
4030 	ssleep(10);
4031 
4032 out:
4033 	if (!retval) {
4034 		mrioc->reset_in_progress = 0;
4035 		scsi_unblock_requests(mrioc->shost);
4036 		mpi3mr_rfresh_tgtdevs(mrioc);
4037 		mrioc->ts_update_counter = 0;
4038 		spin_lock_irqsave(&mrioc->watchdog_lock, flags);
4039 		if (mrioc->watchdog_work_q)
4040 			queue_delayed_work(mrioc->watchdog_work_q,
4041 			    &mrioc->watchdog_work,
4042 			    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
4043 		spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
4044 	} else {
4045 		mpi3mr_issue_reset(mrioc,
4046 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4047 		mrioc->unrecoverable = 1;
4048 		mrioc->reset_in_progress = 0;
4049 		retval = -1;
4050 	}
4051 
4052 	mutex_unlock(&mrioc->reset_mutex);
4053 	ioc_info(mrioc, "%s\n", ((retval == 0) ? "SUCCESS" : "FAILED"));
4054 	return retval;
4055 }
4056