xref: /linux/drivers/scsi/mpi3mr/mpi3mr_fw.c (revision 32d457d5a2af9bf5ddbe28297eabf1fc93451665)
1824a1566SKashyap Desai // SPDX-License-Identifier: GPL-2.0-or-later
2824a1566SKashyap Desai /*
3824a1566SKashyap Desai  * Driver for Broadcom MPI3 Storage Controllers
4824a1566SKashyap Desai  *
521401408SSreekanth Reddy  * Copyright (C) 2017-2022 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);
16c5758fc7SSreekanth Reddy static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
17c5758fc7SSreekanth Reddy 	struct mpi3_ioc_facts_data *facts_data);
1843ca1100SSumit Saxena static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc,
1943ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd);
2059bd9cfeSSreekanth Reddy 
21afd3a579SSreekanth Reddy static int poll_queues;
22afd3a579SSreekanth Reddy module_param(poll_queues, int, 0444);
23afd3a579SSreekanth Reddy MODULE_PARM_DESC(poll_queues, "Number of queues for io_uring poll mode. (Range 1 - 126)");
24afd3a579SSreekanth Reddy 
25824a1566SKashyap Desai #if defined(writeq) && defined(CONFIG_64BIT)
26824a1566SKashyap Desai static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
27824a1566SKashyap Desai {
28824a1566SKashyap Desai 	writeq(b, addr);
29824a1566SKashyap Desai }
30824a1566SKashyap Desai #else
31824a1566SKashyap Desai static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
32824a1566SKashyap Desai {
33824a1566SKashyap Desai 	__u64 data_out = b;
34824a1566SKashyap Desai 
35824a1566SKashyap Desai 	writel((u32)(data_out), addr);
36824a1566SKashyap Desai 	writel((u32)(data_out >> 32), (addr + 4));
37824a1566SKashyap Desai }
38824a1566SKashyap Desai #endif
39824a1566SKashyap Desai 
40023ab2a9SKashyap Desai static inline bool
41023ab2a9SKashyap Desai mpi3mr_check_req_qfull(struct op_req_qinfo *op_req_q)
42023ab2a9SKashyap Desai {
43023ab2a9SKashyap Desai 	u16 pi, ci, max_entries;
44023ab2a9SKashyap Desai 	bool is_qfull = false;
45023ab2a9SKashyap Desai 
46023ab2a9SKashyap Desai 	pi = op_req_q->pi;
47023ab2a9SKashyap Desai 	ci = READ_ONCE(op_req_q->ci);
48023ab2a9SKashyap Desai 	max_entries = op_req_q->num_requests;
49023ab2a9SKashyap Desai 
50023ab2a9SKashyap Desai 	if ((ci == (pi + 1)) || ((!ci) && (pi == (max_entries - 1))))
51023ab2a9SKashyap Desai 		is_qfull = true;
52023ab2a9SKashyap Desai 
53023ab2a9SKashyap Desai 	return is_qfull;
54023ab2a9SKashyap Desai }
55023ab2a9SKashyap Desai 
56824a1566SKashyap Desai static void mpi3mr_sync_irqs(struct mpi3mr_ioc *mrioc)
57824a1566SKashyap Desai {
58824a1566SKashyap Desai 	u16 i, max_vectors;
59824a1566SKashyap Desai 
60824a1566SKashyap Desai 	max_vectors = mrioc->intr_info_count;
61824a1566SKashyap Desai 
62824a1566SKashyap Desai 	for (i = 0; i < max_vectors; i++)
63824a1566SKashyap Desai 		synchronize_irq(pci_irq_vector(mrioc->pdev, i));
64824a1566SKashyap Desai }
65824a1566SKashyap Desai 
66824a1566SKashyap Desai void mpi3mr_ioc_disable_intr(struct mpi3mr_ioc *mrioc)
67824a1566SKashyap Desai {
68824a1566SKashyap Desai 	mrioc->intr_enabled = 0;
69824a1566SKashyap Desai 	mpi3mr_sync_irqs(mrioc);
70824a1566SKashyap Desai }
71824a1566SKashyap Desai 
72824a1566SKashyap Desai void mpi3mr_ioc_enable_intr(struct mpi3mr_ioc *mrioc)
73824a1566SKashyap Desai {
74824a1566SKashyap Desai 	mrioc->intr_enabled = 1;
75824a1566SKashyap Desai }
76824a1566SKashyap Desai 
77824a1566SKashyap Desai static void mpi3mr_cleanup_isr(struct mpi3mr_ioc *mrioc)
78824a1566SKashyap Desai {
79824a1566SKashyap Desai 	u16 i;
80824a1566SKashyap Desai 
81824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
82824a1566SKashyap Desai 
83824a1566SKashyap Desai 	if (!mrioc->intr_info)
84824a1566SKashyap Desai 		return;
85824a1566SKashyap Desai 
86824a1566SKashyap Desai 	for (i = 0; i < mrioc->intr_info_count; i++)
87824a1566SKashyap Desai 		free_irq(pci_irq_vector(mrioc->pdev, i),
88824a1566SKashyap Desai 		    (mrioc->intr_info + i));
89824a1566SKashyap Desai 
90824a1566SKashyap Desai 	kfree(mrioc->intr_info);
91824a1566SKashyap Desai 	mrioc->intr_info = NULL;
92824a1566SKashyap Desai 	mrioc->intr_info_count = 0;
93fe6db615SSreekanth Reddy 	mrioc->is_intr_info_set = false;
94824a1566SKashyap Desai 	pci_free_irq_vectors(mrioc->pdev);
95824a1566SKashyap Desai }
96824a1566SKashyap Desai 
97824a1566SKashyap Desai void mpi3mr_add_sg_single(void *paddr, u8 flags, u32 length,
98824a1566SKashyap Desai 	dma_addr_t dma_addr)
99824a1566SKashyap Desai {
100824a1566SKashyap Desai 	struct mpi3_sge_common *sgel = paddr;
101824a1566SKashyap Desai 
102824a1566SKashyap Desai 	sgel->flags = flags;
103824a1566SKashyap Desai 	sgel->length = cpu_to_le32(length);
104824a1566SKashyap Desai 	sgel->address = cpu_to_le64(dma_addr);
105824a1566SKashyap Desai }
106824a1566SKashyap Desai 
107824a1566SKashyap Desai void mpi3mr_build_zero_len_sge(void *paddr)
108824a1566SKashyap Desai {
109824a1566SKashyap Desai 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
110824a1566SKashyap Desai 
111824a1566SKashyap Desai 	mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1);
112824a1566SKashyap Desai }
113824a1566SKashyap Desai 
114824a1566SKashyap Desai void *mpi3mr_get_reply_virt_addr(struct mpi3mr_ioc *mrioc,
115824a1566SKashyap Desai 	dma_addr_t phys_addr)
116824a1566SKashyap Desai {
117824a1566SKashyap Desai 	if (!phys_addr)
118824a1566SKashyap Desai 		return NULL;
119824a1566SKashyap Desai 
120824a1566SKashyap Desai 	if ((phys_addr < mrioc->reply_buf_dma) ||
121824a1566SKashyap Desai 	    (phys_addr > mrioc->reply_buf_dma_max_address))
122824a1566SKashyap Desai 		return NULL;
123824a1566SKashyap Desai 
124824a1566SKashyap Desai 	return mrioc->reply_buf + (phys_addr - mrioc->reply_buf_dma);
125824a1566SKashyap Desai }
126824a1566SKashyap Desai 
127824a1566SKashyap Desai void *mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_ioc *mrioc,
128824a1566SKashyap Desai 	dma_addr_t phys_addr)
129824a1566SKashyap Desai {
130824a1566SKashyap Desai 	if (!phys_addr)
131824a1566SKashyap Desai 		return NULL;
132824a1566SKashyap Desai 
133824a1566SKashyap Desai 	return mrioc->sense_buf + (phys_addr - mrioc->sense_buf_dma);
134824a1566SKashyap Desai }
135824a1566SKashyap Desai 
136824a1566SKashyap Desai static void mpi3mr_repost_reply_buf(struct mpi3mr_ioc *mrioc,
137824a1566SKashyap Desai 	u64 reply_dma)
138824a1566SKashyap Desai {
139824a1566SKashyap Desai 	u32 old_idx = 0;
140a83ec831SSreekanth Reddy 	unsigned long flags;
141824a1566SKashyap Desai 
142a83ec831SSreekanth Reddy 	spin_lock_irqsave(&mrioc->reply_free_queue_lock, flags);
143824a1566SKashyap Desai 	old_idx  =  mrioc->reply_free_queue_host_index;
144824a1566SKashyap Desai 	mrioc->reply_free_queue_host_index = (
145824a1566SKashyap Desai 	    (mrioc->reply_free_queue_host_index ==
146824a1566SKashyap Desai 	    (mrioc->reply_free_qsz - 1)) ? 0 :
147824a1566SKashyap Desai 	    (mrioc->reply_free_queue_host_index + 1));
148824a1566SKashyap Desai 	mrioc->reply_free_q[old_idx] = cpu_to_le64(reply_dma);
149824a1566SKashyap Desai 	writel(mrioc->reply_free_queue_host_index,
150824a1566SKashyap Desai 	    &mrioc->sysif_regs->reply_free_host_index);
151a83ec831SSreekanth Reddy 	spin_unlock_irqrestore(&mrioc->reply_free_queue_lock, flags);
152824a1566SKashyap Desai }
153824a1566SKashyap Desai 
154824a1566SKashyap Desai void mpi3mr_repost_sense_buf(struct mpi3mr_ioc *mrioc,
155824a1566SKashyap Desai 	u64 sense_buf_dma)
156824a1566SKashyap Desai {
157824a1566SKashyap Desai 	u32 old_idx = 0;
158a83ec831SSreekanth Reddy 	unsigned long flags;
159824a1566SKashyap Desai 
160a83ec831SSreekanth Reddy 	spin_lock_irqsave(&mrioc->sbq_lock, flags);
161824a1566SKashyap Desai 	old_idx  =  mrioc->sbq_host_index;
162824a1566SKashyap Desai 	mrioc->sbq_host_index = ((mrioc->sbq_host_index ==
163824a1566SKashyap Desai 	    (mrioc->sense_buf_q_sz - 1)) ? 0 :
164824a1566SKashyap Desai 	    (mrioc->sbq_host_index + 1));
165824a1566SKashyap Desai 	mrioc->sense_buf_q[old_idx] = cpu_to_le64(sense_buf_dma);
166824a1566SKashyap Desai 	writel(mrioc->sbq_host_index,
167824a1566SKashyap Desai 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
168a83ec831SSreekanth Reddy 	spin_unlock_irqrestore(&mrioc->sbq_lock, flags);
169824a1566SKashyap Desai }
170824a1566SKashyap Desai 
1719fc4abfeSKashyap Desai static void mpi3mr_print_event_data(struct mpi3mr_ioc *mrioc,
1729fc4abfeSKashyap Desai 	struct mpi3_event_notification_reply *event_reply)
1739fc4abfeSKashyap Desai {
1749fc4abfeSKashyap Desai 	char *desc = NULL;
1759fc4abfeSKashyap Desai 	u16 event;
1769fc4abfeSKashyap Desai 
1779fc4abfeSKashyap Desai 	event = event_reply->event;
1789fc4abfeSKashyap Desai 
1799fc4abfeSKashyap Desai 	switch (event) {
1809fc4abfeSKashyap Desai 	case MPI3_EVENT_LOG_DATA:
1819fc4abfeSKashyap Desai 		desc = "Log Data";
1829fc4abfeSKashyap Desai 		break;
1839fc4abfeSKashyap Desai 	case MPI3_EVENT_CHANGE:
1849fc4abfeSKashyap Desai 		desc = "Event Change";
1859fc4abfeSKashyap Desai 		break;
1869fc4abfeSKashyap Desai 	case MPI3_EVENT_GPIO_INTERRUPT:
1879fc4abfeSKashyap Desai 		desc = "GPIO Interrupt";
1889fc4abfeSKashyap Desai 		break;
1899fc4abfeSKashyap Desai 	case MPI3_EVENT_CABLE_MGMT:
1909fc4abfeSKashyap Desai 		desc = "Cable Management";
1919fc4abfeSKashyap Desai 		break;
1929fc4abfeSKashyap Desai 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
1939fc4abfeSKashyap Desai 		desc = "Energy Pack Change";
1949fc4abfeSKashyap Desai 		break;
1959fc4abfeSKashyap Desai 	case MPI3_EVENT_DEVICE_ADDED:
1969fc4abfeSKashyap Desai 	{
1979fc4abfeSKashyap Desai 		struct mpi3_device_page0 *event_data =
1989fc4abfeSKashyap Desai 		    (struct mpi3_device_page0 *)event_reply->event_data;
1999fc4abfeSKashyap Desai 		ioc_info(mrioc, "Device Added: dev=0x%04x Form=0x%x\n",
2009fc4abfeSKashyap Desai 		    event_data->dev_handle, event_data->device_form);
2019fc4abfeSKashyap Desai 		return;
2029fc4abfeSKashyap Desai 	}
2039fc4abfeSKashyap Desai 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
2049fc4abfeSKashyap Desai 	{
2059fc4abfeSKashyap Desai 		struct mpi3_device_page0 *event_data =
2069fc4abfeSKashyap Desai 		    (struct mpi3_device_page0 *)event_reply->event_data;
2079fc4abfeSKashyap Desai 		ioc_info(mrioc, "Device Info Changed: dev=0x%04x Form=0x%x\n",
2089fc4abfeSKashyap Desai 		    event_data->dev_handle, event_data->device_form);
2099fc4abfeSKashyap Desai 		return;
2109fc4abfeSKashyap Desai 	}
2119fc4abfeSKashyap Desai 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2129fc4abfeSKashyap Desai 	{
2139fc4abfeSKashyap Desai 		struct mpi3_event_data_device_status_change *event_data =
2149fc4abfeSKashyap Desai 		    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2159fc4abfeSKashyap Desai 		ioc_info(mrioc, "Device status Change: dev=0x%04x RC=0x%x\n",
2169fc4abfeSKashyap Desai 		    event_data->dev_handle, event_data->reason_code);
2179fc4abfeSKashyap Desai 		return;
2189fc4abfeSKashyap Desai 	}
2199fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_DISCOVERY:
2209fc4abfeSKashyap Desai 	{
2219fc4abfeSKashyap Desai 		struct mpi3_event_data_sas_discovery *event_data =
2229fc4abfeSKashyap Desai 		    (struct mpi3_event_data_sas_discovery *)event_reply->event_data;
2239fc4abfeSKashyap Desai 		ioc_info(mrioc, "SAS Discovery: (%s) status (0x%08x)\n",
2249fc4abfeSKashyap Desai 		    (event_data->reason_code == MPI3_EVENT_SAS_DISC_RC_STARTED) ?
2259fc4abfeSKashyap Desai 		    "start" : "stop",
2269fc4abfeSKashyap Desai 		    le32_to_cpu(event_data->discovery_status));
2279fc4abfeSKashyap Desai 		return;
2289fc4abfeSKashyap Desai 	}
2299fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2309fc4abfeSKashyap Desai 		desc = "SAS Broadcast Primitive";
2319fc4abfeSKashyap Desai 		break;
2329fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_NOTIFY_PRIMITIVE:
2339fc4abfeSKashyap Desai 		desc = "SAS Notify Primitive";
2349fc4abfeSKashyap Desai 		break;
2359fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
2369fc4abfeSKashyap Desai 		desc = "SAS Init Device Status Change";
2379fc4abfeSKashyap Desai 		break;
2389fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_INIT_TABLE_OVERFLOW:
2399fc4abfeSKashyap Desai 		desc = "SAS Init Table Overflow";
2409fc4abfeSKashyap Desai 		break;
2419fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2429fc4abfeSKashyap Desai 		desc = "SAS Topology Change List";
2439fc4abfeSKashyap Desai 		break;
2449fc4abfeSKashyap Desai 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2459fc4abfeSKashyap Desai 		desc = "Enclosure Device Status Change";
2469fc4abfeSKashyap Desai 		break;
2479fc4abfeSKashyap Desai 	case MPI3_EVENT_HARD_RESET_RECEIVED:
2489fc4abfeSKashyap Desai 		desc = "Hard Reset Received";
2499fc4abfeSKashyap Desai 		break;
2509fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_PHY_COUNTER:
2519fc4abfeSKashyap Desai 		desc = "SAS PHY Counter";
2529fc4abfeSKashyap Desai 		break;
2539fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2549fc4abfeSKashyap Desai 		desc = "SAS Device Discovery Error";
2559fc4abfeSKashyap Desai 		break;
2569fc4abfeSKashyap Desai 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2579fc4abfeSKashyap Desai 		desc = "PCIE Topology Change List";
2589fc4abfeSKashyap Desai 		break;
2599fc4abfeSKashyap Desai 	case MPI3_EVENT_PCIE_ENUMERATION:
2609fc4abfeSKashyap Desai 	{
2619fc4abfeSKashyap Desai 		struct mpi3_event_data_pcie_enumeration *event_data =
2629fc4abfeSKashyap Desai 		    (struct mpi3_event_data_pcie_enumeration *)event_reply->event_data;
2639fc4abfeSKashyap Desai 		ioc_info(mrioc, "PCIE Enumeration: (%s)",
2649fc4abfeSKashyap Desai 		    (event_data->reason_code ==
2659fc4abfeSKashyap Desai 		    MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" : "stop");
2669fc4abfeSKashyap Desai 		if (event_data->enumeration_status)
2679fc4abfeSKashyap Desai 			ioc_info(mrioc, "enumeration_status(0x%08x)\n",
2689fc4abfeSKashyap Desai 			    le32_to_cpu(event_data->enumeration_status));
2699fc4abfeSKashyap Desai 		return;
2709fc4abfeSKashyap Desai 	}
2719fc4abfeSKashyap Desai 	case MPI3_EVENT_PREPARE_FOR_RESET:
2729fc4abfeSKashyap Desai 		desc = "Prepare For Reset";
2739fc4abfeSKashyap Desai 		break;
2749fc4abfeSKashyap Desai 	}
2759fc4abfeSKashyap Desai 
2769fc4abfeSKashyap Desai 	if (!desc)
2779fc4abfeSKashyap Desai 		return;
2789fc4abfeSKashyap Desai 
2799fc4abfeSKashyap Desai 	ioc_info(mrioc, "%s\n", desc);
2809fc4abfeSKashyap Desai }
2819fc4abfeSKashyap Desai 
282824a1566SKashyap Desai static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc,
283824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply)
284824a1566SKashyap Desai {
285824a1566SKashyap Desai 	struct mpi3_event_notification_reply *event_reply =
286824a1566SKashyap Desai 	    (struct mpi3_event_notification_reply *)def_reply;
287824a1566SKashyap Desai 
288824a1566SKashyap Desai 	mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count);
2899fc4abfeSKashyap Desai 	mpi3mr_print_event_data(mrioc, event_reply);
29013ef29eaSKashyap Desai 	mpi3mr_os_handle_events(mrioc, event_reply);
291824a1566SKashyap Desai }
292824a1566SKashyap Desai 
293824a1566SKashyap Desai static struct mpi3mr_drv_cmd *
294824a1566SKashyap Desai mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag,
295824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply)
296824a1566SKashyap Desai {
29713ef29eaSKashyap Desai 	u16 idx;
29813ef29eaSKashyap Desai 
299824a1566SKashyap Desai 	switch (host_tag) {
300824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INITCMDS:
301824a1566SKashyap Desai 		return &mrioc->init_cmds;
302*32d457d5SSreekanth Reddy 	case MPI3MR_HOSTTAG_CFG_CMDS:
303*32d457d5SSreekanth Reddy 		return &mrioc->cfg_cmds;
304f5e6d5a3SSumit Saxena 	case MPI3MR_HOSTTAG_BSG_CMDS:
305f5e6d5a3SSumit Saxena 		return &mrioc->bsg_cmds;
306e844adb1SKashyap Desai 	case MPI3MR_HOSTTAG_BLK_TMS:
307e844adb1SKashyap Desai 		return &mrioc->host_tm_cmds;
30843ca1100SSumit Saxena 	case MPI3MR_HOSTTAG_PEL_ABORT:
30943ca1100SSumit Saxena 		return &mrioc->pel_abort_cmd;
31043ca1100SSumit Saxena 	case MPI3MR_HOSTTAG_PEL_WAIT:
31143ca1100SSumit Saxena 		return &mrioc->pel_cmds;
312824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INVALID:
313824a1566SKashyap Desai 		if (def_reply && def_reply->function ==
314824a1566SKashyap Desai 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
315824a1566SKashyap Desai 			mpi3mr_handle_events(mrioc, def_reply);
316824a1566SKashyap Desai 		return NULL;
317824a1566SKashyap Desai 	default:
318824a1566SKashyap Desai 		break;
319824a1566SKashyap Desai 	}
32013ef29eaSKashyap Desai 	if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
32113ef29eaSKashyap Desai 	    host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) {
32213ef29eaSKashyap Desai 		idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
32313ef29eaSKashyap Desai 		return &mrioc->dev_rmhs_cmds[idx];
32413ef29eaSKashyap Desai 	}
325824a1566SKashyap Desai 
326c1af985dSSreekanth Reddy 	if (host_tag >= MPI3MR_HOSTTAG_EVTACKCMD_MIN &&
327c1af985dSSreekanth Reddy 	    host_tag <= MPI3MR_HOSTTAG_EVTACKCMD_MAX) {
328c1af985dSSreekanth Reddy 		idx = host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
329c1af985dSSreekanth Reddy 		return &mrioc->evtack_cmds[idx];
330c1af985dSSreekanth Reddy 	}
331c1af985dSSreekanth Reddy 
332824a1566SKashyap Desai 	return NULL;
333824a1566SKashyap Desai }
334824a1566SKashyap Desai 
335824a1566SKashyap Desai static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
336824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
337824a1566SKashyap Desai {
338824a1566SKashyap Desai 	u16 reply_desc_type, host_tag = 0;
339824a1566SKashyap Desai 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
340824a1566SKashyap Desai 	u32 ioc_loginfo = 0;
341824a1566SKashyap Desai 	struct mpi3_status_reply_descriptor *status_desc;
342824a1566SKashyap Desai 	struct mpi3_address_reply_descriptor *addr_desc;
343824a1566SKashyap Desai 	struct mpi3_success_reply_descriptor *success_desc;
344824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply = NULL;
345824a1566SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr = NULL;
346824a1566SKashyap Desai 	struct mpi3_scsi_io_reply *scsi_reply;
347824a1566SKashyap Desai 	u8 *sense_buf = NULL;
348824a1566SKashyap Desai 
349824a1566SKashyap Desai 	*reply_dma = 0;
350824a1566SKashyap Desai 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
351824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
352824a1566SKashyap Desai 	switch (reply_desc_type) {
353824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
354824a1566SKashyap Desai 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
355824a1566SKashyap Desai 		host_tag = le16_to_cpu(status_desc->host_tag);
356824a1566SKashyap Desai 		ioc_status = le16_to_cpu(status_desc->ioc_status);
357824a1566SKashyap Desai 		if (ioc_status &
358824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
359824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
360824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
361824a1566SKashyap Desai 		break;
362824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
363824a1566SKashyap Desai 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
364824a1566SKashyap Desai 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
365824a1566SKashyap Desai 		def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
366824a1566SKashyap Desai 		if (!def_reply)
367824a1566SKashyap Desai 			goto out;
368824a1566SKashyap Desai 		host_tag = le16_to_cpu(def_reply->host_tag);
369824a1566SKashyap Desai 		ioc_status = le16_to_cpu(def_reply->ioc_status);
370824a1566SKashyap Desai 		if (ioc_status &
371824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
372824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
373824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
374824a1566SKashyap Desai 		if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
375824a1566SKashyap Desai 			scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
376824a1566SKashyap Desai 			sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
377824a1566SKashyap Desai 			    le64_to_cpu(scsi_reply->sense_data_buffer_address));
378824a1566SKashyap Desai 		}
379824a1566SKashyap Desai 		break;
380824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
381824a1566SKashyap Desai 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
382824a1566SKashyap Desai 		host_tag = le16_to_cpu(success_desc->host_tag);
383824a1566SKashyap Desai 		break;
384824a1566SKashyap Desai 	default:
385824a1566SKashyap Desai 		break;
386824a1566SKashyap Desai 	}
387824a1566SKashyap Desai 
388824a1566SKashyap Desai 	cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
389824a1566SKashyap Desai 	if (cmdptr) {
390824a1566SKashyap Desai 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
391824a1566SKashyap Desai 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
392824a1566SKashyap Desai 			cmdptr->ioc_loginfo = ioc_loginfo;
393824a1566SKashyap Desai 			cmdptr->ioc_status = ioc_status;
394824a1566SKashyap Desai 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
395824a1566SKashyap Desai 			if (def_reply) {
396824a1566SKashyap Desai 				cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
397824a1566SKashyap Desai 				memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
398c5758fc7SSreekanth Reddy 				    mrioc->reply_sz);
399824a1566SKashyap Desai 			}
400824a1566SKashyap Desai 			if (cmdptr->is_waiting) {
401824a1566SKashyap Desai 				complete(&cmdptr->done);
402824a1566SKashyap Desai 				cmdptr->is_waiting = 0;
403824a1566SKashyap Desai 			} else if (cmdptr->callback)
404824a1566SKashyap Desai 				cmdptr->callback(mrioc, cmdptr);
405824a1566SKashyap Desai 		}
406824a1566SKashyap Desai 	}
407824a1566SKashyap Desai out:
408824a1566SKashyap Desai 	if (sense_buf)
409824a1566SKashyap Desai 		mpi3mr_repost_sense_buf(mrioc,
410824a1566SKashyap Desai 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
411824a1566SKashyap Desai }
412824a1566SKashyap Desai 
413824a1566SKashyap Desai static int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
414824a1566SKashyap Desai {
415824a1566SKashyap Desai 	u32 exp_phase = mrioc->admin_reply_ephase;
416824a1566SKashyap Desai 	u32 admin_reply_ci = mrioc->admin_reply_ci;
417824a1566SKashyap Desai 	u32 num_admin_replies = 0;
418824a1566SKashyap Desai 	u64 reply_dma = 0;
419824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
420824a1566SKashyap Desai 
421824a1566SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
422824a1566SKashyap Desai 	    admin_reply_ci;
423824a1566SKashyap Desai 
424824a1566SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
425824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
426824a1566SKashyap Desai 		return 0;
427824a1566SKashyap Desai 
428824a1566SKashyap Desai 	do {
429824a1566SKashyap Desai 		mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
430824a1566SKashyap Desai 		mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
431824a1566SKashyap Desai 		if (reply_dma)
432824a1566SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
433824a1566SKashyap Desai 		num_admin_replies++;
434824a1566SKashyap Desai 		if (++admin_reply_ci == mrioc->num_admin_replies) {
435824a1566SKashyap Desai 			admin_reply_ci = 0;
436824a1566SKashyap Desai 			exp_phase ^= 1;
437824a1566SKashyap Desai 		}
438824a1566SKashyap Desai 		reply_desc =
439824a1566SKashyap Desai 		    (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
440824a1566SKashyap Desai 		    admin_reply_ci;
441824a1566SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
442824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
443824a1566SKashyap Desai 			break;
444824a1566SKashyap Desai 	} while (1);
445824a1566SKashyap Desai 
446824a1566SKashyap Desai 	writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
447824a1566SKashyap Desai 	mrioc->admin_reply_ci = admin_reply_ci;
448824a1566SKashyap Desai 	mrioc->admin_reply_ephase = exp_phase;
449824a1566SKashyap Desai 
450824a1566SKashyap Desai 	return num_admin_replies;
451824a1566SKashyap Desai }
452824a1566SKashyap Desai 
453023ab2a9SKashyap Desai /**
454023ab2a9SKashyap Desai  * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to
455023ab2a9SKashyap Desai  *	queue's consumer index from operational reply descriptor queue.
456023ab2a9SKashyap Desai  * @op_reply_q: op_reply_qinfo object
457023ab2a9SKashyap Desai  * @reply_ci: operational reply descriptor's queue consumer index
458023ab2a9SKashyap Desai  *
459023ab2a9SKashyap Desai  * Returns reply descriptor frame address
460023ab2a9SKashyap Desai  */
461023ab2a9SKashyap Desai static inline struct mpi3_default_reply_descriptor *
462023ab2a9SKashyap Desai mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci)
463023ab2a9SKashyap Desai {
464023ab2a9SKashyap Desai 	void *segment_base_addr;
465023ab2a9SKashyap Desai 	struct segments *segments = op_reply_q->q_segments;
466023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc = NULL;
467023ab2a9SKashyap Desai 
468023ab2a9SKashyap Desai 	segment_base_addr =
469023ab2a9SKashyap Desai 	    segments[reply_ci / op_reply_q->segment_qd].segment;
470023ab2a9SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr +
471023ab2a9SKashyap Desai 	    (reply_ci % op_reply_q->segment_qd);
472023ab2a9SKashyap Desai 	return reply_desc;
473023ab2a9SKashyap Desai }
474023ab2a9SKashyap Desai 
475afd3a579SSreekanth Reddy /**
476afd3a579SSreekanth Reddy  * mpi3mr_process_op_reply_q - Operational reply queue handler
477afd3a579SSreekanth Reddy  * @mrioc: Adapter instance reference
478afd3a579SSreekanth Reddy  * @op_reply_q: Operational reply queue info
479afd3a579SSreekanth Reddy  *
480afd3a579SSreekanth Reddy  * Checks the specific operational reply queue and drains the
481afd3a579SSreekanth Reddy  * reply queue entries until the queue is empty and process the
482afd3a579SSreekanth Reddy  * individual reply descriptors.
483afd3a579SSreekanth Reddy  *
484afd3a579SSreekanth Reddy  * Return: 0 if queue is already processed,or number of reply
485afd3a579SSreekanth Reddy  *	    descriptors processed.
486afd3a579SSreekanth Reddy  */
487afd3a579SSreekanth Reddy int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc,
488afd3a579SSreekanth Reddy 	struct op_reply_qinfo *op_reply_q)
489023ab2a9SKashyap Desai {
490023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q;
491023ab2a9SKashyap Desai 	u32 exp_phase;
492023ab2a9SKashyap Desai 	u32 reply_ci;
493023ab2a9SKashyap Desai 	u32 num_op_reply = 0;
494023ab2a9SKashyap Desai 	u64 reply_dma = 0;
495023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
496023ab2a9SKashyap Desai 	u16 req_q_idx = 0, reply_qidx;
497023ab2a9SKashyap Desai 
498023ab2a9SKashyap Desai 	reply_qidx = op_reply_q->qid - 1;
499023ab2a9SKashyap Desai 
500463429f8SKashyap Desai 	if (!atomic_add_unless(&op_reply_q->in_use, 1, 1))
501463429f8SKashyap Desai 		return 0;
502463429f8SKashyap Desai 
503023ab2a9SKashyap Desai 	exp_phase = op_reply_q->ephase;
504023ab2a9SKashyap Desai 	reply_ci = op_reply_q->ci;
505023ab2a9SKashyap Desai 
506023ab2a9SKashyap Desai 	reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
507023ab2a9SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
508023ab2a9SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
509463429f8SKashyap Desai 		atomic_dec(&op_reply_q->in_use);
510023ab2a9SKashyap Desai 		return 0;
511023ab2a9SKashyap Desai 	}
512023ab2a9SKashyap Desai 
513023ab2a9SKashyap Desai 	do {
514023ab2a9SKashyap Desai 		req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1;
515023ab2a9SKashyap Desai 		op_req_q = &mrioc->req_qinfo[req_q_idx];
516023ab2a9SKashyap Desai 
517023ab2a9SKashyap Desai 		WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci));
518023ab2a9SKashyap Desai 		mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma,
519023ab2a9SKashyap Desai 		    reply_qidx);
520463429f8SKashyap Desai 		atomic_dec(&op_reply_q->pend_ios);
521023ab2a9SKashyap Desai 		if (reply_dma)
522023ab2a9SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
523023ab2a9SKashyap Desai 		num_op_reply++;
524023ab2a9SKashyap Desai 
525023ab2a9SKashyap Desai 		if (++reply_ci == op_reply_q->num_replies) {
526023ab2a9SKashyap Desai 			reply_ci = 0;
527023ab2a9SKashyap Desai 			exp_phase ^= 1;
528023ab2a9SKashyap Desai 		}
529023ab2a9SKashyap Desai 
530023ab2a9SKashyap Desai 		reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
531023ab2a9SKashyap Desai 
532023ab2a9SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
533023ab2a9SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
534023ab2a9SKashyap Desai 			break;
535463429f8SKashyap Desai 		/*
536463429f8SKashyap Desai 		 * Exit completion loop to avoid CPU lockup
537463429f8SKashyap Desai 		 * Ensure remaining completion happens from threaded ISR.
538463429f8SKashyap Desai 		 */
539463429f8SKashyap Desai 		if (num_op_reply > mrioc->max_host_ios) {
540afd3a579SSreekanth Reddy 			op_reply_q->enable_irq_poll = true;
541463429f8SKashyap Desai 			break;
542463429f8SKashyap Desai 		}
543023ab2a9SKashyap Desai 
544023ab2a9SKashyap Desai 	} while (1);
545023ab2a9SKashyap Desai 
546023ab2a9SKashyap Desai 	writel(reply_ci,
547023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index);
548023ab2a9SKashyap Desai 	op_reply_q->ci = reply_ci;
549023ab2a9SKashyap Desai 	op_reply_q->ephase = exp_phase;
550023ab2a9SKashyap Desai 
551463429f8SKashyap Desai 	atomic_dec(&op_reply_q->in_use);
552023ab2a9SKashyap Desai 	return num_op_reply;
553023ab2a9SKashyap Desai }
554023ab2a9SKashyap Desai 
555afd3a579SSreekanth Reddy /**
556afd3a579SSreekanth Reddy  * mpi3mr_blk_mq_poll - Operational reply queue handler
557afd3a579SSreekanth Reddy  * @shost: SCSI Host reference
558afd3a579SSreekanth Reddy  * @queue_num: Request queue number (w.r.t OS it is hardware context number)
559afd3a579SSreekanth Reddy  *
560afd3a579SSreekanth Reddy  * Checks the specific operational reply queue and drains the
561afd3a579SSreekanth Reddy  * reply queue entries until the queue is empty and process the
562afd3a579SSreekanth Reddy  * individual reply descriptors.
563afd3a579SSreekanth Reddy  *
564afd3a579SSreekanth Reddy  * Return: 0 if queue is already processed,or number of reply
565afd3a579SSreekanth Reddy  *	    descriptors processed.
566afd3a579SSreekanth Reddy  */
567afd3a579SSreekanth Reddy int mpi3mr_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
568afd3a579SSreekanth Reddy {
569afd3a579SSreekanth Reddy 	int num_entries = 0;
570afd3a579SSreekanth Reddy 	struct mpi3mr_ioc *mrioc;
571afd3a579SSreekanth Reddy 
572afd3a579SSreekanth Reddy 	mrioc = (struct mpi3mr_ioc *)shost->hostdata;
573afd3a579SSreekanth Reddy 
574afd3a579SSreekanth Reddy 	if ((mrioc->reset_in_progress || mrioc->prepare_for_reset))
575afd3a579SSreekanth Reddy 		return 0;
576afd3a579SSreekanth Reddy 
577afd3a579SSreekanth Reddy 	num_entries = mpi3mr_process_op_reply_q(mrioc,
578afd3a579SSreekanth Reddy 			&mrioc->op_reply_qinfo[queue_num]);
579afd3a579SSreekanth Reddy 
580afd3a579SSreekanth Reddy 	return num_entries;
581afd3a579SSreekanth Reddy }
582afd3a579SSreekanth Reddy 
583824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
584824a1566SKashyap Desai {
585824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
586824a1566SKashyap Desai 	struct mpi3mr_ioc *mrioc;
587824a1566SKashyap Desai 	u16 midx;
588463429f8SKashyap Desai 	u32 num_admin_replies = 0, num_op_reply = 0;
589824a1566SKashyap Desai 
590824a1566SKashyap Desai 	if (!intr_info)
591824a1566SKashyap Desai 		return IRQ_NONE;
592824a1566SKashyap Desai 
593824a1566SKashyap Desai 	mrioc = intr_info->mrioc;
594824a1566SKashyap Desai 
595824a1566SKashyap Desai 	if (!mrioc->intr_enabled)
596824a1566SKashyap Desai 		return IRQ_NONE;
597824a1566SKashyap Desai 
598824a1566SKashyap Desai 	midx = intr_info->msix_index;
599824a1566SKashyap Desai 
600824a1566SKashyap Desai 	if (!midx)
601824a1566SKashyap Desai 		num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
602463429f8SKashyap Desai 	if (intr_info->op_reply_q)
603afd3a579SSreekanth Reddy 		num_op_reply = mpi3mr_process_op_reply_q(mrioc,
604afd3a579SSreekanth Reddy 		    intr_info->op_reply_q);
605824a1566SKashyap Desai 
606463429f8SKashyap Desai 	if (num_admin_replies || num_op_reply)
607824a1566SKashyap Desai 		return IRQ_HANDLED;
608824a1566SKashyap Desai 	else
609824a1566SKashyap Desai 		return IRQ_NONE;
610824a1566SKashyap Desai }
611824a1566SKashyap Desai 
612824a1566SKashyap Desai static irqreturn_t mpi3mr_isr(int irq, void *privdata)
613824a1566SKashyap Desai {
614824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
615463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
616463429f8SKashyap Desai 	u16 midx;
617824a1566SKashyap Desai 	int ret;
618824a1566SKashyap Desai 
619824a1566SKashyap Desai 	if (!intr_info)
620824a1566SKashyap Desai 		return IRQ_NONE;
621824a1566SKashyap Desai 
622463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
623463429f8SKashyap Desai 	midx = intr_info->msix_index;
624824a1566SKashyap Desai 	/* Call primary ISR routine */
625824a1566SKashyap Desai 	ret = mpi3mr_isr_primary(irq, privdata);
626824a1566SKashyap Desai 
627463429f8SKashyap Desai 	/*
628463429f8SKashyap Desai 	 * If more IOs are expected, schedule IRQ polling thread.
629463429f8SKashyap Desai 	 * Otherwise exit from ISR.
630463429f8SKashyap Desai 	 */
631463429f8SKashyap Desai 	if (!intr_info->op_reply_q)
632824a1566SKashyap Desai 		return ret;
633463429f8SKashyap Desai 
634463429f8SKashyap Desai 	if (!intr_info->op_reply_q->enable_irq_poll ||
635463429f8SKashyap Desai 	    !atomic_read(&intr_info->op_reply_q->pend_ios))
636463429f8SKashyap Desai 		return ret;
637463429f8SKashyap Desai 
638463429f8SKashyap Desai 	disable_irq_nosync(pci_irq_vector(mrioc->pdev, midx));
639463429f8SKashyap Desai 
640463429f8SKashyap Desai 	return IRQ_WAKE_THREAD;
641824a1566SKashyap Desai }
642824a1566SKashyap Desai 
643824a1566SKashyap Desai /**
644824a1566SKashyap Desai  * mpi3mr_isr_poll - Reply queue polling routine
645824a1566SKashyap Desai  * @irq: IRQ
646824a1566SKashyap Desai  * @privdata: Interrupt info
647824a1566SKashyap Desai  *
648824a1566SKashyap Desai  * poll for pending I/O completions in a loop until pending I/Os
649824a1566SKashyap Desai  * present or controller queue depth I/Os are processed.
650824a1566SKashyap Desai  *
651824a1566SKashyap Desai  * Return: IRQ_NONE or IRQ_HANDLED
652824a1566SKashyap Desai  */
653824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
654824a1566SKashyap Desai {
655463429f8SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
656463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
657463429f8SKashyap Desai 	u16 midx;
658463429f8SKashyap Desai 	u32 num_op_reply = 0;
659463429f8SKashyap Desai 
660463429f8SKashyap Desai 	if (!intr_info || !intr_info->op_reply_q)
661463429f8SKashyap Desai 		return IRQ_NONE;
662463429f8SKashyap Desai 
663463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
664463429f8SKashyap Desai 	midx = intr_info->msix_index;
665463429f8SKashyap Desai 
666463429f8SKashyap Desai 	/* Poll for pending IOs completions */
667463429f8SKashyap Desai 	do {
668463429f8SKashyap Desai 		if (!mrioc->intr_enabled)
669463429f8SKashyap Desai 			break;
670463429f8SKashyap Desai 
671463429f8SKashyap Desai 		if (!midx)
672463429f8SKashyap Desai 			mpi3mr_process_admin_reply_q(mrioc);
673463429f8SKashyap Desai 		if (intr_info->op_reply_q)
674463429f8SKashyap Desai 			num_op_reply +=
675afd3a579SSreekanth Reddy 			    mpi3mr_process_op_reply_q(mrioc,
676afd3a579SSreekanth Reddy 				intr_info->op_reply_q);
677463429f8SKashyap Desai 
678afd3a579SSreekanth Reddy 		usleep_range(MPI3MR_IRQ_POLL_SLEEP, 10 * MPI3MR_IRQ_POLL_SLEEP);
679463429f8SKashyap Desai 
680463429f8SKashyap Desai 	} while (atomic_read(&intr_info->op_reply_q->pend_ios) &&
681463429f8SKashyap Desai 	    (num_op_reply < mrioc->max_host_ios));
682463429f8SKashyap Desai 
683463429f8SKashyap Desai 	intr_info->op_reply_q->enable_irq_poll = false;
684463429f8SKashyap Desai 	enable_irq(pci_irq_vector(mrioc->pdev, midx));
685463429f8SKashyap Desai 
686824a1566SKashyap Desai 	return IRQ_HANDLED;
687824a1566SKashyap Desai }
688824a1566SKashyap Desai 
689824a1566SKashyap Desai /**
690824a1566SKashyap Desai  * mpi3mr_request_irq - Request IRQ and register ISR
691824a1566SKashyap Desai  * @mrioc: Adapter instance reference
692824a1566SKashyap Desai  * @index: IRQ vector index
693824a1566SKashyap Desai  *
694824a1566SKashyap Desai  * Request threaded ISR with primary ISR and secondary
695824a1566SKashyap Desai  *
696824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
697824a1566SKashyap Desai  */
698824a1566SKashyap Desai static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
699824a1566SKashyap Desai {
700824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
701824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
702824a1566SKashyap Desai 	int retval = 0;
703824a1566SKashyap Desai 
704824a1566SKashyap Desai 	intr_info->mrioc = mrioc;
705824a1566SKashyap Desai 	intr_info->msix_index = index;
706824a1566SKashyap Desai 	intr_info->op_reply_q = NULL;
707824a1566SKashyap Desai 
708824a1566SKashyap Desai 	snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
709824a1566SKashyap Desai 	    mrioc->driver_name, mrioc->id, index);
710824a1566SKashyap Desai 
711824a1566SKashyap Desai 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
712824a1566SKashyap Desai 	    mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
713824a1566SKashyap Desai 	if (retval) {
714824a1566SKashyap Desai 		ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
715824a1566SKashyap Desai 		    intr_info->name, pci_irq_vector(pdev, index));
716824a1566SKashyap Desai 		return retval;
717824a1566SKashyap Desai 	}
718824a1566SKashyap Desai 
719824a1566SKashyap Desai 	return retval;
720824a1566SKashyap Desai }
721824a1566SKashyap Desai 
722afd3a579SSreekanth Reddy static void mpi3mr_calc_poll_queues(struct mpi3mr_ioc *mrioc, u16 max_vectors)
723afd3a579SSreekanth Reddy {
724afd3a579SSreekanth Reddy 	if (!mrioc->requested_poll_qcount)
725afd3a579SSreekanth Reddy 		return;
726afd3a579SSreekanth Reddy 
727afd3a579SSreekanth Reddy 	/* Reserved for Admin and Default Queue */
728afd3a579SSreekanth Reddy 	if (max_vectors > 2 &&
729afd3a579SSreekanth Reddy 		(mrioc->requested_poll_qcount < max_vectors - 2)) {
730afd3a579SSreekanth Reddy 		ioc_info(mrioc,
731afd3a579SSreekanth Reddy 		    "enabled polled queues (%d) msix (%d)\n",
732afd3a579SSreekanth Reddy 		    mrioc->requested_poll_qcount, max_vectors);
733afd3a579SSreekanth Reddy 	} else {
734afd3a579SSreekanth Reddy 		ioc_info(mrioc,
735afd3a579SSreekanth Reddy 		    "disabled polled queues (%d) msix (%d) because of no resources for default queue\n",
736afd3a579SSreekanth Reddy 		    mrioc->requested_poll_qcount, max_vectors);
737afd3a579SSreekanth Reddy 		mrioc->requested_poll_qcount = 0;
738afd3a579SSreekanth Reddy 	}
739afd3a579SSreekanth Reddy }
740afd3a579SSreekanth Reddy 
741824a1566SKashyap Desai /**
742824a1566SKashyap Desai  * mpi3mr_setup_isr - Setup ISR for the controller
743824a1566SKashyap Desai  * @mrioc: Adapter instance reference
744824a1566SKashyap Desai  * @setup_one: Request one IRQ or more
745824a1566SKashyap Desai  *
746824a1566SKashyap Desai  * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
747824a1566SKashyap Desai  *
748824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
749824a1566SKashyap Desai  */
750824a1566SKashyap Desai static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
751824a1566SKashyap Desai {
752824a1566SKashyap Desai 	unsigned int irq_flags = PCI_IRQ_MSIX;
753afd3a579SSreekanth Reddy 	int max_vectors, min_vec;
7542938beddSDan Carpenter 	int retval;
7552938beddSDan Carpenter 	int i;
756afd3a579SSreekanth Reddy 	struct irq_affinity desc = { .pre_vectors =  1, .post_vectors = 1 };
757824a1566SKashyap Desai 
758fe6db615SSreekanth Reddy 	if (mrioc->is_intr_info_set)
759fe6db615SSreekanth Reddy 		return 0;
760fe6db615SSreekanth Reddy 
761824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
762824a1566SKashyap Desai 
763afd3a579SSreekanth Reddy 	if (setup_one || reset_devices) {
764824a1566SKashyap Desai 		max_vectors = 1;
765afd3a579SSreekanth Reddy 		retval = pci_alloc_irq_vectors(mrioc->pdev,
766afd3a579SSreekanth Reddy 		    1, max_vectors, irq_flags);
767afd3a579SSreekanth Reddy 		if (retval < 0) {
768afd3a579SSreekanth Reddy 			ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
769afd3a579SSreekanth Reddy 			    retval);
770afd3a579SSreekanth Reddy 			goto out_failed;
771afd3a579SSreekanth Reddy 		}
772afd3a579SSreekanth Reddy 	} else {
773824a1566SKashyap Desai 		max_vectors =
774afd3a579SSreekanth Reddy 		    min_t(int, mrioc->cpu_count + 1 +
775afd3a579SSreekanth Reddy 			mrioc->requested_poll_qcount, mrioc->msix_count);
776afd3a579SSreekanth Reddy 
777afd3a579SSreekanth Reddy 		mpi3mr_calc_poll_queues(mrioc, max_vectors);
778824a1566SKashyap Desai 
779824a1566SKashyap Desai 		ioc_info(mrioc,
780824a1566SKashyap Desai 		    "MSI-X vectors supported: %d, no of cores: %d,",
781824a1566SKashyap Desai 		    mrioc->msix_count, mrioc->cpu_count);
782824a1566SKashyap Desai 		ioc_info(mrioc,
783afd3a579SSreekanth Reddy 		    "MSI-x vectors requested: %d poll_queues %d\n",
784afd3a579SSreekanth Reddy 		    max_vectors, mrioc->requested_poll_qcount);
785824a1566SKashyap Desai 
786afd3a579SSreekanth Reddy 		desc.post_vectors = mrioc->requested_poll_qcount;
787afd3a579SSreekanth Reddy 		min_vec = desc.pre_vectors + desc.post_vectors;
788824a1566SKashyap Desai 		irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
789824a1566SKashyap Desai 
7902938beddSDan Carpenter 		retval = pci_alloc_irq_vectors_affinity(mrioc->pdev,
791afd3a579SSreekanth Reddy 			min_vec, max_vectors, irq_flags, &desc);
792afd3a579SSreekanth Reddy 
7932938beddSDan Carpenter 		if (retval < 0) {
794afd3a579SSreekanth Reddy 			ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
795afd3a579SSreekanth Reddy 			    retval);
796824a1566SKashyap Desai 			goto out_failed;
797824a1566SKashyap Desai 		}
798afd3a579SSreekanth Reddy 
799afd3a579SSreekanth Reddy 
800c9566231SKashyap Desai 		/*
801c9566231SKashyap Desai 		 * If only one MSI-x is allocated, then MSI-x 0 will be shared
802c9566231SKashyap Desai 		 * between Admin queue and operational queue
803c9566231SKashyap Desai 		 */
804afd3a579SSreekanth Reddy 		if (retval == min_vec)
805c9566231SKashyap Desai 			mrioc->op_reply_q_offset = 0;
806afd3a579SSreekanth Reddy 		else if (retval != (max_vectors)) {
807afd3a579SSreekanth Reddy 			ioc_info(mrioc,
808afd3a579SSreekanth Reddy 			    "allocated vectors (%d) are less than configured (%d)\n",
809afd3a579SSreekanth Reddy 			    retval, max_vectors);
810afd3a579SSreekanth Reddy 		}
811824a1566SKashyap Desai 
8122938beddSDan Carpenter 		max_vectors = retval;
813afd3a579SSreekanth Reddy 		mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0;
814afd3a579SSreekanth Reddy 
815afd3a579SSreekanth Reddy 		mpi3mr_calc_poll_queues(mrioc, max_vectors);
816afd3a579SSreekanth Reddy 
817824a1566SKashyap Desai 	}
818afd3a579SSreekanth Reddy 
819824a1566SKashyap Desai 	mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
820824a1566SKashyap Desai 	    GFP_KERNEL);
821824a1566SKashyap Desai 	if (!mrioc->intr_info) {
8222938beddSDan Carpenter 		retval = -ENOMEM;
823824a1566SKashyap Desai 		pci_free_irq_vectors(mrioc->pdev);
824824a1566SKashyap Desai 		goto out_failed;
825824a1566SKashyap Desai 	}
826824a1566SKashyap Desai 	for (i = 0; i < max_vectors; i++) {
827824a1566SKashyap Desai 		retval = mpi3mr_request_irq(mrioc, i);
828824a1566SKashyap Desai 		if (retval) {
829824a1566SKashyap Desai 			mrioc->intr_info_count = i;
830824a1566SKashyap Desai 			goto out_failed;
831824a1566SKashyap Desai 		}
832824a1566SKashyap Desai 	}
833fe6db615SSreekanth Reddy 	if (reset_devices || !setup_one)
834fe6db615SSreekanth Reddy 		mrioc->is_intr_info_set = true;
835824a1566SKashyap Desai 	mrioc->intr_info_count = max_vectors;
836824a1566SKashyap Desai 	mpi3mr_ioc_enable_intr(mrioc);
8372938beddSDan Carpenter 	return 0;
8382938beddSDan Carpenter 
839824a1566SKashyap Desai out_failed:
840824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
841824a1566SKashyap Desai 
842824a1566SKashyap Desai 	return retval;
843824a1566SKashyap Desai }
844824a1566SKashyap Desai 
845824a1566SKashyap Desai static const struct {
846824a1566SKashyap Desai 	enum mpi3mr_iocstate value;
847824a1566SKashyap Desai 	char *name;
848824a1566SKashyap Desai } mrioc_states[] = {
849824a1566SKashyap Desai 	{ MRIOC_STATE_READY, "ready" },
850824a1566SKashyap Desai 	{ MRIOC_STATE_FAULT, "fault" },
851824a1566SKashyap Desai 	{ MRIOC_STATE_RESET, "reset" },
852824a1566SKashyap Desai 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
853824a1566SKashyap Desai 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
854824a1566SKashyap Desai 	{ MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
855824a1566SKashyap Desai };
856824a1566SKashyap Desai 
857824a1566SKashyap Desai static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
858824a1566SKashyap Desai {
859824a1566SKashyap Desai 	int i;
860824a1566SKashyap Desai 	char *name = NULL;
861824a1566SKashyap Desai 
862824a1566SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
863824a1566SKashyap Desai 		if (mrioc_states[i].value == mrioc_state) {
864824a1566SKashyap Desai 			name = mrioc_states[i].name;
865824a1566SKashyap Desai 			break;
866824a1566SKashyap Desai 		}
867824a1566SKashyap Desai 	}
868824a1566SKashyap Desai 	return name;
869824a1566SKashyap Desai }
870824a1566SKashyap Desai 
871f061178eSKashyap Desai /* Reset reason to name mapper structure*/
872f061178eSKashyap Desai static const struct {
873f061178eSKashyap Desai 	enum mpi3mr_reset_reason value;
874f061178eSKashyap Desai 	char *name;
875f061178eSKashyap Desai } mpi3mr_reset_reason_codes[] = {
876f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
877f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
878f5e6d5a3SSumit Saxena 	{ MPI3MR_RESET_FROM_APP, "application invocation" },
879f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EH_HOS, "error handling" },
880f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
881f5e6d5a3SSumit Saxena 	{ MPI3MR_RESET_FROM_APP_TIMEOUT, "application command timeout" },
882f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
883f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
884f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
885f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
886f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
887f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
888f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
889f061178eSKashyap Desai 	{
890f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
891f061178eSKashyap Desai 		"create request queue timeout"
892f061178eSKashyap Desai 	},
893f061178eSKashyap Desai 	{
894f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
895f061178eSKashyap Desai 		"create reply queue timeout"
896f061178eSKashyap Desai 	},
897f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
898f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
899f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
900f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
901f061178eSKashyap Desai 	{
902f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CIACTVRST_TIMER,
903f061178eSKashyap Desai 		"component image activation timeout"
904f061178eSKashyap Desai 	},
905f061178eSKashyap Desai 	{
906f061178eSKashyap Desai 		MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
907f061178eSKashyap Desai 		"get package version timeout"
908f061178eSKashyap Desai 	},
909f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
910f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
9115867b856SColin Ian King 	{ MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronous reset" },
912*32d457d5SSreekanth Reddy 	{ MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT, "configuration request timeout"},
913f061178eSKashyap Desai };
914f061178eSKashyap Desai 
915f061178eSKashyap Desai /**
916f061178eSKashyap Desai  * mpi3mr_reset_rc_name - get reset reason code name
917f061178eSKashyap Desai  * @reason_code: reset reason code value
918f061178eSKashyap Desai  *
919f061178eSKashyap Desai  * Map reset reason to an NULL terminated ASCII string
920f061178eSKashyap Desai  *
921f061178eSKashyap Desai  * Return: name corresponding to reset reason value or NULL.
922f061178eSKashyap Desai  */
923f061178eSKashyap Desai static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
924f061178eSKashyap Desai {
925f061178eSKashyap Desai 	int i;
926f061178eSKashyap Desai 	char *name = NULL;
927f061178eSKashyap Desai 
928f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) {
929f061178eSKashyap Desai 		if (mpi3mr_reset_reason_codes[i].value == reason_code) {
930f061178eSKashyap Desai 			name = mpi3mr_reset_reason_codes[i].name;
931f061178eSKashyap Desai 			break;
932f061178eSKashyap Desai 		}
933f061178eSKashyap Desai 	}
934f061178eSKashyap Desai 	return name;
935f061178eSKashyap Desai }
936f061178eSKashyap Desai 
937f061178eSKashyap Desai /* Reset type to name mapper structure*/
938f061178eSKashyap Desai static const struct {
939f061178eSKashyap Desai 	u16 reset_type;
940f061178eSKashyap Desai 	char *name;
941f061178eSKashyap Desai } mpi3mr_reset_types[] = {
942f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
943f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
944f061178eSKashyap Desai };
945f061178eSKashyap Desai 
946f061178eSKashyap Desai /**
947f061178eSKashyap Desai  * mpi3mr_reset_type_name - get reset type name
948f061178eSKashyap Desai  * @reset_type: reset type value
949f061178eSKashyap Desai  *
950f061178eSKashyap Desai  * Map reset type to an NULL terminated ASCII string
951f061178eSKashyap Desai  *
952f061178eSKashyap Desai  * Return: name corresponding to reset type value or NULL.
953f061178eSKashyap Desai  */
954f061178eSKashyap Desai static const char *mpi3mr_reset_type_name(u16 reset_type)
955f061178eSKashyap Desai {
956f061178eSKashyap Desai 	int i;
957f061178eSKashyap Desai 	char *name = NULL;
958f061178eSKashyap Desai 
959f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) {
960f061178eSKashyap Desai 		if (mpi3mr_reset_types[i].reset_type == reset_type) {
961f061178eSKashyap Desai 			name = mpi3mr_reset_types[i].name;
962f061178eSKashyap Desai 			break;
963f061178eSKashyap Desai 		}
964f061178eSKashyap Desai 	}
965f061178eSKashyap Desai 	return name;
966f061178eSKashyap Desai }
967f061178eSKashyap Desai 
968824a1566SKashyap Desai /**
969824a1566SKashyap Desai  * mpi3mr_print_fault_info - Display fault information
970824a1566SKashyap Desai  * @mrioc: Adapter instance reference
971824a1566SKashyap Desai  *
972824a1566SKashyap Desai  * Display the controller fault information if there is a
973824a1566SKashyap Desai  * controller fault.
974824a1566SKashyap Desai  *
975824a1566SKashyap Desai  * Return: Nothing.
976824a1566SKashyap Desai  */
977b64845a7SSreekanth Reddy void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
978824a1566SKashyap Desai {
979824a1566SKashyap Desai 	u32 ioc_status, code, code1, code2, code3;
980824a1566SKashyap Desai 
981824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
982824a1566SKashyap Desai 
983824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
984824a1566SKashyap Desai 		code = readl(&mrioc->sysif_regs->fault);
985824a1566SKashyap Desai 		code1 = readl(&mrioc->sysif_regs->fault_info[0]);
986824a1566SKashyap Desai 		code2 = readl(&mrioc->sysif_regs->fault_info[1]);
987824a1566SKashyap Desai 		code3 = readl(&mrioc->sysif_regs->fault_info[2]);
988824a1566SKashyap Desai 
989824a1566SKashyap Desai 		ioc_info(mrioc,
990824a1566SKashyap Desai 		    "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
991824a1566SKashyap Desai 		    code, code1, code2, code3);
992824a1566SKashyap Desai 	}
993824a1566SKashyap Desai }
994824a1566SKashyap Desai 
995824a1566SKashyap Desai /**
996824a1566SKashyap Desai  * mpi3mr_get_iocstate - Get IOC State
997824a1566SKashyap Desai  * @mrioc: Adapter instance reference
998824a1566SKashyap Desai  *
999824a1566SKashyap Desai  * Return a proper IOC state enum based on the IOC status and
1000824a1566SKashyap Desai  * IOC configuration and unrcoverable state of the controller.
1001824a1566SKashyap Desai  *
1002824a1566SKashyap Desai  * Return: Current IOC state.
1003824a1566SKashyap Desai  */
1004824a1566SKashyap Desai enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
1005824a1566SKashyap Desai {
1006824a1566SKashyap Desai 	u32 ioc_status, ioc_config;
1007824a1566SKashyap Desai 	u8 ready, enabled;
1008824a1566SKashyap Desai 
1009824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1010824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1011824a1566SKashyap Desai 
1012824a1566SKashyap Desai 	if (mrioc->unrecoverable)
1013824a1566SKashyap Desai 		return MRIOC_STATE_UNRECOVERABLE;
1014824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
1015824a1566SKashyap Desai 		return MRIOC_STATE_FAULT;
1016824a1566SKashyap Desai 
1017824a1566SKashyap Desai 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
1018824a1566SKashyap Desai 	enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
1019824a1566SKashyap Desai 
1020824a1566SKashyap Desai 	if (ready && enabled)
1021824a1566SKashyap Desai 		return MRIOC_STATE_READY;
1022824a1566SKashyap Desai 	if ((!ready) && (!enabled))
1023824a1566SKashyap Desai 		return MRIOC_STATE_RESET;
1024824a1566SKashyap Desai 	if ((!ready) && (enabled))
1025824a1566SKashyap Desai 		return MRIOC_STATE_BECOMING_READY;
1026824a1566SKashyap Desai 
1027824a1566SKashyap Desai 	return MRIOC_STATE_RESET_REQUESTED;
1028824a1566SKashyap Desai }
1029824a1566SKashyap Desai 
1030824a1566SKashyap Desai /**
1031824a1566SKashyap Desai  * mpi3mr_clear_reset_history - clear reset history
1032824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1033824a1566SKashyap Desai  *
1034824a1566SKashyap Desai  * Write the reset history bit in IOC status to clear the bit,
1035824a1566SKashyap Desai  * if it is already set.
1036824a1566SKashyap Desai  *
1037824a1566SKashyap Desai  * Return: Nothing.
1038824a1566SKashyap Desai  */
1039824a1566SKashyap Desai static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
1040824a1566SKashyap Desai {
1041824a1566SKashyap Desai 	u32 ioc_status;
1042824a1566SKashyap Desai 
1043824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1044824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1045824a1566SKashyap Desai 		writel(ioc_status, &mrioc->sysif_regs->ioc_status);
1046824a1566SKashyap Desai }
1047824a1566SKashyap Desai 
1048824a1566SKashyap Desai /**
1049824a1566SKashyap Desai  * mpi3mr_issue_and_process_mur - Message unit Reset handler
1050824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1051824a1566SKashyap Desai  * @reset_reason: Reset reason code
1052824a1566SKashyap Desai  *
1053824a1566SKashyap Desai  * Issue Message unit Reset to the controller and wait for it to
1054824a1566SKashyap Desai  * be complete.
1055824a1566SKashyap Desai  *
1056824a1566SKashyap Desai  * Return: 0 on success, -1 on failure.
1057824a1566SKashyap Desai  */
1058824a1566SKashyap Desai static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
1059824a1566SKashyap Desai 	u32 reset_reason)
1060824a1566SKashyap Desai {
1061824a1566SKashyap Desai 	u32 ioc_config, timeout, ioc_status;
1062824a1566SKashyap Desai 	int retval = -1;
1063824a1566SKashyap Desai 
1064824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
1065824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1066824a1566SKashyap Desai 		ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
1067824a1566SKashyap Desai 		return retval;
1068824a1566SKashyap Desai 	}
1069824a1566SKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1070824a1566SKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1071824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1072824a1566SKashyap Desai 	ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1073824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1074824a1566SKashyap Desai 
1075b64845a7SSreekanth Reddy 	timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1076824a1566SKashyap Desai 	do {
1077824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1078824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
1079824a1566SKashyap Desai 			mpi3mr_clear_reset_history(mrioc);
1080824a1566SKashyap Desai 			break;
1081824a1566SKashyap Desai 		}
1082b64845a7SSreekanth Reddy 		if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
1083b64845a7SSreekanth Reddy 			mpi3mr_print_fault_info(mrioc);
1084b64845a7SSreekanth Reddy 			break;
1085824a1566SKashyap Desai 		}
1086824a1566SKashyap Desai 		msleep(100);
1087824a1566SKashyap Desai 	} while (--timeout);
1088824a1566SKashyap Desai 
1089824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1090b64845a7SSreekanth Reddy 	if (timeout && !((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1091b64845a7SSreekanth Reddy 	      (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
1092b64845a7SSreekanth Reddy 	      (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1093b64845a7SSreekanth Reddy 		retval = 0;
1094824a1566SKashyap Desai 
1095824a1566SKashyap Desai 	ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
1096824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status, ioc_config);
1097824a1566SKashyap Desai 	return retval;
1098824a1566SKashyap Desai }
1099824a1566SKashyap Desai 
1100824a1566SKashyap Desai /**
1101c5758fc7SSreekanth Reddy  * mpi3mr_revalidate_factsdata - validate IOCFacts parameters
1102c5758fc7SSreekanth Reddy  * during reset/resume
1103c5758fc7SSreekanth Reddy  * @mrioc: Adapter instance reference
1104c5758fc7SSreekanth Reddy  *
1105c5758fc7SSreekanth Reddy  * Return zero if the new IOCFacts parameters value is compatible with
1106c5758fc7SSreekanth Reddy  * older values else return -EPERM
1107c5758fc7SSreekanth Reddy  */
1108c5758fc7SSreekanth Reddy static int
1109c5758fc7SSreekanth Reddy mpi3mr_revalidate_factsdata(struct mpi3mr_ioc *mrioc)
1110c5758fc7SSreekanth Reddy {
1111c5758fc7SSreekanth Reddy 	u16 dev_handle_bitmap_sz;
1112c5758fc7SSreekanth Reddy 	void *removepend_bitmap;
1113c5758fc7SSreekanth Reddy 
1114c5758fc7SSreekanth Reddy 	if (mrioc->facts.reply_sz > mrioc->reply_sz) {
1115c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1116c5758fc7SSreekanth Reddy 		    "cannot increase reply size from %d to %d\n",
1117c5758fc7SSreekanth Reddy 		    mrioc->reply_sz, mrioc->facts.reply_sz);
1118c5758fc7SSreekanth Reddy 		return -EPERM;
1119c5758fc7SSreekanth Reddy 	}
1120c5758fc7SSreekanth Reddy 
1121c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_reply_q < mrioc->num_op_reply_q) {
1122c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1123c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational reply queues from %d to %d\n",
1124c5758fc7SSreekanth Reddy 		    mrioc->num_op_reply_q,
1125c5758fc7SSreekanth Reddy 		    mrioc->facts.max_op_reply_q);
1126c5758fc7SSreekanth Reddy 		return -EPERM;
1127c5758fc7SSreekanth Reddy 	}
1128c5758fc7SSreekanth Reddy 
1129c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_req_q < mrioc->num_op_req_q) {
1130c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1131c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational request queues from %d to %d\n",
1132c5758fc7SSreekanth Reddy 		    mrioc->num_op_req_q, mrioc->facts.max_op_req_q);
1133c5758fc7SSreekanth Reddy 		return -EPERM;
1134c5758fc7SSreekanth Reddy 	}
1135c5758fc7SSreekanth Reddy 
1136c5758fc7SSreekanth Reddy 	dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
1137c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_devhandle % 8)
1138c5758fc7SSreekanth Reddy 		dev_handle_bitmap_sz++;
1139c5758fc7SSreekanth Reddy 	if (dev_handle_bitmap_sz > mrioc->dev_handle_bitmap_sz) {
1140c5758fc7SSreekanth Reddy 		removepend_bitmap = krealloc(mrioc->removepend_bitmap,
1141c5758fc7SSreekanth Reddy 		    dev_handle_bitmap_sz, GFP_KERNEL);
1142c5758fc7SSreekanth Reddy 		if (!removepend_bitmap) {
1143c5758fc7SSreekanth Reddy 			ioc_err(mrioc,
1144c5758fc7SSreekanth Reddy 			    "failed to increase removepend_bitmap sz from: %d to %d\n",
1145c5758fc7SSreekanth Reddy 			    mrioc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
1146c5758fc7SSreekanth Reddy 			return -EPERM;
1147c5758fc7SSreekanth Reddy 		}
1148c5758fc7SSreekanth Reddy 		memset(removepend_bitmap + mrioc->dev_handle_bitmap_sz, 0,
1149c5758fc7SSreekanth Reddy 		    dev_handle_bitmap_sz - mrioc->dev_handle_bitmap_sz);
1150c5758fc7SSreekanth Reddy 		mrioc->removepend_bitmap = removepend_bitmap;
1151c5758fc7SSreekanth Reddy 		ioc_info(mrioc,
1152c5758fc7SSreekanth Reddy 		    "increased dev_handle_bitmap_sz from %d to %d\n",
1153c5758fc7SSreekanth Reddy 		    mrioc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
1154c5758fc7SSreekanth Reddy 		mrioc->dev_handle_bitmap_sz = dev_handle_bitmap_sz;
1155c5758fc7SSreekanth Reddy 	}
1156c5758fc7SSreekanth Reddy 
1157c5758fc7SSreekanth Reddy 	return 0;
1158c5758fc7SSreekanth Reddy }
1159c5758fc7SSreekanth Reddy 
1160c5758fc7SSreekanth Reddy /**
1161824a1566SKashyap Desai  * mpi3mr_bring_ioc_ready - Bring controller to ready state
1162824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1163824a1566SKashyap Desai  *
1164824a1566SKashyap Desai  * Set Enable IOC bit in IOC configuration register and wait for
1165824a1566SKashyap Desai  * the controller to become ready.
1166824a1566SKashyap Desai  *
116759bd9cfeSSreekanth Reddy  * Return: 0 on success, appropriate error on failure.
1168824a1566SKashyap Desai  */
1169824a1566SKashyap Desai static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
1170824a1566SKashyap Desai {
117159bd9cfeSSreekanth Reddy 	u32 ioc_config, ioc_status, timeout;
117259bd9cfeSSreekanth Reddy 	int retval = 0;
117359bd9cfeSSreekanth Reddy 	enum mpi3mr_iocstate ioc_state;
117459bd9cfeSSreekanth Reddy 	u64 base_info;
1175824a1566SKashyap Desai 
117659bd9cfeSSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
117759bd9cfeSSreekanth Reddy 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
117859bd9cfeSSreekanth Reddy 	base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
117959bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ioc_status(0x%08x), ioc_config(0x%08x), ioc_info(0x%016llx) at the bringup\n",
118059bd9cfeSSreekanth Reddy 	    ioc_status, ioc_config, base_info);
118159bd9cfeSSreekanth Reddy 
118259bd9cfeSSreekanth Reddy 	/*The timeout value is in 2sec unit, changing it to seconds*/
118359bd9cfeSSreekanth Reddy 	mrioc->ready_timeout =
118459bd9cfeSSreekanth Reddy 	    ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
118559bd9cfeSSreekanth Reddy 	    MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
118659bd9cfeSSreekanth Reddy 
118759bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ready timeout: %d seconds\n", mrioc->ready_timeout);
118859bd9cfeSSreekanth Reddy 
118959bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
119059bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "controller is in %s state during detection\n",
119159bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
119259bd9cfeSSreekanth Reddy 
119359bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
119459bd9cfeSSreekanth Reddy 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
119559bd9cfeSSreekanth Reddy 		timeout = mrioc->ready_timeout * 10;
119659bd9cfeSSreekanth Reddy 		do {
119759bd9cfeSSreekanth Reddy 			msleep(100);
119859bd9cfeSSreekanth Reddy 		} while (--timeout);
119959bd9cfeSSreekanth Reddy 
120059bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
120159bd9cfeSSreekanth Reddy 		ioc_info(mrioc,
120259bd9cfeSSreekanth Reddy 		    "controller is in %s state after waiting to reset\n",
120359bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
120459bd9cfeSSreekanth Reddy 	}
120559bd9cfeSSreekanth Reddy 
120659bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_READY) {
120759bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing message unit reset (MUR) to bring to reset state\n");
120859bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_and_process_mur(mrioc,
120959bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
121059bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
121159bd9cfeSSreekanth Reddy 		if (retval)
121259bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
121359bd9cfeSSreekanth Reddy 			    "message unit reset failed with error %d current state %s\n",
121459bd9cfeSSreekanth Reddy 			    retval, mpi3mr_iocstate_name(ioc_state));
121559bd9cfeSSreekanth Reddy 	}
121659bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
121759bd9cfeSSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
121859bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing soft reset to bring to reset state\n");
121959bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_reset(mrioc,
122059bd9cfeSSreekanth Reddy 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
122159bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
122259bd9cfeSSreekanth Reddy 		if (retval) {
122359bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
122459bd9cfeSSreekanth Reddy 			    "soft reset failed with error %d\n", retval);
122559bd9cfeSSreekanth Reddy 			goto out_failed;
122659bd9cfeSSreekanth Reddy 		}
122759bd9cfeSSreekanth Reddy 	}
122859bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
122959bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
123059bd9cfeSSreekanth Reddy 		ioc_err(mrioc,
123159bd9cfeSSreekanth Reddy 		    "cannot bring controller to reset state, current state: %s\n",
123259bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
123359bd9cfeSSreekanth Reddy 		goto out_failed;
123459bd9cfeSSreekanth Reddy 	}
123559bd9cfeSSreekanth Reddy 	mpi3mr_clear_reset_history(mrioc);
123659bd9cfeSSreekanth Reddy 	retval = mpi3mr_setup_admin_qpair(mrioc);
123759bd9cfeSSreekanth Reddy 	if (retval) {
123859bd9cfeSSreekanth Reddy 		ioc_err(mrioc, "failed to setup admin queues: error %d\n",
123959bd9cfeSSreekanth Reddy 		    retval);
124059bd9cfeSSreekanth Reddy 		goto out_failed;
124159bd9cfeSSreekanth Reddy 	}
124259bd9cfeSSreekanth Reddy 
124359bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "bringing controller to ready state\n");
1244824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1245824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1246824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1247824a1566SKashyap Desai 
1248824a1566SKashyap Desai 	timeout = mrioc->ready_timeout * 10;
1249824a1566SKashyap Desai 	do {
125059bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
125159bd9cfeSSreekanth Reddy 		if (ioc_state == MRIOC_STATE_READY) {
125259bd9cfeSSreekanth Reddy 			ioc_info(mrioc,
12535867b856SColin Ian King 			    "successfully transitioned to %s state\n",
125459bd9cfeSSreekanth Reddy 			    mpi3mr_iocstate_name(ioc_state));
1255824a1566SKashyap Desai 			return 0;
125659bd9cfeSSreekanth Reddy 		}
1257824a1566SKashyap Desai 		msleep(100);
1258824a1566SKashyap Desai 	} while (--timeout);
1259824a1566SKashyap Desai 
126059bd9cfeSSreekanth Reddy out_failed:
126159bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
126259bd9cfeSSreekanth Reddy 	ioc_err(mrioc,
126359bd9cfeSSreekanth Reddy 	    "failed to bring to ready state,  current state: %s\n",
126459bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
126559bd9cfeSSreekanth Reddy 	return retval;
1266824a1566SKashyap Desai }
1267824a1566SKashyap Desai 
1268824a1566SKashyap Desai /**
1269f061178eSKashyap Desai  * mpi3mr_soft_reset_success - Check softreset is success or not
1270f061178eSKashyap Desai  * @ioc_status: IOC status register value
1271f061178eSKashyap Desai  * @ioc_config: IOC config register value
1272f061178eSKashyap Desai  *
1273f061178eSKashyap Desai  * Check whether the soft reset is successful or not based on
1274f061178eSKashyap Desai  * IOC status and IOC config register values.
1275f061178eSKashyap Desai  *
1276f061178eSKashyap Desai  * Return: True when the soft reset is success, false otherwise.
1277f061178eSKashyap Desai  */
1278f061178eSKashyap Desai static inline bool
1279f061178eSKashyap Desai mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config)
1280f061178eSKashyap Desai {
1281f061178eSKashyap Desai 	if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1282f061178eSKashyap Desai 	    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1283f061178eSKashyap Desai 		return true;
1284f061178eSKashyap Desai 	return false;
1285f061178eSKashyap Desai }
1286f061178eSKashyap Desai 
1287f061178eSKashyap Desai /**
1288f061178eSKashyap Desai  * mpi3mr_diagfault_success - Check diag fault is success or not
1289f061178eSKashyap Desai  * @mrioc: Adapter reference
1290f061178eSKashyap Desai  * @ioc_status: IOC status register value
1291f061178eSKashyap Desai  *
1292f061178eSKashyap Desai  * Check whether the controller hit diag reset fault code.
1293f061178eSKashyap Desai  *
1294f061178eSKashyap Desai  * Return: True when there is diag fault, false otherwise.
1295f061178eSKashyap Desai  */
1296f061178eSKashyap Desai static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc,
1297f061178eSKashyap Desai 	u32 ioc_status)
1298f061178eSKashyap Desai {
1299f061178eSKashyap Desai 	u32 fault;
1300f061178eSKashyap Desai 
1301f061178eSKashyap Desai 	if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
1302f061178eSKashyap Desai 		return false;
1303f061178eSKashyap Desai 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
1304b64845a7SSreekanth Reddy 	if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) {
1305b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1306f061178eSKashyap Desai 		return true;
1307b64845a7SSreekanth Reddy 	}
1308f061178eSKashyap Desai 	return false;
1309f061178eSKashyap Desai }
1310f061178eSKashyap Desai 
1311f061178eSKashyap Desai /**
1312824a1566SKashyap Desai  * mpi3mr_set_diagsave - Set diag save bit for snapdump
1313824a1566SKashyap Desai  * @mrioc: Adapter reference
1314824a1566SKashyap Desai  *
1315824a1566SKashyap Desai  * Set diag save bit in IOC configuration register to enable
1316824a1566SKashyap Desai  * snapdump.
1317824a1566SKashyap Desai  *
1318824a1566SKashyap Desai  * Return: Nothing.
1319824a1566SKashyap Desai  */
1320824a1566SKashyap Desai static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
1321824a1566SKashyap Desai {
1322824a1566SKashyap Desai 	u32 ioc_config;
1323824a1566SKashyap Desai 
1324824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1325824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
1326824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1327824a1566SKashyap Desai }
1328824a1566SKashyap Desai 
1329824a1566SKashyap Desai /**
1330824a1566SKashyap Desai  * mpi3mr_issue_reset - Issue reset to the controller
1331824a1566SKashyap Desai  * @mrioc: Adapter reference
1332824a1566SKashyap Desai  * @reset_type: Reset type
1333824a1566SKashyap Desai  * @reset_reason: Reset reason code
1334824a1566SKashyap Desai  *
1335f061178eSKashyap Desai  * Unlock the host diagnostic registers and write the specific
1336f061178eSKashyap Desai  * reset type to that, wait for reset acknowledgment from the
1337f061178eSKashyap Desai  * controller, if the reset is not successful retry for the
1338f061178eSKashyap Desai  * predefined number of times.
1339824a1566SKashyap Desai  *
1340824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1341824a1566SKashyap Desai  */
1342824a1566SKashyap Desai static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
1343824a1566SKashyap Desai 	u32 reset_reason)
1344824a1566SKashyap Desai {
1345f061178eSKashyap Desai 	int retval = -1;
1346b64845a7SSreekanth Reddy 	u8 unlock_retry_count = 0;
1347b64845a7SSreekanth Reddy 	u32 host_diagnostic, ioc_status, ioc_config;
1348b64845a7SSreekanth Reddy 	u32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1349f061178eSKashyap Desai 
1350f061178eSKashyap Desai 	if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
1351f061178eSKashyap Desai 	    (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
1352b64845a7SSreekanth Reddy 		return retval;
1353f061178eSKashyap Desai 	if (mrioc->unrecoverable)
1354b64845a7SSreekanth Reddy 		return retval;
1355b64845a7SSreekanth Reddy 	if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) {
1356b64845a7SSreekanth Reddy 		retval = 0;
1357b64845a7SSreekanth Reddy 		return retval;
1358b64845a7SSreekanth Reddy 	}
1359b64845a7SSreekanth Reddy 
1360b64845a7SSreekanth Reddy 	ioc_info(mrioc, "%s reset due to %s(0x%x)\n",
1361b64845a7SSreekanth Reddy 	    mpi3mr_reset_type_name(reset_type),
1362b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason), reset_reason);
1363b64845a7SSreekanth Reddy 
1364f061178eSKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1365f061178eSKashyap Desai 	do {
1366f061178eSKashyap Desai 		ioc_info(mrioc,
1367f061178eSKashyap Desai 		    "Write magic sequence to unlock host diag register (retry=%d)\n",
1368f061178eSKashyap Desai 		    ++unlock_retry_count);
1369f061178eSKashyap Desai 		if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
1370b64845a7SSreekanth Reddy 			ioc_err(mrioc,
1371b64845a7SSreekanth Reddy 			    "%s reset failed due to unlock failure, host_diagnostic(0x%08x)\n",
1372b64845a7SSreekanth Reddy 			    mpi3mr_reset_type_name(reset_type),
1373b64845a7SSreekanth Reddy 			    host_diagnostic);
1374f061178eSKashyap Desai 			mrioc->unrecoverable = 1;
1375b64845a7SSreekanth Reddy 			return retval;
1376f061178eSKashyap Desai 		}
1377f061178eSKashyap Desai 
1378f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH,
1379f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1380f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST,
1381f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1382f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1383f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1384f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD,
1385f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1386f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH,
1387f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1388f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH,
1389f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1390f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH,
1391f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1392f061178eSKashyap Desai 		usleep_range(1000, 1100);
1393f061178eSKashyap Desai 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
1394f061178eSKashyap Desai 		ioc_info(mrioc,
1395f061178eSKashyap Desai 		    "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
1396f061178eSKashyap Desai 		    unlock_retry_count, host_diagnostic);
1397f061178eSKashyap Desai 	} while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
1398f061178eSKashyap Desai 
1399f061178eSKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1400f061178eSKashyap Desai 	writel(host_diagnostic | reset_type,
1401f061178eSKashyap Desai 	    &mrioc->sysif_regs->host_diagnostic);
1402b64845a7SSreekanth Reddy 	switch (reset_type) {
1403b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET:
1404f061178eSKashyap Desai 		do {
1405f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1406f061178eSKashyap Desai 			ioc_config =
1407f061178eSKashyap Desai 			    readl(&mrioc->sysif_regs->ioc_configuration);
1408b64845a7SSreekanth Reddy 			if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1409b64845a7SSreekanth Reddy 			    && mpi3mr_soft_reset_success(ioc_status, ioc_config)
1410b64845a7SSreekanth Reddy 			    ) {
1411b64845a7SSreekanth Reddy 				mpi3mr_clear_reset_history(mrioc);
1412f061178eSKashyap Desai 				retval = 0;
1413f061178eSKashyap Desai 				break;
1414f061178eSKashyap Desai 			}
1415f061178eSKashyap Desai 			msleep(100);
1416f061178eSKashyap Desai 		} while (--timeout);
1417b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1418b64845a7SSreekanth Reddy 		break;
1419b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT:
1420f061178eSKashyap Desai 		do {
1421f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1422f061178eSKashyap Desai 			if (mpi3mr_diagfault_success(mrioc, ioc_status)) {
1423f061178eSKashyap Desai 				retval = 0;
1424f061178eSKashyap Desai 				break;
1425f061178eSKashyap Desai 			}
1426f061178eSKashyap Desai 			msleep(100);
1427f061178eSKashyap Desai 		} while (--timeout);
1428b64845a7SSreekanth Reddy 		break;
1429b64845a7SSreekanth Reddy 	default:
1430b64845a7SSreekanth Reddy 		break;
1431b64845a7SSreekanth Reddy 	}
1432b64845a7SSreekanth Reddy 
1433f061178eSKashyap Desai 	writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1434f061178eSKashyap Desai 	    &mrioc->sysif_regs->write_sequence);
1435f061178eSKashyap Desai 
1436f061178eSKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1437b64845a7SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1438f061178eSKashyap Desai 	ioc_info(mrioc,
1439b64845a7SSreekanth Reddy 	    "ioc_status/ioc_onfig after %s reset is (0x%x)/(0x%x)\n",
1440f061178eSKashyap Desai 	    (!retval)?"successful":"failed", ioc_status,
1441f061178eSKashyap Desai 	    ioc_config);
1442b64845a7SSreekanth Reddy 	if (retval)
1443b64845a7SSreekanth Reddy 		mrioc->unrecoverable = 1;
1444f061178eSKashyap Desai 	return retval;
1445824a1566SKashyap Desai }
1446824a1566SKashyap Desai 
1447824a1566SKashyap Desai /**
1448824a1566SKashyap Desai  * mpi3mr_admin_request_post - Post request to admin queue
1449824a1566SKashyap Desai  * @mrioc: Adapter reference
1450824a1566SKashyap Desai  * @admin_req: MPI3 request
1451824a1566SKashyap Desai  * @admin_req_sz: Request size
1452824a1566SKashyap Desai  * @ignore_reset: Ignore reset in process
1453824a1566SKashyap Desai  *
1454824a1566SKashyap Desai  * Post the MPI3 request into admin request queue and
1455824a1566SKashyap Desai  * inform the controller, if the queue is full return
1456824a1566SKashyap Desai  * appropriate error.
1457824a1566SKashyap Desai  *
1458824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1459824a1566SKashyap Desai  */
1460824a1566SKashyap Desai int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
1461824a1566SKashyap Desai 	u16 admin_req_sz, u8 ignore_reset)
1462824a1566SKashyap Desai {
1463824a1566SKashyap Desai 	u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
1464824a1566SKashyap Desai 	int retval = 0;
1465824a1566SKashyap Desai 	unsigned long flags;
1466824a1566SKashyap Desai 	u8 *areq_entry;
1467824a1566SKashyap Desai 
1468824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1469824a1566SKashyap Desai 		ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
1470824a1566SKashyap Desai 		return -EFAULT;
1471824a1566SKashyap Desai 	}
1472824a1566SKashyap Desai 
1473824a1566SKashyap Desai 	spin_lock_irqsave(&mrioc->admin_req_lock, flags);
1474824a1566SKashyap Desai 	areq_pi = mrioc->admin_req_pi;
1475824a1566SKashyap Desai 	areq_ci = mrioc->admin_req_ci;
1476824a1566SKashyap Desai 	max_entries = mrioc->num_admin_req;
1477824a1566SKashyap Desai 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
1478824a1566SKashyap Desai 	    (areq_pi == (max_entries - 1)))) {
1479824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ full condition detected\n");
1480824a1566SKashyap Desai 		retval = -EAGAIN;
1481824a1566SKashyap Desai 		goto out;
1482824a1566SKashyap Desai 	}
1483824a1566SKashyap Desai 	if (!ignore_reset && mrioc->reset_in_progress) {
1484824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
1485824a1566SKashyap Desai 		retval = -EAGAIN;
1486824a1566SKashyap Desai 		goto out;
1487824a1566SKashyap Desai 	}
1488824a1566SKashyap Desai 	areq_entry = (u8 *)mrioc->admin_req_base +
1489824a1566SKashyap Desai 	    (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
1490824a1566SKashyap Desai 	memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
1491824a1566SKashyap Desai 	memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
1492824a1566SKashyap Desai 
1493824a1566SKashyap Desai 	if (++areq_pi == max_entries)
1494824a1566SKashyap Desai 		areq_pi = 0;
1495824a1566SKashyap Desai 	mrioc->admin_req_pi = areq_pi;
1496824a1566SKashyap Desai 
1497824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
1498824a1566SKashyap Desai 
1499824a1566SKashyap Desai out:
1500824a1566SKashyap Desai 	spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
1501824a1566SKashyap Desai 
1502824a1566SKashyap Desai 	return retval;
1503824a1566SKashyap Desai }
1504824a1566SKashyap Desai 
1505824a1566SKashyap Desai /**
1506c9566231SKashyap Desai  * mpi3mr_free_op_req_q_segments - free request memory segments
1507c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1508c9566231SKashyap Desai  * @q_idx: operational request queue index
1509c9566231SKashyap Desai  *
1510c9566231SKashyap Desai  * Free memory segments allocated for operational request queue
1511c9566231SKashyap Desai  *
1512c9566231SKashyap Desai  * Return: Nothing.
1513c9566231SKashyap Desai  */
1514c9566231SKashyap Desai static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1515c9566231SKashyap Desai {
1516c9566231SKashyap Desai 	u16 j;
1517c9566231SKashyap Desai 	int size;
1518c9566231SKashyap Desai 	struct segments *segments;
1519c9566231SKashyap Desai 
1520c9566231SKashyap Desai 	segments = mrioc->req_qinfo[q_idx].q_segments;
1521c9566231SKashyap Desai 	if (!segments)
1522c9566231SKashyap Desai 		return;
1523c9566231SKashyap Desai 
1524c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1525c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1526c9566231SKashyap Desai 		if (mrioc->req_qinfo[q_idx].q_segment_list) {
1527c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1528c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1529c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list,
1530c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list_dma);
1531d44b5fefSSreekanth Reddy 			mrioc->req_qinfo[q_idx].q_segment_list = NULL;
1532c9566231SKashyap Desai 		}
1533c9566231SKashyap Desai 	} else
1534243bcc8eSSreekanth Reddy 		size = mrioc->req_qinfo[q_idx].segment_qd *
1535c9566231SKashyap Desai 		    mrioc->facts.op_req_sz;
1536c9566231SKashyap Desai 
1537c9566231SKashyap Desai 	for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) {
1538c9566231SKashyap Desai 		if (!segments[j].segment)
1539c9566231SKashyap Desai 			continue;
1540c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1541c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1542c9566231SKashyap Desai 		segments[j].segment = NULL;
1543c9566231SKashyap Desai 	}
1544c9566231SKashyap Desai 	kfree(mrioc->req_qinfo[q_idx].q_segments);
1545c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].q_segments = NULL;
1546c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].qid = 0;
1547c9566231SKashyap Desai }
1548c9566231SKashyap Desai 
1549c9566231SKashyap Desai /**
1550c9566231SKashyap Desai  * mpi3mr_free_op_reply_q_segments - free reply memory segments
1551c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1552c9566231SKashyap Desai  * @q_idx: operational reply queue index
1553c9566231SKashyap Desai  *
1554c9566231SKashyap Desai  * Free memory segments allocated for operational reply queue
1555c9566231SKashyap Desai  *
1556c9566231SKashyap Desai  * Return: Nothing.
1557c9566231SKashyap Desai  */
1558c9566231SKashyap Desai static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1559c9566231SKashyap Desai {
1560c9566231SKashyap Desai 	u16 j;
1561c9566231SKashyap Desai 	int size;
1562c9566231SKashyap Desai 	struct segments *segments;
1563c9566231SKashyap Desai 
1564c9566231SKashyap Desai 	segments = mrioc->op_reply_qinfo[q_idx].q_segments;
1565c9566231SKashyap Desai 	if (!segments)
1566c9566231SKashyap Desai 		return;
1567c9566231SKashyap Desai 
1568c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1569c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1570c9566231SKashyap Desai 		if (mrioc->op_reply_qinfo[q_idx].q_segment_list) {
1571c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1572c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1573c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list,
1574c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list_dma);
1575c9566231SKashyap Desai 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1576c9566231SKashyap Desai 		}
1577c9566231SKashyap Desai 	} else
1578c9566231SKashyap Desai 		size = mrioc->op_reply_qinfo[q_idx].segment_qd *
1579c9566231SKashyap Desai 		    mrioc->op_reply_desc_sz;
1580c9566231SKashyap Desai 
1581c9566231SKashyap Desai 	for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) {
1582c9566231SKashyap Desai 		if (!segments[j].segment)
1583c9566231SKashyap Desai 			continue;
1584c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1585c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1586c9566231SKashyap Desai 		segments[j].segment = NULL;
1587c9566231SKashyap Desai 	}
1588c9566231SKashyap Desai 
1589c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo[q_idx].q_segments);
1590c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].q_segments = NULL;
1591c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].qid = 0;
1592c9566231SKashyap Desai }
1593c9566231SKashyap Desai 
1594c9566231SKashyap Desai /**
1595c9566231SKashyap Desai  * mpi3mr_delete_op_reply_q - delete operational reply queue
1596c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1597c9566231SKashyap Desai  * @qidx: operational reply queue index
1598c9566231SKashyap Desai  *
1599c9566231SKashyap Desai  * Delete operatinal reply queue by issuing MPI request
1600c9566231SKashyap Desai  * through admin queue.
1601c9566231SKashyap Desai  *
1602c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1603c9566231SKashyap Desai  */
1604c9566231SKashyap Desai static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1605c9566231SKashyap Desai {
1606c9566231SKashyap Desai 	struct mpi3_delete_reply_queue_request delq_req;
1607afd3a579SSreekanth Reddy 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1608c9566231SKashyap Desai 	int retval = 0;
1609c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1610c9566231SKashyap Desai 
1611afd3a579SSreekanth Reddy 	reply_qid = op_reply_q->qid;
1612c9566231SKashyap Desai 
1613c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1614c9566231SKashyap Desai 
1615c9566231SKashyap Desai 	if (!reply_qid)	{
1616c9566231SKashyap Desai 		retval = -1;
1617c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n");
1618c9566231SKashyap Desai 		goto out;
1619c9566231SKashyap Desai 	}
1620c9566231SKashyap Desai 
1621afd3a579SSreekanth Reddy 	(op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount-- :
1622afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount--;
1623afd3a579SSreekanth Reddy 
1624c9566231SKashyap Desai 	memset(&delq_req, 0, sizeof(delq_req));
1625c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1626c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1627c9566231SKashyap Desai 		retval = -1;
1628c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n");
1629c9566231SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
1630c9566231SKashyap Desai 		goto out;
1631c9566231SKashyap Desai 	}
1632c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1633c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1634c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1635c9566231SKashyap Desai 	delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1636c9566231SKashyap Desai 	delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
1637c9566231SKashyap Desai 	delq_req.queue_id = cpu_to_le16(reply_qid);
1638c9566231SKashyap Desai 
1639c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1640c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req),
1641c9566231SKashyap Desai 	    1);
1642c9566231SKashyap Desai 	if (retval) {
1643c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n");
1644c9566231SKashyap Desai 		goto out_unlock;
1645c9566231SKashyap Desai 	}
1646c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1647c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1648c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1649a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "delete reply queue timed out\n");
1650a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1651c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
1652c9566231SKashyap Desai 		retval = -1;
1653c9566231SKashyap Desai 		goto out_unlock;
1654c9566231SKashyap Desai 	}
1655c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1656c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1657c9566231SKashyap Desai 		ioc_err(mrioc,
1658c9566231SKashyap Desai 		    "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1659c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1660c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1661c9566231SKashyap Desai 		retval = -1;
1662c9566231SKashyap Desai 		goto out_unlock;
1663c9566231SKashyap Desai 	}
1664c9566231SKashyap Desai 	mrioc->intr_info[midx].op_reply_q = NULL;
1665c9566231SKashyap Desai 
1666c9566231SKashyap Desai 	mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1667c9566231SKashyap Desai out_unlock:
1668c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1669c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1670c9566231SKashyap Desai out:
1671c9566231SKashyap Desai 
1672c9566231SKashyap Desai 	return retval;
1673c9566231SKashyap Desai }
1674c9566231SKashyap Desai 
1675c9566231SKashyap Desai /**
1676c9566231SKashyap Desai  * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool
1677c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1678c9566231SKashyap Desai  * @qidx: request queue index
1679c9566231SKashyap Desai  *
1680c9566231SKashyap Desai  * Allocate segmented memory pools for operational reply
1681c9566231SKashyap Desai  * queue.
1682c9566231SKashyap Desai  *
1683c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1684c9566231SKashyap Desai  */
1685c9566231SKashyap Desai static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1686c9566231SKashyap Desai {
1687c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1688c9566231SKashyap Desai 	int i, size;
1689c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1690c9566231SKashyap Desai 	struct segments *segments;
1691c9566231SKashyap Desai 
1692c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1693c9566231SKashyap Desai 		op_reply_q->segment_qd =
1694c9566231SKashyap Desai 		    MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz;
1695c9566231SKashyap Desai 
1696c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1697c9566231SKashyap Desai 
1698c9566231SKashyap Desai 		op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1699c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma,
1700c9566231SKashyap Desai 		    GFP_KERNEL);
1701c9566231SKashyap Desai 		if (!op_reply_q->q_segment_list)
1702c9566231SKashyap Desai 			return -ENOMEM;
1703c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_reply_q->q_segment_list;
1704c9566231SKashyap Desai 	} else {
1705c9566231SKashyap Desai 		op_reply_q->segment_qd = op_reply_q->num_replies;
1706c9566231SKashyap Desai 		size = op_reply_q->num_replies * mrioc->op_reply_desc_sz;
1707c9566231SKashyap Desai 	}
1708c9566231SKashyap Desai 
1709c9566231SKashyap Desai 	op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies,
1710c9566231SKashyap Desai 	    op_reply_q->segment_qd);
1711c9566231SKashyap Desai 
1712c9566231SKashyap Desai 	op_reply_q->q_segments = kcalloc(op_reply_q->num_segments,
1713c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1714c9566231SKashyap Desai 	if (!op_reply_q->q_segments)
1715c9566231SKashyap Desai 		return -ENOMEM;
1716c9566231SKashyap Desai 
1717c9566231SKashyap Desai 	segments = op_reply_q->q_segments;
1718c9566231SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++) {
1719c9566231SKashyap Desai 		segments[i].segment =
1720c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1721c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1722c9566231SKashyap Desai 		if (!segments[i].segment)
1723c9566231SKashyap Desai 			return -ENOMEM;
1724c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1725c9566231SKashyap Desai 			q_segment_list_entry[i] =
1726c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1727c9566231SKashyap Desai 	}
1728c9566231SKashyap Desai 
1729c9566231SKashyap Desai 	return 0;
1730c9566231SKashyap Desai }
1731c9566231SKashyap Desai 
1732c9566231SKashyap Desai /**
1733c9566231SKashyap Desai  * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool.
1734c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1735c9566231SKashyap Desai  * @qidx: request queue index
1736c9566231SKashyap Desai  *
1737c9566231SKashyap Desai  * Allocate segmented memory pools for operational request
1738c9566231SKashyap Desai  * queue.
1739c9566231SKashyap Desai  *
1740c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1741c9566231SKashyap Desai  */
1742c9566231SKashyap Desai static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1743c9566231SKashyap Desai {
1744c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
1745c9566231SKashyap Desai 	int i, size;
1746c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1747c9566231SKashyap Desai 	struct segments *segments;
1748c9566231SKashyap Desai 
1749c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1750c9566231SKashyap Desai 		op_req_q->segment_qd =
1751c9566231SKashyap Desai 		    MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz;
1752c9566231SKashyap Desai 
1753c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1754c9566231SKashyap Desai 
1755c9566231SKashyap Desai 		op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1756c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma,
1757c9566231SKashyap Desai 		    GFP_KERNEL);
1758c9566231SKashyap Desai 		if (!op_req_q->q_segment_list)
1759c9566231SKashyap Desai 			return -ENOMEM;
1760c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_req_q->q_segment_list;
1761c9566231SKashyap Desai 
1762c9566231SKashyap Desai 	} else {
1763c9566231SKashyap Desai 		op_req_q->segment_qd = op_req_q->num_requests;
1764c9566231SKashyap Desai 		size = op_req_q->num_requests * mrioc->facts.op_req_sz;
1765c9566231SKashyap Desai 	}
1766c9566231SKashyap Desai 
1767c9566231SKashyap Desai 	op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests,
1768c9566231SKashyap Desai 	    op_req_q->segment_qd);
1769c9566231SKashyap Desai 
1770c9566231SKashyap Desai 	op_req_q->q_segments = kcalloc(op_req_q->num_segments,
1771c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1772c9566231SKashyap Desai 	if (!op_req_q->q_segments)
1773c9566231SKashyap Desai 		return -ENOMEM;
1774c9566231SKashyap Desai 
1775c9566231SKashyap Desai 	segments = op_req_q->q_segments;
1776c9566231SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++) {
1777c9566231SKashyap Desai 		segments[i].segment =
1778c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1779c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1780c9566231SKashyap Desai 		if (!segments[i].segment)
1781c9566231SKashyap Desai 			return -ENOMEM;
1782c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1783c9566231SKashyap Desai 			q_segment_list_entry[i] =
1784c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1785c9566231SKashyap Desai 	}
1786c9566231SKashyap Desai 
1787c9566231SKashyap Desai 	return 0;
1788c9566231SKashyap Desai }
1789c9566231SKashyap Desai 
1790c9566231SKashyap Desai /**
1791c9566231SKashyap Desai  * mpi3mr_create_op_reply_q - create operational reply queue
1792c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1793c9566231SKashyap Desai  * @qidx: operational reply queue index
1794c9566231SKashyap Desai  *
1795c9566231SKashyap Desai  * Create operatinal reply queue by issuing MPI request
1796c9566231SKashyap Desai  * through admin queue.
1797c9566231SKashyap Desai  *
1798c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1799c9566231SKashyap Desai  */
1800c9566231SKashyap Desai static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1801c9566231SKashyap Desai {
1802c9566231SKashyap Desai 	struct mpi3_create_reply_queue_request create_req;
1803c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1804c9566231SKashyap Desai 	int retval = 0;
1805c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1806c9566231SKashyap Desai 
1807c9566231SKashyap Desai 	reply_qid = op_reply_q->qid;
1808c9566231SKashyap Desai 
1809c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1810c9566231SKashyap Desai 
1811c9566231SKashyap Desai 	if (reply_qid) {
1812c9566231SKashyap Desai 		retval = -1;
1813c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n",
1814c9566231SKashyap Desai 		    reply_qid);
1815c9566231SKashyap Desai 
1816c9566231SKashyap Desai 		return retval;
1817c9566231SKashyap Desai 	}
1818c9566231SKashyap Desai 
1819c9566231SKashyap Desai 	reply_qid = qidx + 1;
1820c9566231SKashyap Desai 	op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
1821243bcc8eSSreekanth Reddy 	if (!mrioc->pdev->revision)
1822243bcc8eSSreekanth Reddy 		op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD4K;
1823c9566231SKashyap Desai 	op_reply_q->ci = 0;
1824c9566231SKashyap Desai 	op_reply_q->ephase = 1;
1825463429f8SKashyap Desai 	atomic_set(&op_reply_q->pend_ios, 0);
1826463429f8SKashyap Desai 	atomic_set(&op_reply_q->in_use, 0);
1827463429f8SKashyap Desai 	op_reply_q->enable_irq_poll = false;
1828c9566231SKashyap Desai 
1829c9566231SKashyap Desai 	if (!op_reply_q->q_segments) {
1830c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx);
1831c9566231SKashyap Desai 		if (retval) {
1832c9566231SKashyap Desai 			mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1833c9566231SKashyap Desai 			goto out;
1834c9566231SKashyap Desai 		}
1835c9566231SKashyap Desai 	}
1836c9566231SKashyap Desai 
1837c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1838c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1839c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1840c9566231SKashyap Desai 		retval = -1;
1841c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Init command is in use\n");
1842f9dc034dSYang Yingliang 		goto out_unlock;
1843c9566231SKashyap Desai 	}
1844c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1845c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1846c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1847c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1848c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
1849c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(reply_qid);
1850afd3a579SSreekanth Reddy 
1851afd3a579SSreekanth Reddy 	if (midx < (mrioc->intr_info_count - mrioc->requested_poll_qcount))
1852afd3a579SSreekanth Reddy 		op_reply_q->qtype = MPI3MR_DEFAULT_QUEUE;
1853afd3a579SSreekanth Reddy 	else
1854afd3a579SSreekanth Reddy 		op_reply_q->qtype = MPI3MR_POLL_QUEUE;
1855afd3a579SSreekanth Reddy 
1856afd3a579SSreekanth Reddy 	if (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) {
1857afd3a579SSreekanth Reddy 		create_req.flags =
1858afd3a579SSreekanth Reddy 			MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
1859afd3a579SSreekanth Reddy 		create_req.msix_index =
1860afd3a579SSreekanth Reddy 			cpu_to_le16(mrioc->intr_info[midx].msix_index);
1861afd3a579SSreekanth Reddy 	} else {
1862afd3a579SSreekanth Reddy 		create_req.msix_index = cpu_to_le16(mrioc->intr_info_count - 1);
1863afd3a579SSreekanth Reddy 		ioc_info(mrioc, "create reply queue(polled): for qid(%d), midx(%d)\n",
1864afd3a579SSreekanth Reddy 			reply_qid, midx);
1865afd3a579SSreekanth Reddy 		if (!mrioc->active_poll_qcount)
1866afd3a579SSreekanth Reddy 			disable_irq_nosync(pci_irq_vector(mrioc->pdev,
1867afd3a579SSreekanth Reddy 			    mrioc->intr_info_count - 1));
1868afd3a579SSreekanth Reddy 	}
1869afd3a579SSreekanth Reddy 
1870c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1871c9566231SKashyap Desai 		create_req.flags |=
1872c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1873c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1874c9566231SKashyap Desai 		    op_reply_q->q_segment_list_dma);
1875c9566231SKashyap Desai 	} else
1876c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1877c9566231SKashyap Desai 		    op_reply_q->q_segments[0].segment_dma);
1878c9566231SKashyap Desai 
1879c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_reply_q->num_replies);
1880c9566231SKashyap Desai 
1881c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1882c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1883c9566231SKashyap Desai 	    sizeof(create_req), 1);
1884c9566231SKashyap Desai 	if (retval) {
1885c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Admin Post failed\n");
1886c9566231SKashyap Desai 		goto out_unlock;
1887c9566231SKashyap Desai 	}
1888c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1889c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1890c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1891a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create reply queue timed out\n");
1892a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1893c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
1894c9566231SKashyap Desai 		retval = -1;
1895c9566231SKashyap Desai 		goto out_unlock;
1896c9566231SKashyap Desai 	}
1897c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1898c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1899c9566231SKashyap Desai 		ioc_err(mrioc,
1900c9566231SKashyap Desai 		    "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1901c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1902c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1903c9566231SKashyap Desai 		retval = -1;
1904c9566231SKashyap Desai 		goto out_unlock;
1905c9566231SKashyap Desai 	}
1906c9566231SKashyap Desai 	op_reply_q->qid = reply_qid;
1907fe6db615SSreekanth Reddy 	if (midx < mrioc->intr_info_count)
1908c9566231SKashyap Desai 		mrioc->intr_info[midx].op_reply_q = op_reply_q;
1909c9566231SKashyap Desai 
1910afd3a579SSreekanth Reddy 	(op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount++ :
1911afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount++;
1912afd3a579SSreekanth Reddy 
1913c9566231SKashyap Desai out_unlock:
1914c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1915c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1916c9566231SKashyap Desai out:
1917c9566231SKashyap Desai 
1918c9566231SKashyap Desai 	return retval;
1919c9566231SKashyap Desai }
1920c9566231SKashyap Desai 
1921c9566231SKashyap Desai /**
1922c9566231SKashyap Desai  * mpi3mr_create_op_req_q - create operational request queue
1923c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1924c9566231SKashyap Desai  * @idx: operational request queue index
1925c9566231SKashyap Desai  * @reply_qid: Reply queue ID
1926c9566231SKashyap Desai  *
1927c9566231SKashyap Desai  * Create operatinal request queue by issuing MPI request
1928c9566231SKashyap Desai  * through admin queue.
1929c9566231SKashyap Desai  *
1930c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1931c9566231SKashyap Desai  */
1932c9566231SKashyap Desai static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx,
1933c9566231SKashyap Desai 	u16 reply_qid)
1934c9566231SKashyap Desai {
1935c9566231SKashyap Desai 	struct mpi3_create_request_queue_request create_req;
1936c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx;
1937c9566231SKashyap Desai 	int retval = 0;
1938c9566231SKashyap Desai 	u16 req_qid = 0;
1939c9566231SKashyap Desai 
1940c9566231SKashyap Desai 	req_qid = op_req_q->qid;
1941c9566231SKashyap Desai 
1942c9566231SKashyap Desai 	if (req_qid) {
1943c9566231SKashyap Desai 		retval = -1;
1944c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n",
1945c9566231SKashyap Desai 		    req_qid);
1946c9566231SKashyap Desai 
1947c9566231SKashyap Desai 		return retval;
1948c9566231SKashyap Desai 	}
1949c9566231SKashyap Desai 	req_qid = idx + 1;
1950c9566231SKashyap Desai 
1951c9566231SKashyap Desai 	op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD;
1952c9566231SKashyap Desai 	op_req_q->ci = 0;
1953c9566231SKashyap Desai 	op_req_q->pi = 0;
1954c9566231SKashyap Desai 	op_req_q->reply_qid = reply_qid;
1955c9566231SKashyap Desai 	spin_lock_init(&op_req_q->q_lock);
1956c9566231SKashyap Desai 
1957c9566231SKashyap Desai 	if (!op_req_q->q_segments) {
1958c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx);
1959c9566231SKashyap Desai 		if (retval) {
1960c9566231SKashyap Desai 			mpi3mr_free_op_req_q_segments(mrioc, idx);
1961c9566231SKashyap Desai 			goto out;
1962c9566231SKashyap Desai 		}
1963c9566231SKashyap Desai 	}
1964c9566231SKashyap Desai 
1965c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1966c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1967c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1968c9566231SKashyap Desai 		retval = -1;
1969c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Init command is in use\n");
1970f9dc034dSYang Yingliang 		goto out_unlock;
1971c9566231SKashyap Desai 	}
1972c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1973c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1974c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1975c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1976c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
1977c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(req_qid);
1978c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1979c9566231SKashyap Desai 		create_req.flags =
1980c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1981c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1982c9566231SKashyap Desai 		    op_req_q->q_segment_list_dma);
1983c9566231SKashyap Desai 	} else
1984c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1985c9566231SKashyap Desai 		    op_req_q->q_segments[0].segment_dma);
1986c9566231SKashyap Desai 	create_req.reply_queue_id = cpu_to_le16(reply_qid);
1987c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_req_q->num_requests);
1988c9566231SKashyap Desai 
1989c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1990c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1991c9566231SKashyap Desai 	    sizeof(create_req), 1);
1992c9566231SKashyap Desai 	if (retval) {
1993c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Admin Post failed\n");
1994c9566231SKashyap Desai 		goto out_unlock;
1995c9566231SKashyap Desai 	}
1996c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1997c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1998c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1999a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create request queue timed out\n");
2000a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2001a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
2002c9566231SKashyap Desai 		retval = -1;
2003c9566231SKashyap Desai 		goto out_unlock;
2004c9566231SKashyap Desai 	}
2005c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2006c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2007c9566231SKashyap Desai 		ioc_err(mrioc,
2008c9566231SKashyap Desai 		    "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2009c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2010c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2011c9566231SKashyap Desai 		retval = -1;
2012c9566231SKashyap Desai 		goto out_unlock;
2013c9566231SKashyap Desai 	}
2014c9566231SKashyap Desai 	op_req_q->qid = req_qid;
2015c9566231SKashyap Desai 
2016c9566231SKashyap Desai out_unlock:
2017c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2018c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2019c9566231SKashyap Desai out:
2020c9566231SKashyap Desai 
2021c9566231SKashyap Desai 	return retval;
2022c9566231SKashyap Desai }
2023c9566231SKashyap Desai 
2024c9566231SKashyap Desai /**
2025c9566231SKashyap Desai  * mpi3mr_create_op_queues - create operational queue pairs
2026c9566231SKashyap Desai  * @mrioc: Adapter instance reference
2027c9566231SKashyap Desai  *
2028c9566231SKashyap Desai  * Allocate memory for operational queue meta data and call
2029c9566231SKashyap Desai  * create request and reply queue functions.
2030c9566231SKashyap Desai  *
2031c9566231SKashyap Desai  * Return: 0 on success, non-zero on failures.
2032c9566231SKashyap Desai  */
2033c9566231SKashyap Desai static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc)
2034c9566231SKashyap Desai {
2035c9566231SKashyap Desai 	int retval = 0;
2036c9566231SKashyap Desai 	u16 num_queues = 0, i = 0, msix_count_op_q = 1;
2037c9566231SKashyap Desai 
2038c9566231SKashyap Desai 	num_queues = min_t(int, mrioc->facts.max_op_reply_q,
2039c9566231SKashyap Desai 	    mrioc->facts.max_op_req_q);
2040c9566231SKashyap Desai 
2041c9566231SKashyap Desai 	msix_count_op_q =
2042c9566231SKashyap Desai 	    mrioc->intr_info_count - mrioc->op_reply_q_offset;
2043c9566231SKashyap Desai 	if (!mrioc->num_queues)
2044c9566231SKashyap Desai 		mrioc->num_queues = min_t(int, num_queues, msix_count_op_q);
2045c5758fc7SSreekanth Reddy 	/*
2046c5758fc7SSreekanth Reddy 	 * During reset set the num_queues to the number of queues
2047c5758fc7SSreekanth Reddy 	 * that was set before the reset.
2048c5758fc7SSreekanth Reddy 	 */
2049c5758fc7SSreekanth Reddy 	num_queues = mrioc->num_op_reply_q ?
2050c5758fc7SSreekanth Reddy 	    mrioc->num_op_reply_q : mrioc->num_queues;
2051c5758fc7SSreekanth Reddy 	ioc_info(mrioc, "trying to create %d operational queue pairs\n",
2052c9566231SKashyap Desai 	    num_queues);
2053c9566231SKashyap Desai 
2054c9566231SKashyap Desai 	if (!mrioc->req_qinfo) {
2055c9566231SKashyap Desai 		mrioc->req_qinfo = kcalloc(num_queues,
2056c9566231SKashyap Desai 		    sizeof(struct op_req_qinfo), GFP_KERNEL);
2057c9566231SKashyap Desai 		if (!mrioc->req_qinfo) {
2058c9566231SKashyap Desai 			retval = -1;
2059c9566231SKashyap Desai 			goto out_failed;
2060c9566231SKashyap Desai 		}
2061c9566231SKashyap Desai 
2062c9566231SKashyap Desai 		mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) *
2063c9566231SKashyap Desai 		    num_queues, GFP_KERNEL);
2064c9566231SKashyap Desai 		if (!mrioc->op_reply_qinfo) {
2065c9566231SKashyap Desai 			retval = -1;
2066c9566231SKashyap Desai 			goto out_failed;
2067c9566231SKashyap Desai 		}
2068c9566231SKashyap Desai 	}
2069c9566231SKashyap Desai 
2070c9566231SKashyap Desai 	if (mrioc->enable_segqueue)
2071c9566231SKashyap Desai 		ioc_info(mrioc,
2072c9566231SKashyap Desai 		    "allocating operational queues through segmented queues\n");
2073c9566231SKashyap Desai 
2074c9566231SKashyap Desai 	for (i = 0; i < num_queues; i++) {
2075c9566231SKashyap Desai 		if (mpi3mr_create_op_reply_q(mrioc, i)) {
2076c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP RepQ %d\n", i);
2077c9566231SKashyap Desai 			break;
2078c9566231SKashyap Desai 		}
2079c9566231SKashyap Desai 		if (mpi3mr_create_op_req_q(mrioc, i,
2080c9566231SKashyap Desai 		    mrioc->op_reply_qinfo[i].qid)) {
2081c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i);
2082c9566231SKashyap Desai 			mpi3mr_delete_op_reply_q(mrioc, i);
2083c9566231SKashyap Desai 			break;
2084c9566231SKashyap Desai 		}
2085c9566231SKashyap Desai 	}
2086c9566231SKashyap Desai 
2087c9566231SKashyap Desai 	if (i == 0) {
2088c9566231SKashyap Desai 		/* Not even one queue is created successfully*/
2089c9566231SKashyap Desai 		retval = -1;
2090c9566231SKashyap Desai 		goto out_failed;
2091c9566231SKashyap Desai 	}
2092c9566231SKashyap Desai 	mrioc->num_op_reply_q = mrioc->num_op_req_q = i;
2093afd3a579SSreekanth Reddy 	ioc_info(mrioc,
2094afd3a579SSreekanth Reddy 	    "successfully created %d operational queue pairs(default/polled) queue = (%d/%d)\n",
2095afd3a579SSreekanth Reddy 	    mrioc->num_op_reply_q, mrioc->default_qcount,
2096afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount);
2097c9566231SKashyap Desai 
2098c9566231SKashyap Desai 	return retval;
2099c9566231SKashyap Desai out_failed:
2100c9566231SKashyap Desai 	kfree(mrioc->req_qinfo);
2101c9566231SKashyap Desai 	mrioc->req_qinfo = NULL;
2102c9566231SKashyap Desai 
2103c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
2104c9566231SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
2105c9566231SKashyap Desai 
2106c9566231SKashyap Desai 	return retval;
2107c9566231SKashyap Desai }
2108c9566231SKashyap Desai 
2109c9566231SKashyap Desai /**
2110023ab2a9SKashyap Desai  * mpi3mr_op_request_post - Post request to operational queue
2111023ab2a9SKashyap Desai  * @mrioc: Adapter reference
2112023ab2a9SKashyap Desai  * @op_req_q: Operational request queue info
2113023ab2a9SKashyap Desai  * @req: MPI3 request
2114023ab2a9SKashyap Desai  *
2115023ab2a9SKashyap Desai  * Post the MPI3 request into operational request queue and
2116023ab2a9SKashyap Desai  * inform the controller, if the queue is full return
2117023ab2a9SKashyap Desai  * appropriate error.
2118023ab2a9SKashyap Desai  *
2119023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failure.
2120023ab2a9SKashyap Desai  */
2121023ab2a9SKashyap Desai int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc,
2122023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q, u8 *req)
2123023ab2a9SKashyap Desai {
2124023ab2a9SKashyap Desai 	u16 pi = 0, max_entries, reply_qidx = 0, midx;
2125023ab2a9SKashyap Desai 	int retval = 0;
2126023ab2a9SKashyap Desai 	unsigned long flags;
2127023ab2a9SKashyap Desai 	u8 *req_entry;
2128023ab2a9SKashyap Desai 	void *segment_base_addr;
2129023ab2a9SKashyap Desai 	u16 req_sz = mrioc->facts.op_req_sz;
2130023ab2a9SKashyap Desai 	struct segments *segments = op_req_q->q_segments;
2131023ab2a9SKashyap Desai 
2132023ab2a9SKashyap Desai 	reply_qidx = op_req_q->reply_qid - 1;
2133023ab2a9SKashyap Desai 
2134023ab2a9SKashyap Desai 	if (mrioc->unrecoverable)
2135023ab2a9SKashyap Desai 		return -EFAULT;
2136023ab2a9SKashyap Desai 
2137023ab2a9SKashyap Desai 	spin_lock_irqsave(&op_req_q->q_lock, flags);
2138023ab2a9SKashyap Desai 	pi = op_req_q->pi;
2139023ab2a9SKashyap Desai 	max_entries = op_req_q->num_requests;
2140023ab2a9SKashyap Desai 
2141023ab2a9SKashyap Desai 	if (mpi3mr_check_req_qfull(op_req_q)) {
2142023ab2a9SKashyap Desai 		midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(
2143023ab2a9SKashyap Desai 		    reply_qidx, mrioc->op_reply_q_offset);
2144afd3a579SSreekanth Reddy 		mpi3mr_process_op_reply_q(mrioc, mrioc->intr_info[midx].op_reply_q);
2145023ab2a9SKashyap Desai 
2146023ab2a9SKashyap Desai 		if (mpi3mr_check_req_qfull(op_req_q)) {
2147023ab2a9SKashyap Desai 			retval = -EAGAIN;
2148023ab2a9SKashyap Desai 			goto out;
2149023ab2a9SKashyap Desai 		}
2150023ab2a9SKashyap Desai 	}
2151023ab2a9SKashyap Desai 
2152023ab2a9SKashyap Desai 	if (mrioc->reset_in_progress) {
2153023ab2a9SKashyap Desai 		ioc_err(mrioc, "OpReqQ submit reset in progress\n");
2154023ab2a9SKashyap Desai 		retval = -EAGAIN;
2155023ab2a9SKashyap Desai 		goto out;
2156023ab2a9SKashyap Desai 	}
2157023ab2a9SKashyap Desai 
2158023ab2a9SKashyap Desai 	segment_base_addr = segments[pi / op_req_q->segment_qd].segment;
2159023ab2a9SKashyap Desai 	req_entry = (u8 *)segment_base_addr +
2160023ab2a9SKashyap Desai 	    ((pi % op_req_q->segment_qd) * req_sz);
2161023ab2a9SKashyap Desai 
2162023ab2a9SKashyap Desai 	memset(req_entry, 0, req_sz);
2163023ab2a9SKashyap Desai 	memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ);
2164023ab2a9SKashyap Desai 
2165023ab2a9SKashyap Desai 	if (++pi == max_entries)
2166023ab2a9SKashyap Desai 		pi = 0;
2167023ab2a9SKashyap Desai 	op_req_q->pi = pi;
2168023ab2a9SKashyap Desai 
2169463429f8SKashyap Desai 	if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios)
2170463429f8SKashyap Desai 	    > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT)
2171463429f8SKashyap Desai 		mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true;
2172463429f8SKashyap Desai 
2173023ab2a9SKashyap Desai 	writel(op_req_q->pi,
2174023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index);
2175023ab2a9SKashyap Desai 
2176023ab2a9SKashyap Desai out:
2177023ab2a9SKashyap Desai 	spin_unlock_irqrestore(&op_req_q->q_lock, flags);
2178023ab2a9SKashyap Desai 	return retval;
2179023ab2a9SKashyap Desai }
2180023ab2a9SKashyap Desai 
2181023ab2a9SKashyap Desai /**
2182a6856cc4SSreekanth Reddy  * mpi3mr_check_rh_fault_ioc - check reset history and fault
2183a6856cc4SSreekanth Reddy  * controller
2184a6856cc4SSreekanth Reddy  * @mrioc: Adapter instance reference
21853bb3c24eSYang Li  * @reason_code: reason code for the fault.
2186a6856cc4SSreekanth Reddy  *
2187a6856cc4SSreekanth Reddy  * This routine will save snapdump and fault the controller with
2188a6856cc4SSreekanth Reddy  * the given reason code if it is not already in the fault or
2189a6856cc4SSreekanth Reddy  * not asynchronosuly reset. This will be used to handle
2190a6856cc4SSreekanth Reddy  * initilaization time faults/resets/timeout as in those cases
2191a6856cc4SSreekanth Reddy  * immediate soft reset invocation is not required.
2192a6856cc4SSreekanth Reddy  *
2193a6856cc4SSreekanth Reddy  * Return:  None.
2194a6856cc4SSreekanth Reddy  */
2195a6856cc4SSreekanth Reddy void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code)
2196a6856cc4SSreekanth Reddy {
2197a6856cc4SSreekanth Reddy 	u32 ioc_status, host_diagnostic, timeout;
2198a6856cc4SSreekanth Reddy 
2199a6856cc4SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
2200a6856cc4SSreekanth Reddy 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
2201a6856cc4SSreekanth Reddy 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
2202a6856cc4SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
2203a6856cc4SSreekanth Reddy 		return;
2204a6856cc4SSreekanth Reddy 	}
2205a6856cc4SSreekanth Reddy 	mpi3mr_set_diagsave(mrioc);
2206a6856cc4SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
2207a6856cc4SSreekanth Reddy 	    reason_code);
2208a6856cc4SSreekanth Reddy 	timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
2209a6856cc4SSreekanth Reddy 	do {
2210a6856cc4SSreekanth Reddy 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2211a6856cc4SSreekanth Reddy 		if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
2212a6856cc4SSreekanth Reddy 			break;
2213a6856cc4SSreekanth Reddy 		msleep(100);
2214a6856cc4SSreekanth Reddy 	} while (--timeout);
2215a6856cc4SSreekanth Reddy }
2216a6856cc4SSreekanth Reddy 
2217a6856cc4SSreekanth Reddy /**
221854dfcffbSKashyap Desai  * mpi3mr_sync_timestamp - Issue time stamp sync request
221954dfcffbSKashyap Desai  * @mrioc: Adapter reference
222054dfcffbSKashyap Desai  *
222154dfcffbSKashyap Desai  * Issue IO unit control MPI request to synchornize firmware
222254dfcffbSKashyap Desai  * timestamp with host time.
222354dfcffbSKashyap Desai  *
222454dfcffbSKashyap Desai  * Return: 0 on success, non-zero on failure.
222554dfcffbSKashyap Desai  */
222654dfcffbSKashyap Desai static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc)
222754dfcffbSKashyap Desai {
222854dfcffbSKashyap Desai 	ktime_t current_time;
222954dfcffbSKashyap Desai 	struct mpi3_iounit_control_request iou_ctrl;
223054dfcffbSKashyap Desai 	int retval = 0;
223154dfcffbSKashyap Desai 
223254dfcffbSKashyap Desai 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
223354dfcffbSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
223454dfcffbSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
223554dfcffbSKashyap Desai 		retval = -1;
223654dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n");
223754dfcffbSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
223854dfcffbSKashyap Desai 		goto out;
223954dfcffbSKashyap Desai 	}
224054dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
224154dfcffbSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
224254dfcffbSKashyap Desai 	mrioc->init_cmds.callback = NULL;
224354dfcffbSKashyap Desai 	iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
224454dfcffbSKashyap Desai 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
224554dfcffbSKashyap Desai 	iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP;
224654dfcffbSKashyap Desai 	current_time = ktime_get_real();
224754dfcffbSKashyap Desai 	iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time));
224854dfcffbSKashyap Desai 
224954dfcffbSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
225054dfcffbSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl,
225154dfcffbSKashyap Desai 	    sizeof(iou_ctrl), 0);
225254dfcffbSKashyap Desai 	if (retval) {
225354dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n");
225454dfcffbSKashyap Desai 		goto out_unlock;
225554dfcffbSKashyap Desai 	}
225654dfcffbSKashyap Desai 
225754dfcffbSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
225854dfcffbSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
225954dfcffbSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
226054dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n");
226154dfcffbSKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
2262fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
226354dfcffbSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
226454dfcffbSKashyap Desai 			    MPI3MR_RESET_FROM_TSU_TIMEOUT, 1);
226554dfcffbSKashyap Desai 		retval = -1;
226654dfcffbSKashyap Desai 		goto out_unlock;
226754dfcffbSKashyap Desai 	}
226854dfcffbSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
226954dfcffbSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
227054dfcffbSKashyap Desai 		ioc_err(mrioc,
227154dfcffbSKashyap Desai 		    "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
227254dfcffbSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
227354dfcffbSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
227454dfcffbSKashyap Desai 		retval = -1;
227554dfcffbSKashyap Desai 		goto out_unlock;
227654dfcffbSKashyap Desai 	}
227754dfcffbSKashyap Desai 
227854dfcffbSKashyap Desai out_unlock:
227954dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
228054dfcffbSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
228154dfcffbSKashyap Desai 
228254dfcffbSKashyap Desai out:
228354dfcffbSKashyap Desai 	return retval;
228454dfcffbSKashyap Desai }
228554dfcffbSKashyap Desai 
228654dfcffbSKashyap Desai /**
22872ac794baSSreekanth Reddy  * mpi3mr_print_pkg_ver - display controller fw package version
22882ac794baSSreekanth Reddy  * @mrioc: Adapter reference
22892ac794baSSreekanth Reddy  *
22902ac794baSSreekanth Reddy  * Retrieve firmware package version from the component image
22912ac794baSSreekanth Reddy  * header of the controller flash and display it.
22922ac794baSSreekanth Reddy  *
22932ac794baSSreekanth Reddy  * Return: 0 on success and non-zero on failure.
22942ac794baSSreekanth Reddy  */
22952ac794baSSreekanth Reddy static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc)
22962ac794baSSreekanth Reddy {
22972ac794baSSreekanth Reddy 	struct mpi3_ci_upload_request ci_upload;
22982ac794baSSreekanth Reddy 	int retval = -1;
22992ac794baSSreekanth Reddy 	void *data = NULL;
23002ac794baSSreekanth Reddy 	dma_addr_t data_dma;
23012ac794baSSreekanth Reddy 	struct mpi3_ci_manifest_mpi *manifest;
23022ac794baSSreekanth Reddy 	u32 data_len = sizeof(struct mpi3_ci_manifest_mpi);
23032ac794baSSreekanth Reddy 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
23042ac794baSSreekanth Reddy 
23052ac794baSSreekanth Reddy 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
23062ac794baSSreekanth Reddy 	    GFP_KERNEL);
23072ac794baSSreekanth Reddy 	if (!data)
23082ac794baSSreekanth Reddy 		return -ENOMEM;
23092ac794baSSreekanth Reddy 
23102ac794baSSreekanth Reddy 	memset(&ci_upload, 0, sizeof(ci_upload));
23112ac794baSSreekanth Reddy 	mutex_lock(&mrioc->init_cmds.mutex);
23122ac794baSSreekanth Reddy 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
23132ac794baSSreekanth Reddy 		ioc_err(mrioc, "sending get package version failed due to command in use\n");
23142ac794baSSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
23152ac794baSSreekanth Reddy 		goto out;
23162ac794baSSreekanth Reddy 	}
23172ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
23182ac794baSSreekanth Reddy 	mrioc->init_cmds.is_waiting = 1;
23192ac794baSSreekanth Reddy 	mrioc->init_cmds.callback = NULL;
23202ac794baSSreekanth Reddy 	ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
23212ac794baSSreekanth Reddy 	ci_upload.function = MPI3_FUNCTION_CI_UPLOAD;
23222ac794baSSreekanth Reddy 	ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
23232ac794baSSreekanth Reddy 	ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST);
23242ac794baSSreekanth Reddy 	ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE);
23252ac794baSSreekanth Reddy 	ci_upload.segment_size = cpu_to_le32(data_len);
23262ac794baSSreekanth Reddy 
23272ac794baSSreekanth Reddy 	mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len,
23282ac794baSSreekanth Reddy 	    data_dma);
23292ac794baSSreekanth Reddy 	init_completion(&mrioc->init_cmds.done);
23302ac794baSSreekanth Reddy 	retval = mpi3mr_admin_request_post(mrioc, &ci_upload,
23312ac794baSSreekanth Reddy 	    sizeof(ci_upload), 1);
23322ac794baSSreekanth Reddy 	if (retval) {
23332ac794baSSreekanth Reddy 		ioc_err(mrioc, "posting get package version failed\n");
23342ac794baSSreekanth Reddy 		goto out_unlock;
23352ac794baSSreekanth Reddy 	}
23362ac794baSSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done,
23372ac794baSSreekanth Reddy 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
23382ac794baSSreekanth Reddy 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
23392ac794baSSreekanth Reddy 		ioc_err(mrioc, "get package version timed out\n");
2340a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2341a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
23422ac794baSSreekanth Reddy 		retval = -1;
23432ac794baSSreekanth Reddy 		goto out_unlock;
23442ac794baSSreekanth Reddy 	}
23452ac794baSSreekanth Reddy 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
23462ac794baSSreekanth Reddy 	    == MPI3_IOCSTATUS_SUCCESS) {
23472ac794baSSreekanth Reddy 		manifest = (struct mpi3_ci_manifest_mpi *) data;
23482ac794baSSreekanth Reddy 		if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) {
23492ac794baSSreekanth Reddy 			ioc_info(mrioc,
23502ac794baSSreekanth Reddy 			    "firmware package version(%d.%d.%d.%d.%05d-%05d)\n",
23512ac794baSSreekanth Reddy 			    manifest->package_version.gen_major,
23522ac794baSSreekanth Reddy 			    manifest->package_version.gen_minor,
23532ac794baSSreekanth Reddy 			    manifest->package_version.phase_major,
23542ac794baSSreekanth Reddy 			    manifest->package_version.phase_minor,
23552ac794baSSreekanth Reddy 			    manifest->package_version.customer_id,
23562ac794baSSreekanth Reddy 			    manifest->package_version.build_num);
23572ac794baSSreekanth Reddy 		}
23582ac794baSSreekanth Reddy 	}
23592ac794baSSreekanth Reddy 	retval = 0;
23602ac794baSSreekanth Reddy out_unlock:
23612ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
23622ac794baSSreekanth Reddy 	mutex_unlock(&mrioc->init_cmds.mutex);
23632ac794baSSreekanth Reddy 
23642ac794baSSreekanth Reddy out:
23652ac794baSSreekanth Reddy 	if (data)
23662ac794baSSreekanth Reddy 		dma_free_coherent(&mrioc->pdev->dev, data_len, data,
23672ac794baSSreekanth Reddy 		    data_dma);
23682ac794baSSreekanth Reddy 	return retval;
23692ac794baSSreekanth Reddy }
23702ac794baSSreekanth Reddy 
23712ac794baSSreekanth Reddy /**
2372672ae26cSKashyap Desai  * mpi3mr_watchdog_work - watchdog thread to monitor faults
2373672ae26cSKashyap Desai  * @work: work struct
2374672ae26cSKashyap Desai  *
2375672ae26cSKashyap Desai  * Watch dog work periodically executed (1 second interval) to
2376672ae26cSKashyap Desai  * monitor firmware fault and to issue periodic timer sync to
2377672ae26cSKashyap Desai  * the firmware.
2378672ae26cSKashyap Desai  *
2379672ae26cSKashyap Desai  * Return: Nothing.
2380672ae26cSKashyap Desai  */
2381672ae26cSKashyap Desai static void mpi3mr_watchdog_work(struct work_struct *work)
2382672ae26cSKashyap Desai {
2383672ae26cSKashyap Desai 	struct mpi3mr_ioc *mrioc =
2384672ae26cSKashyap Desai 	    container_of(work, struct mpi3mr_ioc, watchdog_work.work);
2385672ae26cSKashyap Desai 	unsigned long flags;
2386672ae26cSKashyap Desai 	enum mpi3mr_iocstate ioc_state;
238778b76a07SSreekanth Reddy 	u32 fault, host_diagnostic, ioc_status;
238878b76a07SSreekanth Reddy 	u32 reset_reason = MPI3MR_RESET_FROM_FAULT_WATCH;
2389672ae26cSKashyap Desai 
2390b64845a7SSreekanth Reddy 	if (mrioc->reset_in_progress || mrioc->unrecoverable)
2391b64845a7SSreekanth Reddy 		return;
2392b64845a7SSreekanth Reddy 
239354dfcffbSKashyap Desai 	if (mrioc->ts_update_counter++ >= MPI3MR_TSUPDATE_INTERVAL) {
239454dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
239554dfcffbSKashyap Desai 		mpi3mr_sync_timestamp(mrioc);
239654dfcffbSKashyap Desai 	}
239754dfcffbSKashyap Desai 
239878b76a07SSreekanth Reddy 	if ((mrioc->prepare_for_reset) &&
239978b76a07SSreekanth Reddy 	    ((mrioc->prepare_for_reset_timeout_counter++) >=
240078b76a07SSreekanth Reddy 	     MPI3MR_PREPARE_FOR_RESET_TIMEOUT)) {
240178b76a07SSreekanth Reddy 		mpi3mr_soft_reset_handler(mrioc,
240278b76a07SSreekanth Reddy 		    MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1);
240378b76a07SSreekanth Reddy 		return;
240478b76a07SSreekanth Reddy 	}
240578b76a07SSreekanth Reddy 
240678b76a07SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
240778b76a07SSreekanth Reddy 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
240878b76a07SSreekanth Reddy 		mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0);
240978b76a07SSreekanth Reddy 		return;
241078b76a07SSreekanth Reddy 	}
241178b76a07SSreekanth Reddy 
2412672ae26cSKashyap Desai 	/*Check for fault state every one second and issue Soft reset*/
2413672ae26cSKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
241478b76a07SSreekanth Reddy 	if (ioc_state != MRIOC_STATE_FAULT)
241578b76a07SSreekanth Reddy 		goto schedule_work;
241678b76a07SSreekanth Reddy 
241778b76a07SSreekanth Reddy 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
2418672ae26cSKashyap Desai 	host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2419672ae26cSKashyap Desai 	if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
2420672ae26cSKashyap Desai 		if (!mrioc->diagsave_timeout) {
2421672ae26cSKashyap Desai 			mpi3mr_print_fault_info(mrioc);
242278b76a07SSreekanth Reddy 			ioc_warn(mrioc, "diag save in progress\n");
2423672ae26cSKashyap Desai 		}
242478b76a07SSreekanth Reddy 		if ((mrioc->diagsave_timeout++) <= MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
2425672ae26cSKashyap Desai 			goto schedule_work;
242678b76a07SSreekanth Reddy 	}
242778b76a07SSreekanth Reddy 
2428672ae26cSKashyap Desai 	mpi3mr_print_fault_info(mrioc);
2429672ae26cSKashyap Desai 	mrioc->diagsave_timeout = 0;
2430672ae26cSKashyap Desai 
243178b76a07SSreekanth Reddy 	switch (fault) {
243278b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED:
2433672ae26cSKashyap Desai 		ioc_info(mrioc,
243478b76a07SSreekanth Reddy 		    "controller requires system power cycle, marking controller as unrecoverable\n");
2435672ae26cSKashyap Desai 		mrioc->unrecoverable = 1;
243678b76a07SSreekanth Reddy 		return;
243778b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS:
243878b76a07SSreekanth Reddy 		return;
243978b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET:
244078b76a07SSreekanth Reddy 		reset_reason = MPI3MR_RESET_FROM_CIACTIV_FAULT;
244178b76a07SSreekanth Reddy 		break;
244278b76a07SSreekanth Reddy 	default:
244378b76a07SSreekanth Reddy 		break;
2444672ae26cSKashyap Desai 	}
244578b76a07SSreekanth Reddy 	mpi3mr_soft_reset_handler(mrioc, reset_reason, 0);
244678b76a07SSreekanth Reddy 	return;
2447672ae26cSKashyap Desai 
2448672ae26cSKashyap Desai schedule_work:
2449672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2450672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2451672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2452672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2453672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2454672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2455672ae26cSKashyap Desai 	return;
2456672ae26cSKashyap Desai }
2457672ae26cSKashyap Desai 
2458672ae26cSKashyap Desai /**
2459672ae26cSKashyap Desai  * mpi3mr_start_watchdog - Start watchdog
2460672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2461672ae26cSKashyap Desai  *
2462672ae26cSKashyap Desai  * Create and start the watchdog thread to monitor controller
2463672ae26cSKashyap Desai  * faults.
2464672ae26cSKashyap Desai  *
2465672ae26cSKashyap Desai  * Return: Nothing.
2466672ae26cSKashyap Desai  */
2467672ae26cSKashyap Desai void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc)
2468672ae26cSKashyap Desai {
2469672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2470672ae26cSKashyap Desai 		return;
2471672ae26cSKashyap Desai 
2472672ae26cSKashyap Desai 	INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work);
2473672ae26cSKashyap Desai 	snprintf(mrioc->watchdog_work_q_name,
2474672ae26cSKashyap Desai 	    sizeof(mrioc->watchdog_work_q_name), "watchdog_%s%d", mrioc->name,
2475672ae26cSKashyap Desai 	    mrioc->id);
2476672ae26cSKashyap Desai 	mrioc->watchdog_work_q =
2477672ae26cSKashyap Desai 	    create_singlethread_workqueue(mrioc->watchdog_work_q_name);
2478672ae26cSKashyap Desai 	if (!mrioc->watchdog_work_q) {
2479672ae26cSKashyap Desai 		ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__);
2480672ae26cSKashyap Desai 		return;
2481672ae26cSKashyap Desai 	}
2482672ae26cSKashyap Desai 
2483672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2484672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2485672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2486672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2487672ae26cSKashyap Desai }
2488672ae26cSKashyap Desai 
2489672ae26cSKashyap Desai /**
2490672ae26cSKashyap Desai  * mpi3mr_stop_watchdog - Stop watchdog
2491672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2492672ae26cSKashyap Desai  *
2493672ae26cSKashyap Desai  * Stop the watchdog thread created to monitor controller
2494672ae26cSKashyap Desai  * faults.
2495672ae26cSKashyap Desai  *
2496672ae26cSKashyap Desai  * Return: Nothing.
2497672ae26cSKashyap Desai  */
2498672ae26cSKashyap Desai void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc)
2499672ae26cSKashyap Desai {
2500672ae26cSKashyap Desai 	unsigned long flags;
2501672ae26cSKashyap Desai 	struct workqueue_struct *wq;
2502672ae26cSKashyap Desai 
2503672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2504672ae26cSKashyap Desai 	wq = mrioc->watchdog_work_q;
2505672ae26cSKashyap Desai 	mrioc->watchdog_work_q = NULL;
2506672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2507672ae26cSKashyap Desai 	if (wq) {
2508672ae26cSKashyap Desai 		if (!cancel_delayed_work_sync(&mrioc->watchdog_work))
2509672ae26cSKashyap Desai 			flush_workqueue(wq);
2510672ae26cSKashyap Desai 		destroy_workqueue(wq);
2511672ae26cSKashyap Desai 	}
2512672ae26cSKashyap Desai }
2513672ae26cSKashyap Desai 
2514672ae26cSKashyap Desai /**
2515824a1566SKashyap Desai  * mpi3mr_setup_admin_qpair - Setup admin queue pair
2516824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2517824a1566SKashyap Desai  *
2518824a1566SKashyap Desai  * Allocate memory for admin queue pair if required and register
2519824a1566SKashyap Desai  * the admin queue with the controller.
2520824a1566SKashyap Desai  *
2521824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2522824a1566SKashyap Desai  */
2523824a1566SKashyap Desai static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
2524824a1566SKashyap Desai {
2525824a1566SKashyap Desai 	int retval = 0;
2526824a1566SKashyap Desai 	u32 num_admin_entries = 0;
2527824a1566SKashyap Desai 
2528824a1566SKashyap Desai 	mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
2529824a1566SKashyap Desai 	mrioc->num_admin_req = mrioc->admin_req_q_sz /
2530824a1566SKashyap Desai 	    MPI3MR_ADMIN_REQ_FRAME_SZ;
2531824a1566SKashyap Desai 	mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
2532824a1566SKashyap Desai 	mrioc->admin_req_base = NULL;
2533824a1566SKashyap Desai 
2534824a1566SKashyap Desai 	mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
2535824a1566SKashyap Desai 	mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
2536824a1566SKashyap Desai 	    MPI3MR_ADMIN_REPLY_FRAME_SZ;
2537824a1566SKashyap Desai 	mrioc->admin_reply_ci = 0;
2538824a1566SKashyap Desai 	mrioc->admin_reply_ephase = 1;
2539824a1566SKashyap Desai 	mrioc->admin_reply_base = NULL;
2540824a1566SKashyap Desai 
2541824a1566SKashyap Desai 	if (!mrioc->admin_req_base) {
2542824a1566SKashyap Desai 		mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
2543824a1566SKashyap Desai 		    mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
2544824a1566SKashyap Desai 
2545824a1566SKashyap Desai 		if (!mrioc->admin_req_base) {
2546824a1566SKashyap Desai 			retval = -1;
2547824a1566SKashyap Desai 			goto out_failed;
2548824a1566SKashyap Desai 		}
2549824a1566SKashyap Desai 
2550824a1566SKashyap Desai 		mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
2551824a1566SKashyap Desai 		    mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
2552824a1566SKashyap Desai 		    GFP_KERNEL);
2553824a1566SKashyap Desai 
2554824a1566SKashyap Desai 		if (!mrioc->admin_reply_base) {
2555824a1566SKashyap Desai 			retval = -1;
2556824a1566SKashyap Desai 			goto out_failed;
2557824a1566SKashyap Desai 		}
2558824a1566SKashyap Desai 	}
2559824a1566SKashyap Desai 
2560824a1566SKashyap Desai 	num_admin_entries = (mrioc->num_admin_replies << 16) |
2561824a1566SKashyap Desai 	    (mrioc->num_admin_req);
2562824a1566SKashyap Desai 	writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
2563824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_req_dma,
2564824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_request_queue_address);
2565824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_reply_dma,
2566824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_reply_queue_address);
2567824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
2568824a1566SKashyap Desai 	writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
2569824a1566SKashyap Desai 	return retval;
2570824a1566SKashyap Desai 
2571824a1566SKashyap Desai out_failed:
2572824a1566SKashyap Desai 
2573824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
2574824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
2575824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
2576824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
2577824a1566SKashyap Desai 	}
2578824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
2579824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
2580824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
2581824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
2582824a1566SKashyap Desai 	}
2583824a1566SKashyap Desai 	return retval;
2584824a1566SKashyap Desai }
2585824a1566SKashyap Desai 
2586824a1566SKashyap Desai /**
2587824a1566SKashyap Desai  * mpi3mr_issue_iocfacts - Send IOC Facts
2588824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2589824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2590824a1566SKashyap Desai  *
2591824a1566SKashyap Desai  * Issue IOC Facts MPI request through admin queue and wait for
2592824a1566SKashyap Desai  * the completion of it or time out.
2593824a1566SKashyap Desai  *
2594824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2595824a1566SKashyap Desai  */
2596824a1566SKashyap Desai static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
2597824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2598824a1566SKashyap Desai {
2599824a1566SKashyap Desai 	struct mpi3_ioc_facts_request iocfacts_req;
2600824a1566SKashyap Desai 	void *data = NULL;
2601824a1566SKashyap Desai 	dma_addr_t data_dma;
2602824a1566SKashyap Desai 	u32 data_len = sizeof(*facts_data);
2603824a1566SKashyap Desai 	int retval = 0;
2604824a1566SKashyap Desai 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
2605824a1566SKashyap Desai 
2606824a1566SKashyap Desai 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2607824a1566SKashyap Desai 	    GFP_KERNEL);
2608824a1566SKashyap Desai 
2609824a1566SKashyap Desai 	if (!data) {
2610824a1566SKashyap Desai 		retval = -1;
2611824a1566SKashyap Desai 		goto out;
2612824a1566SKashyap Desai 	}
2613824a1566SKashyap Desai 
2614824a1566SKashyap Desai 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
2615824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
2616824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2617824a1566SKashyap Desai 		retval = -1;
2618824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
2619824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
2620824a1566SKashyap Desai 		goto out;
2621824a1566SKashyap Desai 	}
2622824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2623824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
2624824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
2625824a1566SKashyap Desai 	iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2626824a1566SKashyap Desai 	iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
2627824a1566SKashyap Desai 
2628824a1566SKashyap Desai 	mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
2629824a1566SKashyap Desai 	    data_dma);
2630824a1566SKashyap Desai 
2631824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
2632824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
2633824a1566SKashyap Desai 	    sizeof(iocfacts_req), 1);
2634824a1566SKashyap Desai 	if (retval) {
2635824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
2636824a1566SKashyap Desai 		goto out_unlock;
2637824a1566SKashyap Desai 	}
2638824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2639824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2640824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2641a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_facts timed out\n");
2642a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2643824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
2644824a1566SKashyap Desai 		retval = -1;
2645824a1566SKashyap Desai 		goto out_unlock;
2646824a1566SKashyap Desai 	}
2647824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2648824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2649824a1566SKashyap Desai 		ioc_err(mrioc,
2650824a1566SKashyap Desai 		    "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2651824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2652824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2653824a1566SKashyap Desai 		retval = -1;
2654824a1566SKashyap Desai 		goto out_unlock;
2655824a1566SKashyap Desai 	}
2656824a1566SKashyap Desai 	memcpy(facts_data, (u8 *)data, data_len);
2657c5758fc7SSreekanth Reddy 	mpi3mr_process_factsdata(mrioc, facts_data);
2658824a1566SKashyap Desai out_unlock:
2659824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2660824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2661824a1566SKashyap Desai 
2662824a1566SKashyap Desai out:
2663824a1566SKashyap Desai 	if (data)
2664824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
2665824a1566SKashyap Desai 
2666824a1566SKashyap Desai 	return retval;
2667824a1566SKashyap Desai }
2668824a1566SKashyap Desai 
2669824a1566SKashyap Desai /**
2670824a1566SKashyap Desai  * mpi3mr_check_reset_dma_mask - Process IOC facts data
2671824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2672824a1566SKashyap Desai  *
2673824a1566SKashyap Desai  * Check whether the new DMA mask requested through IOCFacts by
2674824a1566SKashyap Desai  * firmware needs to be set, if so set it .
2675824a1566SKashyap Desai  *
2676824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
2677824a1566SKashyap Desai  */
2678824a1566SKashyap Desai static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
2679824a1566SKashyap Desai {
2680824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
2681824a1566SKashyap Desai 	int r;
2682824a1566SKashyap Desai 	u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
2683824a1566SKashyap Desai 
2684824a1566SKashyap Desai 	if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
2685824a1566SKashyap Desai 		return 0;
2686824a1566SKashyap Desai 
2687824a1566SKashyap Desai 	ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
2688824a1566SKashyap Desai 	    mrioc->dma_mask, facts_dma_mask);
2689824a1566SKashyap Desai 
2690824a1566SKashyap Desai 	r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
2691824a1566SKashyap Desai 	if (r) {
2692824a1566SKashyap Desai 		ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
2693824a1566SKashyap Desai 		    facts_dma_mask, r);
2694824a1566SKashyap Desai 		return r;
2695824a1566SKashyap Desai 	}
2696824a1566SKashyap Desai 	mrioc->dma_mask = facts_dma_mask;
2697824a1566SKashyap Desai 	return r;
2698824a1566SKashyap Desai }
2699824a1566SKashyap Desai 
2700824a1566SKashyap Desai /**
2701824a1566SKashyap Desai  * mpi3mr_process_factsdata - Process IOC facts data
2702824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2703824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2704824a1566SKashyap Desai  *
2705824a1566SKashyap Desai  * Convert IOC facts data into cpu endianness and cache it in
2706824a1566SKashyap Desai  * the driver .
2707824a1566SKashyap Desai  *
2708824a1566SKashyap Desai  * Return: Nothing.
2709824a1566SKashyap Desai  */
2710824a1566SKashyap Desai static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
2711824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2712824a1566SKashyap Desai {
2713824a1566SKashyap Desai 	u32 ioc_config, req_sz, facts_flags;
2714824a1566SKashyap Desai 
2715824a1566SKashyap Desai 	if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
2716824a1566SKashyap Desai 	    (sizeof(*facts_data) / 4)) {
2717824a1566SKashyap Desai 		ioc_warn(mrioc,
2718824a1566SKashyap Desai 		    "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
2719824a1566SKashyap Desai 		    sizeof(*facts_data),
2720824a1566SKashyap Desai 		    le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
2721824a1566SKashyap Desai 	}
2722824a1566SKashyap Desai 
2723824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
2724824a1566SKashyap Desai 	req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
2725824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
2726824a1566SKashyap Desai 	if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
2727824a1566SKashyap Desai 		ioc_err(mrioc,
2728824a1566SKashyap Desai 		    "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
2729824a1566SKashyap Desai 		    req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
2730824a1566SKashyap Desai 	}
2731824a1566SKashyap Desai 
2732824a1566SKashyap Desai 	memset(&mrioc->facts, 0, sizeof(mrioc->facts));
2733824a1566SKashyap Desai 
2734824a1566SKashyap Desai 	facts_flags = le32_to_cpu(facts_data->flags);
2735824a1566SKashyap Desai 	mrioc->facts.op_req_sz = req_sz;
2736824a1566SKashyap Desai 	mrioc->op_reply_desc_sz = 1 << ((ioc_config &
2737824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
2738824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
2739824a1566SKashyap Desai 
2740824a1566SKashyap Desai 	mrioc->facts.ioc_num = facts_data->ioc_number;
2741824a1566SKashyap Desai 	mrioc->facts.who_init = facts_data->who_init;
2742824a1566SKashyap Desai 	mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
2743824a1566SKashyap Desai 	mrioc->facts.personality = (facts_flags &
2744824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
2745824a1566SKashyap Desai 	mrioc->facts.dma_mask = (facts_flags &
2746824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
2747824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
2748824a1566SKashyap Desai 	mrioc->facts.protocol_flags = facts_data->protocol_flags;
2749824a1566SKashyap Desai 	mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
275004b27e53SSreekanth Reddy 	mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_requests);
2751824a1566SKashyap Desai 	mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
2752824a1566SKashyap Desai 	mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
2753824a1566SKashyap Desai 	mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
2754824a1566SKashyap Desai 	mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
2755824a1566SKashyap Desai 	mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
2756824a1566SKashyap Desai 	mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
2757ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds);
2758ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds);
2759824a1566SKashyap Desai 	mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
2760824a1566SKashyap Desai 	mrioc->facts.max_pcie_switches =
2761ec5ebd2cSSreekanth Reddy 	    le16_to_cpu(facts_data->max_pcie_switches);
2762824a1566SKashyap Desai 	mrioc->facts.max_sasexpanders =
2763824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_expanders);
2764824a1566SKashyap Desai 	mrioc->facts.max_sasinitiators =
2765824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_initiators);
2766824a1566SKashyap Desai 	mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
2767824a1566SKashyap Desai 	mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
2768824a1566SKashyap Desai 	mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
2769824a1566SKashyap Desai 	mrioc->facts.max_op_req_q =
2770824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_request_queues);
2771824a1566SKashyap Desai 	mrioc->facts.max_op_reply_q =
2772824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_reply_queues);
2773824a1566SKashyap Desai 	mrioc->facts.ioc_capabilities =
2774824a1566SKashyap Desai 	    le32_to_cpu(facts_data->ioc_capabilities);
2775824a1566SKashyap Desai 	mrioc->facts.fw_ver.build_num =
2776824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.build_num);
2777824a1566SKashyap Desai 	mrioc->facts.fw_ver.cust_id =
2778824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.customer_id);
2779824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
2780824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
2781824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
2782824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
2783824a1566SKashyap Desai 	mrioc->msix_count = min_t(int, mrioc->msix_count,
2784824a1566SKashyap Desai 	    mrioc->facts.max_msix_vectors);
2785824a1566SKashyap Desai 	mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
2786824a1566SKashyap Desai 	mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
2787824a1566SKashyap Desai 	mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
2788824a1566SKashyap Desai 	mrioc->facts.shutdown_timeout =
2789824a1566SKashyap Desai 	    le16_to_cpu(facts_data->shutdown_timeout);
2790824a1566SKashyap Desai 
2791f10af057SSreekanth Reddy 	mrioc->facts.max_dev_per_tg =
2792f10af057SSreekanth Reddy 	    facts_data->max_devices_per_throttle_group;
2793f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_data_length =
2794f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->io_throttle_data_length);
2795f10af057SSreekanth Reddy 	mrioc->facts.max_io_throttle_group =
2796f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->max_io_throttle_group);
2797f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_low = le16_to_cpu(facts_data->io_throttle_low);
2798f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_high =
2799f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->io_throttle_high);
2800f10af057SSreekanth Reddy 
2801f10af057SSreekanth Reddy 	/* Store in 512b block count */
2802f10af057SSreekanth Reddy 	if (mrioc->facts.io_throttle_data_length)
2803f10af057SSreekanth Reddy 		mrioc->io_throttle_data_length =
2804f10af057SSreekanth Reddy 		    (mrioc->facts.io_throttle_data_length * 2 * 4);
2805f10af057SSreekanth Reddy 	else
2806f10af057SSreekanth Reddy 		/* set the length to 1MB + 1K to disable throttle */
2807f10af057SSreekanth Reddy 		mrioc->io_throttle_data_length = MPI3MR_MAX_SECTORS + 2;
2808f10af057SSreekanth Reddy 
2809f10af057SSreekanth Reddy 	mrioc->io_throttle_high = (mrioc->facts.io_throttle_high * 2 * 1024);
2810f10af057SSreekanth Reddy 	mrioc->io_throttle_low = (mrioc->facts.io_throttle_low * 2 * 1024);
2811f10af057SSreekanth Reddy 
2812824a1566SKashyap Desai 	ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
2813824a1566SKashyap Desai 	    mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
2814824a1566SKashyap Desai 	    mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
2815824a1566SKashyap Desai 	ioc_info(mrioc,
2816ec5ebd2cSSreekanth Reddy 	    "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n",
2817824a1566SKashyap Desai 	    mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
2818ec5ebd2cSSreekanth Reddy 	    mrioc->facts.max_msix_vectors, mrioc->facts.max_perids);
2819824a1566SKashyap Desai 	ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
2820824a1566SKashyap Desai 	    mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
2821824a1566SKashyap Desai 	    mrioc->facts.sge_mod_shift);
2822824a1566SKashyap Desai 	ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
2823824a1566SKashyap Desai 	    mrioc->facts.dma_mask, (facts_flags &
2824824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
2825f10af057SSreekanth Reddy 	ioc_info(mrioc,
2826f10af057SSreekanth Reddy 	    "max_dev_per_throttle_group(%d), max_throttle_groups(%d)\n",
2827f10af057SSreekanth Reddy 	    mrioc->facts.max_dev_per_tg, mrioc->facts.max_io_throttle_group);
2828f10af057SSreekanth Reddy 	ioc_info(mrioc,
2829f10af057SSreekanth Reddy 	   "io_throttle_data_len(%dKiB), io_throttle_high(%dMiB), io_throttle_low(%dMiB)\n",
2830f10af057SSreekanth Reddy 	   mrioc->facts.io_throttle_data_length * 4,
2831f10af057SSreekanth Reddy 	   mrioc->facts.io_throttle_high, mrioc->facts.io_throttle_low);
2832824a1566SKashyap Desai }
2833824a1566SKashyap Desai 
2834824a1566SKashyap Desai /**
2835824a1566SKashyap Desai  * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
2836824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2837824a1566SKashyap Desai  *
2838824a1566SKashyap Desai  * Allocate and initialize the reply free buffers, sense
2839824a1566SKashyap Desai  * buffers, reply free queue and sense buffer queue.
2840824a1566SKashyap Desai  *
2841824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2842824a1566SKashyap Desai  */
2843824a1566SKashyap Desai static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
2844824a1566SKashyap Desai {
2845824a1566SKashyap Desai 	int retval = 0;
2846824a1566SKashyap Desai 	u32 sz, i;
2847824a1566SKashyap Desai 
2848824a1566SKashyap Desai 	if (mrioc->init_cmds.reply)
2849e3605f65SSreekanth Reddy 		return retval;
2850824a1566SKashyap Desai 
2851c5758fc7SSreekanth Reddy 	mrioc->init_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2852824a1566SKashyap Desai 	if (!mrioc->init_cmds.reply)
2853824a1566SKashyap Desai 		goto out_failed;
2854824a1566SKashyap Desai 
2855f5e6d5a3SSumit Saxena 	mrioc->bsg_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2856f5e6d5a3SSumit Saxena 	if (!mrioc->bsg_cmds.reply)
2857f5e6d5a3SSumit Saxena 		goto out_failed;
2858f5e6d5a3SSumit Saxena 
285913ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
2860c5758fc7SSreekanth Reddy 		mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->reply_sz,
286113ef29eaSKashyap Desai 		    GFP_KERNEL);
286213ef29eaSKashyap Desai 		if (!mrioc->dev_rmhs_cmds[i].reply)
286313ef29eaSKashyap Desai 			goto out_failed;
286413ef29eaSKashyap Desai 	}
286513ef29eaSKashyap Desai 
2866c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
2867c1af985dSSreekanth Reddy 		mrioc->evtack_cmds[i].reply = kzalloc(mrioc->reply_sz,
2868c1af985dSSreekanth Reddy 		    GFP_KERNEL);
2869c1af985dSSreekanth Reddy 		if (!mrioc->evtack_cmds[i].reply)
2870c1af985dSSreekanth Reddy 			goto out_failed;
2871c1af985dSSreekanth Reddy 	}
2872c1af985dSSreekanth Reddy 
2873c5758fc7SSreekanth Reddy 	mrioc->host_tm_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2874e844adb1SKashyap Desai 	if (!mrioc->host_tm_cmds.reply)
2875e844adb1SKashyap Desai 		goto out_failed;
2876e844adb1SKashyap Desai 
287743ca1100SSumit Saxena 	mrioc->pel_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
287843ca1100SSumit Saxena 	if (!mrioc->pel_cmds.reply)
287943ca1100SSumit Saxena 		goto out_failed;
288043ca1100SSumit Saxena 
288143ca1100SSumit Saxena 	mrioc->pel_abort_cmd.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
288243ca1100SSumit Saxena 	if (!mrioc->pel_abort_cmd.reply)
288343ca1100SSumit Saxena 		goto out_failed;
288443ca1100SSumit Saxena 
2885e844adb1SKashyap Desai 	mrioc->dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
2886e844adb1SKashyap Desai 	if (mrioc->facts.max_devhandle % 8)
2887e844adb1SKashyap Desai 		mrioc->dev_handle_bitmap_sz++;
2888e844adb1SKashyap Desai 	mrioc->removepend_bitmap = kzalloc(mrioc->dev_handle_bitmap_sz,
2889e844adb1SKashyap Desai 	    GFP_KERNEL);
2890e844adb1SKashyap Desai 	if (!mrioc->removepend_bitmap)
2891e844adb1SKashyap Desai 		goto out_failed;
2892e844adb1SKashyap Desai 
2893e844adb1SKashyap Desai 	mrioc->devrem_bitmap_sz = MPI3MR_NUM_DEVRMCMD / 8;
2894e844adb1SKashyap Desai 	if (MPI3MR_NUM_DEVRMCMD % 8)
2895e844adb1SKashyap Desai 		mrioc->devrem_bitmap_sz++;
2896e844adb1SKashyap Desai 	mrioc->devrem_bitmap = kzalloc(mrioc->devrem_bitmap_sz,
2897e844adb1SKashyap Desai 	    GFP_KERNEL);
2898e844adb1SKashyap Desai 	if (!mrioc->devrem_bitmap)
2899e844adb1SKashyap Desai 		goto out_failed;
2900e844adb1SKashyap Desai 
2901c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap_sz = MPI3MR_NUM_EVTACKCMD / 8;
2902c1af985dSSreekanth Reddy 	if (MPI3MR_NUM_EVTACKCMD % 8)
2903c1af985dSSreekanth Reddy 		mrioc->evtack_cmds_bitmap_sz++;
2904c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap = kzalloc(mrioc->evtack_cmds_bitmap_sz,
2905c1af985dSSreekanth Reddy 	    GFP_KERNEL);
2906c1af985dSSreekanth Reddy 	if (!mrioc->evtack_cmds_bitmap)
2907c1af985dSSreekanth Reddy 		goto out_failed;
2908c1af985dSSreekanth Reddy 
2909824a1566SKashyap Desai 	mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
2910824a1566SKashyap Desai 	mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
2911824a1566SKashyap Desai 	mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
2912824a1566SKashyap Desai 	mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
2913824a1566SKashyap Desai 
2914824a1566SKashyap Desai 	/* reply buffer pool, 16 byte align */
2915c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2916824a1566SKashyap Desai 	mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
2917824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
2918824a1566SKashyap Desai 	if (!mrioc->reply_buf_pool) {
2919824a1566SKashyap Desai 		ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
2920824a1566SKashyap Desai 		goto out_failed;
2921824a1566SKashyap Desai 	}
2922824a1566SKashyap Desai 
2923824a1566SKashyap Desai 	mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
2924824a1566SKashyap Desai 	    &mrioc->reply_buf_dma);
2925824a1566SKashyap Desai 	if (!mrioc->reply_buf)
2926824a1566SKashyap Desai 		goto out_failed;
2927824a1566SKashyap Desai 
2928824a1566SKashyap Desai 	mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
2929824a1566SKashyap Desai 
2930824a1566SKashyap Desai 	/* reply free queue, 8 byte align */
2931824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
2932824a1566SKashyap Desai 	mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
2933824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
2934824a1566SKashyap Desai 	if (!mrioc->reply_free_q_pool) {
2935824a1566SKashyap Desai 		ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
2936824a1566SKashyap Desai 		goto out_failed;
2937824a1566SKashyap Desai 	}
2938824a1566SKashyap Desai 	mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
2939824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->reply_free_q_dma);
2940824a1566SKashyap Desai 	if (!mrioc->reply_free_q)
2941824a1566SKashyap Desai 		goto out_failed;
2942824a1566SKashyap Desai 
2943824a1566SKashyap Desai 	/* sense buffer pool,  4 byte align */
2944ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2945824a1566SKashyap Desai 	mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
2946824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 4, 0);
2947824a1566SKashyap Desai 	if (!mrioc->sense_buf_pool) {
2948824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
2949824a1566SKashyap Desai 		goto out_failed;
2950824a1566SKashyap Desai 	}
2951824a1566SKashyap Desai 	mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
2952824a1566SKashyap Desai 	    &mrioc->sense_buf_dma);
2953824a1566SKashyap Desai 	if (!mrioc->sense_buf)
2954824a1566SKashyap Desai 		goto out_failed;
2955824a1566SKashyap Desai 
2956824a1566SKashyap Desai 	/* sense buffer queue, 8 byte align */
2957824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
2958824a1566SKashyap Desai 	mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
2959824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
2960824a1566SKashyap Desai 	if (!mrioc->sense_buf_q_pool) {
2961824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
2962824a1566SKashyap Desai 		goto out_failed;
2963824a1566SKashyap Desai 	}
2964824a1566SKashyap Desai 	mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
2965824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->sense_buf_q_dma);
2966824a1566SKashyap Desai 	if (!mrioc->sense_buf_q)
2967824a1566SKashyap Desai 		goto out_failed;
2968824a1566SKashyap Desai 
2969e3605f65SSreekanth Reddy 	return retval;
2970e3605f65SSreekanth Reddy 
2971e3605f65SSreekanth Reddy out_failed:
2972e3605f65SSreekanth Reddy 	retval = -1;
2973e3605f65SSreekanth Reddy 	return retval;
2974e3605f65SSreekanth Reddy }
2975e3605f65SSreekanth Reddy 
2976e3605f65SSreekanth Reddy /**
2977e3605f65SSreekanth Reddy  * mpimr_initialize_reply_sbuf_queues - initialize reply sense
2978e3605f65SSreekanth Reddy  * buffers
2979e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
2980e3605f65SSreekanth Reddy  *
2981e3605f65SSreekanth Reddy  * Helper function to initialize reply and sense buffers along
2982e3605f65SSreekanth Reddy  * with some debug prints.
2983e3605f65SSreekanth Reddy  *
2984e3605f65SSreekanth Reddy  * Return:  None.
2985e3605f65SSreekanth Reddy  */
2986e3605f65SSreekanth Reddy static void mpimr_initialize_reply_sbuf_queues(struct mpi3mr_ioc *mrioc)
2987e3605f65SSreekanth Reddy {
2988e3605f65SSreekanth Reddy 	u32 sz, i;
2989e3605f65SSreekanth Reddy 	dma_addr_t phy_addr;
2990e3605f65SSreekanth Reddy 
2991c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2992824a1566SKashyap Desai 	ioc_info(mrioc,
2993824a1566SKashyap Desai 	    "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2994c5758fc7SSreekanth Reddy 	    mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->reply_sz,
2995824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
2996824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
2997824a1566SKashyap Desai 	ioc_info(mrioc,
2998824a1566SKashyap Desai 	    "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2999824a1566SKashyap Desai 	    mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
3000824a1566SKashyap Desai 	    (unsigned long long)mrioc->reply_free_q_dma);
3001ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
3002824a1566SKashyap Desai 	ioc_info(mrioc,
3003824a1566SKashyap Desai 	    "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
3004ec5ebd2cSSreekanth Reddy 	    mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ,
3005824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
3006824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
3007824a1566SKashyap Desai 	ioc_info(mrioc,
3008824a1566SKashyap Desai 	    "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
3009824a1566SKashyap Desai 	    mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
3010824a1566SKashyap Desai 	    (unsigned long long)mrioc->sense_buf_q_dma);
3011824a1566SKashyap Desai 
3012824a1566SKashyap Desai 	/* initialize Reply buffer Queue */
3013824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->reply_buf_dma;
3014c5758fc7SSreekanth Reddy 	    i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->reply_sz)
3015824a1566SKashyap Desai 		mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
3016824a1566SKashyap Desai 	mrioc->reply_free_q[i] = cpu_to_le64(0);
3017824a1566SKashyap Desai 
3018824a1566SKashyap Desai 	/* initialize Sense Buffer Queue */
3019824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->sense_buf_dma;
3020ec5ebd2cSSreekanth Reddy 	    i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ)
3021824a1566SKashyap Desai 		mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
3022824a1566SKashyap Desai 	mrioc->sense_buf_q[i] = cpu_to_le64(0);
3023824a1566SKashyap Desai }
3024824a1566SKashyap Desai 
3025824a1566SKashyap Desai /**
3026824a1566SKashyap Desai  * mpi3mr_issue_iocinit - Send IOC Init
3027824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3028824a1566SKashyap Desai  *
3029824a1566SKashyap Desai  * Issue IOC Init MPI request through admin queue and wait for
3030824a1566SKashyap Desai  * the completion of it or time out.
3031824a1566SKashyap Desai  *
3032824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
3033824a1566SKashyap Desai  */
3034824a1566SKashyap Desai static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
3035824a1566SKashyap Desai {
3036824a1566SKashyap Desai 	struct mpi3_ioc_init_request iocinit_req;
3037824a1566SKashyap Desai 	struct mpi3_driver_info_layout *drv_info;
3038824a1566SKashyap Desai 	dma_addr_t data_dma;
3039824a1566SKashyap Desai 	u32 data_len = sizeof(*drv_info);
3040824a1566SKashyap Desai 	int retval = 0;
3041824a1566SKashyap Desai 	ktime_t current_time;
3042824a1566SKashyap Desai 
3043824a1566SKashyap Desai 	drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
3044824a1566SKashyap Desai 	    GFP_KERNEL);
3045824a1566SKashyap Desai 	if (!drv_info) {
3046824a1566SKashyap Desai 		retval = -1;
3047824a1566SKashyap Desai 		goto out;
3048824a1566SKashyap Desai 	}
3049e3605f65SSreekanth Reddy 	mpimr_initialize_reply_sbuf_queues(mrioc);
3050e3605f65SSreekanth Reddy 
3051824a1566SKashyap Desai 	drv_info->information_length = cpu_to_le32(data_len);
3052aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
3053aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
3054aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
3055aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
3056aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
3057aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE,
3058aa0dc6a7SSreekanth Reddy 	    sizeof(drv_info->driver_release_date));
3059824a1566SKashyap Desai 	drv_info->driver_capabilities = 0;
3060824a1566SKashyap Desai 	memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
3061824a1566SKashyap Desai 	    sizeof(mrioc->driver_info));
3062824a1566SKashyap Desai 
3063824a1566SKashyap Desai 	memset(&iocinit_req, 0, sizeof(iocinit_req));
3064824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3065824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3066824a1566SKashyap Desai 		retval = -1;
3067824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
3068824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3069824a1566SKashyap Desai 		goto out;
3070824a1566SKashyap Desai 	}
3071824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3072824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
3073824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
3074824a1566SKashyap Desai 	iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3075824a1566SKashyap Desai 	iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
3076824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
3077824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
3078824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
3079824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
3080824a1566SKashyap Desai 	iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
3081824a1566SKashyap Desai 	iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
3082824a1566SKashyap Desai 	iocinit_req.reply_free_queue_address =
3083824a1566SKashyap Desai 	    cpu_to_le64(mrioc->reply_free_q_dma);
3084ec5ebd2cSSreekanth Reddy 	iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ);
3085824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_depth =
3086824a1566SKashyap Desai 	    cpu_to_le16(mrioc->sense_buf_q_sz);
3087824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_address =
3088824a1566SKashyap Desai 	    cpu_to_le64(mrioc->sense_buf_q_dma);
3089824a1566SKashyap Desai 	iocinit_req.driver_information_address = cpu_to_le64(data_dma);
3090824a1566SKashyap Desai 
3091824a1566SKashyap Desai 	current_time = ktime_get_real();
3092824a1566SKashyap Desai 	iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
3093824a1566SKashyap Desai 
3094824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
3095824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
3096824a1566SKashyap Desai 	    sizeof(iocinit_req), 1);
3097824a1566SKashyap Desai 	if (retval) {
3098824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
3099824a1566SKashyap Desai 		goto out_unlock;
3100824a1566SKashyap Desai 	}
3101824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
3102824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
3103824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3104a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
3105824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
3106a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_init timed out\n");
3107824a1566SKashyap Desai 		retval = -1;
3108824a1566SKashyap Desai 		goto out_unlock;
3109824a1566SKashyap Desai 	}
3110824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
3111824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
3112824a1566SKashyap Desai 		ioc_err(mrioc,
3113824a1566SKashyap Desai 		    "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
3114824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
3115824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
3116824a1566SKashyap Desai 		retval = -1;
3117824a1566SKashyap Desai 		goto out_unlock;
3118824a1566SKashyap Desai 	}
3119824a1566SKashyap Desai 
3120e3605f65SSreekanth Reddy 	mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
3121e3605f65SSreekanth Reddy 	writel(mrioc->reply_free_queue_host_index,
3122e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->reply_free_host_index);
3123e3605f65SSreekanth Reddy 
3124e3605f65SSreekanth Reddy 	mrioc->sbq_host_index = mrioc->num_sense_bufs;
3125e3605f65SSreekanth Reddy 	writel(mrioc->sbq_host_index,
3126e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
3127824a1566SKashyap Desai out_unlock:
3128824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3129824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3130824a1566SKashyap Desai 
3131824a1566SKashyap Desai out:
3132824a1566SKashyap Desai 	if (drv_info)
3133824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
3134824a1566SKashyap Desai 		    data_dma);
3135824a1566SKashyap Desai 
3136824a1566SKashyap Desai 	return retval;
3137824a1566SKashyap Desai }
3138824a1566SKashyap Desai 
3139824a1566SKashyap Desai /**
314013ef29eaSKashyap Desai  * mpi3mr_unmask_events - Unmask events in event mask bitmap
314113ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
314213ef29eaSKashyap Desai  * @event: MPI event ID
314313ef29eaSKashyap Desai  *
314413ef29eaSKashyap Desai  * Un mask the specific event by resetting the event_mask
314513ef29eaSKashyap Desai  * bitmap.
314613ef29eaSKashyap Desai  *
314713ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
314813ef29eaSKashyap Desai  */
314913ef29eaSKashyap Desai static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event)
315013ef29eaSKashyap Desai {
315113ef29eaSKashyap Desai 	u32 desired_event;
315213ef29eaSKashyap Desai 	u8 word;
315313ef29eaSKashyap Desai 
315413ef29eaSKashyap Desai 	if (event >= 128)
315513ef29eaSKashyap Desai 		return;
315613ef29eaSKashyap Desai 
315713ef29eaSKashyap Desai 	desired_event = (1 << (event % 32));
315813ef29eaSKashyap Desai 	word = event / 32;
315913ef29eaSKashyap Desai 
316013ef29eaSKashyap Desai 	mrioc->event_masks[word] &= ~desired_event;
316113ef29eaSKashyap Desai }
316213ef29eaSKashyap Desai 
316313ef29eaSKashyap Desai /**
316413ef29eaSKashyap Desai  * mpi3mr_issue_event_notification - Send event notification
316513ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
316613ef29eaSKashyap Desai  *
316713ef29eaSKashyap Desai  * Issue event notification MPI request through admin queue and
316813ef29eaSKashyap Desai  * wait for the completion of it or time out.
316913ef29eaSKashyap Desai  *
317013ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
317113ef29eaSKashyap Desai  */
317213ef29eaSKashyap Desai static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc)
317313ef29eaSKashyap Desai {
317413ef29eaSKashyap Desai 	struct mpi3_event_notification_request evtnotify_req;
317513ef29eaSKashyap Desai 	int retval = 0;
317613ef29eaSKashyap Desai 	u8 i;
317713ef29eaSKashyap Desai 
317813ef29eaSKashyap Desai 	memset(&evtnotify_req, 0, sizeof(evtnotify_req));
317913ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
318013ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
318113ef29eaSKashyap Desai 		retval = -1;
318213ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n");
318313ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
318413ef29eaSKashyap Desai 		goto out;
318513ef29eaSKashyap Desai 	}
318613ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
318713ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
318813ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
318913ef29eaSKashyap Desai 	evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
319013ef29eaSKashyap Desai 	evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION;
319113ef29eaSKashyap Desai 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
319213ef29eaSKashyap Desai 		evtnotify_req.event_masks[i] =
319313ef29eaSKashyap Desai 		    cpu_to_le32(mrioc->event_masks[i]);
319413ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
319513ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req,
319613ef29eaSKashyap Desai 	    sizeof(evtnotify_req), 1);
319713ef29eaSKashyap Desai 	if (retval) {
319813ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n");
319913ef29eaSKashyap Desai 		goto out_unlock;
320013ef29eaSKashyap Desai 	}
320113ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
320213ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
320313ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3204a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "event notification timed out\n");
3205a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
320613ef29eaSKashyap Desai 		    MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
320713ef29eaSKashyap Desai 		retval = -1;
320813ef29eaSKashyap Desai 		goto out_unlock;
320913ef29eaSKashyap Desai 	}
321013ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
321113ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
321213ef29eaSKashyap Desai 		ioc_err(mrioc,
321313ef29eaSKashyap Desai 		    "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
321413ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
321513ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
321613ef29eaSKashyap Desai 		retval = -1;
321713ef29eaSKashyap Desai 		goto out_unlock;
321813ef29eaSKashyap Desai 	}
321913ef29eaSKashyap Desai 
322013ef29eaSKashyap Desai out_unlock:
322113ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
322213ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
322313ef29eaSKashyap Desai out:
322413ef29eaSKashyap Desai 	return retval;
322513ef29eaSKashyap Desai }
322613ef29eaSKashyap Desai 
322713ef29eaSKashyap Desai /**
3228c1af985dSSreekanth Reddy  * mpi3mr_process_event_ack - Process event acknowledgment
322913ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
323013ef29eaSKashyap Desai  * @event: MPI3 event ID
3231c1af985dSSreekanth Reddy  * @event_ctx: event context
323213ef29eaSKashyap Desai  *
323313ef29eaSKashyap Desai  * Send event acknowledgment through admin queue and wait for
323413ef29eaSKashyap Desai  * it to complete.
323513ef29eaSKashyap Desai  *
323613ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
323713ef29eaSKashyap Desai  */
3238c1af985dSSreekanth Reddy int mpi3mr_process_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
323913ef29eaSKashyap Desai 	u32 event_ctx)
324013ef29eaSKashyap Desai {
324113ef29eaSKashyap Desai 	struct mpi3_event_ack_request evtack_req;
324213ef29eaSKashyap Desai 	int retval = 0;
324313ef29eaSKashyap Desai 
324413ef29eaSKashyap Desai 	memset(&evtack_req, 0, sizeof(evtack_req));
324513ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
324613ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
324713ef29eaSKashyap Desai 		retval = -1;
324813ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Init command is in use\n");
324913ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
325013ef29eaSKashyap Desai 		goto out;
325113ef29eaSKashyap Desai 	}
325213ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
325313ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
325413ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
325513ef29eaSKashyap Desai 	evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
325613ef29eaSKashyap Desai 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
325713ef29eaSKashyap Desai 	evtack_req.event = event;
325813ef29eaSKashyap Desai 	evtack_req.event_context = cpu_to_le32(event_ctx);
325913ef29eaSKashyap Desai 
326013ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
326113ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
326213ef29eaSKashyap Desai 	    sizeof(evtack_req), 1);
326313ef29eaSKashyap Desai 	if (retval) {
326413ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Admin Post failed\n");
326513ef29eaSKashyap Desai 		goto out_unlock;
326613ef29eaSKashyap Desai 	}
326713ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
326813ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
326913ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
327013ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: command timed out\n");
3271fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
327213ef29eaSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
327313ef29eaSKashyap Desai 			    MPI3MR_RESET_FROM_EVTACK_TIMEOUT, 1);
327413ef29eaSKashyap Desai 		retval = -1;
327513ef29eaSKashyap Desai 		goto out_unlock;
327613ef29eaSKashyap Desai 	}
327713ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
327813ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
327913ef29eaSKashyap Desai 		ioc_err(mrioc,
328013ef29eaSKashyap Desai 		    "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
328113ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
328213ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
328313ef29eaSKashyap Desai 		retval = -1;
328413ef29eaSKashyap Desai 		goto out_unlock;
328513ef29eaSKashyap Desai 	}
328613ef29eaSKashyap Desai 
328713ef29eaSKashyap Desai out_unlock:
328813ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
328913ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
329013ef29eaSKashyap Desai out:
329113ef29eaSKashyap Desai 	return retval;
329213ef29eaSKashyap Desai }
329313ef29eaSKashyap Desai 
329413ef29eaSKashyap Desai /**
3295824a1566SKashyap Desai  * mpi3mr_alloc_chain_bufs - Allocate chain buffers
3296824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3297824a1566SKashyap Desai  *
3298824a1566SKashyap Desai  * Allocate chain buffers and set a bitmap to indicate free
3299824a1566SKashyap Desai  * chain buffers. Chain buffers are used to pass the SGE
3300824a1566SKashyap Desai  * information along with MPI3 SCSI IO requests for host I/O.
3301824a1566SKashyap Desai  *
3302824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure
3303824a1566SKashyap Desai  */
3304824a1566SKashyap Desai static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
3305824a1566SKashyap Desai {
3306824a1566SKashyap Desai 	int retval = 0;
3307824a1566SKashyap Desai 	u32 sz, i;
3308824a1566SKashyap Desai 	u16 num_chains;
3309824a1566SKashyap Desai 
3310fe6db615SSreekanth Reddy 	if (mrioc->chain_sgl_list)
3311fe6db615SSreekanth Reddy 		return retval;
3312fe6db615SSreekanth Reddy 
3313824a1566SKashyap Desai 	num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
3314824a1566SKashyap Desai 
331574e1f30aSKashyap Desai 	if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION
331674e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE1_PROTECTION
331774e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE2_PROTECTION
331874e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE3_PROTECTION))
331974e1f30aSKashyap Desai 		num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR);
332074e1f30aSKashyap Desai 
3321824a1566SKashyap Desai 	mrioc->chain_buf_count = num_chains;
3322824a1566SKashyap Desai 	sz = sizeof(struct chain_element) * num_chains;
3323824a1566SKashyap Desai 	mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
3324824a1566SKashyap Desai 	if (!mrioc->chain_sgl_list)
3325824a1566SKashyap Desai 		goto out_failed;
3326824a1566SKashyap Desai 
3327824a1566SKashyap Desai 	sz = MPI3MR_PAGE_SIZE_4K;
3328824a1566SKashyap Desai 	mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
3329824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
3330824a1566SKashyap Desai 	if (!mrioc->chain_buf_pool) {
3331824a1566SKashyap Desai 		ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
3332824a1566SKashyap Desai 		goto out_failed;
3333824a1566SKashyap Desai 	}
3334824a1566SKashyap Desai 
3335824a1566SKashyap Desai 	for (i = 0; i < num_chains; i++) {
3336824a1566SKashyap Desai 		mrioc->chain_sgl_list[i].addr =
3337824a1566SKashyap Desai 		    dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
3338824a1566SKashyap Desai 		    &mrioc->chain_sgl_list[i].dma_addr);
3339824a1566SKashyap Desai 
3340824a1566SKashyap Desai 		if (!mrioc->chain_sgl_list[i].addr)
3341824a1566SKashyap Desai 			goto out_failed;
3342824a1566SKashyap Desai 	}
3343824a1566SKashyap Desai 	mrioc->chain_bitmap_sz = num_chains / 8;
3344824a1566SKashyap Desai 	if (num_chains % 8)
3345824a1566SKashyap Desai 		mrioc->chain_bitmap_sz++;
3346824a1566SKashyap Desai 	mrioc->chain_bitmap = kzalloc(mrioc->chain_bitmap_sz, GFP_KERNEL);
3347824a1566SKashyap Desai 	if (!mrioc->chain_bitmap)
3348824a1566SKashyap Desai 		goto out_failed;
3349824a1566SKashyap Desai 	return retval;
3350824a1566SKashyap Desai out_failed:
3351824a1566SKashyap Desai 	retval = -1;
3352824a1566SKashyap Desai 	return retval;
3353824a1566SKashyap Desai }
3354824a1566SKashyap Desai 
3355824a1566SKashyap Desai /**
3356023ab2a9SKashyap Desai  * mpi3mr_port_enable_complete - Mark port enable complete
3357023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3358023ab2a9SKashyap Desai  * @drv_cmd: Internal command tracker
3359023ab2a9SKashyap Desai  *
3360023ab2a9SKashyap Desai  * Call back for asynchronous port enable request sets the
3361023ab2a9SKashyap Desai  * driver command to indicate port enable request is complete.
3362023ab2a9SKashyap Desai  *
3363023ab2a9SKashyap Desai  * Return: Nothing
3364023ab2a9SKashyap Desai  */
3365023ab2a9SKashyap Desai static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc,
3366023ab2a9SKashyap Desai 	struct mpi3mr_drv_cmd *drv_cmd)
3367023ab2a9SKashyap Desai {
3368023ab2a9SKashyap Desai 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3369023ab2a9SKashyap Desai 	drv_cmd->callback = NULL;
3370023ab2a9SKashyap Desai 	mrioc->scan_failed = drv_cmd->ioc_status;
3371023ab2a9SKashyap Desai 	mrioc->scan_started = 0;
3372023ab2a9SKashyap Desai }
3373023ab2a9SKashyap Desai 
3374023ab2a9SKashyap Desai /**
3375023ab2a9SKashyap Desai  * mpi3mr_issue_port_enable - Issue Port Enable
3376023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3377023ab2a9SKashyap Desai  * @async: Flag to wait for completion or not
3378023ab2a9SKashyap Desai  *
3379023ab2a9SKashyap Desai  * Issue Port Enable MPI request through admin queue and if the
3380023ab2a9SKashyap Desai  * async flag is not set wait for the completion of the port
3381023ab2a9SKashyap Desai  * enable or time out.
3382023ab2a9SKashyap Desai  *
3383023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failures.
3384023ab2a9SKashyap Desai  */
3385023ab2a9SKashyap Desai int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async)
3386023ab2a9SKashyap Desai {
3387023ab2a9SKashyap Desai 	struct mpi3_port_enable_request pe_req;
3388023ab2a9SKashyap Desai 	int retval = 0;
3389023ab2a9SKashyap Desai 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3390023ab2a9SKashyap Desai 
3391023ab2a9SKashyap Desai 	memset(&pe_req, 0, sizeof(pe_req));
3392023ab2a9SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3393023ab2a9SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3394023ab2a9SKashyap Desai 		retval = -1;
3395023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Init command is in use\n");
3396023ab2a9SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3397023ab2a9SKashyap Desai 		goto out;
3398023ab2a9SKashyap Desai 	}
3399023ab2a9SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3400023ab2a9SKashyap Desai 	if (async) {
3401023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
3402023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = mpi3mr_port_enable_complete;
3403023ab2a9SKashyap Desai 	} else {
3404023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 1;
3405023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = NULL;
3406023ab2a9SKashyap Desai 		init_completion(&mrioc->init_cmds.done);
3407023ab2a9SKashyap Desai 	}
3408023ab2a9SKashyap Desai 	pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3409023ab2a9SKashyap Desai 	pe_req.function = MPI3_FUNCTION_PORT_ENABLE;
3410023ab2a9SKashyap Desai 
3411023ab2a9SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1);
3412023ab2a9SKashyap Desai 	if (retval) {
3413023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n");
3414023ab2a9SKashyap Desai 		goto out_unlock;
3415023ab2a9SKashyap Desai 	}
3416a6856cc4SSreekanth Reddy 	if (async) {
3417a6856cc4SSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
3418a6856cc4SSreekanth Reddy 		goto out;
3419a6856cc4SSreekanth Reddy 	}
3420a6856cc4SSreekanth Reddy 
3421a6856cc4SSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ));
3422023ab2a9SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3423a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "port enable timed out\n");
3424023ab2a9SKashyap Desai 		retval = -1;
3425a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT);
3426023ab2a9SKashyap Desai 		goto out_unlock;
3427023ab2a9SKashyap Desai 	}
3428023ab2a9SKashyap Desai 	mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds);
3429a6856cc4SSreekanth Reddy 
3430023ab2a9SKashyap Desai out_unlock:
3431a6856cc4SSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3432023ab2a9SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3433023ab2a9SKashyap Desai out:
3434023ab2a9SKashyap Desai 	return retval;
3435023ab2a9SKashyap Desai }
3436023ab2a9SKashyap Desai 
3437ff9561e9SKashyap Desai /* Protocol type to name mapper structure */
3438ff9561e9SKashyap Desai static const struct {
3439ff9561e9SKashyap Desai 	u8 protocol;
3440ff9561e9SKashyap Desai 	char *name;
3441ff9561e9SKashyap Desai } mpi3mr_protocols[] = {
3442ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" },
3443ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" },
3444ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" },
3445ff9561e9SKashyap Desai };
3446ff9561e9SKashyap Desai 
3447ff9561e9SKashyap Desai /* Capability to name mapper structure*/
3448ff9561e9SKashyap Desai static const struct {
3449ff9561e9SKashyap Desai 	u32 capability;
3450ff9561e9SKashyap Desai 	char *name;
3451ff9561e9SKashyap Desai } mpi3mr_capabilities[] = {
3452ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE, "RAID" },
3453ff9561e9SKashyap Desai };
3454ff9561e9SKashyap Desai 
3455ff9561e9SKashyap Desai /**
3456ff9561e9SKashyap Desai  * mpi3mr_print_ioc_info - Display controller information
3457ff9561e9SKashyap Desai  * @mrioc: Adapter instance reference
3458ff9561e9SKashyap Desai  *
3459ff9561e9SKashyap Desai  * Display controller personalit, capability, supported
3460ff9561e9SKashyap Desai  * protocols etc.
3461ff9561e9SKashyap Desai  *
3462ff9561e9SKashyap Desai  * Return: Nothing
3463ff9561e9SKashyap Desai  */
3464ff9561e9SKashyap Desai static void
3465ff9561e9SKashyap Desai mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc)
3466ff9561e9SKashyap Desai {
346776a4f7ccSDan Carpenter 	int i = 0, bytes_written = 0;
3468ff9561e9SKashyap Desai 	char personality[16];
3469ff9561e9SKashyap Desai 	char protocol[50] = {0};
3470ff9561e9SKashyap Desai 	char capabilities[100] = {0};
3471ff9561e9SKashyap Desai 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
3472ff9561e9SKashyap Desai 
3473ff9561e9SKashyap Desai 	switch (mrioc->facts.personality) {
3474ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
3475ff9561e9SKashyap Desai 		strncpy(personality, "Enhanced HBA", sizeof(personality));
3476ff9561e9SKashyap Desai 		break;
3477ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
3478ff9561e9SKashyap Desai 		strncpy(personality, "RAID", sizeof(personality));
3479ff9561e9SKashyap Desai 		break;
3480ff9561e9SKashyap Desai 	default:
3481ff9561e9SKashyap Desai 		strncpy(personality, "Unknown", sizeof(personality));
3482ff9561e9SKashyap Desai 		break;
3483ff9561e9SKashyap Desai 	}
3484ff9561e9SKashyap Desai 
3485ff9561e9SKashyap Desai 	ioc_info(mrioc, "Running in %s Personality", personality);
3486ff9561e9SKashyap Desai 
3487ff9561e9SKashyap Desai 	ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n",
3488ff9561e9SKashyap Desai 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
3489ff9561e9SKashyap Desai 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
3490ff9561e9SKashyap Desai 
3491ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) {
3492ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3493ff9561e9SKashyap Desai 		    mpi3mr_protocols[i].protocol) {
349430e99f05SDan Carpenter 			bytes_written += scnprintf(protocol + bytes_written,
349576a4f7ccSDan Carpenter 				    sizeof(protocol) - bytes_written, "%s%s",
349676a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3497ff9561e9SKashyap Desai 				    mpi3mr_protocols[i].name);
3498ff9561e9SKashyap Desai 		}
3499ff9561e9SKashyap Desai 	}
3500ff9561e9SKashyap Desai 
350176a4f7ccSDan Carpenter 	bytes_written = 0;
3502ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) {
3503ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3504ff9561e9SKashyap Desai 		    mpi3mr_capabilities[i].capability) {
350530e99f05SDan Carpenter 			bytes_written += scnprintf(capabilities + bytes_written,
350676a4f7ccSDan Carpenter 				    sizeof(capabilities) - bytes_written, "%s%s",
350776a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3508ff9561e9SKashyap Desai 				    mpi3mr_capabilities[i].name);
3509ff9561e9SKashyap Desai 		}
3510ff9561e9SKashyap Desai 	}
3511ff9561e9SKashyap Desai 
3512ff9561e9SKashyap Desai 	ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n",
3513ff9561e9SKashyap Desai 		 protocol, capabilities);
3514ff9561e9SKashyap Desai }
3515ff9561e9SKashyap Desai 
3516023ab2a9SKashyap Desai /**
3517824a1566SKashyap Desai  * mpi3mr_cleanup_resources - Free PCI resources
3518824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3519824a1566SKashyap Desai  *
3520824a1566SKashyap Desai  * Unmap PCI device memory and disable PCI device.
3521824a1566SKashyap Desai  *
3522824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3523824a1566SKashyap Desai  */
3524824a1566SKashyap Desai void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
3525824a1566SKashyap Desai {
3526824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3527824a1566SKashyap Desai 
3528824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
3529824a1566SKashyap Desai 
3530824a1566SKashyap Desai 	if (mrioc->sysif_regs) {
3531824a1566SKashyap Desai 		iounmap((void __iomem *)mrioc->sysif_regs);
3532824a1566SKashyap Desai 		mrioc->sysif_regs = NULL;
3533824a1566SKashyap Desai 	}
3534824a1566SKashyap Desai 
3535824a1566SKashyap Desai 	if (pci_is_enabled(pdev)) {
3536824a1566SKashyap Desai 		if (mrioc->bars)
3537824a1566SKashyap Desai 			pci_release_selected_regions(pdev, mrioc->bars);
3538824a1566SKashyap Desai 		pci_disable_device(pdev);
3539824a1566SKashyap Desai 	}
3540824a1566SKashyap Desai }
3541824a1566SKashyap Desai 
3542824a1566SKashyap Desai /**
3543824a1566SKashyap Desai  * mpi3mr_setup_resources - Enable PCI resources
3544824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3545824a1566SKashyap Desai  *
3546824a1566SKashyap Desai  * Enable PCI device memory, MSI-x registers and set DMA mask.
3547824a1566SKashyap Desai  *
3548824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3549824a1566SKashyap Desai  */
3550824a1566SKashyap Desai int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
3551824a1566SKashyap Desai {
3552824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3553824a1566SKashyap Desai 	u32 memap_sz = 0;
3554824a1566SKashyap Desai 	int i, retval = 0, capb = 0;
3555824a1566SKashyap Desai 	u16 message_control;
3556824a1566SKashyap Desai 	u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
3557824a1566SKashyap Desai 	    (((dma_get_required_mask(&pdev->dev) > DMA_BIT_MASK(32)) &&
3558824a1566SKashyap Desai 	    (sizeof(dma_addr_t) > 4)) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
3559824a1566SKashyap Desai 
3560824a1566SKashyap Desai 	if (pci_enable_device_mem(pdev)) {
3561824a1566SKashyap Desai 		ioc_err(mrioc, "pci_enable_device_mem: failed\n");
3562824a1566SKashyap Desai 		retval = -ENODEV;
3563824a1566SKashyap Desai 		goto out_failed;
3564824a1566SKashyap Desai 	}
3565824a1566SKashyap Desai 
3566824a1566SKashyap Desai 	capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3567824a1566SKashyap Desai 	if (!capb) {
3568824a1566SKashyap Desai 		ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
3569824a1566SKashyap Desai 		retval = -ENODEV;
3570824a1566SKashyap Desai 		goto out_failed;
3571824a1566SKashyap Desai 	}
3572824a1566SKashyap Desai 	mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3573824a1566SKashyap Desai 
3574824a1566SKashyap Desai 	if (pci_request_selected_regions(pdev, mrioc->bars,
3575824a1566SKashyap Desai 	    mrioc->driver_name)) {
3576824a1566SKashyap Desai 		ioc_err(mrioc, "pci_request_selected_regions: failed\n");
3577824a1566SKashyap Desai 		retval = -ENODEV;
3578824a1566SKashyap Desai 		goto out_failed;
3579824a1566SKashyap Desai 	}
3580824a1566SKashyap Desai 
3581824a1566SKashyap Desai 	for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
3582824a1566SKashyap Desai 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3583824a1566SKashyap Desai 			mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
3584824a1566SKashyap Desai 			memap_sz = pci_resource_len(pdev, i);
3585824a1566SKashyap Desai 			mrioc->sysif_regs =
3586824a1566SKashyap Desai 			    ioremap(mrioc->sysif_regs_phys, memap_sz);
3587824a1566SKashyap Desai 			break;
3588824a1566SKashyap Desai 		}
3589824a1566SKashyap Desai 	}
3590824a1566SKashyap Desai 
3591824a1566SKashyap Desai 	pci_set_master(pdev);
3592824a1566SKashyap Desai 
3593824a1566SKashyap Desai 	retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
3594824a1566SKashyap Desai 	if (retval) {
3595824a1566SKashyap Desai 		if (dma_mask != DMA_BIT_MASK(32)) {
3596824a1566SKashyap Desai 			ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
3597824a1566SKashyap Desai 			dma_mask = DMA_BIT_MASK(32);
3598824a1566SKashyap Desai 			retval = dma_set_mask_and_coherent(&pdev->dev,
3599824a1566SKashyap Desai 			    dma_mask);
3600824a1566SKashyap Desai 		}
3601824a1566SKashyap Desai 		if (retval) {
3602824a1566SKashyap Desai 			mrioc->dma_mask = 0;
3603824a1566SKashyap Desai 			ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
3604824a1566SKashyap Desai 			goto out_failed;
3605824a1566SKashyap Desai 		}
3606824a1566SKashyap Desai 	}
3607824a1566SKashyap Desai 	mrioc->dma_mask = dma_mask;
3608824a1566SKashyap Desai 
3609824a1566SKashyap Desai 	if (!mrioc->sysif_regs) {
3610824a1566SKashyap Desai 		ioc_err(mrioc,
3611824a1566SKashyap Desai 		    "Unable to map adapter memory or resource not found\n");
3612824a1566SKashyap Desai 		retval = -EINVAL;
3613824a1566SKashyap Desai 		goto out_failed;
3614824a1566SKashyap Desai 	}
3615824a1566SKashyap Desai 
3616824a1566SKashyap Desai 	pci_read_config_word(pdev, capb + 2, &message_control);
3617824a1566SKashyap Desai 	mrioc->msix_count = (message_control & 0x3FF) + 1;
3618824a1566SKashyap Desai 
3619824a1566SKashyap Desai 	pci_save_state(pdev);
3620824a1566SKashyap Desai 
3621824a1566SKashyap Desai 	pci_set_drvdata(pdev, mrioc->shost);
3622824a1566SKashyap Desai 
3623824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
3624824a1566SKashyap Desai 
3625824a1566SKashyap Desai 	ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
3626824a1566SKashyap Desai 	    (unsigned long long)mrioc->sysif_regs_phys,
3627824a1566SKashyap Desai 	    mrioc->sysif_regs, memap_sz);
3628824a1566SKashyap Desai 	ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
3629824a1566SKashyap Desai 	    mrioc->msix_count);
3630afd3a579SSreekanth Reddy 
3631afd3a579SSreekanth Reddy 	if (!reset_devices && poll_queues > 0)
3632afd3a579SSreekanth Reddy 		mrioc->requested_poll_qcount = min_t(int, poll_queues,
3633afd3a579SSreekanth Reddy 				mrioc->msix_count - 2);
3634824a1566SKashyap Desai 	return retval;
3635824a1566SKashyap Desai 
3636824a1566SKashyap Desai out_failed:
3637824a1566SKashyap Desai 	mpi3mr_cleanup_resources(mrioc);
3638824a1566SKashyap Desai 	return retval;
3639824a1566SKashyap Desai }
3640824a1566SKashyap Desai 
3641824a1566SKashyap Desai /**
3642e3605f65SSreekanth Reddy  * mpi3mr_enable_events - Enable required events
3643e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
3644e3605f65SSreekanth Reddy  *
3645e3605f65SSreekanth Reddy  * This routine unmasks the events required by the driver by
3646e3605f65SSreekanth Reddy  * sennding appropriate event mask bitmapt through an event
3647e3605f65SSreekanth Reddy  * notification request.
3648e3605f65SSreekanth Reddy  *
3649e3605f65SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3650e3605f65SSreekanth Reddy  */
3651e3605f65SSreekanth Reddy static int mpi3mr_enable_events(struct mpi3mr_ioc *mrioc)
3652e3605f65SSreekanth Reddy {
3653e3605f65SSreekanth Reddy 	int retval = 0;
3654e3605f65SSreekanth Reddy 	u32  i;
3655e3605f65SSreekanth Reddy 
3656e3605f65SSreekanth Reddy 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3657e3605f65SSreekanth Reddy 		mrioc->event_masks[i] = -1;
3658e3605f65SSreekanth Reddy 
3659e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED);
3660e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED);
3661e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
3662e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
3663e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3664e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY);
3665e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
3666e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
3667e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
3668e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION);
366978b76a07SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PREPARE_FOR_RESET);
3670e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT);
3671e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE);
3672e3605f65SSreekanth Reddy 
3673e3605f65SSreekanth Reddy 	retval = mpi3mr_issue_event_notification(mrioc);
3674e3605f65SSreekanth Reddy 	if (retval)
3675e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to issue event notification %d\n",
3676e3605f65SSreekanth Reddy 		    retval);
3677e3605f65SSreekanth Reddy 	return retval;
3678e3605f65SSreekanth Reddy }
3679e3605f65SSreekanth Reddy 
3680e3605f65SSreekanth Reddy /**
3681824a1566SKashyap Desai  * mpi3mr_init_ioc - Initialize the controller
3682824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3683824a1566SKashyap Desai  *
3684824a1566SKashyap Desai  * This the controller initialization routine, executed either
3685824a1566SKashyap Desai  * after soft reset or from pci probe callback.
3686824a1566SKashyap Desai  * Setup the required resources, memory map the controller
3687824a1566SKashyap Desai  * registers, create admin and operational reply queue pairs,
3688824a1566SKashyap Desai  * allocate required memory for reply pool, sense buffer pool,
3689824a1566SKashyap Desai  * issue IOC init request to the firmware, unmask the events and
3690824a1566SKashyap Desai  * issue port enable to discover SAS/SATA/NVMe devies and RAID
3691824a1566SKashyap Desai  * volumes.
3692824a1566SKashyap Desai  *
3693824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3694824a1566SKashyap Desai  */
3695fe6db615SSreekanth Reddy int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc)
3696824a1566SKashyap Desai {
3697824a1566SKashyap Desai 	int retval = 0;
3698fe6db615SSreekanth Reddy 	u8 retry = 0;
3699824a1566SKashyap Desai 	struct mpi3_ioc_facts_data facts_data;
3700f10af057SSreekanth Reddy 	u32 sz;
3701824a1566SKashyap Desai 
3702fe6db615SSreekanth Reddy retry_init:
3703824a1566SKashyap Desai 	retval = mpi3mr_bring_ioc_ready(mrioc);
3704824a1566SKashyap Desai 	if (retval) {
3705824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
3706824a1566SKashyap Desai 		    retval);
3707fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3708824a1566SKashyap Desai 	}
3709824a1566SKashyap Desai 
3710824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 1);
3711824a1566SKashyap Desai 	if (retval) {
3712824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to setup ISR error %d\n",
3713824a1566SKashyap Desai 		    retval);
3714fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3715824a1566SKashyap Desai 	}
3716824a1566SKashyap Desai 
3717824a1566SKashyap Desai 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3718824a1566SKashyap Desai 	if (retval) {
3719824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
3720824a1566SKashyap Desai 		    retval);
3721824a1566SKashyap Desai 		goto out_failed;
3722824a1566SKashyap Desai 	}
3723824a1566SKashyap Desai 
3724c5758fc7SSreekanth Reddy 	mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
3725c5758fc7SSreekanth Reddy 
3726f10af057SSreekanth Reddy 	mrioc->num_io_throttle_group = mrioc->facts.max_io_throttle_group;
3727f10af057SSreekanth Reddy 	atomic_set(&mrioc->pend_large_data_sz, 0);
3728f10af057SSreekanth Reddy 
3729c5758fc7SSreekanth Reddy 	if (reset_devices)
3730c5758fc7SSreekanth Reddy 		mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
3731c5758fc7SSreekanth Reddy 		    MPI3MR_HOST_IOS_KDUMP);
3732c5758fc7SSreekanth Reddy 
3733c5758fc7SSreekanth Reddy 	mrioc->reply_sz = mrioc->facts.reply_sz;
3734fe6db615SSreekanth Reddy 
3735824a1566SKashyap Desai 	retval = mpi3mr_check_reset_dma_mask(mrioc);
3736824a1566SKashyap Desai 	if (retval) {
3737824a1566SKashyap Desai 		ioc_err(mrioc, "Resetting dma mask failed %d\n",
3738824a1566SKashyap Desai 		    retval);
3739fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3740fb9b0457SKashyap Desai 	}
3741824a1566SKashyap Desai 
3742ff9561e9SKashyap Desai 	mpi3mr_print_ioc_info(mrioc);
3743ff9561e9SKashyap Desai 
3744*32d457d5SSreekanth Reddy 	dprint_init(mrioc, "allocating config page buffers\n");
3745*32d457d5SSreekanth Reddy 	mrioc->cfg_page = dma_alloc_coherent(&mrioc->pdev->dev,
3746*32d457d5SSreekanth Reddy 	    MPI3MR_DEFAULT_CFG_PAGE_SZ, &mrioc->cfg_page_dma, GFP_KERNEL);
3747*32d457d5SSreekanth Reddy 	if (!mrioc->cfg_page)
3748*32d457d5SSreekanth Reddy 		goto out_failed_noretry;
3749*32d457d5SSreekanth Reddy 
3750*32d457d5SSreekanth Reddy 	mrioc->cfg_page_sz = MPI3MR_DEFAULT_CFG_PAGE_SZ;
3751*32d457d5SSreekanth Reddy 
3752824a1566SKashyap Desai 	retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
3753824a1566SKashyap Desai 	if (retval) {
3754824a1566SKashyap Desai 		ioc_err(mrioc,
3755824a1566SKashyap Desai 		    "%s :Failed to allocated reply sense buffers %d\n",
3756824a1566SKashyap Desai 		    __func__, retval);
3757fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3758824a1566SKashyap Desai 	}
3759824a1566SKashyap Desai 
3760824a1566SKashyap Desai 	retval = mpi3mr_alloc_chain_bufs(mrioc);
3761824a1566SKashyap Desai 	if (retval) {
3762824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
3763824a1566SKashyap Desai 		    retval);
3764fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3765fb9b0457SKashyap Desai 	}
3766824a1566SKashyap Desai 
3767824a1566SKashyap Desai 	retval = mpi3mr_issue_iocinit(mrioc);
3768824a1566SKashyap Desai 	if (retval) {
3769824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
3770824a1566SKashyap Desai 		    retval);
3771824a1566SKashyap Desai 		goto out_failed;
3772824a1566SKashyap Desai 	}
3773824a1566SKashyap Desai 
37742ac794baSSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
37752ac794baSSreekanth Reddy 	if (retval) {
37762ac794baSSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
37772ac794baSSreekanth Reddy 		goto out_failed;
37782ac794baSSreekanth Reddy 	}
37792ac794baSSreekanth Reddy 
3780824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 0);
3781824a1566SKashyap Desai 	if (retval) {
3782824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
3783824a1566SKashyap Desai 		    retval);
3784fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3785fb9b0457SKashyap Desai 	}
3786824a1566SKashyap Desai 
3787c9566231SKashyap Desai 	retval = mpi3mr_create_op_queues(mrioc);
3788c9566231SKashyap Desai 	if (retval) {
3789c9566231SKashyap Desai 		ioc_err(mrioc, "Failed to create OpQueues error %d\n",
3790c9566231SKashyap Desai 		    retval);
3791c9566231SKashyap Desai 		goto out_failed;
3792c9566231SKashyap Desai 	}
3793c9566231SKashyap Desai 
379443ca1100SSumit Saxena 	if (!mrioc->pel_seqnum_virt) {
379543ca1100SSumit Saxena 		dprint_init(mrioc, "allocating memory for pel_seqnum_virt\n");
379643ca1100SSumit Saxena 		mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
379743ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
379843ca1100SSumit Saxena 		    mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
379943ca1100SSumit Saxena 		    GFP_KERNEL);
3800bc7896d3SDan Carpenter 		if (!mrioc->pel_seqnum_virt) {
3801bc7896d3SDan Carpenter 			retval = -ENOMEM;
380243ca1100SSumit Saxena 			goto out_failed_noretry;
380343ca1100SSumit Saxena 		}
3804bc7896d3SDan Carpenter 	}
380543ca1100SSumit Saxena 
3806f10af057SSreekanth Reddy 	if (!mrioc->throttle_groups && mrioc->num_io_throttle_group) {
3807f10af057SSreekanth Reddy 		dprint_init(mrioc, "allocating memory for throttle groups\n");
3808f10af057SSreekanth Reddy 		sz = sizeof(struct mpi3mr_throttle_group_info);
3809f10af057SSreekanth Reddy 		mrioc->throttle_groups = (struct mpi3mr_throttle_group_info *)
3810f10af057SSreekanth Reddy 		    kcalloc(mrioc->num_io_throttle_group, sz, GFP_KERNEL);
3811f10af057SSreekanth Reddy 		if (!mrioc->throttle_groups)
3812f10af057SSreekanth Reddy 			goto out_failed_noretry;
3813f10af057SSreekanth Reddy 	}
3814f10af057SSreekanth Reddy 
3815e3605f65SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
381613ef29eaSKashyap Desai 	if (retval) {
3817e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events %d\n",
381813ef29eaSKashyap Desai 		    retval);
381913ef29eaSKashyap Desai 		goto out_failed;
382013ef29eaSKashyap Desai 	}
382113ef29eaSKashyap Desai 
3822fe6db615SSreekanth Reddy 	ioc_info(mrioc, "controller initialization completed successfully\n");
3823824a1566SKashyap Desai 	return retval;
3824824a1566SKashyap Desai out_failed:
3825fe6db615SSreekanth Reddy 	if (retry < 2) {
3826fe6db615SSreekanth Reddy 		retry++;
3827fe6db615SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller initialization, retry_count:%d\n",
3828fe6db615SSreekanth Reddy 		    retry);
3829fe6db615SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3830fe6db615SSreekanth Reddy 		goto retry_init;
3831fe6db615SSreekanth Reddy 	}
3832fe6db615SSreekanth Reddy out_failed_noretry:
3833fe6db615SSreekanth Reddy 	ioc_err(mrioc, "controller initialization failed\n");
3834fe6db615SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3835fe6db615SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3836fe6db615SSreekanth Reddy 	mrioc->unrecoverable = 1;
3837824a1566SKashyap Desai 	return retval;
3838824a1566SKashyap Desai }
3839824a1566SKashyap Desai 
3840c0b00a93SSreekanth Reddy /**
3841c0b00a93SSreekanth Reddy  * mpi3mr_reinit_ioc - Re-Initialize the controller
3842c0b00a93SSreekanth Reddy  * @mrioc: Adapter instance reference
3843c0b00a93SSreekanth Reddy  * @is_resume: Called from resume or reset path
3844c0b00a93SSreekanth Reddy  *
3845c0b00a93SSreekanth Reddy  * This the controller re-initialization routine, executed from
3846c0b00a93SSreekanth Reddy  * the soft reset handler or resume callback. Creates
3847c0b00a93SSreekanth Reddy  * operational reply queue pairs, allocate required memory for
3848c0b00a93SSreekanth Reddy  * reply pool, sense buffer pool, issue IOC init request to the
3849c0b00a93SSreekanth Reddy  * firmware, unmask the events and issue port enable to discover
3850c0b00a93SSreekanth Reddy  * SAS/SATA/NVMe devices and RAID volumes.
3851c0b00a93SSreekanth Reddy  *
3852c0b00a93SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3853c0b00a93SSreekanth Reddy  */
3854fe6db615SSreekanth Reddy int mpi3mr_reinit_ioc(struct mpi3mr_ioc *mrioc, u8 is_resume)
3855fe6db615SSreekanth Reddy {
3856c0b00a93SSreekanth Reddy 	int retval = 0;
3857c0b00a93SSreekanth Reddy 	u8 retry = 0;
3858c0b00a93SSreekanth Reddy 	struct mpi3_ioc_facts_data facts_data;
3859fe6db615SSreekanth Reddy 
3860c0b00a93SSreekanth Reddy retry_init:
3861c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "bringing up the controller to ready state\n");
3862c0b00a93SSreekanth Reddy 	retval = mpi3mr_bring_ioc_ready(mrioc);
3863c0b00a93SSreekanth Reddy 	if (retval) {
3864c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to bring to ready state\n");
3865c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3866c0b00a93SSreekanth Reddy 	}
3867c0b00a93SSreekanth Reddy 
3868c0b00a93SSreekanth Reddy 	if (is_resume) {
3869c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up single ISR\n");
3870c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 1);
3871c0b00a93SSreekanth Reddy 		if (retval) {
3872c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to setup ISR\n");
3873c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3874c0b00a93SSreekanth Reddy 		}
3875c0b00a93SSreekanth Reddy 	} else
3876c0b00a93SSreekanth Reddy 		mpi3mr_ioc_enable_intr(mrioc);
3877c0b00a93SSreekanth Reddy 
3878c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting ioc_facts\n");
3879c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3880c0b00a93SSreekanth Reddy 	if (retval) {
3881c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get ioc_facts\n");
3882c0b00a93SSreekanth Reddy 		goto out_failed;
3883c0b00a93SSreekanth Reddy 	}
3884c0b00a93SSreekanth Reddy 
3885c5758fc7SSreekanth Reddy 	dprint_reset(mrioc, "validating ioc_facts\n");
3886c5758fc7SSreekanth Reddy 	retval = mpi3mr_revalidate_factsdata(mrioc);
3887c5758fc7SSreekanth Reddy 	if (retval) {
3888c5758fc7SSreekanth Reddy 		ioc_err(mrioc, "failed to revalidate ioc_facts data\n");
3889c5758fc7SSreekanth Reddy 		goto out_failed_noretry;
3890c5758fc7SSreekanth Reddy 	}
3891c0b00a93SSreekanth Reddy 
3892c0b00a93SSreekanth Reddy 	mpi3mr_print_ioc_info(mrioc);
3893c0b00a93SSreekanth Reddy 
3894c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "sending ioc_init\n");
3895c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocinit(mrioc);
3896c0b00a93SSreekanth Reddy 	if (retval) {
3897c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to send ioc_init\n");
3898c0b00a93SSreekanth Reddy 		goto out_failed;
3899c0b00a93SSreekanth Reddy 	}
3900c0b00a93SSreekanth Reddy 
3901c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting package version\n");
3902c0b00a93SSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
3903c0b00a93SSreekanth Reddy 	if (retval) {
3904c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
3905c0b00a93SSreekanth Reddy 		goto out_failed;
3906c0b00a93SSreekanth Reddy 	}
3907c0b00a93SSreekanth Reddy 
3908c0b00a93SSreekanth Reddy 	if (is_resume) {
3909c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up multiple ISR\n");
3910c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 0);
3911c0b00a93SSreekanth Reddy 		if (retval) {
3912c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to re-setup ISR\n");
3913c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3914c0b00a93SSreekanth Reddy 		}
3915c0b00a93SSreekanth Reddy 	}
3916c0b00a93SSreekanth Reddy 
3917c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "creating operational queue pairs\n");
3918c0b00a93SSreekanth Reddy 	retval = mpi3mr_create_op_queues(mrioc);
3919c0b00a93SSreekanth Reddy 	if (retval) {
3920c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to create operational queue pairs\n");
3921c0b00a93SSreekanth Reddy 		goto out_failed;
3922c0b00a93SSreekanth Reddy 	}
3923c0b00a93SSreekanth Reddy 
392443ca1100SSumit Saxena 	if (!mrioc->pel_seqnum_virt) {
392543ca1100SSumit Saxena 		dprint_reset(mrioc, "allocating memory for pel_seqnum_virt\n");
392643ca1100SSumit Saxena 		mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
392743ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
392843ca1100SSumit Saxena 		    mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
392943ca1100SSumit Saxena 		    GFP_KERNEL);
3930bc7896d3SDan Carpenter 		if (!mrioc->pel_seqnum_virt) {
3931bc7896d3SDan Carpenter 			retval = -ENOMEM;
393243ca1100SSumit Saxena 			goto out_failed_noretry;
393343ca1100SSumit Saxena 		}
3934bc7896d3SDan Carpenter 	}
393543ca1100SSumit Saxena 
3936c0b00a93SSreekanth Reddy 	if (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q) {
3937c0b00a93SSreekanth Reddy 		ioc_err(mrioc,
39385867b856SColin Ian King 		    "cannot create minimum number of operational queues expected:%d created:%d\n",
3939c0b00a93SSreekanth Reddy 		    mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q);
3940c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3941c0b00a93SSreekanth Reddy 	}
3942c0b00a93SSreekanth Reddy 
3943c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "enabling events\n");
3944c0b00a93SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
3945c0b00a93SSreekanth Reddy 	if (retval) {
3946c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events\n");
3947c0b00a93SSreekanth Reddy 		goto out_failed;
3948c0b00a93SSreekanth Reddy 	}
3949c0b00a93SSreekanth Reddy 
3950c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "sending port enable\n");
3951c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_port_enable(mrioc, 0);
3952c0b00a93SSreekanth Reddy 	if (retval) {
3953c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to issue port enable\n");
3954c0b00a93SSreekanth Reddy 		goto out_failed;
3955c0b00a93SSreekanth Reddy 	}
3956c0b00a93SSreekanth Reddy 
3957c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "controller %s completed successfully\n",
3958c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
3959c0b00a93SSreekanth Reddy 	return retval;
3960c0b00a93SSreekanth Reddy out_failed:
3961c0b00a93SSreekanth Reddy 	if (retry < 2) {
3962c0b00a93SSreekanth Reddy 		retry++;
3963c0b00a93SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller %s, retry_count:%d\n",
3964c0b00a93SSreekanth Reddy 		    (is_resume)?"resume":"re-initialization", retry);
3965c0b00a93SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3966c0b00a93SSreekanth Reddy 		goto retry_init;
3967c0b00a93SSreekanth Reddy 	}
3968c0b00a93SSreekanth Reddy out_failed_noretry:
3969c0b00a93SSreekanth Reddy 	ioc_err(mrioc, "controller %s is failed\n",
3970c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
3971c0b00a93SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3972c0b00a93SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3973c0b00a93SSreekanth Reddy 	mrioc->unrecoverable = 1;
3974c0b00a93SSreekanth Reddy 	return retval;
3975fe6db615SSreekanth Reddy }
3976fe6db615SSreekanth Reddy 
3977824a1566SKashyap Desai /**
3978fb9b0457SKashyap Desai  * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's
3979fb9b0457SKashyap Desai  *					segments
3980fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
3981fb9b0457SKashyap Desai  * @qidx: Operational reply queue index
3982fb9b0457SKashyap Desai  *
3983fb9b0457SKashyap Desai  * Return: Nothing.
3984fb9b0457SKashyap Desai  */
3985fb9b0457SKashyap Desai static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3986fb9b0457SKashyap Desai {
3987fb9b0457SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
3988fb9b0457SKashyap Desai 	struct segments *segments;
3989fb9b0457SKashyap Desai 	int i, size;
3990fb9b0457SKashyap Desai 
3991fb9b0457SKashyap Desai 	if (!op_reply_q->q_segments)
3992fb9b0457SKashyap Desai 		return;
3993fb9b0457SKashyap Desai 
3994fb9b0457SKashyap Desai 	size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz;
3995fb9b0457SKashyap Desai 	segments = op_reply_q->q_segments;
3996fb9b0457SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++)
3997fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
3998fb9b0457SKashyap Desai }
3999fb9b0457SKashyap Desai 
4000fb9b0457SKashyap Desai /**
4001fb9b0457SKashyap Desai  * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's
4002fb9b0457SKashyap Desai  *					segments
4003fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4004fb9b0457SKashyap Desai  * @qidx: Operational request queue index
4005fb9b0457SKashyap Desai  *
4006fb9b0457SKashyap Desai  * Return: Nothing.
4007fb9b0457SKashyap Desai  */
4008fb9b0457SKashyap Desai static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
4009fb9b0457SKashyap Desai {
4010fb9b0457SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
4011fb9b0457SKashyap Desai 	struct segments *segments;
4012fb9b0457SKashyap Desai 	int i, size;
4013fb9b0457SKashyap Desai 
4014fb9b0457SKashyap Desai 	if (!op_req_q->q_segments)
4015fb9b0457SKashyap Desai 		return;
4016fb9b0457SKashyap Desai 
4017fb9b0457SKashyap Desai 	size = op_req_q->segment_qd * mrioc->facts.op_req_sz;
4018fb9b0457SKashyap Desai 	segments = op_req_q->q_segments;
4019fb9b0457SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++)
4020fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
4021fb9b0457SKashyap Desai }
4022fb9b0457SKashyap Desai 
4023fb9b0457SKashyap Desai /**
4024fb9b0457SKashyap Desai  * mpi3mr_memset_buffers - memset memory for a controller
4025fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4026fb9b0457SKashyap Desai  *
4027fb9b0457SKashyap Desai  * clear all the memory allocated for a controller, typically
4028fb9b0457SKashyap Desai  * called post reset to reuse the memory allocated during the
4029fb9b0457SKashyap Desai  * controller init.
4030fb9b0457SKashyap Desai  *
4031fb9b0457SKashyap Desai  * Return: Nothing.
4032fb9b0457SKashyap Desai  */
40330da66348SKashyap Desai void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)
4034fb9b0457SKashyap Desai {
4035fb9b0457SKashyap Desai 	u16 i;
4036f10af057SSreekanth Reddy 	struct mpi3mr_throttle_group_info *tg;
4037fb9b0457SKashyap Desai 
4038fe6db615SSreekanth Reddy 	mrioc->change_count = 0;
4039afd3a579SSreekanth Reddy 	mrioc->active_poll_qcount = 0;
4040afd3a579SSreekanth Reddy 	mrioc->default_qcount = 0;
4041fe6db615SSreekanth Reddy 	if (mrioc->admin_req_base)
4042fb9b0457SKashyap Desai 		memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz);
4043fe6db615SSreekanth Reddy 	if (mrioc->admin_reply_base)
4044fb9b0457SKashyap Desai 		memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz);
4045fb9b0457SKashyap Desai 
4046fe6db615SSreekanth Reddy 	if (mrioc->init_cmds.reply) {
4047fb9b0457SKashyap Desai 		memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply));
4048f5e6d5a3SSumit Saxena 		memset(mrioc->bsg_cmds.reply, 0,
4049f5e6d5a3SSumit Saxena 		    sizeof(*mrioc->bsg_cmds.reply));
4050e844adb1SKashyap Desai 		memset(mrioc->host_tm_cmds.reply, 0,
4051e844adb1SKashyap Desai 		    sizeof(*mrioc->host_tm_cmds.reply));
405243ca1100SSumit Saxena 		memset(mrioc->pel_cmds.reply, 0,
405343ca1100SSumit Saxena 		    sizeof(*mrioc->pel_cmds.reply));
405443ca1100SSumit Saxena 		memset(mrioc->pel_abort_cmd.reply, 0,
405543ca1100SSumit Saxena 		    sizeof(*mrioc->pel_abort_cmd.reply));
4056fb9b0457SKashyap Desai 		for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
4057fb9b0457SKashyap Desai 			memset(mrioc->dev_rmhs_cmds[i].reply, 0,
4058fb9b0457SKashyap Desai 			    sizeof(*mrioc->dev_rmhs_cmds[i].reply));
4059c1af985dSSreekanth Reddy 		for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
4060c1af985dSSreekanth Reddy 			memset(mrioc->evtack_cmds[i].reply, 0,
4061c1af985dSSreekanth Reddy 			    sizeof(*mrioc->evtack_cmds[i].reply));
4062fb9b0457SKashyap Desai 		memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
4063fb9b0457SKashyap Desai 		memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
4064c1af985dSSreekanth Reddy 		memset(mrioc->evtack_cmds_bitmap, 0,
4065c1af985dSSreekanth Reddy 		    mrioc->evtack_cmds_bitmap_sz);
4066fe6db615SSreekanth Reddy 	}
4067fb9b0457SKashyap Desai 
4068fb9b0457SKashyap Desai 	for (i = 0; i < mrioc->num_queues; i++) {
4069fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].qid = 0;
4070fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ci = 0;
4071fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].num_replies = 0;
4072fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ephase = 0;
4073463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
4074463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0);
4075fb9b0457SKashyap Desai 		mpi3mr_memset_op_reply_q_buffers(mrioc, i);
4076fb9b0457SKashyap Desai 
4077fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].ci = 0;
4078fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].pi = 0;
4079fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].num_requests = 0;
4080fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].qid = 0;
4081fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].reply_qid = 0;
4082fb9b0457SKashyap Desai 		spin_lock_init(&mrioc->req_qinfo[i].q_lock);
4083fb9b0457SKashyap Desai 		mpi3mr_memset_op_req_q_buffers(mrioc, i);
4084fb9b0457SKashyap Desai 	}
4085f10af057SSreekanth Reddy 
4086f10af057SSreekanth Reddy 	atomic_set(&mrioc->pend_large_data_sz, 0);
4087f10af057SSreekanth Reddy 	if (mrioc->throttle_groups) {
4088f10af057SSreekanth Reddy 		tg = mrioc->throttle_groups;
4089f10af057SSreekanth Reddy 		for (i = 0; i < mrioc->num_io_throttle_group; i++, tg++) {
4090f10af057SSreekanth Reddy 			tg->id = 0;
4091cf1ce8b7SSreekanth Reddy 			tg->fw_qd = 0;
4092cf1ce8b7SSreekanth Reddy 			tg->modified_qd = 0;
4093f10af057SSreekanth Reddy 			tg->io_divert = 0;
4094cf1ce8b7SSreekanth Reddy 			tg->need_qd_reduction = 0;
4095f10af057SSreekanth Reddy 			tg->high = 0;
4096f10af057SSreekanth Reddy 			tg->low = 0;
4097cf1ce8b7SSreekanth Reddy 			tg->qd_reduction = 0;
4098f10af057SSreekanth Reddy 			atomic_set(&tg->pend_large_data_sz, 0);
4099f10af057SSreekanth Reddy 		}
4100f10af057SSreekanth Reddy 	}
4101fb9b0457SKashyap Desai }
4102fb9b0457SKashyap Desai 
4103fb9b0457SKashyap Desai /**
4104824a1566SKashyap Desai  * mpi3mr_free_mem - Free memory allocated for a controller
4105824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4106824a1566SKashyap Desai  *
4107824a1566SKashyap Desai  * Free all the memory allocated for a controller.
4108824a1566SKashyap Desai  *
4109824a1566SKashyap Desai  * Return: Nothing.
4110824a1566SKashyap Desai  */
4111fe6db615SSreekanth Reddy void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
4112824a1566SKashyap Desai {
4113824a1566SKashyap Desai 	u16 i;
4114824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info;
4115824a1566SKashyap Desai 
4116824a1566SKashyap Desai 	if (mrioc->sense_buf_pool) {
4117824a1566SKashyap Desai 		if (mrioc->sense_buf)
4118824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
4119824a1566SKashyap Desai 			    mrioc->sense_buf_dma);
4120824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_pool);
4121824a1566SKashyap Desai 		mrioc->sense_buf = NULL;
4122824a1566SKashyap Desai 		mrioc->sense_buf_pool = NULL;
4123824a1566SKashyap Desai 	}
4124824a1566SKashyap Desai 	if (mrioc->sense_buf_q_pool) {
4125824a1566SKashyap Desai 		if (mrioc->sense_buf_q)
4126824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_q_pool,
4127824a1566SKashyap Desai 			    mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
4128824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_q_pool);
4129824a1566SKashyap Desai 		mrioc->sense_buf_q = NULL;
4130824a1566SKashyap Desai 		mrioc->sense_buf_q_pool = NULL;
4131824a1566SKashyap Desai 	}
4132824a1566SKashyap Desai 
4133824a1566SKashyap Desai 	if (mrioc->reply_buf_pool) {
4134824a1566SKashyap Desai 		if (mrioc->reply_buf)
4135824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
4136824a1566SKashyap Desai 			    mrioc->reply_buf_dma);
4137824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_buf_pool);
4138824a1566SKashyap Desai 		mrioc->reply_buf = NULL;
4139824a1566SKashyap Desai 		mrioc->reply_buf_pool = NULL;
4140824a1566SKashyap Desai 	}
4141824a1566SKashyap Desai 	if (mrioc->reply_free_q_pool) {
4142824a1566SKashyap Desai 		if (mrioc->reply_free_q)
4143824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_free_q_pool,
4144824a1566SKashyap Desai 			    mrioc->reply_free_q, mrioc->reply_free_q_dma);
4145824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_free_q_pool);
4146824a1566SKashyap Desai 		mrioc->reply_free_q = NULL;
4147824a1566SKashyap Desai 		mrioc->reply_free_q_pool = NULL;
4148824a1566SKashyap Desai 	}
4149824a1566SKashyap Desai 
4150c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_req_q; i++)
4151c9566231SKashyap Desai 		mpi3mr_free_op_req_q_segments(mrioc, i);
4152c9566231SKashyap Desai 
4153c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_reply_q; i++)
4154c9566231SKashyap Desai 		mpi3mr_free_op_reply_q_segments(mrioc, i);
4155c9566231SKashyap Desai 
4156824a1566SKashyap Desai 	for (i = 0; i < mrioc->intr_info_count; i++) {
4157824a1566SKashyap Desai 		intr_info = mrioc->intr_info + i;
4158824a1566SKashyap Desai 		intr_info->op_reply_q = NULL;
4159824a1566SKashyap Desai 	}
4160824a1566SKashyap Desai 
4161824a1566SKashyap Desai 	kfree(mrioc->req_qinfo);
4162824a1566SKashyap Desai 	mrioc->req_qinfo = NULL;
4163824a1566SKashyap Desai 	mrioc->num_op_req_q = 0;
4164824a1566SKashyap Desai 
4165824a1566SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
4166824a1566SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
4167824a1566SKashyap Desai 	mrioc->num_op_reply_q = 0;
4168824a1566SKashyap Desai 
4169824a1566SKashyap Desai 	kfree(mrioc->init_cmds.reply);
4170824a1566SKashyap Desai 	mrioc->init_cmds.reply = NULL;
4171824a1566SKashyap Desai 
4172f5e6d5a3SSumit Saxena 	kfree(mrioc->bsg_cmds.reply);
4173f5e6d5a3SSumit Saxena 	mrioc->bsg_cmds.reply = NULL;
4174f5e6d5a3SSumit Saxena 
4175e844adb1SKashyap Desai 	kfree(mrioc->host_tm_cmds.reply);
4176e844adb1SKashyap Desai 	mrioc->host_tm_cmds.reply = NULL;
4177e844adb1SKashyap Desai 
417843ca1100SSumit Saxena 	kfree(mrioc->pel_cmds.reply);
417943ca1100SSumit Saxena 	mrioc->pel_cmds.reply = NULL;
418043ca1100SSumit Saxena 
418143ca1100SSumit Saxena 	kfree(mrioc->pel_abort_cmd.reply);
418243ca1100SSumit Saxena 	mrioc->pel_abort_cmd.reply = NULL;
418343ca1100SSumit Saxena 
4184c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
4185c1af985dSSreekanth Reddy 		kfree(mrioc->evtack_cmds[i].reply);
4186c1af985dSSreekanth Reddy 		mrioc->evtack_cmds[i].reply = NULL;
4187c1af985dSSreekanth Reddy 	}
4188c1af985dSSreekanth Reddy 
4189e844adb1SKashyap Desai 	kfree(mrioc->removepend_bitmap);
4190e844adb1SKashyap Desai 	mrioc->removepend_bitmap = NULL;
4191e844adb1SKashyap Desai 
4192e844adb1SKashyap Desai 	kfree(mrioc->devrem_bitmap);
4193e844adb1SKashyap Desai 	mrioc->devrem_bitmap = NULL;
4194e844adb1SKashyap Desai 
4195c1af985dSSreekanth Reddy 	kfree(mrioc->evtack_cmds_bitmap);
4196c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap = NULL;
4197c1af985dSSreekanth Reddy 
4198824a1566SKashyap Desai 	kfree(mrioc->chain_bitmap);
4199824a1566SKashyap Desai 	mrioc->chain_bitmap = NULL;
4200824a1566SKashyap Desai 
420113ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
420213ef29eaSKashyap Desai 		kfree(mrioc->dev_rmhs_cmds[i].reply);
420313ef29eaSKashyap Desai 		mrioc->dev_rmhs_cmds[i].reply = NULL;
420413ef29eaSKashyap Desai 	}
420513ef29eaSKashyap Desai 
4206824a1566SKashyap Desai 	if (mrioc->chain_buf_pool) {
4207824a1566SKashyap Desai 		for (i = 0; i < mrioc->chain_buf_count; i++) {
4208824a1566SKashyap Desai 			if (mrioc->chain_sgl_list[i].addr) {
4209824a1566SKashyap Desai 				dma_pool_free(mrioc->chain_buf_pool,
4210824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].addr,
4211824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].dma_addr);
4212824a1566SKashyap Desai 				mrioc->chain_sgl_list[i].addr = NULL;
4213824a1566SKashyap Desai 			}
4214824a1566SKashyap Desai 		}
4215824a1566SKashyap Desai 		dma_pool_destroy(mrioc->chain_buf_pool);
4216824a1566SKashyap Desai 		mrioc->chain_buf_pool = NULL;
4217824a1566SKashyap Desai 	}
4218824a1566SKashyap Desai 
4219824a1566SKashyap Desai 	kfree(mrioc->chain_sgl_list);
4220824a1566SKashyap Desai 	mrioc->chain_sgl_list = NULL;
4221824a1566SKashyap Desai 
4222824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
4223824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
4224824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
4225824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
4226824a1566SKashyap Desai 	}
4227824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
4228824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
4229824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
4230824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
4231824a1566SKashyap Desai 	}
423243ca1100SSumit Saxena 
423343ca1100SSumit Saxena 	if (mrioc->pel_seqnum_virt) {
423443ca1100SSumit Saxena 		dma_free_coherent(&mrioc->pdev->dev, mrioc->pel_seqnum_sz,
423543ca1100SSumit Saxena 		    mrioc->pel_seqnum_virt, mrioc->pel_seqnum_dma);
423643ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = NULL;
423743ca1100SSumit Saxena 	}
423843ca1100SSumit Saxena 
423943ca1100SSumit Saxena 	kfree(mrioc->logdata_buf);
424043ca1100SSumit Saxena 	mrioc->logdata_buf = NULL;
424143ca1100SSumit Saxena 
4242824a1566SKashyap Desai }
4243824a1566SKashyap Desai 
4244824a1566SKashyap Desai /**
4245824a1566SKashyap Desai  * mpi3mr_issue_ioc_shutdown - shutdown controller
4246824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4247824a1566SKashyap Desai  *
4248824a1566SKashyap Desai  * Send shutodwn notification to the controller and wait for the
4249824a1566SKashyap Desai  * shutdown_timeout for it to be completed.
4250824a1566SKashyap Desai  *
4251824a1566SKashyap Desai  * Return: Nothing.
4252824a1566SKashyap Desai  */
4253824a1566SKashyap Desai static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
4254824a1566SKashyap Desai {
4255824a1566SKashyap Desai 	u32 ioc_config, ioc_status;
4256824a1566SKashyap Desai 	u8 retval = 1;
4257824a1566SKashyap Desai 	u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
4258824a1566SKashyap Desai 
4259824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing shutdown Notification\n");
4260824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
4261824a1566SKashyap Desai 		ioc_warn(mrioc,
4262824a1566SKashyap Desai 		    "IOC is unrecoverable shutdown is not issued\n");
4263824a1566SKashyap Desai 		return;
4264824a1566SKashyap Desai 	}
4265824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4266824a1566SKashyap Desai 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4267824a1566SKashyap Desai 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
4268824a1566SKashyap Desai 		ioc_info(mrioc, "shutdown already in progress\n");
4269824a1566SKashyap Desai 		return;
4270824a1566SKashyap Desai 	}
4271824a1566SKashyap Desai 
4272824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
4273824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
4274ec5ebd2cSSreekanth Reddy 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
4275824a1566SKashyap Desai 
4276824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
4277824a1566SKashyap Desai 
4278824a1566SKashyap Desai 	if (mrioc->facts.shutdown_timeout)
4279824a1566SKashyap Desai 		timeout = mrioc->facts.shutdown_timeout * 10;
4280824a1566SKashyap Desai 
4281824a1566SKashyap Desai 	do {
4282824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4283824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4284824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
4285824a1566SKashyap Desai 			retval = 0;
4286824a1566SKashyap Desai 			break;
4287824a1566SKashyap Desai 		}
4288824a1566SKashyap Desai 		msleep(100);
4289824a1566SKashyap Desai 	} while (--timeout);
4290824a1566SKashyap Desai 
4291824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4292824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
4293824a1566SKashyap Desai 
4294824a1566SKashyap Desai 	if (retval) {
4295824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4296824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
4297824a1566SKashyap Desai 			ioc_warn(mrioc,
4298824a1566SKashyap Desai 			    "shutdown still in progress after timeout\n");
4299824a1566SKashyap Desai 	}
4300824a1566SKashyap Desai 
4301824a1566SKashyap Desai 	ioc_info(mrioc,
4302824a1566SKashyap Desai 	    "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
4303824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status,
4304824a1566SKashyap Desai 	    ioc_config);
4305824a1566SKashyap Desai }
4306824a1566SKashyap Desai 
4307824a1566SKashyap Desai /**
4308824a1566SKashyap Desai  * mpi3mr_cleanup_ioc - Cleanup controller
4309824a1566SKashyap Desai  * @mrioc: Adapter instance reference
43103bb3c24eSYang Li  *
4311824a1566SKashyap Desai  * controller cleanup handler, Message unit reset or soft reset
4312fe6db615SSreekanth Reddy  * and shutdown notification is issued to the controller.
4313824a1566SKashyap Desai  *
4314824a1566SKashyap Desai  * Return: Nothing.
4315824a1566SKashyap Desai  */
4316fe6db615SSreekanth Reddy void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)
4317824a1566SKashyap Desai {
4318824a1566SKashyap Desai 	enum mpi3mr_iocstate ioc_state;
4319824a1566SKashyap Desai 
4320fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "cleaning up the controller\n");
4321824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4322824a1566SKashyap Desai 
4323824a1566SKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
4324824a1566SKashyap Desai 
4325824a1566SKashyap Desai 	if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
4326824a1566SKashyap Desai 	    (ioc_state == MRIOC_STATE_READY)) {
4327824a1566SKashyap Desai 		if (mpi3mr_issue_and_process_mur(mrioc,
4328824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
4329824a1566SKashyap Desai 			mpi3mr_issue_reset(mrioc,
4330824a1566SKashyap Desai 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
4331824a1566SKashyap Desai 			    MPI3MR_RESET_FROM_MUR_FAILURE);
4332824a1566SKashyap Desai 		mpi3mr_issue_ioc_shutdown(mrioc);
4333824a1566SKashyap Desai 	}
4334fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "controller cleanup completed\n");
4335fb9b0457SKashyap Desai }
4336fb9b0457SKashyap Desai 
4337fb9b0457SKashyap Desai /**
4338fb9b0457SKashyap Desai  * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
4339fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4340fb9b0457SKashyap Desai  * @cmdptr: Internal command tracker
4341fb9b0457SKashyap Desai  *
4342fb9b0457SKashyap Desai  * Complete an internal driver commands with state indicating it
4343fb9b0457SKashyap Desai  * is completed due to reset.
4344fb9b0457SKashyap Desai  *
4345fb9b0457SKashyap Desai  * Return: Nothing.
4346fb9b0457SKashyap Desai  */
4347fb9b0457SKashyap Desai static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc,
4348fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr)
4349fb9b0457SKashyap Desai {
4350fb9b0457SKashyap Desai 	if (cmdptr->state & MPI3MR_CMD_PENDING) {
4351fb9b0457SKashyap Desai 		cmdptr->state |= MPI3MR_CMD_RESET;
4352fb9b0457SKashyap Desai 		cmdptr->state &= ~MPI3MR_CMD_PENDING;
4353fb9b0457SKashyap Desai 		if (cmdptr->is_waiting) {
4354fb9b0457SKashyap Desai 			complete(&cmdptr->done);
4355fb9b0457SKashyap Desai 			cmdptr->is_waiting = 0;
4356fb9b0457SKashyap Desai 		} else if (cmdptr->callback)
4357fb9b0457SKashyap Desai 			cmdptr->callback(mrioc, cmdptr);
4358fb9b0457SKashyap Desai 	}
4359fb9b0457SKashyap Desai }
4360fb9b0457SKashyap Desai 
4361fb9b0457SKashyap Desai /**
4362fb9b0457SKashyap Desai  * mpi3mr_flush_drv_cmds - Flush internaldriver commands
4363fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4364fb9b0457SKashyap Desai  *
4365fb9b0457SKashyap Desai  * Flush all internal driver commands post reset
4366fb9b0457SKashyap Desai  *
4367fb9b0457SKashyap Desai  * Return: Nothing.
4368fb9b0457SKashyap Desai  */
4369fb9b0457SKashyap Desai static void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc)
4370fb9b0457SKashyap Desai {
4371fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr;
4372fb9b0457SKashyap Desai 	u8 i;
4373fb9b0457SKashyap Desai 
4374fb9b0457SKashyap Desai 	cmdptr = &mrioc->init_cmds;
4375fb9b0457SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4376*32d457d5SSreekanth Reddy 
4377*32d457d5SSreekanth Reddy 	cmdptr = &mrioc->cfg_cmds;
4378*32d457d5SSreekanth Reddy 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4379*32d457d5SSreekanth Reddy 
4380f5e6d5a3SSumit Saxena 	cmdptr = &mrioc->bsg_cmds;
4381f5e6d5a3SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4382e844adb1SKashyap Desai 	cmdptr = &mrioc->host_tm_cmds;
4383e844adb1SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4384fb9b0457SKashyap Desai 
4385fb9b0457SKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
4386fb9b0457SKashyap Desai 		cmdptr = &mrioc->dev_rmhs_cmds[i];
4387fb9b0457SKashyap Desai 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4388fb9b0457SKashyap Desai 	}
4389c1af985dSSreekanth Reddy 
4390c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
4391c1af985dSSreekanth Reddy 		cmdptr = &mrioc->evtack_cmds[i];
4392c1af985dSSreekanth Reddy 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4393c1af985dSSreekanth Reddy 	}
439443ca1100SSumit Saxena 
439543ca1100SSumit Saxena 	cmdptr = &mrioc->pel_cmds;
439643ca1100SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
439743ca1100SSumit Saxena 
439843ca1100SSumit Saxena 	cmdptr = &mrioc->pel_abort_cmd;
439943ca1100SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
440043ca1100SSumit Saxena 
440143ca1100SSumit Saxena }
440243ca1100SSumit Saxena 
440343ca1100SSumit Saxena /**
440443ca1100SSumit Saxena  * mpi3mr_pel_wait_post - Issue PEL Wait
440543ca1100SSumit Saxena  * @mrioc: Adapter instance reference
440643ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
440743ca1100SSumit Saxena  *
440843ca1100SSumit Saxena  * Issue PEL Wait MPI request through admin queue and return.
440943ca1100SSumit Saxena  *
441043ca1100SSumit Saxena  * Return: Nothing.
441143ca1100SSumit Saxena  */
441243ca1100SSumit Saxena static void mpi3mr_pel_wait_post(struct mpi3mr_ioc *mrioc,
441343ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
441443ca1100SSumit Saxena {
441543ca1100SSumit Saxena 	struct mpi3_pel_req_action_wait pel_wait;
441643ca1100SSumit Saxena 
441743ca1100SSumit Saxena 	mrioc->pel_abort_requested = false;
441843ca1100SSumit Saxena 
441943ca1100SSumit Saxena 	memset(&pel_wait, 0, sizeof(pel_wait));
442043ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_PENDING;
442143ca1100SSumit Saxena 	drv_cmd->is_waiting = 0;
442243ca1100SSumit Saxena 	drv_cmd->callback = mpi3mr_pel_wait_complete;
442343ca1100SSumit Saxena 	drv_cmd->ioc_status = 0;
442443ca1100SSumit Saxena 	drv_cmd->ioc_loginfo = 0;
442543ca1100SSumit Saxena 	pel_wait.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
442643ca1100SSumit Saxena 	pel_wait.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
442743ca1100SSumit Saxena 	pel_wait.action = MPI3_PEL_ACTION_WAIT;
442843ca1100SSumit Saxena 	pel_wait.starting_sequence_number = cpu_to_le32(mrioc->pel_newest_seqnum);
442943ca1100SSumit Saxena 	pel_wait.locale = cpu_to_le16(mrioc->pel_locale);
443043ca1100SSumit Saxena 	pel_wait.class = cpu_to_le16(mrioc->pel_class);
443143ca1100SSumit Saxena 	pel_wait.wait_time = MPI3_PEL_WAITTIME_INFINITE_WAIT;
443243ca1100SSumit Saxena 	dprint_bsg_info(mrioc, "sending pel_wait seqnum(%d), class(%d), locale(0x%08x)\n",
443343ca1100SSumit Saxena 	    mrioc->pel_newest_seqnum, mrioc->pel_class, mrioc->pel_locale);
443443ca1100SSumit Saxena 
443543ca1100SSumit Saxena 	if (mpi3mr_admin_request_post(mrioc, &pel_wait, sizeof(pel_wait), 0)) {
443643ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
443743ca1100SSumit Saxena 			    "Issuing PELWait: Admin post failed\n");
443843ca1100SSumit Saxena 		drv_cmd->state = MPI3MR_CMD_NOTUSED;
443943ca1100SSumit Saxena 		drv_cmd->callback = NULL;
444043ca1100SSumit Saxena 		drv_cmd->retry_count = 0;
444143ca1100SSumit Saxena 		mrioc->pel_enabled = false;
444243ca1100SSumit Saxena 	}
444343ca1100SSumit Saxena }
444443ca1100SSumit Saxena 
444543ca1100SSumit Saxena /**
444643ca1100SSumit Saxena  * mpi3mr_pel_get_seqnum_post - Issue PEL Get Sequence number
444743ca1100SSumit Saxena  * @mrioc: Adapter instance reference
444843ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
444943ca1100SSumit Saxena  *
445043ca1100SSumit Saxena  * Issue PEL get sequence number MPI request through admin queue
445143ca1100SSumit Saxena  * and return.
445243ca1100SSumit Saxena  *
445343ca1100SSumit Saxena  * Return: 0 on success, non-zero on failure.
445443ca1100SSumit Saxena  */
445543ca1100SSumit Saxena int mpi3mr_pel_get_seqnum_post(struct mpi3mr_ioc *mrioc,
445643ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
445743ca1100SSumit Saxena {
445843ca1100SSumit Saxena 	struct mpi3_pel_req_action_get_sequence_numbers pel_getseq_req;
445943ca1100SSumit Saxena 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
446043ca1100SSumit Saxena 	int retval = 0;
446143ca1100SSumit Saxena 
446243ca1100SSumit Saxena 	memset(&pel_getseq_req, 0, sizeof(pel_getseq_req));
446343ca1100SSumit Saxena 	mrioc->pel_cmds.state = MPI3MR_CMD_PENDING;
446443ca1100SSumit Saxena 	mrioc->pel_cmds.is_waiting = 0;
446543ca1100SSumit Saxena 	mrioc->pel_cmds.ioc_status = 0;
446643ca1100SSumit Saxena 	mrioc->pel_cmds.ioc_loginfo = 0;
446743ca1100SSumit Saxena 	mrioc->pel_cmds.callback = mpi3mr_pel_get_seqnum_complete;
446843ca1100SSumit Saxena 	pel_getseq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
446943ca1100SSumit Saxena 	pel_getseq_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
447043ca1100SSumit Saxena 	pel_getseq_req.action = MPI3_PEL_ACTION_GET_SEQNUM;
447143ca1100SSumit Saxena 	mpi3mr_add_sg_single(&pel_getseq_req.sgl, sgl_flags,
447243ca1100SSumit Saxena 	    mrioc->pel_seqnum_sz, mrioc->pel_seqnum_dma);
447343ca1100SSumit Saxena 
447443ca1100SSumit Saxena 	retval = mpi3mr_admin_request_post(mrioc, &pel_getseq_req,
447543ca1100SSumit Saxena 			sizeof(pel_getseq_req), 0);
447643ca1100SSumit Saxena 	if (retval) {
447743ca1100SSumit Saxena 		if (drv_cmd) {
447843ca1100SSumit Saxena 			drv_cmd->state = MPI3MR_CMD_NOTUSED;
447943ca1100SSumit Saxena 			drv_cmd->callback = NULL;
448043ca1100SSumit Saxena 			drv_cmd->retry_count = 0;
448143ca1100SSumit Saxena 		}
448243ca1100SSumit Saxena 		mrioc->pel_enabled = false;
448343ca1100SSumit Saxena 	}
448443ca1100SSumit Saxena 
448543ca1100SSumit Saxena 	return retval;
448643ca1100SSumit Saxena }
448743ca1100SSumit Saxena 
448843ca1100SSumit Saxena /**
448943ca1100SSumit Saxena  * mpi3mr_pel_wait_complete - PELWait Completion callback
449043ca1100SSumit Saxena  * @mrioc: Adapter instance reference
449143ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
449243ca1100SSumit Saxena  *
449343ca1100SSumit Saxena  * This is a callback handler for the PELWait request and
449443ca1100SSumit Saxena  * firmware completes a PELWait request when it is aborted or a
449543ca1100SSumit Saxena  * new PEL entry is available. This sends AEN to the application
449643ca1100SSumit Saxena  * and if the PELwait completion is not due to PELAbort then
449743ca1100SSumit Saxena  * this will send a request for new PEL Sequence number
449843ca1100SSumit Saxena  *
449943ca1100SSumit Saxena  * Return: Nothing.
450043ca1100SSumit Saxena  */
450143ca1100SSumit Saxena static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc,
450243ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
450343ca1100SSumit Saxena {
450443ca1100SSumit Saxena 	struct mpi3_pel_reply *pel_reply = NULL;
450543ca1100SSumit Saxena 	u16 ioc_status, pe_log_status;
450643ca1100SSumit Saxena 	bool do_retry = false;
450743ca1100SSumit Saxena 
450843ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_RESET)
450943ca1100SSumit Saxena 		goto cleanup_drv_cmd;
451043ca1100SSumit Saxena 
451143ca1100SSumit Saxena 	ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
451243ca1100SSumit Saxena 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
451343ca1100SSumit Saxena 		ioc_err(mrioc, "%s: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
451443ca1100SSumit Saxena 			__func__, ioc_status, drv_cmd->ioc_loginfo);
451543ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
451643ca1100SSumit Saxena 		    "pel_wait: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
451743ca1100SSumit Saxena 		    ioc_status, drv_cmd->ioc_loginfo);
451843ca1100SSumit Saxena 		do_retry = true;
451943ca1100SSumit Saxena 	}
452043ca1100SSumit Saxena 
452143ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
452243ca1100SSumit Saxena 		pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
452343ca1100SSumit Saxena 
452443ca1100SSumit Saxena 	if (!pel_reply) {
452543ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
452643ca1100SSumit Saxena 		    "pel_wait: failed due to no reply\n");
452743ca1100SSumit Saxena 		goto out_failed;
452843ca1100SSumit Saxena 	}
452943ca1100SSumit Saxena 
453043ca1100SSumit Saxena 	pe_log_status = le16_to_cpu(pel_reply->pe_log_status);
453143ca1100SSumit Saxena 	if ((pe_log_status != MPI3_PEL_STATUS_SUCCESS) &&
453243ca1100SSumit Saxena 	    (pe_log_status != MPI3_PEL_STATUS_ABORTED)) {
453343ca1100SSumit Saxena 		ioc_err(mrioc, "%s: Failed pe_log_status(0x%04x)\n",
453443ca1100SSumit Saxena 			__func__, pe_log_status);
453543ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
453643ca1100SSumit Saxena 		    "pel_wait: failed due to pel_log_status(0x%04x)\n",
453743ca1100SSumit Saxena 		    pe_log_status);
453843ca1100SSumit Saxena 		do_retry = true;
453943ca1100SSumit Saxena 	}
454043ca1100SSumit Saxena 
454143ca1100SSumit Saxena 	if (do_retry) {
454243ca1100SSumit Saxena 		if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
454343ca1100SSumit Saxena 			drv_cmd->retry_count++;
454443ca1100SSumit Saxena 			dprint_bsg_err(mrioc, "pel_wait: retrying(%d)\n",
454543ca1100SSumit Saxena 			    drv_cmd->retry_count);
454643ca1100SSumit Saxena 			mpi3mr_pel_wait_post(mrioc, drv_cmd);
454743ca1100SSumit Saxena 			return;
454843ca1100SSumit Saxena 		}
454943ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
455043ca1100SSumit Saxena 		    "pel_wait: failed after all retries(%d)\n",
455143ca1100SSumit Saxena 		    drv_cmd->retry_count);
455243ca1100SSumit Saxena 		goto out_failed;
455343ca1100SSumit Saxena 	}
455443ca1100SSumit Saxena 	atomic64_inc(&event_counter);
455543ca1100SSumit Saxena 	if (!mrioc->pel_abort_requested) {
455643ca1100SSumit Saxena 		mrioc->pel_cmds.retry_count = 0;
455743ca1100SSumit Saxena 		mpi3mr_pel_get_seqnum_post(mrioc, &mrioc->pel_cmds);
455843ca1100SSumit Saxena 	}
455943ca1100SSumit Saxena 
456043ca1100SSumit Saxena 	return;
456143ca1100SSumit Saxena out_failed:
456243ca1100SSumit Saxena 	mrioc->pel_enabled = false;
456343ca1100SSumit Saxena cleanup_drv_cmd:
456443ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
456543ca1100SSumit Saxena 	drv_cmd->callback = NULL;
456643ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
456743ca1100SSumit Saxena }
456843ca1100SSumit Saxena 
456943ca1100SSumit Saxena /**
457043ca1100SSumit Saxena  * mpi3mr_pel_get_seqnum_complete - PELGetSeqNum Completion callback
457143ca1100SSumit Saxena  * @mrioc: Adapter instance reference
457243ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
457343ca1100SSumit Saxena  *
457443ca1100SSumit Saxena  * This is a callback handler for the PEL get sequence number
457543ca1100SSumit Saxena  * request and a new PEL wait request will be issued to the
457643ca1100SSumit Saxena  * firmware from this
457743ca1100SSumit Saxena  *
457843ca1100SSumit Saxena  * Return: Nothing.
457943ca1100SSumit Saxena  */
458043ca1100SSumit Saxena void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc,
458143ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
458243ca1100SSumit Saxena {
458343ca1100SSumit Saxena 	struct mpi3_pel_reply *pel_reply = NULL;
458443ca1100SSumit Saxena 	struct mpi3_pel_seq *pel_seqnum_virt;
458543ca1100SSumit Saxena 	u16 ioc_status;
458643ca1100SSumit Saxena 	bool do_retry = false;
458743ca1100SSumit Saxena 
458843ca1100SSumit Saxena 	pel_seqnum_virt = (struct mpi3_pel_seq *)mrioc->pel_seqnum_virt;
458943ca1100SSumit Saxena 
459043ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_RESET)
459143ca1100SSumit Saxena 		goto cleanup_drv_cmd;
459243ca1100SSumit Saxena 
459343ca1100SSumit Saxena 	ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
459443ca1100SSumit Saxena 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
459543ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
459643ca1100SSumit Saxena 		    "pel_get_seqnum: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
459743ca1100SSumit Saxena 		    ioc_status, drv_cmd->ioc_loginfo);
459843ca1100SSumit Saxena 		do_retry = true;
459943ca1100SSumit Saxena 	}
460043ca1100SSumit Saxena 
460143ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
460243ca1100SSumit Saxena 		pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
460343ca1100SSumit Saxena 	if (!pel_reply) {
460443ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
460543ca1100SSumit Saxena 		    "pel_get_seqnum: failed due to no reply\n");
460643ca1100SSumit Saxena 		goto out_failed;
460743ca1100SSumit Saxena 	}
460843ca1100SSumit Saxena 
460943ca1100SSumit Saxena 	if (le16_to_cpu(pel_reply->pe_log_status) != MPI3_PEL_STATUS_SUCCESS) {
461043ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
461143ca1100SSumit Saxena 		    "pel_get_seqnum: failed due to pel_log_status(0x%04x)\n",
461243ca1100SSumit Saxena 		    le16_to_cpu(pel_reply->pe_log_status));
461343ca1100SSumit Saxena 		do_retry = true;
461443ca1100SSumit Saxena 	}
461543ca1100SSumit Saxena 
461643ca1100SSumit Saxena 	if (do_retry) {
461743ca1100SSumit Saxena 		if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
461843ca1100SSumit Saxena 			drv_cmd->retry_count++;
461943ca1100SSumit Saxena 			dprint_bsg_err(mrioc,
462043ca1100SSumit Saxena 			    "pel_get_seqnum: retrying(%d)\n",
462143ca1100SSumit Saxena 			    drv_cmd->retry_count);
462243ca1100SSumit Saxena 			mpi3mr_pel_get_seqnum_post(mrioc, drv_cmd);
462343ca1100SSumit Saxena 			return;
462443ca1100SSumit Saxena 		}
462543ca1100SSumit Saxena 
462643ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
462743ca1100SSumit Saxena 		    "pel_get_seqnum: failed after all retries(%d)\n",
462843ca1100SSumit Saxena 		    drv_cmd->retry_count);
462943ca1100SSumit Saxena 		goto out_failed;
463043ca1100SSumit Saxena 	}
463143ca1100SSumit Saxena 	mrioc->pel_newest_seqnum = le32_to_cpu(pel_seqnum_virt->newest) + 1;
463243ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
463343ca1100SSumit Saxena 	mpi3mr_pel_wait_post(mrioc, drv_cmd);
463443ca1100SSumit Saxena 
463543ca1100SSumit Saxena 	return;
463643ca1100SSumit Saxena out_failed:
463743ca1100SSumit Saxena 	mrioc->pel_enabled = false;
463843ca1100SSumit Saxena cleanup_drv_cmd:
463943ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
464043ca1100SSumit Saxena 	drv_cmd->callback = NULL;
464143ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
4642fb9b0457SKashyap Desai }
4643fb9b0457SKashyap Desai 
4644fb9b0457SKashyap Desai /**
4645824a1566SKashyap Desai  * mpi3mr_soft_reset_handler - Reset the controller
4646824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4647824a1566SKashyap Desai  * @reset_reason: Reset reason code
4648824a1566SKashyap Desai  * @snapdump: Flag to generate snapdump in firmware or not
4649824a1566SKashyap Desai  *
4650fb9b0457SKashyap Desai  * This is an handler for recovering controller by issuing soft
4651fb9b0457SKashyap Desai  * reset are diag fault reset.  This is a blocking function and
4652fb9b0457SKashyap Desai  * when one reset is executed if any other resets they will be
4653f5e6d5a3SSumit Saxena  * blocked. All BSG requests will be blocked during the reset. If
4654fb9b0457SKashyap Desai  * controller reset is successful then the controller will be
4655fb9b0457SKashyap Desai  * reinitalized, otherwise the controller will be marked as not
4656fb9b0457SKashyap Desai  * recoverable
4657fb9b0457SKashyap Desai  *
4658fb9b0457SKashyap Desai  * In snapdump bit is set, the controller is issued with diag
4659fb9b0457SKashyap Desai  * fault reset so that the firmware can create a snap dump and
4660fb9b0457SKashyap Desai  * post that the firmware will result in F000 fault and the
4661fb9b0457SKashyap Desai  * driver will issue soft reset to recover from that.
4662824a1566SKashyap Desai  *
4663824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
4664824a1566SKashyap Desai  */
4665824a1566SKashyap Desai int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
4666824a1566SKashyap Desai 	u32 reset_reason, u8 snapdump)
4667824a1566SKashyap Desai {
4668fb9b0457SKashyap Desai 	int retval = 0, i;
4669fb9b0457SKashyap Desai 	unsigned long flags;
4670fb9b0457SKashyap Desai 	u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
4671fb9b0457SKashyap Desai 
4672b64845a7SSreekanth Reddy 	/* Block the reset handler until diag save in progress*/
4673b64845a7SSreekanth Reddy 	dprint_reset(mrioc,
4674b64845a7SSreekanth Reddy 	    "soft_reset_handler: check and block on diagsave_timeout(%d)\n",
4675b64845a7SSreekanth Reddy 	    mrioc->diagsave_timeout);
4676b64845a7SSreekanth Reddy 	while (mrioc->diagsave_timeout)
4677b64845a7SSreekanth Reddy 		ssleep(1);
4678fb9b0457SKashyap Desai 	/*
4679fb9b0457SKashyap Desai 	 * Block new resets until the currently executing one is finished and
4680fb9b0457SKashyap Desai 	 * return the status of the existing reset for all blocked resets
4681fb9b0457SKashyap Desai 	 */
4682b64845a7SSreekanth Reddy 	dprint_reset(mrioc, "soft_reset_handler: acquiring reset_mutex\n");
4683fb9b0457SKashyap Desai 	if (!mutex_trylock(&mrioc->reset_mutex)) {
4684b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4685b64845a7SSreekanth Reddy 		    "controller reset triggered by %s is blocked due to another reset in progress\n",
4686b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4687b64845a7SSreekanth Reddy 		do {
4688b64845a7SSreekanth Reddy 			ssleep(1);
4689b64845a7SSreekanth Reddy 		} while (mrioc->reset_in_progress == 1);
4690b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4691b64845a7SSreekanth Reddy 		    "returning previous reset result(%d) for the reset triggered by %s\n",
4692b64845a7SSreekanth Reddy 		    mrioc->prev_reset_result,
4693b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4694b64845a7SSreekanth Reddy 		return mrioc->prev_reset_result;
4695fb9b0457SKashyap Desai 	}
4696b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is triggered by %s\n",
4697b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason));
4698b64845a7SSreekanth Reddy 
4699fb9b0457SKashyap Desai 	mrioc->reset_in_progress = 1;
4700f5e6d5a3SSumit Saxena 	mrioc->stop_bsgs = 1;
4701b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = -1;
4702fb9b0457SKashyap Desai 
4703fb9b0457SKashyap Desai 	if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
4704b64845a7SSreekanth Reddy 	    (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) &&
4705fb9b0457SKashyap Desai 	    (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
4706fb9b0457SKashyap Desai 		for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4707fb9b0457SKashyap Desai 			mrioc->event_masks[i] = -1;
4708fb9b0457SKashyap Desai 
4709b64845a7SSreekanth Reddy 		dprint_reset(mrioc, "soft_reset_handler: masking events\n");
4710b64845a7SSreekanth Reddy 		mpi3mr_issue_event_notification(mrioc);
4711fb9b0457SKashyap Desai 	}
4712fb9b0457SKashyap Desai 
471344dc724fSKashyap Desai 	mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT);
471444dc724fSKashyap Desai 
4715fb9b0457SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4716fb9b0457SKashyap Desai 
4717fb9b0457SKashyap Desai 	if (snapdump) {
4718fb9b0457SKashyap Desai 		mpi3mr_set_diagsave(mrioc);
4719fb9b0457SKashyap Desai 		retval = mpi3mr_issue_reset(mrioc,
4720fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4721fb9b0457SKashyap Desai 		if (!retval) {
4722fb9b0457SKashyap Desai 			do {
4723fb9b0457SKashyap Desai 				host_diagnostic =
4724fb9b0457SKashyap Desai 				    readl(&mrioc->sysif_regs->host_diagnostic);
4725fb9b0457SKashyap Desai 				if (!(host_diagnostic &
4726fb9b0457SKashyap Desai 				    MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
4727fb9b0457SKashyap Desai 					break;
4728fb9b0457SKashyap Desai 				msleep(100);
4729fb9b0457SKashyap Desai 			} while (--timeout);
4730fb9b0457SKashyap Desai 		}
4731fb9b0457SKashyap Desai 	}
4732fb9b0457SKashyap Desai 
4733fb9b0457SKashyap Desai 	retval = mpi3mr_issue_reset(mrioc,
4734fb9b0457SKashyap Desai 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
4735fb9b0457SKashyap Desai 	if (retval) {
4736fb9b0457SKashyap Desai 		ioc_err(mrioc, "Failed to issue soft reset to the ioc\n");
4737fb9b0457SKashyap Desai 		goto out;
4738fb9b0457SKashyap Desai 	}
4739f10af057SSreekanth Reddy 	if (mrioc->num_io_throttle_group !=
4740f10af057SSreekanth Reddy 	    mrioc->facts.max_io_throttle_group) {
4741f10af057SSreekanth Reddy 		ioc_err(mrioc,
4742f10af057SSreekanth Reddy 		    "max io throttle group doesn't match old(%d), new(%d)\n",
4743f10af057SSreekanth Reddy 		    mrioc->num_io_throttle_group,
4744f10af057SSreekanth Reddy 		    mrioc->facts.max_io_throttle_group);
47452a8a0147SDan Carpenter 		retval = -EPERM;
47462a8a0147SDan Carpenter 		goto out;
4747f10af057SSreekanth Reddy 	}
4748fb9b0457SKashyap Desai 
4749c1af985dSSreekanth Reddy 	mpi3mr_flush_delayed_cmd_lists(mrioc);
4750fb9b0457SKashyap Desai 	mpi3mr_flush_drv_cmds(mrioc);
4751fb9b0457SKashyap Desai 	memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
4752fb9b0457SKashyap Desai 	memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
4753c1af985dSSreekanth Reddy 	memset(mrioc->evtack_cmds_bitmap, 0, mrioc->evtack_cmds_bitmap_sz);
4754fb9b0457SKashyap Desai 	mpi3mr_flush_host_io(mrioc);
4755580e6742SSreekanth Reddy 	mpi3mr_cleanup_fwevt_list(mrioc);
4756fb9b0457SKashyap Desai 	mpi3mr_invalidate_devhandles(mrioc);
475778b76a07SSreekanth Reddy 	if (mrioc->prepare_for_reset) {
475878b76a07SSreekanth Reddy 		mrioc->prepare_for_reset = 0;
475978b76a07SSreekanth Reddy 		mrioc->prepare_for_reset_timeout_counter = 0;
476078b76a07SSreekanth Reddy 	}
4761fb9b0457SKashyap Desai 	mpi3mr_memset_buffers(mrioc);
4762fe6db615SSreekanth Reddy 	retval = mpi3mr_reinit_ioc(mrioc, 0);
4763fb9b0457SKashyap Desai 	if (retval) {
4764fb9b0457SKashyap Desai 		pr_err(IOCNAME "reinit after soft reset failed: reason %d\n",
4765fb9b0457SKashyap Desai 		    mrioc->name, reset_reason);
4766fb9b0457SKashyap Desai 		goto out;
4767fb9b0457SKashyap Desai 	}
4768fb9b0457SKashyap Desai 	ssleep(10);
4769fb9b0457SKashyap Desai 
4770fb9b0457SKashyap Desai out:
4771fb9b0457SKashyap Desai 	if (!retval) {
4772b64845a7SSreekanth Reddy 		mrioc->diagsave_timeout = 0;
4773fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
477443ca1100SSumit Saxena 		mrioc->pel_abort_requested = 0;
477543ca1100SSumit Saxena 		if (mrioc->pel_enabled) {
477643ca1100SSumit Saxena 			mrioc->pel_cmds.retry_count = 0;
477743ca1100SSumit Saxena 			mpi3mr_pel_wait_post(mrioc, &mrioc->pel_cmds);
477843ca1100SSumit Saxena 		}
477943ca1100SSumit Saxena 
4780fb9b0457SKashyap Desai 		mpi3mr_rfresh_tgtdevs(mrioc);
478154dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
4782fb9b0457SKashyap Desai 		spin_lock_irqsave(&mrioc->watchdog_lock, flags);
4783fb9b0457SKashyap Desai 		if (mrioc->watchdog_work_q)
4784fb9b0457SKashyap Desai 			queue_delayed_work(mrioc->watchdog_work_q,
4785fb9b0457SKashyap Desai 			    &mrioc->watchdog_work,
4786fb9b0457SKashyap Desai 			    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
4787fb9b0457SKashyap Desai 		spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
4788f5e6d5a3SSumit Saxena 		mrioc->stop_bsgs = 0;
478943ca1100SSumit Saxena 		if (mrioc->pel_enabled)
479043ca1100SSumit Saxena 			atomic64_inc(&event_counter);
4791fb9b0457SKashyap Desai 	} else {
4792fb9b0457SKashyap Desai 		mpi3mr_issue_reset(mrioc,
4793fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4794fb9b0457SKashyap Desai 		mrioc->unrecoverable = 1;
4795fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
4796fb9b0457SKashyap Desai 		retval = -1;
4797fb9b0457SKashyap Desai 	}
4798b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = retval;
4799fb9b0457SKashyap Desai 	mutex_unlock(&mrioc->reset_mutex);
4800b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is %s\n",
4801b64845a7SSreekanth Reddy 	    ((retval == 0) ? "successful" : "failed"));
4802fb9b0457SKashyap Desai 	return retval;
4803824a1566SKashyap Desai }
4804*32d457d5SSreekanth Reddy 
4805*32d457d5SSreekanth Reddy 
4806*32d457d5SSreekanth Reddy /**
4807*32d457d5SSreekanth Reddy  * mpi3mr_free_config_dma_memory - free memory for config page
4808*32d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
4809*32d457d5SSreekanth Reddy  * @mem_desc: memory descriptor structure
4810*32d457d5SSreekanth Reddy  *
4811*32d457d5SSreekanth Reddy  * Check whether the size of the buffer specified by the memory
4812*32d457d5SSreekanth Reddy  * descriptor is greater than the default page size if so then
4813*32d457d5SSreekanth Reddy  * free the memory pointed by the descriptor.
4814*32d457d5SSreekanth Reddy  *
4815*32d457d5SSreekanth Reddy  * Return: Nothing.
4816*32d457d5SSreekanth Reddy  */
4817*32d457d5SSreekanth Reddy static void mpi3mr_free_config_dma_memory(struct mpi3mr_ioc *mrioc,
4818*32d457d5SSreekanth Reddy 	struct dma_memory_desc *mem_desc)
4819*32d457d5SSreekanth Reddy {
4820*32d457d5SSreekanth Reddy 	if ((mem_desc->size > mrioc->cfg_page_sz) && mem_desc->addr) {
4821*32d457d5SSreekanth Reddy 		dma_free_coherent(&mrioc->pdev->dev, mem_desc->size,
4822*32d457d5SSreekanth Reddy 		    mem_desc->addr, mem_desc->dma_addr);
4823*32d457d5SSreekanth Reddy 		mem_desc->addr = NULL;
4824*32d457d5SSreekanth Reddy 	}
4825*32d457d5SSreekanth Reddy }
4826*32d457d5SSreekanth Reddy 
4827*32d457d5SSreekanth Reddy /**
4828*32d457d5SSreekanth Reddy  * mpi3mr_alloc_config_dma_memory - Alloc memory for config page
4829*32d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
4830*32d457d5SSreekanth Reddy  * @mem_desc: Memory descriptor to hold dma memory info
4831*32d457d5SSreekanth Reddy  *
4832*32d457d5SSreekanth Reddy  * This function allocates new dmaable memory or provides the
4833*32d457d5SSreekanth Reddy  * default config page dmaable memory based on the memory size
4834*32d457d5SSreekanth Reddy  * described by the descriptor.
4835*32d457d5SSreekanth Reddy  *
4836*32d457d5SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
4837*32d457d5SSreekanth Reddy  */
4838*32d457d5SSreekanth Reddy static int mpi3mr_alloc_config_dma_memory(struct mpi3mr_ioc *mrioc,
4839*32d457d5SSreekanth Reddy 	struct dma_memory_desc *mem_desc)
4840*32d457d5SSreekanth Reddy {
4841*32d457d5SSreekanth Reddy 	if (mem_desc->size > mrioc->cfg_page_sz) {
4842*32d457d5SSreekanth Reddy 		mem_desc->addr = dma_alloc_coherent(&mrioc->pdev->dev,
4843*32d457d5SSreekanth Reddy 		    mem_desc->size, &mem_desc->dma_addr, GFP_KERNEL);
4844*32d457d5SSreekanth Reddy 		if (!mem_desc->addr)
4845*32d457d5SSreekanth Reddy 			return -ENOMEM;
4846*32d457d5SSreekanth Reddy 	} else {
4847*32d457d5SSreekanth Reddy 		mem_desc->addr = mrioc->cfg_page;
4848*32d457d5SSreekanth Reddy 		mem_desc->dma_addr = mrioc->cfg_page_dma;
4849*32d457d5SSreekanth Reddy 		memset(mem_desc->addr, 0, mrioc->cfg_page_sz);
4850*32d457d5SSreekanth Reddy 	}
4851*32d457d5SSreekanth Reddy 	return 0;
4852*32d457d5SSreekanth Reddy }
4853*32d457d5SSreekanth Reddy 
4854*32d457d5SSreekanth Reddy /**
4855*32d457d5SSreekanth Reddy  * mpi3mr_post_cfg_req - Issue config requests and wait
4856*32d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
4857*32d457d5SSreekanth Reddy  * @cfg_req: Configuration request
4858*32d457d5SSreekanth Reddy  * @timeout: Timeout in seconds
4859*32d457d5SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
4860*32d457d5SSreekanth Reddy  *
4861*32d457d5SSreekanth Reddy  * A generic function for posting MPI3 configuration request to
4862*32d457d5SSreekanth Reddy  * the firmware. This blocks for the completion of request for
4863*32d457d5SSreekanth Reddy  * timeout seconds and if the request times out this function
4864*32d457d5SSreekanth Reddy  * faults the controller with proper reason code.
4865*32d457d5SSreekanth Reddy  *
4866*32d457d5SSreekanth Reddy  * On successful completion of the request this function returns
4867*32d457d5SSreekanth Reddy  * appropriate ioc status from the firmware back to the caller.
4868*32d457d5SSreekanth Reddy  *
4869*32d457d5SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
4870*32d457d5SSreekanth Reddy  */
4871*32d457d5SSreekanth Reddy static int mpi3mr_post_cfg_req(struct mpi3mr_ioc *mrioc,
4872*32d457d5SSreekanth Reddy 	struct mpi3_config_request *cfg_req, int timeout, u16 *ioc_status)
4873*32d457d5SSreekanth Reddy {
4874*32d457d5SSreekanth Reddy 	int retval = 0;
4875*32d457d5SSreekanth Reddy 
4876*32d457d5SSreekanth Reddy 	mutex_lock(&mrioc->cfg_cmds.mutex);
4877*32d457d5SSreekanth Reddy 	if (mrioc->cfg_cmds.state & MPI3MR_CMD_PENDING) {
4878*32d457d5SSreekanth Reddy 		retval = -1;
4879*32d457d5SSreekanth Reddy 		ioc_err(mrioc, "sending config request failed due to command in use\n");
4880*32d457d5SSreekanth Reddy 		mutex_unlock(&mrioc->cfg_cmds.mutex);
4881*32d457d5SSreekanth Reddy 		goto out;
4882*32d457d5SSreekanth Reddy 	}
4883*32d457d5SSreekanth Reddy 	mrioc->cfg_cmds.state = MPI3MR_CMD_PENDING;
4884*32d457d5SSreekanth Reddy 	mrioc->cfg_cmds.is_waiting = 1;
4885*32d457d5SSreekanth Reddy 	mrioc->cfg_cmds.callback = NULL;
4886*32d457d5SSreekanth Reddy 	mrioc->cfg_cmds.ioc_status = 0;
4887*32d457d5SSreekanth Reddy 	mrioc->cfg_cmds.ioc_loginfo = 0;
4888*32d457d5SSreekanth Reddy 
4889*32d457d5SSreekanth Reddy 	cfg_req->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_CFG_CMDS);
4890*32d457d5SSreekanth Reddy 	cfg_req->function = MPI3_FUNCTION_CONFIG;
4891*32d457d5SSreekanth Reddy 
4892*32d457d5SSreekanth Reddy 	init_completion(&mrioc->cfg_cmds.done);
4893*32d457d5SSreekanth Reddy 	dprint_cfg_info(mrioc, "posting config request\n");
4894*32d457d5SSreekanth Reddy 	if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
4895*32d457d5SSreekanth Reddy 		dprint_dump(cfg_req, sizeof(struct mpi3_config_request),
4896*32d457d5SSreekanth Reddy 		    "mpi3_cfg_req");
4897*32d457d5SSreekanth Reddy 	retval = mpi3mr_admin_request_post(mrioc, cfg_req, sizeof(*cfg_req), 1);
4898*32d457d5SSreekanth Reddy 	if (retval) {
4899*32d457d5SSreekanth Reddy 		ioc_err(mrioc, "posting config request failed\n");
4900*32d457d5SSreekanth Reddy 		goto out_unlock;
4901*32d457d5SSreekanth Reddy 	}
4902*32d457d5SSreekanth Reddy 	wait_for_completion_timeout(&mrioc->cfg_cmds.done, (timeout * HZ));
4903*32d457d5SSreekanth Reddy 	if (!(mrioc->cfg_cmds.state & MPI3MR_CMD_COMPLETE)) {
4904*32d457d5SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
4905*32d457d5SSreekanth Reddy 		    MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT);
4906*32d457d5SSreekanth Reddy 		ioc_err(mrioc, "config request timed out\n");
4907*32d457d5SSreekanth Reddy 		retval = -1;
4908*32d457d5SSreekanth Reddy 		goto out_unlock;
4909*32d457d5SSreekanth Reddy 	}
4910*32d457d5SSreekanth Reddy 	*ioc_status = mrioc->cfg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
4911*32d457d5SSreekanth Reddy 	if ((*ioc_status) != MPI3_IOCSTATUS_SUCCESS)
4912*32d457d5SSreekanth Reddy 		dprint_cfg_err(mrioc,
4913*32d457d5SSreekanth Reddy 		    "cfg_page request returned with ioc_status(0x%04x), log_info(0x%08x)\n",
4914*32d457d5SSreekanth Reddy 		    *ioc_status, mrioc->cfg_cmds.ioc_loginfo);
4915*32d457d5SSreekanth Reddy 
4916*32d457d5SSreekanth Reddy out_unlock:
4917*32d457d5SSreekanth Reddy 	mrioc->cfg_cmds.state = MPI3MR_CMD_NOTUSED;
4918*32d457d5SSreekanth Reddy 	mutex_unlock(&mrioc->cfg_cmds.mutex);
4919*32d457d5SSreekanth Reddy 
4920*32d457d5SSreekanth Reddy out:
4921*32d457d5SSreekanth Reddy 	return retval;
4922*32d457d5SSreekanth Reddy }
4923*32d457d5SSreekanth Reddy 
4924*32d457d5SSreekanth Reddy /**
4925*32d457d5SSreekanth Reddy  * mpi3mr_process_cfg_req - config page request processor
4926*32d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
4927*32d457d5SSreekanth Reddy  * @cfg_req: Configuration request
4928*32d457d5SSreekanth Reddy  * @cfg_hdr: Configuration page header
4929*32d457d5SSreekanth Reddy  * @timeout: Timeout in seconds
4930*32d457d5SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
4931*32d457d5SSreekanth Reddy  * @cfg_buf: Memory pointer to copy config page or header
4932*32d457d5SSreekanth Reddy  * @cfg_buf_sz: Size of the memory to get config page or header
4933*32d457d5SSreekanth Reddy  *
4934*32d457d5SSreekanth Reddy  * This is handler for config page read, write and config page
4935*32d457d5SSreekanth Reddy  * header read operations.
4936*32d457d5SSreekanth Reddy  *
4937*32d457d5SSreekanth Reddy  * This function expects the cfg_req to be populated with page
4938*32d457d5SSreekanth Reddy  * type, page number, action for the header read and with page
4939*32d457d5SSreekanth Reddy  * address for all other operations.
4940*32d457d5SSreekanth Reddy  *
4941*32d457d5SSreekanth Reddy  * The cfg_hdr can be passed as null for reading required header
4942*32d457d5SSreekanth Reddy  * details for read/write pages the cfg_hdr should point valid
4943*32d457d5SSreekanth Reddy  * configuration page header.
4944*32d457d5SSreekanth Reddy  *
4945*32d457d5SSreekanth Reddy  * This allocates dmaable memory based on the size of the config
4946*32d457d5SSreekanth Reddy  * buffer and set the SGE of the cfg_req.
4947*32d457d5SSreekanth Reddy  *
4948*32d457d5SSreekanth Reddy  * For write actions, the config page data has to be passed in
4949*32d457d5SSreekanth Reddy  * the cfg_buf and size of the data has to be mentioned in the
4950*32d457d5SSreekanth Reddy  * cfg_buf_sz.
4951*32d457d5SSreekanth Reddy  *
4952*32d457d5SSreekanth Reddy  * For read/header actions, on successful completion of the
4953*32d457d5SSreekanth Reddy  * request with successful ioc_status the data will be copied
4954*32d457d5SSreekanth Reddy  * into the cfg_buf limited to a minimum of actual page size and
4955*32d457d5SSreekanth Reddy  * cfg_buf_sz
4956*32d457d5SSreekanth Reddy  *
4957*32d457d5SSreekanth Reddy  *
4958*32d457d5SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
4959*32d457d5SSreekanth Reddy  */
4960*32d457d5SSreekanth Reddy static int mpi3mr_process_cfg_req(struct mpi3mr_ioc *mrioc,
4961*32d457d5SSreekanth Reddy 	struct mpi3_config_request *cfg_req,
4962*32d457d5SSreekanth Reddy 	struct mpi3_config_page_header *cfg_hdr, int timeout, u16 *ioc_status,
4963*32d457d5SSreekanth Reddy 	void *cfg_buf, u32 cfg_buf_sz)
4964*32d457d5SSreekanth Reddy {
4965*32d457d5SSreekanth Reddy 	struct dma_memory_desc mem_desc;
4966*32d457d5SSreekanth Reddy 	int retval = -1;
4967*32d457d5SSreekanth Reddy 	u8 invalid_action = 0;
4968*32d457d5SSreekanth Reddy 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
4969*32d457d5SSreekanth Reddy 
4970*32d457d5SSreekanth Reddy 	memset(&mem_desc, 0, sizeof(struct dma_memory_desc));
4971*32d457d5SSreekanth Reddy 
4972*32d457d5SSreekanth Reddy 	if (cfg_req->action == MPI3_CONFIG_ACTION_PAGE_HEADER)
4973*32d457d5SSreekanth Reddy 		mem_desc.size = sizeof(struct mpi3_config_page_header);
4974*32d457d5SSreekanth Reddy 	else {
4975*32d457d5SSreekanth Reddy 		if (!cfg_hdr) {
4976*32d457d5SSreekanth Reddy 			ioc_err(mrioc, "null config header passed for config action(%d), page_type(0x%02x), page_num(%d)\n",
4977*32d457d5SSreekanth Reddy 			    cfg_req->action, cfg_req->page_type,
4978*32d457d5SSreekanth Reddy 			    cfg_req->page_number);
4979*32d457d5SSreekanth Reddy 			goto out;
4980*32d457d5SSreekanth Reddy 		}
4981*32d457d5SSreekanth Reddy 		switch (cfg_hdr->page_attribute & MPI3_CONFIG_PAGEATTR_MASK) {
4982*32d457d5SSreekanth Reddy 		case MPI3_CONFIG_PAGEATTR_READ_ONLY:
4983*32d457d5SSreekanth Reddy 			if (cfg_req->action
4984*32d457d5SSreekanth Reddy 			    != MPI3_CONFIG_ACTION_READ_CURRENT)
4985*32d457d5SSreekanth Reddy 				invalid_action = 1;
4986*32d457d5SSreekanth Reddy 			break;
4987*32d457d5SSreekanth Reddy 		case MPI3_CONFIG_PAGEATTR_CHANGEABLE:
4988*32d457d5SSreekanth Reddy 			if ((cfg_req->action ==
4989*32d457d5SSreekanth Reddy 			     MPI3_CONFIG_ACTION_READ_PERSISTENT) ||
4990*32d457d5SSreekanth Reddy 			    (cfg_req->action ==
4991*32d457d5SSreekanth Reddy 			     MPI3_CONFIG_ACTION_WRITE_PERSISTENT))
4992*32d457d5SSreekanth Reddy 				invalid_action = 1;
4993*32d457d5SSreekanth Reddy 			break;
4994*32d457d5SSreekanth Reddy 		case MPI3_CONFIG_PAGEATTR_PERSISTENT:
4995*32d457d5SSreekanth Reddy 		default:
4996*32d457d5SSreekanth Reddy 			break;
4997*32d457d5SSreekanth Reddy 		}
4998*32d457d5SSreekanth Reddy 		if (invalid_action) {
4999*32d457d5SSreekanth Reddy 			ioc_err(mrioc,
5000*32d457d5SSreekanth Reddy 			    "config action(%d) is not allowed for page_type(0x%02x), page_num(%d) with page_attribute(0x%02x)\n",
5001*32d457d5SSreekanth Reddy 			    cfg_req->action, cfg_req->page_type,
5002*32d457d5SSreekanth Reddy 			    cfg_req->page_number, cfg_hdr->page_attribute);
5003*32d457d5SSreekanth Reddy 			goto out;
5004*32d457d5SSreekanth Reddy 		}
5005*32d457d5SSreekanth Reddy 		mem_desc.size = le16_to_cpu(cfg_hdr->page_length) * 4;
5006*32d457d5SSreekanth Reddy 		cfg_req->page_length = cfg_hdr->page_length;
5007*32d457d5SSreekanth Reddy 		cfg_req->page_version = cfg_hdr->page_version;
5008*32d457d5SSreekanth Reddy 	}
5009*32d457d5SSreekanth Reddy 	if (mpi3mr_alloc_config_dma_memory(mrioc, &mem_desc))
5010*32d457d5SSreekanth Reddy 		goto out;
5011*32d457d5SSreekanth Reddy 
5012*32d457d5SSreekanth Reddy 	mpi3mr_add_sg_single(&cfg_req->sgl, sgl_flags, mem_desc.size,
5013*32d457d5SSreekanth Reddy 	    mem_desc.dma_addr);
5014*32d457d5SSreekanth Reddy 
5015*32d457d5SSreekanth Reddy 	if ((cfg_req->action == MPI3_CONFIG_ACTION_WRITE_PERSISTENT) ||
5016*32d457d5SSreekanth Reddy 	    (cfg_req->action == MPI3_CONFIG_ACTION_WRITE_CURRENT)) {
5017*32d457d5SSreekanth Reddy 		memcpy(mem_desc.addr, cfg_buf, min_t(u16, mem_desc.size,
5018*32d457d5SSreekanth Reddy 		    cfg_buf_sz));
5019*32d457d5SSreekanth Reddy 		dprint_cfg_info(mrioc, "config buffer to be written\n");
5020*32d457d5SSreekanth Reddy 		if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
5021*32d457d5SSreekanth Reddy 			dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf");
5022*32d457d5SSreekanth Reddy 	}
5023*32d457d5SSreekanth Reddy 
5024*32d457d5SSreekanth Reddy 	if (mpi3mr_post_cfg_req(mrioc, cfg_req, timeout, ioc_status))
5025*32d457d5SSreekanth Reddy 		goto out;
5026*32d457d5SSreekanth Reddy 
5027*32d457d5SSreekanth Reddy 	retval = 0;
5028*32d457d5SSreekanth Reddy 	if ((*ioc_status == MPI3_IOCSTATUS_SUCCESS) &&
5029*32d457d5SSreekanth Reddy 	    (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_PERSISTENT) &&
5030*32d457d5SSreekanth Reddy 	    (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_CURRENT)) {
5031*32d457d5SSreekanth Reddy 		memcpy(cfg_buf, mem_desc.addr, min_t(u16, mem_desc.size,
5032*32d457d5SSreekanth Reddy 		    cfg_buf_sz));
5033*32d457d5SSreekanth Reddy 		dprint_cfg_info(mrioc, "config buffer read\n");
5034*32d457d5SSreekanth Reddy 		if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
5035*32d457d5SSreekanth Reddy 			dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf");
5036*32d457d5SSreekanth Reddy 	}
5037*32d457d5SSreekanth Reddy 
5038*32d457d5SSreekanth Reddy out:
5039*32d457d5SSreekanth Reddy 	mpi3mr_free_config_dma_memory(mrioc, &mem_desc);
5040*32d457d5SSreekanth Reddy 	return retval;
5041*32d457d5SSreekanth Reddy }
5042