xref: /linux/drivers/scsi/mpi3mr/mpi3mr_fw.c (revision c5758fc72b9256aae85f5565f5715a3798d337e0)
1824a1566SKashyap Desai // SPDX-License-Identifier: GPL-2.0-or-later
2824a1566SKashyap Desai /*
3824a1566SKashyap Desai  * Driver for Broadcom MPI3 Storage Controllers
4824a1566SKashyap Desai  *
5824a1566SKashyap Desai  * Copyright (C) 2017-2021 Broadcom Inc.
6824a1566SKashyap Desai  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7824a1566SKashyap Desai  *
8824a1566SKashyap Desai  */
9824a1566SKashyap Desai 
10824a1566SKashyap Desai #include "mpi3mr.h"
11824a1566SKashyap Desai #include <linux/io-64-nonatomic-lo-hi.h>
12824a1566SKashyap Desai 
1359bd9cfeSSreekanth Reddy static int
1459bd9cfeSSreekanth Reddy mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type, u32 reset_reason);
1559bd9cfeSSreekanth Reddy static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc);
16*c5758fc7SSreekanth Reddy static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
17*c5758fc7SSreekanth Reddy 	struct mpi3_ioc_facts_data *facts_data);
1859bd9cfeSSreekanth Reddy 
19824a1566SKashyap Desai #if defined(writeq) && defined(CONFIG_64BIT)
20824a1566SKashyap Desai static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
21824a1566SKashyap Desai {
22824a1566SKashyap Desai 	writeq(b, addr);
23824a1566SKashyap Desai }
24824a1566SKashyap Desai #else
25824a1566SKashyap Desai static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
26824a1566SKashyap Desai {
27824a1566SKashyap Desai 	__u64 data_out = b;
28824a1566SKashyap Desai 
29824a1566SKashyap Desai 	writel((u32)(data_out), addr);
30824a1566SKashyap Desai 	writel((u32)(data_out >> 32), (addr + 4));
31824a1566SKashyap Desai }
32824a1566SKashyap Desai #endif
33824a1566SKashyap Desai 
34023ab2a9SKashyap Desai static inline bool
35023ab2a9SKashyap Desai mpi3mr_check_req_qfull(struct op_req_qinfo *op_req_q)
36023ab2a9SKashyap Desai {
37023ab2a9SKashyap Desai 	u16 pi, ci, max_entries;
38023ab2a9SKashyap Desai 	bool is_qfull = false;
39023ab2a9SKashyap Desai 
40023ab2a9SKashyap Desai 	pi = op_req_q->pi;
41023ab2a9SKashyap Desai 	ci = READ_ONCE(op_req_q->ci);
42023ab2a9SKashyap Desai 	max_entries = op_req_q->num_requests;
43023ab2a9SKashyap Desai 
44023ab2a9SKashyap Desai 	if ((ci == (pi + 1)) || ((!ci) && (pi == (max_entries - 1))))
45023ab2a9SKashyap Desai 		is_qfull = true;
46023ab2a9SKashyap Desai 
47023ab2a9SKashyap Desai 	return is_qfull;
48023ab2a9SKashyap Desai }
49023ab2a9SKashyap Desai 
50824a1566SKashyap Desai static void mpi3mr_sync_irqs(struct mpi3mr_ioc *mrioc)
51824a1566SKashyap Desai {
52824a1566SKashyap Desai 	u16 i, max_vectors;
53824a1566SKashyap Desai 
54824a1566SKashyap Desai 	max_vectors = mrioc->intr_info_count;
55824a1566SKashyap Desai 
56824a1566SKashyap Desai 	for (i = 0; i < max_vectors; i++)
57824a1566SKashyap Desai 		synchronize_irq(pci_irq_vector(mrioc->pdev, i));
58824a1566SKashyap Desai }
59824a1566SKashyap Desai 
60824a1566SKashyap Desai void mpi3mr_ioc_disable_intr(struct mpi3mr_ioc *mrioc)
61824a1566SKashyap Desai {
62824a1566SKashyap Desai 	mrioc->intr_enabled = 0;
63824a1566SKashyap Desai 	mpi3mr_sync_irqs(mrioc);
64824a1566SKashyap Desai }
65824a1566SKashyap Desai 
66824a1566SKashyap Desai void mpi3mr_ioc_enable_intr(struct mpi3mr_ioc *mrioc)
67824a1566SKashyap Desai {
68824a1566SKashyap Desai 	mrioc->intr_enabled = 1;
69824a1566SKashyap Desai }
70824a1566SKashyap Desai 
71824a1566SKashyap Desai static void mpi3mr_cleanup_isr(struct mpi3mr_ioc *mrioc)
72824a1566SKashyap Desai {
73824a1566SKashyap Desai 	u16 i;
74824a1566SKashyap Desai 
75824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
76824a1566SKashyap Desai 
77824a1566SKashyap Desai 	if (!mrioc->intr_info)
78824a1566SKashyap Desai 		return;
79824a1566SKashyap Desai 
80824a1566SKashyap Desai 	for (i = 0; i < mrioc->intr_info_count; i++)
81824a1566SKashyap Desai 		free_irq(pci_irq_vector(mrioc->pdev, i),
82824a1566SKashyap Desai 		    (mrioc->intr_info + i));
83824a1566SKashyap Desai 
84824a1566SKashyap Desai 	kfree(mrioc->intr_info);
85824a1566SKashyap Desai 	mrioc->intr_info = NULL;
86824a1566SKashyap Desai 	mrioc->intr_info_count = 0;
87fe6db615SSreekanth Reddy 	mrioc->is_intr_info_set = false;
88824a1566SKashyap Desai 	pci_free_irq_vectors(mrioc->pdev);
89824a1566SKashyap Desai }
90824a1566SKashyap Desai 
91824a1566SKashyap Desai void mpi3mr_add_sg_single(void *paddr, u8 flags, u32 length,
92824a1566SKashyap Desai 	dma_addr_t dma_addr)
93824a1566SKashyap Desai {
94824a1566SKashyap Desai 	struct mpi3_sge_common *sgel = paddr;
95824a1566SKashyap Desai 
96824a1566SKashyap Desai 	sgel->flags = flags;
97824a1566SKashyap Desai 	sgel->length = cpu_to_le32(length);
98824a1566SKashyap Desai 	sgel->address = cpu_to_le64(dma_addr);
99824a1566SKashyap Desai }
100824a1566SKashyap Desai 
101824a1566SKashyap Desai void mpi3mr_build_zero_len_sge(void *paddr)
102824a1566SKashyap Desai {
103824a1566SKashyap Desai 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
104824a1566SKashyap Desai 
105824a1566SKashyap Desai 	mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1);
106824a1566SKashyap Desai }
107824a1566SKashyap Desai 
108824a1566SKashyap Desai void *mpi3mr_get_reply_virt_addr(struct mpi3mr_ioc *mrioc,
109824a1566SKashyap Desai 	dma_addr_t phys_addr)
110824a1566SKashyap Desai {
111824a1566SKashyap Desai 	if (!phys_addr)
112824a1566SKashyap Desai 		return NULL;
113824a1566SKashyap Desai 
114824a1566SKashyap Desai 	if ((phys_addr < mrioc->reply_buf_dma) ||
115824a1566SKashyap Desai 	    (phys_addr > mrioc->reply_buf_dma_max_address))
116824a1566SKashyap Desai 		return NULL;
117824a1566SKashyap Desai 
118824a1566SKashyap Desai 	return mrioc->reply_buf + (phys_addr - mrioc->reply_buf_dma);
119824a1566SKashyap Desai }
120824a1566SKashyap Desai 
121824a1566SKashyap Desai void *mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_ioc *mrioc,
122824a1566SKashyap Desai 	dma_addr_t phys_addr)
123824a1566SKashyap Desai {
124824a1566SKashyap Desai 	if (!phys_addr)
125824a1566SKashyap Desai 		return NULL;
126824a1566SKashyap Desai 
127824a1566SKashyap Desai 	return mrioc->sense_buf + (phys_addr - mrioc->sense_buf_dma);
128824a1566SKashyap Desai }
129824a1566SKashyap Desai 
130824a1566SKashyap Desai static void mpi3mr_repost_reply_buf(struct mpi3mr_ioc *mrioc,
131824a1566SKashyap Desai 	u64 reply_dma)
132824a1566SKashyap Desai {
133824a1566SKashyap Desai 	u32 old_idx = 0;
134a83ec831SSreekanth Reddy 	unsigned long flags;
135824a1566SKashyap Desai 
136a83ec831SSreekanth Reddy 	spin_lock_irqsave(&mrioc->reply_free_queue_lock, flags);
137824a1566SKashyap Desai 	old_idx  =  mrioc->reply_free_queue_host_index;
138824a1566SKashyap Desai 	mrioc->reply_free_queue_host_index = (
139824a1566SKashyap Desai 	    (mrioc->reply_free_queue_host_index ==
140824a1566SKashyap Desai 	    (mrioc->reply_free_qsz - 1)) ? 0 :
141824a1566SKashyap Desai 	    (mrioc->reply_free_queue_host_index + 1));
142824a1566SKashyap Desai 	mrioc->reply_free_q[old_idx] = cpu_to_le64(reply_dma);
143824a1566SKashyap Desai 	writel(mrioc->reply_free_queue_host_index,
144824a1566SKashyap Desai 	    &mrioc->sysif_regs->reply_free_host_index);
145a83ec831SSreekanth Reddy 	spin_unlock_irqrestore(&mrioc->reply_free_queue_lock, flags);
146824a1566SKashyap Desai }
147824a1566SKashyap Desai 
148824a1566SKashyap Desai void mpi3mr_repost_sense_buf(struct mpi3mr_ioc *mrioc,
149824a1566SKashyap Desai 	u64 sense_buf_dma)
150824a1566SKashyap Desai {
151824a1566SKashyap Desai 	u32 old_idx = 0;
152a83ec831SSreekanth Reddy 	unsigned long flags;
153824a1566SKashyap Desai 
154a83ec831SSreekanth Reddy 	spin_lock_irqsave(&mrioc->sbq_lock, flags);
155824a1566SKashyap Desai 	old_idx  =  mrioc->sbq_host_index;
156824a1566SKashyap Desai 	mrioc->sbq_host_index = ((mrioc->sbq_host_index ==
157824a1566SKashyap Desai 	    (mrioc->sense_buf_q_sz - 1)) ? 0 :
158824a1566SKashyap Desai 	    (mrioc->sbq_host_index + 1));
159824a1566SKashyap Desai 	mrioc->sense_buf_q[old_idx] = cpu_to_le64(sense_buf_dma);
160824a1566SKashyap Desai 	writel(mrioc->sbq_host_index,
161824a1566SKashyap Desai 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
162a83ec831SSreekanth Reddy 	spin_unlock_irqrestore(&mrioc->sbq_lock, flags);
163824a1566SKashyap Desai }
164824a1566SKashyap Desai 
1659fc4abfeSKashyap Desai static void mpi3mr_print_event_data(struct mpi3mr_ioc *mrioc,
1669fc4abfeSKashyap Desai 	struct mpi3_event_notification_reply *event_reply)
1679fc4abfeSKashyap Desai {
1689fc4abfeSKashyap Desai 	char *desc = NULL;
1699fc4abfeSKashyap Desai 	u16 event;
1709fc4abfeSKashyap Desai 
1719fc4abfeSKashyap Desai 	event = event_reply->event;
1729fc4abfeSKashyap Desai 
1739fc4abfeSKashyap Desai 	switch (event) {
1749fc4abfeSKashyap Desai 	case MPI3_EVENT_LOG_DATA:
1759fc4abfeSKashyap Desai 		desc = "Log Data";
1769fc4abfeSKashyap Desai 		break;
1779fc4abfeSKashyap Desai 	case MPI3_EVENT_CHANGE:
1789fc4abfeSKashyap Desai 		desc = "Event Change";
1799fc4abfeSKashyap Desai 		break;
1809fc4abfeSKashyap Desai 	case MPI3_EVENT_GPIO_INTERRUPT:
1819fc4abfeSKashyap Desai 		desc = "GPIO Interrupt";
1829fc4abfeSKashyap Desai 		break;
1839fc4abfeSKashyap Desai 	case MPI3_EVENT_TEMP_THRESHOLD:
1849fc4abfeSKashyap Desai 		desc = "Temperature Threshold";
1859fc4abfeSKashyap Desai 		break;
1869fc4abfeSKashyap Desai 	case MPI3_EVENT_CABLE_MGMT:
1879fc4abfeSKashyap Desai 		desc = "Cable Management";
1889fc4abfeSKashyap Desai 		break;
1899fc4abfeSKashyap Desai 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
1909fc4abfeSKashyap Desai 		desc = "Energy Pack Change";
1919fc4abfeSKashyap Desai 		break;
1929fc4abfeSKashyap Desai 	case MPI3_EVENT_DEVICE_ADDED:
1939fc4abfeSKashyap Desai 	{
1949fc4abfeSKashyap Desai 		struct mpi3_device_page0 *event_data =
1959fc4abfeSKashyap Desai 		    (struct mpi3_device_page0 *)event_reply->event_data;
1969fc4abfeSKashyap Desai 		ioc_info(mrioc, "Device Added: dev=0x%04x Form=0x%x\n",
1979fc4abfeSKashyap Desai 		    event_data->dev_handle, event_data->device_form);
1989fc4abfeSKashyap Desai 		return;
1999fc4abfeSKashyap Desai 	}
2009fc4abfeSKashyap Desai 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
2019fc4abfeSKashyap Desai 	{
2029fc4abfeSKashyap Desai 		struct mpi3_device_page0 *event_data =
2039fc4abfeSKashyap Desai 		    (struct mpi3_device_page0 *)event_reply->event_data;
2049fc4abfeSKashyap Desai 		ioc_info(mrioc, "Device Info Changed: dev=0x%04x Form=0x%x\n",
2059fc4abfeSKashyap Desai 		    event_data->dev_handle, event_data->device_form);
2069fc4abfeSKashyap Desai 		return;
2079fc4abfeSKashyap Desai 	}
2089fc4abfeSKashyap Desai 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2099fc4abfeSKashyap Desai 	{
2109fc4abfeSKashyap Desai 		struct mpi3_event_data_device_status_change *event_data =
2119fc4abfeSKashyap Desai 		    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2129fc4abfeSKashyap Desai 		ioc_info(mrioc, "Device status Change: dev=0x%04x RC=0x%x\n",
2139fc4abfeSKashyap Desai 		    event_data->dev_handle, event_data->reason_code);
2149fc4abfeSKashyap Desai 		return;
2159fc4abfeSKashyap Desai 	}
2169fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_DISCOVERY:
2179fc4abfeSKashyap Desai 	{
2189fc4abfeSKashyap Desai 		struct mpi3_event_data_sas_discovery *event_data =
2199fc4abfeSKashyap Desai 		    (struct mpi3_event_data_sas_discovery *)event_reply->event_data;
2209fc4abfeSKashyap Desai 		ioc_info(mrioc, "SAS Discovery: (%s) status (0x%08x)\n",
2219fc4abfeSKashyap Desai 		    (event_data->reason_code == MPI3_EVENT_SAS_DISC_RC_STARTED) ?
2229fc4abfeSKashyap Desai 		    "start" : "stop",
2239fc4abfeSKashyap Desai 		    le32_to_cpu(event_data->discovery_status));
2249fc4abfeSKashyap Desai 		return;
2259fc4abfeSKashyap Desai 	}
2269fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2279fc4abfeSKashyap Desai 		desc = "SAS Broadcast Primitive";
2289fc4abfeSKashyap Desai 		break;
2299fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_NOTIFY_PRIMITIVE:
2309fc4abfeSKashyap Desai 		desc = "SAS Notify Primitive";
2319fc4abfeSKashyap Desai 		break;
2329fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
2339fc4abfeSKashyap Desai 		desc = "SAS Init Device Status Change";
2349fc4abfeSKashyap Desai 		break;
2359fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_INIT_TABLE_OVERFLOW:
2369fc4abfeSKashyap Desai 		desc = "SAS Init Table Overflow";
2379fc4abfeSKashyap Desai 		break;
2389fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2399fc4abfeSKashyap Desai 		desc = "SAS Topology Change List";
2409fc4abfeSKashyap Desai 		break;
2419fc4abfeSKashyap Desai 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2429fc4abfeSKashyap Desai 		desc = "Enclosure Device Status Change";
2439fc4abfeSKashyap Desai 		break;
2449fc4abfeSKashyap Desai 	case MPI3_EVENT_HARD_RESET_RECEIVED:
2459fc4abfeSKashyap Desai 		desc = "Hard Reset Received";
2469fc4abfeSKashyap Desai 		break;
2479fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_PHY_COUNTER:
2489fc4abfeSKashyap Desai 		desc = "SAS PHY Counter";
2499fc4abfeSKashyap Desai 		break;
2509fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2519fc4abfeSKashyap Desai 		desc = "SAS Device Discovery Error";
2529fc4abfeSKashyap Desai 		break;
2539fc4abfeSKashyap Desai 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2549fc4abfeSKashyap Desai 		desc = "PCIE Topology Change List";
2559fc4abfeSKashyap Desai 		break;
2569fc4abfeSKashyap Desai 	case MPI3_EVENT_PCIE_ENUMERATION:
2579fc4abfeSKashyap Desai 	{
2589fc4abfeSKashyap Desai 		struct mpi3_event_data_pcie_enumeration *event_data =
2599fc4abfeSKashyap Desai 		    (struct mpi3_event_data_pcie_enumeration *)event_reply->event_data;
2609fc4abfeSKashyap Desai 		ioc_info(mrioc, "PCIE Enumeration: (%s)",
2619fc4abfeSKashyap Desai 		    (event_data->reason_code ==
2629fc4abfeSKashyap Desai 		    MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" : "stop");
2639fc4abfeSKashyap Desai 		if (event_data->enumeration_status)
2649fc4abfeSKashyap Desai 			ioc_info(mrioc, "enumeration_status(0x%08x)\n",
2659fc4abfeSKashyap Desai 			    le32_to_cpu(event_data->enumeration_status));
2669fc4abfeSKashyap Desai 		return;
2679fc4abfeSKashyap Desai 	}
2689fc4abfeSKashyap Desai 	case MPI3_EVENT_PREPARE_FOR_RESET:
2699fc4abfeSKashyap Desai 		desc = "Prepare For Reset";
2709fc4abfeSKashyap Desai 		break;
2719fc4abfeSKashyap Desai 	}
2729fc4abfeSKashyap Desai 
2739fc4abfeSKashyap Desai 	if (!desc)
2749fc4abfeSKashyap Desai 		return;
2759fc4abfeSKashyap Desai 
2769fc4abfeSKashyap Desai 	ioc_info(mrioc, "%s\n", desc);
2779fc4abfeSKashyap Desai }
2789fc4abfeSKashyap Desai 
279824a1566SKashyap Desai static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc,
280824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply)
281824a1566SKashyap Desai {
282824a1566SKashyap Desai 	struct mpi3_event_notification_reply *event_reply =
283824a1566SKashyap Desai 	    (struct mpi3_event_notification_reply *)def_reply;
284824a1566SKashyap Desai 
285824a1566SKashyap Desai 	mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count);
2869fc4abfeSKashyap Desai 	mpi3mr_print_event_data(mrioc, event_reply);
28713ef29eaSKashyap Desai 	mpi3mr_os_handle_events(mrioc, event_reply);
288824a1566SKashyap Desai }
289824a1566SKashyap Desai 
290824a1566SKashyap Desai static struct mpi3mr_drv_cmd *
291824a1566SKashyap Desai mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag,
292824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply)
293824a1566SKashyap Desai {
29413ef29eaSKashyap Desai 	u16 idx;
29513ef29eaSKashyap Desai 
296824a1566SKashyap Desai 	switch (host_tag) {
297824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INITCMDS:
298824a1566SKashyap Desai 		return &mrioc->init_cmds;
299e844adb1SKashyap Desai 	case MPI3MR_HOSTTAG_BLK_TMS:
300e844adb1SKashyap Desai 		return &mrioc->host_tm_cmds;
301824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INVALID:
302824a1566SKashyap Desai 		if (def_reply && def_reply->function ==
303824a1566SKashyap Desai 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
304824a1566SKashyap Desai 			mpi3mr_handle_events(mrioc, def_reply);
305824a1566SKashyap Desai 		return NULL;
306824a1566SKashyap Desai 	default:
307824a1566SKashyap Desai 		break;
308824a1566SKashyap Desai 	}
30913ef29eaSKashyap Desai 	if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
31013ef29eaSKashyap Desai 	    host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) {
31113ef29eaSKashyap Desai 		idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
31213ef29eaSKashyap Desai 		return &mrioc->dev_rmhs_cmds[idx];
31313ef29eaSKashyap Desai 	}
314824a1566SKashyap Desai 
315824a1566SKashyap Desai 	return NULL;
316824a1566SKashyap Desai }
317824a1566SKashyap Desai 
318824a1566SKashyap Desai static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
319824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
320824a1566SKashyap Desai {
321824a1566SKashyap Desai 	u16 reply_desc_type, host_tag = 0;
322824a1566SKashyap Desai 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
323824a1566SKashyap Desai 	u32 ioc_loginfo = 0;
324824a1566SKashyap Desai 	struct mpi3_status_reply_descriptor *status_desc;
325824a1566SKashyap Desai 	struct mpi3_address_reply_descriptor *addr_desc;
326824a1566SKashyap Desai 	struct mpi3_success_reply_descriptor *success_desc;
327824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply = NULL;
328824a1566SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr = NULL;
329824a1566SKashyap Desai 	struct mpi3_scsi_io_reply *scsi_reply;
330824a1566SKashyap Desai 	u8 *sense_buf = NULL;
331824a1566SKashyap Desai 
332824a1566SKashyap Desai 	*reply_dma = 0;
333824a1566SKashyap Desai 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
334824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
335824a1566SKashyap Desai 	switch (reply_desc_type) {
336824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
337824a1566SKashyap Desai 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
338824a1566SKashyap Desai 		host_tag = le16_to_cpu(status_desc->host_tag);
339824a1566SKashyap Desai 		ioc_status = le16_to_cpu(status_desc->ioc_status);
340824a1566SKashyap Desai 		if (ioc_status &
341824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
342824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
343824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
344824a1566SKashyap Desai 		break;
345824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
346824a1566SKashyap Desai 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
347824a1566SKashyap Desai 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
348824a1566SKashyap Desai 		def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
349824a1566SKashyap Desai 		if (!def_reply)
350824a1566SKashyap Desai 			goto out;
351824a1566SKashyap Desai 		host_tag = le16_to_cpu(def_reply->host_tag);
352824a1566SKashyap Desai 		ioc_status = le16_to_cpu(def_reply->ioc_status);
353824a1566SKashyap Desai 		if (ioc_status &
354824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
355824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
356824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
357824a1566SKashyap Desai 		if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
358824a1566SKashyap Desai 			scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
359824a1566SKashyap Desai 			sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
360824a1566SKashyap Desai 			    le64_to_cpu(scsi_reply->sense_data_buffer_address));
361824a1566SKashyap Desai 		}
362824a1566SKashyap Desai 		break;
363824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
364824a1566SKashyap Desai 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
365824a1566SKashyap Desai 		host_tag = le16_to_cpu(success_desc->host_tag);
366824a1566SKashyap Desai 		break;
367824a1566SKashyap Desai 	default:
368824a1566SKashyap Desai 		break;
369824a1566SKashyap Desai 	}
370824a1566SKashyap Desai 
371824a1566SKashyap Desai 	cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
372824a1566SKashyap Desai 	if (cmdptr) {
373824a1566SKashyap Desai 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
374824a1566SKashyap Desai 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
375824a1566SKashyap Desai 			cmdptr->ioc_loginfo = ioc_loginfo;
376824a1566SKashyap Desai 			cmdptr->ioc_status = ioc_status;
377824a1566SKashyap Desai 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
378824a1566SKashyap Desai 			if (def_reply) {
379824a1566SKashyap Desai 				cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
380824a1566SKashyap Desai 				memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
381*c5758fc7SSreekanth Reddy 				    mrioc->reply_sz);
382824a1566SKashyap Desai 			}
383824a1566SKashyap Desai 			if (cmdptr->is_waiting) {
384824a1566SKashyap Desai 				complete(&cmdptr->done);
385824a1566SKashyap Desai 				cmdptr->is_waiting = 0;
386824a1566SKashyap Desai 			} else if (cmdptr->callback)
387824a1566SKashyap Desai 				cmdptr->callback(mrioc, cmdptr);
388824a1566SKashyap Desai 		}
389824a1566SKashyap Desai 	}
390824a1566SKashyap Desai out:
391824a1566SKashyap Desai 	if (sense_buf)
392824a1566SKashyap Desai 		mpi3mr_repost_sense_buf(mrioc,
393824a1566SKashyap Desai 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
394824a1566SKashyap Desai }
395824a1566SKashyap Desai 
396824a1566SKashyap Desai static int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
397824a1566SKashyap Desai {
398824a1566SKashyap Desai 	u32 exp_phase = mrioc->admin_reply_ephase;
399824a1566SKashyap Desai 	u32 admin_reply_ci = mrioc->admin_reply_ci;
400824a1566SKashyap Desai 	u32 num_admin_replies = 0;
401824a1566SKashyap Desai 	u64 reply_dma = 0;
402824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
403824a1566SKashyap Desai 
404824a1566SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
405824a1566SKashyap Desai 	    admin_reply_ci;
406824a1566SKashyap Desai 
407824a1566SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
408824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
409824a1566SKashyap Desai 		return 0;
410824a1566SKashyap Desai 
411824a1566SKashyap Desai 	do {
412824a1566SKashyap Desai 		mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
413824a1566SKashyap Desai 		mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
414824a1566SKashyap Desai 		if (reply_dma)
415824a1566SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
416824a1566SKashyap Desai 		num_admin_replies++;
417824a1566SKashyap Desai 		if (++admin_reply_ci == mrioc->num_admin_replies) {
418824a1566SKashyap Desai 			admin_reply_ci = 0;
419824a1566SKashyap Desai 			exp_phase ^= 1;
420824a1566SKashyap Desai 		}
421824a1566SKashyap Desai 		reply_desc =
422824a1566SKashyap Desai 		    (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
423824a1566SKashyap Desai 		    admin_reply_ci;
424824a1566SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
425824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
426824a1566SKashyap Desai 			break;
427824a1566SKashyap Desai 	} while (1);
428824a1566SKashyap Desai 
429824a1566SKashyap Desai 	writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
430824a1566SKashyap Desai 	mrioc->admin_reply_ci = admin_reply_ci;
431824a1566SKashyap Desai 	mrioc->admin_reply_ephase = exp_phase;
432824a1566SKashyap Desai 
433824a1566SKashyap Desai 	return num_admin_replies;
434824a1566SKashyap Desai }
435824a1566SKashyap Desai 
436023ab2a9SKashyap Desai /**
437023ab2a9SKashyap Desai  * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to
438023ab2a9SKashyap Desai  *	queue's consumer index from operational reply descriptor queue.
439023ab2a9SKashyap Desai  * @op_reply_q: op_reply_qinfo object
440023ab2a9SKashyap Desai  * @reply_ci: operational reply descriptor's queue consumer index
441023ab2a9SKashyap Desai  *
442023ab2a9SKashyap Desai  * Returns reply descriptor frame address
443023ab2a9SKashyap Desai  */
444023ab2a9SKashyap Desai static inline struct mpi3_default_reply_descriptor *
445023ab2a9SKashyap Desai mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci)
446023ab2a9SKashyap Desai {
447023ab2a9SKashyap Desai 	void *segment_base_addr;
448023ab2a9SKashyap Desai 	struct segments *segments = op_reply_q->q_segments;
449023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc = NULL;
450023ab2a9SKashyap Desai 
451023ab2a9SKashyap Desai 	segment_base_addr =
452023ab2a9SKashyap Desai 	    segments[reply_ci / op_reply_q->segment_qd].segment;
453023ab2a9SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr +
454023ab2a9SKashyap Desai 	    (reply_ci % op_reply_q->segment_qd);
455023ab2a9SKashyap Desai 	return reply_desc;
456023ab2a9SKashyap Desai }
457023ab2a9SKashyap Desai 
458023ab2a9SKashyap Desai static int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc,
459023ab2a9SKashyap Desai 	struct mpi3mr_intr_info *intr_info)
460023ab2a9SKashyap Desai {
461023ab2a9SKashyap Desai 	struct op_reply_qinfo *op_reply_q = intr_info->op_reply_q;
462023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q;
463023ab2a9SKashyap Desai 	u32 exp_phase;
464023ab2a9SKashyap Desai 	u32 reply_ci;
465023ab2a9SKashyap Desai 	u32 num_op_reply = 0;
466023ab2a9SKashyap Desai 	u64 reply_dma = 0;
467023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
468023ab2a9SKashyap Desai 	u16 req_q_idx = 0, reply_qidx;
469023ab2a9SKashyap Desai 
470023ab2a9SKashyap Desai 	reply_qidx = op_reply_q->qid - 1;
471023ab2a9SKashyap Desai 
472463429f8SKashyap Desai 	if (!atomic_add_unless(&op_reply_q->in_use, 1, 1))
473463429f8SKashyap Desai 		return 0;
474463429f8SKashyap Desai 
475023ab2a9SKashyap Desai 	exp_phase = op_reply_q->ephase;
476023ab2a9SKashyap Desai 	reply_ci = op_reply_q->ci;
477023ab2a9SKashyap Desai 
478023ab2a9SKashyap Desai 	reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
479023ab2a9SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
480023ab2a9SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
481463429f8SKashyap Desai 		atomic_dec(&op_reply_q->in_use);
482023ab2a9SKashyap Desai 		return 0;
483023ab2a9SKashyap Desai 	}
484023ab2a9SKashyap Desai 
485023ab2a9SKashyap Desai 	do {
486023ab2a9SKashyap Desai 		req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1;
487023ab2a9SKashyap Desai 		op_req_q = &mrioc->req_qinfo[req_q_idx];
488023ab2a9SKashyap Desai 
489023ab2a9SKashyap Desai 		WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci));
490023ab2a9SKashyap Desai 		mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma,
491023ab2a9SKashyap Desai 		    reply_qidx);
492463429f8SKashyap Desai 		atomic_dec(&op_reply_q->pend_ios);
493023ab2a9SKashyap Desai 		if (reply_dma)
494023ab2a9SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
495023ab2a9SKashyap Desai 		num_op_reply++;
496023ab2a9SKashyap Desai 
497023ab2a9SKashyap Desai 		if (++reply_ci == op_reply_q->num_replies) {
498023ab2a9SKashyap Desai 			reply_ci = 0;
499023ab2a9SKashyap Desai 			exp_phase ^= 1;
500023ab2a9SKashyap Desai 		}
501023ab2a9SKashyap Desai 
502023ab2a9SKashyap Desai 		reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
503023ab2a9SKashyap Desai 
504023ab2a9SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
505023ab2a9SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
506023ab2a9SKashyap Desai 			break;
507463429f8SKashyap Desai 		/*
508463429f8SKashyap Desai 		 * Exit completion loop to avoid CPU lockup
509463429f8SKashyap Desai 		 * Ensure remaining completion happens from threaded ISR.
510463429f8SKashyap Desai 		 */
511463429f8SKashyap Desai 		if (num_op_reply > mrioc->max_host_ios) {
512463429f8SKashyap Desai 			intr_info->op_reply_q->enable_irq_poll = true;
513463429f8SKashyap Desai 			break;
514463429f8SKashyap Desai 		}
515023ab2a9SKashyap Desai 
516023ab2a9SKashyap Desai 	} while (1);
517023ab2a9SKashyap Desai 
518023ab2a9SKashyap Desai 	writel(reply_ci,
519023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index);
520023ab2a9SKashyap Desai 	op_reply_q->ci = reply_ci;
521023ab2a9SKashyap Desai 	op_reply_q->ephase = exp_phase;
522023ab2a9SKashyap Desai 
523463429f8SKashyap Desai 	atomic_dec(&op_reply_q->in_use);
524023ab2a9SKashyap Desai 	return num_op_reply;
525023ab2a9SKashyap Desai }
526023ab2a9SKashyap Desai 
527824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
528824a1566SKashyap Desai {
529824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
530824a1566SKashyap Desai 	struct mpi3mr_ioc *mrioc;
531824a1566SKashyap Desai 	u16 midx;
532463429f8SKashyap Desai 	u32 num_admin_replies = 0, num_op_reply = 0;
533824a1566SKashyap Desai 
534824a1566SKashyap Desai 	if (!intr_info)
535824a1566SKashyap Desai 		return IRQ_NONE;
536824a1566SKashyap Desai 
537824a1566SKashyap Desai 	mrioc = intr_info->mrioc;
538824a1566SKashyap Desai 
539824a1566SKashyap Desai 	if (!mrioc->intr_enabled)
540824a1566SKashyap Desai 		return IRQ_NONE;
541824a1566SKashyap Desai 
542824a1566SKashyap Desai 	midx = intr_info->msix_index;
543824a1566SKashyap Desai 
544824a1566SKashyap Desai 	if (!midx)
545824a1566SKashyap Desai 		num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
546463429f8SKashyap Desai 	if (intr_info->op_reply_q)
547463429f8SKashyap Desai 		num_op_reply = mpi3mr_process_op_reply_q(mrioc, intr_info);
548824a1566SKashyap Desai 
549463429f8SKashyap Desai 	if (num_admin_replies || num_op_reply)
550824a1566SKashyap Desai 		return IRQ_HANDLED;
551824a1566SKashyap Desai 	else
552824a1566SKashyap Desai 		return IRQ_NONE;
553824a1566SKashyap Desai }
554824a1566SKashyap Desai 
555824a1566SKashyap Desai static irqreturn_t mpi3mr_isr(int irq, void *privdata)
556824a1566SKashyap Desai {
557824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
558463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
559463429f8SKashyap Desai 	u16 midx;
560824a1566SKashyap Desai 	int ret;
561824a1566SKashyap Desai 
562824a1566SKashyap Desai 	if (!intr_info)
563824a1566SKashyap Desai 		return IRQ_NONE;
564824a1566SKashyap Desai 
565463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
566463429f8SKashyap Desai 	midx = intr_info->msix_index;
567824a1566SKashyap Desai 	/* Call primary ISR routine */
568824a1566SKashyap Desai 	ret = mpi3mr_isr_primary(irq, privdata);
569824a1566SKashyap Desai 
570463429f8SKashyap Desai 	/*
571463429f8SKashyap Desai 	 * If more IOs are expected, schedule IRQ polling thread.
572463429f8SKashyap Desai 	 * Otherwise exit from ISR.
573463429f8SKashyap Desai 	 */
574463429f8SKashyap Desai 	if (!intr_info->op_reply_q)
575824a1566SKashyap Desai 		return ret;
576463429f8SKashyap Desai 
577463429f8SKashyap Desai 	if (!intr_info->op_reply_q->enable_irq_poll ||
578463429f8SKashyap Desai 	    !atomic_read(&intr_info->op_reply_q->pend_ios))
579463429f8SKashyap Desai 		return ret;
580463429f8SKashyap Desai 
581463429f8SKashyap Desai 	disable_irq_nosync(pci_irq_vector(mrioc->pdev, midx));
582463429f8SKashyap Desai 
583463429f8SKashyap Desai 	return IRQ_WAKE_THREAD;
584824a1566SKashyap Desai }
585824a1566SKashyap Desai 
586824a1566SKashyap Desai /**
587824a1566SKashyap Desai  * mpi3mr_isr_poll - Reply queue polling routine
588824a1566SKashyap Desai  * @irq: IRQ
589824a1566SKashyap Desai  * @privdata: Interrupt info
590824a1566SKashyap Desai  *
591824a1566SKashyap Desai  * poll for pending I/O completions in a loop until pending I/Os
592824a1566SKashyap Desai  * present or controller queue depth I/Os are processed.
593824a1566SKashyap Desai  *
594824a1566SKashyap Desai  * Return: IRQ_NONE or IRQ_HANDLED
595824a1566SKashyap Desai  */
596824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
597824a1566SKashyap Desai {
598463429f8SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
599463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
600463429f8SKashyap Desai 	u16 midx;
601463429f8SKashyap Desai 	u32 num_op_reply = 0;
602463429f8SKashyap Desai 
603463429f8SKashyap Desai 	if (!intr_info || !intr_info->op_reply_q)
604463429f8SKashyap Desai 		return IRQ_NONE;
605463429f8SKashyap Desai 
606463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
607463429f8SKashyap Desai 	midx = intr_info->msix_index;
608463429f8SKashyap Desai 
609463429f8SKashyap Desai 	/* Poll for pending IOs completions */
610463429f8SKashyap Desai 	do {
611463429f8SKashyap Desai 		if (!mrioc->intr_enabled)
612463429f8SKashyap Desai 			break;
613463429f8SKashyap Desai 
614463429f8SKashyap Desai 		if (!midx)
615463429f8SKashyap Desai 			mpi3mr_process_admin_reply_q(mrioc);
616463429f8SKashyap Desai 		if (intr_info->op_reply_q)
617463429f8SKashyap Desai 			num_op_reply +=
618463429f8SKashyap Desai 			    mpi3mr_process_op_reply_q(mrioc, intr_info);
619463429f8SKashyap Desai 
620463429f8SKashyap Desai 		usleep_range(mrioc->irqpoll_sleep, 10 * mrioc->irqpoll_sleep);
621463429f8SKashyap Desai 
622463429f8SKashyap Desai 	} while (atomic_read(&intr_info->op_reply_q->pend_ios) &&
623463429f8SKashyap Desai 	    (num_op_reply < mrioc->max_host_ios));
624463429f8SKashyap Desai 
625463429f8SKashyap Desai 	intr_info->op_reply_q->enable_irq_poll = false;
626463429f8SKashyap Desai 	enable_irq(pci_irq_vector(mrioc->pdev, midx));
627463429f8SKashyap Desai 
628824a1566SKashyap Desai 	return IRQ_HANDLED;
629824a1566SKashyap Desai }
630824a1566SKashyap Desai 
631824a1566SKashyap Desai /**
632824a1566SKashyap Desai  * mpi3mr_request_irq - Request IRQ and register ISR
633824a1566SKashyap Desai  * @mrioc: Adapter instance reference
634824a1566SKashyap Desai  * @index: IRQ vector index
635824a1566SKashyap Desai  *
636824a1566SKashyap Desai  * Request threaded ISR with primary ISR and secondary
637824a1566SKashyap Desai  *
638824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
639824a1566SKashyap Desai  */
640824a1566SKashyap Desai static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
641824a1566SKashyap Desai {
642824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
643824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
644824a1566SKashyap Desai 	int retval = 0;
645824a1566SKashyap Desai 
646824a1566SKashyap Desai 	intr_info->mrioc = mrioc;
647824a1566SKashyap Desai 	intr_info->msix_index = index;
648824a1566SKashyap Desai 	intr_info->op_reply_q = NULL;
649824a1566SKashyap Desai 
650824a1566SKashyap Desai 	snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
651824a1566SKashyap Desai 	    mrioc->driver_name, mrioc->id, index);
652824a1566SKashyap Desai 
653824a1566SKashyap Desai 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
654824a1566SKashyap Desai 	    mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
655824a1566SKashyap Desai 	if (retval) {
656824a1566SKashyap Desai 		ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
657824a1566SKashyap Desai 		    intr_info->name, pci_irq_vector(pdev, index));
658824a1566SKashyap Desai 		return retval;
659824a1566SKashyap Desai 	}
660824a1566SKashyap Desai 
661824a1566SKashyap Desai 	return retval;
662824a1566SKashyap Desai }
663824a1566SKashyap Desai 
664824a1566SKashyap Desai /**
665824a1566SKashyap Desai  * mpi3mr_setup_isr - Setup ISR for the controller
666824a1566SKashyap Desai  * @mrioc: Adapter instance reference
667824a1566SKashyap Desai  * @setup_one: Request one IRQ or more
668824a1566SKashyap Desai  *
669824a1566SKashyap Desai  * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
670824a1566SKashyap Desai  *
671824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
672824a1566SKashyap Desai  */
673824a1566SKashyap Desai static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
674824a1566SKashyap Desai {
675824a1566SKashyap Desai 	unsigned int irq_flags = PCI_IRQ_MSIX;
6762938beddSDan Carpenter 	int max_vectors;
6772938beddSDan Carpenter 	int retval;
6782938beddSDan Carpenter 	int i;
679824a1566SKashyap Desai 	struct irq_affinity desc = { .pre_vectors =  1};
680824a1566SKashyap Desai 
681fe6db615SSreekanth Reddy 	if (mrioc->is_intr_info_set)
682fe6db615SSreekanth Reddy 		return 0;
683fe6db615SSreekanth Reddy 
684824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
685824a1566SKashyap Desai 
686824a1566SKashyap Desai 	if (setup_one || reset_devices)
687824a1566SKashyap Desai 		max_vectors = 1;
688824a1566SKashyap Desai 	else {
689824a1566SKashyap Desai 		max_vectors =
690824a1566SKashyap Desai 		    min_t(int, mrioc->cpu_count + 1, mrioc->msix_count);
691824a1566SKashyap Desai 
692824a1566SKashyap Desai 		ioc_info(mrioc,
693824a1566SKashyap Desai 		    "MSI-X vectors supported: %d, no of cores: %d,",
694824a1566SKashyap Desai 		    mrioc->msix_count, mrioc->cpu_count);
695824a1566SKashyap Desai 		ioc_info(mrioc,
696824a1566SKashyap Desai 		    "MSI-x vectors requested: %d\n", max_vectors);
697824a1566SKashyap Desai 	}
698824a1566SKashyap Desai 
699824a1566SKashyap Desai 	irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
700824a1566SKashyap Desai 
701c9566231SKashyap Desai 	mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0;
7022938beddSDan Carpenter 	retval = pci_alloc_irq_vectors_affinity(mrioc->pdev,
703824a1566SKashyap Desai 				1, max_vectors, irq_flags, &desc);
7042938beddSDan Carpenter 	if (retval < 0) {
705824a1566SKashyap Desai 		ioc_err(mrioc, "Cannot alloc irq vectors\n");
706824a1566SKashyap Desai 		goto out_failed;
707824a1566SKashyap Desai 	}
7082938beddSDan Carpenter 	if (retval != max_vectors) {
709824a1566SKashyap Desai 		ioc_info(mrioc,
710824a1566SKashyap Desai 		    "allocated vectors (%d) are less than configured (%d)\n",
7112938beddSDan Carpenter 		    retval, max_vectors);
712c9566231SKashyap Desai 		/*
713c9566231SKashyap Desai 		 * If only one MSI-x is allocated, then MSI-x 0 will be shared
714c9566231SKashyap Desai 		 * between Admin queue and operational queue
715c9566231SKashyap Desai 		 */
7162938beddSDan Carpenter 		if (retval == 1)
717c9566231SKashyap Desai 			mrioc->op_reply_q_offset = 0;
718824a1566SKashyap Desai 
7192938beddSDan Carpenter 		max_vectors = retval;
720824a1566SKashyap Desai 	}
721824a1566SKashyap Desai 	mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
722824a1566SKashyap Desai 	    GFP_KERNEL);
723824a1566SKashyap Desai 	if (!mrioc->intr_info) {
7242938beddSDan Carpenter 		retval = -ENOMEM;
725824a1566SKashyap Desai 		pci_free_irq_vectors(mrioc->pdev);
726824a1566SKashyap Desai 		goto out_failed;
727824a1566SKashyap Desai 	}
728824a1566SKashyap Desai 	for (i = 0; i < max_vectors; i++) {
729824a1566SKashyap Desai 		retval = mpi3mr_request_irq(mrioc, i);
730824a1566SKashyap Desai 		if (retval) {
731824a1566SKashyap Desai 			mrioc->intr_info_count = i;
732824a1566SKashyap Desai 			goto out_failed;
733824a1566SKashyap Desai 		}
734824a1566SKashyap Desai 	}
735fe6db615SSreekanth Reddy 	if (reset_devices || !setup_one)
736fe6db615SSreekanth Reddy 		mrioc->is_intr_info_set = true;
737824a1566SKashyap Desai 	mrioc->intr_info_count = max_vectors;
738824a1566SKashyap Desai 	mpi3mr_ioc_enable_intr(mrioc);
7392938beddSDan Carpenter 	return 0;
7402938beddSDan Carpenter 
741824a1566SKashyap Desai out_failed:
742824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
743824a1566SKashyap Desai 
744824a1566SKashyap Desai 	return retval;
745824a1566SKashyap Desai }
746824a1566SKashyap Desai 
747824a1566SKashyap Desai static const struct {
748824a1566SKashyap Desai 	enum mpi3mr_iocstate value;
749824a1566SKashyap Desai 	char *name;
750824a1566SKashyap Desai } mrioc_states[] = {
751824a1566SKashyap Desai 	{ MRIOC_STATE_READY, "ready" },
752824a1566SKashyap Desai 	{ MRIOC_STATE_FAULT, "fault" },
753824a1566SKashyap Desai 	{ MRIOC_STATE_RESET, "reset" },
754824a1566SKashyap Desai 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
755824a1566SKashyap Desai 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
756824a1566SKashyap Desai 	{ MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
757824a1566SKashyap Desai };
758824a1566SKashyap Desai 
759824a1566SKashyap Desai static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
760824a1566SKashyap Desai {
761824a1566SKashyap Desai 	int i;
762824a1566SKashyap Desai 	char *name = NULL;
763824a1566SKashyap Desai 
764824a1566SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
765824a1566SKashyap Desai 		if (mrioc_states[i].value == mrioc_state) {
766824a1566SKashyap Desai 			name = mrioc_states[i].name;
767824a1566SKashyap Desai 			break;
768824a1566SKashyap Desai 		}
769824a1566SKashyap Desai 	}
770824a1566SKashyap Desai 	return name;
771824a1566SKashyap Desai }
772824a1566SKashyap Desai 
773f061178eSKashyap Desai /* Reset reason to name mapper structure*/
774f061178eSKashyap Desai static const struct {
775f061178eSKashyap Desai 	enum mpi3mr_reset_reason value;
776f061178eSKashyap Desai 	char *name;
777f061178eSKashyap Desai } mpi3mr_reset_reason_codes[] = {
778f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
779f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
780f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCTL, "application invocation" },
781f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EH_HOS, "error handling" },
782f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
783f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCTL_TIMEOUT, "IOCTL timeout" },
784f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
785f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
786f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
787f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
788f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
789f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
790f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
791f061178eSKashyap Desai 	{
792f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
793f061178eSKashyap Desai 		"create request queue timeout"
794f061178eSKashyap Desai 	},
795f061178eSKashyap Desai 	{
796f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
797f061178eSKashyap Desai 		"create reply queue timeout"
798f061178eSKashyap Desai 	},
799f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
800f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
801f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
802f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
803f061178eSKashyap Desai 	{
804f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CIACTVRST_TIMER,
805f061178eSKashyap Desai 		"component image activation timeout"
806f061178eSKashyap Desai 	},
807f061178eSKashyap Desai 	{
808f061178eSKashyap Desai 		MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
809f061178eSKashyap Desai 		"get package version timeout"
810f061178eSKashyap Desai 	},
811f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
812f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
813b64845a7SSreekanth Reddy 	{ MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronus reset" },
814f061178eSKashyap Desai };
815f061178eSKashyap Desai 
816f061178eSKashyap Desai /**
817f061178eSKashyap Desai  * mpi3mr_reset_rc_name - get reset reason code name
818f061178eSKashyap Desai  * @reason_code: reset reason code value
819f061178eSKashyap Desai  *
820f061178eSKashyap Desai  * Map reset reason to an NULL terminated ASCII string
821f061178eSKashyap Desai  *
822f061178eSKashyap Desai  * Return: name corresponding to reset reason value or NULL.
823f061178eSKashyap Desai  */
824f061178eSKashyap Desai static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
825f061178eSKashyap Desai {
826f061178eSKashyap Desai 	int i;
827f061178eSKashyap Desai 	char *name = NULL;
828f061178eSKashyap Desai 
829f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) {
830f061178eSKashyap Desai 		if (mpi3mr_reset_reason_codes[i].value == reason_code) {
831f061178eSKashyap Desai 			name = mpi3mr_reset_reason_codes[i].name;
832f061178eSKashyap Desai 			break;
833f061178eSKashyap Desai 		}
834f061178eSKashyap Desai 	}
835f061178eSKashyap Desai 	return name;
836f061178eSKashyap Desai }
837f061178eSKashyap Desai 
838f061178eSKashyap Desai /* Reset type to name mapper structure*/
839f061178eSKashyap Desai static const struct {
840f061178eSKashyap Desai 	u16 reset_type;
841f061178eSKashyap Desai 	char *name;
842f061178eSKashyap Desai } mpi3mr_reset_types[] = {
843f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
844f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
845f061178eSKashyap Desai };
846f061178eSKashyap Desai 
847f061178eSKashyap Desai /**
848f061178eSKashyap Desai  * mpi3mr_reset_type_name - get reset type name
849f061178eSKashyap Desai  * @reset_type: reset type value
850f061178eSKashyap Desai  *
851f061178eSKashyap Desai  * Map reset type to an NULL terminated ASCII string
852f061178eSKashyap Desai  *
853f061178eSKashyap Desai  * Return: name corresponding to reset type value or NULL.
854f061178eSKashyap Desai  */
855f061178eSKashyap Desai static const char *mpi3mr_reset_type_name(u16 reset_type)
856f061178eSKashyap Desai {
857f061178eSKashyap Desai 	int i;
858f061178eSKashyap Desai 	char *name = NULL;
859f061178eSKashyap Desai 
860f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) {
861f061178eSKashyap Desai 		if (mpi3mr_reset_types[i].reset_type == reset_type) {
862f061178eSKashyap Desai 			name = mpi3mr_reset_types[i].name;
863f061178eSKashyap Desai 			break;
864f061178eSKashyap Desai 		}
865f061178eSKashyap Desai 	}
866f061178eSKashyap Desai 	return name;
867f061178eSKashyap Desai }
868f061178eSKashyap Desai 
869824a1566SKashyap Desai /**
870824a1566SKashyap Desai  * mpi3mr_print_fault_info - Display fault information
871824a1566SKashyap Desai  * @mrioc: Adapter instance reference
872824a1566SKashyap Desai  *
873824a1566SKashyap Desai  * Display the controller fault information if there is a
874824a1566SKashyap Desai  * controller fault.
875824a1566SKashyap Desai  *
876824a1566SKashyap Desai  * Return: Nothing.
877824a1566SKashyap Desai  */
878b64845a7SSreekanth Reddy void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
879824a1566SKashyap Desai {
880824a1566SKashyap Desai 	u32 ioc_status, code, code1, code2, code3;
881824a1566SKashyap Desai 
882824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
883824a1566SKashyap Desai 
884824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
885824a1566SKashyap Desai 		code = readl(&mrioc->sysif_regs->fault);
886824a1566SKashyap Desai 		code1 = readl(&mrioc->sysif_regs->fault_info[0]);
887824a1566SKashyap Desai 		code2 = readl(&mrioc->sysif_regs->fault_info[1]);
888824a1566SKashyap Desai 		code3 = readl(&mrioc->sysif_regs->fault_info[2]);
889824a1566SKashyap Desai 
890824a1566SKashyap Desai 		ioc_info(mrioc,
891824a1566SKashyap Desai 		    "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
892824a1566SKashyap Desai 		    code, code1, code2, code3);
893824a1566SKashyap Desai 	}
894824a1566SKashyap Desai }
895824a1566SKashyap Desai 
896824a1566SKashyap Desai /**
897824a1566SKashyap Desai  * mpi3mr_get_iocstate - Get IOC State
898824a1566SKashyap Desai  * @mrioc: Adapter instance reference
899824a1566SKashyap Desai  *
900824a1566SKashyap Desai  * Return a proper IOC state enum based on the IOC status and
901824a1566SKashyap Desai  * IOC configuration and unrcoverable state of the controller.
902824a1566SKashyap Desai  *
903824a1566SKashyap Desai  * Return: Current IOC state.
904824a1566SKashyap Desai  */
905824a1566SKashyap Desai enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
906824a1566SKashyap Desai {
907824a1566SKashyap Desai 	u32 ioc_status, ioc_config;
908824a1566SKashyap Desai 	u8 ready, enabled;
909824a1566SKashyap Desai 
910824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
911824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
912824a1566SKashyap Desai 
913824a1566SKashyap Desai 	if (mrioc->unrecoverable)
914824a1566SKashyap Desai 		return MRIOC_STATE_UNRECOVERABLE;
915824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
916824a1566SKashyap Desai 		return MRIOC_STATE_FAULT;
917824a1566SKashyap Desai 
918824a1566SKashyap Desai 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
919824a1566SKashyap Desai 	enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
920824a1566SKashyap Desai 
921824a1566SKashyap Desai 	if (ready && enabled)
922824a1566SKashyap Desai 		return MRIOC_STATE_READY;
923824a1566SKashyap Desai 	if ((!ready) && (!enabled))
924824a1566SKashyap Desai 		return MRIOC_STATE_RESET;
925824a1566SKashyap Desai 	if ((!ready) && (enabled))
926824a1566SKashyap Desai 		return MRIOC_STATE_BECOMING_READY;
927824a1566SKashyap Desai 
928824a1566SKashyap Desai 	return MRIOC_STATE_RESET_REQUESTED;
929824a1566SKashyap Desai }
930824a1566SKashyap Desai 
931824a1566SKashyap Desai /**
932824a1566SKashyap Desai  * mpi3mr_clear_reset_history - clear reset history
933824a1566SKashyap Desai  * @mrioc: Adapter instance reference
934824a1566SKashyap Desai  *
935824a1566SKashyap Desai  * Write the reset history bit in IOC status to clear the bit,
936824a1566SKashyap Desai  * if it is already set.
937824a1566SKashyap Desai  *
938824a1566SKashyap Desai  * Return: Nothing.
939824a1566SKashyap Desai  */
940824a1566SKashyap Desai static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
941824a1566SKashyap Desai {
942824a1566SKashyap Desai 	u32 ioc_status;
943824a1566SKashyap Desai 
944824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
945824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
946824a1566SKashyap Desai 		writel(ioc_status, &mrioc->sysif_regs->ioc_status);
947824a1566SKashyap Desai }
948824a1566SKashyap Desai 
949824a1566SKashyap Desai /**
950824a1566SKashyap Desai  * mpi3mr_issue_and_process_mur - Message unit Reset handler
951824a1566SKashyap Desai  * @mrioc: Adapter instance reference
952824a1566SKashyap Desai  * @reset_reason: Reset reason code
953824a1566SKashyap Desai  *
954824a1566SKashyap Desai  * Issue Message unit Reset to the controller and wait for it to
955824a1566SKashyap Desai  * be complete.
956824a1566SKashyap Desai  *
957824a1566SKashyap Desai  * Return: 0 on success, -1 on failure.
958824a1566SKashyap Desai  */
959824a1566SKashyap Desai static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
960824a1566SKashyap Desai 	u32 reset_reason)
961824a1566SKashyap Desai {
962824a1566SKashyap Desai 	u32 ioc_config, timeout, ioc_status;
963824a1566SKashyap Desai 	int retval = -1;
964824a1566SKashyap Desai 
965824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
966824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
967824a1566SKashyap Desai 		ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
968824a1566SKashyap Desai 		return retval;
969824a1566SKashyap Desai 	}
970824a1566SKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
971824a1566SKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
972824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
973824a1566SKashyap Desai 	ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
974824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
975824a1566SKashyap Desai 
976b64845a7SSreekanth Reddy 	timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
977824a1566SKashyap Desai 	do {
978824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
979824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
980824a1566SKashyap Desai 			mpi3mr_clear_reset_history(mrioc);
981824a1566SKashyap Desai 			break;
982824a1566SKashyap Desai 		}
983b64845a7SSreekanth Reddy 		if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
984b64845a7SSreekanth Reddy 			mpi3mr_print_fault_info(mrioc);
985b64845a7SSreekanth Reddy 			break;
986824a1566SKashyap Desai 		}
987824a1566SKashyap Desai 		msleep(100);
988824a1566SKashyap Desai 	} while (--timeout);
989824a1566SKashyap Desai 
990824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
991b64845a7SSreekanth Reddy 	if (timeout && !((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
992b64845a7SSreekanth Reddy 	      (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
993b64845a7SSreekanth Reddy 	      (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
994b64845a7SSreekanth Reddy 		retval = 0;
995824a1566SKashyap Desai 
996824a1566SKashyap Desai 	ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
997824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status, ioc_config);
998824a1566SKashyap Desai 	return retval;
999824a1566SKashyap Desai }
1000824a1566SKashyap Desai 
1001824a1566SKashyap Desai /**
1002*c5758fc7SSreekanth Reddy  * mpi3mr_revalidate_factsdata - validate IOCFacts parameters
1003*c5758fc7SSreekanth Reddy  * during reset/resume
1004*c5758fc7SSreekanth Reddy  * @mrioc: Adapter instance reference
1005*c5758fc7SSreekanth Reddy  *
1006*c5758fc7SSreekanth Reddy  * Return zero if the new IOCFacts parameters value is compatible with
1007*c5758fc7SSreekanth Reddy  * older values else return -EPERM
1008*c5758fc7SSreekanth Reddy  */
1009*c5758fc7SSreekanth Reddy static int
1010*c5758fc7SSreekanth Reddy mpi3mr_revalidate_factsdata(struct mpi3mr_ioc *mrioc)
1011*c5758fc7SSreekanth Reddy {
1012*c5758fc7SSreekanth Reddy 	u16 dev_handle_bitmap_sz;
1013*c5758fc7SSreekanth Reddy 	void *removepend_bitmap;
1014*c5758fc7SSreekanth Reddy 
1015*c5758fc7SSreekanth Reddy 	if (mrioc->facts.reply_sz > mrioc->reply_sz) {
1016*c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1017*c5758fc7SSreekanth Reddy 		    "cannot increase reply size from %d to %d\n",
1018*c5758fc7SSreekanth Reddy 		    mrioc->reply_sz, mrioc->facts.reply_sz);
1019*c5758fc7SSreekanth Reddy 		return -EPERM;
1020*c5758fc7SSreekanth Reddy 	}
1021*c5758fc7SSreekanth Reddy 
1022*c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_reply_q < mrioc->num_op_reply_q) {
1023*c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1024*c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational reply queues from %d to %d\n",
1025*c5758fc7SSreekanth Reddy 		    mrioc->num_op_reply_q,
1026*c5758fc7SSreekanth Reddy 		    mrioc->facts.max_op_reply_q);
1027*c5758fc7SSreekanth Reddy 		return -EPERM;
1028*c5758fc7SSreekanth Reddy 	}
1029*c5758fc7SSreekanth Reddy 
1030*c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_req_q < mrioc->num_op_req_q) {
1031*c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1032*c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational request queues from %d to %d\n",
1033*c5758fc7SSreekanth Reddy 		    mrioc->num_op_req_q, mrioc->facts.max_op_req_q);
1034*c5758fc7SSreekanth Reddy 		return -EPERM;
1035*c5758fc7SSreekanth Reddy 	}
1036*c5758fc7SSreekanth Reddy 
1037*c5758fc7SSreekanth Reddy 	dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
1038*c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_devhandle % 8)
1039*c5758fc7SSreekanth Reddy 		dev_handle_bitmap_sz++;
1040*c5758fc7SSreekanth Reddy 	if (dev_handle_bitmap_sz > mrioc->dev_handle_bitmap_sz) {
1041*c5758fc7SSreekanth Reddy 		removepend_bitmap = krealloc(mrioc->removepend_bitmap,
1042*c5758fc7SSreekanth Reddy 		    dev_handle_bitmap_sz, GFP_KERNEL);
1043*c5758fc7SSreekanth Reddy 		if (!removepend_bitmap) {
1044*c5758fc7SSreekanth Reddy 			ioc_err(mrioc,
1045*c5758fc7SSreekanth Reddy 			    "failed to increase removepend_bitmap sz from: %d to %d\n",
1046*c5758fc7SSreekanth Reddy 			    mrioc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
1047*c5758fc7SSreekanth Reddy 			return -EPERM;
1048*c5758fc7SSreekanth Reddy 		}
1049*c5758fc7SSreekanth Reddy 		memset(removepend_bitmap + mrioc->dev_handle_bitmap_sz, 0,
1050*c5758fc7SSreekanth Reddy 		    dev_handle_bitmap_sz - mrioc->dev_handle_bitmap_sz);
1051*c5758fc7SSreekanth Reddy 		mrioc->removepend_bitmap = removepend_bitmap;
1052*c5758fc7SSreekanth Reddy 		ioc_info(mrioc,
1053*c5758fc7SSreekanth Reddy 		    "increased dev_handle_bitmap_sz from %d to %d\n",
1054*c5758fc7SSreekanth Reddy 		    mrioc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
1055*c5758fc7SSreekanth Reddy 		mrioc->dev_handle_bitmap_sz = dev_handle_bitmap_sz;
1056*c5758fc7SSreekanth Reddy 	}
1057*c5758fc7SSreekanth Reddy 
1058*c5758fc7SSreekanth Reddy 	return 0;
1059*c5758fc7SSreekanth Reddy }
1060*c5758fc7SSreekanth Reddy 
1061*c5758fc7SSreekanth Reddy /**
1062824a1566SKashyap Desai  * mpi3mr_bring_ioc_ready - Bring controller to ready state
1063824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1064824a1566SKashyap Desai  *
1065824a1566SKashyap Desai  * Set Enable IOC bit in IOC configuration register and wait for
1066824a1566SKashyap Desai  * the controller to become ready.
1067824a1566SKashyap Desai  *
106859bd9cfeSSreekanth Reddy  * Return: 0 on success, appropriate error on failure.
1069824a1566SKashyap Desai  */
1070824a1566SKashyap Desai static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
1071824a1566SKashyap Desai {
107259bd9cfeSSreekanth Reddy 	u32 ioc_config, ioc_status, timeout;
107359bd9cfeSSreekanth Reddy 	int retval = 0;
107459bd9cfeSSreekanth Reddy 	enum mpi3mr_iocstate ioc_state;
107559bd9cfeSSreekanth Reddy 	u64 base_info;
1076824a1566SKashyap Desai 
107759bd9cfeSSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
107859bd9cfeSSreekanth Reddy 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
107959bd9cfeSSreekanth Reddy 	base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
108059bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ioc_status(0x%08x), ioc_config(0x%08x), ioc_info(0x%016llx) at the bringup\n",
108159bd9cfeSSreekanth Reddy 	    ioc_status, ioc_config, base_info);
108259bd9cfeSSreekanth Reddy 
108359bd9cfeSSreekanth Reddy 	/*The timeout value is in 2sec unit, changing it to seconds*/
108459bd9cfeSSreekanth Reddy 	mrioc->ready_timeout =
108559bd9cfeSSreekanth Reddy 	    ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
108659bd9cfeSSreekanth Reddy 	    MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
108759bd9cfeSSreekanth Reddy 
108859bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ready timeout: %d seconds\n", mrioc->ready_timeout);
108959bd9cfeSSreekanth Reddy 
109059bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
109159bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "controller is in %s state during detection\n",
109259bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
109359bd9cfeSSreekanth Reddy 
109459bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
109559bd9cfeSSreekanth Reddy 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
109659bd9cfeSSreekanth Reddy 		timeout = mrioc->ready_timeout * 10;
109759bd9cfeSSreekanth Reddy 		do {
109859bd9cfeSSreekanth Reddy 			msleep(100);
109959bd9cfeSSreekanth Reddy 		} while (--timeout);
110059bd9cfeSSreekanth Reddy 
110159bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
110259bd9cfeSSreekanth Reddy 		ioc_info(mrioc,
110359bd9cfeSSreekanth Reddy 		    "controller is in %s state after waiting to reset\n",
110459bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
110559bd9cfeSSreekanth Reddy 	}
110659bd9cfeSSreekanth Reddy 
110759bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_READY) {
110859bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing message unit reset (MUR) to bring to reset state\n");
110959bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_and_process_mur(mrioc,
111059bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
111159bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
111259bd9cfeSSreekanth Reddy 		if (retval)
111359bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
111459bd9cfeSSreekanth Reddy 			    "message unit reset failed with error %d current state %s\n",
111559bd9cfeSSreekanth Reddy 			    retval, mpi3mr_iocstate_name(ioc_state));
111659bd9cfeSSreekanth Reddy 	}
111759bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
111859bd9cfeSSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
111959bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing soft reset to bring to reset state\n");
112059bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_reset(mrioc,
112159bd9cfeSSreekanth Reddy 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
112259bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
112359bd9cfeSSreekanth Reddy 		if (retval) {
112459bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
112559bd9cfeSSreekanth Reddy 			    "soft reset failed with error %d\n", retval);
112659bd9cfeSSreekanth Reddy 			goto out_failed;
112759bd9cfeSSreekanth Reddy 		}
112859bd9cfeSSreekanth Reddy 	}
112959bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
113059bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
113159bd9cfeSSreekanth Reddy 		ioc_err(mrioc,
113259bd9cfeSSreekanth Reddy 		    "cannot bring controller to reset state, current state: %s\n",
113359bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
113459bd9cfeSSreekanth Reddy 		goto out_failed;
113559bd9cfeSSreekanth Reddy 	}
113659bd9cfeSSreekanth Reddy 	mpi3mr_clear_reset_history(mrioc);
113759bd9cfeSSreekanth Reddy 	retval = mpi3mr_setup_admin_qpair(mrioc);
113859bd9cfeSSreekanth Reddy 	if (retval) {
113959bd9cfeSSreekanth Reddy 		ioc_err(mrioc, "failed to setup admin queues: error %d\n",
114059bd9cfeSSreekanth Reddy 		    retval);
114159bd9cfeSSreekanth Reddy 		goto out_failed;
114259bd9cfeSSreekanth Reddy 	}
114359bd9cfeSSreekanth Reddy 
114459bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "bringing controller to ready state\n");
1145824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1146824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1147824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1148824a1566SKashyap Desai 
1149824a1566SKashyap Desai 	timeout = mrioc->ready_timeout * 10;
1150824a1566SKashyap Desai 	do {
115159bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
115259bd9cfeSSreekanth Reddy 		if (ioc_state == MRIOC_STATE_READY) {
115359bd9cfeSSreekanth Reddy 			ioc_info(mrioc,
115459bd9cfeSSreekanth Reddy 			    "successfully transistioned to %s state\n",
115559bd9cfeSSreekanth Reddy 			    mpi3mr_iocstate_name(ioc_state));
1156824a1566SKashyap Desai 			return 0;
115759bd9cfeSSreekanth Reddy 		}
1158824a1566SKashyap Desai 		msleep(100);
1159824a1566SKashyap Desai 	} while (--timeout);
1160824a1566SKashyap Desai 
116159bd9cfeSSreekanth Reddy out_failed:
116259bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
116359bd9cfeSSreekanth Reddy 	ioc_err(mrioc,
116459bd9cfeSSreekanth Reddy 	    "failed to bring to ready state,  current state: %s\n",
116559bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
116659bd9cfeSSreekanth Reddy 	return retval;
1167824a1566SKashyap Desai }
1168824a1566SKashyap Desai 
1169824a1566SKashyap Desai /**
1170f061178eSKashyap Desai  * mpi3mr_soft_reset_success - Check softreset is success or not
1171f061178eSKashyap Desai  * @ioc_status: IOC status register value
1172f061178eSKashyap Desai  * @ioc_config: IOC config register value
1173f061178eSKashyap Desai  *
1174f061178eSKashyap Desai  * Check whether the soft reset is successful or not based on
1175f061178eSKashyap Desai  * IOC status and IOC config register values.
1176f061178eSKashyap Desai  *
1177f061178eSKashyap Desai  * Return: True when the soft reset is success, false otherwise.
1178f061178eSKashyap Desai  */
1179f061178eSKashyap Desai static inline bool
1180f061178eSKashyap Desai mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config)
1181f061178eSKashyap Desai {
1182f061178eSKashyap Desai 	if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1183f061178eSKashyap Desai 	    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1184f061178eSKashyap Desai 		return true;
1185f061178eSKashyap Desai 	return false;
1186f061178eSKashyap Desai }
1187f061178eSKashyap Desai 
1188f061178eSKashyap Desai /**
1189f061178eSKashyap Desai  * mpi3mr_diagfault_success - Check diag fault is success or not
1190f061178eSKashyap Desai  * @mrioc: Adapter reference
1191f061178eSKashyap Desai  * @ioc_status: IOC status register value
1192f061178eSKashyap Desai  *
1193f061178eSKashyap Desai  * Check whether the controller hit diag reset fault code.
1194f061178eSKashyap Desai  *
1195f061178eSKashyap Desai  * Return: True when there is diag fault, false otherwise.
1196f061178eSKashyap Desai  */
1197f061178eSKashyap Desai static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc,
1198f061178eSKashyap Desai 	u32 ioc_status)
1199f061178eSKashyap Desai {
1200f061178eSKashyap Desai 	u32 fault;
1201f061178eSKashyap Desai 
1202f061178eSKashyap Desai 	if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
1203f061178eSKashyap Desai 		return false;
1204f061178eSKashyap Desai 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
1205b64845a7SSreekanth Reddy 	if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) {
1206b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1207f061178eSKashyap Desai 		return true;
1208b64845a7SSreekanth Reddy 	}
1209f061178eSKashyap Desai 	return false;
1210f061178eSKashyap Desai }
1211f061178eSKashyap Desai 
1212f061178eSKashyap Desai /**
1213824a1566SKashyap Desai  * mpi3mr_set_diagsave - Set diag save bit for snapdump
1214824a1566SKashyap Desai  * @mrioc: Adapter reference
1215824a1566SKashyap Desai  *
1216824a1566SKashyap Desai  * Set diag save bit in IOC configuration register to enable
1217824a1566SKashyap Desai  * snapdump.
1218824a1566SKashyap Desai  *
1219824a1566SKashyap Desai  * Return: Nothing.
1220824a1566SKashyap Desai  */
1221824a1566SKashyap Desai static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
1222824a1566SKashyap Desai {
1223824a1566SKashyap Desai 	u32 ioc_config;
1224824a1566SKashyap Desai 
1225824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1226824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
1227824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1228824a1566SKashyap Desai }
1229824a1566SKashyap Desai 
1230824a1566SKashyap Desai /**
1231824a1566SKashyap Desai  * mpi3mr_issue_reset - Issue reset to the controller
1232824a1566SKashyap Desai  * @mrioc: Adapter reference
1233824a1566SKashyap Desai  * @reset_type: Reset type
1234824a1566SKashyap Desai  * @reset_reason: Reset reason code
1235824a1566SKashyap Desai  *
1236f061178eSKashyap Desai  * Unlock the host diagnostic registers and write the specific
1237f061178eSKashyap Desai  * reset type to that, wait for reset acknowledgment from the
1238f061178eSKashyap Desai  * controller, if the reset is not successful retry for the
1239f061178eSKashyap Desai  * predefined number of times.
1240824a1566SKashyap Desai  *
1241824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1242824a1566SKashyap Desai  */
1243824a1566SKashyap Desai static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
1244824a1566SKashyap Desai 	u32 reset_reason)
1245824a1566SKashyap Desai {
1246f061178eSKashyap Desai 	int retval = -1;
1247b64845a7SSreekanth Reddy 	u8 unlock_retry_count = 0;
1248b64845a7SSreekanth Reddy 	u32 host_diagnostic, ioc_status, ioc_config;
1249b64845a7SSreekanth Reddy 	u32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1250f061178eSKashyap Desai 
1251f061178eSKashyap Desai 	if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
1252f061178eSKashyap Desai 	    (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
1253b64845a7SSreekanth Reddy 		return retval;
1254f061178eSKashyap Desai 	if (mrioc->unrecoverable)
1255b64845a7SSreekanth Reddy 		return retval;
1256b64845a7SSreekanth Reddy 	if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) {
1257b64845a7SSreekanth Reddy 		retval = 0;
1258b64845a7SSreekanth Reddy 		return retval;
1259b64845a7SSreekanth Reddy 	}
1260b64845a7SSreekanth Reddy 
1261b64845a7SSreekanth Reddy 	ioc_info(mrioc, "%s reset due to %s(0x%x)\n",
1262b64845a7SSreekanth Reddy 	    mpi3mr_reset_type_name(reset_type),
1263b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason), reset_reason);
1264b64845a7SSreekanth Reddy 
1265f061178eSKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1266f061178eSKashyap Desai 	do {
1267f061178eSKashyap Desai 		ioc_info(mrioc,
1268f061178eSKashyap Desai 		    "Write magic sequence to unlock host diag register (retry=%d)\n",
1269f061178eSKashyap Desai 		    ++unlock_retry_count);
1270f061178eSKashyap Desai 		if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
1271b64845a7SSreekanth Reddy 			ioc_err(mrioc,
1272b64845a7SSreekanth Reddy 			    "%s reset failed due to unlock failure, host_diagnostic(0x%08x)\n",
1273b64845a7SSreekanth Reddy 			    mpi3mr_reset_type_name(reset_type),
1274b64845a7SSreekanth Reddy 			    host_diagnostic);
1275f061178eSKashyap Desai 			mrioc->unrecoverable = 1;
1276b64845a7SSreekanth Reddy 			return retval;
1277f061178eSKashyap Desai 		}
1278f061178eSKashyap Desai 
1279f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH,
1280f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1281f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST,
1282f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1283f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1284f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1285f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD,
1286f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1287f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH,
1288f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1289f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH,
1290f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1291f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH,
1292f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1293f061178eSKashyap Desai 		usleep_range(1000, 1100);
1294f061178eSKashyap Desai 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
1295f061178eSKashyap Desai 		ioc_info(mrioc,
1296f061178eSKashyap Desai 		    "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
1297f061178eSKashyap Desai 		    unlock_retry_count, host_diagnostic);
1298f061178eSKashyap Desai 	} while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
1299f061178eSKashyap Desai 
1300f061178eSKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1301f061178eSKashyap Desai 	writel(host_diagnostic | reset_type,
1302f061178eSKashyap Desai 	    &mrioc->sysif_regs->host_diagnostic);
1303b64845a7SSreekanth Reddy 	switch (reset_type) {
1304b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET:
1305f061178eSKashyap Desai 		do {
1306f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1307f061178eSKashyap Desai 			ioc_config =
1308f061178eSKashyap Desai 			    readl(&mrioc->sysif_regs->ioc_configuration);
1309b64845a7SSreekanth Reddy 			if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1310b64845a7SSreekanth Reddy 			    && mpi3mr_soft_reset_success(ioc_status, ioc_config)
1311b64845a7SSreekanth Reddy 			    ) {
1312b64845a7SSreekanth Reddy 				mpi3mr_clear_reset_history(mrioc);
1313f061178eSKashyap Desai 				retval = 0;
1314f061178eSKashyap Desai 				break;
1315f061178eSKashyap Desai 			}
1316f061178eSKashyap Desai 			msleep(100);
1317f061178eSKashyap Desai 		} while (--timeout);
1318b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1319b64845a7SSreekanth Reddy 		break;
1320b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT:
1321f061178eSKashyap Desai 		do {
1322f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1323f061178eSKashyap Desai 			if (mpi3mr_diagfault_success(mrioc, ioc_status)) {
1324f061178eSKashyap Desai 				retval = 0;
1325f061178eSKashyap Desai 				break;
1326f061178eSKashyap Desai 			}
1327f061178eSKashyap Desai 			msleep(100);
1328f061178eSKashyap Desai 		} while (--timeout);
1329b64845a7SSreekanth Reddy 		break;
1330b64845a7SSreekanth Reddy 	default:
1331b64845a7SSreekanth Reddy 		break;
1332b64845a7SSreekanth Reddy 	}
1333b64845a7SSreekanth Reddy 
1334f061178eSKashyap Desai 	writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1335f061178eSKashyap Desai 	    &mrioc->sysif_regs->write_sequence);
1336f061178eSKashyap Desai 
1337f061178eSKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1338b64845a7SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1339f061178eSKashyap Desai 	ioc_info(mrioc,
1340b64845a7SSreekanth Reddy 	    "ioc_status/ioc_onfig after %s reset is (0x%x)/(0x%x)\n",
1341f061178eSKashyap Desai 	    (!retval)?"successful":"failed", ioc_status,
1342f061178eSKashyap Desai 	    ioc_config);
1343b64845a7SSreekanth Reddy 	if (retval)
1344b64845a7SSreekanth Reddy 		mrioc->unrecoverable = 1;
1345f061178eSKashyap Desai 	return retval;
1346824a1566SKashyap Desai }
1347824a1566SKashyap Desai 
1348824a1566SKashyap Desai /**
1349824a1566SKashyap Desai  * mpi3mr_admin_request_post - Post request to admin queue
1350824a1566SKashyap Desai  * @mrioc: Adapter reference
1351824a1566SKashyap Desai  * @admin_req: MPI3 request
1352824a1566SKashyap Desai  * @admin_req_sz: Request size
1353824a1566SKashyap Desai  * @ignore_reset: Ignore reset in process
1354824a1566SKashyap Desai  *
1355824a1566SKashyap Desai  * Post the MPI3 request into admin request queue and
1356824a1566SKashyap Desai  * inform the controller, if the queue is full return
1357824a1566SKashyap Desai  * appropriate error.
1358824a1566SKashyap Desai  *
1359824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1360824a1566SKashyap Desai  */
1361824a1566SKashyap Desai int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
1362824a1566SKashyap Desai 	u16 admin_req_sz, u8 ignore_reset)
1363824a1566SKashyap Desai {
1364824a1566SKashyap Desai 	u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
1365824a1566SKashyap Desai 	int retval = 0;
1366824a1566SKashyap Desai 	unsigned long flags;
1367824a1566SKashyap Desai 	u8 *areq_entry;
1368824a1566SKashyap Desai 
1369824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1370824a1566SKashyap Desai 		ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
1371824a1566SKashyap Desai 		return -EFAULT;
1372824a1566SKashyap Desai 	}
1373824a1566SKashyap Desai 
1374824a1566SKashyap Desai 	spin_lock_irqsave(&mrioc->admin_req_lock, flags);
1375824a1566SKashyap Desai 	areq_pi = mrioc->admin_req_pi;
1376824a1566SKashyap Desai 	areq_ci = mrioc->admin_req_ci;
1377824a1566SKashyap Desai 	max_entries = mrioc->num_admin_req;
1378824a1566SKashyap Desai 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
1379824a1566SKashyap Desai 	    (areq_pi == (max_entries - 1)))) {
1380824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ full condition detected\n");
1381824a1566SKashyap Desai 		retval = -EAGAIN;
1382824a1566SKashyap Desai 		goto out;
1383824a1566SKashyap Desai 	}
1384824a1566SKashyap Desai 	if (!ignore_reset && mrioc->reset_in_progress) {
1385824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
1386824a1566SKashyap Desai 		retval = -EAGAIN;
1387824a1566SKashyap Desai 		goto out;
1388824a1566SKashyap Desai 	}
1389824a1566SKashyap Desai 	areq_entry = (u8 *)mrioc->admin_req_base +
1390824a1566SKashyap Desai 	    (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
1391824a1566SKashyap Desai 	memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
1392824a1566SKashyap Desai 	memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
1393824a1566SKashyap Desai 
1394824a1566SKashyap Desai 	if (++areq_pi == max_entries)
1395824a1566SKashyap Desai 		areq_pi = 0;
1396824a1566SKashyap Desai 	mrioc->admin_req_pi = areq_pi;
1397824a1566SKashyap Desai 
1398824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
1399824a1566SKashyap Desai 
1400824a1566SKashyap Desai out:
1401824a1566SKashyap Desai 	spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
1402824a1566SKashyap Desai 
1403824a1566SKashyap Desai 	return retval;
1404824a1566SKashyap Desai }
1405824a1566SKashyap Desai 
1406824a1566SKashyap Desai /**
1407c9566231SKashyap Desai  * mpi3mr_free_op_req_q_segments - free request memory segments
1408c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1409c9566231SKashyap Desai  * @q_idx: operational request queue index
1410c9566231SKashyap Desai  *
1411c9566231SKashyap Desai  * Free memory segments allocated for operational request queue
1412c9566231SKashyap Desai  *
1413c9566231SKashyap Desai  * Return: Nothing.
1414c9566231SKashyap Desai  */
1415c9566231SKashyap Desai static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1416c9566231SKashyap Desai {
1417c9566231SKashyap Desai 	u16 j;
1418c9566231SKashyap Desai 	int size;
1419c9566231SKashyap Desai 	struct segments *segments;
1420c9566231SKashyap Desai 
1421c9566231SKashyap Desai 	segments = mrioc->req_qinfo[q_idx].q_segments;
1422c9566231SKashyap Desai 	if (!segments)
1423c9566231SKashyap Desai 		return;
1424c9566231SKashyap Desai 
1425c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1426c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1427c9566231SKashyap Desai 		if (mrioc->req_qinfo[q_idx].q_segment_list) {
1428c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1429c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1430c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list,
1431c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list_dma);
1432c9566231SKashyap Desai 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1433c9566231SKashyap Desai 		}
1434c9566231SKashyap Desai 	} else
1435c9566231SKashyap Desai 		size = mrioc->req_qinfo[q_idx].num_requests *
1436c9566231SKashyap Desai 		    mrioc->facts.op_req_sz;
1437c9566231SKashyap Desai 
1438c9566231SKashyap Desai 	for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) {
1439c9566231SKashyap Desai 		if (!segments[j].segment)
1440c9566231SKashyap Desai 			continue;
1441c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1442c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1443c9566231SKashyap Desai 		segments[j].segment = NULL;
1444c9566231SKashyap Desai 	}
1445c9566231SKashyap Desai 	kfree(mrioc->req_qinfo[q_idx].q_segments);
1446c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].q_segments = NULL;
1447c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].qid = 0;
1448c9566231SKashyap Desai }
1449c9566231SKashyap Desai 
1450c9566231SKashyap Desai /**
1451c9566231SKashyap Desai  * mpi3mr_free_op_reply_q_segments - free reply memory segments
1452c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1453c9566231SKashyap Desai  * @q_idx: operational reply queue index
1454c9566231SKashyap Desai  *
1455c9566231SKashyap Desai  * Free memory segments allocated for operational reply queue
1456c9566231SKashyap Desai  *
1457c9566231SKashyap Desai  * Return: Nothing.
1458c9566231SKashyap Desai  */
1459c9566231SKashyap Desai static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1460c9566231SKashyap Desai {
1461c9566231SKashyap Desai 	u16 j;
1462c9566231SKashyap Desai 	int size;
1463c9566231SKashyap Desai 	struct segments *segments;
1464c9566231SKashyap Desai 
1465c9566231SKashyap Desai 	segments = mrioc->op_reply_qinfo[q_idx].q_segments;
1466c9566231SKashyap Desai 	if (!segments)
1467c9566231SKashyap Desai 		return;
1468c9566231SKashyap Desai 
1469c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1470c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1471c9566231SKashyap Desai 		if (mrioc->op_reply_qinfo[q_idx].q_segment_list) {
1472c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1473c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1474c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list,
1475c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list_dma);
1476c9566231SKashyap Desai 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1477c9566231SKashyap Desai 		}
1478c9566231SKashyap Desai 	} else
1479c9566231SKashyap Desai 		size = mrioc->op_reply_qinfo[q_idx].segment_qd *
1480c9566231SKashyap Desai 		    mrioc->op_reply_desc_sz;
1481c9566231SKashyap Desai 
1482c9566231SKashyap Desai 	for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) {
1483c9566231SKashyap Desai 		if (!segments[j].segment)
1484c9566231SKashyap Desai 			continue;
1485c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1486c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1487c9566231SKashyap Desai 		segments[j].segment = NULL;
1488c9566231SKashyap Desai 	}
1489c9566231SKashyap Desai 
1490c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo[q_idx].q_segments);
1491c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].q_segments = NULL;
1492c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].qid = 0;
1493c9566231SKashyap Desai }
1494c9566231SKashyap Desai 
1495c9566231SKashyap Desai /**
1496c9566231SKashyap Desai  * mpi3mr_delete_op_reply_q - delete operational reply queue
1497c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1498c9566231SKashyap Desai  * @qidx: operational reply queue index
1499c9566231SKashyap Desai  *
1500c9566231SKashyap Desai  * Delete operatinal reply queue by issuing MPI request
1501c9566231SKashyap Desai  * through admin queue.
1502c9566231SKashyap Desai  *
1503c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1504c9566231SKashyap Desai  */
1505c9566231SKashyap Desai static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1506c9566231SKashyap Desai {
1507c9566231SKashyap Desai 	struct mpi3_delete_reply_queue_request delq_req;
1508c9566231SKashyap Desai 	int retval = 0;
1509c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1510c9566231SKashyap Desai 
1511c9566231SKashyap Desai 	reply_qid = mrioc->op_reply_qinfo[qidx].qid;
1512c9566231SKashyap Desai 
1513c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1514c9566231SKashyap Desai 
1515c9566231SKashyap Desai 	if (!reply_qid)	{
1516c9566231SKashyap Desai 		retval = -1;
1517c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n");
1518c9566231SKashyap Desai 		goto out;
1519c9566231SKashyap Desai 	}
1520c9566231SKashyap Desai 
1521c9566231SKashyap Desai 	memset(&delq_req, 0, sizeof(delq_req));
1522c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1523c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1524c9566231SKashyap Desai 		retval = -1;
1525c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n");
1526c9566231SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
1527c9566231SKashyap Desai 		goto out;
1528c9566231SKashyap Desai 	}
1529c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1530c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1531c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1532c9566231SKashyap Desai 	delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1533c9566231SKashyap Desai 	delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
1534c9566231SKashyap Desai 	delq_req.queue_id = cpu_to_le16(reply_qid);
1535c9566231SKashyap Desai 
1536c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1537c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req),
1538c9566231SKashyap Desai 	    1);
1539c9566231SKashyap Desai 	if (retval) {
1540c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n");
1541c9566231SKashyap Desai 		goto out_unlock;
1542c9566231SKashyap Desai 	}
1543c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1544c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1545c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1546a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "delete reply queue timed out\n");
1547a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1548c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
1549c9566231SKashyap Desai 		retval = -1;
1550c9566231SKashyap Desai 		goto out_unlock;
1551c9566231SKashyap Desai 	}
1552c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1553c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1554c9566231SKashyap Desai 		ioc_err(mrioc,
1555c9566231SKashyap Desai 		    "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1556c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1557c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1558c9566231SKashyap Desai 		retval = -1;
1559c9566231SKashyap Desai 		goto out_unlock;
1560c9566231SKashyap Desai 	}
1561c9566231SKashyap Desai 	mrioc->intr_info[midx].op_reply_q = NULL;
1562c9566231SKashyap Desai 
1563c9566231SKashyap Desai 	mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1564c9566231SKashyap Desai out_unlock:
1565c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1566c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1567c9566231SKashyap Desai out:
1568c9566231SKashyap Desai 
1569c9566231SKashyap Desai 	return retval;
1570c9566231SKashyap Desai }
1571c9566231SKashyap Desai 
1572c9566231SKashyap Desai /**
1573c9566231SKashyap Desai  * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool
1574c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1575c9566231SKashyap Desai  * @qidx: request queue index
1576c9566231SKashyap Desai  *
1577c9566231SKashyap Desai  * Allocate segmented memory pools for operational reply
1578c9566231SKashyap Desai  * queue.
1579c9566231SKashyap Desai  *
1580c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1581c9566231SKashyap Desai  */
1582c9566231SKashyap Desai static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1583c9566231SKashyap Desai {
1584c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1585c9566231SKashyap Desai 	int i, size;
1586c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1587c9566231SKashyap Desai 	struct segments *segments;
1588c9566231SKashyap Desai 
1589c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1590c9566231SKashyap Desai 		op_reply_q->segment_qd =
1591c9566231SKashyap Desai 		    MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz;
1592c9566231SKashyap Desai 
1593c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1594c9566231SKashyap Desai 
1595c9566231SKashyap Desai 		op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1596c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma,
1597c9566231SKashyap Desai 		    GFP_KERNEL);
1598c9566231SKashyap Desai 		if (!op_reply_q->q_segment_list)
1599c9566231SKashyap Desai 			return -ENOMEM;
1600c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_reply_q->q_segment_list;
1601c9566231SKashyap Desai 	} else {
1602c9566231SKashyap Desai 		op_reply_q->segment_qd = op_reply_q->num_replies;
1603c9566231SKashyap Desai 		size = op_reply_q->num_replies * mrioc->op_reply_desc_sz;
1604c9566231SKashyap Desai 	}
1605c9566231SKashyap Desai 
1606c9566231SKashyap Desai 	op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies,
1607c9566231SKashyap Desai 	    op_reply_q->segment_qd);
1608c9566231SKashyap Desai 
1609c9566231SKashyap Desai 	op_reply_q->q_segments = kcalloc(op_reply_q->num_segments,
1610c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1611c9566231SKashyap Desai 	if (!op_reply_q->q_segments)
1612c9566231SKashyap Desai 		return -ENOMEM;
1613c9566231SKashyap Desai 
1614c9566231SKashyap Desai 	segments = op_reply_q->q_segments;
1615c9566231SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++) {
1616c9566231SKashyap Desai 		segments[i].segment =
1617c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1618c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1619c9566231SKashyap Desai 		if (!segments[i].segment)
1620c9566231SKashyap Desai 			return -ENOMEM;
1621c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1622c9566231SKashyap Desai 			q_segment_list_entry[i] =
1623c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1624c9566231SKashyap Desai 	}
1625c9566231SKashyap Desai 
1626c9566231SKashyap Desai 	return 0;
1627c9566231SKashyap Desai }
1628c9566231SKashyap Desai 
1629c9566231SKashyap Desai /**
1630c9566231SKashyap Desai  * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool.
1631c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1632c9566231SKashyap Desai  * @qidx: request queue index
1633c9566231SKashyap Desai  *
1634c9566231SKashyap Desai  * Allocate segmented memory pools for operational request
1635c9566231SKashyap Desai  * queue.
1636c9566231SKashyap Desai  *
1637c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1638c9566231SKashyap Desai  */
1639c9566231SKashyap Desai static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1640c9566231SKashyap Desai {
1641c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
1642c9566231SKashyap Desai 	int i, size;
1643c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1644c9566231SKashyap Desai 	struct segments *segments;
1645c9566231SKashyap Desai 
1646c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1647c9566231SKashyap Desai 		op_req_q->segment_qd =
1648c9566231SKashyap Desai 		    MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz;
1649c9566231SKashyap Desai 
1650c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1651c9566231SKashyap Desai 
1652c9566231SKashyap Desai 		op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1653c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma,
1654c9566231SKashyap Desai 		    GFP_KERNEL);
1655c9566231SKashyap Desai 		if (!op_req_q->q_segment_list)
1656c9566231SKashyap Desai 			return -ENOMEM;
1657c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_req_q->q_segment_list;
1658c9566231SKashyap Desai 
1659c9566231SKashyap Desai 	} else {
1660c9566231SKashyap Desai 		op_req_q->segment_qd = op_req_q->num_requests;
1661c9566231SKashyap Desai 		size = op_req_q->num_requests * mrioc->facts.op_req_sz;
1662c9566231SKashyap Desai 	}
1663c9566231SKashyap Desai 
1664c9566231SKashyap Desai 	op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests,
1665c9566231SKashyap Desai 	    op_req_q->segment_qd);
1666c9566231SKashyap Desai 
1667c9566231SKashyap Desai 	op_req_q->q_segments = kcalloc(op_req_q->num_segments,
1668c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1669c9566231SKashyap Desai 	if (!op_req_q->q_segments)
1670c9566231SKashyap Desai 		return -ENOMEM;
1671c9566231SKashyap Desai 
1672c9566231SKashyap Desai 	segments = op_req_q->q_segments;
1673c9566231SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++) {
1674c9566231SKashyap Desai 		segments[i].segment =
1675c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1676c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1677c9566231SKashyap Desai 		if (!segments[i].segment)
1678c9566231SKashyap Desai 			return -ENOMEM;
1679c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1680c9566231SKashyap Desai 			q_segment_list_entry[i] =
1681c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1682c9566231SKashyap Desai 	}
1683c9566231SKashyap Desai 
1684c9566231SKashyap Desai 	return 0;
1685c9566231SKashyap Desai }
1686c9566231SKashyap Desai 
1687c9566231SKashyap Desai /**
1688c9566231SKashyap Desai  * mpi3mr_create_op_reply_q - create operational reply queue
1689c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1690c9566231SKashyap Desai  * @qidx: operational reply queue index
1691c9566231SKashyap Desai  *
1692c9566231SKashyap Desai  * Create operatinal reply queue by issuing MPI request
1693c9566231SKashyap Desai  * through admin queue.
1694c9566231SKashyap Desai  *
1695c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1696c9566231SKashyap Desai  */
1697c9566231SKashyap Desai static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1698c9566231SKashyap Desai {
1699c9566231SKashyap Desai 	struct mpi3_create_reply_queue_request create_req;
1700c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1701c9566231SKashyap Desai 	int retval = 0;
1702c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1703c9566231SKashyap Desai 
1704c9566231SKashyap Desai 	reply_qid = op_reply_q->qid;
1705c9566231SKashyap Desai 
1706c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1707c9566231SKashyap Desai 
1708c9566231SKashyap Desai 	if (reply_qid) {
1709c9566231SKashyap Desai 		retval = -1;
1710c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n",
1711c9566231SKashyap Desai 		    reply_qid);
1712c9566231SKashyap Desai 
1713c9566231SKashyap Desai 		return retval;
1714c9566231SKashyap Desai 	}
1715c9566231SKashyap Desai 
1716c9566231SKashyap Desai 	reply_qid = qidx + 1;
1717c9566231SKashyap Desai 	op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
1718c9566231SKashyap Desai 	op_reply_q->ci = 0;
1719c9566231SKashyap Desai 	op_reply_q->ephase = 1;
1720463429f8SKashyap Desai 	atomic_set(&op_reply_q->pend_ios, 0);
1721463429f8SKashyap Desai 	atomic_set(&op_reply_q->in_use, 0);
1722463429f8SKashyap Desai 	op_reply_q->enable_irq_poll = false;
1723c9566231SKashyap Desai 
1724c9566231SKashyap Desai 	if (!op_reply_q->q_segments) {
1725c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx);
1726c9566231SKashyap Desai 		if (retval) {
1727c9566231SKashyap Desai 			mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1728c9566231SKashyap Desai 			goto out;
1729c9566231SKashyap Desai 		}
1730c9566231SKashyap Desai 	}
1731c9566231SKashyap Desai 
1732c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1733c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1734c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1735c9566231SKashyap Desai 		retval = -1;
1736c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Init command is in use\n");
1737f9dc034dSYang Yingliang 		goto out_unlock;
1738c9566231SKashyap Desai 	}
1739c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1740c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1741c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1742c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1743c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
1744c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(reply_qid);
1745c9566231SKashyap Desai 	create_req.flags = MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
1746c9566231SKashyap Desai 	create_req.msix_index = cpu_to_le16(mrioc->intr_info[midx].msix_index);
1747c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1748c9566231SKashyap Desai 		create_req.flags |=
1749c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1750c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1751c9566231SKashyap Desai 		    op_reply_q->q_segment_list_dma);
1752c9566231SKashyap Desai 	} else
1753c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1754c9566231SKashyap Desai 		    op_reply_q->q_segments[0].segment_dma);
1755c9566231SKashyap Desai 
1756c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_reply_q->num_replies);
1757c9566231SKashyap Desai 
1758c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1759c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1760c9566231SKashyap Desai 	    sizeof(create_req), 1);
1761c9566231SKashyap Desai 	if (retval) {
1762c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Admin Post failed\n");
1763c9566231SKashyap Desai 		goto out_unlock;
1764c9566231SKashyap Desai 	}
1765c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1766c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1767c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1768a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create reply queue timed out\n");
1769a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1770c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
1771c9566231SKashyap Desai 		retval = -1;
1772c9566231SKashyap Desai 		goto out_unlock;
1773c9566231SKashyap Desai 	}
1774c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1775c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1776c9566231SKashyap Desai 		ioc_err(mrioc,
1777c9566231SKashyap Desai 		    "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1778c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1779c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1780c9566231SKashyap Desai 		retval = -1;
1781c9566231SKashyap Desai 		goto out_unlock;
1782c9566231SKashyap Desai 	}
1783c9566231SKashyap Desai 	op_reply_q->qid = reply_qid;
1784fe6db615SSreekanth Reddy 	if (midx < mrioc->intr_info_count)
1785c9566231SKashyap Desai 		mrioc->intr_info[midx].op_reply_q = op_reply_q;
1786c9566231SKashyap Desai 
1787c9566231SKashyap Desai out_unlock:
1788c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1789c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1790c9566231SKashyap Desai out:
1791c9566231SKashyap Desai 
1792c9566231SKashyap Desai 	return retval;
1793c9566231SKashyap Desai }
1794c9566231SKashyap Desai 
1795c9566231SKashyap Desai /**
1796c9566231SKashyap Desai  * mpi3mr_create_op_req_q - create operational request queue
1797c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1798c9566231SKashyap Desai  * @idx: operational request queue index
1799c9566231SKashyap Desai  * @reply_qid: Reply queue ID
1800c9566231SKashyap Desai  *
1801c9566231SKashyap Desai  * Create operatinal request queue by issuing MPI request
1802c9566231SKashyap Desai  * through admin queue.
1803c9566231SKashyap Desai  *
1804c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1805c9566231SKashyap Desai  */
1806c9566231SKashyap Desai static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx,
1807c9566231SKashyap Desai 	u16 reply_qid)
1808c9566231SKashyap Desai {
1809c9566231SKashyap Desai 	struct mpi3_create_request_queue_request create_req;
1810c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx;
1811c9566231SKashyap Desai 	int retval = 0;
1812c9566231SKashyap Desai 	u16 req_qid = 0;
1813c9566231SKashyap Desai 
1814c9566231SKashyap Desai 	req_qid = op_req_q->qid;
1815c9566231SKashyap Desai 
1816c9566231SKashyap Desai 	if (req_qid) {
1817c9566231SKashyap Desai 		retval = -1;
1818c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n",
1819c9566231SKashyap Desai 		    req_qid);
1820c9566231SKashyap Desai 
1821c9566231SKashyap Desai 		return retval;
1822c9566231SKashyap Desai 	}
1823c9566231SKashyap Desai 	req_qid = idx + 1;
1824c9566231SKashyap Desai 
1825c9566231SKashyap Desai 	op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD;
1826c9566231SKashyap Desai 	op_req_q->ci = 0;
1827c9566231SKashyap Desai 	op_req_q->pi = 0;
1828c9566231SKashyap Desai 	op_req_q->reply_qid = reply_qid;
1829c9566231SKashyap Desai 	spin_lock_init(&op_req_q->q_lock);
1830c9566231SKashyap Desai 
1831c9566231SKashyap Desai 	if (!op_req_q->q_segments) {
1832c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx);
1833c9566231SKashyap Desai 		if (retval) {
1834c9566231SKashyap Desai 			mpi3mr_free_op_req_q_segments(mrioc, idx);
1835c9566231SKashyap Desai 			goto out;
1836c9566231SKashyap Desai 		}
1837c9566231SKashyap Desai 	}
1838c9566231SKashyap Desai 
1839c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1840c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1841c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1842c9566231SKashyap Desai 		retval = -1;
1843c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Init command is in use\n");
1844f9dc034dSYang Yingliang 		goto out_unlock;
1845c9566231SKashyap Desai 	}
1846c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1847c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1848c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1849c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1850c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
1851c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(req_qid);
1852c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1853c9566231SKashyap Desai 		create_req.flags =
1854c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1855c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1856c9566231SKashyap Desai 		    op_req_q->q_segment_list_dma);
1857c9566231SKashyap Desai 	} else
1858c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1859c9566231SKashyap Desai 		    op_req_q->q_segments[0].segment_dma);
1860c9566231SKashyap Desai 	create_req.reply_queue_id = cpu_to_le16(reply_qid);
1861c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_req_q->num_requests);
1862c9566231SKashyap Desai 
1863c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1864c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1865c9566231SKashyap Desai 	    sizeof(create_req), 1);
1866c9566231SKashyap Desai 	if (retval) {
1867c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Admin Post failed\n");
1868c9566231SKashyap Desai 		goto out_unlock;
1869c9566231SKashyap Desai 	}
1870c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1871c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1872c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1873a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create request queue timed out\n");
1874a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1875a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
1876c9566231SKashyap Desai 		retval = -1;
1877c9566231SKashyap Desai 		goto out_unlock;
1878c9566231SKashyap Desai 	}
1879c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1880c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1881c9566231SKashyap Desai 		ioc_err(mrioc,
1882c9566231SKashyap Desai 		    "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1883c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1884c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1885c9566231SKashyap Desai 		retval = -1;
1886c9566231SKashyap Desai 		goto out_unlock;
1887c9566231SKashyap Desai 	}
1888c9566231SKashyap Desai 	op_req_q->qid = req_qid;
1889c9566231SKashyap Desai 
1890c9566231SKashyap Desai out_unlock:
1891c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1892c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1893c9566231SKashyap Desai out:
1894c9566231SKashyap Desai 
1895c9566231SKashyap Desai 	return retval;
1896c9566231SKashyap Desai }
1897c9566231SKashyap Desai 
1898c9566231SKashyap Desai /**
1899c9566231SKashyap Desai  * mpi3mr_create_op_queues - create operational queue pairs
1900c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1901c9566231SKashyap Desai  *
1902c9566231SKashyap Desai  * Allocate memory for operational queue meta data and call
1903c9566231SKashyap Desai  * create request and reply queue functions.
1904c9566231SKashyap Desai  *
1905c9566231SKashyap Desai  * Return: 0 on success, non-zero on failures.
1906c9566231SKashyap Desai  */
1907c9566231SKashyap Desai static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc)
1908c9566231SKashyap Desai {
1909c9566231SKashyap Desai 	int retval = 0;
1910c9566231SKashyap Desai 	u16 num_queues = 0, i = 0, msix_count_op_q = 1;
1911c9566231SKashyap Desai 
1912c9566231SKashyap Desai 	num_queues = min_t(int, mrioc->facts.max_op_reply_q,
1913c9566231SKashyap Desai 	    mrioc->facts.max_op_req_q);
1914c9566231SKashyap Desai 
1915c9566231SKashyap Desai 	msix_count_op_q =
1916c9566231SKashyap Desai 	    mrioc->intr_info_count - mrioc->op_reply_q_offset;
1917c9566231SKashyap Desai 	if (!mrioc->num_queues)
1918c9566231SKashyap Desai 		mrioc->num_queues = min_t(int, num_queues, msix_count_op_q);
1919*c5758fc7SSreekanth Reddy 	/*
1920*c5758fc7SSreekanth Reddy 	 * During reset set the num_queues to the number of queues
1921*c5758fc7SSreekanth Reddy 	 * that was set before the reset.
1922*c5758fc7SSreekanth Reddy 	 */
1923*c5758fc7SSreekanth Reddy 	num_queues = mrioc->num_op_reply_q ?
1924*c5758fc7SSreekanth Reddy 	    mrioc->num_op_reply_q : mrioc->num_queues;
1925*c5758fc7SSreekanth Reddy 	ioc_info(mrioc, "trying to create %d operational queue pairs\n",
1926c9566231SKashyap Desai 	    num_queues);
1927c9566231SKashyap Desai 
1928c9566231SKashyap Desai 	if (!mrioc->req_qinfo) {
1929c9566231SKashyap Desai 		mrioc->req_qinfo = kcalloc(num_queues,
1930c9566231SKashyap Desai 		    sizeof(struct op_req_qinfo), GFP_KERNEL);
1931c9566231SKashyap Desai 		if (!mrioc->req_qinfo) {
1932c9566231SKashyap Desai 			retval = -1;
1933c9566231SKashyap Desai 			goto out_failed;
1934c9566231SKashyap Desai 		}
1935c9566231SKashyap Desai 
1936c9566231SKashyap Desai 		mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) *
1937c9566231SKashyap Desai 		    num_queues, GFP_KERNEL);
1938c9566231SKashyap Desai 		if (!mrioc->op_reply_qinfo) {
1939c9566231SKashyap Desai 			retval = -1;
1940c9566231SKashyap Desai 			goto out_failed;
1941c9566231SKashyap Desai 		}
1942c9566231SKashyap Desai 	}
1943c9566231SKashyap Desai 
1944c9566231SKashyap Desai 	if (mrioc->enable_segqueue)
1945c9566231SKashyap Desai 		ioc_info(mrioc,
1946c9566231SKashyap Desai 		    "allocating operational queues through segmented queues\n");
1947c9566231SKashyap Desai 
1948c9566231SKashyap Desai 	for (i = 0; i < num_queues; i++) {
1949c9566231SKashyap Desai 		if (mpi3mr_create_op_reply_q(mrioc, i)) {
1950c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP RepQ %d\n", i);
1951c9566231SKashyap Desai 			break;
1952c9566231SKashyap Desai 		}
1953c9566231SKashyap Desai 		if (mpi3mr_create_op_req_q(mrioc, i,
1954c9566231SKashyap Desai 		    mrioc->op_reply_qinfo[i].qid)) {
1955c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i);
1956c9566231SKashyap Desai 			mpi3mr_delete_op_reply_q(mrioc, i);
1957c9566231SKashyap Desai 			break;
1958c9566231SKashyap Desai 		}
1959c9566231SKashyap Desai 	}
1960c9566231SKashyap Desai 
1961c9566231SKashyap Desai 	if (i == 0) {
1962c9566231SKashyap Desai 		/* Not even one queue is created successfully*/
1963c9566231SKashyap Desai 		retval = -1;
1964c9566231SKashyap Desai 		goto out_failed;
1965c9566231SKashyap Desai 	}
1966c9566231SKashyap Desai 	mrioc->num_op_reply_q = mrioc->num_op_req_q = i;
1967c9566231SKashyap Desai 	ioc_info(mrioc, "Successfully created %d Operational Q pairs\n",
1968c9566231SKashyap Desai 	    mrioc->num_op_reply_q);
1969c9566231SKashyap Desai 
1970c9566231SKashyap Desai 	return retval;
1971c9566231SKashyap Desai out_failed:
1972c9566231SKashyap Desai 	kfree(mrioc->req_qinfo);
1973c9566231SKashyap Desai 	mrioc->req_qinfo = NULL;
1974c9566231SKashyap Desai 
1975c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
1976c9566231SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
1977c9566231SKashyap Desai 
1978c9566231SKashyap Desai 	return retval;
1979c9566231SKashyap Desai }
1980c9566231SKashyap Desai 
1981c9566231SKashyap Desai /**
1982023ab2a9SKashyap Desai  * mpi3mr_op_request_post - Post request to operational queue
1983023ab2a9SKashyap Desai  * @mrioc: Adapter reference
1984023ab2a9SKashyap Desai  * @op_req_q: Operational request queue info
1985023ab2a9SKashyap Desai  * @req: MPI3 request
1986023ab2a9SKashyap Desai  *
1987023ab2a9SKashyap Desai  * Post the MPI3 request into operational request queue and
1988023ab2a9SKashyap Desai  * inform the controller, if the queue is full return
1989023ab2a9SKashyap Desai  * appropriate error.
1990023ab2a9SKashyap Desai  *
1991023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failure.
1992023ab2a9SKashyap Desai  */
1993023ab2a9SKashyap Desai int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc,
1994023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q, u8 *req)
1995023ab2a9SKashyap Desai {
1996023ab2a9SKashyap Desai 	u16 pi = 0, max_entries, reply_qidx = 0, midx;
1997023ab2a9SKashyap Desai 	int retval = 0;
1998023ab2a9SKashyap Desai 	unsigned long flags;
1999023ab2a9SKashyap Desai 	u8 *req_entry;
2000023ab2a9SKashyap Desai 	void *segment_base_addr;
2001023ab2a9SKashyap Desai 	u16 req_sz = mrioc->facts.op_req_sz;
2002023ab2a9SKashyap Desai 	struct segments *segments = op_req_q->q_segments;
2003023ab2a9SKashyap Desai 
2004023ab2a9SKashyap Desai 	reply_qidx = op_req_q->reply_qid - 1;
2005023ab2a9SKashyap Desai 
2006023ab2a9SKashyap Desai 	if (mrioc->unrecoverable)
2007023ab2a9SKashyap Desai 		return -EFAULT;
2008023ab2a9SKashyap Desai 
2009023ab2a9SKashyap Desai 	spin_lock_irqsave(&op_req_q->q_lock, flags);
2010023ab2a9SKashyap Desai 	pi = op_req_q->pi;
2011023ab2a9SKashyap Desai 	max_entries = op_req_q->num_requests;
2012023ab2a9SKashyap Desai 
2013023ab2a9SKashyap Desai 	if (mpi3mr_check_req_qfull(op_req_q)) {
2014023ab2a9SKashyap Desai 		midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(
2015023ab2a9SKashyap Desai 		    reply_qidx, mrioc->op_reply_q_offset);
2016023ab2a9SKashyap Desai 		mpi3mr_process_op_reply_q(mrioc, &mrioc->intr_info[midx]);
2017023ab2a9SKashyap Desai 
2018023ab2a9SKashyap Desai 		if (mpi3mr_check_req_qfull(op_req_q)) {
2019023ab2a9SKashyap Desai 			retval = -EAGAIN;
2020023ab2a9SKashyap Desai 			goto out;
2021023ab2a9SKashyap Desai 		}
2022023ab2a9SKashyap Desai 	}
2023023ab2a9SKashyap Desai 
2024023ab2a9SKashyap Desai 	if (mrioc->reset_in_progress) {
2025023ab2a9SKashyap Desai 		ioc_err(mrioc, "OpReqQ submit reset in progress\n");
2026023ab2a9SKashyap Desai 		retval = -EAGAIN;
2027023ab2a9SKashyap Desai 		goto out;
2028023ab2a9SKashyap Desai 	}
2029023ab2a9SKashyap Desai 
2030023ab2a9SKashyap Desai 	segment_base_addr = segments[pi / op_req_q->segment_qd].segment;
2031023ab2a9SKashyap Desai 	req_entry = (u8 *)segment_base_addr +
2032023ab2a9SKashyap Desai 	    ((pi % op_req_q->segment_qd) * req_sz);
2033023ab2a9SKashyap Desai 
2034023ab2a9SKashyap Desai 	memset(req_entry, 0, req_sz);
2035023ab2a9SKashyap Desai 	memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ);
2036023ab2a9SKashyap Desai 
2037023ab2a9SKashyap Desai 	if (++pi == max_entries)
2038023ab2a9SKashyap Desai 		pi = 0;
2039023ab2a9SKashyap Desai 	op_req_q->pi = pi;
2040023ab2a9SKashyap Desai 
2041463429f8SKashyap Desai 	if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios)
2042463429f8SKashyap Desai 	    > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT)
2043463429f8SKashyap Desai 		mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true;
2044463429f8SKashyap Desai 
2045023ab2a9SKashyap Desai 	writel(op_req_q->pi,
2046023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index);
2047023ab2a9SKashyap Desai 
2048023ab2a9SKashyap Desai out:
2049023ab2a9SKashyap Desai 	spin_unlock_irqrestore(&op_req_q->q_lock, flags);
2050023ab2a9SKashyap Desai 	return retval;
2051023ab2a9SKashyap Desai }
2052023ab2a9SKashyap Desai 
2053023ab2a9SKashyap Desai /**
2054a6856cc4SSreekanth Reddy  * mpi3mr_check_rh_fault_ioc - check reset history and fault
2055a6856cc4SSreekanth Reddy  * controller
2056a6856cc4SSreekanth Reddy  * @mrioc: Adapter instance reference
2057a6856cc4SSreekanth Reddy  * @reason_code, reason code for the fault.
2058a6856cc4SSreekanth Reddy  *
2059a6856cc4SSreekanth Reddy  * This routine will save snapdump and fault the controller with
2060a6856cc4SSreekanth Reddy  * the given reason code if it is not already in the fault or
2061a6856cc4SSreekanth Reddy  * not asynchronosuly reset. This will be used to handle
2062a6856cc4SSreekanth Reddy  * initilaization time faults/resets/timeout as in those cases
2063a6856cc4SSreekanth Reddy  * immediate soft reset invocation is not required.
2064a6856cc4SSreekanth Reddy  *
2065a6856cc4SSreekanth Reddy  * Return:  None.
2066a6856cc4SSreekanth Reddy  */
2067a6856cc4SSreekanth Reddy void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code)
2068a6856cc4SSreekanth Reddy {
2069a6856cc4SSreekanth Reddy 	u32 ioc_status, host_diagnostic, timeout;
2070a6856cc4SSreekanth Reddy 
2071a6856cc4SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
2072a6856cc4SSreekanth Reddy 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
2073a6856cc4SSreekanth Reddy 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
2074a6856cc4SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
2075a6856cc4SSreekanth Reddy 		return;
2076a6856cc4SSreekanth Reddy 	}
2077a6856cc4SSreekanth Reddy 	mpi3mr_set_diagsave(mrioc);
2078a6856cc4SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
2079a6856cc4SSreekanth Reddy 	    reason_code);
2080a6856cc4SSreekanth Reddy 	timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
2081a6856cc4SSreekanth Reddy 	do {
2082a6856cc4SSreekanth Reddy 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2083a6856cc4SSreekanth Reddy 		if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
2084a6856cc4SSreekanth Reddy 			break;
2085a6856cc4SSreekanth Reddy 		msleep(100);
2086a6856cc4SSreekanth Reddy 	} while (--timeout);
2087a6856cc4SSreekanth Reddy }
2088a6856cc4SSreekanth Reddy 
2089a6856cc4SSreekanth Reddy /**
209054dfcffbSKashyap Desai  * mpi3mr_sync_timestamp - Issue time stamp sync request
209154dfcffbSKashyap Desai  * @mrioc: Adapter reference
209254dfcffbSKashyap Desai  *
209354dfcffbSKashyap Desai  * Issue IO unit control MPI request to synchornize firmware
209454dfcffbSKashyap Desai  * timestamp with host time.
209554dfcffbSKashyap Desai  *
209654dfcffbSKashyap Desai  * Return: 0 on success, non-zero on failure.
209754dfcffbSKashyap Desai  */
209854dfcffbSKashyap Desai static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc)
209954dfcffbSKashyap Desai {
210054dfcffbSKashyap Desai 	ktime_t current_time;
210154dfcffbSKashyap Desai 	struct mpi3_iounit_control_request iou_ctrl;
210254dfcffbSKashyap Desai 	int retval = 0;
210354dfcffbSKashyap Desai 
210454dfcffbSKashyap Desai 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
210554dfcffbSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
210654dfcffbSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
210754dfcffbSKashyap Desai 		retval = -1;
210854dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n");
210954dfcffbSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
211054dfcffbSKashyap Desai 		goto out;
211154dfcffbSKashyap Desai 	}
211254dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
211354dfcffbSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
211454dfcffbSKashyap Desai 	mrioc->init_cmds.callback = NULL;
211554dfcffbSKashyap Desai 	iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
211654dfcffbSKashyap Desai 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
211754dfcffbSKashyap Desai 	iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP;
211854dfcffbSKashyap Desai 	current_time = ktime_get_real();
211954dfcffbSKashyap Desai 	iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time));
212054dfcffbSKashyap Desai 
212154dfcffbSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
212254dfcffbSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl,
212354dfcffbSKashyap Desai 	    sizeof(iou_ctrl), 0);
212454dfcffbSKashyap Desai 	if (retval) {
212554dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n");
212654dfcffbSKashyap Desai 		goto out_unlock;
212754dfcffbSKashyap Desai 	}
212854dfcffbSKashyap Desai 
212954dfcffbSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
213054dfcffbSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
213154dfcffbSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
213254dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n");
213354dfcffbSKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
2134fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
213554dfcffbSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
213654dfcffbSKashyap Desai 			    MPI3MR_RESET_FROM_TSU_TIMEOUT, 1);
213754dfcffbSKashyap Desai 		retval = -1;
213854dfcffbSKashyap Desai 		goto out_unlock;
213954dfcffbSKashyap Desai 	}
214054dfcffbSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
214154dfcffbSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
214254dfcffbSKashyap Desai 		ioc_err(mrioc,
214354dfcffbSKashyap Desai 		    "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
214454dfcffbSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
214554dfcffbSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
214654dfcffbSKashyap Desai 		retval = -1;
214754dfcffbSKashyap Desai 		goto out_unlock;
214854dfcffbSKashyap Desai 	}
214954dfcffbSKashyap Desai 
215054dfcffbSKashyap Desai out_unlock:
215154dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
215254dfcffbSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
215354dfcffbSKashyap Desai 
215454dfcffbSKashyap Desai out:
215554dfcffbSKashyap Desai 	return retval;
215654dfcffbSKashyap Desai }
215754dfcffbSKashyap Desai 
215854dfcffbSKashyap Desai /**
21592ac794baSSreekanth Reddy  * mpi3mr_print_pkg_ver - display controller fw package version
21602ac794baSSreekanth Reddy  * @mrioc: Adapter reference
21612ac794baSSreekanth Reddy  *
21622ac794baSSreekanth Reddy  * Retrieve firmware package version from the component image
21632ac794baSSreekanth Reddy  * header of the controller flash and display it.
21642ac794baSSreekanth Reddy  *
21652ac794baSSreekanth Reddy  * Return: 0 on success and non-zero on failure.
21662ac794baSSreekanth Reddy  */
21672ac794baSSreekanth Reddy static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc)
21682ac794baSSreekanth Reddy {
21692ac794baSSreekanth Reddy 	struct mpi3_ci_upload_request ci_upload;
21702ac794baSSreekanth Reddy 	int retval = -1;
21712ac794baSSreekanth Reddy 	void *data = NULL;
21722ac794baSSreekanth Reddy 	dma_addr_t data_dma;
21732ac794baSSreekanth Reddy 	struct mpi3_ci_manifest_mpi *manifest;
21742ac794baSSreekanth Reddy 	u32 data_len = sizeof(struct mpi3_ci_manifest_mpi);
21752ac794baSSreekanth Reddy 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
21762ac794baSSreekanth Reddy 
21772ac794baSSreekanth Reddy 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
21782ac794baSSreekanth Reddy 	    GFP_KERNEL);
21792ac794baSSreekanth Reddy 	if (!data)
21802ac794baSSreekanth Reddy 		return -ENOMEM;
21812ac794baSSreekanth Reddy 
21822ac794baSSreekanth Reddy 	memset(&ci_upload, 0, sizeof(ci_upload));
21832ac794baSSreekanth Reddy 	mutex_lock(&mrioc->init_cmds.mutex);
21842ac794baSSreekanth Reddy 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
21852ac794baSSreekanth Reddy 		ioc_err(mrioc, "sending get package version failed due to command in use\n");
21862ac794baSSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
21872ac794baSSreekanth Reddy 		goto out;
21882ac794baSSreekanth Reddy 	}
21892ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
21902ac794baSSreekanth Reddy 	mrioc->init_cmds.is_waiting = 1;
21912ac794baSSreekanth Reddy 	mrioc->init_cmds.callback = NULL;
21922ac794baSSreekanth Reddy 	ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
21932ac794baSSreekanth Reddy 	ci_upload.function = MPI3_FUNCTION_CI_UPLOAD;
21942ac794baSSreekanth Reddy 	ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
21952ac794baSSreekanth Reddy 	ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST);
21962ac794baSSreekanth Reddy 	ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE);
21972ac794baSSreekanth Reddy 	ci_upload.segment_size = cpu_to_le32(data_len);
21982ac794baSSreekanth Reddy 
21992ac794baSSreekanth Reddy 	mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len,
22002ac794baSSreekanth Reddy 	    data_dma);
22012ac794baSSreekanth Reddy 	init_completion(&mrioc->init_cmds.done);
22022ac794baSSreekanth Reddy 	retval = mpi3mr_admin_request_post(mrioc, &ci_upload,
22032ac794baSSreekanth Reddy 	    sizeof(ci_upload), 1);
22042ac794baSSreekanth Reddy 	if (retval) {
22052ac794baSSreekanth Reddy 		ioc_err(mrioc, "posting get package version failed\n");
22062ac794baSSreekanth Reddy 		goto out_unlock;
22072ac794baSSreekanth Reddy 	}
22082ac794baSSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done,
22092ac794baSSreekanth Reddy 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
22102ac794baSSreekanth Reddy 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
22112ac794baSSreekanth Reddy 		ioc_err(mrioc, "get package version timed out\n");
2212a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2213a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
22142ac794baSSreekanth Reddy 		retval = -1;
22152ac794baSSreekanth Reddy 		goto out_unlock;
22162ac794baSSreekanth Reddy 	}
22172ac794baSSreekanth Reddy 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
22182ac794baSSreekanth Reddy 	    == MPI3_IOCSTATUS_SUCCESS) {
22192ac794baSSreekanth Reddy 		manifest = (struct mpi3_ci_manifest_mpi *) data;
22202ac794baSSreekanth Reddy 		if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) {
22212ac794baSSreekanth Reddy 			ioc_info(mrioc,
22222ac794baSSreekanth Reddy 			    "firmware package version(%d.%d.%d.%d.%05d-%05d)\n",
22232ac794baSSreekanth Reddy 			    manifest->package_version.gen_major,
22242ac794baSSreekanth Reddy 			    manifest->package_version.gen_minor,
22252ac794baSSreekanth Reddy 			    manifest->package_version.phase_major,
22262ac794baSSreekanth Reddy 			    manifest->package_version.phase_minor,
22272ac794baSSreekanth Reddy 			    manifest->package_version.customer_id,
22282ac794baSSreekanth Reddy 			    manifest->package_version.build_num);
22292ac794baSSreekanth Reddy 		}
22302ac794baSSreekanth Reddy 	}
22312ac794baSSreekanth Reddy 	retval = 0;
22322ac794baSSreekanth Reddy out_unlock:
22332ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
22342ac794baSSreekanth Reddy 	mutex_unlock(&mrioc->init_cmds.mutex);
22352ac794baSSreekanth Reddy 
22362ac794baSSreekanth Reddy out:
22372ac794baSSreekanth Reddy 	if (data)
22382ac794baSSreekanth Reddy 		dma_free_coherent(&mrioc->pdev->dev, data_len, data,
22392ac794baSSreekanth Reddy 		    data_dma);
22402ac794baSSreekanth Reddy 	return retval;
22412ac794baSSreekanth Reddy }
22422ac794baSSreekanth Reddy 
22432ac794baSSreekanth Reddy /**
2244672ae26cSKashyap Desai  * mpi3mr_watchdog_work - watchdog thread to monitor faults
2245672ae26cSKashyap Desai  * @work: work struct
2246672ae26cSKashyap Desai  *
2247672ae26cSKashyap Desai  * Watch dog work periodically executed (1 second interval) to
2248672ae26cSKashyap Desai  * monitor firmware fault and to issue periodic timer sync to
2249672ae26cSKashyap Desai  * the firmware.
2250672ae26cSKashyap Desai  *
2251672ae26cSKashyap Desai  * Return: Nothing.
2252672ae26cSKashyap Desai  */
2253672ae26cSKashyap Desai static void mpi3mr_watchdog_work(struct work_struct *work)
2254672ae26cSKashyap Desai {
2255672ae26cSKashyap Desai 	struct mpi3mr_ioc *mrioc =
2256672ae26cSKashyap Desai 	    container_of(work, struct mpi3mr_ioc, watchdog_work.work);
2257672ae26cSKashyap Desai 	unsigned long flags;
2258672ae26cSKashyap Desai 	enum mpi3mr_iocstate ioc_state;
2259672ae26cSKashyap Desai 	u32 fault, host_diagnostic;
2260672ae26cSKashyap Desai 
2261b64845a7SSreekanth Reddy 	if (mrioc->reset_in_progress || mrioc->unrecoverable)
2262b64845a7SSreekanth Reddy 		return;
2263b64845a7SSreekanth Reddy 
226454dfcffbSKashyap Desai 	if (mrioc->ts_update_counter++ >= MPI3MR_TSUPDATE_INTERVAL) {
226554dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
226654dfcffbSKashyap Desai 		mpi3mr_sync_timestamp(mrioc);
226754dfcffbSKashyap Desai 	}
226854dfcffbSKashyap Desai 
2269672ae26cSKashyap Desai 	/*Check for fault state every one second and issue Soft reset*/
2270672ae26cSKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
2271672ae26cSKashyap Desai 	if (ioc_state == MRIOC_STATE_FAULT) {
2272672ae26cSKashyap Desai 		fault = readl(&mrioc->sysif_regs->fault) &
2273672ae26cSKashyap Desai 		    MPI3_SYSIF_FAULT_CODE_MASK;
2274672ae26cSKashyap Desai 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2275672ae26cSKashyap Desai 		if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
2276672ae26cSKashyap Desai 			if (!mrioc->diagsave_timeout) {
2277672ae26cSKashyap Desai 				mpi3mr_print_fault_info(mrioc);
2278672ae26cSKashyap Desai 				ioc_warn(mrioc, "Diag save in progress\n");
2279672ae26cSKashyap Desai 			}
2280672ae26cSKashyap Desai 			if ((mrioc->diagsave_timeout++) <=
2281672ae26cSKashyap Desai 			    MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
2282672ae26cSKashyap Desai 				goto schedule_work;
2283672ae26cSKashyap Desai 		} else
2284672ae26cSKashyap Desai 			mpi3mr_print_fault_info(mrioc);
2285672ae26cSKashyap Desai 		mrioc->diagsave_timeout = 0;
2286672ae26cSKashyap Desai 
2287ec5ebd2cSSreekanth Reddy 		if (fault == MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED) {
2288672ae26cSKashyap Desai 			ioc_info(mrioc,
2289672ae26cSKashyap Desai 			    "Factory Reset fault occurred marking controller as unrecoverable"
2290672ae26cSKashyap Desai 			    );
2291672ae26cSKashyap Desai 			mrioc->unrecoverable = 1;
2292672ae26cSKashyap Desai 			goto out;
2293672ae26cSKashyap Desai 		}
2294672ae26cSKashyap Desai 
2295672ae26cSKashyap Desai 		if ((fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) ||
2296672ae26cSKashyap Desai 		    (fault == MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS) ||
2297672ae26cSKashyap Desai 		    (mrioc->reset_in_progress))
2298672ae26cSKashyap Desai 			goto out;
2299672ae26cSKashyap Desai 		if (fault == MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET)
2300672ae26cSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
2301672ae26cSKashyap Desai 			    MPI3MR_RESET_FROM_CIACTIV_FAULT, 0);
2302672ae26cSKashyap Desai 		else
2303672ae26cSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
2304672ae26cSKashyap Desai 			    MPI3MR_RESET_FROM_FAULT_WATCH, 0);
2305672ae26cSKashyap Desai 	}
2306672ae26cSKashyap Desai 
2307672ae26cSKashyap Desai schedule_work:
2308672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2309672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2310672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2311672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2312672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2313672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2314672ae26cSKashyap Desai out:
2315672ae26cSKashyap Desai 	return;
2316672ae26cSKashyap Desai }
2317672ae26cSKashyap Desai 
2318672ae26cSKashyap Desai /**
2319672ae26cSKashyap Desai  * mpi3mr_start_watchdog - Start watchdog
2320672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2321672ae26cSKashyap Desai  *
2322672ae26cSKashyap Desai  * Create and start the watchdog thread to monitor controller
2323672ae26cSKashyap Desai  * faults.
2324672ae26cSKashyap Desai  *
2325672ae26cSKashyap Desai  * Return: Nothing.
2326672ae26cSKashyap Desai  */
2327672ae26cSKashyap Desai void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc)
2328672ae26cSKashyap Desai {
2329672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2330672ae26cSKashyap Desai 		return;
2331672ae26cSKashyap Desai 
2332672ae26cSKashyap Desai 	INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work);
2333672ae26cSKashyap Desai 	snprintf(mrioc->watchdog_work_q_name,
2334672ae26cSKashyap Desai 	    sizeof(mrioc->watchdog_work_q_name), "watchdog_%s%d", mrioc->name,
2335672ae26cSKashyap Desai 	    mrioc->id);
2336672ae26cSKashyap Desai 	mrioc->watchdog_work_q =
2337672ae26cSKashyap Desai 	    create_singlethread_workqueue(mrioc->watchdog_work_q_name);
2338672ae26cSKashyap Desai 	if (!mrioc->watchdog_work_q) {
2339672ae26cSKashyap Desai 		ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__);
2340672ae26cSKashyap Desai 		return;
2341672ae26cSKashyap Desai 	}
2342672ae26cSKashyap Desai 
2343672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2344672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2345672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2346672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2347672ae26cSKashyap Desai }
2348672ae26cSKashyap Desai 
2349672ae26cSKashyap Desai /**
2350672ae26cSKashyap Desai  * mpi3mr_stop_watchdog - Stop watchdog
2351672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2352672ae26cSKashyap Desai  *
2353672ae26cSKashyap Desai  * Stop the watchdog thread created to monitor controller
2354672ae26cSKashyap Desai  * faults.
2355672ae26cSKashyap Desai  *
2356672ae26cSKashyap Desai  * Return: Nothing.
2357672ae26cSKashyap Desai  */
2358672ae26cSKashyap Desai void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc)
2359672ae26cSKashyap Desai {
2360672ae26cSKashyap Desai 	unsigned long flags;
2361672ae26cSKashyap Desai 	struct workqueue_struct *wq;
2362672ae26cSKashyap Desai 
2363672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2364672ae26cSKashyap Desai 	wq = mrioc->watchdog_work_q;
2365672ae26cSKashyap Desai 	mrioc->watchdog_work_q = NULL;
2366672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2367672ae26cSKashyap Desai 	if (wq) {
2368672ae26cSKashyap Desai 		if (!cancel_delayed_work_sync(&mrioc->watchdog_work))
2369672ae26cSKashyap Desai 			flush_workqueue(wq);
2370672ae26cSKashyap Desai 		destroy_workqueue(wq);
2371672ae26cSKashyap Desai 	}
2372672ae26cSKashyap Desai }
2373672ae26cSKashyap Desai 
2374672ae26cSKashyap Desai /**
2375824a1566SKashyap Desai  * mpi3mr_setup_admin_qpair - Setup admin queue pair
2376824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2377824a1566SKashyap Desai  *
2378824a1566SKashyap Desai  * Allocate memory for admin queue pair if required and register
2379824a1566SKashyap Desai  * the admin queue with the controller.
2380824a1566SKashyap Desai  *
2381824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2382824a1566SKashyap Desai  */
2383824a1566SKashyap Desai static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
2384824a1566SKashyap Desai {
2385824a1566SKashyap Desai 	int retval = 0;
2386824a1566SKashyap Desai 	u32 num_admin_entries = 0;
2387824a1566SKashyap Desai 
2388824a1566SKashyap Desai 	mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
2389824a1566SKashyap Desai 	mrioc->num_admin_req = mrioc->admin_req_q_sz /
2390824a1566SKashyap Desai 	    MPI3MR_ADMIN_REQ_FRAME_SZ;
2391824a1566SKashyap Desai 	mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
2392824a1566SKashyap Desai 	mrioc->admin_req_base = NULL;
2393824a1566SKashyap Desai 
2394824a1566SKashyap Desai 	mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
2395824a1566SKashyap Desai 	mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
2396824a1566SKashyap Desai 	    MPI3MR_ADMIN_REPLY_FRAME_SZ;
2397824a1566SKashyap Desai 	mrioc->admin_reply_ci = 0;
2398824a1566SKashyap Desai 	mrioc->admin_reply_ephase = 1;
2399824a1566SKashyap Desai 	mrioc->admin_reply_base = NULL;
2400824a1566SKashyap Desai 
2401824a1566SKashyap Desai 	if (!mrioc->admin_req_base) {
2402824a1566SKashyap Desai 		mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
2403824a1566SKashyap Desai 		    mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
2404824a1566SKashyap Desai 
2405824a1566SKashyap Desai 		if (!mrioc->admin_req_base) {
2406824a1566SKashyap Desai 			retval = -1;
2407824a1566SKashyap Desai 			goto out_failed;
2408824a1566SKashyap Desai 		}
2409824a1566SKashyap Desai 
2410824a1566SKashyap Desai 		mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
2411824a1566SKashyap Desai 		    mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
2412824a1566SKashyap Desai 		    GFP_KERNEL);
2413824a1566SKashyap Desai 
2414824a1566SKashyap Desai 		if (!mrioc->admin_reply_base) {
2415824a1566SKashyap Desai 			retval = -1;
2416824a1566SKashyap Desai 			goto out_failed;
2417824a1566SKashyap Desai 		}
2418824a1566SKashyap Desai 	}
2419824a1566SKashyap Desai 
2420824a1566SKashyap Desai 	num_admin_entries = (mrioc->num_admin_replies << 16) |
2421824a1566SKashyap Desai 	    (mrioc->num_admin_req);
2422824a1566SKashyap Desai 	writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
2423824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_req_dma,
2424824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_request_queue_address);
2425824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_reply_dma,
2426824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_reply_queue_address);
2427824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
2428824a1566SKashyap Desai 	writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
2429824a1566SKashyap Desai 	return retval;
2430824a1566SKashyap Desai 
2431824a1566SKashyap Desai out_failed:
2432824a1566SKashyap Desai 
2433824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
2434824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
2435824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
2436824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
2437824a1566SKashyap Desai 	}
2438824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
2439824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
2440824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
2441824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
2442824a1566SKashyap Desai 	}
2443824a1566SKashyap Desai 	return retval;
2444824a1566SKashyap Desai }
2445824a1566SKashyap Desai 
2446824a1566SKashyap Desai /**
2447824a1566SKashyap Desai  * mpi3mr_issue_iocfacts - Send IOC Facts
2448824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2449824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2450824a1566SKashyap Desai  *
2451824a1566SKashyap Desai  * Issue IOC Facts MPI request through admin queue and wait for
2452824a1566SKashyap Desai  * the completion of it or time out.
2453824a1566SKashyap Desai  *
2454824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2455824a1566SKashyap Desai  */
2456824a1566SKashyap Desai static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
2457824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2458824a1566SKashyap Desai {
2459824a1566SKashyap Desai 	struct mpi3_ioc_facts_request iocfacts_req;
2460824a1566SKashyap Desai 	void *data = NULL;
2461824a1566SKashyap Desai 	dma_addr_t data_dma;
2462824a1566SKashyap Desai 	u32 data_len = sizeof(*facts_data);
2463824a1566SKashyap Desai 	int retval = 0;
2464824a1566SKashyap Desai 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
2465824a1566SKashyap Desai 
2466824a1566SKashyap Desai 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2467824a1566SKashyap Desai 	    GFP_KERNEL);
2468824a1566SKashyap Desai 
2469824a1566SKashyap Desai 	if (!data) {
2470824a1566SKashyap Desai 		retval = -1;
2471824a1566SKashyap Desai 		goto out;
2472824a1566SKashyap Desai 	}
2473824a1566SKashyap Desai 
2474824a1566SKashyap Desai 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
2475824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
2476824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2477824a1566SKashyap Desai 		retval = -1;
2478824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
2479824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
2480824a1566SKashyap Desai 		goto out;
2481824a1566SKashyap Desai 	}
2482824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2483824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
2484824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
2485824a1566SKashyap Desai 	iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2486824a1566SKashyap Desai 	iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
2487824a1566SKashyap Desai 
2488824a1566SKashyap Desai 	mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
2489824a1566SKashyap Desai 	    data_dma);
2490824a1566SKashyap Desai 
2491824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
2492824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
2493824a1566SKashyap Desai 	    sizeof(iocfacts_req), 1);
2494824a1566SKashyap Desai 	if (retval) {
2495824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
2496824a1566SKashyap Desai 		goto out_unlock;
2497824a1566SKashyap Desai 	}
2498824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2499824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2500824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2501a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_facts timed out\n");
2502a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2503824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
2504824a1566SKashyap Desai 		retval = -1;
2505824a1566SKashyap Desai 		goto out_unlock;
2506824a1566SKashyap Desai 	}
2507824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2508824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2509824a1566SKashyap Desai 		ioc_err(mrioc,
2510824a1566SKashyap Desai 		    "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2511824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2512824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2513824a1566SKashyap Desai 		retval = -1;
2514824a1566SKashyap Desai 		goto out_unlock;
2515824a1566SKashyap Desai 	}
2516824a1566SKashyap Desai 	memcpy(facts_data, (u8 *)data, data_len);
2517*c5758fc7SSreekanth Reddy 	mpi3mr_process_factsdata(mrioc, facts_data);
2518824a1566SKashyap Desai out_unlock:
2519824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2520824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2521824a1566SKashyap Desai 
2522824a1566SKashyap Desai out:
2523824a1566SKashyap Desai 	if (data)
2524824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
2525824a1566SKashyap Desai 
2526824a1566SKashyap Desai 	return retval;
2527824a1566SKashyap Desai }
2528824a1566SKashyap Desai 
2529824a1566SKashyap Desai /**
2530824a1566SKashyap Desai  * mpi3mr_check_reset_dma_mask - Process IOC facts data
2531824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2532824a1566SKashyap Desai  *
2533824a1566SKashyap Desai  * Check whether the new DMA mask requested through IOCFacts by
2534824a1566SKashyap Desai  * firmware needs to be set, if so set it .
2535824a1566SKashyap Desai  *
2536824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
2537824a1566SKashyap Desai  */
2538824a1566SKashyap Desai static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
2539824a1566SKashyap Desai {
2540824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
2541824a1566SKashyap Desai 	int r;
2542824a1566SKashyap Desai 	u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
2543824a1566SKashyap Desai 
2544824a1566SKashyap Desai 	if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
2545824a1566SKashyap Desai 		return 0;
2546824a1566SKashyap Desai 
2547824a1566SKashyap Desai 	ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
2548824a1566SKashyap Desai 	    mrioc->dma_mask, facts_dma_mask);
2549824a1566SKashyap Desai 
2550824a1566SKashyap Desai 	r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
2551824a1566SKashyap Desai 	if (r) {
2552824a1566SKashyap Desai 		ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
2553824a1566SKashyap Desai 		    facts_dma_mask, r);
2554824a1566SKashyap Desai 		return r;
2555824a1566SKashyap Desai 	}
2556824a1566SKashyap Desai 	mrioc->dma_mask = facts_dma_mask;
2557824a1566SKashyap Desai 	return r;
2558824a1566SKashyap Desai }
2559824a1566SKashyap Desai 
2560824a1566SKashyap Desai /**
2561824a1566SKashyap Desai  * mpi3mr_process_factsdata - Process IOC facts data
2562824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2563824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2564824a1566SKashyap Desai  *
2565824a1566SKashyap Desai  * Convert IOC facts data into cpu endianness and cache it in
2566824a1566SKashyap Desai  * the driver .
2567824a1566SKashyap Desai  *
2568824a1566SKashyap Desai  * Return: Nothing.
2569824a1566SKashyap Desai  */
2570824a1566SKashyap Desai static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
2571824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2572824a1566SKashyap Desai {
2573824a1566SKashyap Desai 	u32 ioc_config, req_sz, facts_flags;
2574824a1566SKashyap Desai 
2575824a1566SKashyap Desai 	if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
2576824a1566SKashyap Desai 	    (sizeof(*facts_data) / 4)) {
2577824a1566SKashyap Desai 		ioc_warn(mrioc,
2578824a1566SKashyap Desai 		    "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
2579824a1566SKashyap Desai 		    sizeof(*facts_data),
2580824a1566SKashyap Desai 		    le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
2581824a1566SKashyap Desai 	}
2582824a1566SKashyap Desai 
2583824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
2584824a1566SKashyap Desai 	req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
2585824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
2586824a1566SKashyap Desai 	if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
2587824a1566SKashyap Desai 		ioc_err(mrioc,
2588824a1566SKashyap Desai 		    "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
2589824a1566SKashyap Desai 		    req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
2590824a1566SKashyap Desai 	}
2591824a1566SKashyap Desai 
2592824a1566SKashyap Desai 	memset(&mrioc->facts, 0, sizeof(mrioc->facts));
2593824a1566SKashyap Desai 
2594824a1566SKashyap Desai 	facts_flags = le32_to_cpu(facts_data->flags);
2595824a1566SKashyap Desai 	mrioc->facts.op_req_sz = req_sz;
2596824a1566SKashyap Desai 	mrioc->op_reply_desc_sz = 1 << ((ioc_config &
2597824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
2598824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
2599824a1566SKashyap Desai 
2600824a1566SKashyap Desai 	mrioc->facts.ioc_num = facts_data->ioc_number;
2601824a1566SKashyap Desai 	mrioc->facts.who_init = facts_data->who_init;
2602824a1566SKashyap Desai 	mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
2603824a1566SKashyap Desai 	mrioc->facts.personality = (facts_flags &
2604824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
2605824a1566SKashyap Desai 	mrioc->facts.dma_mask = (facts_flags &
2606824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
2607824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
2608824a1566SKashyap Desai 	mrioc->facts.protocol_flags = facts_data->protocol_flags;
2609824a1566SKashyap Desai 	mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
2610824a1566SKashyap Desai 	mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_request);
2611824a1566SKashyap Desai 	mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
2612824a1566SKashyap Desai 	mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
2613824a1566SKashyap Desai 	mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
2614824a1566SKashyap Desai 	mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
2615824a1566SKashyap Desai 	mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
2616824a1566SKashyap Desai 	mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
2617ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds);
2618ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds);
2619824a1566SKashyap Desai 	mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
2620824a1566SKashyap Desai 	mrioc->facts.max_pcie_switches =
2621ec5ebd2cSSreekanth Reddy 	    le16_to_cpu(facts_data->max_pcie_switches);
2622824a1566SKashyap Desai 	mrioc->facts.max_sasexpanders =
2623824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_expanders);
2624824a1566SKashyap Desai 	mrioc->facts.max_sasinitiators =
2625824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_initiators);
2626824a1566SKashyap Desai 	mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
2627824a1566SKashyap Desai 	mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
2628824a1566SKashyap Desai 	mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
2629824a1566SKashyap Desai 	mrioc->facts.max_op_req_q =
2630824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_request_queues);
2631824a1566SKashyap Desai 	mrioc->facts.max_op_reply_q =
2632824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_reply_queues);
2633824a1566SKashyap Desai 	mrioc->facts.ioc_capabilities =
2634824a1566SKashyap Desai 	    le32_to_cpu(facts_data->ioc_capabilities);
2635824a1566SKashyap Desai 	mrioc->facts.fw_ver.build_num =
2636824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.build_num);
2637824a1566SKashyap Desai 	mrioc->facts.fw_ver.cust_id =
2638824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.customer_id);
2639824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
2640824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
2641824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
2642824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
2643824a1566SKashyap Desai 	mrioc->msix_count = min_t(int, mrioc->msix_count,
2644824a1566SKashyap Desai 	    mrioc->facts.max_msix_vectors);
2645824a1566SKashyap Desai 	mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
2646824a1566SKashyap Desai 	mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
2647824a1566SKashyap Desai 	mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
2648824a1566SKashyap Desai 	mrioc->facts.shutdown_timeout =
2649824a1566SKashyap Desai 	    le16_to_cpu(facts_data->shutdown_timeout);
2650824a1566SKashyap Desai 
2651824a1566SKashyap Desai 	ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
2652824a1566SKashyap Desai 	    mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
2653824a1566SKashyap Desai 	    mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
2654824a1566SKashyap Desai 	ioc_info(mrioc,
2655ec5ebd2cSSreekanth Reddy 	    "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n",
2656824a1566SKashyap Desai 	    mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
2657ec5ebd2cSSreekanth Reddy 	    mrioc->facts.max_msix_vectors, mrioc->facts.max_perids);
2658824a1566SKashyap Desai 	ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
2659824a1566SKashyap Desai 	    mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
2660824a1566SKashyap Desai 	    mrioc->facts.sge_mod_shift);
2661824a1566SKashyap Desai 	ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
2662824a1566SKashyap Desai 	    mrioc->facts.dma_mask, (facts_flags &
2663824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
2664824a1566SKashyap Desai }
2665824a1566SKashyap Desai 
2666824a1566SKashyap Desai /**
2667824a1566SKashyap Desai  * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
2668824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2669824a1566SKashyap Desai  *
2670824a1566SKashyap Desai  * Allocate and initialize the reply free buffers, sense
2671824a1566SKashyap Desai  * buffers, reply free queue and sense buffer queue.
2672824a1566SKashyap Desai  *
2673824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2674824a1566SKashyap Desai  */
2675824a1566SKashyap Desai static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
2676824a1566SKashyap Desai {
2677824a1566SKashyap Desai 	int retval = 0;
2678824a1566SKashyap Desai 	u32 sz, i;
2679824a1566SKashyap Desai 
2680824a1566SKashyap Desai 	if (mrioc->init_cmds.reply)
2681e3605f65SSreekanth Reddy 		return retval;
2682824a1566SKashyap Desai 
2683*c5758fc7SSreekanth Reddy 	mrioc->init_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2684824a1566SKashyap Desai 	if (!mrioc->init_cmds.reply)
2685824a1566SKashyap Desai 		goto out_failed;
2686824a1566SKashyap Desai 
268713ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
2688*c5758fc7SSreekanth Reddy 		mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->reply_sz,
268913ef29eaSKashyap Desai 		    GFP_KERNEL);
269013ef29eaSKashyap Desai 		if (!mrioc->dev_rmhs_cmds[i].reply)
269113ef29eaSKashyap Desai 			goto out_failed;
269213ef29eaSKashyap Desai 	}
269313ef29eaSKashyap Desai 
2694*c5758fc7SSreekanth Reddy 	mrioc->host_tm_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2695e844adb1SKashyap Desai 	if (!mrioc->host_tm_cmds.reply)
2696e844adb1SKashyap Desai 		goto out_failed;
2697e844adb1SKashyap Desai 
2698e844adb1SKashyap Desai 	mrioc->dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
2699e844adb1SKashyap Desai 	if (mrioc->facts.max_devhandle % 8)
2700e844adb1SKashyap Desai 		mrioc->dev_handle_bitmap_sz++;
2701e844adb1SKashyap Desai 	mrioc->removepend_bitmap = kzalloc(mrioc->dev_handle_bitmap_sz,
2702e844adb1SKashyap Desai 	    GFP_KERNEL);
2703e844adb1SKashyap Desai 	if (!mrioc->removepend_bitmap)
2704e844adb1SKashyap Desai 		goto out_failed;
2705e844adb1SKashyap Desai 
2706e844adb1SKashyap Desai 	mrioc->devrem_bitmap_sz = MPI3MR_NUM_DEVRMCMD / 8;
2707e844adb1SKashyap Desai 	if (MPI3MR_NUM_DEVRMCMD % 8)
2708e844adb1SKashyap Desai 		mrioc->devrem_bitmap_sz++;
2709e844adb1SKashyap Desai 	mrioc->devrem_bitmap = kzalloc(mrioc->devrem_bitmap_sz,
2710e844adb1SKashyap Desai 	    GFP_KERNEL);
2711e844adb1SKashyap Desai 	if (!mrioc->devrem_bitmap)
2712e844adb1SKashyap Desai 		goto out_failed;
2713e844adb1SKashyap Desai 
2714824a1566SKashyap Desai 	mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
2715824a1566SKashyap Desai 	mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
2716824a1566SKashyap Desai 	mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
2717824a1566SKashyap Desai 	mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
2718824a1566SKashyap Desai 
2719824a1566SKashyap Desai 	/* reply buffer pool, 16 byte align */
2720*c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2721824a1566SKashyap Desai 	mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
2722824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
2723824a1566SKashyap Desai 	if (!mrioc->reply_buf_pool) {
2724824a1566SKashyap Desai 		ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
2725824a1566SKashyap Desai 		goto out_failed;
2726824a1566SKashyap Desai 	}
2727824a1566SKashyap Desai 
2728824a1566SKashyap Desai 	mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
2729824a1566SKashyap Desai 	    &mrioc->reply_buf_dma);
2730824a1566SKashyap Desai 	if (!mrioc->reply_buf)
2731824a1566SKashyap Desai 		goto out_failed;
2732824a1566SKashyap Desai 
2733824a1566SKashyap Desai 	mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
2734824a1566SKashyap Desai 
2735824a1566SKashyap Desai 	/* reply free queue, 8 byte align */
2736824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
2737824a1566SKashyap Desai 	mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
2738824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
2739824a1566SKashyap Desai 	if (!mrioc->reply_free_q_pool) {
2740824a1566SKashyap Desai 		ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
2741824a1566SKashyap Desai 		goto out_failed;
2742824a1566SKashyap Desai 	}
2743824a1566SKashyap Desai 	mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
2744824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->reply_free_q_dma);
2745824a1566SKashyap Desai 	if (!mrioc->reply_free_q)
2746824a1566SKashyap Desai 		goto out_failed;
2747824a1566SKashyap Desai 
2748824a1566SKashyap Desai 	/* sense buffer pool,  4 byte align */
2749ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2750824a1566SKashyap Desai 	mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
2751824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 4, 0);
2752824a1566SKashyap Desai 	if (!mrioc->sense_buf_pool) {
2753824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
2754824a1566SKashyap Desai 		goto out_failed;
2755824a1566SKashyap Desai 	}
2756824a1566SKashyap Desai 	mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
2757824a1566SKashyap Desai 	    &mrioc->sense_buf_dma);
2758824a1566SKashyap Desai 	if (!mrioc->sense_buf)
2759824a1566SKashyap Desai 		goto out_failed;
2760824a1566SKashyap Desai 
2761824a1566SKashyap Desai 	/* sense buffer queue, 8 byte align */
2762824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
2763824a1566SKashyap Desai 	mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
2764824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
2765824a1566SKashyap Desai 	if (!mrioc->sense_buf_q_pool) {
2766824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
2767824a1566SKashyap Desai 		goto out_failed;
2768824a1566SKashyap Desai 	}
2769824a1566SKashyap Desai 	mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
2770824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->sense_buf_q_dma);
2771824a1566SKashyap Desai 	if (!mrioc->sense_buf_q)
2772824a1566SKashyap Desai 		goto out_failed;
2773824a1566SKashyap Desai 
2774e3605f65SSreekanth Reddy 	return retval;
2775e3605f65SSreekanth Reddy 
2776e3605f65SSreekanth Reddy out_failed:
2777e3605f65SSreekanth Reddy 	retval = -1;
2778e3605f65SSreekanth Reddy 	return retval;
2779e3605f65SSreekanth Reddy }
2780e3605f65SSreekanth Reddy 
2781e3605f65SSreekanth Reddy /**
2782e3605f65SSreekanth Reddy  * mpimr_initialize_reply_sbuf_queues - initialize reply sense
2783e3605f65SSreekanth Reddy  * buffers
2784e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
2785e3605f65SSreekanth Reddy  *
2786e3605f65SSreekanth Reddy  * Helper function to initialize reply and sense buffers along
2787e3605f65SSreekanth Reddy  * with some debug prints.
2788e3605f65SSreekanth Reddy  *
2789e3605f65SSreekanth Reddy  * Return:  None.
2790e3605f65SSreekanth Reddy  */
2791e3605f65SSreekanth Reddy static void mpimr_initialize_reply_sbuf_queues(struct mpi3mr_ioc *mrioc)
2792e3605f65SSreekanth Reddy {
2793e3605f65SSreekanth Reddy 	u32 sz, i;
2794e3605f65SSreekanth Reddy 	dma_addr_t phy_addr;
2795e3605f65SSreekanth Reddy 
2796*c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2797824a1566SKashyap Desai 	ioc_info(mrioc,
2798824a1566SKashyap Desai 	    "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2799*c5758fc7SSreekanth Reddy 	    mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->reply_sz,
2800824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
2801824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
2802824a1566SKashyap Desai 	ioc_info(mrioc,
2803824a1566SKashyap Desai 	    "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2804824a1566SKashyap Desai 	    mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
2805824a1566SKashyap Desai 	    (unsigned long long)mrioc->reply_free_q_dma);
2806ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2807824a1566SKashyap Desai 	ioc_info(mrioc,
2808824a1566SKashyap Desai 	    "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
2809ec5ebd2cSSreekanth Reddy 	    mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ,
2810824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
2811824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
2812824a1566SKashyap Desai 	ioc_info(mrioc,
2813824a1566SKashyap Desai 	    "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
2814824a1566SKashyap Desai 	    mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
2815824a1566SKashyap Desai 	    (unsigned long long)mrioc->sense_buf_q_dma);
2816824a1566SKashyap Desai 
2817824a1566SKashyap Desai 	/* initialize Reply buffer Queue */
2818824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->reply_buf_dma;
2819*c5758fc7SSreekanth Reddy 	    i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->reply_sz)
2820824a1566SKashyap Desai 		mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
2821824a1566SKashyap Desai 	mrioc->reply_free_q[i] = cpu_to_le64(0);
2822824a1566SKashyap Desai 
2823824a1566SKashyap Desai 	/* initialize Sense Buffer Queue */
2824824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->sense_buf_dma;
2825ec5ebd2cSSreekanth Reddy 	    i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ)
2826824a1566SKashyap Desai 		mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
2827824a1566SKashyap Desai 	mrioc->sense_buf_q[i] = cpu_to_le64(0);
2828824a1566SKashyap Desai }
2829824a1566SKashyap Desai 
2830824a1566SKashyap Desai /**
2831824a1566SKashyap Desai  * mpi3mr_issue_iocinit - Send IOC Init
2832824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2833824a1566SKashyap Desai  *
2834824a1566SKashyap Desai  * Issue IOC Init MPI request through admin queue and wait for
2835824a1566SKashyap Desai  * the completion of it or time out.
2836824a1566SKashyap Desai  *
2837824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2838824a1566SKashyap Desai  */
2839824a1566SKashyap Desai static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
2840824a1566SKashyap Desai {
2841824a1566SKashyap Desai 	struct mpi3_ioc_init_request iocinit_req;
2842824a1566SKashyap Desai 	struct mpi3_driver_info_layout *drv_info;
2843824a1566SKashyap Desai 	dma_addr_t data_dma;
2844824a1566SKashyap Desai 	u32 data_len = sizeof(*drv_info);
2845824a1566SKashyap Desai 	int retval = 0;
2846824a1566SKashyap Desai 	ktime_t current_time;
2847824a1566SKashyap Desai 
2848824a1566SKashyap Desai 	drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2849824a1566SKashyap Desai 	    GFP_KERNEL);
2850824a1566SKashyap Desai 	if (!drv_info) {
2851824a1566SKashyap Desai 		retval = -1;
2852824a1566SKashyap Desai 		goto out;
2853824a1566SKashyap Desai 	}
2854e3605f65SSreekanth Reddy 	mpimr_initialize_reply_sbuf_queues(mrioc);
2855e3605f65SSreekanth Reddy 
2856824a1566SKashyap Desai 	drv_info->information_length = cpu_to_le32(data_len);
2857aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
2858aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
2859aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
2860aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
2861aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
2862aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE,
2863aa0dc6a7SSreekanth Reddy 	    sizeof(drv_info->driver_release_date));
2864824a1566SKashyap Desai 	drv_info->driver_capabilities = 0;
2865824a1566SKashyap Desai 	memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
2866824a1566SKashyap Desai 	    sizeof(mrioc->driver_info));
2867824a1566SKashyap Desai 
2868824a1566SKashyap Desai 	memset(&iocinit_req, 0, sizeof(iocinit_req));
2869824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
2870824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2871824a1566SKashyap Desai 		retval = -1;
2872824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
2873824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
2874824a1566SKashyap Desai 		goto out;
2875824a1566SKashyap Desai 	}
2876824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2877824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
2878824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
2879824a1566SKashyap Desai 	iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2880824a1566SKashyap Desai 	iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
2881824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
2882824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
2883824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
2884824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
2885824a1566SKashyap Desai 	iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
2886824a1566SKashyap Desai 	iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
2887824a1566SKashyap Desai 	iocinit_req.reply_free_queue_address =
2888824a1566SKashyap Desai 	    cpu_to_le64(mrioc->reply_free_q_dma);
2889ec5ebd2cSSreekanth Reddy 	iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ);
2890824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_depth =
2891824a1566SKashyap Desai 	    cpu_to_le16(mrioc->sense_buf_q_sz);
2892824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_address =
2893824a1566SKashyap Desai 	    cpu_to_le64(mrioc->sense_buf_q_dma);
2894824a1566SKashyap Desai 	iocinit_req.driver_information_address = cpu_to_le64(data_dma);
2895824a1566SKashyap Desai 
2896824a1566SKashyap Desai 	current_time = ktime_get_real();
2897824a1566SKashyap Desai 	iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
2898824a1566SKashyap Desai 
2899824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
2900824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
2901824a1566SKashyap Desai 	    sizeof(iocinit_req), 1);
2902824a1566SKashyap Desai 	if (retval) {
2903824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
2904824a1566SKashyap Desai 		goto out_unlock;
2905824a1566SKashyap Desai 	}
2906824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2907824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2908824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2909a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2910824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
2911a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_init timed out\n");
2912824a1566SKashyap Desai 		retval = -1;
2913824a1566SKashyap Desai 		goto out_unlock;
2914824a1566SKashyap Desai 	}
2915824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2916824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2917824a1566SKashyap Desai 		ioc_err(mrioc,
2918824a1566SKashyap Desai 		    "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2919824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2920824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2921824a1566SKashyap Desai 		retval = -1;
2922824a1566SKashyap Desai 		goto out_unlock;
2923824a1566SKashyap Desai 	}
2924824a1566SKashyap Desai 
2925e3605f65SSreekanth Reddy 	mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
2926e3605f65SSreekanth Reddy 	writel(mrioc->reply_free_queue_host_index,
2927e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->reply_free_host_index);
2928e3605f65SSreekanth Reddy 
2929e3605f65SSreekanth Reddy 	mrioc->sbq_host_index = mrioc->num_sense_bufs;
2930e3605f65SSreekanth Reddy 	writel(mrioc->sbq_host_index,
2931e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
2932824a1566SKashyap Desai out_unlock:
2933824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2934824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2935824a1566SKashyap Desai 
2936824a1566SKashyap Desai out:
2937824a1566SKashyap Desai 	if (drv_info)
2938824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
2939824a1566SKashyap Desai 		    data_dma);
2940824a1566SKashyap Desai 
2941824a1566SKashyap Desai 	return retval;
2942824a1566SKashyap Desai }
2943824a1566SKashyap Desai 
2944824a1566SKashyap Desai /**
294513ef29eaSKashyap Desai  * mpi3mr_unmask_events - Unmask events in event mask bitmap
294613ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
294713ef29eaSKashyap Desai  * @event: MPI event ID
294813ef29eaSKashyap Desai  *
294913ef29eaSKashyap Desai  * Un mask the specific event by resetting the event_mask
295013ef29eaSKashyap Desai  * bitmap.
295113ef29eaSKashyap Desai  *
295213ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
295313ef29eaSKashyap Desai  */
295413ef29eaSKashyap Desai static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event)
295513ef29eaSKashyap Desai {
295613ef29eaSKashyap Desai 	u32 desired_event;
295713ef29eaSKashyap Desai 	u8 word;
295813ef29eaSKashyap Desai 
295913ef29eaSKashyap Desai 	if (event >= 128)
296013ef29eaSKashyap Desai 		return;
296113ef29eaSKashyap Desai 
296213ef29eaSKashyap Desai 	desired_event = (1 << (event % 32));
296313ef29eaSKashyap Desai 	word = event / 32;
296413ef29eaSKashyap Desai 
296513ef29eaSKashyap Desai 	mrioc->event_masks[word] &= ~desired_event;
296613ef29eaSKashyap Desai }
296713ef29eaSKashyap Desai 
296813ef29eaSKashyap Desai /**
296913ef29eaSKashyap Desai  * mpi3mr_issue_event_notification - Send event notification
297013ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
297113ef29eaSKashyap Desai  *
297213ef29eaSKashyap Desai  * Issue event notification MPI request through admin queue and
297313ef29eaSKashyap Desai  * wait for the completion of it or time out.
297413ef29eaSKashyap Desai  *
297513ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
297613ef29eaSKashyap Desai  */
297713ef29eaSKashyap Desai static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc)
297813ef29eaSKashyap Desai {
297913ef29eaSKashyap Desai 	struct mpi3_event_notification_request evtnotify_req;
298013ef29eaSKashyap Desai 	int retval = 0;
298113ef29eaSKashyap Desai 	u8 i;
298213ef29eaSKashyap Desai 
298313ef29eaSKashyap Desai 	memset(&evtnotify_req, 0, sizeof(evtnotify_req));
298413ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
298513ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
298613ef29eaSKashyap Desai 		retval = -1;
298713ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n");
298813ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
298913ef29eaSKashyap Desai 		goto out;
299013ef29eaSKashyap Desai 	}
299113ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
299213ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
299313ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
299413ef29eaSKashyap Desai 	evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
299513ef29eaSKashyap Desai 	evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION;
299613ef29eaSKashyap Desai 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
299713ef29eaSKashyap Desai 		evtnotify_req.event_masks[i] =
299813ef29eaSKashyap Desai 		    cpu_to_le32(mrioc->event_masks[i]);
299913ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
300013ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req,
300113ef29eaSKashyap Desai 	    sizeof(evtnotify_req), 1);
300213ef29eaSKashyap Desai 	if (retval) {
300313ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n");
300413ef29eaSKashyap Desai 		goto out_unlock;
300513ef29eaSKashyap Desai 	}
300613ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
300713ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
300813ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3009a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "event notification timed out\n");
3010a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
301113ef29eaSKashyap Desai 		    MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
301213ef29eaSKashyap Desai 		retval = -1;
301313ef29eaSKashyap Desai 		goto out_unlock;
301413ef29eaSKashyap Desai 	}
301513ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
301613ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
301713ef29eaSKashyap Desai 		ioc_err(mrioc,
301813ef29eaSKashyap Desai 		    "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
301913ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
302013ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
302113ef29eaSKashyap Desai 		retval = -1;
302213ef29eaSKashyap Desai 		goto out_unlock;
302313ef29eaSKashyap Desai 	}
302413ef29eaSKashyap Desai 
302513ef29eaSKashyap Desai out_unlock:
302613ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
302713ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
302813ef29eaSKashyap Desai out:
302913ef29eaSKashyap Desai 	return retval;
303013ef29eaSKashyap Desai }
303113ef29eaSKashyap Desai 
303213ef29eaSKashyap Desai /**
303313ef29eaSKashyap Desai  * mpi3mr_send_event_ack - Send event acknowledgment
303413ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
303513ef29eaSKashyap Desai  * @event: MPI3 event ID
303613ef29eaSKashyap Desai  * @event_ctx: Event context
303713ef29eaSKashyap Desai  *
303813ef29eaSKashyap Desai  * Send event acknowledgment through admin queue and wait for
303913ef29eaSKashyap Desai  * it to complete.
304013ef29eaSKashyap Desai  *
304113ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
304213ef29eaSKashyap Desai  */
304313ef29eaSKashyap Desai int mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
304413ef29eaSKashyap Desai 	u32 event_ctx)
304513ef29eaSKashyap Desai {
304613ef29eaSKashyap Desai 	struct mpi3_event_ack_request evtack_req;
304713ef29eaSKashyap Desai 	int retval = 0;
304813ef29eaSKashyap Desai 
304913ef29eaSKashyap Desai 	memset(&evtack_req, 0, sizeof(evtack_req));
305013ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
305113ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
305213ef29eaSKashyap Desai 		retval = -1;
305313ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Init command is in use\n");
305413ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
305513ef29eaSKashyap Desai 		goto out;
305613ef29eaSKashyap Desai 	}
305713ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
305813ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
305913ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
306013ef29eaSKashyap Desai 	evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
306113ef29eaSKashyap Desai 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
306213ef29eaSKashyap Desai 	evtack_req.event = event;
306313ef29eaSKashyap Desai 	evtack_req.event_context = cpu_to_le32(event_ctx);
306413ef29eaSKashyap Desai 
306513ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
306613ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
306713ef29eaSKashyap Desai 	    sizeof(evtack_req), 1);
306813ef29eaSKashyap Desai 	if (retval) {
306913ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Admin Post failed\n");
307013ef29eaSKashyap Desai 		goto out_unlock;
307113ef29eaSKashyap Desai 	}
307213ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
307313ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
307413ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
307513ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: command timed out\n");
3076fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
307713ef29eaSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
307813ef29eaSKashyap Desai 			    MPI3MR_RESET_FROM_EVTACK_TIMEOUT, 1);
307913ef29eaSKashyap Desai 		retval = -1;
308013ef29eaSKashyap Desai 		goto out_unlock;
308113ef29eaSKashyap Desai 	}
308213ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
308313ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
308413ef29eaSKashyap Desai 		ioc_err(mrioc,
308513ef29eaSKashyap Desai 		    "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
308613ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
308713ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
308813ef29eaSKashyap Desai 		retval = -1;
308913ef29eaSKashyap Desai 		goto out_unlock;
309013ef29eaSKashyap Desai 	}
309113ef29eaSKashyap Desai 
309213ef29eaSKashyap Desai out_unlock:
309313ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
309413ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
309513ef29eaSKashyap Desai out:
309613ef29eaSKashyap Desai 	return retval;
309713ef29eaSKashyap Desai }
309813ef29eaSKashyap Desai 
309913ef29eaSKashyap Desai /**
3100824a1566SKashyap Desai  * mpi3mr_alloc_chain_bufs - Allocate chain buffers
3101824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3102824a1566SKashyap Desai  *
3103824a1566SKashyap Desai  * Allocate chain buffers and set a bitmap to indicate free
3104824a1566SKashyap Desai  * chain buffers. Chain buffers are used to pass the SGE
3105824a1566SKashyap Desai  * information along with MPI3 SCSI IO requests for host I/O.
3106824a1566SKashyap Desai  *
3107824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure
3108824a1566SKashyap Desai  */
3109824a1566SKashyap Desai static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
3110824a1566SKashyap Desai {
3111824a1566SKashyap Desai 	int retval = 0;
3112824a1566SKashyap Desai 	u32 sz, i;
3113824a1566SKashyap Desai 	u16 num_chains;
3114824a1566SKashyap Desai 
3115fe6db615SSreekanth Reddy 	if (mrioc->chain_sgl_list)
3116fe6db615SSreekanth Reddy 		return retval;
3117fe6db615SSreekanth Reddy 
3118824a1566SKashyap Desai 	num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
3119824a1566SKashyap Desai 
312074e1f30aSKashyap Desai 	if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION
312174e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE1_PROTECTION
312274e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE2_PROTECTION
312374e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE3_PROTECTION))
312474e1f30aSKashyap Desai 		num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR);
312574e1f30aSKashyap Desai 
3126824a1566SKashyap Desai 	mrioc->chain_buf_count = num_chains;
3127824a1566SKashyap Desai 	sz = sizeof(struct chain_element) * num_chains;
3128824a1566SKashyap Desai 	mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
3129824a1566SKashyap Desai 	if (!mrioc->chain_sgl_list)
3130824a1566SKashyap Desai 		goto out_failed;
3131824a1566SKashyap Desai 
3132824a1566SKashyap Desai 	sz = MPI3MR_PAGE_SIZE_4K;
3133824a1566SKashyap Desai 	mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
3134824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
3135824a1566SKashyap Desai 	if (!mrioc->chain_buf_pool) {
3136824a1566SKashyap Desai 		ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
3137824a1566SKashyap Desai 		goto out_failed;
3138824a1566SKashyap Desai 	}
3139824a1566SKashyap Desai 
3140824a1566SKashyap Desai 	for (i = 0; i < num_chains; i++) {
3141824a1566SKashyap Desai 		mrioc->chain_sgl_list[i].addr =
3142824a1566SKashyap Desai 		    dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
3143824a1566SKashyap Desai 		    &mrioc->chain_sgl_list[i].dma_addr);
3144824a1566SKashyap Desai 
3145824a1566SKashyap Desai 		if (!mrioc->chain_sgl_list[i].addr)
3146824a1566SKashyap Desai 			goto out_failed;
3147824a1566SKashyap Desai 	}
3148824a1566SKashyap Desai 	mrioc->chain_bitmap_sz = num_chains / 8;
3149824a1566SKashyap Desai 	if (num_chains % 8)
3150824a1566SKashyap Desai 		mrioc->chain_bitmap_sz++;
3151824a1566SKashyap Desai 	mrioc->chain_bitmap = kzalloc(mrioc->chain_bitmap_sz, GFP_KERNEL);
3152824a1566SKashyap Desai 	if (!mrioc->chain_bitmap)
3153824a1566SKashyap Desai 		goto out_failed;
3154824a1566SKashyap Desai 	return retval;
3155824a1566SKashyap Desai out_failed:
3156824a1566SKashyap Desai 	retval = -1;
3157824a1566SKashyap Desai 	return retval;
3158824a1566SKashyap Desai }
3159824a1566SKashyap Desai 
3160824a1566SKashyap Desai /**
3161023ab2a9SKashyap Desai  * mpi3mr_port_enable_complete - Mark port enable complete
3162023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3163023ab2a9SKashyap Desai  * @drv_cmd: Internal command tracker
3164023ab2a9SKashyap Desai  *
3165023ab2a9SKashyap Desai  * Call back for asynchronous port enable request sets the
3166023ab2a9SKashyap Desai  * driver command to indicate port enable request is complete.
3167023ab2a9SKashyap Desai  *
3168023ab2a9SKashyap Desai  * Return: Nothing
3169023ab2a9SKashyap Desai  */
3170023ab2a9SKashyap Desai static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc,
3171023ab2a9SKashyap Desai 	struct mpi3mr_drv_cmd *drv_cmd)
3172023ab2a9SKashyap Desai {
3173023ab2a9SKashyap Desai 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3174023ab2a9SKashyap Desai 	drv_cmd->callback = NULL;
3175023ab2a9SKashyap Desai 	mrioc->scan_failed = drv_cmd->ioc_status;
3176023ab2a9SKashyap Desai 	mrioc->scan_started = 0;
3177023ab2a9SKashyap Desai }
3178023ab2a9SKashyap Desai 
3179023ab2a9SKashyap Desai /**
3180023ab2a9SKashyap Desai  * mpi3mr_issue_port_enable - Issue Port Enable
3181023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3182023ab2a9SKashyap Desai  * @async: Flag to wait for completion or not
3183023ab2a9SKashyap Desai  *
3184023ab2a9SKashyap Desai  * Issue Port Enable MPI request through admin queue and if the
3185023ab2a9SKashyap Desai  * async flag is not set wait for the completion of the port
3186023ab2a9SKashyap Desai  * enable or time out.
3187023ab2a9SKashyap Desai  *
3188023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failures.
3189023ab2a9SKashyap Desai  */
3190023ab2a9SKashyap Desai int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async)
3191023ab2a9SKashyap Desai {
3192023ab2a9SKashyap Desai 	struct mpi3_port_enable_request pe_req;
3193023ab2a9SKashyap Desai 	int retval = 0;
3194023ab2a9SKashyap Desai 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3195023ab2a9SKashyap Desai 
3196023ab2a9SKashyap Desai 	memset(&pe_req, 0, sizeof(pe_req));
3197023ab2a9SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3198023ab2a9SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3199023ab2a9SKashyap Desai 		retval = -1;
3200023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Init command is in use\n");
3201023ab2a9SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3202023ab2a9SKashyap Desai 		goto out;
3203023ab2a9SKashyap Desai 	}
3204023ab2a9SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3205023ab2a9SKashyap Desai 	if (async) {
3206023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
3207023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = mpi3mr_port_enable_complete;
3208023ab2a9SKashyap Desai 	} else {
3209023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 1;
3210023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = NULL;
3211023ab2a9SKashyap Desai 		init_completion(&mrioc->init_cmds.done);
3212023ab2a9SKashyap Desai 	}
3213023ab2a9SKashyap Desai 	pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3214023ab2a9SKashyap Desai 	pe_req.function = MPI3_FUNCTION_PORT_ENABLE;
3215023ab2a9SKashyap Desai 
3216023ab2a9SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1);
3217023ab2a9SKashyap Desai 	if (retval) {
3218023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n");
3219023ab2a9SKashyap Desai 		goto out_unlock;
3220023ab2a9SKashyap Desai 	}
3221a6856cc4SSreekanth Reddy 	if (async) {
3222a6856cc4SSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
3223a6856cc4SSreekanth Reddy 		goto out;
3224a6856cc4SSreekanth Reddy 	}
3225a6856cc4SSreekanth Reddy 
3226a6856cc4SSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ));
3227023ab2a9SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3228a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "port enable timed out\n");
3229023ab2a9SKashyap Desai 		retval = -1;
3230a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT);
3231023ab2a9SKashyap Desai 		goto out_unlock;
3232023ab2a9SKashyap Desai 	}
3233023ab2a9SKashyap Desai 	mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds);
3234a6856cc4SSreekanth Reddy 
3235023ab2a9SKashyap Desai out_unlock:
3236a6856cc4SSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3237023ab2a9SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3238023ab2a9SKashyap Desai out:
3239023ab2a9SKashyap Desai 	return retval;
3240023ab2a9SKashyap Desai }
3241023ab2a9SKashyap Desai 
3242ff9561e9SKashyap Desai /* Protocol type to name mapper structure */
3243ff9561e9SKashyap Desai static const struct {
3244ff9561e9SKashyap Desai 	u8 protocol;
3245ff9561e9SKashyap Desai 	char *name;
3246ff9561e9SKashyap Desai } mpi3mr_protocols[] = {
3247ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" },
3248ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" },
3249ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" },
3250ff9561e9SKashyap Desai };
3251ff9561e9SKashyap Desai 
3252ff9561e9SKashyap Desai /* Capability to name mapper structure*/
3253ff9561e9SKashyap Desai static const struct {
3254ff9561e9SKashyap Desai 	u32 capability;
3255ff9561e9SKashyap Desai 	char *name;
3256ff9561e9SKashyap Desai } mpi3mr_capabilities[] = {
3257ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE, "RAID" },
3258ff9561e9SKashyap Desai };
3259ff9561e9SKashyap Desai 
3260ff9561e9SKashyap Desai /**
3261ff9561e9SKashyap Desai  * mpi3mr_print_ioc_info - Display controller information
3262ff9561e9SKashyap Desai  * @mrioc: Adapter instance reference
3263ff9561e9SKashyap Desai  *
3264ff9561e9SKashyap Desai  * Display controller personalit, capability, supported
3265ff9561e9SKashyap Desai  * protocols etc.
3266ff9561e9SKashyap Desai  *
3267ff9561e9SKashyap Desai  * Return: Nothing
3268ff9561e9SKashyap Desai  */
3269ff9561e9SKashyap Desai static void
3270ff9561e9SKashyap Desai mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc)
3271ff9561e9SKashyap Desai {
327276a4f7ccSDan Carpenter 	int i = 0, bytes_written = 0;
3273ff9561e9SKashyap Desai 	char personality[16];
3274ff9561e9SKashyap Desai 	char protocol[50] = {0};
3275ff9561e9SKashyap Desai 	char capabilities[100] = {0};
3276ff9561e9SKashyap Desai 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
3277ff9561e9SKashyap Desai 
3278ff9561e9SKashyap Desai 	switch (mrioc->facts.personality) {
3279ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
3280ff9561e9SKashyap Desai 		strncpy(personality, "Enhanced HBA", sizeof(personality));
3281ff9561e9SKashyap Desai 		break;
3282ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
3283ff9561e9SKashyap Desai 		strncpy(personality, "RAID", sizeof(personality));
3284ff9561e9SKashyap Desai 		break;
3285ff9561e9SKashyap Desai 	default:
3286ff9561e9SKashyap Desai 		strncpy(personality, "Unknown", sizeof(personality));
3287ff9561e9SKashyap Desai 		break;
3288ff9561e9SKashyap Desai 	}
3289ff9561e9SKashyap Desai 
3290ff9561e9SKashyap Desai 	ioc_info(mrioc, "Running in %s Personality", personality);
3291ff9561e9SKashyap Desai 
3292ff9561e9SKashyap Desai 	ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n",
3293ff9561e9SKashyap Desai 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
3294ff9561e9SKashyap Desai 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
3295ff9561e9SKashyap Desai 
3296ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) {
3297ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3298ff9561e9SKashyap Desai 		    mpi3mr_protocols[i].protocol) {
329930e99f05SDan Carpenter 			bytes_written += scnprintf(protocol + bytes_written,
330076a4f7ccSDan Carpenter 				    sizeof(protocol) - bytes_written, "%s%s",
330176a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3302ff9561e9SKashyap Desai 				    mpi3mr_protocols[i].name);
3303ff9561e9SKashyap Desai 		}
3304ff9561e9SKashyap Desai 	}
3305ff9561e9SKashyap Desai 
330676a4f7ccSDan Carpenter 	bytes_written = 0;
3307ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) {
3308ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3309ff9561e9SKashyap Desai 		    mpi3mr_capabilities[i].capability) {
331030e99f05SDan Carpenter 			bytes_written += scnprintf(capabilities + bytes_written,
331176a4f7ccSDan Carpenter 				    sizeof(capabilities) - bytes_written, "%s%s",
331276a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3313ff9561e9SKashyap Desai 				    mpi3mr_capabilities[i].name);
3314ff9561e9SKashyap Desai 		}
3315ff9561e9SKashyap Desai 	}
3316ff9561e9SKashyap Desai 
3317ff9561e9SKashyap Desai 	ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n",
3318ff9561e9SKashyap Desai 		 protocol, capabilities);
3319ff9561e9SKashyap Desai }
3320ff9561e9SKashyap Desai 
3321023ab2a9SKashyap Desai /**
3322824a1566SKashyap Desai  * mpi3mr_cleanup_resources - Free PCI resources
3323824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3324824a1566SKashyap Desai  *
3325824a1566SKashyap Desai  * Unmap PCI device memory and disable PCI device.
3326824a1566SKashyap Desai  *
3327824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3328824a1566SKashyap Desai  */
3329824a1566SKashyap Desai void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
3330824a1566SKashyap Desai {
3331824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3332824a1566SKashyap Desai 
3333824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
3334824a1566SKashyap Desai 
3335824a1566SKashyap Desai 	if (mrioc->sysif_regs) {
3336824a1566SKashyap Desai 		iounmap((void __iomem *)mrioc->sysif_regs);
3337824a1566SKashyap Desai 		mrioc->sysif_regs = NULL;
3338824a1566SKashyap Desai 	}
3339824a1566SKashyap Desai 
3340824a1566SKashyap Desai 	if (pci_is_enabled(pdev)) {
3341824a1566SKashyap Desai 		if (mrioc->bars)
3342824a1566SKashyap Desai 			pci_release_selected_regions(pdev, mrioc->bars);
3343824a1566SKashyap Desai 		pci_disable_device(pdev);
3344824a1566SKashyap Desai 	}
3345824a1566SKashyap Desai }
3346824a1566SKashyap Desai 
3347824a1566SKashyap Desai /**
3348824a1566SKashyap Desai  * mpi3mr_setup_resources - Enable PCI resources
3349824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3350824a1566SKashyap Desai  *
3351824a1566SKashyap Desai  * Enable PCI device memory, MSI-x registers and set DMA mask.
3352824a1566SKashyap Desai  *
3353824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3354824a1566SKashyap Desai  */
3355824a1566SKashyap Desai int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
3356824a1566SKashyap Desai {
3357824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3358824a1566SKashyap Desai 	u32 memap_sz = 0;
3359824a1566SKashyap Desai 	int i, retval = 0, capb = 0;
3360824a1566SKashyap Desai 	u16 message_control;
3361824a1566SKashyap Desai 	u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
3362824a1566SKashyap Desai 	    (((dma_get_required_mask(&pdev->dev) > DMA_BIT_MASK(32)) &&
3363824a1566SKashyap Desai 	    (sizeof(dma_addr_t) > 4)) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
3364824a1566SKashyap Desai 
3365824a1566SKashyap Desai 	if (pci_enable_device_mem(pdev)) {
3366824a1566SKashyap Desai 		ioc_err(mrioc, "pci_enable_device_mem: failed\n");
3367824a1566SKashyap Desai 		retval = -ENODEV;
3368824a1566SKashyap Desai 		goto out_failed;
3369824a1566SKashyap Desai 	}
3370824a1566SKashyap Desai 
3371824a1566SKashyap Desai 	capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3372824a1566SKashyap Desai 	if (!capb) {
3373824a1566SKashyap Desai 		ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
3374824a1566SKashyap Desai 		retval = -ENODEV;
3375824a1566SKashyap Desai 		goto out_failed;
3376824a1566SKashyap Desai 	}
3377824a1566SKashyap Desai 	mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3378824a1566SKashyap Desai 
3379824a1566SKashyap Desai 	if (pci_request_selected_regions(pdev, mrioc->bars,
3380824a1566SKashyap Desai 	    mrioc->driver_name)) {
3381824a1566SKashyap Desai 		ioc_err(mrioc, "pci_request_selected_regions: failed\n");
3382824a1566SKashyap Desai 		retval = -ENODEV;
3383824a1566SKashyap Desai 		goto out_failed;
3384824a1566SKashyap Desai 	}
3385824a1566SKashyap Desai 
3386824a1566SKashyap Desai 	for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
3387824a1566SKashyap Desai 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3388824a1566SKashyap Desai 			mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
3389824a1566SKashyap Desai 			memap_sz = pci_resource_len(pdev, i);
3390824a1566SKashyap Desai 			mrioc->sysif_regs =
3391824a1566SKashyap Desai 			    ioremap(mrioc->sysif_regs_phys, memap_sz);
3392824a1566SKashyap Desai 			break;
3393824a1566SKashyap Desai 		}
3394824a1566SKashyap Desai 	}
3395824a1566SKashyap Desai 
3396824a1566SKashyap Desai 	pci_set_master(pdev);
3397824a1566SKashyap Desai 
3398824a1566SKashyap Desai 	retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
3399824a1566SKashyap Desai 	if (retval) {
3400824a1566SKashyap Desai 		if (dma_mask != DMA_BIT_MASK(32)) {
3401824a1566SKashyap Desai 			ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
3402824a1566SKashyap Desai 			dma_mask = DMA_BIT_MASK(32);
3403824a1566SKashyap Desai 			retval = dma_set_mask_and_coherent(&pdev->dev,
3404824a1566SKashyap Desai 			    dma_mask);
3405824a1566SKashyap Desai 		}
3406824a1566SKashyap Desai 		if (retval) {
3407824a1566SKashyap Desai 			mrioc->dma_mask = 0;
3408824a1566SKashyap Desai 			ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
3409824a1566SKashyap Desai 			goto out_failed;
3410824a1566SKashyap Desai 		}
3411824a1566SKashyap Desai 	}
3412824a1566SKashyap Desai 	mrioc->dma_mask = dma_mask;
3413824a1566SKashyap Desai 
3414824a1566SKashyap Desai 	if (!mrioc->sysif_regs) {
3415824a1566SKashyap Desai 		ioc_err(mrioc,
3416824a1566SKashyap Desai 		    "Unable to map adapter memory or resource not found\n");
3417824a1566SKashyap Desai 		retval = -EINVAL;
3418824a1566SKashyap Desai 		goto out_failed;
3419824a1566SKashyap Desai 	}
3420824a1566SKashyap Desai 
3421824a1566SKashyap Desai 	pci_read_config_word(pdev, capb + 2, &message_control);
3422824a1566SKashyap Desai 	mrioc->msix_count = (message_control & 0x3FF) + 1;
3423824a1566SKashyap Desai 
3424824a1566SKashyap Desai 	pci_save_state(pdev);
3425824a1566SKashyap Desai 
3426824a1566SKashyap Desai 	pci_set_drvdata(pdev, mrioc->shost);
3427824a1566SKashyap Desai 
3428824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
3429824a1566SKashyap Desai 
3430824a1566SKashyap Desai 	ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
3431824a1566SKashyap Desai 	    (unsigned long long)mrioc->sysif_regs_phys,
3432824a1566SKashyap Desai 	    mrioc->sysif_regs, memap_sz);
3433824a1566SKashyap Desai 	ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
3434824a1566SKashyap Desai 	    mrioc->msix_count);
3435824a1566SKashyap Desai 	return retval;
3436824a1566SKashyap Desai 
3437824a1566SKashyap Desai out_failed:
3438824a1566SKashyap Desai 	mpi3mr_cleanup_resources(mrioc);
3439824a1566SKashyap Desai 	return retval;
3440824a1566SKashyap Desai }
3441824a1566SKashyap Desai 
3442824a1566SKashyap Desai /**
3443e3605f65SSreekanth Reddy  * mpi3mr_enable_events - Enable required events
3444e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
3445e3605f65SSreekanth Reddy  *
3446e3605f65SSreekanth Reddy  * This routine unmasks the events required by the driver by
3447e3605f65SSreekanth Reddy  * sennding appropriate event mask bitmapt through an event
3448e3605f65SSreekanth Reddy  * notification request.
3449e3605f65SSreekanth Reddy  *
3450e3605f65SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3451e3605f65SSreekanth Reddy  */
3452e3605f65SSreekanth Reddy static int mpi3mr_enable_events(struct mpi3mr_ioc *mrioc)
3453e3605f65SSreekanth Reddy {
3454e3605f65SSreekanth Reddy 	int retval = 0;
3455e3605f65SSreekanth Reddy 	u32  i;
3456e3605f65SSreekanth Reddy 
3457e3605f65SSreekanth Reddy 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3458e3605f65SSreekanth Reddy 		mrioc->event_masks[i] = -1;
3459e3605f65SSreekanth Reddy 
3460e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED);
3461e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED);
3462e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
3463e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
3464e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3465e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY);
3466e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
3467e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
3468e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
3469e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION);
3470e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT);
3471e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE);
3472e3605f65SSreekanth Reddy 
3473e3605f65SSreekanth Reddy 	retval = mpi3mr_issue_event_notification(mrioc);
3474e3605f65SSreekanth Reddy 	if (retval)
3475e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to issue event notification %d\n",
3476e3605f65SSreekanth Reddy 		    retval);
3477e3605f65SSreekanth Reddy 	return retval;
3478e3605f65SSreekanth Reddy }
3479e3605f65SSreekanth Reddy 
3480e3605f65SSreekanth Reddy /**
3481824a1566SKashyap Desai  * mpi3mr_init_ioc - Initialize the controller
3482824a1566SKashyap Desai  * @mrioc: Adapter instance reference
34830da66348SKashyap Desai  * @init_type: Flag to indicate is the init_type
3484824a1566SKashyap Desai  *
3485824a1566SKashyap Desai  * This the controller initialization routine, executed either
3486824a1566SKashyap Desai  * after soft reset or from pci probe callback.
3487824a1566SKashyap Desai  * Setup the required resources, memory map the controller
3488824a1566SKashyap Desai  * registers, create admin and operational reply queue pairs,
3489824a1566SKashyap Desai  * allocate required memory for reply pool, sense buffer pool,
3490824a1566SKashyap Desai  * issue IOC init request to the firmware, unmask the events and
3491824a1566SKashyap Desai  * issue port enable to discover SAS/SATA/NVMe devies and RAID
3492824a1566SKashyap Desai  * volumes.
3493824a1566SKashyap Desai  *
3494824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3495824a1566SKashyap Desai  */
3496fe6db615SSreekanth Reddy int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc)
3497824a1566SKashyap Desai {
3498824a1566SKashyap Desai 	int retval = 0;
3499fe6db615SSreekanth Reddy 	u8 retry = 0;
3500824a1566SKashyap Desai 	struct mpi3_ioc_facts_data facts_data;
3501824a1566SKashyap Desai 
3502fe6db615SSreekanth Reddy retry_init:
3503824a1566SKashyap Desai 	retval = mpi3mr_bring_ioc_ready(mrioc);
3504824a1566SKashyap Desai 	if (retval) {
3505824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
3506824a1566SKashyap Desai 		    retval);
3507fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3508824a1566SKashyap Desai 	}
3509824a1566SKashyap Desai 
3510824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 1);
3511824a1566SKashyap Desai 	if (retval) {
3512824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to setup ISR error %d\n",
3513824a1566SKashyap Desai 		    retval);
3514fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3515824a1566SKashyap Desai 	}
3516824a1566SKashyap Desai 
3517824a1566SKashyap Desai 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3518824a1566SKashyap Desai 	if (retval) {
3519824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
3520824a1566SKashyap Desai 		    retval);
3521824a1566SKashyap Desai 		goto out_failed;
3522824a1566SKashyap Desai 	}
3523824a1566SKashyap Desai 
3524*c5758fc7SSreekanth Reddy 	mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
3525*c5758fc7SSreekanth Reddy 
3526*c5758fc7SSreekanth Reddy 	if (reset_devices)
3527*c5758fc7SSreekanth Reddy 		mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
3528*c5758fc7SSreekanth Reddy 		    MPI3MR_HOST_IOS_KDUMP);
3529*c5758fc7SSreekanth Reddy 
3530*c5758fc7SSreekanth Reddy 	mrioc->reply_sz = mrioc->facts.reply_sz;
3531fe6db615SSreekanth Reddy 
3532824a1566SKashyap Desai 	retval = mpi3mr_check_reset_dma_mask(mrioc);
3533824a1566SKashyap Desai 	if (retval) {
3534824a1566SKashyap Desai 		ioc_err(mrioc, "Resetting dma mask failed %d\n",
3535824a1566SKashyap Desai 		    retval);
3536fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3537fb9b0457SKashyap Desai 	}
3538824a1566SKashyap Desai 
3539ff9561e9SKashyap Desai 	mpi3mr_print_ioc_info(mrioc);
3540ff9561e9SKashyap Desai 
3541824a1566SKashyap Desai 	retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
3542824a1566SKashyap Desai 	if (retval) {
3543824a1566SKashyap Desai 		ioc_err(mrioc,
3544824a1566SKashyap Desai 		    "%s :Failed to allocated reply sense buffers %d\n",
3545824a1566SKashyap Desai 		    __func__, retval);
3546fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3547824a1566SKashyap Desai 	}
3548824a1566SKashyap Desai 
3549824a1566SKashyap Desai 	retval = mpi3mr_alloc_chain_bufs(mrioc);
3550824a1566SKashyap Desai 	if (retval) {
3551824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
3552824a1566SKashyap Desai 		    retval);
3553fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3554fb9b0457SKashyap Desai 	}
3555824a1566SKashyap Desai 
3556824a1566SKashyap Desai 	retval = mpi3mr_issue_iocinit(mrioc);
3557824a1566SKashyap Desai 	if (retval) {
3558824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
3559824a1566SKashyap Desai 		    retval);
3560824a1566SKashyap Desai 		goto out_failed;
3561824a1566SKashyap Desai 	}
3562824a1566SKashyap Desai 
35632ac794baSSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
35642ac794baSSreekanth Reddy 	if (retval) {
35652ac794baSSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
35662ac794baSSreekanth Reddy 		goto out_failed;
35672ac794baSSreekanth Reddy 	}
35682ac794baSSreekanth Reddy 
3569824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 0);
3570824a1566SKashyap Desai 	if (retval) {
3571824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
3572824a1566SKashyap Desai 		    retval);
3573fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3574fb9b0457SKashyap Desai 	}
3575824a1566SKashyap Desai 
3576c9566231SKashyap Desai 	retval = mpi3mr_create_op_queues(mrioc);
3577c9566231SKashyap Desai 	if (retval) {
3578c9566231SKashyap Desai 		ioc_err(mrioc, "Failed to create OpQueues error %d\n",
3579c9566231SKashyap Desai 		    retval);
3580c9566231SKashyap Desai 		goto out_failed;
3581c9566231SKashyap Desai 	}
3582c9566231SKashyap Desai 
3583e3605f65SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
358413ef29eaSKashyap Desai 	if (retval) {
3585e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events %d\n",
358613ef29eaSKashyap Desai 		    retval);
358713ef29eaSKashyap Desai 		goto out_failed;
358813ef29eaSKashyap Desai 	}
358913ef29eaSKashyap Desai 
3590fe6db615SSreekanth Reddy 	ioc_info(mrioc, "controller initialization completed successfully\n");
3591824a1566SKashyap Desai 	return retval;
3592824a1566SKashyap Desai out_failed:
3593fe6db615SSreekanth Reddy 	if (retry < 2) {
3594fe6db615SSreekanth Reddy 		retry++;
3595fe6db615SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller initialization, retry_count:%d\n",
3596fe6db615SSreekanth Reddy 		    retry);
3597fe6db615SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3598fe6db615SSreekanth Reddy 		goto retry_init;
3599fe6db615SSreekanth Reddy 	}
3600fe6db615SSreekanth Reddy out_failed_noretry:
3601fe6db615SSreekanth Reddy 	ioc_err(mrioc, "controller initialization failed\n");
3602fe6db615SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3603fe6db615SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3604fe6db615SSreekanth Reddy 	mrioc->unrecoverable = 1;
3605824a1566SKashyap Desai 	return retval;
3606824a1566SKashyap Desai }
3607824a1566SKashyap Desai 
3608c0b00a93SSreekanth Reddy /**
3609c0b00a93SSreekanth Reddy  * mpi3mr_reinit_ioc - Re-Initialize the controller
3610c0b00a93SSreekanth Reddy  * @mrioc: Adapter instance reference
3611c0b00a93SSreekanth Reddy  * @is_resume: Called from resume or reset path
3612c0b00a93SSreekanth Reddy  *
3613c0b00a93SSreekanth Reddy  * This the controller re-initialization routine, executed from
3614c0b00a93SSreekanth Reddy  * the soft reset handler or resume callback. Creates
3615c0b00a93SSreekanth Reddy  * operational reply queue pairs, allocate required memory for
3616c0b00a93SSreekanth Reddy  * reply pool, sense buffer pool, issue IOC init request to the
3617c0b00a93SSreekanth Reddy  * firmware, unmask the events and issue port enable to discover
3618c0b00a93SSreekanth Reddy  * SAS/SATA/NVMe devices and RAID volumes.
3619c0b00a93SSreekanth Reddy  *
3620c0b00a93SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3621c0b00a93SSreekanth Reddy  */
3622fe6db615SSreekanth Reddy int mpi3mr_reinit_ioc(struct mpi3mr_ioc *mrioc, u8 is_resume)
3623fe6db615SSreekanth Reddy {
3624c0b00a93SSreekanth Reddy 	int retval = 0;
3625c0b00a93SSreekanth Reddy 	u8 retry = 0;
3626c0b00a93SSreekanth Reddy 	struct mpi3_ioc_facts_data facts_data;
3627fe6db615SSreekanth Reddy 
3628c0b00a93SSreekanth Reddy retry_init:
3629c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "bringing up the controller to ready state\n");
3630c0b00a93SSreekanth Reddy 	retval = mpi3mr_bring_ioc_ready(mrioc);
3631c0b00a93SSreekanth Reddy 	if (retval) {
3632c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to bring to ready state\n");
3633c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3634c0b00a93SSreekanth Reddy 	}
3635c0b00a93SSreekanth Reddy 
3636c0b00a93SSreekanth Reddy 	if (is_resume) {
3637c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up single ISR\n");
3638c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 1);
3639c0b00a93SSreekanth Reddy 		if (retval) {
3640c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to setup ISR\n");
3641c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3642c0b00a93SSreekanth Reddy 		}
3643c0b00a93SSreekanth Reddy 	} else
3644c0b00a93SSreekanth Reddy 		mpi3mr_ioc_enable_intr(mrioc);
3645c0b00a93SSreekanth Reddy 
3646c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting ioc_facts\n");
3647c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3648c0b00a93SSreekanth Reddy 	if (retval) {
3649c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get ioc_facts\n");
3650c0b00a93SSreekanth Reddy 		goto out_failed;
3651c0b00a93SSreekanth Reddy 	}
3652c0b00a93SSreekanth Reddy 
3653*c5758fc7SSreekanth Reddy 	dprint_reset(mrioc, "validating ioc_facts\n");
3654*c5758fc7SSreekanth Reddy 	retval = mpi3mr_revalidate_factsdata(mrioc);
3655*c5758fc7SSreekanth Reddy 	if (retval) {
3656*c5758fc7SSreekanth Reddy 		ioc_err(mrioc, "failed to revalidate ioc_facts data\n");
3657*c5758fc7SSreekanth Reddy 		goto out_failed_noretry;
3658*c5758fc7SSreekanth Reddy 	}
3659c0b00a93SSreekanth Reddy 
3660c0b00a93SSreekanth Reddy 	mpi3mr_print_ioc_info(mrioc);
3661c0b00a93SSreekanth Reddy 
3662c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "sending ioc_init\n");
3663c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocinit(mrioc);
3664c0b00a93SSreekanth Reddy 	if (retval) {
3665c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to send ioc_init\n");
3666c0b00a93SSreekanth Reddy 		goto out_failed;
3667c0b00a93SSreekanth Reddy 	}
3668c0b00a93SSreekanth Reddy 
3669c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting package version\n");
3670c0b00a93SSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
3671c0b00a93SSreekanth Reddy 	if (retval) {
3672c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
3673c0b00a93SSreekanth Reddy 		goto out_failed;
3674c0b00a93SSreekanth Reddy 	}
3675c0b00a93SSreekanth Reddy 
3676c0b00a93SSreekanth Reddy 	if (is_resume) {
3677c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up multiple ISR\n");
3678c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 0);
3679c0b00a93SSreekanth Reddy 		if (retval) {
3680c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to re-setup ISR\n");
3681c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3682c0b00a93SSreekanth Reddy 		}
3683c0b00a93SSreekanth Reddy 	}
3684c0b00a93SSreekanth Reddy 
3685c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "creating operational queue pairs\n");
3686c0b00a93SSreekanth Reddy 	retval = mpi3mr_create_op_queues(mrioc);
3687c0b00a93SSreekanth Reddy 	if (retval) {
3688c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to create operational queue pairs\n");
3689c0b00a93SSreekanth Reddy 		goto out_failed;
3690c0b00a93SSreekanth Reddy 	}
3691c0b00a93SSreekanth Reddy 
3692c0b00a93SSreekanth Reddy 	if (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q) {
3693c0b00a93SSreekanth Reddy 		ioc_err(mrioc,
3694c0b00a93SSreekanth Reddy 		    "cannot create minimum number of operatioanl queues expected:%d created:%d\n",
3695c0b00a93SSreekanth Reddy 		    mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q);
3696c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3697c0b00a93SSreekanth Reddy 	}
3698c0b00a93SSreekanth Reddy 
3699c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "enabling events\n");
3700c0b00a93SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
3701c0b00a93SSreekanth Reddy 	if (retval) {
3702c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events\n");
3703c0b00a93SSreekanth Reddy 		goto out_failed;
3704c0b00a93SSreekanth Reddy 	}
3705c0b00a93SSreekanth Reddy 
3706c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "sending port enable\n");
3707c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_port_enable(mrioc, 0);
3708c0b00a93SSreekanth Reddy 	if (retval) {
3709c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to issue port enable\n");
3710c0b00a93SSreekanth Reddy 		goto out_failed;
3711c0b00a93SSreekanth Reddy 	}
3712c0b00a93SSreekanth Reddy 
3713c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "controller %s completed successfully\n",
3714c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
3715c0b00a93SSreekanth Reddy 	return retval;
3716c0b00a93SSreekanth Reddy out_failed:
3717c0b00a93SSreekanth Reddy 	if (retry < 2) {
3718c0b00a93SSreekanth Reddy 		retry++;
3719c0b00a93SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller %s, retry_count:%d\n",
3720c0b00a93SSreekanth Reddy 		    (is_resume)?"resume":"re-initialization", retry);
3721c0b00a93SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3722c0b00a93SSreekanth Reddy 		goto retry_init;
3723c0b00a93SSreekanth Reddy 	}
3724c0b00a93SSreekanth Reddy out_failed_noretry:
3725c0b00a93SSreekanth Reddy 	ioc_err(mrioc, "controller %s is failed\n",
3726c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
3727c0b00a93SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3728c0b00a93SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3729c0b00a93SSreekanth Reddy 	mrioc->unrecoverable = 1;
3730c0b00a93SSreekanth Reddy 	return retval;
3731fe6db615SSreekanth Reddy }
3732fe6db615SSreekanth Reddy 
3733824a1566SKashyap Desai /**
3734fb9b0457SKashyap Desai  * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's
3735fb9b0457SKashyap Desai  *					segments
3736fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
3737fb9b0457SKashyap Desai  * @qidx: Operational reply queue index
3738fb9b0457SKashyap Desai  *
3739fb9b0457SKashyap Desai  * Return: Nothing.
3740fb9b0457SKashyap Desai  */
3741fb9b0457SKashyap Desai static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3742fb9b0457SKashyap Desai {
3743fb9b0457SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
3744fb9b0457SKashyap Desai 	struct segments *segments;
3745fb9b0457SKashyap Desai 	int i, size;
3746fb9b0457SKashyap Desai 
3747fb9b0457SKashyap Desai 	if (!op_reply_q->q_segments)
3748fb9b0457SKashyap Desai 		return;
3749fb9b0457SKashyap Desai 
3750fb9b0457SKashyap Desai 	size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz;
3751fb9b0457SKashyap Desai 	segments = op_reply_q->q_segments;
3752fb9b0457SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++)
3753fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
3754fb9b0457SKashyap Desai }
3755fb9b0457SKashyap Desai 
3756fb9b0457SKashyap Desai /**
3757fb9b0457SKashyap Desai  * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's
3758fb9b0457SKashyap Desai  *					segments
3759fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
3760fb9b0457SKashyap Desai  * @qidx: Operational request queue index
3761fb9b0457SKashyap Desai  *
3762fb9b0457SKashyap Desai  * Return: Nothing.
3763fb9b0457SKashyap Desai  */
3764fb9b0457SKashyap Desai static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3765fb9b0457SKashyap Desai {
3766fb9b0457SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
3767fb9b0457SKashyap Desai 	struct segments *segments;
3768fb9b0457SKashyap Desai 	int i, size;
3769fb9b0457SKashyap Desai 
3770fb9b0457SKashyap Desai 	if (!op_req_q->q_segments)
3771fb9b0457SKashyap Desai 		return;
3772fb9b0457SKashyap Desai 
3773fb9b0457SKashyap Desai 	size = op_req_q->segment_qd * mrioc->facts.op_req_sz;
3774fb9b0457SKashyap Desai 	segments = op_req_q->q_segments;
3775fb9b0457SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++)
3776fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
3777fb9b0457SKashyap Desai }
3778fb9b0457SKashyap Desai 
3779fb9b0457SKashyap Desai /**
3780fb9b0457SKashyap Desai  * mpi3mr_memset_buffers - memset memory for a controller
3781fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
3782fb9b0457SKashyap Desai  *
3783fb9b0457SKashyap Desai  * clear all the memory allocated for a controller, typically
3784fb9b0457SKashyap Desai  * called post reset to reuse the memory allocated during the
3785fb9b0457SKashyap Desai  * controller init.
3786fb9b0457SKashyap Desai  *
3787fb9b0457SKashyap Desai  * Return: Nothing.
3788fb9b0457SKashyap Desai  */
37890da66348SKashyap Desai void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)
3790fb9b0457SKashyap Desai {
3791fb9b0457SKashyap Desai 	u16 i;
3792fb9b0457SKashyap Desai 
3793fe6db615SSreekanth Reddy 	mrioc->change_count = 0;
3794fe6db615SSreekanth Reddy 	if (mrioc->admin_req_base)
3795fb9b0457SKashyap Desai 		memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz);
3796fe6db615SSreekanth Reddy 	if (mrioc->admin_reply_base)
3797fb9b0457SKashyap Desai 		memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz);
3798fb9b0457SKashyap Desai 
3799fe6db615SSreekanth Reddy 	if (mrioc->init_cmds.reply) {
3800fb9b0457SKashyap Desai 		memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply));
3801e844adb1SKashyap Desai 		memset(mrioc->host_tm_cmds.reply, 0,
3802e844adb1SKashyap Desai 		    sizeof(*mrioc->host_tm_cmds.reply));
3803fb9b0457SKashyap Desai 		for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
3804fb9b0457SKashyap Desai 			memset(mrioc->dev_rmhs_cmds[i].reply, 0,
3805fb9b0457SKashyap Desai 			    sizeof(*mrioc->dev_rmhs_cmds[i].reply));
3806fb9b0457SKashyap Desai 		memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
3807fb9b0457SKashyap Desai 		memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
3808fe6db615SSreekanth Reddy 	}
3809fb9b0457SKashyap Desai 
3810fb9b0457SKashyap Desai 	for (i = 0; i < mrioc->num_queues; i++) {
3811fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].qid = 0;
3812fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ci = 0;
3813fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].num_replies = 0;
3814fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ephase = 0;
3815463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
3816463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0);
3817fb9b0457SKashyap Desai 		mpi3mr_memset_op_reply_q_buffers(mrioc, i);
3818fb9b0457SKashyap Desai 
3819fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].ci = 0;
3820fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].pi = 0;
3821fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].num_requests = 0;
3822fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].qid = 0;
3823fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].reply_qid = 0;
3824fb9b0457SKashyap Desai 		spin_lock_init(&mrioc->req_qinfo[i].q_lock);
3825fb9b0457SKashyap Desai 		mpi3mr_memset_op_req_q_buffers(mrioc, i);
3826fb9b0457SKashyap Desai 	}
3827fb9b0457SKashyap Desai }
3828fb9b0457SKashyap Desai 
3829fb9b0457SKashyap Desai /**
3830824a1566SKashyap Desai  * mpi3mr_free_mem - Free memory allocated for a controller
3831824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3832824a1566SKashyap Desai  *
3833824a1566SKashyap Desai  * Free all the memory allocated for a controller.
3834824a1566SKashyap Desai  *
3835824a1566SKashyap Desai  * Return: Nothing.
3836824a1566SKashyap Desai  */
3837fe6db615SSreekanth Reddy void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
3838824a1566SKashyap Desai {
3839824a1566SKashyap Desai 	u16 i;
3840824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info;
3841824a1566SKashyap Desai 
3842824a1566SKashyap Desai 	if (mrioc->sense_buf_pool) {
3843824a1566SKashyap Desai 		if (mrioc->sense_buf)
3844824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
3845824a1566SKashyap Desai 			    mrioc->sense_buf_dma);
3846824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_pool);
3847824a1566SKashyap Desai 		mrioc->sense_buf = NULL;
3848824a1566SKashyap Desai 		mrioc->sense_buf_pool = NULL;
3849824a1566SKashyap Desai 	}
3850824a1566SKashyap Desai 	if (mrioc->sense_buf_q_pool) {
3851824a1566SKashyap Desai 		if (mrioc->sense_buf_q)
3852824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_q_pool,
3853824a1566SKashyap Desai 			    mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
3854824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_q_pool);
3855824a1566SKashyap Desai 		mrioc->sense_buf_q = NULL;
3856824a1566SKashyap Desai 		mrioc->sense_buf_q_pool = NULL;
3857824a1566SKashyap Desai 	}
3858824a1566SKashyap Desai 
3859824a1566SKashyap Desai 	if (mrioc->reply_buf_pool) {
3860824a1566SKashyap Desai 		if (mrioc->reply_buf)
3861824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
3862824a1566SKashyap Desai 			    mrioc->reply_buf_dma);
3863824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_buf_pool);
3864824a1566SKashyap Desai 		mrioc->reply_buf = NULL;
3865824a1566SKashyap Desai 		mrioc->reply_buf_pool = NULL;
3866824a1566SKashyap Desai 	}
3867824a1566SKashyap Desai 	if (mrioc->reply_free_q_pool) {
3868824a1566SKashyap Desai 		if (mrioc->reply_free_q)
3869824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_free_q_pool,
3870824a1566SKashyap Desai 			    mrioc->reply_free_q, mrioc->reply_free_q_dma);
3871824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_free_q_pool);
3872824a1566SKashyap Desai 		mrioc->reply_free_q = NULL;
3873824a1566SKashyap Desai 		mrioc->reply_free_q_pool = NULL;
3874824a1566SKashyap Desai 	}
3875824a1566SKashyap Desai 
3876c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_req_q; i++)
3877c9566231SKashyap Desai 		mpi3mr_free_op_req_q_segments(mrioc, i);
3878c9566231SKashyap Desai 
3879c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_reply_q; i++)
3880c9566231SKashyap Desai 		mpi3mr_free_op_reply_q_segments(mrioc, i);
3881c9566231SKashyap Desai 
3882824a1566SKashyap Desai 	for (i = 0; i < mrioc->intr_info_count; i++) {
3883824a1566SKashyap Desai 		intr_info = mrioc->intr_info + i;
3884824a1566SKashyap Desai 		intr_info->op_reply_q = NULL;
3885824a1566SKashyap Desai 	}
3886824a1566SKashyap Desai 
3887824a1566SKashyap Desai 	kfree(mrioc->req_qinfo);
3888824a1566SKashyap Desai 	mrioc->req_qinfo = NULL;
3889824a1566SKashyap Desai 	mrioc->num_op_req_q = 0;
3890824a1566SKashyap Desai 
3891824a1566SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
3892824a1566SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
3893824a1566SKashyap Desai 	mrioc->num_op_reply_q = 0;
3894824a1566SKashyap Desai 
3895824a1566SKashyap Desai 	kfree(mrioc->init_cmds.reply);
3896824a1566SKashyap Desai 	mrioc->init_cmds.reply = NULL;
3897824a1566SKashyap Desai 
3898e844adb1SKashyap Desai 	kfree(mrioc->host_tm_cmds.reply);
3899e844adb1SKashyap Desai 	mrioc->host_tm_cmds.reply = NULL;
3900e844adb1SKashyap Desai 
3901e844adb1SKashyap Desai 	kfree(mrioc->removepend_bitmap);
3902e844adb1SKashyap Desai 	mrioc->removepend_bitmap = NULL;
3903e844adb1SKashyap Desai 
3904e844adb1SKashyap Desai 	kfree(mrioc->devrem_bitmap);
3905e844adb1SKashyap Desai 	mrioc->devrem_bitmap = NULL;
3906e844adb1SKashyap Desai 
3907824a1566SKashyap Desai 	kfree(mrioc->chain_bitmap);
3908824a1566SKashyap Desai 	mrioc->chain_bitmap = NULL;
3909824a1566SKashyap Desai 
391013ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
391113ef29eaSKashyap Desai 		kfree(mrioc->dev_rmhs_cmds[i].reply);
391213ef29eaSKashyap Desai 		mrioc->dev_rmhs_cmds[i].reply = NULL;
391313ef29eaSKashyap Desai 	}
391413ef29eaSKashyap Desai 
3915824a1566SKashyap Desai 	if (mrioc->chain_buf_pool) {
3916824a1566SKashyap Desai 		for (i = 0; i < mrioc->chain_buf_count; i++) {
3917824a1566SKashyap Desai 			if (mrioc->chain_sgl_list[i].addr) {
3918824a1566SKashyap Desai 				dma_pool_free(mrioc->chain_buf_pool,
3919824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].addr,
3920824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].dma_addr);
3921824a1566SKashyap Desai 				mrioc->chain_sgl_list[i].addr = NULL;
3922824a1566SKashyap Desai 			}
3923824a1566SKashyap Desai 		}
3924824a1566SKashyap Desai 		dma_pool_destroy(mrioc->chain_buf_pool);
3925824a1566SKashyap Desai 		mrioc->chain_buf_pool = NULL;
3926824a1566SKashyap Desai 	}
3927824a1566SKashyap Desai 
3928824a1566SKashyap Desai 	kfree(mrioc->chain_sgl_list);
3929824a1566SKashyap Desai 	mrioc->chain_sgl_list = NULL;
3930824a1566SKashyap Desai 
3931824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
3932824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
3933824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
3934824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
3935824a1566SKashyap Desai 	}
3936824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
3937824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
3938824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
3939824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
3940824a1566SKashyap Desai 	}
3941824a1566SKashyap Desai }
3942824a1566SKashyap Desai 
3943824a1566SKashyap Desai /**
3944824a1566SKashyap Desai  * mpi3mr_issue_ioc_shutdown - shutdown controller
3945824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3946824a1566SKashyap Desai  *
3947824a1566SKashyap Desai  * Send shutodwn notification to the controller and wait for the
3948824a1566SKashyap Desai  * shutdown_timeout for it to be completed.
3949824a1566SKashyap Desai  *
3950824a1566SKashyap Desai  * Return: Nothing.
3951824a1566SKashyap Desai  */
3952824a1566SKashyap Desai static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
3953824a1566SKashyap Desai {
3954824a1566SKashyap Desai 	u32 ioc_config, ioc_status;
3955824a1566SKashyap Desai 	u8 retval = 1;
3956824a1566SKashyap Desai 	u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
3957824a1566SKashyap Desai 
3958824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing shutdown Notification\n");
3959824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
3960824a1566SKashyap Desai 		ioc_warn(mrioc,
3961824a1566SKashyap Desai 		    "IOC is unrecoverable shutdown is not issued\n");
3962824a1566SKashyap Desai 		return;
3963824a1566SKashyap Desai 	}
3964824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3965824a1566SKashyap Desai 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
3966824a1566SKashyap Desai 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
3967824a1566SKashyap Desai 		ioc_info(mrioc, "shutdown already in progress\n");
3968824a1566SKashyap Desai 		return;
3969824a1566SKashyap Desai 	}
3970824a1566SKashyap Desai 
3971824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
3972824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
3973ec5ebd2cSSreekanth Reddy 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
3974824a1566SKashyap Desai 
3975824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
3976824a1566SKashyap Desai 
3977824a1566SKashyap Desai 	if (mrioc->facts.shutdown_timeout)
3978824a1566SKashyap Desai 		timeout = mrioc->facts.shutdown_timeout * 10;
3979824a1566SKashyap Desai 
3980824a1566SKashyap Desai 	do {
3981824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3982824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
3983824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
3984824a1566SKashyap Desai 			retval = 0;
3985824a1566SKashyap Desai 			break;
3986824a1566SKashyap Desai 		}
3987824a1566SKashyap Desai 		msleep(100);
3988824a1566SKashyap Desai 	} while (--timeout);
3989824a1566SKashyap Desai 
3990824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3991824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
3992824a1566SKashyap Desai 
3993824a1566SKashyap Desai 	if (retval) {
3994824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
3995824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
3996824a1566SKashyap Desai 			ioc_warn(mrioc,
3997824a1566SKashyap Desai 			    "shutdown still in progress after timeout\n");
3998824a1566SKashyap Desai 	}
3999824a1566SKashyap Desai 
4000824a1566SKashyap Desai 	ioc_info(mrioc,
4001824a1566SKashyap Desai 	    "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
4002824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status,
4003824a1566SKashyap Desai 	    ioc_config);
4004824a1566SKashyap Desai }
4005824a1566SKashyap Desai 
4006824a1566SKashyap Desai /**
4007824a1566SKashyap Desai  * mpi3mr_cleanup_ioc - Cleanup controller
4008824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4009fe6db615SSreekanth Reddy 
4010824a1566SKashyap Desai  * controller cleanup handler, Message unit reset or soft reset
4011fe6db615SSreekanth Reddy  * and shutdown notification is issued to the controller.
4012824a1566SKashyap Desai  *
4013824a1566SKashyap Desai  * Return: Nothing.
4014824a1566SKashyap Desai  */
4015fe6db615SSreekanth Reddy void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)
4016824a1566SKashyap Desai {
4017824a1566SKashyap Desai 	enum mpi3mr_iocstate ioc_state;
4018824a1566SKashyap Desai 
4019fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "cleaning up the controller\n");
4020824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4021824a1566SKashyap Desai 
4022824a1566SKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
4023824a1566SKashyap Desai 
4024824a1566SKashyap Desai 	if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
4025824a1566SKashyap Desai 	    (ioc_state == MRIOC_STATE_READY)) {
4026824a1566SKashyap Desai 		if (mpi3mr_issue_and_process_mur(mrioc,
4027824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
4028824a1566SKashyap Desai 			mpi3mr_issue_reset(mrioc,
4029824a1566SKashyap Desai 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
4030824a1566SKashyap Desai 			    MPI3MR_RESET_FROM_MUR_FAILURE);
4031824a1566SKashyap Desai 		mpi3mr_issue_ioc_shutdown(mrioc);
4032824a1566SKashyap Desai 	}
4033fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "controller cleanup completed\n");
4034fb9b0457SKashyap Desai }
4035fb9b0457SKashyap Desai 
4036fb9b0457SKashyap Desai /**
4037fb9b0457SKashyap Desai  * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
4038fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4039fb9b0457SKashyap Desai  * @cmdptr: Internal command tracker
4040fb9b0457SKashyap Desai  *
4041fb9b0457SKashyap Desai  * Complete an internal driver commands with state indicating it
4042fb9b0457SKashyap Desai  * is completed due to reset.
4043fb9b0457SKashyap Desai  *
4044fb9b0457SKashyap Desai  * Return: Nothing.
4045fb9b0457SKashyap Desai  */
4046fb9b0457SKashyap Desai static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc,
4047fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr)
4048fb9b0457SKashyap Desai {
4049fb9b0457SKashyap Desai 	if (cmdptr->state & MPI3MR_CMD_PENDING) {
4050fb9b0457SKashyap Desai 		cmdptr->state |= MPI3MR_CMD_RESET;
4051fb9b0457SKashyap Desai 		cmdptr->state &= ~MPI3MR_CMD_PENDING;
4052fb9b0457SKashyap Desai 		if (cmdptr->is_waiting) {
4053fb9b0457SKashyap Desai 			complete(&cmdptr->done);
4054fb9b0457SKashyap Desai 			cmdptr->is_waiting = 0;
4055fb9b0457SKashyap Desai 		} else if (cmdptr->callback)
4056fb9b0457SKashyap Desai 			cmdptr->callback(mrioc, cmdptr);
4057fb9b0457SKashyap Desai 	}
4058fb9b0457SKashyap Desai }
4059fb9b0457SKashyap Desai 
4060fb9b0457SKashyap Desai /**
4061fb9b0457SKashyap Desai  * mpi3mr_flush_drv_cmds - Flush internaldriver commands
4062fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4063fb9b0457SKashyap Desai  *
4064fb9b0457SKashyap Desai  * Flush all internal driver commands post reset
4065fb9b0457SKashyap Desai  *
4066fb9b0457SKashyap Desai  * Return: Nothing.
4067fb9b0457SKashyap Desai  */
4068fb9b0457SKashyap Desai static void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc)
4069fb9b0457SKashyap Desai {
4070fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr;
4071fb9b0457SKashyap Desai 	u8 i;
4072fb9b0457SKashyap Desai 
4073fb9b0457SKashyap Desai 	cmdptr = &mrioc->init_cmds;
4074fb9b0457SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4075e844adb1SKashyap Desai 	cmdptr = &mrioc->host_tm_cmds;
4076e844adb1SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4077fb9b0457SKashyap Desai 
4078fb9b0457SKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
4079fb9b0457SKashyap Desai 		cmdptr = &mrioc->dev_rmhs_cmds[i];
4080fb9b0457SKashyap Desai 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4081fb9b0457SKashyap Desai 	}
4082fb9b0457SKashyap Desai }
4083fb9b0457SKashyap Desai 
4084fb9b0457SKashyap Desai /**
4085824a1566SKashyap Desai  * mpi3mr_soft_reset_handler - Reset the controller
4086824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4087824a1566SKashyap Desai  * @reset_reason: Reset reason code
4088824a1566SKashyap Desai  * @snapdump: Flag to generate snapdump in firmware or not
4089824a1566SKashyap Desai  *
4090fb9b0457SKashyap Desai  * This is an handler for recovering controller by issuing soft
4091fb9b0457SKashyap Desai  * reset are diag fault reset.  This is a blocking function and
4092fb9b0457SKashyap Desai  * when one reset is executed if any other resets they will be
4093fb9b0457SKashyap Desai  * blocked. All IOCTLs/IO will be blocked during the reset. If
4094fb9b0457SKashyap Desai  * controller reset is successful then the controller will be
4095fb9b0457SKashyap Desai  * reinitalized, otherwise the controller will be marked as not
4096fb9b0457SKashyap Desai  * recoverable
4097fb9b0457SKashyap Desai  *
4098fb9b0457SKashyap Desai  * In snapdump bit is set, the controller is issued with diag
4099fb9b0457SKashyap Desai  * fault reset so that the firmware can create a snap dump and
4100fb9b0457SKashyap Desai  * post that the firmware will result in F000 fault and the
4101fb9b0457SKashyap Desai  * driver will issue soft reset to recover from that.
4102824a1566SKashyap Desai  *
4103824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
4104824a1566SKashyap Desai  */
4105824a1566SKashyap Desai int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
4106824a1566SKashyap Desai 	u32 reset_reason, u8 snapdump)
4107824a1566SKashyap Desai {
4108fb9b0457SKashyap Desai 	int retval = 0, i;
4109fb9b0457SKashyap Desai 	unsigned long flags;
4110fb9b0457SKashyap Desai 	u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
4111fb9b0457SKashyap Desai 
4112b64845a7SSreekanth Reddy 	/* Block the reset handler until diag save in progress*/
4113b64845a7SSreekanth Reddy 	dprint_reset(mrioc,
4114b64845a7SSreekanth Reddy 	    "soft_reset_handler: check and block on diagsave_timeout(%d)\n",
4115b64845a7SSreekanth Reddy 	    mrioc->diagsave_timeout);
4116b64845a7SSreekanth Reddy 	while (mrioc->diagsave_timeout)
4117b64845a7SSreekanth Reddy 		ssleep(1);
4118fb9b0457SKashyap Desai 	/*
4119fb9b0457SKashyap Desai 	 * Block new resets until the currently executing one is finished and
4120fb9b0457SKashyap Desai 	 * return the status of the existing reset for all blocked resets
4121fb9b0457SKashyap Desai 	 */
4122b64845a7SSreekanth Reddy 	dprint_reset(mrioc, "soft_reset_handler: acquiring reset_mutex\n");
4123fb9b0457SKashyap Desai 	if (!mutex_trylock(&mrioc->reset_mutex)) {
4124b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4125b64845a7SSreekanth Reddy 		    "controller reset triggered by %s is blocked due to another reset in progress\n",
4126b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4127b64845a7SSreekanth Reddy 		do {
4128b64845a7SSreekanth Reddy 			ssleep(1);
4129b64845a7SSreekanth Reddy 		} while (mrioc->reset_in_progress == 1);
4130b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4131b64845a7SSreekanth Reddy 		    "returning previous reset result(%d) for the reset triggered by %s\n",
4132b64845a7SSreekanth Reddy 		    mrioc->prev_reset_result,
4133b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4134b64845a7SSreekanth Reddy 		return mrioc->prev_reset_result;
4135fb9b0457SKashyap Desai 	}
4136b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is triggered by %s\n",
4137b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason));
4138b64845a7SSreekanth Reddy 
4139fb9b0457SKashyap Desai 	mrioc->reset_in_progress = 1;
4140b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = -1;
4141fb9b0457SKashyap Desai 
4142fb9b0457SKashyap Desai 	if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
4143b64845a7SSreekanth Reddy 	    (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) &&
4144fb9b0457SKashyap Desai 	    (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
4145fb9b0457SKashyap Desai 		for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4146fb9b0457SKashyap Desai 			mrioc->event_masks[i] = -1;
4147fb9b0457SKashyap Desai 
4148b64845a7SSreekanth Reddy 		dprint_reset(mrioc, "soft_reset_handler: masking events\n");
4149b64845a7SSreekanth Reddy 		mpi3mr_issue_event_notification(mrioc);
4150fb9b0457SKashyap Desai 	}
4151fb9b0457SKashyap Desai 
415244dc724fSKashyap Desai 	mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT);
415344dc724fSKashyap Desai 
4154fb9b0457SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4155fb9b0457SKashyap Desai 
4156fb9b0457SKashyap Desai 	if (snapdump) {
4157fb9b0457SKashyap Desai 		mpi3mr_set_diagsave(mrioc);
4158fb9b0457SKashyap Desai 		retval = mpi3mr_issue_reset(mrioc,
4159fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4160fb9b0457SKashyap Desai 		if (!retval) {
4161fb9b0457SKashyap Desai 			do {
4162fb9b0457SKashyap Desai 				host_diagnostic =
4163fb9b0457SKashyap Desai 				    readl(&mrioc->sysif_regs->host_diagnostic);
4164fb9b0457SKashyap Desai 				if (!(host_diagnostic &
4165fb9b0457SKashyap Desai 				    MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
4166fb9b0457SKashyap Desai 					break;
4167fb9b0457SKashyap Desai 				msleep(100);
4168fb9b0457SKashyap Desai 			} while (--timeout);
4169fb9b0457SKashyap Desai 		}
4170fb9b0457SKashyap Desai 	}
4171fb9b0457SKashyap Desai 
4172fb9b0457SKashyap Desai 	retval = mpi3mr_issue_reset(mrioc,
4173fb9b0457SKashyap Desai 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
4174fb9b0457SKashyap Desai 	if (retval) {
4175fb9b0457SKashyap Desai 		ioc_err(mrioc, "Failed to issue soft reset to the ioc\n");
4176fb9b0457SKashyap Desai 		goto out;
4177fb9b0457SKashyap Desai 	}
4178fb9b0457SKashyap Desai 
4179fb9b0457SKashyap Desai 	mpi3mr_flush_delayed_rmhs_list(mrioc);
4180fb9b0457SKashyap Desai 	mpi3mr_flush_drv_cmds(mrioc);
4181fb9b0457SKashyap Desai 	memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
4182fb9b0457SKashyap Desai 	memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
4183fb9b0457SKashyap Desai 	mpi3mr_cleanup_fwevt_list(mrioc);
4184fb9b0457SKashyap Desai 	mpi3mr_flush_host_io(mrioc);
4185fb9b0457SKashyap Desai 	mpi3mr_invalidate_devhandles(mrioc);
4186fb9b0457SKashyap Desai 	mpi3mr_memset_buffers(mrioc);
4187fe6db615SSreekanth Reddy 	retval = mpi3mr_reinit_ioc(mrioc, 0);
4188fb9b0457SKashyap Desai 	if (retval) {
4189fb9b0457SKashyap Desai 		pr_err(IOCNAME "reinit after soft reset failed: reason %d\n",
4190fb9b0457SKashyap Desai 		    mrioc->name, reset_reason);
4191fb9b0457SKashyap Desai 		goto out;
4192fb9b0457SKashyap Desai 	}
4193fb9b0457SKashyap Desai 	ssleep(10);
4194fb9b0457SKashyap Desai 
4195fb9b0457SKashyap Desai out:
4196fb9b0457SKashyap Desai 	if (!retval) {
4197b64845a7SSreekanth Reddy 		mrioc->diagsave_timeout = 0;
4198fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
4199fb9b0457SKashyap Desai 		mpi3mr_rfresh_tgtdevs(mrioc);
420054dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
4201fb9b0457SKashyap Desai 		spin_lock_irqsave(&mrioc->watchdog_lock, flags);
4202fb9b0457SKashyap Desai 		if (mrioc->watchdog_work_q)
4203fb9b0457SKashyap Desai 			queue_delayed_work(mrioc->watchdog_work_q,
4204fb9b0457SKashyap Desai 			    &mrioc->watchdog_work,
4205fb9b0457SKashyap Desai 			    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
4206fb9b0457SKashyap Desai 		spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
4207fb9b0457SKashyap Desai 	} else {
4208fb9b0457SKashyap Desai 		mpi3mr_issue_reset(mrioc,
4209fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4210fb9b0457SKashyap Desai 		mrioc->unrecoverable = 1;
4211fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
4212fb9b0457SKashyap Desai 		retval = -1;
4213fb9b0457SKashyap Desai 	}
4214b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = retval;
4215fb9b0457SKashyap Desai 	mutex_unlock(&mrioc->reset_mutex);
4216b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is %s\n",
4217b64845a7SSreekanth Reddy 	    ((retval == 0) ? "successful" : "failed"));
4218fb9b0457SKashyap Desai 	return retval;
4219824a1566SKashyap Desai }
4220