xref: /linux/drivers/scsi/mpi3mr/mpi3mr_fw.c (revision 2a8a0147cb523ff8c21e57195dae1833b35dafaf)
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;
302f5e6d5a3SSumit Saxena 	case MPI3MR_HOSTTAG_BSG_CMDS:
303f5e6d5a3SSumit Saxena 		return &mrioc->bsg_cmds;
304e844adb1SKashyap Desai 	case MPI3MR_HOSTTAG_BLK_TMS:
305e844adb1SKashyap Desai 		return &mrioc->host_tm_cmds;
30643ca1100SSumit Saxena 	case MPI3MR_HOSTTAG_PEL_ABORT:
30743ca1100SSumit Saxena 		return &mrioc->pel_abort_cmd;
30843ca1100SSumit Saxena 	case MPI3MR_HOSTTAG_PEL_WAIT:
30943ca1100SSumit Saxena 		return &mrioc->pel_cmds;
310824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INVALID:
311824a1566SKashyap Desai 		if (def_reply && def_reply->function ==
312824a1566SKashyap Desai 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
313824a1566SKashyap Desai 			mpi3mr_handle_events(mrioc, def_reply);
314824a1566SKashyap Desai 		return NULL;
315824a1566SKashyap Desai 	default:
316824a1566SKashyap Desai 		break;
317824a1566SKashyap Desai 	}
31813ef29eaSKashyap Desai 	if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
31913ef29eaSKashyap Desai 	    host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) {
32013ef29eaSKashyap Desai 		idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
32113ef29eaSKashyap Desai 		return &mrioc->dev_rmhs_cmds[idx];
32213ef29eaSKashyap Desai 	}
323824a1566SKashyap Desai 
324c1af985dSSreekanth Reddy 	if (host_tag >= MPI3MR_HOSTTAG_EVTACKCMD_MIN &&
325c1af985dSSreekanth Reddy 	    host_tag <= MPI3MR_HOSTTAG_EVTACKCMD_MAX) {
326c1af985dSSreekanth Reddy 		idx = host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
327c1af985dSSreekanth Reddy 		return &mrioc->evtack_cmds[idx];
328c1af985dSSreekanth Reddy 	}
329c1af985dSSreekanth Reddy 
330824a1566SKashyap Desai 	return NULL;
331824a1566SKashyap Desai }
332824a1566SKashyap Desai 
333824a1566SKashyap Desai static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
334824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
335824a1566SKashyap Desai {
336824a1566SKashyap Desai 	u16 reply_desc_type, host_tag = 0;
337824a1566SKashyap Desai 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
338824a1566SKashyap Desai 	u32 ioc_loginfo = 0;
339824a1566SKashyap Desai 	struct mpi3_status_reply_descriptor *status_desc;
340824a1566SKashyap Desai 	struct mpi3_address_reply_descriptor *addr_desc;
341824a1566SKashyap Desai 	struct mpi3_success_reply_descriptor *success_desc;
342824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply = NULL;
343824a1566SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr = NULL;
344824a1566SKashyap Desai 	struct mpi3_scsi_io_reply *scsi_reply;
345824a1566SKashyap Desai 	u8 *sense_buf = NULL;
346824a1566SKashyap Desai 
347824a1566SKashyap Desai 	*reply_dma = 0;
348824a1566SKashyap Desai 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
349824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
350824a1566SKashyap Desai 	switch (reply_desc_type) {
351824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
352824a1566SKashyap Desai 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
353824a1566SKashyap Desai 		host_tag = le16_to_cpu(status_desc->host_tag);
354824a1566SKashyap Desai 		ioc_status = le16_to_cpu(status_desc->ioc_status);
355824a1566SKashyap Desai 		if (ioc_status &
356824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
357824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
358824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
359824a1566SKashyap Desai 		break;
360824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
361824a1566SKashyap Desai 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
362824a1566SKashyap Desai 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
363824a1566SKashyap Desai 		def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
364824a1566SKashyap Desai 		if (!def_reply)
365824a1566SKashyap Desai 			goto out;
366824a1566SKashyap Desai 		host_tag = le16_to_cpu(def_reply->host_tag);
367824a1566SKashyap Desai 		ioc_status = le16_to_cpu(def_reply->ioc_status);
368824a1566SKashyap Desai 		if (ioc_status &
369824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
370824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
371824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
372824a1566SKashyap Desai 		if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
373824a1566SKashyap Desai 			scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
374824a1566SKashyap Desai 			sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
375824a1566SKashyap Desai 			    le64_to_cpu(scsi_reply->sense_data_buffer_address));
376824a1566SKashyap Desai 		}
377824a1566SKashyap Desai 		break;
378824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
379824a1566SKashyap Desai 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
380824a1566SKashyap Desai 		host_tag = le16_to_cpu(success_desc->host_tag);
381824a1566SKashyap Desai 		break;
382824a1566SKashyap Desai 	default:
383824a1566SKashyap Desai 		break;
384824a1566SKashyap Desai 	}
385824a1566SKashyap Desai 
386824a1566SKashyap Desai 	cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
387824a1566SKashyap Desai 	if (cmdptr) {
388824a1566SKashyap Desai 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
389824a1566SKashyap Desai 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
390824a1566SKashyap Desai 			cmdptr->ioc_loginfo = ioc_loginfo;
391824a1566SKashyap Desai 			cmdptr->ioc_status = ioc_status;
392824a1566SKashyap Desai 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
393824a1566SKashyap Desai 			if (def_reply) {
394824a1566SKashyap Desai 				cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
395824a1566SKashyap Desai 				memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
396c5758fc7SSreekanth Reddy 				    mrioc->reply_sz);
397824a1566SKashyap Desai 			}
398824a1566SKashyap Desai 			if (cmdptr->is_waiting) {
399824a1566SKashyap Desai 				complete(&cmdptr->done);
400824a1566SKashyap Desai 				cmdptr->is_waiting = 0;
401824a1566SKashyap Desai 			} else if (cmdptr->callback)
402824a1566SKashyap Desai 				cmdptr->callback(mrioc, cmdptr);
403824a1566SKashyap Desai 		}
404824a1566SKashyap Desai 	}
405824a1566SKashyap Desai out:
406824a1566SKashyap Desai 	if (sense_buf)
407824a1566SKashyap Desai 		mpi3mr_repost_sense_buf(mrioc,
408824a1566SKashyap Desai 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
409824a1566SKashyap Desai }
410824a1566SKashyap Desai 
411824a1566SKashyap Desai static int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
412824a1566SKashyap Desai {
413824a1566SKashyap Desai 	u32 exp_phase = mrioc->admin_reply_ephase;
414824a1566SKashyap Desai 	u32 admin_reply_ci = mrioc->admin_reply_ci;
415824a1566SKashyap Desai 	u32 num_admin_replies = 0;
416824a1566SKashyap Desai 	u64 reply_dma = 0;
417824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
418824a1566SKashyap Desai 
419824a1566SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
420824a1566SKashyap Desai 	    admin_reply_ci;
421824a1566SKashyap Desai 
422824a1566SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
423824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
424824a1566SKashyap Desai 		return 0;
425824a1566SKashyap Desai 
426824a1566SKashyap Desai 	do {
427824a1566SKashyap Desai 		mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
428824a1566SKashyap Desai 		mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
429824a1566SKashyap Desai 		if (reply_dma)
430824a1566SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
431824a1566SKashyap Desai 		num_admin_replies++;
432824a1566SKashyap Desai 		if (++admin_reply_ci == mrioc->num_admin_replies) {
433824a1566SKashyap Desai 			admin_reply_ci = 0;
434824a1566SKashyap Desai 			exp_phase ^= 1;
435824a1566SKashyap Desai 		}
436824a1566SKashyap Desai 		reply_desc =
437824a1566SKashyap Desai 		    (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
438824a1566SKashyap Desai 		    admin_reply_ci;
439824a1566SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
440824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
441824a1566SKashyap Desai 			break;
442824a1566SKashyap Desai 	} while (1);
443824a1566SKashyap Desai 
444824a1566SKashyap Desai 	writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
445824a1566SKashyap Desai 	mrioc->admin_reply_ci = admin_reply_ci;
446824a1566SKashyap Desai 	mrioc->admin_reply_ephase = exp_phase;
447824a1566SKashyap Desai 
448824a1566SKashyap Desai 	return num_admin_replies;
449824a1566SKashyap Desai }
450824a1566SKashyap Desai 
451023ab2a9SKashyap Desai /**
452023ab2a9SKashyap Desai  * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to
453023ab2a9SKashyap Desai  *	queue's consumer index from operational reply descriptor queue.
454023ab2a9SKashyap Desai  * @op_reply_q: op_reply_qinfo object
455023ab2a9SKashyap Desai  * @reply_ci: operational reply descriptor's queue consumer index
456023ab2a9SKashyap Desai  *
457023ab2a9SKashyap Desai  * Returns reply descriptor frame address
458023ab2a9SKashyap Desai  */
459023ab2a9SKashyap Desai static inline struct mpi3_default_reply_descriptor *
460023ab2a9SKashyap Desai mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci)
461023ab2a9SKashyap Desai {
462023ab2a9SKashyap Desai 	void *segment_base_addr;
463023ab2a9SKashyap Desai 	struct segments *segments = op_reply_q->q_segments;
464023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc = NULL;
465023ab2a9SKashyap Desai 
466023ab2a9SKashyap Desai 	segment_base_addr =
467023ab2a9SKashyap Desai 	    segments[reply_ci / op_reply_q->segment_qd].segment;
468023ab2a9SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr +
469023ab2a9SKashyap Desai 	    (reply_ci % op_reply_q->segment_qd);
470023ab2a9SKashyap Desai 	return reply_desc;
471023ab2a9SKashyap Desai }
472023ab2a9SKashyap Desai 
473afd3a579SSreekanth Reddy /**
474afd3a579SSreekanth Reddy  * mpi3mr_process_op_reply_q - Operational reply queue handler
475afd3a579SSreekanth Reddy  * @mrioc: Adapter instance reference
476afd3a579SSreekanth Reddy  * @op_reply_q: Operational reply queue info
477afd3a579SSreekanth Reddy  *
478afd3a579SSreekanth Reddy  * Checks the specific operational reply queue and drains the
479afd3a579SSreekanth Reddy  * reply queue entries until the queue is empty and process the
480afd3a579SSreekanth Reddy  * individual reply descriptors.
481afd3a579SSreekanth Reddy  *
482afd3a579SSreekanth Reddy  * Return: 0 if queue is already processed,or number of reply
483afd3a579SSreekanth Reddy  *	    descriptors processed.
484afd3a579SSreekanth Reddy  */
485afd3a579SSreekanth Reddy int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc,
486afd3a579SSreekanth Reddy 	struct op_reply_qinfo *op_reply_q)
487023ab2a9SKashyap Desai {
488023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q;
489023ab2a9SKashyap Desai 	u32 exp_phase;
490023ab2a9SKashyap Desai 	u32 reply_ci;
491023ab2a9SKashyap Desai 	u32 num_op_reply = 0;
492023ab2a9SKashyap Desai 	u64 reply_dma = 0;
493023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
494023ab2a9SKashyap Desai 	u16 req_q_idx = 0, reply_qidx;
495023ab2a9SKashyap Desai 
496023ab2a9SKashyap Desai 	reply_qidx = op_reply_q->qid - 1;
497023ab2a9SKashyap Desai 
498463429f8SKashyap Desai 	if (!atomic_add_unless(&op_reply_q->in_use, 1, 1))
499463429f8SKashyap Desai 		return 0;
500463429f8SKashyap Desai 
501023ab2a9SKashyap Desai 	exp_phase = op_reply_q->ephase;
502023ab2a9SKashyap Desai 	reply_ci = op_reply_q->ci;
503023ab2a9SKashyap Desai 
504023ab2a9SKashyap Desai 	reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
505023ab2a9SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
506023ab2a9SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
507463429f8SKashyap Desai 		atomic_dec(&op_reply_q->in_use);
508023ab2a9SKashyap Desai 		return 0;
509023ab2a9SKashyap Desai 	}
510023ab2a9SKashyap Desai 
511023ab2a9SKashyap Desai 	do {
512023ab2a9SKashyap Desai 		req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1;
513023ab2a9SKashyap Desai 		op_req_q = &mrioc->req_qinfo[req_q_idx];
514023ab2a9SKashyap Desai 
515023ab2a9SKashyap Desai 		WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci));
516023ab2a9SKashyap Desai 		mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma,
517023ab2a9SKashyap Desai 		    reply_qidx);
518463429f8SKashyap Desai 		atomic_dec(&op_reply_q->pend_ios);
519023ab2a9SKashyap Desai 		if (reply_dma)
520023ab2a9SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
521023ab2a9SKashyap Desai 		num_op_reply++;
522023ab2a9SKashyap Desai 
523023ab2a9SKashyap Desai 		if (++reply_ci == op_reply_q->num_replies) {
524023ab2a9SKashyap Desai 			reply_ci = 0;
525023ab2a9SKashyap Desai 			exp_phase ^= 1;
526023ab2a9SKashyap Desai 		}
527023ab2a9SKashyap Desai 
528023ab2a9SKashyap Desai 		reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
529023ab2a9SKashyap Desai 
530023ab2a9SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
531023ab2a9SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
532023ab2a9SKashyap Desai 			break;
533463429f8SKashyap Desai 		/*
534463429f8SKashyap Desai 		 * Exit completion loop to avoid CPU lockup
535463429f8SKashyap Desai 		 * Ensure remaining completion happens from threaded ISR.
536463429f8SKashyap Desai 		 */
537463429f8SKashyap Desai 		if (num_op_reply > mrioc->max_host_ios) {
538afd3a579SSreekanth Reddy 			op_reply_q->enable_irq_poll = true;
539463429f8SKashyap Desai 			break;
540463429f8SKashyap Desai 		}
541023ab2a9SKashyap Desai 
542023ab2a9SKashyap Desai 	} while (1);
543023ab2a9SKashyap Desai 
544023ab2a9SKashyap Desai 	writel(reply_ci,
545023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index);
546023ab2a9SKashyap Desai 	op_reply_q->ci = reply_ci;
547023ab2a9SKashyap Desai 	op_reply_q->ephase = exp_phase;
548023ab2a9SKashyap Desai 
549463429f8SKashyap Desai 	atomic_dec(&op_reply_q->in_use);
550023ab2a9SKashyap Desai 	return num_op_reply;
551023ab2a9SKashyap Desai }
552023ab2a9SKashyap Desai 
553afd3a579SSreekanth Reddy /**
554afd3a579SSreekanth Reddy  * mpi3mr_blk_mq_poll - Operational reply queue handler
555afd3a579SSreekanth Reddy  * @shost: SCSI Host reference
556afd3a579SSreekanth Reddy  * @queue_num: Request queue number (w.r.t OS it is hardware context number)
557afd3a579SSreekanth Reddy  *
558afd3a579SSreekanth Reddy  * Checks the specific operational reply queue and drains the
559afd3a579SSreekanth Reddy  * reply queue entries until the queue is empty and process the
560afd3a579SSreekanth Reddy  * individual reply descriptors.
561afd3a579SSreekanth Reddy  *
562afd3a579SSreekanth Reddy  * Return: 0 if queue is already processed,or number of reply
563afd3a579SSreekanth Reddy  *	    descriptors processed.
564afd3a579SSreekanth Reddy  */
565afd3a579SSreekanth Reddy int mpi3mr_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
566afd3a579SSreekanth Reddy {
567afd3a579SSreekanth Reddy 	int num_entries = 0;
568afd3a579SSreekanth Reddy 	struct mpi3mr_ioc *mrioc;
569afd3a579SSreekanth Reddy 
570afd3a579SSreekanth Reddy 	mrioc = (struct mpi3mr_ioc *)shost->hostdata;
571afd3a579SSreekanth Reddy 
572afd3a579SSreekanth Reddy 	if ((mrioc->reset_in_progress || mrioc->prepare_for_reset))
573afd3a579SSreekanth Reddy 		return 0;
574afd3a579SSreekanth Reddy 
575afd3a579SSreekanth Reddy 	num_entries = mpi3mr_process_op_reply_q(mrioc,
576afd3a579SSreekanth Reddy 			&mrioc->op_reply_qinfo[queue_num]);
577afd3a579SSreekanth Reddy 
578afd3a579SSreekanth Reddy 	return num_entries;
579afd3a579SSreekanth Reddy }
580afd3a579SSreekanth Reddy 
581824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
582824a1566SKashyap Desai {
583824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
584824a1566SKashyap Desai 	struct mpi3mr_ioc *mrioc;
585824a1566SKashyap Desai 	u16 midx;
586463429f8SKashyap Desai 	u32 num_admin_replies = 0, num_op_reply = 0;
587824a1566SKashyap Desai 
588824a1566SKashyap Desai 	if (!intr_info)
589824a1566SKashyap Desai 		return IRQ_NONE;
590824a1566SKashyap Desai 
591824a1566SKashyap Desai 	mrioc = intr_info->mrioc;
592824a1566SKashyap Desai 
593824a1566SKashyap Desai 	if (!mrioc->intr_enabled)
594824a1566SKashyap Desai 		return IRQ_NONE;
595824a1566SKashyap Desai 
596824a1566SKashyap Desai 	midx = intr_info->msix_index;
597824a1566SKashyap Desai 
598824a1566SKashyap Desai 	if (!midx)
599824a1566SKashyap Desai 		num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
600463429f8SKashyap Desai 	if (intr_info->op_reply_q)
601afd3a579SSreekanth Reddy 		num_op_reply = mpi3mr_process_op_reply_q(mrioc,
602afd3a579SSreekanth Reddy 		    intr_info->op_reply_q);
603824a1566SKashyap Desai 
604463429f8SKashyap Desai 	if (num_admin_replies || num_op_reply)
605824a1566SKashyap Desai 		return IRQ_HANDLED;
606824a1566SKashyap Desai 	else
607824a1566SKashyap Desai 		return IRQ_NONE;
608824a1566SKashyap Desai }
609824a1566SKashyap Desai 
610824a1566SKashyap Desai static irqreturn_t mpi3mr_isr(int irq, void *privdata)
611824a1566SKashyap Desai {
612824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
613463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
614463429f8SKashyap Desai 	u16 midx;
615824a1566SKashyap Desai 	int ret;
616824a1566SKashyap Desai 
617824a1566SKashyap Desai 	if (!intr_info)
618824a1566SKashyap Desai 		return IRQ_NONE;
619824a1566SKashyap Desai 
620463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
621463429f8SKashyap Desai 	midx = intr_info->msix_index;
622824a1566SKashyap Desai 	/* Call primary ISR routine */
623824a1566SKashyap Desai 	ret = mpi3mr_isr_primary(irq, privdata);
624824a1566SKashyap Desai 
625463429f8SKashyap Desai 	/*
626463429f8SKashyap Desai 	 * If more IOs are expected, schedule IRQ polling thread.
627463429f8SKashyap Desai 	 * Otherwise exit from ISR.
628463429f8SKashyap Desai 	 */
629463429f8SKashyap Desai 	if (!intr_info->op_reply_q)
630824a1566SKashyap Desai 		return ret;
631463429f8SKashyap Desai 
632463429f8SKashyap Desai 	if (!intr_info->op_reply_q->enable_irq_poll ||
633463429f8SKashyap Desai 	    !atomic_read(&intr_info->op_reply_q->pend_ios))
634463429f8SKashyap Desai 		return ret;
635463429f8SKashyap Desai 
636463429f8SKashyap Desai 	disable_irq_nosync(pci_irq_vector(mrioc->pdev, midx));
637463429f8SKashyap Desai 
638463429f8SKashyap Desai 	return IRQ_WAKE_THREAD;
639824a1566SKashyap Desai }
640824a1566SKashyap Desai 
641824a1566SKashyap Desai /**
642824a1566SKashyap Desai  * mpi3mr_isr_poll - Reply queue polling routine
643824a1566SKashyap Desai  * @irq: IRQ
644824a1566SKashyap Desai  * @privdata: Interrupt info
645824a1566SKashyap Desai  *
646824a1566SKashyap Desai  * poll for pending I/O completions in a loop until pending I/Os
647824a1566SKashyap Desai  * present or controller queue depth I/Os are processed.
648824a1566SKashyap Desai  *
649824a1566SKashyap Desai  * Return: IRQ_NONE or IRQ_HANDLED
650824a1566SKashyap Desai  */
651824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
652824a1566SKashyap Desai {
653463429f8SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
654463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
655463429f8SKashyap Desai 	u16 midx;
656463429f8SKashyap Desai 	u32 num_op_reply = 0;
657463429f8SKashyap Desai 
658463429f8SKashyap Desai 	if (!intr_info || !intr_info->op_reply_q)
659463429f8SKashyap Desai 		return IRQ_NONE;
660463429f8SKashyap Desai 
661463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
662463429f8SKashyap Desai 	midx = intr_info->msix_index;
663463429f8SKashyap Desai 
664463429f8SKashyap Desai 	/* Poll for pending IOs completions */
665463429f8SKashyap Desai 	do {
666463429f8SKashyap Desai 		if (!mrioc->intr_enabled)
667463429f8SKashyap Desai 			break;
668463429f8SKashyap Desai 
669463429f8SKashyap Desai 		if (!midx)
670463429f8SKashyap Desai 			mpi3mr_process_admin_reply_q(mrioc);
671463429f8SKashyap Desai 		if (intr_info->op_reply_q)
672463429f8SKashyap Desai 			num_op_reply +=
673afd3a579SSreekanth Reddy 			    mpi3mr_process_op_reply_q(mrioc,
674afd3a579SSreekanth Reddy 				intr_info->op_reply_q);
675463429f8SKashyap Desai 
676afd3a579SSreekanth Reddy 		usleep_range(MPI3MR_IRQ_POLL_SLEEP, 10 * MPI3MR_IRQ_POLL_SLEEP);
677463429f8SKashyap Desai 
678463429f8SKashyap Desai 	} while (atomic_read(&intr_info->op_reply_q->pend_ios) &&
679463429f8SKashyap Desai 	    (num_op_reply < mrioc->max_host_ios));
680463429f8SKashyap Desai 
681463429f8SKashyap Desai 	intr_info->op_reply_q->enable_irq_poll = false;
682463429f8SKashyap Desai 	enable_irq(pci_irq_vector(mrioc->pdev, midx));
683463429f8SKashyap Desai 
684824a1566SKashyap Desai 	return IRQ_HANDLED;
685824a1566SKashyap Desai }
686824a1566SKashyap Desai 
687824a1566SKashyap Desai /**
688824a1566SKashyap Desai  * mpi3mr_request_irq - Request IRQ and register ISR
689824a1566SKashyap Desai  * @mrioc: Adapter instance reference
690824a1566SKashyap Desai  * @index: IRQ vector index
691824a1566SKashyap Desai  *
692824a1566SKashyap Desai  * Request threaded ISR with primary ISR and secondary
693824a1566SKashyap Desai  *
694824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
695824a1566SKashyap Desai  */
696824a1566SKashyap Desai static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
697824a1566SKashyap Desai {
698824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
699824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
700824a1566SKashyap Desai 	int retval = 0;
701824a1566SKashyap Desai 
702824a1566SKashyap Desai 	intr_info->mrioc = mrioc;
703824a1566SKashyap Desai 	intr_info->msix_index = index;
704824a1566SKashyap Desai 	intr_info->op_reply_q = NULL;
705824a1566SKashyap Desai 
706824a1566SKashyap Desai 	snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
707824a1566SKashyap Desai 	    mrioc->driver_name, mrioc->id, index);
708824a1566SKashyap Desai 
709824a1566SKashyap Desai 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
710824a1566SKashyap Desai 	    mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
711824a1566SKashyap Desai 	if (retval) {
712824a1566SKashyap Desai 		ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
713824a1566SKashyap Desai 		    intr_info->name, pci_irq_vector(pdev, index));
714824a1566SKashyap Desai 		return retval;
715824a1566SKashyap Desai 	}
716824a1566SKashyap Desai 
717824a1566SKashyap Desai 	return retval;
718824a1566SKashyap Desai }
719824a1566SKashyap Desai 
720afd3a579SSreekanth Reddy static void mpi3mr_calc_poll_queues(struct mpi3mr_ioc *mrioc, u16 max_vectors)
721afd3a579SSreekanth Reddy {
722afd3a579SSreekanth Reddy 	if (!mrioc->requested_poll_qcount)
723afd3a579SSreekanth Reddy 		return;
724afd3a579SSreekanth Reddy 
725afd3a579SSreekanth Reddy 	/* Reserved for Admin and Default Queue */
726afd3a579SSreekanth Reddy 	if (max_vectors > 2 &&
727afd3a579SSreekanth Reddy 		(mrioc->requested_poll_qcount < max_vectors - 2)) {
728afd3a579SSreekanth Reddy 		ioc_info(mrioc,
729afd3a579SSreekanth Reddy 		    "enabled polled queues (%d) msix (%d)\n",
730afd3a579SSreekanth Reddy 		    mrioc->requested_poll_qcount, max_vectors);
731afd3a579SSreekanth Reddy 	} else {
732afd3a579SSreekanth Reddy 		ioc_info(mrioc,
733afd3a579SSreekanth Reddy 		    "disabled polled queues (%d) msix (%d) because of no resources for default queue\n",
734afd3a579SSreekanth Reddy 		    mrioc->requested_poll_qcount, max_vectors);
735afd3a579SSreekanth Reddy 		mrioc->requested_poll_qcount = 0;
736afd3a579SSreekanth Reddy 	}
737afd3a579SSreekanth Reddy }
738afd3a579SSreekanth Reddy 
739824a1566SKashyap Desai /**
740824a1566SKashyap Desai  * mpi3mr_setup_isr - Setup ISR for the controller
741824a1566SKashyap Desai  * @mrioc: Adapter instance reference
742824a1566SKashyap Desai  * @setup_one: Request one IRQ or more
743824a1566SKashyap Desai  *
744824a1566SKashyap Desai  * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
745824a1566SKashyap Desai  *
746824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
747824a1566SKashyap Desai  */
748824a1566SKashyap Desai static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
749824a1566SKashyap Desai {
750824a1566SKashyap Desai 	unsigned int irq_flags = PCI_IRQ_MSIX;
751afd3a579SSreekanth Reddy 	int max_vectors, min_vec;
7522938beddSDan Carpenter 	int retval;
7532938beddSDan Carpenter 	int i;
754afd3a579SSreekanth Reddy 	struct irq_affinity desc = { .pre_vectors =  1, .post_vectors = 1 };
755824a1566SKashyap Desai 
756fe6db615SSreekanth Reddy 	if (mrioc->is_intr_info_set)
757fe6db615SSreekanth Reddy 		return 0;
758fe6db615SSreekanth Reddy 
759824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
760824a1566SKashyap Desai 
761afd3a579SSreekanth Reddy 	if (setup_one || reset_devices) {
762824a1566SKashyap Desai 		max_vectors = 1;
763afd3a579SSreekanth Reddy 		retval = pci_alloc_irq_vectors(mrioc->pdev,
764afd3a579SSreekanth Reddy 		    1, max_vectors, irq_flags);
765afd3a579SSreekanth Reddy 		if (retval < 0) {
766afd3a579SSreekanth Reddy 			ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
767afd3a579SSreekanth Reddy 			    retval);
768afd3a579SSreekanth Reddy 			goto out_failed;
769afd3a579SSreekanth Reddy 		}
770afd3a579SSreekanth Reddy 	} else {
771824a1566SKashyap Desai 		max_vectors =
772afd3a579SSreekanth Reddy 		    min_t(int, mrioc->cpu_count + 1 +
773afd3a579SSreekanth Reddy 			mrioc->requested_poll_qcount, mrioc->msix_count);
774afd3a579SSreekanth Reddy 
775afd3a579SSreekanth Reddy 		mpi3mr_calc_poll_queues(mrioc, max_vectors);
776824a1566SKashyap Desai 
777824a1566SKashyap Desai 		ioc_info(mrioc,
778824a1566SKashyap Desai 		    "MSI-X vectors supported: %d, no of cores: %d,",
779824a1566SKashyap Desai 		    mrioc->msix_count, mrioc->cpu_count);
780824a1566SKashyap Desai 		ioc_info(mrioc,
781afd3a579SSreekanth Reddy 		    "MSI-x vectors requested: %d poll_queues %d\n",
782afd3a579SSreekanth Reddy 		    max_vectors, mrioc->requested_poll_qcount);
783824a1566SKashyap Desai 
784afd3a579SSreekanth Reddy 		desc.post_vectors = mrioc->requested_poll_qcount;
785afd3a579SSreekanth Reddy 		min_vec = desc.pre_vectors + desc.post_vectors;
786824a1566SKashyap Desai 		irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
787824a1566SKashyap Desai 
7882938beddSDan Carpenter 		retval = pci_alloc_irq_vectors_affinity(mrioc->pdev,
789afd3a579SSreekanth Reddy 			min_vec, max_vectors, irq_flags, &desc);
790afd3a579SSreekanth Reddy 
7912938beddSDan Carpenter 		if (retval < 0) {
792afd3a579SSreekanth Reddy 			ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
793afd3a579SSreekanth Reddy 			    retval);
794824a1566SKashyap Desai 			goto out_failed;
795824a1566SKashyap Desai 		}
796afd3a579SSreekanth Reddy 
797afd3a579SSreekanth Reddy 
798c9566231SKashyap Desai 		/*
799c9566231SKashyap Desai 		 * If only one MSI-x is allocated, then MSI-x 0 will be shared
800c9566231SKashyap Desai 		 * between Admin queue and operational queue
801c9566231SKashyap Desai 		 */
802afd3a579SSreekanth Reddy 		if (retval == min_vec)
803c9566231SKashyap Desai 			mrioc->op_reply_q_offset = 0;
804afd3a579SSreekanth Reddy 		else if (retval != (max_vectors)) {
805afd3a579SSreekanth Reddy 			ioc_info(mrioc,
806afd3a579SSreekanth Reddy 			    "allocated vectors (%d) are less than configured (%d)\n",
807afd3a579SSreekanth Reddy 			    retval, max_vectors);
808afd3a579SSreekanth Reddy 		}
809824a1566SKashyap Desai 
8102938beddSDan Carpenter 		max_vectors = retval;
811afd3a579SSreekanth Reddy 		mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0;
812afd3a579SSreekanth Reddy 
813afd3a579SSreekanth Reddy 		mpi3mr_calc_poll_queues(mrioc, max_vectors);
814afd3a579SSreekanth Reddy 
815824a1566SKashyap Desai 	}
816afd3a579SSreekanth Reddy 
817824a1566SKashyap Desai 	mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
818824a1566SKashyap Desai 	    GFP_KERNEL);
819824a1566SKashyap Desai 	if (!mrioc->intr_info) {
8202938beddSDan Carpenter 		retval = -ENOMEM;
821824a1566SKashyap Desai 		pci_free_irq_vectors(mrioc->pdev);
822824a1566SKashyap Desai 		goto out_failed;
823824a1566SKashyap Desai 	}
824824a1566SKashyap Desai 	for (i = 0; i < max_vectors; i++) {
825824a1566SKashyap Desai 		retval = mpi3mr_request_irq(mrioc, i);
826824a1566SKashyap Desai 		if (retval) {
827824a1566SKashyap Desai 			mrioc->intr_info_count = i;
828824a1566SKashyap Desai 			goto out_failed;
829824a1566SKashyap Desai 		}
830824a1566SKashyap Desai 	}
831fe6db615SSreekanth Reddy 	if (reset_devices || !setup_one)
832fe6db615SSreekanth Reddy 		mrioc->is_intr_info_set = true;
833824a1566SKashyap Desai 	mrioc->intr_info_count = max_vectors;
834824a1566SKashyap Desai 	mpi3mr_ioc_enable_intr(mrioc);
8352938beddSDan Carpenter 	return 0;
8362938beddSDan Carpenter 
837824a1566SKashyap Desai out_failed:
838824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
839824a1566SKashyap Desai 
840824a1566SKashyap Desai 	return retval;
841824a1566SKashyap Desai }
842824a1566SKashyap Desai 
843824a1566SKashyap Desai static const struct {
844824a1566SKashyap Desai 	enum mpi3mr_iocstate value;
845824a1566SKashyap Desai 	char *name;
846824a1566SKashyap Desai } mrioc_states[] = {
847824a1566SKashyap Desai 	{ MRIOC_STATE_READY, "ready" },
848824a1566SKashyap Desai 	{ MRIOC_STATE_FAULT, "fault" },
849824a1566SKashyap Desai 	{ MRIOC_STATE_RESET, "reset" },
850824a1566SKashyap Desai 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
851824a1566SKashyap Desai 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
852824a1566SKashyap Desai 	{ MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
853824a1566SKashyap Desai };
854824a1566SKashyap Desai 
855824a1566SKashyap Desai static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
856824a1566SKashyap Desai {
857824a1566SKashyap Desai 	int i;
858824a1566SKashyap Desai 	char *name = NULL;
859824a1566SKashyap Desai 
860824a1566SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
861824a1566SKashyap Desai 		if (mrioc_states[i].value == mrioc_state) {
862824a1566SKashyap Desai 			name = mrioc_states[i].name;
863824a1566SKashyap Desai 			break;
864824a1566SKashyap Desai 		}
865824a1566SKashyap Desai 	}
866824a1566SKashyap Desai 	return name;
867824a1566SKashyap Desai }
868824a1566SKashyap Desai 
869f061178eSKashyap Desai /* Reset reason to name mapper structure*/
870f061178eSKashyap Desai static const struct {
871f061178eSKashyap Desai 	enum mpi3mr_reset_reason value;
872f061178eSKashyap Desai 	char *name;
873f061178eSKashyap Desai } mpi3mr_reset_reason_codes[] = {
874f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
875f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
876f5e6d5a3SSumit Saxena 	{ MPI3MR_RESET_FROM_APP, "application invocation" },
877f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EH_HOS, "error handling" },
878f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
879f5e6d5a3SSumit Saxena 	{ MPI3MR_RESET_FROM_APP_TIMEOUT, "application command timeout" },
880f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
881f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
882f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
883f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
884f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
885f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
886f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
887f061178eSKashyap Desai 	{
888f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
889f061178eSKashyap Desai 		"create request queue timeout"
890f061178eSKashyap Desai 	},
891f061178eSKashyap Desai 	{
892f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
893f061178eSKashyap Desai 		"create reply queue timeout"
894f061178eSKashyap Desai 	},
895f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
896f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
897f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
898f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
899f061178eSKashyap Desai 	{
900f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CIACTVRST_TIMER,
901f061178eSKashyap Desai 		"component image activation timeout"
902f061178eSKashyap Desai 	},
903f061178eSKashyap Desai 	{
904f061178eSKashyap Desai 		MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
905f061178eSKashyap Desai 		"get package version timeout"
906f061178eSKashyap Desai 	},
907f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
908f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
9095867b856SColin Ian King 	{ MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronous reset" },
910f061178eSKashyap Desai };
911f061178eSKashyap Desai 
912f061178eSKashyap Desai /**
913f061178eSKashyap Desai  * mpi3mr_reset_rc_name - get reset reason code name
914f061178eSKashyap Desai  * @reason_code: reset reason code value
915f061178eSKashyap Desai  *
916f061178eSKashyap Desai  * Map reset reason to an NULL terminated ASCII string
917f061178eSKashyap Desai  *
918f061178eSKashyap Desai  * Return: name corresponding to reset reason value or NULL.
919f061178eSKashyap Desai  */
920f061178eSKashyap Desai static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
921f061178eSKashyap Desai {
922f061178eSKashyap Desai 	int i;
923f061178eSKashyap Desai 	char *name = NULL;
924f061178eSKashyap Desai 
925f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) {
926f061178eSKashyap Desai 		if (mpi3mr_reset_reason_codes[i].value == reason_code) {
927f061178eSKashyap Desai 			name = mpi3mr_reset_reason_codes[i].name;
928f061178eSKashyap Desai 			break;
929f061178eSKashyap Desai 		}
930f061178eSKashyap Desai 	}
931f061178eSKashyap Desai 	return name;
932f061178eSKashyap Desai }
933f061178eSKashyap Desai 
934f061178eSKashyap Desai /* Reset type to name mapper structure*/
935f061178eSKashyap Desai static const struct {
936f061178eSKashyap Desai 	u16 reset_type;
937f061178eSKashyap Desai 	char *name;
938f061178eSKashyap Desai } mpi3mr_reset_types[] = {
939f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
940f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
941f061178eSKashyap Desai };
942f061178eSKashyap Desai 
943f061178eSKashyap Desai /**
944f061178eSKashyap Desai  * mpi3mr_reset_type_name - get reset type name
945f061178eSKashyap Desai  * @reset_type: reset type value
946f061178eSKashyap Desai  *
947f061178eSKashyap Desai  * Map reset type to an NULL terminated ASCII string
948f061178eSKashyap Desai  *
949f061178eSKashyap Desai  * Return: name corresponding to reset type value or NULL.
950f061178eSKashyap Desai  */
951f061178eSKashyap Desai static const char *mpi3mr_reset_type_name(u16 reset_type)
952f061178eSKashyap Desai {
953f061178eSKashyap Desai 	int i;
954f061178eSKashyap Desai 	char *name = NULL;
955f061178eSKashyap Desai 
956f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) {
957f061178eSKashyap Desai 		if (mpi3mr_reset_types[i].reset_type == reset_type) {
958f061178eSKashyap Desai 			name = mpi3mr_reset_types[i].name;
959f061178eSKashyap Desai 			break;
960f061178eSKashyap Desai 		}
961f061178eSKashyap Desai 	}
962f061178eSKashyap Desai 	return name;
963f061178eSKashyap Desai }
964f061178eSKashyap Desai 
965824a1566SKashyap Desai /**
966824a1566SKashyap Desai  * mpi3mr_print_fault_info - Display fault information
967824a1566SKashyap Desai  * @mrioc: Adapter instance reference
968824a1566SKashyap Desai  *
969824a1566SKashyap Desai  * Display the controller fault information if there is a
970824a1566SKashyap Desai  * controller fault.
971824a1566SKashyap Desai  *
972824a1566SKashyap Desai  * Return: Nothing.
973824a1566SKashyap Desai  */
974b64845a7SSreekanth Reddy void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
975824a1566SKashyap Desai {
976824a1566SKashyap Desai 	u32 ioc_status, code, code1, code2, code3;
977824a1566SKashyap Desai 
978824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
979824a1566SKashyap Desai 
980824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
981824a1566SKashyap Desai 		code = readl(&mrioc->sysif_regs->fault);
982824a1566SKashyap Desai 		code1 = readl(&mrioc->sysif_regs->fault_info[0]);
983824a1566SKashyap Desai 		code2 = readl(&mrioc->sysif_regs->fault_info[1]);
984824a1566SKashyap Desai 		code3 = readl(&mrioc->sysif_regs->fault_info[2]);
985824a1566SKashyap Desai 
986824a1566SKashyap Desai 		ioc_info(mrioc,
987824a1566SKashyap Desai 		    "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
988824a1566SKashyap Desai 		    code, code1, code2, code3);
989824a1566SKashyap Desai 	}
990824a1566SKashyap Desai }
991824a1566SKashyap Desai 
992824a1566SKashyap Desai /**
993824a1566SKashyap Desai  * mpi3mr_get_iocstate - Get IOC State
994824a1566SKashyap Desai  * @mrioc: Adapter instance reference
995824a1566SKashyap Desai  *
996824a1566SKashyap Desai  * Return a proper IOC state enum based on the IOC status and
997824a1566SKashyap Desai  * IOC configuration and unrcoverable state of the controller.
998824a1566SKashyap Desai  *
999824a1566SKashyap Desai  * Return: Current IOC state.
1000824a1566SKashyap Desai  */
1001824a1566SKashyap Desai enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
1002824a1566SKashyap Desai {
1003824a1566SKashyap Desai 	u32 ioc_status, ioc_config;
1004824a1566SKashyap Desai 	u8 ready, enabled;
1005824a1566SKashyap Desai 
1006824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1007824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1008824a1566SKashyap Desai 
1009824a1566SKashyap Desai 	if (mrioc->unrecoverable)
1010824a1566SKashyap Desai 		return MRIOC_STATE_UNRECOVERABLE;
1011824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
1012824a1566SKashyap Desai 		return MRIOC_STATE_FAULT;
1013824a1566SKashyap Desai 
1014824a1566SKashyap Desai 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
1015824a1566SKashyap Desai 	enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
1016824a1566SKashyap Desai 
1017824a1566SKashyap Desai 	if (ready && enabled)
1018824a1566SKashyap Desai 		return MRIOC_STATE_READY;
1019824a1566SKashyap Desai 	if ((!ready) && (!enabled))
1020824a1566SKashyap Desai 		return MRIOC_STATE_RESET;
1021824a1566SKashyap Desai 	if ((!ready) && (enabled))
1022824a1566SKashyap Desai 		return MRIOC_STATE_BECOMING_READY;
1023824a1566SKashyap Desai 
1024824a1566SKashyap Desai 	return MRIOC_STATE_RESET_REQUESTED;
1025824a1566SKashyap Desai }
1026824a1566SKashyap Desai 
1027824a1566SKashyap Desai /**
1028824a1566SKashyap Desai  * mpi3mr_clear_reset_history - clear reset history
1029824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1030824a1566SKashyap Desai  *
1031824a1566SKashyap Desai  * Write the reset history bit in IOC status to clear the bit,
1032824a1566SKashyap Desai  * if it is already set.
1033824a1566SKashyap Desai  *
1034824a1566SKashyap Desai  * Return: Nothing.
1035824a1566SKashyap Desai  */
1036824a1566SKashyap Desai static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
1037824a1566SKashyap Desai {
1038824a1566SKashyap Desai 	u32 ioc_status;
1039824a1566SKashyap Desai 
1040824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1041824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1042824a1566SKashyap Desai 		writel(ioc_status, &mrioc->sysif_regs->ioc_status);
1043824a1566SKashyap Desai }
1044824a1566SKashyap Desai 
1045824a1566SKashyap Desai /**
1046824a1566SKashyap Desai  * mpi3mr_issue_and_process_mur - Message unit Reset handler
1047824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1048824a1566SKashyap Desai  * @reset_reason: Reset reason code
1049824a1566SKashyap Desai  *
1050824a1566SKashyap Desai  * Issue Message unit Reset to the controller and wait for it to
1051824a1566SKashyap Desai  * be complete.
1052824a1566SKashyap Desai  *
1053824a1566SKashyap Desai  * Return: 0 on success, -1 on failure.
1054824a1566SKashyap Desai  */
1055824a1566SKashyap Desai static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
1056824a1566SKashyap Desai 	u32 reset_reason)
1057824a1566SKashyap Desai {
1058824a1566SKashyap Desai 	u32 ioc_config, timeout, ioc_status;
1059824a1566SKashyap Desai 	int retval = -1;
1060824a1566SKashyap Desai 
1061824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
1062824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1063824a1566SKashyap Desai 		ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
1064824a1566SKashyap Desai 		return retval;
1065824a1566SKashyap Desai 	}
1066824a1566SKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1067824a1566SKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1068824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1069824a1566SKashyap Desai 	ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1070824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1071824a1566SKashyap Desai 
1072b64845a7SSreekanth Reddy 	timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1073824a1566SKashyap Desai 	do {
1074824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1075824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
1076824a1566SKashyap Desai 			mpi3mr_clear_reset_history(mrioc);
1077824a1566SKashyap Desai 			break;
1078824a1566SKashyap Desai 		}
1079b64845a7SSreekanth Reddy 		if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
1080b64845a7SSreekanth Reddy 			mpi3mr_print_fault_info(mrioc);
1081b64845a7SSreekanth Reddy 			break;
1082824a1566SKashyap Desai 		}
1083824a1566SKashyap Desai 		msleep(100);
1084824a1566SKashyap Desai 	} while (--timeout);
1085824a1566SKashyap Desai 
1086824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1087b64845a7SSreekanth Reddy 	if (timeout && !((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1088b64845a7SSreekanth Reddy 	      (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
1089b64845a7SSreekanth Reddy 	      (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1090b64845a7SSreekanth Reddy 		retval = 0;
1091824a1566SKashyap Desai 
1092824a1566SKashyap Desai 	ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
1093824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status, ioc_config);
1094824a1566SKashyap Desai 	return retval;
1095824a1566SKashyap Desai }
1096824a1566SKashyap Desai 
1097824a1566SKashyap Desai /**
1098c5758fc7SSreekanth Reddy  * mpi3mr_revalidate_factsdata - validate IOCFacts parameters
1099c5758fc7SSreekanth Reddy  * during reset/resume
1100c5758fc7SSreekanth Reddy  * @mrioc: Adapter instance reference
1101c5758fc7SSreekanth Reddy  *
1102c5758fc7SSreekanth Reddy  * Return zero if the new IOCFacts parameters value is compatible with
1103c5758fc7SSreekanth Reddy  * older values else return -EPERM
1104c5758fc7SSreekanth Reddy  */
1105c5758fc7SSreekanth Reddy static int
1106c5758fc7SSreekanth Reddy mpi3mr_revalidate_factsdata(struct mpi3mr_ioc *mrioc)
1107c5758fc7SSreekanth Reddy {
1108c5758fc7SSreekanth Reddy 	u16 dev_handle_bitmap_sz;
1109c5758fc7SSreekanth Reddy 	void *removepend_bitmap;
1110c5758fc7SSreekanth Reddy 
1111c5758fc7SSreekanth Reddy 	if (mrioc->facts.reply_sz > mrioc->reply_sz) {
1112c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1113c5758fc7SSreekanth Reddy 		    "cannot increase reply size from %d to %d\n",
1114c5758fc7SSreekanth Reddy 		    mrioc->reply_sz, mrioc->facts.reply_sz);
1115c5758fc7SSreekanth Reddy 		return -EPERM;
1116c5758fc7SSreekanth Reddy 	}
1117c5758fc7SSreekanth Reddy 
1118c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_reply_q < mrioc->num_op_reply_q) {
1119c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1120c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational reply queues from %d to %d\n",
1121c5758fc7SSreekanth Reddy 		    mrioc->num_op_reply_q,
1122c5758fc7SSreekanth Reddy 		    mrioc->facts.max_op_reply_q);
1123c5758fc7SSreekanth Reddy 		return -EPERM;
1124c5758fc7SSreekanth Reddy 	}
1125c5758fc7SSreekanth Reddy 
1126c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_req_q < mrioc->num_op_req_q) {
1127c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1128c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational request queues from %d to %d\n",
1129c5758fc7SSreekanth Reddy 		    mrioc->num_op_req_q, mrioc->facts.max_op_req_q);
1130c5758fc7SSreekanth Reddy 		return -EPERM;
1131c5758fc7SSreekanth Reddy 	}
1132c5758fc7SSreekanth Reddy 
1133c5758fc7SSreekanth Reddy 	dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
1134c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_devhandle % 8)
1135c5758fc7SSreekanth Reddy 		dev_handle_bitmap_sz++;
1136c5758fc7SSreekanth Reddy 	if (dev_handle_bitmap_sz > mrioc->dev_handle_bitmap_sz) {
1137c5758fc7SSreekanth Reddy 		removepend_bitmap = krealloc(mrioc->removepend_bitmap,
1138c5758fc7SSreekanth Reddy 		    dev_handle_bitmap_sz, GFP_KERNEL);
1139c5758fc7SSreekanth Reddy 		if (!removepend_bitmap) {
1140c5758fc7SSreekanth Reddy 			ioc_err(mrioc,
1141c5758fc7SSreekanth Reddy 			    "failed to increase removepend_bitmap sz from: %d to %d\n",
1142c5758fc7SSreekanth Reddy 			    mrioc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
1143c5758fc7SSreekanth Reddy 			return -EPERM;
1144c5758fc7SSreekanth Reddy 		}
1145c5758fc7SSreekanth Reddy 		memset(removepend_bitmap + mrioc->dev_handle_bitmap_sz, 0,
1146c5758fc7SSreekanth Reddy 		    dev_handle_bitmap_sz - mrioc->dev_handle_bitmap_sz);
1147c5758fc7SSreekanth Reddy 		mrioc->removepend_bitmap = removepend_bitmap;
1148c5758fc7SSreekanth Reddy 		ioc_info(mrioc,
1149c5758fc7SSreekanth Reddy 		    "increased dev_handle_bitmap_sz from %d to %d\n",
1150c5758fc7SSreekanth Reddy 		    mrioc->dev_handle_bitmap_sz, dev_handle_bitmap_sz);
1151c5758fc7SSreekanth Reddy 		mrioc->dev_handle_bitmap_sz = dev_handle_bitmap_sz;
1152c5758fc7SSreekanth Reddy 	}
1153c5758fc7SSreekanth Reddy 
1154c5758fc7SSreekanth Reddy 	return 0;
1155c5758fc7SSreekanth Reddy }
1156c5758fc7SSreekanth Reddy 
1157c5758fc7SSreekanth Reddy /**
1158824a1566SKashyap Desai  * mpi3mr_bring_ioc_ready - Bring controller to ready state
1159824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1160824a1566SKashyap Desai  *
1161824a1566SKashyap Desai  * Set Enable IOC bit in IOC configuration register and wait for
1162824a1566SKashyap Desai  * the controller to become ready.
1163824a1566SKashyap Desai  *
116459bd9cfeSSreekanth Reddy  * Return: 0 on success, appropriate error on failure.
1165824a1566SKashyap Desai  */
1166824a1566SKashyap Desai static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
1167824a1566SKashyap Desai {
116859bd9cfeSSreekanth Reddy 	u32 ioc_config, ioc_status, timeout;
116959bd9cfeSSreekanth Reddy 	int retval = 0;
117059bd9cfeSSreekanth Reddy 	enum mpi3mr_iocstate ioc_state;
117159bd9cfeSSreekanth Reddy 	u64 base_info;
1172824a1566SKashyap Desai 
117359bd9cfeSSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
117459bd9cfeSSreekanth Reddy 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
117559bd9cfeSSreekanth Reddy 	base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
117659bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ioc_status(0x%08x), ioc_config(0x%08x), ioc_info(0x%016llx) at the bringup\n",
117759bd9cfeSSreekanth Reddy 	    ioc_status, ioc_config, base_info);
117859bd9cfeSSreekanth Reddy 
117959bd9cfeSSreekanth Reddy 	/*The timeout value is in 2sec unit, changing it to seconds*/
118059bd9cfeSSreekanth Reddy 	mrioc->ready_timeout =
118159bd9cfeSSreekanth Reddy 	    ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
118259bd9cfeSSreekanth Reddy 	    MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
118359bd9cfeSSreekanth Reddy 
118459bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ready timeout: %d seconds\n", mrioc->ready_timeout);
118559bd9cfeSSreekanth Reddy 
118659bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
118759bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "controller is in %s state during detection\n",
118859bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
118959bd9cfeSSreekanth Reddy 
119059bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
119159bd9cfeSSreekanth Reddy 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
119259bd9cfeSSreekanth Reddy 		timeout = mrioc->ready_timeout * 10;
119359bd9cfeSSreekanth Reddy 		do {
119459bd9cfeSSreekanth Reddy 			msleep(100);
119559bd9cfeSSreekanth Reddy 		} while (--timeout);
119659bd9cfeSSreekanth Reddy 
119759bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
119859bd9cfeSSreekanth Reddy 		ioc_info(mrioc,
119959bd9cfeSSreekanth Reddy 		    "controller is in %s state after waiting to reset\n",
120059bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
120159bd9cfeSSreekanth Reddy 	}
120259bd9cfeSSreekanth Reddy 
120359bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_READY) {
120459bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing message unit reset (MUR) to bring to reset state\n");
120559bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_and_process_mur(mrioc,
120659bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
120759bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
120859bd9cfeSSreekanth Reddy 		if (retval)
120959bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
121059bd9cfeSSreekanth Reddy 			    "message unit reset failed with error %d current state %s\n",
121159bd9cfeSSreekanth Reddy 			    retval, mpi3mr_iocstate_name(ioc_state));
121259bd9cfeSSreekanth Reddy 	}
121359bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
121459bd9cfeSSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
121559bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing soft reset to bring to reset state\n");
121659bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_reset(mrioc,
121759bd9cfeSSreekanth Reddy 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
121859bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
121959bd9cfeSSreekanth Reddy 		if (retval) {
122059bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
122159bd9cfeSSreekanth Reddy 			    "soft reset failed with error %d\n", retval);
122259bd9cfeSSreekanth Reddy 			goto out_failed;
122359bd9cfeSSreekanth Reddy 		}
122459bd9cfeSSreekanth Reddy 	}
122559bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
122659bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
122759bd9cfeSSreekanth Reddy 		ioc_err(mrioc,
122859bd9cfeSSreekanth Reddy 		    "cannot bring controller to reset state, current state: %s\n",
122959bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
123059bd9cfeSSreekanth Reddy 		goto out_failed;
123159bd9cfeSSreekanth Reddy 	}
123259bd9cfeSSreekanth Reddy 	mpi3mr_clear_reset_history(mrioc);
123359bd9cfeSSreekanth Reddy 	retval = mpi3mr_setup_admin_qpair(mrioc);
123459bd9cfeSSreekanth Reddy 	if (retval) {
123559bd9cfeSSreekanth Reddy 		ioc_err(mrioc, "failed to setup admin queues: error %d\n",
123659bd9cfeSSreekanth Reddy 		    retval);
123759bd9cfeSSreekanth Reddy 		goto out_failed;
123859bd9cfeSSreekanth Reddy 	}
123959bd9cfeSSreekanth Reddy 
124059bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "bringing controller to ready state\n");
1241824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1242824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1243824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1244824a1566SKashyap Desai 
1245824a1566SKashyap Desai 	timeout = mrioc->ready_timeout * 10;
1246824a1566SKashyap Desai 	do {
124759bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
124859bd9cfeSSreekanth Reddy 		if (ioc_state == MRIOC_STATE_READY) {
124959bd9cfeSSreekanth Reddy 			ioc_info(mrioc,
12505867b856SColin Ian King 			    "successfully transitioned to %s state\n",
125159bd9cfeSSreekanth Reddy 			    mpi3mr_iocstate_name(ioc_state));
1252824a1566SKashyap Desai 			return 0;
125359bd9cfeSSreekanth Reddy 		}
1254824a1566SKashyap Desai 		msleep(100);
1255824a1566SKashyap Desai 	} while (--timeout);
1256824a1566SKashyap Desai 
125759bd9cfeSSreekanth Reddy out_failed:
125859bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
125959bd9cfeSSreekanth Reddy 	ioc_err(mrioc,
126059bd9cfeSSreekanth Reddy 	    "failed to bring to ready state,  current state: %s\n",
126159bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
126259bd9cfeSSreekanth Reddy 	return retval;
1263824a1566SKashyap Desai }
1264824a1566SKashyap Desai 
1265824a1566SKashyap Desai /**
1266f061178eSKashyap Desai  * mpi3mr_soft_reset_success - Check softreset is success or not
1267f061178eSKashyap Desai  * @ioc_status: IOC status register value
1268f061178eSKashyap Desai  * @ioc_config: IOC config register value
1269f061178eSKashyap Desai  *
1270f061178eSKashyap Desai  * Check whether the soft reset is successful or not based on
1271f061178eSKashyap Desai  * IOC status and IOC config register values.
1272f061178eSKashyap Desai  *
1273f061178eSKashyap Desai  * Return: True when the soft reset is success, false otherwise.
1274f061178eSKashyap Desai  */
1275f061178eSKashyap Desai static inline bool
1276f061178eSKashyap Desai mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config)
1277f061178eSKashyap Desai {
1278f061178eSKashyap Desai 	if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1279f061178eSKashyap Desai 	    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1280f061178eSKashyap Desai 		return true;
1281f061178eSKashyap Desai 	return false;
1282f061178eSKashyap Desai }
1283f061178eSKashyap Desai 
1284f061178eSKashyap Desai /**
1285f061178eSKashyap Desai  * mpi3mr_diagfault_success - Check diag fault is success or not
1286f061178eSKashyap Desai  * @mrioc: Adapter reference
1287f061178eSKashyap Desai  * @ioc_status: IOC status register value
1288f061178eSKashyap Desai  *
1289f061178eSKashyap Desai  * Check whether the controller hit diag reset fault code.
1290f061178eSKashyap Desai  *
1291f061178eSKashyap Desai  * Return: True when there is diag fault, false otherwise.
1292f061178eSKashyap Desai  */
1293f061178eSKashyap Desai static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc,
1294f061178eSKashyap Desai 	u32 ioc_status)
1295f061178eSKashyap Desai {
1296f061178eSKashyap Desai 	u32 fault;
1297f061178eSKashyap Desai 
1298f061178eSKashyap Desai 	if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
1299f061178eSKashyap Desai 		return false;
1300f061178eSKashyap Desai 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
1301b64845a7SSreekanth Reddy 	if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) {
1302b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1303f061178eSKashyap Desai 		return true;
1304b64845a7SSreekanth Reddy 	}
1305f061178eSKashyap Desai 	return false;
1306f061178eSKashyap Desai }
1307f061178eSKashyap Desai 
1308f061178eSKashyap Desai /**
1309824a1566SKashyap Desai  * mpi3mr_set_diagsave - Set diag save bit for snapdump
1310824a1566SKashyap Desai  * @mrioc: Adapter reference
1311824a1566SKashyap Desai  *
1312824a1566SKashyap Desai  * Set diag save bit in IOC configuration register to enable
1313824a1566SKashyap Desai  * snapdump.
1314824a1566SKashyap Desai  *
1315824a1566SKashyap Desai  * Return: Nothing.
1316824a1566SKashyap Desai  */
1317824a1566SKashyap Desai static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
1318824a1566SKashyap Desai {
1319824a1566SKashyap Desai 	u32 ioc_config;
1320824a1566SKashyap Desai 
1321824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1322824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
1323824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1324824a1566SKashyap Desai }
1325824a1566SKashyap Desai 
1326824a1566SKashyap Desai /**
1327824a1566SKashyap Desai  * mpi3mr_issue_reset - Issue reset to the controller
1328824a1566SKashyap Desai  * @mrioc: Adapter reference
1329824a1566SKashyap Desai  * @reset_type: Reset type
1330824a1566SKashyap Desai  * @reset_reason: Reset reason code
1331824a1566SKashyap Desai  *
1332f061178eSKashyap Desai  * Unlock the host diagnostic registers and write the specific
1333f061178eSKashyap Desai  * reset type to that, wait for reset acknowledgment from the
1334f061178eSKashyap Desai  * controller, if the reset is not successful retry for the
1335f061178eSKashyap Desai  * predefined number of times.
1336824a1566SKashyap Desai  *
1337824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1338824a1566SKashyap Desai  */
1339824a1566SKashyap Desai static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
1340824a1566SKashyap Desai 	u32 reset_reason)
1341824a1566SKashyap Desai {
1342f061178eSKashyap Desai 	int retval = -1;
1343b64845a7SSreekanth Reddy 	u8 unlock_retry_count = 0;
1344b64845a7SSreekanth Reddy 	u32 host_diagnostic, ioc_status, ioc_config;
1345b64845a7SSreekanth Reddy 	u32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1346f061178eSKashyap Desai 
1347f061178eSKashyap Desai 	if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
1348f061178eSKashyap Desai 	    (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
1349b64845a7SSreekanth Reddy 		return retval;
1350f061178eSKashyap Desai 	if (mrioc->unrecoverable)
1351b64845a7SSreekanth Reddy 		return retval;
1352b64845a7SSreekanth Reddy 	if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) {
1353b64845a7SSreekanth Reddy 		retval = 0;
1354b64845a7SSreekanth Reddy 		return retval;
1355b64845a7SSreekanth Reddy 	}
1356b64845a7SSreekanth Reddy 
1357b64845a7SSreekanth Reddy 	ioc_info(mrioc, "%s reset due to %s(0x%x)\n",
1358b64845a7SSreekanth Reddy 	    mpi3mr_reset_type_name(reset_type),
1359b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason), reset_reason);
1360b64845a7SSreekanth Reddy 
1361f061178eSKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1362f061178eSKashyap Desai 	do {
1363f061178eSKashyap Desai 		ioc_info(mrioc,
1364f061178eSKashyap Desai 		    "Write magic sequence to unlock host diag register (retry=%d)\n",
1365f061178eSKashyap Desai 		    ++unlock_retry_count);
1366f061178eSKashyap Desai 		if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
1367b64845a7SSreekanth Reddy 			ioc_err(mrioc,
1368b64845a7SSreekanth Reddy 			    "%s reset failed due to unlock failure, host_diagnostic(0x%08x)\n",
1369b64845a7SSreekanth Reddy 			    mpi3mr_reset_type_name(reset_type),
1370b64845a7SSreekanth Reddy 			    host_diagnostic);
1371f061178eSKashyap Desai 			mrioc->unrecoverable = 1;
1372b64845a7SSreekanth Reddy 			return retval;
1373f061178eSKashyap Desai 		}
1374f061178eSKashyap Desai 
1375f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH,
1376f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1377f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST,
1378f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1379f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1380f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1381f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD,
1382f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1383f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH,
1384f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1385f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH,
1386f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1387f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH,
1388f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1389f061178eSKashyap Desai 		usleep_range(1000, 1100);
1390f061178eSKashyap Desai 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
1391f061178eSKashyap Desai 		ioc_info(mrioc,
1392f061178eSKashyap Desai 		    "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
1393f061178eSKashyap Desai 		    unlock_retry_count, host_diagnostic);
1394f061178eSKashyap Desai 	} while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
1395f061178eSKashyap Desai 
1396f061178eSKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1397f061178eSKashyap Desai 	writel(host_diagnostic | reset_type,
1398f061178eSKashyap Desai 	    &mrioc->sysif_regs->host_diagnostic);
1399b64845a7SSreekanth Reddy 	switch (reset_type) {
1400b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET:
1401f061178eSKashyap Desai 		do {
1402f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1403f061178eSKashyap Desai 			ioc_config =
1404f061178eSKashyap Desai 			    readl(&mrioc->sysif_regs->ioc_configuration);
1405b64845a7SSreekanth Reddy 			if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1406b64845a7SSreekanth Reddy 			    && mpi3mr_soft_reset_success(ioc_status, ioc_config)
1407b64845a7SSreekanth Reddy 			    ) {
1408b64845a7SSreekanth Reddy 				mpi3mr_clear_reset_history(mrioc);
1409f061178eSKashyap Desai 				retval = 0;
1410f061178eSKashyap Desai 				break;
1411f061178eSKashyap Desai 			}
1412f061178eSKashyap Desai 			msleep(100);
1413f061178eSKashyap Desai 		} while (--timeout);
1414b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1415b64845a7SSreekanth Reddy 		break;
1416b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT:
1417f061178eSKashyap Desai 		do {
1418f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1419f061178eSKashyap Desai 			if (mpi3mr_diagfault_success(mrioc, ioc_status)) {
1420f061178eSKashyap Desai 				retval = 0;
1421f061178eSKashyap Desai 				break;
1422f061178eSKashyap Desai 			}
1423f061178eSKashyap Desai 			msleep(100);
1424f061178eSKashyap Desai 		} while (--timeout);
1425b64845a7SSreekanth Reddy 		break;
1426b64845a7SSreekanth Reddy 	default:
1427b64845a7SSreekanth Reddy 		break;
1428b64845a7SSreekanth Reddy 	}
1429b64845a7SSreekanth Reddy 
1430f061178eSKashyap Desai 	writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1431f061178eSKashyap Desai 	    &mrioc->sysif_regs->write_sequence);
1432f061178eSKashyap Desai 
1433f061178eSKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1434b64845a7SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1435f061178eSKashyap Desai 	ioc_info(mrioc,
1436b64845a7SSreekanth Reddy 	    "ioc_status/ioc_onfig after %s reset is (0x%x)/(0x%x)\n",
1437f061178eSKashyap Desai 	    (!retval)?"successful":"failed", ioc_status,
1438f061178eSKashyap Desai 	    ioc_config);
1439b64845a7SSreekanth Reddy 	if (retval)
1440b64845a7SSreekanth Reddy 		mrioc->unrecoverable = 1;
1441f061178eSKashyap Desai 	return retval;
1442824a1566SKashyap Desai }
1443824a1566SKashyap Desai 
1444824a1566SKashyap Desai /**
1445824a1566SKashyap Desai  * mpi3mr_admin_request_post - Post request to admin queue
1446824a1566SKashyap Desai  * @mrioc: Adapter reference
1447824a1566SKashyap Desai  * @admin_req: MPI3 request
1448824a1566SKashyap Desai  * @admin_req_sz: Request size
1449824a1566SKashyap Desai  * @ignore_reset: Ignore reset in process
1450824a1566SKashyap Desai  *
1451824a1566SKashyap Desai  * Post the MPI3 request into admin request queue and
1452824a1566SKashyap Desai  * inform the controller, if the queue is full return
1453824a1566SKashyap Desai  * appropriate error.
1454824a1566SKashyap Desai  *
1455824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1456824a1566SKashyap Desai  */
1457824a1566SKashyap Desai int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
1458824a1566SKashyap Desai 	u16 admin_req_sz, u8 ignore_reset)
1459824a1566SKashyap Desai {
1460824a1566SKashyap Desai 	u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
1461824a1566SKashyap Desai 	int retval = 0;
1462824a1566SKashyap Desai 	unsigned long flags;
1463824a1566SKashyap Desai 	u8 *areq_entry;
1464824a1566SKashyap Desai 
1465824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1466824a1566SKashyap Desai 		ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
1467824a1566SKashyap Desai 		return -EFAULT;
1468824a1566SKashyap Desai 	}
1469824a1566SKashyap Desai 
1470824a1566SKashyap Desai 	spin_lock_irqsave(&mrioc->admin_req_lock, flags);
1471824a1566SKashyap Desai 	areq_pi = mrioc->admin_req_pi;
1472824a1566SKashyap Desai 	areq_ci = mrioc->admin_req_ci;
1473824a1566SKashyap Desai 	max_entries = mrioc->num_admin_req;
1474824a1566SKashyap Desai 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
1475824a1566SKashyap Desai 	    (areq_pi == (max_entries - 1)))) {
1476824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ full condition detected\n");
1477824a1566SKashyap Desai 		retval = -EAGAIN;
1478824a1566SKashyap Desai 		goto out;
1479824a1566SKashyap Desai 	}
1480824a1566SKashyap Desai 	if (!ignore_reset && mrioc->reset_in_progress) {
1481824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
1482824a1566SKashyap Desai 		retval = -EAGAIN;
1483824a1566SKashyap Desai 		goto out;
1484824a1566SKashyap Desai 	}
1485824a1566SKashyap Desai 	areq_entry = (u8 *)mrioc->admin_req_base +
1486824a1566SKashyap Desai 	    (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
1487824a1566SKashyap Desai 	memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
1488824a1566SKashyap Desai 	memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
1489824a1566SKashyap Desai 
1490824a1566SKashyap Desai 	if (++areq_pi == max_entries)
1491824a1566SKashyap Desai 		areq_pi = 0;
1492824a1566SKashyap Desai 	mrioc->admin_req_pi = areq_pi;
1493824a1566SKashyap Desai 
1494824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
1495824a1566SKashyap Desai 
1496824a1566SKashyap Desai out:
1497824a1566SKashyap Desai 	spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
1498824a1566SKashyap Desai 
1499824a1566SKashyap Desai 	return retval;
1500824a1566SKashyap Desai }
1501824a1566SKashyap Desai 
1502824a1566SKashyap Desai /**
1503c9566231SKashyap Desai  * mpi3mr_free_op_req_q_segments - free request memory segments
1504c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1505c9566231SKashyap Desai  * @q_idx: operational request queue index
1506c9566231SKashyap Desai  *
1507c9566231SKashyap Desai  * Free memory segments allocated for operational request queue
1508c9566231SKashyap Desai  *
1509c9566231SKashyap Desai  * Return: Nothing.
1510c9566231SKashyap Desai  */
1511c9566231SKashyap Desai static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1512c9566231SKashyap Desai {
1513c9566231SKashyap Desai 	u16 j;
1514c9566231SKashyap Desai 	int size;
1515c9566231SKashyap Desai 	struct segments *segments;
1516c9566231SKashyap Desai 
1517c9566231SKashyap Desai 	segments = mrioc->req_qinfo[q_idx].q_segments;
1518c9566231SKashyap Desai 	if (!segments)
1519c9566231SKashyap Desai 		return;
1520c9566231SKashyap Desai 
1521c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1522c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1523c9566231SKashyap Desai 		if (mrioc->req_qinfo[q_idx].q_segment_list) {
1524c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1525c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1526c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list,
1527c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list_dma);
1528d44b5fefSSreekanth Reddy 			mrioc->req_qinfo[q_idx].q_segment_list = NULL;
1529c9566231SKashyap Desai 		}
1530c9566231SKashyap Desai 	} else
1531243bcc8eSSreekanth Reddy 		size = mrioc->req_qinfo[q_idx].segment_qd *
1532c9566231SKashyap Desai 		    mrioc->facts.op_req_sz;
1533c9566231SKashyap Desai 
1534c9566231SKashyap Desai 	for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) {
1535c9566231SKashyap Desai 		if (!segments[j].segment)
1536c9566231SKashyap Desai 			continue;
1537c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1538c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1539c9566231SKashyap Desai 		segments[j].segment = NULL;
1540c9566231SKashyap Desai 	}
1541c9566231SKashyap Desai 	kfree(mrioc->req_qinfo[q_idx].q_segments);
1542c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].q_segments = NULL;
1543c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].qid = 0;
1544c9566231SKashyap Desai }
1545c9566231SKashyap Desai 
1546c9566231SKashyap Desai /**
1547c9566231SKashyap Desai  * mpi3mr_free_op_reply_q_segments - free reply memory segments
1548c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1549c9566231SKashyap Desai  * @q_idx: operational reply queue index
1550c9566231SKashyap Desai  *
1551c9566231SKashyap Desai  * Free memory segments allocated for operational reply queue
1552c9566231SKashyap Desai  *
1553c9566231SKashyap Desai  * Return: Nothing.
1554c9566231SKashyap Desai  */
1555c9566231SKashyap Desai static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1556c9566231SKashyap Desai {
1557c9566231SKashyap Desai 	u16 j;
1558c9566231SKashyap Desai 	int size;
1559c9566231SKashyap Desai 	struct segments *segments;
1560c9566231SKashyap Desai 
1561c9566231SKashyap Desai 	segments = mrioc->op_reply_qinfo[q_idx].q_segments;
1562c9566231SKashyap Desai 	if (!segments)
1563c9566231SKashyap Desai 		return;
1564c9566231SKashyap Desai 
1565c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1566c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1567c9566231SKashyap Desai 		if (mrioc->op_reply_qinfo[q_idx].q_segment_list) {
1568c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1569c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1570c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list,
1571c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list_dma);
1572c9566231SKashyap Desai 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1573c9566231SKashyap Desai 		}
1574c9566231SKashyap Desai 	} else
1575c9566231SKashyap Desai 		size = mrioc->op_reply_qinfo[q_idx].segment_qd *
1576c9566231SKashyap Desai 		    mrioc->op_reply_desc_sz;
1577c9566231SKashyap Desai 
1578c9566231SKashyap Desai 	for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) {
1579c9566231SKashyap Desai 		if (!segments[j].segment)
1580c9566231SKashyap Desai 			continue;
1581c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1582c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1583c9566231SKashyap Desai 		segments[j].segment = NULL;
1584c9566231SKashyap Desai 	}
1585c9566231SKashyap Desai 
1586c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo[q_idx].q_segments);
1587c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].q_segments = NULL;
1588c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].qid = 0;
1589c9566231SKashyap Desai }
1590c9566231SKashyap Desai 
1591c9566231SKashyap Desai /**
1592c9566231SKashyap Desai  * mpi3mr_delete_op_reply_q - delete operational reply queue
1593c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1594c9566231SKashyap Desai  * @qidx: operational reply queue index
1595c9566231SKashyap Desai  *
1596c9566231SKashyap Desai  * Delete operatinal reply queue by issuing MPI request
1597c9566231SKashyap Desai  * through admin queue.
1598c9566231SKashyap Desai  *
1599c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1600c9566231SKashyap Desai  */
1601c9566231SKashyap Desai static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1602c9566231SKashyap Desai {
1603c9566231SKashyap Desai 	struct mpi3_delete_reply_queue_request delq_req;
1604afd3a579SSreekanth Reddy 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1605c9566231SKashyap Desai 	int retval = 0;
1606c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1607c9566231SKashyap Desai 
1608afd3a579SSreekanth Reddy 	reply_qid = op_reply_q->qid;
1609c9566231SKashyap Desai 
1610c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1611c9566231SKashyap Desai 
1612c9566231SKashyap Desai 	if (!reply_qid)	{
1613c9566231SKashyap Desai 		retval = -1;
1614c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n");
1615c9566231SKashyap Desai 		goto out;
1616c9566231SKashyap Desai 	}
1617c9566231SKashyap Desai 
1618afd3a579SSreekanth Reddy 	(op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount-- :
1619afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount--;
1620afd3a579SSreekanth Reddy 
1621c9566231SKashyap Desai 	memset(&delq_req, 0, sizeof(delq_req));
1622c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1623c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1624c9566231SKashyap Desai 		retval = -1;
1625c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n");
1626c9566231SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
1627c9566231SKashyap Desai 		goto out;
1628c9566231SKashyap Desai 	}
1629c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1630c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1631c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1632c9566231SKashyap Desai 	delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1633c9566231SKashyap Desai 	delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
1634c9566231SKashyap Desai 	delq_req.queue_id = cpu_to_le16(reply_qid);
1635c9566231SKashyap Desai 
1636c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1637c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req),
1638c9566231SKashyap Desai 	    1);
1639c9566231SKashyap Desai 	if (retval) {
1640c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n");
1641c9566231SKashyap Desai 		goto out_unlock;
1642c9566231SKashyap Desai 	}
1643c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1644c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1645c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1646a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "delete reply queue timed out\n");
1647a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1648c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
1649c9566231SKashyap Desai 		retval = -1;
1650c9566231SKashyap Desai 		goto out_unlock;
1651c9566231SKashyap Desai 	}
1652c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1653c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1654c9566231SKashyap Desai 		ioc_err(mrioc,
1655c9566231SKashyap Desai 		    "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1656c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1657c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1658c9566231SKashyap Desai 		retval = -1;
1659c9566231SKashyap Desai 		goto out_unlock;
1660c9566231SKashyap Desai 	}
1661c9566231SKashyap Desai 	mrioc->intr_info[midx].op_reply_q = NULL;
1662c9566231SKashyap Desai 
1663c9566231SKashyap Desai 	mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1664c9566231SKashyap Desai out_unlock:
1665c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1666c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1667c9566231SKashyap Desai out:
1668c9566231SKashyap Desai 
1669c9566231SKashyap Desai 	return retval;
1670c9566231SKashyap Desai }
1671c9566231SKashyap Desai 
1672c9566231SKashyap Desai /**
1673c9566231SKashyap Desai  * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool
1674c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1675c9566231SKashyap Desai  * @qidx: request queue index
1676c9566231SKashyap Desai  *
1677c9566231SKashyap Desai  * Allocate segmented memory pools for operational reply
1678c9566231SKashyap Desai  * queue.
1679c9566231SKashyap Desai  *
1680c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1681c9566231SKashyap Desai  */
1682c9566231SKashyap Desai static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1683c9566231SKashyap Desai {
1684c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1685c9566231SKashyap Desai 	int i, size;
1686c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1687c9566231SKashyap Desai 	struct segments *segments;
1688c9566231SKashyap Desai 
1689c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1690c9566231SKashyap Desai 		op_reply_q->segment_qd =
1691c9566231SKashyap Desai 		    MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz;
1692c9566231SKashyap Desai 
1693c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1694c9566231SKashyap Desai 
1695c9566231SKashyap Desai 		op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1696c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma,
1697c9566231SKashyap Desai 		    GFP_KERNEL);
1698c9566231SKashyap Desai 		if (!op_reply_q->q_segment_list)
1699c9566231SKashyap Desai 			return -ENOMEM;
1700c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_reply_q->q_segment_list;
1701c9566231SKashyap Desai 	} else {
1702c9566231SKashyap Desai 		op_reply_q->segment_qd = op_reply_q->num_replies;
1703c9566231SKashyap Desai 		size = op_reply_q->num_replies * mrioc->op_reply_desc_sz;
1704c9566231SKashyap Desai 	}
1705c9566231SKashyap Desai 
1706c9566231SKashyap Desai 	op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies,
1707c9566231SKashyap Desai 	    op_reply_q->segment_qd);
1708c9566231SKashyap Desai 
1709c9566231SKashyap Desai 	op_reply_q->q_segments = kcalloc(op_reply_q->num_segments,
1710c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1711c9566231SKashyap Desai 	if (!op_reply_q->q_segments)
1712c9566231SKashyap Desai 		return -ENOMEM;
1713c9566231SKashyap Desai 
1714c9566231SKashyap Desai 	segments = op_reply_q->q_segments;
1715c9566231SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++) {
1716c9566231SKashyap Desai 		segments[i].segment =
1717c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1718c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1719c9566231SKashyap Desai 		if (!segments[i].segment)
1720c9566231SKashyap Desai 			return -ENOMEM;
1721c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1722c9566231SKashyap Desai 			q_segment_list_entry[i] =
1723c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1724c9566231SKashyap Desai 	}
1725c9566231SKashyap Desai 
1726c9566231SKashyap Desai 	return 0;
1727c9566231SKashyap Desai }
1728c9566231SKashyap Desai 
1729c9566231SKashyap Desai /**
1730c9566231SKashyap Desai  * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool.
1731c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1732c9566231SKashyap Desai  * @qidx: request queue index
1733c9566231SKashyap Desai  *
1734c9566231SKashyap Desai  * Allocate segmented memory pools for operational request
1735c9566231SKashyap Desai  * queue.
1736c9566231SKashyap Desai  *
1737c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1738c9566231SKashyap Desai  */
1739c9566231SKashyap Desai static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1740c9566231SKashyap Desai {
1741c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
1742c9566231SKashyap Desai 	int i, size;
1743c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1744c9566231SKashyap Desai 	struct segments *segments;
1745c9566231SKashyap Desai 
1746c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1747c9566231SKashyap Desai 		op_req_q->segment_qd =
1748c9566231SKashyap Desai 		    MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz;
1749c9566231SKashyap Desai 
1750c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1751c9566231SKashyap Desai 
1752c9566231SKashyap Desai 		op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1753c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma,
1754c9566231SKashyap Desai 		    GFP_KERNEL);
1755c9566231SKashyap Desai 		if (!op_req_q->q_segment_list)
1756c9566231SKashyap Desai 			return -ENOMEM;
1757c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_req_q->q_segment_list;
1758c9566231SKashyap Desai 
1759c9566231SKashyap Desai 	} else {
1760c9566231SKashyap Desai 		op_req_q->segment_qd = op_req_q->num_requests;
1761c9566231SKashyap Desai 		size = op_req_q->num_requests * mrioc->facts.op_req_sz;
1762c9566231SKashyap Desai 	}
1763c9566231SKashyap Desai 
1764c9566231SKashyap Desai 	op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests,
1765c9566231SKashyap Desai 	    op_req_q->segment_qd);
1766c9566231SKashyap Desai 
1767c9566231SKashyap Desai 	op_req_q->q_segments = kcalloc(op_req_q->num_segments,
1768c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1769c9566231SKashyap Desai 	if (!op_req_q->q_segments)
1770c9566231SKashyap Desai 		return -ENOMEM;
1771c9566231SKashyap Desai 
1772c9566231SKashyap Desai 	segments = op_req_q->q_segments;
1773c9566231SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++) {
1774c9566231SKashyap Desai 		segments[i].segment =
1775c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1776c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1777c9566231SKashyap Desai 		if (!segments[i].segment)
1778c9566231SKashyap Desai 			return -ENOMEM;
1779c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1780c9566231SKashyap Desai 			q_segment_list_entry[i] =
1781c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1782c9566231SKashyap Desai 	}
1783c9566231SKashyap Desai 
1784c9566231SKashyap Desai 	return 0;
1785c9566231SKashyap Desai }
1786c9566231SKashyap Desai 
1787c9566231SKashyap Desai /**
1788c9566231SKashyap Desai  * mpi3mr_create_op_reply_q - create operational reply queue
1789c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1790c9566231SKashyap Desai  * @qidx: operational reply queue index
1791c9566231SKashyap Desai  *
1792c9566231SKashyap Desai  * Create operatinal reply queue by issuing MPI request
1793c9566231SKashyap Desai  * through admin queue.
1794c9566231SKashyap Desai  *
1795c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1796c9566231SKashyap Desai  */
1797c9566231SKashyap Desai static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1798c9566231SKashyap Desai {
1799c9566231SKashyap Desai 	struct mpi3_create_reply_queue_request create_req;
1800c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1801c9566231SKashyap Desai 	int retval = 0;
1802c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1803c9566231SKashyap Desai 
1804c9566231SKashyap Desai 	reply_qid = op_reply_q->qid;
1805c9566231SKashyap Desai 
1806c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1807c9566231SKashyap Desai 
1808c9566231SKashyap Desai 	if (reply_qid) {
1809c9566231SKashyap Desai 		retval = -1;
1810c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n",
1811c9566231SKashyap Desai 		    reply_qid);
1812c9566231SKashyap Desai 
1813c9566231SKashyap Desai 		return retval;
1814c9566231SKashyap Desai 	}
1815c9566231SKashyap Desai 
1816c9566231SKashyap Desai 	reply_qid = qidx + 1;
1817c9566231SKashyap Desai 	op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
1818243bcc8eSSreekanth Reddy 	if (!mrioc->pdev->revision)
1819243bcc8eSSreekanth Reddy 		op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD4K;
1820c9566231SKashyap Desai 	op_reply_q->ci = 0;
1821c9566231SKashyap Desai 	op_reply_q->ephase = 1;
1822463429f8SKashyap Desai 	atomic_set(&op_reply_q->pend_ios, 0);
1823463429f8SKashyap Desai 	atomic_set(&op_reply_q->in_use, 0);
1824463429f8SKashyap Desai 	op_reply_q->enable_irq_poll = false;
1825c9566231SKashyap Desai 
1826c9566231SKashyap Desai 	if (!op_reply_q->q_segments) {
1827c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx);
1828c9566231SKashyap Desai 		if (retval) {
1829c9566231SKashyap Desai 			mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1830c9566231SKashyap Desai 			goto out;
1831c9566231SKashyap Desai 		}
1832c9566231SKashyap Desai 	}
1833c9566231SKashyap Desai 
1834c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1835c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1836c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1837c9566231SKashyap Desai 		retval = -1;
1838c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Init command is in use\n");
1839f9dc034dSYang Yingliang 		goto out_unlock;
1840c9566231SKashyap Desai 	}
1841c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1842c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1843c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1844c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1845c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
1846c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(reply_qid);
1847afd3a579SSreekanth Reddy 
1848afd3a579SSreekanth Reddy 	if (midx < (mrioc->intr_info_count - mrioc->requested_poll_qcount))
1849afd3a579SSreekanth Reddy 		op_reply_q->qtype = MPI3MR_DEFAULT_QUEUE;
1850afd3a579SSreekanth Reddy 	else
1851afd3a579SSreekanth Reddy 		op_reply_q->qtype = MPI3MR_POLL_QUEUE;
1852afd3a579SSreekanth Reddy 
1853afd3a579SSreekanth Reddy 	if (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) {
1854afd3a579SSreekanth Reddy 		create_req.flags =
1855afd3a579SSreekanth Reddy 			MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
1856afd3a579SSreekanth Reddy 		create_req.msix_index =
1857afd3a579SSreekanth Reddy 			cpu_to_le16(mrioc->intr_info[midx].msix_index);
1858afd3a579SSreekanth Reddy 	} else {
1859afd3a579SSreekanth Reddy 		create_req.msix_index = cpu_to_le16(mrioc->intr_info_count - 1);
1860afd3a579SSreekanth Reddy 		ioc_info(mrioc, "create reply queue(polled): for qid(%d), midx(%d)\n",
1861afd3a579SSreekanth Reddy 			reply_qid, midx);
1862afd3a579SSreekanth Reddy 		if (!mrioc->active_poll_qcount)
1863afd3a579SSreekanth Reddy 			disable_irq_nosync(pci_irq_vector(mrioc->pdev,
1864afd3a579SSreekanth Reddy 			    mrioc->intr_info_count - 1));
1865afd3a579SSreekanth Reddy 	}
1866afd3a579SSreekanth Reddy 
1867c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1868c9566231SKashyap Desai 		create_req.flags |=
1869c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1870c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1871c9566231SKashyap Desai 		    op_reply_q->q_segment_list_dma);
1872c9566231SKashyap Desai 	} else
1873c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1874c9566231SKashyap Desai 		    op_reply_q->q_segments[0].segment_dma);
1875c9566231SKashyap Desai 
1876c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_reply_q->num_replies);
1877c9566231SKashyap Desai 
1878c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1879c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1880c9566231SKashyap Desai 	    sizeof(create_req), 1);
1881c9566231SKashyap Desai 	if (retval) {
1882c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Admin Post failed\n");
1883c9566231SKashyap Desai 		goto out_unlock;
1884c9566231SKashyap Desai 	}
1885c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1886c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1887c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1888a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create reply queue timed out\n");
1889a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1890c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
1891c9566231SKashyap Desai 		retval = -1;
1892c9566231SKashyap Desai 		goto out_unlock;
1893c9566231SKashyap Desai 	}
1894c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1895c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1896c9566231SKashyap Desai 		ioc_err(mrioc,
1897c9566231SKashyap Desai 		    "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1898c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1899c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1900c9566231SKashyap Desai 		retval = -1;
1901c9566231SKashyap Desai 		goto out_unlock;
1902c9566231SKashyap Desai 	}
1903c9566231SKashyap Desai 	op_reply_q->qid = reply_qid;
1904fe6db615SSreekanth Reddy 	if (midx < mrioc->intr_info_count)
1905c9566231SKashyap Desai 		mrioc->intr_info[midx].op_reply_q = op_reply_q;
1906c9566231SKashyap Desai 
1907afd3a579SSreekanth Reddy 	(op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount++ :
1908afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount++;
1909afd3a579SSreekanth Reddy 
1910c9566231SKashyap Desai out_unlock:
1911c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1912c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1913c9566231SKashyap Desai out:
1914c9566231SKashyap Desai 
1915c9566231SKashyap Desai 	return retval;
1916c9566231SKashyap Desai }
1917c9566231SKashyap Desai 
1918c9566231SKashyap Desai /**
1919c9566231SKashyap Desai  * mpi3mr_create_op_req_q - create operational request queue
1920c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1921c9566231SKashyap Desai  * @idx: operational request queue index
1922c9566231SKashyap Desai  * @reply_qid: Reply queue ID
1923c9566231SKashyap Desai  *
1924c9566231SKashyap Desai  * Create operatinal request queue by issuing MPI request
1925c9566231SKashyap Desai  * through admin queue.
1926c9566231SKashyap Desai  *
1927c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1928c9566231SKashyap Desai  */
1929c9566231SKashyap Desai static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx,
1930c9566231SKashyap Desai 	u16 reply_qid)
1931c9566231SKashyap Desai {
1932c9566231SKashyap Desai 	struct mpi3_create_request_queue_request create_req;
1933c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx;
1934c9566231SKashyap Desai 	int retval = 0;
1935c9566231SKashyap Desai 	u16 req_qid = 0;
1936c9566231SKashyap Desai 
1937c9566231SKashyap Desai 	req_qid = op_req_q->qid;
1938c9566231SKashyap Desai 
1939c9566231SKashyap Desai 	if (req_qid) {
1940c9566231SKashyap Desai 		retval = -1;
1941c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n",
1942c9566231SKashyap Desai 		    req_qid);
1943c9566231SKashyap Desai 
1944c9566231SKashyap Desai 		return retval;
1945c9566231SKashyap Desai 	}
1946c9566231SKashyap Desai 	req_qid = idx + 1;
1947c9566231SKashyap Desai 
1948c9566231SKashyap Desai 	op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD;
1949c9566231SKashyap Desai 	op_req_q->ci = 0;
1950c9566231SKashyap Desai 	op_req_q->pi = 0;
1951c9566231SKashyap Desai 	op_req_q->reply_qid = reply_qid;
1952c9566231SKashyap Desai 	spin_lock_init(&op_req_q->q_lock);
1953c9566231SKashyap Desai 
1954c9566231SKashyap Desai 	if (!op_req_q->q_segments) {
1955c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx);
1956c9566231SKashyap Desai 		if (retval) {
1957c9566231SKashyap Desai 			mpi3mr_free_op_req_q_segments(mrioc, idx);
1958c9566231SKashyap Desai 			goto out;
1959c9566231SKashyap Desai 		}
1960c9566231SKashyap Desai 	}
1961c9566231SKashyap Desai 
1962c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1963c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1964c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1965c9566231SKashyap Desai 		retval = -1;
1966c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Init command is in use\n");
1967f9dc034dSYang Yingliang 		goto out_unlock;
1968c9566231SKashyap Desai 	}
1969c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1970c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1971c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1972c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1973c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
1974c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(req_qid);
1975c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1976c9566231SKashyap Desai 		create_req.flags =
1977c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1978c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1979c9566231SKashyap Desai 		    op_req_q->q_segment_list_dma);
1980c9566231SKashyap Desai 	} else
1981c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1982c9566231SKashyap Desai 		    op_req_q->q_segments[0].segment_dma);
1983c9566231SKashyap Desai 	create_req.reply_queue_id = cpu_to_le16(reply_qid);
1984c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_req_q->num_requests);
1985c9566231SKashyap Desai 
1986c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1987c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1988c9566231SKashyap Desai 	    sizeof(create_req), 1);
1989c9566231SKashyap Desai 	if (retval) {
1990c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Admin Post failed\n");
1991c9566231SKashyap Desai 		goto out_unlock;
1992c9566231SKashyap Desai 	}
1993c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1994c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1995c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1996a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create request queue timed out\n");
1997a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1998a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
1999c9566231SKashyap Desai 		retval = -1;
2000c9566231SKashyap Desai 		goto out_unlock;
2001c9566231SKashyap Desai 	}
2002c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2003c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2004c9566231SKashyap Desai 		ioc_err(mrioc,
2005c9566231SKashyap Desai 		    "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2006c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2007c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2008c9566231SKashyap Desai 		retval = -1;
2009c9566231SKashyap Desai 		goto out_unlock;
2010c9566231SKashyap Desai 	}
2011c9566231SKashyap Desai 	op_req_q->qid = req_qid;
2012c9566231SKashyap Desai 
2013c9566231SKashyap Desai out_unlock:
2014c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2015c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2016c9566231SKashyap Desai out:
2017c9566231SKashyap Desai 
2018c9566231SKashyap Desai 	return retval;
2019c9566231SKashyap Desai }
2020c9566231SKashyap Desai 
2021c9566231SKashyap Desai /**
2022c9566231SKashyap Desai  * mpi3mr_create_op_queues - create operational queue pairs
2023c9566231SKashyap Desai  * @mrioc: Adapter instance reference
2024c9566231SKashyap Desai  *
2025c9566231SKashyap Desai  * Allocate memory for operational queue meta data and call
2026c9566231SKashyap Desai  * create request and reply queue functions.
2027c9566231SKashyap Desai  *
2028c9566231SKashyap Desai  * Return: 0 on success, non-zero on failures.
2029c9566231SKashyap Desai  */
2030c9566231SKashyap Desai static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc)
2031c9566231SKashyap Desai {
2032c9566231SKashyap Desai 	int retval = 0;
2033c9566231SKashyap Desai 	u16 num_queues = 0, i = 0, msix_count_op_q = 1;
2034c9566231SKashyap Desai 
2035c9566231SKashyap Desai 	num_queues = min_t(int, mrioc->facts.max_op_reply_q,
2036c9566231SKashyap Desai 	    mrioc->facts.max_op_req_q);
2037c9566231SKashyap Desai 
2038c9566231SKashyap Desai 	msix_count_op_q =
2039c9566231SKashyap Desai 	    mrioc->intr_info_count - mrioc->op_reply_q_offset;
2040c9566231SKashyap Desai 	if (!mrioc->num_queues)
2041c9566231SKashyap Desai 		mrioc->num_queues = min_t(int, num_queues, msix_count_op_q);
2042c5758fc7SSreekanth Reddy 	/*
2043c5758fc7SSreekanth Reddy 	 * During reset set the num_queues to the number of queues
2044c5758fc7SSreekanth Reddy 	 * that was set before the reset.
2045c5758fc7SSreekanth Reddy 	 */
2046c5758fc7SSreekanth Reddy 	num_queues = mrioc->num_op_reply_q ?
2047c5758fc7SSreekanth Reddy 	    mrioc->num_op_reply_q : mrioc->num_queues;
2048c5758fc7SSreekanth Reddy 	ioc_info(mrioc, "trying to create %d operational queue pairs\n",
2049c9566231SKashyap Desai 	    num_queues);
2050c9566231SKashyap Desai 
2051c9566231SKashyap Desai 	if (!mrioc->req_qinfo) {
2052c9566231SKashyap Desai 		mrioc->req_qinfo = kcalloc(num_queues,
2053c9566231SKashyap Desai 		    sizeof(struct op_req_qinfo), GFP_KERNEL);
2054c9566231SKashyap Desai 		if (!mrioc->req_qinfo) {
2055c9566231SKashyap Desai 			retval = -1;
2056c9566231SKashyap Desai 			goto out_failed;
2057c9566231SKashyap Desai 		}
2058c9566231SKashyap Desai 
2059c9566231SKashyap Desai 		mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) *
2060c9566231SKashyap Desai 		    num_queues, GFP_KERNEL);
2061c9566231SKashyap Desai 		if (!mrioc->op_reply_qinfo) {
2062c9566231SKashyap Desai 			retval = -1;
2063c9566231SKashyap Desai 			goto out_failed;
2064c9566231SKashyap Desai 		}
2065c9566231SKashyap Desai 	}
2066c9566231SKashyap Desai 
2067c9566231SKashyap Desai 	if (mrioc->enable_segqueue)
2068c9566231SKashyap Desai 		ioc_info(mrioc,
2069c9566231SKashyap Desai 		    "allocating operational queues through segmented queues\n");
2070c9566231SKashyap Desai 
2071c9566231SKashyap Desai 	for (i = 0; i < num_queues; i++) {
2072c9566231SKashyap Desai 		if (mpi3mr_create_op_reply_q(mrioc, i)) {
2073c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP RepQ %d\n", i);
2074c9566231SKashyap Desai 			break;
2075c9566231SKashyap Desai 		}
2076c9566231SKashyap Desai 		if (mpi3mr_create_op_req_q(mrioc, i,
2077c9566231SKashyap Desai 		    mrioc->op_reply_qinfo[i].qid)) {
2078c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i);
2079c9566231SKashyap Desai 			mpi3mr_delete_op_reply_q(mrioc, i);
2080c9566231SKashyap Desai 			break;
2081c9566231SKashyap Desai 		}
2082c9566231SKashyap Desai 	}
2083c9566231SKashyap Desai 
2084c9566231SKashyap Desai 	if (i == 0) {
2085c9566231SKashyap Desai 		/* Not even one queue is created successfully*/
2086c9566231SKashyap Desai 		retval = -1;
2087c9566231SKashyap Desai 		goto out_failed;
2088c9566231SKashyap Desai 	}
2089c9566231SKashyap Desai 	mrioc->num_op_reply_q = mrioc->num_op_req_q = i;
2090afd3a579SSreekanth Reddy 	ioc_info(mrioc,
2091afd3a579SSreekanth Reddy 	    "successfully created %d operational queue pairs(default/polled) queue = (%d/%d)\n",
2092afd3a579SSreekanth Reddy 	    mrioc->num_op_reply_q, mrioc->default_qcount,
2093afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount);
2094c9566231SKashyap Desai 
2095c9566231SKashyap Desai 	return retval;
2096c9566231SKashyap Desai out_failed:
2097c9566231SKashyap Desai 	kfree(mrioc->req_qinfo);
2098c9566231SKashyap Desai 	mrioc->req_qinfo = NULL;
2099c9566231SKashyap Desai 
2100c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
2101c9566231SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
2102c9566231SKashyap Desai 
2103c9566231SKashyap Desai 	return retval;
2104c9566231SKashyap Desai }
2105c9566231SKashyap Desai 
2106c9566231SKashyap Desai /**
2107023ab2a9SKashyap Desai  * mpi3mr_op_request_post - Post request to operational queue
2108023ab2a9SKashyap Desai  * @mrioc: Adapter reference
2109023ab2a9SKashyap Desai  * @op_req_q: Operational request queue info
2110023ab2a9SKashyap Desai  * @req: MPI3 request
2111023ab2a9SKashyap Desai  *
2112023ab2a9SKashyap Desai  * Post the MPI3 request into operational request queue and
2113023ab2a9SKashyap Desai  * inform the controller, if the queue is full return
2114023ab2a9SKashyap Desai  * appropriate error.
2115023ab2a9SKashyap Desai  *
2116023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failure.
2117023ab2a9SKashyap Desai  */
2118023ab2a9SKashyap Desai int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc,
2119023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q, u8 *req)
2120023ab2a9SKashyap Desai {
2121023ab2a9SKashyap Desai 	u16 pi = 0, max_entries, reply_qidx = 0, midx;
2122023ab2a9SKashyap Desai 	int retval = 0;
2123023ab2a9SKashyap Desai 	unsigned long flags;
2124023ab2a9SKashyap Desai 	u8 *req_entry;
2125023ab2a9SKashyap Desai 	void *segment_base_addr;
2126023ab2a9SKashyap Desai 	u16 req_sz = mrioc->facts.op_req_sz;
2127023ab2a9SKashyap Desai 	struct segments *segments = op_req_q->q_segments;
2128023ab2a9SKashyap Desai 
2129023ab2a9SKashyap Desai 	reply_qidx = op_req_q->reply_qid - 1;
2130023ab2a9SKashyap Desai 
2131023ab2a9SKashyap Desai 	if (mrioc->unrecoverable)
2132023ab2a9SKashyap Desai 		return -EFAULT;
2133023ab2a9SKashyap Desai 
2134023ab2a9SKashyap Desai 	spin_lock_irqsave(&op_req_q->q_lock, flags);
2135023ab2a9SKashyap Desai 	pi = op_req_q->pi;
2136023ab2a9SKashyap Desai 	max_entries = op_req_q->num_requests;
2137023ab2a9SKashyap Desai 
2138023ab2a9SKashyap Desai 	if (mpi3mr_check_req_qfull(op_req_q)) {
2139023ab2a9SKashyap Desai 		midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(
2140023ab2a9SKashyap Desai 		    reply_qidx, mrioc->op_reply_q_offset);
2141afd3a579SSreekanth Reddy 		mpi3mr_process_op_reply_q(mrioc, mrioc->intr_info[midx].op_reply_q);
2142023ab2a9SKashyap Desai 
2143023ab2a9SKashyap Desai 		if (mpi3mr_check_req_qfull(op_req_q)) {
2144023ab2a9SKashyap Desai 			retval = -EAGAIN;
2145023ab2a9SKashyap Desai 			goto out;
2146023ab2a9SKashyap Desai 		}
2147023ab2a9SKashyap Desai 	}
2148023ab2a9SKashyap Desai 
2149023ab2a9SKashyap Desai 	if (mrioc->reset_in_progress) {
2150023ab2a9SKashyap Desai 		ioc_err(mrioc, "OpReqQ submit reset in progress\n");
2151023ab2a9SKashyap Desai 		retval = -EAGAIN;
2152023ab2a9SKashyap Desai 		goto out;
2153023ab2a9SKashyap Desai 	}
2154023ab2a9SKashyap Desai 
2155023ab2a9SKashyap Desai 	segment_base_addr = segments[pi / op_req_q->segment_qd].segment;
2156023ab2a9SKashyap Desai 	req_entry = (u8 *)segment_base_addr +
2157023ab2a9SKashyap Desai 	    ((pi % op_req_q->segment_qd) * req_sz);
2158023ab2a9SKashyap Desai 
2159023ab2a9SKashyap Desai 	memset(req_entry, 0, req_sz);
2160023ab2a9SKashyap Desai 	memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ);
2161023ab2a9SKashyap Desai 
2162023ab2a9SKashyap Desai 	if (++pi == max_entries)
2163023ab2a9SKashyap Desai 		pi = 0;
2164023ab2a9SKashyap Desai 	op_req_q->pi = pi;
2165023ab2a9SKashyap Desai 
2166463429f8SKashyap Desai 	if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios)
2167463429f8SKashyap Desai 	    > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT)
2168463429f8SKashyap Desai 		mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true;
2169463429f8SKashyap Desai 
2170023ab2a9SKashyap Desai 	writel(op_req_q->pi,
2171023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index);
2172023ab2a9SKashyap Desai 
2173023ab2a9SKashyap Desai out:
2174023ab2a9SKashyap Desai 	spin_unlock_irqrestore(&op_req_q->q_lock, flags);
2175023ab2a9SKashyap Desai 	return retval;
2176023ab2a9SKashyap Desai }
2177023ab2a9SKashyap Desai 
2178023ab2a9SKashyap Desai /**
2179a6856cc4SSreekanth Reddy  * mpi3mr_check_rh_fault_ioc - check reset history and fault
2180a6856cc4SSreekanth Reddy  * controller
2181a6856cc4SSreekanth Reddy  * @mrioc: Adapter instance reference
21823bb3c24eSYang Li  * @reason_code: reason code for the fault.
2183a6856cc4SSreekanth Reddy  *
2184a6856cc4SSreekanth Reddy  * This routine will save snapdump and fault the controller with
2185a6856cc4SSreekanth Reddy  * the given reason code if it is not already in the fault or
2186a6856cc4SSreekanth Reddy  * not asynchronosuly reset. This will be used to handle
2187a6856cc4SSreekanth Reddy  * initilaization time faults/resets/timeout as in those cases
2188a6856cc4SSreekanth Reddy  * immediate soft reset invocation is not required.
2189a6856cc4SSreekanth Reddy  *
2190a6856cc4SSreekanth Reddy  * Return:  None.
2191a6856cc4SSreekanth Reddy  */
2192a6856cc4SSreekanth Reddy void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code)
2193a6856cc4SSreekanth Reddy {
2194a6856cc4SSreekanth Reddy 	u32 ioc_status, host_diagnostic, timeout;
2195a6856cc4SSreekanth Reddy 
2196a6856cc4SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
2197a6856cc4SSreekanth Reddy 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
2198a6856cc4SSreekanth Reddy 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
2199a6856cc4SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
2200a6856cc4SSreekanth Reddy 		return;
2201a6856cc4SSreekanth Reddy 	}
2202a6856cc4SSreekanth Reddy 	mpi3mr_set_diagsave(mrioc);
2203a6856cc4SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
2204a6856cc4SSreekanth Reddy 	    reason_code);
2205a6856cc4SSreekanth Reddy 	timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
2206a6856cc4SSreekanth Reddy 	do {
2207a6856cc4SSreekanth Reddy 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2208a6856cc4SSreekanth Reddy 		if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
2209a6856cc4SSreekanth Reddy 			break;
2210a6856cc4SSreekanth Reddy 		msleep(100);
2211a6856cc4SSreekanth Reddy 	} while (--timeout);
2212a6856cc4SSreekanth Reddy }
2213a6856cc4SSreekanth Reddy 
2214a6856cc4SSreekanth Reddy /**
221554dfcffbSKashyap Desai  * mpi3mr_sync_timestamp - Issue time stamp sync request
221654dfcffbSKashyap Desai  * @mrioc: Adapter reference
221754dfcffbSKashyap Desai  *
221854dfcffbSKashyap Desai  * Issue IO unit control MPI request to synchornize firmware
221954dfcffbSKashyap Desai  * timestamp with host time.
222054dfcffbSKashyap Desai  *
222154dfcffbSKashyap Desai  * Return: 0 on success, non-zero on failure.
222254dfcffbSKashyap Desai  */
222354dfcffbSKashyap Desai static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc)
222454dfcffbSKashyap Desai {
222554dfcffbSKashyap Desai 	ktime_t current_time;
222654dfcffbSKashyap Desai 	struct mpi3_iounit_control_request iou_ctrl;
222754dfcffbSKashyap Desai 	int retval = 0;
222854dfcffbSKashyap Desai 
222954dfcffbSKashyap Desai 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
223054dfcffbSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
223154dfcffbSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
223254dfcffbSKashyap Desai 		retval = -1;
223354dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n");
223454dfcffbSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
223554dfcffbSKashyap Desai 		goto out;
223654dfcffbSKashyap Desai 	}
223754dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
223854dfcffbSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
223954dfcffbSKashyap Desai 	mrioc->init_cmds.callback = NULL;
224054dfcffbSKashyap Desai 	iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
224154dfcffbSKashyap Desai 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
224254dfcffbSKashyap Desai 	iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP;
224354dfcffbSKashyap Desai 	current_time = ktime_get_real();
224454dfcffbSKashyap Desai 	iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time));
224554dfcffbSKashyap Desai 
224654dfcffbSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
224754dfcffbSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl,
224854dfcffbSKashyap Desai 	    sizeof(iou_ctrl), 0);
224954dfcffbSKashyap Desai 	if (retval) {
225054dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n");
225154dfcffbSKashyap Desai 		goto out_unlock;
225254dfcffbSKashyap Desai 	}
225354dfcffbSKashyap Desai 
225454dfcffbSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
225554dfcffbSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
225654dfcffbSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
225754dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n");
225854dfcffbSKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
2259fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
226054dfcffbSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
226154dfcffbSKashyap Desai 			    MPI3MR_RESET_FROM_TSU_TIMEOUT, 1);
226254dfcffbSKashyap Desai 		retval = -1;
226354dfcffbSKashyap Desai 		goto out_unlock;
226454dfcffbSKashyap Desai 	}
226554dfcffbSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
226654dfcffbSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
226754dfcffbSKashyap Desai 		ioc_err(mrioc,
226854dfcffbSKashyap Desai 		    "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
226954dfcffbSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
227054dfcffbSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
227154dfcffbSKashyap Desai 		retval = -1;
227254dfcffbSKashyap Desai 		goto out_unlock;
227354dfcffbSKashyap Desai 	}
227454dfcffbSKashyap Desai 
227554dfcffbSKashyap Desai out_unlock:
227654dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
227754dfcffbSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
227854dfcffbSKashyap Desai 
227954dfcffbSKashyap Desai out:
228054dfcffbSKashyap Desai 	return retval;
228154dfcffbSKashyap Desai }
228254dfcffbSKashyap Desai 
228354dfcffbSKashyap Desai /**
22842ac794baSSreekanth Reddy  * mpi3mr_print_pkg_ver - display controller fw package version
22852ac794baSSreekanth Reddy  * @mrioc: Adapter reference
22862ac794baSSreekanth Reddy  *
22872ac794baSSreekanth Reddy  * Retrieve firmware package version from the component image
22882ac794baSSreekanth Reddy  * header of the controller flash and display it.
22892ac794baSSreekanth Reddy  *
22902ac794baSSreekanth Reddy  * Return: 0 on success and non-zero on failure.
22912ac794baSSreekanth Reddy  */
22922ac794baSSreekanth Reddy static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc)
22932ac794baSSreekanth Reddy {
22942ac794baSSreekanth Reddy 	struct mpi3_ci_upload_request ci_upload;
22952ac794baSSreekanth Reddy 	int retval = -1;
22962ac794baSSreekanth Reddy 	void *data = NULL;
22972ac794baSSreekanth Reddy 	dma_addr_t data_dma;
22982ac794baSSreekanth Reddy 	struct mpi3_ci_manifest_mpi *manifest;
22992ac794baSSreekanth Reddy 	u32 data_len = sizeof(struct mpi3_ci_manifest_mpi);
23002ac794baSSreekanth Reddy 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
23012ac794baSSreekanth Reddy 
23022ac794baSSreekanth Reddy 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
23032ac794baSSreekanth Reddy 	    GFP_KERNEL);
23042ac794baSSreekanth Reddy 	if (!data)
23052ac794baSSreekanth Reddy 		return -ENOMEM;
23062ac794baSSreekanth Reddy 
23072ac794baSSreekanth Reddy 	memset(&ci_upload, 0, sizeof(ci_upload));
23082ac794baSSreekanth Reddy 	mutex_lock(&mrioc->init_cmds.mutex);
23092ac794baSSreekanth Reddy 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
23102ac794baSSreekanth Reddy 		ioc_err(mrioc, "sending get package version failed due to command in use\n");
23112ac794baSSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
23122ac794baSSreekanth Reddy 		goto out;
23132ac794baSSreekanth Reddy 	}
23142ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
23152ac794baSSreekanth Reddy 	mrioc->init_cmds.is_waiting = 1;
23162ac794baSSreekanth Reddy 	mrioc->init_cmds.callback = NULL;
23172ac794baSSreekanth Reddy 	ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
23182ac794baSSreekanth Reddy 	ci_upload.function = MPI3_FUNCTION_CI_UPLOAD;
23192ac794baSSreekanth Reddy 	ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
23202ac794baSSreekanth Reddy 	ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST);
23212ac794baSSreekanth Reddy 	ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE);
23222ac794baSSreekanth Reddy 	ci_upload.segment_size = cpu_to_le32(data_len);
23232ac794baSSreekanth Reddy 
23242ac794baSSreekanth Reddy 	mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len,
23252ac794baSSreekanth Reddy 	    data_dma);
23262ac794baSSreekanth Reddy 	init_completion(&mrioc->init_cmds.done);
23272ac794baSSreekanth Reddy 	retval = mpi3mr_admin_request_post(mrioc, &ci_upload,
23282ac794baSSreekanth Reddy 	    sizeof(ci_upload), 1);
23292ac794baSSreekanth Reddy 	if (retval) {
23302ac794baSSreekanth Reddy 		ioc_err(mrioc, "posting get package version failed\n");
23312ac794baSSreekanth Reddy 		goto out_unlock;
23322ac794baSSreekanth Reddy 	}
23332ac794baSSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done,
23342ac794baSSreekanth Reddy 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
23352ac794baSSreekanth Reddy 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
23362ac794baSSreekanth Reddy 		ioc_err(mrioc, "get package version timed out\n");
2337a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2338a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
23392ac794baSSreekanth Reddy 		retval = -1;
23402ac794baSSreekanth Reddy 		goto out_unlock;
23412ac794baSSreekanth Reddy 	}
23422ac794baSSreekanth Reddy 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
23432ac794baSSreekanth Reddy 	    == MPI3_IOCSTATUS_SUCCESS) {
23442ac794baSSreekanth Reddy 		manifest = (struct mpi3_ci_manifest_mpi *) data;
23452ac794baSSreekanth Reddy 		if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) {
23462ac794baSSreekanth Reddy 			ioc_info(mrioc,
23472ac794baSSreekanth Reddy 			    "firmware package version(%d.%d.%d.%d.%05d-%05d)\n",
23482ac794baSSreekanth Reddy 			    manifest->package_version.gen_major,
23492ac794baSSreekanth Reddy 			    manifest->package_version.gen_minor,
23502ac794baSSreekanth Reddy 			    manifest->package_version.phase_major,
23512ac794baSSreekanth Reddy 			    manifest->package_version.phase_minor,
23522ac794baSSreekanth Reddy 			    manifest->package_version.customer_id,
23532ac794baSSreekanth Reddy 			    manifest->package_version.build_num);
23542ac794baSSreekanth Reddy 		}
23552ac794baSSreekanth Reddy 	}
23562ac794baSSreekanth Reddy 	retval = 0;
23572ac794baSSreekanth Reddy out_unlock:
23582ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
23592ac794baSSreekanth Reddy 	mutex_unlock(&mrioc->init_cmds.mutex);
23602ac794baSSreekanth Reddy 
23612ac794baSSreekanth Reddy out:
23622ac794baSSreekanth Reddy 	if (data)
23632ac794baSSreekanth Reddy 		dma_free_coherent(&mrioc->pdev->dev, data_len, data,
23642ac794baSSreekanth Reddy 		    data_dma);
23652ac794baSSreekanth Reddy 	return retval;
23662ac794baSSreekanth Reddy }
23672ac794baSSreekanth Reddy 
23682ac794baSSreekanth Reddy /**
2369672ae26cSKashyap Desai  * mpi3mr_watchdog_work - watchdog thread to monitor faults
2370672ae26cSKashyap Desai  * @work: work struct
2371672ae26cSKashyap Desai  *
2372672ae26cSKashyap Desai  * Watch dog work periodically executed (1 second interval) to
2373672ae26cSKashyap Desai  * monitor firmware fault and to issue periodic timer sync to
2374672ae26cSKashyap Desai  * the firmware.
2375672ae26cSKashyap Desai  *
2376672ae26cSKashyap Desai  * Return: Nothing.
2377672ae26cSKashyap Desai  */
2378672ae26cSKashyap Desai static void mpi3mr_watchdog_work(struct work_struct *work)
2379672ae26cSKashyap Desai {
2380672ae26cSKashyap Desai 	struct mpi3mr_ioc *mrioc =
2381672ae26cSKashyap Desai 	    container_of(work, struct mpi3mr_ioc, watchdog_work.work);
2382672ae26cSKashyap Desai 	unsigned long flags;
2383672ae26cSKashyap Desai 	enum mpi3mr_iocstate ioc_state;
238478b76a07SSreekanth Reddy 	u32 fault, host_diagnostic, ioc_status;
238578b76a07SSreekanth Reddy 	u32 reset_reason = MPI3MR_RESET_FROM_FAULT_WATCH;
2386672ae26cSKashyap Desai 
2387b64845a7SSreekanth Reddy 	if (mrioc->reset_in_progress || mrioc->unrecoverable)
2388b64845a7SSreekanth Reddy 		return;
2389b64845a7SSreekanth Reddy 
239054dfcffbSKashyap Desai 	if (mrioc->ts_update_counter++ >= MPI3MR_TSUPDATE_INTERVAL) {
239154dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
239254dfcffbSKashyap Desai 		mpi3mr_sync_timestamp(mrioc);
239354dfcffbSKashyap Desai 	}
239454dfcffbSKashyap Desai 
239578b76a07SSreekanth Reddy 	if ((mrioc->prepare_for_reset) &&
239678b76a07SSreekanth Reddy 	    ((mrioc->prepare_for_reset_timeout_counter++) >=
239778b76a07SSreekanth Reddy 	     MPI3MR_PREPARE_FOR_RESET_TIMEOUT)) {
239878b76a07SSreekanth Reddy 		mpi3mr_soft_reset_handler(mrioc,
239978b76a07SSreekanth Reddy 		    MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1);
240078b76a07SSreekanth Reddy 		return;
240178b76a07SSreekanth Reddy 	}
240278b76a07SSreekanth Reddy 
240378b76a07SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
240478b76a07SSreekanth Reddy 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
240578b76a07SSreekanth Reddy 		mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0);
240678b76a07SSreekanth Reddy 		return;
240778b76a07SSreekanth Reddy 	}
240878b76a07SSreekanth Reddy 
2409672ae26cSKashyap Desai 	/*Check for fault state every one second and issue Soft reset*/
2410672ae26cSKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
241178b76a07SSreekanth Reddy 	if (ioc_state != MRIOC_STATE_FAULT)
241278b76a07SSreekanth Reddy 		goto schedule_work;
241378b76a07SSreekanth Reddy 
241478b76a07SSreekanth Reddy 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
2415672ae26cSKashyap Desai 	host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2416672ae26cSKashyap Desai 	if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
2417672ae26cSKashyap Desai 		if (!mrioc->diagsave_timeout) {
2418672ae26cSKashyap Desai 			mpi3mr_print_fault_info(mrioc);
241978b76a07SSreekanth Reddy 			ioc_warn(mrioc, "diag save in progress\n");
2420672ae26cSKashyap Desai 		}
242178b76a07SSreekanth Reddy 		if ((mrioc->diagsave_timeout++) <= MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
2422672ae26cSKashyap Desai 			goto schedule_work;
242378b76a07SSreekanth Reddy 	}
242478b76a07SSreekanth Reddy 
2425672ae26cSKashyap Desai 	mpi3mr_print_fault_info(mrioc);
2426672ae26cSKashyap Desai 	mrioc->diagsave_timeout = 0;
2427672ae26cSKashyap Desai 
242878b76a07SSreekanth Reddy 	switch (fault) {
242978b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED:
2430672ae26cSKashyap Desai 		ioc_info(mrioc,
243178b76a07SSreekanth Reddy 		    "controller requires system power cycle, marking controller as unrecoverable\n");
2432672ae26cSKashyap Desai 		mrioc->unrecoverable = 1;
243378b76a07SSreekanth Reddy 		return;
243478b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS:
243578b76a07SSreekanth Reddy 		return;
243678b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET:
243778b76a07SSreekanth Reddy 		reset_reason = MPI3MR_RESET_FROM_CIACTIV_FAULT;
243878b76a07SSreekanth Reddy 		break;
243978b76a07SSreekanth Reddy 	default:
244078b76a07SSreekanth Reddy 		break;
2441672ae26cSKashyap Desai 	}
244278b76a07SSreekanth Reddy 	mpi3mr_soft_reset_handler(mrioc, reset_reason, 0);
244378b76a07SSreekanth Reddy 	return;
2444672ae26cSKashyap Desai 
2445672ae26cSKashyap Desai schedule_work:
2446672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2447672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2448672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2449672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2450672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2451672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2452672ae26cSKashyap Desai 	return;
2453672ae26cSKashyap Desai }
2454672ae26cSKashyap Desai 
2455672ae26cSKashyap Desai /**
2456672ae26cSKashyap Desai  * mpi3mr_start_watchdog - Start watchdog
2457672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2458672ae26cSKashyap Desai  *
2459672ae26cSKashyap Desai  * Create and start the watchdog thread to monitor controller
2460672ae26cSKashyap Desai  * faults.
2461672ae26cSKashyap Desai  *
2462672ae26cSKashyap Desai  * Return: Nothing.
2463672ae26cSKashyap Desai  */
2464672ae26cSKashyap Desai void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc)
2465672ae26cSKashyap Desai {
2466672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2467672ae26cSKashyap Desai 		return;
2468672ae26cSKashyap Desai 
2469672ae26cSKashyap Desai 	INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work);
2470672ae26cSKashyap Desai 	snprintf(mrioc->watchdog_work_q_name,
2471672ae26cSKashyap Desai 	    sizeof(mrioc->watchdog_work_q_name), "watchdog_%s%d", mrioc->name,
2472672ae26cSKashyap Desai 	    mrioc->id);
2473672ae26cSKashyap Desai 	mrioc->watchdog_work_q =
2474672ae26cSKashyap Desai 	    create_singlethread_workqueue(mrioc->watchdog_work_q_name);
2475672ae26cSKashyap Desai 	if (!mrioc->watchdog_work_q) {
2476672ae26cSKashyap Desai 		ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__);
2477672ae26cSKashyap Desai 		return;
2478672ae26cSKashyap Desai 	}
2479672ae26cSKashyap Desai 
2480672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2481672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2482672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2483672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2484672ae26cSKashyap Desai }
2485672ae26cSKashyap Desai 
2486672ae26cSKashyap Desai /**
2487672ae26cSKashyap Desai  * mpi3mr_stop_watchdog - Stop watchdog
2488672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2489672ae26cSKashyap Desai  *
2490672ae26cSKashyap Desai  * Stop the watchdog thread created to monitor controller
2491672ae26cSKashyap Desai  * faults.
2492672ae26cSKashyap Desai  *
2493672ae26cSKashyap Desai  * Return: Nothing.
2494672ae26cSKashyap Desai  */
2495672ae26cSKashyap Desai void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc)
2496672ae26cSKashyap Desai {
2497672ae26cSKashyap Desai 	unsigned long flags;
2498672ae26cSKashyap Desai 	struct workqueue_struct *wq;
2499672ae26cSKashyap Desai 
2500672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2501672ae26cSKashyap Desai 	wq = mrioc->watchdog_work_q;
2502672ae26cSKashyap Desai 	mrioc->watchdog_work_q = NULL;
2503672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2504672ae26cSKashyap Desai 	if (wq) {
2505672ae26cSKashyap Desai 		if (!cancel_delayed_work_sync(&mrioc->watchdog_work))
2506672ae26cSKashyap Desai 			flush_workqueue(wq);
2507672ae26cSKashyap Desai 		destroy_workqueue(wq);
2508672ae26cSKashyap Desai 	}
2509672ae26cSKashyap Desai }
2510672ae26cSKashyap Desai 
2511672ae26cSKashyap Desai /**
2512824a1566SKashyap Desai  * mpi3mr_setup_admin_qpair - Setup admin queue pair
2513824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2514824a1566SKashyap Desai  *
2515824a1566SKashyap Desai  * Allocate memory for admin queue pair if required and register
2516824a1566SKashyap Desai  * the admin queue with the controller.
2517824a1566SKashyap Desai  *
2518824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2519824a1566SKashyap Desai  */
2520824a1566SKashyap Desai static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
2521824a1566SKashyap Desai {
2522824a1566SKashyap Desai 	int retval = 0;
2523824a1566SKashyap Desai 	u32 num_admin_entries = 0;
2524824a1566SKashyap Desai 
2525824a1566SKashyap Desai 	mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
2526824a1566SKashyap Desai 	mrioc->num_admin_req = mrioc->admin_req_q_sz /
2527824a1566SKashyap Desai 	    MPI3MR_ADMIN_REQ_FRAME_SZ;
2528824a1566SKashyap Desai 	mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
2529824a1566SKashyap Desai 	mrioc->admin_req_base = NULL;
2530824a1566SKashyap Desai 
2531824a1566SKashyap Desai 	mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
2532824a1566SKashyap Desai 	mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
2533824a1566SKashyap Desai 	    MPI3MR_ADMIN_REPLY_FRAME_SZ;
2534824a1566SKashyap Desai 	mrioc->admin_reply_ci = 0;
2535824a1566SKashyap Desai 	mrioc->admin_reply_ephase = 1;
2536824a1566SKashyap Desai 	mrioc->admin_reply_base = NULL;
2537824a1566SKashyap Desai 
2538824a1566SKashyap Desai 	if (!mrioc->admin_req_base) {
2539824a1566SKashyap Desai 		mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
2540824a1566SKashyap Desai 		    mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
2541824a1566SKashyap Desai 
2542824a1566SKashyap Desai 		if (!mrioc->admin_req_base) {
2543824a1566SKashyap Desai 			retval = -1;
2544824a1566SKashyap Desai 			goto out_failed;
2545824a1566SKashyap Desai 		}
2546824a1566SKashyap Desai 
2547824a1566SKashyap Desai 		mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
2548824a1566SKashyap Desai 		    mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
2549824a1566SKashyap Desai 		    GFP_KERNEL);
2550824a1566SKashyap Desai 
2551824a1566SKashyap Desai 		if (!mrioc->admin_reply_base) {
2552824a1566SKashyap Desai 			retval = -1;
2553824a1566SKashyap Desai 			goto out_failed;
2554824a1566SKashyap Desai 		}
2555824a1566SKashyap Desai 	}
2556824a1566SKashyap Desai 
2557824a1566SKashyap Desai 	num_admin_entries = (mrioc->num_admin_replies << 16) |
2558824a1566SKashyap Desai 	    (mrioc->num_admin_req);
2559824a1566SKashyap Desai 	writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
2560824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_req_dma,
2561824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_request_queue_address);
2562824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_reply_dma,
2563824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_reply_queue_address);
2564824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
2565824a1566SKashyap Desai 	writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
2566824a1566SKashyap Desai 	return retval;
2567824a1566SKashyap Desai 
2568824a1566SKashyap Desai out_failed:
2569824a1566SKashyap Desai 
2570824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
2571824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
2572824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
2573824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
2574824a1566SKashyap Desai 	}
2575824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
2576824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
2577824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
2578824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
2579824a1566SKashyap Desai 	}
2580824a1566SKashyap Desai 	return retval;
2581824a1566SKashyap Desai }
2582824a1566SKashyap Desai 
2583824a1566SKashyap Desai /**
2584824a1566SKashyap Desai  * mpi3mr_issue_iocfacts - Send IOC Facts
2585824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2586824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2587824a1566SKashyap Desai  *
2588824a1566SKashyap Desai  * Issue IOC Facts MPI request through admin queue and wait for
2589824a1566SKashyap Desai  * the completion of it or time out.
2590824a1566SKashyap Desai  *
2591824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2592824a1566SKashyap Desai  */
2593824a1566SKashyap Desai static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
2594824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2595824a1566SKashyap Desai {
2596824a1566SKashyap Desai 	struct mpi3_ioc_facts_request iocfacts_req;
2597824a1566SKashyap Desai 	void *data = NULL;
2598824a1566SKashyap Desai 	dma_addr_t data_dma;
2599824a1566SKashyap Desai 	u32 data_len = sizeof(*facts_data);
2600824a1566SKashyap Desai 	int retval = 0;
2601824a1566SKashyap Desai 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
2602824a1566SKashyap Desai 
2603824a1566SKashyap Desai 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2604824a1566SKashyap Desai 	    GFP_KERNEL);
2605824a1566SKashyap Desai 
2606824a1566SKashyap Desai 	if (!data) {
2607824a1566SKashyap Desai 		retval = -1;
2608824a1566SKashyap Desai 		goto out;
2609824a1566SKashyap Desai 	}
2610824a1566SKashyap Desai 
2611824a1566SKashyap Desai 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
2612824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
2613824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2614824a1566SKashyap Desai 		retval = -1;
2615824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
2616824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
2617824a1566SKashyap Desai 		goto out;
2618824a1566SKashyap Desai 	}
2619824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2620824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
2621824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
2622824a1566SKashyap Desai 	iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2623824a1566SKashyap Desai 	iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
2624824a1566SKashyap Desai 
2625824a1566SKashyap Desai 	mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
2626824a1566SKashyap Desai 	    data_dma);
2627824a1566SKashyap Desai 
2628824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
2629824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
2630824a1566SKashyap Desai 	    sizeof(iocfacts_req), 1);
2631824a1566SKashyap Desai 	if (retval) {
2632824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
2633824a1566SKashyap Desai 		goto out_unlock;
2634824a1566SKashyap Desai 	}
2635824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2636824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2637824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2638a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_facts timed out\n");
2639a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2640824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
2641824a1566SKashyap Desai 		retval = -1;
2642824a1566SKashyap Desai 		goto out_unlock;
2643824a1566SKashyap Desai 	}
2644824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2645824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2646824a1566SKashyap Desai 		ioc_err(mrioc,
2647824a1566SKashyap Desai 		    "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2648824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2649824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2650824a1566SKashyap Desai 		retval = -1;
2651824a1566SKashyap Desai 		goto out_unlock;
2652824a1566SKashyap Desai 	}
2653824a1566SKashyap Desai 	memcpy(facts_data, (u8 *)data, data_len);
2654c5758fc7SSreekanth Reddy 	mpi3mr_process_factsdata(mrioc, facts_data);
2655824a1566SKashyap Desai out_unlock:
2656824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2657824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2658824a1566SKashyap Desai 
2659824a1566SKashyap Desai out:
2660824a1566SKashyap Desai 	if (data)
2661824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
2662824a1566SKashyap Desai 
2663824a1566SKashyap Desai 	return retval;
2664824a1566SKashyap Desai }
2665824a1566SKashyap Desai 
2666824a1566SKashyap Desai /**
2667824a1566SKashyap Desai  * mpi3mr_check_reset_dma_mask - Process IOC facts data
2668824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2669824a1566SKashyap Desai  *
2670824a1566SKashyap Desai  * Check whether the new DMA mask requested through IOCFacts by
2671824a1566SKashyap Desai  * firmware needs to be set, if so set it .
2672824a1566SKashyap Desai  *
2673824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
2674824a1566SKashyap Desai  */
2675824a1566SKashyap Desai static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
2676824a1566SKashyap Desai {
2677824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
2678824a1566SKashyap Desai 	int r;
2679824a1566SKashyap Desai 	u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
2680824a1566SKashyap Desai 
2681824a1566SKashyap Desai 	if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
2682824a1566SKashyap Desai 		return 0;
2683824a1566SKashyap Desai 
2684824a1566SKashyap Desai 	ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
2685824a1566SKashyap Desai 	    mrioc->dma_mask, facts_dma_mask);
2686824a1566SKashyap Desai 
2687824a1566SKashyap Desai 	r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
2688824a1566SKashyap Desai 	if (r) {
2689824a1566SKashyap Desai 		ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
2690824a1566SKashyap Desai 		    facts_dma_mask, r);
2691824a1566SKashyap Desai 		return r;
2692824a1566SKashyap Desai 	}
2693824a1566SKashyap Desai 	mrioc->dma_mask = facts_dma_mask;
2694824a1566SKashyap Desai 	return r;
2695824a1566SKashyap Desai }
2696824a1566SKashyap Desai 
2697824a1566SKashyap Desai /**
2698824a1566SKashyap Desai  * mpi3mr_process_factsdata - Process IOC facts data
2699824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2700824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2701824a1566SKashyap Desai  *
2702824a1566SKashyap Desai  * Convert IOC facts data into cpu endianness and cache it in
2703824a1566SKashyap Desai  * the driver .
2704824a1566SKashyap Desai  *
2705824a1566SKashyap Desai  * Return: Nothing.
2706824a1566SKashyap Desai  */
2707824a1566SKashyap Desai static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
2708824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2709824a1566SKashyap Desai {
2710824a1566SKashyap Desai 	u32 ioc_config, req_sz, facts_flags;
2711824a1566SKashyap Desai 
2712824a1566SKashyap Desai 	if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
2713824a1566SKashyap Desai 	    (sizeof(*facts_data) / 4)) {
2714824a1566SKashyap Desai 		ioc_warn(mrioc,
2715824a1566SKashyap Desai 		    "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
2716824a1566SKashyap Desai 		    sizeof(*facts_data),
2717824a1566SKashyap Desai 		    le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
2718824a1566SKashyap Desai 	}
2719824a1566SKashyap Desai 
2720824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
2721824a1566SKashyap Desai 	req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
2722824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
2723824a1566SKashyap Desai 	if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
2724824a1566SKashyap Desai 		ioc_err(mrioc,
2725824a1566SKashyap Desai 		    "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
2726824a1566SKashyap Desai 		    req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
2727824a1566SKashyap Desai 	}
2728824a1566SKashyap Desai 
2729824a1566SKashyap Desai 	memset(&mrioc->facts, 0, sizeof(mrioc->facts));
2730824a1566SKashyap Desai 
2731824a1566SKashyap Desai 	facts_flags = le32_to_cpu(facts_data->flags);
2732824a1566SKashyap Desai 	mrioc->facts.op_req_sz = req_sz;
2733824a1566SKashyap Desai 	mrioc->op_reply_desc_sz = 1 << ((ioc_config &
2734824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
2735824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
2736824a1566SKashyap Desai 
2737824a1566SKashyap Desai 	mrioc->facts.ioc_num = facts_data->ioc_number;
2738824a1566SKashyap Desai 	mrioc->facts.who_init = facts_data->who_init;
2739824a1566SKashyap Desai 	mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
2740824a1566SKashyap Desai 	mrioc->facts.personality = (facts_flags &
2741824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
2742824a1566SKashyap Desai 	mrioc->facts.dma_mask = (facts_flags &
2743824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
2744824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
2745824a1566SKashyap Desai 	mrioc->facts.protocol_flags = facts_data->protocol_flags;
2746824a1566SKashyap Desai 	mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
274704b27e53SSreekanth Reddy 	mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_requests);
2748824a1566SKashyap Desai 	mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
2749824a1566SKashyap Desai 	mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
2750824a1566SKashyap Desai 	mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
2751824a1566SKashyap Desai 	mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
2752824a1566SKashyap Desai 	mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
2753824a1566SKashyap Desai 	mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
2754ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds);
2755ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds);
2756824a1566SKashyap Desai 	mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
2757824a1566SKashyap Desai 	mrioc->facts.max_pcie_switches =
2758ec5ebd2cSSreekanth Reddy 	    le16_to_cpu(facts_data->max_pcie_switches);
2759824a1566SKashyap Desai 	mrioc->facts.max_sasexpanders =
2760824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_expanders);
2761824a1566SKashyap Desai 	mrioc->facts.max_sasinitiators =
2762824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_initiators);
2763824a1566SKashyap Desai 	mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
2764824a1566SKashyap Desai 	mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
2765824a1566SKashyap Desai 	mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
2766824a1566SKashyap Desai 	mrioc->facts.max_op_req_q =
2767824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_request_queues);
2768824a1566SKashyap Desai 	mrioc->facts.max_op_reply_q =
2769824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_reply_queues);
2770824a1566SKashyap Desai 	mrioc->facts.ioc_capabilities =
2771824a1566SKashyap Desai 	    le32_to_cpu(facts_data->ioc_capabilities);
2772824a1566SKashyap Desai 	mrioc->facts.fw_ver.build_num =
2773824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.build_num);
2774824a1566SKashyap Desai 	mrioc->facts.fw_ver.cust_id =
2775824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.customer_id);
2776824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
2777824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
2778824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
2779824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
2780824a1566SKashyap Desai 	mrioc->msix_count = min_t(int, mrioc->msix_count,
2781824a1566SKashyap Desai 	    mrioc->facts.max_msix_vectors);
2782824a1566SKashyap Desai 	mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
2783824a1566SKashyap Desai 	mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
2784824a1566SKashyap Desai 	mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
2785824a1566SKashyap Desai 	mrioc->facts.shutdown_timeout =
2786824a1566SKashyap Desai 	    le16_to_cpu(facts_data->shutdown_timeout);
2787824a1566SKashyap Desai 
2788f10af057SSreekanth Reddy 	mrioc->facts.max_dev_per_tg =
2789f10af057SSreekanth Reddy 	    facts_data->max_devices_per_throttle_group;
2790f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_data_length =
2791f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->io_throttle_data_length);
2792f10af057SSreekanth Reddy 	mrioc->facts.max_io_throttle_group =
2793f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->max_io_throttle_group);
2794f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_low = le16_to_cpu(facts_data->io_throttle_low);
2795f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_high =
2796f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->io_throttle_high);
2797f10af057SSreekanth Reddy 
2798f10af057SSreekanth Reddy 	/* Store in 512b block count */
2799f10af057SSreekanth Reddy 	if (mrioc->facts.io_throttle_data_length)
2800f10af057SSreekanth Reddy 		mrioc->io_throttle_data_length =
2801f10af057SSreekanth Reddy 		    (mrioc->facts.io_throttle_data_length * 2 * 4);
2802f10af057SSreekanth Reddy 	else
2803f10af057SSreekanth Reddy 		/* set the length to 1MB + 1K to disable throttle */
2804f10af057SSreekanth Reddy 		mrioc->io_throttle_data_length = MPI3MR_MAX_SECTORS + 2;
2805f10af057SSreekanth Reddy 
2806f10af057SSreekanth Reddy 	mrioc->io_throttle_high = (mrioc->facts.io_throttle_high * 2 * 1024);
2807f10af057SSreekanth Reddy 	mrioc->io_throttle_low = (mrioc->facts.io_throttle_low * 2 * 1024);
2808f10af057SSreekanth Reddy 
2809824a1566SKashyap Desai 	ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
2810824a1566SKashyap Desai 	    mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
2811824a1566SKashyap Desai 	    mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
2812824a1566SKashyap Desai 	ioc_info(mrioc,
2813ec5ebd2cSSreekanth Reddy 	    "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n",
2814824a1566SKashyap Desai 	    mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
2815ec5ebd2cSSreekanth Reddy 	    mrioc->facts.max_msix_vectors, mrioc->facts.max_perids);
2816824a1566SKashyap Desai 	ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
2817824a1566SKashyap Desai 	    mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
2818824a1566SKashyap Desai 	    mrioc->facts.sge_mod_shift);
2819824a1566SKashyap Desai 	ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
2820824a1566SKashyap Desai 	    mrioc->facts.dma_mask, (facts_flags &
2821824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
2822f10af057SSreekanth Reddy 	ioc_info(mrioc,
2823f10af057SSreekanth Reddy 	    "max_dev_per_throttle_group(%d), max_throttle_groups(%d)\n",
2824f10af057SSreekanth Reddy 	    mrioc->facts.max_dev_per_tg, mrioc->facts.max_io_throttle_group);
2825f10af057SSreekanth Reddy 	ioc_info(mrioc,
2826f10af057SSreekanth Reddy 	   "io_throttle_data_len(%dKiB), io_throttle_high(%dMiB), io_throttle_low(%dMiB)\n",
2827f10af057SSreekanth Reddy 	   mrioc->facts.io_throttle_data_length * 4,
2828f10af057SSreekanth Reddy 	   mrioc->facts.io_throttle_high, mrioc->facts.io_throttle_low);
2829824a1566SKashyap Desai }
2830824a1566SKashyap Desai 
2831824a1566SKashyap Desai /**
2832824a1566SKashyap Desai  * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
2833824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2834824a1566SKashyap Desai  *
2835824a1566SKashyap Desai  * Allocate and initialize the reply free buffers, sense
2836824a1566SKashyap Desai  * buffers, reply free queue and sense buffer queue.
2837824a1566SKashyap Desai  *
2838824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2839824a1566SKashyap Desai  */
2840824a1566SKashyap Desai static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
2841824a1566SKashyap Desai {
2842824a1566SKashyap Desai 	int retval = 0;
2843824a1566SKashyap Desai 	u32 sz, i;
2844824a1566SKashyap Desai 
2845824a1566SKashyap Desai 	if (mrioc->init_cmds.reply)
2846e3605f65SSreekanth Reddy 		return retval;
2847824a1566SKashyap Desai 
2848c5758fc7SSreekanth Reddy 	mrioc->init_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2849824a1566SKashyap Desai 	if (!mrioc->init_cmds.reply)
2850824a1566SKashyap Desai 		goto out_failed;
2851824a1566SKashyap Desai 
2852f5e6d5a3SSumit Saxena 	mrioc->bsg_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2853f5e6d5a3SSumit Saxena 	if (!mrioc->bsg_cmds.reply)
2854f5e6d5a3SSumit Saxena 		goto out_failed;
2855f5e6d5a3SSumit Saxena 
285613ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
2857c5758fc7SSreekanth Reddy 		mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->reply_sz,
285813ef29eaSKashyap Desai 		    GFP_KERNEL);
285913ef29eaSKashyap Desai 		if (!mrioc->dev_rmhs_cmds[i].reply)
286013ef29eaSKashyap Desai 			goto out_failed;
286113ef29eaSKashyap Desai 	}
286213ef29eaSKashyap Desai 
2863c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
2864c1af985dSSreekanth Reddy 		mrioc->evtack_cmds[i].reply = kzalloc(mrioc->reply_sz,
2865c1af985dSSreekanth Reddy 		    GFP_KERNEL);
2866c1af985dSSreekanth Reddy 		if (!mrioc->evtack_cmds[i].reply)
2867c1af985dSSreekanth Reddy 			goto out_failed;
2868c1af985dSSreekanth Reddy 	}
2869c1af985dSSreekanth Reddy 
2870c5758fc7SSreekanth Reddy 	mrioc->host_tm_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2871e844adb1SKashyap Desai 	if (!mrioc->host_tm_cmds.reply)
2872e844adb1SKashyap Desai 		goto out_failed;
2873e844adb1SKashyap Desai 
287443ca1100SSumit Saxena 	mrioc->pel_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
287543ca1100SSumit Saxena 	if (!mrioc->pel_cmds.reply)
287643ca1100SSumit Saxena 		goto out_failed;
287743ca1100SSumit Saxena 
287843ca1100SSumit Saxena 	mrioc->pel_abort_cmd.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
287943ca1100SSumit Saxena 	if (!mrioc->pel_abort_cmd.reply)
288043ca1100SSumit Saxena 		goto out_failed;
288143ca1100SSumit Saxena 
2882e844adb1SKashyap Desai 	mrioc->dev_handle_bitmap_sz = mrioc->facts.max_devhandle / 8;
2883e844adb1SKashyap Desai 	if (mrioc->facts.max_devhandle % 8)
2884e844adb1SKashyap Desai 		mrioc->dev_handle_bitmap_sz++;
2885e844adb1SKashyap Desai 	mrioc->removepend_bitmap = kzalloc(mrioc->dev_handle_bitmap_sz,
2886e844adb1SKashyap Desai 	    GFP_KERNEL);
2887e844adb1SKashyap Desai 	if (!mrioc->removepend_bitmap)
2888e844adb1SKashyap Desai 		goto out_failed;
2889e844adb1SKashyap Desai 
2890e844adb1SKashyap Desai 	mrioc->devrem_bitmap_sz = MPI3MR_NUM_DEVRMCMD / 8;
2891e844adb1SKashyap Desai 	if (MPI3MR_NUM_DEVRMCMD % 8)
2892e844adb1SKashyap Desai 		mrioc->devrem_bitmap_sz++;
2893e844adb1SKashyap Desai 	mrioc->devrem_bitmap = kzalloc(mrioc->devrem_bitmap_sz,
2894e844adb1SKashyap Desai 	    GFP_KERNEL);
2895e844adb1SKashyap Desai 	if (!mrioc->devrem_bitmap)
2896e844adb1SKashyap Desai 		goto out_failed;
2897e844adb1SKashyap Desai 
2898c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap_sz = MPI3MR_NUM_EVTACKCMD / 8;
2899c1af985dSSreekanth Reddy 	if (MPI3MR_NUM_EVTACKCMD % 8)
2900c1af985dSSreekanth Reddy 		mrioc->evtack_cmds_bitmap_sz++;
2901c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap = kzalloc(mrioc->evtack_cmds_bitmap_sz,
2902c1af985dSSreekanth Reddy 	    GFP_KERNEL);
2903c1af985dSSreekanth Reddy 	if (!mrioc->evtack_cmds_bitmap)
2904c1af985dSSreekanth Reddy 		goto out_failed;
2905c1af985dSSreekanth Reddy 
2906824a1566SKashyap Desai 	mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
2907824a1566SKashyap Desai 	mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
2908824a1566SKashyap Desai 	mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
2909824a1566SKashyap Desai 	mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
2910824a1566SKashyap Desai 
2911824a1566SKashyap Desai 	/* reply buffer pool, 16 byte align */
2912c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2913824a1566SKashyap Desai 	mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
2914824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
2915824a1566SKashyap Desai 	if (!mrioc->reply_buf_pool) {
2916824a1566SKashyap Desai 		ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
2917824a1566SKashyap Desai 		goto out_failed;
2918824a1566SKashyap Desai 	}
2919824a1566SKashyap Desai 
2920824a1566SKashyap Desai 	mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
2921824a1566SKashyap Desai 	    &mrioc->reply_buf_dma);
2922824a1566SKashyap Desai 	if (!mrioc->reply_buf)
2923824a1566SKashyap Desai 		goto out_failed;
2924824a1566SKashyap Desai 
2925824a1566SKashyap Desai 	mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
2926824a1566SKashyap Desai 
2927824a1566SKashyap Desai 	/* reply free queue, 8 byte align */
2928824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
2929824a1566SKashyap Desai 	mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
2930824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
2931824a1566SKashyap Desai 	if (!mrioc->reply_free_q_pool) {
2932824a1566SKashyap Desai 		ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
2933824a1566SKashyap Desai 		goto out_failed;
2934824a1566SKashyap Desai 	}
2935824a1566SKashyap Desai 	mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
2936824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->reply_free_q_dma);
2937824a1566SKashyap Desai 	if (!mrioc->reply_free_q)
2938824a1566SKashyap Desai 		goto out_failed;
2939824a1566SKashyap Desai 
2940824a1566SKashyap Desai 	/* sense buffer pool,  4 byte align */
2941ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2942824a1566SKashyap Desai 	mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
2943824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 4, 0);
2944824a1566SKashyap Desai 	if (!mrioc->sense_buf_pool) {
2945824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
2946824a1566SKashyap Desai 		goto out_failed;
2947824a1566SKashyap Desai 	}
2948824a1566SKashyap Desai 	mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
2949824a1566SKashyap Desai 	    &mrioc->sense_buf_dma);
2950824a1566SKashyap Desai 	if (!mrioc->sense_buf)
2951824a1566SKashyap Desai 		goto out_failed;
2952824a1566SKashyap Desai 
2953824a1566SKashyap Desai 	/* sense buffer queue, 8 byte align */
2954824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
2955824a1566SKashyap Desai 	mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
2956824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
2957824a1566SKashyap Desai 	if (!mrioc->sense_buf_q_pool) {
2958824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
2959824a1566SKashyap Desai 		goto out_failed;
2960824a1566SKashyap Desai 	}
2961824a1566SKashyap Desai 	mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
2962824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->sense_buf_q_dma);
2963824a1566SKashyap Desai 	if (!mrioc->sense_buf_q)
2964824a1566SKashyap Desai 		goto out_failed;
2965824a1566SKashyap Desai 
2966e3605f65SSreekanth Reddy 	return retval;
2967e3605f65SSreekanth Reddy 
2968e3605f65SSreekanth Reddy out_failed:
2969e3605f65SSreekanth Reddy 	retval = -1;
2970e3605f65SSreekanth Reddy 	return retval;
2971e3605f65SSreekanth Reddy }
2972e3605f65SSreekanth Reddy 
2973e3605f65SSreekanth Reddy /**
2974e3605f65SSreekanth Reddy  * mpimr_initialize_reply_sbuf_queues - initialize reply sense
2975e3605f65SSreekanth Reddy  * buffers
2976e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
2977e3605f65SSreekanth Reddy  *
2978e3605f65SSreekanth Reddy  * Helper function to initialize reply and sense buffers along
2979e3605f65SSreekanth Reddy  * with some debug prints.
2980e3605f65SSreekanth Reddy  *
2981e3605f65SSreekanth Reddy  * Return:  None.
2982e3605f65SSreekanth Reddy  */
2983e3605f65SSreekanth Reddy static void mpimr_initialize_reply_sbuf_queues(struct mpi3mr_ioc *mrioc)
2984e3605f65SSreekanth Reddy {
2985e3605f65SSreekanth Reddy 	u32 sz, i;
2986e3605f65SSreekanth Reddy 	dma_addr_t phy_addr;
2987e3605f65SSreekanth Reddy 
2988c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2989824a1566SKashyap Desai 	ioc_info(mrioc,
2990824a1566SKashyap Desai 	    "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2991c5758fc7SSreekanth Reddy 	    mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->reply_sz,
2992824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
2993824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
2994824a1566SKashyap Desai 	ioc_info(mrioc,
2995824a1566SKashyap Desai 	    "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
2996824a1566SKashyap Desai 	    mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
2997824a1566SKashyap Desai 	    (unsigned long long)mrioc->reply_free_q_dma);
2998ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
2999824a1566SKashyap Desai 	ioc_info(mrioc,
3000824a1566SKashyap Desai 	    "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
3001ec5ebd2cSSreekanth Reddy 	    mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ,
3002824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
3003824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
3004824a1566SKashyap Desai 	ioc_info(mrioc,
3005824a1566SKashyap Desai 	    "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
3006824a1566SKashyap Desai 	    mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
3007824a1566SKashyap Desai 	    (unsigned long long)mrioc->sense_buf_q_dma);
3008824a1566SKashyap Desai 
3009824a1566SKashyap Desai 	/* initialize Reply buffer Queue */
3010824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->reply_buf_dma;
3011c5758fc7SSreekanth Reddy 	    i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->reply_sz)
3012824a1566SKashyap Desai 		mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
3013824a1566SKashyap Desai 	mrioc->reply_free_q[i] = cpu_to_le64(0);
3014824a1566SKashyap Desai 
3015824a1566SKashyap Desai 	/* initialize Sense Buffer Queue */
3016824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->sense_buf_dma;
3017ec5ebd2cSSreekanth Reddy 	    i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ)
3018824a1566SKashyap Desai 		mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
3019824a1566SKashyap Desai 	mrioc->sense_buf_q[i] = cpu_to_le64(0);
3020824a1566SKashyap Desai }
3021824a1566SKashyap Desai 
3022824a1566SKashyap Desai /**
3023824a1566SKashyap Desai  * mpi3mr_issue_iocinit - Send IOC Init
3024824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3025824a1566SKashyap Desai  *
3026824a1566SKashyap Desai  * Issue IOC Init MPI request through admin queue and wait for
3027824a1566SKashyap Desai  * the completion of it or time out.
3028824a1566SKashyap Desai  *
3029824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
3030824a1566SKashyap Desai  */
3031824a1566SKashyap Desai static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
3032824a1566SKashyap Desai {
3033824a1566SKashyap Desai 	struct mpi3_ioc_init_request iocinit_req;
3034824a1566SKashyap Desai 	struct mpi3_driver_info_layout *drv_info;
3035824a1566SKashyap Desai 	dma_addr_t data_dma;
3036824a1566SKashyap Desai 	u32 data_len = sizeof(*drv_info);
3037824a1566SKashyap Desai 	int retval = 0;
3038824a1566SKashyap Desai 	ktime_t current_time;
3039824a1566SKashyap Desai 
3040824a1566SKashyap Desai 	drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
3041824a1566SKashyap Desai 	    GFP_KERNEL);
3042824a1566SKashyap Desai 	if (!drv_info) {
3043824a1566SKashyap Desai 		retval = -1;
3044824a1566SKashyap Desai 		goto out;
3045824a1566SKashyap Desai 	}
3046e3605f65SSreekanth Reddy 	mpimr_initialize_reply_sbuf_queues(mrioc);
3047e3605f65SSreekanth Reddy 
3048824a1566SKashyap Desai 	drv_info->information_length = cpu_to_le32(data_len);
3049aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
3050aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
3051aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
3052aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
3053aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
3054aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE,
3055aa0dc6a7SSreekanth Reddy 	    sizeof(drv_info->driver_release_date));
3056824a1566SKashyap Desai 	drv_info->driver_capabilities = 0;
3057824a1566SKashyap Desai 	memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
3058824a1566SKashyap Desai 	    sizeof(mrioc->driver_info));
3059824a1566SKashyap Desai 
3060824a1566SKashyap Desai 	memset(&iocinit_req, 0, sizeof(iocinit_req));
3061824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3062824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3063824a1566SKashyap Desai 		retval = -1;
3064824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
3065824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3066824a1566SKashyap Desai 		goto out;
3067824a1566SKashyap Desai 	}
3068824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3069824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
3070824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
3071824a1566SKashyap Desai 	iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3072824a1566SKashyap Desai 	iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
3073824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
3074824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
3075824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
3076824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
3077824a1566SKashyap Desai 	iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
3078824a1566SKashyap Desai 	iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
3079824a1566SKashyap Desai 	iocinit_req.reply_free_queue_address =
3080824a1566SKashyap Desai 	    cpu_to_le64(mrioc->reply_free_q_dma);
3081ec5ebd2cSSreekanth Reddy 	iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ);
3082824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_depth =
3083824a1566SKashyap Desai 	    cpu_to_le16(mrioc->sense_buf_q_sz);
3084824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_address =
3085824a1566SKashyap Desai 	    cpu_to_le64(mrioc->sense_buf_q_dma);
3086824a1566SKashyap Desai 	iocinit_req.driver_information_address = cpu_to_le64(data_dma);
3087824a1566SKashyap Desai 
3088824a1566SKashyap Desai 	current_time = ktime_get_real();
3089824a1566SKashyap Desai 	iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
3090824a1566SKashyap Desai 
3091824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
3092824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
3093824a1566SKashyap Desai 	    sizeof(iocinit_req), 1);
3094824a1566SKashyap Desai 	if (retval) {
3095824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
3096824a1566SKashyap Desai 		goto out_unlock;
3097824a1566SKashyap Desai 	}
3098824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
3099824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
3100824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3101a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
3102824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
3103a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_init timed out\n");
3104824a1566SKashyap Desai 		retval = -1;
3105824a1566SKashyap Desai 		goto out_unlock;
3106824a1566SKashyap Desai 	}
3107824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
3108824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
3109824a1566SKashyap Desai 		ioc_err(mrioc,
3110824a1566SKashyap Desai 		    "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
3111824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
3112824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
3113824a1566SKashyap Desai 		retval = -1;
3114824a1566SKashyap Desai 		goto out_unlock;
3115824a1566SKashyap Desai 	}
3116824a1566SKashyap Desai 
3117e3605f65SSreekanth Reddy 	mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
3118e3605f65SSreekanth Reddy 	writel(mrioc->reply_free_queue_host_index,
3119e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->reply_free_host_index);
3120e3605f65SSreekanth Reddy 
3121e3605f65SSreekanth Reddy 	mrioc->sbq_host_index = mrioc->num_sense_bufs;
3122e3605f65SSreekanth Reddy 	writel(mrioc->sbq_host_index,
3123e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
3124824a1566SKashyap Desai out_unlock:
3125824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3126824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3127824a1566SKashyap Desai 
3128824a1566SKashyap Desai out:
3129824a1566SKashyap Desai 	if (drv_info)
3130824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
3131824a1566SKashyap Desai 		    data_dma);
3132824a1566SKashyap Desai 
3133824a1566SKashyap Desai 	return retval;
3134824a1566SKashyap Desai }
3135824a1566SKashyap Desai 
3136824a1566SKashyap Desai /**
313713ef29eaSKashyap Desai  * mpi3mr_unmask_events - Unmask events in event mask bitmap
313813ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
313913ef29eaSKashyap Desai  * @event: MPI event ID
314013ef29eaSKashyap Desai  *
314113ef29eaSKashyap Desai  * Un mask the specific event by resetting the event_mask
314213ef29eaSKashyap Desai  * bitmap.
314313ef29eaSKashyap Desai  *
314413ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
314513ef29eaSKashyap Desai  */
314613ef29eaSKashyap Desai static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event)
314713ef29eaSKashyap Desai {
314813ef29eaSKashyap Desai 	u32 desired_event;
314913ef29eaSKashyap Desai 	u8 word;
315013ef29eaSKashyap Desai 
315113ef29eaSKashyap Desai 	if (event >= 128)
315213ef29eaSKashyap Desai 		return;
315313ef29eaSKashyap Desai 
315413ef29eaSKashyap Desai 	desired_event = (1 << (event % 32));
315513ef29eaSKashyap Desai 	word = event / 32;
315613ef29eaSKashyap Desai 
315713ef29eaSKashyap Desai 	mrioc->event_masks[word] &= ~desired_event;
315813ef29eaSKashyap Desai }
315913ef29eaSKashyap Desai 
316013ef29eaSKashyap Desai /**
316113ef29eaSKashyap Desai  * mpi3mr_issue_event_notification - Send event notification
316213ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
316313ef29eaSKashyap Desai  *
316413ef29eaSKashyap Desai  * Issue event notification MPI request through admin queue and
316513ef29eaSKashyap Desai  * wait for the completion of it or time out.
316613ef29eaSKashyap Desai  *
316713ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
316813ef29eaSKashyap Desai  */
316913ef29eaSKashyap Desai static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc)
317013ef29eaSKashyap Desai {
317113ef29eaSKashyap Desai 	struct mpi3_event_notification_request evtnotify_req;
317213ef29eaSKashyap Desai 	int retval = 0;
317313ef29eaSKashyap Desai 	u8 i;
317413ef29eaSKashyap Desai 
317513ef29eaSKashyap Desai 	memset(&evtnotify_req, 0, sizeof(evtnotify_req));
317613ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
317713ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
317813ef29eaSKashyap Desai 		retval = -1;
317913ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n");
318013ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
318113ef29eaSKashyap Desai 		goto out;
318213ef29eaSKashyap Desai 	}
318313ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
318413ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
318513ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
318613ef29eaSKashyap Desai 	evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
318713ef29eaSKashyap Desai 	evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION;
318813ef29eaSKashyap Desai 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
318913ef29eaSKashyap Desai 		evtnotify_req.event_masks[i] =
319013ef29eaSKashyap Desai 		    cpu_to_le32(mrioc->event_masks[i]);
319113ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
319213ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req,
319313ef29eaSKashyap Desai 	    sizeof(evtnotify_req), 1);
319413ef29eaSKashyap Desai 	if (retval) {
319513ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n");
319613ef29eaSKashyap Desai 		goto out_unlock;
319713ef29eaSKashyap Desai 	}
319813ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
319913ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
320013ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3201a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "event notification timed out\n");
3202a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
320313ef29eaSKashyap Desai 		    MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
320413ef29eaSKashyap Desai 		retval = -1;
320513ef29eaSKashyap Desai 		goto out_unlock;
320613ef29eaSKashyap Desai 	}
320713ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
320813ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
320913ef29eaSKashyap Desai 		ioc_err(mrioc,
321013ef29eaSKashyap Desai 		    "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
321113ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
321213ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
321313ef29eaSKashyap Desai 		retval = -1;
321413ef29eaSKashyap Desai 		goto out_unlock;
321513ef29eaSKashyap Desai 	}
321613ef29eaSKashyap Desai 
321713ef29eaSKashyap Desai out_unlock:
321813ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
321913ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
322013ef29eaSKashyap Desai out:
322113ef29eaSKashyap Desai 	return retval;
322213ef29eaSKashyap Desai }
322313ef29eaSKashyap Desai 
322413ef29eaSKashyap Desai /**
3225c1af985dSSreekanth Reddy  * mpi3mr_process_event_ack - Process event acknowledgment
322613ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
322713ef29eaSKashyap Desai  * @event: MPI3 event ID
3228c1af985dSSreekanth Reddy  * @event_ctx: event context
322913ef29eaSKashyap Desai  *
323013ef29eaSKashyap Desai  * Send event acknowledgment through admin queue and wait for
323113ef29eaSKashyap Desai  * it to complete.
323213ef29eaSKashyap Desai  *
323313ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
323413ef29eaSKashyap Desai  */
3235c1af985dSSreekanth Reddy int mpi3mr_process_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
323613ef29eaSKashyap Desai 	u32 event_ctx)
323713ef29eaSKashyap Desai {
323813ef29eaSKashyap Desai 	struct mpi3_event_ack_request evtack_req;
323913ef29eaSKashyap Desai 	int retval = 0;
324013ef29eaSKashyap Desai 
324113ef29eaSKashyap Desai 	memset(&evtack_req, 0, sizeof(evtack_req));
324213ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
324313ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
324413ef29eaSKashyap Desai 		retval = -1;
324513ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Init command is in use\n");
324613ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
324713ef29eaSKashyap Desai 		goto out;
324813ef29eaSKashyap Desai 	}
324913ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
325013ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
325113ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
325213ef29eaSKashyap Desai 	evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
325313ef29eaSKashyap Desai 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
325413ef29eaSKashyap Desai 	evtack_req.event = event;
325513ef29eaSKashyap Desai 	evtack_req.event_context = cpu_to_le32(event_ctx);
325613ef29eaSKashyap Desai 
325713ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
325813ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
325913ef29eaSKashyap Desai 	    sizeof(evtack_req), 1);
326013ef29eaSKashyap Desai 	if (retval) {
326113ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Admin Post failed\n");
326213ef29eaSKashyap Desai 		goto out_unlock;
326313ef29eaSKashyap Desai 	}
326413ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
326513ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
326613ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
326713ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: command timed out\n");
3268fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
326913ef29eaSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
327013ef29eaSKashyap Desai 			    MPI3MR_RESET_FROM_EVTACK_TIMEOUT, 1);
327113ef29eaSKashyap Desai 		retval = -1;
327213ef29eaSKashyap Desai 		goto out_unlock;
327313ef29eaSKashyap Desai 	}
327413ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
327513ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
327613ef29eaSKashyap Desai 		ioc_err(mrioc,
327713ef29eaSKashyap Desai 		    "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
327813ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
327913ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
328013ef29eaSKashyap Desai 		retval = -1;
328113ef29eaSKashyap Desai 		goto out_unlock;
328213ef29eaSKashyap Desai 	}
328313ef29eaSKashyap Desai 
328413ef29eaSKashyap Desai out_unlock:
328513ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
328613ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
328713ef29eaSKashyap Desai out:
328813ef29eaSKashyap Desai 	return retval;
328913ef29eaSKashyap Desai }
329013ef29eaSKashyap Desai 
329113ef29eaSKashyap Desai /**
3292824a1566SKashyap Desai  * mpi3mr_alloc_chain_bufs - Allocate chain buffers
3293824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3294824a1566SKashyap Desai  *
3295824a1566SKashyap Desai  * Allocate chain buffers and set a bitmap to indicate free
3296824a1566SKashyap Desai  * chain buffers. Chain buffers are used to pass the SGE
3297824a1566SKashyap Desai  * information along with MPI3 SCSI IO requests for host I/O.
3298824a1566SKashyap Desai  *
3299824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure
3300824a1566SKashyap Desai  */
3301824a1566SKashyap Desai static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
3302824a1566SKashyap Desai {
3303824a1566SKashyap Desai 	int retval = 0;
3304824a1566SKashyap Desai 	u32 sz, i;
3305824a1566SKashyap Desai 	u16 num_chains;
3306824a1566SKashyap Desai 
3307fe6db615SSreekanth Reddy 	if (mrioc->chain_sgl_list)
3308fe6db615SSreekanth Reddy 		return retval;
3309fe6db615SSreekanth Reddy 
3310824a1566SKashyap Desai 	num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
3311824a1566SKashyap Desai 
331274e1f30aSKashyap Desai 	if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION
331374e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE1_PROTECTION
331474e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE2_PROTECTION
331574e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE3_PROTECTION))
331674e1f30aSKashyap Desai 		num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR);
331774e1f30aSKashyap Desai 
3318824a1566SKashyap Desai 	mrioc->chain_buf_count = num_chains;
3319824a1566SKashyap Desai 	sz = sizeof(struct chain_element) * num_chains;
3320824a1566SKashyap Desai 	mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
3321824a1566SKashyap Desai 	if (!mrioc->chain_sgl_list)
3322824a1566SKashyap Desai 		goto out_failed;
3323824a1566SKashyap Desai 
3324824a1566SKashyap Desai 	sz = MPI3MR_PAGE_SIZE_4K;
3325824a1566SKashyap Desai 	mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
3326824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
3327824a1566SKashyap Desai 	if (!mrioc->chain_buf_pool) {
3328824a1566SKashyap Desai 		ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
3329824a1566SKashyap Desai 		goto out_failed;
3330824a1566SKashyap Desai 	}
3331824a1566SKashyap Desai 
3332824a1566SKashyap Desai 	for (i = 0; i < num_chains; i++) {
3333824a1566SKashyap Desai 		mrioc->chain_sgl_list[i].addr =
3334824a1566SKashyap Desai 		    dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
3335824a1566SKashyap Desai 		    &mrioc->chain_sgl_list[i].dma_addr);
3336824a1566SKashyap Desai 
3337824a1566SKashyap Desai 		if (!mrioc->chain_sgl_list[i].addr)
3338824a1566SKashyap Desai 			goto out_failed;
3339824a1566SKashyap Desai 	}
3340824a1566SKashyap Desai 	mrioc->chain_bitmap_sz = num_chains / 8;
3341824a1566SKashyap Desai 	if (num_chains % 8)
3342824a1566SKashyap Desai 		mrioc->chain_bitmap_sz++;
3343824a1566SKashyap Desai 	mrioc->chain_bitmap = kzalloc(mrioc->chain_bitmap_sz, GFP_KERNEL);
3344824a1566SKashyap Desai 	if (!mrioc->chain_bitmap)
3345824a1566SKashyap Desai 		goto out_failed;
3346824a1566SKashyap Desai 	return retval;
3347824a1566SKashyap Desai out_failed:
3348824a1566SKashyap Desai 	retval = -1;
3349824a1566SKashyap Desai 	return retval;
3350824a1566SKashyap Desai }
3351824a1566SKashyap Desai 
3352824a1566SKashyap Desai /**
3353023ab2a9SKashyap Desai  * mpi3mr_port_enable_complete - Mark port enable complete
3354023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3355023ab2a9SKashyap Desai  * @drv_cmd: Internal command tracker
3356023ab2a9SKashyap Desai  *
3357023ab2a9SKashyap Desai  * Call back for asynchronous port enable request sets the
3358023ab2a9SKashyap Desai  * driver command to indicate port enable request is complete.
3359023ab2a9SKashyap Desai  *
3360023ab2a9SKashyap Desai  * Return: Nothing
3361023ab2a9SKashyap Desai  */
3362023ab2a9SKashyap Desai static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc,
3363023ab2a9SKashyap Desai 	struct mpi3mr_drv_cmd *drv_cmd)
3364023ab2a9SKashyap Desai {
3365023ab2a9SKashyap Desai 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3366023ab2a9SKashyap Desai 	drv_cmd->callback = NULL;
3367023ab2a9SKashyap Desai 	mrioc->scan_failed = drv_cmd->ioc_status;
3368023ab2a9SKashyap Desai 	mrioc->scan_started = 0;
3369023ab2a9SKashyap Desai }
3370023ab2a9SKashyap Desai 
3371023ab2a9SKashyap Desai /**
3372023ab2a9SKashyap Desai  * mpi3mr_issue_port_enable - Issue Port Enable
3373023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3374023ab2a9SKashyap Desai  * @async: Flag to wait for completion or not
3375023ab2a9SKashyap Desai  *
3376023ab2a9SKashyap Desai  * Issue Port Enable MPI request through admin queue and if the
3377023ab2a9SKashyap Desai  * async flag is not set wait for the completion of the port
3378023ab2a9SKashyap Desai  * enable or time out.
3379023ab2a9SKashyap Desai  *
3380023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failures.
3381023ab2a9SKashyap Desai  */
3382023ab2a9SKashyap Desai int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async)
3383023ab2a9SKashyap Desai {
3384023ab2a9SKashyap Desai 	struct mpi3_port_enable_request pe_req;
3385023ab2a9SKashyap Desai 	int retval = 0;
3386023ab2a9SKashyap Desai 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3387023ab2a9SKashyap Desai 
3388023ab2a9SKashyap Desai 	memset(&pe_req, 0, sizeof(pe_req));
3389023ab2a9SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3390023ab2a9SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3391023ab2a9SKashyap Desai 		retval = -1;
3392023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Init command is in use\n");
3393023ab2a9SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3394023ab2a9SKashyap Desai 		goto out;
3395023ab2a9SKashyap Desai 	}
3396023ab2a9SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3397023ab2a9SKashyap Desai 	if (async) {
3398023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
3399023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = mpi3mr_port_enable_complete;
3400023ab2a9SKashyap Desai 	} else {
3401023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 1;
3402023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = NULL;
3403023ab2a9SKashyap Desai 		init_completion(&mrioc->init_cmds.done);
3404023ab2a9SKashyap Desai 	}
3405023ab2a9SKashyap Desai 	pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3406023ab2a9SKashyap Desai 	pe_req.function = MPI3_FUNCTION_PORT_ENABLE;
3407023ab2a9SKashyap Desai 
3408023ab2a9SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1);
3409023ab2a9SKashyap Desai 	if (retval) {
3410023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n");
3411023ab2a9SKashyap Desai 		goto out_unlock;
3412023ab2a9SKashyap Desai 	}
3413a6856cc4SSreekanth Reddy 	if (async) {
3414a6856cc4SSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
3415a6856cc4SSreekanth Reddy 		goto out;
3416a6856cc4SSreekanth Reddy 	}
3417a6856cc4SSreekanth Reddy 
3418a6856cc4SSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ));
3419023ab2a9SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3420a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "port enable timed out\n");
3421023ab2a9SKashyap Desai 		retval = -1;
3422a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT);
3423023ab2a9SKashyap Desai 		goto out_unlock;
3424023ab2a9SKashyap Desai 	}
3425023ab2a9SKashyap Desai 	mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds);
3426a6856cc4SSreekanth Reddy 
3427023ab2a9SKashyap Desai out_unlock:
3428a6856cc4SSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3429023ab2a9SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3430023ab2a9SKashyap Desai out:
3431023ab2a9SKashyap Desai 	return retval;
3432023ab2a9SKashyap Desai }
3433023ab2a9SKashyap Desai 
3434ff9561e9SKashyap Desai /* Protocol type to name mapper structure */
3435ff9561e9SKashyap Desai static const struct {
3436ff9561e9SKashyap Desai 	u8 protocol;
3437ff9561e9SKashyap Desai 	char *name;
3438ff9561e9SKashyap Desai } mpi3mr_protocols[] = {
3439ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" },
3440ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" },
3441ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" },
3442ff9561e9SKashyap Desai };
3443ff9561e9SKashyap Desai 
3444ff9561e9SKashyap Desai /* Capability to name mapper structure*/
3445ff9561e9SKashyap Desai static const struct {
3446ff9561e9SKashyap Desai 	u32 capability;
3447ff9561e9SKashyap Desai 	char *name;
3448ff9561e9SKashyap Desai } mpi3mr_capabilities[] = {
3449ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE, "RAID" },
3450ff9561e9SKashyap Desai };
3451ff9561e9SKashyap Desai 
3452ff9561e9SKashyap Desai /**
3453ff9561e9SKashyap Desai  * mpi3mr_print_ioc_info - Display controller information
3454ff9561e9SKashyap Desai  * @mrioc: Adapter instance reference
3455ff9561e9SKashyap Desai  *
3456ff9561e9SKashyap Desai  * Display controller personalit, capability, supported
3457ff9561e9SKashyap Desai  * protocols etc.
3458ff9561e9SKashyap Desai  *
3459ff9561e9SKashyap Desai  * Return: Nothing
3460ff9561e9SKashyap Desai  */
3461ff9561e9SKashyap Desai static void
3462ff9561e9SKashyap Desai mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc)
3463ff9561e9SKashyap Desai {
346476a4f7ccSDan Carpenter 	int i = 0, bytes_written = 0;
3465ff9561e9SKashyap Desai 	char personality[16];
3466ff9561e9SKashyap Desai 	char protocol[50] = {0};
3467ff9561e9SKashyap Desai 	char capabilities[100] = {0};
3468ff9561e9SKashyap Desai 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
3469ff9561e9SKashyap Desai 
3470ff9561e9SKashyap Desai 	switch (mrioc->facts.personality) {
3471ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
3472ff9561e9SKashyap Desai 		strncpy(personality, "Enhanced HBA", sizeof(personality));
3473ff9561e9SKashyap Desai 		break;
3474ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
3475ff9561e9SKashyap Desai 		strncpy(personality, "RAID", sizeof(personality));
3476ff9561e9SKashyap Desai 		break;
3477ff9561e9SKashyap Desai 	default:
3478ff9561e9SKashyap Desai 		strncpy(personality, "Unknown", sizeof(personality));
3479ff9561e9SKashyap Desai 		break;
3480ff9561e9SKashyap Desai 	}
3481ff9561e9SKashyap Desai 
3482ff9561e9SKashyap Desai 	ioc_info(mrioc, "Running in %s Personality", personality);
3483ff9561e9SKashyap Desai 
3484ff9561e9SKashyap Desai 	ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n",
3485ff9561e9SKashyap Desai 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
3486ff9561e9SKashyap Desai 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
3487ff9561e9SKashyap Desai 
3488ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) {
3489ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3490ff9561e9SKashyap Desai 		    mpi3mr_protocols[i].protocol) {
349130e99f05SDan Carpenter 			bytes_written += scnprintf(protocol + bytes_written,
349276a4f7ccSDan Carpenter 				    sizeof(protocol) - bytes_written, "%s%s",
349376a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3494ff9561e9SKashyap Desai 				    mpi3mr_protocols[i].name);
3495ff9561e9SKashyap Desai 		}
3496ff9561e9SKashyap Desai 	}
3497ff9561e9SKashyap Desai 
349876a4f7ccSDan Carpenter 	bytes_written = 0;
3499ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) {
3500ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3501ff9561e9SKashyap Desai 		    mpi3mr_capabilities[i].capability) {
350230e99f05SDan Carpenter 			bytes_written += scnprintf(capabilities + bytes_written,
350376a4f7ccSDan Carpenter 				    sizeof(capabilities) - bytes_written, "%s%s",
350476a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3505ff9561e9SKashyap Desai 				    mpi3mr_capabilities[i].name);
3506ff9561e9SKashyap Desai 		}
3507ff9561e9SKashyap Desai 	}
3508ff9561e9SKashyap Desai 
3509ff9561e9SKashyap Desai 	ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n",
3510ff9561e9SKashyap Desai 		 protocol, capabilities);
3511ff9561e9SKashyap Desai }
3512ff9561e9SKashyap Desai 
3513023ab2a9SKashyap Desai /**
3514824a1566SKashyap Desai  * mpi3mr_cleanup_resources - Free PCI resources
3515824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3516824a1566SKashyap Desai  *
3517824a1566SKashyap Desai  * Unmap PCI device memory and disable PCI device.
3518824a1566SKashyap Desai  *
3519824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3520824a1566SKashyap Desai  */
3521824a1566SKashyap Desai void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
3522824a1566SKashyap Desai {
3523824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3524824a1566SKashyap Desai 
3525824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
3526824a1566SKashyap Desai 
3527824a1566SKashyap Desai 	if (mrioc->sysif_regs) {
3528824a1566SKashyap Desai 		iounmap((void __iomem *)mrioc->sysif_regs);
3529824a1566SKashyap Desai 		mrioc->sysif_regs = NULL;
3530824a1566SKashyap Desai 	}
3531824a1566SKashyap Desai 
3532824a1566SKashyap Desai 	if (pci_is_enabled(pdev)) {
3533824a1566SKashyap Desai 		if (mrioc->bars)
3534824a1566SKashyap Desai 			pci_release_selected_regions(pdev, mrioc->bars);
3535824a1566SKashyap Desai 		pci_disable_device(pdev);
3536824a1566SKashyap Desai 	}
3537824a1566SKashyap Desai }
3538824a1566SKashyap Desai 
3539824a1566SKashyap Desai /**
3540824a1566SKashyap Desai  * mpi3mr_setup_resources - Enable PCI resources
3541824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3542824a1566SKashyap Desai  *
3543824a1566SKashyap Desai  * Enable PCI device memory, MSI-x registers and set DMA mask.
3544824a1566SKashyap Desai  *
3545824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3546824a1566SKashyap Desai  */
3547824a1566SKashyap Desai int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
3548824a1566SKashyap Desai {
3549824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3550824a1566SKashyap Desai 	u32 memap_sz = 0;
3551824a1566SKashyap Desai 	int i, retval = 0, capb = 0;
3552824a1566SKashyap Desai 	u16 message_control;
3553824a1566SKashyap Desai 	u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
3554824a1566SKashyap Desai 	    (((dma_get_required_mask(&pdev->dev) > DMA_BIT_MASK(32)) &&
3555824a1566SKashyap Desai 	    (sizeof(dma_addr_t) > 4)) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
3556824a1566SKashyap Desai 
3557824a1566SKashyap Desai 	if (pci_enable_device_mem(pdev)) {
3558824a1566SKashyap Desai 		ioc_err(mrioc, "pci_enable_device_mem: failed\n");
3559824a1566SKashyap Desai 		retval = -ENODEV;
3560824a1566SKashyap Desai 		goto out_failed;
3561824a1566SKashyap Desai 	}
3562824a1566SKashyap Desai 
3563824a1566SKashyap Desai 	capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3564824a1566SKashyap Desai 	if (!capb) {
3565824a1566SKashyap Desai 		ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
3566824a1566SKashyap Desai 		retval = -ENODEV;
3567824a1566SKashyap Desai 		goto out_failed;
3568824a1566SKashyap Desai 	}
3569824a1566SKashyap Desai 	mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3570824a1566SKashyap Desai 
3571824a1566SKashyap Desai 	if (pci_request_selected_regions(pdev, mrioc->bars,
3572824a1566SKashyap Desai 	    mrioc->driver_name)) {
3573824a1566SKashyap Desai 		ioc_err(mrioc, "pci_request_selected_regions: failed\n");
3574824a1566SKashyap Desai 		retval = -ENODEV;
3575824a1566SKashyap Desai 		goto out_failed;
3576824a1566SKashyap Desai 	}
3577824a1566SKashyap Desai 
3578824a1566SKashyap Desai 	for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
3579824a1566SKashyap Desai 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3580824a1566SKashyap Desai 			mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
3581824a1566SKashyap Desai 			memap_sz = pci_resource_len(pdev, i);
3582824a1566SKashyap Desai 			mrioc->sysif_regs =
3583824a1566SKashyap Desai 			    ioremap(mrioc->sysif_regs_phys, memap_sz);
3584824a1566SKashyap Desai 			break;
3585824a1566SKashyap Desai 		}
3586824a1566SKashyap Desai 	}
3587824a1566SKashyap Desai 
3588824a1566SKashyap Desai 	pci_set_master(pdev);
3589824a1566SKashyap Desai 
3590824a1566SKashyap Desai 	retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
3591824a1566SKashyap Desai 	if (retval) {
3592824a1566SKashyap Desai 		if (dma_mask != DMA_BIT_MASK(32)) {
3593824a1566SKashyap Desai 			ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
3594824a1566SKashyap Desai 			dma_mask = DMA_BIT_MASK(32);
3595824a1566SKashyap Desai 			retval = dma_set_mask_and_coherent(&pdev->dev,
3596824a1566SKashyap Desai 			    dma_mask);
3597824a1566SKashyap Desai 		}
3598824a1566SKashyap Desai 		if (retval) {
3599824a1566SKashyap Desai 			mrioc->dma_mask = 0;
3600824a1566SKashyap Desai 			ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
3601824a1566SKashyap Desai 			goto out_failed;
3602824a1566SKashyap Desai 		}
3603824a1566SKashyap Desai 	}
3604824a1566SKashyap Desai 	mrioc->dma_mask = dma_mask;
3605824a1566SKashyap Desai 
3606824a1566SKashyap Desai 	if (!mrioc->sysif_regs) {
3607824a1566SKashyap Desai 		ioc_err(mrioc,
3608824a1566SKashyap Desai 		    "Unable to map adapter memory or resource not found\n");
3609824a1566SKashyap Desai 		retval = -EINVAL;
3610824a1566SKashyap Desai 		goto out_failed;
3611824a1566SKashyap Desai 	}
3612824a1566SKashyap Desai 
3613824a1566SKashyap Desai 	pci_read_config_word(pdev, capb + 2, &message_control);
3614824a1566SKashyap Desai 	mrioc->msix_count = (message_control & 0x3FF) + 1;
3615824a1566SKashyap Desai 
3616824a1566SKashyap Desai 	pci_save_state(pdev);
3617824a1566SKashyap Desai 
3618824a1566SKashyap Desai 	pci_set_drvdata(pdev, mrioc->shost);
3619824a1566SKashyap Desai 
3620824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
3621824a1566SKashyap Desai 
3622824a1566SKashyap Desai 	ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
3623824a1566SKashyap Desai 	    (unsigned long long)mrioc->sysif_regs_phys,
3624824a1566SKashyap Desai 	    mrioc->sysif_regs, memap_sz);
3625824a1566SKashyap Desai 	ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
3626824a1566SKashyap Desai 	    mrioc->msix_count);
3627afd3a579SSreekanth Reddy 
3628afd3a579SSreekanth Reddy 	if (!reset_devices && poll_queues > 0)
3629afd3a579SSreekanth Reddy 		mrioc->requested_poll_qcount = min_t(int, poll_queues,
3630afd3a579SSreekanth Reddy 				mrioc->msix_count - 2);
3631824a1566SKashyap Desai 	return retval;
3632824a1566SKashyap Desai 
3633824a1566SKashyap Desai out_failed:
3634824a1566SKashyap Desai 	mpi3mr_cleanup_resources(mrioc);
3635824a1566SKashyap Desai 	return retval;
3636824a1566SKashyap Desai }
3637824a1566SKashyap Desai 
3638824a1566SKashyap Desai /**
3639e3605f65SSreekanth Reddy  * mpi3mr_enable_events - Enable required events
3640e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
3641e3605f65SSreekanth Reddy  *
3642e3605f65SSreekanth Reddy  * This routine unmasks the events required by the driver by
3643e3605f65SSreekanth Reddy  * sennding appropriate event mask bitmapt through an event
3644e3605f65SSreekanth Reddy  * notification request.
3645e3605f65SSreekanth Reddy  *
3646e3605f65SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3647e3605f65SSreekanth Reddy  */
3648e3605f65SSreekanth Reddy static int mpi3mr_enable_events(struct mpi3mr_ioc *mrioc)
3649e3605f65SSreekanth Reddy {
3650e3605f65SSreekanth Reddy 	int retval = 0;
3651e3605f65SSreekanth Reddy 	u32  i;
3652e3605f65SSreekanth Reddy 
3653e3605f65SSreekanth Reddy 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3654e3605f65SSreekanth Reddy 		mrioc->event_masks[i] = -1;
3655e3605f65SSreekanth Reddy 
3656e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED);
3657e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED);
3658e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
3659e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
3660e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3661e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY);
3662e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
3663e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
3664e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
3665e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION);
366678b76a07SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PREPARE_FOR_RESET);
3667e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT);
3668e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE);
3669e3605f65SSreekanth Reddy 
3670e3605f65SSreekanth Reddy 	retval = mpi3mr_issue_event_notification(mrioc);
3671e3605f65SSreekanth Reddy 	if (retval)
3672e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to issue event notification %d\n",
3673e3605f65SSreekanth Reddy 		    retval);
3674e3605f65SSreekanth Reddy 	return retval;
3675e3605f65SSreekanth Reddy }
3676e3605f65SSreekanth Reddy 
3677e3605f65SSreekanth Reddy /**
3678824a1566SKashyap Desai  * mpi3mr_init_ioc - Initialize the controller
3679824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3680824a1566SKashyap Desai  *
3681824a1566SKashyap Desai  * This the controller initialization routine, executed either
3682824a1566SKashyap Desai  * after soft reset or from pci probe callback.
3683824a1566SKashyap Desai  * Setup the required resources, memory map the controller
3684824a1566SKashyap Desai  * registers, create admin and operational reply queue pairs,
3685824a1566SKashyap Desai  * allocate required memory for reply pool, sense buffer pool,
3686824a1566SKashyap Desai  * issue IOC init request to the firmware, unmask the events and
3687824a1566SKashyap Desai  * issue port enable to discover SAS/SATA/NVMe devies and RAID
3688824a1566SKashyap Desai  * volumes.
3689824a1566SKashyap Desai  *
3690824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3691824a1566SKashyap Desai  */
3692fe6db615SSreekanth Reddy int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc)
3693824a1566SKashyap Desai {
3694824a1566SKashyap Desai 	int retval = 0;
3695fe6db615SSreekanth Reddy 	u8 retry = 0;
3696824a1566SKashyap Desai 	struct mpi3_ioc_facts_data facts_data;
3697f10af057SSreekanth Reddy 	u32 sz;
3698824a1566SKashyap Desai 
3699fe6db615SSreekanth Reddy retry_init:
3700824a1566SKashyap Desai 	retval = mpi3mr_bring_ioc_ready(mrioc);
3701824a1566SKashyap Desai 	if (retval) {
3702824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
3703824a1566SKashyap Desai 		    retval);
3704fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3705824a1566SKashyap Desai 	}
3706824a1566SKashyap Desai 
3707824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 1);
3708824a1566SKashyap Desai 	if (retval) {
3709824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to setup ISR error %d\n",
3710824a1566SKashyap Desai 		    retval);
3711fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3712824a1566SKashyap Desai 	}
3713824a1566SKashyap Desai 
3714824a1566SKashyap Desai 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3715824a1566SKashyap Desai 	if (retval) {
3716824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
3717824a1566SKashyap Desai 		    retval);
3718824a1566SKashyap Desai 		goto out_failed;
3719824a1566SKashyap Desai 	}
3720824a1566SKashyap Desai 
3721c5758fc7SSreekanth Reddy 	mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
3722c5758fc7SSreekanth Reddy 
3723f10af057SSreekanth Reddy 	mrioc->num_io_throttle_group = mrioc->facts.max_io_throttle_group;
3724f10af057SSreekanth Reddy 	atomic_set(&mrioc->pend_large_data_sz, 0);
3725f10af057SSreekanth Reddy 
3726c5758fc7SSreekanth Reddy 	if (reset_devices)
3727c5758fc7SSreekanth Reddy 		mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
3728c5758fc7SSreekanth Reddy 		    MPI3MR_HOST_IOS_KDUMP);
3729c5758fc7SSreekanth Reddy 
3730c5758fc7SSreekanth Reddy 	mrioc->reply_sz = mrioc->facts.reply_sz;
3731fe6db615SSreekanth Reddy 
3732824a1566SKashyap Desai 	retval = mpi3mr_check_reset_dma_mask(mrioc);
3733824a1566SKashyap Desai 	if (retval) {
3734824a1566SKashyap Desai 		ioc_err(mrioc, "Resetting dma mask failed %d\n",
3735824a1566SKashyap Desai 		    retval);
3736fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3737fb9b0457SKashyap Desai 	}
3738824a1566SKashyap Desai 
3739ff9561e9SKashyap Desai 	mpi3mr_print_ioc_info(mrioc);
3740ff9561e9SKashyap Desai 
3741824a1566SKashyap Desai 	retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
3742824a1566SKashyap Desai 	if (retval) {
3743824a1566SKashyap Desai 		ioc_err(mrioc,
3744824a1566SKashyap Desai 		    "%s :Failed to allocated reply sense buffers %d\n",
3745824a1566SKashyap Desai 		    __func__, retval);
3746fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3747824a1566SKashyap Desai 	}
3748824a1566SKashyap Desai 
3749824a1566SKashyap Desai 	retval = mpi3mr_alloc_chain_bufs(mrioc);
3750824a1566SKashyap Desai 	if (retval) {
3751824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
3752824a1566SKashyap Desai 		    retval);
3753fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3754fb9b0457SKashyap Desai 	}
3755824a1566SKashyap Desai 
3756824a1566SKashyap Desai 	retval = mpi3mr_issue_iocinit(mrioc);
3757824a1566SKashyap Desai 	if (retval) {
3758824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
3759824a1566SKashyap Desai 		    retval);
3760824a1566SKashyap Desai 		goto out_failed;
3761824a1566SKashyap Desai 	}
3762824a1566SKashyap Desai 
37632ac794baSSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
37642ac794baSSreekanth Reddy 	if (retval) {
37652ac794baSSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
37662ac794baSSreekanth Reddy 		goto out_failed;
37672ac794baSSreekanth Reddy 	}
37682ac794baSSreekanth Reddy 
3769824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 0);
3770824a1566SKashyap Desai 	if (retval) {
3771824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
3772824a1566SKashyap Desai 		    retval);
3773fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3774fb9b0457SKashyap Desai 	}
3775824a1566SKashyap Desai 
3776c9566231SKashyap Desai 	retval = mpi3mr_create_op_queues(mrioc);
3777c9566231SKashyap Desai 	if (retval) {
3778c9566231SKashyap Desai 		ioc_err(mrioc, "Failed to create OpQueues error %d\n",
3779c9566231SKashyap Desai 		    retval);
3780c9566231SKashyap Desai 		goto out_failed;
3781c9566231SKashyap Desai 	}
3782c9566231SKashyap Desai 
378343ca1100SSumit Saxena 	if (!mrioc->pel_seqnum_virt) {
378443ca1100SSumit Saxena 		dprint_init(mrioc, "allocating memory for pel_seqnum_virt\n");
378543ca1100SSumit Saxena 		mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
378643ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
378743ca1100SSumit Saxena 		    mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
378843ca1100SSumit Saxena 		    GFP_KERNEL);
3789bc7896d3SDan Carpenter 		if (!mrioc->pel_seqnum_virt) {
3790bc7896d3SDan Carpenter 			retval = -ENOMEM;
379143ca1100SSumit Saxena 			goto out_failed_noretry;
379243ca1100SSumit Saxena 		}
3793bc7896d3SDan Carpenter 	}
379443ca1100SSumit Saxena 
3795f10af057SSreekanth Reddy 	if (!mrioc->throttle_groups && mrioc->num_io_throttle_group) {
3796f10af057SSreekanth Reddy 		dprint_init(mrioc, "allocating memory for throttle groups\n");
3797f10af057SSreekanth Reddy 		sz = sizeof(struct mpi3mr_throttle_group_info);
3798f10af057SSreekanth Reddy 		mrioc->throttle_groups = (struct mpi3mr_throttle_group_info *)
3799f10af057SSreekanth Reddy 		    kcalloc(mrioc->num_io_throttle_group, sz, GFP_KERNEL);
3800f10af057SSreekanth Reddy 		if (!mrioc->throttle_groups)
3801f10af057SSreekanth Reddy 			goto out_failed_noretry;
3802f10af057SSreekanth Reddy 	}
3803f10af057SSreekanth Reddy 
3804e3605f65SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
380513ef29eaSKashyap Desai 	if (retval) {
3806e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events %d\n",
380713ef29eaSKashyap Desai 		    retval);
380813ef29eaSKashyap Desai 		goto out_failed;
380913ef29eaSKashyap Desai 	}
381013ef29eaSKashyap Desai 
3811fe6db615SSreekanth Reddy 	ioc_info(mrioc, "controller initialization completed successfully\n");
3812824a1566SKashyap Desai 	return retval;
3813824a1566SKashyap Desai out_failed:
3814fe6db615SSreekanth Reddy 	if (retry < 2) {
3815fe6db615SSreekanth Reddy 		retry++;
3816fe6db615SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller initialization, retry_count:%d\n",
3817fe6db615SSreekanth Reddy 		    retry);
3818fe6db615SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3819fe6db615SSreekanth Reddy 		goto retry_init;
3820fe6db615SSreekanth Reddy 	}
3821fe6db615SSreekanth Reddy out_failed_noretry:
3822fe6db615SSreekanth Reddy 	ioc_err(mrioc, "controller initialization failed\n");
3823fe6db615SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3824fe6db615SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3825fe6db615SSreekanth Reddy 	mrioc->unrecoverable = 1;
3826824a1566SKashyap Desai 	return retval;
3827824a1566SKashyap Desai }
3828824a1566SKashyap Desai 
3829c0b00a93SSreekanth Reddy /**
3830c0b00a93SSreekanth Reddy  * mpi3mr_reinit_ioc - Re-Initialize the controller
3831c0b00a93SSreekanth Reddy  * @mrioc: Adapter instance reference
3832c0b00a93SSreekanth Reddy  * @is_resume: Called from resume or reset path
3833c0b00a93SSreekanth Reddy  *
3834c0b00a93SSreekanth Reddy  * This the controller re-initialization routine, executed from
3835c0b00a93SSreekanth Reddy  * the soft reset handler or resume callback. Creates
3836c0b00a93SSreekanth Reddy  * operational reply queue pairs, allocate required memory for
3837c0b00a93SSreekanth Reddy  * reply pool, sense buffer pool, issue IOC init request to the
3838c0b00a93SSreekanth Reddy  * firmware, unmask the events and issue port enable to discover
3839c0b00a93SSreekanth Reddy  * SAS/SATA/NVMe devices and RAID volumes.
3840c0b00a93SSreekanth Reddy  *
3841c0b00a93SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3842c0b00a93SSreekanth Reddy  */
3843fe6db615SSreekanth Reddy int mpi3mr_reinit_ioc(struct mpi3mr_ioc *mrioc, u8 is_resume)
3844fe6db615SSreekanth Reddy {
3845c0b00a93SSreekanth Reddy 	int retval = 0;
3846c0b00a93SSreekanth Reddy 	u8 retry = 0;
3847c0b00a93SSreekanth Reddy 	struct mpi3_ioc_facts_data facts_data;
3848fe6db615SSreekanth Reddy 
3849c0b00a93SSreekanth Reddy retry_init:
3850c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "bringing up the controller to ready state\n");
3851c0b00a93SSreekanth Reddy 	retval = mpi3mr_bring_ioc_ready(mrioc);
3852c0b00a93SSreekanth Reddy 	if (retval) {
3853c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to bring to ready state\n");
3854c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3855c0b00a93SSreekanth Reddy 	}
3856c0b00a93SSreekanth Reddy 
3857c0b00a93SSreekanth Reddy 	if (is_resume) {
3858c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up single ISR\n");
3859c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 1);
3860c0b00a93SSreekanth Reddy 		if (retval) {
3861c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to setup ISR\n");
3862c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3863c0b00a93SSreekanth Reddy 		}
3864c0b00a93SSreekanth Reddy 	} else
3865c0b00a93SSreekanth Reddy 		mpi3mr_ioc_enable_intr(mrioc);
3866c0b00a93SSreekanth Reddy 
3867c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting ioc_facts\n");
3868c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3869c0b00a93SSreekanth Reddy 	if (retval) {
3870c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get ioc_facts\n");
3871c0b00a93SSreekanth Reddy 		goto out_failed;
3872c0b00a93SSreekanth Reddy 	}
3873c0b00a93SSreekanth Reddy 
3874c5758fc7SSreekanth Reddy 	dprint_reset(mrioc, "validating ioc_facts\n");
3875c5758fc7SSreekanth Reddy 	retval = mpi3mr_revalidate_factsdata(mrioc);
3876c5758fc7SSreekanth Reddy 	if (retval) {
3877c5758fc7SSreekanth Reddy 		ioc_err(mrioc, "failed to revalidate ioc_facts data\n");
3878c5758fc7SSreekanth Reddy 		goto out_failed_noretry;
3879c5758fc7SSreekanth Reddy 	}
3880c0b00a93SSreekanth Reddy 
3881c0b00a93SSreekanth Reddy 	mpi3mr_print_ioc_info(mrioc);
3882c0b00a93SSreekanth Reddy 
3883c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "sending ioc_init\n");
3884c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocinit(mrioc);
3885c0b00a93SSreekanth Reddy 	if (retval) {
3886c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to send ioc_init\n");
3887c0b00a93SSreekanth Reddy 		goto out_failed;
3888c0b00a93SSreekanth Reddy 	}
3889c0b00a93SSreekanth Reddy 
3890c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting package version\n");
3891c0b00a93SSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
3892c0b00a93SSreekanth Reddy 	if (retval) {
3893c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
3894c0b00a93SSreekanth Reddy 		goto out_failed;
3895c0b00a93SSreekanth Reddy 	}
3896c0b00a93SSreekanth Reddy 
3897c0b00a93SSreekanth Reddy 	if (is_resume) {
3898c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up multiple ISR\n");
3899c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 0);
3900c0b00a93SSreekanth Reddy 		if (retval) {
3901c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to re-setup ISR\n");
3902c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3903c0b00a93SSreekanth Reddy 		}
3904c0b00a93SSreekanth Reddy 	}
3905c0b00a93SSreekanth Reddy 
3906c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "creating operational queue pairs\n");
3907c0b00a93SSreekanth Reddy 	retval = mpi3mr_create_op_queues(mrioc);
3908c0b00a93SSreekanth Reddy 	if (retval) {
3909c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to create operational queue pairs\n");
3910c0b00a93SSreekanth Reddy 		goto out_failed;
3911c0b00a93SSreekanth Reddy 	}
3912c0b00a93SSreekanth Reddy 
391343ca1100SSumit Saxena 	if (!mrioc->pel_seqnum_virt) {
391443ca1100SSumit Saxena 		dprint_reset(mrioc, "allocating memory for pel_seqnum_virt\n");
391543ca1100SSumit Saxena 		mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
391643ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
391743ca1100SSumit Saxena 		    mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
391843ca1100SSumit Saxena 		    GFP_KERNEL);
3919bc7896d3SDan Carpenter 		if (!mrioc->pel_seqnum_virt) {
3920bc7896d3SDan Carpenter 			retval = -ENOMEM;
392143ca1100SSumit Saxena 			goto out_failed_noretry;
392243ca1100SSumit Saxena 		}
3923bc7896d3SDan Carpenter 	}
392443ca1100SSumit Saxena 
3925c0b00a93SSreekanth Reddy 	if (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q) {
3926c0b00a93SSreekanth Reddy 		ioc_err(mrioc,
39275867b856SColin Ian King 		    "cannot create minimum number of operational queues expected:%d created:%d\n",
3928c0b00a93SSreekanth Reddy 		    mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q);
3929c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3930c0b00a93SSreekanth Reddy 	}
3931c0b00a93SSreekanth Reddy 
3932c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "enabling events\n");
3933c0b00a93SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
3934c0b00a93SSreekanth Reddy 	if (retval) {
3935c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events\n");
3936c0b00a93SSreekanth Reddy 		goto out_failed;
3937c0b00a93SSreekanth Reddy 	}
3938c0b00a93SSreekanth Reddy 
3939c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "sending port enable\n");
3940c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_port_enable(mrioc, 0);
3941c0b00a93SSreekanth Reddy 	if (retval) {
3942c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to issue port enable\n");
3943c0b00a93SSreekanth Reddy 		goto out_failed;
3944c0b00a93SSreekanth Reddy 	}
3945c0b00a93SSreekanth Reddy 
3946c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "controller %s completed successfully\n",
3947c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
3948c0b00a93SSreekanth Reddy 	return retval;
3949c0b00a93SSreekanth Reddy out_failed:
3950c0b00a93SSreekanth Reddy 	if (retry < 2) {
3951c0b00a93SSreekanth Reddy 		retry++;
3952c0b00a93SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller %s, retry_count:%d\n",
3953c0b00a93SSreekanth Reddy 		    (is_resume)?"resume":"re-initialization", retry);
3954c0b00a93SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3955c0b00a93SSreekanth Reddy 		goto retry_init;
3956c0b00a93SSreekanth Reddy 	}
3957c0b00a93SSreekanth Reddy out_failed_noretry:
3958c0b00a93SSreekanth Reddy 	ioc_err(mrioc, "controller %s is failed\n",
3959c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
3960c0b00a93SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3961c0b00a93SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3962c0b00a93SSreekanth Reddy 	mrioc->unrecoverable = 1;
3963c0b00a93SSreekanth Reddy 	return retval;
3964fe6db615SSreekanth Reddy }
3965fe6db615SSreekanth Reddy 
3966824a1566SKashyap Desai /**
3967fb9b0457SKashyap Desai  * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's
3968fb9b0457SKashyap Desai  *					segments
3969fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
3970fb9b0457SKashyap Desai  * @qidx: Operational reply queue index
3971fb9b0457SKashyap Desai  *
3972fb9b0457SKashyap Desai  * Return: Nothing.
3973fb9b0457SKashyap Desai  */
3974fb9b0457SKashyap Desai static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3975fb9b0457SKashyap Desai {
3976fb9b0457SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
3977fb9b0457SKashyap Desai 	struct segments *segments;
3978fb9b0457SKashyap Desai 	int i, size;
3979fb9b0457SKashyap Desai 
3980fb9b0457SKashyap Desai 	if (!op_reply_q->q_segments)
3981fb9b0457SKashyap Desai 		return;
3982fb9b0457SKashyap Desai 
3983fb9b0457SKashyap Desai 	size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz;
3984fb9b0457SKashyap Desai 	segments = op_reply_q->q_segments;
3985fb9b0457SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++)
3986fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
3987fb9b0457SKashyap Desai }
3988fb9b0457SKashyap Desai 
3989fb9b0457SKashyap Desai /**
3990fb9b0457SKashyap Desai  * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's
3991fb9b0457SKashyap Desai  *					segments
3992fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
3993fb9b0457SKashyap Desai  * @qidx: Operational request queue index
3994fb9b0457SKashyap Desai  *
3995fb9b0457SKashyap Desai  * Return: Nothing.
3996fb9b0457SKashyap Desai  */
3997fb9b0457SKashyap Desai static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
3998fb9b0457SKashyap Desai {
3999fb9b0457SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
4000fb9b0457SKashyap Desai 	struct segments *segments;
4001fb9b0457SKashyap Desai 	int i, size;
4002fb9b0457SKashyap Desai 
4003fb9b0457SKashyap Desai 	if (!op_req_q->q_segments)
4004fb9b0457SKashyap Desai 		return;
4005fb9b0457SKashyap Desai 
4006fb9b0457SKashyap Desai 	size = op_req_q->segment_qd * mrioc->facts.op_req_sz;
4007fb9b0457SKashyap Desai 	segments = op_req_q->q_segments;
4008fb9b0457SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++)
4009fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
4010fb9b0457SKashyap Desai }
4011fb9b0457SKashyap Desai 
4012fb9b0457SKashyap Desai /**
4013fb9b0457SKashyap Desai  * mpi3mr_memset_buffers - memset memory for a controller
4014fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4015fb9b0457SKashyap Desai  *
4016fb9b0457SKashyap Desai  * clear all the memory allocated for a controller, typically
4017fb9b0457SKashyap Desai  * called post reset to reuse the memory allocated during the
4018fb9b0457SKashyap Desai  * controller init.
4019fb9b0457SKashyap Desai  *
4020fb9b0457SKashyap Desai  * Return: Nothing.
4021fb9b0457SKashyap Desai  */
40220da66348SKashyap Desai void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)
4023fb9b0457SKashyap Desai {
4024fb9b0457SKashyap Desai 	u16 i;
4025f10af057SSreekanth Reddy 	struct mpi3mr_throttle_group_info *tg;
4026fb9b0457SKashyap Desai 
4027fe6db615SSreekanth Reddy 	mrioc->change_count = 0;
4028afd3a579SSreekanth Reddy 	mrioc->active_poll_qcount = 0;
4029afd3a579SSreekanth Reddy 	mrioc->default_qcount = 0;
4030fe6db615SSreekanth Reddy 	if (mrioc->admin_req_base)
4031fb9b0457SKashyap Desai 		memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz);
4032fe6db615SSreekanth Reddy 	if (mrioc->admin_reply_base)
4033fb9b0457SKashyap Desai 		memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz);
4034fb9b0457SKashyap Desai 
4035fe6db615SSreekanth Reddy 	if (mrioc->init_cmds.reply) {
4036fb9b0457SKashyap Desai 		memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply));
4037f5e6d5a3SSumit Saxena 		memset(mrioc->bsg_cmds.reply, 0,
4038f5e6d5a3SSumit Saxena 		    sizeof(*mrioc->bsg_cmds.reply));
4039e844adb1SKashyap Desai 		memset(mrioc->host_tm_cmds.reply, 0,
4040e844adb1SKashyap Desai 		    sizeof(*mrioc->host_tm_cmds.reply));
404143ca1100SSumit Saxena 		memset(mrioc->pel_cmds.reply, 0,
404243ca1100SSumit Saxena 		    sizeof(*mrioc->pel_cmds.reply));
404343ca1100SSumit Saxena 		memset(mrioc->pel_abort_cmd.reply, 0,
404443ca1100SSumit Saxena 		    sizeof(*mrioc->pel_abort_cmd.reply));
4045fb9b0457SKashyap Desai 		for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
4046fb9b0457SKashyap Desai 			memset(mrioc->dev_rmhs_cmds[i].reply, 0,
4047fb9b0457SKashyap Desai 			    sizeof(*mrioc->dev_rmhs_cmds[i].reply));
4048c1af985dSSreekanth Reddy 		for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
4049c1af985dSSreekanth Reddy 			memset(mrioc->evtack_cmds[i].reply, 0,
4050c1af985dSSreekanth Reddy 			    sizeof(*mrioc->evtack_cmds[i].reply));
4051fb9b0457SKashyap Desai 		memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
4052fb9b0457SKashyap Desai 		memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
4053c1af985dSSreekanth Reddy 		memset(mrioc->evtack_cmds_bitmap, 0,
4054c1af985dSSreekanth Reddy 		    mrioc->evtack_cmds_bitmap_sz);
4055fe6db615SSreekanth Reddy 	}
4056fb9b0457SKashyap Desai 
4057fb9b0457SKashyap Desai 	for (i = 0; i < mrioc->num_queues; i++) {
4058fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].qid = 0;
4059fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ci = 0;
4060fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].num_replies = 0;
4061fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ephase = 0;
4062463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
4063463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0);
4064fb9b0457SKashyap Desai 		mpi3mr_memset_op_reply_q_buffers(mrioc, i);
4065fb9b0457SKashyap Desai 
4066fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].ci = 0;
4067fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].pi = 0;
4068fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].num_requests = 0;
4069fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].qid = 0;
4070fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].reply_qid = 0;
4071fb9b0457SKashyap Desai 		spin_lock_init(&mrioc->req_qinfo[i].q_lock);
4072fb9b0457SKashyap Desai 		mpi3mr_memset_op_req_q_buffers(mrioc, i);
4073fb9b0457SKashyap Desai 	}
4074f10af057SSreekanth Reddy 
4075f10af057SSreekanth Reddy 	atomic_set(&mrioc->pend_large_data_sz, 0);
4076f10af057SSreekanth Reddy 	if (mrioc->throttle_groups) {
4077f10af057SSreekanth Reddy 		tg = mrioc->throttle_groups;
4078f10af057SSreekanth Reddy 		for (i = 0; i < mrioc->num_io_throttle_group; i++, tg++) {
4079f10af057SSreekanth Reddy 			tg->id = 0;
4080cf1ce8b7SSreekanth Reddy 			tg->fw_qd = 0;
4081cf1ce8b7SSreekanth Reddy 			tg->modified_qd = 0;
4082f10af057SSreekanth Reddy 			tg->io_divert = 0;
4083cf1ce8b7SSreekanth Reddy 			tg->need_qd_reduction = 0;
4084f10af057SSreekanth Reddy 			tg->high = 0;
4085f10af057SSreekanth Reddy 			tg->low = 0;
4086cf1ce8b7SSreekanth Reddy 			tg->qd_reduction = 0;
4087f10af057SSreekanth Reddy 			atomic_set(&tg->pend_large_data_sz, 0);
4088f10af057SSreekanth Reddy 		}
4089f10af057SSreekanth Reddy 	}
4090fb9b0457SKashyap Desai }
4091fb9b0457SKashyap Desai 
4092fb9b0457SKashyap Desai /**
4093824a1566SKashyap Desai  * mpi3mr_free_mem - Free memory allocated for a controller
4094824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4095824a1566SKashyap Desai  *
4096824a1566SKashyap Desai  * Free all the memory allocated for a controller.
4097824a1566SKashyap Desai  *
4098824a1566SKashyap Desai  * Return: Nothing.
4099824a1566SKashyap Desai  */
4100fe6db615SSreekanth Reddy void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
4101824a1566SKashyap Desai {
4102824a1566SKashyap Desai 	u16 i;
4103824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info;
4104824a1566SKashyap Desai 
4105824a1566SKashyap Desai 	if (mrioc->sense_buf_pool) {
4106824a1566SKashyap Desai 		if (mrioc->sense_buf)
4107824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
4108824a1566SKashyap Desai 			    mrioc->sense_buf_dma);
4109824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_pool);
4110824a1566SKashyap Desai 		mrioc->sense_buf = NULL;
4111824a1566SKashyap Desai 		mrioc->sense_buf_pool = NULL;
4112824a1566SKashyap Desai 	}
4113824a1566SKashyap Desai 	if (mrioc->sense_buf_q_pool) {
4114824a1566SKashyap Desai 		if (mrioc->sense_buf_q)
4115824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_q_pool,
4116824a1566SKashyap Desai 			    mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
4117824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_q_pool);
4118824a1566SKashyap Desai 		mrioc->sense_buf_q = NULL;
4119824a1566SKashyap Desai 		mrioc->sense_buf_q_pool = NULL;
4120824a1566SKashyap Desai 	}
4121824a1566SKashyap Desai 
4122824a1566SKashyap Desai 	if (mrioc->reply_buf_pool) {
4123824a1566SKashyap Desai 		if (mrioc->reply_buf)
4124824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
4125824a1566SKashyap Desai 			    mrioc->reply_buf_dma);
4126824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_buf_pool);
4127824a1566SKashyap Desai 		mrioc->reply_buf = NULL;
4128824a1566SKashyap Desai 		mrioc->reply_buf_pool = NULL;
4129824a1566SKashyap Desai 	}
4130824a1566SKashyap Desai 	if (mrioc->reply_free_q_pool) {
4131824a1566SKashyap Desai 		if (mrioc->reply_free_q)
4132824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_free_q_pool,
4133824a1566SKashyap Desai 			    mrioc->reply_free_q, mrioc->reply_free_q_dma);
4134824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_free_q_pool);
4135824a1566SKashyap Desai 		mrioc->reply_free_q = NULL;
4136824a1566SKashyap Desai 		mrioc->reply_free_q_pool = NULL;
4137824a1566SKashyap Desai 	}
4138824a1566SKashyap Desai 
4139c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_req_q; i++)
4140c9566231SKashyap Desai 		mpi3mr_free_op_req_q_segments(mrioc, i);
4141c9566231SKashyap Desai 
4142c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_reply_q; i++)
4143c9566231SKashyap Desai 		mpi3mr_free_op_reply_q_segments(mrioc, i);
4144c9566231SKashyap Desai 
4145824a1566SKashyap Desai 	for (i = 0; i < mrioc->intr_info_count; i++) {
4146824a1566SKashyap Desai 		intr_info = mrioc->intr_info + i;
4147824a1566SKashyap Desai 		intr_info->op_reply_q = NULL;
4148824a1566SKashyap Desai 	}
4149824a1566SKashyap Desai 
4150824a1566SKashyap Desai 	kfree(mrioc->req_qinfo);
4151824a1566SKashyap Desai 	mrioc->req_qinfo = NULL;
4152824a1566SKashyap Desai 	mrioc->num_op_req_q = 0;
4153824a1566SKashyap Desai 
4154824a1566SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
4155824a1566SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
4156824a1566SKashyap Desai 	mrioc->num_op_reply_q = 0;
4157824a1566SKashyap Desai 
4158824a1566SKashyap Desai 	kfree(mrioc->init_cmds.reply);
4159824a1566SKashyap Desai 	mrioc->init_cmds.reply = NULL;
4160824a1566SKashyap Desai 
4161f5e6d5a3SSumit Saxena 	kfree(mrioc->bsg_cmds.reply);
4162f5e6d5a3SSumit Saxena 	mrioc->bsg_cmds.reply = NULL;
4163f5e6d5a3SSumit Saxena 
4164e844adb1SKashyap Desai 	kfree(mrioc->host_tm_cmds.reply);
4165e844adb1SKashyap Desai 	mrioc->host_tm_cmds.reply = NULL;
4166e844adb1SKashyap Desai 
416743ca1100SSumit Saxena 	kfree(mrioc->pel_cmds.reply);
416843ca1100SSumit Saxena 	mrioc->pel_cmds.reply = NULL;
416943ca1100SSumit Saxena 
417043ca1100SSumit Saxena 	kfree(mrioc->pel_abort_cmd.reply);
417143ca1100SSumit Saxena 	mrioc->pel_abort_cmd.reply = NULL;
417243ca1100SSumit Saxena 
4173c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
4174c1af985dSSreekanth Reddy 		kfree(mrioc->evtack_cmds[i].reply);
4175c1af985dSSreekanth Reddy 		mrioc->evtack_cmds[i].reply = NULL;
4176c1af985dSSreekanth Reddy 	}
4177c1af985dSSreekanth Reddy 
4178e844adb1SKashyap Desai 	kfree(mrioc->removepend_bitmap);
4179e844adb1SKashyap Desai 	mrioc->removepend_bitmap = NULL;
4180e844adb1SKashyap Desai 
4181e844adb1SKashyap Desai 	kfree(mrioc->devrem_bitmap);
4182e844adb1SKashyap Desai 	mrioc->devrem_bitmap = NULL;
4183e844adb1SKashyap Desai 
4184c1af985dSSreekanth Reddy 	kfree(mrioc->evtack_cmds_bitmap);
4185c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap = NULL;
4186c1af985dSSreekanth Reddy 
4187824a1566SKashyap Desai 	kfree(mrioc->chain_bitmap);
4188824a1566SKashyap Desai 	mrioc->chain_bitmap = NULL;
4189824a1566SKashyap Desai 
419013ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
419113ef29eaSKashyap Desai 		kfree(mrioc->dev_rmhs_cmds[i].reply);
419213ef29eaSKashyap Desai 		mrioc->dev_rmhs_cmds[i].reply = NULL;
419313ef29eaSKashyap Desai 	}
419413ef29eaSKashyap Desai 
4195824a1566SKashyap Desai 	if (mrioc->chain_buf_pool) {
4196824a1566SKashyap Desai 		for (i = 0; i < mrioc->chain_buf_count; i++) {
4197824a1566SKashyap Desai 			if (mrioc->chain_sgl_list[i].addr) {
4198824a1566SKashyap Desai 				dma_pool_free(mrioc->chain_buf_pool,
4199824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].addr,
4200824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].dma_addr);
4201824a1566SKashyap Desai 				mrioc->chain_sgl_list[i].addr = NULL;
4202824a1566SKashyap Desai 			}
4203824a1566SKashyap Desai 		}
4204824a1566SKashyap Desai 		dma_pool_destroy(mrioc->chain_buf_pool);
4205824a1566SKashyap Desai 		mrioc->chain_buf_pool = NULL;
4206824a1566SKashyap Desai 	}
4207824a1566SKashyap Desai 
4208824a1566SKashyap Desai 	kfree(mrioc->chain_sgl_list);
4209824a1566SKashyap Desai 	mrioc->chain_sgl_list = NULL;
4210824a1566SKashyap Desai 
4211824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
4212824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
4213824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
4214824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
4215824a1566SKashyap Desai 	}
4216824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
4217824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
4218824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
4219824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
4220824a1566SKashyap Desai 	}
422143ca1100SSumit Saxena 
422243ca1100SSumit Saxena 	if (mrioc->pel_seqnum_virt) {
422343ca1100SSumit Saxena 		dma_free_coherent(&mrioc->pdev->dev, mrioc->pel_seqnum_sz,
422443ca1100SSumit Saxena 		    mrioc->pel_seqnum_virt, mrioc->pel_seqnum_dma);
422543ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = NULL;
422643ca1100SSumit Saxena 	}
422743ca1100SSumit Saxena 
422843ca1100SSumit Saxena 	kfree(mrioc->logdata_buf);
422943ca1100SSumit Saxena 	mrioc->logdata_buf = NULL;
423043ca1100SSumit Saxena 
4231824a1566SKashyap Desai }
4232824a1566SKashyap Desai 
4233824a1566SKashyap Desai /**
4234824a1566SKashyap Desai  * mpi3mr_issue_ioc_shutdown - shutdown controller
4235824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4236824a1566SKashyap Desai  *
4237824a1566SKashyap Desai  * Send shutodwn notification to the controller and wait for the
4238824a1566SKashyap Desai  * shutdown_timeout for it to be completed.
4239824a1566SKashyap Desai  *
4240824a1566SKashyap Desai  * Return: Nothing.
4241824a1566SKashyap Desai  */
4242824a1566SKashyap Desai static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
4243824a1566SKashyap Desai {
4244824a1566SKashyap Desai 	u32 ioc_config, ioc_status;
4245824a1566SKashyap Desai 	u8 retval = 1;
4246824a1566SKashyap Desai 	u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
4247824a1566SKashyap Desai 
4248824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing shutdown Notification\n");
4249824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
4250824a1566SKashyap Desai 		ioc_warn(mrioc,
4251824a1566SKashyap Desai 		    "IOC is unrecoverable shutdown is not issued\n");
4252824a1566SKashyap Desai 		return;
4253824a1566SKashyap Desai 	}
4254824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4255824a1566SKashyap Desai 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4256824a1566SKashyap Desai 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
4257824a1566SKashyap Desai 		ioc_info(mrioc, "shutdown already in progress\n");
4258824a1566SKashyap Desai 		return;
4259824a1566SKashyap Desai 	}
4260824a1566SKashyap Desai 
4261824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
4262824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
4263ec5ebd2cSSreekanth Reddy 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
4264824a1566SKashyap Desai 
4265824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
4266824a1566SKashyap Desai 
4267824a1566SKashyap Desai 	if (mrioc->facts.shutdown_timeout)
4268824a1566SKashyap Desai 		timeout = mrioc->facts.shutdown_timeout * 10;
4269824a1566SKashyap Desai 
4270824a1566SKashyap Desai 	do {
4271824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4272824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4273824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
4274824a1566SKashyap Desai 			retval = 0;
4275824a1566SKashyap Desai 			break;
4276824a1566SKashyap Desai 		}
4277824a1566SKashyap Desai 		msleep(100);
4278824a1566SKashyap Desai 	} while (--timeout);
4279824a1566SKashyap Desai 
4280824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4281824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
4282824a1566SKashyap Desai 
4283824a1566SKashyap Desai 	if (retval) {
4284824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4285824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
4286824a1566SKashyap Desai 			ioc_warn(mrioc,
4287824a1566SKashyap Desai 			    "shutdown still in progress after timeout\n");
4288824a1566SKashyap Desai 	}
4289824a1566SKashyap Desai 
4290824a1566SKashyap Desai 	ioc_info(mrioc,
4291824a1566SKashyap Desai 	    "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
4292824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status,
4293824a1566SKashyap Desai 	    ioc_config);
4294824a1566SKashyap Desai }
4295824a1566SKashyap Desai 
4296824a1566SKashyap Desai /**
4297824a1566SKashyap Desai  * mpi3mr_cleanup_ioc - Cleanup controller
4298824a1566SKashyap Desai  * @mrioc: Adapter instance reference
42993bb3c24eSYang Li  *
4300824a1566SKashyap Desai  * controller cleanup handler, Message unit reset or soft reset
4301fe6db615SSreekanth Reddy  * and shutdown notification is issued to the controller.
4302824a1566SKashyap Desai  *
4303824a1566SKashyap Desai  * Return: Nothing.
4304824a1566SKashyap Desai  */
4305fe6db615SSreekanth Reddy void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)
4306824a1566SKashyap Desai {
4307824a1566SKashyap Desai 	enum mpi3mr_iocstate ioc_state;
4308824a1566SKashyap Desai 
4309fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "cleaning up the controller\n");
4310824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4311824a1566SKashyap Desai 
4312824a1566SKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
4313824a1566SKashyap Desai 
4314824a1566SKashyap Desai 	if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
4315824a1566SKashyap Desai 	    (ioc_state == MRIOC_STATE_READY)) {
4316824a1566SKashyap Desai 		if (mpi3mr_issue_and_process_mur(mrioc,
4317824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
4318824a1566SKashyap Desai 			mpi3mr_issue_reset(mrioc,
4319824a1566SKashyap Desai 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
4320824a1566SKashyap Desai 			    MPI3MR_RESET_FROM_MUR_FAILURE);
4321824a1566SKashyap Desai 		mpi3mr_issue_ioc_shutdown(mrioc);
4322824a1566SKashyap Desai 	}
4323fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "controller cleanup completed\n");
4324fb9b0457SKashyap Desai }
4325fb9b0457SKashyap Desai 
4326fb9b0457SKashyap Desai /**
4327fb9b0457SKashyap Desai  * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
4328fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4329fb9b0457SKashyap Desai  * @cmdptr: Internal command tracker
4330fb9b0457SKashyap Desai  *
4331fb9b0457SKashyap Desai  * Complete an internal driver commands with state indicating it
4332fb9b0457SKashyap Desai  * is completed due to reset.
4333fb9b0457SKashyap Desai  *
4334fb9b0457SKashyap Desai  * Return: Nothing.
4335fb9b0457SKashyap Desai  */
4336fb9b0457SKashyap Desai static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc,
4337fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr)
4338fb9b0457SKashyap Desai {
4339fb9b0457SKashyap Desai 	if (cmdptr->state & MPI3MR_CMD_PENDING) {
4340fb9b0457SKashyap Desai 		cmdptr->state |= MPI3MR_CMD_RESET;
4341fb9b0457SKashyap Desai 		cmdptr->state &= ~MPI3MR_CMD_PENDING;
4342fb9b0457SKashyap Desai 		if (cmdptr->is_waiting) {
4343fb9b0457SKashyap Desai 			complete(&cmdptr->done);
4344fb9b0457SKashyap Desai 			cmdptr->is_waiting = 0;
4345fb9b0457SKashyap Desai 		} else if (cmdptr->callback)
4346fb9b0457SKashyap Desai 			cmdptr->callback(mrioc, cmdptr);
4347fb9b0457SKashyap Desai 	}
4348fb9b0457SKashyap Desai }
4349fb9b0457SKashyap Desai 
4350fb9b0457SKashyap Desai /**
4351fb9b0457SKashyap Desai  * mpi3mr_flush_drv_cmds - Flush internaldriver commands
4352fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4353fb9b0457SKashyap Desai  *
4354fb9b0457SKashyap Desai  * Flush all internal driver commands post reset
4355fb9b0457SKashyap Desai  *
4356fb9b0457SKashyap Desai  * Return: Nothing.
4357fb9b0457SKashyap Desai  */
4358fb9b0457SKashyap Desai static void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc)
4359fb9b0457SKashyap Desai {
4360fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr;
4361fb9b0457SKashyap Desai 	u8 i;
4362fb9b0457SKashyap Desai 
4363fb9b0457SKashyap Desai 	cmdptr = &mrioc->init_cmds;
4364fb9b0457SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4365f5e6d5a3SSumit Saxena 	cmdptr = &mrioc->bsg_cmds;
4366f5e6d5a3SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4367e844adb1SKashyap Desai 	cmdptr = &mrioc->host_tm_cmds;
4368e844adb1SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4369fb9b0457SKashyap Desai 
4370fb9b0457SKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
4371fb9b0457SKashyap Desai 		cmdptr = &mrioc->dev_rmhs_cmds[i];
4372fb9b0457SKashyap Desai 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4373fb9b0457SKashyap Desai 	}
4374c1af985dSSreekanth Reddy 
4375c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
4376c1af985dSSreekanth Reddy 		cmdptr = &mrioc->evtack_cmds[i];
4377c1af985dSSreekanth Reddy 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4378c1af985dSSreekanth Reddy 	}
437943ca1100SSumit Saxena 
438043ca1100SSumit Saxena 	cmdptr = &mrioc->pel_cmds;
438143ca1100SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
438243ca1100SSumit Saxena 
438343ca1100SSumit Saxena 	cmdptr = &mrioc->pel_abort_cmd;
438443ca1100SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
438543ca1100SSumit Saxena 
438643ca1100SSumit Saxena }
438743ca1100SSumit Saxena 
438843ca1100SSumit Saxena /**
438943ca1100SSumit Saxena  * mpi3mr_pel_wait_post - Issue PEL Wait
439043ca1100SSumit Saxena  * @mrioc: Adapter instance reference
439143ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
439243ca1100SSumit Saxena  *
439343ca1100SSumit Saxena  * Issue PEL Wait MPI request through admin queue and return.
439443ca1100SSumit Saxena  *
439543ca1100SSumit Saxena  * Return: Nothing.
439643ca1100SSumit Saxena  */
439743ca1100SSumit Saxena static void mpi3mr_pel_wait_post(struct mpi3mr_ioc *mrioc,
439843ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
439943ca1100SSumit Saxena {
440043ca1100SSumit Saxena 	struct mpi3_pel_req_action_wait pel_wait;
440143ca1100SSumit Saxena 
440243ca1100SSumit Saxena 	mrioc->pel_abort_requested = false;
440343ca1100SSumit Saxena 
440443ca1100SSumit Saxena 	memset(&pel_wait, 0, sizeof(pel_wait));
440543ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_PENDING;
440643ca1100SSumit Saxena 	drv_cmd->is_waiting = 0;
440743ca1100SSumit Saxena 	drv_cmd->callback = mpi3mr_pel_wait_complete;
440843ca1100SSumit Saxena 	drv_cmd->ioc_status = 0;
440943ca1100SSumit Saxena 	drv_cmd->ioc_loginfo = 0;
441043ca1100SSumit Saxena 	pel_wait.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
441143ca1100SSumit Saxena 	pel_wait.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
441243ca1100SSumit Saxena 	pel_wait.action = MPI3_PEL_ACTION_WAIT;
441343ca1100SSumit Saxena 	pel_wait.starting_sequence_number = cpu_to_le32(mrioc->pel_newest_seqnum);
441443ca1100SSumit Saxena 	pel_wait.locale = cpu_to_le16(mrioc->pel_locale);
441543ca1100SSumit Saxena 	pel_wait.class = cpu_to_le16(mrioc->pel_class);
441643ca1100SSumit Saxena 	pel_wait.wait_time = MPI3_PEL_WAITTIME_INFINITE_WAIT;
441743ca1100SSumit Saxena 	dprint_bsg_info(mrioc, "sending pel_wait seqnum(%d), class(%d), locale(0x%08x)\n",
441843ca1100SSumit Saxena 	    mrioc->pel_newest_seqnum, mrioc->pel_class, mrioc->pel_locale);
441943ca1100SSumit Saxena 
442043ca1100SSumit Saxena 	if (mpi3mr_admin_request_post(mrioc, &pel_wait, sizeof(pel_wait), 0)) {
442143ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
442243ca1100SSumit Saxena 			    "Issuing PELWait: Admin post failed\n");
442343ca1100SSumit Saxena 		drv_cmd->state = MPI3MR_CMD_NOTUSED;
442443ca1100SSumit Saxena 		drv_cmd->callback = NULL;
442543ca1100SSumit Saxena 		drv_cmd->retry_count = 0;
442643ca1100SSumit Saxena 		mrioc->pel_enabled = false;
442743ca1100SSumit Saxena 	}
442843ca1100SSumit Saxena }
442943ca1100SSumit Saxena 
443043ca1100SSumit Saxena /**
443143ca1100SSumit Saxena  * mpi3mr_pel_get_seqnum_post - Issue PEL Get Sequence number
443243ca1100SSumit Saxena  * @mrioc: Adapter instance reference
443343ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
443443ca1100SSumit Saxena  *
443543ca1100SSumit Saxena  * Issue PEL get sequence number MPI request through admin queue
443643ca1100SSumit Saxena  * and return.
443743ca1100SSumit Saxena  *
443843ca1100SSumit Saxena  * Return: 0 on success, non-zero on failure.
443943ca1100SSumit Saxena  */
444043ca1100SSumit Saxena int mpi3mr_pel_get_seqnum_post(struct mpi3mr_ioc *mrioc,
444143ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
444243ca1100SSumit Saxena {
444343ca1100SSumit Saxena 	struct mpi3_pel_req_action_get_sequence_numbers pel_getseq_req;
444443ca1100SSumit Saxena 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
444543ca1100SSumit Saxena 	int retval = 0;
444643ca1100SSumit Saxena 
444743ca1100SSumit Saxena 	memset(&pel_getseq_req, 0, sizeof(pel_getseq_req));
444843ca1100SSumit Saxena 	mrioc->pel_cmds.state = MPI3MR_CMD_PENDING;
444943ca1100SSumit Saxena 	mrioc->pel_cmds.is_waiting = 0;
445043ca1100SSumit Saxena 	mrioc->pel_cmds.ioc_status = 0;
445143ca1100SSumit Saxena 	mrioc->pel_cmds.ioc_loginfo = 0;
445243ca1100SSumit Saxena 	mrioc->pel_cmds.callback = mpi3mr_pel_get_seqnum_complete;
445343ca1100SSumit Saxena 	pel_getseq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
445443ca1100SSumit Saxena 	pel_getseq_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
445543ca1100SSumit Saxena 	pel_getseq_req.action = MPI3_PEL_ACTION_GET_SEQNUM;
445643ca1100SSumit Saxena 	mpi3mr_add_sg_single(&pel_getseq_req.sgl, sgl_flags,
445743ca1100SSumit Saxena 	    mrioc->pel_seqnum_sz, mrioc->pel_seqnum_dma);
445843ca1100SSumit Saxena 
445943ca1100SSumit Saxena 	retval = mpi3mr_admin_request_post(mrioc, &pel_getseq_req,
446043ca1100SSumit Saxena 			sizeof(pel_getseq_req), 0);
446143ca1100SSumit Saxena 	if (retval) {
446243ca1100SSumit Saxena 		if (drv_cmd) {
446343ca1100SSumit Saxena 			drv_cmd->state = MPI3MR_CMD_NOTUSED;
446443ca1100SSumit Saxena 			drv_cmd->callback = NULL;
446543ca1100SSumit Saxena 			drv_cmd->retry_count = 0;
446643ca1100SSumit Saxena 		}
446743ca1100SSumit Saxena 		mrioc->pel_enabled = false;
446843ca1100SSumit Saxena 	}
446943ca1100SSumit Saxena 
447043ca1100SSumit Saxena 	return retval;
447143ca1100SSumit Saxena }
447243ca1100SSumit Saxena 
447343ca1100SSumit Saxena /**
447443ca1100SSumit Saxena  * mpi3mr_pel_wait_complete - PELWait Completion callback
447543ca1100SSumit Saxena  * @mrioc: Adapter instance reference
447643ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
447743ca1100SSumit Saxena  *
447843ca1100SSumit Saxena  * This is a callback handler for the PELWait request and
447943ca1100SSumit Saxena  * firmware completes a PELWait request when it is aborted or a
448043ca1100SSumit Saxena  * new PEL entry is available. This sends AEN to the application
448143ca1100SSumit Saxena  * and if the PELwait completion is not due to PELAbort then
448243ca1100SSumit Saxena  * this will send a request for new PEL Sequence number
448343ca1100SSumit Saxena  *
448443ca1100SSumit Saxena  * Return: Nothing.
448543ca1100SSumit Saxena  */
448643ca1100SSumit Saxena static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc,
448743ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
448843ca1100SSumit Saxena {
448943ca1100SSumit Saxena 	struct mpi3_pel_reply *pel_reply = NULL;
449043ca1100SSumit Saxena 	u16 ioc_status, pe_log_status;
449143ca1100SSumit Saxena 	bool do_retry = false;
449243ca1100SSumit Saxena 
449343ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_RESET)
449443ca1100SSumit Saxena 		goto cleanup_drv_cmd;
449543ca1100SSumit Saxena 
449643ca1100SSumit Saxena 	ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
449743ca1100SSumit Saxena 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
449843ca1100SSumit Saxena 		ioc_err(mrioc, "%s: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
449943ca1100SSumit Saxena 			__func__, ioc_status, drv_cmd->ioc_loginfo);
450043ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
450143ca1100SSumit Saxena 		    "pel_wait: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
450243ca1100SSumit Saxena 		    ioc_status, drv_cmd->ioc_loginfo);
450343ca1100SSumit Saxena 		do_retry = true;
450443ca1100SSumit Saxena 	}
450543ca1100SSumit Saxena 
450643ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
450743ca1100SSumit Saxena 		pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
450843ca1100SSumit Saxena 
450943ca1100SSumit Saxena 	if (!pel_reply) {
451043ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
451143ca1100SSumit Saxena 		    "pel_wait: failed due to no reply\n");
451243ca1100SSumit Saxena 		goto out_failed;
451343ca1100SSumit Saxena 	}
451443ca1100SSumit Saxena 
451543ca1100SSumit Saxena 	pe_log_status = le16_to_cpu(pel_reply->pe_log_status);
451643ca1100SSumit Saxena 	if ((pe_log_status != MPI3_PEL_STATUS_SUCCESS) &&
451743ca1100SSumit Saxena 	    (pe_log_status != MPI3_PEL_STATUS_ABORTED)) {
451843ca1100SSumit Saxena 		ioc_err(mrioc, "%s: Failed pe_log_status(0x%04x)\n",
451943ca1100SSumit Saxena 			__func__, pe_log_status);
452043ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
452143ca1100SSumit Saxena 		    "pel_wait: failed due to pel_log_status(0x%04x)\n",
452243ca1100SSumit Saxena 		    pe_log_status);
452343ca1100SSumit Saxena 		do_retry = true;
452443ca1100SSumit Saxena 	}
452543ca1100SSumit Saxena 
452643ca1100SSumit Saxena 	if (do_retry) {
452743ca1100SSumit Saxena 		if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
452843ca1100SSumit Saxena 			drv_cmd->retry_count++;
452943ca1100SSumit Saxena 			dprint_bsg_err(mrioc, "pel_wait: retrying(%d)\n",
453043ca1100SSumit Saxena 			    drv_cmd->retry_count);
453143ca1100SSumit Saxena 			mpi3mr_pel_wait_post(mrioc, drv_cmd);
453243ca1100SSumit Saxena 			return;
453343ca1100SSumit Saxena 		}
453443ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
453543ca1100SSumit Saxena 		    "pel_wait: failed after all retries(%d)\n",
453643ca1100SSumit Saxena 		    drv_cmd->retry_count);
453743ca1100SSumit Saxena 		goto out_failed;
453843ca1100SSumit Saxena 	}
453943ca1100SSumit Saxena 	atomic64_inc(&event_counter);
454043ca1100SSumit Saxena 	if (!mrioc->pel_abort_requested) {
454143ca1100SSumit Saxena 		mrioc->pel_cmds.retry_count = 0;
454243ca1100SSumit Saxena 		mpi3mr_pel_get_seqnum_post(mrioc, &mrioc->pel_cmds);
454343ca1100SSumit Saxena 	}
454443ca1100SSumit Saxena 
454543ca1100SSumit Saxena 	return;
454643ca1100SSumit Saxena out_failed:
454743ca1100SSumit Saxena 	mrioc->pel_enabled = false;
454843ca1100SSumit Saxena cleanup_drv_cmd:
454943ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
455043ca1100SSumit Saxena 	drv_cmd->callback = NULL;
455143ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
455243ca1100SSumit Saxena }
455343ca1100SSumit Saxena 
455443ca1100SSumit Saxena /**
455543ca1100SSumit Saxena  * mpi3mr_pel_get_seqnum_complete - PELGetSeqNum Completion callback
455643ca1100SSumit Saxena  * @mrioc: Adapter instance reference
455743ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
455843ca1100SSumit Saxena  *
455943ca1100SSumit Saxena  * This is a callback handler for the PEL get sequence number
456043ca1100SSumit Saxena  * request and a new PEL wait request will be issued to the
456143ca1100SSumit Saxena  * firmware from this
456243ca1100SSumit Saxena  *
456343ca1100SSumit Saxena  * Return: Nothing.
456443ca1100SSumit Saxena  */
456543ca1100SSumit Saxena void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc,
456643ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
456743ca1100SSumit Saxena {
456843ca1100SSumit Saxena 	struct mpi3_pel_reply *pel_reply = NULL;
456943ca1100SSumit Saxena 	struct mpi3_pel_seq *pel_seqnum_virt;
457043ca1100SSumit Saxena 	u16 ioc_status;
457143ca1100SSumit Saxena 	bool do_retry = false;
457243ca1100SSumit Saxena 
457343ca1100SSumit Saxena 	pel_seqnum_virt = (struct mpi3_pel_seq *)mrioc->pel_seqnum_virt;
457443ca1100SSumit Saxena 
457543ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_RESET)
457643ca1100SSumit Saxena 		goto cleanup_drv_cmd;
457743ca1100SSumit Saxena 
457843ca1100SSumit Saxena 	ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
457943ca1100SSumit Saxena 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
458043ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
458143ca1100SSumit Saxena 		    "pel_get_seqnum: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
458243ca1100SSumit Saxena 		    ioc_status, drv_cmd->ioc_loginfo);
458343ca1100SSumit Saxena 		do_retry = true;
458443ca1100SSumit Saxena 	}
458543ca1100SSumit Saxena 
458643ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
458743ca1100SSumit Saxena 		pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
458843ca1100SSumit Saxena 	if (!pel_reply) {
458943ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
459043ca1100SSumit Saxena 		    "pel_get_seqnum: failed due to no reply\n");
459143ca1100SSumit Saxena 		goto out_failed;
459243ca1100SSumit Saxena 	}
459343ca1100SSumit Saxena 
459443ca1100SSumit Saxena 	if (le16_to_cpu(pel_reply->pe_log_status) != MPI3_PEL_STATUS_SUCCESS) {
459543ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
459643ca1100SSumit Saxena 		    "pel_get_seqnum: failed due to pel_log_status(0x%04x)\n",
459743ca1100SSumit Saxena 		    le16_to_cpu(pel_reply->pe_log_status));
459843ca1100SSumit Saxena 		do_retry = true;
459943ca1100SSumit Saxena 	}
460043ca1100SSumit Saxena 
460143ca1100SSumit Saxena 	if (do_retry) {
460243ca1100SSumit Saxena 		if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
460343ca1100SSumit Saxena 			drv_cmd->retry_count++;
460443ca1100SSumit Saxena 			dprint_bsg_err(mrioc,
460543ca1100SSumit Saxena 			    "pel_get_seqnum: retrying(%d)\n",
460643ca1100SSumit Saxena 			    drv_cmd->retry_count);
460743ca1100SSumit Saxena 			mpi3mr_pel_get_seqnum_post(mrioc, drv_cmd);
460843ca1100SSumit Saxena 			return;
460943ca1100SSumit Saxena 		}
461043ca1100SSumit Saxena 
461143ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
461243ca1100SSumit Saxena 		    "pel_get_seqnum: failed after all retries(%d)\n",
461343ca1100SSumit Saxena 		    drv_cmd->retry_count);
461443ca1100SSumit Saxena 		goto out_failed;
461543ca1100SSumit Saxena 	}
461643ca1100SSumit Saxena 	mrioc->pel_newest_seqnum = le32_to_cpu(pel_seqnum_virt->newest) + 1;
461743ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
461843ca1100SSumit Saxena 	mpi3mr_pel_wait_post(mrioc, drv_cmd);
461943ca1100SSumit Saxena 
462043ca1100SSumit Saxena 	return;
462143ca1100SSumit Saxena out_failed:
462243ca1100SSumit Saxena 	mrioc->pel_enabled = false;
462343ca1100SSumit Saxena cleanup_drv_cmd:
462443ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
462543ca1100SSumit Saxena 	drv_cmd->callback = NULL;
462643ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
4627fb9b0457SKashyap Desai }
4628fb9b0457SKashyap Desai 
4629fb9b0457SKashyap Desai /**
4630824a1566SKashyap Desai  * mpi3mr_soft_reset_handler - Reset the controller
4631824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4632824a1566SKashyap Desai  * @reset_reason: Reset reason code
4633824a1566SKashyap Desai  * @snapdump: Flag to generate snapdump in firmware or not
4634824a1566SKashyap Desai  *
4635fb9b0457SKashyap Desai  * This is an handler for recovering controller by issuing soft
4636fb9b0457SKashyap Desai  * reset are diag fault reset.  This is a blocking function and
4637fb9b0457SKashyap Desai  * when one reset is executed if any other resets they will be
4638f5e6d5a3SSumit Saxena  * blocked. All BSG requests will be blocked during the reset. If
4639fb9b0457SKashyap Desai  * controller reset is successful then the controller will be
4640fb9b0457SKashyap Desai  * reinitalized, otherwise the controller will be marked as not
4641fb9b0457SKashyap Desai  * recoverable
4642fb9b0457SKashyap Desai  *
4643fb9b0457SKashyap Desai  * In snapdump bit is set, the controller is issued with diag
4644fb9b0457SKashyap Desai  * fault reset so that the firmware can create a snap dump and
4645fb9b0457SKashyap Desai  * post that the firmware will result in F000 fault and the
4646fb9b0457SKashyap Desai  * driver will issue soft reset to recover from that.
4647824a1566SKashyap Desai  *
4648824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
4649824a1566SKashyap Desai  */
4650824a1566SKashyap Desai int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
4651824a1566SKashyap Desai 	u32 reset_reason, u8 snapdump)
4652824a1566SKashyap Desai {
4653fb9b0457SKashyap Desai 	int retval = 0, i;
4654fb9b0457SKashyap Desai 	unsigned long flags;
4655fb9b0457SKashyap Desai 	u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
4656fb9b0457SKashyap Desai 
4657b64845a7SSreekanth Reddy 	/* Block the reset handler until diag save in progress*/
4658b64845a7SSreekanth Reddy 	dprint_reset(mrioc,
4659b64845a7SSreekanth Reddy 	    "soft_reset_handler: check and block on diagsave_timeout(%d)\n",
4660b64845a7SSreekanth Reddy 	    mrioc->diagsave_timeout);
4661b64845a7SSreekanth Reddy 	while (mrioc->diagsave_timeout)
4662b64845a7SSreekanth Reddy 		ssleep(1);
4663fb9b0457SKashyap Desai 	/*
4664fb9b0457SKashyap Desai 	 * Block new resets until the currently executing one is finished and
4665fb9b0457SKashyap Desai 	 * return the status of the existing reset for all blocked resets
4666fb9b0457SKashyap Desai 	 */
4667b64845a7SSreekanth Reddy 	dprint_reset(mrioc, "soft_reset_handler: acquiring reset_mutex\n");
4668fb9b0457SKashyap Desai 	if (!mutex_trylock(&mrioc->reset_mutex)) {
4669b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4670b64845a7SSreekanth Reddy 		    "controller reset triggered by %s is blocked due to another reset in progress\n",
4671b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4672b64845a7SSreekanth Reddy 		do {
4673b64845a7SSreekanth Reddy 			ssleep(1);
4674b64845a7SSreekanth Reddy 		} while (mrioc->reset_in_progress == 1);
4675b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4676b64845a7SSreekanth Reddy 		    "returning previous reset result(%d) for the reset triggered by %s\n",
4677b64845a7SSreekanth Reddy 		    mrioc->prev_reset_result,
4678b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4679b64845a7SSreekanth Reddy 		return mrioc->prev_reset_result;
4680fb9b0457SKashyap Desai 	}
4681b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is triggered by %s\n",
4682b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason));
4683b64845a7SSreekanth Reddy 
4684fb9b0457SKashyap Desai 	mrioc->reset_in_progress = 1;
4685f5e6d5a3SSumit Saxena 	mrioc->stop_bsgs = 1;
4686b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = -1;
4687fb9b0457SKashyap Desai 
4688fb9b0457SKashyap Desai 	if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
4689b64845a7SSreekanth Reddy 	    (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) &&
4690fb9b0457SKashyap Desai 	    (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
4691fb9b0457SKashyap Desai 		for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4692fb9b0457SKashyap Desai 			mrioc->event_masks[i] = -1;
4693fb9b0457SKashyap Desai 
4694b64845a7SSreekanth Reddy 		dprint_reset(mrioc, "soft_reset_handler: masking events\n");
4695b64845a7SSreekanth Reddy 		mpi3mr_issue_event_notification(mrioc);
4696fb9b0457SKashyap Desai 	}
4697fb9b0457SKashyap Desai 
469844dc724fSKashyap Desai 	mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT);
469944dc724fSKashyap Desai 
4700fb9b0457SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4701fb9b0457SKashyap Desai 
4702fb9b0457SKashyap Desai 	if (snapdump) {
4703fb9b0457SKashyap Desai 		mpi3mr_set_diagsave(mrioc);
4704fb9b0457SKashyap Desai 		retval = mpi3mr_issue_reset(mrioc,
4705fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4706fb9b0457SKashyap Desai 		if (!retval) {
4707fb9b0457SKashyap Desai 			do {
4708fb9b0457SKashyap Desai 				host_diagnostic =
4709fb9b0457SKashyap Desai 				    readl(&mrioc->sysif_regs->host_diagnostic);
4710fb9b0457SKashyap Desai 				if (!(host_diagnostic &
4711fb9b0457SKashyap Desai 				    MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
4712fb9b0457SKashyap Desai 					break;
4713fb9b0457SKashyap Desai 				msleep(100);
4714fb9b0457SKashyap Desai 			} while (--timeout);
4715fb9b0457SKashyap Desai 		}
4716fb9b0457SKashyap Desai 	}
4717fb9b0457SKashyap Desai 
4718fb9b0457SKashyap Desai 	retval = mpi3mr_issue_reset(mrioc,
4719fb9b0457SKashyap Desai 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
4720fb9b0457SKashyap Desai 	if (retval) {
4721fb9b0457SKashyap Desai 		ioc_err(mrioc, "Failed to issue soft reset to the ioc\n");
4722fb9b0457SKashyap Desai 		goto out;
4723fb9b0457SKashyap Desai 	}
4724f10af057SSreekanth Reddy 	if (mrioc->num_io_throttle_group !=
4725f10af057SSreekanth Reddy 	    mrioc->facts.max_io_throttle_group) {
4726f10af057SSreekanth Reddy 		ioc_err(mrioc,
4727f10af057SSreekanth Reddy 		    "max io throttle group doesn't match old(%d), new(%d)\n",
4728f10af057SSreekanth Reddy 		    mrioc->num_io_throttle_group,
4729f10af057SSreekanth Reddy 		    mrioc->facts.max_io_throttle_group);
4730*2a8a0147SDan Carpenter 		retval = -EPERM;
4731*2a8a0147SDan Carpenter 		goto out;
4732f10af057SSreekanth Reddy 	}
4733fb9b0457SKashyap Desai 
4734c1af985dSSreekanth Reddy 	mpi3mr_flush_delayed_cmd_lists(mrioc);
4735fb9b0457SKashyap Desai 	mpi3mr_flush_drv_cmds(mrioc);
4736fb9b0457SKashyap Desai 	memset(mrioc->devrem_bitmap, 0, mrioc->devrem_bitmap_sz);
4737fb9b0457SKashyap Desai 	memset(mrioc->removepend_bitmap, 0, mrioc->dev_handle_bitmap_sz);
4738c1af985dSSreekanth Reddy 	memset(mrioc->evtack_cmds_bitmap, 0, mrioc->evtack_cmds_bitmap_sz);
4739fb9b0457SKashyap Desai 	mpi3mr_flush_host_io(mrioc);
4740580e6742SSreekanth Reddy 	mpi3mr_cleanup_fwevt_list(mrioc);
4741fb9b0457SKashyap Desai 	mpi3mr_invalidate_devhandles(mrioc);
474278b76a07SSreekanth Reddy 	if (mrioc->prepare_for_reset) {
474378b76a07SSreekanth Reddy 		mrioc->prepare_for_reset = 0;
474478b76a07SSreekanth Reddy 		mrioc->prepare_for_reset_timeout_counter = 0;
474578b76a07SSreekanth Reddy 	}
4746fb9b0457SKashyap Desai 	mpi3mr_memset_buffers(mrioc);
4747fe6db615SSreekanth Reddy 	retval = mpi3mr_reinit_ioc(mrioc, 0);
4748fb9b0457SKashyap Desai 	if (retval) {
4749fb9b0457SKashyap Desai 		pr_err(IOCNAME "reinit after soft reset failed: reason %d\n",
4750fb9b0457SKashyap Desai 		    mrioc->name, reset_reason);
4751fb9b0457SKashyap Desai 		goto out;
4752fb9b0457SKashyap Desai 	}
4753fb9b0457SKashyap Desai 	ssleep(10);
4754fb9b0457SKashyap Desai 
4755fb9b0457SKashyap Desai out:
4756fb9b0457SKashyap Desai 	if (!retval) {
4757b64845a7SSreekanth Reddy 		mrioc->diagsave_timeout = 0;
4758fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
475943ca1100SSumit Saxena 		mrioc->pel_abort_requested = 0;
476043ca1100SSumit Saxena 		if (mrioc->pel_enabled) {
476143ca1100SSumit Saxena 			mrioc->pel_cmds.retry_count = 0;
476243ca1100SSumit Saxena 			mpi3mr_pel_wait_post(mrioc, &mrioc->pel_cmds);
476343ca1100SSumit Saxena 		}
476443ca1100SSumit Saxena 
4765fb9b0457SKashyap Desai 		mpi3mr_rfresh_tgtdevs(mrioc);
476654dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
4767fb9b0457SKashyap Desai 		spin_lock_irqsave(&mrioc->watchdog_lock, flags);
4768fb9b0457SKashyap Desai 		if (mrioc->watchdog_work_q)
4769fb9b0457SKashyap Desai 			queue_delayed_work(mrioc->watchdog_work_q,
4770fb9b0457SKashyap Desai 			    &mrioc->watchdog_work,
4771fb9b0457SKashyap Desai 			    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
4772fb9b0457SKashyap Desai 		spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
4773f5e6d5a3SSumit Saxena 		mrioc->stop_bsgs = 0;
477443ca1100SSumit Saxena 		if (mrioc->pel_enabled)
477543ca1100SSumit Saxena 			atomic64_inc(&event_counter);
4776fb9b0457SKashyap Desai 	} else {
4777fb9b0457SKashyap Desai 		mpi3mr_issue_reset(mrioc,
4778fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4779fb9b0457SKashyap Desai 		mrioc->unrecoverable = 1;
4780fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
4781fb9b0457SKashyap Desai 		retval = -1;
4782fb9b0457SKashyap Desai 	}
4783b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = retval;
4784fb9b0457SKashyap Desai 	mutex_unlock(&mrioc->reset_mutex);
4785b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is %s\n",
4786b64845a7SSreekanth Reddy 	    ((retval == 0) ? "successful" : "failed"));
4787fb9b0457SKashyap Desai 	return retval;
4788824a1566SKashyap Desai }
4789